file2alias.c 34 KB

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