/* Simple code to turn various tables in an ELF file into alias definitions. * This deals with kernel datastructures where they should be * dealt with: in the kernel source. * * Copyright 2002-2003 Rusty Russell, IBM Corporation * 2003 Kai Germaschewski * * * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. */ #include "modpost.h" /* We use the ELF typedefs for kernel_ulong_t but bite the bullet and * use either stdint.h or inttypes.h for the rest. */ #if KERNEL_ELFCLASS == ELFCLASS32 typedef Elf32_Addr kernel_ulong_t; #define BITS_PER_LONG 32 #else typedef Elf64_Addr kernel_ulong_t; #define BITS_PER_LONG 64 #endif #ifdef __sun__ #include #else #include #endif #include typedef uint32_t __u32; typedef uint16_t __u16; typedef unsigned char __u8; /* Big exception to the "don't include kernel headers into userspace, which * even potentially has different endianness and word sizes, since * we handle those differences explicitly below */ #include "../../include/linux/mod_devicetable.h" #define ADD(str, sep, cond, field) \ do { \ strcat(str, sep); \ if (cond) \ sprintf(str + strlen(str), \ sizeof(field) == 1 ? "%02X" : \ sizeof(field) == 2 ? "%04X" : \ sizeof(field) == 4 ? "%08X" : "", \ field); \ else \ sprintf(str + strlen(str), "*"); \ } while(0) /* Always end in a wildcard, for future extension */ static inline void add_wildcard(char *str) { int len = strlen(str); if (str[len - 1] != '*') strcat(str + len, "*"); } unsigned int cross_build = 0; /** * Check that sizeof(device_id type) are consistent with size of section * in .o file. If in-consistent then userspace and kernel does not agree * on actual size which is a bug. * Also verify that the final entry in the table is all zeros. * Ignore both checks if build host differ from target host and size differs. **/ static void device_id_check(const char *modname, const char *device_id, unsigned long size, unsigned long id_size, void *symval) { int i; if (size % id_size || size < id_size) { if (cross_build != 0) return; fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo " "of the size of section __mod_%s_device_table=%lu.\n" "Fix definition of struct %s_device_id " "in mod_devicetable.h\n", modname, device_id, id_size, device_id, size, device_id); } /* Verify last one is a terminator */ for (i = 0; i < id_size; i++ ) { if (*(uint8_t*)(symval+size-id_size+i)) { fprintf(stderr,"%s: struct %s_device_id is %lu bytes. " "The last of %lu is:\n", modname, device_id, id_size, size / id_size); for (i = 0; i < id_size; i++ ) fprintf(stderr,"0x%02x ", *(uint8_t*)(symval+size-id_size+i) ); fprintf(stderr,"\n"); fatal("%s: struct %s_device_id is not terminated " "with a NULL entry!\n", modname, device_id); } } } /* USB is special because the bcdDevice can be matched against a numeric range */ /* Looks like "usb:vNpNdNdcNdscNdpNicNiscNipN" */ static void do_usb_entry(struct usb_device_id *id, unsigned int bcdDevice_initial, int bcdDevice_initial_digits, unsigned char range_lo, unsigned char range_hi, struct module *mod) { char alias[500]; strcpy(alias, "usb:"); ADD(alias, "v", id->match_flags&USB_DEVICE_ID_MATCH_VENDOR, id->idVendor); ADD(alias, "p", id->match_flags&USB_DEVICE_ID_MATCH_PRODUCT, id->idProduct); strcat(alias, "d"); if (bcdDevice_initial_digits) sprintf(alias + strlen(alias), "%0*X", bcdDevice_initial_digits, bcdDevice_initial); if (range_lo == range_hi) sprintf(alias + strlen(alias), "%u", range_lo); else if (range_lo > 0 || range_hi < 9) sprintf(alias + strlen(alias), "[%u-%u]", range_lo, range_hi); if (bcdDevice_initial_digits < (sizeof(id->bcdDevice_lo) * 2 - 1)) strcat(alias, "*"); ADD(alias, "dc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_CLASS, id->bDeviceClass); ADD(alias, "dsc", id->match_flags&USB_DEVICE_ID_MATCH_DEV_SUBCLASS, id->bDeviceSubClass); ADD(alias, "dp", id->match_flags&USB_DEVICE_ID_MATCH_DEV_PROTOCOL, id->bDeviceProtocol); ADD(alias, "ic", id->match_flags&USB_DEVICE_ID_MATCH_INT_CLASS, id->bInterfaceClass); ADD(alias, "isc", id->match_flags&USB_DEVICE_ID_MATCH_INT_SUBCLASS, id->bInterfaceSubClass); ADD(alias, "ip", id->match_flags&USB_DEVICE_ID_MATCH_INT_PROTOCOL, id->bInterfaceProtocol); add_wildcard(alias); buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias); } static void do_usb_entry_multi(struct usb_device_id *id, struct module *mod) { unsigned int devlo, devhi; unsigned char chi, clo; int ndigits; id->match_flags = TO_NATIVE(id->match_flags); id->idVendor = TO_NATIVE(id->idVendor); id->idProduct = TO_NATIVE(id->idProduct); devlo = id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO ? TO_NATIVE(id->bcdDevice_lo) : 0x0U; devhi = id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI ? TO_NATIVE(id->bcdDevice_hi) : ~0x0U; /* * Some modules (visor) have empty slots as placeholder for * run-time specification that results in catch-all alias */ if (!(id->idVendor | id->idProduct | id->bDeviceClass | id->bInterfaceClass)) return; /* Convert numeric bcdDevice range into fnmatch-able pattern(s) */ for (ndigits = sizeof(id->bcdDevice_lo) * 2 - 1; devlo <= devhi; ndigits--) { clo = devlo & 0xf; chi = devhi & 0xf; if (chi > 9) /* it's bcd not hex */ chi = 9; devlo >>= 4; devhi >>= 4; if (devlo == devhi || !ndigits) { do_usb_entry(id, devlo, ndigits, clo, chi, mod); break; } if (clo > 0) do_usb_entry(id, devlo++, ndigits, clo, 9, mod); if (chi < 9) do_usb_entry(id, devhi--, ndigits, 0, chi, mod); } } static void do_usb_table(void *symval, unsigned long size, struct module *mod) { unsigned int i; const unsigned long id_size = sizeof(struct usb_device_id); device_id_check(mod->name, "usb", size, id_size, symval); /* Leave last one: it's the terminator. */ size -= id_size; for (i = 0; i < size; i += id_size) do_usb_entry_multi(symval + i, mod); } /* Looks like: hid:bNvNpN */ static int do_hid_entry(const char *filename, struct hid_device_id *id, char *alias) { id->bus = TO_NATIVE(id->bus); id->vendor = TO_NATIVE(id->vendor); id->product = TO_NATIVE(id->product); sprintf(alias, "hid:b%04X", id->bus); ADD(alias, "v", id->vendor != HID_ANY_ID, id->vendor); ADD(alias, "p", id->product != HID_ANY_ID, id->product); return 1; } /* Looks like: ieee1394:venNmoNspNverN */ static int do_ieee1394_entry(const char *filename, struct ieee1394_device_id *id, char *alias) { id->match_flags = TO_NATIVE(id->match_flags); id->vendor_id = TO_NATIVE(id->vendor_id); id->model_id = TO_NATIVE(id->model_id); id->specifier_id = TO_NATIVE(id->specifier_id); id->version = TO_NATIVE(id->version); strcpy(alias, "ieee1394:"); ADD(alias, "ven", id->match_flags & IEEE1394_MATCH_VENDOR_ID, id->vendor_id); ADD(alias, "mo", id->match_flags & IEEE1394_MATCH_MODEL_ID, id->model_id); ADD(alias, "sp", id->match_flags & IEEE1394_MATCH_SPECIFIER_ID, id->specifier_id); ADD(alias, "ver", id->match_flags & IEEE1394_MATCH_VERSION, id->version); add_wildcard(alias); return 1; } /* Looks like: pci:vNdNsvNsdNbcNscNiN. */ static int do_pci_entry(const char *filename, struct pci_device_id *id, char *alias) { /* Class field can be divided into these three. */ unsigned char baseclass, subclass, interface, baseclass_mask, subclass_mask, interface_mask; id->vendor = TO_NATIVE(id->vendor); id->device = TO_NATIVE(id->device); id->subvendor = TO_NATIVE(id->subvendor); id->subdevice = TO_NATIVE(id->subdevice); id->class = TO_NATIVE(id->class); id->class_mask = TO_NATIVE(id->class_mask); strcpy(alias, "pci:"); ADD(alias, "v", id->vendor != PCI_ANY_ID, id->vendor); ADD(alias, "d", id->device != PCI_ANY_ID, id->device); ADD(alias, "sv", id->subvendor != PCI_ANY_ID, id->subvendor); ADD(alias, "sd", id->subdevice != PCI_ANY_ID, id->subdevice); baseclass = (id->class) >> 16; baseclass_mask = (id->class_mask) >> 16; subclass = (id->class) >> 8; subclass_mask = (id->class_mask) >> 8; interface = id->class; interface_mask = id->class_mask; if ((baseclass_mask != 0 && baseclass_mask != 0xFF) || (subclass_mask != 0 && subclass_mask != 0xFF) || (interface_mask != 0 && interface_mask != 0xFF)) { warn("Can't handle masks in %s:%04X\n", filename, id->class_mask); return 0; } ADD(alias, "bc", baseclass_mask == 0xFF, baseclass); ADD(alias, "sc", subclass_mask == 0xFF, subclass); ADD(alias, "i", interface_mask == 0xFF, interface); add_wildcard(alias); return 1; } /* looks like: "ccw:tNmNdtNdmN" */ static int do_ccw_entry(const char *filename, struct ccw_device_id *id, char *alias) { id->match_flags = TO_NATIVE(id->match_flags); id->cu_type = TO_NATIVE(id->cu_type); id->cu_model = TO_NATIVE(id->cu_model); id->dev_type = TO_NATIVE(id->dev_type); id->dev_model = TO_NATIVE(id->dev_model); strcpy(alias, "ccw:"); ADD(alias, "t", id->match_flags&CCW_DEVICE_ID_MATCH_CU_TYPE, id->cu_type); ADD(alias, "m", id->match_flags&CCW_DEVICE_ID_MATCH_CU_MODEL, id->cu_model); ADD(alias, "dt", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_TYPE, id->dev_type); ADD(alias, "dm", id->match_flags&CCW_DEVICE_ID_MATCH_DEVICE_MODEL, id->dev_model); add_wildcard(alias); return 1; } /* looks like: "ap:tN" */ static int do_ap_entry(const char *filename, struct ap_device_id *id, char *alias) { sprintf(alias, "ap:t%02X*", id->dev_type); return 1; } /* looks like: "css:tN" */ static int do_css_entry(const char *filename, struct css_device_id *id, char *alias) { sprintf(alias, "css:t%01X", id->type); return 1; } /* Looks like: "serio:tyNprNidNexN" */ static int do_serio_entry(const char *filename, struct serio_device_id *id, char *alias) { id->type = TO_NATIVE(id->type); id->proto = TO_NATIVE(id->proto); id->id = TO_NATIVE(id->id); id->extra = TO_NATIVE(id->extra); strcpy(alias, "serio:"); ADD(alias, "ty", id->type != SERIO_ANY, id->type); ADD(alias, "pr", id->proto != SERIO_ANY, id->proto); ADD(alias, "id", id->id != SERIO_ANY, id->id); ADD(alias, "ex", id->extra != SERIO_ANY, id->extra); add_wildcard(alias); return 1; } /* looks like: "acpi:ACPI0003 or acpi:PNP0C0B" or "acpi:LNXVIDEO" */ static int do_acpi_entry(const char *filename, struct acpi_device_id *id, char *alias) { sprintf(alias, "acpi*:%s:*", id->id); return 1; } /* looks like: "pnp:dD" */ static void do_pnp_device_entry(void *symval, unsigned long size, struct module *mod) { const unsigned long id_size = sizeof(struct pnp_device_id); const unsigned int count = (size / id_size)-1; const struct pnp_device_id *devs = symval; unsigned int i; device_id_check(mod->name, "pnp", size, id_size, symval); for (i = 0; i < count; i++) { const char *id = (char *)devs[i].id; char acpi_id[sizeof(devs[0].id)]; int j; buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"pnp:d%s*\");\n", id); /* fix broken pnp bus lowercasing */ for (j = 0; j < sizeof(acpi_id); j++) acpi_id[j] = toupper(id[j]); buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id); } } /* looks like: "pnp:dD" for every device of the card */ static void do_pnp_card_entries(void *symval, unsigned long size, struct module *mod) { const unsigned long id_size = sizeof(struct pnp_card_device_id); const unsigned int count = (size / id_size)-1; const struct pnp_card_device_id *cards = symval; unsigned int i; device_id_check(mod->name, "pnp", size, id_size, symval); for (i = 0; i < count; i++) { unsigned int j; const struct pnp_card_device_id *card = &cards[i]; for (j = 0; j < PNP_MAX_DEVICES; j++) { const char *id = (char *)card->devs[j].id; int i2, j2; int dup = 0; if (!id[0]) break; /* find duplicate, already added value */ for (i2 = 0; i2 < i && !dup; i2++) { const struct pnp_card_device_id *card2 = &cards[i2]; for (j2 = 0; j2 < PNP_MAX_DEVICES; j2++) { const char *id2 = (char *)card2->devs[j2].id; if (!id2[0]) break; if (!strcmp(id, id2)) { dup = 1; break; } } } /* add an individual alias for every device entry */ if (!dup) { char acpi_id[sizeof(card->devs[0].id)]; int k; buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"pnp:d%s*\");\n", id); /* fix broken pnp bus lowercasing */ for (k = 0; k < sizeof(acpi_id); k++) acpi_id[k] = toupper(id[k]); buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"acpi*:%s:*\");\n", acpi_id); } } } } /* Looks like: pcmcia:mNcNfNfnNpfnNvaNvbNvcNvdN. */ static int do_pcmcia_entry(const char *filename, struct pcmcia_device_id *id, char *alias) { unsigned int i; id->match_flags = TO_NATIVE(id->match_flags); id->manf_id = TO_NATIVE(id->manf_id); id->card_id = TO_NATIVE(id->card_id); id->func_id = TO_NATIVE(id->func_id); id->function = TO_NATIVE(id->function); id->device_no = TO_NATIVE(id->device_no); for (i=0; i<4; i++) { id->prod_id_hash[i] = TO_NATIVE(id->prod_id_hash[i]); } strcpy(alias, "pcmcia:"); ADD(alias, "m", id->match_flags & PCMCIA_DEV_ID_MATCH_MANF_ID, id->manf_id); ADD(alias, "c", id->match_flags & PCMCIA_DEV_ID_MATCH_CARD_ID, id->card_id); ADD(alias, "f", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNC_ID, id->func_id); ADD(alias, "fn", id->match_flags & PCMCIA_DEV_ID_MATCH_FUNCTION, id->function); ADD(alias, "pfn", id->match_flags & PCMCIA_DEV_ID_MATCH_DEVICE_NO, id->device_no); ADD(alias, "pa", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID1, id->prod_id_hash[0]); ADD(alias, "pb", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID2, id->prod_id_hash[1]); ADD(alias, "pc", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID3, id->prod_id_hash[2]); ADD(alias, "pd", id->match_flags & PCMCIA_DEV_ID_MATCH_PROD_ID4, id->prod_id_hash[3]); add_wildcard(alias); return 1; } static int do_of_entry (const char *filename, struct of_device_id *of, char *alias) { int len; char *tmp; len = sprintf (alias, "of:N%sT%s", of->name[0] ? of->name : "*", of->type[0] ? of->type : "*"); if (of->compatible[0]) sprintf (&alias[len], "%sC%s", of->type[0] ? "*" : "", of->compatible); /* Replace all whitespace with underscores */ for (tmp = alias; tmp && *tmp; tmp++) if (isspace (*tmp)) *tmp = '_'; add_wildcard(alias); return 1; } static int do_vio_entry(const char *filename, struct vio_device_id *vio, char *alias) { char *tmp; sprintf(alias, "vio:T%sS%s", vio->type[0] ? vio->type : "*", vio->compat[0] ? vio->compat : "*"); /* Replace all whitespace with underscores */ for (tmp = alias; tmp && *tmp; tmp++) if (isspace (*tmp)) *tmp = '_'; add_wildcard(alias); return 1; } #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) static void do_input(char *alias, kernel_ulong_t *arr, unsigned int min, unsigned int max) { unsigned int i; for (i = min; i < max; i++) if (arr[i / BITS_PER_LONG] & (1L << (i%BITS_PER_LONG))) sprintf(alias + strlen(alias), "%X,*", i); } /* input:b0v0p0e0-eXkXrXaXmXlXsXfXwX where X is comma-separated %02X. */ static int do_input_entry(const char *filename, struct input_device_id *id, char *alias) { sprintf(alias, "input:"); ADD(alias, "b", id->flags & INPUT_DEVICE_ID_MATCH_BUS, id->bustype); ADD(alias, "v", id->flags & INPUT_DEVICE_ID_MATCH_VENDOR, id->vendor); ADD(alias, "p", id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT, id->product); ADD(alias, "e", id->flags & INPUT_DEVICE_ID_MATCH_VERSION, id->version); sprintf(alias + strlen(alias), "-e*"); if (id->flags & INPUT_DEVICE_ID_MATCH_EVBIT) do_input(alias, id->evbit, 0, INPUT_DEVICE_ID_EV_MAX); sprintf(alias + strlen(alias), "k*"); if (id->flags & INPUT_DEVICE_ID_MATCH_KEYBIT) do_input(alias, id->keybit, INPUT_DEVICE_ID_KEY_MIN_INTERESTING, INPUT_DEVICE_ID_KEY_MAX); sprintf(alias + strlen(alias), "r*"); if (id->flags & INPUT_DEVICE_ID_MATCH_RELBIT) do_input(alias, id->relbit, 0, INPUT_DEVICE_ID_REL_MAX); sprintf(alias + strlen(alias), "a*"); if (id->flags & INPUT_DEVICE_ID_MATCH_ABSBIT) do_input(alias, id->absbit, 0, INPUT_DEVICE_ID_ABS_MAX); sprintf(alias + strlen(alias), "m*"); if (id->flags & INPUT_DEVICE_ID_MATCH_MSCIT) do_input(alias, id->mscbit, 0, INPUT_DEVICE_ID_MSC_MAX); sprintf(alias + strlen(alias), "l*"); if (id->flags & INPUT_DEVICE_ID_MATCH_LEDBIT) do_input(alias, id->ledbit, 0, INPUT_DEVICE_ID_LED_MAX); sprintf(alias + strlen(alias), "s*"); if (id->flags & INPUT_DEVICE_ID_MATCH_SNDBIT) do_input(alias, id->sndbit, 0, INPUT_DEVICE_ID_SND_MAX); sprintf(alias + strlen(alias), "f*"); if (id->flags & INPUT_DEVICE_ID_MATCH_FFBIT) do_input(alias, id->ffbit, 0, INPUT_DEVICE_ID_FF_MAX); sprintf(alias + strlen(alias), "w*"); if (id->flags & INPUT_DEVICE_ID_MATCH_SWBIT) do_input(alias, id->swbit, 0, INPUT_DEVICE_ID_SW_MAX); return 1; } static int do_eisa_entry(const char *filename, struct eisa_device_id *eisa, char *alias) { if (eisa->sig[0]) sprintf(alias, EISA_DEVICE_MODALIAS_FMT "*", eisa->sig); else strcat(alias, "*"); return 1; } /* Looks like: parisc:tNhvNrevNsvN */ static int do_parisc_entry(const char *filename, struct parisc_device_id *id, char *alias) { id->hw_type = TO_NATIVE(id->hw_type); id->hversion = TO_NATIVE(id->hversion); id->hversion_rev = TO_NATIVE(id->hversion_rev); id->sversion = TO_NATIVE(id->sversion); strcpy(alias, "parisc:"); ADD(alias, "t", id->hw_type != PA_HWTYPE_ANY_ID, id->hw_type); ADD(alias, "hv", id->hversion != PA_HVERSION_ANY_ID, id->hversion); ADD(alias, "rev", id->hversion_rev != PA_HVERSION_REV_ANY_ID, id->hversion_rev); ADD(alias, "sv", id->sversion != PA_SVERSION_ANY_ID, id->sversion); add_wildcard(alias); return 1; } /* Looks like: sdio:cNvNdN. */ static int do_sdio_entry(const char *filename, struct sdio_device_id *id, char *alias) { id->class = TO_NATIVE(id->class); id->vendor = TO_NATIVE(id->vendor); id->device = TO_NATIVE(id->device); strcpy(alias, "sdio:"); ADD(alias, "c", id->class != (__u8)SDIO_ANY_ID, id->class); ADD(alias, "v", id->vendor != (__u16)SDIO_ANY_ID, id->vendor); ADD(alias, "d", id->device != (__u16)SDIO_ANY_ID, id->device); add_wildcard(alias); return 1; } /* Looks like: ssb:vNidNrevN. */ static int do_ssb_entry(const char *filename, struct ssb_device_id *id, char *alias) { id->vendor = TO_NATIVE(id->vendor); id->coreid = TO_NATIVE(id->coreid); id->revision = TO_NATIVE(id->revision); strcpy(alias, "ssb:"); ADD(alias, "v", id->vendor != SSB_ANY_VENDOR, id->vendor); ADD(alias, "id", id->coreid != SSB_ANY_ID, id->coreid); ADD(alias, "rev", id->revision != SSB_ANY_REV, id->revision); add_wildcard(alias); return 1; } /* Looks like: virtio:dNvN */ static int do_virtio_entry(const char *filename, struct virtio_device_id *id, char *alias) { id->device = TO_NATIVE(id->device); id->vendor = TO_NATIVE(id->vendor); strcpy(alias, "virtio:"); ADD(alias, "d", id->device != VIRTIO_DEV_ANY_ID, id->device); ADD(alias, "v", id->vendor != VIRTIO_DEV_ANY_ID, id->vendor); add_wildcard(alias); return 1; } /* Looks like: i2c:S */ static int do_i2c_entry(const char *filename, struct i2c_device_id *id, char *alias) { sprintf(alias, I2C_MODULE_PREFIX "%s", id->name); return 1; } /* Looks like: spi:S */ static int do_spi_entry(const char *filename, struct spi_device_id *id, char *alias) { sprintf(alias, SPI_MODULE_PREFIX "%s", id->name); return 1; } static const struct dmifield { const char *prefix; int field; } dmi_fields[] = { { "bvn", DMI_BIOS_VENDOR }, { "bvr", DMI_BIOS_VERSION }, { "bd", DMI_BIOS_DATE }, { "svn", DMI_SYS_VENDOR }, { "pn", DMI_PRODUCT_NAME }, { "pvr", DMI_PRODUCT_VERSION }, { "rvn", DMI_BOARD_VENDOR }, { "rn", DMI_BOARD_NAME }, { "rvr", DMI_BOARD_VERSION }, { "cvn", DMI_CHASSIS_VENDOR }, { "ct", DMI_CHASSIS_TYPE }, { "cvr", DMI_CHASSIS_VERSION }, { NULL, DMI_NONE } }; static void dmi_ascii_filter(char *d, const char *s) { /* Filter out characters we don't want to see in the modalias string */ for (; *s; s++) if (*s > ' ' && *s < 127 && *s != ':') *(d++) = *s; *d = 0; } static int do_dmi_entry(const char *filename, struct dmi_system_id *id, char *alias) { int i, j; sprintf(alias, "dmi*"); for (i = 0; i < ARRAY_SIZE(dmi_fields); i++) { for (j = 0; j < 4; j++) { if (id->matches[j].slot && id->matches[j].slot == dmi_fields[i].field) { sprintf(alias + strlen(alias), ":%s*", dmi_fields[i].prefix); dmi_ascii_filter(alias + strlen(alias), id->matches[j].substr); strcat(alias, "*"); } } } strcat(alias, ":"); return 1; } static int do_platform_entry(const char *filename, struct platform_device_id *id, char *alias) { sprintf(alias, PLATFORM_MODULE_PREFIX "%s", id->name); return 1; } /* Ignore any prefix, eg. some architectures prepend _ */ static inline int sym_is(const char *symbol, const char *name) { const char *match; match = strstr(symbol, name); if (!match) return 0; return match[strlen(symbol)] == '\0'; } static void do_table(void *symval, unsigned long size, unsigned long id_size, const char *device_id, void *function, struct module *mod) { unsigned int i; char alias[500]; int (*do_entry)(const char *, void *entry, char *alias) = function; device_id_check(mod->name, device_id, size, id_size, symval); /* Leave last one: it's the terminator. */ size -= id_size; for (i = 0; i < size; i += id_size) { if (do_entry(mod->name, symval+i, alias)) { buf_printf(&mod->dev_table_buf, "MODULE_ALIAS(\"%s\");\n", alias); } } } /* Create MODULE_ALIAS() statements. * At this time, we cannot write the actual output C source yet, * so we write into the mod->dev_table_buf buffer. */ void handle_moddevtable(struct module *mod, struct elf_info *info, Elf_Sym *sym, const char *symname) { void *symval; char *zeros = NULL; /* We're looking for a section relative symbol */ if (!sym->st_shndx || sym->st_shndx >= info->hdr->e_shnum) return; /* Handle all-NULL symbols allocated into .bss */ if (info->sechdrs[sym->st_shndx].sh_type & SHT_NOBITS) { zeros = calloc(1, sym->st_size); symval = zeros; } else { symval = (void *)info->hdr + info->sechdrs[sym->st_shndx].sh_offset + sym->st_value; } if (sym_is(symname, "__mod_pci_device_table")) do_table(symval, sym->st_size, sizeof(struct pci_device_id), "pci", do_pci_entry, mod); else if (sym_is(symname, "__mod_usb_device_table")) /* special case to handle bcdDevice ranges */ do_usb_table(symval, sym->st_size, mod); else if (sym_is(symname, "__mod_hid_device_table")) do_table(symval, sym->st_size, sizeof(struct hid_device_id), "hid", do_hid_entry, mod); else if (sym_is(symname, "__mod_ieee1394_device_table")) do_table(symval, sym->st_size, sizeof(struct ieee1394_device_id), "ieee1394", do_ieee1394_entry, mod); else if (sym_is(symname, "__mod_ccw_device_table")) do_table(symval, sym->st_size, sizeof(struct ccw_device_id), "ccw", do_ccw_entry, mod); else if (sym_is(symname, "__mod_ap_device_table")) do_table(symval, sym->st_size, sizeof(struct ap_device_id), "ap", do_ap_entry, mod); else if (sym_is(symname, "__mod_css_device_table")) do_table(symval, sym->st_size, sizeof(struct css_device_id), "css", do_css_entry, mod); else if (sym_is(symname, "__mod_serio_device_table")) do_table(symval, sym->st_size, sizeof(struct serio_device_id), "serio", do_serio_entry, mod); else if (sym_is(symname, "__mod_acpi_device_table")) do_table(symval, sym->st_size, sizeof(struct acpi_device_id), "acpi", do_acpi_entry, mod); else if (sym_is(symname, "__mod_pnp_device_table")) do_pnp_device_entry(symval, sym->st_size, mod); else if (sym_is(symname, "__mod_pnp_card_device_table")) do_pnp_card_entries(symval, sym->st_size, mod); else if (sym_is(symname, "__mod_pcmcia_device_table")) do_table(symval, sym->st_size, sizeof(struct pcmcia_device_id), "pcmcia", do_pcmcia_entry, mod); else if (sym_is(symname, "__mod_of_device_table")) do_table(symval, sym->st_size, sizeof(struct of_device_id), "of", do_of_entry, mod); else if (sym_is(symname, "__mod_vio_device_table")) do_table(symval, sym->st_size, sizeof(struct vio_device_id), "vio", do_vio_entry, mod); else if (sym_is(symname, "__mod_input_device_table")) do_table(symval, sym->st_size, sizeof(struct input_device_id), "input", do_input_entry, mod); else if (sym_is(symname, "__mod_eisa_device_table")) do_table(symval, sym->st_size, sizeof(struct eisa_device_id), "eisa", do_eisa_entry, mod); else if (sym_is(symname, "__mod_parisc_device_table")) do_table(symval, sym->st_size, sizeof(struct parisc_device_id), "parisc", do_parisc_entry, mod); else if (sym_is(symname, "__mod_sdio_device_table")) do_table(symval, sym->st_size, sizeof(struct sdio_device_id), "sdio", do_sdio_entry, mod); else if (sym_is(symname, "__mod_ssb_device_table")) do_table(symval, sym->st_size, sizeof(struct ssb_device_id), "ssb", do_ssb_entry, mod); else if (sym_is(symname, "__mod_virtio_device_table")) do_table(symval, sym->st_size, sizeof(struct virtio_device_id), "virtio", do_virtio_entry, mod); else if (sym_is(symname, "__mod_i2c_device_table")) do_table(symval, sym->st_size, sizeof(struct i2c_device_id), "i2c", do_i2c_entry, mod); else if (sym_is(symname, "__mod_spi_device_table")) do_table(symval, sym->st_size, sizeof(struct spi_device_id), "spi", do_spi_entry, mod); else if (sym_is(symname, "__mod_dmi_device_table")) do_table(symval, sym->st_size, sizeof(struct dmi_system_id), "dmi", do_dmi_entry, mod); else if (sym_is(symname, "__mod_platform_device_table")) do_table(symval, sym->st_size, sizeof(struct platform_device_id), "platform", do_platform_entry, mod); free(zeros); } /* Now add out buffered information to the generated C source */ void add_moddevtable(struct buffer *buf, struct module *mod) { buf_printf(buf, "\n"); buf_write(buf, mod->dev_table_buf.p, mod->dev_table_buf.pos); free(mod->dev_table_buf.p); } 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021
/**
  * Functions implementing wlan scan IOCTL and firmware command APIs
  *
  * IOCTL handlers as well as command preperation and response routines
  *  for sending scan commands to the firmware.
  */
