usb.c 23 KB

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