Staging: at76_usb: fix build breakage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / at76_usb / at76_usb.c
blobbf42a9861f36f48de0aa737de0b78186e84c11d8
1 /*
2 * at76c503/at76c505 USB driver
4 * Copyright (c) 2002 - 2003 Oliver Kurth
5 * Copyright (c) 2004 Joerg Albert <joerg.albert@gmx.de>
6 * Copyright (c) 2004 Nick Jones
7 * Copyright (c) 2004 Balint Seeber <n0_5p4m_p13453@hotmail.com>
8 * Copyright (c) 2007 Guido Guenther <agx@sigxcpu.org>
9 * Copyright (c) 2007 Kalle Valo <kalle.valo@iki.fi>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This file is part of the Berlios driver for WLAN USB devices based on the
17 * Atmel AT76C503A/505/505A.
19 * Some iw_handler code was taken from airo.c, (C) 1999 Benjamin Reed
21 * TODO for the mac80211 port:
22 * o adhoc support
23 * o RTS/CTS support
24 * o Power Save Mode support
25 * o support for short/long preambles
26 * o export variables through debugfs/sysfs
29 #include <linux/init.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/slab.h>
34 #include <linux/module.h>
35 #include <linux/spinlock.h>
36 #include <linux/list.h>
37 #include <linux/usb.h>
38 #include <linux/netdevice.h>
39 #include <linux/if_arp.h>
40 #include <linux/etherdevice.h>
41 #include <linux/ethtool.h>
42 #include <linux/wireless.h>
43 #include <net/iw_handler.h>
44 #include <net/ieee80211_radiotap.h>
45 #include <linux/firmware.h>
46 #include <linux/leds.h>
47 #include <net/mac80211.h>
49 #include "at76_usb.h"
51 /* Version information */
52 #define DRIVER_NAME "at76_usb"
53 #define DRIVER_VERSION "0.17"
54 #define DRIVER_DESC "Atmel at76x USB Wireless LAN Driver"
56 /* at76_debug bits */
57 #define DBG_PROGRESS 0x00000001 /* authentication/accociation */
58 #define DBG_BSS_TABLE 0x00000002 /* show BSS table after scans */
59 #define DBG_IOCTL 0x00000004 /* ioctl calls / settings */
60 #define DBG_MAC_STATE 0x00000008 /* MAC state transitions */
61 #define DBG_TX_DATA 0x00000010 /* tx header */
62 #define DBG_TX_DATA_CONTENT 0x00000020 /* tx content */
63 #define DBG_TX_MGMT 0x00000040 /* tx management */
64 #define DBG_RX_DATA 0x00000080 /* rx data header */
65 #define DBG_RX_DATA_CONTENT 0x00000100 /* rx data content */
66 #define DBG_RX_MGMT 0x00000200 /* rx mgmt frame headers */
67 #define DBG_RX_BEACON 0x00000400 /* rx beacon */
68 #define DBG_RX_CTRL 0x00000800 /* rx control */
69 #define DBG_RX_MGMT_CONTENT 0x00001000 /* rx mgmt content */
70 #define DBG_RX_FRAGS 0x00002000 /* rx data fragment handling */
71 #define DBG_DEVSTART 0x00004000 /* fw download, device start */
72 #define DBG_URB 0x00008000 /* rx urb status, ... */
73 #define DBG_RX_ATMEL_HDR 0x00010000 /* Atmel-specific Rx headers */
74 #define DBG_PROC_ENTRY 0x00020000 /* procedure entries/exits */
75 #define DBG_PM 0x00040000 /* power management settings */
76 #define DBG_BSS_MATCH 0x00080000 /* BSS match failures */
77 #define DBG_PARAMS 0x00100000 /* show configured parameters */
78 #define DBG_WAIT_COMPLETE 0x00200000 /* command completion */
79 #define DBG_RX_FRAGS_SKB 0x00400000 /* skb header of Rx fragments */
80 #define DBG_BSS_TABLE_RM 0x00800000 /* purging bss table entries */
81 #define DBG_MONITOR_MODE 0x01000000 /* monitor mode */
82 #define DBG_MIB 0x02000000 /* dump all MIBs on startup */
83 #define DBG_MGMT_TIMER 0x04000000 /* dump mgmt_timer ops */
84 #define DBG_WE_EVENTS 0x08000000 /* dump wireless events */
85 #define DBG_FW 0x10000000 /* firmware download */
86 #define DBG_DFU 0x20000000 /* device firmware upgrade */
87 #define DBG_CMD 0x40000000
88 #define DBG_MAC80211 0x80000000
90 #define DBG_DEFAULTS 0
92 /* Use our own dbg macro */
93 #define at76_dbg(bits, format, arg...) \
94 do { \
95 if (at76_debug & (bits)) \
96 printk(KERN_DEBUG DRIVER_NAME ": " format "\n" , ## arg); \
97 } while (0)
99 #define at76_dbg_dump(bits, buf, len, format, arg...) \
100 do { \
101 if (at76_debug & (bits)) { \
102 printk(KERN_DEBUG DRIVER_NAME ": " format "\n" , ## arg); \
103 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len); \
105 } while (0)
107 static int at76_debug = DBG_DEFAULTS;
109 #define FIRMWARE_IS_WPA(ver) ((ver.major == 1) && (ver.minor == 103))
111 /* Protect against concurrent firmware loading and parsing */
112 static struct mutex fw_mutex;
114 static struct fwentry firmwares[] = {
115 [0] = { "" },
116 [BOARD_503_ISL3861] = { "atmel_at76c503-i3861.bin" },
117 [BOARD_503_ISL3863] = { "atmel_at76c503-i3863.bin" },
118 [BOARD_503] = { "atmel_at76c503-rfmd.bin" },
119 [BOARD_503_ACC] = { "atmel_at76c503-rfmd-acc.bin" },
120 [BOARD_505] = { "atmel_at76c505-rfmd.bin" },
121 [BOARD_505_2958] = { "atmel_at76c505-rfmd2958.bin" },
122 [BOARD_505A] = { "atmel_at76c505a-rfmd2958.bin" },
123 [BOARD_505AMX] = { "atmel_at76c505amx-rfmd.bin" },
126 #define USB_DEVICE_DATA(__ops) .driver_info = (kernel_ulong_t)(__ops)
128 static struct usb_device_id dev_table[] = {
130 * at76c503-i3861
132 /* Generic AT76C503/3861 device */
133 { USB_DEVICE(0x03eb, 0x7603), USB_DEVICE_DATA(BOARD_503_ISL3861) },
134 /* Linksys WUSB11 v2.1/v2.6 */
135 { USB_DEVICE(0x066b, 0x2211), USB_DEVICE_DATA(BOARD_503_ISL3861) },
136 /* Netgear MA101 rev. A */
137 { USB_DEVICE(0x0864, 0x4100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
138 /* Tekram U300C / Allnet ALL0193 */
139 { USB_DEVICE(0x0b3b, 0x1612), USB_DEVICE_DATA(BOARD_503_ISL3861) },
140 /* HP HN210W J7801A */
141 { USB_DEVICE(0x03f0, 0x011c), USB_DEVICE_DATA(BOARD_503_ISL3861) },
142 /* Sitecom/Z-Com/Zyxel M4Y-750 */
143 { USB_DEVICE(0x0cde, 0x0001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
144 /* Dynalink/Askey WLL013 (intersil) */
145 { USB_DEVICE(0x069a, 0x0320), USB_DEVICE_DATA(BOARD_503_ISL3861) },
146 /* EZ connect 11Mpbs Wireless USB Adapter SMC2662W v1 */
147 { USB_DEVICE(0x0d5c, 0xa001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
148 /* BenQ AWL300 */
149 { USB_DEVICE(0x04a5, 0x9000), USB_DEVICE_DATA(BOARD_503_ISL3861) },
150 /* Addtron AWU-120, Compex WLU11 */
151 { USB_DEVICE(0x05dd, 0xff31), USB_DEVICE_DATA(BOARD_503_ISL3861) },
152 /* Intel AP310 AnyPoint II USB */
153 { USB_DEVICE(0x8086, 0x0200), USB_DEVICE_DATA(BOARD_503_ISL3861) },
154 /* Dynalink L11U */
155 { USB_DEVICE(0x0d8e, 0x7100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
156 /* Arescom WL-210, FCC id 07J-GL2411USB */
157 { USB_DEVICE(0x0d8e, 0x7110), USB_DEVICE_DATA(BOARD_503_ISL3861) },
158 /* I-O DATA WN-B11/USB */
159 { USB_DEVICE(0x04bb, 0x0919), USB_DEVICE_DATA(BOARD_503_ISL3861) },
160 /* BT Voyager 1010 */
161 { USB_DEVICE(0x069a, 0x0821), USB_DEVICE_DATA(BOARD_503_ISL3861) },
163 * at76c503-i3863
165 /* Generic AT76C503/3863 device */
166 { USB_DEVICE(0x03eb, 0x7604), USB_DEVICE_DATA(BOARD_503_ISL3863) },
167 /* Samsung SWL-2100U */
168 { USB_DEVICE(0x055d, 0xa000), USB_DEVICE_DATA(BOARD_503_ISL3863) },
170 * at76c503-rfmd
172 /* Generic AT76C503/RFMD device */
173 { USB_DEVICE(0x03eb, 0x7605), USB_DEVICE_DATA(BOARD_503) },
174 /* Dynalink/Askey WLL013 (rfmd) */
175 { USB_DEVICE(0x069a, 0x0321), USB_DEVICE_DATA(BOARD_503) },
176 /* Linksys WUSB11 v2.6 */
177 { USB_DEVICE(0x077b, 0x2219), USB_DEVICE_DATA(BOARD_503) },
178 /* Network Everywhere NWU11B */
179 { USB_DEVICE(0x077b, 0x2227), USB_DEVICE_DATA(BOARD_503) },
180 /* Netgear MA101 rev. B */
181 { USB_DEVICE(0x0864, 0x4102), USB_DEVICE_DATA(BOARD_503) },
182 /* D-Link DWL-120 rev. E */
183 { USB_DEVICE(0x2001, 0x3200), USB_DEVICE_DATA(BOARD_503) },
184 /* Actiontec 802UAT1, HWU01150-01UK */
185 { USB_DEVICE(0x1668, 0x7605), USB_DEVICE_DATA(BOARD_503) },
186 /* AirVast W-Buddie WN210 */
187 { USB_DEVICE(0x03eb, 0x4102), USB_DEVICE_DATA(BOARD_503) },
188 /* Dick Smith Electronics XH1153 802.11b USB adapter */
189 { USB_DEVICE(0x1371, 0x5743), USB_DEVICE_DATA(BOARD_503) },
190 /* CNet CNUSB611 */
191 { USB_DEVICE(0x1371, 0x0001), USB_DEVICE_DATA(BOARD_503) },
192 /* FiberLine FL-WL200U */
193 { USB_DEVICE(0x1371, 0x0002), USB_DEVICE_DATA(BOARD_503) },
194 /* BenQ AWL400 USB stick */
195 { USB_DEVICE(0x04a5, 0x9001), USB_DEVICE_DATA(BOARD_503) },
196 /* 3Com 3CRSHEW696 */
197 { USB_DEVICE(0x0506, 0x0a01), USB_DEVICE_DATA(BOARD_503) },
198 /* Siemens Santis ADSL WLAN USB adapter WLL 013 */
199 { USB_DEVICE(0x0681, 0x001b), USB_DEVICE_DATA(BOARD_503) },
200 /* Belkin F5D6050, version 2 */
201 { USB_DEVICE(0x050d, 0x0050), USB_DEVICE_DATA(BOARD_503) },
202 /* iBlitzz, BWU613 (not *B or *SB) */
203 { USB_DEVICE(0x07b8, 0xb000), USB_DEVICE_DATA(BOARD_503) },
204 /* Gigabyte GN-WLBM101 */
205 { USB_DEVICE(0x1044, 0x8003), USB_DEVICE_DATA(BOARD_503) },
206 /* Planex GW-US11S */
207 { USB_DEVICE(0x2019, 0x3220), USB_DEVICE_DATA(BOARD_503) },
208 /* Internal WLAN adapter in h5[4,5]xx series iPAQs */
209 { USB_DEVICE(0x049f, 0x0032), USB_DEVICE_DATA(BOARD_503) },
210 /* Corega Wireless LAN USB-11 mini */
211 { USB_DEVICE(0x07aa, 0x0011), USB_DEVICE_DATA(BOARD_503) },
212 /* Corega Wireless LAN USB-11 mini2 */
213 { USB_DEVICE(0x07aa, 0x0018), USB_DEVICE_DATA(BOARD_503) },
214 /* Uniden PCW100 */
215 { USB_DEVICE(0x05dd, 0xff35), USB_DEVICE_DATA(BOARD_503) },
217 * at76c503-rfmd-acc
219 /* SMC2664W */
220 { USB_DEVICE(0x083a, 0x3501), USB_DEVICE_DATA(BOARD_503_ACC) },
221 /* Belkin F5D6050, SMC2662W v2, SMC2662W-AR */
222 { USB_DEVICE(0x0d5c, 0xa002), USB_DEVICE_DATA(BOARD_503_ACC) },
224 * at76c505-rfmd
226 /* Generic AT76C505/RFMD */
227 { USB_DEVICE(0x03eb, 0x7606), USB_DEVICE_DATA(BOARD_505) },
229 * at76c505-rfmd2958
231 /* Generic AT76C505/RFMD, OvisLink WL-1130USB */
232 { USB_DEVICE(0x03eb, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
233 /* Fiberline FL-WL240U */
234 { USB_DEVICE(0x1371, 0x0014), USB_DEVICE_DATA(BOARD_505_2958) },
235 /* CNet CNUSB-611G */
236 { USB_DEVICE(0x1371, 0x0013), USB_DEVICE_DATA(BOARD_505_2958) },
237 /* Linksys WUSB11 v2.8 */
238 { USB_DEVICE(0x1915, 0x2233), USB_DEVICE_DATA(BOARD_505_2958) },
239 /* Xterasys XN-2122B, IBlitzz BWU613B/BWU613SB */
240 { USB_DEVICE(0x12fd, 0x1001), USB_DEVICE_DATA(BOARD_505_2958) },
241 /* Corega WLAN USB Stick 11 */
242 { USB_DEVICE(0x07aa, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
243 /* Microstar MSI Box MS6978 */
244 { USB_DEVICE(0x0db0, 0x1020), USB_DEVICE_DATA(BOARD_505_2958) },
246 * at76c505a-rfmd2958
248 /* Generic AT76C505A device */
249 { USB_DEVICE(0x03eb, 0x7614), USB_DEVICE_DATA(BOARD_505A) },
250 /* Generic AT76C505AS device */
251 { USB_DEVICE(0x03eb, 0x7617), USB_DEVICE_DATA(BOARD_505A) },
252 /* Siemens Gigaset USB WLAN Adapter 11 */
253 { USB_DEVICE(0x1690, 0x0701), USB_DEVICE_DATA(BOARD_505A) },
255 * at76c505amx-rfmd
257 /* Generic AT76C505AMX device */
258 { USB_DEVICE(0x03eb, 0x7615), USB_DEVICE_DATA(BOARD_505AMX) },
262 MODULE_DEVICE_TABLE(usb, dev_table);
264 /* Supported rates of this hardware, bit 7 marks basic rates */
265 static const u8 hw_rates[] = { 0x82, 0x84, 0x0b, 0x16 };
267 static const char *const preambles[] = { "long", "short", "auto" };
269 /* Firmware download */
270 /* DFU states */
271 #define STATE_IDLE 0x00
272 #define STATE_DETACH 0x01
273 #define STATE_DFU_IDLE 0x02
274 #define STATE_DFU_DOWNLOAD_SYNC 0x03
275 #define STATE_DFU_DOWNLOAD_BUSY 0x04
276 #define STATE_DFU_DOWNLOAD_IDLE 0x05
277 #define STATE_DFU_MANIFEST_SYNC 0x06
278 #define STATE_DFU_MANIFEST 0x07
279 #define STATE_DFU_MANIFEST_WAIT_RESET 0x08
280 #define STATE_DFU_UPLOAD_IDLE 0x09
281 #define STATE_DFU_ERROR 0x0a
283 /* DFU commands */
284 #define DFU_DETACH 0
285 #define DFU_DNLOAD 1
286 #define DFU_UPLOAD 2
287 #define DFU_GETSTATUS 3
288 #define DFU_CLRSTATUS 4
289 #define DFU_GETSTATE 5
290 #define DFU_ABORT 6
292 #define FW_BLOCK_SIZE 1024
294 struct dfu_status {
295 unsigned char status;
296 unsigned char poll_timeout[3];
297 unsigned char state;
298 unsigned char string;
299 } __attribute__((packed));
301 static inline int at76_is_intersil(enum board_type board)
303 return (board == BOARD_503_ISL3861 || board == BOARD_503_ISL3863);
306 static inline int at76_is_503rfmd(enum board_type board)
308 return (board == BOARD_503 || board == BOARD_503_ACC);
311 static inline int at76_is_505(enum board_type board)
313 return (board == BOARD_505 || BOARD_505_2958);
316 static inline int at76_is_505a(enum board_type board)
318 return (board == BOARD_505A || board == BOARD_505AMX);
321 /* Load a block of the first (internal) part of the firmware */
322 static int at76_load_int_fw_block(struct usb_device *udev, int blockno,
323 void *block, int size)
325 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), DFU_DNLOAD,
326 USB_TYPE_CLASS | USB_DIR_OUT |
327 USB_RECIP_INTERFACE, blockno, 0, block, size,
328 USB_CTRL_GET_TIMEOUT);
331 static int at76_dfu_get_status(struct usb_device *udev,
332 struct dfu_status *status)
334 int ret;
336 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATUS,
337 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
338 0, 0, status, sizeof(struct dfu_status),
339 USB_CTRL_GET_TIMEOUT);
340 return ret;
343 static u8 at76_dfu_get_state(struct usb_device *udev, u8 *state)
345 int ret;
347 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATE,
348 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
349 0, 0, state, 1, USB_CTRL_GET_TIMEOUT);
350 return ret;
353 /* Convert timeout from the DFU status to jiffies */
354 static inline unsigned long at76_get_timeout(struct dfu_status *s)
356 return msecs_to_jiffies((s->poll_timeout[2] << 16)
357 | (s->poll_timeout[1] << 8)
358 | (s->poll_timeout[0]));
361 /* Load internal firmware from the buffer. If manifest_sync_timeout > 0, use
362 * its value in jiffies in the MANIFEST_SYNC state. */
363 static int at76_usbdfu_download(struct usb_device *udev, u8 *buf, u32 size,
364 int manifest_sync_timeout)
366 u8 *block;
367 struct dfu_status dfu_stat_buf;
368 int ret = 0;
369 int need_dfu_state = 1;
370 int is_done = 0;
371 u8 dfu_state = 0;
372 u32 dfu_timeout = 0;
373 int bsize = 0;
374 int blockno = 0;
376 at76_dbg(DBG_DFU, "%s( %p, %u, %d)", __func__, buf, size,
377 manifest_sync_timeout);
379 if (!size) {
380 dev_printk(KERN_ERR, &udev->dev, "FW buffer length invalid!\n");
381 return -EINVAL;
384 block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
385 if (!block)
386 return -ENOMEM;
388 do {
389 if (need_dfu_state) {
390 ret = at76_dfu_get_state(udev, &dfu_state);
391 if (ret < 0) {
392 dev_printk(KERN_ERR, &udev->dev,
393 "cannot get DFU state: %d\n", ret);
394 goto exit;
396 need_dfu_state = 0;
399 switch (dfu_state) {
400 case STATE_DFU_DOWNLOAD_SYNC:
401 at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_SYNC");
402 ret = at76_dfu_get_status(udev, &dfu_stat_buf);
403 if (ret >= 0) {
404 dfu_state = dfu_stat_buf.state;
405 dfu_timeout = at76_get_timeout(&dfu_stat_buf);
406 need_dfu_state = 0;
407 } else
408 dev_printk(KERN_ERR, &udev->dev,
409 "at76_dfu_get_status returned %d\n",
410 ret);
411 break;
413 case STATE_DFU_DOWNLOAD_BUSY:
414 at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_BUSY");
415 need_dfu_state = 1;
417 at76_dbg(DBG_DFU, "DFU: Resetting device");
418 schedule_timeout_interruptible(dfu_timeout);
419 break;
421 case STATE_DFU_DOWNLOAD_IDLE:
422 at76_dbg(DBG_DFU, "DOWNLOAD...");
423 /* fall through */
424 case STATE_DFU_IDLE:
425 at76_dbg(DBG_DFU, "DFU IDLE");
427 bsize = min_t(int, size, FW_BLOCK_SIZE);
428 memcpy(block, buf, bsize);
429 at76_dbg(DBG_DFU, "int fw, size left = %5d, "
430 "bsize = %4d, blockno = %2d", size, bsize,
431 blockno);
432 ret =
433 at76_load_int_fw_block(udev, blockno, block, bsize);
434 buf += bsize;
435 size -= bsize;
436 blockno++;
438 if (ret != bsize)
439 dev_printk(KERN_ERR, &udev->dev,
440 "at76_load_int_fw_block "
441 "returned %d\n", ret);
442 need_dfu_state = 1;
443 break;
445 case STATE_DFU_MANIFEST_SYNC:
446 at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_SYNC");
448 ret = at76_dfu_get_status(udev, &dfu_stat_buf);
449 if (ret < 0)
450 break;
452 dfu_state = dfu_stat_buf.state;
453 dfu_timeout = at76_get_timeout(&dfu_stat_buf);
454 need_dfu_state = 0;
456 /* override the timeout from the status response,
457 needed for AT76C505A */
458 if (manifest_sync_timeout > 0)
459 dfu_timeout = manifest_sync_timeout;
461 at76_dbg(DBG_DFU, "DFU: Waiting for manifest phase");
462 schedule_timeout_interruptible(dfu_timeout);
463 break;
465 case STATE_DFU_MANIFEST:
466 at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST");
467 is_done = 1;
468 break;
470 case STATE_DFU_MANIFEST_WAIT_RESET:
471 at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_WAIT_RESET");
472 is_done = 1;
473 break;
475 case STATE_DFU_UPLOAD_IDLE:
476 at76_dbg(DBG_DFU, "STATE_DFU_UPLOAD_IDLE");
477 break;
479 case STATE_DFU_ERROR:
480 at76_dbg(DBG_DFU, "STATE_DFU_ERROR");
481 ret = -EPIPE;
482 break;
484 default:
485 at76_dbg(DBG_DFU, "DFU UNKNOWN STATE (%d)", dfu_state);
486 ret = -EINVAL;
487 break;
489 } while (!is_done && (ret >= 0));
491 exit:
492 kfree(block);
493 if (ret >= 0)
494 ret = 0;
496 return ret;
499 #define HEX2STR_BUFFERS 4
500 #define HEX2STR_MAX_LEN 64
501 #define BIN2HEX(x) ((x) < 10 ? '0' + (x) : (x) + 'A' - 10)
503 /* Convert binary data into hex string */
504 static char *hex2str(void *buf, int len)
506 static atomic_t a = ATOMIC_INIT(0);
507 static char bufs[HEX2STR_BUFFERS][3 * HEX2STR_MAX_LEN + 1];
508 char *ret = bufs[atomic_inc_return(&a) & (HEX2STR_BUFFERS - 1)];
509 char *obuf = ret;
510 u8 *ibuf = buf;
512 if (len > HEX2STR_MAX_LEN)
513 len = HEX2STR_MAX_LEN;
515 if (len <= 0) {
516 ret[0] = '\0';
517 return ret;
520 while (len--) {
521 *obuf++ = BIN2HEX(*ibuf >> 4);
522 *obuf++ = BIN2HEX(*ibuf & 0xf);
523 *obuf++ = '-';
524 ibuf++;
526 *(--obuf) = '\0';
528 return ret;
531 #define MAC2STR_BUFFERS 4
533 static inline char *mac2str(u8 *mac)
535 static atomic_t a = ATOMIC_INIT(0);
536 static char bufs[MAC2STR_BUFFERS][6 * 3];
537 char *str;
539 str = bufs[atomic_inc_return(&a) & (MAC2STR_BUFFERS - 1)];
540 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
541 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
542 return str;
545 /* LED trigger */
546 static int tx_activity;
547 static void at76_ledtrig_tx_timerfunc(unsigned long data);
548 static DEFINE_TIMER(ledtrig_tx_timer, at76_ledtrig_tx_timerfunc, 0, 0);
549 DEFINE_LED_TRIGGER(ledtrig_tx);
551 static void at76_ledtrig_tx_timerfunc(unsigned long data)
553 static int tx_lastactivity;
555 if (tx_lastactivity != tx_activity) {
556 tx_lastactivity = tx_activity;
557 led_trigger_event(ledtrig_tx, LED_FULL);
558 mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
559 } else
560 led_trigger_event(ledtrig_tx, LED_OFF);
563 static void at76_ledtrig_tx_activity(void)
565 tx_activity++;
566 if (!timer_pending(&ledtrig_tx_timer))
567 mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
570 static int at76_remap(struct usb_device *udev)
572 int ret;
573 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0a,
574 USB_TYPE_VENDOR | USB_DIR_OUT |
575 USB_RECIP_INTERFACE, 0, 0, NULL, 0,
576 USB_CTRL_GET_TIMEOUT);
577 if (ret < 0)
578 return ret;
579 return 0;
582 static int at76_get_op_mode(struct usb_device *udev)
584 int ret;
585 u8 op_mode;
587 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
588 USB_TYPE_VENDOR | USB_DIR_IN |
589 USB_RECIP_INTERFACE, 0x01, 0, &op_mode, 1,
590 USB_CTRL_GET_TIMEOUT);
591 if (ret < 0)
592 return ret;
593 else if (ret < 1)
594 return -EIO;
595 else
596 return op_mode;
599 /* Load a block of the second ("external") part of the firmware */
600 static inline int at76_load_ext_fw_block(struct usb_device *udev, int blockno,
601 void *block, int size)
603 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
604 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
605 0x0802, blockno, block, size,
606 USB_CTRL_GET_TIMEOUT);
609 static inline int at76_get_hw_cfg(struct usb_device *udev,
610 union at76_hwcfg *buf, int buf_size)
612 return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
613 USB_TYPE_VENDOR | USB_DIR_IN |
614 USB_RECIP_INTERFACE, 0x0a02, 0,
615 buf, buf_size, USB_CTRL_GET_TIMEOUT);
618 /* Intersil boards use a different "value" for GetHWConfig requests */
619 static inline int at76_get_hw_cfg_intersil(struct usb_device *udev,
620 union at76_hwcfg *buf, int buf_size)
622 return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
623 USB_TYPE_VENDOR | USB_DIR_IN |
624 USB_RECIP_INTERFACE, 0x0902, 0,
625 buf, buf_size, USB_CTRL_GET_TIMEOUT);
628 /* Get the hardware configuration for the adapter and put it to the appropriate
629 * fields of 'priv' (the GetHWConfig request and interpretation of the result
630 * depends on the board type) */
631 static int at76_get_hw_config(struct at76_priv *priv)
633 int ret;
634 union at76_hwcfg *hwcfg = kmalloc(sizeof(*hwcfg), GFP_KERNEL);
636 if (!hwcfg)
637 return -ENOMEM;
639 if (at76_is_intersil(priv->board_type)) {
640 ret = at76_get_hw_cfg_intersil(priv->udev, hwcfg,
641 sizeof(hwcfg->i));
642 if (ret < 0)
643 goto exit;
644 memcpy(priv->mac_addr, hwcfg->i.mac_addr, ETH_ALEN);
645 priv->regulatory_domain = hwcfg->i.regulatory_domain;
646 } else if (at76_is_503rfmd(priv->board_type)) {
647 ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r3));
648 if (ret < 0)
649 goto exit;
650 memcpy(priv->mac_addr, hwcfg->r3.mac_addr, ETH_ALEN);
651 priv->regulatory_domain = hwcfg->r3.regulatory_domain;
652 } else {
653 ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r5));
654 if (ret < 0)
655 goto exit;
656 memcpy(priv->mac_addr, hwcfg->r5.mac_addr, ETH_ALEN);
657 priv->regulatory_domain = hwcfg->r5.regulatory_domain;
660 exit:
661 kfree(hwcfg);
662 if (ret < 0)
663 printk(KERN_ERR "%s: cannot get HW Config (error %d)\n",
664 wiphy_name(priv->hw->wiphy), ret);
666 return ret;
669 static struct reg_domain const *at76_get_reg_domain(u16 code)
671 int i;
672 static struct reg_domain const fd_tab[] = {
673 { 0x10, "FCC (USA)", 0x7ff }, /* ch 1-11 */
674 { 0x20, "IC (Canada)", 0x7ff }, /* ch 1-11 */
675 { 0x30, "ETSI (most of Europe)", 0x1fff }, /* ch 1-13 */
676 { 0x31, "Spain", 0x600 }, /* ch 10-11 */
677 { 0x32, "France", 0x1e00 }, /* ch 10-13 */
678 { 0x40, "MKK (Japan)", 0x2000 }, /* ch 14 */
679 { 0x41, "MKK1 (Japan)", 0x3fff }, /* ch 1-14 */
680 { 0x50, "Israel", 0x3fc }, /* ch 3-9 */
681 { 0x00, "<unknown>", 0xffffffff } /* ch 1-32 */
684 /* Last entry is fallback for unknown domain code */
685 for (i = 0; i < ARRAY_SIZE(fd_tab) - 1; i++)
686 if (code == fd_tab[i].code)
687 break;
689 return &fd_tab[i];
692 static inline int at76_get_mib(struct usb_device *udev, u16 mib, void *buf,
693 int buf_size)
695 int ret;
697 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
698 USB_TYPE_VENDOR | USB_DIR_IN |
699 USB_RECIP_INTERFACE, mib << 8, 0, buf, buf_size,
700 USB_CTRL_GET_TIMEOUT);
701 if (ret >= 0 && ret != buf_size)
702 return -EIO;
703 return ret;
706 /* Return positive number for status, negative for an error */
707 static inline int at76_get_cmd_status(struct usb_device *udev, u8 cmd)
709 u8 stat_buf[40];
710 int ret;
712 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x22,
713 USB_TYPE_VENDOR | USB_DIR_IN |
714 USB_RECIP_INTERFACE, cmd, 0, stat_buf,
715 sizeof(stat_buf), USB_CTRL_GET_TIMEOUT);
716 if (ret < 0)
717 return ret;
719 return stat_buf[5];
722 #define MAKE_CMD_CASE(c) case (c): return #c
724 static const char *at76_get_cmd_string(u8 cmd_status)
726 switch (cmd_status) {
727 MAKE_CMD_CASE(CMD_SET_MIB);
728 MAKE_CMD_CASE(CMD_GET_MIB);
729 MAKE_CMD_CASE(CMD_SCAN);
730 MAKE_CMD_CASE(CMD_JOIN);
731 MAKE_CMD_CASE(CMD_START_IBSS);
732 MAKE_CMD_CASE(CMD_RADIO_ON);
733 MAKE_CMD_CASE(CMD_RADIO_OFF);
734 MAKE_CMD_CASE(CMD_STARTUP);
737 return "UNKNOWN";
740 static int at76_set_card_command(struct usb_device *udev, int cmd, void *buf,
741 int buf_size)
743 int ret;
744 struct at76_command *cmd_buf = kmalloc(sizeof(struct at76_command) +
745 buf_size, GFP_KERNEL);
747 if (!cmd_buf)
748 return -ENOMEM;
750 cmd_buf->cmd = cmd;
751 cmd_buf->reserved = 0;
752 cmd_buf->size = cpu_to_le16(buf_size);
753 memcpy(cmd_buf->data, buf, buf_size);
755 at76_dbg_dump(DBG_CMD, cmd_buf, sizeof(struct at76_command) + buf_size,
756 "issuing command %s (0x%02x)",
757 at76_get_cmd_string(cmd), cmd);
759 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
760 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
761 0, 0, cmd_buf,
762 sizeof(struct at76_command) + buf_size,
763 USB_CTRL_GET_TIMEOUT);
764 kfree(cmd_buf);
765 return ret;
768 #define MAKE_CMD_STATUS_CASE(c) case (c): return #c
769 static const char *at76_get_cmd_status_string(u8 cmd_status)
771 switch (cmd_status) {
772 MAKE_CMD_STATUS_CASE(CMD_STATUS_IDLE);
773 MAKE_CMD_STATUS_CASE(CMD_STATUS_COMPLETE);
774 MAKE_CMD_STATUS_CASE(CMD_STATUS_UNKNOWN);
775 MAKE_CMD_STATUS_CASE(CMD_STATUS_INVALID_PARAMETER);
776 MAKE_CMD_STATUS_CASE(CMD_STATUS_FUNCTION_NOT_SUPPORTED);
777 MAKE_CMD_STATUS_CASE(CMD_STATUS_TIME_OUT);
778 MAKE_CMD_STATUS_CASE(CMD_STATUS_IN_PROGRESS);
779 MAKE_CMD_STATUS_CASE(CMD_STATUS_HOST_FAILURE);
780 MAKE_CMD_STATUS_CASE(CMD_STATUS_SCAN_FAILED);
783 return "UNKNOWN";
786 /* Wait until the command is completed */
787 static int at76_wait_completion(struct at76_priv *priv, int cmd)
789 int status = 0;
790 unsigned long timeout = jiffies + CMD_COMPLETION_TIMEOUT;
792 do {
793 status = at76_get_cmd_status(priv->udev, cmd);
794 if (status < 0) {
795 printk(KERN_ERR "%s: at76_get_cmd_status failed: %d\n",
796 wiphy_name(priv->hw->wiphy), status);
797 break;
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));
805 if (status != CMD_STATUS_IN_PROGRESS
806 && status != CMD_STATUS_IDLE)
807 break;
809 schedule_timeout_interruptible(HZ / 10); /* 100 ms */
810 if (time_after(jiffies, timeout)) {
811 printk(KERN_ERR
812 "%s: completion timeout for command %d\n",
813 wiphy_name(priv->hw->wiphy), cmd);
814 status = -ETIMEDOUT;
815 break;
817 } while (1);
819 return status;
822 static int at76_set_mib(struct at76_priv *priv, struct set_mib_buffer *buf)
824 int ret;
826 ret = at76_set_card_command(priv->udev, CMD_SET_MIB, buf,
827 offsetof(struct set_mib_buffer,
828 data) + buf->size);
829 if (ret < 0)
830 return ret;
832 ret = at76_wait_completion(priv, CMD_SET_MIB);
833 if (ret != CMD_STATUS_COMPLETE) {
834 printk(KERN_INFO
835 "%s: set_mib: at76_wait_completion failed "
836 "with %d\n", wiphy_name(priv->hw->wiphy), ret);
837 ret = -EIO;
840 return ret;
843 /* Return < 0 on error, == 0 if no command sent, == 1 if cmd sent */
844 static int at76_set_radio(struct at76_priv *priv, int enable)
846 int ret;
847 int cmd;
849 if (priv->radio_on == enable)
850 return 0;
852 cmd = enable ? CMD_RADIO_ON : CMD_RADIO_OFF;
854 ret = at76_set_card_command(priv->udev, cmd, NULL, 0);
855 if (ret < 0)
856 printk(KERN_ERR "%s: at76_set_card_command(%d) failed: %d\n",
857 wiphy_name(priv->hw->wiphy), cmd, ret);
858 else
859 ret = 1;
861 priv->radio_on = enable;
862 return ret;
865 /* Set current power save mode (AT76_PM_OFF/AT76_PM_ON/AT76_PM_SMART) */
866 static int at76_set_pm_mode(struct at76_priv *priv)
868 int ret = 0;
870 priv->mib_buf.type = MIB_MAC_MGMT;
871 priv->mib_buf.size = 1;
872 priv->mib_buf.index = offsetof(struct mib_mac_mgmt, power_mgmt_mode);
873 priv->mib_buf.data.byte = priv->pm_mode;
875 ret = at76_set_mib(priv, &priv->mib_buf);
876 if (ret < 0)
877 printk(KERN_ERR "%s: set_mib (pm_mode) failed: %d\n",
878 wiphy_name(priv->hw->wiphy), ret);
880 return ret;
883 /* Set the association id for power save mode */
884 static int at76_set_associd(struct at76_priv *priv, u16 id)
886 int ret = 0;
888 priv->mib_buf.type = MIB_MAC_MGMT;
889 priv->mib_buf.size = 2;
890 priv->mib_buf.index = offsetof(struct mib_mac_mgmt, station_id);
891 priv->mib_buf.data.word = cpu_to_le16(id);
893 ret = at76_set_mib(priv, &priv->mib_buf);
894 if (ret < 0)
895 printk(KERN_ERR "%s: set_mib (associd) failed: %d\n",
896 wiphy_name(priv->hw->wiphy), ret);
898 return ret;
901 /* Set the listen interval for power save mode */
902 static int at76_set_listen_interval(struct at76_priv *priv, u16 interval)
904 int ret = 0;
906 priv->mib_buf.type = MIB_MAC;
907 priv->mib_buf.size = 2;
908 priv->mib_buf.index = offsetof(struct mib_mac, listen_interval);
909 priv->mib_buf.data.word = cpu_to_le16(interval);
911 ret = at76_set_mib(priv, &priv->mib_buf);
912 if (ret < 0)
913 printk(KERN_ERR
914 "%s: set_mib (listen_interval) failed: %d\n",
915 wiphy_name(priv->hw->wiphy), ret);
917 return ret;
920 static int at76_set_preamble(struct at76_priv *priv, u8 type)
922 int ret = 0;
924 priv->mib_buf.type = MIB_LOCAL;
925 priv->mib_buf.size = 1;
926 priv->mib_buf.index = offsetof(struct mib_local, preamble_type);
927 priv->mib_buf.data.byte = type;
929 ret = at76_set_mib(priv, &priv->mib_buf);
930 if (ret < 0)
931 printk(KERN_ERR "%s: set_mib (preamble) failed: %d\n",
932 wiphy_name(priv->hw->wiphy), ret);
934 return ret;
937 static int at76_set_frag(struct at76_priv *priv, u16 size)
939 int ret = 0;
941 priv->mib_buf.type = MIB_MAC;
942 priv->mib_buf.size = 2;
943 priv->mib_buf.index = offsetof(struct mib_mac, frag_threshold);
944 priv->mib_buf.data.word = cpu_to_le16(size);
946 ret = at76_set_mib(priv, &priv->mib_buf);
947 if (ret < 0)
948 printk(KERN_ERR "%s: set_mib (frag threshold) failed: %d\n",
949 wiphy_name(priv->hw->wiphy), ret);
951 return ret;
954 static int at76_set_rts(struct at76_priv *priv, u16 size)
956 int ret = 0;
958 priv->mib_buf.type = MIB_MAC;
959 priv->mib_buf.size = 2;
960 priv->mib_buf.index = offsetof(struct mib_mac, rts_threshold);
961 priv->mib_buf.data.word = cpu_to_le16(size);
963 ret = at76_set_mib(priv, &priv->mib_buf);
964 if (ret < 0)
965 printk(KERN_ERR "%s: set_mib (rts) failed: %d\n",
966 wiphy_name(priv->hw->wiphy), ret);
968 return ret;
971 static int at76_set_autorate_fallback(struct at76_priv *priv, int onoff)
973 int ret = 0;
975 priv->mib_buf.type = MIB_LOCAL;
976 priv->mib_buf.size = 1;
977 priv->mib_buf.index = offsetof(struct mib_local, txautorate_fallback);
978 priv->mib_buf.data.byte = onoff;
980 ret = at76_set_mib(priv, &priv->mib_buf);
981 if (ret < 0)
982 printk(KERN_ERR "%s: set_mib (autorate fallback) failed: %d\n",
983 wiphy_name(priv->hw->wiphy), ret);
985 return ret;
988 static int at76_add_mac_address(struct at76_priv *priv, void *addr)
990 int ret = 0;
992 priv->mib_buf.type = MIB_MAC_ADDR;
993 priv->mib_buf.size = ETH_ALEN;
994 priv->mib_buf.index = offsetof(struct mib_mac_addr, mac_addr);
995 memcpy(priv->mib_buf.data.addr, addr, ETH_ALEN);
997 ret = at76_set_mib(priv, &priv->mib_buf);
998 if (ret < 0)
999 printk(KERN_ERR "%s: set_mib (MAC_ADDR, mac_addr) failed: %d\n",
1000 wiphy_name(priv->hw->wiphy), ret);
1002 return ret;
1005 static int at76_set_tkip_bssid(struct at76_priv *priv, const void *addr)
1007 int ret = 0;
1009 priv->mib_buf.type = MIB_MAC_ENCRYPTION;
1010 priv->mib_buf.size = ETH_ALEN;
1011 priv->mib_buf.index = offsetof(struct mib_mac_encryption, tkip_bssid);
1012 memcpy(priv->mib_buf.data.addr, addr, ETH_ALEN);
1014 ret = at76_set_mib(priv, &priv->mib_buf);
1015 if (ret < 0)
1016 printk(KERN_ERR "%s: set_mib (MAC_ENCRYPTION, tkip_bssid) failed: %d\n",
1017 wiphy_name(priv->hw->wiphy), ret);
1019 return ret;
1022 static int at76_reset_rsc(struct at76_priv *priv)
1024 int ret = 0;
1026 priv->mib_buf.type = MIB_MAC_ENCRYPTION;
1027 priv->mib_buf.size = 4 * 8;
1028 priv->mib_buf.index = offsetof(struct mib_mac_encryption, key_rsc);
1029 memset(priv->mib_buf.data.data, 0 , priv->mib_buf.size);
1031 ret = at76_set_mib(priv, &priv->mib_buf);
1032 if (ret < 0)
1033 printk(KERN_ERR "%s: set_mib (MAC_ENCRYPTION, key_rsc) failed: %d\n",
1034 wiphy_name(priv->hw->wiphy), ret);
1036 return ret;
1039 static void at76_dump_mib_mac_addr(struct at76_priv *priv)
1041 int i;
1042 int ret;
1043 struct mib_mac_addr *m = kmalloc(sizeof(struct mib_mac_addr),
1044 GFP_KERNEL);
1046 if (!m)
1047 return;
1049 ret = at76_get_mib(priv->udev, MIB_MAC_ADDR, m,
1050 sizeof(struct mib_mac_addr));
1051 if (ret < 0) {
1052 printk(KERN_ERR "%s: at76_get_mib (MAC_ADDR) failed: %d\n",
1053 wiphy_name(priv->hw->wiphy), ret);
1054 goto exit;
1057 at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: mac_addr %s res 0x%x 0x%x",
1058 wiphy_name(priv->hw->wiphy),
1059 mac2str(m->mac_addr), m->res[0], m->res[1]);
1060 for (i = 0; i < ARRAY_SIZE(m->group_addr); i++)
1061 at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: group addr %d: %s, "
1062 "status %d", wiphy_name(priv->hw->wiphy), i,
1063 mac2str(m->group_addr[i]), m->group_addr_status[i]);
1064 exit:
1065 kfree(m);
1068 static void at76_dump_mib_mac_wep(struct at76_priv *priv)
1070 int i;
1071 int ret;
1072 int key_len;
1073 struct mib_mac_wep *m = kmalloc(sizeof(struct mib_mac_wep), GFP_KERNEL);
1075 if (!m)
1076 return;
1078 ret = at76_get_mib(priv->udev, MIB_MAC_WEP, m,
1079 sizeof(struct mib_mac_wep));
1080 if (ret < 0) {
1081 printk(KERN_ERR "%s: at76_get_mib (MAC_WEP) failed: %d\n",
1082 wiphy_name(priv->hw->wiphy), ret);
1083 goto exit;
1086 at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: priv_invoked %u def_key_id %u "
1087 "key_len %u excl_unencr %u wep_icv_err %u wep_excluded %u "
1088 "encr_level %u key %d", wiphy_name(priv->hw->wiphy),
1089 m->privacy_invoked, m->wep_default_key_id,
1090 m->wep_key_mapping_len, m->exclude_unencrypted,
1091 le32_to_cpu(m->wep_icv_error_count),
1092 le32_to_cpu(m->wep_excluded_count), m->encryption_level,
1093 m->wep_default_key_id);
1095 key_len = (m->encryption_level == 1) ?
1096 WEP_SMALL_KEY_LEN : WEP_LARGE_KEY_LEN;
1098 for (i = 0; i < WEP_KEYS; i++)
1099 at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: key %d: %s",
1100 wiphy_name(priv->hw->wiphy), i,
1101 hex2str(m->wep_default_keyvalue[i], key_len));
1102 exit:
1103 kfree(m);
1106 static void at76_dump_mib_mac_encryption(struct at76_priv *priv)
1108 int i;
1109 int ret;
1110 /*int key_len;*/
1111 struct mib_mac_encryption *m = kmalloc(sizeof(struct mib_mac_encryption), GFP_KERNEL);
1113 if (!m)
1114 return;
1116 ret = at76_get_mib(priv->udev, MIB_MAC_ENCRYPTION, m,
1117 sizeof(struct mib_mac_encryption));
1118 if (ret < 0) {
1119 printk(KERN_ERR "%s: at76_get_mib (MAC_ENCRYPTION) failed: %d\n",
1120 wiphy_name(priv->hw->wiphy), ret);
1121 goto exit;
1124 at76_dbg(DBG_MIB, "%s: MIB MAC_ENCRYPTION: tkip_bssid %s priv_invoked %u "
1125 "ciph_key_id %u grp_key_id %u excl_unencr %u "
1126 "ckip_key_perm %u wep_icv_err %u wep_excluded %u",
1127 wiphy_name(priv->hw->wiphy), mac2str(m->tkip_bssid),
1128 m->privacy_invoked, m->cipher_default_key_id,
1129 m->cipher_default_group_key_id, m->exclude_unencrypted,
1130 m->ckip_key_permutation,
1131 le32_to_cpu(m->wep_icv_error_count),
1132 le32_to_cpu(m->wep_excluded_count));
1134 /*key_len = (m->encryption_level == 1) ?
1135 WEP_SMALL_KEY_LEN : WEP_LARGE_KEY_LEN;*/
1137 for (i = 0; i < CIPHER_KEYS; i++)
1138 at76_dbg(DBG_MIB, "%s: MIB MAC_ENCRYPTION: key %d: %s",
1139 wiphy_name(priv->hw->wiphy), i,
1140 hex2str(m->cipher_default_keyvalue[i], CIPHER_KEY_LEN));
1141 exit:
1142 kfree(m);
1145 static void at76_dump_mib_mac_mgmt(struct at76_priv *priv)
1147 int ret;
1148 struct mib_mac_mgmt *m = kmalloc(sizeof(struct mib_mac_mgmt),
1149 GFP_KERNEL);
1151 if (!m)
1152 return;
1154 ret = at76_get_mib(priv->udev, MIB_MAC_MGMT, m,
1155 sizeof(struct mib_mac_mgmt));
1156 if (ret < 0) {
1157 printk(KERN_ERR "%s: at76_get_mib (MAC_MGMT) failed: %d\n",
1158 wiphy_name(priv->hw->wiphy), ret);
1159 goto exit;
1162 at76_dbg(DBG_MIB, "%s: MIB MAC_MGMT: beacon_period %d CFP_max_duration "
1163 "%d medium_occupancy_limit %d station_id 0x%x ATIM_window %d "
1164 "CFP_mode %d privacy_opt_impl %d DTIM_period %d CFP_period %d "
1165 "current_bssid %s current_essid %s current_bss_type %d "
1166 "pm_mode %d ibss_change %d res %d "
1167 "multi_domain_capability_implemented %d "
1168 "international_roaming %d country_string %.3s",
1169 wiphy_name(priv->hw->wiphy), le16_to_cpu(m->beacon_period),
1170 le16_to_cpu(m->CFP_max_duration),
1171 le16_to_cpu(m->medium_occupancy_limit),
1172 le16_to_cpu(m->station_id), le16_to_cpu(m->ATIM_window),
1173 m->CFP_mode, m->privacy_option_implemented, m->DTIM_period,
1174 m->CFP_period, mac2str(m->current_bssid),
1175 hex2str(m->current_essid, IW_ESSID_MAX_SIZE),
1176 m->current_bss_type, m->power_mgmt_mode, m->ibss_change,
1177 m->res, m->multi_domain_capability_implemented,
1178 m->multi_domain_capability_enabled, m->country_string);
1179 exit:
1180 kfree(m);
1183 static void at76_dump_mib_mac(struct at76_priv *priv)
1185 int ret;
1186 struct mib_mac *m = kmalloc(sizeof(struct mib_mac), GFP_KERNEL);
1188 if (!m)
1189 return;
1191 ret = at76_get_mib(priv->udev, MIB_MAC, m, sizeof(struct mib_mac));
1192 if (ret < 0) {
1193 printk(KERN_ERR "%s: at76_get_mib (MAC) failed: %d\n",
1194 wiphy_name(priv->hw->wiphy), ret);
1195 goto exit;
1198 at76_dbg(DBG_MIB, "%s: MIB MAC: max_tx_msdu_lifetime %d "
1199 "max_rx_lifetime %d frag_threshold %d rts_threshold %d "
1200 "cwmin %d cwmax %d short_retry_time %d long_retry_time %d "
1201 "scan_type %d scan_channel %d probe_delay %u "
1202 "min_channel_time %d max_channel_time %d listen_int %d "
1203 "desired_ssid %s desired_bssid %s desired_bsstype %d",
1204 wiphy_name(priv->hw->wiphy),
1205 le32_to_cpu(m->max_tx_msdu_lifetime),
1206 le32_to_cpu(m->max_rx_lifetime),
1207 le16_to_cpu(m->frag_threshold), le16_to_cpu(m->rts_threshold),
1208 le16_to_cpu(m->cwmin), le16_to_cpu(m->cwmax),
1209 m->short_retry_time, m->long_retry_time, m->scan_type,
1210 m->scan_channel, le16_to_cpu(m->probe_delay),
1211 le16_to_cpu(m->min_channel_time),
1212 le16_to_cpu(m->max_channel_time),
1213 le16_to_cpu(m->listen_interval),
1214 hex2str(m->desired_ssid, IW_ESSID_MAX_SIZE),
1215 mac2str(m->desired_bssid), m->desired_bsstype);
1216 exit:
1217 kfree(m);
1220 static void at76_dump_mib_phy(struct at76_priv *priv)
1222 int ret;
1223 struct mib_phy *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
1225 if (!m)
1226 return;
1228 ret = at76_get_mib(priv->udev, MIB_PHY, m, sizeof(struct mib_phy));
1229 if (ret < 0) {
1230 printk(KERN_ERR "%s: at76_get_mib (PHY) failed: %d\n",
1231 wiphy_name(priv->hw->wiphy), ret);
1232 goto exit;
1235 at76_dbg(DBG_MIB, "%s: MIB PHY: ed_threshold %d slot_time %d "
1236 "sifs_time %d preamble_length %d plcp_header_length %d "
1237 "mpdu_max_length %d cca_mode_supported %d operation_rate_set "
1238 "0x%x 0x%x 0x%x 0x%x channel_id %d current_cca_mode %d "
1239 "phy_type %d current_reg_domain %d",
1240 wiphy_name(priv->hw->wiphy), le32_to_cpu(m->ed_threshold),
1241 le16_to_cpu(m->slot_time), le16_to_cpu(m->sifs_time),
1242 le16_to_cpu(m->preamble_length),
1243 le16_to_cpu(m->plcp_header_length),
1244 le16_to_cpu(m->mpdu_max_length),
1245 le16_to_cpu(m->cca_mode_supported), m->operation_rate_set[0],
1246 m->operation_rate_set[1], m->operation_rate_set[2],
1247 m->operation_rate_set[3], m->channel_id, m->current_cca_mode,
1248 m->phy_type, m->current_reg_domain);
1249 exit:
1250 kfree(m);
1253 static void at76_dump_mib_local(struct at76_priv *priv)
1255 int ret;
1256 struct mib_local *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
1258 if (!m)
1259 return;
1261 ret = at76_get_mib(priv->udev, MIB_LOCAL, m, sizeof(struct mib_local));
1262 if (ret < 0) {
1263 printk(KERN_ERR "%s: at76_get_mib (LOCAL) failed: %d\n",
1264 wiphy_name(priv->hw->wiphy), ret);
1265 goto exit;
1268 at76_dbg(DBG_MIB, "%s: MIB LOCAL: beacon_enable %d "
1269 "txautorate_fallback %d ssid_size %d promiscuous_mode %d "
1270 "preamble_type %d", wiphy_name(priv->hw->wiphy),
1271 m->beacon_enable,
1272 m->txautorate_fallback, m->ssid_size, m->promiscuous_mode,
1273 m->preamble_type);
1274 exit:
1275 kfree(m);
1278 static void at76_dump_mib_mdomain(struct at76_priv *priv)
1280 int ret;
1281 struct mib_mdomain *m = kmalloc(sizeof(struct mib_mdomain), GFP_KERNEL);
1283 if (!m)
1284 return;
1286 ret = at76_get_mib(priv->udev, MIB_MDOMAIN, m,
1287 sizeof(struct mib_mdomain));
1288 if (ret < 0) {
1289 printk(KERN_ERR "%s: at76_get_mib (MDOMAIN) failed: %d\n",
1290 wiphy_name(priv->hw->wiphy), ret);
1291 goto exit;
1294 at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: channel_list %s",
1295 wiphy_name(priv->hw->wiphy),
1296 hex2str(m->channel_list, sizeof(m->channel_list)));
1298 at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: tx_powerlevel %s",
1299 wiphy_name(priv->hw->wiphy),
1300 hex2str(m->tx_powerlevel, sizeof(m->tx_powerlevel)));
1301 exit:
1302 kfree(m);
1305 /* Enable monitor mode */
1306 static int at76_start_monitor(struct at76_priv *priv)
1308 struct at76_req_scan scan;
1309 int ret;
1311 memset(&scan, 0, sizeof(struct at76_req_scan));
1312 memset(scan.bssid, 0xff, ETH_ALEN);
1314 scan.channel = priv->channel;
1315 scan.scan_type = SCAN_TYPE_PASSIVE;
1316 scan.international_scan = 0;
1318 ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
1319 if (ret >= 0)
1320 ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1322 return ret;
1325 /* Calculate padding from txbuf->wlength (which excludes the USB TX header),
1326 likely to compensate a flaw in the AT76C503A USB part ... */
1327 static inline int at76_calc_padding(int wlen)
1329 /* add the USB TX header */
1330 wlen += AT76_TX_HDRLEN;
1332 wlen = wlen % 64;
1334 if (wlen < 50)
1335 return 50 - wlen;
1337 if (wlen >= 61)
1338 return 64 + 50 - wlen;
1340 return 0;
1343 static void at76_rx_callback(struct urb *urb)
1345 struct at76_priv *priv = urb->context;
1347 priv->rx_tasklet.data = (unsigned long)urb;
1348 tasklet_schedule(&priv->rx_tasklet);
1349 return;
1352 static int at76_submit_rx_urb(struct at76_priv *priv)
1354 int ret;
1355 int size;
1356 struct sk_buff *skb = priv->rx_skb;
1358 if (!priv->rx_urb) {
1359 printk(KERN_ERR "%s: %s: priv->rx_urb is NULL\n",
1360 wiphy_name(priv->hw->wiphy), __func__);
1361 return -EFAULT;
1364 if (!skb) {
1365 skb = dev_alloc_skb(sizeof(struct at76_rx_buffer));
1366 if (!skb) {
1367 printk(KERN_ERR "%s: cannot allocate rx skbuff\n",
1368 wiphy_name(priv->hw->wiphy));
1369 ret = -ENOMEM;
1370 goto exit;
1372 priv->rx_skb = skb;
1373 } else {
1374 skb_push(skb, skb_headroom(skb));
1375 skb_trim(skb, 0);
1378 size = skb_tailroom(skb);
1379 usb_fill_bulk_urb(priv->rx_urb, priv->udev, priv->rx_pipe,
1380 skb_put(skb, size), size, at76_rx_callback, priv);
1381 ret = usb_submit_urb(priv->rx_urb, GFP_ATOMIC);
1382 if (ret < 0) {
1383 if (ret == -ENODEV)
1384 at76_dbg(DBG_DEVSTART,
1385 "usb_submit_urb returned -ENODEV");
1386 else
1387 printk(KERN_ERR "%s: rx, usb_submit_urb failed: %d\n",
1388 wiphy_name(priv->hw->wiphy), ret);
1391 exit:
1392 if (ret < 0 && ret != -ENODEV)
1393 printk(KERN_ERR "%s: cannot submit rx urb - please unload the "
1394 "driver and/or power cycle the device\n",
1395 wiphy_name(priv->hw->wiphy));
1397 return ret;
1400 /* Download external firmware */
1401 static int at76_load_external_fw(struct usb_device *udev, struct fwentry *fwe)
1403 int ret;
1404 int op_mode;
1405 int blockno = 0;
1406 int bsize;
1407 u8 *block;
1408 u8 *buf = fwe->extfw;
1409 int size = fwe->extfw_size;
1411 if (!buf || !size)
1412 return -ENOENT;
1414 op_mode = at76_get_op_mode(udev);
1415 at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
1417 if (op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
1418 dev_printk(KERN_ERR, &udev->dev, "unexpected opmode %d\n",
1419 op_mode);
1420 return -EINVAL;
1423 block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
1424 if (!block)
1425 return -ENOMEM;
1427 at76_dbg(DBG_DEVSTART, "downloading external firmware");
1429 /* for fw >= 0.100, the device needs an extra empty block */
1430 do {
1431 bsize = min_t(int, size, FW_BLOCK_SIZE);
1432 memcpy(block, buf, bsize);
1433 at76_dbg(DBG_DEVSTART,
1434 "ext fw, size left = %5d, bsize = %4d, blockno = %2d",
1435 size, bsize, blockno);
1436 ret = at76_load_ext_fw_block(udev, blockno, block, bsize);
1437 if (ret != bsize) {
1438 dev_printk(KERN_ERR, &udev->dev,
1439 "loading %dth firmware block failed: %d\n",
1440 blockno, ret);
1441 goto exit;
1443 buf += bsize;
1444 size -= bsize;
1445 blockno++;
1446 } while (bsize > 0);
1448 if (at76_is_505a(fwe->board_type)) {
1449 at76_dbg(DBG_DEVSTART, "200 ms delay for 505a");
1450 schedule_timeout_interruptible(HZ / 5 + 1);
1453 exit:
1454 kfree(block);
1455 if (ret < 0)
1456 dev_printk(KERN_ERR, &udev->dev,
1457 "downloading external firmware failed: %d\n", ret);
1458 return ret;
1461 /* Download internal firmware */
1462 static int at76_load_internal_fw(struct usb_device *udev, struct fwentry *fwe)
1464 int ret;
1465 int need_remap = !at76_is_505a(fwe->board_type);
1467 ret = at76_usbdfu_download(udev, fwe->intfw, fwe->intfw_size,
1468 need_remap ? 0 : 2 * HZ);
1470 if (ret < 0) {
1471 dev_printk(KERN_ERR, &udev->dev,
1472 "downloading internal fw failed with %d\n", ret);
1473 goto exit;
1476 at76_dbg(DBG_DEVSTART, "sending REMAP");
1478 /* no REMAP for 505A (see SF driver) */
1479 if (need_remap) {
1480 ret = at76_remap(udev);
1481 if (ret < 0) {
1482 dev_printk(KERN_ERR, &udev->dev,
1483 "sending REMAP failed with %d\n", ret);
1484 goto exit;
1488 at76_dbg(DBG_DEVSTART, "sleeping for 2 seconds");
1489 schedule_timeout_interruptible(2 * HZ + 1);
1490 usb_reset_device(udev);
1492 exit:
1493 return ret;
1496 static int at76_startup_device(struct at76_priv *priv)
1498 struct at76_card_config *ccfg = &priv->card_config;
1499 int ret;
1501 at76_dbg(DBG_PARAMS,
1502 "%s param: ssid %.*s (%s) mode %s ch %d wep %s key %d "
1503 "keylen %d", wiphy_name(priv->hw->wiphy), priv->essid_size,
1504 priv->essid, hex2str(priv->essid, IW_ESSID_MAX_SIZE),
1505 priv->iw_mode == IW_MODE_ADHOC ? "adhoc" : "infra",
1506 priv->channel, priv->wep_enabled ? "enabled" : "disabled",
1507 priv->wep_key_id, priv->wep_keys_len[priv->wep_key_id]);
1508 at76_dbg(DBG_PARAMS,
1509 "%s param: preamble %s rts %d retry %d frag %d "
1510 "txrate %s auth_mode %d", wiphy_name(priv->hw->wiphy),
1511 preambles[priv->preamble_type], priv->rts_threshold,
1512 priv->short_retry_limit, priv->frag_threshold,
1513 priv->txrate == TX_RATE_1MBIT ? "1MBit" : priv->txrate ==
1514 TX_RATE_2MBIT ? "2MBit" : priv->txrate ==
1515 TX_RATE_5_5MBIT ? "5.5MBit" : priv->txrate ==
1516 TX_RATE_11MBIT ? "11MBit" : priv->txrate ==
1517 TX_RATE_AUTO ? "auto" : "<invalid>", priv->auth_mode);
1518 at76_dbg(DBG_PARAMS,
1519 "%s param: pm_mode %d pm_period %d auth_mode %s "
1520 "scan_times %d %d scan_mode %s",
1521 wiphy_name(priv->hw->wiphy), priv->pm_mode, priv->pm_period,
1522 priv->auth_mode == WLAN_AUTH_OPEN ? "open" : "shared_secret",
1523 priv->scan_min_time, priv->scan_max_time,
1524 priv->scan_mode == SCAN_TYPE_ACTIVE ? "active" : "passive");
1526 memset(ccfg, 0, sizeof(struct at76_card_config));
1527 ccfg->promiscuous_mode = 0;
1528 ccfg->short_retry_limit = priv->short_retry_limit;
1530 if (priv->wep_enabled) {
1531 if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
1532 ccfg->encryption_type = 2;
1533 else
1534 ccfg->encryption_type = 1;
1536 /* jal: always exclude unencrypted if WEP is active */
1537 ccfg->exclude_unencrypted = 1;
1538 } else {
1539 ccfg->exclude_unencrypted = 0;
1540 ccfg->encryption_type = 0;
1543 ccfg->rts_threshold = cpu_to_le16(priv->rts_threshold);
1544 ccfg->fragmentation_threshold = cpu_to_le16(priv->frag_threshold);
1546 memcpy(ccfg->basic_rate_set, hw_rates, 4);
1547 /* jal: really needed, we do a set_mib for autorate later ??? */
1548 ccfg->auto_rate_fallback = (priv->txrate == TX_RATE_AUTO ? 1 : 0);
1549 ccfg->channel = priv->channel;
1550 ccfg->privacy_invoked = priv->wep_enabled;
1551 memcpy(ccfg->current_ssid, priv->essid, IW_ESSID_MAX_SIZE);
1552 ccfg->ssid_len = priv->essid_size;
1554 ccfg->wep_default_key_id = priv->wep_key_id;
1555 memcpy(ccfg->wep_default_key_value, priv->wep_keys,
1556 sizeof(priv->wep_keys));
1558 ccfg->short_preamble = priv->preamble_type;
1559 ccfg->beacon_period = cpu_to_le16(priv->beacon_period);
1561 ret = at76_set_card_command(priv->udev, CMD_STARTUP, &priv->card_config,
1562 sizeof(struct at76_card_config));
1563 if (ret < 0) {
1564 printk(KERN_ERR "%s: at76_set_card_command failed: %d\n",
1565 wiphy_name(priv->hw->wiphy), ret);
1566 return ret;
1569 at76_wait_completion(priv, CMD_STARTUP);
1571 /* remove BSSID from previous run */
1572 memset(priv->bssid, 0, ETH_ALEN);
1574 if (at76_set_radio(priv, 1) == 1)
1575 at76_wait_completion(priv, CMD_RADIO_ON);
1577 ret = at76_set_preamble(priv, priv->preamble_type);
1578 if (ret < 0)
1579 return ret;
1581 ret = at76_set_frag(priv, priv->frag_threshold);
1582 if (ret < 0)
1583 return ret;
1585 ret = at76_set_rts(priv, priv->rts_threshold);
1586 if (ret < 0)
1587 return ret;
1589 ret = at76_set_autorate_fallback(priv,
1590 priv->txrate == TX_RATE_AUTO ? 1 : 0);
1591 if (ret < 0)
1592 return ret;
1594 ret = at76_set_pm_mode(priv);
1595 if (ret < 0)
1596 return ret;
1598 if (at76_debug & DBG_MIB) {
1599 at76_dump_mib_mac(priv);
1600 at76_dump_mib_mac_addr(priv);
1601 at76_dump_mib_mac_mgmt(priv);
1602 at76_dump_mib_mac_wep(priv);
1603 at76_dump_mib_mdomain(priv);
1604 at76_dump_mib_phy(priv);
1605 at76_dump_mib_local(priv);
1608 return 0;
1611 /* Enable or disable promiscuous mode */
1612 static void at76_work_set_promisc(struct work_struct *work)
1614 struct at76_priv *priv = container_of(work, struct at76_priv,
1615 work_set_promisc);
1616 int ret = 0;
1618 mutex_lock(&priv->mtx);
1620 priv->mib_buf.type = MIB_LOCAL;
1621 priv->mib_buf.size = 1;
1622 priv->mib_buf.index = offsetof(struct mib_local, promiscuous_mode);
1623 priv->mib_buf.data.byte = priv->promisc ? 1 : 0;
1625 ret = at76_set_mib(priv, &priv->mib_buf);
1626 if (ret < 0)
1627 printk(KERN_ERR "%s: set_mib (promiscuous_mode) failed: %d\n",
1628 wiphy_name(priv->hw->wiphy), ret);
1630 mutex_unlock(&priv->mtx);
1633 /* Submit Rx urb back to the device */
1634 static void at76_work_submit_rx(struct work_struct *work)
1636 struct at76_priv *priv = container_of(work, struct at76_priv,
1637 work_submit_rx);
1639 mutex_lock(&priv->mtx);
1640 at76_submit_rx_urb(priv);
1641 mutex_unlock(&priv->mtx);
1644 static void at76_rx_tasklet(unsigned long param)
1646 struct urb *urb = (struct urb *)param;
1647 struct at76_priv *priv = urb->context;
1648 struct at76_rx_buffer *buf;
1649 struct ieee80211_rx_status rx_status = { 0 };
1651 if (priv->device_unplugged) {
1652 at76_dbg(DBG_DEVSTART, "device unplugged");
1653 if (urb)
1654 at76_dbg(DBG_DEVSTART, "urb status %d", urb->status);
1655 return;
1658 if (!priv->rx_skb || !priv->rx_skb->data)
1659 return;
1661 buf = (struct at76_rx_buffer *)priv->rx_skb->data;
1663 if (urb->status != 0) {
1664 if (urb->status != -ENOENT && urb->status != -ECONNRESET)
1665 at76_dbg(DBG_URB,
1666 "%s %s: - nonzero Rx bulk status received: %d",
1667 __func__, wiphy_name(priv->hw->wiphy),
1668 urb->status);
1669 return;
1672 at76_dbg(DBG_RX_ATMEL_HDR,
1673 "%s: rx frame: rate %d rssi %d noise %d link %d",
1674 wiphy_name(priv->hw->wiphy), buf->rx_rate, buf->rssi,
1675 buf->noise_level, buf->link_quality);
1677 skb_trim(priv->rx_skb, le16_to_cpu(buf->wlength) + AT76_RX_HDRLEN);
1678 at76_dbg_dump(DBG_RX_DATA, &priv->rx_skb->data[AT76_RX_HDRLEN],
1679 priv->rx_skb->len, "RX: len=%d", (int)(priv->rx_skb->len - AT76_RX_HDRLEN));
1681 rx_status.signal = buf->rssi;
1682 rx_status.rate_idx = buf->rx_rate; /* FIXME: is rate_idx still present in structure? */
1683 rx_status.flag |= RX_FLAG_DECRYPTED;
1684 rx_status.flag |= RX_FLAG_IV_STRIPPED;
1686 skb_pull(priv->rx_skb, AT76_RX_HDRLEN);
1687 at76_dbg(DBG_MAC80211, "calling ieee80211_rx_irqsafe(): %d/%d",
1688 priv->rx_skb->len, priv->rx_skb->data_len);
1689 ieee80211_rx_irqsafe(priv->hw, priv->rx_skb, &rx_status);
1691 /* Use a new skb for the next receive */
1692 priv->rx_skb = NULL;
1694 at76_submit_rx_urb(priv);
1697 /* Load firmware into kernel memory and parse it */
1698 static struct fwentry *at76_load_firmware(struct usb_device *udev,
1699 enum board_type board_type)
1701 int ret;
1702 char *str;
1703 struct at76_fw_header *fwh;
1704 struct fwentry *fwe = &firmwares[board_type];
1706 mutex_lock(&fw_mutex);
1708 if (fwe->loaded) {
1709 at76_dbg(DBG_FW, "re-using previously loaded fw");
1710 goto exit;
1713 at76_dbg(DBG_FW, "downloading firmware %s", fwe->fwname);
1714 ret = request_firmware(&fwe->fw, fwe->fwname, &udev->dev);
1715 if (ret < 0) {
1716 dev_printk(KERN_ERR, &udev->dev, "firmware %s not found!\n",
1717 fwe->fwname);
1718 dev_printk(KERN_ERR, &udev->dev,
1719 "you may need to download the firmware from "
1720 "http://developer.berlios.de/projects/at76c503a/\n");
1721 goto exit;
1724 at76_dbg(DBG_FW, "got it.");
1725 fwh = (struct at76_fw_header *)(fwe->fw->data);
1727 if (fwe->fw->size <= sizeof(*fwh)) {
1728 dev_printk(KERN_ERR, &udev->dev,
1729 "firmware is too short (0x%zx)\n", fwe->fw->size);
1730 goto exit;
1733 /* CRC currently not checked */
1734 fwe->board_type = le32_to_cpu(fwh->board_type);
1735 if (fwe->board_type != board_type) {
1736 dev_printk(KERN_ERR, &udev->dev,
1737 "board type mismatch, requested %u, got %u\n",
1738 board_type, fwe->board_type);
1739 goto exit;
1742 fwe->fw_version.major = fwh->major;
1743 fwe->fw_version.minor = fwh->minor;
1744 fwe->fw_version.patch = fwh->patch;
1745 fwe->fw_version.build = fwh->build;
1747 str = (char *)fwh + le32_to_cpu(fwh->str_offset);
1748 fwe->intfw = (u8 *)fwh + le32_to_cpu(fwh->int_fw_offset);
1749 fwe->intfw_size = le32_to_cpu(fwh->int_fw_len);
1750 fwe->extfw = (u8 *)fwh + le32_to_cpu(fwh->ext_fw_offset);
1751 fwe->extfw_size = le32_to_cpu(fwh->ext_fw_len);
1753 fwe->loaded = 1;
1755 dev_printk(KERN_DEBUG, &udev->dev,
1756 "using firmware %s (version %d.%d.%d-%d)\n",
1757 fwe->fwname, fwh->major, fwh->minor, fwh->patch, fwh->build);
1759 at76_dbg(DBG_DEVSTART, "board %u, int %d:%d, ext %d:%d", board_type,
1760 le32_to_cpu(fwh->int_fw_offset), le32_to_cpu(fwh->int_fw_len),
1761 le32_to_cpu(fwh->ext_fw_offset), le32_to_cpu(fwh->ext_fw_len));
1762 at76_dbg(DBG_DEVSTART, "firmware id %s", str);
1764 exit:
1765 mutex_unlock(&fw_mutex);
1767 if (fwe->loaded)
1768 return fwe;
1769 else
1770 return NULL;
1773 static void at76_mac80211_tx_callback(struct urb *urb)
1775 struct at76_priv *priv = urb->context;
1776 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(priv->tx_skb);
1778 at76_dbg(DBG_MAC80211, "%s()", __func__);
1780 switch (urb->status) {
1781 case 0:
1782 /* success */
1783 info->flags |= IEEE80211_TX_STAT_ACK; /* FIXME: is the frame really ACKed when tx_callback is called ? */
1784 break;
1785 case -ENOENT:
1786 case -ECONNRESET:
1787 /* fail, urb has been unlinked */
1788 /* FIXME: add error message */
1789 break;
1790 default:
1791 at76_dbg(DBG_URB, "%s - nonzero tx status received: %d",
1792 __func__, urb->status);
1793 break;
1796 memset(&info->status, 0, sizeof(info->status));
1798 ieee80211_tx_status_irqsafe(priv->hw, priv->tx_skb);
1800 priv->tx_skb = NULL;
1802 ieee80211_wake_queues(priv->hw);
1805 static int at76_mac80211_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1807 struct at76_priv *priv = hw->priv;
1808 struct at76_tx_buffer *tx_buffer = priv->bulk_out_buffer;
1809 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1810 int padding, submit_len, ret;
1812 at76_dbg(DBG_MAC80211, "%s()", __func__);
1814 if (priv->tx_urb->status == -EINPROGRESS) {
1815 printk(KERN_ERR "%s: %s called while tx urb is pending\n",
1816 wiphy_name(priv->hw->wiphy), __func__);
1817 return NETDEV_TX_BUSY;
1820 ieee80211_stop_queues(hw);
1822 at76_ledtrig_tx_activity(); /* tell ledtrigger we send a packet */
1824 WARN_ON(priv->tx_skb != NULL);
1826 priv->tx_skb = skb;
1827 padding = at76_calc_padding(skb->len);
1828 submit_len = AT76_TX_HDRLEN + skb->len + padding;
1830 /* setup 'Atmel' header */
1831 memset(tx_buffer, 0, sizeof(*tx_buffer));
1832 tx_buffer->padding = padding;
1833 tx_buffer->wlength = cpu_to_le16(skb->len);
1834 tx_buffer->tx_rate = ieee80211_get_tx_rate(hw, info)->hw_value;
1835 if (FIRMWARE_IS_WPA(priv->fw_version) && info->control.hw_key) {
1836 tx_buffer->key_id = (info->control.hw_key->keyidx);
1837 tx_buffer->cipher_type = priv->keys[info->control.hw_key->keyidx].cipher;
1838 tx_buffer->cipher_length = priv->keys[info->control.hw_key->keyidx].keylen;
1839 tx_buffer->reserved = 0;
1840 } else {
1841 tx_buffer->key_id = 0;
1842 tx_buffer->cipher_type = 0;
1843 tx_buffer->cipher_length = 0;
1844 tx_buffer->reserved = 0;
1846 /* memset(tx_buffer->reserved, 0, sizeof(tx_buffer->reserved)); */
1847 memcpy(tx_buffer->packet, skb->data, skb->len);
1849 at76_dbg(DBG_TX_DATA, "%s tx: wlen 0x%x pad 0x%x rate %d hdr",
1850 wiphy_name(priv->hw->wiphy), le16_to_cpu(tx_buffer->wlength),
1851 tx_buffer->padding, tx_buffer->tx_rate);
1853 /* send stuff */
1854 at76_dbg_dump(DBG_TX_DATA_CONTENT, tx_buffer, submit_len,
1855 "%s(): tx_buffer %d bytes:", __func__, submit_len);
1856 usb_fill_bulk_urb(priv->tx_urb, priv->udev, priv->tx_pipe, tx_buffer,
1857 submit_len, at76_mac80211_tx_callback, priv);
1858 ret = usb_submit_urb(priv->tx_urb, GFP_ATOMIC);
1859 if (ret) {
1860 printk(KERN_ERR "%s: error in tx submit urb: %d\n",
1861 wiphy_name(priv->hw->wiphy), ret);
1862 if (ret == -EINVAL)
1863 printk(KERN_ERR
1864 "%s: -EINVAL: tx urb %p hcpriv %p complete %p\n",
1865 wiphy_name(priv->hw->wiphy), priv->tx_urb,
1866 priv->tx_urb->hcpriv, priv->tx_urb->complete);
1869 return 0;
1872 static int at76_mac80211_start(struct ieee80211_hw *hw)
1874 struct at76_priv *priv = hw->priv;
1875 int ret;
1877 at76_dbg(DBG_MAC80211, "%s()", __func__);
1879 mutex_lock(&priv->mtx);
1881 ret = at76_submit_rx_urb(priv);
1882 if (ret < 0) {
1883 printk(KERN_ERR "%s: open: submit_rx_urb failed: %d\n",
1884 wiphy_name(priv->hw->wiphy), ret);
1885 goto error;
1888 at76_startup_device(priv);
1890 at76_start_monitor(priv);
1892 error:
1893 mutex_unlock(&priv->mtx);
1895 return 0;
1898 static void at76_mac80211_stop(struct ieee80211_hw *hw)
1900 struct at76_priv *priv = hw->priv;
1902 at76_dbg(DBG_MAC80211, "%s()", __func__);
1904 mutex_lock(&priv->mtx);
1906 if (!priv->device_unplugged) {
1907 /* We are called by "ifconfig ethX down", not because the
1908 * device is not available anymore. */
1909 if (at76_set_radio(priv, 0) == 1)
1910 at76_wait_completion(priv, CMD_RADIO_ON);
1912 /* We unlink rx_urb because at76_open() re-submits it.
1913 * If unplugged, at76_delete_device() takes care of it. */
1914 usb_kill_urb(priv->rx_urb);
1917 mutex_unlock(&priv->mtx);
1920 static int at76_add_interface(struct ieee80211_hw *hw,
1921 struct ieee80211_if_init_conf *conf)
1923 struct at76_priv *priv = hw->priv;
1924 int ret = 0;
1926 at76_dbg(DBG_MAC80211, "%s()", __func__);
1928 mutex_lock(&priv->mtx);
1930 switch (conf->type) {
1931 case NL80211_IFTYPE_STATION:
1932 priv->iw_mode = IW_MODE_INFRA;
1933 break;
1934 default:
1935 ret = -EOPNOTSUPP;
1936 goto exit;
1939 exit:
1940 mutex_unlock(&priv->mtx);
1942 return ret;
1945 static void at76_remove_interface(struct ieee80211_hw *hw,
1946 struct ieee80211_if_init_conf *conf)
1948 at76_dbg(DBG_MAC80211, "%s()", __func__);
1951 static int at76_join(struct at76_priv *priv)
1953 struct at76_req_join join;
1954 int ret;
1956 memset(&join, 0, sizeof(struct at76_req_join));
1957 memcpy(join.essid, priv->essid, priv->essid_size);
1958 join.essid_size = priv->essid_size;
1959 memcpy(join.bssid, priv->bssid, ETH_ALEN);
1960 join.bss_type = INFRASTRUCTURE_MODE;
1961 join.channel = priv->channel;
1962 join.timeout = cpu_to_le16(2000);
1964 at76_dbg(DBG_MAC80211, "%s: sending CMD_JOIN", __func__);
1965 ret = at76_set_card_command(priv->udev, CMD_JOIN, &join,
1966 sizeof(struct at76_req_join));
1968 if (ret < 0) {
1969 printk(KERN_ERR "%s: at76_set_card_command failed: %d\n",
1970 wiphy_name(priv->hw->wiphy), ret);
1971 return 0;
1974 ret = at76_wait_completion(priv, CMD_JOIN);
1975 at76_dbg(DBG_MAC80211, "%s: CMD_JOIN returned: 0x%02x", __func__, ret);
1976 if (ret != CMD_STATUS_COMPLETE) {
1977 printk(KERN_ERR "%s: at76_wait_completion failed: %d\n",
1978 wiphy_name(priv->hw->wiphy), ret);
1979 return 0;
1982 at76_set_tkip_bssid(priv, priv->bssid);
1983 at76_set_pm_mode(priv);
1985 return 0;
1988 static void at76_dwork_hw_scan(struct work_struct *work)
1990 struct at76_priv *priv = container_of(work, struct at76_priv,
1991 dwork_hw_scan.work);
1992 int ret;
1994 ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1995 at76_dbg(DBG_MAC80211, "%s: CMD_SCAN status 0x%02x", __func__, ret);
1997 /* FIXME: add maximum time for scan to complete */
1999 if (ret != CMD_STATUS_COMPLETE) {
2000 queue_delayed_work(priv->hw->workqueue, &priv->dwork_hw_scan,
2001 SCAN_POLL_INTERVAL);
2002 goto exit;
2005 ieee80211_scan_completed(priv->hw);
2007 if (is_valid_ether_addr(priv->bssid)) {
2008 ieee80211_wake_queues(priv->hw);
2009 at76_join(priv);
2012 ieee80211_wake_queues(priv->hw);
2014 exit:
2015 return;
2018 static int at76_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
2020 struct at76_priv *priv = hw->priv;
2021 struct at76_req_scan scan;
2022 int ret;
2024 at76_dbg(DBG_MAC80211, "%s():", __func__);
2025 at76_dbg_dump(DBG_MAC80211, ssid, len, "ssid %zd bytes:", len);
2027 mutex_lock(&priv->mtx);
2029 ieee80211_stop_queues(hw);
2031 memset(&scan, 0, sizeof(struct at76_req_scan));
2032 memset(scan.bssid, 0xFF, ETH_ALEN);
2033 scan.scan_type = SCAN_TYPE_ACTIVE;
2034 if (priv->essid_size > 0) {
2035 memcpy(scan.essid, ssid, len);
2036 scan.essid_size = len;
2038 scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
2039 scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
2040 scan.probe_delay = cpu_to_le16(priv->scan_min_time * 1000);
2041 scan.international_scan = 0;
2043 at76_dbg(DBG_MAC80211, "%s: sending CMD_SCAN", __func__);
2044 ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
2046 if (ret < 0) {
2047 err("CMD_SCAN failed: %d", ret);
2048 goto exit;
2051 queue_delayed_work(priv->hw->workqueue, &priv->dwork_hw_scan,
2052 SCAN_POLL_INTERVAL);
2054 exit:
2055 mutex_unlock(&priv->mtx);
2057 return 0;
2060 static int at76_config(struct ieee80211_hw *hw, u32 changed)
2062 struct at76_priv *priv = hw->priv;
2063 struct ieee80211_conf *conf = &hw->conf;
2065 at76_dbg(DBG_MAC80211, "%s(): channel %d radio %d",
2066 __func__, conf->channel->hw_value, conf->radio_enabled);
2067 at76_dbg_dump(DBG_MAC80211, priv->essid, priv->essid_size, "ssid:");
2068 at76_dbg_dump(DBG_MAC80211, priv->bssid, ETH_ALEN, "bssid:");
2070 mutex_lock(&priv->mtx);
2072 priv->channel = conf->channel->hw_value;
2074 if (is_valid_ether_addr(priv->bssid)) {
2075 at76_join(priv);
2076 ieee80211_wake_queues(priv->hw);
2077 } else {
2078 ieee80211_stop_queues(priv->hw);
2079 at76_start_monitor(priv);
2082 mutex_unlock(&priv->mtx);
2084 return 0;
2087 static int at76_config_interface(struct ieee80211_hw *hw,
2088 struct ieee80211_vif *vif,
2089 struct ieee80211_if_conf *conf)
2091 struct at76_priv *priv = hw->priv;
2093 at76_dbg_dump(DBG_MAC80211, conf->bssid, ETH_ALEN, "bssid:");
2095 mutex_lock(&priv->mtx);
2097 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
2098 // memcpy(priv->essid, conf->ssid, conf->ssid_len);
2099 // priv->essid_size = conf->ssid_len;
2101 if (is_valid_ether_addr(priv->bssid)) {
2102 /* mac80211 is joining a bss */
2103 ieee80211_wake_queues(priv->hw);
2104 at76_join(priv);
2105 } else
2106 ieee80211_stop_queues(priv->hw);
2108 mutex_unlock(&priv->mtx);
2110 return 0;
2113 /* must be atomic */
2114 static void at76_configure_filter(struct ieee80211_hw *hw,
2115 unsigned int changed_flags,
2116 unsigned int *total_flags, int mc_count,
2117 struct dev_addr_list *mc_list)
2119 struct at76_priv *priv = hw->priv;
2120 int flags;
2122 at76_dbg(DBG_MAC80211, "%s(): changed_flags=0x%08x "
2123 "total_flags=0x%08x mc_count=%d",
2124 __func__, changed_flags, *total_flags, mc_count);
2126 flags = changed_flags & AT76_SUPPORTED_FILTERS;
2127 *total_flags = AT76_SUPPORTED_FILTERS;
2129 /* FIXME: access to priv->promisc should be protected with
2130 * priv->mtx, but it's impossible because this function needs to be
2131 * atomic */
2133 if (flags && !priv->promisc) {
2134 /* mac80211 wants us to enable promiscuous mode */
2135 priv->promisc = 1;
2136 } else if (!flags && priv->promisc) {
2137 /* we need to disable promiscuous mode */
2138 priv->promisc = 0;
2139 } else
2140 return;
2142 queue_work(hw->workqueue, &priv->work_set_promisc);
2145 static int at76_set_key_oldfw(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2146 const u8 *local_address, const u8 *address,
2147 struct ieee80211_key_conf *key)
2149 struct at76_priv *priv = hw->priv;
2151 int i;
2153 at76_dbg(DBG_MAC80211, "%s(): cmd %d key->alg %d key->keyidx %d "
2154 "key->keylen %d",
2155 __func__, cmd, key->alg, key->keyidx, key->keylen);
2157 if (key->alg != ALG_WEP)
2158 return -EOPNOTSUPP;
2160 key->hw_key_idx = key->keyidx;
2162 mutex_lock(&priv->mtx);
2164 switch (cmd) {
2165 case SET_KEY:
2166 memcpy(priv->wep_keys[key->keyidx], key->key, key->keylen);
2167 priv->wep_keys_len[key->keyidx] = key->keylen;
2169 /* FIXME: find out how to do this properly */
2170 priv->wep_key_id = key->keyidx;
2172 break;
2173 case DISABLE_KEY:
2174 default:
2175 priv->wep_keys_len[key->keyidx] = 0;
2176 break;
2179 priv->wep_enabled = 0;
2181 for (i = 0; i < WEP_KEYS; i++) {
2182 if (priv->wep_keys_len[i] != 0)
2183 priv->wep_enabled = 1;
2186 at76_startup_device(priv);
2188 mutex_unlock(&priv->mtx);
2190 return 0;
2193 static int at76_set_key_newfw(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2194 const u8 *local_address, const u8 *address,
2195 struct ieee80211_key_conf *key)
2197 struct at76_priv *priv = hw->priv;
2198 int ret = -EOPNOTSUPP;
2200 at76_dbg(DBG_MAC80211, "%s(): cmd %d key->alg %d key->keyidx %d "
2201 "key->keylen %d",
2202 __func__, cmd, key->alg, key->keyidx, key->keylen);
2204 mutex_lock(&priv->mtx);
2206 priv->mib_buf.type = MIB_MAC_ENCRYPTION;
2208 if (cmd == DISABLE_KEY) {
2209 priv->mib_buf.size = CIPHER_KEY_LEN;
2210 priv->mib_buf.index = offsetof(struct mib_mac_encryption,
2211 cipher_default_keyvalue[key->keyidx]);
2212 memset(priv->mib_buf.data.data, 0, CIPHER_KEY_LEN);
2213 if (at76_set_mib(priv, &priv->mib_buf) != CMD_STATUS_COMPLETE)
2214 ret = -EOPNOTSUPP; /* -EIO would be probably better */
2215 else {
2217 priv->keys[key->keyidx].cipher = CIPHER_NONE;
2218 priv->keys[key->keyidx].keylen = 0;
2220 if (priv->default_group_key == key->keyidx)
2221 priv->default_group_key = 0xff;
2223 if (priv->default_pairwise_key == key->keyidx)
2224 priv->default_pairwise_key = 0xff;
2225 /* If default pairwise key is removed, fall back to
2226 * group key? */
2227 ret = 0;
2228 goto exit;
2231 if (cmd == SET_KEY) {
2232 /* store key into MIB */
2233 priv->mib_buf.size = CIPHER_KEY_LEN;
2234 priv->mib_buf.index = offsetof(struct mib_mac_encryption,
2235 cipher_default_keyvalue[key->keyidx]);
2236 memset(priv->mib_buf.data.data, 0, CIPHER_KEY_LEN);
2237 memcpy(priv->mib_buf.data.data, key->key, key->keylen);
2239 switch (key->alg) {
2240 case ALG_WEP:
2241 if (key->keylen == 5) {
2242 priv->keys[key->keyidx].cipher =
2243 CIPHER_WEP64;
2244 priv->keys[key->keyidx].keylen = 8;
2245 } else if (key->keylen == 13) {
2246 priv->keys[key->keyidx].cipher =
2247 CIPHER_WEP128;
2248 /* Firmware needs this */
2249 priv->keys[key->keyidx].keylen = 8;
2250 } else {
2251 ret = -EOPNOTSUPP;
2252 goto exit;
2254 break;
2255 case ALG_TKIP:
2256 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2257 priv->keys[key->keyidx].cipher = CIPHER_TKIP;
2258 priv->keys[key->keyidx].keylen = 12;
2259 break;
2261 case ALG_CCMP:
2262 if (!at76_is_505a(priv->board_type)) {
2263 ret = -EOPNOTSUPP;
2264 goto exit;
2266 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2267 priv->keys[key->keyidx].cipher = CIPHER_CCMP;
2268 priv->keys[key->keyidx].keylen = 16;
2269 break;
2271 default:
2272 ret = -EOPNOTSUPP;
2273 goto exit;
2277 priv->mib_buf.data.data[38] = priv->keys[key->keyidx].cipher;
2278 priv->mib_buf.data.data[39] = 1; /* Taken from atmelwlandriver,
2279 not documented */
2281 if (is_valid_ether_addr(address))
2282 /* Pairwise key */
2283 priv->mib_buf.data.data[39] |= (KEY_PAIRWISE | KEY_TX);
2284 else if (is_broadcast_ether_addr(address))
2285 /* Group key */
2286 priv->mib_buf.data.data[39] |= (KEY_TX);
2287 else /* Key used only for transmission ??? */
2288 priv->mib_buf.data.data[39] |= (KEY_TX);
2290 if (at76_set_mib(priv, &priv->mib_buf) !=
2291 CMD_STATUS_COMPLETE) {
2292 ret = -EOPNOTSUPP; /* -EIO would be probably better */
2293 goto exit;
2296 if ((key->alg == ALG_TKIP) || (key->alg == ALG_CCMP))
2297 at76_reset_rsc(priv);
2299 key->hw_key_idx = key->keyidx;
2301 /* Set up default keys */
2302 if (is_broadcast_ether_addr(address))
2303 priv->default_group_key = key->keyidx;
2304 if (is_valid_ether_addr(address))
2305 priv->default_pairwise_key = key->keyidx;
2307 /* Set up encryption MIBs */
2309 /* first block of settings */
2310 priv->mib_buf.size = 3;
2311 priv->mib_buf.index = offsetof(struct mib_mac_encryption,
2312 privacy_invoked);
2313 priv->mib_buf.data.data[0] = 1; /* privacy_invoked */
2314 priv->mib_buf.data.data[1] = priv->default_pairwise_key;
2315 priv->mib_buf.data.data[2] = priv->default_group_key;
2317 if ((ret = at76_set_mib(priv, &priv->mib_buf)) !=
2318 CMD_STATUS_COMPLETE)
2319 goto exit;
2321 /* second block of settings */
2322 priv->mib_buf.size = 3;
2323 priv->mib_buf.index = offsetof(struct mib_mac_encryption,
2324 exclude_unencrypted);
2325 priv->mib_buf.data.data[0] = 1; /* exclude_unencrypted */
2326 priv->mib_buf.data.data[1] = 0; /* wep_encryption_type */
2327 priv->mib_buf.data.data[2] = 0; /* ckip_key_permutation */
2329 if ((ret = at76_set_mib(priv, &priv->mib_buf)) !=
2330 CMD_STATUS_COMPLETE)
2331 goto exit;
2332 ret = 0;
2334 exit:
2335 at76_dump_mib_mac_encryption(priv);
2336 mutex_unlock(&priv->mtx);
2337 return ret;
2340 static int at76_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2341 const u8 *local_address, const u8 *address,
2342 struct ieee80211_key_conf *key)
2344 struct at76_priv *priv = hw->priv;
2346 // int i;
2348 at76_dbg(DBG_MAC80211, "%s(): cmd %d key->alg %d key->keyidx %d "
2349 "key->keylen %d",
2350 __func__, cmd, key->alg, key->keyidx, key->keylen);
2352 if (FIRMWARE_IS_WPA(priv->fw_version))
2353 return at76_set_key_newfw(hw, cmd, local_address, address, key);
2354 else
2355 return at76_set_key_oldfw(hw, cmd, local_address, address, key);
2359 static const struct ieee80211_ops at76_ops = {
2360 .tx = at76_mac80211_tx,
2361 .add_interface = at76_add_interface,
2362 .remove_interface = at76_remove_interface,
2363 .config = at76_config,
2364 .config_interface = at76_config_interface,
2365 .configure_filter = at76_configure_filter,
2366 .start = at76_mac80211_start,
2367 .stop = at76_mac80211_stop,
2368 .hw_scan = at76_hw_scan,
2369 .set_key = at76_set_key,
2372 /* Allocate network device and initialize private data */
2373 static struct at76_priv *at76_alloc_new_device(struct usb_device *udev)
2375 struct ieee80211_hw *hw;
2376 struct at76_priv *priv;
2378 hw = ieee80211_alloc_hw(sizeof(struct at76_priv), &at76_ops);
2379 if (!hw) {
2380 printk(KERN_ERR DRIVER_NAME ": could not register"
2381 " ieee80211_hw\n");
2382 return NULL;
2385 priv = hw->priv;
2386 priv->hw = hw;
2388 priv->udev = udev;
2390 mutex_init(&priv->mtx);
2391 INIT_WORK(&priv->work_set_promisc, at76_work_set_promisc);
2392 INIT_WORK(&priv->work_submit_rx, at76_work_submit_rx);
2393 INIT_DELAYED_WORK(&priv->dwork_hw_scan, at76_dwork_hw_scan);
2395 priv->rx_tasklet.func = at76_rx_tasklet;
2396 priv->rx_tasklet.data = 0;
2398 priv->pm_mode = AT76_PM_OFF;
2399 priv->pm_period = 0;
2401 /* unit us */
2402 priv->hw->channel_change_time = 100000;
2404 return priv;
2407 static int at76_alloc_urbs(struct at76_priv *priv,
2408 struct usb_interface *interface)
2410 struct usb_endpoint_descriptor *endpoint, *ep_in, *ep_out;
2411 int i;
2412 int buffer_size;
2413 struct usb_host_interface *iface_desc;
2415 at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2417 at76_dbg(DBG_URB, "%s: NumEndpoints %d ", __func__,
2418 interface->altsetting[0].desc.bNumEndpoints);
2420 ep_in = NULL;
2421 ep_out = NULL;
2422 iface_desc = interface->cur_altsetting;
2423 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2424 endpoint = &iface_desc->endpoint[i].desc;
2426 at76_dbg(DBG_URB, "%s: %d. endpoint: addr 0x%x attr 0x%x",
2427 __func__, i, endpoint->bEndpointAddress,
2428 endpoint->bmAttributes);
2430 if (!ep_in && usb_endpoint_is_bulk_in(endpoint))
2431 ep_in = endpoint;
2433 if (!ep_out && usb_endpoint_is_bulk_out(endpoint))
2434 ep_out = endpoint;
2437 if (!ep_in || !ep_out) {
2438 dev_printk(KERN_ERR, &interface->dev,
2439 "bulk endpoints missing\n");
2440 return -ENXIO;
2443 priv->rx_pipe = usb_rcvbulkpipe(priv->udev, ep_in->bEndpointAddress);
2444 priv->tx_pipe = usb_sndbulkpipe(priv->udev, ep_out->bEndpointAddress);
2446 priv->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2447 priv->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2448 if (!priv->rx_urb || !priv->tx_urb) {
2449 dev_printk(KERN_ERR, &interface->dev, "cannot allocate URB\n");
2450 return -ENOMEM;
2453 buffer_size = sizeof(struct at76_tx_buffer) + MAX_PADDING_SIZE;
2454 priv->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
2455 if (!priv->bulk_out_buffer) {
2456 dev_printk(KERN_ERR, &interface->dev,
2457 "cannot allocate output buffer\n");
2458 return -ENOMEM;
2461 at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2463 return 0;
2466 static struct ieee80211_rate at76_rates[] = {
2467 { .bitrate = 10, .hw_value = TX_RATE_1MBIT, },
2468 { .bitrate = 20, .hw_value = TX_RATE_2MBIT, },
2469 { .bitrate = 55, .hw_value = TX_RATE_5_5MBIT, },
2470 { .bitrate = 110, .hw_value = TX_RATE_11MBIT, },
2473 static struct ieee80211_channel at76_channels[] = {
2474 { .center_freq = 2412, .hw_value = 1 },
2475 { .center_freq = 2417, .hw_value = 2 },
2476 { .center_freq = 2422, .hw_value = 3 },
2477 { .center_freq = 2427, .hw_value = 4 },
2478 { .center_freq = 2432, .hw_value = 5 },
2479 { .center_freq = 2437, .hw_value = 6 },
2480 { .center_freq = 2442, .hw_value = 7 },
2481 { .center_freq = 2447, .hw_value = 8 },
2482 { .center_freq = 2452, .hw_value = 9 },
2483 { .center_freq = 2457, .hw_value = 10 },
2484 { .center_freq = 2462, .hw_value = 11 },
2485 { .center_freq = 2467, .hw_value = 12 },
2486 { .center_freq = 2472, .hw_value = 13 },
2487 { .center_freq = 2484, .hw_value = 14 }
2490 static struct ieee80211_supported_band at76_supported_band = {
2491 .channels = at76_channels,
2492 .n_channels = ARRAY_SIZE(at76_channels),
2493 .bitrates = at76_rates,
2494 .n_bitrates = ARRAY_SIZE(at76_rates),
2497 /* Register network device and initialize the hardware */
2498 static int at76_init_new_device(struct at76_priv *priv,
2499 struct usb_interface *interface)
2501 int ret;
2503 /* set up the endpoint information */
2504 /* check out the endpoints */
2506 at76_dbg(DBG_DEVSTART, "USB interface: %d endpoints",
2507 interface->cur_altsetting->desc.bNumEndpoints);
2509 ret = at76_alloc_urbs(priv, interface);
2510 if (ret < 0)
2511 goto exit;
2513 /* MAC address */
2514 ret = at76_get_hw_config(priv);
2515 if (ret < 0) {
2516 dev_printk(KERN_ERR, &interface->dev,
2517 "cannot get MAC address\n");
2518 goto exit;
2521 priv->domain = at76_get_reg_domain(priv->regulatory_domain);
2523 priv->channel = DEF_CHANNEL;
2524 priv->iw_mode = IW_MODE_INFRA;
2525 priv->rts_threshold = DEF_RTS_THRESHOLD;
2526 priv->frag_threshold = DEF_FRAG_THRESHOLD;
2527 priv->short_retry_limit = DEF_SHORT_RETRY_LIMIT;
2528 priv->txrate = TX_RATE_AUTO;
2529 priv->preamble_type = PREAMBLE_TYPE_LONG;
2530 priv->beacon_period = 100;
2531 priv->auth_mode = WLAN_AUTH_OPEN;
2532 priv->scan_min_time = DEF_SCAN_MIN_TIME;
2533 priv->scan_max_time = DEF_SCAN_MAX_TIME;
2534 priv->scan_mode = SCAN_TYPE_ACTIVE;
2535 priv->default_pairwise_key = 0xff;
2536 priv->default_group_key = 0xff;
2538 /* mac80211 initialisation */
2539 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &at76_supported_band;
2541 if (FIRMWARE_IS_WPA(priv->fw_version) &&
2542 (at76_is_503rfmd(priv->board_type) ||
2543 at76_is_505(priv->board_type)))
2544 priv->hw->flags = IEEE80211_HW_SIGNAL_UNSPEC;
2545 else
2546 priv->hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
2547 IEEE80211_HW_SIGNAL_UNSPEC;
2549 priv->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
2551 SET_IEEE80211_DEV(priv->hw, &interface->dev);
2552 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
2554 ret = ieee80211_register_hw(priv->hw);
2555 if (ret) {
2556 printk(KERN_ERR "cannot register mac80211 hw (status %d)!\n",
2557 ret);
2558 goto exit;
2561 priv->mac80211_registered = 1;
2563 printk(KERN_INFO "%s: USB %s, MAC %s, firmware %d.%d.%d-%d\n",
2564 wiphy_name(priv->hw->wiphy),
2565 interface->dev.bus_id, mac2str(priv->mac_addr),
2566 priv->fw_version.major, priv->fw_version.minor,
2567 priv->fw_version.patch, priv->fw_version.build);
2568 printk(KERN_INFO "%s: regulatory domain 0x%02x: %s\n",
2569 wiphy_name(priv->hw->wiphy),
2570 priv->regulatory_domain, priv->domain->name);
2571 printk(KERN_INFO "%s: WPA support: ", wiphy_name(priv->hw->wiphy));
2572 if (!FIRMWARE_IS_WPA(priv->fw_version))
2573 printk("none\n");
2574 else {
2575 if (!at76_is_505a(priv->board_type))
2576 printk("TKIP\n");
2577 else
2578 printk("TKIP, AES/CCMP\n");
2581 exit:
2582 return ret;
2585 static void at76_delete_device(struct at76_priv *priv)
2587 at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2589 /* The device is gone, don't bother turning it off */
2590 priv->device_unplugged = 1;
2592 if (priv->mac80211_registered)
2593 ieee80211_unregister_hw(priv->hw);
2595 /* assuming we used keventd, it must quiesce too */
2596 flush_scheduled_work();
2598 kfree(priv->bulk_out_buffer);
2600 if (priv->tx_urb) {
2601 usb_kill_urb(priv->tx_urb);
2602 usb_free_urb(priv->tx_urb);
2604 if (priv->rx_urb) {
2605 usb_kill_urb(priv->rx_urb);
2606 usb_free_urb(priv->rx_urb);
2609 at76_dbg(DBG_PROC_ENTRY, "%s: unlinked urbs", __func__);
2611 if (priv->rx_skb)
2612 kfree_skb(priv->rx_skb);
2614 usb_put_dev(priv->udev);
2616 at76_dbg(DBG_PROC_ENTRY, "%s: before freeing priv/ieee80211_hw",
2617 __func__);
2618 ieee80211_free_hw(priv->hw);
2620 at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2623 static int at76_probe(struct usb_interface *interface,
2624 const struct usb_device_id *id)
2626 int ret;
2627 struct at76_priv *priv;
2628 struct fwentry *fwe;
2629 struct usb_device *udev;
2630 int op_mode;
2631 int need_ext_fw = 0;
2632 struct mib_fw_version fwv;
2633 int board_type = (int)id->driver_info;
2635 udev = usb_get_dev(interface_to_usbdev(interface));
2637 /* Load firmware into kernel memory */
2638 fwe = at76_load_firmware(udev, board_type);
2639 if (!fwe) {
2640 ret = -ENOENT;
2641 goto error;
2644 op_mode = at76_get_op_mode(udev);
2646 at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
2648 /* we get OPMODE_NONE with 2.4.23, SMC2662W-AR ???
2649 we get 204 with 2.4.23, Fiberline FL-WL240u (505A+RFMD2958) ??? */
2651 if (op_mode == OPMODE_HW_CONFIG_MODE) {
2652 dev_printk(KERN_ERR, &interface->dev,
2653 "cannot handle a device in HW_CONFIG_MODE\n");
2654 ret = -EBUSY;
2655 goto error;
2658 if (op_mode != OPMODE_NORMAL_NIC_WITH_FLASH
2659 && op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
2660 /* download internal firmware part */
2661 dev_printk(KERN_DEBUG, &interface->dev,
2662 "downloading internal firmware\n");
2663 ret = at76_load_internal_fw(udev, fwe);
2664 if (ret < 0) {
2665 dev_printk(KERN_ERR, &interface->dev,
2666 "error %d downloading internal firmware\n",
2667 ret);
2668 goto error;
2670 usb_put_dev(udev);
2671 return ret;
2674 /* Internal firmware already inside the device. Get firmware
2675 * version to test if external firmware is loaded.
2676 * This works only for newer firmware, e.g. the Intersil 0.90.x
2677 * says "control timeout on ep0in" and subsequent
2678 * at76_get_op_mode() fail too :-( */
2680 /* if version >= 0.100.x.y or device with built-in flash we can
2681 * query the device for the fw version */
2682 if ((fwe->fw_version.major > 0 || fwe->fw_version.minor >= 100)
2683 || (op_mode == OPMODE_NORMAL_NIC_WITH_FLASH)) {
2684 ret = at76_get_mib(udev, MIB_FW_VERSION, &fwv, sizeof(fwv));
2685 if (ret < 0 || (fwv.major | fwv.minor) == 0)
2686 need_ext_fw = 1;
2687 } else
2688 /* No way to check firmware version, reload to be sure */
2689 need_ext_fw = 1;
2691 if (need_ext_fw) {
2692 dev_printk(KERN_DEBUG, &interface->dev,
2693 "downloading external firmware\n");
2695 ret = at76_load_external_fw(udev, fwe);
2696 if (ret)
2697 goto error;
2699 /* Re-check firmware version */
2700 ret = at76_get_mib(udev, MIB_FW_VERSION, &fwv, sizeof(fwv));
2701 if (ret < 0) {
2702 dev_printk(KERN_ERR, &interface->dev,
2703 "error %d getting firmware version\n", ret);
2704 goto error;
2708 priv = at76_alloc_new_device(udev);
2709 if (!priv) {
2710 ret = -ENOMEM;
2711 goto error;
2714 usb_set_intfdata(interface, priv);
2716 memcpy(&priv->fw_version, &fwv, sizeof(struct mib_fw_version));
2717 priv->board_type = board_type;
2719 ret = at76_init_new_device(priv, interface);
2720 if (ret < 0)
2721 at76_delete_device(priv);
2723 return ret;
2725 error:
2726 usb_put_dev(udev);
2727 return ret;
2730 static void at76_disconnect(struct usb_interface *interface)
2732 struct at76_priv *priv;
2734 priv = usb_get_intfdata(interface);
2735 usb_set_intfdata(interface, NULL);
2737 /* Disconnect after loading internal firmware */
2738 if (!priv)
2739 return;
2741 printk(KERN_INFO "%s: disconnecting\n", wiphy_name(priv->hw->wiphy));
2742 at76_delete_device(priv);
2743 dev_printk(KERN_INFO, &interface->dev, "disconnected\n");
2746 /* Structure for registering this driver with the USB subsystem */
2747 static struct usb_driver at76_driver = {
2748 .name = DRIVER_NAME,
2749 .probe = at76_probe,
2750 .disconnect = at76_disconnect,
2751 .id_table = dev_table,
2754 static int __init at76_mod_init(void)
2756 int result;
2758 printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " loading\n");
2760 mutex_init(&fw_mutex);
2762 /* register this driver with the USB subsystem */
2763 result = usb_register(&at76_driver);
2764 if (result < 0)
2765 printk(KERN_ERR DRIVER_NAME
2766 ": usb_register failed (status %d)\n", result);
2768 led_trigger_register_simple("at76_usb-tx", &ledtrig_tx);
2769 return result;
2772 static void __exit at76_mod_exit(void)
2774 int i;
2776 printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " unloading\n");
2777 usb_deregister(&at76_driver);
2778 for (i = 0; i < ARRAY_SIZE(firmwares); i++) {
2779 if (firmwares[i].fw)
2780 release_firmware(firmwares[i].fw);
2782 led_trigger_unregister_simple(ledtrig_tx);
2785 module_param_named(debug, at76_debug, int, 0600);
2786 MODULE_PARM_DESC(debug, "Debugging level");
2788 module_init(at76_mod_init);
2789 module_exit(at76_mod_exit);
2791 MODULE_AUTHOR("Oliver Kurth <oku@masqmail.cx>");
2792 MODULE_AUTHOR("Joerg Albert <joerg.albert@gmx.de>");
2793 MODULE_AUTHOR("Alex <alex@foogod.com>");
2794 MODULE_AUTHOR("Nick Jones");
2795 MODULE_AUTHOR("Balint Seeber <n0_5p4m_p13453@hotmail.com>");
2796 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
2797 MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
2798 MODULE_AUTHOR("Kalle Valo <kalle.valo@iki.fi>");
2799 MODULE_AUTHOR("Milan Plzik <milan.plzik@gmail.com>");
2800 MODULE_DESCRIPTION(DRIVER_DESC);
2801 MODULE_LICENSE("GPL");