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

📄 usbdebug.cpp

📁 基于webCan方面的源程序
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//======================================================================
// USBCode - USB support entry points for driver
//
// Copyright (C) 2005 Douglas Boling
//======================================================================
//#include <USBdi.h>
//#include <usb100.h>
//#include <usbclient.h>
//#include "USBVideo.h"


#define dim(a) (sizeof(a)/sizeof(a[0]))
DWORD Num (PBYTE p, int nCntBytes);
void DumpGUID (PBYTE pData);

int DumpEndpointDescriptor (LPCUSB_ENDPOINT_DESCRIPTOR lpepd);
int DumpInterfaceDescriptor (PCUSB_INTERFACE_DESCRIPTOR lpDes);
int DumpUsbEndpoint (LPCUSB_ENDPOINT lpepd, int i);
int DumpDeviceDescriptor (USB_HANDLE hDevice, LPCUSB_FUNCS lpUsbFuncs, LPUSB_DEVICE_DESCRIPTOR lpdev);
int DumpUsbConfiguration (USB_HANDLE hDevice, LPCUSB_FUNCS lpUsbFuncs, LPCUSB_CONFIGURATION lpcfg);
int DumpUsbInterface (LPCUSB_INTERFACE lpif);

BOOL InstallCamDriver (HWND hWnd, LPCWSTR szDriverLibFile, DWORD dwVendor, DWORD dwProd);

extern HINSTANCE g_hInst;
DWORD GetUsbString (USB_HANDLE hDevice, LPCUSB_FUNCS lpUsbFuncs, UCHAR id, LPTSTR sz, int nSize);

BOOL CALLBACK AboutDlgProc (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam);

//----------------------------------------------------------------------
//
//
int DumpUSBDeviceInfo (USB_HANDLE hDevice, LPCUSB_FUNCS lpUsbFuncs,
                      LPCUSB_INTERFACE lpInterface, LPCWSTR szUniqueDriverId,
                      LPCUSB_DRIVER_SETTINGS lpDriverSettings)
{
	DEBUGMSG (1, (TEXT("=========================================================\r\n")));
	DEBUGMSG (1, (TEXT("=========================================================\r\n")));
	DEBUGMSG (1, (TEXT("================  DumpUSBDeviceInfo  ====================\r\n")));
	DEBUGMSG (1, (TEXT("=========================================================\r\n")));
	DEBUGMSG (1, (TEXT("=========================================================\r\n")));

	if (lpInterface)
		DumpUsbInterface (lpInterface);
	//
	// Dump DriverSettings structure
	//
	DEBUGMSG (1, (TEXT("DriverSettings structure\r\n")));
	DEBUGMSG (1, (TEXT("\t dwVendorId:       %08xh (%d)\r\n"), lpDriverSettings->dwVendorId));
	DEBUGMSG (1, (TEXT("\t dwProductId:      %08xh (%d)\r\n"), lpDriverSettings->dwProductId));
	DEBUGMSG (1, (TEXT("\t dwReleaseNumber:  %08xh (%d)\r\n\r\n"), lpDriverSettings->dwReleaseNumber));

	DEBUGMSG (1, (TEXT("\t dwDeviceClass:    %08xh (%d)\r\n"), lpDriverSettings->dwDeviceClass));
	DEBUGMSG (1, (TEXT("\t dwDeviceSubClass: %08xh (%d)\r\n"), lpDriverSettings->dwDeviceSubClass));
	DEBUGMSG (1, (TEXT("\t dwDeviceProtocol: %08xh (%d)\r\n\r\n"), lpDriverSettings->dwDeviceProtocol));

	DEBUGMSG (1, (TEXT("\t dwInterfaceClass:    %08xh (%d)\r\n"), lpDriverSettings->dwInterfaceClass));
	DEBUGMSG (1, (TEXT("\t dwInterfaceSubClass: %08xh (%d)\r\n"), lpDriverSettings->dwInterfaceSubClass));
	DEBUGMSG (1, (TEXT("\t dwInterfaceProtocol: %08xh (%d)\r\n\r\n"), lpDriverSettings->dwInterfaceProtocol));

	// Dump driver ID string
	DEBUGMSG (1, (TEXT("szUniqueDriverId: >%s<\r\n\r\n"), szUniqueDriverId));


	
	//
	// Query the device for more information
	//
	DEBUGMSG (1, (TEXT("\r\n\r\n")));
	DEBUGMSG (1, (TEXT("Querying the device...\r\n\r\n")));

	LPCUSB_DEVICE lpUsbDev = (lpUsbFuncs->lpGetDeviceInfo)(hDevice);
	if (lpUsbDev)
	{
		DEBUGMSG (1, (TEXT("USB_DEVICE dwCount: %d\r\n"), lpUsbDev->dwCount));
		DumpDeviceDescriptor (hDevice, lpUsbFuncs, (LPUSB_DEVICE_DESCRIPTOR) &lpUsbDev->Descriptor);

		DEBUGMSG (1, (TEXT("\r\n\r\n")));
		DEBUGMSG (1, (TEXT("Dumping Configuration\r\n")));
		DumpUsbConfiguration (hDevice, lpUsbFuncs, (LPCUSB_CONFIGURATION) lpUsbDev->lpConfigs);
	}
	else
		DEBUGMSG (1, (TEXT("\r\nGetDeviceInfo failed rc=%d\r\n\r\n"), GetLastError()));

	return 0;
}

