blob: c91eba751804bd64a3f2b1230c82f504ec7eb2f7 [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];
Heikki Krogerus389c9af2019-08-27 14:14:37 +030039} guid_t;
40
41/* backwards compatibility, don't use in new code */
42typedef struct {
43 __u8 b[16];
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -070044} uuid_le;
Sumit Garg0fc1db92019-01-29 11:19:35 +053045typedef struct {
46 __u8 b[16];
47} uuid_t;
Mattias Jacobssoneacc95e2019-02-19 20:59:49 +010048#define UUID_STRING_LEN 36
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Big exception to the "don't include kernel headers into userspace, which
Sam Ravnborg62070fa2006-03-03 16:46:04 +010051 * even potentially has different endianness and word sizes, since
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * we handle those differences explicitly below */
53#include "../../include/linux/mod_devicetable.h"
54
Rusty Russelle49ce142012-01-13 09:32:16 +103055/* This array collects all instances that use the generic do_table */
56struct devtable {
Tom Gundersen21bdd172014-02-03 11:14:13 +103057 const char *device_id; /* name of table, __mod_<name>__*_device_table. */
Rusty Russelle49ce142012-01-13 09:32:16 +103058 unsigned long id_size;
Masahiro Yamadaf880eea2018-11-22 13:28:42 +090059 int (*do_entry)(const char *filename, void *symval, char *alias);
Rusty Russelle49ce142012-01-13 09:32:16 +103060};
61
Mattias Jacobsson841f1b82019-02-07 13:30:22 +010062/* Size of alias provided to do_entry functions */
63#define ALIAS_SIZE 500
64
Andreas Schwab6543bec2013-01-20 17:58:47 +010065/* Define a variable f that holds the value of field f of struct devid
66 * based at address m.
67 */
68#define DEF_FIELD(m, devid, f) \
69 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
Leonardo Brasc2b1a922018-10-24 01:03:52 -030070
71/* Define a variable v that holds the address of field f of struct devid
72 * based at address m. Due to the way typeof works, for a field of type
73 * T[N] the variable has type T(*)[N], _not_ T*.
74 */
75#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
76 typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
77
Andreas Schwab6543bec2013-01-20 17:58:47 +010078/* Define a variable f that holds the address of field f of struct devid
79 * based at address m. Due to the way typeof works, for a field of type
80 * T[N] the variable has type T(*)[N], _not_ T*.
81 */
82#define DEF_FIELD_ADDR(m, devid, f) \
Leonardo Brasc2b1a922018-10-24 01:03:52 -030083 DEF_FIELD_ADDR_VAR(m, devid, f, f)
Andreas Schwab6543bec2013-01-20 17:58:47 +010084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define ADD(str, sep, cond, field) \
86do { \
87 strcat(str, sep); \
88 if (cond) \
89 sprintf(str + strlen(str), \
90 sizeof(field) == 1 ? "%02X" : \
91 sizeof(field) == 2 ? "%04X" : \
92 sizeof(field) == 4 ? "%08X" : "", \
93 field); \
94 else \
95 sprintf(str + strlen(str), "*"); \
96} while(0)
97
Javier Martinez Canillas2f632362016-01-14 15:17:02 -080098/* End in a wildcard, for future extension */
Jean Delvareac551822008-05-02 20:37:21 +020099static inline void add_wildcard(char *str)
100{
101 int len = strlen(str);
102
103 if (str[len - 1] != '*')
104 strcat(str + len, "*");
105}
106
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700107static inline void add_uuid(char *str, uuid_le uuid)
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300108{
109 int len = strlen(str);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300110
Prarit Bhargava59796ed2015-09-10 10:17:59 +0300111 sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
112 uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
113 uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
114 uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
115 uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300116}
117
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200118/**
119 * Check that sizeof(device_id type) are consistent with size of section
120 * in .o file. If in-consistent then userspace and kernel does not agree
121 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200122 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100123 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200124 **/
Kees Cooke0049822007-09-16 11:15:46 +0200125static void device_id_check(const char *modname, const char *device_id,
126 unsigned long size, unsigned long id_size,
127 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200128{
Kees Cooke0049822007-09-16 11:15:46 +0200129 int i;
130
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200131 if (size % id_size || size < id_size) {
132 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
Tom Gundersen21bdd172014-02-03 11:14:13 +1030133 "of the size of "
134 "section __mod_%s__<identifier>_device_table=%lu.\n"
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200135 "Fix definition of struct %s_device_id "
136 "in mod_devicetable.h\n",
137 modname, device_id, id_size, device_id, size, device_id);
138 }
Kees Cooke0049822007-09-16 11:15:46 +0200139 /* Verify last one is a terminator */
140 for (i = 0; i < id_size; i++ ) {
141 if (*(uint8_t*)(symval+size-id_size+i)) {
142 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
143 "The last of %lu is:\n",
144 modname, device_id, id_size, size / id_size);
145 for (i = 0; i < id_size; i++ )
146 fprintf(stderr,"0x%02x ",
147 *(uint8_t*)(symval+size-id_size+i) );
148 fprintf(stderr,"\n");
149 fatal("%s: struct %s_device_id is not terminated "
150 "with a NULL entry!\n", modname, device_id);
151 }
152 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200153}
154
Roman Kaganb19dcd92005-04-22 15:07:01 -0700155/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200156/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100157static void do_usb_entry(void *symval,
Roman Kaganb19dcd92005-04-22 15:07:01 -0700158 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
159 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500160 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700162 char alias[500];
Andreas Schwab6543bec2013-01-20 17:58:47 +0100163 DEF_FIELD(symval, usb_device_id, match_flags);
164 DEF_FIELD(symval, usb_device_id, idVendor);
165 DEF_FIELD(symval, usb_device_id, idProduct);
166 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
167 DEF_FIELD(symval, usb_device_id, bDeviceClass);
168 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
169 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
170 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
171 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
172 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
173 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 strcpy(alias, "usb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100176 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
177 idVendor);
178 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
179 idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700180
181 strcat(alias, "d");
182 if (bcdDevice_initial_digits)
183 sprintf(alias + strlen(alias), "%0*X",
184 bcdDevice_initial_digits, bcdDevice_initial);
185 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500186 sprintf(alias + strlen(alias), "%X", range_lo);
187 else if (range_lo > 0 || range_hi < max) {
188 if (range_lo > 0x9 || range_hi < 0xA)
189 sprintf(alias + strlen(alias),
190 "[%X-%X]",
191 range_lo,
192 range_hi);
193 else {
194 sprintf(alias + strlen(alias),
195 range_lo < 0x9 ? "[%X-9" : "[%X",
196 range_lo);
197 sprintf(alias + strlen(alias),
Jan Moskyto Matejka03b56322014-02-07 19:15:11 +0100198 range_hi > 0xA ? "A-%X]" : "%X]",
199 range_hi);
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500200 }
201 }
Andreas Schwab6543bec2013-01-20 17:58:47 +0100202 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700203 strcat(alias, "*");
204
Andreas Schwab6543bec2013-01-20 17:58:47 +0100205 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
206 bDeviceClass);
207 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
208 bDeviceSubClass);
209 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
210 bDeviceProtocol);
211 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
212 bInterfaceClass);
213 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
214 bInterfaceSubClass);
215 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
216 bInterfaceProtocol);
217 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
218 bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700219
Jean Delvareac551822008-05-02 20:37:21 +0200220 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700221 buf_printf(&mod->dev_table_buf,
222 "MODULE_ALIAS(\"%s\");\n", alias);
223}
224
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500225/* Handles increment/decrement of BCD formatted integers */
226/* Returns the previous value, so it works like i++ or i-- */
227static unsigned int incbcd(unsigned int *bcd,
228 int inc,
229 unsigned char max,
230 size_t chars)
231{
232 unsigned int init = *bcd, i, j;
233 unsigned long long c, dec = 0;
234
235 /* If bcd is not in BCD format, just increment */
236 if (max > 0x9) {
237 *bcd += inc;
238 return init;
239 }
240
241 /* Convert BCD to Decimal */
242 for (i=0 ; i < chars ; i++) {
243 c = (*bcd >> (i << 2)) & 0xf;
244 c = c > 9 ? 9 : c; /* force to bcd just in case */
245 for (j=0 ; j < i ; j++)
246 c = c * 10;
247 dec += c;
248 }
249
250 /* Do our increment/decrement */
251 dec += inc;
252 *bcd = 0;
253
254 /* Convert back to BCD */
255 for (i=0 ; i < chars ; i++) {
256 for (c=1,j=0 ; j < i ; j++)
257 c = c * 10;
258 c = (dec / c) % 10;
259 *bcd += c << (i << 2);
260 }
261 return init;
262}
263
Andreas Schwab6543bec2013-01-20 17:58:47 +0100264static void do_usb_entry_multi(void *symval, struct module *mod)
Roman Kaganb19dcd92005-04-22 15:07:01 -0700265{
266 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500267 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700268 int ndigits;
269
Andreas Schwab6543bec2013-01-20 17:58:47 +0100270 DEF_FIELD(symval, usb_device_id, match_flags);
271 DEF_FIELD(symval, usb_device_id, idVendor);
272 DEF_FIELD(symval, usb_device_id, idProduct);
273 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
274 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
275 DEF_FIELD(symval, usb_device_id, bDeviceClass);
276 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700277
Andreas Schwab6543bec2013-01-20 17:58:47 +0100278 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
279 bcdDevice_lo : 0x0U;
280 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
281 bcdDevice_hi : ~0x0U;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700282
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500283 /* Figure out if this entry is in bcd or hex format */
284 max = 0x9; /* Default to decimal format */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100285 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500286 clo = (devlo >> (ndigits << 2)) & 0xf;
287 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
288 if (clo > max || chi > max) {
289 max = 0xf;
290 break;
291 }
292 }
293
Roman Kaganb19dcd92005-04-22 15:07:01 -0700294 /*
295 * Some modules (visor) have empty slots as placeholder for
296 * run-time specification that results in catch-all alias
297 */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100298 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700299 return;
300
301 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100302 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
Roman Kaganb19dcd92005-04-22 15:07:01 -0700303 clo = devlo & 0xf;
304 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500305 if (chi > max) /* If we are in bcd mode, truncate if necessary */
306 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700307 devlo >>= 4;
308 devhi >>= 4;
309
310 if (devlo == devhi || !ndigits) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100311 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700312 break;
313 }
314
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500315 if (clo > 0x0)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100316 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500317 incbcd(&devlo, 1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100318 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500319 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700320
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500321 if (chi < max)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100322 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500323 incbcd(&devhi, -1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100324 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500325 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700326 }
327}
328
329static void do_usb_table(void *symval, unsigned long size,
330 struct module *mod)
331{
332 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100333 const unsigned long id_size = SIZE_usb_device_id;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700334
Kees Cooke0049822007-09-16 11:15:46 +0200335 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200336
Roman Kaganb19dcd92005-04-22 15:07:01 -0700337 /* Leave last one: it's the terminator. */
338 size -= id_size;
339
340 for (i = 0; i < size; i += id_size)
341 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342}
343
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700344static void do_of_entry_multi(void *symval, struct module *mod)
345{
346 char alias[500];
347 int len;
348 char *tmp;
349
350 DEF_FIELD_ADDR(symval, of_device_id, name);
351 DEF_FIELD_ADDR(symval, of_device_id, type);
352 DEF_FIELD_ADDR(symval, of_device_id, compatible);
353
354 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
355 (*type)[0] ? *type : "*");
356
Wolfram Sangb3c0a4d2016-06-06 18:48:38 +0200357 if ((*compatible)[0])
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700358 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
359 *compatible);
360
361 /* Replace all whitespace with underscores */
362 for (tmp = alias; tmp && *tmp; tmp++)
363 if (isspace(*tmp))
364 *tmp = '_';
365
366 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
367 strcat(alias, "C");
368 add_wildcard(alias);
369 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
370}
371
372static void do_of_table(void *symval, unsigned long size,
373 struct module *mod)
374{
375 unsigned int i;
376 const unsigned long id_size = SIZE_of_device_id;
377
378 device_id_check(mod->name, "of", size, id_size, symval);
379
380 /* Leave last one: it's the terminator. */
381 size -= id_size;
382
383 for (i = 0; i < size; i += id_size)
384 do_of_entry_multi(symval + i, mod);
385}
386
Jiri Slabye8c84f92008-05-19 15:50:01 +0200387/* Looks like: hid:bNvNpN */
388static int do_hid_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100389 void *symval, char *alias)
Jiri Slabye8c84f92008-05-19 15:50:01 +0200390{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100391 DEF_FIELD(symval, hid_device_id, bus);
392 DEF_FIELD(symval, hid_device_id, group);
393 DEF_FIELD(symval, hid_device_id, vendor);
394 DEF_FIELD(symval, hid_device_id, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200395
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200396 sprintf(alias, "hid:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100397 ADD(alias, "b", bus != HID_BUS_ANY, bus);
398 ADD(alias, "g", group != HID_GROUP_ANY, group);
399 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
400 ADD(alias, "p", product != HID_ANY_ID, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200401
402 return 1;
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/* Looks like: ieee1394:venNmoNspNverN */
406static int do_ieee1394_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100407 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100409 DEF_FIELD(symval, ieee1394_device_id, match_flags);
410 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
411 DEF_FIELD(symval, ieee1394_device_id, model_id);
412 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
413 DEF_FIELD(symval, ieee1394_device_id, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 strcpy(alias, "ieee1394:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100416 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
417 vendor_id);
418 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
419 model_id);
420 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
421 specifier_id);
422 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
423 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Jean Delvareac551822008-05-02 20:37:21 +0200425 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return 1;
427}
428
429/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
430static int do_pci_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100431 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 /* Class field can be divided into these three. */
434 unsigned char baseclass, subclass, interface,
435 baseclass_mask, subclass_mask, interface_mask;
436
Andreas Schwab6543bec2013-01-20 17:58:47 +0100437 DEF_FIELD(symval, pci_device_id, vendor);
438 DEF_FIELD(symval, pci_device_id, device);
439 DEF_FIELD(symval, pci_device_id, subvendor);
440 DEF_FIELD(symval, pci_device_id, subdevice);
441 DEF_FIELD(symval, pci_device_id, class);
442 DEF_FIELD(symval, pci_device_id, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 strcpy(alias, "pci:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100445 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
446 ADD(alias, "d", device != PCI_ANY_ID, device);
447 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
448 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Andreas Schwab6543bec2013-01-20 17:58:47 +0100450 baseclass = (class) >> 16;
451 baseclass_mask = (class_mask) >> 16;
452 subclass = (class) >> 8;
453 subclass_mask = (class_mask) >> 8;
454 interface = class;
455 interface_mask = class_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
458 || (subclass_mask != 0 && subclass_mask != 0xFF)
459 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100460 warn("Can't handle masks in %s:%04X\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100461 filename, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return 0;
463 }
464
465 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
466 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
467 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200468 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return 1;
470}
471
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100472/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static int do_ccw_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100474 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100476 DEF_FIELD(symval, ccw_device_id, match_flags);
477 DEF_FIELD(symval, ccw_device_id, cu_type);
478 DEF_FIELD(symval, ccw_device_id, cu_model);
479 DEF_FIELD(symval, ccw_device_id, dev_type);
480 DEF_FIELD(symval, ccw_device_id, dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 strcpy(alias, "ccw:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100483 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
484 cu_type);
485 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
486 cu_model);
487 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
488 dev_type);
489 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
490 dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200491 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 1;
493}
494
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200495/* looks like: "ap:tN" */
496static int do_ap_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100497 void *symval, char *alias)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200498{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100499 DEF_FIELD(symval, ap_device_id, dev_type);
500
501 sprintf(alias, "ap:t%02X*", dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200502 return 1;
503}
504
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200505/* looks like: "css:tN" */
506static int do_css_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100507 void *symval, char *alias)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200508{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100509 DEF_FIELD(symval, css_device_id, type);
510
511 sprintf(alias, "css:t%01X", type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200512 return 1;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/* Looks like: "serio:tyNprNidNexN" */
516static int do_serio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100517 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100519 DEF_FIELD(symval, serio_device_id, type);
520 DEF_FIELD(symval, serio_device_id, proto);
521 DEF_FIELD(symval, serio_device_id, id);
522 DEF_FIELD(symval, serio_device_id, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 strcpy(alias, "serio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100525 ADD(alias, "ty", type != SERIO_ANY, type);
526 ADD(alias, "pr", proto != SERIO_ANY, proto);
527 ADD(alias, "id", id != SERIO_ANY, id);
528 ADD(alias, "ex", extra != SERIO_ANY, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Jean Delvareac551822008-05-02 20:37:21 +0200530 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return 1;
532}
533
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200534/* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
535 * "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
536 *
537 * NOTE: Each driver should use one of the following : _HID, _CIDs
538 * or _CLS. Also, bb, ss, and pp can be substituted with ??
539 * as don't care byte.
540 */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200541static int do_acpi_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100542 void *symval, char *alias)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200543{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100544 DEF_FIELD_ADDR(symval, acpi_device_id, id);
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200545 DEF_FIELD_ADDR(symval, acpi_device_id, cls);
546 DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk);
547
548 if (id && strlen((const char *)*id))
549 sprintf(alias, "acpi*:%s:*", *id);
550 else if (cls) {
551 int i, byte_shift, cnt = 0;
552 unsigned int msk;
553
554 sprintf(&alias[cnt], "acpi*:");
555 cnt = 6;
556 for (i = 1; i <= 3; i++) {
557 byte_shift = 8 * (3-i);
558 msk = (*cls_msk >> byte_shift) & 0xFF;
559 if (msk)
560 sprintf(&alias[cnt], "%02x",
561 (*cls >> byte_shift) & 0xFF);
562 else
563 sprintf(&alias[cnt], "??");
564 cnt += 2;
565 }
566 sprintf(&alias[cnt], ":*");
567 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200568 return 1;
569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200572static void do_pnp_device_entry(void *symval, unsigned long size,
573 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100575 const unsigned long id_size = SIZE_pnp_device_id;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200576 const unsigned int count = (size / id_size)-1;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200577 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200578
579 device_id_check(mod->name, "pnp", size, id_size, symval);
580
Kay Sievers5e4c6562008-08-21 15:28:56 +0200581 for (i = 0; i < count; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100582 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
583 char acpi_id[sizeof(*id)];
Kay Sievers72638f52009-01-08 03:06:42 +0100584 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200585
586 buf_printf(&mod->dev_table_buf,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100587 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
Kay Sievers72638f52009-01-08 03:06:42 +0100588
589 /* fix broken pnp bus lowercasing */
590 for (j = 0; j < sizeof(acpi_id); j++)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100591 acpi_id[j] = toupper((*id)[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200592 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100593 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Kay Sievers0c81eed2008-02-21 00:35:54 +0100597/* looks like: "pnp:dD" for every device of the card */
598static void do_pnp_card_entries(void *symval, unsigned long size,
599 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100601 const unsigned long id_size = SIZE_pnp_card_device_id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100602 const unsigned int count = (size / id_size)-1;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100603 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Kay Sievers0c81eed2008-02-21 00:35:54 +0100605 device_id_check(mod->name, "pnp", size, id_size, symval);
606
607 for (i = 0; i < count; i++) {
608 unsigned int j;
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300609 DEF_FIELD_ADDR(symval + i * id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100610
611 for (j = 0; j < PNP_MAX_DEVICES; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100612 const char *id = (char *)(*devs)[j].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100613 int i2, j2;
614 int dup = 0;
615
616 if (!id[0])
617 break;
618
619 /* find duplicate, already added value */
620 for (i2 = 0; i2 < i && !dup; i2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300621 DEF_FIELD_ADDR_VAR(symval + i2 * id_size,
622 pnp_card_device_id,
623 devs, devs_dup);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100624
625 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300626 const char *id2 =
627 (char *)(*devs_dup)[j2].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100628
629 if (!id2[0])
630 break;
631
632 if (!strcmp(id, id2)) {
633 dup = 1;
634 break;
635 }
636 }
637 }
638
639 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200640 if (!dup) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100641 char acpi_id[PNP_ID_LEN];
Kay Sievers72638f52009-01-08 03:06:42 +0100642 int k;
643
Kay Sievers0c81eed2008-02-21 00:35:54 +0100644 buf_printf(&mod->dev_table_buf,
645 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100646
647 /* fix broken pnp bus lowercasing */
648 for (k = 0; k < sizeof(acpi_id); k++)
649 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200650 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100651 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200652 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700657/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
658static int do_pcmcia_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100659 void *symval, char *alias)
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700660{
661 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100662 DEF_FIELD(symval, pcmcia_device_id, match_flags);
663 DEF_FIELD(symval, pcmcia_device_id, manf_id);
664 DEF_FIELD(symval, pcmcia_device_id, card_id);
665 DEF_FIELD(symval, pcmcia_device_id, func_id);
666 DEF_FIELD(symval, pcmcia_device_id, function);
667 DEF_FIELD(symval, pcmcia_device_id, device_no);
668 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200669
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700670 for (i=0; i<4; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100671 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
672 }
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700673
Andreas Schwab6543bec2013-01-20 17:58:47 +0100674 strcpy(alias, "pcmcia:");
675 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
676 manf_id);
677 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
678 card_id);
679 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
680 func_id);
681 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
682 function);
683 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
684 device_no);
685 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
686 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
687 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
688 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700689
Jean Delvareac551822008-05-02 20:37:21 +0200690 add_wildcard(alias);
Andreas Schwab6543bec2013-01-20 17:58:47 +0100691 return 1;
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700692}
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700693
Andreas Schwab6543bec2013-01-20 17:58:47 +0100694static int do_vio_entry(const char *filename, void *symval,
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000695 char *alias)
696{
697 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100698 DEF_FIELD_ADDR(symval, vio_device_id, type);
699 DEF_FIELD_ADDR(symval, vio_device_id, compat);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000700
Andreas Schwab6543bec2013-01-20 17:58:47 +0100701 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
702 (*compat)[0] ? *compat : "*");
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000703
704 /* Replace all whitespace with underscores */
705 for (tmp = alias; tmp && *tmp; tmp++)
706 if (isspace (*tmp))
707 *tmp = '_';
708
Jean Delvareac551822008-05-02 20:37:21 +0200709 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000710 return 1;
711}
712
Rusty Russell1d8f4302005-12-07 21:40:34 +0100713#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
714
715static void do_input(char *alias,
716 kernel_ulong_t *arr, unsigned int min, unsigned int max)
717{
718 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400719
Andreas Schwab6543bec2013-01-20 17:58:47 +0100720 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
721 arr[i] = TO_NATIVE(arr[i]);
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400722 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200723 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400724 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100725}
726
Dmitry Torokhov09c3e012017-10-22 11:42:29 -0700727/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100728static int do_input_entry(const char *filename, void *symval,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100729 char *alias)
730{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100731 DEF_FIELD(symval, input_device_id, flags);
732 DEF_FIELD(symval, input_device_id, bustype);
733 DEF_FIELD(symval, input_device_id, vendor);
734 DEF_FIELD(symval, input_device_id, product);
735 DEF_FIELD(symval, input_device_id, version);
736 DEF_FIELD_ADDR(symval, input_device_id, evbit);
737 DEF_FIELD_ADDR(symval, input_device_id, keybit);
738 DEF_FIELD_ADDR(symval, input_device_id, relbit);
739 DEF_FIELD_ADDR(symval, input_device_id, absbit);
740 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
741 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
742 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
743 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
744 DEF_FIELD_ADDR(symval, input_device_id, swbit);
745
Rusty Russell1d8f4302005-12-07 21:40:34 +0100746 sprintf(alias, "input:");
747
Andreas Schwab6543bec2013-01-20 17:58:47 +0100748 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
749 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
750 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
751 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100752
753 sprintf(alias + strlen(alias), "-e*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100754 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
755 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100756 sprintf(alias + strlen(alias), "k*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100757 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
758 do_input(alias, *keybit,
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100759 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
760 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100761 sprintf(alias + strlen(alias), "r*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100762 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
763 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100764 sprintf(alias + strlen(alias), "a*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100765 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
766 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100767 sprintf(alias + strlen(alias), "m*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100768 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
769 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100770 sprintf(alias + strlen(alias), "l*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100771 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
772 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100773 sprintf(alias + strlen(alias), "s*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100774 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
775 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100776 sprintf(alias + strlen(alias), "f*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100777 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
778 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100779 sprintf(alias + strlen(alias), "w*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100780 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
781 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100782 return 1;
783}
784
Andreas Schwab6543bec2013-01-20 17:58:47 +0100785static int do_eisa_entry(const char *filename, void *symval,
Michael Tokarev07563c72006-09-27 01:50:56 -0700786 char *alias)
787{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100788 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
789 if (sig[0])
790 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
Jean Delvareac551822008-05-02 20:37:21 +0200791 else
792 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700793 return 1;
794}
795
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500796/* Looks like: parisc:tNhvNrevNsvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100797static int do_parisc_entry(const char *filename, void *symval,
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500798 char *alias)
799{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100800 DEF_FIELD(symval, parisc_device_id, hw_type);
801 DEF_FIELD(symval, parisc_device_id, hversion);
802 DEF_FIELD(symval, parisc_device_id, hversion_rev);
803 DEF_FIELD(symval, parisc_device_id, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500804
805 strcpy(alias, "parisc:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100806 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
807 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
808 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
809 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500810
Jean Delvareac551822008-05-02 20:37:21 +0200811 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500812 return 1;
813}
814
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200815/* Looks like: sdio:cNvNdN. */
816static int do_sdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100817 void *symval, char *alias)
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200818{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100819 DEF_FIELD(symval, sdio_device_id, class);
820 DEF_FIELD(symval, sdio_device_id, vendor);
821 DEF_FIELD(symval, sdio_device_id, device);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200822
823 strcpy(alias, "sdio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100824 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
825 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
826 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
Jean Delvareac551822008-05-02 20:37:21 +0200827 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700828 return 1;
829}
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200830
Michael Buesch61e115a2007-09-18 15:12:50 -0400831/* Looks like: ssb:vNidNrevN. */
832static int do_ssb_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100833 void *symval, char *alias)
Michael Buesch61e115a2007-09-18 15:12:50 -0400834{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100835 DEF_FIELD(symval, ssb_device_id, vendor);
836 DEF_FIELD(symval, ssb_device_id, coreid);
837 DEF_FIELD(symval, ssb_device_id, revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400838
839 strcpy(alias, "ssb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100840 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
841 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
842 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
Jean Delvareac551822008-05-02 20:37:21 +0200843 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200844 return 1;
845}
846
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200847/* Looks like: bcma:mNidNrevNclN. */
848static int do_bcma_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100849 void *symval, char *alias)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200850{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100851 DEF_FIELD(symval, bcma_device_id, manuf);
852 DEF_FIELD(symval, bcma_device_id, id);
853 DEF_FIELD(symval, bcma_device_id, rev);
854 DEF_FIELD(symval, bcma_device_id, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200855
856 strcpy(alias, "bcma:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100857 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
858 ADD(alias, "id", id != BCMA_ANY_ID, id);
859 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
860 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200861 add_wildcard(alias);
862 return 1;
863}
864
Rusty Russellb01d9f22007-10-22 11:03:39 +1000865/* Looks like: virtio:dNvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100866static int do_virtio_entry(const char *filename, void *symval,
Rusty Russellb01d9f22007-10-22 11:03:39 +1000867 char *alias)
868{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100869 DEF_FIELD(symval, virtio_device_id, device);
870 DEF_FIELD(symval, virtio_device_id, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000871
872 strcpy(alias, "virtio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100873 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
874 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000875
Jean Delvareac551822008-05-02 20:37:21 +0200876 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000877 return 1;
878}
879
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700880/*
881 * Looks like: vmbus:guid
882 * Each byte of the guid will be represented by two hex characters
883 * in the name.
884 */
885
Andreas Schwab6543bec2013-01-20 17:58:47 +0100886static int do_vmbus_entry(const char *filename, void *symval,
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700887 char *alias)
888{
889 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100890 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
891 char guid_name[(sizeof(*guid) + 1) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700892
Andreas Schwab6543bec2013-01-20 17:58:47 +0100893 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
K. Y. Srinivasanaf3ff642015-12-14 16:01:43 -0800894 sprintf(&guid_name[i], "%02x", TO_NATIVE((guid->b)[i/2]));
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700895
896 strcpy(alias, "vmbus:");
897 strcat(alias, guid_name);
898
899 return 1;
900}
901
Andrew F. Davis5b7d1272018-04-21 18:55:29 -0500902/* Looks like: rpmsg:S */
903static int do_rpmsg_entry(const char *filename, void *symval,
904 char *alias)
905{
906 DEF_FIELD_ADDR(symval, rpmsg_device_id, name);
907 sprintf(alias, RPMSG_DEVICE_MODALIAS_FMT, *name);
908
909 return 1;
910}
Andrew F. Davis5b7d1272018-04-21 18:55:29 -0500911
Jean Delvared2653e92008-04-29 23:11:39 +0200912/* Looks like: i2c:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100913static int do_i2c_entry(const char *filename, void *symval,
Jean Delvared2653e92008-04-29 23:11:39 +0200914 char *alias)
915{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100916 DEF_FIELD_ADDR(symval, i2c_device_id, name);
917 sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
Jean Delvared2653e92008-04-29 23:11:39 +0200918
919 return 1;
920}
921
Anton Vorontsove0626e32009-09-22 16:46:08 -0700922/* Looks like: spi:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100923static int do_spi_entry(const char *filename, void *symval,
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700924 char *alias)
925{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100926 DEF_FIELD_ADDR(symval, spi_device_id, name);
927 sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700928
929 return 1;
930}
931
David Woodhoused945b692008-09-16 16:23:28 -0700932static const struct dmifield {
933 const char *prefix;
934 int field;
935} dmi_fields[] = {
936 { "bvn", DMI_BIOS_VENDOR },
937 { "bvr", DMI_BIOS_VERSION },
938 { "bd", DMI_BIOS_DATE },
939 { "svn", DMI_SYS_VENDOR },
940 { "pn", DMI_PRODUCT_NAME },
941 { "pvr", DMI_PRODUCT_VERSION },
942 { "rvn", DMI_BOARD_VENDOR },
943 { "rn", DMI_BOARD_NAME },
944 { "rvr", DMI_BOARD_VERSION },
945 { "cvn", DMI_CHASSIS_VENDOR },
946 { "ct", DMI_CHASSIS_TYPE },
947 { "cvr", DMI_CHASSIS_VERSION },
948 { NULL, DMI_NONE }
949};
950
951static void dmi_ascii_filter(char *d, const char *s)
952{
953 /* Filter out characters we don't want to see in the modalias string */
954 for (; *s; s++)
955 if (*s > ' ' && *s < 127 && *s != ':')
956 *(d++) = *s;
957
958 *d = 0;
959}
960
961
Andreas Schwab6543bec2013-01-20 17:58:47 +0100962static int do_dmi_entry(const char *filename, void *symval,
David Woodhoused945b692008-09-16 16:23:28 -0700963 char *alias)
964{
965 int i, j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100966 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
David Woodhoused945b692008-09-16 16:23:28 -0700967 sprintf(alias, "dmi*");
968
969 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
970 for (j = 0; j < 4; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100971 if ((*matches)[j].slot &&
972 (*matches)[j].slot == dmi_fields[i].field) {
David Woodhoused945b692008-09-16 16:23:28 -0700973 sprintf(alias + strlen(alias), ":%s*",
974 dmi_fields[i].prefix);
975 dmi_ascii_filter(alias + strlen(alias),
Andreas Schwab6543bec2013-01-20 17:58:47 +0100976 (*matches)[j].substr);
David Woodhoused945b692008-09-16 16:23:28 -0700977 strcat(alias, "*");
978 }
979 }
980 }
981
982 strcat(alias, ":");
983 return 1;
984}
Eric Miao57fee4a2009-02-04 11:52:40 +0800985
986static int do_platform_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100987 void *symval, char *alias)
Eric Miao57fee4a2009-02-04 11:52:40 +0800988{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100989 DEF_FIELD_ADDR(symval, platform_device_id, name);
990 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
Eric Miao57fee4a2009-02-04 11:52:40 +0800991 return 1;
992}
993
David Woodhouse8626d3b2010-04-02 01:05:27 +0000994static int do_mdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100995 void *symval, char *alias)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000996{
997 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100998 DEF_FIELD(symval, mdio_device_id, phy_id);
999 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
David Woodhouse8626d3b2010-04-02 01:05:27 +00001000
1001 alias += sprintf(alias, MDIO_MODULE_PREFIX);
1002
1003 for (i = 0; i < 32; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +01001004 if (!((phy_id_mask >> (31-i)) & 1))
David Woodhouse8626d3b2010-04-02 01:05:27 +00001005 *(alias++) = '?';
Andreas Schwab6543bec2013-01-20 17:58:47 +01001006 else if ((phy_id >> (31-i)) & 1)
David Woodhouse8626d3b2010-04-02 01:05:27 +00001007 *(alias++) = '1';
1008 else
1009 *(alias++) = '0';
1010 }
1011
1012 /* Terminate the string */
1013 *alias = 0;
1014
1015 return 1;
1016}
1017
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001018/* Looks like: zorro:iN. */
Andreas Schwab6543bec2013-01-20 17:58:47 +01001019static int do_zorro_entry(const char *filename, void *symval,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001020 char *alias)
1021{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001022 DEF_FIELD(symval, zorro_device_id, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001023 strcpy(alias, "zorro:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001024 ADD(alias, "i", id != ZORRO_WILDCARD, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001025 return 1;
1026}
1027
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001028/* looks like: "pnp:dD" */
1029static int do_isapnp_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001030 void *symval, char *alias)
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001031{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001032 DEF_FIELD(symval, isapnp_device_id, vendor);
1033 DEF_FIELD(symval, isapnp_device_id, function);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001034 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001035 'A' + ((vendor >> 2) & 0x3f) - 1,
1036 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1037 'A' + ((vendor >> 8) & 0x1f) - 1,
1038 (function >> 4) & 0x0f, function & 0x0f,
1039 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001040 return 1;
1041}
1042
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001043/* Looks like: "ipack:fNvNdN". */
1044static int do_ipack_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001045 void *symval, char *alias)
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001046{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001047 DEF_FIELD(symval, ipack_device_id, format);
1048 DEF_FIELD(symval, ipack_device_id, vendor);
1049 DEF_FIELD(symval, ipack_device_id, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001050 strcpy(alias, "ipack:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001051 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1052 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1053 ADD(alias, "d", device != IPACK_ANY_ID, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001054 add_wildcard(alias);
1055 return 1;
1056}
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001057
Dave Martin523817b2011-10-05 14:44:57 +01001058/*
1059 * Append a match expression for a single masked hex digit.
1060 * outp points to a pointer to the character at which to append.
1061 * *outp is updated on return to point just after the appended text,
1062 * to facilitate further appending.
1063 */
1064static void append_nibble_mask(char **outp,
1065 unsigned int nibble, unsigned int mask)
1066{
1067 char *p = *outp;
1068 unsigned int i;
1069
1070 switch (mask) {
1071 case 0:
1072 *p++ = '?';
1073 break;
1074
1075 case 0xf:
1076 p += sprintf(p, "%X", nibble);
1077 break;
1078
1079 default:
1080 /*
1081 * Dumbly emit a match pattern for all possible matching
1082 * digits. This could be improved in some cases using ranges,
1083 * but it has the advantage of being trivially correct, and is
1084 * often optimal.
1085 */
1086 *p++ = '[';
1087 for (i = 0; i < 0x10; i++)
1088 if ((i & mask) == nibble)
1089 p += sprintf(p, "%X", i);
1090 *p++ = ']';
1091 }
1092
1093 /* Ensure that the string remains NUL-terminated: */
1094 *p = '\0';
1095
1096 /* Advance the caller's end-of-string pointer: */
1097 *outp = p;
1098}
1099
1100/*
1101 * looks like: "amba:dN"
1102 *
1103 * N is exactly 8 digits, where each is an upper-case hex digit, or
1104 * a ? or [] pattern matching exactly one digit.
1105 */
1106static int do_amba_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001107 void *symval, char *alias)
Dave Martin523817b2011-10-05 14:44:57 +01001108{
1109 unsigned int digit;
1110 char *p = alias;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001111 DEF_FIELD(symval, amba_id, id);
1112 DEF_FIELD(symval, amba_id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001113
Andreas Schwab6543bec2013-01-20 17:58:47 +01001114 if ((id & mask) != id)
Dave Martin523817b2011-10-05 14:44:57 +01001115 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1116 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001117 filename, id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001118
1119 p += sprintf(alias, "amba:d");
1120 for (digit = 0; digit < 8; digit++)
1121 append_nibble_mask(&p,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001122 (id >> (4 * (7 - digit))) & 0xf,
1123 (mask >> (4 * (7 - digit))) & 0xf);
Dave Martin523817b2011-10-05 14:44:57 +01001124
1125 return 1;
1126}
1127
James Hogan8286ae02015-03-25 15:39:50 +00001128/*
1129 * looks like: "mipscdmm:tN"
1130 *
1131 * N is exactly 2 digits, where each is an upper-case hex digit, or
1132 * a ? or [] pattern matching exactly one digit.
1133 */
1134static int do_mips_cdmm_entry(const char *filename,
1135 void *symval, char *alias)
1136{
1137 DEF_FIELD(symval, mips_cdmm_device_id, type);
1138
1139 sprintf(alias, "mipscdmm:t%02X*", type);
1140 return 1;
1141}
James Hogan8286ae02015-03-25 15:39:50 +00001142
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001143/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
Andi Kleen644e9cb2012-01-26 00:09:05 +01001144 * All fields are numbers. It would be nicer to use strings for vendor
1145 * and feature, but getting those out of the build system here is too
1146 * complicated.
1147 */
1148
Andreas Schwab6543bec2013-01-20 17:58:47 +01001149static int do_x86cpu_entry(const char *filename, void *symval,
Andi Kleen644e9cb2012-01-26 00:09:05 +01001150 char *alias)
1151{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001152 DEF_FIELD(symval, x86_cpu_id, feature);
1153 DEF_FIELD(symval, x86_cpu_id, family);
1154 DEF_FIELD(symval, x86_cpu_id, model);
1155 DEF_FIELD(symval, x86_cpu_id, vendor);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001156
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001157 strcpy(alias, "cpu:type:x86,");
1158 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1159 ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1160 ADD(alias, "mod", model != X86_MODEL_ANY, model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001161 strcat(alias, ":feature:*");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001162 if (feature != X86_FEATURE_ANY)
1163 sprintf(alias + strlen(alias), "%04X*", feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001164 return 1;
1165}
Andi Kleen644e9cb2012-01-26 00:09:05 +01001166
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001167/* LOOKS like cpu:type:*:feature:*FEAT* */
1168static int do_cpu_entry(const char *filename, void *symval, char *alias)
1169{
1170 DEF_FIELD(symval, cpu_feature, feature);
1171
1172 sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1173 return 1;
1174}
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001175
Tomas Winklerb26864c2015-09-10 10:18:01 +03001176/* Looks like: mei:S:uuid:N:* */
Samuel Ortize5354102013-03-27 17:29:53 +02001177static int do_mei_entry(const char *filename, void *symval,
1178 char *alias)
1179{
1180 DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001181 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001182 DEF_FIELD(symval, mei_cl_device_id, version);
Samuel Ortize5354102013-03-27 17:29:53 +02001183
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001184 sprintf(alias, MEI_CL_MODULE_PREFIX);
1185 sprintf(alias + strlen(alias), "%s:", (*name)[0] ? *name : "*");
1186 add_uuid(alias, *uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001187 ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001188
1189 strcat(alias, ":*");
Samuel Ortize5354102013-03-27 17:29:53 +02001190
1191 return 1;
1192}
Samuel Ortize5354102013-03-27 17:29:53 +02001193
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001194/* Looks like: rapidio:vNdNavNadN */
1195static int do_rio_entry(const char *filename,
1196 void *symval, char *alias)
1197{
1198 DEF_FIELD(symval, rio_device_id, did);
1199 DEF_FIELD(symval, rio_device_id, vid);
1200 DEF_FIELD(symval, rio_device_id, asm_did);
1201 DEF_FIELD(symval, rio_device_id, asm_vid);
1202
1203 strcpy(alias, "rapidio:");
1204 ADD(alias, "v", vid != RIO_ANY_ID, vid);
1205 ADD(alias, "d", did != RIO_ANY_ID, did);
1206 ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1207 ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1208
1209 add_wildcard(alias);
1210 return 1;
1211}
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001212
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001213/* Looks like: ulpi:vNpN */
1214static int do_ulpi_entry(const char *filename, void *symval,
1215 char *alias)
1216{
1217 DEF_FIELD(symval, ulpi_device_id, vendor);
1218 DEF_FIELD(symval, ulpi_device_id, product);
1219
1220 sprintf(alias, "ulpi:v%04xp%04x", vendor, product);
1221
1222 return 1;
1223}
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001224
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301225/* Looks like: hdaudio:vNrNaN */
1226static int do_hda_entry(const char *filename, void *symval, char *alias)
1227{
1228 DEF_FIELD(symval, hda_device_id, vendor_id);
1229 DEF_FIELD(symval, hda_device_id, rev_id);
1230 DEF_FIELD(symval, hda_device_id, api_version);
1231
1232 strcpy(alias, "hdaudio:");
1233 ADD(alias, "v", vendor_id != 0, vendor_id);
1234 ADD(alias, "r", rev_id != 0, rev_id);
1235 ADD(alias, "a", api_version != 0, api_version);
1236
1237 add_wildcard(alias);
1238 return 1;
1239}
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301240
Vinod Koul92513452017-12-14 11:19:33 +05301241/* Looks like: sdw:mNpN */
1242static int do_sdw_entry(const char *filename, void *symval, char *alias)
1243{
1244 DEF_FIELD(symval, sdw_device_id, mfg_id);
1245 DEF_FIELD(symval, sdw_device_id, part_id);
1246
1247 strcpy(alias, "sdw:");
1248 ADD(alias, "m", mfg_id != 0, mfg_id);
1249 ADD(alias, "p", part_id != 0, part_id);
1250
1251 add_wildcard(alias);
1252 return 1;
1253}
Vinod Koul92513452017-12-14 11:19:33 +05301254
Stuart Yoder0afef452016-06-22 16:40:45 -05001255/* Looks like: fsl-mc:vNdN */
1256static int do_fsl_mc_entry(const char *filename, void *symval,
1257 char *alias)
1258{
1259 DEF_FIELD(symval, fsl_mc_device_id, vendor);
1260 DEF_FIELD_ADDR(symval, fsl_mc_device_id, obj_type);
1261
1262 sprintf(alias, "fsl-mc:v%08Xd%s", vendor, *obj_type);
1263 return 1;
1264}
Stuart Yoder0afef452016-06-22 16:40:45 -05001265
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001266/* Looks like: tbsvc:kSpNvNrN */
1267static int do_tbsvc_entry(const char *filename, void *symval, char *alias)
1268{
1269 DEF_FIELD(symval, tb_service_id, match_flags);
1270 DEF_FIELD_ADDR(symval, tb_service_id, protocol_key);
1271 DEF_FIELD(symval, tb_service_id, protocol_id);
1272 DEF_FIELD(symval, tb_service_id, protocol_version);
1273 DEF_FIELD(symval, tb_service_id, protocol_revision);
1274
1275 strcpy(alias, "tbsvc:");
1276 if (match_flags & TBSVC_MATCH_PROTOCOL_KEY)
1277 sprintf(alias + strlen(alias), "k%s", *protocol_key);
1278 else
1279 strcat(alias + strlen(alias), "k*");
1280 ADD(alias, "p", match_flags & TBSVC_MATCH_PROTOCOL_ID, protocol_id);
1281 ADD(alias, "v", match_flags & TBSVC_MATCH_PROTOCOL_VERSION,
1282 protocol_version);
1283 ADD(alias, "r", match_flags & TBSVC_MATCH_PROTOCOL_REVISION,
1284 protocol_revision);
1285
1286 add_wildcard(alias);
1287 return 1;
1288}
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001289
Heikki Krogerus8a37d872018-06-27 18:19:50 +03001290/* Looks like: typec:idNmN */
1291static int do_typec_entry(const char *filename, void *symval, char *alias)
1292{
1293 DEF_FIELD(symval, typec_device_id, svid);
1294 DEF_FIELD(symval, typec_device_id, mode);
1295
1296 sprintf(alias, "typec:id%04X", svid);
1297 ADD(alias, "m", mode != TYPEC_ANY_MODE, mode);
1298
1299 return 1;
1300}
Heikki Krogerus8a37d872018-06-27 18:19:50 +03001301
Sumit Garg0fc1db92019-01-29 11:19:35 +05301302/* Looks like: tee:uuid */
1303static int do_tee_entry(const char *filename, void *symval, char *alias)
1304{
1305 DEF_FIELD(symval, tee_client_device_id, uuid);
1306
1307 sprintf(alias, "tee:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1308 uuid.b[0], uuid.b[1], uuid.b[2], uuid.b[3], uuid.b[4],
1309 uuid.b[5], uuid.b[6], uuid.b[7], uuid.b[8], uuid.b[9],
1310 uuid.b[10], uuid.b[11], uuid.b[12], uuid.b[13], uuid.b[14],
1311 uuid.b[15]);
1312
1313 add_wildcard(alias);
1314 return 1;
1315}
1316
Mattias Jacobsson0bc44b22019-02-19 20:59:50 +01001317/* Looks like: wmi:guid */
1318static int do_wmi_entry(const char *filename, void *symval, char *alias)
1319{
1320 int len;
1321 DEF_FIELD_ADDR(symval, wmi_device_id, guid_string);
1322
1323 if (strlen(*guid_string) != UUID_STRING_LEN) {
1324 warn("Invalid WMI device id 'wmi:%s' in '%s'\n",
1325 *guid_string, filename);
1326 return 0;
1327 }
1328
1329 len = snprintf(alias, ALIAS_SIZE, WMI_MODULE_PREFIX "%s", *guid_string);
1330 if (len < 0 || len >= ALIAS_SIZE) {
1331 warn("Could not generate all MODULE_ALIAS's in '%s'\n",
1332 filename);
1333 return 0;
1334 }
1335 return 1;
1336}
1337
Rusty Russell626596e2012-01-13 09:32:15 +10301338/* Does namelen bytes of name exactly match the symbol? */
1339static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Rusty Russell626596e2012-01-13 09:32:15 +10301341 if (namelen != strlen(symbol))
1342 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Rusty Russell626596e2012-01-13 09:32:15 +10301344 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345}
1346
1347static void do_table(void *symval, unsigned long size,
1348 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001349 const char *device_id,
Masahiro Yamadaf880eea2018-11-22 13:28:42 +09001350 int (*do_entry)(const char *filename, void *symval, char *alias),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 struct module *mod)
1352{
1353 unsigned int i;
Mattias Jacobsson841f1b82019-02-07 13:30:22 +01001354 char alias[ALIAS_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Kees Cooke0049822007-09-16 11:15:46 +02001356 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /* Leave last one: it's the terminator. */
1358 size -= id_size;
1359
1360 for (i = 0; i < size; i += id_size) {
1361 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 buf_printf(&mod->dev_table_buf,
1363 "MODULE_ALIAS(\"%s\");\n", alias);
1364 }
1365 }
1366}
1367
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001368static const struct devtable devtable[] = {
1369 {"hid", SIZE_hid_device_id, do_hid_entry},
1370 {"ieee1394", SIZE_ieee1394_device_id, do_ieee1394_entry},
1371 {"pci", SIZE_pci_device_id, do_pci_entry},
1372 {"ccw", SIZE_ccw_device_id, do_ccw_entry},
1373 {"ap", SIZE_ap_device_id, do_ap_entry},
1374 {"css", SIZE_css_device_id, do_css_entry},
1375 {"serio", SIZE_serio_device_id, do_serio_entry},
1376 {"acpi", SIZE_acpi_device_id, do_acpi_entry},
1377 {"pcmcia", SIZE_pcmcia_device_id, do_pcmcia_entry},
1378 {"vio", SIZE_vio_device_id, do_vio_entry},
1379 {"input", SIZE_input_device_id, do_input_entry},
1380 {"eisa", SIZE_eisa_device_id, do_eisa_entry},
1381 {"parisc", SIZE_parisc_device_id, do_parisc_entry},
1382 {"sdio", SIZE_sdio_device_id, do_sdio_entry},
1383 {"ssb", SIZE_ssb_device_id, do_ssb_entry},
1384 {"bcma", SIZE_bcma_device_id, do_bcma_entry},
1385 {"virtio", SIZE_virtio_device_id, do_virtio_entry},
1386 {"vmbus", SIZE_hv_vmbus_device_id, do_vmbus_entry},
1387 {"rpmsg", SIZE_rpmsg_device_id, do_rpmsg_entry},
1388 {"i2c", SIZE_i2c_device_id, do_i2c_entry},
1389 {"spi", SIZE_spi_device_id, do_spi_entry},
1390 {"dmi", SIZE_dmi_system_id, do_dmi_entry},
1391 {"platform", SIZE_platform_device_id, do_platform_entry},
1392 {"mdio", SIZE_mdio_device_id, do_mdio_entry},
1393 {"zorro", SIZE_zorro_device_id, do_zorro_entry},
1394 {"isapnp", SIZE_isapnp_device_id, do_isapnp_entry},
1395 {"ipack", SIZE_ipack_device_id, do_ipack_entry},
1396 {"amba", SIZE_amba_id, do_amba_entry},
1397 {"mipscdmm", SIZE_mips_cdmm_device_id, do_mips_cdmm_entry},
1398 {"x86cpu", SIZE_x86_cpu_id, do_x86cpu_entry},
1399 {"cpu", SIZE_cpu_feature, do_cpu_entry},
1400 {"mei", SIZE_mei_cl_device_id, do_mei_entry},
1401 {"rapidio", SIZE_rio_device_id, do_rio_entry},
1402 {"ulpi", SIZE_ulpi_device_id, do_ulpi_entry},
1403 {"hdaudio", SIZE_hda_device_id, do_hda_entry},
1404 {"sdw", SIZE_sdw_device_id, do_sdw_entry},
1405 {"fslmc", SIZE_fsl_mc_device_id, do_fsl_mc_entry},
1406 {"tbsvc", SIZE_tb_service_id, do_tbsvc_entry},
1407 {"typec", SIZE_typec_device_id, do_typec_entry},
Sumit Garg0fc1db92019-01-29 11:19:35 +05301408 {"tee", SIZE_tee_client_device_id, do_tee_entry},
Mattias Jacobsson0bc44b22019-02-19 20:59:50 +01001409 {"wmi", SIZE_wmi_device_id, do_wmi_entry},
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001410};
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/* Create MODULE_ALIAS() statements.
1413 * At this time, we cannot write the actual output C source yet,
1414 * so we write into the mod->dev_table_buf buffer. */
1415void handle_moddevtable(struct module *mod, struct elf_info *info,
1416 Elf_Sym *sym, const char *symname)
1417{
1418 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001419 char *zeros = NULL;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301420 const char *name, *identifier;
Rusty Russell626596e2012-01-13 09:32:15 +10301421 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001424 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 return;
1426
David Millere88aa7b2012-04-12 14:37:30 -04001427 /* We're looking for an object */
1428 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1429 return;
1430
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001431 /* All our symbols are of form __mod_<name>__<identifier>_device_table. */
1432 if (strncmp(symname, "__mod_", strlen("__mod_")))
Rusty Russell626596e2012-01-13 09:32:15 +10301433 return;
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001434 name = symname + strlen("__mod_");
Rusty Russell626596e2012-01-13 09:32:15 +10301435 namelen = strlen(name);
1436 if (namelen < strlen("_device_table"))
1437 return;
1438 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1439 return;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301440 identifier = strstr(name, "__");
1441 if (!identifier)
1442 return;
1443 namelen = identifier - name;
Rusty Russell626596e2012-01-13 09:32:15 +10301444
Kees Cooke0049822007-09-16 11:15:46 +02001445 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001446 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001447 zeros = calloc(1, sym->st_size);
1448 symval = zeros;
1449 } else {
1450 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001451 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001452 + sym->st_value;
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Rusty Russell626596e2012-01-13 09:32:15 +10301455 /* First handle the "special" cases */
1456 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001457 do_usb_table(symval, sym->st_size, mod);
Philipp Zabelacbef7b2016-05-05 16:22:29 -07001458 if (sym_is(name, namelen, "of"))
1459 do_of_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301460 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001461 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301462 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001463 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301464 else {
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001465 int i;
Rusty Russell626596e2012-01-13 09:32:15 +10301466
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001467 for (i = 0; i < ARRAY_SIZE(devtable); i++) {
1468 const struct devtable *p = &devtable[i];
1469
1470 if (sym_is(name, namelen, p->device_id)) {
1471 do_table(symval, sym->st_size, p->id_size,
Masahiro Yamadaf880eea2018-11-22 13:28:42 +09001472 p->device_id, p->do_entry, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301473 break;
1474 }
1475 }
1476 }
Kees Cooke0049822007-09-16 11:15:46 +02001477 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480/* Now add out buffered information to the generated C source */
1481void add_moddevtable(struct buffer *buf, struct module *mod)
1482{
1483 buf_printf(buf, "\n");
1484 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1485 free(mod->dev_table_buf.p);
1486}