Monday, November 2, 2009

USB Function profile switcher

The USB Function driver under Windows CE and Windows Mobile, allows user to change on the fly the loaded profile. It can be used as a Serial or a Mass storage device. The Serial profile (or NDIS on some Windows Mobile devices) is used to do data synchronization through ActiveSync. The Mass storage on its side allows the user to see the device internal storage as a standard USB storage device, from the host device.
To switch between the two modes, some Windows Mobile device manufacturer provides application, usually located in the Settings/Connections panel. But for Windows CE user it have to be implemented.

By calling a specific IoControl of the USB Function driver, you can tell it to load a different profile. The available profiles are listed in the registry in [HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers] and the current active profile is specified in the DefaultClientDriver value in this registry node.


void SwitchUSBFunctionProfile(BOOL bEnableActiveSync)
{
HANDLE hUSBFn;
UFN_CLIENT_INFO info;
DWORD dwBytes;

// Open the USB function driver
hUSBFn = CreateFile(USB_FUN_DEV, DEVACCESS_BUSNAMESPACE,
0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
NULL);

if(bEnableActiveSync)
{
// Enable USB Function profile for activesync
swprintf(info.szName, _T("Serial_Class"));
DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT,
info.szName, sizeof(info.szName), NULL, 0, &dwBytes, NULL))
}
else
{
// Enable USB Function for mass-storage
swprintf(info.szName, _T("Mass_Storage_Class"));
DeviceIoControl(hUSBFn, IOCTL_UFN_CHANGE_CURRENT_CLIENT,
info.szName, sizeof(info.szName), NULL, 0, &dwBytes, NULL);
}
}

The defines values are located in %_WINCEROOT%\PUBLIC\COMMON\OAK\INC\usbfnioctl.h
- Nicolas

No comments: