Platform: mini2440
Kernel: Linux 2.6.32.2
When the USB device is plugged in, the kernel reads the device information, and then compares the information in the id_table with the information read to see if it matches and, if so, calls the probe function. The disconnect function is called when the USB device is unplugged. The URB is used in USB device drivers to describe the basic vectors and core data structures that are needed to communicate with USB devices.
URB (USB request block) processing process:
The ①USB device driver creates and initializes a URB that accesses a specific endpoint of a particular USB device and submits it to the USB core.
②usb Core submits this URB to the USB host controller driver.
The ③USB host controller driver accesses the USB device based on the information described by the URB.
④ USB Host Controller driver notifies USB device driver when device access is complete.
USB Mouse Data format:
①bit0-left,1-> Press,0-> to release
②bit1 right button,1-> Press,0-> to release
③bit2, Middle key,1-> Press,0-> release
Driver Code Listing:
USB_MOUSE_INPUT_TEST.C:
#include <linux/kernel.h> #include <linux/slab.h> #include <linux/module.h> #include <linux/ init.h> #include <linux/usb/input.h> #include <linux/hid.h>static struct URB *uk_urb;static char *usb_b uf;static int len;static struct Input_dev *uk_dev;static dma_addr_t usb_buf_phys;static struct usb_device_id US b_mouse_input_test_id_table [] = {{Usb_interface_info (Usb_interface_class_hid, Usb_interface_subclass_boot, Usb_interface_protocol_mouse)}};static void Usb_mouse_input_test_irq (struct urb *urb) {Static unsigned cha R Pre_val;//usb Mouse writes its data to the drive buffer Usb_bufif ((Pre_val & (1<<0))! = (Usb_buf[0] & (1<<0))) {//state change PRINTK ( "Left!\n");} if (Pre_val & (1<<1)! = (Usb_buf[0] & (1<<1))) {//Status change PRINTK ("right!\n");} if (Pre_val & (1<<2)! = (Usb_buf[0] & (1<<2))) {//Status change PRINTK ("Middle!\n");} Pre_val = Usb_buf[0];usb_submit_urb (Uk_urb, Gfp_kernel);} static int Usb_mouse_Input_test_probe (struct usb_interface *intf, const struct usb_device_id *id) {struct Usb_device *dev = interface_to_ Usbdev (intf);//Get USB device structure body struct usb_host_interface *interface;struct usb_endpoint_descriptor *endpoint; int pipe;interface = intf->cur_altsetting; Gets the USB interface structure in the body of the USB host interface Structure endpoint = &interface->endpoint[0].desc;//Gets the end of the USB host interface in the struct body description struct Uk_dev = Input_ Allocate_device (); Set_bit (Ev_key, uk_dev->evbit);//Set Set_bit (Ev_rep, uk_dev->evbit); Set_bit (KEY_L, uk_dev- >keybit); Set_bit (key_s, uk_dev->keybit); Set_bit (Key_enter, uk_dev->keybit); Input_register_device (Uk_dev );//Register pipe = usb_rcvintpipe (dev, endpoint->bendpointaddress); len = Endpoint->wmaxpacketsize;usb_buf = usb_ Buffer_alloc (Dev, Len, gfp_atomic, &usb_buf_phys); uk_urb = usb_alloc_urb (0, Gfp_kernel);//assign USB request Blockusb_ Fill_int_urb (uk_urb, Dev, pipe, usb_buf, Len, Usb_mouse_input_test_irq, NULL, Endpoint->binterval);uk_urb-> TRANSFER_DMA = Usb_buf_phys; Source, purpose, length, set URBUk_urb->transfer_flags |= urb_no_transfer_dma_map;usb_submit_urb (uk_urb, Gfp_kernel);//submit URB to USB host controller driver return 0 ;} static void Usb_mouse_input_test_disconnect (struct usb_interface *intf) {struct Usb_device *dev = Interface_to_usbdev ( intf);p rintk ("Disconnect mouse!\n"); Usb_kill_urb (uk_urb); Usb_free_urb (uk_urb); Usb_buffer_free (Dev, Len, Usb_buf, Usb_buf_phys); Input_unregister_device (Uk_dev); Input_free_device (Uk_dev);} static struct Usb_driver Usb_mouse_input_test_driver = {. Name= "Usb_mouse_input_test_",. probe= usb_mouse_input_test_ probe,.disconnect= usb_mouse_input_test_disconnect,.id_table= usb_mouse_input_test_id_table,};static int Usb_mouse _input_test_init (void) {usb_register (&usb_mouse_input_test_driver); return 0;} static void Usb_mouse_input_test_exit (void) {usb_deregister (&usb_mouse_input_test_driver);} Module_license ("GPL"); Module_init (Usb_mouse_input_test_init); Module_exit (Usb_mouse_input_test_exit);
Makefile:
obj-m+= Usb_mouse_input_test.okern_dir =/home/***/linux-2.6.32.2all:make-c $ (kern_dir) M= ' pwd ' modules clean:make-c $ (Kern_dir) m= ' pwd ' modules Cleanrm-rf Modules.order
Remove the mouse function from the Linux kernel before testing:
Insmod Usb_mouse_input_test.ko
Inserting a USB mouse
Click the mouse three buttons
Terminal Visible Print Information:
Left!
Right!
Middle!