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