USB device driver under Linux

Source: Internet
Author: User

This paper explains USB device driver with USB mouse.

When calling the Usb_register (struct Usb_driver *) function in the driver, first determine if the USB bus driver supports id_table defined in the Usb_driver, and if so, Usb_driver probe function is called.

So the first thing to do is to define and initialize the USB_DRIVER structure:

Static struct usb_driver usbmouse_as_key_driver = {     "usbmouse_as_key" ,      = Usbmouse_as_key_probe,     = Usbmouse_as_key_disconnect,     = usbmouse_as_key_id_ Table,};
Construction Id_table:
Static struct usb_device_id usbmouse_as_key_id_table[] = {     {usb_interface_info (Usb_interface_class_hid, Usb_interface_subclass, Usb_interface_protocol_mouse)},     {},};
Constructs the probe and disconnect functions: the USB transport needs to use the URB, the definition urb:
Static structURB *uk_urb;Static intUsbmouse_as_key_probe (structUsb_interface *intf,Const structUSB_DEVICE_ID *ID) {     structUsb_device *dev = Interface_to_usbdev (intf);/*device acquired by interface*/     structUsb_host_interface *Interface;/*Setting Descriptor*/     structUsb_endpoint_descripter *endpoint;/*Endpoint Descriptor*/     intpipe; Interface= intf->cur_alterseting;/*Get Set Descriptor*/Endpoint= &Interface->endpoint[0].desc;/*get the endpoint descriptor*/          /*Assigning Settings Input_dev*/Uk_dev=Input_allocate_device (); Set_bit (Ev_key, Uk_dev-evbit); 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); /*Structuring data transfer 3 features*/     /*Source*/Pipe= Usb_rcvintpipe (dev, endpoint->bendpointaddress);//generates an interrupt pipeline and assigns the resulting pipe to the URB     /*length*/Len= endpoint->wmaxpacketsize; /*Purpose*/Uk_buf= Usb_buffer_alloc (0, Gfp_kernel); /*URB fill interrupts with three-factor settings Urb*/usb_fill_int_urb (uk_urb, Dev, pipe, usb_buf, Len, USBMOUSE_AS_KEY_IRQ, NULL, endpoint-binterval); Uk_urb-&GT;TRANSER_DMA =Usb_buf_phys; Uk_urb->transfer_flags |=Urb_no_transfer_dma_rep; Usb_submit_urb (Uk_urb, gfp_kernel);/*submit the URB to the USB core*/     return 0;}Static voidUsbmouse_as_key_disconnect (structUsb_interface *intf) {     structUsb_device *dev =Interface_to_usbdev (intf); //printk ("Disconnect usbmouse!\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 voidUSBMOUSE_AS_KEY_IRQ (structURB *URB) {     StaticUnsignedCharPre_val; /*USB mouse Data meaning * Data[0]: bit0-left button, 1-Press, 0-release * bit1-right, 1-Press, 0-release * bit2-Middle Key, 1-Press, 0-release *     */     if((Pre_val & (1<<0)! = (usb_buf[0] & (1<<0))) {          /*the left button has changed.*/input_event (Uk_dev, Ev_key, key_l, (usb_buf[0] & (1<<0)) ?1:0);     Input_sync (Uk_dev); }     if((Pre_val & (1<<1)! = (usb_buf[0] & (1<<1))) {          /*the right button has changed*/input_event (Uk_dev, Ev_key, key_s, (usb_buf[0] & (1<<1)) ?1:0);     Input_sync (Uk_dev); }     if((Pre_val & (1<<2)! = (usb_buf[0] & (1<<2))) {          /*the middle key has changed*/input_event (Uk_dev, Ev_key, Key_enter, (usb_buf[0] & (1<<2)) ?1:0);     Input_sync (Uk_dev); } pre_val= usb_buf[0]; Usb_submit_urb (Uk_urb, gfp_kernel);/*Resubmit Urb*/}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.