file2alias.c 32 KB

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