file2alias.c 34 KB

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