Posted by Talha Suleman : Talha Suleman Tuesday 9 September 2014

This is my first linux USB driver.Its probe function called whenever i plugged in my pen driver (if usb_storage.ko is unloaded).it will work for other USB pen drive's ,if its vendor and product id resides in  usb_device_id table.

Makefile



 obj-m := myusb_driver.o  
 default:  
      make -C /lib/modules/`uname -r`/build M=`pwd` modules  
 clean:  
      make -C /lib/modules/`uname -r`/build M=`pwd` clean  


myusb_driver.c


/************Headers************/
#include < linux/kernel.h >
#include < linux/init.h >
#include < linux/module.h >
#include < linux/usb.h >


 /*****************************************************************  
 probe_function called on device insertion if and only if no other driver exist for that device ,for USB pen drive you have to remove 'usb_storage' driver then insert this driver.  
 *****************************************************************/  
 static int pen_probe(struct usb_interface *interface,const struct usb_device_id *id)  
 {  
 printk(KERN_INFO"Your pen drive(%4X:%4x)plugged\n",id->idVendor,id->idProduct);  
 printk(KERN_INFO"Your Probe function called\n");  
 return 0;  
 }  
 //disconnect  
 static void pen_disconnect(struct usb_interface *interface) {  
 printk(KERN_INFO"Your pen Drive removed\n");  
 }  
 //usb_device_id  
 static struct usb_device_id pen_table[]={  
      //054c:02a5 this id belongs to my   
      //sony pend drive  
      { USB_DEVICE(0x054c,0x02a5)}, //usb id can obtain using "lsusb"  
      //{USB_DEVICE(v,p)},   
 {}       
 };  
 //usb_driver structure  
 static struct usb_driver mypen_driver =   
 {  
      .name="naveen-USB Driver",  
      .id_table=pen_table, //usb_device_id  
      .probe=pen_probe,     //probe function  
      .disconnect=pen_disconnect, //disconnect function  
 };  
 //init part  
 static int __init pen_init(void) {  
 int ret = -1;  
 printk(KERN_INFO "Init function of driver");  
 printk(KERN_INFO "\tRegistering Driver with kernel");  
 ret=usb_register(&mypen_driver);  
 printk(KERN_INFO "Registration is complete");  
 return ret;  
 }  
 static void __exit pen_exit(void) {  
 //deregister  
 printk(KERN_INFO "rewmovel  of driver");  
 usb_deregister(&mypen_driver);  
 printk(KERN_INFO "\tUnregisteration complete!");  
 }  
 module_init(pen_init);  
 module_exit(pen_exit);  
 MODULE_LICENSE("GPL");  
 MODULE_AUTHOR("Naveen Kumar");  
 MODULE_DESCRIPTION("USB Pen Registration Driver");  

Leave a Reply

Thanks For Visit Tnl Solution

Subscribe to Posts | Subscribe to Comments

Search Tnl Solution

- Copyright © Total Solution - Tnl Solution - Powered by Blogger TnlLinker - Designed by TalhaSuleman -