]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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" | |
6543becf | 14 | #include "devicetable-offsets.h" |
1da177e4 LT |
15 | |
16 | /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and | |
17 | * use either stdint.h or inttypes.h for the rest. */ | |
18 | #if KERNEL_ELFCLASS == ELFCLASS32 | |
19 | typedef Elf32_Addr kernel_ulong_t; | |
1d8f430c | 20 | #define BITS_PER_LONG 32 |
1da177e4 LT |
21 | #else |
22 | typedef Elf64_Addr kernel_ulong_t; | |
1d8f430c | 23 | #define BITS_PER_LONG 64 |
1da177e4 LT |
24 | #endif |
25 | #ifdef __sun__ | |
26 | #include <inttypes.h> | |
27 | #else | |
28 | #include <stdint.h> | |
29 | #endif | |
30 | ||
5e655772 | 31 | #include <ctype.h> |
626596e2 | 32 | #include <stdbool.h> |
5e655772 | 33 | |
1da177e4 LT |
34 | typedef uint32_t __u32; |
35 | typedef uint16_t __u16; | |
36 | typedef unsigned char __u8; | |
37 | ||
38 | /* Big exception to the "don't include kernel headers into userspace, which | |
62070fa4 | 39 | * even potentially has different endianness and word sizes, since |
1da177e4 LT |
40 | * we handle those differences explicitly below */ |
41 | #include "../../include/linux/mod_devicetable.h" | |
42 | ||
e49ce141 RR |
43 | /* This array collects all instances that use the generic do_table */ |
44 | struct devtable { | |
45 | const char *device_id; /* name of table, __mod_<name>_device_table. */ | |
46 | unsigned long id_size; | |
47 | void *function; | |
48 | }; | |
49 | ||
dd2a3aca AB |
50 | #define ___cat(a,b) a ## b |
51 | #define __cat(a,b) ___cat(a,b) | |
52 | ||
53 | /* we need some special handling for this host tool running eventually on | |
54 | * Darwin. The Mach-O section handling is a bit different than ELF section | |
55 | * handling. The differnces in detail are: | |
56 | * a) we have segments which have sections | |
57 | * b) we need a API call to get the respective section symbols */ | |
58 | #if defined(__MACH__) | |
59 | #include <mach-o/getsect.h> | |
60 | ||
61 | #define INIT_SECTION(name) do { \ | |
62 | unsigned long name ## _len; \ | |
63 | char *__cat(pstart_,name) = getsectdata("__TEXT", \ | |
64 | #name, &__cat(name,_len)); \ | |
65 | char *__cat(pstop_,name) = __cat(pstart_,name) + \ | |
66 | __cat(name, _len); \ | |
67 | __cat(__start_,name) = (void *)__cat(pstart_,name); \ | |
68 | __cat(__stop_,name) = (void *)__cat(pstop_,name); \ | |
69 | } while (0) | |
70 | #define SECTION(name) __attribute__((section("__TEXT, " #name))) | |
71 | ||
72 | struct devtable **__start___devtable, **__stop___devtable; | |
73 | #else | |
74 | #define INIT_SECTION(name) /* no-op for ELF */ | |
75 | #define SECTION(name) __attribute__((section(#name))) | |
76 | ||
e49ce141 RR |
77 | /* We construct a table of pointers in an ELF section (pointers generally |
78 | * go unpadded by gcc). ld creates boundary syms for us. */ | |
79 | extern struct devtable *__start___devtable[], *__stop___devtable[]; | |
dd2a3aca | 80 | #endif /* __MACH__ */ |
e49ce141 | 81 | |
04130cc9 DT |
82 | #if !defined(__used) |
83 | # if __GNUC__ == 3 && __GNUC_MINOR__ < 3 | |
84 | # define __used __attribute__((__unused__)) | |
85 | # else | |
86 | # define __used __attribute__((__used__)) | |
87 | # endif | |
e49ce141 RR |
88 | #endif |
89 | ||
6543becf AS |
90 | /* Define a variable f that holds the value of field f of struct devid |
91 | * based at address m. | |
92 | */ | |
93 | #define DEF_FIELD(m, devid, f) \ | |
94 | typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f)) | |
95 | /* Define a variable f that holds the address of field f of struct devid | |
96 | * based at address m. Due to the way typeof works, for a field of type | |
97 | * T[N] the variable has type T(*)[N], _not_ T*. | |
98 | */ | |
99 | #define DEF_FIELD_ADDR(m, devid, f) \ | |
100 | typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f) | |
101 | ||
e49ce141 RR |
102 | /* Add a table entry. We test function type matches while we're here. */ |
103 | #define ADD_TO_DEVTABLE(device_id, type, function) \ | |
104 | static struct devtable __cat(devtable,__LINE__) = { \ | |
105 | device_id + 0*sizeof((function)((const char *)NULL, \ | |
6543becf | 106 | (void *)NULL, \ |
e49ce141 | 107 | (char *)NULL)), \ |
6543becf | 108 | SIZE_##type, (function) }; \ |
dd2a3aca AB |
109 | static struct devtable *SECTION(__devtable) __used \ |
110 | __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__) | |
e49ce141 | 111 | |
1da177e4 LT |
112 | #define ADD(str, sep, cond, field) \ |
113 | do { \ | |
114 | strcat(str, sep); \ | |
115 | if (cond) \ | |
116 | sprintf(str + strlen(str), \ | |
117 | sizeof(field) == 1 ? "%02X" : \ | |
118 | sizeof(field) == 2 ? "%04X" : \ | |
119 | sizeof(field) == 4 ? "%08X" : "", \ | |
120 | field); \ | |
121 | else \ | |
122 | sprintf(str + strlen(str), "*"); \ | |
123 | } while(0) | |
124 | ||
ac551828 JD |
125 | /* Always end in a wildcard, for future extension */ |
126 | static inline void add_wildcard(char *str) | |
127 | { | |
128 | int len = strlen(str); | |
129 | ||
130 | if (str[len - 1] != '*') | |
131 | strcat(str + len, "*"); | |
132 | } | |
133 | ||
fb33d816 SR |
134 | /** |
135 | * Check that sizeof(device_id type) are consistent with size of section | |
136 | * in .o file. If in-consistent then userspace and kernel does not agree | |
137 | * on actual size which is a bug. | |
e0049825 | 138 | * Also verify that the final entry in the table is all zeros. |
4ce6efed | 139 | * Ignore both checks if build host differ from target host and size differs. |
fb33d816 | 140 | **/ |
e0049825 KC |
141 | static void device_id_check(const char *modname, const char *device_id, |
142 | unsigned long size, unsigned long id_size, | |
143 | void *symval) | |
fb33d816 | 144 | { |
e0049825 KC |
145 | int i; |
146 | ||
fb33d816 SR |
147 | if (size % id_size || size < id_size) { |
148 | fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo " | |
149 | "of the size of section __mod_%s_device_table=%lu.\n" | |
150 | "Fix definition of struct %s_device_id " | |
151 | "in mod_devicetable.h\n", | |
152 | modname, device_id, id_size, device_id, size, device_id); | |
153 | } | |
e0049825 KC |
154 | /* Verify last one is a terminator */ |
155 | for (i = 0; i < id_size; i++ ) { | |
156 | if (*(uint8_t*)(symval+size-id_size+i)) { | |
157 | fprintf(stderr,"%s: struct %s_device_id is %lu bytes. " | |
158 | "The last of %lu is:\n", | |
159 | modname, device_id, id_size, size / id_size); | |
160 | for (i = 0; i < id_size; i++ ) | |
161 | fprintf(stderr,"0x%02x ", | |
162 | *(uint8_t*)(symval+size-id_size+i) ); | |
163 | fprintf(stderr,"\n"); | |
164 | fatal("%s: struct %s_device_id is not terminated " | |
165 | "with a NULL entry!\n", modname, device_id); | |
166 | } | |
167 | } | |
fb33d816 SR |
168 | } |
169 | ||
b19dcd93 | 170 | /* USB is special because the bcdDevice can be matched against a numeric range */ |
81df2d59 | 171 | /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */ |
6543becf | 172 | static void do_usb_entry(void *symval, |
b19dcd93 RK |
173 | unsigned int bcdDevice_initial, int bcdDevice_initial_digits, |
174 | unsigned char range_lo, unsigned char range_hi, | |
afe2dab4 | 175 | unsigned char max, struct module *mod) |
1da177e4 | 176 | { |
b19dcd93 | 177 | char alias[500]; |
6543becf AS |
178 | DEF_FIELD(symval, usb_device_id, match_flags); |
179 | DEF_FIELD(symval, usb_device_id, idVendor); | |
180 | DEF_FIELD(symval, usb_device_id, idProduct); | |
181 | DEF_FIELD(symval, usb_device_id, bcdDevice_lo); | |
182 | DEF_FIELD(symval, usb_device_id, bDeviceClass); | |
183 | DEF_FIELD(symval, usb_device_id, bDeviceSubClass); | |
184 | DEF_FIELD(symval, usb_device_id, bDeviceProtocol); | |
185 | DEF_FIELD(symval, usb_device_id, bInterfaceClass); | |
186 | DEF_FIELD(symval, usb_device_id, bInterfaceSubClass); | |
187 | DEF_FIELD(symval, usb_device_id, bInterfaceProtocol); | |
188 | DEF_FIELD(symval, usb_device_id, bInterfaceNumber); | |
189 | ||
1da177e4 | 190 | strcpy(alias, "usb:"); |
6543becf AS |
191 | ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR, |
192 | idVendor); | |
193 | ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT, | |
194 | idProduct); | |
b19dcd93 RK |
195 | |
196 | strcat(alias, "d"); | |
197 | if (bcdDevice_initial_digits) | |
198 | sprintf(alias + strlen(alias), "%0*X", | |
199 | bcdDevice_initial_digits, bcdDevice_initial); | |
200 | if (range_lo == range_hi) | |
afe2dab4 NM |
201 | sprintf(alias + strlen(alias), "%X", range_lo); |
202 | else if (range_lo > 0 || range_hi < max) { | |
203 | if (range_lo > 0x9 || range_hi < 0xA) | |
204 | sprintf(alias + strlen(alias), | |
205 | "[%X-%X]", | |
206 | range_lo, | |
207 | range_hi); | |
208 | else { | |
209 | sprintf(alias + strlen(alias), | |
210 | range_lo < 0x9 ? "[%X-9" : "[%X", | |
211 | range_lo); | |
212 | sprintf(alias + strlen(alias), | |
03b56329 JMM |
213 | range_hi > 0xA ? "A-%X]" : "%X]", |
214 | range_hi); | |
afe2dab4 NM |
215 | } |
216 | } | |
6543becf | 217 | if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1)) |
b19dcd93 RK |
218 | strcat(alias, "*"); |
219 | ||
6543becf AS |
220 | ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS, |
221 | bDeviceClass); | |
222 | ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS, | |
223 | bDeviceSubClass); | |
224 | ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL, | |
225 | bDeviceProtocol); | |
226 | ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS, | |
227 | bInterfaceClass); | |
228 | ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS, | |
229 | bInterfaceSubClass); | |
230 | ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL, | |
231 | bInterfaceProtocol); | |
232 | ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER, | |
233 | bInterfaceNumber); | |
b19dcd93 | 234 | |
ac551828 | 235 | add_wildcard(alias); |
b19dcd93 RK |
236 | buf_printf(&mod->dev_table_buf, |
237 | "MODULE_ALIAS(\"%s\");\n", alias); | |
238 | } | |
239 | ||
55f49f26 NM |
240 | /* Handles increment/decrement of BCD formatted integers */ |
241 | /* Returns the previous value, so it works like i++ or i-- */ | |
242 | static unsigned int incbcd(unsigned int *bcd, | |
243 | int inc, | |
244 | unsigned char max, | |
245 | size_t chars) | |
246 | { | |
247 | unsigned int init = *bcd, i, j; | |
248 | unsigned long long c, dec = 0; | |
249 | ||
250 | /* If bcd is not in BCD format, just increment */ | |
251 | if (max > 0x9) { | |
252 | *bcd += inc; | |
253 | return init; | |
254 | } | |
255 | ||
256 | /* Convert BCD to Decimal */ | |
257 | for (i=0 ; i < chars ; i++) { | |
258 | c = (*bcd >> (i << 2)) & 0xf; | |
259 | c = c > 9 ? 9 : c; /* force to bcd just in case */ | |
260 | for (j=0 ; j < i ; j++) | |
261 | c = c * 10; | |
262 | dec += c; | |
263 | } | |
264 | ||
265 | /* Do our increment/decrement */ | |
266 | dec += inc; | |
267 | *bcd = 0; | |
268 | ||
269 | /* Convert back to BCD */ | |
270 | for (i=0 ; i < chars ; i++) { | |
271 | for (c=1,j=0 ; j < i ; j++) | |
272 | c = c * 10; | |
273 | c = (dec / c) % 10; | |
274 | *bcd += c << (i << 2); | |
275 | } | |
276 | return init; | |
277 | } | |
278 | ||
6543becf | 279 | static void do_usb_entry_multi(void *symval, struct module *mod) |
b19dcd93 RK |
280 | { |
281 | unsigned int devlo, devhi; | |
afe2dab4 | 282 | unsigned char chi, clo, max; |
b19dcd93 RK |
283 | int ndigits; |
284 | ||
6543becf AS |
285 | DEF_FIELD(symval, usb_device_id, match_flags); |
286 | DEF_FIELD(symval, usb_device_id, idVendor); | |
287 | DEF_FIELD(symval, usb_device_id, idProduct); | |
288 | DEF_FIELD(symval, usb_device_id, bcdDevice_lo); | |
289 | DEF_FIELD(symval, usb_device_id, bcdDevice_hi); | |
290 | DEF_FIELD(symval, usb_device_id, bDeviceClass); | |
291 | DEF_FIELD(symval, usb_device_id, bInterfaceClass); | |
b19dcd93 | 292 | |
6543becf AS |
293 | devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ? |
294 | bcdDevice_lo : 0x0U; | |
295 | devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ? | |
296 | bcdDevice_hi : ~0x0U; | |
b19dcd93 | 297 | |
afe2dab4 NM |
298 | /* Figure out if this entry is in bcd or hex format */ |
299 | max = 0x9; /* Default to decimal format */ | |
6543becf | 300 | for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) { |
afe2dab4 NM |
301 | clo = (devlo >> (ndigits << 2)) & 0xf; |
302 | chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf; | |
303 | if (clo > max || chi > max) { | |
304 | max = 0xf; | |
305 | break; | |
306 | } | |
307 | } | |
308 | ||
b19dcd93 RK |
309 | /* |
310 | * Some modules (visor) have empty slots as placeholder for | |
311 | * run-time specification that results in catch-all alias | |
312 | */ | |
6543becf | 313 | if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass)) |
b19dcd93 RK |
314 | return; |
315 | ||
316 | /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */ | |
6543becf | 317 | for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) { |
b19dcd93 RK |
318 | clo = devlo & 0xf; |
319 | chi = devhi & 0xf; | |
afe2dab4 NM |
320 | if (chi > max) /* If we are in bcd mode, truncate if necessary */ |
321 | chi = max; | |
b19dcd93 RK |
322 | devlo >>= 4; |
323 | devhi >>= 4; | |
324 | ||
325 | if (devlo == devhi || !ndigits) { | |
6543becf | 326 | do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod); |
b19dcd93 RK |
327 | break; |
328 | } | |
329 | ||
afe2dab4 | 330 | if (clo > 0x0) |
6543becf | 331 | do_usb_entry(symval, |
55f49f26 | 332 | incbcd(&devlo, 1, max, |
6543becf | 333 | sizeof(bcdDevice_lo) * 2), |
55f49f26 | 334 | ndigits, clo, max, max, mod); |
b19dcd93 | 335 | |
afe2dab4 | 336 | if (chi < max) |
6543becf | 337 | do_usb_entry(symval, |
55f49f26 | 338 | incbcd(&devhi, -1, max, |
6543becf | 339 | sizeof(bcdDevice_lo) * 2), |
55f49f26 | 340 | ndigits, 0x0, chi, max, mod); |
b19dcd93 RK |
341 | } |
342 | } | |
343 | ||
344 | static void do_usb_table(void *symval, unsigned long size, | |
345 | struct module *mod) | |
346 | { | |
347 | unsigned int i; | |
6543becf | 348 | const unsigned long id_size = SIZE_usb_device_id; |
b19dcd93 | 349 | |
e0049825 | 350 | device_id_check(mod->name, "usb", size, id_size, symval); |
fb33d816 | 351 | |
b19dcd93 RK |
352 | /* Leave last one: it's the terminator. */ |
353 | size -= id_size; | |
354 | ||
355 | for (i = 0; i < size; i += id_size) | |
356 | do_usb_entry_multi(symval + i, mod); | |
1da177e4 LT |
357 | } |
358 | ||
e8c84f9a JS |
359 | /* Looks like: hid:bNvNpN */ |
360 | static int do_hid_entry(const char *filename, | |
6543becf | 361 | void *symval, char *alias) |
e8c84f9a | 362 | { |
6543becf AS |
363 | DEF_FIELD(symval, hid_device_id, bus); |
364 | DEF_FIELD(symval, hid_device_id, group); | |
365 | DEF_FIELD(symval, hid_device_id, vendor); | |
366 | DEF_FIELD(symval, hid_device_id, product); | |
e8c84f9a | 367 | |
7431fb76 | 368 | sprintf(alias, "hid:"); |
6543becf AS |
369 | ADD(alias, "b", bus != HID_BUS_ANY, bus); |
370 | ADD(alias, "g", group != HID_GROUP_ANY, group); | |
371 | ADD(alias, "v", vendor != HID_ANY_ID, vendor); | |
372 | ADD(alias, "p", product != HID_ANY_ID, product); | |
e8c84f9a JS |
373 | |
374 | return 1; | |
375 | } | |
6543becf | 376 | ADD_TO_DEVTABLE("hid", hid_device_id, do_hid_entry); |
e8c84f9a | 377 | |
1da177e4 LT |
378 | /* Looks like: ieee1394:venNmoNspNverN */ |
379 | static int do_ieee1394_entry(const char *filename, | |
6543becf | 380 | void *symval, char *alias) |
1da177e4 | 381 | { |
6543becf AS |
382 | DEF_FIELD(symval, ieee1394_device_id, match_flags); |
383 | DEF_FIELD(symval, ieee1394_device_id, vendor_id); | |
384 | DEF_FIELD(symval, ieee1394_device_id, model_id); | |
385 | DEF_FIELD(symval, ieee1394_device_id, specifier_id); | |
386 | DEF_FIELD(symval, ieee1394_device_id, version); | |
1da177e4 LT |
387 | |
388 | strcpy(alias, "ieee1394:"); | |
6543becf AS |
389 | ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID, |
390 | vendor_id); | |
391 | ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID, | |
392 | model_id); | |
393 | ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID, | |
394 | specifier_id); | |
395 | ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION, | |
396 | version); | |
1da177e4 | 397 | |
ac551828 | 398 | add_wildcard(alias); |
1da177e4 LT |
399 | return 1; |
400 | } | |
6543becf | 401 | ADD_TO_DEVTABLE("ieee1394", ieee1394_device_id, do_ieee1394_entry); |
1da177e4 LT |
402 | |
403 | /* Looks like: pci:vNdNsvNsdNbcNscNiN. */ | |
404 | static int do_pci_entry(const char *filename, | |
6543becf | 405 | void *symval, char *alias) |
1da177e4 LT |
406 | { |
407 | /* Class field can be divided into these three. */ | |
408 | unsigned char baseclass, subclass, interface, | |
409 | baseclass_mask, subclass_mask, interface_mask; | |
410 | ||
6543becf AS |
411 | DEF_FIELD(symval, pci_device_id, vendor); |
412 | DEF_FIELD(symval, pci_device_id, device); | |
413 | DEF_FIELD(symval, pci_device_id, subvendor); | |
414 | DEF_FIELD(symval, pci_device_id, subdevice); | |
415 | DEF_FIELD(symval, pci_device_id, class); | |
416 | DEF_FIELD(symval, pci_device_id, class_mask); | |
1da177e4 LT |
417 | |
418 | strcpy(alias, "pci:"); | |
6543becf AS |
419 | ADD(alias, "v", vendor != PCI_ANY_ID, vendor); |
420 | ADD(alias, "d", device != PCI_ANY_ID, device); | |
421 | ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor); | |
422 | ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice); | |
423 | ||
424 | baseclass = (class) >> 16; | |
425 | baseclass_mask = (class_mask) >> 16; | |
426 | subclass = (class) >> 8; | |
427 | subclass_mask = (class_mask) >> 8; | |
428 | interface = class; | |
429 | interface_mask = class_mask; | |
1da177e4 LT |
430 | |
431 | if ((baseclass_mask != 0 && baseclass_mask != 0xFF) | |
432 | || (subclass_mask != 0 && subclass_mask != 0xFF) | |
433 | || (interface_mask != 0 && interface_mask != 0xFF)) { | |
cb80514d | 434 | warn("Can't handle masks in %s:%04X\n", |
6543becf | 435 | filename, class_mask); |
1da177e4 LT |
436 | return 0; |
437 | } | |
438 | ||
439 | ADD(alias, "bc", baseclass_mask == 0xFF, baseclass); | |
440 | ADD(alias, "sc", subclass_mask == 0xFF, subclass); | |
441 | ADD(alias, "i", interface_mask == 0xFF, interface); | |
ac551828 | 442 | add_wildcard(alias); |
1da177e4 LT |
443 | return 1; |
444 | } | |
6543becf | 445 | ADD_TO_DEVTABLE("pci", pci_device_id, do_pci_entry); |
1da177e4 | 446 | |
62070fa4 | 447 | /* looks like: "ccw:tNmNdtNdmN" */ |
1da177e4 | 448 | static int do_ccw_entry(const char *filename, |
6543becf | 449 | void *symval, char *alias) |
1da177e4 | 450 | { |
6543becf AS |
451 | DEF_FIELD(symval, ccw_device_id, match_flags); |
452 | DEF_FIELD(symval, ccw_device_id, cu_type); | |
453 | DEF_FIELD(symval, ccw_device_id, cu_model); | |
454 | DEF_FIELD(symval, ccw_device_id, dev_type); | |
455 | DEF_FIELD(symval, ccw_device_id, dev_model); | |
1da177e4 LT |
456 | |
457 | strcpy(alias, "ccw:"); | |
6543becf AS |
458 | ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE, |
459 | cu_type); | |
460 | ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL, | |
461 | cu_model); | |
462 | ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE, | |
463 | dev_type); | |
464 | ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL, | |
465 | dev_model); | |
ac551828 | 466 | add_wildcard(alias); |
1da177e4 LT |
467 | return 1; |
468 | } | |
6543becf | 469 | ADD_TO_DEVTABLE("ccw", ccw_device_id, do_ccw_entry); |
1da177e4 | 470 | |
1534c382 MS |
471 | /* looks like: "ap:tN" */ |
472 | static int do_ap_entry(const char *filename, | |
6543becf | 473 | void *symval, char *alias) |
1534c382 | 474 | { |
6543becf AS |
475 | DEF_FIELD(symval, ap_device_id, dev_type); |
476 | ||
477 | sprintf(alias, "ap:t%02X*", dev_type); | |
1534c382 MS |
478 | return 1; |
479 | } | |
6543becf | 480 | ADD_TO_DEVTABLE("ap", ap_device_id, do_ap_entry); |
1534c382 | 481 | |
7e9db9ea CH |
482 | /* looks like: "css:tN" */ |
483 | static int do_css_entry(const char *filename, | |
6543becf | 484 | void *symval, char *alias) |
7e9db9ea | 485 | { |
6543becf AS |
486 | DEF_FIELD(symval, css_device_id, type); |
487 | ||
488 | sprintf(alias, "css:t%01X", type); | |
7e9db9ea CH |
489 | return 1; |
490 | } | |
6543becf | 491 | ADD_TO_DEVTABLE("css", css_device_id, do_css_entry); |
7e9db9ea | 492 | |
1da177e4 LT |
493 | /* Looks like: "serio:tyNprNidNexN" */ |
494 | static int do_serio_entry(const char *filename, | |
6543becf | 495 | void *symval, char *alias) |
1da177e4 | 496 | { |
6543becf AS |
497 | DEF_FIELD(symval, serio_device_id, type); |
498 | DEF_FIELD(symval, serio_device_id, proto); | |
499 | DEF_FIELD(symval, serio_device_id, id); | |
500 | DEF_FIELD(symval, serio_device_id, extra); | |
1da177e4 LT |
501 | |
502 | strcpy(alias, "serio:"); | |
6543becf AS |
503 | ADD(alias, "ty", type != SERIO_ANY, type); |
504 | ADD(alias, "pr", proto != SERIO_ANY, proto); | |
505 | ADD(alias, "id", id != SERIO_ANY, id); | |
506 | ADD(alias, "ex", extra != SERIO_ANY, extra); | |
1da177e4 | 507 | |
ac551828 | 508 | add_wildcard(alias); |
1da177e4 LT |
509 | return 1; |
510 | } | |
6543becf | 511 | ADD_TO_DEVTABLE("serio", serio_device_id, do_serio_entry); |
1da177e4 | 512 | |
29b71a1c TR |
513 | /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */ |
514 | static int do_acpi_entry(const char *filename, | |
6543becf | 515 | void *symval, char *alias) |
29b71a1c | 516 | { |
6543becf AS |
517 | DEF_FIELD_ADDR(symval, acpi_device_id, id); |
518 | sprintf(alias, "acpi*:%s:*", *id); | |
29b71a1c TR |
519 | return 1; |
520 | } | |
6543becf | 521 | ADD_TO_DEVTABLE("acpi", acpi_device_id, do_acpi_entry); |
29b71a1c | 522 | |
1da177e4 | 523 | /* looks like: "pnp:dD" */ |
22454cb9 KS |
524 | static void do_pnp_device_entry(void *symval, unsigned long size, |
525 | struct module *mod) | |
1da177e4 | 526 | { |
6543becf | 527 | const unsigned long id_size = SIZE_pnp_device_id; |
5e4c6564 | 528 | const unsigned int count = (size / id_size)-1; |
5e4c6564 | 529 | unsigned int i; |
22454cb9 KS |
530 | |
531 | device_id_check(mod->name, "pnp", size, id_size, symval); | |
532 | ||
5e4c6564 | 533 | for (i = 0; i < count; i++) { |
6543becf AS |
534 | DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id); |
535 | char acpi_id[sizeof(*id)]; | |
72638f59 | 536 | int j; |
5e4c6564 KS |
537 | |
538 | buf_printf(&mod->dev_table_buf, | |
6543becf | 539 | "MODULE_ALIAS(\"pnp:d%s*\");\n", *id); |
72638f59 KS |
540 | |
541 | /* fix broken pnp bus lowercasing */ | |
542 | for (j = 0; j < sizeof(acpi_id); j++) | |
6543becf | 543 | acpi_id[j] = toupper((*id)[j]); |
5e4c6564 | 544 | buf_printf(&mod->dev_table_buf, |
72638f59 | 545 | "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id); |
5e4c6564 | 546 | } |
1da177e4 LT |
547 | } |
548 | ||
0c81eed4 KS |
549 | /* looks like: "pnp:dD" for every device of the card */ |
550 | static void do_pnp_card_entries(void *symval, unsigned long size, | |
551 | struct module *mod) | |
1da177e4 | 552 | { |
6543becf | 553 | const unsigned long id_size = SIZE_pnp_card_device_id; |
0c81eed4 | 554 | const unsigned int count = (size / id_size)-1; |
0c81eed4 | 555 | unsigned int i; |
1da177e4 | 556 | |
0c81eed4 KS |
557 | device_id_check(mod->name, "pnp", size, id_size, symval); |
558 | ||
559 | for (i = 0; i < count; i++) { | |
560 | unsigned int j; | |
6543becf | 561 | DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs); |
0c81eed4 KS |
562 | |
563 | for (j = 0; j < PNP_MAX_DEVICES; j++) { | |
6543becf | 564 | const char *id = (char *)(*devs)[j].id; |
0c81eed4 KS |
565 | int i2, j2; |
566 | int dup = 0; | |
567 | ||
568 | if (!id[0]) | |
569 | break; | |
570 | ||
571 | /* find duplicate, already added value */ | |
572 | for (i2 = 0; i2 < i && !dup; i2++) { | |
6543becf | 573 | DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs); |
0c81eed4 KS |
574 | |
575 | for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) { | |
6543becf | 576 | const char *id2 = (char *)(*devs)[j2].id; |
0c81eed4 KS |
577 | |
578 | if (!id2[0]) | |
579 | break; | |
580 | ||
581 | if (!strcmp(id, id2)) { | |
582 | dup = 1; | |
583 | break; | |
584 | } | |
585 | } | |
586 | } | |
587 | ||
588 | /* add an individual alias for every device entry */ | |
22454cb9 | 589 | if (!dup) { |
6543becf | 590 | char acpi_id[PNP_ID_LEN]; |
72638f59 KS |
591 | int k; |
592 | ||
0c81eed4 KS |
593 | buf_printf(&mod->dev_table_buf, |
594 | "MODULE_ALIAS(\"pnp:d%s*\");\n", id); | |
72638f59 KS |
595 | |
596 | /* fix broken pnp bus lowercasing */ | |
597 | for (k = 0; k < sizeof(acpi_id); k++) | |
598 | acpi_id[k] = toupper(id[k]); | |
22454cb9 | 599 | buf_printf(&mod->dev_table_buf, |
72638f59 | 600 | "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id); |
22454cb9 | 601 | } |
0c81eed4 | 602 | } |
1da177e4 | 603 | } |
1da177e4 LT |
604 | } |
605 | ||
90829cfe DB |
606 | /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */ |
607 | static int do_pcmcia_entry(const char *filename, | |
6543becf | 608 | void *symval, char *alias) |
90829cfe DB |
609 | { |
610 | unsigned int i; | |
6543becf AS |
611 | DEF_FIELD(symval, pcmcia_device_id, match_flags); |
612 | DEF_FIELD(symval, pcmcia_device_id, manf_id); | |
613 | DEF_FIELD(symval, pcmcia_device_id, card_id); | |
614 | DEF_FIELD(symval, pcmcia_device_id, func_id); | |
615 | DEF_FIELD(symval, pcmcia_device_id, function); | |
616 | DEF_FIELD(symval, pcmcia_device_id, device_no); | |
617 | DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash); | |
4fb7edce | 618 | |
90829cfe | 619 | for (i=0; i<4; i++) { |
6543becf AS |
620 | (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]); |
621 | } | |
622 | ||
623 | strcpy(alias, "pcmcia:"); | |
624 | ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID, | |
625 | manf_id); | |
626 | ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID, | |
627 | card_id); | |
628 | ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID, | |
629 | func_id); | |
630 | ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION, | |
631 | function); | |
632 | ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO, | |
633 | device_no); | |
634 | ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]); | |
635 | ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]); | |
636 | ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]); | |
637 | ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]); | |
90829cfe | 638 | |
ac551828 | 639 | add_wildcard(alias); |
6543becf | 640 | return 1; |
90829cfe | 641 | } |
6543becf | 642 | ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id, do_pcmcia_entry); |
90829cfe | 643 | |
6543becf | 644 | static int do_of_entry (const char *filename, void *symval, char *alias) |
5e655772 | 645 | { |
d1ab4235 | 646 | int len; |
5e655772 | 647 | char *tmp; |
6543becf AS |
648 | DEF_FIELD_ADDR(symval, of_device_id, name); |
649 | DEF_FIELD_ADDR(symval, of_device_id, type); | |
650 | DEF_FIELD_ADDR(symval, of_device_id, compatible); | |
651 | ||
d1ab4235 | 652 | len = sprintf (alias, "of:N%sT%s", |
6543becf AS |
653 | (*name)[0] ? *name : "*", |
654 | (*type)[0] ? *type : "*"); | |
d1ab4235 | 655 | |
6543becf | 656 | if (compatible[0]) |
d1ab4235 | 657 | sprintf (&alias[len], "%sC%s", |
6543becf AS |
658 | (*type)[0] ? "*" : "", |
659 | *compatible); | |
5e655772 JM |
660 | |
661 | /* Replace all whitespace with underscores */ | |
662 | for (tmp = alias; tmp && *tmp; tmp++) | |
663 | if (isspace (*tmp)) | |
664 | *tmp = '_'; | |
665 | ||
ac551828 | 666 | add_wildcard(alias); |
5e655772 JM |
667 | return 1; |
668 | } | |
6543becf | 669 | ADD_TO_DEVTABLE("of", of_device_id, do_of_entry); |
5e655772 | 670 | |
6543becf | 671 | static int do_vio_entry(const char *filename, void *symval, |
fb120da6 SR |
672 | char *alias) |
673 | { | |
674 | char *tmp; | |
6543becf AS |
675 | DEF_FIELD_ADDR(symval, vio_device_id, type); |
676 | DEF_FIELD_ADDR(symval, vio_device_id, compat); | |
fb120da6 | 677 | |
6543becf AS |
678 | sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*", |
679 | (*compat)[0] ? *compat : "*"); | |
fb120da6 SR |
680 | |
681 | /* Replace all whitespace with underscores */ | |
682 | for (tmp = alias; tmp && *tmp; tmp++) | |
683 | if (isspace (*tmp)) | |
684 | *tmp = '_'; | |
685 | ||
ac551828 | 686 | add_wildcard(alias); |
fb120da6 SR |
687 | return 1; |
688 | } | |
6543becf | 689 | ADD_TO_DEVTABLE("vio", vio_device_id, do_vio_entry); |
fb120da6 | 690 | |
1d8f430c RR |
691 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
692 | ||
693 | static void do_input(char *alias, | |
694 | kernel_ulong_t *arr, unsigned int min, unsigned int max) | |
695 | { | |
696 | unsigned int i; | |
ddc5d341 | 697 | |
6543becf AS |
698 | for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++) |
699 | arr[i] = TO_NATIVE(arr[i]); | |
ddc5d341 | 700 | for (i = min; i < max; i++) |
e0e92632 | 701 | if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG))) |
ddc5d341 | 702 | sprintf(alias + strlen(alias), "%X,*", i); |
1d8f430c RR |
703 | } |
704 | ||
705 | /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */ | |
6543becf | 706 | static int do_input_entry(const char *filename, void *symval, |
1d8f430c RR |
707 | char *alias) |
708 | { | |
6543becf AS |
709 | DEF_FIELD(symval, input_device_id, flags); |
710 | DEF_FIELD(symval, input_device_id, bustype); | |
711 | DEF_FIELD(symval, input_device_id, vendor); | |
712 | DEF_FIELD(symval, input_device_id, product); | |
713 | DEF_FIELD(symval, input_device_id, version); | |
714 | DEF_FIELD_ADDR(symval, input_device_id, evbit); | |
715 | DEF_FIELD_ADDR(symval, input_device_id, keybit); | |
716 | DEF_FIELD_ADDR(symval, input_device_id, relbit); | |
717 | DEF_FIELD_ADDR(symval, input_device_id, absbit); | |
718 | DEF_FIELD_ADDR(symval, input_device_id, mscbit); | |
719 | DEF_FIELD_ADDR(symval, input_device_id, ledbit); | |
720 | DEF_FIELD_ADDR(symval, input_device_id, sndbit); | |
721 | DEF_FIELD_ADDR(symval, input_device_id, ffbit); | |
722 | DEF_FIELD_ADDR(symval, input_device_id, swbit); | |
723 | ||
1d8f430c RR |
724 | sprintf(alias, "input:"); |
725 | ||
6543becf AS |
726 | ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype); |
727 | ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor); | |
728 | ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product); | |
729 | ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version); | |
1d8f430c RR |
730 | |
731 | sprintf(alias + strlen(alias), "-e*"); | |
6543becf AS |
732 | if (flags & INPUT_DEVICE_ID_MATCH_EVBIT) |
733 | do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX); | |
1d8f430c | 734 | sprintf(alias + strlen(alias), "k*"); |
6543becf AS |
735 | if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT) |
736 | do_input(alias, *keybit, | |
dc24f0e7 SR |
737 | INPUT_DEVICE_ID_KEY_MIN_INTERESTING, |
738 | INPUT_DEVICE_ID_KEY_MAX); | |
1d8f430c | 739 | sprintf(alias + strlen(alias), "r*"); |
6543becf AS |
740 | if (flags & INPUT_DEVICE_ID_MATCH_RELBIT) |
741 | do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX); | |
1d8f430c | 742 | sprintf(alias + strlen(alias), "a*"); |
6543becf AS |
743 | if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT) |
744 | do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX); | |
1d8f430c | 745 | sprintf(alias + strlen(alias), "m*"); |
6543becf AS |
746 | if (flags & INPUT_DEVICE_ID_MATCH_MSCIT) |
747 | do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX); | |
1d8f430c | 748 | sprintf(alias + strlen(alias), "l*"); |
6543becf AS |
749 | if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT) |
750 | do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX); | |
1d8f430c | 751 | sprintf(alias + strlen(alias), "s*"); |
6543becf AS |
752 | if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT) |
753 | do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX); | |
1d8f430c | 754 | sprintf(alias + strlen(alias), "f*"); |
6543becf AS |
755 | if (flags & INPUT_DEVICE_ID_MATCH_FFBIT) |
756 | do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX); | |
1d8f430c | 757 | sprintf(alias + strlen(alias), "w*"); |
6543becf AS |
758 | if (flags & INPUT_DEVICE_ID_MATCH_SWBIT) |
759 | do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX); | |
1d8f430c RR |
760 | return 1; |
761 | } | |
6543becf | 762 | ADD_TO_DEVTABLE("input", input_device_id, do_input_entry); |
1d8f430c | 763 | |
6543becf | 764 | static int do_eisa_entry(const char *filename, void *symval, |
07563c71 MT |
765 | char *alias) |
766 | { | |
6543becf AS |
767 | DEF_FIELD_ADDR(symval, eisa_device_id, sig); |
768 | if (sig[0]) | |
769 | sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig); | |
ac551828 JD |
770 | else |
771 | strcat(alias, "*"); | |
07563c71 MT |
772 | return 1; |
773 | } | |
6543becf | 774 | ADD_TO_DEVTABLE("eisa", eisa_device_id, do_eisa_entry); |
07563c71 | 775 | |
f3cf2673 | 776 | /* Looks like: parisc:tNhvNrevNsvN */ |
6543becf | 777 | static int do_parisc_entry(const char *filename, void *symval, |
f3cf2673 KM |
778 | char *alias) |
779 | { | |
6543becf AS |
780 | DEF_FIELD(symval, parisc_device_id, hw_type); |
781 | DEF_FIELD(symval, parisc_device_id, hversion); | |
782 | DEF_FIELD(symval, parisc_device_id, hversion_rev); | |
783 | DEF_FIELD(symval, parisc_device_id, sversion); | |
f3cf2673 KM |
784 | |
785 | strcpy(alias, "parisc:"); | |
6543becf AS |
786 | ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type); |
787 | ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion); | |
788 | ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev); | |
789 | ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion); | |
f3cf2673 | 790 | |
ac551828 | 791 | add_wildcard(alias); |
f3cf2673 KM |
792 | return 1; |
793 | } | |
6543becf | 794 | ADD_TO_DEVTABLE("parisc", parisc_device_id, do_parisc_entry); |
f3cf2673 | 795 | |
d59b66c7 PO |
796 | /* Looks like: sdio:cNvNdN. */ |
797 | static int do_sdio_entry(const char *filename, | |
6543becf | 798 | void *symval, char *alias) |
d59b66c7 | 799 | { |
6543becf AS |
800 | DEF_FIELD(symval, sdio_device_id, class); |
801 | DEF_FIELD(symval, sdio_device_id, vendor); | |
802 | DEF_FIELD(symval, sdio_device_id, device); | |
d59b66c7 PO |
803 | |
804 | strcpy(alias, "sdio:"); | |
6543becf AS |
805 | ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class); |
806 | ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor); | |
807 | ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device); | |
ac551828 | 808 | add_wildcard(alias); |
038a5008 LT |
809 | return 1; |
810 | } | |
6543becf | 811 | ADD_TO_DEVTABLE("sdio", sdio_device_id, do_sdio_entry); |
038a5008 | 812 | |
61e115a5 MB |
813 | /* Looks like: ssb:vNidNrevN. */ |
814 | static int do_ssb_entry(const char *filename, | |
6543becf | 815 | void *symval, char *alias) |
61e115a5 | 816 | { |
6543becf AS |
817 | DEF_FIELD(symval, ssb_device_id, vendor); |
818 | DEF_FIELD(symval, ssb_device_id, coreid); | |
819 | DEF_FIELD(symval, ssb_device_id, revision); | |
d59b66c7 | 820 | |
61e115a5 | 821 | strcpy(alias, "ssb:"); |
6543becf AS |
822 | ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor); |
823 | ADD(alias, "id", coreid != SSB_ANY_ID, coreid); | |
824 | ADD(alias, "rev", revision != SSB_ANY_REV, revision); | |
ac551828 | 825 | add_wildcard(alias); |
d59b66c7 PO |
826 | return 1; |
827 | } | |
6543becf | 828 | ADD_TO_DEVTABLE("ssb", ssb_device_id, do_ssb_entry); |
d59b66c7 | 829 | |
8369ae33 RM |
830 | /* Looks like: bcma:mNidNrevNclN. */ |
831 | static int do_bcma_entry(const char *filename, | |
6543becf | 832 | void *symval, char *alias) |
8369ae33 | 833 | { |
6543becf AS |
834 | DEF_FIELD(symval, bcma_device_id, manuf); |
835 | DEF_FIELD(symval, bcma_device_id, id); | |
836 | DEF_FIELD(symval, bcma_device_id, rev); | |
837 | DEF_FIELD(symval, bcma_device_id, class); | |
8369ae33 RM |
838 | |
839 | strcpy(alias, "bcma:"); | |
6543becf AS |
840 | ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf); |
841 | ADD(alias, "id", id != BCMA_ANY_ID, id); | |
842 | ADD(alias, "rev", rev != BCMA_ANY_REV, rev); | |
843 | ADD(alias, "cl", class != BCMA_ANY_CLASS, class); | |
8369ae33 RM |
844 | add_wildcard(alias); |
845 | return 1; | |
846 | } | |
6543becf | 847 | ADD_TO_DEVTABLE("bcma", bcma_device_id, do_bcma_entry); |
8369ae33 | 848 | |
b01d9f28 | 849 | /* Looks like: virtio:dNvN */ |
6543becf | 850 | static int do_virtio_entry(const char *filename, void *symval, |
b01d9f28 RR |
851 | char *alias) |
852 | { | |
6543becf AS |
853 | DEF_FIELD(symval, virtio_device_id, device); |
854 | DEF_FIELD(symval, virtio_device_id, vendor); | |
b01d9f28 RR |
855 | |
856 | strcpy(alias, "virtio:"); | |
6543becf AS |
857 | ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device); |
858 | ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor); | |
b01d9f28 | 859 | |
ac551828 | 860 | add_wildcard(alias); |
b01d9f28 RR |
861 | return 1; |
862 | } | |
6543becf | 863 | ADD_TO_DEVTABLE("virtio", virtio_device_id, do_virtio_entry); |
b01d9f28 | 864 | |
d2ee52aa S |
865 | /* |
866 | * Looks like: vmbus:guid | |
867 | * Each byte of the guid will be represented by two hex characters | |
868 | * in the name. | |
869 | */ | |
870 | ||
6543becf | 871 | static int do_vmbus_entry(const char *filename, void *symval, |
d2ee52aa S |
872 | char *alias) |
873 | { | |
874 | int i; | |
6543becf AS |
875 | DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid); |
876 | char guid_name[(sizeof(*guid) + 1) * 2]; | |
d2ee52aa | 877 | |
6543becf AS |
878 | for (i = 0; i < (sizeof(*guid) * 2); i += 2) |
879 | sprintf(&guid_name[i], "%02x", TO_NATIVE((*guid)[i/2])); | |
d2ee52aa S |
880 | |
881 | strcpy(alias, "vmbus:"); | |
882 | strcat(alias, guid_name); | |
883 | ||
884 | return 1; | |
885 | } | |
6543becf | 886 | ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id, do_vmbus_entry); |
d2ee52aa | 887 | |
d2653e92 | 888 | /* Looks like: i2c:S */ |
6543becf | 889 | static int do_i2c_entry(const char *filename, void *symval, |
d2653e92 JD |
890 | char *alias) |
891 | { | |
6543becf AS |
892 | DEF_FIELD_ADDR(symval, i2c_device_id, name); |
893 | sprintf(alias, I2C_MODULE_PREFIX "%s", *name); | |
d2653e92 JD |
894 | |
895 | return 1; | |
896 | } | |
6543becf | 897 | ADD_TO_DEVTABLE("i2c", i2c_device_id, do_i2c_entry); |
d2653e92 | 898 | |
e0626e38 | 899 | /* Looks like: spi:S */ |
6543becf | 900 | static int do_spi_entry(const char *filename, void *symval, |
75368bf6 AV |
901 | char *alias) |
902 | { | |
6543becf AS |
903 | DEF_FIELD_ADDR(symval, spi_device_id, name); |
904 | sprintf(alias, SPI_MODULE_PREFIX "%s", *name); | |
75368bf6 AV |
905 | |
906 | return 1; | |
907 | } | |
6543becf | 908 | ADD_TO_DEVTABLE("spi", spi_device_id, do_spi_entry); |
75368bf6 | 909 | |
d945b697 DW |
910 | static const struct dmifield { |
911 | const char *prefix; | |
912 | int field; | |
913 | } dmi_fields[] = { | |
914 | { "bvn", DMI_BIOS_VENDOR }, | |
915 | { "bvr", DMI_BIOS_VERSION }, | |
916 | { "bd", DMI_BIOS_DATE }, | |
917 | { "svn", DMI_SYS_VENDOR }, | |
918 | { "pn", DMI_PRODUCT_NAME }, | |
919 | { "pvr", DMI_PRODUCT_VERSION }, | |
920 | { "rvn", DMI_BOARD_VENDOR }, | |
921 | { "rn", DMI_BOARD_NAME }, | |
922 | { "rvr", DMI_BOARD_VERSION }, | |
923 | { "cvn", DMI_CHASSIS_VENDOR }, | |
924 | { "ct", DMI_CHASSIS_TYPE }, | |
925 | { "cvr", DMI_CHASSIS_VERSION }, | |
926 | { NULL, DMI_NONE } | |
927 | }; | |
928 | ||
929 | static void dmi_ascii_filter(char *d, const char *s) | |
930 | { | |
931 | /* Filter out characters we don't want to see in the modalias string */ | |
932 | for (; *s; s++) | |
933 | if (*s > ' ' && *s < 127 && *s != ':') | |
934 | *(d++) = *s; | |
935 | ||
936 | *d = 0; | |
937 | } | |
938 | ||
939 | ||
6543becf | 940 | static int do_dmi_entry(const char *filename, void *symval, |
d945b697 DW |
941 | char *alias) |
942 | { | |
943 | int i, j; | |
6543becf | 944 | DEF_FIELD_ADDR(symval, dmi_system_id, matches); |
d945b697 DW |
945 | sprintf(alias, "dmi*"); |
946 | ||
947 | for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) { | |
948 | for (j = 0; j < 4; j++) { | |
6543becf AS |
949 | if ((*matches)[j].slot && |
950 | (*matches)[j].slot == dmi_fields[i].field) { | |
d945b697 DW |
951 | sprintf(alias + strlen(alias), ":%s*", |
952 | dmi_fields[i].prefix); | |
953 | dmi_ascii_filter(alias + strlen(alias), | |
6543becf | 954 | (*matches)[j].substr); |
d945b697 DW |
955 | strcat(alias, "*"); |
956 | } | |
957 | } | |
958 | } | |
959 | ||
960 | strcat(alias, ":"); | |
961 | return 1; | |
962 | } | |
6543becf | 963 | ADD_TO_DEVTABLE("dmi", dmi_system_id, do_dmi_entry); |
57fee4a5 EM |
964 | |
965 | static int do_platform_entry(const char *filename, | |
6543becf | 966 | void *symval, char *alias) |
57fee4a5 | 967 | { |
6543becf AS |
968 | DEF_FIELD_ADDR(symval, platform_device_id, name); |
969 | sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name); | |
57fee4a5 EM |
970 | return 1; |
971 | } | |
6543becf | 972 | ADD_TO_DEVTABLE("platform", platform_device_id, do_platform_entry); |
57fee4a5 | 973 | |
8626d3b4 | 974 | static int do_mdio_entry(const char *filename, |
6543becf | 975 | void *symval, char *alias) |
8626d3b4 DW |
976 | { |
977 | int i; | |
6543becf AS |
978 | DEF_FIELD(symval, mdio_device_id, phy_id); |
979 | DEF_FIELD(symval, mdio_device_id, phy_id_mask); | |
8626d3b4 DW |
980 | |
981 | alias += sprintf(alias, MDIO_MODULE_PREFIX); | |
982 | ||
983 | for (i = 0; i < 32; i++) { | |
6543becf | 984 | if (!((phy_id_mask >> (31-i)) & 1)) |
8626d3b4 | 985 | *(alias++) = '?'; |
6543becf | 986 | else if ((phy_id >> (31-i)) & 1) |
8626d3b4 DW |
987 | *(alias++) = '1'; |
988 | else | |
989 | *(alias++) = '0'; | |
990 | } | |
991 | ||
992 | /* Terminate the string */ | |
993 | *alias = 0; | |
994 | ||
995 | return 1; | |
996 | } | |
6543becf | 997 | ADD_TO_DEVTABLE("mdio", mdio_device_id, do_mdio_entry); |
8626d3b4 | 998 | |
bf54a2b3 | 999 | /* Looks like: zorro:iN. */ |
6543becf | 1000 | static int do_zorro_entry(const char *filename, void *symval, |
bf54a2b3 GU |
1001 | char *alias) |
1002 | { | |
6543becf | 1003 | DEF_FIELD(symval, zorro_device_id, id); |
bf54a2b3 | 1004 | strcpy(alias, "zorro:"); |
6543becf | 1005 | ADD(alias, "i", id != ZORRO_WILDCARD, id); |
bf54a2b3 GU |
1006 | return 1; |
1007 | } | |
6543becf | 1008 | ADD_TO_DEVTABLE("zorro", zorro_device_id, do_zorro_entry); |
bf54a2b3 | 1009 | |
fedb3d27 OZ |
1010 | /* looks like: "pnp:dD" */ |
1011 | static int do_isapnp_entry(const char *filename, | |
6543becf | 1012 | void *symval, char *alias) |
fedb3d27 | 1013 | { |
6543becf AS |
1014 | DEF_FIELD(symval, isapnp_device_id, vendor); |
1015 | DEF_FIELD(symval, isapnp_device_id, function); | |
fedb3d27 | 1016 | sprintf(alias, "pnp:d%c%c%c%x%x%x%x*", |
6543becf AS |
1017 | 'A' + ((vendor >> 2) & 0x3f) - 1, |
1018 | 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1, | |
1019 | 'A' + ((vendor >> 8) & 0x1f) - 1, | |
1020 | (function >> 4) & 0x0f, function & 0x0f, | |
1021 | (function >> 12) & 0x0f, (function >> 8) & 0x0f); | |
fedb3d27 OZ |
1022 | return 1; |
1023 | } | |
6543becf | 1024 | ADD_TO_DEVTABLE("isapnp", isapnp_device_id, do_isapnp_entry); |
fedb3d27 | 1025 | |
849e0ad2 JT |
1026 | /* Looks like: "ipack:fNvNdN". */ |
1027 | static int do_ipack_entry(const char *filename, | |
6543becf | 1028 | void *symval, char *alias) |
849e0ad2 | 1029 | { |
6543becf AS |
1030 | DEF_FIELD(symval, ipack_device_id, format); |
1031 | DEF_FIELD(symval, ipack_device_id, vendor); | |
1032 | DEF_FIELD(symval, ipack_device_id, device); | |
849e0ad2 | 1033 | strcpy(alias, "ipack:"); |
6543becf AS |
1034 | ADD(alias, "f", format != IPACK_ANY_FORMAT, format); |
1035 | ADD(alias, "v", vendor != IPACK_ANY_ID, vendor); | |
1036 | ADD(alias, "d", device != IPACK_ANY_ID, device); | |
849e0ad2 JT |
1037 | add_wildcard(alias); |
1038 | return 1; | |
1039 | } | |
6543becf | 1040 | ADD_TO_DEVTABLE("ipack", ipack_device_id, do_ipack_entry); |
849e0ad2 | 1041 | |
523817bd DM |
1042 | /* |
1043 | * Append a match expression for a single masked hex digit. | |
1044 | * outp points to a pointer to the character at which to append. | |
1045 | * *outp is updated on return to point just after the appended text, | |
1046 | * to facilitate further appending. | |
1047 | */ | |
1048 | static void append_nibble_mask(char **outp, | |
1049 | unsigned int nibble, unsigned int mask) | |
1050 | { | |
1051 | char *p = *outp; | |
1052 | unsigned int i; | |
1053 | ||
1054 | switch (mask) { | |
1055 | case 0: | |
1056 | *p++ = '?'; | |
1057 | break; | |
1058 | ||
1059 | case 0xf: | |
1060 | p += sprintf(p, "%X", nibble); | |
1061 | break; | |
1062 | ||
1063 | default: | |
1064 | /* | |
1065 | * Dumbly emit a match pattern for all possible matching | |
1066 | * digits. This could be improved in some cases using ranges, | |
1067 | * but it has the advantage of being trivially correct, and is | |
1068 | * often optimal. | |
1069 | */ | |
1070 | *p++ = '['; | |
1071 | for (i = 0; i < 0x10; i++) | |
1072 | if ((i & mask) == nibble) | |
1073 | p += sprintf(p, "%X", i); | |
1074 | *p++ = ']'; | |
1075 | } | |
1076 | ||
1077 | /* Ensure that the string remains NUL-terminated: */ | |
1078 | *p = '\0'; | |
1079 | ||
1080 | /* Advance the caller's end-of-string pointer: */ | |
1081 | *outp = p; | |
1082 | } | |
1083 | ||
1084 | /* | |
1085 | * looks like: "amba:dN" | |
1086 | * | |
1087 | * N is exactly 8 digits, where each is an upper-case hex digit, or | |
1088 | * a ? or [] pattern matching exactly one digit. | |
1089 | */ | |
1090 | static int do_amba_entry(const char *filename, | |
6543becf | 1091 | void *symval, char *alias) |
523817bd DM |
1092 | { |
1093 | unsigned int digit; | |
1094 | char *p = alias; | |
6543becf AS |
1095 | DEF_FIELD(symval, amba_id, id); |
1096 | DEF_FIELD(symval, amba_id, mask); | |
523817bd | 1097 | |
6543becf | 1098 | if ((id & mask) != id) |
523817bd DM |
1099 | fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: " |
1100 | "id=0x%08X, mask=0x%08X. Please fix this driver.\n", | |
6543becf | 1101 | filename, id, mask); |
523817bd DM |
1102 | |
1103 | p += sprintf(alias, "amba:d"); | |
1104 | for (digit = 0; digit < 8; digit++) | |
1105 | append_nibble_mask(&p, | |
6543becf AS |
1106 | (id >> (4 * (7 - digit))) & 0xf, |
1107 | (mask >> (4 * (7 - digit))) & 0xf); | |
523817bd DM |
1108 | |
1109 | return 1; | |
1110 | } | |
6543becf | 1111 | ADD_TO_DEVTABLE("amba", amba_id, do_amba_entry); |
523817bd | 1112 | |
644e9cbb AK |
1113 | /* LOOKS like x86cpu:vendor:VVVV:family:FFFF:model:MMMM:feature:*,FEAT,* |
1114 | * All fields are numbers. It would be nicer to use strings for vendor | |
1115 | * and feature, but getting those out of the build system here is too | |
1116 | * complicated. | |
1117 | */ | |
1118 | ||
6543becf | 1119 | static int do_x86cpu_entry(const char *filename, void *symval, |
644e9cbb AK |
1120 | char *alias) |
1121 | { | |
6543becf AS |
1122 | DEF_FIELD(symval, x86_cpu_id, feature); |
1123 | DEF_FIELD(symval, x86_cpu_id, family); | |
1124 | DEF_FIELD(symval, x86_cpu_id, model); | |
1125 | DEF_FIELD(symval, x86_cpu_id, vendor); | |
644e9cbb AK |
1126 | |
1127 | strcpy(alias, "x86cpu:"); | |
6543becf AS |
1128 | ADD(alias, "vendor:", vendor != X86_VENDOR_ANY, vendor); |
1129 | ADD(alias, ":family:", family != X86_FAMILY_ANY, family); | |
1130 | ADD(alias, ":model:", model != X86_MODEL_ANY, model); | |
5467bdda | 1131 | strcat(alias, ":feature:*"); |
6543becf AS |
1132 | if (feature != X86_FEATURE_ANY) |
1133 | sprintf(alias + strlen(alias), "%04X*", feature); | |
644e9cbb AK |
1134 | return 1; |
1135 | } | |
6543becf | 1136 | ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry); |
644e9cbb | 1137 | |
e5354107 SO |
1138 | /* Looks like: mei:S */ |
1139 | static int do_mei_entry(const char *filename, void *symval, | |
1140 | char *alias) | |
1141 | { | |
1142 | DEF_FIELD_ADDR(symval, mei_cl_device_id, name); | |
1143 | ||
1144 | sprintf(alias, MEI_CL_MODULE_PREFIX "%s", *name); | |
1145 | ||
1146 | return 1; | |
1147 | } | |
1148 | ADD_TO_DEVTABLE("mei", mei_cl_device_id, do_mei_entry); | |
1149 | ||
3bdbb62f AB |
1150 | /* Looks like: rapidio:vNdNavNadN */ |
1151 | static int do_rio_entry(const char *filename, | |
1152 | void *symval, char *alias) | |
1153 | { | |
1154 | DEF_FIELD(symval, rio_device_id, did); | |
1155 | DEF_FIELD(symval, rio_device_id, vid); | |
1156 | DEF_FIELD(symval, rio_device_id, asm_did); | |
1157 | DEF_FIELD(symval, rio_device_id, asm_vid); | |
1158 | ||
1159 | strcpy(alias, "rapidio:"); | |
1160 | ADD(alias, "v", vid != RIO_ANY_ID, vid); | |
1161 | ADD(alias, "d", did != RIO_ANY_ID, did); | |
1162 | ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid); | |
1163 | ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did); | |
1164 | ||
1165 | add_wildcard(alias); | |
1166 | return 1; | |
1167 | } | |
1168 | ADD_TO_DEVTABLE("rapidio", rio_device_id, do_rio_entry); | |
1169 | ||
626596e2 RR |
1170 | /* Does namelen bytes of name exactly match the symbol? */ |
1171 | static bool sym_is(const char *name, unsigned namelen, const char *symbol) | |
1da177e4 | 1172 | { |
626596e2 RR |
1173 | if (namelen != strlen(symbol)) |
1174 | return false; | |
1da177e4 | 1175 | |
626596e2 | 1176 | return memcmp(name, symbol, namelen) == 0; |
1da177e4 LT |
1177 | } |
1178 | ||
1179 | static void do_table(void *symval, unsigned long size, | |
1180 | unsigned long id_size, | |
fb33d816 | 1181 | const char *device_id, |
1da177e4 LT |
1182 | void *function, |
1183 | struct module *mod) | |
1184 | { | |
1185 | unsigned int i; | |
1186 | char alias[500]; | |
1187 | int (*do_entry)(const char *, void *entry, char *alias) = function; | |
1188 | ||
e0049825 | 1189 | device_id_check(mod->name, device_id, size, id_size, symval); |
1da177e4 LT |
1190 | /* Leave last one: it's the terminator. */ |
1191 | size -= id_size; | |
1192 | ||
1193 | for (i = 0; i < size; i += id_size) { | |
1194 | if (do_entry(mod->name, symval+i, alias)) { | |
1da177e4 LT |
1195 | buf_printf(&mod->dev_table_buf, |
1196 | "MODULE_ALIAS(\"%s\");\n", alias); | |
1197 | } | |
1198 | } | |
1199 | } | |
1200 | ||
1201 | /* Create MODULE_ALIAS() statements. | |
1202 | * At this time, we cannot write the actual output C source yet, | |
1203 | * so we write into the mod->dev_table_buf buffer. */ | |
1204 | void handle_moddevtable(struct module *mod, struct elf_info *info, | |
1205 | Elf_Sym *sym, const char *symname) | |
1206 | { | |
1207 | void *symval; | |
e0049825 | 1208 | char *zeros = NULL; |
626596e2 RR |
1209 | const char *name; |
1210 | unsigned int namelen; | |
1da177e4 LT |
1211 | |
1212 | /* We're looking for a section relative symbol */ | |
1ce53adf | 1213 | if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections) |
1da177e4 LT |
1214 | return; |
1215 | ||
e88aa7bb DM |
1216 | /* We're looking for an object */ |
1217 | if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT) | |
1218 | return; | |
1219 | ||
626596e2 RR |
1220 | /* All our symbols are of form <prefix>__mod_XXX_device_table. */ |
1221 | name = strstr(symname, "__mod_"); | |
1222 | if (!name) | |
1223 | return; | |
1224 | name += strlen("__mod_"); | |
1225 | namelen = strlen(name); | |
1226 | if (namelen < strlen("_device_table")) | |
1227 | return; | |
1228 | if (strcmp(name + namelen - strlen("_device_table"), "_device_table")) | |
1229 | return; | |
1230 | namelen -= strlen("_device_table"); | |
1231 | ||
e0049825 | 1232 | /* Handle all-NULL symbols allocated into .bss */ |
1ce53adf | 1233 | if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) { |
e0049825 KC |
1234 | zeros = calloc(1, sym->st_size); |
1235 | symval = zeros; | |
1236 | } else { | |
1237 | symval = (void *)info->hdr | |
1ce53adf | 1238 | + info->sechdrs[get_secindex(info, sym)].sh_offset |
e0049825 KC |
1239 | + sym->st_value; |
1240 | } | |
1da177e4 | 1241 | |
626596e2 RR |
1242 | /* First handle the "special" cases */ |
1243 | if (sym_is(name, namelen, "usb")) | |
b19dcd93 | 1244 | do_usb_table(symval, sym->st_size, mod); |
626596e2 | 1245 | else if (sym_is(name, namelen, "pnp")) |
22454cb9 | 1246 | do_pnp_device_entry(symval, sym->st_size, mod); |
626596e2 | 1247 | else if (sym_is(name, namelen, "pnp_card")) |
0c81eed4 | 1248 | do_pnp_card_entries(symval, sym->st_size, mod); |
626596e2 | 1249 | else { |
e49ce141 | 1250 | struct devtable **p; |
dd2a3aca | 1251 | INIT_SECTION(__devtable); |
626596e2 | 1252 | |
e49ce141 RR |
1253 | for (p = __start___devtable; p < __stop___devtable; p++) { |
1254 | if (sym_is(name, namelen, (*p)->device_id)) { | |
1255 | do_table(symval, sym->st_size, (*p)->id_size, | |
1256 | (*p)->device_id, (*p)->function, mod); | |
626596e2 RR |
1257 | break; |
1258 | } | |
1259 | } | |
1260 | } | |
e0049825 | 1261 | free(zeros); |
1da177e4 LT |
1262 | } |
1263 | ||
1264 | /* Now add out buffered information to the generated C source */ | |
1265 | void add_moddevtable(struct buffer *buf, struct module *mod) | |
1266 | { | |
1267 | buf_printf(buf, "\n"); | |
1268 | buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos); | |
1269 | free(mod->dev_table_buf.p); | |
1270 | } |