Linux Audio

Check our new training course

Loading...
v3.5.6
   1/* Simple code to turn various tables in an ELF file into alias definitions.
   2 * This deals with kernel datastructures where they should be
   3 * dealt with: in the kernel source.
   4 *
   5 * Copyright 2002-2003  Rusty Russell, IBM Corporation
   6 *           2003       Kai Germaschewski
   7 *
   8 *
   9 * This software may be used and distributed according to the terms
  10 * of the GNU General Public License, incorporated herein by reference.
  11 */
  12
  13#include "modpost.h"
 
  14
  15/* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
  16 * use either stdint.h or inttypes.h for the rest. */
  17#if KERNEL_ELFCLASS == ELFCLASS32
  18typedef Elf32_Addr	kernel_ulong_t;
  19#define BITS_PER_LONG 32
  20#else
  21typedef Elf64_Addr	kernel_ulong_t;
  22#define BITS_PER_LONG 64
  23#endif
  24#ifdef __sun__
  25#include <inttypes.h>
  26#else
  27#include <stdint.h>
  28#endif
  29
  30#include <ctype.h>
  31#include <stdbool.h>
  32
  33typedef uint32_t	__u32;
  34typedef uint16_t	__u16;
  35typedef unsigned char	__u8;
  36
  37/* Big exception to the "don't include kernel headers into userspace, which
  38 * even potentially has different endianness and word sizes, since
  39 * we handle those differences explicitly below */
  40#include "../../include/linux/mod_devicetable.h"
  41
  42/* This array collects all instances that use the generic do_table */
  43struct devtable {
  44	const char *device_id; /* name of table, __mod_<name>_device_table. */
  45	unsigned long id_size;
  46	void *function;
  47};
  48
  49#define ___cat(a,b) a ## b
  50#define __cat(a,b) ___cat(a,b)
  51
  52/* we need some special handling for this host tool running eventually on
  53 * Darwin. The Mach-O section handling is a bit different than ELF section
  54 * handling. The differnces in detail are:
  55 *  a) we have segments which have sections
  56 *  b) we need a API call to get the respective section symbols */
  57#if defined(__MACH__)
  58#include <mach-o/getsect.h>
  59
  60#define INIT_SECTION(name)  do {					\
  61		unsigned long name ## _len;				\
  62		char *__cat(pstart_,name) = getsectdata("__TEXT",	\
  63			#name, &__cat(name,_len));			\
  64		char *__cat(pstop_,name) = __cat(pstart_,name) +	\
  65			__cat(name, _len);				\
  66		__cat(__start_,name) = (void *)__cat(pstart_,name);	\
  67		__cat(__stop_,name) = (void *)__cat(pstop_,name);	\
  68	} while (0)
  69#define SECTION(name)   __attribute__((section("__TEXT, " #name)))
  70
  71struct devtable **__start___devtable, **__stop___devtable;
  72#else
  73#define INIT_SECTION(name) /* no-op for ELF */
  74#define SECTION(name)   __attribute__((section(#name)))
  75
  76/* We construct a table of pointers in an ELF section (pointers generally
  77 * go unpadded by gcc).  ld creates boundary syms for us. */
  78extern struct devtable *__start___devtable[], *__stop___devtable[];
  79#endif /* __MACH__ */
  80
  81#if __GNUC__ == 3 && __GNUC_MINOR__ < 3
  82# define __used			__attribute__((__unused__))
  83#else
  84# define __used			__attribute__((__used__))
 
 
  85#endif
  86
 
 
 
 
 
 
 
 
 
 
 
 
  87/* Add a table entry.  We test function type matches while we're here. */
  88#define ADD_TO_DEVTABLE(device_id, type, function) \
  89	static struct devtable __cat(devtable,__LINE__) = {	\
  90		device_id + 0*sizeof((function)((const char *)NULL,	\
  91						(type *)NULL,		\
  92						(char *)NULL)),		\
  93		sizeof(type), (function) };				\
  94	static struct devtable *SECTION(__devtable) __used \
  95		__cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
  96
  97#define ADD(str, sep, cond, field)                              \
  98do {                                                            \
  99        strcat(str, sep);                                       \
 100        if (cond)                                               \
 101                sprintf(str + strlen(str),                      \
 102                        sizeof(field) == 1 ? "%02X" :           \
 103                        sizeof(field) == 2 ? "%04X" :           \
 104                        sizeof(field) == 4 ? "%08X" : "",       \
 105                        field);                                 \
 106        else                                                    \
 107                sprintf(str + strlen(str), "*");                \
 108} while(0)
 109
 110/* Always end in a wildcard, for future extension */
 111static inline void add_wildcard(char *str)
 112{
 113	int len = strlen(str);
 114
 115	if (str[len - 1] != '*')
 116		strcat(str + len, "*");
 117}
 118
 119unsigned int cross_build = 0;
 120/**
 121 * Check that sizeof(device_id type) are consistent with size of section
 122 * in .o file. If in-consistent then userspace and kernel does not agree
 123 * on actual size which is a bug.
 124 * Also verify that the final entry in the table is all zeros.
 125 * Ignore both checks if build host differ from target host and size differs.
 126 **/
 127static void device_id_check(const char *modname, const char *device_id,
 128			    unsigned long size, unsigned long id_size,
 129			    void *symval)
 130{
 131	int i;
 132
 133	if (size % id_size || size < id_size) {
 134		if (cross_build != 0)
 135			return;
 136		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
 137		      "of the size of section __mod_%s_device_table=%lu.\n"
 
 138		      "Fix definition of struct %s_device_id "
 139		      "in mod_devicetable.h\n",
 140		      modname, device_id, id_size, device_id, size, device_id);
 141	}
 142	/* Verify last one is a terminator */
 143	for (i = 0; i < id_size; i++ ) {
 144		if (*(uint8_t*)(symval+size-id_size+i)) {
 145			fprintf(stderr,"%s: struct %s_device_id is %lu bytes.  "
 146				"The last of %lu is:\n",
 147				modname, device_id, id_size, size / id_size);
 148			for (i = 0; i < id_size; i++ )
 149				fprintf(stderr,"0x%02x ",
 150					*(uint8_t*)(symval+size-id_size+i) );
 151			fprintf(stderr,"\n");
 152			fatal("%s: struct %s_device_id is not terminated "
 153				"with a NULL entry!\n", modname, device_id);
 154		}
 155	}
 156}
 157
 158/* USB is special because the bcdDevice can be matched against a numeric range */
 159/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
 160static void do_usb_entry(struct usb_device_id *id,
 161			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
 162			 unsigned char range_lo, unsigned char range_hi,
 163			 unsigned char max, struct module *mod)
 164{
 165	char alias[500];
 
 
 
 
 
 
 
 
 
 
 
 
 166	strcpy(alias, "usb:");
 167	ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
 168	    id->idVendor);
 169	ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
 170	    id->idProduct);
 171
 172	strcat(alias, "d");
 173	if (bcdDevice_initial_digits)
 174		sprintf(alias + strlen(alias), "%0*X",
 175			bcdDevice_initial_digits, bcdDevice_initial);
 176	if (range_lo == range_hi)
 177		sprintf(alias + strlen(alias), "%X", range_lo);
 178	else if (range_lo > 0 || range_hi < max) {
 179		if (range_lo > 0x9 || range_hi < 0xA)
 180			sprintf(alias + strlen(alias),
 181				"[%X-%X]",
 182				range_lo,
 183				range_hi);
 184		else {
 185			sprintf(alias + strlen(alias),
 186				range_lo < 0x9 ? "[%X-9" : "[%X",
 187				range_lo);
 188			sprintf(alias + strlen(alias),
 189				range_hi > 0xA ? "a-%X]" : "%X]",
 190				range_lo);
 191		}
 192	}
 193	if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
 194		strcat(alias, "*");
 195
 196	ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
 197	    id->bDeviceClass);
 198	ADD(alias, "dsc",
 199	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
 200	    id->bDeviceSubClass);
 201	ADD(alias, "dp",
 202	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
 203	    id->bDeviceProtocol);
 204	ADD(alias, "ic",
 205	    id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
 206	    id->bInterfaceClass);
 207	ADD(alias, "isc",
 208	    id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
 209	    id->bInterfaceSubClass);
 210	ADD(alias, "ip",
 211	    id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
 212	    id->bInterfaceProtocol);
 213
 214	add_wildcard(alias);
 215	buf_printf(&mod->dev_table_buf,
 216		   "MODULE_ALIAS(\"%s\");\n", alias);
 217}
 218
 219/* Handles increment/decrement of BCD formatted integers */
 220/* Returns the previous value, so it works like i++ or i-- */
 221static unsigned int incbcd(unsigned int *bcd,
 222			   int inc,
 223			   unsigned char max,
 224			   size_t chars)
 225{
 226	unsigned int init = *bcd, i, j;
 227	unsigned long long c, dec = 0;
 228
 229	/* If bcd is not in BCD format, just increment */
 230	if (max > 0x9) {
 231		*bcd += inc;
 232		return init;
 233	}
 234
 235	/* Convert BCD to Decimal */
 236	for (i=0 ; i < chars ; i++) {
 237		c = (*bcd >> (i << 2)) & 0xf;
 238		c = c > 9 ? 9 : c; /* force to bcd just in case */
 239		for (j=0 ; j < i ; j++)
 240			c = c * 10;
 241		dec += c;
 242	}
 243
 244	/* Do our increment/decrement */
 245	dec += inc;
 246	*bcd  = 0;
 247
 248	/* Convert back to BCD */
 249	for (i=0 ; i < chars ; i++) {
 250		for (c=1,j=0 ; j < i ; j++)
 251			c = c * 10;
 252		c = (dec / c) % 10;
 253		*bcd += c << (i << 2);
 254	}
 255	return init;
 256}
 257
 258static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
 259{
 260	unsigned int devlo, devhi;
 261	unsigned char chi, clo, max;
 262	int ndigits;
 263
 264	id->match_flags = TO_NATIVE(id->match_flags);
 265	id->idVendor = TO_NATIVE(id->idVendor);
 266	id->idProduct = TO_NATIVE(id->idProduct);
 267
 268	devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
 269		TO_NATIVE(id->bcdDevice_lo) : 0x0U;
 270	devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
 271		TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
 
 
 
 
 272
 273	/* Figure out if this entry is in bcd or hex format */
 274	max = 0x9; /* Default to decimal format */
 275	for (ndigits = 0 ; ndigits < sizeof(id->bcdDevice_lo) * 2 ; ndigits++) {
 276		clo = (devlo >> (ndigits << 2)) & 0xf;
 277		chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
 278		if (clo > max || chi > max) {
 279			max = 0xf;
 280			break;
 281		}
 282	}
 283
 284	/*
 285	 * Some modules (visor) have empty slots as placeholder for
 286	 * run-time specification that results in catch-all alias
 287	 */
 288	if (!(id->idVendor | id->idProduct | id->bDeviceClass | id->bInterfaceClass))
 289		return;
 290
 291	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
 292	for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
 293		clo = devlo & 0xf;
 294		chi = devhi & 0xf;
 295		if (chi > max)	/* If we are in bcd mode, truncate if necessary */
 296			chi = max;
 297		devlo >>= 4;
 298		devhi >>= 4;
 299
 300		if (devlo == devhi || !ndigits) {
 301			do_usb_entry(id, devlo, ndigits, clo, chi, max, mod);
 302			break;
 303		}
 304
 305		if (clo > 0x0)
 306			do_usb_entry(id,
 307				     incbcd(&devlo, 1, max,
 308					    sizeof(id->bcdDevice_lo) * 2),
 309				     ndigits, clo, max, max, mod);
 310
 311		if (chi < max)
 312			do_usb_entry(id,
 313				     incbcd(&devhi, -1, max,
 314					    sizeof(id->bcdDevice_lo) * 2),
 315				     ndigits, 0x0, chi, max, mod);
 316	}
 317}
 318
 319static void do_usb_table(void *symval, unsigned long size,
 320			 struct module *mod)
 321{
 322	unsigned int i;
 323	const unsigned long id_size = sizeof(struct usb_device_id);
 324
 325	device_id_check(mod->name, "usb", size, id_size, symval);
 326
 327	/* Leave last one: it's the terminator. */
 328	size -= id_size;
 329
 330	for (i = 0; i < size; i += id_size)
 331		do_usb_entry_multi(symval + i, mod);
 332}
 333
 334/* Looks like: hid:bNvNpN */
 335static int do_hid_entry(const char *filename,
 336			     struct hid_device_id *id, char *alias)
 337{
 338	id->bus = TO_NATIVE(id->bus);
 339	id->group = TO_NATIVE(id->group);
 340	id->vendor = TO_NATIVE(id->vendor);
 341	id->product = TO_NATIVE(id->product);
 342
 343	sprintf(alias, "hid:");
 344	ADD(alias, "b", id->bus != HID_BUS_ANY, id->bus);
 345	ADD(alias, "g", id->group != HID_GROUP_ANY, id->group);
 346	ADD(alias, "v", id->vendor != HID_ANY_ID, id->vendor);
 347	ADD(alias, "p", id->product != HID_ANY_ID, id->product);
 348
 349	return 1;
 350}
 351ADD_TO_DEVTABLE("hid", struct hid_device_id, do_hid_entry);
 352
 353/* Looks like: ieee1394:venNmoNspNverN */
 354static int do_ieee1394_entry(const char *filename,
 355			     struct ieee1394_device_id *id, char *alias)
 356{
 357	id->match_flags = TO_NATIVE(id->match_flags);
 358	id->vendor_id = TO_NATIVE(id->vendor_id);
 359	id->model_id = TO_NATIVE(id->model_id);
 360	id->specifier_id = TO_NATIVE(id->specifier_id);
 361	id->version = TO_NATIVE(id->version);
 362
 363	strcpy(alias, "ieee1394:");
 364	ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
 365	    id->vendor_id);
 366	ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
 367	    id->model_id);
 368	ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
 369	    id->specifier_id);
 370	ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
 371	    id->version);
 372
 373	add_wildcard(alias);
 374	return 1;
 375}
 376ADD_TO_DEVTABLE("ieee1394", struct ieee1394_device_id, do_ieee1394_entry);
 377
 378/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
 379static int do_pci_entry(const char *filename,
 380			struct pci_device_id *id, char *alias)
 381{
 382	/* Class field can be divided into these three. */
 383	unsigned char baseclass, subclass, interface,
 384		baseclass_mask, subclass_mask, interface_mask;
 385
 386	id->vendor = TO_NATIVE(id->vendor);
 387	id->device = TO_NATIVE(id->device);
 388	id->subvendor = TO_NATIVE(id->subvendor);
 389	id->subdevice = TO_NATIVE(id->subdevice);
 390	id->class = TO_NATIVE(id->class);
 391	id->class_mask = TO_NATIVE(id->class_mask);
 392
 393	strcpy(alias, "pci:");
 394	ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
 395	ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
 396	ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
 397	ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
 398
 399	baseclass = (id->class) >> 16;
 400	baseclass_mask = (id->class_mask) >> 16;
 401	subclass = (id->class) >> 8;
 402	subclass_mask = (id->class_mask) >> 8;
 403	interface = id->class;
 404	interface_mask = id->class_mask;
 405
 406	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
 407	    || (subclass_mask != 0 && subclass_mask != 0xFF)
 408	    || (interface_mask != 0 && interface_mask != 0xFF)) {
 409		warn("Can't handle masks in %s:%04X\n",
 410		     filename, id->class_mask);
 411		return 0;
 412	}
 413
 414	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
 415	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
 416	ADD(alias, "i", interface_mask == 0xFF, interface);
 417	add_wildcard(alias);
 418	return 1;
 419}
 420ADD_TO_DEVTABLE("pci", struct pci_device_id, do_pci_entry);
 421
 422/* looks like: "ccw:tNmNdtNdmN" */
 423static int do_ccw_entry(const char *filename,
 424			struct ccw_device_id *id, char *alias)
 425{
 426	id->match_flags = TO_NATIVE(id->match_flags);
 427	id->cu_type = TO_NATIVE(id->cu_type);
 428	id->cu_model = TO_NATIVE(id->cu_model);
 429	id->dev_type = TO_NATIVE(id->dev_type);
 430	id->dev_model = TO_NATIVE(id->dev_model);
 431
 432	strcpy(alias, "ccw:");
 433	ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
 434	    id->cu_type);
 435	ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
 436	    id->cu_model);
 437	ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
 438	    id->dev_type);
 439	ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
 440	    id->dev_model);
 441	add_wildcard(alias);
 442	return 1;
 443}
 444ADD_TO_DEVTABLE("ccw", struct ccw_device_id, do_ccw_entry);
 445
 446/* looks like: "ap:tN" */
 447static int do_ap_entry(const char *filename,
 448		       struct ap_device_id *id, char *alias)
 449{
 450	sprintf(alias, "ap:t%02X*", id->dev_type);
 
 
 451	return 1;
 452}
 453ADD_TO_DEVTABLE("ap", struct ap_device_id, do_ap_entry);
 454
 455/* looks like: "css:tN" */
 456static int do_css_entry(const char *filename,
 457			struct css_device_id *id, char *alias)
 458{
 459	sprintf(alias, "css:t%01X", id->type);
 
 
 460	return 1;
 461}
 462ADD_TO_DEVTABLE("css", struct css_device_id, do_css_entry);
 463
 464/* Looks like: "serio:tyNprNidNexN" */
 465static int do_serio_entry(const char *filename,
 466			  struct serio_device_id *id, char *alias)
 467{
 468	id->type = TO_NATIVE(id->type);
 469	id->proto = TO_NATIVE(id->proto);
 470	id->id = TO_NATIVE(id->id);
 471	id->extra = TO_NATIVE(id->extra);
 472
 473	strcpy(alias, "serio:");
 474	ADD(alias, "ty", id->type != SERIO_ANY, id->type);
 475	ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
 476	ADD(alias, "id", id->id != SERIO_ANY, id->id);
 477	ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
 478
 479	add_wildcard(alias);
 480	return 1;
 481}
 482ADD_TO_DEVTABLE("serio", struct serio_device_id, do_serio_entry);
 483
 484/* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
 485static int do_acpi_entry(const char *filename,
 486			struct acpi_device_id *id, char *alias)
 487{
 488	sprintf(alias, "acpi*:%s:*", id->id);
 
 489	return 1;
 490}
 491ADD_TO_DEVTABLE("acpi", struct acpi_device_id, do_acpi_entry);
 492
 493/* looks like: "pnp:dD" */
 494static void do_pnp_device_entry(void *symval, unsigned long size,
 495				struct module *mod)
 496{
 497	const unsigned long id_size = sizeof(struct pnp_device_id);
 498	const unsigned int count = (size / id_size)-1;
 499	const struct pnp_device_id *devs = symval;
 500	unsigned int i;
 501
 502	device_id_check(mod->name, "pnp", size, id_size, symval);
 503
 504	for (i = 0; i < count; i++) {
 505		const char *id = (char *)devs[i].id;
 506		char acpi_id[sizeof(devs[0].id)];
 507		int j;
 508
 509		buf_printf(&mod->dev_table_buf,
 510			   "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
 511
 512		/* fix broken pnp bus lowercasing */
 513		for (j = 0; j < sizeof(acpi_id); j++)
 514			acpi_id[j] = toupper(id[j]);
 515		buf_printf(&mod->dev_table_buf,
 516			   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
 517	}
 518}
 519
 520/* looks like: "pnp:dD" for every device of the card */
 521static void do_pnp_card_entries(void *symval, unsigned long size,
 522				struct module *mod)
 523{
 524	const unsigned long id_size = sizeof(struct pnp_card_device_id);
 525	const unsigned int count = (size / id_size)-1;
 526	const struct pnp_card_device_id *cards = symval;
 527	unsigned int i;
 528
 529	device_id_check(mod->name, "pnp", size, id_size, symval);
 530
 531	for (i = 0; i < count; i++) {
 532		unsigned int j;
 533		const struct pnp_card_device_id *card = &cards[i];
 534
 535		for (j = 0; j < PNP_MAX_DEVICES; j++) {
 536			const char *id = (char *)card->devs[j].id;
 537			int i2, j2;
 538			int dup = 0;
 539
 540			if (!id[0])
 541				break;
 542
 543			/* find duplicate, already added value */
 544			for (i2 = 0; i2 < i && !dup; i2++) {
 545				const struct pnp_card_device_id *card2 = &cards[i2];
 546
 547				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
 548					const char *id2 = (char *)card2->devs[j2].id;
 549
 550					if (!id2[0])
 551						break;
 552
 553					if (!strcmp(id, id2)) {
 554						dup = 1;
 555						break;
 556					}
 557				}
 558			}
 559
 560			/* add an individual alias for every device entry */
 561			if (!dup) {
 562				char acpi_id[sizeof(card->devs[0].id)];
 563				int k;
 564
 565				buf_printf(&mod->dev_table_buf,
 566					   "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
 567
 568				/* fix broken pnp bus lowercasing */
 569				for (k = 0; k < sizeof(acpi_id); k++)
 570					acpi_id[k] = toupper(id[k]);
 571				buf_printf(&mod->dev_table_buf,
 572					   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
 573			}
 574		}
 575	}
 576}
 577
 578/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
 579static int do_pcmcia_entry(const char *filename,
 580			   struct pcmcia_device_id *id, char *alias)
 581{
 582	unsigned int i;
 583
 584	id->match_flags = TO_NATIVE(id->match_flags);
 585	id->manf_id = TO_NATIVE(id->manf_id);
 586	id->card_id = TO_NATIVE(id->card_id);
 587	id->func_id = TO_NATIVE(id->func_id);
 588	id->function = TO_NATIVE(id->function);
 589	id->device_no = TO_NATIVE(id->device_no);
 590
 591	for (i=0; i<4; i++) {
 592		id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
 593       }
 594
 595       strcpy(alias, "pcmcia:");
 596       ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
 597	   id->manf_id);
 598       ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
 599	   id->card_id);
 600       ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
 601	   id->func_id);
 602       ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
 603	   id->function);
 604       ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
 605	   id->device_no);
 606       ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
 607       ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
 608       ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
 609       ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
 610
 611	add_wildcard(alias);
 612       return 1;
 613}
 614ADD_TO_DEVTABLE("pcmcia", struct pcmcia_device_id, do_pcmcia_entry);
 615
 616static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
 617{
 618    int len;
 619    char *tmp;
 
 
 
 
 620    len = sprintf (alias, "of:N%sT%s",
 621                    of->name[0] ? of->name : "*",
 622                    of->type[0] ? of->type : "*");
 623
 624    if (of->compatible[0])
 625        sprintf (&alias[len], "%sC%s",
 626                     of->type[0] ? "*" : "",
 627                     of->compatible);
 628
 629    /* Replace all whitespace with underscores */
 630    for (tmp = alias; tmp && *tmp; tmp++)
 631        if (isspace (*tmp))
 632            *tmp = '_';
 633
 634    add_wildcard(alias);
 635    return 1;
 636}
 637ADD_TO_DEVTABLE("of", struct of_device_id, do_of_entry);
 638
 639static int do_vio_entry(const char *filename, struct vio_device_id *vio,
 640		char *alias)
 641{
 642	char *tmp;
 
 
 643
 644	sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
 645			vio->compat[0] ? vio->compat : "*");
 646
 647	/* Replace all whitespace with underscores */
 648	for (tmp = alias; tmp && *tmp; tmp++)
 649		if (isspace (*tmp))
 650			*tmp = '_';
 651
 652	add_wildcard(alias);
 653	return 1;
 654}
 655ADD_TO_DEVTABLE("vio", struct vio_device_id, do_vio_entry);
 656
 657#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 658
 659static void do_input(char *alias,
 660		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
 661{
 662	unsigned int i;
 663
 
 
 664	for (i = min; i < max; i++)
 665		if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
 666			sprintf(alias + strlen(alias), "%X,*", i);
 667}
 668
 669/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
 670static int do_input_entry(const char *filename, struct input_device_id *id,
 671			  char *alias)
 672{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 673	sprintf(alias, "input:");
 674
 675	ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
 676	ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
 677	ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
 678	ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
 679
 680	sprintf(alias + strlen(alias), "-e*");
 681	if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
 682		do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX);
 683	sprintf(alias + strlen(alias), "k*");
 684	if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
 685		do_input(alias, id->keybit,
 686			 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
 687			 INPUT_DEVICE_ID_KEY_MAX);
 688	sprintf(alias + strlen(alias), "r*");
 689	if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
 690		do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX);
 691	sprintf(alias + strlen(alias), "a*");
 692	if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
 693		do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
 694	sprintf(alias + strlen(alias), "m*");
 695	if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
 696		do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
 697	sprintf(alias + strlen(alias), "l*");
 698	if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
 699		do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
 700	sprintf(alias + strlen(alias), "s*");
 701	if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
 702		do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
 703	sprintf(alias + strlen(alias), "f*");
 704	if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
 705		do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
 706	sprintf(alias + strlen(alias), "w*");
 707	if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
 708		do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX);
 709	return 1;
 710}
 711ADD_TO_DEVTABLE("input", struct input_device_id, do_input_entry);
 712
 713static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
 714		char *alias)
 715{
 716	if (eisa->sig[0])
 717		sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
 
 718	else
 719		strcat(alias, "*");
 720	return 1;
 721}
 722ADD_TO_DEVTABLE("eisa", struct eisa_device_id, do_eisa_entry);
 723
 724/* Looks like: parisc:tNhvNrevNsvN */
 725static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
 726		char *alias)
 727{
 728	id->hw_type = TO_NATIVE(id->hw_type);
 729	id->hversion = TO_NATIVE(id->hversion);
 730	id->hversion_rev = TO_NATIVE(id->hversion_rev);
 731	id->sversion = TO_NATIVE(id->sversion);
 732
 733	strcpy(alias, "parisc:");
 734	ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type);
 735	ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion);
 736	ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev);
 737	ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion);
 738
 739	add_wildcard(alias);
 740	return 1;
 741}
 742ADD_TO_DEVTABLE("parisc", struct parisc_device_id, do_parisc_entry);
 743
 744/* Looks like: sdio:cNvNdN. */
 745static int do_sdio_entry(const char *filename,
 746			struct sdio_device_id *id, char *alias)
 747{
 748	id->class = TO_NATIVE(id->class);
 749	id->vendor = TO_NATIVE(id->vendor);
 750	id->device = TO_NATIVE(id->device);
 751
 752	strcpy(alias, "sdio:");
 753	ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class);
 754	ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor);
 755	ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device);
 756	add_wildcard(alias);
 757	return 1;
 758}
 759ADD_TO_DEVTABLE("sdio", struct sdio_device_id, do_sdio_entry);
 760
 761/* Looks like: ssb:vNidNrevN. */
 762static int do_ssb_entry(const char *filename,
 763			struct ssb_device_id *id, char *alias)
 764{
 765	id->vendor = TO_NATIVE(id->vendor);
 766	id->coreid = TO_NATIVE(id->coreid);
 767	id->revision = TO_NATIVE(id->revision);
 768
 769	strcpy(alias, "ssb:");
 770	ADD(alias, "v", id->vendor != SSB_ANY_VENDOR, id->vendor);
 771	ADD(alias, "id", id->coreid != SSB_ANY_ID, id->coreid);
 772	ADD(alias, "rev", id->revision != SSB_ANY_REV, id->revision);
 773	add_wildcard(alias);
 774	return 1;
 775}
 776ADD_TO_DEVTABLE("ssb", struct ssb_device_id, do_ssb_entry);
 777
 778/* Looks like: bcma:mNidNrevNclN. */
 779static int do_bcma_entry(const char *filename,
 780			 struct bcma_device_id *id, char *alias)
 781{
 782	id->manuf = TO_NATIVE(id->manuf);
 783	id->id = TO_NATIVE(id->id);
 784	id->rev = TO_NATIVE(id->rev);
 785	id->class = TO_NATIVE(id->class);
 786
 787	strcpy(alias, "bcma:");
 788	ADD(alias, "m", id->manuf != BCMA_ANY_MANUF, id->manuf);
 789	ADD(alias, "id", id->id != BCMA_ANY_ID, id->id);
 790	ADD(alias, "rev", id->rev != BCMA_ANY_REV, id->rev);
 791	ADD(alias, "cl", id->class != BCMA_ANY_CLASS, id->class);
 792	add_wildcard(alias);
 793	return 1;
 794}
 795ADD_TO_DEVTABLE("bcma", struct bcma_device_id, do_bcma_entry);
 796
 797/* Looks like: virtio:dNvN */
 798static int do_virtio_entry(const char *filename, struct virtio_device_id *id,
 799			   char *alias)
 800{
 801	id->device = TO_NATIVE(id->device);
 802	id->vendor = TO_NATIVE(id->vendor);
 803
 804	strcpy(alias, "virtio:");
 805	ADD(alias, "d", id->device != VIRTIO_DEV_ANY_ID, id->device);
 806	ADD(alias, "v", id->vendor != VIRTIO_DEV_ANY_ID, id->vendor);
 807
 808	add_wildcard(alias);
 809	return 1;
 810}
 811ADD_TO_DEVTABLE("virtio", struct virtio_device_id, do_virtio_entry);
 812
 813/*
 814 * Looks like: vmbus:guid
 815 * Each byte of the guid will be represented by two hex characters
 816 * in the name.
 817 */
 818
 819static int do_vmbus_entry(const char *filename, struct hv_vmbus_device_id *id,
 820			  char *alias)
 821{
 822	int i;
 823	char guid_name[((sizeof(id->guid) + 1)) * 2];
 
 824
 825	for (i = 0; i < (sizeof(id->guid) * 2); i += 2)
 826		sprintf(&guid_name[i], "%02x", id->guid[i/2]);
 827
 828	strcpy(alias, "vmbus:");
 829	strcat(alias, guid_name);
 830
 831	return 1;
 832}
 833ADD_TO_DEVTABLE("vmbus", struct hv_vmbus_device_id, do_vmbus_entry);
 834
 835/* Looks like: i2c:S */
 836static int do_i2c_entry(const char *filename, struct i2c_device_id *id,
 837			char *alias)
 838{
 839	sprintf(alias, I2C_MODULE_PREFIX "%s", id->name);
 
 840
 841	return 1;
 842}
 843ADD_TO_DEVTABLE("i2c", struct i2c_device_id, do_i2c_entry);
 844
 845/* Looks like: spi:S */
 846static int do_spi_entry(const char *filename, struct spi_device_id *id,
 847			char *alias)
 848{
 849	sprintf(alias, SPI_MODULE_PREFIX "%s", id->name);
 
 850
 851	return 1;
 852}
 853ADD_TO_DEVTABLE("spi", struct spi_device_id, do_spi_entry);
 854
 855static const struct dmifield {
 856	const char *prefix;
 857	int field;
 858} dmi_fields[] = {
 859	{ "bvn", DMI_BIOS_VENDOR },
 860	{ "bvr", DMI_BIOS_VERSION },
 861	{ "bd",  DMI_BIOS_DATE },
 862	{ "svn", DMI_SYS_VENDOR },
 863	{ "pn",  DMI_PRODUCT_NAME },
 864	{ "pvr", DMI_PRODUCT_VERSION },
 865	{ "rvn", DMI_BOARD_VENDOR },
 866	{ "rn",  DMI_BOARD_NAME },
 867	{ "rvr", DMI_BOARD_VERSION },
 868	{ "cvn", DMI_CHASSIS_VENDOR },
 869	{ "ct",  DMI_CHASSIS_TYPE },
 870	{ "cvr", DMI_CHASSIS_VERSION },
 871	{ NULL,  DMI_NONE }
 872};
 873
 874static void dmi_ascii_filter(char *d, const char *s)
 875{
 876	/* Filter out characters we don't want to see in the modalias string */
 877	for (; *s; s++)
 878		if (*s > ' ' && *s < 127 && *s != ':')
 879			*(d++) = *s;
 880
 881	*d = 0;
 882}
 883
 884
 885static int do_dmi_entry(const char *filename, struct dmi_system_id *id,
 886			char *alias)
 887{
 888	int i, j;
 889
 890	sprintf(alias, "dmi*");
 891
 892	for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
 893		for (j = 0; j < 4; j++) {
 894			if (id->matches[j].slot &&
 895			    id->matches[j].slot == dmi_fields[i].field) {
 896				sprintf(alias + strlen(alias), ":%s*",
 897					dmi_fields[i].prefix);
 898				dmi_ascii_filter(alias + strlen(alias),
 899						 id->matches[j].substr);
 900				strcat(alias, "*");
 901			}
 902		}
 903	}
 904
 905	strcat(alias, ":");
 906	return 1;
 907}
 908ADD_TO_DEVTABLE("dmi", struct dmi_system_id, do_dmi_entry);
 909
 910static int do_platform_entry(const char *filename,
 911			     struct platform_device_id *id, char *alias)
 912{
 913	sprintf(alias, PLATFORM_MODULE_PREFIX "%s", id->name);
 
 914	return 1;
 915}
 916ADD_TO_DEVTABLE("platform", struct platform_device_id, do_platform_entry);
 917
 918static int do_mdio_entry(const char *filename,
 919			 struct mdio_device_id *id, char *alias)
 920{
 921	int i;
 
 
 922
 923	alias += sprintf(alias, MDIO_MODULE_PREFIX);
 924
 925	for (i = 0; i < 32; i++) {
 926		if (!((id->phy_id_mask >> (31-i)) & 1))
 927			*(alias++) = '?';
 928		else if ((id->phy_id >> (31-i)) & 1)
 929			*(alias++) = '1';
 930		else
 931			*(alias++) = '0';
 932	}
 933
 934	/* Terminate the string */
 935	*alias = 0;
 936
 937	return 1;
 938}
 939ADD_TO_DEVTABLE("mdio", struct mdio_device_id, do_mdio_entry);
 940
 941/* Looks like: zorro:iN. */
 942static int do_zorro_entry(const char *filename, struct zorro_device_id *id,
 943			  char *alias)
 944{
 945	id->id = TO_NATIVE(id->id);
 946	strcpy(alias, "zorro:");
 947	ADD(alias, "i", id->id != ZORRO_WILDCARD, id->id);
 948	return 1;
 949}
 950ADD_TO_DEVTABLE("zorro", struct zorro_device_id, do_zorro_entry);
 951
 952/* looks like: "pnp:dD" */
 953static int do_isapnp_entry(const char *filename,
 954			   struct isapnp_device_id *id, char *alias)
 955{
 
 
 956	sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
 957		'A' + ((id->vendor >> 2) & 0x3f) - 1,
 958		'A' + (((id->vendor & 3) << 3) | ((id->vendor >> 13) & 7)) - 1,
 959		'A' + ((id->vendor >> 8) & 0x1f) - 1,
 960		(id->function >> 4) & 0x0f, id->function & 0x0f,
 961		(id->function >> 12) & 0x0f, (id->function >> 8) & 0x0f);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 962	return 1;
 963}
 964ADD_TO_DEVTABLE("isapnp", struct isapnp_device_id, do_isapnp_entry);
 965
 966/*
 967 * Append a match expression for a single masked hex digit.
 968 * outp points to a pointer to the character at which to append.
 969 *	*outp is updated on return to point just after the appended text,
 970 *	to facilitate further appending.
 971 */
 972static void append_nibble_mask(char **outp,
 973			       unsigned int nibble, unsigned int mask)
 974{
 975	char *p = *outp;
 976	unsigned int i;
 977
 978	switch (mask) {
 979	case 0:
 980		*p++ = '?';
 981		break;
 982
 983	case 0xf:
 984		p += sprintf(p, "%X",  nibble);
 985		break;
 986
 987	default:
 988		/*
 989		 * Dumbly emit a match pattern for all possible matching
 990		 * digits.  This could be improved in some cases using ranges,
 991		 * but it has the advantage of being trivially correct, and is
 992		 * often optimal.
 993		 */
 994		*p++ = '[';
 995		for (i = 0; i < 0x10; i++)
 996			if ((i & mask) == nibble)
 997				p += sprintf(p, "%X", i);
 998		*p++ = ']';
 999	}
1000
1001	/* Ensure that the string remains NUL-terminated: */
1002	*p = '\0';
1003
1004	/* Advance the caller's end-of-string pointer: */
1005	*outp = p;
1006}
1007
1008/*
1009 * looks like: "amba:dN"
1010 *
1011 * N is exactly 8 digits, where each is an upper-case hex digit, or
1012 *	a ? or [] pattern matching exactly one digit.
1013 */
1014static int do_amba_entry(const char *filename,
1015			 struct amba_id *id, char *alias)
1016{
1017	unsigned int digit;
1018	char *p = alias;
 
 
1019
1020	if ((id->id & id->mask) != id->id)
1021		fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1022		      "id=0x%08X, mask=0x%08X.  Please fix this driver.\n",
1023		      filename, id->id, id->mask);
1024
1025	p += sprintf(alias, "amba:d");
1026	for (digit = 0; digit < 8; digit++)
1027		append_nibble_mask(&p,
1028				   (id->id >> (4 * (7 - digit))) & 0xf,
1029				   (id->mask >> (4 * (7 - digit))) & 0xf);
1030
1031	return 1;
1032}
1033ADD_TO_DEVTABLE("amba", struct amba_id, do_amba_entry);
1034
1035/* LOOKS like x86cpu:vendor:VVVV:family:FFFF:model:MMMM:feature:*,FEAT,*
1036 * All fields are numbers. It would be nicer to use strings for vendor
1037 * and feature, but getting those out of the build system here is too
1038 * complicated.
1039 */
1040
1041static int do_x86cpu_entry(const char *filename, struct x86_cpu_id *id,
1042			   char *alias)
1043{
1044	id->feature = TO_NATIVE(id->feature);
1045	id->family = TO_NATIVE(id->family);
1046	id->model = TO_NATIVE(id->model);
1047	id->vendor = TO_NATIVE(id->vendor);
1048
1049	strcpy(alias, "x86cpu:");
1050	ADD(alias, "vendor:",  id->vendor != X86_VENDOR_ANY, id->vendor);
1051	ADD(alias, ":family:", id->family != X86_FAMILY_ANY, id->family);
1052	ADD(alias, ":model:",  id->model  != X86_MODEL_ANY,  id->model);
1053	strcat(alias, ":feature:*");
1054	if (id->feature != X86_FEATURE_ANY)
1055		sprintf(alias + strlen(alias), "%04X*", id->feature);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1056	return 1;
1057}
1058ADD_TO_DEVTABLE("x86cpu", struct x86_cpu_id, do_x86cpu_entry);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1059
1060/* Does namelen bytes of name exactly match the symbol? */
1061static bool sym_is(const char *name, unsigned namelen, const char *symbol)
1062{
1063	if (namelen != strlen(symbol))
1064		return false;
1065
1066	return memcmp(name, symbol, namelen) == 0;
1067}
1068
1069static void do_table(void *symval, unsigned long size,
1070		     unsigned long id_size,
1071		     const char *device_id,
1072		     void *function,
1073		     struct module *mod)
1074{
1075	unsigned int i;
1076	char alias[500];
1077	int (*do_entry)(const char *, void *entry, char *alias) = function;
1078
1079	device_id_check(mod->name, device_id, size, id_size, symval);
1080	/* Leave last one: it's the terminator. */
1081	size -= id_size;
1082
1083	for (i = 0; i < size; i += id_size) {
1084		if (do_entry(mod->name, symval+i, alias)) {
1085			buf_printf(&mod->dev_table_buf,
1086				   "MODULE_ALIAS(\"%s\");\n", alias);
1087		}
1088	}
1089}
1090
1091/* Create MODULE_ALIAS() statements.
1092 * At this time, we cannot write the actual output C source yet,
1093 * so we write into the mod->dev_table_buf buffer. */
1094void handle_moddevtable(struct module *mod, struct elf_info *info,
1095			Elf_Sym *sym, const char *symname)
1096{
1097	void *symval;
1098	char *zeros = NULL;
1099	const char *name;
1100	unsigned int namelen;
1101
1102	/* We're looking for a section relative symbol */
1103	if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
1104		return;
1105
1106	/* We're looking for an object */
1107	if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1108		return;
1109
1110	/* All our symbols are of form <prefix>__mod_XXX_device_table. */
1111	name = strstr(symname, "__mod_");
1112	if (!name)
1113		return;
1114	name += strlen("__mod_");
1115	namelen = strlen(name);
1116	if (namelen < strlen("_device_table"))
1117		return;
1118	if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1119		return;
1120	namelen -= strlen("_device_table");
 
 
 
1121
1122	/* Handle all-NULL symbols allocated into .bss */
1123	if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
1124		zeros = calloc(1, sym->st_size);
1125		symval = zeros;
1126	} else {
1127		symval = (void *)info->hdr
1128			+ info->sechdrs[get_secindex(info, sym)].sh_offset
1129			+ sym->st_value;
1130	}
1131
1132	/* First handle the "special" cases */
1133	if (sym_is(name, namelen, "usb"))
1134		do_usb_table(symval, sym->st_size, mod);
1135	else if (sym_is(name, namelen, "pnp"))
1136		do_pnp_device_entry(symval, sym->st_size, mod);
1137	else if (sym_is(name, namelen, "pnp_card"))
1138		do_pnp_card_entries(symval, sym->st_size, mod);
1139	else {
1140		struct devtable **p;
1141		INIT_SECTION(__devtable);
1142
1143		for (p = __start___devtable; p < __stop___devtable; p++) {
1144			if (sym_is(name, namelen, (*p)->device_id)) {
1145				do_table(symval, sym->st_size, (*p)->id_size,
1146					 (*p)->device_id, (*p)->function, mod);
1147				break;
1148			}
1149		}
1150	}
1151	free(zeros);
1152}
1153
1154/* Now add out buffered information to the generated C source */
1155void add_moddevtable(struct buffer *buf, struct module *mod)
1156{
1157	buf_printf(buf, "\n");
1158	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1159	free(mod->dev_table_buf.p);
1160}
v3.15
   1/* Simple code to turn various tables in an ELF file into alias definitions.
   2 * This deals with kernel datastructures where they should be
   3 * dealt with: in the kernel source.
   4 *
   5 * Copyright 2002-2003  Rusty Russell, IBM Corporation
   6 *           2003       Kai Germaschewski
   7 *
   8 *
   9 * This software may be used and distributed according to the terms
  10 * of the GNU General Public License, incorporated herein by reference.
  11 */
  12
  13#include "modpost.h"
  14#include "devicetable-offsets.h"
  15
  16/* We use the ELF typedefs for kernel_ulong_t but bite the bullet and
  17 * use either stdint.h or inttypes.h for the rest. */
  18#if KERNEL_ELFCLASS == ELFCLASS32
  19typedef Elf32_Addr	kernel_ulong_t;
  20#define BITS_PER_LONG 32
  21#else
  22typedef Elf64_Addr	kernel_ulong_t;
  23#define BITS_PER_LONG 64
  24#endif
  25#ifdef __sun__
  26#include <inttypes.h>
  27#else
  28#include <stdint.h>
  29#endif
  30
  31#include <ctype.h>
  32#include <stdbool.h>
  33
  34typedef uint32_t	__u32;
  35typedef uint16_t	__u16;
  36typedef unsigned char	__u8;
  37
  38/* Big exception to the "don't include kernel headers into userspace, which
  39 * even potentially has different endianness and word sizes, since
  40 * we handle those differences explicitly below */
  41#include "../../include/linux/mod_devicetable.h"
  42
  43/* This array collects all instances that use the generic do_table */
  44struct devtable {
  45	const char *device_id; /* name of table, __mod_<name>__*_device_table. */
  46	unsigned long id_size;
  47	void *function;
  48};
  49
  50#define ___cat(a,b) a ## b
  51#define __cat(a,b) ___cat(a,b)
  52
  53/* we need some special handling for this host tool running eventually on
  54 * Darwin. The Mach-O section handling is a bit different than ELF section
  55 * handling. The differnces in detail are:
  56 *  a) we have segments which have sections
  57 *  b) we need a API call to get the respective section symbols */
  58#if defined(__MACH__)
  59#include <mach-o/getsect.h>
  60
  61#define INIT_SECTION(name)  do {					\
  62		unsigned long name ## _len;				\
  63		char *__cat(pstart_,name) = getsectdata("__TEXT",	\
  64			#name, &__cat(name,_len));			\
  65		char *__cat(pstop_,name) = __cat(pstart_,name) +	\
  66			__cat(name, _len);				\
  67		__cat(__start_,name) = (void *)__cat(pstart_,name);	\
  68		__cat(__stop_,name) = (void *)__cat(pstop_,name);	\
  69	} while (0)
  70#define SECTION(name)   __attribute__((section("__TEXT, " #name)))
  71
  72struct devtable **__start___devtable, **__stop___devtable;
  73#else
  74#define INIT_SECTION(name) /* no-op for ELF */
  75#define SECTION(name)   __attribute__((section(#name)))
  76
  77/* We construct a table of pointers in an ELF section (pointers generally
  78 * go unpadded by gcc).  ld creates boundary syms for us. */
  79extern struct devtable *__start___devtable[], *__stop___devtable[];
  80#endif /* __MACH__ */
  81
  82#if !defined(__used)
  83# if __GNUC__ == 3 && __GNUC_MINOR__ < 3
  84#  define __used			__attribute__((__unused__))
  85# else
  86#  define __used			__attribute__((__used__))
  87# endif
  88#endif
  89
  90/* Define a variable f that holds the value of field f of struct devid
  91 * based at address m.
  92 */
  93#define DEF_FIELD(m, devid, f) \
  94	typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
  95/* Define a variable f that holds the address of field f of struct devid
  96 * based at address m.  Due to the way typeof works, for a field of type
  97 * T[N] the variable has type T(*)[N], _not_ T*.
  98 */
  99#define DEF_FIELD_ADDR(m, devid, f) \
 100	typeof(((struct devid *)0)->f) *f = ((m) + OFF_##devid##_##f)
 101
 102/* Add a table entry.  We test function type matches while we're here. */
 103#define ADD_TO_DEVTABLE(device_id, type, function) \
 104	static struct devtable __cat(devtable,__LINE__) = {	\
 105		device_id + 0*sizeof((function)((const char *)NULL,	\
 106						(void *)NULL,		\
 107						(char *)NULL)),		\
 108		SIZE_##type, (function) };				\
 109	static struct devtable *SECTION(__devtable) __used \
 110		__cat(devtable_ptr,__LINE__) = &__cat(devtable,__LINE__)
 111
 112#define ADD(str, sep, cond, field)                              \
 113do {                                                            \
 114        strcat(str, sep);                                       \
 115        if (cond)                                               \
 116                sprintf(str + strlen(str),                      \
 117                        sizeof(field) == 1 ? "%02X" :           \
 118                        sizeof(field) == 2 ? "%04X" :           \
 119                        sizeof(field) == 4 ? "%08X" : "",       \
 120                        field);                                 \
 121        else                                                    \
 122                sprintf(str + strlen(str), "*");                \
 123} while(0)
 124
 125/* Always end in a wildcard, for future extension */
 126static inline void add_wildcard(char *str)
 127{
 128	int len = strlen(str);
 129
 130	if (str[len - 1] != '*')
 131		strcat(str + len, "*");
 132}
 133
 
 134/**
 135 * Check that sizeof(device_id type) are consistent with size of section
 136 * in .o file. If in-consistent then userspace and kernel does not agree
 137 * on actual size which is a bug.
 138 * Also verify that the final entry in the table is all zeros.
 139 * Ignore both checks if build host differ from target host and size differs.
 140 **/
 141static void device_id_check(const char *modname, const char *device_id,
 142			    unsigned long size, unsigned long id_size,
 143			    void *symval)
 144{
 145	int i;
 146
 147	if (size % id_size || size < id_size) {
 
 
 148		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
 149		      "of the size of "
 150		      "section __mod_%s__<identifier>_device_table=%lu.\n"
 151		      "Fix definition of struct %s_device_id "
 152		      "in mod_devicetable.h\n",
 153		      modname, device_id, id_size, device_id, size, device_id);
 154	}
 155	/* Verify last one is a terminator */
 156	for (i = 0; i < id_size; i++ ) {
 157		if (*(uint8_t*)(symval+size-id_size+i)) {
 158			fprintf(stderr,"%s: struct %s_device_id is %lu bytes.  "
 159				"The last of %lu is:\n",
 160				modname, device_id, id_size, size / id_size);
 161			for (i = 0; i < id_size; i++ )
 162				fprintf(stderr,"0x%02x ",
 163					*(uint8_t*)(symval+size-id_size+i) );
 164			fprintf(stderr,"\n");
 165			fatal("%s: struct %s_device_id is not terminated "
 166				"with a NULL entry!\n", modname, device_id);
 167		}
 168	}
 169}
 170
 171/* USB is special because the bcdDevice can be matched against a numeric range */
 172/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
 173static void do_usb_entry(void *symval,
 174			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
 175			 unsigned char range_lo, unsigned char range_hi,
 176			 unsigned char max, struct module *mod)
 177{
 178	char alias[500];
 179	DEF_FIELD(symval, usb_device_id, match_flags);
 180	DEF_FIELD(symval, usb_device_id, idVendor);
 181	DEF_FIELD(symval, usb_device_id, idProduct);
 182	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
 183	DEF_FIELD(symval, usb_device_id, bDeviceClass);
 184	DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
 185	DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
 186	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
 187	DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
 188	DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
 189	DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
 190
 191	strcpy(alias, "usb:");
 192	ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
 193	    idVendor);
 194	ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
 195	    idProduct);
 196
 197	strcat(alias, "d");
 198	if (bcdDevice_initial_digits)
 199		sprintf(alias + strlen(alias), "%0*X",
 200			bcdDevice_initial_digits, bcdDevice_initial);
 201	if (range_lo == range_hi)
 202		sprintf(alias + strlen(alias), "%X", range_lo);
 203	else if (range_lo > 0 || range_hi < max) {
 204		if (range_lo > 0x9 || range_hi < 0xA)
 205			sprintf(alias + strlen(alias),
 206				"[%X-%X]",
 207				range_lo,
 208				range_hi);
 209		else {
 210			sprintf(alias + strlen(alias),
 211				range_lo < 0x9 ? "[%X-9" : "[%X",
 212				range_lo);
 213			sprintf(alias + strlen(alias),
 214				range_hi > 0xA ? "A-%X]" : "%X]",
 215				range_hi);
 216		}
 217	}
 218	if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
 219		strcat(alias, "*");
 220
 221	ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
 222	    bDeviceClass);
 223	ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
 224	    bDeviceSubClass);
 225	ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
 226	    bDeviceProtocol);
 227	ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
 228	    bInterfaceClass);
 229	ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
 230	    bInterfaceSubClass);
 231	ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
 232	    bInterfaceProtocol);
 233	ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
 234	    bInterfaceNumber);
 
 
 
 235
 236	add_wildcard(alias);
 237	buf_printf(&mod->dev_table_buf,
 238		   "MODULE_ALIAS(\"%s\");\n", alias);
 239}
 240
 241/* Handles increment/decrement of BCD formatted integers */
 242/* Returns the previous value, so it works like i++ or i-- */
 243static unsigned int incbcd(unsigned int *bcd,
 244			   int inc,
 245			   unsigned char max,
 246			   size_t chars)
 247{
 248	unsigned int init = *bcd, i, j;
 249	unsigned long long c, dec = 0;
 250
 251	/* If bcd is not in BCD format, just increment */
 252	if (max > 0x9) {
 253		*bcd += inc;
 254		return init;
 255	}
 256
 257	/* Convert BCD to Decimal */
 258	for (i=0 ; i < chars ; i++) {
 259		c = (*bcd >> (i << 2)) & 0xf;
 260		c = c > 9 ? 9 : c; /* force to bcd just in case */
 261		for (j=0 ; j < i ; j++)
 262			c = c * 10;
 263		dec += c;
 264	}
 265
 266	/* Do our increment/decrement */
 267	dec += inc;
 268	*bcd  = 0;
 269
 270	/* Convert back to BCD */
 271	for (i=0 ; i < chars ; i++) {
 272		for (c=1,j=0 ; j < i ; j++)
 273			c = c * 10;
 274		c = (dec / c) % 10;
 275		*bcd += c << (i << 2);
 276	}
 277	return init;
 278}
 279
 280static void do_usb_entry_multi(void *symval, struct module *mod)
 281{
 282	unsigned int devlo, devhi;
 283	unsigned char chi, clo, max;
 284	int ndigits;
 285
 286	DEF_FIELD(symval, usb_device_id, match_flags);
 287	DEF_FIELD(symval, usb_device_id, idVendor);
 288	DEF_FIELD(symval, usb_device_id, idProduct);
 289	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
 290	DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
 291	DEF_FIELD(symval, usb_device_id, bDeviceClass);
 292	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
 293
 294	devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
 295		bcdDevice_lo : 0x0U;
 296	devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
 297		bcdDevice_hi : ~0x0U;
 298
 299	/* Figure out if this entry is in bcd or hex format */
 300	max = 0x9; /* Default to decimal format */
 301	for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
 302		clo = (devlo >> (ndigits << 2)) & 0xf;
 303		chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
 304		if (clo > max || chi > max) {
 305			max = 0xf;
 306			break;
 307		}
 308	}
 309
 310	/*
 311	 * Some modules (visor) have empty slots as placeholder for
 312	 * run-time specification that results in catch-all alias
 313	 */
 314	if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
 315		return;
 316
 317	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
 318	for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
 319		clo = devlo & 0xf;
 320		chi = devhi & 0xf;
 321		if (chi > max)	/* If we are in bcd mode, truncate if necessary */
 322			chi = max;
 323		devlo >>= 4;
 324		devhi >>= 4;
 325
 326		if (devlo == devhi || !ndigits) {
 327			do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
 328			break;
 329		}
 330
 331		if (clo > 0x0)
 332			do_usb_entry(symval,
 333				     incbcd(&devlo, 1, max,
 334					    sizeof(bcdDevice_lo) * 2),
 335				     ndigits, clo, max, max, mod);
 336
 337		if (chi < max)
 338			do_usb_entry(symval,
 339				     incbcd(&devhi, -1, max,
 340					    sizeof(bcdDevice_lo) * 2),
 341				     ndigits, 0x0, chi, max, mod);
 342	}
 343}
 344
 345static void do_usb_table(void *symval, unsigned long size,
 346			 struct module *mod)
 347{
 348	unsigned int i;
 349	const unsigned long id_size = SIZE_usb_device_id;
 350
 351	device_id_check(mod->name, "usb", size, id_size, symval);
 352
 353	/* Leave last one: it's the terminator. */
 354	size -= id_size;
 355
 356	for (i = 0; i < size; i += id_size)
 357		do_usb_entry_multi(symval + i, mod);
 358}
 359
 360/* Looks like: hid:bNvNpN */
 361static int do_hid_entry(const char *filename,
 362			     void *symval, char *alias)
 363{
 364	DEF_FIELD(symval, hid_device_id, bus);
 365	DEF_FIELD(symval, hid_device_id, group);
 366	DEF_FIELD(symval, hid_device_id, vendor);
 367	DEF_FIELD(symval, hid_device_id, product);
 368
 369	sprintf(alias, "hid:");
 370	ADD(alias, "b", bus != HID_BUS_ANY, bus);
 371	ADD(alias, "g", group != HID_GROUP_ANY, group);
 372	ADD(alias, "v", vendor != HID_ANY_ID, vendor);
 373	ADD(alias, "p", product != HID_ANY_ID, product);
 374
 375	return 1;
 376}
 377ADD_TO_DEVTABLE("hid", hid_device_id, do_hid_entry);
 378
 379/* Looks like: ieee1394:venNmoNspNverN */
 380static int do_ieee1394_entry(const char *filename,
 381			     void *symval, char *alias)
 382{
 383	DEF_FIELD(symval, ieee1394_device_id, match_flags);
 384	DEF_FIELD(symval, ieee1394_device_id, vendor_id);
 385	DEF_FIELD(symval, ieee1394_device_id, model_id);
 386	DEF_FIELD(symval, ieee1394_device_id, specifier_id);
 387	DEF_FIELD(symval, ieee1394_device_id, version);
 388
 389	strcpy(alias, "ieee1394:");
 390	ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
 391	    vendor_id);
 392	ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
 393	    model_id);
 394	ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
 395	    specifier_id);
 396	ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
 397	    version);
 398
 399	add_wildcard(alias);
 400	return 1;
 401}
 402ADD_TO_DEVTABLE("ieee1394", ieee1394_device_id, do_ieee1394_entry);
 403
 404/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
 405static int do_pci_entry(const char *filename,
 406			void *symval, char *alias)
 407{
 408	/* Class field can be divided into these three. */
 409	unsigned char baseclass, subclass, interface,
 410		baseclass_mask, subclass_mask, interface_mask;
 411
 412	DEF_FIELD(symval, pci_device_id, vendor);
 413	DEF_FIELD(symval, pci_device_id, device);
 414	DEF_FIELD(symval, pci_device_id, subvendor);
 415	DEF_FIELD(symval, pci_device_id, subdevice);
 416	DEF_FIELD(symval, pci_device_id, class);
 417	DEF_FIELD(symval, pci_device_id, class_mask);
 418
 419	strcpy(alias, "pci:");
 420	ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
 421	ADD(alias, "d", device != PCI_ANY_ID, device);
 422	ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
 423	ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
 424
 425	baseclass = (class) >> 16;
 426	baseclass_mask = (class_mask) >> 16;
 427	subclass = (class) >> 8;
 428	subclass_mask = (class_mask) >> 8;
 429	interface = class;
 430	interface_mask = class_mask;
 431
 432	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
 433	    || (subclass_mask != 0 && subclass_mask != 0xFF)
 434	    || (interface_mask != 0 && interface_mask != 0xFF)) {
 435		warn("Can't handle masks in %s:%04X\n",
 436		     filename, class_mask);
 437		return 0;
 438	}
 439
 440	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
 441	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
 442	ADD(alias, "i", interface_mask == 0xFF, interface);
 443	add_wildcard(alias);
 444	return 1;
 445}
 446ADD_TO_DEVTABLE("pci", pci_device_id, do_pci_entry);
 447
 448/* looks like: "ccw:tNmNdtNdmN" */
 449static int do_ccw_entry(const char *filename,
 450			void *symval, char *alias)
 451{
 452	DEF_FIELD(symval, ccw_device_id, match_flags);
 453	DEF_FIELD(symval, ccw_device_id, cu_type);
 454	DEF_FIELD(symval, ccw_device_id, cu_model);
 455	DEF_FIELD(symval, ccw_device_id, dev_type);
 456	DEF_FIELD(symval, ccw_device_id, dev_model);
 457
 458	strcpy(alias, "ccw:");
 459	ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
 460	    cu_type);
 461	ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
 462	    cu_model);
 463	ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
 464	    dev_type);
 465	ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
 466	    dev_model);
 467	add_wildcard(alias);
 468	return 1;
 469}
 470ADD_TO_DEVTABLE("ccw", ccw_device_id, do_ccw_entry);
 471
 472/* looks like: "ap:tN" */
 473static int do_ap_entry(const char *filename,
 474		       void *symval, char *alias)
 475{
 476	DEF_FIELD(symval, ap_device_id, dev_type);
 477
 478	sprintf(alias, "ap:t%02X*", dev_type);
 479	return 1;
 480}
 481ADD_TO_DEVTABLE("ap", ap_device_id, do_ap_entry);
 482
 483/* looks like: "css:tN" */
 484static int do_css_entry(const char *filename,
 485			void *symval, char *alias)
 486{
 487	DEF_FIELD(symval, css_device_id, type);
 488
 489	sprintf(alias, "css:t%01X", type);
 490	return 1;
 491}
 492ADD_TO_DEVTABLE("css", css_device_id, do_css_entry);
 493
 494/* Looks like: "serio:tyNprNidNexN" */
 495static int do_serio_entry(const char *filename,
 496			  void *symval, char *alias)
 497{
 498	DEF_FIELD(symval, serio_device_id, type);
 499	DEF_FIELD(symval, serio_device_id, proto);
 500	DEF_FIELD(symval, serio_device_id, id);
 501	DEF_FIELD(symval, serio_device_id, extra);
 502
 503	strcpy(alias, "serio:");
 504	ADD(alias, "ty", type != SERIO_ANY, type);
 505	ADD(alias, "pr", proto != SERIO_ANY, proto);
 506	ADD(alias, "id", id != SERIO_ANY, id);
 507	ADD(alias, "ex", extra != SERIO_ANY, extra);
 508
 509	add_wildcard(alias);
 510	return 1;
 511}
 512ADD_TO_DEVTABLE("serio", serio_device_id, do_serio_entry);
 513
 514/* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
 515static int do_acpi_entry(const char *filename,
 516			void *symval, char *alias)
 517{
 518	DEF_FIELD_ADDR(symval, acpi_device_id, id);
 519	sprintf(alias, "acpi*:%s:*", *id);
 520	return 1;
 521}
 522ADD_TO_DEVTABLE("acpi", acpi_device_id, do_acpi_entry);
 523
 524/* looks like: "pnp:dD" */
 525static void do_pnp_device_entry(void *symval, unsigned long size,
 526				struct module *mod)
 527{
 528	const unsigned long id_size = SIZE_pnp_device_id;
 529	const unsigned int count = (size / id_size)-1;
 
 530	unsigned int i;
 531
 532	device_id_check(mod->name, "pnp", size, id_size, symval);
 533
 534	for (i = 0; i < count; i++) {
 535		DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
 536		char acpi_id[sizeof(*id)];
 537		int j;
 538
 539		buf_printf(&mod->dev_table_buf,
 540			   "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
 541
 542		/* fix broken pnp bus lowercasing */
 543		for (j = 0; j < sizeof(acpi_id); j++)
 544			acpi_id[j] = toupper((*id)[j]);
 545		buf_printf(&mod->dev_table_buf,
 546			   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
 547	}
 548}
 549
 550/* looks like: "pnp:dD" for every device of the card */
 551static void do_pnp_card_entries(void *symval, unsigned long size,
 552				struct module *mod)
 553{
 554	const unsigned long id_size = SIZE_pnp_card_device_id;
 555	const unsigned int count = (size / id_size)-1;
 
 556	unsigned int i;
 557
 558	device_id_check(mod->name, "pnp", size, id_size, symval);
 559
 560	for (i = 0; i < count; i++) {
 561		unsigned int j;
 562		DEF_FIELD_ADDR(symval + i*id_size, pnp_card_device_id, devs);
 563
 564		for (j = 0; j < PNP_MAX_DEVICES; j++) {
 565			const char *id = (char *)(*devs)[j].id;
 566			int i2, j2;
 567			int dup = 0;
 568
 569			if (!id[0])
 570				break;
 571
 572			/* find duplicate, already added value */
 573			for (i2 = 0; i2 < i && !dup; i2++) {
 574				DEF_FIELD_ADDR(symval + i2*id_size, pnp_card_device_id, devs);
 575
 576				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
 577					const char *id2 = (char *)(*devs)[j2].id;
 578
 579					if (!id2[0])
 580						break;
 581
 582					if (!strcmp(id, id2)) {
 583						dup = 1;
 584						break;
 585					}
 586				}
 587			}
 588
 589			/* add an individual alias for every device entry */
 590			if (!dup) {
 591				char acpi_id[PNP_ID_LEN];
 592				int k;
 593
 594				buf_printf(&mod->dev_table_buf,
 595					   "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
 596
 597				/* fix broken pnp bus lowercasing */
 598				for (k = 0; k < sizeof(acpi_id); k++)
 599					acpi_id[k] = toupper(id[k]);
 600				buf_printf(&mod->dev_table_buf,
 601					   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
 602			}
 603		}
 604	}
 605}
 606
 607/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
 608static int do_pcmcia_entry(const char *filename,
 609			   void *symval, char *alias)
 610{
 611	unsigned int i;
 612	DEF_FIELD(symval, pcmcia_device_id, match_flags);
 613	DEF_FIELD(symval, pcmcia_device_id, manf_id);
 614	DEF_FIELD(symval, pcmcia_device_id, card_id);
 615	DEF_FIELD(symval, pcmcia_device_id, func_id);
 616	DEF_FIELD(symval, pcmcia_device_id, function);
 617	DEF_FIELD(symval, pcmcia_device_id, device_no);
 618	DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
 619
 620	for (i=0; i<4; i++) {
 621		(*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
 622	}
 623
 624	strcpy(alias, "pcmcia:");
 625	ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
 626	    manf_id);
 627	ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
 628	    card_id);
 629	ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
 630	    func_id);
 631	ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
 632	    function);
 633	ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
 634	    device_no);
 635	ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
 636	ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
 637	ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
 638	ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
 639
 640	add_wildcard(alias);
 641	return 1;
 642}
 643ADD_TO_DEVTABLE("pcmcia", pcmcia_device_id, do_pcmcia_entry);
 644
 645static int do_of_entry (const char *filename, void *symval, char *alias)
 646{
 647    int len;
 648    char *tmp;
 649    DEF_FIELD_ADDR(symval, of_device_id, name);
 650    DEF_FIELD_ADDR(symval, of_device_id, type);
 651    DEF_FIELD_ADDR(symval, of_device_id, compatible);
 652
 653    len = sprintf (alias, "of:N%sT%s",
 654                    (*name)[0] ? *name : "*",
 655                    (*type)[0] ? *type : "*");
 656
 657    if (compatible[0])
 658        sprintf (&alias[len], "%sC%s",
 659                     (*type)[0] ? "*" : "",
 660                     *compatible);
 661
 662    /* Replace all whitespace with underscores */
 663    for (tmp = alias; tmp && *tmp; tmp++)
 664        if (isspace (*tmp))
 665            *tmp = '_';
 666
 667    add_wildcard(alias);
 668    return 1;
 669}
 670ADD_TO_DEVTABLE("of", of_device_id, do_of_entry);
 671
 672static int do_vio_entry(const char *filename, void *symval,
 673		char *alias)
 674{
 675	char *tmp;
 676	DEF_FIELD_ADDR(symval, vio_device_id, type);
 677	DEF_FIELD_ADDR(symval, vio_device_id, compat);
 678
 679	sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
 680			(*compat)[0] ? *compat : "*");
 681
 682	/* Replace all whitespace with underscores */
 683	for (tmp = alias; tmp && *tmp; tmp++)
 684		if (isspace (*tmp))
 685			*tmp = '_';
 686
 687	add_wildcard(alias);
 688	return 1;
 689}
 690ADD_TO_DEVTABLE("vio", vio_device_id, do_vio_entry);
 691
 692#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 693
 694static void do_input(char *alias,
 695		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
 696{
 697	unsigned int i;
 698
 699	for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
 700		arr[i] = TO_NATIVE(arr[i]);
 701	for (i = min; i < max; i++)
 702		if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
 703			sprintf(alias + strlen(alias), "%X,*", i);
 704}
 705
 706/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
 707static int do_input_entry(const char *filename, void *symval,
 708			  char *alias)
 709{
 710	DEF_FIELD(symval, input_device_id, flags);
 711	DEF_FIELD(symval, input_device_id, bustype);
 712	DEF_FIELD(symval, input_device_id, vendor);
 713	DEF_FIELD(symval, input_device_id, product);
 714	DEF_FIELD(symval, input_device_id, version);
 715	DEF_FIELD_ADDR(symval, input_device_id, evbit);
 716	DEF_FIELD_ADDR(symval, input_device_id, keybit);
 717	DEF_FIELD_ADDR(symval, input_device_id, relbit);
 718	DEF_FIELD_ADDR(symval, input_device_id, absbit);
 719	DEF_FIELD_ADDR(symval, input_device_id, mscbit);
 720	DEF_FIELD_ADDR(symval, input_device_id, ledbit);
 721	DEF_FIELD_ADDR(symval, input_device_id, sndbit);
 722	DEF_FIELD_ADDR(symval, input_device_id, ffbit);
 723	DEF_FIELD_ADDR(symval, input_device_id, swbit);
 724
 725	sprintf(alias, "input:");
 726
 727	ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
 728	ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
 729	ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
 730	ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
 731
 732	sprintf(alias + strlen(alias), "-e*");
 733	if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
 734		do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
 735	sprintf(alias + strlen(alias), "k*");
 736	if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
 737		do_input(alias, *keybit,
 738			 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
 739			 INPUT_DEVICE_ID_KEY_MAX);
 740	sprintf(alias + strlen(alias), "r*");
 741	if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
 742		do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
 743	sprintf(alias + strlen(alias), "a*");
 744	if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
 745		do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
 746	sprintf(alias + strlen(alias), "m*");
 747	if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
 748		do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
 749	sprintf(alias + strlen(alias), "l*");
 750	if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
 751		do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
 752	sprintf(alias + strlen(alias), "s*");
 753	if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
 754		do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
 755	sprintf(alias + strlen(alias), "f*");
 756	if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
 757		do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
 758	sprintf(alias + strlen(alias), "w*");
 759	if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
 760		do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
 761	return 1;
 762}
 763ADD_TO_DEVTABLE("input", input_device_id, do_input_entry);
 764
 765static int do_eisa_entry(const char *filename, void *symval,
 766		char *alias)
 767{
 768	DEF_FIELD_ADDR(symval, eisa_device_id, sig);
 769	if (sig[0])
 770		sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
 771	else
 772		strcat(alias, "*");
 773	return 1;
 774}
 775ADD_TO_DEVTABLE("eisa", eisa_device_id, do_eisa_entry);
 776
 777/* Looks like: parisc:tNhvNrevNsvN */
 778static int do_parisc_entry(const char *filename, void *symval,
 779		char *alias)
 780{
 781	DEF_FIELD(symval, parisc_device_id, hw_type);
 782	DEF_FIELD(symval, parisc_device_id, hversion);
 783	DEF_FIELD(symval, parisc_device_id, hversion_rev);
 784	DEF_FIELD(symval, parisc_device_id, sversion);
 785
 786	strcpy(alias, "parisc:");
 787	ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
 788	ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
 789	ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
 790	ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
 791
 792	add_wildcard(alias);
 793	return 1;
 794}
 795ADD_TO_DEVTABLE("parisc", parisc_device_id, do_parisc_entry);
 796
 797/* Looks like: sdio:cNvNdN. */
 798static int do_sdio_entry(const char *filename,
 799			void *symval, char *alias)
 800{
 801	DEF_FIELD(symval, sdio_device_id, class);
 802	DEF_FIELD(symval, sdio_device_id, vendor);
 803	DEF_FIELD(symval, sdio_device_id, device);
 804
 805	strcpy(alias, "sdio:");
 806	ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
 807	ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
 808	ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
 809	add_wildcard(alias);
 810	return 1;
 811}
 812ADD_TO_DEVTABLE("sdio", sdio_device_id, do_sdio_entry);
 813
 814/* Looks like: ssb:vNidNrevN. */
 815static int do_ssb_entry(const char *filename,
 816			void *symval, char *alias)
 817{
 818	DEF_FIELD(symval, ssb_device_id, vendor);
 819	DEF_FIELD(symval, ssb_device_id, coreid);
 820	DEF_FIELD(symval, ssb_device_id, revision);
 821
 822	strcpy(alias, "ssb:");
 823	ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
 824	ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
 825	ADD(alias, "rev", revision != SSB_ANY_REV, revision);
 826	add_wildcard(alias);
 827	return 1;
 828}
 829ADD_TO_DEVTABLE("ssb", ssb_device_id, do_ssb_entry);
 830
 831/* Looks like: bcma:mNidNrevNclN. */
 832static int do_bcma_entry(const char *filename,
 833			 void *symval, char *alias)
 834{
 835	DEF_FIELD(symval, bcma_device_id, manuf);
 836	DEF_FIELD(symval, bcma_device_id, id);
 837	DEF_FIELD(symval, bcma_device_id, rev);
 838	DEF_FIELD(symval, bcma_device_id, class);
 839
 840	strcpy(alias, "bcma:");
 841	ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
 842	ADD(alias, "id", id != BCMA_ANY_ID, id);
 843	ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
 844	ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
 845	add_wildcard(alias);
 846	return 1;
 847}
 848ADD_TO_DEVTABLE("bcma", bcma_device_id, do_bcma_entry);
 849
 850/* Looks like: virtio:dNvN */
 851static int do_virtio_entry(const char *filename, void *symval,
 852			   char *alias)
 853{
 854	DEF_FIELD(symval, virtio_device_id, device);
 855	DEF_FIELD(symval, virtio_device_id, vendor);
 856
 857	strcpy(alias, "virtio:");
 858	ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
 859	ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
 860
 861	add_wildcard(alias);
 862	return 1;
 863}
 864ADD_TO_DEVTABLE("virtio", virtio_device_id, do_virtio_entry);
 865
 866/*
 867 * Looks like: vmbus:guid
 868 * Each byte of the guid will be represented by two hex characters
 869 * in the name.
 870 */
 871
 872static int do_vmbus_entry(const char *filename, void *symval,
 873			  char *alias)
 874{
 875	int i;
 876	DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
 877	char guid_name[(sizeof(*guid) + 1) * 2];
 878
 879	for (i = 0; i < (sizeof(*guid) * 2); i += 2)
 880		sprintf(&guid_name[i], "%02x", TO_NATIVE((*guid)[i/2]));
 881
 882	strcpy(alias, "vmbus:");
 883	strcat(alias, guid_name);
 884
 885	return 1;
 886}
 887ADD_TO_DEVTABLE("vmbus", hv_vmbus_device_id, do_vmbus_entry);
 888
 889/* Looks like: i2c:S */
 890static int do_i2c_entry(const char *filename, void *symval,
 891			char *alias)
 892{
 893	DEF_FIELD_ADDR(symval, i2c_device_id, name);
 894	sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
 895
 896	return 1;
 897}
 898ADD_TO_DEVTABLE("i2c", i2c_device_id, do_i2c_entry);
 899
 900/* Looks like: spi:S */
 901static int do_spi_entry(const char *filename, void *symval,
 902			char *alias)
 903{
 904	DEF_FIELD_ADDR(symval, spi_device_id, name);
 905	sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
 906
 907	return 1;
 908}
 909ADD_TO_DEVTABLE("spi", spi_device_id, do_spi_entry);
 910
 911static const struct dmifield {
 912	const char *prefix;
 913	int field;
 914} dmi_fields[] = {
 915	{ "bvn", DMI_BIOS_VENDOR },
 916	{ "bvr", DMI_BIOS_VERSION },
 917	{ "bd",  DMI_BIOS_DATE },
 918	{ "svn", DMI_SYS_VENDOR },
 919	{ "pn",  DMI_PRODUCT_NAME },
 920	{ "pvr", DMI_PRODUCT_VERSION },
 921	{ "rvn", DMI_BOARD_VENDOR },
 922	{ "rn",  DMI_BOARD_NAME },
 923	{ "rvr", DMI_BOARD_VERSION },
 924	{ "cvn", DMI_CHASSIS_VENDOR },
 925	{ "ct",  DMI_CHASSIS_TYPE },
 926	{ "cvr", DMI_CHASSIS_VERSION },
 927	{ NULL,  DMI_NONE }
 928};
 929
 930static void dmi_ascii_filter(char *d, const char *s)
 931{
 932	/* Filter out characters we don't want to see in the modalias string */
 933	for (; *s; s++)
 934		if (*s > ' ' && *s < 127 && *s != ':')
 935			*(d++) = *s;
 936
 937	*d = 0;
 938}
 939
 940
 941static int do_dmi_entry(const char *filename, void *symval,
 942			char *alias)
 943{
 944	int i, j;
 945	DEF_FIELD_ADDR(symval, dmi_system_id, matches);
 946	sprintf(alias, "dmi*");
 947
 948	for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
 949		for (j = 0; j < 4; j++) {
 950			if ((*matches)[j].slot &&
 951			    (*matches)[j].slot == dmi_fields[i].field) {
 952				sprintf(alias + strlen(alias), ":%s*",
 953					dmi_fields[i].prefix);
 954				dmi_ascii_filter(alias + strlen(alias),
 955						 (*matches)[j].substr);
 956				strcat(alias, "*");
 957			}
 958		}
 959	}
 960
 961	strcat(alias, ":");
 962	return 1;
 963}
 964ADD_TO_DEVTABLE("dmi", dmi_system_id, do_dmi_entry);
 965
 966static int do_platform_entry(const char *filename,
 967			     void *symval, char *alias)
 968{
 969	DEF_FIELD_ADDR(symval, platform_device_id, name);
 970	sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
 971	return 1;
 972}
 973ADD_TO_DEVTABLE("platform", platform_device_id, do_platform_entry);
 974
 975static int do_mdio_entry(const char *filename,
 976			 void *symval, char *alias)
 977{
 978	int i;
 979	DEF_FIELD(symval, mdio_device_id, phy_id);
 980	DEF_FIELD(symval, mdio_device_id, phy_id_mask);
 981
 982	alias += sprintf(alias, MDIO_MODULE_PREFIX);
 983
 984	for (i = 0; i < 32; i++) {
 985		if (!((phy_id_mask >> (31-i)) & 1))
 986			*(alias++) = '?';
 987		else if ((phy_id >> (31-i)) & 1)
 988			*(alias++) = '1';
 989		else
 990			*(alias++) = '0';
 991	}
 992
 993	/* Terminate the string */
 994	*alias = 0;
 995
 996	return 1;
 997}
 998ADD_TO_DEVTABLE("mdio", mdio_device_id, do_mdio_entry);
 999
