blob: 28a61665bb9c95648ef3bd123a67d5e79888c28a [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))
Leonardo Brasc2b1a922018-10-24 01:03:52 -030098
99/* Define a variable v that holds the address of field f of struct devid
100 * based at address m. Due to the way typeof works, for a field of type
101 * T[N] the variable has type T(*)[N], _not_ T*.
102 */
103#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
104 typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
105
Andreas Schwab6543bec2013-01-20 17:58:47 +0100106/* Define a variable f that holds the address of field f of struct devid
107 * based at address m. Due to the way typeof works, for a field of type
108 * T[N] the variable has type T(*)[N], _not_ T*.
109 */
110#define DEF_FIELD_ADDR(m, devid, f) \
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300111 DEF_FIELD_ADDR_VAR(m, devid, f, f)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100112
Rusty Russelle49ce142012-01-13 09:32:16 +1030113/* Add a table entry. We test function type matches while we're here. */
114#define ADD_TO_DEVTABLE(device_id, type, function) \
115 static struct devtable __cat(devtable,__LINE__) = { \
116 device_id + 0*sizeof((function)((const char *)NULL, \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100117 (void *)NULL, \
Rusty Russelle49ce142012-01-13 09:32:16 +1030118 (char *)NULL)), \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100119 SIZE_##type, (function) }; \
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +0100120 static struct devtable *SECTION(__devtable) __used \
121 __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
Rusty Russelle49ce142012-01-13 09:32:16 +1030122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#define ADD(str, sep, cond, field) \
124do { \
125 strcat(str, sep); \
126 if (cond) \
127 sprintf(str + strlen(str), \
128 sizeof(field) == 1 ? "%02X" : \
129 sizeof(field) == 2 ? "%04X" : \
130 sizeof(field) == 4 ? "%08X" : "", \
131 field); \
132 else \
133 sprintf(str + strlen(str), "*"); \
134} while(0)
135
Javier Martinez Canillas2f632362016-01-14 15:17:02 -0800136/* End in a wildcard, for future extension */
Jean Delvareac551822008-05-02 20:37:21 +0200137static inline void add_wildcard(char *str)
138{
139 int len = strlen(str);
140
141 if (str[len - 1] != '*')
142 strcat(str + len, "*");
143}
144
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700145static inline void add_uuid(char *str, uuid_le uuid)
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300146{
147 int len = strlen(str);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300148
Prarit Bhargava59796ed2015-09-10 10:17:59 +0300149 sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
150 uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
151 uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
152 uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
153 uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300154}
155
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200156/**
157 * Check that sizeof(device_id type) are consistent with size of section
158 * in .o file. If in-consistent then userspace and kernel does not agree
159 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200160 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100161 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200162 **/
Kees Cooke0049822007-09-16 11:15:46 +0200163static void device_id_check(const char *modname, const char *device_id,
164 unsigned long size, unsigned long id_size,
165 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200166{
Kees Cooke0049822007-09-16 11:15:46 +0200167 int i;
168
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200169 if (size % id_size || size < id_size) {
170 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
Tom Gundersen21bdd172014-02-03 11:14:13 +1030171 "of the size of "
172 "section __mod_%s__<identifier>_device_table=%lu.\n"
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200173 "Fix definition of struct %s_device_id "
174 "in mod_devicetable.h\n",
175 modname, device_id, id_size, device_id, size, device_id);
176 }
Kees Cooke0049822007-09-16 11:15:46 +0200177 /* Verify last one is a terminator */
178 for (i = 0; i < id_size; i++ ) {
179 if (*(uint8_t*)(symval+size-id_size+i)) {
180 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
181 "The last of %lu is:\n",
182 modname, device_id, id_size, size / id_size);
183 for (i = 0; i < id_size; i++ )
184 fprintf(stderr,"0x%02x ",
185 *(uint8_t*)(symval+size-id_size+i) );
186 fprintf(stderr,"\n");
187 fatal("%s: struct %s_device_id is not terminated "
188 "with a NULL entry!\n", modname, device_id);
189 }
190 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200191}
192
Roman Kaganb19dcd92005-04-22 15:07:01 -0700193/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200194/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100195static void do_usb_entry(void *symval,
Roman Kaganb19dcd92005-04-22 15:07:01 -0700196 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
197 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500198 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700200 char alias[500];
Andreas Schwab6543bec2013-01-20 17:58:47 +0100201 DEF_FIELD(symval, usb_device_id, match_flags);
202 DEF_FIELD(symval, usb_device_id, idVendor);
203 DEF_FIELD(symval, usb_device_id, idProduct);
204 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
205 DEF_FIELD(symval, usb_device_id, bDeviceClass);
206 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
207 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
208 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
209 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
210 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
211 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 strcpy(alias, "usb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100214 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
215 idVendor);
216 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
217 idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700218
219 strcat(alias, "d");
220 if (bcdDevice_initial_digits)
221 sprintf(alias + strlen(alias), "%0*X",
222 bcdDevice_initial_digits, bcdDevice_initial);
223 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500224 sprintf(alias + strlen(alias), "%X", range_lo);
225 else if (range_lo > 0 || range_hi < max) {
226 if (range_lo > 0x9 || range_hi < 0xA)
227 sprintf(alias + strlen(alias),
228 "[%X-%X]",
229 range_lo,
230 range_hi);
231 else {
232 sprintf(alias + strlen(alias),
233 range_lo < 0x9 ? "[%X-9" : "[%X",
234 range_lo);
235 sprintf(alias + strlen(alias),
Jan Moskyto Matejka03b56322014-02-07 19:15:11 +0100236 range_hi > 0xA ? "A-%X]" : "%X]",
237 range_hi);
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500238 }
239 }
Andreas Schwab6543bec2013-01-20 17:58:47 +0100240 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700241 strcat(alias, "*");
242
Andreas Schwab6543bec2013-01-20 17:58:47 +0100243 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
244 bDeviceClass);
245 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
246 bDeviceSubClass);
247 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
248 bDeviceProtocol);
249 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
250 bInterfaceClass);
251 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
252 bInterfaceSubClass);
253 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
254 bInterfaceProtocol);
255 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
256 bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700257
Jean Delvareac551822008-05-02 20:37:21 +0200258 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700259 buf_printf(&mod->dev_table_buf,
260 "MODULE_ALIAS(\"%s\");\n", alias);
261}
262
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500263/* Handles increment/decrement of BCD formatted integers */
264/* Returns the previous value, so it works like i++ or i-- */
265static unsigned int incbcd(unsigned int *bcd,
266 int inc,
267 unsigned char max,
268 size_t chars)
269{
270 unsigned int init = *bcd, i, j;
271 unsigned long long c, dec = 0;
272
273 /* If bcd is not in BCD format, just increment */
274 if (max > 0x9) {
275 *bcd += inc;
276 return init;
277 }
278
279 /* Convert BCD to Decimal */
280 for (i=0 ; i < chars ; i++) {
281 c = (*bcd >> (i << 2)) & 0xf;
282 c = c > 9 ? 9 : c; /* force to bcd just in case */
283 for (j=0 ; j < i ; j++)
284 c = c * 10;
285 dec += c;
286 }
287
288 /* Do our increment/decrement */
289 dec += inc;
290 *bcd = 0;
291
292 /* Convert back to BCD */
293 for (i=0 ; i < chars ; i++) {
294 for (c=1,j=0 ; j < i ; j++)
295 c = c * 10;
296 c = (dec / c) % 10;
297 *bcd += c << (i << 2);
298 }
299 return init;
300}
301
Andreas Schwab6543bec2013-01-20 17:58:47 +0100302static void do_usb_entry_multi(void *symval, struct module *mod)
Roman Kaganb19dcd92005-04-22 15:07:01 -0700303{
304 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500305 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700306 int ndigits;
307
Andreas Schwab6543bec2013-01-20 17:58:47 +0100308 DEF_FIELD(symval, usb_device_id, match_flags);
309 DEF_FIELD(symval, usb_device_id, idVendor);
310 DEF_FIELD(symval, usb_device_id, idProduct);
311 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
312 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
313 DEF_FIELD(symval, usb_device_id, bDeviceClass);
314 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700315
Andreas Schwab6543bec2013-01-20 17:58:47 +0100316 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
317 bcdDevice_lo : 0x0U;
318 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
319 bcdDevice_hi : ~0x0U;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700320
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500321 /* Figure out if this entry is in bcd or hex format */
322 max = 0x9; /* Default to decimal format */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100323 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500324 clo = (devlo >> (ndigits << 2)) & 0xf;
325 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
326 if (clo > max || chi > max) {
327 max = 0xf;
328 break;
329 }
330 }
331
Roman Kaganb19dcd92005-04-22 15:07:01 -0700332 /*
333 * Some modules (visor) have empty slots as placeholder for
334 * run-time specification that results in catch-all alias
335 */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100336 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700337 return;
338
339 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100340 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
Roman Kaganb19dcd92005-04-22 15:07:01 -0700341 clo = devlo & 0xf;
342 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500343 if (chi > max) /* If we are in bcd mode, truncate if necessary */
344 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700345 devlo >>= 4;
346 devhi >>= 4;
347
348 if (devlo == devhi || !ndigits) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100349 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700350 break;
351 }
352
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500353 if (clo > 0x0)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100354 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500355 incbcd(&devlo, 1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100356 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500357 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700358
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500359 if (chi < max)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100360 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500361 incbcd(&devhi, -1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100362 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500363 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700364 }
365}
366
367static void do_usb_table(void *symval, unsigned long size,
368 struct module *mod)
369{
370 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100371 const unsigned long id_size = SIZE_usb_device_id;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700372
Kees Cooke0049822007-09-16 11:15:46 +0200373 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200374
Roman Kaganb19dcd92005-04-22 15:07:01 -0700375 /* Leave last one: it's the terminator. */
376 size -= id_size;
377
378 for (i = 0; i < size; i += id_size)
379 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700382static void do_of_entry_multi(void *symval, struct module *mod)
383{
384 char alias[500];
385 int len;
386 char *tmp;
387
388 DEF_FIELD_ADDR(symval, of_device_id, name);
389 DEF_FIELD_ADDR(symval, of_device_id, type);
390 DEF_FIELD_ADDR(symval, of_device_id, compatible);
391
392 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
393 (*type)[0] ? *type : "*");
394
Wolfram Sangb3c0a4d2016-06-06 18:48:38 +0200395 if ((*compatible)[0])
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700396 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
397 *compatible);
398
399 /* Replace all whitespace with underscores */
400 for (tmp = alias; tmp && *tmp; tmp++)
401 if (isspace(*tmp))
402 *tmp = '_';
403
404 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
405 strcat(alias, "C");
406 add_wildcard(alias);
407 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
408}
409
410static void do_of_table(void *symval, unsigned long size,
411 struct module *mod)
412{
413 unsigned int i;
414 const unsigned long id_size = SIZE_of_device_id;
415
416 device_id_check(mod->name, "of", size, id_size, symval);
417
418 /* Leave last one: it's the terminator. */
419 size -= id_size;
420
421 for (i = 0; i < size; i += id_size)
422 do_of_entry_multi(symval + i, mod);
423}
424
Jiri Slabye8c84f92008-05-19 15:50:01 +0200425/* Looks like: hid:bNvNpN */
426static int do_hid_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100427 void *symval, char *alias)
Jiri Slabye8c84f92008-05-19 15:50:01 +0200428{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100429 DEF_FIELD(symval, hid_device_id, bus);
430 DEF_FIELD(symval, hid_device_id, group);
431 DEF_FIELD(symval, hid_device_id, vendor);
432 DEF_FIELD(symval, hid_device_id, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200433
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200434 sprintf(alias, "hid:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100435 ADD(alias, "b", bus != HID_BUS_ANY, bus);
436 ADD(alias, "g", group != HID_GROUP_ANY, group);
437 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
438 ADD(alias, "p", product != HID_ANY_ID, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200439
440 return 1;
441}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100442ADD_TO_DEVTABLE("hid", hid_device_id, do_hid_entry);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/* Looks like: ieee1394:venNmoNspNverN */
445static int do_ieee1394_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100446 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100448 DEF_FIELD(symval, ieee1394_device_id, match_flags);
449 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
450 DEF_FIELD(symval, ieee1394_device_id, model_id);
451 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
452 DEF_FIELD(symval, ieee1394_device_id, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 strcpy(alias, "ieee1394:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100455 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
456 vendor_id);
457 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
458 model_id);
459 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
460 specifier_id);
461 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
462 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
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("ieee1394", ieee1394_device_id, do_ieee1394_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
470static int do_pci_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100471 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 /* Class field can be divided into these three. */
474 unsigned char baseclass, subclass, interface,
475 baseclass_mask, subclass_mask, interface_mask;
476
Andreas Schwab6543bec2013-01-20 17:58:47 +0100477 DEF_FIELD(symval, pci_device_id, vendor);
478 DEF_FIELD(symval, pci_device_id, device);
479 DEF_FIELD(symval, pci_device_id, subvendor);
480 DEF_FIELD(symval, pci_device_id, subdevice);
481 DEF_FIELD(symval, pci_device_id, class);
482 DEF_FIELD(symval, pci_device_id, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 strcpy(alias, "pci:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100485 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
486 ADD(alias, "d", device != PCI_ANY_ID, device);
487 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
488 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Andreas Schwab6543bec2013-01-20 17:58:47 +0100490 baseclass = (class) >> 16;
491 baseclass_mask = (class_mask) >> 16;
492 subclass = (class) >> 8;
493 subclass_mask = (class_mask) >> 8;
494 interface = class;
495 interface_mask = class_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
498 || (subclass_mask != 0 && subclass_mask != 0xFF)
499 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100500 warn("Can't handle masks in %s:%04X\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100501 filename, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return 0;
503 }
504
505 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
506 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
507 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200508 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 return 1;
510}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100511ADD_TO_DEVTABLE("pci", pci_device_id, do_pci_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100513/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514static int do_ccw_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100515 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100517 DEF_FIELD(symval, ccw_device_id, match_flags);
518 DEF_FIELD(symval, ccw_device_id, cu_type);
519 DEF_FIELD(symval, ccw_device_id, cu_model);
520 DEF_FIELD(symval, ccw_device_id, dev_type);
521 DEF_FIELD(symval, ccw_device_id, dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 strcpy(alias, "ccw:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100524 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
525 cu_type);
526 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
527 cu_model);
528 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
529 dev_type);
530 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
531 dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200532 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return 1;
534}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100535ADD_TO_DEVTABLE("ccw", ccw_device_id, do_ccw_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200537/* looks like: "ap:tN" */
538static int do_ap_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100539 void *symval, char *alias)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200540{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100541 DEF_FIELD(symval, ap_device_id, dev_type);
542
543 sprintf(alias, "ap:t%02X*", dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200544 return 1;
545}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100546ADD_TO_DEVTABLE("ap", ap_device_id, do_ap_entry);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200547
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200548/* looks like: "css:tN" */
549static int do_css_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100550 void *symval, char *alias)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200551{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100552 DEF_FIELD(symval, css_device_id, type);
553
554 sprintf(alias, "css:t%01X", type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200555 return 1;
556}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100557ADD_TO_DEVTABLE("css", css_device_id, do_css_entry);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559/* Looks like: "serio:tyNprNidNexN" */
560static int do_serio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100561 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100563 DEF_FIELD(symval, serio_device_id, type);
564 DEF_FIELD(symval, serio_device_id, proto);
565 DEF_FIELD(symval, serio_device_id, id);
566 DEF_FIELD(symval, serio_device_id, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 strcpy(alias, "serio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100569 ADD(alias, "ty", type != SERIO_ANY, type);
570 ADD(alias, "pr", proto != SERIO_ANY, proto);
571 ADD(alias, "id", id != SERIO_ANY, id);
572 ADD(alias, "ex", extra != SERIO_ANY, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Jean Delvareac551822008-05-02 20:37:21 +0200574 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return 1;
576}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100577ADD_TO_DEVTABLE("serio", serio_device_id, do_serio_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200579/* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
580 * "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
581 *
582 * NOTE: Each driver should use one of the following : _HID, _CIDs
583 * or _CLS. Also, bb, ss, and pp can be substituted with ??
584 * as don't care byte.
585 */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200586static int do_acpi_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100587 void *symval, char *alias)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200588{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100589 DEF_FIELD_ADDR(symval, acpi_device_id, id);
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200590 DEF_FIELD_ADDR(symval, acpi_device_id, cls);
591 DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk);
592
593 if (id && strlen((const char *)*id))
594 sprintf(alias, "acpi*:%s:*", *id);
595 else if (cls) {
596 int i, byte_shift, cnt = 0;
597 unsigned int msk;
598
599 sprintf(&alias[cnt], "acpi*:");
600 cnt = 6;
601 for (i = 1; i <= 3; i++) {
602 byte_shift = 8 * (3-i);
603 msk = (*cls_msk >> byte_shift) & 0xFF;
604 if (msk)
605 sprintf(&alias[cnt], "%02x",
606 (*cls >> byte_shift) & 0xFF);
607 else
608 sprintf(&alias[cnt], "??");
609 cnt += 2;
610 }
611 sprintf(&alias[cnt], ":*");
612 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200613 return 1;
614}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100615ADD_TO_DEVTABLE("acpi", acpi_device_id, do_acpi_entry);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200618static void do_pnp_device_entry(void *symval, unsigned long size,
619 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100621 const unsigned long id_size = SIZE_pnp_device_id;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200622 const unsigned int count = (size / id_size)-1;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200623 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200624
625 device_id_check(mod->name, "pnp", size, id_size, symval);
626
Kay Sievers5e4c6562008-08-21 15:28:56 +0200627 for (i = 0; i < count; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100628 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
629 char acpi_id[sizeof(*id)];
Kay Sievers72638f52009-01-08 03:06:42 +0100630 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200631
632 buf_printf(&mod->dev_table_buf,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100633 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
Kay Sievers72638f52009-01-08 03:06:42 +0100634
635 /* fix broken pnp bus lowercasing */
636 for (j = 0; j < sizeof(acpi_id); j++)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100637 acpi_id[j] = toupper((*id)[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200638 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100639 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Kay Sievers0c81eed2008-02-21 00:35:54 +0100643/* looks like: "pnp:dD" for every device of the card */
644static void do_pnp_card_entries(void *symval, unsigned long size,
645 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100647 const unsigned long id_size = SIZE_pnp_card_device_id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100648 const unsigned int count = (size / id_size)-1;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100649 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Kay Sievers0c81eed2008-02-21 00:35:54 +0100651 device_id_check(mod->name, "pnp", size, id_size, symval);
652
653 for (i = 0; i < count; i++) {
654 unsigned int j;
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300655 DEF_FIELD_ADDR(symval + i * id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100656
657 for (j = 0; j < PNP_MAX_DEVICES; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100658 const char *id = (char *)(*devs)[j].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100659 int i2, j2;
660 int dup = 0;
661
662 if (!id[0])
663 break;
664
665 /* find duplicate, already added value */
666 for (i2 = 0; i2 < i && !dup; i2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300667 DEF_FIELD_ADDR_VAR(symval + i2 * id_size,
668 pnp_card_device_id,
669 devs, devs_dup);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100670
671 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300672 const char *id2 =
673 (char *)(*devs_dup)[j2].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100674
675 if (!id2[0])
676 break;
677
678 if (!strcmp(id, id2)) {
679 dup = 1;
680 break;
681 }
682 }
683 }
684
685 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200686 if (!dup) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100687 char acpi_id[PNP_ID_LEN];
Kay Sievers72638f52009-01-08 03:06:42 +0100688 int k;
689
Kay Sievers0c81eed2008-02-21 00:35:54 +0100690 buf_printf(&mod->dev_table_buf,
691 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100692
693 /* fix broken pnp bus lowercasing */
694 for (k = 0; k < sizeof(acpi_id); k++)
695 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200696 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100697 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200698 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
702
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700703/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
704static int do_pcmcia_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100705 void *symval, char *alias)
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700706{
707 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100708 DEF_FIELD(symval, pcmcia_device_id, match_flags);
709 DEF_FIELD(symval, pcmcia_device_id, manf_id);
710 DEF_FIELD(symval, pcmcia_device_id, card_id);
711 DEF_FIELD(symval, pcmcia_device_id, func_id);
712 DEF_FIELD(symval, pcmcia_device_id, function);
713 DEF_FIELD(symval, pcmcia_device_id, device_no);
714 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200715
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700716 for (i=0; i<4; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100717 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
718 }
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700719
Andreas Schwab6543bec2013-01-20 17:58:47 +0100720 strcpy(alias, "pcmcia:");
721 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
722 manf_id);
723 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
724 card_id);
725 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
726 func_id);
727 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
728 function);
729 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
730 device_no);
731 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
732 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
733 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
734 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700735
Jean Delvareac551822008-05-02 20:37:21 +0200736 add_wildcard(alias);
Andreas Schwab6543bec2013-01-20 17:58:47 +0100737 return 1;
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700738}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100739ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id, do_pcmcia_entry);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700740
Andreas Schwab6543bec2013-01-20 17:58:47 +0100741static int do_vio_entry(const char *filename, void *symval,
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000742 char *alias)
743{
744 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100745 DEF_FIELD_ADDR(symval, vio_device_id, type);
746 DEF_FIELD_ADDR(symval, vio_device_id, compat);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000747
Andreas Schwab6543bec2013-01-20 17:58:47 +0100748 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
749 (*compat)[0] ? *compat : "*");
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000750
751 /* Replace all whitespace with underscores */
752 for (tmp = alias; tmp && *tmp; tmp++)
753 if (isspace (*tmp))
754 *tmp = '_';
755
Jean Delvareac551822008-05-02 20:37:21 +0200756 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000757 return 1;
758}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100759ADD_TO_DEVTABLE("vio", vio_device_id, do_vio_entry);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000760
Rusty Russell1d8f4302005-12-07 21:40:34 +0100761#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
762
763static void do_input(char *alias,
764 kernel_ulong_t *arr, unsigned int min, unsigned int max)
765{
766 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400767
Andreas Schwab6543bec2013-01-20 17:58:47 +0100768 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
769 arr[i] = TO_NATIVE(arr[i]);
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400770 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200771 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400772 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100773}
774
Dmitry Torokhov09c3e012017-10-22 11:42:29 -0700775/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100776static int do_input_entry(const char *filename, void *symval,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100777 char *alias)
778{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100779 DEF_FIELD(symval, input_device_id, flags);
780 DEF_FIELD(symval, input_device_id, bustype);
781 DEF_FIELD(symval, input_device_id, vendor);
782 DEF_FIELD(symval, input_device_id, product);
783 DEF_FIELD(symval, input_device_id, version);
784 DEF_FIELD_ADDR(symval, input_device_id, evbit);
785 DEF_FIELD_ADDR(symval, input_device_id, keybit);
786 DEF_FIELD_ADDR(symval, input_device_id, relbit);
787 DEF_FIELD_ADDR(symval, input_device_id, absbit);
788 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
789 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
790 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
791 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
792 DEF_FIELD_ADDR(symval, input_device_id, swbit);
793
Rusty Russell1d8f4302005-12-07 21:40:34 +0100794 sprintf(alias, "input:");
795
Andreas Schwab6543bec2013-01-20 17:58:47 +0100796 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
797 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
798 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
799 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100800
801 sprintf(alias + strlen(alias), "-e*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100802 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
803 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100804 sprintf(alias + strlen(alias), "k*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100805 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
806 do_input(alias, *keybit,
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100807 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
808 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100809 sprintf(alias + strlen(alias), "r*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100810 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
811 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100812 sprintf(alias + strlen(alias), "a*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100813 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
814 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100815 sprintf(alias + strlen(alias), "m*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100816 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
817 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100818 sprintf(alias + strlen(alias), "l*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100819 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
820 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100821 sprintf(alias + strlen(alias), "s*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100822 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
823 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100824 sprintf(alias + strlen(alias), "f*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100825 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
826 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100827 sprintf(alias + strlen(alias), "w*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100828 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
829 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100830 return 1;
831}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100832ADD_TO_DEVTABLE("input", input_device_id, do_input_entry);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100833
Andreas Schwab6543bec2013-01-20 17:58:47 +0100834static int do_eisa_entry(const char *filename, void *symval,
Michael Tokarev07563c72006-09-27 01:50:56 -0700835 char *alias)
836{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100837 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
838 if (sig[0])
839 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
Jean Delvareac551822008-05-02 20:37:21 +0200840 else
841 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700842 return 1;
843}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100844ADD_TO_DEVTABLE("eisa", eisa_device_id, do_eisa_entry);
Michael Tokarev07563c72006-09-27 01:50:56 -0700845
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500846/* Looks like: parisc:tNhvNrevNsvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100847static int do_parisc_entry(const char *filename, void *symval,
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500848 char *alias)
849{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100850 DEF_FIELD(symval, parisc_device_id, hw_type);
851 DEF_FIELD(symval, parisc_device_id, hversion);
852 DEF_FIELD(symval, parisc_device_id, hversion_rev);
853 DEF_FIELD(symval, parisc_device_id, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500854
855 strcpy(alias, "parisc:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100856 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
857 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
858 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
859 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500860
Jean Delvareac551822008-05-02 20:37:21 +0200861 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500862 return 1;
863}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100864ADD_TO_DEVTABLE("parisc", parisc_device_id, do_parisc_entry);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500865
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200866/* Looks like: sdio:cNvNdN. */
867static int do_sdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100868 void *symval, char *alias)
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200869{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100870 DEF_FIELD(symval, sdio_device_id, class);
871 DEF_FIELD(symval, sdio_device_id, vendor);
872 DEF_FIELD(symval, sdio_device_id, device);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200873
874 strcpy(alias, "sdio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100875 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
876 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
877 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
Jean Delvareac551822008-05-02 20:37:21 +0200878 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700879 return 1;
880}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100881ADD_TO_DEVTABLE("sdio", sdio_device_id, do_sdio_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200882
Michael Buesch61e115a2007-09-18 15:12:50 -0400883/* Looks like: ssb:vNidNrevN. */
884static int do_ssb_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100885 void *symval, char *alias)
Michael Buesch61e115a2007-09-18 15:12:50 -0400886{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100887 DEF_FIELD(symval, ssb_device_id, vendor);
888 DEF_FIELD(symval, ssb_device_id, coreid);
889 DEF_FIELD(symval, ssb_device_id, revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400890
891 strcpy(alias, "ssb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100892 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
893 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
894 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
Jean Delvareac551822008-05-02 20:37:21 +0200895 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200896 return 1;
897}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100898ADD_TO_DEVTABLE("ssb", ssb_device_id, do_ssb_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200899
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200900/* Looks like: bcma:mNidNrevNclN. */
901static int do_bcma_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100902 void *symval, char *alias)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200903{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100904 DEF_FIELD(symval, bcma_device_id, manuf);
905 DEF_FIELD(symval, bcma_device_id, id);
906 DEF_FIELD(symval, bcma_device_id, rev);
907 DEF_FIELD(symval, bcma_device_id, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200908
909 strcpy(alias, "bcma:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100910 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
911 ADD(alias, "id", id != BCMA_ANY_ID, id);
912 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
913 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200914 add_wildcard(alias);
915 return 1;
916}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100917ADD_TO_DEVTABLE("bcma", bcma_device_id, do_bcma_entry);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200918
Rusty Russellb01d9f22007-10-22 11:03:39 +1000919/* Looks like: virtio:dNvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100920static int do_virtio_entry(const char *filename, void *symval,
Rusty Russellb01d9f22007-10-22 11:03:39 +1000921 char *alias)
922{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100923 DEF_FIELD(symval, virtio_device_id, device);
924 DEF_FIELD(symval, virtio_device_id, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000925
926 strcpy(alias, "virtio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100927 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
928 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000929
Jean Delvareac551822008-05-02 20:37:21 +0200930 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000931 return 1;
932}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100933ADD_TO_DEVTABLE("virtio", virtio_device_id, do_virtio_entry);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000934
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700935/*
936 * Looks like: vmbus:guid
937 * Each byte of the guid will be represented by two hex characters
938 * in the name.
939 */
940
Andreas Schwab6543bec2013-01-20 17:58:47 +0100941static int do_vmbus_entry(const char *filename, void *symval,
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700942 char *alias)
943{
944 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100945 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
946 char guid_name[(sizeof(*guid) + 1) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700947
Andreas Schwab6543bec2013-01-20 17:58:47 +0100948 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
K. Y. Srinivasanaf3ff642015-12-14 16:01:43 -0800949 sprintf(&guid_name[i], "%02x", TO_NATIVE((guid->b)[i/2]));
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700950
951 strcpy(alias, "vmbus:");
952 strcat(alias, guid_name);
953
954 return 1;
955}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100956ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id, do_vmbus_entry);
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700957
Andrew F. Davis5b7d1272018-04-21 18:55:29 -0500958/* Looks like: rpmsg:S */
959static int do_rpmsg_entry(const char *filename, void *symval,
960 char *alias)
961{
962 DEF_FIELD_ADDR(symval, rpmsg_device_id, name);
963 sprintf(alias, RPMSG_DEVICE_MODALIAS_FMT, *name);
964
965 return 1;
966}
967ADD_TO_DEVTABLE("rpmsg", rpmsg_device_id, do_rpmsg_entry);
968
Jean Delvared2653e92008-04-29 23:11:39 +0200969/* Looks like: i2c:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100970static int do_i2c_entry(const char *filename, void *symval,
Jean Delvared2653e92008-04-29 23:11:39 +0200971 char *alias)
972{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100973 DEF_FIELD_ADDR(symval, i2c_device_id, name);
974 sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
Jean Delvared2653e92008-04-29 23:11:39 +0200975
976 return 1;
977}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100978ADD_TO_DEVTABLE("i2c", i2c_device_id, do_i2c_entry);
Jean Delvared2653e92008-04-29 23:11:39 +0200979
Anton Vorontsove0626e32009-09-22 16:46:08 -0700980/* Looks like: spi:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100981static int do_spi_entry(const char *filename, void *symval,
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700982 char *alias)
983{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100984 DEF_FIELD_ADDR(symval, spi_device_id, name);
985 sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700986
987 return 1;
988}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100989ADD_TO_DEVTABLE("spi", spi_device_id, do_spi_entry);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700990
David Woodhoused945b692008-09-16 16:23:28 -0700991static const struct dmifield {
992 const char *prefix;
993 int field;
994} dmi_fields[] = {
995 { "bvn", DMI_BIOS_VENDOR },
996 { "bvr", DMI_BIOS_VERSION },
997 { "bd", DMI_BIOS_DATE },
998 { "svn", DMI_SYS_VENDOR },
999 { "pn", DMI_PRODUCT_NAME },
1000 { "pvr", DMI_PRODUCT_VERSION },
1001 { "rvn", DMI_BOARD_VENDOR },
1002 { "rn", DMI_BOARD_NAME },
1003 { "rvr", DMI_BOARD_VERSION },
1004 { "cvn", DMI_CHASSIS_VENDOR },
1005 { "ct", DMI_CHASSIS_TYPE },
1006 { "cvr", DMI_CHASSIS_VERSION },
1007 { NULL, DMI_NONE }
1008};
1009
1010static void dmi_ascii_filter(char *d, const char *s)
1011{
1012 /* Filter out characters we don't want to see in the modalias string */
1013 for (; *s; s++)
1014 if (*s > ' ' && *s < 127 && *s != ':')
1015 *(d++) = *s;
1016
1017 *d = 0;
1018}
1019
1020
Andreas Schwab6543bec2013-01-20 17:58:47 +01001021static int do_dmi_entry(const char *filename, void *symval,
David Woodhoused945b692008-09-16 16:23:28 -07001022 char *alias)
1023{
1024 int i, j;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001025 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
David Woodhoused945b692008-09-16 16:23:28 -07001026 sprintf(alias, "dmi*");
1027
1028 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
1029 for (j = 0; j < 4; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +01001030 if ((*matches)[j].slot &&
1031 (*matches)[j].slot == dmi_fields[i].field) {
David Woodhoused945b692008-09-16 16:23:28 -07001032 sprintf(alias + strlen(alias), ":%s*",
1033 dmi_fields[i].prefix);
1034 dmi_ascii_filter(alias + strlen(alias),
Andreas Schwab6543bec2013-01-20 17:58:47 +01001035 (*matches)[j].substr);
David Woodhoused945b692008-09-16 16:23:28 -07001036 strcat(alias, "*");
1037 }
1038 }
1039 }
1040
1041 strcat(alias, ":");
1042 return 1;
1043}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001044ADD_TO_DEVTABLE("dmi", dmi_system_id, do_dmi_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +08001045
1046static int do_platform_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001047 void *symval, char *alias)
Eric Miao57fee4a2009-02-04 11:52:40 +08001048{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001049 DEF_FIELD_ADDR(symval, platform_device_id, name);
1050 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
Eric Miao57fee4a2009-02-04 11:52:40 +08001051 return 1;
1052}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001053ADD_TO_DEVTABLE("platform", platform_device_id, do_platform_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +08001054
David Woodhouse8626d3b2010-04-02 01:05:27 +00001055static int do_mdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001056 void *symval, char *alias)
David Woodhouse8626d3b2010-04-02 01:05:27 +00001057{
1058 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001059 DEF_FIELD(symval, mdio_device_id, phy_id);
1060 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
David Woodhouse8626d3b2010-04-02 01:05:27 +00001061
1062 alias += sprintf(alias, MDIO_MODULE_PREFIX);
1063
1064 for (i = 0; i < 32; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +01001065 if (!((phy_id_mask >> (31-i)) & 1))
David Woodhouse8626d3b2010-04-02 01:05:27 +00001066 *(alias++) = '?';
Andreas Schwab6543bec2013-01-20 17:58:47 +01001067 else if ((phy_id >> (31-i)) & 1)
David Woodhouse8626d3b2010-04-02 01:05:27 +00001068 *(alias++) = '1';
1069 else
1070 *(alias++) = '0';
1071 }
1072
1073 /* Terminate the string */
1074 *alias = 0;
1075
1076 return 1;
1077}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001078ADD_TO_DEVTABLE("mdio", mdio_device_id, do_mdio_entry);
David Woodhouse8626d3b2010-04-02 01:05:27 +00001079
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001080/* Looks like: zorro:iN. */
Andreas Schwab6543bec2013-01-20 17:58:47 +01001081static int do_zorro_entry(const char *filename, void *symval,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001082 char *alias)
1083{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001084 DEF_FIELD(symval, zorro_device_id, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001085 strcpy(alias, "zorro:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001086 ADD(alias, "i", id != ZORRO_WILDCARD, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001087 return 1;
1088}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001089ADD_TO_DEVTABLE("zorro", zorro_device_id, do_zorro_entry);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001090
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001091/* looks like: "pnp:dD" */
1092static int do_isapnp_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001093 void *symval, char *alias)
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001094{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001095 DEF_FIELD(symval, isapnp_device_id, vendor);
1096 DEF_FIELD(symval, isapnp_device_id, function);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001097 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001098 'A' + ((vendor >> 2) & 0x3f) - 1,
1099 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1100 'A' + ((vendor >> 8) & 0x1f) - 1,
1101 (function >> 4) & 0x0f, function & 0x0f,
1102 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001103 return 1;
1104}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001105ADD_TO_DEVTABLE("isapnp", isapnp_device_id, do_isapnp_entry);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001106
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001107/* Looks like: "ipack:fNvNdN". */
1108static int do_ipack_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001109 void *symval, char *alias)
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001110{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001111 DEF_FIELD(symval, ipack_device_id, format);
1112 DEF_FIELD(symval, ipack_device_id, vendor);
1113 DEF_FIELD(symval, ipack_device_id, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001114 strcpy(alias, "ipack:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001115 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1116 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1117 ADD(alias, "d", device != IPACK_ANY_ID, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001118 add_wildcard(alias);
1119 return 1;
1120}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001121ADD_TO_DEVTABLE("ipack", ipack_device_id, do_ipack_entry);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001122
Dave Martin523817b2011-10-05 14:44:57 +01001123/*
1124 * Append a match expression for a single masked hex digit.
1125 * outp points to a pointer to the character at which to append.
1126 * *outp is updated on return to point just after the appended text,
1127 * to facilitate further appending.
1128 */
1129static void append_nibble_mask(char **outp,
1130 unsigned int nibble, unsigned int mask)
1131{
1132 char *p = *outp;
1133 unsigned int i;
1134
1135 switch (mask) {
1136 case 0:
1137 *p++ = '?';
1138 break;
1139
1140 case 0xf:
1141 p += sprintf(p, "%X", nibble);
1142 break;
1143
1144 default:
1145 /*
1146 * Dumbly emit a match pattern for all possible matching
1147 * digits. This could be improved in some cases using ranges,
1148 * but it has the advantage of being trivially correct, and is
1149 * often optimal.
1150 */
1151 *p++ = '[';
1152 for (i = 0; i < 0x10; i++)
1153 if ((i & mask) == nibble)
1154 p += sprintf(p, "%X", i);
1155 *p++ = ']';
1156 }
1157
1158 /* Ensure that the string remains NUL-terminated: */
1159 *p = '\0';
1160
1161 /* Advance the caller's end-of-string pointer: */
1162 *outp = p;
1163}
1164
1165/*
1166 * looks like: "amba:dN"
1167 *
1168 * N is exactly 8 digits, where each is an upper-case hex digit, or
1169 * a ? or [] pattern matching exactly one digit.
1170 */
1171static int do_amba_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001172 void *symval, char *alias)
Dave Martin523817b2011-10-05 14:44:57 +01001173{
1174 unsigned int digit;
1175 char *p = alias;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001176 DEF_FIELD(symval, amba_id, id);
1177 DEF_FIELD(symval, amba_id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001178
Andreas Schwab6543bec2013-01-20 17:58:47 +01001179 if ((id & mask) != id)
Dave Martin523817b2011-10-05 14:44:57 +01001180 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1181 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001182 filename, id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001183
1184 p += sprintf(alias, "amba:d");
1185 for (digit = 0; digit < 8; digit++)
1186 append_nibble_mask(&p,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001187 (id >> (4 * (7 - digit))) & 0xf,
1188 (mask >> (4 * (7 - digit))) & 0xf);
Dave Martin523817b2011-10-05 14:44:57 +01001189
1190 return 1;
1191}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001192ADD_TO_DEVTABLE("amba", amba_id, do_amba_entry);
Dave Martin523817b2011-10-05 14:44:57 +01001193
James Hogan8286ae02015-03-25 15:39:50 +00001194/*
1195 * looks like: "mipscdmm:tN"
1196 *
1197 * N is exactly 2 digits, where each is an upper-case hex digit, or
1198 * a ? or [] pattern matching exactly one digit.
1199 */
1200static int do_mips_cdmm_entry(const char *filename,
1201 void *symval, char *alias)
1202{
1203 DEF_FIELD(symval, mips_cdmm_device_id, type);
1204
1205 sprintf(alias, "mipscdmm:t%02X*", type);
1206 return 1;
1207}
1208ADD_TO_DEVTABLE("mipscdmm", mips_cdmm_device_id, do_mips_cdmm_entry);
1209
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001210/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
Andi Kleen644e9cb2012-01-26 00:09:05 +01001211 * All fields are numbers. It would be nicer to use strings for vendor
1212 * and feature, but getting those out of the build system here is too
1213 * complicated.
1214 */
1215
Andreas Schwab6543bec2013-01-20 17:58:47 +01001216static int do_x86cpu_entry(const char *filename, void *symval,
Andi Kleen644e9cb2012-01-26 00:09:05 +01001217 char *alias)
1218{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001219 DEF_FIELD(symval, x86_cpu_id, feature);
1220 DEF_FIELD(symval, x86_cpu_id, family);
1221 DEF_FIELD(symval, x86_cpu_id, model);
1222 DEF_FIELD(symval, x86_cpu_id, vendor);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001223
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001224 strcpy(alias, "cpu:type:x86,");
1225 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1226 ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1227 ADD(alias, "mod", model != X86_MODEL_ANY, model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001228 strcat(alias, ":feature:*");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001229 if (feature != X86_FEATURE_ANY)
1230 sprintf(alias + strlen(alias), "%04X*", feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001231 return 1;
1232}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001233ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001234
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001235/* LOOKS like cpu:type:*:feature:*FEAT* */
1236static int do_cpu_entry(const char *filename, void *symval, char *alias)
1237{
1238 DEF_FIELD(symval, cpu_feature, feature);
1239
1240 sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1241 return 1;
1242}
1243ADD_TO_DEVTABLE("cpu", cpu_feature, do_cpu_entry);
1244
Tomas Winklerb26864c2015-09-10 10:18:01 +03001245/* Looks like: mei:S:uuid:N:* */
Samuel Ortize5354102013-03-27 17:29:53 +02001246static int do_mei_entry(const char *filename, void *symval,
1247 char *alias)
1248{
1249 DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001250 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001251 DEF_FIELD(symval, mei_cl_device_id, version);
Samuel Ortize5354102013-03-27 17:29:53 +02001252
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001253 sprintf(alias, MEI_CL_MODULE_PREFIX);
1254 sprintf(alias + strlen(alias), "%s:", (*name)[0] ? *name : "*");
1255 add_uuid(alias, *uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001256 ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001257
1258 strcat(alias, ":*");
Samuel Ortize5354102013-03-27 17:29:53 +02001259
1260 return 1;
1261}
1262ADD_TO_DEVTABLE("mei", mei_cl_device_id, do_mei_entry);
1263
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001264/* Looks like: rapidio:vNdNavNadN */
1265static int do_rio_entry(const char *filename,
1266 void *symval, char *alias)
1267{
1268 DEF_FIELD(symval, rio_device_id, did);
1269 DEF_FIELD(symval, rio_device_id, vid);
1270 DEF_FIELD(symval, rio_device_id, asm_did);
1271 DEF_FIELD(symval, rio_device_id, asm_vid);
1272
1273 strcpy(alias, "rapidio:");
1274 ADD(alias, "v", vid != RIO_ANY_ID, vid);
1275 ADD(alias, "d", did != RIO_ANY_ID, did);
1276 ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1277 ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1278
1279 add_wildcard(alias);
1280 return 1;
1281}
1282ADD_TO_DEVTABLE("rapidio", rio_device_id, do_rio_entry);
1283
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001284/* Looks like: ulpi:vNpN */
1285static int do_ulpi_entry(const char *filename, void *symval,
1286 char *alias)
1287{
1288 DEF_FIELD(symval, ulpi_device_id, vendor);
1289 DEF_FIELD(symval, ulpi_device_id, product);
1290
1291 sprintf(alias, "ulpi:v%04xp%04x", vendor, product);
1292
1293 return 1;
1294}
1295ADD_TO_DEVTABLE("ulpi", ulpi_device_id, do_ulpi_entry);
1296
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301297/* Looks like: hdaudio:vNrNaN */
1298static int do_hda_entry(const char *filename, void *symval, char *alias)
1299{
1300 DEF_FIELD(symval, hda_device_id, vendor_id);
1301 DEF_FIELD(symval, hda_device_id, rev_id);
1302 DEF_FIELD(symval, hda_device_id, api_version);
1303
1304 strcpy(alias, "hdaudio:");
1305 ADD(alias, "v", vendor_id != 0, vendor_id);
1306 ADD(alias, "r", rev_id != 0, rev_id);
1307 ADD(alias, "a", api_version != 0, api_version);
1308
1309 add_wildcard(alias);
1310 return 1;
1311}
1312ADD_TO_DEVTABLE("hdaudio", hda_device_id, do_hda_entry);
1313
Vinod Koul92513452017-12-14 11:19:33 +05301314/* Looks like: sdw:mNpN */
1315static int do_sdw_entry(const char *filename, void *symval, char *alias)
1316{
1317 DEF_FIELD(symval, sdw_device_id, mfg_id);
1318 DEF_FIELD(symval, sdw_device_id, part_id);
1319
1320 strcpy(alias, "sdw:");
1321 ADD(alias, "m", mfg_id != 0, mfg_id);
1322 ADD(alias, "p", part_id != 0, part_id);
1323
1324 add_wildcard(alias);
1325 return 1;
1326}
1327ADD_TO_DEVTABLE("sdw", sdw_device_id, do_sdw_entry);
1328
Stuart Yoder0afef452016-06-22 16:40:45 -05001329/* Looks like: fsl-mc:vNdN */
1330static int do_fsl_mc_entry(const char *filename, void *symval,
1331 char *alias)
1332{
1333 DEF_FIELD(symval, fsl_mc_device_id, vendor);
1334 DEF_FIELD_ADDR(symval, fsl_mc_device_id, obj_type);
1335
1336 sprintf(alias, "fsl-mc:v%08Xd%s", vendor, *obj_type);
1337 return 1;
1338}
1339ADD_TO_DEVTABLE("fslmc", fsl_mc_device_id, do_fsl_mc_entry);
1340
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001341/* Looks like: tbsvc:kSpNvNrN */
1342static int do_tbsvc_entry(const char *filename, void *symval, char *alias)
1343{
1344 DEF_FIELD(symval, tb_service_id, match_flags);
1345 DEF_FIELD_ADDR(symval, tb_service_id, protocol_key);
1346 DEF_FIELD(symval, tb_service_id, protocol_id);
1347 DEF_FIELD(symval, tb_service_id, protocol_version);
1348 DEF_FIELD(symval, tb_service_id, protocol_revision);
1349
1350 strcpy(alias, "tbsvc:");
1351 if (match_flags & TBSVC_MATCH_PROTOCOL_KEY)
1352 sprintf(alias + strlen(alias), "k%s", *protocol_key);
1353 else
1354 strcat(alias + strlen(alias), "k*");
1355 ADD(alias, "p", match_flags & TBSVC_MATCH_PROTOCOL_ID, protocol_id);
1356 ADD(alias, "v", match_flags & TBSVC_MATCH_PROTOCOL_VERSION,
1357 protocol_version);
1358 ADD(alias, "r", match_flags & TBSVC_MATCH_PROTOCOL_REVISION,
1359 protocol_revision);
1360
1361 add_wildcard(alias);
1362 return 1;
1363}
1364ADD_TO_DEVTABLE("tbsvc", tb_service_id, do_tbsvc_entry);
1365
Heikki Krogerus8a37d872018-06-27 18:19:50 +03001366/* Looks like: typec:idNmN */
1367static int do_typec_entry(const char *filename, void *symval, char *alias)
1368{
1369 DEF_FIELD(symval, typec_device_id, svid);
1370 DEF_FIELD(symval, typec_device_id, mode);
1371
1372 sprintf(alias, "typec:id%04X", svid);
1373 ADD(alias, "m", mode != TYPEC_ANY_MODE, mode);
1374
1375 return 1;
1376}
1377ADD_TO_DEVTABLE("typec", typec_device_id, do_typec_entry);
1378
Rusty Russell626596e2012-01-13 09:32:15 +10301379/* Does namelen bytes of name exactly match the symbol? */
1380static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Rusty Russell626596e2012-01-13 09:32:15 +10301382 if (namelen != strlen(symbol))
1383 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Rusty Russell626596e2012-01-13 09:32:15 +10301385 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388static void do_table(void *symval, unsigned long size,
1389 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001390 const char *device_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 void *function,
1392 struct module *mod)
1393{
1394 unsigned int i;
1395 char alias[500];
1396 int (*do_entry)(const char *, void *entry, char *alias) = function;
1397
Kees Cooke0049822007-09-16 11:15:46 +02001398 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* Leave last one: it's the terminator. */
1400 size -= id_size;
1401
1402 for (i = 0; i < size; i += id_size) {
1403 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 buf_printf(&mod->dev_table_buf,
1405 "MODULE_ALIAS(\"%s\");\n", alias);
1406 }
1407 }
1408}
1409
1410/* Create MODULE_ALIAS() statements.
1411 * At this time, we cannot write the actual output C source yet,
1412 * so we write into the mod->dev_table_buf buffer. */
1413void handle_moddevtable(struct module *mod, struct elf_info *info,
1414 Elf_Sym *sym, const char *symname)
1415{
1416 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001417 char *zeros = NULL;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301418 const char *name, *identifier;
Rusty Russell626596e2012-01-13 09:32:15 +10301419 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001422 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 return;
1424
David Millere88aa7b2012-04-12 14:37:30 -04001425 /* We're looking for an object */
1426 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1427 return;
1428
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001429 /* All our symbols are of form __mod_<name>__<identifier>_device_table. */
1430 if (strncmp(symname, "__mod_", strlen("__mod_")))
Rusty Russell626596e2012-01-13 09:32:15 +10301431 return;
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001432 name = symname + strlen("__mod_");
Rusty Russell626596e2012-01-13 09:32:15 +10301433 namelen = strlen(name);
1434 if (namelen < strlen("_device_table"))
1435 return;
1436 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1437 return;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301438 identifier = strstr(name, "__");
1439 if (!identifier)
1440 return;
1441 namelen = identifier - name;
Rusty Russell626596e2012-01-13 09:32:15 +10301442
Kees Cooke0049822007-09-16 11:15:46 +02001443 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001444 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001445 zeros = calloc(1, sym->st_size);
1446 symval = zeros;
1447 } else {
1448 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001449 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001450 + sym->st_value;
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Rusty Russell626596e2012-01-13 09:32:15 +10301453 /* First handle the "special" cases */
1454 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001455 do_usb_table(symval, sym->st_size, mod);
Philipp Zabelacbef7b2016-05-05 16:22:29 -07001456 if (sym_is(name, namelen, "of"))
1457 do_of_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301458 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001459 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301460 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001461 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301462 else {
Rusty Russelle49ce142012-01-13 09:32:16 +10301463 struct devtable **p;
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +01001464 INIT_SECTION(__devtable);
Rusty Russell626596e2012-01-13 09:32:15 +10301465
Rusty Russelle49ce142012-01-13 09:32:16 +10301466 for (p = __start___devtable; p < __stop___devtable; p++) {
1467 if (sym_is(name, namelen, (*p)->device_id)) {
1468 do_table(symval, sym->st_size, (*p)->id_size,
1469 (*p)->device_id, (*p)->function, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301470 break;
1471 }
1472 }
1473 }
Kees Cooke0049822007-09-16 11:15:46 +02001474 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475}
1476
1477/* Now add out buffered information to the generated C source */
1478void add_moddevtable(struct buffer *buf, struct module *mod)
1479{
1480 buf_printf(buf, "\n");
1481 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1482 free(mod->dev_table_buf.p);
1483}