usb.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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. usb_fill_int_urb(urb, ar->udev, usb_sndintpipe(ar->udev,
  521. AR9170_USB_EP_CMD), cmd, cmd->hdr.len + 4,
  522. carl9170_usb_cmd_complete, ar, 1);
  523. if (free_buf)
  524. urb->transfer_flags |= URB_FREE_BUFFER;
  525. usb_anchor_urb(urb, &ar->tx_cmd);
  526. usb_free_urb(urb);
  527. return carl9170_usb_submit_cmd_urb(ar);
  528. err_free:
  529. if (free_buf)
  530. kfree(cmd);
  531. return err;
  532. }
  533. int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids cmd,
  534. unsigned int plen, void *payload, unsigned int outlen, void *out)
  535. {
  536. int err = -ENOMEM;
  537. if (!IS_ACCEPTING_CMD(ar))
  538. return -EIO;
  539. if (!(cmd & CARL9170_CMD_ASYNC_FLAG))
  540. might_sleep();
  541. ar->cmd.hdr.len = plen;
  542. ar->cmd.hdr.cmd = cmd;
  543. /* writing multiple regs fills this buffer already */
  544. if (plen && payload != (u8 *)(ar->cmd.data))
  545. memcpy(ar->cmd.data, payload, plen);
  546. spin_lock_bh(&ar->cmd_lock);
  547. ar->readbuf = (u8 *)out;
  548. ar->readlen = outlen;
  549. spin_unlock_bh(&ar->cmd_lock);
  550. err = __carl9170_exec_cmd(ar, &ar->cmd, false);
  551. if (!(cmd & CARL9170_CMD_ASYNC_FLAG)) {
  552. err = wait_for_completion_timeout(&ar->cmd_wait, HZ);
  553. if (err == 0) {
  554. err = -ETIMEDOUT;
  555. goto err_unbuf;
  556. }
  557. if (ar->readlen != outlen) {
  558. err = -EMSGSIZE;
  559. goto err_unbuf;
  560. }
  561. }
  562. return 0;
  563. err_unbuf:
  564. /* Maybe the device was removed in the moment we were waiting? */
  565. if (IS_STARTED(ar)) {
  566. dev_err(&ar->udev->dev, "no command feedback "
  567. "received (%d).\n", err);
  568. /* provide some maybe useful debug information */
  569. print_hex_dump_bytes("carl9170 cmd: ", DUMP_PREFIX_NONE,
  570. &ar->cmd, plen + 4);
  571. carl9170_restart(ar, CARL9170_RR_COMMAND_TIMEOUT);
  572. }
  573. /* invalidate to avoid completing the next command prematurely */
  574. spin_lock_bh(&ar->cmd_lock);
  575. ar->readbuf = NULL;
  576. ar->readlen = 0;
  577. spin_unlock_bh(&ar->cmd_lock);
  578. return err;
  579. }
  580. void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
  581. {
  582. struct urb *urb;
  583. struct ar9170_stream *tx_stream;
  584. void *data;
  585. unsigned int len;
  586. if (!IS_STARTED(ar))
  587. goto err_drop;
  588. urb = usb_alloc_urb(0, GFP_ATOMIC);
  589. if (!urb)
  590. goto err_drop;
  591. if (ar->fw.tx_stream) {
  592. tx_stream = (void *) (skb->data - sizeof(*tx_stream));
  593. len = skb->len + sizeof(*tx_stream);
  594. tx_stream->length = cpu_to_le16(len);
  595. tx_stream->tag = cpu_to_le16(AR9170_TX_STREAM_TAG);
  596. data = tx_stream;
  597. } else {
  598. data = skb->data;
  599. len = skb->len;
  600. }
  601. usb_fill_bulk_urb(urb, ar->udev, usb_sndbulkpipe(ar->udev,
  602. AR9170_USB_EP_TX), data, len,
  603. carl9170_usb_tx_data_complete, skb);
  604. urb->transfer_flags |= URB_ZERO_PACKET;
  605. usb_anchor_urb(urb, &ar->tx_wait);
  606. usb_free_urb(urb);
  607. carl9170_usb_submit_data_urb(ar);
  608. return;
  609. err_drop:
  610. carl9170_tx_drop(ar, skb);
  611. carl9170_tx_callback(ar, skb);
  612. }
  613. static void carl9170_release_firmware(struct ar9170 *ar)
  614. {
  615. if (ar->fw.fw) {
  616. release_firmware(ar->fw.fw);
  617. memset(&ar->fw, 0, sizeof(ar->fw));
  618. }
  619. }
  620. void carl9170_usb_stop(struct ar9170 *ar)
  621. {
  622. int ret;
  623. carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STOPPED);
  624. ret = carl9170_usb_flush(ar);
  625. if (ret)
  626. dev_err(&ar->udev->dev, "kill pending tx urbs.\n");
  627. usb_poison_anchored_urbs(&ar->tx_anch);
  628. carl9170_usb_handle_tx_err(ar);
  629. /* kill any pending command */
  630. spin_lock_bh(&ar->cmd_lock);
  631. ar->readlen = 0;
  632. spin_unlock_bh(&ar->cmd_lock);
  633. complete_all(&ar->cmd_wait);
  634. /* This is required to prevent an early completion on _start */
  635. INIT_COMPLETION(ar->cmd_wait);
  636. /*
  637. * Note:
  638. * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
  639. * Else we would end up with a unresponsive device...
  640. */
  641. }
  642. int carl9170_usb_open(struct ar9170 *ar)
  643. {
  644. usb_unpoison_anchored_urbs(&ar->tx_anch);
  645. carl9170_set_state_when(ar, CARL9170_STOPPED, CARL9170_IDLE);
  646. return 0;
  647. }
  648. static int carl9170_usb_load_firmware(struct ar9170 *ar)
  649. {
  650. const u8 *data;
  651. u8 *buf;
  652. unsigned int transfer;
  653. size_t len;
  654. u32 addr;
  655. int err = 0;
  656. buf = kmalloc(4096, GFP_KERNEL);
  657. if (!buf) {
  658. err = -ENOMEM;
  659. goto err_out;
  660. }
  661. data = ar->fw.fw->data;
  662. len = ar->fw.fw->size;
  663. addr = ar->fw.address;
  664. /* this removes the miniboot image */
  665. data += ar->fw.offset;
  666. len -= ar->fw.offset;
  667. while (len) {
  668. transfer = min_t(unsigned int, len, 4096u);
  669. memcpy(buf, data, transfer);
  670. err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
  671. 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
  672. addr >> 8, 0, buf, transfer, 100);
  673. if (err < 0) {
  674. kfree(buf);
  675. goto err_out;
  676. }
  677. len -= transfer;
  678. data += transfer;
  679. addr += transfer;
  680. }
  681. kfree(buf);
  682. err = usb_control_msg(ar->udev, usb_sndctrlpipe(ar->udev, 0),
  683. 0x31 /* FW DL COMPLETE */,
  684. 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 200);
  685. if (wait_for_completion_timeout(&ar->fw_boot_wait, HZ) == 0) {
  686. err = -ETIMEDOUT;
  687. goto err_out;
  688. }
  689. err = carl9170_echo_test(ar, 0x4a110123);
  690. if (err)
  691. goto err_out;
  692. /* now, start the command response counter */
  693. ar->cmd_seq = -1;
  694. return 0;
  695. err_out:
  696. dev_err(&ar->udev->dev, "firmware upload failed (%d).\n", err);
  697. return err;
  698. }
  699. int carl9170_usb_restart(struct ar9170 *ar)
  700. {
  701. int err = 0;
  702. if (ar->intf->condition != USB_INTERFACE_BOUND)
  703. return 0;
  704. /*
  705. * Disable the command response sequence counter check.
  706. * We already know that the device/firmware is in a bad state.
  707. * So, no extra points are awarded to anyone who reminds the
  708. * driver about that.
  709. */
  710. ar->cmd_seq = -2;
  711. err = carl9170_reboot(ar);
  712. carl9170_usb_stop(ar);
  713. if (err)
  714. goto err_out;
  715. tasklet_schedule(&ar->usb_tasklet);
  716. /* The reboot procedure can take quite a while to complete. */
  717. msleep(1100);
  718. err = carl9170_usb_open(ar);
  719. if (err)
  720. goto err_out;
  721. err = carl9170_usb_load_firmware(ar);
  722. if (err)
  723. goto err_out;
  724. return 0;
  725. err_out:
  726. carl9170_usb_cancel_urbs(ar);
  727. return err;
  728. }
  729. void carl9170_usb_reset(struct ar9170 *ar)
  730. {
  731. /*
  732. * This is the last resort to get the device going again
  733. * without any *user replugging action*.
  734. *
  735. * But there is a catch: usb_reset really is like a physical
  736. * *reconnect*. The mac80211 state will be lost in the process.
  737. * Therefore a userspace application, which is monitoring
  738. * the link must step in.
  739. */
  740. carl9170_usb_cancel_urbs(ar);
  741. carl9170_usb_stop(ar);
  742. usb_queue_reset_device(ar->intf);
  743. }
  744. static int carl9170_usb_init_device(struct ar9170 *ar)
  745. {
  746. int err;
  747. /*
  748. * The carl9170 firmware let's the driver know when it's
  749. * ready for action. But we have to be prepared to gracefully
  750. * handle all spurious [flushed] messages after each (re-)boot.
  751. * Thus the command response counter remains disabled until it
  752. * can be safely synchronized.
  753. */
  754. ar->cmd_seq = -2;
  755. err = carl9170_usb_send_rx_irq_urb(ar);
  756. if (err)
  757. goto err_out;
  758. err = carl9170_usb_init_rx_bulk_urbs(ar);
  759. if (err)
  760. goto err_unrx;
  761. err = carl9170_usb_open(ar);
  762. if (err)
  763. goto err_unrx;
  764. mutex_lock(&ar->mutex);
  765. err = carl9170_usb_load_firmware(ar);
  766. mutex_unlock(&ar->mutex);
  767. if (err)
  768. goto err_stop;
  769. return 0;
  770. err_stop:
  771. carl9170_usb_stop(ar);
  772. err_unrx:
  773. carl9170_usb_cancel_urbs(ar);
  774. err_out:
  775. return err;
  776. }
  777. static void carl9170_usb_firmware_failed(struct ar9170 *ar)
  778. {
  779. struct device *parent = ar->udev->dev.parent;
  780. struct usb_device *udev;
  781. /*
  782. * Store a copy of the usb_device pointer locally.
  783. * This is because device_release_driver initiates
  784. * carl9170_usb_disconnect, which in turn frees our
  785. * driver context (ar).
  786. */
  787. udev = ar->udev;
  788. complete(&ar->fw_load_wait);
  789. /* unbind anything failed */
  790. if (parent)
  791. device_lock(parent);
  792. device_release_driver(&udev->dev);
  793. if (parent)
  794. device_unlock(parent);
  795. usb_put_dev(udev);
  796. }
  797. static void carl9170_usb_firmware_finish(struct ar9170 *ar)
  798. {
  799. int err;
  800. err = carl9170_parse_firmware(ar);
  801. if (err)
  802. goto err_freefw;
  803. err = carl9170_usb_init_device(ar);
  804. if (err)
  805. goto err_freefw;
  806. err = carl9170_register(ar);
  807. carl9170_usb_stop(ar);
  808. if (err)
  809. goto err_unrx;
  810. complete(&ar->fw_load_wait);
  811. usb_put_dev(ar->udev);
  812. return;
  813. err_unrx:
  814. carl9170_usb_cancel_urbs(ar);
  815. err_freefw:
  816. carl9170_release_firmware(ar);
  817. carl9170_usb_firmware_failed(ar);
  818. }
  819. static void carl9170_usb_firmware_step2(const struct firmware *fw,
  820. void *context)
  821. {
  822. struct ar9170 *ar = context;
  823. if (fw) {
  824. ar->fw.fw = fw;
  825. carl9170_usb_firmware_finish(ar);
  826. return;
  827. }
  828. dev_err(&ar->udev->dev, "firmware not found.\n");
  829. carl9170_usb_firmware_failed(ar);
  830. }
  831. static int carl9170_usb_probe(struct usb_interface *intf,
  832. const struct usb_device_id *id)
  833. {
  834. struct ar9170 *ar;
  835. struct usb_device *udev;
  836. int err;
  837. err = usb_reset_device(interface_to_usbdev(intf));
  838. if (err)
  839. return err;
  840. ar = carl9170_alloc(sizeof(*ar));
  841. if (IS_ERR(ar))
  842. return PTR_ERR(ar);
  843. udev = interface_to_usbdev(intf);
  844. usb_get_dev(udev);
  845. ar->udev = udev;
  846. ar->intf = intf;
  847. ar->features = id->driver_info;
  848. usb_set_intfdata(intf, ar);
  849. SET_IEEE80211_DEV(ar->hw, &intf->dev);
  850. init_usb_anchor(&ar->rx_anch);
  851. init_usb_anchor(&ar->rx_pool);
  852. init_usb_anchor(&ar->rx_work);
  853. init_usb_anchor(&ar->tx_wait);
  854. init_usb_anchor(&ar->tx_anch);
  855. init_usb_anchor(&ar->tx_cmd);
  856. init_usb_anchor(&ar->tx_err);
  857. init_completion(&ar->cmd_wait);
  858. init_completion(&ar->fw_boot_wait);
  859. init_completion(&ar->fw_load_wait);
  860. tasklet_init(&ar->usb_tasklet, carl9170_usb_tasklet,
  861. (unsigned long)ar);
  862. atomic_set(&ar->tx_cmd_urbs, 0);
  863. atomic_set(&ar->tx_anch_urbs, 0);
  864. atomic_set(&ar->rx_work_urbs, 0);
  865. atomic_set(&ar->rx_anch_urbs, 0);
  866. atomic_set(&ar->rx_pool_urbs, 0);
  867. usb_get_dev(ar->udev);
  868. carl9170_set_state(ar, CARL9170_STOPPED);
  869. return request_firmware_nowait(THIS_MODULE, 1, CARL9170FW_NAME,
  870. &ar->udev->dev, GFP_KERNEL, ar, carl9170_usb_firmware_step2);
  871. }
  872. static void carl9170_usb_disconnect(struct usb_interface *intf)
  873. {
  874. struct ar9170 *ar = usb_get_intfdata(intf);
  875. struct usb_device *udev;
  876. if (WARN_ON(!ar))
  877. return;
  878. udev = ar->udev;
  879. wait_for_completion(&ar->fw_load_wait);
  880. if (IS_INITIALIZED(ar)) {
  881. carl9170_reboot(ar);
  882. carl9170_usb_stop(ar);
  883. }
  884. carl9170_usb_cancel_urbs(ar);
  885. carl9170_unregister(ar);
  886. usb_set_intfdata(intf, NULL);
  887. carl9170_release_firmware(ar);
  888. carl9170_free(ar);
  889. usb_put_dev(udev);
  890. }
  891. #ifdef CONFIG_PM
  892. static int carl9170_usb_suspend(struct usb_interface *intf,
  893. pm_message_t message)
  894. {
  895. struct ar9170 *ar = usb_get_intfdata(intf);
  896. if (!ar)
  897. return -ENODEV;
  898. carl9170_usb_cancel_urbs(ar);
  899. return 0;
  900. }
  901. static int carl9170_usb_resume(struct usb_interface *intf)
  902. {
  903. struct ar9170 *ar = usb_get_intfdata(intf);
  904. int err;
  905. if (!ar)
  906. return -ENODEV;
  907. usb_unpoison_anchored_urbs(&ar->rx_anch);
  908. carl9170_set_state(ar, CARL9170_STOPPED);
  909. /*
  910. * The USB documentation demands that [for suspend] all traffic
  911. * to and from the device has to stop. This would be fine, but
  912. * there's a catch: the device[usb phy] does not come back.
  913. *
  914. * Upon resume the firmware will "kill" itself and the
  915. * boot-code sorts out the magic voodoo.
  916. * Not very nice, but there's not much what could go wrong.
  917. */
  918. msleep(1100);
  919. err = carl9170_usb_init_device(ar);
  920. if (err)
  921. goto err_unrx;
  922. return 0;
  923. err_unrx:
  924. carl9170_usb_cancel_urbs(ar);
  925. return err;
  926. }
  927. #endif /* CONFIG_PM */
  928. static struct usb_driver carl9170_driver = {
  929. .name = KBUILD_MODNAME,
  930. .probe = carl9170_usb_probe,
  931. .disconnect = carl9170_usb_disconnect,
  932. .id_table = carl9170_usb_ids,
  933. .soft_unbind = 1,
  934. #ifdef CONFIG_PM
  935. .suspend = carl9170_usb_suspend,
  936. .resume = carl9170_usb_resume,
  937. .reset_resume = carl9170_usb_resume,
  938. #endif /* CONFIG_PM */
  939. .disable_hub_initiated_lpm = 1,
  940. };
  941. module_usb_driver(carl9170_driver);