Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / net / wireless / ath / ath10k / usb.c
blobd4803ff5a78a75eb7e2d63bc77a1623f146e283e
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>
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.
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.
19 #include <linux/module.h>
20 #include <linux/usb.h>
22 #include "debug.h"
23 #include "core.h"
24 #include "bmi.h"
25 #include "hif.h"
26 #include "htc.h"
27 #include "usb.h"
29 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
30 struct ath10k_usb_pipe *recv_pipe);
32 /* inlined helper functions */
34 static inline enum ath10k_htc_ep_id
35 eid_from_htc_hdr(struct ath10k_htc_hdr *htc_hdr)
37 return (enum ath10k_htc_ep_id)htc_hdr->eid;
40 static inline bool is_trailer_only_msg(struct ath10k_htc_hdr *htc_hdr)
42 return __le16_to_cpu(htc_hdr->len) == htc_hdr->trailer_len;
45 /* pipe/urb operations */
46 static struct ath10k_urb_context *
47 ath10k_usb_alloc_urb_from_pipe(struct ath10k_usb_pipe *pipe)
49 struct ath10k_urb_context *urb_context = NULL;
50 unsigned long flags;
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--;
59 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
61 return urb_context;
64 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
65 struct ath10k_urb_context *urb_context)
67 unsigned long flags;
69 spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
71 pipe->urb_cnt++;
72 list_add(&urb_context->link, &pipe->urb_list_head);
74 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
77 static void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context)
79 dev_kfree_skb(urb_context->skb);
80 urb_context->skb = NULL;
82 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
85 static void ath10k_usb_free_pipe_resources(struct ath10k *ar,
86 struct ath10k_usb_pipe *pipe)
88 struct ath10k_urb_context *urb_context;
90 if (!pipe->ar_usb) {
91 /* nothing allocated for this pipe */
92 return;
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);
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);
107 for (;;) {
108 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
110 if (!urb_context)
111 break;
113 kfree(urb_context);
117 static void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar)
119 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
120 int i;
122 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++)
123 ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]);
126 /* hif usb rx/tx completion functions */
128 static void ath10k_usb_recv_complete(struct urb *urb)
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;
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);
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
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;
159 goto cleanup_recv_urb;
162 if (urb->actual_length == 0)
163 goto cleanup_recv_urb;
165 skb = urb_context->skb;
167 /* we are going to pass it up */
168 urb_context->skb = NULL;
169 skb_put(skb, urb->actual_length);
171 /* note: queue implements a lock */
172 skb_queue_tail(&pipe->io_comp_queue, skb);
173 schedule_work(&pipe->io_complete_work);
175 cleanup_recv_urb:
176 ath10k_usb_cleanup_recv_urb(urb_context);
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);
185 static void ath10k_usb_transmit_complete(struct urb *urb)
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;
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);
198 skb = urb_context->skb;
199 urb_context->skb = NULL;
200 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
202 /* note: queue implements a lock */
203 skb_queue_tail(&pipe->io_comp_queue, skb);
204 schedule_work(&pipe->io_complete_work);
207 /* pipe operations */
208 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
209 struct ath10k_usb_pipe *recv_pipe)
211 struct ath10k_urb_context *urb_context;
212 struct urb *urb;
213 int usb_status;
215 for (;;) {
216 urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe);
217 if (!urb_context)
218 break;
220 urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE);
221 if (!urb_context->skb)
222 goto err;
224 urb = usb_alloc_urb(0, GFP_ATOMIC);
225 if (!urb)
226 goto err;
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);
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);
241 usb_anchor_urb(urb, &recv_pipe->urb_submitted);
242 usb_status = usb_submit_urb(urb, GFP_ATOMIC);
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;
252 usb_free_urb(urb);
255 return;
257 err:
258 ath10k_usb_cleanup_recv_urb(urb_context);
261 static void ath10k_usb_flush_all(struct ath10k *ar)
263 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
264 int i;
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);
274 static void ath10k_usb_start_recv_pipes(struct ath10k *ar)
276 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
278 ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
280 ath10k_usb_post_recv_transfers(ar,
281 &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
284 static void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb)
286 struct ath10k_htc_hdr *htc_hdr;
287 struct ath10k_htc_ep *ep;
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... */
295 static void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb)
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;
305 htc_hdr = (struct ath10k_htc_hdr *)skb->data;
306 eid = eid_from_htc_hdr(htc_hdr);
307 ep = &ar->htc.endpoint[eid];
309 if (ep->service_id == 0) {
310 ath10k_warn(ar, "ep %d is not connected\n", eid);
311 goto out_free_skb;
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;
320 if (payload_len < htc_hdr->trailer_len) {
321 ath10k_warn(ar, "malformed frame received, firmware crashed?\n");
322 goto out_free_skb;
325 if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) {
326 trailer = skb->data + sizeof(*htc_hdr) + payload_len -
327 htc_hdr->trailer_len;
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;
338 if (is_trailer_only_msg(htc_hdr))
339 goto out_free_skb;
341 /* strip off the trailer from the skb since it should not
342 * be passed on to upper layers
344 skb_trim(skb, skb->len - htc_hdr->trailer_len);
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... */
351 return;
353 out_free_skb:
354 dev_kfree_skb(skb);
357 static void ath10k_usb_io_comp_work(struct work_struct *work)
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;
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);
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))
376 static void ath10k_usb_destroy(struct ath10k *ar)
378 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
380 ath10k_usb_flush_all(ar);
381 ath10k_usb_cleanup_pipe_resources(ar);
382 usb_set_intfdata(ar_usb->interface, NULL);
384 kfree(ar_usb->diag_cmd_buffer);
385 kfree(ar_usb->diag_resp_buffer);
388 static int ath10k_usb_hif_start(struct ath10k *ar)
390 int i;
391 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
393 ath10k_usb_start_recv_pipes(ar);
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;
402 return 0;
405 static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
406 struct ath10k_hif_sg_item *items, int n_items)
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;
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;
422 skb = items[i].transfer_context;
423 urb_context->skb = skb;
425 urb = usb_alloc_urb(0, GFP_ATOMIC);
426 if (!urb) {
427 ret = -ENOMEM;
428 goto err_free_urb_to_pipe;
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);
438 if (!(skb->len % pipe->max_packet_size)) {
439 /* hit a max packet boundary on this pipe */
440 urb->transfer_flags |= URB_ZERO_PACKET;
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;
453 usb_free_urb(urb);
456 return 0;
458 err_free_urb_to_pipe:
459 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
460 err:
461 return ret;
464 static void ath10k_usb_hif_stop(struct ath10k *ar)
466 ath10k_usb_flush_all(ar);
469 static u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id)
471 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
473 return ar_usb->pipes[pipe_id].urb_cnt;
476 static int ath10k_usb_submit_ctrl_out(struct ath10k *ar,
477 u8 req, u16 value, u16 index, void *data,
478 u32 size)
480 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
481 u8 *buf = NULL;
482 int ret;
484 if (size > 0) {
485 buf = kmemdup(data, size, GFP_KERNEL);
486 if (!buf)
487 return -ENOMEM;
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);
498 if (ret < 0) {
499 ath10k_warn(ar, "Failed to submit usb control message: %d\n",
500 ret);
501 kfree(buf);
502 return ret;
505 kfree(buf);
507 return 0;
510 static int ath10k_usb_submit_ctrl_in(struct ath10k *ar,
511 u8 req, u16 value, u16 index, void *data,
512 u32 size)
514 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
515 u8 *buf = NULL;
516 int ret;
518 if (size > 0) {
519 buf = kmalloc(size, GFP_KERNEL);
520 if (!buf)
521 return -ENOMEM;
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);
532 if (ret < 0) {
533 ath10k_warn(ar, "Failed to read usb control message: %d\n",
534 ret);
535 kfree(buf);
536 return ret;
539 memcpy((u8 *)data, buf, size);
541 kfree(buf);
543 return 0;
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)
551 int ret;
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;
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;
567 return 0;
568 err:
569 return ret;
572 static int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
573 size_t buf_len)
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;
580 if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read))
581 return -EINVAL;
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);
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;
598 if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read))
599 return -EMSGSIZE;
601 memcpy(buf, ar_usb->diag_resp_buffer,
602 sizeof(struct ath10k_usb_ctrl_diag_resp_read));
604 return 0;
607 static int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address,
608 const void *data, int nbytes)
610 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
611 struct ath10k_usb_ctrl_diag_cmd_write *cmd;
612 int ret;
614 if (nbytes != sizeof(cmd->value))
615 return -EINVAL;
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);
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;
631 return 0;
634 static int ath10k_usb_bmi_exchange_msg(struct ath10k *ar,
635 void *req, u32 req_len,
636 void *resp, u32 *resp_len)
638 int ret;
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;
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;
664 return 0;
667 static void ath10k_usb_hif_get_default_pipe(struct ath10k *ar,
668 u8 *ul_pipe, u8 *dl_pipe)
670 *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
671 *dl_pipe = ATH10K_USB_PIPE_RX_CTRL;
674 static int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id,
675 u8 *ul_pipe, u8 *dl_pipe)
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
689 *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
690 break;
691 default:
692 return -EPERM;
695 return 0;
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.
704 static void ath10k_usb_hif_send_complete_check(struct ath10k *ar,
705 u8 pipe, int force)
709 static int ath10k_usb_hif_power_up(struct ath10k *ar)
711 return 0;
714 static void ath10k_usb_hif_power_down(struct ath10k *ar)
716 ath10k_usb_flush_all(ar);
719 #ifdef CONFIG_PM
721 static int ath10k_usb_hif_suspend(struct ath10k *ar)
723 return -EOPNOTSUPP;
726 static int ath10k_usb_hif_resume(struct ath10k *ar)
728 return -EOPNOTSUPP;
730 #endif
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
751 static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
753 u8 pipe_num = ATH10K_USB_PIPE_INVALID;
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;
793 return pipe_num;
796 static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
797 struct ath10k_usb_pipe *pipe,
798 int urb_cnt)
800 struct ath10k_urb_context *urb_context;
801 int i;
803 INIT_LIST_HEAD(&pipe->urb_list_head);
804 init_usb_anchor(&pipe->urb_submitted);
806 for (i = 0; i < urb_cnt; i++) {
807 urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
808 if (!urb_context)
809 return -ENOMEM;
811 urb_context->pipe = pipe;
813 /* we are only allocate the urb contexts here, the actual URB
814 * is allocated from the kernel as needed to do a transaction
816 pipe->urb_alloc++;
817 ath10k_usb_free_urb_to_pipe(pipe, urb_context);
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);
825 return 0;
828 static int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
829 struct usb_interface *interface)
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;
838 ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
840 /* walk decriptors and setup pipes */
841 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
842 endpoint = &iface_desc->endpoint[i].desc;
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);
869 urbcount = 0;
871 pipe_num =
872 ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
873 &urbcount);
874 if (pipe_num == ATH10K_USB_PIPE_INVALID)
875 continue;
877 pipe = &ar_usb->pipes[pipe_num];
878 if (pipe->ar_usb)
879 /* hmmm..pipe was already setup */
880 continue;
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);
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);
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);
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);
920 pipe->ep_desc = endpoint;
922 if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
923 pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
925 ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
926 if (ret)
927 return ret;
930 return 0;
933 static int ath10k_usb_create(struct ath10k *ar,
934 struct usb_interface *interface)
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;
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;
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);
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;
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;
966 ret = ath10k_usb_setup_pipe_resources(ar, interface);
967 if (ret)
968 goto err;
970 return 0;
972 err:
973 ath10k_usb_destroy(ar);
974 return ret;
977 /* ath10k usb driver registered functions */
978 static int ath10k_usb_probe(struct usb_interface *interface,
979 const struct usb_device_id *id)
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;
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.
994 hw_rev = ATH10K_HW_QCA9377;
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;
1003 usb_get_dev(dev);
1004 vendor_id = le16_to_cpu(dev->descriptor.idVendor);
1005 product_id = le16_to_cpu(dev->descriptor.idProduct);
1007 ath10k_dbg(ar, ATH10K_DBG_BOOT,
1008 "usb new func vendor 0x%04x product 0x%04x\n",
1009 vendor_id, product_id);
1011 ar_usb = ath10k_usb_priv(ar);
1012 ret = ath10k_usb_create(ar, interface);
1013 ar_usb->ar = ar;
1015 ar->dev_id = product_id;
1016 ar->id.vendor = vendor_id;
1017 ar->id.device = product_id;
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;
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");
1030 return 0;
1032 err:
1033 ath10k_core_destroy(ar);
1035 usb_put_dev(dev);
1037 return ret;
1040 static void ath10k_usb_remove(struct usb_interface *interface)
1042 struct ath10k_usb *ar_usb;
1044 ar_usb = usb_get_intfdata(interface);
1045 if (!ar_usb)
1046 return;
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);
1054 #ifdef CONFIG_PM
1056 static int ath10k_usb_pm_suspend(struct usb_interface *interface,
1057 pm_message_t message)
1059 struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1061 ath10k_usb_flush_all(ar_usb->ar);
1062 return 0;
1065 static int ath10k_usb_pm_resume(struct usb_interface *interface)
1067 struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1068 struct ath10k *ar = ar_usb->ar;
1070 ath10k_usb_post_recv_transfers(ar,
1071 &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
1073 return 0;
1076 #else
1078 #define ath10k_usb_pm_suspend NULL
1079 #define ath10k_usb_pm_resume NULL
1081 #endif
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 */ },
1089 MODULE_DEVICE_TABLE(usb, ath10k_usb_ids);
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,
1102 module_usb_driver(ath10k_usb_driver);
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");