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