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