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 <stdarg.h>
14 #include <stdio.h>
15
16 #include "list.h"
17 #include "xalloc.h"
18
19 #include "modpost.h"
20 #include "devicetable-offsets.h"
21
22 /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
23 * use either stdint.h or inttypes.h for the rest. */
24 #if KERNEL_ELFCLASS == ELFCLASS32
25 typedef Elf32_Addr kernel_ulong_t;
26 #define BITS_PER_LONG 32
27 #else
28 typedef Elf64_Addr kernel_ulong_t;
29 #define BITS_PER_LONG 64
30 #endif
31 #ifdef __sun__
32 #include <inttypes.h>
33 #else
34 #include <stdint.h>
35 #endif
36
37 #include <ctype.h>
38 #include <stdbool.h>
39
40 /**
41 * module_alias_printf - add auto-generated MODULE_ALIAS()
42 *
43 * @mod: module
44 * @append_wildcard: append '*' for future extension if not exist yet
45 * @fmt: printf(3)-like format
46 */
47 static void __attribute__((format (printf, 3, 4)))
module_alias_printf(struct module * mod,bool append_wildcard,const char * fmt,...)48 module_alias_printf(struct module *mod, bool append_wildcard,
49 const char *fmt, ...)
50 {
51 struct module_alias *new, *als;
52 size_t len;
53 int n;
54 va_list ap;
55
56 /* Determine required size. */
57 va_start(ap, fmt);
58 n = vsnprintf(NULL, 0, fmt, ap);
59 va_end(ap);
60
61 if (n < 0) {
62 error("vsnprintf failed\n");
63 return;
64 }
65
66 len = n + 1; /* extra byte for '\0' */
67
68 if (append_wildcard)
69 len++; /* extra byte for '*' */
70
71 new = xmalloc(sizeof(*new) + len);
72
73 /* Now, really print it to the allocated buffer */
74 va_start(ap, fmt);
75 n = vsnprintf(new->str, len, fmt, ap);
76 va_end(ap);
77
78 if (n < 0) {
79 error("vsnprintf failed\n");
80 free(new);
81 return;
82 }
83
84 if (append_wildcard && (n == 0 || new->str[n - 1] != '*')) {
85 new->str[n] = '*';
86 new->str[n + 1] = '\0';
87 }
88
89 /* avoid duplication */
90 list_for_each_entry(als, &mod->aliases, node) {
91 if (!strcmp(als->str, new->str)) {
92 free(new);
93 return;
94 }
95 }
96
97 list_add_tail(&new->node, &mod->aliases);
98 }
99
100 typedef uint32_t __u32;
101 typedef uint16_t __u16;
102 typedef unsigned char __u8;
103
104 /* UUID types for backward compatibility, don't use in new code */
105 typedef struct {
106 __u8 b[16];
107 } guid_t;
108
109 typedef struct {
110 __u8 b[16];
111 } uuid_t;
112
113 #define UUID_STRING_LEN 36
114
115 /* MEI UUID type, don't use anywhere else */
116 typedef struct {
117 __u8 b[16];
118 } uuid_le;
119
120 /* Big exception to the "don't include kernel headers into userspace, which
121 * even potentially has different endianness and word sizes, since
122 * we handle those differences explicitly below */
123 #include "../../include/linux/mod_devicetable.h"
124
125 struct devtable {
126 const char *device_id;
127 unsigned long id_size;
128 void (*do_entry)(struct module *mod, void *symval);
129 };
130
131 /* Define a variable f that holds the value of field f of struct devid
132 * based at address m.
133 */
134 #define DEF_FIELD(m, devid, f) \
135 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
136
137 /* Define a variable f that holds the address of field f of struct devid
138 * based at address m. Due to the way typeof works, for a field of type
139 * T[N] the variable has type T(*)[N], _not_ T*.
140 */
141 #define DEF_FIELD_ADDR(m, devid, f) \
142 typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
143
144 #define ADD(str, sep, cond, field) \
145 do { \
146 strcat(str, sep); \
147 if (cond) \
148 sprintf(str + strlen(str), \
149 sizeof(field) == 1 ? "%02X" : \
150 sizeof(field) == 2 ? "%04X" : \
151 sizeof(field) == 4 ? "%08X" : "", \
152 field); \
153 else \
154 sprintf(str + strlen(str), "*"); \
155 } while(0)
156
add_uuid(char * str,uuid_le uuid)157 static inline void add_uuid(char *str, uuid_le uuid)
158 {
159 int len = strlen(str);
160
161 sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
162 uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
163 uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
164 uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
165 uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
166 }
167
add_guid(char * str,guid_t guid)168 static inline void add_guid(char *str, guid_t guid)
169 {
170 int len = strlen(str);
171
172 sprintf(str + len, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
173 guid.b[3], guid.b[2], guid.b[1], guid.b[0],
174 guid.b[5], guid.b[4], guid.b[7], guid.b[6],
175 guid.b[8], guid.b[9], guid.b[10], guid.b[11],
176 guid.b[12], guid.b[13], guid.b[14], guid.b[15]);
177 }
178
179 /* USB is special because the bcdDevice can be matched against a numeric range */
180 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
do_usb_entry(void * symval,unsigned int bcdDevice_initial,int bcdDevice_initial_digits,unsigned char range_lo,unsigned char range_hi,unsigned char max,struct module * mod)181 static void do_usb_entry(void *symval,
182 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
183 unsigned char range_lo, unsigned char range_hi,
184 unsigned char max, struct module *mod)
185 {
186 char alias[500];
187 DEF_FIELD(symval, usb_device_id, match_flags);
188 DEF_FIELD(symval, usb_device_id, idVendor);
189 DEF_FIELD(symval, usb_device_id, idProduct);
190 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
191 DEF_FIELD(symval, usb_device_id, bDeviceClass);
192 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
193 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
194 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
195 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
196 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
197 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
198
199 strcpy(alias, "usb:");
200 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
201 idVendor);
202 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
203 idProduct);
204
205 strcat(alias, "d");
206 if (bcdDevice_initial_digits)
207 sprintf(alias + strlen(alias), "%0*X",
208 bcdDevice_initial_digits, bcdDevice_initial);
209 if (range_lo == range_hi)
210 sprintf(alias + strlen(alias), "%X", range_lo);
211 else if (range_lo > 0 || range_hi < max) {
212 if (range_lo > 0x9 || range_hi < 0xA)
213 sprintf(alias + strlen(alias),
214 "[%X-%X]",
215 range_lo,
216 range_hi);
217 else {
218 sprintf(alias + strlen(alias),
219 range_lo < 0x9 ? "[%X-9" : "[%X",
220 range_lo);
221 sprintf(alias + strlen(alias),
222 range_hi > 0xA ? "A-%X]" : "%X]",
223 range_hi);
224 }
225 }
226 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
227 strcat(alias, "*");
228
229 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
230 bDeviceClass);
231 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
232 bDeviceSubClass);
233 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
234 bDeviceProtocol);
235 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
236 bInterfaceClass);
237 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
238 bInterfaceSubClass);
239 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
240 bInterfaceProtocol);
241 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
242 bInterfaceNumber);
243
244 module_alias_printf(mod, true, "%s", alias);
245 }
246
247 /* Handles increment/decrement of BCD formatted integers */
248 /* Returns the previous value, so it works like i++ or i-- */
incbcd(unsigned int * bcd,int inc,unsigned char max,size_t chars)249 static unsigned int incbcd(unsigned int *bcd,
250 int inc,
251 unsigned char max,
252 size_t chars)
253 {
254 unsigned int init = *bcd, i, j;
255 unsigned long long c, dec = 0;
256
257 /* If bcd is not in BCD format, just increment */
258 if (max > 0x9) {
259 *bcd += inc;
260 return init;
261 }
262
263 /* Convert BCD to Decimal */
264 for (i=0 ; i < chars ; i++) {
265 c = (*bcd >> (i << 2)) & 0xf;
266 c = c > 9 ? 9 : c; /* force to bcd just in case */
267 for (j=0 ; j < i ; j++)
268 c = c * 10;
269 dec += c;
270 }
271
272 /* Do our increment/decrement */
273 dec += inc;
274 *bcd = 0;
275
276 /* Convert back to BCD */
277 for (i=0 ; i < chars ; i++) {
278 for (c=1,j=0 ; j < i ; j++)
279 c = c * 10;
280 c = (dec / c) % 10;
281 *bcd += c << (i << 2);
282 }
283 return init;
284 }
285
do_usb_entry_multi(struct module * mod,void * symval)286 static void do_usb_entry_multi(struct module *mod, void *symval)
287 {
288 unsigned int devlo, devhi;
289 unsigned char chi, clo, max;
290 int ndigits;
291
292 DEF_FIELD(symval, usb_device_id, match_flags);
293 DEF_FIELD(symval, usb_device_id, idVendor);
294 DEF_FIELD(symval, usb_device_id, idProduct);
295 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
296 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
297 DEF_FIELD(symval, usb_device_id, bDeviceClass);
298 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
299
300 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
301 bcdDevice_lo : 0x0U;
302 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
303 bcdDevice_hi : ~0x0U;
304
305 /* Figure out if this entry is in bcd or hex format */
306 max = 0x9; /* Default to decimal format */
307 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
308 clo = (devlo >> (ndigits << 2)) & 0xf;
309 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
310 if (clo > max || chi > max) {
311 max = 0xf;
312 break;
313 }
314 }
315
316 /*
317 * Some modules (visor) have empty slots as placeholder for
318 * run-time specification that results in catch-all alias
319 */
320 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
321 return;
322
323 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
324 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
325 clo = devlo & 0xf;
326 chi = devhi & 0xf;
327 if (chi > max) /* If we are in bcd mode, truncate if necessary */
328 chi = max;
329 devlo >>= 4;
330 devhi >>= 4;
331
332 if (devlo == devhi || !ndigits) {
333 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
334 break;
335 }
336
337 if (clo > 0x0)
338 do_usb_entry(symval,
339 incbcd(&devlo, 1, max,
340 sizeof(bcdDevice_lo) * 2),
341 ndigits, clo, max, max, mod);
342
343 if (chi < max)
344 do_usb_entry(symval,
345 incbcd(&devhi, -1, max,
346 sizeof(bcdDevice_lo) * 2),
347 ndigits, 0x0, chi, max, mod);
348 }
349 }
350
do_of_entry(struct module * mod,void * symval)351 static void do_of_entry(struct module *mod, void *symval)
352 {
353 char alias[500];
354 int len;
355 char *tmp;
356
357 DEF_FIELD_ADDR(symval, of_device_id, name);
358 DEF_FIELD_ADDR(symval, of_device_id, type);
359 DEF_FIELD_ADDR(symval, of_device_id, compatible);
360
361 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
362 (*type)[0] ? *type : "*");
363
364 if ((*compatible)[0])
365 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
366 *compatible);
367
368 /* Replace all whitespace with underscores */
369 for (tmp = alias; tmp && *tmp; tmp++)
370 if (isspace(*tmp))
371 *tmp = '_';
372
373 module_alias_printf(mod, false, "%s", alias);
374 module_alias_printf(mod, false, "%sC*", alias);
375 }
376
377 /* Looks like: hid:bNvNpN */
do_hid_entry(struct module * mod,void * symval)378 static void do_hid_entry(struct module *mod, void *symval)
379 {
380 char alias[256] = {};
381
382 DEF_FIELD(symval, hid_device_id, bus);
383 DEF_FIELD(symval, hid_device_id, group);
384 DEF_FIELD(symval, hid_device_id, vendor);
385 DEF_FIELD(symval, hid_device_id, product);
386
387 ADD(alias, "b", bus != HID_BUS_ANY, bus);
388 ADD(alias, "g", group != HID_GROUP_ANY, group);
389 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
390 ADD(alias, "p", product != HID_ANY_ID, product);
391
392 module_alias_printf(mod, false, "hid:%s", alias);
393 }
394
395 /* Looks like: ieee1394:venNmoNspNverN */
do_ieee1394_entry(struct module * mod,void * symval)396 static void do_ieee1394_entry(struct module *mod, void *symval)
397 {
398 char alias[256] = {};
399
400 DEF_FIELD(symval, ieee1394_device_id, match_flags);
401 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
402 DEF_FIELD(symval, ieee1394_device_id, model_id);
403 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
404 DEF_FIELD(symval, ieee1394_device_id, version);
405
406 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
407 vendor_id);
408 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
409 model_id);
410 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
411 specifier_id);
412 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
413 version);
414
415 module_alias_printf(mod, true, "ieee1394:%s", alias);
416 }
417
418 /* Looks like: pci:vNdNsvNsdNbcNscNiN or <prefix>_pci:vNdNsvNsdNbcNscNiN. */
do_pci_entry(struct module * mod,void * symval)419 static void do_pci_entry(struct module *mod, void *symval)
420 {
421 char alias[256];
422 /* Class field can be divided into these three. */
423 unsigned char baseclass, subclass, interface,
424 baseclass_mask, subclass_mask, interface_mask;
425
426 DEF_FIELD(symval, pci_device_id, vendor);
427 DEF_FIELD(symval, pci_device_id, device);
428 DEF_FIELD(symval, pci_device_id, subvendor);
429 DEF_FIELD(symval, pci_device_id, subdevice);
430 DEF_FIELD(symval, pci_device_id, class);
431 DEF_FIELD(symval, pci_device_id, class_mask);
432 DEF_FIELD(symval, pci_device_id, override_only);
433
434 switch (override_only) {
435 case 0:
436 strcpy(alias, "pci:");
437 break;
438 case PCI_ID_F_VFIO_DRIVER_OVERRIDE:
439 strcpy(alias, "vfio_pci:");
440 break;
441 default:
442 warn("Unknown PCI driver_override alias %08X\n",
443 override_only);
444 }
445
446 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
447 ADD(alias, "d", device != PCI_ANY_ID, device);
448 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
449 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
450
451 baseclass = (class) >> 16;
452 baseclass_mask = (class_mask) >> 16;
453 subclass = (class) >> 8;
454 subclass_mask = (class_mask) >> 8;
455 interface = class;
456 interface_mask = class_mask;
457
458 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
459 || (subclass_mask != 0 && subclass_mask != 0xFF)
460 || (interface_mask != 0 && interface_mask != 0xFF)) {
461 warn("Can't handle masks in %s:%04X\n",
462 mod->name, class_mask);
463 return;
464 }
465
466 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
467 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
468 ADD(alias, "i", interface_mask == 0xFF, interface);
469
470 module_alias_printf(mod, true, "%s", alias);
471 }
472
473 /* looks like: "ccw:tNmNdtNdmN" */
do_ccw_entry(struct module * mod,void * symval)474 static void do_ccw_entry(struct module *mod, void *symval)
475 {
476 char alias[256] = {};
477
478 DEF_FIELD(symval, ccw_device_id, match_flags);
479 DEF_FIELD(symval, ccw_device_id, cu_type);
480 DEF_FIELD(symval, ccw_device_id, cu_model);
481 DEF_FIELD(symval, ccw_device_id, dev_type);
482 DEF_FIELD(symval, ccw_device_id, dev_model);
483
484 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
485 cu_type);
486 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
487 cu_model);
488 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
489 dev_type);
490 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
491 dev_model);
492
493 module_alias_printf(mod, true, "ccw:%s", alias);
494 }
495
496 /* looks like: "ap:tN" */
do_ap_entry(struct module * mod,void * symval)497 static void do_ap_entry(struct module *mod, void *symval)
498 {
499 DEF_FIELD(symval, ap_device_id, dev_type);
500
501 module_alias_printf(mod, false, "ap:t%02X*", dev_type);
502 }
503
504 /* looks like: "css:tN" */
do_css_entry(struct module * mod,void * symval)505 static void do_css_entry(struct module *mod, void *symval)
506 {
507 DEF_FIELD(symval, css_device_id, type);
508
509 module_alias_printf(mod, false, "css:t%01X", type);
510 }
511
512 /* Looks like: "serio:tyNprNidNexN" */
do_serio_entry(struct module * mod,void * symval)513 static void do_serio_entry(struct module *mod, void *symval)
514 {
515 char alias[256] = {};
516
517 DEF_FIELD(symval, serio_device_id, type);
518 DEF_FIELD(symval, serio_device_id, proto);
519 DEF_FIELD(symval, serio_device_id, id);
520 DEF_FIELD(symval, serio_device_id, extra);
521
522 ADD(alias, "ty", type != SERIO_ANY, type);
523 ADD(alias, "pr", proto != SERIO_ANY, proto);
524 ADD(alias, "id", id != SERIO_ANY, id);
525 ADD(alias, "ex", extra != SERIO_ANY, extra);
526
527 module_alias_printf(mod, true, "serio:%s", alias);
528 }
529
530 /* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
531 * "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
532 *
533 * NOTE: Each driver should use one of the following : _HID, _CIDs
534 * or _CLS. Also, bb, ss, and pp can be substituted with ??
535 * as don't care byte.
536 */
do_acpi_entry(struct module * mod,void * symval)537 static void do_acpi_entry(struct module *mod, void *symval)
538 {
539 DEF_FIELD_ADDR(symval, acpi_device_id, id);
540 DEF_FIELD(symval, acpi_device_id, cls);
541 DEF_FIELD(symval, acpi_device_id, cls_msk);
542
543 if ((*id)[0])
544 module_alias_printf(mod, false, "acpi*:%s:*", *id);
545 else {
546 char alias[256];
547 int i, byte_shift, cnt = 0;
548 unsigned int msk;
549
550 for (i = 1; i <= 3; i++) {
551 byte_shift = 8 * (3-i);
552 msk = (cls_msk >> byte_shift) & 0xFF;
553 if (msk)
554 sprintf(&alias[cnt], "%02x",
555 (cls >> byte_shift) & 0xFF);
556 else
557 sprintf(&alias[cnt], "??");
558 cnt += 2;
559 }
560 module_alias_printf(mod, false, "acpi*:%s:*", alias);
561 }
562 }
563
564 /* looks like: "pnp:dD" */
do_pnp_device_entry(struct module * mod,void * symval)565 static void do_pnp_device_entry(struct module *mod, void *symval)
566 {
567 DEF_FIELD_ADDR(symval, pnp_device_id, id);
568 char acpi_id[sizeof(*id)];
569
570 /* fix broken pnp bus lowercasing */
571 for (unsigned int i = 0; i < sizeof(acpi_id); i++)
572 acpi_id[i] = toupper((*id)[i]);
573 module_alias_printf(mod, false, "pnp:d%s*", *id);
574 module_alias_printf(mod, false, "acpi*:%s:*", acpi_id);
575 }
576
577 /* looks like: "pnp:dD" for every device of the card */
do_pnp_card_entry(struct module * mod,void * symval)578 static void do_pnp_card_entry(struct module *mod, void *symval)
579 {
580 DEF_FIELD_ADDR(symval, pnp_card_device_id, devs);
581
582 for (unsigned int i = 0; i < PNP_MAX_DEVICES; i++) {
583 const char *id = (char *)(*devs)[i].id;
584 char acpi_id[PNP_ID_LEN];
585
586 if (!id[0])
587 break;
588
589 /* fix broken pnp bus lowercasing */
590 for (unsigned int j = 0; j < sizeof(acpi_id); j++)
591 acpi_id[j] = toupper(id[j]);
592
593 /* add an individual alias for every device entry */
594 module_alias_printf(mod, false, "pnp:d%s*", id);
595 module_alias_printf(mod, false, "acpi*:%s:*", acpi_id);
596 }
597 }
598
599 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
do_pcmcia_entry(struct module * mod,void * symval)600 static void do_pcmcia_entry(struct module *mod, void *symval)
601 {
602 char alias[256] = {};
603 unsigned int i;
604 DEF_FIELD(symval, pcmcia_device_id, match_flags);
605 DEF_FIELD(symval, pcmcia_device_id, manf_id);
606 DEF_FIELD(symval, pcmcia_device_id, card_id);
607 DEF_FIELD(symval, pcmcia_device_id, func_id);
608 DEF_FIELD(symval, pcmcia_device_id, function);
609 DEF_FIELD(symval, pcmcia_device_id, device_no);
610 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
611
612 for (i=0; i<4; i++) {
613 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
614 }
615
616 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
617 manf_id);
618 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
619 card_id);
620 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
621 func_id);
622 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
623 function);
624 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
625 device_no);
626 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
627 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
628 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
629 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
630
631 module_alias_printf(mod, true, "pcmcia:%s", alias);
632 }
633
do_vio_entry(struct module * mod,void * symval)634 static void do_vio_entry(struct module *mod, void *symval)
635 {
636 char alias[256];
637 char *tmp;
638 DEF_FIELD_ADDR(symval, vio_device_id, type);
639 DEF_FIELD_ADDR(symval, vio_device_id, compat);
640
641 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
642 (*compat)[0] ? *compat : "*");
643
644 /* Replace all whitespace with underscores */
645 for (tmp = alias; tmp && *tmp; tmp++)
646 if (isspace (*tmp))
647 *tmp = '_';
648
649 module_alias_printf(mod, true, "%s", alias);
650 }
651
do_input(char * alias,kernel_ulong_t * arr,unsigned int min,unsigned int max)652 static void do_input(char *alias,
653 kernel_ulong_t *arr, unsigned int min, unsigned int max)
654 {
655 unsigned int i;
656
657 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
658 arr[i] = TO_NATIVE(arr[i]);
659 for (i = min; i < max; i++)
660 if (arr[i / BITS_PER_LONG] & (1ULL << (i%BITS_PER_LONG)))
661 sprintf(alias + strlen(alias), "%X,*", i);
662 }
663
664 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
do_input_entry(struct module * mod,void * symval)665 static void do_input_entry(struct module *mod, void *symval)
666 {
667 char alias[256] = {};
668
669 DEF_FIELD(symval, input_device_id, flags);
670 DEF_FIELD(symval, input_device_id, bustype);
671 DEF_FIELD(symval, input_device_id, vendor);
672 DEF_FIELD(symval, input_device_id, product);
673 DEF_FIELD(symval, input_device_id, version);
674 DEF_FIELD_ADDR(symval, input_device_id, evbit);
675 DEF_FIELD_ADDR(symval, input_device_id, keybit);
676 DEF_FIELD_ADDR(symval, input_device_id, relbit);
677 DEF_FIELD_ADDR(symval, input_device_id, absbit);
678 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
679 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
680 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
681 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
682 DEF_FIELD_ADDR(symval, input_device_id, swbit);
683
684 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
685 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
686 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
687 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
688
689 sprintf(alias + strlen(alias), "-e*");
690 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
691 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
692 sprintf(alias + strlen(alias), "k*");
693 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
694 do_input(alias, *keybit,
695 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
696 INPUT_DEVICE_ID_KEY_MAX);
697 sprintf(alias + strlen(alias), "r*");
698 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
699 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
700 sprintf(alias + strlen(alias), "a*");
701 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
702 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
703 sprintf(alias + strlen(alias), "m*");
704 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
705 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
706 sprintf(alias + strlen(alias), "l*");
707 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
708 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
709 sprintf(alias + strlen(alias), "s*");
710 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
711 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
712 sprintf(alias + strlen(alias), "f*");
713 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
714 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
715 sprintf(alias + strlen(alias), "w*");
716 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
717 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
718
719 module_alias_printf(mod, false, "input:%s", alias);
720 }
721
do_eisa_entry(struct module * mod,void * symval)722 static void do_eisa_entry(struct module *mod, void *symval)
723 {
724 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
725 module_alias_printf(mod, false, EISA_DEVICE_MODALIAS_FMT "*", *sig);
726 }
727
728 /* Looks like: parisc:tNhvNrevNsvN */
do_parisc_entry(struct module * mod,void * symval)729 static void do_parisc_entry(struct module *mod, void *symval)
730 {
731 char alias[256] = {};
732
733 DEF_FIELD(symval, parisc_device_id, hw_type);
734 DEF_FIELD(symval, parisc_device_id, hversion);
735 DEF_FIELD(symval, parisc_device_id, hversion_rev);
736 DEF_FIELD(symval, parisc_device_id, sversion);
737
738 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
739 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
740 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
741 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
742
743 module_alias_printf(mod, true, "parisc:%s", alias);
744 }
745
746 /* Looks like: sdio:cNvNdN. */
do_sdio_entry(struct module * mod,void * symval)747 static void do_sdio_entry(struct module *mod, void *symval)
748 {
749 char alias[256] = {};
750
751 DEF_FIELD(symval, sdio_device_id, class);
752 DEF_FIELD(symval, sdio_device_id, vendor);
753 DEF_FIELD(symval, sdio_device_id, device);
754
755 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
756 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
757 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
758
759 module_alias_printf(mod, true, "sdio:%s", alias);
760 }
761
762 /* Looks like: ssb:vNidNrevN. */
do_ssb_entry(struct module * mod,void * symval)763 static void do_ssb_entry(struct module *mod, void *symval)
764 {
765 char alias[256] = {};
766
767 DEF_FIELD(symval, ssb_device_id, vendor);
768 DEF_FIELD(symval, ssb_device_id, coreid);
769 DEF_FIELD(symval, ssb_device_id, revision);
770
771 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
772 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
773 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
774
775 module_alias_printf(mod, true, "ssb:%s", alias);
776 }
777
778 /* Looks like: bcma:mNidNrevNclN. */
do_bcma_entry(struct module * mod,void * symval)779 static void do_bcma_entry(struct module *mod, void *symval)
780 {
781 char alias[256] = {};
782
783 DEF_FIELD(symval, bcma_device_id, manuf);
784 DEF_FIELD(symval, bcma_device_id, id);
785 DEF_FIELD(symval, bcma_device_id, rev);
786 DEF_FIELD(symval, bcma_device_id, class);
787
788 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
789 ADD(alias, "id", id != BCMA_ANY_ID, id);
790 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
791 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
792
793 module_alias_printf(mod, true, "bcma:%s", alias);
794 }
795
796 /* Looks like: virtio:dNvN */
do_virtio_entry(struct module * mod,void * symval)797 static void do_virtio_entry(struct module *mod, void *symval)
798 {
799 char alias[256] = {};
800
801 DEF_FIELD(symval, virtio_device_id, device);
802 DEF_FIELD(symval, virtio_device_id, vendor);
803
804 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
805 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
806
807 module_alias_printf(mod, true, "virtio:%s", alias);
808 }
809
810 /*
811 * Looks like: vmbus:guid
812 * Each byte of the guid will be represented by two hex characters
813 * in the name.
814 */
815
do_vmbus_entry(struct module * mod,void * symval)816 static void do_vmbus_entry(struct module *mod, void *symval)
817 {
818 int i;
819 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
820 char guid_name[(sizeof(*guid) + 1) * 2];
821
822 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
823 sprintf(&guid_name[i], "%02x", TO_NATIVE((guid->b)[i/2]));
824
825 module_alias_printf(mod, false, "vmbus:%s", guid_name);
826 }
827
828 /* Looks like: rpmsg:S */
do_rpmsg_entry(struct module * mod,void * symval)829 static void do_rpmsg_entry(struct module *mod, void *symval)
830 {
831 DEF_FIELD_ADDR(symval, rpmsg_device_id, name);
832
833 module_alias_printf(mod, false, RPMSG_DEVICE_MODALIAS_FMT, *name);
834 }
835
836 /* Looks like: i2c:S */
do_i2c_entry(struct module * mod,void * symval)837 static void do_i2c_entry(struct module *mod, void *symval)
838 {
839 DEF_FIELD_ADDR(symval, i2c_device_id, name);
840
841 module_alias_printf(mod, false, I2C_MODULE_PREFIX "%s", *name);
842 }
843
do_i3c_entry(struct module * mod,void * symval)844 static void do_i3c_entry(struct module *mod, void *symval)
845 {
846 char alias[256] = {};
847
848 DEF_FIELD(symval, i3c_device_id, match_flags);
849 DEF_FIELD(symval, i3c_device_id, dcr);
850 DEF_FIELD(symval, i3c_device_id, manuf_id);
851 DEF_FIELD(symval, i3c_device_id, part_id);
852 DEF_FIELD(symval, i3c_device_id, extra_info);
853
854 ADD(alias, "dcr", match_flags & I3C_MATCH_DCR, dcr);
855 ADD(alias, "manuf", match_flags & I3C_MATCH_MANUF, manuf_id);
856 ADD(alias, "part", match_flags & I3C_MATCH_PART, part_id);
857 ADD(alias, "ext", match_flags & I3C_MATCH_EXTRA_INFO, extra_info);
858
859 module_alias_printf(mod, false, "i3c:%s", alias);
860 }
861
do_slim_entry(struct module * mod,void * symval)862 static void do_slim_entry(struct module *mod, void *symval)
863 {
864 DEF_FIELD(symval, slim_device_id, manf_id);
865 DEF_FIELD(symval, slim_device_id, prod_code);
866
867 module_alias_printf(mod, false, "slim:%x:%x:*", manf_id, prod_code);
868 }
869
870 /* Looks like: spi:S */
do_spi_entry(struct module * mod,void * symval)871 static void do_spi_entry(struct module *mod, void *symval)
872 {
873 DEF_FIELD_ADDR(symval, spi_device_id, name);
874
875 module_alias_printf(mod, false, SPI_MODULE_PREFIX "%s", *name);
876 }
877
878 static const struct dmifield {
879 const char *prefix;
880 int field;
881 } dmi_fields[] = {
882 { "bvn", DMI_BIOS_VENDOR },
883 { "bvr", DMI_BIOS_VERSION },
884 { "bd", DMI_BIOS_DATE },
885 { "br", DMI_BIOS_RELEASE },
886 { "efr", DMI_EC_FIRMWARE_RELEASE },
887 { "svn", DMI_SYS_VENDOR },
888 { "pn", DMI_PRODUCT_NAME },
889 { "pvr", DMI_PRODUCT_VERSION },
890 { "rvn", DMI_BOARD_VENDOR },
891 { "rn", DMI_BOARD_NAME },
892 { "rvr", DMI_BOARD_VERSION },
893 { "cvn", DMI_CHASSIS_VENDOR },
894 { "ct", DMI_CHASSIS_TYPE },
895 { "cvr", DMI_CHASSIS_VERSION },
896 { NULL, DMI_NONE }
897 };
898
dmi_ascii_filter(char * d,const char * s)899 static void dmi_ascii_filter(char *d, const char *s)
900 {
901 /* Filter out characters we don't want to see in the modalias string */
902 for (; *s; s++)
903 if (*s > ' ' && *s < 127 && *s != ':')
904 *(d++) = *s;
905
906 *d = 0;
907 }
908
909
do_dmi_entry(struct module * mod,void * symval)910 static void do_dmi_entry(struct module *mod, void *symval)
911 {
912 char alias[256] = {};
913 int i, j;
914 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
915
916 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
917 for (j = 0; j < 4; j++) {
918 if ((*matches)[j].slot &&
919 (*matches)[j].slot == dmi_fields[i].field) {
920 sprintf(alias + strlen(alias), ":%s*",
921 dmi_fields[i].prefix);
922 dmi_ascii_filter(alias + strlen(alias),
923 (*matches)[j].substr);
924 strcat(alias, "*");
925 }
926 }
927 }
928
929 module_alias_printf(mod, false, "dmi*%s:", alias);
930 }
931
do_platform_entry(struct module * mod,void * symval)932 static void do_platform_entry(struct module *mod, void *symval)
933 {
934 DEF_FIELD_ADDR(symval, platform_device_id, name);
935
936 module_alias_printf(mod, false, PLATFORM_MODULE_PREFIX "%s", *name);
937 }
938
do_mdio_entry(struct module * mod,void * symval)939 static void do_mdio_entry(struct module *mod, void *symval)
940 {
941 char id[33];
942 int i;
943 DEF_FIELD(symval, mdio_device_id, phy_id);
944 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
945
946 for (i = 0; i < 32; i++) {
947 if (!((phy_id_mask >> (31-i)) & 1))
948 id[i] = '?';
949 else if ((phy_id >> (31-i)) & 1)
950 id[i] = '1';
951 else
952 id[i] = '0';
953 }
954
955 /* Terminate the string */
956 id[32] = '\0';
957
958 module_alias_printf(mod, false, MDIO_MODULE_PREFIX "%s", id);
959 }
960
961 /* Looks like: zorro:iN. */
do_zorro_entry(struct module * mod,void * symval)962 static void do_zorro_entry(struct module *mod, void *symval)
963 {
964 char alias[256] = {};
965 DEF_FIELD(symval, zorro_device_id, id);
966
967 ADD(alias, "i", id != ZORRO_WILDCARD, id);
968
969 module_alias_printf(mod, false, "zorro:%s", alias);
970 }
971
972 /* looks like: "pnp:dD" */
do_isapnp_entry(struct module * mod,void * symval)973 static void do_isapnp_entry(struct module *mod, void *symval)
974 {
975 DEF_FIELD(symval, isapnp_device_id, vendor);
976 DEF_FIELD(symval, isapnp_device_id, function);
977 module_alias_printf(mod, false, "pnp:d%c%c%c%x%x%x%x*",
978 'A' + ((vendor >> 2) & 0x3f) - 1,
979 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
980 'A' + ((vendor >> 8) & 0x1f) - 1,
981 (function >> 4) & 0x0f, function & 0x0f,
982 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
983 }
984
985 /* Looks like: "ipack:fNvNdN". */
do_ipack_entry(struct module * mod,void * symval)986 static void do_ipack_entry(struct module *mod, void *symval)
987 {
988 char alias[256] = {};
989 DEF_FIELD(symval, ipack_device_id, format);
990 DEF_FIELD(symval, ipack_device_id, vendor);
991 DEF_FIELD(symval, ipack_device_id, device);
992
993 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
994 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
995 ADD(alias, "d", device != IPACK_ANY_ID, device);
996
997 module_alias_printf(mod, true, "ipack:%s", alias);
998 }
999
1000 /*
1001 * Append a match expression for a single masked hex digit.
1002 * outp points to a pointer to the character at which to append.
1003 * *outp is updated on return to point just after the appended text,
1004 * to facilitate further appending.
1005 */
append_nibble_mask(char ** outp,unsigned int nibble,unsigned int mask)1006 static void append_nibble_mask(char **outp,
1007 unsigned int nibble, unsigned int mask)
1008 {
1009 char *p = *outp;
1010 unsigned int i;
1011
1012 switch (mask) {
1013 case 0:
1014 *p++ = '?';
1015 break;
1016
1017 case 0xf:
1018 p += sprintf(p, "%X", nibble);
1019 break;
1020
1021 default:
1022 /*
1023 * Dumbly emit a match pattern for all possible matching
1024 * digits. This could be improved in some cases using ranges,
1025 * but it has the advantage of being trivially correct, and is
1026 * often optimal.
1027 */
1028 *p++ = '[';
1029 for (i = 0; i < 0x10; i++)
1030 if ((i & mask) == nibble)
1031 p += sprintf(p, "%X", i);
1032 *p++ = ']';
1033 }
1034
1035 /* Ensure that the string remains NUL-terminated: */
1036 *p = '\0';
1037
1038 /* Advance the caller's end-of-string pointer: */
1039 *outp = p;
1040 }
1041
1042 /*
1043 * looks like: "amba:dN"
1044 *
1045 * N is exactly 8 digits, where each is an upper-case hex digit, or
1046 * a ? or [] pattern matching exactly one digit.
1047 */
do_amba_entry(struct module * mod,void * symval)1048 static void do_amba_entry(struct module *mod, void *symval)
1049 {
1050 char alias[256];
1051 unsigned int digit;
1052 char *p = alias;
1053 DEF_FIELD(symval, amba_id, id);
1054 DEF_FIELD(symval, amba_id, mask);
1055
1056 if ((id & mask) != id)
1057 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: id=0x%08X, mask=0x%08X. Please fix this driver.\n",
1058 mod->name, id, mask);
1059
1060 for (digit = 0; digit < 8; digit++)
1061 append_nibble_mask(&p,
1062 (id >> (4 * (7 - digit))) & 0xf,
1063 (mask >> (4 * (7 - digit))) & 0xf);
1064
1065 module_alias_printf(mod, false, "amba:d%s", alias);
1066 }
1067
1068 /*
1069 * looks like: "mipscdmm:tN"
1070 *
1071 * N is exactly 2 digits, where each is an upper-case hex digit, or
1072 * a ? or [] pattern matching exactly one digit.
1073 */
do_mips_cdmm_entry(struct module * mod,void * symval)1074 static void do_mips_cdmm_entry(struct module *mod, void *symval)
1075 {
1076 DEF_FIELD(symval, mips_cdmm_device_id, type);
1077
1078 module_alias_printf(mod, false, "mipscdmm:t%02X*", type);
1079 }
1080
1081 /* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
1082 * All fields are numbers. It would be nicer to use strings for vendor
1083 * and feature, but getting those out of the build system here is too
1084 * complicated.
1085 */
1086
do_x86cpu_entry(struct module * mod,void * symval)1087 static void do_x86cpu_entry(struct module *mod, void *symval)
1088 {
1089 char alias[256] = {};
1090
1091 DEF_FIELD(symval, x86_cpu_id, feature);
1092 DEF_FIELD(symval, x86_cpu_id, family);
1093 DEF_FIELD(symval, x86_cpu_id, model);
1094 DEF_FIELD(symval, x86_cpu_id, vendor);
1095
1096 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1097 ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1098 ADD(alias, "mod", model != X86_MODEL_ANY, model);
1099 strcat(alias, ":feature:*");
1100 if (feature != X86_FEATURE_ANY)
1101 sprintf(alias + strlen(alias), "%04X*", feature);
1102
1103 module_alias_printf(mod, false, "cpu:type:x86,%s", alias);
1104 }
1105
1106 /* LOOKS like cpu:type:*:feature:*FEAT* */
do_cpu_entry(struct module * mod,void * symval)1107 static void do_cpu_entry(struct module *mod, void *symval)
1108 {
1109 DEF_FIELD(symval, cpu_feature, feature);
1110
1111 module_alias_printf(mod, false, "cpu:type:*:feature:*%04X*", feature);
1112 }
1113
1114 /* Looks like: mei:S:uuid:N:* */
do_mei_entry(struct module * mod,void * symval)1115 static void do_mei_entry(struct module *mod, void *symval)
1116 {
1117 char alias[256] = {};
1118
1119 DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
1120 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
1121 DEF_FIELD(symval, mei_cl_device_id, version);
1122
1123 add_uuid(alias, *uuid);
1124 ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
1125
1126 module_alias_printf(mod, false, MEI_CL_MODULE_PREFIX "%s:%s:*",
1127 (*name)[0] ? *name : "*", alias);
1128 }
1129
1130 /* Looks like: rapidio:vNdNavNadN */
do_rio_entry(struct module * mod,void * symval)1131 static void do_rio_entry(struct module *mod, void *symval)
1132 {
1133 char alias[256] = {};
1134
1135 DEF_FIELD(symval, rio_device_id, did);
1136 DEF_FIELD(symval, rio_device_id, vid);
1137 DEF_FIELD(symval, rio_device_id, asm_did);
1138 DEF_FIELD(symval, rio_device_id, asm_vid);
1139
1140 ADD(alias, "v", vid != RIO_ANY_ID, vid);
1141 ADD(alias, "d", did != RIO_ANY_ID, did);
1142 ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1143 ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1144
1145 module_alias_printf(mod, true, "rapidio:%s", alias);
1146 }
1147
1148 /* Looks like: ulpi:vNpN */
do_ulpi_entry(struct module * mod,void * symval)1149 static void do_ulpi_entry(struct module *mod, void *symval)
1150 {
1151 DEF_FIELD(symval, ulpi_device_id, vendor);
1152 DEF_FIELD(symval, ulpi_device_id, product);
1153
1154 module_alias_printf(mod, false, "ulpi:v%04xp%04x", vendor, product);
1155 }
1156
1157 /* Looks like: hdaudio:vNrNaN */
do_hda_entry(struct module * mod,void * symval)1158 static void do_hda_entry(struct module *mod, void *symval)
1159 {
1160 char alias[256] = {};
1161
1162 DEF_FIELD(symval, hda_device_id, vendor_id);
1163 DEF_FIELD(symval, hda_device_id, rev_id);
1164 DEF_FIELD(symval, hda_device_id, api_version);
1165
1166 ADD(alias, "v", vendor_id != 0, vendor_id);
1167 ADD(alias, "r", rev_id != 0, rev_id);
1168 ADD(alias, "a", api_version != 0, api_version);
1169
1170 module_alias_printf(mod, true, "hdaudio:%s", alias);
1171 }
1172
1173 /* Looks like: sdw:mNpNvNcN */
do_sdw_entry(struct module * mod,void * symval)1174 static void do_sdw_entry(struct module *mod, void *symval)
1175 {
1176 char alias[256] = {};
1177
1178 DEF_FIELD(symval, sdw_device_id, mfg_id);
1179 DEF_FIELD(symval, sdw_device_id, part_id);
1180 DEF_FIELD(symval, sdw_device_id, sdw_version);
1181 DEF_FIELD(symval, sdw_device_id, class_id);
1182
1183 ADD(alias, "m", mfg_id != 0, mfg_id);
1184 ADD(alias, "p", part_id != 0, part_id);
1185 ADD(alias, "v", sdw_version != 0, sdw_version);
1186 ADD(alias, "c", class_id != 0, class_id);
1187
1188 module_alias_printf(mod, true, "sdw:%s", alias);
1189 }
1190
1191 /* Looks like: fsl-mc:vNdN */
do_fsl_mc_entry(struct module * mod,void * symval)1192 static void do_fsl_mc_entry(struct module *mod, void *symval)
1193 {
1194 DEF_FIELD(symval, fsl_mc_device_id, vendor);
1195 DEF_FIELD_ADDR(symval, fsl_mc_device_id, obj_type);
1196
1197 module_alias_printf(mod, false, "fsl-mc:v%08Xd%s", vendor, *obj_type);
1198 }
1199
1200 /* Looks like: tbsvc:kSpNvNrN */
do_tbsvc_entry(struct module * mod,void * symval)1201 static void do_tbsvc_entry(struct module *mod, void *symval)
1202 {
1203 char alias[256] = {};
1204
1205 DEF_FIELD(symval, tb_service_id, match_flags);
1206 DEF_FIELD_ADDR(symval, tb_service_id, protocol_key);
1207 DEF_FIELD(symval, tb_service_id, protocol_id);
1208 DEF_FIELD(symval, tb_service_id, protocol_version);
1209 DEF_FIELD(symval, tb_service_id, protocol_revision);
1210
1211 if (match_flags & TBSVC_MATCH_PROTOCOL_KEY)
1212 sprintf(alias + strlen(alias), "k%s", *protocol_key);
1213 else
1214 strcat(alias + strlen(alias), "k*");
1215 ADD(alias, "p", match_flags & TBSVC_MATCH_PROTOCOL_ID, protocol_id);
1216 ADD(alias, "v", match_flags & TBSVC_MATCH_PROTOCOL_VERSION,
1217 protocol_version);
1218 ADD(alias, "r", match_flags & TBSVC_MATCH_PROTOCOL_REVISION,
1219 protocol_revision);
1220
1221 module_alias_printf(mod, true, "tbsvc:%s", alias);
1222 }
1223
1224 /* Looks like: typec:idNmN */
do_typec_entry(struct module * mod,void * symval)1225 static void do_typec_entry(struct module *mod, void *symval)
1226 {
1227 char alias[256] = {};
1228
1229 DEF_FIELD(symval, typec_device_id, svid);
1230 DEF_FIELD(symval, typec_device_id, mode);
1231
1232 ADD(alias, "m", mode != TYPEC_ANY_MODE, mode);
1233
1234 module_alias_printf(mod, false, "typec:id%04X%s", svid, alias);
1235 }
1236
1237 /* Looks like: tee:uuid */
do_tee_entry(struct module * mod,void * symval)1238 static void do_tee_entry(struct module *mod, void *symval)
1239 {
1240 DEF_FIELD_ADDR(symval, tee_client_device_id, uuid);
1241
1242 module_alias_printf(mod, true,
1243 "tee:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1244 uuid->b[0], uuid->b[1], uuid->b[2], uuid->b[3], uuid->b[4],
1245 uuid->b[5], uuid->b[6], uuid->b[7], uuid->b[8], uuid->b[9],
1246 uuid->b[10], uuid->b[11], uuid->b[12], uuid->b[13], uuid->b[14],
1247 uuid->b[15]);
1248 }
1249
1250 /* Looks like: wmi:guid */
do_wmi_entry(struct module * mod,void * symval)1251 static void do_wmi_entry(struct module *mod, void *symval)
1252 {
1253 DEF_FIELD_ADDR(symval, wmi_device_id, guid_string);
1254
1255 if (strlen(*guid_string) != UUID_STRING_LEN) {
1256 warn("Invalid WMI device id 'wmi:%s' in '%s'\n",
1257 *guid_string, mod->name);
1258 return;
1259 }
1260
1261 module_alias_printf(mod, false, WMI_MODULE_PREFIX "%s", *guid_string);
1262 }
1263
1264 /* Looks like: mhi:S */
do_mhi_entry(struct module * mod,void * symval)1265 static void do_mhi_entry(struct module *mod, void *symval)
1266 {
1267 DEF_FIELD_ADDR(symval, mhi_device_id, chan);
1268 module_alias_printf(mod, false, MHI_DEVICE_MODALIAS_FMT, *chan);
1269 }
1270
1271 /* Looks like: mhi_ep:S */
do_mhi_ep_entry(struct module * mod,void * symval)1272 static void do_mhi_ep_entry(struct module *mod, void *symval)
1273 {
1274 DEF_FIELD_ADDR(symval, mhi_device_id, chan);
1275
1276 module_alias_printf(mod, false, MHI_EP_DEVICE_MODALIAS_FMT, *chan);
1277 }
1278
1279 /* Looks like: ishtp:{guid} */
do_ishtp_entry(struct module * mod,void * symval)1280 static void do_ishtp_entry(struct module *mod, void *symval)
1281 {
1282 char alias[256] = {};
1283 DEF_FIELD_ADDR(symval, ishtp_device_id, guid);
1284
1285 add_guid(alias, *guid);
1286
1287 module_alias_printf(mod, false, ISHTP_MODULE_PREFIX "{%s}", alias);
1288 }
1289
do_auxiliary_entry(struct module * mod,void * symval)1290 static void do_auxiliary_entry(struct module *mod, void *symval)
1291 {
1292 DEF_FIELD_ADDR(symval, auxiliary_device_id, name);
1293
1294 module_alias_printf(mod, false, AUXILIARY_MODULE_PREFIX "%s", *name);
1295 }
1296
1297 /*
1298 * Looks like: ssam:dNcNtNiNfN
1299 *
1300 * N is exactly 2 digits, where each is an upper-case hex digit.
1301 */
do_ssam_entry(struct module * mod,void * symval)1302 static void do_ssam_entry(struct module *mod, void *symval)
1303 {
1304 char alias[256] = {};
1305
1306 DEF_FIELD(symval, ssam_device_id, match_flags);
1307 DEF_FIELD(symval, ssam_device_id, domain);
1308 DEF_FIELD(symval, ssam_device_id, category);
1309 DEF_FIELD(symval, ssam_device_id, target);
1310 DEF_FIELD(symval, ssam_device_id, instance);
1311 DEF_FIELD(symval, ssam_device_id, function);
1312
1313 ADD(alias, "t", match_flags & SSAM_MATCH_TARGET, target);
1314 ADD(alias, "i", match_flags & SSAM_MATCH_INSTANCE, instance);
1315 ADD(alias, "f", match_flags & SSAM_MATCH_FUNCTION, function);
1316
1317 module_alias_printf(mod, false, "ssam:d%02Xc%02X%s",
1318 domain, category, alias);
1319 }
1320
1321 /* Looks like: dfl:tNfN */
do_dfl_entry(struct module * mod,void * symval)1322 static void do_dfl_entry(struct module *mod, void *symval)
1323 {
1324 DEF_FIELD(symval, dfl_device_id, type);
1325 DEF_FIELD(symval, dfl_device_id, feature_id);
1326
1327 module_alias_printf(mod, true, "dfl:t%04Xf%04X", type, feature_id);
1328 }
1329
1330 /* Looks like: cdx:vNdN */
do_cdx_entry(struct module * mod,void * symval)1331 static void do_cdx_entry(struct module *mod, void *symval)
1332 {
1333 char alias[256];
1334
1335 DEF_FIELD(symval, cdx_device_id, vendor);
1336 DEF_FIELD(symval, cdx_device_id, device);
1337 DEF_FIELD(symval, cdx_device_id, subvendor);
1338 DEF_FIELD(symval, cdx_device_id, subdevice);
1339 DEF_FIELD(symval, cdx_device_id, class);
1340 DEF_FIELD(symval, cdx_device_id, class_mask);
1341 DEF_FIELD(symval, cdx_device_id, override_only);
1342
1343 switch (override_only) {
1344 case 0:
1345 strcpy(alias, "cdx:");
1346 break;
1347 case CDX_ID_F_VFIO_DRIVER_OVERRIDE:
1348 strcpy(alias, "vfio_cdx:");
1349 break;
1350 default:
1351 warn("Unknown CDX driver_override alias %08X\n",
1352 override_only);
1353 return;
1354 }
1355
1356 ADD(alias, "v", vendor != CDX_ANY_ID, vendor);
1357 ADD(alias, "d", device != CDX_ANY_ID, device);
1358 ADD(alias, "sv", subvendor != CDX_ANY_ID, subvendor);
1359 ADD(alias, "sd", subdevice != CDX_ANY_ID, subdevice);
1360 ADD(alias, "c", class_mask == 0xFFFFFF, class);
1361
1362 module_alias_printf(mod, false, "%s", alias);
1363 }
1364
do_vchiq_entry(struct module * mod,void * symval)1365 static void do_vchiq_entry(struct module *mod, void *symval)
1366 {
1367 DEF_FIELD_ADDR(symval, vchiq_device_id, name);
1368
1369 module_alias_printf(mod, false, "vchiq:%s", *name);
1370 }
1371
1372 /* Looks like: coreboot:tN */
do_coreboot_entry(struct module * mod,void * symval)1373 static void do_coreboot_entry(struct module *mod, void *symval)
1374 {
1375 DEF_FIELD(symval, coreboot_device_id, tag);
1376
1377 module_alias_printf(mod, false, "coreboot:t%08X", tag);
1378 }
1379
1380 /* Does namelen bytes of name exactly match the symbol? */
sym_is(const char * name,unsigned namelen,const char * symbol)1381 static bool sym_is(const char *name, unsigned namelen, const char *symbol)
1382 {
1383 if (namelen != strlen(symbol))
1384 return false;
1385
1386 return memcmp(name, symbol, namelen) == 0;
1387 }
1388
do_table(const char * name,void * symval,unsigned long size,unsigned long id_size,const char * device_id,void (* do_entry)(struct module * mod,void * symval),struct module * mod)1389 static void do_table(const char *name, void *symval, unsigned long size,
1390 unsigned long id_size,
1391 const char *device_id,
1392 void (*do_entry)(struct module *mod, void *symval),
1393 struct module *mod)
1394 {
1395 unsigned int i;
1396
1397 if (size % id_size || size < id_size) {
1398 error("%s: type mismatch between %s[] and MODULE_DEVICE_TABLE(%s, ...)\n",
1399 mod->name, name, device_id);
1400 return;
1401 }
1402
1403 /* Verify the last entry is a terminator */
1404 for (i = size - id_size; i < size; i++) {
1405 if (*(uint8_t *)(symval + i)) {
1406 error("%s: %s[] is not terminated with a NULL entry\n",
1407 mod->name, name);
1408 return;
1409 }
1410 }
1411
1412 /* Leave last one: it's the terminator. */
1413 size -= id_size;
1414
1415 for (i = 0; i < size; i += id_size)
1416 do_entry(mod, symval + i);
1417 }
1418
1419 static const struct devtable devtable[] = {
1420 {"hid", SIZE_hid_device_id, do_hid_entry},
1421 {"ieee1394", SIZE_ieee1394_device_id, do_ieee1394_entry},
1422 {"pci", SIZE_pci_device_id, do_pci_entry},
1423 {"ccw", SIZE_ccw_device_id, do_ccw_entry},
1424 {"ap", SIZE_ap_device_id, do_ap_entry},
1425 {"css", SIZE_css_device_id, do_css_entry},
1426 {"serio", SIZE_serio_device_id, do_serio_entry},
1427 {"acpi", SIZE_acpi_device_id, do_acpi_entry},
1428 {"pcmcia", SIZE_pcmcia_device_id, do_pcmcia_entry},
1429 {"vio", SIZE_vio_device_id, do_vio_entry},
1430 {"input", SIZE_input_device_id, do_input_entry},
1431 {"eisa", SIZE_eisa_device_id, do_eisa_entry},
1432 {"parisc", SIZE_parisc_device_id, do_parisc_entry},
1433 {"sdio", SIZE_sdio_device_id, do_sdio_entry},
1434 {"ssb", SIZE_ssb_device_id, do_ssb_entry},
1435 {"bcma", SIZE_bcma_device_id, do_bcma_entry},
1436 {"virtio", SIZE_virtio_device_id, do_virtio_entry},
1437 {"vmbus", SIZE_hv_vmbus_device_id, do_vmbus_entry},
1438 {"rpmsg", SIZE_rpmsg_device_id, do_rpmsg_entry},
1439 {"i2c", SIZE_i2c_device_id, do_i2c_entry},
1440 {"i3c", SIZE_i3c_device_id, do_i3c_entry},
1441 {"slim", SIZE_slim_device_id, do_slim_entry},
1442 {"spi", SIZE_spi_device_id, do_spi_entry},
1443 {"dmi", SIZE_dmi_system_id, do_dmi_entry},
1444 {"platform", SIZE_platform_device_id, do_platform_entry},
1445 {"mdio", SIZE_mdio_device_id, do_mdio_entry},
1446 {"zorro", SIZE_zorro_device_id, do_zorro_entry},
1447 {"isapnp", SIZE_isapnp_device_id, do_isapnp_entry},
1448 {"ipack", SIZE_ipack_device_id, do_ipack_entry},
1449 {"amba", SIZE_amba_id, do_amba_entry},
1450 {"mipscdmm", SIZE_mips_cdmm_device_id, do_mips_cdmm_entry},
1451 {"x86cpu", SIZE_x86_cpu_id, do_x86cpu_entry},
1452 {"cpu", SIZE_cpu_feature, do_cpu_entry},
1453 {"mei", SIZE_mei_cl_device_id, do_mei_entry},
1454 {"rapidio", SIZE_rio_device_id, do_rio_entry},
1455 {"ulpi", SIZE_ulpi_device_id, do_ulpi_entry},
1456 {"hdaudio", SIZE_hda_device_id, do_hda_entry},
1457 {"sdw", SIZE_sdw_device_id, do_sdw_entry},
1458 {"fslmc", SIZE_fsl_mc_device_id, do_fsl_mc_entry},
1459 {"tbsvc", SIZE_tb_service_id, do_tbsvc_entry},
1460 {"typec", SIZE_typec_device_id, do_typec_entry},
1461 {"tee", SIZE_tee_client_device_id, do_tee_entry},
1462 {"wmi", SIZE_wmi_device_id, do_wmi_entry},
1463 {"mhi", SIZE_mhi_device_id, do_mhi_entry},
1464 {"mhi_ep", SIZE_mhi_device_id, do_mhi_ep_entry},
1465 {"auxiliary", SIZE_auxiliary_device_id, do_auxiliary_entry},
1466 {"ssam", SIZE_ssam_device_id, do_ssam_entry},
1467 {"dfl", SIZE_dfl_device_id, do_dfl_entry},
1468 {"ishtp", SIZE_ishtp_device_id, do_ishtp_entry},
1469 {"cdx", SIZE_cdx_device_id, do_cdx_entry},
1470 {"vchiq", SIZE_vchiq_device_id, do_vchiq_entry},
1471 {"coreboot", SIZE_coreboot_device_id, do_coreboot_entry},
1472 {"of", SIZE_of_device_id, do_of_entry},
1473 {"usb", SIZE_usb_device_id, do_usb_entry_multi},
1474 {"pnp", SIZE_pnp_device_id, do_pnp_device_entry},
1475 {"pnp_card", SIZE_pnp_card_device_id, do_pnp_card_entry},
1476 };
1477
1478 /* Create MODULE_ALIAS() statements.
1479 * At this time, we cannot write the actual output C source yet,
1480 * so we write into the mod->dev_table_buf buffer. */
handle_moddevtable(struct module * mod,struct elf_info * info,Elf_Sym * sym,const char * symname)1481 void handle_moddevtable(struct module *mod, struct elf_info *info,
1482 Elf_Sym *sym, const char *symname)
1483 {
1484 void *symval;
1485 char *zeros = NULL;
1486 const char *type, *name;
1487 size_t typelen;
1488 static const char *prefix = "__mod_device_table__";
1489
1490 /* We're looking for a section relative symbol */
1491 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
1492 return;
1493
1494 /* We're looking for an object */
1495 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1496 return;
1497
1498 /* All our symbols are of form __mod_device_table__<type>__<name>. */
1499 if (!strstarts(symname, prefix))
1500 return;
1501 type = symname + strlen(prefix);
1502
1503 name = strstr(type, "__");
1504 if (!name)
1505 return;
1506 typelen = name - type;
1507 name += strlen("__");
1508
1509 /* Handle all-NULL symbols allocated into .bss */
1510 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
1511 zeros = calloc(1, sym->st_size);
1512 symval = zeros;
1513 } else {
1514 symval = sym_get_data(info, sym);
1515 }
1516
1517 for (int i = 0; i < ARRAY_SIZE(devtable); i++) {
1518 const struct devtable *p = &devtable[i];
1519
1520 if (sym_is(type, typelen, p->device_id)) {
1521 do_table(name, symval, sym->st_size, p->id_size,
1522 p->device_id, p->do_entry, mod);
1523 break;
1524 }
1525 }
1526
1527 free(zeros);
1528 }
1529