file2alias.c 32 KB

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