18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright (c) 2010-2011 Atheros Communications Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission to use, copy, modify, and/or distribute this software for any
58c2ecf20Sopenharmony_ci * purpose with or without fee is hereby granted, provided that the above
68c2ecf20Sopenharmony_ci * copyright notice and this permission notice appear in all copies.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98c2ecf20Sopenharmony_ci * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
108c2ecf20Sopenharmony_ci * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
118c2ecf20Sopenharmony_ci * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
128c2ecf20Sopenharmony_ci * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
138c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
148c2ecf20Sopenharmony_ci * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include "htc.h"
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_cistatic int htc_issue_send(struct htc_target *target, struct sk_buff* skb,
228c2ecf20Sopenharmony_ci			  u16 len, u8 flags, u8 epid)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci{
258c2ecf20Sopenharmony_ci	struct htc_frame_hdr *hdr;
268c2ecf20Sopenharmony_ci	struct htc_endpoint *endpoint = &target->endpoint[epid];
278c2ecf20Sopenharmony_ci	int status;
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci	hdr = skb_push(skb, sizeof(struct htc_frame_hdr));
308c2ecf20Sopenharmony_ci	hdr->endpoint_id = epid;
318c2ecf20Sopenharmony_ci	hdr->flags = flags;
328c2ecf20Sopenharmony_ci	hdr->payload_len = cpu_to_be16(len);
338c2ecf20Sopenharmony_ci	memset(hdr->control, 0, sizeof(hdr->control));
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	status = target->hif->send(target->hif_dev, endpoint->ul_pipeid, skb);
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	return status;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic struct htc_endpoint *get_next_avail_ep(struct htc_endpoint *endpoint)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	enum htc_endpoint_id avail_epid;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	for (avail_epid = (ENDPOINT_MAX - 1); avail_epid > ENDPOINT0; avail_epid--)
458c2ecf20Sopenharmony_ci		if (endpoint[avail_epid].service_id == 0)
468c2ecf20Sopenharmony_ci			return &endpoint[avail_epid];
478c2ecf20Sopenharmony_ci	return NULL;
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic u8 service_to_ulpipe(u16 service_id)
518c2ecf20Sopenharmony_ci{
528c2ecf20Sopenharmony_ci	switch (service_id) {
538c2ecf20Sopenharmony_ci	case WMI_CONTROL_SVC:
548c2ecf20Sopenharmony_ci		return 4;
558c2ecf20Sopenharmony_ci	case WMI_BEACON_SVC:
568c2ecf20Sopenharmony_ci	case WMI_CAB_SVC:
578c2ecf20Sopenharmony_ci	case WMI_UAPSD_SVC:
588c2ecf20Sopenharmony_ci	case WMI_MGMT_SVC:
598c2ecf20Sopenharmony_ci	case WMI_DATA_VO_SVC:
608c2ecf20Sopenharmony_ci	case WMI_DATA_VI_SVC:
618c2ecf20Sopenharmony_ci	case WMI_DATA_BE_SVC:
628c2ecf20Sopenharmony_ci	case WMI_DATA_BK_SVC:
638c2ecf20Sopenharmony_ci		return 1;
648c2ecf20Sopenharmony_ci	default:
658c2ecf20Sopenharmony_ci		return 0;
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic u8 service_to_dlpipe(u16 service_id)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	switch (service_id) {
728c2ecf20Sopenharmony_ci	case WMI_CONTROL_SVC:
738c2ecf20Sopenharmony_ci		return 3;
748c2ecf20Sopenharmony_ci	case WMI_BEACON_SVC:
758c2ecf20Sopenharmony_ci	case WMI_CAB_SVC:
768c2ecf20Sopenharmony_ci	case WMI_UAPSD_SVC:
778c2ecf20Sopenharmony_ci	case WMI_MGMT_SVC:
788c2ecf20Sopenharmony_ci	case WMI_DATA_VO_SVC:
798c2ecf20Sopenharmony_ci	case WMI_DATA_VI_SVC:
808c2ecf20Sopenharmony_ci	case WMI_DATA_BE_SVC:
818c2ecf20Sopenharmony_ci	case WMI_DATA_BK_SVC:
828c2ecf20Sopenharmony_ci		return 2;
838c2ecf20Sopenharmony_ci	default:
848c2ecf20Sopenharmony_ci		return 0;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic void htc_process_target_rdy(struct htc_target *target,
898c2ecf20Sopenharmony_ci				   void *buf)
908c2ecf20Sopenharmony_ci{
918c2ecf20Sopenharmony_ci	struct htc_endpoint *endpoint;
928c2ecf20Sopenharmony_ci	struct htc_ready_msg *htc_ready_msg = (struct htc_ready_msg *) buf;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	target->credit_size = be16_to_cpu(htc_ready_msg->credit_size);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	endpoint = &target->endpoint[ENDPOINT0];
978c2ecf20Sopenharmony_ci	endpoint->service_id = HTC_CTRL_RSVD_SVC;
988c2ecf20Sopenharmony_ci	endpoint->max_msglen = HTC_MAX_CONTROL_MESSAGE_LENGTH;
998c2ecf20Sopenharmony_ci	atomic_inc(&target->tgt_ready);
1008c2ecf20Sopenharmony_ci	complete(&target->target_wait);
1018c2ecf20Sopenharmony_ci}
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic void htc_process_conn_rsp(struct htc_target *target,
1048c2ecf20Sopenharmony_ci				 struct htc_frame_hdr *htc_hdr)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct htc_conn_svc_rspmsg *svc_rspmsg;
1078c2ecf20Sopenharmony_ci	struct htc_endpoint *endpoint, *tmp_endpoint = NULL;
1088c2ecf20Sopenharmony_ci	u16 service_id;
1098c2ecf20Sopenharmony_ci	u16 max_msglen;
1108c2ecf20Sopenharmony_ci	enum htc_endpoint_id epid, tepid;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	svc_rspmsg = (struct htc_conn_svc_rspmsg *)
1138c2ecf20Sopenharmony_ci		((void *) htc_hdr + sizeof(struct htc_frame_hdr));
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	if (svc_rspmsg->status == HTC_SERVICE_SUCCESS) {
1168c2ecf20Sopenharmony_ci		epid = svc_rspmsg->endpoint_id;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci		/* Check that the received epid for the endpoint to attach
1198c2ecf20Sopenharmony_ci		 * a new service is valid. ENDPOINT0 can't be used here as it
1208c2ecf20Sopenharmony_ci		 * is already reserved for HTC_CTRL_RSVD_SVC service and thus
1218c2ecf20Sopenharmony_ci		 * should not be modified.
1228c2ecf20Sopenharmony_ci		 */
1238c2ecf20Sopenharmony_ci		if (epid <= ENDPOINT0 || epid >= ENDPOINT_MAX)
1248c2ecf20Sopenharmony_ci			return;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci		service_id = be16_to_cpu(svc_rspmsg->service_id);
1278c2ecf20Sopenharmony_ci		max_msglen = be16_to_cpu(svc_rspmsg->max_msg_len);
1288c2ecf20Sopenharmony_ci		endpoint = &target->endpoint[epid];
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci		for (tepid = (ENDPOINT_MAX - 1); tepid > ENDPOINT0; tepid--) {
1318c2ecf20Sopenharmony_ci			tmp_endpoint = &target->endpoint[tepid];
1328c2ecf20Sopenharmony_ci			if (tmp_endpoint->service_id == service_id) {
1338c2ecf20Sopenharmony_ci				tmp_endpoint->service_id = 0;
1348c2ecf20Sopenharmony_ci				break;
1358c2ecf20Sopenharmony_ci			}
1368c2ecf20Sopenharmony_ci		}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci		if (tepid == ENDPOINT0)
1398c2ecf20Sopenharmony_ci			return;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci		endpoint->service_id = service_id;
1428c2ecf20Sopenharmony_ci		endpoint->max_txqdepth = tmp_endpoint->max_txqdepth;
1438c2ecf20Sopenharmony_ci		endpoint->ep_callbacks = tmp_endpoint->ep_callbacks;
1448c2ecf20Sopenharmony_ci		endpoint->ul_pipeid = tmp_endpoint->ul_pipeid;
1458c2ecf20Sopenharmony_ci		endpoint->dl_pipeid = tmp_endpoint->dl_pipeid;
1468c2ecf20Sopenharmony_ci		endpoint->max_msglen = max_msglen;
1478c2ecf20Sopenharmony_ci		target->conn_rsp_epid = epid;
1488c2ecf20Sopenharmony_ci		complete(&target->cmd_wait);
1498c2ecf20Sopenharmony_ci	} else {
1508c2ecf20Sopenharmony_ci		target->conn_rsp_epid = ENDPOINT_UNUSED;
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci}
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic int htc_config_pipe_credits(struct htc_target *target)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1578c2ecf20Sopenharmony_ci	struct htc_config_pipe_msg *cp_msg;
1588c2ecf20Sopenharmony_ci	int ret;
1598c2ecf20Sopenharmony_ci	unsigned long time_left;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
1628c2ecf20Sopenharmony_ci	if (!skb) {
1638c2ecf20Sopenharmony_ci		dev_err(target->dev, "failed to allocate send buffer\n");
1648c2ecf20Sopenharmony_ci		return -ENOMEM;
1658c2ecf20Sopenharmony_ci	}
1668c2ecf20Sopenharmony_ci	skb_reserve(skb, sizeof(struct htc_frame_hdr));
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	cp_msg = skb_put(skb, sizeof(struct htc_config_pipe_msg));
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	cp_msg->message_id = cpu_to_be16(HTC_MSG_CONFIG_PIPE_ID);
1718c2ecf20Sopenharmony_ci	cp_msg->pipe_id = USB_WLAN_TX_PIPE;
1728c2ecf20Sopenharmony_ci	cp_msg->credits = target->credits;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	target->htc_flags |= HTC_OP_CONFIG_PIPE_CREDITS;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0);
1778c2ecf20Sopenharmony_ci	if (ret)
1788c2ecf20Sopenharmony_ci		goto err;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
1818c2ecf20Sopenharmony_ci	if (!time_left) {
1828c2ecf20Sopenharmony_ci		dev_err(target->dev, "HTC credit config timeout\n");
1838c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
1848c2ecf20Sopenharmony_ci	}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	return 0;
1878c2ecf20Sopenharmony_cierr:
1888c2ecf20Sopenharmony_ci	kfree_skb(skb);
1898c2ecf20Sopenharmony_ci	return -EINVAL;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic int htc_setup_complete(struct htc_target *target)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1958c2ecf20Sopenharmony_ci	struct htc_comp_msg *comp_msg;
1968c2ecf20Sopenharmony_ci	int ret = 0;
1978c2ecf20Sopenharmony_ci	unsigned long time_left;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	skb = alloc_skb(50 + sizeof(struct htc_frame_hdr), GFP_ATOMIC);
2008c2ecf20Sopenharmony_ci	if (!skb) {
2018c2ecf20Sopenharmony_ci		dev_err(target->dev, "failed to allocate send buffer\n");
2028c2ecf20Sopenharmony_ci		return -ENOMEM;
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci	skb_reserve(skb, sizeof(struct htc_frame_hdr));
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	comp_msg = skb_put(skb, sizeof(struct htc_comp_msg));
2078c2ecf20Sopenharmony_ci	comp_msg->msg_id = cpu_to_be16(HTC_MSG_SETUP_COMPLETE_ID);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	target->htc_flags |= HTC_OP_START_WAIT;
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0);
2128c2ecf20Sopenharmony_ci	if (ret)
2138c2ecf20Sopenharmony_ci		goto err;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
2168c2ecf20Sopenharmony_ci	if (!time_left) {
2178c2ecf20Sopenharmony_ci		dev_err(target->dev, "HTC start timeout\n");
2188c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
2198c2ecf20Sopenharmony_ci	}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	return 0;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_cierr:
2248c2ecf20Sopenharmony_ci	kfree_skb(skb);
2258c2ecf20Sopenharmony_ci	return -EINVAL;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/* HTC APIs */
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ciint htc_init(struct htc_target *target)
2318c2ecf20Sopenharmony_ci{
2328c2ecf20Sopenharmony_ci	int ret;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	ret = htc_config_pipe_credits(target);
2358c2ecf20Sopenharmony_ci	if (ret)
2368c2ecf20Sopenharmony_ci		return ret;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	return htc_setup_complete(target);
2398c2ecf20Sopenharmony_ci}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ciint htc_connect_service(struct htc_target *target,
2428c2ecf20Sopenharmony_ci		     struct htc_service_connreq *service_connreq,
2438c2ecf20Sopenharmony_ci		     enum htc_endpoint_id *conn_rsp_epid)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	struct sk_buff *skb;
2468c2ecf20Sopenharmony_ci	struct htc_endpoint *endpoint;
2478c2ecf20Sopenharmony_ci	struct htc_conn_svc_msg *conn_msg;
2488c2ecf20Sopenharmony_ci	int ret;
2498c2ecf20Sopenharmony_ci	unsigned long time_left;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	/* Find an available endpoint */
2528c2ecf20Sopenharmony_ci	endpoint = get_next_avail_ep(target->endpoint);
2538c2ecf20Sopenharmony_ci	if (!endpoint) {
2548c2ecf20Sopenharmony_ci		dev_err(target->dev, "Endpoint is not available for service %d\n",
2558c2ecf20Sopenharmony_ci			service_connreq->service_id);
2568c2ecf20Sopenharmony_ci		return -EINVAL;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	endpoint->service_id = service_connreq->service_id;
2608c2ecf20Sopenharmony_ci	endpoint->max_txqdepth = service_connreq->max_send_qdepth;
2618c2ecf20Sopenharmony_ci	endpoint->ul_pipeid = service_to_ulpipe(service_connreq->service_id);
2628c2ecf20Sopenharmony_ci	endpoint->dl_pipeid = service_to_dlpipe(service_connreq->service_id);
2638c2ecf20Sopenharmony_ci	endpoint->ep_callbacks = service_connreq->ep_callbacks;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	skb = alloc_skb(sizeof(struct htc_conn_svc_msg) +
2668c2ecf20Sopenharmony_ci			    sizeof(struct htc_frame_hdr), GFP_ATOMIC);
2678c2ecf20Sopenharmony_ci	if (!skb) {
2688c2ecf20Sopenharmony_ci		dev_err(target->dev, "Failed to allocate buf to send"
2698c2ecf20Sopenharmony_ci			"service connect req\n");
2708c2ecf20Sopenharmony_ci		return -ENOMEM;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	skb_reserve(skb, sizeof(struct htc_frame_hdr));
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	conn_msg = skb_put(skb, sizeof(struct htc_conn_svc_msg));
2768c2ecf20Sopenharmony_ci	conn_msg->service_id = cpu_to_be16(service_connreq->service_id);
2778c2ecf20Sopenharmony_ci	conn_msg->msg_id = cpu_to_be16(HTC_MSG_CONNECT_SERVICE_ID);
2788c2ecf20Sopenharmony_ci	conn_msg->con_flags = cpu_to_be16(service_connreq->con_flags);
2798c2ecf20Sopenharmony_ci	conn_msg->dl_pipeid = endpoint->dl_pipeid;
2808c2ecf20Sopenharmony_ci	conn_msg->ul_pipeid = endpoint->ul_pipeid;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	/* To prevent infoleak */
2838c2ecf20Sopenharmony_ci	conn_msg->svc_meta_len = 0;
2848c2ecf20Sopenharmony_ci	conn_msg->pad = 0;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	ret = htc_issue_send(target, skb, skb->len, 0, ENDPOINT0);
2878c2ecf20Sopenharmony_ci	if (ret)
2888c2ecf20Sopenharmony_ci		goto err;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	time_left = wait_for_completion_timeout(&target->cmd_wait, HZ);
2918c2ecf20Sopenharmony_ci	if (!time_left) {
2928c2ecf20Sopenharmony_ci		dev_err(target->dev, "Service connection timeout for: %d\n",
2938c2ecf20Sopenharmony_ci			service_connreq->service_id);
2948c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
2958c2ecf20Sopenharmony_ci	}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	*conn_rsp_epid = target->conn_rsp_epid;
2988c2ecf20Sopenharmony_ci	return 0;
2998c2ecf20Sopenharmony_cierr:
3008c2ecf20Sopenharmony_ci	kfree_skb(skb);
3018c2ecf20Sopenharmony_ci	return ret;
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ciint htc_send(struct htc_target *target, struct sk_buff *skb)
3058c2ecf20Sopenharmony_ci{
3068c2ecf20Sopenharmony_ci	struct ath9k_htc_tx_ctl *tx_ctl;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	tx_ctl = HTC_SKB_CB(skb);
3098c2ecf20Sopenharmony_ci	return htc_issue_send(target, skb, skb->len, 0, tx_ctl->epid);
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ciint htc_send_epid(struct htc_target *target, struct sk_buff *skb,
3138c2ecf20Sopenharmony_ci		  enum htc_endpoint_id epid)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	return htc_issue_send(target, skb, skb->len, 0, epid);
3168c2ecf20Sopenharmony_ci}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_civoid htc_stop(struct htc_target *target)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	target->hif->stop(target->hif_dev);
3218c2ecf20Sopenharmony_ci}
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_civoid htc_start(struct htc_target *target)
3248c2ecf20Sopenharmony_ci{
3258c2ecf20Sopenharmony_ci	target->hif->start(target->hif_dev);
3268c2ecf20Sopenharmony_ci}
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_civoid htc_sta_drain(struct htc_target *target, u8 idx)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	target->hif->sta_drain(target->hif_dev, idx);
3318c2ecf20Sopenharmony_ci}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_civoid ath9k_htc_txcompletion_cb(struct htc_target *htc_handle,
3348c2ecf20Sopenharmony_ci			       struct sk_buff *skb, bool txok)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	struct htc_endpoint *endpoint;
3378c2ecf20Sopenharmony_ci	struct htc_frame_hdr *htc_hdr = NULL;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	if (htc_handle->htc_flags & HTC_OP_CONFIG_PIPE_CREDITS) {
3408c2ecf20Sopenharmony_ci		complete(&htc_handle->cmd_wait);
3418c2ecf20Sopenharmony_ci		htc_handle->htc_flags &= ~HTC_OP_CONFIG_PIPE_CREDITS;
3428c2ecf20Sopenharmony_ci		goto ret;
3438c2ecf20Sopenharmony_ci	}
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	if (htc_handle->htc_flags & HTC_OP_START_WAIT) {
3468c2ecf20Sopenharmony_ci		complete(&htc_handle->cmd_wait);
3478c2ecf20Sopenharmony_ci		htc_handle->htc_flags &= ~HTC_OP_START_WAIT;
3488c2ecf20Sopenharmony_ci		goto ret;
3498c2ecf20Sopenharmony_ci	}
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	if (skb) {
3528c2ecf20Sopenharmony_ci		htc_hdr = (struct htc_frame_hdr *) skb->data;
3538c2ecf20Sopenharmony_ci		if (htc_hdr->endpoint_id >= ARRAY_SIZE(htc_handle->endpoint))
3548c2ecf20Sopenharmony_ci			goto ret;
3558c2ecf20Sopenharmony_ci		endpoint = &htc_handle->endpoint[htc_hdr->endpoint_id];
3568c2ecf20Sopenharmony_ci		skb_pull(skb, sizeof(struct htc_frame_hdr));
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci		if (endpoint->ep_callbacks.tx) {
3598c2ecf20Sopenharmony_ci			endpoint->ep_callbacks.tx(endpoint->ep_callbacks.priv,
3608c2ecf20Sopenharmony_ci						  skb, htc_hdr->endpoint_id,
3618c2ecf20Sopenharmony_ci						  txok);
3628c2ecf20Sopenharmony_ci		} else {
3638c2ecf20Sopenharmony_ci			kfree_skb(skb);
3648c2ecf20Sopenharmony_ci		}
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	return;
3688c2ecf20Sopenharmony_ciret:
3698c2ecf20Sopenharmony_ci	kfree_skb(skb);
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic void ath9k_htc_fw_panic_report(struct htc_target *htc_handle,
3738c2ecf20Sopenharmony_ci				      struct sk_buff *skb, u32 len)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	uint32_t *pattern = (uint32_t *)skb->data;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	if (*pattern == 0x33221199 && len >= sizeof(struct htc_panic_bad_vaddr)) {
3788c2ecf20Sopenharmony_ci		struct htc_panic_bad_vaddr *htc_panic;
3798c2ecf20Sopenharmony_ci		htc_panic = (struct htc_panic_bad_vaddr *) skb->data;
3808c2ecf20Sopenharmony_ci		dev_err(htc_handle->dev, "ath: firmware panic! "
3818c2ecf20Sopenharmony_ci			"exccause: 0x%08x; pc: 0x%08x; badvaddr: 0x%08x.\n",
3828c2ecf20Sopenharmony_ci			htc_panic->exccause, htc_panic->pc,
3838c2ecf20Sopenharmony_ci			htc_panic->badvaddr);
3848c2ecf20Sopenharmony_ci		return;
3858c2ecf20Sopenharmony_ci	}
3868c2ecf20Sopenharmony_ci	if (*pattern == 0x33221299) {
3878c2ecf20Sopenharmony_ci		struct htc_panic_bad_epid *htc_panic;
3888c2ecf20Sopenharmony_ci		htc_panic = (struct htc_panic_bad_epid *) skb->data;
3898c2ecf20Sopenharmony_ci		dev_err(htc_handle->dev, "ath: firmware panic! "
3908c2ecf20Sopenharmony_ci			"bad epid: 0x%08x\n", htc_panic->epid);
3918c2ecf20Sopenharmony_ci		return;
3928c2ecf20Sopenharmony_ci	}
3938c2ecf20Sopenharmony_ci	dev_err(htc_handle->dev, "ath: unknown panic pattern!\n");
3948c2ecf20Sopenharmony_ci}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci/*
3978c2ecf20Sopenharmony_ci * HTC Messages are handled directly here and the obtained SKB
3988c2ecf20Sopenharmony_ci * is freed.
3998c2ecf20Sopenharmony_ci *
4008c2ecf20Sopenharmony_ci * Service messages (Data, WMI) are passed to the corresponding
4018c2ecf20Sopenharmony_ci * endpoint RX handlers, which have to free the SKB.
4028c2ecf20Sopenharmony_ci */
4038c2ecf20Sopenharmony_civoid ath9k_htc_rx_msg(struct htc_target *htc_handle,
4048c2ecf20Sopenharmony_ci		      struct sk_buff *skb, u32 len, u8 pipe_id)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	struct htc_frame_hdr *htc_hdr;
4078c2ecf20Sopenharmony_ci	enum htc_endpoint_id epid;
4088c2ecf20Sopenharmony_ci	struct htc_endpoint *endpoint;
4098c2ecf20Sopenharmony_ci	__be16 *msg_id;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	if (!htc_handle || !skb)
4128c2ecf20Sopenharmony_ci		return;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* A valid message requires len >= 8.
4158c2ecf20Sopenharmony_ci	 *
4168c2ecf20Sopenharmony_ci	 *   sizeof(struct htc_frame_hdr) == 8
4178c2ecf20Sopenharmony_ci	 *   sizeof(struct htc_ready_msg) == 8
4188c2ecf20Sopenharmony_ci	 *   sizeof(struct htc_panic_bad_vaddr) == 16
4198c2ecf20Sopenharmony_ci	 *   sizeof(struct htc_panic_bad_epid) == 8
4208c2ecf20Sopenharmony_ci	 */
4218c2ecf20Sopenharmony_ci	if (unlikely(len < sizeof(struct htc_frame_hdr)))
4228c2ecf20Sopenharmony_ci		goto invalid;
4238c2ecf20Sopenharmony_ci	htc_hdr = (struct htc_frame_hdr *) skb->data;
4248c2ecf20Sopenharmony_ci	epid = htc_hdr->endpoint_id;
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci	if (epid == 0x99) {
4278c2ecf20Sopenharmony_ci		ath9k_htc_fw_panic_report(htc_handle, skb, len);
4288c2ecf20Sopenharmony_ci		kfree_skb(skb);
4298c2ecf20Sopenharmony_ci		return;
4308c2ecf20Sopenharmony_ci	}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	if (epid < 0 || epid >= ENDPOINT_MAX) {
4338c2ecf20Sopenharmony_ciinvalid:
4348c2ecf20Sopenharmony_ci		if (pipe_id != USB_REG_IN_PIPE)
4358c2ecf20Sopenharmony_ci			dev_kfree_skb_any(skb);
4368c2ecf20Sopenharmony_ci		else
4378c2ecf20Sopenharmony_ci			kfree_skb(skb);
4388c2ecf20Sopenharmony_ci		return;
4398c2ecf20Sopenharmony_ci	}
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	if (epid == ENDPOINT0) {
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci		/* Handle trailer */
4448c2ecf20Sopenharmony_ci		if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER) {
4458c2ecf20Sopenharmony_ci			if (be32_to_cpu(*(__be32 *) skb->data) == 0x00C60000) {
4468c2ecf20Sopenharmony_ci				/* Move past the Watchdog pattern */
4478c2ecf20Sopenharmony_ci				htc_hdr = (struct htc_frame_hdr *)(skb->data + 4);
4488c2ecf20Sopenharmony_ci				len -= 4;
4498c2ecf20Sopenharmony_ci			}
4508c2ecf20Sopenharmony_ci		}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci		/* Get the message ID */
4538c2ecf20Sopenharmony_ci		if (unlikely(len < sizeof(struct htc_frame_hdr) + sizeof(__be16)))
4548c2ecf20Sopenharmony_ci			goto invalid;
4558c2ecf20Sopenharmony_ci		msg_id = (__be16 *) ((void *) htc_hdr +
4568c2ecf20Sopenharmony_ci				     sizeof(struct htc_frame_hdr));
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci		/* Now process HTC messages */
4598c2ecf20Sopenharmony_ci		switch (be16_to_cpu(*msg_id)) {
4608c2ecf20Sopenharmony_ci		case HTC_MSG_READY_ID:
4618c2ecf20Sopenharmony_ci			if (unlikely(len < sizeof(struct htc_ready_msg)))
4628c2ecf20Sopenharmony_ci				goto invalid;
4638c2ecf20Sopenharmony_ci			htc_process_target_rdy(htc_handle, htc_hdr);
4648c2ecf20Sopenharmony_ci			break;
4658c2ecf20Sopenharmony_ci		case HTC_MSG_CONNECT_SERVICE_RESPONSE_ID:
4668c2ecf20Sopenharmony_ci			if (unlikely(len < sizeof(struct htc_frame_hdr) +
4678c2ecf20Sopenharmony_ci				     sizeof(struct htc_conn_svc_rspmsg)))
4688c2ecf20Sopenharmony_ci				goto invalid;
4698c2ecf20Sopenharmony_ci			htc_process_conn_rsp(htc_handle, htc_hdr);
4708c2ecf20Sopenharmony_ci			break;
4718c2ecf20Sopenharmony_ci		default:
4728c2ecf20Sopenharmony_ci			break;
4738c2ecf20Sopenharmony_ci		}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci		kfree_skb(skb);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	} else {
4788c2ecf20Sopenharmony_ci		if (htc_hdr->flags & HTC_FLAGS_RECV_TRAILER)
4798c2ecf20Sopenharmony_ci			skb_trim(skb, len - htc_hdr->control[0]);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci		skb_pull(skb, sizeof(struct htc_frame_hdr));
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci		endpoint = &htc_handle->endpoint[epid];
4848c2ecf20Sopenharmony_ci		if (endpoint->ep_callbacks.rx)
4858c2ecf20Sopenharmony_ci			endpoint->ep_callbacks.rx(endpoint->ep_callbacks.priv,
4868c2ecf20Sopenharmony_ci						  skb, epid);
4878c2ecf20Sopenharmony_ci		else
4888c2ecf20Sopenharmony_ci			goto invalid;
4898c2ecf20Sopenharmony_ci	}
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistruct htc_target *ath9k_htc_hw_alloc(void *hif_handle,
4938c2ecf20Sopenharmony_ci				      struct ath9k_htc_hif *hif,
4948c2ecf20Sopenharmony_ci				      struct device *dev)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	struct htc_endpoint *endpoint;
4978c2ecf20Sopenharmony_ci	struct htc_target *target;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	target = kzalloc(sizeof(struct htc_target), GFP_KERNEL);
5008c2ecf20Sopenharmony_ci	if (!target)
5018c2ecf20Sopenharmony_ci		return NULL;
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	init_completion(&target->target_wait);
5048c2ecf20Sopenharmony_ci	init_completion(&target->cmd_wait);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	target->hif = hif;
5078c2ecf20Sopenharmony_ci	target->hif_dev = hif_handle;
5088c2ecf20Sopenharmony_ci	target->dev = dev;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	/* Assign control endpoint pipe IDs */
5118c2ecf20Sopenharmony_ci	endpoint = &target->endpoint[ENDPOINT0];
5128c2ecf20Sopenharmony_ci	endpoint->ul_pipeid = hif->control_ul_pipe;
5138c2ecf20Sopenharmony_ci	endpoint->dl_pipeid = hif->control_dl_pipe;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	atomic_set(&target->tgt_ready, 0);
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	return target;
5188c2ecf20Sopenharmony_ci}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_civoid ath9k_htc_hw_free(struct htc_target *htc)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	kfree(htc);
5238c2ecf20Sopenharmony_ci}
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ciint ath9k_htc_hw_init(struct htc_target *target,
5268c2ecf20Sopenharmony_ci		      struct device *dev, u16 devid,
5278c2ecf20Sopenharmony_ci		      char *product, u32 drv_info)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	if (ath9k_htc_probe_device(target, dev, devid, product, drv_info)) {
5308c2ecf20Sopenharmony_ci		pr_err("Failed to initialize the device\n");
5318c2ecf20Sopenharmony_ci		return -ENODEV;
5328c2ecf20Sopenharmony_ci	}
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	return 0;
5358c2ecf20Sopenharmony_ci}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_civoid ath9k_htc_hw_deinit(struct htc_target *target, bool hot_unplug)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	if (target)
5408c2ecf20Sopenharmony_ci		ath9k_htc_disconnect_device(target, hot_unplug);
5418c2ecf20Sopenharmony_ci}
542