Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
| 18 | typedef Elf32_Addr kernel_ulong_t; |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 19 | #define BITS_PER_LONG 32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #else |
| 21 | typedef Elf64_Addr kernel_ulong_t; |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 22 | #define BITS_PER_LONG 64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #endif |
| 24 | #ifdef __sun__ |
| 25 | #include <inttypes.h> |
| 26 | #else |
| 27 | #include <stdint.h> |
| 28 | #endif |
| 29 | |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 30 | #include <ctype.h> |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | typedef uint32_t __u32; |
| 33 | typedef uint16_t __u16; |
| 34 | typedef unsigned char __u8; |
| 35 | |
| 36 | /* Big exception to the "don't include kernel headers into userspace, which |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 37 | * even potentially has different endianness and word sizes, since |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | * we handle those differences explicitly below */ |
| 39 | #include "../../include/linux/mod_devicetable.h" |
| 40 | |
| 41 | #define ADD(str, sep, cond, field) \ |
| 42 | do { \ |
| 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 | |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 54 | /** |
| 55 | * Check that sizeof(device_id type) are consistent with size of section |
| 56 | * in .o file. If in-consistent then userspace and kernel does not agree |
| 57 | * on actual size which is a bug. |
| 58 | **/ |
| 59 | static void device_id_size_check(const char *modname, const char *device_id, |
| 60 | unsigned long size, unsigned long id_size) |
| 61 | { |
| 62 | if (size % id_size || size < id_size) { |
| 63 | fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo " |
| 64 | "of the size of section __mod_%s_device_table=%lu.\n" |
| 65 | "Fix definition of struct %s_device_id " |
| 66 | "in mod_devicetable.h\n", |
| 67 | modname, device_id, id_size, device_id, size, device_id); |
| 68 | } |
| 69 | } |
| 70 | |
Roman Kagan | b19dcd9 | 2005-04-22 15:07:01 -0700 | [diff] [blame] | 71 | /* USB is special because the bcdDevice can be matched against a numeric range */ |
| 72 | /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */ |
| 73 | static void do_usb_entry(struct usb_device_id *id, |
| 74 | unsigned int bcdDevice_initial, int bcdDevice_initial_digits, |
| 75 | unsigned char range_lo, unsigned char range_hi, |
| 76 | struct module *mod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Roman Kagan | b19dcd9 | 2005-04-22 15:07:01 -0700 | [diff] [blame] | 78 | char alias[500]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | strcpy(alias, "usb:"); |
| 80 | ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR, |
| 81 | id->idVendor); |
| 82 | ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT, |
| 83 | id->idProduct); |
Roman Kagan | b19dcd9 | 2005-04-22 15:07:01 -0700 | [diff] [blame] | 84 | |
| 85 | strcat(alias, "d"); |
| 86 | if (bcdDevice_initial_digits) |
| 87 | sprintf(alias + strlen(alias), "%0*X", |
| 88 | bcdDevice_initial_digits, bcdDevice_initial); |
| 89 | if (range_lo == range_hi) |
| 90 | sprintf(alias + strlen(alias), "%u", range_lo); |
| 91 | else if (range_lo > 0 || range_hi < 9) |
| 92 | sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi); |
| 93 | if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1)) |
| 94 | strcat(alias, "*"); |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS, |
| 97 | id->bDeviceClass); |
| 98 | ADD(alias, "dsc", |
| 99 | id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS, |
| 100 | id->bDeviceSubClass); |
| 101 | ADD(alias, "dp", |
| 102 | id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL, |
| 103 | id->bDeviceProtocol); |
| 104 | ADD(alias, "ic", |
| 105 | id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS, |
| 106 | id->bInterfaceClass); |
| 107 | ADD(alias, "isc", |
| 108 | id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS, |
| 109 | id->bInterfaceSubClass); |
| 110 | ADD(alias, "ip", |
| 111 | id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL, |
| 112 | id->bInterfaceProtocol); |
Roman Kagan | b19dcd9 | 2005-04-22 15:07:01 -0700 | [diff] [blame] | 113 | |
| 114 | /* Always end in a wildcard, for future extension */ |
| 115 | if (alias[strlen(alias)-1] != '*') |
| 116 | strcat(alias, "*"); |
| 117 | buf_printf(&mod->dev_table_buf, |
| 118 | "MODULE_ALIAS(\"%s\");\n", alias); |
| 119 | } |
| 120 | |
| 121 | static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod) |
| 122 | { |
| 123 | unsigned int devlo, devhi; |
| 124 | unsigned char chi, clo; |
| 125 | int ndigits; |
| 126 | |
| 127 | id->match_flags = TO_NATIVE(id->match_flags); |
| 128 | id->idVendor = TO_NATIVE(id->idVendor); |
| 129 | id->idProduct = TO_NATIVE(id->idProduct); |
| 130 | |
| 131 | devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ? |
| 132 | TO_NATIVE(id->bcdDevice_lo) : 0x0U; |
| 133 | devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ? |
| 134 | TO_NATIVE(id->bcdDevice_hi) : ~0x0U; |
| 135 | |
| 136 | /* |
| 137 | * Some modules (visor) have empty slots as placeholder for |
| 138 | * run-time specification that results in catch-all alias |
| 139 | */ |
| 140 | if (!(id->idVendor | id->bDeviceClass | id->bInterfaceClass)) |
| 141 | return; |
| 142 | |
| 143 | /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */ |
| 144 | for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) { |
| 145 | clo = devlo & 0xf; |
| 146 | chi = devhi & 0xf; |
| 147 | if (chi > 9) /* it's bcd not hex */ |
| 148 | chi = 9; |
| 149 | devlo >>= 4; |
| 150 | devhi >>= 4; |
| 151 | |
| 152 | if (devlo == devhi || !ndigits) { |
| 153 | do_usb_entry(id, devlo, ndigits, clo, chi, mod); |
| 154 | break; |
| 155 | } |
| 156 | |
| 157 | if (clo > 0) |
| 158 | do_usb_entry(id, devlo++, ndigits, clo, 9, mod); |
| 159 | |
| 160 | if (chi < 9) |
| 161 | do_usb_entry(id, devhi--, ndigits, 0, chi, mod); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static void do_usb_table(void *symval, unsigned long size, |
| 166 | struct module *mod) |
| 167 | { |
| 168 | unsigned int i; |
| 169 | const unsigned long id_size = sizeof(struct usb_device_id); |
| 170 | |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 171 | device_id_size_check(mod->name, "usb", size, id_size); |
| 172 | |
Roman Kagan | b19dcd9 | 2005-04-22 15:07:01 -0700 | [diff] [blame] | 173 | /* Leave last one: it's the terminator. */ |
| 174 | size -= id_size; |
| 175 | |
| 176 | for (i = 0; i < size; i += id_size) |
| 177 | do_usb_entry_multi(symval + i, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* Looks like: ieee1394:venNmoNspNverN */ |
| 181 | static int do_ieee1394_entry(const char *filename, |
| 182 | struct ieee1394_device_id *id, char *alias) |
| 183 | { |
| 184 | id->match_flags = TO_NATIVE(id->match_flags); |
| 185 | id->vendor_id = TO_NATIVE(id->vendor_id); |
| 186 | id->model_id = TO_NATIVE(id->model_id); |
| 187 | id->specifier_id = TO_NATIVE(id->specifier_id); |
| 188 | id->version = TO_NATIVE(id->version); |
| 189 | |
| 190 | strcpy(alias, "ieee1394:"); |
| 191 | ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID, |
| 192 | id->vendor_id); |
| 193 | ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID, |
| 194 | id->model_id); |
| 195 | ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID, |
| 196 | id->specifier_id); |
| 197 | ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION, |
| 198 | id->version); |
| 199 | |
| 200 | return 1; |
| 201 | } |
| 202 | |
| 203 | /* Looks like: pci:vNdNsvNsdNbcNscNiN. */ |
| 204 | static int do_pci_entry(const char *filename, |
| 205 | struct pci_device_id *id, char *alias) |
| 206 | { |
| 207 | /* Class field can be divided into these three. */ |
| 208 | unsigned char baseclass, subclass, interface, |
| 209 | baseclass_mask, subclass_mask, interface_mask; |
| 210 | |
| 211 | id->vendor = TO_NATIVE(id->vendor); |
| 212 | id->device = TO_NATIVE(id->device); |
| 213 | id->subvendor = TO_NATIVE(id->subvendor); |
| 214 | id->subdevice = TO_NATIVE(id->subdevice); |
| 215 | id->class = TO_NATIVE(id->class); |
| 216 | id->class_mask = TO_NATIVE(id->class_mask); |
| 217 | |
| 218 | strcpy(alias, "pci:"); |
| 219 | ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor); |
| 220 | ADD(alias, "d", id->device != PCI_ANY_ID, id->device); |
| 221 | ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor); |
| 222 | ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice); |
| 223 | |
| 224 | baseclass = (id->class) >> 16; |
| 225 | baseclass_mask = (id->class_mask) >> 16; |
| 226 | subclass = (id->class) >> 8; |
| 227 | subclass_mask = (id->class_mask) >> 8; |
| 228 | interface = id->class; |
| 229 | interface_mask = id->class_mask; |
| 230 | |
| 231 | if ((baseclass_mask != 0 && baseclass_mask != 0xFF) |
| 232 | || (subclass_mask != 0 && subclass_mask != 0xFF) |
| 233 | || (interface_mask != 0 && interface_mask != 0xFF)) { |
Sam Ravnborg | cb80514 | 2006-01-28 16:57:26 +0100 | [diff] [blame] | 234 | warn("Can't handle masks in %s:%04X\n", |
| 235 | filename, id->class_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | ADD(alias, "bc", baseclass_mask == 0xFF, baseclass); |
| 240 | ADD(alias, "sc", subclass_mask == 0xFF, subclass); |
| 241 | ADD(alias, "i", interface_mask == 0xFF, interface); |
| 242 | return 1; |
| 243 | } |
| 244 | |
Sam Ravnborg | 62070fa | 2006-03-03 16:46:04 +0100 | [diff] [blame] | 245 | /* looks like: "ccw:tNmNdtNdmN" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | static int do_ccw_entry(const char *filename, |
| 247 | struct ccw_device_id *id, char *alias) |
| 248 | { |
| 249 | id->match_flags = TO_NATIVE(id->match_flags); |
| 250 | id->cu_type = TO_NATIVE(id->cu_type); |
| 251 | id->cu_model = TO_NATIVE(id->cu_model); |
| 252 | id->dev_type = TO_NATIVE(id->dev_type); |
| 253 | id->dev_model = TO_NATIVE(id->dev_model); |
| 254 | |
| 255 | strcpy(alias, "ccw:"); |
| 256 | ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE, |
| 257 | id->cu_type); |
| 258 | ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL, |
| 259 | id->cu_model); |
| 260 | ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE, |
| 261 | id->dev_type); |
Bastian Blank | de1d9c0 | 2006-03-06 15:43:00 -0800 | [diff] [blame] | 262 | ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | id->dev_model); |
| 264 | return 1; |
| 265 | } |
| 266 | |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 267 | /* looks like: "ap:tN" */ |
| 268 | static int do_ap_entry(const char *filename, |
| 269 | struct ap_device_id *id, char *alias) |
| 270 | { |
| 271 | sprintf(alias, "ap:t%02X", id->dev_type); |
| 272 | return 1; |
| 273 | } |
| 274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | /* Looks like: "serio:tyNprNidNexN" */ |
| 276 | static int do_serio_entry(const char *filename, |
| 277 | struct serio_device_id *id, char *alias) |
| 278 | { |
| 279 | id->type = TO_NATIVE(id->type); |
| 280 | id->proto = TO_NATIVE(id->proto); |
| 281 | id->id = TO_NATIVE(id->id); |
| 282 | id->extra = TO_NATIVE(id->extra); |
| 283 | |
| 284 | strcpy(alias, "serio:"); |
| 285 | ADD(alias, "ty", id->type != SERIO_ANY, id->type); |
| 286 | ADD(alias, "pr", id->proto != SERIO_ANY, id->proto); |
| 287 | ADD(alias, "id", id->id != SERIO_ANY, id->id); |
| 288 | ADD(alias, "ex", id->extra != SERIO_ANY, id->extra); |
| 289 | |
| 290 | return 1; |
| 291 | } |
| 292 | |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame^] | 293 | /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */ |
| 294 | static int do_acpi_entry(const char *filename, |
| 295 | struct acpi_device_id *id, char *alias) |
| 296 | { |
| 297 | sprintf(alias, "acpi*:%s:", id->id); |
| 298 | return 1; |
| 299 | } |
| 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | /* looks like: "pnp:dD" */ |
| 302 | static int do_pnp_entry(const char *filename, |
| 303 | struct pnp_device_id *id, char *alias) |
| 304 | { |
| 305 | sprintf(alias, "pnp:d%s", id->id); |
| 306 | return 1; |
| 307 | } |
| 308 | |
| 309 | /* looks like: "pnp:cCdD..." */ |
| 310 | static int do_pnp_card_entry(const char *filename, |
| 311 | struct pnp_card_device_id *id, char *alias) |
| 312 | { |
| 313 | int i; |
| 314 | |
| 315 | sprintf(alias, "pnp:c%s", id->id); |
| 316 | for (i = 0; i < PNP_MAX_DEVICES; i++) { |
| 317 | if (! *id->devs[i].id) |
| 318 | break; |
| 319 | sprintf(alias + strlen(alias), "d%s", id->devs[i].id); |
| 320 | } |
| 321 | return 1; |
| 322 | } |
| 323 | |
Dominik Brodowski | 90829cf | 2005-06-27 16:28:12 -0700 | [diff] [blame] | 324 | /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */ |
| 325 | static int do_pcmcia_entry(const char *filename, |
| 326 | struct pcmcia_device_id *id, char *alias) |
| 327 | { |
| 328 | unsigned int i; |
| 329 | |
Kars de Jong | 4fb7edc | 2005-09-25 14:39:46 +0200 | [diff] [blame] | 330 | id->match_flags = TO_NATIVE(id->match_flags); |
Dominik Brodowski | 90829cf | 2005-06-27 16:28:12 -0700 | [diff] [blame] | 331 | id->manf_id = TO_NATIVE(id->manf_id); |
| 332 | id->card_id = TO_NATIVE(id->card_id); |
| 333 | id->func_id = TO_NATIVE(id->func_id); |
| 334 | id->function = TO_NATIVE(id->function); |
| 335 | id->device_no = TO_NATIVE(id->device_no); |
Kars de Jong | 4fb7edc | 2005-09-25 14:39:46 +0200 | [diff] [blame] | 336 | |
Dominik Brodowski | 90829cf | 2005-06-27 16:28:12 -0700 | [diff] [blame] | 337 | for (i=0; i<4; i++) { |
| 338 | id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]); |
| 339 | } |
| 340 | |
| 341 | strcpy(alias, "pcmcia:"); |
| 342 | ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID, |
| 343 | id->manf_id); |
| 344 | ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID, |
| 345 | id->card_id); |
| 346 | ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID, |
| 347 | id->func_id); |
| 348 | ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION, |
| 349 | id->function); |
| 350 | ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO, |
| 351 | id->device_no); |
| 352 | ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]); |
| 353 | ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]); |
| 354 | ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]); |
| 355 | ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]); |
| 356 | |
| 357 | return 1; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 362 | static int do_of_entry (const char *filename, struct of_device_id *of, char *alias) |
| 363 | { |
Sylvain Munaut | d1ab423 | 2007-05-08 19:59:29 +1000 | [diff] [blame] | 364 | int len; |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 365 | char *tmp; |
Sylvain Munaut | d1ab423 | 2007-05-08 19:59:29 +1000 | [diff] [blame] | 366 | len = sprintf (alias, "of:N%sT%s", |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 367 | of->name[0] ? of->name : "*", |
Sylvain Munaut | d1ab423 | 2007-05-08 19:59:29 +1000 | [diff] [blame] | 368 | of->type[0] ? of->type : "*"); |
| 369 | |
| 370 | if (of->compatible[0]) |
| 371 | sprintf (&alias[len], "%sC%s", |
| 372 | of->type[0] ? "*" : "", |
| 373 | of->compatible); |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 374 | |
| 375 | /* Replace all whitespace with underscores */ |
| 376 | for (tmp = alias; tmp && *tmp; tmp++) |
| 377 | if (isspace (*tmp)) |
| 378 | *tmp = '_'; |
| 379 | |
| 380 | return 1; |
| 381 | } |
| 382 | |
Stephen Rothwell | fb120da | 2005-08-17 16:42:59 +1000 | [diff] [blame] | 383 | static int do_vio_entry(const char *filename, struct vio_device_id *vio, |
| 384 | char *alias) |
| 385 | { |
| 386 | char *tmp; |
| 387 | |
| 388 | sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*", |
| 389 | vio->compat[0] ? vio->compat : "*"); |
| 390 | |
| 391 | /* Replace all whitespace with underscores */ |
| 392 | for (tmp = alias; tmp && *tmp; tmp++) |
| 393 | if (isspace (*tmp)) |
| 394 | *tmp = '_'; |
| 395 | |
| 396 | return 1; |
| 397 | } |
| 398 | |
Greg Kroah-Hartman | a9d1b24 | 2005-10-22 00:23:27 +0200 | [diff] [blame] | 399 | static int do_i2c_entry(const char *filename, struct i2c_device_id *i2c, char *alias) |
| 400 | { |
| 401 | strcpy(alias, "i2c:"); |
| 402 | ADD(alias, "id", 1, i2c->id); |
| 403 | return 1; |
| 404 | } |
| 405 | |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 406 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 407 | |
| 408 | static void do_input(char *alias, |
| 409 | kernel_ulong_t *arr, unsigned int min, unsigned int max) |
| 410 | { |
| 411 | unsigned int i; |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 412 | |
| 413 | for (i = min; i < max; i++) |
Hans de Goede | e0e9263 | 2006-08-15 12:09:27 +0200 | [diff] [blame] | 414 | if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG))) |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 415 | sprintf(alias + strlen(alias), "%X,*", i); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */ |
| 419 | static int do_input_entry(const char *filename, struct input_device_id *id, |
| 420 | char *alias) |
| 421 | { |
| 422 | sprintf(alias, "input:"); |
| 423 | |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 424 | ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype); |
| 425 | ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor); |
| 426 | ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product); |
| 427 | ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 428 | |
| 429 | sprintf(alias + strlen(alias), "-e*"); |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 430 | if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT) |
Sam Ravnborg | dc24f0e | 2007-03-09 19:59:06 +0100 | [diff] [blame] | 431 | do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 432 | sprintf(alias + strlen(alias), "k*"); |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 433 | if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT) |
Sam Ravnborg | dc24f0e | 2007-03-09 19:59:06 +0100 | [diff] [blame] | 434 | do_input(alias, id->keybit, |
| 435 | INPUT_DEVICE_ID_KEY_MIN_INTERESTING, |
| 436 | INPUT_DEVICE_ID_KEY_MAX); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 437 | sprintf(alias + strlen(alias), "r*"); |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 438 | if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT) |
Sam Ravnborg | dc24f0e | 2007-03-09 19:59:06 +0100 | [diff] [blame] | 439 | do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 440 | sprintf(alias + strlen(alias), "a*"); |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 441 | if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT) |
Sam Ravnborg | dc24f0e | 2007-03-09 19:59:06 +0100 | [diff] [blame] | 442 | do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 443 | sprintf(alias + strlen(alias), "m*"); |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 444 | if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT) |
Sam Ravnborg | dc24f0e | 2007-03-09 19:59:06 +0100 | [diff] [blame] | 445 | do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 446 | sprintf(alias + strlen(alias), "l*"); |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 447 | if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT) |
Sam Ravnborg | dc24f0e | 2007-03-09 19:59:06 +0100 | [diff] [blame] | 448 | do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 449 | sprintf(alias + strlen(alias), "s*"); |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 450 | if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT) |
Sam Ravnborg | dc24f0e | 2007-03-09 19:59:06 +0100 | [diff] [blame] | 451 | do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 452 | sprintf(alias + strlen(alias), "f*"); |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 453 | if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT) |
Sam Ravnborg | dc24f0e | 2007-03-09 19:59:06 +0100 | [diff] [blame] | 454 | do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 455 | sprintf(alias + strlen(alias), "w*"); |
Dmitry Torokhov | ddc5d34 | 2006-04-26 00:14:19 -0400 | [diff] [blame] | 456 | if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT) |
Sam Ravnborg | dc24f0e | 2007-03-09 19:59:06 +0100 | [diff] [blame] | 457 | do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 458 | return 1; |
| 459 | } |
| 460 | |
Michael Tokarev | 07563c7 | 2006-09-27 01:50:56 -0700 | [diff] [blame] | 461 | static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa, |
| 462 | char *alias) |
| 463 | { |
| 464 | if (eisa->sig[0]) |
| 465 | sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig); |
| 466 | return 1; |
| 467 | } |
| 468 | |
Kyle McMartin | f3cf267 | 2007-01-13 14:58:21 -0500 | [diff] [blame] | 469 | /* Looks like: parisc:tNhvNrevNsvN */ |
| 470 | static int do_parisc_entry(const char *filename, struct parisc_device_id *id, |
| 471 | char *alias) |
| 472 | { |
| 473 | id->hw_type = TO_NATIVE(id->hw_type); |
| 474 | id->hversion = TO_NATIVE(id->hversion); |
| 475 | id->hversion_rev = TO_NATIVE(id->hversion_rev); |
| 476 | id->sversion = TO_NATIVE(id->sversion); |
| 477 | |
| 478 | strcpy(alias, "parisc:"); |
Kyle McMartin | f354ef8 | 2007-01-13 15:02:09 -0500 | [diff] [blame] | 479 | ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type); |
| 480 | ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion); |
| 481 | ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev); |
| 482 | ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion); |
Kyle McMartin | f3cf267 | 2007-01-13 14:58:21 -0500 | [diff] [blame] | 483 | |
| 484 | return 1; |
| 485 | } |
| 486 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | /* Ignore any prefix, eg. v850 prepends _ */ |
| 488 | static inline int sym_is(const char *symbol, const char *name) |
| 489 | { |
| 490 | const char *match; |
| 491 | |
| 492 | match = strstr(symbol, name); |
| 493 | if (!match) |
| 494 | return 0; |
| 495 | return match[strlen(symbol)] == '\0'; |
| 496 | } |
| 497 | |
| 498 | static void do_table(void *symval, unsigned long size, |
| 499 | unsigned long id_size, |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 500 | const char *device_id, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | void *function, |
| 502 | struct module *mod) |
| 503 | { |
| 504 | unsigned int i; |
| 505 | char alias[500]; |
| 506 | int (*do_entry)(const char *, void *entry, char *alias) = function; |
| 507 | |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 508 | device_id_size_check(mod->name, device_id, size, id_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | /* Leave last one: it's the terminator. */ |
| 510 | size -= id_size; |
| 511 | |
| 512 | for (i = 0; i < size; i += id_size) { |
| 513 | if (do_entry(mod->name, symval+i, alias)) { |
| 514 | /* Always end in a wildcard, for future extension */ |
| 515 | if (alias[strlen(alias)-1] != '*') |
| 516 | strcat(alias, "*"); |
| 517 | buf_printf(&mod->dev_table_buf, |
| 518 | "MODULE_ALIAS(\"%s\");\n", alias); |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /* Create MODULE_ALIAS() statements. |
| 524 | * At this time, we cannot write the actual output C source yet, |
| 525 | * so we write into the mod->dev_table_buf buffer. */ |
| 526 | void handle_moddevtable(struct module *mod, struct elf_info *info, |
| 527 | Elf_Sym *sym, const char *symname) |
| 528 | { |
| 529 | void *symval; |
| 530 | |
| 531 | /* We're looking for a section relative symbol */ |
| 532 | if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum) |
| 533 | return; |
| 534 | |
| 535 | symval = (void *)info->hdr |
| 536 | + info->sechdrs[sym->st_shndx].sh_offset |
| 537 | + sym->st_value; |
| 538 | |
| 539 | if (sym_is(symname, "__mod_pci_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 540 | do_table(symval, sym->st_size, |
| 541 | sizeof(struct pci_device_id), "pci", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | do_pci_entry, mod); |
| 543 | else if (sym_is(symname, "__mod_usb_device_table")) |
Roman Kagan | b19dcd9 | 2005-04-22 15:07:01 -0700 | [diff] [blame] | 544 | /* special case to handle bcdDevice ranges */ |
| 545 | do_usb_table(symval, sym->st_size, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | else if (sym_is(symname, "__mod_ieee1394_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 547 | do_table(symval, sym->st_size, |
| 548 | sizeof(struct ieee1394_device_id), "ieee1394", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | do_ieee1394_entry, mod); |
| 550 | else if (sym_is(symname, "__mod_ccw_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 551 | do_table(symval, sym->st_size, |
| 552 | sizeof(struct ccw_device_id), "ccw", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | do_ccw_entry, mod); |
Martin Schwidefsky | 1534c38 | 2006-09-20 15:58:25 +0200 | [diff] [blame] | 554 | else if (sym_is(symname, "__mod_ap_device_table")) |
| 555 | do_table(symval, sym->st_size, |
| 556 | sizeof(struct ap_device_id), "ap", |
| 557 | do_ap_entry, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | else if (sym_is(symname, "__mod_serio_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 559 | do_table(symval, sym->st_size, |
| 560 | sizeof(struct serio_device_id), "serio", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | do_serio_entry, mod); |
Thomas Renninger | 29b71a1 | 2007-07-23 14:43:51 +0200 | [diff] [blame^] | 562 | else if (sym_is(symname, "__mod_acpi_device_table")) |
| 563 | do_table(symval, sym->st_size, |
| 564 | sizeof(struct acpi_device_id), "acpi", |
| 565 | do_acpi_entry, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | else if (sym_is(symname, "__mod_pnp_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 567 | do_table(symval, sym->st_size, |
| 568 | sizeof(struct pnp_device_id), "pnp", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | do_pnp_entry, mod); |
| 570 | else if (sym_is(symname, "__mod_pnp_card_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 571 | do_table(symval, sym->st_size, |
| 572 | sizeof(struct pnp_card_device_id), "pnp_card", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | do_pnp_card_entry, mod); |
Dominik Brodowski | 90829cf | 2005-06-27 16:28:12 -0700 | [diff] [blame] | 574 | else if (sym_is(symname, "__mod_pcmcia_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 575 | do_table(symval, sym->st_size, |
| 576 | sizeof(struct pcmcia_device_id), "pcmcia", |
Dominik Brodowski | 90829cf | 2005-06-27 16:28:12 -0700 | [diff] [blame] | 577 | do_pcmcia_entry, mod); |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 578 | else if (sym_is(symname, "__mod_of_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 579 | do_table(symval, sym->st_size, |
| 580 | sizeof(struct of_device_id), "of", |
Jeff Mahoney | 5e65577 | 2005-07-06 15:44:41 -0400 | [diff] [blame] | 581 | do_of_entry, mod); |
Stephen Rothwell | fb120da | 2005-08-17 16:42:59 +1000 | [diff] [blame] | 582 | else if (sym_is(symname, "__mod_vio_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 583 | do_table(symval, sym->st_size, |
| 584 | sizeof(struct vio_device_id), "vio", |
Stephen Rothwell | fb120da | 2005-08-17 16:42:59 +1000 | [diff] [blame] | 585 | do_vio_entry, mod); |
Greg Kroah-Hartman | a9d1b24 | 2005-10-22 00:23:27 +0200 | [diff] [blame] | 586 | else if (sym_is(symname, "__mod_i2c_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 587 | do_table(symval, sym->st_size, |
| 588 | sizeof(struct i2c_device_id), "i2c", |
Greg Kroah-Hartman | a9d1b24 | 2005-10-22 00:23:27 +0200 | [diff] [blame] | 589 | do_i2c_entry, mod); |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 590 | else if (sym_is(symname, "__mod_input_device_table")) |
Sam Ravnborg | fb33d81 | 2006-07-09 16:26:07 +0200 | [diff] [blame] | 591 | do_table(symval, sym->st_size, |
| 592 | sizeof(struct input_device_id), "input", |
Rusty Russell | 1d8f430 | 2005-12-07 21:40:34 +0100 | [diff] [blame] | 593 | do_input_entry, mod); |
Michael Tokarev | 07563c7 | 2006-09-27 01:50:56 -0700 | [diff] [blame] | 594 | else if (sym_is(symname, "__mod_eisa_device_table")) |
| 595 | do_table(symval, sym->st_size, |
| 596 | sizeof(struct eisa_device_id), "eisa", |
| 597 | do_eisa_entry, mod); |
Kyle McMartin | f3cf267 | 2007-01-13 14:58:21 -0500 | [diff] [blame] | 598 | else if (sym_is(symname, "__mod_parisc_device_table")) |
| 599 | do_table(symval, sym->st_size, |
| 600 | sizeof(struct parisc_device_id), "parisc", |
| 601 | do_parisc_entry, mod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* Now add out buffered information to the generated C source */ |
| 605 | void add_moddevtable(struct buffer *buf, struct module *mod) |
| 606 | { |
| 607 | buf_printf(buf, "\n"); |
| 608 | buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos); |
| 609 | free(mod->dev_table_buf.p); |
| 610 | } |