[PATCH] powerpc: Make sure we have an RTC before trying to adjust it
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / scripts / mod / file2alias.c
blobf2ee673329a7d509e945825820a8f2e75bcfefef
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.
5 * Copyright 2002-2003 Rusty Russell, IBM Corporation
6 * 2003 Kai Germaschewski
9 * This software may be used and distributed according to the terms
10 * of the GNU General Public License, incorporated herein by reference.
13 #include "modpost.h"
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 #else
20 typedef Elf64_Addr kernel_ulong_t;
21 #endif
22 #ifdef __sun__
23 #include <inttypes.h>
24 #else
25 #include <stdint.h>
26 #endif
28 #include <ctype.h>
30 typedef uint32_t __u32;
31 typedef uint16_t __u16;
32 typedef unsigned char __u8;
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"
39 #define ADD(str, sep, cond, field) \
40 do { \
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)
52 /* USB is special because the bcdDevice can be matched against a numeric range */
53 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
54 static 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)
59 char alias[500];
60 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);
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, "*");
77 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);
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);
102 static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
104 unsigned int devlo, devhi;
105 unsigned char chi, clo;
106 int ndigits;
108 id->match_flags = TO_NATIVE(id->match_flags);
109 id->idVendor = TO_NATIVE(id->idVendor);
110 id->idProduct = TO_NATIVE(id->idProduct);
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;
118 * Some modules (visor) have empty slots as placeholder for
119 * run-time specification that results in catch-all alias
121 if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
122 return;
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;
133 if (devlo == devhi || !ndigits) {
134 do_usb_entry(id, devlo, ndigits, clo, chi, mod);
135 break;
138 if (clo > 0)
139 do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
141 if (chi < 9)
142 do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
146 static void do_usb_table(void *symval, unsigned long size,
147 struct module *mod)
149 unsigned int i;
150 const unsigned long id_size = sizeof(struct usb_device_id);
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);
156 /* Leave last one: it's the terminator. */
157 size -= id_size;
159 for (i = 0; i < size; i += id_size)
160 do_usb_entry_multi(symval + i, mod);
163 /* Looks like: ieee1394:venNmoNspNverN */
164 static int do_ieee1394_entry(const char *filename,
165 struct ieee1394_device_id *id, char *alias)
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);
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);
183 return 1;
186 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
187 static int do_pci_entry(const char *filename,
188 struct pci_device_id *id, char *alias)
190 /* Class field can be divided into these three. */
191 unsigned char baseclass, subclass, interface,
192 baseclass_mask, subclass_mask, interface_mask;
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);
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);
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;
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;
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;
229 /* looks like: "ccw:tNmNdtNdmN" */
230 static int do_ccw_entry(const char *filename,
231 struct ccw_device_id *id, char *alias)
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);
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;
251 /* Looks like: "serio:tyNprNidNexN" */
252 static int do_serio_entry(const char *filename,
253 struct serio_device_id *id, char *alias)
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);
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);
266 return 1;
269 /* looks like: "pnp:dD" */
270 static int do_pnp_entry(const char *filename,
271 struct pnp_device_id *id, char *alias)
273 sprintf(alias, "pnp:d%s", id->id);
274 return 1;
277 /* looks like: "pnp:cCdD..." */
278 static int do_pnp_card_entry(const char *filename,
279 struct pnp_card_device_id *id, char *alias)
281 int i;
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);
289 return 1;
292 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
293 static int do_pcmcia_entry(const char *filename,
294 struct pcmcia_device_id *id, char *alias)
296 unsigned int i;
298 id->match_flags = TO_NATIVE(id->match_flags);
299 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);
305 for (i=0; i<4; i++) {
306 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
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]);
325 return 1;
330 static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
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 : "*");
338 /* Replace all whitespace with underscores */
339 for (tmp = alias; tmp && *tmp; tmp++)
340 if (isspace (*tmp))
341 *tmp = '_';
343 return 1;
346 static int do_vio_entry(const char *filename, struct vio_device_id *vio,
347 char *alias)
349 char *tmp;
351 sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
352 vio->compat[0] ? vio->compat : "*");
354 /* Replace all whitespace with underscores */
355 for (tmp = alias; tmp && *tmp; tmp++)
356 if (isspace (*tmp))
357 *tmp = '_';
359 return 1;
362 /* Ignore any prefix, eg. v850 prepends _ */
363 static inline int sym_is(const char *symbol, const char *name)
365 const char *match;
367 match = strstr(symbol, name);
368 if (!match)
369 return 0;
370 return match[strlen(symbol)] == '\0';
373 static void do_table(void *symval, unsigned long size,
374 unsigned long id_size,
375 void *function,
376 struct module *mod)
378 unsigned int i;
379 char alias[500];
380 int (*do_entry)(const char *, void *entry, char *alias) = function;
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);
386 /* Leave last one: it's the terminator. */
387 size -= id_size;
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);
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. */
403 void handle_moddevtable(struct module *mod, struct elf_info *info,
404 Elf_Sym *sym, const char *symname)
406 void *symval;
408 /* We're looking for a section relative symbol */
409 if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
410 return;
412 symval = (void *)info->hdr
413 + info->sechdrs[sym->st_shndx].sh_offset
414 + sym->st_value;
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"))
420 /* special case to handle bcdDevice ranges */
421 do_usb_table(symval, sym->st_size, mod);
422 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);
437 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);
440 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);
443 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);
449 /* Now add out buffered information to the generated C source */
450 void add_moddevtable(struct buffer *buf, struct module *mod)
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);