USB The bus leads to two important linked lists !
One USB The bus leads to two important linked lists , One for USB Device list , One for USB Drive linked list . The device list contains... In various systems USB Devices and all the interfaces to these devices , The drive list contains USB Device driver (usb device driver) and USB The driver (usb driver).

 

USB Device driver (usb device driver) and USB The driver (usb driver) What's the difference ?
USB The device driver contains USB Some common features of the device , Will be associated with all USB Equipment matching . stay USB core Defined :struct usb_device_driver usb_generic_driver.usb_generic_driver yes USB The only device driver object in the subsystem . and USB The driver matches the interface , An interface is a collection of endpoints that perform specific functions .

 

How the device is added to the device list ?
Insert... In the device USB After the controller ,USB core The device will be registered in the system , Add to USB Equipment list up .

 

USB Device driver (usb device driver) How to add to the driver list ?
Register at system startup USB core when ,USB Device drivers are about to be registered , It is also added to the drive list .

 

How the interface is added to the device list ?
stay USB Device drivers and USB After matching the devices ,USB core The device will be configured , After analyzing the structure of the device, all interfaces of the device will be added to the device list . For example, there is an interface in the mouse device ,USB core After configuring the mouse device , This interface will be added to the device list .

 

USB The driver (usb driver) How to add to the driver list ?
At every USB When the driver is registered ,USB The driver will be added to the driver list . Like mouse drivers ,usb_mouse_init The function will pass through usb_register(&usb_mouse_driver) Register the mouse driver to USB core in , Then it is added to the driver list . among usb_mouse_driver Is the structure that describes the mouse driver .

 

Configured state (configured status) After that
When the mouse device 、 Interfaces are added to the device list , And the mouse driver is also added to the driver list , The system goes into a process called configured (configured) The state of . To reach the configured state , Will go through a complex process ,USB core by USB The equipment is dedicated with no regrets . In the process , The system will set up the device of the device 、 To configure 、 Interface 、 Set up 、 Description of the endpoint , They were usb_device、usb_configuration、usb_interface、usb_host_interface、usb_host_endpoint Description of structure .
After the device reaches the configured state , First of all, of course USB The pairing of driver and corresponding interface , For a mouse device, it's a pairing of the mouse driver and the interface in the mouse .USB core Would call usb_device_match function , By comparing the interface information in the device with USB In the driver id_table, To make a preliminary decision on USB Does the driver match the corresponding interface . After passing this barrier ,USB core I think this device should be responsible by this driver .
However , This is not enough , next , Will call USB In the driver probe Function to further check the corresponding interface . If the driver does fit the device interface , Do some initialization work on the device , Distribute urb Ready for data transfer .
When the mouse device is turned on in user space , Will submit probe Functionally constructed urb Request block ,urb It's going to be busy transferring data .urb The request block is like a container “ Bag ”,USB The driver turns “ Empty bags ” Submit to USB core, And then give it to the main controller , The main controller puts the data into this “ Bag ” And then fill it with data “ Bag ” adopt USB core Return to USB The driver , In this way, the data transmission is completed .

 

Here is the fully annotated mouse driver code usbmouse.c

