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