file2alias.c 38 KB

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