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 warn("%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)) {
220 warn("Can't handle masks in %s:%04X\n",
221 filename, id->class_mask);
225 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
226 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
227 ADD(alias, "i", interface_mask == 0xFF, interface);
231 /* looks like: "ccw:tNmNdtNdmN" */
232 static int do_ccw_entry(const char *filename,
233 struct ccw_device_id *id, char *alias)
235 id->match_flags = TO_NATIVE(id->match_flags);
236 id->cu_type = TO_NATIVE(id->cu_type);
237 id->cu_model = TO_NATIVE(id->cu_model);
238 id->dev_type = TO_NATIVE(id->dev_type);
239 id->dev_model = TO_NATIVE(id->dev_model);
241 strcpy(alias, "ccw:");
242 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
244 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
246 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
248 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
253 /* Looks like: "serio:tyNprNidNexN" */
254 static int do_serio_entry(const char *filename,
255 struct serio_device_id *id, char *alias)
257 id->type = TO_NATIVE(id->type);
258 id->proto = TO_NATIVE(id->proto);
259 id->id = TO_NATIVE(id->id);
260 id->extra = TO_NATIVE(id->extra);
262 strcpy(alias, "serio:");
263 ADD(alias, "ty", id->type != SERIO_ANY, id->type);
264 ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
265 ADD(alias, "id", id->id != SERIO_ANY, id->id);
266 ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
271 /* looks like: "pnp:dD" */
272 static int do_pnp_entry(const char *filename,
273 struct pnp_device_id *id, char *alias)
275 sprintf(alias, "pnp:d%s", id->id);
279 /* looks like: "pnp:cCdD..." */
280 static int do_pnp_card_entry(const char *filename,
281 struct pnp_card_device_id *id, char *alias)
285 sprintf(alias, "pnp:c%s", id->id);
286 for (i = 0; i < PNP_MAX_DEVICES; i++) {
287 if (! *id->devs[i].id)
289 sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
294 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
295 static int do_pcmcia_entry(const char *filename,
296 struct pcmcia_device_id *id, char *alias)
300 id->match_flags = TO_NATIVE(id->match_flags);
301 id->manf_id = TO_NATIVE(id->manf_id);
302 id->card_id = TO_NATIVE(id->card_id);
303 id->func_id = TO_NATIVE(id->func_id);
304 id->function = TO_NATIVE(id->function);
305 id->device_no = TO_NATIVE(id->device_no);
307 for (i=0; i<4; i++) {
308 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
311 strcpy(alias, "pcmcia:");
312 ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
314 ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
316 ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
318 ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
320 ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
322 ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
323 ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
324 ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
325 ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
332 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
335 sprintf (alias, "of:N%sT%sC%s",
336 of->name[0] ? of->name : "*",
337 of->type[0] ? of->type : "*",
338 of->compatible[0] ? of->compatible : "*");
340 /* Replace all whitespace with underscores */
341 for (tmp = alias; tmp && *tmp; tmp++)
348 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
353 sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
354 vio->compat[0] ? vio->compat : "*");
356 /* Replace all whitespace with underscores */
357 for (tmp = alias; tmp && *tmp; tmp++)
364 static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias)
366 strcpy(alias, "i2c:");
367 ADD(alias, "id", 1, i2c->id);
371 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
373 static void do_input(char *alias,
374 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);
383 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
384 static int do_input_entry(const char *filename, struct input_device_id *id,
387 sprintf(alias, "input:");
389 ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
390 ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
391 ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
392 ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
394 sprintf(alias + strlen(alias), "-e*");
395 if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
396 do_input(alias, id->evbit, 0, EV_MAX);
397 sprintf(alias + strlen(alias), "k*");
398 if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
399 do_input(alias, id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
400 sprintf(alias + strlen(alias), "r*");
401 if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
402 do_input(alias, id->relbit, 0, REL_MAX);
403 sprintf(alias + strlen(alias), "a*");
404 if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
405 do_input(alias, id->absbit, 0, ABS_MAX);
406 sprintf(alias + strlen(alias), "m*");
407 if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
408 do_input(alias, id->mscbit, 0, MSC_MAX);
409 sprintf(alias + strlen(alias), "l*");
410 if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
411 do_input(alias, id->ledbit, 0, LED_MAX);
412 sprintf(alias + strlen(alias), "s*");
413 if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
414 do_input(alias, id->sndbit, 0, SND_MAX);
415 sprintf(alias + strlen(alias), "f*");
416 if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
417 do_input(alias, id->ffbit, 0, FF_MAX);
418 sprintf(alias + strlen(alias), "w*");
419 if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
420 do_input(alias, id->swbit, 0, SW_MAX);
424 /* Ignore any prefix, eg. v850 prepends _ */
425 static inline int sym_is(const char *symbol, const char *name)
429 match = strstr(symbol, name);
432 return match[strlen(symbol)] == '\0';
435 static void do_table(void *symval, unsigned long size,
436 unsigned long id_size,
442 int (*do_entry)(const char *, void *entry, char *alias) = function;
444 if (size % id_size || size < id_size) {
445 warn("%s ids %lu bad size "
446 "(each on %lu)\n", mod->name, size, id_size);
448 /* Leave last one: it's the terminator. */
451 for (i = 0; i < size; i += id_size) {
452 if (do_entry(mod->name, symval+i, alias)) {
453 /* Always end in a wildcard, for future extension */
454 if (alias[strlen(alias)-1] != '*')
456 buf_printf(&mod->dev_table_buf,
457 "MODULE_ALIAS(\"%s\");\n", alias);
462 /* Create MODULE_ALIAS() statements.
463 * At this time, we cannot write the actual output C source yet,
464 * so we write into the mod->dev_table_buf buffer. */
465 void handle_moddevtable(struct module *mod, struct elf_info *info,
466 Elf_Sym *sym, const char *symname)
470 /* We're looking for a section relative symbol */
471 if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
474 symval = (void *)info->hdr
475 + info->sechdrs[sym->st_shndx].sh_offset
478 if (sym_is(symname, "__mod_pci_device_table"))
479 do_table(symval, sym->st_size, sizeof(struct pci_device_id),
481 else if (sym_is(symname, "__mod_usb_device_table"))
482 /* special case to handle bcdDevice ranges */
483 do_usb_table(symval, sym->st_size, mod);
484 else if (sym_is(symname, "__mod_ieee1394_device_table"))
485 do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
486 do_ieee1394_entry, mod);
487 else if (sym_is(symname, "__mod_ccw_device_table"))
488 do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
490 else if (sym_is(symname, "__mod_serio_device_table"))
491 do_table(symval, sym->st_size, sizeof(struct serio_device_id),
492 do_serio_entry, mod);
493 else if (sym_is(symname, "__mod_pnp_device_table"))
494 do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
496 else if (sym_is(symname, "__mod_pnp_card_device_table"))
497 do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
498 do_pnp_card_entry, mod);
499 else if (sym_is(symname, "__mod_pcmcia_device_table"))
500 do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id),
501 do_pcmcia_entry, mod);
502 else if (sym_is(symname, "__mod_of_device_table"))
503 do_table(symval, sym->st_size, sizeof(struct of_device_id),
505 else if (sym_is(symname, "__mod_vio_device_table"))
506 do_table(symval, sym->st_size, sizeof(struct vio_device_id),
508 else if (sym_is(symname, "__mod_i2c_device_table"))
509 do_table(symval, sym->st_size, sizeof(struct i2c_device_id),
511 else if (sym_is(symname, "__mod_input_device_table"))
512 do_table(symval, sym->st_size, sizeof(struct input_device_id),
513 do_input_entry, mod);
516 /* Now add out buffered information to the generated C source */
517 void add_moddevtable(struct buffer *buf, struct module *mod)
519 buf_printf(buf, "\n");
520 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
521 free(mod->dev_table_buf.p);