htc_hst.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. * Copyright (c) 2010 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "htc.h"
  17. static int htc_issue_send(struct htc_target *target, struct sk_buff* skb,
  18. u16 len, u8 flags, u8 epid,
  19. struct ath9k_htc_tx_ctl *tx_ctl)
  20. {
  21. struct htc_frame_hdr *hdr;
  22. struct htc_endpoint *endpoint = &target->endpoint[epid];
  23. int status;
  24. hdr = (struct htc_frame_hdr *)
  25. skb_push(skb, sizeof(struct htc_frame_hdr));
  26. hdr->endpoint_id = epid;
  27. hdr->flags = flags;
  28. hdr->payload_len = cpu_to_be16(len);
  29. status = target->hif->send(target->hif_dev, endpoint->ul_pipeid, skb,
  30. tx_ctl);
  31. return status;
  32. }
  33. static struct htc_endpoint *get_next_avail_ep(struct htc_endpoint *endpoint)
  34. {
  35. enum htc_endpoint_id avail_epid;
  36. for (avail_epid = (ENDPOINT_MAX - 1); avail_epid > ENDPOINT0; avail_epid--)
  37. if (endpoint[avail_epid].service_id == 0)
  38. return &endpoint[avail_epid];
  39. return NULL;
  40. }
  41. static u8 service_to_ulpipe(u16 service_id)
  42. {
  43. switch (service_id) {
  44. case WMI_CONTROL_SVC:
  45. return 4;
  46. case WMI_BEACON_SVC:
  47. case WMI_CAB_SVC:
  48. case WMI_UAPSD_SVC:
  49. case WMI_MGMT_SVC:
  50. case WMI_DATA_VO_SVC:
  51. case WMI_DATA_VI_SVC:
  52. case WMI_DATA_BE_SVC:
  53. case WMI_DATA_BK_SVC:
  54. return 1;
  55. default:
  56. return 0;
  57. }
  58. }
  59. static u8 service_to_dlpipe(u16 service_id)
  60. {
  61. switch (service_id) {
  62. case WMI_CONTROL_SVC:
  63. return 3;
  64. case WMI_BEACON_SVC:
  65. case WMI_CAB_SVC:
  66. case WMI_UAPSD_SVC:
  67. case WMI_MGMT_SVC:
  68. case WMI_DATA_VO_SVC:
  69. case WMI_DATA_VI_SVC:
  70. case WMI_DATA_BE_SVC:
  71. case WMI_DATA_BK_SVC:
  72. return 2;
  73. default:
  74. return 0;
  75. }
  76. }
  77. static void htc_process_target_rdy(struct htc_target *target,
  78. void *buf)
  79. {
  80. struct htc_endpoint *endpoint;
  81. struct htc_ready_msg *htc_ready_msg = (struct htc_ready_msg *) buf;
  82. target->credit_size = be16_to_cpu(htc_ready_msg->credit_size);
  83. endpoint = &target->endpoint[ENDPOINT0];
  84. endpoint->service_id = HTC_CTRL_RSVD_SVC;
  85. endpoint->max_msglen = HTC_MAX_CONTROL_MESSAGE_LENGTH;
  86. atomic_inc(&target->tgt_ready);
  87. complete(&target->target_wait);
  88. }
  89. static void htc_process_conn_rsp(struct htc_target *target,
  90. struct htc_frame_hdr *htc_hdr)
  91. {
  92. struct htc_conn_svc_rspmsg *svc_rspmsg;
  93. struct htc_endpoint *endpoint, *tmp_endpoint = NULL;
  94. u16 service_id;
  95. u16 max_msglen;
  96. enum htc_endpoint_id epid, tepid;
  97. svc_rspmsg = (struct htc_conn_svc_rspmsg *)
  98. ((void *) htc_hdr + sizeof(struct htc_frame_hdr));
  99. if (svc_rspmsg->status == HTC_SERVICE_SUCCESS) {
  100. epid = svc_rspmsg->endpoint_id;
  101. service_id = be16_to_cpu(svc_rspmsg->service_id);
  102. max_msglen = be16_to_cpu(svc_rspmsg->max_msg_len);
  103. endpoint = &target->endpoint[epid];
  104. for (tepid = (ENDPOINT_MAX - 1); tepid > ENDPOINT0; tepid--) {
  105. tmp_endpoint = &target->endpoint[tepid];
  106. if (tmp_endpoint->service_id == service_id) {
  107. tmp_endpoint->service_id = 0;
  108. break;
  109. }
  110. }
  111. if (tepid == ENDPOINT0)
  112. return;
  113. endpoint->service_id = service_id;
  114. endpoint->max_txqdepth = tmp_endpoint->max_txqdepth;
  115. endpoint->ep_callbacks = tmp_endpoint->ep_callbacks;
  116. endpoint->ul_pipeid = tmp_endpoint->ul_pipeid;
  117. endpoint->dl_pipeid = tmp_endpoint->dl_pipeid;
  118. endpoint->max_msglen = max_msglen;
  119. target->conn_rsp_epid = epid;
  120. complete(&target->cmd_wait);
  121. } else {
  122. target->conn_rsp_epid = ENDPOINT_UNUSED;
  123. }
  124. }
  125. static int htc_config_pipe_credits(struct htc_target *target)
  126. {
  127. struct sk_buff *skb;
  128. struct htc_config_pipe_msg *cp_msg;
  129. int ret, time_left;
  130. skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  131. if (!skb) {
  132. dev_err(target->dev, "failed to allocate send buffer\n");
  133. return -ENOMEM;
  134. }
  135. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  136. cp_msg = (struct htc_config_pipe_msg *)
  137. skb_put(skb, sizeof(struct htc_config_pipe_msg));
  138. cp_msg->message_id = cpu_to_be16(HTC_MSG_CONFIG_PIPE_ID);
  139. cp_msg->pipe_id = USB_WLAN_TX_PIPE;
  140. cp_msg->credits = target->credits;
  141. target->htc_flags |= HTC_OP_CONFIG_PIPE_CREDITS;
  142. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0, NULL);
  143. if (ret)
  144. goto err;
  145. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  146. if (!time_left) {
  147. dev_err(target->dev, "HTC credit config timeout\n");
  148. return -ETIMEDOUT;
  149. }
  150. return 0;
  151. err:
  152. kfree_skb(skb);
  153. return -EINVAL;
  154. }
  155. static int htc_setup_complete(struct htc_target *target)
  156. {
  157. struct sk_buff *skb;
  158. struct htc_comp_msg *comp_msg;
  159. int ret = 0, time_left;
  160. skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  161. if (!skb) {
  162. dev_err(target->dev, "failed to allocate send buffer\n");
  163. return -ENOMEM;
  164. }
  165. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  166. comp_msg = (struct htc_comp_msg *)
  167. skb_put(skb, sizeof(struct htc_comp_msg));
  168. comp_msg->msg_id = cpu_to_be16(HTC_MSG_SETUP_COMPLETE_ID);
  169. target->htc_flags |= HTC_OP_START_WAIT;
  170. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0, NULL);
  171. if (ret)
  172. goto err;
  173. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  174. if (!time_left) {
  175. dev_err(target->dev, "HTC start timeout\n");
  176. return -ETIMEDOUT;
  177. }
  178. return 0;
  179. err:
  180. kfree_skb(skb);
  181. return -EINVAL;
  182. }
  183. /* HTC APIs */
  184. int htc_init(struct htc_target *target)
  185. {
  186. int ret;
  187. ret = htc_config_pipe_credits(target);
  188. if (ret)
  189. return ret;
  190. return htc_setup_complete(target);
  191. }
  192. int htc_connect_service(struct htc_target *target,
  193. struct htc_service_connreq *service_connreq,
  194. enum htc_endpoint_id *conn_rsp_epid)
  195. {
  196. struct sk_buff *skb;
  197. struct htc_endpoint *endpoint;
  198. struct htc_conn_svc_msg *conn_msg;
  199. int ret, time_left;
  200. /* Find an available endpoint */
  201. endpoint = get_next_avail_ep(target->endpoint);
  202. if (!endpoint) {
  203. dev_err(target->dev, "Endpoint is not available for"
  204. "service %d\n", service_connreq->service_id);
  205. return -EINVAL;
  206. }
  207. endpoint->service_id = service_connreq->service_id;
  208. endpoint->max_txqdepth = service_connreq->max_send_qdepth;
  209. endpoint->ul_pipeid = service_to_ulpipe(service_connreq->service_id);
  210. endpoint->dl_pipeid = service_to_dlpipe(service_connreq->service_id);
  211. endpoint->ep_callbacks = service_connreq->ep_callbacks;
  212. skb = alloc_skb(sizeof(struct htc_conn_svc_msg) +
  213. sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  214. if (!skb) {
  215. dev_err(target->dev, "Failed to allocate buf to send"
  216. "service connect req\n");
  217. return -ENOMEM;
  218. }
  219. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  220. conn_msg = (struct htc_conn_svc_msg *)
  221. skb_put(skb, sizeof(struct htc_conn_svc_msg));
  222. conn_msg->service_id = cpu_to_be16(service_connreq->service_id);
  223. conn_msg->msg_id = cpu_to_be16(HTC_MSG_CONNECT_SERVICE_ID);
  224. conn_msg->con_flags = cpu_to_be16(service_connreq->con_flags);
  225. conn_msg->dl_pipeid = endpoint->dl_pipeid;
  226. conn_msg->ul_pipeid = endpoint->ul_pipeid;
  227. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0, NULL);
  228. if (ret)
  229. goto err;
  230. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  231. if (!time_left) {
  232. dev_err(target->dev, "Service connection timeout for: %d\n",
  233. service_connreq->service_id);
  234. return -ETIMEDOUT;
  235. }
  236. *conn_rsp_epid = target->conn_rsp_epid;
  237. return 0;
  238. err:
  239. kfree_skb(skb);
  240. return ret;
  241. }
  242. int htc_send(struct htc_target *target, struct sk_buff *skb,
  243. enum htc_endpoint_id epid, struct ath9k_htc_tx_ctl *tx_ctl)
  244. {
  245. return htc_issue_send(target, skb, skb->len, 0, epid, tx_ctl);
  246. }
  247. void htc_stop(struct htc_target *target)
  248. {
  249. enum htc_endpoint_id epid;
  250. struct htc_endpoint *endpoint;
  251. for (epid = ENDPOINT0; epid < ENDPOINT_MAX; epid++) {
  252. endpoint = &target->endpoint[epid];
  253. if (endpoint->service_id != 0)
  254. target->hif->stop(target->hif_dev, endpoint->ul_pipeid);
  255. }
  256. }
  257. void htc_start(struct htc_target *target)
  258. {
  259. enum htc_endpoint_id epid;
  260. struct htc_endpoint *endpoint;
  261. for (epid = ENDPOINT0; epid < ENDPOINT_MAX; epid++) {
  262. endpoint = &target->endpoint[epid];
  263. if (endpoint->service_id != 0)
  264. target->hif->start(target->hif_dev,
  265. endpoint->ul_pipeid);
  266. }
  267. }
  268. void ath9k_htc_txcompletion_cb(struct htc_target *htc_handle,
  269. struct sk_buff *skb, bool txok)
  270. {
  271. struct htc_endpoint *endpoint;
  272. struct htc_frame_hdr *htc_hdr = NULL;
  273. if (htc_handle->htc_flags & HTC_OP_CONFIG_PIPE_CREDITS) {
  274. complete(&htc_handle->cmd_wait);
  275. htc_handle->htc_flags &= ~HTC_OP_CONFIG_PIPE_CREDITS;
  276. goto ret;
  277. }
  278. if (htc_handle->htc_flags & HTC_OP_START_WAIT) {
  279. complete(&htc_handle->cmd_wait);
  280. htc_handle->htc_flags &= ~HTC_OP_START_WAIT;
  281. goto ret;
  282. }
  283. if (skb) {
  284. htc_hdr = (struct htc_frame_hdr *) skb->data;
  285. endpoint = &htc_handle->endpoint[htc_hdr->endpoint_id];
  286. skb_pull(skb, sizeof(struct htc_frame_hdr));
  287. if (endpoint->ep_callbacks.tx) {
  288. endpoint->ep_callbacks.tx(endpoint->ep_callbacks.priv,
  289. skb, htc_hdr->endpoint_id,
  290. txok);
  291. }
  292. }
  293. return;
  294. ret:
  295. /* HTC-generated packets are freed here. */
  296. if (htc_hdr && htc_hdr->endpoint_id != ENDPOINT0)
  297. dev_kfree_skb_any(skb);
  298. else
  299. kfree_skb(skb);
  300. }
  301. /*
  302. * HTC Messages are handled directly here and the obtained SKB
  303. * is freed.
  304. *
  305. * Sevice messages (Data, WMI) passed to the corresponding
  306. * endpoint RX handlers, which have to free the SKB.
  307. */
  308. void ath9k_htc_rx_msg(struct htc_target *htc_handle,
  309. struct sk_buff *skb, u32 len, u8 pipe_id)
  310. {
  311. struct htc_frame_hdr *htc_hdr;
  312. enum htc_endpoint_id epid;
  313. struct htc_endpoint *endpoint;
  314. __be16 *msg_id;
  315. if (!htc_handle || !skb)
  316. return;
  317. htc_hdr = (struct htc_frame_hdr *) skb->data;
  318. epid = htc_hdr->endpoint_id;
  319. if (epid >= ENDPOINT_MAX) {
  320. if (pipe_id != USB_REG_IN_PIPE)
  321. dev_kfree_skb_any(skb);
  322. else
  323. kfree_skb(skb);
  324. return;
  325. }
  326. if (epid == ENDPOINT0) {
  327. /* Handle trailer */
  328. if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER) {
  329. if (be32_to_cpu(*(__be32 *) skb->data) == 0x00C60000)
  330. /* Move past the Watchdog pattern */
  331. htc_hdr = (struct htc_frame_hdr *)(skb->data + 4);
  332. }
  333. /* Get the message ID */
  334. msg_id = (__be16 *) ((void *) htc_hdr +
  335. sizeof(struct htc_frame_hdr));
  336. /* Now process HTC messages */
  337. switch (be16_to_cpu(*msg_id)) {
  338. case HTC_MSG_READY_ID:
  339. htc_process_target_rdy(htc_handle, htc_hdr);
  340. break;
  341. case HTC_MSG_CONNECT_SERVICE_RESPONSE_ID:
  342. htc_process_conn_rsp(htc_handle, htc_hdr);
  343. break;
  344. default:
  345. break;
  346. }
  347. kfree_skb(skb);
  348. } else {
  349. if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER)
  350. skb_trim(skb, len - htc_hdr->control[0]);
  351. skb_pull(skb, sizeof(struct htc_frame_hdr));
  352. endpoint = &htc_handle->endpoint[epid];
  353. if (endpoint->ep_callbacks.rx)
  354. endpoint->ep_callbacks.rx(endpoint->ep_callbacks.priv,
  355. skb, epid);
  356. }
  357. }
  358. struct htc_target *ath9k_htc_hw_alloc(void *hif_handle,
  359. struct ath9k_htc_hif *hif,
  360. struct device *dev)
  361. {
  362. struct htc_endpoint *endpoint;
  363. struct htc_target *target;
  364. target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
  365. if (!target) {
  366. printk(KERN_ERR "Unable to allocate memory for"
  367. "target device\n");
  368. return NULL;
  369. }
  370. init_completion(&target->target_wait);
  371. init_completion(&target->cmd_wait);
  372. target->hif = hif;
  373. target->hif_dev = hif_handle;
  374. target->dev = dev;
  375. /* Assign control endpoint pipe IDs */
  376. endpoint = &target->endpoint[ENDPOINT0];
  377. endpoint->ul_pipeid = hif->control_ul_pipe;
  378. endpoint->dl_pipeid = hif->control_dl_pipe;
  379. atomic_set(&target->tgt_ready, 0);
  380. return target;
  381. }
  382. void ath9k_htc_hw_free(struct htc_target *htc)
  383. {
  384. kfree(htc);
  385. }
  386. int ath9k_htc_hw_init(struct htc_target *target,
  387. struct device *dev, u16 devid,
  388. char *product, u32 drv_info)
  389. {
  390. if (ath9k_htc_probe_device(target, dev, devid, product, drv_info)) {
  391. printk(KERN_ERR "Failed to initialize the device\n");
  392. return -ENODEV;
  393. }
  394. return 0;
  395. }
  396. void ath9k_htc_hw_deinit(struct htc_target *target, bool hot_unplug)
  397. {
  398. if (target)
  399. ath9k_htc_disconnect_device(target, hot_unplug);
  400. }