转自http://blog.csdn.net/jiang_dlut/article/details/5836398
//usb_control_msg是没有用到urb的在USB中简单进行发送和接收的一种机制,用于少量的数据通信
在毫秒等待消息前完成时间
out (if 0 the wait is forever)
(如果0等待是永远)
117 * Context: !in_interrupt ()118 *
119 * This function sends a simple control message to a specified endpoint and
120 * waits for the message to complete, or timeout.
121 *这个函数用于发送一个简单的控制信息到一个指定的端点并等待信息传输完成或者传输超时
122 * If successful, it returns the number of bytes transferred, otherwise a
123 * negative error number.
124 *如果成功,返回已经传输成功的字节数,负责返回一个负的错误值
125 * Don't use this function from within an interrupt context, like a bottom half
126 * handler. If you need an asynchronous message, or need to send a message
127 * from within interrupt context, use usb_submit_urb().
128 * If a thread in your driver uses this call, make sure your disconnect()
129 * method can wait for it to complete. Since you don't have a handle on the
130 * URB used, you can't cancel the request.
131 */不要在中断上下文中使用这个函数,比如下半部分。如果需要一个异步信息,或者需要在中断中发送信息,需要使用usb_submit_urb(),如果驱动里的一个线程使用这个机制,确保你的disconnect()函数能够等待它完成,一旦你在URB使用中没有对其进行处理,你不能取消这个请求。
一、函数原型
132int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request,
133 __u8 requesttype, __u16 value, __u16 index, void *data,
134 __u16 size, int timeout)
135{
136 struct usb_ctrlrequest *dr;
137 int ret;
138
139 dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
140 if (!dr)
141 return -ENOMEM;
142
143 dr->bRequestType = requesttype;
144 dr->bRequest = request;
145 dr->wValue = cpu_to_le16(value);
146 dr->wIndex = cpu_to_le16(index);
147 dr->wLength = cpu_to_le16(size);
148
149 /* dbg("usb_control_msg"); */
150
151 ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
152
153 kfree(dr);
154
155 return ret;
156}
157EXPORT_SYMBOL_GPL(usb_control_msg);
二、requesttype, value, index参数分析
一 般对于 struct usb_device *dev, unsigned int pipe, __u8 request,这前三个参数和void *data,__u16 size, int timeout后三个参数没有什么疑问,主要是中间几个__u8 requesttype, __u16 value, __u16 index,
requesttype
requesttype有三部分组成,见以前日志:requesttype 。在内核中为这个三部分分别作了宏定义,分别对应这个字段的三部分:
/* CONTROL REQUEST SUPPORT */
41
42/*
43 * USB directions
44 *
45 * This bit flag is used in endpoint descriptors' bEndpointAddress field.
46 * It's also one of three fields in control requests bRequestType.
47 */
48#define USB_DIR_OUT 0 /* to device */
49#define USB_DIR_IN 0x80 /* to host */
50
51/*
52 * USB types, the second of three bRequestType fields
53 */
54#define USB_TYPE_MASK (0x03 << 5)
55#define USB_TYPE_STANDARD (0x00 << 5)
56#define USB_TYPE_CLASS (0x01 << 5)
57#define USB_TYPE_VENDOR (0x02 << 5)
58#define USB_TYPE_RESERVED (0x03 << 5)
59
60/*
61 * USB recipients, the third of three bRequestType fields
62 */USB的接收
63#define USB_RECIP_MASK 0x1f
64#define USB_RECIP_DEVICE 0x00
65#define USB_RECIP_INTERFACE 0x01
66#define USB_RECIP_ENDPOINT 0x02
67#define USB_RECIP_OTHER 0x03
68/* From Wireless USB 1.0 */
69#define USB_RECIP_PORT 0x04
70#define USB_RECIP_RPIPE 0x05
value :2个字节,高字节是报告类型(1为输入,2为输出,3为特性);低字节为报告ID(预设为0)。例如:
wValue.LowByte 00h Report ID
wValue.HiByte 03h Feature Report
index :索引字段是2个字节,描述的是接口号
三、usb_ctrlrequest结构体
struct usb_ctrlrequest|-----------------------|
| __u8 bRequestType -|
| __u8 bRequest -|
| __le16 -wValue -|
| __le16 -wIndex -|
| __le16 -wLength -|
|-----------------------|
这个数据结构就是SETUP信包的内容,而缓冲区的内容,就是随后的数据信包的内容。
---------------------------------------------------------------
bRequestType
D7 数据的传输方向:0表示从主机到设备; 1表示从设备到主机;
D6~5 命令的类型: 0表示标准命令; 1表示类命令; 2表示厂商提供的命令; 3保留;
D4~0 接收对象; 0表示设备; 1表示接口; 2表示端点; 3表示其他;
bRequest
命令的序号(其实就是命令);所有的命令都是以不同编码值的方式传递给设备的,bRequest就表示USB命令的编码值
wValue, wIndex
这两个字段对于不同的命令有不同的含义
wLength
表示在完成命令控制传输的数据阶段,要求传输数据的字节长度。一般不论是输入还是输出都要求给出准确的数字。当命令不需要传输数据时,此字段设为0
USB标准命令
---------------------------------------------------------------
命令 bReuestType bRequest wValue wIndex wLength 数据
Get_Descriptor 1000 0000b -GET_DESCRIPTOR 描述符类型 -0或语言ID --描述符长度 --描述符
-描述符的索引号
wValue 高8位表示描述符类型; 低8位表示描述符索引号
GET_DESCRIPTOR = 6
GetHubdescriptor = 0XA0
Get_Descriptor = 0X80
---------------------------------------------------------------
bRequestType bRequest wValue wIndex wLength
80 6 100 0 12
80 6 200 0 9
80 6 300 0 FF
80 6 301 409 FF
80 6 302 409 FF
80 6 303 409 FF
100 表示获取设备描述符usb_device_descriptor
200 表示获取配置描述符usb_config_descriptor
300 表示获取字符描述符
301 iSerialNumber
302 iProduct
303 iManufacturer