simple usb driver in linux

#include<linux/module.h>
#include<linux/init.h>
#include<linux/usb.h>


MODULE_LICENSE("GPL");

/* When the device is inserted into the USB bus and the vendor id and product id matches with the driver registered in the core,the probe function is called.The usb_device structure,interface number and interface id are passed to the function */

static int toshiba_probe(struct usb_interface *interface,const struct usb_device_id *id)
{
printk("\n In probe function");
return 0;
}

/* This function is called whenever the device is removed from the USB device */

static void toshiba_disconnect(struct usb_interface *interface)
{
printk("\n In disconnect function");

}
/* struct usb_device_id  structure provides a list of different types of USB devices that the driver supports */

 static const struct usb_device_id usb_table[] = {

{ USB_DEVICE(0x13fe,0x1d00) },
{ }, /*Terminating entry*/
};

/* To enable the linux hotplug system to load the driver automatically when the device is plugged in you need to create a MODULE_DEVICE_TABLE */

MODULE_DEVICE_TABLE(usb,usb_table);





/*  The first thing a linux USB driver needs to do is register itself with the linux subsystem,giving it information which devices the driver support and which function should be called when the device is inserted or removed from the system.All this information is passed with the help of usb_driver struct */

static struct usb_driver toshiba_driver={

.name="toshiba",
.probe=toshiba_probe,
.disconnect=toshiba_disconnect,
.id_table=usb_table,

};

static int usb_init(void)
{

int result;
/* USB driver is registered with the call to usb_register */
result=usb_register(&toshiba_driver);
if(result<0)
{
printk("\n USB registration Failed");
return -1;
}
return 0;

}





static void usb_exit(void)
{
/*USB driver is deregistered with call to usb_deregister */
usb_deregister(&toshiba_driver);


}


module_init(usb_init);
module_exit(usb_exit);

Comments

Popular posts from this blog

bb.utils.contains yocto

Difference between RDEPENDS and DEPENDS in Yocto

PR, PN and PV Variable in Yocto