以下是列印滑鼠左右鍵及其他輸入的源代碼,詳細說明見https://www.cnblogs.com/zhu-g5may/p/9309381.html ...
以下是列印滑鼠左右鍵及其他輸入的源代碼,詳細說明見https://www.cnblogs.com/zhu-g5may/p/9309381.html
1 /*參考/drivers/hid/usbhid/usbmouse.c*/ 2 3 #include <linux/kernel.h> 4 #include <linux/slab.h> 5 #include <linux/module.h> 6 #include <linux/init.h> 7 #include <linux/usb/input.h> 8 #include <linux/hid.h> 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("[email protected]");