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