xref: /openbmc/linux/drivers/net/wireless/ath/carl9170/usb.c (revision c86664e5a285af1afa06416e450e7c4af04daa7c)
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) },
115a84fab3cSChristian Lamparter 	/* AVM FRITZ!WLAN USB Stick N */
116a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x057c, 0x8401) },
117a84fab3cSChristian Lamparter 	/* AVM FRITZ!WLAN USB Stick N 2.4 */
118a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x057c, 0x8402) },
119a84fab3cSChristian Lamparter 	/* Qwest/Actiontec 802AIN Wireless N USB Network Adapter */
120a84fab3cSChristian Lamparter 	{ USB_DEVICE(0x1668, 0x1200) },
121*c86664e5SJan Puk 	/* Airlive X.USB a/b/g/n */
122*c86664e5SJan Puk 	{ USB_DEVICE(0x1b75, 0x9170) },
123a84fab3cSChristian Lamparter 
124a84fab3cSChristian Lamparter 	/* terminate */
125a84fab3cSChristian Lamparter 	{}
126a84fab3cSChristian Lamparter };
127a84fab3cSChristian Lamparter MODULE_DEVICE_TABLE(usb, carl9170_usb_ids);
128a84fab3cSChristian Lamparter 
129a84fab3cSChristian Lamparter static void carl9170_usb_submit_data_urb(struct ar9170 *ar)
130a84fab3cSChristian Lamparter {
131a84fab3cSChristian Lamparter 	struct urb *urb;
132a84fab3cSChristian Lamparter 	int err;
133a84fab3cSChristian Lamparter 
134a84fab3cSChristian Lamparter 	if (atomic_inc_return(&ar->tx_anch_urbs) > AR9170_NUM_TX_URBS)
135a84fab3cSChristian Lamparter 		goto err_acc;
136a84fab3cSChristian Lamparter 
137a84fab3cSChristian Lamparter 	urb = usb_get_from_anchor(&ar->tx_wait);
138a84fab3cSChristian Lamparter 	if (!urb)
139a84fab3cSChristian Lamparter 		goto err_acc;
140a84fab3cSChristian Lamparter 
141a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->tx_anch);
142a84fab3cSChristian Lamparter 
143a84fab3cSChristian Lamparter 	err = usb_submit_urb(urb, GFP_ATOMIC);
144a84fab3cSChristian Lamparter 	if (unlikely(err)) {
145a84fab3cSChristian Lamparter 		if (net_ratelimit()) {
146a84fab3cSChristian Lamparter 			dev_err(&ar->udev->dev, "tx submit failed (%d)\n",
147a84fab3cSChristian Lamparter 				urb->status);
148a84fab3cSChristian Lamparter 		}
149a84fab3cSChristian Lamparter 
150a84fab3cSChristian Lamparter 		usb_unanchor_urb(urb);
151a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->tx_err);
152a84fab3cSChristian Lamparter 	}
153a84fab3cSChristian Lamparter 
154a84fab3cSChristian Lamparter 	usb_free_urb(urb);
155a84fab3cSChristian Lamparter 
156a84fab3cSChristian Lamparter 	if (likely(err == 0))
157a84fab3cSChristian Lamparter 		return;
158a84fab3cSChristian Lamparter 
159a84fab3cSChristian Lamparter err_acc:
160a84fab3cSChristian Lamparter 	atomic_dec(&ar->tx_anch_urbs);
161a84fab3cSChristian Lamparter }
162a84fab3cSChristian Lamparter 
163a84fab3cSChristian Lamparter static void carl9170_usb_tx_data_complete(struct urb *urb)
164a84fab3cSChristian Lamparter {
165b2767363SJoe Perches 	struct ar9170 *ar = usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
166a84fab3cSChristian Lamparter 
167a84fab3cSChristian Lamparter 	if (WARN_ON_ONCE(!ar)) {
168a84fab3cSChristian Lamparter 		dev_kfree_skb_irq(urb->context);
169a84fab3cSChristian Lamparter 		return;
170a84fab3cSChristian Lamparter 	}
171a84fab3cSChristian Lamparter 
172a84fab3cSChristian Lamparter 	atomic_dec(&ar->tx_anch_urbs);
173a84fab3cSChristian Lamparter 
174a84fab3cSChristian Lamparter 	switch (urb->status) {
175a84fab3cSChristian Lamparter 	/* everything is fine */
176a84fab3cSChristian Lamparter 	case 0:
177a84fab3cSChristian Lamparter 		carl9170_tx_callback(ar, (void *)urb->context);
178a84fab3cSChristian Lamparter 		break;
179a84fab3cSChristian Lamparter 
180a84fab3cSChristian Lamparter 	/* disconnect */
181a84fab3cSChristian Lamparter 	case -ENOENT:
182a84fab3cSChristian Lamparter 	case -ECONNRESET:
183a84fab3cSChristian Lamparter 	case -ENODEV:
184a84fab3cSChristian Lamparter 	case -ESHUTDOWN:
185a84fab3cSChristian Lamparter 		/*
186a84fab3cSChristian Lamparter 		 * Defer the frame clean-up to the tasklet worker.
187a84fab3cSChristian Lamparter 		 * This is necessary, because carl9170_tx_drop
188a84fab3cSChristian Lamparter 		 * does not work in an irqsave context.
189a84fab3cSChristian Lamparter 		 */
190a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->tx_err);
191a84fab3cSChristian Lamparter 		return;
192a84fab3cSChristian Lamparter 
193a84fab3cSChristian Lamparter 	/* a random transmission error has occurred? */
194a84fab3cSChristian Lamparter 	default:
195a84fab3cSChristian Lamparter 		if (net_ratelimit()) {
196a84fab3cSChristian Lamparter 			dev_err(&ar->udev->dev, "tx failed (%d)\n",
197a84fab3cSChristian Lamparter 				urb->status);
198a84fab3cSChristian Lamparter 		}
199a84fab3cSChristian Lamparter 
200a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->tx_err);
201a84fab3cSChristian Lamparter 		break;
202a84fab3cSChristian Lamparter 	}
203a84fab3cSChristian Lamparter 
204a84fab3cSChristian Lamparter 	if (likely(IS_STARTED(ar)))
205a84fab3cSChristian Lamparter 		carl9170_usb_submit_data_urb(ar);
206a84fab3cSChristian Lamparter }
207a84fab3cSChristian Lamparter 
208a84fab3cSChristian Lamparter static int carl9170_usb_submit_cmd_urb(struct ar9170 *ar)
209a84fab3cSChristian Lamparter {
210a84fab3cSChristian Lamparter 	struct urb *urb;
211a84fab3cSChristian Lamparter 	int err;
212a84fab3cSChristian Lamparter 
213a84fab3cSChristian Lamparter 	if (atomic_inc_return(&ar->tx_cmd_urbs) != 1) {
214a84fab3cSChristian Lamparter 		atomic_dec(&ar->tx_cmd_urbs);
215a84fab3cSChristian Lamparter 		return 0;
216a84fab3cSChristian Lamparter 	}
217a84fab3cSChristian Lamparter 
218a84fab3cSChristian Lamparter 	urb = usb_get_from_anchor(&ar->tx_cmd);
219a84fab3cSChristian Lamparter 	if (!urb) {
220a84fab3cSChristian Lamparter 		atomic_dec(&ar->tx_cmd_urbs);
221a84fab3cSChristian Lamparter 		return 0;
222a84fab3cSChristian Lamparter 	}
223a84fab3cSChristian Lamparter 
224a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->tx_anch);
225a84fab3cSChristian Lamparter 	err = usb_submit_urb(urb, GFP_ATOMIC);
226a84fab3cSChristian Lamparter 	if (unlikely(err)) {
227a84fab3cSChristian Lamparter 		usb_unanchor_urb(urb);
228a84fab3cSChristian Lamparter 		atomic_dec(&ar->tx_cmd_urbs);
229a84fab3cSChristian Lamparter 	}
230a84fab3cSChristian Lamparter 	usb_free_urb(urb);
231a84fab3cSChristian Lamparter 
232a84fab3cSChristian Lamparter 	return err;
233a84fab3cSChristian Lamparter }
234a84fab3cSChristian Lamparter 
235a84fab3cSChristian Lamparter static void carl9170_usb_cmd_complete(struct urb *urb)
236a84fab3cSChristian Lamparter {
237a84fab3cSChristian Lamparter 	struct ar9170 *ar = urb->context;
238a84fab3cSChristian Lamparter 	int err = 0;
239a84fab3cSChristian Lamparter 
240a84fab3cSChristian Lamparter 	if (WARN_ON_ONCE(!ar))
241a84fab3cSChristian Lamparter 		return;
242a84fab3cSChristian Lamparter 
243a84fab3cSChristian Lamparter 	atomic_dec(&ar->tx_cmd_urbs);
244a84fab3cSChristian Lamparter 
245a84fab3cSChristian Lamparter 	switch (urb->status) {
246a84fab3cSChristian Lamparter 	/* everything is fine */
247a84fab3cSChristian Lamparter 	case 0:
248a84fab3cSChristian Lamparter 		break;
249a84fab3cSChristian Lamparter 
250a84fab3cSChristian Lamparter 	/* disconnect */
251a84fab3cSChristian Lamparter 	case -ENOENT:
252a84fab3cSChristian Lamparter 	case -ECONNRESET:
253a84fab3cSChristian Lamparter 	case -ENODEV:
254a84fab3cSChristian Lamparter 	case -ESHUTDOWN:
255a84fab3cSChristian Lamparter 		return;
256a84fab3cSChristian Lamparter 
257a84fab3cSChristian Lamparter 	default:
258a84fab3cSChristian Lamparter 		err = urb->status;
259a84fab3cSChristian Lamparter 		break;
260a84fab3cSChristian Lamparter 	}
261a84fab3cSChristian Lamparter 
262a84fab3cSChristian Lamparter 	if (!IS_INITIALIZED(ar))
263a84fab3cSChristian Lamparter 		return;
264a84fab3cSChristian Lamparter 
265a84fab3cSChristian Lamparter 	if (err)
266a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "submit cmd cb failed (%d).\n", err);
267a84fab3cSChristian Lamparter 
268a84fab3cSChristian Lamparter 	err = carl9170_usb_submit_cmd_urb(ar);
269a84fab3cSChristian Lamparter 	if (err)
270a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "submit cmd failed (%d).\n", err);
271a84fab3cSChristian Lamparter }
272a84fab3cSChristian Lamparter 
273a84fab3cSChristian Lamparter static void carl9170_usb_rx_irq_complete(struct urb *urb)
274a84fab3cSChristian Lamparter {
275a84fab3cSChristian Lamparter 	struct ar9170 *ar = urb->context;
276a84fab3cSChristian Lamparter 
277a84fab3cSChristian Lamparter 	if (WARN_ON_ONCE(!ar))
278a84fab3cSChristian Lamparter 		return;
279a84fab3cSChristian Lamparter 
280a84fab3cSChristian Lamparter 	switch (urb->status) {
281a84fab3cSChristian Lamparter 	/* everything is fine */
282a84fab3cSChristian Lamparter 	case 0:
283a84fab3cSChristian Lamparter 		break;
284a84fab3cSChristian Lamparter 
285a84fab3cSChristian Lamparter 	/* disconnect */
286a84fab3cSChristian Lamparter 	case -ENOENT:
287a84fab3cSChristian Lamparter 	case -ECONNRESET:
288a84fab3cSChristian Lamparter 	case -ENODEV:
289a84fab3cSChristian Lamparter 	case -ESHUTDOWN:
290a84fab3cSChristian Lamparter 		return;
291a84fab3cSChristian Lamparter 
292a84fab3cSChristian Lamparter 	default:
293a84fab3cSChristian Lamparter 		goto resubmit;
294a84fab3cSChristian Lamparter 	}
295a84fab3cSChristian Lamparter 
296a84fab3cSChristian Lamparter 	carl9170_handle_command_response(ar, urb->transfer_buffer,
297a84fab3cSChristian Lamparter 					 urb->actual_length);
298a84fab3cSChristian Lamparter 
299a84fab3cSChristian Lamparter resubmit:
300a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->rx_anch);
301a84fab3cSChristian Lamparter 	if (unlikely(usb_submit_urb(urb, GFP_ATOMIC)))
302a84fab3cSChristian Lamparter 		usb_unanchor_urb(urb);
303a84fab3cSChristian Lamparter }
304a84fab3cSChristian Lamparter 
305a84fab3cSChristian Lamparter static int carl9170_usb_submit_rx_urb(struct ar9170 *ar, gfp_t gfp)
306a84fab3cSChristian Lamparter {
307a84fab3cSChristian Lamparter 	struct urb *urb;
308a84fab3cSChristian Lamparter 	int err = 0, runs = 0;
309a84fab3cSChristian Lamparter 
310a84fab3cSChristian Lamparter 	while ((atomic_read(&ar->rx_anch_urbs) < AR9170_NUM_RX_URBS) &&
311a84fab3cSChristian Lamparter 		(runs++ < AR9170_NUM_RX_URBS)) {
312a84fab3cSChristian Lamparter 		err = -ENOSPC;
313a84fab3cSChristian Lamparter 		urb = usb_get_from_anchor(&ar->rx_pool);
314a84fab3cSChristian Lamparter 		if (urb) {
315a84fab3cSChristian Lamparter 			usb_anchor_urb(urb, &ar->rx_anch);
316a84fab3cSChristian Lamparter 			err = usb_submit_urb(urb, gfp);
317a84fab3cSChristian Lamparter 			if (unlikely(err)) {
318a84fab3cSChristian Lamparter 				usb_unanchor_urb(urb);
319a84fab3cSChristian Lamparter 				usb_anchor_urb(urb, &ar->rx_pool);
320a84fab3cSChristian Lamparter 			} else {
321a84fab3cSChristian Lamparter 				atomic_dec(&ar->rx_pool_urbs);
322a84fab3cSChristian Lamparter 				atomic_inc(&ar->rx_anch_urbs);
323a84fab3cSChristian Lamparter 			}
324a84fab3cSChristian Lamparter 			usb_free_urb(urb);
325a84fab3cSChristian Lamparter 		}
326a84fab3cSChristian Lamparter 	}
327a84fab3cSChristian Lamparter 
328a84fab3cSChristian Lamparter 	return err;
329a84fab3cSChristian Lamparter }
330a84fab3cSChristian Lamparter 
331a84fab3cSChristian Lamparter static void carl9170_usb_rx_work(struct ar9170 *ar)
332a84fab3cSChristian Lamparter {
333a84fab3cSChristian Lamparter 	struct urb *urb;
334a84fab3cSChristian Lamparter 	int i;
335a84fab3cSChristian Lamparter 
336a84fab3cSChristian Lamparter 	for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
337a84fab3cSChristian Lamparter 		urb = usb_get_from_anchor(&ar->rx_work);
338a84fab3cSChristian Lamparter 		if (!urb)
339a84fab3cSChristian Lamparter 			break;
340a84fab3cSChristian Lamparter 
341a84fab3cSChristian Lamparter 		atomic_dec(&ar->rx_work_urbs);
342a84fab3cSChristian Lamparter 		if (IS_INITIALIZED(ar)) {
343a84fab3cSChristian Lamparter 			carl9170_rx(ar, urb->transfer_buffer,
344a84fab3cSChristian Lamparter 				    urb->actual_length);
345a84fab3cSChristian Lamparter 		}
346a84fab3cSChristian Lamparter 
347a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->rx_pool);
348a84fab3cSChristian Lamparter 		atomic_inc(&ar->rx_pool_urbs);
349a84fab3cSChristian Lamparter 
350a84fab3cSChristian Lamparter 		usb_free_urb(urb);
351a84fab3cSChristian Lamparter 
352a84fab3cSChristian Lamparter 		carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
353a84fab3cSChristian Lamparter 	}
354a84fab3cSChristian Lamparter }
355a84fab3cSChristian Lamparter 
356a84fab3cSChristian Lamparter void carl9170_usb_handle_tx_err(struct ar9170 *ar)
357a84fab3cSChristian Lamparter {
358a84fab3cSChristian Lamparter 	struct urb *urb;
359a84fab3cSChristian Lamparter 
360a84fab3cSChristian Lamparter 	while ((urb = usb_get_from_anchor(&ar->tx_err))) {
361a84fab3cSChristian Lamparter 		struct sk_buff *skb = (void *)urb->context;
362a84fab3cSChristian Lamparter 
363a84fab3cSChristian Lamparter 		carl9170_tx_drop(ar, skb);
364a84fab3cSChristian Lamparter 		carl9170_tx_callback(ar, skb);
365a84fab3cSChristian Lamparter 		usb_free_urb(urb);
366a84fab3cSChristian Lamparter 	}
367a84fab3cSChristian Lamparter }
368a84fab3cSChristian Lamparter 
369a84fab3cSChristian Lamparter static void carl9170_usb_tasklet(unsigned long data)
370a84fab3cSChristian Lamparter {
371a84fab3cSChristian Lamparter 	struct ar9170 *ar = (struct ar9170 *) data;
372a84fab3cSChristian Lamparter 
373cf6487d0SChristian Lamparter 	if (!IS_INITIALIZED(ar))
374cf6487d0SChristian Lamparter 		return;
375cf6487d0SChristian Lamparter 
376a84fab3cSChristian Lamparter 	carl9170_usb_rx_work(ar);
377a84fab3cSChristian Lamparter 
378a84fab3cSChristian Lamparter 	/*
379a84fab3cSChristian Lamparter 	 * Strictly speaking: The tx scheduler is not part of the USB system.
380a84fab3cSChristian Lamparter 	 * But the rx worker returns frames back to the mac80211-stack and
381a84fab3cSChristian Lamparter 	 * this is the _perfect_ place to generate the next transmissions.
382a84fab3cSChristian Lamparter 	 */
383a84fab3cSChristian Lamparter 	if (IS_STARTED(ar))
384a84fab3cSChristian Lamparter 		carl9170_tx_scheduler(ar);
385a84fab3cSChristian Lamparter }
386a84fab3cSChristian Lamparter 
387a84fab3cSChristian Lamparter static void carl9170_usb_rx_complete(struct urb *urb)
388a84fab3cSChristian Lamparter {
389a84fab3cSChristian Lamparter 	struct ar9170 *ar = (struct ar9170 *)urb->context;
390a84fab3cSChristian Lamparter 	int err;
391a84fab3cSChristian Lamparter 
392a84fab3cSChristian Lamparter 	if (WARN_ON_ONCE(!ar))
393a84fab3cSChristian Lamparter 		return;
394a84fab3cSChristian Lamparter 
395a84fab3cSChristian Lamparter 	atomic_dec(&ar->rx_anch_urbs);
396a84fab3cSChristian Lamparter 
397a84fab3cSChristian Lamparter 	switch (urb->status) {
398a84fab3cSChristian Lamparter 	case 0:
399a84fab3cSChristian Lamparter 		/* rx path */
400a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->rx_work);
401a84fab3cSChristian Lamparter 		atomic_inc(&ar->rx_work_urbs);
402a84fab3cSChristian Lamparter 		break;
403a84fab3cSChristian Lamparter 
404a84fab3cSChristian Lamparter 	case -ENOENT:
405a84fab3cSChristian Lamparter 	case -ECONNRESET:
406a84fab3cSChristian Lamparter 	case -ENODEV:
407a84fab3cSChristian Lamparter 	case -ESHUTDOWN:
408a84fab3cSChristian Lamparter 		/* handle disconnect events*/
409a84fab3cSChristian Lamparter 		return;
410a84fab3cSChristian Lamparter 
411a84fab3cSChristian Lamparter 	default:
412a84fab3cSChristian Lamparter 		/* handle all other errors */
413a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->rx_pool);
414a84fab3cSChristian Lamparter 		atomic_inc(&ar->rx_pool_urbs);
415a84fab3cSChristian Lamparter 		break;
416a84fab3cSChristian Lamparter 	}
417a84fab3cSChristian Lamparter 
418a84fab3cSChristian Lamparter 	err = carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
419a84fab3cSChristian Lamparter 	if (unlikely(err)) {
420a84fab3cSChristian Lamparter 		/*
421a84fab3cSChristian Lamparter 		 * usb_submit_rx_urb reported a problem.
422a84fab3cSChristian Lamparter 		 * In case this is due to a rx buffer shortage,
423a84fab3cSChristian Lamparter 		 * elevate the tasklet worker priority to
424a84fab3cSChristian Lamparter 		 * the highest available level.
425a84fab3cSChristian Lamparter 		 */
426a84fab3cSChristian Lamparter 		tasklet_hi_schedule(&ar->usb_tasklet);
427a84fab3cSChristian Lamparter 
428a84fab3cSChristian Lamparter 		if (atomic_read(&ar->rx_anch_urbs) == 0) {
429a84fab3cSChristian Lamparter 			/*
430a84fab3cSChristian Lamparter 			 * The system is too slow to cope with
431a84fab3cSChristian Lamparter 			 * the enormous workload. We have simply
432a84fab3cSChristian Lamparter 			 * run out of active rx urbs and this
433a84fab3cSChristian Lamparter 			 * unfortunatly leads to an unpredictable
434a84fab3cSChristian Lamparter 			 * device.
435a84fab3cSChristian Lamparter 			 */
436a84fab3cSChristian Lamparter 
437e4a668c5SChristian Lamparter 			ieee80211_queue_work(ar->hw, &ar->ping_work);
438a84fab3cSChristian Lamparter 		}
439a84fab3cSChristian Lamparter 	} else {
440a84fab3cSChristian Lamparter 		/*
441a84fab3cSChristian Lamparter 		 * Using anything less than _high_ priority absolutely
442a84fab3cSChristian Lamparter 		 * kills the rx performance my UP-System...
443a84fab3cSChristian Lamparter 		 */
444a84fab3cSChristian Lamparter 		tasklet_hi_schedule(&ar->usb_tasklet);
445a84fab3cSChristian Lamparter 	}
446a84fab3cSChristian Lamparter }
447a84fab3cSChristian Lamparter 
448a84fab3cSChristian Lamparter static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp)
449a84fab3cSChristian Lamparter {
450a84fab3cSChristian Lamparter 	struct urb *urb;
451a84fab3cSChristian Lamparter 	void *buf;
452a84fab3cSChristian Lamparter 
453a84fab3cSChristian Lamparter 	buf = kmalloc(ar->fw.rx_size, gfp);
454a84fab3cSChristian Lamparter 	if (!buf)
455a84fab3cSChristian Lamparter 		return NULL;
456a84fab3cSChristian Lamparter 
457a84fab3cSChristian Lamparter 	urb = usb_alloc_urb(0, gfp);
458a84fab3cSChristian Lamparter 	if (!urb) {
459a84fab3cSChristian Lamparter 		kfree(buf);
460a84fab3cSChristian Lamparter 		return NULL;
461a84fab3cSChristian Lamparter 	}
462a84fab3cSChristian Lamparter 
463a84fab3cSChristian Lamparter 	usb_fill_bulk_urb(urb, ar->udev, usb_rcvbulkpipe(ar->udev,
464a84fab3cSChristian Lamparter 			  AR9170_USB_EP_RX), buf, ar->fw.rx_size,
465a84fab3cSChristian Lamparter 			  carl9170_usb_rx_complete, ar);
466a84fab3cSChristian Lamparter 
467a84fab3cSChristian Lamparter 	urb->transfer_flags |= URB_FREE_BUFFER;
468a84fab3cSChristian Lamparter 
469a84fab3cSChristian Lamparter 	return urb;
470a84fab3cSChristian Lamparter }
471a84fab3cSChristian Lamparter 
472a84fab3cSChristian Lamparter static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar)
473a84fab3cSChristian Lamparter {
474a84fab3cSChristian Lamparter 	struct urb *urb = NULL;
475a84fab3cSChristian Lamparter 	void *ibuf;
476a84fab3cSChristian Lamparter 	int err = -ENOMEM;
477a84fab3cSChristian Lamparter 
478a84fab3cSChristian Lamparter 	urb = usb_alloc_urb(0, GFP_KERNEL);
479a84fab3cSChristian Lamparter 	if (!urb)
480a84fab3cSChristian Lamparter 		goto out;
481a84fab3cSChristian Lamparter 
482a84fab3cSChristian Lamparter 	ibuf = kmalloc(AR9170_USB_EP_CTRL_MAX, GFP_KERNEL);
483a84fab3cSChristian Lamparter 	if (!ibuf)
484a84fab3cSChristian Lamparter 		goto out;
485a84fab3cSChristian Lamparter 
486a84fab3cSChristian Lamparter 	usb_fill_int_urb(urb, ar->udev, usb_rcvintpipe(ar->udev,
487a84fab3cSChristian Lamparter 			 AR9170_USB_EP_IRQ), ibuf, AR9170_USB_EP_CTRL_MAX,
488a84fab3cSChristian Lamparter 			 carl9170_usb_rx_irq_complete, ar, 1);
489a84fab3cSChristian Lamparter 
490a84fab3cSChristian Lamparter 	urb->transfer_flags |= URB_FREE_BUFFER;
491a84fab3cSChristian Lamparter 
492a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->rx_anch);
493a84fab3cSChristian Lamparter 	err = usb_submit_urb(urb, GFP_KERNEL);
494a84fab3cSChristian Lamparter 	if (err)
495a84fab3cSChristian Lamparter 		usb_unanchor_urb(urb);
496a84fab3cSChristian Lamparter 
497a84fab3cSChristian Lamparter out:
498a84fab3cSChristian Lamparter 	usb_free_urb(urb);
499a84fab3cSChristian Lamparter 	return err;
500a84fab3cSChristian Lamparter }
501a84fab3cSChristian Lamparter 
502a84fab3cSChristian Lamparter static int carl9170_usb_init_rx_bulk_urbs(struct ar9170 *ar)
503a84fab3cSChristian Lamparter {
504a84fab3cSChristian Lamparter 	struct urb *urb;
505a84fab3cSChristian Lamparter 	int i, err = -EINVAL;
506a84fab3cSChristian Lamparter 
507a84fab3cSChristian Lamparter 	/*
508a84fab3cSChristian Lamparter 	 * The driver actively maintains a second shadow
509a84fab3cSChristian Lamparter 	 * pool for inactive, but fully-prepared rx urbs.
510a84fab3cSChristian Lamparter 	 *
511a84fab3cSChristian Lamparter 	 * The pool should help the driver to master huge
512a84fab3cSChristian Lamparter 	 * workload spikes without running the risk of
513a84fab3cSChristian Lamparter 	 * undersupplying the hardware or wasting time by
514a84fab3cSChristian Lamparter 	 * processing rx data (streams) inside the urb
515a84fab3cSChristian Lamparter 	 * completion (hardirq context).
516a84fab3cSChristian Lamparter 	 */
517a84fab3cSChristian Lamparter 	for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
518a84fab3cSChristian Lamparter 		urb = carl9170_usb_alloc_rx_urb(ar, GFP_KERNEL);
519a84fab3cSChristian Lamparter 		if (!urb) {
520a84fab3cSChristian Lamparter 			err = -ENOMEM;
521a84fab3cSChristian Lamparter 			goto err_out;
522a84fab3cSChristian Lamparter 		}
523a84fab3cSChristian Lamparter 
524a84fab3cSChristian Lamparter 		usb_anchor_urb(urb, &ar->rx_pool);
525a84fab3cSChristian Lamparter 		atomic_inc(&ar->rx_pool_urbs);
526a84fab3cSChristian Lamparter 		usb_free_urb(urb);
527a84fab3cSChristian Lamparter 	}
528a84fab3cSChristian Lamparter 
529a84fab3cSChristian Lamparter 	err = carl9170_usb_submit_rx_urb(ar, GFP_KERNEL);
530a84fab3cSChristian Lamparter 	if (err)
531a84fab3cSChristian Lamparter 		goto err_out;
532a84fab3cSChristian Lamparter 
533a84fab3cSChristian Lamparter 	/* the device now waiting for the firmware. */
534a84fab3cSChristian Lamparter 	carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
535a84fab3cSChristian Lamparter 	return 0;
536a84fab3cSChristian Lamparter 
537a84fab3cSChristian Lamparter err_out:
538a84fab3cSChristian Lamparter 
539a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->rx_pool);
540a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->rx_work);
541a84fab3cSChristian Lamparter 	usb_kill_anchored_urbs(&ar->rx_anch);
542a84fab3cSChristian Lamparter 	return err;
543a84fab3cSChristian Lamparter }
544a84fab3cSChristian Lamparter 
545a84fab3cSChristian Lamparter static int carl9170_usb_flush(struct ar9170 *ar)
546a84fab3cSChristian Lamparter {
547a84fab3cSChristian Lamparter 	struct urb *urb;
548a84fab3cSChristian Lamparter 	int ret, err = 0;
549a84fab3cSChristian Lamparter 
550a84fab3cSChristian Lamparter 	while ((urb = usb_get_from_anchor(&ar->tx_wait))) {
551a84fab3cSChristian Lamparter 		struct sk_buff *skb = (void *)urb->context;
552a84fab3cSChristian Lamparter 		carl9170_tx_drop(ar, skb);
553a84fab3cSChristian Lamparter 		carl9170_tx_callback(ar, skb);
554a84fab3cSChristian Lamparter 		usb_free_urb(urb);
555a84fab3cSChristian Lamparter 	}
556a84fab3cSChristian Lamparter 
557dfa31fefSChristian Lamparter 	ret = usb_wait_anchor_empty_timeout(&ar->tx_cmd, 1000);
558a84fab3cSChristian Lamparter 	if (ret == 0)
559a84fab3cSChristian Lamparter 		err = -ETIMEDOUT;
560a84fab3cSChristian Lamparter 
561a84fab3cSChristian Lamparter 	/* lets wait a while until the tx - queues are dried out */
562dfa31fefSChristian Lamparter 	ret = usb_wait_anchor_empty_timeout(&ar->tx_anch, 1000);
563a84fab3cSChristian Lamparter 	if (ret == 0)
564a84fab3cSChristian Lamparter 		err = -ETIMEDOUT;
565a84fab3cSChristian Lamparter 
566a84fab3cSChristian Lamparter 	usb_kill_anchored_urbs(&ar->tx_anch);
567a84fab3cSChristian Lamparter 	carl9170_usb_handle_tx_err(ar);
568a84fab3cSChristian Lamparter 
569a84fab3cSChristian Lamparter 	return err;
570a84fab3cSChristian Lamparter }
571a84fab3cSChristian Lamparter 
572a84fab3cSChristian Lamparter static void carl9170_usb_cancel_urbs(struct ar9170 *ar)
573a84fab3cSChristian Lamparter {
574a84fab3cSChristian Lamparter 	int err;
575a84fab3cSChristian Lamparter 
576a84fab3cSChristian Lamparter 	carl9170_set_state(ar, CARL9170_UNKNOWN_STATE);
577a84fab3cSChristian Lamparter 
578a84fab3cSChristian Lamparter 	err = carl9170_usb_flush(ar);
579a84fab3cSChristian Lamparter 	if (err)
580a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "stuck tx urbs!\n");
581a84fab3cSChristian Lamparter 
582a84fab3cSChristian Lamparter 	usb_poison_anchored_urbs(&ar->tx_anch);
583a84fab3cSChristian Lamparter 	carl9170_usb_handle_tx_err(ar);
584a84fab3cSChristian Lamparter 	usb_poison_anchored_urbs(&ar->rx_anch);
585a84fab3cSChristian Lamparter 
586a84fab3cSChristian Lamparter 	tasklet_kill(&ar->usb_tasklet);
587a84fab3cSChristian Lamparter 
588a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->rx_work);
589a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->rx_pool);
590a84fab3cSChristian Lamparter 	usb_scuttle_anchored_urbs(&ar->tx_cmd);
591a84fab3cSChristian Lamparter }
592a84fab3cSChristian Lamparter 
593a84fab3cSChristian Lamparter int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd,
594a84fab3cSChristian Lamparter 			const bool free_buf)
595a84fab3cSChristian Lamparter {
596a84fab3cSChristian Lamparter 	struct urb *urb;
597cae7f953SChristian Lamparter 	int err = 0;
598a84fab3cSChristian Lamparter 
599cae7f953SChristian Lamparter 	if (!IS_INITIALIZED(ar)) {
600cae7f953SChristian Lamparter 		err = -EPERM;
601cae7f953SChristian Lamparter 		goto err_free;
602cae7f953SChristian Lamparter 	}
603a84fab3cSChristian Lamparter 
604cae7f953SChristian Lamparter 	if (WARN_ON(cmd->hdr.len > CARL9170_MAX_CMD_LEN - 4)) {
605cae7f953SChristian Lamparter 		err = -EINVAL;
606cae7f953SChristian Lamparter 		goto err_free;
607cae7f953SChristian Lamparter 	}
608a84fab3cSChristian Lamparter 
609a84fab3cSChristian Lamparter 	urb = usb_alloc_urb(0, GFP_ATOMIC);
610cae7f953SChristian Lamparter 	if (!urb) {
611cae7f953SChristian Lamparter 		err = -ENOMEM;
612cae7f953SChristian Lamparter 		goto err_free;
613cae7f953SChristian Lamparter 	}
614a84fab3cSChristian Lamparter 
615a84fab3cSChristian Lamparter 	usb_fill_int_urb(urb, ar->udev, usb_sndintpipe(ar->udev,
616a84fab3cSChristian Lamparter 		AR9170_USB_EP_CMD), cmd, cmd->hdr.len + 4,
617a84fab3cSChristian Lamparter 		carl9170_usb_cmd_complete, ar, 1);
618a84fab3cSChristian Lamparter 
619a84fab3cSChristian Lamparter 	if (free_buf)
620a84fab3cSChristian Lamparter 		urb->transfer_flags |= URB_FREE_BUFFER;
621a84fab3cSChristian Lamparter 
622a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->tx_cmd);
623a84fab3cSChristian Lamparter 	usb_free_urb(urb);
624a84fab3cSChristian Lamparter 
625a84fab3cSChristian Lamparter 	return carl9170_usb_submit_cmd_urb(ar);
626cae7f953SChristian Lamparter 
627cae7f953SChristian Lamparter err_free:
628cae7f953SChristian Lamparter 	if (free_buf)
629cae7f953SChristian Lamparter 		kfree(cmd);
630cae7f953SChristian Lamparter 
631cae7f953SChristian Lamparter 	return err;
632a84fab3cSChristian Lamparter }
633a84fab3cSChristian Lamparter 
634a84fab3cSChristian Lamparter int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd,
635a84fab3cSChristian Lamparter 	unsigned int plen, void *payload, unsigned int outlen, void *out)
636a84fab3cSChristian Lamparter {
637a84fab3cSChristian Lamparter 	int err = -ENOMEM;
638a84fab3cSChristian Lamparter 
639a84fab3cSChristian Lamparter 	if (!IS_ACCEPTING_CMD(ar))
640a84fab3cSChristian Lamparter 		return -EIO;
641a84fab3cSChristian Lamparter 
642a84fab3cSChristian Lamparter 	if (!(cmd & CARL9170_CMD_ASYNC_FLAG))
643a84fab3cSChristian Lamparter 		might_sleep();
644a84fab3cSChristian Lamparter 
645a84fab3cSChristian Lamparter 	ar->cmd.hdr.len = plen;
646a84fab3cSChristian Lamparter 	ar->cmd.hdr.cmd = cmd;
647a84fab3cSChristian Lamparter 	/* writing multiple regs fills this buffer already */
648a84fab3cSChristian Lamparter 	if (plen && payload != (u8 *)(ar->cmd.data))
649a84fab3cSChristian Lamparter 		memcpy(ar->cmd.data, payload, plen);
650a84fab3cSChristian Lamparter 
651a84fab3cSChristian Lamparter 	spin_lock_bh(&ar->cmd_lock);
652a84fab3cSChristian Lamparter 	ar->readbuf = (u8 *)out;
653a84fab3cSChristian Lamparter 	ar->readlen = outlen;
654a84fab3cSChristian Lamparter 	spin_unlock_bh(&ar->cmd_lock);
655a84fab3cSChristian Lamparter 
656a84fab3cSChristian Lamparter 	err = __carl9170_exec_cmd(ar, &ar->cmd, false);
657a84fab3cSChristian Lamparter 
658a84fab3cSChristian Lamparter 	if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) {
659a84fab3cSChristian Lamparter 		err = wait_for_completion_timeout(&ar->cmd_wait, HZ);
660a84fab3cSChristian Lamparter 		if (err == 0) {
661a84fab3cSChristian Lamparter 			err = -ETIMEDOUT;
662a84fab3cSChristian Lamparter 			goto err_unbuf;
663a84fab3cSChristian Lamparter 		}
664a84fab3cSChristian Lamparter 
665a84fab3cSChristian Lamparter 		if (ar->readlen != outlen) {
666a84fab3cSChristian Lamparter 			err = -EMSGSIZE;
667a84fab3cSChristian Lamparter 			goto err_unbuf;
668a84fab3cSChristian Lamparter 		}
669a84fab3cSChristian Lamparter 	}
670a84fab3cSChristian Lamparter 
671a84fab3cSChristian Lamparter 	return 0;
672a84fab3cSChristian Lamparter 
673a84fab3cSChristian Lamparter err_unbuf:
674a84fab3cSChristian Lamparter 	/* Maybe the device was removed in the moment we were waiting? */
675a84fab3cSChristian Lamparter 	if (IS_STARTED(ar)) {
676a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "no command feedback "
677a84fab3cSChristian Lamparter 			"received (%d).\n", err);
678a84fab3cSChristian Lamparter 
679a84fab3cSChristian Lamparter 		/* provide some maybe useful debug information */
680a84fab3cSChristian Lamparter 		print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE,
681a84fab3cSChristian Lamparter 				     &ar->cmd, plen + 4);
682a84fab3cSChristian Lamparter 
683a84fab3cSChristian Lamparter 		carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT);
684a84fab3cSChristian Lamparter 	}
685a84fab3cSChristian Lamparter 
686a84fab3cSChristian Lamparter 	/* invalidate to avoid completing the next command prematurely */
687a84fab3cSChristian Lamparter 	spin_lock_bh(&ar->cmd_lock);
688a84fab3cSChristian Lamparter 	ar->readbuf = NULL;
689a84fab3cSChristian Lamparter 	ar->readlen = 0;
690a84fab3cSChristian Lamparter 	spin_unlock_bh(&ar->cmd_lock);
691a84fab3cSChristian Lamparter 
692a84fab3cSChristian Lamparter 	return err;
693a84fab3cSChristian Lamparter }
694a84fab3cSChristian Lamparter 
695a84fab3cSChristian Lamparter void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
696a84fab3cSChristian Lamparter {
697a84fab3cSChristian Lamparter 	struct urb *urb;
698a84fab3cSChristian Lamparter 	struct ar9170_stream *tx_stream;
699a84fab3cSChristian Lamparter 	void *data;
700a84fab3cSChristian Lamparter 	unsigned int len;
701a84fab3cSChristian Lamparter 
702a84fab3cSChristian Lamparter 	if (!IS_STARTED(ar))
703a84fab3cSChristian Lamparter 		goto err_drop;
704a84fab3cSChristian Lamparter 
705a84fab3cSChristian Lamparter 	urb = usb_alloc_urb(0, GFP_ATOMIC);
706a84fab3cSChristian Lamparter 	if (!urb)
707a84fab3cSChristian Lamparter 		goto err_drop;
708a84fab3cSChristian Lamparter 
709a84fab3cSChristian Lamparter 	if (ar->fw.tx_stream) {
710a84fab3cSChristian Lamparter 		tx_stream = (void *) (skb->data - sizeof(*tx_stream));
711a84fab3cSChristian Lamparter 
712a84fab3cSChristian Lamparter 		len = skb->len + sizeof(*tx_stream);
713a84fab3cSChristian Lamparter 		tx_stream->length = cpu_to_le16(len);
714a84fab3cSChristian Lamparter 		tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG);
715a84fab3cSChristian Lamparter 		data = tx_stream;
716a84fab3cSChristian Lamparter 	} else {
717a84fab3cSChristian Lamparter 		data = skb->data;
718a84fab3cSChristian Lamparter 		len = skb->len;
719a84fab3cSChristian Lamparter 	}
720a84fab3cSChristian Lamparter 
721a84fab3cSChristian Lamparter 	usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev,
722a84fab3cSChristian Lamparter 		AR9170_USB_EP_TX), data, len,
723a84fab3cSChristian Lamparter 		carl9170_usb_tx_data_complete, skb);
724a84fab3cSChristian Lamparter 
725a84fab3cSChristian Lamparter 	urb->transfer_flags |= URB_ZERO_PACKET;
726a84fab3cSChristian Lamparter 
727a84fab3cSChristian Lamparter 	usb_anchor_urb(urb, &ar->tx_wait);
728a84fab3cSChristian Lamparter 
729a84fab3cSChristian Lamparter 	usb_free_urb(urb);
730a84fab3cSChristian Lamparter 
731a84fab3cSChristian Lamparter 	carl9170_usb_submit_data_urb(ar);
732a84fab3cSChristian Lamparter 	return;
733a84fab3cSChristian Lamparter 
734a84fab3cSChristian Lamparter err_drop:
735a84fab3cSChristian Lamparter 	carl9170_tx_drop(ar, skb);
736a84fab3cSChristian Lamparter 	carl9170_tx_callback(ar, skb);
737a84fab3cSChristian Lamparter }
738a84fab3cSChristian Lamparter 
739a84fab3cSChristian Lamparter static void carl9170_release_firmware(struct ar9170 *ar)
740a84fab3cSChristian Lamparter {
741a84fab3cSChristian Lamparter 	if (ar->fw.fw) {
742a84fab3cSChristian Lamparter 		release_firmware(ar->fw.fw);
743a84fab3cSChristian Lamparter 		memset(&ar->fw, 0, sizeof(ar->fw));
744a84fab3cSChristian Lamparter 	}
745a84fab3cSChristian Lamparter }
746a84fab3cSChristian Lamparter 
747a84fab3cSChristian Lamparter void carl9170_usb_stop(struct ar9170 *ar)
748a84fab3cSChristian Lamparter {
749a84fab3cSChristian Lamparter 	int ret;
750a84fab3cSChristian Lamparter 
751a84fab3cSChristian Lamparter 	carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED);
752a84fab3cSChristian Lamparter 
753a84fab3cSChristian Lamparter 	ret = carl9170_usb_flush(ar);
754a84fab3cSChristian Lamparter 	if (ret)
755a84fab3cSChristian Lamparter 		dev_err(&ar->udev->dev, "kill pending tx urbs.\n");
756a84fab3cSChristian Lamparter 
757a84fab3cSChristian Lamparter 	usb_poison_anchored_urbs(&ar->tx_anch);
758a84fab3cSChristian Lamparter 	carl9170_usb_handle_tx_err(ar);
759a84fab3cSChristian Lamparter 
760a84fab3cSChristian Lamparter 	/* kill any pending command */
761a84fab3cSChristian Lamparter 	spin_lock_bh(&ar->cmd_lock);
762a84fab3cSChristian Lamparter 	ar->readlen = 0;
763a84fab3cSChristian Lamparter 	spin_unlock_bh(&ar->cmd_lock);
764a84fab3cSChristian Lamparter 	complete_all(&ar->cmd_wait);
765a84fab3cSChristian Lamparter 
766a84fab3cSChristian Lamparter 	/* This is required to prevent an early completion on _start */
767a84fab3cSChristian Lamparter 	INIT_COMPLETION(ar->cmd_wait);
768a84fab3cSChristian Lamparter 
769a84fab3cSChristian Lamparter 	/*
770a84fab3cSChristian Lamparter 	 * Note:
771a84fab3cSChristian Lamparter 	 * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
772a84fab3cSChristian Lamparter 	 * Else we would end up with a unresponsive device...
773a84fab3cSChristian Lamparter 	 */
774a84fab3cSChristian Lamparter }
775a84fab3cSChristian Lamparter 
776a84fab3cSChristian Lamparter int carl9170_usb_open(struct ar9170 *ar)
777a84fab3cSChristian Lamparter {
778a84fab3cSChristian Lamparter 	usb_unpoison_anchored_urbs(&ar->tx_anch);
779a84fab3cSChristian Lamparter 
780a84fab3cSChristian Lamparter 	carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
781a84fab3cSChristian Lamparter 	return 0;
782a84fab3cSChristian Lamparter }
783a84fab3cSChristian Lamparter 
784a84fab3cSChristian Lamparter static int carl9170_usb_load_firmware(struct ar9170 *ar)
785a84fab3cSChristian Lamparter {
786a84fab3cSChristian Lamparter 	const u8 *data;
787a84fab3cSChristian Lamparter 	u8 *buf;
788a84fab3cSChristian Lamparter 	unsigned int transfer;
789a84fab3cSChristian Lamparter 	size_t len;
790a84fab3cSChristian Lamparter 	u32 addr;
791a84fab3cSChristian Lamparter 	int err = 0;
792a84fab3cSChristian Lamparter 
793a84fab3cSChristian Lamparter 	buf = kmalloc(4096, GFP_KERNEL);
794a84fab3cSChristian Lamparter 	if (!buf) {
795a84fab3cSChristian Lamparter 		err = -ENOMEM;
796a84fab3cSChristian Lamparter 		goto err_out;
797a84fab3cSChristian Lamparter 	}
798a84fab3cSChristian Lamparter 
799a84fab3cSChristian Lamparter 	data = ar->fw.fw->data;
800a84fab3cSChristian Lamparter 	len = ar->fw.fw->size;
801a84fab3cSChristian Lamparter 	addr = ar->fw.address;
802a84fab3cSChristian Lamparter 
803a84fab3cSChristian Lamparter 	/* this removes the miniboot image */
804a84fab3cSChristian Lamparter 	data += ar->fw.offset;
805a84fab3cSChristian Lamparter 	len -= ar->fw.offset;
806a84fab3cSChristian Lamparter 
807a84fab3cSChristian Lamparter 	while (len) {
808a84fab3cSChristian Lamparter 		transfer = min_t(unsigned int, len, 4096u);
809a84fab3cSChristian Lamparter 		memcpy(buf, data, transfer);
810a84fab3cSChristian Lamparter 
811a84fab3cSChristian Lamparter 		err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
812a84fab3cSChristian Lamparter 				      0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
813a84fab3cSChristian Lamparter 				      addr >> 8, 0, buf, transfer, 100);
814a84fab3cSChristian Lamparter 
815a84fab3cSChristian Lamparter 		if (err < 0) {
816a84fab3cSChristian Lamparter 			kfree(buf);
817a84fab3cSChristian Lamparter 			goto err_out;
818a84fab3cSChristian Lamparter 		}
819a84fab3cSChristian Lamparter 
820a84fab3cSChristian Lamparter 		len -= transfer;
821a84fab3cSChristian Lamparter 		data += transfer;
822a84fab3cSChristian Lamparter 		addr += transfer;
823a84fab3cSChristian Lamparter 	}
824a84fab3cSChristian Lamparter 	kfree(buf);
825a84fab3cSChristian Lamparter 
826a84fab3cSChristian Lamparter 	err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
827a84fab3cSChristian Lamparter 			      0x31 /* FW DL COMPLETE */,
828a84fab3cSChristian Lamparter 			      0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 200);
829a84fab3cSChristian Lamparter 
830a84fab3cSChristian Lamparter 	if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) {
831a84fab3cSChristian Lamparter 		err = -ETIMEDOUT;
832a84fab3cSChristian Lamparter 		goto err_out;
833a84fab3cSChristian Lamparter 	}
834a84fab3cSChristian Lamparter 
835a84fab3cSChristian Lamparter 	err = carl9170_echo_test(ar, 0x4a110123);
836a84fab3cSChristian Lamparter 	if (err)
837a84fab3cSChristian Lamparter 		goto err_out;
838a84fab3cSChristian Lamparter 
83997e2c402SChristian Lamparter 	/* now, start the command response counter */
840a84fab3cSChristian Lamparter 	ar->cmd_seq = -1;
841a84fab3cSChristian Lamparter 
842a84fab3cSChristian Lamparter 	return 0;
843a84fab3cSChristian Lamparter 
844a84fab3cSChristian Lamparter err_out:
845a84fab3cSChristian Lamparter 	dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err);
846a84fab3cSChristian Lamparter 	return err;
847a84fab3cSChristian Lamparter }
848a84fab3cSChristian Lamparter 
849a84fab3cSChristian Lamparter int carl9170_usb_restart(struct ar9170 *ar)
850a84fab3cSChristian Lamparter {
851a84fab3cSChristian Lamparter 	int err = 0;
852a84fab3cSChristian Lamparter 
853a84fab3cSChristian Lamparter 	if (ar->intf->condition != USB_INTERFACE_BOUND)
854a84fab3cSChristian Lamparter 		return 0;
855a84fab3cSChristian Lamparter 
85697e2c402SChristian Lamparter 	/*
85797e2c402SChristian Lamparter 	 * Disable the command response sequence counter check.
85897e2c402SChristian Lamparter 	 * We already know that the device/firmware is in a bad state.
85997e2c402SChristian Lamparter 	 * So, no extra points are awarded to anyone who reminds the
86097e2c402SChristian Lamparter 	 * driver about that.
86197e2c402SChristian Lamparter 	 */
862a84fab3cSChristian Lamparter 	ar->cmd_seq = -2;
863a84fab3cSChristian Lamparter 
864a84fab3cSChristian Lamparter 	err = carl9170_reboot(ar);
865a84fab3cSChristian Lamparter 
866a84fab3cSChristian Lamparter 	carl9170_usb_stop(ar);
867a84fab3cSChristian Lamparter 
868a84fab3cSChristian Lamparter 	if (err)
869a84fab3cSChristian Lamparter 		goto err_out;
870a84fab3cSChristian Lamparter 
871a84fab3cSChristian Lamparter 	tasklet_schedule(&ar->usb_tasklet);
872a84fab3cSChristian Lamparter 
873a84fab3cSChristian Lamparter 	/* The reboot procedure can take quite a while to complete. */
874a84fab3cSChristian Lamparter 	msleep(1100);
875a84fab3cSChristian Lamparter 
876a84fab3cSChristian Lamparter 	err = carl9170_usb_open(ar);
877a84fab3cSChristian Lamparter 	if (err)
878a84fab3cSChristian Lamparter 		goto err_out;
879a84fab3cSChristian Lamparter 
880a84fab3cSChristian Lamparter 	err = carl9170_usb_load_firmware(ar);
881a84fab3cSChristian Lamparter 	if (err)
882a84fab3cSChristian Lamparter 		goto err_out;
883a84fab3cSChristian Lamparter 
884a84fab3cSChristian Lamparter 	return 0;
885a84fab3cSChristian Lamparter 
886a84fab3cSChristian Lamparter err_out:
887a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
888a84fab3cSChristian Lamparter 	return err;
889a84fab3cSChristian Lamparter }
890a84fab3cSChristian Lamparter 
891a84fab3cSChristian Lamparter void carl9170_usb_reset(struct ar9170 *ar)
892a84fab3cSChristian Lamparter {
893a84fab3cSChristian Lamparter 	/*
894a84fab3cSChristian Lamparter 	 * This is the last resort to get the device going again
895a84fab3cSChristian Lamparter 	 * without any *user replugging action*.
896a84fab3cSChristian Lamparter 	 *
897a84fab3cSChristian Lamparter 	 * But there is a catch: usb_reset really is like a physical
898a84fab3cSChristian Lamparter 	 * *reconnect*. The mac80211 state will be lost in the process.
899a84fab3cSChristian Lamparter 	 * Therefore a userspace application, which is monitoring
900a84fab3cSChristian Lamparter 	 * the link must step in.
901a84fab3cSChristian Lamparter 	 */
902a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
903a84fab3cSChristian Lamparter 
904a84fab3cSChristian Lamparter 	carl9170_usb_stop(ar);
905a84fab3cSChristian Lamparter 
906a84fab3cSChristian Lamparter 	usb_queue_reset_device(ar->intf);
907a84fab3cSChristian Lamparter }
908a84fab3cSChristian Lamparter 
909a84fab3cSChristian Lamparter static int carl9170_usb_init_device(struct ar9170 *ar)
910a84fab3cSChristian Lamparter {
911a84fab3cSChristian Lamparter 	int err;
912a84fab3cSChristian Lamparter 
91397e2c402SChristian Lamparter 	/*
91497e2c402SChristian Lamparter 	 * The carl9170 firmware let's the driver know when it's
91597e2c402SChristian Lamparter 	 * ready for action. But we have to be prepared to gracefully
91697e2c402SChristian Lamparter 	 * handle all spurious [flushed] messages after each (re-)boot.
91797e2c402SChristian Lamparter 	 * Thus the command response counter remains disabled until it
91897e2c402SChristian Lamparter 	 * can be safely synchronized.
91997e2c402SChristian Lamparter 	 */
92097e2c402SChristian Lamparter 	ar->cmd_seq = -2;
92197e2c402SChristian Lamparter 
922a84fab3cSChristian Lamparter 	err = carl9170_usb_send_rx_irq_urb(ar);
923a84fab3cSChristian Lamparter 	if (err)
924a84fab3cSChristian Lamparter 		goto err_out;
925a84fab3cSChristian Lamparter 
926a84fab3cSChristian Lamparter 	err = carl9170_usb_init_rx_bulk_urbs(ar);
927a84fab3cSChristian Lamparter 	if (err)
928a84fab3cSChristian Lamparter 		goto err_unrx;
929a84fab3cSChristian Lamparter 
93097e2c402SChristian Lamparter 	err = carl9170_usb_open(ar);
93197e2c402SChristian Lamparter 	if (err)
93297e2c402SChristian Lamparter 		goto err_unrx;
93397e2c402SChristian Lamparter 
934a84fab3cSChristian Lamparter 	mutex_lock(&ar->mutex);
935a84fab3cSChristian Lamparter 	err = carl9170_usb_load_firmware(ar);
936a84fab3cSChristian Lamparter 	mutex_unlock(&ar->mutex);
937a84fab3cSChristian Lamparter 	if (err)
93897e2c402SChristian Lamparter 		goto err_stop;
939a84fab3cSChristian Lamparter 
940a84fab3cSChristian Lamparter 	return 0;
941a84fab3cSChristian Lamparter 
94297e2c402SChristian Lamparter err_stop:
94397e2c402SChristian Lamparter 	carl9170_usb_stop(ar);
94497e2c402SChristian Lamparter 
945a84fab3cSChristian Lamparter err_unrx:
946a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
947a84fab3cSChristian Lamparter 
948a84fab3cSChristian Lamparter err_out:
949a84fab3cSChristian Lamparter 	return err;
950a84fab3cSChristian Lamparter }
951a84fab3cSChristian Lamparter 
952a84fab3cSChristian Lamparter static void carl9170_usb_firmware_failed(struct ar9170 *ar)
953a84fab3cSChristian Lamparter {
954a84fab3cSChristian Lamparter 	struct device *parent = ar->udev->dev.parent;
955a84fab3cSChristian Lamparter 	struct usb_device *udev;
956a84fab3cSChristian Lamparter 
957a84fab3cSChristian Lamparter 	/*
958a84fab3cSChristian Lamparter 	 * Store a copy of the usb_device pointer locally.
959a84fab3cSChristian Lamparter 	 * This is because device_release_driver initiates
960a84fab3cSChristian Lamparter 	 * carl9170_usb_disconnect, which in turn frees our
961a84fab3cSChristian Lamparter 	 * driver context (ar).
962a84fab3cSChristian Lamparter 	 */
963a84fab3cSChristian Lamparter 	udev = ar->udev;
964a84fab3cSChristian Lamparter 
965a84fab3cSChristian Lamparter 	complete(&ar->fw_load_wait);
966a84fab3cSChristian Lamparter 
967a84fab3cSChristian Lamparter 	/* unbind anything failed */
968a84fab3cSChristian Lamparter 	if (parent)
969a84fab3cSChristian Lamparter 		device_lock(parent);
970a84fab3cSChristian Lamparter 
971a84fab3cSChristian Lamparter 	device_release_driver(&udev->dev);
972a84fab3cSChristian Lamparter 	if (parent)
973a84fab3cSChristian Lamparter 		device_unlock(parent);
974a84fab3cSChristian Lamparter 
975a84fab3cSChristian Lamparter 	usb_put_dev(udev);
976a84fab3cSChristian Lamparter }
977a84fab3cSChristian Lamparter 
978a84fab3cSChristian Lamparter static void carl9170_usb_firmware_finish(struct ar9170 *ar)
979a84fab3cSChristian Lamparter {
980a84fab3cSChristian Lamparter 	int err;
981a84fab3cSChristian Lamparter 
982a84fab3cSChristian Lamparter 	err = carl9170_parse_firmware(ar);
983a84fab3cSChristian Lamparter 	if (err)
984a84fab3cSChristian Lamparter 		goto err_freefw;
985a84fab3cSChristian Lamparter 
986a84fab3cSChristian Lamparter 	err = carl9170_usb_init_device(ar);
987a84fab3cSChristian Lamparter 	if (err)
988a84fab3cSChristian Lamparter 		goto err_freefw;
989a84fab3cSChristian Lamparter 
990a84fab3cSChristian Lamparter 	err = carl9170_register(ar);
991a84fab3cSChristian Lamparter 
992a84fab3cSChristian Lamparter 	carl9170_usb_stop(ar);
993a84fab3cSChristian Lamparter 	if (err)
994a84fab3cSChristian Lamparter 		goto err_unrx;
995a84fab3cSChristian Lamparter 
996a84fab3cSChristian Lamparter 	complete(&ar->fw_load_wait);
997a84fab3cSChristian Lamparter 	usb_put_dev(ar->udev);
998a84fab3cSChristian Lamparter 	return;
999a84fab3cSChristian Lamparter 
1000a84fab3cSChristian Lamparter err_unrx:
1001a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
1002a84fab3cSChristian Lamparter 
1003a84fab3cSChristian Lamparter err_freefw:
1004a84fab3cSChristian Lamparter 	carl9170_release_firmware(ar);
1005a84fab3cSChristian Lamparter 	carl9170_usb_firmware_failed(ar);
1006a84fab3cSChristian Lamparter }
1007a84fab3cSChristian Lamparter 
1008a84fab3cSChristian Lamparter static void carl9170_usb_firmware_step2(const struct firmware *fw,
1009a84fab3cSChristian Lamparter 					void *context)
1010a84fab3cSChristian Lamparter {
1011a84fab3cSChristian Lamparter 	struct ar9170 *ar = context;
1012a84fab3cSChristian Lamparter 
1013a84fab3cSChristian Lamparter 	if (fw) {
1014a84fab3cSChristian Lamparter 		ar->fw.fw = fw;
1015a84fab3cSChristian Lamparter 		carl9170_usb_firmware_finish(ar);
1016a84fab3cSChristian Lamparter 		return;
1017a84fab3cSChristian Lamparter 	}
1018a84fab3cSChristian Lamparter 
1019a84fab3cSChristian Lamparter 	dev_err(&ar->udev->dev, "firmware not found.\n");
1020a84fab3cSChristian Lamparter 	carl9170_usb_firmware_failed(ar);
1021a84fab3cSChristian Lamparter }
1022a84fab3cSChristian Lamparter 
1023a84fab3cSChristian Lamparter static int carl9170_usb_probe(struct usb_interface *intf,
1024a84fab3cSChristian Lamparter 			      const struct usb_device_id *id)
1025a84fab3cSChristian Lamparter {
1026a84fab3cSChristian Lamparter 	struct ar9170 *ar;
1027a84fab3cSChristian Lamparter 	struct usb_device *udev;
1028a84fab3cSChristian Lamparter 	int err;
1029a84fab3cSChristian Lamparter 
1030a84fab3cSChristian Lamparter 	err = usb_reset_device(interface_to_usbdev(intf));
1031a84fab3cSChristian Lamparter 	if (err)
1032a84fab3cSChristian Lamparter 		return err;
1033a84fab3cSChristian Lamparter 
1034a84fab3cSChristian Lamparter 	ar = carl9170_alloc(sizeof(*ar));
1035a84fab3cSChristian Lamparter 	if (IS_ERR(ar))
1036a84fab3cSChristian Lamparter 		return PTR_ERR(ar);
1037a84fab3cSChristian Lamparter 
1038a84fab3cSChristian Lamparter 	udev = interface_to_usbdev(intf);
1039a84fab3cSChristian Lamparter 	usb_get_dev(udev);
1040a84fab3cSChristian Lamparter 	ar->udev = udev;
1041a84fab3cSChristian Lamparter 	ar->intf = intf;
1042a84fab3cSChristian Lamparter 	ar->features = id->driver_info;
1043a84fab3cSChristian Lamparter 
1044a84fab3cSChristian Lamparter 	usb_set_intfdata(intf, ar);
1045a84fab3cSChristian Lamparter 	SET_IEEE80211_DEV(ar->hw, &intf->dev);
1046a84fab3cSChristian Lamparter 
1047a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->rx_anch);
1048a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->rx_pool);
1049a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->rx_work);
1050a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->tx_wait);
1051a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->tx_anch);
1052a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->tx_cmd);
1053a84fab3cSChristian Lamparter 	init_usb_anchor(&ar->tx_err);
1054a84fab3cSChristian Lamparter 	init_completion(&ar->cmd_wait);
1055a84fab3cSChristian Lamparter 	init_completion(&ar->fw_boot_wait);
1056a84fab3cSChristian Lamparter 	init_completion(&ar->fw_load_wait);
1057a84fab3cSChristian Lamparter 	tasklet_init(&ar->usb_tasklet, carl9170_usb_tasklet,
1058a84fab3cSChristian Lamparter 		     (unsigned long)ar);
1059a84fab3cSChristian Lamparter 
1060a84fab3cSChristian Lamparter 	atomic_set(&ar->tx_cmd_urbs, 0);
1061a84fab3cSChristian Lamparter 	atomic_set(&ar->tx_anch_urbs, 0);
1062a84fab3cSChristian Lamparter 	atomic_set(&ar->rx_work_urbs, 0);
1063a84fab3cSChristian Lamparter 	atomic_set(&ar->rx_anch_urbs, 0);
1064a84fab3cSChristian Lamparter 	atomic_set(&ar->rx_pool_urbs, 0);
1065a84fab3cSChristian Lamparter 
1066a84fab3cSChristian Lamparter 	usb_get_dev(ar->udev);
1067a84fab3cSChristian Lamparter 
1068a84fab3cSChristian Lamparter 	carl9170_set_state(ar, CARL9170_STOPPED);
1069a84fab3cSChristian Lamparter 
1070a84fab3cSChristian Lamparter 	return request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME,
1071a84fab3cSChristian Lamparter 		&ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2);
1072a84fab3cSChristian Lamparter }
1073a84fab3cSChristian Lamparter 
1074a84fab3cSChristian Lamparter static void carl9170_usb_disconnect(struct usb_interface *intf)
1075a84fab3cSChristian Lamparter {
1076a84fab3cSChristian Lamparter 	struct ar9170 *ar = usb_get_intfdata(intf);
1077a84fab3cSChristian Lamparter 	struct usb_device *udev;
1078a84fab3cSChristian Lamparter 
1079a84fab3cSChristian Lamparter 	if (WARN_ON(!ar))
1080a84fab3cSChristian Lamparter 		return;
1081a84fab3cSChristian Lamparter 
1082a84fab3cSChristian Lamparter 	udev = ar->udev;
1083a84fab3cSChristian Lamparter 	wait_for_completion(&ar->fw_load_wait);
1084a84fab3cSChristian Lamparter 
1085a84fab3cSChristian Lamparter 	if (IS_INITIALIZED(ar)) {
1086a84fab3cSChristian Lamparter 		carl9170_reboot(ar);
1087a84fab3cSChristian Lamparter 		carl9170_usb_stop(ar);
1088a84fab3cSChristian Lamparter 	}
1089a84fab3cSChristian Lamparter 
1090a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
1091a84fab3cSChristian Lamparter 	carl9170_unregister(ar);
1092a84fab3cSChristian Lamparter 
1093a84fab3cSChristian Lamparter 	usb_set_intfdata(intf, NULL);
1094a84fab3cSChristian Lamparter 
1095a84fab3cSChristian Lamparter 	carl9170_release_firmware(ar);
1096a84fab3cSChristian Lamparter 	carl9170_free(ar);
1097a84fab3cSChristian Lamparter 	usb_put_dev(udev);
1098a84fab3cSChristian Lamparter }
1099a84fab3cSChristian Lamparter 
1100a84fab3cSChristian Lamparter #ifdef CONFIG_PM
1101a84fab3cSChristian Lamparter static int carl9170_usb_suspend(struct usb_interface *intf,
1102a84fab3cSChristian Lamparter 				pm_message_t message)
1103a84fab3cSChristian Lamparter {
1104a84fab3cSChristian Lamparter 	struct ar9170 *ar = usb_get_intfdata(intf);
1105a84fab3cSChristian Lamparter 
1106a84fab3cSChristian Lamparter 	if (!ar)
1107a84fab3cSChristian Lamparter 		return -ENODEV;
1108a84fab3cSChristian Lamparter 
1109a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
1110a84fab3cSChristian Lamparter 
1111a84fab3cSChristian Lamparter 	return 0;
1112a84fab3cSChristian Lamparter }
1113a84fab3cSChristian Lamparter 
1114a84fab3cSChristian Lamparter static int carl9170_usb_resume(struct usb_interface *intf)
1115a84fab3cSChristian Lamparter {
1116a84fab3cSChristian Lamparter 	struct ar9170 *ar = usb_get_intfdata(intf);
1117a84fab3cSChristian Lamparter 	int err;
1118a84fab3cSChristian Lamparter 
1119a84fab3cSChristian Lamparter 	if (!ar)
1120a84fab3cSChristian Lamparter 		return -ENODEV;
1121a84fab3cSChristian Lamparter 
1122a84fab3cSChristian Lamparter 	usb_unpoison_anchored_urbs(&ar->rx_anch);
112397e2c402SChristian Lamparter 	carl9170_set_state(ar, CARL9170_STOPPED);
112497e2c402SChristian Lamparter 
112597e2c402SChristian Lamparter 	/*
112697e2c402SChristian Lamparter 	 * The USB documentation demands that [for suspend] all traffic
112797e2c402SChristian Lamparter 	 * to and from the device has to stop. This would be fine, but
112897e2c402SChristian Lamparter 	 * there's a catch: the device[usb phy] does not come back.
112997e2c402SChristian Lamparter 	 *
113097e2c402SChristian Lamparter 	 * Upon resume the firmware will "kill" itself and the
113197e2c402SChristian Lamparter 	 * boot-code sorts out the magic voodoo.
113297e2c402SChristian Lamparter 	 * Not very nice, but there's not much what could go wrong.
113397e2c402SChristian Lamparter 	 */
113497e2c402SChristian Lamparter 	msleep(1100);
1135a84fab3cSChristian Lamparter 
1136a84fab3cSChristian Lamparter 	err = carl9170_usb_init_device(ar);
1137a84fab3cSChristian Lamparter 	if (err)
1138a84fab3cSChristian Lamparter 		goto err_unrx;
1139a84fab3cSChristian Lamparter 
1140a84fab3cSChristian Lamparter 	return 0;
1141a84fab3cSChristian Lamparter 
1142a84fab3cSChristian Lamparter err_unrx:
1143a84fab3cSChristian Lamparter 	carl9170_usb_cancel_urbs(ar);
1144a84fab3cSChristian Lamparter 
1145a84fab3cSChristian Lamparter 	return err;
1146a84fab3cSChristian Lamparter }
1147a84fab3cSChristian Lamparter #endif /* CONFIG_PM */
1148a84fab3cSChristian Lamparter 
1149a84fab3cSChristian Lamparter static struct usb_driver carl9170_driver = {
1150a84fab3cSChristian Lamparter 	.name = KBUILD_MODNAME,
1151a84fab3cSChristian Lamparter 	.probe = carl9170_usb_probe,
1152a84fab3cSChristian Lamparter 	.disconnect = carl9170_usb_disconnect,
1153a84fab3cSChristian Lamparter 	.id_table = carl9170_usb_ids,
1154a84fab3cSChristian Lamparter 	.soft_unbind = 1,
1155a84fab3cSChristian Lamparter #ifdef CONFIG_PM
1156a84fab3cSChristian Lamparter 	.suspend = carl9170_usb_suspend,
1157a84fab3cSChristian Lamparter 	.resume = carl9170_usb_resume,
115897e2c402SChristian Lamparter 	.reset_resume = carl9170_usb_resume,
1159a84fab3cSChristian Lamparter #endif /* CONFIG_PM */
1160a84fab3cSChristian Lamparter };
1161a84fab3cSChristian Lamparter 
1162a84fab3cSChristian Lamparter static int __init carl9170_usb_init(void)
1163a84fab3cSChristian Lamparter {
1164a84fab3cSChristian Lamparter 	return usb_register(&carl9170_driver);
1165a84fab3cSChristian Lamparter }
1166a84fab3cSChristian Lamparter 
1167a84fab3cSChristian Lamparter static void __exit carl9170_usb_exit(void)
1168a84fab3cSChristian Lamparter {
1169a84fab3cSChristian Lamparter 	usb_deregister(&carl9170_driver);
1170a84fab3cSChristian Lamparter }
1171a84fab3cSChristian Lamparter 
1172a84fab3cSChristian Lamparter module_init(carl9170_usb_init);
1173a84fab3cSChristian Lamparter module_exit(carl9170_usb_exit);
1174