When an hardware device is plug-in to a system running Linux the linux kernel try to match the device against its devices table entries .
In order to be included in the device entries table the module should register it self in the following way :
Each device type (USB , PC card etc ) has it own matching structure used to identify the device in the devices table .The declaration of device types can be found here. Popular device type can be i2c , pci , usb , of
examples for device id structure:
struct of_device_id
{
char name[32];
char type[32];
char compatible[128];
const void *data;
};
The structure for usb devices :
struct usb_device_id {
/* which fields to match against? */
__u16 match_flags;
/* Used for product specific matches; range is inclusive */
__u16 idVendor;
__u16 idProduct;
__u16 bcdDevice_lo;
__u16 bcdDevice_hi;
/* Used for device class matches */
__u8 bDeviceClass;
__u8 bDeviceSubClass;
__u8 bDeviceProtocol;
/* Used for interface class matches */
__u8 bInterfaceClass;
__u8 bInterfaceSubClass;
__u8 bInterfaceProtocol;
/* Used for vendor-specific interface matches */
__u8 bInterfaceNumber;
/* not matched against */
kernel_ulong_t driver_info
__attribute__((aligned(sizeof(kernel_ulong_t))));
Example for declaring id for usb device :
Declare the device it entry:
static const struct usb_device_id rio_table[] = {
{
USB_DEVICE(0x0841, 1) }, /* Rio 500 */
{ } /* Terminating entry */
};
Were the USB_DEVICE macro is used to describe a specific USB device based on the vendor id and the product id.
MODULE_DEVICE_TABLE (usb, rio_table);
The MODULE_DEVICE_TABLE macro is used to declare the id table of a device.
References to the MODULE_DEVICE_TABLE can be found here.
#define MODULE_DEVICE_TABLE(type,name)
Declare the usb_driver structure :
static struct usb_driver rio_driver =
{
name ="rio500",
probe = probe_rio,
disconnect = disconnect_rio,
id_table = rio_table,
};
Name is a unique name among other USB drivers usually the module name.
Probe is called in order to check if this module can handle a particular USB interface if it can the probe callback although register the device
Disconnect is called when the device is unplugged or shut down.
Call to register the usb device into the device table
module_usb_driver(rio_driver);
אין תגובות:
הוסף רשומת תגובה