os/kernelhwsrv/kerneltest/e32test/usb/t_usb_win/src/eject.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\usb\t_usb_win\src\eject.cpp
sl@0
    15
// eject functions
sl@0
    16
//
sl@0
    17
sl@0
    18
#include "stdafx.h"
sl@0
    19
#include <windows.h>
sl@0
    20
#include <winioctl.h>
sl@0
    21
#include <tchar.h>
sl@0
    22
#include <stdio.h>
sl@0
    23
#include "global.h"
sl@0
    24
sl@0
    25
// Prototypes
sl@0
    26
extern void PrintOut(BOOL screenFlag, BOOL logFlag, BOOL timeFlag, const char *format, ...);
sl@0
    27
sl@0
    28
HANDLE OpenVolume(char driveLetter)
sl@0
    29
{
sl@0
    30
    HANDLE hVolume;
sl@0
    31
    UINT uDriveType;
sl@0
    32
    TCHAR szVolumeName[8];
sl@0
    33
    TCHAR szRootName[5];
sl@0
    34
 	TCHAR cDriveLetter = driveLetter;
sl@0
    35
	
sl@0
    36
	if (driveLetter == ' ')
sl@0
    37
	{
sl@0
    38
		return INVALID_HANDLE_VALUE;
sl@0
    39
	}
sl@0
    40
sl@0
    41
    wsprintf(szRootName, "%c:\\", cDriveLetter);
sl@0
    42
sl@0
    43
    uDriveType = GetDriveType(szRootName);
sl@0
    44
    if (uDriveType != DRIVE_REMOVABLE)
sl@0
    45
	{
sl@0
    46
        return INVALID_HANDLE_VALUE;
sl@0
    47
    }
sl@0
    48
sl@0
    49
    wsprintf(szVolumeName, "\\\\.\\%c:", cDriveLetter);
sl@0
    50
sl@0
    51
    hVolume = CreateFile(   szVolumeName,
sl@0
    52
                            GENERIC_READ | GENERIC_WRITE,
sl@0
    53
                            FILE_SHARE_READ | FILE_SHARE_WRITE,
sl@0
    54
                            NULL,
sl@0
    55
                            OPEN_EXISTING,
sl@0
    56
                            0,
sl@0
    57
                            NULL );
sl@0
    58
    
sl@0
    59
    return hVolume;
sl@0
    60
}
sl@0
    61
sl@0
    62
#define LOCK_RETRY_DELAY	500       // half a second
sl@0
    63
#define LOCK_RETRY_MAX		20
sl@0
    64
sl@0
    65
BOOL LockAndDismout(HANDLE hVolume)
sl@0
    66
{
sl@0
    67
   DWORD dwBytesReturned;
sl@0
    68
sl@0
    69
   // Retry a number of times
sl@0
    70
   for (int i = 0; i < LOCK_RETRY_MAX; i++)
sl@0
    71
   {
sl@0
    72
       if (DeviceIoControl(hVolume, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL))
sl@0
    73
			{
sl@0
    74
			return DeviceIoControl( hVolume, FSCTL_DISMOUNT_VOLUME, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
sl@0
    75
			}
sl@0
    76
       Sleep (LOCK_RETRY_MAX);
sl@0
    77
   }
sl@0
    78
sl@0
    79
   return FALSE;
sl@0
    80
}
sl@0
    81
sl@0
    82
BOOL AllowRemovalAndEject (HANDLE hVolume)
sl@0
    83
{
sl@0
    84
    DWORD dwBytesReturned;
sl@0
    85
    PREVENT_MEDIA_REMOVAL PMRBuffer;
sl@0
    86
sl@0
    87
    PMRBuffer.PreventMediaRemoval = FALSE;
sl@0
    88
sl@0
    89
    if (DeviceIoControl( hVolume, IOCTL_STORAGE_MEDIA_REMOVAL, &PMRBuffer, sizeof(PREVENT_MEDIA_REMOVAL),
sl@0
    90
                            NULL, 0, &dwBytesReturned, NULL))
sl@0
    91
	    return DeviceIoControl( hVolume, IOCTL_STORAGE_EJECT_MEDIA, NULL, 0, NULL, 0, &dwBytesReturned, NULL);
sl@0
    92
sl@0
    93
	return FALSE;   
sl@0
    94
}
sl@0
    95
sl@0
    96
sl@0
    97
BOOL EjectVolume(char driveLetter)
sl@0
    98
{
sl@0
    99
    HANDLE hVolume = INVALID_HANDLE_VALUE;
sl@0
   100
sl@0
   101
sl@0
   102
	hVolume = OpenVolume(driveLetter);
sl@0
   103
sl@0
   104
	if (hVolume == INVALID_HANDLE_VALUE)
sl@0
   105
		return FALSE;
sl@0
   106
sl@0
   107
    // Lock and dismount the volume.
sl@0
   108
    if (LockAndDismout(hVolume))
sl@0
   109
		{
sl@0
   110
        // Set prevent removal to false and eject the volume.
sl@0
   111
        AllowRemovalAndEject(hVolume);
sl@0
   112
		}
sl@0
   113
sl@0
   114
    // Close the volume so other processes can use the drive.
sl@0
   115
    if (!CloseHandle(hVolume))
sl@0
   116
        return FALSE;
sl@0
   117
sl@0
   118
    return TRUE;
sl@0
   119
}
sl@0
   120
sl@0
   121
#define WAIT_RETRIES 500
sl@0
   122
#define WAIT_TIME 250
sl@0
   123
#define WAIT_DELAY 1500
sl@0
   124
sl@0
   125
BOOL WaitDriveReady (char driveLetter)
sl@0
   126
{
sl@0
   127
    UINT uDriveType = DRIVE_UNKNOWN ;
sl@0
   128
    TCHAR szRootName[5];
sl@0
   129
	UINT waitTime = 0;
sl@0
   130
sl@0
   131
    wsprintf(szRootName, "%c:\\", driveLetter);
sl@0
   132
sl@0
   133
	for (int i = 0; i < WAIT_RETRIES && (uDriveType != DRIVE_REMOVABLE); i++)
sl@0
   134
		{
sl@0
   135
		uDriveType = GetDriveType(szRootName);
sl@0
   136
		if (uDriveType != DRIVE_REMOVABLE)
sl@0
   137
			{
sl@0
   138
			Sleep (WAIT_TIME);
sl@0
   139
			waitTime += WAIT_TIME;
sl@0
   140
			}
sl@0
   141
		else
sl@0
   142
			{
sl@0
   143
			Sleep (WAIT_DELAY);
sl@0
   144
			waitTime += WAIT_DELAY;
sl@0
   145
			}
sl@0
   146
		}
sl@0
   147
	
sl@0
   148
sl@0
   149
    PRINT_ALWAYS "Drive %c ready after %d milliseconds.\n",driveLetter,waitTime);
sl@0
   150
    return uDriveType == DRIVE_REMOVABLE;
sl@0
   151
}