usb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. /*
  2. * Atheros AR9170 driver
  3. *
  4. * USB - frontend
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2009, Christian Lamparter <chunkeey@web.de>
  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 "ar9170.h"
  47. #include "cmd.h"
  48. #include "hw.h"
  49. #include "usb.h"
  50. MODULE_AUTHOR("Johannes Berg <johannes@sipsolutions.net>");
  51. MODULE_AUTHOR("Christian Lamparter <chunkeey@web.de>");
  52. MODULE_LICENSE("GPL");
  53. MODULE_DESCRIPTION("Atheros AR9170 802.11n USB wireless");
  54. MODULE_FIRMWARE("ar9170.fw");
  55. enum ar9170_requirements {
  56. AR9170_REQ_FW1_ONLY = 1,
  57. };
  58. static struct usb_device_id ar9170_usb_ids[] = {
  59. /* Atheros 9170 */
  60. { USB_DEVICE(0x0cf3, 0x9170) },
  61. /* Atheros TG121N */
  62. { USB_DEVICE(0x0cf3, 0x1001) },
  63. /* TP-Link TL-WN821N v2 */
  64. { USB_DEVICE(0x0cf3, 0x1002) },
  65. /* 3Com Dual Band 802.11n USB Adapter */
  66. { USB_DEVICE(0x0cf3, 0x1010) },
  67. /* H3C Dual Band 802.11n USB Adapter */
  68. { USB_DEVICE(0x0cf3, 0x1011) },
  69. /* Cace Airpcap NX */
  70. { USB_DEVICE(0xcace, 0x0300) },
  71. /* D-Link DWA 160 A1 */
  72. { USB_DEVICE(0x07d1, 0x3c10) },
  73. /* D-Link DWA 160 A2 */
  74. { USB_DEVICE(0x07d1, 0x3a09) },
  75. /* Netgear WNA1000 */
  76. { USB_DEVICE(0x0846, 0x9040) },
  77. /* Netgear WNDA3100 */
  78. { USB_DEVICE(0x0846, 0x9010) },
  79. /* Netgear WN111 v2 */
  80. { USB_DEVICE(0x0846, 0x9001) },
  81. /* Zydas ZD1221 */
  82. { USB_DEVICE(0x0ace, 0x1221) },
  83. /* Proxim ORiNOCO 802.11n USB */
  84. { USB_DEVICE(0x1435, 0x0804) },
  85. /* WNC Generic 11n USB Dongle */
  86. { USB_DEVICE(0x1435, 0x0326) },
  87. /* ZyXEL NWD271N */
  88. { USB_DEVICE(0x0586, 0x3417) },
  89. /* Z-Com UB81 BG */
  90. { USB_DEVICE(0x0cde, 0x0023) },
  91. /* Z-Com UB82 ABG */
  92. { USB_DEVICE(0x0cde, 0x0026) },
  93. /* Sphairon Homelink 1202 */
  94. { USB_DEVICE(0x0cde, 0x0027) },
  95. /* Arcadyan WN7512 */
  96. { USB_DEVICE(0x083a, 0xf522) },
  97. /* Planex GWUS300 */
  98. { USB_DEVICE(0x2019, 0x5304) },
  99. /* IO-Data WNGDNUS2 */
  100. { USB_DEVICE(0x04bb, 0x093f) },
  101. /* AVM FRITZ!WLAN USB Stick N */
  102. { USB_DEVICE(0x057C, 0x8401) },
  103. /* NEC WL300NU-G */
  104. { USB_DEVICE(0x0409, 0x0249) },
  105. /* AVM FRITZ!WLAN USB Stick N 2.4 */
  106. { USB_DEVICE(0x057C, 0x8402), .driver_info = AR9170_REQ_FW1_ONLY },
  107. /* Qwest/Actiontec 802AIN Wireless N USB Network Adapter */
  108. { USB_DEVICE(0x1668, 0x1200) },
  109. /* terminate */
  110. {}
  111. };
  112. MODULE_DEVICE_TABLE(usb, ar9170_usb_ids);
  113. static void ar9170_usb_submit_urb(struct ar9170_usb *aru)
  114. {
  115. struct urb *urb;
  116. unsigned long flags;
  117. int err;
  118. if (unlikely(!IS_STARTED(&aru->common)))
  119. return ;
  120. spin_lock_irqsave(&aru->tx_urb_lock, flags);
  121. if (atomic_read(&aru->tx_submitted_urbs) >= AR9170_NUM_TX_URBS) {
  122. spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
  123. return ;
  124. }
  125. atomic_inc(&aru->tx_submitted_urbs);
  126. urb = usb_get_from_anchor(&aru->tx_pending);
  127. if (!urb) {
  128. atomic_dec(&aru->tx_submitted_urbs);
  129. spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
  130. return ;
  131. }
  132. spin_unlock_irqrestore(&aru->tx_urb_lock, flags);
  133. aru->tx_pending_urbs--;
  134. usb_anchor_urb(urb, &aru->tx_submitted);
  135. err = usb_submit_urb(urb, GFP_ATOMIC);
  136. if (unlikely(err)) {
  137. if (ar9170_nag_limiter(&aru->common))
  138. dev_err(&aru->udev->dev, "submit_urb failed (%d).\n",
  139. err);
  140. usb_unanchor_urb(urb);
  141. atomic_dec(&aru->tx_submitted_urbs);
  142. ar9170_tx_callback(&aru->common, urb->context);
  143. }
  144. usb_free_urb(urb);
  145. }
  146. static void ar9170_usb_tx_urb_complete_frame(struct urb *urb)
  147. {
  148. struct sk_buff *skb = urb->context;
  149. struct ar9170_usb *aru = usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  150. if (unlikely(!aru)) {
  151. dev_kfree_skb_irq(skb);
  152. return ;
  153. }
  154. atomic_dec(&aru->tx_submitted_urbs);
  155. ar9170_tx_callback(&aru->common, skb);
  156. ar9170_usb_submit_urb(aru);
  157. }
  158. static void ar9170_usb_tx_urb_complete(struct urb *urb)
  159. {
  160. }
  161. static void ar9170_usb_irq_completed(struct urb *urb)
  162. {
  163. struct ar9170_usb *aru = urb->context;
  164. switch (urb->status) {
  165. /* everything is fine */
  166. case 0:
  167. break;
  168. /* disconnect */
  169. case -ENOENT:
  170. case -ECONNRESET:
  171. case -ENODEV:
  172. case -ESHUTDOWN:
  173. goto free;
  174. default:
  175. goto resubmit;
  176. }
  177. ar9170_handle_command_response(&aru->common, urb->transfer_buffer,
  178. urb->actual_length);
  179. resubmit:
  180. usb_anchor_urb(urb, &aru->rx_submitted);
  181. if (usb_submit_urb(urb, GFP_ATOMIC)) {
  182. usb_unanchor_urb(urb);
  183. goto free;
  184. }
  185. return;
  186. free:
  187. usb_free_coherent(aru->udev, 64, urb->transfer_buffer, urb->transfer_dma);
  188. }
  189. static void ar9170_usb_rx_completed(struct urb *urb)
  190. {
  191. struct sk_buff *skb = urb->context;
  192. struct ar9170_usb *aru = usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  193. int err;
  194. if (!aru)
  195. goto free;
  196. switch (urb->status) {
  197. /* everything is fine */
  198. case 0:
  199. break;
  200. /* disconnect */
  201. case -ENOENT:
  202. case -ECONNRESET:
  203. case -ENODEV:
  204. case -ESHUTDOWN:
  205. goto free;
  206. default:
  207. goto resubmit;
  208. }
  209. skb_put(skb, urb->actual_length);
  210. ar9170_rx(&aru->common, skb);
  211. resubmit:
  212. skb_reset_tail_pointer(skb);
  213. skb_trim(skb, 0);
  214. usb_anchor_urb(urb, &aru->rx_submitted);
  215. err = usb_submit_urb(urb, GFP_ATOMIC);
  216. if (unlikely(err)) {
  217. usb_unanchor_urb(urb);
  218. goto free;
  219. }
  220. return ;
  221. free:
  222. dev_kfree_skb_irq(skb);
  223. }
  224. static int ar9170_usb_prep_rx_urb(struct ar9170_usb *aru,
  225. struct urb *urb, gfp_t gfp)
  226. {
  227. struct sk_buff *skb;
  228. skb = __dev_alloc_skb(AR9170_MAX_RX_BUFFER_SIZE + 32, gfp);
  229. if (!skb)
  230. return -ENOMEM;
  231. /* reserve some space for mac80211's radiotap */
  232. skb_reserve(skb, 32);
  233. usb_fill_bulk_urb(urb, aru->udev,
  234. usb_rcvbulkpipe(aru->udev, AR9170_EP_RX),
  235. skb->data, min(skb_tailroom(skb),
  236. AR9170_MAX_RX_BUFFER_SIZE),
  237. ar9170_usb_rx_completed, skb);
  238. return 0;
  239. }
  240. static int ar9170_usb_alloc_rx_irq_urb(struct ar9170_usb *aru)
  241. {
  242. struct urb *urb = NULL;
  243. void *ibuf;
  244. int err = -ENOMEM;
  245. /* initialize interrupt endpoint */
  246. urb = usb_alloc_urb(0, GFP_KERNEL);
  247. if (!urb)
  248. goto out;
  249. ibuf = usb_alloc_coherent(aru->udev, 64, GFP_KERNEL, &urb->transfer_dma);
  250. if (!ibuf)
  251. goto out;
  252. usb_fill_int_urb(urb, aru->udev,
  253. usb_rcvintpipe(aru->udev, AR9170_EP_IRQ), ibuf,
  254. 64, ar9170_usb_irq_completed, aru, 1);
  255. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  256. usb_anchor_urb(urb, &aru->rx_submitted);
  257. err = usb_submit_urb(urb, GFP_KERNEL);
  258. if (err) {
  259. usb_unanchor_urb(urb);
  260. usb_free_coherent(aru->udev, 64, urb->transfer_buffer,
  261. urb->transfer_dma);
  262. }
  263. out:
  264. usb_free_urb(urb);
  265. return err;
  266. }
  267. static int ar9170_usb_alloc_rx_bulk_urbs(struct ar9170_usb *aru)
  268. {
  269. struct urb *urb;
  270. int i;
  271. int err = -EINVAL;
  272. for (i = 0; i < AR9170_NUM_RX_URBS; i++) {
  273. err = -ENOMEM;
  274. urb = usb_alloc_urb(0, GFP_KERNEL);
  275. if (!urb)
  276. goto err_out;
  277. err = ar9170_usb_prep_rx_urb(aru, urb, GFP_KERNEL);
  278. if (err) {
  279. usb_free_urb(urb);
  280. goto err_out;
  281. }
  282. usb_anchor_urb(urb, &aru->rx_submitted);
  283. err = usb_submit_urb(urb, GFP_KERNEL);
  284. if (err) {
  285. usb_unanchor_urb(urb);
  286. dev_kfree_skb_any((void *) urb->transfer_buffer);
  287. usb_free_urb(urb);
  288. goto err_out;
  289. }
  290. usb_free_urb(urb);
  291. }
  292. /* the device now waiting for a firmware. */
  293. aru->common.state = AR9170_IDLE;
  294. return 0;
  295. err_out:
  296. usb_kill_anchored_urbs(&aru->rx_submitted);
  297. return err;
  298. }
  299. static int ar9170_usb_flush(struct ar9170 *ar)
  300. {
  301. struct ar9170_usb *aru = (void *) ar;
  302. struct urb *urb;
  303. int ret, err = 0;
  304. if (IS_STARTED(ar))
  305. aru->common.state = AR9170_IDLE;
  306. usb_wait_anchor_empty_timeout(&aru->tx_pending,
  307. msecs_to_jiffies(800));
  308. while ((urb = usb_get_from_anchor(&aru->tx_pending))) {
  309. ar9170_tx_callback(&aru->common, (void *) urb->context);
  310. usb_free_urb(urb);
  311. }
  312. /* lets wait a while until the tx - queues are dried out */
  313. ret = usb_wait_anchor_empty_timeout(&aru->tx_submitted,
  314. msecs_to_jiffies(100));
  315. if (ret == 0)
  316. err = -ETIMEDOUT;
  317. usb_kill_anchored_urbs(&aru->tx_submitted);
  318. if (IS_ACCEPTING_CMD(ar))
  319. aru->common.state = AR9170_STARTED;
  320. return err;
  321. }
  322. static void ar9170_usb_cancel_urbs(struct ar9170_usb *aru)
  323. {
  324. int err;
  325. aru->common.state = AR9170_UNKNOWN_STATE;
  326. err = ar9170_usb_flush(&aru->common);
  327. if (err)
  328. dev_err(&aru->udev->dev, "stuck tx urbs!\n");
  329. usb_poison_anchored_urbs(&aru->tx_submitted);
  330. usb_poison_anchored_urbs(&aru->rx_submitted);
  331. }
  332. static int ar9170_usb_exec_cmd(struct ar9170 *ar, enum ar9170_cmd cmd,
  333. unsigned int plen, void *payload,
  334. unsigned int outlen, void *out)
  335. {
  336. struct ar9170_usb *aru = (void *) ar;
  337. struct urb *urb = NULL;
  338. unsigned long flags;
  339. int err = -ENOMEM;
  340. if (unlikely(!IS_ACCEPTING_CMD(ar)))
  341. return -EPERM;
  342. if (WARN_ON(plen > AR9170_MAX_CMD_LEN - 4))
  343. return -EINVAL;
  344. urb = usb_alloc_urb(0, GFP_ATOMIC);
  345. if (unlikely(!urb))
  346. goto err_free;
  347. ar->cmdbuf[0] = cpu_to_le32(plen);
  348. ar->cmdbuf[0] |= cpu_to_le32(cmd << 8);
  349. /* writing multiple regs fills this buffer already */
  350. if (plen && payload != (u8 *)(&ar->cmdbuf[1]))
  351. memcpy(&ar->cmdbuf[1], payload, plen);
  352. spin_lock_irqsave(&aru->common.cmdlock, flags);
  353. aru->readbuf = (u8 *)out;
  354. aru->readlen = outlen;
  355. spin_unlock_irqrestore(&aru->common.cmdlock, flags);
  356. usb_fill_int_urb(urb, aru->udev,
  357. usb_sndintpipe(aru->udev, AR9170_EP_CMD),
  358. aru->common.cmdbuf, plen + 4,
  359. ar9170_usb_tx_urb_complete, NULL, 1);
  360. usb_anchor_urb(urb, &aru->tx_submitted);
  361. err = usb_submit_urb(urb, GFP_ATOMIC);
  362. if (unlikely(err)) {
  363. usb_unanchor_urb(urb);
  364. usb_free_urb(urb);
  365. goto err_unbuf;
  366. }
  367. usb_free_urb(urb);
  368. err = wait_for_completion_timeout(&aru->cmd_wait, HZ);
  369. if (err == 0) {
  370. err = -ETIMEDOUT;
  371. goto err_unbuf;
  372. }
  373. if (aru->readlen != outlen) {
  374. err = -EMSGSIZE;
  375. goto err_unbuf;
  376. }
  377. return 0;
  378. err_unbuf:
  379. /* Maybe the device was removed in the second we were waiting? */
  380. if (IS_STARTED(ar)) {
  381. dev_err(&aru->udev->dev, "no command feedback "
  382. "received (%d).\n", err);
  383. /* provide some maybe useful debug information */
  384. print_hex_dump_bytes("ar9170 cmd: ", DUMP_PREFIX_NONE,
  385. aru->common.cmdbuf, plen + 4);
  386. dump_stack();
  387. }
  388. /* invalidate to avoid completing the next prematurely */
  389. spin_lock_irqsave(&aru->common.cmdlock, flags);
  390. aru->readbuf = NULL;
  391. aru->readlen = 0;
  392. spin_unlock_irqrestore(&aru->common.cmdlock, flags);
  393. err_free:
  394. return err;
  395. }
  396. static int ar9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb)
  397. {
  398. struct ar9170_usb *aru = (struct ar9170_usb *) ar;
  399. struct urb *urb;
  400. if (unlikely(!IS_STARTED(ar))) {
  401. /* Seriously, what were you drink... err... thinking!? */
  402. return -EPERM;
  403. }
  404. urb = usb_alloc_urb(0, GFP_ATOMIC);
  405. if (unlikely(!urb))
  406. return -ENOMEM;
  407. usb_fill_bulk_urb(urb, aru->udev,
  408. usb_sndbulkpipe(aru->udev, AR9170_EP_TX),
  409. skb->data, skb->len,
  410. ar9170_usb_tx_urb_complete_frame, skb);
  411. urb->transfer_flags |= URB_ZERO_PACKET;
  412. usb_anchor_urb(urb, &aru->tx_pending);
  413. aru->tx_pending_urbs++;
  414. usb_free_urb(urb);
  415. ar9170_usb_submit_urb(aru);
  416. return 0;
  417. }
  418. static void ar9170_usb_callback_cmd(struct ar9170 *ar, u32 len , void *buffer)
  419. {
  420. struct ar9170_usb *aru = (void *) ar;
  421. unsigned long flags;
  422. u32 in, out;
  423. if (unlikely(!buffer))
  424. return ;
  425. in = le32_to_cpup((__le32 *)buffer);
  426. out = le32_to_cpu(ar->cmdbuf[0]);
  427. /* mask off length byte */
  428. out &= ~0xFF;
  429. if (aru->readlen >= 0) {
  430. /* add expected length */
  431. out |= aru->readlen;
  432. } else {
  433. /* add obtained length */
  434. out |= in & 0xFF;
  435. }
  436. /*
  437. * Some commands (e.g: AR9170_CMD_FREQUENCY) have a variable response
  438. * length and we cannot predict the correct length in advance.
  439. * So we only check if we provided enough space for the data.
  440. */
  441. if (unlikely(out < in)) {
  442. dev_warn(&aru->udev->dev, "received invalid command response "
  443. "got %d bytes, instead of %d bytes "
  444. "and the resp length is %d bytes\n",
  445. in, out, len);
  446. print_hex_dump_bytes("ar9170 invalid resp: ",
  447. DUMP_PREFIX_OFFSET, buffer, len);
  448. /*
  449. * Do not complete, then the command times out,
  450. * and we get a stack trace from there.
  451. */
  452. return ;
  453. }
  454. spin_lock_irqsave(&aru->common.cmdlock, flags);
  455. if (aru->readbuf && len > 0) {
  456. memcpy(aru->readbuf, buffer + 4, len - 4);
  457. aru->readbuf = NULL;
  458. }
  459. complete(&aru->cmd_wait);
  460. spin_unlock_irqrestore(&aru->common.cmdlock, flags);
  461. }
  462. static int ar9170_usb_upload(struct ar9170_usb *aru, const void *data,
  463. size_t len, u32 addr, bool complete)
  464. {
  465. int transfer, err;
  466. u8 *buf = kmalloc(4096, GFP_KERNEL);
  467. if (!buf)
  468. return -ENOMEM;
  469. while (len) {
  470. transfer = min_t(int, len, 4096);
  471. memcpy(buf, data, transfer);
  472. err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
  473. 0x30 /* FW DL */, 0x40 | USB_DIR_OUT,
  474. addr >> 8, 0, buf, transfer, 1000);
  475. if (err < 0) {
  476. kfree(buf);
  477. return err;
  478. }
  479. len -= transfer;
  480. data += transfer;
  481. addr += transfer;
  482. }
  483. kfree(buf);
  484. if (complete) {
  485. err = usb_control_msg(aru->udev, usb_sndctrlpipe(aru->udev, 0),
  486. 0x31 /* FW DL COMPLETE */,
  487. 0x40 | USB_DIR_OUT, 0, 0, NULL, 0, 5000);
  488. }
  489. return 0;
  490. }
  491. static int ar9170_usb_reset(struct ar9170_usb *aru)
  492. {
  493. int ret, lock = (aru->intf->condition != USB_INTERFACE_BINDING);
  494. if (lock) {
  495. ret = usb_lock_device_for_reset(aru->udev, aru->intf);
  496. if (ret < 0) {
  497. dev_err(&aru->udev->dev, "unable to lock device "
  498. "for reset (%d).\n", ret);
  499. return ret;
  500. }
  501. }
  502. ret = usb_reset_device(aru->udev);
  503. if (lock)
  504. usb_unlock_device(aru->udev);
  505. /* let it rest - for a second - */
  506. msleep(1000);
  507. return ret;
  508. }
  509. static int ar9170_usb_upload_firmware(struct ar9170_usb *aru)
  510. {
  511. int err;
  512. if (!aru->init_values)
  513. goto upload_fw_start;
  514. /* First, upload initial values to device RAM */
  515. err = ar9170_usb_upload(aru, aru->init_values->data,
  516. aru->init_values->size, 0x102800, false);
  517. if (err) {
  518. dev_err(&aru->udev->dev, "firmware part 1 "
  519. "upload failed (%d).\n", err);
  520. return err;
  521. }
  522. upload_fw_start:
  523. /* Then, upload the firmware itself and start it */
  524. return ar9170_usb_upload(aru, aru->firmware->data, aru->firmware->size,
  525. 0x200000, true);
  526. }
  527. static int ar9170_usb_init_transport(struct ar9170_usb *aru)
  528. {
  529. struct ar9170 *ar = (void *) &aru->common;
  530. int err;
  531. ar9170_regwrite_begin(ar);
  532. /* Set USB Rx stream mode MAX packet number to 2 */
  533. ar9170_regwrite(AR9170_USB_REG_MAX_AGG_UPLOAD, 0x4);
  534. /* Set USB Rx stream mode timeout to 10us */
  535. ar9170_regwrite(AR9170_USB_REG_UPLOAD_TIME_CTL, 0x80);
  536. ar9170_regwrite_finish();
  537. err = ar9170_regwrite_result();
  538. if (err)
  539. dev_err(&aru->udev->dev, "USB setup failed (%d).\n", err);
  540. return err;
  541. }
  542. static void ar9170_usb_stop(struct ar9170 *ar)
  543. {
  544. struct ar9170_usb *aru = (void *) ar;
  545. int ret;
  546. if (IS_ACCEPTING_CMD(ar))
  547. aru->common.state = AR9170_STOPPED;
  548. ret = ar9170_usb_flush(ar);
  549. if (ret)
  550. dev_err(&aru->udev->dev, "kill pending tx urbs.\n");
  551. usb_poison_anchored_urbs(&aru->tx_submitted);
  552. /*
  553. * Note:
  554. * So far we freed all tx urbs, but we won't dare to touch any rx urbs.
  555. * Else we would end up with a unresponsive device...
  556. */
  557. }
  558. static int ar9170_usb_open(struct ar9170 *ar)
  559. {
  560. struct ar9170_usb *aru = (void *) ar;
  561. int err;
  562. usb_unpoison_anchored_urbs(&aru->tx_submitted);
  563. err = ar9170_usb_init_transport(aru);
  564. if (err) {
  565. usb_poison_anchored_urbs(&aru->tx_submitted);
  566. return err;
  567. }
  568. aru->common.state = AR9170_IDLE;
  569. return 0;
  570. }
  571. static int ar9170_usb_init_device(struct ar9170_usb *aru)
  572. {
  573. int err;
  574. err = ar9170_usb_alloc_rx_irq_urb(aru);
  575. if (err)
  576. goto err_out;
  577. err = ar9170_usb_alloc_rx_bulk_urbs(aru);
  578. if (err)
  579. goto err_unrx;
  580. err = ar9170_usb_upload_firmware(aru);
  581. if (err) {
  582. err = ar9170_echo_test(&aru->common, 0x60d43110);
  583. if (err) {
  584. /* force user invention, by disabling the device */
  585. err = usb_driver_set_configuration(aru->udev, -1);
  586. dev_err(&aru->udev->dev, "device is in a bad state. "
  587. "please reconnect it!\n");
  588. goto err_unrx;
  589. }
  590. }
  591. return 0;
  592. err_unrx:
  593. ar9170_usb_cancel_urbs(aru);
  594. err_out:
  595. return err;
  596. }
  597. static void ar9170_usb_firmware_failed(struct ar9170_usb *aru)
  598. {
  599. struct device *parent = aru->udev->dev.parent;
  600. struct usb_device *udev;
  601. /*
  602. * Store a copy of the usb_device pointer locally.
  603. * This is because device_release_driver initiates
  604. * ar9170_usb_disconnect, which in turn frees our
  605. * driver context (aru).
  606. */
  607. udev = aru->udev;
  608. complete(&aru->firmware_loading_complete);
  609. /* unbind anything failed */
  610. if (parent)
  611. device_lock(parent);
  612. device_release_driver(&udev->dev);
  613. if (parent)
  614. device_unlock(parent);
  615. usb_put_dev(udev);
  616. }
  617. static void ar9170_usb_firmware_finish(const struct firmware *fw, void *context)
  618. {
  619. struct ar9170_usb *aru = context;
  620. int err;
  621. aru->firmware = fw;
  622. if (!fw) {
  623. dev_err(&aru->udev->dev, "firmware file not found.\n");
  624. goto err_freefw;
  625. }
  626. err = ar9170_usb_init_device(aru);
  627. if (err)
  628. goto err_freefw;
  629. err = ar9170_usb_open(&aru->common);
  630. if (err)
  631. goto err_unrx;
  632. err = ar9170_register(&aru->common, &aru->udev->dev);
  633. ar9170_usb_stop(&aru->common);
  634. if (err)
  635. goto err_unrx;
  636. complete(&aru->firmware_loading_complete);
  637. usb_put_dev(aru->udev);
  638. return;
  639. err_unrx:
  640. ar9170_usb_cancel_urbs(aru);
  641. err_freefw:
  642. ar9170_usb_firmware_failed(aru);
  643. }
  644. static void ar9170_usb_firmware_inits(const struct firmware *fw,
  645. void *context)
  646. {
  647. struct ar9170_usb *aru = context;
  648. int err;
  649. if (!fw) {
  650. dev_err(&aru->udev->dev, "file with init values not found.\n");
  651. ar9170_usb_firmware_failed(aru);
  652. return;
  653. }
  654. aru->init_values = fw;
  655. /* ok so we have the init values -- get code for two-stage */
  656. err = request_firmware_nowait(THIS_MODULE, 1, "ar9170-2.fw",
  657. &aru->udev->dev, GFP_KERNEL, aru,
  658. ar9170_usb_firmware_finish);
  659. if (err)
  660. ar9170_usb_firmware_failed(aru);
  661. }
  662. static void ar9170_usb_firmware_step2(const struct firmware *fw, void *context)
  663. {
  664. struct ar9170_usb *aru = context;
  665. int err;
  666. if (fw) {
  667. ar9170_usb_firmware_finish(fw, context);
  668. return;
  669. }
  670. if (aru->req_one_stage_fw) {
  671. dev_err(&aru->udev->dev, "ar9170.fw firmware file "
  672. "not found and is required for this device\n");
  673. ar9170_usb_firmware_failed(aru);
  674. return;
  675. }
  676. dev_err(&aru->udev->dev, "ar9170.fw firmware file "
  677. "not found, trying old firmware...\n");
  678. err = request_firmware_nowait(THIS_MODULE, 1, "ar9170-1.fw",
  679. &aru->udev->dev, GFP_KERNEL, aru,
  680. ar9170_usb_firmware_inits);
  681. if (err)
  682. ar9170_usb_firmware_failed(aru);
  683. }
  684. static bool ar9170_requires_one_stage(const struct usb_device_id *id)
  685. {
  686. if (!id->driver_info)
  687. return false;
  688. if (id->driver_info == AR9170_REQ_FW1_ONLY)
  689. return true;
  690. return false;
  691. }
  692. static int ar9170_usb_probe(struct usb_interface *intf,
  693. const struct usb_device_id *id)
  694. {
  695. struct ar9170_usb *aru;
  696. struct ar9170 *ar;
  697. struct usb_device *udev;
  698. int err;
  699. aru = ar9170_alloc(sizeof(*aru));
  700. if (IS_ERR(aru)) {
  701. err = PTR_ERR(aru);
  702. goto out;
  703. }
  704. udev = interface_to_usbdev(intf);
  705. usb_get_dev(udev);
  706. aru->udev = udev;
  707. aru->intf = intf;
  708. ar = &aru->common;
  709. aru->req_one_stage_fw = ar9170_requires_one_stage(id);
  710. usb_set_intfdata(intf, aru);
  711. SET_IEEE80211_DEV(ar->hw, &intf->dev);
  712. init_usb_anchor(&aru->rx_submitted);
  713. init_usb_anchor(&aru->tx_pending);
  714. init_usb_anchor(&aru->tx_submitted);
  715. init_completion(&aru->cmd_wait);
  716. init_completion(&aru->firmware_loading_complete);
  717. spin_lock_init(&aru->tx_urb_lock);
  718. aru->tx_pending_urbs = 0;
  719. atomic_set(&aru->tx_submitted_urbs, 0);
  720. aru->common.stop = ar9170_usb_stop;
  721. aru->common.flush = ar9170_usb_flush;
  722. aru->common.open = ar9170_usb_open;
  723. aru->common.tx = ar9170_usb_tx;
  724. aru->common.exec_cmd = ar9170_usb_exec_cmd;
  725. aru->common.callback_cmd = ar9170_usb_callback_cmd;
  726. #ifdef CONFIG_PM
  727. udev->reset_resume = 1;
  728. #endif /* CONFIG_PM */
  729. err = ar9170_usb_reset(aru);
  730. if (err)
  731. goto err_freehw;
  732. usb_get_dev(aru->udev);
  733. return request_firmware_nowait(THIS_MODULE, 1, "ar9170.fw",
  734. &aru->udev->dev, GFP_KERNEL, aru,
  735. ar9170_usb_firmware_step2);
  736. err_freehw:
  737. usb_set_intfdata(intf, NULL);
  738. usb_put_dev(udev);
  739. ieee80211_free_hw(ar->hw);
  740. out:
  741. return err;
  742. }
  743. static void ar9170_usb_disconnect(struct usb_interface *intf)
  744. {
  745. struct ar9170_usb *aru = usb_get_intfdata(intf);
  746. if (!aru)
  747. return;
  748. aru->common.state = AR9170_IDLE;
  749. wait_for_completion(&aru->firmware_loading_complete);
  750. ar9170_unregister(&aru->common);
  751. ar9170_usb_cancel_urbs(aru);
  752. usb_put_dev(aru->udev);
  753. usb_set_intfdata(intf, NULL);
  754. ieee80211_free_hw(aru->common.hw);
  755. release_firmware(aru->init_values);
  756. release_firmware(aru->firmware);
  757. }
  758. #ifdef CONFIG_PM
  759. static int ar9170_suspend(struct usb_interface *intf,
  760. pm_message_t message)
  761. {
  762. struct ar9170_usb *aru = usb_get_intfdata(intf);
  763. if (!aru)
  764. return -ENODEV;
  765. aru->common.state = AR9170_IDLE;
  766. ar9170_usb_cancel_urbs(aru);
  767. return 0;
  768. }
  769. static int ar9170_resume(struct usb_interface *intf)
  770. {
  771. struct ar9170_usb *aru = usb_get_intfdata(intf);
  772. int err;
  773. if (!aru)
  774. return -ENODEV;
  775. usb_unpoison_anchored_urbs(&aru->rx_submitted);
  776. usb_unpoison_anchored_urbs(&aru->tx_submitted);
  777. err = ar9170_usb_init_device(aru);
  778. if (err)
  779. goto err_unrx;
  780. err = ar9170_usb_open(&aru->common);
  781. if (err)
  782. goto err_unrx;
  783. return 0;
  784. err_unrx:
  785. aru->common.state = AR9170_IDLE;
  786. ar9170_usb_cancel_urbs(aru);
  787. return err;
  788. }
  789. #endif /* CONFIG_PM */
  790. static struct usb_driver ar9170_driver = {
  791. .name = "ar9170usb",
  792. .probe = ar9170_usb_probe,
  793. .disconnect = ar9170_usb_disconnect,
  794. .id_table = ar9170_usb_ids,
  795. .soft_unbind = 1,
  796. #ifdef CONFIG_PM
  797. .suspend = ar9170_suspend,
  798. .resume = ar9170_resume,
  799. .reset_resume = ar9170_resume,
  800. #endif /* CONFIG_PM */
  801. };
  802. static int __init ar9170_init(void)
  803. {
  804. return usb_register(&ar9170_driver);
  805. }
  806. static void __exit ar9170_exit(void)
  807. {
  808. usb_deregister(&ar9170_driver);
  809. }
  810. module_init(ar9170_init);
  811. module_exit(ar9170_exit);