blob: 5f208820913259651246d71a3def44f49fa8da44 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41/* Big exception to the "don't include kernel headers into userspace, which
Sam Ravnborg62070fa2006-03-03 16:46:04 +010042 * even potentially has different endianness and word sizes, since
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * we handle those differences explicitly below */
44#include "../../include/linux/mod_devicetable.h"
45
Rusty Russelle49ce142012-01-13 09:32:16 +103046/* This array collects all instances that use the generic do_table */
47struct devtable {
Tom Gundersen21bdd172014-02-03 11:14:13 +103048 const char *device_id; /* name of table, __mod_<name>__*_device_table. */
Rusty Russelle49ce142012-01-13 09:32:16 +103049 unsigned long id_size;
50 void *function;
51};
52
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010053#define ___cat(a,b) a ## b
54#define __cat(a,b) ___cat(a,b)
55
56/* we need some special handling for this host tool running eventually on
57 * Darwin. The Mach-O section handling is a bit different than ELF section
58 * handling. The differnces in detail are:
59 * a) we have segments which have sections
60 * b) we need a API call to get the respective section symbols */
61#if defined(__MACH__)
62#include <mach-o/getsect.h>
63
64#define INIT_SECTION(name) do { \
65 unsigned long name ## _len; \
66 char *__cat(pstart_,name) = getsectdata("__TEXT", \
67 #name, &__cat(name,_len)); \
68 char *__cat(pstop_,name) = __cat(pstart_,name) + \
69 __cat(name, _len); \
70 __cat(__start_,name) = (void *)__cat(pstart_,name); \
71 __cat(__stop_,name) = (void *)__cat(pstop_,name); \
72 } while (0)
73#define SECTION(name) __attribute__((section("__TEXT, " #name)))
74
75struct devtable **__start___devtable, **__stop___devtable;
76#else
77#define INIT_SECTION(name) /* no-op for ELF */
78#define SECTION(name) __attribute__((section(#name)))
79
Rusty Russelle49ce142012-01-13 09:32:16 +103080/* We construct a table of pointers in an ELF section (pointers generally
81 * go unpadded by gcc). ld creates boundary syms for us. */
82extern struct devtable *__start___devtable[], *__stop___devtable[];
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010083#endif /* __MACH__ */
Rusty Russelle49ce142012-01-13 09:32:16 +103084
Daniel Tang04130cc2013-06-09 12:33:55 +100085#if !defined(__used)
86# if __GNUC__ == 3 && __GNUC_MINOR__ < 3
87# define __used __attribute__((__unused__))
88# else
89# define __used __attribute__((__used__))
90# endif
Rusty Russelle49ce142012-01-13 09:32:16 +103091#endif
92
Andreas Schwab6543bec2013-01-20 17:58:47 +010093/* Define a variable f that holds the value of field f of struct devid
94 * based at address m.
95 */
96#define DEF_FIELD(m, devid, f) \
97 typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
98/* Define a variable f that holds the address of field f of struct devid
99 * based at address m. Due to the way typeof works, for a field of type
100 * T[N] the variable has type T(*)[N], _not_ T*.
101 */
102#define DEF_FIELD_ADDR(m, devid, f) \
103 typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
104
Rusty Russelle49ce142012-01-13 09:32:16 +1030105/* Add a table entry. We test function type matches while we're here. */
106#define ADD_TO_DEVTABLE(device_id, type, function) \
107 static struct devtable __cat(devtable,__LINE__) = { \
108 device_id + 0*sizeof((function)((const char *)NULL, \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100109 (void *)NULL, \
Rusty Russelle49ce142012-01-13 09:32:16 +1030110 (char *)NULL)), \
Andreas Schwab6543bec2013-01-20 17:58:47 +0100111 SIZE_##type, (function) }; \
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +0100112 static struct devtable *SECTION(__devtable) __used \
113 __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
Rusty Russelle49ce142012-01-13 09:32:16 +1030114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115#define ADD(str, sep, cond, field) \
116do { \
117 strcat(str, sep); \
118 if (cond) \
119 sprintf(str + strlen(str), \
120 sizeof(field) == 1 ? "%02X" : \
121 sizeof(field) == 2 ? "%04X" : \
122 sizeof(field) == 4 ? "%08X" : "", \
123 field); \
124 else \
125 sprintf(str + strlen(str), "*"); \
126} while(0)
127
Jean Delvareac551822008-05-02 20:37:21 +0200128/* Always end in a wildcard, for future extension */
129static inline void add_wildcard(char *str)
130{
131 int len = strlen(str);
132
133 if (str[len - 1] != '*')
134 strcat(str + len, "*");
135}
136
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700137static inline void add_uuid(char *str, uuid_le uuid)
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300138{
139 int len = strlen(str);
140 int i;
141
142 for (i = 0; i < 16; i++)
Greg Kroah-Hartmanb144ce22015-05-27 17:17:27 -0700143 sprintf(str + len + (i << 1), "%02x", uuid.b[i]);
Tomas Winklerc93b76b2015-05-07 15:54:02 +0300144}
145
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200146/**
147 * Check that sizeof(device_id type) are consistent with size of section
148 * in .o file. If in-consistent then userspace and kernel does not agree
149 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200150 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100151 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200152 **/
Kees Cooke0049822007-09-16 11:15:46 +0200153static void device_id_check(const char *modname, const char *device_id,
154 unsigned long size, unsigned long id_size,
155 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200156{
Kees Cooke0049822007-09-16 11:15:46 +0200157 int i;
158
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200159 if (size % id_size || size < id_size) {
160 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
Tom Gundersen21bdd172014-02-03 11:14:13 +1030161 "of the size of "
162 "section __mod_%s__<identifier>_device_table=%lu.\n"
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200163 "Fix definition of struct %s_device_id "
164 "in mod_devicetable.h\n",
165 modname, device_id, id_size, device_id, size, device_id);
166 }
Kees Cooke0049822007-09-16 11:15:46 +0200167 /* Verify last one is a terminator */
168 for (i = 0; i < id_size; i++ ) {
169 if (*(uint8_t*)(symval+size-id_size+i)) {
170 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
171 "The last of %lu is:\n",
172 modname, device_id, id_size, size / id_size);
173 for (i = 0; i < id_size; i++ )
174 fprintf(stderr,"0x%02x ",
175 *(uint8_t*)(symval+size-id_size+i) );
176 fprintf(stderr,"\n");
177 fatal("%s: struct %s_device_id is not terminated "
178 "with a NULL entry!\n", modname, device_id);
179 }
180 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200181}
182
Roman Kaganb19dcd92005-04-22 15:07:01 -0700183/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200184/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100185static void do_usb_entry(void *symval,
Roman Kaganb19dcd92005-04-22 15:07:01 -0700186 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
187 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500188 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700190 char alias[500];
Andreas Schwab6543bec2013-01-20 17:58:47 +0100191 DEF_FIELD(symval, usb_device_id, match_flags);
192 DEF_FIELD(symval, usb_device_id, idVendor);
193 DEF_FIELD(symval, usb_device_id, idProduct);
194 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
195 DEF_FIELD(symval, usb_device_id, bDeviceClass);
196 DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
197 DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
198 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
199 DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
200 DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
201 DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 strcpy(alias, "usb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100204 ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
205 idVendor);
206 ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
207 idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700208
209 strcat(alias, "d");
210 if (bcdDevice_initial_digits)
211 sprintf(alias + strlen(alias), "%0*X",
212 bcdDevice_initial_digits, bcdDevice_initial);
213 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500214 sprintf(alias + strlen(alias), "%X", range_lo);
215 else if (range_lo > 0 || range_hi < max) {
216 if (range_lo > 0x9 || range_hi < 0xA)
217 sprintf(alias + strlen(alias),
218 "[%X-%X]",
219 range_lo,
220 range_hi);
221 else {
222 sprintf(alias + strlen(alias),
223 range_lo < 0x9 ? "[%X-9" : "[%X",
224 range_lo);
225 sprintf(alias + strlen(alias),
Jan Moskyto Matejka03b56322014-02-07 19:15:11 +0100226 range_hi > 0xA ? "A-%X]" : "%X]",
227 range_hi);
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500228 }
229 }
Andreas Schwab6543bec2013-01-20 17:58:47 +0100230 if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700231 strcat(alias, "*");
232
Andreas Schwab6543bec2013-01-20 17:58:47 +0100233 ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
234 bDeviceClass);
235 ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
236 bDeviceSubClass);
237 ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
238 bDeviceProtocol);
239 ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
240 bInterfaceClass);
241 ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
242 bInterfaceSubClass);
243 ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
244 bInterfaceProtocol);
245 ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
246 bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700247
Jean Delvareac551822008-05-02 20:37:21 +0200248 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700249 buf_printf(&mod->dev_table_buf,
250 "MODULE_ALIAS(\"%s\");\n", alias);
251}
252
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500253/* Handles increment/decrement of BCD formatted integers */
254/* Returns the previous value, so it works like i++ or i-- */
255static unsigned int incbcd(unsigned int *bcd,
256 int inc,
257 unsigned char max,
258 size_t chars)
259{
260 unsigned int init = *bcd, i, j;
261 unsigned long long c, dec = 0;
262
263 /* If bcd is not in BCD format, just increment */
264 if (max > 0x9) {
265 *bcd += inc;
266 return init;
267 }
268
269 /* Convert BCD to Decimal */
270 for (i=0 ; i < chars ; i++) {
271 c = (*bcd >> (i << 2)) & 0xf;
272 c = c > 9 ? 9 : c; /* force to bcd just in case */
273 for (j=0 ; j < i ; j++)
274 c = c * 10;
275 dec += c;
276 }
277
278 /* Do our increment/decrement */
279 dec += inc;
280 *bcd = 0;
281
282 /* Convert back to BCD */
283 for (i=0 ; i < chars ; i++) {
284 for (c=1,j=0 ; j < i ; j++)
285 c = c * 10;
286 c = (dec / c) % 10;
287 *bcd += c << (i << 2);
288 }
289 return init;
290}
291
Andreas Schwab6543bec2013-01-20 17:58:47 +0100292static void do_usb_entry_multi(void *symval, struct module *mod)
Roman Kaganb19dcd92005-04-22 15:07:01 -0700293{
294 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500295 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700296 int ndigits;
297
Andreas Schwab6543bec2013-01-20 17:58:47 +0100298 DEF_FIELD(symval, usb_device_id, match_flags);
299 DEF_FIELD(symval, usb_device_id, idVendor);
300 DEF_FIELD(symval, usb_device_id, idProduct);
301 DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
302 DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
303 DEF_FIELD(symval, usb_device_id, bDeviceClass);
304 DEF_FIELD(symval, usb_device_id, bInterfaceClass);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700305
Andreas Schwab6543bec2013-01-20 17:58:47 +0100306 devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
307 bcdDevice_lo : 0x0U;
308 devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
309 bcdDevice_hi : ~0x0U;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700310
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500311 /* Figure out if this entry is in bcd or hex format */
312 max = 0x9; /* Default to decimal format */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100313 for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500314 clo = (devlo >> (ndigits << 2)) & 0xf;
315 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
316 if (clo > max || chi > max) {
317 max = 0xf;
318 break;
319 }
320 }
321
Roman Kaganb19dcd92005-04-22 15:07:01 -0700322 /*
323 * Some modules (visor) have empty slots as placeholder for
324 * run-time specification that results in catch-all alias
325 */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100326 if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700327 return;
328
329 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100330 for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
Roman Kaganb19dcd92005-04-22 15:07:01 -0700331 clo = devlo & 0xf;
332 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500333 if (chi > max) /* If we are in bcd mode, truncate if necessary */
334 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700335 devlo >>= 4;
336 devhi >>= 4;
337
338 if (devlo == devhi || !ndigits) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100339 do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700340 break;
341 }
342
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500343 if (clo > 0x0)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100344 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500345 incbcd(&devlo, 1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100346 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500347 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700348
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500349 if (chi < max)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100350 do_usb_entry(symval,
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500351 incbcd(&devhi, -1, max,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100352 sizeof(bcdDevice_lo) * 2),
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500353 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700354 }
355}
356
357static void do_usb_table(void *symval, unsigned long size,
358 struct module *mod)
359{
360 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100361 const unsigned long id_size = SIZE_usb_device_id;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700362
Kees Cooke0049822007-09-16 11:15:46 +0200363 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200364
Roman Kaganb19dcd92005-04-22 15:07:01 -0700365 /* Leave last one: it's the terminator. */
366 size -= id_size;
367
368 for (i = 0; i < size; i += id_size)
369 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Jiri Slabye8c84f92008-05-19 15:50:01 +0200372/* Looks like: hid:bNvNpN */
373static int do_hid_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100374 void *symval, char *alias)
Jiri Slabye8c84f92008-05-19 15:50:01 +0200375{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100376 DEF_FIELD(symval, hid_device_id, bus);
377 DEF_FIELD(symval, hid_device_id, group);
378 DEF_FIELD(symval, hid_device_id, vendor);
379 DEF_FIELD(symval, hid_device_id, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200380
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200381 sprintf(alias, "hid:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100382 ADD(alias, "b", bus != HID_BUS_ANY, bus);
383 ADD(alias, "g", group != HID_GROUP_ANY, group);
384 ADD(alias, "v", vendor != HID_ANY_ID, vendor);
385 ADD(alias, "p", product != HID_ANY_ID, product);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200386
387 return 1;
388}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100389ADD_TO_DEVTABLE("hid", hid_device_id, do_hid_entry);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391/* Looks like: ieee1394:venNmoNspNverN */
392static int do_ieee1394_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100393 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100395 DEF_FIELD(symval, ieee1394_device_id, match_flags);
396 DEF_FIELD(symval, ieee1394_device_id, vendor_id);
397 DEF_FIELD(symval, ieee1394_device_id, model_id);
398 DEF_FIELD(symval, ieee1394_device_id, specifier_id);
399 DEF_FIELD(symval, ieee1394_device_id, version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 strcpy(alias, "ieee1394:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100402 ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
403 vendor_id);
404 ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
405 model_id);
406 ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
407 specifier_id);
408 ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
409 version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Jean Delvareac551822008-05-02 20:37:21 +0200411 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return 1;
413}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100414ADD_TO_DEVTABLE("ieee1394", ieee1394_device_id, do_ieee1394_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
417static int do_pci_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100418 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 /* Class field can be divided into these three. */
421 unsigned char baseclass, subclass, interface,
422 baseclass_mask, subclass_mask, interface_mask;
423
Andreas Schwab6543bec2013-01-20 17:58:47 +0100424 DEF_FIELD(symval, pci_device_id, vendor);
425 DEF_FIELD(symval, pci_device_id, device);
426 DEF_FIELD(symval, pci_device_id, subvendor);
427 DEF_FIELD(symval, pci_device_id, subdevice);
428 DEF_FIELD(symval, pci_device_id, class);
429 DEF_FIELD(symval, pci_device_id, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 strcpy(alias, "pci:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100432 ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
433 ADD(alias, "d", device != PCI_ANY_ID, device);
434 ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
435 ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Andreas Schwab6543bec2013-01-20 17:58:47 +0100437 baseclass = (class) >> 16;
438 baseclass_mask = (class_mask) >> 16;
439 subclass = (class) >> 8;
440 subclass_mask = (class_mask) >> 8;
441 interface = class;
442 interface_mask = class_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
445 || (subclass_mask != 0 && subclass_mask != 0xFF)
446 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100447 warn("Can't handle masks in %s:%04X\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +0100448 filename, class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
450 }
451
452 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
453 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
454 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200455 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return 1;
457}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100458ADD_TO_DEVTABLE("pci", pci_device_id, do_pci_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100460/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static int do_ccw_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100462 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100464 DEF_FIELD(symval, ccw_device_id, match_flags);
465 DEF_FIELD(symval, ccw_device_id, cu_type);
466 DEF_FIELD(symval, ccw_device_id, cu_model);
467 DEF_FIELD(symval, ccw_device_id, dev_type);
468 DEF_FIELD(symval, ccw_device_id, dev_model);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 strcpy(alias, "ccw:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100471 ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
472 cu_type);
473 ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
474 cu_model);
475 ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
476 dev_type);
477 ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
478 dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200479 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 1;
481}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100482ADD_TO_DEVTABLE("ccw", ccw_device_id, do_ccw_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200484/* looks like: "ap:tN" */
485static int do_ap_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100486 void *symval, char *alias)
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200487{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100488 DEF_FIELD(symval, ap_device_id, dev_type);
489
490 sprintf(alias, "ap:t%02X*", dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200491 return 1;
492}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100493ADD_TO_DEVTABLE("ap", ap_device_id, do_ap_entry);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200494
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200495/* looks like: "css:tN" */
496static int do_css_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100497 void *symval, char *alias)
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200498{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100499 DEF_FIELD(symval, css_device_id, type);
500
501 sprintf(alias, "css:t%01X", type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200502 return 1;
503}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100504ADD_TO_DEVTABLE("css", css_device_id, do_css_entry);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506/* Looks like: "serio:tyNprNidNexN" */
507static int do_serio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100508 void *symval, char *alias)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100510 DEF_FIELD(symval, serio_device_id, type);
511 DEF_FIELD(symval, serio_device_id, proto);
512 DEF_FIELD(symval, serio_device_id, id);
513 DEF_FIELD(symval, serio_device_id, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 strcpy(alias, "serio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100516 ADD(alias, "ty", type != SERIO_ANY, type);
517 ADD(alias, "pr", proto != SERIO_ANY, proto);
518 ADD(alias, "id", id != SERIO_ANY, id);
519 ADD(alias, "ex", extra != SERIO_ANY, extra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Jean Delvareac551822008-05-02 20:37:21 +0200521 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 1;
523}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100524ADD_TO_DEVTABLE("serio", serio_device_id, do_serio_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200526/* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
527 * "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
528 *
529 * NOTE: Each driver should use one of the following : _HID, _CIDs
530 * or _CLS. Also, bb, ss, and pp can be substituted with ??
531 * as don't care byte.
532 */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200533static int do_acpi_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100534 void *symval, char *alias)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200535{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100536 DEF_FIELD_ADDR(symval, acpi_device_id, id);
Suthikulpanit, Suravee26095a02015-07-07 01:55:20 +0200537 DEF_FIELD_ADDR(symval, acpi_device_id, cls);
538 DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk);
539
540 if (id && strlen((const char *)*id))
541 sprintf(alias, "acpi*:%s:*", *id);
542 else if (cls) {
543 int i, byte_shift, cnt = 0;
544 unsigned int msk;
545
546 sprintf(&alias[cnt], "acpi*:");
547 cnt = 6;
548 for (i = 1; i <= 3; i++) {
549 byte_shift = 8 * (3-i);
550 msk = (*cls_msk >> byte_shift) & 0xFF;
551 if (msk)
552 sprintf(&alias[cnt], "%02x",
553 (*cls >> byte_shift) & 0xFF);
554 else
555 sprintf(&alias[cnt], "??");
556 cnt += 2;
557 }
558 sprintf(&alias[cnt], ":*");
559 }
Thomas Renninger29b71a12007-07-23 14:43:51 +0200560 return 1;
561}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100562ADD_TO_DEVTABLE("acpi", acpi_device_id, do_acpi_entry);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200565static void do_pnp_device_entry(void *symval, unsigned long size,
566 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100568 const unsigned long id_size = SIZE_pnp_device_id;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200569 const unsigned int count = (size / id_size)-1;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200570 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200571
572 device_id_check(mod->name, "pnp", size, id_size, symval);
573
Kay Sievers5e4c6562008-08-21 15:28:56 +0200574 for (i = 0; i < count; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100575 DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
576 char acpi_id[sizeof(*id)];
Kay Sievers72638f52009-01-08 03:06:42 +0100577 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200578
579 buf_printf(&mod->dev_table_buf,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100580 "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
Kay Sievers72638f52009-01-08 03:06:42 +0100581
582 /* fix broken pnp bus lowercasing */
583 for (j = 0; j < sizeof(acpi_id); j++)
Andreas Schwab6543bec2013-01-20 17:58:47 +0100584 acpi_id[j] = toupper((*id)[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200585 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100586 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Kay Sievers0c81eed2008-02-21 00:35:54 +0100590/* looks like: "pnp:dD" for every device of the card */
591static void do_pnp_card_entries(void *symval, unsigned long size,
592 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100594 const unsigned long id_size = SIZE_pnp_card_device_id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100595 const unsigned int count = (size / id_size)-1;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100596 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Kay Sievers0c81eed2008-02-21 00:35:54 +0100598 device_id_check(mod->name, "pnp", size, id_size, symval);
599
600 for (i = 0; i < count; i++) {
601 unsigned int j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100602 DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100603
604 for (j = 0; j < PNP_MAX_DEVICES; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100605 const char *id = (char *)(*devs)[j].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100606 int i2, j2;
607 int dup = 0;
608
609 if (!id[0])
610 break;
611
612 /* find duplicate, already added value */
613 for (i2 = 0; i2 < i && !dup; i2++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100614 DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
Kay Sievers0c81eed2008-02-21 00:35:54 +0100615
616 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100617 const char *id2 = (char *)(*devs)[j2].id;
Kay Sievers0c81eed2008-02-21 00:35:54 +0100618
619 if (!id2[0])
620 break;
621
622 if (!strcmp(id, id2)) {
623 dup = 1;
624 break;
625 }
626 }
627 }
628
629 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200630 if (!dup) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100631 char acpi_id[PNP_ID_LEN];
Kay Sievers72638f52009-01-08 03:06:42 +0100632 int k;
633
Kay Sievers0c81eed2008-02-21 00:35:54 +0100634 buf_printf(&mod->dev_table_buf,
635 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100636
637 /* fix broken pnp bus lowercasing */
638 for (k = 0; k < sizeof(acpi_id); k++)
639 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200640 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100641 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200642 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700647/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
648static int do_pcmcia_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100649 void *symval, char *alias)
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700650{
651 unsigned int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100652 DEF_FIELD(symval, pcmcia_device_id, match_flags);
653 DEF_FIELD(symval, pcmcia_device_id, manf_id);
654 DEF_FIELD(symval, pcmcia_device_id, card_id);
655 DEF_FIELD(symval, pcmcia_device_id, func_id);
656 DEF_FIELD(symval, pcmcia_device_id, function);
657 DEF_FIELD(symval, pcmcia_device_id, device_no);
658 DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200659
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700660 for (i=0; i<4; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100661 (*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
662 }
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700663
Andreas Schwab6543bec2013-01-20 17:58:47 +0100664 strcpy(alias, "pcmcia:");
665 ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
666 manf_id);
667 ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
668 card_id);
669 ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
670 func_id);
671 ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
672 function);
673 ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
674 device_no);
675 ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
676 ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
677 ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
678 ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700679
Jean Delvareac551822008-05-02 20:37:21 +0200680 add_wildcard(alias);
Andreas Schwab6543bec2013-01-20 17:58:47 +0100681 return 1;
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700682}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100683ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id, do_pcmcia_entry);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700684
Andreas Schwab6543bec2013-01-20 17:58:47 +0100685static int do_of_entry (const char *filename, void *symval, char *alias)
Jeff Mahoney5e655772005-07-06 15:44:41 -0400686{
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900687 int len;
688 char *tmp;
689 DEF_FIELD_ADDR(symval, of_device_id, name);
690 DEF_FIELD_ADDR(symval, of_device_id, type);
691 DEF_FIELD_ADDR(symval, of_device_id, compatible);
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000692
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900693 len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
694 (*type)[0] ? *type : "*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100695
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900696 if (compatible[0])
697 sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
698 *compatible);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400699
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900700 /* Replace all whitespace with underscores */
701 for (tmp = alias; tmp && *tmp; tmp++)
702 if (isspace (*tmp))
703 *tmp = '_';
Jeff Mahoney5e655772005-07-06 15:44:41 -0400704
Masahiro Yamadabb66fc62014-06-10 19:08:13 +0900705 add_wildcard(alias);
706 return 1;
Jeff Mahoney5e655772005-07-06 15:44:41 -0400707}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100708ADD_TO_DEVTABLE("of", of_device_id, do_of_entry);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400709
Andreas Schwab6543bec2013-01-20 17:58:47 +0100710static int do_vio_entry(const char *filename, void *symval,
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000711 char *alias)
712{
713 char *tmp;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100714 DEF_FIELD_ADDR(symval, vio_device_id, type);
715 DEF_FIELD_ADDR(symval, vio_device_id, compat);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000716
Andreas Schwab6543bec2013-01-20 17:58:47 +0100717 sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
718 (*compat)[0] ? *compat : "*");
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000719
720 /* Replace all whitespace with underscores */
721 for (tmp = alias; tmp && *tmp; tmp++)
722 if (isspace (*tmp))
723 *tmp = '_';
724
Jean Delvareac551822008-05-02 20:37:21 +0200725 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000726 return 1;
727}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100728ADD_TO_DEVTABLE("vio", vio_device_id, do_vio_entry);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000729
Rusty Russell1d8f4302005-12-07 21:40:34 +0100730#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
731
732static void do_input(char *alias,
733 kernel_ulong_t *arr, unsigned int min, unsigned int max)
734{
735 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400736
Andreas Schwab6543bec2013-01-20 17:58:47 +0100737 for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
738 arr[i] = TO_NATIVE(arr[i]);
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400739 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200740 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400741 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100742}
743
744/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100745static int do_input_entry(const char *filename, void *symval,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100746 char *alias)
747{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100748 DEF_FIELD(symval, input_device_id, flags);
749 DEF_FIELD(symval, input_device_id, bustype);
750 DEF_FIELD(symval, input_device_id, vendor);
751 DEF_FIELD(symval, input_device_id, product);
752 DEF_FIELD(symval, input_device_id, version);
753 DEF_FIELD_ADDR(symval, input_device_id, evbit);
754 DEF_FIELD_ADDR(symval, input_device_id, keybit);
755 DEF_FIELD_ADDR(symval, input_device_id, relbit);
756 DEF_FIELD_ADDR(symval, input_device_id, absbit);
757 DEF_FIELD_ADDR(symval, input_device_id, mscbit);
758 DEF_FIELD_ADDR(symval, input_device_id, ledbit);
759 DEF_FIELD_ADDR(symval, input_device_id, sndbit);
760 DEF_FIELD_ADDR(symval, input_device_id, ffbit);
761 DEF_FIELD_ADDR(symval, input_device_id, swbit);
762
Rusty Russell1d8f4302005-12-07 21:40:34 +0100763 sprintf(alias, "input:");
764
Andreas Schwab6543bec2013-01-20 17:58:47 +0100765 ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
766 ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
767 ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
768 ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100769
770 sprintf(alias + strlen(alias), "-e*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100771 if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
772 do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100773 sprintf(alias + strlen(alias), "k*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100774 if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
775 do_input(alias, *keybit,
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100776 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
777 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100778 sprintf(alias + strlen(alias), "r*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100779 if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
780 do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100781 sprintf(alias + strlen(alias), "a*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100782 if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
783 do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100784 sprintf(alias + strlen(alias), "m*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100785 if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
786 do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100787 sprintf(alias + strlen(alias), "l*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100788 if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
789 do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100790 sprintf(alias + strlen(alias), "s*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100791 if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
792 do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100793 sprintf(alias + strlen(alias), "f*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100794 if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
795 do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100796 sprintf(alias + strlen(alias), "w*");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100797 if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
798 do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100799 return 1;
800}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100801ADD_TO_DEVTABLE("input", input_device_id, do_input_entry);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100802
Andreas Schwab6543bec2013-01-20 17:58:47 +0100803static int do_eisa_entry(const char *filename, void *symval,
Michael Tokarev07563c72006-09-27 01:50:56 -0700804 char *alias)
805{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100806 DEF_FIELD_ADDR(symval, eisa_device_id, sig);
807 if (sig[0])
808 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
Jean Delvareac551822008-05-02 20:37:21 +0200809 else
810 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700811 return 1;
812}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100813ADD_TO_DEVTABLE("eisa", eisa_device_id, do_eisa_entry);
Michael Tokarev07563c72006-09-27 01:50:56 -0700814
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500815/* Looks like: parisc:tNhvNrevNsvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100816static int do_parisc_entry(const char *filename, void *symval,
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500817 char *alias)
818{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100819 DEF_FIELD(symval, parisc_device_id, hw_type);
820 DEF_FIELD(symval, parisc_device_id, hversion);
821 DEF_FIELD(symval, parisc_device_id, hversion_rev);
822 DEF_FIELD(symval, parisc_device_id, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500823
824 strcpy(alias, "parisc:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100825 ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
826 ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
827 ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
828 ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500829
Jean Delvareac551822008-05-02 20:37:21 +0200830 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500831 return 1;
832}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100833ADD_TO_DEVTABLE("parisc", parisc_device_id, do_parisc_entry);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500834
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200835/* Looks like: sdio:cNvNdN. */
836static int do_sdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100837 void *symval, char *alias)
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200838{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100839 DEF_FIELD(symval, sdio_device_id, class);
840 DEF_FIELD(symval, sdio_device_id, vendor);
841 DEF_FIELD(symval, sdio_device_id, device);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200842
843 strcpy(alias, "sdio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100844 ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
845 ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
846 ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
Jean Delvareac551822008-05-02 20:37:21 +0200847 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700848 return 1;
849}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100850ADD_TO_DEVTABLE("sdio", sdio_device_id, do_sdio_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200851
Michael Buesch61e115a2007-09-18 15:12:50 -0400852/* Looks like: ssb:vNidNrevN. */
853static int do_ssb_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100854 void *symval, char *alias)
Michael Buesch61e115a2007-09-18 15:12:50 -0400855{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100856 DEF_FIELD(symval, ssb_device_id, vendor);
857 DEF_FIELD(symval, ssb_device_id, coreid);
858 DEF_FIELD(symval, ssb_device_id, revision);
Michael Buesch61e115a2007-09-18 15:12:50 -0400859
860 strcpy(alias, "ssb:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100861 ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
862 ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
863 ADD(alias, "rev", revision != SSB_ANY_REV, revision);
Jean Delvareac551822008-05-02 20:37:21 +0200864 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200865 return 1;
866}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100867ADD_TO_DEVTABLE("ssb", ssb_device_id, do_ssb_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200868
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200869/* Looks like: bcma:mNidNrevNclN. */
870static int do_bcma_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +0100871 void *symval, char *alias)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200872{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100873 DEF_FIELD(symval, bcma_device_id, manuf);
874 DEF_FIELD(symval, bcma_device_id, id);
875 DEF_FIELD(symval, bcma_device_id, rev);
876 DEF_FIELD(symval, bcma_device_id, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200877
878 strcpy(alias, "bcma:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100879 ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
880 ADD(alias, "id", id != BCMA_ANY_ID, id);
881 ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
882 ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200883 add_wildcard(alias);
884 return 1;
885}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100886ADD_TO_DEVTABLE("bcma", bcma_device_id, do_bcma_entry);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200887
Rusty Russellb01d9f22007-10-22 11:03:39 +1000888/* Looks like: virtio:dNvN */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100889static int do_virtio_entry(const char *filename, void *symval,
Rusty Russellb01d9f22007-10-22 11:03:39 +1000890 char *alias)
891{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100892 DEF_FIELD(symval, virtio_device_id, device);
893 DEF_FIELD(symval, virtio_device_id, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000894
895 strcpy(alias, "virtio:");
Andreas Schwab6543bec2013-01-20 17:58:47 +0100896 ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
897 ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000898
Jean Delvareac551822008-05-02 20:37:21 +0200899 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000900 return 1;
901}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100902ADD_TO_DEVTABLE("virtio", virtio_device_id, do_virtio_entry);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000903
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700904/*
905 * Looks like: vmbus:guid
906 * Each byte of the guid will be represented by two hex characters
907 * in the name.
908 */
909
Andreas Schwab6543bec2013-01-20 17:58:47 +0100910static int do_vmbus_entry(const char *filename, void *symval,
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700911 char *alias)
912{
913 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100914 DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
915 char guid_name[(sizeof(*guid) + 1) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700916
Andreas Schwab6543bec2013-01-20 17:58:47 +0100917 for (i = 0; i < (sizeof(*guid) * 2); i += 2)
918 sprintf(&guid_name[i], "%02x", TO_NATIVE((*guid)[i/2]));
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700919
920 strcpy(alias, "vmbus:");
921 strcat(alias, guid_name);
922
923 return 1;
924}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100925ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id, do_vmbus_entry);
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700926
Jean Delvared2653e92008-04-29 23:11:39 +0200927/* Looks like: i2c:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100928static int do_i2c_entry(const char *filename, void *symval,
Jean Delvared2653e92008-04-29 23:11:39 +0200929 char *alias)
930{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100931 DEF_FIELD_ADDR(symval, i2c_device_id, name);
932 sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
Jean Delvared2653e92008-04-29 23:11:39 +0200933
934 return 1;
935}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100936ADD_TO_DEVTABLE("i2c", i2c_device_id, do_i2c_entry);
Jean Delvared2653e92008-04-29 23:11:39 +0200937
Anton Vorontsove0626e32009-09-22 16:46:08 -0700938/* Looks like: spi:S */
Andreas Schwab6543bec2013-01-20 17:58:47 +0100939static int do_spi_entry(const char *filename, void *symval,
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700940 char *alias)
941{
Andreas Schwab6543bec2013-01-20 17:58:47 +0100942 DEF_FIELD_ADDR(symval, spi_device_id, name);
943 sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700944
945 return 1;
946}
Andreas Schwab6543bec2013-01-20 17:58:47 +0100947ADD_TO_DEVTABLE("spi", spi_device_id, do_spi_entry);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700948
David Woodhoused945b692008-09-16 16:23:28 -0700949static const struct dmifield {
950 const char *prefix;
951 int field;
952} dmi_fields[] = {
953 { "bvn", DMI_BIOS_VENDOR },
954 { "bvr", DMI_BIOS_VERSION },
955 { "bd", DMI_BIOS_DATE },
956 { "svn", DMI_SYS_VENDOR },
957 { "pn", DMI_PRODUCT_NAME },
958 { "pvr", DMI_PRODUCT_VERSION },
959 { "rvn", DMI_BOARD_VENDOR },
960 { "rn", DMI_BOARD_NAME },
961 { "rvr", DMI_BOARD_VERSION },
962 { "cvn", DMI_CHASSIS_VENDOR },
963 { "ct", DMI_CHASSIS_TYPE },
964 { "cvr", DMI_CHASSIS_VERSION },
965 { NULL, DMI_NONE }
966};
967
968static void dmi_ascii_filter(char *d, const char *s)
969{
970 /* Filter out characters we don't want to see in the modalias string */
971 for (; *s; s++)
972 if (*s > ' ' && *s < 127 && *s != ':')
973 *(d++) = *s;
974
975 *d = 0;
976}
977
978
Andreas Schwab6543bec2013-01-20 17:58:47 +0100979static int do_dmi_entry(const char *filename, void *symval,
David Woodhoused945b692008-09-16 16:23:28 -0700980 char *alias)
981{
982 int i, j;
Andreas Schwab6543bec2013-01-20 17:58:47 +0100983 DEF_FIELD_ADDR(symval, dmi_system_id, matches);
David Woodhoused945b692008-09-16 16:23:28 -0700984 sprintf(alias, "dmi*");
985
986 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
987 for (j = 0; j < 4; j++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +0100988 if ((*matches)[j].slot &&
989 (*matches)[j].slot == dmi_fields[i].field) {
David Woodhoused945b692008-09-16 16:23:28 -0700990 sprintf(alias + strlen(alias), ":%s*",
991 dmi_fields[i].prefix);
992 dmi_ascii_filter(alias + strlen(alias),
Andreas Schwab6543bec2013-01-20 17:58:47 +0100993 (*matches)[j].substr);
David Woodhoused945b692008-09-16 16:23:28 -0700994 strcat(alias, "*");
995 }
996 }
997 }
998
999 strcat(alias, ":");
1000 return 1;
1001}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001002ADD_TO_DEVTABLE("dmi", dmi_system_id, do_dmi_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +08001003
1004static int do_platform_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001005 void *symval, char *alias)
Eric Miao57fee4a2009-02-04 11:52:40 +08001006{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001007 DEF_FIELD_ADDR(symval, platform_device_id, name);
1008 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
Eric Miao57fee4a2009-02-04 11:52:40 +08001009 return 1;
1010}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001011ADD_TO_DEVTABLE("platform", platform_device_id, do_platform_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +08001012
David Woodhouse8626d3b2010-04-02 01:05:27 +00001013static int do_mdio_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001014 void *symval, char *alias)
David Woodhouse8626d3b2010-04-02 01:05:27 +00001015{
1016 int i;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001017 DEF_FIELD(symval, mdio_device_id, phy_id);
1018 DEF_FIELD(symval, mdio_device_id, phy_id_mask);
David Woodhouse8626d3b2010-04-02 01:05:27 +00001019
1020 alias += sprintf(alias, MDIO_MODULE_PREFIX);
1021
1022 for (i = 0; i < 32; i++) {
Andreas Schwab6543bec2013-01-20 17:58:47 +01001023 if (!((phy_id_mask >> (31-i)) & 1))
David Woodhouse8626d3b2010-04-02 01:05:27 +00001024 *(alias++) = '?';
Andreas Schwab6543bec2013-01-20 17:58:47 +01001025 else if ((phy_id >> (31-i)) & 1)
David Woodhouse8626d3b2010-04-02 01:05:27 +00001026 *(alias++) = '1';
1027 else
1028 *(alias++) = '0';
1029 }
1030
1031 /* Terminate the string */
1032 *alias = 0;
1033
1034 return 1;
1035}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001036ADD_TO_DEVTABLE("mdio", mdio_device_id, do_mdio_entry);
David Woodhouse8626d3b2010-04-02 01:05:27 +00001037
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001038/* Looks like: zorro:iN. */
Andreas Schwab6543bec2013-01-20 17:58:47 +01001039static int do_zorro_entry(const char *filename, void *symval,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001040 char *alias)
1041{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001042 DEF_FIELD(symval, zorro_device_id, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001043 strcpy(alias, "zorro:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001044 ADD(alias, "i", id != ZORRO_WILDCARD, id);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001045 return 1;
1046}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001047ADD_TO_DEVTABLE("zorro", zorro_device_id, do_zorro_entry);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +01001048
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001049/* looks like: "pnp:dD" */
1050static int do_isapnp_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001051 void *symval, char *alias)
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001052{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001053 DEF_FIELD(symval, isapnp_device_id, vendor);
1054 DEF_FIELD(symval, isapnp_device_id, function);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001055 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001056 'A' + ((vendor >> 2) & 0x3f) - 1,
1057 'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1058 'A' + ((vendor >> 8) & 0x1f) - 1,
1059 (function >> 4) & 0x0f, function & 0x0f,
1060 (function >> 12) & 0x0f, (function >> 8) & 0x0f);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001061 return 1;
1062}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001063ADD_TO_DEVTABLE("isapnp", isapnp_device_id, do_isapnp_entry);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +01001064
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001065/* Looks like: "ipack:fNvNdN". */
1066static int do_ipack_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001067 void *symval, char *alias)
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001068{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001069 DEF_FIELD(symval, ipack_device_id, format);
1070 DEF_FIELD(symval, ipack_device_id, vendor);
1071 DEF_FIELD(symval, ipack_device_id, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001072 strcpy(alias, "ipack:");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001073 ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1074 ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1075 ADD(alias, "d", device != IPACK_ANY_ID, device);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001076 add_wildcard(alias);
1077 return 1;
1078}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001079ADD_TO_DEVTABLE("ipack", ipack_device_id, do_ipack_entry);
Jens Taprogge849e0ad2012-09-04 17:01:13 +02001080
Dave Martin523817b2011-10-05 14:44:57 +01001081/*
1082 * Append a match expression for a single masked hex digit.
1083 * outp points to a pointer to the character at which to append.
1084 * *outp is updated on return to point just after the appended text,
1085 * to facilitate further appending.
1086 */
1087static void append_nibble_mask(char **outp,
1088 unsigned int nibble, unsigned int mask)
1089{
1090 char *p = *outp;
1091 unsigned int i;
1092
1093 switch (mask) {
1094 case 0:
1095 *p++ = '?';
1096 break;
1097
1098 case 0xf:
1099 p += sprintf(p, "%X", nibble);
1100 break;
1101
1102 default:
1103 /*
1104 * Dumbly emit a match pattern for all possible matching
1105 * digits. This could be improved in some cases using ranges,
1106 * but it has the advantage of being trivially correct, and is
1107 * often optimal.
1108 */
1109 *p++ = '[';
1110 for (i = 0; i < 0x10; i++)
1111 if ((i & mask) == nibble)
1112 p += sprintf(p, "%X", i);
1113 *p++ = ']';
1114 }
1115
1116 /* Ensure that the string remains NUL-terminated: */
1117 *p = '\0';
1118
1119 /* Advance the caller's end-of-string pointer: */
1120 *outp = p;
1121}
1122
1123/*
1124 * looks like: "amba:dN"
1125 *
1126 * N is exactly 8 digits, where each is an upper-case hex digit, or
1127 * a ? or [] pattern matching exactly one digit.
1128 */
1129static int do_amba_entry(const char *filename,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001130 void *symval, char *alias)
Dave Martin523817b2011-10-05 14:44:57 +01001131{
1132 unsigned int digit;
1133 char *p = alias;
Andreas Schwab6543bec2013-01-20 17:58:47 +01001134 DEF_FIELD(symval, amba_id, id);
1135 DEF_FIELD(symval, amba_id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001136
Andreas Schwab6543bec2013-01-20 17:58:47 +01001137 if ((id & mask) != id)
Dave Martin523817b2011-10-05 14:44:57 +01001138 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1139 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
Andreas Schwab6543bec2013-01-20 17:58:47 +01001140 filename, id, mask);
Dave Martin523817b2011-10-05 14:44:57 +01001141
1142 p += sprintf(alias, "amba:d");
1143 for (digit = 0; digit < 8; digit++)
1144 append_nibble_mask(&p,
Andreas Schwab6543bec2013-01-20 17:58:47 +01001145 (id >> (4 * (7 - digit))) & 0xf,
1146 (mask >> (4 * (7 - digit))) & 0xf);
Dave Martin523817b2011-10-05 14:44:57 +01001147
1148 return 1;
1149}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001150ADD_TO_DEVTABLE("amba", amba_id, do_amba_entry);
Dave Martin523817b2011-10-05 14:44:57 +01001151
James Hogan8286ae02015-03-25 15:39:50 +00001152/*
1153 * looks like: "mipscdmm:tN"
1154 *
1155 * N is exactly 2 digits, where each is an upper-case hex digit, or
1156 * a ? or [] pattern matching exactly one digit.
1157 */
1158static int do_mips_cdmm_entry(const char *filename,
1159 void *symval, char *alias)
1160{
1161 DEF_FIELD(symval, mips_cdmm_device_id, type);
1162
1163 sprintf(alias, "mipscdmm:t%02X*", type);
1164 return 1;
1165}
1166ADD_TO_DEVTABLE("mipscdmm", mips_cdmm_device_id, do_mips_cdmm_entry);
1167
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001168/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
Andi Kleen644e9cb2012-01-26 00:09:05 +01001169 * All fields are numbers. It would be nicer to use strings for vendor
1170 * and feature, but getting those out of the build system here is too
1171 * complicated.
1172 */
1173
Andreas Schwab6543bec2013-01-20 17:58:47 +01001174static int do_x86cpu_entry(const char *filename, void *symval,
Andi Kleen644e9cb2012-01-26 00:09:05 +01001175 char *alias)
1176{
Andreas Schwab6543bec2013-01-20 17:58:47 +01001177 DEF_FIELD(symval, x86_cpu_id, feature);
1178 DEF_FIELD(symval, x86_cpu_id, family);
1179 DEF_FIELD(symval, x86_cpu_id, model);
1180 DEF_FIELD(symval, x86_cpu_id, vendor);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001181
Ard Biesheuvel2b9c1f02014-02-08 13:34:10 +01001182 strcpy(alias, "cpu:type:x86,");
1183 ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1184 ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1185 ADD(alias, "mod", model != X86_MODEL_ANY, model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001186 strcat(alias, ":feature:*");
Andreas Schwab6543bec2013-01-20 17:58:47 +01001187 if (feature != X86_FEATURE_ANY)
1188 sprintf(alias + strlen(alias), "%04X*", feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001189 return 1;
1190}
Andreas Schwab6543bec2013-01-20 17:58:47 +01001191ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001192
Ard Biesheuvel67bad2f2014-02-08 13:34:09 +01001193/* LOOKS like cpu:type:*:feature:*FEAT* */
1194static int do_cpu_entry(const char *filename, void *symval, char *alias)
1195{
1196 DEF_FIELD(symval, cpu_feature, feature);
1197
1198 sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1199 return 1;
1200}
1201ADD_TO_DEVTABLE("cpu", cpu_feature, do_cpu_entry);
1202
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001203/* Looks like: mei:S:uuid */
Samuel Ortize5354102013-03-27 17:29:53 +02001204static int do_mei_entry(const char *filename, void *symval,
1205 char *alias)
1206{
1207 DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001208 DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
Samuel Ortize5354102013-03-27 17:29:53 +02001209
Tomas Winklerc93b76b2015-05-07 15:54:02 +03001210 sprintf(alias, MEI_CL_MODULE_PREFIX);
1211 sprintf(alias + strlen(alias), "%s:", (*name)[0] ? *name : "*");
1212 add_uuid(alias, *uuid);
1213
1214 strcat(alias, ":*");
Samuel Ortize5354102013-03-27 17:29:53 +02001215
1216 return 1;
1217}
1218ADD_TO_DEVTABLE("mei", mei_cl_device_id, do_mei_entry);
1219
Alexandre Bounine3bdbb622013-07-03 15:08:58 -07001220/* Looks like: rapidio:vNdNavNadN */
1221static int do_rio_entry(const char *filename,
1222 void *symval, char *alias)
1223{
1224 DEF_FIELD(symval, rio_device_id, did);
1225 DEF_FIELD(symval, rio_device_id, vid);
1226 DEF_FIELD(symval, rio_device_id, asm_did);
1227 DEF_FIELD(symval, rio_device_id, asm_vid);
1228
1229 strcpy(alias, "rapidio:");
1230 ADD(alias, "v", vid != RIO_ANY_ID, vid);
1231 ADD(alias, "d", did != RIO_ANY_ID, did);
1232 ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1233 ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1234
1235 add_wildcard(alias);
1236 return 1;
1237}
1238ADD_TO_DEVTABLE("rapidio", rio_device_id, do_rio_entry);
1239
Heikki Krogerus289fcff2015-05-13 15:26:42 +03001240/* Looks like: ulpi:vNpN */
1241static int do_ulpi_entry(const char *filename, void *symval,
1242 char *alias)
1243{
1244 DEF_FIELD(symval, ulpi_device_id, vendor);
1245 DEF_FIELD(symval, ulpi_device_id, product);
1246
1247 sprintf(alias, "ulpi:v%04xp%04x", vendor, product);
1248
1249 return 1;
1250}
1251ADD_TO_DEVTABLE("ulpi", ulpi_device_id, do_ulpi_entry);
1252
Rusty Russell626596e2012-01-13 09:32:15 +10301253/* Does namelen bytes of name exactly match the symbol? */
1254static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Rusty Russell626596e2012-01-13 09:32:15 +10301256 if (namelen != strlen(symbol))
1257 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
Rusty Russell626596e2012-01-13 09:32:15 +10301259 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
1262static void do_table(void *symval, unsigned long size,
1263 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001264 const char *device_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 void *function,
1266 struct module *mod)
1267{
1268 unsigned int i;
1269 char alias[500];
1270 int (*do_entry)(const char *, void *entry, char *alias) = function;
1271
Kees Cooke0049822007-09-16 11:15:46 +02001272 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /* Leave last one: it's the terminator. */
1274 size -= id_size;
1275
1276 for (i = 0; i < size; i += id_size) {
1277 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 buf_printf(&mod->dev_table_buf,
1279 "MODULE_ALIAS(\"%s\");\n", alias);
1280 }
1281 }
1282}
1283
1284/* Create MODULE_ALIAS() statements.
1285 * At this time, we cannot write the actual output C source yet,
1286 * so we write into the mod->dev_table_buf buffer. */
1287void handle_moddevtable(struct module *mod, struct elf_info *info,
1288 Elf_Sym *sym, const char *symname)
1289{
1290 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001291 char *zeros = NULL;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301292 const char *name, *identifier;
Rusty Russell626596e2012-01-13 09:32:15 +10301293 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001296 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return;
1298
David Millere88aa7b2012-04-12 14:37:30 -04001299 /* We're looking for an object */
1300 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1301 return;
1302
Tom Gundersen21bdd172014-02-03 11:14:13 +10301303 /* All our symbols are of form <prefix>__mod_<name>__<identifier>_device_table. */
Rusty Russell626596e2012-01-13 09:32:15 +10301304 name = strstr(symname, "__mod_");
1305 if (!name)
1306 return;
1307 name += strlen("__mod_");
1308 namelen = strlen(name);
1309 if (namelen < strlen("_device_table"))
1310 return;
1311 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1312 return;
Tom Gundersen21bdd172014-02-03 11:14:13 +10301313 identifier = strstr(name, "__");
1314 if (!identifier)
1315 return;
1316 namelen = identifier - name;
Rusty Russell626596e2012-01-13 09:32:15 +10301317
Kees Cooke0049822007-09-16 11:15:46 +02001318 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001319 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001320 zeros = calloc(1, sym->st_size);
1321 symval = zeros;
1322 } else {
1323 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001324 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001325 + sym->st_value;
1326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Rusty Russell626596e2012-01-13 09:32:15 +10301328 /* First handle the "special" cases */
1329 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001330 do_usb_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301331 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001332 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301333 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001334 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301335 else {
Rusty Russelle49ce142012-01-13 09:32:16 +10301336 struct devtable **p;
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +01001337 INIT_SECTION(__devtable);
Rusty Russell626596e2012-01-13 09:32:15 +10301338
Rusty Russelle49ce142012-01-13 09:32:16 +10301339 for (p = __start___devtable; p < __stop___devtable; p++) {
1340 if (sym_is(name, namelen, (*p)->device_id)) {
1341 do_table(symval, sym->st_size, (*p)->id_size,
1342 (*p)->device_id, (*p)->function, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301343 break;
1344 }
1345 }
1346 }
Kees Cooke0049822007-09-16 11:15:46 +02001347 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
1350/* Now add out buffered information to the generated C source */
1351void add_moddevtable(struct buffer *buf, struct module *mod)
1352{
1353 buf_printf(buf, "\n");
1354 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1355 free(mod->dev_table_buf.p);
1356}