gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / wireless / ath / ath10k / usb.c
blob1e0343081be918fb92acf59b45f59085a5da9ffc
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 */
8 #include <linux/module.h>
9 #include <linux/usb.h>
11 #include "debug.h"
12 #include "core.h"
13 #include "bmi.h"
14 #include "hif.h"
15 #include "htc.h"
16 #include "usb.h"
18 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
19 struct ath10k_usb_pipe *recv_pipe);
21 /* inlined helper functions */
23 static inline enum ath10k_htc_ep_id
24 eid_from_htc_hdr(struct ath10k_htc_hdr *htc_hdr)
26 return (enum ath10k_htc_ep_id)htc_hdr->eid;
29 static inline bool is_trailer_only_msg(struct ath10k_htc_hdr *htc_hdr)
31 return __le16_to_cpu(htc_hdr->len) == htc_hdr->trailer_len;
34 /* pipe/urb operations */
35 static struct ath10k_urb_context *
36 ath10k_usb_alloc_urb_from_pipe(struct ath10k_usb_pipe *pipe)
38 struct ath10k_urb_context *urb_context = NULL;
39 unsigned long flags;
41 /* bail if this pipe is not initialized */
42 if (!pipe->ar_usb)
43 return NULL;
45 spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
46 if (!list_empty(&pipe->urb_list_head)) {
47 urb_context = list_first_entry(&pipe->urb_list_head,
48 struct ath10k_urb_context, link);
49 list_del(&urb_context->link);
50 pipe->urb_cnt--;
52 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
54 return urb_context;
57 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
58 struct ath10k_urb_context *urb_context)
60 unsigned long flags;
62 /* bail if this pipe is not initialized */
63 if (!pipe->ar_usb)
64 return;
66 spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
68 pipe->urb_cnt++;
69 list_add(&urb_context->link, &pipe->urb_list_head);
71 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
74 static void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context)
76 dev_kfree_skb(urb_context->skb);
77 urb_context->skb = NULL;
79 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
82 static void ath10k_usb_free_pipe_resources(struct ath10k *ar,
83 struct ath10k_usb_pipe *pipe)
85 struct ath10k_urb_context *urb_context;
87 if (!pipe->ar_usb) {
88 /* nothing allocated for this pipe */
89 return;
92 ath10k_dbg(ar, ATH10K_DBG_USB,
93 "usb free resources lpipe %d hpipe 0x%x urbs %d avail %d\n",
94 pipe->logical_pipe_num, pipe->usb_pipe_handle,
95 pipe->urb_alloc, pipe->urb_cnt);
97 if (pipe->urb_alloc != pipe->urb_cnt) {
98 ath10k_dbg(ar, ATH10K_DBG_USB,
99 "usb urb leak lpipe %d hpipe 0x%x urbs %d avail %d\n",
100 pipe->logical_pipe_num, pipe->usb_pipe_handle,
101 pipe->urb_alloc, pipe->urb_cnt);
104 for (;;) {
105 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
107 if (!urb_context)
108 break;
110 kfree(urb_context);
114 static void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar)
116 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
117 int i;
119 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++)
120 ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]);
123 /* hif usb rx/tx completion functions */
125 static void ath10k_usb_recv_complete(struct urb *urb)
127 struct ath10k_urb_context *urb_context = urb->context;
128 struct ath10k_usb_pipe *pipe = urb_context->pipe;
129 struct ath10k *ar = pipe->ar_usb->ar;
130 struct sk_buff *skb;
131 int status = 0;
133 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
134 "usb recv pipe %d stat %d len %d urb 0x%pK\n",
135 pipe->logical_pipe_num, urb->status, urb->actual_length,
136 urb);
138 if (urb->status != 0) {
139 status = -EIO;
140 switch (urb->status) {
141 case -ECONNRESET:
142 case -ENOENT:
143 case -ESHUTDOWN:
144 /* no need to spew these errors when device
145 * removed or urb killed due to driver shutdown
147 status = -ECANCELED;
148 break;
149 default:
150 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
151 "usb recv pipe %d ep 0x%2.2x failed: %d\n",
152 pipe->logical_pipe_num,
153 pipe->ep_address, urb->status);
154 break;
156 goto cleanup_recv_urb;
159 if (urb->actual_length == 0)
160 goto cleanup_recv_urb;
162 skb = urb_context->skb;
164 /* we are going to pass it up */
165 urb_context->skb = NULL;
166 skb_put(skb, urb->actual_length);
168 /* note: queue implements a lock */
169 skb_queue_tail(&pipe->io_comp_queue, skb);
170 schedule_work(&pipe->io_complete_work);
172 cleanup_recv_urb:
173 ath10k_usb_cleanup_recv_urb(urb_context);
175 if (status == 0 &&
176 pipe->urb_cnt >= pipe->urb_cnt_thresh) {
177 /* our free urbs are piling up, post more transfers */
178 ath10k_usb_post_recv_transfers(ar, pipe);
182 static void ath10k_usb_transmit_complete(struct urb *urb)
184 struct ath10k_urb_context *urb_context = urb->context;
185 struct ath10k_usb_pipe *pipe = urb_context->pipe;
186 struct ath10k *ar = pipe->ar_usb->ar;
187 struct sk_buff *skb;
189 if (urb->status != 0) {
190 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
191 "pipe: %d, failed:%d\n",
192 pipe->logical_pipe_num, urb->status);
195 skb = urb_context->skb;
196 urb_context->skb = NULL;
197 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
199 /* note: queue implements a lock */
200 skb_queue_tail(&pipe->io_comp_queue, skb);
201 schedule_work(&pipe->io_complete_work);
204 /* pipe operations */
205 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
206 struct ath10k_usb_pipe *recv_pipe)
208 struct ath10k_urb_context *urb_context;
209 struct urb *urb;
210 int usb_status;
212 for (;;) {
213 urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe);
214 if (!urb_context)
215 break;
217 urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE);
218 if (!urb_context->skb)
219 goto err;
221 urb = usb_alloc_urb(0, GFP_ATOMIC);
222 if (!urb)
223 goto err;
225 usb_fill_bulk_urb(urb,
226 recv_pipe->ar_usb->udev,
227 recv_pipe->usb_pipe_handle,
228 urb_context->skb->data,
229 ATH10K_USB_RX_BUFFER_SIZE,
230 ath10k_usb_recv_complete, urb_context);
232 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
233 "usb bulk recv submit %d 0x%x ep 0x%2.2x len %d buf 0x%pK\n",
234 recv_pipe->logical_pipe_num,
235 recv_pipe->usb_pipe_handle, recv_pipe->ep_address,
236 ATH10K_USB_RX_BUFFER_SIZE, urb_context->skb);
238 usb_anchor_urb(urb, &recv_pipe->urb_submitted);
239 usb_status = usb_submit_urb(urb, GFP_ATOMIC);
241 if (usb_status) {
242 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
243 "usb bulk recv failed: %d\n",
244 usb_status);
245 usb_unanchor_urb(urb);
246 usb_free_urb(urb);
247 goto err;
249 usb_free_urb(urb);
252 return;
254 err:
255 ath10k_usb_cleanup_recv_urb(urb_context);
258 static void ath10k_usb_flush_all(struct ath10k *ar)
260 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
261 int i;
263 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
264 if (ar_usb->pipes[i].ar_usb) {
265 usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted);
266 cancel_work_sync(&ar_usb->pipes[i].io_complete_work);
271 static void ath10k_usb_start_recv_pipes(struct ath10k *ar)
273 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
275 ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
277 ath10k_usb_post_recv_transfers(ar,
278 &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
281 static void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb)
283 struct ath10k_htc_hdr *htc_hdr;
284 struct ath10k_htc_ep *ep;
286 htc_hdr = (struct ath10k_htc_hdr *)skb->data;
287 ep = &ar->htc.endpoint[htc_hdr->eid];
288 ath10k_htc_notify_tx_completion(ep, skb);
289 /* The TX complete handler now owns the skb... */
292 static void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb)
294 struct ath10k_htc *htc = &ar->htc;
295 struct ath10k_htc_hdr *htc_hdr;
296 enum ath10k_htc_ep_id eid;
297 struct ath10k_htc_ep *ep;
298 u16 payload_len;
299 u8 *trailer;
300 int ret;
302 htc_hdr = (struct ath10k_htc_hdr *)skb->data;
303 eid = eid_from_htc_hdr(htc_hdr);
304 ep = &ar->htc.endpoint[eid];
306 if (ep->service_id == 0) {
307 ath10k_warn(ar, "ep %d is not connected\n", eid);
308 goto out_free_skb;
311 payload_len = le16_to_cpu(htc_hdr->len);
312 if (!payload_len) {
313 ath10k_warn(ar, "zero length frame received, firmware crashed?\n");
314 goto out_free_skb;
317 if (payload_len < htc_hdr->trailer_len) {
318 ath10k_warn(ar, "malformed frame received, firmware crashed?\n");
319 goto out_free_skb;
322 if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) {
323 trailer = skb->data + sizeof(*htc_hdr) + payload_len -
324 htc_hdr->trailer_len;
326 ret = ath10k_htc_process_trailer(htc,
327 trailer,
328 htc_hdr->trailer_len,
329 eid,
330 NULL,
331 NULL);
332 if (ret)
333 goto out_free_skb;
335 if (is_trailer_only_msg(htc_hdr))
336 goto out_free_skb;
338 /* strip off the trailer from the skb since it should not
339 * be passed on to upper layers
341 skb_trim(skb, skb->len - htc_hdr->trailer_len);
344 skb_pull(skb, sizeof(*htc_hdr));
345 ep->ep_ops.ep_rx_complete(ar, skb);
346 /* The RX complete handler now owns the skb... */
348 return;
350 out_free_skb:
351 dev_kfree_skb(skb);
354 static void ath10k_usb_io_comp_work(struct work_struct *work)
356 struct ath10k_usb_pipe *pipe = container_of(work,
357 struct ath10k_usb_pipe,
358 io_complete_work);
359 struct ath10k *ar = pipe->ar_usb->ar;
360 struct sk_buff *skb;
362 while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
363 if (pipe->flags & ATH10K_USB_PIPE_FLAG_TX)
364 ath10k_usb_tx_complete(ar, skb);
365 else
366 ath10k_usb_rx_complete(ar, skb);
370 #define ATH10K_USB_MAX_DIAG_CMD (sizeof(struct ath10k_usb_ctrl_diag_cmd_write))
371 #define ATH10K_USB_MAX_DIAG_RESP (sizeof(struct ath10k_usb_ctrl_diag_resp_read))
373 static void ath10k_usb_destroy(struct ath10k *ar)
375 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
377 ath10k_usb_flush_all(ar);
378 ath10k_usb_cleanup_pipe_resources(ar);
379 usb_set_intfdata(ar_usb->interface, NULL);
381 kfree(ar_usb->diag_cmd_buffer);
382 kfree(ar_usb->diag_resp_buffer);
385 static int ath10k_usb_hif_start(struct ath10k *ar)
387 int i;
388 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
390 ath10k_usb_start_recv_pipes(ar);
392 /* set the TX resource avail threshold for each TX pipe */
393 for (i = ATH10K_USB_PIPE_TX_CTRL;
394 i <= ATH10K_USB_PIPE_TX_DATA_HP; i++) {
395 ar_usb->pipes[i].urb_cnt_thresh =
396 ar_usb->pipes[i].urb_alloc / 2;
399 return 0;
402 static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
403 struct ath10k_hif_sg_item *items, int n_items)
405 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
406 struct ath10k_usb_pipe *pipe = &ar_usb->pipes[pipe_id];
407 struct ath10k_urb_context *urb_context;
408 struct sk_buff *skb;
409 struct urb *urb;
410 int ret, i;
412 for (i = 0; i < n_items; i++) {
413 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
414 if (!urb_context) {
415 ret = -ENOMEM;
416 goto err;
419 skb = items[i].transfer_context;
420 urb_context->skb = skb;
422 urb = usb_alloc_urb(0, GFP_ATOMIC);
423 if (!urb) {
424 ret = -ENOMEM;
425 goto err_free_urb_to_pipe;
428 usb_fill_bulk_urb(urb,
429 ar_usb->udev,
430 pipe->usb_pipe_handle,
431 skb->data,
432 skb->len,
433 ath10k_usb_transmit_complete, urb_context);
435 if (!(skb->len % pipe->max_packet_size)) {
436 /* hit a max packet boundary on this pipe */
437 urb->transfer_flags |= URB_ZERO_PACKET;
440 usb_anchor_urb(urb, &pipe->urb_submitted);
441 ret = usb_submit_urb(urb, GFP_ATOMIC);
442 if (ret) {
443 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
444 "usb bulk transmit failed: %d\n", ret);
445 usb_unanchor_urb(urb);
446 usb_free_urb(urb);
447 ret = -EINVAL;
448 goto err_free_urb_to_pipe;
451 usb_free_urb(urb);
454 return 0;
456 err_free_urb_to_pipe:
457 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
458 err:
459 return ret;
462 static void ath10k_usb_hif_stop(struct ath10k *ar)
464 ath10k_usb_flush_all(ar);
467 static u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id)
469 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
471 return ar_usb->pipes[pipe_id].urb_cnt;
474 static int ath10k_usb_submit_ctrl_out(struct ath10k *ar,
475 u8 req, u16 value, u16 index, void *data,
476 u32 size)
478 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
479 u8 *buf = NULL;
480 int ret;
482 if (size > 0) {
483 buf = kmemdup(data, size, GFP_KERNEL);
484 if (!buf)
485 return -ENOMEM;
488 /* note: if successful returns number of bytes transferred */
489 ret = usb_control_msg(ar_usb->udev,
490 usb_sndctrlpipe(ar_usb->udev, 0),
491 req,
492 USB_DIR_OUT | USB_TYPE_VENDOR |
493 USB_RECIP_DEVICE, value, index, buf,
494 size, 1000);
496 if (ret < 0) {
497 ath10k_warn(ar, "Failed to submit usb control message: %d\n",
498 ret);
499 kfree(buf);
500 return ret;
503 kfree(buf);
505 return 0;
508 static int ath10k_usb_submit_ctrl_in(struct ath10k *ar,
509 u8 req, u16 value, u16 index, void *data,
510 u32 size)
512 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
513 u8 *buf = NULL;
514 int ret;
516 if (size > 0) {
517 buf = kmalloc(size, GFP_KERNEL);
518 if (!buf)
519 return -ENOMEM;
522 /* note: if successful returns number of bytes transferred */
523 ret = usb_control_msg(ar_usb->udev,
524 usb_rcvctrlpipe(ar_usb->udev, 0),
525 req,
526 USB_DIR_IN | USB_TYPE_VENDOR |
527 USB_RECIP_DEVICE, value, index, buf,
528 size, 2 * HZ);
530 if (ret < 0) {
531 ath10k_warn(ar, "Failed to read usb control message: %d\n",
532 ret);
533 kfree(buf);
534 return ret;
537 memcpy((u8 *)data, buf, size);
539 kfree(buf);
541 return 0;
544 static int ath10k_usb_ctrl_msg_exchange(struct ath10k *ar,
545 u8 req_val, u8 *req_buf, u32 req_len,
546 u8 resp_val, u8 *resp_buf,
547 u32 *resp_len)
549 int ret;
551 /* send command */
552 ret = ath10k_usb_submit_ctrl_out(ar, req_val, 0, 0,
553 req_buf, req_len);
554 if (ret)
555 goto err;
557 /* get response */
558 if (resp_buf) {
559 ret = ath10k_usb_submit_ctrl_in(ar, resp_val, 0, 0,
560 resp_buf, *resp_len);
561 if (ret)
562 goto err;
565 return 0;
566 err:
567 return ret;
570 static int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
571 size_t buf_len)
573 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
574 struct ath10k_usb_ctrl_diag_cmd_read *cmd;
575 u32 resp_len;
576 int ret;
578 if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read))
579 return -EINVAL;
581 cmd = (struct ath10k_usb_ctrl_diag_cmd_read *)ar_usb->diag_cmd_buffer;
582 memset(cmd, 0, sizeof(*cmd));
583 cmd->cmd = ATH10K_USB_CTRL_DIAG_CC_READ;
584 cmd->address = cpu_to_le32(address);
585 resp_len = sizeof(struct ath10k_usb_ctrl_diag_resp_read);
587 ret = ath10k_usb_ctrl_msg_exchange(ar,
588 ATH10K_USB_CONTROL_REQ_DIAG_CMD,
589 (u8 *)cmd,
590 sizeof(*cmd),
591 ATH10K_USB_CONTROL_REQ_DIAG_RESP,
592 ar_usb->diag_resp_buffer, &resp_len);
593 if (ret)
594 return ret;
596 if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read))
597 return -EMSGSIZE;
599 memcpy(buf, ar_usb->diag_resp_buffer,
600 sizeof(struct ath10k_usb_ctrl_diag_resp_read));
602 return 0;
605 static int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address,
606 const void *data, int nbytes)
608 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
609 struct ath10k_usb_ctrl_diag_cmd_write *cmd;
610 int ret;
612 if (nbytes != sizeof(cmd->value))
613 return -EINVAL;
615 cmd = (struct ath10k_usb_ctrl_diag_cmd_write *)ar_usb->diag_cmd_buffer;
616 memset(cmd, 0, sizeof(*cmd));
617 cmd->cmd = cpu_to_le32(ATH10K_USB_CTRL_DIAG_CC_WRITE);
618 cmd->address = cpu_to_le32(address);
619 memcpy(&cmd->value, data, nbytes);
621 ret = ath10k_usb_ctrl_msg_exchange(ar,
622 ATH10K_USB_CONTROL_REQ_DIAG_CMD,
623 (u8 *)cmd,
624 sizeof(*cmd),
625 0, NULL, NULL);
626 if (ret)
627 return ret;
629 return 0;
632 static int ath10k_usb_bmi_exchange_msg(struct ath10k *ar,
633 void *req, u32 req_len,
634 void *resp, u32 *resp_len)
636 int ret;
638 if (req) {
639 ret = ath10k_usb_submit_ctrl_out(ar,
640 ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD,
641 0, 0, req, req_len);
642 if (ret) {
643 ath10k_warn(ar,
644 "unable to send the bmi data to the device: %d\n",
645 ret);
646 return ret;
650 if (resp) {
651 ret = ath10k_usb_submit_ctrl_in(ar,
652 ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP,
653 0, 0, resp, *resp_len);
654 if (ret) {
655 ath10k_warn(ar,
656 "Unable to read the bmi data from the device: %d\n",
657 ret);
658 return ret;
662 return 0;
665 static void ath10k_usb_hif_get_default_pipe(struct ath10k *ar,
666 u8 *ul_pipe, u8 *dl_pipe)
668 *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
669 *dl_pipe = ATH10K_USB_PIPE_RX_CTRL;
672 static int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id,
673 u8 *ul_pipe, u8 *dl_pipe)
675 switch (svc_id) {
676 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
677 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
678 *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
679 /* due to large control packets, shift to data pipe */
680 *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
681 break;
682 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
683 *ul_pipe = ATH10K_USB_PIPE_TX_DATA_LP;
684 /* Disable rxdata2 directly, it will be enabled
685 * if FW enable rxdata2
687 *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
688 break;
689 default:
690 return -EPERM;
693 return 0;
696 /* This op is currently only used by htc_wait_target if the HTC ready
697 * message times out. It is not applicable for USB since there is nothing
698 * we can do if the HTC ready message does not arrive in time.
699 * TODO: Make this op non mandatory by introducing a NULL check in the
700 * hif op wrapper.
702 static void ath10k_usb_hif_send_complete_check(struct ath10k *ar,
703 u8 pipe, int force)
707 static int ath10k_usb_hif_power_up(struct ath10k *ar,
708 enum ath10k_firmware_mode fw_mode)
710 return 0;
713 static void ath10k_usb_hif_power_down(struct ath10k *ar)
715 ath10k_usb_flush_all(ar);
718 #ifdef CONFIG_PM
720 static int ath10k_usb_hif_suspend(struct ath10k *ar)
722 return -EOPNOTSUPP;
725 static int ath10k_usb_hif_resume(struct ath10k *ar)
727 return -EOPNOTSUPP;
729 #endif
731 static const struct ath10k_hif_ops ath10k_usb_hif_ops = {
732 .tx_sg = ath10k_usb_hif_tx_sg,
733 .diag_read = ath10k_usb_hif_diag_read,
734 .diag_write = ath10k_usb_hif_diag_write,
735 .exchange_bmi_msg = ath10k_usb_bmi_exchange_msg,
736 .start = ath10k_usb_hif_start,
737 .stop = ath10k_usb_hif_stop,
738 .map_service_to_pipe = ath10k_usb_hif_map_service_to_pipe,
739 .get_default_pipe = ath10k_usb_hif_get_default_pipe,
740 .send_complete_check = ath10k_usb_hif_send_complete_check,
741 .get_free_queue_number = ath10k_usb_hif_get_free_queue_number,
742 .power_up = ath10k_usb_hif_power_up,
743 .power_down = ath10k_usb_hif_power_down,
744 #ifdef CONFIG_PM
745 .suspend = ath10k_usb_hif_suspend,
746 .resume = ath10k_usb_hif_resume,
747 #endif
750 static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
752 u8 pipe_num = ATH10K_USB_PIPE_INVALID;
754 switch (ep_address) {
755 case ATH10K_USB_EP_ADDR_APP_CTRL_IN:
756 pipe_num = ATH10K_USB_PIPE_RX_CTRL;
757 *urb_count = RX_URB_COUNT;
758 break;
759 case ATH10K_USB_EP_ADDR_APP_DATA_IN:
760 pipe_num = ATH10K_USB_PIPE_RX_DATA;
761 *urb_count = RX_URB_COUNT;
762 break;
763 case ATH10K_USB_EP_ADDR_APP_INT_IN:
764 pipe_num = ATH10K_USB_PIPE_RX_INT;
765 *urb_count = RX_URB_COUNT;
766 break;
767 case ATH10K_USB_EP_ADDR_APP_DATA2_IN:
768 pipe_num = ATH10K_USB_PIPE_RX_DATA2;
769 *urb_count = RX_URB_COUNT;
770 break;
771 case ATH10K_USB_EP_ADDR_APP_CTRL_OUT:
772 pipe_num = ATH10K_USB_PIPE_TX_CTRL;
773 *urb_count = TX_URB_COUNT;
774 break;
775 case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT:
776 pipe_num = ATH10K_USB_PIPE_TX_DATA_LP;
777 *urb_count = TX_URB_COUNT;
778 break;
779 case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT:
780 pipe_num = ATH10K_USB_PIPE_TX_DATA_MP;
781 *urb_count = TX_URB_COUNT;
782 break;
783 case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT:
784 pipe_num = ATH10K_USB_PIPE_TX_DATA_HP;
785 *urb_count = TX_URB_COUNT;
786 break;
787 default:
788 /* note: there may be endpoints not currently used */
789 break;
792 return pipe_num;
795 static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
796 struct ath10k_usb_pipe *pipe,
797 int urb_cnt)
799 struct ath10k_urb_context *urb_context;
800 int i;
802 INIT_LIST_HEAD(&pipe->urb_list_head);
803 init_usb_anchor(&pipe->urb_submitted);
805 for (i = 0; i < urb_cnt; i++) {
806 urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
807 if (!urb_context)
808 return -ENOMEM;
810 urb_context->pipe = pipe;
812 /* we are only allocate the urb contexts here, the actual URB
813 * is allocated from the kernel as needed to do a transaction
815 pipe->urb_alloc++;
816 ath10k_usb_free_urb_to_pipe(pipe, urb_context);
819 ath10k_dbg(ar, ATH10K_DBG_USB,
820 "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n",
821 pipe->logical_pipe_num, pipe->usb_pipe_handle,
822 pipe->urb_alloc);
824 return 0;
827 static int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
828 struct usb_interface *interface)
830 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
831 struct usb_host_interface *iface_desc = interface->cur_altsetting;
832 struct usb_endpoint_descriptor *endpoint;
833 struct ath10k_usb_pipe *pipe;
834 int ret, i, urbcount;
835 u8 pipe_num;
837 ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
839 /* walk decriptors and setup pipes */
840 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
841 endpoint = &iface_desc->endpoint[i].desc;
843 if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
844 ath10k_dbg(ar, ATH10K_DBG_USB,
845 "usb %s bulk ep 0x%2.2x maxpktsz %d\n",
846 ATH10K_USB_IS_DIR_IN
847 (endpoint->bEndpointAddress) ?
848 "rx" : "tx", endpoint->bEndpointAddress,
849 le16_to_cpu(endpoint->wMaxPacketSize));
850 } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
851 ath10k_dbg(ar, ATH10K_DBG_USB,
852 "usb %s int 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 } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
859 /* TODO for ISO */
860 ath10k_dbg(ar, ATH10K_DBG_USB,
861 "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
862 ATH10K_USB_IS_DIR_IN
863 (endpoint->bEndpointAddress) ?
864 "rx" : "tx", endpoint->bEndpointAddress,
865 le16_to_cpu(endpoint->wMaxPacketSize),
866 endpoint->bInterval);
868 urbcount = 0;
870 pipe_num =
871 ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
872 &urbcount);
873 if (pipe_num == ATH10K_USB_PIPE_INVALID)
874 continue;
876 pipe = &ar_usb->pipes[pipe_num];
877 if (pipe->ar_usb)
878 /* hmmm..pipe was already setup */
879 continue;
881 pipe->ar_usb = ar_usb;
882 pipe->logical_pipe_num = pipe_num;
883 pipe->ep_address = endpoint->bEndpointAddress;
884 pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
886 if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
887 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
888 pipe->usb_pipe_handle =
889 usb_rcvbulkpipe(ar_usb->udev,
890 pipe->ep_address);
891 } else {
892 pipe->usb_pipe_handle =
893 usb_sndbulkpipe(ar_usb->udev,
894 pipe->ep_address);
896 } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
897 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
898 pipe->usb_pipe_handle =
899 usb_rcvintpipe(ar_usb->udev,
900 pipe->ep_address);
901 } else {
902 pipe->usb_pipe_handle =
903 usb_sndintpipe(ar_usb->udev,
904 pipe->ep_address);
906 } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
907 /* TODO for ISO */
908 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
909 pipe->usb_pipe_handle =
910 usb_rcvisocpipe(ar_usb->udev,
911 pipe->ep_address);
912 } else {
913 pipe->usb_pipe_handle =
914 usb_sndisocpipe(ar_usb->udev,
915 pipe->ep_address);
919 pipe->ep_desc = endpoint;
921 if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
922 pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
924 ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
925 if (ret)
926 return ret;
929 return 0;
932 static int ath10k_usb_create(struct ath10k *ar,
933 struct usb_interface *interface)
935 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
936 struct usb_device *dev = interface_to_usbdev(interface);
937 struct ath10k_usb_pipe *pipe;
938 int ret, i;
940 usb_set_intfdata(interface, ar_usb);
941 spin_lock_init(&ar_usb->cs_lock);
942 ar_usb->udev = dev;
943 ar_usb->interface = interface;
945 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
946 pipe = &ar_usb->pipes[i];
947 INIT_WORK(&pipe->io_complete_work,
948 ath10k_usb_io_comp_work);
949 skb_queue_head_init(&pipe->io_comp_queue);
952 ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL);
953 if (!ar_usb->diag_cmd_buffer) {
954 ret = -ENOMEM;
955 goto err;
958 ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP,
959 GFP_KERNEL);
960 if (!ar_usb->diag_resp_buffer) {
961 ret = -ENOMEM;
962 goto err;
965 ret = ath10k_usb_setup_pipe_resources(ar, interface);
966 if (ret)
967 goto err;
969 return 0;
971 err:
972 ath10k_usb_destroy(ar);
973 return ret;
976 /* ath10k usb driver registered functions */
977 static int ath10k_usb_probe(struct usb_interface *interface,
978 const struct usb_device_id *id)
980 struct ath10k *ar;
981 struct ath10k_usb *ar_usb;
982 struct usb_device *dev = interface_to_usbdev(interface);
983 int ret, vendor_id, product_id;
984 enum ath10k_hw_rev hw_rev;
985 struct ath10k_bus_params bus_params = {};
987 /* Assumption: All USB based chipsets (so far) are QCA9377 based.
988 * If there will be newer chipsets that does not use the hw reg
989 * setup as defined in qca6174_regs and qca6174_values, this
990 * assumption is no longer valid and hw_rev must be setup differently
991 * depending on chipset.
993 hw_rev = ATH10K_HW_QCA9377;
995 ar = ath10k_core_create(sizeof(*ar_usb), &dev->dev, ATH10K_BUS_USB,
996 hw_rev, &ath10k_usb_hif_ops);
997 if (!ar) {
998 dev_err(&dev->dev, "failed to allocate core\n");
999 return -ENOMEM;
1002 usb_get_dev(dev);
1003 vendor_id = le16_to_cpu(dev->descriptor.idVendor);
1004 product_id = le16_to_cpu(dev->descriptor.idProduct);
1006 ath10k_dbg(ar, ATH10K_DBG_BOOT,
1007 "usb new func vendor 0x%04x product 0x%04x\n",
1008 vendor_id, product_id);
1010 ar_usb = ath10k_usb_priv(ar);
1011 ret = ath10k_usb_create(ar, interface);
1012 ar_usb->ar = ar;
1014 ar->dev_id = product_id;
1015 ar->id.vendor = vendor_id;
1016 ar->id.device = product_id;
1018 bus_params.dev_type = ATH10K_DEV_TYPE_HL;
1019 /* TODO: don't know yet how to get chip_id with USB */
1020 bus_params.chip_id = 0;
1021 ret = ath10k_core_register(ar, &bus_params);
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");