内核:linux-3.4.99
学习了韦东山第二期视频关于USB鼠标模拟键盘那一章,现在自己简单分析下内核自带的usb键盘源码。(drivers\hid\usbhid\Usbkbd.c)
当插入键盘的USB口时,会调用probe函数,分析如下:
static int usb_kbd_probe(struct usb_interface *iface,
const struct usb_device_id *id)
{
struct usb_device *dev = interface_to_usbdev(iface);
struct usb_host_interface *interface;
struct usb_endpoint_descriptor *endpoint;
struct usb_kbd *kbd;
struct input_dev *input_dev;
int i, pipe, maxp;
int error = -ENOMEM;
interface = iface->cur_altsetting;
//desc.bNumEndpoints !=1时,枚举不正常
if (interface->desc.bNumEndpoints != 1)
return -ENODEV;
endpoint = &interface->endpoint[0].desc;
//检测端点是不是输入中断输入,不是的话,退出
if (!usb_endpoint_is_int_in(endpoint))
return -ENODEV;
//USB设备的源地址
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
//USB设备的最大包长度,最后返回的数据长度,相当于endpoint->wMaxPacketSize;
maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
//分配一个usb_kbd结构体,该结构体里包含了USB键盘用到的一些参数
kbd = kzalloc(sizeof(struct usb_kbd), GFP_KERNEL);
//分配一个input_dev结构
input_dev = input_allocate_device();
if (!kbd || !input_dev)
goto fail1;
if (usb_kbd_alloc_mem(dev, kbd))
goto fail2;
kbd->usbdev = dev;
kbd->dev = input_dev;
//初始化一个led自旋锁
spin_lock_init(&kbd->leds_lock);
/*读取该USB键盘的一些参数,并打印*/
if (dev->manufacturer)
strlcpy(kbd->name, dev->manufacturer, sizeof(kbd->name));
if (dev->product) {
if (dev->manufacturer)
strlcat(kbd->name, " ", sizeof(kbd->name));
strlcat(kbd->name, dev->product, sizeof(kbd->name));
}
if (!strlen(kbd->name))
snprintf(kbd->name, sizeof(kbd->name),
"USB HIDBP Keyboard %04x:%04x",
le16_to_cpu(dev->descriptor.idVendor),
le16_to_cpu(dev->descriptor.idProduct));
usb_make_path(dev, kbd->phys, sizeof(kbd->phys));
strlcat(kbd->phys, "/input0", sizeof(kbd->phys));
input_dev->name = kbd->name;
input_dev->phys = kbd->phys;
usb_to_input_id(dev, &input_dev->id);
input_dev->dev.parent = &iface->dev;
input_set_drvdata(input_dev, kbd);
//设置该设备能产生那些类,KEY,LED,REP类
input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
BIT_MASK(EV_REP);
//设置LED类能产生哪些事件
input_dev->ledbit[0] = BIT_MASK(LED_NUML) | BIT_MASK(LED_CAPSL) |
BIT_MASK(LED_SCROLLL) | BIT_MASK(LED_COMPOSE) |
BIT_MASK(LED_KANA);
//设置键盘能产生哪些事件,该处定义了一个usb_kbd_keycode数组来存储这些事件值,
// 这些事件值可以参考宏KEY_I 等等
for (i = 0; i < 255; i++)
set_bit(usb_kbd_keycode[i], input_dev->keybit);
clear_bit(0, input_dev->keybit);
input_dev->event = usb_kbd_event;
input_dev->open = usb_kbd_open;
input_dev->close = usb_kbd_close;
/*使用目的,源,长度,3要素设置urb"
* kbd作为参数传入usb_kbd_irq中断处理函数
*/
usb_fill_int_urb(kbd->irq, dev, pipe,
kbd->new, (maxp > 8 ? 8 : maxp),
usb_kbd_irq, kbd, endpoint->bInterval);
kbd->irq->transfer_dma = kbd->new_dma;
kbd->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
kbd->cr->bRequestType = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
kbd->cr->bRequest = 0x09;
kbd->cr->wValue = cpu_to_le16(0x200);
kbd->cr->wIndex = cpu_to_le16(interface->desc.bInterfaceNumber);
kbd->cr->wLength = cpu_to_le16(1);
/*设置 控制urb
* 控制参数主要通过键盘上LED来作为参考
*/
usb_fill_control_urb(kbd->led, dev, usb_sndctrlpipe(dev, 0),
(void *) kbd->cr, kbd->leds, 1,
usb_kbd_led, kbd);
kbd->led->transfer_dma = kbd->leds_dma;
kbd->led->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
/*注册input设备,此时usb_kbd_open会被调用*/
error = input_register_device(kbd->dev);
if (error)
goto fail2;
usb_set_intfdata(iface, kbd);
/*"should_wakeup"标志控制设备是否应该尝试启用他的唤醒机制。
device_set_wakeup_enable()会影响该标志。大部分的驱动程序不
会主动修改它们的值。大多数设备的should_wakeup的初始值都被设为false,
也有例外,比如电源键、键盘和由ethtool设置了wake-on-LAN功能的网卡,
所以在这边第二个参数为true*/
device_set_wakeup_enable(&dev->dev, 1);
return 0;
fail2:
usb_kbd_free_mem(dev, kbd);
fail1:
input_free_device(input_dev);
kfree(kbd);
return error;
}
在注册input设备时,会调用usb_kbd_open函数,在该函数里,会提交urb。
后来看了下内核代码里的USB鼠标相关的代码,其probe函数及相关的处理函数也是相类似的。