USB 总线引出两个重要的链表!
一个
USB 总线引出两个重要的链表,一个为 USB 设备链表,一个为 USB 驱动链表。设备链表包含各种系统中的 USB
设备以及这些设备的所有接口,驱动链表包含 USB 设备驱动程序(usb device driver)和 USB 驱动程序(usb
driver)。
USB 设备驱动程序(usb device driver)和 USB 驱动程序(usb driver)的区别是什么?
USB
设备驱动程序包含 USB 设备的一些通用特性,将与所有 USB 设备相匹配。在 USB core 定义了:struct
usb_device_driver usb_generic_driver。usb_generic_driver 是 USB
子系统中唯一的一个设备驱动程序对象。而 USB 驱动程序则是与接口相匹配,接口是一个完成特定功能的端点的集合。
设备是如何添加到设备链表上去的?
在设备插入 USB 控制器之后,USB core 即会将设备在系统中注册,添加到 USB 设备链表上去。
USB 设备驱动程序(usb device driver)是如何添加到驱动链表上去的?
在系统启动注册 USB core 时,USB 设备驱动程序即将被注册,也就添加到驱动链表上去了。
接口是如何添加到设备链表上去的?
在 USB 设备驱动程序和 USB 设备的匹配之后,USB core 会对设备进行配置,分析设备的结构之后会将设备所有接口都添加到设备链表上去。比如鼠标设备中有一个接口,USB core 对鼠标设备配置后,会将这个接口添加到设备链表上去。
USB 驱动程序(usb driver)是如何添加到驱动链表上去的?
在每个 USB
驱动程序的被注册时,USB 驱动程序即会添加到驱动链表上去。比如鼠标驱动程序,usb_mouse_init 函数将通过
usb_register(&usb_mouse_driver) 将鼠标驱动程序注册到 USB core
中,然后就添加到驱动链表中去了。其中 usb_mouse_driver 是描述鼠标驱动程序的结构体。
已配置状态(configured status)之后话
当鼠标的设备、接口都添加到设备链表,并且鼠
标驱动程序也添加到驱动链表上去了,系统就进入一种叫做已配置(configured)的状态。要达到已配置状态,将经历复杂的过程,USB core 为
USB 设备奉献着无怨无悔。在这个过程中,系统将会建立起该设备的的设备、配置、接口、设置、端点的描述信息,它们分别被
usb_device、usb_configuration、usb_interface、usb_host_interface、
usb_host_endpoint 结构体描述。
设备达到已配置状态后,首先当然就要进行 USB
驱动程序和相应接口的配对,对于鼠标设备来说则是鼠标驱动程序和鼠标中的接口的配对。USB core 会调用 usb_device_match
函数,通过比较设备中的接口信息和 USB 驱动程序中的 id_table,来初步决定该 USB
驱动程序是不是跟相应接口相匹配。通过这一道关卡后,USB core 会认为这个设备应该由这个驱动程序负责。
然而,仅仅这一步是不够的,接着,将会调用 USB 驱动程序中的 probe 函数对相应接口进行进一步检查。如果该驱动程序确实适合设备接口,对设备做一些初始化工作,分配 urb 准备数据传输。
当
鼠标设备在用户空间打开时,将提交 probe 函数构建的 urb 请求块,urb 将开始为传送数据而忙碌了。urb
请求块就像一个装东西的“袋子”,USB 驱动程序把“空袋子”提交给 USB
core,然后再交给主控制器,主控制器把数据放入这个“袋子”后再将装满数据的“袋子”通过 USB core 交还给 USB
驱动程序,这样一次数据传输就完成了。
以下是完全注释后的鼠标驱动程序代码 usbmouse.c
-
-
-
-
-
-
-
-
- #include
- #include
- #include
- #include
- #include
- #include
-
-
-
-
- #define DRIVER_VERSION "v1.6"
- #define DRIVER_AUTHOR "Vojtech Pavlik "
- #define DRIVER_DESC "USB HID Boot Protocol mouse driver"
- #define DRIVER_LICENSE "GPL"
-
- MODULE_AUTHOR(DRIVER_AUTHOR);
- MODULE_DESCRIPTION(DRIVER_DESC);
- MODULE_LICENSE(DRIVER_LICENSE);
-
-
-
-
- struct usb_mouse
- {
-
- char name[128];
-
- char phys[64];
-
- struct usb_device *usbdev;
-
- struct input_dev *dev;
-
- struct urb *irq;
-
- signed char *data;
-
- dma_addr_t data_dma;
- };
-
-
-
-
-
- static void usb_mouse_irq(struct urb *urb)
- {
-
-
-
-
-
-
-
-
-
-
- struct usb_mouse *mouse = urb->context;
- signed char *data = mouse->data;
- struct input_dev *dev = mouse->dev;
- int status;
-
-
-
-
-
-
-
-
-
-
- switch (urb->status)
- {
- case 0:
- break;
- case -ECONNRESET:
- case -ENOENT:
- case -ESHUTDOWN:
- return;
-
- default:
- goto resubmit;
- }
-
-
-
-
-
-
-
-
- input_report_key(dev, BTN_LEFT, data[0] & 0x01);
- input_report_key(dev, BTN_RIGHT, data[0] & 0x02);
- input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);
- input_report_key(dev, BTN_SIDE, data[0] & 0x08);
- input_report_key(dev, BTN_EXTRA, data[0] & 0x10);
- input_report_rel(dev, REL_X, data[1]);
- input_report_rel(dev, REL_Y, data[2]);
- input_report_rel(dev, REL_WHEEL, data[3]);
-
-
-
-
-
-
- input_sync(dev);
-
-
-
-
-
-
-
-
- resubmit:
- status = usb_submit_urb (urb, GFP_ATOMIC);
- if (status)
- err ("can't resubmit intr, %s-%s/input0, status %d",
- mouse->usbdev->bus->bus_name,
- mouse->usbdev->devpath, status);
- }
-
-
-
-
- static int usb_mouse_open(struct input_dev *dev)
- {
- struct usb_mouse *mouse = dev->private;
-
- mouse->irq->dev = mouse->usbdev;
- if (usb_submit_urb(mouse->irq, GFP_KERNEL))
- return -EIO;
-
- return 0;
- }
-
-
-
-
- static void usb_mouse_close(struct input_dev *dev)
- {
- struct usb_mouse *mouse = dev->private;
-
- usb_kill_urb(mouse->irq);
- }
-
-
-
-
- static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
- {
-
-
-
-
-
-
- struct usb_device *dev = interface_to_usbdev(intf);
- struct usb_host_interface *interface;
- struct usb_endpoint_descriptor *endpoint;
- struct usb_mouse *mouse;
- struct input_dev *input_dev;
- int pipe, maxp;
-
- interface = intf->cur_altsetting;
-
-
- if (interface->desc.bNumEndpoints != 1)
- return -ENODEV;
-
- endpoint = &interface->endpoint[0].desc;
- if (!usb_endpoint_is_int_in(endpoint))
- return -ENODEV;
-
-
-
-
-
- pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
- maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
-
-
- mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);
-
- input_dev = input_allocate_device();
- if (!mouse || !input_dev)
- goto fail1;
-
-
-
-
-
-
-
-
-
- mouse->data = usb_buffer_alloc(dev, 8, GFP_ATOMIC, &mouse->data_dma);
- if (!mouse->data)
- goto fail1;
-
-
-
-
-
- mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
- if (!mouse->irq)
- goto fail2;
-
-
- mouse->usbdev = dev;
- mouse->dev = input_dev;
-
-
- if (dev->manufacturer)
- strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));
-
- if (dev->product)
- {
- if (dev->manufacturer)
- strlcat(mouse->name, " ", sizeof(mouse->name));
- strlcat(mouse->name, dev->product, sizeof(mouse->name));
- }
-
- if (!strlen(mouse->name))
- snprintf(mouse->name, sizeof(mouse->name),
- "USB HIDBP Mouse %04x:%04x",
- le16_to_cpu(dev->descriptor.idVendor),
- le16_to_cpu(dev->descriptor.idProduct));
-
-
-
-
-
- usb_make_path(dev, mouse->phys, sizeof(mouse->phys));
- strlcat(mouse->phys, "/input0", sizeof(mouse->phys));
-
-
- input_dev->name = mouse->name;
-
- input_dev->phys = mouse->phys;
-
-
-
-
- usb_to_input_id(dev, &input_dev->id);
-
- input_dev->cdev.dev = &intf->dev;
-
-
- input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
-
- input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);
-
- input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);
-
- input_dev->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);
-
- input_dev->relbit[0] |= BIT(REL_WHEEL);
-
-
- input_dev->private = mouse;
-
- input_dev->open = usb_mouse_open;
-
- input_dev->close = usb_mouse_close;
-
-
-
-
-
-
-
- usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
- (maxp > 8 ? 8 : maxp),
- usb_mouse_irq, mouse, endpoint->bInterval);
- mouse->irq->transfer_dma = mouse->data_dma;
- mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
-
-
- input_register_device(mouse->dev);
-
-
-
-
-
-
- usb_set_intfdata(intf, mouse);
- return 0;
-
- fail2: usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);
- fail1: input_free_device(input_dev);
- kfree(mouse);
- return -ENOMEM;
- }
-
-
-
-
- static void usb_mouse_disconnect(struct usb_interface *intf)
- {
-
- struct usb_mouse *mouse = usb_get_intfdata (intf);
-
-
- usb_set_intfdata(intf, NULL);
- if (mouse)
- {
-
- usb_kill_urb(mouse->irq);
-
- input_unregister_device(mouse->dev);
-
- usb_free_urb(mouse->irq);
-
- usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);
-
- kfree(mouse);
- }
- }
-
-
-
-
-
-
-
-
- static struct usb_device_id usb_mouse_id_table [] = {
- { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
- USB_INTERFACE_PROTOCOL_MOUSE) },
- { }
- };
-
-
-
-
-
- MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);
-
-
-
-
- static struct usb_driver usb_mouse_driver = {
- .name = "usbmouse",
- .probe = usb_mouse_probe,
- .disconnect = usb_mouse_disconnect,
- .id_table = usb_mouse_id_table,
- };
-
-
-
-
- static int __init usb_mouse_init(void)
- {
- int retval = usb_register(&usb_mouse_driver);
- if (retval == 0)
- info(DRIVER_VERSION ":" DRIVER_DESC);
- return retval;
- }
-
-
-
-
- static void __exit usb_mouse_exit(void)
- {
- usb_deregister(&usb_mouse_driver);
- }
-
- module_init(usb_mouse_init);
- module_exit(usb_mouse_exit);