📄 displayusb.cpp
字号:
// DisplayUSB.cpp: Defines the entry point for the console application.
//
// Copyright (C) 2001, Intel Corporation
// All rights reserved.
// Permission is hereby granted to merge this program code with other program
// material to create a derivative work. This derivative work may be distributed
// in compiled object form only. Any other publication of this program, in any form,
// without the explicit permission of the copyright holder is prohibited.
//
// Send questions and comments to John.Hyde@intel.com
#include "stdafx.h"
#include "objbase.h"
#include "winioctl.h"
#include "usbioctl.h"
#include "stdio.h"
#include "stdlib.h"
bool DEBUG; // Set to true to enable DEBUG messages
SECURITY_ATTRIBUTES SA; // Needed for Win2000
const char ClassName[] [20] = {
"Reserved", "Audio", "Communications", "Human Interface",
"Monitor", "Physical Interface", "Power", "Printer",
"Storage", "Hub", "Vendor Specific", "*ILLEGAL VALUE*"
};
const char ConnectionStatus[] [30] = {
"No device connected", "Device connected", "Device FAILED enumeration",
"Device general FAILURE", "Device caused overcurrent", "Not enough power for device"
};
// Define all stuctures using UCHAR or BOOLEAN so that the variables are not 'aligned' by the compiler
typedef struct DESCRIPTOR_REQUEST {
ULONG ConnectionIndex;
struct {UCHAR bmRequest; UCHAR bRequest; UCHAR wValue[2]; UCHAR wIndex[2]; UCHAR wLength[2];} SetupPacket;
UCHAR Data[2048];
};
typedef struct DEVICE_DESCRIPTOR {
UCHAR bLength; UCHAR bDescriptorType; UCHAR bcdUSB[2];
UCHAR bDeviceClass; UCHAR bDeviceSubClass; UCHAR bDeviceProtocol;
UCHAR bMaxPacketSize0; UCHAR idVendor[2]; UCHAR idProduct[2];
UCHAR bcdDevice[2]; UCHAR iManufacturer; UCHAR iProduct;
UCHAR iSerialNumber; UCHAR bNumConfigurations;
};
typedef struct HUB_DESCRIPTOR {
UCHAR bDescriptorLength; UCHAR bDescriptorType; UCHAR bNumberOfPorts;
UCHAR wHubCharacteristics[2]; UCHAR bPowerOnToPowerGood; UCHAR bHubControlCurrent;
UCHAR bRemoveAndPowerMask[64];
};
typedef struct NODE_INFORMATION {
USB_HUB_NODE NodeType; HUB_DESCRIPTOR HubDescriptor; BOOLEAN HubIsBusPowered;
};
typedef struct NODE_CONNECTION_INFORMATION {
ULONG ConnectionIndex; DEVICE_DESCRIPTOR DeviceDescriptor; UCHAR CurrentConfigurationValue;
BOOLEAN LowSpeed; BOOLEAN DeviceIsHub; UCHAR DeviceAddress[2];
UCHAR NumberOfOpenPipes[4]; UCHAR ConnectionStatus[4]; USB_PIPE_INFO PipeList[32];
};
USHORT DisplayStringDescriptor (HANDLE HubHandle, ULONG PortIndex, USHORT LanguageID, UCHAR Index) {
if (DEBUG) printf("\nIn DisplayStringDescriptor with HubHandle = %x, PortIndex = %x, LanguageID = %x, Index = %x\n",
HubHandle, PortIndex, LanguageID, Index);
DESCRIPTOR_REQUEST Packet;
DWORD BytesReturned;
bool Success;
if (LanguageID == 0) { // Get the language ID
memset(&Packet, 0, sizeof(Packet));
Packet.ConnectionIndex = PortIndex;
Packet.SetupPacket.bmRequest = 0x80;
Packet.SetupPacket.bRequest = USB_REQUEST_GET_DESCRIPTOR;
Packet.SetupPacket.wValue[1] = USB_STRING_DESCRIPTOR_TYPE;
Packet.SetupPacket.wLength[0] = 4;
Success = DeviceIoControl(HubHandle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &Packet,
sizeof(Packet), &Packet, sizeof(Packet), &BytesReturned, NULL);
if (!Success) printf(" *** ERROR *** String Descriptor 0 not returned, ErrorCode = %d\n", GetLastError());
LanguageID = Packet.Data[2] + (Packet.Data[3] << 8);
}
memset(&Packet, 0, sizeof(Packet));
Packet.ConnectionIndex = PortIndex;
Packet.SetupPacket.bmRequest = 0x80;
Packet.SetupPacket.bRequest = USB_REQUEST_GET_DESCRIPTOR;
Packet.SetupPacket.wValue[1] = USB_STRING_DESCRIPTOR_TYPE;
Packet.SetupPacket.wValue[0] = Index;
Packet.SetupPacket.wIndex[0] = LanguageID & 0xFF;
Packet.SetupPacket.wIndex[1] = (LanguageID >> 8) & 0xFF;
Packet.SetupPacket.wLength[0] = 255;
Success = DeviceIoControl(HubHandle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &Packet,
sizeof(Packet), &Packet, sizeof(Packet), &BytesReturned, NULL);
if (!Success) printf(" *** ERROR *** String Descriptor %d not returned. ErrorCode = %d\n", Index, GetLastError());
printf(" = %ws", &Packet.Data[2]);
return LanguageID;
}
USHORT DisplayDeviceDescriptor (HANDLE HubHandle, ULONG PortIndex, USHORT LanguageID, PUCHAR BufferPtr) {
if (DEBUG) printf("In DisplayDeviceDescriptor with HubHandle = %x, PortIndex = %x, LanguageID = %x\n", HubHandle, PortIndex, LanguageID);
UCHAR LowByte;
printf("Device Descriptor");
BufferPtr--; // Backup pointer to prepare for pre-increment
printf("\n bLength %2.2x", *++BufferPtr);
printf("\n bDescriptorType %2.2x", *++BufferPtr);
LowByte = *++BufferPtr;
printf("\n bcdUSB %4.4x", LowByte + (*++BufferPtr << 8));
printf("\n bDeviceClass %2.2x", *++BufferPtr);
printf("\n bDeviceSubClass %2.2x", *++BufferPtr);
printf("\n bDeviceProtocol %2.2x", *++BufferPtr);
printf("\n bMaxEP0Size %2.2x", *++BufferPtr);
LowByte = *++BufferPtr;
printf("\n wVendorID %4.4x", LowByte + (*++BufferPtr << 8));
LowByte = *++BufferPtr;
printf("\n wProductID %4.4x", LowByte + (*++BufferPtr << 8));
LowByte = *++BufferPtr;
printf("\n wDeviceID %4.4x", LowByte + (*++BufferPtr << 8));
printf("\n iManufacturer %2.2x", *++BufferPtr);
if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr);
printf("\n iProduct %2.2x", *++BufferPtr);
if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr);
printf("\n iSerialNumber %2.2x", *++BufferPtr);
if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr);
printf("\n bNumConfigurations %2.2x\n", *++BufferPtr);
return LanguageID;
}
USHORT DisplayConfigurationDescriptor(HANDLE HubHandle, ULONG PortIndex, USHORT LanguageID) {
if (DEBUG) printf("In DisplayConfigurationDescriptor with HubHandle = %x, PortIndex = %x, LanguageID = %x\n", HubHandle, PortIndex, LanguageID);
DWORD BytesReturned;
bool Success;
UCHAR LowByte;
DESCRIPTOR_REQUEST Packet;
int i;
printf("\nConfiguration Descriptor");
// First need to get the configuration descriptor
memset(&Packet, 0, sizeof(Packet));
Packet.ConnectionIndex = PortIndex;
Packet.SetupPacket.bmRequest = 0x80;
Packet.SetupPacket.bRequest = USB_REQUEST_GET_DESCRIPTOR;
Packet.SetupPacket.wValue[1] = USB_CONFIGURATION_DESCRIPTOR_TYPE;
Packet.SetupPacket.wLength[1] = 1; // Using a 2K buffer
Success = DeviceIoControl(HubHandle, IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION, &Packet,
sizeof(Packet), &Packet, sizeof(Packet), &BytesReturned, NULL);
if (!Success) printf(" *** ERROR *** Configuration Descriptor not returned. ErrorCode = %d\n", GetLastError());
PUCHAR BufferPtr = &Packet.Data[0];
UCHAR Length = *BufferPtr;
while (Length != 0) {
UCHAR Type = *++BufferPtr;
switch (Type) {
case 2:
printf("\n bLength %2.2x", Length);
printf("\n bDescriptorType %2.2x = Configuration Header", Type);
LowByte = *++BufferPtr;
printf("\n wTotalLength %4.4x", LowByte + (*++BufferPtr << 8));
printf("\n bNumInterfaces %2.2x", *++BufferPtr);
printf("\n bConfigValue %2.2x", *++BufferPtr);
printf("\n iConfiguration %2.2x", *++BufferPtr);
if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr);
printf("\n bmAttributes %2.2x", *++BufferPtr);
LowByte = *++BufferPtr;
printf("\n bMaxPower %2.2x = %d mA", LowByte, (LowByte << 1));
break;
case 4:
printf("\n bLength %2.2x", Length);
printf("\n bDescriptorType %2.2x = Interface Descriptor", Type);
printf("\n bInterfaceNum %2.2x", *++BufferPtr);
printf("\n bAlternateSetting %2.2x", *++BufferPtr);
printf("\n bNumEndpoints %2.2x", *++BufferPtr);
LowByte = *++BufferPtr;
if ((LowByte > 9) & (LowByte < 255)) LowByte = 11;
if (LowByte == 255) LowByte = 10;
printf("\n bInterfaceClass %2.2x = %s", *BufferPtr, ClassName[LowByte]);
printf("\n bSubClass %2.2x", *++BufferPtr);
printf("\n bProtocol %2.2x", *++BufferPtr);
printf("\n iInterface %2.2x", *++BufferPtr);
if (*BufferPtr != 0) LanguageID = DisplayStringDescriptor(HubHandle, PortIndex, LanguageID, *BufferPtr);
break;
case 5:
printf("\n bLength %2.2x", Length);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -