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 /* bail if this pipe is not initialized */
56 spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
57 if (!list_empty(&pipe->urb_list_head)) {
58 urb_context = list_first_entry(&pipe->urb_list_head,
59 struct ath10k_urb_context, link);
60 list_del(&urb_context->link);
63 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
68 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
69 struct ath10k_urb_context *urb_context)
73 /* bail if this pipe is not initialized */
77 spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
80 list_add(&urb_context->link, &pipe->urb_list_head);
82 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
85 static void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context)
87 dev_kfree_skb(urb_context->skb);
88 urb_context->skb = NULL;
90 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
93 static void ath10k_usb_free_pipe_resources(struct ath10k *ar,
94 struct ath10k_usb_pipe *pipe)
96 struct ath10k_urb_context *urb_context;
99 /* nothing allocated for this pipe */
103 ath10k_dbg(ar, ATH10K_DBG_USB,
104 "usb free resources lpipe %d hpipe 0x%x urbs %d avail %d\n",
105 pipe->logical_pipe_num, pipe->usb_pipe_handle,
106 pipe->urb_alloc, pipe->urb_cnt);
108 if (pipe->urb_alloc != pipe->urb_cnt) {
109 ath10k_dbg(ar, ATH10K_DBG_USB,
110 "usb urb leak lpipe %d hpipe 0x%x urbs %d avail %d\n",
111 pipe->logical_pipe_num, pipe->usb_pipe_handle,
112 pipe->urb_alloc, pipe->urb_cnt);
116 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
125 static void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar)
127 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
130 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++)
131 ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]);
134 /* hif usb rx/tx completion functions */
136 static void ath10k_usb_recv_complete(struct urb *urb)
138 struct ath10k_urb_context *urb_context = urb->context;
139 struct ath10k_usb_pipe *pipe = urb_context->pipe;
140 struct ath10k *ar = pipe->ar_usb->ar;
144 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
145 "usb recv pipe %d stat %d len %d urb 0x%pK\n",
146 pipe->logical_pipe_num, urb->status, urb->actual_length,
149 if (urb->status != 0) {
151 switch (urb->status) {
155 /* no need to spew these errors when device
156 * removed or urb killed due to driver shutdown
161 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
162 "usb recv pipe %d ep 0x%2.2x failed: %d\n",
163 pipe->logical_pipe_num,
164 pipe->ep_address, urb->status);
167 goto cleanup_recv_urb;
170 if (urb->actual_length == 0)
171 goto cleanup_recv_urb;
173 skb = urb_context->skb;
175 /* we are going to pass it up */
176 urb_context->skb = NULL;
177 skb_put(skb, urb->actual_length);
179 /* note: queue implements a lock */
180 skb_queue_tail(&pipe->io_comp_queue, skb);
181 schedule_work(&pipe->io_complete_work);
184 ath10k_usb_cleanup_recv_urb(urb_context);
187 pipe->urb_cnt >= pipe->urb_cnt_thresh) {
188 /* our free urbs are piling up, post more transfers */
189 ath10k_usb_post_recv_transfers(ar, pipe);
193 static void ath10k_usb_transmit_complete(struct urb *urb)
195 struct ath10k_urb_context *urb_context = urb->context;
196 struct ath10k_usb_pipe *pipe = urb_context->pipe;
197 struct ath10k *ar = pipe->ar_usb->ar;
200 if (urb->status != 0) {
201 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
202 "pipe: %d, failed:%d\n",
203 pipe->logical_pipe_num, urb->status);
206 skb = urb_context->skb;
207 urb_context->skb = NULL;
208 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
210 /* note: queue implements a lock */
211 skb_queue_tail(&pipe->io_comp_queue, skb);
212 schedule_work(&pipe->io_complete_work);
215 /* pipe operations */
216 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
217 struct ath10k_usb_pipe *recv_pipe)
219 struct ath10k_urb_context *urb_context;
224 urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe);
228 urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE);
229 if (!urb_context->skb)
232 urb = usb_alloc_urb(0, GFP_ATOMIC);
236 usb_fill_bulk_urb(urb,
237 recv_pipe->ar_usb->udev,
238 recv_pipe->usb_pipe_handle,
239 urb_context->skb->data,
240 ATH10K_USB_RX_BUFFER_SIZE,
241 ath10k_usb_recv_complete, urb_context);
243 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
244 "usb bulk recv submit %d 0x%x ep 0x%2.2x len %d buf 0x%pK\n",
245 recv_pipe->logical_pipe_num,
246 recv_pipe->usb_pipe_handle, recv_pipe->ep_address,
247 ATH10K_USB_RX_BUFFER_SIZE, urb_context->skb);
249 usb_anchor_urb(urb, &recv_pipe->urb_submitted);
250 usb_status = usb_submit_urb(urb, GFP_ATOMIC);
253 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
254 "usb bulk recv failed: %d\n",
256 usb_unanchor_urb(urb);
266 ath10k_usb_cleanup_recv_urb(urb_context);
269 static void ath10k_usb_flush_all(struct ath10k *ar)
271 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
274 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
275 if (ar_usb->pipes[i].ar_usb) {
276 usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted);
277 cancel_work_sync(&ar_usb->pipes[i].io_complete_work);
282 static void ath10k_usb_start_recv_pipes(struct ath10k *ar)
284 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
286 ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
288 ath10k_usb_post_recv_transfers(ar,
289 &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
292 static void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb)
294 struct ath10k_htc_hdr *htc_hdr;
295 struct ath10k_htc_ep *ep;
297 htc_hdr = (struct ath10k_htc_hdr *)skb->data;
298 ep = &ar->htc.endpoint[htc_hdr->eid];
299 ath10k_htc_notify_tx_completion(ep, skb);
300 /* The TX complete handler now owns the skb... */
303 static void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb)
305 struct ath10k_htc *htc = &ar->htc;
306 struct ath10k_htc_hdr *htc_hdr;
307 enum ath10k_htc_ep_id eid;
308 struct ath10k_htc_ep *ep;
313 htc_hdr = (struct ath10k_htc_hdr *)skb->data;
314 eid = eid_from_htc_hdr(htc_hdr);
315 ep = &ar->htc.endpoint[eid];
317 if (ep->service_id == 0) {
318 ath10k_warn(ar, "ep %d is not connected\n", eid);
322 payload_len = le16_to_cpu(htc_hdr->len);
324 ath10k_warn(ar, "zero length frame received, firmware crashed?\n");
328 if (payload_len < htc_hdr->trailer_len) {
329 ath10k_warn(ar, "malformed frame received, firmware crashed?\n");
333 if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) {
334 trailer = skb->data + sizeof(*htc_hdr) + payload_len -
335 htc_hdr->trailer_len;
337 ret = ath10k_htc_process_trailer(htc,
339 htc_hdr->trailer_len,
346 if (is_trailer_only_msg(htc_hdr))
349 /* strip off the trailer from the skb since it should not
350 * be passed on to upper layers
352 skb_trim(skb, skb->len - htc_hdr->trailer_len);
355 skb_pull(skb, sizeof(*htc_hdr));
356 ep->ep_ops.ep_rx_complete(ar, skb);
357 /* The RX complete handler now owns the skb... */
365 static void ath10k_usb_io_comp_work(struct work_struct *work)
367 struct ath10k_usb_pipe *pipe = container_of(work,
368 struct ath10k_usb_pipe,
370 struct ath10k *ar = pipe->ar_usb->ar;
373 while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
374 if (pipe->flags & ATH10K_USB_PIPE_FLAG_TX)
375 ath10k_usb_tx_complete(ar, skb);
377 ath10k_usb_rx_complete(ar, skb);
381 #define ATH10K_USB_MAX_DIAG_CMD (sizeof(struct ath10k_usb_ctrl_diag_cmd_write))
382 #define ATH10K_USB_MAX_DIAG_RESP (sizeof(struct ath10k_usb_ctrl_diag_resp_read))
384 static void ath10k_usb_destroy(struct ath10k *ar)
386 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
388 ath10k_usb_flush_all(ar);
389 ath10k_usb_cleanup_pipe_resources(ar);
390 usb_set_intfdata(ar_usb->interface, NULL);
392 kfree(ar_usb->diag_cmd_buffer);
393 kfree(ar_usb->diag_resp_buffer);
396 static int ath10k_usb_hif_start(struct ath10k *ar)
399 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
401 ath10k_usb_start_recv_pipes(ar);
403 /* set the TX resource avail threshold for each TX pipe */
404 for (i = ATH10K_USB_PIPE_TX_CTRL;
405 i <= ATH10K_USB_PIPE_TX_DATA_HP; i++) {
406 ar_usb->pipes[i].urb_cnt_thresh =
407 ar_usb->pipes[i].urb_alloc / 2;
413 static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
414 struct ath10k_hif_sg_item *items, int n_items)
416 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
417 struct ath10k_usb_pipe *pipe = &ar_usb->pipes[pipe_id];
418 struct ath10k_urb_context *urb_context;
423 for (i = 0; i < n_items; i++) {
424 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
430 skb = items[i].transfer_context;
431 urb_context->skb = skb;
433 urb = usb_alloc_urb(0, GFP_ATOMIC);
436 goto err_free_urb_to_pipe;
439 usb_fill_bulk_urb(urb,
441 pipe->usb_pipe_handle,
444 ath10k_usb_transmit_complete, urb_context);
446 if (!(skb->len % pipe->max_packet_size)) {
447 /* hit a max packet boundary on this pipe */
448 urb->transfer_flags |= URB_ZERO_PACKET;
451 usb_anchor_urb(urb, &pipe->urb_submitted);
452 ret = usb_submit_urb(urb, GFP_ATOMIC);
454 ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
455 "usb bulk transmit failed: %d\n", ret);
456 usb_unanchor_urb(urb);
459 goto err_free_urb_to_pipe;
467 err_free_urb_to_pipe:
468 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
473 static void ath10k_usb_hif_stop(struct ath10k *ar)
475 ath10k_usb_flush_all(ar);
478 static u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id)
480 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
482 return ar_usb->pipes[pipe_id].urb_cnt;
485 static int ath10k_usb_submit_ctrl_out(struct ath10k *ar,
486 u8 req, u16 value, u16 index, void *data,
489 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
494 buf = kmemdup(data, size, GFP_KERNEL);
499 /* note: if successful returns number of bytes transferred */
500 ret = usb_control_msg(ar_usb->udev,
501 usb_sndctrlpipe(ar_usb->udev, 0),
503 USB_DIR_OUT | USB_TYPE_VENDOR |
504 USB_RECIP_DEVICE, value, index, buf,
508 ath10k_warn(ar, "Failed to submit usb control message: %d\n",
519 static int ath10k_usb_submit_ctrl_in(struct ath10k *ar,
520 u8 req, u16 value, u16 index, void *data,
523 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
528 buf = kmalloc(size, GFP_KERNEL);
533 /* note: if successful returns number of bytes transferred */
534 ret = usb_control_msg(ar_usb->udev,
535 usb_rcvctrlpipe(ar_usb->udev, 0),
537 USB_DIR_IN | USB_TYPE_VENDOR |
538 USB_RECIP_DEVICE, value, index, buf,
542 ath10k_warn(ar, "Failed to read usb control message: %d\n",
548 memcpy((u8 *)data, buf, size);
555 static int ath10k_usb_ctrl_msg_exchange(struct ath10k *ar,
556 u8 req_val, u8 *req_buf, u32 req_len,
557 u8 resp_val, u8 *resp_buf,
563 ret = ath10k_usb_submit_ctrl_out(ar, req_val, 0, 0,
570 ret = ath10k_usb_submit_ctrl_in(ar, resp_val, 0, 0,
571 resp_buf, *resp_len);
581 static int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
584 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
585 struct ath10k_usb_ctrl_diag_cmd_read *cmd;
589 if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read))
592 cmd = (struct ath10k_usb_ctrl_diag_cmd_read *)ar_usb->diag_cmd_buffer;
593 memset(cmd, 0, sizeof(*cmd));
594 cmd->cmd = ATH10K_USB_CTRL_DIAG_CC_READ;
595 cmd->address = cpu_to_le32(address);
596 resp_len = sizeof(struct ath10k_usb_ctrl_diag_resp_read);
598 ret = ath10k_usb_ctrl_msg_exchange(ar,
599 ATH10K_USB_CONTROL_REQ_DIAG_CMD,
602 ATH10K_USB_CONTROL_REQ_DIAG_RESP,
603 ar_usb->diag_resp_buffer, &resp_len);
607 if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read))
610 memcpy(buf, ar_usb->diag_resp_buffer,
611 sizeof(struct ath10k_usb_ctrl_diag_resp_read));
616 static int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address,
617 const void *data, int nbytes)
619 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
620 struct ath10k_usb_ctrl_diag_cmd_write *cmd;
623 if (nbytes != sizeof(cmd->value))
626 cmd = (struct ath10k_usb_ctrl_diag_cmd_write *)ar_usb->diag_cmd_buffer;
627 memset(cmd, 0, sizeof(*cmd));
628 cmd->cmd = cpu_to_le32(ATH10K_USB_CTRL_DIAG_CC_WRITE);
629 cmd->address = cpu_to_le32(address);
630 memcpy(&cmd->value, data, nbytes);
632 ret = ath10k_usb_ctrl_msg_exchange(ar,
633 ATH10K_USB_CONTROL_REQ_DIAG_CMD,
643 static int ath10k_usb_bmi_exchange_msg(struct ath10k *ar,
644 void *req, u32 req_len,
645 void *resp, u32 *resp_len)
650 ret = ath10k_usb_submit_ctrl_out(ar,
651 ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD,
655 "unable to send the bmi data to the device: %d\n",
662 ret = ath10k_usb_submit_ctrl_in(ar,
663 ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP,
664 0, 0, resp, *resp_len);
667 "Unable to read the bmi data from the device: %d\n",
676 static void ath10k_usb_hif_get_default_pipe(struct ath10k *ar,
677 u8 *ul_pipe, u8 *dl_pipe)
679 *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
680 *dl_pipe = ATH10K_USB_PIPE_RX_CTRL;
683 static int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id,
684 u8 *ul_pipe, u8 *dl_pipe)
687 case ATH10K_HTC_SVC_ID_RSVD_CTRL:
688 case ATH10K_HTC_SVC_ID_WMI_CONTROL:
689 *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
690 /* due to large control packets, shift to data pipe */
691 *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
693 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
694 *ul_pipe = ATH10K_USB_PIPE_TX_DATA_LP;
695 /* Disable rxdata2 directly, it will be enabled
696 * if FW enable rxdata2
698 *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
707 /* This op is currently only used by htc_wait_target if the HTC ready
708 * message times out. It is not applicable for USB since there is nothing
709 * we can do if the HTC ready message does not arrive in time.
710 * TODO: Make this op non mandatory by introducing a NULL check in the
713 static void ath10k_usb_hif_send_complete_check(struct ath10k *ar,
718 static int ath10k_usb_hif_power_up(struct ath10k *ar)
723 static void ath10k_usb_hif_power_down(struct ath10k *ar)
725 ath10k_usb_flush_all(ar);
730 static int ath10k_usb_hif_suspend(struct ath10k *ar)
735 static int ath10k_usb_hif_resume(struct ath10k *ar)
741 static const struct ath10k_hif_ops ath10k_usb_hif_ops = {
742 .tx_sg = ath10k_usb_hif_tx_sg,
743 .diag_read = ath10k_usb_hif_diag_read,
744 .diag_write = ath10k_usb_hif_diag_write,
745 .exchange_bmi_msg = ath10k_usb_bmi_exchange_msg,
746 .start = ath10k_usb_hif_start,
747 .stop = ath10k_usb_hif_stop,
748 .map_service_to_pipe = ath10k_usb_hif_map_service_to_pipe,
749 .get_default_pipe = ath10k_usb_hif_get_default_pipe,
750 .send_complete_check = ath10k_usb_hif_send_complete_check,
751 .get_free_queue_number = ath10k_usb_hif_get_free_queue_number,
752 .power_up = ath10k_usb_hif_power_up,
753 .power_down = ath10k_usb_hif_power_down,
755 .suspend = ath10k_usb_hif_suspend,
756 .resume = ath10k_usb_hif_resume,
760 static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
762 u8 pipe_num = ATH10K_USB_PIPE_INVALID;
764 switch (ep_address) {
765 case ATH10K_USB_EP_ADDR_APP_CTRL_IN:
766 pipe_num = ATH10K_USB_PIPE_RX_CTRL;
767 *urb_count = RX_URB_COUNT;
769 case ATH10K_USB_EP_ADDR_APP_DATA_IN:
770 pipe_num = ATH10K_USB_PIPE_RX_DATA;
771 *urb_count = RX_URB_COUNT;
773 case ATH10K_USB_EP_ADDR_APP_INT_IN:
774 pipe_num = ATH10K_USB_PIPE_RX_INT;
775 *urb_count = RX_URB_COUNT;
777 case ATH10K_USB_EP_ADDR_APP_DATA2_IN:
778 pipe_num = ATH10K_USB_PIPE_RX_DATA2;
779 *urb_count = RX_URB_COUNT;
781 case ATH10K_USB_EP_ADDR_APP_CTRL_OUT:
782 pipe_num = ATH10K_USB_PIPE_TX_CTRL;
783 *urb_count = TX_URB_COUNT;
785 case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT:
786 pipe_num = ATH10K_USB_PIPE_TX_DATA_LP;
787 *urb_count = TX_URB_COUNT;
789 case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT:
790 pipe_num = ATH10K_USB_PIPE_TX_DATA_MP;
791 *urb_count = TX_URB_COUNT;
793 case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT:
794 pipe_num = ATH10K_USB_PIPE_TX_DATA_HP;
795 *urb_count = TX_URB_COUNT;
798 /* note: there may be endpoints not currently used */
805 static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
806 struct ath10k_usb_pipe *pipe,
809 struct ath10k_urb_context *urb_context;
812 INIT_LIST_HEAD(&pipe->urb_list_head);
813 init_usb_anchor(&pipe->urb_submitted);
815 for (i = 0; i < urb_cnt; i++) {
816 urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
820 urb_context->pipe = pipe;
822 /* we are only allocate the urb contexts here, the actual URB
823 * is allocated from the kernel as needed to do a transaction
826 ath10k_usb_free_urb_to_pipe(pipe, urb_context);
829 ath10k_dbg(ar, ATH10K_DBG_USB,
830 "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n",
831 pipe->logical_pipe_num, pipe->usb_pipe_handle,
837 static int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
838 struct usb_interface *interface)
840 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
841 struct usb_host_interface *iface_desc = interface->cur_altsetting;
842 struct usb_endpoint_descriptor *endpoint;
843 struct ath10k_usb_pipe *pipe;
844 int ret, i, urbcount;
847 ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
849 /* walk decriptors and setup pipes */
850 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
851 endpoint = &iface_desc->endpoint[i].desc;
853 if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
854 ath10k_dbg(ar, ATH10K_DBG_USB,
855 "usb %s bulk ep 0x%2.2x maxpktsz %d\n",
857 (endpoint->bEndpointAddress) ?
858 "rx" : "tx", endpoint->bEndpointAddress,
859 le16_to_cpu(endpoint->wMaxPacketSize));
860 } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
861 ath10k_dbg(ar, ATH10K_DBG_USB,
862 "usb %s int 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);
868 } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
870 ath10k_dbg(ar, ATH10K_DBG_USB,
871 "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
873 (endpoint->bEndpointAddress) ?
874 "rx" : "tx", endpoint->bEndpointAddress,
875 le16_to_cpu(endpoint->wMaxPacketSize),
876 endpoint->bInterval);
881 ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
883 if (pipe_num == ATH10K_USB_PIPE_INVALID)
886 pipe = &ar_usb->pipes[pipe_num];
888 /* hmmm..pipe was already setup */
891 pipe->ar_usb = ar_usb;
892 pipe->logical_pipe_num = pipe_num;
893 pipe->ep_address = endpoint->bEndpointAddress;
894 pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
896 if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
897 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
898 pipe->usb_pipe_handle =
899 usb_rcvbulkpipe(ar_usb->udev,
902 pipe->usb_pipe_handle =
903 usb_sndbulkpipe(ar_usb->udev,
906 } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
907 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
908 pipe->usb_pipe_handle =
909 usb_rcvintpipe(ar_usb->udev,
912 pipe->usb_pipe_handle =
913 usb_sndintpipe(ar_usb->udev,
916 } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
918 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
919 pipe->usb_pipe_handle =
920 usb_rcvisocpipe(ar_usb->udev,
923 pipe->usb_pipe_handle =
924 usb_sndisocpipe(ar_usb->udev,
929 pipe->ep_desc = endpoint;
931 if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
932 pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
934 ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
942 static int ath10k_usb_create(struct ath10k *ar,
943 struct usb_interface *interface)
945 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
946 struct usb_device *dev = interface_to_usbdev(interface);
947 struct ath10k_usb_pipe *pipe;
950 usb_set_intfdata(interface, ar_usb);
951 spin_lock_init(&ar_usb->cs_lock);
953 ar_usb->interface = interface;
955 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
956 pipe = &ar_usb->pipes[i];
957 INIT_WORK(&pipe->io_complete_work,
958 ath10k_usb_io_comp_work);
959 skb_queue_head_init(&pipe->io_comp_queue);
962 ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL);
963 if (!ar_usb->diag_cmd_buffer) {
968 ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP,
970 if (!ar_usb->diag_resp_buffer) {
975 ret = ath10k_usb_setup_pipe_resources(ar, interface);
982 ath10k_usb_destroy(ar);
986 /* ath10k usb driver registered functions */
987 static int ath10k_usb_probe(struct usb_interface *interface,
988 const struct usb_device_id *id)
991 struct ath10k_usb *ar_usb;
992 struct usb_device *dev = interface_to_usbdev(interface);
993 int ret, vendor_id, product_id;
994 enum ath10k_hw_rev hw_rev;
997 /* Assumption: All USB based chipsets (so far) are QCA9377 based.
998 * If there will be newer chipsets that does not use the hw reg
999 * setup as defined in qca6174_regs and qca6174_values, this
1000 * assumption is no longer valid and hw_rev must be setup differently
1001 * depending on chipset.
1003 hw_rev = ATH10K_HW_QCA9377;
1005 ar = ath10k_core_create(sizeof(*ar_usb), &dev->dev, ATH10K_BUS_USB,
1006 hw_rev, &ath10k_usb_hif_ops);
1008 dev_err(&dev->dev, "failed to allocate core\n");
1013 vendor_id = le16_to_cpu(dev->descriptor.idVendor);
1014 product_id = le16_to_cpu(dev->descriptor.idProduct);
1016 ath10k_dbg(ar, ATH10K_DBG_BOOT,
1017 "usb new func vendor 0x%04x product 0x%04x\n",
1018 vendor_id, product_id);
1020 ar_usb = ath10k_usb_priv(ar);
1021 ret = ath10k_usb_create(ar, interface);
1026 ar->dev_id = product_id;
1027 ar->id.vendor = vendor_id;
1028 ar->id.device = product_id;
1030 /* TODO: don't know yet how to get chip_id with USB */
1032 ret = ath10k_core_register(ar, chip_id);
1034 ath10k_warn(ar, "failed to register driver core: %d\n", ret);
1035 goto err_usb_destroy;
1038 /* TODO: remove this once USB support is fully implemented */
1039 ath10k_warn(ar, "Warning: ath10k USB support is incomplete, don't expect anything to work!\n");
1044 ath10k_usb_destroy(ar);
1047 ath10k_core_destroy(ar);
1054 static void ath10k_usb_remove(struct usb_interface *interface)
1056 struct ath10k_usb *ar_usb;
1058 ar_usb = usb_get_intfdata(interface);
1062 ath10k_core_unregister(ar_usb->ar);
1063 ath10k_usb_destroy(ar_usb->ar);
1064 usb_put_dev(interface_to_usbdev(interface));
1065 ath10k_core_destroy(ar_usb->ar);
1070 static int ath10k_usb_pm_suspend(struct usb_interface *interface,
1071 pm_message_t message)
1073 struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1075 ath10k_usb_flush_all(ar_usb->ar);
1079 static int ath10k_usb_pm_resume(struct usb_interface *interface)
1081 struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1082 struct ath10k *ar = ar_usb->ar;
1084 ath10k_usb_post_recv_transfers(ar,
1085 &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
1092 #define ath10k_usb_pm_suspend NULL
1093 #define ath10k_usb_pm_resume NULL
1097 /* table of devices that work with this driver */
1098 static struct usb_device_id ath10k_usb_ids[] = {
1099 {USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */
1100 { /* Terminating entry */ },
1103 MODULE_DEVICE_TABLE(usb, ath10k_usb_ids);
1105 static struct usb_driver ath10k_usb_driver = {
1106 .name = "ath10k_usb",
1107 .probe = ath10k_usb_probe,
1108 .suspend = ath10k_usb_pm_suspend,
1109 .resume = ath10k_usb_pm_resume,
1110 .disconnect = ath10k_usb_remove,
1111 .id_table = ath10k_usb_ids,
1112 .supports_autosuspend = true,
1113 .disable_hub_initiated_lpm = 1,
1116 module_usb_driver(ath10k_usb_driver);
1118 MODULE_AUTHOR("Atheros Communications, Inc.");
1119 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices");
1120 MODULE_LICENSE("Dual BSD/GPL");