blob: df4fc23dd8360bf40673311511d6a4dac5b100b0 [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"
14
15/* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
16 * use either stdint.h or inttypes.h for the rest. */
17#if KERNEL_ELFCLASS == ELFCLASS32
18typedef Elf32_Addr kernel_ulong_t;
Rusty Russell1d8f4302005-12-07 21:40:34 +010019#define BITS_PER_LONG 32
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#else
21typedef Elf64_Addr kernel_ulong_t;
Rusty Russell1d8f4302005-12-07 21:40:34 +010022#define BITS_PER_LONG 64
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#endif
24#ifdef __sun__
25#include <inttypes.h>
26#else
27#include <stdint.h>
28#endif
29
Jeff Mahoney5e655772005-07-06 15:44:41 -040030#include <ctype.h>
Rusty Russell626596e2012-01-13 09:32:15 +103031#include <stdbool.h>
Jeff Mahoney5e655772005-07-06 15:44:41 -040032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033typedef uint32_t __u32;
34typedef uint16_t __u16;
35typedef unsigned char __u8;
36
37/* Big exception to the "don't include kernel headers into userspace, which
Sam Ravnborg62070fa2006-03-03 16:46:04 +010038 * even potentially has different endianness and word sizes, since
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * we handle those differences explicitly below */
40#include "../../include/linux/mod_devicetable.h"
41
Rusty Russelle49ce142012-01-13 09:32:16 +103042/* This array collects all instances that use the generic do_table */
43struct devtable {
44 const char *device_id; /* name of table, __mod_<name>_device_table. */
45 unsigned long id_size;
46 void *function;
47};
48
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010049#define ___cat(a,b) a ## b
50#define __cat(a,b) ___cat(a,b)
51
52/* we need some special handling for this host tool running eventually on
53 * Darwin. The Mach-O section handling is a bit different than ELF section
54 * handling. The differnces in detail are:
55 * a) we have segments which have sections
56 * b) we need a API call to get the respective section symbols */
57#if defined(__MACH__)
58#include <mach-o/getsect.h>
59
60#define INIT_SECTION(name) do { \
61 unsigned long name ## _len; \
62 char *__cat(pstart_,name) = getsectdata("__TEXT", \
63 #name, &__cat(name,_len)); \
64 char *__cat(pstop_,name) = __cat(pstart_,name) + \
65 __cat(name, _len); \
66 __cat(__start_,name) = (void *)__cat(pstart_,name); \
67 __cat(__stop_,name) = (void *)__cat(pstop_,name); \
68 } while (0)
69#define SECTION(name) __attribute__((section("__TEXT, " #name)))
70
71struct devtable **__start___devtable, **__stop___devtable;
72#else
73#define INIT_SECTION(name) /* no-op for ELF */
74#define SECTION(name) __attribute__((section(#name)))
75
Rusty Russelle49ce142012-01-13 09:32:16 +103076/* We construct a table of pointers in an ELF section (pointers generally
77 * go unpadded by gcc). ld creates boundary syms for us. */
78extern struct devtable *__start___devtable[], *__stop___devtable[];
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010079#endif /* __MACH__ */
Rusty Russelle49ce142012-01-13 09:32:16 +103080
81#if __GNUC__ == 3 && __GNUC_MINOR__ < 3
82# define __used __attribute__((__unused__))
83#else
84# define __used __attribute__((__used__))
85#endif
86
87/* Add a table entry. We test function type matches while we're here. */
88#define ADD_TO_DEVTABLE(device_id, type, function) \
89 static struct devtable __cat(devtable,__LINE__) = { \
90 device_id + 0*sizeof((function)((const char *)NULL, \
91 (type *)NULL, \
92 (char *)NULL)), \
93 sizeof(type), (function) }; \
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +010094 static struct devtable *SECTION(__devtable) __used \
95 __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
Rusty Russelle49ce142012-01-13 09:32:16 +103096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#define ADD(str, sep, cond, field) \
98do { \
99 strcat(str, sep); \
100 if (cond) \
101 sprintf(str + strlen(str), \
102 sizeof(field) == 1 ? "%02X" : \
103 sizeof(field) == 2 ? "%04X" : \
104 sizeof(field) == 4 ? "%08X" : "", \
105 field); \
106 else \
107 sprintf(str + strlen(str), "*"); \
108} while(0)
109
Jean Delvareac551822008-05-02 20:37:21 +0200110/* Always end in a wildcard, for future extension */
111static inline void add_wildcard(char *str)
112{
113 int len = strlen(str);
114
115 if (str[len - 1] != '*')
116 strcat(str + len, "*");
117}
118
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100119unsigned int cross_build = 0;
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200120/**
121 * Check that sizeof(device_id type) are consistent with size of section
122 * in .o file. If in-consistent then userspace and kernel does not agree
123 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +0200124 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100125 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200126 **/
Kees Cooke0049822007-09-16 11:15:46 +0200127static void device_id_check(const char *modname, const char *device_id,
128 unsigned long size, unsigned long id_size,
129 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200130{
Kees Cooke0049822007-09-16 11:15:46 +0200131 int i;
132
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200133 if (size % id_size || size < id_size) {
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100134 if (cross_build != 0)
135 return;
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200136 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
137 "of the size of section __mod_%s_device_table=%lu.\n"
138 "Fix definition of struct %s_device_id "
139 "in mod_devicetable.h\n",
140 modname, device_id, id_size, device_id, size, device_id);
141 }
Kees Cooke0049822007-09-16 11:15:46 +0200142 /* Verify last one is a terminator */
143 for (i = 0; i < id_size; i++ ) {
144 if (*(uint8_t*)(symval+size-id_size+i)) {
145 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
146 "The last of %lu is:\n",
147 modname, device_id, id_size, size / id_size);
148 for (i = 0; i < id_size; i++ )
149 fprintf(stderr,"0x%02x ",
150 *(uint8_t*)(symval+size-id_size+i) );
151 fprintf(stderr,"\n");
152 fatal("%s: struct %s_device_id is not terminated "
153 "with a NULL entry!\n", modname, device_id);
154 }
155 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200156}
157
Roman Kaganb19dcd92005-04-22 15:07:01 -0700158/* USB is special because the bcdDevice can be matched against a numeric range */
Bjørn Mork81df2d52012-05-18 21:27:43 +0200159/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
Roman Kaganb19dcd92005-04-22 15:07:01 -0700160static void do_usb_entry(struct usb_device_id *id,
161 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
162 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500163 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700165 char alias[500];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 strcpy(alias, "usb:");
167 ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
168 id->idVendor);
169 ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
170 id->idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700171
172 strcat(alias, "d");
173 if (bcdDevice_initial_digits)
174 sprintf(alias + strlen(alias), "%0*X",
175 bcdDevice_initial_digits, bcdDevice_initial);
176 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500177 sprintf(alias + strlen(alias), "%X", range_lo);
178 else if (range_lo > 0 || range_hi < max) {
179 if (range_lo > 0x9 || range_hi < 0xA)
180 sprintf(alias + strlen(alias),
181 "[%X-%X]",
182 range_lo,
183 range_hi);
184 else {
185 sprintf(alias + strlen(alias),
186 range_lo < 0x9 ? "[%X-9" : "[%X",
187 range_lo);
188 sprintf(alias + strlen(alias),
189 range_hi > 0xA ? "a-%X]" : "%X]",
190 range_lo);
191 }
192 }
Roman Kaganb19dcd92005-04-22 15:07:01 -0700193 if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
194 strcat(alias, "*");
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
197 id->bDeviceClass);
198 ADD(alias, "dsc",
199 id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
200 id->bDeviceSubClass);
201 ADD(alias, "dp",
202 id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
203 id->bDeviceProtocol);
204 ADD(alias, "ic",
205 id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
206 id->bInterfaceClass);
207 ADD(alias, "isc",
208 id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
209 id->bInterfaceSubClass);
210 ADD(alias, "ip",
211 id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
212 id->bInterfaceProtocol);
Bjørn Mork81df2d52012-05-18 21:27:43 +0200213 ADD(alias, "in",
214 id->match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
215 id->bInterfaceNumber);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700216
Jean Delvareac551822008-05-02 20:37:21 +0200217 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700218 buf_printf(&mod->dev_table_buf,
219 "MODULE_ALIAS(\"%s\");\n", alias);
220}
221
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500222/* Handles increment/decrement of BCD formatted integers */
223/* Returns the previous value, so it works like i++ or i-- */
224static unsigned int incbcd(unsigned int *bcd,
225 int inc,
226 unsigned char max,
227 size_t chars)
228{
229 unsigned int init = *bcd, i, j;
230 unsigned long long c, dec = 0;
231
232 /* If bcd is not in BCD format, just increment */
233 if (max > 0x9) {
234 *bcd += inc;
235 return init;
236 }
237
238 /* Convert BCD to Decimal */
239 for (i=0 ; i < chars ; i++) {
240 c = (*bcd >> (i << 2)) & 0xf;
241 c = c > 9 ? 9 : c; /* force to bcd just in case */
242 for (j=0 ; j < i ; j++)
243 c = c * 10;
244 dec += c;
245 }
246
247 /* Do our increment/decrement */
248 dec += inc;
249 *bcd = 0;
250
251 /* Convert back to BCD */
252 for (i=0 ; i < chars ; i++) {
253 for (c=1,j=0 ; j < i ; j++)
254 c = c * 10;
255 c = (dec / c) % 10;
256 *bcd += c << (i << 2);
257 }
258 return init;
259}
260
Roman Kaganb19dcd92005-04-22 15:07:01 -0700261static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
262{
263 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500264 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700265 int ndigits;
266
267 id->match_flags = TO_NATIVE(id->match_flags);
268 id->idVendor = TO_NATIVE(id->idVendor);
269 id->idProduct = TO_NATIVE(id->idProduct);
270
271 devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
272 TO_NATIVE(id->bcdDevice_lo) : 0x0U;
273 devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
274 TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
275
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500276 /* Figure out if this entry is in bcd or hex format */
277 max = 0x9; /* Default to decimal format */
278 for (ndigits = 0 ; ndigits < sizeof(id->bcdDevice_lo) * 2 ; ndigits++) {
279 clo = (devlo >> (ndigits << 2)) & 0xf;
280 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
281 if (clo > max || chi > max) {
282 max = 0xf;
283 break;
284 }
285 }
286
Roman Kaganb19dcd92005-04-22 15:07:01 -0700287 /*
288 * Some modules (visor) have empty slots as placeholder for
289 * run-time specification that results in catch-all alias
290 */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800291 if (!(id->idVendor | id->idProduct | id->bDeviceClass | id->bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700292 return;
293
294 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
295 for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
296 clo = devlo & 0xf;
297 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500298 if (chi > max) /* If we are in bcd mode, truncate if necessary */
299 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700300 devlo >>= 4;
301 devhi >>= 4;
302
303 if (devlo == devhi || !ndigits) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500304 do_usb_entry(id, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700305 break;
306 }
307
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500308 if (clo > 0x0)
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500309 do_usb_entry(id,
310 incbcd(&devlo, 1, max,
311 sizeof(id->bcdDevice_lo) * 2),
312 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700313
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500314 if (chi < max)
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500315 do_usb_entry(id,
316 incbcd(&devhi, -1, max,
317 sizeof(id->bcdDevice_lo) * 2),
318 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700319 }
320}
321
322static void do_usb_table(void *symval, unsigned long size,
323 struct module *mod)
324{
325 unsigned int i;
326 const unsigned long id_size = sizeof(struct usb_device_id);
327
Kees Cooke0049822007-09-16 11:15:46 +0200328 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200329
Roman Kaganb19dcd92005-04-22 15:07:01 -0700330 /* Leave last one: it's the terminator. */
331 size -= id_size;
332
333 for (i = 0; i < size; i += id_size)
334 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Jiri Slabye8c84f92008-05-19 15:50:01 +0200337/* Looks like: hid:bNvNpN */
338static int do_hid_entry(const char *filename,
339 struct hid_device_id *id, char *alias)
340{
Jiri Slaby2b639382009-02-17 12:38:36 +0100341 id->bus = TO_NATIVE(id->bus);
Henrik Rydberg4d53b802012-04-23 12:07:02 +0200342 id->group = TO_NATIVE(id->group);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200343 id->vendor = TO_NATIVE(id->vendor);
344 id->product = TO_NATIVE(id->product);
345
Henrik Rydberg7431fb72012-04-23 12:07:04 +0200346 sprintf(alias, "hid:");
347 ADD(alias, "b", id->bus != HID_BUS_ANY, id->bus);
Henrik Rydberg4d53b802012-04-23 12:07:02 +0200348 ADD(alias, "g", id->group != HID_GROUP_ANY, id->group);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200349 ADD(alias, "v", id->vendor != HID_ANY_ID, id->vendor);
350 ADD(alias, "p", id->product != HID_ANY_ID, id->product);
351
352 return 1;
353}
Rusty Russelle49ce142012-01-13 09:32:16 +1030354ADD_TO_DEVTABLE("hid", struct hid_device_id, do_hid_entry);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/* Looks like: ieee1394:venNmoNspNverN */
357static int do_ieee1394_entry(const char *filename,
358 struct ieee1394_device_id *id, char *alias)
359{
360 id->match_flags = TO_NATIVE(id->match_flags);
361 id->vendor_id = TO_NATIVE(id->vendor_id);
362 id->model_id = TO_NATIVE(id->model_id);
363 id->specifier_id = TO_NATIVE(id->specifier_id);
364 id->version = TO_NATIVE(id->version);
365
366 strcpy(alias, "ieee1394:");
367 ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
368 id->vendor_id);
369 ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
370 id->model_id);
371 ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
372 id->specifier_id);
373 ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
374 id->version);
375
Jean Delvareac551822008-05-02 20:37:21 +0200376 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 1;
378}
Rusty Russelle49ce142012-01-13 09:32:16 +1030379ADD_TO_DEVTABLE("ieee1394", struct ieee1394_device_id, do_ieee1394_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
382static int do_pci_entry(const char *filename,
383 struct pci_device_id *id, char *alias)
384{
385 /* Class field can be divided into these three. */
386 unsigned char baseclass, subclass, interface,
387 baseclass_mask, subclass_mask, interface_mask;
388
389 id->vendor = TO_NATIVE(id->vendor);
390 id->device = TO_NATIVE(id->device);
391 id->subvendor = TO_NATIVE(id->subvendor);
392 id->subdevice = TO_NATIVE(id->subdevice);
393 id->class = TO_NATIVE(id->class);
394 id->class_mask = TO_NATIVE(id->class_mask);
395
396 strcpy(alias, "pci:");
397 ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
398 ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
399 ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
400 ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
401
402 baseclass = (id->class) >> 16;
403 baseclass_mask = (id->class_mask) >> 16;
404 subclass = (id->class) >> 8;
405 subclass_mask = (id->class_mask) >> 8;
406 interface = id->class;
407 interface_mask = id->class_mask;
408
409 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
410 || (subclass_mask != 0 && subclass_mask != 0xFF)
411 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100412 warn("Can't handle masks in %s:%04X\n",
413 filename, id->class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return 0;
415 }
416
417 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
418 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
419 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200420 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 return 1;
422}
Rusty Russelle49ce142012-01-13 09:32:16 +1030423ADD_TO_DEVTABLE("pci", struct pci_device_id, do_pci_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100425/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426static int do_ccw_entry(const char *filename,
427 struct ccw_device_id *id, char *alias)
428{
429 id->match_flags = TO_NATIVE(id->match_flags);
430 id->cu_type = TO_NATIVE(id->cu_type);
431 id->cu_model = TO_NATIVE(id->cu_model);
432 id->dev_type = TO_NATIVE(id->dev_type);
433 id->dev_model = TO_NATIVE(id->dev_model);
434
435 strcpy(alias, "ccw:");
436 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
437 id->cu_type);
438 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
439 id->cu_model);
440 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
441 id->dev_type);
Bastian Blankde1d9c02006-03-06 15:43:00 -0800442 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 id->dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200444 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return 1;
446}
Rusty Russelle49ce142012-01-13 09:32:16 +1030447ADD_TO_DEVTABLE("ccw", struct ccw_device_id, do_ccw_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200449/* looks like: "ap:tN" */
450static int do_ap_entry(const char *filename,
451 struct ap_device_id *id, char *alias)
452{
Jean Delvareac551822008-05-02 20:37:21 +0200453 sprintf(alias, "ap:t%02X*", id->dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200454 return 1;
455}
Rusty Russelle49ce142012-01-13 09:32:16 +1030456ADD_TO_DEVTABLE("ap", struct ap_device_id, do_ap_entry);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200457
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200458/* looks like: "css:tN" */
459static int do_css_entry(const char *filename,
460 struct css_device_id *id, char *alias)
461{
462 sprintf(alias, "css:t%01X", id->type);
463 return 1;
464}
Rusty Russelle49ce142012-01-13 09:32:16 +1030465ADD_TO_DEVTABLE("css", struct css_device_id, do_css_entry);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/* Looks like: "serio:tyNprNidNexN" */
468static int do_serio_entry(const char *filename,
469 struct serio_device_id *id, char *alias)
470{
471 id->type = TO_NATIVE(id->type);
472 id->proto = TO_NATIVE(id->proto);
473 id->id = TO_NATIVE(id->id);
474 id->extra = TO_NATIVE(id->extra);
475
476 strcpy(alias, "serio:");
477 ADD(alias, "ty", id->type != SERIO_ANY, id->type);
478 ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
479 ADD(alias, "id", id->id != SERIO_ANY, id->id);
480 ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
481
Jean Delvareac551822008-05-02 20:37:21 +0200482 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 1;
484}
Rusty Russelle49ce142012-01-13 09:32:16 +1030485ADD_TO_DEVTABLE("serio", struct serio_device_id, do_serio_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Thomas Renninger29b71a12007-07-23 14:43:51 +0200487/* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
488static int do_acpi_entry(const char *filename,
489 struct acpi_device_id *id, char *alias)
490{
Jean Delvareac551822008-05-02 20:37:21 +0200491 sprintf(alias, "acpi*:%s:*", id->id);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200492 return 1;
493}
Rusty Russelle49ce142012-01-13 09:32:16 +1030494ADD_TO_DEVTABLE("acpi", struct acpi_device_id, do_acpi_entry);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200497static void do_pnp_device_entry(void *symval, unsigned long size,
498 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Kay Sievers22454cb2008-05-28 23:06:47 +0200500 const unsigned long id_size = sizeof(struct pnp_device_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200501 const unsigned int count = (size / id_size)-1;
502 const struct pnp_device_id *devs = symval;
503 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200504
505 device_id_check(mod->name, "pnp", size, id_size, symval);
506
Kay Sievers5e4c6562008-08-21 15:28:56 +0200507 for (i = 0; i < count; i++) {
508 const char *id = (char *)devs[i].id;
Kay Sievers72638f52009-01-08 03:06:42 +0100509 char acpi_id[sizeof(devs[0].id)];
510 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200511
512 buf_printf(&mod->dev_table_buf,
513 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100514
515 /* fix broken pnp bus lowercasing */
516 for (j = 0; j < sizeof(acpi_id); j++)
517 acpi_id[j] = toupper(id[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200518 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100519 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Kay Sievers0c81eed2008-02-21 00:35:54 +0100523/* looks like: "pnp:dD" for every device of the card */
524static void do_pnp_card_entries(void *symval, unsigned long size,
525 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Kay Sievers0c81eed2008-02-21 00:35:54 +0100527 const unsigned long id_size = sizeof(struct pnp_card_device_id);
528 const unsigned int count = (size / id_size)-1;
529 const struct pnp_card_device_id *cards = symval;
530 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Kay Sievers0c81eed2008-02-21 00:35:54 +0100532 device_id_check(mod->name, "pnp", size, id_size, symval);
533
534 for (i = 0; i < count; i++) {
535 unsigned int j;
536 const struct pnp_card_device_id *card = &cards[i];
537
538 for (j = 0; j < PNP_MAX_DEVICES; j++) {
539 const char *id = (char *)card->devs[j].id;
540 int i2, j2;
541 int dup = 0;
542
543 if (!id[0])
544 break;
545
546 /* find duplicate, already added value */
547 for (i2 = 0; i2 < i && !dup; i2++) {
548 const struct pnp_card_device_id *card2 = &cards[i2];
549
550 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
551 const char *id2 = (char *)card2->devs[j2].id;
552
553 if (!id2[0])
554 break;
555
556 if (!strcmp(id, id2)) {
557 dup = 1;
558 break;
559 }
560 }
561 }
562
563 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200564 if (!dup) {
Kay Sievers72638f52009-01-08 03:06:42 +0100565 char acpi_id[sizeof(card->devs[0].id)];
566 int k;
567
Kay Sievers0c81eed2008-02-21 00:35:54 +0100568 buf_printf(&mod->dev_table_buf,
569 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100570
571 /* fix broken pnp bus lowercasing */
572 for (k = 0; k < sizeof(acpi_id); k++)
573 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200574 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100575 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200576 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100577 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700581/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
582static int do_pcmcia_entry(const char *filename,
583 struct pcmcia_device_id *id, char *alias)
584{
585 unsigned int i;
586
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200587 id->match_flags = TO_NATIVE(id->match_flags);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700588 id->manf_id = TO_NATIVE(id->manf_id);
589 id->card_id = TO_NATIVE(id->card_id);
590 id->func_id = TO_NATIVE(id->func_id);
591 id->function = TO_NATIVE(id->function);
592 id->device_no = TO_NATIVE(id->device_no);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200593
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700594 for (i=0; i<4; i++) {
595 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
596 }
597
598 strcpy(alias, "pcmcia:");
599 ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
600 id->manf_id);
601 ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
602 id->card_id);
603 ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
604 id->func_id);
605 ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
606 id->function);
607 ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
608 id->device_no);
609 ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
610 ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
611 ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
612 ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
613
Jean Delvareac551822008-05-02 20:37:21 +0200614 add_wildcard(alias);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700615 return 1;
616}
Rusty Russelle49ce142012-01-13 09:32:16 +1030617ADD_TO_DEVTABLE("pcmcia", struct pcmcia_device_id, do_pcmcia_entry);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700618
Jeff Mahoney5e655772005-07-06 15:44:41 -0400619static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
620{
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000621 int len;
Jeff Mahoney5e655772005-07-06 15:44:41 -0400622 char *tmp;
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000623 len = sprintf (alias, "of:N%sT%s",
Jeff Mahoney5e655772005-07-06 15:44:41 -0400624 of->name[0] ? of->name : "*",
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000625 of->type[0] ? of->type : "*");
626
627 if (of->compatible[0])
628 sprintf (&alias[len], "%sC%s",
629 of->type[0] ? "*" : "",
630 of->compatible);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400631
632 /* Replace all whitespace with underscores */
633 for (tmp = alias; tmp && *tmp; tmp++)
634 if (isspace (*tmp))
635 *tmp = '_';
636
Jean Delvareac551822008-05-02 20:37:21 +0200637 add_wildcard(alias);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400638 return 1;
639}
Rusty Russelle49ce142012-01-13 09:32:16 +1030640ADD_TO_DEVTABLE("of", struct of_device_id, do_of_entry);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400641
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000642static int do_vio_entry(const char *filename, struct vio_device_id *vio,
643 char *alias)
644{
645 char *tmp;
646
647 sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
648 vio->compat[0] ? vio->compat : "*");
649
650 /* Replace all whitespace with underscores */
651 for (tmp = alias; tmp && *tmp; tmp++)
652 if (isspace (*tmp))
653 *tmp = '_';
654
Jean Delvareac551822008-05-02 20:37:21 +0200655 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000656 return 1;
657}
Rusty Russelle49ce142012-01-13 09:32:16 +1030658ADD_TO_DEVTABLE("vio", struct vio_device_id, do_vio_entry);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000659
Rusty Russell1d8f4302005-12-07 21:40:34 +0100660#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
661
662static void do_input(char *alias,
663 kernel_ulong_t *arr, unsigned int min, unsigned int max)
664{
665 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400666
667 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200668 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400669 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100670}
671
672/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
673static int do_input_entry(const char *filename, struct input_device_id *id,
674 char *alias)
675{
676 sprintf(alias, "input:");
677
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400678 ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
679 ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
680 ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
681 ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100682
683 sprintf(alias + strlen(alias), "-e*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400684 if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100685 do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100686 sprintf(alias + strlen(alias), "k*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400687 if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100688 do_input(alias, id->keybit,
689 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
690 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100691 sprintf(alias + strlen(alias), "r*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400692 if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100693 do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100694 sprintf(alias + strlen(alias), "a*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400695 if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100696 do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100697 sprintf(alias + strlen(alias), "m*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400698 if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100699 do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100700 sprintf(alias + strlen(alias), "l*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400701 if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100702 do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100703 sprintf(alias + strlen(alias), "s*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400704 if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100705 do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100706 sprintf(alias + strlen(alias), "f*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400707 if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100708 do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100709 sprintf(alias + strlen(alias), "w*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400710 if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100711 do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100712 return 1;
713}
Rusty Russelle49ce142012-01-13 09:32:16 +1030714ADD_TO_DEVTABLE("input", struct input_device_id, do_input_entry);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100715
Michael Tokarev07563c72006-09-27 01:50:56 -0700716static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
717 char *alias)
718{
719 if (eisa->sig[0])
720 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
Jean Delvareac551822008-05-02 20:37:21 +0200721 else
722 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700723 return 1;
724}
Rusty Russelle49ce142012-01-13 09:32:16 +1030725ADD_TO_DEVTABLE("eisa", struct eisa_device_id, do_eisa_entry);
Michael Tokarev07563c72006-09-27 01:50:56 -0700726
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500727/* Looks like: parisc:tNhvNrevNsvN */
728static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
729 char *alias)
730{
731 id->hw_type = TO_NATIVE(id->hw_type);
732 id->hversion = TO_NATIVE(id->hversion);
733 id->hversion_rev = TO_NATIVE(id->hversion_rev);
734 id->sversion = TO_NATIVE(id->sversion);
735
736 strcpy(alias, "parisc:");
Kyle McMartinf354ef82007-01-13 15:02:09 -0500737 ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type);
738 ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion);
739 ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev);
740 ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500741
Jean Delvareac551822008-05-02 20:37:21 +0200742 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500743 return 1;
744}
Rusty Russelle49ce142012-01-13 09:32:16 +1030745ADD_TO_DEVTABLE("parisc", struct parisc_device_id, do_parisc_entry);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500746
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200747/* Looks like: sdio:cNvNdN. */
748static int do_sdio_entry(const char *filename,
749 struct sdio_device_id *id, char *alias)
750{
751 id->class = TO_NATIVE(id->class);
752 id->vendor = TO_NATIVE(id->vendor);
753 id->device = TO_NATIVE(id->device);
754
755 strcpy(alias, "sdio:");
756 ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class);
757 ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor);
758 ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device);
Jean Delvareac551822008-05-02 20:37:21 +0200759 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700760 return 1;
761}
Rusty Russelle49ce142012-01-13 09:32:16 +1030762ADD_TO_DEVTABLE("sdio", struct sdio_device_id, do_sdio_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200763
Michael Buesch61e115a2007-09-18 15:12:50 -0400764/* Looks like: ssb:vNidNrevN. */
765static int do_ssb_entry(const char *filename,
766 struct ssb_device_id *id, char *alias)
767{
768 id->vendor = TO_NATIVE(id->vendor);
769 id->coreid = TO_NATIVE(id->coreid);
770 id->revision = TO_NATIVE(id->revision);
771
772 strcpy(alias, "ssb:");
773 ADD(alias, "v", id->vendor != SSB_ANY_VENDOR, id->vendor);
774 ADD(alias, "id", id->coreid != SSB_ANY_ID, id->coreid);
775 ADD(alias, "rev", id->revision != SSB_ANY_REV, id->revision);
Jean Delvareac551822008-05-02 20:37:21 +0200776 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200777 return 1;
778}
Rusty Russelle49ce142012-01-13 09:32:16 +1030779ADD_TO_DEVTABLE("ssb", struct ssb_device_id, do_ssb_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200780
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200781/* Looks like: bcma:mNidNrevNclN. */
782static int do_bcma_entry(const char *filename,
783 struct bcma_device_id *id, char *alias)
784{
785 id->manuf = TO_NATIVE(id->manuf);
786 id->id = TO_NATIVE(id->id);
787 id->rev = TO_NATIVE(id->rev);
788 id->class = TO_NATIVE(id->class);
789
790 strcpy(alias, "bcma:");
791 ADD(alias, "m", id->manuf != BCMA_ANY_MANUF, id->manuf);
792 ADD(alias, "id", id->id != BCMA_ANY_ID, id->id);
793 ADD(alias, "rev", id->rev != BCMA_ANY_REV, id->rev);
794 ADD(alias, "cl", id->class != BCMA_ANY_CLASS, id->class);
795 add_wildcard(alias);
796 return 1;
797}
Rusty Russelle49ce142012-01-13 09:32:16 +1030798ADD_TO_DEVTABLE("bcma", struct bcma_device_id, do_bcma_entry);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200799
Rusty Russellb01d9f22007-10-22 11:03:39 +1000800/* Looks like: virtio:dNvN */
801static int do_virtio_entry(const char *filename, struct virtio_device_id *id,
802 char *alias)
803{
804 id->device = TO_NATIVE(id->device);
805 id->vendor = TO_NATIVE(id->vendor);
806
807 strcpy(alias, "virtio:");
Christian Borntraegere3353852009-05-26 15:46:10 +0200808 ADD(alias, "d", id->device != VIRTIO_DEV_ANY_ID, id->device);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000809 ADD(alias, "v", id->vendor != VIRTIO_DEV_ANY_ID, id->vendor);
810
Jean Delvareac551822008-05-02 20:37:21 +0200811 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000812 return 1;
813}
Rusty Russelle49ce142012-01-13 09:32:16 +1030814ADD_TO_DEVTABLE("virtio", struct virtio_device_id, do_virtio_entry);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000815
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700816/*
817 * Looks like: vmbus:guid
818 * Each byte of the guid will be represented by two hex characters
819 * in the name.
820 */
821
822static int do_vmbus_entry(const char *filename, struct hv_vmbus_device_id *id,
823 char *alias)
824{
825 int i;
Greg Kroah-Hartmanebf16e32011-08-25 11:28:11 -0700826 char guid_name[((sizeof(id->guid) + 1)) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700827
Greg Kroah-Hartmanebf16e32011-08-25 11:28:11 -0700828 for (i = 0; i < (sizeof(id->guid) * 2); i += 2)
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700829 sprintf(&guid_name[i], "%02x", id->guid[i/2]);
830
831 strcpy(alias, "vmbus:");
832 strcat(alias, guid_name);
833
834 return 1;
835}
Rusty Russelle49ce142012-01-13 09:32:16 +1030836ADD_TO_DEVTABLE("vmbus", struct hv_vmbus_device_id, do_vmbus_entry);
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700837
Jean Delvared2653e92008-04-29 23:11:39 +0200838/* Looks like: i2c:S */
839static int do_i2c_entry(const char *filename, struct i2c_device_id *id,
840 char *alias)
841{
842 sprintf(alias, I2C_MODULE_PREFIX "%s", id->name);
843
844 return 1;
845}
Rusty Russelle49ce142012-01-13 09:32:16 +1030846ADD_TO_DEVTABLE("i2c", struct i2c_device_id, do_i2c_entry);
Jean Delvared2653e92008-04-29 23:11:39 +0200847
Anton Vorontsove0626e32009-09-22 16:46:08 -0700848/* Looks like: spi:S */
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700849static int do_spi_entry(const char *filename, struct spi_device_id *id,
850 char *alias)
851{
Anton Vorontsove0626e32009-09-22 16:46:08 -0700852 sprintf(alias, SPI_MODULE_PREFIX "%s", id->name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700853
854 return 1;
855}
Rusty Russelle49ce142012-01-13 09:32:16 +1030856ADD_TO_DEVTABLE("spi", struct spi_device_id, do_spi_entry);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700857
David Woodhoused945b692008-09-16 16:23:28 -0700858static const struct dmifield {
859 const char *prefix;
860 int field;
861} dmi_fields[] = {
862 { "bvn", DMI_BIOS_VENDOR },
863 { "bvr", DMI_BIOS_VERSION },
864 { "bd", DMI_BIOS_DATE },
865 { "svn", DMI_SYS_VENDOR },
866 { "pn", DMI_PRODUCT_NAME },
867 { "pvr", DMI_PRODUCT_VERSION },
868 { "rvn", DMI_BOARD_VENDOR },
869 { "rn", DMI_BOARD_NAME },
870 { "rvr", DMI_BOARD_VERSION },
871 { "cvn", DMI_CHASSIS_VENDOR },
872 { "ct", DMI_CHASSIS_TYPE },
873 { "cvr", DMI_CHASSIS_VERSION },
874 { NULL, DMI_NONE }
875};
876
877static void dmi_ascii_filter(char *d, const char *s)
878{
879 /* Filter out characters we don't want to see in the modalias string */
880 for (; *s; s++)
881 if (*s > ' ' && *s < 127 && *s != ':')
882 *(d++) = *s;
883
884 *d = 0;
885}
886
887
888static int do_dmi_entry(const char *filename, struct dmi_system_id *id,
889 char *alias)
890{
891 int i, j;
892
893 sprintf(alias, "dmi*");
894
895 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
896 for (j = 0; j < 4; j++) {
897 if (id->matches[j].slot &&
898 id->matches[j].slot == dmi_fields[i].field) {
899 sprintf(alias + strlen(alias), ":%s*",
900 dmi_fields[i].prefix);
901 dmi_ascii_filter(alias + strlen(alias),
902 id->matches[j].substr);
903 strcat(alias, "*");
904 }
905 }
906 }
907
908 strcat(alias, ":");
909 return 1;
910}
Rusty Russelle49ce142012-01-13 09:32:16 +1030911ADD_TO_DEVTABLE("dmi", struct dmi_system_id, do_dmi_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +0800912
913static int do_platform_entry(const char *filename,
914 struct platform_device_id *id, char *alias)
915{
916 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", id->name);
917 return 1;
918}
Rusty Russelle49ce142012-01-13 09:32:16 +1030919ADD_TO_DEVTABLE("platform", struct platform_device_id, do_platform_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +0800920
David Woodhouse8626d3b2010-04-02 01:05:27 +0000921static int do_mdio_entry(const char *filename,
922 struct mdio_device_id *id, char *alias)
923{
924 int i;
925
926 alias += sprintf(alias, MDIO_MODULE_PREFIX);
927
928 for (i = 0; i < 32; i++) {
929 if (!((id->phy_id_mask >> (31-i)) & 1))
930 *(alias++) = '?';
931 else if ((id->phy_id >> (31-i)) & 1)
932 *(alias++) = '1';
933 else
934 *(alias++) = '0';
935 }
936
937 /* Terminate the string */
938 *alias = 0;
939
940 return 1;
941}
Rusty Russelle49ce142012-01-13 09:32:16 +1030942ADD_TO_DEVTABLE("mdio", struct mdio_device_id, do_mdio_entry);
David Woodhouse8626d3b2010-04-02 01:05:27 +0000943
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100944/* Looks like: zorro:iN. */
945static int do_zorro_entry(const char *filename, struct zorro_device_id *id,
946 char *alias)
947{
948 id->id = TO_NATIVE(id->id);
949 strcpy(alias, "zorro:");
950 ADD(alias, "i", id->id != ZORRO_WILDCARD, id->id);
951 return 1;
952}
Rusty Russelle49ce142012-01-13 09:32:16 +1030953ADD_TO_DEVTABLE("zorro", struct zorro_device_id, do_zorro_entry);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100954
Ondrej Zaryfedb3d22009-12-18 20:52:39 +0100955/* looks like: "pnp:dD" */
956static int do_isapnp_entry(const char *filename,
957 struct isapnp_device_id *id, char *alias)
958{
959 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
960 'A' + ((id->vendor >> 2) & 0x3f) - 1,
961 'A' + (((id->vendor & 3) << 3) | ((id->vendor >> 13) & 7)) - 1,
962 'A' + ((id->vendor >> 8) & 0x1f) - 1,
963 (id->function >> 4) & 0x0f, id->function & 0x0f,
964 (id->function >> 12) & 0x0f, (id->function >> 8) & 0x0f);
965 return 1;
966}
Ondrej Zary0d86f652012-02-10 20:12:27 +0100967ADD_TO_DEVTABLE("isapnp", struct isapnp_device_id, do_isapnp_entry);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +0100968
Jens Taprogge849e0ad2012-09-04 17:01:13 +0200969/* Looks like: "ipack:fNvNdN". */
970static int do_ipack_entry(const char *filename,
971 struct ipack_device_id *id, char *alias)
972{
973 id->vendor = TO_NATIVE(id->vendor);
974 id->device = TO_NATIVE(id->device);
975 strcpy(alias, "ipack:");
Jens Taprogge5948ae22012-09-07 10:29:19 +0200976 ADD(alias, "f", id->format != IPACK_ANY_FORMAT, id->format);
Jens Taprogge849e0ad2012-09-04 17:01:13 +0200977 ADD(alias, "v", id->vendor != IPACK_ANY_ID, id->vendor);
978 ADD(alias, "d", id->device != IPACK_ANY_ID, id->device);
979 add_wildcard(alias);
980 return 1;
981}
982ADD_TO_DEVTABLE("ipack", struct ipack_device_id, do_ipack_entry);
983
Dave Martin523817b2011-10-05 14:44:57 +0100984/*
985 * Append a match expression for a single masked hex digit.
986 * outp points to a pointer to the character at which to append.
987 * *outp is updated on return to point just after the appended text,
988 * to facilitate further appending.
989 */
990static void append_nibble_mask(char **outp,
991 unsigned int nibble, unsigned int mask)
992{
993 char *p = *outp;
994 unsigned int i;
995
996 switch (mask) {
997 case 0:
998 *p++ = '?';
999 break;
1000
1001 case 0xf:
1002 p += sprintf(p, "%X", nibble);
1003 break;
1004
1005 default:
1006 /*
1007 * Dumbly emit a match pattern for all possible matching
1008 * digits. This could be improved in some cases using ranges,
1009 * but it has the advantage of being trivially correct, and is
1010 * often optimal.
1011 */
1012 *p++ = '[';
1013 for (i = 0; i < 0x10; i++)
1014 if ((i & mask) == nibble)
1015 p += sprintf(p, "%X", i);
1016 *p++ = ']';
1017 }
1018
1019 /* Ensure that the string remains NUL-terminated: */
1020 *p = '\0';
1021
1022 /* Advance the caller's end-of-string pointer: */
1023 *outp = p;
1024}
1025
1026/*
1027 * looks like: "amba:dN"
1028 *
1029 * N is exactly 8 digits, where each is an upper-case hex digit, or
1030 * a ? or [] pattern matching exactly one digit.
1031 */
1032static int do_amba_entry(const char *filename,
1033 struct amba_id *id, char *alias)
1034{
1035 unsigned int digit;
1036 char *p = alias;
1037
1038 if ((id->id & id->mask) != id->id)
1039 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1040 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
1041 filename, id->id, id->mask);
1042
1043 p += sprintf(alias, "amba:d");
1044 for (digit = 0; digit < 8; digit++)
1045 append_nibble_mask(&p,
1046 (id->id >> (4 * (7 - digit))) & 0xf,
1047 (id->mask >> (4 * (7 - digit))) & 0xf);
1048
1049 return 1;
1050}
Rusty Russelle49ce142012-01-13 09:32:16 +10301051ADD_TO_DEVTABLE("amba", struct amba_id, do_amba_entry);
Dave Martin523817b2011-10-05 14:44:57 +01001052
Andi Kleen644e9cb2012-01-26 00:09:05 +01001053/* LOOKS like x86cpu:vendor:VVVV:family:FFFF:model:MMMM:feature:*,FEAT,*
1054 * All fields are numbers. It would be nicer to use strings for vendor
1055 * and feature, but getting those out of the build system here is too
1056 * complicated.
1057 */
1058
1059static int do_x86cpu_entry(const char *filename, struct x86_cpu_id *id,
1060 char *alias)
1061{
1062 id->feature = TO_NATIVE(id->feature);
1063 id->family = TO_NATIVE(id->family);
1064 id->model = TO_NATIVE(id->model);
1065 id->vendor = TO_NATIVE(id->vendor);
1066
1067 strcpy(alias, "x86cpu:");
1068 ADD(alias, "vendor:", id->vendor != X86_VENDOR_ANY, id->vendor);
1069 ADD(alias, ":family:", id->family != X86_FAMILY_ANY, id->family);
1070 ADD(alias, ":model:", id->model != X86_MODEL_ANY, id->model);
Ben Hutchings5467bdd2012-02-11 22:57:19 +00001071 strcat(alias, ":feature:*");
1072 if (id->feature != X86_FEATURE_ANY)
1073 sprintf(alias + strlen(alias), "%04X*", id->feature);
Andi Kleen644e9cb2012-01-26 00:09:05 +01001074 return 1;
1075}
1076ADD_TO_DEVTABLE("x86cpu", struct x86_cpu_id, do_x86cpu_entry);
1077
Rusty Russell626596e2012-01-13 09:32:15 +10301078/* Does namelen bytes of name exactly match the symbol? */
1079static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Rusty Russell626596e2012-01-13 09:32:15 +10301081 if (namelen != strlen(symbol))
1082 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Rusty Russell626596e2012-01-13 09:32:15 +10301084 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085}
1086
1087static void do_table(void *symval, unsigned long size,
1088 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001089 const char *device_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 void *function,
1091 struct module *mod)
1092{
1093 unsigned int i;
1094 char alias[500];
1095 int (*do_entry)(const char *, void *entry, char *alias) = function;
1096
Kees Cooke0049822007-09-16 11:15:46 +02001097 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /* Leave last one: it's the terminator. */
1099 size -= id_size;
1100
1101 for (i = 0; i < size; i += id_size) {
1102 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 buf_printf(&mod->dev_table_buf,
1104 "MODULE_ALIAS(\"%s\");\n", alias);
1105 }
1106 }
1107}
1108
1109/* Create MODULE_ALIAS() statements.
1110 * At this time, we cannot write the actual output C source yet,
1111 * so we write into the mod->dev_table_buf buffer. */
1112void handle_moddevtable(struct module *mod, struct elf_info *info,
1113 Elf_Sym *sym, const char *symname)
1114{
1115 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001116 char *zeros = NULL;
Rusty Russell626596e2012-01-13 09:32:15 +10301117 const char *name;
1118 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001121 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 return;
1123
David Millere88aa7b2012-04-12 14:37:30 -04001124 /* We're looking for an object */
1125 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1126 return;
1127
Rusty Russell626596e2012-01-13 09:32:15 +10301128 /* All our symbols are of form <prefix>__mod_XXX_device_table. */
1129 name = strstr(symname, "__mod_");
1130 if (!name)
1131 return;
1132 name += strlen("__mod_");
1133 namelen = strlen(name);
1134 if (namelen < strlen("_device_table"))
1135 return;
1136 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1137 return;
1138 namelen -= strlen("_device_table");
1139
Kees Cooke0049822007-09-16 11:15:46 +02001140 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001141 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001142 zeros = calloc(1, sym->st_size);
1143 symval = zeros;
1144 } else {
1145 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001146 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001147 + sym->st_value;
1148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Rusty Russell626596e2012-01-13 09:32:15 +10301150 /* First handle the "special" cases */
1151 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001152 do_usb_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301153 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001154 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301155 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001156 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301157 else {
Rusty Russelle49ce142012-01-13 09:32:16 +10301158 struct devtable **p;
Andreas Bießmanndd2a3ac2012-02-24 08:23:53 +01001159 INIT_SECTION(__devtable);
Rusty Russell626596e2012-01-13 09:32:15 +10301160
Rusty Russelle49ce142012-01-13 09:32:16 +10301161 for (p = __start___devtable; p < __stop___devtable; p++) {
1162 if (sym_is(name, namelen, (*p)->device_id)) {
1163 do_table(symval, sym->st_size, (*p)->id_size,
1164 (*p)->device_id, (*p)->function, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301165 break;
1166 }
1167 }
1168 }
Kees Cooke0049822007-09-16 11:15:46 +02001169 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172/* Now add out buffered information to the generated C source */
1173void add_moddevtable(struct buffer *buf, struct module *mod)
1174{
1175 buf_printf(buf, "\n");
1176 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1177 free(mod->dev_table_buf.p);
1178}