#include <linux/ctype.h>
#include <linux/if.h>
#include <linux/netdevice.h>
#include <linux/wireless.h>

#include <net/ieee80211.h>
#include <net/iw_handler.h>

#include "host.h"
#include "decl.h"
#include "dev.h"
#include "scan.h"

//! Approximate amount of data needed to pass a scan result back to iwlist
#define MAX_SCAN_CELL_SIZE  (IW_EV_ADDR_LEN             \
                             + IW_ESSID_MAX_SIZE        \
                             + IW_EV_UINT_LEN           \
                             + IW_EV_FREQ_LEN           \
                             + IW_EV_QUAL_LEN           \
                             + IW_ESSID_MAX_SIZE        \
                             + IW_EV_PARAM_LEN          \
                             + 40)	/* 40 for WPAIE */

//! Memory needed to store a max sized channel List TLV for a firmware scan
#define CHAN_TLV_MAX_SIZE  (sizeof(struct mrvlietypesheader)    \
                            + (MRVDRV_MAX_CHANNELS_PER_SCAN     \
                               * sizeof(struct chanscanparamset)))

//! Memory needed to store a max number/size SSID TLV for a firmware scan
#define SSID_TLV_MAX_SIZE  (1 * sizeof(struct mrvlietypes_ssidparamset))

