blob: d0de2a2c3a2de00cacf74ba796f5b02fa5b56a5d [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
49/* We construct a table of pointers in an ELF section (pointers generally
50 * go unpadded by gcc). ld creates boundary syms for us. */
51extern struct devtable *__start___devtable[], *__stop___devtable[];
52#define ___cat(a,b) a ## b
53#define __cat(a,b) ___cat(a,b)
54
55#if __GNUC__ == 3 && __GNUC_MINOR__ < 3
56# define __used __attribute__((__unused__))
57#else
58# define __used __attribute__((__used__))
59#endif
60
61/* Add a table entry. We test function type matches while we're here. */
62#define ADD_TO_DEVTABLE(device_id, type, function) \
63 static struct devtable __cat(devtable,__LINE__) = { \
64 device_id + 0*sizeof((function)((const char *)NULL, \
65 (type *)NULL, \
66 (char *)NULL)), \
67 sizeof(type), (function) }; \
68 static struct devtable *__attribute__((section("__devtable"))) \
69 __used __cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define ADD(str, sep, cond, field) \
72do { \
73 strcat(str, sep); \
74 if (cond) \
75 sprintf(str + strlen(str), \
76 sizeof(field) == 1 ? "%02X" : \
77 sizeof(field) == 2 ? "%04X" : \
78 sizeof(field) == 4 ? "%08X" : "", \
79 field); \
80 else \
81 sprintf(str + strlen(str), "*"); \
82} while(0)
83
Jean Delvareac551822008-05-02 20:37:21 +020084/* Always end in a wildcard, for future extension */
85static inline void add_wildcard(char *str)
86{
87 int len = strlen(str);
88
89 if (str[len - 1] != '*')
90 strcat(str + len, "*");
91}
92
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +010093unsigned int cross_build = 0;
Sam Ravnborgfb33d812006-07-09 16:26:07 +020094/**
95 * Check that sizeof(device_id type) are consistent with size of section
96 * in .o file. If in-consistent then userspace and kernel does not agree
97 * on actual size which is a bug.
Kees Cooke0049822007-09-16 11:15:46 +020098 * Also verify that the final entry in the table is all zeros.
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +010099 * Ignore both checks if build host differ from target host and size differs.
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200100 **/
Kees Cooke0049822007-09-16 11:15:46 +0200101static void device_id_check(const char *modname, const char *device_id,
102 unsigned long size, unsigned long id_size,
103 void *symval)
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200104{
Kees Cooke0049822007-09-16 11:15:46 +0200105 int i;
106
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200107 if (size % id_size || size < id_size) {
Sam Ravnborg4ce6efe2008-03-23 21:38:54 +0100108 if (cross_build != 0)
109 return;
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200110 fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
111 "of the size of section __mod_%s_device_table=%lu.\n"
112 "Fix definition of struct %s_device_id "
113 "in mod_devicetable.h\n",
114 modname, device_id, id_size, device_id, size, device_id);
115 }
Kees Cooke0049822007-09-16 11:15:46 +0200116 /* Verify last one is a terminator */
117 for (i = 0; i < id_size; i++ ) {
118 if (*(uint8_t*)(symval+size-id_size+i)) {
119 fprintf(stderr,"%s: struct %s_device_id is %lu bytes. "
120 "The last of %lu is:\n",
121 modname, device_id, id_size, size / id_size);
122 for (i = 0; i < id_size; i++ )
123 fprintf(stderr,"0x%02x ",
124 *(uint8_t*)(symval+size-id_size+i) );
125 fprintf(stderr,"\n");
126 fatal("%s: struct %s_device_id is not terminated "
127 "with a NULL entry!\n", modname, device_id);
128 }
129 }
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200130}
131
Roman Kaganb19dcd92005-04-22 15:07:01 -0700132/* USB is special because the bcdDevice can be matched against a numeric range */
133/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
134static void do_usb_entry(struct usb_device_id *id,
135 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
136 unsigned char range_lo, unsigned char range_hi,
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500137 unsigned char max, struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Roman Kaganb19dcd92005-04-22 15:07:01 -0700139 char alias[500];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 strcpy(alias, "usb:");
141 ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
142 id->idVendor);
143 ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
144 id->idProduct);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700145
146 strcat(alias, "d");
147 if (bcdDevice_initial_digits)
148 sprintf(alias + strlen(alias), "%0*X",
149 bcdDevice_initial_digits, bcdDevice_initial);
150 if (range_lo == range_hi)
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500151 sprintf(alias + strlen(alias), "%X", range_lo);
152 else if (range_lo > 0 || range_hi < max) {
153 if (range_lo > 0x9 || range_hi < 0xA)
154 sprintf(alias + strlen(alias),
155 "[%X-%X]",
156 range_lo,
157 range_hi);
158 else {
159 sprintf(alias + strlen(alias),
160 range_lo < 0x9 ? "[%X-9" : "[%X",
161 range_lo);
162 sprintf(alias + strlen(alias),
163 range_hi > 0xA ? "a-%X]" : "%X]",
164 range_lo);
165 }
166 }
Roman Kaganb19dcd92005-04-22 15:07:01 -0700167 if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
168 strcat(alias, "*");
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
171 id->bDeviceClass);
172 ADD(alias, "dsc",
173 id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
174 id->bDeviceSubClass);
175 ADD(alias, "dp",
176 id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
177 id->bDeviceProtocol);
178 ADD(alias, "ic",
179 id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
180 id->bInterfaceClass);
181 ADD(alias, "isc",
182 id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
183 id->bInterfaceSubClass);
184 ADD(alias, "ip",
185 id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
186 id->bInterfaceProtocol);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700187
Jean Delvareac551822008-05-02 20:37:21 +0200188 add_wildcard(alias);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700189 buf_printf(&mod->dev_table_buf,
190 "MODULE_ALIAS(\"%s\");\n", alias);
191}
192
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500193/* Handles increment/decrement of BCD formatted integers */
194/* Returns the previous value, so it works like i++ or i-- */
195static unsigned int incbcd(unsigned int *bcd,
196 int inc,
197 unsigned char max,
198 size_t chars)
199{
200 unsigned int init = *bcd, i, j;
201 unsigned long long c, dec = 0;
202
203 /* If bcd is not in BCD format, just increment */
204 if (max > 0x9) {
205 *bcd += inc;
206 return init;
207 }
208
209 /* Convert BCD to Decimal */
210 for (i=0 ; i < chars ; i++) {
211 c = (*bcd >> (i << 2)) & 0xf;
212 c = c > 9 ? 9 : c; /* force to bcd just in case */
213 for (j=0 ; j < i ; j++)
214 c = c * 10;
215 dec += c;
216 }
217
218 /* Do our increment/decrement */
219 dec += inc;
220 *bcd = 0;
221
222 /* Convert back to BCD */
223 for (i=0 ; i < chars ; i++) {
224 for (c=1,j=0 ; j < i ; j++)
225 c = c * 10;
226 c = (dec / c) % 10;
227 *bcd += c << (i << 2);
228 }
229 return init;
230}
231
Roman Kaganb19dcd92005-04-22 15:07:01 -0700232static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
233{
234 unsigned int devlo, devhi;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500235 unsigned char chi, clo, max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700236 int ndigits;
237
238 id->match_flags = TO_NATIVE(id->match_flags);
239 id->idVendor = TO_NATIVE(id->idVendor);
240 id->idProduct = TO_NATIVE(id->idProduct);
241
242 devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
243 TO_NATIVE(id->bcdDevice_lo) : 0x0U;
244 devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
245 TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
246
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500247 /* Figure out if this entry is in bcd or hex format */
248 max = 0x9; /* Default to decimal format */
249 for (ndigits = 0 ; ndigits < sizeof(id->bcdDevice_lo) * 2 ; ndigits++) {
250 clo = (devlo >> (ndigits << 2)) & 0xf;
251 chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
252 if (clo > max || chi > max) {
253 max = 0xf;
254 break;
255 }
256 }
257
Roman Kaganb19dcd92005-04-22 15:07:01 -0700258 /*
259 * Some modules (visor) have empty slots as placeholder for
260 * run-time specification that results in catch-all alias
261 */
Greg Kroah-Hartmande6f92b2008-01-28 09:50:12 -0800262 if (!(id->idVendor | id->idProduct | id->bDeviceClass | id->bInterfaceClass))
Roman Kaganb19dcd92005-04-22 15:07:01 -0700263 return;
264
265 /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
266 for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
267 clo = devlo & 0xf;
268 chi = devhi & 0xf;
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500269 if (chi > max) /* If we are in bcd mode, truncate if necessary */
270 chi = max;
Roman Kaganb19dcd92005-04-22 15:07:01 -0700271 devlo >>= 4;
272 devhi >>= 4;
273
274 if (devlo == devhi || !ndigits) {
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500275 do_usb_entry(id, devlo, ndigits, clo, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700276 break;
277 }
278
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500279 if (clo > 0x0)
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500280 do_usb_entry(id,
281 incbcd(&devlo, 1, max,
282 sizeof(id->bcdDevice_lo) * 2),
283 ndigits, clo, max, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700284
Nathaniel McCallumafe2dab2009-11-18 20:11:23 -0500285 if (chi < max)
Nathaniel McCallum55f49f22009-11-18 20:15:28 -0500286 do_usb_entry(id,
287 incbcd(&devhi, -1, max,
288 sizeof(id->bcdDevice_lo) * 2),
289 ndigits, 0x0, chi, max, mod);
Roman Kaganb19dcd92005-04-22 15:07:01 -0700290 }
291}
292
293static void do_usb_table(void *symval, unsigned long size,
294 struct module *mod)
295{
296 unsigned int i;
297 const unsigned long id_size = sizeof(struct usb_device_id);
298
Kees Cooke0049822007-09-16 11:15:46 +0200299 device_id_check(mod->name, "usb", size, id_size, symval);
Sam Ravnborgfb33d812006-07-09 16:26:07 +0200300
Roman Kaganb19dcd92005-04-22 15:07:01 -0700301 /* Leave last one: it's the terminator. */
302 size -= id_size;
303
304 for (i = 0; i < size; i += id_size)
305 do_usb_entry_multi(symval + i, mod);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Jiri Slabye8c84f92008-05-19 15:50:01 +0200308/* Looks like: hid:bNvNpN */
309static int do_hid_entry(const char *filename,
310 struct hid_device_id *id, char *alias)
311{
Jiri Slaby2b639382009-02-17 12:38:36 +0100312 id->bus = TO_NATIVE(id->bus);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200313 id->vendor = TO_NATIVE(id->vendor);
314 id->product = TO_NATIVE(id->product);
315
316 sprintf(alias, "hid:b%04X", id->bus);
317 ADD(alias, "v", id->vendor != HID_ANY_ID, id->vendor);
318 ADD(alias, "p", id->product != HID_ANY_ID, id->product);
319
320 return 1;
321}
Rusty Russelle49ce142012-01-13 09:32:16 +1030322ADD_TO_DEVTABLE("hid", struct hid_device_id, do_hid_entry);
Jiri Slabye8c84f92008-05-19 15:50:01 +0200323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/* Looks like: ieee1394:venNmoNspNverN */
325static int do_ieee1394_entry(const char *filename,
326 struct ieee1394_device_id *id, char *alias)
327{
328 id->match_flags = TO_NATIVE(id->match_flags);
329 id->vendor_id = TO_NATIVE(id->vendor_id);
330 id->model_id = TO_NATIVE(id->model_id);
331 id->specifier_id = TO_NATIVE(id->specifier_id);
332 id->version = TO_NATIVE(id->version);
333
334 strcpy(alias, "ieee1394:");
335 ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
336 id->vendor_id);
337 ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
338 id->model_id);
339 ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
340 id->specifier_id);
341 ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
342 id->version);
343
Jean Delvareac551822008-05-02 20:37:21 +0200344 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return 1;
346}
Rusty Russelle49ce142012-01-13 09:32:16 +1030347ADD_TO_DEVTABLE("ieee1394", struct ieee1394_device_id, do_ieee1394_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
350static int do_pci_entry(const char *filename,
351 struct pci_device_id *id, char *alias)
352{
353 /* Class field can be divided into these three. */
354 unsigned char baseclass, subclass, interface,
355 baseclass_mask, subclass_mask, interface_mask;
356
357 id->vendor = TO_NATIVE(id->vendor);
358 id->device = TO_NATIVE(id->device);
359 id->subvendor = TO_NATIVE(id->subvendor);
360 id->subdevice = TO_NATIVE(id->subdevice);
361 id->class = TO_NATIVE(id->class);
362 id->class_mask = TO_NATIVE(id->class_mask);
363
364 strcpy(alias, "pci:");
365 ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
366 ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
367 ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
368 ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
369
370 baseclass = (id->class) >> 16;
371 baseclass_mask = (id->class_mask) >> 16;
372 subclass = (id->class) >> 8;
373 subclass_mask = (id->class_mask) >> 8;
374 interface = id->class;
375 interface_mask = id->class_mask;
376
377 if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
378 || (subclass_mask != 0 && subclass_mask != 0xFF)
379 || (interface_mask != 0 && interface_mask != 0xFF)) {
Sam Ravnborgcb805142006-01-28 16:57:26 +0100380 warn("Can't handle masks in %s:%04X\n",
381 filename, id->class_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return 0;
383 }
384
385 ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
386 ADD(alias, "sc", subclass_mask == 0xFF, subclass);
387 ADD(alias, "i", interface_mask == 0xFF, interface);
Jean Delvareac551822008-05-02 20:37:21 +0200388 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 1;
390}
Rusty Russelle49ce142012-01-13 09:32:16 +1030391ADD_TO_DEVTABLE("pci", struct pci_device_id, do_pci_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Sam Ravnborg62070fa2006-03-03 16:46:04 +0100393/* looks like: "ccw:tNmNdtNdmN" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394static int do_ccw_entry(const char *filename,
395 struct ccw_device_id *id, char *alias)
396{
397 id->match_flags = TO_NATIVE(id->match_flags);
398 id->cu_type = TO_NATIVE(id->cu_type);
399 id->cu_model = TO_NATIVE(id->cu_model);
400 id->dev_type = TO_NATIVE(id->dev_type);
401 id->dev_model = TO_NATIVE(id->dev_model);
402
403 strcpy(alias, "ccw:");
404 ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
405 id->cu_type);
406 ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
407 id->cu_model);
408 ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
409 id->dev_type);
Bastian Blankde1d9c02006-03-06 15:43:00 -0800410 ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 id->dev_model);
Jean Delvareac551822008-05-02 20:37:21 +0200412 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return 1;
414}
Rusty Russelle49ce142012-01-13 09:32:16 +1030415ADD_TO_DEVTABLE("ccw", struct ccw_device_id, do_ccw_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200417/* looks like: "ap:tN" */
418static int do_ap_entry(const char *filename,
419 struct ap_device_id *id, char *alias)
420{
Jean Delvareac551822008-05-02 20:37:21 +0200421 sprintf(alias, "ap:t%02X*", id->dev_type);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200422 return 1;
423}
Rusty Russelle49ce142012-01-13 09:32:16 +1030424ADD_TO_DEVTABLE("ap", struct ap_device_id, do_ap_entry);
Martin Schwidefsky1534c382006-09-20 15:58:25 +0200425
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200426/* looks like: "css:tN" */
427static int do_css_entry(const char *filename,
428 struct css_device_id *id, char *alias)
429{
430 sprintf(alias, "css:t%01X", id->type);
431 return 1;
432}
Rusty Russelle49ce142012-01-13 09:32:16 +1030433ADD_TO_DEVTABLE("css", struct css_device_id, do_css_entry);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/* Looks like: "serio:tyNprNidNexN" */
436static int do_serio_entry(const char *filename,
437 struct serio_device_id *id, char *alias)
438{
439 id->type = TO_NATIVE(id->type);
440 id->proto = TO_NATIVE(id->proto);
441 id->id = TO_NATIVE(id->id);
442 id->extra = TO_NATIVE(id->extra);
443
444 strcpy(alias, "serio:");
445 ADD(alias, "ty", id->type != SERIO_ANY, id->type);
446 ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
447 ADD(alias, "id", id->id != SERIO_ANY, id->id);
448 ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
449
Jean Delvareac551822008-05-02 20:37:21 +0200450 add_wildcard(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return 1;
452}
Rusty Russelle49ce142012-01-13 09:32:16 +1030453ADD_TO_DEVTABLE("serio", struct serio_device_id, do_serio_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Thomas Renninger29b71a12007-07-23 14:43:51 +0200455/* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
456static int do_acpi_entry(const char *filename,
457 struct acpi_device_id *id, char *alias)
458{
Jean Delvareac551822008-05-02 20:37:21 +0200459 sprintf(alias, "acpi*:%s:*", id->id);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200460 return 1;
461}
Rusty Russelle49ce142012-01-13 09:32:16 +1030462ADD_TO_DEVTABLE("acpi", struct acpi_device_id, do_acpi_entry);
Thomas Renninger29b71a12007-07-23 14:43:51 +0200463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/* looks like: "pnp:dD" */
Kay Sievers22454cb2008-05-28 23:06:47 +0200465static void do_pnp_device_entry(void *symval, unsigned long size,
466 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Kay Sievers22454cb2008-05-28 23:06:47 +0200468 const unsigned long id_size = sizeof(struct pnp_device_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200469 const unsigned int count = (size / id_size)-1;
470 const struct pnp_device_id *devs = symval;
471 unsigned int i;
Kay Sievers22454cb2008-05-28 23:06:47 +0200472
473 device_id_check(mod->name, "pnp", size, id_size, symval);
474
Kay Sievers5e4c6562008-08-21 15:28:56 +0200475 for (i = 0; i < count; i++) {
476 const char *id = (char *)devs[i].id;
Kay Sievers72638f52009-01-08 03:06:42 +0100477 char acpi_id[sizeof(devs[0].id)];
478 int j;
Kay Sievers5e4c6562008-08-21 15:28:56 +0200479
480 buf_printf(&mod->dev_table_buf,
481 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100482
483 /* fix broken pnp bus lowercasing */
484 for (j = 0; j < sizeof(acpi_id); j++)
485 acpi_id[j] = toupper(id[j]);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200486 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100487 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers5e4c6562008-08-21 15:28:56 +0200488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Kay Sievers0c81eed2008-02-21 00:35:54 +0100491/* looks like: "pnp:dD" for every device of the card */
492static void do_pnp_card_entries(void *symval, unsigned long size,
493 struct module *mod)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Kay Sievers0c81eed2008-02-21 00:35:54 +0100495 const unsigned long id_size = sizeof(struct pnp_card_device_id);
496 const unsigned int count = (size / id_size)-1;
497 const struct pnp_card_device_id *cards = symval;
498 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Kay Sievers0c81eed2008-02-21 00:35:54 +0100500 device_id_check(mod->name, "pnp", size, id_size, symval);
501
502 for (i = 0; i < count; i++) {
503 unsigned int j;
504 const struct pnp_card_device_id *card = &cards[i];
505
506 for (j = 0; j < PNP_MAX_DEVICES; j++) {
507 const char *id = (char *)card->devs[j].id;
508 int i2, j2;
509 int dup = 0;
510
511 if (!id[0])
512 break;
513
514 /* find duplicate, already added value */
515 for (i2 = 0; i2 < i && !dup; i2++) {
516 const struct pnp_card_device_id *card2 = &cards[i2];
517
518 for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
519 const char *id2 = (char *)card2->devs[j2].id;
520
521 if (!id2[0])
522 break;
523
524 if (!strcmp(id, id2)) {
525 dup = 1;
526 break;
527 }
528 }
529 }
530
531 /* add an individual alias for every device entry */
Kay Sievers22454cb2008-05-28 23:06:47 +0200532 if (!dup) {
Kay Sievers72638f52009-01-08 03:06:42 +0100533 char acpi_id[sizeof(card->devs[0].id)];
534 int k;
535
Kay Sievers0c81eed2008-02-21 00:35:54 +0100536 buf_printf(&mod->dev_table_buf,
537 "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
Kay Sievers72638f52009-01-08 03:06:42 +0100538
539 /* fix broken pnp bus lowercasing */
540 for (k = 0; k < sizeof(acpi_id); k++)
541 acpi_id[k] = toupper(id[k]);
Kay Sievers22454cb2008-05-28 23:06:47 +0200542 buf_printf(&mod->dev_table_buf,
Kay Sievers72638f52009-01-08 03:06:42 +0100543 "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
Kay Sievers22454cb2008-05-28 23:06:47 +0200544 }
Kay Sievers0c81eed2008-02-21 00:35:54 +0100545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700549/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
550static int do_pcmcia_entry(const char *filename,
551 struct pcmcia_device_id *id, char *alias)
552{
553 unsigned int i;
554
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200555 id->match_flags = TO_NATIVE(id->match_flags);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700556 id->manf_id = TO_NATIVE(id->manf_id);
557 id->card_id = TO_NATIVE(id->card_id);
558 id->func_id = TO_NATIVE(id->func_id);
559 id->function = TO_NATIVE(id->function);
560 id->device_no = TO_NATIVE(id->device_no);
Kars de Jong4fb7edc2005-09-25 14:39:46 +0200561
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700562 for (i=0; i<4; i++) {
563 id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
564 }
565
566 strcpy(alias, "pcmcia:");
567 ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
568 id->manf_id);
569 ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
570 id->card_id);
571 ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
572 id->func_id);
573 ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
574 id->function);
575 ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
576 id->device_no);
577 ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
578 ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
579 ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
580 ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
581
Jean Delvareac551822008-05-02 20:37:21 +0200582 add_wildcard(alias);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700583 return 1;
584}
Rusty Russelle49ce142012-01-13 09:32:16 +1030585ADD_TO_DEVTABLE("pcmcia", struct pcmcia_device_id, do_pcmcia_entry);
Dominik Brodowski90829cf2005-06-27 16:28:12 -0700586
Jeff Mahoney5e655772005-07-06 15:44:41 -0400587static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
588{
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000589 int len;
Jeff Mahoney5e655772005-07-06 15:44:41 -0400590 char *tmp;
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000591 len = sprintf (alias, "of:N%sT%s",
Jeff Mahoney5e655772005-07-06 15:44:41 -0400592 of->name[0] ? of->name : "*",
Sylvain Munautd1ab4232007-05-08 19:59:29 +1000593 of->type[0] ? of->type : "*");
594
595 if (of->compatible[0])
596 sprintf (&alias[len], "%sC%s",
597 of->type[0] ? "*" : "",
598 of->compatible);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400599
600 /* Replace all whitespace with underscores */
601 for (tmp = alias; tmp && *tmp; tmp++)
602 if (isspace (*tmp))
603 *tmp = '_';
604
Jean Delvareac551822008-05-02 20:37:21 +0200605 add_wildcard(alias);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400606 return 1;
607}
Rusty Russelle49ce142012-01-13 09:32:16 +1030608ADD_TO_DEVTABLE("of", struct of_device_id, do_of_entry);
Jeff Mahoney5e655772005-07-06 15:44:41 -0400609
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000610static int do_vio_entry(const char *filename, struct vio_device_id *vio,
611 char *alias)
612{
613 char *tmp;
614
615 sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
616 vio->compat[0] ? vio->compat : "*");
617
618 /* Replace all whitespace with underscores */
619 for (tmp = alias; tmp && *tmp; tmp++)
620 if (isspace (*tmp))
621 *tmp = '_';
622
Jean Delvareac551822008-05-02 20:37:21 +0200623 add_wildcard(alias);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000624 return 1;
625}
Rusty Russelle49ce142012-01-13 09:32:16 +1030626ADD_TO_DEVTABLE("vio", struct vio_device_id, do_vio_entry);
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000627
Rusty Russell1d8f4302005-12-07 21:40:34 +0100628#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
629
630static void do_input(char *alias,
631 kernel_ulong_t *arr, unsigned int min, unsigned int max)
632{
633 unsigned int i;
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400634
635 for (i = min; i < max; i++)
Hans de Goedee0e92632006-08-15 12:09:27 +0200636 if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400637 sprintf(alias + strlen(alias), "%X,*", i);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100638}
639
640/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
641static int do_input_entry(const char *filename, struct input_device_id *id,
642 char *alias)
643{
644 sprintf(alias, "input:");
645
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400646 ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
647 ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
648 ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
649 ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100650
651 sprintf(alias + strlen(alias), "-e*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400652 if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100653 do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100654 sprintf(alias + strlen(alias), "k*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400655 if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100656 do_input(alias, id->keybit,
657 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
658 INPUT_DEVICE_ID_KEY_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100659 sprintf(alias + strlen(alias), "r*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400660 if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100661 do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100662 sprintf(alias + strlen(alias), "a*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400663 if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100664 do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100665 sprintf(alias + strlen(alias), "m*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400666 if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100667 do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100668 sprintf(alias + strlen(alias), "l*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400669 if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100670 do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100671 sprintf(alias + strlen(alias), "s*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400672 if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100673 do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100674 sprintf(alias + strlen(alias), "f*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400675 if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100676 do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100677 sprintf(alias + strlen(alias), "w*");
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400678 if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
Sam Ravnborgdc24f0e2007-03-09 19:59:06 +0100679 do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100680 return 1;
681}
Rusty Russelle49ce142012-01-13 09:32:16 +1030682ADD_TO_DEVTABLE("input", struct input_device_id, do_input_entry);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100683
Michael Tokarev07563c72006-09-27 01:50:56 -0700684static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
685 char *alias)
686{
687 if (eisa->sig[0])
688 sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
Jean Delvareac551822008-05-02 20:37:21 +0200689 else
690 strcat(alias, "*");
Michael Tokarev07563c72006-09-27 01:50:56 -0700691 return 1;
692}
Rusty Russelle49ce142012-01-13 09:32:16 +1030693ADD_TO_DEVTABLE("eisa", struct eisa_device_id, do_eisa_entry);
Michael Tokarev07563c72006-09-27 01:50:56 -0700694
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500695/* Looks like: parisc:tNhvNrevNsvN */
696static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
697 char *alias)
698{
699 id->hw_type = TO_NATIVE(id->hw_type);
700 id->hversion = TO_NATIVE(id->hversion);
701 id->hversion_rev = TO_NATIVE(id->hversion_rev);
702 id->sversion = TO_NATIVE(id->sversion);
703
704 strcpy(alias, "parisc:");
Kyle McMartinf354ef82007-01-13 15:02:09 -0500705 ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type);
706 ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion);
707 ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev);
708 ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500709
Jean Delvareac551822008-05-02 20:37:21 +0200710 add_wildcard(alias);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500711 return 1;
712}
Rusty Russelle49ce142012-01-13 09:32:16 +1030713ADD_TO_DEVTABLE("parisc", struct parisc_device_id, do_parisc_entry);
Kyle McMartinf3cf2672007-01-13 14:58:21 -0500714
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200715/* Looks like: sdio:cNvNdN. */
716static int do_sdio_entry(const char *filename,
717 struct sdio_device_id *id, char *alias)
718{
719 id->class = TO_NATIVE(id->class);
720 id->vendor = TO_NATIVE(id->vendor);
721 id->device = TO_NATIVE(id->device);
722
723 strcpy(alias, "sdio:");
724 ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class);
725 ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor);
726 ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device);
Jean Delvareac551822008-05-02 20:37:21 +0200727 add_wildcard(alias);
Linus Torvalds038a5002007-10-11 19:40:14 -0700728 return 1;
729}
Rusty Russelle49ce142012-01-13 09:32:16 +1030730ADD_TO_DEVTABLE("sdio", struct sdio_device_id, do_sdio_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200731
Michael Buesch61e115a2007-09-18 15:12:50 -0400732/* Looks like: ssb:vNidNrevN. */
733static int do_ssb_entry(const char *filename,
734 struct ssb_device_id *id, char *alias)
735{
736 id->vendor = TO_NATIVE(id->vendor);
737 id->coreid = TO_NATIVE(id->coreid);
738 id->revision = TO_NATIVE(id->revision);
739
740 strcpy(alias, "ssb:");
741 ADD(alias, "v", id->vendor != SSB_ANY_VENDOR, id->vendor);
742 ADD(alias, "id", id->coreid != SSB_ANY_ID, id->coreid);
743 ADD(alias, "rev", id->revision != SSB_ANY_REV, id->revision);
Jean Delvareac551822008-05-02 20:37:21 +0200744 add_wildcard(alias);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200745 return 1;
746}
Rusty Russelle49ce142012-01-13 09:32:16 +1030747ADD_TO_DEVTABLE("ssb", struct ssb_device_id, do_ssb_entry);
Pierre Ossmand59b66c2007-06-17 11:34:23 +0200748
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200749/* Looks like: bcma:mNidNrevNclN. */
750static int do_bcma_entry(const char *filename,
751 struct bcma_device_id *id, char *alias)
752{
753 id->manuf = TO_NATIVE(id->manuf);
754 id->id = TO_NATIVE(id->id);
755 id->rev = TO_NATIVE(id->rev);
756 id->class = TO_NATIVE(id->class);
757
758 strcpy(alias, "bcma:");
759 ADD(alias, "m", id->manuf != BCMA_ANY_MANUF, id->manuf);
760 ADD(alias, "id", id->id != BCMA_ANY_ID, id->id);
761 ADD(alias, "rev", id->rev != BCMA_ANY_REV, id->rev);
762 ADD(alias, "cl", id->class != BCMA_ANY_CLASS, id->class);
763 add_wildcard(alias);
764 return 1;
765}
Rusty Russelle49ce142012-01-13 09:32:16 +1030766ADD_TO_DEVTABLE("bcma", struct bcma_device_id, do_bcma_entry);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200767
Rusty Russellb01d9f22007-10-22 11:03:39 +1000768/* Looks like: virtio:dNvN */
769static int do_virtio_entry(const char *filename, struct virtio_device_id *id,
770 char *alias)
771{
772 id->device = TO_NATIVE(id->device);
773 id->vendor = TO_NATIVE(id->vendor);
774
775 strcpy(alias, "virtio:");
Christian Borntraegere3353852009-05-26 15:46:10 +0200776 ADD(alias, "d", id->device != VIRTIO_DEV_ANY_ID, id->device);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000777 ADD(alias, "v", id->vendor != VIRTIO_DEV_ANY_ID, id->vendor);
778
Jean Delvareac551822008-05-02 20:37:21 +0200779 add_wildcard(alias);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000780 return 1;
781}
Rusty Russelle49ce142012-01-13 09:32:16 +1030782ADD_TO_DEVTABLE("virtio", struct virtio_device_id, do_virtio_entry);
Rusty Russellb01d9f22007-10-22 11:03:39 +1000783
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700784/*
785 * Looks like: vmbus:guid
786 * Each byte of the guid will be represented by two hex characters
787 * in the name.
788 */
789
790static int do_vmbus_entry(const char *filename, struct hv_vmbus_device_id *id,
791 char *alias)
792{
793 int i;
Greg Kroah-Hartmanebf16e32011-08-25 11:28:11 -0700794 char guid_name[((sizeof(id->guid) + 1)) * 2];
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700795
Greg Kroah-Hartmanebf16e32011-08-25 11:28:11 -0700796 for (i = 0; i < (sizeof(id->guid) * 2); i += 2)
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700797 sprintf(&guid_name[i], "%02x", id->guid[i/2]);
798
799 strcpy(alias, "vmbus:");
800 strcat(alias, guid_name);
801
802 return 1;
803}
Rusty Russelle49ce142012-01-13 09:32:16 +1030804ADD_TO_DEVTABLE("vmbus", struct hv_vmbus_device_id, do_vmbus_entry);
K. Y. Srinivasand2ee52a2011-08-25 09:48:30 -0700805
Jean Delvared2653e92008-04-29 23:11:39 +0200806/* Looks like: i2c:S */
807static int do_i2c_entry(const char *filename, struct i2c_device_id *id,
808 char *alias)
809{
810 sprintf(alias, I2C_MODULE_PREFIX "%s", id->name);
811
812 return 1;
813}
Rusty Russelle49ce142012-01-13 09:32:16 +1030814ADD_TO_DEVTABLE("i2c", struct i2c_device_id, do_i2c_entry);
Jean Delvared2653e92008-04-29 23:11:39 +0200815
Anton Vorontsove0626e32009-09-22 16:46:08 -0700816/* Looks like: spi:S */
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700817static int do_spi_entry(const char *filename, struct spi_device_id *id,
818 char *alias)
819{
Anton Vorontsove0626e32009-09-22 16:46:08 -0700820 sprintf(alias, SPI_MODULE_PREFIX "%s", id->name);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700821
822 return 1;
823}
Rusty Russelle49ce142012-01-13 09:32:16 +1030824ADD_TO_DEVTABLE("spi", struct spi_device_id, do_spi_entry);
Anton Vorontsov75368bf2009-09-22 16:46:04 -0700825
David Woodhoused945b692008-09-16 16:23:28 -0700826static const struct dmifield {
827 const char *prefix;
828 int field;
829} dmi_fields[] = {
830 { "bvn", DMI_BIOS_VENDOR },
831 { "bvr", DMI_BIOS_VERSION },
832 { "bd", DMI_BIOS_DATE },
833 { "svn", DMI_SYS_VENDOR },
834 { "pn", DMI_PRODUCT_NAME },
835 { "pvr", DMI_PRODUCT_VERSION },
836 { "rvn", DMI_BOARD_VENDOR },
837 { "rn", DMI_BOARD_NAME },
838 { "rvr", DMI_BOARD_VERSION },
839 { "cvn", DMI_CHASSIS_VENDOR },
840 { "ct", DMI_CHASSIS_TYPE },
841 { "cvr", DMI_CHASSIS_VERSION },
842 { NULL, DMI_NONE }
843};
844
845static void dmi_ascii_filter(char *d, const char *s)
846{
847 /* Filter out characters we don't want to see in the modalias string */
848 for (; *s; s++)
849 if (*s > ' ' && *s < 127 && *s != ':')
850 *(d++) = *s;
851
852 *d = 0;
853}
854
855
856static int do_dmi_entry(const char *filename, struct dmi_system_id *id,
857 char *alias)
858{
859 int i, j;
860
861 sprintf(alias, "dmi*");
862
863 for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
864 for (j = 0; j < 4; j++) {
865 if (id->matches[j].slot &&
866 id->matches[j].slot == dmi_fields[i].field) {
867 sprintf(alias + strlen(alias), ":%s*",
868 dmi_fields[i].prefix);
869 dmi_ascii_filter(alias + strlen(alias),
870 id->matches[j].substr);
871 strcat(alias, "*");
872 }
873 }
874 }
875
876 strcat(alias, ":");
877 return 1;
878}
Rusty Russelle49ce142012-01-13 09:32:16 +1030879ADD_TO_DEVTABLE("dmi", struct dmi_system_id, do_dmi_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +0800880
881static int do_platform_entry(const char *filename,
882 struct platform_device_id *id, char *alias)
883{
884 sprintf(alias, PLATFORM_MODULE_PREFIX "%s", id->name);
885 return 1;
886}
Rusty Russelle49ce142012-01-13 09:32:16 +1030887ADD_TO_DEVTABLE("platform", struct platform_device_id, do_platform_entry);
Eric Miao57fee4a2009-02-04 11:52:40 +0800888
David Woodhouse8626d3b2010-04-02 01:05:27 +0000889static int do_mdio_entry(const char *filename,
890 struct mdio_device_id *id, char *alias)
891{
892 int i;
893
894 alias += sprintf(alias, MDIO_MODULE_PREFIX);
895
896 for (i = 0; i < 32; i++) {
897 if (!((id->phy_id_mask >> (31-i)) & 1))
898 *(alias++) = '?';
899 else if ((id->phy_id >> (31-i)) & 1)
900 *(alias++) = '1';
901 else
902 *(alias++) = '0';
903 }
904
905 /* Terminate the string */
906 *alias = 0;
907
908 return 1;
909}
Rusty Russelle49ce142012-01-13 09:32:16 +1030910ADD_TO_DEVTABLE("mdio", struct mdio_device_id, do_mdio_entry);
David Woodhouse8626d3b2010-04-02 01:05:27 +0000911
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100912/* Looks like: zorro:iN. */
913static int do_zorro_entry(const char *filename, struct zorro_device_id *id,
914 char *alias)
915{
916 id->id = TO_NATIVE(id->id);
917 strcpy(alias, "zorro:");
918 ADD(alias, "i", id->id != ZORRO_WILDCARD, id->id);
919 return 1;
920}
Rusty Russelle49ce142012-01-13 09:32:16 +1030921ADD_TO_DEVTABLE("zorro", struct zorro_device_id, do_zorro_entry);
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100922
Ondrej Zaryfedb3d22009-12-18 20:52:39 +0100923/* looks like: "pnp:dD" */
924static int do_isapnp_entry(const char *filename,
925 struct isapnp_device_id *id, char *alias)
926{
927 sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
928 'A' + ((id->vendor >> 2) & 0x3f) - 1,
929 'A' + (((id->vendor & 3) << 3) | ((id->vendor >> 13) & 7)) - 1,
930 'A' + ((id->vendor >> 8) & 0x1f) - 1,
931 (id->function >> 4) & 0x0f, id->function & 0x0f,
932 (id->function >> 12) & 0x0f, (id->function >> 8) & 0x0f);
933 return 1;
934}
Ondrej Zary0d86f652012-02-10 20:12:27 +0100935ADD_TO_DEVTABLE("isapnp", struct isapnp_device_id, do_isapnp_entry);
Ondrej Zaryfedb3d22009-12-18 20:52:39 +0100936
Dave Martin523817b2011-10-05 14:44:57 +0100937/*
938 * Append a match expression for a single masked hex digit.
939 * outp points to a pointer to the character at which to append.
940 * *outp is updated on return to point just after the appended text,
941 * to facilitate further appending.
942 */
943static void append_nibble_mask(char **outp,
944 unsigned int nibble, unsigned int mask)
945{
946 char *p = *outp;
947 unsigned int i;
948
949 switch (mask) {
950 case 0:
951 *p++ = '?';
952 break;
953
954 case 0xf:
955 p += sprintf(p, "%X", nibble);
956 break;
957
958 default:
959 /*
960 * Dumbly emit a match pattern for all possible matching
961 * digits. This could be improved in some cases using ranges,
962 * but it has the advantage of being trivially correct, and is
963 * often optimal.
964 */
965 *p++ = '[';
966 for (i = 0; i < 0x10; i++)
967 if ((i & mask) == nibble)
968 p += sprintf(p, "%X", i);
969 *p++ = ']';
970 }
971
972 /* Ensure that the string remains NUL-terminated: */
973 *p = '\0';
974
975 /* Advance the caller's end-of-string pointer: */
976 *outp = p;
977}
978
979/*
980 * looks like: "amba:dN"
981 *
982 * N is exactly 8 digits, where each is an upper-case hex digit, or
983 * a ? or [] pattern matching exactly one digit.
984 */
985static int do_amba_entry(const char *filename,
986 struct amba_id *id, char *alias)
987{
988 unsigned int digit;
989 char *p = alias;
990
991 if ((id->id & id->mask) != id->id)
992 fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
993 "id=0x%08X, mask=0x%08X. Please fix this driver.\n",
994 filename, id->id, id->mask);
995
996 p += sprintf(alias, "amba:d");
997 for (digit = 0; digit < 8; digit++)
998 append_nibble_mask(&p,
999 (id->id >> (4 * (7 - digit))) & 0xf,
1000 (id->mask >> (4 * (7 - digit))) & 0xf);
1001
1002 return 1;
1003}
Rusty Russelle49ce142012-01-13 09:32:16 +10301004ADD_TO_DEVTABLE("amba", struct amba_id, do_amba_entry);
Dave Martin523817b2011-10-05 14:44:57 +01001005
Rusty Russell626596e2012-01-13 09:32:15 +10301006/* Does namelen bytes of name exactly match the symbol? */
1007static bool sym_is(const char *name, unsigned namelen, const char *symbol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Rusty Russell626596e2012-01-13 09:32:15 +10301009 if (namelen != strlen(symbol))
1010 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Rusty Russell626596e2012-01-13 09:32:15 +10301012 return memcmp(name, symbol, namelen) == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015static void do_table(void *symval, unsigned long size,
1016 unsigned long id_size,
Sam Ravnborgfb33d812006-07-09 16:26:07 +02001017 const char *device_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 void *function,
1019 struct module *mod)
1020{
1021 unsigned int i;
1022 char alias[500];
1023 int (*do_entry)(const char *, void *entry, char *alias) = function;
1024
Kees Cooke0049822007-09-16 11:15:46 +02001025 device_id_check(mod->name, device_id, size, id_size, symval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /* Leave last one: it's the terminator. */
1027 size -= id_size;
1028
1029 for (i = 0; i < size; i += id_size) {
1030 if (do_entry(mod->name, symval+i, alias)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 buf_printf(&mod->dev_table_buf,
1032 "MODULE_ALIAS(\"%s\");\n", alias);
1033 }
1034 }
1035}
1036
1037/* Create MODULE_ALIAS() statements.
1038 * At this time, we cannot write the actual output C source yet,
1039 * so we write into the mod->dev_table_buf buffer. */
1040void handle_moddevtable(struct module *mod, struct elf_info *info,
1041 Elf_Sym *sym, const char *symname)
1042{
1043 void *symval;
Kees Cooke0049822007-09-16 11:15:46 +02001044 char *zeros = NULL;
Rusty Russell626596e2012-01-13 09:32:15 +10301045 const char *name;
1046 unsigned int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /* We're looking for a section relative symbol */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001049 if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return;
1051
Rusty Russell626596e2012-01-13 09:32:15 +10301052 /* All our symbols are of form <prefix>__mod_XXX_device_table. */
1053 name = strstr(symname, "__mod_");
1054 if (!name)
1055 return;
1056 name += strlen("__mod_");
1057 namelen = strlen(name);
1058 if (namelen < strlen("_device_table"))
1059 return;
1060 if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1061 return;
1062 namelen -= strlen("_device_table");
1063
Kees Cooke0049822007-09-16 11:15:46 +02001064 /* Handle all-NULL symbols allocated into .bss */
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001065 if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
Kees Cooke0049822007-09-16 11:15:46 +02001066 zeros = calloc(1, sym->st_size);
1067 symval = zeros;
1068 } else {
1069 symval = (void *)info->hdr
Denys Vlasenko1ce53ad2010-07-29 01:47:53 +02001070 + info->sechdrs[get_secindex(info, sym)].sh_offset
Kees Cooke0049822007-09-16 11:15:46 +02001071 + sym->st_value;
1072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Rusty Russell626596e2012-01-13 09:32:15 +10301074 /* First handle the "special" cases */
1075 if (sym_is(name, namelen, "usb"))
Roman Kaganb19dcd92005-04-22 15:07:01 -07001076 do_usb_table(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301077 else if (sym_is(name, namelen, "pnp"))
Kay Sievers22454cb2008-05-28 23:06:47 +02001078 do_pnp_device_entry(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301079 else if (sym_is(name, namelen, "pnp_card"))
Kay Sievers0c81eed2008-02-21 00:35:54 +01001080 do_pnp_card_entries(symval, sym->st_size, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301081 else {
Rusty Russelle49ce142012-01-13 09:32:16 +10301082 struct devtable **p;
Rusty Russell626596e2012-01-13 09:32:15 +10301083
Rusty Russelle49ce142012-01-13 09:32:16 +10301084 for (p = __start___devtable; p < __stop___devtable; p++) {
1085 if (sym_is(name, namelen, (*p)->device_id)) {
1086 do_table(symval, sym->st_size, (*p)->id_size,
1087 (*p)->device_id, (*p)->function, mod);
Rusty Russell626596e2012-01-13 09:32:15 +10301088 break;
1089 }
1090 }
1091 }
Kees Cooke0049822007-09-16 11:15:46 +02001092 free(zeros);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095/* Now add out buffered information to the generated C source */
1096void add_moddevtable(struct buffer *buf, struct module *mod)
1097{
1098 buf_printf(buf, "\n");
1099 buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1100 free(mod->dev_table_buf.p);
1101}