file2alias.c 34 KB

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