📄 usb.h
字号:
#ifndef __LINUX_USB_H#define __LINUX_USB_H#include <linux/types.h>#include <linux/ioctl.h>#include <linux/version.h>#include <linux/sched.h>#include <linux/delay.h>#include <linux/interrupt.h> /* for in_interrupt() *//* 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_PRINTER 7#define USB_CLASS_MASS_STORAGE 8#define USB_CLASS_HUB 9#define USB_CLASS_DATA 10#define USB_CLASS_VENDOR_SPEC 0xff/* * USB types */#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_DEVICE 0x00#define USB_RECIP_INTERFACE 0x01#define USB_RECIP_ENDPOINT 0x02#define USB_RECIP_OTHER 0x03/* * USB directions */#define USB_DIR_OUT 0#define USB_DIR_IN 0x80/* * 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_UNDEF_4 0xb4#define USB_PID_SOF 0xa5#define USB_PID_UNDEF_6 0x96#define USB_PID_UNDEF_7 0x87#define USB_PID_UNDEF_8 0x78#define USB_PID_IN 0x69#define USB_PID_NAK 0x5a#define USB_PID_DATA1 0x4b#define USB_PID_PREAMBLE 0x3c#define USB_PID_SETUP 0x2d#define USB_PID_STALL 0x1e#define USB_PID_UNDEF_F 0x0f/* * 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#ifdef __KERNEL__#include <linux/config.h>#include <linux/list.h>#define USB_MAJOR 180/* for 2.2-kernels */ #if LINUX_VERSION_CODE < KERNEL_VERSION(2,3,0)static __inline__ void list_add_tail(struct list_head *new, struct list_head *head){ __list_add(new, head->prev, head);}#define LIST_HEAD_INIT(name) { &(name), &(name) }typedef struct wait_queue wait_queue_t;typedef struct wait_queue *wait_queue_head_t;#define DECLARE_WAITQUEUE(wait, current) \ struct wait_queue wait = { current, NULL }#define DECLARE_WAIT_QUEUE_HEAD(wait)\ wait_queue_head_t wait#define init_waitqueue_head(x) *x=NULL#define init_MUTEX(x) *(x)=MUTEX#define DECLARE_MUTEX(name) struct semaphore name=MUTEX#define DECLARE_MUTEX_LOCKED(name) struct semaphore name=MUTEX_LOCKED#define __set_current_state(state_value) \ do { current->state = state_value; } while (0)#ifdef __SMP__#define set_current_state(state_value) \ set_mb(current->state, state_value)#else#define set_current_state(state_value) \ __set_current_state(state_value)#endif#endif // 2.2.xstatic __inline__ void wait_ms(unsigned int ms){ if(!in_interrupt()) { current->state = TASK_UNINTERRUPTIBLE; schedule_timeout(1 + ms * HZ / 1000); } else mdelay(ms);}typedef struct { __u8 requesttype; __u8 request; __u16 value; __u16 index; __u16 length;} devrequest __attribute__ ((packed));/* * USB-status codes: * USB_ST* maps to -E* and should go away in the future */#define USB_ST_NOERROR 0#define USB_ST_CRC (-EILSEQ)#define USB_ST_BITSTUFF (-EPROTO)#define USB_ST_NORESPONSE (-ETIMEDOUT) /* device not responding/handshaking */#define USB_ST_DATAOVERRUN (-EOVERFLOW)#define USB_ST_DATAUNDERRUN (-EREMOTEIO)#define USB_ST_BUFFEROVERRUN (-ECOMM)#define USB_ST_BUFFERUNDERRUN (-ENOSR)#define USB_ST_INTERNALERROR (-EPROTO) /* unknown error */#define USB_ST_SHORT_PACKET (-EREMOTEIO)#define USB_ST_PARTIAL_ERROR (-EXDEV) /* ISO transfer only partially completed */#define USB_ST_URB_KILLED (-ENOENT) /* URB canceled by user */#define USB_ST_URB_PENDING (-EINPROGRESS)#define USB_ST_REMOVED (-ENODEV) /* device not existing or removed */#define USB_ST_TIMEOUT (-ETIMEDOUT) /* communication timed out, also in urb->status**/#define USB_ST_NOTSUPPORTED (-ENOSYS) #define USB_ST_BANDWIDTH_ERROR (-ENOSPC) /* too much bandwidth used */#define USB_ST_URB_INVALID_ERROR (-EINVAL) /* invalid value/transfer type */#define USB_ST_URB_REQUEST_ERROR (-ENXIO) /* invalid endpoint */#define USB_ST_STALL (-EPIPE) /* pipe stalled, also in urb->status*//* * USB device number allocation bitmap. There's one bitmap * per USB tree. */struct usb_devmap { unsigned long devicemap[128 / (8*sizeof(unsigned long))];};#define USB_MAXBUS 64struct usb_busmap { unsigned long busmap[USB_MAXBUS / (8*sizeof(unsigned long))];};/* * This is a USB device descriptor. * * USB device information *//* Everything but the endpoint maximums are aribtrary */#define USB_MAXCONFIG 8#define USB_ALTSETTINGALLOC 4#define USB_MAXALTSETTING 128 /* Hard limit */#define USB_MAXINTERFACES 32#define USB_MAXENDPOINTS 32/* All standard descriptors have these 2 fields in common */struct usb_descriptor_header { __u8 bLength; __u8 bDescriptorType;} __attribute__ ((packed));/* Device descriptor */struct usb_device_descriptor { __u8 bLength; __u8 bDescriptorType; __u16 bcdUSB; __u8 bDeviceClass; __u8 bDeviceSubClass; __u8 bDeviceProtocol; __u8 bMaxPacketSize0; __u16 idVendor; __u16 idProduct; __u16 bcdDevice; __u8 iManufacturer; __u8 iProduct; __u8 iSerialNumber; __u8 bNumConfigurations;} __attribute__ ((packed));/* Endpoint descriptor */struct usb_endpoint_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bEndpointAddress; __u8 bmAttributes; __u16 wMaxPacketSize; __u8 bInterval; __u8 bRefresh; __u8 bSynchAddress; unsigned char *extra; /* Extra descriptors */ int extralen;} __attribute__ ((packed));/* Interface descriptor */struct usb_interface_descriptor { __u8 bLength; __u8 bDescriptorType; __u8 bInterfaceNumber; __u8 bAlternateSetting; __u8 bNumEndpoints; __u8 bInterfaceClass; __u8 bInterfaceSubClass; __u8 bInterfaceProtocol; __u8 iInterface; struct usb_endpoint_descriptor *endpoint; unsigned char *extra; int extralen;} __attribute__ ((packed));struct usb_interface { struct usb_interface_descriptor *altsetting; int act_altsetting; /* active alternate setting */ int num_altsetting; /* number of alternate settings */ int max_altsetting; /* total memory allocated */ struct usb_driver *driver; /* driver */ void *private_data;};/* Configuration descriptor information.. */struct usb_config_descriptor { __u8 bLength; __u8 bDescriptorType; __u16 wTotalLength; __u8 bNumInterfaces; __u8 bConfigurationValue; __u8 iConfiguration; __u8 bmAttributes; __u8 MaxPower; struct usb_interface *interface;} __attribute__ ((packed));/* String descriptor */struct usb_string_descriptor { __u8 bLength; __u8 bDescriptorType; __u16 wData[1];} __attribute__ ((packed));struct usb_device;struct usb_driver { const char *name; void * (*probe)(struct usb_device *, unsigned int); void (*disconnect)(struct usb_device *, void *); struct list_head driver_list; struct file_operations *fops; int minor;};/* * Pointer to a device endpoint interrupt function -greg * Parameters: * int status - This needs to be defined. Right now each HCD * passes different transfer status bits back. Don't use it * until we come up with a common meaning. * void *buffer - This is a pointer to the data used in this * USB transfer. * int length - This is the number of bytes transferred in or out * of the buffer by this transfer. (-1 = unknown/unsupported) * void *dev_id - This is a user defined pointer set when the IRQ * is requested that is passed back. * * Special Cases: * if (status == USB_ST_REMOVED), don't trust buffer or len. */typedef int (*usb_device_irq)(int, void *, int, void *);/* -------------------------------------------------------------------------------------* * New USB Structures * * -------------------------------------------------------------------------------------*/#define USB_DISABLE_SPD 0x0001#define USB_ISO_ASAP 0x0002#define USB_URB_EARLY_COMPLETE 0x0004#define USB_ASYNC_UNLINK 0x0008#define USB_QUEUE_BULK 0x0010#define USB_TIMEOUT_KILLED 0x1000 // only set by HCD!typedef struct{ unsigned int offset; unsigned int length; // expected length unsigned int actual_length; unsigned int status;} iso_packet_descriptor_t, *piso_packet_descriptor_t;struct urb;typedef void (*usb_complete_t)(struct urb *);typedef struct urb{ spinlock_t lock; // lock for the URB void *hcpriv; // private data for host controller struct list_head urb_list; // list pointer to all active urbs struct urb *next; // pointer to next URB struct usb_device *dev; // pointer to associated USB device unsigned int pipe; // pipe information int status; // returned status unsigned int transfer_flags; // USB_DISABLE_SPD | USB_ISO_ASAP | USB_URB_EARLY_COMPLETE void *transfer_buffer; // associated data buffer int transfer_buffer_length; // data buffer length int actual_length; // actual data buffer length unsigned char *setup_packet; // setup packet (control only) // int start_frame; // start frame (iso/irq only) int number_of_packets; // number of packets in this request (iso/irq only) int interval; // polling interval (irq only) int error_count; // number of errors in this transfer (iso only) int timeout; // timeout (in jiffies)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -