blob: f2ee673329a7d509e945825820a8f2e75bcfefef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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
13#include "modpost.h"
14
15/* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
16 * use either stdint.h or inttypes.h for the rest. */
17#if KERNEL_ELFCLASS == ELFCLASS32
18typedef Elf32_Addr kernel_ulong_t;
19#else
20typedef Elf64_Addr kernel_ulong_t;
21#endif
22#ifdef __sun__
23#include <inttypes.h>
24#else
25#include <stdint.h>
26#endif
27
Jeff Mahoney5e655772005-07-06 15:44:41 -040028#include <ctype.h>
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030typedef uint32_t __u32;
31typedef uint16_t __u16;
32typedef unsigned char __u8;
33
34/* Big exception to the "don't include kernel headers into userspace, which
35 * even potentially has different endianness and word sizes, since
36 * we handle those differences explicitly below */
37#include "../../include/linux/mod_devicetable.h"
38
39#define ADD(str, sep, cond, field) \
40do { \
41 strcat(str, sep); \
42 if (cond) \
43 sprintf(str + strlen(str), \
44 sizeof(field) == 1 ? "%02X" : \
45 sizeof(field) == 2 ? "%04X" : \
46 sizeof(field) == 4 ? "%08X" : "", \
47 field); \
48 else \
49 sprintf(str + strlen(str), "*"); \
50} while(0)
51
Roman Kaganb19dcd92005-04-22 15:07:01 -070052/* USB is special because the bcdDevice can be matched against a numeric range */
53/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
54static void do_usb_entry(struct usb_device_id *id,
55 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
56 unsigned char range_lo, unsigned char range_hi,
57 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Roman Kaganb19dcd92005-04-22 15:07:01 -070059 char alias[500];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 strcpy(alias, "usb:");
61 ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
62 id->idVendor);
63 ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
64 id->idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -070065
66 strcat(alias, "d");
67 if (bcdDevice_initial_digits)
68 sprintf(alias + strlen(alias), "%0*X",
69 bcdDevice_initial_digits, bcdDevice_initial);
70 if (range_lo == range_hi)
71 sprintf(alias + strlen(alias), "%u", range_lo);
72 else if (range_lo > 0 || range_hi < 9)
73 sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
74 if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
75 strcat(alias, "*");
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
78 id->bDeviceClass);
79 ADD(alias, "dsc",
80 id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
81 id->bDeviceSubClass);
82 ADD(alias, "dp",
83 id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
84 id->bDeviceProtocol);
85 ADD(alias, "ic",
86 id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
87 id->bInterfaceClass);
88 ADD(alias, "isc",
89 id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
90 id->bInterfaceSubClass);
91 ADD(alias, "ip",
92 id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
93 id->bInterfaceProtocol);
Roman Kaganb19dcd92005-04-22 15:07:01 -070094
95 /* Always end in a wildcard, for future extension */
96 if (alias[strlen(alias)-1] != '*')
97 strcat(alias, "*");
98 buf_printf(&mod->dev_table_buf,
99 "MODULE_ALIAS(\"%s\");\n", alias);
100}
101
102static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
103{
104 unsigned int devlo, devhi;
105 unsigned char chi, clo;
106 int ndigits;
107
108 id->match_flags = TO_NATIVE(id->match_flags);
109 id->idVendor = TO_NATIVE(id->idVendor);
110 id->idProduct = TO_NATIVE(id->idProduct);
111
112 devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
113 TO_NATIVE(id->bcdDevice_lo) : 0x0U;
114 devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
115 TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
116
117 /*
118 * Some modules (visor) have empty slots as placeholder for
119 * run-time specification that results in catch-all alias
120 */
121 if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
122 return;
123
124 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
125 for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
126 clo = devlo & 0xf;
127 chi = devhi & 0xf;
128 if (chi > 9) /* it's bcd not hex */
129 chi = 9;
130 devlo >>= 4;
131 devhi >>= 4;
132
133 if (devlo == devhi || !ndigits) {
134 do_usb_entry(id, devlo, ndigits, clo, chi, mod);
135 break;
136 }
137
138 if (clo > 0)
139 do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
140
141 if (chi < 9)
142 do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
143 }
144}
145
146static void do_usb_table(void *symval, unsigned long size,
147 struct module *mod)
148{
149 unsigned int i;
150 const unsigned long id_size = sizeof(struct usb_device_id);
151
152 if (size % id_size || size < id_size) {
153 fprintf(stderr, "*** Warning: %s ids %lu bad size "
154 "(each on %lu)\n", mod->name, size, id_size);
155 }
156 /* Leave last one: it's the terminator. */
157 size -= id_size;
158
159 for (i = 0; i < size; i += id_size)
160 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/* Looks like: ieee1394:venNmoNspNverN */
164static int do_ieee1394_entry(const char *filename,
165 struct ieee1394_device_id *id, char *alias)
166{
167 id->match_flags = TO_NATIVE(id->match_flags);
168 id->vendor_id = TO_NATIVE(id->vendor_id);
169 id->model_id = TO_NATIVE(id->model_id);
170 id->specifier_id = TO_NATIVE(id->specifier_id);
171 id->version = TO_NATIVE(id->version);
172
173 strcpy(alias, "ieee1394:");
174 ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
175 id->vendor_id);
176 ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
177 id->model_id);
178 ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
179 id->specifier_id);
180 ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
181 id->version);
182
183 return 1;
184}
185
186/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
187static int do_pci_entry(const char *filename,
188 struct pci_device_id *id, char *alias)
189{
190 /* Class field can be divided into these three. */
191 unsigned char baseclass, subclass, interface,
192 baseclass_mask, subclass_mask, interface_mask;
193
194 id->vendor = TO_NATIVE(id->vendor);
195 id->device = TO_NATIVE(id->device);
196 id->subvendor = TO_NATIVE(id->subvendor);
197 id->subdevice = TO_NATIVE(id->subdevice);
198 id->class = TO_NATIVE(id->class);
199 id->class_mask = TO_NATIVE(id->class_mask);
200
201 strcpy(alias, "pci:");
202 ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
203 ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
204 ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
205 ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
206
207 baseclass = (id->class) >> 16;
208 baseclass_mask = (id->class_mask) >> 16;
209 subclass = (id->class) >> 8;
210 subclass_mask = (id->class_mask) >> 8;
211 interface = id->class;
212 interface_mask = id->class_mask;
213
214 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
215 || (subclass_mask != 0 && subclass_mask != 0xFF)
216 || (interface_mask != 0 && interface_mask != 0xFF)) {
217 fprintf(stderr,
218 "*** Warning: Can't handle masks in %s:%04X\n",
219 filename, id->class_mask);
220 return 0;
221 }
222
223 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
224 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
225 ADD(alias, "i", interface_mask == 0xFF, interface);
226 return 1;
227}
228
229/* looks like: "ccw:tNmNdtNdmN" */
230static int do_ccw_entry(const char *filename,
231 struct ccw_device_id *id, char *alias)
232{
233 id->match_flags = TO_NATIVE(id->match_flags);
234 id->cu_type = TO_NATIVE(id->cu_type);
235 id->cu_model = TO_NATIVE(id->cu_model);
236 id->dev_type = TO_NATIVE(id->dev_type);
237 id->dev_model = TO_NATIVE(id->dev_model);
238
239 strcpy(alias, "ccw:");
240 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
241 id->cu_type);
242 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
243 id->cu_model);
244 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
245 id->dev_type);
246 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
247 id->dev_model);
248 return 1;
249}
250
251/* Looks like: "serio:tyNprNidNexN" */
252static int do_serio_entry(const char *filename,
253 struct serio_device_id *id, char *alias)
254{
255 id->type = TO_NATIVE(id->type);
256 id->proto = TO_NATIVE(id->proto);
257 id->id = TO_NATIVE(id->id);
258 id->extra = TO_NATIVE(id->extra);
259
260 strcpy(alias, "serio:");
261 ADD(alias, "ty", id->type != SERIO_ANY, id->type);
262 ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
263 ADD(alias, "id", id->id != SERIO_ANY, id->id);
264 ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
265
266 return 1;
267}
268
269/* looks like: "pnp:dD" */
270static int do_pnp_entry(const char *filename,
271 struct pnp_device_id *id, char *alias)
272{
273 sprintf(alias, "pnp:d%s", id->id);
274 return 1;
275}
276
277/* looks like: "pnp:cCdD..." */
278static int do_pnp_card_entry(const char *filename,
279 struct pnp_card_device_id *id, char *alias)
280{
281 int i;
282
283 sprintf(alias, "pnp:c%s", id->id);
284 for (i = 0; i < PNP_MAX_DEVICES; i++) {
285 if (! *id->devs[i].id)
286 break;
287 sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
288 }
289 return 1;
290}
291
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700292/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
293static int do_pcmcia_entry(const char *filename,
294 struct pcmcia_device_id *id, char *alias)
295{
296 unsigned int i;
297
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200298 id->match_flags = TO_NATIVE(id->match_flags);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700299 id->manf_id = TO_NATIVE(id->manf_id);
300 id->card_id = TO_NATIVE(id->card_id);
301 id->func_id = TO_NATIVE(id->func_id);
302 id->function = TO_NATIVE(id->function);
303 id->device_no = TO_NATIVE(id->device_no);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200304
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700305 for (i=0; i<4; i++) {
306 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
307 }
308
309 strcpy(alias, "pcmcia:");
310 ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
311 id->manf_id);
312 ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
313 id->card_id);
314 ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
315 id->func_id);
316 ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
317 id->function);
318 ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
319 id->device_no);
320 ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
321 ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
322 ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
323 ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
324
325 return 1;
326}
327
328
329
Jeff Mahoney5e655772005-07-06 15:44:41 -0400330static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
331{
332 char *tmp;
333 sprintf (alias, "of:N%sT%sC%s",
334 of->name[0] ? of->name : "*",
335 of->type[0] ? of->type : "*",
336 of->compatible[0] ? of->compatible : "*");
337
338 /* Replace all whitespace with underscores */
339 for (tmp = alias; tmp && *tmp; tmp++)
340 if (isspace (*tmp))
341 *tmp = '_';
342
343 return 1;
344}
345
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000346static int do_vio_entry(const char *filename, struct vio_device_id *vio,
347 char *alias)
348{
349 char *tmp;
350
351 sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
352 vio->compat[0] ? vio->compat : "*");
353
354 /* Replace all whitespace with underscores */
355 for (tmp = alias; tmp && *tmp; tmp++)
356 if (isspace (*tmp))
357 *tmp = '_';
358
359 return 1;
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/* Ignore any prefix, eg. v850 prepends _ */
363static inline int sym_is(const char *symbol, const char *name)
364{
365 const char *match;
366
367 match = strstr(symbol, name);
368 if (!match)
369 return 0;
370 return match[strlen(symbol)] == '\0';
371}
372
373static void do_table(void *symval, unsigned long size,
374 unsigned long id_size,
375 void *function,
376 struct module *mod)
377{
378 unsigned int i;
379 char alias[500];
380 int (*do_entry)(const char *, void *entry, char *alias) = function;
381
382 if (size % id_size || size < id_size) {
383 fprintf(stderr, "*** Warning: %s ids %lu bad size "
384 "(each on %lu)\n", mod->name, size, id_size);
385 }
386 /* Leave last one: it's the terminator. */
387 size -= id_size;
388
389 for (i = 0; i < size; i += id_size) {
390 if (do_entry(mod->name, symval+i, alias)) {
391 /* Always end in a wildcard, for future extension */
392 if (alias[strlen(alias)-1] != '*')
393 strcat(alias, "*");
394 buf_printf(&mod->dev_table_buf,
395 "MODULE_ALIAS(\"%s\");\n", alias);
396 }
397 }
398}
399
400/* Create MODULE_ALIAS() statements.
401 * At this time, we cannot write the actual output C source yet,
402 * so we write into the mod->dev_table_buf buffer. */
403void handle_moddevtable(struct module *mod, struct elf_info *info,
404 Elf_Sym *sym, const char *symname)
405{
406 void *symval;
407
408 /* We're looking for a section relative symbol */
409 if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
410 return;
411
412 symval = (void *)info->hdr
413 + info->sechdrs[sym->st_shndx].sh_offset
414 + sym->st_value;
415
416 if (sym_is(symname, "__mod_pci_device_table"))
417 do_table(symval, sym->st_size, sizeof(struct pci_device_id),
418 do_pci_entry, mod);
419 else if (sym_is(symname, "__mod_usb_device_table"))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700420 /* special case to handle bcdDevice ranges */
421 do_usb_table(symval, sym->st_size, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 else if (sym_is(symname, "__mod_ieee1394_device_table"))
423 do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
424 do_ieee1394_entry, mod);
425 else if (sym_is(symname, "__mod_ccw_device_table"))
426 do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
427 do_ccw_entry, mod);
428 else if (sym_is(symname, "__mod_serio_device_table"))
429 do_table(symval, sym->st_size, sizeof(struct serio_device_id),
430 do_serio_entry, mod);
431 else if (sym_is(symname, "__mod_pnp_device_table"))
432 do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
433 do_pnp_entry, mod);
434 else if (sym_is(symname, "__mod_pnp_card_device_table"))
435 do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
436 do_pnp_card_entry, mod);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700437 else if (sym_is(symname, "__mod_pcmcia_device_table"))
438 do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id),
439 do_pcmcia_entry, mod);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400440 else if (sym_is(symname, "__mod_of_device_table"))
441 do_table(symval, sym->st_size, sizeof(struct of_device_id),
442 do_of_entry, mod);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000443 else if (sym_is(symname, "__mod_vio_device_table"))
444 do_table(symval, sym->st_size, sizeof(struct vio_device_id),
445 do_vio_entry, mod);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/* Now add out buffered information to the generated C source */
450void add_moddevtable(struct buffer *buf, struct module *mod)
451{
452 buf_printf(buf, "\n");
453 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
454 free(mod->dev_table_buf.p);
455}