blob: d0e41723627f96cd79ff2975b1cd1b7aba4e4f3c [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;
Sumit Garg0fc1db92019-01-29 11:19:35 +053040typedef struct {
41 __u8 b[16];
42} uuid_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* Big exception to the "don't include kernel headers into userspace, which
Sam Ravnborg62070fa2006-03-03 16:46:04 +010045 * even potentially has different endianness and word sizes, since
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * we handle those differences explicitly below */
47#include "../../include/linux/mod_devicetable.h"
48
Rusty Russelle49ce142012-01-13 09:32:16 +103049/* This array collects all instances that use the generic do_table */
50struct devtable {
Tom Gundersen21bdd172014-02-03 11:14:13 +103051 const char *device_id; /* name of table, __mod_<name>__*_device_table. */
Rusty Russelle49ce142012-01-13 09:32:16 +103052 unsigned long id_size;
Masahiro Yamadaf880eea2018-11-22 13:28:42 +090053 int (*do_entry)(const char *filename, void *symval, char *alias);
Rusty Russelle49ce142012-01-13 09:32:16 +103054};
55
Andreas Schwab6543bec2013-01-20 17:58:47 +010056/* Define a variable f that holds the value of field f of struct devid
57 * based at address m.
58 */
59#define DEF_FIELD(m, devid, f) \
60 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
Leonardo Brasc2b1a922018-10-24 01:03:52 -030061
62/* Define a variable v that holds the address of field f of struct devid
63 * based at address m. Due to the way typeof works, for a field of type
64 * T[N] the variable has type T(*)[N], _not_ T*.
65 */
66#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
67 typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
68
Andreas Schwab6543bec2013-01-20 17:58:47 +010069/* Define a variable f that holds the address of field f of struct devid
70 * based at address m. Due to the way typeof works, for a field of type
71 * T[N] the variable has type T(*)[N], _not_ T*.
72 */
73#define DEF_FIELD_ADDR(m, devid, f) \
Leonardo Brasc2b1a922018-10-24 01:03:52 -030074 DEF_FIELD_ADDR_VAR(m, devid, f, f)
Andreas Schwab6543bec2013-01-20 17:58:47 +010075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#define ADD(str, sep, cond, field) \
77do { \
78 strcat(str, sep); \
79 if (cond) \
80 sprintf(str + strlen(str), \
81 sizeof(field) == 1 ? "%02X" : \
82 sizeof(field) == 2 ? "%04X" : \
83 sizeof(field) == 4 ? "%08X" : "", \
84 field); \
85 else \
86 sprintf(str + strlen(str), "*"); \
87} while(0)
88
Javier Martinez Canillas2f632362016-01-14 15:17:02 -080089/* End in a wildcard, for future extension */
Jean Delvareac551822008-05-02 20:37:21 +020090static inline void add_wildcard(char *str)
91{
92 int len = strlen(str);
93
94 if (str[len - 1] != '*')
95 strcat(str + len, "*");
96}
97
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -070098static inline void add_uuid(char *str, uuid_le uuid)
Tomas Winklerc93b76b2015-05-07 15:54:02 +030099{
100 int len = strlen(str);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300101
Prarit Bhargava59796ed2015-09-10 10:17:59 +0300102 sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
103 uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
104 uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
105 uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
106 uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300107}
108
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200109/**
110 * Check that sizeof(device_id type) are consistent with size of section
111 * in .o file. If in-consistent then userspace and kernel does not agree
112 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200113 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100114 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200115 **/
Kees Cooke0049822007-09-16 11:15:46 +0200116static void device_id_check(const char *modname, const char *device_id,
117 unsigned long size, unsigned long id_size,
118 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200119{
Kees Cooke0049822007-09-16 11:15:46 +0200120 int i;
121
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200122 if (size % id_size || size < id_size) {
123 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
Tom Gundersen21bdd172014-02-03 11:14:13 +1030124 "of the size of "
125 "section __mod_%s__<identifier>_device_table=%lu.\n"
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200126 "Fix definition of struct %s_device_id "
127 "in mod_devicetable.h\n",
128 modname, device_id, id_size, device_id, size, device_id);
129 }
Kees Cooke0049822007-09-16 11:15:46 +0200130 /* Verify last one is a terminator */
131 for (i = 0; i < id_size; i++ ) {
132 if (*(uint8_t*)(symval+size-id_size+i)) {
133 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
134 "The last of %lu is:\n",
135 modname, device_id, id_size, size / id_size);
136 for (i = 0; i < id_size; i++ )
137 fprintf(stderr,"0x%02x ",
138 *(uint8_t*)(symval+size-id_size+i) );
139 fprintf(stderr,"\n");
140 fatal("%s: struct %s_device_id is not terminated "
141 "with a NULL entry!\n", modname, device_id);
142 }
143 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200144}
145
Roman Kaganb19dcd92005-04-22 15:07:01 -0700146/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200147/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100148static void do_usb_entry(void *symval,
Roman Kaganb19dcd92005-04-22 15:07:01 -0700149 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
150 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500151 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700153 char alias[500];
Andreas Schwab6543bec2013-01-20 17:58:47 +0100154 DEF_FIELD(symval, usb_device_id, match_flags);
155 DEF_FIELD(symval, usb_device_id, idVendor);
156 DEF_FIELD(symval, usb_device_id, idProduct);
157 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
158 DEF_FIELD(symval, usb_device_id, bDeviceClass);
159 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
160 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
161 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
162 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
163 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
164 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 strcpy(alias, "usb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100167 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
168 idVendor);
169 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
170 idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700171
172 strcat(alias, "d");
173 if (bcdDevice_initial_digits)
174 sprintf(alias + strlen(alias), "%0*X",
175 bcdDevice_initial_digits, bcdDevice_initial);
176 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500177 sprintf(alias + strlen(alias), "%X", range_lo);
178 else if (range_lo > 0 || range_hi < max) {
179 if (range_lo > 0x9 || range_hi < 0xA)
180 sprintf(alias + strlen(alias),
181 "[%X-%X]",
182 range_lo,
183 range_hi);
184 else {
185 sprintf(alias + strlen(alias),
186 range_lo < 0x9 ? "[%X-9" : "[%X",
187 range_lo);
188 sprintf(alias + strlen(alias),
Jan Moskyto Matejka03b56322014-02-07 19:15:11 +0100189 range_hi > 0xA ? "A-%X]" : "%X]",
190 range_hi);
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500191 }
192 }
Andreas Schwab6543bec2013-01-20 17:58:47 +0100193 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700194 strcat(alias, "*");
195
Andreas Schwab6543bec2013-01-20 17:58:47 +0100196 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
197 bDeviceClass);
198 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
199 bDeviceSubClass);
200 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
201 bDeviceProtocol);
202 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
203 bInterfaceClass);
204 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
205 bInterfaceSubClass);
206 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
207 bInterfaceProtocol);
208 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
209 bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700210
Jean Delvareac551822008-05-02 20:37:21 +0200211 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700212 buf_printf(&mod->dev_table_buf,
213 "MODULE_ALIAS(\"%s\");\n", alias);
214}
215
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500216/* Handles increment/decrement of BCD formatted integers */
217/* Returns the previous value, so it works like i++ or i-- */
218static unsigned int incbcd(unsigned int *bcd,
219 int inc,
220 unsigned char max,
221 size_t chars)
222{
223 unsigned int init = *bcd, i, j;
224 unsigned long long c, dec = 0;
225
226 /* If bcd is not in BCD format, just increment */
227 if (max > 0x9) {
228 *bcd += inc;
229 return init;
230 }
231
232 /* Convert BCD to Decimal */
233 for (i=0 ; i < chars ; i++) {
234 c = (*bcd >> (i << 2)) & 0xf;
235 c = c > 9 ? 9 : c; /* force to bcd just in case */
236 for (j=0 ; j < i ; j++)
237 c = c * 10;
238 dec += c;
239 }
240
241 /* Do our increment/decrement */
242 dec += inc;
243 *bcd = 0;
244
245 /* Convert back to BCD */
246 for (i=0 ; i < chars ; i++) {
247 for (c=1,j=0 ; j < i ; j++)
248 c = c * 10;
249 c = (dec / c) % 10;
250 *bcd += c << (i << 2);
251 }
252 return init;
253}
254
Andreas Schwab6543bec2013-01-20 17:58:47 +0100255static void do_usb_entry_multi(void *symval, struct module *mod)
Roman Kaganb19dcd92005-04-22 15:07:01 -0700256{
257 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500258 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700259 int ndigits;
260
Andreas Schwab6543bec2013-01-20 17:58:47 +0100261 DEF_FIELD(symval, usb_device_id, match_flags);
262 DEF_FIELD(symval, usb_device_id, idVendor);
263 DEF_FIELD(symval, usb_device_id, idProduct);
264 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
265 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
266 DEF_FIELD(symval, usb_device_id, bDeviceClass);
267 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700268
Andreas Schwab6543bec2013-01-20 17:58:47 +0100269 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
270 bcdDevice_lo : 0x0U;
271 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
272 bcdDevice_hi : ~0x0U;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700273
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500274 /* Figure out if this entry is in bcd or hex format */
275 max = 0x9; /* Default to decimal format */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100276 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500277 clo = (devlo >> (ndigits << 2)) & 0xf;
278 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
279 if (clo > max || chi > max) {
280 max = 0xf;
281 break;
282 }
283 }
284
Roman Kaganb19dcd92005-04-22 15:07:01 -0700285 /*
286 * Some modules (visor) have empty slots as placeholder for
287 * run-time specification that results in catch-all alias
288 */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100289 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700290 return;
291
292 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100293 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
Roman Kaganb19dcd92005-04-22 15:07:01 -0700294 clo = devlo & 0xf;
295 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500296 if (chi > max) /* If we are in bcd mode, truncate if necessary */
297 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700298 devlo >>= 4;
299 devhi >>= 4;
300
301 if (devlo == devhi || !ndigits) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100302 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700303 break;
304 }
305
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500306 if (clo > 0x0)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100307 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500308 incbcd(&devlo, 1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100309 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500310 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700311
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500312 if (chi < max)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100313 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500314 incbcd(&devhi, -1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100315 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500316 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700317 }
318}
319
320static void do_usb_table(void *symval, unsigned long size,
321 struct module *mod)
322{
323 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100324 const unsigned long id_size = SIZE_usb_device_id;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700325
Kees Cooke0049822007-09-16 11:15:46 +0200326 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200327
Roman Kaganb19dcd92005-04-22 15:07:01 -0700328 /* Leave last one: it's the terminator. */
329 size -= id_size;
330
331 for (i = 0; i < size; i += id_size)
332 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700335static void do_of_entry_multi(void *symval, struct module *mod)
336{
337 char alias[500];
338 int len;
339 char *tmp;
340
341 DEF_FIELD_ADDR(symval, of_device_id, name);
342 DEF_FIELD_ADDR(symval, of_device_id, type);
343 DEF_FIELD_ADDR(symval, of_device_id, compatible);
344
345 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
346 (*type)[0] ? *type : "*");
347
Wolfram Sangb3c0a4d2016-06-06 18:48:38 +0200348 if ((*compatible)[0])
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700349 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
350 *compatible);
351
352 /* Replace all whitespace with underscores */
353 for (tmp = alias; tmp && *tmp; tmp++)
354 if (isspace(*tmp))
355 *tmp = '_';
356
357 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
358 strcat(alias, "C");
359 add_wildcard(alias);
360 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
361}
362
363static void do_of_table(void *symval, unsigned long size,
364 struct module *mod)
365{
366 unsigned int i;
367 const unsigned long id_size = SIZE_of_device_id;
368
369 device_id_check(mod->name, "of", size, id_size, symval);
370
371 /* Leave last one: it's the terminator. */
372 size -= id_size;
373
374 for (i = 0; i < size; i += id_size)
375 do_of_entry_multi(symval + i, mod);
376}
377
Jiri Slabye8c84f92008-05-19 15:50:01 +0200378/* Looks like: hid:bNvNpN */
379static int do_hid_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100380 void *symval, char *alias)
Jiri Slabye8c84f92008-05-19 15:50:01 +0200381{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100382 DEF_FIELD(symval, hid_device_id, bus);
383 DEF_FIELD(symval, hid_device_id, group);
384 DEF_FIELD(symval, hid_device_id, vendor);
385 DEF_FIELD(symval, hid_device_id, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200386
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200387 sprintf(alias, "hid:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100388 ADD(alias, "b", bus != HID_BUS_ANY, bus);
389 ADD(alias, "g", group != HID_GROUP_ANY, group);
390 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
391 ADD(alias, "p", product != HID_ANY_ID, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200392
393 return 1;
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/* Looks like: ieee1394:venNmoNspNverN */
397static int do_ieee1394_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100398 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100400 DEF_FIELD(symval, ieee1394_device_id, match_flags);
401 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
402 DEF_FIELD(symval, ieee1394_device_id, model_id);
403 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
404 DEF_FIELD(symval, ieee1394_device_id, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 strcpy(alias, "ieee1394:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100407 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
408 vendor_id);
409 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
410 model_id);
411 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
412 specifier_id);
413 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
414 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Jean Delvareac551822008-05-02 20:37:21 +0200416 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return 1;
418}
419
420/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
421static int do_pci_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100422 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 /* Class field can be divided into these three. */
425 unsigned char baseclass, subclass, interface,
426 baseclass_mask, subclass_mask, interface_mask;
427
Andreas Schwab6543bec2013-01-20 17:58:47 +0100428 DEF_FIELD(symval, pci_device_id, vendor);
429 DEF_FIELD(symval, pci_device_id, device);
430 DEF_FIELD(symval, pci_device_id, subvendor);
431 DEF_FIELD(symval, pci_device_id, subdevice);
432 DEF_FIELD(symval, pci_device_id, class);
433 DEF_FIELD(symval, pci_device_id, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 strcpy(alias, "pci:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100436 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
437 ADD(alias, "d", device != PCI_ANY_ID, device);
438 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
439 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Andreas Schwab6543bec2013-01-20 17:58:47 +0100441 baseclass = (class) >> 16;
442 baseclass_mask = (class_mask) >> 16;
443 subclass = (class) >> 8;
444 subclass_mask = (class_mask) >> 8;
445 interface = class;
446 interface_mask = class_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
449 || (subclass_mask != 0 && subclass_mask != 0xFF)
450 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100451 warn("Can't handle masks in %s:%04X\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100452 filename, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 0;
454 }
455
456 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
457 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
458 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200459 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 1;
461}
462
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100463/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static int do_ccw_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100465 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100467 DEF_FIELD(symval, ccw_device_id, match_flags);
468 DEF_FIELD(symval, ccw_device_id, cu_type);
469 DEF_FIELD(symval, ccw_device_id, cu_model);
470 DEF_FIELD(symval, ccw_device_id, dev_type);
471 DEF_FIELD(symval, ccw_device_id, dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 strcpy(alias, "ccw:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100474 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
475 cu_type);
476 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
477 cu_model);
478 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
479 dev_type);
480 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
481 dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200482 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 1;
484}
485
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200486/* looks like: "ap:tN" */
487static int do_ap_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100488 void *symval, char *alias)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200489{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100490 DEF_FIELD(symval, ap_device_id, dev_type);
491
492 sprintf(alias, "ap:t%02X*", dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200493 return 1;
494}
495
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200496/* looks like: "css:tN" */
497static int do_css_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100498 void *symval, char *alias)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200499{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100500 DEF_FIELD(symval, css_device_id, type);
501
502 sprintf(alias, "css:t%01X", type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200503 return 1;
504}
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/* Looks like: "serio:tyNprNidNexN" */
507static int do_serio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100508 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100510 DEF_FIELD(symval, serio_device_id, type);
511 DEF_FIELD(symval, serio_device_id, proto);
512 DEF_FIELD(symval, serio_device_id, id);
513 DEF_FIELD(symval, serio_device_id, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 strcpy(alias, "serio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100516 ADD(alias, "ty", type != SERIO_ANY, type);
517 ADD(alias, "pr", proto != SERIO_ANY, proto);
518 ADD(alias, "id", id != SERIO_ANY, id);
519 ADD(alias, "ex", extra != SERIO_ANY, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jean Delvareac551822008-05-02 20:37:21 +0200521 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 1;
523}
524
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200525/* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
526 * "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
527 *
528 * NOTE: Each driver should use one of the following : _HID, _CIDs
529 * or _CLS. Also, bb, ss, and pp can be substituted with ??
530 * as don't care byte.
531 */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200532static int do_acpi_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100533 void *symval, char *alias)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200534{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100535 DEF_FIELD_ADDR(symval, acpi_device_id, id);
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200536 DEF_FIELD_ADDR(symval, acpi_device_id, cls);
537 DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk);
538
539 if (id && strlen((const char *)*id))
540 sprintf(alias, "acpi*:%s:*", *id);
541 else if (cls) {
542 int i, byte_shift, cnt = 0;
543 unsigned int msk;
544
545 sprintf(&alias[cnt], "acpi*:");
546 cnt = 6;
547 for (i = 1; i <= 3; i++) {
548 byte_shift = 8 * (3-i);
549 msk = (*cls_msk >> byte_shift) & 0xFF;
550 if (msk)
551 sprintf(&alias[cnt], "%02x",
552 (*cls >> byte_shift) & 0xFF);
553 else
554 sprintf(&alias[cnt], "??");
555 cnt += 2;
556 }
557 sprintf(&alias[cnt], ":*");
558 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200559 return 1;
560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200563static void do_pnp_device_entry(void *symval, unsigned long size,
564 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100566 const unsigned long id_size = SIZE_pnp_device_id;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200567 const unsigned int count = (size / id_size)-1;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200568 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200569
570 device_id_check(mod->name, "pnp", size, id_size, symval);
571
Kay Sievers5e4c6562008-08-21 15:28:56 +0200572 for (i = 0; i < count; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100573 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
574 char acpi_id[sizeof(*id)];
Kay Sievers72638f52009-01-08 03:06:42 +0100575 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200576
577 buf_printf(&mod->dev_table_buf,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100578 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
Kay Sievers72638f52009-01-08 03:06:42 +0100579
580 /* fix broken pnp bus lowercasing */
581 for (j = 0; j < sizeof(acpi_id); j++)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100582 acpi_id[j] = toupper((*id)[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200583 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100584 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Kay Sievers0c81eed2008-02-21 00:35:54 +0100588/* looks like: "pnp:dD" for every device of the card */
589static void do_pnp_card_entries(void *symval, unsigned long size,
590 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100592 const unsigned long id_size = SIZE_pnp_card_device_id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100593 const unsigned int count = (size / id_size)-1;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100594 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Kay Sievers0c81eed2008-02-21 00:35:54 +0100596 device_id_check(mod->name, "pnp", size, id_size, symval);
597
598 for (i = 0; i < count; i++) {
599 unsigned int j;
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300600 DEF_FIELD_ADDR(symval + i * id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100601
602 for (j = 0; j < PNP_MAX_DEVICES; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100603 const char *id = (char *)(*devs)[j].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100604 int i2, j2;
605 int dup = 0;
606
607 if (!id[0])
608 break;
609
610 /* find duplicate, already added value */
611 for (i2 = 0; i2 < i && !dup; i2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300612 DEF_FIELD_ADDR_VAR(symval + i2 * id_size,
613 pnp_card_device_id,
614 devs, devs_dup);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100615
616 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300617 const char *id2 =
618 (char *)(*devs_dup)[j2].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100619
620 if (!id2[0])
621 break;
622
623 if (!strcmp(id, id2)) {
624 dup = 1;
625 break;
626 }
627 }
628 }
629
630 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200631 if (!dup) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100632 char acpi_id[PNP_ID_LEN];
Kay Sievers72638f52009-01-08 03:06:42 +0100633 int k;
634
Kay Sievers0c81eed2008-02-21 00:35:54 +0100635 buf_printf(&mod->dev_table_buf,
636 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100637
638 /* fix broken pnp bus lowercasing */
639 for (k = 0; k < sizeof(acpi_id); k++)
640 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200641 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100642 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200643 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700648/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
649static int do_pcmcia_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100650 void *symval, char *alias)
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700651{
652 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100653 DEF_FIELD(symval, pcmcia_device_id, match_flags);
654 DEF_FIELD(symval, pcmcia_device_id, manf_id);
655 DEF_FIELD(symval, pcmcia_device_id, card_id);
656 DEF_FIELD(symval, pcmcia_device_id, func_id);
657 DEF_FIELD(symval, pcmcia_device_id, function);
658 DEF_FIELD(symval, pcmcia_device_id, device_no);
659 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200660
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700661 for (i=0; i<4; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100662 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
663 }
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700664
Andreas Schwab6543bec2013-01-20 17:58:47 +0100665 strcpy(alias, "pcmcia:");
666 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
667 manf_id);
668 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
669 card_id);
670 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
671 func_id);
672 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
673 function);
674 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
675 device_no);
676 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
677 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
678 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
679 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700680
Jean Delvareac551822008-05-02 20:37:21 +0200681 add_wildcard(alias);
Andreas Schwab6543bec2013-01-20 17:58:47 +0100682 return 1;
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700683}
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700684
Andreas Schwab6543bec2013-01-20 17:58:47 +0100685static int do_vio_entry(const char *filename, void *symval,
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000686 char *alias)
687{
688 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100689 DEF_FIELD_ADDR(symval, vio_device_id, type);
690 DEF_FIELD_ADDR(symval, vio_device_id, compat);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000691
Andreas Schwab6543bec2013-01-20 17:58:47 +0100692 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
693 (*compat)[0] ? *compat : "*");
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000694
695 /* Replace all whitespace with underscores */
696 for (tmp = alias; tmp && *tmp; tmp++)
697 if (isspace (*tmp))
698 *tmp = '_';
699
Jean Delvareac551822008-05-02 20:37:21 +0200700 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000701 return 1;
702}
703
Rusty Russell1d8f4302005-12-07 21:40:34 +0100704#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
705
706static void do_input(char *alias,
707 kernel_ulong_t *arr, unsigned int min, unsigned int max)
708{
709 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400710
Andreas Schwab6543bec2013-01-20 17:58:47 +0100711 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
712 arr[i] = TO_NATIVE(arr[i]);
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400713 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200714 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400715 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100716}
717
Dmitry Torokhov09c3e012017-10-22 11:42:29 -0700718/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100719static int do_input_entry(const char *filename, void *symval,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100720 char *alias)
721{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100722 DEF_FIELD(symval, input_device_id, flags);
723 DEF_FIELD(symval, input_device_id, bustype);
724 DEF_FIELD(symval, input_device_id, vendor);
725 DEF_FIELD(symval, input_device_id, product);
726 DEF_FIELD(symval, input_device_id, version);
727 DEF_FIELD_ADDR(symval, input_device_id, evbit);
728 DEF_FIELD_ADDR(symval, input_device_id, keybit);
729 DEF_FIELD_ADDR(symval, input_device_id, relbit);
730 DEF_FIELD_ADDR(symval, input_device_id, absbit);
731 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
732 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
733 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
734 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
735 DEF_FIELD_ADDR(symval, input_device_id, swbit);
736
Rusty Russell1d8f4302005-12-07 21:40:34 +0100737 sprintf(alias, "input:");
738
Andreas Schwab6543bec2013-01-20 17:58:47 +0100739 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
740 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
741 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
742 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100743
744 sprintf(alias + strlen(alias), "-e*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100745 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
746 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100747 sprintf(alias + strlen(alias), "k*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100748 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
749 do_input(alias, *keybit,
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100750 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
751 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100752 sprintf(alias + strlen(alias), "r*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100753 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
754 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100755 sprintf(alias + strlen(alias), "a*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100756 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
757 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100758 sprintf(alias + strlen(alias), "m*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100759 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
760 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100761 sprintf(alias + strlen(alias), "l*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100762 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
763 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100764 sprintf(alias + strlen(alias), "s*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100765 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
766 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100767 sprintf(alias + strlen(alias), "f*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100768 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
769 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100770 sprintf(alias + strlen(alias), "w*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100771 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
772 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100773 return 1;
774}
775
Andreas Schwab6543bec2013-01-20 17:58:47 +0100776static int do_eisa_entry(const char *filename, void *symval,
Michael Tokarev07563c72006-09-27 01:50:56 -0700777 char *alias)
778{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100779 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
780 if (sig[0])
781 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
Jean Delvareac551822008-05-02 20:37:21 +0200782 else
783 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700784 return 1;
785}
786
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500787/* Looks like: parisc:tNhvNrevNsvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100788static int do_parisc_entry(const char *filename, void *symval,
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500789 char *alias)
790{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100791 DEF_FIELD(symval, parisc_device_id, hw_type);
792 DEF_FIELD(symval, parisc_device_id, hversion);
793 DEF_FIELD(symval, parisc_device_id, hversion_rev);
794 DEF_FIELD(symval, parisc_device_id, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500795
796 strcpy(alias, "parisc:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100797 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
798 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
799 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
800 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500801
Jean Delvareac551822008-05-02 20:37:21 +0200802 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500803 return 1;
804}
805
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200806/* Looks like: sdio:cNvNdN. */
807static int do_sdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100808 void *symval, char *alias)
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200809{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100810 DEF_FIELD(symval, sdio_device_id, class);
811 DEF_FIELD(symval, sdio_device_id, vendor);
812 DEF_FIELD(symval, sdio_device_id, device);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200813
814 strcpy(alias, "sdio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100815 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
816 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
817 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
Jean Delvareac551822008-05-02 20:37:21 +0200818 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700819 return 1;
820}
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200821
Michael Buesch61e115a2007-09-18 15:12:50 -0400822/* Looks like: ssb:vNidNrevN. */
823static int do_ssb_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100824 void *symval, char *alias)
Michael Buesch61e115a2007-09-18 15:12:50 -0400825{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100826 DEF_FIELD(symval, ssb_device_id, vendor);
827 DEF_FIELD(symval, ssb_device_id, coreid);
828 DEF_FIELD(symval, ssb_device_id, revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400829
830 strcpy(alias, "ssb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100831 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
832 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
833 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
Jean Delvareac551822008-05-02 20:37:21 +0200834 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200835 return 1;
836}
837
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200838/* Looks like: bcma:mNidNrevNclN. */
839static int do_bcma_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100840 void *symval, char *alias)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200841{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100842 DEF_FIELD(symval, bcma_device_id, manuf);
843 DEF_FIELD(symval, bcma_device_id, id);
844 DEF_FIELD(symval, bcma_device_id, rev);
845 DEF_FIELD(symval, bcma_device_id, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200846
847 strcpy(alias, "bcma:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100848 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
849 ADD(alias, "id", id != BCMA_ANY_ID, id);
850 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
851 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200852 add_wildcard(alias);
853 return 1;
854}
855
Rusty Russellb01d9f22007-10-22 11:03:39 +1000856/* Looks like: virtio:dNvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100857static int do_virtio_entry(const char *filename, void *symval,
Rusty Russellb01d9f22007-10-22 11:03:39 +1000858 char *alias)
859{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100860 DEF_FIELD(symval, virtio_device_id, device);
861 DEF_FIELD(symval, virtio_device_id, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000862
863 strcpy(alias, "virtio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100864 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
865 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000866
Jean Delvareac551822008-05-02 20:37:21 +0200867 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000868 return 1;
869}
870
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700871/*
872 * Looks like: vmbus:guid
873 * Each byte of the guid will be represented by two hex characters
874 * in the name.
875 */
876
Andreas Schwab6543bec2013-01-20 17:58:47 +0100877static int do_vmbus_entry(const char *filename, void *symval,
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700878 char *alias)
879{
880 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100881 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
882 char guid_name[(sizeof(*guid) + 1) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700883
Andreas Schwab6543bec2013-01-20 17:58:47 +0100884 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
K. Y. Srinivasanaf3ff642015-12-14 16:01:43 -0800885 sprintf(&guid_name[i], "%02x", TO_NATIVE((guid->b)[i/2]));
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700886
887 strcpy(alias, "vmbus:");
888 strcat(alias, guid_name);
889
890 return 1;
891}
892
Andrew F. Davis5b7d1272018-04-21 18:55:29 -0500893/* Looks like: rpmsg:S */
894static int do_rpmsg_entry(const char *filename, void *symval,
895 char *alias)
896{
897 DEF_FIELD_ADDR(symval, rpmsg_device_id, name);
898 sprintf(alias, RPMSG_DEVICE_MODALIAS_FMT, *name);
899
900 return 1;
901}
Andrew F. Davis5b7d1272018-04-21 18:55:29 -0500902
Jean Delvared2653e92008-04-29 23:11:39 +0200903/* Looks like: i2c:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100904static int do_i2c_entry(const char *filename, void *symval,
Jean Delvared2653e92008-04-29 23:11:39 +0200905 char *alias)
906{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100907 DEF_FIELD_ADDR(symval, i2c_device_id, name);
908 sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
Jean Delvared2653e92008-04-29 23:11:39 +0200909
910 return 1;
911}
912
Anton Vorontsove0626e32009-09-22 16:46:08 -0700913/* Looks like: spi:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100914static int do_spi_entry(const char *filename, void *symval,
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700915 char *alias)
916{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100917 DEF_FIELD_ADDR(symval, spi_device_id, name);
918 sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700919
920 return 1;
921}
922
David Woodhoused945b692008-09-16 16:23:28 -0700923static const struct dmifield {
924 const char *prefix;
925 int field;
926} dmi_fields[] = {
927 { "bvn", DMI_BIOS_VENDOR },
928 { "bvr", DMI_BIOS_VERSION },
929 { "bd", DMI_BIOS_DATE },
930 { "svn", DMI_SYS_VENDOR },
931 { "pn", DMI_PRODUCT_NAME },
932 { "pvr", DMI_PRODUCT_VERSION },
933 { "rvn", DMI_BOARD_VENDOR },
934 { "rn", DMI_BOARD_NAME },
935 { "rvr", DMI_BOARD_VERSION },
936 { "cvn", DMI_CHASSIS_VENDOR },
937 { "ct", DMI_CHASSIS_TYPE },
938 { "cvr", DMI_CHASSIS_VERSION },
939 { NULL, DMI_NONE }
940};
941
942static void dmi_ascii_filter(char *d, const char *s)
943{
944 /* Filter out characters we don't want to see in the modalias string */
945 for (; *s; s++)
946 if (*s > ' ' && *s < 127 && *s != ':')
947 *(d++) = *s;
948
949 *d = 0;
950}
951
952
Andreas Schwab6543bec2013-01-20 17:58:47 +0100953static int do_dmi_entry(const char *filename, void *symval,
David Woodhoused945b692008-09-16 16:23:28 -0700954 char *alias)
955{
956 int i, j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100957 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
David Woodhoused945b692008-09-16 16:23:28 -0700958 sprintf(alias, "dmi*");
959
960 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
961 for (j = 0; j < 4; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100962 if ((*matches)[j].slot &&
963 (*matches)[j].slot == dmi_fields[i].field) {
David Woodhoused945b692008-09-16 16:23:28 -0700964 sprintf(alias + strlen(alias), ":%s*",
965 dmi_fields[i].prefix);
966 dmi_ascii_filter(alias + strlen(alias),
Andreas Schwab6543bec2013-01-20 17:58:47 +0100967 (*matches)[j].substr);
David Woodhoused945b692008-09-16 16:23:28 -0700968 strcat(alias, "*");
969 }
970 }
971 }
972
973 strcat(alias, ":");
974 return 1;
975}
Eric Miao57fee4a2009-02-04 11:52:40 +0800976
977static int do_platform_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100978 void *symval, char *alias)
Eric Miao57fee4a2009-02-04 11:52:40 +0800979{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100980 DEF_FIELD_ADDR(symval, platform_device_id, name);
981 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
Eric Miao57fee4a2009-02-04 11:52:40 +0800982 return 1;
983}
984
David Woodhouse8626d3b2010-04-02 01:05:27 +0000985static int do_mdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100986 void *symval, char *alias)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000987{
988 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100989 DEF_FIELD(symval, mdio_device_id, phy_id);
990 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
David Woodhouse8626d3b2010-04-02 01:05:27 +0000991
992 alias += sprintf(alias, MDIO_MODULE_PREFIX);
993
994 for (i = 0; i < 32; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100995 if (!((phy_id_mask >> (31-i)) & 1))
David Woodhouse8626d3b2010-04-02 01:05:27 +0000996 *(alias++) = '?';
Andreas Schwab6543bec2013-01-20 17:58:47 +0100997 else if ((phy_id >> (31-i)) & 1)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000998 *(alias++) = '1';
999 else
1000 *(alias++) = '0';
1001 }
1002
1003 /* Terminate the string */
1004 *alias = 0;
1005
1006 return 1;
1007}
1008
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001009/* Looks like: zorro:iN. */
Andreas Schwab6543bec2013-01-20 17:58:47 +01001010static int do_zorro_entry(const char *filename, void *symval,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001011 char *alias)
1012{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001013 DEF_FIELD(symval, zorro_device_id, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001014 strcpy(alias, "zorro:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001015 ADD(alias, "i", id != ZORRO_WILDCARD, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001016 return 1;
1017}
1018
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001019/* looks like: "pnp:dD" */
1020static int do_isapnp_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001021 void *symval, char *alias)
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001022{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001023 DEF_FIELD(symval, isapnp_device_id, vendor);
1024 DEF_FIELD(symval, isapnp_device_id, function);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001025 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001026 'A' + ((vendor >> 2) & 0x3f) - 1,
1027 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1028 'A' + ((vendor >> 8) & 0x1f) - 1,
1029 (function >> 4) & 0x0f, function & 0x0f,
1030 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001031 return 1;
1032}
1033
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001034/* Looks like: "ipack:fNvNdN". */
1035static int do_ipack_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001036 void *symval, char *alias)
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001037{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001038 DEF_FIELD(symval, ipack_device_id, format);
1039 DEF_FIELD(symval, ipack_device_id, vendor);
1040 DEF_FIELD(symval, ipack_device_id, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001041 strcpy(alias, "ipack:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001042 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1043 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1044 ADD(alias, "d", device != IPACK_ANY_ID, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001045 add_wildcard(alias);
1046 return 1;
1047}
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001048
Dave Martin523817b2011-10-05 14:44:57 +01001049/*
1050 * Append a match expression for a single masked hex digit.
1051 * outp points to a pointer to the character at which to append.
1052 * *outp is updated on return to point just after the appended text,
1053 * to facilitate further appending.
1054 */
1055static void append_nibble_mask(char **outp,
1056 unsigned int nibble, unsigned int mask)
1057{
1058 char *p = *outp;
1059 unsigned int i;
1060
1061 switch (mask) {
1062 case 0:
1063 *p++ = '?';
1064 break;
1065
1066 case 0xf:
1067 p += sprintf(p, "%X", nibble);
1068 break;
1069
1070 default:
1071 /*
1072 * Dumbly emit a match pattern for all possible matching
1073 * digits. This could be improved in some cases using ranges,
1074 * but it has the advantage of being trivially correct, and is
1075 * often optimal.
1076 */
1077 *p++ = '[';
1078 for (i = 0; i < 0x10; i++)
1079 if ((i & mask) == nibble)
1080 p += sprintf(p, "%X", i);
1081 *p++ = ']';
1082 }
1083
1084 /* Ensure that the string remains NUL-terminated: */
1085 *p = '\0';
1086
1087 /* Advance the caller's end-of-string pointer: */
1088 *outp = p;
1089}
1090
1091/*
1092 * looks like: "amba:dN"
1093 *
1094 * N is exactly 8 digits, where each is an upper-case hex digit, or
1095 * a ? or [] pattern matching exactly one digit.
1096 */
1097static int do_amba_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001098 void *symval, char *alias)
Dave Martin523817b2011-10-05 14:44:57 +01001099{
1100 unsigned int digit;
1101 char *p = alias;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001102 DEF_FIELD(symval, amba_id, id);
1103 DEF_FIELD(symval, amba_id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001104
Andreas Schwab6543bec2013-01-20 17:58:47 +01001105 if ((id & mask) != id)
Dave Martin523817b2011-10-05 14:44:57 +01001106 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1107 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001108 filename, id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001109
1110 p += sprintf(alias, "amba:d");
1111 for (digit = 0; digit < 8; digit++)
1112 append_nibble_mask(&p,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001113 (id >> (4 * (7 - digit))) & 0xf,
1114 (mask >> (4 * (7 - digit))) & 0xf);
Dave Martin523817b2011-10-05 14:44:57 +01001115
1116 return 1;
1117}
1118
James Hogan8286ae02015-03-25 15:39:50 +00001119/*
1120 * looks like: "mipscdmm:tN"
1121 *
1122 * N is exactly 2 digits, where each is an upper-case hex digit, or
1123 * a ? or [] pattern matching exactly one digit.
1124 */
1125static int do_mips_cdmm_entry(const char *filename,
1126 void *symval, char *alias)
1127{
1128 DEF_FIELD(symval, mips_cdmm_device_id, type);
1129
1130 sprintf(alias, "mipscdmm:t%02X*", type);
1131 return 1;
1132}
James Hogan8286ae02015-03-25 15:39:50 +00001133
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001134/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
Andi Kleen644e9cb2012-01-26 00:09:05 +01001135 * All fields are numbers. It would be nicer to use strings for vendor
1136 * and feature, but getting those out of the build system here is too
1137 * complicated.
1138 */
1139
Andreas Schwab6543bec2013-01-20 17:58:47 +01001140static int do_x86cpu_entry(const char *filename, void *symval,
Andi Kleen644e9cb2012-01-26 00:09:05 +01001141 char *alias)
1142{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001143 DEF_FIELD(symval, x86_cpu_id, feature);
1144 DEF_FIELD(symval, x86_cpu_id, family);
1145 DEF_FIELD(symval, x86_cpu_id, model);
1146 DEF_FIELD(symval, x86_cpu_id, vendor);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001147
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001148 strcpy(alias, "cpu:type:x86,");
1149 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1150 ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1151 ADD(alias, "mod", model != X86_MODEL_ANY, model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001152 strcat(alias, ":feature:*");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001153 if (feature != X86_FEATURE_ANY)
1154 sprintf(alias + strlen(alias), "%04X*", feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001155 return 1;
1156}
Andi Kleen644e9cb2012-01-26 00:09:05 +01001157
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001158/* LOOKS like cpu:type:*:feature:*FEAT* */
1159static int do_cpu_entry(const char *filename, void *symval, char *alias)
1160{
1161 DEF_FIELD(symval, cpu_feature, feature);
1162
1163 sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1164 return 1;
1165}
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001166
Tomas Winklerb26864c2015-09-10 10:18:01 +03001167/* Looks like: mei:S:uuid:N:* */
Samuel Ortize5354102013-03-27 17:29:53 +02001168static int do_mei_entry(const char *filename, void *symval,
1169 char *alias)
1170{
1171 DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001172 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001173 DEF_FIELD(symval, mei_cl_device_id, version);
Samuel Ortize5354102013-03-27 17:29:53 +02001174
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001175 sprintf(alias, MEI_CL_MODULE_PREFIX);
1176 sprintf(alias + strlen(alias), "%s:", (*name)[0] ? *name : "*");
1177 add_uuid(alias, *uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001178 ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001179
1180 strcat(alias, ":*");
Samuel Ortize5354102013-03-27 17:29:53 +02001181
1182 return 1;
1183}
Samuel Ortize5354102013-03-27 17:29:53 +02001184
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001185/* Looks like: rapidio:vNdNavNadN */
1186static int do_rio_entry(const char *filename,
1187 void *symval, char *alias)
1188{
1189 DEF_FIELD(symval, rio_device_id, did);
1190 DEF_FIELD(symval, rio_device_id, vid);
1191 DEF_FIELD(symval, rio_device_id, asm_did);
1192 DEF_FIELD(symval, rio_device_id, asm_vid);
1193
1194 strcpy(alias, "rapidio:");
1195 ADD(alias, "v", vid != RIO_ANY_ID, vid);
1196 ADD(alias, "d", did != RIO_ANY_ID, did);
1197 ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1198 ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1199
1200 add_wildcard(alias);
1201 return 1;
1202}
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001203
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001204/* Looks like: ulpi:vNpN */
1205static int do_ulpi_entry(const char *filename, void *symval,
1206 char *alias)
1207{
1208 DEF_FIELD(symval, ulpi_device_id, vendor);
1209 DEF_FIELD(symval, ulpi_device_id, product);
1210
1211 sprintf(alias, "ulpi:v%04xp%04x", vendor, product);
1212
1213 return 1;
1214}
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001215
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301216/* Looks like: hdaudio:vNrNaN */
1217static int do_hda_entry(const char *filename, void *symval, char *alias)
1218{
1219 DEF_FIELD(symval, hda_device_id, vendor_id);
1220 DEF_FIELD(symval, hda_device_id, rev_id);
1221 DEF_FIELD(symval, hda_device_id, api_version);
1222
1223 strcpy(alias, "hdaudio:");
1224 ADD(alias, "v", vendor_id != 0, vendor_id);
1225 ADD(alias, "r", rev_id != 0, rev_id);
1226 ADD(alias, "a", api_version != 0, api_version);
1227
1228 add_wildcard(alias);
1229 return 1;
1230}
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301231
Vinod Koul92513452017-12-14 11:19:33 +05301232/* Looks like: sdw:mNpN */
1233static int do_sdw_entry(const char *filename, void *symval, char *alias)
1234{
1235 DEF_FIELD(symval, sdw_device_id, mfg_id);
1236 DEF_FIELD(symval, sdw_device_id, part_id);
1237
1238 strcpy(alias, "sdw:");
1239 ADD(alias, "m", mfg_id != 0, mfg_id);
1240 ADD(alias, "p", part_id != 0, part_id);
1241
1242 add_wildcard(alias);
1243 return 1;
1244}
Vinod Koul92513452017-12-14 11:19:33 +05301245
Stuart Yoder0afef452016-06-22 16:40:45 -05001246/* Looks like: fsl-mc:vNdN */
1247static int do_fsl_mc_entry(const char *filename, void *symval,
1248 char *alias)
1249{
1250 DEF_FIELD(symval, fsl_mc_device_id, vendor);
1251 DEF_FIELD_ADDR(symval, fsl_mc_device_id, obj_type);
1252
1253 sprintf(alias, "fsl-mc:v%08Xd%s", vendor, *obj_type);
1254 return 1;
1255}
Stuart Yoder0afef452016-06-22 16:40:45 -05001256
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001257/* Looks like: tbsvc:kSpNvNrN */
1258static int do_tbsvc_entry(const char *filename, void *symval, char *alias)
1259{
1260 DEF_FIELD(symval, tb_service_id, match_flags);
1261 DEF_FIELD_ADDR(symval, tb_service_id, protocol_key);
1262 DEF_FIELD(symval, tb_service_id, protocol_id);
1263 DEF_FIELD(symval, tb_service_id, protocol_version);
1264 DEF_FIELD(symval, tb_service_id, protocol_revision);
1265
1266 strcpy(alias, "tbsvc:");
1267 if (match_flags & TBSVC_MATCH_PROTOCOL_KEY)
1268 sprintf(alias + strlen(alias), "k%s", *protocol_key);
1269 else
1270 strcat(alias + strlen(alias), "k*");
1271 ADD(alias, "p", match_flags & TBSVC_MATCH_PROTOCOL_ID, protocol_id);
1272 ADD(alias, "v", match_flags & TBSVC_MATCH_PROTOCOL_VERSION,
1273 protocol_version);
1274 ADD(alias, "r", match_flags & TBSVC_MATCH_PROTOCOL_REVISION,
1275 protocol_revision);
1276
1277 add_wildcard(alias);
1278 return 1;
1279}
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001280
Heikki Krogerus8a37d872018-06-27 18:19:50 +03001281/* Looks like: typec:idNmN */
1282static int do_typec_entry(const char *filename, void *symval, char *alias)
1283{
1284 DEF_FIELD(symval, typec_device_id, svid);
1285 DEF_FIELD(symval, typec_device_id, mode);
1286
1287 sprintf(alias, "typec:id%04X", svid);
1288 ADD(alias, "m", mode != TYPEC_ANY_MODE, mode);
1289
1290 return 1;
1291}
Heikki Krogerus8a37d872018-06-27 18:19:50 +03001292
Sumit Garg0fc1db92019-01-29 11:19:35 +05301293/* Looks like: tee:uuid */
1294static int do_tee_entry(const char *filename, void *symval, char *alias)
1295{
1296 DEF_FIELD(symval, tee_client_device_id, uuid);
1297
1298 sprintf(alias, "tee:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1299 uuid.b[0], uuid.b[1], uuid.b[2], uuid.b[3], uuid.b[4],
1300 uuid.b[5], uuid.b[6], uuid.b[7], uuid.b[8], uuid.b[9],
1301 uuid.b[10], uuid.b[11], uuid.b[12], uuid.b[13], uuid.b[14],
1302 uuid.b[15]);
1303
1304 add_wildcard(alias);
1305 return 1;
1306}
1307
Rusty Russell626596e2012-01-13 09:32:15 +10301308/* Does namelen bytes of name exactly match the symbol? */
1309static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Rusty Russell626596e2012-01-13 09:32:15 +10301311 if (namelen != strlen(symbol))
1312 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Rusty Russell626596e2012-01-13 09:32:15 +10301314 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317static void do_table(void *symval, unsigned long size,
1318 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001319 const char *device_id,
Masahiro Yamadaf880eea2018-11-22 13:28:42 +09001320 int (*do_entry)(const char *filename, void *symval, char *alias),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct module *mod)
1322{
1323 unsigned int i;
1324 char alias[500];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Kees Cooke0049822007-09-16 11:15:46 +02001326 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 /* Leave last one: it's the terminator. */
1328 size -= id_size;
1329
1330 for (i = 0; i < size; i += id_size) {
1331 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 buf_printf(&mod->dev_table_buf,
1333 "MODULE_ALIAS(\"%s\");\n", alias);
1334 }
1335 }
1336}
1337
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001338static const struct devtable devtable[] = {
1339 {"hid", SIZE_hid_device_id, do_hid_entry},
1340 {"ieee1394", SIZE_ieee1394_device_id, do_ieee1394_entry},
1341 {"pci", SIZE_pci_device_id, do_pci_entry},
1342 {"ccw", SIZE_ccw_device_id, do_ccw_entry},
1343 {"ap", SIZE_ap_device_id, do_ap_entry},
1344 {"css", SIZE_css_device_id, do_css_entry},
1345 {"serio", SIZE_serio_device_id, do_serio_entry},
1346 {"acpi", SIZE_acpi_device_id, do_acpi_entry},
1347 {"pcmcia", SIZE_pcmcia_device_id, do_pcmcia_entry},
1348 {"vio", SIZE_vio_device_id, do_vio_entry},
1349 {"input", SIZE_input_device_id, do_input_entry},
1350 {"eisa", SIZE_eisa_device_id, do_eisa_entry},
1351 {"parisc", SIZE_parisc_device_id, do_parisc_entry},
1352 {"sdio", SIZE_sdio_device_id, do_sdio_entry},
1353 {"ssb", SIZE_ssb_device_id, do_ssb_entry},
1354 {"bcma", SIZE_bcma_device_id, do_bcma_entry},
1355 {"virtio", SIZE_virtio_device_id, do_virtio_entry},
1356 {"vmbus", SIZE_hv_vmbus_device_id, do_vmbus_entry},
1357 {"rpmsg", SIZE_rpmsg_device_id, do_rpmsg_entry},
1358 {"i2c", SIZE_i2c_device_id, do_i2c_entry},
1359 {"spi", SIZE_spi_device_id, do_spi_entry},
1360 {"dmi", SIZE_dmi_system_id, do_dmi_entry},
1361 {"platform", SIZE_platform_device_id, do_platform_entry},
1362 {"mdio", SIZE_mdio_device_id, do_mdio_entry},
1363 {"zorro", SIZE_zorro_device_id, do_zorro_entry},
1364 {"isapnp", SIZE_isapnp_device_id, do_isapnp_entry},
1365 {"ipack", SIZE_ipack_device_id, do_ipack_entry},
1366 {"amba", SIZE_amba_id, do_amba_entry},
1367 {"mipscdmm", SIZE_mips_cdmm_device_id, do_mips_cdmm_entry},
1368 {"x86cpu", SIZE_x86_cpu_id, do_x86cpu_entry},
1369 {"cpu", SIZE_cpu_feature, do_cpu_entry},
1370 {"mei", SIZE_mei_cl_device_id, do_mei_entry},
1371 {"rapidio", SIZE_rio_device_id, do_rio_entry},
1372 {"ulpi", SIZE_ulpi_device_id, do_ulpi_entry},
1373 {"hdaudio", SIZE_hda_device_id, do_hda_entry},
1374 {"sdw", SIZE_sdw_device_id, do_sdw_entry},
1375 {"fslmc", SIZE_fsl_mc_device_id, do_fsl_mc_entry},
1376 {"tbsvc", SIZE_tb_service_id, do_tbsvc_entry},
1377 {"typec", SIZE_typec_device_id, do_typec_entry},
Sumit Garg0fc1db92019-01-29 11:19:35 +05301378 {"tee", SIZE_tee_client_device_id, do_tee_entry},
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001379};
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381/* Create MODULE_ALIAS() statements.
1382 * At this time, we cannot write the actual output C source yet,
1383 * so we write into the mod->dev_table_buf buffer. */
1384void handle_moddevtable(struct module *mod, struct elf_info *info,
1385 Elf_Sym *sym, const char *symname)
1386{
1387 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001388 char *zeros = NULL;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301389 const char *name, *identifier;
Rusty Russell626596e2012-01-13 09:32:15 +10301390 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001393 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return;
1395
David Millere88aa7b2012-04-12 14:37:30 -04001396 /* We're looking for an object */
1397 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1398 return;
1399
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001400 /* All our symbols are of form __mod_<name>__<identifier>_device_table. */
1401 if (strncmp(symname, "__mod_", strlen("__mod_")))
Rusty Russell626596e2012-01-13 09:32:15 +10301402 return;
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001403 name = symname + strlen("__mod_");
Rusty Russell626596e2012-01-13 09:32:15 +10301404 namelen = strlen(name);
1405 if (namelen < strlen("_device_table"))
1406 return;
1407 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1408 return;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301409 identifier = strstr(name, "__");
1410 if (!identifier)
1411 return;
1412 namelen = identifier - name;
Rusty Russell626596e2012-01-13 09:32:15 +10301413
Kees Cooke0049822007-09-16 11:15:46 +02001414 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001415 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001416 zeros = calloc(1, sym->st_size);
1417 symval = zeros;
1418 } else {
1419 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001420 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001421 + sym->st_value;
1422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423
Rusty Russell626596e2012-01-13 09:32:15 +10301424 /* First handle the "special" cases */
1425 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001426 do_usb_table(symval, sym->st_size, mod);
Philipp Zabelacbef7b2016-05-05 16:22:29 -07001427 if (sym_is(name, namelen, "of"))
1428 do_of_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301429 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001430 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301431 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001432 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301433 else {
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001434 int i;
Rusty Russell626596e2012-01-13 09:32:15 +10301435
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001436 for (i = 0; i < ARRAY_SIZE(devtable); i++) {
1437 const struct devtable *p = &devtable[i];
1438
1439 if (sym_is(name, namelen, p->device_id)) {
1440 do_table(symval, sym->st_size, p->id_size,
Masahiro Yamadaf880eea2018-11-22 13:28:42 +09001441 p->device_id, p->do_entry, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301442 break;
1443 }
1444 }
1445 }
Kees Cooke0049822007-09-16 11:15:46 +02001446 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
1449/* Now add out buffered information to the generated C source */
1450void add_moddevtable(struct buffer *buf, struct module *mod)
1451{
1452 buf_printf(buf, "\n");
1453 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1454 free(mod->dev_table_buf.p);
1455}