Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Atheros CARL9170 driver
0003  *
0004  * USB - frontend
0005  *
0006  * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
0007  * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
0008  *
0009  * This program is free software; you can redistribute it and/or modify
0010  * it under the terms of the GNU General Public License as published by
0011  * the Free Software Foundation; either version 2 of the License, or
0012  * (at your option) any later version.
0013  *
0014  * This program is distributed in the hope that it will be useful,
0015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017  * GNU General Public License for more details.
0018  *
0019  * You should have received a copy of the GNU General Public License
0020  * along with this program; see the file COPYING.  If not, see
0021  * http://www.gnu.org/licenses/.
0022  *
0023  * This file incorporates work covered by the following copyright and
0024  * permission notice:
0025  *    Copyright (c) 2007-2008 Atheros Communications, Inc.
0026  *
0027  *    Permission to use, copy, modify, and/or distribute this software for any
0028  *    purpose with or without fee is hereby granted, provided that the above
0029  *    copyright notice and this permission notice appear in all copies.
0030  *
0031  *    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
0032  *    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
0033  *    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
0034  *    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
0035  *    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
0036  *    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0037  *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0038  */
0039 
0040 #include <linux/module.h>
0041 #include <linux/slab.h>
0042 #include <linux/usb.h>
0043 #include <linux/firmware.h>
0044 #include <linux/etherdevice.h>
0045 #include <linux/device.h>
0046 #include <net/mac80211.h>
0047 #include "carl9170.h"
0048 #include "cmd.h"
0049 #include "hw.h"
0050 #include "fwcmd.h"
0051 
0052 MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
0053 MODULE_AUTHOR("Christian Lamparter <chunkeey@googlemail.com>");
0054 MODULE_LICENSE("GPL");
0055 MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
0056 MODULE_FIRMWARE(CARL9170FW_NAME);
0057 MODULE_ALIAS("ar9170usb");
0058 MODULE_ALIAS("arusb_lnx");
0059 
0060 /*
0061  * Note:
0062  *
0063  * Always update our wiki's device list (located at:
0064  * https://wireless.wiki.kernel.org/en/users/Drivers/ar9170/devices ),
0065  * whenever you add a new device.
0066  */
0067 static const struct usb_device_id carl9170_usb_ids[] = {
0068     /* Atheros 9170 */
0069     { USB_DEVICE(0x0cf3, 0x9170) },
0070     /* Atheros TG121N */
0071     { USB_DEVICE(0x0cf3, 0x1001) },
0072     /* TP-Link TL-WN821N v2 */
0073     { USB_DEVICE(0x0cf3, 0x1002), .driver_info = CARL9170_WPS_BUTTON |
0074          CARL9170_ONE_LED },
0075     /* 3Com Dual Band 802.11n USB Adapter */
0076     { USB_DEVICE(0x0cf3, 0x1010) },
0077     /* H3C Dual Band 802.11n USB Adapter */
0078     { USB_DEVICE(0x0cf3, 0x1011) },
0079     /* Cace Airpcap NX */
0080     { USB_DEVICE(0xcace, 0x0300) },
0081     /* D-Link DWA 160 A1 */
0082     { USB_DEVICE(0x07d1, 0x3c10) },
0083     /* D-Link DWA 160 A2 */
0084     { USB_DEVICE(0x07d1, 0x3a09) },
0085     /* D-Link DWA 130 D */
0086     { USB_DEVICE(0x07d1, 0x3a0f) },
0087     /* Netgear WNA1000 */
0088     { USB_DEVICE(0x0846, 0x9040) },
0089     /* Netgear WNDA3100 (v1) */
0090     { USB_DEVICE(0x0846, 0x9010) },
0091     /* Netgear WN111 v2 */
0092     { USB_DEVICE(0x0846, 0x9001), .driver_info = CARL9170_ONE_LED },
0093     /* Zydas ZD1221 */
0094     { USB_DEVICE(0x0ace, 0x1221) },
0095     /* Proxim ORiNOCO 802.11n USB */
0096     { USB_DEVICE(0x1435, 0x0804) },
0097     /* WNC Generic 11n USB Dongle */
0098     { USB_DEVICE(0x1435, 0x0326) },
0099     /* ZyXEL NWD271N */
0100     { USB_DEVICE(0x0586, 0x3417) },
0101     /* Z-Com UB81 BG */
0102     { USB_DEVICE(0x0cde, 0x0023) },
0103     /* Z-Com UB82 ABG */
0104     { USB_DEVICE(0x0cde, 0x0026) },
0105     /* Sphairon Homelink 1202 */
0106     { USB_DEVICE(0x0cde, 0x0027) },
0107     /* Arcadyan WN7512 */
0108     { USB_DEVICE(0x083a, 0xf522) },
0109     /* Planex GWUS300 */
0110     { USB_DEVICE(0x2019, 0x5304) },
0111     /* IO-Data WNGDNUS2 */
0112     { USB_DEVICE(0x04bb, 0x093f) },
0113     /* NEC WL300NU-G */
0114     { USB_DEVICE(0x0409, 0x0249) },
0115     /* NEC WL300NU-AG */
0116     { USB_DEVICE(0x0409, 0x02b4) },
0117     /* AVM FRITZ!WLAN USB Stick N */
0118     { USB_DEVICE(0x057c, 0x8401) },
0119     /* AVM FRITZ!WLAN USB Stick N 2.4 */
0120     { USB_DEVICE(0x057c, 0x8402) },
0121     /* Qwest/Actiontec 802AIN Wireless N USB Network Adapter */
0122     { USB_DEVICE(0x1668, 0x1200) },
0123     /* Airlive X.USB a/b/g/n */
0124     { USB_DEVICE(0x1b75, 0x9170) },
0125 
0126     /* terminate */
0127     {}
0128 };
0129 MODULE_DEVICE_TABLE(usb, carl9170_usb_ids);
0130 
0131 static struct usb_driver carl9170_driver;
0132 
0133 static void carl9170_usb_submit_data_urb(struct ar9170 *ar)
0134 {
0135     struct urb *urb;
0136     int err;
0137 
0138     if (atomic_inc_return(&ar->tx_anch_urbs) > AR9170_NUM_TX_URBS)
0139         goto err_acc;
0140 
0141     urb = usb_get_from_anchor(&ar->tx_wait);
0142     if (!urb)
0143         goto err_acc;
0144 
0145     usb_anchor_urb(urb, &ar->tx_anch);
0146 
0147     err = usb_submit_urb(urb, GFP_ATOMIC);
0148     if (unlikely(err)) {
0149         if (net_ratelimit()) {
0150             dev_err(&ar->udev->dev, "tx submit failed (%d)\n",
0151                 urb->status);
0152         }
0153 
0154         usb_unanchor_urb(urb);
0155         usb_anchor_urb(urb, &ar->tx_err);
0156     }
0157 
0158     usb_free_urb(urb);
0159 
0160     if (likely(err == 0))
0161         return;
0162 
0163 err_acc:
0164     atomic_dec(&ar->tx_anch_urbs);
0165 }
0166 
0167 static void carl9170_usb_tx_data_complete(struct urb *urb)
0168 {
0169     struct ar9170 *ar = usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
0170 
0171     if (WARN_ON_ONCE(!ar)) {
0172         dev_kfree_skb_irq(urb->context);
0173         return;
0174     }
0175 
0176     atomic_dec(&ar->tx_anch_urbs);
0177 
0178     switch (urb->status) {
0179     /* everything is fine */
0180     case 0:
0181         carl9170_tx_callback(ar, (void *)urb->context);
0182         break;
0183 
0184     /* disconnect */
0185     case -ENOENT:
0186     case -ECONNRESET:
0187     case -ENODEV:
0188     case -ESHUTDOWN:
0189         /*
0190          * Defer the frame clean-up to the tasklet worker.
0191          * This is necessary, because carl9170_tx_drop
0192          * does not work in an irqsave context.
0193          */
0194         usb_anchor_urb(urb, &ar->tx_err);
0195         return;
0196 
0197     /* a random transmission error has occurred? */
0198     default:
0199         if (net_ratelimit()) {
0200             dev_err(&ar->udev->dev, "tx failed (%d)\n",
0201                 urb->status);
0202         }
0203 
0204         usb_anchor_urb(urb, &ar->tx_err);
0205         break;
0206     }
0207 
0208     if (likely(IS_STARTED(ar)))
0209         carl9170_usb_submit_data_urb(ar);
0210 }
0211 
0212 static int carl9170_usb_submit_cmd_urb(struct ar9170 *ar)
0213 {
0214     struct urb *urb;
0215     int err;
0216 
0217     if (atomic_inc_return(&ar->tx_cmd_urbs) != 1) {
0218         atomic_dec(&ar->tx_cmd_urbs);
0219         return 0;
0220     }
0221 
0222     urb = usb_get_from_anchor(&ar->tx_cmd);
0223     if (!urb) {
0224         atomic_dec(&ar->tx_cmd_urbs);
0225         return 0;
0226     }
0227 
0228     usb_anchor_urb(urb, &ar->tx_anch);
0229     err = usb_submit_urb(urb, GFP_ATOMIC);
0230     if (unlikely(err)) {
0231         usb_unanchor_urb(urb);
0232         atomic_dec(&ar->tx_cmd_urbs);
0233     }
0234     usb_free_urb(urb);
0235 
0236     return err;
0237 }
0238 
0239 static void carl9170_usb_cmd_complete(struct urb *urb)
0240 {
0241     struct ar9170 *ar = urb->context;
0242     int err = 0;
0243 
0244     if (WARN_ON_ONCE(!ar))
0245         return;
0246 
0247     atomic_dec(&ar->tx_cmd_urbs);
0248 
0249     switch (urb->status) {
0250     /* everything is fine */
0251     case 0:
0252         break;
0253 
0254     /* disconnect */
0255     case -ENOENT:
0256     case -ECONNRESET:
0257     case -ENODEV:
0258     case -ESHUTDOWN:
0259         return;
0260 
0261     default:
0262         err = urb->status;
0263         break;
0264     }
0265 
0266     if (!IS_INITIALIZED(ar))
0267         return;
0268 
0269     if (err)
0270         dev_err(&ar->udev->dev, "submit cmd cb failed (%d).\n", err);
0271 
0272     err = carl9170_usb_submit_cmd_urb(ar);
0273     if (err)
0274         dev_err(&ar->udev->dev, "submit cmd failed (%d).\n", err);
0275 }
0276 
0277 static void carl9170_usb_rx_irq_complete(struct urb *urb)
0278 {
0279     struct ar9170 *ar = urb->context;
0280 
0281     if (WARN_ON_ONCE(!ar))
0282         return;
0283 
0284     switch (urb->status) {
0285     /* everything is fine */
0286     case 0:
0287         break;
0288 
0289     /* disconnect */
0290     case -ENOENT:
0291     case -ECONNRESET:
0292     case -ENODEV:
0293     case -ESHUTDOWN:
0294         return;
0295 
0296     default:
0297         goto resubmit;
0298     }
0299 
0300     /*
0301      * While the carl9170 firmware does not use this EP, the
0302      * firmware loader in the EEPROM unfortunately does.
0303      * Therefore we need to be ready to handle out-of-band
0304      * responses and traps in case the firmware crashed and
0305      * the loader took over again.
0306      */
0307     carl9170_handle_command_response(ar, urb->transfer_buffer,
0308                      urb->actual_length);
0309 
0310 resubmit:
0311     usb_anchor_urb(urb, &ar->rx_anch);
0312     if (unlikely(usb_submit_urb(urb, GFP_ATOMIC)))
0313         usb_unanchor_urb(urb);
0314 }
0315 
0316 static int carl9170_usb_submit_rx_urb(struct ar9170 *ar, gfp_t gfp)
0317 {
0318     struct urb *urb;
0319     int err = 0, runs = 0;
0320 
0321     while ((atomic_read(&ar->rx_anch_urbs) < AR9170_NUM_RX_URBS) &&
0322         (runs++ < AR9170_NUM_RX_URBS)) {
0323         err = -ENOSPC;
0324         urb = usb_get_from_anchor(&ar->rx_pool);
0325         if (urb) {
0326             usb_anchor_urb(urb, &ar->rx_anch);
0327             err = usb_submit_urb(urb, gfp);
0328             if (unlikely(err)) {
0329                 usb_unanchor_urb(urb);
0330                 usb_anchor_urb(urb, &ar->rx_pool);
0331             } else {
0332                 atomic_dec(&ar->rx_pool_urbs);
0333                 atomic_inc(&ar->rx_anch_urbs);
0334             }
0335             usb_free_urb(urb);
0336         }
0337     }
0338 
0339     return err;
0340 }
0341 
0342 static void carl9170_usb_rx_work(struct ar9170 *ar)
0343 {
0344     struct urb *urb;
0345     int i;
0346 
0347     for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
0348         urb = usb_get_from_anchor(&ar->rx_work);
0349         if (!urb)
0350             break;
0351 
0352         atomic_dec(&ar->rx_work_urbs);
0353         if (IS_INITIALIZED(ar)) {
0354             carl9170_rx(ar, urb->transfer_buffer,
0355                     urb->actual_length);
0356         }
0357 
0358         usb_anchor_urb(urb, &ar->rx_pool);
0359         atomic_inc(&ar->rx_pool_urbs);
0360 
0361         usb_free_urb(urb);
0362 
0363         carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
0364     }
0365 }
0366 
0367 void carl9170_usb_handle_tx_err(struct ar9170 *ar)
0368 {
0369     struct urb *urb;
0370 
0371     while ((urb = usb_get_from_anchor(&ar->tx_err))) {
0372         struct sk_buff *skb = (void *)urb->context;
0373 
0374         carl9170_tx_drop(ar, skb);
0375         carl9170_tx_callback(ar, skb);
0376         usb_free_urb(urb);
0377     }
0378 }
0379 
0380 static void carl9170_usb_tasklet(struct tasklet_struct *t)
0381 {
0382     struct ar9170 *ar = from_tasklet(ar, t, usb_tasklet);
0383 
0384     if (!IS_INITIALIZED(ar))
0385         return;
0386 
0387     carl9170_usb_rx_work(ar);
0388 
0389     /*
0390      * Strictly speaking: The tx scheduler is not part of the USB system.
0391      * But the rx worker returns frames back to the mac80211-stack and
0392      * this is the _perfect_ place to generate the next transmissions.
0393      */
0394     if (IS_STARTED(ar))
0395         carl9170_tx_scheduler(ar);
0396 }
0397 
0398 static void carl9170_usb_rx_complete(struct urb *urb)
0399 {
0400     struct ar9170 *ar = (struct ar9170 *)urb->context;
0401     int err;
0402 
0403     if (WARN_ON_ONCE(!ar))
0404         return;
0405 
0406     atomic_dec(&ar->rx_anch_urbs);
0407 
0408     switch (urb->status) {
0409     case 0:
0410         /* rx path */
0411         usb_anchor_urb(urb, &ar->rx_work);
0412         atomic_inc(&ar->rx_work_urbs);
0413         break;
0414 
0415     case -ENOENT:
0416     case -ECONNRESET:
0417     case -ENODEV:
0418     case -ESHUTDOWN:
0419         /* handle disconnect events*/
0420         return;
0421 
0422     default:
0423         /* handle all other errors */
0424         usb_anchor_urb(urb, &ar->rx_pool);
0425         atomic_inc(&ar->rx_pool_urbs);
0426         break;
0427     }
0428 
0429     err = carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
0430     if (unlikely(err)) {
0431         /*
0432          * usb_submit_rx_urb reported a problem.
0433          * In case this is due to a rx buffer shortage,
0434          * elevate the tasklet worker priority to
0435          * the highest available level.
0436          */
0437         tasklet_hi_schedule(&ar->usb_tasklet);
0438 
0439         if (atomic_read(&ar->rx_anch_urbs) == 0) {
0440             /*
0441              * The system is too slow to cope with
0442              * the enormous workload. We have simply
0443              * run out of active rx urbs and this
0444              * unfortunately leads to an unpredictable
0445              * device.
0446              */
0447 
0448             ieee80211_queue_work(ar->hw, &ar->ping_work);
0449         }
0450     } else {
0451         /*
0452          * Using anything less than _high_ priority absolutely
0453          * kills the rx performance my UP-System...
0454          */
0455         tasklet_hi_schedule(&ar->usb_tasklet);
0456     }
0457 }
0458 
0459 static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp)
0460 {
0461     struct urb *urb;
0462     void *buf;
0463 
0464     buf = kmalloc(ar->fw.rx_size, gfp);
0465     if (!buf)
0466         return NULL;
0467 
0468     urb = usb_alloc_urb(0, gfp);
0469     if (!urb) {
0470         kfree(buf);
0471         return NULL;
0472     }
0473 
0474     usb_fill_bulk_urb(urb, ar->udev, usb_rcvbulkpipe(ar->udev,
0475               AR9170_USB_EP_RX), buf, ar->fw.rx_size,
0476               carl9170_usb_rx_complete, ar);
0477 
0478     urb->transfer_flags |= URB_FREE_BUFFER;
0479 
0480     return urb;
0481 }
0482 
0483 static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar)
0484 {
0485     struct urb *urb = NULL;
0486     void *ibuf;
0487     int err = -ENOMEM;
0488 
0489     urb = usb_alloc_urb(0, GFP_KERNEL);
0490     if (!urb)
0491         goto out;
0492 
0493     ibuf = kmalloc(AR9170_USB_EP_CTRL_MAX, GFP_KERNEL);
0494     if (!ibuf)
0495         goto out;
0496 
0497     usb_fill_int_urb(urb, ar->udev, usb_rcvintpipe(ar->udev,
0498              AR9170_USB_EP_IRQ), ibuf, AR9170_USB_EP_CTRL_MAX,
0499              carl9170_usb_rx_irq_complete, ar, 1);
0500 
0501     urb->transfer_flags |= URB_FREE_BUFFER;
0502 
0503     usb_anchor_urb(urb, &ar->rx_anch);
0504     err = usb_submit_urb(urb, GFP_KERNEL);
0505     if (err)
0506         usb_unanchor_urb(urb);
0507 
0508 out:
0509     usb_free_urb(urb);
0510     return err;
0511 }
0512 
0513 static int carl9170_usb_init_rx_bulk_urbs(struct ar9170 *ar)
0514 {
0515     struct urb *urb;
0516     int i, err = -EINVAL;
0517 
0518     /*
0519      * The driver actively maintains a second shadow
0520      * pool for inactive, but fully-prepared rx urbs.
0521      *
0522      * The pool should help the driver to master huge
0523      * workload spikes without running the risk of
0524      * undersupplying the hardware or wasting time by
0525      * processing rx data (streams) inside the urb
0526      * completion (hardirq context).
0527      */
0528     for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
0529         urb = carl9170_usb_alloc_rx_urb(ar, GFP_KERNEL);
0530         if (!urb) {
0531             err = -ENOMEM;
0532             goto err_out;
0533         }
0534 
0535         usb_anchor_urb(urb, &ar->rx_pool);
0536         atomic_inc(&ar->rx_pool_urbs);
0537         usb_free_urb(urb);
0538     }
0539 
0540     err = carl9170_usb_submit_rx_urb(ar, GFP_KERNEL);
0541     if (err)
0542         goto err_out;
0543 
0544     /* the device now waiting for the firmware. */
0545     carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
0546     return 0;
0547 
0548 err_out:
0549 
0550     usb_scuttle_anchored_urbs(&ar->rx_pool);
0551     usb_scuttle_anchored_urbs(&ar->rx_work);
0552     usb_kill_anchored_urbs(&ar->rx_anch);
0553     return err;
0554 }
0555 
0556 static int carl9170_usb_flush(struct ar9170 *ar)
0557 {
0558     struct urb *urb;
0559     int ret, err = 0;
0560 
0561     while ((urb = usb_get_from_anchor(&ar->tx_wait))) {
0562         struct sk_buff *skb = (void *)urb->context;
0563         carl9170_tx_drop(ar, skb);
0564         carl9170_tx_callback(ar, skb);
0565         usb_free_urb(urb);
0566     }
0567 
0568     ret = usb_wait_anchor_empty_timeout(&ar->tx_cmd, 1000);
0569     if (ret == 0)
0570         err = -ETIMEDOUT;
0571 
0572     /* lets wait a while until the tx - queues are dried out */
0573     ret = usb_wait_anchor_empty_timeout(&ar->tx_anch, 1000);
0574     if (ret == 0)
0575         err = -ETIMEDOUT;
0576 
0577     usb_kill_anchored_urbs(&ar->tx_anch);
0578     carl9170_usb_handle_tx_err(ar);
0579 
0580     return err;
0581 }
0582 
0583 static void carl9170_usb_cancel_urbs(struct ar9170 *ar)
0584 {
0585     int err;
0586 
0587     carl9170_set_state(ar, CARL9170_UNKNOWN_STATE);
0588 
0589     err = carl9170_usb_flush(ar);
0590     if (err)
0591         dev_err(&ar->udev->dev, "stuck tx urbs!\n");
0592 
0593     usb_poison_anchored_urbs(&ar->tx_anch);
0594     carl9170_usb_handle_tx_err(ar);
0595     usb_poison_anchored_urbs(&ar->rx_anch);
0596 
0597     tasklet_kill(&ar->usb_tasklet);
0598 
0599     usb_scuttle_anchored_urbs(&ar->rx_work);
0600     usb_scuttle_anchored_urbs(&ar->rx_pool);
0601     usb_scuttle_anchored_urbs(&ar->tx_cmd);
0602 }
0603 
0604 int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd,
0605             const bool free_buf)
0606 {
0607     struct urb *urb;
0608     int err = 0;
0609 
0610     if (!IS_INITIALIZED(ar)) {
0611         err = -EPERM;
0612         goto err_free;
0613     }
0614 
0615     if (WARN_ON(cmd->hdr.len > CARL9170_MAX_CMD_LEN - 4)) {
0616         err = -EINVAL;
0617         goto err_free;
0618     }
0619 
0620     urb = usb_alloc_urb(0, GFP_ATOMIC);
0621     if (!urb) {
0622         err = -ENOMEM;
0623         goto err_free;
0624     }
0625 
0626     if (ar->usb_ep_cmd_is_bulk)
0627         usb_fill_bulk_urb(urb, ar->udev,
0628                   usb_sndbulkpipe(ar->udev, AR9170_USB_EP_CMD),
0629                   cmd, cmd->hdr.len + 4,
0630                   carl9170_usb_cmd_complete, ar);
0631     else
0632         usb_fill_int_urb(urb, ar->udev,
0633                  usb_sndintpipe(ar->udev, AR9170_USB_EP_CMD),
0634                  cmd, cmd->hdr.len + 4,
0635                  carl9170_usb_cmd_complete, ar, 1);
0636 
0637     if (free_buf)
0638         urb->transfer_flags |= URB_FREE_BUFFER;
0639 
0640     usb_anchor_urb(urb, &ar->tx_cmd);
0641     usb_free_urb(urb);
0642 
0643     return carl9170_usb_submit_cmd_urb(ar);
0644 
0645 err_free:
0646     if (free_buf)
0647         kfree(cmd);
0648 
0649     return err;
0650 }
0651 
0652 int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd,
0653     unsigned int plen, void *payload, unsigned int outlen, void *out)
0654 {
0655     int err = -ENOMEM;
0656     unsigned long time_left;
0657 
0658     if (!IS_ACCEPTING_CMD(ar))
0659         return -EIO;
0660 
0661     if (!(cmd & CARL9170_CMD_ASYNC_FLAG))
0662         might_sleep();
0663 
0664     ar->cmd.hdr.len = plen;
0665     ar->cmd.hdr.cmd = cmd;
0666     /* writing multiple regs fills this buffer already */
0667     if (plen && payload != (u8 *)(ar->cmd.data))
0668         memcpy(ar->cmd.data, payload, plen);
0669 
0670     spin_lock_bh(&ar->cmd_lock);
0671     ar->readbuf = (u8 *)out;
0672     ar->readlen = outlen;
0673     spin_unlock_bh(&ar->cmd_lock);
0674 
0675     reinit_completion(&ar->cmd_wait);
0676     err = __carl9170_exec_cmd(ar, &ar->cmd, false);
0677 
0678     if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) {
0679         time_left = wait_for_completion_timeout(&ar->cmd_wait, HZ);
0680         if (time_left == 0) {
0681             err = -ETIMEDOUT;
0682             goto err_unbuf;
0683         }
0684 
0685         if (ar->readlen != outlen) {
0686             err = -EMSGSIZE;
0687             goto err_unbuf;
0688         }
0689     }
0690 
0691     return 0;
0692 
0693 err_unbuf:
0694     /* Maybe the device was removed in the moment we were waiting? */
0695     if (IS_STARTED(ar)) {
0696         dev_err(&ar->udev->dev, "no command feedback "
0697             "received (%d).\n", err);
0698 
0699         /* provide some maybe useful debug information */
0700         print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE,
0701                      &ar->cmd, plen + 4);
0702 
0703         carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT);
0704     }
0705 
0706     /* invalidate to avoid completing the next command prematurely */
0707     spin_lock_bh(&ar->cmd_lock);
0708     ar->readbuf = NULL;
0709     ar->readlen = 0;
0710     spin_unlock_bh(&ar->cmd_lock);
0711 
0712     return err;
0713 }
0714 
0715 void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
0716 {
0717     struct urb *urb;
0718     struct ar9170_stream *tx_stream;
0719     void *data;
0720     unsigned int len;
0721 
0722     if (!IS_STARTED(ar))
0723         goto err_drop;
0724 
0725     urb = usb_alloc_urb(0, GFP_ATOMIC);
0726     if (!urb)
0727         goto err_drop;
0728 
0729     if (ar->fw.tx_stream) {
0730         tx_stream = (void *) (skb->data - sizeof(*tx_stream));
0731 
0732         len = skb->len + sizeof(*tx_stream);
0733         tx_stream->length = cpu_to_le16(len);
0734         tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG);
0735         data = tx_stream;
0736     } else {
0737         data = skb->data;
0738         len = skb->len;
0739     }
0740 
0741     usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev,
0742         AR9170_USB_EP_TX), data, len,
0743         carl9170_usb_tx_data_complete, skb);
0744 
0745     urb->transfer_flags |= URB_ZERO_PACKET;
0746 
0747     usb_anchor_urb(urb, &ar->tx_wait);
0748 
0749     usb_free_urb(urb);
0750 
0751     carl9170_usb_submit_data_urb(ar);
0752     return;
0753 
0754 err_drop:
0755     carl9170_tx_drop(ar, skb);
0756     carl9170_tx_callback(ar, skb);
0757 }
0758 
0759 static void carl9170_release_firmware(struct ar9170 *ar)
0760 {
0761     if (ar->fw.fw) {
0762         release_firmware(ar->fw.fw);
0763         memset(&ar->fw, 0, sizeof(ar->fw));
0764     }
0765 }
0766 
0767 void carl9170_usb_stop(struct ar9170 *ar)
0768 {
0769     int ret;
0770 
0771     carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED);
0772 
0773     ret = carl9170_usb_flush(ar);
0774     if (ret)
0775         dev_err(&ar->udev->dev, "kill pending tx urbs.\n");
0776 
0777     usb_poison_anchored_urbs(&ar->tx_anch);
0778     carl9170_usb_handle_tx_err(ar);
0779 
0780     /* kill any pending command */
0781     spin_lock_bh(&ar->cmd_lock);
0782     ar->readlen = 0;
0783     spin_unlock_bh(&ar->cmd_lock);
0784     complete(&ar->cmd_wait);
0785 
0786     /*
0787      * Note:
0788      * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
0789      * Else we would end up with a unresponsive device...
0790      */
0791 }
0792 
0793 int carl9170_usb_open(struct ar9170 *ar)
0794 {
0795     usb_unpoison_anchored_urbs(&ar->tx_anch);
0796 
0797     carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
0798     return 0;
0799 }
0800 
0801 static int carl9170_usb_load_firmware(struct ar9170 *ar)
0802 {
0803     const u8 *data;
0804     u8 *buf;
0805     unsigned int transfer;
0806     size_t len;
0807     u32 addr;
0808     int err = 0;
0809 
0810     buf = kmalloc(4096, GFP_KERNEL);
0811     if (!buf) {
0812         err = -ENOMEM;
0813         goto err_out;
0814     }
0815 
0816     data = ar->fw.fw->data;
0817     len = ar->fw.fw->size;
0818     addr = ar->fw.address;
0819 
0820     /* this removes the miniboot image */
0821     data += ar->fw.offset;
0822     len -= ar->fw.offset;
0823 
0824     while (len) {
0825         transfer = min_t(unsigned int, len, 4096u);
0826         memcpy(buf, data, transfer);
0827 
0828         err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
0829                       0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
0830                       addr >> 8, 0, buf, transfer, 100);
0831 
0832         if (err < 0) {
0833             kfree(buf);
0834             goto err_out;
0835         }
0836 
0837         len -= transfer;
0838         data += transfer;
0839         addr += transfer;
0840     }
0841     kfree(buf);
0842 
0843     err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
0844                   0x31 /* FW DL COMPLETE */,
0845                   0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 200);
0846 
0847     if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) {
0848         err = -ETIMEDOUT;
0849         goto err_out;
0850     }
0851 
0852     err = carl9170_echo_test(ar, 0x4a110123);
0853     if (err)
0854         goto err_out;
0855 
0856     /* now, start the command response counter */
0857     ar->cmd_seq = -1;
0858 
0859     return 0;
0860 
0861 err_out:
0862     dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err);
0863     return err;
0864 }
0865 
0866 int carl9170_usb_restart(struct ar9170 *ar)
0867 {
0868     int err = 0;
0869 
0870     if (ar->intf->condition != USB_INTERFACE_BOUND)
0871         return 0;
0872 
0873     /*
0874      * Disable the command response sequence counter check.
0875      * We already know that the device/firmware is in a bad state.
0876      * So, no extra points are awarded to anyone who reminds the
0877      * driver about that.
0878      */
0879     ar->cmd_seq = -2;
0880 
0881     err = carl9170_reboot(ar);
0882 
0883     carl9170_usb_stop(ar);
0884 
0885     if (err)
0886         goto err_out;
0887 
0888     tasklet_schedule(&ar->usb_tasklet);
0889 
0890     /* The reboot procedure can take quite a while to complete. */
0891     msleep(1100);
0892 
0893     err = carl9170_usb_open(ar);
0894     if (err)
0895         goto err_out;
0896 
0897     err = carl9170_usb_load_firmware(ar);
0898     if (err)
0899         goto err_out;
0900 
0901     return 0;
0902 
0903 err_out:
0904     carl9170_usb_cancel_urbs(ar);
0905     return err;
0906 }
0907 
0908 void carl9170_usb_reset(struct ar9170 *ar)
0909 {
0910     /*
0911      * This is the last resort to get the device going again
0912      * without any *user replugging action*.
0913      *
0914      * But there is a catch: usb_reset really is like a physical
0915      * *reconnect*. The mac80211 state will be lost in the process.
0916      * Therefore a userspace application, which is monitoring
0917      * the link must step in.
0918      */
0919     carl9170_usb_cancel_urbs(ar);
0920 
0921     carl9170_usb_stop(ar);
0922 
0923     usb_queue_reset_device(ar->intf);
0924 }
0925 
0926 static int carl9170_usb_init_device(struct ar9170 *ar)
0927 {
0928     int err;
0929 
0930     /*
0931      * The carl9170 firmware let's the driver know when it's
0932      * ready for action. But we have to be prepared to gracefully
0933      * handle all spurious [flushed] messages after each (re-)boot.
0934      * Thus the command response counter remains disabled until it
0935      * can be safely synchronized.
0936      */
0937     ar->cmd_seq = -2;
0938 
0939     err = carl9170_usb_send_rx_irq_urb(ar);
0940     if (err)
0941         goto err_out;
0942 
0943     err = carl9170_usb_init_rx_bulk_urbs(ar);
0944     if (err)
0945         goto err_unrx;
0946 
0947     err = carl9170_usb_open(ar);
0948     if (err)
0949         goto err_unrx;
0950 
0951     mutex_lock(&ar->mutex);
0952     err = carl9170_usb_load_firmware(ar);
0953     mutex_unlock(&ar->mutex);
0954     if (err)
0955         goto err_stop;
0956 
0957     return 0;
0958 
0959 err_stop:
0960     carl9170_usb_stop(ar);
0961 
0962 err_unrx:
0963     carl9170_usb_cancel_urbs(ar);
0964 
0965 err_out:
0966     return err;
0967 }
0968 
0969 static void carl9170_usb_firmware_failed(struct ar9170 *ar)
0970 {
0971     /* Store a copies of the usb_interface and usb_device pointer locally.
0972      * This is because release_driver initiates carl9170_usb_disconnect,
0973      * which in turn frees our driver context (ar).
0974      */
0975     struct usb_interface *intf = ar->intf;
0976     struct usb_device *udev = ar->udev;
0977 
0978     complete(&ar->fw_load_wait);
0979     /* at this point 'ar' could be already freed. Don't use it anymore */
0980     ar = NULL;
0981 
0982     /* unbind anything failed */
0983     usb_lock_device(udev);
0984     usb_driver_release_interface(&carl9170_driver, intf);
0985     usb_unlock_device(udev);
0986 
0987     usb_put_intf(intf);
0988 }
0989 
0990 static void carl9170_usb_firmware_finish(struct ar9170 *ar)
0991 {
0992     struct usb_interface *intf = ar->intf;
0993     int err;
0994 
0995     err = carl9170_parse_firmware(ar);
0996     if (err)
0997         goto err_freefw;
0998 
0999     err = carl9170_usb_init_device(ar);
1000     if (err)
1001         goto err_freefw;
1002 
1003     err = carl9170_register(ar);
1004 
1005     carl9170_usb_stop(ar);
1006     if (err)
1007         goto err_unrx;
1008 
1009     complete(&ar->fw_load_wait);
1010     usb_put_intf(intf);
1011     return;
1012 
1013 err_unrx:
1014     carl9170_usb_cancel_urbs(ar);
1015 
1016 err_freefw:
1017     carl9170_release_firmware(ar);
1018     carl9170_usb_firmware_failed(ar);
1019 }
1020 
1021 static void carl9170_usb_firmware_step2(const struct firmware *fw,
1022                     void *context)
1023 {
1024     struct ar9170 *ar = context;
1025 
1026     if (fw) {
1027         ar->fw.fw = fw;
1028         carl9170_usb_firmware_finish(ar);
1029         return;
1030     }
1031 
1032     dev_err(&ar->udev->dev, "firmware not found.\n");
1033     carl9170_usb_firmware_failed(ar);
1034 }
1035 
1036 static int carl9170_usb_probe(struct usb_interface *intf,
1037                   const struct usb_device_id *id)
1038 {
1039     struct usb_endpoint_descriptor *ep;
1040     struct ar9170 *ar;
1041     struct usb_device *udev;
1042     int i, err;
1043 
1044     err = usb_reset_device(interface_to_usbdev(intf));
1045     if (err)
1046         return err;
1047 
1048     ar = carl9170_alloc(sizeof(*ar));
1049     if (IS_ERR(ar))
1050         return PTR_ERR(ar);
1051 
1052     udev = interface_to_usbdev(intf);
1053     ar->udev = udev;
1054     ar->intf = intf;
1055     ar->features = id->driver_info;
1056 
1057     /* We need to remember the type of endpoint 4 because it differs
1058      * between high- and full-speed configuration. The high-speed
1059      * configuration specifies it as interrupt and the full-speed
1060      * configuration as bulk endpoint. This information is required
1061      * later when sending urbs to that endpoint.
1062      */
1063     for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; ++i) {
1064         ep = &intf->cur_altsetting->endpoint[i].desc;
1065 
1066         if (usb_endpoint_num(ep) == AR9170_USB_EP_CMD &&
1067             usb_endpoint_dir_out(ep) &&
1068             usb_endpoint_type(ep) == USB_ENDPOINT_XFER_BULK)
1069             ar->usb_ep_cmd_is_bulk = true;
1070     }
1071 
1072     usb_set_intfdata(intf, ar);
1073     SET_IEEE80211_DEV(ar->hw, &intf->dev);
1074 
1075     init_usb_anchor(&ar->rx_anch);
1076     init_usb_anchor(&ar->rx_pool);
1077     init_usb_anchor(&ar->rx_work);
1078     init_usb_anchor(&ar->tx_wait);
1079     init_usb_anchor(&ar->tx_anch);
1080     init_usb_anchor(&ar->tx_cmd);
1081     init_usb_anchor(&ar->tx_err);
1082     init_completion(&ar->cmd_wait);
1083     init_completion(&ar->fw_boot_wait);
1084     init_completion(&ar->fw_load_wait);
1085     tasklet_setup(&ar->usb_tasklet, carl9170_usb_tasklet);
1086 
1087     atomic_set(&ar->tx_cmd_urbs, 0);
1088     atomic_set(&ar->tx_anch_urbs, 0);
1089     atomic_set(&ar->rx_work_urbs, 0);
1090     atomic_set(&ar->rx_anch_urbs, 0);
1091     atomic_set(&ar->rx_pool_urbs, 0);
1092 
1093     usb_get_intf(intf);
1094 
1095     carl9170_set_state(ar, CARL9170_STOPPED);
1096 
1097     err = request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME,
1098         &ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2);
1099     if (err) {
1100         usb_put_intf(intf);
1101         carl9170_free(ar);
1102     }
1103     return err;
1104 }
1105 
1106 static void carl9170_usb_disconnect(struct usb_interface *intf)
1107 {
1108     struct ar9170 *ar = usb_get_intfdata(intf);
1109 
1110     if (WARN_ON(!ar))
1111         return;
1112 
1113     wait_for_completion(&ar->fw_load_wait);
1114 
1115     if (IS_INITIALIZED(ar)) {
1116         carl9170_reboot(ar);
1117         carl9170_usb_stop(ar);
1118     }
1119 
1120     carl9170_usb_cancel_urbs(ar);
1121     carl9170_unregister(ar);
1122 
1123     usb_set_intfdata(intf, NULL);
1124 
1125     carl9170_release_firmware(ar);
1126     carl9170_free(ar);
1127 }
1128 
1129 #ifdef CONFIG_PM
1130 static int carl9170_usb_suspend(struct usb_interface *intf,
1131                 pm_message_t message)
1132 {
1133     struct ar9170 *ar = usb_get_intfdata(intf);
1134 
1135     if (!ar)
1136         return -ENODEV;
1137 
1138     carl9170_usb_cancel_urbs(ar);
1139 
1140     return 0;
1141 }
1142 
1143 static int carl9170_usb_resume(struct usb_interface *intf)
1144 {
1145     struct ar9170 *ar = usb_get_intfdata(intf);
1146     int err;
1147 
1148     if (!ar)
1149         return -ENODEV;
1150 
1151     usb_unpoison_anchored_urbs(&ar->rx_anch);
1152     carl9170_set_state(ar, CARL9170_STOPPED);
1153 
1154     /*
1155      * The USB documentation demands that [for suspend] all traffic
1156      * to and from the device has to stop. This would be fine, but
1157      * there's a catch: the device[usb phy] does not come back.
1158      *
1159      * Upon resume the firmware will "kill" itself and the
1160      * boot-code sorts out the magic voodoo.
1161      * Not very nice, but there's not much what could go wrong.
1162      */
1163     msleep(1100);
1164 
1165     err = carl9170_usb_init_device(ar);
1166     if (err)
1167         goto err_unrx;
1168 
1169     return 0;
1170 
1171 err_unrx:
1172     carl9170_usb_cancel_urbs(ar);
1173 
1174     return err;
1175 }
1176 #endif /* CONFIG_PM */
1177 
1178 static struct usb_driver carl9170_driver = {
1179     .name = KBUILD_MODNAME,
1180     .probe = carl9170_usb_probe,
1181     .disconnect = carl9170_usb_disconnect,
1182     .id_table = carl9170_usb_ids,
1183     .soft_unbind = 1,
1184 #ifdef CONFIG_PM
1185     .suspend = carl9170_usb_suspend,
1186     .resume = carl9170_usb_resume,
1187     .reset_resume = carl9170_usb_resume,
1188 #endif /* CONFIG_PM */
1189     .disable_hub_initiated_lpm = 1,
1190 };
1191 
1192 module_usb_driver(carl9170_driver);