blob: e17a29ae2e97adfbf4dcd576452ca807427c37a7 [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;
Mattias Jacobssoneacc95e2019-02-19 20:59:49 +010043#define UUID_STRING_LEN 36
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/* Big exception to the "don't include kernel headers into userspace, which
Sam Ravnborg62070fa2006-03-03 16:46:04 +010046 * even potentially has different endianness and word sizes, since
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * we handle those differences explicitly below */
48#include "../../include/linux/mod_devicetable.h"
49
Rusty Russelle49ce142012-01-13 09:32:16 +103050/* This array collects all instances that use the generic do_table */
51struct devtable {
Tom Gundersen21bdd172014-02-03 11:14:13 +103052 const char *device_id; /* name of table, __mod_<name>__*_device_table. */
Rusty Russelle49ce142012-01-13 09:32:16 +103053 unsigned long id_size;
Masahiro Yamadaf880eea2018-11-22 13:28:42 +090054 int (*do_entry)(const char *filename, void *symval, char *alias);
Rusty Russelle49ce142012-01-13 09:32:16 +103055};
56
Mattias Jacobsson841f1b82019-02-07 13:30:22 +010057/* Size of alias provided to do_entry functions */
58#define ALIAS_SIZE 500
59
Andreas Schwab6543bec2013-01-20 17:58:47 +010060/* Define a variable f that holds the value of field f of struct devid
61 * based at address m.
62 */
63#define DEF_FIELD(m, devid, f) \
64 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
Leonardo Brasc2b1a922018-10-24 01:03:52 -030065
66/* Define a variable v that holds the address of field f of struct devid
67 * based at address m. Due to the way typeof works, for a field of type
68 * T[N] the variable has type T(*)[N], _not_ T*.
69 */
70#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
71 typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
72
Andreas Schwab6543bec2013-01-20 17:58:47 +010073/* Define a variable f that holds the address of field f of struct devid
74 * based at address m. Due to the way typeof works, for a field of type
75 * T[N] the variable has type T(*)[N], _not_ T*.
76 */
77#define DEF_FIELD_ADDR(m, devid, f) \
Leonardo Brasc2b1a922018-10-24 01:03:52 -030078 DEF_FIELD_ADDR_VAR(m, devid, f, f)
Andreas Schwab6543bec2013-01-20 17:58:47 +010079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080#define ADD(str, sep, cond, field) \
81do { \
82 strcat(str, sep); \
83 if (cond) \
84 sprintf(str + strlen(str), \
85 sizeof(field) == 1 ? "%02X" : \
86 sizeof(field) == 2 ? "%04X" : \
87 sizeof(field) == 4 ? "%08X" : "", \
88 field); \
89 else \
90 sprintf(str + strlen(str), "*"); \
91} while(0)
92
Javier Martinez Canillas2f632362016-01-14 15:17:02 -080093/* End in a wildcard, for future extension */
Jean Delvareac551822008-05-02 20:37:21 +020094static inline void add_wildcard(char *str)
95{
96 int len = strlen(str);
97
98 if (str[len - 1] != '*')
99 strcat(str + len, "*");
100}
101
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700102static inline void add_uuid(char *str, uuid_le uuid)
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300103{
104 int len = strlen(str);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300105
Prarit Bhargava59796ed2015-09-10 10:17:59 +0300106 sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
107 uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
108 uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
109 uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
110 uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300111}
112
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200113/**
114 * Check that sizeof(device_id type) are consistent with size of section
115 * in .o file. If in-consistent then userspace and kernel does not agree
116 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200117 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100118 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200119 **/
Kees Cooke0049822007-09-16 11:15:46 +0200120static void device_id_check(const char *modname, const char *device_id,
121 unsigned long size, unsigned long id_size,
122 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200123{
Kees Cooke0049822007-09-16 11:15:46 +0200124 int i;
125
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200126 if (size % id_size || size < id_size) {
127 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
Tom Gundersen21bdd172014-02-03 11:14:13 +1030128 "of the size of "
129 "section __mod_%s__<identifier>_device_table=%lu.\n"
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200130 "Fix definition of struct %s_device_id "
131 "in mod_devicetable.h\n",
132 modname, device_id, id_size, device_id, size, device_id);
133 }
Kees Cooke0049822007-09-16 11:15:46 +0200134 /* Verify last one is a terminator */
135 for (i = 0; i < id_size; i++ ) {
136 if (*(uint8_t*)(symval+size-id_size+i)) {
137 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
138 "The last of %lu is:\n",
139 modname, device_id, id_size, size / id_size);
140 for (i = 0; i < id_size; i++ )
141 fprintf(stderr,"0x%02x ",
142 *(uint8_t*)(symval+size-id_size+i) );
143 fprintf(stderr,"\n");
144 fatal("%s: struct %s_device_id is not terminated "
145 "with a NULL entry!\n", modname, device_id);
146 }
147 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200148}
149
Roman Kaganb19dcd92005-04-22 15:07:01 -0700150/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200151/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100152static void do_usb_entry(void *symval,
Roman Kaganb19dcd92005-04-22 15:07:01 -0700153 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
154 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500155 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700157 char alias[500];
Andreas Schwab6543bec2013-01-20 17:58:47 +0100158 DEF_FIELD(symval, usb_device_id, match_flags);
159 DEF_FIELD(symval, usb_device_id, idVendor);
160 DEF_FIELD(symval, usb_device_id, idProduct);
161 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
162 DEF_FIELD(symval, usb_device_id, bDeviceClass);
163 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
164 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
165 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
166 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
167 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
168 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 strcpy(alias, "usb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100171 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
172 idVendor);
173 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
174 idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700175
176 strcat(alias, "d");
177 if (bcdDevice_initial_digits)
178 sprintf(alias + strlen(alias), "%0*X",
179 bcdDevice_initial_digits, bcdDevice_initial);
180 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500181 sprintf(alias + strlen(alias), "%X", range_lo);
182 else if (range_lo > 0 || range_hi < max) {
183 if (range_lo > 0x9 || range_hi < 0xA)
184 sprintf(alias + strlen(alias),
185 "[%X-%X]",
186 range_lo,
187 range_hi);
188 else {
189 sprintf(alias + strlen(alias),
190 range_lo < 0x9 ? "[%X-9" : "[%X",
191 range_lo);
192 sprintf(alias + strlen(alias),
Jan Moskyto Matejka03b56322014-02-07 19:15:11 +0100193 range_hi > 0xA ? "A-%X]" : "%X]",
194 range_hi);
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500195 }
196 }
Andreas Schwab6543bec2013-01-20 17:58:47 +0100197 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700198 strcat(alias, "*");
199
Andreas Schwab6543bec2013-01-20 17:58:47 +0100200 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
201 bDeviceClass);
202 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
203 bDeviceSubClass);
204 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
205 bDeviceProtocol);
206 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
207 bInterfaceClass);
208 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
209 bInterfaceSubClass);
210 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
211 bInterfaceProtocol);
212 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
213 bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700214
Jean Delvareac551822008-05-02 20:37:21 +0200215 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700216 buf_printf(&mod->dev_table_buf,
217 "MODULE_ALIAS(\"%s\");\n", alias);
218}
219
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500220/* Handles increment/decrement of BCD formatted integers */
221/* Returns the previous value, so it works like i++ or i-- */
222static unsigned int incbcd(unsigned int *bcd,
223 int inc,
224 unsigned char max,
225 size_t chars)
226{
227 unsigned int init = *bcd, i, j;
228 unsigned long long c, dec = 0;
229
230 /* If bcd is not in BCD format, just increment */
231 if (max > 0x9) {
232 *bcd += inc;
233 return init;
234 }
235
236 /* Convert BCD to Decimal */
237 for (i=0 ; i < chars ; i++) {
238 c = (*bcd >> (i << 2)) & 0xf;
239 c = c > 9 ? 9 : c; /* force to bcd just in case */
240 for (j=0 ; j < i ; j++)
241 c = c * 10;
242 dec += c;
243 }
244
245 /* Do our increment/decrement */
246 dec += inc;
247 *bcd = 0;
248
249 /* Convert back to BCD */
250 for (i=0 ; i < chars ; i++) {
251 for (c=1,j=0 ; j < i ; j++)
252 c = c * 10;
253 c = (dec / c) % 10;
254 *bcd += c << (i << 2);
255 }
256 return init;
257}
258
Andreas Schwab6543bec2013-01-20 17:58:47 +0100259static void do_usb_entry_multi(void *symval, struct module *mod)
Roman Kaganb19dcd92005-04-22 15:07:01 -0700260{
261 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500262 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700263 int ndigits;
264
Andreas Schwab6543bec2013-01-20 17:58:47 +0100265 DEF_FIELD(symval, usb_device_id, match_flags);
266 DEF_FIELD(symval, usb_device_id, idVendor);
267 DEF_FIELD(symval, usb_device_id, idProduct);
268 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
269 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
270 DEF_FIELD(symval, usb_device_id, bDeviceClass);
271 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700272
Andreas Schwab6543bec2013-01-20 17:58:47 +0100273 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
274 bcdDevice_lo : 0x0U;
275 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
276 bcdDevice_hi : ~0x0U;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700277
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500278 /* Figure out if this entry is in bcd or hex format */
279 max = 0x9; /* Default to decimal format */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100280 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500281 clo = (devlo >> (ndigits << 2)) & 0xf;
282 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
283 if (clo > max || chi > max) {
284 max = 0xf;
285 break;
286 }
287 }
288
Roman Kaganb19dcd92005-04-22 15:07:01 -0700289 /*
290 * Some modules (visor) have empty slots as placeholder for
291 * run-time specification that results in catch-all alias
292 */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100293 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700294 return;
295
296 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100297 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
Roman Kaganb19dcd92005-04-22 15:07:01 -0700298 clo = devlo & 0xf;
299 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500300 if (chi > max) /* If we are in bcd mode, truncate if necessary */
301 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700302 devlo >>= 4;
303 devhi >>= 4;
304
305 if (devlo == devhi || !ndigits) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100306 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700307 break;
308 }
309
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500310 if (clo > 0x0)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100311 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500312 incbcd(&devlo, 1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100313 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500314 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700315
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500316 if (chi < max)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100317 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500318 incbcd(&devhi, -1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100319 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500320 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700321 }
322}
323
324static void do_usb_table(void *symval, unsigned long size,
325 struct module *mod)
326{
327 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100328 const unsigned long id_size = SIZE_usb_device_id;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700329
Kees Cooke0049822007-09-16 11:15:46 +0200330 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200331
Roman Kaganb19dcd92005-04-22 15:07:01 -0700332 /* Leave last one: it's the terminator. */
333 size -= id_size;
334
335 for (i = 0; i < size; i += id_size)
336 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700339static void do_of_entry_multi(void *symval, struct module *mod)
340{
341 char alias[500];
342 int len;
343 char *tmp;
344
345 DEF_FIELD_ADDR(symval, of_device_id, name);
346 DEF_FIELD_ADDR(symval, of_device_id, type);
347 DEF_FIELD_ADDR(symval, of_device_id, compatible);
348
349 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
350 (*type)[0] ? *type : "*");
351
Wolfram Sangb3c0a4d2016-06-06 18:48:38 +0200352 if ((*compatible)[0])
Philipp Zabelacbef7b2016-05-05 16:22:29 -0700353 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
354 *compatible);
355
356 /* Replace all whitespace with underscores */
357 for (tmp = alias; tmp && *tmp; tmp++)
358 if (isspace(*tmp))
359 *tmp = '_';
360
361 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
362 strcat(alias, "C");
363 add_wildcard(alias);
364 buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
365}
366
367static void do_of_table(void *symval, unsigned long size,
368 struct module *mod)
369{
370 unsigned int i;
371 const unsigned long id_size = SIZE_of_device_id;
372
373 device_id_check(mod->name, "of", size, id_size, symval);
374
375 /* Leave last one: it's the terminator. */
376 size -= id_size;
377
378 for (i = 0; i < size; i += id_size)
379 do_of_entry_multi(symval + i, mod);
380}
381
Jiri Slabye8c84f92008-05-19 15:50:01 +0200382/* Looks like: hid:bNvNpN */
383static int do_hid_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100384 void *symval, char *alias)
Jiri Slabye8c84f92008-05-19 15:50:01 +0200385{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100386 DEF_FIELD(symval, hid_device_id, bus);
387 DEF_FIELD(symval, hid_device_id, group);
388 DEF_FIELD(symval, hid_device_id, vendor);
389 DEF_FIELD(symval, hid_device_id, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200390
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200391 sprintf(alias, "hid:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100392 ADD(alias, "b", bus != HID_BUS_ANY, bus);
393 ADD(alias, "g", group != HID_GROUP_ANY, group);
394 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
395 ADD(alias, "p", product != HID_ANY_ID, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200396
397 return 1;
398}
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400/* Looks like: ieee1394:venNmoNspNverN */
401static int do_ieee1394_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100402 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100404 DEF_FIELD(symval, ieee1394_device_id, match_flags);
405 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
406 DEF_FIELD(symval, ieee1394_device_id, model_id);
407 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
408 DEF_FIELD(symval, ieee1394_device_id, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 strcpy(alias, "ieee1394:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100411 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
412 vendor_id);
413 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
414 model_id);
415 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
416 specifier_id);
417 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
418 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Jean Delvareac551822008-05-02 20:37:21 +0200420 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 1;
422}
423
424/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
425static int do_pci_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100426 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
428 /* Class field can be divided into these three. */
429 unsigned char baseclass, subclass, interface,
430 baseclass_mask, subclass_mask, interface_mask;
431
Andreas Schwab6543bec2013-01-20 17:58:47 +0100432 DEF_FIELD(symval, pci_device_id, vendor);
433 DEF_FIELD(symval, pci_device_id, device);
434 DEF_FIELD(symval, pci_device_id, subvendor);
435 DEF_FIELD(symval, pci_device_id, subdevice);
436 DEF_FIELD(symval, pci_device_id, class);
437 DEF_FIELD(symval, pci_device_id, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 strcpy(alias, "pci:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100440 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
441 ADD(alias, "d", device != PCI_ANY_ID, device);
442 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
443 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Andreas Schwab6543bec2013-01-20 17:58:47 +0100445 baseclass = (class) >> 16;
446 baseclass_mask = (class_mask) >> 16;
447 subclass = (class) >> 8;
448 subclass_mask = (class_mask) >> 8;
449 interface = class;
450 interface_mask = class_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
453 || (subclass_mask != 0 && subclass_mask != 0xFF)
454 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100455 warn("Can't handle masks in %s:%04X\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100456 filename, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458 }
459
460 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
461 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
462 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200463 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 return 1;
465}
466
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100467/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468static int do_ccw_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100469 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100471 DEF_FIELD(symval, ccw_device_id, match_flags);
472 DEF_FIELD(symval, ccw_device_id, cu_type);
473 DEF_FIELD(symval, ccw_device_id, cu_model);
474 DEF_FIELD(symval, ccw_device_id, dev_type);
475 DEF_FIELD(symval, ccw_device_id, dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 strcpy(alias, "ccw:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100478 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
479 cu_type);
480 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
481 cu_model);
482 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
483 dev_type);
484 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
485 dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200486 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return 1;
488}
489
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200490/* looks like: "ap:tN" */
491static int do_ap_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100492 void *symval, char *alias)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200493{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100494 DEF_FIELD(symval, ap_device_id, dev_type);
495
496 sprintf(alias, "ap:t%02X*", dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200497 return 1;
498}
499
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200500/* looks like: "css:tN" */
501static int do_css_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100502 void *symval, char *alias)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200503{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100504 DEF_FIELD(symval, css_device_id, type);
505
506 sprintf(alias, "css:t%01X", type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200507 return 1;
508}
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510/* Looks like: "serio:tyNprNidNexN" */
511static int do_serio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100512 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100514 DEF_FIELD(symval, serio_device_id, type);
515 DEF_FIELD(symval, serio_device_id, proto);
516 DEF_FIELD(symval, serio_device_id, id);
517 DEF_FIELD(symval, serio_device_id, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 strcpy(alias, "serio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100520 ADD(alias, "ty", type != SERIO_ANY, type);
521 ADD(alias, "pr", proto != SERIO_ANY, proto);
522 ADD(alias, "id", id != SERIO_ANY, id);
523 ADD(alias, "ex", extra != SERIO_ANY, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Jean Delvareac551822008-05-02 20:37:21 +0200525 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return 1;
527}
528
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200529/* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
530 * "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
531 *
532 * NOTE: Each driver should use one of the following : _HID, _CIDs
533 * or _CLS. Also, bb, ss, and pp can be substituted with ??
534 * as don't care byte.
535 */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200536static int do_acpi_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100537 void *symval, char *alias)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200538{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100539 DEF_FIELD_ADDR(symval, acpi_device_id, id);
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200540 DEF_FIELD_ADDR(symval, acpi_device_id, cls);
541 DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk);
542
543 if (id && strlen((const char *)*id))
544 sprintf(alias, "acpi*:%s:*", *id);
545 else if (cls) {
546 int i, byte_shift, cnt = 0;
547 unsigned int msk;
548
549 sprintf(&alias[cnt], "acpi*:");
550 cnt = 6;
551 for (i = 1; i <= 3; i++) {
552 byte_shift = 8 * (3-i);
553 msk = (*cls_msk >> byte_shift) & 0xFF;
554 if (msk)
555 sprintf(&alias[cnt], "%02x",
556 (*cls >> byte_shift) & 0xFF);
557 else
558 sprintf(&alias[cnt], "??");
559 cnt += 2;
560 }
561 sprintf(&alias[cnt], ":*");
562 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200563 return 1;
564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200567static void do_pnp_device_entry(void *symval, unsigned long size,
568 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100570 const unsigned long id_size = SIZE_pnp_device_id;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200571 const unsigned int count = (size / id_size)-1;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200572 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200573
574 device_id_check(mod->name, "pnp", size, id_size, symval);
575
Kay Sievers5e4c6562008-08-21 15:28:56 +0200576 for (i = 0; i < count; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100577 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
578 char acpi_id[sizeof(*id)];
Kay Sievers72638f52009-01-08 03:06:42 +0100579 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200580
581 buf_printf(&mod->dev_table_buf,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100582 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
Kay Sievers72638f52009-01-08 03:06:42 +0100583
584 /* fix broken pnp bus lowercasing */
585 for (j = 0; j < sizeof(acpi_id); j++)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100586 acpi_id[j] = toupper((*id)[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200587 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100588 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
Kay Sievers0c81eed2008-02-21 00:35:54 +0100592/* looks like: "pnp:dD" for every device of the card */
593static void do_pnp_card_entries(void *symval, unsigned long size,
594 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100596 const unsigned long id_size = SIZE_pnp_card_device_id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100597 const unsigned int count = (size / id_size)-1;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100598 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Kay Sievers0c81eed2008-02-21 00:35:54 +0100600 device_id_check(mod->name, "pnp", size, id_size, symval);
601
602 for (i = 0; i < count; i++) {
603 unsigned int j;
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300604 DEF_FIELD_ADDR(symval + i * id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100605
606 for (j = 0; j < PNP_MAX_DEVICES; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100607 const char *id = (char *)(*devs)[j].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100608 int i2, j2;
609 int dup = 0;
610
611 if (!id[0])
612 break;
613
614 /* find duplicate, already added value */
615 for (i2 = 0; i2 < i && !dup; i2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300616 DEF_FIELD_ADDR_VAR(symval + i2 * id_size,
617 pnp_card_device_id,
618 devs, devs_dup);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100619
620 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
Leonardo Brasc2b1a922018-10-24 01:03:52 -0300621 const char *id2 =
622 (char *)(*devs_dup)[j2].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100623
624 if (!id2[0])
625 break;
626
627 if (!strcmp(id, id2)) {
628 dup = 1;
629 break;
630 }
631 }
632 }
633
634 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200635 if (!dup) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100636 char acpi_id[PNP_ID_LEN];
Kay Sievers72638f52009-01-08 03:06:42 +0100637 int k;
638
Kay Sievers0c81eed2008-02-21 00:35:54 +0100639 buf_printf(&mod->dev_table_buf,
640 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100641
642 /* fix broken pnp bus lowercasing */
643 for (k = 0; k < sizeof(acpi_id); k++)
644 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200645 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100646 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200647 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700652/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
653static int do_pcmcia_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100654 void *symval, char *alias)
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700655{
656 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100657 DEF_FIELD(symval, pcmcia_device_id, match_flags);
658 DEF_FIELD(symval, pcmcia_device_id, manf_id);
659 DEF_FIELD(symval, pcmcia_device_id, card_id);
660 DEF_FIELD(symval, pcmcia_device_id, func_id);
661 DEF_FIELD(symval, pcmcia_device_id, function);
662 DEF_FIELD(symval, pcmcia_device_id, device_no);
663 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200664
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700665 for (i=0; i<4; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100666 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
667 }
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700668
Andreas Schwab6543bec2013-01-20 17:58:47 +0100669 strcpy(alias, "pcmcia:");
670 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
671 manf_id);
672 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
673 card_id);
674 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
675 func_id);
676 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
677 function);
678 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
679 device_no);
680 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
681 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
682 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
683 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700684
Jean Delvareac551822008-05-02 20:37:21 +0200685 add_wildcard(alias);
Andreas Schwab6543bec2013-01-20 17:58:47 +0100686 return 1;
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700687}
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700688
Andreas Schwab6543bec2013-01-20 17:58:47 +0100689static int do_vio_entry(const char *filename, void *symval,
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000690 char *alias)
691{
692 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100693 DEF_FIELD_ADDR(symval, vio_device_id, type);
694 DEF_FIELD_ADDR(symval, vio_device_id, compat);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000695
Andreas Schwab6543bec2013-01-20 17:58:47 +0100696 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
697 (*compat)[0] ? *compat : "*");
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000698
699 /* Replace all whitespace with underscores */
700 for (tmp = alias; tmp && *tmp; tmp++)
701 if (isspace (*tmp))
702 *tmp = '_';
703
Jean Delvareac551822008-05-02 20:37:21 +0200704 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000705 return 1;
706}
707
Rusty Russell1d8f4302005-12-07 21:40:34 +0100708#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
709
710static void do_input(char *alias,
711 kernel_ulong_t *arr, unsigned int min, unsigned int max)
712{
713 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400714
Andreas Schwab6543bec2013-01-20 17:58:47 +0100715 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
716 arr[i] = TO_NATIVE(arr[i]);
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400717 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200718 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400719 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100720}
721
Dmitry Torokhov09c3e012017-10-22 11:42:29 -0700722/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100723static int do_input_entry(const char *filename, void *symval,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100724 char *alias)
725{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100726 DEF_FIELD(symval, input_device_id, flags);
727 DEF_FIELD(symval, input_device_id, bustype);
728 DEF_FIELD(symval, input_device_id, vendor);
729 DEF_FIELD(symval, input_device_id, product);
730 DEF_FIELD(symval, input_device_id, version);
731 DEF_FIELD_ADDR(symval, input_device_id, evbit);
732 DEF_FIELD_ADDR(symval, input_device_id, keybit);
733 DEF_FIELD_ADDR(symval, input_device_id, relbit);
734 DEF_FIELD_ADDR(symval, input_device_id, absbit);
735 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
736 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
737 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
738 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
739 DEF_FIELD_ADDR(symval, input_device_id, swbit);
740
Rusty Russell1d8f4302005-12-07 21:40:34 +0100741 sprintf(alias, "input:");
742
Andreas Schwab6543bec2013-01-20 17:58:47 +0100743 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
744 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
745 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
746 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100747
748 sprintf(alias + strlen(alias), "-e*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100749 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
750 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100751 sprintf(alias + strlen(alias), "k*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100752 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
753 do_input(alias, *keybit,
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100754 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
755 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100756 sprintf(alias + strlen(alias), "r*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100757 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
758 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100759 sprintf(alias + strlen(alias), "a*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100760 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
761 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100762 sprintf(alias + strlen(alias), "m*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100763 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
764 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100765 sprintf(alias + strlen(alias), "l*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100766 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
767 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100768 sprintf(alias + strlen(alias), "s*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100769 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
770 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100771 sprintf(alias + strlen(alias), "f*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100772 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
773 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100774 sprintf(alias + strlen(alias), "w*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100775 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
776 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100777 return 1;
778}
779
Andreas Schwab6543bec2013-01-20 17:58:47 +0100780static int do_eisa_entry(const char *filename, void *symval,
Michael Tokarev07563c72006-09-27 01:50:56 -0700781 char *alias)
782{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100783 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
784 if (sig[0])
785 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
Jean Delvareac551822008-05-02 20:37:21 +0200786 else
787 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700788 return 1;
789}
790
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500791/* Looks like: parisc:tNhvNrevNsvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100792static int do_parisc_entry(const char *filename, void *symval,
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500793 char *alias)
794{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100795 DEF_FIELD(symval, parisc_device_id, hw_type);
796 DEF_FIELD(symval, parisc_device_id, hversion);
797 DEF_FIELD(symval, parisc_device_id, hversion_rev);
798 DEF_FIELD(symval, parisc_device_id, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500799
800 strcpy(alias, "parisc:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100801 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
802 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
803 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
804 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500805
Jean Delvareac551822008-05-02 20:37:21 +0200806 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500807 return 1;
808}
809
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200810/* Looks like: sdio:cNvNdN. */
811static int do_sdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100812 void *symval, char *alias)
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200813{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100814 DEF_FIELD(symval, sdio_device_id, class);
815 DEF_FIELD(symval, sdio_device_id, vendor);
816 DEF_FIELD(symval, sdio_device_id, device);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200817
818 strcpy(alias, "sdio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100819 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
820 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
821 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
Jean Delvareac551822008-05-02 20:37:21 +0200822 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700823 return 1;
824}
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200825
Michael Buesch61e115a2007-09-18 15:12:50 -0400826/* Looks like: ssb:vNidNrevN. */
827static int do_ssb_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100828 void *symval, char *alias)
Michael Buesch61e115a2007-09-18 15:12:50 -0400829{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100830 DEF_FIELD(symval, ssb_device_id, vendor);
831 DEF_FIELD(symval, ssb_device_id, coreid);
832 DEF_FIELD(symval, ssb_device_id, revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400833
834 strcpy(alias, "ssb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100835 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
836 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
837 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
Jean Delvareac551822008-05-02 20:37:21 +0200838 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200839 return 1;
840}
841
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200842/* Looks like: bcma:mNidNrevNclN. */
843static int do_bcma_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100844 void *symval, char *alias)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200845{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100846 DEF_FIELD(symval, bcma_device_id, manuf);
847 DEF_FIELD(symval, bcma_device_id, id);
848 DEF_FIELD(symval, bcma_device_id, rev);
849 DEF_FIELD(symval, bcma_device_id, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200850
851 strcpy(alias, "bcma:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100852 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
853 ADD(alias, "id", id != BCMA_ANY_ID, id);
854 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
855 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200856 add_wildcard(alias);
857 return 1;
858}
859
Rusty Russellb01d9f22007-10-22 11:03:39 +1000860/* Looks like: virtio:dNvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100861static int do_virtio_entry(const char *filename, void *symval,
Rusty Russellb01d9f22007-10-22 11:03:39 +1000862 char *alias)
863{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100864 DEF_FIELD(symval, virtio_device_id, device);
865 DEF_FIELD(symval, virtio_device_id, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000866
867 strcpy(alias, "virtio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100868 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
869 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000870
Jean Delvareac551822008-05-02 20:37:21 +0200871 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000872 return 1;
873}
874
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700875/*
876 * Looks like: vmbus:guid
877 * Each byte of the guid will be represented by two hex characters
878 * in the name.
879 */
880
Andreas Schwab6543bec2013-01-20 17:58:47 +0100881static int do_vmbus_entry(const char *filename, void *symval,
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700882 char *alias)
883{
884 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100885 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
886 char guid_name[(sizeof(*guid) + 1) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700887
Andreas Schwab6543bec2013-01-20 17:58:47 +0100888 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
K. Y. Srinivasanaf3ff642015-12-14 16:01:43 -0800889 sprintf(&guid_name[i], "%02x", TO_NATIVE((guid->b)[i/2]));
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700890
891 strcpy(alias, "vmbus:");
892 strcat(alias, guid_name);
893
894 return 1;
895}
896
Andrew F. Davis5b7d1272018-04-21 18:55:29 -0500897/* Looks like: rpmsg:S */
898static int do_rpmsg_entry(const char *filename, void *symval,
899 char *alias)
900{
901 DEF_FIELD_ADDR(symval, rpmsg_device_id, name);
902 sprintf(alias, RPMSG_DEVICE_MODALIAS_FMT, *name);
903
904 return 1;
905}
Andrew F. Davis5b7d1272018-04-21 18:55:29 -0500906
Jean Delvared2653e92008-04-29 23:11:39 +0200907/* Looks like: i2c:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100908static int do_i2c_entry(const char *filename, void *symval,
Jean Delvared2653e92008-04-29 23:11:39 +0200909 char *alias)
910{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100911 DEF_FIELD_ADDR(symval, i2c_device_id, name);
912 sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
Jean Delvared2653e92008-04-29 23:11:39 +0200913
914 return 1;
915}
916
Anton Vorontsove0626e32009-09-22 16:46:08 -0700917/* Looks like: spi:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100918static int do_spi_entry(const char *filename, void *symval,
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700919 char *alias)
920{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100921 DEF_FIELD_ADDR(symval, spi_device_id, name);
922 sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700923
924 return 1;
925}
926
David Woodhoused945b692008-09-16 16:23:28 -0700927static const struct dmifield {
928 const char *prefix;
929 int field;
930} dmi_fields[] = {
931 { "bvn", DMI_BIOS_VENDOR },
932 { "bvr", DMI_BIOS_VERSION },
933 { "bd", DMI_BIOS_DATE },
934 { "svn", DMI_SYS_VENDOR },
935 { "pn", DMI_PRODUCT_NAME },
936 { "pvr", DMI_PRODUCT_VERSION },
937 { "rvn", DMI_BOARD_VENDOR },
938 { "rn", DMI_BOARD_NAME },
939 { "rvr", DMI_BOARD_VERSION },
940 { "cvn", DMI_CHASSIS_VENDOR },
941 { "ct", DMI_CHASSIS_TYPE },
942 { "cvr", DMI_CHASSIS_VERSION },
943 { NULL, DMI_NONE }
944};
945
946static void dmi_ascii_filter(char *d, const char *s)
947{
948 /* Filter out characters we don't want to see in the modalias string */
949 for (; *s; s++)
950 if (*s > ' ' && *s < 127 && *s != ':')
951 *(d++) = *s;
952
953 *d = 0;
954}
955
956
Andreas Schwab6543bec2013-01-20 17:58:47 +0100957static int do_dmi_entry(const char *filename, void *symval,
David Woodhoused945b692008-09-16 16:23:28 -0700958 char *alias)
959{
960 int i, j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100961 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
David Woodhoused945b692008-09-16 16:23:28 -0700962 sprintf(alias, "dmi*");
963
964 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
965 for (j = 0; j < 4; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100966 if ((*matches)[j].slot &&
967 (*matches)[j].slot == dmi_fields[i].field) {
David Woodhoused945b692008-09-16 16:23:28 -0700968 sprintf(alias + strlen(alias), ":%s*",
969 dmi_fields[i].prefix);
970 dmi_ascii_filter(alias + strlen(alias),
Andreas Schwab6543bec2013-01-20 17:58:47 +0100971 (*matches)[j].substr);
David Woodhoused945b692008-09-16 16:23:28 -0700972 strcat(alias, "*");
973 }
974 }
975 }
976
977 strcat(alias, ":");
978 return 1;
979}
Eric Miao57fee4a2009-02-04 11:52:40 +0800980
981static int do_platform_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100982 void *symval, char *alias)
Eric Miao57fee4a2009-02-04 11:52:40 +0800983{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100984 DEF_FIELD_ADDR(symval, platform_device_id, name);
985 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
Eric Miao57fee4a2009-02-04 11:52:40 +0800986 return 1;
987}
988
David Woodhouse8626d3b2010-04-02 01:05:27 +0000989static int do_mdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100990 void *symval, char *alias)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000991{
992 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100993 DEF_FIELD(symval, mdio_device_id, phy_id);
994 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
David Woodhouse8626d3b2010-04-02 01:05:27 +0000995
996 alias += sprintf(alias, MDIO_MODULE_PREFIX);
997
998 for (i = 0; i < 32; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100999 if (!((phy_id_mask >> (31-i)) & 1))
David Woodhouse8626d3b2010-04-02 01:05:27 +00001000 *(alias++) = '?';
Andreas Schwab6543bec2013-01-20 17:58:47 +01001001 else if ((phy_id >> (31-i)) & 1)
David Woodhouse8626d3b2010-04-02 01:05:27 +00001002 *(alias++) = '1';
1003 else
1004 *(alias++) = '0';
1005 }
1006
1007 /* Terminate the string */
1008 *alias = 0;
1009
1010 return 1;
1011}
1012
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001013/* Looks like: zorro:iN. */
Andreas Schwab6543bec2013-01-20 17:58:47 +01001014static int do_zorro_entry(const char *filename, void *symval,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001015 char *alias)
1016{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001017 DEF_FIELD(symval, zorro_device_id, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001018 strcpy(alias, "zorro:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001019 ADD(alias, "i", id != ZORRO_WILDCARD, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001020 return 1;
1021}
1022
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001023/* looks like: "pnp:dD" */
1024static int do_isapnp_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001025 void *symval, char *alias)
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001026{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001027 DEF_FIELD(symval, isapnp_device_id, vendor);
1028 DEF_FIELD(symval, isapnp_device_id, function);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001029 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001030 'A' + ((vendor >> 2) & 0x3f) - 1,
1031 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1032 'A' + ((vendor >> 8) & 0x1f) - 1,
1033 (function >> 4) & 0x0f, function & 0x0f,
1034 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001035 return 1;
1036}
1037
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001038/* Looks like: "ipack:fNvNdN". */
1039static int do_ipack_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001040 void *symval, char *alias)
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001041{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001042 DEF_FIELD(symval, ipack_device_id, format);
1043 DEF_FIELD(symval, ipack_device_id, vendor);
1044 DEF_FIELD(symval, ipack_device_id, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001045 strcpy(alias, "ipack:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001046 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1047 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1048 ADD(alias, "d", device != IPACK_ANY_ID, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001049 add_wildcard(alias);
1050 return 1;
1051}
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001052
Dave Martin523817b2011-10-05 14:44:57 +01001053/*
1054 * Append a match expression for a single masked hex digit.
1055 * outp points to a pointer to the character at which to append.
1056 * *outp is updated on return to point just after the appended text,
1057 * to facilitate further appending.
1058 */
1059static void append_nibble_mask(char **outp,
1060 unsigned int nibble, unsigned int mask)
1061{
1062 char *p = *outp;
1063 unsigned int i;
1064
1065 switch (mask) {
1066 case 0:
1067 *p++ = '?';
1068 break;
1069
1070 case 0xf:
1071 p += sprintf(p, "%X", nibble);
1072 break;
1073
1074 default:
1075 /*
1076 * Dumbly emit a match pattern for all possible matching
1077 * digits. This could be improved in some cases using ranges,
1078 * but it has the advantage of being trivially correct, and is
1079 * often optimal.
1080 */
1081 *p++ = '[';
1082 for (i = 0; i < 0x10; i++)
1083 if ((i & mask) == nibble)
1084 p += sprintf(p, "%X", i);
1085 *p++ = ']';
1086 }
1087
1088 /* Ensure that the string remains NUL-terminated: */
1089 *p = '\0';
1090
1091 /* Advance the caller's end-of-string pointer: */
1092 *outp = p;
1093}
1094
1095/*
1096 * looks like: "amba:dN"
1097 *
1098 * N is exactly 8 digits, where each is an upper-case hex digit, or
1099 * a ? or [] pattern matching exactly one digit.
1100 */
1101static int do_amba_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001102 void *symval, char *alias)
Dave Martin523817b2011-10-05 14:44:57 +01001103{
1104 unsigned int digit;
1105 char *p = alias;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001106 DEF_FIELD(symval, amba_id, id);
1107 DEF_FIELD(symval, amba_id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001108
Andreas Schwab6543bec2013-01-20 17:58:47 +01001109 if ((id & mask) != id)
Dave Martin523817b2011-10-05 14:44:57 +01001110 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1111 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001112 filename, id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001113
1114 p += sprintf(alias, "amba:d");
1115 for (digit = 0; digit < 8; digit++)
1116 append_nibble_mask(&p,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001117 (id >> (4 * (7 - digit))) & 0xf,
1118 (mask >> (4 * (7 - digit))) & 0xf);
Dave Martin523817b2011-10-05 14:44:57 +01001119
1120 return 1;
1121}
1122
James Hogan8286ae02015-03-25 15:39:50 +00001123/*
1124 * looks like: "mipscdmm:tN"
1125 *
1126 * N is exactly 2 digits, where each is an upper-case hex digit, or
1127 * a ? or [] pattern matching exactly one digit.
1128 */
1129static int do_mips_cdmm_entry(const char *filename,
1130 void *symval, char *alias)
1131{
1132 DEF_FIELD(symval, mips_cdmm_device_id, type);
1133
1134 sprintf(alias, "mipscdmm:t%02X*", type);
1135 return 1;
1136}
James Hogan8286ae02015-03-25 15:39:50 +00001137
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001138/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
Andi Kleen644e9cb2012-01-26 00:09:05 +01001139 * All fields are numbers. It would be nicer to use strings for vendor
1140 * and feature, but getting those out of the build system here is too
1141 * complicated.
1142 */
1143
Andreas Schwab6543bec2013-01-20 17:58:47 +01001144static int do_x86cpu_entry(const char *filename, void *symval,
Andi Kleen644e9cb2012-01-26 00:09:05 +01001145 char *alias)
1146{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001147 DEF_FIELD(symval, x86_cpu_id, feature);
1148 DEF_FIELD(symval, x86_cpu_id, family);
1149 DEF_FIELD(symval, x86_cpu_id, model);
1150 DEF_FIELD(symval, x86_cpu_id, vendor);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001151
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001152 strcpy(alias, "cpu:type:x86,");
1153 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1154 ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1155 ADD(alias, "mod", model != X86_MODEL_ANY, model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001156 strcat(alias, ":feature:*");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001157 if (feature != X86_FEATURE_ANY)
1158 sprintf(alias + strlen(alias), "%04X*", feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001159 return 1;
1160}
Andi Kleen644e9cb2012-01-26 00:09:05 +01001161
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001162/* LOOKS like cpu:type:*:feature:*FEAT* */
1163static int do_cpu_entry(const char *filename, void *symval, char *alias)
1164{
1165 DEF_FIELD(symval, cpu_feature, feature);
1166
1167 sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1168 return 1;
1169}
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001170
Tomas Winklerb26864c2015-09-10 10:18:01 +03001171/* Looks like: mei:S:uuid:N:* */
Samuel Ortize5354102013-03-27 17:29:53 +02001172static int do_mei_entry(const char *filename, void *symval,
1173 char *alias)
1174{
1175 DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001176 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001177 DEF_FIELD(symval, mei_cl_device_id, version);
Samuel Ortize5354102013-03-27 17:29:53 +02001178
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001179 sprintf(alias, MEI_CL_MODULE_PREFIX);
1180 sprintf(alias + strlen(alias), "%s:", (*name)[0] ? *name : "*");
1181 add_uuid(alias, *uuid);
Tomas Winklerb26864c2015-09-10 10:18:01 +03001182 ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001183
1184 strcat(alias, ":*");
Samuel Ortize5354102013-03-27 17:29:53 +02001185
1186 return 1;
1187}
Samuel Ortize5354102013-03-27 17:29:53 +02001188
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001189/* Looks like: rapidio:vNdNavNadN */
1190static int do_rio_entry(const char *filename,
1191 void *symval, char *alias)
1192{
1193 DEF_FIELD(symval, rio_device_id, did);
1194 DEF_FIELD(symval, rio_device_id, vid);
1195 DEF_FIELD(symval, rio_device_id, asm_did);
1196 DEF_FIELD(symval, rio_device_id, asm_vid);
1197
1198 strcpy(alias, "rapidio:");
1199 ADD(alias, "v", vid != RIO_ANY_ID, vid);
1200 ADD(alias, "d", did != RIO_ANY_ID, did);
1201 ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1202 ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1203
1204 add_wildcard(alias);
1205 return 1;
1206}
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001207
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001208/* Looks like: ulpi:vNpN */
1209static int do_ulpi_entry(const char *filename, void *symval,
1210 char *alias)
1211{
1212 DEF_FIELD(symval, ulpi_device_id, vendor);
1213 DEF_FIELD(symval, ulpi_device_id, product);
1214
1215 sprintf(alias, "ulpi:v%04xp%04x", vendor, product);
1216
1217 return 1;
1218}
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001219
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301220/* Looks like: hdaudio:vNrNaN */
1221static int do_hda_entry(const char *filename, void *symval, char *alias)
1222{
1223 DEF_FIELD(symval, hda_device_id, vendor_id);
1224 DEF_FIELD(symval, hda_device_id, rev_id);
1225 DEF_FIELD(symval, hda_device_id, api_version);
1226
1227 strcpy(alias, "hdaudio:");
1228 ADD(alias, "v", vendor_id != 0, vendor_id);
1229 ADD(alias, "r", rev_id != 0, rev_id);
1230 ADD(alias, "a", api_version != 0, api_version);
1231
1232 add_wildcard(alias);
1233 return 1;
1234}
Subhransu S. Prustyda23ac12015-09-29 13:56:10 +05301235
Vinod Koul92513452017-12-14 11:19:33 +05301236/* Looks like: sdw:mNpN */
1237static int do_sdw_entry(const char *filename, void *symval, char *alias)
1238{
1239 DEF_FIELD(symval, sdw_device_id, mfg_id);
1240 DEF_FIELD(symval, sdw_device_id, part_id);
1241
1242 strcpy(alias, "sdw:");
1243 ADD(alias, "m", mfg_id != 0, mfg_id);
1244 ADD(alias, "p", part_id != 0, part_id);
1245
1246 add_wildcard(alias);
1247 return 1;
1248}
Vinod Koul92513452017-12-14 11:19:33 +05301249
Stuart Yoder0afef452016-06-22 16:40:45 -05001250/* Looks like: fsl-mc:vNdN */
1251static int do_fsl_mc_entry(const char *filename, void *symval,
1252 char *alias)
1253{
1254 DEF_FIELD(symval, fsl_mc_device_id, vendor);
1255 DEF_FIELD_ADDR(symval, fsl_mc_device_id, obj_type);
1256
1257 sprintf(alias, "fsl-mc:v%08Xd%s", vendor, *obj_type);
1258 return 1;
1259}
Stuart Yoder0afef452016-06-22 16:40:45 -05001260
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001261/* Looks like: tbsvc:kSpNvNrN */
1262static int do_tbsvc_entry(const char *filename, void *symval, char *alias)
1263{
1264 DEF_FIELD(symval, tb_service_id, match_flags);
1265 DEF_FIELD_ADDR(symval, tb_service_id, protocol_key);
1266 DEF_FIELD(symval, tb_service_id, protocol_id);
1267 DEF_FIELD(symval, tb_service_id, protocol_version);
1268 DEF_FIELD(symval, tb_service_id, protocol_revision);
1269
1270 strcpy(alias, "tbsvc:");
1271 if (match_flags & TBSVC_MATCH_PROTOCOL_KEY)
1272 sprintf(alias + strlen(alias), "k%s", *protocol_key);
1273 else
1274 strcat(alias + strlen(alias), "k*");
1275 ADD(alias, "p", match_flags & TBSVC_MATCH_PROTOCOL_ID, protocol_id);
1276 ADD(alias, "v", match_flags & TBSVC_MATCH_PROTOCOL_VERSION,
1277 protocol_version);
1278 ADD(alias, "r", match_flags & TBSVC_MATCH_PROTOCOL_REVISION,
1279 protocol_revision);
1280
1281 add_wildcard(alias);
1282 return 1;
1283}
Mika Westerbergd1ff7022017-10-02 13:38:34 +03001284
Heikki Krogerus8a37d872018-06-27 18:19:50 +03001285/* Looks like: typec:idNmN */
1286static int do_typec_entry(const char *filename, void *symval, char *alias)
1287{
1288 DEF_FIELD(symval, typec_device_id, svid);
1289 DEF_FIELD(symval, typec_device_id, mode);
1290
1291 sprintf(alias, "typec:id%04X", svid);
1292 ADD(alias, "m", mode != TYPEC_ANY_MODE, mode);
1293
1294 return 1;
1295}
Heikki Krogerus8a37d872018-06-27 18:19:50 +03001296
Sumit Garg0fc1db92019-01-29 11:19:35 +05301297/* Looks like: tee:uuid */
1298static int do_tee_entry(const char *filename, void *symval, char *alias)
1299{
1300 DEF_FIELD(symval, tee_client_device_id, uuid);
1301
1302 sprintf(alias, "tee:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1303 uuid.b[0], uuid.b[1], uuid.b[2], uuid.b[3], uuid.b[4],
1304 uuid.b[5], uuid.b[6], uuid.b[7], uuid.b[8], uuid.b[9],
1305 uuid.b[10], uuid.b[11], uuid.b[12], uuid.b[13], uuid.b[14],
1306 uuid.b[15]);
1307
1308 add_wildcard(alias);
1309 return 1;
1310}
1311
Mattias Jacobsson0bc44b22019-02-19 20:59:50 +01001312/* Looks like: wmi:guid */
1313static int do_wmi_entry(const char *filename, void *symval, char *alias)
1314{
1315 int len;
1316 DEF_FIELD_ADDR(symval, wmi_device_id, guid_string);
1317
1318 if (strlen(*guid_string) != UUID_STRING_LEN) {
1319 warn("Invalid WMI device id 'wmi:%s' in '%s'\n",
1320 *guid_string, filename);
1321 return 0;
1322 }
1323
1324 len = snprintf(alias, ALIAS_SIZE, WMI_MODULE_PREFIX "%s", *guid_string);
1325 if (len < 0 || len >= ALIAS_SIZE) {
1326 warn("Could not generate all MODULE_ALIAS's in '%s'\n",
1327 filename);
1328 return 0;
1329 }
1330 return 1;
1331}
1332
Rusty Russell626596e2012-01-13 09:32:15 +10301333/* Does namelen bytes of name exactly match the symbol? */
1334static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Rusty Russell626596e2012-01-13 09:32:15 +10301336 if (namelen != strlen(symbol))
1337 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Rusty Russell626596e2012-01-13 09:32:15 +10301339 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342static void do_table(void *symval, unsigned long size,
1343 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001344 const char *device_id,
Masahiro Yamadaf880eea2018-11-22 13:28:42 +09001345 int (*do_entry)(const char *filename, void *symval, char *alias),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 struct module *mod)
1347{
1348 unsigned int i;
Mattias Jacobsson841f1b82019-02-07 13:30:22 +01001349 char alias[ALIAS_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Kees Cooke0049822007-09-16 11:15:46 +02001351 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 /* Leave last one: it's the terminator. */
1353 size -= id_size;
1354
1355 for (i = 0; i < size; i += id_size) {
1356 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 buf_printf(&mod->dev_table_buf,
1358 "MODULE_ALIAS(\"%s\");\n", alias);
1359 }
1360 }
1361}
1362
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001363static const struct devtable devtable[] = {
1364 {"hid", SIZE_hid_device_id, do_hid_entry},
1365 {"ieee1394", SIZE_ieee1394_device_id, do_ieee1394_entry},
1366 {"pci", SIZE_pci_device_id, do_pci_entry},
1367 {"ccw", SIZE_ccw_device_id, do_ccw_entry},
1368 {"ap", SIZE_ap_device_id, do_ap_entry},
1369 {"css", SIZE_css_device_id, do_css_entry},
1370 {"serio", SIZE_serio_device_id, do_serio_entry},
1371 {"acpi", SIZE_acpi_device_id, do_acpi_entry},
1372 {"pcmcia", SIZE_pcmcia_device_id, do_pcmcia_entry},
1373 {"vio", SIZE_vio_device_id, do_vio_entry},
1374 {"input", SIZE_input_device_id, do_input_entry},
1375 {"eisa", SIZE_eisa_device_id, do_eisa_entry},
1376 {"parisc", SIZE_parisc_device_id, do_parisc_entry},
1377 {"sdio", SIZE_sdio_device_id, do_sdio_entry},
1378 {"ssb", SIZE_ssb_device_id, do_ssb_entry},
1379 {"bcma", SIZE_bcma_device_id, do_bcma_entry},
1380 {"virtio", SIZE_virtio_device_id, do_virtio_entry},
1381 {"vmbus", SIZE_hv_vmbus_device_id, do_vmbus_entry},
1382 {"rpmsg", SIZE_rpmsg_device_id, do_rpmsg_entry},
1383 {"i2c", SIZE_i2c_device_id, do_i2c_entry},
1384 {"spi", SIZE_spi_device_id, do_spi_entry},
1385 {"dmi", SIZE_dmi_system_id, do_dmi_entry},
1386 {"platform", SIZE_platform_device_id, do_platform_entry},
1387 {"mdio", SIZE_mdio_device_id, do_mdio_entry},
1388 {"zorro", SIZE_zorro_device_id, do_zorro_entry},
1389 {"isapnp", SIZE_isapnp_device_id, do_isapnp_entry},
1390 {"ipack", SIZE_ipack_device_id, do_ipack_entry},
1391 {"amba", SIZE_amba_id, do_amba_entry},
1392 {"mipscdmm", SIZE_mips_cdmm_device_id, do_mips_cdmm_entry},
1393 {"x86cpu", SIZE_x86_cpu_id, do_x86cpu_entry},
1394 {"cpu", SIZE_cpu_feature, do_cpu_entry},
1395 {"mei", SIZE_mei_cl_device_id, do_mei_entry},
1396 {"rapidio", SIZE_rio_device_id, do_rio_entry},
1397 {"ulpi", SIZE_ulpi_device_id, do_ulpi_entry},
1398 {"hdaudio", SIZE_hda_device_id, do_hda_entry},
1399 {"sdw", SIZE_sdw_device_id, do_sdw_entry},
1400 {"fslmc", SIZE_fsl_mc_device_id, do_fsl_mc_entry},
1401 {"tbsvc", SIZE_tb_service_id, do_tbsvc_entry},
1402 {"typec", SIZE_typec_device_id, do_typec_entry},
Sumit Garg0fc1db92019-01-29 11:19:35 +05301403 {"tee", SIZE_tee_client_device_id, do_tee_entry},
Mattias Jacobsson0bc44b22019-02-19 20:59:50 +01001404 {"wmi", SIZE_wmi_device_id, do_wmi_entry},
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001405};
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407/* Create MODULE_ALIAS() statements.
1408 * At this time, we cannot write the actual output C source yet,
1409 * so we write into the mod->dev_table_buf buffer. */
1410void handle_moddevtable(struct module *mod, struct elf_info *info,
1411 Elf_Sym *sym, const char *symname)
1412{
1413 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001414 char *zeros = NULL;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301415 const char *name, *identifier;
Rusty Russell626596e2012-01-13 09:32:15 +10301416 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001419 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 return;
1421
David Millere88aa7b2012-04-12 14:37:30 -04001422 /* We're looking for an object */
1423 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1424 return;
1425
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001426 /* All our symbols are of form __mod_<name>__<identifier>_device_table. */
1427 if (strncmp(symname, "__mod_", strlen("__mod_")))
Rusty Russell626596e2012-01-13 09:32:15 +10301428 return;
Masahiro Yamada153e04b2018-09-28 15:21:55 +09001429 name = symname + strlen("__mod_");
Rusty Russell626596e2012-01-13 09:32:15 +10301430 namelen = strlen(name);
1431 if (namelen < strlen("_device_table"))
1432 return;
1433 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1434 return;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301435 identifier = strstr(name, "__");
1436 if (!identifier)
1437 return;
1438 namelen = identifier - name;
Rusty Russell626596e2012-01-13 09:32:15 +10301439
Kees Cooke0049822007-09-16 11:15:46 +02001440 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001441 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001442 zeros = calloc(1, sym->st_size);
1443 symval = zeros;
1444 } else {
1445 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001446 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001447 + sym->st_value;
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Rusty Russell626596e2012-01-13 09:32:15 +10301450 /* First handle the "special" cases */
1451 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001452 do_usb_table(symval, sym->st_size, mod);
Philipp Zabelacbef7b2016-05-05 16:22:29 -07001453 if (sym_is(name, namelen, "of"))
1454 do_of_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301455 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001456 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301457 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001458 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301459 else {
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001460 int i;
Rusty Russell626596e2012-01-13 09:32:15 +10301461
Masahiro Yamadaec91e78d2018-11-22 13:28:41 +09001462 for (i = 0; i < ARRAY_SIZE(devtable); i++) {
1463 const struct devtable *p = &devtable[i];
1464
1465 if (sym_is(name, namelen, p->device_id)) {
1466 do_table(symval, sym->st_size, p->id_size,
Masahiro Yamadaf880eea2018-11-22 13:28:42 +09001467 p->device_id, p->do_entry, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301468 break;
1469 }
1470 }
1471 }
Kees Cooke0049822007-09-16 11:15:46 +02001472 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
1475/* Now add out buffered information to the generated C source */
1476void add_moddevtable(struct buffer *buf, struct module *mod)
1477{
1478 buf_printf(buf, "\n");
1479 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1480 free(mod->dev_table_buf.p);
1481}