blob: 771ac17f635d5be5e3dc3a515b1a24288060be8d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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"
Andreas Schwab6543bec2013-01-20 17:58:47 +010014#include "devicetable-offsets.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
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
19typedef Elf32_Addr kernel_ulong_t;
Rusty Russell1d8f4302005-12-07 21:40:34 +010020#define BITS_PER_LONG 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#else
22typedef Elf64_Addr kernel_ulong_t;
Rusty Russell1d8f4302005-12-07 21:40:34 +010023#define BITS_PER_LONG 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#endif
25#ifdef __sun__
26#include <inttypes.h>
27#else
28#include <stdint.h>
29#endif
30
Jeff Mahoney5e655772005-07-06 15:44:41 -040031#include <ctype.h>
Rusty Russell626596e2012-01-13 09:32:15 +103032#include <stdbool.h>
Jeff Mahoney5e655772005-07-06 15:44:41 -040033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034typedef uint32_t __u32;
35typedef uint16_t __u16;
36typedef unsigned char __u8;
37
38/* Big exception to the "don't include kernel headers into userspace, which
Sam Ravnborg62070fa2006-03-03 16:46:04 +010039 * even potentially has different endianness and word sizes, since
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * we handle those differences explicitly below */
41#include "../../include/linux/mod_devicetable.h"
42
Rusty Russelle49ce142012-01-13 09:32:16 +103043/* This array collects all instances that use the generic do_table */
44struct devtable {
45 const char *device_id; /* name of table, __mod_<name>_device_table. */
46 unsigned long id_size;
47 void *function;
48};
49
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010050#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
72struct devtable **__start___devtable, **__stop___devtable;
73#else
74#define INIT_SECTION(name) /* no-op for ELF */
75#define SECTION(name) __attribute__((section(#name)))
76
Rusty Russelle49ce142012-01-13 09:32:16 +103077/* We construct a table of pointers in an ELF section (pointers generally
78 * go unpadded by gcc). ld creates boundary syms for us. */
79extern struct devtable *__start___devtable[], *__stop___devtable[];
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010080#endif /* __MACH__ */
Rusty Russelle49ce142012-01-13 09:32:16 +103081
82#if __GNUC__ == 3 && __GNUC_MINOR__ < 3
83# define __used __attribute__((__unused__))
84#else
85# define __used __attribute__((__used__))
86#endif
87
Andreas Schwab6543bec2013-01-20 17:58:47 +010088/* Define a variable f that holds the value of field f of struct devid
89 * based at address m.
90 */
91#define DEF_FIELD(m, devid, f) \
92 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
93/* Define a variable f that holds the address of field f of struct devid
94 * based at address m. Due to the way typeof works, for a field of type
95 * T[N] the variable has type T(*)[N], _not_ T*.
96 */
97#define DEF_FIELD_ADDR(m, devid, f) \
98 typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
99
Rusty Russelle49ce142012-01-13 09:32:16 +1030100/* Add a table entry. We test function type matches while we're here. */
101#define ADD_TO_DEVTABLE(device_id, type, function) \
102 static struct devtable __cat(devtable,__LINE__) = { \
103 device_id + 0*sizeof((function)((const char *)NULL, \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100104 (void *)NULL, \
Rusty Russelle49ce142012-01-13 09:32:16 +1030105 (char *)NULL)), \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100106 SIZE_##type, (function) }; \
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +0100107 static struct devtable *SECTION(__devtable) __used \
108 __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
Rusty Russelle49ce142012-01-13 09:32:16 +1030109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#define ADD(str, sep, cond, field) \
111do { \
112 strcat(str, sep); \
113 if (cond) \
114 sprintf(str + strlen(str), \
115 sizeof(field) == 1 ? "%02X" : \
116 sizeof(field) == 2 ? "%04X" : \
117 sizeof(field) == 4 ? "%08X" : "", \
118 field); \
119 else \
120 sprintf(str + strlen(str), "*"); \
121} while(0)
122
Jean Delvareac551822008-05-02 20:37:21 +0200123/* Always end in a wildcard, for future extension */
124static inline void add_wildcard(char *str)
125{
126 int len = strlen(str);
127
128 if (str[len - 1] != '*')
129 strcat(str + len, "*");
130}
131
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200132/**
133 * Check that sizeof(device_id type) are consistent with size of section
134 * in .o file. If in-consistent then userspace and kernel does not agree
135 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200136 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100137 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200138 **/
Kees Cooke0049822007-09-16 11:15:46 +0200139static void device_id_check(const char *modname, const char *device_id,
140 unsigned long size, unsigned long id_size,
141 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200142{
Kees Cooke0049822007-09-16 11:15:46 +0200143 int i;
144
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200145 if (size % id_size || size < id_size) {
146 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
147 "of the size of section __mod_%s_device_table=%lu.\n"
148 "Fix definition of struct %s_device_id "
149 "in mod_devicetable.h\n",
150 modname, device_id, id_size, device_id, size, device_id);
151 }
Kees Cooke0049822007-09-16 11:15:46 +0200152 /* Verify last one is a terminator */
153 for (i = 0; i < id_size; i++ ) {
154 if (*(uint8_t*)(symval+size-id_size+i)) {
155 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
156 "The last of %lu is:\n",
157 modname, device_id, id_size, size / id_size);
158 for (i = 0; i < id_size; i++ )
159 fprintf(stderr,"0x%02x ",
160 *(uint8_t*)(symval+size-id_size+i) );
161 fprintf(stderr,"\n");
162 fatal("%s: struct %s_device_id is not terminated "
163 "with a NULL entry!\n", modname, device_id);
164 }
165 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200166}
167
Roman Kaganb19dcd92005-04-22 15:07:01 -0700168/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200169/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100170static void do_usb_entry(void *symval,
Roman Kaganb19dcd92005-04-22 15:07:01 -0700171 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
172 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500173 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700175 char alias[500];
Andreas Schwab6543bec2013-01-20 17:58:47 +0100176 DEF_FIELD(symval, usb_device_id, match_flags);
177 DEF_FIELD(symval, usb_device_id, idVendor);
178 DEF_FIELD(symval, usb_device_id, idProduct);
179 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
180 DEF_FIELD(symval, usb_device_id, bDeviceClass);
181 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
182 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
183 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
184 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
185 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
186 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 strcpy(alias, "usb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100189 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
190 idVendor);
191 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
192 idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700193
194 strcat(alias, "d");
195 if (bcdDevice_initial_digits)
196 sprintf(alias + strlen(alias), "%0*X",
197 bcdDevice_initial_digits, bcdDevice_initial);
198 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500199 sprintf(alias + strlen(alias), "%X", range_lo);
200 else if (range_lo > 0 || range_hi < max) {
201 if (range_lo > 0x9 || range_hi < 0xA)
202 sprintf(alias + strlen(alias),
203 "[%X-%X]",
204 range_lo,
205 range_hi);
206 else {
207 sprintf(alias + strlen(alias),
208 range_lo < 0x9 ? "[%X-9" : "[%X",
209 range_lo);
210 sprintf(alias + strlen(alias),
211 range_hi > 0xA ? "a-%X]" : "%X]",
212 range_lo);
213 }
214 }
Andreas Schwab6543bec2013-01-20 17:58:47 +0100215 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700216 strcat(alias, "*");
217
Andreas Schwab6543bec2013-01-20 17:58:47 +0100218 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
219 bDeviceClass);
220 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
221 bDeviceSubClass);
222 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
223 bDeviceProtocol);
224 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
225 bInterfaceClass);
226 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
227 bInterfaceSubClass);
228 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
229 bInterfaceProtocol);
230 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
231 bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700232
Jean Delvareac551822008-05-02 20:37:21 +0200233 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700234 buf_printf(&mod->dev_table_buf,
235 "MODULE_ALIAS(\"%s\");\n", alias);
236}
237
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500238/* Handles increment/decrement of BCD formatted integers */
239/* Returns the previous value, so it works like i++ or i-- */
240static unsigned int incbcd(unsigned int *bcd,
241 int inc,
242 unsigned char max,
243 size_t chars)
244{
245 unsigned int init = *bcd, i, j;
246 unsigned long long c, dec = 0;
247
248 /* If bcd is not in BCD format, just increment */
249 if (max > 0x9) {
250 *bcd += inc;
251 return init;
252 }
253
254 /* Convert BCD to Decimal */
255 for (i=0 ; i < chars ; i++) {
256 c = (*bcd >> (i << 2)) & 0xf;
257 c = c > 9 ? 9 : c; /* force to bcd just in case */
258 for (j=0 ; j < i ; j++)
259 c = c * 10;
260 dec += c;
261 }
262
263 /* Do our increment/decrement */
264 dec += inc;
265 *bcd = 0;
266
267 /* Convert back to BCD */
268 for (i=0 ; i < chars ; i++) {
269 for (c=1,j=0 ; j < i ; j++)
270 c = c * 10;
271 c = (dec / c) % 10;
272 *bcd += c << (i << 2);
273 }
274 return init;
275}
276
Andreas Schwab6543bec2013-01-20 17:58:47 +0100277static void do_usb_entry_multi(void *symval, struct module *mod)
Roman Kaganb19dcd92005-04-22 15:07:01 -0700278{
279 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500280 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700281 int ndigits;
282
Andreas Schwab6543bec2013-01-20 17:58:47 +0100283 DEF_FIELD(symval, usb_device_id, match_flags);
284 DEF_FIELD(symval, usb_device_id, idVendor);
285 DEF_FIELD(symval, usb_device_id, idProduct);
286 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
287 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
288 DEF_FIELD(symval, usb_device_id, bDeviceClass);
289 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700290
Andreas Schwab6543bec2013-01-20 17:58:47 +0100291 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
292 bcdDevice_lo : 0x0U;
293 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
294 bcdDevice_hi : ~0x0U;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700295
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500296 /* Figure out if this entry is in bcd or hex format */
297 max = 0x9; /* Default to decimal format */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100298 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500299 clo = (devlo >> (ndigits << 2)) & 0xf;
300 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
301 if (clo > max || chi > max) {
302 max = 0xf;
303 break;
304 }
305 }
306
Roman Kaganb19dcd92005-04-22 15:07:01 -0700307 /*
308 * Some modules (visor) have empty slots as placeholder for
309 * run-time specification that results in catch-all alias
310 */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100311 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700312 return;
313
314 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100315 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
Roman Kaganb19dcd92005-04-22 15:07:01 -0700316 clo = devlo & 0xf;
317 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500318 if (chi > max) /* If we are in bcd mode, truncate if necessary */
319 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700320 devlo >>= 4;
321 devhi >>= 4;
322
323 if (devlo == devhi || !ndigits) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100324 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700325 break;
326 }
327
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500328 if (clo > 0x0)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100329 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500330 incbcd(&devlo, 1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100331 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500332 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700333
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500334 if (chi < max)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100335 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500336 incbcd(&devhi, -1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100337 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500338 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700339 }
340}
341
342static void do_usb_table(void *symval, unsigned long size,
343 struct module *mod)
344{
345 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100346 const unsigned long id_size = SIZE_usb_device_id;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700347
Kees Cooke0049822007-09-16 11:15:46 +0200348 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200349
Roman Kaganb19dcd92005-04-22 15:07:01 -0700350 /* Leave last one: it's the terminator. */
351 size -= id_size;
352
353 for (i = 0; i < size; i += id_size)
354 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Jiri Slabye8c84f92008-05-19 15:50:01 +0200357/* Looks like: hid:bNvNpN */
358static int do_hid_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100359 void *symval, char *alias)
Jiri Slabye8c84f92008-05-19 15:50:01 +0200360{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100361 DEF_FIELD(symval, hid_device_id, bus);
362 DEF_FIELD(symval, hid_device_id, group);
363 DEF_FIELD(symval, hid_device_id, vendor);
364 DEF_FIELD(symval, hid_device_id, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200365
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200366 sprintf(alias, "hid:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100367 ADD(alias, "b", bus != HID_BUS_ANY, bus);
368 ADD(alias, "g", group != HID_GROUP_ANY, group);
369 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
370 ADD(alias, "p", product != HID_ANY_ID, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200371
372 return 1;
373}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100374ADD_TO_DEVTABLE("hid", hid_device_id, do_hid_entry);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376/* Looks like: ieee1394:venNmoNspNverN */
377static int do_ieee1394_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100378 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100380 DEF_FIELD(symval, ieee1394_device_id, match_flags);
381 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
382 DEF_FIELD(symval, ieee1394_device_id, model_id);
383 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
384 DEF_FIELD(symval, ieee1394_device_id, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 strcpy(alias, "ieee1394:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100387 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
388 vendor_id);
389 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
390 model_id);
391 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
392 specifier_id);
393 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
394 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Jean Delvareac551822008-05-02 20:37:21 +0200396 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return 1;
398}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100399ADD_TO_DEVTABLE("ieee1394", ieee1394_device_id, do_ieee1394_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
402static int do_pci_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100403 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 /* Class field can be divided into these three. */
406 unsigned char baseclass, subclass, interface,
407 baseclass_mask, subclass_mask, interface_mask;
408
Andreas Schwab6543bec2013-01-20 17:58:47 +0100409 DEF_FIELD(symval, pci_device_id, vendor);
410 DEF_FIELD(symval, pci_device_id, device);
411 DEF_FIELD(symval, pci_device_id, subvendor);
412 DEF_FIELD(symval, pci_device_id, subdevice);
413 DEF_FIELD(symval, pci_device_id, class);
414 DEF_FIELD(symval, pci_device_id, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 strcpy(alias, "pci:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100417 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
418 ADD(alias, "d", device != PCI_ANY_ID, device);
419 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
420 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Andreas Schwab6543bec2013-01-20 17:58:47 +0100422 baseclass = (class) >> 16;
423 baseclass_mask = (class_mask) >> 16;
424 subclass = (class) >> 8;
425 subclass_mask = (class_mask) >> 8;
426 interface = class;
427 interface_mask = class_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
430 || (subclass_mask != 0 && subclass_mask != 0xFF)
431 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100432 warn("Can't handle masks in %s:%04X\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100433 filename, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return 0;
435 }
436
437 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
438 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
439 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200440 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return 1;
442}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100443ADD_TO_DEVTABLE("pci", pci_device_id, do_pci_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100445/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446static int do_ccw_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100447 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100449 DEF_FIELD(symval, ccw_device_id, match_flags);
450 DEF_FIELD(symval, ccw_device_id, cu_type);
451 DEF_FIELD(symval, ccw_device_id, cu_model);
452 DEF_FIELD(symval, ccw_device_id, dev_type);
453 DEF_FIELD(symval, ccw_device_id, dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 strcpy(alias, "ccw:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100456 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
457 cu_type);
458 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
459 cu_model);
460 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
461 dev_type);
462 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
463 dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200464 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return 1;
466}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100467ADD_TO_DEVTABLE("ccw", ccw_device_id, do_ccw_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200469/* looks like: "ap:tN" */
470static int do_ap_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100471 void *symval, char *alias)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200472{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100473 DEF_FIELD(symval, ap_device_id, dev_type);
474
475 sprintf(alias, "ap:t%02X*", dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200476 return 1;
477}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100478ADD_TO_DEVTABLE("ap", ap_device_id, do_ap_entry);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200479
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200480/* looks like: "css:tN" */
481static int do_css_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100482 void *symval, char *alias)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200483{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100484 DEF_FIELD(symval, css_device_id, type);
485
486 sprintf(alias, "css:t%01X", type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200487 return 1;
488}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100489ADD_TO_DEVTABLE("css", css_device_id, do_css_entry);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491/* Looks like: "serio:tyNprNidNexN" */
492static int do_serio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100493 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100495 DEF_FIELD(symval, serio_device_id, type);
496 DEF_FIELD(symval, serio_device_id, proto);
497 DEF_FIELD(symval, serio_device_id, id);
498 DEF_FIELD(symval, serio_device_id, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 strcpy(alias, "serio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100501 ADD(alias, "ty", type != SERIO_ANY, type);
502 ADD(alias, "pr", proto != SERIO_ANY, proto);
503 ADD(alias, "id", id != SERIO_ANY, id);
504 ADD(alias, "ex", extra != SERIO_ANY, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Jean Delvareac551822008-05-02 20:37:21 +0200506 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return 1;
508}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100509ADD_TO_DEVTABLE("serio", serio_device_id, do_serio_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Thomas Renninger29b71a12007-07-23 14:43:51 +0200511/* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
512static int do_acpi_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100513 void *symval, char *alias)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200514{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100515 DEF_FIELD_ADDR(symval, acpi_device_id, id);
516 sprintf(alias, "acpi*:%s:*", *id);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200517 return 1;
518}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100519ADD_TO_DEVTABLE("acpi", acpi_device_id, do_acpi_entry);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200522static void do_pnp_device_entry(void *symval, unsigned long size,
523 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100525 const unsigned long id_size = SIZE_pnp_device_id;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200526 const unsigned int count = (size / id_size)-1;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200527 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200528
529 device_id_check(mod->name, "pnp", size, id_size, symval);
530
Kay Sievers5e4c6562008-08-21 15:28:56 +0200531 for (i = 0; i < count; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100532 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
533 char acpi_id[sizeof(*id)];
Kay Sievers72638f52009-01-08 03:06:42 +0100534 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200535
536 buf_printf(&mod->dev_table_buf,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100537 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
Kay Sievers72638f52009-01-08 03:06:42 +0100538
539 /* fix broken pnp bus lowercasing */
540 for (j = 0; j < sizeof(acpi_id); j++)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100541 acpi_id[j] = toupper((*id)[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200542 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100543 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Kay Sievers0c81eed2008-02-21 00:35:54 +0100547/* looks like: "pnp:dD" for every device of the card */
548static void do_pnp_card_entries(void *symval, unsigned long size,
549 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100551 const unsigned long id_size = SIZE_pnp_card_device_id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100552 const unsigned int count = (size / id_size)-1;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100553 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Kay Sievers0c81eed2008-02-21 00:35:54 +0100555 device_id_check(mod->name, "pnp", size, id_size, symval);
556
557 for (i = 0; i < count; i++) {
558 unsigned int j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100559 DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100560
561 for (j = 0; j < PNP_MAX_DEVICES; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100562 const char *id = (char *)(*devs)[j].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100563 int i2, j2;
564 int dup = 0;
565
566 if (!id[0])
567 break;
568
569 /* find duplicate, already added value */
570 for (i2 = 0; i2 < i && !dup; i2++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100571 DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100572
573 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100574 const char *id2 = (char *)(*devs)[j2].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100575
576 if (!id2[0])
577 break;
578
579 if (!strcmp(id, id2)) {
580 dup = 1;
581 break;
582 }
583 }
584 }
585
586 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200587 if (!dup) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100588 char acpi_id[PNP_ID_LEN];
Kay Sievers72638f52009-01-08 03:06:42 +0100589 int k;
590
Kay Sievers0c81eed2008-02-21 00:35:54 +0100591 buf_printf(&mod->dev_table_buf,
592 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100593
594 /* fix broken pnp bus lowercasing */
595 for (k = 0; k < sizeof(acpi_id); k++)
596 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200597 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100598 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200599 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700604/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
605static int do_pcmcia_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100606 void *symval, char *alias)
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700607{
608 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100609 DEF_FIELD(symval, pcmcia_device_id, match_flags);
610 DEF_FIELD(symval, pcmcia_device_id, manf_id);
611 DEF_FIELD(symval, pcmcia_device_id, card_id);
612 DEF_FIELD(symval, pcmcia_device_id, func_id);
613 DEF_FIELD(symval, pcmcia_device_id, function);
614 DEF_FIELD(symval, pcmcia_device_id, device_no);
615 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200616
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700617 for (i=0; i<4; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100618 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
619 }
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700620
Andreas Schwab6543bec2013-01-20 17:58:47 +0100621 strcpy(alias, "pcmcia:");
622 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
623 manf_id);
624 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
625 card_id);
626 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
627 func_id);
628 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
629 function);
630 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
631 device_no);
632 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
633 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
634 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
635 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700636
Jean Delvareac551822008-05-02 20:37:21 +0200637 add_wildcard(alias);
Andreas Schwab6543bec2013-01-20 17:58:47 +0100638 return 1;
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700639}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100640ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id, do_pcmcia_entry);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700641
Andreas Schwab6543bec2013-01-20 17:58:47 +0100642static int do_of_entry (const char *filename, void *symval, char *alias)
Jeff Mahoney5e655772005-07-06 15:44:41 -0400643{
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000644 int len;
Jeff Mahoney5e655772005-07-06 15:44:41 -0400645 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100646 DEF_FIELD_ADDR(symval, of_device_id, name);
647 DEF_FIELD_ADDR(symval, of_device_id, type);
648 DEF_FIELD_ADDR(symval, of_device_id, compatible);
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000649
Andreas Schwab6543bec2013-01-20 17:58:47 +0100650 len = sprintf (alias, "of:N%sT%s",
651 (*name)[0] ? *name : "*",
652 (*type)[0] ? *type : "*");
653
654 if (compatible[0])
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000655 sprintf (&alias[len], "%sC%s",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100656 (*type)[0] ? "*" : "",
657 *compatible);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400658
659 /* Replace all whitespace with underscores */
660 for (tmp = alias; tmp && *tmp; tmp++)
661 if (isspace (*tmp))
662 *tmp = '_';
663
Jean Delvareac551822008-05-02 20:37:21 +0200664 add_wildcard(alias);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400665 return 1;
666}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100667ADD_TO_DEVTABLE("of", of_device_id, do_of_entry);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400668
Andreas Schwab6543bec2013-01-20 17:58:47 +0100669static int do_vio_entry(const char *filename, void *symval,
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000670 char *alias)
671{
672 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100673 DEF_FIELD_ADDR(symval, vio_device_id, type);
674 DEF_FIELD_ADDR(symval, vio_device_id, compat);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000675
Andreas Schwab6543bec2013-01-20 17:58:47 +0100676 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
677 (*compat)[0] ? *compat : "*");
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000678
679 /* Replace all whitespace with underscores */
680 for (tmp = alias; tmp && *tmp; tmp++)
681 if (isspace (*tmp))
682 *tmp = '_';
683
Jean Delvareac551822008-05-02 20:37:21 +0200684 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000685 return 1;
686}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100687ADD_TO_DEVTABLE("vio", vio_device_id, do_vio_entry);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000688
Rusty Russell1d8f4302005-12-07 21:40:34 +0100689#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
690
691static void do_input(char *alias,
692 kernel_ulong_t *arr, unsigned int min, unsigned int max)
693{
694 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400695
Andreas Schwab6543bec2013-01-20 17:58:47 +0100696 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
697 arr[i] = TO_NATIVE(arr[i]);
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400698 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200699 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400700 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100701}
702
703/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100704static int do_input_entry(const char *filename, void *symval,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100705 char *alias)
706{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100707 DEF_FIELD(symval, input_device_id, flags);
708 DEF_FIELD(symval, input_device_id, bustype);
709 DEF_FIELD(symval, input_device_id, vendor);
710 DEF_FIELD(symval, input_device_id, product);
711 DEF_FIELD(symval, input_device_id, version);
712 DEF_FIELD_ADDR(symval, input_device_id, evbit);
713 DEF_FIELD_ADDR(symval, input_device_id, keybit);
714 DEF_FIELD_ADDR(symval, input_device_id, relbit);
715 DEF_FIELD_ADDR(symval, input_device_id, absbit);
716 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
717 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
718 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
719 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
720 DEF_FIELD_ADDR(symval, input_device_id, swbit);
721
Rusty Russell1d8f4302005-12-07 21:40:34 +0100722 sprintf(alias, "input:");
723
Andreas Schwab6543bec2013-01-20 17:58:47 +0100724 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
725 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
726 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
727 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100728
729 sprintf(alias + strlen(alias), "-e*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100730 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
731 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100732 sprintf(alias + strlen(alias), "k*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100733 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
734 do_input(alias, *keybit,
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100735 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
736 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100737 sprintf(alias + strlen(alias), "r*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100738 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
739 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100740 sprintf(alias + strlen(alias), "a*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100741 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
742 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100743 sprintf(alias + strlen(alias), "m*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100744 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
745 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100746 sprintf(alias + strlen(alias), "l*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100747 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
748 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100749 sprintf(alias + strlen(alias), "s*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100750 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
751 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100752 sprintf(alias + strlen(alias), "f*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100753 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
754 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100755 sprintf(alias + strlen(alias), "w*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100756 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
757 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100758 return 1;
759}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100760ADD_TO_DEVTABLE("input", input_device_id, do_input_entry);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100761
Andreas Schwab6543bec2013-01-20 17:58:47 +0100762static int do_eisa_entry(const char *filename, void *symval,
Michael Tokarev07563c72006-09-27 01:50:56 -0700763 char *alias)
764{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100765 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
766 if (sig[0])
767 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
Jean Delvareac551822008-05-02 20:37:21 +0200768 else
769 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700770 return 1;
771}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100772ADD_TO_DEVTABLE("eisa", eisa_device_id, do_eisa_entry);
Michael Tokarev07563c72006-09-27 01:50:56 -0700773
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500774/* Looks like: parisc:tNhvNrevNsvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100775static int do_parisc_entry(const char *filename, void *symval,
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500776 char *alias)
777{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100778 DEF_FIELD(symval, parisc_device_id, hw_type);
779 DEF_FIELD(symval, parisc_device_id, hversion);
780 DEF_FIELD(symval, parisc_device_id, hversion_rev);
781 DEF_FIELD(symval, parisc_device_id, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500782
783 strcpy(alias, "parisc:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100784 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
785 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
786 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
787 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500788
Jean Delvareac551822008-05-02 20:37:21 +0200789 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500790 return 1;
791}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100792ADD_TO_DEVTABLE("parisc", parisc_device_id, do_parisc_entry);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500793
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200794/* Looks like: sdio:cNvNdN. */
795static int do_sdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100796 void *symval, char *alias)
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200797{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100798 DEF_FIELD(symval, sdio_device_id, class);
799 DEF_FIELD(symval, sdio_device_id, vendor);
800 DEF_FIELD(symval, sdio_device_id, device);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200801
802 strcpy(alias, "sdio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100803 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
804 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
805 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
Jean Delvareac551822008-05-02 20:37:21 +0200806 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700807 return 1;
808}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100809ADD_TO_DEVTABLE("sdio", sdio_device_id, do_sdio_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200810
Michael Buesch61e115a2007-09-18 15:12:50 -0400811/* Looks like: ssb:vNidNrevN. */
812static int do_ssb_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100813 void *symval, char *alias)
Michael Buesch61e115a2007-09-18 15:12:50 -0400814{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100815 DEF_FIELD(symval, ssb_device_id, vendor);
816 DEF_FIELD(symval, ssb_device_id, coreid);
817 DEF_FIELD(symval, ssb_device_id, revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400818
819 strcpy(alias, "ssb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100820 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
821 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
822 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
Jean Delvareac551822008-05-02 20:37:21 +0200823 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200824 return 1;
825}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100826ADD_TO_DEVTABLE("ssb", ssb_device_id, do_ssb_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200827
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200828/* Looks like: bcma:mNidNrevNclN. */
829static int do_bcma_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100830 void *symval, char *alias)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200831{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100832 DEF_FIELD(symval, bcma_device_id, manuf);
833 DEF_FIELD(symval, bcma_device_id, id);
834 DEF_FIELD(symval, bcma_device_id, rev);
835 DEF_FIELD(symval, bcma_device_id, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200836
837 strcpy(alias, "bcma:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100838 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
839 ADD(alias, "id", id != BCMA_ANY_ID, id);
840 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
841 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200842 add_wildcard(alias);
843 return 1;
844}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100845ADD_TO_DEVTABLE("bcma", bcma_device_id, do_bcma_entry);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200846
Rusty Russellb01d9f22007-10-22 11:03:39 +1000847/* Looks like: virtio:dNvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100848static int do_virtio_entry(const char *filename, void *symval,
Rusty Russellb01d9f22007-10-22 11:03:39 +1000849 char *alias)
850{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100851 DEF_FIELD(symval, virtio_device_id, device);
852 DEF_FIELD(symval, virtio_device_id, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000853
854 strcpy(alias, "virtio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100855 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
856 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000857
Jean Delvareac551822008-05-02 20:37:21 +0200858 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000859 return 1;
860}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100861ADD_TO_DEVTABLE("virtio", virtio_device_id, do_virtio_entry);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000862
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700863/*
864 * Looks like: vmbus:guid
865 * Each byte of the guid will be represented by two hex characters
866 * in the name.
867 */
868
Andreas Schwab6543bec2013-01-20 17:58:47 +0100869static int do_vmbus_entry(const char *filename, void *symval,
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700870 char *alias)
871{
872 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100873 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
874 char guid_name[(sizeof(*guid) + 1) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700875
Andreas Schwab6543bec2013-01-20 17:58:47 +0100876 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
877 sprintf(&guid_name[i], "%02x", TO_NATIVE((*guid)[i/2]));
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700878
879 strcpy(alias, "vmbus:");
880 strcat(alias, guid_name);
881
882 return 1;
883}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100884ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id, do_vmbus_entry);
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700885
Jean Delvared2653e92008-04-29 23:11:39 +0200886/* Looks like: i2c:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100887static int do_i2c_entry(const char *filename, void *symval,
Jean Delvared2653e92008-04-29 23:11:39 +0200888 char *alias)
889{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100890 DEF_FIELD_ADDR(symval, i2c_device_id, name);
891 sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
Jean Delvared2653e92008-04-29 23:11:39 +0200892
893 return 1;
894}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100895ADD_TO_DEVTABLE("i2c", i2c_device_id, do_i2c_entry);
Jean Delvared2653e92008-04-29 23:11:39 +0200896
Anton Vorontsove0626e32009-09-22 16:46:08 -0700897/* Looks like: spi:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100898static int do_spi_entry(const char *filename, void *symval,
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700899 char *alias)
900{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100901 DEF_FIELD_ADDR(symval, spi_device_id, name);
902 sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700903
904 return 1;
905}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100906ADD_TO_DEVTABLE("spi", spi_device_id, do_spi_entry);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700907
David Woodhoused945b692008-09-16 16:23:28 -0700908static const struct dmifield {
909 const char *prefix;
910 int field;
911} dmi_fields[] = {
912 { "bvn", DMI_BIOS_VENDOR },
913 { "bvr", DMI_BIOS_VERSION },
914 { "bd", DMI_BIOS_DATE },
915 { "svn", DMI_SYS_VENDOR },
916 { "pn", DMI_PRODUCT_NAME },
917 { "pvr", DMI_PRODUCT_VERSION },
918 { "rvn", DMI_BOARD_VENDOR },
919 { "rn", DMI_BOARD_NAME },
920 { "rvr", DMI_BOARD_VERSION },
921 { "cvn", DMI_CHASSIS_VENDOR },
922 { "ct", DMI_CHASSIS_TYPE },
923 { "cvr", DMI_CHASSIS_VERSION },
924 { NULL, DMI_NONE }
925};
926
927static void dmi_ascii_filter(char *d, const char *s)
928{
929 /* Filter out characters we don't want to see in the modalias string */
930 for (; *s; s++)
931 if (*s > ' ' && *s < 127 && *s != ':')
932 *(d++) = *s;
933
934 *d = 0;
935}
936
937
Andreas Schwab6543bec2013-01-20 17:58:47 +0100938static int do_dmi_entry(const char *filename, void *symval,
David Woodhoused945b692008-09-16 16:23:28 -0700939 char *alias)
940{
941 int i, j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100942 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
David Woodhoused945b692008-09-16 16:23:28 -0700943 sprintf(alias, "dmi*");
944
945 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
946 for (j = 0; j < 4; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100947 if ((*matches)[j].slot &&
948 (*matches)[j].slot == dmi_fields[i].field) {
David Woodhoused945b692008-09-16 16:23:28 -0700949 sprintf(alias + strlen(alias), ":%s*",
950 dmi_fields[i].prefix);
951 dmi_ascii_filter(alias + strlen(alias),
Andreas Schwab6543bec2013-01-20 17:58:47 +0100952 (*matches)[j].substr);
David Woodhoused945b692008-09-16 16:23:28 -0700953 strcat(alias, "*");
954 }
955 }
956 }
957
958 strcat(alias, ":");
959 return 1;
960}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100961ADD_TO_DEVTABLE("dmi", dmi_system_id, do_dmi_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +0800962
963static int do_platform_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100964 void *symval, char *alias)
Eric Miao57fee4a2009-02-04 11:52:40 +0800965{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100966 DEF_FIELD_ADDR(symval, platform_device_id, name);
967 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
Eric Miao57fee4a2009-02-04 11:52:40 +0800968 return 1;
969}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100970ADD_TO_DEVTABLE("platform", platform_device_id, do_platform_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +0800971
David Woodhouse8626d3b2010-04-02 01:05:27 +0000972static int do_mdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100973 void *symval, char *alias)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000974{
975 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100976 DEF_FIELD(symval, mdio_device_id, phy_id);
977 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
David Woodhouse8626d3b2010-04-02 01:05:27 +0000978
979 alias += sprintf(alias, MDIO_MODULE_PREFIX);
980
981 for (i = 0; i < 32; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100982 if (!((phy_id_mask >> (31-i)) & 1))
David Woodhouse8626d3b2010-04-02 01:05:27 +0000983 *(alias++) = '?';
Andreas Schwab6543bec2013-01-20 17:58:47 +0100984 else if ((phy_id >> (31-i)) & 1)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000985 *(alias++) = '1';
986 else
987 *(alias++) = '0';
988 }
989
990 /* Terminate the string */
991 *alias = 0;
992
993 return 1;
994}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100995ADD_TO_DEVTABLE("mdio", mdio_device_id, do_mdio_entry);
David Woodhouse8626d3b2010-04-02 01:05:27 +0000996
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100997/* Looks like: zorro:iN. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100998static int do_zorro_entry(const char *filename, void *symval,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100999 char *alias)
1000{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001001 DEF_FIELD(symval, zorro_device_id, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001002 strcpy(alias, "zorro:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001003 ADD(alias, "i", id != ZORRO_WILDCARD, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001004 return 1;
1005}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001006ADD_TO_DEVTABLE("zorro", zorro_device_id, do_zorro_entry);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001007
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001008/* looks like: "pnp:dD" */
1009static int do_isapnp_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001010 void *symval, char *alias)
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001011{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001012 DEF_FIELD(symval, isapnp_device_id, vendor);
1013 DEF_FIELD(symval, isapnp_device_id, function);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001014 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001015 'A' + ((vendor >> 2) & 0x3f) - 1,
1016 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1017 'A' + ((vendor >> 8) & 0x1f) - 1,
1018 (function >> 4) & 0x0f, function & 0x0f,
1019 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001020 return 1;
1021}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001022ADD_TO_DEVTABLE("isapnp", isapnp_device_id, do_isapnp_entry);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001023
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001024/* Looks like: "ipack:fNvNdN". */
1025static int do_ipack_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001026 void *symval, char *alias)
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001027{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001028 DEF_FIELD(symval, ipack_device_id, format);
1029 DEF_FIELD(symval, ipack_device_id, vendor);
1030 DEF_FIELD(symval, ipack_device_id, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001031 strcpy(alias, "ipack:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001032 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1033 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1034 ADD(alias, "d", device != IPACK_ANY_ID, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001035 add_wildcard(alias);
1036 return 1;
1037}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001038ADD_TO_DEVTABLE("ipack", ipack_device_id, do_ipack_entry);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001039
Dave Martin523817b2011-10-05 14:44:57 +01001040/*
1041 * Append a match expression for a single masked hex digit.
1042 * outp points to a pointer to the character at which to append.
1043 * *outp is updated on return to point just after the appended text,
1044 * to facilitate further appending.
1045 */
1046static void append_nibble_mask(char **outp,
1047 unsigned int nibble, unsigned int mask)
1048{
1049 char *p = *outp;
1050 unsigned int i;
1051
1052 switch (mask) {
1053 case 0:
1054 *p++ = '?';
1055 break;
1056
1057 case 0xf:
1058 p += sprintf(p, "%X", nibble);
1059 break;
1060
1061 default:
1062 /*
1063 * Dumbly emit a match pattern for all possible matching
1064 * digits. This could be improved in some cases using ranges,
1065 * but it has the advantage of being trivially correct, and is
1066 * often optimal.
1067 */
1068 *p++ = '[';
1069 for (i = 0; i < 0x10; i++)
1070 if ((i & mask) == nibble)
1071 p += sprintf(p, "%X", i);
1072 *p++ = ']';
1073 }
1074
1075 /* Ensure that the string remains NUL-terminated: */
1076 *p = '\0';
1077
1078 /* Advance the caller's end-of-string pointer: */
1079 *outp = p;
1080}
1081
1082/*
1083 * looks like: "amba:dN"
1084 *
1085 * N is exactly 8 digits, where each is an upper-case hex digit, or
1086 * a ? or [] pattern matching exactly one digit.
1087 */
1088static int do_amba_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001089 void *symval, char *alias)
Dave Martin523817b2011-10-05 14:44:57 +01001090{
1091 unsigned int digit;
1092 char *p = alias;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001093 DEF_FIELD(symval, amba_id, id);
1094 DEF_FIELD(symval, amba_id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001095
Andreas Schwab6543bec2013-01-20 17:58:47 +01001096 if ((id & mask) != id)
Dave Martin523817b2011-10-05 14:44:57 +01001097 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1098 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001099 filename, id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001100
1101 p += sprintf(alias, "amba:d");
1102 for (digit = 0; digit < 8; digit++)
1103 append_nibble_mask(&p,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001104 (id >> (4 * (7 - digit))) & 0xf,
1105 (mask >> (4 * (7 - digit))) & 0xf);
Dave Martin523817b2011-10-05 14:44:57 +01001106
1107 return 1;
1108}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001109ADD_TO_DEVTABLE("amba", amba_id, do_amba_entry);
Dave Martin523817b2011-10-05 14:44:57 +01001110
Andi Kleen644e9cb2012-01-26 00:09:05 +01001111/* LOOKS like x86cpu:vendor:VVVV:family:FFFF:model:MMMM:feature:*,FEAT,*
1112 * All fields are numbers. It would be nicer to use strings for vendor
1113 * and feature, but getting those out of the build system here is too
1114 * complicated.
1115 */
1116
Andreas Schwab6543bec2013-01-20 17:58:47 +01001117static int do_x86cpu_entry(const char *filename, void *symval,
Andi Kleen644e9cb2012-01-26 00:09:05 +01001118 char *alias)
1119{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001120 DEF_FIELD(symval, x86_cpu_id, feature);
1121 DEF_FIELD(symval, x86_cpu_id, family);
1122 DEF_FIELD(symval, x86_cpu_id, model);
1123 DEF_FIELD(symval, x86_cpu_id, vendor);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001124
1125 strcpy(alias, "x86cpu:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001126 ADD(alias, "vendor:", vendor != X86_VENDOR_ANY, vendor);
1127 ADD(alias, ":family:", family != X86_FAMILY_ANY, family);
1128 ADD(alias, ":model:", model != X86_MODEL_ANY, model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001129 strcat(alias, ":feature:*");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001130 if (feature != X86_FEATURE_ANY)
1131 sprintf(alias + strlen(alias), "%04X*", feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001132 return 1;
1133}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001134ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001135
Rusty Russell626596e2012-01-13 09:32:15 +10301136/* Does namelen bytes of name exactly match the symbol? */
1137static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Rusty Russell626596e2012-01-13 09:32:15 +10301139 if (namelen != strlen(symbol))
1140 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Rusty Russell626596e2012-01-13 09:32:15 +10301142 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
1145static void do_table(void *symval, unsigned long size,
1146 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001147 const char *device_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 void *function,
1149 struct module *mod)
1150{
1151 unsigned int i;
1152 char alias[500];
1153 int (*do_entry)(const char *, void *entry, char *alias) = function;
1154
Kees Cooke0049822007-09-16 11:15:46 +02001155 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 /* Leave last one: it's the terminator. */
1157 size -= id_size;
1158
1159 for (i = 0; i < size; i += id_size) {
1160 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 buf_printf(&mod->dev_table_buf,
1162 "MODULE_ALIAS(\"%s\");\n", alias);
1163 }
1164 }
1165}
1166
1167/* Create MODULE_ALIAS() statements.
1168 * At this time, we cannot write the actual output C source yet,
1169 * so we write into the mod->dev_table_buf buffer. */
1170void handle_moddevtable(struct module *mod, struct elf_info *info,
1171 Elf_Sym *sym, const char *symname)
1172{
1173 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001174 char *zeros = NULL;
Rusty Russell626596e2012-01-13 09:32:15 +10301175 const char *name;
1176 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001179 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 return;
1181
David Millere88aa7b2012-04-12 14:37:30 -04001182 /* We're looking for an object */
1183 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1184 return;
1185
Rusty Russell626596e2012-01-13 09:32:15 +10301186 /* All our symbols are of form <prefix>__mod_XXX_device_table. */
1187 name = strstr(symname, "__mod_");
1188 if (!name)
1189 return;
1190 name += strlen("__mod_");
1191 namelen = strlen(name);
1192 if (namelen < strlen("_device_table"))
1193 return;
1194 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1195 return;
1196 namelen -= strlen("_device_table");
1197
Kees Cooke0049822007-09-16 11:15:46 +02001198 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001199 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001200 zeros = calloc(1, sym->st_size);
1201 symval = zeros;
1202 } else {
1203 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001204 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001205 + sym->st_value;
1206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Rusty Russell626596e2012-01-13 09:32:15 +10301208 /* First handle the "special" cases */
1209 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001210 do_usb_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301211 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001212 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301213 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001214 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301215 else {
Rusty Russelle49ce142012-01-13 09:32:16 +10301216 struct devtable **p;
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +01001217 INIT_SECTION(__devtable);
Rusty Russell626596e2012-01-13 09:32:15 +10301218
Rusty Russelle49ce142012-01-13 09:32:16 +10301219 for (p = __start___devtable; p < __stop___devtable; p++) {
1220 if (sym_is(name, namelen, (*p)->device_id)) {
1221 do_table(symval, sym->st_size, (*p)->id_size,
1222 (*p)->device_id, (*p)->function, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301223 break;
1224 }
1225 }
1226 }
Kees Cooke0049822007-09-16 11:15:46 +02001227 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230/* Now add out buffered information to the generated C source */
1231void add_moddevtable(struct buffer *buf, struct module *mod)
1232{
1233 buf_printf(buf, "\n");
1234 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1235 free(mod->dev_table_buf.p);
1236}