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