file2alias.c 34 KB

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