//------------------------------------------------------------------------
//
//
int DumpDeviceDescriptor (USB_HANDLE hDevice, LPCUSB_FUNCS lpUsbFuncs, LPUSB_DEVICE_DESCRIPTOR lpdev)
{
	TCHAR sz[256];

	DEBUGMSG (1, (TEXT("================================\r\n")));
	DEBUGMSG (1, (TEXT("Device descriptor\r\n")));

	// Size changes between 5 and 6
//	if (lpdev->bLength != sizeof (USB_DEVICE_DESCRIPTOR))
//		DEBUGMSG (1, (TEXT("\t Unexpected structure length %d   *********** \r\n"), lpdev->bLength));

	if (lpdev->bDescriptorType != USB_DEVICE_DESCRIPTOR_TYPE)
		DEBUGMSG (1, (TEXT("\t Unexpected DescriptorType!  %02xh \r\n"), lpdev->bDescriptorType));

	DEBUGMSG (1, (TEXT("\t Expected USB version:  %d.%02d\r\n"), lpdev->bcdUSB>>8, lpdev->bcdUSB & 0xff));
	DEBUGMSG (1, (TEXT("\t bDeviceClass:          %02xh\r\n"), lpdev->bDeviceClass));
	DEBUGMSG (1, (TEXT("\t bDeviceSubClass:       %02xh\r\n"), lpdev->bDeviceSubClass));
	DEBUGMSG (1, (TEXT("\t bDeviceProtocol:       %02xh\r\n"), lpdev->bDeviceProtocol));
	DEBUGMSG (1, (TEXT("\t bMaxPacketSize0:       %02xh\r\n"), lpdev->bMaxPacketSize0));
	DEBUGMSG (1, (TEXT("\t idVendor:              %04xh\r\n"), lpdev->idVendor));
	DEBUGMSG (1, (TEXT("\t idProduct:             %04xh\r\n"), lpdev->idProduct));
	DEBUGMSG (1, (TEXT("\t Device version:        %d.%02d\r\n"), lpdev->bcdDevice>>8, lpdev->bcdDevice & 0xff));

	GetUsbString (hDevice, lpUsbFuncs, lpdev->iManufacturer, sz, dim(sz));
	DEBUGMSG (1, (TEXT("\t Manufacturer: (ID:%d)  >%s<\r\n"), lpdev->iManufacturer, sz));

	GetUsbString (hDevice, lpUsbFuncs, lpdev->iProduct, sz, dim(sz));
	DEBUGMSG (1, (TEXT("\t Product:      (ID:%d)  >%s<\r\n"), lpdev->iProduct, sz));

	GetUsbString (hDevice, lpUsbFuncs, lpdev->iSerialNumber, sz, dim(sz));
	DEBUGMSG (1, (TEXT("\t SerialNumber: (ID:%d)  >%s<\r\n"), lpdev->iSerialNumber, sz));

	DEBUGMSG (1, (TEXT("\t bNumConfigurations:    %02xh\r\n"), lpdev->bNumConfigurations));


	DEBUGMSG (1, (TEXT("\t Expected USB version:  %d.%02d\r\n"), lpdev->bcdUSB>>8, lpdev->bcdUSB & 0xff));
	DEBUGMSG (1, (TEXT("\t bDeviceClass:          %02xh\r\n"), lpdev->bDeviceClass));
	DEBUGMSG (1, (TEXT("\t bDeviceSubClass:       %02xh\r\n"), lpdev->bDeviceSubClass));
	DEBUGMSG (1, (TEXT("\t bDeviceProtocol:       %02xh\r\n"), lpdev->bDeviceProtocol));
	DEBUGMSG (1, (TEXT("\t bMaxPacketSize0:       %02xh\r\n"), lpdev->bMaxPacketSize0));
	DEBUGMSG (1, (TEXT("\t idVendor:              %04xh\r\n"), lpdev->idVendor));
	DEBUGMSG (1, (TEXT("\t idProduct:             %04xh\r\n"), lpdev->idProduct));
	DEBUGMSG (1, (TEXT("\t Device version:        %d.%02d\r\n"), lpdev->bcdDevice>>8, lpdev->bcdDevice & 0xff));

	GetUsbString (hDevice, lpUsbFuncs, lpdev->iManufacturer, sz, dim(sz));
	DEBUGMSG (1, (TEXT("\t Manufacturer: (ID:%d)  >%s<\r\n"), lpdev->iManufacturer, sz));

	GetUsbString (hDevice, lpUsbFuncs, lpdev->iProduct, sz, dim(sz));
	DEBUGMSG (1, (TEXT("\t Product:      (ID:%d)  >%s<\r\n"), lpdev->iProduct, sz));

	GetUsbString (hDevice, lpUsbFuncs, lpdev->iSerialNumber, sz, dim(sz));
	DEBUGMSG (1, (TEXT("\t SerialNumber: (ID:%d)  >%s<\r\n"), lpdev->iSerialNumber, sz));

	DEBUGMSG (1, (TEXT("\t bNumConfigurations:    %02xh\r\n"), lpdev->bNumConfigurations));

	return 0;
}

