EmbLogic's Blog

MODULE_DEVICE_TABLE Macro

This Macro is used by all USB and PCI drivers. This macro describes which devices each specific driver can support. At compilation time, the build process extracts this information out of the driver and builds a table. The table is called modules.pcimap and modules.usbmap for all PCI and USB devices, respectively, and exists in the directory /lib/modules/kernel_version/.

e.g.:-

static struct usb_device_id puzzle_id_table[]=
{
{       USB_DEVICE(VENDOR_ID,PRODUCT_ID)        },
{0,},

};
MODULE_DEVICE_TABLE(usb,puzzle_id_table);

This will add following lines to modules.usbmap

# usb module         match_flags idVendor idProduct bcdDevice_lo bcdDevice_hi bDeviceClass bDeviceSubClass bDeviceProtocol bInterfaceClass bInterfaceSubClass bInterfaceProtocol driver_info
lkm                  0×0003      0×0951   0×1653    0×0000       0×0000       0×00         0×00            0×00            0×00            0×00               0×00               0×0

The macro MODULE_DEVICE_TABLE automatically creates two variables. For the example: MODULE_DEVICE_TABLE (usb, puzzle_id_table); the variables __module_usb_device_size and __module_usb_device_table are created and placed into the read-only data section and the initialized data section of the module, respectively. The variable __module_usb_device_size contains the value of the size of the struct usb_id structure, and __module_usb_device_table points to the puzzle_id_table structure. The puzzle_id_table variable is an array of usb_id structures with a terminating NULL structure at the end of the list.

When the depmod program is run, as part of the kernel installation process, it goes through every module looking for the symbol __module_usb_device_size to be present in the compiled module. If it finds it, it copies the data pointed to by the __module_usb_device_table symbol into a structure, extracts all of the information and writes it out to the modules.usbmap file, which is located in the module root directory.

One Response to MODULE_DEVICE_TABLE Macro

  1. Dharmendra Nath Shandilya says:

    Respected Sir

    Please explain the issue more elaborately.
    How can we exactly use this macro and where in our usb LKM which have the information of vendor details or it is used by kernel at the time of kernel installation .
    is it is in open function or at time of intialization of device?

    Thanks and regards
    Dharmendra Nath Shandilya

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>