1 static int usb_kbd_probe(struct usb_interface *iface, const struct 2 usb_device_id *id) 3 { 4 struct usb_device *dev = interface_to_usbdev(iface); 5 struct usb_host_interface *interface; 6 struct usb_endpoint_descriptor *endpoint; 7 struct usb_kbd *kbd; 8 struct input_dev *input_dev; 9 int i, pipe, maxp; 10 11 interface = iface->cur_altsetting; 12 /* 设备是否适合本驱动?*/ 13 if (interface->desc.bNumEndpoints != 1) 14 return - ENODEV; 15 16 endpoint = &interface->endpoint[0].desc; 17 if (!(endpoint->bEndpointAddress &USB_DIR_IN)) 18 return - ENODEV; 19 if ((endpoint->bmAttributes &USB_ENDPOINT_XFERTYPE_MASK) != 20 USB_ENDPOINT_XFER_INT) 21 return - ENODEV; 22 23 pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);//创建端点的管道 24 maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe)); 25 26 kbd = kzalloc(sizeof(struct usb_kbd), GFP_KERNEL); 27 input_dev = input_allocate_device();//分配input_dev结构体 28 if (!kbd || !input_dev) //分配 29 goto fail1; 30 31 if (usb_kbd_alloc_mem(dev, kbd)) 32 goto fail2; 33 34 kbd->usbdev = dev; 35 kbd->dev = input_dev; 36 37 if (dev->manufacturer)//制造商名非空 38 strlcpy(kbd->name, dev->manufacturer, sizeof(kbd->name)); 39 40 if (dev->product)//产品名非空 41 { 42 if (dev->manufacturer) 43 strlcat(kbd->name, " ", sizeof(kbd->name)); 44 strlcat(kbd->name, dev->product, sizeof(kbd->name)); 45 } 46 47 if (!strlen(kbd->name)) 48 snprintf(kbd->name, sizeof(kbd->name), "USB HIDBP Keyboard 49 %04x:%04x",le16_to_cpu(dev->descriptor.idVendor), le16_to_cpu(dev 50 ->descriptor.idProduct)); 51 52 usb_make_path(dev, kbd->phys, sizeof(kbd->phys)); 53 strlcpy(kbd->phys, "/input0", sizeof(kbd->phys)); 54 /* 输入设备初始化 */ 55 input_dev->name = kbd->name; 56 input_dev->phys = kbd->phys; 57 usb_to_input_id(dev, &input_dev->id); 58 input_dev->cdev.dev = &iface->dev; 59 input_dev->private = kbd; 60 61 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_LED) | BIT(EV_REP); 62 input_dev->ledbit[0] = BIT(LED_NUML) | BIT(LED_CAPSL) | 63 BIT(LED_SCROLLL) |BIT(LED_COMPOSE) | BIT(LED_KANA); 64 65 for (i = 0; i < 255; i++) 66 set_bit(usb_kbd_keycode[i], input_dev->keybit); 67 clear_bit(0, input_dev->keybit); 68 69 input_dev->event = usb_kbd_event; 70 input_dev->open = usb_kbd_open; 71 input_dev->close = usb_kbd_close; 72 /* 初始化中断urb */ 73 usb_fill_int_urb(kbd->irq, dev, pipe, kbd->new, (maxp > 8 ? 8 : maxp), 74 usb_kbd_irq, kbd, endpoint->bInterval); 75 kbd->irq->transfer_dma = kbd->new_dma; 76 kbd->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; 77 78 kbd->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE; 79 kbd->cr->bRequest = 0x09; 80 kbd->cr->wValue = cpu_to_le16(0x200); 81 kbd->cr->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber); 82 kbd->cr->wLength = cpu_to_le16(1); 83 /* 初始化控制urb */ 84 usb_fill_control_urb(kbd->led, dev, usb_sndctrlpipe(dev, 0), (void*)kbd->cr, 85 kbd->leds, 1, usb_kbd_led, kbd); 86 kbd->led->setup_dma = kbd->cr_dma; 87 kbd->led->transfer_dma = kbd->leds_dma; 88 kbd->led->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP | 89 URB_NO_SETUP_DMA_MAP); 90 input_register_device(kbd->dev); //注册输入设备 91 92 usb_set_intfdata(iface, kbd); //设置接口私有数据 93 return 0; 94 95 fail2: usb_kbd_free_mem(dev, kbd); 96 fail1: input_free_device(input_dev); 97 kfree(kbd); 98 return - ENOMEM; 99 }
|