]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/at76c50x-usb.c
Merge remote-tracking branch 'signal/for-next'
[karo-tx-linux.git] / drivers / net / wireless / at76c50x-usb.c
1 /*
2  * at76c503/at76c505 USB driver
3  *
4  * Copyright (c) 2002 - 2003 Oliver Kurth
5  * Copyright (c) 2004 Joerg Albert <joerg.albert@gmx.de>
6  * Copyright (c) 2004 Nick Jones
7  * Copyright (c) 2004 Balint Seeber <n0_5p4m_p13453@hotmail.com>
8  * Copyright (c) 2007 Guido Guenther <agx@sigxcpu.org>
9  * Copyright (c) 2007 Kalle Valo <kalle.valo@iki.fi>
10  * Copyright (c) 2010 Sebastian Smolorz <sesmo@gmx.net>
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License as
14  * published by the Free Software Foundation; either version 2 of
15  * the License, or (at your option) any later version.
16  *
17  * This file is part of the Berlios driver for WLAN USB devices based on the
18  * Atmel AT76C503A/505/505A.
19  *
20  * Some iw_handler code was taken from airo.c, (C) 1999 Benjamin Reed
21  *
22  * TODO list is at the wiki:
23  *
24  * http://wireless.kernel.org/en/users/Drivers/at76c50x-usb#TODO
25  *
26  */
27
28 #include <linux/init.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <linux/module.h>
34 #include <linux/spinlock.h>
35 #include <linux/list.h>
36 #include <linux/usb.h>
37 #include <linux/netdevice.h>
38 #include <linux/if_arp.h>
39 #include <linux/etherdevice.h>
40 #include <linux/ethtool.h>
41 #include <linux/wireless.h>
42 #include <net/iw_handler.h>
43 #include <net/ieee80211_radiotap.h>
44 #include <linux/firmware.h>
45 #include <linux/leds.h>
46 #include <net/mac80211.h>
47
48 #include "at76c50x-usb.h"
49
50 /* Version information */
51 #define DRIVER_NAME "at76c50x-usb"
52 #define DRIVER_VERSION  "0.17"
53 #define DRIVER_DESC "Atmel at76x USB Wireless LAN Driver"
54
55 /* at76_debug bits */
56 #define DBG_PROGRESS            0x00000001      /* authentication/accociation */
57 #define DBG_BSS_TABLE           0x00000002      /* show BSS table after scans */
58 #define DBG_IOCTL               0x00000004      /* ioctl calls / settings */
59 #define DBG_MAC_STATE           0x00000008      /* MAC state transitions */
60 #define DBG_TX_DATA             0x00000010      /* tx header */
61 #define DBG_TX_DATA_CONTENT     0x00000020      /* tx content */
62 #define DBG_TX_MGMT             0x00000040      /* tx management */
63 #define DBG_RX_DATA             0x00000080      /* rx data header */
64 #define DBG_RX_DATA_CONTENT     0x00000100      /* rx data content */
65 #define DBG_RX_MGMT             0x00000200      /* rx mgmt frame headers */
66 #define DBG_RX_BEACON           0x00000400      /* rx beacon */
67 #define DBG_RX_CTRL             0x00000800      /* rx control */
68 #define DBG_RX_MGMT_CONTENT     0x00001000      /* rx mgmt content */
69 #define DBG_RX_FRAGS            0x00002000      /* rx data fragment handling */
70 #define DBG_DEVSTART            0x00004000      /* fw download, device start */
71 #define DBG_URB                 0x00008000      /* rx urb status, ... */
72 #define DBG_RX_ATMEL_HDR        0x00010000      /* Atmel-specific Rx headers */
73 #define DBG_PROC_ENTRY          0x00020000      /* procedure entries/exits */
74 #define DBG_PM                  0x00040000      /* power management settings */
75 #define DBG_BSS_MATCH           0x00080000      /* BSS match failures */
76 #define DBG_PARAMS              0x00100000      /* show configured parameters */
77 #define DBG_WAIT_COMPLETE       0x00200000      /* command completion */
78 #define DBG_RX_FRAGS_SKB        0x00400000      /* skb header of Rx fragments */
79 #define DBG_BSS_TABLE_RM        0x00800000      /* purging bss table entries */
80 #define DBG_MONITOR_MODE        0x01000000      /* monitor mode */
81 #define DBG_MIB                 0x02000000      /* dump all MIBs on startup */
82 #define DBG_MGMT_TIMER          0x04000000      /* dump mgmt_timer ops */
83 #define DBG_WE_EVENTS           0x08000000      /* dump wireless events */
84 #define DBG_FW                  0x10000000      /* firmware download */
85 #define DBG_DFU                 0x20000000      /* device firmware upgrade */
86 #define DBG_CMD                 0x40000000
87 #define DBG_MAC80211            0x80000000
88
89 #define DBG_DEFAULTS            0
90
91 /* Use our own dbg macro */
92 #define at76_dbg(bits, format, arg...)                                  \
93 do {                                                                    \
94         if (at76_debug & (bits))                                        \
95                 printk(KERN_DEBUG DRIVER_NAME ": " format "\n", ##arg); \
96 } while (0)
97
98 #define at76_dbg_dump(bits, buf, len, format, arg...)                   \
99 do {                                                                    \
100         if (at76_debug & (bits)) {                                      \
101                 printk(KERN_DEBUG DRIVER_NAME ": " format "\n", ##arg); \
102                 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len); \
103         }                                                               \
104 } while (0)
105
106 static uint at76_debug = DBG_DEFAULTS;
107
108 /* Protect against concurrent firmware loading and parsing */
109 static struct mutex fw_mutex;
110
111 static struct fwentry firmwares[] = {
112         [0] = { "" },
113         [BOARD_503_ISL3861] = { "atmel_at76c503-i3861.bin" },
114         [BOARD_503_ISL3863] = { "atmel_at76c503-i3863.bin" },
115         [BOARD_503] = { "atmel_at76c503-rfmd.bin" },
116         [BOARD_503_ACC] = { "atmel_at76c503-rfmd-acc.bin" },
117         [BOARD_505] = { "atmel_at76c505-rfmd.bin" },
118         [BOARD_505_2958] = { "atmel_at76c505-rfmd2958.bin" },
119         [BOARD_505A] = { "atmel_at76c505a-rfmd2958.bin" },
120         [BOARD_505AMX] = { "atmel_at76c505amx-rfmd.bin" },
121 };
122 MODULE_FIRMWARE("atmel_at76c503-i3861.bin");
123 MODULE_FIRMWARE("atmel_at76c503-i3863.bin");
124 MODULE_FIRMWARE("atmel_at76c503-rfmd.bin");
125 MODULE_FIRMWARE("atmel_at76c503-rfmd-acc.bin");
126 MODULE_FIRMWARE("atmel_at76c505-rfmd.bin");
127 MODULE_FIRMWARE("atmel_at76c505-rfmd2958.bin");
128 MODULE_FIRMWARE("atmel_at76c505a-rfmd2958.bin");
129 MODULE_FIRMWARE("atmel_at76c505amx-rfmd.bin");
130
131 #define USB_DEVICE_DATA(__ops)  .driver_info = (kernel_ulong_t)(__ops)
132
133 static struct usb_device_id dev_table[] = {
134         /*
135          * at76c503-i3861
136          */
137         /* Generic AT76C503/3861 device */
138         { USB_DEVICE(0x03eb, 0x7603), USB_DEVICE_DATA(BOARD_503_ISL3861) },
139         /* Linksys WUSB11 v2.1/v2.6 */
140         { USB_DEVICE(0x066b, 0x2211), USB_DEVICE_DATA(BOARD_503_ISL3861) },
141         /* Netgear MA101 rev. A */
142         { USB_DEVICE(0x0864, 0x4100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
143         /* Tekram U300C / Allnet ALL0193 */
144         { USB_DEVICE(0x0b3b, 0x1612), USB_DEVICE_DATA(BOARD_503_ISL3861) },
145         /* HP HN210W J7801A */
146         { USB_DEVICE(0x03f0, 0x011c), USB_DEVICE_DATA(BOARD_503_ISL3861) },
147         /* Sitecom/Z-Com/Zyxel M4Y-750 */
148         { USB_DEVICE(0x0cde, 0x0001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
149         /* Dynalink/Askey WLL013 (intersil) */
150         { USB_DEVICE(0x069a, 0x0320), USB_DEVICE_DATA(BOARD_503_ISL3861) },
151         /* EZ connect 11Mpbs Wireless USB Adapter SMC2662W v1 */
152         { USB_DEVICE(0x0d5c, 0xa001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
153         /* BenQ AWL300 */
154         { USB_DEVICE(0x04a5, 0x9000), USB_DEVICE_DATA(BOARD_503_ISL3861) },
155         /* Addtron AWU-120, Compex WLU11 */
156         { USB_DEVICE(0x05dd, 0xff31), USB_DEVICE_DATA(BOARD_503_ISL3861) },
157         /* Intel AP310 AnyPoint II USB */
158         { USB_DEVICE(0x8086, 0x0200), USB_DEVICE_DATA(BOARD_503_ISL3861) },
159         /* Dynalink L11U */
160         { USB_DEVICE(0x0d8e, 0x7100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
161         /* Arescom WL-210, FCC id 07J-GL2411USB */
162         { USB_DEVICE(0x0d8e, 0x7110), USB_DEVICE_DATA(BOARD_503_ISL3861) },
163         /* I-O DATA WN-B11/USB */
164         { USB_DEVICE(0x04bb, 0x0919), USB_DEVICE_DATA(BOARD_503_ISL3861) },
165         /* BT Voyager 1010 */
166         { USB_DEVICE(0x069a, 0x0821), USB_DEVICE_DATA(BOARD_503_ISL3861) },
167         /*
168          * at76c503-i3863
169          */
170         /* Generic AT76C503/3863 device */
171         { USB_DEVICE(0x03eb, 0x7604), USB_DEVICE_DATA(BOARD_503_ISL3863) },
172         /* Samsung SWL-2100U */
173         { USB_DEVICE(0x055d, 0xa000), USB_DEVICE_DATA(BOARD_503_ISL3863) },
174         /*
175          * at76c503-rfmd
176          */
177         /* Generic AT76C503/RFMD device */
178         { USB_DEVICE(0x03eb, 0x7605), USB_DEVICE_DATA(BOARD_503) },
179         /* Dynalink/Askey WLL013 (rfmd) */
180         { USB_DEVICE(0x069a, 0x0321), USB_DEVICE_DATA(BOARD_503) },
181         /* Linksys WUSB11 v2.6 */
182         { USB_DEVICE(0x077b, 0x2219), USB_DEVICE_DATA(BOARD_503) },
183         /* Network Everywhere NWU11B */
184         { USB_DEVICE(0x077b, 0x2227), USB_DEVICE_DATA(BOARD_503) },
185         /* Netgear MA101 rev. B */
186         { USB_DEVICE(0x0864, 0x4102), USB_DEVICE_DATA(BOARD_503) },
187         /* D-Link DWL-120 rev. E */
188         { USB_DEVICE(0x2001, 0x3200), USB_DEVICE_DATA(BOARD_503) },
189         /* Actiontec 802UAT1, HWU01150-01UK */
190         { USB_DEVICE(0x1668, 0x7605), USB_DEVICE_DATA(BOARD_503) },
191         /* AirVast W-Buddie WN210 */
192         { USB_DEVICE(0x03eb, 0x4102), USB_DEVICE_DATA(BOARD_503) },
193         /* Dick Smith Electronics XH1153 802.11b USB adapter */
194         { USB_DEVICE(0x1371, 0x5743), USB_DEVICE_DATA(BOARD_503) },
195         /* CNet CNUSB611 */
196         { USB_DEVICE(0x1371, 0x0001), USB_DEVICE_DATA(BOARD_503) },
197         /* FiberLine FL-WL200U */
198         { USB_DEVICE(0x1371, 0x0002), USB_DEVICE_DATA(BOARD_503) },
199         /* BenQ AWL400 USB stick */
200         { USB_DEVICE(0x04a5, 0x9001), USB_DEVICE_DATA(BOARD_503) },
201         /* 3Com 3CRSHEW696 */
202         { USB_DEVICE(0x0506, 0x0a01), USB_DEVICE_DATA(BOARD_503) },
203         /* Siemens Santis ADSL WLAN USB adapter WLL 013 */
204         { USB_DEVICE(0x0681, 0x001b), USB_DEVICE_DATA(BOARD_503) },
205         /* Belkin F5D6050, version 2 */
206         { USB_DEVICE(0x050d, 0x0050), USB_DEVICE_DATA(BOARD_503) },
207         /* iBlitzz, BWU613 (not *B or *SB) */
208         { USB_DEVICE(0x07b8, 0xb000), USB_DEVICE_DATA(BOARD_503) },
209         /* Gigabyte GN-WLBM101 */
210         { USB_DEVICE(0x1044, 0x8003), USB_DEVICE_DATA(BOARD_503) },
211         /* Planex GW-US11S */
212         { USB_DEVICE(0x2019, 0x3220), USB_DEVICE_DATA(BOARD_503) },
213         /* Internal WLAN adapter in h5[4,5]xx series iPAQs */
214         { USB_DEVICE(0x049f, 0x0032), USB_DEVICE_DATA(BOARD_503) },
215         /* Corega Wireless LAN USB-11 mini */
216         { USB_DEVICE(0x07aa, 0x0011), USB_DEVICE_DATA(BOARD_503) },
217         /* Corega Wireless LAN USB-11 mini2 */
218         { USB_DEVICE(0x07aa, 0x0018), USB_DEVICE_DATA(BOARD_503) },
219         /* Uniden PCW100 */
220         { USB_DEVICE(0x05dd, 0xff35), USB_DEVICE_DATA(BOARD_503) },
221         /*
222          * at76c503-rfmd-acc
223          */
224         /* SMC2664W */
225         { USB_DEVICE(0x083a, 0x3501), USB_DEVICE_DATA(BOARD_503_ACC) },
226         /* Belkin F5D6050, SMC2662W v2, SMC2662W-AR */
227         { USB_DEVICE(0x0d5c, 0xa002), USB_DEVICE_DATA(BOARD_503_ACC) },
228         /*
229          * at76c505-rfmd
230          */
231         /* Generic AT76C505/RFMD */
232         { USB_DEVICE(0x03eb, 0x7606), USB_DEVICE_DATA(BOARD_505) },
233         /*
234          * at76c505-rfmd2958
235          */
236         /* Generic AT76C505/RFMD, OvisLink WL-1130USB */
237         { USB_DEVICE(0x03eb, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
238         /* Fiberline FL-WL240U */
239         { USB_DEVICE(0x1371, 0x0014), USB_DEVICE_DATA(BOARD_505_2958) },
240         /* CNet CNUSB-611G */
241         { USB_DEVICE(0x1371, 0x0013), USB_DEVICE_DATA(BOARD_505_2958) },
242         /* Linksys WUSB11 v2.8 */
243         { USB_DEVICE(0x1915, 0x2233), USB_DEVICE_DATA(BOARD_505_2958) },
244         /* Xterasys XN-2122B, IBlitzz BWU613B/BWU613SB */
245         { USB_DEVICE(0x12fd, 0x1001), USB_DEVICE_DATA(BOARD_505_2958) },
246         /* Corega WLAN USB Stick 11 */
247         { USB_DEVICE(0x07aa, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
248         /* Microstar MSI Box MS6978 */
249         { USB_DEVICE(0x0db0, 0x1020), USB_DEVICE_DATA(BOARD_505_2958) },
250         /*
251          * at76c505a-rfmd2958
252          */
253         /* Generic AT76C505A device */
254         { USB_DEVICE(0x03eb, 0x7614), USB_DEVICE_DATA(BOARD_505A) },
255         /* Generic AT76C505AS device */
256         { USB_DEVICE(0x03eb, 0x7617), USB_DEVICE_DATA(BOARD_505A) },
257         /* Siemens Gigaset USB WLAN Adapter 11 */
258         { USB_DEVICE(0x1690, 0x0701), USB_DEVICE_DATA(BOARD_505A) },
259         /* OQO Model 01+ Internal Wi-Fi */
260         { USB_DEVICE(0x1557, 0x0002), USB_DEVICE_DATA(BOARD_505A) },
261         /*
262          * at76c505amx-rfmd
263          */
264         /* Generic AT76C505AMX device */
265         { USB_DEVICE(0x03eb, 0x7615), USB_DEVICE_DATA(BOARD_505AMX) },
266         { }
267 };
268
269 MODULE_DEVICE_TABLE(usb, dev_table);
270
271 /* Supported rates of this hardware, bit 7 marks basic rates */
272 static const u8 hw_rates[] = { 0x82, 0x84, 0x0b, 0x16 };
273
274 static const char *const preambles[] = { "long", "short", "auto" };
275
276 /* Firmware download */
277 /* DFU states */
278 #define STATE_IDLE                      0x00
279 #define STATE_DETACH                    0x01
280 #define STATE_DFU_IDLE                  0x02
281 #define STATE_DFU_DOWNLOAD_SYNC         0x03
282 #define STATE_DFU_DOWNLOAD_BUSY         0x04
283 #define STATE_DFU_DOWNLOAD_IDLE         0x05
284 #define STATE_DFU_MANIFEST_SYNC         0x06
285 #define STATE_DFU_MANIFEST              0x07
286 #define STATE_DFU_MANIFEST_WAIT_RESET   0x08
287 #define STATE_DFU_UPLOAD_IDLE           0x09
288 #define STATE_DFU_ERROR                 0x0a
289
290 /* DFU commands */
291 #define DFU_DETACH                      0
292 #define DFU_DNLOAD                      1
293 #define DFU_UPLOAD                      2
294 #define DFU_GETSTATUS                   3
295 #define DFU_CLRSTATUS                   4
296 #define DFU_GETSTATE                    5
297 #define DFU_ABORT                       6
298
299 #define FW_BLOCK_SIZE 1024
300
301 struct dfu_status {
302         unsigned char status;
303         unsigned char poll_timeout[3];
304         unsigned char state;
305         unsigned char string;
306 } __packed;
307
308 static inline int at76_is_intersil(enum board_type board)
309 {
310         return (board == BOARD_503_ISL3861 || board == BOARD_503_ISL3863);
311 }
312
313 static inline int at76_is_503rfmd(enum board_type board)
314 {
315         return (board == BOARD_503 || board == BOARD_503_ACC);
316 }
317
318 static inline int at76_is_505a(enum board_type board)
319 {
320         return (board == BOARD_505A || board == BOARD_505AMX);
321 }
322
323 /* Load a block of the first (internal) part of the firmware */
324 static int at76_load_int_fw_block(struct usb_device *udev, int blockno,
325                                   void *block, int size)
326 {
327         return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), DFU_DNLOAD,
328                                USB_TYPE_CLASS | USB_DIR_OUT |
329                                USB_RECIP_INTERFACE, blockno, 0, block, size,
330                                USB_CTRL_GET_TIMEOUT);
331 }
332
333 static int at76_dfu_get_status(struct usb_device *udev,
334                                struct dfu_status *status)
335 {
336         int ret;
337
338         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATUS,
339                               USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
340                               0, 0, status, sizeof(struct dfu_status),
341                               USB_CTRL_GET_TIMEOUT);
342         return ret;
343 }
344
345 static int at76_dfu_get_state(struct usb_device *udev, u8 *state)
346 {
347         int ret;
348
349         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATE,
350                               USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
351                               0, 0, state, 1, USB_CTRL_GET_TIMEOUT);
352         return ret;
353 }
354
355 /* Convert timeout from the DFU status to jiffies */
356 static inline unsigned long at76_get_timeout(struct dfu_status *s)
357 {
358         return msecs_to_jiffies((s->poll_timeout[2] << 16)
359                                 | (s->poll_timeout[1] << 8)
360                                 | (s->poll_timeout[0]));
361 }
362
363 /* Load internal firmware from the buffer.  If manifest_sync_timeout > 0, use
364  * its value in jiffies in the MANIFEST_SYNC state.  */
365 static int at76_usbdfu_download(struct usb_device *udev, u8 *buf, u32 size,
366                                 int manifest_sync_timeout)
367 {
368         u8 *block;
369         struct dfu_status dfu_stat_buf;
370         int ret = 0;
371         int need_dfu_state = 1;
372         int is_done = 0;
373         u8 dfu_state = 0;
374         u32 dfu_timeout = 0;
375         int bsize = 0;
376         int blockno = 0;
377
378         at76_dbg(DBG_DFU, "%s( %p, %u, %d)", __func__, buf, size,
379                  manifest_sync_timeout);
380
381         if (!size) {
382                 dev_printk(KERN_ERR, &udev->dev, "FW buffer length invalid!\n");
383                 return -EINVAL;
384         }
385
386         block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
387         if (!block)
388                 return -ENOMEM;
389
390         do {
391                 if (need_dfu_state) {
392                         ret = at76_dfu_get_state(udev, &dfu_state);
393                         if (ret < 0) {
394                                 dev_printk(KERN_ERR, &udev->dev,
395                                            "cannot get DFU state: %d\n", ret);
396                                 goto exit;
397                         }
398                         need_dfu_state = 0;
399                 }
400
401                 switch (dfu_state) {
402                 case STATE_DFU_DOWNLOAD_SYNC:
403                         at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_SYNC");
404                         ret = at76_dfu_get_status(udev, &dfu_stat_buf);
405                         if (ret >= 0) {
406                                 dfu_state = dfu_stat_buf.state;
407                                 dfu_timeout = at76_get_timeout(&dfu_stat_buf);
408                                 need_dfu_state = 0;
409                         } else
410                                 dev_printk(KERN_ERR, &udev->dev,
411                                            "at76_dfu_get_status returned %d\n",
412                                            ret);
413                         break;
414
415                 case STATE_DFU_DOWNLOAD_BUSY:
416                         at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_BUSY");
417                         need_dfu_state = 1;
418
419                         at76_dbg(DBG_DFU, "DFU: Resetting device");
420                         schedule_timeout_interruptible(dfu_timeout);
421                         break;
422
423                 case STATE_DFU_DOWNLOAD_IDLE:
424                         at76_dbg(DBG_DFU, "DOWNLOAD...");
425                         /* fall through */
426                 case STATE_DFU_IDLE:
427                         at76_dbg(DBG_DFU, "DFU IDLE");
428
429                         bsize = min_t(int, size, FW_BLOCK_SIZE);
430                         memcpy(block, buf, bsize);
431                         at76_dbg(DBG_DFU, "int fw, size left = %5d, "
432                                  "bsize = %4d, blockno = %2d", size, bsize,
433                                  blockno);
434                         ret =
435                             at76_load_int_fw_block(udev, blockno, block, bsize);
436                         buf += bsize;
437                         size -= bsize;
438                         blockno++;
439
440                         if (ret != bsize)
441                                 dev_printk(KERN_ERR, &udev->dev,
442                                            "at76_load_int_fw_block "
443                                            "returned %d\n", ret);
444                         need_dfu_state = 1;
445                         break;
446
447                 case STATE_DFU_MANIFEST_SYNC:
448                         at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_SYNC");
449
450                         ret = at76_dfu_get_status(udev, &dfu_stat_buf);
451                         if (ret < 0)
452                                 break;
453
454                         dfu_state = dfu_stat_buf.state;
455                         dfu_timeout = at76_get_timeout(&dfu_stat_buf);
456                         need_dfu_state = 0;
457
458                         /* override the timeout from the status response,
459                            needed for AT76C505A */
460                         if (manifest_sync_timeout > 0)
461                                 dfu_timeout = manifest_sync_timeout;
462
463                         at76_dbg(DBG_DFU, "DFU: Waiting for manifest phase");
464                         schedule_timeout_interruptible(dfu_timeout);
465                         break;
466
467                 case STATE_DFU_MANIFEST:
468                         at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST");
469                         is_done = 1;
470                         break;
471
472                 case STATE_DFU_MANIFEST_WAIT_RESET:
473                         at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_WAIT_RESET");
474                         is_done = 1;
475                         break;
476
477                 case STATE_DFU_UPLOAD_IDLE:
478                         at76_dbg(DBG_DFU, "STATE_DFU_UPLOAD_IDLE");
479                         break;
480
481                 case STATE_DFU_ERROR:
482                         at76_dbg(DBG_DFU, "STATE_DFU_ERROR");
483                         ret = -EPIPE;
484                         break;
485
486                 default:
487                         at76_dbg(DBG_DFU, "DFU UNKNOWN STATE (%d)", dfu_state);
488                         ret = -EINVAL;
489                         break;
490                 }
491         } while (!is_done && (ret >= 0));
492
493 exit:
494         kfree(block);
495         if (ret >= 0)
496                 ret = 0;
497
498         return ret;
499 }
500
501 /* LED trigger */
502 static int tx_activity;
503 static void at76_ledtrig_tx_timerfunc(unsigned long data);
504 static DEFINE_TIMER(ledtrig_tx_timer, at76_ledtrig_tx_timerfunc, 0, 0);
505 DEFINE_LED_TRIGGER(ledtrig_tx);
506
507 static void at76_ledtrig_tx_timerfunc(unsigned long data)
508 {
509         static int tx_lastactivity;
510
511         if (tx_lastactivity != tx_activity) {
512                 tx_lastactivity = tx_activity;
513                 led_trigger_event(ledtrig_tx, LED_FULL);
514                 mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
515         } else
516                 led_trigger_event(ledtrig_tx, LED_OFF);
517 }
518
519 static void at76_ledtrig_tx_activity(void)
520 {
521         tx_activity++;
522         if (!timer_pending(&ledtrig_tx_timer))
523                 mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
524 }
525
526 static int at76_remap(struct usb_device *udev)
527 {
528         int ret;
529         ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0a,
530                               USB_TYPE_VENDOR | USB_DIR_OUT |
531                               USB_RECIP_INTERFACE, 0, 0, NULL, 0,
532                               USB_CTRL_GET_TIMEOUT);
533         if (ret < 0)
534                 return ret;
535         return 0;
536 }
537
538 static int at76_get_op_mode(struct usb_device *udev)
539 {
540         int ret;
541         u8 saved;
542         u8 *op_mode;
543
544         op_mode = kmalloc(1, GFP_NOIO);
545         if (!op_mode)
546                 return -ENOMEM;
547         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
548                               USB_TYPE_VENDOR | USB_DIR_IN |
549                               USB_RECIP_INTERFACE, 0x01, 0, op_mode, 1,
550                               USB_CTRL_GET_TIMEOUT);
551         saved = *op_mode;
552         kfree(op_mode);
553
554         if (ret < 0)
555                 return ret;
556         else if (ret < 1)
557                 return -EIO;
558         else
559                 return saved;
560 }
561
562 /* Load a block of the second ("external") part of the firmware */
563 static inline int at76_load_ext_fw_block(struct usb_device *udev, int blockno,
564                                          void *block, int size)
565 {
566         return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
567                                USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
568                                0x0802, blockno, block, size,
569                                USB_CTRL_GET_TIMEOUT);
570 }
571
572 static inline int at76_get_hw_cfg(struct usb_device *udev,
573                                   union at76_hwcfg *buf, int buf_size)
574 {
575         return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
576                                USB_TYPE_VENDOR | USB_DIR_IN |
577                                USB_RECIP_INTERFACE, 0x0a02, 0,
578                                buf, buf_size, USB_CTRL_GET_TIMEOUT);
579 }
580
581 /* Intersil boards use a different "value" for GetHWConfig requests */
582 static inline int at76_get_hw_cfg_intersil(struct usb_device *udev,
583                                            union at76_hwcfg *buf, int buf_size)
584 {
585         return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
586                                USB_TYPE_VENDOR | USB_DIR_IN |
587                                USB_RECIP_INTERFACE, 0x0902, 0,
588                                buf, buf_size, USB_CTRL_GET_TIMEOUT);
589 }
590
591 /* Get the hardware configuration for the adapter and put it to the appropriate
592  * fields of 'priv' (the GetHWConfig request and interpretation of the result
593  * depends on the board type) */
594 static int at76_get_hw_config(struct at76_priv *priv)
595 {
596         int ret;
597         union at76_hwcfg *hwcfg = kmalloc(sizeof(*hwcfg), GFP_KERNEL);
598
599         if (!hwcfg)
600                 return -ENOMEM;
601
602         if (at76_is_intersil(priv->board_type)) {
603                 ret = at76_get_hw_cfg_intersil(priv->udev, hwcfg,
604                                                sizeof(hwcfg->i));
605                 if (ret < 0)
606                         goto exit;
607                 memcpy(priv->mac_addr, hwcfg->i.mac_addr, ETH_ALEN);
608                 priv->regulatory_domain = hwcfg->i.regulatory_domain;
609         } else if (at76_is_503rfmd(priv->board_type)) {
610                 ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r3));
611                 if (ret < 0)
612                         goto exit;
613                 memcpy(priv->mac_addr, hwcfg->r3.mac_addr, ETH_ALEN);
614                 priv->regulatory_domain = hwcfg->r3.regulatory_domain;
615         } else {
616                 ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r5));
617                 if (ret < 0)
618                         goto exit;
619                 memcpy(priv->mac_addr, hwcfg->r5.mac_addr, ETH_ALEN);
620                 priv->regulatory_domain = hwcfg->r5.regulatory_domain;
621         }
622
623 exit:
624         kfree(hwcfg);
625         if (ret < 0)
626                 wiphy_err(priv->hw->wiphy, "cannot get HW Config (error %d)\n",
627                           ret);
628
629         return ret;
630 }
631
632 static struct reg_domain const *at76_get_reg_domain(u16 code)
633 {
634         int i;
635         static struct reg_domain const fd_tab[] = {
636                 { 0x10, "FCC (USA)", 0x7ff },   /* ch 1-11 */
637                 { 0x20, "IC (Canada)", 0x7ff }, /* ch 1-11 */
638                 { 0x30, "ETSI (most of Europe)", 0x1fff },      /* ch 1-13 */
639                 { 0x31, "Spain", 0x600 },       /* ch 10-11 */
640                 { 0x32, "France", 0x1e00 },     /* ch 10-13 */
641                 { 0x40, "MKK (Japan)", 0x2000 },        /* ch 14 */
642                 { 0x41, "MKK1 (Japan)", 0x3fff },       /* ch 1-14 */
643                 { 0x50, "Israel", 0x3fc },      /* ch 3-9 */
644                 { 0x00, "<unknown>", 0xffffffff }       /* ch 1-32 */
645         };
646
647         /* Last entry is fallback for unknown domain code */
648         for (i = 0; i < ARRAY_SIZE(fd_tab) - 1; i++)
649                 if (code == fd_tab[i].code)
650                         break;
651
652         return &fd_tab[i];
653 }
654
655 static inline int at76_get_mib(struct usb_device *udev, u16 mib, void *buf,
656                                int buf_size)
657 {
658         int ret;
659
660         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
661                               USB_TYPE_VENDOR | USB_DIR_IN |
662                               USB_RECIP_INTERFACE, mib << 8, 0, buf, buf_size,
663                               USB_CTRL_GET_TIMEOUT);
664         if (ret >= 0 && ret != buf_size)
665                 return -EIO;
666         return ret;
667 }
668
669 /* Return positive number for status, negative for an error */
670 static inline int at76_get_cmd_status(struct usb_device *udev, u8 cmd)
671 {
672         u8 *stat_buf;
673         int ret;
674
675         stat_buf = kmalloc(40, GFP_NOIO);
676         if (!stat_buf)
677                 return -ENOMEM;
678
679         ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x22,
680                         USB_TYPE_VENDOR | USB_DIR_IN |
681                         USB_RECIP_INTERFACE, cmd, 0, stat_buf,
682                         40, USB_CTRL_GET_TIMEOUT);
683         if (ret >= 0)
684                 ret = stat_buf[5];
685         kfree(stat_buf);
686
687         return ret;
688 }
689
690 #define MAKE_CMD_CASE(c) case (c): return #c
691 static const char *at76_get_cmd_string(u8 cmd_status)
692 {
693         switch (cmd_status) {
694                 MAKE_CMD_CASE(CMD_SET_MIB);
695                 MAKE_CMD_CASE(CMD_GET_MIB);
696                 MAKE_CMD_CASE(CMD_SCAN);
697                 MAKE_CMD_CASE(CMD_JOIN);
698                 MAKE_CMD_CASE(CMD_START_IBSS);
699                 MAKE_CMD_CASE(CMD_RADIO_ON);
700                 MAKE_CMD_CASE(CMD_RADIO_OFF);
701                 MAKE_CMD_CASE(CMD_STARTUP);
702         }
703
704         return "UNKNOWN";
705 }
706
707 static int at76_set_card_command(struct usb_device *udev, u8 cmd, void *buf,
708                                  int buf_size)
709 {
710         int ret;
711         struct at76_command *cmd_buf = kmalloc(sizeof(struct at76_command) +
712                                                buf_size, GFP_KERNEL);
713
714         if (!cmd_buf)
715                 return -ENOMEM;
716
717         cmd_buf->cmd = cmd;
718         cmd_buf->reserved = 0;
719         cmd_buf->size = cpu_to_le16(buf_size);
720         memcpy(cmd_buf->data, buf, buf_size);
721
722         at76_dbg_dump(DBG_CMD, cmd_buf, sizeof(struct at76_command) + buf_size,
723                       "issuing command %s (0x%02x)",
724                       at76_get_cmd_string(cmd), cmd);
725
726         ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
727                               USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
728                               0, 0, cmd_buf,
729                               sizeof(struct at76_command) + buf_size,
730                               USB_CTRL_GET_TIMEOUT);
731         kfree(cmd_buf);
732         return ret;
733 }
734
735 #define MAKE_CMD_STATUS_CASE(c) case (c): return #c
736 static const char *at76_get_cmd_status_string(u8 cmd_status)
737 {
738         switch (cmd_status) {
739                 MAKE_CMD_STATUS_CASE(CMD_STATUS_IDLE);
740                 MAKE_CMD_STATUS_CASE(CMD_STATUS_COMPLETE);
741                 MAKE_CMD_STATUS_CASE(CMD_STATUS_UNKNOWN);
742                 MAKE_CMD_STATUS_CASE(CMD_STATUS_INVALID_PARAMETER);
743                 MAKE_CMD_STATUS_CASE(CMD_STATUS_FUNCTION_NOT_SUPPORTED);
744                 MAKE_CMD_STATUS_CASE(CMD_STATUS_TIME_OUT);
745                 MAKE_CMD_STATUS_CASE(CMD_STATUS_IN_PROGRESS);
746                 MAKE_CMD_STATUS_CASE(CMD_STATUS_HOST_FAILURE);
747                 MAKE_CMD_STATUS_CASE(CMD_STATUS_SCAN_FAILED);
748         }
749
750         return "UNKNOWN";
751 }
752
753 /* Wait until the command is completed */
754 static int at76_wait_completion(struct at76_priv *priv, int cmd)
755 {
756         int status = 0;
757         unsigned long timeout = jiffies + CMD_COMPLETION_TIMEOUT;
758
759         do {
760                 status = at76_get_cmd_status(priv->udev, cmd);
761                 if (status < 0) {
762                         wiphy_err(priv->hw->wiphy,
763                                   "at76_get_cmd_status failed: %d\n",
764                                   status);
765                         break;
766                 }
767
768                 at76_dbg(DBG_WAIT_COMPLETE,
769                          "%s: Waiting on cmd %d, status = %d (%s)",
770                          wiphy_name(priv->hw->wiphy), cmd, status,
771                          at76_get_cmd_status_string(status));
772
773                 if (status != CMD_STATUS_IN_PROGRESS
774                     && status != CMD_STATUS_IDLE)
775                         break;
776
777                 schedule_timeout_interruptible(HZ / 10);        /* 100 ms */
778                 if (time_after(jiffies, timeout)) {
779                         wiphy_err(priv->hw->wiphy,
780                                   "completion timeout for command %d\n", cmd);
781                         status = -ETIMEDOUT;
782                         break;
783                 }
784         } while (1);
785
786         return status;
787 }
788
789 static int at76_set_mib(struct at76_priv *priv, struct set_mib_buffer *buf)
790 {
791         int ret;
792
793         ret = at76_set_card_command(priv->udev, CMD_SET_MIB, buf,
794                                     offsetof(struct set_mib_buffer,
795                                              data) + buf->size);
796         if (ret < 0)
797                 return ret;
798
799         ret = at76_wait_completion(priv, CMD_SET_MIB);
800         if (ret != CMD_STATUS_COMPLETE) {
801                 wiphy_info(priv->hw->wiphy,
802                            "set_mib: at76_wait_completion failed with %d\n",
803                            ret);
804                 ret = -EIO;
805         }
806
807         return ret;
808 }
809
810 /* Return < 0 on error, == 0 if no command sent, == 1 if cmd sent */
811 static int at76_set_radio(struct at76_priv *priv, int enable)
812 {
813         int ret;
814         int cmd;
815
816         if (priv->radio_on == enable)
817                 return 0;
818
819         cmd = enable ? CMD_RADIO_ON : CMD_RADIO_OFF;
820
821         ret = at76_set_card_command(priv->udev, cmd, NULL, 0);
822         if (ret < 0)
823                 wiphy_err(priv->hw->wiphy,
824                           "at76_set_card_command(%d) failed: %d\n", cmd, ret);
825         else
826                 ret = 1;
827
828         priv->radio_on = enable;
829         return ret;
830 }
831
832 /* Set current power save mode (AT76_PM_OFF/AT76_PM_ON/AT76_PM_SMART) */
833 static int at76_set_pm_mode(struct at76_priv *priv)
834 {
835         int ret = 0;
836
837         priv->mib_buf.type = MIB_MAC_MGMT;
838         priv->mib_buf.size = 1;
839         priv->mib_buf.index = offsetof(struct mib_mac_mgmt, power_mgmt_mode);
840         priv->mib_buf.data.byte = priv->pm_mode;
841
842         ret = at76_set_mib(priv, &priv->mib_buf);
843         if (ret < 0)
844                 wiphy_err(priv->hw->wiphy, "set_mib (pm_mode) failed: %d\n",
845                           ret);
846
847         return ret;
848 }
849
850 static int at76_set_preamble(struct at76_priv *priv, u8 type)
851 {
852         int ret = 0;
853
854         priv->mib_buf.type = MIB_LOCAL;
855         priv->mib_buf.size = 1;
856         priv->mib_buf.index = offsetof(struct mib_local, preamble_type);
857         priv->mib_buf.data.byte = type;
858
859         ret = at76_set_mib(priv, &priv->mib_buf);
860         if (ret < 0)
861                 wiphy_err(priv->hw->wiphy, "set_mib (preamble) failed: %d\n",
862                           ret);
863
864         return ret;
865 }
866
867 static int at76_set_frag(struct at76_priv *priv, u16 size)
868 {
869         int ret = 0;
870
871         priv->mib_buf.type = MIB_MAC;
872         priv->mib_buf.size = 2;
873         priv->mib_buf.index = offsetof(struct mib_mac, frag_threshold);
874         priv->mib_buf.data.word = cpu_to_le16(size);
875
876         ret = at76_set_mib(priv, &priv->mib_buf);
877         if (ret < 0)
878                 wiphy_err(priv->hw->wiphy,
879                           "set_mib (frag threshold) failed: %d\n", ret);
880
881         return ret;
882 }
883
884 static int at76_set_rts(struct at76_priv *priv, u16 size)
885 {
886         int ret = 0;
887
888         priv->mib_buf.type = MIB_MAC;
889         priv->mib_buf.size = 2;
890         priv->mib_buf.index = offsetof(struct mib_mac, rts_threshold);
891         priv->mib_buf.data.word = cpu_to_le16(size);
892
893         ret = at76_set_mib(priv, &priv->mib_buf);
894         if (ret < 0)
895                 wiphy_err(priv->hw->wiphy, "set_mib (rts) failed: %d\n", ret);
896
897         return ret;
898 }
899
900 static int at76_set_autorate_fallback(struct at76_priv *priv, int onoff)
901 {
902         int ret = 0;
903
904         priv->mib_buf.type = MIB_LOCAL;
905         priv->mib_buf.size = 1;
906         priv->mib_buf.index = offsetof(struct mib_local, txautorate_fallback);
907         priv->mib_buf.data.byte = onoff;
908
909         ret = at76_set_mib(priv, &priv->mib_buf);
910         if (ret < 0)
911                 wiphy_err(priv->hw->wiphy,
912                           "set_mib (autorate fallback) failed: %d\n", ret);
913
914         return ret;
915 }
916
917 static void at76_dump_mib_mac_addr(struct at76_priv *priv)
918 {
919         int i;
920         int ret;
921         struct mib_mac_addr *m = kmalloc(sizeof(struct mib_mac_addr),
922                                          GFP_KERNEL);
923
924         if (!m)
925                 return;
926
927         ret = at76_get_mib(priv->udev, MIB_MAC_ADDR, m,
928                            sizeof(struct mib_mac_addr));
929         if (ret < 0) {
930                 wiphy_err(priv->hw->wiphy,
931                           "at76_get_mib (MAC_ADDR) failed: %d\n", ret);
932                 goto exit;
933         }
934
935         at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: mac_addr %pM res 0x%x 0x%x",
936                  wiphy_name(priv->hw->wiphy),
937                  m->mac_addr, m->res[0], m->res[1]);
938         for (i = 0; i < ARRAY_SIZE(m->group_addr); i++)
939                 at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: group addr %d: %pM, "
940                          "status %d", wiphy_name(priv->hw->wiphy), i,
941                          m->group_addr[i], m->group_addr_status[i]);
942 exit:
943         kfree(m);
944 }
945
946 static void at76_dump_mib_mac_wep(struct at76_priv *priv)
947 {
948         int i;
949         int ret;
950         int key_len;
951         struct mib_mac_wep *m = kmalloc(sizeof(struct mib_mac_wep), GFP_KERNEL);
952
953         if (!m)
954                 return;
955
956         ret = at76_get_mib(priv->udev, MIB_MAC_WEP, m,
957                            sizeof(struct mib_mac_wep));
958         if (ret < 0) {
959                 wiphy_err(priv->hw->wiphy,
960                           "at76_get_mib (MAC_WEP) failed: %d\n", ret);
961                 goto exit;
962         }
963
964         at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: priv_invoked %u def_key_id %u "
965                  "key_len %u excl_unencr %u wep_icv_err %u wep_excluded %u "
966                  "encr_level %u key %d", wiphy_name(priv->hw->wiphy),
967                  m->privacy_invoked, m->wep_default_key_id,
968                  m->wep_key_mapping_len, m->exclude_unencrypted,
969                  le32_to_cpu(m->wep_icv_error_count),
970                  le32_to_cpu(m->wep_excluded_count), m->encryption_level,
971                  m->wep_default_key_id);
972
973         key_len = (m->encryption_level == 1) ?
974             WEP_SMALL_KEY_LEN : WEP_LARGE_KEY_LEN;
975
976         for (i = 0; i < WEP_KEYS; i++)
977                 at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: key %d: %*phD",
978                          wiphy_name(priv->hw->wiphy), i,
979                          key_len, m->wep_default_keyvalue[i]);
980 exit:
981         kfree(m);
982 }
983
984 static void at76_dump_mib_mac_mgmt(struct at76_priv *priv)
985 {
986         int ret;
987         struct mib_mac_mgmt *m = kmalloc(sizeof(struct mib_mac_mgmt),
988                                          GFP_KERNEL);
989
990         if (!m)
991                 return;
992
993         ret = at76_get_mib(priv->udev, MIB_MAC_MGMT, m,
994                            sizeof(struct mib_mac_mgmt));
995         if (ret < 0) {
996                 wiphy_err(priv->hw->wiphy,
997                           "at76_get_mib (MAC_MGMT) failed: %d\n", ret);
998                 goto exit;
999         }
1000
1001         at76_dbg(DBG_MIB, "%s: MIB MAC_MGMT: beacon_period %d CFP_max_duration "
1002                  "%d medium_occupancy_limit %d station_id 0x%x ATIM_window %d "
1003                  "CFP_mode %d privacy_opt_impl %d DTIM_period %d CFP_period %d "
1004                  "current_bssid %pM current_essid %*phD current_bss_type %d "
1005                  "pm_mode %d ibss_change %d res %d "
1006                  "multi_domain_capability_implemented %d "
1007                  "international_roaming %d country_string %.3s",
1008                  wiphy_name(priv->hw->wiphy), le16_to_cpu(m->beacon_period),
1009                  le16_to_cpu(m->CFP_max_duration),
1010                  le16_to_cpu(m->medium_occupancy_limit),
1011                  le16_to_cpu(m->station_id), le16_to_cpu(m->ATIM_window),
1012                  m->CFP_mode, m->privacy_option_implemented, m->DTIM_period,
1013                  m->CFP_period, m->current_bssid,
1014                  IW_ESSID_MAX_SIZE, m->current_essid,
1015                  m->current_bss_type, m->power_mgmt_mode, m->ibss_change,
1016                  m->res, m->multi_domain_capability_implemented,
1017                  m->multi_domain_capability_enabled, m->country_string);
1018 exit:
1019         kfree(m);
1020 }
1021
1022 static void at76_dump_mib_mac(struct at76_priv *priv)
1023 {
1024         int ret;
1025         struct mib_mac *m = kmalloc(sizeof(struct mib_mac), GFP_KERNEL);
1026
1027         if (!m)
1028                 return;
1029
1030         ret = at76_get_mib(priv->udev, MIB_MAC, m, sizeof(struct mib_mac));
1031         if (ret < 0) {
1032                 wiphy_err(priv->hw->wiphy,
1033                           "at76_get_mib (MAC) failed: %d\n", ret);
1034                 goto exit;
1035         }
1036
1037         at76_dbg(DBG_MIB, "%s: MIB MAC: max_tx_msdu_lifetime %d "
1038                  "max_rx_lifetime %d frag_threshold %d rts_threshold %d "
1039                  "cwmin %d cwmax %d short_retry_time %d long_retry_time %d "
1040                  "scan_type %d scan_channel %d probe_delay %u "
1041                  "min_channel_time %d max_channel_time %d listen_int %d "
1042                  "desired_ssid %*phD desired_bssid %pM desired_bsstype %d",
1043                  wiphy_name(priv->hw->wiphy),
1044                  le32_to_cpu(m->max_tx_msdu_lifetime),
1045                  le32_to_cpu(m->max_rx_lifetime),
1046                  le16_to_cpu(m->frag_threshold), le16_to_cpu(m->rts_threshold),
1047                  le16_to_cpu(m->cwmin), le16_to_cpu(m->cwmax),
1048                  m->short_retry_time, m->long_retry_time, m->scan_type,
1049                  m->scan_channel, le16_to_cpu(m->probe_delay),
1050                  le16_to_cpu(m->min_channel_time),
1051                  le16_to_cpu(m->max_channel_time),
1052                  le16_to_cpu(m->listen_interval),
1053                  IW_ESSID_MAX_SIZE, m->desired_ssid,
1054                  m->desired_bssid, m->desired_bsstype);
1055 exit:
1056         kfree(m);
1057 }
1058
1059 static void at76_dump_mib_phy(struct at76_priv *priv)
1060 {
1061         int ret;
1062         struct mib_phy *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
1063
1064         if (!m)
1065                 return;
1066
1067         ret = at76_get_mib(priv->udev, MIB_PHY, m, sizeof(struct mib_phy));
1068         if (ret < 0) {
1069                 wiphy_err(priv->hw->wiphy,
1070                           "at76_get_mib (PHY) failed: %d\n", ret);
1071                 goto exit;
1072         }
1073
1074         at76_dbg(DBG_MIB, "%s: MIB PHY: ed_threshold %d slot_time %d "
1075                  "sifs_time %d preamble_length %d plcp_header_length %d "
1076                  "mpdu_max_length %d cca_mode_supported %d operation_rate_set "
1077                  "0x%x 0x%x 0x%x 0x%x channel_id %d current_cca_mode %d "
1078                  "phy_type %d current_reg_domain %d",
1079                  wiphy_name(priv->hw->wiphy), le32_to_cpu(m->ed_threshold),
1080                  le16_to_cpu(m->slot_time), le16_to_cpu(m->sifs_time),
1081                  le16_to_cpu(m->preamble_length),
1082                  le16_to_cpu(m->plcp_header_length),
1083                  le16_to_cpu(m->mpdu_max_length),
1084                  le16_to_cpu(m->cca_mode_supported), m->operation_rate_set[0],
1085                  m->operation_rate_set[1], m->operation_rate_set[2],
1086                  m->operation_rate_set[3], m->channel_id, m->current_cca_mode,
1087                  m->phy_type, m->current_reg_domain);
1088 exit:
1089         kfree(m);
1090 }
1091
1092 static void at76_dump_mib_local(struct at76_priv *priv)
1093 {
1094         int ret;
1095         struct mib_local *m = kmalloc(sizeof(*m), GFP_KERNEL);
1096
1097         if (!m)
1098                 return;
1099
1100         ret = at76_get_mib(priv->udev, MIB_LOCAL, m, sizeof(*m));
1101         if (ret < 0) {
1102                 wiphy_err(priv->hw->wiphy,
1103                           "at76_get_mib (LOCAL) failed: %d\n", ret);
1104                 goto exit;
1105         }
1106
1107         at76_dbg(DBG_MIB, "%s: MIB LOCAL: beacon_enable %d "
1108                  "txautorate_fallback %d ssid_size %d promiscuous_mode %d "
1109                  "preamble_type %d", wiphy_name(priv->hw->wiphy),
1110                  m->beacon_enable,
1111                  m->txautorate_fallback, m->ssid_size, m->promiscuous_mode,
1112                  m->preamble_type);
1113 exit:
1114         kfree(m);
1115 }
1116
1117 static void at76_dump_mib_mdomain(struct at76_priv *priv)
1118 {
1119         int ret;
1120         struct mib_mdomain *m = kmalloc(sizeof(struct mib_mdomain), GFP_KERNEL);
1121
1122         if (!m)
1123                 return;
1124
1125         ret = at76_get_mib(priv->udev, MIB_MDOMAIN, m,
1126                            sizeof(struct mib_mdomain));
1127         if (ret < 0) {
1128                 wiphy_err(priv->hw->wiphy,
1129                           "at76_get_mib (MDOMAIN) failed: %d\n", ret);
1130                 goto exit;
1131         }
1132
1133         at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: channel_list %*phD",
1134                  wiphy_name(priv->hw->wiphy),
1135                  (int)sizeof(m->channel_list), m->channel_list);
1136
1137         at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: tx_powerlevel %*phD",
1138                  wiphy_name(priv->hw->wiphy),
1139                  (int)sizeof(m->tx_powerlevel), m->tx_powerlevel);
1140 exit:
1141         kfree(m);
1142 }
1143
1144 /* Enable monitor mode */
1145 static int at76_start_monitor(struct at76_priv *priv)
1146 {
1147         struct at76_req_scan scan;
1148         int ret;
1149
1150         memset(&scan, 0, sizeof(struct at76_req_scan));
1151         memset(scan.bssid, 0xff, ETH_ALEN);
1152
1153         scan.channel = priv->channel;
1154         scan.scan_type = SCAN_TYPE_PASSIVE;
1155         scan.international_scan = 0;
1156         scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
1157         scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
1158         scan.probe_delay = cpu_to_le16(0);
1159
1160         ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
1161         if (ret >= 0)
1162                 ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1163
1164         return ret;
1165 }
1166
1167 /* Calculate padding from txbuf->wlength (which excludes the USB TX header),
1168    likely to compensate a flaw in the AT76C503A USB part ... */
1169 static inline int at76_calc_padding(int wlen)
1170 {
1171         /* add the USB TX header */
1172         wlen += AT76_TX_HDRLEN;
1173
1174         wlen = wlen % 64;
1175
1176         if (wlen < 50)
1177                 return 50 - wlen;
1178
1179         if (wlen >= 61)
1180                 return 64 + 50 - wlen;
1181
1182         return 0;
1183 }
1184
1185 static void at76_rx_callback(struct urb *urb)
1186 {
1187         struct at76_priv *priv = urb->context;
1188
1189         priv->rx_tasklet.data = (unsigned long)urb;
1190         tasklet_schedule(&priv->rx_tasklet);
1191 }
1192
1193 static int at76_submit_rx_urb(struct at76_priv *priv)
1194 {
1195         int ret;
1196         int size;
1197         struct sk_buff *skb = priv->rx_skb;
1198
1199         if (!priv->rx_urb) {
1200                 wiphy_err(priv->hw->wiphy, "%s: priv->rx_urb is NULL\n",
1201                           __func__);
1202                 return -EFAULT;
1203         }
1204
1205         if (!skb) {
1206                 skb = dev_alloc_skb(sizeof(struct at76_rx_buffer));
1207                 if (!skb) {
1208                         wiphy_err(priv->hw->wiphy,
1209                                   "cannot allocate rx skbuff\n");
1210                         ret = -ENOMEM;
1211                         goto exit;
1212                 }
1213                 priv->rx_skb = skb;
1214         } else {
1215                 skb_push(skb, skb_headroom(skb));
1216                 skb_trim(skb, 0);
1217         }
1218
1219         size = skb_tailroom(skb);
1220         usb_fill_bulk_urb(priv->rx_urb, priv->udev, priv->rx_pipe,
1221                           skb_put(skb, size), size, at76_rx_callback, priv);
1222         ret = usb_submit_urb(priv->rx_urb, GFP_ATOMIC);
1223         if (ret < 0) {
1224                 if (ret == -ENODEV)
1225                         at76_dbg(DBG_DEVSTART,
1226                                  "usb_submit_urb returned -ENODEV");
1227                 else
1228                         wiphy_err(priv->hw->wiphy,
1229                                   "rx, usb_submit_urb failed: %d\n", ret);
1230         }
1231
1232 exit:
1233         if (ret < 0 && ret != -ENODEV)
1234                 wiphy_err(priv->hw->wiphy,
1235                           "cannot submit rx urb - please unload the driver and/or power cycle the device\n");
1236
1237         return ret;
1238 }
1239
1240 /* Download external firmware */
1241 static int at76_load_external_fw(struct usb_device *udev, struct fwentry *fwe)
1242 {
1243         int ret;
1244         int op_mode;
1245         int blockno = 0;
1246         int bsize;
1247         u8 *block;
1248         u8 *buf = fwe->extfw;
1249         int size = fwe->extfw_size;
1250
1251         if (!buf || !size)
1252                 return -ENOENT;
1253
1254         op_mode = at76_get_op_mode(udev);
1255         at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
1256
1257         if (op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
1258                 dev_printk(KERN_ERR, &udev->dev, "unexpected opmode %d\n",
1259                            op_mode);
1260                 return -EINVAL;
1261         }
1262
1263         block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
1264         if (!block)
1265                 return -ENOMEM;
1266
1267         at76_dbg(DBG_DEVSTART, "downloading external firmware");
1268
1269         /* for fw >= 0.100, the device needs an extra empty block */
1270         do {
1271                 bsize = min_t(int, size, FW_BLOCK_SIZE);
1272                 memcpy(block, buf, bsize);
1273                 at76_dbg(DBG_DEVSTART,
1274                          "ext fw, size left = %5d, bsize = %4d, blockno = %2d",
1275                          size, bsize, blockno);
1276                 ret = at76_load_ext_fw_block(udev, blockno, block, bsize);
1277                 if (ret != bsize) {
1278                         dev_printk(KERN_ERR, &udev->dev,
1279                                    "loading %dth firmware block failed: %d\n",
1280                                    blockno, ret);
1281                         goto exit;
1282                 }
1283                 buf += bsize;
1284                 size -= bsize;
1285                 blockno++;
1286         } while (bsize > 0);
1287
1288         if (at76_is_505a(fwe->board_type)) {
1289                 at76_dbg(DBG_DEVSTART, "200 ms delay for 505a");
1290                 schedule_timeout_interruptible(HZ / 5 + 1);
1291         }
1292
1293 exit:
1294         kfree(block);
1295         if (ret < 0)
1296                 dev_printk(KERN_ERR, &udev->dev,
1297                            "downloading external firmware failed: %d\n", ret);
1298         return ret;
1299 }
1300
1301 /* Download internal firmware */
1302 static int at76_load_internal_fw(struct usb_device *udev, struct fwentry *fwe)
1303 {
1304         int ret;
1305         int need_remap = !at76_is_505a(fwe->board_type);
1306
1307         ret = at76_usbdfu_download(udev, fwe->intfw, fwe->intfw_size,
1308                                    need_remap ? 0 : 2 * HZ);
1309
1310         if (ret < 0) {
1311                 dev_printk(KERN_ERR, &udev->dev,
1312                            "downloading internal fw failed with %d\n", ret);
1313                 goto exit;
1314         }
1315
1316         at76_dbg(DBG_DEVSTART, "sending REMAP");
1317
1318         /* no REMAP for 505A (see SF driver) */
1319         if (need_remap) {
1320                 ret = at76_remap(udev);
1321                 if (ret < 0) {
1322                         dev_printk(KERN_ERR, &udev->dev,
1323                                    "sending REMAP failed with %d\n", ret);
1324                         goto exit;
1325                 }
1326         }
1327
1328         at76_dbg(DBG_DEVSTART, "sleeping for 2 seconds");
1329         schedule_timeout_interruptible(2 * HZ + 1);
1330         usb_reset_device(udev);
1331
1332 exit:
1333         return ret;
1334 }
1335
1336 static int at76_startup_device(struct at76_priv *priv)
1337 {
1338         struct at76_card_config *ccfg = &priv->card_config;
1339         int ret;
1340
1341         at76_dbg(DBG_PARAMS,
1342                  "%s param: ssid %.*s (%*phD) mode %s ch %d wep %s key %d "
1343                  "keylen %d", wiphy_name(priv->hw->wiphy), priv->essid_size,
1344                  priv->essid, IW_ESSID_MAX_SIZE, priv->essid,
1345                  priv->iw_mode == IW_MODE_ADHOC ? "adhoc" : "infra",
1346                  priv->channel, priv->wep_enabled ? "enabled" : "disabled",
1347                  priv->wep_key_id, priv->wep_keys_len[priv->wep_key_id]);
1348         at76_dbg(DBG_PARAMS,
1349                  "%s param: preamble %s rts %d retry %d frag %d "
1350                  "txrate %s auth_mode %d", wiphy_name(priv->hw->wiphy),
1351                  preambles[priv->preamble_type], priv->rts_threshold,
1352                  priv->short_retry_limit, priv->frag_threshold,
1353                  priv->txrate == TX_RATE_1MBIT ? "1MBit" : priv->txrate ==
1354                  TX_RATE_2MBIT ? "2MBit" : priv->txrate ==
1355                  TX_RATE_5_5MBIT ? "5.5MBit" : priv->txrate ==
1356                  TX_RATE_11MBIT ? "11MBit" : priv->txrate ==
1357                  TX_RATE_AUTO ? "auto" : "<invalid>", priv->auth_mode);
1358         at76_dbg(DBG_PARAMS,
1359                  "%s param: pm_mode %d pm_period %d auth_mode %s "
1360                  "scan_times %d %d scan_mode %s",
1361                  wiphy_name(priv->hw->wiphy), priv->pm_mode, priv->pm_period,
1362                  priv->auth_mode == WLAN_AUTH_OPEN ? "open" : "shared_secret",
1363                  priv->scan_min_time, priv->scan_max_time,
1364                  priv->scan_mode == SCAN_TYPE_ACTIVE ? "active" : "passive");
1365
1366         memset(ccfg, 0, sizeof(struct at76_card_config));
1367         ccfg->promiscuous_mode = 0;
1368         ccfg->short_retry_limit = priv->short_retry_limit;
1369
1370         if (priv->wep_enabled) {
1371                 if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
1372                         ccfg->encryption_type = 2;
1373                 else
1374                         ccfg->encryption_type = 1;
1375
1376                 /* jal: always exclude unencrypted if WEP is active */
1377                 ccfg->exclude_unencrypted = 1;
1378         } else {
1379                 ccfg->exclude_unencrypted = 0;
1380                 ccfg->encryption_type = 0;
1381         }
1382
1383         ccfg->rts_threshold = cpu_to_le16(priv->rts_threshold);
1384         ccfg->fragmentation_threshold = cpu_to_le16(priv->frag_threshold);
1385
1386         memcpy(ccfg->basic_rate_set, hw_rates, 4);
1387         /* jal: really needed, we do a set_mib for autorate later ??? */
1388         ccfg->auto_rate_fallback = (priv->txrate == TX_RATE_AUTO ? 1 : 0);
1389         ccfg->channel = priv->channel;
1390         ccfg->privacy_invoked = priv->wep_enabled;
1391         memcpy(ccfg->current_ssid, priv->essid, IW_ESSID_MAX_SIZE);
1392         ccfg->ssid_len = priv->essid_size;
1393
1394         ccfg->wep_default_key_id = priv->wep_key_id;
1395         memcpy(ccfg->wep_default_key_value, priv->wep_keys,
1396                sizeof(priv->wep_keys));
1397
1398         ccfg->short_preamble = priv->preamble_type;
1399         ccfg->beacon_period = cpu_to_le16(priv->beacon_period);
1400
1401         ret = at76_set_card_command(priv->udev, CMD_STARTUP, &priv->card_config,
1402                                     sizeof(struct at76_card_config));
1403         if (ret < 0) {
1404                 wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
1405                           ret);
1406                 return ret;
1407         }
1408
1409         at76_wait_completion(priv, CMD_STARTUP);
1410
1411         /* remove BSSID from previous run */
1412         memset(priv->bssid, 0, ETH_ALEN);
1413
1414         if (at76_set_radio(priv, 1) == 1)
1415                 at76_wait_completion(priv, CMD_RADIO_ON);
1416
1417         ret = at76_set_preamble(priv, priv->preamble_type);
1418         if (ret < 0)
1419                 return ret;
1420
1421         ret = at76_set_frag(priv, priv->frag_threshold);
1422         if (ret < 0)
1423                 return ret;
1424
1425         ret = at76_set_rts(priv, priv->rts_threshold);
1426         if (ret < 0)
1427                 return ret;
1428
1429         ret = at76_set_autorate_fallback(priv,
1430                                          priv->txrate == TX_RATE_AUTO ? 1 : 0);
1431         if (ret < 0)
1432                 return ret;
1433
1434         ret = at76_set_pm_mode(priv);
1435         if (ret < 0)
1436                 return ret;
1437
1438         if (at76_debug & DBG_MIB) {
1439                 at76_dump_mib_mac(priv);
1440                 at76_dump_mib_mac_addr(priv);
1441                 at76_dump_mib_mac_mgmt(priv);
1442                 at76_dump_mib_mac_wep(priv);
1443                 at76_dump_mib_mdomain(priv);
1444                 at76_dump_mib_phy(priv);
1445                 at76_dump_mib_local(priv);
1446         }
1447
1448         return 0;
1449 }
1450
1451 /* Enable or disable promiscuous mode */
1452 static void at76_work_set_promisc(struct work_struct *work)
1453 {
1454         struct at76_priv *priv = container_of(work, struct at76_priv,
1455                                               work_set_promisc);
1456         int ret = 0;
1457
1458         if (priv->device_unplugged)
1459                 return;
1460
1461         mutex_lock(&priv->mtx);
1462
1463         priv->mib_buf.type = MIB_LOCAL;
1464         priv->mib_buf.size = 1;
1465         priv->mib_buf.index = offsetof(struct mib_local, promiscuous_mode);
1466         priv->mib_buf.data.byte = priv->promisc ? 1 : 0;
1467
1468         ret = at76_set_mib(priv, &priv->mib_buf);
1469         if (ret < 0)
1470                 wiphy_err(priv->hw->wiphy,
1471                           "set_mib (promiscuous_mode) failed: %d\n", ret);
1472
1473         mutex_unlock(&priv->mtx);
1474 }
1475
1476 /* Submit Rx urb back to the device */
1477 static void at76_work_submit_rx(struct work_struct *work)
1478 {
1479         struct at76_priv *priv = container_of(work, struct at76_priv,
1480                                               work_submit_rx);
1481
1482         mutex_lock(&priv->mtx);
1483         at76_submit_rx_urb(priv);
1484         mutex_unlock(&priv->mtx);
1485 }
1486
1487 static void at76_rx_tasklet(unsigned long param)
1488 {
1489         struct urb *urb = (struct urb *)param;
1490         struct at76_priv *priv = urb->context;
1491         struct at76_rx_buffer *buf;
1492         struct ieee80211_rx_status rx_status = { 0 };
1493
1494         if (priv->device_unplugged) {
1495                 at76_dbg(DBG_DEVSTART, "device unplugged");
1496                 at76_dbg(DBG_DEVSTART, "urb status %d", urb->status);
1497                 return;
1498         }
1499
1500         if (!priv->rx_skb || !priv->rx_skb->data)
1501                 return;
1502
1503         buf = (struct at76_rx_buffer *)priv->rx_skb->data;
1504
1505         if (urb->status != 0) {
1506                 if (urb->status != -ENOENT && urb->status != -ECONNRESET)
1507                         at76_dbg(DBG_URB,
1508                                  "%s %s: - nonzero Rx bulk status received: %d",
1509                                  __func__, wiphy_name(priv->hw->wiphy),
1510                                  urb->status);
1511                 return;
1512         }
1513
1514         at76_dbg(DBG_RX_ATMEL_HDR,
1515                  "%s: rx frame: rate %d rssi %d noise %d link %d",
1516                  wiphy_name(priv->hw->wiphy), buf->rx_rate, buf->rssi,
1517                  buf->noise_level, buf->link_quality);
1518
1519         skb_pull(priv->rx_skb, AT76_RX_HDRLEN);
1520         skb_trim(priv->rx_skb, le16_to_cpu(buf->wlength));
1521         at76_dbg_dump(DBG_RX_DATA, priv->rx_skb->data,
1522                       priv->rx_skb->len, "RX: len=%d", priv->rx_skb->len);
1523
1524         rx_status.signal = buf->rssi;
1525         rx_status.flag |= RX_FLAG_DECRYPTED;
1526         rx_status.flag |= RX_FLAG_IV_STRIPPED;
1527
1528         at76_dbg(DBG_MAC80211, "calling ieee80211_rx_irqsafe(): %d/%d",
1529                  priv->rx_skb->len, priv->rx_skb->data_len);
1530         memcpy(IEEE80211_SKB_RXCB(priv->rx_skb), &rx_status, sizeof(rx_status));
1531         ieee80211_rx_irqsafe(priv->hw, priv->rx_skb);
1532
1533         /* Use a new skb for the next receive */
1534         priv->rx_skb = NULL;
1535
1536         at76_submit_rx_urb(priv);
1537 }
1538
1539 /* Load firmware into kernel memory and parse it */
1540 static struct fwentry *at76_load_firmware(struct usb_device *udev,
1541                                           enum board_type board_type)
1542 {
1543         int ret;
1544         char *str;
1545         struct at76_fw_header *fwh;
1546         struct fwentry *fwe = &firmwares[board_type];
1547
1548         mutex_lock(&fw_mutex);
1549
1550         if (fwe->loaded) {
1551                 at76_dbg(DBG_FW, "re-using previously loaded fw");
1552                 goto exit;
1553         }
1554
1555         at76_dbg(DBG_FW, "downloading firmware %s", fwe->fwname);
1556         ret = request_firmware(&fwe->fw, fwe->fwname, &udev->dev);
1557         if (ret < 0) {
1558                 dev_printk(KERN_ERR, &udev->dev, "firmware %s not found!\n",
1559                            fwe->fwname);
1560                 dev_printk(KERN_ERR, &udev->dev,
1561                            "you may need to download the firmware from "
1562                            "http://developer.berlios.de/projects/at76c503a/\n");
1563                 goto exit;
1564         }
1565
1566         at76_dbg(DBG_FW, "got it.");
1567         fwh = (struct at76_fw_header *)(fwe->fw->data);
1568
1569         if (fwe->fw->size <= sizeof(*fwh)) {
1570                 dev_printk(KERN_ERR, &udev->dev,
1571                            "firmware is too short (0x%zx)\n", fwe->fw->size);
1572                 goto exit;
1573         }
1574
1575         /* CRC currently not checked */
1576         fwe->board_type = le32_to_cpu(fwh->board_type);
1577         if (fwe->board_type != board_type) {
1578                 dev_printk(KERN_ERR, &udev->dev,
1579                            "board type mismatch, requested %u, got %u\n",
1580                            board_type, fwe->board_type);
1581                 goto exit;
1582         }
1583
1584         fwe->fw_version.major = fwh->major;
1585         fwe->fw_version.minor = fwh->minor;
1586         fwe->fw_version.patch = fwh->patch;
1587         fwe->fw_version.build = fwh->build;
1588
1589         str = (char *)fwh + le32_to_cpu(fwh->str_offset);
1590         fwe->intfw = (u8 *)fwh + le32_to_cpu(fwh->int_fw_offset);
1591         fwe->intfw_size = le32_to_cpu(fwh->int_fw_len);
1592         fwe->extfw = (u8 *)fwh + le32_to_cpu(fwh->ext_fw_offset);
1593         fwe->extfw_size = le32_to_cpu(fwh->ext_fw_len);
1594
1595         fwe->loaded = 1;
1596
1597         dev_printk(KERN_DEBUG, &udev->dev,
1598                    "using firmware %s (version %d.%d.%d-%d)\n",
1599                    fwe->fwname, fwh->major, fwh->minor, fwh->patch, fwh->build);
1600
1601         at76_dbg(DBG_DEVSTART, "board %u, int %d:%d, ext %d:%d", board_type,
1602                  le32_to_cpu(fwh->int_fw_offset), le32_to_cpu(fwh->int_fw_len),
1603                  le32_to_cpu(fwh->ext_fw_offset), le32_to_cpu(fwh->ext_fw_len));
1604         at76_dbg(DBG_DEVSTART, "firmware id %s", str);
1605
1606 exit:
1607         mutex_unlock(&fw_mutex);
1608
1609         if (fwe->loaded)
1610                 return fwe;
1611         else
1612                 return NULL;
1613 }
1614
1615 static int at76_join(struct at76_priv *priv)
1616 {
1617         struct at76_req_join join;
1618         int ret;
1619
1620         memset(&join, 0, sizeof(struct at76_req_join));
1621         memcpy(join.essid, priv->essid, priv->essid_size);
1622         join.essid_size = priv->essid_size;
1623         memcpy(join.bssid, priv->bssid, ETH_ALEN);
1624         join.bss_type = INFRASTRUCTURE_MODE;
1625         join.channel = priv->channel;
1626         join.timeout = cpu_to_le16(2000);
1627
1628         at76_dbg(DBG_MAC80211, "%s: sending CMD_JOIN", __func__);
1629         ret = at76_set_card_command(priv->udev, CMD_JOIN, &join,
1630                                     sizeof(struct at76_req_join));
1631
1632         if (ret < 0) {
1633                 wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
1634                           ret);
1635                 return 0;
1636         }
1637
1638         ret = at76_wait_completion(priv, CMD_JOIN);
1639         at76_dbg(DBG_MAC80211, "%s: CMD_JOIN returned: 0x%02x", __func__, ret);
1640         if (ret != CMD_STATUS_COMPLETE) {
1641                 wiphy_err(priv->hw->wiphy, "at76_wait_completion failed: %d\n",
1642                           ret);
1643                 return 0;
1644         }
1645
1646         at76_set_pm_mode(priv);
1647
1648         return 0;
1649 }
1650
1651 static void at76_work_join_bssid(struct work_struct *work)
1652 {
1653         struct at76_priv *priv = container_of(work, struct at76_priv,
1654                                               work_join_bssid);
1655
1656         if (priv->device_unplugged)
1657                 return;
1658
1659         mutex_lock(&priv->mtx);
1660
1661         if (is_valid_ether_addr(priv->bssid))
1662                 at76_join(priv);
1663
1664         mutex_unlock(&priv->mtx);
1665 }
1666
1667 static void at76_mac80211_tx_callback(struct urb *urb)
1668 {
1669         struct at76_priv *priv = urb->context;
1670         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(priv->tx_skb);
1671
1672         at76_dbg(DBG_MAC80211, "%s()", __func__);
1673
1674         switch (urb->status) {
1675         case 0:
1676                 /* success */
1677                 info->flags |= IEEE80211_TX_STAT_ACK;
1678                 break;
1679         case -ENOENT:
1680         case -ECONNRESET:
1681                 /* fail, urb has been unlinked */
1682                 /* FIXME: add error message */
1683                 break;
1684         default:
1685                 at76_dbg(DBG_URB, "%s - nonzero tx status received: %d",
1686                          __func__, urb->status);
1687                 break;
1688         }
1689
1690         memset(&info->status, 0, sizeof(info->status));
1691
1692         ieee80211_tx_status_irqsafe(priv->hw, priv->tx_skb);
1693
1694         priv->tx_skb = NULL;
1695
1696         ieee80211_wake_queues(priv->hw);
1697 }
1698
1699 static void at76_mac80211_tx(struct ieee80211_hw *hw,
1700                              struct ieee80211_tx_control *control,
1701                              struct sk_buff *skb)
1702 {
1703         struct at76_priv *priv = hw->priv;
1704         struct at76_tx_buffer *tx_buffer = priv->bulk_out_buffer;
1705         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1706         struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1707         int padding, submit_len, ret;
1708
1709         at76_dbg(DBG_MAC80211, "%s()", __func__);
1710
1711         if (priv->tx_urb->status == -EINPROGRESS) {
1712                 wiphy_err(priv->hw->wiphy,
1713                           "%s called while tx urb is pending\n", __func__);
1714                 dev_kfree_skb_any(skb);
1715                 return;
1716         }
1717
1718         /* The following code lines are important when the device is going to
1719          * authenticate with a new bssid. The driver must send CMD_JOIN before
1720          * an authentication frame is transmitted. For this to succeed, the
1721          * correct bssid of the AP must be known. As mac80211 does not inform
1722          * drivers about the bssid prior to the authentication process the
1723          * following workaround is necessary. If the TX frame is an
1724          * authentication frame extract the bssid and send the CMD_JOIN. */
1725         if (mgmt->frame_control & cpu_to_le16(IEEE80211_STYPE_AUTH)) {
1726                 if (!ether_addr_equal(priv->bssid, mgmt->bssid)) {
1727                         memcpy(priv->bssid, mgmt->bssid, ETH_ALEN);
1728                         ieee80211_queue_work(hw, &priv->work_join_bssid);
1729                         dev_kfree_skb_any(skb);
1730                         return;
1731                 }
1732         }
1733
1734         ieee80211_stop_queues(hw);
1735
1736         at76_ledtrig_tx_activity();     /* tell ledtrigger we send a packet */
1737
1738         WARN_ON(priv->tx_skb != NULL);
1739
1740         priv->tx_skb = skb;
1741         padding = at76_calc_padding(skb->len);
1742         submit_len = AT76_TX_HDRLEN + skb->len + padding;
1743
1744         /* setup 'Atmel' header */
1745         memset(tx_buffer, 0, sizeof(*tx_buffer));
1746         tx_buffer->padding = padding;
1747         tx_buffer->wlength = cpu_to_le16(skb->len);
1748         tx_buffer->tx_rate = ieee80211_get_tx_rate(hw, info)->hw_value;
1749         memset(tx_buffer->reserved, 0, sizeof(tx_buffer->reserved));
1750         memcpy(tx_buffer->packet, skb->data, skb->len);
1751
1752         at76_dbg(DBG_TX_DATA, "%s tx: wlen 0x%x pad 0x%x rate %d hdr",
1753                  wiphy_name(priv->hw->wiphy), le16_to_cpu(tx_buffer->wlength),
1754                  tx_buffer->padding, tx_buffer->tx_rate);
1755
1756         /* send stuff */
1757         at76_dbg_dump(DBG_TX_DATA_CONTENT, tx_buffer, submit_len,
1758                       "%s(): tx_buffer %d bytes:", __func__, submit_len);
1759         usb_fill_bulk_urb(priv->tx_urb, priv->udev, priv->tx_pipe, tx_buffer,
1760                           submit_len, at76_mac80211_tx_callback, priv);
1761         ret = usb_submit_urb(priv->tx_urb, GFP_ATOMIC);
1762         if (ret) {
1763                 wiphy_err(priv->hw->wiphy, "error in tx submit urb: %d\n", ret);
1764                 if (ret == -EINVAL)
1765                         wiphy_err(priv->hw->wiphy,
1766                                   "-EINVAL: tx urb %p hcpriv %p complete %p\n",
1767                                   priv->tx_urb,
1768                                   priv->tx_urb->hcpriv, priv->tx_urb->complete);
1769         }
1770 }
1771
1772 static int at76_mac80211_start(struct ieee80211_hw *hw)
1773 {
1774         struct at76_priv *priv = hw->priv;
1775         int ret;
1776
1777         at76_dbg(DBG_MAC80211, "%s()", __func__);
1778
1779         mutex_lock(&priv->mtx);
1780
1781         ret = at76_submit_rx_urb(priv);
1782         if (ret < 0) {
1783                 wiphy_err(priv->hw->wiphy, "open: submit_rx_urb failed: %d\n",
1784                           ret);
1785                 goto error;
1786         }
1787
1788         at76_startup_device(priv);
1789
1790         at76_start_monitor(priv);
1791
1792 error:
1793         mutex_unlock(&priv->mtx);
1794
1795         return 0;
1796 }
1797
1798 static void at76_mac80211_stop(struct ieee80211_hw *hw)
1799 {
1800         struct at76_priv *priv = hw->priv;
1801
1802         at76_dbg(DBG_MAC80211, "%s()", __func__);
1803
1804         cancel_delayed_work(&priv->dwork_hw_scan);
1805         cancel_work_sync(&priv->work_join_bssid);
1806         cancel_work_sync(&priv->work_set_promisc);
1807
1808         mutex_lock(&priv->mtx);
1809
1810         if (!priv->device_unplugged) {
1811                 /* We are called by "ifconfig ethX down", not because the
1812                  * device is not available anymore. */
1813                 at76_set_radio(priv, 0);
1814
1815                 /* We unlink rx_urb because at76_open() re-submits it.
1816                  * If unplugged, at76_delete_device() takes care of it. */
1817                 usb_kill_urb(priv->rx_urb);
1818         }
1819
1820         mutex_unlock(&priv->mtx);
1821 }
1822
1823 static int at76_add_interface(struct ieee80211_hw *hw,
1824                               struct ieee80211_vif *vif)
1825 {
1826         struct at76_priv *priv = hw->priv;
1827         int ret = 0;
1828
1829         at76_dbg(DBG_MAC80211, "%s()", __func__);
1830
1831         mutex_lock(&priv->mtx);
1832
1833         switch (vif->type) {
1834         case NL80211_IFTYPE_STATION:
1835                 priv->iw_mode = IW_MODE_INFRA;
1836                 break;
1837         default:
1838                 ret = -EOPNOTSUPP;
1839                 goto exit;
1840         }
1841
1842 exit:
1843         mutex_unlock(&priv->mtx);
1844
1845         return ret;
1846 }
1847
1848 static void at76_remove_interface(struct ieee80211_hw *hw,
1849                                   struct ieee80211_vif *vif)
1850 {
1851         at76_dbg(DBG_MAC80211, "%s()", __func__);
1852 }
1853
1854 static void at76_dwork_hw_scan(struct work_struct *work)
1855 {
1856         struct at76_priv *priv = container_of(work, struct at76_priv,
1857                                               dwork_hw_scan.work);
1858         int ret;
1859
1860         if (priv->device_unplugged)
1861                 return;
1862
1863         mutex_lock(&priv->mtx);
1864
1865         ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1866         at76_dbg(DBG_MAC80211, "%s: CMD_SCAN status 0x%02x", __func__, ret);
1867
1868         /* FIXME: add maximum time for scan to complete */
1869
1870         if (ret != CMD_STATUS_COMPLETE) {
1871                 ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
1872                                              SCAN_POLL_INTERVAL);
1873                 mutex_unlock(&priv->mtx);
1874                 return;
1875         }
1876
1877         if (is_valid_ether_addr(priv->bssid))
1878                 at76_join(priv);
1879
1880         mutex_unlock(&priv->mtx);
1881
1882         ieee80211_scan_completed(priv->hw, false);
1883
1884         ieee80211_wake_queues(priv->hw);
1885 }
1886
1887 static int at76_hw_scan(struct ieee80211_hw *hw,
1888                         struct ieee80211_vif *vif,
1889                         struct cfg80211_scan_request *req)
1890 {
1891         struct at76_priv *priv = hw->priv;
1892         struct at76_req_scan scan;
1893         u8 *ssid = NULL;
1894         int ret, len = 0;
1895
1896         at76_dbg(DBG_MAC80211, "%s():", __func__);
1897
1898         if (priv->device_unplugged)
1899                 return 0;
1900
1901         mutex_lock(&priv->mtx);
1902
1903         ieee80211_stop_queues(hw);
1904
1905         memset(&scan, 0, sizeof(struct at76_req_scan));
1906         memset(scan.bssid, 0xFF, ETH_ALEN);
1907
1908         if (req->n_ssids) {
1909                 scan.scan_type = SCAN_TYPE_ACTIVE;
1910                 ssid = req->ssids[0].ssid;
1911                 len = req->ssids[0].ssid_len;
1912         } else {
1913                 scan.scan_type = SCAN_TYPE_PASSIVE;
1914         }
1915
1916         if (len) {
1917                 memcpy(scan.essid, ssid, len);
1918                 scan.essid_size = len;
1919         }
1920
1921         scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
1922         scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
1923         scan.probe_delay = cpu_to_le16(priv->scan_min_time * 1000);
1924         scan.international_scan = 0;
1925
1926         at76_dbg(DBG_MAC80211, "%s: sending CMD_SCAN", __func__);
1927         ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
1928
1929         if (ret < 0) {
1930                 wiphy_err(priv->hw->wiphy, "CMD_SCAN failed: %d\n", ret);
1931                 goto exit;
1932         }
1933
1934         ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
1935                                      SCAN_POLL_INTERVAL);
1936
1937 exit:
1938         mutex_unlock(&priv->mtx);
1939
1940         return 0;
1941 }
1942
1943 static int at76_config(struct ieee80211_hw *hw, u32 changed)
1944 {
1945         struct at76_priv *priv = hw->priv;
1946
1947         at76_dbg(DBG_MAC80211, "%s(): channel %d",
1948                  __func__, hw->conf.channel->hw_value);
1949         at76_dbg_dump(DBG_MAC80211, priv->bssid, ETH_ALEN, "bssid:");
1950
1951         mutex_lock(&priv->mtx);
1952
1953         priv->channel = hw->conf.channel->hw_value;
1954
1955         if (is_valid_ether_addr(priv->bssid))
1956                 at76_join(priv);
1957         else
1958                 at76_start_monitor(priv);
1959
1960         mutex_unlock(&priv->mtx);
1961
1962         return 0;
1963 }
1964
1965 static void at76_bss_info_changed(struct ieee80211_hw *hw,
1966                                   struct ieee80211_vif *vif,
1967                                   struct ieee80211_bss_conf *conf,
1968                                   u32 changed)
1969 {
1970         struct at76_priv *priv = hw->priv;
1971
1972         at76_dbg(DBG_MAC80211, "%s():", __func__);
1973
1974         if (!(changed & BSS_CHANGED_BSSID))
1975                 return;
1976
1977         at76_dbg_dump(DBG_MAC80211, conf->bssid, ETH_ALEN, "bssid:");
1978
1979         mutex_lock(&priv->mtx);
1980
1981         memcpy(priv->bssid, conf->bssid, ETH_ALEN);
1982
1983         if (is_valid_ether_addr(priv->bssid))
1984                 /* mac80211 is joining a bss */
1985                 at76_join(priv);
1986
1987         mutex_unlock(&priv->mtx);
1988 }
1989
1990 /* must be atomic */
1991 static void at76_configure_filter(struct ieee80211_hw *hw,
1992                                   unsigned int changed_flags,
1993                                   unsigned int *total_flags, u64 multicast)
1994 {
1995         struct at76_priv *priv = hw->priv;
1996         int flags;
1997
1998         at76_dbg(DBG_MAC80211, "%s(): changed_flags=0x%08x "
1999                  "total_flags=0x%08x",
2000                  __func__, changed_flags, *total_flags);
2001
2002         flags = changed_flags & AT76_SUPPORTED_FILTERS;
2003         *total_flags = AT76_SUPPORTED_FILTERS;
2004
2005         /* Bail out after updating flags to prevent a WARN_ON in mac80211. */
2006         if (priv->device_unplugged)
2007                 return;
2008
2009         /* FIXME: access to priv->promisc should be protected with
2010          * priv->mtx, but it's impossible because this function needs to be
2011          * atomic */
2012
2013         if (flags && !priv->promisc) {
2014                 /* mac80211 wants us to enable promiscuous mode */
2015                 priv->promisc = 1;
2016         } else if (!flags && priv->promisc) {
2017                 /* we need to disable promiscuous mode */
2018                 priv->promisc = 0;
2019         } else
2020                 return;
2021
2022         ieee80211_queue_work(hw, &priv->work_set_promisc);
2023 }
2024
2025 static int at76_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2026                         struct ieee80211_vif *vif, struct ieee80211_sta *sta,
2027                         struct ieee80211_key_conf *key)
2028 {
2029         struct at76_priv *priv = hw->priv;
2030
2031         int i;
2032
2033         at76_dbg(DBG_MAC80211, "%s(): cmd %d key->cipher %d key->keyidx %d "
2034                  "key->keylen %d",
2035                  __func__, cmd, key->cipher, key->keyidx, key->keylen);
2036
2037         if ((key->cipher != WLAN_CIPHER_SUITE_WEP40) &&
2038             (key->cipher != WLAN_CIPHER_SUITE_WEP104))
2039                 return -EOPNOTSUPP;
2040
2041         key->hw_key_idx = key->keyidx;
2042
2043         mutex_lock(&priv->mtx);
2044
2045         switch (cmd) {
2046         case SET_KEY:
2047                 memcpy(priv->wep_keys[key->keyidx], key->key, key->keylen);
2048                 priv->wep_keys_len[key->keyidx] = key->keylen;
2049
2050                 /* FIXME: find out how to do this properly */
2051                 priv->wep_key_id = key->keyidx;
2052
2053                 break;
2054         case DISABLE_KEY:
2055         default:
2056                 priv->wep_keys_len[key->keyidx] = 0;
2057                 break;
2058         }
2059
2060         priv->wep_enabled = 0;
2061
2062         for (i = 0; i < WEP_KEYS; i++) {
2063                 if (priv->wep_keys_len[i] != 0)
2064                         priv->wep_enabled = 1;
2065         }
2066
2067         at76_startup_device(priv);
2068
2069         mutex_unlock(&priv->mtx);
2070
2071         return 0;
2072 }
2073
2074 static const struct ieee80211_ops at76_ops = {
2075         .tx = at76_mac80211_tx,
2076         .add_interface = at76_add_interface,
2077         .remove_interface = at76_remove_interface,
2078         .config = at76_config,
2079         .bss_info_changed = at76_bss_info_changed,
2080         .configure_filter = at76_configure_filter,
2081         .start = at76_mac80211_start,
2082         .stop = at76_mac80211_stop,
2083         .hw_scan = at76_hw_scan,
2084         .set_key = at76_set_key,
2085 };
2086
2087 /* Allocate network device and initialize private data */
2088 static struct at76_priv *at76_alloc_new_device(struct usb_device *udev)
2089 {
2090         struct ieee80211_hw *hw;
2091         struct at76_priv *priv;
2092
2093         hw = ieee80211_alloc_hw(sizeof(struct at76_priv), &at76_ops);
2094         if (!hw) {
2095                 printk(KERN_ERR DRIVER_NAME ": could not register"
2096                        " ieee80211_hw\n");
2097                 return NULL;
2098         }
2099
2100         priv = hw->priv;
2101         priv->hw = hw;
2102
2103         priv->udev = udev;
2104
2105         mutex_init(&priv->mtx);
2106         INIT_WORK(&priv->work_set_promisc, at76_work_set_promisc);
2107         INIT_WORK(&priv->work_submit_rx, at76_work_submit_rx);
2108         INIT_WORK(&priv->work_join_bssid, at76_work_join_bssid);
2109         INIT_DELAYED_WORK(&priv->dwork_hw_scan, at76_dwork_hw_scan);
2110
2111         tasklet_init(&priv->rx_tasklet, at76_rx_tasklet, 0);
2112
2113         priv->pm_mode = AT76_PM_OFF;
2114         priv->pm_period = 0;
2115
2116         /* unit us */
2117         priv->hw->channel_change_time = 100000;
2118
2119         return priv;
2120 }
2121
2122 static int at76_alloc_urbs(struct at76_priv *priv,
2123                            struct usb_interface *interface)
2124 {
2125         struct usb_endpoint_descriptor *endpoint, *ep_in, *ep_out;
2126         int i;
2127         int buffer_size;
2128         struct usb_host_interface *iface_desc;
2129
2130         at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2131
2132         at76_dbg(DBG_URB, "%s: NumEndpoints %d ", __func__,
2133                  interface->altsetting[0].desc.bNumEndpoints);
2134
2135         ep_in = NULL;
2136         ep_out = NULL;
2137         iface_desc = interface->cur_altsetting;
2138         for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2139                 endpoint = &iface_desc->endpoint[i].desc;
2140
2141                 at76_dbg(DBG_URB, "%s: %d. endpoint: addr 0x%x attr 0x%x",
2142                          __func__, i, endpoint->bEndpointAddress,
2143                          endpoint->bmAttributes);
2144
2145                 if (!ep_in && usb_endpoint_is_bulk_in(endpoint))
2146                         ep_in = endpoint;
2147
2148                 if (!ep_out && usb_endpoint_is_bulk_out(endpoint))
2149                         ep_out = endpoint;
2150         }
2151
2152         if (!ep_in || !ep_out) {
2153                 dev_printk(KERN_ERR, &interface->dev,
2154                            "bulk endpoints missing\n");
2155                 return -ENXIO;
2156         }
2157
2158         priv->rx_pipe = usb_rcvbulkpipe(priv->udev, ep_in->bEndpointAddress);
2159         priv->tx_pipe = usb_sndbulkpipe(priv->udev, ep_out->bEndpointAddress);
2160
2161         priv->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2162         priv->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2163         if (!priv->rx_urb || !priv->tx_urb) {
2164                 dev_printk(KERN_ERR, &interface->dev, "cannot allocate URB\n");
2165                 return -ENOMEM;
2166         }
2167
2168         buffer_size = sizeof(struct at76_tx_buffer) + MAX_PADDING_SIZE;
2169         priv->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
2170         if (!priv->bulk_out_buffer) {
2171                 dev_printk(KERN_ERR, &interface->dev,
2172                            "cannot allocate output buffer\n");
2173                 return -ENOMEM;
2174         }
2175
2176         at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2177
2178         return 0;
2179 }
2180
2181 static struct ieee80211_rate at76_rates[] = {
2182         { .bitrate = 10, .hw_value = TX_RATE_1MBIT, },
2183         { .bitrate = 20, .hw_value = TX_RATE_2MBIT, },
2184         { .bitrate = 55, .hw_value = TX_RATE_5_5MBIT, },
2185         { .bitrate = 110, .hw_value = TX_RATE_11MBIT, },
2186 };
2187
2188 static struct ieee80211_channel at76_channels[] = {
2189         { .center_freq = 2412, .hw_value = 1 },
2190         { .center_freq = 2417, .hw_value = 2 },
2191         { .center_freq = 2422, .hw_value = 3 },
2192         { .center_freq = 2427, .hw_value = 4 },
2193         { .center_freq = 2432, .hw_value = 5 },
2194         { .center_freq = 2437, .hw_value = 6 },
2195         { .center_freq = 2442, .hw_value = 7 },
2196         { .center_freq = 2447, .hw_value = 8 },
2197         { .center_freq = 2452, .hw_value = 9 },
2198         { .center_freq = 2457, .hw_value = 10 },
2199         { .center_freq = 2462, .hw_value = 11 },
2200         { .center_freq = 2467, .hw_value = 12 },
2201         { .center_freq = 2472, .hw_value = 13 },
2202         { .center_freq = 2484, .hw_value = 14 }
2203 };
2204
2205 static struct ieee80211_supported_band at76_supported_band = {
2206         .channels = at76_channels,
2207         .n_channels = ARRAY_SIZE(at76_channels),
2208         .bitrates = at76_rates,
2209         .n_bitrates = ARRAY_SIZE(at76_rates),
2210 };
2211
2212 /* Register network device and initialize the hardware */
2213 static int at76_init_new_device(struct at76_priv *priv,
2214                                 struct usb_interface *interface)
2215 {
2216         struct wiphy *wiphy;
2217         size_t len;
2218         int ret;
2219
2220         /* set up the endpoint information */
2221         /* check out the endpoints */
2222
2223         at76_dbg(DBG_DEVSTART, "USB interface: %d endpoints",
2224                  interface->cur_altsetting->desc.bNumEndpoints);
2225
2226         ret = at76_alloc_urbs(priv, interface);
2227         if (ret < 0)
2228                 goto exit;
2229
2230         /* MAC address */
2231         ret = at76_get_hw_config(priv);
2232         if (ret < 0) {
2233                 dev_printk(KERN_ERR, &interface->dev,
2234                            "cannot get MAC address\n");
2235                 goto exit;
2236         }
2237
2238         priv->domain = at76_get_reg_domain(priv->regulatory_domain);
2239
2240         priv->channel = DEF_CHANNEL;
2241         priv->iw_mode = IW_MODE_INFRA;
2242         priv->rts_threshold = DEF_RTS_THRESHOLD;
2243         priv->frag_threshold = DEF_FRAG_THRESHOLD;
2244         priv->short_retry_limit = DEF_SHORT_RETRY_LIMIT;
2245         priv->txrate = TX_RATE_AUTO;
2246         priv->preamble_type = PREAMBLE_TYPE_LONG;
2247         priv->beacon_period = 100;
2248         priv->auth_mode = WLAN_AUTH_OPEN;
2249         priv->scan_min_time = DEF_SCAN_MIN_TIME;
2250         priv->scan_max_time = DEF_SCAN_MAX_TIME;
2251         priv->scan_mode = SCAN_TYPE_ACTIVE;
2252         priv->device_unplugged = 0;
2253
2254         /* mac80211 initialisation */
2255         wiphy = priv->hw->wiphy;
2256         priv->hw->wiphy->max_scan_ssids = 1;
2257         priv->hw->wiphy->max_scan_ie_len = 0;
2258         priv->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
2259         priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &at76_supported_band;
2260         priv->hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
2261                           IEEE80211_HW_SIGNAL_UNSPEC;
2262         priv->hw->max_signal = 100;
2263
2264         SET_IEEE80211_DEV(priv->hw, &interface->dev);
2265         SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
2266
2267         len = sizeof(wiphy->fw_version);
2268         snprintf(wiphy->fw_version, len, "%d.%d.%d-%d",
2269                  priv->fw_version.major, priv->fw_version.minor,
2270                  priv->fw_version.patch, priv->fw_version.build);
2271
2272         wiphy->hw_version = priv->board_type;
2273
2274         ret = ieee80211_register_hw(priv->hw);
2275         if (ret) {
2276                 printk(KERN_ERR "cannot register mac80211 hw (status %d)!\n",
2277                        ret);
2278                 goto exit;
2279         }
2280
2281         priv->mac80211_registered = 1;
2282
2283         wiphy_info(priv->hw->wiphy, "USB %s, MAC %pM, firmware %d.%d.%d-%d\n",
2284                    dev_name(&interface->dev), priv->mac_addr,
2285                    priv->fw_version.major, priv->fw_version.minor,
2286                    priv->fw_version.patch, priv->fw_version.build);
2287         wiphy_info(priv->hw->wiphy, "regulatory domain 0x%02x: %s\n",
2288                    priv->regulatory_domain, priv->domain->name);
2289
2290 exit:
2291         return ret;
2292 }
2293
2294 static void at76_delete_device(struct at76_priv *priv)
2295 {
2296         at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2297
2298         /* The device is gone, don't bother turning it off */
2299         priv->device_unplugged = 1;
2300
2301         tasklet_kill(&priv->rx_tasklet);
2302
2303         if (priv->mac80211_registered)
2304                 ieee80211_unregister_hw(priv->hw);
2305
2306         if (priv->tx_urb) {
2307                 usb_kill_urb(priv->tx_urb);
2308                 usb_free_urb(priv->tx_urb);
2309         }
2310         if (priv->rx_urb) {
2311                 usb_kill_urb(priv->rx_urb);
2312                 usb_free_urb(priv->rx_urb);
2313         }
2314
2315         at76_dbg(DBG_PROC_ENTRY, "%s: unlinked urbs", __func__);
2316
2317         kfree(priv->bulk_out_buffer);
2318
2319         del_timer_sync(&ledtrig_tx_timer);
2320
2321         kfree_skb(priv->rx_skb);
2322
2323         usb_put_dev(priv->udev);
2324
2325         at76_dbg(DBG_PROC_ENTRY, "%s: before freeing priv/ieee80211_hw",
2326                  __func__);
2327         ieee80211_free_hw(priv->hw);
2328
2329         at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2330 }
2331
2332 static int at76_probe(struct usb_interface *interface,
2333                       const struct usb_device_id *id)
2334 {
2335         int ret;
2336         struct at76_priv *priv;
2337         struct fwentry *fwe;
2338         struct usb_device *udev;
2339         int op_mode;
2340         int need_ext_fw = 0;
2341         struct mib_fw_version fwv;
2342         int board_type = (int)id->driver_info;
2343
2344         udev = usb_get_dev(interface_to_usbdev(interface));
2345
2346         /* Load firmware into kernel memory */
2347         fwe = at76_load_firmware(udev, board_type);
2348         if (!fwe) {
2349                 ret = -ENOENT;
2350                 goto error;
2351         }
2352
2353         op_mode = at76_get_op_mode(udev);
2354
2355         at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
2356
2357         /* we get OPMODE_NONE with 2.4.23, SMC2662W-AR ???
2358            we get 204 with 2.4.23, Fiberline FL-WL240u (505A+RFMD2958) ??? */
2359
2360         if (op_mode == OPMODE_HW_CONFIG_MODE) {
2361                 dev_printk(KERN_ERR, &interface->dev,
2362                            "cannot handle a device in HW_CONFIG_MODE\n");
2363                 ret = -EBUSY;
2364                 goto error;
2365         }
2366
2367         if (op_mode != OPMODE_NORMAL_NIC_WITH_FLASH
2368             && op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
2369                 /* download internal firmware part */
2370                 dev_printk(KERN_DEBUG, &interface->dev,
2371                            "downloading internal firmware\n");
2372                 ret = at76_load_internal_fw(udev, fwe);
2373                 if (ret < 0) {
2374                         dev_printk(KERN_ERR, &interface->dev,
2375                                    "error %d downloading internal firmware\n",
2376                                    ret);
2377                         goto error;
2378                 }
2379                 usb_put_dev(udev);
2380                 return ret;
2381         }
2382
2383         /* Internal firmware already inside the device.  Get firmware
2384          * version to test if external firmware is loaded.
2385          * This works only for newer firmware, e.g. the Intersil 0.90.x
2386          * says "control timeout on ep0in" and subsequent
2387          * at76_get_op_mode() fail too :-( */
2388
2389         /* if version >= 0.100.x.y or device with built-in flash we can
2390          * query the device for the fw version */
2391         if ((fwe->fw_version.major > 0 || fwe->fw_version.minor >= 100)
2392             || (op_mode == OPMODE_NORMAL_NIC_WITH_FLASH)) {
2393                 ret = at76_get_mib(udev, MIB_FW_VERSION, &fwv, sizeof(fwv));
2394                 if (ret < 0 || (fwv.major | fwv.minor) == 0)
2395                         need_ext_fw = 1;
2396         } else
2397                 /* No way to check firmware version, reload to be sure */
2398                 need_ext_fw = 1;
2399
2400         if (need_ext_fw) {
2401                 dev_printk(KERN_DEBUG, &interface->dev,
2402                            "downloading external firmware\n");
2403
2404                 ret = at76_load_external_fw(udev, fwe);
2405                 if (ret)
2406                         goto error;
2407
2408                 /* Re-check firmware version */
2409                 ret = at76_get_mib(udev, MIB_FW_VERSION, &fwv, sizeof(fwv));
2410                 if (ret < 0) {
2411                         dev_printk(KERN_ERR, &interface->dev,
2412                                    "error %d getting firmware version\n", ret);
2413                         goto error;
2414                 }
2415         }
2416
2417         priv = at76_alloc_new_device(udev);
2418         if (!priv) {
2419                 ret = -ENOMEM;
2420                 goto error;
2421         }
2422
2423         usb_set_intfdata(interface, priv);
2424
2425         memcpy(&priv->fw_version, &fwv, sizeof(struct mib_fw_version));
2426         priv->board_type = board_type;
2427
2428         ret = at76_init_new_device(priv, interface);
2429         if (ret < 0)
2430                 at76_delete_device(priv);
2431
2432         return ret;
2433
2434 error:
2435         usb_put_dev(udev);
2436         return ret;
2437 }
2438
2439 static void at76_disconnect(struct usb_interface *interface)
2440 {
2441         struct at76_priv *priv;
2442
2443         priv = usb_get_intfdata(interface);
2444         usb_set_intfdata(interface, NULL);
2445
2446         /* Disconnect after loading internal firmware */
2447         if (!priv)
2448                 return;
2449
2450         wiphy_info(priv->hw->wiphy, "disconnecting\n");
2451         at76_delete_device(priv);
2452         dev_printk(KERN_INFO, &interface->dev, "disconnected\n");
2453 }
2454
2455 /* Structure for registering this driver with the USB subsystem */
2456 static struct usb_driver at76_driver = {
2457         .name = DRIVER_NAME,
2458         .probe = at76_probe,
2459         .disconnect = at76_disconnect,
2460         .id_table = dev_table,
2461         .disable_hub_initiated_lpm = 1,
2462 };
2463
2464 static int __init at76_mod_init(void)
2465 {
2466         int result;
2467
2468         printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " loading\n");
2469
2470         mutex_init(&fw_mutex);
2471
2472         /* register this driver with the USB subsystem */
2473         result = usb_register(&at76_driver);
2474         if (result < 0)
2475                 printk(KERN_ERR DRIVER_NAME
2476                        ": usb_register failed (status %d)\n", result);
2477
2478         led_trigger_register_simple("at76_usb-tx", &ledtrig_tx);
2479         return result;
2480 }
2481
2482 static void __exit at76_mod_exit(void)
2483 {
2484         int i;
2485
2486         printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " unloading\n");
2487         usb_deregister(&at76_driver);
2488         for (i = 0; i < ARRAY_SIZE(firmwares); i++)
2489                 release_firmware(firmwares[i].fw);
2490         led_trigger_unregister_simple(ledtrig_tx);
2491 }
2492
2493 module_param_named(debug, at76_debug, uint, 0600);
2494 MODULE_PARM_DESC(debug, "Debugging level");
2495
2496 module_init(at76_mod_init);
2497 module_exit(at76_mod_exit);
2498
2499 MODULE_AUTHOR("Oliver Kurth <oku@masqmail.cx>");
2500 MODULE_AUTHOR("Joerg Albert <joerg.albert@gmx.de>");
2501 MODULE_AUTHOR("Alex <alex@foogod.com>");
2502 MODULE_AUTHOR("Nick Jones");
2503 MODULE_AUTHOR("Balint Seeber <n0_5p4m_p13453@hotmail.com>");
2504 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
2505 MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
2506 MODULE_AUTHOR("Kalle Valo <kalle.valo@iki.fi>");
2507 MODULE_AUTHOR("Sebastian Smolorz <sesmo@gmx.net>");
2508 MODULE_DESCRIPTION(DRIVER_DESC);
2509 MODULE_LICENSE("GPL");