modpost: use linker section to generate table.
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / scripts / mod / file2alias.c
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.
4 *
5 * Copyright 2002-2003 Rusty Russell, IBM Corporation
6 * 2003 Kai Germaschewski
7 *
8 *
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
11 */
12
13 #include "modpost.h"
14
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
29
30 #include <ctype.h>
31 #include <stdbool.h>
32
33 typedef uint32_t __u32;
34 typedef uint16_t __u16;
35 typedef unsigned char __u8;
36
37 /* Big exception to the "don't include kernel headers into userspace, which
38 * even potentially has different endianness and word sizes, since
39 * we handle those differences explicitly below */
40 #include "../../include/linux/mod_devicetable.h"
41
42 /* This array collects all instances that use the generic do_table */
43 struct devtable {
44 const char *device_id; /* name of table, __mod_<name>_device_table. */
45 unsigned long id_size;
46 void *function;
47 };
48
49 /* We construct a table of pointers in an ELF section (pointers generally
50 * go unpadded by gcc). ld creates boundary syms for us. */
51 extern struct devtable *__start___devtable[], *__stop___devtable[];
52 #define ___cat(a,b) a ## b
53 #define __cat(a,b) ___cat(a,b)
54
55 #if __GNUC__ == 3 && __GNUC_MINOR__ < 3
56 # define __used __attribute__((__unused__))
57 #else
58 # define __used __attribute__((__used__))
59 #endif
60
61 /* Add a table entry. We test function type matches while we're here. */
62 #define ADD_TO_DEVTABLE(device_id, type, function) \
63 static struct devtable __cat(devtable,__LINE__) = { \
64 device_id + 0*sizeof((function)((const char *)NULL, \
65 (type *)NULL, \
66 (char *)NULL)), \
67 sizeof(type), (function) }; \
68 static struct devtable *__attribute__((section("__devtable"))) \
69 __used __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
70
71 #define ADD(str, sep, cond, field) \
72 do { \
73 strcat(str, sep); \
74 if (cond) \
75 sprintf(str + strlen(str), \
76 sizeof(field) == 1 ? "%02X" : \
77 sizeof(field) == 2 ? "%04X" : \
78 sizeof(field) == 4 ? "%08X" : "", \
79 field); \
80 else \
81 sprintf(str + strlen(str), "*"); \
82 } while(0)
83
84 /* Always end in a wildcard, for future extension */
85 static inline void add_wildcard(char *str)
86 {
87 int len = strlen(str);
88
89 if (str[len - 1] != '*')
90 strcat(str + len, "*");
91 }
92
93 unsigned int cross_build = 0;
94 /**
95 * Check that sizeof(device_id type) are consistent with size of section
96 * in .o file. If in-consistent then userspace and kernel does not agree
97 * on actual size which is a bug.
98 * Also verify that the final entry in the table is all zeros.
99 * Ignore both checks if build host differ from target host and size differs.
100 **/
101 static void device_id_check(const char *modname, const char *device_id,
102 unsigned long size, unsigned long id_size,
103 void *symval)
104 {
105 int i;
106
107 if (size % id_size || size < id_size) {
108 if (cross_build != 0)
109 return;
110 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
111 "of the size of section __mod_%s_device_table=%lu.\n"
112 "Fix definition of struct %s_device_id "
113 "in mod_devicetable.h\n",
114 modname, device_id, id_size, device_id, size, device_id);
115 }
116 /* Verify last one is a terminator */
117 for (i = 0; i < id_size; i++ ) {
118 if (*(uint8_t*)(symval+size-id_size+i)) {
119 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
120 "The last of %lu is:\n",
121 modname, device_id, id_size, size / id_size);
122 for (i = 0; i < id_size; i++ )
123 fprintf(stderr,"0x%02x ",
124 *(uint8_t*)(symval+size-id_size+i) );
125 fprintf(stderr,"\n");
126 fatal("%s: struct %s_device_id is not terminated "
127 "with a NULL entry!\n", modname, device_id);
128 }
129 }
130 }
131
132 /* USB is special because the bcdDevice can be matched against a numeric range */
133 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
134 static void do_usb_entry(struct usb_device_id *id,
135 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
136 unsigned char range_lo, unsigned char range_hi,
137 unsigned char max, struct module *mod)
138 {
139 char alias[500];
140 strcpy(alias, "usb:");
141 ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
142 id->idVendor);
143 ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
144 id->idProduct);
145
146 strcat(alias, "d");
147 if (bcdDevice_initial_digits)
148 sprintf(alias + strlen(alias), "%0*X",
149 bcdDevice_initial_digits, bcdDevice_initial);
150 if (range_lo == range_hi)
151 sprintf(alias + strlen(alias), "%X", range_lo);
152 else if (range_lo > 0 || range_hi < max) {
153 if (range_lo > 0x9 || range_hi < 0xA)
154 sprintf(alias + strlen(alias),
155 "[%X-%X]",
156 range_lo,
157 range_hi);
158 else {
159 sprintf(alias + strlen(alias),
160 range_lo < 0x9 ? "[%X-9" : "[%X",
161 range_lo);
162 sprintf(alias + strlen(alias),
163 range_hi > 0xA ? "a-%X]" : "%X]",
164 range_lo);
165 }
166 }
167 if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
168 strcat(alias, "*");
169
170 ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
171 id->bDeviceClass);
172 ADD(alias, "dsc",
173 id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
174 id->bDeviceSubClass);
175 ADD(alias, "dp",
176 id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
177 id->bDeviceProtocol);
178 ADD(alias, "ic",
179 id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
180 id->bInterfaceClass);
181 ADD(alias, "isc",
182 id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
183 id->bInterfaceSubClass);
184 ADD(alias, "ip",
185 id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
186 id->bInterfaceProtocol);
187
188 add_wildcard(alias);
189 buf_printf(&mod->dev_table_buf,
190 "MODULE_ALIAS(\"%s\");\n", alias);
191 }
192
193 /* Handles increment/decrement of BCD formatted integers */
194 /* Returns the previous value, so it works like i++ or i-- */
195 static unsigned int incbcd(unsigned int *bcd,
196 int inc,
197 unsigned char max,
198 size_t chars)
199 {
200 unsigned int init = *bcd, i, j;
201 unsigned long long c, dec = 0;
202
203 /* If bcd is not in BCD format, just increment */
204 if (max > 0x9) {
205 *bcd += inc;
206 return init;
207 }
208
209 /* Convert BCD to Decimal */
210 for (i=0 ; i < chars ; i++) {
211 c = (*bcd >> (i << 2)) & 0xf;
212 c = c > 9 ? 9 : c; /* force to bcd just in case */
213 for (j=0 ; j < i ; j++)
214 c = c * 10;
215 dec += c;
216 }
217
218 /* Do our increment/decrement */
219 dec += inc;
220 *bcd = 0;
221
222 /* Convert back to BCD */
223 for (i=0 ; i < chars ; i++) {
224 for (c=1,j=0 ; j < i ; j++)
225 c = c * 10;
226 c = (dec / c) % 10;
227 *bcd += c << (i << 2);
228 }
229 return init;
230 }
231
232 static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
233 {
234 unsigned int devlo, devhi;
235 unsigned char chi, clo, max;
236 int ndigits;
237
238 id->match_flags = TO_NATIVE(id->match_flags);
239 id->idVendor = TO_NATIVE(id->idVendor);
240 id->idProduct = TO_NATIVE(id->idProduct);
241
242 devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
243 TO_NATIVE(id->bcdDevice_lo) : 0x0U;
244 devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
245 TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
246
247 /* Figure out if this entry is in bcd or hex format */
248 max = 0x9; /* Default to decimal format */
249 for (ndigits = 0 ; ndigits < sizeof(id->bcdDevice_lo) * 2 ; ndigits++) {
250 clo = (devlo >> (ndigits << 2)) & 0xf;
251 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
252 if (clo > max || chi > max) {
253 max = 0xf;
254 break;
255 }
256 }
257
258 /*
259 * Some modules (visor) have empty slots as placeholder for
260 * run-time specification that results in catch-all alias
261 */
262 if (!(id->idVendor | id->idProduct | id->bDeviceClass | id->bInterfaceClass))
263 return;
264
265 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
266 for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
267 clo = devlo & 0xf;
268 chi = devhi & 0xf;
269 if (chi > max) /* If we are in bcd mode, truncate if necessary */
270 chi = max;
271 devlo >>= 4;
272 devhi >>= 4;
273
274 if (devlo == devhi || !ndigits) {
275 do_usb_entry(id, devlo, ndigits, clo, chi, max, mod);
276 break;
277 }
278
279 if (clo > 0x0)
280 do_usb_entry(id,
281 incbcd(&devlo, 1, max,
282 sizeof(id->bcdDevice_lo) * 2),
283 ndigits, clo, max, max, mod);
284
285 if (chi < max)
286 do_usb_entry(id,
287 incbcd(&devhi, -1, max,
288 sizeof(id->bcdDevice_lo) * 2),
289 ndigits, 0x0, chi, max, mod);
290 }
291 }
292
293 static void do_usb_table(void *symval, unsigned long size,
294 struct module *mod)
295 {
296 unsigned int i;
297 const unsigned long id_size = sizeof(struct usb_device_id);
298
299 device_id_check(mod->name, "usb", size, id_size, symval);
300
301 /* Leave last one: it's the terminator. */
302 size -= id_size;
303
304 for (i = 0; i < size; i += id_size)
305 do_usb_entry_multi(symval + i, mod);
306 }
307
308 /* Looks like: hid:bNvNpN */
309 static int do_hid_entry(const char *filename,
310 struct hid_device_id *id, char *alias)
311 {
312 id->bus = TO_NATIVE(id->bus);
313 id->vendor = TO_NATIVE(id->vendor);
314 id->product = TO_NATIVE(id->product);
315
316 sprintf(alias, "hid:b%04X", id->bus);
317 ADD(alias, "v", id->vendor != HID_ANY_ID, id->vendor);
318 ADD(alias, "p", id->product != HID_ANY_ID, id->product);
319
320 return 1;
321 }
322 ADD_TO_DEVTABLE("hid", struct hid_device_id, do_hid_entry);
323
324 /* Looks like: ieee1394:venNmoNspNverN */
325 static int do_ieee1394_entry(const char *filename,
326 struct ieee1394_device_id *id, char *alias)
327 {
328 id->match_flags = TO_NATIVE(id->match_flags);
329 id->vendor_id = TO_NATIVE(id->vendor_id);
330 id->model_id = TO_NATIVE(id->model_id);
331 id->specifier_id = TO_NATIVE(id->specifier_id);
332 id->version = TO_NATIVE(id->version);
333
334 strcpy(alias, "ieee1394:");
335 ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
336 id->vendor_id);
337 ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
338 id->model_id);
339 ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
340 id->specifier_id);
341 ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
342 id->version);
343
344 add_wildcard(alias);
345 return 1;
346 }
347 ADD_TO_DEVTABLE("ieee1394", struct ieee1394_device_id, do_ieee1394_entry);
348
349 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
350 static int do_pci_entry(const char *filename,
351 struct pci_device_id *id, char *alias)
352 {
353 /* Class field can be divided into these three. */
354 unsigned char baseclass, subclass, interface,
355 baseclass_mask, subclass_mask, interface_mask;
356
357 id->vendor = TO_NATIVE(id->vendor);
358 id->device = TO_NATIVE(id->device);
359 id->subvendor = TO_NATIVE(id->subvendor);
360 id->subdevice = TO_NATIVE(id->subdevice);
361 id->class = TO_NATIVE(id->class);
362 id->class_mask = TO_NATIVE(id->class_mask);
363
364 strcpy(alias, "pci:");
365 ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
366 ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
367 ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
368 ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
369
370 baseclass = (id->class) >> 16;
371 baseclass_mask = (id->class_mask) >> 16;
372 subclass = (id->class) >> 8;
373 subclass_mask = (id->class_mask) >> 8;
374 interface = id->class;
375 interface_mask = id->class_mask;
376
377 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
378 || (subclass_mask != 0 && subclass_mask != 0xFF)
379 || (interface_mask != 0 && interface_mask != 0xFF)) {
380 warn("Can't handle masks in %s:%04X\n",
381 filename, id->class_mask);
382 return 0;
383 }
384
385 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
386 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
387 ADD(alias, "i", interface_mask == 0xFF, interface);
388 add_wildcard(alias);
389 return 1;
390 }
391 ADD_TO_DEVTABLE("pci", struct pci_device_id, do_pci_entry);
392
393 /* looks like: "ccw:tNmNdtNdmN" */
394 static int do_ccw_entry(const char *filename,
395 struct ccw_device_id *id, char *alias)
396 {
397 id->match_flags = TO_NATIVE(id->match_flags);
398 id->cu_type = TO_NATIVE(id->cu_type);
399 id->cu_model = TO_NATIVE(id->cu_model);
400 id->dev_type = TO_NATIVE(id->dev_type);
401 id->dev_model = TO_NATIVE(id->dev_model);
402
403 strcpy(alias, "ccw:");
404 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
405 id->cu_type);
406 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
407 id->cu_model);
408 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
409 id->dev_type);
410 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
411 id->dev_model);
412 add_wildcard(alias);
413 return 1;
414 }
415 ADD_TO_DEVTABLE("ccw", struct ccw_device_id, do_ccw_entry);
416
417 /* looks like: "ap:tN" */
418 static int do_ap_entry(const char *filename,
419 struct ap_device_id *id, char *alias)
420 {
421 sprintf(alias, "ap:t%02X*", id->dev_type);
422 return 1;
423 }
424 ADD_TO_DEVTABLE("ap", struct ap_device_id, do_ap_entry);
425
426 /* looks like: "css:tN" */
427 static int do_css_entry(const char *filename,
428 struct css_device_id *id, char *alias)
429 {
430 sprintf(alias, "css:t%01X", id->type);
431 return 1;
432 }
433 ADD_TO_DEVTABLE("css", struct css_device_id, do_css_entry);
434
435 /* Looks like: "serio:tyNprNidNexN" */
436 static int do_serio_entry(const char *filename,
437 struct serio_device_id *id, char *alias)
438 {
439 id->type = TO_NATIVE(id->type);
440 id->proto = TO_NATIVE(id->proto);
441 id->id = TO_NATIVE(id->id);
442 id->extra = TO_NATIVE(id->extra);
443
444 strcpy(alias, "serio:");
445 ADD(alias, "ty", id->type != SERIO_ANY, id->type);
446 ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
447 ADD(alias, "id", id->id != SERIO_ANY, id->id);
448 ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
449
450 add_wildcard(alias);
451 return 1;
452 }
453 ADD_TO_DEVTABLE("serio", struct serio_device_id, do_serio_entry);
454
455 /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
456 static int do_acpi_entry(const char *filename,
457 struct acpi_device_id *id, char *alias)
458 {
459 sprintf(alias, "acpi*:%s:*", id->id);
460 return 1;
461 }
462 ADD_TO_DEVTABLE("acpi", struct acpi_device_id, do_acpi_entry);
463
464 /* looks like: "pnp:dD" */
465 static void do_pnp_device_entry(void *symval, unsigned long size,
466 struct module *mod)
467 {
468 const unsigned long id_size = sizeof(struct pnp_device_id);
469 const unsigned int count = (size / id_size)-1;
470 const struct pnp_device_id *devs = symval;
471 unsigned int i;
472
473 device_id_check(mod->name, "pnp", size, id_size, symval);
474
475 for (i = 0; i < count; i++) {
476 const char *id = (char *)devs[i].id;
477 char acpi_id[sizeof(devs[0].id)];
478 int j;
479
480 buf_printf(&mod->dev_table_buf,
481 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
482
483 /* fix broken pnp bus lowercasing */
484 for (j = 0; j < sizeof(acpi_id); j++)
485 acpi_id[j] = toupper(id[j]);
486 buf_printf(&mod->dev_table_buf,
487 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
488 }
489 }
490
491 /* looks like: "pnp:dD" for every device of the card */
492 static void do_pnp_card_entries(void *symval, unsigned long size,
493 struct module *mod)
494 {
495 const unsigned long id_size = sizeof(struct pnp_card_device_id);
496 const unsigned int count = (size / id_size)-1;
497 const struct pnp_card_device_id *cards = symval;
498 unsigned int i;
499
500 device_id_check(mod->name, "pnp", size, id_size, symval);
501
502 for (i = 0; i < count; i++) {
503 unsigned int j;
504 const struct pnp_card_device_id *card = &cards[i];
505
506 for (j = 0; j < PNP_MAX_DEVICES; j++) {
507 const char *id = (char *)card->devs[j].id;
508 int i2, j2;
509 int dup = 0;
510
511 if (!id[0])
512 break;
513
514 /* find duplicate, already added value */
515 for (i2 = 0; i2 < i && !dup; i2++) {
516 const struct pnp_card_device_id *card2 = &cards[i2];
517
518 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
519 const char *id2 = (char *)card2->devs[j2].id;
520
521 if (!id2[0])
522 break;
523
524 if (!strcmp(id, id2)) {
525 dup = 1;
526 break;
527 }
528 }
529 }
530
531 /* add an individual alias for every device entry */
532 if (!dup) {
533 char acpi_id[sizeof(card->devs[0].id)];
534 int k;
535
536 buf_printf(&mod->dev_table_buf,
537 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
538
539 /* fix broken pnp bus lowercasing */
540 for (k = 0; k < sizeof(acpi_id); k++)
541 acpi_id[k] = toupper(id[k]);
542 buf_printf(&mod->dev_table_buf,
543 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
544 }
545 }
546 }
547 }
548
549 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
550 static int do_pcmcia_entry(const char *filename,
551 struct pcmcia_device_id *id, char *alias)
552 {
553 unsigned int i;
554
555 id->match_flags = TO_NATIVE(id->match_flags);
556 id->manf_id = TO_NATIVE(id->manf_id);
557 id->card_id = TO_NATIVE(id->card_id);
558 id->func_id = TO_NATIVE(id->func_id);
559 id->function = TO_NATIVE(id->function);
560 id->device_no = TO_NATIVE(id->device_no);
561
562 for (i=0; i<4; i++) {
563 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
564 }
565
566 strcpy(alias, "pcmcia:");
567 ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
568 id->manf_id);
569 ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
570 id->card_id);
571 ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
572 id->func_id);
573 ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
574 id->function);
575 ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
576 id->device_no);
577 ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
578 ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
579 ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
580 ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
581
582 add_wildcard(alias);
583 return 1;
584 }
585 ADD_TO_DEVTABLE("pcmcia", struct pcmcia_device_id, do_pcmcia_entry);
586
587 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
588 {
589 int len;
590 char *tmp;
591 len = sprintf (alias, "of:N%sT%s",
592 of->name[0] ? of->name : "*",
593 of->type[0] ? of->type : "*");
594
595 if (of->compatible[0])
596 sprintf (&alias[len], "%sC%s",
597 of->type[0] ? "*" : "",
598 of->compatible);
599
600 /* Replace all whitespace with underscores */
601 for (tmp = alias; tmp && *tmp; tmp++)
602 if (isspace (*tmp))
603 *tmp = '_';
604
605 add_wildcard(alias);
606 return 1;
607 }
608 ADD_TO_DEVTABLE("of", struct of_device_id, do_of_entry);
609
610 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
611 char *alias)
612 {
613 char *tmp;
614
615 sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
616 vio->compat[0] ? vio->compat : "*");
617
618 /* Replace all whitespace with underscores */
619 for (tmp = alias; tmp && *tmp; tmp++)
620 if (isspace (*tmp))
621 *tmp = '_';
622
623 add_wildcard(alias);
624 return 1;
625 }
626 ADD_TO_DEVTABLE("vio", struct vio_device_id, do_vio_entry);
627
628 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
629
630 static void do_input(char *alias,
631 kernel_ulong_t *arr, unsigned int min, unsigned int max)
632 {
633 unsigned int i;
634
635 for (i = min; i < max; i++)
636 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
637 sprintf(alias + strlen(alias), "%X,*", i);
638 }
639
640 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
641 static int do_input_entry(const char *filename, struct input_device_id *id,
642 char *alias)
643 {
644 sprintf(alias, "input:");
645
646 ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
647 ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
648 ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
649 ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
650
651 sprintf(alias + strlen(alias), "-e*");
652 if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
653 do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX);
654 sprintf(alias + strlen(alias), "k*");
655 if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
656 do_input(alias, id->keybit,
657 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
658 INPUT_DEVICE_ID_KEY_MAX);
659 sprintf(alias + strlen(alias), "r*");
660 if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
661 do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX);
662 sprintf(alias + strlen(alias), "a*");
663 if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
664 do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
665 sprintf(alias + strlen(alias), "m*");
666 if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
667 do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
668 sprintf(alias + strlen(alias), "l*");
669 if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
670 do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
671 sprintf(alias + strlen(alias), "s*");
672 if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
673 do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
674 sprintf(alias + strlen(alias), "f*");
675 if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
676 do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
677 sprintf(alias + strlen(alias), "w*");
678 if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
679 do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX);
680 return 1;
681 }
682 ADD_TO_DEVTABLE("input", struct input_device_id, do_input_entry);
683
684 static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
685 char *alias)
686 {
687 if (eisa->sig[0])
688 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
689 else
690 strcat(alias, "*");
691 return 1;
692 }
693 ADD_TO_DEVTABLE("eisa", struct eisa_device_id, do_eisa_entry);
694
695 /* Looks like: parisc:tNhvNrevNsvN */
696 static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
697 char *alias)
698 {
699 id->hw_type = TO_NATIVE(id->hw_type);
700 id->hversion = TO_NATIVE(id->hversion);
701 id->hversion_rev = TO_NATIVE(id->hversion_rev);
702 id->sversion = TO_NATIVE(id->sversion);
703
704 strcpy(alias, "parisc:");
705 ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type);
706 ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion);
707 ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev);
708 ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion);
709
710 add_wildcard(alias);
711 return 1;
712 }
713 ADD_TO_DEVTABLE("parisc", struct parisc_device_id, do_parisc_entry);
714
715 /* Looks like: sdio:cNvNdN. */
716 static int do_sdio_entry(const char *filename,
717 struct sdio_device_id *id, char *alias)
718 {
719 id->class = TO_NATIVE(id->class);
720 id->vendor = TO_NATIVE(id->vendor);
721 id->device = TO_NATIVE(id->device);
722
723 strcpy(alias, "sdio:");
724 ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class);
725 ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor);
726 ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device);
727 add_wildcard(alias);
728 return 1;
729 }
730 ADD_TO_DEVTABLE("sdio", struct sdio_device_id, do_sdio_entry);
731
732 /* Looks like: ssb:vNidNrevN. */
733 static int do_ssb_entry(const char *filename,
734 struct ssb_device_id *id, char *alias)
735 {
736 id->vendor = TO_NATIVE(id->vendor);
737 id->coreid = TO_NATIVE(id->coreid);
738 id->revision = TO_NATIVE(id->revision);
739
740 strcpy(alias, "ssb:");
741 ADD(alias, "v", id->vendor != SSB_ANY_VENDOR, id->vendor);
742 ADD(alias, "id", id->coreid != SSB_ANY_ID, id->coreid);
743 ADD(alias, "rev", id->revision != SSB_ANY_REV, id->revision);
744 add_wildcard(alias);
745 return 1;
746 }
747 ADD_TO_DEVTABLE("ssb", struct ssb_device_id, do_ssb_entry);
748
749 /* Looks like: bcma:mNidNrevNclN. */
750 static int do_bcma_entry(const char *filename,
751 struct bcma_device_id *id, char *alias)
752 {
753 id->manuf = TO_NATIVE(id->manuf);
754 id->id = TO_NATIVE(id->id);
755 id->rev = TO_NATIVE(id->rev);
756 id->class = TO_NATIVE(id->class);
757
758 strcpy(alias, "bcma:");
759 ADD(alias, "m", id->manuf != BCMA_ANY_MANUF, id->manuf);
760 ADD(alias, "id", id->id != BCMA_ANY_ID, id->id);
761 ADD(alias, "rev", id->rev != BCMA_ANY_REV, id->rev);
762 ADD(alias, "cl", id->class != BCMA_ANY_CLASS, id->class);
763 add_wildcard(alias);
764 return 1;
765 }
766 ADD_TO_DEVTABLE("bcma", struct bcma_device_id, do_bcma_entry);
767
768 /* Looks like: virtio:dNvN */
769 static int do_virtio_entry(const char *filename, struct virtio_device_id *id,
770 char *alias)
771 {
772 id->device = TO_NATIVE(id->device);
773 id->vendor = TO_NATIVE(id->vendor);
774
775 strcpy(alias, "virtio:");
776 ADD(alias, "d", id->device != VIRTIO_DEV_ANY_ID, id->device);
777 ADD(alias, "v", id->vendor != VIRTIO_DEV_ANY_ID, id->vendor);
778
779 add_wildcard(alias);
780 return 1;
781 }
782 ADD_TO_DEVTABLE("virtio", struct virtio_device_id, do_virtio_entry);
783
784 /*
785 * Looks like: vmbus:guid
786 * Each byte of the guid will be represented by two hex characters
787 * in the name.
788 */
789
790 static int do_vmbus_entry(const char *filename, struct hv_vmbus_device_id *id,
791 char *alias)
792 {
793 int i;
794 char guid_name[((sizeof(id->guid) + 1)) * 2];
795
796 for (i = 0; i < (sizeof(id->guid) * 2); i += 2)
797 sprintf(&guid_name[i], "%02x", id->guid[i/2]);
798
799 strcpy(alias, "vmbus:");
800 strcat(alias, guid_name);
801
802 return 1;
803 }
804 ADD_TO_DEVTABLE("vmbus", struct hv_vmbus_device_id, do_vmbus_entry);
805
806 /* Looks like: i2c:S */
807 static int do_i2c_entry(const char *filename, struct i2c_device_id *id,
808 char *alias)
809 {
810 sprintf(alias, I2C_MODULE_PREFIX "%s", id->name);
811
812 return 1;
813 }
814 ADD_TO_DEVTABLE("i2c", struct i2c_device_id, do_i2c_entry);
815
816 /* Looks like: spi:S */
817 static int do_spi_entry(const char *filename, struct spi_device_id *id,
818 char *alias)
819 {
820 sprintf(alias, SPI_MODULE_PREFIX "%s", id->name);
821
822 return 1;
823 }
824 ADD_TO_DEVTABLE("spi", struct spi_device_id, do_spi_entry);
825
826 static const struct dmifield {
827 const char *prefix;
828 int field;
829 } dmi_fields[] = {
830 { "bvn", DMI_BIOS_VENDOR },
831 { "bvr", DMI_BIOS_VERSION },
832 { "bd", DMI_BIOS_DATE },
833 { "svn", DMI_SYS_VENDOR },
834 { "pn", DMI_PRODUCT_NAME },
835 { "pvr", DMI_PRODUCT_VERSION },
836 { "rvn", DMI_BOARD_VENDOR },
837 { "rn", DMI_BOARD_NAME },
838 { "rvr", DMI_BOARD_VERSION },
839 { "cvn", DMI_CHASSIS_VENDOR },
840 { "ct", DMI_CHASSIS_TYPE },
841 { "cvr", DMI_CHASSIS_VERSION },
842 { NULL, DMI_NONE }
843 };
844
845 static void dmi_ascii_filter(char *d, const char *s)
846 {
847 /* Filter out characters we don't want to see in the modalias string */
848 for (; *s; s++)
849 if (*s > ' ' && *s < 127 && *s != ':')
850 *(d++) = *s;
851
852 *d = 0;
853 }
854
855
856 static int do_dmi_entry(const char *filename, struct dmi_system_id *id,
857 char *alias)
858 {
859 int i, j;
860
861 sprintf(alias, "dmi*");
862
863 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
864 for (j = 0; j < 4; j++) {
865 if (id->matches[j].slot &&
866 id->matches[j].slot == dmi_fields[i].field) {
867 sprintf(alias + strlen(alias), ":%s*",
868 dmi_fields[i].prefix);
869 dmi_ascii_filter(alias + strlen(alias),
870 id->matches[j].substr);
871 strcat(alias, "*");
872 }
873 }
874 }
875
876 strcat(alias, ":");
877 return 1;
878 }
879 ADD_TO_DEVTABLE("dmi", struct dmi_system_id, do_dmi_entry);
880
881 static int do_platform_entry(const char *filename,
882 struct platform_device_id *id, char *alias)
883 {
884 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", id->name);
885 return 1;
886 }
887 ADD_TO_DEVTABLE("platform", struct platform_device_id, do_platform_entry);
888
889 static int do_mdio_entry(const char *filename,
890 struct mdio_device_id *id, char *alias)
891 {
892 int i;
893
894 alias += sprintf(alias, MDIO_MODULE_PREFIX);
895
896 for (i = 0; i < 32; i++) {
897 if (!((id->phy_id_mask >> (31-i)) & 1))
898 *(alias++) = '?';
899 else if ((id->phy_id >> (31-i)) & 1)
900 *(alias++) = '1';
901 else
902 *(alias++) = '0';
903 }
904
905 /* Terminate the string */
906 *alias = 0;
907
908 return 1;
909 }
910 ADD_TO_DEVTABLE("mdio", struct mdio_device_id, do_mdio_entry);
911
912 /* Looks like: zorro:iN. */
913 static int do_zorro_entry(const char *filename, struct zorro_device_id *id,
914 char *alias)
915 {
916 id->id = TO_NATIVE(id->id);
917 strcpy(alias, "zorro:");
918 ADD(alias, "i", id->id != ZORRO_WILDCARD, id->id);
919 return 1;
920 }
921 ADD_TO_DEVTABLE("zorro", struct zorro_device_id, do_zorro_entry);
922
923 /* looks like: "pnp:dD" */
924 static int do_isapnp_entry(const char *filename,
925 struct isapnp_device_id *id, char *alias)
926 {
927 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
928 'A' + ((id->vendor >> 2) & 0x3f) - 1,
929 'A' + (((id->vendor & 3) << 3) | ((id->vendor >> 13) & 7)) - 1,
930 'A' + ((id->vendor >> 8) & 0x1f) - 1,
931 (id->function >> 4) & 0x0f, id->function & 0x0f,
932 (id->function >> 12) & 0x0f, (id->function >> 8) & 0x0f);
933 return 1;
934 }
935 ADD_TO_DEVTABLE("isa", struct isapnp_device_id, do_isapnp_entry);
936
937 /*
938 * Append a match expression for a single masked hex digit.
939 * outp points to a pointer to the character at which to append.
940 * *outp is updated on return to point just after the appended text,
941 * to facilitate further appending.
942 */
943 static void append_nibble_mask(char **outp,
944 unsigned int nibble, unsigned int mask)
945 {
946 char *p = *outp;
947 unsigned int i;
948
949 switch (mask) {
950 case 0:
951 *p++ = '?';
952 break;
953
954 case 0xf:
955 p += sprintf(p, "%X", nibble);
956 break;
957
958 default:
959 /*
960 * Dumbly emit a match pattern for all possible matching
961 * digits. This could be improved in some cases using ranges,
962 * but it has the advantage of being trivially correct, and is
963 * often optimal.
964 */
965 *p++ = '[';
966 for (i = 0; i < 0x10; i++)
967 if ((i & mask) == nibble)
968 p += sprintf(p, "%X", i);
969 *p++ = ']';
970 }
971
972 /* Ensure that the string remains NUL-terminated: */
973 *p = '\0';
974
975 /* Advance the caller's end-of-string pointer: */
976 *outp = p;
977 }
978
979 /*
980 * looks like: "amba:dN"
981 *
982 * N is exactly 8 digits, where each is an upper-case hex digit, or
983 * a ? or [] pattern matching exactly one digit.
984 */
985 static int do_amba_entry(const char *filename,
986 struct amba_id *id, char *alias)
987 {
988 unsigned int digit;
989 char *p = alias;
990
991 if ((id->id & id->mask) != id->id)
992 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
993 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
994 filename, id->id, id->mask);
995
996 p += sprintf(alias, "amba:d");
997 for (digit = 0; digit < 8; digit++)
998 append_nibble_mask(&p,
999 (id->id >> (4 * (7 - digit))) & 0xf,
1000 (id->mask >> (4 * (7 - digit))) & 0xf);
1001
1002 return 1;
1003 }
1004 ADD_TO_DEVTABLE("amba", struct amba_id, do_amba_entry);
1005
1006 /* Does namelen bytes of name exactly match the symbol? */
1007 static bool sym_is(const char *name, unsigned namelen, const char *symbol)
1008 {
1009 if (namelen != strlen(symbol))
1010 return false;
1011
1012 return memcmp(name, symbol, namelen) == 0;
1013 }
1014
1015 static void do_table(void *symval, unsigned long size,
1016 unsigned long id_size,
1017 const char *device_id,
1018 void *function,
1019 struct module *mod)
1020 {
1021 unsigned int i;
1022 char alias[500];
1023 int (*do_entry)(const char *, void *entry, char *alias) = function;
1024
1025 device_id_check(mod->name, device_id, size, id_size, symval);
1026 /* Leave last one: it's the terminator. */
1027 size -= id_size;
1028
1029 for (i = 0; i < size; i += id_size) {
1030 if (do_entry(mod->name, symval+i, alias)) {
1031 buf_printf(&mod->dev_table_buf,
1032 "MODULE_ALIAS(\"%s\");\n", alias);
1033 }
1034 }
1035 }
1036
1037 /* Create MODULE_ALIAS() statements.
1038 * At this time, we cannot write the actual output C source yet,
1039 * so we write into the mod->dev_table_buf buffer. */
1040 void handle_moddevtable(struct module *mod, struct elf_info *info,
1041 Elf_Sym *sym, const char *symname)
1042 {
1043 void *symval;
1044 char *zeros = NULL;
1045 const char *name;
1046 unsigned int namelen;
1047
1048 /* We're looking for a section relative symbol */
1049 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
1050 return;
1051
1052 /* All our symbols are of form <prefix>__mod_XXX_device_table. */
1053 name = strstr(symname, "__mod_");
1054 if (!name)
1055 return;
1056 name += strlen("__mod_");
1057 namelen = strlen(name);
1058 if (namelen < strlen("_device_table"))
1059 return;
1060 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1061 return;
1062 namelen -= strlen("_device_table");
1063
1064 /* Handle all-NULL symbols allocated into .bss */
1065 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
1066 zeros = calloc(1, sym->st_size);
1067 symval = zeros;
1068 } else {
1069 symval = (void *)info->hdr
1070 + info->sechdrs[get_secindex(info, sym)].sh_offset
1071 + sym->st_value;
1072 }
1073
1074 /* First handle the "special" cases */
1075 if (sym_is(name, namelen, "usb"))
1076 do_usb_table(symval, sym->st_size, mod);
1077 else if (sym_is(name, namelen, "pnp"))
1078 do_pnp_device_entry(symval, sym->st_size, mod);
1079 else if (sym_is(name, namelen, "pnp_card"))
1080 do_pnp_card_entries(symval, sym->st_size, mod);
1081 else {
1082 struct devtable **p;
1083
1084 for (p = __start___devtable; p < __stop___devtable; p++) {
1085 if (sym_is(name, namelen, (*p)->device_id)) {
1086 do_table(symval, sym->st_size, (*p)->id_size,
1087 (*p)->device_id, (*p)->function, mod);
1088 break;
1089 }
1090 }
1091 }
1092 free(zeros);
1093 }
1094
1095 /* Now add out buffered information to the generated C source */
1096 void add_moddevtable(struct buffer *buf, struct module *mod)
1097 {
1098 buf_printf(buf, "\n");
1099 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1100 free(mod->dev_table_buf.p);
1101 }