Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: ISC
0002 /*
0003  * Copyright (c) 2007-2011 Atheros Communications Inc.
0004  * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
0005  * Copyright (c) 2016-2017 Erik Stromdahl <erik.stromdahl@gmail.com>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/usb.h>
0010 
0011 #include "debug.h"
0012 #include "core.h"
0013 #include "bmi.h"
0014 #include "hif.h"
0015 #include "htc.h"
0016 #include "usb.h"
0017 
0018 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
0019                        struct ath10k_usb_pipe *recv_pipe);
0020 
0021 /* inlined helper functions */
0022 
0023 static inline enum ath10k_htc_ep_id
0024 eid_from_htc_hdr(struct ath10k_htc_hdr *htc_hdr)
0025 {
0026     return (enum ath10k_htc_ep_id)htc_hdr->eid;
0027 }
0028 
0029 static inline bool is_trailer_only_msg(struct ath10k_htc_hdr *htc_hdr)
0030 {
0031     return __le16_to_cpu(htc_hdr->len) == htc_hdr->trailer_len;
0032 }
0033 
0034 /* pipe/urb operations */
0035 static struct ath10k_urb_context *
0036 ath10k_usb_alloc_urb_from_pipe(struct ath10k_usb_pipe *pipe)
0037 {
0038     struct ath10k_urb_context *urb_context = NULL;
0039     unsigned long flags;
0040 
0041     /* bail if this pipe is not initialized */
0042     if (!pipe->ar_usb)
0043         return NULL;
0044 
0045     spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
0046     if (!list_empty(&pipe->urb_list_head)) {
0047         urb_context = list_first_entry(&pipe->urb_list_head,
0048                            struct ath10k_urb_context, link);
0049         list_del(&urb_context->link);
0050         pipe->urb_cnt--;
0051     }
0052     spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
0053 
0054     return urb_context;
0055 }
0056 
0057 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
0058                     struct ath10k_urb_context *urb_context)
0059 {
0060     unsigned long flags;
0061 
0062     /* bail if this pipe is not initialized */
0063     if (!pipe->ar_usb)
0064         return;
0065 
0066     spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
0067 
0068     pipe->urb_cnt++;
0069     list_add(&urb_context->link, &pipe->urb_list_head);
0070 
0071     spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
0072 }
0073 
0074 static void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context)
0075 {
0076     dev_kfree_skb(urb_context->skb);
0077     urb_context->skb = NULL;
0078 
0079     ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
0080 }
0081 
0082 static void ath10k_usb_free_pipe_resources(struct ath10k *ar,
0083                        struct ath10k_usb_pipe *pipe)
0084 {
0085     struct ath10k_urb_context *urb_context;
0086 
0087     if (!pipe->ar_usb) {
0088         /* nothing allocated for this pipe */
0089         return;
0090     }
0091 
0092     ath10k_dbg(ar, ATH10K_DBG_USB,
0093            "usb free resources lpipe %d hpipe 0x%x urbs %d avail %d\n",
0094            pipe->logical_pipe_num, pipe->usb_pipe_handle,
0095            pipe->urb_alloc, pipe->urb_cnt);
0096 
0097     if (pipe->urb_alloc != pipe->urb_cnt) {
0098         ath10k_dbg(ar, ATH10K_DBG_USB,
0099                "usb urb leak lpipe %d hpipe 0x%x urbs %d avail %d\n",
0100                pipe->logical_pipe_num, pipe->usb_pipe_handle,
0101                pipe->urb_alloc, pipe->urb_cnt);
0102     }
0103 
0104     for (;;) {
0105         urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
0106 
0107         if (!urb_context)
0108             break;
0109 
0110         kfree(urb_context);
0111     }
0112 }
0113 
0114 static void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar)
0115 {
0116     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0117     int i;
0118 
0119     for (i = 0; i < ATH10K_USB_PIPE_MAX; i++)
0120         ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]);
0121 }
0122 
0123 /* hif usb rx/tx completion functions */
0124 
0125 static void ath10k_usb_recv_complete(struct urb *urb)
0126 {
0127     struct ath10k_urb_context *urb_context = urb->context;
0128     struct ath10k_usb_pipe *pipe = urb_context->pipe;
0129     struct ath10k *ar = pipe->ar_usb->ar;
0130     struct sk_buff *skb;
0131     int status = 0;
0132 
0133     ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
0134            "usb recv pipe %d stat %d len %d urb 0x%pK\n",
0135            pipe->logical_pipe_num, urb->status, urb->actual_length,
0136            urb);
0137 
0138     if (urb->status != 0) {
0139         status = -EIO;
0140         switch (urb->status) {
0141         case -ECONNRESET:
0142         case -ENOENT:
0143         case -ESHUTDOWN:
0144             /* no need to spew these errors when device
0145              * removed or urb killed due to driver shutdown
0146              */
0147             status = -ECANCELED;
0148             break;
0149         default:
0150             ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
0151                    "usb recv pipe %d ep 0x%2.2x failed: %d\n",
0152                    pipe->logical_pipe_num,
0153                    pipe->ep_address, urb->status);
0154             break;
0155         }
0156         goto cleanup_recv_urb;
0157     }
0158 
0159     if (urb->actual_length == 0)
0160         goto cleanup_recv_urb;
0161 
0162     skb = urb_context->skb;
0163 
0164     /* we are going to pass it up */
0165     urb_context->skb = NULL;
0166     skb_put(skb, urb->actual_length);
0167 
0168     /* note: queue implements a lock */
0169     skb_queue_tail(&pipe->io_comp_queue, skb);
0170     schedule_work(&pipe->io_complete_work);
0171 
0172 cleanup_recv_urb:
0173     ath10k_usb_cleanup_recv_urb(urb_context);
0174 
0175     if (status == 0 &&
0176         pipe->urb_cnt >= pipe->urb_cnt_thresh) {
0177         /* our free urbs are piling up, post more transfers */
0178         ath10k_usb_post_recv_transfers(ar, pipe);
0179     }
0180 }
0181 
0182 static void ath10k_usb_transmit_complete(struct urb *urb)
0183 {
0184     struct ath10k_urb_context *urb_context = urb->context;
0185     struct ath10k_usb_pipe *pipe = urb_context->pipe;
0186     struct ath10k *ar = pipe->ar_usb->ar;
0187     struct sk_buff *skb;
0188 
0189     if (urb->status != 0) {
0190         ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
0191                "pipe: %d, failed:%d\n",
0192                pipe->logical_pipe_num, urb->status);
0193     }
0194 
0195     skb = urb_context->skb;
0196     urb_context->skb = NULL;
0197     ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
0198 
0199     /* note: queue implements a lock */
0200     skb_queue_tail(&pipe->io_comp_queue, skb);
0201     schedule_work(&pipe->io_complete_work);
0202 }
0203 
0204 /* pipe operations */
0205 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
0206                        struct ath10k_usb_pipe *recv_pipe)
0207 {
0208     struct ath10k_urb_context *urb_context;
0209     struct urb *urb;
0210     int usb_status;
0211 
0212     for (;;) {
0213         urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe);
0214         if (!urb_context)
0215             break;
0216 
0217         urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE);
0218         if (!urb_context->skb)
0219             goto err;
0220 
0221         urb = usb_alloc_urb(0, GFP_ATOMIC);
0222         if (!urb)
0223             goto err;
0224 
0225         usb_fill_bulk_urb(urb,
0226                   recv_pipe->ar_usb->udev,
0227                   recv_pipe->usb_pipe_handle,
0228                   urb_context->skb->data,
0229                   ATH10K_USB_RX_BUFFER_SIZE,
0230                   ath10k_usb_recv_complete, urb_context);
0231 
0232         ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
0233                "usb bulk recv submit %d 0x%x ep 0x%2.2x len %d buf 0x%pK\n",
0234                recv_pipe->logical_pipe_num,
0235                recv_pipe->usb_pipe_handle, recv_pipe->ep_address,
0236                ATH10K_USB_RX_BUFFER_SIZE, urb_context->skb);
0237 
0238         usb_anchor_urb(urb, &recv_pipe->urb_submitted);
0239         usb_status = usb_submit_urb(urb, GFP_ATOMIC);
0240 
0241         if (usb_status) {
0242             ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
0243                    "usb bulk recv failed: %d\n",
0244                    usb_status);
0245             usb_unanchor_urb(urb);
0246             usb_free_urb(urb);
0247             goto err;
0248         }
0249         usb_free_urb(urb);
0250     }
0251 
0252     return;
0253 
0254 err:
0255     ath10k_usb_cleanup_recv_urb(urb_context);
0256 }
0257 
0258 static void ath10k_usb_flush_all(struct ath10k *ar)
0259 {
0260     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0261     int i;
0262 
0263     for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
0264         if (ar_usb->pipes[i].ar_usb) {
0265             usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted);
0266             cancel_work_sync(&ar_usb->pipes[i].io_complete_work);
0267         }
0268     }
0269 }
0270 
0271 static void ath10k_usb_start_recv_pipes(struct ath10k *ar)
0272 {
0273     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0274 
0275     ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
0276 
0277     ath10k_usb_post_recv_transfers(ar,
0278                        &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
0279 }
0280 
0281 static void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb)
0282 {
0283     struct ath10k_htc_hdr *htc_hdr;
0284     struct ath10k_htc_ep *ep;
0285 
0286     htc_hdr = (struct ath10k_htc_hdr *)skb->data;
0287     ep = &ar->htc.endpoint[htc_hdr->eid];
0288     ath10k_htc_notify_tx_completion(ep, skb);
0289     /* The TX complete handler now owns the skb... */
0290 }
0291 
0292 static void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb)
0293 {
0294     struct ath10k_htc *htc = &ar->htc;
0295     struct ath10k_htc_hdr *htc_hdr;
0296     enum ath10k_htc_ep_id eid;
0297     struct ath10k_htc_ep *ep;
0298     u16 payload_len;
0299     u8 *trailer;
0300     int ret;
0301 
0302     htc_hdr = (struct ath10k_htc_hdr *)skb->data;
0303     eid = eid_from_htc_hdr(htc_hdr);
0304     ep = &ar->htc.endpoint[eid];
0305 
0306     if (ep->service_id == 0) {
0307         ath10k_warn(ar, "ep %d is not connected\n", eid);
0308         goto out_free_skb;
0309     }
0310 
0311     payload_len = le16_to_cpu(htc_hdr->len);
0312     if (!payload_len) {
0313         ath10k_warn(ar, "zero length frame received, firmware crashed?\n");
0314         goto out_free_skb;
0315     }
0316 
0317     if (payload_len < htc_hdr->trailer_len) {
0318         ath10k_warn(ar, "malformed frame received, firmware crashed?\n");
0319         goto out_free_skb;
0320     }
0321 
0322     if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) {
0323         trailer = skb->data + sizeof(*htc_hdr) + payload_len -
0324               htc_hdr->trailer_len;
0325 
0326         ret = ath10k_htc_process_trailer(htc,
0327                          trailer,
0328                          htc_hdr->trailer_len,
0329                          eid,
0330                          NULL,
0331                          NULL);
0332         if (ret)
0333             goto out_free_skb;
0334 
0335         if (is_trailer_only_msg(htc_hdr))
0336             goto out_free_skb;
0337 
0338         /* strip off the trailer from the skb since it should not
0339          * be passed on to upper layers
0340          */
0341         skb_trim(skb, skb->len - htc_hdr->trailer_len);
0342     }
0343 
0344     skb_pull(skb, sizeof(*htc_hdr));
0345     ep->ep_ops.ep_rx_complete(ar, skb);
0346     /* The RX complete handler now owns the skb... */
0347 
0348     if (test_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags)) {
0349         local_bh_disable();
0350         napi_schedule(&ar->napi);
0351         local_bh_enable();
0352     }
0353 
0354     return;
0355 
0356 out_free_skb:
0357     dev_kfree_skb(skb);
0358 }
0359 
0360 static void ath10k_usb_io_comp_work(struct work_struct *work)
0361 {
0362     struct ath10k_usb_pipe *pipe = container_of(work,
0363                             struct ath10k_usb_pipe,
0364                             io_complete_work);
0365     struct ath10k *ar = pipe->ar_usb->ar;
0366     struct sk_buff *skb;
0367 
0368     while ((skb = skb_dequeue(&pipe->io_comp_queue))) {
0369         if (pipe->flags & ATH10K_USB_PIPE_FLAG_TX)
0370             ath10k_usb_tx_complete(ar, skb);
0371         else
0372             ath10k_usb_rx_complete(ar, skb);
0373     }
0374 }
0375 
0376 #define ATH10K_USB_MAX_DIAG_CMD (sizeof(struct ath10k_usb_ctrl_diag_cmd_write))
0377 #define ATH10K_USB_MAX_DIAG_RESP (sizeof(struct ath10k_usb_ctrl_diag_resp_read))
0378 
0379 static void ath10k_usb_destroy(struct ath10k *ar)
0380 {
0381     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0382 
0383     ath10k_usb_flush_all(ar);
0384     ath10k_usb_cleanup_pipe_resources(ar);
0385     usb_set_intfdata(ar_usb->interface, NULL);
0386 
0387     kfree(ar_usb->diag_cmd_buffer);
0388     kfree(ar_usb->diag_resp_buffer);
0389 }
0390 
0391 static int ath10k_usb_hif_start(struct ath10k *ar)
0392 {
0393     int i;
0394     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0395 
0396     ath10k_core_napi_enable(ar);
0397     ath10k_usb_start_recv_pipes(ar);
0398 
0399     /* set the TX resource avail threshold for each TX pipe */
0400     for (i = ATH10K_USB_PIPE_TX_CTRL;
0401          i <= ATH10K_USB_PIPE_TX_DATA_HP; i++) {
0402         ar_usb->pipes[i].urb_cnt_thresh =
0403             ar_usb->pipes[i].urb_alloc / 2;
0404     }
0405 
0406     return 0;
0407 }
0408 
0409 static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
0410                 struct ath10k_hif_sg_item *items, int n_items)
0411 {
0412     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0413     struct ath10k_usb_pipe *pipe = &ar_usb->pipes[pipe_id];
0414     struct ath10k_urb_context *urb_context;
0415     struct sk_buff *skb;
0416     struct urb *urb;
0417     int ret, i;
0418 
0419     for (i = 0; i < n_items; i++) {
0420         urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
0421         if (!urb_context) {
0422             ret = -ENOMEM;
0423             goto err;
0424         }
0425 
0426         skb = items[i].transfer_context;
0427         urb_context->skb = skb;
0428 
0429         urb = usb_alloc_urb(0, GFP_ATOMIC);
0430         if (!urb) {
0431             ret = -ENOMEM;
0432             goto err_free_urb_to_pipe;
0433         }
0434 
0435         usb_fill_bulk_urb(urb,
0436                   ar_usb->udev,
0437                   pipe->usb_pipe_handle,
0438                   skb->data,
0439                   skb->len,
0440                   ath10k_usb_transmit_complete, urb_context);
0441 
0442         if (!(skb->len % pipe->max_packet_size)) {
0443             /* hit a max packet boundary on this pipe */
0444             urb->transfer_flags |= URB_ZERO_PACKET;
0445         }
0446 
0447         usb_anchor_urb(urb, &pipe->urb_submitted);
0448         ret = usb_submit_urb(urb, GFP_ATOMIC);
0449         if (ret) {
0450             ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
0451                    "usb bulk transmit failed: %d\n", ret);
0452             usb_unanchor_urb(urb);
0453             usb_free_urb(urb);
0454             ret = -EINVAL;
0455             goto err_free_urb_to_pipe;
0456         }
0457 
0458         usb_free_urb(urb);
0459     }
0460 
0461     return 0;
0462 
0463 err_free_urb_to_pipe:
0464     ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
0465 err:
0466     return ret;
0467 }
0468 
0469 static void ath10k_usb_hif_stop(struct ath10k *ar)
0470 {
0471     ath10k_usb_flush_all(ar);
0472     ath10k_core_napi_sync_disable(ar);
0473 }
0474 
0475 static u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id)
0476 {
0477     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0478 
0479     return ar_usb->pipes[pipe_id].urb_cnt;
0480 }
0481 
0482 static int ath10k_usb_submit_ctrl_out(struct ath10k *ar,
0483                       u8 req, u16 value, u16 index, void *data,
0484                       u32 size)
0485 {
0486     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0487     u8 *buf = NULL;
0488     int ret;
0489 
0490     if (size > 0) {
0491         buf = kmemdup(data, size, GFP_KERNEL);
0492         if (!buf)
0493             return -ENOMEM;
0494     }
0495 
0496     /* note: if successful returns number of bytes transferred */
0497     ret = usb_control_msg(ar_usb->udev,
0498                   usb_sndctrlpipe(ar_usb->udev, 0),
0499                   req,
0500                   USB_DIR_OUT | USB_TYPE_VENDOR |
0501                   USB_RECIP_DEVICE, value, index, buf,
0502                   size, 1000);
0503 
0504     if (ret < 0) {
0505         ath10k_warn(ar, "Failed to submit usb control message: %d\n",
0506                 ret);
0507         kfree(buf);
0508         return ret;
0509     }
0510 
0511     kfree(buf);
0512 
0513     return 0;
0514 }
0515 
0516 static int ath10k_usb_submit_ctrl_in(struct ath10k *ar,
0517                      u8 req, u16 value, u16 index, void *data,
0518                      u32 size)
0519 {
0520     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0521     u8 *buf = NULL;
0522     int ret;
0523 
0524     if (size > 0) {
0525         buf = kmalloc(size, GFP_KERNEL);
0526         if (!buf)
0527             return -ENOMEM;
0528     }
0529 
0530     /* note: if successful returns number of bytes transferred */
0531     ret = usb_control_msg(ar_usb->udev,
0532                   usb_rcvctrlpipe(ar_usb->udev, 0),
0533                   req,
0534                   USB_DIR_IN | USB_TYPE_VENDOR |
0535                   USB_RECIP_DEVICE, value, index, buf,
0536                   size, 2000);
0537 
0538     if (ret < 0) {
0539         ath10k_warn(ar, "Failed to read usb control message: %d\n",
0540                 ret);
0541         kfree(buf);
0542         return ret;
0543     }
0544 
0545     memcpy((u8 *)data, buf, size);
0546 
0547     kfree(buf);
0548 
0549     return 0;
0550 }
0551 
0552 static int ath10k_usb_ctrl_msg_exchange(struct ath10k *ar,
0553                     u8 req_val, u8 *req_buf, u32 req_len,
0554                     u8 resp_val, u8 *resp_buf,
0555                     u32 *resp_len)
0556 {
0557     int ret;
0558 
0559     /* send command */
0560     ret = ath10k_usb_submit_ctrl_out(ar, req_val, 0, 0,
0561                      req_buf, req_len);
0562     if (ret)
0563         goto err;
0564 
0565     /* get response */
0566     if (resp_buf) {
0567         ret = ath10k_usb_submit_ctrl_in(ar, resp_val, 0, 0,
0568                         resp_buf, *resp_len);
0569         if (ret)
0570             goto err;
0571     }
0572 
0573     return 0;
0574 err:
0575     return ret;
0576 }
0577 
0578 static int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
0579                     size_t buf_len)
0580 {
0581     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0582     struct ath10k_usb_ctrl_diag_cmd_read *cmd;
0583     u32 resp_len;
0584     int ret;
0585 
0586     if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read))
0587         return -EINVAL;
0588 
0589     cmd = (struct ath10k_usb_ctrl_diag_cmd_read *)ar_usb->diag_cmd_buffer;
0590     memset(cmd, 0, sizeof(*cmd));
0591     cmd->cmd = ATH10K_USB_CTRL_DIAG_CC_READ;
0592     cmd->address = cpu_to_le32(address);
0593     resp_len = sizeof(struct ath10k_usb_ctrl_diag_resp_read);
0594 
0595     ret = ath10k_usb_ctrl_msg_exchange(ar,
0596                        ATH10K_USB_CONTROL_REQ_DIAG_CMD,
0597                        (u8 *)cmd,
0598                        sizeof(*cmd),
0599                        ATH10K_USB_CONTROL_REQ_DIAG_RESP,
0600                        ar_usb->diag_resp_buffer, &resp_len);
0601     if (ret)
0602         return ret;
0603 
0604     if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read))
0605         return -EMSGSIZE;
0606 
0607     memcpy(buf, ar_usb->diag_resp_buffer,
0608            sizeof(struct ath10k_usb_ctrl_diag_resp_read));
0609 
0610     return 0;
0611 }
0612 
0613 static int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address,
0614                      const void *data, int nbytes)
0615 {
0616     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0617     struct ath10k_usb_ctrl_diag_cmd_write *cmd;
0618     int ret;
0619 
0620     if (nbytes != sizeof(cmd->value))
0621         return -EINVAL;
0622 
0623     cmd = (struct ath10k_usb_ctrl_diag_cmd_write *)ar_usb->diag_cmd_buffer;
0624     memset(cmd, 0, sizeof(*cmd));
0625     cmd->cmd = cpu_to_le32(ATH10K_USB_CTRL_DIAG_CC_WRITE);
0626     cmd->address = cpu_to_le32(address);
0627     memcpy(&cmd->value, data, nbytes);
0628 
0629     ret = ath10k_usb_ctrl_msg_exchange(ar,
0630                        ATH10K_USB_CONTROL_REQ_DIAG_CMD,
0631                        (u8 *)cmd,
0632                        sizeof(*cmd),
0633                        0, NULL, NULL);
0634     if (ret)
0635         return ret;
0636 
0637     return 0;
0638 }
0639 
0640 static int ath10k_usb_bmi_exchange_msg(struct ath10k *ar,
0641                        void *req, u32 req_len,
0642                        void *resp, u32 *resp_len)
0643 {
0644     int ret;
0645 
0646     if (req) {
0647         ret = ath10k_usb_submit_ctrl_out(ar,
0648                          ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD,
0649                          0, 0, req, req_len);
0650         if (ret) {
0651             ath10k_warn(ar,
0652                     "unable to send the bmi data to the device: %d\n",
0653                     ret);
0654             return ret;
0655         }
0656     }
0657 
0658     if (resp) {
0659         ret = ath10k_usb_submit_ctrl_in(ar,
0660                         ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP,
0661                         0, 0, resp, *resp_len);
0662         if (ret) {
0663             ath10k_warn(ar,
0664                     "Unable to read the bmi data from the device: %d\n",
0665                     ret);
0666             return ret;
0667         }
0668     }
0669 
0670     return 0;
0671 }
0672 
0673 static void ath10k_usb_hif_get_default_pipe(struct ath10k *ar,
0674                         u8 *ul_pipe, u8 *dl_pipe)
0675 {
0676     *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
0677     *dl_pipe = ATH10K_USB_PIPE_RX_CTRL;
0678 }
0679 
0680 static int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id,
0681                           u8 *ul_pipe, u8 *dl_pipe)
0682 {
0683     switch (svc_id) {
0684     case ATH10K_HTC_SVC_ID_RSVD_CTRL:
0685     case ATH10K_HTC_SVC_ID_WMI_CONTROL:
0686         *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
0687         /* due to large control packets, shift to data pipe */
0688         *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
0689         break;
0690     case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
0691         *ul_pipe = ATH10K_USB_PIPE_TX_DATA_LP;
0692         /* Disable rxdata2 directly, it will be enabled
0693          * if FW enable rxdata2
0694          */
0695         *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
0696         break;
0697     default:
0698         return -EPERM;
0699     }
0700 
0701     return 0;
0702 }
0703 
0704 static int ath10k_usb_hif_power_up(struct ath10k *ar,
0705                    enum ath10k_firmware_mode fw_mode)
0706 {
0707     return 0;
0708 }
0709 
0710 static void ath10k_usb_hif_power_down(struct ath10k *ar)
0711 {
0712     ath10k_usb_flush_all(ar);
0713 }
0714 
0715 #ifdef CONFIG_PM
0716 
0717 static int ath10k_usb_hif_suspend(struct ath10k *ar)
0718 {
0719     return -EOPNOTSUPP;
0720 }
0721 
0722 static int ath10k_usb_hif_resume(struct ath10k *ar)
0723 {
0724     return -EOPNOTSUPP;
0725 }
0726 #endif
0727 
0728 static const struct ath10k_hif_ops ath10k_usb_hif_ops = {
0729     .tx_sg          = ath10k_usb_hif_tx_sg,
0730     .diag_read      = ath10k_usb_hif_diag_read,
0731     .diag_write     = ath10k_usb_hif_diag_write,
0732     .exchange_bmi_msg   = ath10k_usb_bmi_exchange_msg,
0733     .start          = ath10k_usb_hif_start,
0734     .stop           = ath10k_usb_hif_stop,
0735     .map_service_to_pipe    = ath10k_usb_hif_map_service_to_pipe,
0736     .get_default_pipe   = ath10k_usb_hif_get_default_pipe,
0737     .get_free_queue_number  = ath10k_usb_hif_get_free_queue_number,
0738     .power_up       = ath10k_usb_hif_power_up,
0739     .power_down     = ath10k_usb_hif_power_down,
0740 #ifdef CONFIG_PM
0741     .suspend        = ath10k_usb_hif_suspend,
0742     .resume         = ath10k_usb_hif_resume,
0743 #endif
0744 };
0745 
0746 static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
0747 {
0748     u8 pipe_num = ATH10K_USB_PIPE_INVALID;
0749 
0750     switch (ep_address) {
0751     case ATH10K_USB_EP_ADDR_APP_CTRL_IN:
0752         pipe_num = ATH10K_USB_PIPE_RX_CTRL;
0753         *urb_count = RX_URB_COUNT;
0754         break;
0755     case ATH10K_USB_EP_ADDR_APP_DATA_IN:
0756         pipe_num = ATH10K_USB_PIPE_RX_DATA;
0757         *urb_count = RX_URB_COUNT;
0758         break;
0759     case ATH10K_USB_EP_ADDR_APP_INT_IN:
0760         pipe_num = ATH10K_USB_PIPE_RX_INT;
0761         *urb_count = RX_URB_COUNT;
0762         break;
0763     case ATH10K_USB_EP_ADDR_APP_DATA2_IN:
0764         pipe_num = ATH10K_USB_PIPE_RX_DATA2;
0765         *urb_count = RX_URB_COUNT;
0766         break;
0767     case ATH10K_USB_EP_ADDR_APP_CTRL_OUT:
0768         pipe_num = ATH10K_USB_PIPE_TX_CTRL;
0769         *urb_count = TX_URB_COUNT;
0770         break;
0771     case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT:
0772         pipe_num = ATH10K_USB_PIPE_TX_DATA_LP;
0773         *urb_count = TX_URB_COUNT;
0774         break;
0775     case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT:
0776         pipe_num = ATH10K_USB_PIPE_TX_DATA_MP;
0777         *urb_count = TX_URB_COUNT;
0778         break;
0779     case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT:
0780         pipe_num = ATH10K_USB_PIPE_TX_DATA_HP;
0781         *urb_count = TX_URB_COUNT;
0782         break;
0783     default:
0784         /* note: there may be endpoints not currently used */
0785         break;
0786     }
0787 
0788     return pipe_num;
0789 }
0790 
0791 static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
0792                        struct ath10k_usb_pipe *pipe,
0793                        int urb_cnt)
0794 {
0795     struct ath10k_urb_context *urb_context;
0796     int i;
0797 
0798     INIT_LIST_HEAD(&pipe->urb_list_head);
0799     init_usb_anchor(&pipe->urb_submitted);
0800 
0801     for (i = 0; i < urb_cnt; i++) {
0802         urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
0803         if (!urb_context)
0804             return -ENOMEM;
0805 
0806         urb_context->pipe = pipe;
0807 
0808         /* we are only allocate the urb contexts here, the actual URB
0809          * is allocated from the kernel as needed to do a transaction
0810          */
0811         pipe->urb_alloc++;
0812         ath10k_usb_free_urb_to_pipe(pipe, urb_context);
0813     }
0814 
0815     ath10k_dbg(ar, ATH10K_DBG_USB,
0816            "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n",
0817            pipe->logical_pipe_num, pipe->usb_pipe_handle,
0818            pipe->urb_alloc);
0819 
0820     return 0;
0821 }
0822 
0823 static int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
0824                        struct usb_interface *interface)
0825 {
0826     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0827     struct usb_host_interface *iface_desc = interface->cur_altsetting;
0828     struct usb_endpoint_descriptor *endpoint;
0829     struct ath10k_usb_pipe *pipe;
0830     int ret, i, urbcount;
0831     u8 pipe_num;
0832 
0833     ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
0834 
0835     /* walk descriptors and setup pipes */
0836     for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
0837         endpoint = &iface_desc->endpoint[i].desc;
0838 
0839         if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
0840             ath10k_dbg(ar, ATH10K_DBG_USB,
0841                    "usb %s bulk ep 0x%2.2x maxpktsz %d\n",
0842                    ATH10K_USB_IS_DIR_IN
0843                    (endpoint->bEndpointAddress) ?
0844                    "rx" : "tx", endpoint->bEndpointAddress,
0845                    le16_to_cpu(endpoint->wMaxPacketSize));
0846         } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
0847             ath10k_dbg(ar, ATH10K_DBG_USB,
0848                    "usb %s int ep 0x%2.2x maxpktsz %d interval %d\n",
0849                    ATH10K_USB_IS_DIR_IN
0850                    (endpoint->bEndpointAddress) ?
0851                    "rx" : "tx", endpoint->bEndpointAddress,
0852                    le16_to_cpu(endpoint->wMaxPacketSize),
0853                    endpoint->bInterval);
0854         } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
0855             /* TODO for ISO */
0856             ath10k_dbg(ar, ATH10K_DBG_USB,
0857                    "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
0858                    ATH10K_USB_IS_DIR_IN
0859                    (endpoint->bEndpointAddress) ?
0860                    "rx" : "tx", endpoint->bEndpointAddress,
0861                    le16_to_cpu(endpoint->wMaxPacketSize),
0862                    endpoint->bInterval);
0863         }
0864 
0865         /* Ignore broken descriptors. */
0866         if (usb_endpoint_maxp(endpoint) == 0)
0867             continue;
0868 
0869         urbcount = 0;
0870 
0871         pipe_num =
0872             ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
0873                             &urbcount);
0874         if (pipe_num == ATH10K_USB_PIPE_INVALID)
0875             continue;
0876 
0877         pipe = &ar_usb->pipes[pipe_num];
0878         if (pipe->ar_usb)
0879             /* hmmm..pipe was already setup */
0880             continue;
0881 
0882         pipe->ar_usb = ar_usb;
0883         pipe->logical_pipe_num = pipe_num;
0884         pipe->ep_address = endpoint->bEndpointAddress;
0885         pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
0886 
0887         if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
0888             if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
0889                 pipe->usb_pipe_handle =
0890                     usb_rcvbulkpipe(ar_usb->udev,
0891                             pipe->ep_address);
0892             } else {
0893                 pipe->usb_pipe_handle =
0894                     usb_sndbulkpipe(ar_usb->udev,
0895                             pipe->ep_address);
0896             }
0897         } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
0898             if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
0899                 pipe->usb_pipe_handle =
0900                     usb_rcvintpipe(ar_usb->udev,
0901                            pipe->ep_address);
0902             } else {
0903                 pipe->usb_pipe_handle =
0904                     usb_sndintpipe(ar_usb->udev,
0905                            pipe->ep_address);
0906             }
0907         } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
0908             /* TODO for ISO */
0909             if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
0910                 pipe->usb_pipe_handle =
0911                     usb_rcvisocpipe(ar_usb->udev,
0912                             pipe->ep_address);
0913             } else {
0914                 pipe->usb_pipe_handle =
0915                     usb_sndisocpipe(ar_usb->udev,
0916                             pipe->ep_address);
0917             }
0918         }
0919 
0920         pipe->ep_desc = endpoint;
0921 
0922         if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
0923             pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
0924 
0925         ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
0926         if (ret)
0927             return ret;
0928     }
0929 
0930     return 0;
0931 }
0932 
0933 static int ath10k_usb_create(struct ath10k *ar,
0934                  struct usb_interface *interface)
0935 {
0936     struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
0937     struct usb_device *dev = interface_to_usbdev(interface);
0938     struct ath10k_usb_pipe *pipe;
0939     int ret, i;
0940 
0941     usb_set_intfdata(interface, ar_usb);
0942     spin_lock_init(&ar_usb->cs_lock);
0943     ar_usb->udev = dev;
0944     ar_usb->interface = interface;
0945 
0946     for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
0947         pipe = &ar_usb->pipes[i];
0948         INIT_WORK(&pipe->io_complete_work,
0949               ath10k_usb_io_comp_work);
0950         skb_queue_head_init(&pipe->io_comp_queue);
0951     }
0952 
0953     ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL);
0954     if (!ar_usb->diag_cmd_buffer) {
0955         ret = -ENOMEM;
0956         goto err;
0957     }
0958 
0959     ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP,
0960                        GFP_KERNEL);
0961     if (!ar_usb->diag_resp_buffer) {
0962         ret = -ENOMEM;
0963         goto err;
0964     }
0965 
0966     ret = ath10k_usb_setup_pipe_resources(ar, interface);
0967     if (ret)
0968         goto err;
0969 
0970     return 0;
0971 
0972 err:
0973     ath10k_usb_destroy(ar);
0974     return ret;
0975 }
0976 
0977 static int ath10k_usb_napi_poll(struct napi_struct *ctx, int budget)
0978 {
0979     struct ath10k *ar = container_of(ctx, struct ath10k, napi);
0980     int done;
0981 
0982     done = ath10k_htt_rx_hl_indication(ar, budget);
0983     ath10k_dbg(ar, ATH10K_DBG_USB, "napi poll: done: %d, budget:%d\n", done, budget);
0984 
0985     if (done < budget)
0986         napi_complete_done(ctx, done);
0987 
0988     return done;
0989 }
0990 
0991 /* ath10k usb driver registered functions */
0992 static int ath10k_usb_probe(struct usb_interface *interface,
0993                 const struct usb_device_id *id)
0994 {
0995     struct ath10k *ar;
0996     struct ath10k_usb *ar_usb;
0997     struct usb_device *dev = interface_to_usbdev(interface);
0998     int ret, vendor_id, product_id;
0999     enum ath10k_hw_rev hw_rev;
1000     struct ath10k_bus_params bus_params = {};
1001 
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.
1007      */
1008     hw_rev = ATH10K_HW_QCA9377;
1009 
1010     ar = ath10k_core_create(sizeof(*ar_usb), &dev->dev, ATH10K_BUS_USB,
1011                 hw_rev, &ath10k_usb_hif_ops);
1012     if (!ar) {
1013         dev_err(&dev->dev, "failed to allocate core\n");
1014         return -ENOMEM;
1015     }
1016 
1017     netif_napi_add(&ar->napi_dev, &ar->napi, ath10k_usb_napi_poll,
1018                NAPI_POLL_WEIGHT);
1019 
1020     usb_get_dev(dev);
1021     vendor_id = le16_to_cpu(dev->descriptor.idVendor);
1022     product_id = le16_to_cpu(dev->descriptor.idProduct);
1023 
1024     ath10k_dbg(ar, ATH10K_DBG_BOOT,
1025            "usb new func vendor 0x%04x product 0x%04x\n",
1026            vendor_id, product_id);
1027 
1028     ar_usb = ath10k_usb_priv(ar);
1029     ret = ath10k_usb_create(ar, interface);
1030     if (ret)
1031         goto err;
1032     ar_usb->ar = ar;
1033 
1034     ar->dev_id = product_id;
1035     ar->id.vendor = vendor_id;
1036     ar->id.device = product_id;
1037 
1038     bus_params.dev_type = ATH10K_DEV_TYPE_HL;
1039     /* TODO: don't know yet how to get chip_id with USB */
1040     bus_params.chip_id = 0;
1041     bus_params.hl_msdu_ids = true;
1042     ret = ath10k_core_register(ar, &bus_params);
1043     if (ret) {
1044         ath10k_warn(ar, "failed to register driver core: %d\n", ret);
1045         goto err_usb_destroy;
1046     }
1047 
1048     /* TODO: remove this once USB support is fully implemented */
1049     ath10k_warn(ar, "Warning: ath10k USB support is incomplete, don't expect anything to work!\n");
1050 
1051     return 0;
1052 
1053 err_usb_destroy:
1054     ath10k_usb_destroy(ar);
1055 
1056 err:
1057     ath10k_core_destroy(ar);
1058 
1059     usb_put_dev(dev);
1060 
1061     return ret;
1062 }
1063 
1064 static void ath10k_usb_remove(struct usb_interface *interface)
1065 {
1066     struct ath10k_usb *ar_usb;
1067 
1068     ar_usb = usb_get_intfdata(interface);
1069     if (!ar_usb)
1070         return;
1071 
1072     ath10k_core_unregister(ar_usb->ar);
1073     netif_napi_del(&ar_usb->ar->napi);
1074     ath10k_usb_destroy(ar_usb->ar);
1075     usb_put_dev(interface_to_usbdev(interface));
1076     ath10k_core_destroy(ar_usb->ar);
1077 }
1078 
1079 #ifdef CONFIG_PM
1080 
1081 static int ath10k_usb_pm_suspend(struct usb_interface *interface,
1082                  pm_message_t message)
1083 {
1084     struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1085 
1086     ath10k_usb_flush_all(ar_usb->ar);
1087     return 0;
1088 }
1089 
1090 static int ath10k_usb_pm_resume(struct usb_interface *interface)
1091 {
1092     struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1093     struct ath10k *ar = ar_usb->ar;
1094 
1095     ath10k_usb_post_recv_transfers(ar,
1096                        &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
1097 
1098     return 0;
1099 }
1100 
1101 #else
1102 
1103 #define ath10k_usb_pm_suspend NULL
1104 #define ath10k_usb_pm_resume NULL
1105 
1106 #endif
1107 
1108 /* table of devices that work with this driver */
1109 static struct usb_device_id ath10k_usb_ids[] = {
1110     {USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */
1111     { /* Terminating entry */ },
1112 };
1113 
1114 MODULE_DEVICE_TABLE(usb, ath10k_usb_ids);
1115 
1116 static struct usb_driver ath10k_usb_driver = {
1117     .name = "ath10k_usb",
1118     .probe = ath10k_usb_probe,
1119     .suspend = ath10k_usb_pm_suspend,
1120     .resume = ath10k_usb_pm_resume,
1121     .disconnect = ath10k_usb_remove,
1122     .id_table = ath10k_usb_ids,
1123     .supports_autosuspend = true,
1124     .disable_hub_initiated_lpm = 1,
1125 };
1126 
1127 module_usb_driver(ath10k_usb_driver);
1128 
1129 MODULE_AUTHOR("Atheros Communications, Inc.");
1130 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices");
1131 MODULE_LICENSE("Dual BSD/GPL");