嵌入式Linux下USB驅動程序的設計
else
{ handle_scancode(0xe0,1);
handle_scancode(0x4d,1);
handle_scancode(0xe0,0);
handle_scancode(0x4d,0);
}
}
printk("new=%x %x %x %x %x %x %x %x", kbd->new[0],kbd->new[1],kbd->new[2],kbd->new[3],
kbd->new[4],kbd->new[5],kbd->new[6],kbd->new[7]);
}
static void *usb_kbd_probe(struct usb_device *dev, unsigned int ifnum, const struct usb_device_id *id)
{
struct usb_interface *iface;
struct usb_interface_descriptor *interface;
struct usb_endpoint_descriptor *endpoint;
struct usb_kbd *kbd;
int pipe, maxp;
iface = dev->actconfig->interface[ifnum];
interface = iface->altsetting[iface->act_altsetting];
if ((dev->descriptor.idVendor != USB_HOTKEY_VENDOR_ID) || (dev->descriptor.idProduct != USB_HOTKEY_PRODUCT_ID) || (ifnum != 1))
{
return NULL;
}
if (dev->actconfig->bNumInterfaces != 2)
{
return NULL;
}
if (interface->bNumEndpoints != 1) return NULL;
endpoint = interface->endpoint + 0;
pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
usb_set_protocol(dev, interface->bInterfaceNumber, 0);
usb_set_idle(dev, interface->bInterfaceNumber, 0, 0);
printk(KERN_INFO "GUO: Vid = %.4x, Pid = %.4x, Device = %.2x, ifnum = %.2x, bufCount = %.8x", dev->descriptor.idVendor,dev->descriptor.idProduct,dev->descriptor.bcdDevice, ifnum, maxp);
if (!(kbd = kmalloc(sizeof(struct usb_kbd), GFP_KERNEL))) return NULL;
memset(kbd, 0, sizeof(struct usb_kbd));
kbd->usbdev = dev;
FILL_INT_URB(kbd->irq, dev, pipe, kbd->new, maxp > 8 ? 8 : maxp, usb_kbd_irq,kbd, endpoint->bInterval); kbd->irq.dev = kbd->usbdev;
if (dev->descriptor.iManufacturer) usb_string(dev, dev->descriptor.iManufacturer, kbd->name, 63);
if (usb_submit_urb(kbd->irq)) {
kfree(kbd); return NULL; }
printk(KERN_INFO "input%d: %s on usb%d:%d.%d", kbd->dev.number, kbd->name, dev->bus->busnum, dev->devnum, ifnum);
return kbd; }
static void usb_kbd_disconnect(struct usb_device *dev, void *ptr)
{
struct usb_kbd *kbd = ptr;
usb_unlink_urb(kbd->irq);
kfree(kbd);
}
static struct usb_device_id usb_kbd_id_table [] = {
{ USB_DEVICE(USB_HOTKEY_VENDOR_ID, USB_HOTKEY_PRODUCT_ID) },
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, usb_kbd_id_table);
static struct usb_driver usb_kbd_driver = {
name: "Hotkey",
probe: usb_kbd_probe,
disconnect: usb_kbd_disconnect,
id_table: usb_kbd_id_table,
NULL,
};
static int __init usb_kbd_init(void)
{
usb_register(usb_kbd_driver);
info(DRIVER_VERSION ":" DRIVER_DESC);
return 0;
}
static void __exit usb_kbd_exit(void)
{
usb_deregister(usb_kbd_driver);
}
module_init(usb_kbd_init);
module_exit(usb_kbd_exit);
三、結語
USB規(guī)范是一門比較新的技術,接口使用方便,但是驅動程序的設計較復雜。上面介紹了USB設備驅動程序的設計,主要分析了主機端驅動程序的設計,并且給出了一個編寫USB驅動程序的實例。
linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)
linux操作系統(tǒng)文章專題:linux操作系統(tǒng)詳解(linux不再難懂)linux相關文章:linux教程
評論