file2alias.c 38 KB

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