usb.c 27 KB

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