以下是打印鼠标左右键及其他输入的源代码,详细说明见
1 /*参考/drivers/hid/usbhid/usbmouse.c*/ 2 3 #include4 #include 5 #include 6 #include 7 #include 8 #include 9 10 static struct urb *mouse_urb;11 static int len;12 static char *buf;13 static dma_addr_t mouse_dma;14 15 static const struct usb_device_id mouse_table[] = {16 { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,17 USB_INTERFACE_PROTOCOL_MOUSE) }, /* mouse */18 { }/* Terminating entry */19 };20 21 MODULE_DEVICE_TABLE (usb, mouse_table);22 23 static void usb_mouse_irq(struct urb *urb)24 {25 static int cnt = 0;26 int i;27 printk("data %d : \n",++cnt);28 for(i = 0; i < len; i++){29 printk("0x%02x\n",buf[i]);30 }31 usb_submit_urb(mouse_urb, GFP_KERNEL);32 }33 34 static int mouse_probe(struct usb_interface *intf,35 const struct usb_device_id *id)36 {37 int pipe;38 struct usb_device *mdev = interface_to_usbdev(intf); //设备39 struct usb_endpoint_descriptor *endpoint;40 struct usb_host_interface *interface;41 42 interface = intf->cur_altsetting; //接口43 if (interface->desc.bNumEndpoints != 1)44 return -ENODEV;45 46 endpoint = &interface->endpoint[0].desc; //端点47 if (!usb_endpoint_is_int_in(endpoint))48 return -ENODEV;49 50 len = endpoint->wMaxPacketSize; //长度(多少字节)51 52 buf = usb_alloc_coherent(mdev, len, GFP_ATOMIC, &mouse_dma); //传输buffer,目的 53 54 pipe=usb_rcvintpipe(mdev,endpoint->bEndpointAddress); //源55 56 mouse_urb = usb_alloc_urb(0, GFP_KERNEL); 57 58 usb_fill_int_urb(mouse_urb,mdev,pipe,buf,len,usb_mouse_irq, NULL, endpoint->bInterval); //初始化中断型urb结构体59 mouse_urb->transfer_dma = mouse_dma;60 mouse_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;61 62 usb_submit_urb(mouse_urb, GFP_KERNEL);63 64 return 0;65 }66 67 static void mouse_disconnect(struct usb_interface *intf)68 {69 struct usb_device *mdev = interface_to_usbdev(intf); //设备70 usb_kill_urb(mouse_urb);71 usb_free_urb(mouse_urb);72 usb_free_coherent(mdev, len, buf, mouse_dma);73 }74 75 static struct usb_driver usb_driver = {76 .name = "mouse",77 .probe = mouse_probe,78 .disconnect = mouse_disconnect,79 .id_table = mouse_table,80 };81 82 static int usb_drv_init(void)83 {84 usb_register(&usb_driver);85 return 0;86 }87 88 static void usb_drv_exit(void)89 {90 usb_deregister(&usb_driver);91 }92 93 module_init(usb_drv_init);94 module_exit(usb_drv_exit);95 //module_usb_driver(usb_driver);96 97 MODULE_LICENSE("GPL");98 MODULE_AUTHOR("1653699780@qq.com");