xref: /openbmc/linux/drivers/net/wireless/ath/ath10k/usb.c (revision 3c545a25939c27f85430588110c657ede5fdfe0a)
1 /*
2  * Copyright (c) 2007-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
4  * Copyright (c) 2016-2017 Erik Stromdahl <erik.stromdahl@gmail.com>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <linux/module.h>
20 #include <linux/usb.h>
21 
22 #include "debug.h"
23 #include "core.h"
24 #include "bmi.h"
25 #include "hif.h"
26 #include "htc.h"
27 #include "usb.h"
28 
29 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
30 					   struct ath10k_usb_pipe *recv_pipe);
31 
32 /* inlined helper functions */
33 
34 static inline enum ath10k_htc_ep_id
35 eid_from_htc_hdr(struct ath10k_htc_hdr *htc_hdr)
36 {
37 	return (enum ath10k_htc_ep_id)htc_hdr->eid;
38 }
39 
40 static inline bool is_trailer_only_msg(struct ath10k_htc_hdr *htc_hdr)
41 {
42 	return __le16_to_cpu(htc_hdr->len) == htc_hdr->trailer_len;
43 }
44 
45 /* pipe/urb operations */
46 static struct ath10k_urb_context *
47 ath10k_usb_alloc_urb_from_pipe(struct ath10k_usb_pipe *pipe)
48 {
49 	struct ath10k_urb_context *urb_context = NULL;
50 	unsigned long flags;
51 
52 	spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
53 	if (!list_empty(&pipe->urb_list_head)) {
54 		urb_context = list_first_entry(&pipe->urb_list_head,
55 					       struct ath10k_urb_context, link);
56 		list_del(&urb_context->link);
57 		pipe->urb_cnt--;
58 	}
59 	spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
60 
61 	return urb_context;
62 }
63 
64 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
65 					struct ath10k_urb_context *urb_context)
66 {
67 	unsigned long flags;
68 
69 	spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
70 
71 	pipe->urb_cnt++;
72 	list_add(&urb_context->link, &pipe->urb_list_head);
73 
74 	spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
75 }
76 
77 static void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context)
78 {
79 	dev_kfree_skb(urb_context->skb);
80 	urb_context->skb = NULL;
81 
82 	ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
83 }
84 
85 static void ath10k_usb_free_pipe_resources(struct ath10k *ar,
86 					   struct ath10k_usb_pipe *pipe)
87 {
88 	struct ath10k_urb_context *urb_context;
89 
90 	if (!pipe->ar_usb) {
91 		/* nothing allocated for this pipe */
92 		return;
93 	}
94 
95 	ath10k_dbg(ar, ATH10K_DBG_USB,
96 		   "usb free resources lpipe %d hpipe 0x%x urbs %d avail %d\n",
97 		   pipe->logical_pipe_num, pipe->usb_pipe_handle,
98 		   pipe->urb_alloc, pipe->urb_cnt);
99 
100 	if (pipe->urb_alloc != pipe->urb_cnt) {
101 		ath10k_dbg(ar, ATH10K_DBG_USB,
102 			   "usb urb leak lpipe %d hpipe 0x%x urbs %d avail %d\n",
103 			   pipe->logical_pipe_num, pipe->usb_pipe_handle,
104 			   pipe->urb_alloc, pipe->urb_cnt);
105 	}
106 
107 	for (;;) {
108 		urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
109 
110 		if (!urb_context)
111 			break;
112 
113 		kfree(urb_context);
114 	}
115 }
116 
117 static void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar)
118 {
119 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
120 	int i;
121 
122 	for (i = 0; i < ATH10K_USB_PIPE_MAX; i++)
123 		ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]);
124 }
125 
126 /* hif usb rx/tx completion functions */
127 
128 static void ath10k_usb_recv_complete(struct urb *urb)
129 {
130 	struct ath10k_urb_context *urb_context = urb->context;
131 	struct ath10k_usb_pipe *pipe = urb_context->pipe;
132 	struct ath10k *ar = pipe->ar_usb->ar;
133 	struct sk_buff *skb;
134 	int status = 0;
135 
136 	ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
137 		   "usb recv pipe %d stat %d len %d urb 0x%pK\n",
138 		   pipe->logical_pipe_num, urb->status, urb->actual_length,
139 		   urb);
140 
141 	if (urb->status != 0) {
142 		status = -EIO;
143 		switch (urb->status) {
144 		case -ECONNRESET:
145 		case -ENOENT:
146 		case -ESHUTDOWN:
147 			/* no need to spew these errors when device
148 			 * removed or urb killed due to driver shutdown
149 			 */
150 			status = -ECANCELED;
151 			break;
152 		default:
153 			ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
154 				   "usb recv pipe %d ep 0x%2.2x failed: %d\n",
155 				   pipe->logical_pipe_num,
156 				   pipe->ep_address, urb->status);
157 			break;
158 		}
159 		goto cleanup_recv_urb;
160 	}
161 
162 	if (urb->actual_length == 0)
163 		goto cleanup_recv_urb;
164 
165 	skb = urb_context->skb;
166 
167 	/* we are going to pass it up */
168 	urb_context->skb = NULL;
169 	skb_put(skb, urb->actual_length);
170 
171 	/* note: queue implements a lock */
172 	skb_queue_tail(&pipe->io_comp_queue, skb);
173 	schedule_work(&pipe->io_complete_work);
174 
175 cleanup_recv_urb:
176 	ath10k_usb_cleanup_recv_urb(urb_context);
177 
178 	if (status == 0 &&
179 	    pipe->urb_cnt >= pipe->urb_cnt_thresh) {
180 		/* our free urbs are piling up, post more transfers */
181 		ath10k_usb_post_recv_transfers(ar, pipe);
182 	}
183 }
184 
185 static void ath10k_usb_transmit_complete(struct urb *urb)
186 {
187 	struct ath10k_urb_context *urb_context = urb->context;
188 	struct ath10k_usb_pipe *pipe = urb_context->pipe;
189 	struct ath10k *ar = pipe->ar_usb->ar;
190 	struct sk_buff *skb;
191 
192 	if (urb->status != 0) {
193 		ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
194 			   "pipe: %d, failed:%d\n",
195 			   pipe->logical_pipe_num, urb->status);
196 	}
197 
198 	skb = urb_context->skb;
199 	urb_context->skb = NULL;
200 	ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
201 
202 	/* note: queue implements a lock */
203 	skb_queue_tail(&pipe->io_comp_queue, skb);
204 	schedule_work(&pipe->io_complete_work);
205 }
206 
207 /* pipe operations */
208 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
209 					   struct ath10k_usb_pipe *recv_pipe)
210 {
211 	struct ath10k_urb_context *urb_context;
212 	struct urb *urb;
213 	int usb_status;
214 
215 	for (;;) {
216 		urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe);
217 		if (!urb_context)
218 			break;
219 
220 		urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE);
221 		if (!urb_context->skb)
222 			goto err;
223 
224 		urb = usb_alloc_urb(0, GFP_ATOMIC);
225 		if (!urb)
226 			goto err;
227 
228 		usb_fill_bulk_urb(urb,
229 				  recv_pipe->ar_usb->udev,
230 				  recv_pipe->usb_pipe_handle,
231 				  urb_context->skb->data,
232 				  ATH10K_USB_RX_BUFFER_SIZE,
233 				  ath10k_usb_recv_complete, urb_context);
234 
235 		ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
236 			   "usb bulk recv submit %d 0x%x ep 0x%2.2x len %d buf 0x%pK\n",
237 			   recv_pipe->logical_pipe_num,
238 			   recv_pipe->usb_pipe_handle, recv_pipe->ep_address,
239 			   ATH10K_USB_RX_BUFFER_SIZE, urb_context->skb);
240 
241 		usb_anchor_urb(urb, &recv_pipe->urb_submitted);
242 		usb_status = usb_submit_urb(urb, GFP_ATOMIC);
243 
244 		if (usb_status) {
245 			ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
246 				   "usb bulk recv failed: %d\n",
247 				   usb_status);
248 			usb_unanchor_urb(urb);
249 			usb_free_urb(urb);
250 			goto err;
251 		}
252 		usb_free_urb(urb);
253 	}
254 
255 	return;
256 
257 err:
258 	ath10k_usb_cleanup_recv_urb(urb_context);
259 }
260 
261 static void ath10k_usb_flush_all(struct ath10k *ar)
262 {
263 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
264 	int i;
265 
266 	for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
267 		if (ar_usb->pipes[i].ar_usb) {
268 			usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted);
269 			cancel_work_sync(&ar_usb->pipes[i].io_complete_work);
270 		}
271 	}
272 }
273 
274 static void ath10k_usb_start_recv_pipes(struct ath10k *ar)
275 {
276 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
277 
278 	ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
279 
280 	ath10k_usb_post_recv_transfers(ar,
281 				       &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
282 }
283 
284 static void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb)
285 {
286 	struct ath10k_htc_hdr *htc_hdr;
287 	struct ath10k_htc_ep *ep;
288 
289 	htc_hdr = (struct ath10k_htc_hdr *)skb->data;
290 	ep = &ar->htc.endpoint[htc_hdr->eid];
291 	ath10k_htc_notify_tx_completion(ep, skb);
292 	/* The TX complete handler now owns the skb... */
293 }
294 
295 static void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb)
296 {
297 	struct ath10k_htc *htc = &ar->htc;
298 	struct ath10k_htc_hdr *htc_hdr;
299 	enum ath10k_htc_ep_id eid;
300 	struct ath10k_htc_ep *ep;
301 	u16 payload_len;
302 	u8 *trailer;
303 	int ret;
304 
305 	htc_hdr = (struct ath10k_htc_hdr *)skb->data;
306 	eid = eid_from_htc_hdr(htc_hdr);
307 	ep = &ar->htc.endpoint[eid];
308 
309 	if (ep->service_id == 0) {
310 		ath10k_warn(ar, "ep %d is not connected\n", eid);
311 		goto out_free_skb;
312 	}
313 
314 	payload_len = le16_to_cpu(htc_hdr->len);
315 	if (!payload_len) {
316 		ath10k_warn(ar, "zero length frame received, firmware crashed?\n");
317 		goto out_free_skb;
318 	}
319 
320 	if (payload_len < htc_hdr->trailer_len) {
321 		ath10k_warn(ar, "malformed frame received, firmware crashed?\n");
322 		goto out_free_skb;
323 	}
324 
325 	if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) {
326 		trailer = skb->data + sizeof(*htc_hdr) + payload_len -
327 			  htc_hdr->trailer_len;
328 
329 		ret = ath10k_htc_process_trailer(htc,
330 						 trailer,
331 						 htc_hdr->trailer_len,
332 						 eid,
333 						 NULL,
334 						 NULL);
335 		if (ret)
336 			goto out_free_skb;
337 
338 		if (is_trailer_only_msg(htc_hdr))
339 			goto out_free_skb;
340 
341 		/* strip off the trailer from the skb since it should not
342 		 * be passed on to upper layers
343 		 */
344 		skb_trim(skb, skb->len - htc_hdr->trailer_len);
345 	}
346 
347 	skb_pull(skb, sizeof(*htc_hdr));
348 	ep->ep_ops.ep_rx_complete(ar, skb);
349 	/* The RX complete handler now owns the skb... */
350 
351 	return;
352 
353 out_free_skb:
354 	dev_kfree_skb(skb);
355 }
356 
357 static void ath10k_usb_io_comp_work(struct work_struct *work)
358 {
359 	struct ath10k_usb_pipe *pipe = container_of(work,
360 						    struct ath10k_usb_pipe,
361 						    io_complete_work);
362 	struct ath10k *ar = pipe->ar_usb->ar;
363 	struct sk_buff *skb;
364 
365 	while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
366 		if (pipe->flags & ATH10K_USB_PIPE_FLAG_TX)
367 			ath10k_usb_tx_complete(ar, skb);
368 		else
369 			ath10k_usb_rx_complete(ar, skb);
370 	}
371 }
372 
373 #define ATH10K_USB_MAX_DIAG_CMD (sizeof(struct ath10k_usb_ctrl_diag_cmd_write))
374 #define ATH10K_USB_MAX_DIAG_RESP (sizeof(struct ath10k_usb_ctrl_diag_resp_read))
375 
376 static void ath10k_usb_destroy(struct ath10k *ar)
377 {
378 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
379 
380 	ath10k_usb_flush_all(ar);
381 	ath10k_usb_cleanup_pipe_resources(ar);
382 	usb_set_intfdata(ar_usb->interface, NULL);
383 
384 	kfree(ar_usb->diag_cmd_buffer);
385 	kfree(ar_usb->diag_resp_buffer);
386 }
387 
388 static int ath10k_usb_hif_start(struct ath10k *ar)
389 {
390 	int i;
391 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
392 
393 	ath10k_usb_start_recv_pipes(ar);
394 
395 	/* set the TX resource avail threshold for each TX pipe */
396 	for (i = ATH10K_USB_PIPE_TX_CTRL;
397 	     i <= ATH10K_USB_PIPE_TX_DATA_HP; i++) {
398 		ar_usb->pipes[i].urb_cnt_thresh =
399 		    ar_usb->pipes[i].urb_alloc / 2;
400 	}
401 
402 	return 0;
403 }
404 
405 static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
406 				struct ath10k_hif_sg_item *items, int n_items)
407 {
408 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
409 	struct ath10k_usb_pipe *pipe = &ar_usb->pipes[pipe_id];
410 	struct ath10k_urb_context *urb_context;
411 	struct sk_buff *skb;
412 	struct urb *urb;
413 	int ret, i;
414 
415 	for (i = 0; i < n_items; i++) {
416 		urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
417 		if (!urb_context) {
418 			ret = -ENOMEM;
419 			goto err;
420 		}
421 
422 		skb = items[i].transfer_context;
423 		urb_context->skb = skb;
424 
425 		urb = usb_alloc_urb(0, GFP_ATOMIC);
426 		if (!urb) {
427 			ret = -ENOMEM;
428 			goto err_free_urb_to_pipe;
429 		}
430 
431 		usb_fill_bulk_urb(urb,
432 				  ar_usb->udev,
433 				  pipe->usb_pipe_handle,
434 				  skb->data,
435 				  skb->len,
436 				  ath10k_usb_transmit_complete, urb_context);
437 
438 		if (!(skb->len % pipe->max_packet_size)) {
439 			/* hit a max packet boundary on this pipe */
440 			urb->transfer_flags |= URB_ZERO_PACKET;
441 		}
442 
443 		usb_anchor_urb(urb, &pipe->urb_submitted);
444 		ret = usb_submit_urb(urb, GFP_ATOMIC);
445 		if (ret) {
446 			ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
447 				   "usb bulk transmit failed: %d\n", ret);
448 			usb_unanchor_urb(urb);
449 			ret = -EINVAL;
450 			goto err_free_urb_to_pipe;
451 		}
452 
453 		usb_free_urb(urb);
454 	}
455 
456 	return 0;
457 
458 err_free_urb_to_pipe:
459 	ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
460 err:
461 	return ret;
462 }
463 
464 static void ath10k_usb_hif_stop(struct ath10k *ar)
465 {
466 	ath10k_usb_flush_all(ar);
467 }
468 
469 static u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id)
470 {
471 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
472 
473 	return ar_usb->pipes[pipe_id].urb_cnt;
474 }
475 
476 static int ath10k_usb_submit_ctrl_out(struct ath10k *ar,
477 				      u8 req, u16 value, u16 index, void *data,
478 				      u32 size)
479 {
480 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
481 	u8 *buf = NULL;
482 	int ret;
483 
484 	if (size > 0) {
485 		buf = kmemdup(data, size, GFP_KERNEL);
486 		if (!buf)
487 			return -ENOMEM;
488 	}
489 
490 	/* note: if successful returns number of bytes transferred */
491 	ret = usb_control_msg(ar_usb->udev,
492 			      usb_sndctrlpipe(ar_usb->udev, 0),
493 			      req,
494 			      USB_DIR_OUT | USB_TYPE_VENDOR |
495 			      USB_RECIP_DEVICE, value, index, buf,
496 			      size, 1000);
497 
498 	if (ret < 0) {
499 		ath10k_warn(ar, "Failed to submit usb control message: %d\n",
500 			    ret);
501 		kfree(buf);
502 		return ret;
503 	}
504 
505 	kfree(buf);
506 
507 	return 0;
508 }
509 
510 static int ath10k_usb_submit_ctrl_in(struct ath10k *ar,
511 				     u8 req, u16 value, u16 index, void *data,
512 				     u32 size)
513 {
514 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
515 	u8 *buf = NULL;
516 	int ret;
517 
518 	if (size > 0) {
519 		buf = kmalloc(size, GFP_KERNEL);
520 		if (!buf)
521 			return -ENOMEM;
522 	}
523 
524 	/* note: if successful returns number of bytes transferred */
525 	ret = usb_control_msg(ar_usb->udev,
526 			      usb_rcvctrlpipe(ar_usb->udev, 0),
527 			      req,
528 			      USB_DIR_IN | USB_TYPE_VENDOR |
529 			      USB_RECIP_DEVICE, value, index, buf,
530 			      size, 2 * HZ);
531 
532 	if (ret < 0) {
533 		ath10k_warn(ar, "Failed to read usb control message: %d\n",
534 			    ret);
535 		kfree(buf);
536 		return ret;
537 	}
538 
539 	memcpy((u8 *)data, buf, size);
540 
541 	kfree(buf);
542 
543 	return 0;
544 }
545 
546 static int ath10k_usb_ctrl_msg_exchange(struct ath10k *ar,
547 					u8 req_val, u8 *req_buf, u32 req_len,
548 					u8 resp_val, u8 *resp_buf,
549 					u32 *resp_len)
550 {
551 	int ret;
552 
553 	/* send command */
554 	ret = ath10k_usb_submit_ctrl_out(ar, req_val, 0, 0,
555 					 req_buf, req_len);
556 	if (ret)
557 		goto err;
558 
559 	/* get response */
560 	if (resp_buf) {
561 		ret = ath10k_usb_submit_ctrl_in(ar, resp_val, 0, 0,
562 						resp_buf, *resp_len);
563 		if (ret)
564 			goto err;
565 	}
566 
567 	return 0;
568 err:
569 	return ret;
570 }
571 
572 static int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
573 				    size_t buf_len)
574 {
575 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
576 	struct ath10k_usb_ctrl_diag_cmd_read *cmd;
577 	u32 resp_len;
578 	int ret;
579 
580 	if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read))
581 		return -EINVAL;
582 
583 	cmd = (struct ath10k_usb_ctrl_diag_cmd_read *)ar_usb->diag_cmd_buffer;
584 	memset(cmd, 0, sizeof(*cmd));
585 	cmd->cmd = ATH10K_USB_CTRL_DIAG_CC_READ;
586 	cmd->address = cpu_to_le32(address);
587 	resp_len = sizeof(struct ath10k_usb_ctrl_diag_resp_read);
588 
589 	ret = ath10k_usb_ctrl_msg_exchange(ar,
590 					   ATH10K_USB_CONTROL_REQ_DIAG_CMD,
591 					   (u8 *)cmd,
592 					   sizeof(*cmd),
593 					   ATH10K_USB_CONTROL_REQ_DIAG_RESP,
594 					   ar_usb->diag_resp_buffer, &resp_len);
595 	if (ret)
596 		return ret;
597 
598 	if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read))
599 		return -EMSGSIZE;
600 
601 	memcpy(buf, ar_usb->diag_resp_buffer,
602 	       sizeof(struct ath10k_usb_ctrl_diag_resp_read));
603 
604 	return 0;
605 }
606 
607 static int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address,
608 				     const void *data, int nbytes)
609 {
610 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
611 	struct ath10k_usb_ctrl_diag_cmd_write *cmd;
612 	int ret;
613 
614 	if (nbytes != sizeof(cmd->value))
615 		return -EINVAL;
616 
617 	cmd = (struct ath10k_usb_ctrl_diag_cmd_write *)ar_usb->diag_cmd_buffer;
618 	memset(cmd, 0, sizeof(*cmd));
619 	cmd->cmd = cpu_to_le32(ATH10K_USB_CTRL_DIAG_CC_WRITE);
620 	cmd->address = cpu_to_le32(address);
621 	memcpy(&cmd->value, data, nbytes);
622 
623 	ret = ath10k_usb_ctrl_msg_exchange(ar,
624 					   ATH10K_USB_CONTROL_REQ_DIAG_CMD,
625 					   (u8 *)cmd,
626 					   sizeof(*cmd),
627 					   0, NULL, NULL);
628 	if (ret)
629 		return ret;
630 
631 	return 0;
632 }
633 
634 static int ath10k_usb_bmi_exchange_msg(struct ath10k *ar,
635 				       void *req, u32 req_len,
636 				       void *resp, u32 *resp_len)
637 {
638 	int ret;
639 
640 	if (req) {
641 		ret = ath10k_usb_submit_ctrl_out(ar,
642 						 ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD,
643 						 0, 0, req, req_len);
644 		if (ret) {
645 			ath10k_warn(ar,
646 				    "unable to send the bmi data to the device: %d\n",
647 				    ret);
648 			return ret;
649 		}
650 	}
651 
652 	if (resp) {
653 		ret = ath10k_usb_submit_ctrl_in(ar,
654 						ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP,
655 						0, 0, resp, *resp_len);
656 		if (ret) {
657 			ath10k_warn(ar,
658 				    "Unable to read the bmi data from the device: %d\n",
659 				    ret);
660 			return ret;
661 		}
662 	}
663 
664 	return 0;
665 }
666 
667 static void ath10k_usb_hif_get_default_pipe(struct ath10k *ar,
668 					    u8 *ul_pipe, u8 *dl_pipe)
669 {
670 	*ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
671 	*dl_pipe = ATH10K_USB_PIPE_RX_CTRL;
672 }
673 
674 static int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id,
675 					      u8 *ul_pipe, u8 *dl_pipe)
676 {
677 	switch (svc_id) {
678 	case ATH10K_HTC_SVC_ID_RSVD_CTRL:
679 	case ATH10K_HTC_SVC_ID_WMI_CONTROL:
680 		*ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
681 		/* due to large control packets, shift to data pipe */
682 		*dl_pipe = ATH10K_USB_PIPE_RX_DATA;
683 		break;
684 	case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
685 		*ul_pipe = ATH10K_USB_PIPE_TX_DATA_LP;
686 		/* Disable rxdata2 directly, it will be enabled
687 		 * if FW enable rxdata2
688 		 */
689 		*dl_pipe = ATH10K_USB_PIPE_RX_DATA;
690 		break;
691 	default:
692 		return -EPERM;
693 	}
694 
695 	return 0;
696 }
697 
698 /* This op is currently only used by htc_wait_target if the HTC ready
699  * message times out. It is not applicable for USB since there is nothing
700  * we can do if the HTC ready message does not arrive in time.
701  * TODO: Make this op non mandatory by introducing a NULL check in the
702  * hif op wrapper.
703  */
704 static void ath10k_usb_hif_send_complete_check(struct ath10k *ar,
705 					       u8 pipe, int force)
706 {
707 }
708 
709 static int ath10k_usb_hif_power_up(struct ath10k *ar,
710 				   enum ath10k_firmware_mode fw_mode)
711 {
712 	return 0;
713 }
714 
715 static void ath10k_usb_hif_power_down(struct ath10k *ar)
716 {
717 	ath10k_usb_flush_all(ar);
718 }
719 
720 #ifdef CONFIG_PM
721 
722 static int ath10k_usb_hif_suspend(struct ath10k *ar)
723 {
724 	return -EOPNOTSUPP;
725 }
726 
727 static int ath10k_usb_hif_resume(struct ath10k *ar)
728 {
729 	return -EOPNOTSUPP;
730 }
731 #endif
732 
733 static const struct ath10k_hif_ops ath10k_usb_hif_ops = {
734 	.tx_sg			= ath10k_usb_hif_tx_sg,
735 	.diag_read		= ath10k_usb_hif_diag_read,
736 	.diag_write		= ath10k_usb_hif_diag_write,
737 	.exchange_bmi_msg	= ath10k_usb_bmi_exchange_msg,
738 	.start			= ath10k_usb_hif_start,
739 	.stop			= ath10k_usb_hif_stop,
740 	.map_service_to_pipe	= ath10k_usb_hif_map_service_to_pipe,
741 	.get_default_pipe	= ath10k_usb_hif_get_default_pipe,
742 	.send_complete_check	= ath10k_usb_hif_send_complete_check,
743 	.get_free_queue_number	= ath10k_usb_hif_get_free_queue_number,
744 	.power_up		= ath10k_usb_hif_power_up,
745 	.power_down		= ath10k_usb_hif_power_down,
746 #ifdef CONFIG_PM
747 	.suspend		= ath10k_usb_hif_suspend,
748 	.resume			= ath10k_usb_hif_resume,
749 #endif
750 };
751 
752 static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
753 {
754 	u8 pipe_num = ATH10K_USB_PIPE_INVALID;
755 
756 	switch (ep_address) {
757 	case ATH10K_USB_EP_ADDR_APP_CTRL_IN:
758 		pipe_num = ATH10K_USB_PIPE_RX_CTRL;
759 		*urb_count = RX_URB_COUNT;
760 		break;
761 	case ATH10K_USB_EP_ADDR_APP_DATA_IN:
762 		pipe_num = ATH10K_USB_PIPE_RX_DATA;
763 		*urb_count = RX_URB_COUNT;
764 		break;
765 	case ATH10K_USB_EP_ADDR_APP_INT_IN:
766 		pipe_num = ATH10K_USB_PIPE_RX_INT;
767 		*urb_count = RX_URB_COUNT;
768 		break;
769 	case ATH10K_USB_EP_ADDR_APP_DATA2_IN:
770 		pipe_num = ATH10K_USB_PIPE_RX_DATA2;
771 		*urb_count = RX_URB_COUNT;
772 		break;
773 	case ATH10K_USB_EP_ADDR_APP_CTRL_OUT:
774 		pipe_num = ATH10K_USB_PIPE_TX_CTRL;
775 		*urb_count = TX_URB_COUNT;
776 		break;
777 	case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT:
778 		pipe_num = ATH10K_USB_PIPE_TX_DATA_LP;
779 		*urb_count = TX_URB_COUNT;
780 		break;
781 	case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT:
782 		pipe_num = ATH10K_USB_PIPE_TX_DATA_MP;
783 		*urb_count = TX_URB_COUNT;
784 		break;
785 	case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT:
786 		pipe_num = ATH10K_USB_PIPE_TX_DATA_HP;
787 		*urb_count = TX_URB_COUNT;
788 		break;
789 	default:
790 		/* note: there may be endpoints not currently used */
791 		break;
792 	}
793 
794 	return pipe_num;
795 }
796 
797 static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
798 					   struct ath10k_usb_pipe *pipe,
799 					   int urb_cnt)
800 {
801 	struct ath10k_urb_context *urb_context;
802 	int i;
803 
804 	INIT_LIST_HEAD(&pipe->urb_list_head);
805 	init_usb_anchor(&pipe->urb_submitted);
806 
807 	for (i = 0; i < urb_cnt; i++) {
808 		urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
809 		if (!urb_context)
810 			return -ENOMEM;
811 
812 		urb_context->pipe = pipe;
813 
814 		/* we are only allocate the urb contexts here, the actual URB
815 		 * is allocated from the kernel as needed to do a transaction
816 		 */
817 		pipe->urb_alloc++;
818 		ath10k_usb_free_urb_to_pipe(pipe, urb_context);
819 	}
820 
821 	ath10k_dbg(ar, ATH10K_DBG_USB,
822 		   "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n",
823 		   pipe->logical_pipe_num, pipe->usb_pipe_handle,
824 		   pipe->urb_alloc);
825 
826 	return 0;
827 }
828 
829 static int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
830 					   struct usb_interface *interface)
831 {
832 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
833 	struct usb_host_interface *iface_desc = interface->cur_altsetting;
834 	struct usb_endpoint_descriptor *endpoint;
835 	struct ath10k_usb_pipe *pipe;
836 	int ret, i, urbcount;
837 	u8 pipe_num;
838 
839 	ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
840 
841 	/* walk decriptors and setup pipes */
842 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
843 		endpoint = &iface_desc->endpoint[i].desc;
844 
845 		if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
846 			ath10k_dbg(ar, ATH10K_DBG_USB,
847 				   "usb %s bulk ep 0x%2.2x maxpktsz %d\n",
848 				   ATH10K_USB_IS_DIR_IN
849 				   (endpoint->bEndpointAddress) ?
850 				   "rx" : "tx", endpoint->bEndpointAddress,
851 				   le16_to_cpu(endpoint->wMaxPacketSize));
852 		} else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
853 			ath10k_dbg(ar, ATH10K_DBG_USB,
854 				   "usb %s int ep 0x%2.2x maxpktsz %d interval %d\n",
855 				   ATH10K_USB_IS_DIR_IN
856 				   (endpoint->bEndpointAddress) ?
857 				   "rx" : "tx", endpoint->bEndpointAddress,
858 				   le16_to_cpu(endpoint->wMaxPacketSize),
859 				   endpoint->bInterval);
860 		} else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
861 			/* TODO for ISO */
862 			ath10k_dbg(ar, ATH10K_DBG_USB,
863 				   "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
864 				   ATH10K_USB_IS_DIR_IN
865 				   (endpoint->bEndpointAddress) ?
866 				   "rx" : "tx", endpoint->bEndpointAddress,
867 				   le16_to_cpu(endpoint->wMaxPacketSize),
868 				   endpoint->bInterval);
869 		}
870 		urbcount = 0;
871 
872 		pipe_num =
873 		    ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
874 						    &urbcount);
875 		if (pipe_num == ATH10K_USB_PIPE_INVALID)
876 			continue;
877 
878 		pipe = &ar_usb->pipes[pipe_num];
879 		if (pipe->ar_usb)
880 			/* hmmm..pipe was already setup */
881 			continue;
882 
883 		pipe->ar_usb = ar_usb;
884 		pipe->logical_pipe_num = pipe_num;
885 		pipe->ep_address = endpoint->bEndpointAddress;
886 		pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
887 
888 		if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
889 			if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
890 				pipe->usb_pipe_handle =
891 				    usb_rcvbulkpipe(ar_usb->udev,
892 						    pipe->ep_address);
893 			} else {
894 				pipe->usb_pipe_handle =
895 				    usb_sndbulkpipe(ar_usb->udev,
896 						    pipe->ep_address);
897 			}
898 		} else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
899 			if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
900 				pipe->usb_pipe_handle =
901 				    usb_rcvintpipe(ar_usb->udev,
902 						   pipe->ep_address);
903 			} else {
904 				pipe->usb_pipe_handle =
905 				    usb_sndintpipe(ar_usb->udev,
906 						   pipe->ep_address);
907 			}
908 		} else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
909 			/* TODO for ISO */
910 			if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
911 				pipe->usb_pipe_handle =
912 				    usb_rcvisocpipe(ar_usb->udev,
913 						    pipe->ep_address);
914 			} else {
915 				pipe->usb_pipe_handle =
916 				    usb_sndisocpipe(ar_usb->udev,
917 						    pipe->ep_address);
918 			}
919 		}
920 
921 		pipe->ep_desc = endpoint;
922 
923 		if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
924 			pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
925 
926 		ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
927 		if (ret)
928 			return ret;
929 	}
930 
931 	return 0;
932 }
933 
934 static int ath10k_usb_create(struct ath10k *ar,
935 			     struct usb_interface *interface)
936 {
937 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
938 	struct usb_device *dev = interface_to_usbdev(interface);
939 	struct ath10k_usb_pipe *pipe;
940 	int ret, i;
941 
942 	usb_set_intfdata(interface, ar_usb);
943 	spin_lock_init(&ar_usb->cs_lock);
944 	ar_usb->udev = dev;
945 	ar_usb->interface = interface;
946 
947 	for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
948 		pipe = &ar_usb->pipes[i];
949 		INIT_WORK(&pipe->io_complete_work,
950 			  ath10k_usb_io_comp_work);
951 		skb_queue_head_init(&pipe->io_comp_queue);
952 	}
953 
954 	ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL);
955 	if (!ar_usb->diag_cmd_buffer) {
956 		ret = -ENOMEM;
957 		goto err;
958 	}
959 
960 	ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP,
961 					   GFP_KERNEL);
962 	if (!ar_usb->diag_resp_buffer) {
963 		ret = -ENOMEM;
964 		goto err;
965 	}
966 
967 	ret = ath10k_usb_setup_pipe_resources(ar, interface);
968 	if (ret)
969 		goto err;
970 
971 	return 0;
972 
973 err:
974 	ath10k_usb_destroy(ar);
975 	return ret;
976 }
977 
978 /* ath10k usb driver registered functions */
979 static int ath10k_usb_probe(struct usb_interface *interface,
980 			    const struct usb_device_id *id)
981 {
982 	struct ath10k *ar;
983 	struct ath10k_usb *ar_usb;
984 	struct usb_device *dev = interface_to_usbdev(interface);
985 	int ret, vendor_id, product_id;
986 	enum ath10k_hw_rev hw_rev;
987 	struct ath10k_bus_params bus_params;
988 
989 	/* Assumption: All USB based chipsets (so far) are QCA9377 based.
990 	 * If there will be newer chipsets that does not use the hw reg
991 	 * setup as defined in qca6174_regs and qca6174_values, this
992 	 * assumption is no longer valid and hw_rev must be setup differently
993 	 * depending on chipset.
994 	 */
995 	hw_rev = ATH10K_HW_QCA9377;
996 
997 	ar = ath10k_core_create(sizeof(*ar_usb), &dev->dev, ATH10K_BUS_USB,
998 				hw_rev, &ath10k_usb_hif_ops);
999 	if (!ar) {
1000 		dev_err(&dev->dev, "failed to allocate core\n");
1001 		return -ENOMEM;
1002 	}
1003 
1004 	usb_get_dev(dev);
1005 	vendor_id = le16_to_cpu(dev->descriptor.idVendor);
1006 	product_id = le16_to_cpu(dev->descriptor.idProduct);
1007 
1008 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1009 		   "usb new func vendor 0x%04x product 0x%04x\n",
1010 		   vendor_id, product_id);
1011 
1012 	ar_usb = ath10k_usb_priv(ar);
1013 	ret = ath10k_usb_create(ar, interface);
1014 	ar_usb->ar = ar;
1015 
1016 	ar->dev_id = product_id;
1017 	ar->id.vendor = vendor_id;
1018 	ar->id.device = product_id;
1019 
1020 	bus_params.dev_type = ATH10K_DEV_TYPE_HL;
1021 	/* TODO: don't know yet how to get chip_id with USB */
1022 	bus_params.chip_id = 0;
1023 	ret = ath10k_core_register(ar, &bus_params);
1024 	if (ret) {
1025 		ath10k_warn(ar, "failed to register driver core: %d\n", ret);
1026 		goto err;
1027 	}
1028 
1029 	/* TODO: remove this once USB support is fully implemented */
1030 	ath10k_warn(ar, "WARNING: ath10k USB support is incomplete, don't expect anything to work!\n");
1031 
1032 	return 0;
1033 
1034 err:
1035 	ath10k_core_destroy(ar);
1036 
1037 	usb_put_dev(dev);
1038 
1039 	return ret;
1040 }
1041 
1042 static void ath10k_usb_remove(struct usb_interface *interface)
1043 {
1044 	struct ath10k_usb *ar_usb;
1045 
1046 	ar_usb = usb_get_intfdata(interface);
1047 	if (!ar_usb)
1048 		return;
1049 
1050 	ath10k_core_unregister(ar_usb->ar);
1051 	ath10k_usb_destroy(ar_usb->ar);
1052 	usb_put_dev(interface_to_usbdev(interface));
1053 	ath10k_core_destroy(ar_usb->ar);
1054 }
1055 
1056 #ifdef CONFIG_PM
1057 
1058 static int ath10k_usb_pm_suspend(struct usb_interface *interface,
1059 				 pm_message_t message)
1060 {
1061 	struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1062 
1063 	ath10k_usb_flush_all(ar_usb->ar);
1064 	return 0;
1065 }
1066 
1067 static int ath10k_usb_pm_resume(struct usb_interface *interface)
1068 {
1069 	struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1070 	struct ath10k *ar = ar_usb->ar;
1071 
1072 	ath10k_usb_post_recv_transfers(ar,
1073 				       &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
1074 
1075 	return 0;
1076 }
1077 
1078 #else
1079 
1080 #define ath10k_usb_pm_suspend NULL
1081 #define ath10k_usb_pm_resume NULL
1082 
1083 #endif
1084 
1085 /* table of devices that work with this driver */
1086 static struct usb_device_id ath10k_usb_ids[] = {
1087 	{USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */
1088 	{ /* Terminating entry */ },
1089 };
1090 
1091 MODULE_DEVICE_TABLE(usb, ath10k_usb_ids);
1092 
1093 static struct usb_driver ath10k_usb_driver = {
1094 	.name = "ath10k_usb",
1095 	.probe = ath10k_usb_probe,
1096 	.suspend = ath10k_usb_pm_suspend,
1097 	.resume = ath10k_usb_pm_resume,
1098 	.disconnect = ath10k_usb_remove,
1099 	.id_table = ath10k_usb_ids,
1100 	.supports_autosuspend = true,
1101 	.disable_hub_initiated_lpm = 1,
1102 };
1103 
1104 module_usb_driver(ath10k_usb_driver);
1105 
1106 MODULE_AUTHOR("Atheros Communications, Inc.");
1107 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices");
1108 MODULE_LICENSE("Dual BSD/GPL");
1109