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>
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
;
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
);
59 spin_unlock_irqrestore(&pipe
->ar_usb
->cs_lock
, flags
);
64 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe
*pipe
,
65 struct ath10k_urb_context
*urb_context
)
69 spin_lock_irqsave(&pipe
->ar_usb
->cs_lock
, flags
);
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
;
91 /* nothing allocated for this pipe */
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
);
108 urb_context
= ath10k_usb_alloc_urb_from_pipe(pipe
);
117 static void ath10k_usb_cleanup_pipe_resources(struct ath10k
*ar
)
119 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
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
;
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
,
141 if (urb
->status
!= 0) {
143 switch (urb
->status
) {
147 /* no need to spew these errors when device
148 * removed or urb killed due to driver shutdown
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
);
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
);
176 ath10k_usb_cleanup_recv_urb(urb_context
);
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
;
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
;
216 urb_context
= ath10k_usb_alloc_urb_from_pipe(recv_pipe
);
220 urb_context
->skb
= dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE
);
221 if (!urb_context
->skb
)
224 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
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
);
245 ath10k_dbg(ar
, ATH10K_DBG_USB_BULK
,
246 "usb bulk recv failed: %d\n",
248 usb_unanchor_urb(urb
);
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
);
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
;
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
);
314 payload_len
= le16_to_cpu(htc_hdr
->len
);
316 ath10k_warn(ar
, "zero length frame received, firmware crashed?\n");
320 if (payload_len
< htc_hdr
->trailer_len
) {
321 ath10k_warn(ar
, "malformed frame received, firmware crashed?\n");
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
,
331 htc_hdr
->trailer_len
,
338 if (is_trailer_only_msg(htc_hdr
))
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... */
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
,
362 struct ath10k
*ar
= pipe
->ar_usb
->ar
;
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
);
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
)
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;
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
;
415 for (i
= 0; i
< n_items
; i
++) {
416 urb_context
= ath10k_usb_alloc_urb_from_pipe(pipe
);
422 skb
= items
[i
].transfer_context
;
423 urb_context
->skb
= skb
;
425 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
428 goto err_free_urb_to_pipe
;
431 usb_fill_bulk_urb(urb
,
433 pipe
->usb_pipe_handle
,
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
);
446 ath10k_dbg(ar
, ATH10K_DBG_USB_BULK
,
447 "usb bulk transmit failed: %d\n", ret
);
448 usb_unanchor_urb(urb
);
450 goto err_free_urb_to_pipe
;
458 err_free_urb_to_pipe
:
459 ath10k_usb_free_urb_to_pipe(urb_context
->pipe
, urb_context
);
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
,
480 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
485 buf
= kmemdup(data
, size
, GFP_KERNEL
);
490 /* note: if successful returns number of bytes transferred */
491 ret
= usb_control_msg(ar_usb
->udev
,
492 usb_sndctrlpipe(ar_usb
->udev
, 0),
494 USB_DIR_OUT
| USB_TYPE_VENDOR
|
495 USB_RECIP_DEVICE
, value
, index
, buf
,
499 ath10k_warn(ar
, "Failed to submit usb control message: %d\n",
510 static int ath10k_usb_submit_ctrl_in(struct ath10k
*ar
,
511 u8 req
, u16 value
, u16 index
, void *data
,
514 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
519 buf
= kmalloc(size
, GFP_KERNEL
);
524 /* note: if successful returns number of bytes transferred */
525 ret
= usb_control_msg(ar_usb
->udev
,
526 usb_rcvctrlpipe(ar_usb
->udev
, 0),
528 USB_DIR_IN
| USB_TYPE_VENDOR
|
529 USB_RECIP_DEVICE
, value
, index
, buf
,
533 ath10k_warn(ar
, "Failed to read usb control message: %d\n",
539 memcpy((u8
*)data
, buf
, size
);
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
,
554 ret
= ath10k_usb_submit_ctrl_out(ar
, req_val
, 0, 0,
561 ret
= ath10k_usb_submit_ctrl_in(ar
, resp_val
, 0, 0,
562 resp_buf
, *resp_len
);
572 static int ath10k_usb_hif_diag_read(struct ath10k
*ar
, u32 address
, void *buf
,
575 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
576 struct ath10k_usb_ctrl_diag_cmd_read
*cmd
;
580 if (buf_len
< sizeof(struct ath10k_usb_ctrl_diag_resp_read
))
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
,
593 ATH10K_USB_CONTROL_REQ_DIAG_RESP
,
594 ar_usb
->diag_resp_buffer
, &resp_len
);
598 if (resp_len
!= sizeof(struct ath10k_usb_ctrl_diag_resp_read
))
601 memcpy(buf
, ar_usb
->diag_resp_buffer
,
602 sizeof(struct ath10k_usb_ctrl_diag_resp_read
));
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
;
614 if (nbytes
!= sizeof(cmd
->value
))
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
,
634 static int ath10k_usb_bmi_exchange_msg(struct ath10k
*ar
,
635 void *req
, u32 req_len
,
636 void *resp
, u32
*resp_len
)
641 ret
= ath10k_usb_submit_ctrl_out(ar
,
642 ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD
,
646 "unable to send the bmi data to the device: %d\n",
653 ret
= ath10k_usb_submit_ctrl_in(ar
,
654 ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP
,
655 0, 0, resp
, *resp_len
);
658 "Unable to read the bmi data from the device: %d\n",
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
)
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
;
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
;
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
704 static void ath10k_usb_hif_send_complete_check(struct ath10k
*ar
,
709 static int ath10k_usb_hif_power_up(struct ath10k
*ar
)
714 static void ath10k_usb_hif_power_down(struct ath10k
*ar
)
716 ath10k_usb_flush_all(ar
);
721 static int ath10k_usb_hif_suspend(struct ath10k
*ar
)
726 static int ath10k_usb_hif_resume(struct ath10k
*ar
)
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
,
746 .suspend
= ath10k_usb_hif_suspend
,
747 .resume
= ath10k_usb_hif_resume
,
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
;
760 case ATH10K_USB_EP_ADDR_APP_DATA_IN
:
761 pipe_num
= ATH10K_USB_PIPE_RX_DATA
;
762 *urb_count
= RX_URB_COUNT
;
764 case ATH10K_USB_EP_ADDR_APP_INT_IN
:
765 pipe_num
= ATH10K_USB_PIPE_RX_INT
;
766 *urb_count
= RX_URB_COUNT
;
768 case ATH10K_USB_EP_ADDR_APP_DATA2_IN
:
769 pipe_num
= ATH10K_USB_PIPE_RX_DATA2
;
770 *urb_count
= RX_URB_COUNT
;
772 case ATH10K_USB_EP_ADDR_APP_CTRL_OUT
:
773 pipe_num
= ATH10K_USB_PIPE_TX_CTRL
;
774 *urb_count
= TX_URB_COUNT
;
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
;
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
;
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
;
789 /* note: there may be endpoints not currently used */
796 static int ath10k_usb_alloc_pipe_resources(struct ath10k
*ar
,
797 struct ath10k_usb_pipe
*pipe
,
800 struct ath10k_urb_context
*urb_context
;
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
);
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
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
,
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
;
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",
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",
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
)) {
861 ath10k_dbg(ar
, ATH10K_DBG_USB
,
862 "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
864 (endpoint
->bEndpointAddress
) ?
865 "rx" : "tx", endpoint
->bEndpointAddress
,
866 le16_to_cpu(endpoint
->wMaxPacketSize
),
867 endpoint
->bInterval
);
872 ath10k_usb_get_logical_pipe_num(endpoint
->bEndpointAddress
,
874 if (pipe_num
== ATH10K_USB_PIPE_INVALID
)
877 pipe
= &ar_usb
->pipes
[pipe_num
];
879 /* hmmm..pipe was already setup */
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
,
893 pipe
->usb_pipe_handle
=
894 usb_sndbulkpipe(ar_usb
->udev
,
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
,
903 pipe
->usb_pipe_handle
=
904 usb_sndintpipe(ar_usb
->udev
,
907 } else if (ATH10K_USB_IS_ISOC_EP(endpoint
->bmAttributes
)) {
909 if (ATH10K_USB_IS_DIR_IN(pipe
->ep_address
)) {
910 pipe
->usb_pipe_handle
=
911 usb_rcvisocpipe(ar_usb
->udev
,
914 pipe
->usb_pipe_handle
=
915 usb_sndisocpipe(ar_usb
->udev
,
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
);
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
;
941 usb_set_intfdata(interface
, ar_usb
);
942 spin_lock_init(&ar_usb
->cs_lock
);
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
) {
959 ar_usb
->diag_resp_buffer
= kzalloc(ATH10K_USB_MAX_DIAG_RESP
,
961 if (!ar_usb
->diag_resp_buffer
) {
966 ret
= ath10k_usb_setup_pipe_resources(ar
, interface
);
973 ath10k_usb_destroy(ar
);
977 /* ath10k usb driver registered functions */
978 static int ath10k_usb_probe(struct usb_interface
*interface
,
979 const struct usb_device_id
*id
)
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
;
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
);
999 dev_err(&dev
->dev
, "failed to allocate core\n");
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
);
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 */
1021 ret
= ath10k_core_register(ar
, chip_id
);
1023 ath10k_warn(ar
, "failed to register driver core: %d\n", ret
);
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");
1033 ath10k_core_destroy(ar
);
1040 static void ath10k_usb_remove(struct usb_interface
*interface
)
1042 struct ath10k_usb
*ar_usb
;
1044 ar_usb
= usb_get_intfdata(interface
);
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
);
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
);
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
]);
1078 #define ath10k_usb_pm_suspend NULL
1079 #define ath10k_usb_pm_resume NULL
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");