//! Maximum memory needed for a wlan_scan_cmd_config with all TLVs at max
#define MAX_SCAN_CFG_ALLOC (sizeof(struct wlan_scan_cmd_config)  \
                            + sizeof(struct mrvlietypes_numprobes)   \
                            + CHAN_TLV_MAX_SIZE                 \
                            + SSID_TLV_MAX_SIZE)

//! The maximum number of channels the firmware can scan per command
#define MRVDRV_MAX_CHANNELS_PER_SCAN   14

/**
 * @brief Number of channels to scan per firmware scan command issuance.
 *
 *  Number restricted to prevent hitting the limit on the amount of scan data
 *  returned in a single firmware scan command.
 */
#define MRVDRV_CHANNELS_PER_SCAN_CMD   4

//! Scan time specified in the channel TLV for each channel for passive scans
#define MRVDRV_PASSIVE_SCAN_CHAN_TIME  100

//! Scan time specified in the channel TLV for each channel for active scans
#define MRVDRV_ACTIVE_SCAN_CHAN_TIME   100

//! Macro to enable/disable SSID checking before storing a scan table
#ifdef DISCARD_BAD_SSID
#define CHECK_SSID_IS_VALID(x) ssid_valid(&bssidEntry.ssid)
#else
#define CHECK_SSID_IS_VALID(x) 1
#endif

