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 spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
42 if (!list_empty(&pipe->urb_list_head)) {
43 urb_context = list_first_entry(&pipe->urb_list_head,
44 struct ath10k_urb_context, link);
45 list_del(&urb_context->link);
48 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
53 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
54 struct ath10k_urb_context *urb_context)
58 spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
61 list_add(&urb_context->link, &pipe->urb_list_head);
63 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
66 static void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context)
68 dev_kfree_skb(urb_context->skb);
69 urb_context->skb = NULL;
71 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
74 static void ath10k_usb_free_pipe_resources(struct ath10k *ar,
75 struct ath10k_usb_pipe *pipe)
77 struct ath10k_urb_context *urb_context;
80 /* nothing allocated for this pipe */
84 ath10k_dbg(ar, ATH10K_DBG_USB,
85 "usb free resources lpipe %d hpipe 0x%x urbs %d avail %d\n",
86 pipe->logical_pipe_num, pipe->usb_pipe_handle,
87 pipe->urb_alloc, pipe->urb_cnt);
89 if (pipe->urb_alloc != pipe->urb_cnt) {
90 ath10k_dbg(ar, ATH10K_DBG_USB,
91 "usb urb leak lpipe %d hpipe 0x%x urbs %d avail %d\n",
92 pipe->logical_pipe_num, pipe->usb_pipe_handle,
93 pipe->urb_alloc, pipe->urb_cnt);
97 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
106 static void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar)
108 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
111 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++)
112 ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]);
115 /* hif usb rx/tx completion functions */
117 static void ath10k_usb_recv_complete(struct urb *urb)
119 struct ath10k_urb_context *urb_context = urb->context;
120 struct ath10k_usb_pipe *pipe = urb_context->pipe;
121 struct ath10k *ar = pipe->ar_usb->ar;
125 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
126 "usb recv pipe %d stat %d len %d urb 0x%pK\n",
127 pipe->logical_pipe_num, urb->status, urb->actual_length,
130 if (urb->status != 0) {
132 switch (urb->status) {
136 /* no need to spew these errors when device
137 * removed or urb killed due to driver shutdown
142 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
143 "usb recv pipe %d ep 0x%2.2x failed: %d\n",
144 pipe->logical_pipe_num,
145 pipe->ep_address, urb->status);
148 goto cleanup_recv_urb;
151 if (urb->actual_length == 0)
152 goto cleanup_recv_urb;
154 skb = urb_context->skb;
156 /* we are going to pass it up */
157 urb_context->skb = NULL;
158 skb_put(skb, urb->actual_length);
160 /* note: queue implements a lock */
161 skb_queue_tail(&pipe->io_comp_queue, skb);
162 schedule_work(&pipe->io_complete_work);
165 ath10k_usb_cleanup_recv_urb(urb_context);
168 pipe->urb_cnt >= pipe->urb_cnt_thresh) {
169 /* our free urbs are piling up, post more transfers */
170 ath10k_usb_post_recv_transfers(ar, pipe);
174 static void ath10k_usb_transmit_complete(struct urb *urb)
176 struct ath10k_urb_context *urb_context = urb->context;
177 struct ath10k_usb_pipe *pipe = urb_context->pipe;
178 struct ath10k *ar = pipe->ar_usb->ar;
181 if (urb->status != 0) {
182 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
183 "pipe: %d, failed:%d\n",
184 pipe->logical_pipe_num, urb->status);
187 skb = urb_context->skb;
188 urb_context->skb = NULL;
189 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
191 /* note: queue implements a lock */
192 skb_queue_tail(&pipe->io_comp_queue, skb);
193 schedule_work(&pipe->io_complete_work);
196 /* pipe operations */
197 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
198 struct ath10k_usb_pipe *recv_pipe)
200 struct ath10k_urb_context *urb_context;
205 urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe);
209 urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE);
210 if (!urb_context->skb)
213 urb = usb_alloc_urb(0, GFP_ATOMIC);
217 usb_fill_bulk_urb(urb,
218 recv_pipe->ar_usb->udev,
219 recv_pipe->usb_pipe_handle,
220 urb_context->skb->data,
221 ATH10K_USB_RX_BUFFER_SIZE,
222 ath10k_usb_recv_complete, urb_context);
224 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
225 "usb bulk recv submit %d 0x%x ep 0x%2.2x len %d buf 0x%pK\n",
226 recv_pipe->logical_pipe_num,
227 recv_pipe->usb_pipe_handle, recv_pipe->ep_address,
228 ATH10K_USB_RX_BUFFER_SIZE, urb_context->skb);
230 usb_anchor_urb(urb, &recv_pipe->urb_submitted);
231 usb_status = usb_submit_urb(urb, GFP_ATOMIC);
234 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
235 "usb bulk recv failed: %d\n",
237 usb_unanchor_urb(urb);
247 ath10k_usb_cleanup_recv_urb(urb_context);
250 static void ath10k_usb_flush_all(struct ath10k *ar)
252 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
255 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
256 if (ar_usb->pipes[i].ar_usb) {
257 usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted);
258 cancel_work_sync(&ar_usb->pipes[i].io_complete_work);
263 static void ath10k_usb_start_recv_pipes(struct ath10k *ar)
265 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
267 ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
269 ath10k_usb_post_recv_transfers(ar,
270 &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
273 static void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb)
275 struct ath10k_htc_hdr *htc_hdr;
276 struct ath10k_htc_ep *ep;
278 htc_hdr = (struct ath10k_htc_hdr *)skb->data;
279 ep = &ar->htc.endpoint[htc_hdr->eid];
280 ath10k_htc_notify_tx_completion(ep, skb);
281 /* The TX complete handler now owns the skb... */
284 static void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb)
286 struct ath10k_htc *htc = &ar->htc;
287 struct ath10k_htc_hdr *htc_hdr;
288 enum ath10k_htc_ep_id eid;
289 struct ath10k_htc_ep *ep;
294 htc_hdr = (struct ath10k_htc_hdr *)skb->data;
295 eid = eid_from_htc_hdr(htc_hdr);
296 ep = &ar->htc.endpoint[eid];
298 if (ep->service_id == 0) {
299 ath10k_warn(ar, "ep %d is not connected\n", eid);
303 payload_len = le16_to_cpu(htc_hdr->len);
305 ath10k_warn(ar, "zero length frame received, firmware crashed?\n");
309 if (payload_len < htc_hdr->trailer_len) {
310 ath10k_warn(ar, "malformed frame received, firmware crashed?\n");
314 if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) {
315 trailer = skb->data + sizeof(*htc_hdr) + payload_len -
316 htc_hdr->trailer_len;
318 ret = ath10k_htc_process_trailer(htc,
320 htc_hdr->trailer_len,
327 if (is_trailer_only_msg(htc_hdr))
330 /* strip off the trailer from the skb since it should not
331 * be passed on to upper layers
333 skb_trim(skb, skb->len - htc_hdr->trailer_len);
336 skb_pull(skb, sizeof(*htc_hdr));
337 ep->ep_ops.ep_rx_complete(ar, skb);
338 /* The RX complete handler now owns the skb... */
346 static void ath10k_usb_io_comp_work(struct work_struct *work)
348 struct ath10k_usb_pipe *pipe = container_of(work,
349 struct ath10k_usb_pipe,
351 struct ath10k *ar = pipe->ar_usb->ar;
354 while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
355 if (pipe->flags & ATH10K_USB_PIPE_FLAG_TX)
356 ath10k_usb_tx_complete(ar, skb);
358 ath10k_usb_rx_complete(ar, skb);
362 #define ATH10K_USB_MAX_DIAG_CMD (sizeof(struct ath10k_usb_ctrl_diag_cmd_write))
363 #define ATH10K_USB_MAX_DIAG_RESP (sizeof(struct ath10k_usb_ctrl_diag_resp_read))
365 static void ath10k_usb_destroy(struct ath10k *ar)
367 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
369 ath10k_usb_flush_all(ar);
370 ath10k_usb_cleanup_pipe_resources(ar);
371 usb_set_intfdata(ar_usb->interface, NULL);
373 kfree(ar_usb->diag_cmd_buffer);
374 kfree(ar_usb->diag_resp_buffer);
377 static int ath10k_usb_hif_start(struct ath10k *ar)
380 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
382 ath10k_usb_start_recv_pipes(ar);
384 /* set the TX resource avail threshold for each TX pipe */
385 for (i = ATH10K_USB_PIPE_TX_CTRL;
386 i <= ATH10K_USB_PIPE_TX_DATA_HP; i++) {
387 ar_usb->pipes[i].urb_cnt_thresh =
388 ar_usb->pipes[i].urb_alloc / 2;
394 static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
395 struct ath10k_hif_sg_item *items, int n_items)
397 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
398 struct ath10k_usb_pipe *pipe = &ar_usb->pipes[pipe_id];
399 struct ath10k_urb_context *urb_context;
404 for (i = 0; i < n_items; i++) {
405 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
411 skb = items[i].transfer_context;
412 urb_context->skb = skb;
414 urb = usb_alloc_urb(0, GFP_ATOMIC);
417 goto err_free_urb_to_pipe;
420 usb_fill_bulk_urb(urb,
422 pipe->usb_pipe_handle,
425 ath10k_usb_transmit_complete, urb_context);
427 if (!(skb->len % pipe->max_packet_size)) {
428 /* hit a max packet boundary on this pipe */
429 urb->transfer_flags |= URB_ZERO_PACKET;
432 usb_anchor_urb(urb, &pipe->urb_submitted);
433 ret = usb_submit_urb(urb, GFP_ATOMIC);
435 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
436 "usb bulk transmit failed: %d\n", ret);
437 usb_unanchor_urb(urb);
439 goto err_free_urb_to_pipe;
447 err_free_urb_to_pipe:
448 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
453 static void ath10k_usb_hif_stop(struct ath10k *ar)
455 ath10k_usb_flush_all(ar);
458 static u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id)
460 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
462 return ar_usb->pipes[pipe_id].urb_cnt;
465 static int ath10k_usb_submit_ctrl_out(struct ath10k *ar,
466 u8 req, u16 value, u16 index, void *data,
469 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
474 buf = kmemdup(data, size, GFP_KERNEL);
479 /* note: if successful returns number of bytes transferred */
480 ret = usb_control_msg(ar_usb->udev,
481 usb_sndctrlpipe(ar_usb->udev, 0),
483 USB_DIR_OUT | USB_TYPE_VENDOR |
484 USB_RECIP_DEVICE, value, index, buf,
488 ath10k_warn(ar, "Failed to submit usb control message: %d\n",
499 static int ath10k_usb_submit_ctrl_in(struct ath10k *ar,
500 u8 req, u16 value, u16 index, void *data,
503 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
508 buf = kmalloc(size, GFP_KERNEL);
513 /* note: if successful returns number of bytes transferred */
514 ret = usb_control_msg(ar_usb->udev,
515 usb_rcvctrlpipe(ar_usb->udev, 0),
517 USB_DIR_IN | USB_TYPE_VENDOR |
518 USB_RECIP_DEVICE, value, index, buf,
522 ath10k_warn(ar, "Failed to read usb control message: %d\n",
528 memcpy((u8 *)data, buf, size);
535 static int ath10k_usb_ctrl_msg_exchange(struct ath10k *ar,
536 u8 req_val, u8 *req_buf, u32 req_len,
537 u8 resp_val, u8 *resp_buf,
543 ret = ath10k_usb_submit_ctrl_out(ar, req_val, 0, 0,
550 ret = ath10k_usb_submit_ctrl_in(ar, resp_val, 0, 0,
551 resp_buf, *resp_len);
561 static int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
564 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
565 struct ath10k_usb_ctrl_diag_cmd_read *cmd;
569 if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read))
572 cmd = (struct ath10k_usb_ctrl_diag_cmd_read *)ar_usb->diag_cmd_buffer;
573 memset(cmd, 0, sizeof(*cmd));
574 cmd->cmd = ATH10K_USB_CTRL_DIAG_CC_READ;
575 cmd->address = cpu_to_le32(address);
576 resp_len = sizeof(struct ath10k_usb_ctrl_diag_resp_read);
578 ret = ath10k_usb_ctrl_msg_exchange(ar,
579 ATH10K_USB_CONTROL_REQ_DIAG_CMD,
582 ATH10K_USB_CONTROL_REQ_DIAG_RESP,
583 ar_usb->diag_resp_buffer, &resp_len);
587 if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read))
590 memcpy(buf, ar_usb->diag_resp_buffer,
591 sizeof(struct ath10k_usb_ctrl_diag_resp_read));
596 static int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address,
597 const void *data, int nbytes)
599 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
600 struct ath10k_usb_ctrl_diag_cmd_write *cmd;
603 if (nbytes != sizeof(cmd->value))
606 cmd = (struct ath10k_usb_ctrl_diag_cmd_write *)ar_usb->diag_cmd_buffer;
607 memset(cmd, 0, sizeof(*cmd));
608 cmd->cmd = cpu_to_le32(ATH10K_USB_CTRL_DIAG_CC_WRITE);
609 cmd->address = cpu_to_le32(address);
610 memcpy(&cmd->value, data, nbytes);
612 ret = ath10k_usb_ctrl_msg_exchange(ar,
613 ATH10K_USB_CONTROL_REQ_DIAG_CMD,
623 static int ath10k_usb_bmi_exchange_msg(struct ath10k *ar,
624 void *req, u32 req_len,
625 void *resp, u32 *resp_len)
630 ret = ath10k_usb_submit_ctrl_out(ar,
631 ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD,
635 "unable to send the bmi data to the device: %d\n",
642 ret = ath10k_usb_submit_ctrl_in(ar,
643 ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP,
644 0, 0, resp, *resp_len);
647 "Unable to read the bmi data from the device: %d\n",
656 static void ath10k_usb_hif_get_default_pipe(struct ath10k *ar,
657 u8 *ul_pipe, u8 *dl_pipe)
659 *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
660 *dl_pipe = ATH10K_USB_PIPE_RX_CTRL;
663 static int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id,
664 u8 *ul_pipe, u8 *dl_pipe)
667 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
668 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
669 *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
670 /* due to large control packets, shift to data pipe */
671 *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
673 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
674 *ul_pipe = ATH10K_USB_PIPE_TX_DATA_LP;
675 /* Disable rxdata2 directly, it will be enabled
676 * if FW enable rxdata2
678 *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
687 /* This op is currently only used by htc_wait_target if the HTC ready
688 * message times out. It is not applicable for USB since there is nothing
689 * we can do if the HTC ready message does not arrive in time.
690 * TODO: Make this op non mandatory by introducing a NULL check in the
693 static void ath10k_usb_hif_send_complete_check(struct ath10k *ar,
698 static int ath10k_usb_hif_power_up(struct ath10k *ar,
699 enum ath10k_firmware_mode fw_mode)
704 static void ath10k_usb_hif_power_down(struct ath10k *ar)
706 ath10k_usb_flush_all(ar);
711 static int ath10k_usb_hif_suspend(struct ath10k *ar)
716 static int ath10k_usb_hif_resume(struct ath10k *ar)
722 static const struct ath10k_hif_ops ath10k_usb_hif_ops = {
723 .tx_sg = ath10k_usb_hif_tx_sg,
724 .diag_read = ath10k_usb_hif_diag_read,
725 .diag_write = ath10k_usb_hif_diag_write,
726 .exchange_bmi_msg = ath10k_usb_bmi_exchange_msg,
727 .start = ath10k_usb_hif_start,
728 .stop = ath10k_usb_hif_stop,
729 .map_service_to_pipe = ath10k_usb_hif_map_service_to_pipe,
730 .get_default_pipe = ath10k_usb_hif_get_default_pipe,
731 .send_complete_check = ath10k_usb_hif_send_complete_check,
732 .get_free_queue_number = ath10k_usb_hif_get_free_queue_number,
733 .power_up = ath10k_usb_hif_power_up,
734 .power_down = ath10k_usb_hif_power_down,
736 .suspend = ath10k_usb_hif_suspend,
737 .resume = ath10k_usb_hif_resume,
741 static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
743 u8 pipe_num = ATH10K_USB_PIPE_INVALID;
745 switch (ep_address) {
746 case ATH10K_USB_EP_ADDR_APP_CTRL_IN:
747 pipe_num = ATH10K_USB_PIPE_RX_CTRL;
748 *urb_count = RX_URB_COUNT;
750 case ATH10K_USB_EP_ADDR_APP_DATA_IN:
751 pipe_num = ATH10K_USB_PIPE_RX_DATA;
752 *urb_count = RX_URB_COUNT;
754 case ATH10K_USB_EP_ADDR_APP_INT_IN:
755 pipe_num = ATH10K_USB_PIPE_RX_INT;
756 *urb_count = RX_URB_COUNT;
758 case ATH10K_USB_EP_ADDR_APP_DATA2_IN:
759 pipe_num = ATH10K_USB_PIPE_RX_DATA2;
760 *urb_count = RX_URB_COUNT;
762 case ATH10K_USB_EP_ADDR_APP_CTRL_OUT:
763 pipe_num = ATH10K_USB_PIPE_TX_CTRL;
764 *urb_count = TX_URB_COUNT;
766 case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT:
767 pipe_num = ATH10K_USB_PIPE_TX_DATA_LP;
768 *urb_count = TX_URB_COUNT;
770 case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT:
771 pipe_num = ATH10K_USB_PIPE_TX_DATA_MP;
772 *urb_count = TX_URB_COUNT;
774 case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT:
775 pipe_num = ATH10K_USB_PIPE_TX_DATA_HP;
776 *urb_count = TX_URB_COUNT;
779 /* note: there may be endpoints not currently used */
786 static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
787 struct ath10k_usb_pipe *pipe,
790 struct ath10k_urb_context *urb_context;
793 INIT_LIST_HEAD(&pipe->urb_list_head);
794 init_usb_anchor(&pipe->urb_submitted);
796 for (i = 0; i < urb_cnt; i++) {
797 urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
801 urb_context->pipe = pipe;
803 /* we are only allocate the urb contexts here, the actual URB
804 * is allocated from the kernel as needed to do a transaction
807 ath10k_usb_free_urb_to_pipe(pipe, urb_context);
810 ath10k_dbg(ar, ATH10K_DBG_USB,
811 "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n",
812 pipe->logical_pipe_num, pipe->usb_pipe_handle,
818 static int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
819 struct usb_interface *interface)
821 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
822 struct usb_host_interface *iface_desc = interface->cur_altsetting;
823 struct usb_endpoint_descriptor *endpoint;
824 struct ath10k_usb_pipe *pipe;
825 int ret, i, urbcount;
828 ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
830 /* walk decriptors and setup pipes */
831 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
832 endpoint = &iface_desc->endpoint[i].desc;
834 if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
835 ath10k_dbg(ar, ATH10K_DBG_USB,
836 "usb %s bulk ep 0x%2.2x maxpktsz %d\n",
838 (endpoint->bEndpointAddress) ?
839 "rx" : "tx", endpoint->bEndpointAddress,
840 le16_to_cpu(endpoint->wMaxPacketSize));
841 } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
842 ath10k_dbg(ar, ATH10K_DBG_USB,
843 "usb %s int ep 0x%2.2x maxpktsz %d interval %d\n",
845 (endpoint->bEndpointAddress) ?
846 "rx" : "tx", endpoint->bEndpointAddress,
847 le16_to_cpu(endpoint->wMaxPacketSize),
848 endpoint->bInterval);
849 } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
851 ath10k_dbg(ar, ATH10K_DBG_USB,
852 "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
854 (endpoint->bEndpointAddress) ?
855 "rx" : "tx", endpoint->bEndpointAddress,
856 le16_to_cpu(endpoint->wMaxPacketSize),
857 endpoint->bInterval);
862 ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
864 if (pipe_num == ATH10K_USB_PIPE_INVALID)
867 pipe = &ar_usb->pipes[pipe_num];
869 /* hmmm..pipe was already setup */
872 pipe->ar_usb = ar_usb;
873 pipe->logical_pipe_num = pipe_num;
874 pipe->ep_address = endpoint->bEndpointAddress;
875 pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
877 if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
878 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
879 pipe->usb_pipe_handle =
880 usb_rcvbulkpipe(ar_usb->udev,
883 pipe->usb_pipe_handle =
884 usb_sndbulkpipe(ar_usb->udev,
887 } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
888 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
889 pipe->usb_pipe_handle =
890 usb_rcvintpipe(ar_usb->udev,
893 pipe->usb_pipe_handle =
894 usb_sndintpipe(ar_usb->udev,
897 } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
899 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
900 pipe->usb_pipe_handle =
901 usb_rcvisocpipe(ar_usb->udev,
904 pipe->usb_pipe_handle =
905 usb_sndisocpipe(ar_usb->udev,
910 pipe->ep_desc = endpoint;
912 if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
913 pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
915 ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
923 static int ath10k_usb_create(struct ath10k *ar,
924 struct usb_interface *interface)
926 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
927 struct usb_device *dev = interface_to_usbdev(interface);
928 struct ath10k_usb_pipe *pipe;
931 usb_set_intfdata(interface, ar_usb);
932 spin_lock_init(&ar_usb->cs_lock);
934 ar_usb->interface = interface;
936 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
937 pipe = &ar_usb->pipes[i];
938 INIT_WORK(&pipe->io_complete_work,
939 ath10k_usb_io_comp_work);
940 skb_queue_head_init(&pipe->io_comp_queue);
943 ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL);
944 if (!ar_usb->diag_cmd_buffer) {
949 ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP,
951 if (!ar_usb->diag_resp_buffer) {
956 ret = ath10k_usb_setup_pipe_resources(ar, interface);
963 ath10k_usb_destroy(ar);
967 /* ath10k usb driver registered functions */
968 static int ath10k_usb_probe(struct usb_interface *interface,
969 const struct usb_device_id *id)
972 struct ath10k_usb *ar_usb;
973 struct usb_device *dev = interface_to_usbdev(interface);
974 int ret, vendor_id, product_id;
975 enum ath10k_hw_rev hw_rev;
976 struct ath10k_bus_params bus_params;
978 /* Assumption: All USB based chipsets (so far) are QCA9377 based.
979 * If there will be newer chipsets that does not use the hw reg
980 * setup as defined in qca6174_regs and qca6174_values, this
981 * assumption is no longer valid and hw_rev must be setup differently
982 * depending on chipset.
984 hw_rev = ATH10K_HW_QCA9377;
986 ar = ath10k_core_create(sizeof(*ar_usb), &dev->dev, ATH10K_BUS_USB,
987 hw_rev, &ath10k_usb_hif_ops);
989 dev_err(&dev->dev, "failed to allocate core\n");
994 vendor_id = le16_to_cpu(dev->descriptor.idVendor);
995 product_id = le16_to_cpu(dev->descriptor.idProduct);
997 ath10k_dbg(ar, ATH10K_DBG_BOOT,
998 "usb new func vendor 0x%04x product 0x%04x\n",
999 vendor_id, product_id);
1001 ar_usb = ath10k_usb_priv(ar);
1002 ret = ath10k_usb_create(ar, interface);
1005 ar->dev_id = product_id;
1006 ar->id.vendor = vendor_id;
1007 ar->id.device = product_id;
1009 bus_params.dev_type = ATH10K_DEV_TYPE_HL;
1010 /* TODO: don't know yet how to get chip_id with USB */
1011 bus_params.chip_id = 0;
1012 ret = ath10k_core_register(ar, &bus_params);
1014 ath10k_warn(ar, "failed to register driver core: %d\n", ret);
1018 /* TODO: remove this once USB support is fully implemented */
1019 ath10k_warn(ar, "WARNING: ath10k USB support is incomplete, don't expect anything to work!\n");
1024 ath10k_core_destroy(ar);
1031 static void ath10k_usb_remove(struct usb_interface *interface)
1033 struct ath10k_usb *ar_usb;
1035 ar_usb = usb_get_intfdata(interface);
1039 ath10k_core_unregister(ar_usb->ar);
1040 ath10k_usb_destroy(ar_usb->ar);
1041 usb_put_dev(interface_to_usbdev(interface));
1042 ath10k_core_destroy(ar_usb->ar);
1047 static int ath10k_usb_pm_suspend(struct usb_interface *interface,
1048 pm_message_t message)
1050 struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1052 ath10k_usb_flush_all(ar_usb->ar);
1056 static int ath10k_usb_pm_resume(struct usb_interface *interface)
1058 struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1059 struct ath10k *ar = ar_usb->ar;
1061 ath10k_usb_post_recv_transfers(ar,
1062 &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
1069 #define ath10k_usb_pm_suspend NULL
1070 #define ath10k_usb_pm_resume NULL
1074 /* table of devices that work with this driver */
1075 static struct usb_device_id ath10k_usb_ids[] = {
1076 {USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */
1077 { /* Terminating entry */ },
1080 MODULE_DEVICE_TABLE(usb, ath10k_usb_ids);
1082 static struct usb_driver ath10k_usb_driver = {
1083 .name = "ath10k_usb",
1084 .probe = ath10k_usb_probe,
1085 .suspend = ath10k_usb_pm_suspend,
1086 .resume = ath10k_usb_pm_resume,
1087 .disconnect = ath10k_usb_remove,
1088 .id_table = ath10k_usb_ids,
1089 .supports_autosuspend = true,
1090 .disable_hub_initiated_lpm = 1,
1093 module_usb_driver(ath10k_usb_driver);
1095 MODULE_AUTHOR("Atheros Communications, Inc.");
1096 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices");
1097 MODULE_LICENSE("Dual BSD/GPL");