modpost/file2alias: Fix typo
[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                 fprintf(stderr, "*** Warning: %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                 fprintf(stderr,
221                         "*** Warning: Can't handle masks in %s:%04X\n",
222                         filename, id->class_mask);
223                 return 0;
224         }
225
226         ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
227         ADD(alias, "sc", subclass_mask == 0xFF, subclass);
228         ADD(alias, "i", interface_mask == 0xFF, interface);
229         return 1;
230 }
231
232 /* looks like: "ccw:tNmNdtNdmN" */ 
233 static int do_ccw_entry(const char *filename,
234                         struct ccw_device_id *id, char *alias)
235 {
236         id->match_flags = TO_NATIVE(id->match_flags);
237         id->cu_type = TO_NATIVE(id->cu_type);
238         id->cu_model = TO_NATIVE(id->cu_model);
239         id->dev_type = TO_NATIVE(id->dev_type);
240         id->dev_model = TO_NATIVE(id->dev_model);
241
242         strcpy(alias, "ccw:");
243         ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
244             id->cu_type);
245         ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
246             id->cu_model);
247         ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
248             id->dev_type);
249         ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
250             id->dev_model);
251         return 1;
252 }
253
254 /* Looks like: "serio:tyNprNidNexN" */
255 static int do_serio_entry(const char *filename,
256                           struct serio_device_id *id, char *alias)
257 {
258         id->type = TO_NATIVE(id->type);
259         id->proto = TO_NATIVE(id->proto);
260         id->id = TO_NATIVE(id->id);
261         id->extra = TO_NATIVE(id->extra);
262
263         strcpy(alias, "serio:");
264         ADD(alias, "ty", id->type != SERIO_ANY, id->type);
265         ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
266         ADD(alias, "id", id->id != SERIO_ANY, id->id);
267         ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
268
269         return 1;
270 }
271
272 /* looks like: "pnp:dD" */
273 static int do_pnp_entry(const char *filename,
274                         struct pnp_device_id *id, char *alias)
275 {
276         sprintf(alias, "pnp:d%s", id->id);
277         return 1;
278 }
279
280 /* looks like: "pnp:cCdD..." */
281 static int do_pnp_card_entry(const char *filename,
282                         struct pnp_card_device_id *id, char *alias)
283 {
284         int i;
285
286         sprintf(alias, "pnp:c%s", id->id);
287         for (i = 0; i < PNP_MAX_DEVICES; i++) {
288                 if (! *id->devs[i].id)
289                         break;
290                 sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
291         }
292         return 1;
293 }
294
295 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
296 static int do_pcmcia_entry(const char *filename,
297                            struct pcmcia_device_id *id, char *alias)
298 {
299         unsigned int i;
300
301         id->match_flags = TO_NATIVE(id->match_flags);
302         id->manf_id = TO_NATIVE(id->manf_id);
303         id->card_id = TO_NATIVE(id->card_id);
304         id->func_id = TO_NATIVE(id->func_id);
305         id->function = TO_NATIVE(id->function);
306         id->device_no = TO_NATIVE(id->device_no);
307
308         for (i=0; i<4; i++) {
309                 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
310        }
311
312        strcpy(alias, "pcmcia:");
313        ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
314            id->manf_id);
315        ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
316            id->card_id);
317        ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
318            id->func_id);
319        ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
320            id->function);
321        ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
322            id->device_no);
323        ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
324        ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
325        ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
326        ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
327
328        return 1;
329 }
330
331
332
333 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
334 {
335     char *tmp;
336     sprintf (alias, "of:N%sT%sC%s",
337                     of->name[0] ? of->name : "*",
338                     of->type[0] ? of->type : "*",
339                     of->compatible[0] ? of->compatible : "*");
340
341     /* Replace all whitespace with underscores */
342     for (tmp = alias; tmp && *tmp; tmp++)
343         if (isspace (*tmp))
344             *tmp = '_';
345
346     return 1;
347 }
348
349 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
350                 char *alias)
351 {
352         char *tmp;
353
354         sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
355                         vio->compat[0] ? vio->compat : "*");
356
357         /* Replace all whitespace with underscores */
358         for (tmp = alias; tmp && *tmp; tmp++)
359                 if (isspace (*tmp))
360                         *tmp = '_';
361
362         return 1;
363 }
364
365 static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias)
366 {
367         strcpy(alias, "i2c:");
368         ADD(alias, "id", 1, i2c->id);
369         return 1;
370 }
371
372 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
373
374 static void do_input(char *alias,
375                      kernel_ulong_t *arr, unsigned int min, unsigned int max)
376 {
377         unsigned int i;
378         for (i = min; i < max; i++) {
379                 if (arr[i/BITS_PER_LONG] & (1 << (i%BITS_PER_LONG)))
380                         sprintf(alias+strlen(alias), "%X,*", i);
381         }
382 }
383
384 /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
385 static int do_input_entry(const char *filename, struct input_device_id *id,
386                           char *alias)
387 {
388         sprintf(alias, "input:");
389
390         ADD(alias, "b", id->flags&INPUT_DEVICE_ID_MATCH_BUS, id->id.bustype);
391         ADD(alias, "v", id->flags&INPUT_DEVICE_ID_MATCH_VENDOR, id->id.vendor);
392         ADD(alias, "p", id->flags&INPUT_DEVICE_ID_MATCH_PRODUCT,
393             id->id.product);
394         ADD(alias, "e", id->flags&INPUT_DEVICE_ID_MATCH_VERSION,
395             id->id.version);
396
397         sprintf(alias + strlen(alias), "-e*");
398         if (id->flags&INPUT_DEVICE_ID_MATCH_EVBIT)
399                 do_input(alias, id->evbit, 0, EV_MAX);
400         sprintf(alias + strlen(alias), "k*");
401         if (id->flags&INPUT_DEVICE_ID_MATCH_KEYBIT)
402                 do_input(alias, id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
403         sprintf(alias + strlen(alias), "r*");
404         if (id->flags&INPUT_DEVICE_ID_MATCH_RELBIT)
405                 do_input(alias, id->relbit, 0, REL_MAX);
406         sprintf(alias + strlen(alias), "a*");
407         if (id->flags&INPUT_DEVICE_ID_MATCH_ABSBIT)
408                 do_input(alias, id->absbit, 0, ABS_MAX);
409         sprintf(alias + strlen(alias), "m*");
410         if (id->flags&INPUT_DEVICE_ID_MATCH_MSCIT)
411                 do_input(alias, id->mscbit, 0, MSC_MAX);
412         sprintf(alias + strlen(alias), "l*");
413         if (id->flags&INPUT_DEVICE_ID_MATCH_LEDBIT)
414                 do_input(alias, id->ledbit, 0, LED_MAX);
415         sprintf(alias + strlen(alias), "s*");
416         if (id->flags&INPUT_DEVICE_ID_MATCH_SNDBIT)
417                 do_input(alias, id->sndbit, 0, SND_MAX);
418         sprintf(alias + strlen(alias), "f*");
419         if (id->flags&INPUT_DEVICE_ID_MATCH_FFBIT)
420                 do_input(alias, id->ffbit, 0, FF_MAX);
421         sprintf(alias + strlen(alias), "w*");
422         if (id->flags&INPUT_DEVICE_ID_MATCH_SWBIT)
423                 do_input(alias, id->swbit, 0, SW_MAX);
424         return 1;
425 }
426
427 /* Ignore any prefix, eg. v850 prepends _ */
428 static inline int sym_is(const char *symbol, const char *name)
429 {
430         const char *match;
431
432         match = strstr(symbol, name);
433         if (!match)
434                 return 0;
435         return match[strlen(symbol)] == '\0';
436 }
437
438 static void do_table(void *symval, unsigned long size,
439                      unsigned long id_size,
440                      void *function,
441                      struct module *mod)
442 {
443         unsigned int i;
444         char alias[500];
445         int (*do_entry)(const char *, void *entry, char *alias) = function;
446
447         if (size % id_size || size < id_size) {
448                 fprintf(stderr, "*** Warning: %s ids %lu bad size "
449                         "(each on %lu)\n", mod->name, size, id_size);
450         }
451         /* Leave last one: it's the terminator. */
452         size -= id_size;
453
454         for (i = 0; i < size; i += id_size) {
455                 if (do_entry(mod->name, symval+i, alias)) {
456                         /* Always end in a wildcard, for future extension */
457                         if (alias[strlen(alias)-1] != '*')
458                                 strcat(alias, "*");
459                         buf_printf(&mod->dev_table_buf,
460                                    "MODULE_ALIAS(\"%s\");\n", alias);
461                 }
462         }
463 }
464
465 /* Create MODULE_ALIAS() statements.
466  * At this time, we cannot write the actual output C source yet,
467  * so we write into the mod->dev_table_buf buffer. */
468 void handle_moddevtable(struct module *mod, struct elf_info *info,
469                         Elf_Sym *sym, const char *symname)
470 {
471         void *symval;
472
473         /* We're looking for a section relative symbol */
474         if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
475                 return;
476
477         symval = (void *)info->hdr
478                 + info->sechdrs[sym->st_shndx].sh_offset
479                 + sym->st_value;
480
481         if (sym_is(symname, "__mod_pci_device_table"))
482                 do_table(symval, sym->st_size, sizeof(struct pci_device_id),
483                          do_pci_entry, mod);
484         else if (sym_is(symname, "__mod_usb_device_table"))
485                 /* special case to handle bcdDevice ranges */
486                 do_usb_table(symval, sym->st_size, mod);
487         else if (sym_is(symname, "__mod_ieee1394_device_table"))
488                 do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
489                          do_ieee1394_entry, mod);
490         else if (sym_is(symname, "__mod_ccw_device_table"))
491                 do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
492                          do_ccw_entry, mod);
493         else if (sym_is(symname, "__mod_serio_device_table"))
494                 do_table(symval, sym->st_size, sizeof(struct serio_device_id),
495                          do_serio_entry, mod);
496         else if (sym_is(symname, "__mod_pnp_device_table"))
497                 do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
498                          do_pnp_entry, mod);
499         else if (sym_is(symname, "__mod_pnp_card_device_table"))
500                 do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
501                          do_pnp_card_entry, mod);
502         else if (sym_is(symname, "__mod_pcmcia_device_table"))
503                 do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id),
504                          do_pcmcia_entry, mod);
505         else if (sym_is(symname, "__mod_of_device_table"))
506                 do_table(symval, sym->st_size, sizeof(struct of_device_id),
507                          do_of_entry, mod);
508         else if (sym_is(symname, "__mod_vio_device_table"))
509                 do_table(symval, sym->st_size, sizeof(struct vio_device_id),
510                          do_vio_entry, mod);
511         else if (sym_is(symname, "__mod_i2c_device_table"))
512                 do_table(symval, sym->st_size, sizeof(struct i2c_device_id),
513                          do_i2c_entry, mod);
514         else if (sym_is(symname, "__mod_input_device_table"))
515                 do_table(symval, sym->st_size, sizeof(struct input_device_id),
516                          do_input_entry, mod);
517 }
518
519 /* Now add out buffered information to the generated C source */
520 void add_moddevtable(struct buffer *buf, struct module *mod)
521 {
522         buf_printf(buf, "\n");
523         buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
524         free(mod->dev_table_buf.p);
525 }