原文链接地址:http://www.linuxidc.com/Linux/2012-12/76197p9.htm
跟USB鼠标类型一样,USB键盘也属于HID类型,代码在/dirver/hid/usbhid/usbkbd.c下。USB键盘除了提交中断URB外,还需要提交控制URB。不多话,我们看代码
- static int __init usb_kbd_init(void)
- {
- int result = usb_register(&usb_kbd_driver);
- if (result == 0)
- printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
- DRIVER_DESC "\n");
- return result;
- }
- static struct usb_driver usb_kbd_driver = {
- .name = "usbkbd",
- .probe = usb_kbd_probe,
- .disconnect = usb_kbd_disconnect,
- .id_table = usb_kbd_id_table, //驱动设备ID表,用来指定设备或接口
- };
下面跟踪usb_driver中的probe
- static int usb_kbd_probe(struct usb_interface *iface,
- const struct usb_device_id *id)
- {
- struct usb_device *dev = interface_to_usbdev(iface); //通过接口获取USB设备指针
- struct usb_host_interface *interface; //设置
- struct usb_endpoint_descriptor *endpoint; //端点描述符
- struct usb_kbd *kbd; //usb_kbd私有数据
- struct input_dev *input_dev; //input设备
- int i, pipe, maxp;
- int error = -ENOMEM;
- interface = iface->cur_altsetting; //获取设置
- if (interface->desc.bNumEndpoints != 1) //与mouse一样只有一个端点
- return -ENODEV;
- endpoint = &interface->endpoint[0].desc; //获取端点描述符
- if (!usb_endpoint_is_int_in(endpoint)) //检查端点是否为中断输入端点
- return -ENODEV;
- pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress); //将endpoint设置为中断IN端点
- maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); //端点传输的最大数据包
- kbd = kzalloc(sizeof(struct usb_kbd), GFP_KERNEL); //分配urb
- input_dev = input_allocate_device(); //分配input设备空间
- if (!kbd || !input_dev)
- goto fail1;
- if (usb_kbd_alloc_mem(dev, kbd)) //分配urb空间和其他缓冲区
- goto fail2;
- kbd->usbdev = dev; //给内嵌结构体赋值
- kbd->dev = input_dev;
- if (dev->manufacturer) //拷贝厂商ID
- strlcpy(kbd->name, dev->manufacturer, sizeof(kbd->name));
- if (dev->product) { //拷贝产品ID
- if (dev->manufacturer)
- strlcat(kbd->name, " ", sizeof(kbd->name));
- strlcat(kbd->name, dev->product, sizeof(kbd->name));
- }
- if (!strlen(kbd->name)) //检测不到厂商名字
- snprintf(kbd->name, sizeof(kbd->name),
- "USB HIDBP Keyboard %04x:%04x",
- le16_to_cpu(dev->descriptor.idVendor),
- le16_to_cpu(dev->descriptor.idProduct));
- //设备链接地址
- usb_make_path(dev, kbd->phys, sizeof(kbd->phys));
- strlcat(kbd->phys, "/input0", sizeof(kbd->phys));
- input_dev->name = kbd->name; //给input_dev结构体赋值
- input_dev->phys = kbd->phys;
- usb_to_input_id(dev, &input_dev->id); //拷贝usb_driver的支持给input,设置bustype,vendo,product等
- input_dev->dev.parent = &iface->dev;
- input_set_drvdata(input_dev, kbd); //将kbd设置为input的私有数据
- input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
- BIT_MASK(EV_REP); //支持的按键事件类型
- input_dev->ledbit[0] = BIT_MASK(LED_NUML) | BIT_MASK(LED_CAPSL) |
- BIT_MASK(LED_SCROLLL) | BIT_MASK(LED_COMPOSE) |
- BIT_MASK(LED_KANA); //EV_LED事件支持的事件码
- for (i = 0; i < 255; i++)
- set_bit(usb_kbd_keycode[i], input_dev->keybit); //EV_KEY事件支持的事件码(即设置支持的键盘码)
- clear_bit(0, input_dev->keybit);
- input_dev->event = usb_kbd_event; //定义event函数
- input_dev->open = usb_kbd_open;
- input_dev->close = usb_kbd_close;
- usb_fill_int_urb(kbd->irq, dev, pipe,
- kbd->new, (maxp > 8 ? 8 : maxp),
- usb_kbd_irq, kbd, endpoint->bInterval);//填充中断urb
- kbd->irq->transfer_dma = kbd->new_dma;
- kbd->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
- kbd->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
- kbd->cr->bRequest = 0x09;//设置控制请求的格式
- kbd->cr->wValue = cpu_to_le16(0x200);
- kbd->cr->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
- kbd->cr->wLength = cpu_to_le16(1);
- usb_fill_control_urb(kbd->led, dev, usb_sndctrlpipe(dev, 0),
- (void *) kbd->cr, kbd->leds, 1,
- usb_kbd_led, kbd);//填充控制urb
- kbd->led->transfer_dma = kbd->leds_dma;
- kbd->led->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
- error = input_register_device(kbd->dev);
- if (error)
- goto fail2;
- usb_set_intfdata(iface, kbd);
- device_set_wakeup_enable(&dev->dev, 1);
- return 0;
- fail2:
- usb_kbd_free_mem(dev, kbd);
- fail1:
- input_free_device(input_dev);
- kfree(kbd);
- return error;
- }
在上面的probe中,我们主要是初始化一些结构体,然后提交中断urb和控制urb,并注册input设备。其中有几个地方需要细看下,其一,usb_kbd_alloc_mem的实现。其二,设置控制请求的格式。
先来看看usb_kbd_alloc_mem的实现
- static int usb_kbd_alloc_mem(struct usb_device *dev, struct usb_kbd *kbd)
- {
- if (!(kbd->irq = usb_alloc_urb(0, GFP_KERNEL))) //分配中断urb
- return -1;
- if (!(kbd->led = usb_alloc_urb(0, GFP_KERNEL))) //分配控制urb
- return -1;
- if (!(kbd->new = usb_alloc_coherent(dev, 8, GFP_ATOMIC, &kbd->new_dma)))
- return -1; //分配中断urb使用的缓冲区
- if (!(kbd->cr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL)))
- return -1; //分配控制urb使用的控制请求描述符
- if (!(kbd->leds = usb_alloc_coherent(dev, 1, GFP_ATOMIC, &kbd->leds_dma)))
- return -1; //分配控制urb使用的缓冲区
- return 0;
- }
这里我们需要明白中断urb和控制urb需要分配不同的urb结构体,同时在提交urb之前,需要填充的内容也不同,中断urb填充的是缓冲区和中断处理函数,控制urb填充的是控制请求描述符与回调函数。
设置控制请求的格式。cr是struct usb_ctrlrequest结构的指针,USB协议中规定一个控制请求的格式为一个8个字节的数据包,其定义如下
- /**
- * struct usb_ctrlrequest - SETUP data for a USB device control request
- * @bRequestType: matches the USB bmRequestType field
- * @bRequest: matches the USB bRequest field
- * @wValue: matches the USB wValue field (le16 byte order)
- * @wIndex: matches the USB wIndex field (le16 byte order)
- * @wLength: matches the USB wLength field (le16 byte order)
- *
- * This structure is used to send control requests to a USB device. It matches
- * the different fields of the USB 2.0 Spec section 9.3, table 9-2. See the
- * USB spec for a fuller description of the different fields, and what they are
- * used for.
- *
- * Note that the driver for any interface can issue control requests.
- * For most devices, interfaces don\'t coordinate with each other, so
- * such requests may be made at any time.
- */
- struct usb_ctrlrequest {
- __u8 bRequestType; //设定传输方向、请求类型等
- __u8 bRequest; //指定哪个请求,可以是规定的标准值也可以是厂家定义的值
- __le16 wValue; //即将写到寄存器的数据
- __le16 wIndex; //接口数量,也就是寄存器的偏移地址
- __le16 wLength; //数据传输阶段传输多少个字节
- } __attribute__ ((packed));
USB协议中规定,所有的USB设备都会响应主机的一些请求,这些请求来自USB主机控制器,主机控制器通过设备的默认控制管道发出这些请求。默认的管道为0号端口对应的那个管道。
同样这个input设备首先由用户层调用open函数,所以先看看input中定义的open
- static int usb_kbd_open(struct input_dev *dev)
- {
- struct usb_kbd *kbd = input_get_drvdata(dev);
- kbd->irq->dev = kbd->usbdev;
- if (usb_submit_urb(kbd->irq, GFP_KERNEL))
- return -EIO;
- return 0;
- }
因为这个驱动里面有一个中断urb一个控制urb,我们先看中断urb的处理流程。中断urb在input的open中被提交后,当USB core处理完毕,会通知这个USB设备驱动,然后执行回调函数,也就是中断处理函数usb_kbd_irq
- static void usb_kbd_irq(struct urb *urb)
- {
- struct usb_kbd *kbd = urb->context;
- int i;
- switch (urb->status) {
- case 0: /* success */
- break;
- case -ECONNRESET: /* unlink */
- case -ENOENT:
- case -ESHUTDOWN:
- return;
- /* -EPIPE: should clear the halt */
- default: /* error */
- goto resubmit;
- }
- //报告usb_kbd_keycode[224..231]8按键状态
- //KEY_LEFTCTRL,KEY_LEFTSHIFT,KEY_LEFTALT,KEY_LEFTMETA,
- //KEY_RIGHTCTRL,KEY_RIGHTSHIFT,KEY_RIGHTALT,KEY_RIGHTMETA
- for (i = 0; i < 8; i++)
- input_report_key(kbd->dev, usb_kbd_keycode[i + 224], (kbd->new[0] >> i) & 1);
- //若同时只按下1个按键则在第[2]个字节,若同时有两个按键则第二个在第[3]字节,类推最多可有6个按键同时按下
- for (i = 2; i < 8; i++) {
- //获取键盘离开的中断
- //同时没有该KEY的按下状态
- if (kbd->old[i] > 3 && memscan(kbd->new + 2, kbd->old[i], 6) == kbd->new + 8) {
- if (usb_kbd_keycode[kbd->old[i]])
- input_report_key(kbd->dev, usb_kbd_keycode[kbd->old[i]], 0);
- else
- hid_info(urb->dev,
- "Unknown key (scancode %#x) released.\n",
- kbd->old[i]);
- }
- //获取键盘按下的中断
- //同时没有该KEY的离开状态
- if (kbd->new[i] > 3 && memscan(kbd->old + 2, kbd->new[i], 6) == kbd->old + 8) {
- if (usb_kbd_keycode[kbd->new[i]])
- input_report_key(kbd->dev, usb_kbd_keycode[kbd->new[i]], 1);
- else
- hid_info(urb->dev,
- "Unknown key (scancode %#x) released.\n",
- kbd->new[i]);
- }
- }
- input_sync(kbd->dev); //同步设备,告知事件的接收者驱动已经发出了一个完整的报告
- memcpy(kbd->old, kbd->new, 8); //防止未松开时被当成新的按键处理
- resubmit:
- i = usb_submit_urb (urb, GFP_ATOMIC);
- if (i)
- hid_err(urb->dev, "can\'t resubmit intr, %s-%s/input0, status %d",
- kbd->usbdev->bus->bus_name,
- kbd->usbdev->devpath, i);
- }
这个就是中断urb的处理流程,跟前面讲的的USB鼠标中断处理流程类似。好了,我们再来看看剩下的控制urb处理流程吧。
我们有个疑问,我们知道在probe中,我们填充了中断urb和控制urb,但是在input的open中,我们只提交了中断urb,那么控制urb什么时候提交呢?
我们知道对于input子系统,如果有事件被响应,我们会调用事件处理层的event函数,而该函数最终调用的是input下的event。所以,对于input设备,我们在USB键盘驱动中只设置了支持LED选项,也就是ledbit项,这是怎么回事呢?刚才我们分析的那个中断urb其实跟这个 input基本没啥关系,中断urb并不是像讲键盘input实现的那样属于input下的中断。我们在USB键盘驱动中的input子系统中只设计了 LED选项,那么当input子系统有按键选项的时候必然会使得内核调用调用事件处理层的event函数,最终调用input下的event。好了,那我们来看看input下的event干了些什么。
- static int usb_kbd_event(struct input_dev *dev, unsigned int type,
- unsigned int code, int value)
- {
- struct usb_kbd *kbd = input_get_drvdata(dev);
- if (type != EV_LED)//不支持LED事件
- return -1;
- //获取指示灯的目标状态
- kbd->newleds = (!!test_bit(LED_KANA, dev->led) << 3) | (!!test_bit(LED_COMPOSE, dev->led) << 3) |
- (!!test_bit(LED_SCROLLL, dev->led) << 2) | (!!test_bit(LED_CAPSL, dev->led) << 1) |
- (!!test_bit(LED_NUML, dev->led));
- if (kbd->led->status == -EINPROGRESS)
- return 0;
- //指示灯状态已经是目标状态则不需要再做任何操作
- if (*(kbd->leds) == kbd->newleds)
- return 0;
- *(kbd->leds) = kbd->newleds;
- kbd->led->dev = kbd->usbdev;
- if (usb_submit_urb(kbd->led, GFP_ATOMIC))
- pr_err("usb_submit_urb(leds) failed\n");
- //提交控制urb
- return 0;
- }
当在input的event里提交了控制urb后,经过URB处理流程,最后返回给USB设备驱动的回调函数,也就是在probe中定义的usb_kbd_led
- static void usb_kbd_led(struct urb *urb)
- {
- struct usb_kbd *kbd = urb->context;
- if (urb->status)
- hid_warn(urb->dev, "led urb status %d received\n",
- urb->status);
- if (*(kbd->leds) == kbd->newleds)
- return;
- *(kbd->leds) = kbd->newleds;
- kbd->led->dev = kbd->usbdev;
- if (usb_submit_urb(kbd->led, GFP_ATOMIC))
- hid_err(urb->dev, "usb_submit_urb(leds) failed\n");
- }
总结下,我们的控制urb走的是先由input的event提交,触发后由控制urb的回调函数再次提交。好了,通过USB鼠标,我们已经知道了控制urb和中断urb的设计和处理流程。