/**
 *  @brief Check if a scanned network compatible with the driver settings
 *
 *   WEP     WPA     WPA2    ad-hoc  encrypt                      Network
 * enabled enabled  enabled   AES     mode   privacy  WPA  WPA2  Compatible
 *    0       0        0       0      NONE      0      0    0   yes No security
 *    1       0        0       0      NONE      1      0    0   yes Static WEP
 *    0       1        0       0       x        1x     1    x   yes WPA
 *    0       0        1       0       x        1x     x    1   yes WPA2
 *    0       0        0       1      NONE      1      0    0   yes Ad-hoc AES
 *    0       0        0       0     !=NONE     1      0    0   yes Dynamic WEP
 *
 *
 *  @param adapter A pointer to wlan_adapter
 *  @param index   Index in scantable to check against current driver settings
 *  @param mode    Network mode: Infrastructure or IBSS
 *
 *  @return        Index in scantable, or error code if negative
 */
static int is_network_compatible(wlan_adapter * adapter, int index, u8 mode)
{
	ENTER();

	if (adapter->scantable[index].mode == mode) {
		if (   !adapter->secinfo.wep_enabled
		    && !adapter->secinfo.WPAenabled
		    && !adapter->secinfo.WPA2enabled
		    && adapter->scantable[index].wpa_ie[0] != WPA_IE
		    && adapter->scantable[index].rsn_ie[0] != WPA2_IE
		    && !adapter->scantable[index].privacy) {
			/* no security */
			LEAVE();
			return index;
		} else if (   adapter->secinfo.wep_enabled
			   && !adapter->secinfo.WPAenabled
			   && !adapter->secinfo.WPA2enabled
			   && adapter->scantable[index].privacy) {
			/* static WEP enabled */
			LEAVE();
			return index;
		} else if (   !adapter->secinfo.wep_enabled
			   && adapter->secinfo.WPAenabled
			   && !adapter->secinfo.WPA2enabled
			   && (adapter->scantable[index].wpa_ie[0] == WPA_IE)
			   /* privacy bit may NOT be set in some APs like LinkSys WRT54G
			      && adapter->scantable[index].privacy */
		    ) {
			/* WPA enabled */
			lbs_pr_debug(1,
			       "is_network_compatible() WPA: index=%d wpa_ie=%#x "
			       "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
			       "privacy=%#x\n", index,
			       adapter->scantable[index].wpa_ie[0],
			       adapter->scantable[index].rsn_ie[0],
			       adapter->secinfo.wep_enabled ? "e" : "d",
			       adapter->secinfo.WPAenabled ? "e" : "d",
			       adapter->secinfo.WPA2enabled ? "e" : "d",
			       adapter->scantable[index].privacy);
			LEAVE();
			return index;
		} else if (   !adapter->secinfo.wep_enabled
			   && !adapter->secinfo.WPAenabled
			   && adapter->secinfo.WPA2enabled
			   && (adapter->scantable[index].rsn_ie[0] == WPA2_IE)
			   /* privacy bit may NOT be set in some APs like LinkSys WRT54G
			      && adapter->scantable[index].privacy */
		    ) {
			/* WPA2 enabled */
			lbs_pr_debug(1,
			       "is_network_compatible() WPA2: index=%d wpa_ie=%#x "
			       "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s "
			       "privacy=%#x\n", index,
			       adapter->scantable[index].wpa_ie[0],
			       adapter->scantable[index].rsn_ie[0],
			       adapter->secinfo.wep_enabled ? "e" : "d",
			       adapter->secinfo.WPAenabled ? "e" : "d",
			       adapter->secinfo.WPA2enabled ? "e" : "d",
			       adapter->scantable[index].privacy);
			LEAVE();
			return index;
		} else if (   !adapter->secinfo.wep_enabled
			   && !adapter->secinfo.WPAenabled
			   && !adapter->secinfo.WPA2enabled
			   && (adapter->scantable[index].wpa_ie[0] != WPA_IE)
			   && (adapter->scantable[index].rsn_ie[0] != WPA2_IE)
			   && adapter->scantable[index].privacy) {
			/* dynamic WEP enabled */
			lbs_pr_debug(1,
			       "is_network_compatible() dynamic WEP: index=%d "
			       "wpa_ie=%#x wpa2_ie=%#x privacy=%#x\n",
			       index,
			       adapter->scantable[index].wpa_ie[0],
			       adapter->scantable[index].rsn_ie[0],
			       adapter->scantable[index].privacy);
			LEAVE();
			return index;
		}

		/* security doesn't match */
		lbs_pr_debug(1,
		       "is_network_compatible() FAILED: index=%d wpa_ie=%#x "
		       "wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s privacy=%#x\n",
		       index,
		       adapter->scantable[index].wpa_ie[0],
		       adapter->scantable[index].rsn_ie[0],
		       adapter->secinfo.wep_enabled ? "e" : "d",
		       adapter->secinfo.WPAenabled ? "e" : "d",
		       adapter->secinfo.WPA2enabled ? "e" : "d",
		       adapter->scantable[index].privacy);
		LEAVE();
		return -ECONNREFUSED;
	}

	/* mode doesn't match */
	LEAVE();
	return -ENETUNREACH;
}

