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