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