框架
usb 驱动是基于usb core 的,设备插上之后,host 层自然会进行识别,设备驱动通过core层的接口操作设备,而不用直接面对usb硬件。对于应用层需要封装成一个usb 的设备。
驱动是基于urb 数据进行操作的。
49 static void usb_mouse_irq(struct urb *urb)50 { 51 struct usb_mouse *mouse = urb->context;52 signed char *data = mouse->data;53 struct input_dev *dev = mouse->dev;54 int status;55 56 switch (urb->status) {57 case 0: /* success */58 break;59 case -ECONNRESET: /* unlink */60 case -ENOENT:61 case -ESHUTDOWN:62 return;63 /* -EPIPE: should clear the halt */64 default: /* error */65 goto resubmit;66 }67 68 input_report_key(dev, BTN_LEFT, data[0] & 0x01);69 input_report_key(dev, BTN_RIGHT, data[0] & 0x02);70 input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);71 input_report_key(dev, BTN_SIDE, data[0] & 0x08);72 input_report_key(dev, BTN_EXTRA, data[0] & 0x10);73 74 input_report_rel(dev, REL_X, data[1]);75 input_report_rel(dev, REL_Y, data[2]);76 input_report_rel(dev, REL_WHEEL, data[3]);77 78 input_sync(dev);
...
}
106 static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)
107 {
111 struct usb_mouse *mouse;
112 struct input_dev *input_dev;
137 mouse->irq = usb_alloc_urb(0, GFP_KERNEL);
138 if (!mouse->irq)
139 goto fail2;
175 input_set_drvdata(input_dev, mouse);
180 usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,
181 (maxp > 8 ? 8 : maxp),
182 usb_mouse_irq, mouse, endpoint->bInterval);
186 error = input_register_device(mouse->dev);
187 if (error)
188 goto fail3;
...
}