usb.c 27 KB

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