Linux Audio

Check our new training course

Loading...
v5.9
   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;
  37typedef struct {
  38	__u8 b[16];
  39} guid_t;
  40
  41/* backwards compatibility, don't use in new code */
  42typedef struct {
  43	__u8 b[16];
  44} uuid_le;
  45typedef struct {
  46	__u8 b[16];
  47} uuid_t;
  48#define	UUID_STRING_LEN		36
  49
  50/* Big exception to the "don't include kernel headers into userspace, which
  51 * even potentially has different endianness and word sizes, since
  52 * we handle those differences explicitly below */
  53#include "../../include/linux/mod_devicetable.h"
  54
  55/* This array collects all instances that use the generic do_table */
  56struct devtable {
  57	const char *device_id; /* name of table, __mod_<name>__*_device_table. */
  58	unsigned long id_size;
  59	int (*do_entry)(const char *filename, void *symval, char *alias);
  60};
  61
  62/* Size of alias provided to do_entry functions */
  63#define ALIAS_SIZE 500
  64
  65/* Define a variable f that holds the value of field f of struct devid
  66 * based at address m.
  67 */
  68#define DEF_FIELD(m, devid, f) \
  69	typeof(((struct devid *)0)->f) f = TO_NATIVE(*(typeof(f) *)((m) + OFF_##devid##_##f))
  70
  71/* Define a variable v that holds the address of field f of struct devid
  72 * based at address m.  Due to the way typeof works, for a field of type
  73 * T[N] the variable has type T(*)[N], _not_ T*.
  74 */
  75#define DEF_FIELD_ADDR_VAR(m, devid, f, v) \
  76	typeof(((struct devid *)0)->f) *v = ((m) + OFF_##devid##_##f)
  77
  78/* Define a variable f that holds the address of field f of struct devid
  79 * based at address m.  Due to the way typeof works, for a field of type
  80 * T[N] the variable has type T(*)[N], _not_ T*.
  81 */
  82#define DEF_FIELD_ADDR(m, devid, f) \
  83	DEF_FIELD_ADDR_VAR(m, devid, f, f)
  84
  85#define ADD(str, sep, cond, field)                              \
  86do {                                                            \
  87        strcat(str, sep);                                       \
  88        if (cond)                                               \
  89                sprintf(str + strlen(str),                      \
  90                        sizeof(field) == 1 ? "%02X" :           \
  91                        sizeof(field) == 2 ? "%04X" :           \
  92                        sizeof(field) == 4 ? "%08X" : "",       \
  93                        field);                                 \
  94        else                                                    \
  95                sprintf(str + strlen(str), "*");                \
  96} while(0)
  97
  98/* End in a wildcard, for future extension */
  99static inline void add_wildcard(char *str)
 100{
 101	int len = strlen(str);
 102
 103	if (str[len - 1] != '*')
 104		strcat(str + len, "*");
 105}
 106
 107static inline void add_uuid(char *str, uuid_le uuid)
 108{
 109	int len = strlen(str);
 110
 111	sprintf(str + len, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
 112		uuid.b[3], uuid.b[2], uuid.b[1], uuid.b[0],
 113		uuid.b[5], uuid.b[4], uuid.b[7], uuid.b[6],
 114		uuid.b[8], uuid.b[9], uuid.b[10], uuid.b[11],
 115		uuid.b[12], uuid.b[13], uuid.b[14], uuid.b[15]);
 116}
 117
 118/**
 119 * Check that sizeof(device_id type) are consistent with size of section
 120 * in .o file. If in-consistent then userspace and kernel does not agree
 121 * on actual size which is a bug.
 122 * Also verify that the final entry in the table is all zeros.
 123 * Ignore both checks if build host differ from target host and size differs.
 124 **/
 125static void device_id_check(const char *modname, const char *device_id,
 126			    unsigned long size, unsigned long id_size,
 127			    void *symval)
 128{
 129	int i;
 130
 131	if (size % id_size || size < id_size) {
 
 
 132		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
 133		      "of the size of "
 134		      "section __mod_%s__<identifier>_device_table=%lu.\n"
 135		      "Fix definition of struct %s_device_id "
 136		      "in mod_devicetable.h\n",
 137		      modname, device_id, id_size, device_id, size, device_id);
 138	}
 139	/* Verify last one is a terminator */
 140	for (i = 0; i < id_size; i++ ) {
 141		if (*(uint8_t*)(symval+size-id_size+i)) {
 142			fprintf(stderr,"%s: struct %s_device_id is %lu bytes.  "
 143				"The last of %lu is:\n",
 144				modname, device_id, id_size, size / id_size);
 145			for (i = 0; i < id_size; i++ )
 146				fprintf(stderr,"0x%02x ",
 147					*(uint8_t*)(symval+size-id_size+i) );
 148			fprintf(stderr,"\n");
 149			fatal("%s: struct %s_device_id is not terminated "
 150				"with a NULL entry!\n", modname, device_id);
 151		}
 152	}
 153}
 154
 155/* USB is special because the bcdDevice can be matched against a numeric range */
 156/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipNinN" */
 157static void do_usb_entry(void *symval,
 158			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
 159			 unsigned char range_lo, unsigned char range_hi,
 160			 unsigned char max, struct module *mod)
 161{
 162	char alias[500];
 163	DEF_FIELD(symval, usb_device_id, match_flags);
 164	DEF_FIELD(symval, usb_device_id, idVendor);
 165	DEF_FIELD(symval, usb_device_id, idProduct);
 166	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
 167	DEF_FIELD(symval, usb_device_id, bDeviceClass);
 168	DEF_FIELD(symval, usb_device_id, bDeviceSubClass);
 169	DEF_FIELD(symval, usb_device_id, bDeviceProtocol);
 170	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
 171	DEF_FIELD(symval, usb_device_id, bInterfaceSubClass);
 172	DEF_FIELD(symval, usb_device_id, bInterfaceProtocol);
 173	DEF_FIELD(symval, usb_device_id, bInterfaceNumber);
 174
 175	strcpy(alias, "usb:");
 176	ADD(alias, "v", match_flags&USB_DEVICE_ID_MATCH_VENDOR,
 177	    idVendor);
 178	ADD(alias, "p", match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
 179	    idProduct);
 180
 181	strcat(alias, "d");
 182	if (bcdDevice_initial_digits)
 183		sprintf(alias + strlen(alias), "%0*X",
 184			bcdDevice_initial_digits, bcdDevice_initial);
 185	if (range_lo == range_hi)
 186		sprintf(alias + strlen(alias), "%X", range_lo);
 187	else if (range_lo > 0 || range_hi < max) {
 188		if (range_lo > 0x9 || range_hi < 0xA)
 189			sprintf(alias + strlen(alias),
 190				"[%X-%X]",
 191				range_lo,
 192				range_hi);
 193		else {
 194			sprintf(alias + strlen(alias),
 195				range_lo < 0x9 ? "[%X-9" : "[%X",
 196				range_lo);
 197			sprintf(alias + strlen(alias),
 198				range_hi > 0xA ? "A-%X]" : "%X]",
 199				range_hi);
 200		}
 201	}
 202	if (bcdDevice_initial_digits < (sizeof(bcdDevice_lo) * 2 - 1))
 203		strcat(alias, "*");
 204
 205	ADD(alias, "dc", match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
 206	    bDeviceClass);
 207	ADD(alias, "dsc", match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
 208	    bDeviceSubClass);
 209	ADD(alias, "dp", match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
 210	    bDeviceProtocol);
 211	ADD(alias, "ic", match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
 212	    bInterfaceClass);
 213	ADD(alias, "isc", match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
 214	    bInterfaceSubClass);
 215	ADD(alias, "ip", match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
 216	    bInterfaceProtocol);
 217	ADD(alias, "in", match_flags&USB_DEVICE_ID_MATCH_INT_NUMBER,
 218	    bInterfaceNumber);
 
 
 
 219
 220	add_wildcard(alias);
 221	buf_printf(&mod->dev_table_buf,
 222		   "MODULE_ALIAS(\"%s\");\n", alias);
 223}
 224
 225/* Handles increment/decrement of BCD formatted integers */
 226/* Returns the previous value, so it works like i++ or i-- */
 227static unsigned int incbcd(unsigned int *bcd,
 228			   int inc,
 229			   unsigned char max,
 230			   size_t chars)
 231{
 232	unsigned int init = *bcd, i, j;
 233	unsigned long long c, dec = 0;
 234
 235	/* If bcd is not in BCD format, just increment */
 236	if (max > 0x9) {
 237		*bcd += inc;
 238		return init;
 239	}
 240
 241	/* Convert BCD to Decimal */
 242	for (i=0 ; i < chars ; i++) {
 243		c = (*bcd >> (i << 2)) & 0xf;
 244		c = c > 9 ? 9 : c; /* force to bcd just in case */
 245		for (j=0 ; j < i ; j++)
 246			c = c * 10;
 247		dec += c;
 248	}
 249
 250	/* Do our increment/decrement */
 251	dec += inc;
 252	*bcd  = 0;
 253
 254	/* Convert back to BCD */
 255	for (i=0 ; i < chars ; i++) {
 256		for (c=1,j=0 ; j < i ; j++)
 257			c = c * 10;
 258		c = (dec / c) % 10;
 259		*bcd += c << (i << 2);
 260	}
 261	return init;
 262}
 263
 264static void do_usb_entry_multi(void *symval, struct module *mod)
 265{
 266	unsigned int devlo, devhi;
 267	unsigned char chi, clo, max;
 268	int ndigits;
 269
 270	DEF_FIELD(symval, usb_device_id, match_flags);
 271	DEF_FIELD(symval, usb_device_id, idVendor);
 272	DEF_FIELD(symval, usb_device_id, idProduct);
 273	DEF_FIELD(symval, usb_device_id, bcdDevice_lo);
 274	DEF_FIELD(symval, usb_device_id, bcdDevice_hi);
 275	DEF_FIELD(symval, usb_device_id, bDeviceClass);
 276	DEF_FIELD(symval, usb_device_id, bInterfaceClass);
 277
 278	devlo = match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
 279		bcdDevice_lo : 0x0U;
 280	devhi = match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
 281		bcdDevice_hi : ~0x0U;
 282
 283	/* Figure out if this entry is in bcd or hex format */
 284	max = 0x9; /* Default to decimal format */
 285	for (ndigits = 0 ; ndigits < sizeof(bcdDevice_lo) * 2 ; ndigits++) {
 286		clo = (devlo >> (ndigits << 2)) & 0xf;
 287		chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
 288		if (clo > max || chi > max) {
 289			max = 0xf;
 290			break;
 291		}
 292	}
 293
 294	/*
 295	 * Some modules (visor) have empty slots as placeholder for
 296	 * run-time specification that results in catch-all alias
 297	 */
 298	if (!(idVendor | idProduct | bDeviceClass | bInterfaceClass))
 299		return;
 300
 301	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
 302	for (ndigits = sizeof(bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
 303		clo = devlo & 0xf;
 304		chi = devhi & 0xf;
 305		if (chi > max)	/* If we are in bcd mode, truncate if necessary */
 306			chi = max;
 307		devlo >>= 4;
 308		devhi >>= 4;
 309
 310		if (devlo == devhi || !ndigits) {
 311			do_usb_entry(symval, devlo, ndigits, clo, chi, max, mod);
 312			break;
 313		}
 314
 315		if (clo > 0x0)
 316			do_usb_entry(symval,
 317				     incbcd(&devlo, 1, max,
 318					    sizeof(bcdDevice_lo) * 2),
 319				     ndigits, clo, max, max, mod);
 320
 321		if (chi < max)
 322			do_usb_entry(symval,
 323				     incbcd(&devhi, -1, max,
 324					    sizeof(bcdDevice_lo) * 2),
 325				     ndigits, 0x0, chi, max, mod);
 326	}
 327}
 328
 329static void do_usb_table(void *symval, unsigned long size,
 330			 struct module *mod)
 331{
 332	unsigned int i;
 333	const unsigned long id_size = SIZE_usb_device_id;
 334
 335	device_id_check(mod->name, "usb", size, id_size, symval);
 336
 337	/* Leave last one: it's the terminator. */
 338	size -= id_size;
 339
 340	for (i = 0; i < size; i += id_size)
 341		do_usb_entry_multi(symval + i, mod);
 342}
 343
 344static void do_of_entry_multi(void *symval, struct module *mod)
 345{
 346	char alias[500];
 347	int len;
 348	char *tmp;
 349
 350	DEF_FIELD_ADDR(symval, of_device_id, name);
 351	DEF_FIELD_ADDR(symval, of_device_id, type);
 352	DEF_FIELD_ADDR(symval, of_device_id, compatible);
 353
 354	len = sprintf(alias, "of:N%sT%s", (*name)[0] ? *name : "*",
 355		      (*type)[0] ? *type : "*");
 356
 357	if ((*compatible)[0])
 358		sprintf(&alias[len], "%sC%s", (*type)[0] ? "*" : "",
 359			*compatible);
 360
 361	/* Replace all whitespace with underscores */
 362	for (tmp = alias; tmp && *tmp; tmp++)
 363		if (isspace(*tmp))
 364			*tmp = '_';
 365
 366	buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
 367	strcat(alias, "C");
 368	add_wildcard(alias);
 369	buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias);
 370}
 371
 372static void do_of_table(void *symval, unsigned long size,
 373			struct module *mod)
 374{
 375	unsigned int i;
 376	const unsigned long id_size = SIZE_of_device_id;
 377
 378	device_id_check(mod->name, "of", size, id_size, symval);
 379
 380	/* Leave last one: it's the terminator. */
 381	size -= id_size;
 382
 383	for (i = 0; i < size; i += id_size)
 384		do_of_entry_multi(symval + i, mod);
 385}
 386
 387/* Looks like: hid:bNvNpN */
 388static int do_hid_entry(const char *filename,
 389			     void *symval, char *alias)
 390{
 391	DEF_FIELD(symval, hid_device_id, bus);
 392	DEF_FIELD(symval, hid_device_id, group);
 393	DEF_FIELD(symval, hid_device_id, vendor);
 394	DEF_FIELD(symval, hid_device_id, product);
 395
 396	sprintf(alias, "hid:");
 397	ADD(alias, "b", bus != HID_BUS_ANY, bus);
 398	ADD(alias, "g", group != HID_GROUP_ANY, group);
 399	ADD(alias, "v", vendor != HID_ANY_ID, vendor);
 400	ADD(alias, "p", product != HID_ANY_ID, product);
 401
 402	return 1;
 403}
 404
 405/* Looks like: ieee1394:venNmoNspNverN */
 406static int do_ieee1394_entry(const char *filename,
 407			     void *symval, char *alias)
 408{
 409	DEF_FIELD(symval, ieee1394_device_id, match_flags);
 410	DEF_FIELD(symval, ieee1394_device_id, vendor_id);
 411	DEF_FIELD(symval, ieee1394_device_id, model_id);
 412	DEF_FIELD(symval, ieee1394_device_id, specifier_id);
 413	DEF_FIELD(symval, ieee1394_device_id, version);
 414
 415	strcpy(alias, "ieee1394:");
 416	ADD(alias, "ven", match_flags & IEEE1394_MATCH_VENDOR_ID,
 417	    vendor_id);
 418	ADD(alias, "mo", match_flags & IEEE1394_MATCH_MODEL_ID,
 419	    model_id);
 420	ADD(alias, "sp", match_flags & IEEE1394_MATCH_SPECIFIER_ID,
 421	    specifier_id);
 422	ADD(alias, "ver", match_flags & IEEE1394_MATCH_VERSION,
 423	    version);
 424
 425	add_wildcard(alias);
 426	return 1;
 427}
 428
 429/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
 430static int do_pci_entry(const char *filename,
 431			void *symval, char *alias)
 432{
 433	/* Class field can be divided into these three. */
 434	unsigned char baseclass, subclass, interface,
 435		baseclass_mask, subclass_mask, interface_mask;
 436
 437	DEF_FIELD(symval, pci_device_id, vendor);
 438	DEF_FIELD(symval, pci_device_id, device);
 439	DEF_FIELD(symval, pci_device_id, subvendor);
 440	DEF_FIELD(symval, pci_device_id, subdevice);
 441	DEF_FIELD(symval, pci_device_id, class);
 442	DEF_FIELD(symval, pci_device_id, class_mask);
 443
 444	strcpy(alias, "pci:");
 445	ADD(alias, "v", vendor != PCI_ANY_ID, vendor);
 446	ADD(alias, "d", device != PCI_ANY_ID, device);
 447	ADD(alias, "sv", subvendor != PCI_ANY_ID, subvendor);
 448	ADD(alias, "sd", subdevice != PCI_ANY_ID, subdevice);
 449
 450	baseclass = (class) >> 16;
 451	baseclass_mask = (class_mask) >> 16;
 452	subclass = (class) >> 8;
 453	subclass_mask = (class_mask) >> 8;
 454	interface = class;
 455	interface_mask = class_mask;
 456
 457	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
 458	    || (subclass_mask != 0 && subclass_mask != 0xFF)
 459	    || (interface_mask != 0 && interface_mask != 0xFF)) {
 460		warn("Can't handle masks in %s:%04X\n",
 461		     filename, class_mask);
 462		return 0;
 463	}
 464
 465	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
 466	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
 467	ADD(alias, "i", interface_mask == 0xFF, interface);
 468	add_wildcard(alias);
 469	return 1;
 470}
 471
 472/* looks like: "ccw:tNmNdtNdmN" */
 473static int do_ccw_entry(const char *filename,
 474			void *symval, char *alias)
 475{
 476	DEF_FIELD(symval, ccw_device_id, match_flags);
 477	DEF_FIELD(symval, ccw_device_id, cu_type);
 478	DEF_FIELD(symval, ccw_device_id, cu_model);
 479	DEF_FIELD(symval, ccw_device_id, dev_type);
 480	DEF_FIELD(symval, ccw_device_id, dev_model);
 481
 482	strcpy(alias, "ccw:");
 483	ADD(alias, "t", match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
 484	    cu_type);
 485	ADD(alias, "m", match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
 486	    cu_model);
 487	ADD(alias, "dt", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
 488	    dev_type);
 489	ADD(alias, "dm", match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
 490	    dev_model);
 491	add_wildcard(alias);
 492	return 1;
 493}
 494
 495/* looks like: "ap:tN" */
 496static int do_ap_entry(const char *filename,
 497		       void *symval, char *alias)
 498{
 499	DEF_FIELD(symval, ap_device_id, dev_type);
 500
 501	sprintf(alias, "ap:t%02X*", dev_type);
 502	return 1;
 503}
 504
 505/* looks like: "css:tN" */
 506static int do_css_entry(const char *filename,
 507			void *symval, char *alias)
 508{
 509	DEF_FIELD(symval, css_device_id, type);
 510
 511	sprintf(alias, "css:t%01X", type);
 512	return 1;
 513}
 514
 515/* Looks like: "serio:tyNprNidNexN" */
 516static int do_serio_entry(const char *filename,
 517			  void *symval, char *alias)
 518{
 519	DEF_FIELD(symval, serio_device_id, type);
 520	DEF_FIELD(symval, serio_device_id, proto);
 521	DEF_FIELD(symval, serio_device_id, id);
 522	DEF_FIELD(symval, serio_device_id, extra);
 523
 524	strcpy(alias, "serio:");
 525	ADD(alias, "ty", type != SERIO_ANY, type);
 526	ADD(alias, "pr", proto != SERIO_ANY, proto);
 527	ADD(alias, "id", id != SERIO_ANY, id);
 528	ADD(alias, "ex", extra != SERIO_ANY, extra);
 529
 530	add_wildcard(alias);
 531	return 1;
 532}
 533
 534/* looks like: "acpi:ACPI0003" or "acpi:PNP0C0B" or "acpi:LNXVIDEO" or
 535 *             "acpi:bbsspp" (bb=base-class, ss=sub-class, pp=prog-if)
 536 *
 537 * NOTE: Each driver should use one of the following : _HID, _CIDs
 538 *       or _CLS. Also, bb, ss, and pp can be substituted with ??
 539 *       as don't care byte.
 540 */
 541static int do_acpi_entry(const char *filename,
 542			void *symval, char *alias)
 543{
 544	DEF_FIELD_ADDR(symval, acpi_device_id, id);
 545	DEF_FIELD_ADDR(symval, acpi_device_id, cls);
 546	DEF_FIELD_ADDR(symval, acpi_device_id, cls_msk);
 547
 548	if (id && strlen((const char *)*id))
 549		sprintf(alias, "acpi*:%s:*", *id);
 550	else if (cls) {
 551		int i, byte_shift, cnt = 0;
 552		unsigned int msk;
 553
 554		sprintf(&alias[cnt], "acpi*:");
 555		cnt = 6;
 556		for (i = 1; i <= 3; i++) {
 557			byte_shift = 8 * (3-i);
 558			msk = (*cls_msk >> byte_shift) & 0xFF;
 559			if (msk)
 560				sprintf(&alias[cnt], "%02x",
 561					(*cls >> byte_shift) & 0xFF);
 562			else
 563				sprintf(&alias[cnt], "??");
 564			cnt += 2;
 565		}
 566		sprintf(&alias[cnt], ":*");
 567	}
 568	return 1;
 569}
 570
 571/* looks like: "pnp:dD" */
 572static void do_pnp_device_entry(void *symval, unsigned long size,
 573				struct module *mod)
 574{
 575	const unsigned long id_size = SIZE_pnp_device_id;
 576	const unsigned int count = (size / id_size)-1;
 
 577	unsigned int i;
 578
 579	device_id_check(mod->name, "pnp", size, id_size, symval);
 580
 581	for (i = 0; i < count; i++) {
 582		DEF_FIELD_ADDR(symval + i*id_size, pnp_device_id, id);
 583		char acpi_id[sizeof(*id)];
 584		int j;
 585
 586		buf_printf(&mod->dev_table_buf,
 587			   "MODULE_ALIAS(\"pnp:d%s*\");\n", *id);
 588
 589		/* fix broken pnp bus lowercasing */
 590		for (j = 0; j < sizeof(acpi_id); j++)
 591			acpi_id[j] = toupper((*id)[j]);
 592		buf_printf(&mod->dev_table_buf,
 593			   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
 594	}
 595}
 596
 597/* looks like: "pnp:dD" for every device of the card */
 598static void do_pnp_card_entries(void *symval, unsigned long size,
 599				struct module *mod)
 600{
 601	const unsigned long id_size = SIZE_pnp_card_device_id;
 602	const unsigned int count = (size / id_size)-1;
 
 603	unsigned int i;
 604
 605	device_id_check(mod->name, "pnp", size, id_size, symval);
 606
 607	for (i = 0; i < count; i++) {
 608		unsigned int j;
 609		DEF_FIELD_ADDR(symval + i * id_size, pnp_card_device_id, devs);
 610
 611		for (j = 0; j < PNP_MAX_DEVICES; j++) {
 612			const char *id = (char *)(*devs)[j].id;
 613			int i2, j2;
 614			int dup = 0;
 615
 616			if (!id[0])
 617				break;
 618
 619			/* find duplicate, already added value */
 620			for (i2 = 0; i2 < i && !dup; i2++) {
 621				DEF_FIELD_ADDR_VAR(symval + i2 * id_size,
 622						   pnp_card_device_id,
 623						   devs, devs_dup);
 624
 625				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
 626					const char *id2 =
 627						(char *)(*devs_dup)[j2].id;
 628
 629					if (!id2[0])
 630						break;
 631
 632					if (!strcmp(id, id2)) {
 633						dup = 1;
 634						break;
 635					}
 636				}
 637			}
 638
 639			/* add an individual alias for every device entry */
 640			if (!dup) {
 641				char acpi_id[PNP_ID_LEN];
 642				int k;
 643
 644				buf_printf(&mod->dev_table_buf,
 645					   "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
 646
 647				/* fix broken pnp bus lowercasing */
 648				for (k = 0; k < sizeof(acpi_id); k++)
 649					acpi_id[k] = toupper(id[k]);
 650				buf_printf(&mod->dev_table_buf,
 651					   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
 652			}
 653		}
 654	}
 655}
 656
 657/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
 658static int do_pcmcia_entry(const char *filename,
 659			   void *symval, char *alias)
 660{
 661	unsigned int i;
 662	DEF_FIELD(symval, pcmcia_device_id, match_flags);
 663	DEF_FIELD(symval, pcmcia_device_id, manf_id);
 664	DEF_FIELD(symval, pcmcia_device_id, card_id);
 665	DEF_FIELD(symval, pcmcia_device_id, func_id);
 666	DEF_FIELD(symval, pcmcia_device_id, function);
 667	DEF_FIELD(symval, pcmcia_device_id, device_no);
 668	DEF_FIELD_ADDR(symval, pcmcia_device_id, prod_id_hash);
 669
 670	for (i=0; i<4; i++) {
 671		(*prod_id_hash)[i] = TO_NATIVE((*prod_id_hash)[i]);
 672	}
 673
 674	strcpy(alias, "pcmcia:");
 675	ADD(alias, "m", match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
 676	    manf_id);
 677	ADD(alias, "c", match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
 678	    card_id);
 679	ADD(alias, "f", match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
 680	    func_id);
 681	ADD(alias, "fn", match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
 682	    function);
 683	ADD(alias, "pfn", match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
 684	    device_no);
 685	ADD(alias, "pa", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, (*prod_id_hash)[0]);
 686	ADD(alias, "pb", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, (*prod_id_hash)[1]);
 687	ADD(alias, "pc", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, (*prod_id_hash)[2]);
 688	ADD(alias, "pd", match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, (*prod_id_hash)[3]);
 689
 690	add_wildcard(alias);
 691	return 1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 692}
 693
 694static int do_vio_entry(const char *filename, void *symval,
 695		char *alias)
 696{
 697	char *tmp;
 698	DEF_FIELD_ADDR(symval, vio_device_id, type);
 699	DEF_FIELD_ADDR(symval, vio_device_id, compat);
 700
 701	sprintf(alias, "vio:T%sS%s", (*type)[0] ? *type : "*",
 702			(*compat)[0] ? *compat : "*");
 703
 704	/* Replace all whitespace with underscores */
 705	for (tmp = alias; tmp && *tmp; tmp++)
 706		if (isspace (*tmp))
 707			*tmp = '_';
 708
 709	add_wildcard(alias);
 710	return 1;
 711}
 712
 713#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 714
 715static void do_input(char *alias,
 716		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
 717{
 718	unsigned int i;
 719
 720	for (i = min / BITS_PER_LONG; i < max / BITS_PER_LONG + 1; i++)
 721		arr[i] = TO_NATIVE(arr[i]);
 722	for (i = min; i < max; i++)
 723		if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
 724			sprintf(alias + strlen(alias), "%X,*", i);
 725}
 726
 727/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
 728static int do_input_entry(const char *filename, void *symval,
 729			  char *alias)
 730{
 731	DEF_FIELD(symval, input_device_id, flags);
 732	DEF_FIELD(symval, input_device_id, bustype);
 733	DEF_FIELD(symval, input_device_id, vendor);
 734	DEF_FIELD(symval, input_device_id, product);
 735	DEF_FIELD(symval, input_device_id, version);
 736	DEF_FIELD_ADDR(symval, input_device_id, evbit);
 737	DEF_FIELD_ADDR(symval, input_device_id, keybit);
 738	DEF_FIELD_ADDR(symval, input_device_id, relbit);
 739	DEF_FIELD_ADDR(symval, input_device_id, absbit);
 740	DEF_FIELD_ADDR(symval, input_device_id, mscbit);
 741	DEF_FIELD_ADDR(symval, input_device_id, ledbit);
 742	DEF_FIELD_ADDR(symval, input_device_id, sndbit);
 743	DEF_FIELD_ADDR(symval, input_device_id, ffbit);
 744	DEF_FIELD_ADDR(symval, input_device_id, swbit);
 745
 746	sprintf(alias, "input:");
 747
 748	ADD(alias, "b", flags & INPUT_DEVICE_ID_MATCH_BUS, bustype);
 749	ADD(alias, "v", flags & INPUT_DEVICE_ID_MATCH_VENDOR, vendor);
 750	ADD(alias, "p", flags & INPUT_DEVICE_ID_MATCH_PRODUCT, product);
 751	ADD(alias, "e", flags & INPUT_DEVICE_ID_MATCH_VERSION, version);
 752
 753	sprintf(alias + strlen(alias), "-e*");
 754	if (flags & INPUT_DEVICE_ID_MATCH_EVBIT)
 755		do_input(alias, *evbit, 0, INPUT_DEVICE_ID_EV_MAX);
 756	sprintf(alias + strlen(alias), "k*");
 757	if (flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
 758		do_input(alias, *keybit,
 759			 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
 760			 INPUT_DEVICE_ID_KEY_MAX);
 761	sprintf(alias + strlen(alias), "r*");
 762	if (flags & INPUT_DEVICE_ID_MATCH_RELBIT)
 763		do_input(alias, *relbit, 0, INPUT_DEVICE_ID_REL_MAX);
 764	sprintf(alias + strlen(alias), "a*");
 765	if (flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
 766		do_input(alias, *absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
 767	sprintf(alias + strlen(alias), "m*");
 768	if (flags & INPUT_DEVICE_ID_MATCH_MSCIT)
 769		do_input(alias, *mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
 770	sprintf(alias + strlen(alias), "l*");
 771	if (flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
 772		do_input(alias, *ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
 773	sprintf(alias + strlen(alias), "s*");
 774	if (flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
 775		do_input(alias, *sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
 776	sprintf(alias + strlen(alias), "f*");
 777	if (flags & INPUT_DEVICE_ID_MATCH_FFBIT)
 778		do_input(alias, *ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
 779	sprintf(alias + strlen(alias), "w*");
 780	if (flags & INPUT_DEVICE_ID_MATCH_SWBIT)
 781		do_input(alias, *swbit, 0, INPUT_DEVICE_ID_SW_MAX);
 782	return 1;
 783}
 784
 785static int do_eisa_entry(const char *filename, void *symval,
 786		char *alias)
 787{
 788	DEF_FIELD_ADDR(symval, eisa_device_id, sig);
 789	if (sig[0])
 790		sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", *sig);
 791	else
 792		strcat(alias, "*");
 793	return 1;
 794}
 795
 796/* Looks like: parisc:tNhvNrevNsvN */
 797static int do_parisc_entry(const char *filename, void *symval,
 798		char *alias)
 799{
 800	DEF_FIELD(symval, parisc_device_id, hw_type);
 801	DEF_FIELD(symval, parisc_device_id, hversion);
 802	DEF_FIELD(symval, parisc_device_id, hversion_rev);
 803	DEF_FIELD(symval, parisc_device_id, sversion);
 804
 805	strcpy(alias, "parisc:");
 806	ADD(alias, "t", hw_type != PA_HWTYPE_ANY_ID, hw_type);
 807	ADD(alias, "hv", hversion != PA_HVERSION_ANY_ID, hversion);
 808	ADD(alias, "rev", hversion_rev != PA_HVERSION_REV_ANY_ID, hversion_rev);
 809	ADD(alias, "sv", sversion != PA_SVERSION_ANY_ID, sversion);
 810
 811	add_wildcard(alias);
 812	return 1;
 813}
 814
 815/* Looks like: sdio:cNvNdN. */
 816static int do_sdio_entry(const char *filename,
 817			void *symval, char *alias)
 818{
 819	DEF_FIELD(symval, sdio_device_id, class);
 820	DEF_FIELD(symval, sdio_device_id, vendor);
 821	DEF_FIELD(symval, sdio_device_id, device);
 822
 823	strcpy(alias, "sdio:");
 824	ADD(alias, "c", class != (__u8)SDIO_ANY_ID, class);
 825	ADD(alias, "v", vendor != (__u16)SDIO_ANY_ID, vendor);
 826	ADD(alias, "d", device != (__u16)SDIO_ANY_ID, device);
 827	add_wildcard(alias);
 828	return 1;
 829}
 830
 831/* Looks like: ssb:vNidNrevN. */
 832static int do_ssb_entry(const char *filename,
 833			void *symval, char *alias)
 834{
 835	DEF_FIELD(symval, ssb_device_id, vendor);
 836	DEF_FIELD(symval, ssb_device_id, coreid);
 837	DEF_FIELD(symval, ssb_device_id, revision);
 838
 839	strcpy(alias, "ssb:");
 840	ADD(alias, "v", vendor != SSB_ANY_VENDOR, vendor);
 841	ADD(alias, "id", coreid != SSB_ANY_ID, coreid);
 842	ADD(alias, "rev", revision != SSB_ANY_REV, revision);
 843	add_wildcard(alias);
 844	return 1;
 845}
 846
 847/* Looks like: bcma:mNidNrevNclN. */
 848static int do_bcma_entry(const char *filename,
 849			 void *symval, char *alias)
 850{
 851	DEF_FIELD(symval, bcma_device_id, manuf);
 852	DEF_FIELD(symval, bcma_device_id, id);
 853	DEF_FIELD(symval, bcma_device_id, rev);
 854	DEF_FIELD(symval, bcma_device_id, class);
 855
 856	strcpy(alias, "bcma:");
 857	ADD(alias, "m", manuf != BCMA_ANY_MANUF, manuf);
 858	ADD(alias, "id", id != BCMA_ANY_ID, id);
 859	ADD(alias, "rev", rev != BCMA_ANY_REV, rev);
 860	ADD(alias, "cl", class != BCMA_ANY_CLASS, class);
 861	add_wildcard(alias);
 862	return 1;
 863}
 864
 865/* Looks like: virtio:dNvN */
 866static int do_virtio_entry(const char *filename, void *symval,
 867			   char *alias)
 868{
 869	DEF_FIELD(symval, virtio_device_id, device);
 870	DEF_FIELD(symval, virtio_device_id, vendor);
 871
 872	strcpy(alias, "virtio:");
 873	ADD(alias, "d", device != VIRTIO_DEV_ANY_ID, device);
 874	ADD(alias, "v", vendor != VIRTIO_DEV_ANY_ID, vendor);
 875
 876	add_wildcard(alias);
 877	return 1;
 878}
 879
 880/*
 881 * Looks like: vmbus:guid
 882 * Each byte of the guid will be represented by two hex characters
 883 * in the name.
 884 */
 885
 886static int do_vmbus_entry(const char *filename, void *symval,
 887			  char *alias)
 888{
 889	int i;
 890	DEF_FIELD_ADDR(symval, hv_vmbus_device_id, guid);
 891	char guid_name[(sizeof(*guid) + 1) * 2];
 892
 893	for (i = 0; i < (sizeof(*guid) * 2); i += 2)
 894		sprintf(&guid_name[i], "%02x", TO_NATIVE((guid->b)[i/2]));
 895
 896	strcpy(alias, "vmbus:");
 897	strcat(alias, guid_name);
 898
 899	return 1;
 900}
 901
 902/* Looks like: rpmsg:S */
 903static int do_rpmsg_entry(const char *filename, void *symval,
 904			  char *alias)
 905{
 906	DEF_FIELD_ADDR(symval, rpmsg_device_id, name);
 907	sprintf(alias, RPMSG_DEVICE_MODALIAS_FMT, *name);
 908
 909	return 1;
 910}
 911
 912/* Looks like: i2c:S */
 913static int do_i2c_entry(const char *filename, void *symval,
 914			char *alias)
 915{
 916	DEF_FIELD_ADDR(symval, i2c_device_id, name);
 917	sprintf(alias, I2C_MODULE_PREFIX "%s", *name);
 918
 919	return 1;
 920}
 921
 922static int do_i3c_entry(const char *filename, void *symval,
 923			char *alias)
 924{
 925	DEF_FIELD(symval, i3c_device_id, match_flags);
 926	DEF_FIELD(symval, i3c_device_id, dcr);
 927	DEF_FIELD(symval, i3c_device_id, manuf_id);
 928	DEF_FIELD(symval, i3c_device_id, part_id);
 929	DEF_FIELD(symval, i3c_device_id, extra_info);
 930
 931	strcpy(alias, "i3c:");
 932	ADD(alias, "dcr", match_flags & I3C_MATCH_DCR, dcr);
 933	ADD(alias, "manuf", match_flags & I3C_MATCH_MANUF, manuf_id);
 934	ADD(alias, "part", match_flags & I3C_MATCH_PART, part_id);
 935	ADD(alias, "ext", match_flags & I3C_MATCH_EXTRA_INFO, extra_info);
 936
 937	return 1;
 938}
 939
 940/* Looks like: spi:S */
 941static int do_spi_entry(const char *filename, void *symval,
 942			char *alias)
 943{
 944	DEF_FIELD_ADDR(symval, spi_device_id, name);
 945	sprintf(alias, SPI_MODULE_PREFIX "%s", *name);
 946
 947	return 1;
 948}
 949
 950static const struct dmifield {
 951	const char *prefix;
 952	int field;
 953} dmi_fields[] = {
 954	{ "bvn", DMI_BIOS_VENDOR },
 955	{ "bvr", DMI_BIOS_VERSION },
 956	{ "bd",  DMI_BIOS_DATE },
 957	{ "br",  DMI_BIOS_RELEASE },
 958	{ "efr", DMI_EC_FIRMWARE_RELEASE },
 959	{ "svn", DMI_SYS_VENDOR },
 960	{ "pn",  DMI_PRODUCT_NAME },
 961	{ "pvr", DMI_PRODUCT_VERSION },
 962	{ "rvn", DMI_BOARD_VENDOR },
 963	{ "rn",  DMI_BOARD_NAME },
 964	{ "rvr", DMI_BOARD_VERSION },
 965	{ "cvn", DMI_CHASSIS_VENDOR },
 966	{ "ct",  DMI_CHASSIS_TYPE },
 967	{ "cvr", DMI_CHASSIS_VERSION },
 968	{ NULL,  DMI_NONE }
 969};
 970
 971static void dmi_ascii_filter(char *d, const char *s)
 972{
 973	/* Filter out characters we don't want to see in the modalias string */
 974	for (; *s; s++)
 975		if (*s > ' ' && *s < 127 && *s != ':')
 976			*(d++) = *s;
 977
 978	*d = 0;
 979}
 980
 981
 982static int do_dmi_entry(const char *filename, void *symval,
 983			char *alias)
 984{
 985	int i, j;
 986	DEF_FIELD_ADDR(symval, dmi_system_id, matches);
 987	sprintf(alias, "dmi*");
 988
 989	for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
 990		for (j = 0; j < 4; j++) {
 991			if ((*matches)[j].slot &&
 992			    (*matches)[j].slot == dmi_fields[i].field) {
 993				sprintf(alias + strlen(alias), ":%s*",
 994					dmi_fields[i].prefix);
 995				dmi_ascii_filter(alias + strlen(alias),
 996						 (*matches)[j].substr);
 997				strcat(alias, "*");
 998			}
 999		}
1000	}
1001
1002	strcat(alias, ":");
1003	return 1;
1004}
1005
1006static int do_platform_entry(const char *filename,
1007			     void *symval, char *alias)
1008{
1009	DEF_FIELD_ADDR(symval, platform_device_id, name);
1010	sprintf(alias, PLATFORM_MODULE_PREFIX "%s", *name);
1011	return 1;
1012}
1013
1014static int do_mdio_entry(const char *filename,
1015			 void *symval, char *alias)
1016{
1017	int i;
1018	DEF_FIELD(symval, mdio_device_id, phy_id);
1019	DEF_FIELD(symval, mdio_device_id, phy_id_mask);
1020
1021	alias += sprintf(alias, MDIO_MODULE_PREFIX);
1022
1023	for (i = 0; i < 32; i++) {
1024		if (!((phy_id_mask >> (31-i)) & 1))
1025			*(alias++) = '?';
1026		else if ((phy_id >> (31-i)) & 1)
1027			*(alias++) = '1';
1028		else
1029			*(alias++) = '0';
1030	}
1031
1032	/* Terminate the string */
1033	*alias = 0;
1034
1035	return 1;
1036}
1037
1038/* Looks like: zorro:iN. */
1039static int do_zorro_entry(const char *filename, void *symval,
1040			  char *alias)
1041{
1042	DEF_FIELD(symval, zorro_device_id, id);
1043	strcpy(alias, "zorro:");
1044	ADD(alias, "i", id != ZORRO_WILDCARD, id);
1045	return 1;
1046}
1047
1048/* looks like: "pnp:dD" */
1049static int do_isapnp_entry(const char *filename,
1050			   void *symval, char *alias)
1051{
1052	DEF_FIELD(symval, isapnp_device_id, vendor);
1053	DEF_FIELD(symval, isapnp_device_id, function);
1054	sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
1055		'A' + ((vendor >> 2) & 0x3f) - 1,
1056		'A' + (((vendor & 3) << 3) | ((vendor >> 13) & 7)) - 1,
1057		'A' + ((vendor >> 8) & 0x1f) - 1,
1058		(function >> 4) & 0x0f, function & 0x0f,
1059		(function >> 12) & 0x0f, (function >> 8) & 0x0f);
1060	return 1;
1061}
1062
1063/* Looks like: "ipack:fNvNdN". */
1064static int do_ipack_entry(const char *filename,
1065			  void *symval, char *alias)
1066{
1067	DEF_FIELD(symval, ipack_device_id, format);
1068	DEF_FIELD(symval, ipack_device_id, vendor);
1069	DEF_FIELD(symval, ipack_device_id, device);
1070	strcpy(alias, "ipack:");
1071	ADD(alias, "f", format != IPACK_ANY_FORMAT, format);
1072	ADD(alias, "v", vendor != IPACK_ANY_ID, vendor);
1073	ADD(alias, "d", device != IPACK_ANY_ID, device);
1074	add_wildcard(alias);
1075	return 1;
1076}
1077
1078/*
1079 * Append a match expression for a single masked hex digit.
1080 * outp points to a pointer to the character at which to append.
1081 *	*outp is updated on return to point just after the appended text,
1082 *	to facilitate further appending.
1083 */
1084static void append_nibble_mask(char **outp,
1085			       unsigned int nibble, unsigned int mask)
1086{
1087	char *p = *outp;
1088	unsigned int i;
1089
1090	switch (mask) {
1091	case 0:
1092		*p++ = '?';
1093		break;
1094
1095	case 0xf:
1096		p += sprintf(p, "%X",  nibble);
1097		break;
1098
1099	default:
1100		/*
1101		 * Dumbly emit a match pattern for all possible matching
1102		 * digits.  This could be improved in some cases using ranges,
1103		 * but it has the advantage of being trivially correct, and is
1104		 * often optimal.
1105		 */
1106		*p++ = '[';
1107		for (i = 0; i < 0x10; i++)
1108			if ((i & mask) == nibble)
1109				p += sprintf(p, "%X", i);
1110		*p++ = ']';
1111	}
1112
1113	/* Ensure that the string remains NUL-terminated: */
1114	*p = '\0';
1115
1116	/* Advance the caller's end-of-string pointer: */
1117	*outp = p;
1118}
1119
1120/*
1121 * looks like: "amba:dN"
1122 *
1123 * N is exactly 8 digits, where each is an upper-case hex digit, or
1124 *	a ? or [] pattern matching exactly one digit.
1125 */
1126static int do_amba_entry(const char *filename,
1127			 void *symval, char *alias)
1128{
1129	unsigned int digit;
1130	char *p = alias;
1131	DEF_FIELD(symval, amba_id, id);
1132	DEF_FIELD(symval, amba_id, mask);
1133
1134	if ((id & mask) != id)
1135		fatal("%s: Masked-off bit(s) of AMBA device ID are non-zero: "
1136		      "id=0x%08X, mask=0x%08X.  Please fix this driver.\n",
1137		      filename, id, mask);
1138
1139	p += sprintf(alias, "amba:d");
1140	for (digit = 0; digit < 8; digit++)
1141		append_nibble_mask(&p,
1142				   (id >> (4 * (7 - digit))) & 0xf,
1143				   (mask >> (4 * (7 - digit))) & 0xf);
1144
1145	return 1;
1146}
1147
1148/*
1149 * looks like: "mipscdmm:tN"
1150 *
1151 * N is exactly 2 digits, where each is an upper-case hex digit, or
1152 *	a ? or [] pattern matching exactly one digit.
1153 */
1154static int do_mips_cdmm_entry(const char *filename,
1155			      void *symval, char *alias)
1156{
1157	DEF_FIELD(symval, mips_cdmm_device_id, type);
1158
1159	sprintf(alias, "mipscdmm:t%02X*", type);
1160	return 1;
1161}
1162
1163/* LOOKS like cpu:type:x86,venVVVVfamFFFFmodMMMM:feature:*,FEAT,*
1164 * All fields are numbers. It would be nicer to use strings for vendor
1165 * and feature, but getting those out of the build system here is too
1166 * complicated.
1167 */
1168
1169static int do_x86cpu_entry(const char *filename, void *symval,
1170			   char *alias)
1171{
1172	DEF_FIELD(symval, x86_cpu_id, feature);
1173	DEF_FIELD(symval, x86_cpu_id, family);
1174	DEF_FIELD(symval, x86_cpu_id, model);
1175	DEF_FIELD(symval, x86_cpu_id, vendor);
1176
1177	strcpy(alias, "cpu:type:x86,");
1178	ADD(alias, "ven", vendor != X86_VENDOR_ANY, vendor);
1179	ADD(alias, "fam", family != X86_FAMILY_ANY, family);
1180	ADD(alias, "mod", model  != X86_MODEL_ANY,  model);
1181	strcat(alias, ":feature:*");
1182	if (feature != X86_FEATURE_ANY)
1183		sprintf(alias + strlen(alias), "%04X*", feature);
1184	return 1;
1185}
1186
1187/* LOOKS like cpu:type:*:feature:*FEAT* */
1188static int do_cpu_entry(const char *filename, void *symval, char *alias)
1189{
1190	DEF_FIELD(symval, cpu_feature, feature);
1191
1192	sprintf(alias, "cpu:type:*:feature:*%04X*", feature);
1193	return 1;
1194}
1195
1196/* Looks like: mei:S:uuid:N:* */
1197static int do_mei_entry(const char *filename, void *symval,
1198			char *alias)
1199{
1200	DEF_FIELD_ADDR(symval, mei_cl_device_id, name);
1201	DEF_FIELD_ADDR(symval, mei_cl_device_id, uuid);
1202	DEF_FIELD(symval, mei_cl_device_id, version);
1203
1204	sprintf(alias, MEI_CL_MODULE_PREFIX);
1205	sprintf(alias + strlen(alias), "%s:",  (*name)[0]  ? *name : "*");
1206	add_uuid(alias, *uuid);
1207	ADD(alias, ":", version != MEI_CL_VERSION_ANY, version);
1208
1209	strcat(alias, ":*");
1210
1211	return 1;
1212}
1213
1214/* Looks like: rapidio:vNdNavNadN */
1215static int do_rio_entry(const char *filename,
1216			void *symval, char *alias)
1217{
1218	DEF_FIELD(symval, rio_device_id, did);
1219	DEF_FIELD(symval, rio_device_id, vid);
1220	DEF_FIELD(symval, rio_device_id, asm_did);
1221	DEF_FIELD(symval, rio_device_id, asm_vid);
1222
1223	strcpy(alias, "rapidio:");
1224	ADD(alias, "v", vid != RIO_ANY_ID, vid);
1225	ADD(alias, "d", did != RIO_ANY_ID, did);
1226	ADD(alias, "av", asm_vid != RIO_ANY_ID, asm_vid);
1227	ADD(alias, "ad", asm_did != RIO_ANY_ID, asm_did);
1228
1229	add_wildcard(alias);
1230	return 1;
1231}
1232
1233/* Looks like: ulpi:vNpN */
1234static int do_ulpi_entry(const char *filename, void *symval,
1235			 char *alias)
1236{
1237	DEF_FIELD(symval, ulpi_device_id, vendor);
1238	DEF_FIELD(symval, ulpi_device_id, product);
1239
1240	sprintf(alias, "ulpi:v%04xp%04x", vendor, product);
1241
1242	return 1;
1243}
1244
1245/* Looks like: hdaudio:vNrNaN */
1246static int do_hda_entry(const char *filename, void *symval, char *alias)
1247{
1248	DEF_FIELD(symval, hda_device_id, vendor_id);
1249	DEF_FIELD(symval, hda_device_id, rev_id);
1250	DEF_FIELD(symval, hda_device_id, api_version);
1251
1252	strcpy(alias, "hdaudio:");
1253	ADD(alias, "v", vendor_id != 0, vendor_id);
1254	ADD(alias, "r", rev_id != 0, rev_id);
1255	ADD(alias, "a", api_version != 0, api_version);
1256
1257	add_wildcard(alias);
1258	return 1;
1259}
1260
1261/* Looks like: sdw:mNpNvNcN */
1262static int do_sdw_entry(const char *filename, void *symval, char *alias)
1263{
1264	DEF_FIELD(symval, sdw_device_id, mfg_id);
1265	DEF_FIELD(symval, sdw_device_id, part_id);
1266	DEF_FIELD(symval, sdw_device_id, sdw_version);
1267	DEF_FIELD(symval, sdw_device_id, class_id);
1268
1269	strcpy(alias, "sdw:");
1270	ADD(alias, "m", mfg_id != 0, mfg_id);
1271	ADD(alias, "p", part_id != 0, part_id);
1272	ADD(alias, "v", sdw_version != 0, sdw_version);
1273	ADD(alias, "c", class_id != 0, class_id);
1274
1275	add_wildcard(alias);
1276	return 1;
1277}
1278
1279/* Looks like: fsl-mc:vNdN */
1280static int do_fsl_mc_entry(const char *filename, void *symval,
1281			   char *alias)
1282{
1283	DEF_FIELD(symval, fsl_mc_device_id, vendor);
1284	DEF_FIELD_ADDR(symval, fsl_mc_device_id, obj_type);
1285
1286	sprintf(alias, "fsl-mc:v%08Xd%s", vendor, *obj_type);
1287	return 1;
1288}
1289
1290/* Looks like: tbsvc:kSpNvNrN */
1291static int do_tbsvc_entry(const char *filename, void *symval, char *alias)
1292{
1293	DEF_FIELD(symval, tb_service_id, match_flags);
1294	DEF_FIELD_ADDR(symval, tb_service_id, protocol_key);
1295	DEF_FIELD(symval, tb_service_id, protocol_id);
1296	DEF_FIELD(symval, tb_service_id, protocol_version);
1297	DEF_FIELD(symval, tb_service_id, protocol_revision);
1298
1299	strcpy(alias, "tbsvc:");
1300	if (match_flags & TBSVC_MATCH_PROTOCOL_KEY)
1301		sprintf(alias + strlen(alias), "k%s", *protocol_key);
1302	else
1303		strcat(alias + strlen(alias), "k*");
1304	ADD(alias, "p", match_flags & TBSVC_MATCH_PROTOCOL_ID, protocol_id);
1305	ADD(alias, "v", match_flags & TBSVC_MATCH_PROTOCOL_VERSION,
1306	    protocol_version);
1307	ADD(alias, "r", match_flags & TBSVC_MATCH_PROTOCOL_REVISION,
1308	    protocol_revision);
1309
1310	add_wildcard(alias);
1311	return 1;
1312}
1313
1314/* Looks like: typec:idNmN */
1315static int do_typec_entry(const char *filename, void *symval, char *alias)
1316{
1317	DEF_FIELD(symval, typec_device_id, svid);
1318	DEF_FIELD(symval, typec_device_id, mode);
1319
1320	sprintf(alias, "typec:id%04X", svid);
1321	ADD(alias, "m", mode != TYPEC_ANY_MODE, mode);
1322
1323	return 1;
1324}
1325
1326/* Looks like: tee:uuid */
1327static int do_tee_entry(const char *filename, void *symval, char *alias)
1328{
1329	DEF_FIELD(symval, tee_client_device_id, uuid);
1330
1331	sprintf(alias, "tee:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1332		uuid.b[0], uuid.b[1], uuid.b[2], uuid.b[3], uuid.b[4],
1333		uuid.b[5], uuid.b[6], uuid.b[7], uuid.b[8], uuid.b[9],
1334		uuid.b[10], uuid.b[11], uuid.b[12], uuid.b[13], uuid.b[14],
1335		uuid.b[15]);
1336
1337	add_wildcard(alias);
1338	return 1;
1339}
1340
1341/* Looks like: wmi:guid */
1342static int do_wmi_entry(const char *filename, void *symval, char *alias)
1343{
1344	int len;
1345	DEF_FIELD_ADDR(symval, wmi_device_id, guid_string);
1346
1347	if (strlen(*guid_string) != UUID_STRING_LEN) {
1348		warn("Invalid WMI device id 'wmi:%s' in '%s'\n",
1349				*guid_string, filename);
1350		return 0;
1351	}
1352
1353	len = snprintf(alias, ALIAS_SIZE, WMI_MODULE_PREFIX "%s", *guid_string);
1354	if (len < 0 || len >= ALIAS_SIZE) {
1355		warn("Could not generate all MODULE_ALIAS's in '%s'\n",
1356				filename);
1357		return 0;
1358	}
1359	return 1;
1360}
1361
1362/* Looks like: mhi:S */
1363static int do_mhi_entry(const char *filename, void *symval, char *alias)
1364{
1365	DEF_FIELD_ADDR(symval, mhi_device_id, chan);
1366	sprintf(alias, MHI_DEVICE_MODALIAS_FMT, *chan);
1367
1368	return 1;
1369}
1370
1371/* Does namelen bytes of name exactly match the symbol? */
1372static bool sym_is(const char *name, unsigned namelen, const char *symbol)
1373{
1374	if (namelen != strlen(symbol))
1375		return false;
1376
1377	return memcmp(name, symbol, namelen) == 0;
1378}
1379
1380static void do_table(void *symval, unsigned long size,
1381		     unsigned long id_size,
1382		     const char *device_id,
1383		     int (*do_entry)(const char *filename, void *symval, char *alias),
1384		     struct module *mod)
1385{
1386	unsigned int i;
1387	char alias[ALIAS_SIZE];
 
1388
1389	device_id_check(mod->name, device_id, size, id_size, symval);
1390	/* Leave last one: it's the terminator. */
1391	size -= id_size;
1392
1393	for (i = 0; i < size; i += id_size) {
1394		if (do_entry(mod->name, symval+i, alias)) {
1395			buf_printf(&mod->dev_table_buf,
1396				   "MODULE_ALIAS(\"%s\");\n", alias);
1397		}
1398	}
1399}
1400
1401static const struct devtable devtable[] = {
1402	{"hid", SIZE_hid_device_id, do_hid_entry},
1403	{"ieee1394", SIZE_ieee1394_device_id, do_ieee1394_entry},
1404	{"pci", SIZE_pci_device_id, do_pci_entry},
1405	{"ccw", SIZE_ccw_device_id, do_ccw_entry},
1406	{"ap", SIZE_ap_device_id, do_ap_entry},
1407	{"css", SIZE_css_device_id, do_css_entry},
1408	{"serio", SIZE_serio_device_id, do_serio_entry},
1409	{"acpi", SIZE_acpi_device_id, do_acpi_entry},
1410	{"pcmcia", SIZE_pcmcia_device_id, do_pcmcia_entry},
1411	{"vio", SIZE_vio_device_id, do_vio_entry},
1412	{"input", SIZE_input_device_id, do_input_entry},
1413	{"eisa", SIZE_eisa_device_id, do_eisa_entry},
1414	{"parisc", SIZE_parisc_device_id, do_parisc_entry},
1415	{"sdio", SIZE_sdio_device_id, do_sdio_entry},
1416	{"ssb", SIZE_ssb_device_id, do_ssb_entry},
1417	{"bcma", SIZE_bcma_device_id, do_bcma_entry},
1418	{"virtio", SIZE_virtio_device_id, do_virtio_entry},
1419	{"vmbus", SIZE_hv_vmbus_device_id, do_vmbus_entry},
1420	{"rpmsg", SIZE_rpmsg_device_id, do_rpmsg_entry},
1421	{"i2c", SIZE_i2c_device_id, do_i2c_entry},
1422	{"i3c", SIZE_i3c_device_id, do_i3c_entry},
1423	{"spi", SIZE_spi_device_id, do_spi_entry},
1424	{"dmi", SIZE_dmi_system_id, do_dmi_entry},
1425	{"platform", SIZE_platform_device_id, do_platform_entry},
1426	{"mdio", SIZE_mdio_device_id, do_mdio_entry},
1427	{"zorro", SIZE_zorro_device_id, do_zorro_entry},
1428	{"isapnp", SIZE_isapnp_device_id, do_isapnp_entry},
1429	{"ipack", SIZE_ipack_device_id, do_ipack_entry},
1430	{"amba", SIZE_amba_id, do_amba_entry},
1431	{"mipscdmm", SIZE_mips_cdmm_device_id, do_mips_cdmm_entry},
1432	{"x86cpu", SIZE_x86_cpu_id, do_x86cpu_entry},
1433	{"cpu", SIZE_cpu_feature, do_cpu_entry},
1434	{"mei", SIZE_mei_cl_device_id, do_mei_entry},
1435	{"rapidio", SIZE_rio_device_id, do_rio_entry},
1436	{"ulpi", SIZE_ulpi_device_id, do_ulpi_entry},
1437	{"hdaudio", SIZE_hda_device_id, do_hda_entry},
1438	{"sdw", SIZE_sdw_device_id, do_sdw_entry},
1439	{"fslmc", SIZE_fsl_mc_device_id, do_fsl_mc_entry},
1440	{"tbsvc", SIZE_tb_service_id, do_tbsvc_entry},
1441	{"typec", SIZE_typec_device_id, do_typec_entry},
1442	{"tee", SIZE_tee_client_device_id, do_tee_entry},
1443	{"wmi", SIZE_wmi_device_id, do_wmi_entry},
1444	{"mhi", SIZE_mhi_device_id, do_mhi_entry},
1445};
1446
1447/* Create MODULE_ALIAS() statements.
1448 * At this time, we cannot write the actual output C source yet,
1449 * so we write into the mod->dev_table_buf buffer. */
1450void handle_moddevtable(struct module *mod, struct elf_info *info,
1451			Elf_Sym *sym, const char *symname)
1452{
1453	void *symval;
1454	char *zeros = NULL;
1455	const char *name, *identifier;
1456	unsigned int namelen;
1457
1458	/* We're looking for a section relative symbol */
1459	if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
1460		return;
1461
1462	/* We're looking for an object */
1463	if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT)
1464		return;
1465
1466	/* All our symbols are of form __mod_<name>__<identifier>_device_table. */
1467	if (strncmp(symname, "__mod_", strlen("__mod_")))
1468		return;
1469	name = symname + strlen("__mod_");
1470	namelen = strlen(name);
1471	if (namelen < strlen("_device_table"))
1472		return;
1473	if (strcmp(name + namelen - strlen("_device_table"), "_device_table"))
1474		return;
1475	identifier = strstr(name, "__");
1476	if (!identifier)
1477		return;
1478	namelen = identifier - name;
1479
1480	/* Handle all-NULL symbols allocated into .bss */
1481	if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
1482		zeros = calloc(1, sym->st_size);
1483		symval = zeros;
1484	} else {
1485		symval = (void *)info->hdr
1486			+ info->sechdrs[get_secindex(info, sym)].sh_offset
1487			+ sym->st_value;
1488	}
1489
1490	/* First handle the "special" cases */
1491	if (sym_is(name, namelen, "usb"))
 
 
 
 
1492		do_usb_table(symval, sym->st_size, mod);
1493	if (sym_is(name, namelen, "of"))
1494		do_of_table(symval, sym->st_size, mod);
1495	else if (sym_is(name, namelen, "pnp"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1496		do_pnp_device_entry(symval, sym->st_size, mod);
1497	else if (sym_is(name, namelen, "pnp_card"))
1498		do_pnp_card_entries(symval, sym->st_size, mod);
1499	else {
1500		int i;
1501
1502		for (i = 0; i < ARRAY_SIZE(devtable); i++) {
1503			const struct devtable *p = &devtable[i];
1504
1505			if (sym_is(name, namelen, p->device_id)) {
1506				do_table(symval, sym->st_size, p->id_size,
1507					 p->device_id, p->do_entry, mod);
1508				break;
1509			}
1510		}
1511	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1512	free(zeros);
1513}
1514
1515/* Now add out buffered information to the generated C source */
1516void add_moddevtable(struct buffer *buf, struct module *mod)
1517{
1518	buf_printf(buf, "\n");
1519	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1520	free(mod->dev_table_buf.p);
1521}
v3.1
   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
  32typedef uint32_t	__u32;
  33typedef uint16_t	__u16;
  34typedef unsigned char	__u8;
 
 
 
 
 
 
 
 
 
 
 
 
  35
  36/* Big exception to the "don't include kernel headers into userspace, which
  37 * even potentially has different endianness and word sizes, since
  38 * we handle those differences explicitly below */
  39#include "../../include/linux/mod_devicetable.h"
  40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  41#define ADD(str, sep, cond, field)                              \
  42do {                                                            \
  43        strcat(str, sep);                                       \
  44        if (cond)                                               \
  45                sprintf(str + strlen(str),                      \
  46                        sizeof(field) == 1 ? "%02X" :           \
  47                        sizeof(field) == 2 ? "%04X" :           \
  48                        sizeof(field) == 4 ? "%08X" : "",       \
  49                        field);                                 \
  50        else                                                    \
  51                sprintf(str + strlen(str), "*");                \
  52} while(0)
  53
  54/* Always end in a wildcard, for future extension */
  55static inline void add_wildcard(char *str)
  56{
  57	int len = strlen(str);
  58
  59	if (str[len - 1] != '*')
  60		strcat(str + len, "*");
  61}
  62
  63unsigned int cross_build = 0;
 
 
 
 
 
 
 
 
 
 
  64/**
  65 * Check that sizeof(device_id type) are consistent with size of section
  66 * in .o file. If in-consistent then userspace and kernel does not agree
  67 * on actual size which is a bug.
  68 * Also verify that the final entry in the table is all zeros.
  69 * Ignore both checks if build host differ from target host and size differs.
  70 **/
  71static void device_id_check(const char *modname, const char *device_id,
  72			    unsigned long size, unsigned long id_size,
  73			    void *symval)
  74{
  75	int i;
  76
  77	if (size % id_size || size < id_size) {
  78		if (cross_build != 0)
  79			return;
  80		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
  81		      "of the size of section __mod_%s_device_table=%lu.\n"
 
  82		      "Fix definition of struct %s_device_id "
  83		      "in mod_devicetable.h\n",
  84		      modname, device_id, id_size, device_id, size, device_id);
  85	}
  86	/* Verify last one is a terminator */
  87	for (i = 0; i < id_size; i++ ) {
  88		if (*(uint8_t*)(symval+size-id_size+i)) {
  89			fprintf(stderr,"%s: struct %s_device_id is %lu bytes.  "
  90				"The last of %lu is:\n",
  91				modname, device_id, id_size, size / id_size);
  92			for (i = 0; i < id_size; i++ )
  93				fprintf(stderr,"0x%02x ",
  94					*(uint8_t*)(symval+size-id_size+i) );
  95			fprintf(stderr,"\n");
  96			fatal("%s: struct %s_device_id is not terminated "
  97				"with a NULL entry!\n", modname, device_id);
  98		}
  99	}
 100}
 101
 102/* USB is special because the bcdDevice can be matched against a numeric range */
 103/* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */
 104static void do_usb_entry(struct usb_device_id *id,
 105			 unsigned int bcdDevice_initial, int bcdDevice_initial_digits,
 106			 unsigned char range_lo, unsigned char range_hi,
 107			 unsigned char max, struct module *mod)
 108{
 109	char alias[500];
 
 
 
 
 
 
 
 
 
 
 
 
 110	strcpy(alias, "usb:");
 111	ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR,
 112	    id->idVendor);
 113	ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT,
 114	    id->idProduct);
 115
 116	strcat(alias, "d");
 117	if (bcdDevice_initial_digits)
 118		sprintf(alias + strlen(alias), "%0*X",
 119			bcdDevice_initial_digits, bcdDevice_initial);
 120	if (range_lo == range_hi)
 121		sprintf(alias + strlen(alias), "%X", range_lo);
 122	else if (range_lo > 0 || range_hi < max) {
 123		if (range_lo > 0x9 || range_hi < 0xA)
 124			sprintf(alias + strlen(alias),
 125				"[%X-%X]",
 126				range_lo,
 127				range_hi);
 128		else {
 129			sprintf(alias + strlen(alias),
 130				range_lo < 0x9 ? "[%X-9" : "[%X",
 131				range_lo);
 132			sprintf(alias + strlen(alias),
 133				range_hi > 0xA ? "a-%X]" : "%X]",
 134				range_lo);
 135		}
 136	}
 137	if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1))
 138		strcat(alias, "*");
 139
 140	ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS,
 141	    id->bDeviceClass);
 142	ADD(alias, "dsc",
 143	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS,
 144	    id->bDeviceSubClass);
 145	ADD(alias, "dp",
 146	    id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL,
 147	    id->bDeviceProtocol);
 148	ADD(alias, "ic",
 149	    id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS,
 150	    id->bInterfaceClass);
 151	ADD(alias, "isc",
 152	    id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS,
 153	    id->bInterfaceSubClass);
 154	ADD(alias, "ip",
 155	    id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL,
 156	    id->bInterfaceProtocol);
 157
 158	add_wildcard(alias);
 159	buf_printf(&mod->dev_table_buf,
 160		   "MODULE_ALIAS(\"%s\");\n", alias);
 161}
 162
 163/* Handles increment/decrement of BCD formatted integers */
 164/* Returns the previous value, so it works like i++ or i-- */
 165static unsigned int incbcd(unsigned int *bcd,
 166			   int inc,
 167			   unsigned char max,
 168			   size_t chars)
 169{
 170	unsigned int init = *bcd, i, j;
 171	unsigned long long c, dec = 0;
 172
 173	/* If bcd is not in BCD format, just increment */
 174	if (max > 0x9) {
 175		*bcd += inc;
 176		return init;
 177	}
 178
 179	/* Convert BCD to Decimal */
 180	for (i=0 ; i < chars ; i++) {
 181		c = (*bcd >> (i << 2)) & 0xf;
 182		c = c > 9 ? 9 : c; /* force to bcd just in case */
 183		for (j=0 ; j < i ; j++)
 184			c = c * 10;
 185		dec += c;
 186	}
 187
 188	/* Do our increment/decrement */
 189	dec += inc;
 190	*bcd  = 0;
 191
 192	/* Convert back to BCD */
 193	for (i=0 ; i < chars ; i++) {
 194		for (c=1,j=0 ; j < i ; j++)
 195			c = c * 10;
 196		c = (dec / c) % 10;
 197		*bcd += c << (i << 2);
 198	}
 199	return init;
 200}
 201
 202static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod)
 203{
 204	unsigned int devlo, devhi;
 205	unsigned char chi, clo, max;
 206	int ndigits;
 207
 208	id->match_flags = TO_NATIVE(id->match_flags);
 209	id->idVendor = TO_NATIVE(id->idVendor);
 210	id->idProduct = TO_NATIVE(id->idProduct);
 211
 212	devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ?
 213		TO_NATIVE(id->bcdDevice_lo) : 0x0U;
 214	devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ?
 215		TO_NATIVE(id->bcdDevice_hi) : ~0x0U;
 
 
 
 
 216
 217	/* Figure out if this entry is in bcd or hex format */
 218	max = 0x9; /* Default to decimal format */
 219	for (ndigits = 0 ; ndigits < sizeof(id->bcdDevice_lo) * 2 ; ndigits++) {
 220		clo = (devlo >> (ndigits << 2)) & 0xf;
 221		chi = ((devhi > 0x9999 ? 0x9999 : devhi) >> (ndigits << 2)) & 0xf;
 222		if (clo > max || chi > max) {
 223			max = 0xf;
 224			break;
 225		}
 226	}
 227
 228	/*
 229	 * Some modules (visor) have empty slots as placeholder for
 230	 * run-time specification that results in catch-all alias
 231	 */
 232	if (!(id->idVendor | id->idProduct | id->bDeviceClass | id->bInterfaceClass))
 233		return;
 234
 235	/* Convert numeric bcdDevice range into fnmatch-able pattern(s) */
 236	for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) {
 237		clo = devlo & 0xf;
 238		chi = devhi & 0xf;
 239		if (chi > max)	/* If we are in bcd mode, truncate if necessary */
 240			chi = max;
 241		devlo >>= 4;
 242		devhi >>= 4;
 243
 244		if (devlo == devhi || !ndigits) {
 245			do_usb_entry(id, devlo, ndigits, clo, chi, max, mod);
 246			break;
 247		}
 248
 249		if (clo > 0x0)
 250			do_usb_entry(id,
 251				     incbcd(&devlo, 1, max,
 252					    sizeof(id->bcdDevice_lo) * 2),
 253				     ndigits, clo, max, max, mod);
 254
 255		if (chi < max)
 256			do_usb_entry(id,
 257				     incbcd(&devhi, -1, max,
 258					    sizeof(id->bcdDevice_lo) * 2),
 259				     ndigits, 0x0, chi, max, mod);
 260	}
 261}
 262
 263static void do_usb_table(void *symval, unsigned long size,
 264			 struct module *mod)
 265{
 266	unsigned int i;
 267	const unsigned long id_size = sizeof(struct usb_device_id);
 268
 269	device_id_check(mod->name, "usb", size, id_size, symval);
 270
 271	/* Leave last one: it's the terminator. */
 272	size -= id_size;
 273
 274	for (i = 0; i < size; i += id_size)
 275		do_usb_entry_multi(symval + i, mod);
 276}
 277
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 278/* Looks like: hid:bNvNpN */
 279static int do_hid_entry(const char *filename,
 280			     struct hid_device_id *id, char *alias)
 281{
 282	id->bus = TO_NATIVE(id->bus);
 283	id->vendor = TO_NATIVE(id->vendor);
 284	id->product = TO_NATIVE(id->product);
 285
 286	sprintf(alias, "hid:b%04X", id->bus);
 287	ADD(alias, "v", id->vendor != HID_ANY_ID, id->vendor);
 288	ADD(alias, "p", id->product != HID_ANY_ID, id->product);
 
 
 
 289
 290	return 1;
 291}
 292
 293/* Looks like: ieee1394:venNmoNspNverN */
 294static int do_ieee1394_entry(const char *filename,
 295			     struct ieee1394_device_id *id, char *alias)
 296{
 297	id->match_flags = TO_NATIVE(id->match_flags);
 298	id->vendor_id = TO_NATIVE(id->vendor_id);
 299	id->model_id = TO_NATIVE(id->model_id);
 300	id->specifier_id = TO_NATIVE(id->specifier_id);
 301	id->version = TO_NATIVE(id->version);
 302
 303	strcpy(alias, "ieee1394:");
 304	ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID,
 305	    id->vendor_id);
 306	ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID,
 307	    id->model_id);
 308	ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID,
 309	    id->specifier_id);
 310	ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION,
 311	    id->version);
 312
 313	add_wildcard(alias);
 314	return 1;
 315}
 316
 317/* Looks like: pci:vNdNsvNsdNbcNscNiN. */
 318static int do_pci_entry(const char *filename,
 319			struct pci_device_id *id, char *alias)
 320{
 321	/* Class field can be divided into these three. */
 322	unsigned char baseclass, subclass, interface,
 323		baseclass_mask, subclass_mask, interface_mask;
 324
 325	id->vendor = TO_NATIVE(id->vendor);
 326	id->device = TO_NATIVE(id->device);
 327	id->subvendor = TO_NATIVE(id->subvendor);
 328	id->subdevice = TO_NATIVE(id->subdevice);
 329	id->class = TO_NATIVE(id->class);
 330	id->class_mask = TO_NATIVE(id->class_mask);
 331
 332	strcpy(alias, "pci:");
 333	ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor);
 334	ADD(alias, "d", id->device != PCI_ANY_ID, id->device);
 335	ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor);
 336	ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice);
 337
 338	baseclass = (id->class) >> 16;
 339	baseclass_mask = (id->class_mask) >> 16;
 340	subclass = (id->class) >> 8;
 341	subclass_mask = (id->class_mask) >> 8;
 342	interface = id->class;
 343	interface_mask = id->class_mask;
 344
 345	if ((baseclass_mask != 0 && baseclass_mask != 0xFF)
 346	    || (subclass_mask != 0 && subclass_mask != 0xFF)
 347	    || (interface_mask != 0 && interface_mask != 0xFF)) {
 348		warn("Can't handle masks in %s:%04X\n",
 349		     filename, id->class_mask);
 350		return 0;
 351	}
 352
 353	ADD(alias, "bc", baseclass_mask == 0xFF, baseclass);
 354	ADD(alias, "sc", subclass_mask == 0xFF, subclass);
 355	ADD(alias, "i", interface_mask == 0xFF, interface);
 356	add_wildcard(alias);
 357	return 1;
 358}
 359
 360/* looks like: "ccw:tNmNdtNdmN" */
 361static int do_ccw_entry(const char *filename,
 362			struct ccw_device_id *id, char *alias)
 363{
 364	id->match_flags = TO_NATIVE(id->match_flags);
 365	id->cu_type = TO_NATIVE(id->cu_type);
 366	id->cu_model = TO_NATIVE(id->cu_model);
 367	id->dev_type = TO_NATIVE(id->dev_type);
 368	id->dev_model = TO_NATIVE(id->dev_model);
 369
 370	strcpy(alias, "ccw:");
 371	ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE,
 372	    id->cu_type);
 373	ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL,
 374	    id->cu_model);
 375	ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE,
 376	    id->dev_type);
 377	ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL,
 378	    id->dev_model);
 379	add_wildcard(alias);
 380	return 1;
 381}
 382
 383/* looks like: "ap:tN" */
 384static int do_ap_entry(const char *filename,
 385		       struct ap_device_id *id, char *alias)
 386{
 387	sprintf(alias, "ap:t%02X*", id->dev_type);
 
 
 388	return 1;
 389}
 390
 391/* looks like: "css:tN" */
 392static int do_css_entry(const char *filename,
 393			struct css_device_id *id, char *alias)
 394{
 395	sprintf(alias, "css:t%01X", id->type);
 
 
 396	return 1;
 397}
 398
 399/* Looks like: "serio:tyNprNidNexN" */
 400static int do_serio_entry(const char *filename,
 401			  struct serio_device_id *id, char *alias)
 402{
 403	id->type = TO_NATIVE(id->type);
 404	id->proto = TO_NATIVE(id->proto);
 405	id->id = TO_NATIVE(id->id);
 406	id->extra = TO_NATIVE(id->extra);
 407
 408	strcpy(alias, "serio:");
 409	ADD(alias, "ty", id->type != SERIO_ANY, id->type);
 410	ADD(alias, "pr", id->proto != SERIO_ANY, id->proto);
 411	ADD(alias, "id", id->id != SERIO_ANY, id->id);
 412	ADD(alias, "ex", id->extra != SERIO_ANY, id->extra);
 413
 414	add_wildcard(alias);
 415	return 1;
 416}
 417
 418/* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */
 
 
 
 
 
 
 419static int do_acpi_entry(const char *filename,
 420			struct acpi_device_id *id, char *alias)
 421{
 422	sprintf(alias, "acpi*:%s:*", id->id);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 423	return 1;
 424}
 425
 426/* looks like: "pnp:dD" */
 427static void do_pnp_device_entry(void *symval, unsigned long size,
 428				struct module *mod)
 429{
 430	const unsigned long id_size = sizeof(struct pnp_device_id);
 431	const unsigned int count = (size / id_size)-1;
 432	const struct pnp_device_id *devs = symval;
 433	unsigned int i;
 434
 435	device_id_check(mod->name, "pnp", size, id_size, symval);
 436
 437	for (i = 0; i < count; i++) {
 438		const char *id = (char *)devs[i].id;
 439		char acpi_id[sizeof(devs[0].id)];
 440		int j;
 441
 442		buf_printf(&mod->dev_table_buf,
 443			   "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
 444
 445		/* fix broken pnp bus lowercasing */
 446		for (j = 0; j < sizeof(acpi_id); j++)
 447			acpi_id[j] = toupper(id[j]);
 448		buf_printf(&mod->dev_table_buf,
 449			   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
 450	}
 451}
 452
 453/* looks like: "pnp:dD" for every device of the card */
 454static void do_pnp_card_entries(void *symval, unsigned long size,
 455				struct module *mod)
 456{
 457	const unsigned long id_size = sizeof(struct pnp_card_device_id);
 458	const unsigned int count = (size / id_size)-1;
 459	const struct pnp_card_device_id *cards = symval;
 460	unsigned int i;
 461
 462	device_id_check(mod->name, "pnp", size, id_size, symval);
 463
 464	for (i = 0; i < count; i++) {
 465		unsigned int j;
 466		const struct pnp_card_device_id *card = &cards[i];
 467
 468		for (j = 0; j < PNP_MAX_DEVICES; j++) {
 469			const char *id = (char *)card->devs[j].id;
 470			int i2, j2;
 471			int dup = 0;
 472
 473			if (!id[0])
 474				break;
 475
 476			/* find duplicate, already added value */
 477			for (i2 = 0; i2 < i && !dup; i2++) {
 478				const struct pnp_card_device_id *card2 = &cards[i2];
 
 
 479
 480				for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) {
 481					const char *id2 = (char *)card2->devs[j2].id;
 
 482
 483					if (!id2[0])
 484						break;
 485
 486					if (!strcmp(id, id2)) {
 487						dup = 1;
 488						break;
 489					}
 490				}
 491			}
 492
 493			/* add an individual alias for every device entry */
 494			if (!dup) {
 495				char acpi_id[sizeof(card->devs[0].id)];
 496				int k;
 497
 498				buf_printf(&mod->dev_table_buf,
 499					   "MODULE_ALIAS(\"pnp:d%s*\");\n", id);
 500
 501				/* fix broken pnp bus lowercasing */
 502				for (k = 0; k < sizeof(acpi_id); k++)
 503					acpi_id[k] = toupper(id[k]);
 504				buf_printf(&mod->dev_table_buf,
 505					   "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id);
 506			}
 507		}
 508	}
 509}
 510
 511/* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */
 512static int do_pcmcia_entry(const char *filename,
 513			   struct pcmcia_device_id *id, char *alias)
 514{
 515	unsigned int i;
 516
 517	id->match_flags = TO_NATIVE(id->match_flags);
 518	id->manf_id = TO_NATIVE(id->manf_id);
 519	id->card_id = TO_NATIVE(id->card_id);
 520	id->func_id = TO_NATIVE(id->func_id);
 521	id->function = TO_NATIVE(id->function);
 522	id->device_no = TO_NATIVE(id->device_no);
 523
 524	for (i=0; i<4; i++) {
 525		id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]);
 526       }
 527
 528       strcpy(alias, "pcmcia:");
 529       ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID,
 530	   id->manf_id);
 531       ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID,
 532	   id->card_id);
 533       ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID,
 534	   id->func_id);
 535       ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION,
 536	   id->function);
 537       ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO,
 538	   id->device_no);
 539       ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]);
 540       ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]);
 541       ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]);
 542       ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]);
 543
 544	add_wildcard(alias);
 545       return 1;
 546}
 547
 548
 549
 550static int do_of_entry (const char *filename, struct of_device_id *of, char *alias)
 551{
 552    int len;
 553    char *tmp;
 554    len = sprintf (alias, "of:N%sT%s",
 555                    of->name[0] ? of->name : "*",
 556                    of->type[0] ? of->type : "*");
 557
 558    if (of->compatible[0])
 559        sprintf (&alias[len], "%sC%s",
 560                     of->type[0] ? "*" : "",
 561                     of->compatible);
 562
 563    /* Replace all whitespace with underscores */
 564    for (tmp = alias; tmp && *tmp; tmp++)
 565        if (isspace (*tmp))
 566            *tmp = '_';
 567
 568    add_wildcard(alias);
 569    return 1;
 570}
 571
 572static int do_vio_entry(const char *filename, struct vio_device_id *vio,
 573		char *alias)
 574{
 575	char *tmp;
 
 
 576
 577	sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*",
 578			vio->compat[0] ? vio->compat : "*");
 579
 580	/* Replace all whitespace with underscores */
 581	for (tmp = alias; tmp && *tmp; tmp++)
 582		if (isspace (*tmp))
 583			*tmp = '_';
 584
 585	add_wildcard(alias);
 586	return 1;
 587}
 588
 589#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 590
 591static void do_input(char *alias,
 592		     kernel_ulong_t *arr, unsigned int min, unsigned int max)
 593{
 594	unsigned int i;
 595
 
 
 596	for (i = min; i < max; i++)
 597		if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG)))
 598			sprintf(alias + strlen(alias), "%X,*", i);
 599}
 600
 601/* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */
 602static int do_input_entry(const char *filename, struct input_device_id *id,
 603			  char *alias)
 604{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 605	sprintf(alias, "input:");
 606
 607	ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype);
 608	ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor);
 609	ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product);
 610	ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version);
 611
 612	sprintf(alias + strlen(alias), "-e*");
 613	if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT)
 614		do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX);
 615	sprintf(alias + strlen(alias), "k*");
 616	if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT)
 617		do_input(alias, id->keybit,
 618			 INPUT_DEVICE_ID_KEY_MIN_INTERESTING,
 619			 INPUT_DEVICE_ID_KEY_MAX);
 620	sprintf(alias + strlen(alias), "r*");
 621	if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT)
 622		do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX);
 623	sprintf(alias + strlen(alias), "a*");
 624	if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT)
 625		do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX);
 626	sprintf(alias + strlen(alias), "m*");
 627	if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT)
 628		do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX);
 629	sprintf(alias + strlen(alias), "l*");
 630	if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT)
 631		do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX);
 632	sprintf(alias + strlen(alias), "s*");
 633	if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT)
 634		do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX);
 635	sprintf(alias + strlen(alias), "f*");
 636	if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT)
 637		do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX);
 638	sprintf(alias + strlen(alias), "w*");
 639	if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT)
 640		do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX);
 641	return 1;
 642}
 643
 644static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa,
 645		char *alias)
 646{
 647	if (eisa->sig[0])
 648		sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig);
 
 649	else
 650		strcat(alias, "*");
 651	return 1;
 652}
 653
 654/* Looks like: parisc:tNhvNrevNsvN */
 655static int do_parisc_entry(const char *filename, struct parisc_device_id *id,
 656		char *alias)
 657{
 658	id->hw_type = TO_NATIVE(id->hw_type);
 659	id->hversion = TO_NATIVE(id->hversion);
 660	id->hversion_rev = TO_NATIVE(id->hversion_rev);
 661	id->sversion = TO_NATIVE(id->sversion);
 662
 663	strcpy(alias, "parisc:");
 664	ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type);
 665	ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion);
 666	ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev);
 667	ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion);
 668
 669	add_wildcard(alias);
 670	return 1;
 671}
 672
 673/* Looks like: sdio:cNvNdN. */
 674static int do_sdio_entry(const char *filename,
 675			struct sdio_device_id *id, char *alias)
 676{
 677	id->class = TO_NATIVE(id->class);
 678	id->vendor = TO_NATIVE(id->vendor);
 679	id->device = TO_NATIVE(id->device);
 680
 681	strcpy(alias, "sdio:");
 682	ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class);
 683	ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor);
 684	ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device);
 685	add_wildcard(alias);
 686	return 1;
 687}
 688
 689/* Looks like: ssb:vNidNrevN. */
 690static int do_ssb_entry(const char *filename,
 691			struct ssb_device_id *id, char *alias)
 692{
 693	id->vendor = TO_NATIVE(id->vendor);
 694	id->coreid = TO_NATIVE(id->coreid);
 695	id->revision = TO_NATIVE(id->revision);
 696
 697	strcpy(alias, "ssb:");
 698	ADD(alias, "v", id->vendor != SSB_ANY_VENDOR, id->vendor);
 699	ADD(alias, "id", id->coreid != SSB_ANY_ID, id->coreid);
 700	ADD(alias, "rev", id->revision != SSB_ANY_REV, id->revision);
 701	add_wildcard(alias);
 702	return 1;
 703}
 704
 705/* Looks like: bcma:mNidNrevNclN. */
 706static int do_bcma_entry(const char *filename,
 707			 struct bcma_device_id *id, char *alias)
 708{
 709	id->manuf = TO_NATIVE(id->manuf);
 710	id->id = TO_NATIVE(id->id);
 711	id->rev = TO_NATIVE(id->rev);
 712	id->class = TO_NATIVE(id->class);
 713
 714	strcpy(alias, "bcma:");
 715	ADD(alias, "m", id->manuf != BCMA_ANY_MANUF, id->manuf);
 716	ADD(alias, "id", id->id != BCMA_ANY_ID, id->id);
 717	ADD(alias, "rev", id->rev != BCMA_ANY_REV, id->rev);
 718	ADD(alias, "cl", id->class != BCMA_ANY_CLASS, id->class);
 719	add_wildcard(alias);
 720	return 1;
 721}
 722
 723/* Looks like: virtio:dNvN */
 724static int do_virtio_entry(const char *filename, struct virtio_device_id *id,
 725			   char *alias)
 726{
 727	id->device = TO_NATIVE(id->device);
 728	id->vendor = TO_NATIVE(id->vendor);
 729
 730	strcpy(alias, "virtio:");
 731	ADD(alias, "d", id->device != VIRTIO_DEV_ANY_ID, id->device);
 732	ADD(alias, "v", id->vendor != VIRTIO_DEV_ANY_ID, id->vendor);
 733
 734	add_wildcard(alias);
 735	return 1;
 736}
 737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 738/* Looks like: i2c:S */
 739static int do_i2c_entry(const char *filename, struct i2c_device_id *id,
 
 
 
 
 
 
 
 
 
 740			char *alias)
 741{
 742	sprintf(alias, I2C_MODULE_PREFIX "%s", id->name);
 
 
 
 
 
 
 
 
 
 
 743
 744	return 1;
 745}
 746
 747/* Looks like: spi:S */
 748static int do_spi_entry(const char *filename, struct spi_device_id *id,
 749			char *alias)
 750{
 751	sprintf(alias, SPI_MODULE_PREFIX "%s", id->name);
 
 752
 753	return 1;
 754}
 755
 756static const struct dmifield {
 757	const char *prefix;
 758	int field;
 759} dmi_fields[] = {
 760	{ "bvn", DMI_BIOS_VENDOR },
 761	{ "bvr", DMI_BIOS_VERSION },
 762	{ "bd",  DMI_BIOS_DATE },
 
 
 763	{ "svn", DMI_SYS_VENDOR },
 764	{ "pn",  DMI_PRODUCT_NAME },
 765	{ "pvr", DMI_PRODUCT_VERSION },
 766	{ "rvn", DMI_BOARD_VENDOR },
 767	{ "rn",  DMI_BOARD_NAME },
 768	{ "rvr", DMI_BOARD_VERSION },
 769	{ "cvn", DMI_CHASSIS_VENDOR },
 770	{ "ct",  DMI_CHASSIS_TYPE },
 771	{ "cvr", DMI_CHASSIS_VERSION },
 772	{ NULL,  DMI_NONE }
 773};
 774
 775static void dmi_ascii_filter(char *d, const char *s)
 776{
 777	/* Filter out characters we don't want to see in the modalias string */
 778	for (; *s; s++)
 779		if (*s > ' ' && *s < 127 && *s != ':')
 780			*(d++) = *s;
 781
 782	*d = 0;
 783}
 784
 785
 786static int do_dmi_entry(const char *filename, struct dmi_system_id *id,
 787			char *alias)
 788{
 789	int i, j;
 790
 791	sprintf(alias, "dmi*");
 792
 793	for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) {
 794		for (j = 0; j < 4; j++) {
 795			if (id->matches[j].slot &&
 796			    id->matches[j].slot == dmi_fields[i].field) {
 797				sprintf(alias + strlen(alias), ":%s*",
 798					dmi_fields[i].prefix);
 799				dmi_ascii_filter(alias + strlen(alias),
 800						 id->matches[j].substr);
 801				strcat(alias, "*");
 802			}
 803		}
 804	}
 805
 806	strcat(alias, ":");
 807	return 1;
 808}
 809
 810static int do_platform_entry(const char *filename,
 811			     struct platform_device_id *id, char *alias)
 812{
 813	sprintf(alias, PLATFORM_MODULE_PREFIX "%s", id->name);
 
 814	return 1;
 815}
 816
 817static int do_mdio_entry(const char *filename,
 818			 struct mdio_device_id *id, char *alias)
 819{
 820	int i;
 
 
 821
 822	alias += sprintf(alias, MDIO_MODULE_PREFIX);
 823
 824	for (i = 0; i < 32; i++) {
 825		if (!((id->phy_id_mask >> (31-i)) & 1))
 826			*(alias++) = '?';
 827		else if ((id->phy_id >> (31-i)) & 1)
 828			*(alias++) = '1';
 829		else
 830			*(alias++) = '0';
 831	}
 832
 833	/* Terminate the string */
 834	*alias = 0;
 835
 836	return 1;
 837}
 838
 839/* Looks like: zorro:iN. */
 840static int do_zorro_entry(const char *filename, struct zorro_device_id *id,
 841			  char *alias)
 842{
 843	id->id = TO_NATIVE(id->id);
 844	strcpy(alias, "zorro:");
 845	ADD(alias, "i", id->id != ZORRO_WILDCARD, id->id);
 846	return 1;
 847}
 848
 849/* looks like: "pnp:dD" */
 850static int do_isapnp_entry(const char *filename,
 851			   struct isapnp_device_id *id, char *alias)
 852{
 
 
 853	sprintf(alias, "pnp:d%c%c%c%x%x%x%x*",
 854		'A' + ((id->vendor >> 2) & 0x3f) - 1,
 855		'A' + (((id->vendor & 3) << 3) | ((id->vendor >> 13) & 7)) - 1,
 856		'A' + ((id->vendor >> 8) & 0x1f) - 1,
 857		(id->function >> 4) & 0x0f, id->function & 0x0f,
 858		(id->function >> 12) & 0x0f, (id->function >> 8) & 0x0f);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 859	return 1;
 860}
 861
 862/* Ignore any prefix, eg. some architectures prepend _ */
 863static inline int sym_is(const char *symbol, const char *name)
 864{
 865	const char *match;
 866
 867	match = strstr(symbol, name);
 868	if (!match)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 869		return 0;
 870	return match[strlen(name)] == '\0';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 871}
 872
 873static void do_table(void *symval, unsigned long size,
 874		     unsigned long id_size,
 875		     const char *device_id,
 876		     void *function,
 877		     struct module *mod)
 878{
 879	unsigned int i;
 880	char alias[500];
 881	int (*do_entry)(const char *, void *entry, char *alias) = function;
 882
 883	device_id_check(mod->name, device_id, size, id_size, symval);
 884	/* Leave last one: it's the terminator. */
 885	size -= id_size;
 886
 887	for (i = 0; i < size; i += id_size) {
 888		if (do_entry(mod->name, symval+i, alias)) {
 889			buf_printf(&mod->dev_table_buf,
 890				   "MODULE_ALIAS(\"%s\");\n", alias);
 891		}
 892	}
 893}
 894
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 895/* Create MODULE_ALIAS() statements.
 896 * At this time, we cannot write the actual output C source yet,
 897 * so we write into the mod->dev_table_buf buffer. */
 898void handle_moddevtable(struct module *mod, struct elf_info *info,
 899			Elf_Sym *sym, const char *symname)
 900{
 901	void *symval;
 902	char *zeros = NULL;
 
 
 903
 904	/* We're looking for a section relative symbol */
 905	if (!sym->st_shndx || get_secindex(info, sym) >= info->num_sections)
 906		return;
 907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 908	/* Handle all-NULL symbols allocated into .bss */
 909	if (info->sechdrs[get_secindex(info, sym)].sh_type & SHT_NOBITS) {
 910		zeros = calloc(1, sym->st_size);
 911		symval = zeros;
 912	} else {
 913		symval = (void *)info->hdr
 914			+ info->sechdrs[get_secindex(info, sym)].sh_offset
 915			+ sym->st_value;
 916	}
 917
 918	if (sym_is(symname, "__mod_pci_device_table"))
 919		do_table(symval, sym->st_size,
 920			 sizeof(struct pci_device_id), "pci",
 921			 do_pci_entry, mod);
 922	else if (sym_is(symname, "__mod_usb_device_table"))
 923		/* special case to handle bcdDevice ranges */
 924		do_usb_table(symval, sym->st_size, mod);
 925	else if (sym_is(symname, "__mod_hid_device_table"))
 926		do_table(symval, sym->st_size,
 927			 sizeof(struct hid_device_id), "hid",
 928			 do_hid_entry, mod);
 929	else if (sym_is(symname, "__mod_ieee1394_device_table"))
 930		do_table(symval, sym->st_size,
 931			 sizeof(struct ieee1394_device_id), "ieee1394",
 932			 do_ieee1394_entry, mod);
 933	else if (sym_is(symname, "__mod_ccw_device_table"))
 934		do_table(symval, sym->st_size,
 935			 sizeof(struct ccw_device_id), "ccw",
 936			 do_ccw_entry, mod);
 937	else if (sym_is(symname, "__mod_ap_device_table"))
 938		do_table(symval, sym->st_size,
 939			 sizeof(struct ap_device_id), "ap",
 940			 do_ap_entry, mod);
 941	else if (sym_is(symname, "__mod_css_device_table"))
 942		do_table(symval, sym->st_size,
 943			 sizeof(struct css_device_id), "css",
 944			 do_css_entry, mod);
 945	else if (sym_is(symname, "__mod_serio_device_table"))
 946		do_table(symval, sym->st_size,
 947			 sizeof(struct serio_device_id), "serio",
 948			 do_serio_entry, mod);
 949	else if (sym_is(symname, "__mod_acpi_device_table"))
 950		do_table(symval, sym->st_size,
 951			 sizeof(struct acpi_device_id), "acpi",
 952			 do_acpi_entry, mod);
 953	else if (sym_is(symname, "__mod_pnp_device_table"))
 954		do_pnp_device_entry(symval, sym->st_size, mod);
 955	else if (sym_is(symname, "__mod_pnp_card_device_table"))
 956		do_pnp_card_entries(symval, sym->st_size, mod);
 957	else if (sym_is(symname, "__mod_pcmcia_device_table"))
 958		do_table(symval, sym->st_size,
 959			 sizeof(struct pcmcia_device_id), "pcmcia",
 960			 do_pcmcia_entry, mod);
 961        else if (sym_is(symname, "__mod_of_device_table"))
 962		do_table(symval, sym->st_size,
 963			 sizeof(struct of_device_id), "of",
 964			 do_of_entry, mod);
 965        else if (sym_is(symname, "__mod_vio_device_table"))
 966		do_table(symval, sym->st_size,
 967			 sizeof(struct vio_device_id), "vio",
 968			 do_vio_entry, mod);
 969	else if (sym_is(symname, "__mod_input_device_table"))
 970		do_table(symval, sym->st_size,
 971			 sizeof(struct input_device_id), "input",
 972			 do_input_entry, mod);
 973	else if (sym_is(symname, "__mod_eisa_device_table"))
 974		do_table(symval, sym->st_size,
 975			 sizeof(struct eisa_device_id), "eisa",
 976			 do_eisa_entry, mod);
 977	else if (sym_is(symname, "__mod_parisc_device_table"))
 978		do_table(symval, sym->st_size,
 979			 sizeof(struct parisc_device_id), "parisc",
 980			 do_parisc_entry, mod);
 981	else if (sym_is(symname, "__mod_sdio_device_table"))
 982		do_table(symval, sym->st_size,
 983			 sizeof(struct sdio_device_id), "sdio",
 984			 do_sdio_entry, mod);
 985	else if (sym_is(symname, "__mod_ssb_device_table"))
 986		do_table(symval, sym->st_size,
 987			 sizeof(struct ssb_device_id), "ssb",
 988			 do_ssb_entry, mod);
 989	else if (sym_is(symname, "__mod_bcma_device_table"))
 990		do_table(symval, sym->st_size,
 991			 sizeof(struct bcma_device_id), "bcma",
 992			 do_bcma_entry, mod);
 993	else if (sym_is(symname, "__mod_virtio_device_table"))
 994		do_table(symval, sym->st_size,
 995			 sizeof(struct virtio_device_id), "virtio",
 996			 do_virtio_entry, mod);
 997	else if (sym_is(symname, "__mod_i2c_device_table"))
 998		do_table(symval, sym->st_size,
 999			 sizeof(struct i2c_device_id), "i2c",
1000			 do_i2c_entry, mod);
1001	else if (sym_is(symname, "__mod_spi_device_table"))
1002		do_table(symval, sym->st_size,
1003			 sizeof(struct spi_device_id), "spi",
1004			 do_spi_entry, mod);
1005	else if (sym_is(symname, "__mod_dmi_device_table"))
1006		do_table(symval, sym->st_size,
1007			 sizeof(struct dmi_system_id), "dmi",
1008			 do_dmi_entry, mod);
1009	else if (sym_is(symname, "__mod_platform_device_table"))
1010		do_table(symval, sym->st_size,
1011			 sizeof(struct platform_device_id), "platform",
1012			 do_platform_entry, mod);
1013	else if (sym_is(symname, "__mod_mdio_device_table"))
1014		do_table(symval, sym->st_size,
1015			 sizeof(struct mdio_device_id), "mdio",
1016			 do_mdio_entry, mod);
1017	else if (sym_is(symname, "__mod_zorro_device_table"))
1018		do_table(symval, sym->st_size,
1019			 sizeof(struct zorro_device_id), "zorro",
1020			 do_zorro_entry, mod);
1021	else if (sym_is(symname, "__mod_isapnp_device_table"))
1022		do_table(symval, sym->st_size,
1023			sizeof(struct isapnp_device_id), "isa",
1024			do_isapnp_entry, mod);
1025	free(zeros);
1026}
1027
1028/* Now add out buffered information to the generated C source */
1029void add_moddevtable(struct buffer *buf, struct module *mod)
1030{
1031	buf_printf(buf, "\n");
1032	buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos);
1033	free(mod->dev_table_buf.p);
1034}