键盘是一种输入型的设备,基于USB接口,内核提供了USB键盘的驱动,定义在drivers/hid/usbhid/usbkhd.c
一、USB键盘驱动的结构
定义了USB键盘的usb_driver的实例
-
static struct usb_driver usb_kbd_driver = {
-
.name = "usbkbd",//驱动名
-
.probe = usb_kbd_probe,//探测方法
-
.disconnect = usb_kbd_disconnect,//断开连接方法
-
.id_table = usb_kbd_id_table,//支持的设备ID
-
};
驱动支持的设备ID列表,定义了驱动支持的设备ID
-
static struct usb_device_id usb_kbd_id_table [] = {
-
{ USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,
-
USB_INTERFACE_PROTOCOL_KEYBOARD) },
-
{ } /* Terminating entry */
-
};
-
MODULE_DEVICE_TABLE (usb, usb_kbd_id_table);
二、驱动的加载和卸载方法
-
//驱动加载方法
-
static int __init usb_kbd_init(void)
-
{
-
//注册USB驱动
-
int result = usb_register(&usb_kbd_driver);
-
if (result == 0)
-
printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
-
DRIVER_DESC "\n");
-
return result;
-
}
-
//驱动卸载方法
-
static void __exit usb_kbd_exit(void)
-
{
-
//注销USB驱动
-
usb_deregister(&usb_kbd_driver);
-
}
三、探测方法
当USB键盘插入USB接口,USB核心通过USB键盘的属性匹配对应的驱动,找到对应的驱动后会调用驱动的探测方法
-
static int usb_kbd_probe(struct usb_interface *iface,
-
const struct usb_device_id *id)
-
{
-
//通过接口得到USB设备结构
-
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;
-
//判断当前接口的端点数是否为1
-
if (interface->desc.bNumEndpoints != 1)
-
return -ENODEV;
-
//获得端点的描述
-
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));
-
-
kbd = kzalloc(sizeof(struct usb_kbd), GFP_KERNEL);
-
//创建input_dev结构
-
input_dev = input_allocate_device();
-
if (!kbd || !input_dev)
-
goto fail1;
-
//分配usb_kbd内存空间
-
if (usb_kbd_alloc_mem(dev, kbd))
-
goto fail2;
-
//为usb_kbd结构赋值
-
kbd->usbdev = dev;
-
kbd->dev = input_dev;
-
//获得制造商
-
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结构赋值
-
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);
-
-
input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_LED) |
-
BIT_MASK(EV_REP);
-
input_dev->ledbit[0] = BIT_MASK(LED_NUML) | BIT_MASK(LED_CAPSL) |
-
BIT_MASK(LED_SCROLLL) | BIT_MASK(LED_COMPOSE) |
-
BIT_MASK(LED_KANA);
-
//设置键编码
-
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;
-
//中断urb初始化
-
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初始化
-
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;
-
//注册输入设备
-
error = input_register_device(kbd->dev);
-
if (error)
-
goto fail2;
-
-
usb_set_intfdata(iface, kbd);
-
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_dev结构并初始化input_dev结构;分配中断URB,中断初始化URB;分配控制URB,
控制初始化URB;注册输入设备
四、断开连接的方法
-
static void usb_kbd_disconnect(struct usb_interface *intf)
-
{
-
struct usb_kbd *kbd = usb_get_intfdata (intf);
-
-
usb_set_intfdata(intf, NULL);
-
if (kbd) {
-
//取消urb传递
-
usb_kill_urb(kbd->irq);
-
//注销input设备
-
input_unregister_device(kbd->dev);
-
//释放USB DMA空间
-
usb_kbd_free_mem(interface_to_usbdev(intf), kbd);
-
kfree(kbd);
-
}
-
}
五、中断URB回调方法
-
static void usb_kbd_irq(struct urb *urb)
-
{
-
struct usb_kbd *kbd = urb->context;
-
int i;
-
//判断URB状态
-
switch (urb->status) {
-
case 0: /* success */
-
break;
-
case -ECONNRESET: /* unlink */
-
case -ENOENT:
-
case -ESHUTDOWN:
-
return;
-
/* -EPIPE: should clear the halt */
-
default: /* error */
-
goto resubmit;
-
}
-
//向输入子系统核心提交事件
-
for (i = 0; i < 8; i++)
-
input_report_key(kbd->dev, usb_kbd_keycode[i + 224], (kbd->new[0] >> i) & 1);
-
-
for (i = 2; i < 8; i++) {
-
-
if (kbd->old[i] > 3 && memscan(kbd->new + 2, kbd->old[i], 6) == kbd->new + 8) {
-
if (usb_kbd_keycode[kbd->old[i]])
-
input_report_key(kbd->dev, usb_kbd_keycode[kbd->old[i]], 0);
-
else
-
hid_info(urb->dev,
-
"Unknown key (scancode %#x) released.\n",
-
kbd->old[i]);
-
}
-
-
if (kbd->new[i] > 3 && memscan(kbd->old + 2, kbd->new[i], 6) == kbd->old + 8) {
-
if (usb_kbd_keycode[kbd->new[i]])
-
input_report_key(kbd->dev, usb_kbd_keycode[kbd->new[i]], 1);
-
else
-
hid_info(urb->dev,
-
"Unknown key (scancode %#x) released.\n",
-
kbd->new[i]);
-
}
-
}
-
//同步事件
-
input_sync(kbd->dev);
-
-
memcpy(kbd->old, kbd->new, 8);
-
-
resubmit:
-
//重新提交事件
-
i = usb_submit_urb (urb, GFP_ATOMIC);
-
if (i)
-
hid_err(urb->dev, "can't resubmit intr, %s-%s/input0, status %d",
-
kbd->usbdev->bus->bus_name,
-
kbd->usbdev->devpath, i);
-
}
六、控制URB回调方法
-
static void usb_kbd_led(struct urb *urb)
-
{
-
struct usb_kbd *kbd = urb->context;
-
//判断URB状态
-
if (urb->status)
-
hid_warn(urb->dev, "led urb status %d received\n",
-
urb->status);
-
-
if (*(kbd->leds) == kbd->newleds)
-
return;
-
-
*(kbd->leds) = kbd->newleds;
-
kbd->led->dev = kbd->usbdev;
-
if (usb_submit_urb(kbd->led, GFP_ATOMIC))
-
hid_err(urb->dev, "usb_submit_urb(leds) failed\n");
-
}
七、输入设备的事件处理方法
-
static int usb_kbd_event(struct input_dev *dev, unsigned int type,
-
unsigned int code, int value)
-
{
-
struct usb_kbd *kbd = input_get_drvdata(dev);
-
-
if (type != EV_LED)
-
return -1;
-
-
kbd->newleds = (!!test_bit(LED_KANA, dev->led) << 3) | (!!test_bit(LED_COMPOSE, dev->led) << 3) |
-
(!!test_bit(LED_SCROLLL, dev->led) << 2) | (!!test_bit(LED_CAPSL, dev->led) << 1) |
-
(!!test_bit(LED_NUML, dev->led));
-
-
if (kbd->led->status == -EINPROGRESS)
-
return 0;
-
-
if (*(kbd->leds) == kbd->newleds)
-
return 0;
-
-
*(kbd->leds) = kbd->newleds;
-
kbd->led->dev = kbd->usbdev;
-
if (usb_submit_urb(kbd->led, GFP_ATOMIC))
-
pr_err("usb_submit_urb(leds) failed\n");
-
-
return 0;
-
}
USB键盘驱动使用了中断URB和控制URB
阅读(1904) | 评论(0) | 转发(0) |