1 /* Simple code to turn various tables in an ELF file into alias definitions.
2 * This deals with kernel datastructures where they should be
3 * dealt with: in the kernel source.
5 * Copyright 2002-2003 Rusty Russell, IBM Corporation
6 * 2003 Kai Germaschewski
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
15 /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
16 * use either stdint.h or inttypes.h for the rest. */
17 #if KERNEL_ELFCLASS == ELFCLASS32
18 typedef Elf32_Addr kernel_ulong_t;
19 #define BITS_PER_LONG 32
21 typedef Elf64_Addr kernel_ulong_t;
22 #define BITS_PER_LONG 64
32 typedef uint32_t __u32;
33 typedef uint16_t __u16;
34 typedef unsigned char __u8;
36 /* Big exception to the "don't include kernel headers into userspace, which
37 * even potentially has different endianness and word sizes, since
38 * we handle those differences explicitly below */
39 #include "../../include/linux/mod_devicetable.h"
41 #define ADD(str, sep, cond, field) \
45 sprintf(str + strlen(str), \
46 sizeof(field) == 1 ? "%02X" : \
47 sizeof(field) == 2 ? "%04X" : \
48 sizeof(field) == 4 ? "%08X" : "", \
51 sprintf(str + strlen(str), "*"); \
55 * Check that sizeof(device_id type) are consistent with size of section
56 * in .o file. If in-consistent then userspace and kernel does not agree
57 * on actual size which is a bug.
59 static void device_id_size_check(const char *modname, const char *device_id,
60 unsigned long size, unsigned long id_size)
62 if (size % id_size || size < id_size) {
63 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
64 "of the size of section __mod_%s_device_table=%lu.\n"
65 "Fix definition of struct %s_device_id "
66 "in mod_devicetable.h\n",
67 modname, device_id, id_size, device_id, size, device_id);
71 /* USB is special because the bcdDevice can be matched against a numeric range */
72 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
73 static void do_usb_entry(struct usb_device_id *id,
74 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
75 unsigned char range_lo, unsigned char range_hi,
79 strcpy(alias, "usb:");
80 ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
82 ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
86 if (bcdDevice_initial_digits)
87 sprintf(alias + strlen(alias), "%0*X",
88 bcdDevice_initial_digits, bcdDevice_initial);
89 if (range_lo == range_hi)
90 sprintf(alias + strlen(alias), "%u", range_lo);
91 else if (range_lo > 0 || range_hi < 9)
92 sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
93 if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
96 ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
99 id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
100 id->bDeviceSubClass);
102 id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
103 id->bDeviceProtocol);
105 id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
106 id->bInterfaceClass);
108 id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
109 id->bInterfaceSubClass);
111 id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
112 id->bInterfaceProtocol);
114 /* Always end in a wildcard, for future extension */
115 if (alias[strlen(alias)-1] != '*')
117 buf_printf(&mod->dev_table_buf,
118 "MODULE_ALIAS(\"%s\");\n", alias);
121 static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
123 unsigned int devlo, devhi;
124 unsigned char chi, clo;
127 id->match_flags = TO_NATIVE(id->match_flags);
128 id->idVendor = TO_NATIVE(id->idVendor);
129 id->idProduct = TO_NATIVE(id->idProduct);
131 devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
132 TO_NATIVE(id->bcdDevice_lo) : 0x0U;
133 devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
134 TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
137 * Some modules (visor) have empty slots as placeholder for
138 * run-time specification that results in catch-all alias
140 if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
143 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
144 for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
147 if (chi > 9) /* it's bcd not hex */
152 if (devlo == devhi || !ndigits) {
153 do_usb_entry(id, devlo, ndigits, clo, chi, mod);
158 do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
161 do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
165 static void do_usb_table(void *symval, unsigned long size,
169 const unsigned long id_size = sizeof(struct usb_device_id);
171 device_id_size_check(mod->name, "usb", size, id_size);
173 /* Leave last one: it's the terminator. */
176 for (i = 0; i < size; i += id_size)
177 do_usb_entry_multi(symval + i, mod);
180 /* Looks like: ieee1394:venNmoNspNverN */
181 static int do_ieee1394_entry(const char *filename,
182 struct ieee1394_device_id *id, char *alias)
184 id->match_flags = TO_NATIVE(id->match_flags);
185 id->vendor_id = TO_NATIVE(id->vendor_id);
186 id->model_id = TO_NATIVE(id->model_id);
187 id->specifier_id = TO_NATIVE(id->specifier_id);
188 id->version = TO_NATIVE(id->version);
190 strcpy(alias, "ieee1394:");
191 ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
193 ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
195 ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
197 ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
203 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
204 static int do_pci_entry(const char *filename,
205 struct pci_device_id *id, char *alias)
207 /* Class field can be divided into these three. */
208 unsigned char baseclass, subclass, interface,
209 baseclass_mask, subclass_mask, interface_mask;
211 id->vendor = TO_NATIVE(id->vendor);
212 id->device = TO_NATIVE(id->device);
213 id->subvendor = TO_NATIVE(id->subvendor);
214 id->subdevice = TO_NATIVE(id->subdevice);
215 id->class = TO_NATIVE(id->class);
216 id->class_mask = TO_NATIVE(id->class_mask);
218 strcpy(alias, "pci:");
219 ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
220 ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
221 ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
222 ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
224 baseclass = (id->class) >> 16;
225 baseclass_mask = (id->class_mask) >> 16;
226 subclass = (id->class) >> 8;
227 subclass_mask = (id->class_mask) >> 8;
228 interface = id->class;
229 interface_mask = id->class_mask;
231 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
232 || (subclass_mask != 0 && subclass_mask != 0xFF)
233 || (interface_mask != 0 && interface_mask != 0xFF)) {
234 warn("Can't handle masks in %s:%04X\n",
235 filename, id->class_mask);
239 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
240 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
241 ADD(alias, "i", interface_mask == 0xFF, interface);
245 /* looks like: "ccw:tNmNdtNdmN" */
246 static int do_ccw_entry(const char *filename,
247 struct ccw_device_id *id, char *alias)
249 id->match_flags = TO_NATIVE(id->match_flags);
250 id->cu_type = TO_NATIVE(id->cu_type);
251 id->cu_model = TO_NATIVE(id->cu_model);
252 id->dev_type = TO_NATIVE(id->dev_type);
253 id->dev_model = TO_NATIVE(id->dev_model);
255 strcpy(alias, "ccw:");
256 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
258 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
260 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
262 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
267 /* looks like: "ap:tN" */
268 static int do_ap_entry(const char *filename,
269 struct ap_device_id *id, char *alias)
271 sprintf(alias, "ap:t%02X", id->dev_type);
275 /* Looks like: "serio:tyNprNidNexN" */
276 static int do_serio_entry(const char *filename,
277 struct serio_device_id *id, char *alias)
279 id->type = TO_NATIVE(id->type);
280 id->proto = TO_NATIVE(id->proto);
281 id->id = TO_NATIVE(id->id);
282 id->extra = TO_NATIVE(id->extra);
284 strcpy(alias, "serio:");
285 ADD(alias, "ty", id->type != SERIO_ANY, id->type);
286 ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
287 ADD(alias, "id", id->id != SERIO_ANY, id->id);
288 ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
293 /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
294 static int do_acpi_entry(const char *filename,
295 struct acpi_device_id *id, char *alias)
297 sprintf(alias, "acpi*:%s:", id->id);
301 /* looks like: "pnp:dD" */
302 static int do_pnp_entry(const char *filename,
303 struct pnp_device_id *id, char *alias)
305 sprintf(alias, "pnp:d%s", id->id);
309 /* looks like: "pnp:cCdD..." */
310 static int do_pnp_card_entry(const char *filename,
311 struct pnp_card_device_id *id, char *alias)
315 sprintf(alias, "pnp:c%s", id->id);
316 for (i = 0; i < PNP_MAX_DEVICES; i++) {
317 if (! *id->devs[i].id)
319 sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
324 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
325 static int do_pcmcia_entry(const char *filename,
326 struct pcmcia_device_id *id, char *alias)
330 id->match_flags = TO_NATIVE(id->match_flags);
331 id->manf_id = TO_NATIVE(id->manf_id);
332 id->card_id = TO_NATIVE(id->card_id);
333 id->func_id = TO_NATIVE(id->func_id);
334 id->function = TO_NATIVE(id->function);
335 id->device_no = TO_NATIVE(id->device_no);
337 for (i=0; i<4; i++) {
338 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
341 strcpy(alias, "pcmcia:");
342 ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
344 ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
346 ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
348 ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
350 ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
352 ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
353 ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
354 ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
355 ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
362 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
366 len = sprintf (alias, "of:N%sT%s",
367 of->name[0] ? of->name : "*",
368 of->type[0] ? of->type : "*");
370 if (of->compatible[0])
371 sprintf (&alias[len], "%sC%s",
372 of->type[0] ? "*" : "",
375 /* Replace all whitespace with underscores */
376 for (tmp = alias; tmp && *tmp; tmp++)
383 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
388 sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
389 vio->compat[0] ? vio->compat : "*");
391 /* Replace all whitespace with underscores */
392 for (tmp = alias; tmp && *tmp; tmp++)
399 static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias)
401 strcpy(alias, "i2c:");
402 ADD(alias, "id", 1, i2c->id);
406 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
408 static void do_input(char *alias,
409 kernel_ulong_t *arr, unsigned int min, unsigned int max)
413 for (i = min; i < max; i++)
414 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
415 sprintf(alias + strlen(alias), "%X,*", i);
418 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
419 static int do_input_entry(const char *filename, struct input_device_id *id,
422 sprintf(alias, "input:");
424 ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
425 ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
426 ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
427 ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
429 sprintf(alias + strlen(alias), "-e*");
430 if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
431 do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX);
432 sprintf(alias + strlen(alias), "k*");
433 if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
434 do_input(alias, id->keybit,
435 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
436 INPUT_DEVICE_ID_KEY_MAX);
437 sprintf(alias + strlen(alias), "r*");
438 if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
439 do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX);
440 sprintf(alias + strlen(alias), "a*");
441 if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
442 do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
443 sprintf(alias + strlen(alias), "m*");
444 if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
445 do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
446 sprintf(alias + strlen(alias), "l*");
447 if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
448 do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
449 sprintf(alias + strlen(alias), "s*");
450 if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
451 do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
452 sprintf(alias + strlen(alias), "f*");
453 if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
454 do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
455 sprintf(alias + strlen(alias), "w*");
456 if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
457 do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX);
461 static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
465 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
469 /* Looks like: parisc:tNhvNrevNsvN */
470 static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
473 id->hw_type = TO_NATIVE(id->hw_type);
474 id->hversion = TO_NATIVE(id->hversion);
475 id->hversion_rev = TO_NATIVE(id->hversion_rev);
476 id->sversion = TO_NATIVE(id->sversion);
478 strcpy(alias, "parisc:");
479 ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type);
480 ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion);
481 ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev);
482 ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion);
487 /* Looks like: sdio:cNvNdN. */
488 static int do_sdio_entry(const char *filename,
489 struct sdio_device_id *id, char *alias)
491 id->class = TO_NATIVE(id->class);
492 id->vendor = TO_NATIVE(id->vendor);
493 id->device = TO_NATIVE(id->device);
495 strcpy(alias, "sdio:");
496 ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class);
497 ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor);
498 ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device);
502 /* Looks like: ssb:vNidNrevN. */
503 static int do_ssb_entry(const char *filename,
504 struct ssb_device_id *id, char *alias)
506 id->vendor = TO_NATIVE(id->vendor);
507 id->coreid = TO_NATIVE(id->coreid);
508 id->revision = TO_NATIVE(id->revision);
510 strcpy(alias, "ssb:");
511 ADD(alias, "v", id->vendor != SSB_ANY_VENDOR, id->vendor);
512 ADD(alias, "id", id->coreid != SSB_ANY_ID, id->coreid);
513 ADD(alias, "rev", id->revision != SSB_ANY_REV, id->revision);
517 /* Ignore any prefix, eg. v850 prepends _ */
518 static inline int sym_is(const char *symbol, const char *name)
522 match = strstr(symbol, name);
525 return match[strlen(symbol)] == '\0';
528 static void do_table(void *symval, unsigned long size,
529 unsigned long id_size,
530 const char *device_id,
536 int (*do_entry)(const char *, void *entry, char *alias) = function;
538 device_id_size_check(mod->name, device_id, size, id_size);
539 /* Leave last one: it's the terminator. */
542 for (i = 0; i < size; i += id_size) {
543 if (do_entry(mod->name, symval+i, alias)) {
544 /* Always end in a wildcard, for future extension */
545 if (alias[strlen(alias)-1] != '*')
547 buf_printf(&mod->dev_table_buf,
548 "MODULE_ALIAS(\"%s\");\n", alias);
553 /* Create MODULE_ALIAS() statements.
554 * At this time, we cannot write the actual output C source yet,
555 * so we write into the mod->dev_table_buf buffer. */
556 void handle_moddevtable(struct module *mod, struct elf_info *info,
557 Elf_Sym *sym, const char *symname)
561 /* We're looking for a section relative symbol */
562 if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
565 symval = (void *)info->hdr
566 + info->sechdrs[sym->st_shndx].sh_offset
569 if (sym_is(symname, "__mod_pci_device_table"))
570 do_table(symval, sym->st_size,
571 sizeof(struct pci_device_id), "pci",
573 else if (sym_is(symname, "__mod_usb_device_table"))
574 /* special case to handle bcdDevice ranges */
575 do_usb_table(symval, sym->st_size, mod);
576 else if (sym_is(symname, "__mod_ieee1394_device_table"))
577 do_table(symval, sym->st_size,
578 sizeof(struct ieee1394_device_id), "ieee1394",
579 do_ieee1394_entry, mod);
580 else if (sym_is(symname, "__mod_ccw_device_table"))
581 do_table(symval, sym->st_size,
582 sizeof(struct ccw_device_id), "ccw",
584 else if (sym_is(symname, "__mod_ap_device_table"))
585 do_table(symval, sym->st_size,
586 sizeof(struct ap_device_id), "ap",
588 else if (sym_is(symname, "__mod_serio_device_table"))
589 do_table(symval, sym->st_size,
590 sizeof(struct serio_device_id), "serio",
591 do_serio_entry, mod);
592 else if (sym_is(symname, "__mod_acpi_device_table"))
593 do_table(symval, sym->st_size,
594 sizeof(struct acpi_device_id), "acpi",
596 else if (sym_is(symname, "__mod_pnp_device_table"))
597 do_table(symval, sym->st_size,
598 sizeof(struct pnp_device_id), "pnp",
600 else if (sym_is(symname, "__mod_pnp_card_device_table"))
601 do_table(symval, sym->st_size,
602 sizeof(struct pnp_card_device_id), "pnp_card",
603 do_pnp_card_entry, mod);
604 else if (sym_is(symname, "__mod_pcmcia_device_table"))
605 do_table(symval, sym->st_size,
606 sizeof(struct pcmcia_device_id), "pcmcia",
607 do_pcmcia_entry, mod);
608 else if (sym_is(symname, "__mod_of_device_table"))
609 do_table(symval, sym->st_size,
610 sizeof(struct of_device_id), "of",
612 else if (sym_is(symname, "__mod_vio_device_table"))
613 do_table(symval, sym->st_size,
614 sizeof(struct vio_device_id), "vio",
616 else if (sym_is(symname, "__mod_i2c_device_table"))
617 do_table(symval, sym->st_size,
618 sizeof(struct i2c_device_id), "i2c",
620 else if (sym_is(symname, "__mod_input_device_table"))
621 do_table(symval, sym->st_size,
622 sizeof(struct input_device_id), "input",
623 do_input_entry, mod);
624 else if (sym_is(symname, "__mod_eisa_device_table"))
625 do_table(symval, sym->st_size,
626 sizeof(struct eisa_device_id), "eisa",
628 else if (sym_is(symname, "__mod_parisc_device_table"))
629 do_table(symval, sym->st_size,
630 sizeof(struct parisc_device_id), "parisc",
631 do_parisc_entry, mod);
632 else if (sym_is(symname, "__mod_sdio_device_table"))
633 do_table(symval, sym->st_size,
634 sizeof(struct sdio_device_id), "sdio",
636 else if (sym_is(symname, "__mod_ssb_device_table"))
637 do_table(symval, sym->st_size,
638 sizeof(struct ssb_device_id), "ssb",
642 /* Now add out buffered information to the generated C source */
643 void add_moddevtable(struct buffer *buf, struct module *mod)
645 buf_printf(buf, "\n");
646 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
647 free(mod->dev_table_buf.p);