⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbdrv.cpp

📁 Windows CE操作系统中适用的蓝牙驱动程序
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
/*++
THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

Module Name:  

usbdrv.cpp

Abstract:  
    Exports required by USBD

Functions:

Notes: 

@doc DRIVERS
--*/
#include <windows.h>
#include <dbgapi.h>
#include <svsutil.hxx>
#include "usbdi.h"
#include "usbdrv.h"

#include <bt_buffer.h>
#include <bt_hcip.h>
#include <bt_os.h>
#include <bt_debug.h>

#include <bt_tdbg.h>
#include "usbdev.h"

const WCHAR gcszRegisterClientDriverId[] = L"RegisterClientDriverID";
const WCHAR gcszRegisterClientSettings[] = L"RegisterClientSettings";

const WCHAR gcszUnRegisterClientDriverId[] = L"UnRegisterClientDriverID";
const WCHAR gcszUnRegisterClientSettings[] = L"UnRegisterClientSettings";

const WCHAR gcszBluetoothUSBDriverId[] = L"Bluetooth_USB_Driver";

HINSTANCE ghInst = NULL;

DECLARE_DEBUG_VARS();

BOOL WINAPI DllMain(HANDLE hinstDLL, DWORD dwReason, LPVOID lpvReserved)
{
	int i;
    switch (dwReason) 
    {
    case DLL_PROCESS_ATTACH:
		svsutil_Initialize ();
		ghInst = (HINSTANCE )hinstDLL;

		gpsynchUsbDevice = new CSynch();
		if (!gpsynchUsbDevice)
			return FALSE;

		gpUsbDevice = new CUsbDevice();
		if (!gpUsbDevice)
			return FALSE;

		gpRingBuffer = new CRingBuffer();
		if (!gpRingBuffer)
			return FALSE;
		
		for (i=0; i<NUM_IN_PIPES; i++)
		{
			gheventRead[i] = CreateEvent(NULL, TRUE, FALSE, NULL);
			if (!gheventRead[i])
				return FALSE;
		}

		gheventClose = CreateEvent(NULL, TRUE, FALSE, NULL);
		if (!gheventClose)
			return FALSE;

		gheventWritten = CreateEvent(NULL, FALSE, FALSE, NULL);
		if (!gheventWritten)
			return FALSE;

		gheventPackets = CreateEvent(NULL, FALSE, FALSE, NULL);
		if (!gheventPackets)
			return FALSE;

		DisableThreadLibraryCalls((HMODULE) hinstDLL);
		break;
	
	case DLL_PROCESS_DETACH:
		SVSUTIL_ASSERT(gpUsbDevice && gpsynchUsbDevice);

		delete gpsynchUsbDevice;
		delete gpUsbDevice;
		delete gpRingBuffer;
		
		for (i=0; i<NUM_IN_PIPES; i++)
		{
			if (gheventRead[i])
				CloseHandle(gheventRead[i]);
		}
		
		if (gheventClose)
			CloseHandle(gheventClose);

		if (gheventWritten)
			CloseHandle(gheventWritten);

		if (gheventPackets)
			CloseHandle(gheventPackets);

		svsutil_DeInitialize ();
		break;

    default:
        break;
    }
    return TRUE ;
}

/*
 * @func   BOOL | USBInstallDriver | Install USB client driver.
 * @rdesc  Return TRUE if install succeeds, or FALSE if there is some error.
 * @comm   This function is called by USBD when an unrecognized device
 *         is attached to the USB and the user enters the client driver
 *         DLL name.  It should register a unique client id string with
 *         USBD, and set up any client driver settings.
 * @xref   <f USBUnInstallDriver>
 */
