1 // SPDX-License-Identifier: ISC
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>
8 #include <linux/module.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
;
41 /* bail if this pipe is not initialized */
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
);
52 spin_unlock_irqrestore(&pipe
->ar_usb
->cs_lock
, flags
);
57 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe
*pipe
,
58 struct ath10k_urb_context
*urb_context
)
62 /* bail if this pipe is not initialized */
66 spin_lock_irqsave(&pipe
->ar_usb
->cs_lock
, flags
);
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
;
88 /* nothing allocated for this pipe */
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
);
105 urb_context
= ath10k_usb_alloc_urb_from_pipe(pipe
);
114 static void ath10k_usb_cleanup_pipe_resources(struct ath10k
*ar
)
116 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
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
;
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
,
138 if (urb
->status
!= 0) {
140 switch (urb
->status
) {
144 /* no need to spew these errors when device
145 * removed or urb killed due to driver shutdown
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
);
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
);
173 ath10k_usb_cleanup_recv_urb(urb_context
);
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
;
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
;
213 urb_context
= ath10k_usb_alloc_urb_from_pipe(recv_pipe
);
217 urb_context
->skb
= dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE
);
218 if (!urb_context
->skb
)
221 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
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
);
242 ath10k_dbg(ar
, ATH10K_DBG_USB_BULK
,
243 "usb bulk recv failed: %d\n",
245 usb_unanchor_urb(urb
);
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
);
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
;
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
);
311 payload_len
= le16_to_cpu(htc_hdr
->len
);
313 ath10k_warn(ar
, "zero length frame received, firmware crashed?\n");
317 if (payload_len
< htc_hdr
->trailer_len
) {
318 ath10k_warn(ar
, "malformed frame received, firmware crashed?\n");
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
,
328 htc_hdr
->trailer_len
,
335 if (is_trailer_only_msg(htc_hdr
))
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 if (test_bit(ATH10K_FLAG_CORE_REGISTERED
, &ar
->dev_flags
)) {
350 napi_schedule(&ar
->napi
);
360 static void ath10k_usb_io_comp_work(struct work_struct
*work
)
362 struct ath10k_usb_pipe
*pipe
= container_of(work
,
363 struct ath10k_usb_pipe
,
365 struct ath10k
*ar
= pipe
->ar_usb
->ar
;
368 while ((skb
= skb_dequeue(&pipe
->io_comp_queue
))) {
369 if (pipe
->flags
& ATH10K_USB_PIPE_FLAG_TX
)
370 ath10k_usb_tx_complete(ar
, skb
);
372 ath10k_usb_rx_complete(ar
, skb
);
376 #define ATH10K_USB_MAX_DIAG_CMD (sizeof(struct ath10k_usb_ctrl_diag_cmd_write))
377 #define ATH10K_USB_MAX_DIAG_RESP (sizeof(struct ath10k_usb_ctrl_diag_resp_read))
379 static void ath10k_usb_destroy(struct ath10k
*ar
)
381 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
383 ath10k_usb_flush_all(ar
);
384 ath10k_usb_cleanup_pipe_resources(ar
);
385 usb_set_intfdata(ar_usb
->interface
, NULL
);
387 kfree(ar_usb
->diag_cmd_buffer
);
388 kfree(ar_usb
->diag_resp_buffer
);
391 static int ath10k_usb_hif_start(struct ath10k
*ar
)
394 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
396 ath10k_core_napi_enable(ar
);
397 ath10k_usb_start_recv_pipes(ar
);
399 /* set the TX resource avail threshold for each TX pipe */
400 for (i
= ATH10K_USB_PIPE_TX_CTRL
;
401 i
<= ATH10K_USB_PIPE_TX_DATA_HP
; i
++) {
402 ar_usb
->pipes
[i
].urb_cnt_thresh
=
403 ar_usb
->pipes
[i
].urb_alloc
/ 2;
409 static int ath10k_usb_hif_tx_sg(struct ath10k
*ar
, u8 pipe_id
,
410 struct ath10k_hif_sg_item
*items
, int n_items
)
412 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
413 struct ath10k_usb_pipe
*pipe
= &ar_usb
->pipes
[pipe_id
];
414 struct ath10k_urb_context
*urb_context
;
419 for (i
= 0; i
< n_items
; i
++) {
420 urb_context
= ath10k_usb_alloc_urb_from_pipe(pipe
);
426 skb
= items
[i
].transfer_context
;
427 urb_context
->skb
= skb
;
429 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
432 goto err_free_urb_to_pipe
;
435 usb_fill_bulk_urb(urb
,
437 pipe
->usb_pipe_handle
,
440 ath10k_usb_transmit_complete
, urb_context
);
442 if (!(skb
->len
% pipe
->max_packet_size
)) {
443 /* hit a max packet boundary on this pipe */
444 urb
->transfer_flags
|= URB_ZERO_PACKET
;
447 usb_anchor_urb(urb
, &pipe
->urb_submitted
);
448 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
450 ath10k_dbg(ar
, ATH10K_DBG_USB_BULK
,
451 "usb bulk transmit failed: %d\n", ret
);
452 usb_unanchor_urb(urb
);
455 goto err_free_urb_to_pipe
;
463 err_free_urb_to_pipe
:
464 ath10k_usb_free_urb_to_pipe(urb_context
->pipe
, urb_context
);
469 static void ath10k_usb_hif_stop(struct ath10k
*ar
)
471 ath10k_usb_flush_all(ar
);
472 ath10k_core_napi_sync_disable(ar
);
475 static u16
ath10k_usb_hif_get_free_queue_number(struct ath10k
*ar
, u8 pipe_id
)
477 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
479 return ar_usb
->pipes
[pipe_id
].urb_cnt
;
482 static int ath10k_usb_submit_ctrl_out(struct ath10k
*ar
,
483 u8 req
, u16 value
, u16 index
, void *data
,
486 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
491 buf
= kmemdup(data
, size
, GFP_KERNEL
);
496 /* note: if successful returns number of bytes transferred */
497 ret
= usb_control_msg(ar_usb
->udev
,
498 usb_sndctrlpipe(ar_usb
->udev
, 0),
500 USB_DIR_OUT
| USB_TYPE_VENDOR
|
501 USB_RECIP_DEVICE
, value
, index
, buf
,
505 ath10k_warn(ar
, "Failed to submit usb control message: %d\n",
516 static int ath10k_usb_submit_ctrl_in(struct ath10k
*ar
,
517 u8 req
, u16 value
, u16 index
, void *data
,
520 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
525 buf
= kmalloc(size
, GFP_KERNEL
);
530 /* note: if successful returns number of bytes transferred */
531 ret
= usb_control_msg(ar_usb
->udev
,
532 usb_rcvctrlpipe(ar_usb
->udev
, 0),
534 USB_DIR_IN
| USB_TYPE_VENDOR
|
535 USB_RECIP_DEVICE
, value
, index
, buf
,
539 ath10k_warn(ar
, "Failed to read usb control message: %d\n",
545 memcpy((u8
*)data
, buf
, size
);
552 static int ath10k_usb_ctrl_msg_exchange(struct ath10k
*ar
,
553 u8 req_val
, u8
*req_buf
, u32 req_len
,
554 u8 resp_val
, u8
*resp_buf
,
560 ret
= ath10k_usb_submit_ctrl_out(ar
, req_val
, 0, 0,
567 ret
= ath10k_usb_submit_ctrl_in(ar
, resp_val
, 0, 0,
568 resp_buf
, *resp_len
);
578 static int ath10k_usb_hif_diag_read(struct ath10k
*ar
, u32 address
, void *buf
,
581 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
582 struct ath10k_usb_ctrl_diag_cmd_read
*cmd
;
586 if (buf_len
< sizeof(struct ath10k_usb_ctrl_diag_resp_read
))
589 cmd
= (struct ath10k_usb_ctrl_diag_cmd_read
*)ar_usb
->diag_cmd_buffer
;
590 memset(cmd
, 0, sizeof(*cmd
));
591 cmd
->cmd
= ATH10K_USB_CTRL_DIAG_CC_READ
;
592 cmd
->address
= cpu_to_le32(address
);
593 resp_len
= sizeof(struct ath10k_usb_ctrl_diag_resp_read
);
595 ret
= ath10k_usb_ctrl_msg_exchange(ar
,
596 ATH10K_USB_CONTROL_REQ_DIAG_CMD
,
599 ATH10K_USB_CONTROL_REQ_DIAG_RESP
,
600 ar_usb
->diag_resp_buffer
, &resp_len
);
604 if (resp_len
!= sizeof(struct ath10k_usb_ctrl_diag_resp_read
))
607 memcpy(buf
, ar_usb
->diag_resp_buffer
,
608 sizeof(struct ath10k_usb_ctrl_diag_resp_read
));
613 static int ath10k_usb_hif_diag_write(struct ath10k
*ar
, u32 address
,
614 const void *data
, int nbytes
)
616 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
617 struct ath10k_usb_ctrl_diag_cmd_write
*cmd
;
620 if (nbytes
!= sizeof(cmd
->value
))
623 cmd
= (struct ath10k_usb_ctrl_diag_cmd_write
*)ar_usb
->diag_cmd_buffer
;
624 memset(cmd
, 0, sizeof(*cmd
));
625 cmd
->cmd
= cpu_to_le32(ATH10K_USB_CTRL_DIAG_CC_WRITE
);
626 cmd
->address
= cpu_to_le32(address
);
627 memcpy(&cmd
->value
, data
, nbytes
);
629 ret
= ath10k_usb_ctrl_msg_exchange(ar
,
630 ATH10K_USB_CONTROL_REQ_DIAG_CMD
,
640 static int ath10k_usb_bmi_exchange_msg(struct ath10k
*ar
,
641 void *req
, u32 req_len
,
642 void *resp
, u32
*resp_len
)
647 ret
= ath10k_usb_submit_ctrl_out(ar
,
648 ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD
,
652 "unable to send the bmi data to the device: %d\n",
659 ret
= ath10k_usb_submit_ctrl_in(ar
,
660 ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP
,
661 0, 0, resp
, *resp_len
);
664 "Unable to read the bmi data from the device: %d\n",
673 static void ath10k_usb_hif_get_default_pipe(struct ath10k
*ar
,
674 u8
*ul_pipe
, u8
*dl_pipe
)
676 *ul_pipe
= ATH10K_USB_PIPE_TX_CTRL
;
677 *dl_pipe
= ATH10K_USB_PIPE_RX_CTRL
;
680 static int ath10k_usb_hif_map_service_to_pipe(struct ath10k
*ar
, u16 svc_id
,
681 u8
*ul_pipe
, u8
*dl_pipe
)
684 case ATH10K_HTC_SVC_ID_RSVD_CTRL
:
685 case ATH10K_HTC_SVC_ID_WMI_CONTROL
:
686 *ul_pipe
= ATH10K_USB_PIPE_TX_CTRL
;
687 /* due to large control packets, shift to data pipe */
688 *dl_pipe
= ATH10K_USB_PIPE_RX_DATA
;
690 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG
:
691 *ul_pipe
= ATH10K_USB_PIPE_TX_DATA_LP
;
692 /* Disable rxdata2 directly, it will be enabled
693 * if FW enable rxdata2
695 *dl_pipe
= ATH10K_USB_PIPE_RX_DATA
;
704 static int ath10k_usb_hif_power_up(struct ath10k
*ar
,
705 enum ath10k_firmware_mode fw_mode
)
710 static void ath10k_usb_hif_power_down(struct ath10k
*ar
)
712 ath10k_usb_flush_all(ar
);
717 static int ath10k_usb_hif_suspend(struct ath10k
*ar
)
722 static int ath10k_usb_hif_resume(struct ath10k
*ar
)
728 static const struct ath10k_hif_ops ath10k_usb_hif_ops
= {
729 .tx_sg
= ath10k_usb_hif_tx_sg
,
730 .diag_read
= ath10k_usb_hif_diag_read
,
731 .diag_write
= ath10k_usb_hif_diag_write
,
732 .exchange_bmi_msg
= ath10k_usb_bmi_exchange_msg
,
733 .start
= ath10k_usb_hif_start
,
734 .stop
= ath10k_usb_hif_stop
,
735 .map_service_to_pipe
= ath10k_usb_hif_map_service_to_pipe
,
736 .get_default_pipe
= ath10k_usb_hif_get_default_pipe
,
737 .get_free_queue_number
= ath10k_usb_hif_get_free_queue_number
,
738 .power_up
= ath10k_usb_hif_power_up
,
739 .power_down
= ath10k_usb_hif_power_down
,
741 .suspend
= ath10k_usb_hif_suspend
,
742 .resume
= ath10k_usb_hif_resume
,
746 static u8
ath10k_usb_get_logical_pipe_num(u8 ep_address
, int *urb_count
)
748 u8 pipe_num
= ATH10K_USB_PIPE_INVALID
;
750 switch (ep_address
) {
751 case ATH10K_USB_EP_ADDR_APP_CTRL_IN
:
752 pipe_num
= ATH10K_USB_PIPE_RX_CTRL
;
753 *urb_count
= RX_URB_COUNT
;
755 case ATH10K_USB_EP_ADDR_APP_DATA_IN
:
756 pipe_num
= ATH10K_USB_PIPE_RX_DATA
;
757 *urb_count
= RX_URB_COUNT
;
759 case ATH10K_USB_EP_ADDR_APP_INT_IN
:
760 pipe_num
= ATH10K_USB_PIPE_RX_INT
;
761 *urb_count
= RX_URB_COUNT
;
763 case ATH10K_USB_EP_ADDR_APP_DATA2_IN
:
764 pipe_num
= ATH10K_USB_PIPE_RX_DATA2
;
765 *urb_count
= RX_URB_COUNT
;
767 case ATH10K_USB_EP_ADDR_APP_CTRL_OUT
:
768 pipe_num
= ATH10K_USB_PIPE_TX_CTRL
;
769 *urb_count
= TX_URB_COUNT
;
771 case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT
:
772 pipe_num
= ATH10K_USB_PIPE_TX_DATA_LP
;
773 *urb_count
= TX_URB_COUNT
;
775 case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT
:
776 pipe_num
= ATH10K_USB_PIPE_TX_DATA_MP
;
777 *urb_count
= TX_URB_COUNT
;
779 case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT
:
780 pipe_num
= ATH10K_USB_PIPE_TX_DATA_HP
;
781 *urb_count
= TX_URB_COUNT
;
784 /* note: there may be endpoints not currently used */
791 static int ath10k_usb_alloc_pipe_resources(struct ath10k
*ar
,
792 struct ath10k_usb_pipe
*pipe
,
795 struct ath10k_urb_context
*urb_context
;
798 INIT_LIST_HEAD(&pipe
->urb_list_head
);
799 init_usb_anchor(&pipe
->urb_submitted
);
801 for (i
= 0; i
< urb_cnt
; i
++) {
802 urb_context
= kzalloc(sizeof(*urb_context
), GFP_KERNEL
);
806 urb_context
->pipe
= pipe
;
808 /* we are only allocate the urb contexts here, the actual URB
809 * is allocated from the kernel as needed to do a transaction
812 ath10k_usb_free_urb_to_pipe(pipe
, urb_context
);
815 ath10k_dbg(ar
, ATH10K_DBG_USB
,
816 "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n",
817 pipe
->logical_pipe_num
, pipe
->usb_pipe_handle
,
823 static int ath10k_usb_setup_pipe_resources(struct ath10k
*ar
,
824 struct usb_interface
*interface
)
826 struct ath10k_usb
*ar_usb
= ath10k_usb_priv(ar
);
827 struct usb_host_interface
*iface_desc
= interface
->cur_altsetting
;
828 struct usb_endpoint_descriptor
*endpoint
;
829 struct ath10k_usb_pipe
*pipe
;
830 int ret
, i
, urbcount
;
833 ath10k_dbg(ar
, ATH10K_DBG_USB
, "usb setting up pipes using interface\n");
835 /* walk descriptors and setup pipes */
836 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
837 endpoint
= &iface_desc
->endpoint
[i
].desc
;
839 if (ATH10K_USB_IS_BULK_EP(endpoint
->bmAttributes
)) {
840 ath10k_dbg(ar
, ATH10K_DBG_USB
,
841 "usb %s bulk ep 0x%2.2x maxpktsz %d\n",
843 (endpoint
->bEndpointAddress
) ?
844 "rx" : "tx", endpoint
->bEndpointAddress
,
845 le16_to_cpu(endpoint
->wMaxPacketSize
));
846 } else if (ATH10K_USB_IS_INT_EP(endpoint
->bmAttributes
)) {
847 ath10k_dbg(ar
, ATH10K_DBG_USB
,
848 "usb %s int ep 0x%2.2x maxpktsz %d interval %d\n",
850 (endpoint
->bEndpointAddress
) ?
851 "rx" : "tx", endpoint
->bEndpointAddress
,
852 le16_to_cpu(endpoint
->wMaxPacketSize
),
853 endpoint
->bInterval
);
854 } else if (ATH10K_USB_IS_ISOC_EP(endpoint
->bmAttributes
)) {
856 ath10k_dbg(ar
, ATH10K_DBG_USB
,
857 "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
859 (endpoint
->bEndpointAddress
) ?
860 "rx" : "tx", endpoint
->bEndpointAddress
,
861 le16_to_cpu(endpoint
->wMaxPacketSize
),
862 endpoint
->bInterval
);
865 /* Ignore broken descriptors. */
866 if (usb_endpoint_maxp(endpoint
) == 0)
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 static int ath10k_usb_napi_poll(struct napi_struct
*ctx
, int budget
)
979 struct ath10k
*ar
= container_of(ctx
, struct ath10k
, napi
);
982 done
= ath10k_htt_rx_hl_indication(ar
, budget
);
983 ath10k_dbg(ar
, ATH10K_DBG_USB
, "napi poll: done: %d, budget:%d\n", done
, budget
);
986 napi_complete_done(ctx
, done
);
991 /* ath10k usb driver registered functions */
992 static int ath10k_usb_probe(struct usb_interface
*interface
,
993 const struct usb_device_id
*id
)
996 struct ath10k_usb
*ar_usb
;
997 struct usb_device
*dev
= interface_to_usbdev(interface
);
998 int ret
, vendor_id
, product_id
;
999 enum ath10k_hw_rev hw_rev
;
1000 struct ath10k_bus_params bus_params
= {};
1002 /* Assumption: All USB based chipsets (so far) are QCA9377 based.
1003 * If there will be newer chipsets that does not use the hw reg
1004 * setup as defined in qca6174_regs and qca6174_values, this
1005 * assumption is no longer valid and hw_rev must be setup differently
1006 * depending on chipset.
1008 hw_rev
= ATH10K_HW_QCA9377
;
1010 ar
= ath10k_core_create(sizeof(*ar_usb
), &dev
->dev
, ATH10K_BUS_USB
,
1011 hw_rev
, &ath10k_usb_hif_ops
);
1013 dev_err(&dev
->dev
, "failed to allocate core\n");
1017 netif_napi_add(ar
->napi_dev
, &ar
->napi
, ath10k_usb_napi_poll
);
1020 vendor_id
= le16_to_cpu(dev
->descriptor
.idVendor
);
1021 product_id
= le16_to_cpu(dev
->descriptor
.idProduct
);
1023 ath10k_dbg(ar
, ATH10K_DBG_BOOT
,
1024 "usb new func vendor 0x%04x product 0x%04x\n",
1025 vendor_id
, product_id
);
1027 ar_usb
= ath10k_usb_priv(ar
);
1028 ret
= ath10k_usb_create(ar
, interface
);
1033 ar
->dev_id
= product_id
;
1034 ar
->id
.vendor
= vendor_id
;
1035 ar
->id
.device
= product_id
;
1037 bus_params
.dev_type
= ATH10K_DEV_TYPE_HL
;
1038 /* TODO: don't know yet how to get chip_id with USB */
1039 bus_params
.chip_id
= 0;
1040 bus_params
.hl_msdu_ids
= true;
1041 ret
= ath10k_core_register(ar
, &bus_params
);
1043 ath10k_warn(ar
, "failed to register driver core: %d\n", ret
);
1044 goto err_usb_destroy
;
1047 /* TODO: remove this once USB support is fully implemented */
1048 ath10k_warn(ar
, "Warning: ath10k USB support is incomplete, don't expect anything to work!\n");
1053 ath10k_usb_destroy(ar
);
1056 ath10k_core_destroy(ar
);
1063 static void ath10k_usb_remove(struct usb_interface
*interface
)
1065 struct ath10k_usb
*ar_usb
;
1067 ar_usb
= usb_get_intfdata(interface
);
1071 ath10k_core_unregister(ar_usb
->ar
);
1072 netif_napi_del(&ar_usb
->ar
->napi
);
1073 ath10k_usb_destroy(ar_usb
->ar
);
1074 usb_put_dev(interface_to_usbdev(interface
));
1075 ath10k_core_destroy(ar_usb
->ar
);
1080 static int ath10k_usb_pm_suspend(struct usb_interface
*interface
,
1081 pm_message_t message
)
1083 struct ath10k_usb
*ar_usb
= usb_get_intfdata(interface
);
1085 ath10k_usb_flush_all(ar_usb
->ar
);
1089 static int ath10k_usb_pm_resume(struct usb_interface
*interface
)
1091 struct ath10k_usb
*ar_usb
= usb_get_intfdata(interface
);
1092 struct ath10k
*ar
= ar_usb
->ar
;
1094 ath10k_usb_post_recv_transfers(ar
,
1095 &ar_usb
->pipes
[ATH10K_USB_PIPE_RX_DATA
]);
1102 #define ath10k_usb_pm_suspend NULL
1103 #define ath10k_usb_pm_resume NULL
1107 /* table of devices that work with this driver */
1108 static struct usb_device_id ath10k_usb_ids
[] = {
1109 {USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */
1110 { /* Terminating entry */ },
1113 MODULE_DEVICE_TABLE(usb
, ath10k_usb_ids
);
1115 static struct usb_driver ath10k_usb_driver
= {
1116 .name
= "ath10k_usb",
1117 .probe
= ath10k_usb_probe
,
1118 .suspend
= ath10k_usb_pm_suspend
,
1119 .resume
= ath10k_usb_pm_resume
,
1120 .disconnect
= ath10k_usb_remove
,
1121 .id_table
= ath10k_usb_ids
,
1122 .supports_autosuspend
= true,
1123 .disable_hub_initiated_lpm
= 1,
1126 module_usb_driver(ath10k_usb_driver
);
1128 MODULE_AUTHOR("Atheros Communications, Inc.");
1129 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros USB 802.11ac WLAN devices");
1130 MODULE_LICENSE("Dual BSD/GPL");