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