xref: /wlan-dirver/qca-wifi-host-cmn/hif/src/usb/hif_usb.c (revision dd4dc88b837a295134aa9869114a2efee0f4894b)
1 /*
2  * Copyright (c) 2013-2019 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <qdf_time.h>
20 #include <qdf_lock.h>
21 #include <qdf_mem.h>
22 #include <qdf_util.h>
23 #include <qdf_defer.h>
24 #include <qdf_atomic.h>
25 #include <qdf_nbuf.h>
26 #include "qdf_net_types.h"
27 #include <hif_usb_internal.h>
28 #include <htc_services.h>
29 #include <hif_debug.h>
30 #define ATH_MODULE_NAME hif
31 #include <a_debug.h>
32 #include "qdf_module.h"
33 #include "hif_usb_internal.h"
34 #include "if_usb.h"
35 #include "usb_api.h"
36 #include "target_type.h"
37 
38 #if defined(WLAN_DEBUG) || defined(DEBUG)
39 static ATH_DEBUG_MASK_DESCRIPTION g_hif_debug_description[] = {
40 	{USB_HIF_DEBUG_CTRL_TRANS, "Control Transfers"},
41 	{USB_HIF_DEBUG_BULK_IN, "BULK In Transfers"},
42 	{USB_HIF_DEBUG_BULK_OUT, "BULK Out Transfers"},
43 	{USB_HIF_DEBUG_DUMP_DATA, "Dump data"},
44 	{USB_HIF_DEBUG_ENUM, "Enumeration"},
45 };
46 
47 ATH_DEBUG_INSTANTIATE_MODULE_VAR(hif,
48 				 "hif",
49 				 "USB Host Interface",
50 				 ATH_DEBUG_MASK_DEFAULTS | ATH_DEBUG_INFO |
51 				 USB_HIF_DEBUG_ENUM,
52 				 ATH_DEBUG_DESCRIPTION_COUNT
53 				 (g_hif_debug_description),
54 				 g_hif_debug_description);
55 
56 #endif
57 
58 #ifdef USB_ISOC_SUPPORT
59 unsigned int hif_usb_isoch_vo = 1;
60 #else
61 unsigned int hif_usb_isoch_vo;
62 #endif
63 unsigned int hif_usb_disable_rxdata2 = 1;
64 
65 /**
66  * usb_hif_usb_transmit_complete() - completion routing for tx urb's
67  * @urb: pointer to urb for which tx completion is called
68  *
69  * Return: none
70  */
71 static void usb_hif_usb_transmit_complete(struct urb *urb)
72 {
73 	struct HIF_URB_CONTEXT *urb_context =
74 		(struct HIF_URB_CONTEXT *)urb->context;
75 	qdf_nbuf_t buf;
76 	struct HIF_USB_PIPE *pipe = urb_context->pipe;
77 	struct hif_usb_send_context *send_context;
78 
79 	HIF_DBG("+%s: pipe: %d, stat:%d, len:%d", __func__,
80 		pipe->logical_pipe_num, urb->status, urb->actual_length);
81 
82 	/* this urb is not pending anymore */
83 	usb_hif_remove_pending_transfer(urb_context);
84 
85 	if (urb->status != 0) {
86 		HIF_ERROR("%s:  pipe: %d, failed:%d",
87 			  __func__, pipe->logical_pipe_num, urb->status);
88 	}
89 
90 	buf = urb_context->buf;
91 	send_context = urb_context->send_context;
92 
93 	if (send_context->new_alloc)
94 		qdf_mem_free(send_context);
95 	else
96 		qdf_nbuf_pull_head(buf, send_context->head_data_len);
97 
98 	urb_context->buf = NULL;
99 	usb_hif_cleanup_transmit_urb(urb_context);
100 
101 	/* note: queue implements a lock */
102 	skb_queue_tail(&pipe->io_comp_queue, buf);
103 	HIF_USB_SCHEDULE_WORK(pipe);
104 
105 	HIF_DBG("-%s", __func__);
106 }
107 
108 /**
109  * hif_send_internal() - HIF internal routine to prepare and submit tx urbs
110  * @hif_usb_device: pointer to HIF_DEVICE_USB structure
111  * @pipe_id: HIF pipe on which data is to be sent
112  * @hdr_buf: any header buf to be prepended, currently ignored
113  * @buf: qdf_nbuf_t containing data to be transmitted
114  * @nbytes: number of bytes to be transmitted
115  *
116  * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
117  */
118 static QDF_STATUS hif_send_internal(struct HIF_DEVICE_USB *hif_usb_device,
119 				    uint8_t pipe_id,
120 				    qdf_nbuf_t hdr_buf,
121 				    qdf_nbuf_t buf, unsigned int nbytes)
122 {
123 	QDF_STATUS status = QDF_STATUS_SUCCESS;
124 	struct HIF_DEVICE_USB *device = hif_usb_device;
125 	struct HIF_USB_PIPE *pipe = &device->pipes[pipe_id];
126 	struct HIF_URB_CONTEXT *urb_context;
127 	uint8_t *data;
128 	uint32_t len;
129 	struct urb *urb;
130 	int usb_status;
131 	int i;
132 	struct hif_usb_send_context *send_context;
133 	uint8_t frag_count;
134 	uint32_t head_data_len, tmp_frag_count = 0;
135 	unsigned char *data_ptr;
136 
137 	HIF_DBG("+%s pipe : %d, buf:0x%pK nbytes %u",
138 		__func__, pipe_id, buf, nbytes);
139 
140 	frag_count = qdf_nbuf_get_num_frags(buf);
141 	if (frag_count == 1) {
142 		/*
143 		 * | hif_usb_send_context | netbuf->data
144 		 */
145 		head_data_len = sizeof(struct hif_usb_send_context);
146 	} else if ((frag_count - 1) <= QDF_NBUF_CB_TX_MAX_EXTRA_FRAGS) {
147 		/*
148 		 * means have extra fragment buf in skb
149 		 * header data length should be total sending length subtract
150 		 * internal data length of netbuf
151 		 * | hif_usb_send_context | fragments except internal buffer |
152 		 * netbuf->data
153 		 */
154 		head_data_len = sizeof(struct hif_usb_send_context);
155 		while (tmp_frag_count < (frag_count - 1)) {
156 			head_data_len =
157 				head_data_len + qdf_nbuf_get_frag_len(buf,
158 						tmp_frag_count);
159 			tmp_frag_count = tmp_frag_count + 1;
160 		}
161 	} else {
162 		/* Extra fragments overflow */
163 		HIF_ERROR("%s Extra fragments count overflow : %d\n",
164 			  __func__, frag_count);
165 		status = QDF_STATUS_E_RESOURCES;
166 		goto err;
167 	}
168 
169 	/* Check whether head room is enough to save extra head data */
170 	if (head_data_len <= qdf_nbuf_headroom(buf)) {
171 		send_context = (struct hif_usb_send_context *)
172 		    qdf_nbuf_push_head(buf, head_data_len);
173 		send_context->new_alloc = false;
174 	} else {
175 		send_context =
176 		    qdf_mem_malloc(sizeof(struct hif_usb_send_context)
177 				   + head_data_len + nbytes);
178 		if (!send_context) {
179 			status = QDF_STATUS_E_NOMEM;
180 			goto err;
181 		}
182 		send_context->new_alloc = true;
183 	}
184 	send_context->netbuf = buf;
185 	send_context->hif_usb_device = hif_usb_device;
186 	send_context->transfer_id = pipe_id;
187 	send_context->head_data_len = head_data_len;
188 	/*
189 	 * Copy data to head part of netbuf or head of allocated buffer.
190 	 * if buffer is new allocated, the last buffer should be copied also.
191 	 * It assume last fragment is internal buffer of netbuf
192 	 * sometime total length of fragments larger than nbytes
193 	 */
194 	data_ptr = (unsigned char *)send_context +
195 				sizeof(struct hif_usb_send_context);
196 	for (i = 0;
197 	     i < (send_context->new_alloc ? frag_count : frag_count - 1); i++) {
198 		int frag_len = qdf_nbuf_get_frag_len(buf, i);
199 		unsigned char *frag_addr = qdf_nbuf_get_frag_vaddr(buf, i);
200 
201 		qdf_mem_copy(data_ptr, frag_addr, frag_len);
202 		data_ptr += frag_len;
203 	}
204 	/* Reset pData pointer and send out */
205 	data_ptr = (unsigned char *)send_context +
206 				sizeof(struct hif_usb_send_context);
207 
208 	urb_context = usb_hif_alloc_urb_from_pipe(pipe);
209 	if (!urb_context) {
210 		/* TODO : note, it is possible to run out of urbs if 2
211 		 * endpoints map to the same pipe ID
212 		 */
213 		HIF_ERROR("%s pipe:%d no urbs left. URB Cnt : %d",
214 			__func__, pipe_id, pipe->urb_cnt);
215 		status = QDF_STATUS_E_RESOURCES;
216 		goto err;
217 	}
218 	urb_context->send_context = send_context;
219 	urb = urb_context->urb;
220 	urb_context->buf = buf;
221 	data = data_ptr;
222 	len = nbytes;
223 
224 	usb_fill_bulk_urb(urb,
225 			  device->udev,
226 			  pipe->usb_pipe_handle,
227 			  data,
228 			  (len % pipe->max_packet_size) ==
229 			  0 ? (len + 1) : len,
230 			  usb_hif_usb_transmit_complete, urb_context);
231 
232 	if ((len % pipe->max_packet_size) == 0)
233 		/* hit a max packet boundary on this pipe */
234 
235 	HIF_DBG
236 	    ("athusb bulk send submit:%d, 0x%X (ep:0x%2.2X), %d bytes",
237 	     pipe->logical_pipe_num, pipe->usb_pipe_handle,
238 	     pipe->ep_address, nbytes);
239 
240 	usb_hif_enqueue_pending_transfer(pipe, urb_context);
241 	usb_status = usb_submit_urb(urb, GFP_ATOMIC);
242 	if (usb_status) {
243 		if (send_context->new_alloc)
244 			qdf_mem_free(send_context);
245 		else
246 			qdf_nbuf_pull_head(buf, head_data_len);
247 		urb_context->buf = NULL;
248 		HIF_ERROR("athusb : usb bulk transmit failed %d",
249 				usb_status);
250 		usb_hif_remove_pending_transfer(urb_context);
251 		usb_hif_cleanup_transmit_urb(urb_context);
252 		status = QDF_STATUS_E_FAILURE;
253 		goto err;
254 	}
255 
256 err:
257 	if (!QDF_IS_STATUS_SUCCESS(status) &&
258 				(status != QDF_STATUS_E_RESOURCES)) {
259 		HIF_ERROR("athusb send failed %d", status);
260 	}
261 
262 	HIF_DBG("-%s pipe : %d", __func__, pipe_id);
263 
264 	return status;
265 }
266 
267 /**
268  * hif_send_head() - HIF routine exposed to upper layers to send data
269  * @scn: pointer to hif_opaque_softc structure
270  * @pipe_id: HIF pipe on which data is to be sent
271  * @transfer_id: endpoint ID on which data is to be sent
272  * @nbytes: number of bytes to be transmitted
273  * @wbuf: qdf_nbuf_t containing data to be transmitted
274  * @hdr_buf: any header buf to be prepended, currently ignored
275  * @data_attr: data_attr field from cvg_nbuf_cb of wbuf
276  *
277  * Return: QDF_STATUS_SUCCESS on success and error QDF status on failure
278  */
279 QDF_STATUS hif_send_head(struct hif_opaque_softc *scn, uint8_t pipe_id,
280 				uint32_t transfer_id, uint32_t nbytes,
281 				qdf_nbuf_t wbuf, uint32_t data_attr)
282 {
283 	QDF_STATUS status = QDF_STATUS_SUCCESS;
284 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(scn);
285 
286 	status = hif_send_internal(device, pipe_id, NULL, wbuf, nbytes);
287 	return status;
288 }
289 
290 /**
291  * hif_get_free_queue_number() - get # of free TX resources in a given HIF pipe
292  * @scn: pointer to hif_opaque_softc structure
293  * @pipe_id: HIF pipe which is being polled for free resources
294  *
295  * Return: # of free resources in pipe_id
296  */
297 uint16_t hif_get_free_queue_number(struct hif_opaque_softc *scn,
298 				   uint8_t pipe_id)
299 {
300 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(scn);
301 
302 	return device->pipes[pipe_id].urb_cnt;
303 }
304 
305 /**
306  * hif_post_init() - copy HTC callbacks to HIF
307  * @scn: pointer to hif_opaque_softc structure
308  * @target: pointer to HTC_TARGET structure
309  * @callbacks: htc callbacks
310  *
311  * Return: none
312  */
313 void hif_post_init(struct hif_opaque_softc *scn, void *target,
314 		struct hif_msg_callbacks *callbacks)
315 {
316 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(scn);
317 
318 	qdf_mem_copy(&device->htc_callbacks, callbacks,
319 			sizeof(device->htc_callbacks));
320 }
321 
322 /**
323  * hif_detach_htc() - remove HTC callbacks from HIF
324  * @scn: pointer to hif_opaque_softc structure
325  *
326  * Return: none
327  */
328 void hif_detach_htc(struct hif_opaque_softc *scn)
329 {
330 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(scn);
331 
332 	usb_hif_flush_all(device);
333 	qdf_mem_zero(&device->htc_callbacks, sizeof(device->htc_callbacks));
334 }
335 
336 /**
337  * hif_usb_device_deinit() - de- init  HIF_DEVICE_USB, cleanup pipe resources
338  * @sc: pointer to hif_usb_softc structure
339  *
340  * Return: None
341  */
342 void hif_usb_device_deinit(struct hif_usb_softc *sc)
343 {
344 	struct HIF_DEVICE_USB *device = &sc->hif_hdl;
345 
346 	HIF_TRACE("+%s", __func__);
347 
348 	usb_hif_cleanup_pipe_resources(device);
349 
350 	if (device->diag_cmd_buffer)
351 		qdf_mem_free(device->diag_cmd_buffer);
352 
353 	if (device->diag_resp_buffer)
354 		qdf_mem_free(device->diag_resp_buffer);
355 
356 	HIF_TRACE("-%s", __func__);
357 }
358 
359 /**
360  * hif_usb_device_init() - init  HIF_DEVICE_USB, setup pipe resources
361  * @sc: pointer to hif_usb_softc structure
362  *
363  * Return: QDF_STATUS_SUCCESS on success or a QDF error
364  */
365 QDF_STATUS hif_usb_device_init(struct hif_usb_softc *sc)
366 {
367 	int i;
368 	struct HIF_DEVICE_USB *device = &sc->hif_hdl;
369 	struct usb_interface *interface = sc->interface;
370 	struct usb_device *dev = interface_to_usbdev(interface);
371 	QDF_STATUS status = QDF_STATUS_SUCCESS;
372 	struct HIF_USB_PIPE *pipe;
373 
374 	HIF_TRACE("+%s", __func__);
375 
376 	do {
377 
378 		qdf_spinlock_create(&(device->cs_lock));
379 		qdf_spinlock_create(&(device->rx_lock));
380 		qdf_spinlock_create(&(device->tx_lock));
381 		device->udev = dev;
382 		device->interface = interface;
383 
384 		HIF_ERROR("%s device %pK device->udev %pK device->interface %pK",
385 			__func__,
386 			device,
387 			device->udev,
388 			device->interface);
389 
390 		for (i = 0; i < HIF_USB_PIPE_MAX; i++) {
391 			pipe = &device->pipes[i];
392 
393 			HIF_USB_INIT_WORK(pipe);
394 			skb_queue_head_init(&pipe->io_comp_queue);
395 		}
396 
397 		device->diag_cmd_buffer =
398 			qdf_mem_malloc(USB_CTRL_MAX_DIAG_CMD_SIZE);
399 		if (!device->diag_cmd_buffer) {
400 			status = QDF_STATUS_E_NOMEM;
401 			break;
402 		}
403 		device->diag_resp_buffer =
404 			qdf_mem_malloc(USB_CTRL_MAX_DIAG_RESP_SIZE);
405 		if (!device->diag_resp_buffer) {
406 			status = QDF_STATUS_E_NOMEM;
407 			break;
408 		}
409 
410 		status = usb_hif_setup_pipe_resources(device);
411 
412 	} while (false);
413 
414 	if (hif_is_supported_rx_ctrl_pipe(HIF_GET_SOFTC(sc)))
415 		device->rx_ctrl_pipe_supported = 1;
416 
417 	if (status != QDF_STATUS_SUCCESS)
418 		HIF_ERROR("%s: abnormal condition", __func__);
419 
420 	HIF_TRACE("+%s", __func__);
421 	return status;
422 }
423 
424 /**
425  * hif_start() - Enable HIF TX and RX
426  * @scn: pointer to hif_opaque_softc structure
427  *
428  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
429  */
430 QDF_STATUS hif_start(struct hif_opaque_softc *scn)
431 {
432 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(scn);
433 	int i;
434 
435 	HIF_TRACE("+%s", __func__);
436 	usb_hif_prestart_recv_pipes(device);
437 
438 	/* set the TX resource avail threshold for each TX pipe */
439 	for (i = HIF_TX_CTRL_PIPE; i <= HIF_TX_DATA_HP_PIPE; i++) {
440 		device->pipes[i].urb_cnt_thresh =
441 		    device->pipes[i].urb_alloc / 2;
442 	}
443 
444 	HIF_TRACE("-%s", __func__);
445 	return QDF_STATUS_SUCCESS;
446 }
447 
448 /**
449  * hif_usb_stop_device() - Stop/flush all HIF communication
450  * @scn: pointer to hif_opaque_softc structure
451  *
452  * Return: none
453  */
454 void hif_usb_stop_device(struct hif_softc *hif_sc)
455 {
456 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(hif_sc);
457 
458 	HIF_TRACE("+%s", __func__);
459 
460 	usb_hif_flush_all(device);
461 
462 	HIF_TRACE("-%s", __func__);
463 }
464 
465 /**
466  * hif_get_default_pipe() - get default pipes for HIF TX/RX
467  * @scn: pointer to hif_opaque_softc structure
468  * @ul_pipe: pointer to TX pipe
469  * @ul_pipe: pointer to TX pipe
470  *
471  * Return: none
472  */
473 void hif_get_default_pipe(struct hif_opaque_softc *scn, uint8_t *ul_pipe,
474 			  uint8_t *dl_pipe)
475 {
476 	*ul_pipe = HIF_TX_CTRL_PIPE;
477 	*dl_pipe = HIF_RX_CTRL_PIPE;
478 }
479 
480 #if defined(USB_MULTI_IN_TEST) || defined(USB_ISOC_TEST)
481 /**
482  * hif_map_service_to_pipe() - maps ul/dl pipe to service id.
483  * @scn: HIF context
484  * @svc_id: sevice index
485  * @ul_pipe: pointer to uplink pipe id
486  * @dl_pipe: pointer to down-linklink pipe id
487  * @ul_is_polled: if ul is polling based
488  * @ul_is_polled: if dl is polling based
489  *
490  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
491  */
492 int hif_map_service_to_pipe(struct hif_opaque_softc *scn, uint16_t svc_id,
493 			    uint8_t *ul_pipe, uint8_t *dl_pipe,
494 			    int *ul_is_polled, int *dl_is_polled)
495 {
496 	QDF_STATUS status = QDF_STATUS_SUCCESS;
497 
498 	switch (svc_id) {
499 	case HTC_CTRL_RSVD_SVC:
500 	case WMI_CONTROL_SVC:
501 	case HTC_RAW_STREAMS_SVC:
502 		*ul_pipe = HIF_TX_CTRL_PIPE;
503 		*dl_pipe = HIF_RX_DATA_PIPE;
504 		break;
505 	case WMI_DATA_BE_SVC:
506 		*ul_pipe = HIF_TX_DATA_LP_PIPE;
507 		*dl_pipe = HIF_RX_DATA_PIPE;
508 		break;
509 	case WMI_DATA_BK_SVC:
510 		*ul_pipe = HIF_TX_DATA_MP_PIPE;
511 		*dl_pipe = HIF_RX_DATA2_PIPE;
512 		break;
513 	case WMI_DATA_VI_SVC:
514 		*ul_pipe = HIF_TX_DATA_HP_PIPE;
515 		*dl_pipe = HIF_RX_DATA_PIPE;
516 		break;
517 	case WMI_DATA_VO_SVC:
518 		*ul_pipe = HIF_TX_DATA_LP_PIPE;
519 		*dl_pipe = HIF_RX_DATA_PIPE;
520 		break;
521 	default:
522 		status = QDF_STATUS_E_FAILURE;
523 		break;
524 	}
525 
526 	return status;
527 }
528 #else
529 
530 #ifdef QCA_TX_HTT2_SUPPORT
531 #define USB_TX_CHECK_HTT2_SUPPORT 1
532 #else
533 #define USB_TX_CHECK_HTT2_SUPPORT 0
534 #endif
535 
536 /**
537  * hif_map_service_to_pipe() - maps ul/dl pipe to service id.
538  * @scn: HIF context
539  * @svc_id: sevice index
540  * @ul_pipe: pointer to uplink pipe id
541  * @dl_pipe: pointer to down-linklink pipe id
542  * @ul_is_polled: if ul is polling based
543  * @ul_is_polled: if dl is polling based
544  *
545  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
546  */
547 int hif_map_service_to_pipe(struct hif_opaque_softc *scn, uint16_t svc_id,
548 			    uint8_t *ul_pipe, uint8_t *dl_pipe,
549 			    int *ul_is_polled, int *dl_is_polled)
550 {
551 	QDF_STATUS status = QDF_STATUS_SUCCESS;
552 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(scn);
553 
554 	switch (svc_id) {
555 	case HTC_CTRL_RSVD_SVC:
556 	case WMI_CONTROL_SVC:
557 		*ul_pipe = HIF_TX_CTRL_PIPE;
558 		if (device->rx_ctrl_pipe_supported)
559 			*dl_pipe = HIF_RX_CTRL_PIPE;
560 		else
561 			*dl_pipe = HIF_RX_DATA_PIPE;
562 		break;
563 	case WMI_DATA_BE_SVC:
564 	case WMI_DATA_BK_SVC:
565 		*ul_pipe = HIF_TX_DATA_LP_PIPE;
566 		if (hif_usb_disable_rxdata2)
567 			*dl_pipe = HIF_RX_DATA_PIPE;
568 		else
569 			*dl_pipe = HIF_RX_DATA2_PIPE;
570 		break;
571 	case WMI_DATA_VI_SVC:
572 		*ul_pipe = HIF_TX_DATA_MP_PIPE;
573 		if (hif_usb_disable_rxdata2)
574 			*dl_pipe = HIF_RX_DATA_PIPE;
575 		else
576 			*dl_pipe = HIF_RX_DATA2_PIPE;
577 		break;
578 	case WMI_DATA_VO_SVC:
579 		*ul_pipe = HIF_TX_DATA_HP_PIPE;
580 		if (hif_usb_disable_rxdata2)
581 			*dl_pipe = HIF_RX_DATA_PIPE;
582 		else
583 			*dl_pipe = HIF_RX_DATA2_PIPE;
584 		break;
585 	case HTC_RAW_STREAMS_SVC:
586 		*ul_pipe = HIF_TX_CTRL_PIPE;
587 		*dl_pipe = HIF_RX_DATA_PIPE;
588 		break;
589 	case HTT_DATA_MSG_SVC:
590 		*ul_pipe = HIF_TX_DATA_LP_PIPE;
591 		if (hif_usb_disable_rxdata2)
592 			*dl_pipe = HIF_RX_DATA_PIPE;
593 		else
594 			*dl_pipe = HIF_RX_DATA2_PIPE;
595 		break;
596 	case HTT_DATA2_MSG_SVC:
597 		if (USB_TX_CHECK_HTT2_SUPPORT) {
598 			*ul_pipe = HIF_TX_DATA_HP_PIPE;
599 			if (hif_usb_disable_rxdata2)
600 				*dl_pipe = HIF_RX_DATA_PIPE;
601 			else
602 				*dl_pipe = HIF_RX_DATA2_PIPE;
603 			}
604 		break;
605 	default:
606 		status = QDF_STATUS_E_FAILURE;
607 		break;
608 	}
609 
610 	return status;
611 }
612 #endif
613 
614 /**
615  * hif_ctrl_msg_exchange() - send usb ctrl message and receive response
616  * @macp: pointer to HIF_DEVICE_USB
617  * @send_req_val: USB send message request value
618  * @send_msg: pointer to data to send
619  * @len: length in bytes of the data to send
620  * @response_req_val: USB response message request value
621  * @response_msg: pointer to response msg
622  * @response_len: length of the response message
623  *
624  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
625  */
626 static QDF_STATUS hif_ctrl_msg_exchange(struct HIF_DEVICE_USB *macp,
627 					uint8_t send_req_val,
628 					uint8_t *send_msg,
629 					uint32_t len,
630 					uint8_t response_req_val,
631 					uint8_t *response_msg,
632 					uint32_t *response_len)
633 {
634 	QDF_STATUS status;
635 
636 	do {
637 
638 		/* send command */
639 		status = usb_hif_submit_ctrl_out(macp, send_req_val, 0, 0,
640 						 send_msg, len);
641 
642 		if (!QDF_IS_STATUS_SUCCESS(status))
643 			break;
644 
645 		if (!response_msg) {
646 			/* no expected response */
647 			break;
648 		}
649 
650 		/* get response */
651 		status = usb_hif_submit_ctrl_in(macp, response_req_val, 0, 0,
652 						response_msg, *response_len);
653 
654 		if (!QDF_IS_STATUS_SUCCESS(status))
655 			break;
656 
657 	} while (false);
658 
659 	return status;
660 }
661 
662 #ifdef WLAN_FEATURE_BMI
663 /**
664  * hif_exchange_bmi_msg() - send/recev ctrl message of type BMI_CMD/BMI_RESP
665  * @scn: pointer to hif_opaque_softc
666  * @bmi_request: pointer to data to send
667  * @request_length: length in bytes of the data to send
668  * @bmi_response: pointer to response msg
669  * @bmi_response_length: length of the response message
670  * @timeout_ms: timeout to wait for response (ignored in current implementation)
671  *
672  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
673  */
674 
675 QDF_STATUS hif_exchange_bmi_msg(struct hif_opaque_softc *scn,
676 				qdf_dma_addr_t cmd, qdf_dma_addr_t rsp,
677 				uint8_t *bmi_request,
678 				uint32_t request_length,
679 				uint8_t *bmi_response,
680 				uint32_t *bmi_response_lengthp,
681 				uint32_t timeout_ms)
682 {
683 	struct HIF_DEVICE_USB *macp = HIF_GET_USB_DEVICE(scn);
684 
685 	return hif_ctrl_msg_exchange(macp,
686 				USB_CONTROL_REQ_SEND_BMI_CMD,
687 				bmi_request,
688 				request_length,
689 				USB_CONTROL_REQ_RECV_BMI_RESP,
690 				bmi_response, bmi_response_lengthp);
691 }
692 
693 void hif_register_bmi_callbacks(struct hif_opaque_softc *hif_ctx)
694 {
695 }
696 #endif /* WLAN_FEATURE_BMI */
697 
698 /**
699  * hif_diag_read_access() - Read data from target memory or register
700  * @scn: pointer to hif_opaque_softc
701  * @address: register address to read from
702  * @data: pointer to buffer to store the value read from the register
703  *
704  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
705  */
706 QDF_STATUS hif_diag_read_access(struct hif_opaque_softc *scn, uint32_t address,
707 					uint32_t *data)
708 {
709 	struct HIF_DEVICE_USB *macp = HIF_GET_USB_DEVICE(scn);
710 	QDF_STATUS status;
711 	USB_CTRL_DIAG_CMD_READ *cmd;
712 	uint32_t respLength;
713 
714 	cmd = (USB_CTRL_DIAG_CMD_READ *) macp->diag_cmd_buffer;
715 
716 	qdf_mem_zero(cmd, sizeof(*cmd));
717 	cmd->Cmd = USB_CTRL_DIAG_CC_READ;
718 	cmd->Address = address;
719 	respLength = sizeof(USB_CTRL_DIAG_RESP_READ);
720 
721 	status = hif_ctrl_msg_exchange(macp,
722 				USB_CONTROL_REQ_DIAG_CMD,
723 				(uint8_t *) cmd,
724 				sizeof(*cmd),
725 				USB_CONTROL_REQ_DIAG_RESP,
726 				macp->diag_resp_buffer, &respLength);
727 
728 	if (QDF_IS_STATUS_SUCCESS(status)) {
729 		USB_CTRL_DIAG_RESP_READ *pResp =
730 			(USB_CTRL_DIAG_RESP_READ *) macp->diag_resp_buffer;
731 		*data = pResp->ReadValue;
732 		status = QDF_STATUS_SUCCESS;
733 	} else {
734 		status = QDF_STATUS_E_FAILURE;
735 	}
736 
737 	return status;
738 }
739 
740 /**
741  * hif_diag_write_access() - write data to target memory or register
742  * @scn: pointer to hif_opaque_softc
743  * @address: register address to write to
744  * @data: value to be written to the address
745  *
746  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
747  */
748 QDF_STATUS hif_diag_write_access(struct hif_opaque_softc *scn,
749 					uint32_t address,
750 					uint32_t data)
751 {
752 	struct HIF_DEVICE_USB *macp = HIF_GET_USB_DEVICE(scn);
753 	USB_CTRL_DIAG_CMD_WRITE *cmd;
754 
755 	cmd = (USB_CTRL_DIAG_CMD_WRITE *) macp->diag_cmd_buffer;
756 
757 	qdf_mem_zero(cmd, sizeof(*cmd));
758 	cmd->Cmd = USB_CTRL_DIAG_CC_WRITE;
759 	cmd->Address = address;
760 	cmd->Value = data;
761 
762 	return hif_ctrl_msg_exchange(macp,
763 				USB_CONTROL_REQ_DIAG_CMD,
764 				(uint8_t *) cmd,
765 				sizeof(*cmd), 0, NULL, 0);
766 }
767 
768 /**
769  * hif_dump_info() - dump info about all HIF pipes and endpoints
770  * @scn: pointer to hif_opaque_softc
771  *
772  * Return: none
773  */
774 void hif_dump_info(struct hif_opaque_softc *scn)
775 {
776 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(scn);
777 	struct HIF_USB_PIPE *pipe = NULL;
778 	struct usb_host_interface *iface_desc = NULL;
779 	struct usb_endpoint_descriptor *ep_desc;
780 	uint8_t i = 0;
781 
782 	for (i = 0; i < HIF_USB_PIPE_MAX; i++) {
783 		pipe = &device->pipes[i];
784 		HIF_ERROR("PipeIndex : %d URB Cnt : %d PipeHandle : %x",
785 			i, pipe->urb_cnt,
786 			pipe->usb_pipe_handle);
787 		if (usb_pipeisoc(pipe->usb_pipe_handle))
788 			HIF_INFO("Pipe Type ISOC");
789 		else if (usb_pipebulk(pipe->usb_pipe_handle))
790 			HIF_INFO("Pipe Type BULK");
791 		else if (usb_pipeint(pipe->usb_pipe_handle))
792 			HIF_INFO("Pipe Type INT");
793 		else if (usb_pipecontrol(pipe->usb_pipe_handle))
794 			HIF_INFO("Pipe Type control");
795 	}
796 
797 	for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
798 		ep_desc = &iface_desc->endpoint[i].desc;
799 		if (ep_desc) {
800 			HIF_INFO(
801 				"ep_desc : %pK Index : %d: DescType : %d Addr : %d Maxp : %d Atrrib : %d",
802 				ep_desc, i, ep_desc->bDescriptorType,
803 				ep_desc->bEndpointAddress,
804 				ep_desc->wMaxPacketSize,
805 				ep_desc->bmAttributes);
806 			if ((ep_desc) && (usb_endpoint_type(ep_desc) ==
807 						USB_ENDPOINT_XFER_ISOC)) {
808 				HIF_INFO("ISOC EP Detected");
809 			}
810 		}
811 	}
812 
813 }
814 
815 /**
816  * hif_flush_surprise_remove() - Cleanup residual buffers for device shutdown
817  * @scn: HIF context
818  *
819  * Not applicable to USB bus
820  *
821  * Return: none
822  */
823 void hif_flush_surprise_remove(struct hif_opaque_softc *scn)
824 {
825 /* TO DO... */
826 }
827 
828 /**
829  * hif_diag_read_mem() -read nbytes of data from target memory or register
830  * @scn: pointer to hif_opaque_softc
831  * @address: register address to read from
832  * @data: buffer to store the value read
833  * @nbytes: number of bytes to be read from 'address'
834  *
835  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
836  */
837 QDF_STATUS hif_diag_read_mem(struct hif_opaque_softc *scn,
838 					 uint32_t address, uint8_t *data,
839 					 int nbytes)
840 {
841 	QDF_STATUS status = QDF_STATUS_SUCCESS;
842 
843 	HIF_TRACE("+%s", __func__);
844 
845 	if ((address & 0x3) || ((uintptr_t)data & 0x3))
846 		return QDF_STATUS_E_IO;
847 
848 	while ((nbytes >= 4) &&
849 		QDF_IS_STATUS_SUCCESS(status =
850 					hif_diag_read_access(scn,
851 							address,
852 							(uint32_t *)data))) {
853 
854 		nbytes -= sizeof(uint32_t);
855 		address += sizeof(uint32_t);
856 		data += sizeof(uint32_t);
857 
858 	}
859 	HIF_TRACE("-%s", __func__);
860 	return status;
861 }
862 qdf_export_symbol(hif_diag_read_mem);
863 
864 /**
865  * hif_diag_write_mem() -write  nbytes of data to target memory or register
866  * @scn: pointer to hif_opaque_softc
867  * @address: register address to write to
868  * @data: buffer containing data to be written
869  * @nbytes: number of bytes to be written
870  *
871  * Return: QDF_STATUS_SUCCESS if success else an appropriate QDF_STATUS error
872  */
873 QDF_STATUS hif_diag_write_mem(struct hif_opaque_softc *scn,
874 					   uint32_t address,
875 					   uint8_t *data, int nbytes)
876 {
877 	QDF_STATUS status = QDF_STATUS_SUCCESS;
878 
879 	HIF_TRACE("+%s", __func__);
880 	if ((address & 0x3) || ((uintptr_t)data & 0x3))
881 		return QDF_STATUS_E_IO;
882 
883 	while (nbytes >= 4 &&
884 		QDF_IS_STATUS_SUCCESS(status =
885 					hif_diag_write_access(scn,
886 						address,
887 						*((uint32_t *)data)))) {
888 
889 		nbytes -= sizeof(uint32_t);
890 		address += sizeof(uint32_t);
891 		data += sizeof(uint32_t);
892 
893 	}
894 	HIF_TRACE("-%s", __func__);
895 	return status;
896 }
897 
898 void hif_send_complete_check(struct hif_opaque_softc *scn,
899 						uint8_t PipeID, int force)
900 {
901 	/* NO-OP*/
902 }
903 
904 /* diagnostic command defnitions */
905 #define USB_CTRL_DIAG_CC_READ       0
906 #define USB_CTRL_DIAG_CC_WRITE      1
907 #define USB_CTRL_DIAG_CC_WARM_RESET 2
908 
909 void hif_suspend_wow(struct hif_opaque_softc *scn)
910 {
911 	HIF_INFO("HIFsuspendwow - TODO");
912 }
913 
914 /**
915  * hif_usb_set_bundle_mode() - enable bundling and set default rx bundle cnt
916  * @scn: pointer to hif_opaque_softc structure
917  * @enabled: flag to enable/disable bundling
918  * @rx_bundle_cnt: bundle count to be used for RX
919  *
920  * Return: none
921  */
922 void hif_usb_set_bundle_mode(struct hif_softc *scn,
923 					bool enabled, int rx_bundle_cnt)
924 {
925 	struct HIF_DEVICE_USB *device = HIF_GET_USB_DEVICE(scn);
926 
927 	device->is_bundle_enabled = enabled;
928 	device->rx_bundle_cnt = rx_bundle_cnt;
929 	if (device->is_bundle_enabled && (device->rx_bundle_cnt == 0))
930 		device->rx_bundle_cnt = 1;
931 
932 	device->rx_bundle_buf_len = device->rx_bundle_cnt *
933 					HIF_USB_RX_BUNDLE_ONE_PKT_SIZE;
934 
935 	HIF_DBG("athusb bundle %s cnt %d", enabled ? "enabled" : "disabled",
936 			rx_bundle_cnt);
937 }
938 
939 /**
940  * hif_is_supported_rx_ctrl_pipe() - return true if device supports exclusive
941  * control pipe in the RX direction.
942  * @scn: hif context
943  *
944  * Return: true if device supports RX control pipe.
945  */
946 bool hif_is_supported_rx_ctrl_pipe(struct hif_softc *scn)
947 {
948 	struct hif_opaque_softc *hif_hdl = GET_HIF_OPAQUE_HDL(scn);
949 	struct hif_target_info *tgt_info = hif_get_target_info_handle(hif_hdl);
950 
951 	switch (tgt_info->target_type) {
952 	case TARGET_TYPE_QCN7605:
953 		return true;
954 	default:
955 		return false;
956 	}
957 }
958