/**
 *  @brief This function validates a SSID as being able to be printed
 *
 *  @param pssid   SSID structure to validate
 *
 *  @return        TRUE or FALSE
 */
static u8 ssid_valid(struct WLAN_802_11_SSID *pssid)
{
	int ssididx;

	for (ssididx = 0; ssididx < pssid->ssidlength; ssididx++) {
		if (!isprint(pssid->ssid[ssididx])) {
			return 0;
		}
	}

	return 1;
}

/**
 *  @brief Post process the scan table after a new scan command has completed
 *
 *  Inspect each entry of the scan table and try to find an entry that
 *    matches our current associated/joined network from the scan.  If
 *    one is found, update the stored copy of the bssdescriptor for our
 *    current network.
 *
 *  Debug dump the current scan table contents if compiled accordingly.
 *
 *  @param priv   A pointer to wlan_private structure
 *
 *  @return       void
 */
static void wlan_scan_process_results(wlan_private * priv)
{
	wlan_adapter *adapter = priv->adapter;
	int foundcurrent;
	int i;

	foundcurrent = 0;

	if (adapter->connect_status == libertas_connected) {
		/* try to find the current BSSID in the new scan list */
		for (i = 0; i < adapter->numinscantable; i++) {
			if (!libertas_SSID_cmp(&adapter->scantable[i].ssid,
				     &adapter->curbssparams.ssid) &&
			    !memcmp(adapter->curbssparams.bssid,
				    adapter->scantable[i].macaddress,
				    ETH_ALEN)) {
				foundcurrent = 1;
			}
		}

		if (foundcurrent) {
			/* Make a copy of current BSSID descriptor */
			memcpy(&adapter->curbssparams.bssdescriptor,
			       &adapter->scantable[i],
			       sizeof(adapter->curbssparams.bssdescriptor));
		}
	}

	for (i = 0; i < adapter->numinscantable; i++) {
		lbs_pr_debug(1, "Scan:(%02d) %02x:%02x:%02x:%02x:%02x:%02x, "
		       "RSSI[%03d], SSID[%s]\n",
		       i,
		       adapter->scantable[i].macaddress[0],
		       adapter->scantable[i].macaddress[1],
		       adapter->scantable[i].macaddress[2],
		       adapter->scantable[i].macaddress[3],
		       adapter->scantable[i].macaddress[4],
		       adapter->scantable[i].macaddress[5],
		       (s32) adapter->scantable[i].rssi,
		       adapter->scantable[i].ssid.ssid);
	}
}

