Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * at76c503/at76c505 USB driver
0004  *
0005  * Copyright (c) 2002 - 2003 Oliver Kurth
0006  * Copyright (c) 2004 Joerg Albert <joerg.albert@gmx.de>
0007  * Copyright (c) 2004 Nick Jones
0008  * Copyright (c) 2004 Balint Seeber <n0_5p4m_p13453@hotmail.com>
0009  * Copyright (c) 2007 Guido Guenther <agx@sigxcpu.org>
0010  * Copyright (c) 2007 Kalle Valo <kalle.valo@iki.fi>
0011  * Copyright (c) 2010 Sebastian Smolorz <sesmo@gmx.net>
0012  *
0013  * This file is part of the Berlios driver for WLAN USB devices based on the
0014  * Atmel AT76C503A/505/505A.
0015  *
0016  * Some iw_handler code was taken from airo.c, (C) 1999 Benjamin Reed
0017  *
0018  * TODO list is at the wiki:
0019  *
0020  * https://wireless.wiki.kernel.org/en/users/Drivers/at76c50x-usb#TODO
0021  */
0022 
0023 #include <linux/init.h>
0024 #include <linux/kernel.h>
0025 #include <linux/sched.h>
0026 #include <linux/errno.h>
0027 #include <linux/slab.h>
0028 #include <linux/module.h>
0029 #include <linux/spinlock.h>
0030 #include <linux/list.h>
0031 #include <linux/usb.h>
0032 #include <linux/netdevice.h>
0033 #include <linux/if_arp.h>
0034 #include <linux/etherdevice.h>
0035 #include <linux/ethtool.h>
0036 #include <linux/wireless.h>
0037 #include <net/iw_handler.h>
0038 #include <net/ieee80211_radiotap.h>
0039 #include <linux/firmware.h>
0040 #include <linux/leds.h>
0041 #include <net/mac80211.h>
0042 
0043 #include "at76c50x-usb.h"
0044 
0045 /* Version information */
0046 #define DRIVER_NAME "at76c50x-usb"
0047 #define DRIVER_VERSION  "0.17"
0048 #define DRIVER_DESC "Atmel at76x USB Wireless LAN Driver"
0049 
0050 /* at76_debug bits */
0051 #define DBG_PROGRESS        0x00000001  /* authentication/accociation */
0052 #define DBG_BSS_TABLE       0x00000002  /* show BSS table after scans */
0053 #define DBG_IOCTL       0x00000004  /* ioctl calls / settings */
0054 #define DBG_MAC_STATE       0x00000008  /* MAC state transitions */
0055 #define DBG_TX_DATA     0x00000010  /* tx header */
0056 #define DBG_TX_DATA_CONTENT 0x00000020  /* tx content */
0057 #define DBG_TX_MGMT     0x00000040  /* tx management */
0058 #define DBG_RX_DATA     0x00000080  /* rx data header */
0059 #define DBG_RX_DATA_CONTENT 0x00000100  /* rx data content */
0060 #define DBG_RX_MGMT     0x00000200  /* rx mgmt frame headers */
0061 #define DBG_RX_BEACON       0x00000400  /* rx beacon */
0062 #define DBG_RX_CTRL     0x00000800  /* rx control */
0063 #define DBG_RX_MGMT_CONTENT 0x00001000  /* rx mgmt content */
0064 #define DBG_RX_FRAGS        0x00002000  /* rx data fragment handling */
0065 #define DBG_DEVSTART        0x00004000  /* fw download, device start */
0066 #define DBG_URB         0x00008000  /* rx urb status, ... */
0067 #define DBG_RX_ATMEL_HDR    0x00010000  /* Atmel-specific Rx headers */
0068 #define DBG_PROC_ENTRY      0x00020000  /* procedure entries/exits */
0069 #define DBG_PM          0x00040000  /* power management settings */
0070 #define DBG_BSS_MATCH       0x00080000  /* BSS match failures */
0071 #define DBG_PARAMS      0x00100000  /* show configured parameters */
0072 #define DBG_WAIT_COMPLETE   0x00200000  /* command completion */
0073 #define DBG_RX_FRAGS_SKB    0x00400000  /* skb header of Rx fragments */
0074 #define DBG_BSS_TABLE_RM    0x00800000  /* purging bss table entries */
0075 #define DBG_MONITOR_MODE    0x01000000  /* monitor mode */
0076 #define DBG_MIB         0x02000000  /* dump all MIBs on startup */
0077 #define DBG_MGMT_TIMER      0x04000000  /* dump mgmt_timer ops */
0078 #define DBG_WE_EVENTS       0x08000000  /* dump wireless events */
0079 #define DBG_FW          0x10000000  /* firmware download */
0080 #define DBG_DFU         0x20000000  /* device firmware upgrade */
0081 #define DBG_CMD         0x40000000
0082 #define DBG_MAC80211        0x80000000
0083 
0084 #define DBG_DEFAULTS        0
0085 
0086 /* Use our own dbg macro */
0087 #define at76_dbg(bits, format, arg...)                  \
0088 do {                                    \
0089     if (at76_debug & (bits))                    \
0090         printk(KERN_DEBUG DRIVER_NAME ": " format "\n", ##arg); \
0091 } while (0)
0092 
0093 #define at76_dbg_dump(bits, buf, len, format, arg...)           \
0094 do {                                    \
0095     if (at76_debug & (bits)) {                  \
0096         printk(KERN_DEBUG DRIVER_NAME ": " format "\n", ##arg); \
0097         print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len); \
0098     }                               \
0099 } while (0)
0100 
0101 static uint at76_debug = DBG_DEFAULTS;
0102 
0103 /* Protect against concurrent firmware loading and parsing */
0104 static DEFINE_MUTEX(fw_mutex);
0105 
0106 static struct fwentry firmwares[] = {
0107     [0] = { "" },
0108     [BOARD_503_ISL3861] = { "atmel_at76c503-i3861.bin" },
0109     [BOARD_503_ISL3863] = { "atmel_at76c503-i3863.bin" },
0110     [BOARD_503] = { "atmel_at76c503-rfmd.bin" },
0111     [BOARD_503_ACC] = { "atmel_at76c503-rfmd-acc.bin" },
0112     [BOARD_505] = { "atmel_at76c505-rfmd.bin" },
0113     [BOARD_505_2958] = { "atmel_at76c505-rfmd2958.bin" },
0114     [BOARD_505A] = { "atmel_at76c505a-rfmd2958.bin" },
0115     [BOARD_505AMX] = { "atmel_at76c505amx-rfmd.bin" },
0116 };
0117 MODULE_FIRMWARE("atmel_at76c503-i3861.bin");
0118 MODULE_FIRMWARE("atmel_at76c503-i3863.bin");
0119 MODULE_FIRMWARE("atmel_at76c503-rfmd.bin");
0120 MODULE_FIRMWARE("atmel_at76c503-rfmd-acc.bin");
0121 MODULE_FIRMWARE("atmel_at76c505-rfmd.bin");
0122 MODULE_FIRMWARE("atmel_at76c505-rfmd2958.bin");
0123 MODULE_FIRMWARE("atmel_at76c505a-rfmd2958.bin");
0124 MODULE_FIRMWARE("atmel_at76c505amx-rfmd.bin");
0125 
0126 #define USB_DEVICE_DATA(__ops)  .driver_info = (kernel_ulong_t)(__ops)
0127 
0128 static const struct usb_device_id dev_table[] = {
0129     /*
0130      * at76c503-i3861
0131      */
0132     /* Generic AT76C503/3861 device */
0133     { USB_DEVICE(0x03eb, 0x7603), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0134     /* Linksys WUSB11 v2.1/v2.6 */
0135     { USB_DEVICE(0x066b, 0x2211), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0136     /* Netgear MA101 rev. A */
0137     { USB_DEVICE(0x0864, 0x4100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0138     /* Tekram U300C / Allnet ALL0193 */
0139     { USB_DEVICE(0x0b3b, 0x1612), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0140     /* HP HN210W J7801A */
0141     { USB_DEVICE(0x03f0, 0x011c), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0142     /* Sitecom/Z-Com/Zyxel M4Y-750 */
0143     { USB_DEVICE(0x0cde, 0x0001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0144     /* Dynalink/Askey WLL013 (intersil) */
0145     { USB_DEVICE(0x069a, 0x0320), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0146     /* EZ connect 11Mpbs Wireless USB Adapter SMC2662W v1 */
0147     { USB_DEVICE(0x0d5c, 0xa001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0148     /* BenQ AWL300 */
0149     { USB_DEVICE(0x04a5, 0x9000), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0150     /* Addtron AWU-120, Compex WLU11 */
0151     { USB_DEVICE(0x05dd, 0xff31), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0152     /* Intel AP310 AnyPoint II USB */
0153     { USB_DEVICE(0x8086, 0x0200), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0154     /* Dynalink L11U */
0155     { USB_DEVICE(0x0d8e, 0x7100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0156     /* Arescom WL-210, FCC id 07J-GL2411USB */
0157     { USB_DEVICE(0x0d8e, 0x7110), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0158     /* I-O DATA WN-B11/USB */
0159     { USB_DEVICE(0x04bb, 0x0919), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0160     /* BT Voyager 1010 */
0161     { USB_DEVICE(0x069a, 0x0821), USB_DEVICE_DATA(BOARD_503_ISL3861) },
0162     /*
0163      * at76c503-i3863
0164      */
0165     /* Generic AT76C503/3863 device */
0166     { USB_DEVICE(0x03eb, 0x7604), USB_DEVICE_DATA(BOARD_503_ISL3863) },
0167     /* Samsung SWL-2100U */
0168     { USB_DEVICE(0x055d, 0xa000), USB_DEVICE_DATA(BOARD_503_ISL3863) },
0169     /*
0170      * at76c503-rfmd
0171      */
0172     /* Generic AT76C503/RFMD device */
0173     { USB_DEVICE(0x03eb, 0x7605), USB_DEVICE_DATA(BOARD_503) },
0174     /* Dynalink/Askey WLL013 (rfmd) */
0175     { USB_DEVICE(0x069a, 0x0321), USB_DEVICE_DATA(BOARD_503) },
0176     /* Linksys WUSB11 v2.6 */
0177     { USB_DEVICE(0x077b, 0x2219), USB_DEVICE_DATA(BOARD_503) },
0178     /* Network Everywhere NWU11B */
0179     { USB_DEVICE(0x077b, 0x2227), USB_DEVICE_DATA(BOARD_503) },
0180     /* Netgear MA101 rev. B */
0181     { USB_DEVICE(0x0864, 0x4102), USB_DEVICE_DATA(BOARD_503) },
0182     /* D-Link DWL-120 rev. E */
0183     { USB_DEVICE(0x2001, 0x3200), USB_DEVICE_DATA(BOARD_503) },
0184     /* Actiontec 802UAT1, HWU01150-01UK */
0185     { USB_DEVICE(0x1668, 0x7605), USB_DEVICE_DATA(BOARD_503) },
0186     /* AirVast W-Buddie WN210 */
0187     { USB_DEVICE(0x03eb, 0x4102), USB_DEVICE_DATA(BOARD_503) },
0188     /* Dick Smith Electronics XH1153 802.11b USB adapter */
0189     { USB_DEVICE(0x1371, 0x5743), USB_DEVICE_DATA(BOARD_503) },
0190     /* CNet CNUSB611 */
0191     { USB_DEVICE(0x1371, 0x0001), USB_DEVICE_DATA(BOARD_503) },
0192     /* FiberLine FL-WL200U */
0193     { USB_DEVICE(0x1371, 0x0002), USB_DEVICE_DATA(BOARD_503) },
0194     /* BenQ AWL400 USB stick */
0195     { USB_DEVICE(0x04a5, 0x9001), USB_DEVICE_DATA(BOARD_503) },
0196     /* 3Com 3CRSHEW696 */
0197     { USB_DEVICE(0x0506, 0x0a01), USB_DEVICE_DATA(BOARD_503) },
0198     /* Siemens Santis ADSL WLAN USB adapter WLL 013 */
0199     { USB_DEVICE(0x0681, 0x001b), USB_DEVICE_DATA(BOARD_503) },
0200     /* Belkin F5D6050, version 2 */
0201     { USB_DEVICE(0x050d, 0x0050), USB_DEVICE_DATA(BOARD_503) },
0202     /* iBlitzz, BWU613 (not *B or *SB) */
0203     { USB_DEVICE(0x07b8, 0xb000), USB_DEVICE_DATA(BOARD_503) },
0204     /* Gigabyte GN-WLBM101 */
0205     { USB_DEVICE(0x1044, 0x8003), USB_DEVICE_DATA(BOARD_503) },
0206     /* Planex GW-US11S */
0207     { USB_DEVICE(0x2019, 0x3220), USB_DEVICE_DATA(BOARD_503) },
0208     /* Internal WLAN adapter in h5[4,5]xx series iPAQs */
0209     { USB_DEVICE(0x049f, 0x0032), USB_DEVICE_DATA(BOARD_503) },
0210     /* Corega Wireless LAN USB-11 mini */
0211     { USB_DEVICE(0x07aa, 0x0011), USB_DEVICE_DATA(BOARD_503) },
0212     /* Corega Wireless LAN USB-11 mini2 */
0213     { USB_DEVICE(0x07aa, 0x0018), USB_DEVICE_DATA(BOARD_503) },
0214     /* Uniden PCW100 */
0215     { USB_DEVICE(0x05dd, 0xff35), USB_DEVICE_DATA(BOARD_503) },
0216     /*
0217      * at76c503-rfmd-acc
0218      */
0219     /* SMC2664W */
0220     { USB_DEVICE(0x083a, 0x3501), USB_DEVICE_DATA(BOARD_503_ACC) },
0221     /* Belkin F5D6050, SMC2662W v2, SMC2662W-AR */
0222     { USB_DEVICE(0x0d5c, 0xa002), USB_DEVICE_DATA(BOARD_503_ACC) },
0223     /*
0224      * at76c505-rfmd
0225      */
0226     /* Generic AT76C505/RFMD */
0227     { USB_DEVICE(0x03eb, 0x7606), USB_DEVICE_DATA(BOARD_505) },
0228     /*
0229      * at76c505-rfmd2958
0230      */
0231     /* Generic AT76C505/RFMD, OvisLink WL-1130USB */
0232     { USB_DEVICE(0x03eb, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
0233     /* Fiberline FL-WL240U */
0234     { USB_DEVICE(0x1371, 0x0014), USB_DEVICE_DATA(BOARD_505_2958) },
0235     /* CNet CNUSB-611G */
0236     { USB_DEVICE(0x1371, 0x0013), USB_DEVICE_DATA(BOARD_505_2958) },
0237     /* Linksys WUSB11 v2.8 */
0238     { USB_DEVICE(0x1915, 0x2233), USB_DEVICE_DATA(BOARD_505_2958) },
0239     /* Xterasys XN-2122B, IBlitzz BWU613B/BWU613SB */
0240     { USB_DEVICE(0x12fd, 0x1001), USB_DEVICE_DATA(BOARD_505_2958) },
0241     /* Corega WLAN USB Stick 11 */
0242     { USB_DEVICE(0x07aa, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
0243     /* Microstar MSI Box MS6978 */
0244     { USB_DEVICE(0x0db0, 0x1020), USB_DEVICE_DATA(BOARD_505_2958) },
0245     /*
0246      * at76c505a-rfmd2958
0247      */
0248     /* Generic AT76C505A device */
0249     { USB_DEVICE(0x03eb, 0x7614), USB_DEVICE_DATA(BOARD_505A) },
0250     /* Generic AT76C505AS device */
0251     { USB_DEVICE(0x03eb, 0x7617), USB_DEVICE_DATA(BOARD_505A) },
0252     /* Siemens Gigaset USB WLAN Adapter 11 */
0253     { USB_DEVICE(0x1690, 0x0701), USB_DEVICE_DATA(BOARD_505A) },
0254     /* OQO Model 01+ Internal Wi-Fi */
0255     { USB_DEVICE(0x1557, 0x0002), USB_DEVICE_DATA(BOARD_505A) },
0256     /*
0257      * at76c505amx-rfmd
0258      */
0259     /* Generic AT76C505AMX device */
0260     { USB_DEVICE(0x03eb, 0x7615), USB_DEVICE_DATA(BOARD_505AMX) },
0261     { }
0262 };
0263 
0264 MODULE_DEVICE_TABLE(usb, dev_table);
0265 
0266 /* Supported rates of this hardware, bit 7 marks basic rates */
0267 static const u8 hw_rates[] = { 0x82, 0x84, 0x0b, 0x16 };
0268 
0269 static const char *const preambles[] = { "long", "short", "auto" };
0270 
0271 /* Firmware download */
0272 /* DFU states */
0273 #define STATE_IDLE          0x00
0274 #define STATE_DETACH            0x01
0275 #define STATE_DFU_IDLE          0x02
0276 #define STATE_DFU_DOWNLOAD_SYNC     0x03
0277 #define STATE_DFU_DOWNLOAD_BUSY     0x04
0278 #define STATE_DFU_DOWNLOAD_IDLE     0x05
0279 #define STATE_DFU_MANIFEST_SYNC     0x06
0280 #define STATE_DFU_MANIFEST      0x07
0281 #define STATE_DFU_MANIFEST_WAIT_RESET   0x08
0282 #define STATE_DFU_UPLOAD_IDLE       0x09
0283 #define STATE_DFU_ERROR         0x0a
0284 
0285 /* DFU commands */
0286 #define DFU_DETACH          0
0287 #define DFU_DNLOAD          1
0288 #define DFU_UPLOAD          2
0289 #define DFU_GETSTATUS           3
0290 #define DFU_CLRSTATUS           4
0291 #define DFU_GETSTATE            5
0292 #define DFU_ABORT           6
0293 
0294 #define FW_BLOCK_SIZE 1024
0295 
0296 struct dfu_status {
0297     unsigned char status;
0298     unsigned char poll_timeout[3];
0299     unsigned char state;
0300     unsigned char string;
0301 } __packed;
0302 
0303 static inline int at76_is_intersil(enum board_type board)
0304 {
0305     return (board == BOARD_503_ISL3861 || board == BOARD_503_ISL3863);
0306 }
0307 
0308 static inline int at76_is_503rfmd(enum board_type board)
0309 {
0310     return (board == BOARD_503 || board == BOARD_503_ACC);
0311 }
0312 
0313 static inline int at76_is_505a(enum board_type board)
0314 {
0315     return (board == BOARD_505A || board == BOARD_505AMX);
0316 }
0317 
0318 /* Load a block of the first (internal) part of the firmware */
0319 static int at76_load_int_fw_block(struct usb_device *udev, int blockno,
0320                   void *block, int size)
0321 {
0322     return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), DFU_DNLOAD,
0323                    USB_TYPE_CLASS | USB_DIR_OUT |
0324                    USB_RECIP_INTERFACE, blockno, 0, block, size,
0325                    USB_CTRL_GET_TIMEOUT);
0326 }
0327 
0328 static int at76_dfu_get_status(struct usb_device *udev,
0329                    struct dfu_status *status)
0330 {
0331     int ret;
0332 
0333     ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATUS,
0334                   USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
0335                   0, 0, status, sizeof(struct dfu_status),
0336                   USB_CTRL_GET_TIMEOUT);
0337     return ret;
0338 }
0339 
0340 static int at76_dfu_get_state(struct usb_device *udev, u8 *state)
0341 {
0342     int ret;
0343 
0344     ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATE,
0345                   USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
0346                   0, 0, state, 1, USB_CTRL_GET_TIMEOUT);
0347     return ret;
0348 }
0349 
0350 /* Convert timeout from the DFU status to jiffies */
0351 static inline unsigned long at76_get_timeout(struct dfu_status *s)
0352 {
0353     return msecs_to_jiffies((s->poll_timeout[2] << 16)
0354                 | (s->poll_timeout[1] << 8)
0355                 | (s->poll_timeout[0]));
0356 }
0357 
0358 /* Load internal firmware from the buffer.  If manifest_sync_timeout > 0, use
0359  * its value in jiffies in the MANIFEST_SYNC state.  */
0360 static int at76_usbdfu_download(struct usb_device *udev, u8 *buf, u32 size,
0361                 int manifest_sync_timeout)
0362 {
0363     int ret = 0;
0364     int need_dfu_state = 1;
0365     int is_done = 0;
0366     u32 dfu_timeout = 0;
0367     int bsize = 0;
0368     int blockno = 0;
0369     struct dfu_status *dfu_stat_buf = NULL;
0370     u8 *dfu_state = NULL;
0371     u8 *block = NULL;
0372 
0373     at76_dbg(DBG_DFU, "%s( %p, %u, %d)", __func__, buf, size,
0374          manifest_sync_timeout);
0375 
0376     if (!size) {
0377         dev_err(&udev->dev, "FW buffer length invalid!\n");
0378         return -EINVAL;
0379     }
0380 
0381     dfu_stat_buf = kmalloc(sizeof(struct dfu_status), GFP_KERNEL);
0382     if (!dfu_stat_buf) {
0383         ret = -ENOMEM;
0384         goto exit;
0385     }
0386 
0387     block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
0388     if (!block) {
0389         ret = -ENOMEM;
0390         goto exit;
0391     }
0392 
0393     dfu_state = kmalloc(sizeof(u8), GFP_KERNEL);
0394     if (!dfu_state) {
0395         ret = -ENOMEM;
0396         goto exit;
0397     }
0398     *dfu_state = 0;
0399 
0400     do {
0401         if (need_dfu_state) {
0402             ret = at76_dfu_get_state(udev, dfu_state);
0403             if (ret < 0) {
0404                 dev_err(&udev->dev,
0405                     "cannot get DFU state: %d\n", ret);
0406                 goto exit;
0407             }
0408             need_dfu_state = 0;
0409         }
0410 
0411         switch (*dfu_state) {
0412         case STATE_DFU_DOWNLOAD_SYNC:
0413             at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_SYNC");
0414             ret = at76_dfu_get_status(udev, dfu_stat_buf);
0415             if (ret >= 0) {
0416                 *dfu_state = dfu_stat_buf->state;
0417                 dfu_timeout = at76_get_timeout(dfu_stat_buf);
0418                 need_dfu_state = 0;
0419             } else
0420                 dev_err(&udev->dev,
0421                     "at76_dfu_get_status returned %d\n",
0422                     ret);
0423             break;
0424 
0425         case STATE_DFU_DOWNLOAD_BUSY:
0426             at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_BUSY");
0427             need_dfu_state = 1;
0428 
0429             at76_dbg(DBG_DFU, "DFU: Resetting device");
0430             schedule_timeout_interruptible(dfu_timeout);
0431             break;
0432 
0433         case STATE_DFU_DOWNLOAD_IDLE:
0434             at76_dbg(DBG_DFU, "DOWNLOAD...");
0435             fallthrough;
0436         case STATE_DFU_IDLE:
0437             at76_dbg(DBG_DFU, "DFU IDLE");
0438 
0439             bsize = min_t(int, size, FW_BLOCK_SIZE);
0440             memcpy(block, buf, bsize);
0441             at76_dbg(DBG_DFU, "int fw, size left = %5d, "
0442                  "bsize = %4d, blockno = %2d", size, bsize,
0443                  blockno);
0444             ret =
0445                 at76_load_int_fw_block(udev, blockno, block, bsize);
0446             buf += bsize;
0447             size -= bsize;
0448             blockno++;
0449 
0450             if (ret != bsize)
0451                 dev_err(&udev->dev,
0452                     "at76_load_int_fw_block returned %d\n",
0453                     ret);
0454             need_dfu_state = 1;
0455             break;
0456 
0457         case STATE_DFU_MANIFEST_SYNC:
0458             at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_SYNC");
0459 
0460             ret = at76_dfu_get_status(udev, dfu_stat_buf);
0461             if (ret < 0)
0462                 break;
0463 
0464             *dfu_state = dfu_stat_buf->state;
0465             dfu_timeout = at76_get_timeout(dfu_stat_buf);
0466             need_dfu_state = 0;
0467 
0468             /* override the timeout from the status response,
0469                needed for AT76C505A */
0470             if (manifest_sync_timeout > 0)
0471                 dfu_timeout = manifest_sync_timeout;
0472 
0473             at76_dbg(DBG_DFU, "DFU: Waiting for manifest phase");
0474             schedule_timeout_interruptible(dfu_timeout);
0475             break;
0476 
0477         case STATE_DFU_MANIFEST:
0478             at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST");
0479             is_done = 1;
0480             break;
0481 
0482         case STATE_DFU_MANIFEST_WAIT_RESET:
0483             at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_WAIT_RESET");
0484             is_done = 1;
0485             break;
0486 
0487         case STATE_DFU_UPLOAD_IDLE:
0488             at76_dbg(DBG_DFU, "STATE_DFU_UPLOAD_IDLE");
0489             break;
0490 
0491         case STATE_DFU_ERROR:
0492             at76_dbg(DBG_DFU, "STATE_DFU_ERROR");
0493             ret = -EPIPE;
0494             break;
0495 
0496         default:
0497             at76_dbg(DBG_DFU, "DFU UNKNOWN STATE (%d)", *dfu_state);
0498             ret = -EINVAL;
0499             break;
0500         }
0501     } while (!is_done && (ret >= 0));
0502 
0503 exit:
0504     kfree(dfu_state);
0505     kfree(block);
0506     kfree(dfu_stat_buf);
0507 
0508     if (ret >= 0)
0509         ret = 0;
0510 
0511     return ret;
0512 }
0513 
0514 /* LED trigger */
0515 static int tx_activity;
0516 static void at76_ledtrig_tx_timerfunc(struct timer_list *unused);
0517 static DEFINE_TIMER(ledtrig_tx_timer, at76_ledtrig_tx_timerfunc);
0518 DEFINE_LED_TRIGGER(ledtrig_tx);
0519 
0520 static void at76_ledtrig_tx_timerfunc(struct timer_list *unused)
0521 {
0522     static int tx_lastactivity;
0523 
0524     if (tx_lastactivity != tx_activity) {
0525         tx_lastactivity = tx_activity;
0526         led_trigger_event(ledtrig_tx, LED_FULL);
0527         mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
0528     } else
0529         led_trigger_event(ledtrig_tx, LED_OFF);
0530 }
0531 
0532 static void at76_ledtrig_tx_activity(void)
0533 {
0534     tx_activity++;
0535     if (!timer_pending(&ledtrig_tx_timer))
0536         mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
0537 }
0538 
0539 static int at76_remap(struct usb_device *udev)
0540 {
0541     int ret;
0542     ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0a,
0543                   USB_TYPE_VENDOR | USB_DIR_OUT |
0544                   USB_RECIP_INTERFACE, 0, 0, NULL, 0,
0545                   USB_CTRL_GET_TIMEOUT);
0546     if (ret < 0)
0547         return ret;
0548     return 0;
0549 }
0550 
0551 static int at76_get_op_mode(struct usb_device *udev)
0552 {
0553     int ret;
0554     u8 saved;
0555     u8 *op_mode;
0556 
0557     op_mode = kmalloc(1, GFP_NOIO);
0558     if (!op_mode)
0559         return -ENOMEM;
0560     ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
0561                   USB_TYPE_VENDOR | USB_DIR_IN |
0562                   USB_RECIP_INTERFACE, 0x01, 0, op_mode, 1,
0563                   USB_CTRL_GET_TIMEOUT);
0564     saved = *op_mode;
0565     kfree(op_mode);
0566 
0567     if (ret < 0)
0568         return ret;
0569     else if (ret < 1)
0570         return -EIO;
0571     else
0572         return saved;
0573 }
0574 
0575 /* Load a block of the second ("external") part of the firmware */
0576 static inline int at76_load_ext_fw_block(struct usb_device *udev, int blockno,
0577                      void *block, int size)
0578 {
0579     return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
0580                    USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
0581                    0x0802, blockno, block, size,
0582                    USB_CTRL_GET_TIMEOUT);
0583 }
0584 
0585 static inline int at76_get_hw_cfg(struct usb_device *udev,
0586                   union at76_hwcfg *buf, int buf_size)
0587 {
0588     return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
0589                    USB_TYPE_VENDOR | USB_DIR_IN |
0590                    USB_RECIP_INTERFACE, 0x0a02, 0,
0591                    buf, buf_size, USB_CTRL_GET_TIMEOUT);
0592 }
0593 
0594 /* Intersil boards use a different "value" for GetHWConfig requests */
0595 static inline int at76_get_hw_cfg_intersil(struct usb_device *udev,
0596                        union at76_hwcfg *buf, int buf_size)
0597 {
0598     return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
0599                    USB_TYPE_VENDOR | USB_DIR_IN |
0600                    USB_RECIP_INTERFACE, 0x0902, 0,
0601                    buf, buf_size, USB_CTRL_GET_TIMEOUT);
0602 }
0603 
0604 /* Get the hardware configuration for the adapter and put it to the appropriate
0605  * fields of 'priv' (the GetHWConfig request and interpretation of the result
0606  * depends on the board type) */
0607 static int at76_get_hw_config(struct at76_priv *priv)
0608 {
0609     int ret;
0610     union at76_hwcfg *hwcfg = kmalloc(sizeof(*hwcfg), GFP_KERNEL);
0611 
0612     if (!hwcfg)
0613         return -ENOMEM;
0614 
0615     if (at76_is_intersil(priv->board_type)) {
0616         ret = at76_get_hw_cfg_intersil(priv->udev, hwcfg,
0617                            sizeof(hwcfg->i));
0618         if (ret < 0)
0619             goto exit;
0620         memcpy(priv->mac_addr, hwcfg->i.mac_addr, ETH_ALEN);
0621         priv->regulatory_domain = hwcfg->i.regulatory_domain;
0622     } else if (at76_is_503rfmd(priv->board_type)) {
0623         ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r3));
0624         if (ret < 0)
0625             goto exit;
0626         memcpy(priv->mac_addr, hwcfg->r3.mac_addr, ETH_ALEN);
0627         priv->regulatory_domain = hwcfg->r3.regulatory_domain;
0628     } else {
0629         ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r5));
0630         if (ret < 0)
0631             goto exit;
0632         memcpy(priv->mac_addr, hwcfg->r5.mac_addr, ETH_ALEN);
0633         priv->regulatory_domain = hwcfg->r5.regulatory_domain;
0634     }
0635 
0636 exit:
0637     kfree(hwcfg);
0638     if (ret < 0)
0639         wiphy_err(priv->hw->wiphy, "cannot get HW Config (error %d)\n",
0640               ret);
0641 
0642     return ret;
0643 }
0644 
0645 static struct reg_domain const *at76_get_reg_domain(u16 code)
0646 {
0647     int i;
0648     static struct reg_domain const fd_tab[] = {
0649         { 0x10, "FCC (USA)", 0x7ff },   /* ch 1-11 */
0650         { 0x20, "IC (Canada)", 0x7ff }, /* ch 1-11 */
0651         { 0x30, "ETSI (most of Europe)", 0x1fff },  /* ch 1-13 */
0652         { 0x31, "Spain", 0x600 },   /* ch 10-11 */
0653         { 0x32, "France", 0x1e00 }, /* ch 10-13 */
0654         { 0x40, "MKK (Japan)", 0x2000 },    /* ch 14 */
0655         { 0x41, "MKK1 (Japan)", 0x3fff },   /* ch 1-14 */
0656         { 0x50, "Israel", 0x3fc },  /* ch 3-9 */
0657         { 0x00, "<unknown>", 0xffffffff }   /* ch 1-32 */
0658     };
0659 
0660     /* Last entry is fallback for unknown domain code */
0661     for (i = 0; i < ARRAY_SIZE(fd_tab) - 1; i++)
0662         if (code == fd_tab[i].code)
0663             break;
0664 
0665     return &fd_tab[i];
0666 }
0667 
0668 static inline int at76_get_mib(struct usb_device *udev, u16 mib, void *buf,
0669                    int buf_size)
0670 {
0671     int ret;
0672 
0673     ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
0674                   USB_TYPE_VENDOR | USB_DIR_IN |
0675                   USB_RECIP_INTERFACE, mib << 8, 0, buf, buf_size,
0676                   USB_CTRL_GET_TIMEOUT);
0677     if (ret >= 0 && ret != buf_size)
0678         return -EIO;
0679     return ret;
0680 }
0681 
0682 /* Return positive number for status, negative for an error */
0683 static inline int at76_get_cmd_status(struct usb_device *udev, u8 cmd)
0684 {
0685     u8 *stat_buf;
0686     int ret;
0687 
0688     stat_buf = kmalloc(40, GFP_NOIO);
0689     if (!stat_buf)
0690         return -ENOMEM;
0691 
0692     ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x22,
0693             USB_TYPE_VENDOR | USB_DIR_IN |
0694             USB_RECIP_INTERFACE, cmd, 0, stat_buf,
0695             40, USB_CTRL_GET_TIMEOUT);
0696     if (ret >= 0)
0697         ret = stat_buf[5];
0698     kfree(stat_buf);
0699 
0700     return ret;
0701 }
0702 
0703 #define MAKE_CMD_CASE(c) case (c): return #c
0704 static const char *at76_get_cmd_string(u8 cmd_status)
0705 {
0706     switch (cmd_status) {
0707         MAKE_CMD_CASE(CMD_SET_MIB);
0708         MAKE_CMD_CASE(CMD_GET_MIB);
0709         MAKE_CMD_CASE(CMD_SCAN);
0710         MAKE_CMD_CASE(CMD_JOIN);
0711         MAKE_CMD_CASE(CMD_START_IBSS);
0712         MAKE_CMD_CASE(CMD_RADIO_ON);
0713         MAKE_CMD_CASE(CMD_RADIO_OFF);
0714         MAKE_CMD_CASE(CMD_STARTUP);
0715     }
0716 
0717     return "UNKNOWN";
0718 }
0719 
0720 static int at76_set_card_command(struct usb_device *udev, u8 cmd, void *buf,
0721                  int buf_size)
0722 {
0723     int ret;
0724     struct at76_command *cmd_buf = kmalloc(sizeof(struct at76_command) +
0725                            buf_size, GFP_KERNEL);
0726 
0727     if (!cmd_buf)
0728         return -ENOMEM;
0729 
0730     cmd_buf->cmd = cmd;
0731     cmd_buf->reserved = 0;
0732     cmd_buf->size = cpu_to_le16(buf_size);
0733     memcpy(cmd_buf->data, buf, buf_size);
0734 
0735     at76_dbg_dump(DBG_CMD, cmd_buf, sizeof(struct at76_command) + buf_size,
0736               "issuing command %s (0x%02x)",
0737               at76_get_cmd_string(cmd), cmd);
0738 
0739     ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
0740                   USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
0741                   0, 0, cmd_buf,
0742                   sizeof(struct at76_command) + buf_size,
0743                   USB_CTRL_GET_TIMEOUT);
0744     kfree(cmd_buf);
0745     return ret;
0746 }
0747 
0748 #define MAKE_CMD_STATUS_CASE(c) case (c): return #c
0749 static const char *at76_get_cmd_status_string(u8 cmd_status)
0750 {
0751     switch (cmd_status) {
0752         MAKE_CMD_STATUS_CASE(CMD_STATUS_IDLE);
0753         MAKE_CMD_STATUS_CASE(CMD_STATUS_COMPLETE);
0754         MAKE_CMD_STATUS_CASE(CMD_STATUS_UNKNOWN);
0755         MAKE_CMD_STATUS_CASE(CMD_STATUS_INVALID_PARAMETER);
0756         MAKE_CMD_STATUS_CASE(CMD_STATUS_FUNCTION_NOT_SUPPORTED);
0757         MAKE_CMD_STATUS_CASE(CMD_STATUS_TIME_OUT);
0758         MAKE_CMD_STATUS_CASE(CMD_STATUS_IN_PROGRESS);
0759         MAKE_CMD_STATUS_CASE(CMD_STATUS_HOST_FAILURE);
0760         MAKE_CMD_STATUS_CASE(CMD_STATUS_SCAN_FAILED);
0761     }
0762 
0763     return "UNKNOWN";
0764 }
0765 
0766 /* Wait until the command is completed */
0767 static int at76_wait_completion(struct at76_priv *priv, int cmd)
0768 {
0769     int status = 0;
0770     unsigned long timeout = jiffies + CMD_COMPLETION_TIMEOUT;
0771 
0772     do {
0773         status = at76_get_cmd_status(priv->udev, cmd);
0774         if (status < 0) {
0775             wiphy_err(priv->hw->wiphy,
0776                   "at76_get_cmd_status failed: %d\n",
0777                   status);
0778             break;
0779         }
0780 
0781         at76_dbg(DBG_WAIT_COMPLETE,
0782              "%s: Waiting on cmd %d, status = %d (%s)",
0783              wiphy_name(priv->hw->wiphy), cmd, status,
0784              at76_get_cmd_status_string(status));
0785 
0786         if (status != CMD_STATUS_IN_PROGRESS
0787             && status != CMD_STATUS_IDLE)
0788             break;
0789 
0790         schedule_timeout_interruptible(HZ / 10);    /* 100 ms */
0791         if (time_after(jiffies, timeout)) {
0792             wiphy_err(priv->hw->wiphy,
0793                   "completion timeout for command %d\n", cmd);
0794             status = -ETIMEDOUT;
0795             break;
0796         }
0797     } while (1);
0798 
0799     return status;
0800 }
0801 
0802 static int at76_set_mib(struct at76_priv *priv, struct set_mib_buffer *buf)
0803 {
0804     int ret;
0805 
0806     ret = at76_set_card_command(priv->udev, CMD_SET_MIB, buf,
0807                     offsetof(struct set_mib_buffer,
0808                          data) + buf->size);
0809     if (ret < 0)
0810         return ret;
0811 
0812     ret = at76_wait_completion(priv, CMD_SET_MIB);
0813     if (ret != CMD_STATUS_COMPLETE) {
0814         wiphy_info(priv->hw->wiphy,
0815                "set_mib: at76_wait_completion failed with %d\n",
0816                ret);
0817         ret = -EIO;
0818     }
0819 
0820     return ret;
0821 }
0822 
0823 /* Return < 0 on error, == 0 if no command sent, == 1 if cmd sent */
0824 static int at76_set_radio(struct at76_priv *priv, int enable)
0825 {
0826     int ret;
0827     int cmd;
0828 
0829     if (priv->radio_on == enable)
0830         return 0;
0831 
0832     cmd = enable ? CMD_RADIO_ON : CMD_RADIO_OFF;
0833 
0834     ret = at76_set_card_command(priv->udev, cmd, NULL, 0);
0835     if (ret < 0)
0836         wiphy_err(priv->hw->wiphy,
0837               "at76_set_card_command(%d) failed: %d\n", cmd, ret);
0838     else
0839         ret = 1;
0840 
0841     priv->radio_on = enable;
0842     return ret;
0843 }
0844 
0845 /* Set current power save mode (AT76_PM_OFF/AT76_PM_ON/AT76_PM_SMART) */
0846 static int at76_set_pm_mode(struct at76_priv *priv)
0847 {
0848     int ret = 0;
0849 
0850     priv->mib_buf.type = MIB_MAC_MGMT;
0851     priv->mib_buf.size = 1;
0852     priv->mib_buf.index = offsetof(struct mib_mac_mgmt, power_mgmt_mode);
0853     priv->mib_buf.data.byte = priv->pm_mode;
0854 
0855     ret = at76_set_mib(priv, &priv->mib_buf);
0856     if (ret < 0)
0857         wiphy_err(priv->hw->wiphy, "set_mib (pm_mode) failed: %d\n",
0858               ret);
0859 
0860     return ret;
0861 }
0862 
0863 static int at76_set_preamble(struct at76_priv *priv, u8 type)
0864 {
0865     int ret = 0;
0866 
0867     priv->mib_buf.type = MIB_LOCAL;
0868     priv->mib_buf.size = 1;
0869     priv->mib_buf.index = offsetof(struct mib_local, preamble_type);
0870     priv->mib_buf.data.byte = type;
0871 
0872     ret = at76_set_mib(priv, &priv->mib_buf);
0873     if (ret < 0)
0874         wiphy_err(priv->hw->wiphy, "set_mib (preamble) failed: %d\n",
0875               ret);
0876 
0877     return ret;
0878 }
0879 
0880 static int at76_set_frag(struct at76_priv *priv, u16 size)
0881 {
0882     int ret = 0;
0883 
0884     priv->mib_buf.type = MIB_MAC;
0885     priv->mib_buf.size = 2;
0886     priv->mib_buf.index = offsetof(struct mib_mac, frag_threshold);
0887     priv->mib_buf.data.word = cpu_to_le16(size);
0888 
0889     ret = at76_set_mib(priv, &priv->mib_buf);
0890     if (ret < 0)
0891         wiphy_err(priv->hw->wiphy,
0892               "set_mib (frag threshold) failed: %d\n", ret);
0893 
0894     return ret;
0895 }
0896 
0897 static int at76_set_rts(struct at76_priv *priv, u16 size)
0898 {
0899     int ret = 0;
0900 
0901     priv->mib_buf.type = MIB_MAC;
0902     priv->mib_buf.size = 2;
0903     priv->mib_buf.index = offsetof(struct mib_mac, rts_threshold);
0904     priv->mib_buf.data.word = cpu_to_le16(size);
0905 
0906     ret = at76_set_mib(priv, &priv->mib_buf);
0907     if (ret < 0)
0908         wiphy_err(priv->hw->wiphy, "set_mib (rts) failed: %d\n", ret);
0909 
0910     return ret;
0911 }
0912 
0913 static int at76_set_autorate_fallback(struct at76_priv *priv, int onoff)
0914 {
0915     int ret = 0;
0916 
0917     priv->mib_buf.type = MIB_LOCAL;
0918     priv->mib_buf.size = 1;
0919     priv->mib_buf.index = offsetof(struct mib_local, txautorate_fallback);
0920     priv->mib_buf.data.byte = onoff;
0921 
0922     ret = at76_set_mib(priv, &priv->mib_buf);
0923     if (ret < 0)
0924         wiphy_err(priv->hw->wiphy,
0925               "set_mib (autorate fallback) failed: %d\n", ret);
0926 
0927     return ret;
0928 }
0929 
0930 static void at76_dump_mib_mac_addr(struct at76_priv *priv)
0931 {
0932     int i;
0933     int ret;
0934     struct mib_mac_addr *m = kmalloc(sizeof(struct mib_mac_addr),
0935                      GFP_KERNEL);
0936 
0937     if (!m)
0938         return;
0939 
0940     ret = at76_get_mib(priv->udev, MIB_MAC_ADDR, m,
0941                sizeof(struct mib_mac_addr));
0942     if (ret < 0) {
0943         wiphy_err(priv->hw->wiphy,
0944               "at76_get_mib (MAC_ADDR) failed: %d\n", ret);
0945         goto exit;
0946     }
0947 
0948     at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: mac_addr %pM res 0x%x 0x%x",
0949          wiphy_name(priv->hw->wiphy),
0950          m->mac_addr, m->res[0], m->res[1]);
0951     for (i = 0; i < ARRAY_SIZE(m->group_addr); i++)
0952         at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: group addr %d: %pM, "
0953              "status %d", wiphy_name(priv->hw->wiphy), i,
0954              m->group_addr[i], m->group_addr_status[i]);
0955 exit:
0956     kfree(m);
0957 }
0958 
0959 static void at76_dump_mib_mac_wep(struct at76_priv *priv)
0960 {
0961     int i;
0962     int ret;
0963     int key_len;
0964     struct mib_mac_wep *m = kmalloc(sizeof(struct mib_mac_wep), GFP_KERNEL);
0965 
0966     if (!m)
0967         return;
0968 
0969     ret = at76_get_mib(priv->udev, MIB_MAC_WEP, m,
0970                sizeof(struct mib_mac_wep));
0971     if (ret < 0) {
0972         wiphy_err(priv->hw->wiphy,
0973               "at76_get_mib (MAC_WEP) failed: %d\n", ret);
0974         goto exit;
0975     }
0976 
0977     at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: priv_invoked %u def_key_id %u "
0978          "key_len %u excl_unencr %u wep_icv_err %u wep_excluded %u "
0979          "encr_level %u key %d", wiphy_name(priv->hw->wiphy),
0980          m->privacy_invoked, m->wep_default_key_id,
0981          m->wep_key_mapping_len, m->exclude_unencrypted,
0982          le32_to_cpu(m->wep_icv_error_count),
0983          le32_to_cpu(m->wep_excluded_count), m->encryption_level,
0984          m->wep_default_key_id);
0985 
0986     key_len = (m->encryption_level == 1) ?
0987         WEP_SMALL_KEY_LEN : WEP_LARGE_KEY_LEN;
0988 
0989     for (i = 0; i < WEP_KEYS; i++)
0990         at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: key %d: %*phD",
0991              wiphy_name(priv->hw->wiphy), i,
0992              key_len, m->wep_default_keyvalue[i]);
0993 exit:
0994     kfree(m);
0995 }
0996 
0997 static void at76_dump_mib_mac_mgmt(struct at76_priv *priv)
0998 {
0999     int ret;
1000     struct mib_mac_mgmt *m = kmalloc(sizeof(struct mib_mac_mgmt),
1001                      GFP_KERNEL);
1002 
1003     if (!m)
1004         return;
1005 
1006     ret = at76_get_mib(priv->udev, MIB_MAC_MGMT, m,
1007                sizeof(struct mib_mac_mgmt));
1008     if (ret < 0) {
1009         wiphy_err(priv->hw->wiphy,
1010               "at76_get_mib (MAC_MGMT) failed: %d\n", ret);
1011         goto exit;
1012     }
1013 
1014     at76_dbg(DBG_MIB, "%s: MIB MAC_MGMT: beacon_period %d CFP_max_duration "
1015          "%d medium_occupancy_limit %d station_id 0x%x ATIM_window %d "
1016          "CFP_mode %d privacy_opt_impl %d DTIM_period %d CFP_period %d "
1017          "current_bssid %pM current_essid %*phD current_bss_type %d "
1018          "pm_mode %d ibss_change %d res %d "
1019          "multi_domain_capability_implemented %d "
1020          "international_roaming %d country_string %.3s",
1021          wiphy_name(priv->hw->wiphy), le16_to_cpu(m->beacon_period),
1022          le16_to_cpu(m->CFP_max_duration),
1023          le16_to_cpu(m->medium_occupancy_limit),
1024          le16_to_cpu(m->station_id), le16_to_cpu(m->ATIM_window),
1025          m->CFP_mode, m->privacy_option_implemented, m->DTIM_period,
1026          m->CFP_period, m->current_bssid,
1027          IW_ESSID_MAX_SIZE, m->current_essid,
1028          m->current_bss_type, m->power_mgmt_mode, m->ibss_change,
1029          m->res, m->multi_domain_capability_implemented,
1030          m->multi_domain_capability_enabled, m->country_string);
1031 exit:
1032     kfree(m);
1033 }
1034 
1035 static void at76_dump_mib_mac(struct at76_priv *priv)
1036 {
1037     int ret;
1038     struct mib_mac *m = kmalloc(sizeof(struct mib_mac), GFP_KERNEL);
1039 
1040     if (!m)
1041         return;
1042 
1043     ret = at76_get_mib(priv->udev, MIB_MAC, m, sizeof(struct mib_mac));
1044     if (ret < 0) {
1045         wiphy_err(priv->hw->wiphy,
1046               "at76_get_mib (MAC) failed: %d\n", ret);
1047         goto exit;
1048     }
1049 
1050     at76_dbg(DBG_MIB, "%s: MIB MAC: max_tx_msdu_lifetime %d "
1051          "max_rx_lifetime %d frag_threshold %d rts_threshold %d "
1052          "cwmin %d cwmax %d short_retry_time %d long_retry_time %d "
1053          "scan_type %d scan_channel %d probe_delay %u "
1054          "min_channel_time %d max_channel_time %d listen_int %d "
1055          "desired_ssid %*phD desired_bssid %pM desired_bsstype %d",
1056          wiphy_name(priv->hw->wiphy),
1057          le32_to_cpu(m->max_tx_msdu_lifetime),
1058          le32_to_cpu(m->max_rx_lifetime),
1059          le16_to_cpu(m->frag_threshold), le16_to_cpu(m->rts_threshold),
1060          le16_to_cpu(m->cwmin), le16_to_cpu(m->cwmax),
1061          m->short_retry_time, m->long_retry_time, m->scan_type,
1062          m->scan_channel, le16_to_cpu(m->probe_delay),
1063          le16_to_cpu(m->min_channel_time),
1064          le16_to_cpu(m->max_channel_time),
1065          le16_to_cpu(m->listen_interval),
1066          IW_ESSID_MAX_SIZE, m->desired_ssid,
1067          m->desired_bssid, m->desired_bsstype);
1068 exit:
1069     kfree(m);
1070 }
1071 
1072 static void at76_dump_mib_phy(struct at76_priv *priv)
1073 {
1074     int ret;
1075     struct mib_phy *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
1076 
1077     if (!m)
1078         return;
1079 
1080     ret = at76_get_mib(priv->udev, MIB_PHY, m, sizeof(struct mib_phy));
1081     if (ret < 0) {
1082         wiphy_err(priv->hw->wiphy,
1083               "at76_get_mib (PHY) failed: %d\n", ret);
1084         goto exit;
1085     }
1086 
1087     at76_dbg(DBG_MIB, "%s: MIB PHY: ed_threshold %d slot_time %d "
1088          "sifs_time %d preamble_length %d plcp_header_length %d "
1089          "mpdu_max_length %d cca_mode_supported %d operation_rate_set "
1090          "0x%x 0x%x 0x%x 0x%x channel_id %d current_cca_mode %d "
1091          "phy_type %d current_reg_domain %d",
1092          wiphy_name(priv->hw->wiphy), le32_to_cpu(m->ed_threshold),
1093          le16_to_cpu(m->slot_time), le16_to_cpu(m->sifs_time),
1094          le16_to_cpu(m->preamble_length),
1095          le16_to_cpu(m->plcp_header_length),
1096          le16_to_cpu(m->mpdu_max_length),
1097          le16_to_cpu(m->cca_mode_supported), m->operation_rate_set[0],
1098          m->operation_rate_set[1], m->operation_rate_set[2],
1099          m->operation_rate_set[3], m->channel_id, m->current_cca_mode,
1100          m->phy_type, m->current_reg_domain);
1101 exit:
1102     kfree(m);
1103 }
1104 
1105 static void at76_dump_mib_local(struct at76_priv *priv)
1106 {
1107     int ret;
1108     struct mib_local *m = kmalloc(sizeof(*m), GFP_KERNEL);
1109 
1110     if (!m)
1111         return;
1112 
1113     ret = at76_get_mib(priv->udev, MIB_LOCAL, m, sizeof(*m));
1114     if (ret < 0) {
1115         wiphy_err(priv->hw->wiphy,
1116               "at76_get_mib (LOCAL) failed: %d\n", ret);
1117         goto exit;
1118     }
1119 
1120     at76_dbg(DBG_MIB, "%s: MIB LOCAL: beacon_enable %d "
1121          "txautorate_fallback %d ssid_size %d promiscuous_mode %d "
1122          "preamble_type %d", wiphy_name(priv->hw->wiphy),
1123          m->beacon_enable,
1124          m->txautorate_fallback, m->ssid_size, m->promiscuous_mode,
1125          m->preamble_type);
1126 exit:
1127     kfree(m);
1128 }
1129 
1130 static void at76_dump_mib_mdomain(struct at76_priv *priv)
1131 {
1132     int ret;
1133     struct mib_mdomain *m = kmalloc(sizeof(struct mib_mdomain), GFP_KERNEL);
1134 
1135     if (!m)
1136         return;
1137 
1138     ret = at76_get_mib(priv->udev, MIB_MDOMAIN, m,
1139                sizeof(struct mib_mdomain));
1140     if (ret < 0) {
1141         wiphy_err(priv->hw->wiphy,
1142               "at76_get_mib (MDOMAIN) failed: %d\n", ret);
1143         goto exit;
1144     }
1145 
1146     at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: channel_list %*phD",
1147          wiphy_name(priv->hw->wiphy),
1148          (int)sizeof(m->channel_list), m->channel_list);
1149 
1150     at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: tx_powerlevel %*phD",
1151          wiphy_name(priv->hw->wiphy),
1152          (int)sizeof(m->tx_powerlevel), m->tx_powerlevel);
1153 exit:
1154     kfree(m);
1155 }
1156 
1157 /* Enable monitor mode */
1158 static int at76_start_monitor(struct at76_priv *priv)
1159 {
1160     struct at76_req_scan scan;
1161     int ret;
1162 
1163     memset(&scan, 0, sizeof(struct at76_req_scan));
1164     eth_broadcast_addr(scan.bssid);
1165 
1166     scan.channel = priv->channel;
1167     scan.scan_type = SCAN_TYPE_PASSIVE;
1168     scan.international_scan = 0;
1169     scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
1170     scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
1171     scan.probe_delay = cpu_to_le16(0);
1172 
1173     ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
1174     if (ret >= 0)
1175         ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1176 
1177     return ret;
1178 }
1179 
1180 /* Calculate padding from txbuf->wlength (which excludes the USB TX header),
1181    likely to compensate a flaw in the AT76C503A USB part ... */
1182 static inline int at76_calc_padding(int wlen)
1183 {
1184     /* add the USB TX header */
1185     wlen += AT76_TX_HDRLEN;
1186 
1187     wlen = wlen % 64;
1188 
1189     if (wlen < 50)
1190         return 50 - wlen;
1191 
1192     if (wlen >= 61)
1193         return 64 + 50 - wlen;
1194 
1195     return 0;
1196 }
1197 
1198 static void at76_rx_callback(struct urb *urb)
1199 {
1200     struct at76_priv *priv = urb->context;
1201 
1202     tasklet_schedule(&priv->rx_tasklet);
1203 }
1204 
1205 static int at76_submit_rx_urb(struct at76_priv *priv)
1206 {
1207     int ret;
1208     int size;
1209     struct sk_buff *skb = priv->rx_skb;
1210 
1211     if (!priv->rx_urb) {
1212         wiphy_err(priv->hw->wiphy, "%s: priv->rx_urb is NULL\n",
1213               __func__);
1214         return -EFAULT;
1215     }
1216 
1217     if (!skb) {
1218         skb = dev_alloc_skb(sizeof(struct at76_rx_buffer));
1219         if (!skb) {
1220             wiphy_err(priv->hw->wiphy,
1221                   "cannot allocate rx skbuff\n");
1222             ret = -ENOMEM;
1223             goto exit;
1224         }
1225         priv->rx_skb = skb;
1226     } else {
1227         skb_push(skb, skb_headroom(skb));
1228         skb_trim(skb, 0);
1229     }
1230 
1231     size = skb_tailroom(skb);
1232     usb_fill_bulk_urb(priv->rx_urb, priv->udev, priv->rx_pipe,
1233               skb_put(skb, size), size, at76_rx_callback, priv);
1234     ret = usb_submit_urb(priv->rx_urb, GFP_ATOMIC);
1235     if (ret < 0) {
1236         if (ret == -ENODEV)
1237             at76_dbg(DBG_DEVSTART,
1238                  "usb_submit_urb returned -ENODEV");
1239         else
1240             wiphy_err(priv->hw->wiphy,
1241                   "rx, usb_submit_urb failed: %d\n", ret);
1242     }
1243 
1244 exit:
1245     if (ret < 0 && ret != -ENODEV)
1246         wiphy_err(priv->hw->wiphy,
1247               "cannot submit rx urb - please unload the driver and/or power cycle the device\n");
1248 
1249     return ret;
1250 }
1251 
1252 /* Download external firmware */
1253 static int at76_load_external_fw(struct usb_device *udev, struct fwentry *fwe)
1254 {
1255     int ret;
1256     int op_mode;
1257     int blockno = 0;
1258     int bsize;
1259     u8 *block;
1260     u8 *buf = fwe->extfw;
1261     int size = fwe->extfw_size;
1262 
1263     if (!buf || !size)
1264         return -ENOENT;
1265 
1266     op_mode = at76_get_op_mode(udev);
1267     at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
1268 
1269     if (op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
1270         dev_err(&udev->dev, "unexpected opmode %d\n", op_mode);
1271         return -EINVAL;
1272     }
1273 
1274     block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
1275     if (!block)
1276         return -ENOMEM;
1277 
1278     at76_dbg(DBG_DEVSTART, "downloading external firmware");
1279 
1280     /* for fw >= 0.100, the device needs an extra empty block */
1281     do {
1282         bsize = min_t(int, size, FW_BLOCK_SIZE);
1283         memcpy(block, buf, bsize);
1284         at76_dbg(DBG_DEVSTART,
1285              "ext fw, size left = %5d, bsize = %4d, blockno = %2d",
1286              size, bsize, blockno);
1287         ret = at76_load_ext_fw_block(udev, blockno, block, bsize);
1288         if (ret != bsize) {
1289             dev_err(&udev->dev,
1290                 "loading %dth firmware block failed: %d\n",
1291                 blockno, ret);
1292             ret = -EIO;
1293             goto exit;
1294         }
1295         buf += bsize;
1296         size -= bsize;
1297         blockno++;
1298     } while (bsize > 0);
1299 
1300     if (at76_is_505a(fwe->board_type)) {
1301         at76_dbg(DBG_DEVSTART, "200 ms delay for 505a");
1302         schedule_timeout_interruptible(HZ / 5 + 1);
1303     }
1304 
1305 exit:
1306     kfree(block);
1307     if (ret < 0)
1308         dev_err(&udev->dev,
1309             "downloading external firmware failed: %d\n", ret);
1310     return ret;
1311 }
1312 
1313 /* Download internal firmware */
1314 static int at76_load_internal_fw(struct usb_device *udev, struct fwentry *fwe)
1315 {
1316     int ret;
1317     int need_remap = !at76_is_505a(fwe->board_type);
1318 
1319     ret = at76_usbdfu_download(udev, fwe->intfw, fwe->intfw_size,
1320                    need_remap ? 0 : 2 * HZ);
1321 
1322     if (ret < 0) {
1323         dev_err(&udev->dev,
1324             "downloading internal fw failed with %d\n", ret);
1325         goto exit;
1326     }
1327 
1328     at76_dbg(DBG_DEVSTART, "sending REMAP");
1329 
1330     /* no REMAP for 505A (see SF driver) */
1331     if (need_remap) {
1332         ret = at76_remap(udev);
1333         if (ret < 0) {
1334             dev_err(&udev->dev,
1335                 "sending REMAP failed with %d\n", ret);
1336             goto exit;
1337         }
1338     }
1339 
1340     at76_dbg(DBG_DEVSTART, "sleeping for 2 seconds");
1341     schedule_timeout_interruptible(2 * HZ + 1);
1342     usb_reset_device(udev);
1343 
1344 exit:
1345     return ret;
1346 }
1347 
1348 static int at76_startup_device(struct at76_priv *priv)
1349 {
1350     struct at76_card_config *ccfg = &priv->card_config;
1351     int ret;
1352 
1353     at76_dbg(DBG_PARAMS,
1354          "%s param: ssid %.*s (%*phD) mode %s ch %d wep %s key %d "
1355          "keylen %d", wiphy_name(priv->hw->wiphy), priv->essid_size,
1356          priv->essid, IW_ESSID_MAX_SIZE, priv->essid,
1357          priv->iw_mode == IW_MODE_ADHOC ? "adhoc" : "infra",
1358          priv->channel, priv->wep_enabled ? "enabled" : "disabled",
1359          priv->wep_key_id, priv->wep_keys_len[priv->wep_key_id]);
1360     at76_dbg(DBG_PARAMS,
1361          "%s param: preamble %s rts %d retry %d frag %d "
1362          "txrate %s auth_mode %d", wiphy_name(priv->hw->wiphy),
1363          preambles[priv->preamble_type], priv->rts_threshold,
1364          priv->short_retry_limit, priv->frag_threshold,
1365          priv->txrate == TX_RATE_1MBIT ? "1MBit" : priv->txrate ==
1366          TX_RATE_2MBIT ? "2MBit" : priv->txrate ==
1367          TX_RATE_5_5MBIT ? "5.5MBit" : priv->txrate ==
1368          TX_RATE_11MBIT ? "11MBit" : priv->txrate ==
1369          TX_RATE_AUTO ? "auto" : "<invalid>", priv->auth_mode);
1370     at76_dbg(DBG_PARAMS,
1371          "%s param: pm_mode %d pm_period %d auth_mode %s "
1372          "scan_times %d %d scan_mode %s",
1373          wiphy_name(priv->hw->wiphy), priv->pm_mode, priv->pm_period,
1374          priv->auth_mode == WLAN_AUTH_OPEN ? "open" : "shared_secret",
1375          priv->scan_min_time, priv->scan_max_time,
1376          priv->scan_mode == SCAN_TYPE_ACTIVE ? "active" : "passive");
1377 
1378     memset(ccfg, 0, sizeof(struct at76_card_config));
1379     ccfg->promiscuous_mode = 0;
1380     ccfg->short_retry_limit = priv->short_retry_limit;
1381 
1382     if (priv->wep_enabled) {
1383         if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
1384             ccfg->encryption_type = 2;
1385         else
1386             ccfg->encryption_type = 1;
1387 
1388         /* jal: always exclude unencrypted if WEP is active */
1389         ccfg->exclude_unencrypted = 1;
1390     } else {
1391         ccfg->exclude_unencrypted = 0;
1392         ccfg->encryption_type = 0;
1393     }
1394 
1395     ccfg->rts_threshold = cpu_to_le16(priv->rts_threshold);
1396     ccfg->fragmentation_threshold = cpu_to_le16(priv->frag_threshold);
1397 
1398     memcpy(ccfg->basic_rate_set, hw_rates, 4);
1399     /* jal: really needed, we do a set_mib for autorate later ??? */
1400     ccfg->auto_rate_fallback = (priv->txrate == TX_RATE_AUTO ? 1 : 0);
1401     ccfg->channel = priv->channel;
1402     ccfg->privacy_invoked = priv->wep_enabled;
1403     memcpy(ccfg->current_ssid, priv->essid, IW_ESSID_MAX_SIZE);
1404     ccfg->ssid_len = priv->essid_size;
1405 
1406     ccfg->wep_default_key_id = priv->wep_key_id;
1407     memcpy(ccfg->wep_default_key_value, priv->wep_keys,
1408            sizeof(priv->wep_keys));
1409 
1410     ccfg->short_preamble = priv->preamble_type;
1411     ccfg->beacon_period = cpu_to_le16(priv->beacon_period);
1412 
1413     ret = at76_set_card_command(priv->udev, CMD_STARTUP, &priv->card_config,
1414                     sizeof(struct at76_card_config));
1415     if (ret < 0) {
1416         wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
1417               ret);
1418         return ret;
1419     }
1420 
1421     at76_wait_completion(priv, CMD_STARTUP);
1422 
1423     /* remove BSSID from previous run */
1424     eth_zero_addr(priv->bssid);
1425 
1426     priv->scanning = false;
1427 
1428     if (at76_set_radio(priv, 1) == 1)
1429         at76_wait_completion(priv, CMD_RADIO_ON);
1430 
1431     ret = at76_set_preamble(priv, priv->preamble_type);
1432     if (ret < 0)
1433         return ret;
1434 
1435     ret = at76_set_frag(priv, priv->frag_threshold);
1436     if (ret < 0)
1437         return ret;
1438 
1439     ret = at76_set_rts(priv, priv->rts_threshold);
1440     if (ret < 0)
1441         return ret;
1442 
1443     ret = at76_set_autorate_fallback(priv,
1444                      priv->txrate == TX_RATE_AUTO ? 1 : 0);
1445     if (ret < 0)
1446         return ret;
1447 
1448     ret = at76_set_pm_mode(priv);
1449     if (ret < 0)
1450         return ret;
1451 
1452     if (at76_debug & DBG_MIB) {
1453         at76_dump_mib_mac(priv);
1454         at76_dump_mib_mac_addr(priv);
1455         at76_dump_mib_mac_mgmt(priv);
1456         at76_dump_mib_mac_wep(priv);
1457         at76_dump_mib_mdomain(priv);
1458         at76_dump_mib_phy(priv);
1459         at76_dump_mib_local(priv);
1460     }
1461 
1462     return 0;
1463 }
1464 
1465 /* Enable or disable promiscuous mode */
1466 static void at76_work_set_promisc(struct work_struct *work)
1467 {
1468     struct at76_priv *priv = container_of(work, struct at76_priv,
1469                           work_set_promisc);
1470     int ret = 0;
1471 
1472     if (priv->device_unplugged)
1473         return;
1474 
1475     mutex_lock(&priv->mtx);
1476 
1477     priv->mib_buf.type = MIB_LOCAL;
1478     priv->mib_buf.size = 1;
1479     priv->mib_buf.index = offsetof(struct mib_local, promiscuous_mode);
1480     priv->mib_buf.data.byte = priv->promisc ? 1 : 0;
1481 
1482     ret = at76_set_mib(priv, &priv->mib_buf);
1483     if (ret < 0)
1484         wiphy_err(priv->hw->wiphy,
1485               "set_mib (promiscuous_mode) failed: %d\n", ret);
1486 
1487     mutex_unlock(&priv->mtx);
1488 }
1489 
1490 /* Submit Rx urb back to the device */
1491 static void at76_work_submit_rx(struct work_struct *work)
1492 {
1493     struct at76_priv *priv = container_of(work, struct at76_priv,
1494                           work_submit_rx);
1495 
1496     mutex_lock(&priv->mtx);
1497     at76_submit_rx_urb(priv);
1498     mutex_unlock(&priv->mtx);
1499 }
1500 
1501 /* This is a workaround to make scan working:
1502  * currently mac80211 does not process frames with no frequency
1503  * information.
1504  * However during scan the HW performs a sweep by itself, and we
1505  * are unable to know where the radio is actually tuned.
1506  * This function tries to do its best to guess this information..
1507  * During scan, If the current frame is a beacon or a probe response,
1508  * the channel information is extracted from it.
1509  * When not scanning, for other frames, or if it happens that for
1510  * whatever reason we fail to parse beacons and probe responses, this
1511  * function returns the priv->channel information, that should be correct
1512  * at least when we are not scanning.
1513  */
1514 static inline int at76_guess_freq(struct at76_priv *priv)
1515 {
1516     size_t el_off;
1517     const u8 *el;
1518     int channel = priv->channel;
1519     int len = priv->rx_skb->len;
1520     struct ieee80211_hdr *hdr = (void *)priv->rx_skb->data;
1521 
1522     if (!priv->scanning)
1523         goto exit;
1524 
1525     if (len < 24)
1526         goto exit;
1527 
1528     if (ieee80211_is_probe_resp(hdr->frame_control)) {
1529         el_off = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
1530         el = ((struct ieee80211_mgmt *)hdr)->u.probe_resp.variable;
1531     } else if (ieee80211_is_beacon(hdr->frame_control)) {
1532         el_off = offsetof(struct ieee80211_mgmt, u.beacon.variable);
1533         el = ((struct ieee80211_mgmt *)hdr)->u.beacon.variable;
1534     } else {
1535         goto exit;
1536     }
1537     len -= el_off;
1538 
1539     el = cfg80211_find_ie(WLAN_EID_DS_PARAMS, el, len);
1540     if (el && el[1] > 0)
1541         channel = el[2];
1542 
1543 exit:
1544     return ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
1545 }
1546 
1547 static void at76_rx_tasklet(struct tasklet_struct *t)
1548 {
1549     struct at76_priv *priv = from_tasklet(priv, t, rx_tasklet);
1550     struct urb *urb = priv->rx_urb;
1551     struct at76_rx_buffer *buf;
1552     struct ieee80211_rx_status rx_status = { 0 };
1553 
1554     if (priv->device_unplugged) {
1555         at76_dbg(DBG_DEVSTART, "device unplugged");
1556         at76_dbg(DBG_DEVSTART, "urb status %d", urb->status);
1557         return;
1558     }
1559 
1560     if (!priv->rx_skb || !priv->rx_skb->data)
1561         return;
1562 
1563     buf = (struct at76_rx_buffer *)priv->rx_skb->data;
1564 
1565     if (urb->status != 0) {
1566         if (urb->status != -ENOENT && urb->status != -ECONNRESET)
1567             at76_dbg(DBG_URB,
1568                  "%s %s: - nonzero Rx bulk status received: %d",
1569                  __func__, wiphy_name(priv->hw->wiphy),
1570                  urb->status);
1571         return;
1572     }
1573 
1574     at76_dbg(DBG_RX_ATMEL_HDR,
1575          "%s: rx frame: rate %d rssi %d noise %d link %d",
1576          wiphy_name(priv->hw->wiphy), buf->rx_rate, buf->rssi,
1577          buf->noise_level, buf->link_quality);
1578 
1579     skb_pull(priv->rx_skb, AT76_RX_HDRLEN);
1580     skb_trim(priv->rx_skb, le16_to_cpu(buf->wlength));
1581     at76_dbg_dump(DBG_RX_DATA, priv->rx_skb->data,
1582               priv->rx_skb->len, "RX: len=%d", priv->rx_skb->len);
1583 
1584     rx_status.signal = buf->rssi;
1585     rx_status.flag |= RX_FLAG_DECRYPTED;
1586     rx_status.flag |= RX_FLAG_IV_STRIPPED;
1587     rx_status.band = NL80211_BAND_2GHZ;
1588     rx_status.freq = at76_guess_freq(priv);
1589 
1590     at76_dbg(DBG_MAC80211, "calling ieee80211_rx_irqsafe(): %d/%d",
1591          priv->rx_skb->len, priv->rx_skb->data_len);
1592     memcpy(IEEE80211_SKB_RXCB(priv->rx_skb), &rx_status, sizeof(rx_status));
1593     ieee80211_rx_irqsafe(priv->hw, priv->rx_skb);
1594 
1595     /* Use a new skb for the next receive */
1596     priv->rx_skb = NULL;
1597 
1598     at76_submit_rx_urb(priv);
1599 }
1600 
1601 /* Load firmware into kernel memory and parse it */
1602 static struct fwentry *at76_load_firmware(struct usb_device *udev,
1603                       enum board_type board_type)
1604 {
1605     int ret;
1606     char *str;
1607     struct at76_fw_header *fwh;
1608     struct fwentry *fwe = &firmwares[board_type];
1609 
1610     mutex_lock(&fw_mutex);
1611 
1612     if (fwe->loaded) {
1613         at76_dbg(DBG_FW, "re-using previously loaded fw");
1614         goto exit;
1615     }
1616 
1617     at76_dbg(DBG_FW, "downloading firmware %s", fwe->fwname);
1618     ret = request_firmware(&fwe->fw, fwe->fwname, &udev->dev);
1619     if (ret < 0) {
1620         dev_err(&udev->dev, "firmware %s not found!\n",
1621             fwe->fwname);
1622         dev_err(&udev->dev,
1623             "you may need to download the firmware from http://developer.berlios.de/projects/at76c503a/\n");
1624         goto exit;
1625     }
1626 
1627     at76_dbg(DBG_FW, "got it.");
1628     fwh = (struct at76_fw_header *)(fwe->fw->data);
1629 
1630     if (fwe->fw->size <= sizeof(*fwh)) {
1631         dev_err(&udev->dev,
1632             "firmware is too short (0x%zx)\n", fwe->fw->size);
1633         goto exit;
1634     }
1635 
1636     /* CRC currently not checked */
1637     fwe->board_type = le32_to_cpu(fwh->board_type);
1638     if (fwe->board_type != board_type) {
1639         dev_err(&udev->dev,
1640             "board type mismatch, requested %u, got %u\n",
1641             board_type, fwe->board_type);
1642         goto exit;
1643     }
1644 
1645     fwe->fw_version.major = fwh->major;
1646     fwe->fw_version.minor = fwh->minor;
1647     fwe->fw_version.patch = fwh->patch;
1648     fwe->fw_version.build = fwh->build;
1649 
1650     str = (char *)fwh + le32_to_cpu(fwh->str_offset);
1651     fwe->intfw = (u8 *)fwh + le32_to_cpu(fwh->int_fw_offset);
1652     fwe->intfw_size = le32_to_cpu(fwh->int_fw_len);
1653     fwe->extfw = (u8 *)fwh + le32_to_cpu(fwh->ext_fw_offset);
1654     fwe->extfw_size = le32_to_cpu(fwh->ext_fw_len);
1655 
1656     fwe->loaded = 1;
1657 
1658     dev_printk(KERN_DEBUG, &udev->dev,
1659            "using firmware %s (version %d.%d.%d-%d)\n",
1660            fwe->fwname, fwh->major, fwh->minor, fwh->patch, fwh->build);
1661 
1662     at76_dbg(DBG_DEVSTART, "board %u, int %d:%d, ext %d:%d", board_type,
1663          le32_to_cpu(fwh->int_fw_offset), le32_to_cpu(fwh->int_fw_len),
1664          le32_to_cpu(fwh->ext_fw_offset), le32_to_cpu(fwh->ext_fw_len));
1665     at76_dbg(DBG_DEVSTART, "firmware id %s", str);
1666 
1667 exit:
1668     mutex_unlock(&fw_mutex);
1669 
1670     if (fwe->loaded)
1671         return fwe;
1672     else
1673         return NULL;
1674 }
1675 
1676 static int at76_join(struct at76_priv *priv)
1677 {
1678     struct at76_req_join join;
1679     int ret;
1680 
1681     memset(&join, 0, sizeof(struct at76_req_join));
1682     memcpy(join.essid, priv->essid, priv->essid_size);
1683     join.essid_size = priv->essid_size;
1684     memcpy(join.bssid, priv->bssid, ETH_ALEN);
1685     join.bss_type = INFRASTRUCTURE_MODE;
1686     join.channel = priv->channel;
1687     join.timeout = cpu_to_le16(2000);
1688 
1689     at76_dbg(DBG_MAC80211, "%s: sending CMD_JOIN", __func__);
1690     ret = at76_set_card_command(priv->udev, CMD_JOIN, &join,
1691                     sizeof(struct at76_req_join));
1692 
1693     if (ret < 0) {
1694         wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
1695               ret);
1696         return 0;
1697     }
1698 
1699     ret = at76_wait_completion(priv, CMD_JOIN);
1700     at76_dbg(DBG_MAC80211, "%s: CMD_JOIN returned: 0x%02x", __func__, ret);
1701     if (ret != CMD_STATUS_COMPLETE) {
1702         wiphy_err(priv->hw->wiphy, "at76_wait_completion failed: %d\n",
1703               ret);
1704         return 0;
1705     }
1706 
1707     at76_set_pm_mode(priv);
1708 
1709     return 0;
1710 }
1711 
1712 static void at76_work_join_bssid(struct work_struct *work)
1713 {
1714     struct at76_priv *priv = container_of(work, struct at76_priv,
1715                           work_join_bssid);
1716 
1717     if (priv->device_unplugged)
1718         return;
1719 
1720     mutex_lock(&priv->mtx);
1721 
1722     if (is_valid_ether_addr(priv->bssid))
1723         at76_join(priv);
1724 
1725     mutex_unlock(&priv->mtx);
1726 }
1727 
1728 static void at76_mac80211_tx_callback(struct urb *urb)
1729 {
1730     struct at76_priv *priv = urb->context;
1731     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(priv->tx_skb);
1732 
1733     at76_dbg(DBG_MAC80211, "%s()", __func__);
1734 
1735     switch (urb->status) {
1736     case 0:
1737         /* success */
1738         info->flags |= IEEE80211_TX_STAT_ACK;
1739         break;
1740     case -ENOENT:
1741     case -ECONNRESET:
1742         /* fail, urb has been unlinked */
1743         /* FIXME: add error message */
1744         break;
1745     default:
1746         at76_dbg(DBG_URB, "%s - nonzero tx status received: %d",
1747              __func__, urb->status);
1748         break;
1749     }
1750 
1751     memset(&info->status, 0, sizeof(info->status));
1752 
1753     ieee80211_tx_status_irqsafe(priv->hw, priv->tx_skb);
1754 
1755     priv->tx_skb = NULL;
1756 
1757     ieee80211_wake_queues(priv->hw);
1758 }
1759 
1760 static void at76_mac80211_tx(struct ieee80211_hw *hw,
1761                  struct ieee80211_tx_control *control,
1762                  struct sk_buff *skb)
1763 {
1764     struct at76_priv *priv = hw->priv;
1765     struct at76_tx_buffer *tx_buffer = priv->bulk_out_buffer;
1766     struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1767     struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1768     int padding, submit_len, ret;
1769 
1770     at76_dbg(DBG_MAC80211, "%s()", __func__);
1771 
1772     if (priv->tx_urb->status == -EINPROGRESS) {
1773         wiphy_err(priv->hw->wiphy,
1774               "%s called while tx urb is pending\n", __func__);
1775         dev_kfree_skb_any(skb);
1776         return;
1777     }
1778 
1779     /* The following code lines are important when the device is going to
1780      * authenticate with a new bssid. The driver must send CMD_JOIN before
1781      * an authentication frame is transmitted. For this to succeed, the
1782      * correct bssid of the AP must be known. As mac80211 does not inform
1783      * drivers about the bssid prior to the authentication process the
1784      * following workaround is necessary. If the TX frame is an
1785      * authentication frame extract the bssid and send the CMD_JOIN. */
1786     if (mgmt->frame_control & cpu_to_le16(IEEE80211_STYPE_AUTH)) {
1787         if (!ether_addr_equal_64bits(priv->bssid, mgmt->bssid)) {
1788             memcpy(priv->bssid, mgmt->bssid, ETH_ALEN);
1789             ieee80211_queue_work(hw, &priv->work_join_bssid);
1790             dev_kfree_skb_any(skb);
1791             return;
1792         }
1793     }
1794 
1795     ieee80211_stop_queues(hw);
1796 
1797     at76_ledtrig_tx_activity(); /* tell ledtrigger we send a packet */
1798 
1799     WARN_ON(priv->tx_skb != NULL);
1800 
1801     priv->tx_skb = skb;
1802     padding = at76_calc_padding(skb->len);
1803     submit_len = AT76_TX_HDRLEN + skb->len + padding;
1804 
1805     /* setup 'Atmel' header */
1806     memset(tx_buffer, 0, sizeof(*tx_buffer));
1807     tx_buffer->padding = padding;
1808     tx_buffer->wlength = cpu_to_le16(skb->len);
1809     tx_buffer->tx_rate = ieee80211_get_tx_rate(hw, info)->hw_value;
1810     memset(tx_buffer->reserved, 0, sizeof(tx_buffer->reserved));
1811     memcpy(tx_buffer->packet, skb->data, skb->len);
1812 
1813     at76_dbg(DBG_TX_DATA, "%s tx: wlen 0x%x pad 0x%x rate %d hdr",
1814          wiphy_name(priv->hw->wiphy), le16_to_cpu(tx_buffer->wlength),
1815          tx_buffer->padding, tx_buffer->tx_rate);
1816 
1817     /* send stuff */
1818     at76_dbg_dump(DBG_TX_DATA_CONTENT, tx_buffer, submit_len,
1819               "%s(): tx_buffer %d bytes:", __func__, submit_len);
1820     usb_fill_bulk_urb(priv->tx_urb, priv->udev, priv->tx_pipe, tx_buffer,
1821               submit_len, at76_mac80211_tx_callback, priv);
1822     ret = usb_submit_urb(priv->tx_urb, GFP_ATOMIC);
1823     if (ret) {
1824         wiphy_err(priv->hw->wiphy, "error in tx submit urb: %d\n", ret);
1825         if (ret == -EINVAL)
1826             wiphy_err(priv->hw->wiphy,
1827                   "-EINVAL: tx urb %p hcpriv %p complete %p\n",
1828                   priv->tx_urb,
1829                   priv->tx_urb->hcpriv, priv->tx_urb->complete);
1830     }
1831 }
1832 
1833 static int at76_mac80211_start(struct ieee80211_hw *hw)
1834 {
1835     struct at76_priv *priv = hw->priv;
1836     int ret;
1837 
1838     at76_dbg(DBG_MAC80211, "%s()", __func__);
1839 
1840     mutex_lock(&priv->mtx);
1841 
1842     ret = at76_submit_rx_urb(priv);
1843     if (ret < 0) {
1844         wiphy_err(priv->hw->wiphy, "open: submit_rx_urb failed: %d\n",
1845               ret);
1846         goto error;
1847     }
1848 
1849     at76_startup_device(priv);
1850 
1851     at76_start_monitor(priv);
1852 
1853 error:
1854     mutex_unlock(&priv->mtx);
1855 
1856     return 0;
1857 }
1858 
1859 static void at76_mac80211_stop(struct ieee80211_hw *hw)
1860 {
1861     struct at76_priv *priv = hw->priv;
1862 
1863     at76_dbg(DBG_MAC80211, "%s()", __func__);
1864 
1865     cancel_delayed_work(&priv->dwork_hw_scan);
1866     cancel_work_sync(&priv->work_join_bssid);
1867     cancel_work_sync(&priv->work_set_promisc);
1868 
1869     mutex_lock(&priv->mtx);
1870 
1871     if (!priv->device_unplugged) {
1872         /* We are called by "ifconfig ethX down", not because the
1873          * device is not available anymore. */
1874         at76_set_radio(priv, 0);
1875 
1876         /* We unlink rx_urb because at76_open() re-submits it.
1877          * If unplugged, at76_delete_device() takes care of it. */
1878         usb_kill_urb(priv->rx_urb);
1879     }
1880 
1881     mutex_unlock(&priv->mtx);
1882 }
1883 
1884 static int at76_add_interface(struct ieee80211_hw *hw,
1885                   struct ieee80211_vif *vif)
1886 {
1887     struct at76_priv *priv = hw->priv;
1888     int ret = 0;
1889 
1890     at76_dbg(DBG_MAC80211, "%s()", __func__);
1891 
1892     mutex_lock(&priv->mtx);
1893 
1894     switch (vif->type) {
1895     case NL80211_IFTYPE_STATION:
1896         priv->iw_mode = IW_MODE_INFRA;
1897         break;
1898     default:
1899         ret = -EOPNOTSUPP;
1900         goto exit;
1901     }
1902 
1903 exit:
1904     mutex_unlock(&priv->mtx);
1905 
1906     return ret;
1907 }
1908 
1909 static void at76_remove_interface(struct ieee80211_hw *hw,
1910                   struct ieee80211_vif *vif)
1911 {
1912     at76_dbg(DBG_MAC80211, "%s()", __func__);
1913 }
1914 
1915 static void at76_dwork_hw_scan(struct work_struct *work)
1916 {
1917     struct at76_priv *priv = container_of(work, struct at76_priv,
1918                           dwork_hw_scan.work);
1919     struct cfg80211_scan_info info = {
1920         .aborted = false,
1921     };
1922     int ret;
1923 
1924     if (priv->device_unplugged)
1925         return;
1926 
1927     mutex_lock(&priv->mtx);
1928 
1929     ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1930     at76_dbg(DBG_MAC80211, "%s: CMD_SCAN status 0x%02x", __func__, ret);
1931 
1932     /* FIXME: add maximum time for scan to complete */
1933 
1934     if (ret != CMD_STATUS_COMPLETE) {
1935         ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
1936                          SCAN_POLL_INTERVAL);
1937         mutex_unlock(&priv->mtx);
1938         return;
1939     }
1940 
1941     if (is_valid_ether_addr(priv->bssid))
1942         at76_join(priv);
1943 
1944     priv->scanning = false;
1945 
1946     mutex_unlock(&priv->mtx);
1947 
1948     ieee80211_scan_completed(priv->hw, &info);
1949 
1950     ieee80211_wake_queues(priv->hw);
1951 }
1952 
1953 static int at76_hw_scan(struct ieee80211_hw *hw,
1954             struct ieee80211_vif *vif,
1955             struct ieee80211_scan_request *hw_req)
1956 {
1957     struct cfg80211_scan_request *req = &hw_req->req;
1958     struct at76_priv *priv = hw->priv;
1959     struct at76_req_scan scan;
1960     u8 *ssid = NULL;
1961     int ret, len = 0;
1962 
1963     at76_dbg(DBG_MAC80211, "%s():", __func__);
1964 
1965     if (priv->device_unplugged)
1966         return 0;
1967 
1968     mutex_lock(&priv->mtx);
1969 
1970     ieee80211_stop_queues(hw);
1971 
1972     memset(&scan, 0, sizeof(struct at76_req_scan));
1973     eth_broadcast_addr(scan.bssid);
1974 
1975     if (req->n_ssids) {
1976         scan.scan_type = SCAN_TYPE_ACTIVE;
1977         ssid = req->ssids[0].ssid;
1978         len = req->ssids[0].ssid_len;
1979     } else {
1980         scan.scan_type = SCAN_TYPE_PASSIVE;
1981     }
1982 
1983     if (len) {
1984         memcpy(scan.essid, ssid, len);
1985         scan.essid_size = len;
1986     }
1987 
1988     scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
1989     scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
1990     scan.probe_delay = cpu_to_le16(priv->scan_min_time * 1000);
1991     scan.international_scan = 0;
1992 
1993     at76_dbg(DBG_MAC80211, "%s: sending CMD_SCAN", __func__);
1994     ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
1995 
1996     if (ret < 0) {
1997         wiphy_err(priv->hw->wiphy, "CMD_SCAN failed: %d\n", ret);
1998         goto exit;
1999     }
2000 
2001     priv->scanning = true;
2002     ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
2003                      SCAN_POLL_INTERVAL);
2004 
2005 exit:
2006     mutex_unlock(&priv->mtx);
2007 
2008     return 0;
2009 }
2010 
2011 static int at76_config(struct ieee80211_hw *hw, u32 changed)
2012 {
2013     struct at76_priv *priv = hw->priv;
2014 
2015     at76_dbg(DBG_MAC80211, "%s(): channel %d",
2016          __func__, hw->conf.chandef.chan->hw_value);
2017     at76_dbg_dump(DBG_MAC80211, priv->bssid, ETH_ALEN, "bssid:");
2018 
2019     mutex_lock(&priv->mtx);
2020 
2021     priv->channel = hw->conf.chandef.chan->hw_value;
2022 
2023     if (is_valid_ether_addr(priv->bssid))
2024         at76_join(priv);
2025     else
2026         at76_start_monitor(priv);
2027 
2028     mutex_unlock(&priv->mtx);
2029 
2030     return 0;
2031 }
2032 
2033 static void at76_bss_info_changed(struct ieee80211_hw *hw,
2034                   struct ieee80211_vif *vif,
2035                   struct ieee80211_bss_conf *conf,
2036                   u64 changed)
2037 {
2038     struct at76_priv *priv = hw->priv;
2039 
2040     at76_dbg(DBG_MAC80211, "%s():", __func__);
2041 
2042     if (!(changed & BSS_CHANGED_BSSID))
2043         return;
2044 
2045     at76_dbg_dump(DBG_MAC80211, conf->bssid, ETH_ALEN, "bssid:");
2046 
2047     mutex_lock(&priv->mtx);
2048 
2049     memcpy(priv->bssid, conf->bssid, ETH_ALEN);
2050 
2051     if (is_valid_ether_addr(priv->bssid))
2052         /* mac80211 is joining a bss */
2053         at76_join(priv);
2054 
2055     mutex_unlock(&priv->mtx);
2056 }
2057 
2058 /* must be atomic */
2059 static void at76_configure_filter(struct ieee80211_hw *hw,
2060                   unsigned int changed_flags,
2061                   unsigned int *total_flags, u64 multicast)
2062 {
2063     struct at76_priv *priv = hw->priv;
2064     int flags;
2065 
2066     at76_dbg(DBG_MAC80211, "%s(): changed_flags=0x%08x "
2067          "total_flags=0x%08x",
2068          __func__, changed_flags, *total_flags);
2069 
2070     flags = changed_flags & AT76_SUPPORTED_FILTERS;
2071     *total_flags = AT76_SUPPORTED_FILTERS;
2072 
2073     /* Bail out after updating flags to prevent a WARN_ON in mac80211. */
2074     if (priv->device_unplugged)
2075         return;
2076 
2077     /* FIXME: access to priv->promisc should be protected with
2078      * priv->mtx, but it's impossible because this function needs to be
2079      * atomic */
2080 
2081     if (flags && !priv->promisc) {
2082         /* mac80211 wants us to enable promiscuous mode */
2083         priv->promisc = 1;
2084     } else if (!flags && priv->promisc) {
2085         /* we need to disable promiscuous mode */
2086         priv->promisc = 0;
2087     } else
2088         return;
2089 
2090     ieee80211_queue_work(hw, &priv->work_set_promisc);
2091 }
2092 
2093 static int at76_set_wep(struct at76_priv *priv)
2094 {
2095     int ret = 0;
2096     struct mib_mac_wep *mib_data = &priv->mib_buf.data.wep_mib;
2097 
2098     priv->mib_buf.type = MIB_MAC_WEP;
2099     priv->mib_buf.size = sizeof(struct mib_mac_wep);
2100     priv->mib_buf.index = 0;
2101 
2102     memset(mib_data, 0, sizeof(*mib_data));
2103 
2104     if (priv->wep_enabled) {
2105         if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
2106             mib_data->encryption_level = 2;
2107         else
2108             mib_data->encryption_level = 1;
2109 
2110         /* always exclude unencrypted if WEP is active */
2111         mib_data->exclude_unencrypted = 1;
2112     } else {
2113         mib_data->exclude_unencrypted = 0;
2114         mib_data->encryption_level = 0;
2115     }
2116 
2117     mib_data->privacy_invoked = priv->wep_enabled;
2118     mib_data->wep_default_key_id = priv->wep_key_id;
2119     memcpy(mib_data->wep_default_keyvalue, priv->wep_keys,
2120            sizeof(priv->wep_keys));
2121 
2122     ret = at76_set_mib(priv, &priv->mib_buf);
2123 
2124     if (ret < 0)
2125         wiphy_err(priv->hw->wiphy,
2126               "set_mib (wep) failed: %d\n", ret);
2127 
2128     return ret;
2129 }
2130 
2131 static int at76_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2132             struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2133             struct ieee80211_key_conf *key)
2134 {
2135     struct at76_priv *priv = hw->priv;
2136 
2137     int i;
2138 
2139     at76_dbg(DBG_MAC80211, "%s(): cmd %d key->cipher %d key->keyidx %d "
2140          "key->keylen %d",
2141          __func__, cmd, key->cipher, key->keyidx, key->keylen);
2142 
2143     if ((key->cipher != WLAN_CIPHER_SUITE_WEP40) &&
2144         (key->cipher != WLAN_CIPHER_SUITE_WEP104))
2145         return -EOPNOTSUPP;
2146 
2147     key->hw_key_idx = key->keyidx;
2148 
2149     mutex_lock(&priv->mtx);
2150 
2151     switch (cmd) {
2152     case SET_KEY:
2153         memcpy(priv->wep_keys[key->keyidx], key->key, key->keylen);
2154         priv->wep_keys_len[key->keyidx] = key->keylen;
2155 
2156         /* FIXME: find out how to do this properly */
2157         priv->wep_key_id = key->keyidx;
2158 
2159         break;
2160     case DISABLE_KEY:
2161     default:
2162         priv->wep_keys_len[key->keyidx] = 0;
2163         break;
2164     }
2165 
2166     priv->wep_enabled = 0;
2167 
2168     for (i = 0; i < WEP_KEYS; i++) {
2169         if (priv->wep_keys_len[i] != 0)
2170             priv->wep_enabled = 1;
2171     }
2172 
2173     at76_set_wep(priv);
2174 
2175     mutex_unlock(&priv->mtx);
2176 
2177     return 0;
2178 }
2179 
2180 static const struct ieee80211_ops at76_ops = {
2181     .tx = at76_mac80211_tx,
2182     .add_interface = at76_add_interface,
2183     .remove_interface = at76_remove_interface,
2184     .config = at76_config,
2185     .bss_info_changed = at76_bss_info_changed,
2186     .configure_filter = at76_configure_filter,
2187     .start = at76_mac80211_start,
2188     .stop = at76_mac80211_stop,
2189     .hw_scan = at76_hw_scan,
2190     .set_key = at76_set_key,
2191 };
2192 
2193 /* Allocate network device and initialize private data */
2194 static struct at76_priv *at76_alloc_new_device(struct usb_device *udev)
2195 {
2196     struct ieee80211_hw *hw;
2197     struct at76_priv *priv;
2198 
2199     hw = ieee80211_alloc_hw(sizeof(struct at76_priv), &at76_ops);
2200     if (!hw) {
2201         printk(KERN_ERR DRIVER_NAME ": could not register"
2202                " ieee80211_hw\n");
2203         return NULL;
2204     }
2205 
2206     priv = hw->priv;
2207     priv->hw = hw;
2208 
2209     priv->udev = udev;
2210 
2211     mutex_init(&priv->mtx);
2212     INIT_WORK(&priv->work_set_promisc, at76_work_set_promisc);
2213     INIT_WORK(&priv->work_submit_rx, at76_work_submit_rx);
2214     INIT_WORK(&priv->work_join_bssid, at76_work_join_bssid);
2215     INIT_DELAYED_WORK(&priv->dwork_hw_scan, at76_dwork_hw_scan);
2216 
2217     tasklet_setup(&priv->rx_tasklet, at76_rx_tasklet);
2218 
2219     priv->pm_mode = AT76_PM_OFF;
2220     priv->pm_period = 0;
2221 
2222     /* unit us */
2223 
2224     return priv;
2225 }
2226 
2227 static int at76_alloc_urbs(struct at76_priv *priv,
2228                struct usb_interface *interface)
2229 {
2230     struct usb_endpoint_descriptor *endpoint, *ep_in, *ep_out;
2231     int i;
2232     int buffer_size;
2233     struct usb_host_interface *iface_desc;
2234 
2235     at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2236 
2237     at76_dbg(DBG_URB, "%s: NumEndpoints %d ", __func__,
2238          interface->cur_altsetting->desc.bNumEndpoints);
2239 
2240     ep_in = NULL;
2241     ep_out = NULL;
2242     iface_desc = interface->cur_altsetting;
2243     for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2244         endpoint = &iface_desc->endpoint[i].desc;
2245 
2246         at76_dbg(DBG_URB, "%s: %d. endpoint: addr 0x%x attr 0x%x",
2247              __func__, i, endpoint->bEndpointAddress,
2248              endpoint->bmAttributes);
2249 
2250         if (!ep_in && usb_endpoint_is_bulk_in(endpoint))
2251             ep_in = endpoint;
2252 
2253         if (!ep_out && usb_endpoint_is_bulk_out(endpoint))
2254             ep_out = endpoint;
2255     }
2256 
2257     if (!ep_in || !ep_out) {
2258         dev_err(&interface->dev, "bulk endpoints missing\n");
2259         return -ENXIO;
2260     }
2261 
2262     priv->rx_pipe = usb_rcvbulkpipe(priv->udev, ep_in->bEndpointAddress);
2263     priv->tx_pipe = usb_sndbulkpipe(priv->udev, ep_out->bEndpointAddress);
2264 
2265     priv->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2266     priv->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2267     if (!priv->rx_urb || !priv->tx_urb) {
2268         dev_err(&interface->dev, "cannot allocate URB\n");
2269         return -ENOMEM;
2270     }
2271 
2272     buffer_size = sizeof(struct at76_tx_buffer) + MAX_PADDING_SIZE;
2273     priv->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
2274     if (!priv->bulk_out_buffer)
2275         return -ENOMEM;
2276 
2277     at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2278 
2279     return 0;
2280 }
2281 
2282 static struct ieee80211_rate at76_rates[] = {
2283     { .bitrate = 10, .hw_value = TX_RATE_1MBIT, },
2284     { .bitrate = 20, .hw_value = TX_RATE_2MBIT, },
2285     { .bitrate = 55, .hw_value = TX_RATE_5_5MBIT, },
2286     { .bitrate = 110, .hw_value = TX_RATE_11MBIT, },
2287 };
2288 
2289 static struct ieee80211_channel at76_channels[] = {
2290     { .center_freq = 2412, .hw_value = 1 },
2291     { .center_freq = 2417, .hw_value = 2 },
2292     { .center_freq = 2422, .hw_value = 3 },
2293     { .center_freq = 2427, .hw_value = 4 },
2294     { .center_freq = 2432, .hw_value = 5 },
2295     { .center_freq = 2437, .hw_value = 6 },
2296     { .center_freq = 2442, .hw_value = 7 },
2297     { .center_freq = 2447, .hw_value = 8 },
2298     { .center_freq = 2452, .hw_value = 9 },
2299     { .center_freq = 2457, .hw_value = 10 },
2300     { .center_freq = 2462, .hw_value = 11 },
2301     { .center_freq = 2467, .hw_value = 12 },
2302     { .center_freq = 2472, .hw_value = 13 },
2303     { .center_freq = 2484, .hw_value = 14 }
2304 };
2305 
2306 static struct ieee80211_supported_band at76_supported_band = {
2307     .channels = at76_channels,
2308     .n_channels = ARRAY_SIZE(at76_channels),
2309     .bitrates = at76_rates,
2310     .n_bitrates = ARRAY_SIZE(at76_rates),
2311 };
2312 
2313 /* Register network device and initialize the hardware */
2314 static int at76_init_new_device(struct at76_priv *priv,
2315                 struct usb_interface *interface)
2316 {
2317     struct wiphy *wiphy;
2318     size_t len;
2319     int ret;
2320 
2321     /* set up the endpoint information */
2322     /* check out the endpoints */
2323 
2324     at76_dbg(DBG_DEVSTART, "USB interface: %d endpoints",
2325          interface->cur_altsetting->desc.bNumEndpoints);
2326 
2327     ret = at76_alloc_urbs(priv, interface);
2328     if (ret < 0)
2329         goto exit;
2330 
2331     /* MAC address */
2332     ret = at76_get_hw_config(priv);
2333     if (ret < 0) {
2334         dev_err(&interface->dev, "cannot get MAC address\n");
2335         goto exit;
2336     }
2337 
2338     priv->domain = at76_get_reg_domain(priv->regulatory_domain);
2339 
2340     priv->channel = DEF_CHANNEL;
2341     priv->iw_mode = IW_MODE_INFRA;
2342     priv->rts_threshold = DEF_RTS_THRESHOLD;
2343     priv->frag_threshold = DEF_FRAG_THRESHOLD;
2344     priv->short_retry_limit = DEF_SHORT_RETRY_LIMIT;
2345     priv->txrate = TX_RATE_AUTO;
2346     priv->preamble_type = PREAMBLE_TYPE_LONG;
2347     priv->beacon_period = 100;
2348     priv->auth_mode = WLAN_AUTH_OPEN;
2349     priv->scan_min_time = DEF_SCAN_MIN_TIME;
2350     priv->scan_max_time = DEF_SCAN_MAX_TIME;
2351     priv->scan_mode = SCAN_TYPE_ACTIVE;
2352     priv->device_unplugged = 0;
2353 
2354     /* mac80211 initialisation */
2355     wiphy = priv->hw->wiphy;
2356     priv->hw->wiphy->max_scan_ssids = 1;
2357     priv->hw->wiphy->max_scan_ie_len = 0;
2358     priv->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
2359     priv->hw->wiphy->bands[NL80211_BAND_2GHZ] = &at76_supported_band;
2360     ieee80211_hw_set(priv->hw, RX_INCLUDES_FCS);
2361     ieee80211_hw_set(priv->hw, SIGNAL_UNSPEC);
2362     priv->hw->max_signal = 100;
2363 
2364     SET_IEEE80211_DEV(priv->hw, &interface->dev);
2365     SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
2366 
2367     len = sizeof(wiphy->fw_version);
2368     snprintf(wiphy->fw_version, len, "%d.%d.%d-%d",
2369          priv->fw_version.major, priv->fw_version.minor,
2370          priv->fw_version.patch, priv->fw_version.build);
2371 
2372     wiphy->hw_version = priv->board_type;
2373 
2374     wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
2375 
2376     ret = ieee80211_register_hw(priv->hw);
2377     if (ret) {
2378         printk(KERN_ERR "cannot register mac80211 hw (status %d)!\n",
2379                ret);
2380         goto exit;
2381     }
2382 
2383     priv->mac80211_registered = 1;
2384 
2385     wiphy_info(priv->hw->wiphy, "USB %s, MAC %pM, firmware %d.%d.%d-%d\n",
2386            dev_name(&interface->dev), priv->mac_addr,
2387            priv->fw_version.major, priv->fw_version.minor,
2388            priv->fw_version.patch, priv->fw_version.build);
2389     wiphy_info(priv->hw->wiphy, "regulatory domain 0x%02x: %s\n",
2390            priv->regulatory_domain, priv->domain->name);
2391 
2392 exit:
2393     return ret;
2394 }
2395 
2396 static void at76_delete_device(struct at76_priv *priv)
2397 {
2398     at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2399 
2400     /* The device is gone, don't bother turning it off */
2401     priv->device_unplugged = 1;
2402 
2403     tasklet_kill(&priv->rx_tasklet);
2404 
2405     if (priv->mac80211_registered)
2406         ieee80211_unregister_hw(priv->hw);
2407 
2408     if (priv->tx_urb) {
2409         usb_kill_urb(priv->tx_urb);
2410         usb_free_urb(priv->tx_urb);
2411     }
2412     if (priv->rx_urb) {
2413         usb_kill_urb(priv->rx_urb);
2414         usb_free_urb(priv->rx_urb);
2415     }
2416 
2417     at76_dbg(DBG_PROC_ENTRY, "%s: unlinked urbs", __func__);
2418 
2419     kfree(priv->bulk_out_buffer);
2420 
2421     del_timer_sync(&ledtrig_tx_timer);
2422 
2423     kfree_skb(priv->rx_skb);
2424 
2425     at76_dbg(DBG_PROC_ENTRY, "%s: before freeing priv/ieee80211_hw",
2426          __func__);
2427     ieee80211_free_hw(priv->hw);
2428 
2429     at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2430 }
2431 
2432 static int at76_probe(struct usb_interface *interface,
2433               const struct usb_device_id *id)
2434 {
2435     int ret;
2436     struct at76_priv *priv;
2437     struct fwentry *fwe;
2438     struct usb_device *udev;
2439     int op_mode;
2440     int need_ext_fw = 0;
2441     struct mib_fw_version *fwv = NULL;
2442     int board_type = (int)id->driver_info;
2443 
2444     udev = usb_get_dev(interface_to_usbdev(interface));
2445 
2446     fwv = kmalloc(sizeof(*fwv), GFP_KERNEL);
2447     if (!fwv) {
2448         ret = -ENOMEM;
2449         goto exit;
2450     }
2451 
2452     /* Load firmware into kernel memory */
2453     fwe = at76_load_firmware(udev, board_type);
2454     if (!fwe) {
2455         ret = -ENOENT;
2456         goto exit;
2457     }
2458 
2459     op_mode = at76_get_op_mode(udev);
2460 
2461     at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
2462 
2463     /* we get OPMODE_NONE with 2.4.23, SMC2662W-AR ???
2464        we get 204 with 2.4.23, Fiberline FL-WL240u (505A+RFMD2958) ??? */
2465 
2466     if (op_mode == OPMODE_HW_CONFIG_MODE) {
2467         dev_err(&interface->dev,
2468             "cannot handle a device in HW_CONFIG_MODE\n");
2469         ret = -EBUSY;
2470         goto exit;
2471     }
2472 
2473     if (op_mode != OPMODE_NORMAL_NIC_WITH_FLASH
2474         && op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
2475         /* download internal firmware part */
2476         dev_printk(KERN_DEBUG, &interface->dev,
2477                "downloading internal firmware\n");
2478         ret = at76_load_internal_fw(udev, fwe);
2479         if (ret < 0) {
2480             dev_err(&interface->dev,
2481                 "error %d downloading internal firmware\n",
2482                 ret);
2483         }
2484         goto exit;
2485     }
2486 
2487     /* Internal firmware already inside the device.  Get firmware
2488      * version to test if external firmware is loaded.
2489      * This works only for newer firmware, e.g. the Intersil 0.90.x
2490      * says "control timeout on ep0in" and subsequent
2491      * at76_get_op_mode() fail too :-( */
2492 
2493     /* if version >= 0.100.x.y or device with built-in flash we can
2494      * query the device for the fw version */
2495     if ((fwe->fw_version.major > 0 || fwe->fw_version.minor >= 100)
2496         || (op_mode == OPMODE_NORMAL_NIC_WITH_FLASH)) {
2497         ret = at76_get_mib(udev, MIB_FW_VERSION, fwv, sizeof(*fwv));
2498         if (ret < 0 || (fwv->major | fwv->minor) == 0)
2499             need_ext_fw = 1;
2500     } else
2501         /* No way to check firmware version, reload to be sure */
2502         need_ext_fw = 1;
2503 
2504     if (need_ext_fw) {
2505         dev_printk(KERN_DEBUG, &interface->dev,
2506                "downloading external firmware\n");
2507 
2508         ret = at76_load_external_fw(udev, fwe);
2509         if (ret < 0)
2510             goto exit;
2511 
2512         /* Re-check firmware version */
2513         ret = at76_get_mib(udev, MIB_FW_VERSION, fwv, sizeof(*fwv));
2514         if (ret < 0) {
2515             dev_err(&interface->dev,
2516                 "error %d getting firmware version\n", ret);
2517             goto exit;
2518         }
2519     }
2520 
2521     priv = at76_alloc_new_device(udev);
2522     if (!priv) {
2523         ret = -ENOMEM;
2524         goto exit;
2525     }
2526 
2527     usb_set_intfdata(interface, priv);
2528 
2529     memcpy(&priv->fw_version, fwv, sizeof(struct mib_fw_version));
2530     priv->board_type = board_type;
2531 
2532     ret = at76_init_new_device(priv, interface);
2533     if (ret < 0)
2534         at76_delete_device(priv);
2535 
2536 exit:
2537     kfree(fwv);
2538     if (ret < 0)
2539         usb_put_dev(udev);
2540     return ret;
2541 }
2542 
2543 static void at76_disconnect(struct usb_interface *interface)
2544 {
2545     struct at76_priv *priv;
2546 
2547     priv = usb_get_intfdata(interface);
2548     usb_set_intfdata(interface, NULL);
2549 
2550     /* Disconnect after loading internal firmware */
2551     if (!priv)
2552         return;
2553 
2554     wiphy_info(priv->hw->wiphy, "disconnecting\n");
2555     at76_delete_device(priv);
2556     usb_put_dev(priv->udev);
2557     dev_info(&interface->dev, "disconnected\n");
2558 }
2559 
2560 /* Structure for registering this driver with the USB subsystem */
2561 static struct usb_driver at76_driver = {
2562     .name = DRIVER_NAME,
2563     .probe = at76_probe,
2564     .disconnect = at76_disconnect,
2565     .id_table = dev_table,
2566     .disable_hub_initiated_lpm = 1,
2567 };
2568 
2569 static int __init at76_mod_init(void)
2570 {
2571     int result;
2572 
2573     printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " loading\n");
2574 
2575     /* register this driver with the USB subsystem */
2576     result = usb_register(&at76_driver);
2577     if (result < 0)
2578         printk(KERN_ERR DRIVER_NAME
2579                ": usb_register failed (status %d)\n", result);
2580     else
2581         led_trigger_register_simple("at76_usb-tx", &ledtrig_tx);
2582     return result;
2583 }
2584 
2585 static void __exit at76_mod_exit(void)
2586 {
2587     int i;
2588 
2589     printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " unloading\n");
2590     usb_deregister(&at76_driver);
2591     for (i = 0; i < ARRAY_SIZE(firmwares); i++)
2592         release_firmware(firmwares[i].fw);
2593     led_trigger_unregister_simple(ledtrig_tx);
2594 }
2595 
2596 module_param_named(debug, at76_debug, uint, 0600);
2597 MODULE_PARM_DESC(debug, "Debugging level");
2598 
2599 module_init(at76_mod_init);
2600 module_exit(at76_mod_exit);
2601 
2602 MODULE_AUTHOR("Oliver Kurth <oku@masqmail.cx>");
2603 MODULE_AUTHOR("Joerg Albert <joerg.albert@gmx.de>");
2604 MODULE_AUTHOR("Alex <alex@foogod.com>");
2605 MODULE_AUTHOR("Nick Jones");
2606 MODULE_AUTHOR("Balint Seeber <n0_5p4m_p13453@hotmail.com>");
2607 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
2608 MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
2609 MODULE_AUTHOR("Kalle Valo <kalle.valo@iki.fi>");
2610 MODULE_AUTHOR("Sebastian Smolorz <sesmo@gmx.net>");
2611 MODULE_DESCRIPTION(DRIVER_DESC);
2612 MODULE_LICENSE("GPL");