htc_hst.c 12 KB

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