[PATCH] pcmcia: file2alias
[deliverable/linux.git] / 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 #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
27
28 typedef uint32_t __u32;
29 typedef uint16_t __u16;
30 typedef unsigned char __u8;
31
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
37 #define ADD(str, sep, cond, field) \
38 do { \
39 strcat(str, sep); \
40 if (cond) \
41 sprintf(str + strlen(str), \
42 sizeof(field) == 1 ? "%02X" : \
43 sizeof(field) == 2 ? "%04X" : \
44 sizeof(field) == 4 ? "%08X" : "", \
45 field); \
46 else \
47 sprintf(str + strlen(str), "*"); \
48 } while(0)
49
50 /* USB is special because the bcdDevice can be matched against a numeric range */
51 /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
52 static void do_usb_entry(struct usb_device_id *id,
53 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
54 unsigned char range_lo, unsigned char range_hi,
55 struct module *mod)
56 {
57 char alias[500];
58 strcpy(alias, "usb:");
59 ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
60 id->idVendor);
61 ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
62 id->idProduct);
63
64 strcat(alias, "d");
65 if (bcdDevice_initial_digits)
66 sprintf(alias + strlen(alias), "%0*X",
67 bcdDevice_initial_digits, bcdDevice_initial);
68 if (range_lo == range_hi)
69 sprintf(alias + strlen(alias), "%u", range_lo);
70 else if (range_lo > 0 || range_hi < 9)
71 sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
72 if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
73 strcat(alias, "*");
74
75 ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
76 id->bDeviceClass);
77 ADD(alias, "dsc",
78 id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
79 id->bDeviceSubClass);
80 ADD(alias, "dp",
81 id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
82 id->bDeviceProtocol);
83 ADD(alias, "ic",
84 id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
85 id->bInterfaceClass);
86 ADD(alias, "isc",
87 id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
88 id->bInterfaceSubClass);
89 ADD(alias, "ip",
90 id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
91 id->bInterfaceProtocol);
92
93 /* Always end in a wildcard, for future extension */
94 if (alias[strlen(alias)-1] != '*')
95 strcat(alias, "*");
96 buf_printf(&mod->dev_table_buf,
97 "MODULE_ALIAS(\"%s\");\n", alias);
98 }
99
100 static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
101 {
102 unsigned int devlo, devhi;
103 unsigned char chi, clo;
104 int ndigits;
105
106 id->match_flags = TO_NATIVE(id->match_flags);
107 id->idVendor = TO_NATIVE(id->idVendor);
108 id->idProduct = TO_NATIVE(id->idProduct);
109
110 devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
111 TO_NATIVE(id->bcdDevice_lo) : 0x0U;
112 devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
113 TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
114
115 /*
116 * Some modules (visor) have empty slots as placeholder for
117 * run-time specification that results in catch-all alias
118 */
119 if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
120 return;
121
122 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
123 for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
124 clo = devlo & 0xf;
125 chi = devhi & 0xf;
126 if (chi > 9) /* it's bcd not hex */
127 chi = 9;
128 devlo >>= 4;
129 devhi >>= 4;
130
131 if (devlo == devhi || !ndigits) {
132 do_usb_entry(id, devlo, ndigits, clo, chi, mod);
133 break;
134 }
135
136 if (clo > 0)
137 do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
138
139 if (chi < 9)
140 do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
141 }
142 }
143
144 static void do_usb_table(void *symval, unsigned long size,
145 struct module *mod)
146 {
147 unsigned int i;
148 const unsigned long id_size = sizeof(struct usb_device_id);
149
150 if (size % id_size || size < id_size) {
151 fprintf(stderr, "*** Warning: %s ids %lu bad size "
152 "(each on %lu)\n", mod->name, size, id_size);
153 }
154 /* Leave last one: it's the terminator. */
155 size -= id_size;
156
157 for (i = 0; i < size; i += id_size)
158 do_usb_entry_multi(symval + i, mod);
159 }
160
161 /* Looks like: ieee1394:venNmoNspNverN */
162 static int do_ieee1394_entry(const char *filename,
163 struct ieee1394_device_id *id, char *alias)
164 {
165 id->match_flags = TO_NATIVE(id->match_flags);
166 id->vendor_id = TO_NATIVE(id->vendor_id);
167 id->model_id = TO_NATIVE(id->model_id);
168 id->specifier_id = TO_NATIVE(id->specifier_id);
169 id->version = TO_NATIVE(id->version);
170
171 strcpy(alias, "ieee1394:");
172 ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
173 id->vendor_id);
174 ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
175 id->model_id);
176 ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
177 id->specifier_id);
178 ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
179 id->version);
180
181 return 1;
182 }
183
184 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
185 static int do_pci_entry(const char *filename,
186 struct pci_device_id *id, char *alias)
187 {
188 /* Class field can be divided into these three. */
189 unsigned char baseclass, subclass, interface,
190 baseclass_mask, subclass_mask, interface_mask;
191
192 id->vendor = TO_NATIVE(id->vendor);
193 id->device = TO_NATIVE(id->device);
194 id->subvendor = TO_NATIVE(id->subvendor);
195 id->subdevice = TO_NATIVE(id->subdevice);
196 id->class = TO_NATIVE(id->class);
197 id->class_mask = TO_NATIVE(id->class_mask);
198
199 strcpy(alias, "pci:");
200 ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
201 ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
202 ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
203 ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
204
205 baseclass = (id->class) >> 16;
206 baseclass_mask = (id->class_mask) >> 16;
207 subclass = (id->class) >> 8;
208 subclass_mask = (id->class_mask) >> 8;
209 interface = id->class;
210 interface_mask = id->class_mask;
211
212 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
213 || (subclass_mask != 0 && subclass_mask != 0xFF)
214 || (interface_mask != 0 && interface_mask != 0xFF)) {
215 fprintf(stderr,
216 "*** Warning: Can't handle masks in %s:%04X\n",
217 filename, id->class_mask);
218 return 0;
219 }
220
221 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
222 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
223 ADD(alias, "i", interface_mask == 0xFF, interface);
224 return 1;
225 }
226
227 /* looks like: "ccw:tNmNdtNdmN" */
228 static int do_ccw_entry(const char *filename,
229 struct ccw_device_id *id, char *alias)
230 {
231 id->match_flags = TO_NATIVE(id->match_flags);
232 id->cu_type = TO_NATIVE(id->cu_type);
233 id->cu_model = TO_NATIVE(id->cu_model);
234 id->dev_type = TO_NATIVE(id->dev_type);
235 id->dev_model = TO_NATIVE(id->dev_model);
236
237 strcpy(alias, "ccw:");
238 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
239 id->cu_type);
240 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
241 id->cu_model);
242 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
243 id->dev_type);
244 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
245 id->dev_model);
246 return 1;
247 }
248
249 /* Looks like: "serio:tyNprNidNexN" */
250 static int do_serio_entry(const char *filename,
251 struct serio_device_id *id, char *alias)
252 {
253 id->type = TO_NATIVE(id->type);
254 id->proto = TO_NATIVE(id->proto);
255 id->id = TO_NATIVE(id->id);
256 id->extra = TO_NATIVE(id->extra);
257
258 strcpy(alias, "serio:");
259 ADD(alias, "ty", id->type != SERIO_ANY, id->type);
260 ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
261 ADD(alias, "id", id->id != SERIO_ANY, id->id);
262 ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
263
264 return 1;
265 }
266
267 /* looks like: "pnp:dD" */
268 static int do_pnp_entry(const char *filename,
269 struct pnp_device_id *id, char *alias)
270 {
271 sprintf(alias, "pnp:d%s", id->id);
272 return 1;
273 }
274
275 /* looks like: "pnp:cCdD..." */
276 static int do_pnp_card_entry(const char *filename,
277 struct pnp_card_device_id *id, char *alias)
278 {
279 int i;
280
281 sprintf(alias, "pnp:c%s", id->id);
282 for (i = 0; i < PNP_MAX_DEVICES; i++) {
283 if (! *id->devs[i].id)
284 break;
285 sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
286 }
287 return 1;
288 }
289
290 /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
291 static int do_pcmcia_entry(const char *filename,
292 struct pcmcia_device_id *id, char *alias)
293 {
294 unsigned int i;
295
296 id->manf_id = TO_NATIVE(id->manf_id);
297 id->card_id = TO_NATIVE(id->card_id);
298 id->func_id = TO_NATIVE(id->func_id);
299 id->function = TO_NATIVE(id->function);
300 id->device_no = TO_NATIVE(id->device_no);
301 for (i=0; i<4; i++) {
302 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
303 }
304
305 strcpy(alias, "pcmcia:");
306 ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
307 id->manf_id);
308 ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
309 id->card_id);
310 ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
311 id->func_id);
312 ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
313 id->function);
314 ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
315 id->device_no);
316 ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
317 ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
318 ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
319 ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
320
321 return 1;
322 }
323
324
325
326 /* Ignore any prefix, eg. v850 prepends _ */
327 static inline int sym_is(const char *symbol, const char *name)
328 {
329 const char *match;
330
331 match = strstr(symbol, name);
332 if (!match)
333 return 0;
334 return match[strlen(symbol)] == '\0';
335 }
336
337 static void do_table(void *symval, unsigned long size,
338 unsigned long id_size,
339 void *function,
340 struct module *mod)
341 {
342 unsigned int i;
343 char alias[500];
344 int (*do_entry)(const char *, void *entry, char *alias) = function;
345
346 if (size % id_size || size < id_size) {
347 fprintf(stderr, "*** Warning: %s ids %lu bad size "
348 "(each on %lu)\n", mod->name, size, id_size);
349 }
350 /* Leave last one: it's the terminator. */
351 size -= id_size;
352
353 for (i = 0; i < size; i += id_size) {
354 if (do_entry(mod->name, symval+i, alias)) {
355 /* Always end in a wildcard, for future extension */
356 if (alias[strlen(alias)-1] != '*')
357 strcat(alias, "*");
358 buf_printf(&mod->dev_table_buf,
359 "MODULE_ALIAS(\"%s\");\n", alias);
360 }
361 }
362 }
363
364 /* Create MODULE_ALIAS() statements.
365 * At this time, we cannot write the actual output C source yet,
366 * so we write into the mod->dev_table_buf buffer. */
367 void handle_moddevtable(struct module *mod, struct elf_info *info,
368 Elf_Sym *sym, const char *symname)
369 {
370 void *symval;
371
372 /* We're looking for a section relative symbol */
373 if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
374 return;
375
376 symval = (void *)info->hdr
377 + info->sechdrs[sym->st_shndx].sh_offset
378 + sym->st_value;
379
380 if (sym_is(symname, "__mod_pci_device_table"))
381 do_table(symval, sym->st_size, sizeof(struct pci_device_id),
382 do_pci_entry, mod);
383 else if (sym_is(symname, "__mod_usb_device_table"))
384 /* special case to handle bcdDevice ranges */
385 do_usb_table(symval, sym->st_size, mod);
386 else if (sym_is(symname, "__mod_ieee1394_device_table"))
387 do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
388 do_ieee1394_entry, mod);
389 else if (sym_is(symname, "__mod_ccw_device_table"))
390 do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
391 do_ccw_entry, mod);
392 else if (sym_is(symname, "__mod_serio_device_table"))
393 do_table(symval, sym->st_size, sizeof(struct serio_device_id),
394 do_serio_entry, mod);
395 else if (sym_is(symname, "__mod_pnp_device_table"))
396 do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
397 do_pnp_entry, mod);
398 else if (sym_is(symname, "__mod_pnp_card_device_table"))
399 do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
400 do_pnp_card_entry, mod);
401 else if (sym_is(symname, "__mod_pcmcia_device_table"))
402 do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id),
403 do_pcmcia_entry, mod);
404 }
405
406 /* Now add out buffered information to the generated C source */
407 void add_moddevtable(struct buffer *buf, struct module *mod)
408 {
409 buf_printf(buf, "\n");
410 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
411 free(mod->dev_table_buf.p);
412 }
This page took 0.066268 seconds and 5 git commands to generate.