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