usb.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * Copyright (c) 2007-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
  4. * Copyright (c) 2016-2017 Erik Stromdahl <erik.stromdahl@gmail.com>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/usb.h>
  20. #include "debug.h"
  21. #include "core.h"
  22. #include "bmi.h"
  23. #include "hif.h"
  24. #include "htc.h"
  25. #include "usb.h"
  26. static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
  27. struct ath10k_usb_pipe *recv_pipe);
  28. /* inlined helper functions */
  29. static inline enum ath10k_htc_ep_id
  30. eid_from_htc_hdr(struct ath10k_htc_hdr *htc_hdr)
  31. {
  32. return (enum ath10k_htc_ep_id)htc_hdr->eid;
  33. }
  34. static inline bool is_trailer_only_msg(struct ath10k_htc_hdr *htc_hdr)
  35. {
  36. return __le16_to_cpu(htc_hdr->len) == htc_hdr->trailer_len;
  37. }
  38. /* pipe/urb operations */
  39. static struct ath10k_urb_context *
  40. ath10k_usb_alloc_urb_from_pipe(struct ath10k_usb_pipe *pipe)
  41. {
  42. struct ath10k_urb_context *urb_context = NULL;
  43. unsigned long flags;
  44. /* bail if this pipe is not initialized */
  45. if (!pipe->ar_usb)
  46. return NULL;
  47. spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
  48. if (!list_empty(&pipe->urb_list_head)) {
  49. urb_context = list_first_entry(&pipe->urb_list_head,
  50. struct ath10k_urb_context, link);
  51. list_del(&urb_context->link);
  52. pipe->urb_cnt--;
  53. }
  54. spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
  55. return urb_context;
  56. }
  57. static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
  58. struct ath10k_urb_context *urb_context)
  59. {
  60. unsigned long flags;
  61. /* bail if this pipe is not initialized */
  62. if (!pipe->ar_usb)
  63. return;
  64. spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
  65. pipe->urb_cnt++;
  66. list_add(&urb_context->link, &pipe->urb_list_head);
  67. spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
  68. }
  69. static void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context)
  70. {
  71. dev_kfree_skb(urb_context->skb);
  72. urb_context->skb = NULL;
  73. ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
  74. }
  75. static void ath10k_usb_free_pipe_resources(struct ath10k *ar,
  76. struct ath10k_usb_pipe *pipe)
  77. {
  78. struct ath10k_urb_context *urb_context;
  79. if (!pipe->ar_usb) {
  80. /* nothing allocated for this pipe */
  81. return;
  82. }
  83. ath10k_dbg(ar, ATH10K_DBG_USB,
  84. "usb free resources lpipe %d hpipe 0x%x urbs %d avail %d\n",
  85. pipe->logical_pipe_num, pipe->usb_pipe_handle,
  86. pipe->urb_alloc, pipe->urb_cnt);
  87. if (pipe->urb_alloc != pipe->urb_cnt) {
  88. ath10k_dbg(ar, ATH10K_DBG_USB,
  89. "usb urb leak lpipe %d hpipe 0x%x urbs %d avail %d\n",
  90. pipe->logical_pipe_num, pipe->usb_pipe_handle,
  91. pipe->urb_alloc, pipe->urb_cnt);
  92. }
  93. for (;;) {
  94. urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
  95. if (!urb_context)
  96. break;
  97. kfree(urb_context);
  98. }
  99. }
  100. static void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar)
  101. {
  102. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  103. int i;
  104. for (i = 0; i < ATH10K_USB_PIPE_MAX; i++)
  105. ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]);
  106. }
  107. /* hif usb rx/tx completion functions */
  108. static void ath10k_usb_recv_complete(struct urb *urb)
  109. {
  110. struct ath10k_urb_context *urb_context = urb->context;
  111. struct ath10k_usb_pipe *pipe = urb_context->pipe;
  112. struct ath10k *ar = pipe->ar_usb->ar;
  113. struct sk_buff *skb;
  114. int status = 0;
  115. ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
  116. "usb recv pipe %d stat %d len %d urb 0x%pK\n",
  117. pipe->logical_pipe_num, urb->status, urb->actual_length,
  118. urb);
  119. if (urb->status != 0) {
  120. status = -EIO;
  121. switch (urb->status) {
  122. case -ECONNRESET:
  123. case -ENOENT:
  124. case -ESHUTDOWN:
  125. /* no need to spew these errors when device
  126. * removed or urb killed due to driver shutdown
  127. */
  128. status = -ECANCELED;
  129. break;
  130. default:
  131. ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
  132. "usb recv pipe %d ep 0x%2.2x failed: %d\n",
  133. pipe->logical_pipe_num,
  134. pipe->ep_address, urb->status);
  135. break;
  136. }
  137. goto cleanup_recv_urb;
  138. }
  139. if (urb->actual_length == 0)
  140. goto cleanup_recv_urb;
  141. skb = urb_context->skb;
  142. /* we are going to pass it up */
  143. urb_context->skb = NULL;
  144. skb_put(skb, urb->actual_length);
  145. /* note: queue implements a lock */
  146. skb_queue_tail(&pipe->io_comp_queue, skb);
  147. schedule_work(&pipe->io_complete_work);
  148. cleanup_recv_urb:
  149. ath10k_usb_cleanup_recv_urb(urb_context);
  150. if (status == 0 &&
  151. pipe->urb_cnt >= pipe->urb_cnt_thresh) {
  152. /* our free urbs are piling up, post more transfers */
  153. ath10k_usb_post_recv_transfers(ar, pipe);
  154. }
  155. }
  156. static void ath10k_usb_transmit_complete(struct urb *urb)
  157. {
  158. struct ath10k_urb_context *urb_context = urb->context;
  159. struct ath10k_usb_pipe *pipe = urb_context->pipe;
  160. struct ath10k *ar = pipe->ar_usb->ar;
  161. struct sk_buff *skb;
  162. if (urb->status != 0) {
  163. ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
  164. "pipe: %d, failed:%d\n",
  165. pipe->logical_pipe_num, urb->status);
  166. }
  167. skb = urb_context->skb;
  168. urb_context->skb = NULL;
  169. ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
  170. /* note: queue implements a lock */
  171. skb_queue_tail(&pipe->io_comp_queue, skb);
  172. schedule_work(&pipe->io_complete_work);
  173. }
  174. /* pipe operations */
  175. static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
  176. struct ath10k_usb_pipe *recv_pipe)
  177. {
  178. struct ath10k_urb_context *urb_context;
  179. struct urb *urb;
  180. int usb_status;
  181. for (;;) {
  182. urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe);
  183. if (!urb_context)
  184. break;
  185. urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE);
  186. if (!urb_context->skb)
  187. goto err;
  188. urb = usb_alloc_urb(0, GFP_ATOMIC);
  189. if (!urb)
  190. goto err;
  191. usb_fill_bulk_urb(urb,
  192. recv_pipe->ar_usb->udev,
  193. recv_pipe->usb_pipe_handle,
  194. urb_context->skb->data,
  195. ATH10K_USB_RX_BUFFER_SIZE,
  196. ath10k_usb_recv_complete, urb_context);
  197. ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
  198. "usb bulk recv submit %d 0x%x ep 0x%2.2x len %d buf 0x%pK\n",
  199. recv_pipe->logical_pipe_num,
  200. recv_pipe->usb_pipe_handle, recv_pipe->ep_address,
  201. ATH10K_USB_RX_BUFFER_SIZE, urb_context->skb);
  202. usb_anchor_urb(urb, &recv_pipe->urb_submitted);
  203. usb_status = usb_submit_urb(urb, GFP_ATOMIC);
  204. if (usb_status) {
  205. ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
  206. "usb bulk recv failed: %d\n",
  207. usb_status);
  208. usb_unanchor_urb(urb);
  209. usb_free_urb(urb);
  210. goto err;
  211. }
  212. usb_free_urb(urb);
  213. }
  214. return;
  215. err:
  216. ath10k_usb_cleanup_recv_urb(urb_context);
  217. }
  218. static void ath10k_usb_flush_all(struct ath10k *ar)
  219. {
  220. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  221. int i;
  222. for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
  223. if (ar_usb->pipes[i].ar_usb) {
  224. usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted);
  225. cancel_work_sync(&ar_usb->pipes[i].io_complete_work);
  226. }
  227. }
  228. }
  229. static void ath10k_usb_start_recv_pipes(struct ath10k *ar)
  230. {
  231. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  232. ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
  233. ath10k_usb_post_recv_transfers(ar,
  234. &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
  235. }
  236. static void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb)
  237. {
  238. struct ath10k_htc_hdr *htc_hdr;
  239. struct ath10k_htc_ep *ep;
  240. htc_hdr = (struct ath10k_htc_hdr *)skb->data;
  241. ep = &ar->htc.endpoint[htc_hdr->eid];
  242. ath10k_htc_notify_tx_completion(ep, skb);
  243. /* The TX complete handler now owns the skb... */
  244. }
  245. static void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb)
  246. {
  247. struct ath10k_htc *htc = &ar->htc;
  248. struct ath10k_htc_hdr *htc_hdr;
  249. enum ath10k_htc_ep_id eid;
  250. struct ath10k_htc_ep *ep;
  251. u16 payload_len;
  252. u8 *trailer;
  253. int ret;
  254. htc_hdr = (struct ath10k_htc_hdr *)skb->data;
  255. eid = eid_from_htc_hdr(htc_hdr);
  256. ep = &ar->htc.endpoint[eid];
  257. if (ep->service_id == 0) {
  258. ath10k_warn(ar, "ep %d is not connected\n", eid);
  259. goto out_free_skb;
  260. }
  261. payload_len = le16_to_cpu(htc_hdr->len);
  262. if (!payload_len) {
  263. ath10k_warn(ar, "zero length frame received, firmware crashed?\n");
  264. goto out_free_skb;
  265. }
  266. if (payload_len < htc_hdr->trailer_len) {
  267. ath10k_warn(ar, "malformed frame received, firmware crashed?\n");
  268. goto out_free_skb;
  269. }
  270. if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) {
  271. trailer = skb->data + sizeof(*htc_hdr) + payload_len -
  272. htc_hdr->trailer_len;
  273. ret = ath10k_htc_process_trailer(htc,
  274. trailer,
  275. htc_hdr->trailer_len,
  276. eid,
  277. NULL,
  278. NULL);
  279. if (ret)
  280. goto out_free_skb;
  281. if (is_trailer_only_msg(htc_hdr))
  282. goto out_free_skb;
  283. /* strip off the trailer from the skb since it should not
  284. * be passed on to upper layers
  285. */
  286. skb_trim(skb, skb->len - htc_hdr->trailer_len);
  287. }
  288. skb_pull(skb, sizeof(*htc_hdr));
  289. ep->ep_ops.ep_rx_complete(ar, skb);
  290. /* The RX complete handler now owns the skb... */
  291. return;
  292. out_free_skb:
  293. dev_kfree_skb(skb);
  294. }
  295. static void ath10k_usb_io_comp_work(struct work_struct *work)
  296. {
  297. struct ath10k_usb_pipe *pipe = container_of(work,
  298. struct ath10k_usb_pipe,
  299. io_complete_work);
  300. struct ath10k *ar = pipe->ar_usb->ar;
  301. struct sk_buff *skb;
  302. while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
  303. if (pipe->flags & ATH10K_USB_PIPE_FLAG_TX)
  304. ath10k_usb_tx_complete(ar, skb);
  305. else
  306. ath10k_usb_rx_complete(ar, skb);
  307. }
  308. }
  309. #define ATH10K_USB_MAX_DIAG_CMD (sizeof(struct ath10k_usb_ctrl_diag_cmd_write))
  310. #define ATH10K_USB_MAX_DIAG_RESP (sizeof(struct ath10k_usb_ctrl_diag_resp_read))
  311. static void ath10k_usb_destroy(struct ath10k *ar)
  312. {
  313. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  314. ath10k_usb_flush_all(ar);
  315. ath10k_usb_cleanup_pipe_resources(ar);
  316. usb_set_intfdata(ar_usb->interface, NULL);
  317. kfree(ar_usb->diag_cmd_buffer);
  318. kfree(ar_usb->diag_resp_buffer);
  319. }
  320. static int ath10k_usb_hif_start(struct ath10k *ar)
  321. {
  322. int i;
  323. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  324. ath10k_usb_start_recv_pipes(ar);
  325. /* set the TX resource avail threshold for each TX pipe */
  326. for (i = ATH10K_USB_PIPE_TX_CTRL;
  327. i <= ATH10K_USB_PIPE_TX_DATA_HP; i++) {
  328. ar_usb->pipes[i].urb_cnt_thresh =
  329. ar_usb->pipes[i].urb_alloc / 2;
  330. }
  331. return 0;
  332. }
  333. static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
  334. struct ath10k_hif_sg_item *items, int n_items)
  335. {
  336. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  337. struct ath10k_usb_pipe *pipe = &ar_usb->pipes[pipe_id];
  338. struct ath10k_urb_context *urb_context;
  339. struct sk_buff *skb;
  340. struct urb *urb;
  341. int ret, i;
  342. for (i = 0; i < n_items; i++) {
  343. urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
  344. if (!urb_context) {
  345. ret = -ENOMEM;
  346. goto err;
  347. }
  348. skb = items[i].transfer_context;
  349. urb_context->skb = skb;
  350. urb = usb_alloc_urb(0, GFP_ATOMIC);
  351. if (!urb) {
  352. ret = -ENOMEM;
  353. goto err_free_urb_to_pipe;
  354. }
  355. usb_fill_bulk_urb(urb,
  356. ar_usb->udev,
  357. pipe->usb_pipe_handle,
  358. skb->data,
  359. skb->len,
  360. ath10k_usb_transmit_complete, urb_context);
  361. if (!(skb->len % pipe->max_packet_size)) {
  362. /* hit a max packet boundary on this pipe */
  363. urb->transfer_flags |= URB_ZERO_PACKET;
  364. }
  365. usb_anchor_urb(urb, &pipe->urb_submitted);
  366. ret = usb_submit_urb(urb, GFP_ATOMIC);
  367. if (ret) {
  368. ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
  369. "usb bulk transmit failed: %d\n", ret);
  370. usb_unanchor_urb(urb);
  371. usb_free_urb(urb);
  372. ret = -EINVAL;
  373. goto err_free_urb_to_pipe;
  374. }
  375. usb_free_urb(urb);
  376. }
  377. return 0;
  378. err_free_urb_to_pipe:
  379. ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
  380. err:
  381. return ret;
  382. }
  383. static void ath10k_usb_hif_stop(struct ath10k *ar)
  384. {
  385. ath10k_usb_flush_all(ar);
  386. }
  387. static u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id)
  388. {
  389. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  390. return ar_usb->pipes[pipe_id].urb_cnt;
  391. }
  392. static int ath10k_usb_submit_ctrl_out(struct ath10k *ar,
  393. u8 req, u16 value, u16 index, void *data,
  394. u32 size)
  395. {
  396. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  397. u8 *buf = NULL;
  398. int ret;
  399. if (size > 0) {
  400. buf = kmemdup(data, size, GFP_KERNEL);
  401. if (!buf)
  402. return -ENOMEM;
  403. }
  404. /* note: if successful returns number of bytes transferred */
  405. ret = usb_control_msg(ar_usb->udev,
  406. usb_sndctrlpipe(ar_usb->udev, 0),
  407. req,
  408. USB_DIR_OUT | USB_TYPE_VENDOR |
  409. USB_RECIP_DEVICE, value, index, buf,
  410. size, 1000);
  411. if (ret < 0) {
  412. ath10k_warn(ar, "Failed to submit usb control message: %d\n",
  413. ret);
  414. kfree(buf);
  415. return ret;
  416. }
  417. kfree(buf);
  418. return 0;
  419. }
  420. static int ath10k_usb_submit_ctrl_in(struct ath10k *ar,
  421. u8 req, u16 value, u16 index, void *data,
  422. u32 size)
  423. {
  424. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  425. u8 *buf = NULL;
  426. int ret;
  427. if (size > 0) {
  428. buf = kmalloc(size, GFP_KERNEL);
  429. if (!buf)
  430. return -ENOMEM;
  431. }
  432. /* note: if successful returns number of bytes transferred */
  433. ret = usb_control_msg(ar_usb->udev,
  434. usb_rcvctrlpipe(ar_usb->udev, 0),
  435. req,
  436. USB_DIR_IN | USB_TYPE_VENDOR |
  437. USB_RECIP_DEVICE, value, index, buf,
  438. size, 2 * HZ);
  439. if (ret < 0) {
  440. ath10k_warn(ar, "Failed to read usb control message: %d\n",
  441. ret);
  442. kfree(buf);
  443. return ret;
  444. }
  445. memcpy((u8 *)data, buf, size);
  446. kfree(buf);
  447. return 0;
  448. }
  449. static int ath10k_usb_ctrl_msg_exchange(struct ath10k *ar,
  450. u8 req_val, u8 *req_buf, u32 req_len,
  451. u8 resp_val, u8 *resp_buf,
  452. u32 *resp_len)
  453. {
  454. int ret;
  455. /* send command */
  456. ret = ath10k_usb_submit_ctrl_out(ar, req_val, 0, 0,
  457. req_buf, req_len);
  458. if (ret)
  459. goto err;
  460. /* get response */
  461. if (resp_buf) {
  462. ret = ath10k_usb_submit_ctrl_in(ar, resp_val, 0, 0,
  463. resp_buf, *resp_len);
  464. if (ret)
  465. goto err;
  466. }
  467. return 0;
  468. err:
  469. return ret;
  470. }
  471. static int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
  472. size_t buf_len)
  473. {
  474. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  475. struct ath10k_usb_ctrl_diag_cmd_read *cmd;
  476. u32 resp_len;
  477. int ret;
  478. if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read))
  479. return -EINVAL;
  480. cmd = (struct ath10k_usb_ctrl_diag_cmd_read *)ar_usb->diag_cmd_buffer;
  481. memset(cmd, 0, sizeof(*cmd));
  482. cmd->cmd = ATH10K_USB_CTRL_DIAG_CC_READ;
  483. cmd->address = cpu_to_le32(address);
  484. resp_len = sizeof(struct ath10k_usb_ctrl_diag_resp_read);
  485. ret = ath10k_usb_ctrl_msg_exchange(ar,
  486. ATH10K_USB_CONTROL_REQ_DIAG_CMD,
  487. (u8 *)cmd,
  488. sizeof(*cmd),
  489. ATH10K_USB_CONTROL_REQ_DIAG_RESP,
  490. ar_usb->diag_resp_buffer, &resp_len);
  491. if (ret)
  492. return ret;
  493. if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read))
  494. return -EMSGSIZE;
  495. memcpy(buf, ar_usb->diag_resp_buffer,
  496. sizeof(struct ath10k_usb_ctrl_diag_resp_read));
  497. return 0;
  498. }
  499. static int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address,
  500. const void *data, int nbytes)
  501. {
  502. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  503. struct ath10k_usb_ctrl_diag_cmd_write *cmd;
  504. int ret;
  505. if (nbytes != sizeof(cmd->value))
  506. return -EINVAL;
  507. cmd = (struct ath10k_usb_ctrl_diag_cmd_write *)ar_usb->diag_cmd_buffer;
  508. memset(cmd, 0, sizeof(*cmd));
  509. cmd->cmd = cpu_to_le32(ATH10K_USB_CTRL_DIAG_CC_WRITE);
  510. cmd->address = cpu_to_le32(address);
  511. memcpy(&cmd->value, data, nbytes);
  512. ret = ath10k_usb_ctrl_msg_exchange(ar,
  513. ATH10K_USB_CONTROL_REQ_DIAG_CMD,
  514. (u8 *)cmd,
  515. sizeof(*cmd),
  516. 0, NULL, NULL);
  517. if (ret)
  518. return ret;
  519. return 0;
  520. }
  521. static int ath10k_usb_bmi_exchange_msg(struct ath10k *ar,
  522. void *req, u32 req_len,
  523. void *resp, u32 *resp_len)
  524. {
  525. int ret;
  526. if (req) {
  527. ret = ath10k_usb_submit_ctrl_out(ar,
  528. ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD,
  529. 0, 0, req, req_len);
  530. if (ret) {
  531. ath10k_warn(ar,
  532. "unable to send the bmi data to the device: %d\n",
  533. ret);
  534. return ret;
  535. }
  536. }
  537. if (resp) {
  538. ret = ath10k_usb_submit_ctrl_in(ar,
  539. ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP,
  540. 0, 0, resp, *resp_len);
  541. if (ret) {
  542. ath10k_warn(ar,
  543. "Unable to read the bmi data from the device: %d\n",
  544. ret);
  545. return ret;
  546. }
  547. }
  548. return 0;
  549. }
  550. static void ath10k_usb_hif_get_default_pipe(struct ath10k *ar,
  551. u8 *ul_pipe, u8 *dl_pipe)
  552. {
  553. *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
  554. *dl_pipe = ATH10K_USB_PIPE_RX_CTRL;
  555. }
  556. static int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id,
  557. u8 *ul_pipe, u8 *dl_pipe)
  558. {
  559. switch (svc_id) {
  560. case ATH10K_HTC_SVC_ID_RSVD_CTRL:
  561. case ATH10K_HTC_SVC_ID_WMI_CONTROL:
  562. *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
  563. /* due to large control packets, shift to data pipe */
  564. *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
  565. break;
  566. case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
  567. *ul_pipe = ATH10K_USB_PIPE_TX_DATA_LP;
  568. /* Disable rxdata2 directly, it will be enabled
  569. * if FW enable rxdata2
  570. */
  571. *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
  572. break;
  573. default:
  574. return -EPERM;
  575. }
  576. return 0;
  577. }
  578. /* This op is currently only used by htc_wait_target if the HTC ready
  579. * message times out. It is not applicable for USB since there is nothing
  580. * we can do if the HTC ready message does not arrive in time.
  581. * TODO: Make this op non mandatory by introducing a NULL check in the
  582. * hif op wrapper.
  583. */
  584. static void ath10k_usb_hif_send_complete_check(struct ath10k *ar,
  585. u8 pipe, int force)
  586. {
  587. }
  588. static int ath10k_usb_hif_power_up(struct ath10k *ar)
  589. {
  590. return 0;
  591. }
  592. static void ath10k_usb_hif_power_down(struct ath10k *ar)
  593. {
  594. ath10k_usb_flush_all(ar);
  595. }
  596. #ifdef CONFIG_PM
  597. static int ath10k_usb_hif_suspend(struct ath10k *ar)
  598. {
  599. return -EOPNOTSUPP;
  600. }
  601. static int ath10k_usb_hif_resume(struct ath10k *ar)
  602. {
  603. return -EOPNOTSUPP;
  604. }
  605. #endif
  606. static const struct ath10k_hif_ops ath10k_usb_hif_ops = {
  607. .tx_sg = ath10k_usb_hif_tx_sg,
  608. .diag_read = ath10k_usb_hif_diag_read,
  609. .diag_write = ath10k_usb_hif_diag_write,
  610. .exchange_bmi_msg = ath10k_usb_bmi_exchange_msg,
  611. .start = ath10k_usb_hif_start,
  612. .stop = ath10k_usb_hif_stop,
  613. .map_service_to_pipe = ath10k_usb_hif_map_service_to_pipe,
  614. .get_default_pipe = ath10k_usb_hif_get_default_pipe,
  615. .send_complete_check = ath10k_usb_hif_send_complete_check,
  616. .get_free_queue_number = ath10k_usb_hif_get_free_queue_number,
  617. .power_up = ath10k_usb_hif_power_up,
  618. .power_down = ath10k_usb_hif_power_down,
  619. #ifdef CONFIG_PM
  620. .suspend = ath10k_usb_hif_suspend,
  621. .resume = ath10k_usb_hif_resume,
  622. #endif
  623. };
  624. static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
  625. {
  626. u8 pipe_num = ATH10K_USB_PIPE_INVALID;
  627. switch (ep_address) {
  628. case ATH10K_USB_EP_ADDR_APP_CTRL_IN:
  629. pipe_num = ATH10K_USB_PIPE_RX_CTRL;
  630. *urb_count = RX_URB_COUNT;
  631. break;
  632. case ATH10K_USB_EP_ADDR_APP_DATA_IN:
  633. pipe_num = ATH10K_USB_PIPE_RX_DATA;
  634. *urb_count = RX_URB_COUNT;
  635. break;
  636. case ATH10K_USB_EP_ADDR_APP_INT_IN:
  637. pipe_num = ATH10K_USB_PIPE_RX_INT;
  638. *urb_count = RX_URB_COUNT;
  639. break;
  640. case ATH10K_USB_EP_ADDR_APP_DATA2_IN:
  641. pipe_num = ATH10K_USB_PIPE_RX_DATA2;
  642. *urb_count = RX_URB_COUNT;
  643. break;
  644. case ATH10K_USB_EP_ADDR_APP_CTRL_OUT:
  645. pipe_num = ATH10K_USB_PIPE_TX_CTRL;
  646. *urb_count = TX_URB_COUNT;
  647. break;
  648. case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT:
  649. pipe_num = ATH10K_USB_PIPE_TX_DATA_LP;
  650. *urb_count = TX_URB_COUNT;
  651. break;
  652. case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT:
  653. pipe_num = ATH10K_USB_PIPE_TX_DATA_MP;
  654. *urb_count = TX_URB_COUNT;
  655. break;
  656. case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT:
  657. pipe_num = ATH10K_USB_PIPE_TX_DATA_HP;
  658. *urb_count = TX_URB_COUNT;
  659. break;
  660. default:
  661. /* note: there may be endpoints not currently used */
  662. break;
  663. }
  664. return pipe_num;
  665. }
  666. static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
  667. struct ath10k_usb_pipe *pipe,
  668. int urb_cnt)
  669. {
  670. struct ath10k_urb_context *urb_context;
  671. int i;
  672. INIT_LIST_HEAD(&pipe->urb_list_head);
  673. init_usb_anchor(&pipe->urb_submitted);
  674. for (i = 0; i < urb_cnt; i++) {
  675. urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
  676. if (!urb_context)
  677. return -ENOMEM;
  678. urb_context->pipe = pipe;
  679. /* we are only allocate the urb contexts here, the actual URB
  680. * is allocated from the kernel as needed to do a transaction
  681. */
  682. pipe->urb_alloc++;
  683. ath10k_usb_free_urb_to_pipe(pipe, urb_context);
  684. }
  685. ath10k_dbg(ar, ATH10K_DBG_USB,
  686. "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n",
  687. pipe->logical_pipe_num, pipe->usb_pipe_handle,
  688. pipe->urb_alloc);
  689. return 0;
  690. }
  691. static int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
  692. struct usb_interface *interface)
  693. {
  694. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  695. struct usb_host_interface *iface_desc = interface->cur_altsetting;
  696. struct usb_endpoint_descriptor *endpoint;
  697. struct ath10k_usb_pipe *pipe;
  698. int ret, i, urbcount;
  699. u8 pipe_num;
  700. ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
  701. /* walk decriptors and setup pipes */
  702. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  703. endpoint = &iface_desc->endpoint[i].desc;
  704. if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
  705. ath10k_dbg(ar, ATH10K_DBG_USB,
  706. "usb %s bulk ep 0x%2.2x maxpktsz %d\n",
  707. ATH10K_USB_IS_DIR_IN
  708. (endpoint->bEndpointAddress) ?
  709. "rx" : "tx", endpoint->bEndpointAddress,
  710. le16_to_cpu(endpoint->wMaxPacketSize));
  711. } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
  712. ath10k_dbg(ar, ATH10K_DBG_USB,
  713. "usb %s int ep 0x%2.2x maxpktsz %d interval %d\n",
  714. ATH10K_USB_IS_DIR_IN
  715. (endpoint->bEndpointAddress) ?
  716. "rx" : "tx", endpoint->bEndpointAddress,
  717. le16_to_cpu(endpoint->wMaxPacketSize),
  718. endpoint->bInterval);
  719. } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
  720. /* TODO for ISO */
  721. ath10k_dbg(ar, ATH10K_DBG_USB,
  722. "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
  723. ATH10K_USB_IS_DIR_IN
  724. (endpoint->bEndpointAddress) ?
  725. "rx" : "tx", endpoint->bEndpointAddress,
  726. le16_to_cpu(endpoint->wMaxPacketSize),
  727. endpoint->bInterval);
  728. }
  729. urbcount = 0;
  730. pipe_num =
  731. ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
  732. &urbcount);
  733. if (pipe_num == ATH10K_USB_PIPE_INVALID)
  734. continue;
  735. pipe = &ar_usb->pipes[pipe_num];
  736. if (pipe->ar_usb)
  737. /* hmmm..pipe was already setup */
  738. continue;
  739. pipe->ar_usb = ar_usb;
  740. pipe->logical_pipe_num = pipe_num;
  741. pipe->ep_address = endpoint->bEndpointAddress;
  742. pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
  743. if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
  744. if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
  745. pipe->usb_pipe_handle =
  746. usb_rcvbulkpipe(ar_usb->udev,
  747. pipe->ep_address);
  748. } else {
  749. pipe->usb_pipe_handle =
  750. usb_sndbulkpipe(ar_usb->udev,
  751. pipe->ep_address);
  752. }
  753. } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
  754. if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
  755. pipe->usb_pipe_handle =
  756. usb_rcvintpipe(ar_usb->udev,
  757. pipe->ep_address);
  758. } else {
  759. pipe->usb_pipe_handle =
  760. usb_sndintpipe(ar_usb->udev,
  761. pipe->ep_address);
  762. }
  763. } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
  764. /* TODO for ISO */
  765. if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
  766. pipe->usb_pipe_handle =
  767. usb_rcvisocpipe(ar_usb->udev,
  768. pipe->ep_address);
  769. } else {
  770. pipe->usb_pipe_handle =
  771. usb_sndisocpipe(ar_usb->udev,
  772. pipe->ep_address);
  773. }
  774. }
  775. pipe->ep_desc = endpoint;
  776. if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
  777. pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
  778. ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
  779. if (ret)
  780. return ret;
  781. }
  782. return 0;
  783. }
  784. static int ath10k_usb_create(struct ath10k *ar,
  785. struct usb_interface *interface)
  786. {
  787. struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
  788. struct usb_device *dev = interface_to_usbdev(interface);
  789. struct ath10k_usb_pipe *pipe;
  790. int ret, i;
  791. usb_set_intfdata(interface, ar_usb);
  792. spin_lock_init(&ar_usb->cs_lock);
  793. ar_usb->udev = dev;
  794. ar_usb->interface = interface;
  795. for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
  796. pipe = &ar_usb->pipes[i];
  797. INIT_WORK(&pipe->io_complete_work,
  798. ath10k_usb_io_comp_work);
  799. skb_queue_head_init(&pipe->io_comp_queue);
  800. }
  801. ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL);
  802. if (!ar_usb->diag_cmd_buffer) {
  803. ret = -ENOMEM;
  804. goto err;
  805. }
  806. ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP,
  807. GFP_KERNEL);
  808. if (!ar_usb->diag_resp_buffer) {
  809. ret = -ENOMEM;
  810. goto err;
  811. }
  812. ret = ath10k_usb_setup_pipe_resources(ar, interface);
  813. if (ret)
  814. goto err;
  815. return 0;
  816. err:
  817. ath10k_usb_destroy(ar);
  818. return ret;
  819. }
  820. /* ath10k usb driver registered functions */
  821. static int ath10k_usb_probe(struct usb_interface *interface,
  822. const struct usb_device_id *id)
  823. {
  824. struct ath10k *ar;
  825. struct ath10k_usb *ar_usb;
  826. struct usb_device *dev = interface_to_usbdev(interface);
  827. int ret, vendor_id, product_id;
  828. enum ath10k_hw_rev hw_rev;
  829. u32 chip_id;
  830. /* Assumption: All USB based chipsets (so far) are QCA9377 based.
  831. * If there will be newer chipsets that does not use the hw reg
  832. * setup as defined in qca6174_regs and qca6174_values, this
  833. * assumption is no longer valid and hw_rev must be setup differently
  834. * depending on chipset.
  835. */
  836. hw_rev = ATH10K_HW_QCA9377;
  837. ar = ath10k_core_create(sizeof(*ar_usb), &dev->dev, ATH10K_BUS_USB,
  838. hw_rev, &ath10k_usb_hif_ops);
  839. if (!ar) {
  840. dev_err(&dev->dev, "failed to allocate core\n");
  841. return -ENOMEM;
  842. }
  843. usb_get_dev(dev);
  844. vendor_id = le16_to_cpu(dev->descriptor.idVendor);
  845. product_id = le16_to_cpu(dev->descriptor.idProduct);
  846. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  847. "usb new func vendor 0x%04x product 0x%04x\n",
  848. vendor_id, product_id);
  849. ar_usb = ath10k_usb_priv(ar);
  850. ret = ath10k_usb_create(ar, interface);
  851. if (ret)
  852. goto err;
  853. ar_usb->ar = ar;
  854. ar->dev_id = product_id;
  855. ar->id.vendor = vendor_id;
  856. ar->id.device = product_id;
  857. /* TODO: don't know yet how to get chip_id with USB */
  858. chip_id = 0;
  859. ret = ath10k_core_register(ar, chip_id);
  860. if (ret) {
  861. ath10k_warn(ar, "failed to register driver core: %d\n", ret);
  862. goto err_usb_destroy;
  863. }
  864. /* TODO: remove this once USB support is fully implemented */
  865. ath10k_warn(ar, "Warning: ath10k USB support is incomplete, don't expect anything to work!\n");
  866. return 0;
  867. err_usb_destroy:
  868. ath10k_usb_destroy(ar);
  869. err:
  870. ath10k_core_destroy(ar);
  871. usb_put_dev(dev);
  872. return ret;
  873. }
  874. static void ath10k_usb_remove(struct usb_interface *interface)
  875. {
  876. struct ath10k_usb *ar_usb;
  877. ar_usb = usb_get_intfdata(interface);
  878. if (!ar_usb)
  879. return;
  880. ath10k_core_unregister(ar_usb->ar);
  881. ath10k_usb_destroy(ar_usb->ar);
  882. usb_put_dev(interface_to_usbdev(interface));
  883. ath10k_core_destroy(ar_usb->ar);
  884. }
  885. #ifdef CONFIG_PM
  886. static int ath10k_usb_pm_suspend(struct usb_interface *interface,
  887. pm_message_t message)
  888. {
  889. struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
  890. ath10k_usb_flush_all(ar_usb->ar);
  891. return 0;
  892. }
  893. static int ath10k_usb_pm_resume(struct usb_interface *interface)
  894. {
  895. struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
  896. struct ath10k *ar = ar_usb->ar;
  897. ath10k_usb_post_recv_transfers(ar,
  898. &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
  899. return 0;
  900. }
  901. #else
  902. #define ath10k_usb_pm_suspend NULL
  903. #define ath10k_usb_pm_resume NULL
  904. #endif
  905. /* table of devices that work with this driver */
  906. static struct usb_device_id ath10k_usb_ids[] = {
  907. {USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */
  908. { /* Terminating entry */ },
  909. };
  910. MODULE_DEVICE_TABLE(usb, ath10k_usb_ids);
  911. static struct usb_driver ath10k_usb_driver = {
  912. .name = "ath10k_usb",
  913. .probe = ath10k_usb_probe,
  914. .suspend = ath10k_usb_pm_suspend,
  915. .resume = ath10k_usb_pm_resume,
  916. .disconnect = ath10k_usb_remove,
  917. .id_table = ath10k_usb_ids,
  918. .supports_autosuspend = true,
  919. .disable_hub_initiated_lpm = 1,
  920. };
  921. module_usb_driver(ath10k_usb_driver);
  922. MODULE_AUTHOR("Atheros Communications, Inc.");
  923. MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices");
  924. MODULE_LICENSE("Dual BSD/GPL");