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