1000/* Looks like: zorro:iN. */
1001static int do_zorro_entry(const char *filename, void *symval,
1002			  char *alias)
1003{
1004	DEF_FIELD(symval, zorro_device_id, id);
1005	strcpy(alias, "zorro:");
1006	ADD(alias, "i", id != ZORRO_WILDCARD, id);
1007	return 1;
1008}
1009ADD_TO_DEVTABLE("zorro", zorro_device_id, do_zorro_entry);
1010
1011/* looks like: "pnp:dD" */
1012static int do_isapnp_entry(const char *filename,
1013			   void *symval, char *alias)
1014{
1015	DEF_FIELD(symval, isapnp_device_id, vendor);
1016	DEF_FIELD(symval, isapnp_device_id, function);
1017	sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
1018		'A' + ((vendor >> 2) & 0x3f) - 1,
1019		'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1020		'A' + ((vendor >> 8) & 0x1f) - 1,
1021		(function >> 4) & 0x0f, function & 0x0f,
1022		(function >> 12) & 0x0f, (function >> 8) & 0x0f);
1023	return 1;
1024}
1025ADD_TO_DEVTABLE("isapnp", isapnp_device_id, do_isapnp_entry);
1026
1027/* Looks like: "ipack:fNvNdN". */
1028static int do_ipack_entry(const char *filename,
1029			  void *symval, char *alias)
1030{
1031	DEF_FIELD(symval, ipack_device_id, format);
1032	DEF_FIELD(symval, ipack_device_id, vendor);
1033	DEF_FIELD(symval, ipack_device_id, device);
1034	strcpy(alias, "ipack:");
1035	ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1036	ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1037	ADD(alias, "d", device != IPACK_ANY_ID, device);
1038	add_wildcard(alias);
1039	return 1;
1040}
1041ADD_TO_DEVTABLE("ipack", ipack_device_id, do_ipack_entry);
1042
1043/*
1044 * Append a match expression for a single masked hex digit.
1045 * outp points to a pointer to the character at which to append.
1046 *	*outp is updated on return to point just after the appended text,
1047 *	to facilitate further appending.
1048 */
1049static void append_nibble_mask(char **outp,
1050			       unsigned int nibble, unsigned int mask)
1051{
1052	char *p = *outp;
1053	unsigned int i;
1054
1055	switch (mask) {
1056	case 0:
1057		*p++ = '?';
1058		break;
1059
1060	case 0xf:
1061		p += sprintf(p, "%X",  nibble);
1062		break;
1063
1064	default:
1065		/*
1066		 * Dumbly emit a match pattern for all possible matching
1067		 * digits.  This could be improved in some cases using ranges,
1068		 * but it has the advantage of being trivially correct, and is
1069		 * often optimal.
1070		 */
1071		*p++ = '[';
1072		for (i = 0; i < 0x10; i++)
1073			if ((i & mask) == nibble)
1074				p += sprintf(p, "%X", i);
1075		*p++ = ']';
1076	}
1077
1078	/* Ensure that the string remains NUL-terminated: */
1079	*p = '\0';
1080
1081	/* Advance the caller's end-of-string pointer: */
1082	*outp = p;
1083}
1084
1085/*
1086 * looks like: "amba:dN"
1087 *
1088 * N is exactly 8 digits, where each is an upper-case hex digit, or
1089 *	a ? or [] pattern matching exactly one digit.
1090 */
1091static int do_amba_entry(const char *filename,
1092			 void *symval, char *alias)
1093{
1094	unsigned int digit;
1095	char *p = alias;
1096	DEF_FIELD(symval, amba_id, id);
1097	DEF_FIELD(symval, amba_id, mask);
1098
1099	if ((id & mask) != id)
1100		fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1101		      "id=0x%08X, mask=0x%08X.  Please fix this driver.\n",
1102		      filename, id, mask);
1103
1104	p += sprintf(alias, "amba:d");
1105	for (digit = 0; digit < 8; digit++)
1106		append_nibble_mask(&p,
1107				   (id >> (4 * (7 - digit))) & 0xf,
1108				   (mask >> (4 * (7 - digit))) & 0xf);
1109
1110	return 1;
1111}
1112ADD_TO_DEVTABLE("amba", amba_id, do_amba_entry);
1113
1114/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
1115 * All fields are numbers. It would be nicer to use strings for vendor
1116 * and feature, but getting those out of the build system here is too
1117 * complicated.
1118 */
1119
1120static int do_x86cpu_entry(const char *filename, void *symval,
1121			   char *alias)
1122{
1123	DEF_FIELD(symval, x86_cpu_id, feature);
1124	DEF_FIELD(symval, x86_cpu_id, family);
1125	DEF_FIELD(symval, x86_cpu_id, model);
1126	DEF_FIELD(symval, x86_cpu_id, vendor);
1127
1128	strcpy(alias, "cpu:type:x86,");
1129	ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1130	ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1131	ADD(alias, "mod", model  != X86_MODEL_ANY,  model);
1132	strcat(alias, ":feature:*");
1133	if (feature != X86_FEATURE_ANY)
1134		sprintf(alias + strlen(alias), "%04X*", feature);
1135	return 1;
1136}
1137ADD_TO_DEVTABLE("x86cpu", x86_cpu_id, do_x86cpu_entry);
1138
1139/* LOOKS like cpu:type:*:feature:*FEAT* */
1140static int do_cpu_entry(const char *filename, void *symval, char *alias)
1141{
1142	DEF_FIELD(symval, cpu_feature, feature);
1143
1144	sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1145	return 1;
1146}
1147ADD_TO_DEVTABLE("cpu", cpu_feature, do_cpu_entry);
1148
1149/* Looks like: mei:S */
1150static int do_mei_entry(const char *filename, void *symval,
1151			char *alias)
1152{
1153	DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
1154
1155	sprintf(alias, MEI_CL_MODULE_PREFIX "%s", *name);
1156
1157	return 1;
1158}
1159ADD_TO_DEVTABLE("mei", mei_cl_device_id, do_mei_entry);
1160
1161/* Looks like: rapidio:vNdNavNadN */
1162static int do_rio_entry(const char *filename,
1163			void *symval, char *alias)
1164{
1165	DEF_FIELD(symval, rio_device_id, did);
1166	DEF_FIELD(symval, rio_device_id, vid);
1167	DEF_FIELD(symval, rio_device_id, asm_did);
1168	DEF_FIELD(symval, rio_device_id, asm_vid);
1169
1170	strcpy(alias, "rapidio:");
1171	ADD(alias, "v", vid != RIO_ANY_ID, vid);
1172	ADD(alias, "d", did != RIO_ANY_ID, did);
1173	ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1174	ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1175
1176	add_wildcard(alias);
1177	return 1;
1178}
1179ADD_TO_DEVTABLE("rapidio", rio_device_id, do_rio_entry);
1180
1181/* Does namelen bytes of name exactly match the symbol? */
1182static bool sym_is(const char *name, unsigned namelen, const char *symbol)
1183{
1184	if (namelen != strlen(symbol))
1185		return false;
1186
1187	return memcmp(name, symbol, namelen) == 0;
1188}
1189
1190static void do_table(void *symval, unsigned long size,
1191		     unsigned long id_size,
1192		     const char *device_id,
1193		     void *function,
1194		     struct module *mod)
1195{
1196	unsigned int i;
1197	char alias[500];
1198	int (*do_entry)(const char *, void *entry, char *alias) = function;
1199
1200	device_id_check(mod->name, device_id, size, id_size, symval);
1201	/* Leave last one: it's the terminator. */
1202	size -= id_size;
1203
1204	for (i = 0; i < size; i += id_size) {
1205		if (do_entry(mod->name, symval+i, alias)) {
1206			buf_printf(&mod->dev_table_buf,
1207				   "MODULE_ALIAS(\"%s\");\n", alias);
1208		}
1209	}
1210}
1211
1212/* Create MODULE_ALIAS() statements.
1213 * At this time, we cannot write the actual output C source yet,
1214 * so we write into the mod->dev_table_buf buffer. */
1215void handle_moddevtable(struct module *mod, struct elf_info *info,
1216			Elf_Sym *sym, const char *symname)
1217{
1218	void *symval;
1219	char *zeros = NULL;
1220	const char *name, *identifier;
1221	unsigned int namelen;
1222
1223	/* We're looking for a section relative symbol */
1224	if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
1225		return;
1226
1227	/* We're looking for an object */
1228	if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1229		return;
1230
1231	/* All our symbols are of form <prefix>__mod_<name>__<identifier>_device_table. */
1232	name = strstr(symname, "__mod_");
1233	if (!name)
1234		return;
1235	name += strlen("__mod_");
1236	namelen = strlen(name);
1237	if (namelen < strlen("_device_table"))
1238		return;
1239	if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1240		return;
1241	identifier = strstr(name, "__");
1242	if (!identifier)
1243		return;
1244	namelen = identifier - name;
1245
1246	/* Handle all-NULL symbols allocated into .bss */
1247	if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
1248		zeros = calloc(1, sym->st_size);
1249		symval = zeros;
1250	} else {
1251		symval = (void *)info->hdr
1252			+ info->sechdrs[get_secindex(info, sym)].sh_offset
1253			+ sym->st_value;
1254	}
1255
1256	/* First handle the "special" cases */
1257	if (sym_is(name, namelen, "usb"))
1258		do_usb_table(symval, sym->st_size, mod);
1259	else if (sym_is(name, namelen, "pnp"))
1260		do_pnp_device_entry(symval, sym->st_size, mod);
1261	else if (sym_is(name, namelen, "pnp_card"))
1262		do_pnp_card_entries(symval, sym->st_size, mod);
1263	else {
1264		struct devtable **p;
1265		INIT_SECTION(__devtable);
1266
1267		for (p = __start___devtable; p < __stop___devtable; p++) {
1268			if (sym_is(name, namelen, (*p)->device_id)) {
1269				do_table(symval, sym->st_size, (*p)->id_size,
1270					 (*p)->device_id, (*p)->function, mod);
1271				break;
1272			}
1273		}
1274	}
1275	free(zeros);
1276}
1277
1278/* Now add out buffered information to the generated C source */
1279void add_moddevtable(struct buffer *buf, struct module *mod)
1280{
1281	buf_printf(buf, "\n");
1282	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1283	free(mod->dev_table_buf.p);
1284}