blob: 29d6699d5a06c1eddb52e9aa4175766297754e4c [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;
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -070037typedef struct {
38 __u8 b[16];
39} uuid_le;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Big exception to the "don't include kernel headers into userspace, which
Sam Ravnborg62070fa2006-03-03 16:46:04 +010042 * even potentially has different endianness and word sizes, since
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * we handle those differences explicitly below */
44#include "../../include/linux/mod_devicetable.h"
45
Rusty Russelle49ce142012-01-13 09:32:16 +103046/* This array collects all instances that use the generic do_table */
47struct devtable {
Tom Gundersen21bdd172014-02-03 11:14:13 +103048 const char *device_id; /* name of table, __mod_<name>__*_device_table. */
Rusty Russelle49ce142012-01-13 09:32:16 +103049 unsigned long id_size;
50 void *function;
51};
52
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010053#define ___cat(a,b) a ## b
54#define __cat(a,b) ___cat(a,b)
55
56/* we need some special handling for this host tool running eventually on
57 * Darwin. The Mach-O section handling is a bit different than ELF section
58 * handling. The differnces in detail are:
59 * a) we have segments which have sections
60 * b) we need a API call to get the respective section symbols */
61#if defined(__MACH__)
62#include <mach-o/getsect.h>
63
64#define INIT_SECTION(name) do { \
65 unsigned long name ## _len; \
66 char *__cat(pstart_,name) = getsectdata("__TEXT", \
67 #name, &__cat(name,_len)); \
68 char *__cat(pstop_,name) = __cat(pstart_,name) + \
69 __cat(name, _len); \
70 __cat(__start_,name) = (void *)__cat(pstart_,name); \
71 __cat(__stop_,name) = (void *)__cat(pstop_,name); \
72 } while (0)
73#define SECTION(name) __attribute__((section("__TEXT, " #name)))
74
75struct devtable **__start___devtable, **__stop___devtable;
76#else
77#define INIT_SECTION(name) /* no-op for ELF */
78#define SECTION(name) __attribute__((section(#name)))
79
Rusty Russelle49ce142012-01-13 09:32:16 +103080/* We construct a table of pointers in an ELF section (pointers generally
81 * go unpadded by gcc). ld creates boundary syms for us. */
82extern struct devtable *__start___devtable[], *__stop___devtable[];
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010083#endif /* __MACH__ */
Rusty Russelle49ce142012-01-13 09:32:16 +103084
Daniel Tang04130cc2013-06-09 12:33:55 +100085#if !defined(__used)
86# if __GNUC__ == 3 && __GNUC_MINOR__ < 3
87# define __used __attribute__((__unused__))
88# else
89# define __used __attribute__((__used__))
90# endif
Rusty Russelle49ce142012-01-13 09:32:16 +103091#endif
92
Andreas Schwab6543bec2013-01-20 17:58:47 +010093/* Define a variable f that holds the value of field f of struct devid
94 * based at address m.
95 */
96#define DEF_FIELD(m, devid, f) \
97 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
98/* Define a variable f that holds the address of field f of struct devid
99 * based at address m. Due to the way typeof works, for a field of type
100 * T[N] the variable has type T(*)[N], _not_ T*.
101 */
102#define DEF_FIELD_ADDR(m, devid, f) \
103 typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
104
Rusty Russelle49ce142012-01-13 09:32:16 +1030105/* Add a table entry. We test function type matches while we're here. */
106#define ADD_TO_DEVTABLE(device_id, type, function) \
107 static struct devtable __cat(devtable,__LINE__) = { \
108 device_id + 0*sizeof((function)((const char *)NULL, \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100109 (void *)NULL, \
Rusty Russelle49ce142012-01-13 09:32:16 +1030110 (char *)NULL)), \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100111 SIZE_##type, (function) }; \
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +0100112 static struct devtable *SECTION(__devtable) __used \
113 __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
Rusty Russelle49ce142012-01-13 09:32:16 +1030114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define ADD(str, sep, cond, field) \
116do { \
117 strcat(str, sep); \
118 if (cond) \
119 sprintf(str + strlen(str), \
120 sizeof(field) == 1 ? "%02X" : \
121 sizeof(field) == 2 ? "%04X" : \
122 sizeof(field) == 4 ? "%08X" : "", \
123 field); \
124 else \
125 sprintf(str + strlen(str), "*"); \
126} while(0)
127
Javier Martinez Canillas2f632362016-01-14 15:17:02 -0800128/* End in a wildcard, for future extension */
Jean Delvareac551822008-05-02 20:37:21 +0200129static inline void add_wildcard(char *str)
130{
131 int len = strlen(str);
132
133 if (str[len - 1] != '*')
134 strcat(str + len, "*");
135}
136
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700137static inline void add_uuid(char *str, uuid_le uuid)
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300138{
139 int len = strlen(str);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300140
Prarit Bhargava59796ed2015-09-10 10:17:59 +0300141 sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
142 uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
143 uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
144 uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
145 uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300146}
147
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200148/**
149 * Check that sizeof(device_id type) are consistent with size of section
150 * in .o file. If in-consistent then userspace and kernel does not agree
151 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200152 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100153 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200154 **/
Kees Cooke0049822007-09-16 11:15:46 +0200155static void device_id_check(const char *modname, const char *device_id,
156 unsigned long size, unsigned long id_size,
157 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200158{
Kees Cooke0049822007-09-16 11:15:46 +0200159 int i;
160
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200161 if (size % id_size || size < id_size) {
162 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
Tom Gundersen21bdd172014-02-03 11:14:13 +1030163 "of the size of "
164 "section __mod_%s__<identifier>_device_table=%lu.\n"
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200165 "Fix definition of struct %s_device_id "
166 "in mod_devicetable.h\n",
167 modname, device_id, id_size, device_id, size, device_id);
168 }
Kees Cooke0049822007-09-16 11:15:46 +0200169 /* Verify last one is a terminator */
170 for (i = 0; i < id_size; i++ ) {
171 if (*(uint8_t*)(symval+size-id_size+i)) {
172 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
173 "The last of %lu is:\n",
174 modname, device_id, id_size, size / id_size);
175 for (i = 0; i < id_size; i++ )
176 fprintf(stderr,"0x%02x ",
177 *(uint8_t*)(symval+size-id_size+i) );
178 fprintf(stderr,"\n");
179 fatal("%s: struct %s_device_id is not terminated "
180 "with a NULL entry!\n", modname, device_id);
181 }
182 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200183}
184
Roman Kaganb19dcd92005-04-22 15:07:01 -0700185/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200186/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100187static void do_usb_entry(void *symval,
Roman Kaganb19dcd92005-04-22 15:07:01 -0700188 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
189 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500190 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700192 char alias[500];
Andreas Schwab6543bec2013-01-20 17:58:47 +0100193 DEF_FIELD(symval, usb_device_id, match_flags);
194 DEF_FIELD(symval, usb_device_id, idVendor);
195 DEF_FIELD(symval, usb_device_id, idProduct);
196 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
197 DEF_FIELD(symval, usb_device_id, bDeviceClass);
198 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
199 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
200 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
201 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
202 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
203 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 strcpy(alias, "usb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100206 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
207 idVendor);
208 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
209 idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700210
211 strcat(alias, "d");
212 if (bcdDevice_initial_digits)
213 sprintf(alias + strlen(alias), "%0*X",
214 bcdDevice_initial_digits, bcdDevice_initial);
215 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500216 sprintf(alias + strlen(alias), "%X", range_lo);
217 else if (range_lo > 0 || range_hi < max) {
218 if (range_lo > 0x9 || range_hi < 0xA)
219 sprintf(alias + strlen(alias),
220 "[%X-%X]",
221 range_lo,
222 range_hi);
223 else {
224 sprintf(alias + strlen(alias),
225 range_lo < 0x9 ? "[%X-9" : "[%X",
226 range_lo);
227 sprintf(alias + strlen(alias),
Jan Moskyto Matejka03b56322014-02-07 19:15:11 +0100228 range_hi > 0xA ? "A-%X]" : "%X]",
229 range_hi);
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500230 }
231 }
Andreas Schwab6543bec2013-01-20 17:58:47 +0100232 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700233 strcat(alias, "*");
234
Andreas Schwab6543bec2013-01-20 17:58:47 +0100235 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
236 bDeviceClass);
237 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
238 bDeviceSubClass);
239 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
240 bDeviceProtocol);
241 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
242 bInterfaceClass);
243 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
244 bInterfaceSubClass);
245 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
246 bInterfaceProtocol);
247 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
248 bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700249
Jean Delvareac551822008-05-02 20:37:21 +0200250 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700251 buf_printf(&mod->dev_table_buf,
252 "MODULE_ALIAS(\"%s\");\n", alias);
253}
254
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500255/* Handles increment/decrement of BCD formatted integers */
256/* Returns the previous value, so it works like i++ or i-- */
257static unsigned int incbcd(unsigned int *bcd,
258 int inc,
259 unsigned char max,
260 size_t chars)
261{
262 unsigned int init = *bcd, i, j;
263 unsigned long long c, dec = 0;
264
265 /* If bcd is not in BCD format, just increment */
266 if (max > 0x9) {
267 *bcd += inc;
268 return init;
269 }
270
271 /* Convert BCD to Decimal */
272 for (i=0 ; i < chars ; i++) {
273 c = (*bcd >> (i << 2)) & 0xf;
274 c = c > 9 ? 9 : c; /* force to bcd just in case */
275 for (j=0 ; j < i ; j++)
276 c = c * 10;
277 dec += c;
278 }
279
280 /* Do our increment/decrement */
281 dec += inc;
282 *bcd = 0;
283
284 /* Convert back to BCD */
285 for (i=0 ; i < chars ; i++) {
286 for (c=1,j=0 ; j < i ; j++)
287 c = c * 10;
288 c = (dec / c) % 10;
289 *bcd += c << (i << 2);
290 }
291 return init;
292}
293
Andreas Schwab6543bec2013-01-20 17:58:47 +0100294static void do_usb_entry_multi(void *symval, struct module *mod)
Roman Kaganb19dcd92005-04-22 15:07:01 -0700295{
296 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500297 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700298 int ndigits;
299
Andreas Schwab6543bec2013-01-20 17:58:47 +0100300 DEF_FIELD(symval, usb_device_id, match_flags);
301 DEF_FIELD(symval, usb_device_id, idVendor);
302 DEF_FIELD(symval, usb_device_id, idProduct);
303 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
304 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
305 DEF_FIELD(symval, usb_device_id, bDeviceClass);
306 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700307
Andreas Schwab6543bec2013-01-20 17:58:47 +0100308 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
309 bcdDevice_lo : 0x0U;
310 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
311 bcdDevice_hi : ~0x0U;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700312
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500313 /* Figure out if this entry is in bcd or hex format */
314 max = 0x9; /* Default to decimal format */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100315 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500316 clo = (devlo >> (ndigits << 2)) & 0xf;
317 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
318 if (clo > max || chi > max) {
319 max = 0xf;
320 break;
321 }
322 }
323
Roman Kaganb19dcd92005-04-22 15:07:01 -0700324 /*
325 * Some modules (visor) have empty slots as placeholder for
326 * run-time specification that results in catch-all alias
327 */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100328 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700329 return;
330
331 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100332 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
Roman Kaganb19dcd92005-04-22 15:07:01 -0700333 clo = devlo & 0xf;
334 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500335 if (chi > max) /* If we are in bcd mode, truncate if necessary */
336 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700337 devlo >>= 4;
338 devhi >>= 4;
339
340 if (devlo == devhi || !ndigits) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100341 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700342 break;
343 }
344
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500345 if (clo > 0x0)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100346 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500347 incbcd(&devlo, 1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100348 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500349 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700350
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500351 if (chi < max)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100352 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500353 incbcd(&devhi, -1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100354 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500355 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700356 }
357}
358
359static void do_usb_table(void *symval, unsigned long size,
360 struct module *mod)
361{
362 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100363 const unsigned long id_size = SIZE_usb_device_id;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700364
Kees Cooke0049822007-09-16 11:15:46 +0200365 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200366
Roman Kaganb19dcd92005-04-22 15:07:01 -0700367 /* Leave last one: it's the terminator. */
368 size -= id_size;
369
370 for (i = 0; i < size; i += id_size)
371 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700374static void do_of_entry_multi(void *symval, struct module *mod)
375{
376 char alias[500];
377 int len;
378 char *tmp;
379
380 DEF_FIELD_ADDR(symval, of_device_id, name);
381 DEF_FIELD_ADDR(symval, of_device_id, type);
382 DEF_FIELD_ADDR(symval, of_device_id, compatible);
383
384 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
385 (*type)[0] ? *type : "*");
386
Wolfram Sangb3c0a4d2016-06-06 18:48:38 +0200387 if ((*compatible)[0])
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700388 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
389 *compatible);
390
391 /* Replace all whitespace with underscores */
392 for (tmp = alias; tmp && *tmp; tmp++)
393 if (isspace(*tmp))
394 *tmp = '_';
395
396 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
397 strcat(alias, "C");
398 add_wildcard(alias);
399 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
400}
401
402static void do_of_table(void *symval, unsigned long size,
403 struct module *mod)
404{
405 unsigned int i;
406 const unsigned long id_size = SIZE_of_device_id;
407
408 device_id_check(mod->name, "of", size, id_size, symval);
409
410 /* Leave last one: it's the terminator. */
411 size -= id_size;
412
413 for (i = 0; i < size; i += id_size)
414 do_of_entry_multi(symval + i, mod);
415}
416
Jiri Slabye8c84f92008-05-19 15:50:01 +0200417/* Looks like: hid:bNvNpN */
418static int do_hid_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100419 void *symval, char *alias)
Jiri Slabye8c84f92008-05-19 15:50:01 +0200420{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100421 DEF_FIELD(symval, hid_device_id, bus);
422 DEF_FIELD(symval, hid_device_id, group);
423 DEF_FIELD(symval, hid_device_id, vendor);
424 DEF_FIELD(symval, hid_device_id, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200425
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200426 sprintf(alias, "hid:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100427 ADD(alias, "b", bus != HID_BUS_ANY, bus);
428 ADD(alias, "g", group != HID_GROUP_ANY, group);
429 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
430 ADD(alias, "p", product != HID_ANY_ID, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200431
432 return 1;
433}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100434ADD_TO_DEVTABLE("hid", hid_device_id, do_hid_entry);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/* Looks like: ieee1394:venNmoNspNverN */
437static int do_ieee1394_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100438 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100440 DEF_FIELD(symval, ieee1394_device_id, match_flags);
441 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
442 DEF_FIELD(symval, ieee1394_device_id, model_id);
443 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
444 DEF_FIELD(symval, ieee1394_device_id, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 strcpy(alias, "ieee1394:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100447 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
448 vendor_id);
449 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
450 model_id);
451 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
452 specifier_id);
453 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
454 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Jean Delvareac551822008-05-02 20:37:21 +0200456 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 1;
458}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100459ADD_TO_DEVTABLE("ieee1394", ieee1394_device_id, do_ieee1394_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
462static int do_pci_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100463 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 /* Class field can be divided into these three. */
466 unsigned char baseclass, subclass, interface,
467 baseclass_mask, subclass_mask, interface_mask;
468
Andreas Schwab6543bec2013-01-20 17:58:47 +0100469 DEF_FIELD(symval, pci_device_id, vendor);
470 DEF_FIELD(symval, pci_device_id, device);
471 DEF_FIELD(symval, pci_device_id, subvendor);
472 DEF_FIELD(symval, pci_device_id, subdevice);
473 DEF_FIELD(symval, pci_device_id, class);
474 DEF_FIELD(symval, pci_device_id, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 strcpy(alias, "pci:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100477 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
478 ADD(alias, "d", device != PCI_ANY_ID, device);
479 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
480 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Andreas Schwab6543bec2013-01-20 17:58:47 +0100482 baseclass = (class) >> 16;
483 baseclass_mask = (class_mask) >> 16;
484 subclass = (class) >> 8;
485 subclass_mask = (class_mask) >> 8;
486 interface = class;
487 interface_mask = class_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
490 || (subclass_mask != 0 && subclass_mask != 0xFF)
491 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100492 warn("Can't handle masks in %s:%04X\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100493 filename, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return 0;
495 }
496
497 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
498 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
499 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200500 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return 1;
502}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100503ADD_TO_DEVTABLE("pci", pci_device_id, do_pci_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100505/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506static int do_ccw_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100507 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100509 DEF_FIELD(symval, ccw_device_id, match_flags);
510 DEF_FIELD(symval, ccw_device_id, cu_type);
511 DEF_FIELD(symval, ccw_device_id, cu_model);
512 DEF_FIELD(symval, ccw_device_id, dev_type);
513 DEF_FIELD(symval, ccw_device_id, dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 strcpy(alias, "ccw:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100516 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
517 cu_type);
518 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
519 cu_model);
520 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
521 dev_type);
522 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
523 dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200524 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return 1;
526}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100527ADD_TO_DEVTABLE("ccw", ccw_device_id, do_ccw_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200529/* looks like: "ap:tN" */
530static int do_ap_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100531 void *symval, char *alias)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200532{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100533 DEF_FIELD(symval, ap_device_id, dev_type);
534
535 sprintf(alias, "ap:t%02X*", dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200536 return 1;
537}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100538ADD_TO_DEVTABLE("ap", ap_device_id, do_ap_entry);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200539
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200540/* looks like: "css:tN" */
541static int do_css_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100542 void *symval, char *alias)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200543{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100544 DEF_FIELD(symval, css_device_id, type);
545
546 sprintf(alias, "css:t%01X", type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200547 return 1;
548}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100549ADD_TO_DEVTABLE("css", css_device_id, do_css_entry);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/* Looks like: "serio:tyNprNidNexN" */
552static int do_serio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100553 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100555 DEF_FIELD(symval, serio_device_id, type);
556 DEF_FIELD(symval, serio_device_id, proto);
557 DEF_FIELD(symval, serio_device_id, id);
558 DEF_FIELD(symval, serio_device_id, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 strcpy(alias, "serio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100561 ADD(alias, "ty", type != SERIO_ANY, type);
562 ADD(alias, "pr", proto != SERIO_ANY, proto);
563 ADD(alias, "id", id != SERIO_ANY, id);
564 ADD(alias, "ex", extra != SERIO_ANY, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Jean Delvareac551822008-05-02 20:37:21 +0200566 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 1;
568}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100569ADD_TO_DEVTABLE("serio", serio_device_id, do_serio_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200571/* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
572 * "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
573 *
574 * NOTE: Each driver should use one of the following : _HID, _CIDs
575 * or _CLS. Also, bb, ss, and pp can be substituted with ??
576 * as don't care byte.
577 */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200578static int do_acpi_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100579 void *symval, char *alias)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200580{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100581 DEF_FIELD_ADDR(symval, acpi_device_id, id);
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200582 DEF_FIELD_ADDR(symval, acpi_device_id, cls);
583 DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk);
584
585 if (id && strlen((const char *)*id))
586 sprintf(alias, "acpi*:%s:*", *id);
587 else if (cls) {
588 int i, byte_shift, cnt = 0;
589 unsigned int msk;
590
591 sprintf(&alias[cnt], "acpi*:");
592 cnt = 6;
593 for (i = 1; i <= 3; i++) {
594 byte_shift = 8 * (3-i);
595 msk = (*cls_msk >> byte_shift) & 0xFF;
596 if (msk)
597 sprintf(&alias[cnt], "%02x",
598 (*cls >> byte_shift) & 0xFF);
599 else
600 sprintf(&alias[cnt], "??");
601 cnt += 2;
602 }
603 sprintf(&alias[cnt], ":*");
604 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200605 return 1;
606}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100607ADD_TO_DEVTABLE("acpi", acpi_device_id, do_acpi_entry);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200610static void do_pnp_device_entry(void *symval, unsigned long size,
611 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100613 const unsigned long id_size = SIZE_pnp_device_id;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200614 const unsigned int count = (size / id_size)-1;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200615 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200616
617 device_id_check(mod->name, "pnp", size, id_size, symval);
618
Kay Sievers5e4c6562008-08-21 15:28:56 +0200619 for (i = 0; i < count; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100620 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
621 char acpi_id[sizeof(*id)];
Kay Sievers72638f52009-01-08 03:06:42 +0100622 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200623
624 buf_printf(&mod->dev_table_buf,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100625 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
Kay Sievers72638f52009-01-08 03:06:42 +0100626
627 /* fix broken pnp bus lowercasing */
628 for (j = 0; j < sizeof(acpi_id); j++)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100629 acpi_id[j] = toupper((*id)[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200630 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100631 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Kay Sievers0c81eed2008-02-21 00:35:54 +0100635/* looks like: "pnp:dD" for every device of the card */
636static void do_pnp_card_entries(void *symval, unsigned long size,
637 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100639 const unsigned long id_size = SIZE_pnp_card_device_id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100640 const unsigned int count = (size / id_size)-1;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100641 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Kay Sievers0c81eed2008-02-21 00:35:54 +0100643 device_id_check(mod->name, "pnp", size, id_size, symval);
644
645 for (i = 0; i < count; i++) {
646 unsigned int j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100647 DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100648
649 for (j = 0; j < PNP_MAX_DEVICES; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100650 const char *id = (char *)(*devs)[j].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100651 int i2, j2;
652 int dup = 0;
653
654 if (!id[0])
655 break;
656
657 /* find duplicate, already added value */
658 for (i2 = 0; i2 < i && !dup; i2++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100659 DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100660
661 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100662 const char *id2 = (char *)(*devs)[j2].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100663
664 if (!id2[0])
665 break;
666
667 if (!strcmp(id, id2)) {
668 dup = 1;
669 break;
670 }
671 }
672 }
673
674 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200675 if (!dup) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100676 char acpi_id[PNP_ID_LEN];
Kay Sievers72638f52009-01-08 03:06:42 +0100677 int k;
678
Kay Sievers0c81eed2008-02-21 00:35:54 +0100679 buf_printf(&mod->dev_table_buf,
680 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100681
682 /* fix broken pnp bus lowercasing */
683 for (k = 0; k < sizeof(acpi_id); k++)
684 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200685 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100686 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200687 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700692/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
693static int do_pcmcia_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100694 void *symval, char *alias)
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700695{
696 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100697 DEF_FIELD(symval, pcmcia_device_id, match_flags);
698 DEF_FIELD(symval, pcmcia_device_id, manf_id);
699 DEF_FIELD(symval, pcmcia_device_id, card_id);
700 DEF_FIELD(symval, pcmcia_device_id, func_id);
701 DEF_FIELD(symval, pcmcia_device_id, function);
702 DEF_FIELD(symval, pcmcia_device_id, device_no);
703 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200704
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700705 for (i=0; i<4; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100706 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
707 }
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700708
Andreas Schwab6543bec2013-01-20 17:58:47 +0100709 strcpy(alias, "pcmcia:");
710 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
711 manf_id);
712 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
713 card_id);
714 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
715 func_id);
716 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
717 function);
718 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
719 device_no);
720 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
721 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
722 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
723 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700724
Jean Delvareac551822008-05-02 20:37:21 +0200725 add_wildcard(alias);
Andreas Schwab6543bec2013-01-20 17:58:47 +0100726 return 1;
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700727}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100728ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id, do_pcmcia_entry);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700729
Andreas Schwab6543bec2013-01-20 17:58:47 +0100730static int do_vio_entry(const char *filename, void *symval,
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000731 char *alias)
732{
733 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100734 DEF_FIELD_ADDR(symval, vio_device_id, type);
735 DEF_FIELD_ADDR(symval, vio_device_id, compat);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000736
Andreas Schwab6543bec2013-01-20 17:58:47 +0100737 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
738 (*compat)[0] ? *compat : "*");
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000739
740 /* Replace all whitespace with underscores */
741 for (tmp = alias; tmp && *tmp; tmp++)
742 if (isspace (*tmp))
743 *tmp = '_';
744
Jean Delvareac551822008-05-02 20:37:21 +0200745 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000746 return 1;
747}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100748ADD_TO_DEVTABLE("vio", vio_device_id, do_vio_entry);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000749
Rusty Russell1d8f4302005-12-07 21:40:34 +0100750#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
751
752static void do_input(char *alias,
753 kernel_ulong_t *arr, unsigned int min, unsigned int max)
754{
755 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400756
Andreas Schwab6543bec2013-01-20 17:58:47 +0100757 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
758 arr[i] = TO_NATIVE(arr[i]);
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400759 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200760 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400761 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100762}
763
764/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100765static int do_input_entry(const char *filename, void *symval,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100766 char *alias)
767{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100768 DEF_FIELD(symval, input_device_id, flags);
769 DEF_FIELD(symval, input_device_id, bustype);
770 DEF_FIELD(symval, input_device_id, vendor);
771 DEF_FIELD(symval, input_device_id, product);
772 DEF_FIELD(symval, input_device_id, version);
773 DEF_FIELD_ADDR(symval, input_device_id, evbit);
774 DEF_FIELD_ADDR(symval, input_device_id, keybit);
775 DEF_FIELD_ADDR(symval, input_device_id, relbit);
776 DEF_FIELD_ADDR(symval, input_device_id, absbit);
777 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
778 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
779 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
780 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
781 DEF_FIELD_ADDR(symval, input_device_id, swbit);
782
Rusty Russell1d8f4302005-12-07 21:40:34 +0100783 sprintf(alias, "input:");
784
Andreas Schwab6543bec2013-01-20 17:58:47 +0100785 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
786 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
787 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
788 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100789
790 sprintf(alias + strlen(alias), "-e*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100791 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
792 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100793 sprintf(alias + strlen(alias), "k*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100794 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
795 do_input(alias, *keybit,
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100796 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
797 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100798 sprintf(alias + strlen(alias), "r*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100799 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
800 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100801 sprintf(alias + strlen(alias), "a*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100802 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
803 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100804 sprintf(alias + strlen(alias), "m*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100805 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
806 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100807 sprintf(alias + strlen(alias), "l*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100808 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
809 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100810 sprintf(alias + strlen(alias), "s*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100811 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
812 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100813 sprintf(alias + strlen(alias), "f*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100814 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
815 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100816 sprintf(alias + strlen(alias), "w*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100817 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
818 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100819 return 1;
820}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100821ADD_TO_DEVTABLE("input", input_device_id, do_input_entry);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100822
Andreas Schwab6543bec2013-01-20 17:58:47 +0100823static int do_eisa_entry(const char *filename, void *symval,
Michael Tokarev07563c72006-09-27 01:50:56 -0700824 char *alias)
825{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100826 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
827 if (sig[0])
828 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
Jean Delvareac551822008-05-02 20:37:21 +0200829 else
830 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700831 return 1;
832}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100833ADD_TO_DEVTABLE("eisa", eisa_device_id, do_eisa_entry);
Michael Tokarev07563c72006-09-27 01:50:56 -0700834
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500835/* Looks like: parisc:tNhvNrevNsvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100836static int do_parisc_entry(const char *filename, void *symval,
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500837 char *alias)
838{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100839 DEF_FIELD(symval, parisc_device_id, hw_type);
840 DEF_FIELD(symval, parisc_device_id, hversion);
841 DEF_FIELD(symval, parisc_device_id, hversion_rev);
842 DEF_FIELD(symval, parisc_device_id, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500843
844 strcpy(alias, "parisc:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100845 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
846 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
847 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
848 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500849
Jean Delvareac551822008-05-02 20:37:21 +0200850 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500851 return 1;
852}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100853ADD_TO_DEVTABLE("parisc", parisc_device_id, do_parisc_entry);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500854
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200855/* Looks like: sdio:cNvNdN. */
856static int do_sdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100857 void *symval, char *alias)
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200858{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100859 DEF_FIELD(symval, sdio_device_id, class);
860 DEF_FIELD(symval, sdio_device_id, vendor);
861 DEF_FIELD(symval, sdio_device_id, device);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200862
863 strcpy(alias, "sdio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100864 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
865 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
866 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
Jean Delvareac551822008-05-02 20:37:21 +0200867 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700868 return 1;
869}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100870ADD_TO_DEVTABLE("sdio", sdio_device_id, do_sdio_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200871
Michael Buesch61e115a2007-09-18 15:12:50 -0400872/* Looks like: ssb:vNidNrevN. */
873static int do_ssb_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100874 void *symval, char *alias)
Michael Buesch61e115a2007-09-18 15:12:50 -0400875{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100876 DEF_FIELD(symval, ssb_device_id, vendor);
877 DEF_FIELD(symval, ssb_device_id, coreid);
878 DEF_FIELD(symval, ssb_device_id, revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400879
880 strcpy(alias, "ssb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100881 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
882 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
883 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
Jean Delvareac551822008-05-02 20:37:21 +0200884 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200885 return 1;
886}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100887ADD_TO_DEVTABLE("ssb", ssb_device_id, do_ssb_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200888
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200889/* Looks like: bcma:mNidNrevNclN. */
890static int do_bcma_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100891 void *symval, char *alias)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200892{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100893 DEF_FIELD(symval, bcma_device_id, manuf);
894 DEF_FIELD(symval, bcma_device_id, id);
895 DEF_FIELD(symval, bcma_device_id, rev);
896 DEF_FIELD(symval, bcma_device_id, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200897
898 strcpy(alias, "bcma:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100899 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
900 ADD(alias, "id", id != BCMA_ANY_ID, id);
901 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
902 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200903 add_wildcard(alias);
904 return 1;
905}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100906ADD_TO_DEVTABLE("bcma", bcma_device_id, do_bcma_entry);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200907
Rusty Russellb01d9f22007-10-22 11:03:39 +1000908/* Looks like: virtio:dNvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100909static int do_virtio_entry(const char *filename, void *symval,
Rusty Russellb01d9f22007-10-22 11:03:39 +1000910 char *alias)
911{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100912 DEF_FIELD(symval, virtio_device_id, device);
913 DEF_FIELD(symval, virtio_device_id, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000914
915 strcpy(alias, "virtio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100916 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
917 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000918
Jean Delvareac551822008-05-02 20:37:21 +0200919 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000920 return 1;
921}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100922ADD_TO_DEVTABLE("virtio", virtio_device_id, do_virtio_entry);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000923
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700924/*
925 * Looks like: vmbus:guid
926 * Each byte of the guid will be represented by two hex characters
927 * in the name.
928 */
929
Andreas Schwab6543bec2013-01-20 17:58:47 +0100930static int do_vmbus_entry(const char *filename, void *symval,
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700931 char *alias)
932{
933 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100934 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
935 char guid_name[(sizeof(*guid) + 1) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700936
Andreas Schwab6543bec2013-01-20 17:58:47 +0100937 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
K. Y. Srinivasanaf3ff642015-12-14 16:01:43 -0800938 sprintf(&guid_name[i], "%02x", TO_NATIVE((guid->b)[i/2]));
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700939
940 strcpy(alias, "vmbus:");
941 strcat(alias, guid_name);
942
943 return 1;
944}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100945ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id, do_vmbus_entry);
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700946
Jean Delvared2653e92008-04-29 23:11:39 +0200947/* Looks like: i2c:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100948static int do_i2c_entry(const char *filename, void *symval,
Jean Delvared2653e92008-04-29 23:11:39 +0200949 char *alias)
950{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100951 DEF_FIELD_ADDR(symval, i2c_device_id, name);
952 sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
Jean Delvared2653e92008-04-29 23:11:39 +0200953
954 return 1;
955}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100956ADD_TO_DEVTABLE("i2c", i2c_device_id, do_i2c_entry);
Jean Delvared2653e92008-04-29 23:11:39 +0200957
Anton Vorontsove0626e32009-09-22 16:46:08 -0700958/* Looks like: spi:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100959static int do_spi_entry(const char *filename, void *symval,
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700960 char *alias)
961{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100962 DEF_FIELD_ADDR(symval, spi_device_id, name);
963 sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700964
965 return 1;
966}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100967ADD_TO_DEVTABLE("spi", spi_device_id, do_spi_entry);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700968
David Woodhoused945b692008-09-16 16:23:28 -0700969static const struct dmifield {
970 const char *prefix;
971 int field;
972} dmi_fields[] = {
973 { "bvn", DMI_BIOS_VENDOR },
974 { "bvr", DMI_BIOS_VERSION },
975 { "bd", DMI_BIOS_DATE },
976 { "svn", DMI_SYS_VENDOR },
977 { "pn", DMI_PRODUCT_NAME },
978 { "pvr", DMI_PRODUCT_VERSION },
979 { "rvn", DMI_BOARD_VENDOR },
980 { "rn", DMI_BOARD_NAME },
981 { "rvr", DMI_BOARD_VERSION },
982 { "cvn", DMI_CHASSIS_VENDOR },
983 { "ct", DMI_CHASSIS_TYPE },
984 { "cvr", DMI_CHASSIS_VERSION },
985 { NULL, DMI_NONE }
986};
987
988static void dmi_ascii_filter(char *d, const char *s)
989{
990 /* Filter out characters we don't want to see in the modalias string */
991 for (; *s; s++)
992 if (*s > ' ' && *s < 127 && *s != ':')
993 *(d++) = *s;
994
995 *d = 0;
996}
997
998
Andreas Schwab6543bec2013-01-20 17:58:47 +0100999static int do_dmi_entry(const char *filename, void *symval,
David Woodhoused945b692008-09-16 16:23:28 -07001000 char *alias)
1001{
1002 int i, j;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001003 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
David Woodhoused945b692008-09-16 16:23:28 -07001004 sprintf(alias, "dmi*");
1005
1006 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
1007 for (j = 0; j < 4; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +01001008 if ((*matches)[j].slot &&
1009 (*matches)[j].slot == dmi_fields[i].field) {
David Woodhoused945b692008-09-16 16:23:28 -07001010 sprintf(alias + strlen(alias), ":%s*",
1011 dmi_fields[i].prefix);
1012 dmi_ascii_filter(alias + strlen(alias),
Andreas Schwab6543bec2013-01-20 17:58:47 +01001013 (*matches)[j].substr);
David Woodhoused945b692008-09-16 16:23:28 -07001014 strcat(alias, "*");
1015 }
1016 }
1017 }
1018
1019 strcat(alias, ":");
1020 return 1;
1021}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001022ADD_TO_DEVTABLE("dmi", dmi_system_id, do_dmi_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +08001023
1024static int do_platform_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001025 void *symval, char *alias)
Eric Miao57fee4a2009-02-04 11:52:40 +08001026{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001027 DEF_FIELD_ADDR(symval, platform_device_id, name);
1028 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
Eric Miao57fee4a2009-02-04 11:52:40 +08001029 return 1;
1030}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001031ADD_TO_DEVTABLE("platform", platform_device_id, do_platform_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +08001032
David Woodhouse8626d3b2010-04-02 01:05:27 +00001033static int do_mdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001034 void *symval, char *alias)
David Woodhouse8626d3b2010-04-02 01:05:27 +00001035{
1036 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001037 DEF_FIELD(symval, mdio_device_id, phy_id);
1038 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
David Woodhouse8626d3b2010-04-02 01:05:27 +00001039
1040 alias += sprintf(alias, MDIO_MODULE_PREFIX);
1041
1042 for (i = 0; i < 32; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +01001043 if (!((phy_id_mask >> (31-i)) & 1))
David Woodhouse8626d3b2010-04-02 01:05:27 +00001044 *(alias++) = '?';
Andreas Schwab6543bec2013-01-20 17:58:47 +01001045 else if ((phy_id >> (31-i)) & 1)
David Woodhouse8626d3b2010-04-02 01:05:27 +00001046 *(alias++) = '1';
1047 else
1048 *(alias++) = '0';
1049 }
1050
1051 /* Terminate the string */
1052 *alias = 0;
1053
1054 return 1;
1055}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001056ADD_TO_DEVTABLE("mdio", mdio_device_id, do_mdio_entry);
David Woodhouse8626d3b2010-04-02 01:05:27 +00001057
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001058/* Looks like: zorro:iN. */
Andreas Schwab6543bec2013-01-20 17:58:47 +01001059static int do_zorro_entry(const char *filename, void *symval,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001060 char *alias)
1061{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001062 DEF_FIELD(symval, zorro_device_id, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001063 strcpy(alias, "zorro:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001064 ADD(alias, "i", id != ZORRO_WILDCARD, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001065 return 1;
1066}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001067ADD_TO_DEVTABLE("zorro", zorro_device_id, do_zorro_entry);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001068
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001069/* looks like: "pnp:dD" */
1070static int do_isapnp_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001071 void *symval, char *alias)
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001072{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001073 DEF_FIELD(symval, isapnp_device_id, vendor);
1074 DEF_FIELD(symval, isapnp_device_id, function);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001075 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001076 'A' + ((vendor >> 2) & 0x3f) - 1,
1077 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1078 'A' + ((vendor >> 8) & 0x1f) - 1,
1079 (function >> 4) & 0x0f, function & 0x0f,
1080 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001081 return 1;
1082}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001083ADD_TO_DEVTABLE("isapnp", isapnp_device_id, do_isapnp_entry);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001084
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001085/* Looks like: "ipack:fNvNdN". */
1086static int do_ipack_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001087 void *symval, char *alias)
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001088{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001089 DEF_FIELD(symval, ipack_device_id, format);
1090 DEF_FIELD(symval, ipack_device_id, vendor);
1091 DEF_FIELD(symval, ipack_device_id, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001092 strcpy(alias, "ipack:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001093 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1094 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1095 ADD(alias, "d", device != IPACK_ANY_ID, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001096 add_wildcard(alias);
1097 return 1;
1098}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001099ADD_TO_DEVTABLE("ipack", ipack_device_id, do_ipack_entry);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001100
Dave Martin523817b2011-10-05 14:44:57 +01001101/*
1102 * Append a match expression for a single masked hex digit.
1103 * outp points to a pointer to the character at which to append.
1104 * *outp is updated on return to point just after the appended text,
1105 * to facilitate further appending.
1106 */
1107static void append_nibble_mask(char **outp,
1108 unsigned int nibble, unsigned int mask)
1109{
1110 char *p = *outp;
1111 unsigned int i;
1112
1113 switch (mask) {
1114 case 0:
1115 *p++ = '?';
1116 break;
1117
1118 case 0xf:
1119 p += sprintf(p, "%X", nibble);
1120 break;
1121
1122 default:
1123 /*
1124 * Dumbly emit a match pattern for all possible matching
1125 * digits. This could be improved in some cases using ranges,
1126 * but it has the advantage of being trivially correct, and is
1127 * often optimal.
1128 */
1129 *p++ = '[';
1130 for (i = 0; i < 0x10; i++)
1131 if ((i & mask) == nibble)
1132 p += sprintf(p, "%X", i);
1133 *p++ = ']';
1134 }
1135
1136 /* Ensure that the string remains NUL-terminated: */
1137 *p = '\0';
1138
1139 /* Advance the caller's end-of-string pointer: */
1140 *outp = p;
1141}
1142
1143/*
1144 * looks like: "amba:dN"
1145 *
1146 * N is exactly 8 digits, where each is an upper-case hex digit, or
1147 * a ? or [] pattern matching exactly one digit.
1148 */
1149static int do_amba_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001150 void *symval, char *alias)
Dave Martin523817b2011-10-05 14:44:57 +01001151{
1152 unsigned int digit;
1153 char *p = alias;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001154 DEF_FIELD(symval, amba_id, id);
1155 DEF_FIELD(symval, amba_id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001156
Andreas Schwab6543bec2013-01-20 17:58:47 +01001157 if ((id & mask) != id)
Dave Martin523817b2011-10-05 14:44:57 +01001158 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1159 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001160 filename, id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001161
1162 p += sprintf(alias, "amba:d");
1163 for (digit = 0; digit < 8; digit++)
1164 append_nibble_mask(&p,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001165 (id >> (4 * (7 - digit))) & 0xf,
1166 (mask >> (4 * (7 - digit))) & 0xf);
Dave Martin523817b2011-10-05 14:44:57 +01001167
1168 return 1;
1169}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001170ADD_TO_DEVTABLE("amba", amba_id, do_amba_entry);
Dave Martin523817b2011-10-05 14:44:57 +01001171
James Hogan8286ae02015-03-25 15:39:50 +00001172/*
1173 * looks like: "mipscdmm:tN"
1174 *
1175 * N is exactly 2 digits, where each is an upper-case hex digit, or
1176 * a ? or [] pattern matching exactly one digit.
1177 */
1178static int do_mips_cdmm_entry(const char *filename,
1179 void *symval, char *alias)
1180{
1181 DEF_FIELD(symval, mips_cdmm_device_id, type);
1182
1183 sprintf(alias, "mipscdmm:t%02X*", type);
1184 return 1;
1185}
1186ADD_TO_DEVTABLE("mipscdmm", mips_cdmm_device_id, do_mips_cdmm_entry);
1187
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001188/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
Andi Kleen644e9cb2012-01-26 00:09:05 +01001189 * All fields are numbers. It would be nicer to use strings for vendor
1190 * and feature, but getting those out of the build system here is too
1191 * complicated.
1192 */
1193
Andreas Schwab6543bec2013-01-20 17:58:47 +01001194static int do_x86cpu_entry(const char *filename, void *symval,
Andi Kleen644e9cb2012-01-26 00:09:05 +01001195 char *alias)
1196{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001197 DEF_FIELD(symval, x86_cpu_id, feature);
1198 DEF_FIELD(symval, x86_cpu_id, family);
1199 DEF_FIELD(symval, x86_cpu_id, model);
1200 DEF_FIELD(symval, x86_cpu_id, vendor);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001201
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001202 strcpy(alias, "cpu:type:x86,");
1203 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1204 ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1205 ADD(alias, "mod", model != X86_MODEL_ANY, model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001206 strcat(alias, ":feature:*");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001207 if (feature != X86_FEATURE_ANY)
1208 sprintf(alias + strlen(alias), "%04X*", feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001209 return 1;
1210}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001211ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001212
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001213/* LOOKS like cpu:type:*:feature:*FEAT* */
1214static int do_cpu_entry(const char *filename, void *symval, char *alias)
1215{
1216 DEF_FIELD(symval, cpu_feature, feature);
1217
1218 sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1219 return 1;
1220}
1221ADD_TO_DEVTABLE("cpu", cpu_feature, do_cpu_entry);
1222
Tomas Winklerb26864c2015-09-10 10:18:01 +03001223/* Looks like: mei:S:uuid:N:* */
Samuel Ortize5354102013-03-27 17:29:53 +02001224static int do_mei_entry(const char *filename, void *symval,
1225 char *alias)
1226{
1227 DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001228 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001229 DEF_FIELD(symval, mei_cl_device_id, version);
Samuel Ortize5354102013-03-27 17:29:53 +02001230
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001231 sprintf(alias, MEI_CL_MODULE_PREFIX);
1232 sprintf(alias + strlen(alias), "%s:", (*name)[0] ? *name : "*");
1233 add_uuid(alias, *uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001234 ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001235
1236 strcat(alias, ":*");
Samuel Ortize5354102013-03-27 17:29:53 +02001237
1238 return 1;
1239}
1240ADD_TO_DEVTABLE("mei", mei_cl_device_id, do_mei_entry);
1241
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001242/* Looks like: rapidio:vNdNavNadN */
1243static int do_rio_entry(const char *filename,
1244 void *symval, char *alias)
1245{
1246 DEF_FIELD(symval, rio_device_id, did);
1247 DEF_FIELD(symval, rio_device_id, vid);
1248 DEF_FIELD(symval, rio_device_id, asm_did);
1249 DEF_FIELD(symval, rio_device_id, asm_vid);
1250
1251 strcpy(alias, "rapidio:");
1252 ADD(alias, "v", vid != RIO_ANY_ID, vid);
1253 ADD(alias, "d", did != RIO_ANY_ID, did);
1254 ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1255 ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1256
1257 add_wildcard(alias);
1258 return 1;
1259}
1260ADD_TO_DEVTABLE("rapidio", rio_device_id, do_rio_entry);
1261
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001262/* Looks like: ulpi:vNpN */
1263static int do_ulpi_entry(const char *filename, void *symval,
1264 char *alias)
1265{
1266 DEF_FIELD(symval, ulpi_device_id, vendor);
1267 DEF_FIELD(symval, ulpi_device_id, product);
1268
1269 sprintf(alias, "ulpi:v%04xp%04x", vendor, product);
1270
1271 return 1;
1272}
1273ADD_TO_DEVTABLE("ulpi", ulpi_device_id, do_ulpi_entry);
1274
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301275/* Looks like: hdaudio:vNrNaN */
1276static int do_hda_entry(const char *filename, void *symval, char *alias)
1277{
1278 DEF_FIELD(symval, hda_device_id, vendor_id);
1279 DEF_FIELD(symval, hda_device_id, rev_id);
1280 DEF_FIELD(symval, hda_device_id, api_version);
1281
1282 strcpy(alias, "hdaudio:");
1283 ADD(alias, "v", vendor_id != 0, vendor_id);
1284 ADD(alias, "r", rev_id != 0, rev_id);
1285 ADD(alias, "a", api_version != 0, api_version);
1286
1287 add_wildcard(alias);
1288 return 1;
1289}
1290ADD_TO_DEVTABLE("hdaudio", hda_device_id, do_hda_entry);
1291
Stuart Yoder0afef452016-06-22 16:40:45 -05001292/* Looks like: fsl-mc:vNdN */
1293static int do_fsl_mc_entry(const char *filename, void *symval,
1294 char *alias)
1295{
1296 DEF_FIELD(symval, fsl_mc_device_id, vendor);
1297 DEF_FIELD_ADDR(symval, fsl_mc_device_id, obj_type);
1298
1299 sprintf(alias, "fsl-mc:v%08Xd%s", vendor, *obj_type);
1300 return 1;
1301}
1302ADD_TO_DEVTABLE("fslmc", fsl_mc_device_id, do_fsl_mc_entry);
1303
Rusty Russell626596e2012-01-13 09:32:15 +10301304/* Does namelen bytes of name exactly match the symbol? */
1305static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306{
Rusty Russell626596e2012-01-13 09:32:15 +10301307 if (namelen != strlen(symbol))
1308 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Rusty Russell626596e2012-01-13 09:32:15 +10301310 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313static void do_table(void *symval, unsigned long size,
1314 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001315 const char *device_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 void *function,
1317 struct module *mod)
1318{
1319 unsigned int i;
1320 char alias[500];
1321 int (*do_entry)(const char *, void *entry, char *alias) = function;
1322
Kees Cooke0049822007-09-16 11:15:46 +02001323 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 /* Leave last one: it's the terminator. */
1325 size -= id_size;
1326
1327 for (i = 0; i < size; i += id_size) {
1328 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 buf_printf(&mod->dev_table_buf,
1330 "MODULE_ALIAS(\"%s\");\n", alias);
1331 }
1332 }
1333}
1334
1335/* Create MODULE_ALIAS() statements.
1336 * At this time, we cannot write the actual output C source yet,
1337 * so we write into the mod->dev_table_buf buffer. */
1338void handle_moddevtable(struct module *mod, struct elf_info *info,
1339 Elf_Sym *sym, const char *symname)
1340{
1341 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001342 char *zeros = NULL;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301343 const char *name, *identifier;
Rusty Russell626596e2012-01-13 09:32:15 +10301344 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001347 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return;
1349
David Millere88aa7b2012-04-12 14:37:30 -04001350 /* We're looking for an object */
1351 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1352 return;
1353
Tom Gundersen21bdd172014-02-03 11:14:13 +10301354 /* All our symbols are of form <prefix>__mod_<name>__<identifier>_device_table. */
Rusty Russell626596e2012-01-13 09:32:15 +10301355 name = strstr(symname, "__mod_");
1356 if (!name)
1357 return;
1358 name += strlen("__mod_");
1359 namelen = strlen(name);
1360 if (namelen < strlen("_device_table"))
1361 return;
1362 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1363 return;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301364 identifier = strstr(name, "__");
1365 if (!identifier)
1366 return;
1367 namelen = identifier - name;
Rusty Russell626596e2012-01-13 09:32:15 +10301368
Kees Cooke0049822007-09-16 11:15:46 +02001369 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001370 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001371 zeros = calloc(1, sym->st_size);
1372 symval = zeros;
1373 } else {
1374 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001375 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001376 + sym->st_value;
1377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Rusty Russell626596e2012-01-13 09:32:15 +10301379 /* First handle the "special" cases */
1380 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001381 do_usb_table(symval, sym->st_size, mod);
Philipp Zabelacbef7b2016-05-05 16:22:29 -07001382 if (sym_is(name, namelen, "of"))
1383 do_of_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301384 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001385 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301386 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001387 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301388 else {
Rusty Russelle49ce142012-01-13 09:32:16 +10301389 struct devtable **p;
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +01001390 INIT_SECTION(__devtable);
Rusty Russell626596e2012-01-13 09:32:15 +10301391
Rusty Russelle49ce142012-01-13 09:32:16 +10301392 for (p = __start___devtable; p < __stop___devtable; p++) {
1393 if (sym_is(name, namelen, (*p)->device_id)) {
1394 do_table(symval, sym->st_size, (*p)->id_size,
1395 (*p)->device_id, (*p)->function, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301396 break;
1397 }
1398 }
1399 }
Kees Cooke0049822007-09-16 11:15:46 +02001400 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403/* Now add out buffered information to the generated C source */
1404void add_moddevtable(struct buffer *buf, struct module *mod)
1405{
1406 buf_printf(buf, "\n");
1407 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1408 free(mod->dev_table_buf.p);
1409}