Staging: at76_usb: fix up all remaining checkpatch.pl warnings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / at76_usb / at76_usb.c
blob375ba7cc300600fa39290c3f12c6da68af3835a5
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 if (board == BOARD_503_ISL3861 || board == BOARD_503_ISL3863)
304 return 1;
305 return 0;
308 static inline int at76_is_503rfmd(enum board_type board)
310 if (board == BOARD_503 || board == BOARD_503_ACC)
311 return 1;
312 return 0;
315 static inline int at76_is_505(enum board_type board)
317 if (board == BOARD_505 || board == BOARD_505_2958)
318 return 1;
319 return 0;
322 static inline int at76_is_505a(enum board_type board)
324 if (board == BOARD_505A || board == BOARD_505AMX)
325 return 1;
326 return 0;
329 /* Load a block of the first (internal) part of the firmware */
330 static int at76_load_int_fw_block(struct usb_device *udev, int blockno,
331 void *block, int size)
333 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), DFU_DNLOAD,
334 USB_TYPE_CLASS | USB_DIR_OUT |
335 USB_RECIP_INTERFACE, blockno, 0, block, size,
336 USB_CTRL_GET_TIMEOUT);
339 static int at76_dfu_get_status(struct usb_device *udev,
340 struct dfu_status *status)
342 int ret;
344 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATUS,
345 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
346 0, 0, status, sizeof(struct dfu_status),
347 USB_CTRL_GET_TIMEOUT);
348 return ret;
351 static u8 at76_dfu_get_state(struct usb_device *udev, u8 *state)
353 int ret;
355 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATE,
356 USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
357 0, 0, state, 1, USB_CTRL_GET_TIMEOUT);
358 return ret;
361 /* Convert timeout from the DFU status to jiffies */
362 static inline unsigned long at76_get_timeout(struct dfu_status *s)
364 return msecs_to_jiffies((s->poll_timeout[2] << 16)
365 | (s->poll_timeout[1] << 8)
366 | (s->poll_timeout[0]));
369 /* Load internal firmware from the buffer. If manifest_sync_timeout > 0, use
370 * its value in jiffies in the MANIFEST_SYNC state. */
371 static int at76_usbdfu_download(struct usb_device *udev, u8 *buf, u32 size,
372 int manifest_sync_timeout)
374 u8 *block;
375 struct dfu_status dfu_stat_buf;
376 int ret = 0;
377 int need_dfu_state = 1;
378 int is_done = 0;
379 u8 dfu_state = 0;
380 u32 dfu_timeout = 0;
381 int bsize = 0;
382 int blockno = 0;
384 at76_dbg(DBG_DFU, "%s( %p, %u, %d)", __func__, buf, size,
385 manifest_sync_timeout);
387 if (!size) {
388 dev_printk(KERN_ERR, &udev->dev, "FW buffer length invalid!\n");
389 return -EINVAL;
392 block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
393 if (!block)
394 return -ENOMEM;
396 do {
397 if (need_dfu_state) {
398 ret = at76_dfu_get_state(udev, &dfu_state);
399 if (ret < 0) {
400 dev_printk(KERN_ERR, &udev->dev,
401 "cannot get DFU state: %d\n", ret);
402 goto exit;
404 need_dfu_state = 0;
407 switch (dfu_state) {
408 case STATE_DFU_DOWNLOAD_SYNC:
409 at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_SYNC");
410 ret = at76_dfu_get_status(udev, &dfu_stat_buf);
411 if (ret >= 0) {
412 dfu_state = dfu_stat_buf.state;
413 dfu_timeout = at76_get_timeout(&dfu_stat_buf);
414 need_dfu_state = 0;
415 } else
416 dev_printk(KERN_ERR, &udev->dev,
417 "at76_dfu_get_status returned %d\n",
418 ret);
419 break;
421 case STATE_DFU_DOWNLOAD_BUSY:
422 at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_BUSY");
423 need_dfu_state = 1;
425 at76_dbg(DBG_DFU, "DFU: Resetting device");
426 schedule_timeout_interruptible(dfu_timeout);
427 break;
429 case STATE_DFU_DOWNLOAD_IDLE:
430 at76_dbg(DBG_DFU, "DOWNLOAD...");
431 /* fall through */
432 case STATE_DFU_IDLE:
433 at76_dbg(DBG_DFU, "DFU IDLE");
435 bsize = min_t(int, size, FW_BLOCK_SIZE);
436 memcpy(block, buf, bsize);
437 at76_dbg(DBG_DFU, "int fw, size left = %5d, "
438 "bsize = %4d, blockno = %2d", size, bsize,
439 blockno);
440 ret =
441 at76_load_int_fw_block(udev, blockno, block, bsize);
442 buf += bsize;
443 size -= bsize;
444 blockno++;
446 if (ret != bsize)
447 dev_printk(KERN_ERR, &udev->dev,
448 "at76_load_int_fw_block "
449 "returned %d\n", ret);
450 need_dfu_state = 1;
451 break;
453 case STATE_DFU_MANIFEST_SYNC:
454 at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_SYNC");
456 ret = at76_dfu_get_status(udev, &dfu_stat_buf);
457 if (ret < 0)
458 break;
460 dfu_state = dfu_stat_buf.state;
461 dfu_timeout = at76_get_timeout(&dfu_stat_buf);
462 need_dfu_state = 0;
464 /* override the timeout from the status response,
465 needed for AT76C505A */
466 if (manifest_sync_timeout > 0)
467 dfu_timeout = manifest_sync_timeout;
469 at76_dbg(DBG_DFU, "DFU: Waiting for manifest phase");
470 schedule_timeout_interruptible(dfu_timeout);
471 break;
473 case STATE_DFU_MANIFEST:
474 at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST");
475 is_done = 1;
476 break;
478 case STATE_DFU_MANIFEST_WAIT_RESET:
479 at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_WAIT_RESET");
480 is_done = 1;
481 break;
483 case STATE_DFU_UPLOAD_IDLE:
484 at76_dbg(DBG_DFU, "STATE_DFU_UPLOAD_IDLE");
485 break;
487 case STATE_DFU_ERROR:
488 at76_dbg(DBG_DFU, "STATE_DFU_ERROR");
489 ret = -EPIPE;
490 break;
492 default:
493 at76_dbg(DBG_DFU, "DFU UNKNOWN STATE (%d)", dfu_state);
494 ret = -EINVAL;
495 break;
497 } while (!is_done && (ret >= 0));
499 exit:
500 kfree(block);
501 if (ret >= 0)
502 ret = 0;
504 return ret;
507 #define HEX2STR_BUFFERS 4
508 #define HEX2STR_MAX_LEN 64
509 #define BIN2HEX(x) ((x) < 10 ? '0' + (x) : (x) + 'A' - 10)
511 /* Convert binary data into hex string */
512 static char *hex2str(void *buf, int len)
514 static atomic_t a = ATOMIC_INIT(0);
515 static char bufs[HEX2STR_BUFFERS][3 * HEX2STR_MAX_LEN + 1];
516 char *ret = bufs[atomic_inc_return(&a) & (HEX2STR_BUFFERS - 1)];
517 char *obuf = ret;
518 u8 *ibuf = buf;
520 if (len > HEX2STR_MAX_LEN)
521 len = HEX2STR_MAX_LEN;
523 if (len <= 0) {
524 ret[0] = '\0';
525 return ret;
528 while (len--) {
529 *obuf++ = BIN2HEX(*ibuf >> 4);
530 *obuf++ = BIN2HEX(*ibuf & 0xf);
531 *obuf++ = '-';
532 ibuf++;
534 *(--obuf) = '\0';
536 return ret;
539 #define MAC2STR_BUFFERS 4
541 static inline char *mac2str(u8 *mac)
543 static atomic_t a = ATOMIC_INIT(0);
544 static char bufs[MAC2STR_BUFFERS][6 * 3];
545 char *str;
547 str = bufs[atomic_inc_return(&a) & (MAC2STR_BUFFERS - 1)];
548 sprintf(str, "%02x:%02x:%02x:%02x:%02x:%02x",
549 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
550 return str;
553 /* LED trigger */
554 static int tx_activity;
555 static void at76_ledtrig_tx_timerfunc(unsigned long data);
556 static DEFINE_TIMER(ledtrig_tx_timer, at76_ledtrig_tx_timerfunc, 0, 0);
557 DEFINE_LED_TRIGGER(ledtrig_tx);
559 static void at76_ledtrig_tx_timerfunc(unsigned long data)
561 static int tx_lastactivity;
563 if (tx_lastactivity != tx_activity) {
564 tx_lastactivity = tx_activity;
565 led_trigger_event(ledtrig_tx, LED_FULL);
566 mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
567 } else
568 led_trigger_event(ledtrig_tx, LED_OFF);
571 static void at76_ledtrig_tx_activity(void)
573 tx_activity++;
574 if (!timer_pending(&ledtrig_tx_timer))
575 mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
578 static int at76_remap(struct usb_device *udev)
580 int ret;
581 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0a,
582 USB_TYPE_VENDOR | USB_DIR_OUT |
583 USB_RECIP_INTERFACE, 0, 0, NULL, 0,
584 USB_CTRL_GET_TIMEOUT);
585 if (ret < 0)
586 return ret;
587 return 0;
590 static int at76_get_op_mode(struct usb_device *udev)
592 int ret;
593 u8 op_mode;
595 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
596 USB_TYPE_VENDOR | USB_DIR_IN |
597 USB_RECIP_INTERFACE, 0x01, 0, &op_mode, 1,
598 USB_CTRL_GET_TIMEOUT);
599 if (ret < 0)
600 return ret;
601 else if (ret < 1)
602 return -EIO;
603 else
604 return op_mode;
607 /* Load a block of the second ("external") part of the firmware */
608 static inline int at76_load_ext_fw_block(struct usb_device *udev, int blockno,
609 void *block, int size)
611 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
612 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
613 0x0802, blockno, block, size,
614 USB_CTRL_GET_TIMEOUT);
617 static inline int at76_get_hw_cfg(struct usb_device *udev,
618 union at76_hwcfg *buf, int buf_size)
620 return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
621 USB_TYPE_VENDOR | USB_DIR_IN |
622 USB_RECIP_INTERFACE, 0x0a02, 0,
623 buf, buf_size, USB_CTRL_GET_TIMEOUT);
626 /* Intersil boards use a different "value" for GetHWConfig requests */
627 static inline int at76_get_hw_cfg_intersil(struct usb_device *udev,
628 union at76_hwcfg *buf, int buf_size)
630 return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
631 USB_TYPE_VENDOR | USB_DIR_IN |
632 USB_RECIP_INTERFACE, 0x0902, 0,
633 buf, buf_size, USB_CTRL_GET_TIMEOUT);
636 /* Get the hardware configuration for the adapter and put it to the appropriate
637 * fields of 'priv' (the GetHWConfig request and interpretation of the result
638 * depends on the board type) */
639 static int at76_get_hw_config(struct at76_priv *priv)
641 int ret;
642 union at76_hwcfg *hwcfg = kmalloc(sizeof(*hwcfg), GFP_KERNEL);
644 if (!hwcfg)
645 return -ENOMEM;
647 if (at76_is_intersil(priv->board_type)) {
648 ret = at76_get_hw_cfg_intersil(priv->udev, hwcfg,
649 sizeof(hwcfg->i));
650 if (ret < 0)
651 goto exit;
652 memcpy(priv->mac_addr, hwcfg->i.mac_addr, ETH_ALEN);
653 priv->regulatory_domain = hwcfg->i.regulatory_domain;
654 } else if (at76_is_503rfmd(priv->board_type)) {
655 ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r3));
656 if (ret < 0)
657 goto exit;
658 memcpy(priv->mac_addr, hwcfg->r3.mac_addr, ETH_ALEN);
659 priv->regulatory_domain = hwcfg->r3.regulatory_domain;
660 } else {
661 ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r5));
662 if (ret < 0)
663 goto exit;
664 memcpy(priv->mac_addr, hwcfg->r5.mac_addr, ETH_ALEN);
665 priv->regulatory_domain = hwcfg->r5.regulatory_domain;
668 exit:
669 kfree(hwcfg);
670 if (ret < 0)
671 printk(KERN_ERR "%s: cannot get HW Config (error %d)\n",
672 wiphy_name(priv->hw->wiphy), ret);
674 return ret;
677 static struct reg_domain const *at76_get_reg_domain(u16 code)
679 int i;
680 static struct reg_domain const fd_tab[] = {
681 { 0x10, "FCC (USA)", 0x7ff }, /* ch 1-11 */
682 { 0x20, "IC (Canada)", 0x7ff }, /* ch 1-11 */
683 { 0x30, "ETSI (most of Europe)", 0x1fff }, /* ch 1-13 */
684 { 0x31, "Spain", 0x600 }, /* ch 10-11 */
685 { 0x32, "France", 0x1e00 }, /* ch 10-13 */
686 { 0x40, "MKK (Japan)", 0x2000 }, /* ch 14 */
687 { 0x41, "MKK1 (Japan)", 0x3fff }, /* ch 1-14 */
688 { 0x50, "Israel", 0x3fc }, /* ch 3-9 */
689 { 0x00, "<unknown>", 0xffffffff } /* ch 1-32 */
692 /* Last entry is fallback for unknown domain code */
693 for (i = 0; i < ARRAY_SIZE(fd_tab) - 1; i++)
694 if (code == fd_tab[i].code)
695 break;
697 return &fd_tab[i];
700 static inline int at76_get_mib(struct usb_device *udev, u16 mib, void *buf,
701 int buf_size)
703 int ret;
705 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
706 USB_TYPE_VENDOR | USB_DIR_IN |
707 USB_RECIP_INTERFACE, mib << 8, 0, buf, buf_size,
708 USB_CTRL_GET_TIMEOUT);
709 if (ret >= 0 && ret != buf_size)
710 return -EIO;
711 return ret;
714 /* Return positive number for status, negative for an error */
715 static inline int at76_get_cmd_status(struct usb_device *udev, u8 cmd)
717 u8 stat_buf[40];
718 int ret;
720 ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x22,
721 USB_TYPE_VENDOR | USB_DIR_IN |
722 USB_RECIP_INTERFACE, cmd, 0, stat_buf,
723 sizeof(stat_buf), USB_CTRL_GET_TIMEOUT);
724 if (ret < 0)
725 return ret;
727 return stat_buf[5];
730 #define MAKE_CMD_CASE(c) case (c): return #c
732 static const char *at76_get_cmd_string(u8 cmd_status)
734 switch (cmd_status) {
735 MAKE_CMD_CASE(CMD_SET_MIB);
736 MAKE_CMD_CASE(CMD_GET_MIB);
737 MAKE_CMD_CASE(CMD_SCAN);
738 MAKE_CMD_CASE(CMD_JOIN);
739 MAKE_CMD_CASE(CMD_START_IBSS);
740 MAKE_CMD_CASE(CMD_RADIO_ON);
741 MAKE_CMD_CASE(CMD_RADIO_OFF);
742 MAKE_CMD_CASE(CMD_STARTUP);
745 return "UNKNOWN";
748 static int at76_set_card_command(struct usb_device *udev, int cmd, void *buf,
749 int buf_size)
751 int ret;
752 struct at76_command *cmd_buf = kmalloc(sizeof(struct at76_command) +
753 buf_size, GFP_KERNEL);
755 if (!cmd_buf)
756 return -ENOMEM;
758 cmd_buf->cmd = cmd;
759 cmd_buf->reserved = 0;
760 cmd_buf->size = cpu_to_le16(buf_size);
761 memcpy(cmd_buf->data, buf, buf_size);
763 at76_dbg_dump(DBG_CMD, cmd_buf, sizeof(struct at76_command) + buf_size,
764 "issuing command %s (0x%02x)",
765 at76_get_cmd_string(cmd), cmd);
767 ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
768 USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
769 0, 0, cmd_buf,
770 sizeof(struct at76_command) + buf_size,
771 USB_CTRL_GET_TIMEOUT);
772 kfree(cmd_buf);
773 return ret;
776 #define MAKE_CMD_STATUS_CASE(c) case (c): return #c
777 static const char *at76_get_cmd_status_string(u8 cmd_status)
779 switch (cmd_status) {
780 MAKE_CMD_STATUS_CASE(CMD_STATUS_IDLE);
781 MAKE_CMD_STATUS_CASE(CMD_STATUS_COMPLETE);
782 MAKE_CMD_STATUS_CASE(CMD_STATUS_UNKNOWN);
783 MAKE_CMD_STATUS_CASE(CMD_STATUS_INVALID_PARAMETER);
784 MAKE_CMD_STATUS_CASE(CMD_STATUS_FUNCTION_NOT_SUPPORTED);
785 MAKE_CMD_STATUS_CASE(CMD_STATUS_TIME_OUT);
786 MAKE_CMD_STATUS_CASE(CMD_STATUS_IN_PROGRESS);
787 MAKE_CMD_STATUS_CASE(CMD_STATUS_HOST_FAILURE);
788 MAKE_CMD_STATUS_CASE(CMD_STATUS_SCAN_FAILED);
791 return "UNKNOWN";
794 /* Wait until the command is completed */
795 static int at76_wait_completion(struct at76_priv *priv, int cmd)
797 int status = 0;
798 unsigned long timeout = jiffies + CMD_COMPLETION_TIMEOUT;
800 do {
801 status = at76_get_cmd_status(priv->udev, cmd);
802 if (status < 0) {
803 printk(KERN_ERR "%s: at76_get_cmd_status failed: %d\n",
804 wiphy_name(priv->hw->wiphy), status);
805 break;
808 at76_dbg(DBG_WAIT_COMPLETE,
809 "%s: Waiting on cmd %d, status = %d (%s)",
810 wiphy_name(priv->hw->wiphy), cmd, status,
811 at76_get_cmd_status_string(status));
813 if (status != CMD_STATUS_IN_PROGRESS
814 && status != CMD_STATUS_IDLE)
815 break;
817 schedule_timeout_interruptible(HZ / 10); /* 100 ms */
818 if (time_after(jiffies, timeout)) {
819 printk(KERN_ERR
820 "%s: completion timeout for command %d\n",
821 wiphy_name(priv->hw->wiphy), cmd);
822 status = -ETIMEDOUT;
823 break;
825 } while (1);
827 return status;
830 static int at76_set_mib(struct at76_priv *priv, struct set_mib_buffer *buf)
832 int ret;
834 ret = at76_set_card_command(priv->udev, CMD_SET_MIB, buf,
835 offsetof(struct set_mib_buffer,
836 data) + buf->size);
837 if (ret < 0)
838 return ret;
840 ret = at76_wait_completion(priv, CMD_SET_MIB);
841 if (ret != CMD_STATUS_COMPLETE) {
842 printk(KERN_INFO
843 "%s: set_mib: at76_wait_completion failed "
844 "with %d\n", wiphy_name(priv->hw->wiphy), ret);
845 ret = -EIO;
848 return ret;
851 /* Return < 0 on error, == 0 if no command sent, == 1 if cmd sent */
852 static int at76_set_radio(struct at76_priv *priv, int enable)
854 int ret;
855 int cmd;
857 if (priv->radio_on == enable)
858 return 0;
860 cmd = enable ? CMD_RADIO_ON : CMD_RADIO_OFF;
862 ret = at76_set_card_command(priv->udev, cmd, NULL, 0);
863 if (ret < 0)
864 printk(KERN_ERR "%s: at76_set_card_command(%d) failed: %d\n",
865 wiphy_name(priv->hw->wiphy), cmd, ret);
866 else
867 ret = 1;
869 priv->radio_on = enable;
870 return ret;
873 /* Set current power save mode (AT76_PM_OFF/AT76_PM_ON/AT76_PM_SMART) */
874 static int at76_set_pm_mode(struct at76_priv *priv)
876 int ret = 0;
878 priv->mib_buf.type = MIB_MAC_MGMT;
879 priv->mib_buf.size = 1;
880 priv->mib_buf.index = offsetof(struct mib_mac_mgmt, power_mgmt_mode);
881 priv->mib_buf.data.byte = priv->pm_mode;
883 ret = at76_set_mib(priv, &priv->mib_buf);
884 if (ret < 0)
885 printk(KERN_ERR "%s: set_mib (pm_mode) failed: %d\n",
886 wiphy_name(priv->hw->wiphy), ret);
888 return ret;
891 static int at76_set_preamble(struct at76_priv *priv, u8 type)
893 int ret = 0;
895 priv->mib_buf.type = MIB_LOCAL;
896 priv->mib_buf.size = 1;
897 priv->mib_buf.index = offsetof(struct mib_local, preamble_type);
898 priv->mib_buf.data.byte = type;
900 ret = at76_set_mib(priv, &priv->mib_buf);
901 if (ret < 0)
902 printk(KERN_ERR "%s: set_mib (preamble) failed: %d\n",
903 wiphy_name(priv->hw->wiphy), ret);
905 return ret;
908 static int at76_set_frag(struct at76_priv *priv, u16 size)
910 int ret = 0;
912 priv->mib_buf.type = MIB_MAC;
913 priv->mib_buf.size = 2;
914 priv->mib_buf.index = offsetof(struct mib_mac, frag_threshold);
915 priv->mib_buf.data.word = cpu_to_le16(size);
917 ret = at76_set_mib(priv, &priv->mib_buf);
918 if (ret < 0)
919 printk(KERN_ERR "%s: set_mib (frag threshold) failed: %d\n",
920 wiphy_name(priv->hw->wiphy), ret);
922 return ret;
925 static int at76_set_rts(struct at76_priv *priv, u16 size)
927 int ret = 0;
929 priv->mib_buf.type = MIB_MAC;
930 priv->mib_buf.size = 2;
931 priv->mib_buf.index = offsetof(struct mib_mac, rts_threshold);
932 priv->mib_buf.data.word = cpu_to_le16(size);
934 ret = at76_set_mib(priv, &priv->mib_buf);
935 if (ret < 0)
936 printk(KERN_ERR "%s: set_mib (rts) failed: %d\n",
937 wiphy_name(priv->hw->wiphy), ret);
939 return ret;
942 static int at76_set_autorate_fallback(struct at76_priv *priv, int onoff)
944 int ret = 0;
946 priv->mib_buf.type = MIB_LOCAL;
947 priv->mib_buf.size = 1;
948 priv->mib_buf.index = offsetof(struct mib_local, txautorate_fallback);
949 priv->mib_buf.data.byte = onoff;
951 ret = at76_set_mib(priv, &priv->mib_buf);
952 if (ret < 0)
953 printk(KERN_ERR "%s: set_mib (autorate fallback) failed: %d\n",
954 wiphy_name(priv->hw->wiphy), ret);
956 return ret;
959 static int at76_set_tkip_bssid(struct at76_priv *priv, const void *addr)
961 int ret = 0;
963 priv->mib_buf.type = MIB_MAC_ENCRYPTION;
964 priv->mib_buf.size = ETH_ALEN;
965 priv->mib_buf.index = offsetof(struct mib_mac_encryption, tkip_bssid);
966 memcpy(priv->mib_buf.data.addr, addr, ETH_ALEN);
968 ret = at76_set_mib(priv, &priv->mib_buf);
969 if (ret < 0)
970 printk(KERN_ERR "%s: set_mib (MAC_ENCRYPTION, tkip_bssid) failed: %d\n",
971 wiphy_name(priv->hw->wiphy), ret);
973 return ret;
976 static int at76_reset_rsc(struct at76_priv *priv)
978 int ret = 0;
980 priv->mib_buf.type = MIB_MAC_ENCRYPTION;
981 priv->mib_buf.size = 4 * 8;
982 priv->mib_buf.index = offsetof(struct mib_mac_encryption, key_rsc);
983 memset(priv->mib_buf.data.data, 0 , priv->mib_buf.size);
985 ret = at76_set_mib(priv, &priv->mib_buf);
986 if (ret < 0)
987 printk(KERN_ERR "%s: set_mib (MAC_ENCRYPTION, key_rsc) failed: %d\n",
988 wiphy_name(priv->hw->wiphy), ret);
990 return ret;
993 static void at76_dump_mib_mac_addr(struct at76_priv *priv)
995 int i;
996 int ret;
997 struct mib_mac_addr *m = kmalloc(sizeof(struct mib_mac_addr),
998 GFP_KERNEL);
1000 if (!m)
1001 return;
1003 ret = at76_get_mib(priv->udev, MIB_MAC_ADDR, m,
1004 sizeof(struct mib_mac_addr));
1005 if (ret < 0) {
1006 printk(KERN_ERR "%s: at76_get_mib (MAC_ADDR) failed: %d\n",
1007 wiphy_name(priv->hw->wiphy), ret);
1008 goto exit;
1011 at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: mac_addr %s res 0x%x 0x%x",
1012 wiphy_name(priv->hw->wiphy),
1013 mac2str(m->mac_addr), m->res[0], m->res[1]);
1014 for (i = 0; i < ARRAY_SIZE(m->group_addr); i++)
1015 at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: group addr %d: %s, "
1016 "status %d", wiphy_name(priv->hw->wiphy), i,
1017 mac2str(m->group_addr[i]), m->group_addr_status[i]);
1018 exit:
1019 kfree(m);
1022 static void at76_dump_mib_mac_wep(struct at76_priv *priv)
1024 int i;
1025 int ret;
1026 int key_len;
1027 struct mib_mac_wep *m = kmalloc(sizeof(struct mib_mac_wep), GFP_KERNEL);
1029 if (!m)
1030 return;
1032 ret = at76_get_mib(priv->udev, MIB_MAC_WEP, m,
1033 sizeof(struct mib_mac_wep));
1034 if (ret < 0) {
1035 printk(KERN_ERR "%s: at76_get_mib (MAC_WEP) failed: %d\n",
1036 wiphy_name(priv->hw->wiphy), ret);
1037 goto exit;
1040 at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: priv_invoked %u def_key_id %u "
1041 "key_len %u excl_unencr %u wep_icv_err %u wep_excluded %u "
1042 "encr_level %u key %d", wiphy_name(priv->hw->wiphy),
1043 m->privacy_invoked, m->wep_default_key_id,
1044 m->wep_key_mapping_len, m->exclude_unencrypted,
1045 le32_to_cpu(m->wep_icv_error_count),
1046 le32_to_cpu(m->wep_excluded_count), m->encryption_level,
1047 m->wep_default_key_id);
1049 key_len = (m->encryption_level == 1) ?
1050 WEP_SMALL_KEY_LEN : WEP_LARGE_KEY_LEN;
1052 for (i = 0; i < WEP_KEYS; i++)
1053 at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: key %d: %s",
1054 wiphy_name(priv->hw->wiphy), i,
1055 hex2str(m->wep_default_keyvalue[i], key_len));
1056 exit:
1057 kfree(m);
1060 static void at76_dump_mib_mac_encryption(struct at76_priv *priv)
1062 int i;
1063 int ret;
1064 /*int key_len;*/
1065 struct mib_mac_encryption *m;
1067 m = kmalloc(sizeof(struct mib_mac_encryption), GFP_KERNEL);
1068 if (!m)
1069 return;
1071 ret = at76_get_mib(priv->udev, MIB_MAC_ENCRYPTION, m,
1072 sizeof(struct mib_mac_encryption));
1073 if (ret < 0) {
1074 dev_err(&priv->udev->dev,
1075 "%s: at76_get_mib (MAC_ENCRYPTION) failed: %d\n",
1076 wiphy_name(priv->hw->wiphy), ret);
1077 goto exit;
1080 at76_dbg(DBG_MIB,
1081 "%s: MIB MAC_ENCRYPTION: tkip_bssid %s priv_invoked %u "
1082 "ciph_key_id %u grp_key_id %u excl_unencr %u "
1083 "ckip_key_perm %u wep_icv_err %u wep_excluded %u",
1084 wiphy_name(priv->hw->wiphy), mac2str(m->tkip_bssid),
1085 m->privacy_invoked, m->cipher_default_key_id,
1086 m->cipher_default_group_key_id, m->exclude_unencrypted,
1087 m->ckip_key_permutation,
1088 le32_to_cpu(m->wep_icv_error_count),
1089 le32_to_cpu(m->wep_excluded_count));
1091 /*key_len = (m->encryption_level == 1) ?
1092 WEP_SMALL_KEY_LEN : WEP_LARGE_KEY_LEN;*/
1094 for (i = 0; i < CIPHER_KEYS; i++)
1095 at76_dbg(DBG_MIB, "%s: MIB MAC_ENCRYPTION: key %d: %s",
1096 wiphy_name(priv->hw->wiphy), i,
1097 hex2str(m->cipher_default_keyvalue[i],
1098 CIPHER_KEY_LEN));
1099 exit:
1100 kfree(m);
1103 static void at76_dump_mib_mac_mgmt(struct at76_priv *priv)
1105 int ret;
1106 struct mib_mac_mgmt *m = kmalloc(sizeof(struct mib_mac_mgmt),
1107 GFP_KERNEL);
1109 if (!m)
1110 return;
1112 ret = at76_get_mib(priv->udev, MIB_MAC_MGMT, m,
1113 sizeof(struct mib_mac_mgmt));
1114 if (ret < 0) {
1115 printk(KERN_ERR "%s: at76_get_mib (MAC_MGMT) failed: %d\n",
1116 wiphy_name(priv->hw->wiphy), ret);
1117 goto exit;
1120 at76_dbg(DBG_MIB, "%s: MIB MAC_MGMT: beacon_period %d CFP_max_duration "
1121 "%d medium_occupancy_limit %d station_id 0x%x ATIM_window %d "
1122 "CFP_mode %d privacy_opt_impl %d DTIM_period %d CFP_period %d "
1123 "current_bssid %s current_essid %s current_bss_type %d "
1124 "pm_mode %d ibss_change %d res %d "
1125 "multi_domain_capability_implemented %d "
1126 "international_roaming %d country_string %.3s",
1127 wiphy_name(priv->hw->wiphy), le16_to_cpu(m->beacon_period),
1128 le16_to_cpu(m->CFP_max_duration),
1129 le16_to_cpu(m->medium_occupancy_limit),
1130 le16_to_cpu(m->station_id), le16_to_cpu(m->ATIM_window),
1131 m->CFP_mode, m->privacy_option_implemented, m->DTIM_period,
1132 m->CFP_period, mac2str(m->current_bssid),
1133 hex2str(m->current_essid, IW_ESSID_MAX_SIZE),
1134 m->current_bss_type, m->power_mgmt_mode, m->ibss_change,
1135 m->res, m->multi_domain_capability_implemented,
1136 m->multi_domain_capability_enabled, m->country_string);
1137 exit:
1138 kfree(m);
1141 static void at76_dump_mib_mac(struct at76_priv *priv)
1143 int ret;
1144 struct mib_mac *m = kmalloc(sizeof(struct mib_mac), GFP_KERNEL);
1146 if (!m)
1147 return;
1149 ret = at76_get_mib(priv->udev, MIB_MAC, m, sizeof(struct mib_mac));
1150 if (ret < 0) {
1151 printk(KERN_ERR "%s: at76_get_mib (MAC) failed: %d\n",
1152 wiphy_name(priv->hw->wiphy), ret);
1153 goto exit;
1156 at76_dbg(DBG_MIB, "%s: MIB MAC: max_tx_msdu_lifetime %d "
1157 "max_rx_lifetime %d frag_threshold %d rts_threshold %d "
1158 "cwmin %d cwmax %d short_retry_time %d long_retry_time %d "
1159 "scan_type %d scan_channel %d probe_delay %u "
1160 "min_channel_time %d max_channel_time %d listen_int %d "
1161 "desired_ssid %s desired_bssid %s desired_bsstype %d",
1162 wiphy_name(priv->hw->wiphy),
1163 le32_to_cpu(m->max_tx_msdu_lifetime),
1164 le32_to_cpu(m->max_rx_lifetime),
1165 le16_to_cpu(m->frag_threshold), le16_to_cpu(m->rts_threshold),
1166 le16_to_cpu(m->cwmin), le16_to_cpu(m->cwmax),
1167 m->short_retry_time, m->long_retry_time, m->scan_type,
1168 m->scan_channel, le16_to_cpu(m->probe_delay),
1169 le16_to_cpu(m->min_channel_time),
1170 le16_to_cpu(m->max_channel_time),
1171 le16_to_cpu(m->listen_interval),
1172 hex2str(m->desired_ssid, IW_ESSID_MAX_SIZE),
1173 mac2str(m->desired_bssid), m->desired_bsstype);
1174 exit:
1175 kfree(m);
1178 static void at76_dump_mib_phy(struct at76_priv *priv)
1180 int ret;
1181 struct mib_phy *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
1183 if (!m)
1184 return;
1186 ret = at76_get_mib(priv->udev, MIB_PHY, m, sizeof(struct mib_phy));
1187 if (ret < 0) {
1188 printk(KERN_ERR "%s: at76_get_mib (PHY) failed: %d\n",
1189 wiphy_name(priv->hw->wiphy), ret);
1190 goto exit;
1193 at76_dbg(DBG_MIB, "%s: MIB PHY: ed_threshold %d slot_time %d "
1194 "sifs_time %d preamble_length %d plcp_header_length %d "
1195 "mpdu_max_length %d cca_mode_supported %d operation_rate_set "
1196 "0x%x 0x%x 0x%x 0x%x channel_id %d current_cca_mode %d "
1197 "phy_type %d current_reg_domain %d",
1198 wiphy_name(priv->hw->wiphy), le32_to_cpu(m->ed_threshold),
1199 le16_to_cpu(m->slot_time), le16_to_cpu(m->sifs_time),
1200 le16_to_cpu(m->preamble_length),
1201 le16_to_cpu(m->plcp_header_length),
1202 le16_to_cpu(m->mpdu_max_length),
1203 le16_to_cpu(m->cca_mode_supported), m->operation_rate_set[0],
1204 m->operation_rate_set[1], m->operation_rate_set[2],
1205 m->operation_rate_set[3], m->channel_id, m->current_cca_mode,
1206 m->phy_type, m->current_reg_domain);
1207 exit:
1208 kfree(m);
1211 static void at76_dump_mib_local(struct at76_priv *priv)
1213 int ret;
1214 struct mib_local *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
1216 if (!m)
1217 return;
1219 ret = at76_get_mib(priv->udev, MIB_LOCAL, m, sizeof(struct mib_local));
1220 if (ret < 0) {
1221 printk(KERN_ERR "%s: at76_get_mib (LOCAL) failed: %d\n",
1222 wiphy_name(priv->hw->wiphy), ret);
1223 goto exit;
1226 at76_dbg(DBG_MIB, "%s: MIB LOCAL: beacon_enable %d "
1227 "txautorate_fallback %d ssid_size %d promiscuous_mode %d "
1228 "preamble_type %d", wiphy_name(priv->hw->wiphy),
1229 m->beacon_enable,
1230 m->txautorate_fallback, m->ssid_size, m->promiscuous_mode,
1231 m->preamble_type);
1232 exit:
1233 kfree(m);
1236 static void at76_dump_mib_mdomain(struct at76_priv *priv)
1238 int ret;
1239 struct mib_mdomain *m = kmalloc(sizeof(struct mib_mdomain), GFP_KERNEL);
1241 if (!m)
1242 return;
1244 ret = at76_get_mib(priv->udev, MIB_MDOMAIN, m,
1245 sizeof(struct mib_mdomain));
1246 if (ret < 0) {
1247 printk(KERN_ERR "%s: at76_get_mib (MDOMAIN) failed: %d\n",
1248 wiphy_name(priv->hw->wiphy), ret);
1249 goto exit;
1252 at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: channel_list %s",
1253 wiphy_name(priv->hw->wiphy),
1254 hex2str(m->channel_list, sizeof(m->channel_list)));
1256 at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: tx_powerlevel %s",
1257 wiphy_name(priv->hw->wiphy),
1258 hex2str(m->tx_powerlevel, sizeof(m->tx_powerlevel)));
1259 exit:
1260 kfree(m);
1263 /* Enable monitor mode */
1264 static int at76_start_monitor(struct at76_priv *priv)
1266 struct at76_req_scan scan;
1267 int ret;
1269 memset(&scan, 0, sizeof(struct at76_req_scan));
1270 memset(scan.bssid, 0xff, ETH_ALEN);
1272 scan.channel = priv->channel;
1273 scan.scan_type = SCAN_TYPE_PASSIVE;
1274 scan.international_scan = 0;
1276 ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
1277 if (ret >= 0)
1278 ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1280 return ret;
1283 /* Calculate padding from txbuf->wlength (which excludes the USB TX header),
1284 likely to compensate a flaw in the AT76C503A USB part ... */
1285 static inline int at76_calc_padding(int wlen)
1287 /* add the USB TX header */
1288 wlen += AT76_TX_HDRLEN;
1290 wlen = wlen % 64;
1292 if (wlen < 50)
1293 return 50 - wlen;
1295 if (wlen >= 61)
1296 return 64 + 50 - wlen;
1298 return 0;
1301 static void at76_rx_callback(struct urb *urb)
1303 struct at76_priv *priv = urb->context;
1305 priv->rx_tasklet.data = (unsigned long)urb;
1306 tasklet_schedule(&priv->rx_tasklet);
1307 return;
1310 static int at76_submit_rx_urb(struct at76_priv *priv)
1312 int ret;
1313 int size;
1314 struct sk_buff *skb = priv->rx_skb;
1316 if (!priv->rx_urb) {
1317 printk(KERN_ERR "%s: %s: priv->rx_urb is NULL\n",
1318 wiphy_name(priv->hw->wiphy), __func__);
1319 return -EFAULT;
1322 if (!skb) {
1323 skb = dev_alloc_skb(sizeof(struct at76_rx_buffer));
1324 if (!skb) {
1325 printk(KERN_ERR "%s: cannot allocate rx skbuff\n",
1326 wiphy_name(priv->hw->wiphy));
1327 ret = -ENOMEM;
1328 goto exit;
1330 priv->rx_skb = skb;
1331 } else {
1332 skb_push(skb, skb_headroom(skb));
1333 skb_trim(skb, 0);
1336 size = skb_tailroom(skb);
1337 usb_fill_bulk_urb(priv->rx_urb, priv->udev, priv->rx_pipe,
1338 skb_put(skb, size), size, at76_rx_callback, priv);
1339 ret = usb_submit_urb(priv->rx_urb, GFP_ATOMIC);
1340 if (ret < 0) {
1341 if (ret == -ENODEV)
1342 at76_dbg(DBG_DEVSTART,
1343 "usb_submit_urb returned -ENODEV");
1344 else
1345 printk(KERN_ERR "%s: rx, usb_submit_urb failed: %d\n",
1346 wiphy_name(priv->hw->wiphy), ret);
1349 exit:
1350 if (ret < 0 && ret != -ENODEV)
1351 printk(KERN_ERR "%s: cannot submit rx urb - please unload the "
1352 "driver and/or power cycle the device\n",
1353 wiphy_name(priv->hw->wiphy));
1355 return ret;
1358 /* Download external firmware */
1359 static int at76_load_external_fw(struct usb_device *udev, struct fwentry *fwe)
1361 int ret;
1362 int op_mode;
1363 int blockno = 0;
1364 int bsize;
1365 u8 *block;
1366 u8 *buf = fwe->extfw;
1367 int size = fwe->extfw_size;
1369 if (!buf || !size)
1370 return -ENOENT;
1372 op_mode = at76_get_op_mode(udev);
1373 at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
1375 if (op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
1376 dev_printk(KERN_ERR, &udev->dev, "unexpected opmode %d\n",
1377 op_mode);
1378 return -EINVAL;
1381 block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
1382 if (!block)
1383 return -ENOMEM;
1385 at76_dbg(DBG_DEVSTART, "downloading external firmware");
1387 /* for fw >= 0.100, the device needs an extra empty block */
1388 do {
1389 bsize = min_t(int, size, FW_BLOCK_SIZE);
1390 memcpy(block, buf, bsize);
1391 at76_dbg(DBG_DEVSTART,
1392 "ext fw, size left = %5d, bsize = %4d, blockno = %2d",
1393 size, bsize, blockno);
1394 ret = at76_load_ext_fw_block(udev, blockno, block, bsize);
1395 if (ret != bsize) {
1396 dev_printk(KERN_ERR, &udev->dev,
1397 "loading %dth firmware block failed: %d\n",
1398 blockno, ret);
1399 goto exit;
1401 buf += bsize;
1402 size -= bsize;
1403 blockno++;
1404 } while (bsize > 0);
1406 if (at76_is_505a(fwe->board_type)) {
1407 at76_dbg(DBG_DEVSTART, "200 ms delay for 505a");
1408 schedule_timeout_interruptible(HZ / 5 + 1);
1411 exit:
1412 kfree(block);
1413 if (ret < 0)
1414 dev_printk(KERN_ERR, &udev->dev,
1415 "downloading external firmware failed: %d\n", ret);
1416 return ret;
1419 /* Download internal firmware */
1420 static int at76_load_internal_fw(struct usb_device *udev, struct fwentry *fwe)
1422 int ret;
1423 int need_remap = !at76_is_505a(fwe->board_type);
1425 ret = at76_usbdfu_download(udev, fwe->intfw, fwe->intfw_size,
1426 need_remap ? 0 : 2 * HZ);
1428 if (ret < 0) {
1429 dev_printk(KERN_ERR, &udev->dev,
1430 "downloading internal fw failed with %d\n", ret);
1431 goto exit;
1434 at76_dbg(DBG_DEVSTART, "sending REMAP");
1436 /* no REMAP for 505A (see SF driver) */
1437 if (need_remap) {
1438 ret = at76_remap(udev);
1439 if (ret < 0) {
1440 dev_printk(KERN_ERR, &udev->dev,
1441 "sending REMAP failed with %d\n", ret);
1442 goto exit;
1446 at76_dbg(DBG_DEVSTART, "sleeping for 2 seconds");
1447 schedule_timeout_interruptible(2 * HZ + 1);
1448 usb_reset_device(udev);
1450 exit:
1451 return ret;
1454 static int at76_startup_device(struct at76_priv *priv)
1456 struct at76_card_config *ccfg = &priv->card_config;
1457 int ret;
1459 at76_dbg(DBG_PARAMS,
1460 "%s param: ssid %.*s (%s) mode %s ch %d wep %s key %d "
1461 "keylen %d", wiphy_name(priv->hw->wiphy), priv->essid_size,
1462 priv->essid, hex2str(priv->essid, IW_ESSID_MAX_SIZE),
1463 priv->iw_mode == IW_MODE_ADHOC ? "adhoc" : "infra",
1464 priv->channel, priv->wep_enabled ? "enabled" : "disabled",
1465 priv->wep_key_id, priv->wep_keys_len[priv->wep_key_id]);
1466 at76_dbg(DBG_PARAMS,
1467 "%s param: preamble %s rts %d retry %d frag %d "
1468 "txrate %s auth_mode %d", wiphy_name(priv->hw->wiphy),
1469 preambles[priv->preamble_type], priv->rts_threshold,
1470 priv->short_retry_limit, priv->frag_threshold,
1471 priv->txrate == TX_RATE_1MBIT ? "1MBit" : priv->txrate ==
1472 TX_RATE_2MBIT ? "2MBit" : priv->txrate ==
1473 TX_RATE_5_5MBIT ? "5.5MBit" : priv->txrate ==
1474 TX_RATE_11MBIT ? "11MBit" : priv->txrate ==
1475 TX_RATE_AUTO ? "auto" : "<invalid>", priv->auth_mode);
1476 at76_dbg(DBG_PARAMS,
1477 "%s param: pm_mode %d pm_period %d auth_mode %s "
1478 "scan_times %d %d scan_mode %s",
1479 wiphy_name(priv->hw->wiphy), priv->pm_mode, priv->pm_period,
1480 priv->auth_mode == WLAN_AUTH_OPEN ? "open" : "shared_secret",
1481 priv->scan_min_time, priv->scan_max_time,
1482 priv->scan_mode == SCAN_TYPE_ACTIVE ? "active" : "passive");
1484 memset(ccfg, 0, sizeof(struct at76_card_config));
1485 ccfg->promiscuous_mode = 0;
1486 ccfg->short_retry_limit = priv->short_retry_limit;
1488 if (priv->wep_enabled) {
1489 if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
1490 ccfg->encryption_type = 2;
1491 else
1492 ccfg->encryption_type = 1;
1494 /* jal: always exclude unencrypted if WEP is active */
1495 ccfg->exclude_unencrypted = 1;
1496 } else {
1497 ccfg->exclude_unencrypted = 0;
1498 ccfg->encryption_type = 0;
1501 ccfg->rts_threshold = cpu_to_le16(priv->rts_threshold);
1502 ccfg->fragmentation_threshold = cpu_to_le16(priv->frag_threshold);
1504 memcpy(ccfg->basic_rate_set, hw_rates, 4);
1505 /* jal: really needed, we do a set_mib for autorate later ??? */
1506 ccfg->auto_rate_fallback = (priv->txrate == TX_RATE_AUTO ? 1 : 0);
1507 ccfg->channel = priv->channel;
1508 ccfg->privacy_invoked = priv->wep_enabled;
1509 memcpy(ccfg->current_ssid, priv->essid, IW_ESSID_MAX_SIZE);
1510 ccfg->ssid_len = priv->essid_size;
1512 ccfg->wep_default_key_id = priv->wep_key_id;
1513 memcpy(ccfg->wep_default_key_value, priv->wep_keys,
1514 sizeof(priv->wep_keys));
1516 ccfg->short_preamble = priv->preamble_type;
1517 ccfg->beacon_period = cpu_to_le16(priv->beacon_period);
1519 ret = at76_set_card_command(priv->udev, CMD_STARTUP, &priv->card_config,
1520 sizeof(struct at76_card_config));
1521 if (ret < 0) {
1522 printk(KERN_ERR "%s: at76_set_card_command failed: %d\n",
1523 wiphy_name(priv->hw->wiphy), ret);
1524 return ret;
1527 at76_wait_completion(priv, CMD_STARTUP);
1529 /* remove BSSID from previous run */
1530 memset(priv->bssid, 0, ETH_ALEN);
1532 if (at76_set_radio(priv, 1) == 1)
1533 at76_wait_completion(priv, CMD_RADIO_ON);
1535 ret = at76_set_preamble(priv, priv->preamble_type);
1536 if (ret < 0)
1537 return ret;
1539 ret = at76_set_frag(priv, priv->frag_threshold);
1540 if (ret < 0)
1541 return ret;
1543 ret = at76_set_rts(priv, priv->rts_threshold);
1544 if (ret < 0)
1545 return ret;
1547 ret = at76_set_autorate_fallback(priv,
1548 priv->txrate == TX_RATE_AUTO ? 1 : 0);
1549 if (ret < 0)
1550 return ret;
1552 ret = at76_set_pm_mode(priv);
1553 if (ret < 0)
1554 return ret;
1556 if (at76_debug & DBG_MIB) {
1557 at76_dump_mib_mac(priv);
1558 at76_dump_mib_mac_addr(priv);
1559 at76_dump_mib_mac_mgmt(priv);
1560 at76_dump_mib_mac_wep(priv);
1561 at76_dump_mib_mdomain(priv);
1562 at76_dump_mib_phy(priv);
1563 at76_dump_mib_local(priv);
1566 return 0;
1569 /* Enable or disable promiscuous mode */
1570 static void at76_work_set_promisc(struct work_struct *work)
1572 struct at76_priv *priv = container_of(work, struct at76_priv,
1573 work_set_promisc);
1574 int ret = 0;
1576 mutex_lock(&priv->mtx);
1578 priv->mib_buf.type = MIB_LOCAL;
1579 priv->mib_buf.size = 1;
1580 priv->mib_buf.index = offsetof(struct mib_local, promiscuous_mode);
1581 priv->mib_buf.data.byte = priv->promisc ? 1 : 0;
1583 ret = at76_set_mib(priv, &priv->mib_buf);
1584 if (ret < 0)
1585 printk(KERN_ERR "%s: set_mib (promiscuous_mode) failed: %d\n",
1586 wiphy_name(priv->hw->wiphy), ret);
1588 mutex_unlock(&priv->mtx);
1591 /* Submit Rx urb back to the device */
1592 static void at76_work_submit_rx(struct work_struct *work)
1594 struct at76_priv *priv = container_of(work, struct at76_priv,
1595 work_submit_rx);
1597 mutex_lock(&priv->mtx);
1598 at76_submit_rx_urb(priv);
1599 mutex_unlock(&priv->mtx);
1602 static void at76_rx_tasklet(unsigned long param)
1604 struct urb *urb = (struct urb *)param;
1605 struct at76_priv *priv = urb->context;
1606 struct at76_rx_buffer *buf;
1607 struct ieee80211_rx_status rx_status = { 0 };
1609 if (priv->device_unplugged) {
1610 at76_dbg(DBG_DEVSTART, "device unplugged");
1611 if (urb)
1612 at76_dbg(DBG_DEVSTART, "urb status %d", urb->status);
1613 return;
1616 if (!priv->rx_skb || !priv->rx_skb->data)
1617 return;
1619 buf = (struct at76_rx_buffer *)priv->rx_skb->data;
1621 if (urb->status != 0) {
1622 if (urb->status != -ENOENT && urb->status != -ECONNRESET)
1623 at76_dbg(DBG_URB,
1624 "%s %s: - nonzero Rx bulk status received: %d",
1625 __func__, wiphy_name(priv->hw->wiphy),
1626 urb->status);
1627 return;
1630 at76_dbg(DBG_RX_ATMEL_HDR,
1631 "%s: rx frame: rate %d rssi %d noise %d link %d",
1632 wiphy_name(priv->hw->wiphy), buf->rx_rate, buf->rssi,
1633 buf->noise_level, buf->link_quality);
1635 skb_trim(priv->rx_skb, le16_to_cpu(buf->wlength) + AT76_RX_HDRLEN);
1636 at76_dbg_dump(DBG_RX_DATA, &priv->rx_skb->data[AT76_RX_HDRLEN],
1637 priv->rx_skb->len, "RX: len=%d",
1638 (int)(priv->rx_skb->len - AT76_RX_HDRLEN));
1640 rx_status.signal = buf->rssi;
1641 /* FIXME: is rate_idx still present in structure? */
1642 rx_status.rate_idx = buf->rx_rate;
1643 rx_status.flag |= RX_FLAG_DECRYPTED;
1644 rx_status.flag |= RX_FLAG_IV_STRIPPED;
1646 skb_pull(priv->rx_skb, AT76_RX_HDRLEN);
1647 at76_dbg(DBG_MAC80211, "calling ieee80211_rx_irqsafe(): %d/%d",
1648 priv->rx_skb->len, priv->rx_skb->data_len);
1649 ieee80211_rx_irqsafe(priv->hw, priv->rx_skb, &rx_status);
1651 /* Use a new skb for the next receive */
1652 priv->rx_skb = NULL;
1654 at76_submit_rx_urb(priv);
1657 /* Load firmware into kernel memory and parse it */
1658 static struct fwentry *at76_load_firmware(struct usb_device *udev,
1659 enum board_type board_type)
1661 int ret;
1662 char *str;
1663 struct at76_fw_header *fwh;
1664 struct fwentry *fwe = &firmwares[board_type];
1666 mutex_lock(&fw_mutex);
1668 if (fwe->loaded) {
1669 at76_dbg(DBG_FW, "re-using previously loaded fw");
1670 goto exit;
1673 at76_dbg(DBG_FW, "downloading firmware %s", fwe->fwname);
1674 ret = request_firmware(&fwe->fw, fwe->fwname, &udev->dev);
1675 if (ret < 0) {
1676 dev_printk(KERN_ERR, &udev->dev, "firmware %s not found!\n",
1677 fwe->fwname);
1678 dev_printk(KERN_ERR, &udev->dev,
1679 "you may need to download the firmware from "
1680 "http://developer.berlios.de/projects/at76c503a/\n");
1681 goto exit;
1684 at76_dbg(DBG_FW, "got it.");
1685 fwh = (struct at76_fw_header *)(fwe->fw->data);
1687 if (fwe->fw->size <= sizeof(*fwh)) {
1688 dev_printk(KERN_ERR, &udev->dev,
1689 "firmware is too short (0x%zx)\n", fwe->fw->size);
1690 goto exit;
1693 /* CRC currently not checked */
1694 fwe->board_type = le32_to_cpu(fwh->board_type);
1695 if (fwe->board_type != board_type) {
1696 dev_printk(KERN_ERR, &udev->dev,
1697 "board type mismatch, requested %u, got %u\n",
1698 board_type, fwe->board_type);
1699 goto exit;
1702 fwe->fw_version.major = fwh->major;
1703 fwe->fw_version.minor = fwh->minor;
1704 fwe->fw_version.patch = fwh->patch;
1705 fwe->fw_version.build = fwh->build;
1707 str = (char *)fwh + le32_to_cpu(fwh->str_offset);
1708 fwe->intfw = (u8 *)fwh + le32_to_cpu(fwh->int_fw_offset);
1709 fwe->intfw_size = le32_to_cpu(fwh->int_fw_len);
1710 fwe->extfw = (u8 *)fwh + le32_to_cpu(fwh->ext_fw_offset);
1711 fwe->extfw_size = le32_to_cpu(fwh->ext_fw_len);
1713 fwe->loaded = 1;
1715 dev_printk(KERN_DEBUG, &udev->dev,
1716 "using firmware %s (version %d.%d.%d-%d)\n",
1717 fwe->fwname, fwh->major, fwh->minor, fwh->patch, fwh->build);
1719 at76_dbg(DBG_DEVSTART, "board %u, int %d:%d, ext %d:%d", board_type,
1720 le32_to_cpu(fwh->int_fw_offset), le32_to_cpu(fwh->int_fw_len),
1721 le32_to_cpu(fwh->ext_fw_offset), le32_to_cpu(fwh->ext_fw_len));
1722 at76_dbg(DBG_DEVSTART, "firmware id %s", str);
1724 exit:
1725 mutex_unlock(&fw_mutex);
1727 if (fwe->loaded)
1728 return fwe;
1729 else
1730 return NULL;
1733 static void at76_mac80211_tx_callback(struct urb *urb)
1735 struct at76_priv *priv = urb->context;
1736 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(priv->tx_skb);
1738 at76_dbg(DBG_MAC80211, "%s()", __func__);
1740 switch (urb->status) {
1741 case 0:
1742 /* success */
1743 /* FIXME:
1744 * is the frame really ACKed when tx_callback is called ? */
1745 info->flags |= IEEE80211_TX_STAT_ACK;
1746 break;
1747 case -ENOENT:
1748 case -ECONNRESET:
1749 /* fail, urb has been unlinked */
1750 /* FIXME: add error message */
1751 break;
1752 default:
1753 at76_dbg(DBG_URB, "%s - nonzero tx status received: %d",
1754 __func__, urb->status);
1755 break;
1758 memset(&info->status, 0, sizeof(info->status));
1760 ieee80211_tx_status_irqsafe(priv->hw, priv->tx_skb);
1762 priv->tx_skb = NULL;
1764 ieee80211_wake_queues(priv->hw);
1767 static int at76_mac80211_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
1769 struct at76_priv *priv = hw->priv;
1770 struct at76_tx_buffer *tx_buffer = priv->bulk_out_buffer;
1771 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1772 int padding, submit_len, ret;
1774 at76_dbg(DBG_MAC80211, "%s()", __func__);
1776 if (priv->tx_urb->status == -EINPROGRESS) {
1777 printk(KERN_ERR "%s: %s called while tx urb is pending\n",
1778 wiphy_name(priv->hw->wiphy), __func__);
1779 return NETDEV_TX_BUSY;
1782 ieee80211_stop_queues(hw);
1784 at76_ledtrig_tx_activity(); /* tell ledtrigger we send a packet */
1786 WARN_ON(priv->tx_skb != NULL);
1788 priv->tx_skb = skb;
1789 padding = at76_calc_padding(skb->len);
1790 submit_len = AT76_TX_HDRLEN + skb->len + padding;
1792 /* setup 'Atmel' header */
1793 memset(tx_buffer, 0, sizeof(*tx_buffer));
1794 tx_buffer->padding = padding;
1795 tx_buffer->wlength = cpu_to_le16(skb->len);
1796 tx_buffer->tx_rate = ieee80211_get_tx_rate(hw, info)->hw_value;
1797 if (FIRMWARE_IS_WPA(priv->fw_version) && info->control.hw_key) {
1798 tx_buffer->key_id = (info->control.hw_key->keyidx);
1799 tx_buffer->cipher_type =
1800 priv->keys[info->control.hw_key->keyidx].cipher;
1801 tx_buffer->cipher_length =
1802 priv->keys[info->control.hw_key->keyidx].keylen;
1803 tx_buffer->reserved = 0;
1804 } else {
1805 tx_buffer->key_id = 0;
1806 tx_buffer->cipher_type = 0;
1807 tx_buffer->cipher_length = 0;
1808 tx_buffer->reserved = 0;
1810 /* memset(tx_buffer->reserved, 0, sizeof(tx_buffer->reserved)); */
1811 memcpy(tx_buffer->packet, skb->data, skb->len);
1813 at76_dbg(DBG_TX_DATA, "%s tx: wlen 0x%x pad 0x%x rate %d hdr",
1814 wiphy_name(priv->hw->wiphy), le16_to_cpu(tx_buffer->wlength),
1815 tx_buffer->padding, tx_buffer->tx_rate);
1817 /* send stuff */
1818 at76_dbg_dump(DBG_TX_DATA_CONTENT, tx_buffer, submit_len,
1819 "%s(): tx_buffer %d bytes:", __func__, submit_len);
1820 usb_fill_bulk_urb(priv->tx_urb, priv->udev, priv->tx_pipe, tx_buffer,
1821 submit_len, at76_mac80211_tx_callback, priv);
1822 ret = usb_submit_urb(priv->tx_urb, GFP_ATOMIC);
1823 if (ret) {
1824 printk(KERN_ERR "%s: error in tx submit urb: %d\n",
1825 wiphy_name(priv->hw->wiphy), ret);
1826 if (ret == -EINVAL)
1827 printk(KERN_ERR
1828 "%s: -EINVAL: tx urb %p hcpriv %p complete %p\n",
1829 wiphy_name(priv->hw->wiphy), priv->tx_urb,
1830 priv->tx_urb->hcpriv, priv->tx_urb->complete);
1833 return 0;
1836 static int at76_mac80211_start(struct ieee80211_hw *hw)
1838 struct at76_priv *priv = hw->priv;
1839 int ret;
1841 at76_dbg(DBG_MAC80211, "%s()", __func__);
1843 mutex_lock(&priv->mtx);
1845 ret = at76_submit_rx_urb(priv);
1846 if (ret < 0) {
1847 printk(KERN_ERR "%s: open: submit_rx_urb failed: %d\n",
1848 wiphy_name(priv->hw->wiphy), ret);
1849 goto error;
1852 at76_startup_device(priv);
1854 at76_start_monitor(priv);
1856 error:
1857 mutex_unlock(&priv->mtx);
1859 return 0;
1862 static void at76_mac80211_stop(struct ieee80211_hw *hw)
1864 struct at76_priv *priv = hw->priv;
1866 at76_dbg(DBG_MAC80211, "%s()", __func__);
1868 mutex_lock(&priv->mtx);
1870 if (!priv->device_unplugged) {
1871 /* We are called by "ifconfig ethX down", not because the
1872 * device is not available anymore. */
1873 if (at76_set_radio(priv, 0) == 1)
1874 at76_wait_completion(priv, CMD_RADIO_ON);
1876 /* We unlink rx_urb because at76_open() re-submits it.
1877 * If unplugged, at76_delete_device() takes care of it. */
1878 usb_kill_urb(priv->rx_urb);
1881 mutex_unlock(&priv->mtx);
1884 static int at76_add_interface(struct ieee80211_hw *hw,
1885 struct ieee80211_if_init_conf *conf)
1887 struct at76_priv *priv = hw->priv;
1888 int ret = 0;
1890 at76_dbg(DBG_MAC80211, "%s()", __func__);
1892 mutex_lock(&priv->mtx);
1894 switch (conf->type) {
1895 case NL80211_IFTYPE_STATION:
1896 priv->iw_mode = IW_MODE_INFRA;
1897 break;
1898 default:
1899 ret = -EOPNOTSUPP;
1900 goto exit;
1903 exit:
1904 mutex_unlock(&priv->mtx);
1906 return ret;
1909 static void at76_remove_interface(struct ieee80211_hw *hw,
1910 struct ieee80211_if_init_conf *conf)
1912 at76_dbg(DBG_MAC80211, "%s()", __func__);
1915 static int at76_join(struct at76_priv *priv)
1917 struct at76_req_join join;
1918 int ret;
1920 memset(&join, 0, sizeof(struct at76_req_join));
1921 memcpy(join.essid, priv->essid, priv->essid_size);
1922 join.essid_size = priv->essid_size;
1923 memcpy(join.bssid, priv->bssid, ETH_ALEN);
1924 join.bss_type = INFRASTRUCTURE_MODE;
1925 join.channel = priv->channel;
1926 join.timeout = cpu_to_le16(2000);
1928 at76_dbg(DBG_MAC80211, "%s: sending CMD_JOIN", __func__);
1929 ret = at76_set_card_command(priv->udev, CMD_JOIN, &join,
1930 sizeof(struct at76_req_join));
1932 if (ret < 0) {
1933 printk(KERN_ERR "%s: at76_set_card_command failed: %d\n",
1934 wiphy_name(priv->hw->wiphy), ret);
1935 return 0;
1938 ret = at76_wait_completion(priv, CMD_JOIN);
1939 at76_dbg(DBG_MAC80211, "%s: CMD_JOIN returned: 0x%02x", __func__, ret);
1940 if (ret != CMD_STATUS_COMPLETE) {
1941 printk(KERN_ERR "%s: at76_wait_completion failed: %d\n",
1942 wiphy_name(priv->hw->wiphy), ret);
1943 return 0;
1946 at76_set_tkip_bssid(priv, priv->bssid);
1947 at76_set_pm_mode(priv);
1949 return 0;
1952 static void at76_dwork_hw_scan(struct work_struct *work)
1954 struct at76_priv *priv = container_of(work, struct at76_priv,
1955 dwork_hw_scan.work);
1956 int ret;
1958 ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
1959 at76_dbg(DBG_MAC80211, "%s: CMD_SCAN status 0x%02x", __func__, ret);
1961 /* FIXME: add maximum time for scan to complete */
1963 if (ret != CMD_STATUS_COMPLETE) {
1964 queue_delayed_work(priv->hw->workqueue, &priv->dwork_hw_scan,
1965 SCAN_POLL_INTERVAL);
1966 goto exit;
1969 ieee80211_scan_completed(priv->hw);
1971 if (is_valid_ether_addr(priv->bssid)) {
1972 ieee80211_wake_queues(priv->hw);
1973 at76_join(priv);
1976 ieee80211_wake_queues(priv->hw);
1978 exit:
1979 return;
1982 static int at76_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
1984 struct at76_priv *priv = hw->priv;
1985 struct at76_req_scan scan;
1986 int ret;
1988 at76_dbg(DBG_MAC80211, "%s():", __func__);
1989 at76_dbg_dump(DBG_MAC80211, ssid, len, "ssid %zd bytes:", len);
1991 mutex_lock(&priv->mtx);
1993 ieee80211_stop_queues(hw);
1995 memset(&scan, 0, sizeof(struct at76_req_scan));
1996 memset(scan.bssid, 0xFF, ETH_ALEN);
1997 scan.scan_type = SCAN_TYPE_ACTIVE;
1998 if (priv->essid_size > 0) {
1999 memcpy(scan.essid, ssid, len);
2000 scan.essid_size = len;
2002 scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
2003 scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
2004 scan.probe_delay = cpu_to_le16(priv->scan_min_time * 1000);
2005 scan.international_scan = 0;
2007 at76_dbg(DBG_MAC80211, "%s: sending CMD_SCAN", __func__);
2008 ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
2010 if (ret < 0) {
2011 err("CMD_SCAN failed: %d", ret);
2012 goto exit;
2015 queue_delayed_work(priv->hw->workqueue, &priv->dwork_hw_scan,
2016 SCAN_POLL_INTERVAL);
2018 exit:
2019 mutex_unlock(&priv->mtx);
2021 return 0;
2024 static int at76_config(struct ieee80211_hw *hw, u32 changed)
2026 struct at76_priv *priv = hw->priv;
2027 struct ieee80211_conf *conf = &hw->conf;
2029 at76_dbg(DBG_MAC80211, "%s(): channel %d radio %d",
2030 __func__, conf->channel->hw_value, conf->radio_enabled);
2031 at76_dbg_dump(DBG_MAC80211, priv->essid, priv->essid_size, "ssid:");
2032 at76_dbg_dump(DBG_MAC80211, priv->bssid, ETH_ALEN, "bssid:");
2034 mutex_lock(&priv->mtx);
2036 priv->channel = conf->channel->hw_value;
2038 if (is_valid_ether_addr(priv->bssid)) {
2039 at76_join(priv);
2040 ieee80211_wake_queues(priv->hw);
2041 } else {
2042 ieee80211_stop_queues(priv->hw);
2043 at76_start_monitor(priv);
2046 mutex_unlock(&priv->mtx);
2048 return 0;
2051 static int at76_config_interface(struct ieee80211_hw *hw,
2052 struct ieee80211_vif *vif,
2053 struct ieee80211_if_conf *conf)
2055 struct at76_priv *priv = hw->priv;
2057 at76_dbg_dump(DBG_MAC80211, conf->bssid, ETH_ALEN, "bssid:");
2059 mutex_lock(&priv->mtx);
2061 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
2062 // memcpy(priv->essid, conf->ssid, conf->ssid_len);
2063 // priv->essid_size = conf->ssid_len;
2065 if (is_valid_ether_addr(priv->bssid)) {
2066 /* mac80211 is joining a bss */
2067 ieee80211_wake_queues(priv->hw);
2068 at76_join(priv);
2069 } else
2070 ieee80211_stop_queues(priv->hw);
2072 mutex_unlock(&priv->mtx);
2074 return 0;
2077 /* must be atomic */
2078 static void at76_configure_filter(struct ieee80211_hw *hw,
2079 unsigned int changed_flags,
2080 unsigned int *total_flags, int mc_count,
2081 struct dev_addr_list *mc_list)
2083 struct at76_priv *priv = hw->priv;
2084 int flags;
2086 at76_dbg(DBG_MAC80211, "%s(): changed_flags=0x%08x "
2087 "total_flags=0x%08x mc_count=%d",
2088 __func__, changed_flags, *total_flags, mc_count);
2090 flags = changed_flags & AT76_SUPPORTED_FILTERS;
2091 *total_flags = AT76_SUPPORTED_FILTERS;
2093 /* FIXME: access to priv->promisc should be protected with
2094 * priv->mtx, but it's impossible because this function needs to be
2095 * atomic */
2097 if (flags && !priv->promisc) {
2098 /* mac80211 wants us to enable promiscuous mode */
2099 priv->promisc = 1;
2100 } else if (!flags && priv->promisc) {
2101 /* we need to disable promiscuous mode */
2102 priv->promisc = 0;
2103 } else
2104 return;
2106 queue_work(hw->workqueue, &priv->work_set_promisc);
2109 static int at76_set_key_oldfw(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2110 const u8 *local_address, const u8 *address,
2111 struct ieee80211_key_conf *key)
2113 struct at76_priv *priv = hw->priv;
2115 int i;
2117 at76_dbg(DBG_MAC80211, "%s(): cmd %d key->alg %d key->keyidx %d "
2118 "key->keylen %d",
2119 __func__, cmd, key->alg, key->keyidx, key->keylen);
2121 if (key->alg != ALG_WEP)
2122 return -EOPNOTSUPP;
2124 key->hw_key_idx = key->keyidx;
2126 mutex_lock(&priv->mtx);
2128 switch (cmd) {
2129 case SET_KEY:
2130 memcpy(priv->wep_keys[key->keyidx], key->key, key->keylen);
2131 priv->wep_keys_len[key->keyidx] = key->keylen;
2133 /* FIXME: find out how to do this properly */
2134 priv->wep_key_id = key->keyidx;
2136 break;
2137 case DISABLE_KEY:
2138 default:
2139 priv->wep_keys_len[key->keyidx] = 0;
2140 break;
2143 priv->wep_enabled = 0;
2145 for (i = 0; i < WEP_KEYS; i++) {
2146 if (priv->wep_keys_len[i] != 0)
2147 priv->wep_enabled = 1;
2150 at76_startup_device(priv);
2152 mutex_unlock(&priv->mtx);
2154 return 0;
2157 static int at76_set_key_newfw(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2158 const u8 *local_address, const u8 *address,
2159 struct ieee80211_key_conf *key)
2161 struct at76_priv *priv = hw->priv;
2162 int ret = -EOPNOTSUPP;
2164 at76_dbg(DBG_MAC80211, "%s(): cmd %d key->alg %d key->keyidx %d "
2165 "key->keylen %d",
2166 __func__, cmd, key->alg, key->keyidx, key->keylen);
2168 mutex_lock(&priv->mtx);
2170 priv->mib_buf.type = MIB_MAC_ENCRYPTION;
2172 if (cmd == DISABLE_KEY) {
2173 priv->mib_buf.size = CIPHER_KEY_LEN;
2174 priv->mib_buf.index = offsetof(struct mib_mac_encryption,
2175 cipher_default_keyvalue[key->keyidx]);
2176 memset(priv->mib_buf.data.data, 0, CIPHER_KEY_LEN);
2177 if (at76_set_mib(priv, &priv->mib_buf) != CMD_STATUS_COMPLETE)
2178 ret = -EOPNOTSUPP; /* -EIO would be probably better */
2179 else {
2181 priv->keys[key->keyidx].cipher = CIPHER_NONE;
2182 priv->keys[key->keyidx].keylen = 0;
2184 if (priv->default_group_key == key->keyidx)
2185 priv->default_group_key = 0xff;
2187 if (priv->default_pairwise_key == key->keyidx)
2188 priv->default_pairwise_key = 0xff;
2189 /* If default pairwise key is removed, fall back to
2190 * group key? */
2191 ret = 0;
2192 goto exit;
2195 if (cmd == SET_KEY) {
2196 /* store key into MIB */
2197 priv->mib_buf.size = CIPHER_KEY_LEN;
2198 priv->mib_buf.index = offsetof(struct mib_mac_encryption,
2199 cipher_default_keyvalue[key->keyidx]);
2200 memset(priv->mib_buf.data.data, 0, CIPHER_KEY_LEN);
2201 memcpy(priv->mib_buf.data.data, key->key, key->keylen);
2203 switch (key->alg) {
2204 case ALG_WEP:
2205 if (key->keylen == 5) {
2206 priv->keys[key->keyidx].cipher =
2207 CIPHER_WEP64;
2208 priv->keys[key->keyidx].keylen = 8;
2209 } else if (key->keylen == 13) {
2210 priv->keys[key->keyidx].cipher =
2211 CIPHER_WEP128;
2212 /* Firmware needs this */
2213 priv->keys[key->keyidx].keylen = 8;
2214 } else {
2215 ret = -EOPNOTSUPP;
2216 goto exit;
2218 break;
2219 case ALG_TKIP:
2220 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2221 priv->keys[key->keyidx].cipher = CIPHER_TKIP;
2222 priv->keys[key->keyidx].keylen = 12;
2223 break;
2225 case ALG_CCMP:
2226 if (!at76_is_505a(priv->board_type)) {
2227 ret = -EOPNOTSUPP;
2228 goto exit;
2230 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
2231 priv->keys[key->keyidx].cipher = CIPHER_CCMP;
2232 priv->keys[key->keyidx].keylen = 16;
2233 break;
2235 default:
2236 ret = -EOPNOTSUPP;
2237 goto exit;
2240 priv->mib_buf.data.data[38] = priv->keys[key->keyidx].cipher;
2241 priv->mib_buf.data.data[39] = 1; /* Taken from atmelwlandriver,
2242 not documented */
2244 if (is_valid_ether_addr(address))
2245 /* Pairwise key */
2246 priv->mib_buf.data.data[39] |= (KEY_PAIRWISE | KEY_TX);
2247 else if (is_broadcast_ether_addr(address))
2248 /* Group key */
2249 priv->mib_buf.data.data[39] |= (KEY_TX);
2250 else /* Key used only for transmission ??? */
2251 priv->mib_buf.data.data[39] |= (KEY_TX);
2253 if (at76_set_mib(priv, &priv->mib_buf) !=
2254 CMD_STATUS_COMPLETE) {
2255 ret = -EOPNOTSUPP; /* -EIO would be probably better */
2256 goto exit;
2259 if ((key->alg == ALG_TKIP) || (key->alg == ALG_CCMP))
2260 at76_reset_rsc(priv);
2262 key->hw_key_idx = key->keyidx;
2264 /* Set up default keys */
2265 if (is_broadcast_ether_addr(address))
2266 priv->default_group_key = key->keyidx;
2267 if (is_valid_ether_addr(address))
2268 priv->default_pairwise_key = key->keyidx;
2270 /* Set up encryption MIBs */
2272 /* first block of settings */
2273 priv->mib_buf.size = 3;
2274 priv->mib_buf.index = offsetof(struct mib_mac_encryption,
2275 privacy_invoked);
2276 priv->mib_buf.data.data[0] = 1; /* privacy_invoked */
2277 priv->mib_buf.data.data[1] = priv->default_pairwise_key;
2278 priv->mib_buf.data.data[2] = priv->default_group_key;
2280 ret = at76_set_mib(priv, &priv->mib_buf);
2281 if (ret != CMD_STATUS_COMPLETE)
2282 goto exit;
2284 /* second block of settings */
2285 priv->mib_buf.size = 3;
2286 priv->mib_buf.index = offsetof(struct mib_mac_encryption,
2287 exclude_unencrypted);
2288 priv->mib_buf.data.data[0] = 1; /* exclude_unencrypted */
2289 priv->mib_buf.data.data[1] = 0; /* wep_encryption_type */
2290 priv->mib_buf.data.data[2] = 0; /* ckip_key_permutation */
2292 ret = at76_set_mib(priv, &priv->mib_buf);
2293 if (ret != CMD_STATUS_COMPLETE)
2294 goto exit;
2295 ret = 0;
2297 exit:
2298 at76_dump_mib_mac_encryption(priv);
2299 mutex_unlock(&priv->mtx);
2300 return ret;
2303 static int at76_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
2304 const u8 *local_address, const u8 *address,
2305 struct ieee80211_key_conf *key)
2307 struct at76_priv *priv = hw->priv;
2309 at76_dbg(DBG_MAC80211, "%s(): cmd %d key->alg %d key->keyidx %d "
2310 "key->keylen %d",
2311 __func__, cmd, key->alg, key->keyidx, key->keylen);
2313 if (FIRMWARE_IS_WPA(priv->fw_version))
2314 return at76_set_key_newfw(hw, cmd, local_address, address, key);
2315 else
2316 return at76_set_key_oldfw(hw, cmd, local_address, address, key);
2320 static const struct ieee80211_ops at76_ops = {
2321 .tx = at76_mac80211_tx,
2322 .add_interface = at76_add_interface,
2323 .remove_interface = at76_remove_interface,
2324 .config = at76_config,
2325 .config_interface = at76_config_interface,
2326 .configure_filter = at76_configure_filter,
2327 .start = at76_mac80211_start,
2328 .stop = at76_mac80211_stop,
2329 .hw_scan = at76_hw_scan,
2330 .set_key = at76_set_key,
2333 /* Allocate network device and initialize private data */
2334 static struct at76_priv *at76_alloc_new_device(struct usb_device *udev)
2336 struct ieee80211_hw *hw;
2337 struct at76_priv *priv;
2339 hw = ieee80211_alloc_hw(sizeof(struct at76_priv), &at76_ops);
2340 if (!hw) {
2341 printk(KERN_ERR DRIVER_NAME ": could not register"
2342 " ieee80211_hw\n");
2343 return NULL;
2346 priv = hw->priv;
2347 priv->hw = hw;
2349 priv->udev = udev;
2351 mutex_init(&priv->mtx);
2352 INIT_WORK(&priv->work_set_promisc, at76_work_set_promisc);
2353 INIT_WORK(&priv->work_submit_rx, at76_work_submit_rx);
2354 INIT_DELAYED_WORK(&priv->dwork_hw_scan, at76_dwork_hw_scan);
2356 priv->rx_tasklet.func = at76_rx_tasklet;
2357 priv->rx_tasklet.data = 0;
2359 priv->pm_mode = AT76_PM_OFF;
2360 priv->pm_period = 0;
2362 /* unit us */
2363 priv->hw->channel_change_time = 100000;
2365 return priv;
2368 static int at76_alloc_urbs(struct at76_priv *priv,
2369 struct usb_interface *interface)
2371 struct usb_endpoint_descriptor *endpoint, *ep_in, *ep_out;
2372 int i;
2373 int buffer_size;
2374 struct usb_host_interface *iface_desc;
2376 at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2378 at76_dbg(DBG_URB, "%s: NumEndpoints %d ", __func__,
2379 interface->altsetting[0].desc.bNumEndpoints);
2381 ep_in = NULL;
2382 ep_out = NULL;
2383 iface_desc = interface->cur_altsetting;
2384 for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
2385 endpoint = &iface_desc->endpoint[i].desc;
2387 at76_dbg(DBG_URB, "%s: %d. endpoint: addr 0x%x attr 0x%x",
2388 __func__, i, endpoint->bEndpointAddress,
2389 endpoint->bmAttributes);
2391 if (!ep_in && usb_endpoint_is_bulk_in(endpoint))
2392 ep_in = endpoint;
2394 if (!ep_out && usb_endpoint_is_bulk_out(endpoint))
2395 ep_out = endpoint;
2398 if (!ep_in || !ep_out) {
2399 dev_printk(KERN_ERR, &interface->dev,
2400 "bulk endpoints missing\n");
2401 return -ENXIO;
2404 priv->rx_pipe = usb_rcvbulkpipe(priv->udev, ep_in->bEndpointAddress);
2405 priv->tx_pipe = usb_sndbulkpipe(priv->udev, ep_out->bEndpointAddress);
2407 priv->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
2408 priv->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
2409 if (!priv->rx_urb || !priv->tx_urb) {
2410 dev_printk(KERN_ERR, &interface->dev, "cannot allocate URB\n");
2411 return -ENOMEM;
2414 buffer_size = sizeof(struct at76_tx_buffer) + MAX_PADDING_SIZE;
2415 priv->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
2416 if (!priv->bulk_out_buffer) {
2417 dev_printk(KERN_ERR, &interface->dev,
2418 "cannot allocate output buffer\n");
2419 return -ENOMEM;
2422 at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2424 return 0;
2427 static struct ieee80211_rate at76_rates[] = {
2428 { .bitrate = 10, .hw_value = TX_RATE_1MBIT, },
2429 { .bitrate = 20, .hw_value = TX_RATE_2MBIT, },
2430 { .bitrate = 55, .hw_value = TX_RATE_5_5MBIT, },
2431 { .bitrate = 110, .hw_value = TX_RATE_11MBIT, },
2434 static struct ieee80211_channel at76_channels[] = {
2435 { .center_freq = 2412, .hw_value = 1 },
2436 { .center_freq = 2417, .hw_value = 2 },
2437 { .center_freq = 2422, .hw_value = 3 },
2438 { .center_freq = 2427, .hw_value = 4 },
2439 { .center_freq = 2432, .hw_value = 5 },
2440 { .center_freq = 2437, .hw_value = 6 },
2441 { .center_freq = 2442, .hw_value = 7 },
2442 { .center_freq = 2447, .hw_value = 8 },
2443 { .center_freq = 2452, .hw_value = 9 },
2444 { .center_freq = 2457, .hw_value = 10 },
2445 { .center_freq = 2462, .hw_value = 11 },
2446 { .center_freq = 2467, .hw_value = 12 },
2447 { .center_freq = 2472, .hw_value = 13 },
2448 { .center_freq = 2484, .hw_value = 14 }
2451 static struct ieee80211_supported_band at76_supported_band = {
2452 .channels = at76_channels,
2453 .n_channels = ARRAY_SIZE(at76_channels),
2454 .bitrates = at76_rates,
2455 .n_bitrates = ARRAY_SIZE(at76_rates),
2458 /* Register network device and initialize the hardware */
2459 static int at76_init_new_device(struct at76_priv *priv,
2460 struct usb_interface *interface)
2462 struct device *dev = &interface->dev;
2463 int ret;
2465 /* set up the endpoint information */
2466 /* check out the endpoints */
2468 at76_dbg(DBG_DEVSTART, "USB interface: %d endpoints",
2469 interface->cur_altsetting->desc.bNumEndpoints);
2471 ret = at76_alloc_urbs(priv, interface);
2472 if (ret < 0)
2473 goto exit;
2475 /* MAC address */
2476 ret = at76_get_hw_config(priv);
2477 if (ret < 0) {
2478 dev_err(dev, "cannot get MAC address\n");
2479 goto exit;
2482 priv->domain = at76_get_reg_domain(priv->regulatory_domain);
2484 priv->channel = DEF_CHANNEL;
2485 priv->iw_mode = IW_MODE_INFRA;
2486 priv->rts_threshold = DEF_RTS_THRESHOLD;
2487 priv->frag_threshold = DEF_FRAG_THRESHOLD;
2488 priv->short_retry_limit = DEF_SHORT_RETRY_LIMIT;
2489 priv->txrate = TX_RATE_AUTO;
2490 priv->preamble_type = PREAMBLE_TYPE_LONG;
2491 priv->beacon_period = 100;
2492 priv->auth_mode = WLAN_AUTH_OPEN;
2493 priv->scan_min_time = DEF_SCAN_MIN_TIME;
2494 priv->scan_max_time = DEF_SCAN_MAX_TIME;
2495 priv->scan_mode = SCAN_TYPE_ACTIVE;
2496 priv->default_pairwise_key = 0xff;
2497 priv->default_group_key = 0xff;
2499 /* mac80211 initialisation */
2500 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &at76_supported_band;
2502 if (FIRMWARE_IS_WPA(priv->fw_version) &&
2503 (at76_is_503rfmd(priv->board_type) ||
2504 at76_is_505(priv->board_type)))
2505 priv->hw->flags = IEEE80211_HW_SIGNAL_UNSPEC;
2506 else
2507 priv->hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
2508 IEEE80211_HW_SIGNAL_UNSPEC;
2510 priv->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
2512 SET_IEEE80211_DEV(priv->hw, &interface->dev);
2513 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
2515 ret = ieee80211_register_hw(priv->hw);
2516 if (ret) {
2517 dev_err(dev, "cannot register mac80211 hw (status %d)!\n", ret);
2518 goto exit;
2521 priv->mac80211_registered = 1;
2523 dev_info(dev, "%s: USB %s, MAC %s, firmware %d.%d.%d-%d\n",
2524 wiphy_name(priv->hw->wiphy),
2525 interface->dev.bus_id, mac2str(priv->mac_addr),
2526 priv->fw_version.major, priv->fw_version.minor,
2527 priv->fw_version.patch, priv->fw_version.build);
2528 dev_info(dev, "%s: regulatory domain 0x%02x: %s\n",
2529 wiphy_name(priv->hw->wiphy),
2530 priv->regulatory_domain, priv->domain->name);
2531 dev_info(dev, "%s: WPA support: ", wiphy_name(priv->hw->wiphy));
2532 if (!FIRMWARE_IS_WPA(priv->fw_version))
2533 printk("none\n");
2534 else {
2535 if (!at76_is_505a(priv->board_type))
2536 printk("TKIP\n");
2537 else
2538 printk("TKIP, AES/CCMP\n");
2541 exit:
2542 return ret;
2545 static void at76_delete_device(struct at76_priv *priv)
2547 at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
2549 /* The device is gone, don't bother turning it off */
2550 priv->device_unplugged = 1;
2552 if (priv->mac80211_registered)
2553 ieee80211_unregister_hw(priv->hw);
2555 /* assuming we used keventd, it must quiesce too */
2556 flush_scheduled_work();
2558 kfree(priv->bulk_out_buffer);
2560 if (priv->tx_urb) {
2561 usb_kill_urb(priv->tx_urb);
2562 usb_free_urb(priv->tx_urb);
2564 if (priv->rx_urb) {
2565 usb_kill_urb(priv->rx_urb);
2566 usb_free_urb(priv->rx_urb);
2569 at76_dbg(DBG_PROC_ENTRY, "%s: unlinked urbs", __func__);
2571 if (priv->rx_skb)
2572 kfree_skb(priv->rx_skb);
2574 usb_put_dev(priv->udev);
2576 at76_dbg(DBG_PROC_ENTRY, "%s: before freeing priv/ieee80211_hw",
2577 __func__);
2578 ieee80211_free_hw(priv->hw);
2580 at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
2583 static int at76_probe(struct usb_interface *interface,
2584 const struct usb_device_id *id)
2586 int ret;
2587 struct at76_priv *priv;
2588 struct fwentry *fwe;
2589 struct usb_device *udev;
2590 int op_mode;
2591 int need_ext_fw = 0;
2592 struct mib_fw_version fwv;
2593 int board_type = (int)id->driver_info;
2595 udev = usb_get_dev(interface_to_usbdev(interface));
2597 /* Load firmware into kernel memory */
2598 fwe = at76_load_firmware(udev, board_type);
2599 if (!fwe) {
2600 ret = -ENOENT;
2601 goto error;
2604 op_mode = at76_get_op_mode(udev);
2606 at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
2608 /* we get OPMODE_NONE with 2.4.23, SMC2662W-AR ???
2609 we get 204 with 2.4.23, Fiberline FL-WL240u (505A+RFMD2958) ??? */
2611 if (op_mode == OPMODE_HW_CONFIG_MODE) {
2612 dev_err(&interface->dev,
2613 "cannot handle a device in HW_CONFIG_MODE\n");
2614 ret = -EBUSY;
2615 goto error;
2618 if (op_mode != OPMODE_NORMAL_NIC_WITH_FLASH
2619 && op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
2620 /* download internal firmware part */
2621 dev_dbg(&interface->dev, "downloading internal firmware\n");
2622 ret = at76_load_internal_fw(udev, fwe);
2623 if (ret < 0) {
2624 dev_err(&interface->dev,
2625 "error %d downloading internal firmware\n",
2626 ret);
2627 goto error;
2629 usb_put_dev(udev);
2630 return ret;
2633 /* Internal firmware already inside the device. Get firmware
2634 * version to test if external firmware is loaded.
2635 * This works only for newer firmware, e.g. the Intersil 0.90.x
2636 * says "control timeout on ep0in" and subsequent
2637 * at76_get_op_mode() fail too :-( */
2639 /* if version >= 0.100.x.y or device with built-in flash we can
2640 * query the device for the fw version */
2641 if ((fwe->fw_version.major > 0 || fwe->fw_version.minor >= 100)
2642 || (op_mode == OPMODE_NORMAL_NIC_WITH_FLASH)) {
2643 ret = at76_get_mib(udev, MIB_FW_VERSION, &fwv, sizeof(fwv));
2644 if (ret < 0 || (fwv.major | fwv.minor) == 0)
2645 need_ext_fw = 1;
2646 } else
2647 /* No way to check firmware version, reload to be sure */
2648 need_ext_fw = 1;
2650 if (need_ext_fw) {
2651 dev_dbg(&interface->dev, "downloading external firmware\n");
2653 ret = at76_load_external_fw(udev, fwe);
2654 if (ret)
2655 goto error;
2657 /* Re-check firmware version */
2658 ret = at76_get_mib(udev, MIB_FW_VERSION, &fwv, sizeof(fwv));
2659 if (ret < 0) {
2660 dev_err(&interface->dev,
2661 "error %d getting firmware version\n", ret);
2662 goto error;
2666 priv = at76_alloc_new_device(udev);
2667 if (!priv) {
2668 ret = -ENOMEM;
2669 goto error;
2672 usb_set_intfdata(interface, priv);
2674 memcpy(&priv->fw_version, &fwv, sizeof(struct mib_fw_version));
2675 priv->board_type = board_type;
2677 ret = at76_init_new_device(priv, interface);
2678 if (ret < 0)
2679 at76_delete_device(priv);
2681 return ret;
2683 error:
2684 usb_put_dev(udev);
2685 return ret;
2688 static void at76_disconnect(struct usb_interface *interface)
2690 struct at76_priv *priv;
2692 priv = usb_get_intfdata(interface);
2693 usb_set_intfdata(interface, NULL);
2695 /* Disconnect after loading internal firmware */
2696 if (!priv)
2697 return;
2699 printk(KERN_INFO "%s: disconnecting\n", wiphy_name(priv->hw->wiphy));
2700 at76_delete_device(priv);
2701 dev_printk(KERN_INFO, &interface->dev, "disconnected\n");
2704 /* Structure for registering this driver with the USB subsystem */
2705 static struct usb_driver at76_driver = {
2706 .name = DRIVER_NAME,
2707 .probe = at76_probe,
2708 .disconnect = at76_disconnect,
2709 .id_table = dev_table,
2712 static int __init at76_mod_init(void)
2714 int result;
2716 printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " loading\n");
2718 mutex_init(&fw_mutex);
2720 /* register this driver with the USB subsystem */
2721 result = usb_register(&at76_driver);
2722 if (result < 0)
2723 printk(KERN_ERR DRIVER_NAME
2724 ": usb_register failed (status %d)\n", result);
2726 led_trigger_register_simple("at76_usb-tx", &ledtrig_tx);
2727 return result;
2730 static void __exit at76_mod_exit(void)
2732 int i;
2734 printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " unloading\n");
2735 usb_deregister(&at76_driver);
2736 for (i = 0; i < ARRAY_SIZE(firmwares); i++) {
2737 if (firmwares[i].fw)
2738 release_firmware(firmwares[i].fw);
2740 led_trigger_unregister_simple(ledtrig_tx);
2743 module_param_named(debug, at76_debug, int, 0600);
2744 MODULE_PARM_DESC(debug, "Debugging level");
2746 module_init(at76_mod_init);
2747 module_exit(at76_mod_exit);
2749 MODULE_AUTHOR("Oliver Kurth <oku@masqmail.cx>");
2750 MODULE_AUTHOR("Joerg Albert <joerg.albert@gmx.de>");
2751 MODULE_AUTHOR("Alex <alex@foogod.com>");
2752 MODULE_AUTHOR("Nick Jones");
2753 MODULE_AUTHOR("Balint Seeber <n0_5p4m_p13453@hotmail.com>");
2754 MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
2755 MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
2756 MODULE_AUTHOR("Kalle Valo <kalle.valo@iki.fi>");
2757 MODULE_AUTHOR("Milan Plzik <milan.plzik@gmail.com>");
2758 MODULE_DESCRIPTION(DRIVER_DESC);
2759 MODULE_LICENSE("GPL");