Merge master.kernel.org:/pub/scm/linux/kernel/git/sam/kbuild
[safe/jmp/linux-2.6] / scripts / mod / file2alias.c
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
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
18 typedef Elf32_Addr      kernel_ulong_t;
19 #define BITS_PER_LONG 32
20 #else
21 typedef Elf64_Addr      kernel_ulong_t;
22 #define BITS_PER_LONG 64
23 #endif
24 #ifdef __sun__
25 #include <inttypes.h>
26 #else
27 #include <stdint.h>
28 #endif
29
30 #include <ctype.h>
31
32 typedef uint32_t        __u32;
33 typedef uint16_t        __u16;
34 typedef unsigned char   __u8;
35
36 /* Big exception to the "don't include kernel headers into userspace, which
37  * even potentially has different endianness and word sizes, since
38  * we handle those differences explicitly below */
39 #include "../../include/linux/mod_devicetable.h"
40 #include "../../include/linux/input.h"
41
42 #define ADD(str, sep, cond, field)                              \
43 do {                                                            \
44         strcat(str, sep);                                       \
45         if (cond)                                               \
46                 sprintf(str + strlen(str),                      \
47                         sizeof(field) == 1 ? "%02X" :           \
48                         sizeof(field) == 2 ? "%04X" :           \
49                         sizeof(field) == 4 ? "%08X" : "",       \
50                         field);                                 \
51         else                                                    \
52                 sprintf(str + strlen(str), "*");                \
53 } while(0)
54
55 /* USB is special because the bcdDevice can be matched against a numeric range */
56 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
57 static void do_usb_entry(struct usb_device_id *id,
58                          unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
59                          unsigned char range_lo, unsigned char range_hi,
60                          struct module *mod)
61 {
62         char alias[500];
63         strcpy(alias, "usb:");
64         ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
65             id->idVendor);
66         ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
67             id->idProduct);
68
69         strcat(alias, "d");
70         if (bcdDevice_initial_digits)
71                 sprintf(alias + strlen(alias), "%0*X",
72                         bcdDevice_initial_digits, bcdDevice_initial);
73         if (range_lo == range_hi)
74                 sprintf(alias + strlen(alias), "%u", range_lo);
75         else if (range_lo > 0 || range_hi < 9)
76                 sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
77         if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
78                 strcat(alias, "*");
79
80         ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
81             id->bDeviceClass);
82         ADD(alias, "dsc",
83             id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
84             id->bDeviceSubClass);
85         ADD(alias, "dp",
86             id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
87             id->bDeviceProtocol);
88         ADD(alias, "ic",
89             id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
90             id->bInterfaceClass);
91         ADD(alias, "isc",
92             id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
93             id->bInterfaceSubClass);
94         ADD(alias, "ip",
95             id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
96             id->bInterfaceProtocol);
97
98         /* Always end in a wildcard, for future extension */
99         if (alias[strlen(alias)-1] != '*')
100                 strcat(alias, "*");
101         buf_printf(&mod->dev_table_buf,
102                    "MODULE_ALIAS(\"%s\");\n", alias);
103 }
104
105 static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
106 {
107         unsigned int devlo, devhi;
108         unsigned char chi, clo;
109         int ndigits;
110
111         id->match_flags = TO_NATIVE(id->match_flags);
112         id->idVendor = TO_NATIVE(id->idVendor);
113         id->idProduct = TO_NATIVE(id->idProduct);
114
115         devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
116                 TO_NATIVE(id->bcdDevice_lo) : 0x0U;
117         devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
118                 TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
119
120         /*
121          * Some modules (visor) have empty slots as placeholder for
122          * run-time specification that results in catch-all alias
123          */
124         if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
125                 return;
126
127         /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
128         for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
129                 clo = devlo & 0xf;
130                 chi = devhi & 0xf;
131                 if (chi > 9)    /* it's bcd not hex */
132                         chi = 9;
133                 devlo >>= 4;
134                 devhi >>= 4;
135
136                 if (devlo == devhi || !ndigits) {
137                         do_usb_entry(id, devlo, ndigits, clo, chi, mod);
138                         break;
139                 }
140
141                 if (clo > 0)
142                         do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
143
144                 if (chi < 9)
145                         do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
146         }
147 }
148
149 static void do_usb_table(void *symval, unsigned long size,
150                          struct module *mod)
151 {
152         unsigned int i;
153         const unsigned long id_size = sizeof(struct usb_device_id);
154
155         if (size % id_size || size < id_size) {
156                 warn("%s ids %lu bad size "
157                      "(each on %lu)\n", mod->name, size, id_size);
158         }
159         /* Leave last one: it's the terminator. */
160         size -= id_size;
161
162         for (i = 0; i < size; i += id_size)
163                 do_usb_entry_multi(symval + i, mod);
164 }
165
166 /* Looks like: ieee1394:venNmoNspNverN */
167 static int do_ieee1394_entry(const char *filename,
168                              struct ieee1394_device_id *id, char *alias)
169 {
170         id->match_flags = TO_NATIVE(id->match_flags);
171         id->vendor_id = TO_NATIVE(id->vendor_id);
172         id->model_id = TO_NATIVE(id->model_id);
173         id->specifier_id = TO_NATIVE(id->specifier_id);
174         id->version = TO_NATIVE(id->version);
175
176         strcpy(alias, "ieee1394:");
177         ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
178             id->vendor_id);
179         ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
180             id->model_id);
181         ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
182             id->specifier_id);
183         ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
184             id->version);
185
186         return 1;
187 }
188
189 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
190 static int do_pci_entry(const char *filename,
191                         struct pci_device_id *id, char *alias)
192 {
193         /* Class field can be divided into these three. */
194         unsigned char baseclass, subclass, interface,
195                 baseclass_mask, subclass_mask, interface_mask;
196
197         id->vendor = TO_NATIVE(id->vendor);
198         id->device = TO_NATIVE(id->device);
199         id->subvendor = TO_NATIVE(id->subvendor);
200         id->subdevice = TO_NATIVE(id->subdevice);
201         id->class = TO_NATIVE(id->class);
202         id->class_mask = TO_NATIVE(id->class_mask);
203
204         strcpy(alias, "pci:");
205         ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
206         ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
207         ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
208         ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
209
210         baseclass = (id->class) >> 16;
211         baseclass_mask = (id->class_mask) >> 16;
212         subclass = (id->class) >> 8;
213         subclass_mask = (id->class_mask) >> 8;
214         interface = id->class;
215         interface_mask = id->class_mask;
216
217         if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
218             || (subclass_mask != 0 && subclass_mask != 0xFF)
219             || (interface_mask != 0 && interface_mask != 0xFF)) {
220                 warn("Can't handle masks in %s:%04X\n",
221                      filename, id->class_mask);
222                 return 0;
223         }
224
225         ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
226         ADD(alias, "sc", subclass_mask == 0xFF, subclass);
227         ADD(alias, "i", interface_mask == 0xFF, interface);
228         return 1;
229 }
230
231 /* looks like: "ccw:tNmNdtNdmN" */
232 static int do_ccw_entry(const char *filename,
233                         struct ccw_device_id *id, char *alias)
234 {
235         id->match_flags = TO_NATIVE(id->match_flags);
236         id->cu_type = TO_NATIVE(id->cu_type);
237         id->cu_model = TO_NATIVE(id->cu_model);
238         id->dev_type = TO_NATIVE(id->dev_type);
239         id->dev_model = TO_NATIVE(id->dev_model);
240
241         strcpy(alias, "ccw:");
242         ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
243             id->cu_type);
244         ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
245             id->cu_model);
246         ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
247             id->dev_type);
248         ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
249             id->dev_model);
250         return 1;
251 }
252
253 /* Looks like: "serio:tyNprNidNexN" */
254 static int do_serio_entry(const char *filename,
255                           struct serio_device_id *id, char *alias)
256 {
257         id->type = TO_NATIVE(id->type);
258         id->proto = TO_NATIVE(id->proto);
259         id->id = TO_NATIVE(id->id);
260         id->extra = TO_NATIVE(id->extra);
261
262         strcpy(alias, "serio:");
263         ADD(alias, "ty", id->type != SERIO_ANY, id->type);
264         ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
265         ADD(alias, "id", id->id != SERIO_ANY, id->id);
266         ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
267
268         return 1;
269 }
270
271 /* looks like: "pnp:dD" */
272 static int do_pnp_entry(const char *filename,
273                         struct pnp_device_id *id, char *alias)
274 {
275         sprintf(alias, "pnp:d%s", id->id);
276         return 1;
277 }
278
279 /* looks like: "pnp:cCdD..." */
280 static int do_pnp_card_entry(const char *filename,
281                         struct pnp_card_device_id *id, char *alias)
282 {
283         int i;
284
285         sprintf(alias, "pnp:c%s", id->id);
286         for (i = 0; i < PNP_MAX_DEVICES; i++) {
287                 if (! *id->devs[i].id)
288                         break;
289                 sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
290         }
291         return 1;
292 }
293
294 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
295 static int do_pcmcia_entry(const char *filename,
296                            struct pcmcia_device_id *id, char *alias)
297 {
298         unsigned int i;
299
300         id->match_flags = TO_NATIVE(id->match_flags);
301         id->manf_id = TO_NATIVE(id->manf_id);
302         id->card_id = TO_NATIVE(id->card_id);
303         id->func_id = TO_NATIVE(id->func_id);
304         id->function = TO_NATIVE(id->function);
305         id->device_no = TO_NATIVE(id->device_no);
306
307         for (i=0; i<4; i++) {
308                 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
309        }
310
311        strcpy(alias, "pcmcia:");
312        ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
313            id->manf_id);
314        ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
315            id->card_id);
316        ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
317            id->func_id);
318        ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
319            id->function);
320        ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
321            id->device_no);
322        ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
323        ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
324        ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
325        ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
326
327        return 1;
328 }
329
330
331
332 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
333 {
334     char *tmp;
335     sprintf (alias, "of:N%sT%sC%s",
336                     of->name[0] ? of->name : "*",
337                     of->type[0] ? of->type : "*",
338                     of->compatible[0] ? of->compatible : "*");
339
340     /* Replace all whitespace with underscores */
341     for (tmp = alias; tmp && *tmp; tmp++)
342         if (isspace (*tmp))
343             *tmp = '_';
344
345     return 1;
346 }
347
348 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
349                 char *alias)
350 {
351         char *tmp;
352
353         sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
354                         vio->compat[0] ? vio->compat : "*");
355
356         /* Replace all whitespace with underscores */
357         for (tmp = alias; tmp && *tmp; tmp++)
358                 if (isspace (*tmp))
359                         *tmp = '_';
360
361         return 1;
362 }
363
364 static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias)
365 {
366         strcpy(alias, "i2c:");
367         ADD(alias, "id", 1, i2c->id);
368         return 1;
369 }
370
371 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
372
373 static void do_input(char *alias,
374                      kernel_ulong_t *arr, unsigned int min, unsigned int max)
375 {
376         unsigned int i;
377         for (i = min; i < max; i++) {
378                 if (arr[i/BITS_PER_LONG] & (1 << (i%BITS_PER_LONG)))
379                         sprintf(alias+strlen(alias), "%X,*", i);
380         }
381 }
382
383 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
384 static int do_input_entry(const char *filename, struct input_device_id *id,
385                           char *alias)
386 {
387         sprintf(alias, "input:");
388
389         ADD(alias, "b", id->flags&INPUT_DEVICE_ID_MATCH_BUS, id->id.bustype);
390         ADD(alias, "v", id->flags&INPUT_DEVICE_ID_MATCH_VENDOR, id->id.vendor);
391         ADD(alias, "p", id->flags&INPUT_DEVICE_ID_MATCH_PRODUCT,
392             id->id.product);
393         ADD(alias, "e", id->flags&INPUT_DEVICE_ID_MATCH_VERSION,
394             id->id.version);
395
396         sprintf(alias + strlen(alias), "-e*");
397         if (id->flags&INPUT_DEVICE_ID_MATCH_EVBIT)
398                 do_input(alias, id->evbit, 0, EV_MAX);
399         sprintf(alias + strlen(alias), "k*");
400         if (id->flags&INPUT_DEVICE_ID_MATCH_KEYBIT)
401                 do_input(alias, id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
402         sprintf(alias + strlen(alias), "r*");
403         if (id->flags&INPUT_DEVICE_ID_MATCH_RELBIT)
404                 do_input(alias, id->relbit, 0, REL_MAX);
405         sprintf(alias + strlen(alias), "a*");
406         if (id->flags&INPUT_DEVICE_ID_MATCH_ABSBIT)
407                 do_input(alias, id->absbit, 0, ABS_MAX);
408         sprintf(alias + strlen(alias), "m*");
409         if (id->flags&INPUT_DEVICE_ID_MATCH_MSCIT)
410                 do_input(alias, id->mscbit, 0, MSC_MAX);
411         sprintf(alias + strlen(alias), "l*");
412         if (id->flags&INPUT_DEVICE_ID_MATCH_LEDBIT)
413                 do_input(alias, id->ledbit, 0, LED_MAX);
414         sprintf(alias + strlen(alias), "s*");
415         if (id->flags&INPUT_DEVICE_ID_MATCH_SNDBIT)
416                 do_input(alias, id->sndbit, 0, SND_MAX);
417         sprintf(alias + strlen(alias), "f*");
418         if (id->flags&INPUT_DEVICE_ID_MATCH_FFBIT)
419                 do_input(alias, id->ffbit, 0, FF_MAX);
420         sprintf(alias + strlen(alias), "w*");
421         if (id->flags&INPUT_DEVICE_ID_MATCH_SWBIT)
422                 do_input(alias, id->swbit, 0, SW_MAX);
423         return 1;
424 }
425
426 /* Ignore any prefix, eg. v850 prepends _ */
427 static inline int sym_is(const char *symbol, const char *name)
428 {
429         const char *match;
430
431         match = strstr(symbol, name);
432         if (!match)
433                 return 0;
434         return match[strlen(symbol)] == '\0';
435 }
436
437 static void do_table(void *symval, unsigned long size,
438                      unsigned long id_size,
439                      void *function,
440                      struct module *mod)
441 {
442         unsigned int i;
443         char alias[500];
444         int (*do_entry)(const char *, void *entry, char *alias) = function;
445
446         if (size % id_size || size < id_size) {
447                 warn("%s ids %lu bad size "
448                      "(each on %lu)\n", mod->name, size, id_size);
449         }
450         /* Leave last one: it's the terminator. */
451         size -= id_size;
452
453         for (i = 0; i < size; i += id_size) {
454                 if (do_entry(mod->name, symval+i, alias)) {
455                         /* Always end in a wildcard, for future extension */
456                         if (alias[strlen(alias)-1] != '*')
457                                 strcat(alias, "*");
458                         buf_printf(&mod->dev_table_buf,
459                                    "MODULE_ALIAS(\"%s\");\n", alias);
460                 }
461         }
462 }
463
464 /* Create MODULE_ALIAS() statements.
465  * At this time, we cannot write the actual output C source yet,
466  * so we write into the mod->dev_table_buf buffer. */
467 void handle_moddevtable(struct module *mod, struct elf_info *info,
468                         Elf_Sym *sym, const char *symname)
469 {
470         void *symval;
471
472         /* We're looking for a section relative symbol */
473         if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
474                 return;
475
476         symval = (void *)info->hdr
477                 + info->sechdrs[sym->st_shndx].sh_offset
478                 + sym->st_value;
479
480         if (sym_is(symname, "__mod_pci_device_table"))
481                 do_table(symval, sym->st_size, sizeof(struct pci_device_id),
482                          do_pci_entry, mod);
483         else if (sym_is(symname, "__mod_usb_device_table"))
484                 /* special case to handle bcdDevice ranges */
485                 do_usb_table(symval, sym->st_size, mod);
486         else if (sym_is(symname, "__mod_ieee1394_device_table"))
487                 do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
488                          do_ieee1394_entry, mod);
489         else if (sym_is(symname, "__mod_ccw_device_table"))
490                 do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
491                          do_ccw_entry, mod);
492         else if (sym_is(symname, "__mod_serio_device_table"))
493                 do_table(symval, sym->st_size, sizeof(struct serio_device_id),
494                          do_serio_entry, mod);
495         else if (sym_is(symname, "__mod_pnp_device_table"))
496                 do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
497                          do_pnp_entry, mod);
498         else if (sym_is(symname, "__mod_pnp_card_device_table"))
499                 do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
500                          do_pnp_card_entry, mod);
501         else if (sym_is(symname, "__mod_pcmcia_device_table"))
502                 do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id),
503                          do_pcmcia_entry, mod);
504         else if (sym_is(symname, "__mod_of_device_table"))
505                 do_table(symval, sym->st_size, sizeof(struct of_device_id),
506                          do_of_entry, mod);
507         else if (sym_is(symname, "__mod_vio_device_table"))
508                 do_table(symval, sym->st_size, sizeof(struct vio_device_id),
509                          do_vio_entry, mod);
510         else if (sym_is(symname, "__mod_i2c_device_table"))
511                 do_table(symval, sym->st_size, sizeof(struct i2c_device_id),
512                          do_i2c_entry, mod);
513         else if (sym_is(symname, "__mod_input_device_table"))
514                 do_table(symval, sym->st_size, sizeof(struct input_device_id),
515                          do_input_entry, mod);
516 }
517
518 /* Now add out buffered information to the generated C source */
519 void add_moddevtable(struct buffer *buf, struct module *mod)
520 {
521         buf_printf(buf, "\n");
522         buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
523         free(mod->dev_table_buf.p);
524 }