xref: /openbmc/linux/drivers/net/wireless/ath/ath10k/usb.c (revision 0a73d21e)
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 {
711 	return 0;
712 }
713 
714 static void ath10k_usb_hif_power_down(struct ath10k *ar)
715 {
716 	ath10k_usb_flush_all(ar);
717 }
718 
719 #ifdef CONFIG_PM
720 
721 static int ath10k_usb_hif_suspend(struct ath10k *ar)
722 {
723 	return -EOPNOTSUPP;
724 }
725 
726 static int ath10k_usb_hif_resume(struct ath10k *ar)
727 {
728 	return -EOPNOTSUPP;
729 }
730 #endif
731 
732 static const struct ath10k_hif_ops ath10k_usb_hif_ops = {
733 	.tx_sg			= ath10k_usb_hif_tx_sg,
734 	.diag_read		= ath10k_usb_hif_diag_read,
735 	.diag_write		= ath10k_usb_hif_diag_write,
736 	.exchange_bmi_msg	= ath10k_usb_bmi_exchange_msg,
737 	.start			= ath10k_usb_hif_start,
738 	.stop			= ath10k_usb_hif_stop,
739 	.map_service_to_pipe	= ath10k_usb_hif_map_service_to_pipe,
740 	.get_default_pipe	= ath10k_usb_hif_get_default_pipe,
741 	.send_complete_check	= ath10k_usb_hif_send_complete_check,
742 	.get_free_queue_number	= ath10k_usb_hif_get_free_queue_number,
743 	.power_up		= ath10k_usb_hif_power_up,
744 	.power_down		= ath10k_usb_hif_power_down,
745 #ifdef CONFIG_PM
746 	.suspend		= ath10k_usb_hif_suspend,
747 	.resume			= ath10k_usb_hif_resume,
748 #endif
749 };
750 
751 static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
752 {
753 	u8 pipe_num = ATH10K_USB_PIPE_INVALID;
754 
755 	switch (ep_address) {
756 	case ATH10K_USB_EP_ADDR_APP_CTRL_IN:
757 		pipe_num = ATH10K_USB_PIPE_RX_CTRL;
758 		*urb_count = RX_URB_COUNT;
759 		break;
760 	case ATH10K_USB_EP_ADDR_APP_DATA_IN:
761 		pipe_num = ATH10K_USB_PIPE_RX_DATA;
762 		*urb_count = RX_URB_COUNT;
763 		break;
764 	case ATH10K_USB_EP_ADDR_APP_INT_IN:
765 		pipe_num = ATH10K_USB_PIPE_RX_INT;
766 		*urb_count = RX_URB_COUNT;
767 		break;
768 	case ATH10K_USB_EP_ADDR_APP_DATA2_IN:
769 		pipe_num = ATH10K_USB_PIPE_RX_DATA2;
770 		*urb_count = RX_URB_COUNT;
771 		break;
772 	case ATH10K_USB_EP_ADDR_APP_CTRL_OUT:
773 		pipe_num = ATH10K_USB_PIPE_TX_CTRL;
774 		*urb_count = TX_URB_COUNT;
775 		break;
776 	case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT:
777 		pipe_num = ATH10K_USB_PIPE_TX_DATA_LP;
778 		*urb_count = TX_URB_COUNT;
779 		break;
780 	case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT:
781 		pipe_num = ATH10K_USB_PIPE_TX_DATA_MP;
782 		*urb_count = TX_URB_COUNT;
783 		break;
784 	case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT:
785 		pipe_num = ATH10K_USB_PIPE_TX_DATA_HP;
786 		*urb_count = TX_URB_COUNT;
787 		break;
788 	default:
789 		/* note: there may be endpoints not currently used */
790 		break;
791 	}
792 
793 	return pipe_num;
794 }
795 
796 static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
797 					   struct ath10k_usb_pipe *pipe,
798 					   int urb_cnt)
799 {
800 	struct ath10k_urb_context *urb_context;
801 	int i;
802 
803 	INIT_LIST_HEAD(&pipe->urb_list_head);
804 	init_usb_anchor(&pipe->urb_submitted);
805 
806 	for (i = 0; i < urb_cnt; i++) {
807 		urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
808 		if (!urb_context)
809 			return -ENOMEM;
810 
811 		urb_context->pipe = pipe;
812 
813 		/* we are only allocate the urb contexts here, the actual URB
814 		 * is allocated from the kernel as needed to do a transaction
815 		 */
816 		pipe->urb_alloc++;
817 		ath10k_usb_free_urb_to_pipe(pipe, urb_context);
818 	}
819 
820 	ath10k_dbg(ar, ATH10K_DBG_USB,
821 		   "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n",
822 		   pipe->logical_pipe_num, pipe->usb_pipe_handle,
823 		   pipe->urb_alloc);
824 
825 	return 0;
826 }
827 
828 static int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
829 					   struct usb_interface *interface)
830 {
831 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
832 	struct usb_host_interface *iface_desc = interface->cur_altsetting;
833 	struct usb_endpoint_descriptor *endpoint;
834 	struct ath10k_usb_pipe *pipe;
835 	int ret, i, urbcount;
836 	u8 pipe_num;
837 
838 	ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
839 
840 	/* walk decriptors and setup pipes */
841 	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
842 		endpoint = &iface_desc->endpoint[i].desc;
843 
844 		if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
845 			ath10k_dbg(ar, ATH10K_DBG_USB,
846 				   "usb %s bulk ep 0x%2.2x maxpktsz %d\n",
847 				   ATH10K_USB_IS_DIR_IN
848 				   (endpoint->bEndpointAddress) ?
849 				   "rx" : "tx", endpoint->bEndpointAddress,
850 				   le16_to_cpu(endpoint->wMaxPacketSize));
851 		} else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
852 			ath10k_dbg(ar, ATH10K_DBG_USB,
853 				   "usb %s int ep 0x%2.2x maxpktsz %d interval %d\n",
854 				   ATH10K_USB_IS_DIR_IN
855 				   (endpoint->bEndpointAddress) ?
856 				   "rx" : "tx", endpoint->bEndpointAddress,
857 				   le16_to_cpu(endpoint->wMaxPacketSize),
858 				   endpoint->bInterval);
859 		} else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
860 			/* TODO for ISO */
861 			ath10k_dbg(ar, ATH10K_DBG_USB,
862 				   "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
863 				   ATH10K_USB_IS_DIR_IN
864 				   (endpoint->bEndpointAddress) ?
865 				   "rx" : "tx", endpoint->bEndpointAddress,
866 				   le16_to_cpu(endpoint->wMaxPacketSize),
867 				   endpoint->bInterval);
868 		}
869 		urbcount = 0;
870 
871 		pipe_num =
872 		    ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
873 						    &urbcount);
874 		if (pipe_num == ATH10K_USB_PIPE_INVALID)
875 			continue;
876 
877 		pipe = &ar_usb->pipes[pipe_num];
878 		if (pipe->ar_usb)
879 			/* hmmm..pipe was already setup */
880 			continue;
881 
882 		pipe->ar_usb = ar_usb;
883 		pipe->logical_pipe_num = pipe_num;
884 		pipe->ep_address = endpoint->bEndpointAddress;
885 		pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
886 
887 		if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
888 			if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
889 				pipe->usb_pipe_handle =
890 				    usb_rcvbulkpipe(ar_usb->udev,
891 						    pipe->ep_address);
892 			} else {
893 				pipe->usb_pipe_handle =
894 				    usb_sndbulkpipe(ar_usb->udev,
895 						    pipe->ep_address);
896 			}
897 		} else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
898 			if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
899 				pipe->usb_pipe_handle =
900 				    usb_rcvintpipe(ar_usb->udev,
901 						   pipe->ep_address);
902 			} else {
903 				pipe->usb_pipe_handle =
904 				    usb_sndintpipe(ar_usb->udev,
905 						   pipe->ep_address);
906 			}
907 		} else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
908 			/* TODO for ISO */
909 			if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
910 				pipe->usb_pipe_handle =
911 				    usb_rcvisocpipe(ar_usb->udev,
912 						    pipe->ep_address);
913 			} else {
914 				pipe->usb_pipe_handle =
915 				    usb_sndisocpipe(ar_usb->udev,
916 						    pipe->ep_address);
917 			}
918 		}
919 
920 		pipe->ep_desc = endpoint;
921 
922 		if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
923 			pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
924 
925 		ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
926 		if (ret)
927 			return ret;
928 	}
929 
930 	return 0;
931 }
932 
933 static int ath10k_usb_create(struct ath10k *ar,
934 			     struct usb_interface *interface)
935 {
936 	struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
937 	struct usb_device *dev = interface_to_usbdev(interface);
938 	struct ath10k_usb_pipe *pipe;
939 	int ret, i;
940 
941 	usb_set_intfdata(interface, ar_usb);
942 	spin_lock_init(&ar_usb->cs_lock);
943 	ar_usb->udev = dev;
944 	ar_usb->interface = interface;
945 
946 	for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
947 		pipe = &ar_usb->pipes[i];
948 		INIT_WORK(&pipe->io_complete_work,
949 			  ath10k_usb_io_comp_work);
950 		skb_queue_head_init(&pipe->io_comp_queue);
951 	}
952 
953 	ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL);
954 	if (!ar_usb->diag_cmd_buffer) {
955 		ret = -ENOMEM;
956 		goto err;
957 	}
958 
959 	ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP,
960 					   GFP_KERNEL);
961 	if (!ar_usb->diag_resp_buffer) {
962 		ret = -ENOMEM;
963 		goto err;
964 	}
965 
966 	ret = ath10k_usb_setup_pipe_resources(ar, interface);
967 	if (ret)
968 		goto err;
969 
970 	return 0;
971 
972 err:
973 	ath10k_usb_destroy(ar);
974 	return ret;
975 }
976 
977 /* ath10k usb driver registered functions */
978 static int ath10k_usb_probe(struct usb_interface *interface,
979 			    const struct usb_device_id *id)
980 {
981 	struct ath10k *ar;
982 	struct ath10k_usb *ar_usb;
983 	struct usb_device *dev = interface_to_usbdev(interface);
984 	int ret, vendor_id, product_id;
985 	enum ath10k_hw_rev hw_rev;
986 	u32 chip_id;
987 
988 	/* Assumption: All USB based chipsets (so far) are QCA9377 based.
989 	 * If there will be newer chipsets that does not use the hw reg
990 	 * setup as defined in qca6174_regs and qca6174_values, this
991 	 * assumption is no longer valid and hw_rev must be setup differently
992 	 * depending on chipset.
993 	 */
994 	hw_rev = ATH10K_HW_QCA9377;
995 
996 	ar = ath10k_core_create(sizeof(*ar_usb), &dev->dev, ATH10K_BUS_USB,
997 				hw_rev, &ath10k_usb_hif_ops);
998 	if (!ar) {
999 		dev_err(&dev->dev, "failed to allocate core\n");
1000 		return -ENOMEM;
1001 	}
1002 
1003 	usb_get_dev(dev);
1004 	vendor_id = le16_to_cpu(dev->descriptor.idVendor);
1005 	product_id = le16_to_cpu(dev->descriptor.idProduct);
1006 
1007 	ath10k_dbg(ar, ATH10K_DBG_BOOT,
1008 		   "usb new func vendor 0x%04x product 0x%04x\n",
1009 		   vendor_id, product_id);
1010 
1011 	ar_usb = ath10k_usb_priv(ar);
1012 	ret = ath10k_usb_create(ar, interface);
1013 	ar_usb->ar = ar;
1014 
1015 	ar->dev_id = product_id;
1016 	ar->id.vendor = vendor_id;
1017 	ar->id.device = product_id;
1018 
1019 	/* TODO: don't know yet how to get chip_id with USB */
1020 	chip_id = 0;
1021 	ret = ath10k_core_register(ar, chip_id);
1022 	if (ret) {
1023 		ath10k_warn(ar, "failed to register driver core: %d\n", ret);
1024 		goto err;
1025 	}
1026 
1027 	/* TODO: remove this once USB support is fully implemented */
1028 	ath10k_warn(ar, "WARNING: ath10k USB support is incomplete, don't expect anything to work!\n");
1029 
1030 	return 0;
1031 
1032 err:
1033 	ath10k_core_destroy(ar);
1034 
1035 	usb_put_dev(dev);
1036 
1037 	return ret;
1038 }
1039 
1040 static void ath10k_usb_remove(struct usb_interface *interface)
1041 {
1042 	struct ath10k_usb *ar_usb;
1043 
1044 	ar_usb = usb_get_intfdata(interface);
1045 	if (!ar_usb)
1046 		return;
1047 
1048 	ath10k_core_unregister(ar_usb->ar);
1049 	ath10k_usb_destroy(ar_usb->ar);
1050 	usb_put_dev(interface_to_usbdev(interface));
1051 	ath10k_core_destroy(ar_usb->ar);
1052 }
1053 
1054 #ifdef CONFIG_PM
1055 
1056 static int ath10k_usb_pm_suspend(struct usb_interface *interface,
1057 				 pm_message_t message)
1058 {
1059 	struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1060 
1061 	ath10k_usb_flush_all(ar_usb->ar);
1062 	return 0;
1063 }
1064 
1065 static int ath10k_usb_pm_resume(struct usb_interface *interface)
1066 {
1067 	struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1068 	struct ath10k *ar = ar_usb->ar;
1069 
1070 	ath10k_usb_post_recv_transfers(ar,
1071 				       &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
1072 
1073 	return 0;
1074 }
1075 
1076 #else
1077 
1078 #define ath10k_usb_pm_suspend NULL
1079 #define ath10k_usb_pm_resume NULL
1080 
1081 #endif
1082 
1083 /* table of devices that work with this driver */
1084 static struct usb_device_id ath10k_usb_ids[] = {
1085 	{USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */
1086 	{ /* Terminating entry */ },
1087 };
1088 
1089 MODULE_DEVICE_TABLE(usb, ath10k_usb_ids);
1090 
1091 static struct usb_driver ath10k_usb_driver = {
1092 	.name = "ath10k_usb",
1093 	.probe = ath10k_usb_probe,
1094 	.suspend = ath10k_usb_pm_suspend,
1095 	.resume = ath10k_usb_pm_resume,
1096 	.disconnect = ath10k_usb_remove,
1097 	.id_table = ath10k_usb_ids,
1098 	.supports_autosuspend = true,
1099 	.disable_hub_initiated_lpm = 1,
1100 };
1101 
1102 module_usb_driver(ath10k_usb_driver);
1103 
1104 MODULE_AUTHOR("Atheros Communications, Inc.");
1105 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices");
1106 MODULE_LICENSE("Dual BSD/GPL");
1107