/**
 *  @brief Create a channel list for the driver to scan based on region info
 *
 *  Use the driver region/band information to construct a comprehensive list
 *    of channels to scan.  This routine is used for any scan that is not
 *    provided a specific channel list to scan.
 *
 *  @param priv          A pointer to wlan_private structure
 *  @param scanchanlist  Output parameter: resulting channel list to scan
 *  @param filteredscan  Flag indicating whether or not a BSSID or SSID filter
 *                       is being sent in the command to firmware.  Used to
 *                       increase the number of channels sent in a scan
 *                       command and to disable the firmware channel scan
 *                       filter.
 *
 *  @return              void
 */
static void wlan_scan_create_channel_list(wlan_private * priv,
					  struct chanscanparamset * scanchanlist,
					  u8 filteredscan)
{

	wlan_adapter *adapter = priv->adapter;
	struct region_channel *scanregion;
	struct chan_freq_power *cfp;
	int rgnidx;
	int chanidx;
	int nextchan;
	u8 scantype;

	chanidx = 0;

	/* Set the default scan type to the user specified type, will later
	 *   be changed to passive on a per channel basis if restricted by
	 *   regulatory requirements (11d or 11h)
	 */
	scantype = adapter->scantype;

	for (rgnidx = 0; rgnidx < ARRAY_SIZE(adapter->region_channel); rgnidx++) {
		if (priv->adapter->enable11d &&
		    adapter->connect_status != libertas_connected) {
			/* Scan all the supported chan for the first scan */
			if (!adapter->universal_channel[rgnidx].valid)
				continue;
			scanregion = &adapter->universal_channel[rgnidx];

			/* clear the parsed_region_chan for the first scan */
			memset(&adapter->parsed_region_chan, 0x00,
			       sizeof(adapter->parsed_region_chan));
		} else {
			if (!adapter->region_channel[rgnidx].valid)
				continue;
			scanregion = &adapter->region_channel[rgnidx];
		}

		for (nextchan = 0;
		     nextchan < scanregion->nrcfp; nextchan++, chanidx++) {

			cfp = scanregion->CFP + nextchan;

			if (priv->adapter->enable11d) {
				scantype =
				    libertas_get_scan_type_11d(cfp->channel,
							   &adapter->
							   parsed_region_chan);
			}

			switch (scanregion->band) {
			case BAND_B:
			case BAND_G:
			default:
				scanchanlist[chanidx].radiotype =
				    cmd_scan_radio_type_bg;
				break;
			}

			if (scantype == cmd_scan_type_passive) {
				scanchanlist[chanidx].maxscantime =
				    cpu_to_le16
				    (MRVDRV_PASSIVE_SCAN_CHAN_TIME);
				scanchanlist[chanidx].chanscanmode.passivescan =
				    1;
			} else {
				scanchanlist[chanidx].maxscantime =
				    cpu_to_le16
				    (MRVDRV_ACTIVE_SCAN_CHAN_TIME);
				scanchanlist[chanidx].chanscanmode.passivescan =
				    0;
			}

			scanchanlist[chanidx].channumber = cfp->channel;

			if (filteredscan) {
				scanchanlist[chanidx].chanscanmode.
				    disablechanfilt = 1;
			}
		}
	}
}

