Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

Write own simple driver

Hi all !

To answer all questions about writting a driver in uClinux, I create this topic to collect all solutions. Please share all !

If your device type is:

[uCLinux USB interface]<--------------USB cable ----------->[your device here]

And you want to write a driver for it.

First, There is a struct usb_driver in the kernel

//location: kernel-2.6.x/include/linux/usb.h
struct usb_driver {
    struct module *owner;
    const char *name;
    int (*probe) (struct usb_interface *intf,
        const struct usb_device_id *id);
    void (*disconnect) (struct usb_interface *intf);
    int (*ioctl) (struct usb_interface *intf, unsigned int code, void *buf);
    int (*suspend) (struct usb_interface *intf, u32 state);
    int (*resume) (struct usb_interface *intf);
    const struct usb_device_id *id_table;
    struct device_driver driver;
};

In your driver application, you declare:

static struct usb_driver myMotor_driver = {
        .owner =        THIS_MODULE,
        .name =         "usbmotor",
        .probe =        motor_probe,
        .disconnect =   motor_disconnect,
        .id_table =     id_table,
};
//The id_table variable is defined as:
static struct usb_device_id id_table  = {
        { USB_DEVICE(VENDOR_ID, PRODUCT_ID) },
        { },
};
MODULE_DEVICE_TABLE (usb, id_table);

When the driver module is loaded, this motor_driver structure must be registered with the USB core. This is accomplished with a single call to the usb_register() function:

retval = usb_register(&motor_driver);
if (retval)
        err("usb_register failed. "
            "Error number %d", retval);

Likewise, when the driver is unloaded from the system, it must unregister itself from the USB core:

 usb_deregister(&motor_driver);

All processing is in motor_probe() function ( using usb_control_msg() kernel function ) ...

/*-------------------------------------------------------------------------------------------------------------------------*/

but if your device is standalone ( ie: connected via some pins on the altera board ), how can you do ?

//location: kernel-2.6.x/include/linux/device.h
struct device_driver {
    char      * name;
    struct bus_type  * bus;
    struct semaphore    unload_sem;
    struct kobject  kobj;
    struct list_head    devices;
    struct module      * owner;
    int    (*probe)    (struct device * dev);
    int  (*remove)    (struct device * dev);
    void    (*shutdown)    (struct device * dev);
    int    (*suspend)    (struct device * dev, u32 state, u32 level);
    int    (*resume)    (struct device * dev, u32 level);
};

what steps or what functions to :

- Delcare my device ( Address of device, port ... )

- Register it

- Processing data or control

- Deregister

...

Thank you very much !

OneNet