18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: ISC
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) 2007-2011 Atheros Communications Inc.
48c2ecf20Sopenharmony_ci * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
58c2ecf20Sopenharmony_ci * Copyright (c) 2016-2017 Erik Stromdahl <erik.stromdahl@gmail.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/usb.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include "debug.h"
128c2ecf20Sopenharmony_ci#include "core.h"
138c2ecf20Sopenharmony_ci#include "bmi.h"
148c2ecf20Sopenharmony_ci#include "hif.h"
158c2ecf20Sopenharmony_ci#include "htc.h"
168c2ecf20Sopenharmony_ci#include "usb.h"
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_cistatic void ath10k_usb_post_recv_transfers(struct ath10k *ar,
198c2ecf20Sopenharmony_ci					   struct ath10k_usb_pipe *recv_pipe);
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* inlined helper functions */
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic inline enum ath10k_htc_ep_id
248c2ecf20Sopenharmony_cieid_from_htc_hdr(struct ath10k_htc_hdr *htc_hdr)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	return (enum ath10k_htc_ep_id)htc_hdr->eid;
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic inline bool is_trailer_only_msg(struct ath10k_htc_hdr *htc_hdr)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	return __le16_to_cpu(htc_hdr->len) == htc_hdr->trailer_len;
328c2ecf20Sopenharmony_ci}
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* pipe/urb operations */
358c2ecf20Sopenharmony_cistatic struct ath10k_urb_context *
368c2ecf20Sopenharmony_ciath10k_usb_alloc_urb_from_pipe(struct ath10k_usb_pipe *pipe)
378c2ecf20Sopenharmony_ci{
388c2ecf20Sopenharmony_ci	struct ath10k_urb_context *urb_context = NULL;
398c2ecf20Sopenharmony_ci	unsigned long flags;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	/* bail if this pipe is not initialized */
428c2ecf20Sopenharmony_ci	if (!pipe->ar_usb)
438c2ecf20Sopenharmony_ci		return NULL;
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
468c2ecf20Sopenharmony_ci	if (!list_empty(&pipe->urb_list_head)) {
478c2ecf20Sopenharmony_ci		urb_context = list_first_entry(&pipe->urb_list_head,
488c2ecf20Sopenharmony_ci					       struct ath10k_urb_context, link);
498c2ecf20Sopenharmony_ci		list_del(&urb_context->link);
508c2ecf20Sopenharmony_ci		pipe->urb_cnt--;
518c2ecf20Sopenharmony_ci	}
528c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	return urb_context;
558c2ecf20Sopenharmony_ci}
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
588c2ecf20Sopenharmony_ci					struct ath10k_urb_context *urb_context)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	unsigned long flags;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	/* bail if this pipe is not initialized */
638c2ecf20Sopenharmony_ci	if (!pipe->ar_usb)
648c2ecf20Sopenharmony_ci		return;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	pipe->urb_cnt++;
698c2ecf20Sopenharmony_ci	list_add(&urb_context->link, &pipe->urb_list_head);
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	dev_kfree_skb(urb_context->skb);
778c2ecf20Sopenharmony_ci	urb_context->skb = NULL;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cistatic void ath10k_usb_free_pipe_resources(struct ath10k *ar,
838c2ecf20Sopenharmony_ci					   struct ath10k_usb_pipe *pipe)
848c2ecf20Sopenharmony_ci{
858c2ecf20Sopenharmony_ci	struct ath10k_urb_context *urb_context;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	if (!pipe->ar_usb) {
888c2ecf20Sopenharmony_ci		/* nothing allocated for this pipe */
898c2ecf20Sopenharmony_ci		return;
908c2ecf20Sopenharmony_ci	}
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	ath10k_dbg(ar, ATH10K_DBG_USB,
938c2ecf20Sopenharmony_ci		   "usb free resources lpipe %d hpipe 0x%x urbs %d avail %d\n",
948c2ecf20Sopenharmony_ci		   pipe->logical_pipe_num, pipe->usb_pipe_handle,
958c2ecf20Sopenharmony_ci		   pipe->urb_alloc, pipe->urb_cnt);
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	if (pipe->urb_alloc != pipe->urb_cnt) {
988c2ecf20Sopenharmony_ci		ath10k_dbg(ar, ATH10K_DBG_USB,
998c2ecf20Sopenharmony_ci			   "usb urb leak lpipe %d hpipe 0x%x urbs %d avail %d\n",
1008c2ecf20Sopenharmony_ci			   pipe->logical_pipe_num, pipe->usb_pipe_handle,
1018c2ecf20Sopenharmony_ci			   pipe->urb_alloc, pipe->urb_cnt);
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	for (;;) {
1058c2ecf20Sopenharmony_ci		urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		if (!urb_context)
1088c2ecf20Sopenharmony_ci			break;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci		kfree(urb_context);
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
1178c2ecf20Sopenharmony_ci	int i;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	for (i = 0; i < ATH10K_USB_PIPE_MAX; i++)
1208c2ecf20Sopenharmony_ci		ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]);
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* hif usb rx/tx completion functions */
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic void ath10k_usb_recv_complete(struct urb *urb)
1268c2ecf20Sopenharmony_ci{
1278c2ecf20Sopenharmony_ci	struct ath10k_urb_context *urb_context = urb->context;
1288c2ecf20Sopenharmony_ci	struct ath10k_usb_pipe *pipe = urb_context->pipe;
1298c2ecf20Sopenharmony_ci	struct ath10k *ar = pipe->ar_usb->ar;
1308c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1318c2ecf20Sopenharmony_ci	int status = 0;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
1348c2ecf20Sopenharmony_ci		   "usb recv pipe %d stat %d len %d urb 0x%pK\n",
1358c2ecf20Sopenharmony_ci		   pipe->logical_pipe_num, urb->status, urb->actual_length,
1368c2ecf20Sopenharmony_ci		   urb);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	if (urb->status != 0) {
1398c2ecf20Sopenharmony_ci		status = -EIO;
1408c2ecf20Sopenharmony_ci		switch (urb->status) {
1418c2ecf20Sopenharmony_ci		case -ECONNRESET:
1428c2ecf20Sopenharmony_ci		case -ENOENT:
1438c2ecf20Sopenharmony_ci		case -ESHUTDOWN:
1448c2ecf20Sopenharmony_ci			/* no need to spew these errors when device
1458c2ecf20Sopenharmony_ci			 * removed or urb killed due to driver shutdown
1468c2ecf20Sopenharmony_ci			 */
1478c2ecf20Sopenharmony_ci			status = -ECANCELED;
1488c2ecf20Sopenharmony_ci			break;
1498c2ecf20Sopenharmony_ci		default:
1508c2ecf20Sopenharmony_ci			ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
1518c2ecf20Sopenharmony_ci				   "usb recv pipe %d ep 0x%2.2x failed: %d\n",
1528c2ecf20Sopenharmony_ci				   pipe->logical_pipe_num,
1538c2ecf20Sopenharmony_ci				   pipe->ep_address, urb->status);
1548c2ecf20Sopenharmony_ci			break;
1558c2ecf20Sopenharmony_ci		}
1568c2ecf20Sopenharmony_ci		goto cleanup_recv_urb;
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	if (urb->actual_length == 0)
1608c2ecf20Sopenharmony_ci		goto cleanup_recv_urb;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	skb = urb_context->skb;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	/* we are going to pass it up */
1658c2ecf20Sopenharmony_ci	urb_context->skb = NULL;
1668c2ecf20Sopenharmony_ci	skb_put(skb, urb->actual_length);
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/* note: queue implements a lock */
1698c2ecf20Sopenharmony_ci	skb_queue_tail(&pipe->io_comp_queue, skb);
1708c2ecf20Sopenharmony_ci	schedule_work(&pipe->io_complete_work);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cicleanup_recv_urb:
1738c2ecf20Sopenharmony_ci	ath10k_usb_cleanup_recv_urb(urb_context);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	if (status == 0 &&
1768c2ecf20Sopenharmony_ci	    pipe->urb_cnt >= pipe->urb_cnt_thresh) {
1778c2ecf20Sopenharmony_ci		/* our free urbs are piling up, post more transfers */
1788c2ecf20Sopenharmony_ci		ath10k_usb_post_recv_transfers(ar, pipe);
1798c2ecf20Sopenharmony_ci	}
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic void ath10k_usb_transmit_complete(struct urb *urb)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	struct ath10k_urb_context *urb_context = urb->context;
1858c2ecf20Sopenharmony_ci	struct ath10k_usb_pipe *pipe = urb_context->pipe;
1868c2ecf20Sopenharmony_ci	struct ath10k *ar = pipe->ar_usb->ar;
1878c2ecf20Sopenharmony_ci	struct sk_buff *skb;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if (urb->status != 0) {
1908c2ecf20Sopenharmony_ci		ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
1918c2ecf20Sopenharmony_ci			   "pipe: %d, failed:%d\n",
1928c2ecf20Sopenharmony_ci			   pipe->logical_pipe_num, urb->status);
1938c2ecf20Sopenharmony_ci	}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	skb = urb_context->skb;
1968c2ecf20Sopenharmony_ci	urb_context->skb = NULL;
1978c2ecf20Sopenharmony_ci	ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	/* note: queue implements a lock */
2008c2ecf20Sopenharmony_ci	skb_queue_tail(&pipe->io_comp_queue, skb);
2018c2ecf20Sopenharmony_ci	schedule_work(&pipe->io_complete_work);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/* pipe operations */
2058c2ecf20Sopenharmony_cistatic void ath10k_usb_post_recv_transfers(struct ath10k *ar,
2068c2ecf20Sopenharmony_ci					   struct ath10k_usb_pipe *recv_pipe)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	struct ath10k_urb_context *urb_context;
2098c2ecf20Sopenharmony_ci	struct urb *urb;
2108c2ecf20Sopenharmony_ci	int usb_status;
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	for (;;) {
2138c2ecf20Sopenharmony_ci		urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe);
2148c2ecf20Sopenharmony_ci		if (!urb_context)
2158c2ecf20Sopenharmony_ci			break;
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci		urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE);
2188c2ecf20Sopenharmony_ci		if (!urb_context->skb)
2198c2ecf20Sopenharmony_ci			goto err;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		urb = usb_alloc_urb(0, GFP_ATOMIC);
2228c2ecf20Sopenharmony_ci		if (!urb)
2238c2ecf20Sopenharmony_ci			goto err;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci		usb_fill_bulk_urb(urb,
2268c2ecf20Sopenharmony_ci				  recv_pipe->ar_usb->udev,
2278c2ecf20Sopenharmony_ci				  recv_pipe->usb_pipe_handle,
2288c2ecf20Sopenharmony_ci				  urb_context->skb->data,
2298c2ecf20Sopenharmony_ci				  ATH10K_USB_RX_BUFFER_SIZE,
2308c2ecf20Sopenharmony_ci				  ath10k_usb_recv_complete, urb_context);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci		ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
2338c2ecf20Sopenharmony_ci			   "usb bulk recv submit %d 0x%x ep 0x%2.2x len %d buf 0x%pK\n",
2348c2ecf20Sopenharmony_ci			   recv_pipe->logical_pipe_num,
2358c2ecf20Sopenharmony_ci			   recv_pipe->usb_pipe_handle, recv_pipe->ep_address,
2368c2ecf20Sopenharmony_ci			   ATH10K_USB_RX_BUFFER_SIZE, urb_context->skb);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci		usb_anchor_urb(urb, &recv_pipe->urb_submitted);
2398c2ecf20Sopenharmony_ci		usb_status = usb_submit_urb(urb, GFP_ATOMIC);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci		if (usb_status) {
2428c2ecf20Sopenharmony_ci			ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
2438c2ecf20Sopenharmony_ci				   "usb bulk recv failed: %d\n",
2448c2ecf20Sopenharmony_ci				   usb_status);
2458c2ecf20Sopenharmony_ci			usb_unanchor_urb(urb);
2468c2ecf20Sopenharmony_ci			usb_free_urb(urb);
2478c2ecf20Sopenharmony_ci			goto err;
2488c2ecf20Sopenharmony_ci		}
2498c2ecf20Sopenharmony_ci		usb_free_urb(urb);
2508c2ecf20Sopenharmony_ci	}
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	return;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cierr:
2558c2ecf20Sopenharmony_ci	ath10k_usb_cleanup_recv_urb(urb_context);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic void ath10k_usb_flush_all(struct ath10k *ar)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
2618c2ecf20Sopenharmony_ci	int i;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
2648c2ecf20Sopenharmony_ci		if (ar_usb->pipes[i].ar_usb) {
2658c2ecf20Sopenharmony_ci			usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted);
2668c2ecf20Sopenharmony_ci			cancel_work_sync(&ar_usb->pipes[i].io_complete_work);
2678c2ecf20Sopenharmony_ci		}
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic void ath10k_usb_start_recv_pipes(struct ath10k *ar)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	ath10k_usb_post_recv_transfers(ar,
2788c2ecf20Sopenharmony_ci				       &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct ath10k_htc_hdr *htc_hdr;
2848c2ecf20Sopenharmony_ci	struct ath10k_htc_ep *ep;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	htc_hdr = (struct ath10k_htc_hdr *)skb->data;
2878c2ecf20Sopenharmony_ci	ep = &ar->htc.endpoint[htc_hdr->eid];
2888c2ecf20Sopenharmony_ci	ath10k_htc_notify_tx_completion(ep, skb);
2898c2ecf20Sopenharmony_ci	/* The TX complete handler now owns the skb... */
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct ath10k_htc *htc = &ar->htc;
2958c2ecf20Sopenharmony_ci	struct ath10k_htc_hdr *htc_hdr;
2968c2ecf20Sopenharmony_ci	enum ath10k_htc_ep_id eid;
2978c2ecf20Sopenharmony_ci	struct ath10k_htc_ep *ep;
2988c2ecf20Sopenharmony_ci	u16 payload_len;
2998c2ecf20Sopenharmony_ci	u8 *trailer;
3008c2ecf20Sopenharmony_ci	int ret;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	htc_hdr = (struct ath10k_htc_hdr *)skb->data;
3038c2ecf20Sopenharmony_ci	eid = eid_from_htc_hdr(htc_hdr);
3048c2ecf20Sopenharmony_ci	ep = &ar->htc.endpoint[eid];
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	if (ep->service_id == 0) {
3078c2ecf20Sopenharmony_ci		ath10k_warn(ar, "ep %d is not connected\n", eid);
3088c2ecf20Sopenharmony_ci		goto out_free_skb;
3098c2ecf20Sopenharmony_ci	}
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	payload_len = le16_to_cpu(htc_hdr->len);
3128c2ecf20Sopenharmony_ci	if (!payload_len) {
3138c2ecf20Sopenharmony_ci		ath10k_warn(ar, "zero length frame received, firmware crashed?\n");
3148c2ecf20Sopenharmony_ci		goto out_free_skb;
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	if (payload_len < htc_hdr->trailer_len) {
3188c2ecf20Sopenharmony_ci		ath10k_warn(ar, "malformed frame received, firmware crashed?\n");
3198c2ecf20Sopenharmony_ci		goto out_free_skb;
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) {
3238c2ecf20Sopenharmony_ci		trailer = skb->data + sizeof(*htc_hdr) + payload_len -
3248c2ecf20Sopenharmony_ci			  htc_hdr->trailer_len;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci		ret = ath10k_htc_process_trailer(htc,
3278c2ecf20Sopenharmony_ci						 trailer,
3288c2ecf20Sopenharmony_ci						 htc_hdr->trailer_len,
3298c2ecf20Sopenharmony_ci						 eid,
3308c2ecf20Sopenharmony_ci						 NULL,
3318c2ecf20Sopenharmony_ci						 NULL);
3328c2ecf20Sopenharmony_ci		if (ret)
3338c2ecf20Sopenharmony_ci			goto out_free_skb;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		if (is_trailer_only_msg(htc_hdr))
3368c2ecf20Sopenharmony_ci			goto out_free_skb;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci		/* strip off the trailer from the skb since it should not
3398c2ecf20Sopenharmony_ci		 * be passed on to upper layers
3408c2ecf20Sopenharmony_ci		 */
3418c2ecf20Sopenharmony_ci		skb_trim(skb, skb->len - htc_hdr->trailer_len);
3428c2ecf20Sopenharmony_ci	}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	skb_pull(skb, sizeof(*htc_hdr));
3458c2ecf20Sopenharmony_ci	ep->ep_ops.ep_rx_complete(ar, skb);
3468c2ecf20Sopenharmony_ci	/* The RX complete handler now owns the skb... */
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	return;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ciout_free_skb:
3518c2ecf20Sopenharmony_ci	dev_kfree_skb(skb);
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic void ath10k_usb_io_comp_work(struct work_struct *work)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	struct ath10k_usb_pipe *pipe = container_of(work,
3578c2ecf20Sopenharmony_ci						    struct ath10k_usb_pipe,
3588c2ecf20Sopenharmony_ci						    io_complete_work);
3598c2ecf20Sopenharmony_ci	struct ath10k *ar = pipe->ar_usb->ar;
3608c2ecf20Sopenharmony_ci	struct sk_buff *skb;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
3638c2ecf20Sopenharmony_ci		if (pipe->flags & ATH10K_USB_PIPE_FLAG_TX)
3648c2ecf20Sopenharmony_ci			ath10k_usb_tx_complete(ar, skb);
3658c2ecf20Sopenharmony_ci		else
3668c2ecf20Sopenharmony_ci			ath10k_usb_rx_complete(ar, skb);
3678c2ecf20Sopenharmony_ci	}
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci#define ATH10K_USB_MAX_DIAG_CMD (sizeof(struct ath10k_usb_ctrl_diag_cmd_write))
3718c2ecf20Sopenharmony_ci#define ATH10K_USB_MAX_DIAG_RESP (sizeof(struct ath10k_usb_ctrl_diag_resp_read))
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_cistatic void ath10k_usb_destroy(struct ath10k *ar)
3748c2ecf20Sopenharmony_ci{
3758c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	ath10k_usb_flush_all(ar);
3788c2ecf20Sopenharmony_ci	ath10k_usb_cleanup_pipe_resources(ar);
3798c2ecf20Sopenharmony_ci	usb_set_intfdata(ar_usb->interface, NULL);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	kfree(ar_usb->diag_cmd_buffer);
3828c2ecf20Sopenharmony_ci	kfree(ar_usb->diag_resp_buffer);
3838c2ecf20Sopenharmony_ci}
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic int ath10k_usb_hif_start(struct ath10k *ar)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	int i;
3888c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	ath10k_usb_start_recv_pipes(ar);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/* set the TX resource avail threshold for each TX pipe */
3938c2ecf20Sopenharmony_ci	for (i = ATH10K_USB_PIPE_TX_CTRL;
3948c2ecf20Sopenharmony_ci	     i <= ATH10K_USB_PIPE_TX_DATA_HP; i++) {
3958c2ecf20Sopenharmony_ci		ar_usb->pipes[i].urb_cnt_thresh =
3968c2ecf20Sopenharmony_ci		    ar_usb->pipes[i].urb_alloc / 2;
3978c2ecf20Sopenharmony_ci	}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	return 0;
4008c2ecf20Sopenharmony_ci}
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_cistatic int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
4038c2ecf20Sopenharmony_ci				struct ath10k_hif_sg_item *items, int n_items)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
4068c2ecf20Sopenharmony_ci	struct ath10k_usb_pipe *pipe = &ar_usb->pipes[pipe_id];
4078c2ecf20Sopenharmony_ci	struct ath10k_urb_context *urb_context;
4088c2ecf20Sopenharmony_ci	struct sk_buff *skb;
4098c2ecf20Sopenharmony_ci	struct urb *urb;
4108c2ecf20Sopenharmony_ci	int ret, i;
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	for (i = 0; i < n_items; i++) {
4138c2ecf20Sopenharmony_ci		urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
4148c2ecf20Sopenharmony_ci		if (!urb_context) {
4158c2ecf20Sopenharmony_ci			ret = -ENOMEM;
4168c2ecf20Sopenharmony_ci			goto err;
4178c2ecf20Sopenharmony_ci		}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci		skb = items[i].transfer_context;
4208c2ecf20Sopenharmony_ci		urb_context->skb = skb;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci		urb = usb_alloc_urb(0, GFP_ATOMIC);
4238c2ecf20Sopenharmony_ci		if (!urb) {
4248c2ecf20Sopenharmony_ci			ret = -ENOMEM;
4258c2ecf20Sopenharmony_ci			goto err_free_urb_to_pipe;
4268c2ecf20Sopenharmony_ci		}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci		usb_fill_bulk_urb(urb,
4298c2ecf20Sopenharmony_ci				  ar_usb->udev,
4308c2ecf20Sopenharmony_ci				  pipe->usb_pipe_handle,
4318c2ecf20Sopenharmony_ci				  skb->data,
4328c2ecf20Sopenharmony_ci				  skb->len,
4338c2ecf20Sopenharmony_ci				  ath10k_usb_transmit_complete, urb_context);
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci		if (!(skb->len % pipe->max_packet_size)) {
4368c2ecf20Sopenharmony_ci			/* hit a max packet boundary on this pipe */
4378c2ecf20Sopenharmony_ci			urb->transfer_flags |= URB_ZERO_PACKET;
4388c2ecf20Sopenharmony_ci		}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci		usb_anchor_urb(urb, &pipe->urb_submitted);
4418c2ecf20Sopenharmony_ci		ret = usb_submit_urb(urb, GFP_ATOMIC);
4428c2ecf20Sopenharmony_ci		if (ret) {
4438c2ecf20Sopenharmony_ci			ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
4448c2ecf20Sopenharmony_ci				   "usb bulk transmit failed: %d\n", ret);
4458c2ecf20Sopenharmony_ci			usb_unanchor_urb(urb);
4468c2ecf20Sopenharmony_ci			usb_free_urb(urb);
4478c2ecf20Sopenharmony_ci			ret = -EINVAL;
4488c2ecf20Sopenharmony_ci			goto err_free_urb_to_pipe;
4498c2ecf20Sopenharmony_ci		}
4508c2ecf20Sopenharmony_ci
4518c2ecf20Sopenharmony_ci		usb_free_urb(urb);
4528c2ecf20Sopenharmony_ci	}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	return 0;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_cierr_free_urb_to_pipe:
4578c2ecf20Sopenharmony_ci	ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
4588c2ecf20Sopenharmony_cierr:
4598c2ecf20Sopenharmony_ci	return ret;
4608c2ecf20Sopenharmony_ci}
4618c2ecf20Sopenharmony_ci
4628c2ecf20Sopenharmony_cistatic void ath10k_usb_hif_stop(struct ath10k *ar)
4638c2ecf20Sopenharmony_ci{
4648c2ecf20Sopenharmony_ci	ath10k_usb_flush_all(ar);
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id)
4688c2ecf20Sopenharmony_ci{
4698c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	return ar_usb->pipes[pipe_id].urb_cnt;
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic int ath10k_usb_submit_ctrl_out(struct ath10k *ar,
4758c2ecf20Sopenharmony_ci				      u8 req, u16 value, u16 index, void *data,
4768c2ecf20Sopenharmony_ci				      u32 size)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
4798c2ecf20Sopenharmony_ci	u8 *buf = NULL;
4808c2ecf20Sopenharmony_ci	int ret;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	if (size > 0) {
4838c2ecf20Sopenharmony_ci		buf = kmemdup(data, size, GFP_KERNEL);
4848c2ecf20Sopenharmony_ci		if (!buf)
4858c2ecf20Sopenharmony_ci			return -ENOMEM;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	/* note: if successful returns number of bytes transferred */
4898c2ecf20Sopenharmony_ci	ret = usb_control_msg(ar_usb->udev,
4908c2ecf20Sopenharmony_ci			      usb_sndctrlpipe(ar_usb->udev, 0),
4918c2ecf20Sopenharmony_ci			      req,
4928c2ecf20Sopenharmony_ci			      USB_DIR_OUT | USB_TYPE_VENDOR |
4938c2ecf20Sopenharmony_ci			      USB_RECIP_DEVICE, value, index, buf,
4948c2ecf20Sopenharmony_ci			      size, 1000);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	if (ret < 0) {
4978c2ecf20Sopenharmony_ci		ath10k_warn(ar, "Failed to submit usb control message: %d\n",
4988c2ecf20Sopenharmony_ci			    ret);
4998c2ecf20Sopenharmony_ci		kfree(buf);
5008c2ecf20Sopenharmony_ci		return ret;
5018c2ecf20Sopenharmony_ci	}
5028c2ecf20Sopenharmony_ci
5038c2ecf20Sopenharmony_ci	kfree(buf);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	return 0;
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic int ath10k_usb_submit_ctrl_in(struct ath10k *ar,
5098c2ecf20Sopenharmony_ci				     u8 req, u16 value, u16 index, void *data,
5108c2ecf20Sopenharmony_ci				     u32 size)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
5138c2ecf20Sopenharmony_ci	u8 *buf = NULL;
5148c2ecf20Sopenharmony_ci	int ret;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	if (size > 0) {
5178c2ecf20Sopenharmony_ci		buf = kmalloc(size, GFP_KERNEL);
5188c2ecf20Sopenharmony_ci		if (!buf)
5198c2ecf20Sopenharmony_ci			return -ENOMEM;
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	/* note: if successful returns number of bytes transferred */
5238c2ecf20Sopenharmony_ci	ret = usb_control_msg(ar_usb->udev,
5248c2ecf20Sopenharmony_ci			      usb_rcvctrlpipe(ar_usb->udev, 0),
5258c2ecf20Sopenharmony_ci			      req,
5268c2ecf20Sopenharmony_ci			      USB_DIR_IN | USB_TYPE_VENDOR |
5278c2ecf20Sopenharmony_ci			      USB_RECIP_DEVICE, value, index, buf,
5288c2ecf20Sopenharmony_ci			      size, 2000);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (ret < 0) {
5318c2ecf20Sopenharmony_ci		ath10k_warn(ar, "Failed to read usb control message: %d\n",
5328c2ecf20Sopenharmony_ci			    ret);
5338c2ecf20Sopenharmony_ci		kfree(buf);
5348c2ecf20Sopenharmony_ci		return ret;
5358c2ecf20Sopenharmony_ci	}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	memcpy((u8 *)data, buf, size);
5388c2ecf20Sopenharmony_ci
5398c2ecf20Sopenharmony_ci	kfree(buf);
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	return 0;
5428c2ecf20Sopenharmony_ci}
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_cistatic int ath10k_usb_ctrl_msg_exchange(struct ath10k *ar,
5458c2ecf20Sopenharmony_ci					u8 req_val, u8 *req_buf, u32 req_len,
5468c2ecf20Sopenharmony_ci					u8 resp_val, u8 *resp_buf,
5478c2ecf20Sopenharmony_ci					u32 *resp_len)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	int ret;
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_ci	/* send command */
5528c2ecf20Sopenharmony_ci	ret = ath10k_usb_submit_ctrl_out(ar, req_val, 0, 0,
5538c2ecf20Sopenharmony_ci					 req_buf, req_len);
5548c2ecf20Sopenharmony_ci	if (ret)
5558c2ecf20Sopenharmony_ci		goto err;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	/* get response */
5588c2ecf20Sopenharmony_ci	if (resp_buf) {
5598c2ecf20Sopenharmony_ci		ret = ath10k_usb_submit_ctrl_in(ar, resp_val, 0, 0,
5608c2ecf20Sopenharmony_ci						resp_buf, *resp_len);
5618c2ecf20Sopenharmony_ci		if (ret)
5628c2ecf20Sopenharmony_ci			goto err;
5638c2ecf20Sopenharmony_ci	}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	return 0;
5668c2ecf20Sopenharmony_cierr:
5678c2ecf20Sopenharmony_ci	return ret;
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
5718c2ecf20Sopenharmony_ci				    size_t buf_len)
5728c2ecf20Sopenharmony_ci{
5738c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
5748c2ecf20Sopenharmony_ci	struct ath10k_usb_ctrl_diag_cmd_read *cmd;
5758c2ecf20Sopenharmony_ci	u32 resp_len;
5768c2ecf20Sopenharmony_ci	int ret;
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read))
5798c2ecf20Sopenharmony_ci		return -EINVAL;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	cmd = (struct ath10k_usb_ctrl_diag_cmd_read *)ar_usb->diag_cmd_buffer;
5828c2ecf20Sopenharmony_ci	memset(cmd, 0, sizeof(*cmd));
5838c2ecf20Sopenharmony_ci	cmd->cmd = ATH10K_USB_CTRL_DIAG_CC_READ;
5848c2ecf20Sopenharmony_ci	cmd->address = cpu_to_le32(address);
5858c2ecf20Sopenharmony_ci	resp_len = sizeof(struct ath10k_usb_ctrl_diag_resp_read);
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_ci	ret = ath10k_usb_ctrl_msg_exchange(ar,
5888c2ecf20Sopenharmony_ci					   ATH10K_USB_CONTROL_REQ_DIAG_CMD,
5898c2ecf20Sopenharmony_ci					   (u8 *)cmd,
5908c2ecf20Sopenharmony_ci					   sizeof(*cmd),
5918c2ecf20Sopenharmony_ci					   ATH10K_USB_CONTROL_REQ_DIAG_RESP,
5928c2ecf20Sopenharmony_ci					   ar_usb->diag_resp_buffer, &resp_len);
5938c2ecf20Sopenharmony_ci	if (ret)
5948c2ecf20Sopenharmony_ci		return ret;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read))
5978c2ecf20Sopenharmony_ci		return -EMSGSIZE;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	memcpy(buf, ar_usb->diag_resp_buffer,
6008c2ecf20Sopenharmony_ci	       sizeof(struct ath10k_usb_ctrl_diag_resp_read));
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	return 0;
6038c2ecf20Sopenharmony_ci}
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_cistatic int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address,
6068c2ecf20Sopenharmony_ci				     const void *data, int nbytes)
6078c2ecf20Sopenharmony_ci{
6088c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
6098c2ecf20Sopenharmony_ci	struct ath10k_usb_ctrl_diag_cmd_write *cmd;
6108c2ecf20Sopenharmony_ci	int ret;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	if (nbytes != sizeof(cmd->value))
6138c2ecf20Sopenharmony_ci		return -EINVAL;
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	cmd = (struct ath10k_usb_ctrl_diag_cmd_write *)ar_usb->diag_cmd_buffer;
6168c2ecf20Sopenharmony_ci	memset(cmd, 0, sizeof(*cmd));
6178c2ecf20Sopenharmony_ci	cmd->cmd = cpu_to_le32(ATH10K_USB_CTRL_DIAG_CC_WRITE);
6188c2ecf20Sopenharmony_ci	cmd->address = cpu_to_le32(address);
6198c2ecf20Sopenharmony_ci	memcpy(&cmd->value, data, nbytes);
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	ret = ath10k_usb_ctrl_msg_exchange(ar,
6228c2ecf20Sopenharmony_ci					   ATH10K_USB_CONTROL_REQ_DIAG_CMD,
6238c2ecf20Sopenharmony_ci					   (u8 *)cmd,
6248c2ecf20Sopenharmony_ci					   sizeof(*cmd),
6258c2ecf20Sopenharmony_ci					   0, NULL, NULL);
6268c2ecf20Sopenharmony_ci	if (ret)
6278c2ecf20Sopenharmony_ci		return ret;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	return 0;
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cistatic int ath10k_usb_bmi_exchange_msg(struct ath10k *ar,
6338c2ecf20Sopenharmony_ci				       void *req, u32 req_len,
6348c2ecf20Sopenharmony_ci				       void *resp, u32 *resp_len)
6358c2ecf20Sopenharmony_ci{
6368c2ecf20Sopenharmony_ci	int ret;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (req) {
6398c2ecf20Sopenharmony_ci		ret = ath10k_usb_submit_ctrl_out(ar,
6408c2ecf20Sopenharmony_ci						 ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD,
6418c2ecf20Sopenharmony_ci						 0, 0, req, req_len);
6428c2ecf20Sopenharmony_ci		if (ret) {
6438c2ecf20Sopenharmony_ci			ath10k_warn(ar,
6448c2ecf20Sopenharmony_ci				    "unable to send the bmi data to the device: %d\n",
6458c2ecf20Sopenharmony_ci				    ret);
6468c2ecf20Sopenharmony_ci			return ret;
6478c2ecf20Sopenharmony_ci		}
6488c2ecf20Sopenharmony_ci	}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci	if (resp) {
6518c2ecf20Sopenharmony_ci		ret = ath10k_usb_submit_ctrl_in(ar,
6528c2ecf20Sopenharmony_ci						ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP,
6538c2ecf20Sopenharmony_ci						0, 0, resp, *resp_len);
6548c2ecf20Sopenharmony_ci		if (ret) {
6558c2ecf20Sopenharmony_ci			ath10k_warn(ar,
6568c2ecf20Sopenharmony_ci				    "Unable to read the bmi data from the device: %d\n",
6578c2ecf20Sopenharmony_ci				    ret);
6588c2ecf20Sopenharmony_ci			return ret;
6598c2ecf20Sopenharmony_ci		}
6608c2ecf20Sopenharmony_ci	}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	return 0;
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_cistatic void ath10k_usb_hif_get_default_pipe(struct ath10k *ar,
6668c2ecf20Sopenharmony_ci					    u8 *ul_pipe, u8 *dl_pipe)
6678c2ecf20Sopenharmony_ci{
6688c2ecf20Sopenharmony_ci	*ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
6698c2ecf20Sopenharmony_ci	*dl_pipe = ATH10K_USB_PIPE_RX_CTRL;
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cistatic int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id,
6738c2ecf20Sopenharmony_ci					      u8 *ul_pipe, u8 *dl_pipe)
6748c2ecf20Sopenharmony_ci{
6758c2ecf20Sopenharmony_ci	switch (svc_id) {
6768c2ecf20Sopenharmony_ci	case ATH10K_HTC_SVC_ID_RSVD_CTRL:
6778c2ecf20Sopenharmony_ci	case ATH10K_HTC_SVC_ID_WMI_CONTROL:
6788c2ecf20Sopenharmony_ci		*ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
6798c2ecf20Sopenharmony_ci		/* due to large control packets, shift to data pipe */
6808c2ecf20Sopenharmony_ci		*dl_pipe = ATH10K_USB_PIPE_RX_DATA;
6818c2ecf20Sopenharmony_ci		break;
6828c2ecf20Sopenharmony_ci	case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
6838c2ecf20Sopenharmony_ci		*ul_pipe = ATH10K_USB_PIPE_TX_DATA_LP;
6848c2ecf20Sopenharmony_ci		/* Disable rxdata2 directly, it will be enabled
6858c2ecf20Sopenharmony_ci		 * if FW enable rxdata2
6868c2ecf20Sopenharmony_ci		 */
6878c2ecf20Sopenharmony_ci		*dl_pipe = ATH10K_USB_PIPE_RX_DATA;
6888c2ecf20Sopenharmony_ci		break;
6898c2ecf20Sopenharmony_ci	default:
6908c2ecf20Sopenharmony_ci		return -EPERM;
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	return 0;
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_cistatic int ath10k_usb_hif_power_up(struct ath10k *ar,
6978c2ecf20Sopenharmony_ci				   enum ath10k_firmware_mode fw_mode)
6988c2ecf20Sopenharmony_ci{
6998c2ecf20Sopenharmony_ci	return 0;
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic void ath10k_usb_hif_power_down(struct ath10k *ar)
7038c2ecf20Sopenharmony_ci{
7048c2ecf20Sopenharmony_ci	ath10k_usb_flush_all(ar);
7058c2ecf20Sopenharmony_ci}
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_cistatic int ath10k_usb_hif_suspend(struct ath10k *ar)
7108c2ecf20Sopenharmony_ci{
7118c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic int ath10k_usb_hif_resume(struct ath10k *ar)
7158c2ecf20Sopenharmony_ci{
7168c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci#endif
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_cistatic const struct ath10k_hif_ops ath10k_usb_hif_ops = {
7218c2ecf20Sopenharmony_ci	.tx_sg			= ath10k_usb_hif_tx_sg,
7228c2ecf20Sopenharmony_ci	.diag_read		= ath10k_usb_hif_diag_read,
7238c2ecf20Sopenharmony_ci	.diag_write		= ath10k_usb_hif_diag_write,
7248c2ecf20Sopenharmony_ci	.exchange_bmi_msg	= ath10k_usb_bmi_exchange_msg,
7258c2ecf20Sopenharmony_ci	.start			= ath10k_usb_hif_start,
7268c2ecf20Sopenharmony_ci	.stop			= ath10k_usb_hif_stop,
7278c2ecf20Sopenharmony_ci	.map_service_to_pipe	= ath10k_usb_hif_map_service_to_pipe,
7288c2ecf20Sopenharmony_ci	.get_default_pipe	= ath10k_usb_hif_get_default_pipe,
7298c2ecf20Sopenharmony_ci	.get_free_queue_number	= ath10k_usb_hif_get_free_queue_number,
7308c2ecf20Sopenharmony_ci	.power_up		= ath10k_usb_hif_power_up,
7318c2ecf20Sopenharmony_ci	.power_down		= ath10k_usb_hif_power_down,
7328c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
7338c2ecf20Sopenharmony_ci	.suspend		= ath10k_usb_hif_suspend,
7348c2ecf20Sopenharmony_ci	.resume			= ath10k_usb_hif_resume,
7358c2ecf20Sopenharmony_ci#endif
7368c2ecf20Sopenharmony_ci};
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_cistatic u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
7398c2ecf20Sopenharmony_ci{
7408c2ecf20Sopenharmony_ci	u8 pipe_num = ATH10K_USB_PIPE_INVALID;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	switch (ep_address) {
7438c2ecf20Sopenharmony_ci	case ATH10K_USB_EP_ADDR_APP_CTRL_IN:
7448c2ecf20Sopenharmony_ci		pipe_num = ATH10K_USB_PIPE_RX_CTRL;
7458c2ecf20Sopenharmony_ci		*urb_count = RX_URB_COUNT;
7468c2ecf20Sopenharmony_ci		break;
7478c2ecf20Sopenharmony_ci	case ATH10K_USB_EP_ADDR_APP_DATA_IN:
7488c2ecf20Sopenharmony_ci		pipe_num = ATH10K_USB_PIPE_RX_DATA;
7498c2ecf20Sopenharmony_ci		*urb_count = RX_URB_COUNT;
7508c2ecf20Sopenharmony_ci		break;
7518c2ecf20Sopenharmony_ci	case ATH10K_USB_EP_ADDR_APP_INT_IN:
7528c2ecf20Sopenharmony_ci		pipe_num = ATH10K_USB_PIPE_RX_INT;
7538c2ecf20Sopenharmony_ci		*urb_count = RX_URB_COUNT;
7548c2ecf20Sopenharmony_ci		break;
7558c2ecf20Sopenharmony_ci	case ATH10K_USB_EP_ADDR_APP_DATA2_IN:
7568c2ecf20Sopenharmony_ci		pipe_num = ATH10K_USB_PIPE_RX_DATA2;
7578c2ecf20Sopenharmony_ci		*urb_count = RX_URB_COUNT;
7588c2ecf20Sopenharmony_ci		break;
7598c2ecf20Sopenharmony_ci	case ATH10K_USB_EP_ADDR_APP_CTRL_OUT:
7608c2ecf20Sopenharmony_ci		pipe_num = ATH10K_USB_PIPE_TX_CTRL;
7618c2ecf20Sopenharmony_ci		*urb_count = TX_URB_COUNT;
7628c2ecf20Sopenharmony_ci		break;
7638c2ecf20Sopenharmony_ci	case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT:
7648c2ecf20Sopenharmony_ci		pipe_num = ATH10K_USB_PIPE_TX_DATA_LP;
7658c2ecf20Sopenharmony_ci		*urb_count = TX_URB_COUNT;
7668c2ecf20Sopenharmony_ci		break;
7678c2ecf20Sopenharmony_ci	case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT:
7688c2ecf20Sopenharmony_ci		pipe_num = ATH10K_USB_PIPE_TX_DATA_MP;
7698c2ecf20Sopenharmony_ci		*urb_count = TX_URB_COUNT;
7708c2ecf20Sopenharmony_ci		break;
7718c2ecf20Sopenharmony_ci	case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT:
7728c2ecf20Sopenharmony_ci		pipe_num = ATH10K_USB_PIPE_TX_DATA_HP;
7738c2ecf20Sopenharmony_ci		*urb_count = TX_URB_COUNT;
7748c2ecf20Sopenharmony_ci		break;
7758c2ecf20Sopenharmony_ci	default:
7768c2ecf20Sopenharmony_ci		/* note: there may be endpoints not currently used */
7778c2ecf20Sopenharmony_ci		break;
7788c2ecf20Sopenharmony_ci	}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	return pipe_num;
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_cistatic int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
7848c2ecf20Sopenharmony_ci					   struct ath10k_usb_pipe *pipe,
7858c2ecf20Sopenharmony_ci					   int urb_cnt)
7868c2ecf20Sopenharmony_ci{
7878c2ecf20Sopenharmony_ci	struct ath10k_urb_context *urb_context;
7888c2ecf20Sopenharmony_ci	int i;
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&pipe->urb_list_head);
7918c2ecf20Sopenharmony_ci	init_usb_anchor(&pipe->urb_submitted);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	for (i = 0; i < urb_cnt; i++) {
7948c2ecf20Sopenharmony_ci		urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
7958c2ecf20Sopenharmony_ci		if (!urb_context)
7968c2ecf20Sopenharmony_ci			return -ENOMEM;
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci		urb_context->pipe = pipe;
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci		/* we are only allocate the urb contexts here, the actual URB
8018c2ecf20Sopenharmony_ci		 * is allocated from the kernel as needed to do a transaction
8028c2ecf20Sopenharmony_ci		 */
8038c2ecf20Sopenharmony_ci		pipe->urb_alloc++;
8048c2ecf20Sopenharmony_ci		ath10k_usb_free_urb_to_pipe(pipe, urb_context);
8058c2ecf20Sopenharmony_ci	}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	ath10k_dbg(ar, ATH10K_DBG_USB,
8088c2ecf20Sopenharmony_ci		   "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n",
8098c2ecf20Sopenharmony_ci		   pipe->logical_pipe_num, pipe->usb_pipe_handle,
8108c2ecf20Sopenharmony_ci		   pipe->urb_alloc);
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_ci	return 0;
8138c2ecf20Sopenharmony_ci}
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_cistatic int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
8168c2ecf20Sopenharmony_ci					   struct usb_interface *interface)
8178c2ecf20Sopenharmony_ci{
8188c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
8198c2ecf20Sopenharmony_ci	struct usb_host_interface *iface_desc = interface->cur_altsetting;
8208c2ecf20Sopenharmony_ci	struct usb_endpoint_descriptor *endpoint;
8218c2ecf20Sopenharmony_ci	struct ath10k_usb_pipe *pipe;
8228c2ecf20Sopenharmony_ci	int ret, i, urbcount;
8238c2ecf20Sopenharmony_ci	u8 pipe_num;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	/* walk descriptors and setup pipes */
8288c2ecf20Sopenharmony_ci	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
8298c2ecf20Sopenharmony_ci		endpoint = &iface_desc->endpoint[i].desc;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci		if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
8328c2ecf20Sopenharmony_ci			ath10k_dbg(ar, ATH10K_DBG_USB,
8338c2ecf20Sopenharmony_ci				   "usb %s bulk ep 0x%2.2x maxpktsz %d\n",
8348c2ecf20Sopenharmony_ci				   ATH10K_USB_IS_DIR_IN
8358c2ecf20Sopenharmony_ci				   (endpoint->bEndpointAddress) ?
8368c2ecf20Sopenharmony_ci				   "rx" : "tx", endpoint->bEndpointAddress,
8378c2ecf20Sopenharmony_ci				   le16_to_cpu(endpoint->wMaxPacketSize));
8388c2ecf20Sopenharmony_ci		} else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
8398c2ecf20Sopenharmony_ci			ath10k_dbg(ar, ATH10K_DBG_USB,
8408c2ecf20Sopenharmony_ci				   "usb %s int ep 0x%2.2x maxpktsz %d interval %d\n",
8418c2ecf20Sopenharmony_ci				   ATH10K_USB_IS_DIR_IN
8428c2ecf20Sopenharmony_ci				   (endpoint->bEndpointAddress) ?
8438c2ecf20Sopenharmony_ci				   "rx" : "tx", endpoint->bEndpointAddress,
8448c2ecf20Sopenharmony_ci				   le16_to_cpu(endpoint->wMaxPacketSize),
8458c2ecf20Sopenharmony_ci				   endpoint->bInterval);
8468c2ecf20Sopenharmony_ci		} else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
8478c2ecf20Sopenharmony_ci			/* TODO for ISO */
8488c2ecf20Sopenharmony_ci			ath10k_dbg(ar, ATH10K_DBG_USB,
8498c2ecf20Sopenharmony_ci				   "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
8508c2ecf20Sopenharmony_ci				   ATH10K_USB_IS_DIR_IN
8518c2ecf20Sopenharmony_ci				   (endpoint->bEndpointAddress) ?
8528c2ecf20Sopenharmony_ci				   "rx" : "tx", endpoint->bEndpointAddress,
8538c2ecf20Sopenharmony_ci				   le16_to_cpu(endpoint->wMaxPacketSize),
8548c2ecf20Sopenharmony_ci				   endpoint->bInterval);
8558c2ecf20Sopenharmony_ci		}
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci		/* Ignore broken descriptors. */
8588c2ecf20Sopenharmony_ci		if (usb_endpoint_maxp(endpoint) == 0)
8598c2ecf20Sopenharmony_ci			continue;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci		urbcount = 0;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci		pipe_num =
8648c2ecf20Sopenharmony_ci		    ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
8658c2ecf20Sopenharmony_ci						    &urbcount);
8668c2ecf20Sopenharmony_ci		if (pipe_num == ATH10K_USB_PIPE_INVALID)
8678c2ecf20Sopenharmony_ci			continue;
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci		pipe = &ar_usb->pipes[pipe_num];
8708c2ecf20Sopenharmony_ci		if (pipe->ar_usb)
8718c2ecf20Sopenharmony_ci			/* hmmm..pipe was already setup */
8728c2ecf20Sopenharmony_ci			continue;
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci		pipe->ar_usb = ar_usb;
8758c2ecf20Sopenharmony_ci		pipe->logical_pipe_num = pipe_num;
8768c2ecf20Sopenharmony_ci		pipe->ep_address = endpoint->bEndpointAddress;
8778c2ecf20Sopenharmony_ci		pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
8788c2ecf20Sopenharmony_ci
8798c2ecf20Sopenharmony_ci		if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
8808c2ecf20Sopenharmony_ci			if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
8818c2ecf20Sopenharmony_ci				pipe->usb_pipe_handle =
8828c2ecf20Sopenharmony_ci				    usb_rcvbulkpipe(ar_usb->udev,
8838c2ecf20Sopenharmony_ci						    pipe->ep_address);
8848c2ecf20Sopenharmony_ci			} else {
8858c2ecf20Sopenharmony_ci				pipe->usb_pipe_handle =
8868c2ecf20Sopenharmony_ci				    usb_sndbulkpipe(ar_usb->udev,
8878c2ecf20Sopenharmony_ci						    pipe->ep_address);
8888c2ecf20Sopenharmony_ci			}
8898c2ecf20Sopenharmony_ci		} else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
8908c2ecf20Sopenharmony_ci			if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
8918c2ecf20Sopenharmony_ci				pipe->usb_pipe_handle =
8928c2ecf20Sopenharmony_ci				    usb_rcvintpipe(ar_usb->udev,
8938c2ecf20Sopenharmony_ci						   pipe->ep_address);
8948c2ecf20Sopenharmony_ci			} else {
8958c2ecf20Sopenharmony_ci				pipe->usb_pipe_handle =
8968c2ecf20Sopenharmony_ci				    usb_sndintpipe(ar_usb->udev,
8978c2ecf20Sopenharmony_ci						   pipe->ep_address);
8988c2ecf20Sopenharmony_ci			}
8998c2ecf20Sopenharmony_ci		} else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
9008c2ecf20Sopenharmony_ci			/* TODO for ISO */
9018c2ecf20Sopenharmony_ci			if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
9028c2ecf20Sopenharmony_ci				pipe->usb_pipe_handle =
9038c2ecf20Sopenharmony_ci				    usb_rcvisocpipe(ar_usb->udev,
9048c2ecf20Sopenharmony_ci						    pipe->ep_address);
9058c2ecf20Sopenharmony_ci			} else {
9068c2ecf20Sopenharmony_ci				pipe->usb_pipe_handle =
9078c2ecf20Sopenharmony_ci				    usb_sndisocpipe(ar_usb->udev,
9088c2ecf20Sopenharmony_ci						    pipe->ep_address);
9098c2ecf20Sopenharmony_ci			}
9108c2ecf20Sopenharmony_ci		}
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci		pipe->ep_desc = endpoint;
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci		if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
9158c2ecf20Sopenharmony_ci			pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
9168c2ecf20Sopenharmony_ci
9178c2ecf20Sopenharmony_ci		ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
9188c2ecf20Sopenharmony_ci		if (ret)
9198c2ecf20Sopenharmony_ci			return ret;
9208c2ecf20Sopenharmony_ci	}
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	return 0;
9238c2ecf20Sopenharmony_ci}
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_cistatic int ath10k_usb_create(struct ath10k *ar,
9268c2ecf20Sopenharmony_ci			     struct usb_interface *interface)
9278c2ecf20Sopenharmony_ci{
9288c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
9298c2ecf20Sopenharmony_ci	struct usb_device *dev = interface_to_usbdev(interface);
9308c2ecf20Sopenharmony_ci	struct ath10k_usb_pipe *pipe;
9318c2ecf20Sopenharmony_ci	int ret, i;
9328c2ecf20Sopenharmony_ci
9338c2ecf20Sopenharmony_ci	usb_set_intfdata(interface, ar_usb);
9348c2ecf20Sopenharmony_ci	spin_lock_init(&ar_usb->cs_lock);
9358c2ecf20Sopenharmony_ci	ar_usb->udev = dev;
9368c2ecf20Sopenharmony_ci	ar_usb->interface = interface;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
9398c2ecf20Sopenharmony_ci		pipe = &ar_usb->pipes[i];
9408c2ecf20Sopenharmony_ci		INIT_WORK(&pipe->io_complete_work,
9418c2ecf20Sopenharmony_ci			  ath10k_usb_io_comp_work);
9428c2ecf20Sopenharmony_ci		skb_queue_head_init(&pipe->io_comp_queue);
9438c2ecf20Sopenharmony_ci	}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL);
9468c2ecf20Sopenharmony_ci	if (!ar_usb->diag_cmd_buffer) {
9478c2ecf20Sopenharmony_ci		ret = -ENOMEM;
9488c2ecf20Sopenharmony_ci		goto err;
9498c2ecf20Sopenharmony_ci	}
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP,
9528c2ecf20Sopenharmony_ci					   GFP_KERNEL);
9538c2ecf20Sopenharmony_ci	if (!ar_usb->diag_resp_buffer) {
9548c2ecf20Sopenharmony_ci		ret = -ENOMEM;
9558c2ecf20Sopenharmony_ci		goto err;
9568c2ecf20Sopenharmony_ci	}
9578c2ecf20Sopenharmony_ci
9588c2ecf20Sopenharmony_ci	ret = ath10k_usb_setup_pipe_resources(ar, interface);
9598c2ecf20Sopenharmony_ci	if (ret)
9608c2ecf20Sopenharmony_ci		goto err;
9618c2ecf20Sopenharmony_ci
9628c2ecf20Sopenharmony_ci	return 0;
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_cierr:
9658c2ecf20Sopenharmony_ci	ath10k_usb_destroy(ar);
9668c2ecf20Sopenharmony_ci	return ret;
9678c2ecf20Sopenharmony_ci}
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci/* ath10k usb driver registered functions */
9708c2ecf20Sopenharmony_cistatic int ath10k_usb_probe(struct usb_interface *interface,
9718c2ecf20Sopenharmony_ci			    const struct usb_device_id *id)
9728c2ecf20Sopenharmony_ci{
9738c2ecf20Sopenharmony_ci	struct ath10k *ar;
9748c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb;
9758c2ecf20Sopenharmony_ci	struct usb_device *dev = interface_to_usbdev(interface);
9768c2ecf20Sopenharmony_ci	int ret, vendor_id, product_id;
9778c2ecf20Sopenharmony_ci	enum ath10k_hw_rev hw_rev;
9788c2ecf20Sopenharmony_ci	struct ath10k_bus_params bus_params = {};
9798c2ecf20Sopenharmony_ci
9808c2ecf20Sopenharmony_ci	/* Assumption: All USB based chipsets (so far) are QCA9377 based.
9818c2ecf20Sopenharmony_ci	 * If there will be newer chipsets that does not use the hw reg
9828c2ecf20Sopenharmony_ci	 * setup as defined in qca6174_regs and qca6174_values, this
9838c2ecf20Sopenharmony_ci	 * assumption is no longer valid and hw_rev must be setup differently
9848c2ecf20Sopenharmony_ci	 * depending on chipset.
9858c2ecf20Sopenharmony_ci	 */
9868c2ecf20Sopenharmony_ci	hw_rev = ATH10K_HW_QCA9377;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	ar = ath10k_core_create(sizeof(*ar_usb), &dev->dev, ATH10K_BUS_USB,
9898c2ecf20Sopenharmony_ci				hw_rev, &ath10k_usb_hif_ops);
9908c2ecf20Sopenharmony_ci	if (!ar) {
9918c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "failed to allocate core\n");
9928c2ecf20Sopenharmony_ci		return -ENOMEM;
9938c2ecf20Sopenharmony_ci	}
9948c2ecf20Sopenharmony_ci
9958c2ecf20Sopenharmony_ci	usb_get_dev(dev);
9968c2ecf20Sopenharmony_ci	vendor_id = le16_to_cpu(dev->descriptor.idVendor);
9978c2ecf20Sopenharmony_ci	product_id = le16_to_cpu(dev->descriptor.idProduct);
9988c2ecf20Sopenharmony_ci
9998c2ecf20Sopenharmony_ci	ath10k_dbg(ar, ATH10K_DBG_BOOT,
10008c2ecf20Sopenharmony_ci		   "usb new func vendor 0x%04x product 0x%04x\n",
10018c2ecf20Sopenharmony_ci		   vendor_id, product_id);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	ar_usb = ath10k_usb_priv(ar);
10048c2ecf20Sopenharmony_ci	ret = ath10k_usb_create(ar, interface);
10058c2ecf20Sopenharmony_ci	if (ret)
10068c2ecf20Sopenharmony_ci		goto err;
10078c2ecf20Sopenharmony_ci	ar_usb->ar = ar;
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	ar->dev_id = product_id;
10108c2ecf20Sopenharmony_ci	ar->id.vendor = vendor_id;
10118c2ecf20Sopenharmony_ci	ar->id.device = product_id;
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	bus_params.dev_type = ATH10K_DEV_TYPE_HL;
10148c2ecf20Sopenharmony_ci	/* TODO: don't know yet how to get chip_id with USB */
10158c2ecf20Sopenharmony_ci	bus_params.chip_id = 0;
10168c2ecf20Sopenharmony_ci	ret = ath10k_core_register(ar, &bus_params);
10178c2ecf20Sopenharmony_ci	if (ret) {
10188c2ecf20Sopenharmony_ci		ath10k_warn(ar, "failed to register driver core: %d\n", ret);
10198c2ecf20Sopenharmony_ci		goto err_usb_destroy;
10208c2ecf20Sopenharmony_ci	}
10218c2ecf20Sopenharmony_ci
10228c2ecf20Sopenharmony_ci	/* TODO: remove this once USB support is fully implemented */
10238c2ecf20Sopenharmony_ci	ath10k_warn(ar, "Warning: ath10k USB support is incomplete, don't expect anything to work!\n");
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	return 0;
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_cierr_usb_destroy:
10288c2ecf20Sopenharmony_ci	ath10k_usb_destroy(ar);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_cierr:
10318c2ecf20Sopenharmony_ci	ath10k_core_destroy(ar);
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	usb_put_dev(dev);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	return ret;
10368c2ecf20Sopenharmony_ci}
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_cistatic void ath10k_usb_remove(struct usb_interface *interface)
10398c2ecf20Sopenharmony_ci{
10408c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb;
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	ar_usb = usb_get_intfdata(interface);
10438c2ecf20Sopenharmony_ci	if (!ar_usb)
10448c2ecf20Sopenharmony_ci		return;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	ath10k_core_unregister(ar_usb->ar);
10478c2ecf20Sopenharmony_ci	ath10k_usb_destroy(ar_usb->ar);
10488c2ecf20Sopenharmony_ci	usb_put_dev(interface_to_usbdev(interface));
10498c2ecf20Sopenharmony_ci	ath10k_core_destroy(ar_usb->ar);
10508c2ecf20Sopenharmony_ci}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ci#ifdef CONFIG_PM
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_cistatic int ath10k_usb_pm_suspend(struct usb_interface *interface,
10558c2ecf20Sopenharmony_ci				 pm_message_t message)
10568c2ecf20Sopenharmony_ci{
10578c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	ath10k_usb_flush_all(ar_usb->ar);
10608c2ecf20Sopenharmony_ci	return 0;
10618c2ecf20Sopenharmony_ci}
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_cistatic int ath10k_usb_pm_resume(struct usb_interface *interface)
10648c2ecf20Sopenharmony_ci{
10658c2ecf20Sopenharmony_ci	struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
10668c2ecf20Sopenharmony_ci	struct ath10k *ar = ar_usb->ar;
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	ath10k_usb_post_recv_transfers(ar,
10698c2ecf20Sopenharmony_ci				       &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	return 0;
10728c2ecf20Sopenharmony_ci}
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci#else
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci#define ath10k_usb_pm_suspend NULL
10778c2ecf20Sopenharmony_ci#define ath10k_usb_pm_resume NULL
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci#endif
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci/* table of devices that work with this driver */
10828c2ecf20Sopenharmony_cistatic struct usb_device_id ath10k_usb_ids[] = {
10838c2ecf20Sopenharmony_ci	{USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */
10848c2ecf20Sopenharmony_ci	{ /* Terminating entry */ },
10858c2ecf20Sopenharmony_ci};
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(usb, ath10k_usb_ids);
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_cistatic struct usb_driver ath10k_usb_driver = {
10908c2ecf20Sopenharmony_ci	.name = "ath10k_usb",
10918c2ecf20Sopenharmony_ci	.probe = ath10k_usb_probe,
10928c2ecf20Sopenharmony_ci	.suspend = ath10k_usb_pm_suspend,
10938c2ecf20Sopenharmony_ci	.resume = ath10k_usb_pm_resume,
10948c2ecf20Sopenharmony_ci	.disconnect = ath10k_usb_remove,
10958c2ecf20Sopenharmony_ci	.id_table = ath10k_usb_ids,
10968c2ecf20Sopenharmony_ci	.supports_autosuspend = true,
10978c2ecf20Sopenharmony_ci	.disable_hub_initiated_lpm = 1,
10988c2ecf20Sopenharmony_ci};
10998c2ecf20Sopenharmony_ci
11008c2ecf20Sopenharmony_cimodule_usb_driver(ath10k_usb_driver);
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_ciMODULE_AUTHOR("Atheros Communications, Inc.");
11038c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices");
11048c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
1105