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.
14 #include "devicetable-offsets.h"
16 /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
17 * use either stdint.h or inttypes.h for the rest. */
18 #if KERNEL_ELFCLASS == ELFCLASS32
19 typedef Elf32_Addr kernel_ulong_t
;
20 #define BITS_PER_LONG 32
22 typedef Elf64_Addr kernel_ulong_t
;
23 #define BITS_PER_LONG 64
34 typedef uint32_t __u32
;
35 typedef uint16_t __u16
;
36 typedef unsigned char __u8
;
38 /* Big exception to the "don't include kernel headers into userspace, which
39 * even potentially has different endianness and word sizes, since
40 * we handle those differences explicitly below */
41 #include "../../include/linux/mod_devicetable.h"
43 /* This array collects all instances that use the generic do_table */
45 const char *device_id
; /* name of table, __mod_<name>_device_table. */
46 unsigned long id_size
;
50 #define ___cat(a,b) a ## b
51 #define __cat(a,b) ___cat(a,b)
53 /* we need some special handling for this host tool running eventually on
54 * Darwin. The Mach-O section handling is a bit different than ELF section
55 * handling. The differnces in detail are:
56 * a) we have segments which have sections
57 * b) we need a API call to get the respective section symbols */
59 #include <mach-o/getsect.h>
61 #define INIT_SECTION(name) do { \
62 unsigned long name ## _len; \
63 char *__cat(pstart_,name) = getsectdata("__TEXT", \
64 #name, &__cat(name,_len)); \
65 char *__cat(pstop_,name) = __cat(pstart_,name) + \
67 __cat(__start_,name) = (void *)__cat(pstart_,name); \
68 __cat(__stop_,name) = (void *)__cat(pstop_,name); \
70 #define SECTION(name) __attribute__((section("__TEXT, " #name)))
72 struct devtable
**__start___devtable
, **__stop___devtable
;
74 #define INIT_SECTION(name) /* no-op for ELF */
75 #define SECTION(name) __attribute__((section(#name)))
77 /* We construct a table of pointers in an ELF section (pointers generally
78 * go unpadded by gcc). ld creates boundary syms for us. */
79 extern struct devtable
*__start___devtable
[], *__stop___devtable
[];
82 #if __GNUC__ == 3 && __GNUC_MINOR__ < 3
83 # define __used __attribute__((__unused__))
85 # define __used __attribute__((__used__))
88 /* Define a variable f that holds the value of field f of struct devid
91 #define DEF_FIELD(m, devid, f) \
92 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
93 /* Define a variable f that holds the address of field f of struct devid
94 * based at address m. Due to the way typeof works, for a field of type
95 * T[N] the variable has type T(*)[N], _not_ T*.
97 #define DEF_FIELD_ADDR(m, devid, f) \
98 typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
100 /* Add a table entry. We test function type matches while we're here. */
101 #define ADD_TO_DEVTABLE(device_id, type, function) \
102 static struct devtable __cat(devtable,__LINE__) = { \
103 device_id + 0*sizeof((function)((const char *)NULL, \
106 SIZE_##type, (function) }; \
107 static struct devtable *SECTION(__devtable) __used \
108 __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
110 #define ADD(str, sep, cond, field) \
114 sprintf(str + strlen(str), \
115 sizeof(field) == 1 ? "%02X" : \
116 sizeof(field) == 2 ? "%04X" : \
117 sizeof(field) == 4 ? "%08X" : "", \
120 sprintf(str + strlen(str), "*"); \
123 /* Always end in a wildcard, for future extension */
124 static inline void add_wildcard(char *str
)
126 int len
= strlen(str
);
128 if (str
[len
- 1] != '*')
129 strcat(str
+ len
, "*");
133 * Check that sizeof(device_id type) are consistent with size of section
134 * in .o file. If in-consistent then userspace and kernel does not agree
135 * on actual size which is a bug.
136 * Also verify that the final entry in the table is all zeros.
137 * Ignore both checks if build host differ from target host and size differs.
139 static void device_id_check(const char *modname
, const char *device_id
,
140 unsigned long size
, unsigned long id_size
,
145 if (size
% id_size
|| size
< id_size
) {
146 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
147 "of the size of section __mod_%s_device_table=%lu.\n"
148 "Fix definition of struct %s_device_id "
149 "in mod_devicetable.h\n",
150 modname
, device_id
, id_size
, device_id
, size
, device_id
);
152 /* Verify last one is a terminator */
153 for (i
= 0; i
< id_size
; i
++ ) {
154 if (*(uint8_t*)(symval
+size
-id_size
+i
)) {
155 fprintf(stderr
,"%s: struct %s_device_id is %lu bytes. "
156 "The last of %lu is:\n",
157 modname
, device_id
, id_size
, size
/ id_size
);
158 for (i
= 0; i
< id_size
; i
++ )
159 fprintf(stderr
,"0x%02x ",
160 *(uint8_t*)(symval
+size
-id_size
+i
) );
161 fprintf(stderr
,"\n");
162 fatal("%s: struct %s_device_id is not terminated "
163 "with a NULL entry!\n", modname
, device_id
);
168 /* USB is special because the bcdDevice can be matched against a numeric range */
169 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
170 static void do_usb_entry(void *symval
,
171 unsigned int bcdDevice_initial
, int bcdDevice_initial_digits
,
172 unsigned char range_lo
, unsigned char range_hi
,
173 unsigned char max
, struct module
*mod
)
176 DEF_FIELD(symval
, usb_device_id
, match_flags
);
177 DEF_FIELD(symval
, usb_device_id
, idVendor
);
178 DEF_FIELD(symval
, usb_device_id
, idProduct
);
179 DEF_FIELD(symval
, usb_device_id
, bcdDevice_lo
);
180 DEF_FIELD(symval
, usb_device_id
, bDeviceClass
);
181 DEF_FIELD(symval
, usb_device_id
, bDeviceSubClass
);
182 DEF_FIELD(symval
, usb_device_id
, bDeviceProtocol
);
183 DEF_FIELD(symval
, usb_device_id
, bInterfaceClass
);
184 DEF_FIELD(symval
, usb_device_id
, bInterfaceSubClass
);
185 DEF_FIELD(symval
, usb_device_id
, bInterfaceProtocol
);
186 DEF_FIELD(symval
, usb_device_id
, bInterfaceNumber
);
188 strcpy(alias
, "usb:");
189 ADD(alias
, "v", match_flags
&USB_DEVICE_ID_MATCH_VENDOR
,
191 ADD(alias
, "p", match_flags
&USB_DEVICE_ID_MATCH_PRODUCT
,
195 if (bcdDevice_initial_digits
)
196 sprintf(alias
+ strlen(alias
), "%0*X",
197 bcdDevice_initial_digits
, bcdDevice_initial
);
198 if (range_lo
== range_hi
)
199 sprintf(alias
+ strlen(alias
), "%X", range_lo
);
200 else if (range_lo
> 0 || range_hi
< max
) {
201 if (range_lo
> 0x9 || range_hi
< 0xA)
202 sprintf(alias
+ strlen(alias
),
207 sprintf(alias
+ strlen(alias
),
208 range_lo
< 0x9 ? "[%X-9" : "[%X",
210 sprintf(alias
+ strlen(alias
),
211 range_hi
> 0xA ? "a-%X]" : "%X]",
215 if (bcdDevice_initial_digits
< (sizeof(bcdDevice_lo
) * 2 - 1))
218 ADD(alias
, "dc", match_flags
&USB_DEVICE_ID_MATCH_DEV_CLASS
,
220 ADD(alias
, "dsc", match_flags
&USB_DEVICE_ID_MATCH_DEV_SUBCLASS
,
222 ADD(alias
, "dp", match_flags
&USB_DEVICE_ID_MATCH_DEV_PROTOCOL
,
224 ADD(alias
, "ic", match_flags
&USB_DEVICE_ID_MATCH_INT_CLASS
,
226 ADD(alias
, "isc", match_flags
&USB_DEVICE_ID_MATCH_INT_SUBCLASS
,
228 ADD(alias
, "ip", match_flags
&USB_DEVICE_ID_MATCH_INT_PROTOCOL
,
230 ADD(alias
, "in", match_flags
&USB_DEVICE_ID_MATCH_INT_NUMBER
,
234 buf_printf(&mod
->dev_table_buf
,
235 "MODULE_ALIAS(\"%s\");\n", alias
);
238 /* Handles increment/decrement of BCD formatted integers */
239 /* Returns the previous value, so it works like i++ or i-- */
240 static unsigned int incbcd(unsigned int *bcd
,
245 unsigned int init
= *bcd
, i
, j
;
246 unsigned long long c
, dec
= 0;
248 /* If bcd is not in BCD format, just increment */
254 /* Convert BCD to Decimal */
255 for (i
=0 ; i
< chars
; i
++) {
256 c
= (*bcd
>> (i
<< 2)) & 0xf;
257 c
= c
> 9 ? 9 : c
; /* force to bcd just in case */
258 for (j
=0 ; j
< i
; j
++)
263 /* Do our increment/decrement */
267 /* Convert back to BCD */
268 for (i
=0 ; i
< chars
; i
++) {
269 for (c
=1,j
=0 ; j
< i
; j
++)
272 *bcd
+= c
<< (i
<< 2);
277 static void do_usb_entry_multi(void *symval
, struct module
*mod
)
279 unsigned int devlo
, devhi
;
280 unsigned char chi
, clo
, max
;
283 DEF_FIELD(symval
, usb_device_id
, match_flags
);
284 DEF_FIELD(symval
, usb_device_id
, idVendor
);
285 DEF_FIELD(symval
, usb_device_id
, idProduct
);
286 DEF_FIELD(symval
, usb_device_id
, bcdDevice_lo
);
287 DEF_FIELD(symval
, usb_device_id
, bcdDevice_hi
);
288 DEF_FIELD(symval
, usb_device_id
, bDeviceClass
);
289 DEF_FIELD(symval
, usb_device_id
, bInterfaceClass
);
291 devlo
= match_flags
& USB_DEVICE_ID_MATCH_DEV_LO
?
293 devhi
= match_flags
& USB_DEVICE_ID_MATCH_DEV_HI
?
294 bcdDevice_hi
: ~0x0U
;
296 /* Figure out if this entry is in bcd or hex format */
297 max
= 0x9; /* Default to decimal format */
298 for (ndigits
= 0 ; ndigits
< sizeof(bcdDevice_lo
) * 2 ; ndigits
++) {
299 clo
= (devlo
>> (ndigits
<< 2)) & 0xf;
300 chi
= ((devhi
> 0x9999 ? 0x9999 : devhi
) >> (ndigits
<< 2)) & 0xf;
301 if (clo
> max
|| chi
> max
) {
308 * Some modules (visor) have empty slots as placeholder for
309 * run-time specification that results in catch-all alias
311 if (!(idVendor
| idProduct
| bDeviceClass
| bInterfaceClass
))
314 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
315 for (ndigits
= sizeof(bcdDevice_lo
) * 2 - 1; devlo
<= devhi
; ndigits
--) {
318 if (chi
> max
) /* If we are in bcd mode, truncate if necessary */
323 if (devlo
== devhi
|| !ndigits
) {
324 do_usb_entry(symval
, devlo
, ndigits
, clo
, chi
, max
, mod
);
330 incbcd(&devlo
, 1, max
,
331 sizeof(bcdDevice_lo
) * 2),
332 ndigits
, clo
, max
, max
, mod
);
336 incbcd(&devhi
, -1, max
,
337 sizeof(bcdDevice_lo
) * 2),
338 ndigits
, 0x0, chi
, max
, mod
);
342 static void do_usb_table(void *symval
, unsigned long size
,
346 const unsigned long id_size
= SIZE_usb_device_id
;
348 device_id_check(mod
->name
, "usb", size
, id_size
, symval
);
350 /* Leave last one: it's the terminator. */
353 for (i
= 0; i
< size
; i
+= id_size
)
354 do_usb_entry_multi(symval
+ i
, mod
);
357 /* Looks like: hid:bNvNpN */
358 static int do_hid_entry(const char *filename
,
359 void *symval
, char *alias
)
361 DEF_FIELD(symval
, hid_device_id
, bus
);
362 DEF_FIELD(symval
, hid_device_id
, group
);
363 DEF_FIELD(symval
, hid_device_id
, vendor
);
364 DEF_FIELD(symval
, hid_device_id
, product
);
366 sprintf(alias
, "hid:");
367 ADD(alias
, "b", bus
!= HID_BUS_ANY
, bus
);
368 ADD(alias
, "g", group
!= HID_GROUP_ANY
, group
);
369 ADD(alias
, "v", vendor
!= HID_ANY_ID
, vendor
);
370 ADD(alias
, "p", product
!= HID_ANY_ID
, product
);
374 ADD_TO_DEVTABLE("hid", hid_device_id
, do_hid_entry
);
376 /* Looks like: ieee1394:venNmoNspNverN */
377 static int do_ieee1394_entry(const char *filename
,
378 void *symval
, char *alias
)
380 DEF_FIELD(symval
, ieee1394_device_id
, match_flags
);
381 DEF_FIELD(symval
, ieee1394_device_id
, vendor_id
);
382 DEF_FIELD(symval
, ieee1394_device_id
, model_id
);
383 DEF_FIELD(symval
, ieee1394_device_id
, specifier_id
);
384 DEF_FIELD(symval
, ieee1394_device_id
, version
);
386 strcpy(alias
, "ieee1394:");
387 ADD(alias
, "ven", match_flags
& IEEE1394_MATCH_VENDOR_ID
,
389 ADD(alias
, "mo", match_flags
& IEEE1394_MATCH_MODEL_ID
,
391 ADD(alias
, "sp", match_flags
& IEEE1394_MATCH_SPECIFIER_ID
,
393 ADD(alias
, "ver", match_flags
& IEEE1394_MATCH_VERSION
,
399 ADD_TO_DEVTABLE("ieee1394", ieee1394_device_id
, do_ieee1394_entry
);
401 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
402 static int do_pci_entry(const char *filename
,
403 void *symval
, char *alias
)
405 /* Class field can be divided into these three. */
406 unsigned char baseclass
, subclass
, interface
,
407 baseclass_mask
, subclass_mask
, interface_mask
;
409 DEF_FIELD(symval
, pci_device_id
, vendor
);
410 DEF_FIELD(symval
, pci_device_id
, device
);
411 DEF_FIELD(symval
, pci_device_id
, subvendor
);
412 DEF_FIELD(symval
, pci_device_id
, subdevice
);
413 DEF_FIELD(symval
, pci_device_id
, class);
414 DEF_FIELD(symval
, pci_device_id
, class_mask
);
416 strcpy(alias
, "pci:");
417 ADD(alias
, "v", vendor
!= PCI_ANY_ID
, vendor
);
418 ADD(alias
, "d", device
!= PCI_ANY_ID
, device
);
419 ADD(alias
, "sv", subvendor
!= PCI_ANY_ID
, subvendor
);
420 ADD(alias
, "sd", subdevice
!= PCI_ANY_ID
, subdevice
);
422 baseclass
= (class) >> 16;
423 baseclass_mask
= (class_mask
) >> 16;
424 subclass
= (class) >> 8;
425 subclass_mask
= (class_mask
) >> 8;
427 interface_mask
= class_mask
;
429 if ((baseclass_mask
!= 0 && baseclass_mask
!= 0xFF)
430 || (subclass_mask
!= 0 && subclass_mask
!= 0xFF)
431 || (interface_mask
!= 0 && interface_mask
!= 0xFF)) {
432 warn("Can't handle masks in %s:%04X\n",
433 filename
, class_mask
);
437 ADD(alias
, "bc", baseclass_mask
== 0xFF, baseclass
);
438 ADD(alias
, "sc", subclass_mask
== 0xFF, subclass
);
439 ADD(alias
, "i", interface_mask
== 0xFF, interface
);
443 ADD_TO_DEVTABLE("pci", pci_device_id
, do_pci_entry
);
445 /* looks like: "ccw:tNmNdtNdmN" */
446 static int do_ccw_entry(const char *filename
,
447 void *symval
, char *alias
)
449 DEF_FIELD(symval
, ccw_device_id
, match_flags
);
450 DEF_FIELD(symval
, ccw_device_id
, cu_type
);
451 DEF_FIELD(symval
, ccw_device_id
, cu_model
);
452 DEF_FIELD(symval
, ccw_device_id
, dev_type
);
453 DEF_FIELD(symval
, ccw_device_id
, dev_model
);
455 strcpy(alias
, "ccw:");
456 ADD(alias
, "t", match_flags
&CCW_DEVICE_ID_MATCH_CU_TYPE
,
458 ADD(alias
, "m", match_flags
&CCW_DEVICE_ID_MATCH_CU_MODEL
,
460 ADD(alias
, "dt", match_flags
&CCW_DEVICE_ID_MATCH_DEVICE_TYPE
,
462 ADD(alias
, "dm", match_flags
&CCW_DEVICE_ID_MATCH_DEVICE_MODEL
,
467 ADD_TO_DEVTABLE("ccw", ccw_device_id
, do_ccw_entry
);
469 /* looks like: "ap:tN" */
470 static int do_ap_entry(const char *filename
,
471 void *symval
, char *alias
)
473 DEF_FIELD(symval
, ap_device_id
, dev_type
);
475 sprintf(alias
, "ap:t%02X*", dev_type
);
478 ADD_TO_DEVTABLE("ap", ap_device_id
, do_ap_entry
);
480 /* looks like: "css:tN" */
481 static int do_css_entry(const char *filename
,
482 void *symval
, char *alias
)
484 DEF_FIELD(symval
, css_device_id
, type
);
486 sprintf(alias
, "css:t%01X", type
);
489 ADD_TO_DEVTABLE("css", css_device_id
, do_css_entry
);
491 /* Looks like: "serio:tyNprNidNexN" */
492 static int do_serio_entry(const char *filename
,
493 void *symval
, char *alias
)
495 DEF_FIELD(symval
, serio_device_id
, type
);
496 DEF_FIELD(symval
, serio_device_id
, proto
);
497 DEF_FIELD(symval
, serio_device_id
, id
);
498 DEF_FIELD(symval
, serio_device_id
, extra
);
500 strcpy(alias
, "serio:");
501 ADD(alias
, "ty", type
!= SERIO_ANY
, type
);
502 ADD(alias
, "pr", proto
!= SERIO_ANY
, proto
);
503 ADD(alias
, "id", id
!= SERIO_ANY
, id
);
504 ADD(alias
, "ex", extra
!= SERIO_ANY
, extra
);
509 ADD_TO_DEVTABLE("serio", serio_device_id
, do_serio_entry
);
511 /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
512 static int do_acpi_entry(const char *filename
,
513 void *symval
, char *alias
)
515 DEF_FIELD_ADDR(symval
, acpi_device_id
, id
);
516 sprintf(alias
, "acpi*:%s:*", *id
);
519 ADD_TO_DEVTABLE("acpi", acpi_device_id
, do_acpi_entry
);
521 /* looks like: "pnp:dD" */
522 static void do_pnp_device_entry(void *symval
, unsigned long size
,
525 const unsigned long id_size
= SIZE_pnp_device_id
;
526 const unsigned int count
= (size
/ id_size
)-1;
529 device_id_check(mod
->name
, "pnp", size
, id_size
, symval
);
531 for (i
= 0; i
< count
; i
++) {
532 DEF_FIELD_ADDR(symval
+ i
*id_size
, pnp_device_id
, id
);
533 char acpi_id
[sizeof(*id
)];
536 buf_printf(&mod
->dev_table_buf
,
537 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id
);
539 /* fix broken pnp bus lowercasing */
540 for (j
= 0; j
< sizeof(acpi_id
); j
++)
541 acpi_id
[j
] = toupper((*id
)[j
]);
542 buf_printf(&mod
->dev_table_buf
,
543 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id
);
547 /* looks like: "pnp:dD" for every device of the card */
548 static void do_pnp_card_entries(void *symval
, unsigned long size
,
551 const unsigned long id_size
= SIZE_pnp_card_device_id
;
552 const unsigned int count
= (size
/ id_size
)-1;
555 device_id_check(mod
->name
, "pnp", size
, id_size
, symval
);
557 for (i
= 0; i
< count
; i
++) {
559 DEF_FIELD_ADDR(symval
+ i
*id_size
, pnp_card_device_id
, devs
);
561 for (j
= 0; j
< PNP_MAX_DEVICES
; j
++) {
562 const char *id
= (char *)(*devs
)[j
].id
;
569 /* find duplicate, already added value */
570 for (i2
= 0; i2
< i
&& !dup
; i2
++) {
571 DEF_FIELD_ADDR(symval
+ i2
*id_size
, pnp_card_device_id
, devs
);
573 for (j2
= 0; j2
< PNP_MAX_DEVICES
; j2
++) {
574 const char *id2
= (char *)(*devs
)[j2
].id
;
579 if (!strcmp(id
, id2
)) {
586 /* add an individual alias for every device entry */
588 char acpi_id
[PNP_ID_LEN
];
591 buf_printf(&mod
->dev_table_buf
,
592 "MODULE_ALIAS(\"pnp:d%s*\");\n", id
);
594 /* fix broken pnp bus lowercasing */
595 for (k
= 0; k
< sizeof(acpi_id
); k
++)
596 acpi_id
[k
] = toupper(id
[k
]);
597 buf_printf(&mod
->dev_table_buf
,
598 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id
);
604 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
605 static int do_pcmcia_entry(const char *filename
,
606 void *symval
, char *alias
)
609 DEF_FIELD(symval
, pcmcia_device_id
, match_flags
);
610 DEF_FIELD(symval
, pcmcia_device_id
, manf_id
);
611 DEF_FIELD(symval
, pcmcia_device_id
, card_id
);
612 DEF_FIELD(symval
, pcmcia_device_id
, func_id
);
613 DEF_FIELD(symval
, pcmcia_device_id
, function
);
614 DEF_FIELD(symval
, pcmcia_device_id
, device_no
);
615 DEF_FIELD_ADDR(symval
, pcmcia_device_id
, prod_id_hash
);
617 for (i
=0; i
<4; i
++) {
618 (*prod_id_hash
)[i
] = TO_NATIVE((*prod_id_hash
)[i
]);
621 strcpy(alias
, "pcmcia:");
622 ADD(alias
, "m", match_flags
& PCMCIA_DEV_ID_MATCH_MANF_ID
,
624 ADD(alias
, "c", match_flags
& PCMCIA_DEV_ID_MATCH_CARD_ID
,
626 ADD(alias
, "f", match_flags
& PCMCIA_DEV_ID_MATCH_FUNC_ID
,
628 ADD(alias
, "fn", match_flags
& PCMCIA_DEV_ID_MATCH_FUNCTION
,
630 ADD(alias
, "pfn", match_flags
& PCMCIA_DEV_ID_MATCH_DEVICE_NO
,
632 ADD(alias
, "pa", match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID1
, (*prod_id_hash
)[0]);
633 ADD(alias
, "pb", match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID2
, (*prod_id_hash
)[1]);
634 ADD(alias
, "pc", match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID3
, (*prod_id_hash
)[2]);
635 ADD(alias
, "pd", match_flags
& PCMCIA_DEV_ID_MATCH_PROD_ID4
, (*prod_id_hash
)[3]);
640 ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id
, do_pcmcia_entry
);
642 static int do_of_entry (const char *filename
, void *symval
, char *alias
)
646 DEF_FIELD_ADDR(symval
, of_device_id
, name
);
647 DEF_FIELD_ADDR(symval
, of_device_id
, type
);
648 DEF_FIELD_ADDR(symval
, of_device_id
, compatible
);
650 len
= sprintf (alias
, "of:N%sT%s",
651 (*name
)[0] ? *name
: "*",
652 (*type
)[0] ? *type
: "*");
655 sprintf (&alias
[len
], "%sC%s",
656 (*type
)[0] ? "*" : "",
659 /* Replace all whitespace with underscores */
660 for (tmp
= alias
; tmp
&& *tmp
; tmp
++)
667 ADD_TO_DEVTABLE("of", of_device_id
, do_of_entry
);
669 static int do_vio_entry(const char *filename
, void *symval
,
673 DEF_FIELD_ADDR(symval
, vio_device_id
, type
);
674 DEF_FIELD_ADDR(symval
, vio_device_id
, compat
);
676 sprintf(alias
, "vio:T%sS%s", (*type
)[0] ? *type
: "*",
677 (*compat
)[0] ? *compat
: "*");
679 /* Replace all whitespace with underscores */
680 for (tmp
= alias
; tmp
&& *tmp
; tmp
++)
687 ADD_TO_DEVTABLE("vio", vio_device_id
, do_vio_entry
);
689 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
691 static void do_input(char *alias
,
692 kernel_ulong_t
*arr
, unsigned int min
, unsigned int max
)
696 for (i
= min
/ BITS_PER_LONG
; i
< max
/ BITS_PER_LONG
+ 1; i
++)
697 arr
[i
] = TO_NATIVE(arr
[i
]);
698 for (i
= min
; i
< max
; i
++)
699 if (arr
[i
/ BITS_PER_LONG
] & (1L << (i
%BITS_PER_LONG
)))
700 sprintf(alias
+ strlen(alias
), "%X,*", i
);
703 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
704 static int do_input_entry(const char *filename
, void *symval
,
707 DEF_FIELD(symval
, input_device_id
, flags
);
708 DEF_FIELD(symval
, input_device_id
, bustype
);
709 DEF_FIELD(symval
, input_device_id
, vendor
);
710 DEF_FIELD(symval
, input_device_id
, product
);
711 DEF_FIELD(symval
, input_device_id
, version
);
712 DEF_FIELD_ADDR(symval
, input_device_id
, evbit
);
713 DEF_FIELD_ADDR(symval
, input_device_id
, keybit
);
714 DEF_FIELD_ADDR(symval
, input_device_id
, relbit
);
715 DEF_FIELD_ADDR(symval
, input_device_id
, absbit
);
716 DEF_FIELD_ADDR(symval
, input_device_id
, mscbit
);
717 DEF_FIELD_ADDR(symval
, input_device_id
, ledbit
);
718 DEF_FIELD_ADDR(symval
, input_device_id
, sndbit
);
719 DEF_FIELD_ADDR(symval
, input_device_id
, ffbit
);
720 DEF_FIELD_ADDR(symval
, input_device_id
, swbit
);
722 sprintf(alias
, "input:");
724 ADD(alias
, "b", flags
& INPUT_DEVICE_ID_MATCH_BUS
, bustype
);
725 ADD(alias
, "v", flags
& INPUT_DEVICE_ID_MATCH_VENDOR
, vendor
);
726 ADD(alias
, "p", flags
& INPUT_DEVICE_ID_MATCH_PRODUCT
, product
);
727 ADD(alias
, "e", flags
& INPUT_DEVICE_ID_MATCH_VERSION
, version
);
729 sprintf(alias
+ strlen(alias
), "-e*");
730 if (flags
& INPUT_DEVICE_ID_MATCH_EVBIT
)
731 do_input(alias
, *evbit
, 0, INPUT_DEVICE_ID_EV_MAX
);
732 sprintf(alias
+ strlen(alias
), "k*");
733 if (flags
& INPUT_DEVICE_ID_MATCH_KEYBIT
)
734 do_input(alias
, *keybit
,
735 INPUT_DEVICE_ID_KEY_MIN_INTERESTING
,
736 INPUT_DEVICE_ID_KEY_MAX
);
737 sprintf(alias
+ strlen(alias
), "r*");
738 if (flags
& INPUT_DEVICE_ID_MATCH_RELBIT
)
739 do_input(alias
, *relbit
, 0, INPUT_DEVICE_ID_REL_MAX
);
740 sprintf(alias
+ strlen(alias
), "a*");
741 if (flags
& INPUT_DEVICE_ID_MATCH_ABSBIT
)
742 do_input(alias
, *absbit
, 0, INPUT_DEVICE_ID_ABS_MAX
);
743 sprintf(alias
+ strlen(alias
), "m*");
744 if (flags
& INPUT_DEVICE_ID_MATCH_MSCIT
)
745 do_input(alias
, *mscbit
, 0, INPUT_DEVICE_ID_MSC_MAX
);
746 sprintf(alias
+ strlen(alias
), "l*");
747 if (flags
& INPUT_DEVICE_ID_MATCH_LEDBIT
)
748 do_input(alias
, *ledbit
, 0, INPUT_DEVICE_ID_LED_MAX
);
749 sprintf(alias
+ strlen(alias
), "s*");
750 if (flags
& INPUT_DEVICE_ID_MATCH_SNDBIT
)
751 do_input(alias
, *sndbit
, 0, INPUT_DEVICE_ID_SND_MAX
);
752 sprintf(alias
+ strlen(alias
), "f*");
753 if (flags
& INPUT_DEVICE_ID_MATCH_FFBIT
)
754 do_input(alias
, *ffbit
, 0, INPUT_DEVICE_ID_FF_MAX
);
755 sprintf(alias
+ strlen(alias
), "w*");
756 if (flags
& INPUT_DEVICE_ID_MATCH_SWBIT
)
757 do_input(alias
, *swbit
, 0, INPUT_DEVICE_ID_SW_MAX
);
760 ADD_TO_DEVTABLE("input", input_device_id
, do_input_entry
);
762 static int do_eisa_entry(const char *filename
, void *symval
,
765 DEF_FIELD_ADDR(symval
, eisa_device_id
, sig
);
767 sprintf(alias
, EISA_DEVICE_MODALIAS_FMT
"*", *sig
);
772 ADD_TO_DEVTABLE("eisa", eisa_device_id
, do_eisa_entry
);
774 /* Looks like: parisc:tNhvNrevNsvN */
775 static int do_parisc_entry(const char *filename
, void *symval
,
778 DEF_FIELD(symval
, parisc_device_id
, hw_type
);
779 DEF_FIELD(symval
, parisc_device_id
, hversion
);
780 DEF_FIELD(symval
, parisc_device_id
, hversion_rev
);
781 DEF_FIELD(symval
, parisc_device_id
, sversion
);
783 strcpy(alias
, "parisc:");
784 ADD(alias
, "t", hw_type
!= PA_HWTYPE_ANY_ID
, hw_type
);
785 ADD(alias
, "hv", hversion
!= PA_HVERSION_ANY_ID
, hversion
);
786 ADD(alias
, "rev", hversion_rev
!= PA_HVERSION_REV_ANY_ID
, hversion_rev
);
787 ADD(alias
, "sv", sversion
!= PA_SVERSION_ANY_ID
, sversion
);
792 ADD_TO_DEVTABLE("parisc", parisc_device_id
, do_parisc_entry
);
794 /* Looks like: sdio:cNvNdN. */
795 static int do_sdio_entry(const char *filename
,
796 void *symval
, char *alias
)
798 DEF_FIELD(symval
, sdio_device_id
, class);
799 DEF_FIELD(symval
, sdio_device_id
, vendor
);
800 DEF_FIELD(symval
, sdio_device_id
, device
);
802 strcpy(alias
, "sdio:");
803 ADD(alias
, "c", class != (__u8
)SDIO_ANY_ID
, class);
804 ADD(alias
, "v", vendor
!= (__u16
)SDIO_ANY_ID
, vendor
);
805 ADD(alias
, "d", device
!= (__u16
)SDIO_ANY_ID
, device
);
809 ADD_TO_DEVTABLE("sdio", sdio_device_id
, do_sdio_entry
);
811 /* Looks like: ssb:vNidNrevN. */
812 static int do_ssb_entry(const char *filename
,
813 void *symval
, char *alias
)
815 DEF_FIELD(symval
, ssb_device_id
, vendor
);
816 DEF_FIELD(symval
, ssb_device_id
, coreid
);
817 DEF_FIELD(symval
, ssb_device_id
, revision
);
819 strcpy(alias
, "ssb:");
820 ADD(alias
, "v", vendor
!= SSB_ANY_VENDOR
, vendor
);
821 ADD(alias
, "id", coreid
!= SSB_ANY_ID
, coreid
);
822 ADD(alias
, "rev", revision
!= SSB_ANY_REV
, revision
);
826 ADD_TO_DEVTABLE("ssb", ssb_device_id
, do_ssb_entry
);
828 /* Looks like: bcma:mNidNrevNclN. */
829 static int do_bcma_entry(const char *filename
,
830 void *symval
, char *alias
)
832 DEF_FIELD(symval
, bcma_device_id
, manuf
);
833 DEF_FIELD(symval
, bcma_device_id
, id
);
834 DEF_FIELD(symval
, bcma_device_id
, rev
);
835 DEF_FIELD(symval
, bcma_device_id
, class);
837 strcpy(alias
, "bcma:");
838 ADD(alias
, "m", manuf
!= BCMA_ANY_MANUF
, manuf
);
839 ADD(alias
, "id", id
!= BCMA_ANY_ID
, id
);
840 ADD(alias
, "rev", rev
!= BCMA_ANY_REV
, rev
);
841 ADD(alias
, "cl", class != BCMA_ANY_CLASS
, class);
845 ADD_TO_DEVTABLE("bcma", bcma_device_id
, do_bcma_entry
);
847 /* Looks like: virtio:dNvN */
848 static int do_virtio_entry(const char *filename
, void *symval
,
851 DEF_FIELD(symval
, virtio_device_id
, device
);
852 DEF_FIELD(symval
, virtio_device_id
, vendor
);
854 strcpy(alias
, "virtio:");
855 ADD(alias
, "d", device
!= VIRTIO_DEV_ANY_ID
, device
);
856 ADD(alias
, "v", vendor
!= VIRTIO_DEV_ANY_ID
, vendor
);
861 ADD_TO_DEVTABLE("virtio", virtio_device_id
, do_virtio_entry
);
864 * Looks like: vmbus:guid
865 * Each byte of the guid will be represented by two hex characters
869 static int do_vmbus_entry(const char *filename
, void *symval
,
873 DEF_FIELD_ADDR(symval
, hv_vmbus_device_id
, guid
);
874 char guid_name
[(sizeof(*guid
) + 1) * 2];
876 for (i
= 0; i
< (sizeof(*guid
) * 2); i
+= 2)
877 sprintf(&guid_name
[i
], "%02x", TO_NATIVE((*guid
)[i
/2]));
879 strcpy(alias
, "vmbus:");
880 strcat(alias
, guid_name
);
884 ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id
, do_vmbus_entry
);
886 /* Looks like: i2c:S */
887 static int do_i2c_entry(const char *filename
, void *symval
,
890 DEF_FIELD_ADDR(symval
, i2c_device_id
, name
);
891 sprintf(alias
, I2C_MODULE_PREFIX
"%s", *name
);
895 ADD_TO_DEVTABLE("i2c", i2c_device_id
, do_i2c_entry
);
897 /* Looks like: spi:S */
898 static int do_spi_entry(const char *filename
, void *symval
,
901 DEF_FIELD_ADDR(symval
, spi_device_id
, name
);
902 sprintf(alias
, SPI_MODULE_PREFIX
"%s", *name
);
906 ADD_TO_DEVTABLE("spi", spi_device_id
, do_spi_entry
);
908 static const struct dmifield
{
912 { "bvn", DMI_BIOS_VENDOR
},
913 { "bvr", DMI_BIOS_VERSION
},
914 { "bd", DMI_BIOS_DATE
},
915 { "svn", DMI_SYS_VENDOR
},
916 { "pn", DMI_PRODUCT_NAME
},
917 { "pvr", DMI_PRODUCT_VERSION
},
918 { "rvn", DMI_BOARD_VENDOR
},
919 { "rn", DMI_BOARD_NAME
},
920 { "rvr", DMI_BOARD_VERSION
},
921 { "cvn", DMI_CHASSIS_VENDOR
},
922 { "ct", DMI_CHASSIS_TYPE
},
923 { "cvr", DMI_CHASSIS_VERSION
},
927 static void dmi_ascii_filter(char *d
, const char *s
)
929 /* Filter out characters we don't want to see in the modalias string */
931 if (*s
> ' ' && *s
< 127 && *s
!= ':')
938 static int do_dmi_entry(const char *filename
, void *symval
,
942 DEF_FIELD_ADDR(symval
, dmi_system_id
, matches
);
943 sprintf(alias
, "dmi*");
945 for (i
= 0; i
< ARRAY_SIZE(dmi_fields
); i
++) {
946 for (j
= 0; j
< 4; j
++) {
947 if ((*matches
)[j
].slot
&&
948 (*matches
)[j
].slot
== dmi_fields
[i
].field
) {
949 sprintf(alias
+ strlen(alias
), ":%s*",
950 dmi_fields
[i
].prefix
);
951 dmi_ascii_filter(alias
+ strlen(alias
),
952 (*matches
)[j
].substr
);
961 ADD_TO_DEVTABLE("dmi", dmi_system_id
, do_dmi_entry
);
963 static int do_platform_entry(const char *filename
,
964 void *symval
, char *alias
)
966 DEF_FIELD_ADDR(symval
, platform_device_id
, name
);
967 sprintf(alias
, PLATFORM_MODULE_PREFIX
"%s", *name
);
970 ADD_TO_DEVTABLE("platform", platform_device_id
, do_platform_entry
);
972 static int do_mdio_entry(const char *filename
,
973 void *symval
, char *alias
)
976 DEF_FIELD(symval
, mdio_device_id
, phy_id
);
977 DEF_FIELD(symval
, mdio_device_id
, phy_id_mask
);
979 alias
+= sprintf(alias
, MDIO_MODULE_PREFIX
);
981 for (i
= 0; i
< 32; i
++) {
982 if (!((phy_id_mask
>> (31-i
)) & 1))
984 else if ((phy_id
>> (31-i
)) & 1)
990 /* Terminate the string */
995 ADD_TO_DEVTABLE("mdio", mdio_device_id
, do_mdio_entry
);
997 /* Looks like: zorro:iN. */
998 static int do_zorro_entry(const char *filename
, void *symval
,
1001 DEF_FIELD(symval
, zorro_device_id
, id
);
1002 strcpy(alias
, "zorro:");
1003 ADD(alias
, "i", id
!= ZORRO_WILDCARD
, id
);
1006 ADD_TO_DEVTABLE("zorro", zorro_device_id
, do_zorro_entry
);
1008 /* looks like: "pnp:dD" */
1009 static int do_isapnp_entry(const char *filename
,
1010 void *symval
, char *alias
)
1012 DEF_FIELD(symval
, isapnp_device_id
, vendor
);
1013 DEF_FIELD(symval
, isapnp_device_id
, function
);
1014 sprintf(alias
, "pnp:d%c%c%c%x%x%x%x*",
1015 'A' + ((vendor
>> 2) & 0x3f) - 1,
1016 'A' + (((vendor
& 3) << 3) | ((vendor
>> 13) & 7)) - 1,
1017 'A' + ((vendor
>> 8) & 0x1f) - 1,
1018 (function
>> 4) & 0x0f, function
& 0x0f,
1019 (function
>> 12) & 0x0f, (function
>> 8) & 0x0f);
1022 ADD_TO_DEVTABLE("isapnp", isapnp_device_id
, do_isapnp_entry
);
1024 /* Looks like: "ipack:fNvNdN". */
1025 static int do_ipack_entry(const char *filename
,
1026 void *symval
, char *alias
)
1028 DEF_FIELD(symval
, ipack_device_id
, format
);
1029 DEF_FIELD(symval
, ipack_device_id
, vendor
);
1030 DEF_FIELD(symval
, ipack_device_id
, device
);
1031 strcpy(alias
, "ipack:");
1032 ADD(alias
, "f", format
!= IPACK_ANY_FORMAT
, format
);
1033 ADD(alias
, "v", vendor
!= IPACK_ANY_ID
, vendor
);
1034 ADD(alias
, "d", device
!= IPACK_ANY_ID
, device
);
1035 add_wildcard(alias
);
1038 ADD_TO_DEVTABLE("ipack", ipack_device_id
, do_ipack_entry
);
1041 * Append a match expression for a single masked hex digit.
1042 * outp points to a pointer to the character at which to append.
1043 * *outp is updated on return to point just after the appended text,
1044 * to facilitate further appending.
1046 static void append_nibble_mask(char **outp
,
1047 unsigned int nibble
, unsigned int mask
)
1058 p
+= sprintf(p
, "%X", nibble
);
1063 * Dumbly emit a match pattern for all possible matching
1064 * digits. This could be improved in some cases using ranges,
1065 * but it has the advantage of being trivially correct, and is
1069 for (i
= 0; i
< 0x10; i
++)
1070 if ((i
& mask
) == nibble
)
1071 p
+= sprintf(p
, "%X", i
);
1075 /* Ensure that the string remains NUL-terminated: */
1078 /* Advance the caller's end-of-string pointer: */
1083 * looks like: "amba:dN"
1085 * N is exactly 8 digits, where each is an upper-case hex digit, or
1086 * a ? or [] pattern matching exactly one digit.
1088 static int do_amba_entry(const char *filename
,
1089 void *symval
, char *alias
)
1093 DEF_FIELD(symval
, amba_id
, id
);
1094 DEF_FIELD(symval
, amba_id
, mask
);
1096 if ((id
& mask
) != id
)
1097 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1098 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
1099 filename
, id
, mask
);
1101 p
+= sprintf(alias
, "amba:d");
1102 for (digit
= 0; digit
< 8; digit
++)
1103 append_nibble_mask(&p
,
1104 (id
>> (4 * (7 - digit
))) & 0xf,
1105 (mask
>> (4 * (7 - digit
))) & 0xf);
1109 ADD_TO_DEVTABLE("amba", amba_id
, do_amba_entry
);
1111 /* LOOKS like x86cpu:vendor:VVVV:family:FFFF:model:MMMM:feature:*,FEAT,*
1112 * All fields are numbers. It would be nicer to use strings for vendor
1113 * and feature, but getting those out of the build system here is too
1117 static int do_x86cpu_entry(const char *filename
, void *symval
,
1120 DEF_FIELD(symval
, x86_cpu_id
, feature
);
1121 DEF_FIELD(symval
, x86_cpu_id
, family
);
1122 DEF_FIELD(symval
, x86_cpu_id
, model
);
1123 DEF_FIELD(symval
, x86_cpu_id
, vendor
);
1125 strcpy(alias
, "x86cpu:");
1126 ADD(alias
, "vendor:", vendor
!= X86_VENDOR_ANY
, vendor
);
1127 ADD(alias
, ":family:", family
!= X86_FAMILY_ANY
, family
);
1128 ADD(alias
, ":model:", model
!= X86_MODEL_ANY
, model
);
1129 strcat(alias
, ":feature:*");
1130 if (feature
!= X86_FEATURE_ANY
)
1131 sprintf(alias
+ strlen(alias
), "%04X*", feature
);
1134 ADD_TO_DEVTABLE("x86cpu", x86_cpu_id
, do_x86cpu_entry
);
1136 /* Does namelen bytes of name exactly match the symbol? */
1137 static bool sym_is(const char *name
, unsigned namelen
, const char *symbol
)
1139 if (namelen
!= strlen(symbol
))
1142 return memcmp(name
, symbol
, namelen
) == 0;
1145 static void do_table(void *symval
, unsigned long size
,
1146 unsigned long id_size
,
1147 const char *device_id
,
1153 int (*do_entry
)(const char *, void *entry
, char *alias
) = function
;
1155 device_id_check(mod
->name
, device_id
, size
, id_size
, symval
);
1156 /* Leave last one: it's the terminator. */
1159 for (i
= 0; i
< size
; i
+= id_size
) {
1160 if (do_entry(mod
->name
, symval
+i
, alias
)) {
1161 buf_printf(&mod
->dev_table_buf
,
1162 "MODULE_ALIAS(\"%s\");\n", alias
);
1167 /* Create MODULE_ALIAS() statements.
1168 * At this time, we cannot write the actual output C source yet,
1169 * so we write into the mod->dev_table_buf buffer. */
1170 void handle_moddevtable(struct module
*mod
, struct elf_info
*info
,
1171 Elf_Sym
*sym
, const char *symname
)
1176 unsigned int namelen
;
1178 /* We're looking for a section relative symbol */
1179 if (!sym
->st_shndx
|| get_secindex(info
, sym
) >= info
->num_sections
)
1182 /* We're looking for an object */
1183 if (ELF_ST_TYPE(sym
->st_info
) != STT_OBJECT
)
1186 /* All our symbols are of form <prefix>__mod_XXX_device_table. */
1187 name
= strstr(symname
, "__mod_");
1190 name
+= strlen("__mod_");
1191 namelen
= strlen(name
);
1192 if (namelen
< strlen("_device_table"))
1194 if (strcmp(name
+ namelen
- strlen("_device_table"), "_device_table"))
1196 namelen
-= strlen("_device_table");
1198 /* Handle all-NULL symbols allocated into .bss */
1199 if (info
->sechdrs
[get_secindex(info
, sym
)].sh_type
& SHT_NOBITS
) {
1200 zeros
= calloc(1, sym
->st_size
);
1203 symval
= (void *)info
->hdr
1204 + info
->sechdrs
[get_secindex(info
, sym
)].sh_offset
1208 /* First handle the "special" cases */
1209 if (sym_is(name
, namelen
, "usb"))
1210 do_usb_table(symval
, sym
->st_size
, mod
);
1211 else if (sym_is(name
, namelen
, "pnp"))
1212 do_pnp_device_entry(symval
, sym
->st_size
, mod
);
1213 else if (sym_is(name
, namelen
, "pnp_card"))
1214 do_pnp_card_entries(symval
, sym
->st_size
, mod
);
1216 struct devtable
**p
;
1217 INIT_SECTION(__devtable
);
1219 for (p
= __start___devtable
; p
< __stop___devtable
; p
++) {
1220 if (sym_is(name
, namelen
, (*p
)->device_id
)) {
1221 do_table(symval
, sym
->st_size
, (*p
)->id_size
,
1222 (*p
)->device_id
, (*p
)->function
, mod
);
1230 /* Now add out buffered information to the generated C source */
1231 void add_moddevtable(struct buffer
*buf
, struct module
*mod
)
1233 buf_printf(buf
, "\n");
1234 buf_write(buf
, mod
->dev_table_buf
.p
, mod
->dev_table_buf
.pos
);
1235 free(mod
->dev_table_buf
.p
);