xref: /openbmc/linux/scripts/mod/file2alias.c (revision 22246614)
1 /* Simple code to turn various tables in an ELF file into alias definitions.
2  * This deals with kernel datastructures where they should be
3  * dealt with: in the kernel source.
4  *
5  * Copyright 2002-2003  Rusty Russell, IBM Corporation
6  *           2003       Kai Germaschewski
7  *
8  *
9  * This software may be used and distributed according to the terms
10  * of the GNU General Public License, incorporated herein by reference.
11  */
12 
13 #include "modpost.h"
14 
15 /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
16  * use either stdint.h or inttypes.h for the rest. */
17 #if KERNEL_ELFCLASS == ELFCLASS32
18 typedef Elf32_Addr	kernel_ulong_t;
19 #define BITS_PER_LONG 32
20 #else
21 typedef Elf64_Addr	kernel_ulong_t;
22 #define BITS_PER_LONG 64
23 #endif
24 #ifdef __sun__
25 #include <inttypes.h>
26 #else
27 #include <stdint.h>
28 #endif
29 
30 #include <ctype.h>
31 
32 typedef uint32_t	__u32;
33 typedef uint16_t	__u16;
34 typedef unsigned char	__u8;
35 
36 /* Big exception to the "don't include kernel headers into userspace, which
37  * even potentially has different endianness and word sizes, since
38  * we handle those differences explicitly below */
39 #include "../../include/linux/mod_devicetable.h"
40 
41 #define ADD(str, sep, cond, field)                              \
42 do {                                                            \
43         strcat(str, sep);                                       \
44         if (cond)                                               \
45                 sprintf(str + strlen(str),                      \
46                         sizeof(field) == 1 ? "%02X" :           \
47                         sizeof(field) == 2 ? "%04X" :           \
48                         sizeof(field) == 4 ? "%08X" : "",       \
49                         field);                                 \
50         else                                                    \
51                 sprintf(str + strlen(str), "*");                \
52 } while(0)
53 
54 /* Always end in a wildcard, for future extension */
55 static inline void add_wildcard(char *str)
56 {
57 	int len = strlen(str);
58 
59 	if (str[len - 1] != '*')
60 		strcat(str + len, "*");
61 }
62 
63 unsigned int cross_build = 0;
64 /**
65  * Check that sizeof(device_id type) are consistent with size of section
66  * in .o file. If in-consistent then userspace and kernel does not agree
67  * on actual size which is a bug.
68  * Also verify that the final entry in the table is all zeros.
69  * Ignore both checks if build host differ from target host and size differs.
70  **/
71 static void device_id_check(const char *modname, const char *device_id,
72 			    unsigned long size, unsigned long id_size,
73 			    void *symval)
74 {
75 	int i;
76 
77 	if (size % id_size || size < id_size) {
78 		if (cross_build != 0)
79 			return;
80 		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
81 		      "of the size of section __mod_%s_device_table=%lu.\n"
82 		      "Fix definition of struct %s_device_id "
83 		      "in mod_devicetable.h\n",
84 		      modname, device_id, id_size, device_id, size, device_id);
85 	}
86 	/* Verify last one is a terminator */
87 	for (i = 0; i < id_size; i++ ) {
88 		if (*(uint8_t*)(symval+size-id_size+i)) {
89 			fprintf(stderr,"%s: struct %s_device_id is %lu bytes.  "
90 				"The last of %lu is:\n",
91 				modname, device_id, id_size, size / id_size);
92 			for (i = 0; i < id_size; i++ )
93 				fprintf(stderr,"0x%02x ",
94 					*(uint8_t*)(symval+size-id_size+i) );
95 			fprintf(stderr,"\n");
96 			fatal("%s: struct %s_device_id is not terminated "
97 				"with a NULL entry!\n", modname, device_id);
98 		}
99 	}
100 }
101 
102 /* USB is special because the bcdDevice can be matched against a numeric range */
103 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
104 static void do_usb_entry(struct usb_device_id *id,
105 			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
106 			 unsigned char range_lo, unsigned char range_hi,
107 			 struct module *mod)
108 {
109 	char alias[500];
110 	strcpy(alias, "usb:");
111 	ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
112 	    id->idVendor);
113 	ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
114 	    id->idProduct);
115 
116 	strcat(alias, "d");
117 	if (bcdDevice_initial_digits)
118 		sprintf(alias + strlen(alias), "%0*X",
119 			bcdDevice_initial_digits, bcdDevice_initial);
120 	if (range_lo == range_hi)
121 		sprintf(alias + strlen(alias), "%u", range_lo);
122 	else if (range_lo > 0 || range_hi < 9)
123 		sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
124 	if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
125 		strcat(alias, "*");
126 
127 	ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
128 	    id->bDeviceClass);
129 	ADD(alias, "dsc",
130 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
131 	    id->bDeviceSubClass);
132 	ADD(alias, "dp",
133 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
134 	    id->bDeviceProtocol);
135 	ADD(alias, "ic",
136 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
137 	    id->bInterfaceClass);
138 	ADD(alias, "isc",
139 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
140 	    id->bInterfaceSubClass);
141 	ADD(alias, "ip",
142 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
143 	    id->bInterfaceProtocol);
144 
145 	add_wildcard(alias);
146 	buf_printf(&mod->dev_table_buf,
147 		   "MODULE_ALIAS(\"%s\");\n", alias);
148 }
149 
150 static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
151 {
152 	unsigned int devlo, devhi;
153 	unsigned char chi, clo;
154 	int ndigits;
155 
156 	id->match_flags = TO_NATIVE(id->match_flags);
157 	id->idVendor = TO_NATIVE(id->idVendor);
158 	id->idProduct = TO_NATIVE(id->idProduct);
159 
160 	devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
161 		TO_NATIVE(id->bcdDevice_lo) : 0x0U;
162 	devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
163 		TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
164 
165 	/*
166 	 * Some modules (visor) have empty slots as placeholder for
167 	 * run-time specification that results in catch-all alias
168 	 */
169 	if (!(id->idVendor | id->idProduct | id->bDeviceClass | id->bInterfaceClass))
170 		return;
171 
172 	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
173 	for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
174 		clo = devlo & 0xf;
175 		chi = devhi & 0xf;
176 		if (chi > 9)	/* it's bcd not hex */
177 			chi = 9;
178 		devlo >>= 4;
179 		devhi >>= 4;
180 
181 		if (devlo == devhi || !ndigits) {
182 			do_usb_entry(id, devlo, ndigits, clo, chi, mod);
183 			break;
184 		}
185 
186 		if (clo > 0)
187 			do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
188 
189 		if (chi < 9)
190 			do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
191 	}
192 }
193 
194 static void do_usb_table(void *symval, unsigned long size,
195 			 struct module *mod)
196 {
197 	unsigned int i;
198 	const unsigned long id_size = sizeof(struct usb_device_id);
199 
200 	device_id_check(mod->name, "usb", size, id_size, symval);
201 
202 	/* Leave last one: it's the terminator. */
203 	size -= id_size;
204 
205 	for (i = 0; i < size; i += id_size)
206 		do_usb_entry_multi(symval + i, mod);
207 }
208 
209 /* Looks like: ieee1394:venNmoNspNverN */
210 static int do_ieee1394_entry(const char *filename,
211 			     struct ieee1394_device_id *id, char *alias)
212 {
213 	id->match_flags = TO_NATIVE(id->match_flags);
214 	id->vendor_id = TO_NATIVE(id->vendor_id);
215 	id->model_id = TO_NATIVE(id->model_id);
216 	id->specifier_id = TO_NATIVE(id->specifier_id);
217 	id->version = TO_NATIVE(id->version);
218 
219 	strcpy(alias, "ieee1394:");
220 	ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
221 	    id->vendor_id);
222 	ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
223 	    id->model_id);
224 	ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
225 	    id->specifier_id);
226 	ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
227 	    id->version);
228 
229 	add_wildcard(alias);
230 	return 1;
231 }
232 
233 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
234 static int do_pci_entry(const char *filename,
235 			struct pci_device_id *id, char *alias)
236 {
237 	/* Class field can be divided into these three. */
238 	unsigned char baseclass, subclass, interface,
239 		baseclass_mask, subclass_mask, interface_mask;
240 
241 	id->vendor = TO_NATIVE(id->vendor);
242 	id->device = TO_NATIVE(id->device);
243 	id->subvendor = TO_NATIVE(id->subvendor);
244 	id->subdevice = TO_NATIVE(id->subdevice);
245 	id->class = TO_NATIVE(id->class);
246 	id->class_mask = TO_NATIVE(id->class_mask);
247 
248 	strcpy(alias, "pci:");
249 	ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
250 	ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
251 	ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
252 	ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
253 
254 	baseclass = (id->class) >> 16;
255 	baseclass_mask = (id->class_mask) >> 16;
256 	subclass = (id->class) >> 8;
257 	subclass_mask = (id->class_mask) >> 8;
258 	interface = id->class;
259 	interface_mask = id->class_mask;
260 
261 	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
262 	    || (subclass_mask != 0 && subclass_mask != 0xFF)
263 	    || (interface_mask != 0 && interface_mask != 0xFF)) {
264 		warn("Can't handle masks in %s:%04X\n",
265 		     filename, id->class_mask);
266 		return 0;
267 	}
268 
269 	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
270 	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
271 	ADD(alias, "i", interface_mask == 0xFF, interface);
272 	add_wildcard(alias);
273 	return 1;
274 }
275 
276 /* looks like: "ccw:tNmNdtNdmN" */
277 static int do_ccw_entry(const char *filename,
278 			struct ccw_device_id *id, char *alias)
279 {
280 	id->match_flags = TO_NATIVE(id->match_flags);
281 	id->cu_type = TO_NATIVE(id->cu_type);
282 	id->cu_model = TO_NATIVE(id->cu_model);
283 	id->dev_type = TO_NATIVE(id->dev_type);
284 	id->dev_model = TO_NATIVE(id->dev_model);
285 
286 	strcpy(alias, "ccw:");
287 	ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
288 	    id->cu_type);
289 	ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
290 	    id->cu_model);
291 	ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
292 	    id->dev_type);
293 	ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
294 	    id->dev_model);
295 	add_wildcard(alias);
296 	return 1;
297 }
298 
299 /* looks like: "ap:tN" */
300 static int do_ap_entry(const char *filename,
301 		       struct ap_device_id *id, char *alias)
302 {
303 	sprintf(alias, "ap:t%02X*", id->dev_type);
304 	return 1;
305 }
306 
307 /* Looks like: "serio:tyNprNidNexN" */
308 static int do_serio_entry(const char *filename,
309 			  struct serio_device_id *id, char *alias)
310 {
311 	id->type = TO_NATIVE(id->type);
312 	id->proto = TO_NATIVE(id->proto);
313 	id->id = TO_NATIVE(id->id);
314 	id->extra = TO_NATIVE(id->extra);
315 
316 	strcpy(alias, "serio:");
317 	ADD(alias, "ty", id->type != SERIO_ANY, id->type);
318 	ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
319 	ADD(alias, "id", id->id != SERIO_ANY, id->id);
320 	ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
321 
322 	add_wildcard(alias);
323 	return 1;
324 }
325 
326 /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
327 static int do_acpi_entry(const char *filename,
328 			struct acpi_device_id *id, char *alias)
329 {
330 	sprintf(alias, "acpi*:%s:*", id->id);
331 	return 1;
332 }
333 
334 /* looks like: "pnp:dD" */
335 static int do_pnp_entry(const char *filename,
336 			struct pnp_device_id *id, char *alias)
337 {
338 	sprintf(alias, "pnp:d%s*", id->id);
339 	return 1;
340 }
341 
342 /* looks like: "pnp:dD" for every device of the card */
343 static void do_pnp_card_entries(void *symval, unsigned long size,
344 				struct module *mod)
345 {
346 	const unsigned long id_size = sizeof(struct pnp_card_device_id);
347 	const unsigned int count = (size / id_size)-1;
348 	const struct pnp_card_device_id *cards = symval;
349 	unsigned int i;
350 
351 	device_id_check(mod->name, "pnp", size, id_size, symval);
352 
353 	for (i = 0; i < count; i++) {
354 		unsigned int j;
355 		const struct pnp_card_device_id *card = &cards[i];
356 
357 		for (j = 0; j < PNP_MAX_DEVICES; j++) {
358 			const char *id = (char *)card->devs[j].id;
359 			int i2, j2;
360 			int dup = 0;
361 
362 			if (!id[0])
363 				break;
364 
365 			/* find duplicate, already added value */
366 			for (i2 = 0; i2 < i && !dup; i2++) {
367 				const struct pnp_card_device_id *card2 = &cards[i2];
368 
369 				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
370 					const char *id2 = (char *)card2->devs[j2].id;
371 
372 					if (!id2[0])
373 						break;
374 
375 					if (!strcmp(id, id2)) {
376 						dup = 1;
377 						break;
378 					}
379 				}
380 			}
381 
382 			/* add an individual alias for every device entry */
383 			if (!dup)
384 				buf_printf(&mod->dev_table_buf,
385 					   "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
386 		}
387 	}
388 }
389 
390 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
391 static int do_pcmcia_entry(const char *filename,
392 			   struct pcmcia_device_id *id, char *alias)
393 {
394 	unsigned int i;
395 
396 	id->match_flags = TO_NATIVE(id->match_flags);
397 	id->manf_id = TO_NATIVE(id->manf_id);
398 	id->card_id = TO_NATIVE(id->card_id);
399 	id->func_id = TO_NATIVE(id->func_id);
400 	id->function = TO_NATIVE(id->function);
401 	id->device_no = TO_NATIVE(id->device_no);
402 
403 	for (i=0; i<4; i++) {
404 		id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
405        }
406 
407        strcpy(alias, "pcmcia:");
408        ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
409 	   id->manf_id);
410        ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
411 	   id->card_id);
412        ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
413 	   id->func_id);
414        ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
415 	   id->function);
416        ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
417 	   id->device_no);
418        ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
419        ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
420        ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
421        ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
422 
423 	add_wildcard(alias);
424        return 1;
425 }
426 
427 
428 
429 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
430 {
431     int len;
432     char *tmp;
433     len = sprintf (alias, "of:N%sT%s",
434                     of->name[0] ? of->name : "*",
435                     of->type[0] ? of->type : "*");
436 
437     if (of->compatible[0])
438         sprintf (&alias[len], "%sC%s",
439                      of->type[0] ? "*" : "",
440                      of->compatible);
441 
442     /* Replace all whitespace with underscores */
443     for (tmp = alias; tmp && *tmp; tmp++)
444         if (isspace (*tmp))
445             *tmp = '_';
446 
447     add_wildcard(alias);
448     return 1;
449 }
450 
451 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
452 		char *alias)
453 {
454 	char *tmp;
455 
456 	sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
457 			vio->compat[0] ? vio->compat : "*");
458 
459 	/* Replace all whitespace with underscores */
460 	for (tmp = alias; tmp && *tmp; tmp++)
461 		if (isspace (*tmp))
462 			*tmp = '_';
463 
464 	add_wildcard(alias);
465 	return 1;
466 }
467 
468 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
469 
470 static void do_input(char *alias,
471 		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
472 {
473 	unsigned int i;
474 
475 	for (i = min; i < max; i++)
476 		if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
477 			sprintf(alias + strlen(alias), "%X,*", i);
478 }
479 
480 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
481 static int do_input_entry(const char *filename, struct input_device_id *id,
482 			  char *alias)
483 {
484 	sprintf(alias, "input:");
485 
486 	ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
487 	ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
488 	ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
489 	ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
490 
491 	sprintf(alias + strlen(alias), "-e*");
492 	if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
493 		do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX);
494 	sprintf(alias + strlen(alias), "k*");
495 	if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
496 		do_input(alias, id->keybit,
497 			 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
498 			 INPUT_DEVICE_ID_KEY_MAX);
499 	sprintf(alias + strlen(alias), "r*");
500 	if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
501 		do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX);
502 	sprintf(alias + strlen(alias), "a*");
503 	if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
504 		do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
505 	sprintf(alias + strlen(alias), "m*");
506 	if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
507 		do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
508 	sprintf(alias + strlen(alias), "l*");
509 	if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
510 		do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
511 	sprintf(alias + strlen(alias), "s*");
512 	if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
513 		do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
514 	sprintf(alias + strlen(alias), "f*");
515 	if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
516 		do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
517 	sprintf(alias + strlen(alias), "w*");
518 	if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
519 		do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX);
520 	return 1;
521 }
522 
523 static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
524 		char *alias)
525 {
526 	if (eisa->sig[0])
527 		sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
528 	else
529 		strcat(alias, "*");
530 	return 1;
531 }
532 
533 /* Looks like: parisc:tNhvNrevNsvN */
534 static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
535 		char *alias)
536 {
537 	id->hw_type = TO_NATIVE(id->hw_type);
538 	id->hversion = TO_NATIVE(id->hversion);
539 	id->hversion_rev = TO_NATIVE(id->hversion_rev);
540 	id->sversion = TO_NATIVE(id->sversion);
541 
542 	strcpy(alias, "parisc:");
543 	ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type);
544 	ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion);
545 	ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev);
546 	ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion);
547 
548 	add_wildcard(alias);
549 	return 1;
550 }
551 
552 /* Looks like: sdio:cNvNdN. */
553 static int do_sdio_entry(const char *filename,
554 			struct sdio_device_id *id, char *alias)
555 {
556 	id->class = TO_NATIVE(id->class);
557 	id->vendor = TO_NATIVE(id->vendor);
558 	id->device = TO_NATIVE(id->device);
559 
560 	strcpy(alias, "sdio:");
561 	ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class);
562 	ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor);
563 	ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device);
564 	add_wildcard(alias);
565 	return 1;
566 }
567 
568 /* Looks like: ssb:vNidNrevN. */
569 static int do_ssb_entry(const char *filename,
570 			struct ssb_device_id *id, char *alias)
571 {
572 	id->vendor = TO_NATIVE(id->vendor);
573 	id->coreid = TO_NATIVE(id->coreid);
574 	id->revision = TO_NATIVE(id->revision);
575 
576 	strcpy(alias, "ssb:");
577 	ADD(alias, "v", id->vendor != SSB_ANY_VENDOR, id->vendor);
578 	ADD(alias, "id", id->coreid != SSB_ANY_ID, id->coreid);
579 	ADD(alias, "rev", id->revision != SSB_ANY_REV, id->revision);
580 	add_wildcard(alias);
581 	return 1;
582 }
583 
584 /* Looks like: virtio:dNvN */
585 static int do_virtio_entry(const char *filename, struct virtio_device_id *id,
586 			   char *alias)
587 {
588 	id->device = TO_NATIVE(id->device);
589 	id->vendor = TO_NATIVE(id->vendor);
590 
591 	strcpy(alias, "virtio:");
592 	ADD(alias, "d", 1, id->device);
593 	ADD(alias, "v", id->vendor != VIRTIO_DEV_ANY_ID, id->vendor);
594 
595 	add_wildcard(alias);
596 	return 1;
597 }
598 
599 /* Looks like: i2c:S */
600 static int do_i2c_entry(const char *filename, struct i2c_device_id *id,
601 			char *alias)
602 {
603 	sprintf(alias, I2C_MODULE_PREFIX "%s", id->name);
604 
605 	return 1;
606 }
607 
608 /* Ignore any prefix, eg. v850 prepends _ */
609 static inline int sym_is(const char *symbol, const char *name)
610 {
611 	const char *match;
612 
613 	match = strstr(symbol, name);
614 	if (!match)
615 		return 0;
616 	return match[strlen(symbol)] == '\0';
617 }
618 
619 static void do_table(void *symval, unsigned long size,
620 		     unsigned long id_size,
621 		     const char *device_id,
622 		     void *function,
623 		     struct module *mod)
624 {
625 	unsigned int i;
626 	char alias[500];
627 	int (*do_entry)(const char *, void *entry, char *alias) = function;
628 
629 	device_id_check(mod->name, device_id, size, id_size, symval);
630 	/* Leave last one: it's the terminator. */
631 	size -= id_size;
632 
633 	for (i = 0; i < size; i += id_size) {
634 		if (do_entry(mod->name, symval+i, alias)) {
635 			buf_printf(&mod->dev_table_buf,
636 				   "MODULE_ALIAS(\"%s\");\n", alias);
637 		}
638 	}
639 }
640 
641 /* Create MODULE_ALIAS() statements.
642  * At this time, we cannot write the actual output C source yet,
643  * so we write into the mod->dev_table_buf buffer. */
644 void handle_moddevtable(struct module *mod, struct elf_info *info,
645 			Elf_Sym *sym, const char *symname)
646 {
647 	void *symval;
648 	char *zeros = NULL;
649 
650 	/* We're looking for a section relative symbol */
651 	if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
652 		return;
653 
654 	/* Handle all-NULL symbols allocated into .bss */
655 	if (info->sechdrs[sym->st_shndx].sh_type & SHT_NOBITS) {
656 		zeros = calloc(1, sym->st_size);
657 		symval = zeros;
658 	} else {
659 		symval = (void *)info->hdr
660 			+ info->sechdrs[sym->st_shndx].sh_offset
661 			+ sym->st_value;
662 	}
663 
664 	if (sym_is(symname, "__mod_pci_device_table"))
665 		do_table(symval, sym->st_size,
666 			 sizeof(struct pci_device_id), "pci",
667 			 do_pci_entry, mod);
668 	else if (sym_is(symname, "__mod_usb_device_table"))
669 		/* special case to handle bcdDevice ranges */
670 		do_usb_table(symval, sym->st_size, mod);
671 	else if (sym_is(symname, "__mod_ieee1394_device_table"))
672 		do_table(symval, sym->st_size,
673 			 sizeof(struct ieee1394_device_id), "ieee1394",
674 			 do_ieee1394_entry, mod);
675 	else if (sym_is(symname, "__mod_ccw_device_table"))
676 		do_table(symval, sym->st_size,
677 			 sizeof(struct ccw_device_id), "ccw",
678 			 do_ccw_entry, mod);
679 	else if (sym_is(symname, "__mod_ap_device_table"))
680 		do_table(symval, sym->st_size,
681 			 sizeof(struct ap_device_id), "ap",
682 			 do_ap_entry, mod);
683 	else if (sym_is(symname, "__mod_serio_device_table"))
684 		do_table(symval, sym->st_size,
685 			 sizeof(struct serio_device_id), "serio",
686 			 do_serio_entry, mod);
687 	else if (sym_is(symname, "__mod_acpi_device_table"))
688 		do_table(symval, sym->st_size,
689 			 sizeof(struct acpi_device_id), "acpi",
690 			 do_acpi_entry, mod);
691 	else if (sym_is(symname, "__mod_pnp_device_table"))
692 		do_table(symval, sym->st_size,
693 			 sizeof(struct pnp_device_id), "pnp",
694 			 do_pnp_entry, mod);
695 	else if (sym_is(symname, "__mod_pnp_card_device_table"))
696 		do_pnp_card_entries(symval, sym->st_size, mod);
697 	else if (sym_is(symname, "__mod_pcmcia_device_table"))
698 		do_table(symval, sym->st_size,
699 			 sizeof(struct pcmcia_device_id), "pcmcia",
700 			 do_pcmcia_entry, mod);
701         else if (sym_is(symname, "__mod_of_device_table"))
702 		do_table(symval, sym->st_size,
703 			 sizeof(struct of_device_id), "of",
704 			 do_of_entry, mod);
705         else if (sym_is(symname, "__mod_vio_device_table"))
706 		do_table(symval, sym->st_size,
707 			 sizeof(struct vio_device_id), "vio",
708 			 do_vio_entry, mod);
709 	else if (sym_is(symname, "__mod_input_device_table"))
710 		do_table(symval, sym->st_size,
711 			 sizeof(struct input_device_id), "input",
712 			 do_input_entry, mod);
713 	else if (sym_is(symname, "__mod_eisa_device_table"))
714 		do_table(symval, sym->st_size,
715 			 sizeof(struct eisa_device_id), "eisa",
716 			 do_eisa_entry, mod);
717 	else if (sym_is(symname, "__mod_parisc_device_table"))
718 		do_table(symval, sym->st_size,
719 			 sizeof(struct parisc_device_id), "parisc",
720 			 do_parisc_entry, mod);
721 	else if (sym_is(symname, "__mod_sdio_device_table"))
722 		do_table(symval, sym->st_size,
723 			 sizeof(struct sdio_device_id), "sdio",
724 			 do_sdio_entry, mod);
725 	else if (sym_is(symname, "__mod_ssb_device_table"))
726 		do_table(symval, sym->st_size,
727 			 sizeof(struct ssb_device_id), "ssb",
728 			 do_ssb_entry, mod);
729 	else if (sym_is(symname, "__mod_virtio_device_table"))
730 		do_table(symval, sym->st_size,
731 			 sizeof(struct virtio_device_id), "virtio",
732 			 do_virtio_entry, mod);
733 	else if (sym_is(symname, "__mod_i2c_device_table"))
734 		do_table(symval, sym->st_size,
735 			 sizeof(struct i2c_device_id), "i2c",
736 			 do_i2c_entry, mod);
737 	free(zeros);
738 }
739 
740 /* Now add out buffered information to the generated C source */
741 void add_moddevtable(struct buffer *buf, struct module *mod)
742 {
743 	buf_printf(buf, "\n");
744 	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
745 	free(mod->dev_table_buf.p);
746 }
747