blob: 62c517f4b5923a1f2efedcb7bb2e0ecd1bde5500 [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;
37
38/* Big exception to the "don't include kernel headers into userspace, which
Sam Ravnborg62070fa2006-03-03 16:46:04 +010039 * even potentially has different endianness and word sizes, since
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 * we handle those differences explicitly below */
41#include "../../include/linux/mod_devicetable.h"
42
Rusty Russelle49ce142012-01-13 09:32:16 +103043/* This array collects all instances that use the generic do_table */
44struct devtable {
Tom Gundersen21bdd172014-02-03 11:14:13 +103045 const char *device_id; /* name of table, __mod_<name>__*_device_table. */
Rusty Russelle49ce142012-01-13 09:32:16 +103046 unsigned long id_size;
47 void *function;
48};
49
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010050#define ___cat(a,b) a ## b
51#define __cat(a,b) ___cat(a,b)
52
53/* we need some special handling for this host tool running eventually on
54 * Darwin. The Mach-O section handling is a bit different than ELF section
55 * handling. The differnces in detail are:
56 * a) we have segments which have sections
57 * b) we need a API call to get the respective section symbols */
58#if defined(__MACH__)
59#include <mach-o/getsect.h>
60
61#define INIT_SECTION(name) do { \
62 unsigned long name ## _len; \
63 char *__cat(pstart_,name) = getsectdata("__TEXT", \
64 #name, &__cat(name,_len)); \
65 char *__cat(pstop_,name) = __cat(pstart_,name) + \
66 __cat(name, _len); \
67 __cat(__start_,name) = (void *)__cat(pstart_,name); \
68 __cat(__stop_,name) = (void *)__cat(pstop_,name); \
69 } while (0)
70#define SECTION(name) __attribute__((section("__TEXT, " #name)))
71
72struct devtable **__start___devtable, **__stop___devtable;
73#else
74#define INIT_SECTION(name) /* no-op for ELF */
75#define SECTION(name) __attribute__((section(#name)))
76
Rusty Russelle49ce142012-01-13 09:32:16 +103077/* We construct a table of pointers in an ELF section (pointers generally
78 * go unpadded by gcc). ld creates boundary syms for us. */
79extern struct devtable *__start___devtable[], *__stop___devtable[];
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010080#endif /* __MACH__ */
Rusty Russelle49ce142012-01-13 09:32:16 +103081
Daniel Tang04130cc2013-06-09 12:33:55 +100082#if !defined(__used)
83# if __GNUC__ == 3 && __GNUC_MINOR__ < 3
84# define __used __attribute__((__unused__))
85# else
86# define __used __attribute__((__used__))
87# endif
Rusty Russelle49ce142012-01-13 09:32:16 +103088#endif
89
Andreas Schwab6543bec2013-01-20 17:58:47 +010090/* Define a variable f that holds the value of field f of struct devid
91 * based at address m.
92 */
93#define DEF_FIELD(m, devid, f) \
94 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
95/* Define a variable f that holds the address of field f of struct devid
96 * based at address m. Due to the way typeof works, for a field of type
97 * T[N] the variable has type T(*)[N], _not_ T*.
98 */
99#define DEF_FIELD_ADDR(m, devid, f) \
100 typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
101
Rusty Russelle49ce142012-01-13 09:32:16 +1030102/* Add a table entry. We test function type matches while we're here. */
103#define ADD_TO_DEVTABLE(device_id, type, function) \
104 static struct devtable __cat(devtable,__LINE__) = { \
105 device_id + 0*sizeof((function)((const char *)NULL, \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100106 (void *)NULL, \
Rusty Russelle49ce142012-01-13 09:32:16 +1030107 (char *)NULL)), \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100108 SIZE_##type, (function) }; \
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +0100109 static struct devtable *SECTION(__devtable) __used \
110 __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
Rusty Russelle49ce142012-01-13 09:32:16 +1030111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112#define ADD(str, sep, cond, field) \
113do { \
114 strcat(str, sep); \
115 if (cond) \
116 sprintf(str + strlen(str), \
117 sizeof(field) == 1 ? "%02X" : \
118 sizeof(field) == 2 ? "%04X" : \
119 sizeof(field) == 4 ? "%08X" : "", \
120 field); \
121 else \
122 sprintf(str + strlen(str), "*"); \
123} while(0)
124
Jean Delvareac551822008-05-02 20:37:21 +0200125/* Always end in a wildcard, for future extension */
126static inline void add_wildcard(char *str)
127{
128 int len = strlen(str);
129
130 if (str[len - 1] != '*')
131 strcat(str + len, "*");
132}
133
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300134static inline void add_uuid(char *str, __u8 uuid[16])
135{
136 int len = strlen(str);
137 int i;
138
139 for (i = 0; i < 16; i++)
140 sprintf(str + len + (i << 1), "%02x", uuid[i]);
141}
142
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200143/**
144 * Check that sizeof(device_id type) are consistent with size of section
145 * in .o file. If in-consistent then userspace and kernel does not agree
146 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200147 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100148 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200149 **/
Kees Cooke0049822007-09-16 11:15:46 +0200150static void device_id_check(const char *modname, const char *device_id,
151 unsigned long size, unsigned long id_size,
152 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200153{
Kees Cooke0049822007-09-16 11:15:46 +0200154 int i;
155
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200156 if (size % id_size || size < id_size) {
157 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
Tom Gundersen21bdd172014-02-03 11:14:13 +1030158 "of the size of "
159 "section __mod_%s__<identifier>_device_table=%lu.\n"
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200160 "Fix definition of struct %s_device_id "
161 "in mod_devicetable.h\n",
162 modname, device_id, id_size, device_id, size, device_id);
163 }
Kees Cooke0049822007-09-16 11:15:46 +0200164 /* Verify last one is a terminator */
165 for (i = 0; i < id_size; i++ ) {
166 if (*(uint8_t*)(symval+size-id_size+i)) {
167 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
168 "The last of %lu is:\n",
169 modname, device_id, id_size, size / id_size);
170 for (i = 0; i < id_size; i++ )
171 fprintf(stderr,"0x%02x ",
172 *(uint8_t*)(symval+size-id_size+i) );
173 fprintf(stderr,"\n");
174 fatal("%s: struct %s_device_id is not terminated "
175 "with a NULL entry!\n", modname, device_id);
176 }
177 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200178}
179
Roman Kaganb19dcd92005-04-22 15:07:01 -0700180/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200181/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100182static void do_usb_entry(void *symval,
Roman Kaganb19dcd92005-04-22 15:07:01 -0700183 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
184 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500185 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700187 char alias[500];
Andreas Schwab6543bec2013-01-20 17:58:47 +0100188 DEF_FIELD(symval, usb_device_id, match_flags);
189 DEF_FIELD(symval, usb_device_id, idVendor);
190 DEF_FIELD(symval, usb_device_id, idProduct);
191 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
192 DEF_FIELD(symval, usb_device_id, bDeviceClass);
193 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
194 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
195 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
196 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
197 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
198 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 strcpy(alias, "usb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100201 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
202 idVendor);
203 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
204 idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700205
206 strcat(alias, "d");
207 if (bcdDevice_initial_digits)
208 sprintf(alias + strlen(alias), "%0*X",
209 bcdDevice_initial_digits, bcdDevice_initial);
210 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500211 sprintf(alias + strlen(alias), "%X", range_lo);
212 else if (range_lo > 0 || range_hi < max) {
213 if (range_lo > 0x9 || range_hi < 0xA)
214 sprintf(alias + strlen(alias),
215 "[%X-%X]",
216 range_lo,
217 range_hi);
218 else {
219 sprintf(alias + strlen(alias),
220 range_lo < 0x9 ? "[%X-9" : "[%X",
221 range_lo);
222 sprintf(alias + strlen(alias),
Jan Moskyto Matejka03b56322014-02-07 19:15:11 +0100223 range_hi > 0xA ? "A-%X]" : "%X]",
224 range_hi);
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500225 }
226 }
Andreas Schwab6543bec2013-01-20 17:58:47 +0100227 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700228 strcat(alias, "*");
229
Andreas Schwab6543bec2013-01-20 17:58:47 +0100230 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
231 bDeviceClass);
232 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
233 bDeviceSubClass);
234 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
235 bDeviceProtocol);
236 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
237 bInterfaceClass);
238 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
239 bInterfaceSubClass);
240 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
241 bInterfaceProtocol);
242 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
243 bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700244
Jean Delvareac551822008-05-02 20:37:21 +0200245 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700246 buf_printf(&mod->dev_table_buf,
247 "MODULE_ALIAS(\"%s\");\n", alias);
248}
249
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500250/* Handles increment/decrement of BCD formatted integers */
251/* Returns the previous value, so it works like i++ or i-- */
252static unsigned int incbcd(unsigned int *bcd,
253 int inc,
254 unsigned char max,
255 size_t chars)
256{
257 unsigned int init = *bcd, i, j;
258 unsigned long long c, dec = 0;
259
260 /* If bcd is not in BCD format, just increment */
261 if (max > 0x9) {
262 *bcd += inc;
263 return init;
264 }
265
266 /* Convert BCD to Decimal */
267 for (i=0 ; i < chars ; i++) {
268 c = (*bcd >> (i << 2)) & 0xf;
269 c = c > 9 ? 9 : c; /* force to bcd just in case */
270 for (j=0 ; j < i ; j++)
271 c = c * 10;
272 dec += c;
273 }
274
275 /* Do our increment/decrement */
276 dec += inc;
277 *bcd = 0;
278
279 /* Convert back to BCD */
280 for (i=0 ; i < chars ; i++) {
281 for (c=1,j=0 ; j < i ; j++)
282 c = c * 10;
283 c = (dec / c) % 10;
284 *bcd += c << (i << 2);
285 }
286 return init;
287}
288
Andreas Schwab6543bec2013-01-20 17:58:47 +0100289static void do_usb_entry_multi(void *symval, struct module *mod)
Roman Kaganb19dcd92005-04-22 15:07:01 -0700290{
291 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500292 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700293 int ndigits;
294
Andreas Schwab6543bec2013-01-20 17:58:47 +0100295 DEF_FIELD(symval, usb_device_id, match_flags);
296 DEF_FIELD(symval, usb_device_id, idVendor);
297 DEF_FIELD(symval, usb_device_id, idProduct);
298 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
299 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
300 DEF_FIELD(symval, usb_device_id, bDeviceClass);
301 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700302
Andreas Schwab6543bec2013-01-20 17:58:47 +0100303 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
304 bcdDevice_lo : 0x0U;
305 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
306 bcdDevice_hi : ~0x0U;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700307
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500308 /* Figure out if this entry is in bcd or hex format */
309 max = 0x9; /* Default to decimal format */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100310 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500311 clo = (devlo >> (ndigits << 2)) & 0xf;
312 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
313 if (clo > max || chi > max) {
314 max = 0xf;
315 break;
316 }
317 }
318
Roman Kaganb19dcd92005-04-22 15:07:01 -0700319 /*
320 * Some modules (visor) have empty slots as placeholder for
321 * run-time specification that results in catch-all alias
322 */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100323 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700324 return;
325
326 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100327 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
Roman Kaganb19dcd92005-04-22 15:07:01 -0700328 clo = devlo & 0xf;
329 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500330 if (chi > max) /* If we are in bcd mode, truncate if necessary */
331 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700332 devlo >>= 4;
333 devhi >>= 4;
334
335 if (devlo == devhi || !ndigits) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100336 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700337 break;
338 }
339
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500340 if (clo > 0x0)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100341 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500342 incbcd(&devlo, 1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100343 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500344 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700345
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500346 if (chi < max)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100347 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500348 incbcd(&devhi, -1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100349 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500350 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700351 }
352}
353
354static void do_usb_table(void *symval, unsigned long size,
355 struct module *mod)
356{
357 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100358 const unsigned long id_size = SIZE_usb_device_id;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700359
Kees Cooke0049822007-09-16 11:15:46 +0200360 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200361
Roman Kaganb19dcd92005-04-22 15:07:01 -0700362 /* Leave last one: it's the terminator. */
363 size -= id_size;
364
365 for (i = 0; i < size; i += id_size)
366 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Jiri Slabye8c84f92008-05-19 15:50:01 +0200369/* Looks like: hid:bNvNpN */
370static int do_hid_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100371 void *symval, char *alias)
Jiri Slabye8c84f92008-05-19 15:50:01 +0200372{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100373 DEF_FIELD(symval, hid_device_id, bus);
374 DEF_FIELD(symval, hid_device_id, group);
375 DEF_FIELD(symval, hid_device_id, vendor);
376 DEF_FIELD(symval, hid_device_id, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200377
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200378 sprintf(alias, "hid:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100379 ADD(alias, "b", bus != HID_BUS_ANY, bus);
380 ADD(alias, "g", group != HID_GROUP_ANY, group);
381 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
382 ADD(alias, "p", product != HID_ANY_ID, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200383
384 return 1;
385}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100386ADD_TO_DEVTABLE("hid", hid_device_id, do_hid_entry);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/* Looks like: ieee1394:venNmoNspNverN */
389static int do_ieee1394_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100390 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100392 DEF_FIELD(symval, ieee1394_device_id, match_flags);
393 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
394 DEF_FIELD(symval, ieee1394_device_id, model_id);
395 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
396 DEF_FIELD(symval, ieee1394_device_id, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 strcpy(alias, "ieee1394:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100399 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
400 vendor_id);
401 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
402 model_id);
403 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
404 specifier_id);
405 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
406 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Jean Delvareac551822008-05-02 20:37:21 +0200408 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return 1;
410}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100411ADD_TO_DEVTABLE("ieee1394", ieee1394_device_id, do_ieee1394_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
414static int do_pci_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100415 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 /* Class field can be divided into these three. */
418 unsigned char baseclass, subclass, interface,
419 baseclass_mask, subclass_mask, interface_mask;
420
Andreas Schwab6543bec2013-01-20 17:58:47 +0100421 DEF_FIELD(symval, pci_device_id, vendor);
422 DEF_FIELD(symval, pci_device_id, device);
423 DEF_FIELD(symval, pci_device_id, subvendor);
424 DEF_FIELD(symval, pci_device_id, subdevice);
425 DEF_FIELD(symval, pci_device_id, class);
426 DEF_FIELD(symval, pci_device_id, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 strcpy(alias, "pci:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100429 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
430 ADD(alias, "d", device != PCI_ANY_ID, device);
431 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
432 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Andreas Schwab6543bec2013-01-20 17:58:47 +0100434 baseclass = (class) >> 16;
435 baseclass_mask = (class_mask) >> 16;
436 subclass = (class) >> 8;
437 subclass_mask = (class_mask) >> 8;
438 interface = class;
439 interface_mask = class_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
442 || (subclass_mask != 0 && subclass_mask != 0xFF)
443 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100444 warn("Can't handle masks in %s:%04X\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100445 filename, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
447 }
448
449 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
450 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
451 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200452 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return 1;
454}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100455ADD_TO_DEVTABLE("pci", pci_device_id, do_pci_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100457/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static int do_ccw_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100459 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100461 DEF_FIELD(symval, ccw_device_id, match_flags);
462 DEF_FIELD(symval, ccw_device_id, cu_type);
463 DEF_FIELD(symval, ccw_device_id, cu_model);
464 DEF_FIELD(symval, ccw_device_id, dev_type);
465 DEF_FIELD(symval, ccw_device_id, dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 strcpy(alias, "ccw:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100468 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
469 cu_type);
470 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
471 cu_model);
472 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
473 dev_type);
474 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
475 dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200476 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return 1;
478}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100479ADD_TO_DEVTABLE("ccw", ccw_device_id, do_ccw_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200481/* looks like: "ap:tN" */
482static int do_ap_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100483 void *symval, char *alias)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200484{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100485 DEF_FIELD(symval, ap_device_id, dev_type);
486
487 sprintf(alias, "ap:t%02X*", dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200488 return 1;
489}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100490ADD_TO_DEVTABLE("ap", ap_device_id, do_ap_entry);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200491
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200492/* looks like: "css:tN" */
493static int do_css_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100494 void *symval, char *alias)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200495{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100496 DEF_FIELD(symval, css_device_id, type);
497
498 sprintf(alias, "css:t%01X", type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200499 return 1;
500}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100501ADD_TO_DEVTABLE("css", css_device_id, do_css_entry);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503/* Looks like: "serio:tyNprNidNexN" */
504static int do_serio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100505 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100507 DEF_FIELD(symval, serio_device_id, type);
508 DEF_FIELD(symval, serio_device_id, proto);
509 DEF_FIELD(symval, serio_device_id, id);
510 DEF_FIELD(symval, serio_device_id, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 strcpy(alias, "serio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100513 ADD(alias, "ty", type != SERIO_ANY, type);
514 ADD(alias, "pr", proto != SERIO_ANY, proto);
515 ADD(alias, "id", id != SERIO_ANY, id);
516 ADD(alias, "ex", extra != SERIO_ANY, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jean Delvareac551822008-05-02 20:37:21 +0200518 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 1;
520}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100521ADD_TO_DEVTABLE("serio", serio_device_id, do_serio_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Thomas Renninger29b71a12007-07-23 14:43:51 +0200523/* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
524static int do_acpi_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100525 void *symval, char *alias)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200526{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100527 DEF_FIELD_ADDR(symval, acpi_device_id, id);
528 sprintf(alias, "acpi*:%s:*", *id);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200529 return 1;
530}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100531ADD_TO_DEVTABLE("acpi", acpi_device_id, do_acpi_entry);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200534static void do_pnp_device_entry(void *symval, unsigned long size,
535 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100537 const unsigned long id_size = SIZE_pnp_device_id;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200538 const unsigned int count = (size / id_size)-1;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200539 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200540
541 device_id_check(mod->name, "pnp", size, id_size, symval);
542
Kay Sievers5e4c6562008-08-21 15:28:56 +0200543 for (i = 0; i < count; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100544 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
545 char acpi_id[sizeof(*id)];
Kay Sievers72638f52009-01-08 03:06:42 +0100546 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200547
548 buf_printf(&mod->dev_table_buf,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100549 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
Kay Sievers72638f52009-01-08 03:06:42 +0100550
551 /* fix broken pnp bus lowercasing */
552 for (j = 0; j < sizeof(acpi_id); j++)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100553 acpi_id[j] = toupper((*id)[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200554 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100555 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Kay Sievers0c81eed2008-02-21 00:35:54 +0100559/* looks like: "pnp:dD" for every device of the card */
560static void do_pnp_card_entries(void *symval, unsigned long size,
561 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100563 const unsigned long id_size = SIZE_pnp_card_device_id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100564 const unsigned int count = (size / id_size)-1;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100565 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Kay Sievers0c81eed2008-02-21 00:35:54 +0100567 device_id_check(mod->name, "pnp", size, id_size, symval);
568
569 for (i = 0; i < count; i++) {
570 unsigned int j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100571 DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100572
573 for (j = 0; j < PNP_MAX_DEVICES; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100574 const char *id = (char *)(*devs)[j].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100575 int i2, j2;
576 int dup = 0;
577
578 if (!id[0])
579 break;
580
581 /* find duplicate, already added value */
582 for (i2 = 0; i2 < i && !dup; i2++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100583 DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100584
585 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100586 const char *id2 = (char *)(*devs)[j2].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100587
588 if (!id2[0])
589 break;
590
591 if (!strcmp(id, id2)) {
592 dup = 1;
593 break;
594 }
595 }
596 }
597
598 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200599 if (!dup) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100600 char acpi_id[PNP_ID_LEN];
Kay Sievers72638f52009-01-08 03:06:42 +0100601 int k;
602
Kay Sievers0c81eed2008-02-21 00:35:54 +0100603 buf_printf(&mod->dev_table_buf,
604 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100605
606 /* fix broken pnp bus lowercasing */
607 for (k = 0; k < sizeof(acpi_id); k++)
608 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200609 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100610 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200611 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700616/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
617static int do_pcmcia_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100618 void *symval, char *alias)
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700619{
620 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100621 DEF_FIELD(symval, pcmcia_device_id, match_flags);
622 DEF_FIELD(symval, pcmcia_device_id, manf_id);
623 DEF_FIELD(symval, pcmcia_device_id, card_id);
624 DEF_FIELD(symval, pcmcia_device_id, func_id);
625 DEF_FIELD(symval, pcmcia_device_id, function);
626 DEF_FIELD(symval, pcmcia_device_id, device_no);
627 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200628
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700629 for (i=0; i<4; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100630 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
631 }
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700632
Andreas Schwab6543bec2013-01-20 17:58:47 +0100633 strcpy(alias, "pcmcia:");
634 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
635 manf_id);
636 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
637 card_id);
638 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
639 func_id);
640 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
641 function);
642 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
643 device_no);
644 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
645 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
646 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
647 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700648
Jean Delvareac551822008-05-02 20:37:21 +0200649 add_wildcard(alias);
Andreas Schwab6543bec2013-01-20 17:58:47 +0100650 return 1;
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700651}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100652ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id, do_pcmcia_entry);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700653
Andreas Schwab6543bec2013-01-20 17:58:47 +0100654static int do_of_entry (const char *filename, void *symval, char *alias)
Jeff Mahoney5e655772005-07-06 15:44:41 -0400655{
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900656 int len;
657 char *tmp;
658 DEF_FIELD_ADDR(symval, of_device_id, name);
659 DEF_FIELD_ADDR(symval, of_device_id, type);
660 DEF_FIELD_ADDR(symval, of_device_id, compatible);
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000661
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900662 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
663 (*type)[0] ? *type : "*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100664
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900665 if (compatible[0])
666 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
667 *compatible);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400668
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900669 /* Replace all whitespace with underscores */
670 for (tmp = alias; tmp && *tmp; tmp++)
671 if (isspace (*tmp))
672 *tmp = '_';
Jeff Mahoney5e655772005-07-06 15:44:41 -0400673
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900674 add_wildcard(alias);
675 return 1;
Jeff Mahoney5e655772005-07-06 15:44:41 -0400676}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100677ADD_TO_DEVTABLE("of", of_device_id, do_of_entry);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400678
Andreas Schwab6543bec2013-01-20 17:58:47 +0100679static int do_vio_entry(const char *filename, void *symval,
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000680 char *alias)
681{
682 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100683 DEF_FIELD_ADDR(symval, vio_device_id, type);
684 DEF_FIELD_ADDR(symval, vio_device_id, compat);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000685
Andreas Schwab6543bec2013-01-20 17:58:47 +0100686 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
687 (*compat)[0] ? *compat : "*");
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000688
689 /* Replace all whitespace with underscores */
690 for (tmp = alias; tmp && *tmp; tmp++)
691 if (isspace (*tmp))
692 *tmp = '_';
693
Jean Delvareac551822008-05-02 20:37:21 +0200694 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000695 return 1;
696}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100697ADD_TO_DEVTABLE("vio", vio_device_id, do_vio_entry);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000698
Rusty Russell1d8f4302005-12-07 21:40:34 +0100699#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
700
701static void do_input(char *alias,
702 kernel_ulong_t *arr, unsigned int min, unsigned int max)
703{
704 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400705
Andreas Schwab6543bec2013-01-20 17:58:47 +0100706 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
707 arr[i] = TO_NATIVE(arr[i]);
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400708 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200709 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400710 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100711}
712
713/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100714static int do_input_entry(const char *filename, void *symval,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100715 char *alias)
716{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100717 DEF_FIELD(symval, input_device_id, flags);
718 DEF_FIELD(symval, input_device_id, bustype);
719 DEF_FIELD(symval, input_device_id, vendor);
720 DEF_FIELD(symval, input_device_id, product);
721 DEF_FIELD(symval, input_device_id, version);
722 DEF_FIELD_ADDR(symval, input_device_id, evbit);
723 DEF_FIELD_ADDR(symval, input_device_id, keybit);
724 DEF_FIELD_ADDR(symval, input_device_id, relbit);
725 DEF_FIELD_ADDR(symval, input_device_id, absbit);
726 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
727 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
728 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
729 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
730 DEF_FIELD_ADDR(symval, input_device_id, swbit);
731
Rusty Russell1d8f4302005-12-07 21:40:34 +0100732 sprintf(alias, "input:");
733
Andreas Schwab6543bec2013-01-20 17:58:47 +0100734 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
735 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
736 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
737 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100738
739 sprintf(alias + strlen(alias), "-e*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100740 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
741 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100742 sprintf(alias + strlen(alias), "k*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100743 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
744 do_input(alias, *keybit,
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100745 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
746 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100747 sprintf(alias + strlen(alias), "r*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100748 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
749 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100750 sprintf(alias + strlen(alias), "a*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100751 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
752 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100753 sprintf(alias + strlen(alias), "m*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100754 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
755 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100756 sprintf(alias + strlen(alias), "l*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100757 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
758 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100759 sprintf(alias + strlen(alias), "s*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100760 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
761 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100762 sprintf(alias + strlen(alias), "f*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100763 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
764 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100765 sprintf(alias + strlen(alias), "w*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100766 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
767 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100768 return 1;
769}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100770ADD_TO_DEVTABLE("input", input_device_id, do_input_entry);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100771
Andreas Schwab6543bec2013-01-20 17:58:47 +0100772static int do_eisa_entry(const char *filename, void *symval,
Michael Tokarev07563c72006-09-27 01:50:56 -0700773 char *alias)
774{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100775 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
776 if (sig[0])
777 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
Jean Delvareac551822008-05-02 20:37:21 +0200778 else
779 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700780 return 1;
781}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100782ADD_TO_DEVTABLE("eisa", eisa_device_id, do_eisa_entry);
Michael Tokarev07563c72006-09-27 01:50:56 -0700783
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500784/* Looks like: parisc:tNhvNrevNsvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100785static int do_parisc_entry(const char *filename, void *symval,
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500786 char *alias)
787{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100788 DEF_FIELD(symval, parisc_device_id, hw_type);
789 DEF_FIELD(symval, parisc_device_id, hversion);
790 DEF_FIELD(symval, parisc_device_id, hversion_rev);
791 DEF_FIELD(symval, parisc_device_id, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500792
793 strcpy(alias, "parisc:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100794 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
795 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
796 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
797 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500798
Jean Delvareac551822008-05-02 20:37:21 +0200799 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500800 return 1;
801}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100802ADD_TO_DEVTABLE("parisc", parisc_device_id, do_parisc_entry);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500803
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200804/* Looks like: sdio:cNvNdN. */
805static int do_sdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100806 void *symval, char *alias)
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200807{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100808 DEF_FIELD(symval, sdio_device_id, class);
809 DEF_FIELD(symval, sdio_device_id, vendor);
810 DEF_FIELD(symval, sdio_device_id, device);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200811
812 strcpy(alias, "sdio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100813 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
814 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
815 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
Jean Delvareac551822008-05-02 20:37:21 +0200816 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700817 return 1;
818}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100819ADD_TO_DEVTABLE("sdio", sdio_device_id, do_sdio_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200820
Michael Buesch61e115a2007-09-18 15:12:50 -0400821/* Looks like: ssb:vNidNrevN. */
822static int do_ssb_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100823 void *symval, char *alias)
Michael Buesch61e115a2007-09-18 15:12:50 -0400824{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100825 DEF_FIELD(symval, ssb_device_id, vendor);
826 DEF_FIELD(symval, ssb_device_id, coreid);
827 DEF_FIELD(symval, ssb_device_id, revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400828
829 strcpy(alias, "ssb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100830 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
831 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
832 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
Jean Delvareac551822008-05-02 20:37:21 +0200833 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200834 return 1;
835}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100836ADD_TO_DEVTABLE("ssb", ssb_device_id, do_ssb_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200837
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200838/* Looks like: bcma:mNidNrevNclN. */
839static int do_bcma_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100840 void *symval, char *alias)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200841{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100842 DEF_FIELD(symval, bcma_device_id, manuf);
843 DEF_FIELD(symval, bcma_device_id, id);
844 DEF_FIELD(symval, bcma_device_id, rev);
845 DEF_FIELD(symval, bcma_device_id, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200846
847 strcpy(alias, "bcma:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100848 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
849 ADD(alias, "id", id != BCMA_ANY_ID, id);
850 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
851 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200852 add_wildcard(alias);
853 return 1;
854}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100855ADD_TO_DEVTABLE("bcma", bcma_device_id, do_bcma_entry);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200856
Rusty Russellb01d9f22007-10-22 11:03:39 +1000857/* Looks like: virtio:dNvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100858static int do_virtio_entry(const char *filename, void *symval,
Rusty Russellb01d9f22007-10-22 11:03:39 +1000859 char *alias)
860{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100861 DEF_FIELD(symval, virtio_device_id, device);
862 DEF_FIELD(symval, virtio_device_id, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000863
864 strcpy(alias, "virtio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100865 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
866 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000867
Jean Delvareac551822008-05-02 20:37:21 +0200868 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000869 return 1;
870}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100871ADD_TO_DEVTABLE("virtio", virtio_device_id, do_virtio_entry);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000872
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700873/*
874 * Looks like: vmbus:guid
875 * Each byte of the guid will be represented by two hex characters
876 * in the name.
877 */
878
Andreas Schwab6543bec2013-01-20 17:58:47 +0100879static int do_vmbus_entry(const char *filename, void *symval,
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700880 char *alias)
881{
882 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100883 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
884 char guid_name[(sizeof(*guid) + 1) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700885
Andreas Schwab6543bec2013-01-20 17:58:47 +0100886 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
887 sprintf(&guid_name[i], "%02x", TO_NATIVE((*guid)[i/2]));
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700888
889 strcpy(alias, "vmbus:");
890 strcat(alias, guid_name);
891
892 return 1;
893}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100894ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id, do_vmbus_entry);
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700895
Jean Delvared2653e92008-04-29 23:11:39 +0200896/* Looks like: i2c:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100897static int do_i2c_entry(const char *filename, void *symval,
Jean Delvared2653e92008-04-29 23:11:39 +0200898 char *alias)
899{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100900 DEF_FIELD_ADDR(symval, i2c_device_id, name);
901 sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
Jean Delvared2653e92008-04-29 23:11:39 +0200902
903 return 1;
904}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100905ADD_TO_DEVTABLE("i2c", i2c_device_id, do_i2c_entry);
Jean Delvared2653e92008-04-29 23:11:39 +0200906
Anton Vorontsove0626e32009-09-22 16:46:08 -0700907/* Looks like: spi:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100908static int do_spi_entry(const char *filename, void *symval,
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700909 char *alias)
910{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100911 DEF_FIELD_ADDR(symval, spi_device_id, name);
912 sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700913
914 return 1;
915}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100916ADD_TO_DEVTABLE("spi", spi_device_id, do_spi_entry);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700917
David Woodhoused945b692008-09-16 16:23:28 -0700918static const struct dmifield {
919 const char *prefix;
920 int field;
921} dmi_fields[] = {
922 { "bvn", DMI_BIOS_VENDOR },
923 { "bvr", DMI_BIOS_VERSION },
924 { "bd", DMI_BIOS_DATE },
925 { "svn", DMI_SYS_VENDOR },
926 { "pn", DMI_PRODUCT_NAME },
927 { "pvr", DMI_PRODUCT_VERSION },
928 { "rvn", DMI_BOARD_VENDOR },
929 { "rn", DMI_BOARD_NAME },
930 { "rvr", DMI_BOARD_VERSION },
931 { "cvn", DMI_CHASSIS_VENDOR },
932 { "ct", DMI_CHASSIS_TYPE },
933 { "cvr", DMI_CHASSIS_VERSION },
934 { NULL, DMI_NONE }
935};
936
937static void dmi_ascii_filter(char *d, const char *s)
938{
939 /* Filter out characters we don't want to see in the modalias string */
940 for (; *s; s++)
941 if (*s > ' ' && *s < 127 && *s != ':')
942 *(d++) = *s;
943
944 *d = 0;
945}
946
947
Andreas Schwab6543bec2013-01-20 17:58:47 +0100948static int do_dmi_entry(const char *filename, void *symval,
David Woodhoused945b692008-09-16 16:23:28 -0700949 char *alias)
950{
951 int i, j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100952 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
David Woodhoused945b692008-09-16 16:23:28 -0700953 sprintf(alias, "dmi*");
954
955 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
956 for (j = 0; j < 4; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100957 if ((*matches)[j].slot &&
958 (*matches)[j].slot == dmi_fields[i].field) {
David Woodhoused945b692008-09-16 16:23:28 -0700959 sprintf(alias + strlen(alias), ":%s*",
960 dmi_fields[i].prefix);
961 dmi_ascii_filter(alias + strlen(alias),
Andreas Schwab6543bec2013-01-20 17:58:47 +0100962 (*matches)[j].substr);
David Woodhoused945b692008-09-16 16:23:28 -0700963 strcat(alias, "*");
964 }
965 }
966 }
967
968 strcat(alias, ":");
969 return 1;
970}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100971ADD_TO_DEVTABLE("dmi", dmi_system_id, do_dmi_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +0800972
973static int do_platform_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100974 void *symval, char *alias)
Eric Miao57fee4a2009-02-04 11:52:40 +0800975{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100976 DEF_FIELD_ADDR(symval, platform_device_id, name);
977 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
Eric Miao57fee4a2009-02-04 11:52:40 +0800978 return 1;
979}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100980ADD_TO_DEVTABLE("platform", platform_device_id, do_platform_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +0800981
David Woodhouse8626d3b2010-04-02 01:05:27 +0000982static int do_mdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100983 void *symval, char *alias)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000984{
985 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100986 DEF_FIELD(symval, mdio_device_id, phy_id);
987 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
David Woodhouse8626d3b2010-04-02 01:05:27 +0000988
989 alias += sprintf(alias, MDIO_MODULE_PREFIX);
990
991 for (i = 0; i < 32; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100992 if (!((phy_id_mask >> (31-i)) & 1))
David Woodhouse8626d3b2010-04-02 01:05:27 +0000993 *(alias++) = '?';
Andreas Schwab6543bec2013-01-20 17:58:47 +0100994 else if ((phy_id >> (31-i)) & 1)
David Woodhouse8626d3b2010-04-02 01:05:27 +0000995 *(alias++) = '1';
996 else
997 *(alias++) = '0';
998 }
999
1000 /* Terminate the string */
1001 *alias = 0;
1002
1003 return 1;
1004}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001005ADD_TO_DEVTABLE("mdio", mdio_device_id, do_mdio_entry);
David Woodhouse8626d3b2010-04-02 01:05:27 +00001006
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001007/* Looks like: zorro:iN. */
Andreas Schwab6543bec2013-01-20 17:58:47 +01001008static int do_zorro_entry(const char *filename, void *symval,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001009 char *alias)
1010{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001011 DEF_FIELD(symval, zorro_device_id, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001012 strcpy(alias, "zorro:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001013 ADD(alias, "i", id != ZORRO_WILDCARD, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001014 return 1;
1015}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001016ADD_TO_DEVTABLE("zorro", zorro_device_id, do_zorro_entry);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001017
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001018/* looks like: "pnp:dD" */
1019static int do_isapnp_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001020 void *symval, char *alias)
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001021{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001022 DEF_FIELD(symval, isapnp_device_id, vendor);
1023 DEF_FIELD(symval, isapnp_device_id, function);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001024 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001025 'A' + ((vendor >> 2) & 0x3f) - 1,
1026 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1027 'A' + ((vendor >> 8) & 0x1f) - 1,
1028 (function >> 4) & 0x0f, function & 0x0f,
1029 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001030 return 1;
1031}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001032ADD_TO_DEVTABLE("isapnp", isapnp_device_id, do_isapnp_entry);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001033
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001034/* Looks like: "ipack:fNvNdN". */
1035static int do_ipack_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001036 void *symval, char *alias)
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001037{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001038 DEF_FIELD(symval, ipack_device_id, format);
1039 DEF_FIELD(symval, ipack_device_id, vendor);
1040 DEF_FIELD(symval, ipack_device_id, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001041 strcpy(alias, "ipack:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001042 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1043 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1044 ADD(alias, "d", device != IPACK_ANY_ID, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001045 add_wildcard(alias);
1046 return 1;
1047}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001048ADD_TO_DEVTABLE("ipack", ipack_device_id, do_ipack_entry);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001049
Dave Martin523817b2011-10-05 14:44:57 +01001050/*
1051 * Append a match expression for a single masked hex digit.
1052 * outp points to a pointer to the character at which to append.
1053 * *outp is updated on return to point just after the appended text,
1054 * to facilitate further appending.
1055 */
1056static void append_nibble_mask(char **outp,
1057 unsigned int nibble, unsigned int mask)
1058{
1059 char *p = *outp;
1060 unsigned int i;
1061
1062 switch (mask) {
1063 case 0:
1064 *p++ = '?';
1065 break;
1066
1067 case 0xf:
1068 p += sprintf(p, "%X", nibble);
1069 break;
1070
1071 default:
1072 /*
1073 * Dumbly emit a match pattern for all possible matching
1074 * digits. This could be improved in some cases using ranges,
1075 * but it has the advantage of being trivially correct, and is
1076 * often optimal.
1077 */
1078 *p++ = '[';
1079 for (i = 0; i < 0x10; i++)
1080 if ((i & mask) == nibble)
1081 p += sprintf(p, "%X", i);
1082 *p++ = ']';
1083 }
1084
1085 /* Ensure that the string remains NUL-terminated: */
1086 *p = '\0';
1087
1088 /* Advance the caller's end-of-string pointer: */
1089 *outp = p;
1090}
1091
1092/*
1093 * looks like: "amba:dN"
1094 *
1095 * N is exactly 8 digits, where each is an upper-case hex digit, or
1096 * a ? or [] pattern matching exactly one digit.
1097 */
1098static int do_amba_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001099 void *symval, char *alias)
Dave Martin523817b2011-10-05 14:44:57 +01001100{
1101 unsigned int digit;
1102 char *p = alias;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001103 DEF_FIELD(symval, amba_id, id);
1104 DEF_FIELD(symval, amba_id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001105
Andreas Schwab6543bec2013-01-20 17:58:47 +01001106 if ((id & mask) != id)
Dave Martin523817b2011-10-05 14:44:57 +01001107 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1108 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001109 filename, id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001110
1111 p += sprintf(alias, "amba:d");
1112 for (digit = 0; digit < 8; digit++)
1113 append_nibble_mask(&p,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001114 (id >> (4 * (7 - digit))) & 0xf,
1115 (mask >> (4 * (7 - digit))) & 0xf);
Dave Martin523817b2011-10-05 14:44:57 +01001116
1117 return 1;
1118}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001119ADD_TO_DEVTABLE("amba", amba_id, do_amba_entry);
Dave Martin523817b2011-10-05 14:44:57 +01001120
James Hogan8286ae02015-03-25 15:39:50 +00001121/*
1122 * looks like: "mipscdmm:tN"
1123 *
1124 * N is exactly 2 digits, where each is an upper-case hex digit, or
1125 * a ? or [] pattern matching exactly one digit.
1126 */
1127static int do_mips_cdmm_entry(const char *filename,
1128 void *symval, char *alias)
1129{
1130 DEF_FIELD(symval, mips_cdmm_device_id, type);
1131
1132 sprintf(alias, "mipscdmm:t%02X*", type);
1133 return 1;
1134}
1135ADD_TO_DEVTABLE("mipscdmm", mips_cdmm_device_id, do_mips_cdmm_entry);
1136
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001137/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
Andi Kleen644e9cb2012-01-26 00:09:05 +01001138 * All fields are numbers. It would be nicer to use strings for vendor
1139 * and feature, but getting those out of the build system here is too
1140 * complicated.
1141 */
1142
Andreas Schwab6543bec2013-01-20 17:58:47 +01001143static int do_x86cpu_entry(const char *filename, void *symval,
Andi Kleen644e9cb2012-01-26 00:09:05 +01001144 char *alias)
1145{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001146 DEF_FIELD(symval, x86_cpu_id, feature);
1147 DEF_FIELD(symval, x86_cpu_id, family);
1148 DEF_FIELD(symval, x86_cpu_id, model);
1149 DEF_FIELD(symval, x86_cpu_id, vendor);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001150
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001151 strcpy(alias, "cpu:type:x86,");
1152 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1153 ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1154 ADD(alias, "mod", model != X86_MODEL_ANY, model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001155 strcat(alias, ":feature:*");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001156 if (feature != X86_FEATURE_ANY)
1157 sprintf(alias + strlen(alias), "%04X*", feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001158 return 1;
1159}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001160ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry);
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}
1170ADD_TO_DEVTABLE("cpu", cpu_feature, do_cpu_entry);
1171
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001172/* Looks like: mei:S:uuid */
Samuel Ortize5354102013-03-27 17:29:53 +02001173static int do_mei_entry(const char *filename, void *symval,
1174 char *alias)
1175{
1176 DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001177 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
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);
1182
1183 strcat(alias, ":*");
Samuel Ortize5354102013-03-27 17:29:53 +02001184
1185 return 1;
1186}
1187ADD_TO_DEVTABLE("mei", mei_cl_device_id, do_mei_entry);
1188
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}
1207ADD_TO_DEVTABLE("rapidio", rio_device_id, do_rio_entry);
1208
Rusty Russell626596e2012-01-13 09:32:15 +10301209/* Does namelen bytes of name exactly match the symbol? */
1210static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Rusty Russell626596e2012-01-13 09:32:15 +10301212 if (namelen != strlen(symbol))
1213 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Rusty Russell626596e2012-01-13 09:32:15 +10301215 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218static void do_table(void *symval, unsigned long size,
1219 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001220 const char *device_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 void *function,
1222 struct module *mod)
1223{
1224 unsigned int i;
1225 char alias[500];
1226 int (*do_entry)(const char *, void *entry, char *alias) = function;
1227
Kees Cooke0049822007-09-16 11:15:46 +02001228 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /* Leave last one: it's the terminator. */
1230 size -= id_size;
1231
1232 for (i = 0; i < size; i += id_size) {
1233 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 buf_printf(&mod->dev_table_buf,
1235 "MODULE_ALIAS(\"%s\");\n", alias);
1236 }
1237 }
1238}
1239
1240/* Create MODULE_ALIAS() statements.
1241 * At this time, we cannot write the actual output C source yet,
1242 * so we write into the mod->dev_table_buf buffer. */
1243void handle_moddevtable(struct module *mod, struct elf_info *info,
1244 Elf_Sym *sym, const char *symname)
1245{
1246 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001247 char *zeros = NULL;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301248 const char *name, *identifier;
Rusty Russell626596e2012-01-13 09:32:15 +10301249 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001252 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return;
1254
David Millere88aa7b2012-04-12 14:37:30 -04001255 /* We're looking for an object */
1256 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1257 return;
1258
Tom Gundersen21bdd172014-02-03 11:14:13 +10301259 /* All our symbols are of form <prefix>__mod_<name>__<identifier>_device_table. */
Rusty Russell626596e2012-01-13 09:32:15 +10301260 name = strstr(symname, "__mod_");
1261 if (!name)
1262 return;
1263 name += strlen("__mod_");
1264 namelen = strlen(name);
1265 if (namelen < strlen("_device_table"))
1266 return;
1267 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1268 return;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301269 identifier = strstr(name, "__");
1270 if (!identifier)
1271 return;
1272 namelen = identifier - name;
Rusty Russell626596e2012-01-13 09:32:15 +10301273
Kees Cooke0049822007-09-16 11:15:46 +02001274 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001275 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001276 zeros = calloc(1, sym->st_size);
1277 symval = zeros;
1278 } else {
1279 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001280 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001281 + sym->st_value;
1282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Rusty Russell626596e2012-01-13 09:32:15 +10301284 /* First handle the "special" cases */
1285 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001286 do_usb_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301287 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001288 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301289 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001290 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301291 else {
Rusty Russelle49ce142012-01-13 09:32:16 +10301292 struct devtable **p;
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +01001293 INIT_SECTION(__devtable);
Rusty Russell626596e2012-01-13 09:32:15 +10301294
Rusty Russelle49ce142012-01-13 09:32:16 +10301295 for (p = __start___devtable; p < __stop___devtable; p++) {
1296 if (sym_is(name, namelen, (*p)->device_id)) {
1297 do_table(symval, sym->st_size, (*p)->id_size,
1298 (*p)->device_id, (*p)->function, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301299 break;
1300 }
1301 }
1302 }
Kees Cooke0049822007-09-16 11:15:46 +02001303 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
1306/* Now add out buffered information to the generated C source */
1307void add_moddevtable(struct buffer *buf, struct module *mod)
1308{
1309 buf_printf(buf, "\n");
1310 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1311 free(mod->dev_table_buf.p);
1312}