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