📄 device.c
字号:
*usbEndPointTypeString(UCHAR bmAttributes)
{
UINT typ = bmAttributes & USB_ENDPOINT_TYPE_MASK;
switch( typ) {
case USB_ENDPOINT_TYPE_INTERRUPT:
return "USB_ENDPOINT_TYPE_INTERRUPT";
case USB_ENDPOINT_TYPE_BULK:
return "USB_ENDPOINT_TYPE_BULK";
case USB_ENDPOINT_TYPE_ISOCHRONOUS:
return "USB_ENDPOINT_TYPE_ISOCHRONOUS";
case USB_ENDPOINT_TYPE_CONTROL:
return "USB_ENDPOINT_TYPE_CONTROL";
default:
return "??? UNKNOWN!!";
}
}
/*++
Routine Description:
Called to get ascii string of USB_CONFIGURATION_DESCRIPTOR attributes
Arguments:
PUSB_CONFIGURATION_DESCRIPTOR->bmAttributes
Return Value:
ptr to string
--*/
char
*usbConfigAttributesString(UCHAR bmAttributes)
{
UINT typ = bmAttributes & USB_CONFIG_POWERED_MASK;
switch( typ) {
case USB_CONFIG_BUS_POWERED:
return "USB_CONFIG_BUS_POWERED";
case USB_CONFIG_SELF_POWERED:
return "USB_CONFIG_SELF_POWERED";
case USB_CONFIG_REMOTE_WAKEUP:
return "USB_CONFIG_REMOTE_WAKEUP";
default:
return "??? UNKNOWN!!";
}
}
/*++
Routine Description:
Called to do formatted ascii dump to console of a USB config descriptor
Arguments:
ptr to USB configuration descriptor
Return Value:
none
--*/
void
print_USB_CONFIGURATION_DESCRIPTOR(PUSB_CONFIGURATION_DESCRIPTOR cd)
{
fprintf(stdout, "\n===================\nUSB_CONFIGURATION_DESCRIPTOR\n");
fprintf(stdout,
"bLength = 0x%x, decimal %d\n", cd->bLength, cd->bLength
);
fprintf(stdout,
"bDescriptorType = 0x%x ( %s )\n", cd->bDescriptorType, usbDescriptorTypeString( cd->bDescriptorType )
);
fprintf(stdout,
"wTotalLength = 0x%x, decimal %d\n", cd->wTotalLength, cd->wTotalLength
);
fprintf(stdout,
"bNumInterfaces = 0x%x, decimal %d\n", cd->bNumInterfaces, cd->bNumInterfaces
);
fprintf(stdout,
"bConfigurationValue = 0x%x, decimal %d\n", cd->bConfigurationValue, cd->bConfigurationValue
);
fprintf(stdout,
"iConfiguration = 0x%x, decimal %d\n", cd->iConfiguration, cd->iConfiguration
);
fprintf(stdout,
"bmAttributes = 0x%x ( %s )\n", cd->bmAttributes, usbConfigAttributesString( cd->bmAttributes )
);
fprintf(stdout,
"MaxPower = 0x%x, decimal %d\n", cd->MaxPower, cd->MaxPower
);
}
/*++
Routine Description:
Called to do formatted ascii dump to console of a USB interface descriptor
Arguments:
ptr to USB interface descriptor
Return Value:
none
--*/
void
print_USB_INTERFACE_DESCRIPTOR(PUSB_INTERFACE_DESCRIPTOR id, UINT ix)
{
fprintf(stdout, "\n-----------------------------\nUSB_INTERFACE_DESCRIPTOR #%d\n", ix);
fprintf(stdout,
"bLength = 0x%x\n", id->bLength
);
fprintf(stdout,
"bDescriptorType = 0x%x ( %s )\n", id->bDescriptorType, usbDescriptorTypeString( id->bDescriptorType )
);
fprintf(stdout,
"bInterfaceNumber = 0x%x\n", id->bInterfaceNumber
);
fprintf(stdout,
"bAlternateSetting = 0x%x\n", id->bAlternateSetting
);
fprintf(stdout,
"bNumEndpoints = 0x%x\n", id->bNumEndpoints
);
fprintf(stdout,
"bInterfaceClass = 0x%x\n", id->bInterfaceClass
);
fprintf(stdout,
"bInterfaceSubClass = 0x%x\n", id->bInterfaceSubClass
);
fprintf(stdout,
"bInterfaceProtocol = 0x%x\n", id->bInterfaceProtocol
);
fprintf(stdout,
"bInterface = 0x%x\n", id->iInterface
);
}
/*++
Routine Description:
Called to do formatted ascii dump to console of a USB endpoint descriptor
Arguments:
ptr to USB endpoint descriptor,
index of this endpt in interface desc
Return Value:
none
--*/
void
print_USB_ENDPOINT_DESCRIPTOR(PUSB_ENDPOINT_DESCRIPTOR ed, int i)
{
fprintf(stdout,
"------------------------------\nUSB_ENDPOINT_DESCRIPTOR for Pipe%02d\n", i
);
fprintf(stdout,
"bLength = 0x%x\n", ed->bLength
);
fprintf(stdout,
"bDescriptorType = 0x%x ( %s )\n", ed->bDescriptorType, usbDescriptorTypeString( ed->bDescriptorType )
);
if ( USB_ENDPOINT_DIRECTION_IN( ed->bEndpointAddress ) ) {
fprintf(stdout,
"bEndpointAddress= 0x%x ( INPUT )\n", ed->bEndpointAddress
);
} else {
fprintf(stdout,
"bEndpointAddress= 0x%x ( OUTPUT )\n", ed->bEndpointAddress
);
}
fprintf(stdout,
"bmAttributes= 0x%x ( %s )\n", ed->bmAttributes, usbEndPointTypeString ( ed->bmAttributes )
);
fprintf(stdout,
"wMaxPacketSize= 0x%x, decimal %d\n", ed->wMaxPacketSize, ed->wMaxPacketSize
);
fprintf(stdout,
"bInterval = 0x%x, decimal %d\n", ed->bInterval, ed->bInterval
);
}
/*++
Routine Description:
Called to do formatted ascii dump to console of USB
configuration, interface, and endpoint descriptors
(Cmdline "rwbulk -u" )
Arguments:
handle to device
Return Value:
none
--*/
void
rw_dev( HANDLE hDEV )
{
UINT success;
int siz, nBytes;
char buf[256];
PUSB_CONFIGURATION_DESCRIPTOR cd;
PUSB_INTERFACE_DESCRIPTOR id;
PUSB_ENDPOINT_DESCRIPTOR ed;
siz = sizeof(buf);
if (hDEV == INVALID_HANDLE_VALUE) {
NOISY(("DEV not open"));
return;
}
success = DeviceIoControl(hDEV,
IOCTL_BULKUSB_GET_CONFIG_DESCRIPTOR,
buf,
siz,
buf,
siz,
&nBytes,
NULL);
NOISY(("IOCTL_TIGLUSB_GET_CONFIG_DESCRIPTOR request complete, success = %d nBytes = %d\n", success, nBytes));
if (success) {
ULONG i;
UINT j, n;
char *pch;
pch = buf;
n = 0;
cd = (PUSB_CONFIGURATION_DESCRIPTOR) pch;
print_USB_CONFIGURATION_DESCRIPTOR( cd );
pch += cd->bLength;
do {
id = (PUSB_INTERFACE_DESCRIPTOR) pch;
print_USB_INTERFACE_DESCRIPTOR(id, n++);
pch += id->bLength;
for (j=0; j<id->bNumEndpoints; j++) {
ed = (PUSB_ENDPOINT_DESCRIPTOR) pch;
print_USB_ENDPOINT_DESCRIPTOR(ed,j);
pch += ed->bLength;
}
i = pch - buf;
} while (i<cd->wTotalLength);
}
return;
}
/*++
Routine Description:
Called to do formatted ascii dump to console of USB
configuration, interface, and endpoint descriptors
(Cmdline "rwbulk -u" )
Arguments:
none
Return Value:
none
--*/
int dumpUsbConfig()
{
HANDLE hDEV = open_dev();
if ( hDEV )
{
rw_dev( hDEV );
CloseHandle(hDEV);
}
return 0;
}
// End, routines for USB configuration and pipe info dump (Cmdline "rwbulk -u" )
/*++
Routine Description:
Try to reset the pipe by using IOCTL codes.
Arguments:
none
Return Value:
none
--*/
int resetPipe()
{
UINT success;
int siz, nBytes;
char buf[256];
HANDLE hDEV = open_dev();
if ( hDEV )
{
siz = sizeof(buf);
if (hDEV == INVALID_HANDLE_VALUE) {
NOISY(("DEV not open"));
return -1;
}
success = DeviceIoControl(hDEV,
IOCTL_BULKUSB_RESET_PIPE,
buf,
siz,
buf,
siz,
&nBytes,
NULL);
fprintf(stdout, "TiglUsb.dll: resetPipe\n");
NOISY(("TiglUsb.dll: IOCTL_TIGLUSB_RESET_PIPE request complete, success = %s nBytes = %d\n", success ? "ok" : "nok", nBytes));
CloseHandle(hDEV);
if(success) return 0;
}
return -1;
}
/*++
Routine Description:
Try to reset the device driver by using IOCTL codes.
Beware: dangerous to use, system can become unstable (3, 2, 1, boum ?!)
Arguments:
none
Return Value:
none
--*/
int resetDevice()
{
UINT success;
int siz, nBytes;
char buf[256];
HANDLE hDEV = open_dev();
if ( hDEV )
{
siz = sizeof(buf);
if (hDEV == INVALID_HANDLE_VALUE) {
NOISY(("DEV not open"));
return -1;
}
success = DeviceIoControl(hDEV,
IOCTL_BULKUSB_RESET_DEVICE,
buf,
siz,
buf,
siz,
&nBytes,
NULL);
NOISY(("IOCTL_TIGLUSB_RESET_DEVICE request complete, success = %s nBytes = %d\n", success ? "ok" : "nok", nBytes));
CloseHandle(hDEV);
if(success) return 0;
}
return -1;
}
/*++
Routine Description:
Try to reset IN & OUT pipes by using IOCTL codes.
Arguments:
none
Return Value:
none
--*/
int resetPipes()
{
UINT success;
int siz, nBytes;
char buf[256];
HANDLE hDEV = open_dev();
if ( hDEV )
{
siz = sizeof(buf);
if (hDEV == INVALID_HANDLE_VALUE) {
NOISY(("DEV not open"));
return -1;
}
success = DeviceIoControl(hDEV,
IOCTL_BULKUSB_RESET_PIPES,
buf,
siz,
buf,
siz,
&nBytes,
NULL);
fprintf(stdout, "TiglUsb.dll: resetPipes\n");
NOISY(("TiglUsb.dll: IOCTL_TIGLUSB_RESET_PIPES request complete, success = %s nBytes = %d\n", success ? "ok" : "nok", nBytes));
CloseHandle(hDEV);
if(success) return 0;
}
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -