xref: /openbmc/linux/drivers/net/wireless/ath/carl9170/usb.c (revision b4764c809a306ea37b6409494896e919bbb5ec5f)
1a84fab3cSChristian Lamparter /*
2a84fab3cSChristian Lamparter  * Atheros CARL9170 driver
3a84fab3cSChristian Lamparter  *
4a84fab3cSChristian Lamparter  * USB - frontend
5a84fab3cSChristian Lamparter  *
6a84fab3cSChristian Lamparter  * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7a84fab3cSChristian Lamparter  * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
8a84fab3cSChristian Lamparter  *
9a84fab3cSChristian Lamparter  * This program is free software; you can redistribute it and/or modify
10a84fab3cSChristian Lamparter  * it under the terms of the GNU General Public License as published by
11a84fab3cSChristian Lamparter  * the Free Software Foundation; either version 2 of the License, or
12a84fab3cSChristian Lamparter  * (at your option) any later version.
13a84fab3cSChristian Lamparter  *
14a84fab3cSChristian Lamparter  * This program is distributed in the hope that it will be useful,
15a84fab3cSChristian Lamparter  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16a84fab3cSChristian Lamparter  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17a84fab3cSChristian Lamparter  * GNU General Public License for more details.
18a84fab3cSChristian Lamparter  *
19a84fab3cSChristian Lamparter  * You should have received a copy of the GNU General Public License
20a84fab3cSChristian Lamparter  * along with this program; see the file COPYING.  If not, see
21a84fab3cSChristian Lamparter  * http://www.gnu.org/licenses/.
22a84fab3cSChristian Lamparter  *
23a84fab3cSChristian Lamparter  * This file incorporates work covered by the following copyright and
24a84fab3cSChristian Lamparter  * permission notice:
25a84fab3cSChristian Lamparter  *    Copyright (c) 2007-2008 Atheros Communications, Inc.
26a84fab3cSChristian Lamparter  *
27a84fab3cSChristian Lamparter  *    Permission to use, copy, modify, and/or distribute this software for any
28a84fab3cSChristian Lamparter  *    purpose with or without fee is hereby granted, provided that the above
29a84fab3cSChristian Lamparter  *    copyright notice and this permission notice appear in all copies.
30a84fab3cSChristian Lamparter  *
31a84fab3cSChristian Lamparter  *    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32a84fab3cSChristian Lamparter  *    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33a84fab3cSChristian Lamparter  *    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34a84fab3cSChristian Lamparter  *    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35a84fab3cSChristian Lamparter  *    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36a84fab3cSChristian Lamparter  *    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37a84fab3cSChristian Lamparter  *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38a84fab3cSChristian Lamparter  */
39a84fab3cSChristian Lamparter 
40a84fab3cSChristian Lamparter #include <linux/module.h>
41a84fab3cSChristian Lamparter #include <linux/slab.h>
42a84fab3cSChristian Lamparter #include <linux/usb.h>
43a84fab3cSChristian Lamparter #include <linux/firmware.h>
44a84fab3cSChristian Lamparter #include <linux/etherdevice.h>
45a84fab3cSChristian Lamparter #include <linux/device.h>
46a84fab3cSChristian Lamparter #include <net/mac80211.h>
47a84fab3cSChristian Lamparter #include "carl9170.h"
48a84fab3cSChristian Lamparter #include "cmd.h"
49a84fab3cSChristian Lamparter #include "hw.h"
50a84fab3cSChristian Lamparter #include "fwcmd.h"
51a84fab3cSChristian Lamparter 
52a84fab3cSChristian Lamparter MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
53a84fab3cSChristian Lamparter MODULE_AUTHOR("Christian Lamparter <chunkeey@googlemail.com>");
54a84fab3cSChristian Lamparter MODULE_LICENSE("GPL");
55a84fab3cSChristian Lamparter MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
56a84fab3cSChristian Lamparter MODULE_FIRMWARE(CARL9170FW_NAME);
57a84fab3cSChristian Lamparter MODULE_ALIAS("ar9170usb");
58a84fab3cSChristian Lamparter MODULE_ALIAS("arusb_lnx");
59a84fab3cSChristian Lamparter 
60a84fab3cSChristian Lamparter /*
61a84fab3cSChristian Lamparter  * Note:
62a84fab3cSChristian Lamparter  *
63a84fab3cSChristian Lamparter  * Always update our wiki's device list (located at:
64a84fab3cSChristian Lamparter  * http://wireless.kernel.org/en/users/Drivers/ar9170/devices ),
65a84fab3cSChristian Lamparter  * whenever you add a new device.
66a84fab3cSChristian Lamparter  */
67a84fab3cSChristian Lamparter static struct usb_device_id carl9170_usb_ids[] = {
68a84fab3cSChristian Lamparter 	/* Atheros 9170 */
69a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0cf3, 0x9170) },
70a84fab3cSChristian Lamparter 	/* Atheros TG121N */
71a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0cf3, 0x1001) },
72a84fab3cSChristian Lamparter 	/* TP-Link TL-WN821N v2 */
73a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0cf3, 0x1002), .driver_info = CARL9170_WPS_BUTTON |
74a84fab3cSChristian Lamparter 		 CARL9170_ONE_LED },
75a84fab3cSChristian Lamparter 	/* 3Com Dual Band 802.11n USB Adapter */
76a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0cf3, 0x1010) },
77a84fab3cSChristian Lamparter 	/* H3C Dual Band 802.11n USB Adapter */
78a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0cf3, 0x1011) },
79a84fab3cSChristian Lamparter 	/* Cace Airpcap NX */
80a84fab3cSChristian Lamparter 	{ USB_DEVICE(0xcace, 0x0300) },
81a84fab3cSChristian Lamparter 	/* D-Link DWA 160 A1 */
82a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x07d1, 0x3c10) },
83a84fab3cSChristian Lamparter 	/* D-Link DWA 160 A2 */
84a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x07d1, 0x3a09) },
858df86db9SChristian Lamparter 	/* D-Link DWA 130 D */
868df86db9SChristian Lamparter 	{ USB_DEVICE(0x07d1, 0x3a0f) },
87a84fab3cSChristian Lamparter 	/* Netgear WNA1000 */
88a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0846, 0x9040) },
898df86db9SChristian Lamparter 	/* Netgear WNDA3100 (v1) */
90a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0846, 0x9010) },
91a84fab3cSChristian Lamparter 	/* Netgear WN111 v2 */
92a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0846, 0x9001), .driver_info = CARL9170_ONE_LED },
93a84fab3cSChristian Lamparter 	/* Zydas ZD1221 */
94a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0ace, 0x1221) },
95a84fab3cSChristian Lamparter 	/* Proxim ORiNOCO 802.11n USB */
96a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x1435, 0x0804) },
97a84fab3cSChristian Lamparter 	/* WNC Generic 11n USB Dongle */
98a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x1435, 0x0326) },
99a84fab3cSChristian Lamparter 	/* ZyXEL NWD271N */
100a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0586, 0x3417) },
101a84fab3cSChristian Lamparter 	/* Z-Com UB81 BG */
102a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0cde, 0x0023) },
103a84fab3cSChristian Lamparter 	/* Z-Com UB82 ABG */
104a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0cde, 0x0026) },
105a84fab3cSChristian Lamparter 	/* Sphairon Homelink 1202 */
106a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0cde, 0x0027) },
107a84fab3cSChristian Lamparter 	/* Arcadyan WN7512 */
108a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x083a, 0xf522) },
109a84fab3cSChristian Lamparter 	/* Planex GWUS300 */
110a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x2019, 0x5304) },
111a84fab3cSChristian Lamparter 	/* IO-Data WNGDNUS2 */
112a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x04bb, 0x093f) },
113a84fab3cSChristian Lamparter 	/* NEC WL300NU-G */
114a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x0409, 0x0249) },
11506a86ddbSChristian Lamparter 	/* NEC WL300NU-AG */
11606a86ddbSChristian Lamparter 	{ USB_DEVICE(0x0409, 0x02b4) },
117a84fab3cSChristian Lamparter 	/* AVM FRITZ!WLAN USB Stick N */
118a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x057c, 0x8401) },
119a84fab3cSChristian Lamparter 	/* AVM FRITZ!WLAN USB Stick N 2.4 */
120a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x057c, 0x8402) },
121a84fab3cSChristian Lamparter 	/* Qwest/Actiontec 802AIN Wireless N USB Network Adapter */
122a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x1668, 0x1200) },
123c86664e5SJan Puk 	/* Airlive X.USB a/b/g/n */
124c86664e5SJan Puk 	{ USB_DEVICE(0x1b75, 0x9170) },
125a84fab3cSChristian Lamparter 
126a84fab3cSChristian Lamparter 	/* terminate */
127a84fab3cSChristian Lamparter 	{}
128a84fab3cSChristian Lamparter };
129a84fab3cSChristian Lamparter MODULE_DEVICE_TABLE(usb, carl9170_usb_ids);
130a84fab3cSChristian Lamparter 
131a84fab3cSChristian Lamparter static void carl9170_usb_submit_data_urb(struct ar9170 *ar)
132a84fab3cSChristian Lamparter {
133a84fab3cSChristian Lamparter 	struct urb *urb;
134a84fab3cSChristian Lamparter 	int err;
135a84fab3cSChristian Lamparter 
136a84fab3cSChristian Lamparter 	if (atomic_inc_return(&ar->tx_anch_urbs) > AR9170_NUM_TX_URBS)
137a84fab3cSChristian Lamparter 		goto err_acc;
138a84fab3cSChristian Lamparter 
139a84fab3cSChristian Lamparter 	urb = usb_get_from_anchor(&ar->tx_wait);
140a84fab3cSChristian Lamparter 	if (!urb)
141a84fab3cSChristian Lamparter 		goto err_acc;
142a84fab3cSChristian Lamparter 
143a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->tx_anch);
144a84fab3cSChristian Lamparter 
145a84fab3cSChristian Lamparter 	err = usb_submit_urb(urb, GFP_ATOMIC);
146a84fab3cSChristian Lamparter 	if (unlikely(err)) {
147a84fab3cSChristian Lamparter 		if (net_ratelimit()) {
148a84fab3cSChristian Lamparter 			dev_err(&ar->udev->dev, "tx submit failed (%d)\n",
149a84fab3cSChristian Lamparter 				urb->status);
150a84fab3cSChristian Lamparter 		}
151a84fab3cSChristian Lamparter 
152a84fab3cSChristian Lamparter 		usb_unanchor_urb(urb);
153a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->tx_err);
154a84fab3cSChristian Lamparter 	}
155a84fab3cSChristian Lamparter 
156a84fab3cSChristian Lamparter 	usb_free_urb(urb);
157a84fab3cSChristian Lamparter 
158a84fab3cSChristian Lamparter 	if (likely(err == 0))
159a84fab3cSChristian Lamparter 		return;
160a84fab3cSChristian Lamparter 
161a84fab3cSChristian Lamparter err_acc:
162a84fab3cSChristian Lamparter 	atomic_dec(&ar->tx_anch_urbs);
163a84fab3cSChristian Lamparter }
164a84fab3cSChristian Lamparter 
165a84fab3cSChristian Lamparter static void carl9170_usb_tx_data_complete(struct urb *urb)
166a84fab3cSChristian Lamparter {
167b2767363SJoe Perches 	struct ar9170 *ar = usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
168a84fab3cSChristian Lamparter 
169a84fab3cSChristian Lamparter 	if (WARN_ON_ONCE(!ar)) {
170a84fab3cSChristian Lamparter 		dev_kfree_skb_irq(urb->context);
171a84fab3cSChristian Lamparter 		return;
172a84fab3cSChristian Lamparter 	}
173a84fab3cSChristian Lamparter 
174a84fab3cSChristian Lamparter 	atomic_dec(&ar->tx_anch_urbs);
175a84fab3cSChristian Lamparter 
176a84fab3cSChristian Lamparter 	switch (urb->status) {
177a84fab3cSChristian Lamparter 	/* everything is fine */
178a84fab3cSChristian Lamparter 	case 0:
179a84fab3cSChristian Lamparter 		carl9170_tx_callback(ar, (void *)urb->context);
180a84fab3cSChristian Lamparter 		break;
181a84fab3cSChristian Lamparter 
182a84fab3cSChristian Lamparter 	/* disconnect */
183a84fab3cSChristian Lamparter 	case -ENOENT:
184a84fab3cSChristian Lamparter 	case -ECONNRESET:
185a84fab3cSChristian Lamparter 	case -ENODEV:
186a84fab3cSChristian Lamparter 	case -ESHUTDOWN:
187a84fab3cSChristian Lamparter 		/*
188a84fab3cSChristian Lamparter 		 * Defer the frame clean-up to the tasklet worker.
189a84fab3cSChristian Lamparter 		 * This is necessary, because carl9170_tx_drop
190a84fab3cSChristian Lamparter 		 * does not work in an irqsave context.
191a84fab3cSChristian Lamparter 		 */
192a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->tx_err);
193a84fab3cSChristian Lamparter 		return;
194a84fab3cSChristian Lamparter 
195a84fab3cSChristian Lamparter 	/* a random transmission error has occurred? */
196a84fab3cSChristian Lamparter 	default:
197a84fab3cSChristian Lamparter 		if (net_ratelimit()) {
198a84fab3cSChristian Lamparter 			dev_err(&ar->udev->dev, "tx failed (%d)\n",
199a84fab3cSChristian Lamparter 				urb->status);
200a84fab3cSChristian Lamparter 		}
201a84fab3cSChristian Lamparter 
202a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->tx_err);
203a84fab3cSChristian Lamparter 		break;
204a84fab3cSChristian Lamparter 	}
205a84fab3cSChristian Lamparter 
206a84fab3cSChristian Lamparter 	if (likely(IS_STARTED(ar)))
207a84fab3cSChristian Lamparter 		carl9170_usb_submit_data_urb(ar);
208a84fab3cSChristian Lamparter }
209a84fab3cSChristian Lamparter 
210a84fab3cSChristian Lamparter static int carl9170_usb_submit_cmd_urb(struct ar9170 *ar)
211a84fab3cSChristian Lamparter {
212a84fab3cSChristian Lamparter 	struct urb *urb;
213a84fab3cSChristian Lamparter 	int err;
214a84fab3cSChristian Lamparter 
215a84fab3cSChristian Lamparter 	if (atomic_inc_return(&ar->tx_cmd_urbs) != 1) {
216a84fab3cSChristian Lamparter 		atomic_dec(&ar->tx_cmd_urbs);
217a84fab3cSChristian Lamparter 		return 0;
218a84fab3cSChristian Lamparter 	}
219a84fab3cSChristian Lamparter 
220a84fab3cSChristian Lamparter 	urb = usb_get_from_anchor(&ar->tx_cmd);
221a84fab3cSChristian Lamparter 	if (!urb) {
222a84fab3cSChristian Lamparter 		atomic_dec(&ar->tx_cmd_urbs);
223a84fab3cSChristian Lamparter 		return 0;
224a84fab3cSChristian Lamparter 	}
225a84fab3cSChristian Lamparter 
226a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->tx_anch);
227a84fab3cSChristian Lamparter 	err = usb_submit_urb(urb, GFP_ATOMIC);
228a84fab3cSChristian Lamparter 	if (unlikely(err)) {
229a84fab3cSChristian Lamparter 		usb_unanchor_urb(urb);
230a84fab3cSChristian Lamparter 		atomic_dec(&ar->tx_cmd_urbs);
231a84fab3cSChristian Lamparter 	}
232a84fab3cSChristian Lamparter 	usb_free_urb(urb);
233a84fab3cSChristian Lamparter 
234a84fab3cSChristian Lamparter 	return err;
235a84fab3cSChristian Lamparter }
236a84fab3cSChristian Lamparter 
237a84fab3cSChristian Lamparter static void carl9170_usb_cmd_complete(struct urb *urb)
238a84fab3cSChristian Lamparter {
239a84fab3cSChristian Lamparter 	struct ar9170 *ar = urb->context;
240a84fab3cSChristian Lamparter 	int err = 0;
241a84fab3cSChristian Lamparter 
242a84fab3cSChristian Lamparter 	if (WARN_ON_ONCE(!ar))
243a84fab3cSChristian Lamparter 		return;
244a84fab3cSChristian Lamparter 
245a84fab3cSChristian Lamparter 	atomic_dec(&ar->tx_cmd_urbs);
246a84fab3cSChristian Lamparter 
247a84fab3cSChristian Lamparter 	switch (urb->status) {
248a84fab3cSChristian Lamparter 	/* everything is fine */
249a84fab3cSChristian Lamparter 	case 0:
250a84fab3cSChristian Lamparter 		break;
251a84fab3cSChristian Lamparter 
252a84fab3cSChristian Lamparter 	/* disconnect */
253a84fab3cSChristian Lamparter 	case -ENOENT:
254a84fab3cSChristian Lamparter 	case -ECONNRESET:
255a84fab3cSChristian Lamparter 	case -ENODEV:
256a84fab3cSChristian Lamparter 	case -ESHUTDOWN:
257a84fab3cSChristian Lamparter 		return;
258a84fab3cSChristian Lamparter 
259a84fab3cSChristian Lamparter 	default:
260a84fab3cSChristian Lamparter 		err = urb->status;
261a84fab3cSChristian Lamparter 		break;
262a84fab3cSChristian Lamparter 	}
263a84fab3cSChristian Lamparter 
264a84fab3cSChristian Lamparter 	if (!IS_INITIALIZED(ar))
265a84fab3cSChristian Lamparter 		return;
266a84fab3cSChristian Lamparter 
267a84fab3cSChristian Lamparter 	if (err)
268a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "submit cmd cb failed (%d).\n", err);
269a84fab3cSChristian Lamparter 
270a84fab3cSChristian Lamparter 	err = carl9170_usb_submit_cmd_urb(ar);
271a84fab3cSChristian Lamparter 	if (err)
272a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "submit cmd failed (%d).\n", err);
273a84fab3cSChristian Lamparter }
274a84fab3cSChristian Lamparter 
275a84fab3cSChristian Lamparter static void carl9170_usb_rx_irq_complete(struct urb *urb)
276a84fab3cSChristian Lamparter {
277a84fab3cSChristian Lamparter 	struct ar9170 *ar = urb->context;
278a84fab3cSChristian Lamparter 
279a84fab3cSChristian Lamparter 	if (WARN_ON_ONCE(!ar))
280a84fab3cSChristian Lamparter 		return;
281a84fab3cSChristian Lamparter 
282a84fab3cSChristian Lamparter 	switch (urb->status) {
283a84fab3cSChristian Lamparter 	/* everything is fine */
284a84fab3cSChristian Lamparter 	case 0:
285a84fab3cSChristian Lamparter 		break;
286a84fab3cSChristian Lamparter 
287a84fab3cSChristian Lamparter 	/* disconnect */
288a84fab3cSChristian Lamparter 	case -ENOENT:
289a84fab3cSChristian Lamparter 	case -ECONNRESET:
290a84fab3cSChristian Lamparter 	case -ENODEV:
291a84fab3cSChristian Lamparter 	case -ESHUTDOWN:
292a84fab3cSChristian Lamparter 		return;
293a84fab3cSChristian Lamparter 
294a84fab3cSChristian Lamparter 	default:
295a84fab3cSChristian Lamparter 		goto resubmit;
296a84fab3cSChristian Lamparter 	}
297a84fab3cSChristian Lamparter 
298*b4764c80SChristian Lamparter 	/*
299*b4764c80SChristian Lamparter 	 * While the carl9170 firmware does not use this EP, the
300*b4764c80SChristian Lamparter 	 * firmware loader in the EEPROM unfortunately does.
301*b4764c80SChristian Lamparter 	 * Therefore we need to be ready to handle out-of-band
302*b4764c80SChristian Lamparter 	 * responses and traps in case the firmware crashed and
303*b4764c80SChristian Lamparter 	 * the loader took over again.
304*b4764c80SChristian Lamparter 	 */
305a84fab3cSChristian Lamparter 	carl9170_handle_command_response(ar, urb->transfer_buffer,
306a84fab3cSChristian Lamparter 					 urb->actual_length);
307a84fab3cSChristian Lamparter 
308a84fab3cSChristian Lamparter resubmit:
309a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->rx_anch);
310a84fab3cSChristian Lamparter 	if (unlikely(usb_submit_urb(urb, GFP_ATOMIC)))
311a84fab3cSChristian Lamparter 		usb_unanchor_urb(urb);
312a84fab3cSChristian Lamparter }
313a84fab3cSChristian Lamparter 
314a84fab3cSChristian Lamparter static int carl9170_usb_submit_rx_urb(struct ar9170 *ar, gfp_t gfp)
315a84fab3cSChristian Lamparter {
316a84fab3cSChristian Lamparter 	struct urb *urb;
317a84fab3cSChristian Lamparter 	int err = 0, runs = 0;
318a84fab3cSChristian Lamparter 
319a84fab3cSChristian Lamparter 	while ((atomic_read(&ar->rx_anch_urbs) < AR9170_NUM_RX_URBS) &&
320a84fab3cSChristian Lamparter 		(runs++ < AR9170_NUM_RX_URBS)) {
321a84fab3cSChristian Lamparter 		err = -ENOSPC;
322a84fab3cSChristian Lamparter 		urb = usb_get_from_anchor(&ar->rx_pool);
323a84fab3cSChristian Lamparter 		if (urb) {
324a84fab3cSChristian Lamparter 			usb_anchor_urb(urb, &ar->rx_anch);
325a84fab3cSChristian Lamparter 			err = usb_submit_urb(urb, gfp);
326a84fab3cSChristian Lamparter 			if (unlikely(err)) {
327a84fab3cSChristian Lamparter 				usb_unanchor_urb(urb);
328a84fab3cSChristian Lamparter 				usb_anchor_urb(urb, &ar->rx_pool);
329a84fab3cSChristian Lamparter 			} else {
330a84fab3cSChristian Lamparter 				atomic_dec(&ar->rx_pool_urbs);
331a84fab3cSChristian Lamparter 				atomic_inc(&ar->rx_anch_urbs);
332a84fab3cSChristian Lamparter 			}
333a84fab3cSChristian Lamparter 			usb_free_urb(urb);
334a84fab3cSChristian Lamparter 		}
335a84fab3cSChristian Lamparter 	}
336a84fab3cSChristian Lamparter 
337a84fab3cSChristian Lamparter 	return err;
338a84fab3cSChristian Lamparter }
339a84fab3cSChristian Lamparter 
340a84fab3cSChristian Lamparter static void carl9170_usb_rx_work(struct ar9170 *ar)
341a84fab3cSChristian Lamparter {
342a84fab3cSChristian Lamparter 	struct urb *urb;
343a84fab3cSChristian Lamparter 	int i;
344a84fab3cSChristian Lamparter 
345a84fab3cSChristian Lamparter 	for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
346a84fab3cSChristian Lamparter 		urb = usb_get_from_anchor(&ar->rx_work);
347a84fab3cSChristian Lamparter 		if (!urb)
348a84fab3cSChristian Lamparter 			break;
349a84fab3cSChristian Lamparter 
350a84fab3cSChristian Lamparter 		atomic_dec(&ar->rx_work_urbs);
351a84fab3cSChristian Lamparter 		if (IS_INITIALIZED(ar)) {
352a84fab3cSChristian Lamparter 			carl9170_rx(ar, urb->transfer_buffer,
353a84fab3cSChristian Lamparter 				    urb->actual_length);
354a84fab3cSChristian Lamparter 		}
355a84fab3cSChristian Lamparter 
356a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->rx_pool);
357a84fab3cSChristian Lamparter 		atomic_inc(&ar->rx_pool_urbs);
358a84fab3cSChristian Lamparter 
359a84fab3cSChristian Lamparter 		usb_free_urb(urb);
360a84fab3cSChristian Lamparter 
361a84fab3cSChristian Lamparter 		carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
362a84fab3cSChristian Lamparter 	}
363a84fab3cSChristian Lamparter }
364a84fab3cSChristian Lamparter 
365a84fab3cSChristian Lamparter void carl9170_usb_handle_tx_err(struct ar9170 *ar)
366a84fab3cSChristian Lamparter {
367a84fab3cSChristian Lamparter 	struct urb *urb;
368a84fab3cSChristian Lamparter 
369a84fab3cSChristian Lamparter 	while ((urb = usb_get_from_anchor(&ar->tx_err))) {
370a84fab3cSChristian Lamparter 		struct sk_buff *skb = (void *)urb->context;
371a84fab3cSChristian Lamparter 
372a84fab3cSChristian Lamparter 		carl9170_tx_drop(ar, skb);
373a84fab3cSChristian Lamparter 		carl9170_tx_callback(ar, skb);
374a84fab3cSChristian Lamparter 		usb_free_urb(urb);
375a84fab3cSChristian Lamparter 	}
376a84fab3cSChristian Lamparter }
377a84fab3cSChristian Lamparter 
378a84fab3cSChristian Lamparter static void carl9170_usb_tasklet(unsigned long data)
379a84fab3cSChristian Lamparter {
380a84fab3cSChristian Lamparter 	struct ar9170 *ar = (struct ar9170 *) data;
381a84fab3cSChristian Lamparter 
382cf6487d0SChristian Lamparter 	if (!IS_INITIALIZED(ar))
383cf6487d0SChristian Lamparter 		return;
384cf6487d0SChristian Lamparter 
385a84fab3cSChristian Lamparter 	carl9170_usb_rx_work(ar);
386a84fab3cSChristian Lamparter 
387a84fab3cSChristian Lamparter 	/*
388a84fab3cSChristian Lamparter 	 * Strictly speaking: The tx scheduler is not part of the USB system.
389a84fab3cSChristian Lamparter 	 * But the rx worker returns frames back to the mac80211-stack and
390a84fab3cSChristian Lamparter 	 * this is the _perfect_ place to generate the next transmissions.
391a84fab3cSChristian Lamparter 	 */
392a84fab3cSChristian Lamparter 	if (IS_STARTED(ar))
393a84fab3cSChristian Lamparter 		carl9170_tx_scheduler(ar);
394a84fab3cSChristian Lamparter }
395a84fab3cSChristian Lamparter 
396a84fab3cSChristian Lamparter static void carl9170_usb_rx_complete(struct urb *urb)
397a84fab3cSChristian Lamparter {
398a84fab3cSChristian Lamparter 	struct ar9170 *ar = (struct ar9170 *)urb->context;
399a84fab3cSChristian Lamparter 	int err;
400a84fab3cSChristian Lamparter 
401a84fab3cSChristian Lamparter 	if (WARN_ON_ONCE(!ar))
402a84fab3cSChristian Lamparter 		return;
403a84fab3cSChristian Lamparter 
404a84fab3cSChristian Lamparter 	atomic_dec(&ar->rx_anch_urbs);
405a84fab3cSChristian Lamparter 
406a84fab3cSChristian Lamparter 	switch (urb->status) {
407a84fab3cSChristian Lamparter 	case 0:
408a84fab3cSChristian Lamparter 		/* rx path */
409a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->rx_work);
410a84fab3cSChristian Lamparter 		atomic_inc(&ar->rx_work_urbs);
411a84fab3cSChristian Lamparter 		break;
412a84fab3cSChristian Lamparter 
413a84fab3cSChristian Lamparter 	case -ENOENT:
414a84fab3cSChristian Lamparter 	case -ECONNRESET:
415a84fab3cSChristian Lamparter 	case -ENODEV:
416a84fab3cSChristian Lamparter 	case -ESHUTDOWN:
417a84fab3cSChristian Lamparter 		/* handle disconnect events*/
418a84fab3cSChristian Lamparter 		return;
419a84fab3cSChristian Lamparter 
420a84fab3cSChristian Lamparter 	default:
421a84fab3cSChristian Lamparter 		/* handle all other errors */
422a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->rx_pool);
423a84fab3cSChristian Lamparter 		atomic_inc(&ar->rx_pool_urbs);
424a84fab3cSChristian Lamparter 		break;
425a84fab3cSChristian Lamparter 	}
426a84fab3cSChristian Lamparter 
427a84fab3cSChristian Lamparter 	err = carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
428a84fab3cSChristian Lamparter 	if (unlikely(err)) {
429a84fab3cSChristian Lamparter 		/*
430a84fab3cSChristian Lamparter 		 * usb_submit_rx_urb reported a problem.
431a84fab3cSChristian Lamparter 		 * In case this is due to a rx buffer shortage,
432a84fab3cSChristian Lamparter 		 * elevate the tasklet worker priority to
433a84fab3cSChristian Lamparter 		 * the highest available level.
434a84fab3cSChristian Lamparter 		 */
435a84fab3cSChristian Lamparter 		tasklet_hi_schedule(&ar->usb_tasklet);
436a84fab3cSChristian Lamparter 
437a84fab3cSChristian Lamparter 		if (atomic_read(&ar->rx_anch_urbs) == 0) {
438a84fab3cSChristian Lamparter 			/*
439a84fab3cSChristian Lamparter 			 * The system is too slow to cope with
440a84fab3cSChristian Lamparter 			 * the enormous workload. We have simply
441a84fab3cSChristian Lamparter 			 * run out of active rx urbs and this
44225985edcSLucas De Marchi 			 * unfortunately leads to an unpredictable
443a84fab3cSChristian Lamparter 			 * device.
444a84fab3cSChristian Lamparter 			 */
445a84fab3cSChristian Lamparter 
446e4a668c5SChristian Lamparter 			ieee80211_queue_work(ar->hw, &ar->ping_work);
447a84fab3cSChristian Lamparter 		}
448a84fab3cSChristian Lamparter 	} else {
449a84fab3cSChristian Lamparter 		/*
450a84fab3cSChristian Lamparter 		 * Using anything less than _high_ priority absolutely
451a84fab3cSChristian Lamparter 		 * kills the rx performance my UP-System...
452a84fab3cSChristian Lamparter 		 */
453a84fab3cSChristian Lamparter 		tasklet_hi_schedule(&ar->usb_tasklet);
454a84fab3cSChristian Lamparter 	}
455a84fab3cSChristian Lamparter }
456a84fab3cSChristian Lamparter 
457a84fab3cSChristian Lamparter static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp)
458a84fab3cSChristian Lamparter {
459a84fab3cSChristian Lamparter 	struct urb *urb;
460a84fab3cSChristian Lamparter 	void *buf;
461a84fab3cSChristian Lamparter 
462a84fab3cSChristian Lamparter 	buf = kmalloc(ar->fw.rx_size, gfp);
463a84fab3cSChristian Lamparter 	if (!buf)
464a84fab3cSChristian Lamparter 		return NULL;
465a84fab3cSChristian Lamparter 
466a84fab3cSChristian Lamparter 	urb = usb_alloc_urb(0, gfp);
467a84fab3cSChristian Lamparter 	if (!urb) {
468a84fab3cSChristian Lamparter 		kfree(buf);
469a84fab3cSChristian Lamparter 		return NULL;
470a84fab3cSChristian Lamparter 	}
471a84fab3cSChristian Lamparter 
472a84fab3cSChristian Lamparter 	usb_fill_bulk_urb(urb, ar->udev, usb_rcvbulkpipe(ar->udev,
473a84fab3cSChristian Lamparter 			  AR9170_USB_EP_RX), buf, ar->fw.rx_size,
474a84fab3cSChristian Lamparter 			  carl9170_usb_rx_complete, ar);
475a84fab3cSChristian Lamparter 
476a84fab3cSChristian Lamparter 	urb->transfer_flags |= URB_FREE_BUFFER;
477a84fab3cSChristian Lamparter 
478a84fab3cSChristian Lamparter 	return urb;
479a84fab3cSChristian Lamparter }
480a84fab3cSChristian Lamparter 
481a84fab3cSChristian Lamparter static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar)
482a84fab3cSChristian Lamparter {
483a84fab3cSChristian Lamparter 	struct urb *urb = NULL;
484a84fab3cSChristian Lamparter 	void *ibuf;
485a84fab3cSChristian Lamparter 	int err = -ENOMEM;
486a84fab3cSChristian Lamparter 
487a84fab3cSChristian Lamparter 	urb = usb_alloc_urb(0, GFP_KERNEL);
488a84fab3cSChristian Lamparter 	if (!urb)
489a84fab3cSChristian Lamparter 		goto out;
490a84fab3cSChristian Lamparter 
491a84fab3cSChristian Lamparter 	ibuf = kmalloc(AR9170_USB_EP_CTRL_MAX, GFP_KERNEL);
492a84fab3cSChristian Lamparter 	if (!ibuf)
493a84fab3cSChristian Lamparter 		goto out;
494a84fab3cSChristian Lamparter 
495a84fab3cSChristian Lamparter 	usb_fill_int_urb(urb, ar->udev, usb_rcvintpipe(ar->udev,
496a84fab3cSChristian Lamparter 			 AR9170_USB_EP_IRQ), ibuf, AR9170_USB_EP_CTRL_MAX,
497a84fab3cSChristian Lamparter 			 carl9170_usb_rx_irq_complete, ar, 1);
498a84fab3cSChristian Lamparter 
499a84fab3cSChristian Lamparter 	urb->transfer_flags |= URB_FREE_BUFFER;
500a84fab3cSChristian Lamparter 
501a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->rx_anch);
502a84fab3cSChristian Lamparter 	err = usb_submit_urb(urb, GFP_KERNEL);
503a84fab3cSChristian Lamparter 	if (err)
504a84fab3cSChristian Lamparter 		usb_unanchor_urb(urb);
505a84fab3cSChristian Lamparter 
506a84fab3cSChristian Lamparter out:
507a84fab3cSChristian Lamparter 	usb_free_urb(urb);
508a84fab3cSChristian Lamparter 	return err;
509a84fab3cSChristian Lamparter }
510a84fab3cSChristian Lamparter 
511a84fab3cSChristian Lamparter static int carl9170_usb_init_rx_bulk_urbs(struct ar9170 *ar)
512a84fab3cSChristian Lamparter {
513a84fab3cSChristian Lamparter 	struct urb *urb;
514a84fab3cSChristian Lamparter 	int i, err = -EINVAL;
515a84fab3cSChristian Lamparter 
516a84fab3cSChristian Lamparter 	/*
517a84fab3cSChristian Lamparter 	 * The driver actively maintains a second shadow
518a84fab3cSChristian Lamparter 	 * pool for inactive, but fully-prepared rx urbs.
519a84fab3cSChristian Lamparter 	 *
520a84fab3cSChristian Lamparter 	 * The pool should help the driver to master huge
521a84fab3cSChristian Lamparter 	 * workload spikes without running the risk of
522a84fab3cSChristian Lamparter 	 * undersupplying the hardware or wasting time by
523a84fab3cSChristian Lamparter 	 * processing rx data (streams) inside the urb
524a84fab3cSChristian Lamparter 	 * completion (hardirq context).
525a84fab3cSChristian Lamparter 	 */
526a84fab3cSChristian Lamparter 	for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
527a84fab3cSChristian Lamparter 		urb = carl9170_usb_alloc_rx_urb(ar, GFP_KERNEL);
528a84fab3cSChristian Lamparter 		if (!urb) {
529a84fab3cSChristian Lamparter 			err = -ENOMEM;
530a84fab3cSChristian Lamparter 			goto err_out;
531a84fab3cSChristian Lamparter 		}
532a84fab3cSChristian Lamparter 
533a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->rx_pool);
534a84fab3cSChristian Lamparter 		atomic_inc(&ar->rx_pool_urbs);
535a84fab3cSChristian Lamparter 		usb_free_urb(urb);
536a84fab3cSChristian Lamparter 	}
537a84fab3cSChristian Lamparter 
538a84fab3cSChristian Lamparter 	err = carl9170_usb_submit_rx_urb(ar, GFP_KERNEL);
539a84fab3cSChristian Lamparter 	if (err)
540a84fab3cSChristian Lamparter 		goto err_out;
541a84fab3cSChristian Lamparter 
542a84fab3cSChristian Lamparter 	/* the device now waiting for the firmware. */
543a84fab3cSChristian Lamparter 	carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
544a84fab3cSChristian Lamparter 	return 0;
545a84fab3cSChristian Lamparter 
546a84fab3cSChristian Lamparter err_out:
547a84fab3cSChristian Lamparter 
548a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->rx_pool);
549a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->rx_work);
550a84fab3cSChristian Lamparter 	usb_kill_anchored_urbs(&ar->rx_anch);
551a84fab3cSChristian Lamparter 	return err;
552a84fab3cSChristian Lamparter }
553a84fab3cSChristian Lamparter 
554a84fab3cSChristian Lamparter static int carl9170_usb_flush(struct ar9170 *ar)
555a84fab3cSChristian Lamparter {
556a84fab3cSChristian Lamparter 	struct urb *urb;
557a84fab3cSChristian Lamparter 	int ret, err = 0;
558a84fab3cSChristian Lamparter 
559a84fab3cSChristian Lamparter 	while ((urb = usb_get_from_anchor(&ar->tx_wait))) {
560a84fab3cSChristian Lamparter 		struct sk_buff *skb = (void *)urb->context;
561a84fab3cSChristian Lamparter 		carl9170_tx_drop(ar, skb);
562a84fab3cSChristian Lamparter 		carl9170_tx_callback(ar, skb);
563a84fab3cSChristian Lamparter 		usb_free_urb(urb);
564a84fab3cSChristian Lamparter 	}
565a84fab3cSChristian Lamparter 
566dfa31fefSChristian Lamparter 	ret = usb_wait_anchor_empty_timeout(&ar->tx_cmd, 1000);
567a84fab3cSChristian Lamparter 	if (ret == 0)
568a84fab3cSChristian Lamparter 		err = -ETIMEDOUT;
569a84fab3cSChristian Lamparter 
570a84fab3cSChristian Lamparter 	/* lets wait a while until the tx - queues are dried out */
571dfa31fefSChristian Lamparter 	ret = usb_wait_anchor_empty_timeout(&ar->tx_anch, 1000);
572a84fab3cSChristian Lamparter 	if (ret == 0)
573a84fab3cSChristian Lamparter 		err = -ETIMEDOUT;
574a84fab3cSChristian Lamparter 
575a84fab3cSChristian Lamparter 	usb_kill_anchored_urbs(&ar->tx_anch);
576a84fab3cSChristian Lamparter 	carl9170_usb_handle_tx_err(ar);
577a84fab3cSChristian Lamparter 
578a84fab3cSChristian Lamparter 	return err;
579a84fab3cSChristian Lamparter }
580a84fab3cSChristian Lamparter 
581a84fab3cSChristian Lamparter static void carl9170_usb_cancel_urbs(struct ar9170 *ar)
582a84fab3cSChristian Lamparter {
583a84fab3cSChristian Lamparter 	int err;
584a84fab3cSChristian Lamparter 
585a84fab3cSChristian Lamparter 	carl9170_set_state(ar, CARL9170_UNKNOWN_STATE);
586a84fab3cSChristian Lamparter 
587a84fab3cSChristian Lamparter 	err = carl9170_usb_flush(ar);
588a84fab3cSChristian Lamparter 	if (err)
589a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "stuck tx urbs!\n");
590a84fab3cSChristian Lamparter 
591a84fab3cSChristian Lamparter 	usb_poison_anchored_urbs(&ar->tx_anch);
592a84fab3cSChristian Lamparter 	carl9170_usb_handle_tx_err(ar);
593a84fab3cSChristian Lamparter 	usb_poison_anchored_urbs(&ar->rx_anch);
594a84fab3cSChristian Lamparter 
595a84fab3cSChristian Lamparter 	tasklet_kill(&ar->usb_tasklet);
596a84fab3cSChristian Lamparter 
597a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->rx_work);
598a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->rx_pool);
599a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->tx_cmd);
600a84fab3cSChristian Lamparter }
601a84fab3cSChristian Lamparter 
602a84fab3cSChristian Lamparter int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd,
603a84fab3cSChristian Lamparter 			const bool free_buf)
604a84fab3cSChristian Lamparter {
605a84fab3cSChristian Lamparter 	struct urb *urb;
606cae7f953SChristian Lamparter 	int err = 0;
607a84fab3cSChristian Lamparter 
608cae7f953SChristian Lamparter 	if (!IS_INITIALIZED(ar)) {
609cae7f953SChristian Lamparter 		err = -EPERM;
610cae7f953SChristian Lamparter 		goto err_free;
611cae7f953SChristian Lamparter 	}
612a84fab3cSChristian Lamparter 
613cae7f953SChristian Lamparter 	if (WARN_ON(cmd->hdr.len > CARL9170_MAX_CMD_LEN - 4)) {
614cae7f953SChristian Lamparter 		err = -EINVAL;
615cae7f953SChristian Lamparter 		goto err_free;
616cae7f953SChristian Lamparter 	}
617a84fab3cSChristian Lamparter 
618a84fab3cSChristian Lamparter 	urb = usb_alloc_urb(0, GFP_ATOMIC);
619cae7f953SChristian Lamparter 	if (!urb) {
620cae7f953SChristian Lamparter 		err = -ENOMEM;
621cae7f953SChristian Lamparter 		goto err_free;
622cae7f953SChristian Lamparter 	}
623a84fab3cSChristian Lamparter 
624a84fab3cSChristian Lamparter 	usb_fill_int_urb(urb, ar->udev, usb_sndintpipe(ar->udev,
625a84fab3cSChristian Lamparter 		AR9170_USB_EP_CMD), cmd, cmd->hdr.len + 4,
626a84fab3cSChristian Lamparter 		carl9170_usb_cmd_complete, ar, 1);
627a84fab3cSChristian Lamparter 
628a84fab3cSChristian Lamparter 	if (free_buf)
629a84fab3cSChristian Lamparter 		urb->transfer_flags |= URB_FREE_BUFFER;
630a84fab3cSChristian Lamparter 
631a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->tx_cmd);
632a84fab3cSChristian Lamparter 	usb_free_urb(urb);
633a84fab3cSChristian Lamparter 
634a84fab3cSChristian Lamparter 	return carl9170_usb_submit_cmd_urb(ar);
635cae7f953SChristian Lamparter 
636cae7f953SChristian Lamparter err_free:
637cae7f953SChristian Lamparter 	if (free_buf)
638cae7f953SChristian Lamparter 		kfree(cmd);
639cae7f953SChristian Lamparter 
640cae7f953SChristian Lamparter 	return err;
641a84fab3cSChristian Lamparter }
642a84fab3cSChristian Lamparter 
643a84fab3cSChristian Lamparter int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd,
644a84fab3cSChristian Lamparter 	unsigned int plen, void *payload, unsigned int outlen, void *out)
645a84fab3cSChristian Lamparter {
646a84fab3cSChristian Lamparter 	int err = -ENOMEM;
647a84fab3cSChristian Lamparter 
648a84fab3cSChristian Lamparter 	if (!IS_ACCEPTING_CMD(ar))
649a84fab3cSChristian Lamparter 		return -EIO;
650a84fab3cSChristian Lamparter 
651a84fab3cSChristian Lamparter 	if (!(cmd & CARL9170_CMD_ASYNC_FLAG))
652a84fab3cSChristian Lamparter 		might_sleep();
653a84fab3cSChristian Lamparter 
654a84fab3cSChristian Lamparter 	ar->cmd.hdr.len = plen;
655a84fab3cSChristian Lamparter 	ar->cmd.hdr.cmd = cmd;
656a84fab3cSChristian Lamparter 	/* writing multiple regs fills this buffer already */
657a84fab3cSChristian Lamparter 	if (plen && payload != (u8 *)(ar->cmd.data))
658a84fab3cSChristian Lamparter 		memcpy(ar->cmd.data, payload, plen);
659a84fab3cSChristian Lamparter 
660a84fab3cSChristian Lamparter 	spin_lock_bh(&ar->cmd_lock);
661a84fab3cSChristian Lamparter 	ar->readbuf = (u8 *)out;
662a84fab3cSChristian Lamparter 	ar->readlen = outlen;
663a84fab3cSChristian Lamparter 	spin_unlock_bh(&ar->cmd_lock);
664a84fab3cSChristian Lamparter 
665a84fab3cSChristian Lamparter 	err = __carl9170_exec_cmd(ar, &ar->cmd, false);
666a84fab3cSChristian Lamparter 
667a84fab3cSChristian Lamparter 	if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) {
668a84fab3cSChristian Lamparter 		err = wait_for_completion_timeout(&ar->cmd_wait, HZ);
669a84fab3cSChristian Lamparter 		if (err == 0) {
670a84fab3cSChristian Lamparter 			err = -ETIMEDOUT;
671a84fab3cSChristian Lamparter 			goto err_unbuf;
672a84fab3cSChristian Lamparter 		}
673a84fab3cSChristian Lamparter 
674a84fab3cSChristian Lamparter 		if (ar->readlen != outlen) {
675a84fab3cSChristian Lamparter 			err = -EMSGSIZE;
676a84fab3cSChristian Lamparter 			goto err_unbuf;
677a84fab3cSChristian Lamparter 		}
678a84fab3cSChristian Lamparter 	}
679a84fab3cSChristian Lamparter 
680a84fab3cSChristian Lamparter 	return 0;
681a84fab3cSChristian Lamparter 
682a84fab3cSChristian Lamparter err_unbuf:
683a84fab3cSChristian Lamparter 	/* Maybe the device was removed in the moment we were waiting? */
684a84fab3cSChristian Lamparter 	if (IS_STARTED(ar)) {
685a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "no command feedback "
686a84fab3cSChristian Lamparter 			"received (%d).\n", err);
687a84fab3cSChristian Lamparter 
688a84fab3cSChristian Lamparter 		/* provide some maybe useful debug information */
689a84fab3cSChristian Lamparter 		print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE,
690a84fab3cSChristian Lamparter 				     &ar->cmd, plen + 4);
691a84fab3cSChristian Lamparter 
692a84fab3cSChristian Lamparter 		carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT);
693a84fab3cSChristian Lamparter 	}
694a84fab3cSChristian Lamparter 
695a84fab3cSChristian Lamparter 	/* invalidate to avoid completing the next command prematurely */
696a84fab3cSChristian Lamparter 	spin_lock_bh(&ar->cmd_lock);
697a84fab3cSChristian Lamparter 	ar->readbuf = NULL;
698a84fab3cSChristian Lamparter 	ar->readlen = 0;
699a84fab3cSChristian Lamparter 	spin_unlock_bh(&ar->cmd_lock);
700a84fab3cSChristian Lamparter 
701a84fab3cSChristian Lamparter 	return err;
702a84fab3cSChristian Lamparter }
703a84fab3cSChristian Lamparter 
704a84fab3cSChristian Lamparter void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
705a84fab3cSChristian Lamparter {
706a84fab3cSChristian Lamparter 	struct urb *urb;
707a84fab3cSChristian Lamparter 	struct ar9170_stream *tx_stream;
708a84fab3cSChristian Lamparter 	void *data;
709a84fab3cSChristian Lamparter 	unsigned int len;
710a84fab3cSChristian Lamparter 
711a84fab3cSChristian Lamparter 	if (!IS_STARTED(ar))
712a84fab3cSChristian Lamparter 		goto err_drop;
713a84fab3cSChristian Lamparter 
714a84fab3cSChristian Lamparter 	urb = usb_alloc_urb(0, GFP_ATOMIC);
715a84fab3cSChristian Lamparter 	if (!urb)
716a84fab3cSChristian Lamparter 		goto err_drop;
717a84fab3cSChristian Lamparter 
718a84fab3cSChristian Lamparter 	if (ar->fw.tx_stream) {
719a84fab3cSChristian Lamparter 		tx_stream = (void *) (skb->data - sizeof(*tx_stream));
720a84fab3cSChristian Lamparter 
721a84fab3cSChristian Lamparter 		len = skb->len + sizeof(*tx_stream);
722a84fab3cSChristian Lamparter 		tx_stream->length = cpu_to_le16(len);
723a84fab3cSChristian Lamparter 		tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG);
724a84fab3cSChristian Lamparter 		data = tx_stream;
725a84fab3cSChristian Lamparter 	} else {
726a84fab3cSChristian Lamparter 		data = skb->data;
727a84fab3cSChristian Lamparter 		len = skb->len;
728a84fab3cSChristian Lamparter 	}
729a84fab3cSChristian Lamparter 
730a84fab3cSChristian Lamparter 	usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev,
731a84fab3cSChristian Lamparter 		AR9170_USB_EP_TX), data, len,
732a84fab3cSChristian Lamparter 		carl9170_usb_tx_data_complete, skb);
733a84fab3cSChristian Lamparter 
734a84fab3cSChristian Lamparter 	urb->transfer_flags |= URB_ZERO_PACKET;
735a84fab3cSChristian Lamparter 
736a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->tx_wait);
737a84fab3cSChristian Lamparter 
738a84fab3cSChristian Lamparter 	usb_free_urb(urb);
739a84fab3cSChristian Lamparter 
740a84fab3cSChristian Lamparter 	carl9170_usb_submit_data_urb(ar);
741a84fab3cSChristian Lamparter 	return;
742a84fab3cSChristian Lamparter 
743a84fab3cSChristian Lamparter err_drop:
744a84fab3cSChristian Lamparter 	carl9170_tx_drop(ar, skb);
745a84fab3cSChristian Lamparter 	carl9170_tx_callback(ar, skb);
746a84fab3cSChristian Lamparter }
747a84fab3cSChristian Lamparter 
748a84fab3cSChristian Lamparter static void carl9170_release_firmware(struct ar9170 *ar)
749a84fab3cSChristian Lamparter {
750a84fab3cSChristian Lamparter 	if (ar->fw.fw) {
751a84fab3cSChristian Lamparter 		release_firmware(ar->fw.fw);
752a84fab3cSChristian Lamparter 		memset(&ar->fw, 0, sizeof(ar->fw));
753a84fab3cSChristian Lamparter 	}
754a84fab3cSChristian Lamparter }
755a84fab3cSChristian Lamparter 
756a84fab3cSChristian Lamparter void carl9170_usb_stop(struct ar9170 *ar)
757a84fab3cSChristian Lamparter {
758a84fab3cSChristian Lamparter 	int ret;
759a84fab3cSChristian Lamparter 
760a84fab3cSChristian Lamparter 	carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED);
761a84fab3cSChristian Lamparter 
762a84fab3cSChristian Lamparter 	ret = carl9170_usb_flush(ar);
763a84fab3cSChristian Lamparter 	if (ret)
764a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "kill pending tx urbs.\n");
765a84fab3cSChristian Lamparter 
766a84fab3cSChristian Lamparter 	usb_poison_anchored_urbs(&ar->tx_anch);
767a84fab3cSChristian Lamparter 	carl9170_usb_handle_tx_err(ar);
768a84fab3cSChristian Lamparter 
769a84fab3cSChristian Lamparter 	/* kill any pending command */
770a84fab3cSChristian Lamparter 	spin_lock_bh(&ar->cmd_lock);
771a84fab3cSChristian Lamparter 	ar->readlen = 0;
772a84fab3cSChristian Lamparter 	spin_unlock_bh(&ar->cmd_lock);
773a84fab3cSChristian Lamparter 	complete_all(&ar->cmd_wait);
774a84fab3cSChristian Lamparter 
775a84fab3cSChristian Lamparter 	/* This is required to prevent an early completion on _start */
776a84fab3cSChristian Lamparter 	INIT_COMPLETION(ar->cmd_wait);
777a84fab3cSChristian Lamparter 
778a84fab3cSChristian Lamparter 	/*
779a84fab3cSChristian Lamparter 	 * Note:
780a84fab3cSChristian Lamparter 	 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
781a84fab3cSChristian Lamparter 	 * Else we would end up with a unresponsive device...
782a84fab3cSChristian Lamparter 	 */
783a84fab3cSChristian Lamparter }
784a84fab3cSChristian Lamparter 
785a84fab3cSChristian Lamparter int carl9170_usb_open(struct ar9170 *ar)
786a84fab3cSChristian Lamparter {
787a84fab3cSChristian Lamparter 	usb_unpoison_anchored_urbs(&ar->tx_anch);
788a84fab3cSChristian Lamparter 
789a84fab3cSChristian Lamparter 	carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
790a84fab3cSChristian Lamparter 	return 0;
791a84fab3cSChristian Lamparter }
792a84fab3cSChristian Lamparter 
793a84fab3cSChristian Lamparter static int carl9170_usb_load_firmware(struct ar9170 *ar)
794a84fab3cSChristian Lamparter {
795a84fab3cSChristian Lamparter 	const u8 *data;
796a84fab3cSChristian Lamparter 	u8 *buf;
797a84fab3cSChristian Lamparter 	unsigned int transfer;
798a84fab3cSChristian Lamparter 	size_t len;
799a84fab3cSChristian Lamparter 	u32 addr;
800a84fab3cSChristian Lamparter 	int err = 0;
801a84fab3cSChristian Lamparter 
802a84fab3cSChristian Lamparter 	buf = kmalloc(4096, GFP_KERNEL);
803a84fab3cSChristian Lamparter 	if (!buf) {
804a84fab3cSChristian Lamparter 		err = -ENOMEM;
805a84fab3cSChristian Lamparter 		goto err_out;
806a84fab3cSChristian Lamparter 	}
807a84fab3cSChristian Lamparter 
808a84fab3cSChristian Lamparter 	data = ar->fw.fw->data;
809a84fab3cSChristian Lamparter 	len = ar->fw.fw->size;
810a84fab3cSChristian Lamparter 	addr = ar->fw.address;
811a84fab3cSChristian Lamparter 
812a84fab3cSChristian Lamparter 	/* this removes the miniboot image */
813a84fab3cSChristian Lamparter 	data += ar->fw.offset;
814a84fab3cSChristian Lamparter 	len -= ar->fw.offset;
815a84fab3cSChristian Lamparter 
816a84fab3cSChristian Lamparter 	while (len) {
817a84fab3cSChristian Lamparter 		transfer = min_t(unsigned int, len, 4096u);
818a84fab3cSChristian Lamparter 		memcpy(buf, data, transfer);
819a84fab3cSChristian Lamparter 
820a84fab3cSChristian Lamparter 		err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
821a84fab3cSChristian Lamparter 				      0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
822a84fab3cSChristian Lamparter 				      addr >> 8, 0, buf, transfer, 100);
823a84fab3cSChristian Lamparter 
824a84fab3cSChristian Lamparter 		if (err < 0) {
825a84fab3cSChristian Lamparter 			kfree(buf);
826a84fab3cSChristian Lamparter 			goto err_out;
827a84fab3cSChristian Lamparter 		}
828a84fab3cSChristian Lamparter 
829a84fab3cSChristian Lamparter 		len -= transfer;
830a84fab3cSChristian Lamparter 		data += transfer;
831a84fab3cSChristian Lamparter 		addr += transfer;
832a84fab3cSChristian Lamparter 	}
833a84fab3cSChristian Lamparter 	kfree(buf);
834a84fab3cSChristian Lamparter 
835a84fab3cSChristian Lamparter 	err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
836a84fab3cSChristian Lamparter 			      0x31 /* FW DL COMPLETE */,
837a84fab3cSChristian Lamparter 			      0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 200);
838a84fab3cSChristian Lamparter 
839a84fab3cSChristian Lamparter 	if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) {
840a84fab3cSChristian Lamparter 		err = -ETIMEDOUT;
841a84fab3cSChristian Lamparter 		goto err_out;
842a84fab3cSChristian Lamparter 	}
843a84fab3cSChristian Lamparter 
844a84fab3cSChristian Lamparter 	err = carl9170_echo_test(ar, 0x4a110123);
845a84fab3cSChristian Lamparter 	if (err)
846a84fab3cSChristian Lamparter 		goto err_out;
847a84fab3cSChristian Lamparter 
84897e2c402SChristian Lamparter 	/* now, start the command response counter */
849a84fab3cSChristian Lamparter 	ar->cmd_seq = -1;
850a84fab3cSChristian Lamparter 
851a84fab3cSChristian Lamparter 	return 0;
852a84fab3cSChristian Lamparter 
853a84fab3cSChristian Lamparter err_out:
854a84fab3cSChristian Lamparter 	dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err);
855a84fab3cSChristian Lamparter 	return err;
856a84fab3cSChristian Lamparter }
857a84fab3cSChristian Lamparter 
858a84fab3cSChristian Lamparter int carl9170_usb_restart(struct ar9170 *ar)
859a84fab3cSChristian Lamparter {
860a84fab3cSChristian Lamparter 	int err = 0;
861a84fab3cSChristian Lamparter 
862a84fab3cSChristian Lamparter 	if (ar->intf->condition != USB_INTERFACE_BOUND)
863a84fab3cSChristian Lamparter 		return 0;
864a84fab3cSChristian Lamparter 
86597e2c402SChristian Lamparter 	/*
86697e2c402SChristian Lamparter 	 * Disable the command response sequence counter check.
86797e2c402SChristian Lamparter 	 * We already know that the device/firmware is in a bad state.
86897e2c402SChristian Lamparter 	 * So, no extra points are awarded to anyone who reminds the
86997e2c402SChristian Lamparter 	 * driver about that.
87097e2c402SChristian Lamparter 	 */
871a84fab3cSChristian Lamparter 	ar->cmd_seq = -2;
872a84fab3cSChristian Lamparter 
873a84fab3cSChristian Lamparter 	err = carl9170_reboot(ar);
874a84fab3cSChristian Lamparter 
875a84fab3cSChristian Lamparter 	carl9170_usb_stop(ar);
876a84fab3cSChristian Lamparter 
877a84fab3cSChristian Lamparter 	if (err)
878a84fab3cSChristian Lamparter 		goto err_out;
879a84fab3cSChristian Lamparter 
880a84fab3cSChristian Lamparter 	tasklet_schedule(&ar->usb_tasklet);
881a84fab3cSChristian Lamparter 
882a84fab3cSChristian Lamparter 	/* The reboot procedure can take quite a while to complete. */
883a84fab3cSChristian Lamparter 	msleep(1100);
884a84fab3cSChristian Lamparter 
885a84fab3cSChristian Lamparter 	err = carl9170_usb_open(ar);
886a84fab3cSChristian Lamparter 	if (err)
887a84fab3cSChristian Lamparter 		goto err_out;
888a84fab3cSChristian Lamparter 
889a84fab3cSChristian Lamparter 	err = carl9170_usb_load_firmware(ar);
890a84fab3cSChristian Lamparter 	if (err)
891a84fab3cSChristian Lamparter 		goto err_out;
892a84fab3cSChristian Lamparter 
893a84fab3cSChristian Lamparter 	return 0;
894a84fab3cSChristian Lamparter 
895a84fab3cSChristian Lamparter err_out:
896a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
897a84fab3cSChristian Lamparter 	return err;
898a84fab3cSChristian Lamparter }
899a84fab3cSChristian Lamparter 
900a84fab3cSChristian Lamparter void carl9170_usb_reset(struct ar9170 *ar)
901a84fab3cSChristian Lamparter {
902a84fab3cSChristian Lamparter 	/*
903a84fab3cSChristian Lamparter 	 * This is the last resort to get the device going again
904a84fab3cSChristian Lamparter 	 * without any *user replugging action*.
905a84fab3cSChristian Lamparter 	 *
906a84fab3cSChristian Lamparter 	 * But there is a catch: usb_reset really is like a physical
907a84fab3cSChristian Lamparter 	 * *reconnect*. The mac80211 state will be lost in the process.
908a84fab3cSChristian Lamparter 	 * Therefore a userspace application, which is monitoring
909a84fab3cSChristian Lamparter 	 * the link must step in.
910a84fab3cSChristian Lamparter 	 */
911a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
912a84fab3cSChristian Lamparter 
913a84fab3cSChristian Lamparter 	carl9170_usb_stop(ar);
914a84fab3cSChristian Lamparter 
915a84fab3cSChristian Lamparter 	usb_queue_reset_device(ar->intf);
916a84fab3cSChristian Lamparter }
917a84fab3cSChristian Lamparter 
918a84fab3cSChristian Lamparter static int carl9170_usb_init_device(struct ar9170 *ar)
919a84fab3cSChristian Lamparter {
920a84fab3cSChristian Lamparter 	int err;
921a84fab3cSChristian Lamparter 
92297e2c402SChristian Lamparter 	/*
92397e2c402SChristian Lamparter 	 * The carl9170 firmware let's the driver know when it's
92497e2c402SChristian Lamparter 	 * ready for action. But we have to be prepared to gracefully
92597e2c402SChristian Lamparter 	 * handle all spurious [flushed] messages after each (re-)boot.
92697e2c402SChristian Lamparter 	 * Thus the command response counter remains disabled until it
92797e2c402SChristian Lamparter 	 * can be safely synchronized.
92897e2c402SChristian Lamparter 	 */
92997e2c402SChristian Lamparter 	ar->cmd_seq = -2;
93097e2c402SChristian Lamparter 
931a84fab3cSChristian Lamparter 	err = carl9170_usb_send_rx_irq_urb(ar);
932a84fab3cSChristian Lamparter 	if (err)
933a84fab3cSChristian Lamparter 		goto err_out;
934a84fab3cSChristian Lamparter 
935a84fab3cSChristian Lamparter 	err = carl9170_usb_init_rx_bulk_urbs(ar);
936a84fab3cSChristian Lamparter 	if (err)
937a84fab3cSChristian Lamparter 		goto err_unrx;
938a84fab3cSChristian Lamparter 
93997e2c402SChristian Lamparter 	err = carl9170_usb_open(ar);
94097e2c402SChristian Lamparter 	if (err)
94197e2c402SChristian Lamparter 		goto err_unrx;
94297e2c402SChristian Lamparter 
943a84fab3cSChristian Lamparter 	mutex_lock(&ar->mutex);
944a84fab3cSChristian Lamparter 	err = carl9170_usb_load_firmware(ar);
945a84fab3cSChristian Lamparter 	mutex_unlock(&ar->mutex);
946a84fab3cSChristian Lamparter 	if (err)
94797e2c402SChristian Lamparter 		goto err_stop;
948a84fab3cSChristian Lamparter 
949a84fab3cSChristian Lamparter 	return 0;
950a84fab3cSChristian Lamparter 
95197e2c402SChristian Lamparter err_stop:
95297e2c402SChristian Lamparter 	carl9170_usb_stop(ar);
95397e2c402SChristian Lamparter 
954a84fab3cSChristian Lamparter err_unrx:
955a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
956a84fab3cSChristian Lamparter 
957a84fab3cSChristian Lamparter err_out:
958a84fab3cSChristian Lamparter 	return err;
959a84fab3cSChristian Lamparter }
960a84fab3cSChristian Lamparter 
961a84fab3cSChristian Lamparter static void carl9170_usb_firmware_failed(struct ar9170 *ar)
962a84fab3cSChristian Lamparter {
963a84fab3cSChristian Lamparter 	struct device *parent = ar->udev->dev.parent;
964a84fab3cSChristian Lamparter 	struct usb_device *udev;
965a84fab3cSChristian Lamparter 
966a84fab3cSChristian Lamparter 	/*
967a84fab3cSChristian Lamparter 	 * Store a copy of the usb_device pointer locally.
968a84fab3cSChristian Lamparter 	 * This is because device_release_driver initiates
969a84fab3cSChristian Lamparter 	 * carl9170_usb_disconnect, which in turn frees our
970a84fab3cSChristian Lamparter 	 * driver context (ar).
971a84fab3cSChristian Lamparter 	 */
972a84fab3cSChristian Lamparter 	udev = ar->udev;
973a84fab3cSChristian Lamparter 
974a84fab3cSChristian Lamparter 	complete(&ar->fw_load_wait);
975a84fab3cSChristian Lamparter 
976a84fab3cSChristian Lamparter 	/* unbind anything failed */
977a84fab3cSChristian Lamparter 	if (parent)
978a84fab3cSChristian Lamparter 		device_lock(parent);
979a84fab3cSChristian Lamparter 
980a84fab3cSChristian Lamparter 	device_release_driver(&udev->dev);
981a84fab3cSChristian Lamparter 	if (parent)
982a84fab3cSChristian Lamparter 		device_unlock(parent);
983a84fab3cSChristian Lamparter 
984a84fab3cSChristian Lamparter 	usb_put_dev(udev);
985a84fab3cSChristian Lamparter }
986a84fab3cSChristian Lamparter 
987a84fab3cSChristian Lamparter static void carl9170_usb_firmware_finish(struct ar9170 *ar)
988a84fab3cSChristian Lamparter {
989a84fab3cSChristian Lamparter 	int err;
990a84fab3cSChristian Lamparter 
991a84fab3cSChristian Lamparter 	err = carl9170_parse_firmware(ar);
992a84fab3cSChristian Lamparter 	if (err)
993a84fab3cSChristian Lamparter 		goto err_freefw;
994a84fab3cSChristian Lamparter 
995a84fab3cSChristian Lamparter 	err = carl9170_usb_init_device(ar);
996a84fab3cSChristian Lamparter 	if (err)
997a84fab3cSChristian Lamparter 		goto err_freefw;
998a84fab3cSChristian Lamparter 
999a84fab3cSChristian Lamparter 	err = carl9170_register(ar);
1000a84fab3cSChristian Lamparter 
1001a84fab3cSChristian Lamparter 	carl9170_usb_stop(ar);
1002a84fab3cSChristian Lamparter 	if (err)
1003a84fab3cSChristian Lamparter 		goto err_unrx;
1004a84fab3cSChristian Lamparter 
1005a84fab3cSChristian Lamparter 	complete(&ar->fw_load_wait);
1006a84fab3cSChristian Lamparter 	usb_put_dev(ar->udev);
1007a84fab3cSChristian Lamparter 	return;
1008a84fab3cSChristian Lamparter 
1009a84fab3cSChristian Lamparter err_unrx:
1010a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
1011a84fab3cSChristian Lamparter 
1012a84fab3cSChristian Lamparter err_freefw:
1013a84fab3cSChristian Lamparter 	carl9170_release_firmware(ar);
1014a84fab3cSChristian Lamparter 	carl9170_usb_firmware_failed(ar);
1015a84fab3cSChristian Lamparter }
1016a84fab3cSChristian Lamparter 
1017a84fab3cSChristian Lamparter static void carl9170_usb_firmware_step2(const struct firmware *fw,
1018a84fab3cSChristian Lamparter 					void *context)
1019a84fab3cSChristian Lamparter {
1020a84fab3cSChristian Lamparter 	struct ar9170 *ar = context;
1021a84fab3cSChristian Lamparter 
1022a84fab3cSChristian Lamparter 	if (fw) {
1023a84fab3cSChristian Lamparter 		ar->fw.fw = fw;
1024a84fab3cSChristian Lamparter 		carl9170_usb_firmware_finish(ar);
1025a84fab3cSChristian Lamparter 		return;
1026a84fab3cSChristian Lamparter 	}
1027a84fab3cSChristian Lamparter 
1028a84fab3cSChristian Lamparter 	dev_err(&ar->udev->dev, "firmware not found.\n");
1029a84fab3cSChristian Lamparter 	carl9170_usb_firmware_failed(ar);
1030a84fab3cSChristian Lamparter }
1031a84fab3cSChristian Lamparter 
1032a84fab3cSChristian Lamparter static int carl9170_usb_probe(struct usb_interface *intf,
1033a84fab3cSChristian Lamparter 			      const struct usb_device_id *id)
1034a84fab3cSChristian Lamparter {
1035a84fab3cSChristian Lamparter 	struct ar9170 *ar;
1036a84fab3cSChristian Lamparter 	struct usb_device *udev;
1037a84fab3cSChristian Lamparter 	int err;
1038a84fab3cSChristian Lamparter 
1039a84fab3cSChristian Lamparter 	err = usb_reset_device(interface_to_usbdev(intf));
1040a84fab3cSChristian Lamparter 	if (err)
1041a84fab3cSChristian Lamparter 		return err;
1042a84fab3cSChristian Lamparter 
1043a84fab3cSChristian Lamparter 	ar = carl9170_alloc(sizeof(*ar));
1044a84fab3cSChristian Lamparter 	if (IS_ERR(ar))
1045a84fab3cSChristian Lamparter 		return PTR_ERR(ar);
1046a84fab3cSChristian Lamparter 
1047a84fab3cSChristian Lamparter 	udev = interface_to_usbdev(intf);
1048a84fab3cSChristian Lamparter 	usb_get_dev(udev);
1049a84fab3cSChristian Lamparter 	ar->udev = udev;
1050a84fab3cSChristian Lamparter 	ar->intf = intf;
1051a84fab3cSChristian Lamparter 	ar->features = id->driver_info;
1052a84fab3cSChristian Lamparter 
1053a84fab3cSChristian Lamparter 	usb_set_intfdata(intf, ar);
1054a84fab3cSChristian Lamparter 	SET_IEEE80211_DEV(ar->hw, &intf->dev);
1055a84fab3cSChristian Lamparter 
1056a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->rx_anch);
1057a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->rx_pool);
1058a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->rx_work);
1059a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->tx_wait);
1060a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->tx_anch);
1061a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->tx_cmd);
1062a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->tx_err);
1063a84fab3cSChristian Lamparter 	init_completion(&ar->cmd_wait);
1064a84fab3cSChristian Lamparter 	init_completion(&ar->fw_boot_wait);
1065a84fab3cSChristian Lamparter 	init_completion(&ar->fw_load_wait);
1066a84fab3cSChristian Lamparter 	tasklet_init(&ar->usb_tasklet, carl9170_usb_tasklet,
1067a84fab3cSChristian Lamparter 		     (unsigned long)ar);
1068a84fab3cSChristian Lamparter 
1069a84fab3cSChristian Lamparter 	atomic_set(&ar->tx_cmd_urbs, 0);
1070a84fab3cSChristian Lamparter 	atomic_set(&ar->tx_anch_urbs, 0);
1071a84fab3cSChristian Lamparter 	atomic_set(&ar->rx_work_urbs, 0);
1072a84fab3cSChristian Lamparter 	atomic_set(&ar->rx_anch_urbs, 0);
1073a84fab3cSChristian Lamparter 	atomic_set(&ar->rx_pool_urbs, 0);
1074a84fab3cSChristian Lamparter 
1075a84fab3cSChristian Lamparter 	usb_get_dev(ar->udev);
1076a84fab3cSChristian Lamparter 
1077a84fab3cSChristian Lamparter 	carl9170_set_state(ar, CARL9170_STOPPED);
1078a84fab3cSChristian Lamparter 
1079a84fab3cSChristian Lamparter 	return request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME,
1080a84fab3cSChristian Lamparter 		&ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2);
1081a84fab3cSChristian Lamparter }
1082a84fab3cSChristian Lamparter 
1083a84fab3cSChristian Lamparter static void carl9170_usb_disconnect(struct usb_interface *intf)
1084a84fab3cSChristian Lamparter {
1085a84fab3cSChristian Lamparter 	struct ar9170 *ar = usb_get_intfdata(intf);
1086a84fab3cSChristian Lamparter 	struct usb_device *udev;
1087a84fab3cSChristian Lamparter 
1088a84fab3cSChristian Lamparter 	if (WARN_ON(!ar))
1089a84fab3cSChristian Lamparter 		return;
1090a84fab3cSChristian Lamparter 
1091a84fab3cSChristian Lamparter 	udev = ar->udev;
1092a84fab3cSChristian Lamparter 	wait_for_completion(&ar->fw_load_wait);
1093a84fab3cSChristian Lamparter 
1094a84fab3cSChristian Lamparter 	if (IS_INITIALIZED(ar)) {
1095a84fab3cSChristian Lamparter 		carl9170_reboot(ar);
1096a84fab3cSChristian Lamparter 		carl9170_usb_stop(ar);
1097a84fab3cSChristian Lamparter 	}
1098a84fab3cSChristian Lamparter 
1099a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
1100a84fab3cSChristian Lamparter 	carl9170_unregister(ar);
1101a84fab3cSChristian Lamparter 
1102a84fab3cSChristian Lamparter 	usb_set_intfdata(intf, NULL);
1103a84fab3cSChristian Lamparter 
1104a84fab3cSChristian Lamparter 	carl9170_release_firmware(ar);
1105a84fab3cSChristian Lamparter 	carl9170_free(ar);
1106a84fab3cSChristian Lamparter 	usb_put_dev(udev);
1107a84fab3cSChristian Lamparter }
1108a84fab3cSChristian Lamparter 
1109a84fab3cSChristian Lamparter #ifdef CONFIG_PM
1110a84fab3cSChristian Lamparter static int carl9170_usb_suspend(struct usb_interface *intf,
1111a84fab3cSChristian Lamparter 				pm_message_t message)
1112a84fab3cSChristian Lamparter {
1113a84fab3cSChristian Lamparter 	struct ar9170 *ar = usb_get_intfdata(intf);
1114a84fab3cSChristian Lamparter 
1115a84fab3cSChristian Lamparter 	if (!ar)
1116a84fab3cSChristian Lamparter 		return -ENODEV;
1117a84fab3cSChristian Lamparter 
1118a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
1119a84fab3cSChristian Lamparter 
1120a84fab3cSChristian Lamparter 	return 0;
1121a84fab3cSChristian Lamparter }
1122a84fab3cSChristian Lamparter 
1123a84fab3cSChristian Lamparter static int carl9170_usb_resume(struct usb_interface *intf)
1124a84fab3cSChristian Lamparter {
1125a84fab3cSChristian Lamparter 	struct ar9170 *ar = usb_get_intfdata(intf);
1126a84fab3cSChristian Lamparter 	int err;
1127a84fab3cSChristian Lamparter 
1128a84fab3cSChristian Lamparter 	if (!ar)
1129a84fab3cSChristian Lamparter 		return -ENODEV;
1130a84fab3cSChristian Lamparter 
1131a84fab3cSChristian Lamparter 	usb_unpoison_anchored_urbs(&ar->rx_anch);
113297e2c402SChristian Lamparter 	carl9170_set_state(ar, CARL9170_STOPPED);
113397e2c402SChristian Lamparter 
113497e2c402SChristian Lamparter 	/*
113597e2c402SChristian Lamparter 	 * The USB documentation demands that [for suspend] all traffic
113697e2c402SChristian Lamparter 	 * to and from the device has to stop. This would be fine, but
113797e2c402SChristian Lamparter 	 * there's a catch: the device[usb phy] does not come back.
113897e2c402SChristian Lamparter 	 *
113997e2c402SChristian Lamparter 	 * Upon resume the firmware will "kill" itself and the
114097e2c402SChristian Lamparter 	 * boot-code sorts out the magic voodoo.
114197e2c402SChristian Lamparter 	 * Not very nice, but there's not much what could go wrong.
114297e2c402SChristian Lamparter 	 */
114397e2c402SChristian Lamparter 	msleep(1100);
1144a84fab3cSChristian Lamparter 
1145a84fab3cSChristian Lamparter 	err = carl9170_usb_init_device(ar);
1146a84fab3cSChristian Lamparter 	if (err)
1147a84fab3cSChristian Lamparter 		goto err_unrx;
1148a84fab3cSChristian Lamparter 
1149a84fab3cSChristian Lamparter 	return 0;
1150a84fab3cSChristian Lamparter 
1151a84fab3cSChristian Lamparter err_unrx:
1152a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
1153a84fab3cSChristian Lamparter 
1154a84fab3cSChristian Lamparter 	return err;
1155a84fab3cSChristian Lamparter }
1156a84fab3cSChristian Lamparter #endif /* CONFIG_PM */
1157a84fab3cSChristian Lamparter 
1158a84fab3cSChristian Lamparter static struct usb_driver carl9170_driver = {
1159a84fab3cSChristian Lamparter 	.name = KBUILD_MODNAME,
1160a84fab3cSChristian Lamparter 	.probe = carl9170_usb_probe,
1161a84fab3cSChristian Lamparter 	.disconnect = carl9170_usb_disconnect,
1162a84fab3cSChristian Lamparter 	.id_table = carl9170_usb_ids,
1163a84fab3cSChristian Lamparter 	.soft_unbind = 1,
1164a84fab3cSChristian Lamparter #ifdef CONFIG_PM
1165a84fab3cSChristian Lamparter 	.suspend = carl9170_usb_suspend,
1166a84fab3cSChristian Lamparter 	.resume = carl9170_usb_resume,
116797e2c402SChristian Lamparter 	.reset_resume = carl9170_usb_resume,
1168a84fab3cSChristian Lamparter #endif /* CONFIG_PM */
1169e1f12eb6SSarah Sharp 	.disable_hub_initiated_lpm = 1,
1170a84fab3cSChristian Lamparter };
1171a84fab3cSChristian Lamparter 
1172d632eb1bSGreg Kroah-Hartman module_usb_driver(carl9170_driver);
1173