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