usb.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /*
  2. * Atheros CARL9170 driver
  3. *
  4. * USB - frontend
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
  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 <johannes@sipsolutions.net>");
  51. MODULE_AUTHOR("Christian Lamparter <chunkeey@googlemail.com>");
  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. * http://wireless.kernel.org/en/users/Drivers/ar9170/devices ),
  62. * whenever you add a new device.
  63. */
  64. static 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 void carl9170_usb_submit_data_urb(struct ar9170 *ar)
  127. {
  128. struct urb *urb;
  129. int err;
  130. if (atomic_inc_return(&ar->tx_anch_urbs) > AR9170_NUM_TX_URBS)
  131. goto err_acc;
  132. urb = usb_get_from_anchor(&ar->tx_wait);
  133. if (!urb)
  134. goto err_acc;
  135. usb_anchor_urb(urb, &ar->tx_anch);
  136. err = usb_submit_urb(urb, GFP_ATOMIC);
  137. if (unlikely(err)) {
  138. if (net_ratelimit()) {
  139. dev_err(&ar->udev->dev, "tx submit failed (%d)\n",
  140. urb->status);
  141. }
  142. usb_unanchor_urb(urb);
  143. usb_anchor_urb(urb, &ar->tx_err);
  144. }
  145. usb_free_urb(urb);
  146. if (likely(err == 0))
  147. return;
  148. err_acc:
  149. atomic_dec(&ar->tx_anch_urbs);
  150. }
  151. static void carl9170_usb_tx_data_complete(struct urb *urb)
  152. {
  153. struct ar9170 *ar = usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  154. if (WARN_ON_ONCE(!ar)) {
  155. dev_kfree_skb_irq(urb->context);
  156. return;
  157. }
  158. atomic_dec(&ar->tx_anch_urbs);
  159. switch (urb->status) {
  160. /* everything is fine */
  161. case 0:
  162. carl9170_tx_callback(ar, (void *)urb->context);
  163. break;
  164. /* disconnect */
  165. case -ENOENT:
  166. case -ECONNRESET:
  167. case -ENODEV:
  168. case -ESHUTDOWN:
  169. /*
  170. * Defer the frame clean-up to the tasklet worker.
  171. * This is necessary, because carl9170_tx_drop
  172. * does not work in an irqsave context.
  173. */
  174. usb_anchor_urb(urb, &ar->tx_err);
  175. return;
  176. /* a random transmission error has occurred? */
  177. default:
  178. if (net_ratelimit()) {
  179. dev_err(&ar->udev->dev, "tx failed (%d)\n",
  180. urb->status);
  181. }
  182. usb_anchor_urb(urb, &ar->tx_err);
  183. break;
  184. }
  185. if (likely(IS_STARTED(ar)))
  186. carl9170_usb_submit_data_urb(ar);
  187. }
  188. static int carl9170_usb_submit_cmd_urb(struct ar9170 *ar)
  189. {
  190. struct urb *urb;
  191. int err;
  192. if (atomic_inc_return(&ar->tx_cmd_urbs) != 1) {
  193. atomic_dec(&ar->tx_cmd_urbs);
  194. return 0;
  195. }
  196. urb = usb_get_from_anchor(&ar->tx_cmd);
  197. if (!urb) {
  198. atomic_dec(&ar->tx_cmd_urbs);
  199. return 0;
  200. }
  201. usb_anchor_urb(urb, &ar->tx_anch);
  202. err = usb_submit_urb(urb, GFP_ATOMIC);
  203. if (unlikely(err)) {
  204. usb_unanchor_urb(urb);
  205. atomic_dec(&ar->tx_cmd_urbs);
  206. }
  207. usb_free_urb(urb);
  208. return err;
  209. }
  210. static void carl9170_usb_cmd_complete(struct urb *urb)
  211. {
  212. struct ar9170 *ar = urb->context;
  213. int err = 0;
  214. if (WARN_ON_ONCE(!ar))
  215. return;
  216. atomic_dec(&ar->tx_cmd_urbs);
  217. switch (urb->status) {
  218. /* everything is fine */
  219. case 0:
  220. break;
  221. /* disconnect */
  222. case -ENOENT:
  223. case -ECONNRESET:
  224. case -ENODEV:
  225. case -ESHUTDOWN:
  226. return;
  227. default:
  228. err = urb->status;
  229. break;
  230. }
  231. if (!IS_INITIALIZED(ar))
  232. return;
  233. if (err)
  234. dev_err(&ar->udev->dev, "submit cmd cb failed (%d).\n", err);
  235. err = carl9170_usb_submit_cmd_urb(ar);
  236. if (err)
  237. dev_err(&ar->udev->dev, "submit cmd failed (%d).\n", err);
  238. }
  239. static void carl9170_usb_rx_irq_complete(struct urb *urb)
  240. {
  241. struct ar9170 *ar = urb->context;
  242. if (WARN_ON_ONCE(!ar))
  243. return;
  244. switch (urb->status) {
  245. /* everything is fine */
  246. case 0:
  247. break;
  248. /* disconnect */
  249. case -ENOENT:
  250. case -ECONNRESET:
  251. case -ENODEV:
  252. case -ESHUTDOWN:
  253. return;
  254. default:
  255. goto resubmit;
  256. }
  257. /*
  258. * While the carl9170 firmware does not use this EP, the
  259. * firmware loader in the EEPROM unfortunately does.
  260. * Therefore we need to be ready to handle out-of-band
  261. * responses and traps in case the firmware crashed and
  262. * the loader took over again.
  263. */
  264. carl9170_handle_command_response(ar, urb->transfer_buffer,
  265. urb->actual_length);
  266. resubmit:
  267. usb_anchor_urb(urb, &ar->rx_anch);
  268. if (unlikely(usb_submit_urb(urb, GFP_ATOMIC)))
  269. usb_unanchor_urb(urb);
  270. }
  271. static int carl9170_usb_submit_rx_urb(struct ar9170 *ar, gfp_t gfp)
  272. {
  273. struct urb *urb;
  274. int err = 0, runs = 0;
  275. while ((atomic_read(&ar->rx_anch_urbs) < AR9170_NUM_RX_URBS) &&
  276. (runs++ < AR9170_NUM_RX_URBS)) {
  277. err = -ENOSPC;
  278. urb = usb_get_from_anchor(&ar->rx_pool);
  279. if (urb) {
  280. usb_anchor_urb(urb, &ar->rx_anch);
  281. err = usb_submit_urb(urb, gfp);
  282. if (unlikely(err)) {
  283. usb_unanchor_urb(urb);
  284. usb_anchor_urb(urb, &ar->rx_pool);
  285. } else {
  286. atomic_dec(&ar->rx_pool_urbs);
  287. atomic_inc(&ar->rx_anch_urbs);
  288. }
  289. usb_free_urb(urb);
  290. }
  291. }
  292. return err;
  293. }
  294. static void carl9170_usb_rx_work(struct ar9170 *ar)
  295. {
  296. struct urb *urb;
  297. int i;
  298. for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
  299. urb = usb_get_from_anchor(&ar->rx_work);
  300. if (!urb)
  301. break;
  302. atomic_dec(&ar->rx_work_urbs);
  303. if (IS_INITIALIZED(ar)) {
  304. carl9170_rx(ar, urb->transfer_buffer,
  305. urb->actual_length);
  306. }
  307. usb_anchor_urb(urb, &ar->rx_pool);
  308. atomic_inc(&ar->rx_pool_urbs);
  309. usb_free_urb(urb);
  310. carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
  311. }
  312. }
  313. void carl9170_usb_handle_tx_err(struct ar9170 *ar)
  314. {
  315. struct urb *urb;
  316. while ((urb = usb_get_from_anchor(&ar->tx_err))) {
  317. struct sk_buff *skb = (void *)urb->context;
  318. carl9170_tx_drop(ar, skb);
  319. carl9170_tx_callback(ar, skb);
  320. usb_free_urb(urb);
  321. }
  322. }
  323. static void carl9170_usb_tasklet(unsigned long data)
  324. {
  325. struct ar9170 *ar = (struct ar9170 *) data;
  326. if (!IS_INITIALIZED(ar))
  327. return;
  328. carl9170_usb_rx_work(ar);
  329. /*
  330. * Strictly speaking: The tx scheduler is not part of the USB system.
  331. * But the rx worker returns frames back to the mac80211-stack and
  332. * this is the _perfect_ place to generate the next transmissions.
  333. */
  334. if (IS_STARTED(ar))
  335. carl9170_tx_scheduler(ar);
  336. }
  337. static void carl9170_usb_rx_complete(struct urb *urb)
  338. {
  339. struct ar9170 *ar = (struct ar9170 *)urb->context;
  340. int err;
  341. if (WARN_ON_ONCE(!ar))
  342. return;
  343. atomic_dec(&ar->rx_anch_urbs);
  344. switch (urb->status) {
  345. case 0:
  346. /* rx path */
  347. usb_anchor_urb(urb, &ar->rx_work);
  348. atomic_inc(&ar->rx_work_urbs);
  349. break;
  350. case -ENOENT:
  351. case -ECONNRESET:
  352. case -ENODEV:
  353. case -ESHUTDOWN:
  354. /* handle disconnect events*/
  355. return;
  356. default:
  357. /* handle all other errors */
  358. usb_anchor_urb(urb, &ar->rx_pool);
  359. atomic_inc(&ar->rx_pool_urbs);
  360. break;
  361. }
  362. err = carl9170_usb_submit_rx_urb(ar, GFP_ATOMIC);
  363. if (unlikely(err)) {
  364. /*
  365. * usb_submit_rx_urb reported a problem.
  366. * In case this is due to a rx buffer shortage,
  367. * elevate the tasklet worker priority to
  368. * the highest available level.
  369. */
  370. tasklet_hi_schedule(&ar->usb_tasklet);
  371. if (atomic_read(&ar->rx_anch_urbs) == 0) {
  372. /*
  373. * The system is too slow to cope with
  374. * the enormous workload. We have simply
  375. * run out of active rx urbs and this
  376. * unfortunately leads to an unpredictable
  377. * device.
  378. */
  379. ieee80211_queue_work(ar->hw, &ar->ping_work);
  380. }
  381. } else {
  382. /*
  383. * Using anything less than _high_ priority absolutely
  384. * kills the rx performance my UP-System...
  385. */
  386. tasklet_hi_schedule(&ar->usb_tasklet);
  387. }
  388. }
  389. static struct urb *carl9170_usb_alloc_rx_urb(struct ar9170 *ar, gfp_t gfp)
  390. {
  391. struct urb *urb;
  392. void *buf;
  393. buf = kmalloc(ar->fw.rx_size, gfp);
  394. if (!buf)
  395. return NULL;
  396. urb = usb_alloc_urb(0, gfp);
  397. if (!urb) {
  398. kfree(buf);
  399. return NULL;
  400. }
  401. usb_fill_bulk_urb(urb, ar->udev, usb_rcvbulkpipe(ar->udev,
  402. AR9170_USB_EP_RX), buf, ar->fw.rx_size,
  403. carl9170_usb_rx_complete, ar);
  404. urb->transfer_flags |= URB_FREE_BUFFER;
  405. return urb;
  406. }
  407. static int carl9170_usb_send_rx_irq_urb(struct ar9170 *ar)
  408. {
  409. struct urb *urb = NULL;
  410. void *ibuf;
  411. int err = -ENOMEM;
  412. urb = usb_alloc_urb(0, GFP_KERNEL);
  413. if (!urb)
  414. goto out;
  415. ibuf = kmalloc(AR9170_USB_EP_CTRL_MAX, GFP_KERNEL);
  416. if (!ibuf)
  417. goto out;
  418. usb_fill_int_urb(urb, ar->udev, usb_rcvintpipe(ar->udev,
  419. AR9170_USB_EP_IRQ), ibuf, AR9170_USB_EP_CTRL_MAX,
  420. carl9170_usb_rx_irq_complete, ar, 1);
  421. urb->transfer_flags |= URB_FREE_BUFFER;
  422. usb_anchor_urb(urb, &ar->rx_anch);
  423. err = usb_submit_urb(urb, GFP_KERNEL);
  424. if (err)
  425. usb_unanchor_urb(urb);
  426. out:
  427. usb_free_urb(urb);
  428. return err;
  429. }
  430. static int carl9170_usb_init_rx_bulk_urbs(struct ar9170 *ar)
  431. {
  432. struct urb *urb;
  433. int i, err = -EINVAL;
  434. /*
  435. * The driver actively maintains a second shadow
  436. * pool for inactive, but fully-prepared rx urbs.
  437. *
  438. * The pool should help the driver to master huge
  439. * workload spikes without running the risk of
  440. * undersupplying the hardware or wasting time by
  441. * processing rx data (streams) inside the urb
  442. * completion (hardirq context).
  443. */
  444. for (i = 0; i < AR9170_NUM_RX_URBS_POOL; i++) {
  445. urb = carl9170_usb_alloc_rx_urb(ar, GFP_KERNEL);
  446. if (!urb) {
  447. err = -ENOMEM;
  448. goto err_out;
  449. }
  450. usb_anchor_urb(urb, &ar->rx_pool);
  451. atomic_inc(&ar->rx_pool_urbs);
  452. usb_free_urb(urb);
  453. }
  454. err = carl9170_usb_submit_rx_urb(ar, GFP_KERNEL);
  455. if (err)
  456. goto err_out;
  457. /* the device now waiting for the firmware. */
  458. carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
  459. return 0;
  460. err_out:
  461. usb_scuttle_anchored_urbs(&ar->rx_pool);
  462. usb_scuttle_anchored_urbs(&ar->rx_work);
  463. usb_kill_anchored_urbs(&ar->rx_anch);
  464. return err;
  465. }
  466. static int carl9170_usb_flush(struct ar9170 *ar)
  467. {
  468. struct urb *urb;
  469. int ret, err = 0;
  470. while ((urb = usb_get_from_anchor(&ar->tx_wait))) {
  471. struct sk_buff *skb = (void *)urb->context;
  472. carl9170_tx_drop(ar, skb);
  473. carl9170_tx_callback(ar, skb);
  474. usb_free_urb(urb);
  475. }
  476. ret = usb_wait_anchor_empty_timeout(&ar->tx_cmd, 1000);
  477. if (ret == 0)
  478. err = -ETIMEDOUT;
  479. /* lets wait a while until the tx - queues are dried out */
  480. ret = usb_wait_anchor_empty_timeout(&ar->tx_anch, 1000);
  481. if (ret == 0)
  482. err = -ETIMEDOUT;
  483. usb_kill_anchored_urbs(&ar->tx_anch);
  484. carl9170_usb_handle_tx_err(ar);
  485. return err;
  486. }
  487. static void carl9170_usb_cancel_urbs(struct ar9170 *ar)
  488. {
  489. int err;
  490. carl9170_set_state(ar, CARL9170_UNKNOWN_STATE);
  491. err = carl9170_usb_flush(ar);
  492. if (err)
  493. dev_err(&ar->udev->dev, "stuck tx urbs!\n");
  494. usb_poison_anchored_urbs(&ar->tx_anch);
  495. carl9170_usb_handle_tx_err(ar);
  496. usb_poison_anchored_urbs(&ar->rx_anch);
  497. tasklet_kill(&ar->usb_tasklet);
  498. usb_scuttle_anchored_urbs(&ar->rx_work);
  499. usb_scuttle_anchored_urbs(&ar->rx_pool);
  500. usb_scuttle_anchored_urbs(&ar->tx_cmd);
  501. }
  502. int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd,
  503. const bool free_buf)
  504. {
  505. struct urb *urb;
  506. int err = 0;
  507. if (!IS_INITIALIZED(ar)) {
  508. err = -EPERM;
  509. goto err_free;
  510. }
  511. if (WARN_ON(cmd->hdr.len > CARL9170_MAX_CMD_LEN - 4)) {
  512. err = -EINVAL;
  513. goto err_free;
  514. }
  515. urb = usb_alloc_urb(0, GFP_ATOMIC);
  516. if (!urb) {
  517. err = -ENOMEM;
  518. goto err_free;
  519. }
  520. if (ar->usb_ep_cmd_is_bulk)
  521. usb_fill_bulk_urb(urb, ar->udev,
  522. usb_sndbulkpipe(ar->udev, AR9170_USB_EP_CMD),
  523. cmd, cmd->hdr.len + 4,
  524. carl9170_usb_cmd_complete, ar);
  525. else
  526. usb_fill_int_urb(urb, ar->udev,
  527. usb_sndintpipe(ar->udev, AR9170_USB_EP_CMD),
  528. cmd, cmd->hdr.len + 4,
  529. carl9170_usb_cmd_complete, ar, 1);
  530. if (free_buf)
  531. urb->transfer_flags |= URB_FREE_BUFFER;
  532. usb_anchor_urb(urb, &ar->tx_cmd);
  533. usb_free_urb(urb);
  534. return carl9170_usb_submit_cmd_urb(ar);
  535. err_free:
  536. if (free_buf)
  537. kfree(cmd);
  538. return err;
  539. }
  540. int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd,
  541. unsigned int plen, void *payload, unsigned int outlen, void *out)
  542. {
  543. int err = -ENOMEM;
  544. if (!IS_ACCEPTING_CMD(ar))
  545. return -EIO;
  546. if (!(cmd & CARL9170_CMD_ASYNC_FLAG))
  547. might_sleep();
  548. ar->cmd.hdr.len = plen;
  549. ar->cmd.hdr.cmd = cmd;
  550. /* writing multiple regs fills this buffer already */
  551. if (plen && payload != (u8 *)(ar->cmd.data))
  552. memcpy(ar->cmd.data, payload, plen);
  553. spin_lock_bh(&ar->cmd_lock);
  554. ar->readbuf = (u8 *)out;
  555. ar->readlen = outlen;
  556. spin_unlock_bh(&ar->cmd_lock);
  557. err = __carl9170_exec_cmd(ar, &ar->cmd, false);
  558. if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) {
  559. err = wait_for_completion_timeout(&ar->cmd_wait, HZ);
  560. if (err == 0) {
  561. err = -ETIMEDOUT;
  562. goto err_unbuf;
  563. }
  564. if (ar->readlen != outlen) {
  565. err = -EMSGSIZE;
  566. goto err_unbuf;
  567. }
  568. }
  569. return 0;
  570. err_unbuf:
  571. /* Maybe the device was removed in the moment we were waiting? */
  572. if (IS_STARTED(ar)) {
  573. dev_err(&ar->udev->dev, "no command feedback "
  574. "received (%d).\n", err);
  575. /* provide some maybe useful debug information */
  576. print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE,
  577. &ar->cmd, plen + 4);
  578. carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT);
  579. }
  580. /* invalidate to avoid completing the next command prematurely */
  581. spin_lock_bh(&ar->cmd_lock);
  582. ar->readbuf = NULL;
  583. ar->readlen = 0;
  584. spin_unlock_bh(&ar->cmd_lock);
  585. return err;
  586. }
  587. void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
  588. {
  589. struct urb *urb;
  590. struct ar9170_stream *tx_stream;
  591. void *data;
  592. unsigned int len;
  593. if (!IS_STARTED(ar))
  594. goto err_drop;
  595. urb = usb_alloc_urb(0, GFP_ATOMIC);
  596. if (!urb)
  597. goto err_drop;
  598. if (ar->fw.tx_stream) {
  599. tx_stream = (void *) (skb->data - sizeof(*tx_stream));
  600. len = skb->len + sizeof(*tx_stream);
  601. tx_stream->length = cpu_to_le16(len);
  602. tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG);
  603. data = tx_stream;
  604. } else {
  605. data = skb->data;
  606. len = skb->len;
  607. }
  608. usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev,
  609. AR9170_USB_EP_TX), data, len,
  610. carl9170_usb_tx_data_complete, skb);
  611. urb->transfer_flags |= URB_ZERO_PACKET;
  612. usb_anchor_urb(urb, &ar->tx_wait);
  613. usb_free_urb(urb);
  614. carl9170_usb_submit_data_urb(ar);
  615. return;
  616. err_drop:
  617. carl9170_tx_drop(ar, skb);
  618. carl9170_tx_callback(ar, skb);
  619. }
  620. static void carl9170_release_firmware(struct ar9170 *ar)
  621. {
  622. if (ar->fw.fw) {
  623. release_firmware(ar->fw.fw);
  624. memset(&ar->fw, 0, sizeof(ar->fw));
  625. }
  626. }
  627. void carl9170_usb_stop(struct ar9170 *ar)
  628. {
  629. int ret;
  630. carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED);
  631. ret = carl9170_usb_flush(ar);
  632. if (ret)
  633. dev_err(&ar->udev->dev, "kill pending tx urbs.\n");
  634. usb_poison_anchored_urbs(&ar->tx_anch);
  635. carl9170_usb_handle_tx_err(ar);
  636. /* kill any pending command */
  637. spin_lock_bh(&ar->cmd_lock);
  638. ar->readlen = 0;
  639. spin_unlock_bh(&ar->cmd_lock);
  640. complete_all(&ar->cmd_wait);
  641. /* This is required to prevent an early completion on _start */
  642. reinit_completion(&ar->cmd_wait);
  643. /*
  644. * Note:
  645. * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
  646. * Else we would end up with a unresponsive device...
  647. */
  648. }
  649. int carl9170_usb_open(struct ar9170 *ar)
  650. {
  651. usb_unpoison_anchored_urbs(&ar->tx_anch);
  652. carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
  653. return 0;
  654. }
  655. static int carl9170_usb_load_firmware(struct ar9170 *ar)
  656. {
  657. const u8 *data;
  658. u8 *buf;
  659. unsigned int transfer;
  660. size_t len;
  661. u32 addr;
  662. int err = 0;
  663. buf = kmalloc(4096, GFP_KERNEL);
  664. if (!buf) {
  665. err = -ENOMEM;
  666. goto err_out;
  667. }
  668. data = ar->fw.fw->data;
  669. len = ar->fw.fw->size;
  670. addr = ar->fw.address;
  671. /* this removes the miniboot image */
  672. data += ar->fw.offset;
  673. len -= ar->fw.offset;
  674. while (len) {
  675. transfer = min_t(unsigned int, len, 4096u);
  676. memcpy(buf, data, transfer);
  677. err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
  678. 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
  679. addr >> 8, 0, buf, transfer, 100);
  680. if (err < 0) {
  681. kfree(buf);
  682. goto err_out;
  683. }
  684. len -= transfer;
  685. data += transfer;
  686. addr += transfer;
  687. }
  688. kfree(buf);
  689. err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
  690. 0x31 /* FW DL COMPLETE */,
  691. 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 200);
  692. if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) {
  693. err = -ETIMEDOUT;
  694. goto err_out;
  695. }
  696. err = carl9170_echo_test(ar, 0x4a110123);
  697. if (err)
  698. goto err_out;
  699. /* now, start the command response counter */
  700. ar->cmd_seq = -1;
  701. return 0;
  702. err_out:
  703. dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err);
  704. return err;
  705. }
  706. int carl9170_usb_restart(struct ar9170 *ar)
  707. {
  708. int err = 0;
  709. if (ar->intf->condition != USB_INTERFACE_BOUND)
  710. return 0;
  711. /*
  712. * Disable the command response sequence counter check.
  713. * We already know that the device/firmware is in a bad state.
  714. * So, no extra points are awarded to anyone who reminds the
  715. * driver about that.
  716. */
  717. ar->cmd_seq = -2;
  718. err = carl9170_reboot(ar);
  719. carl9170_usb_stop(ar);
  720. if (err)
  721. goto err_out;
  722. tasklet_schedule(&ar->usb_tasklet);
  723. /* The reboot procedure can take quite a while to complete. */
  724. msleep(1100);
  725. err = carl9170_usb_open(ar);
  726. if (err)
  727. goto err_out;
  728. err = carl9170_usb_load_firmware(ar);
  729. if (err)
  730. goto err_out;
  731. return 0;
  732. err_out:
  733. carl9170_usb_cancel_urbs(ar);
  734. return err;
  735. }
  736. void carl9170_usb_reset(struct ar9170 *ar)
  737. {
  738. /*
  739. * This is the last resort to get the device going again
  740. * without any *user replugging action*.
  741. *
  742. * But there is a catch: usb_reset really is like a physical
  743. * *reconnect*. The mac80211 state will be lost in the process.
  744. * Therefore a userspace application, which is monitoring
  745. * the link must step in.
  746. */
  747. carl9170_usb_cancel_urbs(ar);
  748. carl9170_usb_stop(ar);
  749. usb_queue_reset_device(ar->intf);
  750. }
  751. static int carl9170_usb_init_device(struct ar9170 *ar)
  752. {
  753. int err;
  754. /*
  755. * The carl9170 firmware let's the driver know when it's
  756. * ready for action. But we have to be prepared to gracefully
  757. * handle all spurious [flushed] messages after each (re-)boot.
  758. * Thus the command response counter remains disabled until it
  759. * can be safely synchronized.
  760. */
  761. ar->cmd_seq = -2;
  762. err = carl9170_usb_send_rx_irq_urb(ar);
  763. if (err)
  764. goto err_out;
  765. err = carl9170_usb_init_rx_bulk_urbs(ar);
  766. if (err)
  767. goto err_unrx;
  768. err = carl9170_usb_open(ar);
  769. if (err)
  770. goto err_unrx;
  771. mutex_lock(&ar->mutex);
  772. err = carl9170_usb_load_firmware(ar);
  773. mutex_unlock(&ar->mutex);
  774. if (err)
  775. goto err_stop;
  776. return 0;
  777. err_stop:
  778. carl9170_usb_stop(ar);
  779. err_unrx:
  780. carl9170_usb_cancel_urbs(ar);
  781. err_out:
  782. return err;
  783. }
  784. static void carl9170_usb_firmware_failed(struct ar9170 *ar)
  785. {
  786. struct device *parent = ar->udev->dev.parent;
  787. struct usb_device *udev;
  788. /*
  789. * Store a copy of the usb_device pointer locally.
  790. * This is because device_release_driver initiates
  791. * carl9170_usb_disconnect, which in turn frees our
  792. * driver context (ar).
  793. */
  794. udev = ar->udev;
  795. complete(&ar->fw_load_wait);
  796. /* unbind anything failed */
  797. if (parent)
  798. device_lock(parent);
  799. device_release_driver(&udev->dev);
  800. if (parent)
  801. device_unlock(parent);
  802. usb_put_dev(udev);
  803. }
  804. static void carl9170_usb_firmware_finish(struct ar9170 *ar)
  805. {
  806. int err;
  807. err = carl9170_parse_firmware(ar);
  808. if (err)
  809. goto err_freefw;
  810. err = carl9170_usb_init_device(ar);
  811. if (err)
  812. goto err_freefw;
  813. err = carl9170_register(ar);
  814. carl9170_usb_stop(ar);
  815. if (err)
  816. goto err_unrx;
  817. complete(&ar->fw_load_wait);
  818. usb_put_dev(ar->udev);
  819. return;
  820. err_unrx:
  821. carl9170_usb_cancel_urbs(ar);
  822. err_freefw:
  823. carl9170_release_firmware(ar);
  824. carl9170_usb_firmware_failed(ar);
  825. }
  826. static void carl9170_usb_firmware_step2(const struct firmware *fw,
  827. void *context)
  828. {
  829. struct ar9170 *ar = context;
  830. if (fw) {
  831. ar->fw.fw = fw;
  832. carl9170_usb_firmware_finish(ar);
  833. return;
  834. }
  835. dev_err(&ar->udev->dev, "firmware not found.\n");
  836. carl9170_usb_firmware_failed(ar);
  837. }
  838. static int carl9170_usb_probe(struct usb_interface *intf,
  839. const struct usb_device_id *id)
  840. {
  841. struct usb_endpoint_descriptor *ep;
  842. struct ar9170 *ar;
  843. struct usb_device *udev;
  844. int i, err;
  845. err = usb_reset_device(interface_to_usbdev(intf));
  846. if (err)
  847. return err;
  848. ar = carl9170_alloc(sizeof(*ar));
  849. if (IS_ERR(ar))
  850. return PTR_ERR(ar);
  851. udev = interface_to_usbdev(intf);
  852. usb_get_dev(udev);
  853. ar->udev = udev;
  854. ar->intf = intf;
  855. ar->features = id->driver_info;
  856. /* We need to remember the type of endpoint 4 because it differs
  857. * between high- and full-speed configuration. The high-speed
  858. * configuration specifies it as interrupt and the full-speed
  859. * configuration as bulk endpoint. This information is required
  860. * later when sending urbs to that endpoint.
  861. */
  862. for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; ++i) {
  863. ep = &intf->cur_altsetting->endpoint[i].desc;
  864. if (usb_endpoint_num(ep) == AR9170_USB_EP_CMD &&
  865. usb_endpoint_dir_out(ep) &&
  866. usb_endpoint_type(ep) == USB_ENDPOINT_XFER_BULK)
  867. ar->usb_ep_cmd_is_bulk = true;
  868. }
  869. usb_set_intfdata(intf, ar);
  870. SET_IEEE80211_DEV(ar->hw, &intf->dev);
  871. init_usb_anchor(&ar->rx_anch);
  872. init_usb_anchor(&ar->rx_pool);
  873. init_usb_anchor(&ar->rx_work);
  874. init_usb_anchor(&ar->tx_wait);
  875. init_usb_anchor(&ar->tx_anch);
  876. init_usb_anchor(&ar->tx_cmd);
  877. init_usb_anchor(&ar->tx_err);
  878. init_completion(&ar->cmd_wait);
  879. init_completion(&ar->fw_boot_wait);
  880. init_completion(&ar->fw_load_wait);
  881. tasklet_init(&ar->usb_tasklet, carl9170_usb_tasklet,
  882. (unsigned long)ar);
  883. atomic_set(&ar->tx_cmd_urbs, 0);
  884. atomic_set(&ar->tx_anch_urbs, 0);
  885. atomic_set(&ar->rx_work_urbs, 0);
  886. atomic_set(&ar->rx_anch_urbs, 0);
  887. atomic_set(&ar->rx_pool_urbs, 0);
  888. usb_get_dev(ar->udev);
  889. carl9170_set_state(ar, CARL9170_STOPPED);
  890. err = request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME,
  891. &ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2);
  892. if (err) {
  893. usb_put_dev(udev);
  894. usb_put_dev(udev);
  895. carl9170_free(ar);
  896. }
  897. return err;
  898. }
  899. static void carl9170_usb_disconnect(struct usb_interface *intf)
  900. {
  901. struct ar9170 *ar = usb_get_intfdata(intf);
  902. struct usb_device *udev;
  903. if (WARN_ON(!ar))
  904. return;
  905. udev = ar->udev;
  906. wait_for_completion(&ar->fw_load_wait);
  907. if (IS_INITIALIZED(ar)) {
  908. carl9170_reboot(ar);
  909. carl9170_usb_stop(ar);
  910. }
  911. carl9170_usb_cancel_urbs(ar);
  912. carl9170_unregister(ar);
  913. usb_set_intfdata(intf, NULL);
  914. carl9170_release_firmware(ar);
  915. carl9170_free(ar);
  916. usb_put_dev(udev);
  917. }
  918. #ifdef CONFIG_PM
  919. static int carl9170_usb_suspend(struct usb_interface *intf,
  920. pm_message_t message)
  921. {
  922. struct ar9170 *ar = usb_get_intfdata(intf);
  923. if (!ar)
  924. return -ENODEV;
  925. carl9170_usb_cancel_urbs(ar);
  926. return 0;
  927. }
  928. static int carl9170_usb_resume(struct usb_interface *intf)
  929. {
  930. struct ar9170 *ar = usb_get_intfdata(intf);
  931. int err;
  932. if (!ar)
  933. return -ENODEV;
  934. usb_unpoison_anchored_urbs(&ar->rx_anch);
  935. carl9170_set_state(ar, CARL9170_STOPPED);
  936. /*
  937. * The USB documentation demands that [for suspend] all traffic
  938. * to and from the device has to stop. This would be fine, but
  939. * there's a catch: the device[usb phy] does not come back.
  940. *
  941. * Upon resume the firmware will "kill" itself and the
  942. * boot-code sorts out the magic voodoo.
  943. * Not very nice, but there's not much what could go wrong.
  944. */
  945. msleep(1100);
  946. err = carl9170_usb_init_device(ar);
  947. if (err)
  948. goto err_unrx;
  949. return 0;
  950. err_unrx:
  951. carl9170_usb_cancel_urbs(ar);
  952. return err;
  953. }
  954. #endif /* CONFIG_PM */
  955. static struct usb_driver carl9170_driver = {
  956. .name = KBUILD_MODNAME,
  957. .probe = carl9170_usb_probe,
  958. .disconnect = carl9170_usb_disconnect,
  959. .id_table = carl9170_usb_ids,
  960. .soft_unbind = 1,
  961. #ifdef CONFIG_PM
  962. .suspend = carl9170_usb_suspend,
  963. .resume = carl9170_usb_resume,
  964. .reset_resume = carl9170_usb_resume,
  965. #endif /* CONFIG_PM */
  966. .disable_hub_initiated_lpm = 1,
  967. };
  968. module_usb_driver(carl9170_driver);