usb_control_msg函數用法和說明

usb_control_msg是沒有用到urb的在USB中簡單進行發送和接收的一種機制,用於少許的數據通訊。原型爲:linux

程序代碼 程序代碼
linux+v2.6.35/drivers/usb/core/message.c
104/**
105 * usb_control_msg - Builds a control urb, sends it off and waits for completion
106 * @dev: pointer to the usb device to send the message to
107 * @pipe: endpoint "pipe" to send the message to
108 * @request: USB message request value
109 * @requesttype: USB message request type value
110 * @value: USB message value
111 * @index: USB message index value
112 * @data: pointer to the data to send
113 * @size: length in bytes of the data to send
114 * @timeout: time in msecs to wait for the message to complete before timing
115 *      out (if 0 the wait is forever)
116 *
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 */
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); 


一 般對於 struct usb_device *dev, unsigned int pipe, __u8 request,這前三個參數和void *data,__u16 size, int timeout後三個參數沒有什麼疑問,主要是中間幾個__u8 requesttype, __u16 value, __u16 index,

requesttype 
requesttype有三部分組成,見之前日誌:requesttype 。在內核中爲這個三部分分別做了宏定義,分別對應這個字段的三部分:less

程序代碼 程序代碼
linux+v2.6.35/include/linux/usb/ch9.h
/* 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 */
  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個字節,描述的是接口號async

相關文章
相關標籤/搜索