usb.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. unsigned long time_left;
  545. if (!IS_ACCEPTING_CMD(ar))
  546. return -EIO;
  547. if (!(cmd & CARL9170_CMD_ASYNC_FLAG))
  548. might_sleep();
  549. ar->cmd.hdr.len = plen;
  550. ar->cmd.hdr.cmd = cmd;
  551. /* writing multiple regs fills this buffer already */
  552. if (plen && payload != (u8 *)(ar->cmd.data))
  553. memcpy(ar->cmd.data, payload, plen);
  554. spin_lock_bh(&ar->cmd_lock);
  555. ar->readbuf = (u8 *)out;
  556. ar->readlen = outlen;
  557. spin_unlock_bh(&ar->cmd_lock);
  558. reinit_completion(&ar->cmd_wait);
  559. err = __carl9170_exec_cmd(ar, &ar->cmd, false);
  560. if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) {
  561. time_left = wait_for_completion_timeout(&ar->cmd_wait, HZ);
  562. if (time_left == 0) {
  563. err = -ETIMEDOUT;
  564. goto err_unbuf;
  565. }
  566. if (ar->readlen != outlen) {
  567. err = -EMSGSIZE;
  568. goto err_unbuf;
  569. }
  570. }
  571. return 0;
  572. err_unbuf:
  573. /* Maybe the device was removed in the moment we were waiting? */
  574. if (IS_STARTED(ar)) {
  575. dev_err(&ar->udev->dev, "no command feedback "
  576. "received (%d).\n", err);
  577. /* provide some maybe useful debug information */
  578. print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE,
  579. &ar->cmd, plen + 4);
  580. carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT);
  581. }
  582. /* invalidate to avoid completing the next command prematurely */
  583. spin_lock_bh(&ar->cmd_lock);
  584. ar->readbuf = NULL;
  585. ar->readlen = 0;
  586. spin_unlock_bh(&ar->cmd_lock);
  587. return err;
  588. }
  589. void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
  590. {
  591. struct urb *urb;
  592. struct ar9170_stream *tx_stream;
  593. void *data;
  594. unsigned int len;
  595. if (!IS_STARTED(ar))
  596. goto err_drop;
  597. urb = usb_alloc_urb(0, GFP_ATOMIC);
  598. if (!urb)
  599. goto err_drop;
  600. if (ar->fw.tx_stream) {
  601. tx_stream = (void *) (skb->data - sizeof(*tx_stream));
  602. len = skb->len + sizeof(*tx_stream);
  603. tx_stream->length = cpu_to_le16(len);
  604. tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG);
  605. data = tx_stream;
  606. } else {
  607. data = skb->data;
  608. len = skb->len;
  609. }
  610. usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev,
  611. AR9170_USB_EP_TX), data, len,
  612. carl9170_usb_tx_data_complete, skb);
  613. urb->transfer_flags |= URB_ZERO_PACKET;
  614. usb_anchor_urb(urb, &ar->tx_wait);
  615. usb_free_urb(urb);
  616. carl9170_usb_submit_data_urb(ar);
  617. return;
  618. err_drop:
  619. carl9170_tx_drop(ar, skb);
  620. carl9170_tx_callback(ar, skb);
  621. }
  622. static void carl9170_release_firmware(struct ar9170 *ar)
  623. {
  624. if (ar->fw.fw) {
  625. release_firmware(ar->fw.fw);
  626. memset(&ar->fw, 0, sizeof(ar->fw));
  627. }
  628. }
  629. void carl9170_usb_stop(struct ar9170 *ar)
  630. {
  631. int ret;
  632. carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED);
  633. ret = carl9170_usb_flush(ar);
  634. if (ret)
  635. dev_err(&ar->udev->dev, "kill pending tx urbs.\n");
  636. usb_poison_anchored_urbs(&ar->tx_anch);
  637. carl9170_usb_handle_tx_err(ar);
  638. /* kill any pending command */
  639. spin_lock_bh(&ar->cmd_lock);
  640. ar->readlen = 0;
  641. spin_unlock_bh(&ar->cmd_lock);
  642. complete(&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);