os/kernelhwsrv/kerneltest/e32test/usb/t_usb_win/src/eject.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/usb/t_usb_win/src/eject.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,151 @@
     1.4 +// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of the License "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// e32test\usb\t_usb_win\src\eject.cpp
    1.18 +// eject functions
    1.19 +//
    1.20 +
    1.21 +#include "stdafx.h"
    1.22 +#include <windows.h>
    1.23 +#include <winioctl.h>
    1.24 +#include <tchar.h>
    1.25 +#include <stdio.h>
    1.26 +#include "global.h"
    1.27 +
    1.28 +// Prototypes
    1.29 +extern void PrintOut(BOOL screenFlag, BOOL logFlag, BOOL timeFlag, const char *format, ...);
    1.30 +
    1.31 +HANDLE OpenVolume(char driveLetter)
    1.32 +{
    1.33 +    HANDLE hVolume;
    1.34 +    UINT uDriveType;
    1.35 +    TCHAR szVolumeName[8];
    1.36 +    TCHAR szRootName[5];
    1.37 + 	TCHAR cDriveLetter = driveLetter;
    1.38 +	
    1.39 +	if (driveLetter == ' ')
    1.40 +	{
    1.41 +		return INVALID_HANDLE_VALUE;
    1.42 +	}
    1.43 +
    1.44 +    wsprintf(szRootName, "%c:\\", cDriveLetter);
    1.45 +
    1.46 +    uDriveType = GetDriveType(szRootName);
    1.47 +    if (uDriveType != DRIVE_REMOVABLE)
    1.48 +	{
    1.49 +        return INVALID_HANDLE_VALUE;
    1.50 +    }
    1.51 +
    1.52 +    wsprintf(szVolumeName, "\\\\.\\%c:", cDriveLetter);
    1.53 +
    1.54 +    hVolume = CreateFile(   szVolumeName,
    1.55 +                            GENERIC_READ | GENERIC_WRITE,
    1.56 +                            FILE_SHARE_READ | FILE_SHARE_WRITE,
    1.57 +                            NULL,
    1.58 +                            OPEN_EXISTING,
    1.59 +                            0,
    1.60 +                            NULL );
    1.61 +    
    1.62 +    return hVolume;
    1.63 +}
    1.64 +
    1.65 +#define LOCK_RETRY_DELAY	500       // half a second
    1.66 +#define LOCK_RETRY_MAX		20
    1.67 +
    1.68 +BOOL LockAndDismout(HANDLE hVolume)
    1.69 +{
    1.70 +   DWORD dwBytesReturned;
    1.71 +
    1.72 +   // Retry a number of times
    1.73 +   for (int i = 0; i < LOCK_RETRY_MAX; i++)
    1.74 +   {
    1.75 +       if (DeviceIoControl(hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL))
    1.76 +			{
    1.77 +			return DeviceIoControl( hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
    1.78 +			}
    1.79 +       Sleep (LOCK_RETRY_MAX);
    1.80 +   }
    1.81 +
    1.82 +   return FALSE;
    1.83 +}
    1.84 +
    1.85 +BOOL AllowRemovalAndEject (HANDLE hVolume)
    1.86 +{
    1.87 +    DWORD dwBytesReturned;
    1.88 +    PREVENT_MEDIA_REMOVAL PMRBuffer;
    1.89 +
    1.90 +    PMRBuffer.PreventMediaRemoval = FALSE;
    1.91 +
    1.92 +    if (DeviceIoControl( hVolume, IOCTL_STORAGE_MEDIA_REMOVAL, &PMRBuffer, sizeof(PREVENT_MEDIA_REMOVAL),
    1.93 +                            NULL, 0, &dwBytesReturned, NULL))
    1.94 +	    return DeviceIoControl( hVolume, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
    1.95 +
    1.96 +	return FALSE;   
    1.97 +}
    1.98 +
    1.99 +
   1.100 +BOOL EjectVolume(char driveLetter)
   1.101 +{
   1.102 +    HANDLE hVolume = INVALID_HANDLE_VALUE;
   1.103 +
   1.104 +
   1.105 +	hVolume = OpenVolume(driveLetter);
   1.106 +
   1.107 +	if (hVolume == INVALID_HANDLE_VALUE)
   1.108 +		return FALSE;
   1.109 +
   1.110 +    // Lock and dismount the volume.
   1.111 +    if (LockAndDismout(hVolume))
   1.112 +		{
   1.113 +        // Set prevent removal to false and eject the volume.
   1.114 +        AllowRemovalAndEject(hVolume);
   1.115 +		}
   1.116 +
   1.117 +    // Close the volume so other processes can use the drive.
   1.118 +    if (!CloseHandle(hVolume))
   1.119 +        return FALSE;
   1.120 +
   1.121 +    return TRUE;
   1.122 +}
   1.123 +
   1.124 +#define WAIT_RETRIES 500
   1.125 +#define WAIT_TIME 250
   1.126 +#define WAIT_DELAY 1500
   1.127 +
   1.128 +BOOL WaitDriveReady (char driveLetter)
   1.129 +{
   1.130 +    UINT uDriveType = DRIVE_UNKNOWN ;
   1.131 +    TCHAR szRootName[5];
   1.132 +	UINT waitTime = 0;
   1.133 +
   1.134 +    wsprintf(szRootName, "%c:\\", driveLetter);
   1.135 +
   1.136 +	for (int i = 0; i < WAIT_RETRIES && (uDriveType != DRIVE_REMOVABLE); i++)
   1.137 +		{
   1.138 +		uDriveType = GetDriveType(szRootName);
   1.139 +		if (uDriveType != DRIVE_REMOVABLE)
   1.140 +			{
   1.141 +			Sleep (WAIT_TIME);
   1.142 +			waitTime += WAIT_TIME;
   1.143 +			}
   1.144 +		else
   1.145 +			{
   1.146 +			Sleep (WAIT_DELAY);
   1.147 +			waitTime += WAIT_DELAY;
   1.148 +			}
   1.149 +		}
   1.150 +	
   1.151 +
   1.152 +    PRINT_ALWAYS "Drive %c ready after %d milliseconds.\n",driveLetter,waitTime);
   1.153 +    return uDriveType == DRIVE_REMOVABLE;
   1.154 +}