Search This Blog

Wednesday, March 28, 2012

USB DEVICE DRIVER PROGRAM

header:

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


#ifndef PRINTK
#define PRINTK
#endif

const char *DEVICE_NAME ="Abhi_usb";
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Abhishek");
MODULE_DESCRIPTION("USB_DRIVER");

static int pen_probe(struct usb_interface *interface, const struct usb_device_id *id);

static void pen_disconnect(struct usb_interface *interface);

static struct usb_device_id pen_table[] =
{
        {USB_DEVICE(0x0781,0x5567)},
        {}
};
MODULE_DEVICE_TABLE(usb,pen_table);

int usb_register_driver(struct usb_driver *, struct module *,
                               const char *);
void usb_deregister(struct usb_driver *);
static struct usb_driver pen_driver=
{
        .name="Abhishek_usb_driver",
        .id_table = pen_table,
        .probe=pen_probe,
        .disconnect=pen_disconnect
};

PROGRAM:
 #include"header.h"


static int pen_probe(struct usb_interface *interface, const struct usb_device_id *id)
{

        printk(KERN_INFO "Abhishek Driver:Pen drive (%04X:%04X) plugged\n", id->idVendor, id->idProduct);

    return 0;
}

static void pen_disconnect(struct usb_interface *interface)
{
    printk(KERN_INFO "Abhishek Driver:Pen drive removed\n");
}


static int usb_initialisation(void)
{
        int no;
        #ifdef PRINTK
        printk(KERN_INFO"Begin:%s",__func__);
        #endif
        no=usb_register_driver(&pen_driver,THIS_MODULE, DEVICE_NAME);
        #ifdef PRINTK
        printk(KERN_INFO"usb_register:%d",no);
        printk(KERN_INFO"End:%s",__func__);
        #endif
return 0;
}

static void usb_cleanup(void)
{
        #ifdef PRINTK
        printk(KERN_INFO"Begin:%s",__func__);
        #endif
        usb_deregister(&pen_driver);
        #ifdef PRINTK
        printk(KERN_INFO"End:%s",__func__);
        #endif
}

module_init(usb_initialisation);
module_exit(usb_cleanup);