view plaincopy to clipboardprint?

  1. /* 
  2.  * $Id: usbmouse.c,v 1.15 2001/12/27 10:37:41 vojtech Exp $ 
  3.  * 
  4.  *  Copyright (c) 1999-2001 Vojtech Pavlik 
  5.  * 
  6.  *  USB HIDBP Mouse support 
  7.  */  
  8.   
  9. #include <linux/kernel.h>    
  10. #include <linux/slab.h>    
  11. #include <linux/module.h>    
  12. #include <linux/init.h>    
  13. #include <linux/usb/input.h>    
  14. #include <linux/hid.h>    
  15.   
  16. /* 
  17.  * Version Information 
  18.  */  
  19. #define DRIVER_VERSION "v1.6"    
  20. #define DRIVER_AUTHOR "Vojtech Pavlik <[email protected]>"    
  21. #define DRIVER_DESC "USB HID Boot Protocol mouse driver"    
  22. #define DRIVER_LICENSE "GPL"    
  23.   
  24. MODULE_AUTHOR(DRIVER_AUTHOR);  
  25. MODULE_DESCRIPTION(DRIVER_DESC);  
  26. MODULE_LICENSE(DRIVER_LICENSE);  
  27.   
  28. /* 
  29.  *  Mouse structure , Used to describe a mouse device . 
  30.  */  
  31. struct usb_mouse   
  32. {  
  33.     /*  The name of the mouse device , Including manufacturers 、 Product category 、 Product information  */  
  34.     char name[128];   
  35.     /*  Device node name  */  
  36.     char phys[64];    
  37.     /* USB  The mouse is a kind of  USB  equipment , You need to embed a  USB  Device structure to describe its  USB  attribute  */  
  38.     struct usb_device *usbdev;  
  39.     /* USB  The mouse is also an input device , You need to embed an input device structure to describe its input device properties  */  
  40.     struct input_dev *dev;    
  41.     /* URB  Request package structure , Used to transfer data  */  
  42.     struct urb *irq;  
  43.     /*  Address for normal transmission  */  
  44.     signed char *data;  
  45.     /* dma  Address for transmission  */  
  46.     dma_addr_t data_dma;          
  47. };  
  48.   
  49. /* 
  50.  * urb  Callback function , On completion of the submission  urb  after ,urb  The callback function will be called . 
  51.  *  This function acts as  usb_fill_int_urb  The formal parameter of the function , Built for  urb  The callback function specified . 
  52.  */  
  53. static void usb_mouse_irq(struct urb *urb)  
  54. {  
  55.     /* 
  56.      * urb  Medium  context  The pointer is used to  USB  The driver holds some data . For example, the formal parameters of the callback function are not passed in the  probe 
  57.      *  In Chinese, it means  mouse  The address pointer of the memory allocated by the structure , And you need to use the data in that memory area ,context  The pointer helps  
  58.      *  I'm very busy ! 
  59.      *  In filling  urb  When will  context  Pointer to  mouse  Structure data area , Create another part here  mouse  The pointer points to  probe 
  60.      *  In the function is  mouse  The memory you applied for , That memory holds very important data . 
  61.      *  When  urb  adopt  USB core  Submit to  hc  after , If the result is OK ,mouse->data  The memory area you point to will hold the mouse buttons  
  62.      *  And move coordinate information , The system relies on this information to react to the behavior of the mouse .  
  63.      * mouse  Embedded in  dev  The pointer , Point to  input_dev  The memory area that belongs to . 
  64.      */  
  65.     struct usb_mouse *mouse = urb->context;  
  66.     signed char *data = mouse->data;  
  67.     struct input_dev *dev = mouse->dev;  
  68.     int status;  
  69.   
  70.     /* 
  71.      * status  The value is  0  Express  urb  Successfully returns , Jump out of the loop and report mouse events to the input subsystem . 
  72.      * ECONNRESET  The error message indicates  urb  By  usb_unlink_urb  Function gives  unlink  了 ,ENOENT  The error message indicates  urb  By   
  73.      * usb_kill_urb  Function gives  kill  了 .usb_kill_urb  It's the end of it  urb  Life cycle of , and  usb_unlink_urb  be  
  74.      *  It's to stop  urb, This function is not equal to  urb  Complete termination is returned to the callback function . This is when the interrupt handler is running or waiting for a spin lock  
  75.      *  It's very useful , In both cases, you can't sleep , And waiting for a  urb  A complete stop is likely to lead to sleep . 
  76.      * ESHUTDOWN  This error means  USB  A serious error has occurred in the master controller driver , Or submit it  urb  The moment the device was pulled out . 
  77.      *  In addition to the above three kinds of mistakes encountered in the error , Retransmit the application  urb. 
  78.      */  
  79.     switch (urb->status)  
  80.     {  
  81.     case 0:     /* success */  
  82.         break;  
  83.     case -ECONNRESET:   /* unlink */  
  84.     case -ENOENT:  
  85.     case -ESHUTDOWN:  
  86.         return;  
  87.     /* -EPIPE:  should clear the halt */  
  88.     default:        /* error */  
  89.         goto resubmit;  
  90.     }  
  91.   
  92.     /* 
  93.      *  Report mouse events to the input subsystem , In order to respond . 
  94.      * data  The first of an array of 0 Bytes :bit 0、1、2、3、4 Left respectively 、 Right 、 in 、SIDE、EXTRA Key press ; 
  95.      * data  The first of an array of 1 Bytes : Represents the horizontal displacement of the mouse ; 
  96.      * data  The first of an array of 2 Bytes : Represents the vertical displacement of the mouse ; 
  97.      * data  The first of an array of 3 Bytes :REL_WHEEL Displacement . 
  98.      */  
  99.     input_report_key(dev, BTN_LEFT,   data[0] & 0x01);  
  100.     input_report_key(dev, BTN_RIGHT,  data[0] & 0x02);  
  101.     input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);  
  102.     input_report_key(dev, BTN_SIDE,   data[0] & 0x08);  
  103.     input_report_key(dev, BTN_EXTRA,  data[0] & 0x10);  
  104.     input_report_rel(dev, REL_X,     data[1]);  
  105.     input_report_rel(dev, REL_Y,     data[2]);  
  106.     input_report_rel(dev, REL_WHEEL, data[3]);  
  107.   
  108.     /* 
  109.      *  This is for event synchronization . The above lines are a complete mouse event , Including key information 、 Absolute coordinate information and wheel information , Input sub  
  110.      *  It is through this synchronization signal that the system distinguishes each complete event report among multiple complete event reports . It is shown as follows : 
  111.      *  Key information   Coordinate displacement information   Scroll wheel information  EV_SYC |  Key information   Coordinate displacement information   Scroll wheel information  EV_SYC ... 
  112.      */  
  113.     input_sync(dev);  
  114.   
  115.     /* 
  116.      *  The system needs to get the mouse event information periodically and continuously , So in  urb  The end of the callback function is submitted again  urb  Request block , And then again  
  117.      *  Call the new callback function , Go round and begin again . 
  118.      *  Submit... In the callback function  urb  It must be  GFP_ATOMIC  Priority , because  urb  The callback function runs in the interrupt context , I'm talking about  
  119.      *  hand over  urb  You may need to apply for memory in the process 、 Hold the semaphore , These operations may lead to  USB core  sleep , Everything that leads to sleep  
  120.      *  It's not allowed to do anything . 
  121.      */  
  122. resubmit:  
  123.     status = usb_submit_urb (urb, GFP_ATOMIC);  
  124.     if (status)  
  125.         err ("can't resubmit intr, %s-%s/input0, status %d",  
  126.                 mouse->usbdev->bus->bus_name,  
  127.                 mouse->usbdev->devpath, status);  
  128. }  
  129.   
  130. /* 
  131.  *  When you turn on the mouse device , Start submitting in  probe  Function  urb, Get into  urb  cycle . 
  132.  */  
  133. static int usb_mouse_open(struct input_dev *dev)  
  134. {  
  135.     struct usb_mouse *mouse = dev->private;  
  136.   
  137.     mouse->irq->dev = mouse->usbdev;  
  138.     if (usb_submit_urb(mouse->irq, GFP_KERNEL))  
  139.         return -EIO;  
  140.   
  141.     return 0;  
  142. }  
  143.   
  144. /* 
  145.  *  When you turn off the mouse device , end  urb  Life cycle . 
  146.  */  
  147. static void usb_mouse_close(struct input_dev *dev)  
  148. {  
  149.     struct usb_mouse *mouse = dev->private;  
  150.   
  151.     usb_kill_urb(mouse->irq);  
  152. }  
  153.   
  154. /* 
  155.  *  The probe function of the driver  
  156.  */  
  157. static int usb_mouse_probe(struct usb_interface *intf, const struct usb_device_id *id)  
  158. {  
  159.     /*  
  160.      *  The interface structure is contained in the device structure ,interface_to_usbdev  It gets its device structure through the interface structure . 
  161.      * usb_host_interface  Is a structure used to describe interface settings , Embedded in the interface structure  usb_interface  in . 
  162.      * usb_endpoint_descriptor  Is the endpoint descriptor structure , Embedded in the end structure  usb_host_endpoint  in , And the endpoint  
  163.      *  The structure is embedded in the interface setting structure . 
  164.      */  
  165.     struct usb_device *dev = interface_to_usbdev(intf);  
  166.     struct usb_host_interface *interface;  
  167.     struct usb_endpoint_descriptor *endpoint;  
  168.     struct usb_mouse *mouse;  
  169.     struct input_dev *input_dev;  
  170.     int pipe, maxp;  
  171.   
  172.     interface = intf->cur_altsetting;  
  173.   
  174.     /*  There is only one mouse  interrupt  Type of  in  Endpoint , All devices that do not meet this requirement report errors  */  
  175.     if (interface->desc.bNumEndpoints != 1)  
  176.         return -ENODEV;  
  177.   
  178.     endpoint = &interface->endpoint[0].desc;  
  179.     if (!usb_endpoint_is_int_in(endpoint))  
  180.         return -ENODEV;  
  181.   
  182.     /* 
  183.      *  Returns the largest packet that the corresponding endpoint can transmit , The maximum packet returned by the mouse is 4 Bytes , The details of the packet are in  urb 
  184.      *  The callback function is described in detail . 
  185.      */  
  186.     pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);  
  187.     maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));  
  188.   
  189.     /*  by  mouse  The device structure allocates memory  */  
  190.     mouse = kzalloc(sizeof(struct usb_mouse), GFP_KERNEL);  
  191.     /* input_dev */  
  192.     input_dev = input_allocate_device();  
  193.     if (!mouse || !input_dev)  
  194.         goto fail1;  
  195.   
  196.     /* 
  197.      *  Apply for memory space for data transfer ,data  To point to the address of the space ,data_dma  It's about this memory space  dma  mapping , 
  198.      *  This memory space corresponds to  dma  Address . In the use of  dma  In case of transmission , Then use  data_dma  Point to the  dma  Area , 
  199.      *  Otherwise use  data  Point to the normal memory area for transmission . 
  200.      * GFP_ATOMIC  It means not to wait ,GFP_KERNEL  It's a normal priority , You can sleep and wait , Because the mouse uses interrupt transmission mode , 
  201.      *  Sleep is not allowed ,data  It is also the storage area that periodically obtains mouse events , Therefore use  GFP_ATOMIC  priority , If not  
  202.      *  Allocated to memory and returned immediately  0. 
  203.      */  
  204.     mouse->data = usb_buffer_alloc(dev, 8, GFP_ATOMIC, &mouse->data_dma);  
  205.     if (!mouse->data)  
  206.         goto fail1;  
  207.   
  208.     /* 
  209.      *  by  urb  Structure requests memory space , The first parameter represents the number of packets to be transmitted for isochronous transmission , Other modes of transmission are 0. 
  210.      *  The memory application will be made through the following  usb_fill_int_urb  Function to fill in .  
  211.      */  
  212.     mouse->irq = usb_alloc_urb(0, GFP_KERNEL);  
  213.     if (!mouse->irq)  
  214.         goto fail2;  
  215.   
  216.     /*  fill  usb  Device structure and input device structure  */  
  217.     mouse->usbdev = dev;  
  218.     mouse->dev = input_dev;  
  219.   
  220.     /*  Get the name of the mouse device  */  
  221.     if (dev->manufacturer)  
  222.         strlcpy(mouse->name, dev->manufacturer, sizeof(mouse->name));  
  223.   
  224.     if (dev->product)   
  225.     {  
  226.         if (dev->manufacturer)  
  227.             strlcat(mouse->name, " ", sizeof(mouse->name));  
  228.         strlcat(mouse->name, dev->product, sizeof(mouse->name));  
  229.     }  
  230.   
  231.     if (!strlen(mouse->name))  
  232.         snprintf(mouse->name, sizeof(mouse->name),  
  233.              "USB HIDBP Mouse %04x:%04x",  
  234.              le16_to_cpu(dev->descriptor.idVendor),  
  235.              le16_to_cpu(dev->descriptor.idProduct));  
  236.   
  237.     /* 
  238.      *  Fill in the node name in the mouse device structure .usb_make_path  Used to obtain  USB  Equipment in  Sysfs  In the path , Format  
  239.      *  by :usb-usb  Bus number - Pathname . 
  240.      */  
  241.     usb_make_path(dev, mouse->phys, sizeof(mouse->phys));  
  242.     strlcat(mouse->phys, "/input0", sizeof(mouse->phys));  
  243.   
  244.     /*  Assign the name of the mouse device to the embedded input subsystem structure of the mouse device  */  
  245.     input_dev->name = mouse->name;  
  246.     /*  Assign the device node name of the mouse device to the embedded input subsystem structure of the mouse device  */  
  247.     input_dev->phys = mouse->phys;  
  248.     /* 
  249.      * input_dev  Medium  input_id  Structure , Used to store 、 Equipment type and equipment number , This function converts the device descriptor  
  250.      *  The number in is assigned to the embedded input subsystem structure  
  251.      */  
  252.     usb_to_input_id(dev, &input_dev->id);  
  253.     /* cdev  Is the category of the device (class device) */  
  254.     input_dev->cdev.dev = &intf->dev;  
  255.   
  256.     /* evbit  Used to describe events ,EV_KEY  It's a keystroke event ,EV_REL  It's a relative coordinate event  */  
  257.     input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);  
  258.     /* keybit  Represents a key value , Including the left button 、 Right and middle  */  
  259.     input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT) | BIT(BTN_MIDDLE);  
  260.     /* relbit  Used to represent relative coordinate values  */  
  261.     input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y);  
  262.     /*  Some mice have other buttons  */  
  263.     input_dev->keybit[LONG(BTN_MOUSE)] |= BIT(BTN_SIDE) | BIT(BTN_EXTRA);  
  264.     /*  The scroll value of the middle key wheel  */  
  265.     input_dev->relbit[0] |= BIT(REL_WHEEL);  
  266.   
  267.     /* input_dev  Of  private  Data items are used to represent the type of current input device , Here we assign the mouse structure object to it  */  
  268.     input_dev->private = mouse;  
  269.     /*  Fill in input device, open function pointer  */  
  270.     input_dev->open = usb_mouse_open;  
  271.     /*  Fill in input device close function pointer  */  
  272.     input_dev->close = usb_mouse_close;  
  273.   
  274.     /* 
  275.      *  Fill in the build  urb, Just fill in the  mouse  Data filling of structure  urb  In the structure , stay  open  Submitted in  urb. 
  276.      *  When  urb  Contains a  DMA  Buffer should be set  URB_NO_TRANSFER_DMA_MAP.USB Core use  
  277.      * transfer_dma The buffer that the variable points to , instead of transfer_buffer The variable points to . 
  278.      * URB_NO_SETUP_DMA_MAP  be used for  Setup  package ,URB_NO_TRANSFER_DMA_MAP  For all  Data  package . 
  279.      */  
  280.     usb_fill_int_urb(mouse->irq, dev, pipe, mouse->data,  
  281.              (maxp > 8 ? 8 : maxp),  
  282.              usb_mouse_irq, mouse, endpoint->bInterval);  
  283.     mouse->irq->transfer_dma = mouse->data_dma;  
  284.     mouse->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;  
  285.   
  286.     /*  Register the input device with the system  */  
  287.     input_register_device(mouse->dev);  
  288.   
  289.     /* 
  290.      *  Generally in  probe  Function , You need to save device related information in a  usb_interface  In the structure , In order to pass  
  291.      * usb_get_intfdata  Get use . Here, the mouse device structure information will be saved in  intf  The interface structure is embedded in the device structure  
  292.      *  Of  driver_data  Member data , namely  intf->dev->dirver_data = mouse. 
  293.      */  
  294.     usb_set_intfdata(intf, mouse);  
  295.     return 0;  
  296.   
  297. fail2:  usb_buffer_free(dev, 8, mouse->data, mouse->data_dma);  
  298. fail1:  input_free_device(input_dev);  
  299.     kfree(mouse);  
  300.     return -ENOMEM;  
  301. }  
  302.   
  303. /* 
  304.  *  Mouse device pull out processing function  
  305.  */  
  306. static void usb_mouse_disconnect(struct usb_interface *intf)  
  307. {  
  308.     /*  Get the mouse device structure  */  
  309.     struct usb_mouse *mouse = usb_get_intfdata (intf);  
  310.   
  311.     /* intf->dev->dirver_data = NULL, Leave the mouse device pointer in the interface structure empty .*/  
  312.     usb_set_intfdata(intf, NULL);  
  313.     if (mouse)  
  314.     {  
  315.         /*  end  urb  Life cycle  */  
  316.         usb_kill_urb(mouse->irq);  
  317.         /*  Log the mouse device out of the input subsystem  */  
  318.         input_unregister_device(mouse->dev);  
  319.         /*  Release  urb  Storage space  */  
  320.         usb_free_urb(mouse->irq);  
  321.         /*  Release the  data  Storage space  */  
  322.         usb_buffer_free(interface_to_usbdev(intf), 8, mouse->data, mouse->data_dma);  
  323.         /*  Release the storage space for the mouse structure  */  
  324.         kfree(mouse);  
  325.     }  
  326. }  
  327.   
  328. /* 
  329.  * usb_device_id  Structure is used to represent the device supported by the driver ,USB_INTERFACE_INFO  Can be used to match specific types of interfaces , 
  330.  *  The argument to this macro means  ( Category ,  Subcategory ,  agreement ). 
  331.  * USB_INTERFACE_CLASS_HID  It is a kind of  HID (Human Interface Device), That is, the category of human-computer interaction equipment ; 
  332.  * USB_INTERFACE_SUBCLASS_BOOT  It's a subcategory , It is a kind of  boot  Used in stages  HID; 
  333.  * USB_INTERFACE_PROTOCOL_MOUSE  It means a mouse device , Follow the protocol of the mouse . 
  334.  */  
  335. static struct usb_device_id usb_mouse_id_table [] = {  
  336.     { USB_INTERFACE_INFO(USB_INTERFACE_CLASS_HID, USB_INTERFACE_SUBCLASS_BOOT,  
  337.         USB_INTERFACE_PROTOCOL_MOUSE) },  
  338.     { } /* Terminating entry */  
  339. };  
  340.   
  341. /* 
  342.  *  This macro is used to let the program running in user space know the device that the driver can support , about  USB  For drivers , The first argument must be  
  343.  *  yes  usb. 
  344.  */  
  345. MODULE_DEVICE_TABLE (usb, usb_mouse_id_table);  
  346.   
  347. /* 
  348.  *  Mouse driver structure  
  349.  */  
  350. static struct usb_driver usb_mouse_driver = {  
  351.     .name       = "usbmouse",  
  352.     .probe      = usb_mouse_probe,  
  353.     .disconnect = usb_mouse_disconnect,  
  354.     .id_table   = usb_mouse_id_table,  
  355. };  
  356.   
  357. /* 
  358.  *  The beginning of the driver life cycle , towards  USB core  Register this mouse driver . 
  359.  */  
  360. static int __init usb_mouse_init(void)  
  361. {  
  362.     int retval = usb_register(&usb_mouse_driver);  
  363.     if (retval == 0)  
  364.         info(DRIVER_VERSION ":" DRIVER_DESC);  
  365.     return retval;  
  366. }  
  367.   
  368. /* 
  369.  *  The end of the driver life cycle , towards  USB core  Log off this mouse driver . 
  370.  */  
  371. static void __exit usb_mouse_exit(void)  
  372. {  
  373.     usb_deregister(&usb_mouse_driver);  
  374. }  
  375.   
  376. module_init(usb_mouse_init);  
  377. module_exit(usb_mouse_exit);