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 /* Always 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 /* Looks like: hid:bNvNpN */ 375 static int do_hid_entry(const char *filename, 376 void *symval, char *alias) 377 { 378 DEF_FIELD(symval, hid_device_id, bus); 379 DEF_FIELD(symval, hid_device_id, group); 380 DEF_FIELD(symval, hid_device_id, vendor); 381 DEF_FIELD(symval, hid_device_id, product); 382 383 sprintf(alias, "hid:"); 384 ADD(alias, "b", bus != HID_BUS_ANY, bus); 385 ADD(alias, "g", group != HID_GROUP_ANY, group); 386 ADD(alias, "v", vendor != HID_ANY_ID, vendor); 387 ADD(alias, "p", product != HID_ANY_ID, product); 388 389 return 1; 390 } 391 ADD_TO_DEVTABLE("hid", hid_device_id, do_hid_entry); 392 393 /* Looks like: ieee1394:venNmoNspNverN */ 394 static int do_ieee1394_entry(const char *filename, 395 void *symval, char *alias) 396 { 397 DEF_FIELD(symval, ieee1394_device_id, match_flags); 398 DEF_FIELD(symval, ieee1394_device_id, vendor_id); 399 DEF_FIELD(symval, ieee1394_device_id, model_id); 400 DEF_FIELD(symval, ieee1394_device_id, specifier_id); 401 DEF_FIELD(symval, ieee1394_device_id, version); 402 403 strcpy(alias, "ieee1394:"); 404 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID, 405 vendor_id); 406 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID, 407 model_id); 408 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID, 409 specifier_id); 410 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION, 411 version); 412 413 add_wildcard(alias); 414 return 1; 415 } 416 ADD_TO_DEVTABLE("ieee1394", ieee1394_device_id, do_ieee1394_entry); 417 418 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */ 419 static int do_pci_entry(const char *filename, 420 void *symval, char *alias) 421 { 422 /* Class field can be divided into these three. */ 423 unsigned char baseclass, subclass, interface, 424 baseclass_mask, subclass_mask, interface_mask; 425 426 DEF_FIELD(symval, pci_device_id, vendor); 427 DEF_FIELD(symval, pci_device_id, device); 428 DEF_FIELD(symval, pci_device_id, subvendor); 429 DEF_FIELD(symval, pci_device_id, subdevice); 430 DEF_FIELD(symval, pci_device_id, class); 431 DEF_FIELD(symval, pci_device_id, class_mask); 432 433 strcpy(alias, "pci:"); 434 ADD(alias, "v", vendor != PCI_ANY_ID, vendor); 435 ADD(alias, "d", device != PCI_ANY_ID, device); 436 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor); 437 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice); 438 439 baseclass = (class) >> 16; 440 baseclass_mask = (class_mask) >> 16; 441 subclass = (class) >> 8; 442 subclass_mask = (class_mask) >> 8; 443 interface = class; 444 interface_mask = class_mask; 445 446 if ((baseclass_mask != 0 && baseclass_mask != 0xFF) 447 || (subclass_mask != 0 && subclass_mask != 0xFF) 448 || (interface_mask != 0 && interface_mask != 0xFF)) { 449 warn("Can't handle masks in %s:%04X\n", 450 filename, class_mask); 451 return 0; 452 } 453 454 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass); 455 ADD(alias, "sc", subclass_mask == 0xFF, subclass); 456 ADD(alias, "i", interface_mask == 0xFF, interface); 457 add_wildcard(alias); 458 return 1; 459 } 460 ADD_TO_DEVTABLE("pci", pci_device_id, do_pci_entry); 461 462 /* looks like: "ccw:tNmNdtNdmN" */ 463 static int do_ccw_entry(const char *filename, 464 void *symval, char *alias) 465 { 466 DEF_FIELD(symval, ccw_device_id, match_flags); 467 DEF_FIELD(symval, ccw_device_id, cu_type); 468 DEF_FIELD(symval, ccw_device_id, cu_model); 469 DEF_FIELD(symval, ccw_device_id, dev_type); 470 DEF_FIELD(symval, ccw_device_id, dev_model); 471 472 strcpy(alias, "ccw:"); 473 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE, 474 cu_type); 475 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL, 476 cu_model); 477 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE, 478 dev_type); 479 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL, 480 dev_model); 481 add_wildcard(alias); 482 return 1; 483 } 484 ADD_TO_DEVTABLE("ccw", ccw_device_id, do_ccw_entry); 485 486 /* looks like: "ap:tN" */ 487 static int do_ap_entry(const char *filename, 488 void *symval, char *alias) 489 { 490 DEF_FIELD(symval, ap_device_id, dev_type); 491 492 sprintf(alias, "ap:t%02X*", dev_type); 493 return 1; 494 } 495 ADD_TO_DEVTABLE("ap", ap_device_id, do_ap_entry); 496 497 /* looks like: "css:tN" */ 498 static int do_css_entry(const char *filename, 499 void *symval, char *alias) 500 { 501 DEF_FIELD(symval, css_device_id, type); 502 503 sprintf(alias, "css:t%01X", type); 504 return 1; 505 } 506 ADD_TO_DEVTABLE("css", css_device_id, do_css_entry); 507 508 /* Looks like: "serio:tyNprNidNexN" */ 509 static int do_serio_entry(const char *filename, 510 void *symval, char *alias) 511 { 512 DEF_FIELD(symval, serio_device_id, type); 513 DEF_FIELD(symval, serio_device_id, proto); 514 DEF_FIELD(symval, serio_device_id, id); 515 DEF_FIELD(symval, serio_device_id, extra); 516 517 strcpy(alias, "serio:"); 518 ADD(alias, "ty", type != SERIO_ANY, type); 519 ADD(alias, "pr", proto != SERIO_ANY, proto); 520 ADD(alias, "id", id != SERIO_ANY, id); 521 ADD(alias, "ex", extra != SERIO_ANY, extra); 522 523 add_wildcard(alias); 524 return 1; 525 } 526 ADD_TO_DEVTABLE("serio", serio_device_id, do_serio_entry); 527 528 /* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or 529 * "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if) 530 * 531 * NOTE: Each driver should use one of the following : _HID, _CIDs 532 * or _CLS. Also, bb, ss, and pp can be substituted with ?? 533 * as don't care byte. 534 */ 535 static int do_acpi_entry(const char *filename, 536 void *symval, char *alias) 537 { 538 DEF_FIELD_ADDR(symval, acpi_device_id, id); 539 DEF_FIELD_ADDR(symval, acpi_device_id, cls); 540 DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk); 541 542 if (id && strlen((const char *)*id)) 543 sprintf(alias, "acpi*:%s:*", *id); 544 else if (cls) { 545 int i, byte_shift, cnt = 0; 546 unsigned int msk; 547 548 sprintf(&alias[cnt], "acpi*:"); 549 cnt = 6; 550 for (i = 1; i <= 3; i++) { 551 byte_shift = 8 * (3-i); 552 msk = (*cls_msk >> byte_shift) & 0xFF; 553 if (msk) 554 sprintf(&alias[cnt], "%02x", 555 (*cls >> byte_shift) & 0xFF); 556 else 557 sprintf(&alias[cnt], "??"); 558 cnt += 2; 559 } 560 sprintf(&alias[cnt], ":*"); 561 } 562 return 1; 563 } 564 ADD_TO_DEVTABLE("acpi", acpi_device_id, do_acpi_entry); 565 566 /* looks like: "pnp:dD" */ 567 static void do_pnp_device_entry(void *symval, unsigned long size, 568 struct module *mod) 569 { 570 const unsigned long id_size = SIZE_pnp_device_id; 571 const unsigned int count = (size / id_size)-1; 572 unsigned int i; 573 574 device_id_check(mod->name, "pnp", size, id_size, symval); 575 576 for (i = 0; i < count; i++) { 577 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id); 578 char acpi_id[sizeof(*id)]; 579 int j; 580 581 buf_printf(&mod->dev_table_buf, 582 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id); 583 584 /* fix broken pnp bus lowercasing */ 585 for (j = 0; j < sizeof(acpi_id); j++) 586 acpi_id[j] = toupper((*id)[j]); 587 buf_printf(&mod->dev_table_buf, 588 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id); 589 } 590 } 591 592 /* looks like: "pnp:dD" for every device of the card */ 593 static void do_pnp_card_entries(void *symval, unsigned long size, 594 struct module *mod) 595 { 596 const unsigned long id_size = SIZE_pnp_card_device_id; 597 const unsigned int count = (size / id_size)-1; 598 unsigned int i; 599 600 device_id_check(mod->name, "pnp", size, id_size, symval); 601 602 for (i = 0; i < count; i++) { 603 unsigned int j; 604 DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs); 605 606 for (j = 0; j < PNP_MAX_DEVICES; j++) { 607 const char *id = (char *)(*devs)[j].id; 608 int i2, j2; 609 int dup = 0; 610 611 if (!id[0]) 612 break; 613 614 /* find duplicate, already added value */ 615 for (i2 = 0; i2 < i && !dup; i2++) { 616 DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs); 617 618 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) { 619 const char *id2 = (char *)(*devs)[j2].id; 620 621 if (!id2[0]) 622 break; 623 624 if (!strcmp(id, id2)) { 625 dup = 1; 626 break; 627 } 628 } 629 } 630 631 /* add an individual alias for every device entry */ 632 if (!dup) { 633 char acpi_id[PNP_ID_LEN]; 634 int k; 635 636 buf_printf(&mod->dev_table_buf, 637 "MODULE_ALIAS(\"pnp:d%s*\");\n", id); 638 639 /* fix broken pnp bus lowercasing */ 640 for (k = 0; k < sizeof(acpi_id); k++) 641 acpi_id[k] = toupper(id[k]); 642 buf_printf(&mod->dev_table_buf, 643 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id); 644 } 645 } 646 } 647 } 648 649 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */ 650 static int do_pcmcia_entry(const char *filename, 651 void *symval, char *alias) 652 { 653 unsigned int i; 654 DEF_FIELD(symval, pcmcia_device_id, match_flags); 655 DEF_FIELD(symval, pcmcia_device_id, manf_id); 656 DEF_FIELD(symval, pcmcia_device_id, card_id); 657 DEF_FIELD(symval, pcmcia_device_id, func_id); 658 DEF_FIELD(symval, pcmcia_device_id, function); 659 DEF_FIELD(symval, pcmcia_device_id, device_no); 660 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash); 661 662 for (i=0; i<4; i++) { 663 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]); 664 } 665 666 strcpy(alias, "pcmcia:"); 667 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID, 668 manf_id); 669 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID, 670 card_id); 671 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID, 672 func_id); 673 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION, 674 function); 675 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO, 676 device_no); 677 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]); 678 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]); 679 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]); 680 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]); 681 682 add_wildcard(alias); 683 return 1; 684 } 685 ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id, do_pcmcia_entry); 686 687 static int do_of_entry (const char *filename, void *symval, char *alias) 688 { 689 int len; 690 char *tmp; 691 DEF_FIELD_ADDR(symval, of_device_id, name); 692 DEF_FIELD_ADDR(symval, of_device_id, type); 693 DEF_FIELD_ADDR(symval, of_device_id, compatible); 694 695 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*", 696 (*type)[0] ? *type : "*"); 697 698 if (compatible[0]) 699 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "", 700 *compatible); 701 702 /* Replace all whitespace with underscores */ 703 for (tmp = alias; tmp && *tmp; tmp++) 704 if (isspace (*tmp)) 705 *tmp = '_'; 706 707 add_wildcard(alias); 708 return 1; 709 } 710 ADD_TO_DEVTABLE("of", of_device_id, do_of_entry); 711 712 static int do_vio_entry(const char *filename, void *symval, 713 char *alias) 714 { 715 char *tmp; 716 DEF_FIELD_ADDR(symval, vio_device_id, type); 717 DEF_FIELD_ADDR(symval, vio_device_id, compat); 718 719 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*", 720 (*compat)[0] ? *compat : "*"); 721 722 /* Replace all whitespace with underscores */ 723 for (tmp = alias; tmp && *tmp; tmp++) 724 if (isspace (*tmp)) 725 *tmp = '_'; 726 727 add_wildcard(alias); 728 return 1; 729 } 730 ADD_TO_DEVTABLE("vio", vio_device_id, do_vio_entry); 731 732 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 733 734 static void do_input(char *alias, 735 kernel_ulong_t *arr, unsigned int min, unsigned int max) 736 { 737 unsigned int i; 738 739 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++) 740 arr[i] = TO_NATIVE(arr[i]); 741 for (i = min; i < max; i++) 742 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG))) 743 sprintf(alias + strlen(alias), "%X,*", i); 744 } 745 746 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */ 747 static int do_input_entry(const char *filename, void *symval, 748 char *alias) 749 { 750 DEF_FIELD(symval, input_device_id, flags); 751 DEF_FIELD(symval, input_device_id, bustype); 752 DEF_FIELD(symval, input_device_id, vendor); 753 DEF_FIELD(symval, input_device_id, product); 754 DEF_FIELD(symval, input_device_id, version); 755 DEF_FIELD_ADDR(symval, input_device_id, evbit); 756 DEF_FIELD_ADDR(symval, input_device_id, keybit); 757 DEF_FIELD_ADDR(symval, input_device_id, relbit); 758 DEF_FIELD_ADDR(symval, input_device_id, absbit); 759 DEF_FIELD_ADDR(symval, input_device_id, mscbit); 760 DEF_FIELD_ADDR(symval, input_device_id, ledbit); 761 DEF_FIELD_ADDR(symval, input_device_id, sndbit); 762 DEF_FIELD_ADDR(symval, input_device_id, ffbit); 763 DEF_FIELD_ADDR(symval, input_device_id, swbit); 764 765 sprintf(alias, "input:"); 766 767 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype); 768 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor); 769 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product); 770 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version); 771 772 sprintf(alias + strlen(alias), "-e*"); 773 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT) 774 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX); 775 sprintf(alias + strlen(alias), "k*"); 776 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT) 777 do_input(alias, *keybit, 778 INPUT_DEVICE_ID_KEY_MIN_INTERESTING, 779 INPUT_DEVICE_ID_KEY_MAX); 780 sprintf(alias + strlen(alias), "r*"); 781 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT) 782 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX); 783 sprintf(alias + strlen(alias), "a*"); 784 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT) 785 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX); 786 sprintf(alias + strlen(alias), "m*"); 787 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT) 788 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX); 789 sprintf(alias + strlen(alias), "l*"); 790 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT) 791 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX); 792 sprintf(alias + strlen(alias), "s*"); 793 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT) 794 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX); 795 sprintf(alias + strlen(alias), "f*"); 796 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT) 797 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX); 798 sprintf(alias + strlen(alias), "w*"); 799 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT) 800 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX); 801 return 1; 802 } 803 ADD_TO_DEVTABLE("input", input_device_id, do_input_entry); 804 805 static int do_eisa_entry(const char *filename, void *symval, 806 char *alias) 807 { 808 DEF_FIELD_ADDR(symval, eisa_device_id, sig); 809 if (sig[0]) 810 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig); 811 else 812 strcat(alias, "*"); 813 return 1; 814 } 815 ADD_TO_DEVTABLE("eisa", eisa_device_id, do_eisa_entry); 816 817 /* Looks like: parisc:tNhvNrevNsvN */ 818 static int do_parisc_entry(const char *filename, void *symval, 819 char *alias) 820 { 821 DEF_FIELD(symval, parisc_device_id, hw_type); 822 DEF_FIELD(symval, parisc_device_id, hversion); 823 DEF_FIELD(symval, parisc_device_id, hversion_rev); 824 DEF_FIELD(symval, parisc_device_id, sversion); 825 826 strcpy(alias, "parisc:"); 827 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type); 828 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion); 829 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev); 830 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion); 831 832 add_wildcard(alias); 833 return 1; 834 } 835 ADD_TO_DEVTABLE("parisc", parisc_device_id, do_parisc_entry); 836 837 /* Looks like: sdio:cNvNdN. */ 838 static int do_sdio_entry(const char *filename, 839 void *symval, char *alias) 840 { 841 DEF_FIELD(symval, sdio_device_id, class); 842 DEF_FIELD(symval, sdio_device_id, vendor); 843 DEF_FIELD(symval, sdio_device_id, device); 844 845 strcpy(alias, "sdio:"); 846 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class); 847 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor); 848 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device); 849 add_wildcard(alias); 850 return 1; 851 } 852 ADD_TO_DEVTABLE("sdio", sdio_device_id, do_sdio_entry); 853 854 /* Looks like: ssb:vNidNrevN. */ 855 static int do_ssb_entry(const char *filename, 856 void *symval, char *alias) 857 { 858 DEF_FIELD(symval, ssb_device_id, vendor); 859 DEF_FIELD(symval, ssb_device_id, coreid); 860 DEF_FIELD(symval, ssb_device_id, revision); 861 862 strcpy(alias, "ssb:"); 863 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor); 864 ADD(alias, "id", coreid != SSB_ANY_ID, coreid); 865 ADD(alias, "rev", revision != SSB_ANY_REV, revision); 866 add_wildcard(alias); 867 return 1; 868 } 869 ADD_TO_DEVTABLE("ssb", ssb_device_id, do_ssb_entry); 870 871 /* Looks like: bcma:mNidNrevNclN. */ 872 static int do_bcma_entry(const char *filename, 873 void *symval, char *alias) 874 { 875 DEF_FIELD(symval, bcma_device_id, manuf); 876 DEF_FIELD(symval, bcma_device_id, id); 877 DEF_FIELD(symval, bcma_device_id, rev); 878 DEF_FIELD(symval, bcma_device_id, class); 879 880 strcpy(alias, "bcma:"); 881 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf); 882 ADD(alias, "id", id != BCMA_ANY_ID, id); 883 ADD(alias, "rev", rev != BCMA_ANY_REV, rev); 884 ADD(alias, "cl", class != BCMA_ANY_CLASS, class); 885 add_wildcard(alias); 886 return 1; 887 } 888 ADD_TO_DEVTABLE("bcma", bcma_device_id, do_bcma_entry); 889 890 /* Looks like: virtio:dNvN */ 891 static int do_virtio_entry(const char *filename, void *symval, 892 char *alias) 893 { 894 DEF_FIELD(symval, virtio_device_id, device); 895 DEF_FIELD(symval, virtio_device_id, vendor); 896 897 strcpy(alias, "virtio:"); 898 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device); 899 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor); 900 901 add_wildcard(alias); 902 return 1; 903 } 904 ADD_TO_DEVTABLE("virtio", virtio_device_id, do_virtio_entry); 905 906 /* 907 * Looks like: vmbus:guid 908 * Each byte of the guid will be represented by two hex characters 909 * in the name. 910 */ 911 912 static int do_vmbus_entry(const char *filename, void *symval, 913 char *alias) 914 { 915 int i; 916 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid); 917 char guid_name[(sizeof(*guid) + 1) * 2]; 918 919 for (i = 0; i < (sizeof(*guid) * 2); i += 2) 920 sprintf(&guid_name[i], "%02x", TO_NATIVE((*guid)[i/2])); 921 922 strcpy(alias, "vmbus:"); 923 strcat(alias, guid_name); 924 925 return 1; 926 } 927 ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id, do_vmbus_entry); 928 929 /* Looks like: i2c:S */ 930 static int do_i2c_entry(const char *filename, void *symval, 931 char *alias) 932 { 933 DEF_FIELD_ADDR(symval, i2c_device_id, name); 934 sprintf(alias, I2C_MODULE_PREFIX "%s", *name); 935 936 return 1; 937 } 938 ADD_TO_DEVTABLE("i2c", i2c_device_id, do_i2c_entry); 939 940 /* Looks like: spi:S */ 941 static int do_spi_entry(const char *filename, void *symval, 942 char *alias) 943 { 944 DEF_FIELD_ADDR(symval, spi_device_id, name); 945 sprintf(alias, SPI_MODULE_PREFIX "%s", *name); 946 947 return 1; 948 } 949 ADD_TO_DEVTABLE("spi", spi_device_id, do_spi_entry); 950 951 static const struct dmifield { 952 const char *prefix; 953 int field; 954 } dmi_fields[] = { 955 { "bvn", DMI_BIOS_VENDOR }, 956 { "bvr", DMI_BIOS_VERSION }, 957 { "bd", DMI_BIOS_DATE }, 958 { "svn", DMI_SYS_VENDOR }, 959 { "pn", DMI_PRODUCT_NAME }, 960 { "pvr", DMI_PRODUCT_VERSION }, 961 { "rvn", DMI_BOARD_VENDOR }, 962 { "rn", DMI_BOARD_NAME }, 963 { "rvr", DMI_BOARD_VERSION }, 964 { "cvn", DMI_CHASSIS_VENDOR }, 965 { "ct", DMI_CHASSIS_TYPE }, 966 { "cvr", DMI_CHASSIS_VERSION }, 967 { NULL, DMI_NONE } 968 }; 969 970 static void dmi_ascii_filter(char *d, const char *s) 971 { 972 /* Filter out characters we don't want to see in the modalias string */ 973 for (; *s; s++) 974 if (*s > ' ' && *s < 127 && *s != ':') 975 *(d++) = *s; 976 977 *d = 0; 978 } 979 980 981 static int do_dmi_entry(const char *filename, void *symval, 982 char *alias) 983 { 984 int i, j; 985 DEF_FIELD_ADDR(symval, dmi_system_id, matches); 986 sprintf(alias, "dmi*"); 987 988 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) { 989 for (j = 0; j < 4; j++) { 990 if ((*matches)[j].slot && 991 (*matches)[j].slot == dmi_fields[i].field) { 992 sprintf(alias + strlen(alias), ":%s*", 993 dmi_fields[i].prefix); 994 dmi_ascii_filter(alias + strlen(alias), 995 (*matches)[j].substr); 996 strcat(alias, "*"); 997 } 998 } 999 } 1000 1001 strcat(alias, ":"); 1002 return 1; 1003 } 1004 ADD_TO_DEVTABLE("dmi", dmi_system_id, do_dmi_entry); 1005 1006 static int do_platform_entry(const char *filename, 1007 void *symval, char *alias) 1008 { 1009 DEF_FIELD_ADDR(symval, platform_device_id, name); 1010 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name); 1011 return 1; 1012 } 1013 ADD_TO_DEVTABLE("platform", platform_device_id, do_platform_entry); 1014 1015 static int do_mdio_entry(const char *filename, 1016 void *symval, char *alias) 1017 { 1018 int i; 1019 DEF_FIELD(symval, mdio_device_id, phy_id); 1020 DEF_FIELD(symval, mdio_device_id, phy_id_mask); 1021 1022 alias += sprintf(alias, MDIO_MODULE_PREFIX); 1023 1024 for (i = 0; i < 32; i++) { 1025 if (!((phy_id_mask >> (31-i)) & 1)) 1026 *(alias++) = '?'; 1027 else if ((phy_id >> (31-i)) & 1) 1028 *(alias++) = '1'; 1029 else 1030 *(alias++) = '0'; 1031 } 1032 1033 /* Terminate the string */ 1034 *alias = 0; 1035 1036 return 1; 1037 } 1038 ADD_TO_DEVTABLE("mdio", mdio_device_id, do_mdio_entry); 1039 1040 /* Looks like: zorro:iN. */ 1041 static int do_zorro_entry(const char *filename, void *symval, 1042 char *alias) 1043 { 1044 DEF_FIELD(symval, zorro_device_id, id); 1045 strcpy(alias, "zorro:"); 1046 ADD(alias, "i", id != ZORRO_WILDCARD, id); 1047 return 1; 1048 } 1049 ADD_TO_DEVTABLE("zorro", zorro_device_id, do_zorro_entry); 1050 1051 /* looks like: "pnp:dD" */ 1052 static int do_isapnp_entry(const char *filename, 1053 void *symval, char *alias) 1054 { 1055 DEF_FIELD(symval, isapnp_device_id, vendor); 1056 DEF_FIELD(symval, isapnp_device_id, function); 1057 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*", 1058 'A' + ((vendor >> 2) & 0x3f) - 1, 1059 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1, 1060 'A' + ((vendor >> 8) & 0x1f) - 1, 1061 (function >> 4) & 0x0f, function & 0x0f, 1062 (function >> 12) & 0x0f, (function >> 8) & 0x0f); 1063 return 1; 1064 } 1065 ADD_TO_DEVTABLE("isapnp", isapnp_device_id, do_isapnp_entry); 1066 1067 /* Looks like: "ipack:fNvNdN". */ 1068 static int do_ipack_entry(const char *filename, 1069 void *symval, char *alias) 1070 { 1071 DEF_FIELD(symval, ipack_device_id, format); 1072 DEF_FIELD(symval, ipack_device_id, vendor); 1073 DEF_FIELD(symval, ipack_device_id, device); 1074 strcpy(alias, "ipack:"); 1075 ADD(alias, "f", format != IPACK_ANY_FORMAT, format); 1076 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor); 1077 ADD(alias, "d", device != IPACK_ANY_ID, device); 1078 add_wildcard(alias); 1079 return 1; 1080 } 1081 ADD_TO_DEVTABLE("ipack", ipack_device_id, do_ipack_entry); 1082 1083 /* 1084 * Append a match expression for a single masked hex digit. 1085 * outp points to a pointer to the character at which to append. 1086 * *outp is updated on return to point just after the appended text, 1087 * to facilitate further appending. 1088 */ 1089 static void append_nibble_mask(char **outp, 1090 unsigned int nibble, unsigned int mask) 1091 { 1092 char *p = *outp; 1093 unsigned int i; 1094 1095 switch (mask) { 1096 case 0: 1097 *p++ = '?'; 1098 break; 1099 1100 case 0xf: 1101 p += sprintf(p, "%X", nibble); 1102 break; 1103 1104 default: 1105 /* 1106 * Dumbly emit a match pattern for all possible matching 1107 * digits. This could be improved in some cases using ranges, 1108 * but it has the advantage of being trivially correct, and is 1109 * often optimal. 1110 */ 1111 *p++ = '['; 1112 for (i = 0; i < 0x10; i++) 1113 if ((i & mask) == nibble) 1114 p += sprintf(p, "%X", i); 1115 *p++ = ']'; 1116 } 1117 1118 /* Ensure that the string remains NUL-terminated: */ 1119 *p = '\0'; 1120 1121 /* Advance the caller's end-of-string pointer: */ 1122 *outp = p; 1123 } 1124 1125 /* 1126 * looks like: "amba:dN" 1127 * 1128 * N is exactly 8 digits, where each is an upper-case hex digit, or 1129 * a ? or [] pattern matching exactly one digit. 1130 */ 1131 static int do_amba_entry(const char *filename, 1132 void *symval, char *alias) 1133 { 1134 unsigned int digit; 1135 char *p = alias; 1136 DEF_FIELD(symval, amba_id, id); 1137 DEF_FIELD(symval, amba_id, mask); 1138 1139 if ((id & mask) != id) 1140 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: " 1141 "id=0x%08X, mask=0x%08X. Please fix this driver.\n", 1142 filename, id, mask); 1143 1144 p += sprintf(alias, "amba:d"); 1145 for (digit = 0; digit < 8; digit++) 1146 append_nibble_mask(&p, 1147 (id >> (4 * (7 - digit))) & 0xf, 1148 (mask >> (4 * (7 - digit))) & 0xf); 1149 1150 return 1; 1151 } 1152 ADD_TO_DEVTABLE("amba", amba_id, do_amba_entry); 1153 1154 /* 1155 * looks like: "mipscdmm:tN" 1156 * 1157 * N is exactly 2 digits, where each is an upper-case hex digit, or 1158 * a ? or [] pattern matching exactly one digit. 1159 */ 1160 static int do_mips_cdmm_entry(const char *filename, 1161 void *symval, char *alias) 1162 { 1163 DEF_FIELD(symval, mips_cdmm_device_id, type); 1164 1165 sprintf(alias, "mipscdmm:t%02X*", type); 1166 return 1; 1167 } 1168 ADD_TO_DEVTABLE("mipscdmm", mips_cdmm_device_id, do_mips_cdmm_entry); 1169 1170 /* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,* 1171 * All fields are numbers. It would be nicer to use strings for vendor 1172 * and feature, but getting those out of the build system here is too 1173 * complicated. 1174 */ 1175 1176 static int do_x86cpu_entry(const char *filename, void *symval, 1177 char *alias) 1178 { 1179 DEF_FIELD(symval, x86_cpu_id, feature); 1180 DEF_FIELD(symval, x86_cpu_id, family); 1181 DEF_FIELD(symval, x86_cpu_id, model); 1182 DEF_FIELD(symval, x86_cpu_id, vendor); 1183 1184 strcpy(alias, "cpu:type:x86,"); 1185 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor); 1186 ADD(alias, "fam", family != X86_FAMILY_ANY, family); 1187 ADD(alias, "mod", model != X86_MODEL_ANY, model); 1188 strcat(alias, ":feature:*"); 1189 if (feature != X86_FEATURE_ANY) 1190 sprintf(alias + strlen(alias), "%04X*", feature); 1191 return 1; 1192 } 1193 ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry); 1194 1195 /* LOOKS like cpu:type:*:feature:*FEAT* */ 1196 static int do_cpu_entry(const char *filename, void *symval, char *alias) 1197 { 1198 DEF_FIELD(symval, cpu_feature, feature); 1199 1200 sprintf(alias, "cpu:type:*:feature:*%04X*", feature); 1201 return 1; 1202 } 1203 ADD_TO_DEVTABLE("cpu", cpu_feature, do_cpu_entry); 1204 1205 /* Looks like: mei:S:uuid:N:* */ 1206 static int do_mei_entry(const char *filename, void *symval, 1207 char *alias) 1208 { 1209 DEF_FIELD_ADDR(symval, mei_cl_device_id, name); 1210 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid); 1211 DEF_FIELD(symval, mei_cl_device_id, version); 1212 1213 sprintf(alias, MEI_CL_MODULE_PREFIX); 1214 sprintf(alias + strlen(alias), "%s:", (*name)[0] ? *name : "*"); 1215 add_uuid(alias, *uuid); 1216 ADD(alias, ":", version != MEI_CL_VERSION_ANY, version); 1217 1218 strcat(alias, ":*"); 1219 1220 return 1; 1221 } 1222 ADD_TO_DEVTABLE("mei", mei_cl_device_id, do_mei_entry); 1223 1224 /* Looks like: rapidio:vNdNavNadN */ 1225 static int do_rio_entry(const char *filename, 1226 void *symval, char *alias) 1227 { 1228 DEF_FIELD(symval, rio_device_id, did); 1229 DEF_FIELD(symval, rio_device_id, vid); 1230 DEF_FIELD(symval, rio_device_id, asm_did); 1231 DEF_FIELD(symval, rio_device_id, asm_vid); 1232 1233 strcpy(alias, "rapidio:"); 1234 ADD(alias, "v", vid != RIO_ANY_ID, vid); 1235 ADD(alias, "d", did != RIO_ANY_ID, did); 1236 ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid); 1237 ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did); 1238 1239 add_wildcard(alias); 1240 return 1; 1241 } 1242 ADD_TO_DEVTABLE("rapidio", rio_device_id, do_rio_entry); 1243 1244 /* Looks like: ulpi:vNpN */ 1245 static int do_ulpi_entry(const char *filename, void *symval, 1246 char *alias) 1247 { 1248 DEF_FIELD(symval, ulpi_device_id, vendor); 1249 DEF_FIELD(symval, ulpi_device_id, product); 1250 1251 sprintf(alias, "ulpi:v%04xp%04x", vendor, product); 1252 1253 return 1; 1254 } 1255 ADD_TO_DEVTABLE("ulpi", ulpi_device_id, do_ulpi_entry); 1256 1257 /* Looks like: hdaudio:vNrNaN */ 1258 static int do_hda_entry(const char *filename, void *symval, char *alias) 1259 { 1260 DEF_FIELD(symval, hda_device_id, vendor_id); 1261 DEF_FIELD(symval, hda_device_id, rev_id); 1262 DEF_FIELD(symval, hda_device_id, api_version); 1263 1264 strcpy(alias, "hdaudio:"); 1265 ADD(alias, "v", vendor_id != 0, vendor_id); 1266 ADD(alias, "r", rev_id != 0, rev_id); 1267 ADD(alias, "a", api_version != 0, api_version); 1268 1269 add_wildcard(alias); 1270 return 1; 1271 } 1272 ADD_TO_DEVTABLE("hdaudio", hda_device_id, do_hda_entry); 1273 1274 /* Does namelen bytes of name exactly match the symbol? */ 1275 static bool sym_is(const char *name, unsigned namelen, const char *symbol) 1276 { 1277 if (namelen != strlen(symbol)) 1278 return false; 1279 1280 return memcmp(name, symbol, namelen) == 0; 1281 } 1282 1283 static void do_table(void *symval, unsigned long size, 1284 unsigned long id_size, 1285 const char *device_id, 1286 void *function, 1287 struct module *mod) 1288 { 1289 unsigned int i; 1290 char alias[500]; 1291 int (*do_entry)(const char *, void *entry, char *alias) = function; 1292 1293 device_id_check(mod->name, device_id, size, id_size, symval); 1294 /* Leave last one: it's the terminator. */ 1295 size -= id_size; 1296 1297 for (i = 0; i < size; i += id_size) { 1298 if (do_entry(mod->name, symval+i, alias)) { 1299 buf_printf(&mod->dev_table_buf, 1300 "MODULE_ALIAS(\"%s\");\n", alias); 1301 } 1302 } 1303 } 1304 1305 /* Create MODULE_ALIAS() statements. 1306 * At this time, we cannot write the actual output C source yet, 1307 * so we write into the mod->dev_table_buf buffer. */ 1308 void handle_moddevtable(struct module *mod, struct elf_info *info, 1309 Elf_Sym *sym, const char *symname) 1310 { 1311 void *symval; 1312 char *zeros = NULL; 1313 const char *name, *identifier; 1314 unsigned int namelen; 1315 1316 /* We're looking for a section relative symbol */ 1317 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections) 1318 return; 1319 1320 /* We're looking for an object */ 1321 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT) 1322 return; 1323 1324 /* All our symbols are of form <prefix>__mod_<name>__<identifier>_device_table. */ 1325 name = strstr(symname, "__mod_"); 1326 if (!name) 1327 return; 1328 name += strlen("__mod_"); 1329 namelen = strlen(name); 1330 if (namelen < strlen("_device_table")) 1331 return; 1332 if (strcmp(name + namelen - strlen("_device_table"), "_device_table")) 1333 return; 1334 identifier = strstr(name, "__"); 1335 if (!identifier) 1336 return; 1337 namelen = identifier - name; 1338 1339 /* Handle all-NULL symbols allocated into .bss */ 1340 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) { 1341 zeros = calloc(1, sym->st_size); 1342 symval = zeros; 1343 } else { 1344 symval = (void *)info->hdr 1345 + info->sechdrs[get_secindex(info, sym)].sh_offset 1346 + sym->st_value; 1347 } 1348 1349 /* First handle the "special" cases */ 1350 if (sym_is(name, namelen, "usb")) 1351 do_usb_table(symval, sym->st_size, mod); 1352 else if (sym_is(name, namelen, "pnp")) 1353 do_pnp_device_entry(symval, sym->st_size, mod); 1354 else if (sym_is(name, namelen, "pnp_card")) 1355 do_pnp_card_entries(symval, sym->st_size, mod); 1356 else { 1357 struct devtable **p; 1358 INIT_SECTION(__devtable); 1359 1360 for (p = __start___devtable; p < __stop___devtable; p++) { 1361 if (sym_is(name, namelen, (*p)->device_id)) { 1362 do_table(symval, sym->st_size, (*p)->id_size, 1363 (*p)->device_id, (*p)->function, mod); 1364 break; 1365 } 1366 } 1367 } 1368 free(zeros); 1369 } 1370 1371 /* Now add out buffered information to the generated C source */ 1372 void add_moddevtable(struct buffer *buf, struct module *mod) 1373 { 1374 buf_printf(buf, "\n"); 1375 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos); 1376 free(mod->dev_table_buf.p); 1377 } 1378