• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 typedef struct {
38 	__u8 b[16];
39 } uuid_le;
40 
41 /* Big exception to the "don't include kernel headers into userspace, which
42  * even potentially has different endianness and word sizes, since
43  * we handle those differences explicitly below */
44 #include "../../include/linux/mod_devicetable.h"
45 
46 /* This array collects all instances that use the generic do_table */
47 struct devtable {
48 	const char *device_id; /* name of table, __mod_<name>__*_device_table. */
49 	unsigned long id_size;
50 	int (*do_entry)(const char *filename, void *symval, char *alias);
51 };
52 
53 /* Define a variable f that holds the value of field f of struct devid
54  * based at address m.
55  */
56 #define DEF_FIELD(m, devid, f) \
57 	typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
58 /* Define a variable f that holds the address of field f of struct devid
59  * based at address m.  Due to the way typeof works, for a field of type
60  * T[N] the variable has type T(*)[N], _not_ T*.
61  */
62 #define DEF_FIELD_ADDR(m, devid, f) \
63 	typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
64 
65 #define ADD(str, sep, cond, field)                              \
66 do {                                                            \
67         strcat(str, sep);                                       \
68         if (cond)                                               \
69                 sprintf(str + strlen(str),                      \
70                         sizeof(field) == 1 ? "%02X" :           \
71                         sizeof(field) == 2 ? "%04X" :           \
72                         sizeof(field) == 4 ? "%08X" : "",       \
73                         field);                                 \
74         else                                                    \
75                 sprintf(str + strlen(str), "*");                \
76 } while(0)
77 
78 /* Always end in a wildcard, for future extension */
add_wildcard(char * str)79 static inline void add_wildcard(char *str)
80 {
81 	int len = strlen(str);
82 
83 	if (str[len - 1] != '*')
84 		strcat(str + len, "*");
85 }
86 
add_uuid(char * str,uuid_le uuid)87 static inline void add_uuid(char *str, uuid_le uuid)
88 {
89 	int len = strlen(str);
90 
91 	sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
92 		uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
93 		uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
94 		uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
95 		uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
96 }
97 
98 /**
99  * Check that sizeof(device_id type) are consistent with size of section
100  * in .o file. If in-consistent then userspace and kernel does not agree
101  * on actual size which is a bug.
102  * Also verify that the final entry in the table is all zeros.
103  * Ignore both checks if build host differ from target host and size differs.
104  **/
device_id_check(const char * modname,const char * device_id,unsigned long size,unsigned long id_size,void * symval)105 static void device_id_check(const char *modname, const char *device_id,
106 			    unsigned long size, unsigned long id_size,
107 			    void *symval)
108 {
109 	int i;
110 
111 	if (size % id_size || size < id_size) {
112 		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
113 		      "of the size of "
114 		      "section __mod_%s__<identifier>_device_table=%lu.\n"
115 		      "Fix definition of struct %s_device_id "
116 		      "in mod_devicetable.h\n",
117 		      modname, device_id, id_size, device_id, size, device_id);
118 	}
119 	/* Verify last one is a terminator */
120 	for (i = 0; i < id_size; i++ ) {
121 		if (*(uint8_t*)(symval+size-id_size+i)) {
122 			fprintf(stderr,"%s: struct %s_device_id is %lu bytes.  "
123 				"The last of %lu is:\n",
124 				modname, device_id, id_size, size / id_size);
125 			for (i = 0; i < id_size; i++ )
126 				fprintf(stderr,"0x%02x ",
127 					*(uint8_t*)(symval+size-id_size+i) );
128 			fprintf(stderr,"\n");
129 			fatal("%s: struct %s_device_id is not terminated "
130 				"with a NULL entry!\n", modname, device_id);
131 		}
132 	}
133 }
134 
135 /* USB is special because the bcdDevice can be matched against a numeric range */
136 /* 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)137 static void do_usb_entry(void *symval,
138 			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
139 			 unsigned char range_lo, unsigned char range_hi,
140 			 unsigned char max, struct module *mod)
141 {
142 	char alias[500];
143 	DEF_FIELD(symval, usb_device_id, match_flags);
144 	DEF_FIELD(symval, usb_device_id, idVendor);
145 	DEF_FIELD(symval, usb_device_id, idProduct);
146 	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
147 	DEF_FIELD(symval, usb_device_id, bDeviceClass);
148 	DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
149 	DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
150 	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
151 	DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
152 	DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
153 	DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
154 
155 	strcpy(alias, "usb:");
156 	ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
157 	    idVendor);
158 	ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
159 	    idProduct);
160 
161 	strcat(alias, "d");
162 	if (bcdDevice_initial_digits)
163 		sprintf(alias + strlen(alias), "%0*X",
164 			bcdDevice_initial_digits, bcdDevice_initial);
165 	if (range_lo == range_hi)
166 		sprintf(alias + strlen(alias), "%X", range_lo);
167 	else if (range_lo > 0 || range_hi < max) {
168 		if (range_lo > 0x9 || range_hi < 0xA)
169 			sprintf(alias + strlen(alias),
170 				"[%X-%X]",
171 				range_lo,
172 				range_hi);
173 		else {
174 			sprintf(alias + strlen(alias),
175 				range_lo < 0x9 ? "[%X-9" : "[%X",
176 				range_lo);
177 			sprintf(alias + strlen(alias),
178 				range_hi > 0xA ? "A-%X]" : "%X]",
179 				range_hi);
180 		}
181 	}
182 	if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
183 		strcat(alias, "*");
184 
185 	ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
186 	    bDeviceClass);
187 	ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
188 	    bDeviceSubClass);
189 	ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
190 	    bDeviceProtocol);
191 	ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
192 	    bInterfaceClass);
193 	ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
194 	    bInterfaceSubClass);
195 	ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
196 	    bInterfaceProtocol);
197 	ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
198 	    bInterfaceNumber);
199 
200 	add_wildcard(alias);
201 	buf_printf(&mod->dev_table_buf,
202 		   "MODULE_ALIAS(\"%s\");\n", alias);
203 }
204 
205 /* Handles increment/decrement of BCD formatted integers */
206 /* Returns the previous value, so it works like i++ or i-- */
incbcd(unsigned int * bcd,int inc,unsigned char max,size_t chars)207 static unsigned int incbcd(unsigned int *bcd,
208 			   int inc,
209 			   unsigned char max,
210 			   size_t chars)
211 {
212 	unsigned int init = *bcd, i, j;
213 	unsigned long long c, dec = 0;
214 
215 	/* If bcd is not in BCD format, just increment */
216 	if (max > 0x9) {
217 		*bcd += inc;
218 		return init;
219 	}
220 
221 	/* Convert BCD to Decimal */
222 	for (i=0 ; i < chars ; i++) {
223 		c = (*bcd >> (i << 2)) & 0xf;
224 		c = c > 9 ? 9 : c; /* force to bcd just in case */
225 		for (j=0 ; j < i ; j++)
226 			c = c * 10;
227 		dec += c;
228 	}
229 
230 	/* Do our increment/decrement */
231 	dec += inc;
232 	*bcd  = 0;
233 
234 	/* Convert back to BCD */
235 	for (i=0 ; i < chars ; i++) {
236 		for (c=1,j=0 ; j < i ; j++)
237 			c = c * 10;
238 		c = (dec / c) % 10;
239 		*bcd += c << (i << 2);
240 	}
241 	return init;
242 }
243 
do_usb_entry_multi(void * symval,struct module * mod)244 static void do_usb_entry_multi(void *symval, struct module *mod)
245 {
246 	unsigned int devlo, devhi;
247 	unsigned char chi, clo, max;
248 	int ndigits;
249 
250 	DEF_FIELD(symval, usb_device_id, match_flags);
251 	DEF_FIELD(symval, usb_device_id, idVendor);
252 	DEF_FIELD(symval, usb_device_id, idProduct);
253 	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
254 	DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
255 	DEF_FIELD(symval, usb_device_id, bDeviceClass);
256 	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
257 
258 	devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
259 		bcdDevice_lo : 0x0U;
260 	devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
261 		bcdDevice_hi : ~0x0U;
262 
263 	/* Figure out if this entry is in bcd or hex format */
264 	max = 0x9; /* Default to decimal format */
265 	for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
266 		clo = (devlo >> (ndigits << 2)) & 0xf;
267 		chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
268 		if (clo > max || chi > max) {
269 			max = 0xf;
270 			break;
271 		}
272 	}
273 
274 	/*
275 	 * Some modules (visor) have empty slots as placeholder for
276 	 * run-time specification that results in catch-all alias
277 	 */
278 	if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
279 		return;
280 
281 	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
282 	for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
283 		clo = devlo & 0xf;
284 		chi = devhi & 0xf;
285 		if (chi > max)	/* If we are in bcd mode, truncate if necessary */
286 			chi = max;
287 		devlo >>= 4;
288 		devhi >>= 4;
289 
290 		if (devlo == devhi || !ndigits) {
291 			do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
292 			break;
293 		}
294 
295 		if (clo > 0x0)
296 			do_usb_entry(symval,
297 				     incbcd(&devlo, 1, max,
298 					    sizeof(bcdDevice_lo) * 2),
299 				     ndigits, clo, max, max, mod);
300 
301 		if (chi < max)
302 			do_usb_entry(symval,
303 				     incbcd(&devhi, -1, max,
304 					    sizeof(bcdDevice_lo) * 2),
305 				     ndigits, 0x0, chi, max, mod);
306 	}
307 }
308 
do_usb_table(void * symval,unsigned long size,struct module * mod)309 static void do_usb_table(void *symval, unsigned long size,
310 			 struct module *mod)
311 {
312 	unsigned int i;
313 	const unsigned long id_size = SIZE_usb_device_id;
314 
315 	device_id_check(mod->name, "usb", size, id_size, symval);
316 
317 	/* Leave last one: it's the terminator. */
318 	size -= id_size;
319 
320 	for (i = 0; i < size; i += id_size)
321 		do_usb_entry_multi(symval + i, mod);
322 }
323 
324 /* Looks like: hid:bNvNpN */
do_hid_entry(const char * filename,void * symval,char * alias)325 static int do_hid_entry(const char *filename,
326 			     void *symval, char *alias)
327 {
328 	DEF_FIELD(symval, hid_device_id, bus);
329 	DEF_FIELD(symval, hid_device_id, group);
330 	DEF_FIELD(symval, hid_device_id, vendor);
331 	DEF_FIELD(symval, hid_device_id, product);
332 
333 	sprintf(alias, "hid:");
334 	ADD(alias, "b", bus != HID_BUS_ANY, bus);
335 	ADD(alias, "g", group != HID_GROUP_ANY, group);
336 	ADD(alias, "v", vendor != HID_ANY_ID, vendor);
337 	ADD(alias, "p", product != HID_ANY_ID, product);
338 
339 	return 1;
340 }
341 
342 /* Looks like: ieee1394:venNmoNspNverN */
do_ieee1394_entry(const char * filename,void * symval,char * alias)343 static int do_ieee1394_entry(const char *filename,
344 			     void *symval, char *alias)
345 {
346 	DEF_FIELD(symval, ieee1394_device_id, match_flags);
347 	DEF_FIELD(symval, ieee1394_device_id, vendor_id);
348 	DEF_FIELD(symval, ieee1394_device_id, model_id);
349 	DEF_FIELD(symval, ieee1394_device_id, specifier_id);
350 	DEF_FIELD(symval, ieee1394_device_id, version);
351 
352 	strcpy(alias, "ieee1394:");
353 	ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
354 	    vendor_id);
355 	ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
356 	    model_id);
357 	ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
358 	    specifier_id);
359 	ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
360 	    version);
361 
362 	add_wildcard(alias);
363 	return 1;
364 }
365 
366 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
do_pci_entry(const char * filename,void * symval,char * alias)367 static int do_pci_entry(const char *filename,
368 			void *symval, char *alias)
369 {
370 	/* Class field can be divided into these three. */
371 	unsigned char baseclass, subclass, interface,
372 		baseclass_mask, subclass_mask, interface_mask;
373 
374 	DEF_FIELD(symval, pci_device_id, vendor);
375 	DEF_FIELD(symval, pci_device_id, device);
376 	DEF_FIELD(symval, pci_device_id, subvendor);
377 	DEF_FIELD(symval, pci_device_id, subdevice);
378 	DEF_FIELD(symval, pci_device_id, class);
379 	DEF_FIELD(symval, pci_device_id, class_mask);
380 
381 	strcpy(alias, "pci:");
382 	ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
383 	ADD(alias, "d", device != PCI_ANY_ID, device);
384 	ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
385 	ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
386 
387 	baseclass = (class) >> 16;
388 	baseclass_mask = (class_mask) >> 16;
389 	subclass = (class) >> 8;
390 	subclass_mask = (class_mask) >> 8;
391 	interface = class;
392 	interface_mask = class_mask;
393 
394 	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
395 	    || (subclass_mask != 0 && subclass_mask != 0xFF)
396 	    || (interface_mask != 0 && interface_mask != 0xFF)) {
397 		warn("Can't handle masks in %s:%04X\n",
398 		     filename, class_mask);
399 		return 0;
400 	}
401 
402 	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
403 	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
404 	ADD(alias, "i", interface_mask == 0xFF, interface);
405 	add_wildcard(alias);
406 	return 1;
407 }
408 
409 /* looks like: "ccw:tNmNdtNdmN" */
do_ccw_entry(const char * filename,void * symval,char * alias)410 static int do_ccw_entry(const char *filename,
411 			void *symval, char *alias)
412 {
413 	DEF_FIELD(symval, ccw_device_id, match_flags);
414 	DEF_FIELD(symval, ccw_device_id, cu_type);
415 	DEF_FIELD(symval, ccw_device_id, cu_model);
416 	DEF_FIELD(symval, ccw_device_id, dev_type);
417 	DEF_FIELD(symval, ccw_device_id, dev_model);
418 
419 	strcpy(alias, "ccw:");
420 	ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
421 	    cu_type);
422 	ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
423 	    cu_model);
424 	ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
425 	    dev_type);
426 	ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
427 	    dev_model);
428 	add_wildcard(alias);
429 	return 1;
430 }
431 
432 /* looks like: "ap:tN" */
do_ap_entry(const char * filename,void * symval,char * alias)433 static int do_ap_entry(const char *filename,
434 		       void *symval, char *alias)
435 {
436 	DEF_FIELD(symval, ap_device_id, dev_type);
437 
438 	sprintf(alias, "ap:t%02X*", dev_type);
439 	return 1;
440 }
441 
442 /* looks like: "css:tN" */
do_css_entry(const char * filename,void * symval,char * alias)443 static int do_css_entry(const char *filename,
444 			void *symval, char *alias)
445 {
446 	DEF_FIELD(symval, css_device_id, type);
447 
448 	sprintf(alias, "css:t%01X", type);
449 	return 1;
450 }
451 
452 /* Looks like: "serio:tyNprNidNexN" */
do_serio_entry(const char * filename,void * symval,char * alias)453 static int do_serio_entry(const char *filename,
454 			  void *symval, char *alias)
455 {
456 	DEF_FIELD(symval, serio_device_id, type);
457 	DEF_FIELD(symval, serio_device_id, proto);
458 	DEF_FIELD(symval, serio_device_id, id);
459 	DEF_FIELD(symval, serio_device_id, extra);
460 
461 	strcpy(alias, "serio:");
462 	ADD(alias, "ty", type != SERIO_ANY, type);
463 	ADD(alias, "pr", proto != SERIO_ANY, proto);
464 	ADD(alias, "id", id != SERIO_ANY, id);
465 	ADD(alias, "ex", extra != SERIO_ANY, extra);
466 
467 	add_wildcard(alias);
468 	return 1;
469 }
470 
471 /* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
472  *             "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
473  *
474  * NOTE: Each driver should use one of the following : _HID, _CIDs
475  *       or _CLS. Also, bb, ss, and pp can be substituted with ??
476  *       as don't care byte.
477  */
do_acpi_entry(const char * filename,void * symval,char * alias)478 static int do_acpi_entry(const char *filename,
479 			void *symval, char *alias)
480 {
481 	DEF_FIELD_ADDR(symval, acpi_device_id, id);
482 	DEF_FIELD_ADDR(symval, acpi_device_id, cls);
483 	DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk);
484 
485 	if (id && strlen((const char *)*id))
486 		sprintf(alias, "acpi*:%s:*", *id);
487 	else if (cls) {
488 		int i, byte_shift, cnt = 0;
489 		unsigned int msk;
490 
491 		sprintf(&alias[cnt], "acpi*:");
492 		cnt = 6;
493 		for (i = 1; i <= 3; i++) {
494 			byte_shift = 8 * (3-i);
495 			msk = (*cls_msk >> byte_shift) & 0xFF;
496 			if (msk)
497 				sprintf(&alias[cnt], "%02x",
498 					(*cls >> byte_shift) & 0xFF);
499 			else
500 				sprintf(&alias[cnt], "??");
501 			cnt += 2;
502 		}
503 		sprintf(&alias[cnt], ":*");
504 	}
505 	return 1;
506 }
507 
508 /* looks like: "pnp:dD" */
do_pnp_device_entry(void * symval,unsigned long size,struct module * mod)509 static void do_pnp_device_entry(void *symval, unsigned long size,
510 				struct module *mod)
511 {
512 	const unsigned long id_size = SIZE_pnp_device_id;
513 	const unsigned int count = (size / id_size)-1;
514 	unsigned int i;
515 
516 	device_id_check(mod->name, "pnp", size, id_size, symval);
517 
518 	for (i = 0; i < count; i++) {
519 		DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
520 		char acpi_id[sizeof(*id)];
521 		int j;
522 
523 		buf_printf(&mod->dev_table_buf,
524 			   "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
525 
526 		/* fix broken pnp bus lowercasing */
527 		for (j = 0; j < sizeof(acpi_id); j++)
528 			acpi_id[j] = toupper((*id)[j]);
529 		buf_printf(&mod->dev_table_buf,
530 			   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
531 	}
532 }
533 
534 /* looks like: "pnp:dD" for every device of the card */
do_pnp_card_entries(void * symval,unsigned long size,struct module * mod)535 static void do_pnp_card_entries(void *symval, unsigned long size,
536 				struct module *mod)
537 {
538 	const unsigned long id_size = SIZE_pnp_card_device_id;
539 	const unsigned int count = (size / id_size)-1;
540 	unsigned int i;
541 
542 	device_id_check(mod->name, "pnp", size, id_size, symval);
543 
544 	for (i = 0; i < count; i++) {
545 		unsigned int j;
546 		DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
547 
548 		for (j = 0; j < PNP_MAX_DEVICES; j++) {
549 			const char *id = (char *)(*devs)[j].id;
550 			int i2, j2;
551 			int dup = 0;
552 
553 			if (!id[0])
554 				break;
555 
556 			/* find duplicate, already added value */
557 			for (i2 = 0; i2 < i && !dup; i2++) {
558 				DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
559 
560 				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
561 					const char *id2 = (char *)(*devs)[j2].id;
562 
563 					if (!id2[0])
564 						break;
565 
566 					if (!strcmp(id, id2)) {
567 						dup = 1;
568 						break;
569 					}
570 				}
571 			}
572 
573 			/* add an individual alias for every device entry */
574 			if (!dup) {
575 				char acpi_id[PNP_ID_LEN];
576 				int k;
577 
578 				buf_printf(&mod->dev_table_buf,
579 					   "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
580 
581 				/* fix broken pnp bus lowercasing */
582 				for (k = 0; k < sizeof(acpi_id); k++)
583 					acpi_id[k] = toupper(id[k]);
584 				buf_printf(&mod->dev_table_buf,
585 					   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
586 			}
587 		}
588 	}
589 }
590 
591 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
do_pcmcia_entry(const char * filename,void * symval,char * alias)592 static int do_pcmcia_entry(const char *filename,
593 			   void *symval, char *alias)
594 {
595 	unsigned int i;
596 	DEF_FIELD(symval, pcmcia_device_id, match_flags);
597 	DEF_FIELD(symval, pcmcia_device_id, manf_id);
598 	DEF_FIELD(symval, pcmcia_device_id, card_id);
599 	DEF_FIELD(symval, pcmcia_device_id, func_id);
600 	DEF_FIELD(symval, pcmcia_device_id, function);
601 	DEF_FIELD(symval, pcmcia_device_id, device_no);
602 	DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
603 
604 	for (i=0; i<4; i++) {
605 		(*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
606 	}
607 
608 	strcpy(alias, "pcmcia:");
609 	ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
610 	    manf_id);
611 	ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
612 	    card_id);
613 	ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
614 	    func_id);
615 	ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
616 	    function);
617 	ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
618 	    device_no);
619 	ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
620 	ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
621 	ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
622 	ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
623 
624 	add_wildcard(alias);
625 	return 1;
626 }
627 
do_of_entry(const char * filename,void * symval,char * alias)628 static int do_of_entry (const char *filename, void *symval, char *alias)
629 {
630 	int len;
631 	char *tmp;
632 	DEF_FIELD_ADDR(symval, of_device_id, name);
633 	DEF_FIELD_ADDR(symval, of_device_id, type);
634 	DEF_FIELD_ADDR(symval, of_device_id, compatible);
635 
636 	len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
637 		      (*type)[0] ? *type : "*");
638 
639 	if ((*compatible)[0])
640 		sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
641 			*compatible);
642 
643 	/* Replace all whitespace with underscores */
644 	for (tmp = alias; tmp && *tmp; tmp++)
645 		if (isspace (*tmp))
646 			*tmp = '_';
647 
648 	add_wildcard(alias);
649 	return 1;
650 }
651 
do_vio_entry(const char * filename,void * symval,char * alias)652 static int do_vio_entry(const char *filename, void *symval,
653 		char *alias)
654 {
655 	char *tmp;
656 	DEF_FIELD_ADDR(symval, vio_device_id, type);
657 	DEF_FIELD_ADDR(symval, vio_device_id, compat);
658 
659 	sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
660 			(*compat)[0] ? *compat : "*");
661 
662 	/* Replace all whitespace with underscores */
663 	for (tmp = alias; tmp && *tmp; tmp++)
664 		if (isspace (*tmp))
665 			*tmp = '_';
666 
667 	add_wildcard(alias);
668 	return 1;
669 }
670 
671 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
672 
do_input(char * alias,kernel_ulong_t * arr,unsigned int min,unsigned int max)673 static void do_input(char *alias,
674 		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
675 {
676 	unsigned int i;
677 
678 	for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
679 		arr[i] = TO_NATIVE(arr[i]);
680 	for (i = min; i < max; i++)
681 		if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
682 			sprintf(alias + strlen(alias), "%X,*", i);
683 }
684 
685 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
do_input_entry(const char * filename,void * symval,char * alias)686 static int do_input_entry(const char *filename, void *symval,
687 			  char *alias)
688 {
689 	DEF_FIELD(symval, input_device_id, flags);
690 	DEF_FIELD(symval, input_device_id, bustype);
691 	DEF_FIELD(symval, input_device_id, vendor);
692 	DEF_FIELD(symval, input_device_id, product);
693 	DEF_FIELD(symval, input_device_id, version);
694 	DEF_FIELD_ADDR(symval, input_device_id, evbit);
695 	DEF_FIELD_ADDR(symval, input_device_id, keybit);
696 	DEF_FIELD_ADDR(symval, input_device_id, relbit);
697 	DEF_FIELD_ADDR(symval, input_device_id, absbit);
698 	DEF_FIELD_ADDR(symval, input_device_id, mscbit);
699 	DEF_FIELD_ADDR(symval, input_device_id, ledbit);
700 	DEF_FIELD_ADDR(symval, input_device_id, sndbit);
701 	DEF_FIELD_ADDR(symval, input_device_id, ffbit);
702 	DEF_FIELD_ADDR(symval, input_device_id, swbit);
703 
704 	sprintf(alias, "input:");
705 
706 	ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
707 	ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
708 	ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
709 	ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
710 
711 	sprintf(alias + strlen(alias), "-e*");
712 	if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
713 		do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
714 	sprintf(alias + strlen(alias), "k*");
715 	if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
716 		do_input(alias, *keybit,
717 			 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
718 			 INPUT_DEVICE_ID_KEY_MAX);
719 	sprintf(alias + strlen(alias), "r*");
720 	if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
721 		do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
722 	sprintf(alias + strlen(alias), "a*");
723 	if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
724 		do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
725 	sprintf(alias + strlen(alias), "m*");
726 	if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
727 		do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
728 	sprintf(alias + strlen(alias), "l*");
729 	if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
730 		do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
731 	sprintf(alias + strlen(alias), "s*");
732 	if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
733 		do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
734 	sprintf(alias + strlen(alias), "f*");
735 	if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
736 		do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
737 	sprintf(alias + strlen(alias), "w*");
738 	if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
739 		do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
740 	return 1;
741 }
742 
do_eisa_entry(const char * filename,void * symval,char * alias)743 static int do_eisa_entry(const char *filename, void *symval,
744 		char *alias)
745 {
746 	DEF_FIELD_ADDR(symval, eisa_device_id, sig);
747 	if (sig[0])
748 		sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
749 	else
750 		strcat(alias, "*");
751 	return 1;
752 }
753 
754 /* Looks like: parisc:tNhvNrevNsvN */
do_parisc_entry(const char * filename,void * symval,char * alias)755 static int do_parisc_entry(const char *filename, void *symval,
756 		char *alias)
757 {
758 	DEF_FIELD(symval, parisc_device_id, hw_type);
759 	DEF_FIELD(symval, parisc_device_id, hversion);
760 	DEF_FIELD(symval, parisc_device_id, hversion_rev);
761 	DEF_FIELD(symval, parisc_device_id, sversion);
762 
763 	strcpy(alias, "parisc:");
764 	ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
765 	ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
766 	ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
767 	ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
768 
769 	add_wildcard(alias);
770 	return 1;
771 }
772 
773 /* Looks like: sdio:cNvNdN. */
do_sdio_entry(const char * filename,void * symval,char * alias)774 static int do_sdio_entry(const char *filename,
775 			void *symval, char *alias)
776 {
777 	DEF_FIELD(symval, sdio_device_id, class);
778 	DEF_FIELD(symval, sdio_device_id, vendor);
779 	DEF_FIELD(symval, sdio_device_id, device);
780 
781 	strcpy(alias, "sdio:");
782 	ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
783 	ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
784 	ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
785 	add_wildcard(alias);
786 	return 1;
787 }
788 
789 /* Looks like: ssb:vNidNrevN. */
do_ssb_entry(const char * filename,void * symval,char * alias)790 static int do_ssb_entry(const char *filename,
791 			void *symval, char *alias)
792 {
793 	DEF_FIELD(symval, ssb_device_id, vendor);
794 	DEF_FIELD(symval, ssb_device_id, coreid);
795 	DEF_FIELD(symval, ssb_device_id, revision);
796 
797 	strcpy(alias, "ssb:");
798 	ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
799 	ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
800 	ADD(alias, "rev", revision != SSB_ANY_REV, revision);
801 	add_wildcard(alias);
802 	return 1;
803 }
804 
805 /* Looks like: bcma:mNidNrevNclN. */
do_bcma_entry(const char * filename,void * symval,char * alias)806 static int do_bcma_entry(const char *filename,
807 			 void *symval, char *alias)
808 {
809 	DEF_FIELD(symval, bcma_device_id, manuf);
810 	DEF_FIELD(symval, bcma_device_id, id);
811 	DEF_FIELD(symval, bcma_device_id, rev);
812 	DEF_FIELD(symval, bcma_device_id, class);
813 
814 	strcpy(alias, "bcma:");
815 	ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
816 	ADD(alias, "id", id != BCMA_ANY_ID, id);
817 	ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
818 	ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
819 	add_wildcard(alias);
820 	return 1;
821 }
822 
823 /* Looks like: virtio:dNvN */
do_virtio_entry(const char * filename,void * symval,char * alias)824 static int do_virtio_entry(const char *filename, void *symval,
825 			   char *alias)
826 {
827 	DEF_FIELD(symval, virtio_device_id, device);
828 	DEF_FIELD(symval, virtio_device_id, vendor);
829 
830 	strcpy(alias, "virtio:");
831 	ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
832 	ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
833 
834 	add_wildcard(alias);
835 	return 1;
836 }
837 
838 /*
839  * Looks like: vmbus:guid
840  * Each byte of the guid will be represented by two hex characters
841  * in the name.
842  */
843 
do_vmbus_entry(const char * filename,void * symval,char * alias)844 static int do_vmbus_entry(const char *filename, void *symval,
845 			  char *alias)
846 {
847 	int i;
848 	DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
849 	char guid_name[(sizeof(*guid) + 1) * 2];
850 
851 	for (i = 0; i < (sizeof(*guid) * 2); i += 2)
852 		sprintf(&guid_name[i], "%02x", TO_NATIVE((*guid)[i/2]));
853 
854 	strcpy(alias, "vmbus:");
855 	strcat(alias, guid_name);
856 
857 	return 1;
858 }
859 
860 /* Looks like: i2c:S */
do_i2c_entry(const char * filename,void * symval,char * alias)861 static int do_i2c_entry(const char *filename, void *symval,
862 			char *alias)
863 {
864 	DEF_FIELD_ADDR(symval, i2c_device_id, name);
865 	sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
866 
867 	return 1;
868 }
869 
870 /* Looks like: spi:S */
do_spi_entry(const char * filename,void * symval,char * alias)871 static int do_spi_entry(const char *filename, void *symval,
872 			char *alias)
873 {
874 	DEF_FIELD_ADDR(symval, spi_device_id, name);
875 	sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
876 
877 	return 1;
878 }
879 
880 static const struct dmifield {
881 	const char *prefix;
882 	int field;
883 } dmi_fields[] = {
884 	{ "bvn", DMI_BIOS_VENDOR },
885 	{ "bvr", DMI_BIOS_VERSION },
886 	{ "bd",  DMI_BIOS_DATE },
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(const char * filename,void * symval,char * alias)910 static int do_dmi_entry(const char *filename, void *symval,
911 			char *alias)
912 {
913 	int i, j;
914 	DEF_FIELD_ADDR(symval, dmi_system_id, matches);
915 	sprintf(alias, "dmi*");
916 
917 	for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
918 		for (j = 0; j < 4; j++) {
919 			if ((*matches)[j].slot &&
920 			    (*matches)[j].slot == dmi_fields[i].field) {
921 				sprintf(alias + strlen(alias), ":%s*",
922 					dmi_fields[i].prefix);
923 				dmi_ascii_filter(alias + strlen(alias),
924 						 (*matches)[j].substr);
925 				strcat(alias, "*");
926 			}
927 		}
928 	}
929 
930 	strcat(alias, ":");
931 	return 1;
932 }
933 
do_platform_entry(const char * filename,void * symval,char * alias)934 static int do_platform_entry(const char *filename,
935 			     void *symval, char *alias)
936 {
937 	DEF_FIELD_ADDR(symval, platform_device_id, name);
938 	sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
939 	return 1;
940 }
941 
do_mdio_entry(const char * filename,void * symval,char * alias)942 static int do_mdio_entry(const char *filename,
943 			 void *symval, char *alias)
944 {
945 	int i;
946 	DEF_FIELD(symval, mdio_device_id, phy_id);
947 	DEF_FIELD(symval, mdio_device_id, phy_id_mask);
948 
949 	alias += sprintf(alias, MDIO_MODULE_PREFIX);
950 
951 	for (i = 0; i < 32; i++) {
952 		if (!((phy_id_mask >> (31-i)) & 1))
953 			*(alias++) = '?';
954 		else if ((phy_id >> (31-i)) & 1)
955 			*(alias++) = '1';
956 		else
957 			*(alias++) = '0';
958 	}
959 
960 	/* Terminate the string */
961 	*alias = 0;
962 
963 	return 1;
964 }
965 
966 /* Looks like: zorro:iN. */
do_zorro_entry(const char * filename,void * symval,char * alias)967 static int do_zorro_entry(const char *filename, void *symval,
968 			  char *alias)
969 {
970 	DEF_FIELD(symval, zorro_device_id, id);
971 	strcpy(alias, "zorro:");
972 	ADD(alias, "i", id != ZORRO_WILDCARD, id);
973 	return 1;
974 }
975 
976 /* looks like: "pnp:dD" */
do_isapnp_entry(const char * filename,void * symval,char * alias)977 static int do_isapnp_entry(const char *filename,
978 			   void *symval, char *alias)
979 {
980 	DEF_FIELD(symval, isapnp_device_id, vendor);
981 	DEF_FIELD(symval, isapnp_device_id, function);
982 	sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
983 		'A' + ((vendor >> 2) & 0x3f) - 1,
984 		'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
985 		'A' + ((vendor >> 8) & 0x1f) - 1,
986 		(function >> 4) & 0x0f, function & 0x0f,
987 		(function >> 12) & 0x0f, (function >> 8) & 0x0f);
988 	return 1;
989 }
990 
991 /* Looks like: "ipack:fNvNdN". */
do_ipack_entry(const char * filename,void * symval,char * alias)992 static int do_ipack_entry(const char *filename,
993 			  void *symval, char *alias)
994 {
995 	DEF_FIELD(symval, ipack_device_id, format);
996 	DEF_FIELD(symval, ipack_device_id, vendor);
997 	DEF_FIELD(symval, ipack_device_id, device);
998 	strcpy(alias, "ipack:");
999 	ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1000 	ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1001 	ADD(alias, "d", device != IPACK_ANY_ID, device);
1002 	add_wildcard(alias);
1003 	return 1;
1004 }
1005 
1006 /*
1007  * Append a match expression for a single masked hex digit.
1008  * outp points to a pointer to the character at which to append.
1009  *	*outp is updated on return to point just after the appended text,
1010  *	to facilitate further appending.
1011  */
append_nibble_mask(char ** outp,unsigned int nibble,unsigned int mask)1012 static void append_nibble_mask(char **outp,
1013 			       unsigned int nibble, unsigned int mask)
1014 {
1015 	char *p = *outp;
1016 	unsigned int i;
1017 
1018 	switch (mask) {
1019 	case 0:
1020 		*p++ = '?';
1021 		break;
1022 
1023 	case 0xf:
1024 		p += sprintf(p, "%X",  nibble);
1025 		break;
1026 
1027 	default:
1028 		/*
1029 		 * Dumbly emit a match pattern for all possible matching
1030 		 * digits.  This could be improved in some cases using ranges,
1031 		 * but it has the advantage of being trivially correct, and is
1032 		 * often optimal.
1033 		 */
1034 		*p++ = '[';
1035 		for (i = 0; i < 0x10; i++)
1036 			if ((i & mask) == nibble)
1037 				p += sprintf(p, "%X", i);
1038 		*p++ = ']';
1039 	}
1040 
1041 	/* Ensure that the string remains NUL-terminated: */
1042 	*p = '\0';
1043 
1044 	/* Advance the caller's end-of-string pointer: */
1045 	*outp = p;
1046 }
1047 
1048 /*
1049  * looks like: "amba:dN"
1050  *
1051  * N is exactly 8 digits, where each is an upper-case hex digit, or
1052  *	a ? or [] pattern matching exactly one digit.
1053  */
do_amba_entry(const char * filename,void * symval,char * alias)1054 static int do_amba_entry(const char *filename,
1055 			 void *symval, char *alias)
1056 {
1057 	unsigned int digit;
1058 	char *p = alias;
1059 	DEF_FIELD(symval, amba_id, id);
1060 	DEF_FIELD(symval, amba_id, mask);
1061 
1062 	if ((id & mask) != id)
1063 		fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1064 		      "id=0x%08X, mask=0x%08X.  Please fix this driver.\n",
1065 		      filename, id, mask);
1066 
1067 	p += sprintf(alias, "amba:d");
1068 	for (digit = 0; digit < 8; digit++)
1069 		append_nibble_mask(&p,
1070 				   (id >> (4 * (7 - digit))) & 0xf,
1071 				   (mask >> (4 * (7 - digit))) & 0xf);
1072 
1073 	return 1;
1074 }
1075 
1076 /*
1077  * looks like: "mipscdmm:tN"
1078  *
1079  * N is exactly 2 digits, where each is an upper-case hex digit, or
1080  *	a ? or [] pattern matching exactly one digit.
1081  */
do_mips_cdmm_entry(const char * filename,void * symval,char * alias)1082 static int do_mips_cdmm_entry(const char *filename,
1083 			      void *symval, char *alias)
1084 {
1085 	DEF_FIELD(symval, mips_cdmm_device_id, type);
1086 
1087 	sprintf(alias, "mipscdmm:t%02X*", type);
1088 	return 1;
1089 }
1090 
1091 /* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
1092  * All fields are numbers. It would be nicer to use strings for vendor
1093  * and feature, but getting those out of the build system here is too
1094  * complicated.
1095  */
1096 
do_x86cpu_entry(const char * filename,void * symval,char * alias)1097 static int do_x86cpu_entry(const char *filename, void *symval,
1098 			   char *alias)
1099 {
1100 	DEF_FIELD(symval, x86_cpu_id, feature);
1101 	DEF_FIELD(symval, x86_cpu_id, family);
1102 	DEF_FIELD(symval, x86_cpu_id, model);
1103 	DEF_FIELD(symval, x86_cpu_id, vendor);
1104 
1105 	strcpy(alias, "cpu:type:x86,");
1106 	ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1107 	ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1108 	ADD(alias, "mod", model  != X86_MODEL_ANY,  model);
1109 	strcat(alias, ":feature:*");
1110 	if (feature != X86_FEATURE_ANY)
1111 		sprintf(alias + strlen(alias), "%04X*", feature);
1112 	return 1;
1113 }
1114 
1115 /* LOOKS like cpu:type:*:feature:*FEAT* */
do_cpu_entry(const char * filename,void * symval,char * alias)1116 static int do_cpu_entry(const char *filename, void *symval, char *alias)
1117 {
1118 	DEF_FIELD(symval, cpu_feature, feature);
1119 
1120 	sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1121 	return 1;
1122 }
1123 
1124 /* Looks like: mei:S:uuid:N:* */
do_mei_entry(const char * filename,void * symval,char * alias)1125 static int do_mei_entry(const char *filename, void *symval,
1126 			char *alias)
1127 {
1128 	DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
1129 	DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
1130 	DEF_FIELD(symval, mei_cl_device_id, version);
1131 
1132 	sprintf(alias, MEI_CL_MODULE_PREFIX);
1133 	sprintf(alias + strlen(alias), "%s:",  (*name)[0]  ? *name : "*");
1134 	add_uuid(alias, *uuid);
1135 	ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
1136 
1137 	strcat(alias, ":*");
1138 
1139 	return 1;
1140 }
1141 
1142 /* Looks like: rapidio:vNdNavNadN */
do_rio_entry(const char * filename,void * symval,char * alias)1143 static int do_rio_entry(const char *filename,
1144 			void *symval, char *alias)
1145 {
1146 	DEF_FIELD(symval, rio_device_id, did);
1147 	DEF_FIELD(symval, rio_device_id, vid);
1148 	DEF_FIELD(symval, rio_device_id, asm_did);
1149 	DEF_FIELD(symval, rio_device_id, asm_vid);
1150 
1151 	strcpy(alias, "rapidio:");
1152 	ADD(alias, "v", vid != RIO_ANY_ID, vid);
1153 	ADD(alias, "d", did != RIO_ANY_ID, did);
1154 	ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1155 	ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1156 
1157 	add_wildcard(alias);
1158 	return 1;
1159 }
1160 
1161 /* Looks like: ulpi:vNpN */
do_ulpi_entry(const char * filename,void * symval,char * alias)1162 static int do_ulpi_entry(const char *filename, void *symval,
1163 			 char *alias)
1164 {
1165 	DEF_FIELD(symval, ulpi_device_id, vendor);
1166 	DEF_FIELD(symval, ulpi_device_id, product);
1167 
1168 	sprintf(alias, "ulpi:v%04xp%04x", vendor, product);
1169 
1170 	return 1;
1171 }
1172 
1173 /* Looks like: hdaudio:vNrNaN */
do_hda_entry(const char * filename,void * symval,char * alias)1174 static int do_hda_entry(const char *filename, void *symval, char *alias)
1175 {
1176 	DEF_FIELD(symval, hda_device_id, vendor_id);
1177 	DEF_FIELD(symval, hda_device_id, rev_id);
1178 	DEF_FIELD(symval, hda_device_id, api_version);
1179 
1180 	strcpy(alias, "hdaudio:");
1181 	ADD(alias, "v", vendor_id != 0, vendor_id);
1182 	ADD(alias, "r", rev_id != 0, rev_id);
1183 	ADD(alias, "a", api_version != 0, api_version);
1184 
1185 	add_wildcard(alias);
1186 	return 1;
1187 }
1188 
1189 /* Does namelen bytes of name exactly match the symbol? */
sym_is(const char * name,unsigned namelen,const char * symbol)1190 static bool sym_is(const char *name, unsigned namelen, const char *symbol)
1191 {
1192 	if (namelen != strlen(symbol))
1193 		return false;
1194 
1195 	return memcmp(name, symbol, namelen) == 0;
1196 }
1197 
do_table(void * symval,unsigned long size,unsigned long id_size,const char * device_id,int (* do_entry)(const char * filename,void * symval,char * alias),struct module * mod)1198 static void do_table(void *symval, unsigned long size,
1199 		     unsigned long id_size,
1200 		     const char *device_id,
1201 		     int (*do_entry)(const char *filename, void *symval, char *alias),
1202 		     struct module *mod)
1203 {
1204 	unsigned int i;
1205 	char alias[500];
1206 
1207 	device_id_check(mod->name, device_id, size, id_size, symval);
1208 	/* Leave last one: it's the terminator. */
1209 	size -= id_size;
1210 
1211 	for (i = 0; i < size; i += id_size) {
1212 		if (do_entry(mod->name, symval+i, alias)) {
1213 			buf_printf(&mod->dev_table_buf,
1214 				   "MODULE_ALIAS(\"%s\");\n", alias);
1215 		}
1216 	}
1217 }
1218 
1219 static const struct devtable devtable[] = {
1220 	{"hid", SIZE_hid_device_id, do_hid_entry},
1221 	{"ieee1394", SIZE_ieee1394_device_id, do_ieee1394_entry},
1222 	{"pci", SIZE_pci_device_id, do_pci_entry},
1223 	{"ccw", SIZE_ccw_device_id, do_ccw_entry},
1224 	{"ap", SIZE_ap_device_id, do_ap_entry},
1225 	{"css", SIZE_css_device_id, do_css_entry},
1226 	{"serio", SIZE_serio_device_id, do_serio_entry},
1227 	{"acpi", SIZE_acpi_device_id, do_acpi_entry},
1228 	{"pcmcia", SIZE_pcmcia_device_id, do_pcmcia_entry},
1229 	{"vio", SIZE_vio_device_id, do_vio_entry},
1230 	{"input", SIZE_input_device_id, do_input_entry},
1231 	{"eisa", SIZE_eisa_device_id, do_eisa_entry},
1232 	{"parisc", SIZE_parisc_device_id, do_parisc_entry},
1233 	{"sdio", SIZE_sdio_device_id, do_sdio_entry},
1234 	{"ssb", SIZE_ssb_device_id, do_ssb_entry},
1235 	{"bcma", SIZE_bcma_device_id, do_bcma_entry},
1236 	{"virtio", SIZE_virtio_device_id, do_virtio_entry},
1237 	{"vmbus", SIZE_hv_vmbus_device_id, do_vmbus_entry},
1238 	{"i2c", SIZE_i2c_device_id, do_i2c_entry},
1239 	{"spi", SIZE_spi_device_id, do_spi_entry},
1240 	{"dmi", SIZE_dmi_system_id, do_dmi_entry},
1241 	{"platform", SIZE_platform_device_id, do_platform_entry},
1242 	{"mdio", SIZE_mdio_device_id, do_mdio_entry},
1243 	{"zorro", SIZE_zorro_device_id, do_zorro_entry},
1244 	{"isapnp", SIZE_isapnp_device_id, do_isapnp_entry},
1245 	{"ipack", SIZE_ipack_device_id, do_ipack_entry},
1246 	{"amba", SIZE_amba_id, do_amba_entry},
1247 	{"mipscdmm", SIZE_mips_cdmm_device_id, do_mips_cdmm_entry},
1248 	{"x86cpu", SIZE_x86_cpu_id, do_x86cpu_entry},
1249 	{"cpu", SIZE_cpu_feature, do_cpu_entry},
1250 	{"mei", SIZE_mei_cl_device_id, do_mei_entry},
1251 	{"rapidio", SIZE_rio_device_id, do_rio_entry},
1252 	{"ulpi", SIZE_ulpi_device_id, do_ulpi_entry},
1253 	{"hdaudio", SIZE_hda_device_id, do_hda_entry},
1254 	{"of", SIZE_of_device_id, do_of_entry},
1255 };
1256 
1257 /* Create MODULE_ALIAS() statements.
1258  * At this time, we cannot write the actual output C source yet,
1259  * 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)1260 void handle_moddevtable(struct module *mod, struct elf_info *info,
1261 			Elf_Sym *sym, const char *symname)
1262 {
1263 	void *symval;
1264 	char *zeros = NULL;
1265 	const char *name, *identifier;
1266 	unsigned int namelen;
1267 
1268 	/* We're looking for a section relative symbol */
1269 	if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
1270 		return;
1271 
1272 	/* We're looking for an object */
1273 	if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1274 		return;
1275 
1276 	/* All our symbols are of form <prefix>__mod_<name>__<identifier>_device_table. */
1277 	name = strstr(symname, "__mod_");
1278 	if (!name)
1279 		return;
1280 	name += strlen("__mod_");
1281 	namelen = strlen(name);
1282 	if (namelen < strlen("_device_table"))
1283 		return;
1284 	if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1285 		return;
1286 	identifier = strstr(name, "__");
1287 	if (!identifier)
1288 		return;
1289 	namelen = identifier - name;
1290 
1291 	/* Handle all-NULL symbols allocated into .bss */
1292 	if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
1293 		zeros = calloc(1, sym->st_size);
1294 		symval = zeros;
1295 	} else {
1296 		symval = (void *)info->hdr
1297 			+ info->sechdrs[get_secindex(info, sym)].sh_offset
1298 			+ sym->st_value;
1299 	}
1300 
1301 	/* First handle the "special" cases */
1302 	if (sym_is(name, namelen, "usb"))
1303 		do_usb_table(symval, sym->st_size, mod);
1304 	else if (sym_is(name, namelen, "pnp"))
1305 		do_pnp_device_entry(symval, sym->st_size, mod);
1306 	else if (sym_is(name, namelen, "pnp_card"))
1307 		do_pnp_card_entries(symval, sym->st_size, mod);
1308 	else {
1309 		int i;
1310 
1311 		for (i = 0; i < ARRAY_SIZE(devtable); i++) {
1312 			const struct devtable *p = &devtable[i];
1313 
1314 			if (sym_is(name, namelen, p->device_id)) {
1315 				do_table(symval, sym->st_size, p->id_size,
1316 					 p->device_id, p->do_entry, mod);
1317 				break;
1318 			}
1319 		}
1320 	}
1321 	free(zeros);
1322 }
1323 
1324 /* Now add out buffered information to the generated C source */
add_moddevtable(struct buffer * buf,struct module * mod)1325 void add_moddevtable(struct buffer *buf, struct module *mod)
1326 {
1327 	buf_printf(buf, "\n");
1328 	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1329 	free(mod->dev_table_buf.p);
1330 }
1331