On Tue, Nov 06, 2007 at 02:33:53AM -0800, akpm@linux-foundation.org wrote:
[mmotm.git] / scripts / mod / file2alias.c
blob62a9025cdcc78c0640c2457f4735e0d782f815c6
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.
13 #include "modpost.h"
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
20 #else
21 typedef Elf64_Addr kernel_ulong_t;
22 #define BITS_PER_LONG 64
23 #endif
24 #ifdef __sun__
25 #include <inttypes.h>
26 #else
27 #include <stdint.h>
28 #endif
30 #include <ctype.h>
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) \
42 do { \
43 strcat(str, sep); \
44 if (cond) \
45 sprintf(str + strlen(str), \
46 sizeof(field) == 1 ? "%02X" : \
47 sizeof(field) == 2 ? "%04X" : \
48 sizeof(field) == 4 ? "%08X" : "", \
49 field); \
50 else \
51 sprintf(str + strlen(str), "*"); \
52 } while(0)
54 /* Always end in a wildcard, for future extension */
55 static inline void add_wildcard(char *str)
57 int len = strlen(str);
59 if (str[len - 1] != '*')
60 strcat(str + len, "*");
63 unsigned int cross_build = 0;
64 /**
65 * Check that sizeof(device_id type) are consistent with size of section
66 * in .o file. If in-consistent then userspace and kernel does not agree
67 * on actual size which is a bug.
68 * Also verify that the final entry in the table is all zeros.
69 * Ignore both checks if build host differ from target host and size differs.
70 **/
71 static void device_id_check(const char *modname, const char *device_id,
72 unsigned long size, unsigned long id_size,
73 void *symval)
75 int i;
77 if (size % id_size || size < id_size) {
78 if (cross_build != 0)
79 return;
80 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
81 "of the size of section __mod_%s_device_table=%lu.\n"
82 "Fix definition of struct %s_device_id "
83 "in mod_devicetable.h\n",
84 modname, device_id, id_size, device_id, size, device_id);
86 /* Verify last one is a terminator */
87 for (i = 0; i < id_size; i++ ) {
88 if (*(uint8_t*)(symval+size-id_size+i)) {
89 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
90 "The last of %lu is:\n",
91 modname, device_id, id_size, size / id_size);
92 for (i = 0; i < id_size; i++ )
93 fprintf(stderr,"0x%02x ",
94 *(uint8_t*)(symval+size-id_size+i) );
95 fprintf(stderr,"\n");
96 fatal("%s: struct %s_device_id is not terminated "
97 "with a NULL entry!\n", modname, device_id);
102 /* USB is special because the bcdDevice can be matched against a numeric range */
103 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
104 static void do_usb_entry(struct usb_device_id *id,
105 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
106 unsigned char range_lo, unsigned char range_hi,
107 struct module *mod)
109 char alias[500];
110 strcpy(alias, "usb:");
111 ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
112 id->idVendor);
113 ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
114 id->idProduct);
116 strcat(alias, "d");
117 if (bcdDevice_initial_digits)
118 sprintf(alias + strlen(alias), "%0*X",
119 bcdDevice_initial_digits, bcdDevice_initial);
120 if (range_lo == range_hi)
121 sprintf(alias + strlen(alias), "%u", range_lo);
122 else if (range_lo > 0 || range_hi < 9)
123 sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
124 if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
125 strcat(alias, "*");
127 ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
128 id->bDeviceClass);
129 ADD(alias, "dsc",
130 id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
131 id->bDeviceSubClass);
132 ADD(alias, "dp",
133 id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
134 id->bDeviceProtocol);
135 ADD(alias, "ic",
136 id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
137 id->bInterfaceClass);
138 ADD(alias, "isc",
139 id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
140 id->bInterfaceSubClass);
141 ADD(alias, "ip",
142 id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
143 id->bInterfaceProtocol);
145 add_wildcard(alias);
146 buf_printf(&mod->dev_table_buf,
147 "MODULE_ALIAS(\"%s\");\n", alias);
150 static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
152 unsigned int devlo, devhi;
153 unsigned char chi, clo;
154 int ndigits;
156 id->match_flags = TO_NATIVE(id->match_flags);
157 id->idVendor = TO_NATIVE(id->idVendor);
158 id->idProduct = TO_NATIVE(id->idProduct);
160 devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
161 TO_NATIVE(id->bcdDevice_lo) : 0x0U;
162 devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
163 TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
166 * Some modules (visor) have empty slots as placeholder for
167 * run-time specification that results in catch-all alias
169 if (!(id->idVendor | id->idProduct | id->bDeviceClass | id->bInterfaceClass))
170 return;
172 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
173 for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
174 clo = devlo & 0xf;
175 chi = devhi & 0xf;
176 if (chi > 9) /* it's bcd not hex */
177 chi = 9;
178 devlo >>= 4;
179 devhi >>= 4;
181 if (devlo == devhi || !ndigits) {
182 do_usb_entry(id, devlo, ndigits, clo, chi, mod);
183 break;
186 if (clo > 0)
187 do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
189 if (chi < 9)
190 do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
194 static void do_usb_table(void *symval, unsigned long size,
195 struct module *mod)
197 unsigned int i;
198 const unsigned long id_size = sizeof(struct usb_device_id);
200 device_id_check(mod->name, "usb", size, id_size, symval);
202 /* Leave last one: it's the terminator. */
203 size -= id_size;
205 for (i = 0; i < size; i += id_size)
206 do_usb_entry_multi(symval + i, mod);
209 /* Looks like: hid:bNvNpN */
210 static int do_hid_entry(const char *filename,
211 struct hid_device_id *id, char *alias)
213 id->bus = TO_NATIVE(id->bus);
214 id->vendor = TO_NATIVE(id->vendor);
215 id->product = TO_NATIVE(id->product);
217 sprintf(alias, "hid:b%04X", id->bus);
218 ADD(alias, "v", id->vendor != HID_ANY_ID, id->vendor);
219 ADD(alias, "p", id->product != HID_ANY_ID, id->product);
221 return 1;
224 /* Looks like: ieee1394:venNmoNspNverN */
225 static int do_ieee1394_entry(const char *filename,
226 struct ieee1394_device_id *id, char *alias)
228 id->match_flags = TO_NATIVE(id->match_flags);
229 id->vendor_id = TO_NATIVE(id->vendor_id);
230 id->model_id = TO_NATIVE(id->model_id);
231 id->specifier_id = TO_NATIVE(id->specifier_id);
232 id->version = TO_NATIVE(id->version);
234 strcpy(alias, "ieee1394:");
235 ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
236 id->vendor_id);
237 ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
238 id->model_id);
239 ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
240 id->specifier_id);
241 ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
242 id->version);
244 add_wildcard(alias);
245 return 1;
248 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
249 static int do_pci_entry(const char *filename,
250 struct pci_device_id *id, char *alias)
252 /* Class field can be divided into these three. */
253 unsigned char baseclass, subclass, interface,
254 baseclass_mask, subclass_mask, interface_mask;
256 id->vendor = TO_NATIVE(id->vendor);
257 id->device = TO_NATIVE(id->device);
258 id->subvendor = TO_NATIVE(id->subvendor);
259 id->subdevice = TO_NATIVE(id->subdevice);
260 id->class = TO_NATIVE(id->class);
261 id->class_mask = TO_NATIVE(id->class_mask);
263 strcpy(alias, "pci:");
264 ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
265 ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
266 ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
267 ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
269 baseclass = (id->class) >> 16;
270 baseclass_mask = (id->class_mask) >> 16;
271 subclass = (id->class) >> 8;
272 subclass_mask = (id->class_mask) >> 8;
273 interface = id->class;
274 interface_mask = id->class_mask;
276 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
277 || (subclass_mask != 0 && subclass_mask != 0xFF)
278 || (interface_mask != 0 && interface_mask != 0xFF)) {
279 warn("Can't handle masks in %s:%04X\n",
280 filename, id->class_mask);
281 return 0;
284 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
285 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
286 ADD(alias, "i", interface_mask == 0xFF, interface);
287 add_wildcard(alias);
288 return 1;
291 /* looks like: "ccw:tNmNdtNdmN" */
292 static int do_ccw_entry(const char *filename,
293 struct ccw_device_id *id, char *alias)
295 id->match_flags = TO_NATIVE(id->match_flags);
296 id->cu_type = TO_NATIVE(id->cu_type);
297 id->cu_model = TO_NATIVE(id->cu_model);
298 id->dev_type = TO_NATIVE(id->dev_type);
299 id->dev_model = TO_NATIVE(id->dev_model);
301 strcpy(alias, "ccw:");
302 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
303 id->cu_type);
304 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
305 id->cu_model);
306 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
307 id->dev_type);
308 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
309 id->dev_model);
310 add_wildcard(alias);
311 return 1;
314 /* looks like: "ap:tN" */
315 static int do_ap_entry(const char *filename,
316 struct ap_device_id *id, char *alias)
318 sprintf(alias, "ap:t%02X*", id->dev_type);
319 return 1;
322 /* looks like: "css:tN" */
323 static int do_css_entry(const char *filename,
324 struct css_device_id *id, char *alias)
326 sprintf(alias, "css:t%01X", id->type);
327 return 1;
330 /* Looks like: "serio:tyNprNidNexN" */
331 static int do_serio_entry(const char *filename,
332 struct serio_device_id *id, char *alias)
334 id->type = TO_NATIVE(id->type);
335 id->proto = TO_NATIVE(id->proto);
336 id->id = TO_NATIVE(id->id);
337 id->extra = TO_NATIVE(id->extra);
339 strcpy(alias, "serio:");
340 ADD(alias, "ty", id->type != SERIO_ANY, id->type);
341 ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
342 ADD(alias, "id", id->id != SERIO_ANY, id->id);
343 ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
345 add_wildcard(alias);
346 return 1;
349 /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
350 static int do_acpi_entry(const char *filename,
351 struct acpi_device_id *id, char *alias)
353 sprintf(alias, "acpi*:%s:*", id->id);
354 return 1;
357 /* looks like: "pnp:dD" */
358 static void do_pnp_device_entry(void *symval, unsigned long size,
359 struct module *mod)
361 const unsigned long id_size = sizeof(struct pnp_device_id);
362 const unsigned int count = (size / id_size)-1;
363 const struct pnp_device_id *devs = symval;
364 unsigned int i;
366 device_id_check(mod->name, "pnp", size, id_size, symval);
368 for (i = 0; i < count; i++) {
369 const char *id = (char *)devs[i].id;
370 char acpi_id[sizeof(devs[0].id)];
371 int j;
373 buf_printf(&mod->dev_table_buf,
374 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
376 /* fix broken pnp bus lowercasing */
377 for (j = 0; j < sizeof(acpi_id); j++)
378 acpi_id[j] = toupper(id[j]);
379 buf_printf(&mod->dev_table_buf,
380 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
384 /* looks like: "pnp:dD" for every device of the card */
385 static void do_pnp_card_entries(void *symval, unsigned long size,
386 struct module *mod)
388 const unsigned long id_size = sizeof(struct pnp_card_device_id);
389 const unsigned int count = (size / id_size)-1;
390 const struct pnp_card_device_id *cards = symval;
391 unsigned int i;
393 device_id_check(mod->name, "pnp", size, id_size, symval);
395 for (i = 0; i < count; i++) {
396 unsigned int j;
397 const struct pnp_card_device_id *card = &cards[i];
399 for (j = 0; j < PNP_MAX_DEVICES; j++) {
400 const char *id = (char *)card->devs[j].id;
401 int i2, j2;
402 int dup = 0;
404 if (!id[0])
405 break;
407 /* find duplicate, already added value */
408 for (i2 = 0; i2 < i && !dup; i2++) {
409 const struct pnp_card_device_id *card2 = &cards[i2];
411 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
412 const char *id2 = (char *)card2->devs[j2].id;
414 if (!id2[0])
415 break;
417 if (!strcmp(id, id2)) {
418 dup = 1;
419 break;
424 /* add an individual alias for every device entry */
425 if (!dup) {
426 char acpi_id[sizeof(card->devs[0].id)];
427 int k;
429 buf_printf(&mod->dev_table_buf,
430 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
432 /* fix broken pnp bus lowercasing */
433 for (k = 0; k < sizeof(acpi_id); k++)
434 acpi_id[k] = toupper(id[k]);
435 buf_printf(&mod->dev_table_buf,
436 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
442 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
443 static int do_pcmcia_entry(const char *filename,
444 struct pcmcia_device_id *id, char *alias)
446 unsigned int i;
448 id->match_flags = TO_NATIVE(id->match_flags);
449 id->manf_id = TO_NATIVE(id->manf_id);
450 id->card_id = TO_NATIVE(id->card_id);
451 id->func_id = TO_NATIVE(id->func_id);
452 id->function = TO_NATIVE(id->function);
453 id->device_no = TO_NATIVE(id->device_no);
455 for (i=0; i<4; i++) {
456 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
459 strcpy(alias, "pcmcia:");
460 ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
461 id->manf_id);
462 ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
463 id->card_id);
464 ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
465 id->func_id);
466 ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
467 id->function);
468 ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
469 id->device_no);
470 ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
471 ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
472 ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
473 ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
475 add_wildcard(alias);
476 return 1;
481 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
483 int len;
484 char *tmp;
485 len = sprintf (alias, "of:N%sT%s",
486 of->name[0] ? of->name : "*",
487 of->type[0] ? of->type : "*");
489 if (of->compatible[0])
490 sprintf (&alias[len], "%sC%s",
491 of->type[0] ? "*" : "",
492 of->compatible);
494 /* Replace all whitespace with underscores */
495 for (tmp = alias; tmp && *tmp; tmp++)
496 if (isspace (*tmp))
497 *tmp = '_';
499 add_wildcard(alias);
500 return 1;
503 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
504 char *alias)
506 char *tmp;
508 sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
509 vio->compat[0] ? vio->compat : "*");
511 /* Replace all whitespace with underscores */
512 for (tmp = alias; tmp && *tmp; tmp++)
513 if (isspace (*tmp))
514 *tmp = '_';
516 add_wildcard(alias);
517 return 1;
520 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
522 static void do_input(char *alias,
523 kernel_ulong_t *arr, unsigned int min, unsigned int max)
525 unsigned int i;
527 for (i = min; i < max; i++)
528 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
529 sprintf(alias + strlen(alias), "%X,*", i);
532 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
533 static int do_input_entry(const char *filename, struct input_device_id *id,
534 char *alias)
536 sprintf(alias, "input:");
538 ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
539 ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
540 ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
541 ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
543 sprintf(alias + strlen(alias), "-e*");
544 if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
545 do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX);
546 sprintf(alias + strlen(alias), "k*");
547 if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
548 do_input(alias, id->keybit,
549 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
550 INPUT_DEVICE_ID_KEY_MAX);
551 sprintf(alias + strlen(alias), "r*");
552 if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
553 do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX);
554 sprintf(alias + strlen(alias), "a*");
555 if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
556 do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
557 sprintf(alias + strlen(alias), "m*");
558 if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
559 do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
560 sprintf(alias + strlen(alias), "l*");
561 if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
562 do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
563 sprintf(alias + strlen(alias), "s*");
564 if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
565 do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
566 sprintf(alias + strlen(alias), "f*");
567 if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
568 do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
569 sprintf(alias + strlen(alias), "w*");
570 if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
571 do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX);
572 return 1;
575 static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
576 char *alias)
578 if (eisa->sig[0])
579 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
580 else
581 strcat(alias, "*");
582 return 1;
585 /* Looks like: parisc:tNhvNrevNsvN */
586 static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
587 char *alias)
589 id->hw_type = TO_NATIVE(id->hw_type);
590 id->hversion = TO_NATIVE(id->hversion);
591 id->hversion_rev = TO_NATIVE(id->hversion_rev);
592 id->sversion = TO_NATIVE(id->sversion);
594 strcpy(alias, "parisc:");
595 ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type);
596 ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion);
597 ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev);
598 ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion);
600 add_wildcard(alias);
601 return 1;
604 /* Looks like: sdio:cNvNdN. */
605 static int do_sdio_entry(const char *filename,
606 struct sdio_device_id *id, char *alias)
608 id->class = TO_NATIVE(id->class);
609 id->vendor = TO_NATIVE(id->vendor);
610 id->device = TO_NATIVE(id->device);
612 strcpy(alias, "sdio:");
613 ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class);
614 ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor);
615 ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device);
616 add_wildcard(alias);
617 return 1;
620 /* Looks like: ssb:vNidNrevN. */
621 static int do_ssb_entry(const char *filename,
622 struct ssb_device_id *id, char *alias)
624 id->vendor = TO_NATIVE(id->vendor);
625 id->coreid = TO_NATIVE(id->coreid);
626 id->revision = TO_NATIVE(id->revision);
628 strcpy(alias, "ssb:");
629 ADD(alias, "v", id->vendor != SSB_ANY_VENDOR, id->vendor);
630 ADD(alias, "id", id->coreid != SSB_ANY_ID, id->coreid);
631 ADD(alias, "rev", id->revision != SSB_ANY_REV, id->revision);
632 add_wildcard(alias);
633 return 1;
636 /* Looks like: virtio:dNvN */
637 static int do_virtio_entry(const char *filename, struct virtio_device_id *id,
638 char *alias)
640 id->device = TO_NATIVE(id->device);
641 id->vendor = TO_NATIVE(id->vendor);
643 strcpy(alias, "virtio:");
644 ADD(alias, "d", id->device != VIRTIO_DEV_ANY_ID, id->device);
645 ADD(alias, "v", id->vendor != VIRTIO_DEV_ANY_ID, id->vendor);
647 add_wildcard(alias);
648 return 1;
651 /* Looks like: i2c:S */
652 static int do_i2c_entry(const char *filename, struct i2c_device_id *id,
653 char *alias)
655 sprintf(alias, I2C_MODULE_PREFIX "%s", id->name);
657 return 1;
660 /* Looks like: spi:S */
661 static int do_spi_entry(const char *filename, struct spi_device_id *id,
662 char *alias)
664 sprintf(alias, SPI_MODULE_PREFIX "%s", id->name);
666 return 1;
669 static const struct dmifield {
670 const char *prefix;
671 int field;
672 } dmi_fields[] = {
673 { "bvn", DMI_BIOS_VENDOR },
674 { "bvr", DMI_BIOS_VERSION },
675 { "bd", DMI_BIOS_DATE },
676 { "svn", DMI_SYS_VENDOR },
677 { "pn", DMI_PRODUCT_NAME },
678 { "pvr", DMI_PRODUCT_VERSION },
679 { "rvn", DMI_BOARD_VENDOR },
680 { "rn", DMI_BOARD_NAME },
681 { "rvr", DMI_BOARD_VERSION },
682 { "cvn", DMI_CHASSIS_VENDOR },
683 { "ct", DMI_CHASSIS_TYPE },
684 { "cvr", DMI_CHASSIS_VERSION },
685 { NULL, DMI_NONE }
688 static void dmi_ascii_filter(char *d, const char *s)
690 /* Filter out characters we don't want to see in the modalias string */
691 for (; *s; s++)
692 if (*s > ' ' && *s < 127 && *s != ':')
693 *(d++) = *s;
695 *d = 0;
699 static int do_dmi_entry(const char *filename, struct dmi_system_id *id,
700 char *alias)
702 int i, j;
704 sprintf(alias, "dmi*");
706 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
707 for (j = 0; j < 4; j++) {
708 if (id->matches[j].slot &&
709 id->matches[j].slot == dmi_fields[i].field) {
710 sprintf(alias + strlen(alias), ":%s*",
711 dmi_fields[i].prefix);
712 dmi_ascii_filter(alias + strlen(alias),
713 id->matches[j].substr);
714 strcat(alias, "*");
719 strcat(alias, ":");
720 return 1;
723 static int do_platform_entry(const char *filename,
724 struct platform_device_id *id, char *alias)
726 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", id->name);
727 return 1;
730 /* Ignore any prefix, eg. some architectures prepend _ */
731 static inline int sym_is(const char *symbol, const char *name)
733 const char *match;
735 match = strstr(symbol, name);
736 if (!match)
737 return 0;
738 return match[strlen(symbol)] == '\0';
741 static void do_table(void *symval, unsigned long size,
742 unsigned long id_size,
743 const char *device_id,
744 void *function,
745 struct module *mod)
747 unsigned int i;
748 char alias[500];
749 int (*do_entry)(const char *, void *entry, char *alias) = function;
751 device_id_check(mod->name, device_id, size, id_size, symval);
752 /* Leave last one: it's the terminator. */
753 size -= id_size;
755 for (i = 0; i < size; i += id_size) {
756 if (do_entry(mod->name, symval+i, alias)) {
757 buf_printf(&mod->dev_table_buf,
758 "MODULE_ALIAS(\"%s\");\n", alias);
763 /* Create MODULE_ALIAS() statements.
764 * At this time, we cannot write the actual output C source yet,
765 * so we write into the mod->dev_table_buf buffer. */
766 void handle_moddevtable(struct module *mod, struct elf_info *info,
767 Elf_Sym *sym, const char *symname)
769 void *symval;
770 char *zeros = NULL;
772 /* We're looking for a section relative symbol */
773 if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
774 return;
776 /* Handle all-NULL symbols allocated into .bss */
777 if (info->sechdrs[sym->st_shndx].sh_type & SHT_NOBITS) {
778 zeros = calloc(1, sym->st_size);
779 symval = zeros;
780 } else {
781 symval = (void *)info->hdr
782 + info->sechdrs[sym->st_shndx].sh_offset
783 + sym->st_value;
786 if (sym_is(symname, "__mod_pci_device_table"))
787 do_table(symval, sym->st_size,
788 sizeof(struct pci_device_id), "pci",
789 do_pci_entry, mod);
790 else if (sym_is(symname, "__mod_usb_device_table"))
791 /* special case to handle bcdDevice ranges */
792 do_usb_table(symval, sym->st_size, mod);
793 else if (sym_is(symname, "__mod_hid_device_table"))
794 do_table(symval, sym->st_size,
795 sizeof(struct hid_device_id), "hid",
796 do_hid_entry, mod);
797 else if (sym_is(symname, "__mod_ieee1394_device_table"))
798 do_table(symval, sym->st_size,
799 sizeof(struct ieee1394_device_id), "ieee1394",
800 do_ieee1394_entry, mod);
801 else if (sym_is(symname, "__mod_ccw_device_table"))
802 do_table(symval, sym->st_size,
803 sizeof(struct ccw_device_id), "ccw",
804 do_ccw_entry, mod);
805 else if (sym_is(symname, "__mod_ap_device_table"))
806 do_table(symval, sym->st_size,
807 sizeof(struct ap_device_id), "ap",
808 do_ap_entry, mod);
809 else if (sym_is(symname, "__mod_css_device_table"))
810 do_table(symval, sym->st_size,
811 sizeof(struct css_device_id), "css",
812 do_css_entry, mod);
813 else if (sym_is(symname, "__mod_serio_device_table"))
814 do_table(symval, sym->st_size,
815 sizeof(struct serio_device_id), "serio",
816 do_serio_entry, mod);
817 else if (sym_is(symname, "__mod_acpi_device_table"))
818 do_table(symval, sym->st_size,
819 sizeof(struct acpi_device_id), "acpi",
820 do_acpi_entry, mod);
821 else if (sym_is(symname, "__mod_pnp_device_table"))
822 do_pnp_device_entry(symval, sym->st_size, mod);
823 else if (sym_is(symname, "__mod_pnp_card_device_table"))
824 do_pnp_card_entries(symval, sym->st_size, mod);
825 else if (sym_is(symname, "__mod_pcmcia_device_table"))
826 do_table(symval, sym->st_size,
827 sizeof(struct pcmcia_device_id), "pcmcia",
828 do_pcmcia_entry, mod);
829 else if (sym_is(symname, "__mod_of_device_table"))
830 do_table(symval, sym->st_size,
831 sizeof(struct of_device_id), "of",
832 do_of_entry, mod);
833 else if (sym_is(symname, "__mod_vio_device_table"))
834 do_table(symval, sym->st_size,
835 sizeof(struct vio_device_id), "vio",
836 do_vio_entry, mod);
837 else if (sym_is(symname, "__mod_input_device_table"))
838 do_table(symval, sym->st_size,
839 sizeof(struct input_device_id), "input",
840 do_input_entry, mod);
841 else if (sym_is(symname, "__mod_eisa_device_table"))
842 do_table(symval, sym->st_size,
843 sizeof(struct eisa_device_id), "eisa",
844 do_eisa_entry, mod);
845 else if (sym_is(symname, "__mod_parisc_device_table"))
846 do_table(symval, sym->st_size,
847 sizeof(struct parisc_device_id), "parisc",
848 do_parisc_entry, mod);
849 else if (sym_is(symname, "__mod_sdio_device_table"))
850 do_table(symval, sym->st_size,
851 sizeof(struct sdio_device_id), "sdio",
852 do_sdio_entry, mod);
853 else if (sym_is(symname, "__mod_ssb_device_table"))
854 do_table(symval, sym->st_size,
855 sizeof(struct ssb_device_id), "ssb",
856 do_ssb_entry, mod);
857 else if (sym_is(symname, "__mod_virtio_device_table"))
858 do_table(symval, sym->st_size,
859 sizeof(struct virtio_device_id), "virtio",
860 do_virtio_entry, mod);
861 else if (sym_is(symname, "__mod_i2c_device_table"))
862 do_table(symval, sym->st_size,
863 sizeof(struct i2c_device_id), "i2c",
864 do_i2c_entry, mod);
865 else if (sym_is(symname, "__mod_spi_device_table"))
866 do_table(symval, sym->st_size,
867 sizeof(struct spi_device_id), "spi",
868 do_spi_entry, mod);
869 else if (sym_is(symname, "__mod_dmi_device_table"))
870 do_table(symval, sym->st_size,
871 sizeof(struct dmi_system_id), "dmi",
872 do_dmi_entry, mod);
873 else if (sym_is(symname, "__mod_platform_device_table"))
874 do_table(symval, sym->st_size,
875 sizeof(struct platform_device_id), "platform",
876 do_platform_entry, mod);
877 free(zeros);
880 /* Now add out buffered information to the generated C source */
881 void add_moddevtable(struct buffer *buf, struct module *mod)
883 buf_printf(buf, "\n");
884 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
885 free(mod->dev_table_buf.p);