//------------------------------------------------------------------------
//
//
int DumpConfigurationDescriptor (USB_HANDLE hDevice, LPCUSB_FUNCS lpUsbFuncs, LPUSB_CONFIGURATION_DESCRIPTOR lpcfg)
{
	TCHAR sz[256];
//	DEBUGMSG (1, (TEXT("Configuration descriptor\r\n")));

	if (lpcfg->bLength != sizeof (USB_CONFIGURATION_DESCRIPTOR))
		DEBUGMSG (1, (TEXT("\t Unexpected structure length %d    ***********\r\n"), lpcfg->bLength));

	if (lpcfg->bDescriptorType != USB_CONFIGURATION_DESCRIPTOR_TYPE)
		DEBUGMSG (1, (TEXT("\t Unexpected DescriptorType!  %02xh \r\n"), lpcfg->bDescriptorType));

	DEBUGMSG (1, (TEXT("\t wTotalLength:          %02xh\r\n"), lpcfg->wTotalLength));
	DEBUGMSG (1, (TEXT("\t bNumInterfaces:        %02xh\r\n"), lpcfg->bNumInterfaces));
	DEBUGMSG (1, (TEXT("\t bConfigurationValue:   %02xh\r\n"), lpcfg->bConfigurationValue));

	GetUsbString (hDevice, lpUsbFuncs, lpcfg->iConfiguration, sz, dim(sz));
	DEBUGMSG (1, (TEXT("\t Configuration: (ID:%d) >%s<\r\n"), lpcfg->iConfiguration, sz));

	memset (sz, 0, sizeof (sz));
	if (lpcfg->bmAttributes & USB_CONFIG_REMOTE_WAKEUP)
		lstrcat (sz, TEXT("Remote Wakeup  "));

	if (lpcfg->bmAttributes & USB_CONFIG_SELF_POWERED)
		lstrcat (sz, TEXT("Self Powered"));

	DEBUGMSG (1, (TEXT("\t bmAttributes:  (%02xh) >%s<\r\n"), lpcfg->bmAttributes, sz));
	DEBUGMSG (1, (TEXT("\t MaxPower:      (%02xh) %d mA\r\n"), lpcfg->MaxPower, lpcfg->MaxPower*2));
	return 0;
}
//------------------------------------------------------------------------
// DumpExtendedConfigurationDescriptor
// 
int DumpExtendedConfigDescriptor (LPBYTE lpex)
{
	if (lpex == 0)
		return -1;

	BYTE len;
	BYTE desType;
	__try {
		len = *lpex;
		desType = *(lpex+1);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		DEBUGMSG (1, (TEXT("Exception dumping extended descriptor\r\n")));
		return -1;
	}

	if ((len >= 8) && (desType == 0x0b)) // is it an Interface Association Descriptor?
	{
		DEBUGMSG (1, (TEXT("Interface Association Descriptor\r\n")));
		__try {
			DEBUGMSG (1, (TEXT("\t First Interface:          %02xh\r\n"), *(lpex+2)));
			DEBUGMSG (1, (TEXT("\t Interface Cnt:            %02xh\r\n"), *(lpex+3)));
			DEBUGMSG (1, (TEXT("\t Function Class:           %02xh\r\n"), *(lpex+4)));
			DEBUGMSG (1, (TEXT("\t Function Subclass:        %02xh\r\n"), *(lpex+5)));
			DEBUGMSG (1, (TEXT("\t Function Protocol:        %02xh\r\n"), *(lpex+6)));
			DEBUGMSG (1, (TEXT("\t iFunction:                %02xh\r\n"), *(lpex+7)));
		}
		__except (EXCEPTION_EXECUTE_HANDLER)
		{
			DEBUGMSG (1, (TEXT("Exception dumping extended descriptor\r\n")));
			return -1;
		}
	}
	else
	{
		DEBUGMSG (1, (TEXT("Extended Configuration descriptor\r\n")));
		DEBUGMSG (1, (TEXT("\t Descriptor Length:        %02xh\r\n"), len));
		DEBUGMSG (1, (TEXT("\t I/F Descriptor Type:      %02xh\r\n"), desType));
	}
	return 0;
}
//------------------------------------------------------------------------
// DumpUsbConfiguration
// 
int DumpUsbConfiguration (USB_HANDLE hDevice, LPCUSB_FUNCS lpUsbFuncs, LPCUSB_CONFIGURATION lpcfg)
{
	DEBUGMSG (1, (TEXT("================================\r\n")));
	DEBUGMSG (1, (TEXT("Configuration\r\n")));

	if (lpcfg->dwCount != sizeof (USB_CONFIGURATION))
		DEBUGMSG (1, (TEXT("Unexpected CUSB_CONFIGURATION dwCount: %d  ***********\r\n"), lpcfg->dwCount));

	DumpConfigurationDescriptor (hDevice, lpUsbFuncs, (LPUSB_CONFIGURATION_DESCRIPTOR)&lpcfg->Descriptor);

	if (lpcfg->lpvExtended)
		DumpExtendedConfigDescriptor ((LPBYTE)lpcfg->lpvExtended);
	else
		DEBUGMSG (1, (TEXT("No Extended information\r\n")));


	DEBUGMSG (1, (TEXT("================================\r\n")));
	DEBUGMSG (1, (TEXT("CUSB_CONFIGURATION dwNumInterfaces: %d\r\n"), lpcfg->dwNumInterfaces));

	LPCUSB_INTERFACE lpInterfaces = lpcfg->lpInterfaces;
	DWORD i;
	__try {
		for (i = 0; i < lpcfg->dwNumInterfaces; i++)
		{
			DumpUsbInterface (lpInterfaces);
			lpInterfaces = (LPCUSB_INTERFACE)((DWORD) lpInterfaces + lpInterfaces->dwCount);
		}
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		DEBUGMSG (1, (TEXT("Exception dumping interfaces\r\n")));
		return -1;
	}
	return 0;
}