/**
 *  @brief Construct a wlan_scan_cmd_config structure to use in issue scan cmds
 *
 *  Application layer or other functions can invoke wlan_scan_networks
 *    with a scan configuration supplied in a wlan_ioctl_user_scan_cfg struct.
 *    This structure is used as the basis of one or many wlan_scan_cmd_config
 *    commands that are sent to the command processing module and sent to
 *    firmware.
 *
 *  Create a wlan_scan_cmd_config based on the following user supplied
 *    parameters (if present):
 *             - SSID filter
 *             - BSSID filter
 *             - Number of Probes to be sent
 *             - channel list
 *
 *  If the SSID or BSSID filter is not present, disable/clear the filter.
 *  If the number of probes is not set, use the adapter default setting
 *  Qualify the channel
 *
 *  @param priv             A pointer to wlan_private structure
 *  @param puserscanin      NULL or pointer to scan configuration parameters
 *  @param ppchantlvout     Output parameter: Pointer to the start of the
 *                          channel TLV portion of the output scan config
 *  @param pscanchanlist    Output parameter: Pointer to the resulting channel
 *                          list to scan
 *  @param pmaxchanperscan  Output parameter: Number of channels to scan for
 *                          each issuance of the firmware scan command
 *  @param pfilteredscan    Output parameter: Flag indicating whether or not
 *                          a BSSID or SSID filter is being sent in the
 *                          command to firmware.  Used to increase the number
 *                          of channels sent in a scan command and to
 *                          disable the firmware channel scan filter.
 *  @param pscancurrentonly Output parameter: Flag indicating whether or not
 *                          we are only scanning our current active channel
 *
 *  @return                 resulting scan configuration
 */
