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