Program for Linux usb-devices driver Step by step (one)

Source: Internet
Author: User

Program for Linux usb-devices driver


Start to chew Nut ~ Here I intend to step by step to give the USB device driver demo, I hope the heart can be able to exchange learning together. Want to meet more geek interested in Linux.

At present, because of the environment and their own ability to do the physical test, the final plan to give a x86 on the analog USB read-write driver, after the physical test can be done after the update this blog


My Contact information:

[Email protected] (because I can not login qq, so Thunder bird mail as long as the computer on the online ~)



Test environment:

Ubuntu 14.0

kernel:3.13.0


Below I have more detailed comments on the code, the main points are in the code ~


This code is just an example, equivalent to the first drive device Hello World, to the most simplistic generalization of the USB device plug-in, unplug the relevant processing process, the later slowly write deeper, the code is more and more "complex"


I have compiled USB device driver code-related structure, made a directory for easy access


/**************************************************************code writer:eofcode Date:2014.08.18code file:./ Usb_1/usb_1.ce-mail: [Email protected]code Purpose:this is a demo-to-code for-a USB device driver. If there is something wrong with my code, please touch me by e-mail. Thank. /#include <linux/kernel.h> #include <linux/ init.h> #include <linux/usb.h> #include <linux/module.h> #include <linux/moduleparam.h>/*** this Double Macro used to create a structure--* * * ' usb_device_id ' by ' usb_device ' * * #define Vendor_id0x08f7#define product_id0x000 2#define device_name "usb_1" Module_author ("EOF"); Module_license ("Dual BSD/GPL");/*** id_table stored many USB device ' s ' usb_device_id ' structure */static struct Usb_devic e_id id_table[] = {{Usb_device (vendor_id, product_id)},{},};/***the ' module_device_table ' Macro marks DEVICE in the** mo Dule image So, the module can be loaded ON Demand If * * the device is hotplugged.*/module_device_table (usb,id_table);/***the probe () method was invoked by KHUBD aft  ER device enumeration.**** @interface: Contains information gleaned during the * * Enumeration process.**** @id: the entry in The driver ' s ' usb_device_id ' table** that matches the values read from the usb-device.***/static int usb_probe (struct US B_interface * interface, const struct USB_DEVICE_ID *id) {dev_info (&interface->dev, "USB device Now attached\n"); return 0;} /*** Disconnect () method was called when the device was unplugged or** when the module is unloaded.*/static void Usb_disconn ECT (struct usb_interface* interface) {Dev_info (&interface->dev, "USB device Now disconnect\n");} /***each USB Device has a ' usb_driver ' data structure.*/static struct usb_driver usb_driver = {. name=device_name,.probe=us B_probe,.disconnect =usb_disconnect,.id_table =id_table,};static int usb_init (void) {return Usb_register (&usb_ driver);} static void Usb_exit (void) {Usb_deRegister (&usb_driver);} Module_init (Usb_init); Module_exit (Usb_exit);







Hengshan Sunrise







Related Article

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.