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->credits = be16_to_cpu(htc_ready_msg->credits);
  83. target->credit_size = be16_to_cpu(htc_ready_msg->credit_size);
  84. endpoint = &target->endpoint[ENDPOINT0];
  85. endpoint->service_id = HTC_CTRL_RSVD_SVC;
  86. endpoint->max_msglen = HTC_MAX_CONTROL_MESSAGE_LENGTH;
  87. atomic_inc(&target->tgt_ready);
  88. complete(&target->target_wait);
  89. }
  90. static void htc_process_conn_rsp(struct htc_target *target,
  91. struct htc_frame_hdr *htc_hdr)
  92. {
  93. struct htc_conn_svc_rspmsg *svc_rspmsg;
  94. struct htc_endpoint *endpoint, *tmp_endpoint = NULL;
  95. u16 service_id;
  96. u16 max_msglen;
  97. enum htc_endpoint_id epid, tepid;
  98. svc_rspmsg = (struct htc_conn_svc_rspmsg *)
  99. ((void *) htc_hdr + sizeof(struct htc_frame_hdr));
  100. if (svc_rspmsg->status == HTC_SERVICE_SUCCESS) {
  101. epid = svc_rspmsg->endpoint_id;
  102. service_id = be16_to_cpu(svc_rspmsg->service_id);
  103. max_msglen = be16_to_cpu(svc_rspmsg->max_msg_len);
  104. endpoint = &target->endpoint[epid];
  105. for (tepid = (ENDPOINT_MAX - 1); tepid > ENDPOINT0; tepid--) {
  106. tmp_endpoint = &target->endpoint[tepid];
  107. if (tmp_endpoint->service_id == service_id) {
  108. tmp_endpoint->service_id = 0;
  109. break;
  110. }
  111. }
  112. if (tepid == ENDPOINT0)
  113. return;
  114. endpoint->service_id = service_id;
  115. endpoint->max_txqdepth = tmp_endpoint->max_txqdepth;
  116. endpoint->ep_callbacks = tmp_endpoint->ep_callbacks;
  117. endpoint->ul_pipeid = tmp_endpoint->ul_pipeid;
  118. endpoint->dl_pipeid = tmp_endpoint->dl_pipeid;
  119. endpoint->max_msglen = max_msglen;
  120. target->conn_rsp_epid = epid;
  121. complete(&target->cmd_wait);
  122. } else {
  123. target->conn_rsp_epid = ENDPOINT_UNUSED;
  124. }
  125. }
  126. static int htc_config_pipe_credits(struct htc_target *target)
  127. {
  128. struct sk_buff *skb;
  129. struct htc_config_pipe_msg *cp_msg;
  130. int ret, time_left;
  131. skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  132. if (!skb) {
  133. dev_err(target->dev, "failed to allocate send buffer\n");
  134. return -ENOMEM;
  135. }
  136. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  137. cp_msg = (struct htc_config_pipe_msg *)
  138. skb_put(skb, sizeof(struct htc_config_pipe_msg));
  139. cp_msg->message_id = cpu_to_be16(HTC_MSG_CONFIG_PIPE_ID);
  140. cp_msg->pipe_id = USB_WLAN_TX_PIPE;
  141. cp_msg->credits = 28;
  142. target->htc_flags |= HTC_OP_CONFIG_PIPE_CREDITS;
  143. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0, NULL);
  144. if (ret)
  145. goto err;
  146. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  147. if (!time_left) {
  148. dev_err(target->dev, "HTC credit config timeout\n");
  149. return -ETIMEDOUT;
  150. }
  151. return 0;
  152. err:
  153. kfree_skb(skb);
  154. return -EINVAL;
  155. }
  156. static int htc_setup_complete(struct htc_target *target)
  157. {
  158. struct sk_buff *skb;
  159. struct htc_comp_msg *comp_msg;
  160. int ret = 0, time_left;
  161. skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  162. if (!skb) {
  163. dev_err(target->dev, "failed to allocate send buffer\n");
  164. return -ENOMEM;
  165. }
  166. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  167. comp_msg = (struct htc_comp_msg *)
  168. skb_put(skb, sizeof(struct htc_comp_msg));
  169. comp_msg->msg_id = cpu_to_be16(HTC_MSG_SETUP_COMPLETE_ID);
  170. target->htc_flags |= HTC_OP_START_WAIT;
  171. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0, NULL);
  172. if (ret)
  173. goto err;
  174. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  175. if (!time_left) {
  176. dev_err(target->dev, "HTC start timeout\n");
  177. return -ETIMEDOUT;
  178. }
  179. return 0;
  180. err:
  181. kfree_skb(skb);
  182. return -EINVAL;
  183. }
  184. /* HTC APIs */
  185. int htc_init(struct htc_target *target)
  186. {
  187. int ret;
  188. ret = htc_config_pipe_credits(target);
  189. if (ret)
  190. return ret;
  191. return htc_setup_complete(target);
  192. }
  193. int htc_connect_service(struct htc_target *target,
  194. struct htc_service_connreq *service_connreq,
  195. enum htc_endpoint_id *conn_rsp_epid)
  196. {
  197. struct sk_buff *skb;
  198. struct htc_endpoint *endpoint;
  199. struct htc_conn_svc_msg *conn_msg;
  200. int ret, time_left;
  201. /* Find an available endpoint */
  202. endpoint = get_next_avail_ep(target->endpoint);
  203. if (!endpoint) {
  204. dev_err(target->dev, "Endpoint is not available for"
  205. "service %d\n", service_connreq->service_id);
  206. return -EINVAL;
  207. }
  208. endpoint->service_id = service_connreq->service_id;
  209. endpoint->max_txqdepth = service_connreq->max_send_qdepth;
  210. endpoint->ul_pipeid = service_to_ulpipe(service_connreq->service_id);
  211. endpoint->dl_pipeid = service_to_dlpipe(service_connreq->service_id);
  212. endpoint->ep_callbacks = service_connreq->ep_callbacks;
  213. skb = alloc_skb(sizeof(struct htc_conn_svc_msg) +
  214. sizeof(struct htc_frame_hdr), GFP_ATOMIC);
  215. if (!skb) {
  216. dev_err(target->dev, "Failed to allocate buf to send"
  217. "service connect req\n");
  218. return -ENOMEM;
  219. }
  220. skb_reserve(skb, sizeof(struct htc_frame_hdr));
  221. conn_msg = (struct htc_conn_svc_msg *)
  222. skb_put(skb, sizeof(struct htc_conn_svc_msg));
  223. conn_msg->service_id = cpu_to_be16(service_connreq->service_id);
  224. conn_msg->msg_id = cpu_to_be16(HTC_MSG_CONNECT_SERVICE_ID);
  225. conn_msg->con_flags = cpu_to_be16(service_connreq->con_flags);
  226. conn_msg->dl_pipeid = endpoint->dl_pipeid;
  227. conn_msg->ul_pipeid = endpoint->ul_pipeid;
  228. ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0, NULL);
  229. if (ret)
  230. goto err;
  231. time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
  232. if (!time_left) {
  233. dev_err(target->dev, "Service connection timeout for: %d\n",
  234. service_connreq->service_id);
  235. return -ETIMEDOUT;
  236. }
  237. *conn_rsp_epid = target->conn_rsp_epid;
  238. return 0;
  239. err:
  240. kfree_skb(skb);
  241. return ret;
  242. }
  243. int htc_send(struct htc_target *target, struct sk_buff *skb,
  244. enum htc_endpoint_id epid, struct ath9k_htc_tx_ctl *tx_ctl)
  245. {
  246. return htc_issue_send(target, skb, skb->len, 0, epid, tx_ctl);
  247. }
  248. void htc_stop(struct htc_target *target)
  249. {
  250. enum htc_endpoint_id epid;
  251. struct htc_endpoint *endpoint;
  252. for (epid = ENDPOINT0; epid < ENDPOINT_MAX; epid++) {
  253. endpoint = &target->endpoint[epid];
  254. if (endpoint->service_id != 0)
  255. target->hif->stop(target->hif_dev, endpoint->ul_pipeid);
  256. }
  257. }
  258. void htc_start(struct htc_target *target)
  259. {
  260. enum htc_endpoint_id epid;
  261. struct htc_endpoint *endpoint;
  262. for (epid = ENDPOINT0; epid < ENDPOINT_MAX; epid++) {
  263. endpoint = &target->endpoint[epid];
  264. if (endpoint->service_id != 0)
  265. target->hif->start(target->hif_dev,
  266. endpoint->ul_pipeid);
  267. }
  268. }
  269. void ath9k_htc_txcompletion_cb(struct htc_target *htc_handle,
  270. struct sk_buff *skb, bool txok)
  271. {
  272. struct htc_endpoint *endpoint;
  273. struct htc_frame_hdr *htc_hdr = NULL;
  274. if (htc_handle->htc_flags & HTC_OP_CONFIG_PIPE_CREDITS) {
  275. complete(&htc_handle->cmd_wait);
  276. htc_handle->htc_flags &= ~HTC_OP_CONFIG_PIPE_CREDITS;
  277. goto ret;
  278. }
  279. if (htc_handle->htc_flags & HTC_OP_START_WAIT) {
  280. complete(&htc_handle->cmd_wait);
  281. htc_handle->htc_flags &= ~HTC_OP_START_WAIT;
  282. goto ret;
  283. }
  284. if (skb) {
  285. htc_hdr = (struct htc_frame_hdr *) skb->data;
  286. endpoint = &htc_handle->endpoint[htc_hdr->endpoint_id];
  287. skb_pull(skb, sizeof(struct htc_frame_hdr));
  288. if (endpoint->ep_callbacks.tx) {
  289. endpoint->ep_callbacks.tx(endpoint->ep_callbacks.priv,
  290. skb, htc_hdr->endpoint_id,
  291. txok);
  292. }
  293. }
  294. return;
  295. ret:
  296. /* HTC-generated packets are freed here. */
  297. if (htc_hdr && htc_hdr->endpoint_id != ENDPOINT0)
  298. dev_kfree_skb_any(skb);
  299. else
  300. kfree_skb(skb);
  301. }
  302. /*
  303. * HTC Messages are handled directly here and the obtained SKB
  304. * is freed.
  305. *
  306. * Sevice messages (Data, WMI) passed to the corresponding
  307. * endpoint RX handlers, which have to free the SKB.
  308. */
  309. void ath9k_htc_rx_msg(struct htc_target *htc_handle,
  310. struct sk_buff *skb, u32 len, u8 pipe_id)
  311. {
  312. struct htc_frame_hdr *htc_hdr;
  313. enum htc_endpoint_id epid;
  314. struct htc_endpoint *endpoint;
  315. __be16 *msg_id;
  316. if (!htc_handle || !skb)
  317. return;
  318. htc_hdr = (struct htc_frame_hdr *) skb->data;
  319. epid = htc_hdr->endpoint_id;
  320. if (epid >= ENDPOINT_MAX) {
  321. if (pipe_id != USB_REG_IN_PIPE)
  322. dev_kfree_skb_any(skb);
  323. else
  324. kfree_skb(skb);
  325. return;
  326. }
  327. if (epid == ENDPOINT0) {
  328. /* Handle trailer */
  329. if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER) {
  330. if (be32_to_cpu(*(__be32 *) skb->data) == 0x00C60000)
  331. /* Move past the Watchdog pattern */
  332. htc_hdr = (struct htc_frame_hdr *)(skb->data + 4);
  333. }
  334. /* Get the message ID */
  335. msg_id = (__be16 *) ((void *) htc_hdr +
  336. sizeof(struct htc_frame_hdr));
  337. /* Now process HTC messages */
  338. switch (be16_to_cpu(*msg_id)) {
  339. case HTC_MSG_READY_ID:
  340. htc_process_target_rdy(htc_handle, htc_hdr);
  341. break;
  342. case HTC_MSG_CONNECT_SERVICE_RESPONSE_ID:
  343. htc_process_conn_rsp(htc_handle, htc_hdr);
  344. break;
  345. default:
  346. break;
  347. }
  348. kfree_skb(skb);
  349. } else {
  350. if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER)
  351. skb_trim(skb, len - htc_hdr->control[0]);
  352. skb_pull(skb, sizeof(struct htc_frame_hdr));
  353. endpoint = &htc_handle->endpoint[epid];
  354. if (endpoint->ep_callbacks.rx)
  355. endpoint->ep_callbacks.rx(endpoint->ep_callbacks.priv,
  356. skb, epid);
  357. }
  358. }
  359. struct htc_target *ath9k_htc_hw_alloc(void *hif_handle,
  360. struct ath9k_htc_hif *hif,
  361. struct device *dev)
  362. {
  363. struct htc_endpoint *endpoint;
  364. struct htc_target *target;
  365. target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
  366. if (!target) {
  367. printk(KERN_ERR "Unable to allocate memory for"
  368. "target device\n");
  369. return NULL;
  370. }
  371. init_completion(&target->target_wait);
  372. init_completion(&target->cmd_wait);
  373. target->hif = hif;
  374. target->hif_dev = hif_handle;
  375. target->dev = dev;
  376. /* Assign control endpoint pipe IDs */
  377. endpoint = &target->endpoint[ENDPOINT0];
  378. endpoint->ul_pipeid = hif->control_ul_pipe;
  379. endpoint->dl_pipeid = hif->control_dl_pipe;
  380. atomic_set(&target->tgt_ready, 0);
  381. return target;
  382. }
  383. void ath9k_htc_hw_free(struct htc_target *htc)
  384. {
  385. kfree(htc);
  386. }
  387. int ath9k_htc_hw_init(struct htc_target *target,
  388. struct device *dev, u16 devid)
  389. {
  390. if (ath9k_htc_probe_device(target, dev, devid)) {
  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. }