Edit online

Driver (struct usb_driver)

31 Jan 2024
Read time: 6 minute(s)

USB Interface 这一层次的驱动就非常的多了,这一层主要是在 USB 传输层之上,针对 USB Device 的某个功能 Function 开发对应的 USB 功能业务驱动,即常说的 USB Client Software。在 USB 定义中,一个 Interface 就是一个 Function

USB Interface Driver 对应的数据结构为 struct usb_driver ,使用 usb_register_driver() 函数进行注册:
int usb_register_driver(struct usb_driver *new_driver, struct module *owner,
            const char *mod_name)
{

    /* (1) 设置 devices 标志为 0,表面这个驱动时给 usb interface 使用的 */
    new_driver->drvwrap.for_devices = 0;
    new_driver->drvwrap.driver.name = new_driver->name;
    new_driver->drvwrap.driver.bus = &usb_bus_type;
    new_driver->drvwrap.driver.probe = usb_probe_interface;
    new_driver->drvwrap.driver.remove = usb_unbind_interface;
    new_driver->drvwrap.driver.owner = owner;
    new_driver->drvwrap.driver.mod_name = mod_name;
    new_driver->drvwrap.driver.dev_groups = new_driver->dev_groups;
    spin_lock_init(&new_driver->dynids.lock);
    INIT_LIST_HEAD(&new_driver->dynids.list);

    retval = driver_register(&new_driver->drvwrap.driver);

}
一个最简单的 Usb Interface Driverusb_mouse_driver :
static const struct usb_device_id usb_mouse_id_table[] = {
    { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
        USB_INTERFACE_PROTOCOL_MOUSE) },
    { }     /* Terminating entry */
};
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,
};

module_usb_driver(usb_mouse_driver);
  • 首先根据得到的 endpoint 准备好 urb,创建好 input 设备:
    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;
        int error = -ENOMEM;
    
        interface = intf->cur_altsetting;
    
        if (interface->desc.bNumEndpoints != 1)
            return -ENODEV;
    
        /* (1) 得到当前 interface 中的第一个 endpoint,mouse 设备只需一个 endpoint */
        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);
        /* (2.1) 分配 input device */
        input_dev = input_allocate_device();
        if (!mouse || !input_dev)
            goto fail1;
    
        mouse->data = usb_alloc_coherent(dev, 8, GFP_ATOMIC, &mouse->data_dma);
        if (!mouse->data)
            goto fail1;
    
        /* (3.1) 分配 urb */
        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));
    
        /* (2.2) 初始化 input device */
        input_dev->name = mouse->name;
        input_dev->phys = mouse->phys;
        usb_to_input_id(dev, &input_dev->id);
        input_dev->dev.parent = &intf->dev;
    
        input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
        input_dev->keybit[BIT_WORD(BTN_MOUSE)] = BIT_MASK(BTN_LEFT) |
            BIT_MASK(BTN_RIGHT) | BIT_MASK(BTN_MIDDLE);
        input_dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
        input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_SIDE) |
            BIT_MASK(BTN_EXTRA);
        input_dev->relbit[0] |= BIT_MASK(REL_WHEEL);
    
        input_set_drvdata(input_dev, mouse);
    
        input_dev->open = usb_mouse_open;
        input_dev->close = usb_mouse_close;
    
        /* (3.2) 初始化 urb */
        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;
    
        /* (2.3) 注册 input device */
        error = input_register_device(mouse->dev);
        if (error)
            goto fail3;
    
        usb_set_intfdata(intf, mouse);
        return 0;
    
    fail3:
        usb_free_urb(mouse->irq);
    fail2:
        usb_free_coherent(dev, 8, mouse->data, mouse->data_dma);
    fail1:
        input_free_device(input_dev);
        kfree(mouse);
        return error;
    }
    
  • 在 input device 被 open 时提交 urb 启动传输:
    static int usb_mouse_open(struct input_dev *dev)
    {
        struct usb_mouse *mouse = input_get_drvdata(dev);
    
        mouse->irq->dev = mouse->usbdev;
        /* (1) 提交初始化好的 usb,开始查询数据 */
        if (usb_submit_urb(mouse->irq, GFP_KERNEL))
            return -EIO;
    
        return 0;
    }
    
  • 在传输完 urb 的回调函数中,根据读回的数据上报 input 事件,并且重新提交 urb 继续查询:
    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:                 /* success */
            break;
        case -ECONNRESET:       /* unlink */
        case -ENOENT:
        case -ESHUTDOWN:
            return;
        /* -EPIPE:  should clear the halt */
        default:                /* error */
            goto resubmit;
        }
    
        /* (1) 根据 urb 读回的数据,上报 input event */
        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:
        /* (2) 重新提交 urb 继续查询 */
        status = usb_submit_urb (urb, GFP_ATOMIC);
        if (status)
            dev_err(&mouse->usbdev->dev,
                "can't resubmit intr, %s-%s/input0, status %d\n",
                mouse->usbdev->bus->bus_name,
                mouse->usbdev->devpath, status);
    }