file2alias.c 39 KB

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