📄 ul_wdusb.h
字号:
#ifndef __WINDOWS_USB_H#define __WINDOWS_USB_Htypedef struct usb_device usb_device; /* forward declaration *//* USB constants *//* * Device and/or Interface Class codes */#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */#define USB_CLASS_AUDIO 1#define USB_CLASS_COMM 2#define USB_CLASS_HID 3#define USB_CLASS_PHYSICAL 5#define USB_CLASS_STILL_IMAGE 6#define USB_CLASS_PRINTER 7#define USB_CLASS_MASS_STORAGE 8#define USB_CLASS_HUB 9#define USB_CLASS_CDC_DATA 0x0a#define USB_CLASS_CSCID 0x0b /* chip+ smart card */#define USB_CLASS_CONTENT_SEC 0x0d /* content security */#define USB_CLASS_APP_SPEC 0xfe#define USB_CLASS_VENDOR_SPEC 0xff/* * USB types */#define USB_TYPE_MASK (0x03 << 5)#define USB_TYPE_STANDARD (0x00 << 5)#define USB_TYPE_CLASS (0x01 << 5)#define USB_TYPE_VENDOR (0x02 << 5)#define USB_TYPE_RESERVED (0x03 << 5)/* * USB recipients */#define USB_RECIP_MASK 0x1f#define USB_RECIP_DEVICE 0x00#define USB_RECIP_INTERFACE 0x01#define USB_RECIP_ENDPOINT 0x02#define USB_RECIP_OTHER 0x03/* * USB directions */#define USB_DIR_OUT 0 /* to device */#define USB_DIR_IN 0x80 /* to host *//* * Descriptor types */#define USB_DT_DEVICE 0x01#define USB_DT_CONFIG 0x02#define USB_DT_STRING 0x03#define USB_DT_INTERFACE 0x04#define USB_DT_ENDPOINT 0x05#define USB_DT_HID (USB_TYPE_CLASS | 0x01)#define USB_DT_REPORT (USB_TYPE_CLASS | 0x02)#define USB_DT_PHYSICAL (USB_TYPE_CLASS | 0x03)#define USB_DT_HUB (USB_TYPE_CLASS | 0x09)/* * Descriptor sizes per descriptor type */#define USB_DT_DEVICE_SIZE 18#define USB_DT_CONFIG_SIZE 9#define USB_DT_INTERFACE_SIZE 9#define USB_DT_ENDPOINT_SIZE 7#define USB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */#define USB_DT_HUB_NONVAR_SIZE 7#define USB_DT_HID_SIZE 9/* * Endpoints */#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */#define USB_ENDPOINT_DIR_MASK 0x80#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */#define USB_ENDPOINT_XFER_CONTROL 0#define USB_ENDPOINT_XFER_ISOC 1#define USB_ENDPOINT_XFER_BULK 2#define USB_ENDPOINT_XFER_INT 3/* * USB Packet IDs (PIDs) */#define USB_PID_UNDEF_0 0xf0#define USB_PID_OUT 0xe1#define USB_PID_ACK 0xd2#define USB_PID_DATA0 0xc3#define USB_PID_PING 0xb4 /* USB 2.0 */#define USB_PID_SOF 0xa5#define USB_PID_NYET 0x96 /* USB 2.0 */#define USB_PID_DATA2 0x87 /* USB 2.0 */#define USB_PID_SPLIT 0x78 /* USB 2.0 */#define USB_PID_IN 0x69#define USB_PID_NAK 0x5a#define USB_PID_DATA1 0x4b#define USB_PID_PREAMBLE 0x3c /* Token mode */#define USB_PID_ERR 0x3c /* USB 2.0: handshake mode */#define USB_PID_SETUP 0x2d#define USB_PID_STALL 0x1e#define USB_PID_MDATA 0x0f /* USB 2.0 *//* * Standard requests */#define USB_REQ_GET_STATUS 0x00#define USB_REQ_CLEAR_FEATURE 0x01#define USB_REQ_SET_FEATURE 0x03#define USB_REQ_SET_ADDRESS 0x05#define USB_REQ_GET_DESCRIPTOR 0x06#define USB_REQ_SET_DESCRIPTOR 0x07#define USB_REQ_GET_CONFIGURATION 0x08#define USB_REQ_SET_CONFIGURATION 0x09#define USB_REQ_GET_INTERFACE 0x0A#define USB_REQ_SET_INTERFACE 0x0B#define USB_REQ_SYNCH_FRAME 0x0C/* * HID requests */#define USB_REQ_GET_REPORT 0x01#define USB_REQ_GET_IDLE 0x02#define USB_REQ_GET_PROTOCOL 0x03#define USB_REQ_SET_REPORT 0x09#define USB_REQ_SET_IDLE 0x0A#define USB_REQ_SET_PROTOCOL 0x0B/* * Calling this entity a "pipe" is glorifying it. A USB pipe * is something embarrassingly simple: it basically consists * of the following information: * - device number (7 bits) * - endpoint number (4 bits) * - current Data0/1 state (1 bit) * - direction (1 bit) * - speed (1 bit) * - max packet size (2 bits: 8, 16, 32 or 64) [Historical; now gone.] * - pipe type (2 bits: control, interrupt, bulk, isochronous) * * That's 18 bits. Really. Nothing more. And the USB people have * documented these eighteen bits as some kind of glorious * virtual data structure. * * Let's not fall in that trap. We'll just encode it as a simple * unsigned int. The encoding is: * * - max size: bits 0-1 (00 = 8, 01 = 16, 10 = 32, 11 = 64) [Historical; now gone.] * - direction: bit 7 (0 = Host-to-Device [Out], 1 = Device-to-Host [In]) * - device: bits 8-14 * - endpoint: bits 15-18 * - Data0/1: bit 19 * - speed: bit 26 (0 = Full, 1 = Low Speed) * - pipe type: bits 30-31 (00 = isochronous, 01 = interrupt, 10 = control, 11 = bulk) * * Why? Because it's arbitrary, and whatever encoding we select is really * up to us. This one happens to share a lot of bit positions with the UHCI * specification, so that much of the uhci driver can just mask the bits * appropriately. * * NOTE: there's no encoding (yet?) for a "high speed" endpoint; treat them * like full speed devices. */#define PIPE_ISOCHRONOUS 0#define PIPE_INTERRUPT 1#define PIPE_CONTROL 2#define PIPE_BULK 3#define usb_maxpacket(dev, pipe, out) (out \ ? (dev)->epmaxpacketout[usb_pipeendpoint(pipe)] \ : (dev)->epmaxpacketin [usb_pipeendpoint(pipe)] )#define usb_packetid(pipe) (((pipe) & USB_DIR_IN) ? USB_PID_IN : USB_PID_OUT)#define usb_pipeout(pipe) ((((pipe) >> 7) & 1) ^ 1)#define usb_pipein(pipe) (((pipe) >> 7) & 1)#define usb_pipedevice(pipe) (((pipe) >> 8) & 0x7f)#define usb_pipe_endpdev(pipe) (((pipe) >> 8) & 0x7ff)#define usb_pipeendpoint(pipe) (((pipe) >> 15) & 0xf)#define usb_pipedata(pipe) (((pipe) >> 19) & 1)#define usb_pipeslow(pipe) (((pipe) >> 26) & 1)#define usb_pipetype(pipe) (((pipe) >> 30) & 3)#define usb_pipeisoc(pipe) (usb_pipetype((pipe)) == PIPE_ISOCHRONOUS)#define usb_pipeint(pipe) (usb_pipetype((pipe)) == PIPE_INTERRUPT)#define usb_pipecontrol(pipe) (usb_pipetype((pipe)) == PIPE_CONTROL)#define usb_pipebulk(pipe) (usb_pipetype((pipe)) == PIPE_BULK)#define PIPE_DEVEP_MASK 0x0007ff00/* Endpoint halt control/status */#define usb_endpoint_out(ep_dir) (((ep_dir >> 7) & 1) ^ 1)#define usb_endpoint_halt(dev, ep, out) ((dev)->halted[out] |= (1 << (ep)))#define usb_endpoint_running(dev, ep, out) ((dev)->halted[out] &= ~(1 << (ep)))#define usb_endpoint_halted(dev, ep, out) ((dev)->halted[out] & (1 << (ep)))static _inline ULONG __create_pipe(usb_device *dev, unsigned int endpoint){/* return (dev->devnum << 8) | (endpoint << 15) | ((dev->speed == USB_SPEED_LOW) << 26);*/ return (endpoint << 15);}static _inline ULONG __default_pipe(usb_device *dev){/* return ((dev->speed == USB_SPEED_LOW) << 26);*/ return 0;}/* Create various pipes... */#define usb_sndctrlpipe(dev,endpoint) ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint))#define usb_rcvctrlpipe(dev,endpoint) ((PIPE_CONTROL << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)#define usb_sndisocpipe(dev,endpoint) ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint))#define usb_rcvisocpipe(dev,endpoint) ((PIPE_ISOCHRONOUS << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)#define usb_sndbulkpipe(dev,endpoint) ((PIPE_BULK << 30) | __create_pipe(dev,endpoint))#define usb_rcvbulkpipe(dev,endpoint) ((PIPE_BULK << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)#define usb_sndintpipe(dev,endpoint) ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint))#define usb_rcvintpipe(dev,endpoint) ((PIPE_INTERRUPT << 30) | __create_pipe(dev,endpoint) | USB_DIR_IN)#define usb_snddefctrl(dev) ((PIPE_CONTROL << 30) | __default_pipe(dev))#define usb_rcvdefctrl(dev) ((PIPE_CONTROL << 30) | __default_pipe(dev) | USB_DIR_IN)typedef struct usb_device { PDEVICE_OBJECT fdo; /* fdo */ PUSB_DEVICE_DESCRIPTOR descriptor; /* Descriptor */ char **rawdescriptors; /* Raw descriptors for each config */ PUSB_CONFIGURATION_DESCRIPTOR actconfig; /* the active configuration */ USBD_CONFIGURATION_HANDLE cfghandle; /* controller configuration handle */ USBD_INTERFACE_LIST_ENTRY interfaces; /* list of selected interfaces */} usb_device;typedef NTSTATUS (*_usb_completation_rtn)( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp, IN PVOID Context);/* * Send and receive control messages... */NTSTATUS _usb_submit_urb( IN usb_device *dev, IN PURB urb,IN PLARGE_INTEGER timeout, IN _usb_completation_rtn rtn, IN PVOID rtn_param, OUT PIRP *pirp);NTSTATUS _usb_fill_bulk_urb ( IN PURB urb, IN usb_device *dev,IN ULONG pipe, IN PVOID data, IN LONG len);NTSTATUS usb_create_dev(IN usb_device **pdev,IN PDEVICE_OBJECT fdo);NTSTATUS usb_destroy_dev(IN usb_device *dev);NTSTATUS usb_control_msg(IN usb_device *dev,IN ULONG pipe, IN UCHAR request,IN USHORT requesttype, IN USHORT value, IN USHORT index, IN PVOID data, IN ULONG size, IN ULONG time);NTSTATUS usb_bulk_msg(IN usb_device *dev,IN ULONG pipe, IN PVOID data, IN LONG len, IN LONG *actual_length,IN ULONG time);NTSTATUS usb_get_device_descriptor(IN usb_device *dev);NTSTATUS usb_get_configuration(IN usb_device *dev);NTSTATUS usb_set_configuration(IN usb_device *dev,IN int configuration);NTSTATUS usb_reset_configuration(IN usb_device *dev);NTSTATUS usb_clear_halt(IN usb_device *dev,IN int pipe);/* * Debugging helpers... */void usb_show_device_descriptor(IN PUSB_DEVICE_DESCRIPTOR deviceDescriptor);void usb_show_config_descriptor(IN PUSB_CONFIGURATION_DESCRIPTOR configurationDescriptor);void usb_show_interface_information(IN PUSBD_INTERFACE_INFORMATION interface);void usb_show_endpoint_information(IN PUSBD_PIPE_INFORMATION pipe);void usb_show_device(usb_device *dev);#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -