xref: /linux/scripts/mod/file2alias.c (revision 07563c711fbc25389e58ab9c9f0b9de2fce56760)
11da177e4SLinus Torvalds /* Simple code to turn various tables in an ELF file into alias definitions.
21da177e4SLinus Torvalds  * This deals with kernel datastructures where they should be
31da177e4SLinus Torvalds  * dealt with: in the kernel source.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright 2002-2003  Rusty Russell, IBM Corporation
61da177e4SLinus Torvalds  *           2003       Kai Germaschewski
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * This software may be used and distributed according to the terms
101da177e4SLinus Torvalds  * of the GNU General Public License, incorporated herein by reference.
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds #include "modpost.h"
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
161da177e4SLinus Torvalds  * use either stdint.h or inttypes.h for the rest. */
171da177e4SLinus Torvalds #if KERNEL_ELFCLASS == ELFCLASS32
181da177e4SLinus Torvalds typedef Elf32_Addr	kernel_ulong_t;
191d8f430cSRusty Russell #define BITS_PER_LONG 32
201da177e4SLinus Torvalds #else
211da177e4SLinus Torvalds typedef Elf64_Addr	kernel_ulong_t;
221d8f430cSRusty Russell #define BITS_PER_LONG 64
231da177e4SLinus Torvalds #endif
241da177e4SLinus Torvalds #ifdef __sun__
251da177e4SLinus Torvalds #include <inttypes.h>
261da177e4SLinus Torvalds #else
271da177e4SLinus Torvalds #include <stdint.h>
281da177e4SLinus Torvalds #endif
291da177e4SLinus Torvalds 
305e655772SJeff Mahoney #include <ctype.h>
315e655772SJeff Mahoney 
321da177e4SLinus Torvalds typedef uint32_t	__u32;
331da177e4SLinus Torvalds typedef uint16_t	__u16;
341da177e4SLinus Torvalds typedef unsigned char	__u8;
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /* Big exception to the "don't include kernel headers into userspace, which
371da177e4SLinus Torvalds  * even potentially has different endianness and word sizes, since
381da177e4SLinus Torvalds  * we handle those differences explicitly below */
391da177e4SLinus Torvalds #include "../../include/linux/mod_devicetable.h"
401d8f430cSRusty Russell #include "../../include/linux/input.h"
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds #define ADD(str, sep, cond, field)                              \
431da177e4SLinus Torvalds do {                                                            \
441da177e4SLinus Torvalds         strcat(str, sep);                                       \
451da177e4SLinus Torvalds         if (cond)                                               \
461da177e4SLinus Torvalds                 sprintf(str + strlen(str),                      \
471da177e4SLinus Torvalds                         sizeof(field) == 1 ? "%02X" :           \
481da177e4SLinus Torvalds                         sizeof(field) == 2 ? "%04X" :           \
491da177e4SLinus Torvalds                         sizeof(field) == 4 ? "%08X" : "",       \
501da177e4SLinus Torvalds                         field);                                 \
511da177e4SLinus Torvalds         else                                                    \
521da177e4SLinus Torvalds                 sprintf(str + strlen(str), "*");                \
531da177e4SLinus Torvalds } while(0)
541da177e4SLinus Torvalds 
55fb33d816SSam Ravnborg /**
56fb33d816SSam Ravnborg  * Check that sizeof(device_id type) are consistent with size of section
57fb33d816SSam Ravnborg  * in .o file. If in-consistent then userspace and kernel does not agree
58fb33d816SSam Ravnborg  * on actual size which is a bug.
59fb33d816SSam Ravnborg  **/
60fb33d816SSam Ravnborg static void device_id_size_check(const char *modname, const char *device_id,
61fb33d816SSam Ravnborg 				 unsigned long size, unsigned long id_size)
62fb33d816SSam Ravnborg {
63fb33d816SSam Ravnborg 	if (size % id_size || size < id_size) {
64fb33d816SSam Ravnborg 		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
65fb33d816SSam Ravnborg 		      "of the size of section __mod_%s_device_table=%lu.\n"
66fb33d816SSam Ravnborg 		      "Fix definition of struct %s_device_id "
67fb33d816SSam Ravnborg 		      "in mod_devicetable.h\n",
68fb33d816SSam Ravnborg 		      modname, device_id, id_size, device_id, size, device_id);
69fb33d816SSam Ravnborg 	}
70fb33d816SSam Ravnborg }
71fb33d816SSam Ravnborg 
72b19dcd93SRoman Kagan /* USB is special because the bcdDevice can be matched against a numeric range */
73b19dcd93SRoman Kagan /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
74b19dcd93SRoman Kagan static void do_usb_entry(struct usb_device_id *id,
75b19dcd93SRoman Kagan 			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
76b19dcd93SRoman Kagan 			 unsigned char range_lo, unsigned char range_hi,
77b19dcd93SRoman Kagan 			 struct module *mod)
781da177e4SLinus Torvalds {
79b19dcd93SRoman Kagan 	char alias[500];
801da177e4SLinus Torvalds 	strcpy(alias, "usb:");
811da177e4SLinus Torvalds 	ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
821da177e4SLinus Torvalds 	    id->idVendor);
831da177e4SLinus Torvalds 	ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
841da177e4SLinus Torvalds 	    id->idProduct);
85b19dcd93SRoman Kagan 
86b19dcd93SRoman Kagan 	strcat(alias, "d");
87b19dcd93SRoman Kagan 	if (bcdDevice_initial_digits)
88b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "%0*X",
89b19dcd93SRoman Kagan 			bcdDevice_initial_digits, bcdDevice_initial);
90b19dcd93SRoman Kagan 	if (range_lo == range_hi)
91b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "%u", range_lo);
92b19dcd93SRoman Kagan 	else if (range_lo > 0 || range_hi < 9)
93b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
94b19dcd93SRoman Kagan 	if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
95b19dcd93SRoman Kagan 		strcat(alias, "*");
96b19dcd93SRoman Kagan 
971da177e4SLinus Torvalds 	ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
981da177e4SLinus Torvalds 	    id->bDeviceClass);
991da177e4SLinus Torvalds 	ADD(alias, "dsc",
1001da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
1011da177e4SLinus Torvalds 	    id->bDeviceSubClass);
1021da177e4SLinus Torvalds 	ADD(alias, "dp",
1031da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
1041da177e4SLinus Torvalds 	    id->bDeviceProtocol);
1051da177e4SLinus Torvalds 	ADD(alias, "ic",
1061da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
1071da177e4SLinus Torvalds 	    id->bInterfaceClass);
1081da177e4SLinus Torvalds 	ADD(alias, "isc",
1091da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
1101da177e4SLinus Torvalds 	    id->bInterfaceSubClass);
1111da177e4SLinus Torvalds 	ADD(alias, "ip",
1121da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
1131da177e4SLinus Torvalds 	    id->bInterfaceProtocol);
114b19dcd93SRoman Kagan 
115b19dcd93SRoman Kagan 	/* Always end in a wildcard, for future extension */
116b19dcd93SRoman Kagan 	if (alias[strlen(alias)-1] != '*')
117b19dcd93SRoman Kagan 		strcat(alias, "*");
118b19dcd93SRoman Kagan 	buf_printf(&mod->dev_table_buf,
119b19dcd93SRoman Kagan 		   "MODULE_ALIAS(\"%s\");\n", alias);
120b19dcd93SRoman Kagan }
121b19dcd93SRoman Kagan 
122b19dcd93SRoman Kagan static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
123b19dcd93SRoman Kagan {
124b19dcd93SRoman Kagan 	unsigned int devlo, devhi;
125b19dcd93SRoman Kagan 	unsigned char chi, clo;
126b19dcd93SRoman Kagan 	int ndigits;
127b19dcd93SRoman Kagan 
128b19dcd93SRoman Kagan 	id->match_flags = TO_NATIVE(id->match_flags);
129b19dcd93SRoman Kagan 	id->idVendor = TO_NATIVE(id->idVendor);
130b19dcd93SRoman Kagan 	id->idProduct = TO_NATIVE(id->idProduct);
131b19dcd93SRoman Kagan 
132b19dcd93SRoman Kagan 	devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
133b19dcd93SRoman Kagan 		TO_NATIVE(id->bcdDevice_lo) : 0x0U;
134b19dcd93SRoman Kagan 	devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
135b19dcd93SRoman Kagan 		TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
136b19dcd93SRoman Kagan 
137b19dcd93SRoman Kagan 	/*
138b19dcd93SRoman Kagan 	 * Some modules (visor) have empty slots as placeholder for
139b19dcd93SRoman Kagan 	 * run-time specification that results in catch-all alias
140b19dcd93SRoman Kagan 	 */
141b19dcd93SRoman Kagan 	if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
142b19dcd93SRoman Kagan 		return;
143b19dcd93SRoman Kagan 
144b19dcd93SRoman Kagan 	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
145b19dcd93SRoman Kagan 	for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
146b19dcd93SRoman Kagan 		clo = devlo & 0xf;
147b19dcd93SRoman Kagan 		chi = devhi & 0xf;
148b19dcd93SRoman Kagan 		if (chi > 9)	/* it's bcd not hex */
149b19dcd93SRoman Kagan 			chi = 9;
150b19dcd93SRoman Kagan 		devlo >>= 4;
151b19dcd93SRoman Kagan 		devhi >>= 4;
152b19dcd93SRoman Kagan 
153b19dcd93SRoman Kagan 		if (devlo == devhi || !ndigits) {
154b19dcd93SRoman Kagan 			do_usb_entry(id, devlo, ndigits, clo, chi, mod);
155b19dcd93SRoman Kagan 			break;
156b19dcd93SRoman Kagan 		}
157b19dcd93SRoman Kagan 
158b19dcd93SRoman Kagan 		if (clo > 0)
159b19dcd93SRoman Kagan 			do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
160b19dcd93SRoman Kagan 
161b19dcd93SRoman Kagan 		if (chi < 9)
162b19dcd93SRoman Kagan 			do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
163b19dcd93SRoman Kagan 	}
164b19dcd93SRoman Kagan }
165b19dcd93SRoman Kagan 
166b19dcd93SRoman Kagan static void do_usb_table(void *symval, unsigned long size,
167b19dcd93SRoman Kagan 			 struct module *mod)
168b19dcd93SRoman Kagan {
169b19dcd93SRoman Kagan 	unsigned int i;
170b19dcd93SRoman Kagan 	const unsigned long id_size = sizeof(struct usb_device_id);
171b19dcd93SRoman Kagan 
172fb33d816SSam Ravnborg 	device_id_size_check(mod->name, "usb", size, id_size);
173fb33d816SSam Ravnborg 
174b19dcd93SRoman Kagan 	/* Leave last one: it's the terminator. */
175b19dcd93SRoman Kagan 	size -= id_size;
176b19dcd93SRoman Kagan 
177b19dcd93SRoman Kagan 	for (i = 0; i < size; i += id_size)
178b19dcd93SRoman Kagan 		do_usb_entry_multi(symval + i, mod);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /* Looks like: ieee1394:venNmoNspNverN */
1821da177e4SLinus Torvalds static int do_ieee1394_entry(const char *filename,
1831da177e4SLinus Torvalds 			     struct ieee1394_device_id *id, char *alias)
1841da177e4SLinus Torvalds {
1851da177e4SLinus Torvalds 	id->match_flags = TO_NATIVE(id->match_flags);
1861da177e4SLinus Torvalds 	id->vendor_id = TO_NATIVE(id->vendor_id);
1871da177e4SLinus Torvalds 	id->model_id = TO_NATIVE(id->model_id);
1881da177e4SLinus Torvalds 	id->specifier_id = TO_NATIVE(id->specifier_id);
1891da177e4SLinus Torvalds 	id->version = TO_NATIVE(id->version);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	strcpy(alias, "ieee1394:");
1921da177e4SLinus Torvalds 	ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
1931da177e4SLinus Torvalds 	    id->vendor_id);
1941da177e4SLinus Torvalds 	ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
1951da177e4SLinus Torvalds 	    id->model_id);
1961da177e4SLinus Torvalds 	ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
1971da177e4SLinus Torvalds 	    id->specifier_id);
1981da177e4SLinus Torvalds 	ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
1991da177e4SLinus Torvalds 	    id->version);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	return 1;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
2051da177e4SLinus Torvalds static int do_pci_entry(const char *filename,
2061da177e4SLinus Torvalds 			struct pci_device_id *id, char *alias)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	/* Class field can be divided into these three. */
2091da177e4SLinus Torvalds 	unsigned char baseclass, subclass, interface,
2101da177e4SLinus Torvalds 		baseclass_mask, subclass_mask, interface_mask;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	id->vendor = TO_NATIVE(id->vendor);
2131da177e4SLinus Torvalds 	id->device = TO_NATIVE(id->device);
2141da177e4SLinus Torvalds 	id->subvendor = TO_NATIVE(id->subvendor);
2151da177e4SLinus Torvalds 	id->subdevice = TO_NATIVE(id->subdevice);
2161da177e4SLinus Torvalds 	id->class = TO_NATIVE(id->class);
2171da177e4SLinus Torvalds 	id->class_mask = TO_NATIVE(id->class_mask);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	strcpy(alias, "pci:");
2201da177e4SLinus Torvalds 	ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
2211da177e4SLinus Torvalds 	ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
2221da177e4SLinus Torvalds 	ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
2231da177e4SLinus Torvalds 	ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	baseclass = (id->class) >> 16;
2261da177e4SLinus Torvalds 	baseclass_mask = (id->class_mask) >> 16;
2271da177e4SLinus Torvalds 	subclass = (id->class) >> 8;
2281da177e4SLinus Torvalds 	subclass_mask = (id->class_mask) >> 8;
2291da177e4SLinus Torvalds 	interface = id->class;
2301da177e4SLinus Torvalds 	interface_mask = id->class_mask;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
2331da177e4SLinus Torvalds 	    || (subclass_mask != 0 && subclass_mask != 0xFF)
2341da177e4SLinus Torvalds 	    || (interface_mask != 0 && interface_mask != 0xFF)) {
235cb80514dSSam Ravnborg 		warn("Can't handle masks in %s:%04X\n",
2361da177e4SLinus Torvalds 		     filename, id->class_mask);
2371da177e4SLinus Torvalds 		return 0;
2381da177e4SLinus Torvalds 	}
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
2411da177e4SLinus Torvalds 	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
2421da177e4SLinus Torvalds 	ADD(alias, "i", interface_mask == 0xFF, interface);
2431da177e4SLinus Torvalds 	return 1;
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds /* looks like: "ccw:tNmNdtNdmN" */
2471da177e4SLinus Torvalds static int do_ccw_entry(const char *filename,
2481da177e4SLinus Torvalds 			struct ccw_device_id *id, char *alias)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	id->match_flags = TO_NATIVE(id->match_flags);
2511da177e4SLinus Torvalds 	id->cu_type = TO_NATIVE(id->cu_type);
2521da177e4SLinus Torvalds 	id->cu_model = TO_NATIVE(id->cu_model);
2531da177e4SLinus Torvalds 	id->dev_type = TO_NATIVE(id->dev_type);
2541da177e4SLinus Torvalds 	id->dev_model = TO_NATIVE(id->dev_model);
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 	strcpy(alias, "ccw:");
2571da177e4SLinus Torvalds 	ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
2581da177e4SLinus Torvalds 	    id->cu_type);
2591da177e4SLinus Torvalds 	ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
2601da177e4SLinus Torvalds 	    id->cu_model);
2611da177e4SLinus Torvalds 	ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
2621da177e4SLinus Torvalds 	    id->dev_type);
263de1d9c03SBastian Blank 	ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
2641da177e4SLinus Torvalds 	    id->dev_model);
2651da177e4SLinus Torvalds 	return 1;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681534c382SMartin Schwidefsky /* looks like: "ap:tN" */
2691534c382SMartin Schwidefsky static int do_ap_entry(const char *filename,
2701534c382SMartin Schwidefsky 		       struct ap_device_id *id, char *alias)
2711534c382SMartin Schwidefsky {
2721534c382SMartin Schwidefsky 	sprintf(alias, "ap:t%02X", id->dev_type);
2731534c382SMartin Schwidefsky 	return 1;
2741534c382SMartin Schwidefsky }
2751534c382SMartin Schwidefsky 
2761da177e4SLinus Torvalds /* Looks like: "serio:tyNprNidNexN" */
2771da177e4SLinus Torvalds static int do_serio_entry(const char *filename,
2781da177e4SLinus Torvalds 			  struct serio_device_id *id, char *alias)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	id->type = TO_NATIVE(id->type);
2811da177e4SLinus Torvalds 	id->proto = TO_NATIVE(id->proto);
2821da177e4SLinus Torvalds 	id->id = TO_NATIVE(id->id);
2831da177e4SLinus Torvalds 	id->extra = TO_NATIVE(id->extra);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	strcpy(alias, "serio:");
2861da177e4SLinus Torvalds 	ADD(alias, "ty", id->type != SERIO_ANY, id->type);
2871da177e4SLinus Torvalds 	ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
2881da177e4SLinus Torvalds 	ADD(alias, "id", id->id != SERIO_ANY, id->id);
2891da177e4SLinus Torvalds 	ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	return 1;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds /* looks like: "pnp:dD" */
2951da177e4SLinus Torvalds static int do_pnp_entry(const char *filename,
2961da177e4SLinus Torvalds 			struct pnp_device_id *id, char *alias)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	sprintf(alias, "pnp:d%s", id->id);
2991da177e4SLinus Torvalds 	return 1;
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds /* looks like: "pnp:cCdD..." */
3031da177e4SLinus Torvalds static int do_pnp_card_entry(const char *filename,
3041da177e4SLinus Torvalds 			struct pnp_card_device_id *id, char *alias)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	int i;
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds 	sprintf(alias, "pnp:c%s", id->id);
3091da177e4SLinus Torvalds 	for (i = 0; i < PNP_MAX_DEVICES; i++) {
3101da177e4SLinus Torvalds 		if (! *id->devs[i].id)
3111da177e4SLinus Torvalds 			break;
3121da177e4SLinus Torvalds 		sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 	return 1;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
31790829cfeSDominik Brodowski /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
31890829cfeSDominik Brodowski static int do_pcmcia_entry(const char *filename,
31990829cfeSDominik Brodowski 			   struct pcmcia_device_id *id, char *alias)
32090829cfeSDominik Brodowski {
32190829cfeSDominik Brodowski 	unsigned int i;
32290829cfeSDominik Brodowski 
3234fb7edceSKars de Jong 	id->match_flags = TO_NATIVE(id->match_flags);
32490829cfeSDominik Brodowski 	id->manf_id = TO_NATIVE(id->manf_id);
32590829cfeSDominik Brodowski 	id->card_id = TO_NATIVE(id->card_id);
32690829cfeSDominik Brodowski 	id->func_id = TO_NATIVE(id->func_id);
32790829cfeSDominik Brodowski 	id->function = TO_NATIVE(id->function);
32890829cfeSDominik Brodowski 	id->device_no = TO_NATIVE(id->device_no);
3294fb7edceSKars de Jong 
33090829cfeSDominik Brodowski 	for (i=0; i<4; i++) {
33190829cfeSDominik Brodowski 		id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
33290829cfeSDominik Brodowski        }
33390829cfeSDominik Brodowski 
33490829cfeSDominik Brodowski        strcpy(alias, "pcmcia:");
33590829cfeSDominik Brodowski        ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
33690829cfeSDominik Brodowski 	   id->manf_id);
33790829cfeSDominik Brodowski        ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
33890829cfeSDominik Brodowski 	   id->card_id);
33990829cfeSDominik Brodowski        ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
34090829cfeSDominik Brodowski 	   id->func_id);
34190829cfeSDominik Brodowski        ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
34290829cfeSDominik Brodowski 	   id->function);
34390829cfeSDominik Brodowski        ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
34490829cfeSDominik Brodowski 	   id->device_no);
34590829cfeSDominik Brodowski        ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
34690829cfeSDominik Brodowski        ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
34790829cfeSDominik Brodowski        ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
34890829cfeSDominik Brodowski        ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
34990829cfeSDominik Brodowski 
35090829cfeSDominik Brodowski        return 1;
35190829cfeSDominik Brodowski }
35290829cfeSDominik Brodowski 
35390829cfeSDominik Brodowski 
35490829cfeSDominik Brodowski 
3555e655772SJeff Mahoney static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
3565e655772SJeff Mahoney {
3575e655772SJeff Mahoney     char *tmp;
3585e655772SJeff Mahoney     sprintf (alias, "of:N%sT%sC%s",
3595e655772SJeff Mahoney                     of->name[0] ? of->name : "*",
3605e655772SJeff Mahoney                     of->type[0] ? of->type : "*",
3615e655772SJeff Mahoney                     of->compatible[0] ? of->compatible : "*");
3625e655772SJeff Mahoney 
3635e655772SJeff Mahoney     /* Replace all whitespace with underscores */
3645e655772SJeff Mahoney     for (tmp = alias; tmp && *tmp; tmp++)
3655e655772SJeff Mahoney         if (isspace (*tmp))
3665e655772SJeff Mahoney             *tmp = '_';
3675e655772SJeff Mahoney 
3685e655772SJeff Mahoney     return 1;
3695e655772SJeff Mahoney }
3705e655772SJeff Mahoney 
371fb120da6SStephen Rothwell static int do_vio_entry(const char *filename, struct vio_device_id *vio,
372fb120da6SStephen Rothwell 		char *alias)
373fb120da6SStephen Rothwell {
374fb120da6SStephen Rothwell 	char *tmp;
375fb120da6SStephen Rothwell 
376fb120da6SStephen Rothwell 	sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
377fb120da6SStephen Rothwell 			vio->compat[0] ? vio->compat : "*");
378fb120da6SStephen Rothwell 
379fb120da6SStephen Rothwell 	/* Replace all whitespace with underscores */
380fb120da6SStephen Rothwell 	for (tmp = alias; tmp && *tmp; tmp++)
381fb120da6SStephen Rothwell 		if (isspace (*tmp))
382fb120da6SStephen Rothwell 			*tmp = '_';
383fb120da6SStephen Rothwell 
384fb120da6SStephen Rothwell 	return 1;
385fb120da6SStephen Rothwell }
386fb120da6SStephen Rothwell 
387a9d1b24dSGreg Kroah-Hartman static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias)
388a9d1b24dSGreg Kroah-Hartman {
389a9d1b24dSGreg Kroah-Hartman 	strcpy(alias, "i2c:");
390a9d1b24dSGreg Kroah-Hartman 	ADD(alias, "id", 1, i2c->id);
391a9d1b24dSGreg Kroah-Hartman 	return 1;
392a9d1b24dSGreg Kroah-Hartman }
393a9d1b24dSGreg Kroah-Hartman 
3941d8f430cSRusty Russell #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
3951d8f430cSRusty Russell 
3961d8f430cSRusty Russell static void do_input(char *alias,
3971d8f430cSRusty Russell 		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
3981d8f430cSRusty Russell {
3991d8f430cSRusty Russell 	unsigned int i;
400ddc5d341SDmitry Torokhov 
401ddc5d341SDmitry Torokhov 	for (i = min; i < max; i++)
402e0e92632SHans de Goede 		if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
4031d8f430cSRusty Russell 			sprintf(alias + strlen(alias), "%X,*", i);
4041d8f430cSRusty Russell }
4051d8f430cSRusty Russell 
4061d8f430cSRusty Russell /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
4071d8f430cSRusty Russell static int do_input_entry(const char *filename, struct input_device_id *id,
4081d8f430cSRusty Russell 			  char *alias)
4091d8f430cSRusty Russell {
4101d8f430cSRusty Russell 	sprintf(alias, "input:");
4111d8f430cSRusty Russell 
412ddc5d341SDmitry Torokhov 	ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
413ddc5d341SDmitry Torokhov 	ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
414ddc5d341SDmitry Torokhov 	ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
415ddc5d341SDmitry Torokhov 	ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
4161d8f430cSRusty Russell 
4171d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "-e*");
4181d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
4191d8f430cSRusty Russell 		do_input(alias, id->evbit, 0, EV_MAX);
4201d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "k*");
4211d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
4221d8f430cSRusty Russell 		do_input(alias, id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
4231d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "r*");
4241d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
4251d8f430cSRusty Russell 		do_input(alias, id->relbit, 0, REL_MAX);
4261d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "a*");
4271d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
4281d8f430cSRusty Russell 		do_input(alias, id->absbit, 0, ABS_MAX);
4291d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "m*");
4301d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
4311d8f430cSRusty Russell 		do_input(alias, id->mscbit, 0, MSC_MAX);
4321d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "l*");
4331d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
4341d8f430cSRusty Russell 		do_input(alias, id->ledbit, 0, LED_MAX);
4351d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "s*");
4361d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
4371d8f430cSRusty Russell 		do_input(alias, id->sndbit, 0, SND_MAX);
4381d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "f*");
4391d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
440cc6fa432SBrian Gerst 		do_input(alias, id->ffbit, 0, FF_MAX);
4411d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "w*");
4421d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
4431d8f430cSRusty Russell 		do_input(alias, id->swbit, 0, SW_MAX);
4441d8f430cSRusty Russell 	return 1;
4451d8f430cSRusty Russell }
4461d8f430cSRusty Russell 
447*07563c71SMichael Tokarev static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
448*07563c71SMichael Tokarev 		char *alias)
449*07563c71SMichael Tokarev {
450*07563c71SMichael Tokarev 	if (eisa->sig[0])
451*07563c71SMichael Tokarev 		sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
452*07563c71SMichael Tokarev 	return 1;
453*07563c71SMichael Tokarev }
454*07563c71SMichael Tokarev 
4551da177e4SLinus Torvalds /* Ignore any prefix, eg. v850 prepends _ */
4561da177e4SLinus Torvalds static inline int sym_is(const char *symbol, const char *name)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	const char *match;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 	match = strstr(symbol, name);
4611da177e4SLinus Torvalds 	if (!match)
4621da177e4SLinus Torvalds 		return 0;
4631da177e4SLinus Torvalds 	return match[strlen(symbol)] == '\0';
4641da177e4SLinus Torvalds }
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds static void do_table(void *symval, unsigned long size,
4671da177e4SLinus Torvalds 		     unsigned long id_size,
468fb33d816SSam Ravnborg 		     const char *device_id,
4691da177e4SLinus Torvalds 		     void *function,
4701da177e4SLinus Torvalds 		     struct module *mod)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	unsigned int i;
4731da177e4SLinus Torvalds 	char alias[500];
4741da177e4SLinus Torvalds 	int (*do_entry)(const char *, void *entry, char *alias) = function;
4751da177e4SLinus Torvalds 
476fb33d816SSam Ravnborg 	device_id_size_check(mod->name, device_id, size, id_size);
4771da177e4SLinus Torvalds 	/* Leave last one: it's the terminator. */
4781da177e4SLinus Torvalds 	size -= id_size;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	for (i = 0; i < size; i += id_size) {
4811da177e4SLinus Torvalds 		if (do_entry(mod->name, symval+i, alias)) {
4821da177e4SLinus Torvalds 			/* Always end in a wildcard, for future extension */
4831da177e4SLinus Torvalds 			if (alias[strlen(alias)-1] != '*')
4841da177e4SLinus Torvalds 				strcat(alias, "*");
4851da177e4SLinus Torvalds 			buf_printf(&mod->dev_table_buf,
4861da177e4SLinus Torvalds 				   "MODULE_ALIAS(\"%s\");\n", alias);
4871da177e4SLinus Torvalds 		}
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds }
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds /* Create MODULE_ALIAS() statements.
4921da177e4SLinus Torvalds  * At this time, we cannot write the actual output C source yet,
4931da177e4SLinus Torvalds  * so we write into the mod->dev_table_buf buffer. */
4941da177e4SLinus Torvalds void handle_moddevtable(struct module *mod, struct elf_info *info,
4951da177e4SLinus Torvalds 			Elf_Sym *sym, const char *symname)
4961da177e4SLinus Torvalds {
4971da177e4SLinus Torvalds 	void *symval;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	/* We're looking for a section relative symbol */
5001da177e4SLinus Torvalds 	if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
5011da177e4SLinus Torvalds 		return;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	symval = (void *)info->hdr
5041da177e4SLinus Torvalds 		+ info->sechdrs[sym->st_shndx].sh_offset
5051da177e4SLinus Torvalds 		+ sym->st_value;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds 	if (sym_is(symname, "__mod_pci_device_table"))
508fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
509fb33d816SSam Ravnborg 			 sizeof(struct pci_device_id), "pci",
5101da177e4SLinus Torvalds 			 do_pci_entry, mod);
5111da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_usb_device_table"))
512b19dcd93SRoman Kagan 		/* special case to handle bcdDevice ranges */
513b19dcd93SRoman Kagan 		do_usb_table(symval, sym->st_size, mod);
5141da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_ieee1394_device_table"))
515fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
516fb33d816SSam Ravnborg 			 sizeof(struct ieee1394_device_id), "ieee1394",
5171da177e4SLinus Torvalds 			 do_ieee1394_entry, mod);
5181da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_ccw_device_table"))
519fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
520fb33d816SSam Ravnborg 			 sizeof(struct ccw_device_id), "ccw",
5211da177e4SLinus Torvalds 			 do_ccw_entry, mod);
5221534c382SMartin Schwidefsky 	else if (sym_is(symname, "__mod_ap_device_table"))
5231534c382SMartin Schwidefsky 		do_table(symval, sym->st_size,
5241534c382SMartin Schwidefsky 			 sizeof(struct ap_device_id), "ap",
5251534c382SMartin Schwidefsky 			 do_ap_entry, mod);
5261da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_serio_device_table"))
527fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
528fb33d816SSam Ravnborg 			 sizeof(struct serio_device_id), "serio",
5291da177e4SLinus Torvalds 			 do_serio_entry, mod);
5301da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_pnp_device_table"))
531fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
532fb33d816SSam Ravnborg 			 sizeof(struct pnp_device_id), "pnp",
5331da177e4SLinus Torvalds 			 do_pnp_entry, mod);
5341da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_pnp_card_device_table"))
535fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
536fb33d816SSam Ravnborg 			 sizeof(struct pnp_card_device_id), "pnp_card",
5371da177e4SLinus Torvalds 			 do_pnp_card_entry, mod);
53890829cfeSDominik Brodowski 	else if (sym_is(symname, "__mod_pcmcia_device_table"))
539fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
540fb33d816SSam Ravnborg 			 sizeof(struct pcmcia_device_id), "pcmcia",
54190829cfeSDominik Brodowski 			 do_pcmcia_entry, mod);
5425e655772SJeff Mahoney         else if (sym_is(symname, "__mod_of_device_table"))
543fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
544fb33d816SSam Ravnborg 			 sizeof(struct of_device_id), "of",
5455e655772SJeff Mahoney 			 do_of_entry, mod);
546fb120da6SStephen Rothwell         else if (sym_is(symname, "__mod_vio_device_table"))
547fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
548fb33d816SSam Ravnborg 			 sizeof(struct vio_device_id), "vio",
549fb120da6SStephen Rothwell 			 do_vio_entry, mod);
550a9d1b24dSGreg Kroah-Hartman 	else if (sym_is(symname, "__mod_i2c_device_table"))
551fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
552fb33d816SSam Ravnborg 			 sizeof(struct i2c_device_id), "i2c",
553a9d1b24dSGreg Kroah-Hartman 			 do_i2c_entry, mod);
5541d8f430cSRusty Russell 	else if (sym_is(symname, "__mod_input_device_table"))
555fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
556fb33d816SSam Ravnborg 			 sizeof(struct input_device_id), "input",
5571d8f430cSRusty Russell 			 do_input_entry, mod);
558*07563c71SMichael Tokarev 	else if (sym_is(symname, "__mod_eisa_device_table"))
559*07563c71SMichael Tokarev 		do_table(symval, sym->st_size,
560*07563c71SMichael Tokarev 			 sizeof(struct eisa_device_id), "eisa",
561*07563c71SMichael Tokarev 			 do_eisa_entry, mod);
5621da177e4SLinus Torvalds }
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds /* Now add out buffered information to the generated C source */
5651da177e4SLinus Torvalds void add_moddevtable(struct buffer *buf, struct module *mod)
5661da177e4SLinus Torvalds {
5671da177e4SLinus Torvalds 	buf_printf(buf, "\n");
5681da177e4SLinus Torvalds 	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
5691da177e4SLinus Torvalds 	free(mod->dev_table_buf.p);
5701da177e4SLinus Torvalds }
571