extern "C" BOOL 
USBInstallDriver(
    LPCWSTR szDriverLibFile)  // @parm [IN] - Contains client driver DLL name
{

#if defined (DEBUG) || defined (_DEBUG) || defined (RETAILLOG)
	gpsynchUsbDevice->Lock();
	DebugInit();
	gpsynchUsbDevice->Unlock();
#endif

	IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] BTHUSB: USBInstallDriver %s\n", szDriverLibFile));

    BOOL fRet = FALSE;
    HINSTANCE hInst = LoadLibrary(L"USBD.DLL");
    
    if(hInst)
    {
        LPREGISTER_CLIENT_DRIVER_ID pRegisterId = (LPREGISTER_CLIENT_DRIVER_ID)
                GetProcAddress(hInst, gcszRegisterClientDriverId);

        LPREGISTER_CLIENT_SETTINGS pRegisterSettings =
                (LPREGISTER_CLIENT_SETTINGS) GetProcAddress(hInst,
                gcszRegisterClientSettings);

        if(pRegisterId && pRegisterSettings)
        {
            USB_DRIVER_SETTINGS DriverSettings;

            DriverSettings.dwCount = sizeof(DriverSettings);

            // Set up our DriverSettings struct to specify how we are to
            // be loaded.  Mice and keyboards can be identified as boot
            // devices within the HID interface class.
            DriverSettings.dwVendorId = USB_NO_INFO;
            DriverSettings.dwProductId = USB_NO_INFO;
            DriverSettings.dwReleaseNumber = USB_NO_INFO;

            DriverSettings.dwDeviceClass = USB_DEVICE_CLASS_WIRELESS_CONTROLLER;
            DriverSettings.dwDeviceSubClass = USB_DEVICE_SUBCLASS_RFCONTROLLER;
            DriverSettings.dwDeviceProtocol = USB_DEVICE_PROTOCOL_BLUETOOTH;

            DriverSettings.dwInterfaceClass = USB_NO_INFO;    
            DriverSettings.dwInterfaceSubClass = USB_NO_INFO; 
            DriverSettings.dwInterfaceProtocol = USB_NO_INFO; 

            fRet = (*pRegisterId)(gcszBluetoothUSBDriverId);

            if(fRet)
            {
                fRet = (*pRegisterSettings)(szDriverLibFile, gcszBluetoothUSBDriverId, NULL, &DriverSettings);

                if(!fRet)
                {
					LPUN_REGISTER_CLIENT_DRIVER_ID pUnRegisterId =
							(LPUN_REGISTER_CLIENT_DRIVER_ID)
							GetProcAddress(hInst, gcszUnRegisterClientDriverId);

					if(pUnRegisterId)
						(*pUnRegisterId)(gcszBluetoothUSBDriverId);
                }
            }
			else
				IFDBG(DebugOut (DEBUG_ERROR, L"BTHUSB: Failed to register Bluetooth drier id\n"));
        }
        else 
            IFDBG(DebugOut (DEBUG_ERROR, L"BTHUSB: Error getting USBD function pointers\n"));

        FreeLibrary(hInst);
    } else
        IFDBG(DebugOut (DEBUG_ERROR, L"BTHUSB: USBD.DLL load failure : %d\n", GetLastError ()));

    IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] -BTHUSB: USBInstallDriver %s %d\n", szDriverLibFile, fRet));
    return fRet;
}

/*
 * @func   BOOL | USBUnInstallDriver | Uninstall USB client driver.
 * @rdesc  Return TRUE if install succeeds, or FALSE if there is some error.
 * @comm   This function can be called by a client driver to deregister itself
 *         with USBD.
 * @xref   <f USBInstallDriver>
 */
extern "C" BOOL 
USBUnInstallDriver()
{
    IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] BTUSB: USBUnInstallDriver\n"));

    BOOL fRet = FALSE;
    HINSTANCE hInst = LoadLibrary(L"USBD.DLL");

    if(hInst)
    {
        LPUN_REGISTER_CLIENT_DRIVER_ID pUnRegisterId =
                (LPUN_REGISTER_CLIENT_DRIVER_ID)
                GetProcAddress(hInst, gcszUnRegisterClientDriverId);

        LPUN_REGISTER_CLIENT_SETTINGS pUnRegisterSettings =
                (LPUN_REGISTER_CLIENT_SETTINGS) GetProcAddress(hInst,
                gcszUnRegisterClientSettings);

        if(pUnRegisterSettings)
        {
            USB_DRIVER_SETTINGS DriverSettings;

            DriverSettings.dwCount = sizeof(DriverSettings);

            // This structure must be filled out the same as we registered with
            DriverSettings.dwVendorId = USB_NO_INFO;
            DriverSettings.dwProductId = USB_NO_INFO;
            DriverSettings.dwReleaseNumber = USB_NO_INFO;

            DriverSettings.dwDeviceClass = USB_DEVICE_CLASS_WIRELESS_CONTROLLER;
            DriverSettings.dwDeviceSubClass = USB_DEVICE_SUBCLASS_RFCONTROLLER;
            DriverSettings.dwDeviceProtocol = USB_DEVICE_PROTOCOL_BLUETOOTH;

            DriverSettings.dwInterfaceClass = USB_NO_INFO;    
            DriverSettings.dwInterfaceSubClass = USB_NO_INFO; 
            DriverSettings.dwInterfaceProtocol = USB_NO_INFO; 

            fRet = (*pUnRegisterSettings)(gcszBluetoothUSBDriverId, NULL,
                    &DriverSettings);
        } else
    		IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] BTUSB: USBUnInstallDriver : no procaddr for pUnRegisterSettings\n"));


        if(pUnRegisterId)
        {
            BOOL fRetTemp = (*pUnRegisterId)(gcszBluetoothUSBDriverId);
            fRet = fRet ? fRetTemp : fRet;
        } else
    		IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] BTUSB: USBUnInstallDriver : no procaddr for pUnRegisterId\n"));

        FreeLibrary(hInst);
    } else
    	IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] BTUSB: USBUnInstallDriver : could not load usbd.dll : %d\n", GetLastError ()));

    IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] BTUSB: -USBUnInstallDriver %d\n", fRet));

#if defined (DEBUG) || defined (_DEBUG) || defined (RETAILLOG)
	gpsynchUsbDevice->Lock();
	DebugDeInit();
	gpsynchUsbDevice->Unlock();
#endif

	return fRet;
}

