xref: /linux/scripts/mod/file2alias.c (revision a9d1b24d91f91b77db3da8aeacb414764f789b9c)
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;
191da177e4SLinus Torvalds #else
201da177e4SLinus Torvalds typedef Elf64_Addr	kernel_ulong_t;
211da177e4SLinus Torvalds #endif
221da177e4SLinus Torvalds #ifdef __sun__
231da177e4SLinus Torvalds #include <inttypes.h>
241da177e4SLinus Torvalds #else
251da177e4SLinus Torvalds #include <stdint.h>
261da177e4SLinus Torvalds #endif
271da177e4SLinus Torvalds 
285e655772SJeff Mahoney #include <ctype.h>
295e655772SJeff Mahoney 
301da177e4SLinus Torvalds typedef uint32_t	__u32;
311da177e4SLinus Torvalds typedef uint16_t	__u16;
321da177e4SLinus Torvalds typedef unsigned char	__u8;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds /* Big exception to the "don't include kernel headers into userspace, which
351da177e4SLinus Torvalds  * even potentially has different endianness and word sizes, since
361da177e4SLinus Torvalds  * we handle those differences explicitly below */
371da177e4SLinus Torvalds #include "../../include/linux/mod_devicetable.h"
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define ADD(str, sep, cond, field)                              \
401da177e4SLinus Torvalds do {                                                            \
411da177e4SLinus Torvalds         strcat(str, sep);                                       \
421da177e4SLinus Torvalds         if (cond)                                               \
431da177e4SLinus Torvalds                 sprintf(str + strlen(str),                      \
441da177e4SLinus Torvalds                         sizeof(field) == 1 ? "%02X" :           \
451da177e4SLinus Torvalds                         sizeof(field) == 2 ? "%04X" :           \
461da177e4SLinus Torvalds                         sizeof(field) == 4 ? "%08X" : "",       \
471da177e4SLinus Torvalds                         field);                                 \
481da177e4SLinus Torvalds         else                                                    \
491da177e4SLinus Torvalds                 sprintf(str + strlen(str), "*");                \
501da177e4SLinus Torvalds } while(0)
511da177e4SLinus Torvalds 
52b19dcd93SRoman Kagan /* USB is special because the bcdDevice can be matched against a numeric range */
53b19dcd93SRoman Kagan /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
54b19dcd93SRoman Kagan static void do_usb_entry(struct usb_device_id *id,
55b19dcd93SRoman Kagan 			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
56b19dcd93SRoman Kagan 			 unsigned char range_lo, unsigned char range_hi,
57b19dcd93SRoman Kagan 			 struct module *mod)
581da177e4SLinus Torvalds {
59b19dcd93SRoman Kagan 	char alias[500];
601da177e4SLinus Torvalds 	strcpy(alias, "usb:");
611da177e4SLinus Torvalds 	ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
621da177e4SLinus Torvalds 	    id->idVendor);
631da177e4SLinus Torvalds 	ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
641da177e4SLinus Torvalds 	    id->idProduct);
65b19dcd93SRoman Kagan 
66b19dcd93SRoman Kagan 	strcat(alias, "d");
67b19dcd93SRoman Kagan 	if (bcdDevice_initial_digits)
68b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "%0*X",
69b19dcd93SRoman Kagan 			bcdDevice_initial_digits, bcdDevice_initial);
70b19dcd93SRoman Kagan 	if (range_lo == range_hi)
71b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "%u", range_lo);
72b19dcd93SRoman Kagan 	else if (range_lo > 0 || range_hi < 9)
73b19dcd93SRoman Kagan 		sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi);
74b19dcd93SRoman Kagan 	if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
75b19dcd93SRoman Kagan 		strcat(alias, "*");
76b19dcd93SRoman Kagan 
771da177e4SLinus Torvalds 	ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
781da177e4SLinus Torvalds 	    id->bDeviceClass);
791da177e4SLinus Torvalds 	ADD(alias, "dsc",
801da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
811da177e4SLinus Torvalds 	    id->bDeviceSubClass);
821da177e4SLinus Torvalds 	ADD(alias, "dp",
831da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
841da177e4SLinus Torvalds 	    id->bDeviceProtocol);
851da177e4SLinus Torvalds 	ADD(alias, "ic",
861da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
871da177e4SLinus Torvalds 	    id->bInterfaceClass);
881da177e4SLinus Torvalds 	ADD(alias, "isc",
891da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
901da177e4SLinus Torvalds 	    id->bInterfaceSubClass);
911da177e4SLinus Torvalds 	ADD(alias, "ip",
921da177e4SLinus Torvalds 	    id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
931da177e4SLinus Torvalds 	    id->bInterfaceProtocol);
94b19dcd93SRoman Kagan 
95b19dcd93SRoman Kagan 	/* Always end in a wildcard, for future extension */
96b19dcd93SRoman Kagan 	if (alias[strlen(alias)-1] != '*')
97b19dcd93SRoman Kagan 		strcat(alias, "*");
98b19dcd93SRoman Kagan 	buf_printf(&mod->dev_table_buf,
99b19dcd93SRoman Kagan 		   "MODULE_ALIAS(\"%s\");\n", alias);
100b19dcd93SRoman Kagan }
101b19dcd93SRoman Kagan 
102b19dcd93SRoman Kagan static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
103b19dcd93SRoman Kagan {
104b19dcd93SRoman Kagan 	unsigned int devlo, devhi;
105b19dcd93SRoman Kagan 	unsigned char chi, clo;
106b19dcd93SRoman Kagan 	int ndigits;
107b19dcd93SRoman Kagan 
108b19dcd93SRoman Kagan 	id->match_flags = TO_NATIVE(id->match_flags);
109b19dcd93SRoman Kagan 	id->idVendor = TO_NATIVE(id->idVendor);
110b19dcd93SRoman Kagan 	id->idProduct = TO_NATIVE(id->idProduct);
111b19dcd93SRoman Kagan 
112b19dcd93SRoman Kagan 	devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
113b19dcd93SRoman Kagan 		TO_NATIVE(id->bcdDevice_lo) : 0x0U;
114b19dcd93SRoman Kagan 	devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
115b19dcd93SRoman Kagan 		TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
116b19dcd93SRoman Kagan 
117b19dcd93SRoman Kagan 	/*
118b19dcd93SRoman Kagan 	 * Some modules (visor) have empty slots as placeholder for
119b19dcd93SRoman Kagan 	 * run-time specification that results in catch-all alias
120b19dcd93SRoman Kagan 	 */
121b19dcd93SRoman Kagan 	if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass))
122b19dcd93SRoman Kagan 		return;
123b19dcd93SRoman Kagan 
124b19dcd93SRoman Kagan 	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
125b19dcd93SRoman Kagan 	for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
126b19dcd93SRoman Kagan 		clo = devlo & 0xf;
127b19dcd93SRoman Kagan 		chi = devhi & 0xf;
128b19dcd93SRoman Kagan 		if (chi > 9)	/* it's bcd not hex */
129b19dcd93SRoman Kagan 			chi = 9;
130b19dcd93SRoman Kagan 		devlo >>= 4;
131b19dcd93SRoman Kagan 		devhi >>= 4;
132b19dcd93SRoman Kagan 
133b19dcd93SRoman Kagan 		if (devlo == devhi || !ndigits) {
134b19dcd93SRoman Kagan 			do_usb_entry(id, devlo, ndigits, clo, chi, mod);
135b19dcd93SRoman Kagan 			break;
136b19dcd93SRoman Kagan 		}
137b19dcd93SRoman Kagan 
138b19dcd93SRoman Kagan 		if (clo > 0)
139b19dcd93SRoman Kagan 			do_usb_entry(id, devlo++, ndigits, clo, 9, mod);
140b19dcd93SRoman Kagan 
141b19dcd93SRoman Kagan 		if (chi < 9)
142b19dcd93SRoman Kagan 			do_usb_entry(id, devhi--, ndigits, 0, chi, mod);
143b19dcd93SRoman Kagan 	}
144b19dcd93SRoman Kagan }
145b19dcd93SRoman Kagan 
146b19dcd93SRoman Kagan static void do_usb_table(void *symval, unsigned long size,
147b19dcd93SRoman Kagan 			 struct module *mod)
148b19dcd93SRoman Kagan {
149b19dcd93SRoman Kagan 	unsigned int i;
150b19dcd93SRoman Kagan 	const unsigned long id_size = sizeof(struct usb_device_id);
151b19dcd93SRoman Kagan 
152b19dcd93SRoman Kagan 	if (size % id_size || size < id_size) {
153b19dcd93SRoman Kagan 		fprintf(stderr, "*** Warning: %s ids %lu bad size "
154b19dcd93SRoman Kagan 			"(each on %lu)\n", mod->name, size, id_size);
155b19dcd93SRoman Kagan 	}
156b19dcd93SRoman Kagan 	/* Leave last one: it's the terminator. */
157b19dcd93SRoman Kagan 	size -= id_size;
158b19dcd93SRoman Kagan 
159b19dcd93SRoman Kagan 	for (i = 0; i < size; i += id_size)
160b19dcd93SRoman Kagan 		do_usb_entry_multi(symval + i, mod);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds /* Looks like: ieee1394:venNmoNspNverN */
1641da177e4SLinus Torvalds static int do_ieee1394_entry(const char *filename,
1651da177e4SLinus Torvalds 			     struct ieee1394_device_id *id, char *alias)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	id->match_flags = TO_NATIVE(id->match_flags);
1681da177e4SLinus Torvalds 	id->vendor_id = TO_NATIVE(id->vendor_id);
1691da177e4SLinus Torvalds 	id->model_id = TO_NATIVE(id->model_id);
1701da177e4SLinus Torvalds 	id->specifier_id = TO_NATIVE(id->specifier_id);
1711da177e4SLinus Torvalds 	id->version = TO_NATIVE(id->version);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 	strcpy(alias, "ieee1394:");
1741da177e4SLinus Torvalds 	ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
1751da177e4SLinus Torvalds 	    id->vendor_id);
1761da177e4SLinus Torvalds 	ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
1771da177e4SLinus Torvalds 	    id->model_id);
1781da177e4SLinus Torvalds 	ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
1791da177e4SLinus Torvalds 	    id->specifier_id);
1801da177e4SLinus Torvalds 	ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
1811da177e4SLinus Torvalds 	    id->version);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	return 1;
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /* Looks like: pci:vNdNsvNsdNbcNscNiN. */
1871da177e4SLinus Torvalds static int do_pci_entry(const char *filename,
1881da177e4SLinus Torvalds 			struct pci_device_id *id, char *alias)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	/* Class field can be divided into these three. */
1911da177e4SLinus Torvalds 	unsigned char baseclass, subclass, interface,
1921da177e4SLinus Torvalds 		baseclass_mask, subclass_mask, interface_mask;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	id->vendor = TO_NATIVE(id->vendor);
1951da177e4SLinus Torvalds 	id->device = TO_NATIVE(id->device);
1961da177e4SLinus Torvalds 	id->subvendor = TO_NATIVE(id->subvendor);
1971da177e4SLinus Torvalds 	id->subdevice = TO_NATIVE(id->subdevice);
1981da177e4SLinus Torvalds 	id->class = TO_NATIVE(id->class);
1991da177e4SLinus Torvalds 	id->class_mask = TO_NATIVE(id->class_mask);
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	strcpy(alias, "pci:");
2021da177e4SLinus Torvalds 	ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
2031da177e4SLinus Torvalds 	ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
2041da177e4SLinus Torvalds 	ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
2051da177e4SLinus Torvalds 	ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 	baseclass = (id->class) >> 16;
2081da177e4SLinus Torvalds 	baseclass_mask = (id->class_mask) >> 16;
2091da177e4SLinus Torvalds 	subclass = (id->class) >> 8;
2101da177e4SLinus Torvalds 	subclass_mask = (id->class_mask) >> 8;
2111da177e4SLinus Torvalds 	interface = id->class;
2121da177e4SLinus Torvalds 	interface_mask = id->class_mask;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
2151da177e4SLinus Torvalds 	    || (subclass_mask != 0 && subclass_mask != 0xFF)
2161da177e4SLinus Torvalds 	    || (interface_mask != 0 && interface_mask != 0xFF)) {
2171da177e4SLinus Torvalds 		fprintf(stderr,
2181da177e4SLinus Torvalds 			"*** Warning: Can't handle masks in %s:%04X\n",
2191da177e4SLinus Torvalds 			filename, id->class_mask);
2201da177e4SLinus Torvalds 		return 0;
2211da177e4SLinus Torvalds 	}
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
2241da177e4SLinus Torvalds 	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
2251da177e4SLinus Torvalds 	ADD(alias, "i", interface_mask == 0xFF, interface);
2261da177e4SLinus Torvalds 	return 1;
2271da177e4SLinus Torvalds }
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds /* looks like: "ccw:tNmNdtNdmN" */
2301da177e4SLinus Torvalds static int do_ccw_entry(const char *filename,
2311da177e4SLinus Torvalds 			struct ccw_device_id *id, char *alias)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	id->match_flags = TO_NATIVE(id->match_flags);
2341da177e4SLinus Torvalds 	id->cu_type = TO_NATIVE(id->cu_type);
2351da177e4SLinus Torvalds 	id->cu_model = TO_NATIVE(id->cu_model);
2361da177e4SLinus Torvalds 	id->dev_type = TO_NATIVE(id->dev_type);
2371da177e4SLinus Torvalds 	id->dev_model = TO_NATIVE(id->dev_model);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	strcpy(alias, "ccw:");
2401da177e4SLinus Torvalds 	ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
2411da177e4SLinus Torvalds 	    id->cu_type);
2421da177e4SLinus Torvalds 	ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
2431da177e4SLinus Torvalds 	    id->cu_model);
2441da177e4SLinus Torvalds 	ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
2451da177e4SLinus Torvalds 	    id->dev_type);
2461da177e4SLinus Torvalds 	ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
2471da177e4SLinus Torvalds 	    id->dev_model);
2481da177e4SLinus Torvalds 	return 1;
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds /* Looks like: "serio:tyNprNidNexN" */
2521da177e4SLinus Torvalds static int do_serio_entry(const char *filename,
2531da177e4SLinus Torvalds 			  struct serio_device_id *id, char *alias)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	id->type = TO_NATIVE(id->type);
2561da177e4SLinus Torvalds 	id->proto = TO_NATIVE(id->proto);
2571da177e4SLinus Torvalds 	id->id = TO_NATIVE(id->id);
2581da177e4SLinus Torvalds 	id->extra = TO_NATIVE(id->extra);
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	strcpy(alias, "serio:");
2611da177e4SLinus Torvalds 	ADD(alias, "ty", id->type != SERIO_ANY, id->type);
2621da177e4SLinus Torvalds 	ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
2631da177e4SLinus Torvalds 	ADD(alias, "id", id->id != SERIO_ANY, id->id);
2641da177e4SLinus Torvalds 	ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	return 1;
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds /* looks like: "pnp:dD" */
2701da177e4SLinus Torvalds static int do_pnp_entry(const char *filename,
2711da177e4SLinus Torvalds 			struct pnp_device_id *id, char *alias)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	sprintf(alias, "pnp:d%s", id->id);
2741da177e4SLinus Torvalds 	return 1;
2751da177e4SLinus Torvalds }
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds /* looks like: "pnp:cCdD..." */
2781da177e4SLinus Torvalds static int do_pnp_card_entry(const char *filename,
2791da177e4SLinus Torvalds 			struct pnp_card_device_id *id, char *alias)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	int i;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	sprintf(alias, "pnp:c%s", id->id);
2841da177e4SLinus Torvalds 	for (i = 0; i < PNP_MAX_DEVICES; i++) {
2851da177e4SLinus Torvalds 		if (! *id->devs[i].id)
2861da177e4SLinus Torvalds 			break;
2871da177e4SLinus Torvalds 		sprintf(alias + strlen(alias), "d%s", id->devs[i].id);
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 	return 1;
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
29290829cfeSDominik Brodowski /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
29390829cfeSDominik Brodowski static int do_pcmcia_entry(const char *filename,
29490829cfeSDominik Brodowski 			   struct pcmcia_device_id *id, char *alias)
29590829cfeSDominik Brodowski {
29690829cfeSDominik Brodowski 	unsigned int i;
29790829cfeSDominik Brodowski 
2984fb7edceSKars de Jong 	id->match_flags = TO_NATIVE(id->match_flags);
29990829cfeSDominik Brodowski 	id->manf_id = TO_NATIVE(id->manf_id);
30090829cfeSDominik Brodowski 	id->card_id = TO_NATIVE(id->card_id);
30190829cfeSDominik Brodowski 	id->func_id = TO_NATIVE(id->func_id);
30290829cfeSDominik Brodowski 	id->function = TO_NATIVE(id->function);
30390829cfeSDominik Brodowski 	id->device_no = TO_NATIVE(id->device_no);
3044fb7edceSKars de Jong 
30590829cfeSDominik Brodowski 	for (i=0; i<4; i++) {
30690829cfeSDominik Brodowski 		id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
30790829cfeSDominik Brodowski        }
30890829cfeSDominik Brodowski 
30990829cfeSDominik Brodowski        strcpy(alias, "pcmcia:");
31090829cfeSDominik Brodowski        ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
31190829cfeSDominik Brodowski 	   id->manf_id);
31290829cfeSDominik Brodowski        ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
31390829cfeSDominik Brodowski 	   id->card_id);
31490829cfeSDominik Brodowski        ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
31590829cfeSDominik Brodowski 	   id->func_id);
31690829cfeSDominik Brodowski        ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
31790829cfeSDominik Brodowski 	   id->function);
31890829cfeSDominik Brodowski        ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
31990829cfeSDominik Brodowski 	   id->device_no);
32090829cfeSDominik Brodowski        ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
32190829cfeSDominik Brodowski        ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
32290829cfeSDominik Brodowski        ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
32390829cfeSDominik Brodowski        ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
32490829cfeSDominik Brodowski 
32590829cfeSDominik Brodowski        return 1;
32690829cfeSDominik Brodowski }
32790829cfeSDominik Brodowski 
32890829cfeSDominik Brodowski 
32990829cfeSDominik Brodowski 
3305e655772SJeff Mahoney static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
3315e655772SJeff Mahoney {
3325e655772SJeff Mahoney     char *tmp;
3335e655772SJeff Mahoney     sprintf (alias, "of:N%sT%sC%s",
3345e655772SJeff Mahoney                     of->name[0] ? of->name : "*",
3355e655772SJeff Mahoney                     of->type[0] ? of->type : "*",
3365e655772SJeff Mahoney                     of->compatible[0] ? of->compatible : "*");
3375e655772SJeff Mahoney 
3385e655772SJeff Mahoney     /* Replace all whitespace with underscores */
3395e655772SJeff Mahoney     for (tmp = alias; tmp && *tmp; tmp++)
3405e655772SJeff Mahoney         if (isspace (*tmp))
3415e655772SJeff Mahoney             *tmp = '_';
3425e655772SJeff Mahoney 
3435e655772SJeff Mahoney     return 1;
3445e655772SJeff Mahoney }
3455e655772SJeff Mahoney 
346fb120da6SStephen Rothwell static int do_vio_entry(const char *filename, struct vio_device_id *vio,
347fb120da6SStephen Rothwell 		char *alias)
348fb120da6SStephen Rothwell {
349fb120da6SStephen Rothwell 	char *tmp;
350fb120da6SStephen Rothwell 
351fb120da6SStephen Rothwell 	sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
352fb120da6SStephen Rothwell 			vio->compat[0] ? vio->compat : "*");
353fb120da6SStephen Rothwell 
354fb120da6SStephen Rothwell 	/* Replace all whitespace with underscores */
355fb120da6SStephen Rothwell 	for (tmp = alias; tmp && *tmp; tmp++)
356fb120da6SStephen Rothwell 		if (isspace (*tmp))
357fb120da6SStephen Rothwell 			*tmp = '_';
358fb120da6SStephen Rothwell 
359fb120da6SStephen Rothwell 	return 1;
360fb120da6SStephen Rothwell }
361fb120da6SStephen Rothwell 
362*a9d1b24dSGreg Kroah-Hartman static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias)
363*a9d1b24dSGreg Kroah-Hartman {
364*a9d1b24dSGreg Kroah-Hartman 	strcpy(alias, "i2c:");
365*a9d1b24dSGreg Kroah-Hartman 	ADD(alias, "id", 1, i2c->id);
366*a9d1b24dSGreg Kroah-Hartman 	return 1;
367*a9d1b24dSGreg Kroah-Hartman }
368*a9d1b24dSGreg Kroah-Hartman 
3691da177e4SLinus Torvalds /* Ignore any prefix, eg. v850 prepends _ */
3701da177e4SLinus Torvalds static inline int sym_is(const char *symbol, const char *name)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	const char *match;
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	match = strstr(symbol, name);
3751da177e4SLinus Torvalds 	if (!match)
3761da177e4SLinus Torvalds 		return 0;
3771da177e4SLinus Torvalds 	return match[strlen(symbol)] == '\0';
3781da177e4SLinus Torvalds }
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds static void do_table(void *symval, unsigned long size,
3811da177e4SLinus Torvalds 		     unsigned long id_size,
3821da177e4SLinus Torvalds 		     void *function,
3831da177e4SLinus Torvalds 		     struct module *mod)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	unsigned int i;
3861da177e4SLinus Torvalds 	char alias[500];
3871da177e4SLinus Torvalds 	int (*do_entry)(const char *, void *entry, char *alias) = function;
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	if (size % id_size || size < id_size) {
3901da177e4SLinus Torvalds 		fprintf(stderr, "*** Warning: %s ids %lu bad size "
3911da177e4SLinus Torvalds 			"(each on %lu)\n", mod->name, size, id_size);
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 	/* Leave last one: it's the terminator. */
3941da177e4SLinus Torvalds 	size -= id_size;
3951da177e4SLinus Torvalds 
3961da177e4SLinus Torvalds 	for (i = 0; i < size; i += id_size) {
3971da177e4SLinus Torvalds 		if (do_entry(mod->name, symval+i, alias)) {
3981da177e4SLinus Torvalds 			/* Always end in a wildcard, for future extension */
3991da177e4SLinus Torvalds 			if (alias[strlen(alias)-1] != '*')
4001da177e4SLinus Torvalds 				strcat(alias, "*");
4011da177e4SLinus Torvalds 			buf_printf(&mod->dev_table_buf,
4021da177e4SLinus Torvalds 				   "MODULE_ALIAS(\"%s\");\n", alias);
4031da177e4SLinus Torvalds 		}
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds }
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds /* Create MODULE_ALIAS() statements.
4081da177e4SLinus Torvalds  * At this time, we cannot write the actual output C source yet,
4091da177e4SLinus Torvalds  * so we write into the mod->dev_table_buf buffer. */
4101da177e4SLinus Torvalds void handle_moddevtable(struct module *mod, struct elf_info *info,
4111da177e4SLinus Torvalds 			Elf_Sym *sym, const char *symname)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	void *symval;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	/* We're looking for a section relative symbol */
4161da177e4SLinus Torvalds 	if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum)
4171da177e4SLinus Torvalds 		return;
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	symval = (void *)info->hdr
4201da177e4SLinus Torvalds 		+ info->sechdrs[sym->st_shndx].sh_offset
4211da177e4SLinus Torvalds 		+ sym->st_value;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	if (sym_is(symname, "__mod_pci_device_table"))
4241da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct pci_device_id),
4251da177e4SLinus Torvalds 			 do_pci_entry, mod);
4261da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_usb_device_table"))
427b19dcd93SRoman Kagan 		/* special case to handle bcdDevice ranges */
428b19dcd93SRoman Kagan 		do_usb_table(symval, sym->st_size, mod);
4291da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_ieee1394_device_table"))
4301da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id),
4311da177e4SLinus Torvalds 			 do_ieee1394_entry, mod);
4321da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_ccw_device_table"))
4331da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct ccw_device_id),
4341da177e4SLinus Torvalds 			 do_ccw_entry, mod);
4351da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_serio_device_table"))
4361da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct serio_device_id),
4371da177e4SLinus Torvalds 			 do_serio_entry, mod);
4381da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_pnp_device_table"))
4391da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct pnp_device_id),
4401da177e4SLinus Torvalds 			 do_pnp_entry, mod);
4411da177e4SLinus Torvalds 	else if (sym_is(symname, "__mod_pnp_card_device_table"))
4421da177e4SLinus Torvalds 		do_table(symval, sym->st_size, sizeof(struct pnp_card_device_id),
4431da177e4SLinus Torvalds 			 do_pnp_card_entry, mod);
44490829cfeSDominik Brodowski 	else if (sym_is(symname, "__mod_pcmcia_device_table"))
44590829cfeSDominik Brodowski 		do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id),
44690829cfeSDominik Brodowski 			 do_pcmcia_entry, mod);
4475e655772SJeff Mahoney         else if (sym_is(symname, "__mod_of_device_table"))
4485e655772SJeff Mahoney 		do_table(symval, sym->st_size, sizeof(struct of_device_id),
4495e655772SJeff Mahoney 			 do_of_entry, mod);
450fb120da6SStephen Rothwell         else if (sym_is(symname, "__mod_vio_device_table"))
451fb120da6SStephen Rothwell 		do_table(symval, sym->st_size, sizeof(struct vio_device_id),
452fb120da6SStephen Rothwell 			 do_vio_entry, mod);
453*a9d1b24dSGreg Kroah-Hartman 	else if (sym_is(symname, "__mod_i2c_device_table"))
454*a9d1b24dSGreg Kroah-Hartman 		do_table(symval, sym->st_size, sizeof(struct i2c_device_id),
455*a9d1b24dSGreg Kroah-Hartman 			 do_i2c_entry, mod);
4565e655772SJeff Mahoney 
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds /* Now add out buffered information to the generated C source */
4601da177e4SLinus Torvalds void add_moddevtable(struct buffer *buf, struct module *mod)
4611da177e4SLinus Torvalds {
4621da177e4SLinus Torvalds 	buf_printf(buf, "\n");
4631da177e4SLinus Torvalds 	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
4641da177e4SLinus Torvalds 	free(mod->dev_table_buf.p);
4651da177e4SLinus Torvalds }
466