blob: 4e4f03a12cc0870792c8543a0d399ed1fe90d313 [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;
Mattias Jacobssoneacc95e2019-02-19 20:59:49 +010040#define UUID_STRING_LEN 36
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/* Big exception to the "don't include kernel headers into userspace, which
Sam Ravnborg62070fa2006-03-03 16:46:04 +010043 * even potentially has different endianness and word sizes, since
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 * we handle those differences explicitly below */
45#include "../../include/linux/mod_devicetable.h"
46
Rusty Russelle49ce142012-01-13 09:32:16 +103047/* This array collects all instances that use the generic do_table */
48struct devtable {
Tom Gundersen21bdd172014-02-03 11:14:13 +103049 const char *device_id; /* name of table, __mod_<name>__*_device_table. */
Rusty Russelle49ce142012-01-13 09:32:16 +103050 unsigned long id_size;
Masahiro Yamadaf880eea2018-11-22 13:28:42 +090051 int (*do_entry)(const char *filename, void *symval, char *alias);
Rusty Russelle49ce142012-01-13 09:32:16 +103052};
53
Mattias Jacobsson841f1b82019-02-07 13:30:22 +010054/* Size of alias provided to do_entry functions */
55#define ALIAS_SIZE 500
56
Andreas Schwab6543bec2013-01-20 17:58:47 +010057/* Define a variable f that holds the value of field f of struct devid
58 * based at address m.
59 */
60#define DEF_FIELD(m, devid, f) \
61 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
Leonardo Brasc2b1a922018-10-24 01:03:52 -030062
63/* Define a variable v that holds the address of field f of struct devid
64 * based at address m. Due to the way typeof works, for a field of type
65 * T[N] the variable has type T(*)[N], _not_ T*.
66 */
67#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
68 typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
69
Andreas Schwab6543bec2013-01-20 17:58:47 +010070/* Define a variable f that holds the address of field f of struct devid
71 * based at address m. Due to the way typeof works, for a field of type
72 * T[N] the variable has type T(*)[N], _not_ T*.
73 */
74#define DEF_FIELD_ADDR(m, devid, f) \
Leonardo Brasc2b1a922018-10-24 01:03:52 -030075 DEF_FIELD_ADDR_VAR(m, devid, f, f)
Andreas Schwab6543bec2013-01-20 17:58:47 +010076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define ADD(str, sep, cond, field) \
78do { \
79 strcat(str, sep); \
80 if (cond) \
81 sprintf(str + strlen(str), \
82 sizeof(field) == 1 ? "%02X" : \
83 sizeof(field) == 2 ? "%04X" : \
84 sizeof(field) == 4 ? "%08X" : "", \
85 field); \
86 else \
87 sprintf(str + strlen(str), "*"); \
88} while(0)
89
Javier Martinez Canillas2f632362016-01-14 15:17:02 -080090/* End in a wildcard, for future extension */
Jean Delvareac551822008-05-02 20:37:21 +020091static inline void add_wildcard(char *str)
92{
93 int len = strlen(str);
94
95 if (str[len - 1] != '*')
96 strcat(str + len, "*");
97}
98
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -070099static inline void add_uuid(char *str, uuid_le uuid)
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300100{
101 int len = strlen(str);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300102
Prarit Bhargava59796ed2015-09-10 10:17:59 +0300103 sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
104 uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
105 uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
106 uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
107 uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300108}
109
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200110/**
111 * Check that sizeof(device_id type) are consistent with size of section
112 * in .o file. If in-consistent then userspace and kernel does not agree
113 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200114 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100115 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200116 **/
Kees Cooke0049822007-09-16 11:15:46 +0200117static void device_id_check(const char *modname, const char *device_id,
118 unsigned long size, unsigned long id_size,
119 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200120{
Kees Cooke0049822007-09-16 11:15:46 +0200121 int i;
122
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200123 if (size % id_size || size < id_size) {
124 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
Tom Gundersen21bdd172014-02-03 11:14:13 +1030125 "of the size of "
126 "section __mod_%s__<identifier>_device_table=%lu.\n"
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200127 "Fix definition of struct %s_device_id "
128 "in mod_devicetable.h\n",
129 modname, device_id, id_size, device_id, size, device_id);
130 }
Kees Cooke0049822007-09-16 11:15:46 +0200131 /* Verify last one is a terminator */
132 for (i = 0; i < id_size; i++ ) {
133 if (*(uint8_t*)(symval+size-id_size+i)) {
134 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
135 "The last of %lu is:\n",
136 modname, device_id, id_size, size / id_size);
137 for (i = 0; i < id_size; i++ )
138 fprintf(stderr,"0x%02x ",
139 *(uint8_t*)(symval+size-id_size+i) );
140 fprintf(stderr,"\n");
141 fatal("%s: struct %s_device_id is not terminated "
142 "with a NULL entry!\n", modname, device_id);
143 }
144 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200145}
146
Roman Kaganb19dcd92005-04-22 15:07:01 -0700147/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200148/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100149static void do_usb_entry(void *symval,
Roman Kaganb19dcd92005-04-22 15:07:01 -0700150 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
151 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500152 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700154 char alias[500];
Andreas Schwab6543bec2013-01-20 17:58:47 +0100155 DEF_FIELD(symval, usb_device_id, match_flags);
156 DEF_FIELD(symval, usb_device_id, idVendor);
157 DEF_FIELD(symval, usb_device_id, idProduct);
158 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
159 DEF_FIELD(symval, usb_device_id, bDeviceClass);
160 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
161 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
162 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
163 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
164 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
165 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 strcpy(alias, "usb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100168 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
169 idVendor);
170 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
171 idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700172
173 strcat(alias, "d");
174 if (bcdDevice_initial_digits)
175 sprintf(alias + strlen(alias), "%0*X",
176 bcdDevice_initial_digits, bcdDevice_initial);
177 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500178 sprintf(alias + strlen(alias), "%X", range_lo);
179 else if (range_lo > 0 || range_hi < max) {
180 if (range_lo > 0x9 || range_hi < 0xA)
181 sprintf(alias + strlen(alias),
182 "[%X-%X]",
183 range_lo,
184 range_hi);
185 else {
186 sprintf(alias + strlen(alias),
187 range_lo < 0x9 ? "[%X-9" : "[%X",
188 range_lo);
189 sprintf(alias + strlen(alias),
Jan Moskyto Matejka03b56322014-02-07 19:15:11 +0100190 range_hi > 0xA ? "A-%X]" : "%X]",
191 range_hi);
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500192 }
193 }
Andreas Schwab6543bec2013-01-20 17:58:47 +0100194 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700195 strcat(alias, "*");
196
Andreas Schwab6543bec2013-01-20 17:58:47 +0100197 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
198 bDeviceClass);
199 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
200 bDeviceSubClass);
201 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
202 bDeviceProtocol);
203 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
204 bInterfaceClass);
205 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
206 bInterfaceSubClass);
207 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
208 bInterfaceProtocol);
209 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
210 bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700211
Jean Delvareac551822008-05-02 20:37:21 +0200212 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700213 buf_printf(&mod->dev_table_buf,
214 "MODULE_ALIAS(\"%s\");\n", alias);
215}
216
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500217/* Handles increment/decrement of BCD formatted integers */
218/* Returns the previous value, so it works like i++ or i-- */
219static unsigned int incbcd(unsigned int *bcd,
220 int inc,
221 unsigned char max,
222 size_t chars)
223{
224 unsigned int init = *bcd, i, j;
225 unsigned long long c, dec = 0;
226
227 /* If bcd is not in BCD format, just increment */
228 if (max > 0x9) {
229 *bcd += inc;
230 return init;
231 }
232
233 /* Convert BCD to Decimal */
234 for (i=0 ; i < chars ; i++) {
235 c = (*bcd >> (i << 2)) & 0xf;
236 c = c > 9 ? 9 : c; /* force to bcd just in case */
237 for (j=0 ; j < i ; j++)
238 c = c * 10;
239 dec += c;
240 }
241
242 /* Do our increment/decrement */
243 dec += inc;
244 *bcd = 0;
245
246 /* Convert back to BCD */
247 for (i=0 ; i < chars ; i++) {
248 for (c=1,j=0 ; j < i ; j++)
249 c = c * 10;
250 c = (dec / c) % 10;
251 *bcd += c << (i << 2);
252 }
253 return init;
254}
255
Andreas Schwab6543bec2013-01-20 17:58:47 +0100256static void do_usb_entry_multi(void *symval, struct module *mod)
Roman Kaganb19dcd92005-04-22 15:07:01 -0700257{
258 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500259 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700260 int ndigits;
261
Andreas Schwab6543bec2013-01-20 17:58:47 +0100262 DEF_FIELD(symval, usb_device_id, match_flags);
263 DEF_FIELD(symval, usb_device_id, idVendor);
264 DEF_FIELD(symval, usb_device_id, idProduct);
265 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
266 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
267 DEF_FIELD(symval, usb_device_id, bDeviceClass);
268 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700269
Andreas Schwab6543bec2013-01-20 17:58:47 +0100270 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
271 bcdDevice_lo : 0x0U;
272 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
273 bcdDevice_hi : ~0x0U;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700274
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500275 /* Figure out if this entry is in bcd or hex format */
276 max = 0x9; /* Default to decimal format */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100277 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500278 clo = (devlo >> (ndigits << 2)) & 0xf;
279 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
280 if (clo > max || chi > max) {
281 max = 0xf;
282 break;
283 }
284 }
285
Roman Kaganb19dcd92005-04-22 15:07:01 -0700286 /*
287 * Some modules (visor) have empty slots as placeholder for
288 * run-time specification that results in catch-all alias
289 */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100290 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700291 return;
292
293 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100294 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
Roman Kaganb19dcd92005-04-22 15:07:01 -0700295 clo = devlo & 0xf;
296 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500297 if (chi > max) /* If we are in bcd mode, truncate if necessary */
298 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700299 devlo >>= 4;
300 devhi >>= 4;
301
302 if (devlo == devhi || !ndigits) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100303 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700304 break;
305 }
306
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500307 if (clo > 0x0)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100308 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500309 incbcd(&devlo, 1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100310 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500311 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700312
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500313 if (chi < max)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100314 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500315 incbcd(&devhi, -1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100316 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500317 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700318 }
319}
320
321static void do_usb_table(void *symval, unsigned long size,
322 struct module *mod)
323{
324 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100325 const unsigned long id_size = SIZE_usb_device_id;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700326
Kees Cooke0049822007-09-16 11:15:46 +0200327 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200328
Roman Kaganb19dcd92005-04-22 15:07:01 -0700329 /* Leave last one: it's the terminator. */
330 size -= id_size;
331
332 for (i = 0; i < size; i += id_size)
333 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700336static void do_of_entry_multi(void *symval, struct module *mod)
337{
338 char alias[500];
339 int len;
340 char *tmp;
341
342 DEF_FIELD_ADDR(symval, of_device_id, name);
343 DEF_FIELD_ADDR(symval, of_device_id, type);
344 DEF_FIELD_ADDR(symval, of_device_id, compatible);
345
346 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
347 (*type)[0] ? *type : "*");
348
Wolfram Sangb3c0a4d2016-06-06 18:48:38 +0200349 if ((*compatible)[0])
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700350 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
351 *compatible);
352
353 /* Replace all whitespace with underscores */
354 for (tmp = alias; tmp && *tmp; tmp++)
355 if (isspace(*tmp))
356 *tmp = '_';
357
358 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
359 strcat(alias, "C");
360 add_wildcard(alias);
361 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
362}
363
364static void do_of_table(void *symval, unsigned long size,
365 struct module *mod)
366{
367 unsigned int i;
368 const unsigned long id_size = SIZE_of_device_id;
369
370 device_id_check(mod->name, "of", size, id_size, symval);
371
372 /* Leave last one: it's the terminator. */
373 size -= id_size;
374
375 for (i = 0; i < size; i += id_size)
376 do_of_entry_multi(symval + i, mod);
377}
378
Jiri Slabye8c84f92008-05-19 15:50:01 +0200379/* Looks like: hid:bNvNpN */
380static int do_hid_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100381 void *symval, char *alias)
Jiri Slabye8c84f92008-05-19 15:50:01 +0200382{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100383 DEF_FIELD(symval, hid_device_id, bus);
384 DEF_FIELD(symval, hid_device_id, group);
385 DEF_FIELD(symval, hid_device_id, vendor);
386 DEF_FIELD(symval, hid_device_id, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200387
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200388 sprintf(alias, "hid:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100389 ADD(alias, "b", bus != HID_BUS_ANY, bus);
390 ADD(alias, "g", group != HID_GROUP_ANY, group);
391 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
392 ADD(alias, "p", product != HID_ANY_ID, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200393
394 return 1;
395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/* Looks like: ieee1394:venNmoNspNverN */
398static int do_ieee1394_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100399 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100401 DEF_FIELD(symval, ieee1394_device_id, match_flags);
402 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
403 DEF_FIELD(symval, ieee1394_device_id, model_id);
404 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
405 DEF_FIELD(symval, ieee1394_device_id, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 strcpy(alias, "ieee1394:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100408 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
409 vendor_id);
410 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
411 model_id);
412 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
413 specifier_id);
414 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
415 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Jean Delvareac551822008-05-02 20:37:21 +0200417 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return 1;
419}
420
421/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
422static int do_pci_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100423 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 /* Class field can be divided into these three. */
426 unsigned char baseclass, subclass, interface,
427 baseclass_mask, subclass_mask, interface_mask;
428
Andreas Schwab6543bec2013-01-20 17:58:47 +0100429 DEF_FIELD(symval, pci_device_id, vendor);
430 DEF_FIELD(symval, pci_device_id, device);
431 DEF_FIELD(symval, pci_device_id, subvendor);
432 DEF_FIELD(symval, pci_device_id, subdevice);
433 DEF_FIELD(symval, pci_device_id, class);
434 DEF_FIELD(symval, pci_device_id, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 strcpy(alias, "pci:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100437 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
438 ADD(alias, "d", device != PCI_ANY_ID, device);
439 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
440 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Andreas Schwab6543bec2013-01-20 17:58:47 +0100442 baseclass = (class) >> 16;
443 baseclass_mask = (class_mask) >> 16;
444 subclass = (class) >> 8;
445 subclass_mask = (class_mask) >> 8;
446 interface = class;
447 interface_mask = class_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
450 || (subclass_mask != 0 && subclass_mask != 0xFF)
451 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100452 warn("Can't handle masks in %s:%04X\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100453 filename, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455 }
456
457 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
458 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
459 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200460 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 return 1;
462}
463
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100464/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465static int do_ccw_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100466 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100468 DEF_FIELD(symval, ccw_device_id, match_flags);
469 DEF_FIELD(symval, ccw_device_id, cu_type);
470 DEF_FIELD(symval, ccw_device_id, cu_model);
471 DEF_FIELD(symval, ccw_device_id, dev_type);
472 DEF_FIELD(symval, ccw_device_id, dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 strcpy(alias, "ccw:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100475 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
476 cu_type);
477 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
478 cu_model);
479 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
480 dev_type);
481 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
482 dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200483 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return 1;
485}
486
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200487/* looks like: "ap:tN" */
488static int do_ap_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100489 void *symval, char *alias)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200490{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100491 DEF_FIELD(symval, ap_device_id, dev_type);
492
493 sprintf(alias, "ap:t%02X*", dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200494 return 1;
495}
496
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200497/* looks like: "css:tN" */
498static int do_css_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100499 void *symval, char *alias)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200500{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100501 DEF_FIELD(symval, css_device_id, type);
502
503 sprintf(alias, "css:t%01X", type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200504 return 1;
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507/* Looks like: "serio:tyNprNidNexN" */
508static int do_serio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100509 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100511 DEF_FIELD(symval, serio_device_id, type);
512 DEF_FIELD(symval, serio_device_id, proto);
513 DEF_FIELD(symval, serio_device_id, id);
514 DEF_FIELD(symval, serio_device_id, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 strcpy(alias, "serio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100517 ADD(alias, "ty", type != SERIO_ANY, type);
518 ADD(alias, "pr", proto != SERIO_ANY, proto);
519 ADD(alias, "id", id != SERIO_ANY, id);
520 ADD(alias, "ex", extra != SERIO_ANY, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Jean Delvareac551822008-05-02 20:37:21 +0200522 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return 1;
524}
525
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200526/* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
527 * "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
528 *
529 * NOTE: Each driver should use one of the following : _HID, _CIDs
530 * or _CLS. Also, bb, ss, and pp can be substituted with ??
531 * as don't care byte.
532 */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200533static int do_acpi_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100534 void *symval, char *alias)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200535{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100536 DEF_FIELD_ADDR(symval, acpi_device_id, id);
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200537 DEF_FIELD_ADDR(symval, acpi_device_id, cls);
538 DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk);
539
540 if (id && strlen((const char *)*id))
541 sprintf(alias, "acpi*:%s:*", *id);
542 else if (cls) {
543 int i, byte_shift, cnt = 0;
544 unsigned int msk;
545
546 sprintf(&alias[cnt], "acpi*:");
547 cnt = 6;
548 for (i = 1; i <= 3; i++) {
549 byte_shift = 8 * (3-i);
550 msk = (*cls_msk >> byte_shift) & 0xFF;
551 if (msk)
552 sprintf(&alias[cnt], "%02x",
553 (*cls >> byte_shift) & 0xFF);
554 else
555 sprintf(&alias[cnt], "??");
556 cnt += 2;
557 }
558 sprintf(&alias[cnt], ":*");
559 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200560 return 1;
561}
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200564static void do_pnp_device_entry(void *symval, unsigned long size,
565 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100567 const unsigned long id_size = SIZE_pnp_device_id;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200568 const unsigned int count = (size / id_size)-1;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200569 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200570
571 device_id_check(mod->name, "pnp", size, id_size, symval);
572
Kay Sievers5e4c6562008-08-21 15:28:56 +0200573 for (i = 0; i < count; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100574 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
575 char acpi_id[sizeof(*id)];
Kay Sievers72638f52009-01-08 03:06:42 +0100576 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200577
578 buf_printf(&mod->dev_table_buf,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100579 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
Kay Sievers72638f52009-01-08 03:06:42 +0100580
581 /* fix broken pnp bus lowercasing */
582 for (j = 0; j < sizeof(acpi_id); j++)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100583 acpi_id[j] = toupper((*id)[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200584 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100585 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Kay Sievers0c81eed2008-02-21 00:35:54 +0100589/* looks like: "pnp:dD" for every device of the card */
590static void do_pnp_card_entries(void *symval, unsigned long size,
591 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100593 const unsigned long id_size = SIZE_pnp_card_device_id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100594 const unsigned int count = (size / id_size)-1;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100595 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Kay Sievers0c81eed2008-02-21 00:35:54 +0100597 device_id_check(mod->name, "pnp", size, id_size, symval);
598
599 for (i = 0; i < count; i++) {
600 unsigned int j;
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300601 DEF_FIELD_ADDR(symval + i * id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100602
603 for (j = 0; j < PNP_MAX_DEVICES; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100604 const char *id = (char *)(*devs)[j].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100605 int i2, j2;
606 int dup = 0;
607
608 if (!id[0])
609 break;
610
611 /* find duplicate, already added value */
612 for (i2 = 0; i2 < i && !dup; i2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300613 DEF_FIELD_ADDR_VAR(symval + i2 * id_size,
614 pnp_card_device_id,
615 devs, devs_dup);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100616
617 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300618 const char *id2 =
619 (char *)(*devs_dup)[j2].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100620
621 if (!id2[0])
622 break;
623
624 if (!strcmp(id, id2)) {
625 dup = 1;
626 break;
627 }
628 }
629 }
630
631 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200632 if (!dup) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100633 char acpi_id[PNP_ID_LEN];
Kay Sievers72638f52009-01-08 03:06:42 +0100634 int k;
635
Kay Sievers0c81eed2008-02-21 00:35:54 +0100636 buf_printf(&mod->dev_table_buf,
637 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100638
639 /* fix broken pnp bus lowercasing */
640 for (k = 0; k < sizeof(acpi_id); k++)
641 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200642 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100643 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200644 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700649/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
650static int do_pcmcia_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100651 void *symval, char *alias)
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700652{
653 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100654 DEF_FIELD(symval, pcmcia_device_id, match_flags);
655 DEF_FIELD(symval, pcmcia_device_id, manf_id);
656 DEF_FIELD(symval, pcmcia_device_id, card_id);
657 DEF_FIELD(symval, pcmcia_device_id, func_id);
658 DEF_FIELD(symval, pcmcia_device_id, function);
659 DEF_FIELD(symval, pcmcia_device_id, device_no);
660 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200661
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700662 for (i=0; i<4; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100663 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
664 }
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700665
Andreas Schwab6543bec2013-01-20 17:58:47 +0100666 strcpy(alias, "pcmcia:");
667 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
668 manf_id);
669 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
670 card_id);
671 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
672 func_id);
673 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
674 function);
675 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
676 device_no);
677 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
678 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
679 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
680 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700681
Jean Delvareac551822008-05-02 20:37:21 +0200682 add_wildcard(alias);
Andreas Schwab6543bec2013-01-20 17:58:47 +0100683 return 1;
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700684}
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700685
Andreas Schwab6543bec2013-01-20 17:58:47 +0100686static int do_vio_entry(const char *filename, void *symval,
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000687 char *alias)
688{
689 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100690 DEF_FIELD_ADDR(symval, vio_device_id, type);
691 DEF_FIELD_ADDR(symval, vio_device_id, compat);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000692
Andreas Schwab6543bec2013-01-20 17:58:47 +0100693 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
694 (*compat)[0] ? *compat : "*");
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000695
696 /* Replace all whitespace with underscores */
697 for (tmp = alias; tmp && *tmp; tmp++)
698 if (isspace (*tmp))
699 *tmp = '_';
700
Jean Delvareac551822008-05-02 20:37:21 +0200701 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000702 return 1;
703}
704
Rusty Russell1d8f4302005-12-07 21:40:34 +0100705#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
706
707static void do_input(char *alias,
708 kernel_ulong_t *arr, unsigned int min, unsigned int max)
709{
710 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400711
Andreas Schwab6543bec2013-01-20 17:58:47 +0100712 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
713 arr[i] = TO_NATIVE(arr[i]);
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400714 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200715 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400716 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100717}
718
Dmitry Torokhov09c3e012017-10-22 11:42:29 -0700719/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100720static int do_input_entry(const char *filename, void *symval,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100721 char *alias)
722{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100723 DEF_FIELD(symval, input_device_id, flags);
724 DEF_FIELD(symval, input_device_id, bustype);
725 DEF_FIELD(symval, input_device_id, vendor);
726 DEF_FIELD(symval, input_device_id, product);
727 DEF_FIELD(symval, input_device_id, version);
728 DEF_FIELD_ADDR(symval, input_device_id, evbit);
729 DEF_FIELD_ADDR(symval, input_device_id, keybit);
730 DEF_FIELD_ADDR(symval, input_device_id, relbit);
731 DEF_FIELD_ADDR(symval, input_device_id, absbit);
732 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
733 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
734 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
735 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
736 DEF_FIELD_ADDR(symval, input_device_id, swbit);
737
Rusty Russell1d8f4302005-12-07 21:40:34 +0100738 sprintf(alias, "input:");
739
Andreas Schwab6543bec2013-01-20 17:58:47 +0100740 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
741 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
742 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
743 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100744
745 sprintf(alias + strlen(alias), "-e*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100746 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
747 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100748 sprintf(alias + strlen(alias), "k*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100749 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
750 do_input(alias, *keybit,
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100751 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
752 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100753 sprintf(alias + strlen(alias), "r*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100754 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
755 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100756 sprintf(alias + strlen(alias), "a*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100757 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
758 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100759 sprintf(alias + strlen(alias), "m*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100760 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
761 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100762 sprintf(alias + strlen(alias), "l*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100763 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
764 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100765 sprintf(alias + strlen(alias), "s*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100766 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
767 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100768 sprintf(alias + strlen(alias), "f*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100769 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
770 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100771 sprintf(alias + strlen(alias), "w*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100772 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
773 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100774 return 1;
775}
776
Andreas Schwab6543bec2013-01-20 17:58:47 +0100777static int do_eisa_entry(const char *filename, void *symval,
Michael Tokarev07563c72006-09-27 01:50:56 -0700778 char *alias)
779{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100780 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
781 if (sig[0])
782 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
Jean Delvareac551822008-05-02 20:37:21 +0200783 else
784 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700785 return 1;
786}
787
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500788/* Looks like: parisc:tNhvNrevNsvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100789static int do_parisc_entry(const char *filename, void *symval,
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500790 char *alias)
791{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100792 DEF_FIELD(symval, parisc_device_id, hw_type);
793 DEF_FIELD(symval, parisc_device_id, hversion);
794 DEF_FIELD(symval, parisc_device_id, hversion_rev);
795 DEF_FIELD(symval, parisc_device_id, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500796
797 strcpy(alias, "parisc:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100798 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
799 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
800 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
801 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500802
Jean Delvareac551822008-05-02 20:37:21 +0200803 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500804 return 1;
805}
806
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200807/* Looks like: sdio:cNvNdN. */
808static int do_sdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100809 void *symval, char *alias)
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200810{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100811 DEF_FIELD(symval, sdio_device_id, class);
812 DEF_FIELD(symval, sdio_device_id, vendor);
813 DEF_FIELD(symval, sdio_device_id, device);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200814
815 strcpy(alias, "sdio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100816 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
817 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
818 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
Jean Delvareac551822008-05-02 20:37:21 +0200819 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700820 return 1;
821}
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200822
Michael Buesch61e115a2007-09-18 15:12:50 -0400823/* Looks like: ssb:vNidNrevN. */
824static int do_ssb_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100825 void *symval, char *alias)
Michael Buesch61e115a2007-09-18 15:12:50 -0400826{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100827 DEF_FIELD(symval, ssb_device_id, vendor);
828 DEF_FIELD(symval, ssb_device_id, coreid);
829 DEF_FIELD(symval, ssb_device_id, revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400830
831 strcpy(alias, "ssb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100832 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
833 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
834 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
Jean Delvareac551822008-05-02 20:37:21 +0200835 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200836 return 1;
837}
838
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200839/* Looks like: bcma:mNidNrevNclN. */
840static int do_bcma_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100841 void *symval, char *alias)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200842{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100843 DEF_FIELD(symval, bcma_device_id, manuf);
844 DEF_FIELD(symval, bcma_device_id, id);
845 DEF_FIELD(symval, bcma_device_id, rev);
846 DEF_FIELD(symval, bcma_device_id, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200847
848 strcpy(alias, "bcma:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100849 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
850 ADD(alias, "id", id != BCMA_ANY_ID, id);
851 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
852 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200853 add_wildcard(alias);
854 return 1;
855}
856
Rusty Russellb01d9f22007-10-22 11:03:39 +1000857/* Looks like: virtio:dNvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100858static int do_virtio_entry(const char *filename, void *symval,
Rusty Russellb01d9f22007-10-22 11:03:39 +1000859 char *alias)
860{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100861 DEF_FIELD(symval, virtio_device_id, device);
862 DEF_FIELD(symval, virtio_device_id, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000863
864 strcpy(alias, "virtio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100865 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
866 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000867
Jean Delvareac551822008-05-02 20:37:21 +0200868 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000869 return 1;
870}
871
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700872/*
873 * Looks like: vmbus:guid
874 * Each byte of the guid will be represented by two hex characters
875 * in the name.
876 */
877
Andreas Schwab6543bec2013-01-20 17:58:47 +0100878static int do_vmbus_entry(const char *filename, void *symval,
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700879 char *alias)
880{
881 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100882 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
883 char guid_name[(sizeof(*guid) + 1) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700884
Andreas Schwab6543bec2013-01-20 17:58:47 +0100885 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
K. Y. Srinivasanaf3ff642015-12-14 16:01:43 -0800886 sprintf(&guid_name[i], "%02x", TO_NATIVE((guid->b)[i/2]));
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700887
888 strcpy(alias, "vmbus:");
889 strcat(alias, guid_name);
890
891 return 1;
892}
893
Andrew F. Davis5b7d1272018-04-21 18:55:29 -0500894/* Looks like: rpmsg:S */
895static int do_rpmsg_entry(const char *filename, void *symval,
896 char *alias)
897{
898 DEF_FIELD_ADDR(symval, rpmsg_device_id, name);
899 sprintf(alias, RPMSG_DEVICE_MODALIAS_FMT, *name);
900
901 return 1;
902}
Andrew F. Davis5b7d1272018-04-21 18:55:29 -0500903
Jean Delvared2653e92008-04-29 23:11:39 +0200904/* Looks like: i2c:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100905static int do_i2c_entry(const char *filename, void *symval,
Jean Delvared2653e92008-04-29 23:11:39 +0200906 char *alias)
907{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100908 DEF_FIELD_ADDR(symval, i2c_device_id, name);
909 sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
Jean Delvared2653e92008-04-29 23:11:39 +0200910
911 return 1;
912}
913
Anton Vorontsove0626e32009-09-22 16:46:08 -0700914/* Looks like: spi:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100915static int do_spi_entry(const char *filename, void *symval,
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700916 char *alias)
917{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100918 DEF_FIELD_ADDR(symval, spi_device_id, name);
919 sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700920
921 return 1;
922}
923
David Woodhoused945b692008-09-16 16:23:28 -0700924static const struct dmifield {
925 const char *prefix;
926 int field;
927} dmi_fields[] = {
928 { "bvn", DMI_BIOS_VENDOR },
929 { "bvr", DMI_BIOS_VERSION },
930 { "bd", DMI_BIOS_DATE },
931 { "svn", DMI_SYS_VENDOR },
932 { "pn", DMI_PRODUCT_NAME },
933 { "pvr", DMI_PRODUCT_VERSION },
934 { "rvn", DMI_BOARD_VENDOR },
935 { "rn", DMI_BOARD_NAME },
936 { "rvr", DMI_BOARD_VERSION },
937 { "cvn", DMI_CHASSIS_VENDOR },
938 { "ct", DMI_CHASSIS_TYPE },
939 { "cvr", DMI_CHASSIS_VERSION },
940 { NULL, DMI_NONE }
941};
942
943static void dmi_ascii_filter(char *d, const char *s)
944{
945 /* Filter out characters we don't want to see in the modalias string */
946 for (; *s; s++)
947 if (*s > ' ' && *s < 127 && *s != ':')
948 *(d++) = *s;
949
950 *d = 0;
951}
952
953
Andreas Schwab6543bec2013-01-20 17:58:47 +0100954static int do_dmi_entry(const char *filename, void *symval,
David Woodhoused945b692008-09-16 16:23:28 -0700955 char *alias)
956{
957 int i, j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100958 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
David Woodhoused945b692008-09-16 16:23:28 -0700959 sprintf(alias, "dmi*");
960
961 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
962 for (j = 0; j < 4; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100963 if ((*matches)[j].slot &&
964 (*matches)[j].slot == dmi_fields[i].field) {
David Woodhoused945b692008-09-16 16:23:28 -0700965 sprintf(alias + strlen(alias), ":%s*",
966 dmi_fields[i].prefix);
967 dmi_ascii_filter(alias + strlen(alias),
Andreas Schwab6543bec2013-01-20 17:58:47 +0100968 (*matches)[j].substr);
David Woodhoused945b692008-09-16 16:23:28 -0700969 strcat(alias, "*");
970 }
971 }
972 }
973
974 strcat(alias, ":");
975 return 1;
976}
Eric Miao57fee4a2009-02-04 11:52:40 +0800977
978static int do_platform_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100979 void *symval, char *alias)
Eric Miao57fee4a2009-02-04 11:52:40 +0800980{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100981 DEF_FIELD_ADDR(symval, platform_device_id, name);
982 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
Eric Miao57fee4a2009-02-04 11:52:40 +0800983 return 1;
984}
985
David Woodhouse8626d3b2010-04-02 01:05:27 +0000986static int do_mdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100987 void *symval, char *alias)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000988{
989 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100990 DEF_FIELD(symval, mdio_device_id, phy_id);
991 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
David Woodhouse8626d3b2010-04-02 01:05:27 +0000992
993 alias += sprintf(alias, MDIO_MODULE_PREFIX);
994
995 for (i = 0; i < 32; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100996 if (!((phy_id_mask >> (31-i)) & 1))
David Woodhouse8626d3b2010-04-02 01:05:27 +0000997 *(alias++) = '?';
Andreas Schwab6543bec2013-01-20 17:58:47 +0100998 else if ((phy_id >> (31-i)) & 1)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000999 *(alias++) = '1';
1000 else
1001 *(alias++) = '0';
1002 }
1003
1004 /* Terminate the string */
1005 *alias = 0;
1006
1007 return 1;
1008}
1009
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001010/* Looks like: zorro:iN. */
Andreas Schwab6543bec2013-01-20 17:58:47 +01001011static int do_zorro_entry(const char *filename, void *symval,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001012 char *alias)
1013{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001014 DEF_FIELD(symval, zorro_device_id, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001015 strcpy(alias, "zorro:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001016 ADD(alias, "i", id != ZORRO_WILDCARD, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001017 return 1;
1018}
1019
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001020/* looks like: "pnp:dD" */
1021static int do_isapnp_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001022 void *symval, char *alias)
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001023{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001024 DEF_FIELD(symval, isapnp_device_id, vendor);
1025 DEF_FIELD(symval, isapnp_device_id, function);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001026 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001027 'A' + ((vendor >> 2) & 0x3f) - 1,
1028 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1029 'A' + ((vendor >> 8) & 0x1f) - 1,
1030 (function >> 4) & 0x0f, function & 0x0f,
1031 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001032 return 1;
1033}
1034
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001035/* Looks like: "ipack:fNvNdN". */
1036static int do_ipack_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001037 void *symval, char *alias)
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001038{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001039 DEF_FIELD(symval, ipack_device_id, format);
1040 DEF_FIELD(symval, ipack_device_id, vendor);
1041 DEF_FIELD(symval, ipack_device_id, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001042 strcpy(alias, "ipack:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001043 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1044 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1045 ADD(alias, "d", device != IPACK_ANY_ID, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001046 add_wildcard(alias);
1047 return 1;
1048}
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001049
Dave Martin523817b2011-10-05 14:44:57 +01001050/*
1051 * Append a match expression for a single masked hex digit.
1052 * outp points to a pointer to the character at which to append.
1053 * *outp is updated on return to point just after the appended text,
1054 * to facilitate further appending.
1055 */
1056static void append_nibble_mask(char **outp,
1057 unsigned int nibble, unsigned int mask)
1058{
1059 char *p = *outp;
1060 unsigned int i;
1061
1062 switch (mask) {
1063 case 0:
1064 *p++ = '?';
1065 break;
1066
1067 case 0xf:
1068 p += sprintf(p, "%X", nibble);
1069 break;
1070
1071 default:
1072 /*
1073 * Dumbly emit a match pattern for all possible matching
1074 * digits. This could be improved in some cases using ranges,
1075 * but it has the advantage of being trivially correct, and is
1076 * often optimal.
1077 */
1078 *p++ = '[';
1079 for (i = 0; i < 0x10; i++)
1080 if ((i & mask) == nibble)
1081 p += sprintf(p, "%X", i);
1082 *p++ = ']';
1083 }
1084
1085 /* Ensure that the string remains NUL-terminated: */
1086 *p = '\0';
1087
1088 /* Advance the caller's end-of-string pointer: */
1089 *outp = p;
1090}
1091
1092/*
1093 * looks like: "amba:dN"
1094 *
1095 * N is exactly 8 digits, where each is an upper-case hex digit, or
1096 * a ? or [] pattern matching exactly one digit.
1097 */
1098static int do_amba_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001099 void *symval, char *alias)
Dave Martin523817b2011-10-05 14:44:57 +01001100{
1101 unsigned int digit;
1102 char *p = alias;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001103 DEF_FIELD(symval, amba_id, id);
1104 DEF_FIELD(symval, amba_id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001105
Andreas Schwab6543bec2013-01-20 17:58:47 +01001106 if ((id & mask) != id)
Dave Martin523817b2011-10-05 14:44:57 +01001107 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1108 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001109 filename, id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001110
1111 p += sprintf(alias, "amba:d");
1112 for (digit = 0; digit < 8; digit++)
1113 append_nibble_mask(&p,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001114 (id >> (4 * (7 - digit))) & 0xf,
1115 (mask >> (4 * (7 - digit))) & 0xf);
Dave Martin523817b2011-10-05 14:44:57 +01001116
1117 return 1;
1118}
1119
James Hogan8286ae02015-03-25 15:39:50 +00001120/*
1121 * looks like: "mipscdmm:tN"
1122 *
1123 * N is exactly 2 digits, where each is an upper-case hex digit, or
1124 * a ? or [] pattern matching exactly one digit.
1125 */
1126static int do_mips_cdmm_entry(const char *filename,
1127 void *symval, char *alias)
1128{
1129 DEF_FIELD(symval, mips_cdmm_device_id, type);
1130
1131 sprintf(alias, "mipscdmm:t%02X*", type);
1132 return 1;
1133}
James Hogan8286ae02015-03-25 15:39:50 +00001134
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001135/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
Andi Kleen644e9cb2012-01-26 00:09:05 +01001136 * All fields are numbers. It would be nicer to use strings for vendor
1137 * and feature, but getting those out of the build system here is too
1138 * complicated.
1139 */
1140
Andreas Schwab6543bec2013-01-20 17:58:47 +01001141static int do_x86cpu_entry(const char *filename, void *symval,
Andi Kleen644e9cb2012-01-26 00:09:05 +01001142 char *alias)
1143{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001144 DEF_FIELD(symval, x86_cpu_id, feature);
1145 DEF_FIELD(symval, x86_cpu_id, family);
1146 DEF_FIELD(symval, x86_cpu_id, model);
1147 DEF_FIELD(symval, x86_cpu_id, vendor);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001148
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001149 strcpy(alias, "cpu:type:x86,");
1150 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1151 ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1152 ADD(alias, "mod", model != X86_MODEL_ANY, model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001153 strcat(alias, ":feature:*");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001154 if (feature != X86_FEATURE_ANY)
1155 sprintf(alias + strlen(alias), "%04X*", feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001156 return 1;
1157}
Andi Kleen644e9cb2012-01-26 00:09:05 +01001158
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001159/* LOOKS like cpu:type:*:feature:*FEAT* */
1160static int do_cpu_entry(const char *filename, void *symval, char *alias)
1161{
1162 DEF_FIELD(symval, cpu_feature, feature);
1163
1164 sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1165 return 1;
1166}
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001167
Tomas Winklerb26864c2015-09-10 10:18:01 +03001168/* Looks like: mei:S:uuid:N:* */
Samuel Ortize5354102013-03-27 17:29:53 +02001169static int do_mei_entry(const char *filename, void *symval,
1170 char *alias)
1171{
1172 DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001173 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001174 DEF_FIELD(symval, mei_cl_device_id, version);
Samuel Ortize5354102013-03-27 17:29:53 +02001175
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001176 sprintf(alias, MEI_CL_MODULE_PREFIX);
1177 sprintf(alias + strlen(alias), "%s:", (*name)[0] ? *name : "*");
1178 add_uuid(alias, *uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001179 ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001180
1181 strcat(alias, ":*");
Samuel Ortize5354102013-03-27 17:29:53 +02001182
1183 return 1;
1184}
Samuel Ortize5354102013-03-27 17:29:53 +02001185
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001186/* Looks like: rapidio:vNdNavNadN */
1187static int do_rio_entry(const char *filename,
1188 void *symval, char *alias)
1189{
1190 DEF_FIELD(symval, rio_device_id, did);
1191 DEF_FIELD(symval, rio_device_id, vid);
1192 DEF_FIELD(symval, rio_device_id, asm_did);
1193 DEF_FIELD(symval, rio_device_id, asm_vid);
1194
1195 strcpy(alias, "rapidio:");
1196 ADD(alias, "v", vid != RIO_ANY_ID, vid);
1197 ADD(alias, "d", did != RIO_ANY_ID, did);
1198 ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1199 ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1200
1201 add_wildcard(alias);
1202 return 1;
1203}
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001204
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001205/* Looks like: ulpi:vNpN */
1206static int do_ulpi_entry(const char *filename, void *symval,
1207 char *alias)
1208{
1209 DEF_FIELD(symval, ulpi_device_id, vendor);
1210 DEF_FIELD(symval, ulpi_device_id, product);
1211
1212 sprintf(alias, "ulpi:v%04xp%04x", vendor, product);
1213
1214 return 1;
1215}
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001216
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301217/* Looks like: hdaudio:vNrNaN */
1218static int do_hda_entry(const char *filename, void *symval, char *alias)
1219{
1220 DEF_FIELD(symval, hda_device_id, vendor_id);
1221 DEF_FIELD(symval, hda_device_id, rev_id);
1222 DEF_FIELD(symval, hda_device_id, api_version);
1223
1224 strcpy(alias, "hdaudio:");
1225 ADD(alias, "v", vendor_id != 0, vendor_id);
1226 ADD(alias, "r", rev_id != 0, rev_id);
1227 ADD(alias, "a", api_version != 0, api_version);
1228
1229 add_wildcard(alias);
1230 return 1;
1231}
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301232
Vinod Koul92513452017-12-14 11:19:33 +05301233/* Looks like: sdw:mNpN */
1234static int do_sdw_entry(const char *filename, void *symval, char *alias)
1235{
1236 DEF_FIELD(symval, sdw_device_id, mfg_id);
1237 DEF_FIELD(symval, sdw_device_id, part_id);
1238
1239 strcpy(alias, "sdw:");
1240 ADD(alias, "m", mfg_id != 0, mfg_id);
1241 ADD(alias, "p", part_id != 0, part_id);
1242
1243 add_wildcard(alias);
1244 return 1;
1245}
Vinod Koul92513452017-12-14 11:19:33 +05301246
Stuart Yoder0afef452016-06-22 16:40:45 -05001247/* Looks like: fsl-mc:vNdN */
1248static int do_fsl_mc_entry(const char *filename, void *symval,
1249 char *alias)
1250{
1251 DEF_FIELD(symval, fsl_mc_device_id, vendor);
1252 DEF_FIELD_ADDR(symval, fsl_mc_device_id, obj_type);
1253
1254 sprintf(alias, "fsl-mc:v%08Xd%s", vendor, *obj_type);
1255 return 1;
1256}
Stuart Yoder0afef452016-06-22 16:40:45 -05001257
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001258/* Looks like: tbsvc:kSpNvNrN */
1259static int do_tbsvc_entry(const char *filename, void *symval, char *alias)
1260{
1261 DEF_FIELD(symval, tb_service_id, match_flags);
1262 DEF_FIELD_ADDR(symval, tb_service_id, protocol_key);
1263 DEF_FIELD(symval, tb_service_id, protocol_id);
1264 DEF_FIELD(symval, tb_service_id, protocol_version);
1265 DEF_FIELD(symval, tb_service_id, protocol_revision);
1266
1267 strcpy(alias, "tbsvc:");
1268 if (match_flags & TBSVC_MATCH_PROTOCOL_KEY)
1269 sprintf(alias + strlen(alias), "k%s", *protocol_key);
1270 else
1271 strcat(alias + strlen(alias), "k*");
1272 ADD(alias, "p", match_flags & TBSVC_MATCH_PROTOCOL_ID, protocol_id);
1273 ADD(alias, "v", match_flags & TBSVC_MATCH_PROTOCOL_VERSION,
1274 protocol_version);
1275 ADD(alias, "r", match_flags & TBSVC_MATCH_PROTOCOL_REVISION,
1276 protocol_revision);
1277
1278 add_wildcard(alias);
1279 return 1;
1280}
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001281
Heikki Krogerus8a37d872018-06-27 18:19:50 +03001282/* Looks like: typec:idNmN */
1283static int do_typec_entry(const char *filename, void *symval, char *alias)
1284{
1285 DEF_FIELD(symval, typec_device_id, svid);
1286 DEF_FIELD(symval, typec_device_id, mode);
1287
1288 sprintf(alias, "typec:id%04X", svid);
1289 ADD(alias, "m", mode != TYPEC_ANY_MODE, mode);
1290
1291 return 1;
1292}
Heikki Krogerus8a37d872018-06-27 18:19:50 +03001293
Rusty Russell626596e2012-01-13 09:32:15 +10301294/* Does namelen bytes of name exactly match the symbol? */
1295static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
Rusty Russell626596e2012-01-13 09:32:15 +10301297 if (namelen != strlen(symbol))
1298 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Rusty Russell626596e2012-01-13 09:32:15 +10301300 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301}
1302
1303static void do_table(void *symval, unsigned long size,
1304 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001305 const char *device_id,
Masahiro Yamadaf880eea2018-11-22 13:28:42 +09001306 int (*do_entry)(const char *filename, void *symval, char *alias),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct module *mod)
1308{
1309 unsigned int i;
Mattias Jacobsson841f1b82019-02-07 13:30:22 +01001310 char alias[ALIAS_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Kees Cooke0049822007-09-16 11:15:46 +02001312 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 /* Leave last one: it's the terminator. */
1314 size -= id_size;
1315
1316 for (i = 0; i < size; i += id_size) {
1317 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 buf_printf(&mod->dev_table_buf,
1319 "MODULE_ALIAS(\"%s\");\n", alias);
1320 }
1321 }
1322}
1323
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001324static const struct devtable devtable[] = {
1325 {"hid", SIZE_hid_device_id, do_hid_entry},
1326 {"ieee1394", SIZE_ieee1394_device_id, do_ieee1394_entry},
1327 {"pci", SIZE_pci_device_id, do_pci_entry},
1328 {"ccw", SIZE_ccw_device_id, do_ccw_entry},
1329 {"ap", SIZE_ap_device_id, do_ap_entry},
1330 {"css", SIZE_css_device_id, do_css_entry},
1331 {"serio", SIZE_serio_device_id, do_serio_entry},
1332 {"acpi", SIZE_acpi_device_id, do_acpi_entry},
1333 {"pcmcia", SIZE_pcmcia_device_id, do_pcmcia_entry},
1334 {"vio", SIZE_vio_device_id, do_vio_entry},
1335 {"input", SIZE_input_device_id, do_input_entry},
1336 {"eisa", SIZE_eisa_device_id, do_eisa_entry},
1337 {"parisc", SIZE_parisc_device_id, do_parisc_entry},
1338 {"sdio", SIZE_sdio_device_id, do_sdio_entry},
1339 {"ssb", SIZE_ssb_device_id, do_ssb_entry},
1340 {"bcma", SIZE_bcma_device_id, do_bcma_entry},
1341 {"virtio", SIZE_virtio_device_id, do_virtio_entry},
1342 {"vmbus", SIZE_hv_vmbus_device_id, do_vmbus_entry},
1343 {"rpmsg", SIZE_rpmsg_device_id, do_rpmsg_entry},
1344 {"i2c", SIZE_i2c_device_id, do_i2c_entry},
1345 {"spi", SIZE_spi_device_id, do_spi_entry},
1346 {"dmi", SIZE_dmi_system_id, do_dmi_entry},
1347 {"platform", SIZE_platform_device_id, do_platform_entry},
1348 {"mdio", SIZE_mdio_device_id, do_mdio_entry},
1349 {"zorro", SIZE_zorro_device_id, do_zorro_entry},
1350 {"isapnp", SIZE_isapnp_device_id, do_isapnp_entry},
1351 {"ipack", SIZE_ipack_device_id, do_ipack_entry},
1352 {"amba", SIZE_amba_id, do_amba_entry},
1353 {"mipscdmm", SIZE_mips_cdmm_device_id, do_mips_cdmm_entry},
1354 {"x86cpu", SIZE_x86_cpu_id, do_x86cpu_entry},
1355 {"cpu", SIZE_cpu_feature, do_cpu_entry},
1356 {"mei", SIZE_mei_cl_device_id, do_mei_entry},
1357 {"rapidio", SIZE_rio_device_id, do_rio_entry},
1358 {"ulpi", SIZE_ulpi_device_id, do_ulpi_entry},
1359 {"hdaudio", SIZE_hda_device_id, do_hda_entry},
1360 {"sdw", SIZE_sdw_device_id, do_sdw_entry},
1361 {"fslmc", SIZE_fsl_mc_device_id, do_fsl_mc_entry},
1362 {"tbsvc", SIZE_tb_service_id, do_tbsvc_entry},
1363 {"typec", SIZE_typec_device_id, do_typec_entry},
1364};
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366/* Create MODULE_ALIAS() statements.
1367 * At this time, we cannot write the actual output C source yet,
1368 * so we write into the mod->dev_table_buf buffer. */
1369void handle_moddevtable(struct module *mod, struct elf_info *info,
1370 Elf_Sym *sym, const char *symname)
1371{
1372 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001373 char *zeros = NULL;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301374 const char *name, *identifier;
Rusty Russell626596e2012-01-13 09:32:15 +10301375 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001378 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return;
1380
David Millere88aa7b2012-04-12 14:37:30 -04001381 /* We're looking for an object */
1382 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1383 return;
1384
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001385 /* All our symbols are of form __mod_<name>__<identifier>_device_table. */
1386 if (strncmp(symname, "__mod_", strlen("__mod_")))
Rusty Russell626596e2012-01-13 09:32:15 +10301387 return;
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001388 name = symname + strlen("__mod_");
Rusty Russell626596e2012-01-13 09:32:15 +10301389 namelen = strlen(name);
1390 if (namelen < strlen("_device_table"))
1391 return;
1392 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1393 return;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301394 identifier = strstr(name, "__");
1395 if (!identifier)
1396 return;
1397 namelen = identifier - name;
Rusty Russell626596e2012-01-13 09:32:15 +10301398
Kees Cooke0049822007-09-16 11:15:46 +02001399 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001400 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001401 zeros = calloc(1, sym->st_size);
1402 symval = zeros;
1403 } else {
1404 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001405 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001406 + sym->st_value;
1407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Rusty Russell626596e2012-01-13 09:32:15 +10301409 /* First handle the "special" cases */
1410 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001411 do_usb_table(symval, sym->st_size, mod);
Philipp Zabelacbef7b2016-05-05 16:22:29 -07001412 if (sym_is(name, namelen, "of"))
1413 do_of_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301414 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001415 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301416 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001417 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301418 else {
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001419 int i;
Rusty Russell626596e2012-01-13 09:32:15 +10301420
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001421 for (i = 0; i < ARRAY_SIZE(devtable); i++) {
1422 const struct devtable *p = &devtable[i];
1423
1424 if (sym_is(name, namelen, p->device_id)) {
1425 do_table(symval, sym->st_size, p->id_size,
Masahiro Yamadaf880eea2018-11-22 13:28:42 +09001426 p->device_id, p->do_entry, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301427 break;
1428 }
1429 }
1430 }
Kees Cooke0049822007-09-16 11:15:46 +02001431 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
1434/* Now add out buffered information to the generated C source */
1435void add_moddevtable(struct buffer *buf, struct module *mod)
1436{
1437 buf_printf(buf, "\n");
1438 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1439 free(mod->dev_table_buf.p);
1440}