// {B3DD867A-1E6E-4215-8AA7-EAC1DFC46548}
static const GUID guidUSBIntf = 
{ 0xb3dd867a, 0x1e6e, 0x4215, { 0x8a, 0xa7, 0xea, 0xc1, 0xdf, 0xc4, 0x65, 0x48 } };


// Process notifications from USBD.  Currently, the only notification
// supported is a device removal.
extern "C" BOOL USBDeviceNotifications(LPVOID lpvNotifyParameter, DWORD dwCode,
        LPDWORD * dwInfo1, LPDWORD * dwInfo2, LPDWORD * dwInfo3,
        LPDWORD * dwInfo4)
{
    IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] BTUSB: USBDeviceNotifications code %d parm 0x%08x\n", dwCode, lpvNotifyParameter));

    CUsbDevice  *pusbDevice = (CUsbDevice *)lpvNotifyParameter;

    switch(dwCode)
    {
    case USB_CLOSE_DEVICE:
		{
			SVSUTIL_ASSERT(gpUsbDevice);
			SVSUTIL_ASSERT(gpUsbDevice = pusbDevice);

			if (! AdvertiseInterface(&guidUSBIntf, L"bthusb", FALSE)) 
			{
				IFDBG(DebugOut(DEBUG_HCI_INIT, L"[USB] -USBDeviceNotifications : Failed to AdvertiseInterface\n"));
				ASSERT(0);
			}

			gpUsbDevice->CloseDevice();

#if defined (DEBUG) || defined (_DEBUG) || defined (RETAILLOG)
			gpsynchUsbDevice->Lock();
			DebugDeInit();
			gpsynchUsbDevice->Unlock();
#endif

			return TRUE;
		}

	default:
		SVSUTIL_ASSERT(FALSE);
    }

    IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] -BTUSB: USBDeviceNotifications code %d parm 0x%08x\n", dwCode, lpvNotifyParameter));

    return FALSE;
}


/*
 *  @func   BOOL | USBDeviceAttach | USB device attach routine.
 *  @rdesc  Return TRUE upon success, or FALSE if an error occurs.
 *  @comm   This function is called by USBD when a device is attached
 *          to the USB, and a matching registry key is found off the
 *          LoadClients registry key. The client  should determine whether 
 *          the device may be controlled by this driver, and must load 
 *          drivers for any uncontrolled interfaces.
 *  @xref   <f FindInterface> <f LoadGenericInterfaceDriver>
 */
extern "C" BOOL 
USBDeviceAttach(
    USB_HANDLE hDevice,           // @parm [IN] - USB device handle
    LPCUSB_FUNCS lpUsbFuncs,      // @parm [IN] - Pointer to USBDI function table
    LPCUSB_INTERFACE lpInterface, // @parm [IN] - If client is being loaded as an interface
                                  //              driver, contains a pointer to the USB_INTERFACE
                                  //              structure that contains interface information.
                                  //              If client is not loaded for a specific interface,
                                  //              this parameter will be NULL, and the client 
                                  //              may get interface information through <f FindInterface>

    LPCWSTR szUniqueDriverId,     // @parm [IN] - Contains client driver id string
    LPBOOL fAcceptControl,        // @parm [OUT]- Filled in with TRUE if we accept control 
                                  //              of the device, or FALSE if USBD should continue
                                  //              to try to load client drivers.
    LPCUSB_DRIVER_SETTINGS lpDriverSettings,// @parm [IN] - Contains pointer to USB_DRIVER_SETTINGS
                                            //              struct that indicates how we were loaded.

    DWORD dwUnused)               // @parm [IN] - Reserved for use with future versions of USBD
{	
#if defined (DEBUG) || defined (_DEBUG) || defined (RETAILLOG)
	gpsynchUsbDevice->Lock();
	DebugInit();
	gpsynchUsbDevice->Unlock();
#endif
	
    IFDBG(DebugOut (DEBUG_HCI_INIT, L"[USB] BTUSB: USBDeviceAttach 0x%08x %s\n", hDevice, szUniqueDriverId));

	*fAcceptControl = FALSE;

	if (! (hDevice && lpUsbFuncs && gpUsbDevice))
	{
    	IFDBG(DebugOut(DEBUG_ERROR, L"[HCI USB] -DeviceAttach :: some of essential parameters undefined\n"));
		return FALSE;
	}

    if (gpUsbDevice->DeviceAttach(hDevice, lpUsbFuncs))
	{
		*fAcceptControl = TRUE;
		(*lpUsbFuncs->lpRegisterNotificationRoutine)(hDevice, USBDeviceNotifications, gpUsbDevice);

		if (! AdvertiseInterface(&guidUSBIntf, L"bthusb", TRUE)) 
		{
			IFDBG(DebugOut(DEBUG_HCI_INIT, L"[USB] -DeviceAttach : Failed to AdvertiseInterface\n"));
			ASSERT(0);
		}
	}

    IFDBG(DebugOut(DEBUG_HCI_INIT, L"[USB] -DeviceAttach : %d\n", *fAcceptControl));
    
	return *fAcceptControl;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -