xref: /linux/scripts/mod/file2alias.c (revision 29b71a1ca74491fab9fed09e9d835d840d042690)
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"
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define ADD(str, sep, cond, field)                              \
421da177e4SLinus Torvalds do {                                                            \
431da177e4SLinus Torvalds         strcat(str, sep);                                       \
441da177e4SLinus Torvalds         if (cond)                                               \
451da177e4SLinus Torvalds                 sprintf(str + strlen(str),                      \
461da177e4SLinus Torvalds                         sizeof(field) == 1 ? "%02X" :           \
471da177e4SLinus Torvalds                         sizeof(field) == 2 ? "%04X" :           \
481da177e4SLinus Torvalds                         sizeof(field) == 4 ? "%08X" : "",       \
491da177e4SLinus Torvalds                         field);                                 \
501da177e4SLinus Torvalds         else                                                    \
511da177e4SLinus Torvalds                 sprintf(str + strlen(str), "*");                \
521da177e4SLinus Torvalds } while(0)
531da177e4SLinus Torvalds 
54fb33d816SSam Ravnborg /**
55fb33d816SSam Ravnborg  * Check that sizeof(device_id type) are consistent with size of section
56fb33d816SSam Ravnborg  * in .o file. If in-consistent then userspace and kernel does not agree
57fb33d816SSam Ravnborg  * on actual size which is a bug.
58fb33d816SSam Ravnborg  **/
59fb33d816SSam Ravnborg static void device_id_size_check(const char *modname, const char *device_id,
60fb33d816SSam Ravnborg 				 unsigned long size, unsigned long id_size)
61fb33d816SSam Ravnborg {
62fb33d816SSam Ravnborg 	if (size % id_size || size < id_size) {
63fb33d816SSam Ravnborg 		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
64fb33d816SSam Ravnborg 		      "of the size of section __mod_%s_device_table=%lu.\n"
65fb33d816SSam Ravnborg 		      "Fix definition of struct %s_device_id "
66fb33d816SSam Ravnborg 		      "in mod_devicetable.h\n",
67fb33d816SSam Ravnborg 		      modname, device_id, id_size, device_id, size, device_id);
68fb33d816SSam Ravnborg 	}
69fb33d816SSam Ravnborg }
70fb33d816SSam Ravnborg 
71b19dcd93SRoman Kagan /* USB is special because the bcdDevice can be matched against a numeric range */
72b19dcd93SRoman Kagan /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
73b19dcd93SRoman Kagan static void do_usb_entry(struct usb_device_id *id,
74b19dcd93SRoman Kagan 			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
75b19dcd93SRoman Kagan 			 unsigned char range_lo, unsigned char range_hi,
76b19dcd93SRoman Kagan 			 struct module *mod)
771da177e4SLinus Torvalds {
78b19dcd93SRoman Kagan 	char alias[500];
791da177e4SLinus Torvalds 	strcpy(alias, "usb:");
801da177e4SLinus Torvalds 	ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
811da177e4SLinus Torvalds 	    id->idVendor);
821da177e4SLinus Torvalds 	ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
831da177e4SLinus Torvalds 	    id->idProduct);
84b19dcd93SRoman Kagan 
85b19dcd93SRoman Kagan 	strcat(alias, "d");
86b19dcd93SRoman Kagan 	if (bcdDevice_initial_digits)
87b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "%0*X",
88b19dcd93SRoman Kagan 			bcdDevice_initial_digits, bcdDevice_initial);
89b19dcd93SRoman Kagan 	if (range_lo == range_hi)
90b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "%u", range_lo);
91b19dcd93SRoman Kagan 	else if (range_lo > 0 || range_hi < 9)
92b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
93b19dcd93SRoman Kagan 	if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
94b19dcd93SRoman Kagan 		strcat(alias, "*");
95b19dcd93SRoman Kagan 
961da177e4SLinus Torvalds 	ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
971da177e4SLinus Torvalds 	    id->bDeviceClass);
981da177e4SLinus Torvalds 	ADD(alias, "dsc",
991da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
1001da177e4SLinus Torvalds 	    id->bDeviceSubClass);
1011da177e4SLinus Torvalds 	ADD(alias, "dp",
1021da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
1031da177e4SLinus Torvalds 	    id->bDeviceProtocol);
1041da177e4SLinus Torvalds 	ADD(alias, "ic",
1051da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
1061da177e4SLinus Torvalds 	    id->bInterfaceClass);
1071da177e4SLinus Torvalds 	ADD(alias, "isc",
1081da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
1091da177e4SLinus Torvalds 	    id->bInterfaceSubClass);
1101da177e4SLinus Torvalds 	ADD(alias, "ip",
1111da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
1121da177e4SLinus Torvalds 	    id->bInterfaceProtocol);
113b19dcd93SRoman Kagan 
114b19dcd93SRoman Kagan 	/* Always end in a wildcard, for future extension */
115b19dcd93SRoman Kagan 	if (alias[strlen(alias)-1] != '*')
116b19dcd93SRoman Kagan 		strcat(alias, "*");
117b19dcd93SRoman Kagan 	buf_printf(&mod->dev_table_buf,
118b19dcd93SRoman Kagan 		   "MODULE_ALIAS(\"%s\");\n", alias);
119b19dcd93SRoman Kagan }
120b19dcd93SRoman Kagan 
121b19dcd93SRoman Kagan static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
122b19dcd93SRoman Kagan {
123b19dcd93SRoman Kagan 	unsigned int devlo, devhi;
124b19dcd93SRoman Kagan 	unsigned char chi, clo;
125b19dcd93SRoman Kagan 	int ndigits;
126b19dcd93SRoman Kagan 
127b19dcd93SRoman Kagan 	id->match_flags = TO_NATIVE(id->match_flags);
128b19dcd93SRoman Kagan 	id->idVendor = TO_NATIVE(id->idVendor);
129b19dcd93SRoman Kagan 	id->idProduct = TO_NATIVE(id->idProduct);
130b19dcd93SRoman Kagan 
131b19dcd93SRoman Kagan 	devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
132b19dcd93SRoman Kagan 		TO_NATIVE(id->bcdDevice_lo) : 0x0U;
133b19dcd93SRoman Kagan 	devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
134b19dcd93SRoman Kagan 		TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
135b19dcd93SRoman Kagan 
136b19dcd93SRoman Kagan 	/*
137b19dcd93SRoman Kagan 	 * Some modules (visor) have empty slots as placeholder for
138b19dcd93SRoman Kagan 	 * run-time specification that results in catch-all alias
139b19dcd93SRoman Kagan 	 */
140b19dcd93SRoman Kagan 	if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
141b19dcd93SRoman Kagan 		return;
142b19dcd93SRoman Kagan 
143b19dcd93SRoman Kagan 	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
144b19dcd93SRoman Kagan 	for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
145b19dcd93SRoman Kagan 		clo = devlo & 0xf;
146b19dcd93SRoman Kagan 		chi = devhi & 0xf;
147b19dcd93SRoman Kagan 		if (chi > 9)	/* it's bcd not hex */
148b19dcd93SRoman Kagan 			chi = 9;
149b19dcd93SRoman Kagan 		devlo >>= 4;
150b19dcd93SRoman Kagan 		devhi >>= 4;
151b19dcd93SRoman Kagan 
152b19dcd93SRoman Kagan 		if (devlo == devhi || !ndigits) {
153b19dcd93SRoman Kagan 			do_usb_entry(id, devlo, ndigits, clo, chi, mod);
154b19dcd93SRoman Kagan 			break;
155b19dcd93SRoman Kagan 		}
156b19dcd93SRoman Kagan 
157b19dcd93SRoman Kagan 		if (clo > 0)
158b19dcd93SRoman Kagan 			do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
159b19dcd93SRoman Kagan 
160b19dcd93SRoman Kagan 		if (chi < 9)
161b19dcd93SRoman Kagan 			do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
162b19dcd93SRoman Kagan 	}
163b19dcd93SRoman Kagan }
164b19dcd93SRoman Kagan 
165b19dcd93SRoman Kagan static void do_usb_table(void *symval, unsigned long size,
166b19dcd93SRoman Kagan 			 struct module *mod)
167b19dcd93SRoman Kagan {
168b19dcd93SRoman Kagan 	unsigned int i;
169b19dcd93SRoman Kagan 	const unsigned long id_size = sizeof(struct usb_device_id);
170b19dcd93SRoman Kagan 
171fb33d816SSam Ravnborg 	device_id_size_check(mod->name, "usb", size, id_size);
172fb33d816SSam Ravnborg 
173b19dcd93SRoman Kagan 	/* Leave last one: it's the terminator. */
174b19dcd93SRoman Kagan 	size -= id_size;
175b19dcd93SRoman Kagan 
176b19dcd93SRoman Kagan 	for (i = 0; i < size; i += id_size)
177b19dcd93SRoman Kagan 		do_usb_entry_multi(symval + i, mod);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /* Looks like: ieee1394:venNmoNspNverN */
1811da177e4SLinus Torvalds static int do_ieee1394_entry(const char *filename,
1821da177e4SLinus Torvalds 			     struct ieee1394_device_id *id, char *alias)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	id->match_flags = TO_NATIVE(id->match_flags);
1851da177e4SLinus Torvalds 	id->vendor_id = TO_NATIVE(id->vendor_id);
1861da177e4SLinus Torvalds 	id->model_id = TO_NATIVE(id->model_id);
1871da177e4SLinus Torvalds 	id->specifier_id = TO_NATIVE(id->specifier_id);
1881da177e4SLinus Torvalds 	id->version = TO_NATIVE(id->version);
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds 	strcpy(alias, "ieee1394:");
1911da177e4SLinus Torvalds 	ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
1921da177e4SLinus Torvalds 	    id->vendor_id);
1931da177e4SLinus Torvalds 	ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
1941da177e4SLinus Torvalds 	    id->model_id);
1951da177e4SLinus Torvalds 	ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
1961da177e4SLinus Torvalds 	    id->specifier_id);
1971da177e4SLinus Torvalds 	ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
1981da177e4SLinus Torvalds 	    id->version);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	return 1;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
2041da177e4SLinus Torvalds static int do_pci_entry(const char *filename,
2051da177e4SLinus Torvalds 			struct pci_device_id *id, char *alias)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	/* Class field can be divided into these three. */
2081da177e4SLinus Torvalds 	unsigned char baseclass, subclass, interface,
2091da177e4SLinus Torvalds 		baseclass_mask, subclass_mask, interface_mask;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	id->vendor = TO_NATIVE(id->vendor);
2121da177e4SLinus Torvalds 	id->device = TO_NATIVE(id->device);
2131da177e4SLinus Torvalds 	id->subvendor = TO_NATIVE(id->subvendor);
2141da177e4SLinus Torvalds 	id->subdevice = TO_NATIVE(id->subdevice);
2151da177e4SLinus Torvalds 	id->class = TO_NATIVE(id->class);
2161da177e4SLinus Torvalds 	id->class_mask = TO_NATIVE(id->class_mask);
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	strcpy(alias, "pci:");
2191da177e4SLinus Torvalds 	ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
2201da177e4SLinus Torvalds 	ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
2211da177e4SLinus Torvalds 	ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
2221da177e4SLinus Torvalds 	ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	baseclass = (id->class) >> 16;
2251da177e4SLinus Torvalds 	baseclass_mask = (id->class_mask) >> 16;
2261da177e4SLinus Torvalds 	subclass = (id->class) >> 8;
2271da177e4SLinus Torvalds 	subclass_mask = (id->class_mask) >> 8;
2281da177e4SLinus Torvalds 	interface = id->class;
2291da177e4SLinus Torvalds 	interface_mask = id->class_mask;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
2321da177e4SLinus Torvalds 	    || (subclass_mask != 0 && subclass_mask != 0xFF)
2331da177e4SLinus Torvalds 	    || (interface_mask != 0 && interface_mask != 0xFF)) {
234cb80514dSSam Ravnborg 		warn("Can't handle masks in %s:%04X\n",
2351da177e4SLinus Torvalds 		     filename, id->class_mask);
2361da177e4SLinus Torvalds 		return 0;
2371da177e4SLinus Torvalds 	}
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
2401da177e4SLinus Torvalds 	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
2411da177e4SLinus Torvalds 	ADD(alias, "i", interface_mask == 0xFF, interface);
2421da177e4SLinus Torvalds 	return 1;
2431da177e4SLinus Torvalds }
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds /* looks like: "ccw:tNmNdtNdmN" */
2461da177e4SLinus Torvalds static int do_ccw_entry(const char *filename,
2471da177e4SLinus Torvalds 			struct ccw_device_id *id, char *alias)
2481da177e4SLinus Torvalds {
2491da177e4SLinus Torvalds 	id->match_flags = TO_NATIVE(id->match_flags);
2501da177e4SLinus Torvalds 	id->cu_type = TO_NATIVE(id->cu_type);
2511da177e4SLinus Torvalds 	id->cu_model = TO_NATIVE(id->cu_model);
2521da177e4SLinus Torvalds 	id->dev_type = TO_NATIVE(id->dev_type);
2531da177e4SLinus Torvalds 	id->dev_model = TO_NATIVE(id->dev_model);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	strcpy(alias, "ccw:");
2561da177e4SLinus Torvalds 	ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
2571da177e4SLinus Torvalds 	    id->cu_type);
2581da177e4SLinus Torvalds 	ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
2591da177e4SLinus Torvalds 	    id->cu_model);
2601da177e4SLinus Torvalds 	ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
2611da177e4SLinus Torvalds 	    id->dev_type);
262de1d9c03SBastian Blank 	ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
2631da177e4SLinus Torvalds 	    id->dev_model);
2641da177e4SLinus Torvalds 	return 1;
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2671534c382SMartin Schwidefsky /* looks like: "ap:tN" */
2681534c382SMartin Schwidefsky static int do_ap_entry(const char *filename,
2691534c382SMartin Schwidefsky 		       struct ap_device_id *id, char *alias)
2701534c382SMartin Schwidefsky {
2711534c382SMartin Schwidefsky 	sprintf(alias, "ap:t%02X", id->dev_type);
2721534c382SMartin Schwidefsky 	return 1;
2731534c382SMartin Schwidefsky }
2741534c382SMartin Schwidefsky 
2751da177e4SLinus Torvalds /* Looks like: "serio:tyNprNidNexN" */
2761da177e4SLinus Torvalds static int do_serio_entry(const char *filename,
2771da177e4SLinus Torvalds 			  struct serio_device_id *id, char *alias)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	id->type = TO_NATIVE(id->type);
2801da177e4SLinus Torvalds 	id->proto = TO_NATIVE(id->proto);
2811da177e4SLinus Torvalds 	id->id = TO_NATIVE(id->id);
2821da177e4SLinus Torvalds 	id->extra = TO_NATIVE(id->extra);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	strcpy(alias, "serio:");
2851da177e4SLinus Torvalds 	ADD(alias, "ty", id->type != SERIO_ANY, id->type);
2861da177e4SLinus Torvalds 	ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
2871da177e4SLinus Torvalds 	ADD(alias, "id", id->id != SERIO_ANY, id->id);
2881da177e4SLinus Torvalds 	ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	return 1;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
293*29b71a1cSThomas Renninger /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
294*29b71a1cSThomas Renninger static int do_acpi_entry(const char *filename,
295*29b71a1cSThomas Renninger 			struct acpi_device_id *id, char *alias)
296*29b71a1cSThomas Renninger {
297*29b71a1cSThomas Renninger 	sprintf(alias, "acpi*:%s:", id->id);
298*29b71a1cSThomas Renninger 	return 1;
299*29b71a1cSThomas Renninger }
300*29b71a1cSThomas Renninger 
3011da177e4SLinus Torvalds /* looks like: "pnp:dD" */
3021da177e4SLinus Torvalds static int do_pnp_entry(const char *filename,
3031da177e4SLinus Torvalds 			struct pnp_device_id *id, char *alias)
3041da177e4SLinus Torvalds {
3051da177e4SLinus Torvalds 	sprintf(alias, "pnp:d%s", id->id);
3061da177e4SLinus Torvalds 	return 1;
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds /* looks like: "pnp:cCdD..." */
3101da177e4SLinus Torvalds static int do_pnp_card_entry(const char *filename,
3111da177e4SLinus Torvalds 			struct pnp_card_device_id *id, char *alias)
3121da177e4SLinus Torvalds {
3131da177e4SLinus Torvalds 	int i;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	sprintf(alias, "pnp:c%s", id->id);
3161da177e4SLinus Torvalds 	for (i = 0; i < PNP_MAX_DEVICES; i++) {
3171da177e4SLinus Torvalds 		if (! *id->devs[i].id)
3181da177e4SLinus Torvalds 			break;
3191da177e4SLinus Torvalds 		sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 	return 1;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds 
32490829cfeSDominik Brodowski /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
32590829cfeSDominik Brodowski static int do_pcmcia_entry(const char *filename,
32690829cfeSDominik Brodowski 			   struct pcmcia_device_id *id, char *alias)
32790829cfeSDominik Brodowski {
32890829cfeSDominik Brodowski 	unsigned int i;
32990829cfeSDominik Brodowski 
3304fb7edceSKars de Jong 	id->match_flags = TO_NATIVE(id->match_flags);
33190829cfeSDominik Brodowski 	id->manf_id = TO_NATIVE(id->manf_id);
33290829cfeSDominik Brodowski 	id->card_id = TO_NATIVE(id->card_id);
33390829cfeSDominik Brodowski 	id->func_id = TO_NATIVE(id->func_id);
33490829cfeSDominik Brodowski 	id->function = TO_NATIVE(id->function);
33590829cfeSDominik Brodowski 	id->device_no = TO_NATIVE(id->device_no);
3364fb7edceSKars de Jong 
33790829cfeSDominik Brodowski 	for (i=0; i<4; i++) {
33890829cfeSDominik Brodowski 		id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
33990829cfeSDominik Brodowski        }
34090829cfeSDominik Brodowski 
34190829cfeSDominik Brodowski        strcpy(alias, "pcmcia:");
34290829cfeSDominik Brodowski        ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
34390829cfeSDominik Brodowski 	   id->manf_id);
34490829cfeSDominik Brodowski        ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
34590829cfeSDominik Brodowski 	   id->card_id);
34690829cfeSDominik Brodowski        ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
34790829cfeSDominik Brodowski 	   id->func_id);
34890829cfeSDominik Brodowski        ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
34990829cfeSDominik Brodowski 	   id->function);
35090829cfeSDominik Brodowski        ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
35190829cfeSDominik Brodowski 	   id->device_no);
35290829cfeSDominik Brodowski        ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
35390829cfeSDominik Brodowski        ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
35490829cfeSDominik Brodowski        ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
35590829cfeSDominik Brodowski        ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
35690829cfeSDominik Brodowski 
35790829cfeSDominik Brodowski        return 1;
35890829cfeSDominik Brodowski }
35990829cfeSDominik Brodowski 
36090829cfeSDominik Brodowski 
36190829cfeSDominik Brodowski 
3625e655772SJeff Mahoney static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
3635e655772SJeff Mahoney {
364d1ab4235SSylvain Munaut     int len;
3655e655772SJeff Mahoney     char *tmp;
366d1ab4235SSylvain Munaut     len = sprintf (alias, "of:N%sT%s",
3675e655772SJeff Mahoney                     of->name[0] ? of->name : "*",
368d1ab4235SSylvain Munaut                     of->type[0] ? of->type : "*");
369d1ab4235SSylvain Munaut 
370d1ab4235SSylvain Munaut     if (of->compatible[0])
371d1ab4235SSylvain Munaut         sprintf (&alias[len], "%sC%s",
372d1ab4235SSylvain Munaut                      of->type[0] ? "*" : "",
373d1ab4235SSylvain Munaut                      of->compatible);
3745e655772SJeff Mahoney 
3755e655772SJeff Mahoney     /* Replace all whitespace with underscores */
3765e655772SJeff Mahoney     for (tmp = alias; tmp && *tmp; tmp++)
3775e655772SJeff Mahoney         if (isspace (*tmp))
3785e655772SJeff Mahoney             *tmp = '_';
3795e655772SJeff Mahoney 
3805e655772SJeff Mahoney     return 1;
3815e655772SJeff Mahoney }
3825e655772SJeff Mahoney 
383fb120da6SStephen Rothwell static int do_vio_entry(const char *filename, struct vio_device_id *vio,
384fb120da6SStephen Rothwell 		char *alias)
385fb120da6SStephen Rothwell {
386fb120da6SStephen Rothwell 	char *tmp;
387fb120da6SStephen Rothwell 
388fb120da6SStephen Rothwell 	sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
389fb120da6SStephen Rothwell 			vio->compat[0] ? vio->compat : "*");
390fb120da6SStephen Rothwell 
391fb120da6SStephen Rothwell 	/* Replace all whitespace with underscores */
392fb120da6SStephen Rothwell 	for (tmp = alias; tmp && *tmp; tmp++)
393fb120da6SStephen Rothwell 		if (isspace (*tmp))
394fb120da6SStephen Rothwell 			*tmp = '_';
395fb120da6SStephen Rothwell 
396fb120da6SStephen Rothwell 	return 1;
397fb120da6SStephen Rothwell }
398fb120da6SStephen Rothwell 
399a9d1b24dSGreg Kroah-Hartman static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias)
400a9d1b24dSGreg Kroah-Hartman {
401a9d1b24dSGreg Kroah-Hartman 	strcpy(alias, "i2c:");
402a9d1b24dSGreg Kroah-Hartman 	ADD(alias, "id", 1, i2c->id);
403a9d1b24dSGreg Kroah-Hartman 	return 1;
404a9d1b24dSGreg Kroah-Hartman }
405a9d1b24dSGreg Kroah-Hartman 
4061d8f430cSRusty Russell #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
4071d8f430cSRusty Russell 
4081d8f430cSRusty Russell static void do_input(char *alias,
4091d8f430cSRusty Russell 		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
4101d8f430cSRusty Russell {
4111d8f430cSRusty Russell 	unsigned int i;
412ddc5d341SDmitry Torokhov 
413ddc5d341SDmitry Torokhov 	for (i = min; i < max; i++)
414e0e92632SHans de Goede 		if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
4151d8f430cSRusty Russell 			sprintf(alias + strlen(alias), "%X,*", i);
4161d8f430cSRusty Russell }
4171d8f430cSRusty Russell 
4181d8f430cSRusty Russell /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
4191d8f430cSRusty Russell static int do_input_entry(const char *filename, struct input_device_id *id,
4201d8f430cSRusty Russell 			  char *alias)
4211d8f430cSRusty Russell {
4221d8f430cSRusty Russell 	sprintf(alias, "input:");
4231d8f430cSRusty Russell 
424ddc5d341SDmitry Torokhov 	ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
425ddc5d341SDmitry Torokhov 	ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
426ddc5d341SDmitry Torokhov 	ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
427ddc5d341SDmitry Torokhov 	ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
4281d8f430cSRusty Russell 
4291d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "-e*");
4301d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
431dc24f0e7SSam Ravnborg 		do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX);
4321d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "k*");
4331d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
434dc24f0e7SSam Ravnborg 		do_input(alias, id->keybit,
435dc24f0e7SSam Ravnborg 			 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
436dc24f0e7SSam Ravnborg 			 INPUT_DEVICE_ID_KEY_MAX);
4371d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "r*");
4381d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
439dc24f0e7SSam Ravnborg 		do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX);
4401d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "a*");
4411d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
442dc24f0e7SSam Ravnborg 		do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
4431d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "m*");
4441d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
445dc24f0e7SSam Ravnborg 		do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
4461d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "l*");
4471d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
448dc24f0e7SSam Ravnborg 		do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
4491d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "s*");
4501d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
451dc24f0e7SSam Ravnborg 		do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
4521d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "f*");
4531d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
454dc24f0e7SSam Ravnborg 		do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
4551d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "w*");
4561d8f430cSRusty Russell 	if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
457dc24f0e7SSam Ravnborg 		do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX);
4581d8f430cSRusty Russell 	return 1;
4591d8f430cSRusty Russell }
4601d8f430cSRusty Russell 
46107563c71SMichael Tokarev static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
46207563c71SMichael Tokarev 		char *alias)
46307563c71SMichael Tokarev {
46407563c71SMichael Tokarev 	if (eisa->sig[0])
46507563c71SMichael Tokarev 		sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
46607563c71SMichael Tokarev 	return 1;
46707563c71SMichael Tokarev }
46807563c71SMichael Tokarev 
469f3cf2673SKyle McMartin /* Looks like: parisc:tNhvNrevNsvN */
470f3cf2673SKyle McMartin static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
471f3cf2673SKyle McMartin 		char *alias)
472f3cf2673SKyle McMartin {
473f3cf2673SKyle McMartin 	id->hw_type = TO_NATIVE(id->hw_type);
474f3cf2673SKyle McMartin 	id->hversion = TO_NATIVE(id->hversion);
475f3cf2673SKyle McMartin 	id->hversion_rev = TO_NATIVE(id->hversion_rev);
476f3cf2673SKyle McMartin 	id->sversion = TO_NATIVE(id->sversion);
477f3cf2673SKyle McMartin 
478f3cf2673SKyle McMartin 	strcpy(alias, "parisc:");
479f354ef8aSKyle McMartin 	ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type);
480f354ef8aSKyle McMartin 	ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion);
481f354ef8aSKyle McMartin 	ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev);
482f354ef8aSKyle McMartin 	ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion);
483f3cf2673SKyle McMartin 
484f3cf2673SKyle McMartin 	return 1;
485f3cf2673SKyle McMartin }
486f3cf2673SKyle McMartin 
4871da177e4SLinus Torvalds /* Ignore any prefix, eg. v850 prepends _ */
4881da177e4SLinus Torvalds static inline int sym_is(const char *symbol, const char *name)
4891da177e4SLinus Torvalds {
4901da177e4SLinus Torvalds 	const char *match;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	match = strstr(symbol, name);
4931da177e4SLinus Torvalds 	if (!match)
4941da177e4SLinus Torvalds 		return 0;
4951da177e4SLinus Torvalds 	return match[strlen(symbol)] == '\0';
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds static void do_table(void *symval, unsigned long size,
4991da177e4SLinus Torvalds 		     unsigned long id_size,
500fb33d816SSam Ravnborg 		     const char *device_id,
5011da177e4SLinus Torvalds 		     void *function,
5021da177e4SLinus Torvalds 		     struct module *mod)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	unsigned int i;
5051da177e4SLinus Torvalds 	char alias[500];
5061da177e4SLinus Torvalds 	int (*do_entry)(const char *, void *entry, char *alias) = function;
5071da177e4SLinus Torvalds 
508fb33d816SSam Ravnborg 	device_id_size_check(mod->name, device_id, size, id_size);
5091da177e4SLinus Torvalds 	/* Leave last one: it's the terminator. */
5101da177e4SLinus Torvalds 	size -= id_size;
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds 	for (i = 0; i < size; i += id_size) {
5131da177e4SLinus Torvalds 		if (do_entry(mod->name, symval+i, alias)) {
5141da177e4SLinus Torvalds 			/* Always end in a wildcard, for future extension */
5151da177e4SLinus Torvalds 			if (alias[strlen(alias)-1] != '*')
5161da177e4SLinus Torvalds 				strcat(alias, "*");
5171da177e4SLinus Torvalds 			buf_printf(&mod->dev_table_buf,
5181da177e4SLinus Torvalds 				   "MODULE_ALIAS(\"%s\");\n", alias);
5191da177e4SLinus Torvalds 		}
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds /* Create MODULE_ALIAS() statements.
5241da177e4SLinus Torvalds  * At this time, we cannot write the actual output C source yet,
5251da177e4SLinus Torvalds  * so we write into the mod->dev_table_buf buffer. */
5261da177e4SLinus Torvalds void handle_moddevtable(struct module *mod, struct elf_info *info,
5271da177e4SLinus Torvalds 			Elf_Sym *sym, const char *symname)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	void *symval;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 	/* We're looking for a section relative symbol */
5321da177e4SLinus Torvalds 	if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
5331da177e4SLinus Torvalds 		return;
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds 	symval = (void *)info->hdr
5361da177e4SLinus Torvalds 		+ info->sechdrs[sym->st_shndx].sh_offset
5371da177e4SLinus Torvalds 		+ sym->st_value;
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	if (sym_is(symname, "__mod_pci_device_table"))
540fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
541fb33d816SSam Ravnborg 			 sizeof(struct pci_device_id), "pci",
5421da177e4SLinus Torvalds 			 do_pci_entry, mod);
5431da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_usb_device_table"))
544b19dcd93SRoman Kagan 		/* special case to handle bcdDevice ranges */
545b19dcd93SRoman Kagan 		do_usb_table(symval, sym->st_size, mod);
5461da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_ieee1394_device_table"))
547fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
548fb33d816SSam Ravnborg 			 sizeof(struct ieee1394_device_id), "ieee1394",
5491da177e4SLinus Torvalds 			 do_ieee1394_entry, mod);
5501da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_ccw_device_table"))
551fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
552fb33d816SSam Ravnborg 			 sizeof(struct ccw_device_id), "ccw",
5531da177e4SLinus Torvalds 			 do_ccw_entry, mod);
5541534c382SMartin Schwidefsky 	else if (sym_is(symname, "__mod_ap_device_table"))
5551534c382SMartin Schwidefsky 		do_table(symval, sym->st_size,
5561534c382SMartin Schwidefsky 			 sizeof(struct ap_device_id), "ap",
5571534c382SMartin Schwidefsky 			 do_ap_entry, mod);
5581da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_serio_device_table"))
559fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
560fb33d816SSam Ravnborg 			 sizeof(struct serio_device_id), "serio",
5611da177e4SLinus Torvalds 			 do_serio_entry, mod);
562*29b71a1cSThomas Renninger 	else if (sym_is(symname, "__mod_acpi_device_table"))
563*29b71a1cSThomas Renninger 		do_table(symval, sym->st_size,
564*29b71a1cSThomas Renninger 			 sizeof(struct acpi_device_id), "acpi",
565*29b71a1cSThomas Renninger 			 do_acpi_entry, mod);
5661da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_pnp_device_table"))
567fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
568fb33d816SSam Ravnborg 			 sizeof(struct pnp_device_id), "pnp",
5691da177e4SLinus Torvalds 			 do_pnp_entry, mod);
5701da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_pnp_card_device_table"))
571fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
572fb33d816SSam Ravnborg 			 sizeof(struct pnp_card_device_id), "pnp_card",
5731da177e4SLinus Torvalds 			 do_pnp_card_entry, mod);
57490829cfeSDominik Brodowski 	else if (sym_is(symname, "__mod_pcmcia_device_table"))
575fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
576fb33d816SSam Ravnborg 			 sizeof(struct pcmcia_device_id), "pcmcia",
57790829cfeSDominik Brodowski 			 do_pcmcia_entry, mod);
5785e655772SJeff Mahoney         else if (sym_is(symname, "__mod_of_device_table"))
579fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
580fb33d816SSam Ravnborg 			 sizeof(struct of_device_id), "of",
5815e655772SJeff Mahoney 			 do_of_entry, mod);
582fb120da6SStephen Rothwell         else if (sym_is(symname, "__mod_vio_device_table"))
583fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
584fb33d816SSam Ravnborg 			 sizeof(struct vio_device_id), "vio",
585fb120da6SStephen Rothwell 			 do_vio_entry, mod);
586a9d1b24dSGreg Kroah-Hartman 	else if (sym_is(symname, "__mod_i2c_device_table"))
587fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
588fb33d816SSam Ravnborg 			 sizeof(struct i2c_device_id), "i2c",
589a9d1b24dSGreg Kroah-Hartman 			 do_i2c_entry, mod);
5901d8f430cSRusty Russell 	else if (sym_is(symname, "__mod_input_device_table"))
591fb33d816SSam Ravnborg 		do_table(symval, sym->st_size,
592fb33d816SSam Ravnborg 			 sizeof(struct input_device_id), "input",
5931d8f430cSRusty Russell 			 do_input_entry, mod);
59407563c71SMichael Tokarev 	else if (sym_is(symname, "__mod_eisa_device_table"))
59507563c71SMichael Tokarev 		do_table(symval, sym->st_size,
59607563c71SMichael Tokarev 			 sizeof(struct eisa_device_id), "eisa",
59707563c71SMichael Tokarev 			 do_eisa_entry, mod);
598f3cf2673SKyle McMartin 	else if (sym_is(symname, "__mod_parisc_device_table"))
599f3cf2673SKyle McMartin 		do_table(symval, sym->st_size,
600f3cf2673SKyle McMartin 			 sizeof(struct parisc_device_id), "parisc",
601f3cf2673SKyle McMartin 			 do_parisc_entry, mod);
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds /* Now add out buffered information to the generated C source */
6051da177e4SLinus Torvalds void add_moddevtable(struct buffer *buf, struct module *mod)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	buf_printf(buf, "\n");
6081da177e4SLinus Torvalds 	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
6091da177e4SLinus Torvalds 	free(mod->dev_table_buf.p);
6101da177e4SLinus Torvalds }
611