USB结构体
struct usb_device_id来自include/linux/mod_devicetable.h struct usb_device_id {
98 /* which fields to match against? */
99 __u16 match_flags;
100
/* Used for product specific matches; range is inclusive */ 101
102 __u16 idVendor;
103 __u16 idProduct;
104 __u16 bcdDevice_lo;
105 __u16 bcdDevice_hi;
106
107 /* Used for device class matches */
108 __u8 bDeviceClass;
109 __u8 bDeviceSubClass;
110 __u8 bDeviceProtocol;
111
112 /* Used for interface class matches */
113 __u8 bInterfaceClass;
114 __u8 bInterfaceSubClass;
115 __u8 bInterfaceProtocol;
116
117 /* not matched against */
118 kernel_ulong_t driver_info;
119 };
上节我们注意到id_index=id-storage_usb_ids,id我们知道,storage_probe函数的两个形参之一,而
storage_usb_ids,不是别人,正是我们曾经赋给usb_storage_driver的成员id_table的值.忘记了
id_table的可以回去看.它实际上就是一张表格,告诉全世界我这个driver支持怎样的一些设
备.storage_usb_ids同样来自drivers/usb/storage/usb.c中,
111 /* The entries in this table, except for final ones here
112 * (USB_MASS_STORAGE_CLASS and the empty entry), correspond,
113 * line for line with the entries of us_unsuaul_dev_list[].
114 */
115
116 #define UNUSUAL_DEV(id_vendor, id_product, bcdDeviceMin, bcdDeviceMax,
\
117 vendorName, productName,useProtocol, useTransport, \
118 initFunction, flags) \
119 { USB_DEVICE_VER(id_vendor, id_product, bcdDeviceMin,bcdDeviceMax) }
120
121 static struct usb_device_id storage_usb_ids [] = {
122
123 # include "unusual_devs.h"
124 #undef UNUSUAL_DEV
125 /* Control/Bulk transport for all SubClass values */
126 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_RBC,
US_PR_CB) },
127 { USB_INTERFACE_INFO(USB_CLASS_MASS_STORAGE, US_SC_8020,
US_PR_CB) },
USB_INTERFACE_INFO这个咚咚,很显
然这是一个宏,来自include/linux/usb.h,
482 #define USB_INTERFACE_INFO(cl,sc,pr) \
483 .match_flags = USB_DEVICE_ID_MATCH_INT_INFO, .bInterfaceClass = (cl), .bInterfaceSubClass = (sc), .bInterfaceProtocol = (pr) 每一个USB_INTERFACE_INFO就是构造一个struct usb_device_id的结构体变量,回顾一下我们之前
给出的struct usb_device_id的定义,这里实际上就是为其中的四个元素赋了值,它们是
match_flags,bInterfaceClass,bInterfaceSubClass,bInterfaceProtocol.
match_flag,它又是表示什么意思?USB_INTERFACE_INFO这个宏貌似把所有的设 备的match_flag都给设成了USB_DEVICE_ID_MATCH_INT_INFO,这是为啥?这个宏来自
include/linux/usb.h,
435 #define USB_DEVICE_ID_MATCH_INT_INFO \
436 (USB_DEVICE_ID_MATCH_INT_CLASS |
USB_DEVICE_ID_MATCH_INT_SUBCLASS |
USB_DEVICE_ID_MATCH_INT_PROTOCOL)
match_flag这个咚咚是给usb core去用的,usb core负责给设备寻找适合她的driver,负责给driver
寻找适合他的device,它所比较的就是struct usb_device_id的变量,而struct usb_device_id结构体
中有许多成员,那么是不是一定要把每一个成员都给比较一下呢,其实没有必要那么细,差不多就行了,比如
咱们这里,就是告诉usb core,你只要比较
bInterfaceClass,bInterfaceSubClass,bInterfaceProtocol
即可.include/linux/mod_devicetable.h中针对struct usb_device_id中的每一个要比较的项定义了
一个宏: 32
121 /* Some useful macros to use to create struct usb_device_id */
122 #define USB_DEVICE_ID_MATCH_VENDOR 0x0001
123 #define USB_DEVICE_ID_MATCH_PRODUCT 0x0002
124 #define USB_DEVICE_ID_MATCH_DEV_LO 0x0004 125 #define USB_DEVICE_ID_MATCH_DEV_HI 0x0008
126 #define USB_DEVICE_ID_MATCH_DEV_CLASS 0x0010
127 #define USB_DEVICE_ID_MATCH_DEV_SUBCLASS 0x0020
128 #define USB_DEVICE_ID_MATCH_DEV_PROTOCOL 0x0040
129 #define USB_DEVICE_ID_MATCH_INT_CLASS 0x0080 130 #define USB_DEVICE_ID_MATCH_INT_SUBCLASS 0x0100
131 #define USB_DEVICE_ID_MATCH_INT_PROTOCOL 0x0200 回去对比一下struct usb_device_id就知道这些宏是什么意思了.
472行, struct us_unusual_dev,这个结构体是第一次出现,她定义于drivers/usb/storage/usb.h中,
55 /*
56 * Unusual device list definitions
57 */
58
59 struct us_unusual_dev {
60 const char* vendorName;
61 const char* productName;
62 __u8 useProtocol;
63 __u8 useTransport;
64 int (*initFunction)(struct us_data *);
65 unsigned int flags;
66 };
us_unusal_dev_list是一个数组,定义于drivers/usb/storage/usb.c: static struct us_unusual_dev us_unusual_dev_list[] = {
#undef UNUSUAL_DEV
169 #define UNUSUAL_DEV(idVendor, idProduct, bcdDeviceMin, bcdDeviceMax, \
170 vendor_name, product_name, use_protocol, use_transport, \
171 init_function, Flags) \
172 { \
173 .vendorName = vendor_name, \
174 .productName = product_name, \
175 .useProtocol = use_protocol, \
176 .useTransport = use_transport, \
177 .initFunction = init_function, \
178 .flags = Flags, \
179 }
180
182 # include "unusual_devs.h"
183 # undef UNUSUAL_DEV
184 /* Control/Bulk transport for all SubClass values */
185 { .useProtocol = US_SC_RBC,
186 .useTransport = US_PR_CB},
187 { .useProtocol = US_SC_8020,
188 .useTransport = US_PR_CB},
.
.
.
usb_interface.它定义于 struct
include/linux/usb.h:
struct usb_interface {
116 /* array of alternate settings for this interface, 117 * stored in no particular order */
118 struct usb_host_interface *altsetting;
119
120 struct usb_host_interface *cur_altsetting; /* the currently 121 * active alternate setting */ 122 unsigned num_altsetting; /* number of alternate settings */
123
124 int minor; /* minor number this interface is bound to */
125 enum usb_interface_condition condition; /* state of binding */
126 struct device dev; /* interface specific device info */
127 struct class_device *class_dev;
128 };
129 #define to_usb_interface(d) container_of(d, struct usb_interface, dev)
130 #define interface_to_usbdev(intf) \
131 container_of(intf->dev.parent, struct usb_device, dev)
于include/linux/usb.h:
struct usb_host_interface {
53 struct usb_interface_descriptor desc;
54
55 /* array of desc.bNumEndpoint endpoints associated with this
56 * interface setting. these will be in no particular order.
57 */
58 struct usb_host_endpoint *endpoint;
59
60 unsigned char *extra; /* Extra descriptors */
61 int extralen; 35
62 };
include/linux/usb_ch9.h.(这里取名为"ch9"是因为这个文件很多东西对应于usb
spec 2.0中的第九章,chapter 9.):
242 /* USB_DT_INTERFACE: Interface descriptor */
243 struct usb_interface_descriptor {
244 __u8 bLength;
245 __u8 bDescriptorType;
246
247 __u8 bInterfaceNumber;
248 __u8 bAlternateSetting;
249 __u8 bNumEndpoints;
250 __u8 bInterfaceClass;
251 __u8 bInterfaceSubClass;
252 __u8 bInterfaceProtocol;
253 __u8 iInterface;
254 } __attribute__ ((packed));
struct usb_host_endpoint的定义,来自include/linux/usb.h:
43 /* host-side wrapper for parsed endpoint descriptors */
44 struct usb_host_endpoint {
45 struct usb_endpoint_descriptor desc;
46
47 unsigned char *extra; /* Extra descriptors */
48 int extralen;
49 };
struct usb_endpoint_descriptor的结构体指针.顾名思义,这就是对应endpoint的
描述符的.其定义来自于include/linux/usb_ch9.h:
260 /* USB_DT_ENDPOINT: Endpoint descriptor */
261 struct usb_endpoint_descriptor {
262 __u8 bLength;
263 __u8 bDescriptorType;
264
265 __u8 bEndpointAddress;
266 __u8 bmAttributes;
267 __u16 wMaxPacketSize;
268 __u8 bInterval;
269
270 // NOTE: these two are _only_ in audio endpoints.
271 // use USB_DT_ENDPOINT*_SIZE in bLength, not sizeof.
272 __u8 bRefresh;
273 __u8 bSynchAddress;
274 } __attribute__ ((packed));
struct usb_endpoint_descriptor,它的成员中, bmAttributes表示属性,总共8位,其中bit1 和bit0共同称为Transfer Type,即传输类型,即00表示控制,01表示等时,10表示批量,11表示中断.而
719行我们看到, USB_ENDPOINT_XFERTYPE_MASK这个宏定义
于include/linux/usb_ch9.h中:
286 #define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
287 #define USB_ENDPOINT_XFER_CONTROL 0
288 #define USB_ENDPOINT_XFER_ISOC 1
289 #define USB_ENDPOINT_XFER_BULK 2
3 290 #define USB_ENDPOINT_XFER_INT
USB_DIR_IN仍然来自
include/linux/usb_ch9.h
* USB directions 57
27 *
28 * This bit flag is used in endpoint descriptors' bEndpointAddress field.
29 * It's also one of three fields in control requests bRequestType.
30 */
31 #define USB_DIR_OUT 0 /* to device */
32 #define USB_DIR_IN 0x80 /* to host */
68页
include/linux/usb.h
779 struct urb
780 {
781 /* private, usb core and host controller only fields in the urb */
782 struct kref kref; /* reference count of the URB */
783 spinlock_t lock; /* lock for the URB */
784 void *hcpriv; /* private data for host controller */
785 struct list_head urb_list; /* list pointer to all active urbs */
786 int bandwidth; /* bandwidth for INT/ISO request */
787 atomic_t use_count; /* concurrent submissions counter */
788 u8 reject; /* submissions will fail */
789
790 /* public, documented fields in the urb that can be used by drivers */
791 struct usb_device *dev; /* (in) pointer to associated device */
792 unsigned int pipe; /* (in) pipe information */ 793 int status; /* (return) non-ISO status */
794 unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/
795 void *transfer_buffer; /* (in) associated data buffer */
796 dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */
797 int transfer_buffer_length; /* (in) data buffer length */
798 int actual_length; /* (return) actual transfer length */
799 unsigned char *setup_packet; /* (in) setup packet (control only) */
800 dma_addr_t setup_dma; /* (in) dma addr for setup_packet */
801 int start_frame; /* (modify) start frame (ISO) */ 802 int number_of_packets; /* (in) number of ISO packets */
803 int interval; /* (modify) transfer interval (INT/ISO) */
804 int error_count; /* (return) number of ISO errors */
805 void *context; /* (in) context for completion */
806 usb_complete_t complete; /* (in) completion routine */
807 struct usb_iso_packet_descriptor iso_frame_desc[0]; /* (in) ISO ONLY */
808 };
drivers/usb/storage/usb.h:
105 /* we allocate one of these for every device that we remember */
106 struct us_data {
107 /* The device we're working with
108 * It's important to note:
109 * (o) you must hold dev_semaphore to change pusb_dev
110 */
111 struct semaphore dev_semaphore; /* protect pusb_dev */
112 struct usb_device *pusb_dev; /* this usb_device */
113 struct usb_interface *pusb_intf; /* this interface */
114 struct us_unusual_dev *unusual_dev; /* device-filter entry */ 115 unsigned long flags; /* from filter initially */
116 unsigned int send_bulk_pipe; /* cached pipe values */
117 unsigned int recv_bulk_pipe;
118 unsigned int send_ctrl_pipe;
119 unsigned int recv_ctrl_pipe;
120 unsigned int recv_intr_pipe;
121
122 /* information about the device */
123 char vendor[USB_STOR_STRING_LEN];
124 char product[USB_STOR_STRING_LEN];
125 char serial[USB_STOR_STRING_LEN];
126 char *transport_name;
127 char *protocol_name;
128 u8 subclass;
129 u8 protocol;
130 u8 max_lun;
131
132 u8 ifnum; /* interface number */
133 u8 ep_bInterval; /* interrupt interval */
134
135 /* function pointers for this device */
136 trans_cmnd transport; /* transport function */
137 trans_reset transport_reset; /* transport device reset */
138 proto_cmnd proto_handler; /* protocol handler */
139
140 /* SCSI interfaces */
141 struct Scsi_Host *host; /* our dummy host data */ 142 struct scsi_cmnd *srb; /* current srb */
143
144 /* thread information */
145 int pid; /* control thread */ 146
147 /* control and bulk communications data */
148 struct urb *current_urb; /* USB requests */
149 struct usb_ctrlrequest *cr; /* control requests */
150 struct usb_sg_request current_sg; /* scatter-gather req. */
151 unsigned char *iobuf; /* I/O buffer */
152 dma_addr_t cr_dma; /* buffer DMA addresses */
153 dma_addr_t iobuf_dma;
154
155 /* mutual exclusion and synchronization structures */
156 struct semaphore sema; /* to sleep thread on */
157 struct completion notify; /* thread begin/end */
158 wait_queue_head_t dev_reset_wait; /* wait during reset */
159 wait_queue_head_t scsi_scan_wait; /* wait before scanning */
160 struct completion scsi_scan_done; /* scan thread end */
161
162 /* subdriver information */
163 void *extra; /* Any extra data */
164 extra_data_destructor extra_destructor;/* extra data destructor */
165 };
struct usb_ctrlrequest结构指针, usb规范
规定
关于下班后关闭电源的规定党章中关于入党时间的规定公务员考核规定下载规定办法文件下载宁波关于闷顶的规定
了一个控制请求的
格式
pdf格式笔记格式下载页码格式下载公文格式下载简报格式下载
为一个8个字节的数据包,
而struct usb_ctrlrequest正是专门为此而准备的8个字节的一个变量,所以控制传输中总会用到她.她的
定义在include/linux/usb_ch9.h中:
103 struct usb_ctrlrequest {
104 __u8 bRequestType;
105 __u8 bRequest;
106 __le16 wValue;
107 __le16 wIndex; 77
__le16 wLength; 108
109 } __attribute__ ((packed));
需要说明一点,在usb spec中,不叫cr,而叫setup packet,在struct urb里边就有这么一个名字
一样的成员,
在usb mass storage bulk only transport协议里面,规定的很清楚,要发送GET MAX LUN请求,
必须把bmRequestType设置为device to host,class,interface,同时把bRequest设置为254(FEh),
即咱们这里的0xfe,而wValue设置为0,wIndex设置为接口的编号,最后wLength设置为1.
byte0: bmRequestType,注意,在刚才咱们代码中数据结构struct ctrlrequest里边是写的
bRequestType,但是她们对应的是相同的冬冬.而之所以usb协议里写成bmRequestType,是因为她实
际上又是一个位图(m表示map),也就是说,尽管她只有1个byte,但是她仍然是当作8位来用.她的8位
的含义是:
D7: 控制传输数据传输的方向,0表示方向为主机到设备,1表示方向为设备到主机.(有时控制传
输除了发命令阶段以外,可能还有数据传输阶段,此处表示的是在数据传输那个阶段的传输方向.)
D6...5: 请求的类型,0称为
标准
excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载
类型(Standard),1称为Class,2称为Vendor,3则是保留的,
不能使用.
D4...0: 接收者,0表示设备,1表示接口,2表示端点,3表示其它,4...31都是保留的,不能使用.
定义于include/linux/usb.h
static inline void usb_fill_control_urb (struct urb *urb,
struct usb_device *dev,
unsigned int pipe,
unsigned char *setup_packet,
void *transfer_buffer,
int buffer_length,
usb_complete_t complete,
void *context) {
spin_lock_init(&urb->lock);
urb->dev = dev;
urb->pipe = pipe;
urb->setup_packet = setup_packet; urb->transfer_buffer = transfer_buffer;
urb->transfer_buffer_length = buffer_length;
urb->complete = complete;
urb->context = context;
}