usb.c 26 KB

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