• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/net/wireless/ath/ar9170/
1/*
2 * Atheros AR9170 driver
3 *
4 * USB - frontend
5 *
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, Christian Lamparter <chunkeey@web.de>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING.  If not, see
21 * http://www.gnu.org/licenses/.
22 *
23 * This file incorporates work covered by the following copyright and
24 * permission notice:
25 *    Copyright (c) 2007-2008 Atheros Communications, Inc.
26 *
27 *    Permission to use, copy, modify, and/or distribute this software for any
28 *    purpose with or without fee is hereby granted, provided that the above
29 *    copyright notice and this permission notice appear in all copies.
30 *
31 *    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32 *    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 *    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34 *    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 *    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 *    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37 *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 */
39
40#include <linux/module.h>
41#include <linux/slab.h>
42#include <linux/usb.h>
43#include <linux/firmware.h>
44#include <linux/etherdevice.h>
45#include <linux/device.h>
46#include <net/mac80211.h>
47#include "ar9170.h"
48#include "cmd.h"
49#include "hw.h"
50#include "usb.h"
51
52MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
53MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
54MODULE_LICENSE("GPL");
55MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
56MODULE_FIRMWARE("ar9170.fw");
57MODULE_FIRMWARE("ar9170-1.fw");
58MODULE_FIRMWARE("ar9170-2.fw");
59
60enum ar9170_requirements {
61	AR9170_REQ_FW1_ONLY = 1,
62};
63
64static struct usb_device_id ar9170_usb_ids[] = {
65	/* Atheros 9170 */
66	{ USB_DEVICE(0x0cf3, 0x9170) },
67	/* Atheros TG121N */
68	{ USB_DEVICE(0x0cf3, 0x1001) },
69	/* TP-Link TL-WN821N v2 */
70	{ USB_DEVICE(0x0cf3, 0x1002) },
71	/* 3Com Dual Band 802.11n USB Adapter */
72	{ USB_DEVICE(0x0cf3, 0x1010) },
73	/* H3C Dual Band 802.11n USB Adapter */
74	{ USB_DEVICE(0x0cf3, 0x1011) },
75	/* Cace Airpcap NX */
76	{ USB_DEVICE(0xcace, 0x0300) },
77	/* D-Link DWA 160 A1 */
78	{ USB_DEVICE(0x07d1, 0x3c10) },
79	/* D-Link DWA 160 A2 */
80	{ USB_DEVICE(0x07d1, 0x3a09) },
81	/* Netgear WNA1000 */
82	{ USB_DEVICE(0x0846, 0x9040) },
83	/* Netgear WNDA3100 */
84	{ USB_DEVICE(0x0846, 0x9010) },
85	/* Netgear WN111 v2 */
86	{ USB_DEVICE(0x0846, 0x9001) },
87	/* Zydas ZD1221 */
88	{ USB_DEVICE(0x0ace, 0x1221) },
89	/* Proxim ORiNOCO 802.11n USB */
90	{ USB_DEVICE(0x1435, 0x0804) },
91	/* WNC Generic 11n USB Dongle */
92	{ USB_DEVICE(0x1435, 0x0326) },
93	/* ZyXEL NWD271N */
94	{ USB_DEVICE(0x0586, 0x3417) },
95	/* Z-Com UB81 BG */
96	{ USB_DEVICE(0x0cde, 0x0023) },
97	/* Z-Com UB82 ABG */
98	{ USB_DEVICE(0x0cde, 0x0026) },
99	/* Sphairon Homelink 1202 */
100	{ USB_DEVICE(0x0cde, 0x0027) },
101	/* Arcadyan WN7512 */
102	{ USB_DEVICE(0x083a, 0xf522) },
103	/* Planex GWUS300 */
104	{ USB_DEVICE(0x2019, 0x5304) },
105	/* IO-Data WNGDNUS2 */
106	{ USB_DEVICE(0x04bb, 0x093f) },
107	/* AVM FRITZ!WLAN USB Stick N */
108	{ USB_DEVICE(0x057C, 0x8401) },
109	/* NEC WL300NU-G */
110	{ USB_DEVICE(0x0409, 0x0249) },
111	/* AVM FRITZ!WLAN USB Stick N 2.4 */
112	{ USB_DEVICE(0x057C, 0x8402), .driver_info = AR9170_REQ_FW1_ONLY },
113	/* Qwest/Actiontec 802AIN Wireless N USB Network Adapter */
114	{ USB_DEVICE(0x1668, 0x1200) },
115
116	/* terminate */
117	{}
118};
119MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
120
121static void ar9170_usb_submit_urb(struct ar9170_usb *aru)
122{
123	struct urb *urb;
124	unsigned long flags;
125	int err;
126
127	if (unlikely(!IS_STARTED(&aru->common)))
128		return ;
129
130	spin_lock_irqsave(&aru->tx_urb_lock, flags);
131	if (atomic_read(&aru->tx_submitted_urbs) >= AR9170_NUM_TX_URBS) {
132		spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
133		return ;
134	}
135	atomic_inc(&aru->tx_submitted_urbs);
136
137	urb = usb_get_from_anchor(&aru->tx_pending);
138	if (!urb) {
139		atomic_dec(&aru->tx_submitted_urbs);
140		spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
141
142		return ;
143	}
144	spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
145
146	aru->tx_pending_urbs--;
147	usb_anchor_urb(urb, &aru->tx_submitted);
148
149	err = usb_submit_urb(urb, GFP_ATOMIC);
150	if (unlikely(err)) {
151		if (ar9170_nag_limiter(&aru->common))
152			dev_err(&aru->udev->dev, "submit_urb failed (%d).\n",
153				err);
154
155		usb_unanchor_urb(urb);
156		atomic_dec(&aru->tx_submitted_urbs);
157		ar9170_tx_callback(&aru->common, urb->context);
158	}
159
160	usb_free_urb(urb);
161}
162
163static void ar9170_usb_tx_urb_complete_frame(struct urb *urb)
164{
165	struct sk_buff *skb = urb->context;
166	struct ar9170_usb *aru = (struct ar9170_usb *)
167	      usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
168
169	if (unlikely(!aru)) {
170		dev_kfree_skb_irq(skb);
171		return ;
172	}
173
174	atomic_dec(&aru->tx_submitted_urbs);
175
176	ar9170_tx_callback(&aru->common, skb);
177
178	ar9170_usb_submit_urb(aru);
179}
180
181static void ar9170_usb_tx_urb_complete(struct urb *urb)
182{
183}
184
185static void ar9170_usb_irq_completed(struct urb *urb)
186{
187	struct ar9170_usb *aru = urb->context;
188
189	switch (urb->status) {
190	/* everything is fine */
191	case 0:
192		break;
193
194	/* disconnect */
195	case -ENOENT:
196	case -ECONNRESET:
197	case -ENODEV:
198	case -ESHUTDOWN:
199		goto free;
200
201	default:
202		goto resubmit;
203	}
204
205	ar9170_handle_command_response(&aru->common, urb->transfer_buffer,
206				       urb->actual_length);
207
208resubmit:
209	usb_anchor_urb(urb, &aru->rx_submitted);
210	if (usb_submit_urb(urb, GFP_ATOMIC)) {
211		usb_unanchor_urb(urb);
212		goto free;
213	}
214
215	return;
216
217free:
218	usb_free_coherent(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
219}
220
221static void ar9170_usb_rx_completed(struct urb *urb)
222{
223	struct sk_buff *skb = urb->context;
224	struct ar9170_usb *aru = (struct ar9170_usb *)
225		usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
226	int err;
227
228	if (!aru)
229		goto free;
230
231	switch (urb->status) {
232	/* everything is fine */
233	case 0:
234		break;
235
236	/* disconnect */
237	case -ENOENT:
238	case -ECONNRESET:
239	case -ENODEV:
240	case -ESHUTDOWN:
241		goto free;
242
243	default:
244		goto resubmit;
245	}
246
247	skb_put(skb, urb->actual_length);
248	ar9170_rx(&aru->common, skb);
249
250resubmit:
251	skb_reset_tail_pointer(skb);
252	skb_trim(skb, 0);
253
254	usb_anchor_urb(urb, &aru->rx_submitted);
255	err = usb_submit_urb(urb, GFP_ATOMIC);
256	if (unlikely(err)) {
257		usb_unanchor_urb(urb);
258		goto free;
259	}
260
261	return ;
262
263free:
264	dev_kfree_skb_irq(skb);
265}
266
267static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
268				  struct urb *urb, gfp_t gfp)
269{
270	struct sk_buff *skb;
271
272	skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
273	if (!skb)
274		return -ENOMEM;
275
276	/* reserve some space for mac80211's radiotap */
277	skb_reserve(skb, 32);
278
279	usb_fill_bulk_urb(urb, aru->udev,
280			  usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
281			  skb->data, min(skb_tailroom(skb),
282			  AR9170_MAX_RX_BUFFER_SIZE),
283			  ar9170_usb_rx_completed, skb);
284
285	return 0;
286}
287
288static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
289{
290	struct urb *urb = NULL;
291	void *ibuf;
292	int err = -ENOMEM;
293
294	/* initialize interrupt endpoint */
295	urb = usb_alloc_urb(0, GFP_KERNEL);
296	if (!urb)
297		goto out;
298
299	ibuf = usb_alloc_coherent(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
300	if (!ibuf)
301		goto out;
302
303	usb_fill_int_urb(urb, aru->udev,
304			 usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
305			 64, ar9170_usb_irq_completed, aru, 1);
306	urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
307
308	usb_anchor_urb(urb, &aru->rx_submitted);
309	err = usb_submit_urb(urb, GFP_KERNEL);
310	if (err) {
311		usb_unanchor_urb(urb);
312		usb_free_coherent(aru->udev, 64, urb->transfer_buffer,
313				  urb->transfer_dma);
314	}
315
316out:
317	usb_free_urb(urb);
318	return err;
319}
320
321static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
322{
323	struct urb *urb;
324	int i;
325	int err = -EINVAL;
326
327	for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
328		err = -ENOMEM;
329		urb = usb_alloc_urb(0, GFP_KERNEL);
330		if (!urb)
331			goto err_out;
332
333		err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
334		if (err) {
335			usb_free_urb(urb);
336			goto err_out;
337		}
338
339		usb_anchor_urb(urb, &aru->rx_submitted);
340		err = usb_submit_urb(urb, GFP_KERNEL);
341		if (err) {
342			usb_unanchor_urb(urb);
343			dev_kfree_skb_any((void *) urb->transfer_buffer);
344			usb_free_urb(urb);
345			goto err_out;
346		}
347		usb_free_urb(urb);
348	}
349
350	/* the device now waiting for a firmware. */
351	aru->common.state = AR9170_IDLE;
352	return 0;
353
354err_out:
355
356	usb_kill_anchored_urbs(&aru->rx_submitted);
357	return err;
358}
359
360static int ar9170_usb_flush(struct ar9170 *ar)
361{
362	struct ar9170_usb *aru = (void *) ar;
363	struct urb *urb;
364	int ret, err = 0;
365
366	if (IS_STARTED(ar))
367		aru->common.state = AR9170_IDLE;
368
369	usb_wait_anchor_empty_timeout(&aru->tx_pending,
370					    msecs_to_jiffies(800));
371	while ((urb = usb_get_from_anchor(&aru->tx_pending))) {
372		ar9170_tx_callback(&aru->common, (void *) urb->context);
373		usb_free_urb(urb);
374	}
375
376	/* lets wait a while until the tx - queues are dried out */
377	ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
378					    msecs_to_jiffies(100));
379	if (ret == 0)
380		err = -ETIMEDOUT;
381
382	usb_kill_anchored_urbs(&aru->tx_submitted);
383
384	if (IS_ACCEPTING_CMD(ar))
385		aru->common.state = AR9170_STARTED;
386
387	return err;
388}
389
390static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
391{
392	int err;
393
394	aru->common.state = AR9170_UNKNOWN_STATE;
395
396	err = ar9170_usb_flush(&aru->common);
397	if (err)
398		dev_err(&aru->udev->dev, "stuck tx urbs!\n");
399
400	usb_poison_anchored_urbs(&aru->tx_submitted);
401	usb_poison_anchored_urbs(&aru->rx_submitted);
402}
403
404static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
405			       unsigned int plen, void *payload,
406			       unsigned int outlen, void *out)
407{
408	struct ar9170_usb *aru = (void *) ar;
409	struct urb *urb = NULL;
410	unsigned long flags;
411	int err = -ENOMEM;
412
413	if (unlikely(!IS_ACCEPTING_CMD(ar)))
414		return -EPERM;
415
416	if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
417		return -EINVAL;
418
419	urb = usb_alloc_urb(0, GFP_ATOMIC);
420	if (unlikely(!urb))
421		goto err_free;
422
423	ar->cmdbuf[0] = cpu_to_le32(plen);
424	ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
425	/* writing multiple regs fills this buffer already */
426	if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
427		memcpy(&ar->cmdbuf[1], payload, plen);
428
429	spin_lock_irqsave(&aru->common.cmdlock, flags);
430	aru->readbuf = (u8 *)out;
431	aru->readlen = outlen;
432	spin_unlock_irqrestore(&aru->common.cmdlock, flags);
433
434	usb_fill_int_urb(urb, aru->udev,
435			 usb_sndintpipe(aru->udev, AR9170_EP_CMD),
436			 aru->common.cmdbuf, plen + 4,
437			 ar9170_usb_tx_urb_complete, NULL, 1);
438
439	usb_anchor_urb(urb, &aru->tx_submitted);
440	err = usb_submit_urb(urb, GFP_ATOMIC);
441	if (unlikely(err)) {
442		usb_unanchor_urb(urb);
443		usb_free_urb(urb);
444		goto err_unbuf;
445	}
446	usb_free_urb(urb);
447
448	err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
449	if (err == 0) {
450		err = -ETIMEDOUT;
451		goto err_unbuf;
452	}
453
454	if (aru->readlen != outlen) {
455		err = -EMSGSIZE;
456		goto err_unbuf;
457	}
458
459	return 0;
460
461err_unbuf:
462	/* Maybe the device was removed in the second we were waiting? */
463	if (IS_STARTED(ar)) {
464		dev_err(&aru->udev->dev, "no command feedback "
465					 "received (%d).\n", err);
466
467		/* provide some maybe useful debug information */
468		print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
469				     aru->common.cmdbuf, plen + 4);
470		dump_stack();
471	}
472
473	/* invalidate to avoid completing the next prematurely */
474	spin_lock_irqsave(&aru->common.cmdlock, flags);
475	aru->readbuf = NULL;
476	aru->readlen = 0;
477	spin_unlock_irqrestore(&aru->common.cmdlock, flags);
478
479err_free:
480
481	return err;
482}
483
484static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
485{
486	struct ar9170_usb *aru = (struct ar9170_usb *) ar;
487	struct urb *urb;
488
489	if (unlikely(!IS_STARTED(ar))) {
490		/* Seriously, what were you drink... err... thinking!? */
491		return -EPERM;
492	}
493
494	urb = usb_alloc_urb(0, GFP_ATOMIC);
495	if (unlikely(!urb))
496		return -ENOMEM;
497
498	usb_fill_bulk_urb(urb, aru->udev,
499			  usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
500			  skb->data, skb->len,
501			  ar9170_usb_tx_urb_complete_frame, skb);
502	urb->transfer_flags |= URB_ZERO_PACKET;
503
504	usb_anchor_urb(urb, &aru->tx_pending);
505	aru->tx_pending_urbs++;
506
507	usb_free_urb(urb);
508
509	ar9170_usb_submit_urb(aru);
510	return 0;
511}
512
513static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
514{
515	struct ar9170_usb *aru = (void *) ar;
516	unsigned long flags;
517	u32 in, out;
518
519	if (unlikely(!buffer))
520		return ;
521
522	in = le32_to_cpup((__le32 *)buffer);
523	out = le32_to_cpu(ar->cmdbuf[0]);
524
525	/* mask off length byte */
526	out &= ~0xFF;
527
528	if (aru->readlen >= 0) {
529		/* add expected length */
530		out |= aru->readlen;
531	} else {
532		/* add obtained length */
533		out |= in & 0xFF;
534	}
535
536	/*
537	 * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
538	 * length and we cannot predict the correct length in advance.
539	 * So we only check if we provided enough space for the data.
540	 */
541	if (unlikely(out < in)) {
542		dev_warn(&aru->udev->dev, "received invalid command response "
543					  "got %d bytes, instead of %d bytes "
544					  "and the resp length is %d bytes\n",
545			 in, out, len);
546		print_hex_dump_bytes("ar9170 invalid resp: ",
547				     DUMP_PREFIX_OFFSET, buffer, len);
548		/*
549		 * Do not complete, then the command times out,
550		 * and we get a stack trace from there.
551		 */
552		return ;
553	}
554
555	spin_lock_irqsave(&aru->common.cmdlock, flags);
556	if (aru->readbuf && len > 0) {
557		memcpy(aru->readbuf, buffer + 4, len - 4);
558		aru->readbuf = NULL;
559	}
560	complete(&aru->cmd_wait);
561	spin_unlock_irqrestore(&aru->common.cmdlock, flags);
562}
563
564static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
565			     size_t len, u32 addr, bool complete)
566{
567	int transfer, err;
568	u8 *buf = kmalloc(4096, GFP_KERNEL);
569
570	if (!buf)
571		return -ENOMEM;
572
573	while (len) {
574		transfer = min_t(int, len, 4096);
575		memcpy(buf, data, transfer);
576
577		err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
578				      0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
579				      addr >> 8, 0, buf, transfer, 1000);
580
581		if (err < 0) {
582			kfree(buf);
583			return err;
584		}
585
586		len -= transfer;
587		data += transfer;
588		addr += transfer;
589	}
590	kfree(buf);
591
592	if (complete) {
593		err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
594				      0x31 /* FW DL COMPLETE */,
595				      0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
596	}
597
598	return 0;
599}
600
601static int ar9170_usb_reset(struct ar9170_usb *aru)
602{
603	int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
604
605	if (lock) {
606		ret = usb_lock_device_for_reset(aru->udev, aru->intf);
607		if (ret < 0) {
608			dev_err(&aru->udev->dev, "unable to lock device "
609				"for reset (%d).\n", ret);
610			return ret;
611		}
612	}
613
614	ret = usb_reset_device(aru->udev);
615	if (lock)
616		usb_unlock_device(aru->udev);
617
618	/* let it rest - for a second - */
619	msleep(1000);
620
621	return ret;
622}
623
624static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
625{
626	int err;
627
628	if (!aru->init_values)
629		goto upload_fw_start;
630
631	/* First, upload initial values to device RAM */
632	err = ar9170_usb_upload(aru, aru->init_values->data,
633				aru->init_values->size, 0x102800, false);
634	if (err) {
635		dev_err(&aru->udev->dev, "firmware part 1 "
636			"upload failed (%d).\n", err);
637		return err;
638	}
639
640upload_fw_start:
641
642	/* Then, upload the firmware itself and start it */
643	return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
644				0x200000, true);
645}
646
647static int ar9170_usb_init_transport(struct ar9170_usb *aru)
648{
649	struct ar9170 *ar = (void *) &aru->common;
650	int err;
651
652	ar9170_regwrite_begin(ar);
653
654	/* Set USB Rx stream mode MAX packet number to 2 */
655	ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
656
657	/* Set USB Rx stream mode timeout to 10us */
658	ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
659
660	ar9170_regwrite_finish();
661
662	err = ar9170_regwrite_result();
663	if (err)
664		dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
665
666	return err;
667}
668
669static void ar9170_usb_stop(struct ar9170 *ar)
670{
671	struct ar9170_usb *aru = (void *) ar;
672	int ret;
673
674	if (IS_ACCEPTING_CMD(ar))
675		aru->common.state = AR9170_STOPPED;
676
677	ret = ar9170_usb_flush(ar);
678	if (ret)
679		dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
680
681	usb_poison_anchored_urbs(&aru->tx_submitted);
682
683	/*
684	 * Note:
685	 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
686	 * Else we would end up with a unresponsive device...
687	 */
688}
689
690static int ar9170_usb_open(struct ar9170 *ar)
691{
692	struct ar9170_usb *aru = (void *) ar;
693	int err;
694
695	usb_unpoison_anchored_urbs(&aru->tx_submitted);
696	err = ar9170_usb_init_transport(aru);
697	if (err) {
698		usb_poison_anchored_urbs(&aru->tx_submitted);
699		return err;
700	}
701
702	aru->common.state = AR9170_IDLE;
703	return 0;
704}
705
706static int ar9170_usb_init_device(struct ar9170_usb *aru)
707{
708	int err;
709
710	err = ar9170_usb_alloc_rx_irq_urb(aru);
711	if (err)
712		goto err_out;
713
714	err = ar9170_usb_alloc_rx_bulk_urbs(aru);
715	if (err)
716		goto err_unrx;
717
718	err = ar9170_usb_upload_firmware(aru);
719	if (err) {
720		err = ar9170_echo_test(&aru->common, 0x60d43110);
721		if (err) {
722			/* force user invention, by disabling the device */
723			err = usb_driver_set_configuration(aru->udev, -1);
724			dev_err(&aru->udev->dev, "device is in a bad state. "
725						 "please reconnect it!\n");
726			goto err_unrx;
727		}
728	}
729
730	return 0;
731
732err_unrx:
733	ar9170_usb_cancel_urbs(aru);
734
735err_out:
736	return err;
737}
738
739static void ar9170_usb_firmware_failed(struct ar9170_usb *aru)
740{
741	struct device *parent = aru->udev->dev.parent;
742	struct usb_device *udev;
743
744	/*
745	 * Store a copy of the usb_device pointer locally.
746	 * This is because device_release_driver initiates
747	 * ar9170_usb_disconnect, which in turn frees our
748	 * driver context (aru).
749	 */
750	udev = aru->udev;
751
752	complete(&aru->firmware_loading_complete);
753
754	/* unbind anything failed */
755	if (parent)
756		device_lock(parent);
757
758	device_release_driver(&udev->dev);
759	if (parent)
760		device_unlock(parent);
761
762	usb_put_dev(udev);
763}
764
765static void ar9170_usb_firmware_finish(const struct firmware *fw, void *context)
766{
767	struct ar9170_usb *aru = context;
768	int err;
769
770	aru->firmware = fw;
771
772	if (!fw) {
773		dev_err(&aru->udev->dev, "firmware file not found.\n");
774		goto err_freefw;
775	}
776
777	err = ar9170_usb_init_device(aru);
778	if (err)
779		goto err_freefw;
780
781	err = ar9170_usb_open(&aru->common);
782	if (err)
783		goto err_unrx;
784
785	err = ar9170_register(&aru->common, &aru->udev->dev);
786
787	ar9170_usb_stop(&aru->common);
788	if (err)
789		goto err_unrx;
790
791	complete(&aru->firmware_loading_complete);
792	usb_put_dev(aru->udev);
793	return;
794
795 err_unrx:
796	ar9170_usb_cancel_urbs(aru);
797
798 err_freefw:
799	ar9170_usb_firmware_failed(aru);
800}
801
802static void ar9170_usb_firmware_inits(const struct firmware *fw,
803				      void *context)
804{
805	struct ar9170_usb *aru = context;
806	int err;
807
808	if (!fw) {
809		dev_err(&aru->udev->dev, "file with init values not found.\n");
810		ar9170_usb_firmware_failed(aru);
811		return;
812	}
813
814	aru->init_values = fw;
815
816	/* ok so we have the init values -- get code for two-stage */
817
818	err = request_firmware_nowait(THIS_MODULE, 1, "ar9170-2.fw",
819				      &aru->udev->dev, GFP_KERNEL, aru,
820				      ar9170_usb_firmware_finish);
821	if (err)
822		ar9170_usb_firmware_failed(aru);
823}
824
825static void ar9170_usb_firmware_step2(const struct firmware *fw, void *context)
826{
827	struct ar9170_usb *aru = context;
828	int err;
829
830	if (fw) {
831		ar9170_usb_firmware_finish(fw, context);
832		return;
833	}
834
835	if (aru->req_one_stage_fw) {
836		dev_err(&aru->udev->dev, "ar9170.fw firmware file "
837			"not found and is required for this device\n");
838		ar9170_usb_firmware_failed(aru);
839		return;
840	}
841
842	dev_err(&aru->udev->dev, "ar9170.fw firmware file "
843		"not found, trying old firmware...\n");
844
845	err = request_firmware_nowait(THIS_MODULE, 1, "ar9170-1.fw",
846				      &aru->udev->dev, GFP_KERNEL, aru,
847				      ar9170_usb_firmware_inits);
848	if (err)
849		ar9170_usb_firmware_failed(aru);
850}
851
852static bool ar9170_requires_one_stage(const struct usb_device_id *id)
853{
854	if (!id->driver_info)
855		return false;
856	if (id->driver_info == AR9170_REQ_FW1_ONLY)
857		return true;
858	return false;
859}
860
861static int ar9170_usb_probe(struct usb_interface *intf,
862			const struct usb_device_id *id)
863{
864	struct ar9170_usb *aru;
865	struct ar9170 *ar;
866	struct usb_device *udev;
867	int err;
868
869	aru = ar9170_alloc(sizeof(*aru));
870	if (IS_ERR(aru)) {
871		err = PTR_ERR(aru);
872		goto out;
873	}
874
875	udev = interface_to_usbdev(intf);
876	usb_get_dev(udev);
877	aru->udev = udev;
878	aru->intf = intf;
879	ar = &aru->common;
880
881	aru->req_one_stage_fw = ar9170_requires_one_stage(id);
882
883	usb_set_intfdata(intf, aru);
884	SET_IEEE80211_DEV(ar->hw, &intf->dev);
885
886	init_usb_anchor(&aru->rx_submitted);
887	init_usb_anchor(&aru->tx_pending);
888	init_usb_anchor(&aru->tx_submitted);
889	init_completion(&aru->cmd_wait);
890	init_completion(&aru->firmware_loading_complete);
891	spin_lock_init(&aru->tx_urb_lock);
892
893	aru->tx_pending_urbs = 0;
894	atomic_set(&aru->tx_submitted_urbs, 0);
895
896	aru->common.stop = ar9170_usb_stop;
897	aru->common.flush = ar9170_usb_flush;
898	aru->common.open = ar9170_usb_open;
899	aru->common.tx = ar9170_usb_tx;
900	aru->common.exec_cmd = ar9170_usb_exec_cmd;
901	aru->common.callback_cmd = ar9170_usb_callback_cmd;
902
903#ifdef CONFIG_PM
904	udev->reset_resume = 1;
905#endif /* CONFIG_PM */
906	err = ar9170_usb_reset(aru);
907	if (err)
908		goto err_freehw;
909
910	usb_get_dev(aru->udev);
911	return request_firmware_nowait(THIS_MODULE, 1, "ar9170.fw",
912				       &aru->udev->dev, GFP_KERNEL, aru,
913				       ar9170_usb_firmware_step2);
914err_freehw:
915	usb_set_intfdata(intf, NULL);
916	usb_put_dev(udev);
917	ieee80211_free_hw(ar->hw);
918out:
919	return err;
920}
921
922static void ar9170_usb_disconnect(struct usb_interface *intf)
923{
924	struct ar9170_usb *aru = usb_get_intfdata(intf);
925
926	if (!aru)
927		return;
928
929	aru->common.state = AR9170_IDLE;
930
931	wait_for_completion(&aru->firmware_loading_complete);
932
933	ar9170_unregister(&aru->common);
934	ar9170_usb_cancel_urbs(aru);
935
936	usb_put_dev(aru->udev);
937	usb_set_intfdata(intf, NULL);
938	ieee80211_free_hw(aru->common.hw);
939
940	release_firmware(aru->init_values);
941	release_firmware(aru->firmware);
942}
943
944#ifdef CONFIG_PM
945static int ar9170_suspend(struct usb_interface *intf,
946			  pm_message_t  message)
947{
948	struct ar9170_usb *aru = usb_get_intfdata(intf);
949
950	if (!aru)
951		return -ENODEV;
952
953	aru->common.state = AR9170_IDLE;
954	ar9170_usb_cancel_urbs(aru);
955
956	return 0;
957}
958
959static int ar9170_resume(struct usb_interface *intf)
960{
961	struct ar9170_usb *aru = usb_get_intfdata(intf);
962	int err;
963
964	if (!aru)
965		return -ENODEV;
966
967	usb_unpoison_anchored_urbs(&aru->rx_submitted);
968	usb_unpoison_anchored_urbs(&aru->tx_submitted);
969
970	err = ar9170_usb_init_device(aru);
971	if (err)
972		goto err_unrx;
973
974	err = ar9170_usb_open(&aru->common);
975	if (err)
976		goto err_unrx;
977
978	return 0;
979
980err_unrx:
981	aru->common.state = AR9170_IDLE;
982	ar9170_usb_cancel_urbs(aru);
983
984	return err;
985}
986#endif /* CONFIG_PM */
987
988static struct usb_driver ar9170_driver = {
989	.name = "ar9170usb",
990	.probe = ar9170_usb_probe,
991	.disconnect = ar9170_usb_disconnect,
992	.id_table = ar9170_usb_ids,
993	.soft_unbind = 1,
994#ifdef CONFIG_PM
995	.suspend = ar9170_suspend,
996	.resume = ar9170_resume,
997	.reset_resume = ar9170_resume,
998#endif /* CONFIG_PM */
999};
1000
1001static int __init ar9170_init(void)
1002{
1003	return usb_register(&ar9170_driver);
1004}
1005
1006static void __exit ar9170_exit(void)
1007{
1008	usb_deregister(&ar9170_driver);
1009}
1010
1011module_init(ar9170_init);
1012module_exit(ar9170_exit);
1013