USB探测函数:
1 static int skel_probe(struct usb_interface *interface, const struct usb_device_id *id)
2 {
3 struct usb_skel *dev = NULL;
4 struct usb_host_interface *iface_desc;
5 struct usb_endpoint_descriptor *endpoint;
6 size_t buffer_size;
7 int i;
8 int retval = -ENOMEM;
9
10 /* 分配设备状态的内存并初始化 */
11 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
12 if (dev == NULL) {
13 err("Out of memory");
14 goto error;
15 }
16 kref_init(&dev->kref);
17 sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
18
19 dev->udev = usb_get_dev(interface_to_usbdev(interface));
20 dev->interface = interface;
21
22 /* 设置端点信息 */
23 /* 仅使用第一个bulk-in和bulk-out */
24 iface_desc = interface->cur_altsetting;
25 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
26 endpoint = &iface_desc->endpoint.desc;
27
28 if (!dev->bulk_in_endpointAddr &&
29 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
30 == USB_DIR_IN) &&
31 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
32 == USB_ENDPOINT_XFER_BULK)) {
33 /* 找到了一个批量IN端点 */
34 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
35 dev->bulk_in_size = buffer_size;
36 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
37 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
38 if (!dev->bulk_in_buffer) {
39 err("Could not allocate bulk_in_buffer");
40 goto error;
41 }
42 }
43
44 if (!dev->bulk_out_endpointAddr &&
45 ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
46 == USB_DIR_OUT) &&
47 ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
48 == USB_ENDPOINT_XFER_BULK)) {
49 /* 找到了一个批量OUT端点 */
50 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
51 }
52 }
53 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
54 err("Could not find both bulk-in and bulk-out endpoints");
55 goto error;
56 }
57
58 /* 在设备结构中保存数据指针 */
59 usb_set_intfdata(interface, dev);
60
61 /* 注册USB设备 */
62 retval = usb_register_dev(interface, &skel_class);
63 if (retval) {
64 /* something prevented us from registering this driver */
65 err("Not able to get a minor for this device.");
66 usb_set_intfdata(interface, NULL);
67 goto error;
68 }
69
70 /* 告知用户设备依附于什么node */
71 info("USB Skeleton device now attached to USBSkel-%d", interface->minor);
72 return 0;
73
74 error:
75 if (dev)
76 kref_put(&dev->kref, skel_delete);
77 return retval;
78 }
--------------------next---------------------
阅读(704) | 评论(0) | 转发(0) |