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