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