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