//------------------------------------------------------------------------
//
//
int DumpEndpointDescriptor (LPCUSB_ENDPOINT_DESCRIPTOR lpepd)
{
//	DEBUGMSG (1, (TEXT("Endpoint descriptor\r\n")));

	if (lpepd->bLength != sizeof (USB_ENDPOINT_DESCRIPTOR))
		DEBUGMSG (1, (TEXT("\t\t Unexpected structure length %d   ***********\r\n"), lpepd->bLength));

	if (lpepd->bDescriptorType != USB_ENDPOINT_DESCRIPTOR_TYPE)
		DEBUGMSG (1, (TEXT("\t\t Unexpected DescriptorType!  %02xh \r\n"), lpepd->bDescriptorType));

	DEBUGMSG (1, (TEXT("\t\t bEndpointAddress:  %02xh  %s\r\n"), (lpepd->bEndpointAddress &0x0f), (lpepd->bEndpointAddress &0x80) ? TEXT("In") : TEXT("Out")));
	TCHAR szType[32] = TEXT("unknown");

	switch (lpepd->bmAttributes & USB_ENDPOINT_TYPE_MASK)
	{
	case USB_ENDPOINT_TYPE_CONTROL:
		lstrcpy (szType, TEXT("Control"));
		break;
	case USB_ENDPOINT_TYPE_ISOCHRONOUS:
		lstrcpy (szType, TEXT("Isochronous"));
		break;
	case USB_ENDPOINT_TYPE_BULK:
		lstrcpy (szType, TEXT("Bulk"));
		break;
	case USB_ENDPOINT_TYPE_INTERRUPT:
		lstrcpy (szType, TEXT("Interrupt"));
		break;
	}
	DEBUGMSG (1, (TEXT("\t\t bmAttributes:      %s\r\n"), szType));
	DEBUGMSG (1, (TEXT("\t\t wMaxPacketSize:    %xh (%d)\r\n"), lpepd->wMaxPacketSize, lpepd->wMaxPacketSize));
	DEBUGMSG (1, (TEXT("\t\t bInterval:         %xh (%d)\n"), lpepd->bInterval, lpepd->bInterval));
	return 0;
}
//------------------------------------------------------------------------
//
//
int DumpUsbEndpoint (LPCUSB_ENDPOINT lpepd, int i)
{
	DEBUGMSG (1, (TEXT("\t--------------------------------\r\n")));
	DEBUGMSG (1, (TEXT("\tEndpoint  %d\r\n"), i));

	if (lpepd->dwCount != sizeof (USB_ENDPOINT))
		DEBUGMSG (1, (TEXT("\tUnexpected CUSB_ENDPOINT dwCount: %d   ***********\r\n"), lpepd->dwCount));

	if (lpepd->lpvExtended)
		DEBUGMSG (1, (TEXT("\tCUSB_ENDPOINT lpvExtended: %08x\r\n"), lpepd->lpvExtended));

	DumpEndpointDescriptor (&lpepd->Descriptor);
	return 0;
}
//------------------------------------------------------------------------
//
//
LPTSTR GetVideoStreamingSubclassName (UCHAR id) 
{
	typedef struct {
		UCHAR id;
		LPTSTR szName;
	} SUBCLASS, *PSUBCLASS;

	SUBCLASS scVideoStreaming[] = 
	{
		{0x00,             TEXT("Undefined")},
		{0x01,             TEXT("Control")},
		{0x02,             TEXT("Streaming")},
		{0x03,             TEXT("Interface Collection")},
	};
	// Look up ID
	int i;
	for (i = 0; i < dim(scVideoStreaming); i++)
	{
		if (id == scVideoStreaming[i].id)
			return scVideoStreaming[i].szName;
	}
	return TEXT("Unknown");
}
//------------------------------------------------------------------------
//
//
LPTSTR GetIFName (UCHAR id) 
{
	struct {
		UCHAR id;
		LPTSTR szName;
	} ifIDs [] =
	{
		{USB_DEVICE_CLASS_RESERVED,          TEXT("Reserved")},
		{USB_DEVICE_CLASS_AUDIO,             TEXT("Audio")},
		{USB_DEVICE_CLASS_COMMUNICATIONS,    TEXT("Communications")},
		{USB_DEVICE_CLASS_HUMAN_INTERFACE,   TEXT("Human Interface")},
		{USB_DEVICE_CLASS_MONITOR,           TEXT("Monitor")},
		{USB_DEVICE_CLASS_PHYSICAL_INTERFACE,TEXT("Physical I/F")},
		{USB_DEVICE_CLASS_POWER,             TEXT("Power")},
		{USB_DEVICE_CLASS_PRINTER,           TEXT("Printer")},
		{USB_DEVICE_CLASS_STORAGE,           TEXT("Storage")},
		{USB_DEVICE_CLASS_HUB,               TEXT("Hub")},
		// From USB.org...
		{0x0a,                               TEXT("CDC-Data")},
		{0x0b,                               TEXT("Chip/Smartcard")},
//		{0x0c,                               TEXT("")},
		{0x0d,                               TEXT("Content-Security")},
		{0x0e,                               TEXT("Video")},
		{0xdc,                               TEXT("Diagnostic Device")},
		{0xe0,                               TEXT("Wireless Controller")},
		{0xfe,                               TEXT("Application Specific")},
		{USB_DEVICE_CLASS_VENDOR_SPECIFIC,   TEXT("Vendor Specific")},
	};
	// Look up ID
	int i;
	for (i = 0; i < dim(ifIDs); i++)
	{
		if (id == ifIDs[i].id)
			return ifIDs[i].szName;
	}
	return TEXT("Unknown");

⌨️ 快捷键说明

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