xref: /linux/scripts/mod/file2alias.c (revision cb80514d9c517cc1d101ef304529a0e9b76b4468)
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 
55b19dcd93SRoman Kagan /* USB is special because the bcdDevice can be matched against a numeric range */
56b19dcd93SRoman Kagan /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
57b19dcd93SRoman Kagan static void do_usb_entry(struct usb_device_id *id,
58b19dcd93SRoman Kagan 			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
59b19dcd93SRoman Kagan 			 unsigned char range_lo, unsigned char range_hi,
60b19dcd93SRoman Kagan 			 struct module *mod)
611da177e4SLinus Torvalds {
62b19dcd93SRoman Kagan 	char alias[500];
631da177e4SLinus Torvalds 	strcpy(alias, "usb:");
641da177e4SLinus Torvalds 	ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
651da177e4SLinus Torvalds 	    id->idVendor);
661da177e4SLinus Torvalds 	ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
671da177e4SLinus Torvalds 	    id->idProduct);
68b19dcd93SRoman Kagan 
69b19dcd93SRoman Kagan 	strcat(alias, "d");
70b19dcd93SRoman Kagan 	if (bcdDevice_initial_digits)
71b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "%0*X",
72b19dcd93SRoman Kagan 			bcdDevice_initial_digits, bcdDevice_initial);
73b19dcd93SRoman Kagan 	if (range_lo == range_hi)
74b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "%u", range_lo);
75b19dcd93SRoman Kagan 	else if (range_lo > 0 || range_hi < 9)
76b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
77b19dcd93SRoman Kagan 	if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
78b19dcd93SRoman Kagan 		strcat(alias, "*");
79b19dcd93SRoman Kagan 
801da177e4SLinus Torvalds 	ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
811da177e4SLinus Torvalds 	    id->bDeviceClass);
821da177e4SLinus Torvalds 	ADD(alias, "dsc",
831da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
841da177e4SLinus Torvalds 	    id->bDeviceSubClass);
851da177e4SLinus Torvalds 	ADD(alias, "dp",
861da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
871da177e4SLinus Torvalds 	    id->bDeviceProtocol);
881da177e4SLinus Torvalds 	ADD(alias, "ic",
891da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
901da177e4SLinus Torvalds 	    id->bInterfaceClass);
911da177e4SLinus Torvalds 	ADD(alias, "isc",
921da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
931da177e4SLinus Torvalds 	    id->bInterfaceSubClass);
941da177e4SLinus Torvalds 	ADD(alias, "ip",
951da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
961da177e4SLinus Torvalds 	    id->bInterfaceProtocol);
97b19dcd93SRoman Kagan 
98b19dcd93SRoman Kagan 	/* Always end in a wildcard, for future extension */
99b19dcd93SRoman Kagan 	if (alias[strlen(alias)-1] != '*')
100b19dcd93SRoman Kagan 		strcat(alias, "*");
101b19dcd93SRoman Kagan 	buf_printf(&mod->dev_table_buf,
102b19dcd93SRoman Kagan 		   "MODULE_ALIAS(\"%s\");\n", alias);
103b19dcd93SRoman Kagan }
104b19dcd93SRoman Kagan 
105b19dcd93SRoman Kagan static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
106b19dcd93SRoman Kagan {
107b19dcd93SRoman Kagan 	unsigned int devlo, devhi;
108b19dcd93SRoman Kagan 	unsigned char chi, clo;
109b19dcd93SRoman Kagan 	int ndigits;
110b19dcd93SRoman Kagan 
111b19dcd93SRoman Kagan 	id->match_flags = TO_NATIVE(id->match_flags);
112b19dcd93SRoman Kagan 	id->idVendor = TO_NATIVE(id->idVendor);
113b19dcd93SRoman Kagan 	id->idProduct = TO_NATIVE(id->idProduct);
114b19dcd93SRoman Kagan 
115b19dcd93SRoman Kagan 	devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
116b19dcd93SRoman Kagan 		TO_NATIVE(id->bcdDevice_lo) : 0x0U;
117b19dcd93SRoman Kagan 	devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
118b19dcd93SRoman Kagan 		TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
119b19dcd93SRoman Kagan 
120b19dcd93SRoman Kagan 	/*
121b19dcd93SRoman Kagan 	 * Some modules (visor) have empty slots as placeholder for
122b19dcd93SRoman Kagan 	 * run-time specification that results in catch-all alias
123b19dcd93SRoman Kagan 	 */
124b19dcd93SRoman Kagan 	if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
125b19dcd93SRoman Kagan 		return;
126b19dcd93SRoman Kagan 
127b19dcd93SRoman Kagan 	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
128b19dcd93SRoman Kagan 	for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
129b19dcd93SRoman Kagan 		clo = devlo & 0xf;
130b19dcd93SRoman Kagan 		chi = devhi & 0xf;
131b19dcd93SRoman Kagan 		if (chi > 9)	/* it's bcd not hex */
132b19dcd93SRoman Kagan 			chi = 9;
133b19dcd93SRoman Kagan 		devlo >>= 4;
134b19dcd93SRoman Kagan 		devhi >>= 4;
135b19dcd93SRoman Kagan 
136b19dcd93SRoman Kagan 		if (devlo == devhi || !ndigits) {
137b19dcd93SRoman Kagan 			do_usb_entry(id, devlo, ndigits, clo, chi, mod);
138b19dcd93SRoman Kagan 			break;
139b19dcd93SRoman Kagan 		}
140b19dcd93SRoman Kagan 
141b19dcd93SRoman Kagan 		if (clo > 0)
142b19dcd93SRoman Kagan 			do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
143b19dcd93SRoman Kagan 
144b19dcd93SRoman Kagan 		if (chi < 9)
145b19dcd93SRoman Kagan 			do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
146b19dcd93SRoman Kagan 	}
147b19dcd93SRoman Kagan }
148b19dcd93SRoman Kagan 
149b19dcd93SRoman Kagan static void do_usb_table(void *symval, unsigned long size,
150b19dcd93SRoman Kagan 			 struct module *mod)
151b19dcd93SRoman Kagan {
152b19dcd93SRoman Kagan 	unsigned int i;
153b19dcd93SRoman Kagan 	const unsigned long id_size = sizeof(struct usb_device_id);
154b19dcd93SRoman Kagan 
155b19dcd93SRoman Kagan 	if (size % id_size || size < id_size) {
156*cb80514dSSam Ravnborg 		warn("%s ids %lu bad size "
157b19dcd93SRoman Kagan 		     "(each on %lu)\n", mod->name, size, id_size);
158b19dcd93SRoman Kagan 	}
159b19dcd93SRoman Kagan 	/* Leave last one: it's the terminator. */
160b19dcd93SRoman Kagan 	size -= id_size;
161b19dcd93SRoman Kagan 
162b19dcd93SRoman Kagan 	for (i = 0; i < size; i += id_size)
163b19dcd93SRoman Kagan 		do_usb_entry_multi(symval + i, mod);
1641da177e4SLinus Torvalds }
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds /* Looks like: ieee1394:venNmoNspNverN */
1671da177e4SLinus Torvalds static int do_ieee1394_entry(const char *filename,
1681da177e4SLinus Torvalds 			     struct ieee1394_device_id *id, char *alias)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds 	id->match_flags = TO_NATIVE(id->match_flags);
1711da177e4SLinus Torvalds 	id->vendor_id = TO_NATIVE(id->vendor_id);
1721da177e4SLinus Torvalds 	id->model_id = TO_NATIVE(id->model_id);
1731da177e4SLinus Torvalds 	id->specifier_id = TO_NATIVE(id->specifier_id);
1741da177e4SLinus Torvalds 	id->version = TO_NATIVE(id->version);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	strcpy(alias, "ieee1394:");
1771da177e4SLinus Torvalds 	ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
1781da177e4SLinus Torvalds 	    id->vendor_id);
1791da177e4SLinus Torvalds 	ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
1801da177e4SLinus Torvalds 	    id->model_id);
1811da177e4SLinus Torvalds 	ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
1821da177e4SLinus Torvalds 	    id->specifier_id);
1831da177e4SLinus Torvalds 	ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
1841da177e4SLinus Torvalds 	    id->version);
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	return 1;
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
1901da177e4SLinus Torvalds static int do_pci_entry(const char *filename,
1911da177e4SLinus Torvalds 			struct pci_device_id *id, char *alias)
1921da177e4SLinus Torvalds {
1931da177e4SLinus Torvalds 	/* Class field can be divided into these three. */
1941da177e4SLinus Torvalds 	unsigned char baseclass, subclass, interface,
1951da177e4SLinus Torvalds 		baseclass_mask, subclass_mask, interface_mask;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 	id->vendor = TO_NATIVE(id->vendor);
1981da177e4SLinus Torvalds 	id->device = TO_NATIVE(id->device);
1991da177e4SLinus Torvalds 	id->subvendor = TO_NATIVE(id->subvendor);
2001da177e4SLinus Torvalds 	id->subdevice = TO_NATIVE(id->subdevice);
2011da177e4SLinus Torvalds 	id->class = TO_NATIVE(id->class);
2021da177e4SLinus Torvalds 	id->class_mask = TO_NATIVE(id->class_mask);
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	strcpy(alias, "pci:");
2051da177e4SLinus Torvalds 	ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
2061da177e4SLinus Torvalds 	ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
2071da177e4SLinus Torvalds 	ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
2081da177e4SLinus Torvalds 	ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	baseclass = (id->class) >> 16;
2111da177e4SLinus Torvalds 	baseclass_mask = (id->class_mask) >> 16;
2121da177e4SLinus Torvalds 	subclass = (id->class) >> 8;
2131da177e4SLinus Torvalds 	subclass_mask = (id->class_mask) >> 8;
2141da177e4SLinus Torvalds 	interface = id->class;
2151da177e4SLinus Torvalds 	interface_mask = id->class_mask;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
2181da177e4SLinus Torvalds 	    || (subclass_mask != 0 && subclass_mask != 0xFF)
2191da177e4SLinus Torvalds 	    || (interface_mask != 0 && interface_mask != 0xFF)) {
220*cb80514dSSam Ravnborg 		warn("Can't handle masks in %s:%04X\n",
2211da177e4SLinus Torvalds 		     filename, id->class_mask);
2221da177e4SLinus Torvalds 		return 0;
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
2261da177e4SLinus Torvalds 	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
2271da177e4SLinus Torvalds 	ADD(alias, "i", interface_mask == 0xFF, interface);
2281da177e4SLinus Torvalds 	return 1;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds /* looks like: "ccw:tNmNdtNdmN" */
2321da177e4SLinus Torvalds static int do_ccw_entry(const char *filename,
2331da177e4SLinus Torvalds 			struct ccw_device_id *id, char *alias)
2341da177e4SLinus Torvalds {
2351da177e4SLinus Torvalds 	id->match_flags = TO_NATIVE(id->match_flags);
2361da177e4SLinus Torvalds 	id->cu_type = TO_NATIVE(id->cu_type);
2371da177e4SLinus Torvalds 	id->cu_model = TO_NATIVE(id->cu_model);
2381da177e4SLinus Torvalds 	id->dev_type = TO_NATIVE(id->dev_type);
2391da177e4SLinus Torvalds 	id->dev_model = TO_NATIVE(id->dev_model);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 	strcpy(alias, "ccw:");
2421da177e4SLinus Torvalds 	ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
2431da177e4SLinus Torvalds 	    id->cu_type);
2441da177e4SLinus Torvalds 	ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
2451da177e4SLinus Torvalds 	    id->cu_model);
2461da177e4SLinus Torvalds 	ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
2471da177e4SLinus Torvalds 	    id->dev_type);
2481da177e4SLinus Torvalds 	ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
2491da177e4SLinus Torvalds 	    id->dev_model);
2501da177e4SLinus Torvalds 	return 1;
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds /* Looks like: "serio:tyNprNidNexN" */
2541da177e4SLinus Torvalds static int do_serio_entry(const char *filename,
2551da177e4SLinus Torvalds 			  struct serio_device_id *id, char *alias)
2561da177e4SLinus Torvalds {
2571da177e4SLinus Torvalds 	id->type = TO_NATIVE(id->type);
2581da177e4SLinus Torvalds 	id->proto = TO_NATIVE(id->proto);
2591da177e4SLinus Torvalds 	id->id = TO_NATIVE(id->id);
2601da177e4SLinus Torvalds 	id->extra = TO_NATIVE(id->extra);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	strcpy(alias, "serio:");
2631da177e4SLinus Torvalds 	ADD(alias, "ty", id->type != SERIO_ANY, id->type);
2641da177e4SLinus Torvalds 	ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
2651da177e4SLinus Torvalds 	ADD(alias, "id", id->id != SERIO_ANY, id->id);
2661da177e4SLinus Torvalds 	ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	return 1;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds /* looks like: "pnp:dD" */
2721da177e4SLinus Torvalds static int do_pnp_entry(const char *filename,
2731da177e4SLinus Torvalds 			struct pnp_device_id *id, char *alias)
2741da177e4SLinus Torvalds {
2751da177e4SLinus Torvalds 	sprintf(alias, "pnp:d%s", id->id);
2761da177e4SLinus Torvalds 	return 1;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds /* looks like: "pnp:cCdD..." */
2801da177e4SLinus Torvalds static int do_pnp_card_entry(const char *filename,
2811da177e4SLinus Torvalds 			struct pnp_card_device_id *id, char *alias)
2821da177e4SLinus Torvalds {
2831da177e4SLinus Torvalds 	int i;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	sprintf(alias, "pnp:c%s", id->id);
2861da177e4SLinus Torvalds 	for (i = 0; i < PNP_MAX_DEVICES; i++) {
2871da177e4SLinus Torvalds 		if (! *id->devs[i].id)
2881da177e4SLinus Torvalds 			break;
2891da177e4SLinus Torvalds 		sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 	return 1;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
29490829cfeSDominik Brodowski /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
29590829cfeSDominik Brodowski static int do_pcmcia_entry(const char *filename,
29690829cfeSDominik Brodowski 			   struct pcmcia_device_id *id, char *alias)
29790829cfeSDominik Brodowski {
29890829cfeSDominik Brodowski 	unsigned int i;
29990829cfeSDominik Brodowski 
3004fb7edceSKars de Jong 	id->match_flags = TO_NATIVE(id->match_flags);
30190829cfeSDominik Brodowski 	id->manf_id = TO_NATIVE(id->manf_id);
30290829cfeSDominik Brodowski 	id->card_id = TO_NATIVE(id->card_id);
30390829cfeSDominik Brodowski 	id->func_id = TO_NATIVE(id->func_id);
30490829cfeSDominik Brodowski 	id->function = TO_NATIVE(id->function);
30590829cfeSDominik Brodowski 	id->device_no = TO_NATIVE(id->device_no);
3064fb7edceSKars de Jong 
30790829cfeSDominik Brodowski 	for (i=0; i<4; i++) {
30890829cfeSDominik Brodowski 		id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
30990829cfeSDominik Brodowski        }
31090829cfeSDominik Brodowski 
31190829cfeSDominik Brodowski        strcpy(alias, "pcmcia:");
31290829cfeSDominik Brodowski        ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
31390829cfeSDominik Brodowski 	   id->manf_id);
31490829cfeSDominik Brodowski        ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
31590829cfeSDominik Brodowski 	   id->card_id);
31690829cfeSDominik Brodowski        ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
31790829cfeSDominik Brodowski 	   id->func_id);
31890829cfeSDominik Brodowski        ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
31990829cfeSDominik Brodowski 	   id->function);
32090829cfeSDominik Brodowski        ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
32190829cfeSDominik Brodowski 	   id->device_no);
32290829cfeSDominik Brodowski        ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
32390829cfeSDominik Brodowski        ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
32490829cfeSDominik Brodowski        ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
32590829cfeSDominik Brodowski        ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
32690829cfeSDominik Brodowski 
32790829cfeSDominik Brodowski        return 1;
32890829cfeSDominik Brodowski }
32990829cfeSDominik Brodowski 
33090829cfeSDominik Brodowski 
33190829cfeSDominik Brodowski 
3325e655772SJeff Mahoney static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
3335e655772SJeff Mahoney {
3345e655772SJeff Mahoney     char *tmp;
3355e655772SJeff Mahoney     sprintf (alias, "of:N%sT%sC%s",
3365e655772SJeff Mahoney                     of->name[0] ? of->name : "*",
3375e655772SJeff Mahoney                     of->type[0] ? of->type : "*",
3385e655772SJeff Mahoney                     of->compatible[0] ? of->compatible : "*");
3395e655772SJeff Mahoney 
3405e655772SJeff Mahoney     /* Replace all whitespace with underscores */
3415e655772SJeff Mahoney     for (tmp = alias; tmp && *tmp; tmp++)
3425e655772SJeff Mahoney         if (isspace (*tmp))
3435e655772SJeff Mahoney             *tmp = '_';
3445e655772SJeff Mahoney 
3455e655772SJeff Mahoney     return 1;
3465e655772SJeff Mahoney }
3475e655772SJeff Mahoney 
348fb120da6SStephen Rothwell static int do_vio_entry(const char *filename, struct vio_device_id *vio,
349fb120da6SStephen Rothwell 		char *alias)
350fb120da6SStephen Rothwell {
351fb120da6SStephen Rothwell 	char *tmp;
352fb120da6SStephen Rothwell 
353fb120da6SStephen Rothwell 	sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
354fb120da6SStephen Rothwell 			vio->compat[0] ? vio->compat : "*");
355fb120da6SStephen Rothwell 
356fb120da6SStephen Rothwell 	/* Replace all whitespace with underscores */
357fb120da6SStephen Rothwell 	for (tmp = alias; tmp && *tmp; tmp++)
358fb120da6SStephen Rothwell 		if (isspace (*tmp))
359fb120da6SStephen Rothwell 			*tmp = '_';
360fb120da6SStephen Rothwell 
361fb120da6SStephen Rothwell 	return 1;
362fb120da6SStephen Rothwell }
363fb120da6SStephen Rothwell 
364a9d1b24dSGreg Kroah-Hartman static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias)
365a9d1b24dSGreg Kroah-Hartman {
366a9d1b24dSGreg Kroah-Hartman 	strcpy(alias, "i2c:");
367a9d1b24dSGreg Kroah-Hartman 	ADD(alias, "id", 1, i2c->id);
368a9d1b24dSGreg Kroah-Hartman 	return 1;
369a9d1b24dSGreg Kroah-Hartman }
370a9d1b24dSGreg Kroah-Hartman 
3711d8f430cSRusty Russell #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
3721d8f430cSRusty Russell 
3731d8f430cSRusty Russell static void do_input(char *alias,
3741d8f430cSRusty Russell 		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
3751d8f430cSRusty Russell {
3761d8f430cSRusty Russell 	unsigned int i;
3771d8f430cSRusty Russell 	for (i = min; i < max; i++) {
3781d8f430cSRusty Russell 		if (arr[i/BITS_PER_LONG] & (1 << (i%BITS_PER_LONG)))
3791d8f430cSRusty Russell 			sprintf(alias+strlen(alias), "%X,*", i);
3801d8f430cSRusty Russell 	}
3811d8f430cSRusty Russell }
3821d8f430cSRusty Russell 
3831d8f430cSRusty Russell /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
3841d8f430cSRusty Russell static int do_input_entry(const char *filename, struct input_device_id *id,
3851d8f430cSRusty Russell 			  char *alias)
3861d8f430cSRusty Russell {
3871d8f430cSRusty Russell 	sprintf(alias, "input:");
3881d8f430cSRusty Russell 
3891d8f430cSRusty Russell 	ADD(alias, "b", id->flags&INPUT_DEVICE_ID_MATCH_BUS, id->id.bustype);
3901d8f430cSRusty Russell 	ADD(alias, "v", id->flags&INPUT_DEVICE_ID_MATCH_VENDOR, id->id.vendor);
3911d8f430cSRusty Russell 	ADD(alias, "p", id->flags&INPUT_DEVICE_ID_MATCH_PRODUCT,
3921d8f430cSRusty Russell 	    id->id.product);
3931d8f430cSRusty Russell 	ADD(alias, "e", id->flags&INPUT_DEVICE_ID_MATCH_VERSION,
3941d8f430cSRusty Russell 	    id->id.version);
3951d8f430cSRusty Russell 
3961d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "-e*");
3971d8f430cSRusty Russell 	if (id->flags&INPUT_DEVICE_ID_MATCH_EVBIT)
3981d8f430cSRusty Russell 		do_input(alias, id->evbit, 0, EV_MAX);
3991d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "k*");
4001d8f430cSRusty Russell 	if (id->flags&INPUT_DEVICE_ID_MATCH_KEYBIT)
4011d8f430cSRusty Russell 		do_input(alias, id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
4021d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "r*");
4031d8f430cSRusty Russell 	if (id->flags&INPUT_DEVICE_ID_MATCH_RELBIT)
4041d8f430cSRusty Russell 		do_input(alias, id->relbit, 0, REL_MAX);
4051d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "a*");
4061d8f430cSRusty Russell 	if (id->flags&INPUT_DEVICE_ID_MATCH_ABSBIT)
4071d8f430cSRusty Russell 		do_input(alias, id->absbit, 0, ABS_MAX);
4081d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "m*");
4091d8f430cSRusty Russell 	if (id->flags&INPUT_DEVICE_ID_MATCH_MSCIT)
4101d8f430cSRusty Russell 		do_input(alias, id->mscbit, 0, MSC_MAX);
4111d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "l*");
4121d8f430cSRusty Russell 	if (id->flags&INPUT_DEVICE_ID_MATCH_LEDBIT)
4131d8f430cSRusty Russell 		do_input(alias, id->ledbit, 0, LED_MAX);
4141d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "s*");
4151d8f430cSRusty Russell 	if (id->flags&INPUT_DEVICE_ID_MATCH_SNDBIT)
4161d8f430cSRusty Russell 		do_input(alias, id->sndbit, 0, SND_MAX);
4171d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "f*");
4181d8f430cSRusty Russell 	if (id->flags&INPUT_DEVICE_ID_MATCH_FFBIT)
419cc6fa432SBrian Gerst 		do_input(alias, id->ffbit, 0, FF_MAX);
4201d8f430cSRusty Russell 	sprintf(alias + strlen(alias), "w*");
4211d8f430cSRusty Russell 	if (id->flags&INPUT_DEVICE_ID_MATCH_SWBIT)
4221d8f430cSRusty Russell 		do_input(alias, id->swbit, 0, SW_MAX);
4231d8f430cSRusty Russell 	return 1;
4241d8f430cSRusty Russell }
4251d8f430cSRusty Russell 
4261da177e4SLinus Torvalds /* Ignore any prefix, eg. v850 prepends _ */
4271da177e4SLinus Torvalds static inline int sym_is(const char *symbol, const char *name)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds 	const char *match;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	match = strstr(symbol, name);
4321da177e4SLinus Torvalds 	if (!match)
4331da177e4SLinus Torvalds 		return 0;
4341da177e4SLinus Torvalds 	return match[strlen(symbol)] == '\0';
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds static void do_table(void *symval, unsigned long size,
4381da177e4SLinus Torvalds 		     unsigned long id_size,
4391da177e4SLinus Torvalds 		     void *function,
4401da177e4SLinus Torvalds 		     struct module *mod)
4411da177e4SLinus Torvalds {
4421da177e4SLinus Torvalds 	unsigned int i;
4431da177e4SLinus Torvalds 	char alias[500];
4441da177e4SLinus Torvalds 	int (*do_entry)(const char *, void *entry, char *alias) = function;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	if (size % id_size || size < id_size) {
447*cb80514dSSam Ravnborg 		warn("%s ids %lu bad size "
4481da177e4SLinus Torvalds 		     "(each on %lu)\n", mod->name, size, id_size);
4491da177e4SLinus Torvalds 	}
4501da177e4SLinus Torvalds 	/* Leave last one: it's the terminator. */
4511da177e4SLinus Torvalds 	size -= id_size;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	for (i = 0; i < size; i += id_size) {
4541da177e4SLinus Torvalds 		if (do_entry(mod->name, symval+i, alias)) {
4551da177e4SLinus Torvalds 			/* Always end in a wildcard, for future extension */
4561da177e4SLinus Torvalds 			if (alias[strlen(alias)-1] != '*')
4571da177e4SLinus Torvalds 				strcat(alias, "*");
4581da177e4SLinus Torvalds 			buf_printf(&mod->dev_table_buf,
4591da177e4SLinus Torvalds 				   "MODULE_ALIAS(\"%s\");\n", alias);
4601da177e4SLinus Torvalds 		}
4611da177e4SLinus Torvalds 	}
4621da177e4SLinus Torvalds }
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds /* Create MODULE_ALIAS() statements.
4651da177e4SLinus Torvalds  * At this time, we cannot write the actual output C source yet,
4661da177e4SLinus Torvalds  * so we write into the mod->dev_table_buf buffer. */
4671da177e4SLinus Torvalds void handle_moddevtable(struct module *mod, struct elf_info *info,
4681da177e4SLinus Torvalds 			Elf_Sym *sym, const char *symname)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	void *symval;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	/* We're looking for a section relative symbol */
4731da177e4SLinus Torvalds 	if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
4741da177e4SLinus Torvalds 		return;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	symval = (void *)info->hdr
4771da177e4SLinus Torvalds 		+ info->sechdrs[sym->st_shndx].sh_offset
4781da177e4SLinus Torvalds 		+ sym->st_value;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	if (sym_is(symname, "__mod_pci_device_table"))
4811da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct pci_device_id),
4821da177e4SLinus Torvalds 			 do_pci_entry, mod);
4831da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_usb_device_table"))
484b19dcd93SRoman Kagan 		/* special case to handle bcdDevice ranges */
485b19dcd93SRoman Kagan 		do_usb_table(symval, sym->st_size, mod);
4861da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_ieee1394_device_table"))
4871da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
4881da177e4SLinus Torvalds 			 do_ieee1394_entry, mod);
4891da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_ccw_device_table"))
4901da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
4911da177e4SLinus Torvalds 			 do_ccw_entry, mod);
4921da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_serio_device_table"))
4931da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct serio_device_id),
4941da177e4SLinus Torvalds 			 do_serio_entry, mod);
4951da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_pnp_device_table"))
4961da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
4971da177e4SLinus Torvalds 			 do_pnp_entry, mod);
4981da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_pnp_card_device_table"))
4991da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
5001da177e4SLinus Torvalds 			 do_pnp_card_entry, mod);
50190829cfeSDominik Brodowski 	else if (sym_is(symname, "__mod_pcmcia_device_table"))
50290829cfeSDominik Brodowski 		do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id),
50390829cfeSDominik Brodowski 			 do_pcmcia_entry, mod);
5045e655772SJeff Mahoney         else if (sym_is(symname, "__mod_of_device_table"))
5055e655772SJeff Mahoney 		do_table(symval, sym->st_size, sizeof(struct of_device_id),
5065e655772SJeff Mahoney 			 do_of_entry, mod);
507fb120da6SStephen Rothwell         else if (sym_is(symname, "__mod_vio_device_table"))
508fb120da6SStephen Rothwell 		do_table(symval, sym->st_size, sizeof(struct vio_device_id),
509fb120da6SStephen Rothwell 			 do_vio_entry, mod);
510a9d1b24dSGreg Kroah-Hartman 	else if (sym_is(symname, "__mod_i2c_device_table"))
511a9d1b24dSGreg Kroah-Hartman 		do_table(symval, sym->st_size, sizeof(struct i2c_device_id),
512a9d1b24dSGreg Kroah-Hartman 			 do_i2c_entry, mod);
5131d8f430cSRusty Russell 	else if (sym_is(symname, "__mod_input_device_table"))
5141d8f430cSRusty Russell 		do_table(symval, sym->st_size, sizeof(struct input_device_id),
5151d8f430cSRusty Russell 			 do_input_entry, mod);
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds /* Now add out buffered information to the generated C source */
5191da177e4SLinus Torvalds void add_moddevtable(struct buffer *buf, struct module *mod)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	buf_printf(buf, "\n");
5221da177e4SLinus Torvalds 	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
5231da177e4SLinus Torvalds 	free(mod->dev_table_buf.p);
5241da177e4SLinus Torvalds }
525