static struct wlan_scan_cmd_config *
wlan_scan_setup_scan_config(wlan_private * priv,
			    const struct wlan_ioctl_user_scan_cfg * puserscanin,
			    struct mrvlietypes_chanlistparamset ** ppchantlvout,
			    struct chanscanparamset * pscanchanlist,
			    int *pmaxchanperscan,
			    u8 * pfilteredscan,
			    u8 * pscancurrentonly)
{
	wlan_adapter *adapter = priv->adapter;
	const u8 zeromac[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
	struct mrvlietypes_numprobes *pnumprobestlv;
	struct mrvlietypes_ssidparamset *pssidtlv;
	struct wlan_scan_cmd_config * pscancfgout = NULL;
	u8 *ptlvpos;
	u16 numprobes;
	u16 ssidlen;
	int chanidx;
	int scantype;
	int scandur;
	int channel;
	int radiotype;

	pscancfgout = kzalloc(MAX_SCAN_CFG_ALLOC, GFP_KERNEL);
	if (pscancfgout == NULL)
		goto out;

	/* The tlvbufferlen is calculated for each scan command.  The TLVs added
	 *   in this routine will be preserved since the routine that sends
	 *   the command will append channelTLVs at *ppchantlvout.  The difference
	 *   between the *ppchantlvout and the tlvbuffer start will be used
	 *   to calculate the size of anything we add in this routine.
	 */
	pscancfgout->tlvbufferlen = 0;

	/* Running tlv pointer.  Assigned to ppchantlvout at end of function
	 *  so later routines know where channels can be added to the command buf
	 */
	ptlvpos = pscancfgout->tlvbuffer;

	/*
	 * Set the initial scan paramters for progressive scanning.  If a specific
	 *   BSSID or SSID is used, the number of channels in the scan command
	 *   will be increased to the absolute maximum
	 */
	*pmaxchanperscan = MRVDRV_CHANNELS_PER_SCAN_CMD;

	/* Initialize the scan as un-filtered by firmware, set to TRUE below if
	 *   a SSID or BSSID filter is sent in the command
	 */
	*pfilteredscan = 0;

	/* Initialize the scan as not being only on the current channel.  If
	 *   the channel list is customized, only contains one channel, and
	 *   is the active channel, this is set true and data flow is not halted.
	 */
	*pscancurrentonly = 0;

	if (puserscanin) {

		/* Set the bss type scan filter, use adapter setting if unset */
		pscancfgout->bsstype =
		    (puserscanin->bsstype ? puserscanin->bsstype : adapter->
		     scanmode);

		/* Set the number of probes to send, use adapter setting if unset */
		numprobes = (puserscanin->numprobes ? puserscanin->numprobes :
			     adapter->scanprobes);

		/*
		 * Set the BSSID filter to the incoming configuration,
		 *   if non-zero.  If not set, it will remain disabled (all zeros).
		 */
		memcpy(pscancfgout->specificBSSID,
		       puserscanin->specificBSSID,
		       sizeof(pscancfgout->specificBSSID));

		ssidlen = strlen(puserscanin->specificSSID);

		if (ssidlen) {
			pssidtlv =
			    (struct mrvlietypes_ssidparamset *) pscancfgout->
			    tlvbuffer;
			pssidtlv->header.type = cpu_to_le16(TLV_TYPE_SSID);
			pssidtlv->header.len = cpu_to_le16(ssidlen);
			memcpy(pssidtlv->ssid, puserscanin->specificSSID,
			       ssidlen);
			ptlvpos += sizeof(pssidtlv->header) + ssidlen;
		}

		/*
		 *  The default number of channels sent in the command is low to
		 *    ensure the response buffer from the firmware does not truncate
		 *    scan results.  That is not an issue with an SSID or BSSID
		 *    filter applied to the scan results in the firmware.
		 */
		if (ssidlen || (memcmp(pscancfgout->specificBSSID,
				       &zeromac, sizeof(zeromac)) != 0)) {
			*pmaxchanperscan = MRVDRV_MAX_CHANNELS_PER_SCAN;
			*pfilteredscan = 1;
		}
	} else {
		pscancfgout->bsstype = adapter->scanmode;
		numprobes = adapter->scanprobes;
	}

	/* If the input config or adapter has the number of Probes set, add tlv */
	if (numprobes) {
		pnumprobestlv = (struct mrvlietypes_numprobes *) ptlvpos;
		pnumprobestlv->header.type =
		    cpu_to_le16(TLV_TYPE_NUMPROBES);
		pnumprobestlv->header.len = sizeof(pnumprobestlv->numprobes);
		pnumprobestlv->numprobes = cpu_to_le16(numprobes);

		ptlvpos +=
		    sizeof(pnumprobestlv->header) + pnumprobestlv->header.len;

		pnumprobestlv->header.len =
		    cpu_to_le16(pnumprobestlv->header.len);
	}

	/*
	 * Set the output for the channel TLV to the address in the tlv buffer
	 *   past any TLVs that were added in this fuction (SSID, numprobes).
	 *   channel TLVs will be added past this for each scan command, preserving
	 *   the TLVs that were previously added.
	 */
	*ppchantlvout = (struct mrvlietypes_chanlistparamset *) ptlvpos;

	if (puserscanin && puserscanin->chanlist[0].channumber) {

		lbs_pr_debug(1, "Scan: Using supplied channel list\n");

		for (chanidx = 0;
		     chanidx < WLAN_IOCTL_USER_SCAN_CHAN_MAX
		     && puserscanin->chanlist[chanidx].channumber; chanidx++) {

			channel = puserscanin->chanlist[chanidx].channumber;
			(pscanchanlist + chanidx)->channumber = channel;

			radiotype = puserscanin->chanlist[chanidx].radiotype;
			(pscanchanlist + chanidx)->radiotype = radiotype;

			scantype = puserscanin->chanlist[chanidx].scantype;

			if (scantype == cmd_scan_type_passive) {
				(pscanchanlist +
				 chanidx)->chanscanmode.passivescan = 1;
			} else {
				(pscanchanlist +
				 chanidx)->chanscanmode.passivescan = 0;
			}

			if (puserscanin->chanlist[chanidx].scantime) {
				scandur =
				    puserscanin->chanlist[chanidx].scantime;
			} else {
				if (scantype == cmd_scan_type_passive) {
					scandur = MRVDRV_PASSIVE_SCAN_CHAN_TIME;
				} else {
					scandur = MRVDRV_ACTIVE_SCAN_CHAN_TIME;
				}
			}

			(pscanchanlist + chanidx)->minscantime =
			    cpu_to_le16(scandur);
			(pscanchanlist + chanidx)->maxscantime =
			    cpu_to_le16(scandur);
		}

		/* Check if we are only scanning the current channel */
		if ((chanidx == 1) && (puserscanin->chanlist[0].channumber
				       ==
				       priv->adapter->curbssparams.channel)) {