file2alias.c 34 KB

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