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"
40 #include "../../include/linux/input.h"
42 #define ADD(str, sep, cond, field) \
46 sprintf(str + strlen(str), \
47 sizeof(field) == 1 ? "%02X" : \
48 sizeof(field) == 2 ? "%04X" : \
49 sizeof(field) == 4 ? "%08X" : "", \
52 sprintf(str + strlen(str), "*"); \
55 /* USB is special because the bcdDevice can be matched against a numeric range */
56 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
57 static void do_usb_entry(struct usb_device_id *id,
58 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
59 unsigned char range_lo, unsigned char range_hi,
63 strcpy(alias, "usb:");
64 ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
66 ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
70 if (bcdDevice_initial_digits)
71 sprintf(alias + strlen(alias), "%0*X",
72 bcdDevice_initial_digits, bcdDevice_initial);
73 if (range_lo == range_hi)
74 sprintf(alias + strlen(alias), "%u", range_lo);
75 else if (range_lo > 0 || range_hi < 9)
76 sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
77 if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
80 ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
83 id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
86 id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
89 id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
92 id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
93 id->bInterfaceSubClass);
95 id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
96 id->bInterfaceProtocol);
98 /* Always end in a wildcard, for future extension */
99 if (alias[strlen(alias)-1] != '*')
101 buf_printf(&mod->dev_table_buf,
102 "MODULE_ALIAS(\"%s\");\n", alias);
105 static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
107 unsigned int devlo, devhi;
108 unsigned char chi, clo;
111 id->match_flags = TO_NATIVE(id->match_flags);
112 id->idVendor = TO_NATIVE(id->idVendor);
113 id->idProduct = TO_NATIVE(id->idProduct);
115 devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
116 TO_NATIVE(id->bcdDevice_lo) : 0x0U;
117 devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
118 TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
121 * Some modules (visor) have empty slots as placeholder for
122 * run-time specification that results in catch-all alias
124 if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
127 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
128 for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
131 if (chi > 9) /* it's bcd not hex */
136 if (devlo == devhi || !ndigits) {
137 do_usb_entry(id, devlo, ndigits, clo, chi, mod);
142 do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
145 do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
149 static void do_usb_table(void *symval, unsigned long size,
153 const unsigned long id_size = sizeof(struct usb_device_id);
155 if (size % id_size || size < id_size) {
156 fprintf(stderr, "*** Warning: %s ids %lu bad size "
157 "(each on %lu)\n", mod->name, size, id_size);
159 /* Leave last one: it's the terminator. */
162 for (i = 0; i < size; i += id_size)
163 do_usb_entry_multi(symval + i, mod);
166 /* Looks like: ieee1394:venNmoNspNverN */
167 static int do_ieee1394_entry(const char *filename,
168 struct ieee1394_device_id *id, char *alias)
170 id->match_flags = TO_NATIVE(id->match_flags);
171 id->vendor_id = TO_NATIVE(id->vendor_id);
172 id->model_id = TO_NATIVE(id->model_id);
173 id->specifier_id = TO_NATIVE(id->specifier_id);
174 id->version = TO_NATIVE(id->version);
176 strcpy(alias, "ieee1394:");
177 ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
179 ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
181 ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
183 ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
189 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
190 static int do_pci_entry(const char *filename,
191 struct pci_device_id *id, char *alias)
193 /* Class field can be divided into these three. */
194 unsigned char baseclass, subclass, interface,
195 baseclass_mask, subclass_mask, interface_mask;
197 id->vendor = TO_NATIVE(id->vendor);
198 id->device = TO_NATIVE(id->device);
199 id->subvendor = TO_NATIVE(id->subvendor);
200 id->subdevice = TO_NATIVE(id->subdevice);
201 id->class = TO_NATIVE(id->class);
202 id->class_mask = TO_NATIVE(id->class_mask);
204 strcpy(alias, "pci:");
205 ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
206 ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
207 ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
208 ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
210 baseclass = (id->class) >> 16;
211 baseclass_mask = (id->class_mask) >> 16;
212 subclass = (id->class) >> 8;
213 subclass_mask = (id->class_mask) >> 8;
214 interface = id->class;
215 interface_mask = id->class_mask;
217 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
218 || (subclass_mask != 0 && subclass_mask != 0xFF)
219 || (interface_mask != 0 && interface_mask != 0xFF)) {
221 "*** Warning: Can't handle masks in %s:%04X\n",
222 filename, id->class_mask);
226 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
227 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
228 ADD(alias, "i", interface_mask == 0xFF, interface);
232 /* looks like: "ccw:tNmNdtNdmN" */
233 static int do_ccw_entry(const char *filename,
234 struct ccw_device_id *id, char *alias)
236 id->match_flags = TO_NATIVE(id->match_flags);
237 id->cu_type = TO_NATIVE(id->cu_type);
238 id->cu_model = TO_NATIVE(id->cu_model);
239 id->dev_type = TO_NATIVE(id->dev_type);
240 id->dev_model = TO_NATIVE(id->dev_model);
242 strcpy(alias, "ccw:");
243 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
245 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
247 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
249 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
254 /* Looks like: "serio:tyNprNidNexN" */
255 static int do_serio_entry(const char *filename,
256 struct serio_device_id *id, char *alias)
258 id->type = TO_NATIVE(id->type);
259 id->proto = TO_NATIVE(id->proto);
260 id->id = TO_NATIVE(id->id);
261 id->extra = TO_NATIVE(id->extra);
263 strcpy(alias, "serio:");
264 ADD(alias, "ty", id->type != SERIO_ANY, id->type);
265 ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
266 ADD(alias, "id", id->id != SERIO_ANY, id->id);
267 ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
272 /* looks like: "pnp:dD" */
273 static int do_pnp_entry(const char *filename,
274 struct pnp_device_id *id, char *alias)
276 sprintf(alias, "pnp:d%s", id->id);
280 /* looks like: "pnp:cCdD..." */
281 static int do_pnp_card_entry(const char *filename,
282 struct pnp_card_device_id *id, char *alias)
286 sprintf(alias, "pnp:c%s", id->id);
287 for (i = 0; i < PNP_MAX_DEVICES; i++) {
288 if (! *id->devs[i].id)
290 sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
295 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
296 static int do_pcmcia_entry(const char *filename,
297 struct pcmcia_device_id *id, char *alias)
301 id->match_flags = TO_NATIVE(id->match_flags);
302 id->manf_id = TO_NATIVE(id->manf_id);
303 id->card_id = TO_NATIVE(id->card_id);
304 id->func_id = TO_NATIVE(id->func_id);
305 id->function = TO_NATIVE(id->function);
306 id->device_no = TO_NATIVE(id->device_no);
308 for (i=0; i<4; i++) {
309 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
312 strcpy(alias, "pcmcia:");
313 ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
315 ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
317 ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
319 ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
321 ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
323 ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
324 ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
325 ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
326 ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
333 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
336 sprintf (alias, "of:N%sT%sC%s",
337 of->name[0] ? of->name : "*",
338 of->type[0] ? of->type : "*",
339 of->compatible[0] ? of->compatible : "*");
341 /* Replace all whitespace with underscores */
342 for (tmp = alias; tmp && *tmp; tmp++)
349 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
354 sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
355 vio->compat[0] ? vio->compat : "*");
357 /* Replace all whitespace with underscores */
358 for (tmp = alias; tmp && *tmp; tmp++)
365 static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias)
367 strcpy(alias, "i2c:");
368 ADD(alias, "id", 1, i2c->id);
372 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
374 static void do_input(char *alias,
375 kernel_ulong_t *arr, unsigned int min, unsigned int max)
378 for (i = min; i < max; i++) {
379 if (arr[i/BITS_PER_LONG] & (1 << (i%BITS_PER_LONG)))
380 sprintf(alias+strlen(alias), "%X,*", i);
384 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
385 static int do_input_entry(const char *filename, struct input_device_id *id,
388 sprintf(alias, "input:");
390 ADD(alias, "b", id->flags&INPUT_DEVICE_ID_MATCH_BUS, id->id.bustype);
391 ADD(alias, "v", id->flags&INPUT_DEVICE_ID_MATCH_VENDOR, id->id.vendor);
392 ADD(alias, "p", id->flags&INPUT_DEVICE_ID_MATCH_PRODUCT,
394 ADD(alias, "e", id->flags&INPUT_DEVICE_ID_MATCH_VERSION,
397 sprintf(alias + strlen(alias), "-e*");
398 if (id->flags&INPUT_DEVICE_ID_MATCH_EVBIT)
399 do_input(alias, id->evbit, 0, EV_MAX);
400 sprintf(alias + strlen(alias), "k*");
401 if (id->flags&INPUT_DEVICE_ID_MATCH_KEYBIT)
402 do_input(alias, id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
403 sprintf(alias + strlen(alias), "r*");
404 if (id->flags&INPUT_DEVICE_ID_MATCH_RELBIT)
405 do_input(alias, id->relbit, 0, REL_MAX);
406 sprintf(alias + strlen(alias), "a*");
407 if (id->flags&INPUT_DEVICE_ID_MATCH_ABSBIT)
408 do_input(alias, id->absbit, 0, ABS_MAX);
409 sprintf(alias + strlen(alias), "m*");
410 if (id->flags&INPUT_DEVICE_ID_MATCH_MSCIT)
411 do_input(alias, id->mscbit, 0, MSC_MAX);
412 sprintf(alias + strlen(alias), "l*");
413 if (id->flags&INPUT_DEVICE_ID_MATCH_LEDBIT)
414 do_input(alias, id->ledbit, 0, LED_MAX);
415 sprintf(alias + strlen(alias), "s*");
416 if (id->flags&INPUT_DEVICE_ID_MATCH_SNDBIT)
417 do_input(alias, id->sndbit, 0, SND_MAX);
418 sprintf(alias + strlen(alias), "f*");
419 if (id->flags&INPUT_DEVICE_ID_MATCH_FFBIT)
420 do_input(alias, id->ffbit, 0, FF_MAX);
421 sprintf(alias + strlen(alias), "w*");
422 if (id->flags&INPUT_DEVICE_ID_MATCH_SWBIT)
423 do_input(alias, id->swbit, 0, SW_MAX);
427 /* Ignore any prefix, eg. v850 prepends _ */
428 static inline int sym_is(const char *symbol, const char *name)
432 match = strstr(symbol, name);
435 return match[strlen(symbol)] == '\0';
438 static void do_table(void *symval, unsigned long size,
439 unsigned long id_size,
445 int (*do_entry)(const char *, void *entry, char *alias) = function;
447 if (size % id_size || size < id_size) {
448 fprintf(stderr, "*** Warning: %s ids %lu bad size "
449 "(each on %lu)\n", mod->name, size, id_size);
451 /* Leave last one: it's the terminator. */
454 for (i = 0; i < size; i += id_size) {
455 if (do_entry(mod->name, symval+i, alias)) {
456 /* Always end in a wildcard, for future extension */
457 if (alias[strlen(alias)-1] != '*')
459 buf_printf(&mod->dev_table_buf,
460 "MODULE_ALIAS(\"%s\");\n", alias);
465 /* Create MODULE_ALIAS() statements.
466 * At this time, we cannot write the actual output C source yet,
467 * so we write into the mod->dev_table_buf buffer. */
468 void handle_moddevtable(struct module *mod, struct elf_info *info,
469 Elf_Sym *sym, const char *symname)
473 /* We're looking for a section relative symbol */
474 if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
477 symval = (void *)info->hdr
478 + info->sechdrs[sym->st_shndx].sh_offset
481 if (sym_is(symname, "__mod_pci_device_table"))
482 do_table(symval, sym->st_size, sizeof(struct pci_device_id),
484 else if (sym_is(symname, "__mod_usb_device_table"))
485 /* special case to handle bcdDevice ranges */
486 do_usb_table(symval, sym->st_size, mod);
487 else if (sym_is(symname, "__mod_ieee1394_device_table"))
488 do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
489 do_ieee1394_entry, mod);
490 else if (sym_is(symname, "__mod_ccw_device_table"))
491 do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
493 else if (sym_is(symname, "__mod_serio_device_table"))
494 do_table(symval, sym->st_size, sizeof(struct serio_device_id),
495 do_serio_entry, mod);
496 else if (sym_is(symname, "__mod_pnp_device_table"))
497 do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
499 else if (sym_is(symname, "__mod_pnp_card_device_table"))
500 do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
501 do_pnp_card_entry, mod);
502 else if (sym_is(symname, "__mod_pcmcia_device_table"))
503 do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id),
504 do_pcmcia_entry, mod);
505 else if (sym_is(symname, "__mod_of_device_table"))
506 do_table(symval, sym->st_size, sizeof(struct of_device_id),
508 else if (sym_is(symname, "__mod_vio_device_table"))
509 do_table(symval, sym->st_size, sizeof(struct vio_device_id),
511 else if (sym_is(symname, "__mod_i2c_device_table"))
512 do_table(symval, sym->st_size, sizeof(struct i2c_device_id),
514 else if (sym_is(symname, "__mod_input_device_table"))
515 do_table(symval, sym->st_size, sizeof(struct input_device_id),
516 do_input_entry, mod);
519 /* Now add out buffered information to the generated C source */
520 void add_moddevtable(struct buffer *buf, struct module *mod)
522 buf_printf(buf, "\n");
523 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
524 free(mod->dev_table_buf.p);