1 /*
2  * Copyright (c) 2006 Damien Bergamini <damien.bergamini@free.fr>
3  * Copyright (c) 2006 Sam Leffler, Errno Consulting
4  * Copyright (c) 2007 Christoph Hellwig <hch@lst.de>
5  * Copyright (c) 2008-2009 Weongyo Jeong <weongyo@freebsd.org>
6  * Copyright (c) 2012 Pontus Fuchs <pontus.fuchs@gmail.com>
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20 
21 /*
22  * This driver is based on the uath driver written by Damien Bergamini for
23  * OpenBSD, who did black-box analysis of the Windows binary driver to find
24  * out how the hardware works.  It contains a lot magic numbers because of
25  * that and only has minimal functionality.
26  */
27 #include <linux/compiler.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/list.h>
31 #include <linux/completion.h>
32 #include <linux/firmware.h>
33 #include <linux/skbuff.h>
34 #include <linux/usb.h>
35 #include <net/mac80211.h>
36 
37 #include "ar5523.h"
38 #include "ar5523_hw.h"
39 
40 /*
41  * Various supported device vendors/products.
42  * UB51: AR5005UG 802.11b/g, UB52: AR5005UX 802.11a/b/g
43  */
44 
45 static int ar5523_submit_rx_cmd(struct ar5523 *ar);
46 static void ar5523_data_tx_pkt_put(struct ar5523 *ar);
47 
48 static void ar5523_read_reply(struct ar5523 *ar, struct ar5523_cmd_hdr *hdr,
49 			      struct ar5523_tx_cmd *cmd)
50 {
51 	int dlen, olen;
52 	__be32 *rp;
53 
54 	dlen = be32_to_cpu(hdr->len) - sizeof(*hdr);
55 
56 	if (dlen < 0) {
57 		WARN_ON(1);
58 		goto out;
59 	}
60 
61 	ar5523_dbg(ar, "Code = %d len = %d\n", be32_to_cpu(hdr->code) & 0xff,
62 		   dlen);
63 
64 	rp = (__be32 *)(hdr + 1);
65 	if (dlen >= sizeof(u32)) {
66 		olen = be32_to_cpu(rp[0]);
67 		dlen -= sizeof(u32);
68 		if (olen == 0) {
69 			/* convention is 0 =>'s one word */
70 			olen = sizeof(u32);
71 		}
72 	} else
73 		olen = 0;
74 
75 	if (cmd->odata) {
76 		if (cmd->olen < olen) {
77 			ar5523_err(ar, "olen too small %d < %d\n",
78 				   cmd->olen, olen);
79 			cmd->olen = 0;
80 			cmd->res = -EOVERFLOW;
81 		} else {
82 			cmd->olen = olen;
83 			memcpy(cmd->odata, &rp[1], olen);
84 			cmd->res = 0;
85 		}
86 	}
87 
88 out:
89 	complete(&cmd->done);
90 }
91 
92 static void ar5523_cmd_rx_cb(struct urb *urb)
93 {
94 	struct ar5523 *ar = urb->context;
95 	struct ar5523_tx_cmd *cmd = &ar->tx_cmd;
96 	struct ar5523_cmd_hdr *hdr = ar->rx_cmd_buf;
97 	int dlen;
98 	u32 code, hdrlen;
99 
100 	if (urb->status) {
101 		if (urb->status != -ESHUTDOWN)
102 			ar5523_err(ar, "RX USB error %d.\n", urb->status);
103 		goto skip;
104 	}
105 
106 	if (urb->actual_length < sizeof(struct ar5523_cmd_hdr)) {
107 		ar5523_err(ar, "RX USB too short.\n");
108 		goto skip;
109 	}
110 
111 	ar5523_dbg(ar, "%s code %02x priv %d\n", __func__,
112 		   be32_to_cpu(hdr->code) & 0xff, hdr->priv);
113 
114 	code = be32_to_cpu(hdr->code);
115 	hdrlen = be32_to_cpu(hdr->len);
116 
117 	switch (code & 0xff) {
118 	default:
119 		/* reply to a read command */
120 		if (hdr->priv != AR5523_CMD_ID) {
121 			ar5523_err(ar, "Unexpected command id: %02x\n",
122 				   code & 0xff);
123 			goto skip;
124 		}
125 		ar5523_read_reply(ar, hdr, cmd);
126 		break;
127 
128 	case WDCMSG_DEVICE_AVAIL:
129 		ar5523_dbg(ar, "WDCMSG_DEVICE_AVAIL\n");
130 		cmd->res = 0;
131 		cmd->olen = 0;
132 		complete(&cmd->done);
133 		break;
134 
135 	case WDCMSG_SEND_COMPLETE:
136 		ar5523_dbg(ar, "WDCMSG_SEND_COMPLETE: %d pending\n",
137 			atomic_read(&ar->tx_nr_pending));
138 		if (!test_bit(AR5523_HW_UP, &ar->flags))
139 			ar5523_dbg(ar, "Unexpected WDCMSG_SEND_COMPLETE\n");
140 		else {
141 			mod_timer(&ar->tx_wd_timer,
142 				  jiffies + AR5523_TX_WD_TIMEOUT);
143 			ar5523_data_tx_pkt_put(ar);
144 
145 		}
146 		break;
147 
148 	case WDCMSG_TARGET_START:
149 		/* This command returns a bogus id so it needs special
150 		   handling */
151 		dlen = hdrlen - sizeof(*hdr);
152 		if (dlen != (int)sizeof(u32)) {
153 			ar5523_err(ar, "Invalid reply to WDCMSG_TARGET_START");
154 			return;
155 		}
156 		if (!cmd->odata) {
157 			ar5523_err(ar, "Unexpected WDCMSG_TARGET_START reply");
158 			return;
159 		}
160 		memcpy(cmd->odata, hdr + 1, sizeof(u32));
161 		cmd->olen = sizeof(u32);
162 		cmd->res = 0;
163 		complete(&cmd->done);
164 		break;
165 
166 	case WDCMSG_STATS_UPDATE:
167 		ar5523_dbg(ar, "WDCMSG_STATS_UPDATE\n");
168 		break;
169 	}
170 
171 skip:
172 	ar5523_submit_rx_cmd(ar);
173 }
174 
175 static int ar5523_alloc_rx_cmd(struct ar5523 *ar)
176 {
177 	ar->rx_cmd_urb = usb_alloc_urb(0, GFP_KERNEL);
178 	if (!ar->rx_cmd_urb)
179 		return -ENOMEM;
180 
181 	ar->rx_cmd_buf = usb_alloc_coherent(ar->dev, AR5523_MAX_RXCMDSZ,
182 					    GFP_KERNEL,
183 					    &ar->rx_cmd_urb->transfer_dma);
184 	if (!ar->rx_cmd_buf) {
185 		usb_free_urb(ar->rx_cmd_urb);
186 		return -ENOMEM;
187 	}
188 	return 0;
189 }
190 
191 static void ar5523_cancel_rx_cmd(struct ar5523 *ar)
192 {
193 	usb_kill_urb(ar->rx_cmd_urb);
194 }
195 
196 static void ar5523_free_rx_cmd(struct ar5523 *ar)
197 {
198 	usb_free_coherent(ar->dev, AR5523_MAX_RXCMDSZ,
199 			  ar->rx_cmd_buf, ar->rx_cmd_urb->transfer_dma);
200 	usb_free_urb(ar->rx_cmd_urb);
201 }
202 
203 static int ar5523_submit_rx_cmd(struct ar5523 *ar)
204 {
205 	int error;
206 
207 	usb_fill_bulk_urb(ar->rx_cmd_urb, ar->dev,
208 			  ar5523_cmd_rx_pipe(ar->dev), ar->rx_cmd_buf,
209 			  AR5523_MAX_RXCMDSZ, ar5523_cmd_rx_cb, ar);
210 	ar->rx_cmd_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
211 
212 	error = usb_submit_urb(ar->rx_cmd_urb, GFP_ATOMIC);
213 	if (error) {
214 		if (error != -ENODEV)
215 			ar5523_err(ar, "error %d when submitting rx urb\n",
216 				   error);
217 		return error;
218 	}
219 	return 0;
220 }
221 
222 /*
223  * Command submitted cb
224  */
225 static void ar5523_cmd_tx_cb(struct urb *urb)
226 {
227 	struct ar5523_tx_cmd *cmd = urb->context;
228 	struct ar5523 *ar = cmd->ar;
229 
230 	if (urb->status) {
231 		ar5523_err(ar, "Failed to TX command. Status = %d\n",
232 			   urb->status);
233 		cmd->res = urb->status;
234 		complete(&cmd->done);
235 		return;
236 	}
237 
238 	if (!(cmd->flags & AR5523_CMD_FLAG_READ)) {
239 		cmd->res = 0;
240 		complete(&cmd->done);
241 	}
242 }
243 
244 static int ar5523_cmd(struct ar5523 *ar, u32 code, const void *idata,
245 		      int ilen, void *odata, int olen, int flags)
246 {
247 	struct ar5523_cmd_hdr *hdr;
248 	struct ar5523_tx_cmd *cmd = &ar->tx_cmd;
249 	int xferlen, error;
250 
251 	/* always bulk-out a multiple of 4 bytes */
252 	xferlen = (sizeof(struct ar5523_cmd_hdr) + ilen + 3) & ~3;
253 
254 	hdr = (struct ar5523_cmd_hdr *)cmd->buf_tx;
255 	memset(hdr, 0, sizeof(struct ar5523_cmd_hdr));
256 	hdr->len  = cpu_to_be32(xferlen);
257 	hdr->code = cpu_to_be32(code);
258 	hdr->priv = AR5523_CMD_ID;
259 
260 	if (flags & AR5523_CMD_FLAG_MAGIC)
261 		hdr->magic = cpu_to_be32(1 << 24);
262 	if (ilen)
263 		memcpy(hdr + 1, idata, ilen);
264 
265 	cmd->odata = odata;
266 	cmd->olen = olen;
267 	cmd->flags = flags;
268 
269 	ar5523_dbg(ar, "do cmd %02x\n", code);
270 
271 	usb_fill_bulk_urb(cmd->urb_tx, ar->dev, ar5523_cmd_tx_pipe(ar->dev),
272 			  cmd->buf_tx, xferlen, ar5523_cmd_tx_cb, cmd);
273 	cmd->urb_tx->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
274 
275 	error = usb_submit_urb(cmd->urb_tx, GFP_KERNEL);
276 	if (error) {
277 		ar5523_err(ar, "could not send command 0x%x, error=%d\n",
278 			   code, error);
279 		return error;
280 	}
281 
282 	if (!wait_for_completion_timeout(&cmd->done, 2 * HZ)) {
283 		cmd->odata = NULL;
284 		ar5523_err(ar, "timeout waiting for command %02x reply\n",
285 			   code);
286 		cmd->res = -ETIMEDOUT;
287 	}
288 	return cmd->res;
289 }
290 
291 static int ar5523_cmd_write(struct ar5523 *ar, u32 code, const void *data,
292 			    int len, int flags)
293 {
294 	flags &= ~AR5523_CMD_FLAG_READ;
295 	return ar5523_cmd(ar, code, data, len, NULL, 0, flags);
296 }
297 
298 static int ar5523_cmd_read(struct ar5523 *ar, u32 code, const void *idata,
299 			   int ilen, void *odata, int olen, int flags)
300 {
301 	flags |= AR5523_CMD_FLAG_READ;
302 	return ar5523_cmd(ar, code, idata, ilen, odata, olen, flags);
303 }
304 
305 static int ar5523_config(struct ar5523 *ar, u32 reg, u32 val)
306 {
307 	struct ar5523_write_mac write;
308 	int error;
309 
310 	write.reg = cpu_to_be32(reg);
311 	write.len = cpu_to_be32(0);	/* 0 = single write */
312 	*(__be32 *)write.data = cpu_to_be32(val);
313 
314 	error = ar5523_cmd_write(ar, WDCMSG_TARGET_SET_CONFIG, &write,
315 				 3 * sizeof(u32), 0);
316 	if (error != 0)
317 		ar5523_err(ar, "could not write register 0x%02x\n", reg);
318 	return error;
319 }
320 
321 static int ar5523_config_multi(struct ar5523 *ar, u32 reg, const void *data,
322 			       int len)
323 {
324 	struct ar5523_write_mac write;
325 	int error;
326 
327 	write.reg = cpu_to_be32(reg);
328 	write.len = cpu_to_be32(len);
329 	memcpy(write.data, data, len);
330 
331 	/* properly handle the case where len is zero (reset) */
332 	error = ar5523_cmd_write(ar, WDCMSG_TARGET_SET_CONFIG, &write,
333 	    (len == 0) ? sizeof(u32) : 2 * sizeof(u32) + len, 0);
334 	if (error != 0)
335 		ar5523_err(ar, "could not write %d bytes to register 0x%02x\n",
336 			   len, reg);
337 	return error;
338 }
339 
340 static int ar5523_get_status(struct ar5523 *ar, u32 which, void *odata,
341 			     int olen)
342 {
343 	int error;
344 	__be32 which_be;
345 
346 	which_be = cpu_to_be32(which);
347 	error = ar5523_cmd_read(ar, WDCMSG_TARGET_GET_STATUS,
348 	    &which_be, sizeof(which_be), odata, olen, AR5523_CMD_FLAG_MAGIC);
349 	if (error != 0)
350 		ar5523_err(ar, "could not read EEPROM offset 0x%02x\n", which);
351 	return error;
352 }
353 
354 static int ar5523_get_capability(struct ar5523 *ar, u32 cap, u32 *val)
355 {
356 	int error;
357 	__be32 cap_be, val_be;
358 
359 	cap_be = cpu_to_be32(cap);
360 	error = ar5523_cmd_read(ar, WDCMSG_TARGET_GET_CAPABILITY, &cap_be,
361 				sizeof(cap_be), &val_be, sizeof(__be32),
362 				AR5523_CMD_FLAG_MAGIC);
363 	if (error != 0) {
364 		ar5523_err(ar, "could not read capability %u\n", cap);
365 		return error;
366 	}
367 	*val = be32_to_cpu(val_be);
368 	return error;
369 }
370 
371 static int ar5523_get_devcap(struct ar5523 *ar)
372 {
373 #define	GETCAP(x) do {				\
374 	error = ar5523_get_capability(ar, x, &cap);		\
375 	if (error != 0)					\
376 		return error;				\
377 	ar5523_info(ar, "Cap: "			\
378 	    "%s=0x%08x\n", #x, cap);	\
379 } while (0)
380 	int error;
381 	u32 cap;
382 
383 	/* collect device capabilities */
384 	GETCAP(CAP_TARGET_VERSION);
385 	GETCAP(CAP_TARGET_REVISION);
386 	GETCAP(CAP_MAC_VERSION);
387 	GETCAP(CAP_MAC_REVISION);
388 	GETCAP(CAP_PHY_REVISION);
389 	GETCAP(CAP_ANALOG_5GHz_REVISION);
390 	GETCAP(CAP_ANALOG_2GHz_REVISION);
391 
392 	GETCAP(CAP_REG_DOMAIN);
393 	GETCAP(CAP_REG_CAP_BITS);
394 	GETCAP(CAP_WIRELESS_MODES);
395 	GETCAP(CAP_CHAN_SPREAD_SUPPORT);
396 	GETCAP(CAP_COMPRESS_SUPPORT);
397 	GETCAP(CAP_BURST_SUPPORT);
398 	GETCAP(CAP_FAST_FRAMES_SUPPORT);
399 	GETCAP(CAP_CHAP_TUNING_SUPPORT);
400 	GETCAP(CAP_TURBOG_SUPPORT);
401 	GETCAP(CAP_TURBO_PRIME_SUPPORT);
402 	GETCAP(CAP_DEVICE_TYPE);
403 	GETCAP(CAP_WME_SUPPORT);
404 	GETCAP(CAP_TOTAL_QUEUES);
405 	GETCAP(CAP_CONNECTION_ID_MAX);
406 
407 	GETCAP(CAP_LOW_5GHZ_CHAN);
408 	GETCAP(CAP_HIGH_5GHZ_CHAN);
409 	GETCAP(CAP_LOW_2GHZ_CHAN);
410 	GETCAP(CAP_HIGH_2GHZ_CHAN);
411 	GETCAP(CAP_TWICE_ANTENNAGAIN_5G);
412 	GETCAP(CAP_TWICE_ANTENNAGAIN_2G);
413 
414 	GETCAP(CAP_CIPHER_AES_CCM);
415 	GETCAP(CAP_CIPHER_TKIP);
416 	GETCAP(CAP_MIC_TKIP);
417 	return 0;
418 }
419 
420 static int ar5523_set_ledsteady(struct ar5523 *ar, int lednum, int ledmode)
421 {
422 	struct ar5523_cmd_ledsteady led;
423 
424 	led.lednum = cpu_to_be32(lednum);
425 	led.ledmode = cpu_to_be32(ledmode);
426 
427 	ar5523_dbg(ar, "set %s led %s (steady)\n",
428 		   (lednum == UATH_LED_LINK) ? "link" : "activity",
429 		   ledmode ? "on" : "off");
430 	return ar5523_cmd_write(ar, WDCMSG_SET_LED_STEADY, &led, sizeof(led),
431 				 0);
432 }
433 
434 static int ar5523_set_rxfilter(struct ar5523 *ar, u32 bits, u32 op)
435 {
436 	struct ar5523_cmd_rx_filter rxfilter;
437 
438 	rxfilter.bits = cpu_to_be32(bits);
439 	rxfilter.op = cpu_to_be32(op);
440 
441 	ar5523_dbg(ar, "setting Rx filter=0x%x flags=0x%x\n", bits, op);
442 	return ar5523_cmd_write(ar, WDCMSG_RX_FILTER, &rxfilter,
443 				 sizeof(rxfilter), 0);
444 }
445 
446 static int ar5523_reset_tx_queues(struct ar5523 *ar)
447 {
448 	__be32 qid = cpu_to_be32(0);
449 
450 	ar5523_dbg(ar, "resetting Tx queue\n");
451 	return ar5523_cmd_write(ar, WDCMSG_RELEASE_TX_QUEUE,
452 				 &qid, sizeof(qid), 0);
453 }
454 
455 static int ar5523_set_chan(struct ar5523 *ar)
456 {
457 	struct ieee80211_conf *conf = &ar->hw->conf;
458 
459 	struct ar5523_cmd_reset reset;
460 
461 	memset(&reset, 0, sizeof(reset));
462 	reset.flags |= cpu_to_be32(UATH_CHAN_2GHZ);
463 	reset.flags |= cpu_to_be32(UATH_CHAN_OFDM);
464 	reset.freq = cpu_to_be32(conf->chandef.chan->center_freq);
465 	reset.maxrdpower = cpu_to_be32(50);	/* XXX */
466 	reset.channelchange = cpu_to_be32(1);
467 	reset.keeprccontent = cpu_to_be32(0);
468 
469 	ar5523_dbg(ar, "set chan flags 0x%x freq %d\n",
470 		   be32_to_cpu(reset.flags),
471 		   conf->chandef.chan->center_freq);
472 	return ar5523_cmd_write(ar, WDCMSG_RESET, &reset, sizeof(reset), 0);
473 }
474 
475 static int ar5523_queue_init(struct ar5523 *ar)
476 {
477 	struct ar5523_cmd_txq_setup qinfo;
478 
479 	ar5523_dbg(ar, "setting up Tx queue\n");
480 	qinfo.qid	     = cpu_to_be32(0);
481 	qinfo.len	     = cpu_to_be32(sizeof(qinfo.attr));
482 	qinfo.attr.priority  = cpu_to_be32(0);	/* XXX */
483 	qinfo.attr.aifs	     = cpu_to_be32(3);
484 	qinfo.attr.logcwmin  = cpu_to_be32(4);
485 	qinfo.attr.logcwmax  = cpu_to_be32(10);
486 	qinfo.attr.bursttime = cpu_to_be32(0);
487 	qinfo.attr.mode	     = cpu_to_be32(0);
488 	qinfo.attr.qflags    = cpu_to_be32(1);	/* XXX? */
489 	return ar5523_cmd_write(ar, WDCMSG_SETUP_TX_QUEUE, &qinfo,
490 				 sizeof(qinfo), 0);
491 }
492 
493 static int ar5523_switch_chan(struct ar5523 *ar)
494 {
495 	int error;
496 
497 	error = ar5523_set_chan(ar);
498 	if (error) {
499 		ar5523_err(ar, "could not set chan, error %d\n", error);
500 		goto out_err;
501 	}
502 
503 	/* reset Tx rings */
504 	error = ar5523_reset_tx_queues(ar);
505 	if (error) {
506 		ar5523_err(ar, "could not reset Tx queues, error %d\n",
507 			   error);
508 		goto out_err;
509 	}
510 	/* set Tx rings WME properties */
511 	error = ar5523_queue_init(ar);
512 	if (error)
513 		ar5523_err(ar, "could not init wme, error %d\n", error);
514 
515 out_err:
516 	return error;
517 }
518 
519 static void ar5523_rx_data_put(struct ar5523 *ar,
520 				struct ar5523_rx_data *data)
521 {
522 	unsigned long flags;
523 	spin_lock_irqsave(&ar->rx_data_list_lock, flags);
524 	list_move(&data->list, &ar->rx_data_free);
525 	spin_unlock_irqrestore(&ar->rx_data_list_lock, flags);
526 }
527 
528 static void ar5523_data_rx_cb(struct urb *urb)
529 {
530 	struct ar5523_rx_data *data = urb->context;
531 	struct ar5523 *ar = data->ar;
532 	struct ar5523_rx_desc *desc;
533 	struct ar5523_chunk *chunk;
534 	struct ieee80211_hw *hw = ar->hw;
535 	struct ieee80211_rx_status *rx_status;
536 	u32 rxlen;
537 	int usblen = urb->actual_length;
538 	int hdrlen, pad;
539 
540 	ar5523_dbg(ar, "%s\n", __func__);
541 	/* sync/async unlink faults aren't errors */
542 	if (urb->status) {
543 		if (urb->status != -ESHUTDOWN)
544 			ar5523_err(ar, "%s: USB err: %d\n", __func__,
545 				   urb->status);
546 		goto skip;
547 	}
548 
549 	if (usblen < AR5523_MIN_RXBUFSZ) {
550 		ar5523_err(ar, "RX: wrong xfer size (usblen=%d)\n", usblen);
551 		goto skip;
552 	}
553 
554 	chunk = (struct ar5523_chunk *) data->skb->data;
555 
556 	if (((chunk->flags & UATH_CFLAGS_FINAL) == 0) ||
557 		chunk->seqnum != 0) {
558 		ar5523_dbg(ar, "RX: No final flag. s: %d f: %02x l: %d\n",
559 			   chunk->seqnum, chunk->flags,
560 			   be16_to_cpu(chunk->length));
561 		goto skip;
562 	}
563 
564 	/* Rx descriptor is located at the end, 32-bit aligned */
565 	desc = (struct ar5523_rx_desc *)
566 		(data->skb->data + usblen - sizeof(struct ar5523_rx_desc));
567 
568 	rxlen = be32_to_cpu(desc->len);
569 	if (rxlen > ar->rxbufsz) {
570 		ar5523_dbg(ar, "RX: Bad descriptor (len=%d)\n",
571 			   be32_to_cpu(desc->len));
572 		goto skip;
573 	}
574 
575 	if (!rxlen) {
576 		ar5523_dbg(ar, "RX: rxlen is 0\n");
577 		goto skip;
578 	}
579 
580 	if (be32_to_cpu(desc->status) != 0) {
581 		ar5523_dbg(ar, "Bad RX status (0x%x len = %d). Skip\n",
582 			   be32_to_cpu(desc->status), be32_to_cpu(desc->len));
583 		goto skip;
584 	}
585 
586 	skb_reserve(data->skb, sizeof(*chunk));
587 	skb_put(data->skb, rxlen - sizeof(struct ar5523_rx_desc));
588 
589 	hdrlen = ieee80211_get_hdrlen_from_skb(data->skb);
590 	if (!IS_ALIGNED(hdrlen, 4)) {
591 		ar5523_dbg(ar, "eek, alignment workaround activated\n");
592 		pad = ALIGN(hdrlen, 4) - hdrlen;
593 		memmove(data->skb->data + pad, data->skb->data, hdrlen);
594 		skb_pull(data->skb, pad);
595 		skb_put(data->skb, pad);
596 	}
597 
598 	rx_status = IEEE80211_SKB_RXCB(data->skb);
599 	memset(rx_status, 0, sizeof(*rx_status));
600 	rx_status->freq = be32_to_cpu(desc->channel);
601 	rx_status->band = hw->conf.chandef.chan->band;
602 	rx_status->signal = -95 + be32_to_cpu(desc->rssi);
603 
604 	ieee80211_rx_irqsafe(hw, data->skb);
605 	data->skb = NULL;
606 
607 skip:
608 	if (data->skb) {
609 		dev_kfree_skb_irq(data->skb);
610 		data->skb = NULL;
611 	}
612 
613 	ar5523_rx_data_put(ar, data);
614 	if (atomic_inc_return(&ar->rx_data_free_cnt) >=
615 	    AR5523_RX_DATA_REFILL_COUNT &&
616 	    test_bit(AR5523_HW_UP, &ar->flags))
617 		queue_work(ar->wq, &ar->rx_refill_work);
618 }
619 
620 static void ar5523_rx_refill_work(struct work_struct *work)
621 {
622 	struct ar5523 *ar = container_of(work, struct ar5523, rx_refill_work);
623 	struct ar5523_rx_data *data;
624 	unsigned long flags;
625 	int error;
626 
627 	ar5523_dbg(ar, "%s\n", __func__);
628 	do {
629 		spin_lock_irqsave(&ar->rx_data_list_lock, flags);
630 
631 		if (!list_empty(&ar->rx_data_free))
632 			data = (struct ar5523_rx_data *) ar->rx_data_free.next;
633 		else
634 			data = NULL;
635 		spin_unlock_irqrestore(&ar->rx_data_list_lock, flags);
636 
637 		if (!data)
638 			goto done;
639 
640 		data->skb = alloc_skb(ar->rxbufsz, GFP_KERNEL);
641 		if (!data->skb) {
642 			ar5523_err(ar, "could not allocate rx skbuff\n");
643 			return;
644 		}
645 
646 		usb_fill_bulk_urb(data->urb, ar->dev,
647 				  ar5523_data_rx_pipe(ar->dev), data->skb->data,
648 				  ar->rxbufsz, ar5523_data_rx_cb, data);
649 
650 		spin_lock_irqsave(&ar->rx_data_list_lock, flags);
651 		list_move(&data->list, &ar->rx_data_used);
652 		spin_unlock_irqrestore(&ar->rx_data_list_lock, flags);
653 		atomic_dec(&ar->rx_data_free_cnt);
654 
655 		error = usb_submit_urb(data->urb, GFP_KERNEL);
656 		if (error) {
657 			kfree_skb(data->skb);
658 			if (error != -ENODEV)
659 				ar5523_err(ar, "Err sending rx data urb %d\n",
660 					   error);
661 			ar5523_rx_data_put(ar, data);
662 			atomic_inc(&ar->rx_data_free_cnt);
663 			return;
664 		}
665 
666 	} while (true);
667 done:
668 	return;
669 }
670 
671 static void ar5523_cancel_rx_bufs(struct ar5523 *ar)
672 {
673 	struct ar5523_rx_data *data;
674 	unsigned long flags;
675 
676 	do {
677 		spin_lock_irqsave(&ar->rx_data_list_lock, flags);
678 		if (!list_empty(&ar->rx_data_used))
679 			data = (struct ar5523_rx_data *) ar->rx_data_used.next;
680 		else
681 			data = NULL;
682 		spin_unlock_irqrestore(&ar->rx_data_list_lock, flags);
683 
684 		if (!data)
685 			break;
686 
687 		usb_kill_urb(data->urb);
688 		list_move(&data->list, &ar->rx_data_free);
689 		atomic_inc(&ar->rx_data_free_cnt);
690 	} while (data);
691 }
692 
693 static void ar5523_free_rx_bufs(struct ar5523 *ar)
694 {
695 	struct ar5523_rx_data *data;
696 
697 	ar5523_cancel_rx_bufs(ar);
698 	while (!list_empty(&ar->rx_data_free)) {
699 		data = (struct ar5523_rx_data *) ar->rx_data_free.next;
700 		list_del(&data->list);
701 		usb_free_urb(data->urb);
702 	}
703 }
704 
705 static int ar5523_alloc_rx_bufs(struct ar5523 *ar)
706 {
707 	int i;
708 
709 	for (i = 0; i < AR5523_RX_DATA_COUNT; i++) {
710 		struct ar5523_rx_data *data = &ar->rx_data[i];
711 
712 		data->ar = ar;
713 		data->urb = usb_alloc_urb(0, GFP_KERNEL);
714 		if (!data->urb)
715 			goto err;
716 		list_add_tail(&data->list, &ar->rx_data_free);
717 		atomic_inc(&ar->rx_data_free_cnt);
718 	}
719 	return 0;
720 
721 err:
722 	ar5523_free_rx_bufs(ar);
723 	return -ENOMEM;
724 }
725 
726 static void ar5523_data_tx_pkt_put(struct ar5523 *ar)
727 {
728 	atomic_dec(&ar->tx_nr_total);
729 	if (!atomic_dec_return(&ar->tx_nr_pending)) {
730 		del_timer(&ar->tx_wd_timer);
731 		wake_up(&ar->tx_flush_waitq);
732 	}
733 
734 	if (atomic_read(&ar->tx_nr_total) < AR5523_TX_DATA_RESTART_COUNT) {
735 		ar5523_dbg(ar, "restart tx queue\n");
736 		ieee80211_wake_queues(ar->hw);
737 	}
738 }
739 
740 static void ar5523_data_tx_cb(struct urb *urb)
741 {
742 	struct sk_buff *skb = urb->context;
743 	struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
744 	struct ar5523_tx_data *data = (struct ar5523_tx_data *)
745 				       txi->driver_data;
746 	struct ar5523 *ar = data->ar;
747 	unsigned long flags;
748 
749 	ar5523_dbg(ar, "data tx urb completed: %d\n", urb->status);
750 
751 	spin_lock_irqsave(&ar->tx_data_list_lock, flags);
752 	list_del(&data->list);
753 	spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
754 
755 	if (urb->status) {
756 		ar5523_dbg(ar, "%s: urb status: %d\n", __func__, urb->status);
757 		ar5523_data_tx_pkt_put(ar);
758 		ieee80211_free_txskb(ar->hw, skb);
759 	} else {
760 		skb_pull(skb, sizeof(struct ar5523_tx_desc) + sizeof(__be32));
761 		ieee80211_tx_status_irqsafe(ar->hw, skb);
762 	}
763 	usb_free_urb(urb);
764 }
765 
766 static void ar5523_tx(struct ieee80211_hw *hw,
767 		       struct ieee80211_tx_control *control,
768 		       struct sk_buff *skb)
769 {
770 	struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
771 	struct ar5523_tx_data *data = (struct ar5523_tx_data *)
772 					txi->driver_data;
773 	struct ar5523 *ar = hw->priv;
774 	unsigned long flags;
775 
776 	ar5523_dbg(ar, "tx called\n");
777 	if (atomic_inc_return(&ar->tx_nr_total) >= AR5523_TX_DATA_COUNT) {
778 		ar5523_dbg(ar, "tx queue full\n");
779 		ar5523_dbg(ar, "stop queues (tot %d pend %d)\n",
780 			   atomic_read(&ar->tx_nr_total),
781 			   atomic_read(&ar->tx_nr_pending));
782 		ieee80211_stop_queues(hw);
783 	}
784 
785 	spin_lock_irqsave(&ar->tx_data_list_lock, flags);
786 	list_add_tail(&data->list, &ar->tx_queue_pending);
787 	spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
788 
789 	ieee80211_queue_work(ar->hw, &ar->tx_work);
790 }
791 
792 static void ar5523_tx_work_locked(struct ar5523 *ar)
793 {
794 	struct ar5523_tx_data *data;
795 	struct ar5523_tx_desc *desc;
796 	struct ar5523_chunk *chunk;
797 	struct ieee80211_tx_info *txi;
798 	struct urb *urb;
799 	struct sk_buff *skb;
800 	int error = 0, paylen;
801 	u32 txqid;
802 	unsigned long flags;
803 
804 	BUILD_BUG_ON(sizeof(struct ar5523_tx_data) >
805 		     IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
806 
807 	ar5523_dbg(ar, "%s\n", __func__);
808 	do {
809 		spin_lock_irqsave(&ar->tx_data_list_lock, flags);
810 		if (!list_empty(&ar->tx_queue_pending)) {
811 			data = (struct ar5523_tx_data *)
812 				ar->tx_queue_pending.next;
813 			list_del(&data->list);
814 		} else
815 			data = NULL;
816 		spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
817 
818 		if (!data)
819 			break;
820 
821 		txi = container_of((void *)data, struct ieee80211_tx_info,
822 				   driver_data);
823 		txqid = 0;
824 
825 		skb = container_of((void *)txi, struct sk_buff, cb);
826 		paylen = skb->len;
827 
828 		urb = usb_alloc_urb(0, GFP_KERNEL);
829 		if (!urb) {
830 			ieee80211_free_txskb(ar->hw, skb);
831 			continue;
832 		}
833 
834 		data->ar = ar;
835 		data->urb = urb;
836 
837 		desc = skb_push(skb, sizeof(*desc));
838 		chunk = skb_push(skb, sizeof(*chunk));
839 
840 		chunk->seqnum = 0;
841 		chunk->flags = UATH_CFLAGS_FINAL;
842 		chunk->length = cpu_to_be16(skb->len);
843 
844 		desc->msglen = cpu_to_be32(skb->len);
845 		desc->msgid  = AR5523_DATA_ID;
846 		desc->buflen = cpu_to_be32(paylen);
847 		desc->type   = cpu_to_be32(WDCMSG_SEND);
848 		desc->flags  = cpu_to_be32(UATH_TX_NOTIFY);
849 
850 		if (test_bit(AR5523_CONNECTED, &ar->flags))
851 			desc->connid = cpu_to_be32(AR5523_ID_BSS);
852 		else
853 			desc->connid = cpu_to_be32(AR5523_ID_BROADCAST);
854 
855 		if (txi->flags & IEEE80211_TX_CTL_USE_MINRATE)
856 			txqid |= UATH_TXQID_MINRATE;
857 
858 		desc->txqid = cpu_to_be32(txqid);
859 
860 		urb->transfer_flags = URB_ZERO_PACKET;
861 		usb_fill_bulk_urb(urb, ar->dev, ar5523_data_tx_pipe(ar->dev),
862 				  skb->data, skb->len, ar5523_data_tx_cb, skb);
863 
864 		spin_lock_irqsave(&ar->tx_data_list_lock, flags);
865 		list_add_tail(&data->list, &ar->tx_queue_submitted);
866 		spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
867 		mod_timer(&ar->tx_wd_timer, jiffies + AR5523_TX_WD_TIMEOUT);
868 		atomic_inc(&ar->tx_nr_pending);
869 
870 		ar5523_dbg(ar, "TX Frame (%d pending)\n",
871 			   atomic_read(&ar->tx_nr_pending));
872 		error = usb_submit_urb(urb, GFP_KERNEL);
873 		if (error) {
874 			ar5523_err(ar, "error %d when submitting tx urb\n",
875 				   error);
876 			spin_lock_irqsave(&ar->tx_data_list_lock, flags);
877 			list_del(&data->list);
878 			spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
879 			atomic_dec(&ar->tx_nr_pending);
880 			ar5523_data_tx_pkt_put(ar);
881 			usb_free_urb(urb);
882 			ieee80211_free_txskb(ar->hw, skb);
883 		}
884 	} while (true);
885 }
886 
887 static void ar5523_tx_work(struct work_struct *work)
888 {
889 	struct ar5523 *ar = container_of(work, struct ar5523, tx_work);
890 
891 	ar5523_dbg(ar, "%s\n", __func__);
892 	mutex_lock(&ar->mutex);
893 	ar5523_tx_work_locked(ar);
894 	mutex_unlock(&ar->mutex);
895 }
896 
897 static void ar5523_tx_wd_timer(struct timer_list *t)
898 {
899 	struct ar5523 *ar = from_timer(ar, t, tx_wd_timer);
900 
901 	ar5523_dbg(ar, "TX watchdog timer triggered\n");
902 	ieee80211_queue_work(ar->hw, &ar->tx_wd_work);
903 }
904 
905 static void ar5523_tx_wd_work(struct work_struct *work)
906 {
907 	struct ar5523 *ar = container_of(work, struct ar5523, tx_wd_work);
908 
909 	/* Occasionally the TX queues stop responding. The only way to
910 	 * recover seems to be to reset the dongle.
911 	 */
912 
913 	mutex_lock(&ar->mutex);
914 	ar5523_err(ar, "TX queue stuck (tot %d pend %d)\n",
915 		   atomic_read(&ar->tx_nr_total),
916 		   atomic_read(&ar->tx_nr_pending));
917 
918 	ar5523_err(ar, "Will restart dongle.\n");
919 	ar5523_cmd_write(ar, WDCMSG_TARGET_RESET, NULL, 0, 0);
920 	mutex_unlock(&ar->mutex);
921 }
922 
923 static void ar5523_flush_tx(struct ar5523 *ar)
924 {
925 	ar5523_tx_work_locked(ar);
926 
927 	/* Don't waste time trying to flush if USB is disconnected */
928 	if (test_bit(AR5523_USB_DISCONNECTED, &ar->flags))
929 		return;
930 	if (!wait_event_timeout(ar->tx_flush_waitq,
931 	    !atomic_read(&ar->tx_nr_pending), AR5523_FLUSH_TIMEOUT))
932 		ar5523_err(ar, "flush timeout (tot %d pend %d)\n",
933 			   atomic_read(&ar->tx_nr_total),
934 			   atomic_read(&ar->tx_nr_pending));
935 }
936 
937 static void ar5523_free_tx_cmd(struct ar5523 *ar)
938 {
939 	struct ar5523_tx_cmd *cmd = &ar->tx_cmd;
940 
941 	usb_free_coherent(ar->dev, AR5523_MAX_RXCMDSZ, cmd->buf_tx,
942 			  cmd->urb_tx->transfer_dma);
943 	usb_free_urb(cmd->urb_tx);
944 }
945 
946 static int ar5523_alloc_tx_cmd(struct ar5523 *ar)
947 {
948 	struct ar5523_tx_cmd *cmd = &ar->tx_cmd;
949 
950 	cmd->ar = ar;
951 	init_completion(&cmd->done);
952 
953 	cmd->urb_tx = usb_alloc_urb(0, GFP_KERNEL);
954 	if (!cmd->urb_tx)
955 		return -ENOMEM;
956 	cmd->buf_tx = usb_alloc_coherent(ar->dev, AR5523_MAX_TXCMDSZ,
957 					 GFP_KERNEL,
958 					 &cmd->urb_tx->transfer_dma);
959 	if (!cmd->buf_tx) {
960 		usb_free_urb(cmd->urb_tx);
961 		return -ENOMEM;
962 	}
963 	return 0;
964 }
965 
966 /*
967  * This function is called periodically (every second) when associated to
968  * query device statistics.
969  */
970 static void ar5523_stat_work(struct work_struct *work)
971 {
972 	struct ar5523 *ar = container_of(work, struct ar5523, stat_work.work);
973 	int error;
974 
975 	ar5523_dbg(ar, "%s\n", __func__);
976 	mutex_lock(&ar->mutex);
977 
978 	/*
979 	 * Send request for statistics asynchronously once a second. This
980 	 * seems to be important. Throughput is a lot better if this is done.
981 	 */
982 	error = ar5523_cmd_write(ar, WDCMSG_TARGET_GET_STATS, NULL, 0, 0);
983 	if (error)
984 		ar5523_err(ar, "could not query stats, error %d\n", error);
985 	mutex_unlock(&ar->mutex);
986 	ieee80211_queue_delayed_work(ar->hw, &ar->stat_work, HZ);
987 }
988 
989 /*
990  * Interface routines to the mac80211 stack.
991  */
992 static int ar5523_start(struct ieee80211_hw *hw)
993 {
994 	struct ar5523 *ar = hw->priv;
995 	int error;
996 	__be32 val;
997 
998 	ar5523_dbg(ar, "start called\n");
999 
1000 	mutex_lock(&ar->mutex);
1001 	val = cpu_to_be32(0);
1002 	ar5523_cmd_write(ar, WDCMSG_BIND, &val, sizeof(val), 0);
1003 
1004 	/* set MAC address */
1005 	ar5523_config_multi(ar, CFG_MAC_ADDR, &ar->hw->wiphy->perm_addr,
1006 			    ETH_ALEN);
1007 
1008 	/* XXX honor net80211 state */
1009 	ar5523_config(ar, CFG_RATE_CONTROL_ENABLE, 0x00000001);
1010 	ar5523_config(ar, CFG_DIVERSITY_CTL, 0x00000001);
1011 	ar5523_config(ar, CFG_ABOLT, 0x0000003f);
1012 	ar5523_config(ar, CFG_WME_ENABLED, 0x00000000);
1013 
1014 	ar5523_config(ar, CFG_SERVICE_TYPE, 1);
1015 	ar5523_config(ar, CFG_TP_SCALE, 0x00000000);
1016 	ar5523_config(ar, CFG_TPC_HALF_DBM5, 0x0000003c);
1017 	ar5523_config(ar, CFG_TPC_HALF_DBM2, 0x0000003c);
1018 	ar5523_config(ar, CFG_OVERRD_TX_POWER, 0x00000000);
1019 	ar5523_config(ar, CFG_GMODE_PROTECTION, 0x00000000);
1020 	ar5523_config(ar, CFG_GMODE_PROTECT_RATE_INDEX, 0x00000003);
1021 	ar5523_config(ar, CFG_PROTECTION_TYPE, 0x00000000);
1022 	ar5523_config(ar, CFG_MODE_CTS, 0x00000002);
1023 
1024 	error = ar5523_cmd_read(ar, WDCMSG_TARGET_START, NULL, 0,
1025 	    &val, sizeof(val), AR5523_CMD_FLAG_MAGIC);
1026 	if (error) {
1027 		ar5523_dbg(ar, "could not start target, error %d\n", error);
1028 		goto err;
1029 	}
1030 	ar5523_dbg(ar, "WDCMSG_TARGET_START returns handle: 0x%x\n",
1031 		   be32_to_cpu(val));
1032 
1033 	ar5523_switch_chan(ar);
1034 
1035 	val = cpu_to_be32(TARGET_DEVICE_AWAKE);
1036 	ar5523_cmd_write(ar, WDCMSG_SET_PWR_MODE, &val, sizeof(val), 0);
1037 	/* XXX? check */
1038 	ar5523_cmd_write(ar, WDCMSG_RESET_KEY_CACHE, NULL, 0, 0);
1039 
1040 	set_bit(AR5523_HW_UP, &ar->flags);
1041 	queue_work(ar->wq, &ar->rx_refill_work);
1042 
1043 	/* enable Rx */
1044 	ar5523_set_rxfilter(ar, 0, UATH_FILTER_OP_INIT);
1045 	ar5523_set_rxfilter(ar,
1046 			    UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST |
1047 			    UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON,
1048 			    UATH_FILTER_OP_SET);
1049 
1050 	ar5523_set_ledsteady(ar, UATH_LED_ACTIVITY, UATH_LED_ON);
1051 	ar5523_dbg(ar, "start OK\n");
1052 
1053 err:
1054 	mutex_unlock(&ar->mutex);
1055 	return error;
1056 }
1057 
1058 static void ar5523_stop(struct ieee80211_hw *hw)
1059 {
1060 	struct ar5523 *ar = hw->priv;
1061 
1062 	ar5523_dbg(ar, "stop called\n");
1063 
1064 	cancel_delayed_work_sync(&ar->stat_work);
1065 	mutex_lock(&ar->mutex);
1066 	clear_bit(AR5523_HW_UP, &ar->flags);
1067 
1068 	ar5523_set_ledsteady(ar, UATH_LED_LINK, UATH_LED_OFF);
1069 	ar5523_set_ledsteady(ar, UATH_LED_ACTIVITY, UATH_LED_OFF);
1070 
1071 	ar5523_cmd_write(ar, WDCMSG_TARGET_STOP, NULL, 0, 0);
1072 
1073 	del_timer_sync(&ar->tx_wd_timer);
1074 	cancel_work_sync(&ar->tx_wd_work);
1075 	cancel_work_sync(&ar->rx_refill_work);
1076 	ar5523_cancel_rx_bufs(ar);
1077 	mutex_unlock(&ar->mutex);
1078 }
1079 
1080 static int ar5523_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1081 {
1082 	struct ar5523 *ar = hw->priv;
1083 	int ret;
1084 
1085 	ar5523_dbg(ar, "set_rts_threshold called\n");
1086 	mutex_lock(&ar->mutex);
1087 
1088 	ret = ar5523_config(ar, CFG_USER_RTS_THRESHOLD, value);
1089 
1090 	mutex_unlock(&ar->mutex);
1091 	return ret;
1092 }
1093 
1094 static void ar5523_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1095 			 u32 queues, bool drop)
1096 {
1097 	struct ar5523 *ar = hw->priv;
1098 
1099 	ar5523_dbg(ar, "flush called\n");
1100 	ar5523_flush_tx(ar);
1101 }
1102 
1103 static int ar5523_add_interface(struct ieee80211_hw *hw,
1104 				struct ieee80211_vif *vif)
1105 {
1106 	struct ar5523 *ar = hw->priv;
1107 
1108 	ar5523_dbg(ar, "add interface called\n");
1109 
1110 	if (ar->vif) {
1111 		ar5523_dbg(ar, "invalid add_interface\n");
1112 		return -EOPNOTSUPP;
1113 	}
1114 
1115 	switch (vif->type) {
1116 	case NL80211_IFTYPE_STATION:
1117 		ar->vif = vif;
1118 		break;
1119 	default:
1120 		return -EOPNOTSUPP;
1121 	}
1122 	return 0;
1123 }
1124 
1125 static void ar5523_remove_interface(struct ieee80211_hw *hw,
1126 				    struct ieee80211_vif *vif)
1127 {
1128 	struct ar5523 *ar = hw->priv;
1129 
1130 	ar5523_dbg(ar, "remove interface called\n");
1131 	ar->vif = NULL;
1132 }
1133 
1134 static int ar5523_hwconfig(struct ieee80211_hw *hw, u32 changed)
1135 {
1136 	struct ar5523 *ar = hw->priv;
1137 
1138 	ar5523_dbg(ar, "config called\n");
1139 	mutex_lock(&ar->mutex);
1140 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1141 		ar5523_dbg(ar, "Do channel switch\n");
1142 		ar5523_flush_tx(ar);
1143 		ar5523_switch_chan(ar);
1144 	}
1145 	mutex_unlock(&ar->mutex);
1146 	return 0;
1147 }
1148 
1149 static int ar5523_get_wlan_mode(struct ar5523 *ar,
1150 				struct ieee80211_bss_conf *bss_conf)
1151 {
1152 	struct ieee80211_supported_band *band;
1153 	int bit;
1154 	struct ieee80211_sta *sta;
1155 	u32 sta_rate_set;
1156 
1157 	band = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1158 	sta = ieee80211_find_sta(ar->vif, bss_conf->bssid);
1159 	if (!sta) {
1160 		ar5523_info(ar, "STA not found!\n");
1161 		return WLAN_MODE_11b;
1162 	}
1163 	sta_rate_set = sta->deflink.supp_rates[ar->hw->conf.chandef.chan->band];
1164 
1165 	for (bit = 0; bit < band->n_bitrates; bit++) {
1166 		if (sta_rate_set & 1) {
1167 			int rate = band->bitrates[bit].bitrate;
1168 			switch (rate) {
1169 			case 60:
1170 			case 90:
1171 			case 120:
1172 			case 180:
1173 			case 240:
1174 			case 360:
1175 			case 480:
1176 			case 540:
1177 				return WLAN_MODE_11g;
1178 			}
1179 		}
1180 		sta_rate_set >>= 1;
1181 	}
1182 	return WLAN_MODE_11b;
1183 }
1184 
1185 static void ar5523_create_rateset(struct ar5523 *ar,
1186 				  struct ieee80211_bss_conf *bss_conf,
1187 				  struct ar5523_cmd_rateset *rs,
1188 				  bool basic)
1189 {
1190 	struct ieee80211_supported_band *band;
1191 	struct ieee80211_sta *sta;
1192 	int bit, i = 0;
1193 	u32 sta_rate_set, basic_rate_set;
1194 
1195 	sta = ieee80211_find_sta(ar->vif, bss_conf->bssid);
1196 	basic_rate_set = bss_conf->basic_rates;
1197 	if (!sta) {
1198 		ar5523_info(ar, "STA not found. Cannot set rates\n");
1199 		sta_rate_set = bss_conf->basic_rates;
1200 	} else
1201 		sta_rate_set = sta->deflink.supp_rates[ar->hw->conf.chandef.chan->band];
1202 
1203 	ar5523_dbg(ar, "sta rate_set = %08x\n", sta_rate_set);
1204 
1205 	band = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1206 	for (bit = 0; bit < band->n_bitrates; bit++) {
1207 		BUG_ON(i >= AR5523_MAX_NRATES);
1208 		ar5523_dbg(ar, "Considering rate %d : %d\n",
1209 			   band->bitrates[bit].hw_value, sta_rate_set & 1);
1210 		if (sta_rate_set & 1) {
1211 			rs->set[i] = band->bitrates[bit].hw_value;
1212 			if (basic_rate_set & 1 && basic)
1213 				rs->set[i] |= 0x80;
1214 			i++;
1215 		}
1216 		sta_rate_set >>= 1;
1217 		basic_rate_set >>= 1;
1218 	}
1219 
1220 	rs->length = i;
1221 }
1222 
1223 static int ar5523_set_basic_rates(struct ar5523 *ar,
1224 				  struct ieee80211_bss_conf *bss)
1225 {
1226 	struct ar5523_cmd_rates rates;
1227 
1228 	memset(&rates, 0, sizeof(rates));
1229 	rates.connid = cpu_to_be32(2);		/* XXX */
1230 	rates.size   = cpu_to_be32(sizeof(struct ar5523_cmd_rateset));
1231 	ar5523_create_rateset(ar, bss, &rates.rateset, true);
1232 
1233 	return ar5523_cmd_write(ar, WDCMSG_SET_BASIC_RATE, &rates,
1234 				sizeof(rates), 0);
1235 }
1236 
1237 static int ar5523_create_connection(struct ar5523 *ar,
1238 				    struct ieee80211_vif *vif,
1239 				    struct ieee80211_bss_conf *bss)
1240 {
1241 	struct ar5523_cmd_create_connection create;
1242 	int wlan_mode;
1243 
1244 	memset(&create, 0, sizeof(create));
1245 	create.connid = cpu_to_be32(2);
1246 	create.bssid = cpu_to_be32(0);
1247 	/* XXX packed or not?  */
1248 	create.size = cpu_to_be32(sizeof(struct ar5523_cmd_rateset));
1249 
1250 	ar5523_create_rateset(ar, bss, &create.connattr.rateset, false);
1251 
1252 	wlan_mode = ar5523_get_wlan_mode(ar, bss);
1253 	create.connattr.wlanmode = cpu_to_be32(wlan_mode);
1254 
1255 	return ar5523_cmd_write(ar, WDCMSG_CREATE_CONNECTION, &create,
1256 				sizeof(create), 0);
1257 }
1258 
1259 static int ar5523_write_associd(struct ar5523 *ar,
1260 				struct ieee80211_bss_conf *bss)
1261 {
1262 	struct ar5523_cmd_set_associd associd;
1263 
1264 	memset(&associd, 0, sizeof(associd));
1265 	associd.defaultrateix = cpu_to_be32(0);	/* XXX */
1266 	associd.associd = cpu_to_be32(bss->aid);
1267 	associd.timoffset = cpu_to_be32(0x3b);	/* XXX */
1268 	memcpy(associd.bssid, bss->bssid, ETH_ALEN);
1269 	return ar5523_cmd_write(ar, WDCMSG_WRITE_ASSOCID, &associd,
1270 				sizeof(associd), 0);
1271 }
1272 
1273 static void ar5523_bss_info_changed(struct ieee80211_hw *hw,
1274 				    struct ieee80211_vif *vif,
1275 				    struct ieee80211_bss_conf *bss,
1276 				    u32 changed)
1277 {
1278 	struct ar5523 *ar = hw->priv;
1279 	int error;
1280 
1281 	ar5523_dbg(ar, "bss_info_changed called\n");
1282 	mutex_lock(&ar->mutex);
1283 
1284 	if (!(changed & BSS_CHANGED_ASSOC))
1285 		goto out_unlock;
1286 
1287 	if (bss->assoc) {
1288 		error = ar5523_create_connection(ar, vif, bss);
1289 		if (error) {
1290 			ar5523_err(ar, "could not create connection\n");
1291 			goto out_unlock;
1292 		}
1293 
1294 		error = ar5523_set_basic_rates(ar, bss);
1295 		if (error) {
1296 			ar5523_err(ar, "could not set negotiated rate set\n");
1297 			goto out_unlock;
1298 		}
1299 
1300 		error = ar5523_write_associd(ar, bss);
1301 		if (error) {
1302 			ar5523_err(ar, "could not set association\n");
1303 			goto out_unlock;
1304 		}
1305 
1306 		/* turn link LED on */
1307 		ar5523_set_ledsteady(ar, UATH_LED_LINK, UATH_LED_ON);
1308 		set_bit(AR5523_CONNECTED, &ar->flags);
1309 		ieee80211_queue_delayed_work(hw, &ar->stat_work, HZ);
1310 
1311 	} else {
1312 		cancel_delayed_work(&ar->stat_work);
1313 		clear_bit(AR5523_CONNECTED, &ar->flags);
1314 		ar5523_set_ledsteady(ar, UATH_LED_LINK, UATH_LED_OFF);
1315 	}
1316 
1317 out_unlock:
1318 	mutex_unlock(&ar->mutex);
1319 
1320 }
1321 
1322 #define AR5523_SUPPORTED_FILTERS (FIF_ALLMULTI | \
1323 				  FIF_FCSFAIL | \
1324 				  FIF_OTHER_BSS)
1325 
1326 static void ar5523_configure_filter(struct ieee80211_hw *hw,
1327 				    unsigned int changed_flags,
1328 				    unsigned int *total_flags,
1329 				    u64 multicast)
1330 {
1331 	struct ar5523 *ar = hw->priv;
1332 	u32 filter = 0;
1333 
1334 	ar5523_dbg(ar, "configure_filter called\n");
1335 	mutex_lock(&ar->mutex);
1336 	ar5523_flush_tx(ar);
1337 
1338 	*total_flags &= AR5523_SUPPORTED_FILTERS;
1339 
1340 	/* The filters seems strange. UATH_FILTER_RX_BCAST and
1341 	 * UATH_FILTER_RX_MCAST does not result in those frames being RXed.
1342 	 * The only way I have found to get [mb]cast frames seems to be
1343 	 * to set UATH_FILTER_RX_PROM. */
1344 	filter |= UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST |
1345 		  UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON |
1346 		  UATH_FILTER_RX_PROM;
1347 
1348 	ar5523_set_rxfilter(ar, 0, UATH_FILTER_OP_INIT);
1349 	ar5523_set_rxfilter(ar, filter, UATH_FILTER_OP_SET);
1350 
1351 	mutex_unlock(&ar->mutex);
1352 }
1353 
1354 static const struct ieee80211_ops ar5523_ops = {
1355 	.start			= ar5523_start,
1356 	.stop			= ar5523_stop,
1357 	.tx			= ar5523_tx,
1358 	.set_rts_threshold	= ar5523_set_rts_threshold,
1359 	.add_interface		= ar5523_add_interface,
1360 	.remove_interface	= ar5523_remove_interface,
1361 	.config			= ar5523_hwconfig,
1362 	.bss_info_changed	= ar5523_bss_info_changed,
1363 	.configure_filter	= ar5523_configure_filter,
1364 	.flush			= ar5523_flush,
1365 };
1366 
1367 static int ar5523_host_available(struct ar5523 *ar)
1368 {
1369 	struct ar5523_cmd_host_available setup;
1370 
1371 	/* inform target the host is available */
1372 	setup.sw_ver_major = cpu_to_be32(ATH_SW_VER_MAJOR);
1373 	setup.sw_ver_minor = cpu_to_be32(ATH_SW_VER_MINOR);
1374 	setup.sw_ver_patch = cpu_to_be32(ATH_SW_VER_PATCH);
1375 	setup.sw_ver_build = cpu_to_be32(ATH_SW_VER_BUILD);
1376 	return ar5523_cmd_read(ar, WDCMSG_HOST_AVAILABLE,
1377 			       &setup, sizeof(setup), NULL, 0, 0);
1378 }
1379 
1380 static int ar5523_get_devstatus(struct ar5523 *ar)
1381 {
1382 	u8 macaddr[ETH_ALEN];
1383 	int error;
1384 
1385 	/* retrieve MAC address */
1386 	error = ar5523_get_status(ar, ST_MAC_ADDR, macaddr, ETH_ALEN);
1387 	if (error) {
1388 		ar5523_err(ar, "could not read MAC address\n");
1389 		return error;
1390 	}
1391 
1392 	SET_IEEE80211_PERM_ADDR(ar->hw, macaddr);
1393 
1394 	error = ar5523_get_status(ar, ST_SERIAL_NUMBER,
1395 	    &ar->serial[0], sizeof(ar->serial));
1396 	if (error) {
1397 		ar5523_err(ar, "could not read device serial number\n");
1398 		return error;
1399 	}
1400 	return 0;
1401 }
1402 
1403 #define AR5523_SANE_RXBUFSZ 2000
1404 
1405 static int ar5523_get_max_rxsz(struct ar5523 *ar)
1406 {
1407 	int error;
1408 	__be32 rxsize;
1409 
1410 	/* Get max rx size */
1411 	error = ar5523_get_status(ar, ST_WDC_TRANSPORT_CHUNK_SIZE, &rxsize,
1412 				  sizeof(rxsize));
1413 	if (error != 0) {
1414 		ar5523_err(ar, "could not read max RX size\n");
1415 		return error;
1416 	}
1417 
1418 	ar->rxbufsz = be32_to_cpu(rxsize);
1419 
1420 	if (!ar->rxbufsz || ar->rxbufsz > AR5523_SANE_RXBUFSZ) {
1421 		ar5523_err(ar, "Bad rxbufsz from device. Using %d instead\n",
1422 			   AR5523_SANE_RXBUFSZ);
1423 		ar->rxbufsz = AR5523_SANE_RXBUFSZ;
1424 	}
1425 
1426 	ar5523_dbg(ar, "Max RX buf size: %d\n", ar->rxbufsz);
1427 	return 0;
1428 }
1429 
1430 /*
1431  * This is copied from rtl818x, but we should probably move this
1432  * to common code as in OpenBSD.
1433  */
1434 static const struct ieee80211_rate ar5523_rates[] = {
1435 	{ .bitrate = 10, .hw_value = 2, },
1436 	{ .bitrate = 20, .hw_value = 4 },
1437 	{ .bitrate = 55, .hw_value = 11, },
1438 	{ .bitrate = 110, .hw_value = 22, },
1439 	{ .bitrate = 60, .hw_value = 12, },
1440 	{ .bitrate = 90, .hw_value = 18, },
1441 	{ .bitrate = 120, .hw_value = 24, },
1442 	{ .bitrate = 180, .hw_value = 36, },
1443 	{ .bitrate = 240, .hw_value = 48, },
1444 	{ .bitrate = 360, .hw_value = 72, },
1445 	{ .bitrate = 480, .hw_value = 96, },
1446 	{ .bitrate = 540, .hw_value = 108, },
1447 };
1448 
1449 static const struct ieee80211_channel ar5523_channels[] = {
1450 	{ .center_freq = 2412 },
1451 	{ .center_freq = 2417 },
1452 	{ .center_freq = 2422 },
1453 	{ .center_freq = 2427 },
1454 	{ .center_freq = 2432 },
1455 	{ .center_freq = 2437 },
1456 	{ .center_freq = 2442 },
1457 	{ .center_freq = 2447 },
1458 	{ .center_freq = 2452 },
1459 	{ .center_freq = 2457 },
1460 	{ .center_freq = 2462 },
1461 	{ .center_freq = 2467 },
1462 	{ .center_freq = 2472 },
1463 	{ .center_freq = 2484 },
1464 };
1465 
1466 static int ar5523_init_modes(struct ar5523 *ar)
1467 {
1468 	BUILD_BUG_ON(sizeof(ar->channels) != sizeof(ar5523_channels));
1469 	BUILD_BUG_ON(sizeof(ar->rates) != sizeof(ar5523_rates));
1470 
1471 	memcpy(ar->channels, ar5523_channels, sizeof(ar5523_channels));
1472 	memcpy(ar->rates, ar5523_rates, sizeof(ar5523_rates));
1473 
1474 	ar->band.band = NL80211_BAND_2GHZ;
1475 	ar->band.channels = ar->channels;
1476 	ar->band.n_channels = ARRAY_SIZE(ar5523_channels);
1477 	ar->band.bitrates = ar->rates;
1478 	ar->band.n_bitrates = ARRAY_SIZE(ar5523_rates);
1479 	ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = &ar->band;
1480 	return 0;
1481 }
1482 
1483 /*
1484  * Load the MIPS R4000 microcode into the device.  Once the image is loaded,
1485  * the device will detach itself from the bus and reattach later with a new
1486  * product Id (a la ezusb).
1487  */
1488 static int ar5523_load_firmware(struct usb_device *dev)
1489 {
1490 	struct ar5523_fwblock *txblock, *rxblock;
1491 	const struct firmware *fw;
1492 	void *fwbuf;
1493 	int len, offset;
1494 	int foolen; /* XXX(hch): handle short transfers */
1495 	int error = -ENXIO;
1496 
1497 	if (request_firmware(&fw, AR5523_FIRMWARE_FILE, &dev->dev)) {
1498 		dev_err(&dev->dev, "no firmware found: %s\n",
1499 			AR5523_FIRMWARE_FILE);
1500 		return -ENOENT;
1501 	}
1502 
1503 	txblock = kzalloc(sizeof(*txblock), GFP_KERNEL);
1504 	if (!txblock)
1505 		goto out;
1506 
1507 	rxblock = kmalloc(sizeof(*rxblock), GFP_KERNEL);
1508 	if (!rxblock)
1509 		goto out_free_txblock;
1510 
1511 	fwbuf = kmalloc(AR5523_MAX_FWBLOCK_SIZE, GFP_KERNEL);
1512 	if (!fwbuf)
1513 		goto out_free_rxblock;
1514 
1515 	txblock->flags = cpu_to_be32(AR5523_WRITE_BLOCK);
1516 	txblock->total = cpu_to_be32(fw->size);
1517 
1518 	offset = 0;
1519 	len = fw->size;
1520 	while (len > 0) {
1521 		int mlen = min(len, AR5523_MAX_FWBLOCK_SIZE);
1522 
1523 		txblock->remain = cpu_to_be32(len - mlen);
1524 		txblock->len = cpu_to_be32(mlen);
1525 
1526 		/* send firmware block meta-data */
1527 		error = usb_bulk_msg(dev, ar5523_cmd_tx_pipe(dev),
1528 				     txblock, sizeof(*txblock), &foolen,
1529 				     AR5523_CMD_TIMEOUT);
1530 		if (error) {
1531 			dev_err(&dev->dev,
1532 				"could not send firmware block info\n");
1533 			goto out_free_fwbuf;
1534 		}
1535 
1536 		/* send firmware block data */
1537 		memcpy(fwbuf, fw->data + offset, mlen);
1538 		error = usb_bulk_msg(dev, ar5523_data_tx_pipe(dev),
1539 				     fwbuf, mlen, &foolen,
1540 				     AR5523_DATA_TIMEOUT);
1541 		if (error) {
1542 			dev_err(&dev->dev,
1543 				"could not send firmware block data\n");
1544 			goto out_free_fwbuf;
1545 		}
1546 
1547 		/* wait for ack from firmware */
1548 		error = usb_bulk_msg(dev, ar5523_cmd_rx_pipe(dev),
1549 				     rxblock, sizeof(*rxblock), &foolen,
1550 				     AR5523_CMD_TIMEOUT);
1551 		if (error) {
1552 			dev_err(&dev->dev,
1553 				"could not read firmware answer\n");
1554 			goto out_free_fwbuf;
1555 		}
1556 
1557 		len -= mlen;
1558 		offset += mlen;
1559 	}
1560 
1561 	/*
1562 	 * Set the error to -ENXIO to make sure we continue probing for
1563 	 * a driver.
1564 	 */
1565 	error = -ENXIO;
1566 
1567  out_free_fwbuf:
1568 	kfree(fwbuf);
1569  out_free_rxblock:
1570 	kfree(rxblock);
1571  out_free_txblock:
1572 	kfree(txblock);
1573  out:
1574 	release_firmware(fw);
1575 	return error;
1576 }
1577 
1578 static int ar5523_probe(struct usb_interface *intf,
1579 			const struct usb_device_id *id)
1580 {
1581 	struct usb_device *dev = interface_to_usbdev(intf);
1582 	struct ieee80211_hw *hw;
1583 	struct ar5523 *ar;
1584 	int error = -ENOMEM;
1585 
1586 	/*
1587 	 * Load firmware if the device requires it.  This will return
1588 	 * -ENXIO on success and we'll get called back afer the usb
1589 	 * id changes to indicate that the firmware is present.
1590 	 */
1591 	if (id->driver_info & AR5523_FLAG_PRE_FIRMWARE)
1592 		return ar5523_load_firmware(dev);
1593 
1594 
1595 	hw = ieee80211_alloc_hw(sizeof(*ar), &ar5523_ops);
1596 	if (!hw)
1597 		goto out;
1598 	SET_IEEE80211_DEV(hw, &intf->dev);
1599 
1600 	ar = hw->priv;
1601 	ar->hw = hw;
1602 	ar->dev = dev;
1603 	mutex_init(&ar->mutex);
1604 
1605 	INIT_DELAYED_WORK(&ar->stat_work, ar5523_stat_work);
1606 	timer_setup(&ar->tx_wd_timer, ar5523_tx_wd_timer, 0);
1607 	INIT_WORK(&ar->tx_wd_work, ar5523_tx_wd_work);
1608 	INIT_WORK(&ar->tx_work, ar5523_tx_work);
1609 	INIT_LIST_HEAD(&ar->tx_queue_pending);
1610 	INIT_LIST_HEAD(&ar->tx_queue_submitted);
1611 	spin_lock_init(&ar->tx_data_list_lock);
1612 	atomic_set(&ar->tx_nr_total, 0);
1613 	atomic_set(&ar->tx_nr_pending, 0);
1614 	init_waitqueue_head(&ar->tx_flush_waitq);
1615 
1616 	atomic_set(&ar->rx_data_free_cnt, 0);
1617 	INIT_WORK(&ar->rx_refill_work, ar5523_rx_refill_work);
1618 	INIT_LIST_HEAD(&ar->rx_data_free);
1619 	INIT_LIST_HEAD(&ar->rx_data_used);
1620 	spin_lock_init(&ar->rx_data_list_lock);
1621 
1622 	ar->wq = create_singlethread_workqueue("ar5523");
1623 	if (!ar->wq) {
1624 		ar5523_err(ar, "Could not create wq\n");
1625 		goto out_free_ar;
1626 	}
1627 
1628 	error = ar5523_alloc_rx_bufs(ar);
1629 	if (error) {
1630 		ar5523_err(ar, "Could not allocate rx buffers\n");
1631 		goto out_free_wq;
1632 	}
1633 
1634 	error = ar5523_alloc_rx_cmd(ar);
1635 	if (error) {
1636 		ar5523_err(ar, "Could not allocate rx command buffers\n");
1637 		goto out_free_rx_bufs;
1638 	}
1639 
1640 	error = ar5523_alloc_tx_cmd(ar);
1641 	if (error) {
1642 		ar5523_err(ar, "Could not allocate tx command buffers\n");
1643 		goto out_free_rx_cmd;
1644 	}
1645 
1646 	error = ar5523_submit_rx_cmd(ar);
1647 	if (error) {
1648 		ar5523_err(ar, "Failed to submit rx cmd\n");
1649 		goto out_free_tx_cmd;
1650 	}
1651 
1652 	/*
1653 	 * We're now ready to send/receive firmware commands.
1654 	 */
1655 	error = ar5523_host_available(ar);
1656 	if (error) {
1657 		ar5523_err(ar, "could not initialize adapter\n");
1658 		goto out_cancel_rx_cmd;
1659 	}
1660 
1661 	error = ar5523_get_max_rxsz(ar);
1662 	if (error) {
1663 		ar5523_err(ar, "could not get caps from adapter\n");
1664 		goto out_cancel_rx_cmd;
1665 	}
1666 
1667 	error = ar5523_get_devcap(ar);
1668 	if (error) {
1669 		ar5523_err(ar, "could not get caps from adapter\n");
1670 		goto out_cancel_rx_cmd;
1671 	}
1672 
1673 	error = ar5523_get_devstatus(ar);
1674 	if (error != 0) {
1675 		ar5523_err(ar, "could not get device status\n");
1676 		goto out_cancel_rx_cmd;
1677 	}
1678 
1679 	ar5523_info(ar, "MAC/BBP AR5523, RF AR%c112\n",
1680 			(id->driver_info & AR5523_FLAG_ABG) ? '5' : '2');
1681 
1682 	ar->vif = NULL;
1683 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
1684 	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
1685 	ieee80211_hw_set(hw, SIGNAL_DBM);
1686 	hw->extra_tx_headroom = sizeof(struct ar5523_tx_desc) +
1687 				sizeof(struct ar5523_chunk);
1688 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
1689 	hw->queues = 1;
1690 
1691 	error = ar5523_init_modes(ar);
1692 	if (error)
1693 		goto out_cancel_rx_cmd;
1694 
1695 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1696 
1697 	usb_set_intfdata(intf, hw);
1698 
1699 	error = ieee80211_register_hw(hw);
1700 	if (error) {
1701 		ar5523_err(ar, "could not register device\n");
1702 		goto out_cancel_rx_cmd;
1703 	}
1704 
1705 	ar5523_info(ar, "Found and initialized AR5523 device\n");
1706 	return 0;
1707 
1708 out_cancel_rx_cmd:
1709 	ar5523_cancel_rx_cmd(ar);
1710 out_free_tx_cmd:
1711 	ar5523_free_tx_cmd(ar);
1712 out_free_rx_cmd:
1713 	ar5523_free_rx_cmd(ar);
1714 out_free_rx_bufs:
1715 	ar5523_free_rx_bufs(ar);
1716 out_free_wq:
1717 	destroy_workqueue(ar->wq);
1718 out_free_ar:
1719 	ieee80211_free_hw(hw);
1720 out:
1721 	return error;
1722 }
1723 
1724 static void ar5523_disconnect(struct usb_interface *intf)
1725 {
1726 	struct ieee80211_hw *hw = usb_get_intfdata(intf);
1727 	struct ar5523 *ar = hw->priv;
1728 
1729 	ar5523_dbg(ar, "detaching\n");
1730 	set_bit(AR5523_USB_DISCONNECTED, &ar->flags);
1731 
1732 	ieee80211_unregister_hw(hw);
1733 
1734 	ar5523_cancel_rx_cmd(ar);
1735 	ar5523_free_tx_cmd(ar);
1736 	ar5523_free_rx_cmd(ar);
1737 	ar5523_free_rx_bufs(ar);
1738 
1739 	destroy_workqueue(ar->wq);
1740 
1741 	ieee80211_free_hw(hw);
1742 	usb_set_intfdata(intf, NULL);
1743 }
1744 
1745 #define AR5523_DEVICE_UG(vendor, device) \
1746 	{ USB_DEVICE((vendor), (device)) }, \
1747 	{ USB_DEVICE((vendor), (device) + 1), \
1748 		.driver_info = AR5523_FLAG_PRE_FIRMWARE }
1749 #define AR5523_DEVICE_UX(vendor, device) \
1750 	{ USB_DEVICE((vendor), (device)), \
1751 		.driver_info = AR5523_FLAG_ABG }, \
1752 	{ USB_DEVICE((vendor), (device) + 1), \
1753 		.driver_info = AR5523_FLAG_ABG|AR5523_FLAG_PRE_FIRMWARE }
1754 
1755 static const struct usb_device_id ar5523_id_table[] = {
1756 	AR5523_DEVICE_UG(0x168c, 0x0001),	/* Atheros / AR5523 */
1757 	AR5523_DEVICE_UG(0x0cf3, 0x0001),	/* Atheros2 / AR5523_1 */
1758 	AR5523_DEVICE_UG(0x0cf3, 0x0003),	/* Atheros2 / AR5523_2 */
1759 	AR5523_DEVICE_UX(0x0cf3, 0x0005),	/* Atheros2 / AR5523_3 */
1760 	AR5523_DEVICE_UG(0x0d8e, 0x7801),	/* Conceptronic / AR5523_1 */
1761 	AR5523_DEVICE_UX(0x0d8e, 0x7811),	/* Conceptronic / AR5523_2 */
1762 	AR5523_DEVICE_UX(0x2001, 0x3a00),	/* Dlink / DWLAG132 */
1763 	AR5523_DEVICE_UG(0x2001, 0x3a02),	/* Dlink / DWLG132 */
1764 	AR5523_DEVICE_UX(0x2001, 0x3a04),	/* Dlink / DWLAG122 */
1765 	AR5523_DEVICE_UG(0x07d1, 0x3a07),	/* D-Link / WUA-2340 rev A1 */
1766 	AR5523_DEVICE_UG(0x1690, 0x0712),	/* Gigaset / AR5523 */
1767 	AR5523_DEVICE_UG(0x1690, 0x0710),	/* Gigaset / SMCWUSBTG */
1768 	AR5523_DEVICE_UG(0x129b, 0x160b),	/* Gigaset / USB stick 108
1769 						   (CyberTAN Technology) */
1770 	AR5523_DEVICE_UG(0x16ab, 0x7801),	/* Globalsun / AR5523_1 */
1771 	AR5523_DEVICE_UX(0x16ab, 0x7811),	/* Globalsun / AR5523_2 */
1772 	AR5523_DEVICE_UG(0x0d8e, 0x7802),	/* Globalsun / AR5523_3 */
1773 	AR5523_DEVICE_UX(0x0846, 0x4300),	/* Netgear / WG111U */
1774 	AR5523_DEVICE_UG(0x0846, 0x4250),	/* Netgear / WG111T */
1775 	AR5523_DEVICE_UG(0x0846, 0x5f00),	/* Netgear / WPN111 */
1776 	AR5523_DEVICE_UG(0x083a, 0x4506),	/* SMC / EZ Connect
1777 						   SMCWUSBT-G2 */
1778 	AR5523_DEVICE_UG(0x157e, 0x3006),	/* Umedia / AR5523_1, TEW444UBEU*/
1779 	AR5523_DEVICE_UX(0x157e, 0x3205),	/* Umedia / AR5523_2 */
1780 	AR5523_DEVICE_UG(0x1435, 0x0826),	/* Wistronneweb / AR5523_1 */
1781 	AR5523_DEVICE_UX(0x1435, 0x0828),	/* Wistronneweb / AR5523_2 */
1782 	AR5523_DEVICE_UG(0x0cde, 0x0012),	/* Zcom / AR5523 */
1783 	AR5523_DEVICE_UG(0x1385, 0x4250),	/* Netgear3 / WG111T (2) */
1784 	AR5523_DEVICE_UG(0x1385, 0x5f00),	/* Netgear / WPN111 */
1785 	AR5523_DEVICE_UG(0x1385, 0x5f02),	/* Netgear / WPN111 */
1786 	{ }
1787 };
1788 MODULE_DEVICE_TABLE(usb, ar5523_id_table);
1789 
1790 static struct usb_driver ar5523_driver = {
1791 	.name		= "ar5523",
1792 	.id_table	= ar5523_id_table,
1793 	.probe		= ar5523_probe,
1794 	.disconnect	= ar5523_disconnect,
1795 };
1796 
1797 module_usb_driver(ar5523_driver);
1798 
1799 MODULE_LICENSE("Dual BSD/GPL");
1800 MODULE_FIRMWARE(AR5523_FIRMWARE_FILE);
1801