[PATCH] DVB: Other DVB core updates
[linux-2.6/history.git] / scripts / file2alias.c
blobfa8fd160b2b86a0da6f71fe217af248c74b301f9
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
7 *
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, since we can't rely on stdint.h being present. */
17 #if KERNEL_ELFCLASS == ELFCLASS32
18 typedef Elf32_Addr kernel_ulong_t;
19 #else
20 typedef Elf64_Addr kernel_ulong_t;
21 #endif
23 typedef Elf32_Word __u32;
24 typedef Elf32_Half __u16;
25 typedef unsigned char __u8;
27 /* Big exception to the "don't include kernel headers into userspace, which
28 * even potentially has different endianness and word sizes, since
29 * we handle those differences explicitly below */
30 #include "../include/linux/mod_devicetable.h"
32 #define ADD(str, sep, cond, field) \
33 do { \
34 strcat(str, sep); \
35 if (cond) \
36 sprintf(str + strlen(str), \
37 sizeof(field) == 1 ? "%02X" : \
38 sizeof(field) == 2 ? "%04X" : \
39 sizeof(field) == 4 ? "%08X" : "", \
40 field); \
41 else \
42 sprintf(str + strlen(str), "*"); \
43 } while(0)
45 /* Looks like "usb:vNpNdlNdhNdcNdscNdpNicNiscNipN" */
46 static int do_usb_entry(const char *filename,
47 struct usb_device_id *id, char *alias)
49 id->match_flags = TO_NATIVE(id->match_flags);
50 id->idVendor = TO_NATIVE(id->idVendor);
51 id->idProduct = TO_NATIVE(id->idProduct);
52 id->bcdDevice_lo = TO_NATIVE(id->bcdDevice_lo);
53 id->bcdDevice_hi = TO_NATIVE(id->bcdDevice_hi);
56 * Some modules (visor) have empty slots as placeholder for
57 * run-time specification that results in catch-all alias
59 if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
60 return 1;
62 strcpy(alias, "usb:");
63 ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
64 id->idVendor);
65 ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
66 id->idProduct);
67 ADD(alias, "dl", id->match_flags&USB_DEVICE_ID_MATCH_DEV_LO,
68 id->bcdDevice_lo);
69 ADD(alias, "dh", id->match_flags&USB_DEVICE_ID_MATCH_DEV_HI,
70 id->bcdDevice_hi);
71 ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
72 id->bDeviceClass);
73 ADD(alias, "dsc",
74 id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
75 id->bDeviceSubClass);
76 ADD(alias, "dp",
77 id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
78 id->bDeviceProtocol);
79 ADD(alias, "ic",
80 id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
81 id->bInterfaceClass);
82 ADD(alias, "isc",
83 id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
84 id->bInterfaceSubClass);
85 ADD(alias, "ip",
86 id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
87 id->bInterfaceProtocol);
88 return 1;
91 /* Looks like: ieee1394:venNmoNspNverN */
92 static int do_ieee1394_entry(const char *filename,
93 struct ieee1394_device_id *id, char *alias)
95 id->match_flags = TO_NATIVE(id->match_flags);
96 id->vendor_id = TO_NATIVE(id->vendor_id);
97 id->model_id = TO_NATIVE(id->model_id);
98 id->specifier_id = TO_NATIVE(id->specifier_id);
99 id->version = TO_NATIVE(id->version);
101 strcpy(alias, "ieee1394:");
102 ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
103 id->vendor_id);
104 ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
105 id->model_id);
106 ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
107 id->specifier_id);
108 ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
109 id->version);
111 return 1;
114 /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
115 static int do_pci_entry(const char *filename,
116 struct pci_device_id *id, char *alias)
118 /* Class field can be divided into these three. */
119 unsigned char baseclass, subclass, interface,
120 baseclass_mask, subclass_mask, interface_mask;
122 id->vendor = TO_NATIVE(id->vendor);
123 id->device = TO_NATIVE(id->device);
124 id->subvendor = TO_NATIVE(id->subvendor);
125 id->subdevice = TO_NATIVE(id->subdevice);
126 id->class = TO_NATIVE(id->class);
127 id->class_mask = TO_NATIVE(id->class_mask);
129 strcpy(alias, "pci:");
130 ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
131 ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
132 ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
133 ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
135 baseclass = (id->class) >> 16;
136 baseclass_mask = (id->class_mask) >> 16;
137 subclass = (id->class) >> 8;
138 subclass_mask = (id->class_mask) >> 8;
139 interface = id->class;
140 interface_mask = id->class_mask;
142 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
143 || (subclass_mask != 0 && subclass_mask != 0xFF)
144 || (interface_mask != 0 && interface_mask != 0xFF)) {
145 fprintf(stderr,
146 "*** Warning: Can't handle masks in %s:%04X\n",
147 filename, id->class_mask);
148 return 0;
151 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
152 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
153 ADD(alias, "i", interface_mask == 0xFF, interface);
154 return 1;
157 /* looks like: "ccw:tNmNdtNdmN" */
158 static int do_ccw_entry(const char *filename,
159 struct ccw_device_id *id, char *alias)
161 id->match_flags = TO_NATIVE(id->match_flags);
162 id->cu_type = TO_NATIVE(id->cu_type);
163 id->cu_model = TO_NATIVE(id->cu_model);
164 id->dev_type = TO_NATIVE(id->dev_type);
165 id->dev_model = TO_NATIVE(id->dev_model);
167 strcpy(alias, "ccw:");
168 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
169 id->cu_type);
170 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
171 id->cu_model);
172 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
173 id->dev_type);
174 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
175 id->dev_model);
176 return 1;
179 /* looks like: "pnp:dD" */
180 static int do_pnp_entry(const char *filename,
181 struct pnp_device_id *id, char *alias)
183 sprintf(alias, "pnp:d%s", id->id);
184 return 1;
187 /* looks like: "pnp:cCdD..." */
188 static int do_pnp_card_entry(const char *filename,
189 struct pnp_card_device_id *id, char *alias)
191 int i;
193 sprintf(alias, "pnp:c%s", id->id);
194 for (i = 0; i < PNP_MAX_DEVICES; i++) {
195 if (! *id->devs[i].id)
196 break;
197 sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
199 return 1;
202 /* Ignore any prefix, eg. v850 prepends _ */
203 static inline int sym_is(const char *symbol, const char *name)
205 const char *match;
207 match = strstr(symbol, name);
208 if (!match)
209 return 0;
210 return match[strlen(symbol)] == '\0';
213 static void do_table(void *symval, unsigned long size,
214 unsigned long id_size,
215 void *function,
216 struct module *mod)
218 unsigned int i;
219 char alias[500];
220 int (*do_entry)(const char *, void *entry, char *alias) = function;
222 if (size % id_size || size < id_size) {
223 fprintf(stderr, "*** Warning: %s ids %lu bad size "
224 "(each on %lu)\n", mod->name, size, id_size);
226 /* Leave last one: it's the terminator. */
227 size -= id_size;
229 for (i = 0; i < size; i += id_size) {
230 if (do_entry(mod->name, symval+i, alias)) {
231 /* Always end in a wildcard, for future extension */
232 if (alias[strlen(alias)-1] != '*')
233 strcat(alias, "*");
234 buf_printf(&mod->dev_table_buf,
235 "MODULE_ALIAS(\"%s\");\n", alias);
240 /* Create MODULE_ALIAS() statements.
241 * At this time, we cannot write the actual output C source yet,
242 * so we write into the mod->dev_table_buf buffer. */
243 void handle_moddevtable(struct module *mod, struct elf_info *info,
244 Elf_Sym *sym, const char *symname)
246 void *symval;
248 /* We're looking for a section relative symbol */
249 if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
250 return;
252 symval = (void *)info->hdr
253 + info->sechdrs[sym->st_shndx].sh_offset
254 + sym->st_value;
256 if (sym_is(symname, "__mod_pci_device_table"))
257 do_table(symval, sym->st_size, sizeof(struct pci_device_id),
258 do_pci_entry, mod);
259 else if (sym_is(symname, "__mod_usb_device_table"))
260 do_table(symval, sym->st_size, sizeof(struct usb_device_id),
261 do_usb_entry, mod);
262 else if (sym_is(symname, "__mod_ieee1394_device_table"))
263 do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
264 do_ieee1394_entry, mod);
265 else if (sym_is(symname, "__mod_ccw_device_table"))
266 do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
267 do_ccw_entry, mod);
268 else if (sym_is(symname, "__mod_pnp_device_table"))
269 do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
270 do_pnp_entry, mod);
271 else if (sym_is(symname, "__mod_pnp_card_device_table"))
272 do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
273 do_pnp_card_entry, mod);
276 /* Now add out buffered information to the generated C source */
277 void add_moddevtable(struct buffer *buf, struct module *mod)
279 buf_printf(buf, "\n");
280 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
281 free(mod->dev_table_buf.p);