分类: Linux驱动开发 2010-08-25 08:58 785人阅读 评论(0) 收藏 举报

usb_control_msg是没有用到urb的在USB中简单进行发送和接收的一种机制,用于少量的数据通信。原型为:

程序代码 程序代码
linux+v2.6.35/drivers/usb/core/message.c
104
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        
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 。在内核中为这个三部分分别作了宏定义,分别对应这个字段的三部分:

程序代码 程序代码
linux+v2.6.35/include/linux/usb/ch9.h

  41
  42
  48#define USB_DIR_OUT                                 
  49#define USB_DIR_IN                      0x80            
  50
  51
  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
  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
  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个字节,描述的是接口号

Logo

更多推荐