GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / net / wireless / ath / ath10k / usb.c
1 /*
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>
5  *
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.
9  *
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.
17  */
18
19 #include <linux/module.h>
20 #include <linux/usb.h>
21
22 #include "debug.h"
23 #include "core.h"
24 #include "bmi.h"
25 #include "hif.h"
26 #include "htc.h"
27 #include "usb.h"
28
29 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
30                                            struct ath10k_usb_pipe *recv_pipe);
31
32 /* inlined helper functions */
33
34 static inline enum ath10k_htc_ep_id
35 eid_from_htc_hdr(struct ath10k_htc_hdr *htc_hdr)
36 {
37         return (enum ath10k_htc_ep_id)htc_hdr->eid;
38 }
39
40 static inline bool is_trailer_only_msg(struct ath10k_htc_hdr *htc_hdr)
41 {
42         return __le16_to_cpu(htc_hdr->len) == htc_hdr->trailer_len;
43 }
44
45 /* pipe/urb operations */
46 static struct ath10k_urb_context *
47 ath10k_usb_alloc_urb_from_pipe(struct ath10k_usb_pipe *pipe)
48 {
49         struct ath10k_urb_context *urb_context = NULL;
50         unsigned long flags;
51
52         /* bail if this pipe is not initialized */
53         if (!pipe->ar_usb)
54                 return NULL;
55
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);
61                 pipe->urb_cnt--;
62         }
63         spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
64
65         return urb_context;
66 }
67
68 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe,
69                                         struct ath10k_urb_context *urb_context)
70 {
71         unsigned long flags;
72
73         /* bail if this pipe is not initialized */
74         if (!pipe->ar_usb)
75                 return;
76
77         spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags);
78
79         pipe->urb_cnt++;
80         list_add(&urb_context->link, &pipe->urb_list_head);
81
82         spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags);
83 }
84
85 static void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context)
86 {
87         dev_kfree_skb(urb_context->skb);
88         urb_context->skb = NULL;
89
90         ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
91 }
92
93 static void ath10k_usb_free_pipe_resources(struct ath10k *ar,
94                                            struct ath10k_usb_pipe *pipe)
95 {
96         struct ath10k_urb_context *urb_context;
97
98         if (!pipe->ar_usb) {
99                 /* nothing allocated for this pipe */
100                 return;
101         }
102
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);
107
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);
113         }
114
115         for (;;) {
116                 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
117
118                 if (!urb_context)
119                         break;
120
121                 kfree(urb_context);
122         }
123 }
124
125 static void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar)
126 {
127         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
128         int i;
129
130         for (i = 0; i < ATH10K_USB_PIPE_MAX; i++)
131                 ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]);
132 }
133
134 /* hif usb rx/tx completion functions */
135
136 static void ath10k_usb_recv_complete(struct urb *urb)
137 {
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;
141         struct sk_buff *skb;
142         int status = 0;
143
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,
147                    urb);
148
149         if (urb->status != 0) {
150                 status = -EIO;
151                 switch (urb->status) {
152                 case -ECONNRESET:
153                 case -ENOENT:
154                 case -ESHUTDOWN:
155                         /* no need to spew these errors when device
156                          * removed or urb killed due to driver shutdown
157                          */
158                         status = -ECANCELED;
159                         break;
160                 default:
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);
165                         break;
166                 }
167                 goto cleanup_recv_urb;
168         }
169
170         if (urb->actual_length == 0)
171                 goto cleanup_recv_urb;
172
173         skb = urb_context->skb;
174
175         /* we are going to pass it up */
176         urb_context->skb = NULL;
177         skb_put(skb, urb->actual_length);
178
179         /* note: queue implements a lock */
180         skb_queue_tail(&pipe->io_comp_queue, skb);
181         schedule_work(&pipe->io_complete_work);
182
183 cleanup_recv_urb:
184         ath10k_usb_cleanup_recv_urb(urb_context);
185
186         if (status == 0 &&
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);
190         }
191 }
192
193 static void ath10k_usb_transmit_complete(struct urb *urb)
194 {
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;
198         struct sk_buff *skb;
199
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);
204         }
205
206         skb = urb_context->skb;
207         urb_context->skb = NULL;
208         ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
209
210         /* note: queue implements a lock */
211         skb_queue_tail(&pipe->io_comp_queue, skb);
212         schedule_work(&pipe->io_complete_work);
213 }
214
215 /* pipe operations */
216 static void ath10k_usb_post_recv_transfers(struct ath10k *ar,
217                                            struct ath10k_usb_pipe *recv_pipe)
218 {
219         struct ath10k_urb_context *urb_context;
220         struct urb *urb;
221         int usb_status;
222
223         for (;;) {
224                 urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe);
225                 if (!urb_context)
226                         break;
227
228                 urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE);
229                 if (!urb_context->skb)
230                         goto err;
231
232                 urb = usb_alloc_urb(0, GFP_ATOMIC);
233                 if (!urb)
234                         goto err;
235
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);
242
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);
248
249                 usb_anchor_urb(urb, &recv_pipe->urb_submitted);
250                 usb_status = usb_submit_urb(urb, GFP_ATOMIC);
251
252                 if (usb_status) {
253                         ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
254                                    "usb bulk recv failed: %d\n",
255                                    usb_status);
256                         usb_unanchor_urb(urb);
257                         usb_free_urb(urb);
258                         goto err;
259                 }
260                 usb_free_urb(urb);
261         }
262
263         return;
264
265 err:
266         ath10k_usb_cleanup_recv_urb(urb_context);
267 }
268
269 static void ath10k_usb_flush_all(struct ath10k *ar)
270 {
271         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
272         int i;
273
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);
278                 }
279         }
280 }
281
282 static void ath10k_usb_start_recv_pipes(struct ath10k *ar)
283 {
284         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
285
286         ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1;
287
288         ath10k_usb_post_recv_transfers(ar,
289                                        &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
290 }
291
292 static void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb)
293 {
294         struct ath10k_htc_hdr *htc_hdr;
295         struct ath10k_htc_ep *ep;
296
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... */
301 }
302
303 static void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb)
304 {
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;
309         u16 payload_len;
310         u8 *trailer;
311         int ret;
312
313         htc_hdr = (struct ath10k_htc_hdr *)skb->data;
314         eid = eid_from_htc_hdr(htc_hdr);
315         ep = &ar->htc.endpoint[eid];
316
317         if (ep->service_id == 0) {
318                 ath10k_warn(ar, "ep %d is not connected\n", eid);
319                 goto out_free_skb;
320         }
321
322         payload_len = le16_to_cpu(htc_hdr->len);
323         if (!payload_len) {
324                 ath10k_warn(ar, "zero length frame received, firmware crashed?\n");
325                 goto out_free_skb;
326         }
327
328         if (payload_len < htc_hdr->trailer_len) {
329                 ath10k_warn(ar, "malformed frame received, firmware crashed?\n");
330                 goto out_free_skb;
331         }
332
333         if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) {
334                 trailer = skb->data + sizeof(*htc_hdr) + payload_len -
335                           htc_hdr->trailer_len;
336
337                 ret = ath10k_htc_process_trailer(htc,
338                                                  trailer,
339                                                  htc_hdr->trailer_len,
340                                                  eid,
341                                                  NULL,
342                                                  NULL);
343                 if (ret)
344                         goto out_free_skb;
345
346                 if (is_trailer_only_msg(htc_hdr))
347                         goto out_free_skb;
348
349                 /* strip off the trailer from the skb since it should not
350                  * be passed on to upper layers
351                  */
352                 skb_trim(skb, skb->len - htc_hdr->trailer_len);
353         }
354
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... */
358
359         return;
360
361 out_free_skb:
362         dev_kfree_skb(skb);
363 }
364
365 static void ath10k_usb_io_comp_work(struct work_struct *work)
366 {
367         struct ath10k_usb_pipe *pipe = container_of(work,
368                                                     struct ath10k_usb_pipe,
369                                                     io_complete_work);
370         struct ath10k *ar = pipe->ar_usb->ar;
371         struct sk_buff *skb;
372
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);
376                 else
377                         ath10k_usb_rx_complete(ar, skb);
378         }
379 }
380
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))
383
384 static void ath10k_usb_destroy(struct ath10k *ar)
385 {
386         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
387
388         ath10k_usb_flush_all(ar);
389         ath10k_usb_cleanup_pipe_resources(ar);
390         usb_set_intfdata(ar_usb->interface, NULL);
391
392         kfree(ar_usb->diag_cmd_buffer);
393         kfree(ar_usb->diag_resp_buffer);
394 }
395
396 static int ath10k_usb_hif_start(struct ath10k *ar)
397 {
398         int i;
399         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
400
401         ath10k_usb_start_recv_pipes(ar);
402
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;
408         }
409
410         return 0;
411 }
412
413 static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
414                                 struct ath10k_hif_sg_item *items, int n_items)
415 {
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;
419         struct sk_buff *skb;
420         struct urb *urb;
421         int ret, i;
422
423         for (i = 0; i < n_items; i++) {
424                 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe);
425                 if (!urb_context) {
426                         ret = -ENOMEM;
427                         goto err;
428                 }
429
430                 skb = items[i].transfer_context;
431                 urb_context->skb = skb;
432
433                 urb = usb_alloc_urb(0, GFP_ATOMIC);
434                 if (!urb) {
435                         ret = -ENOMEM;
436                         goto err_free_urb_to_pipe;
437                 }
438
439                 usb_fill_bulk_urb(urb,
440                                   ar_usb->udev,
441                                   pipe->usb_pipe_handle,
442                                   skb->data,
443                                   skb->len,
444                                   ath10k_usb_transmit_complete, urb_context);
445
446                 if (!(skb->len % pipe->max_packet_size)) {
447                         /* hit a max packet boundary on this pipe */
448                         urb->transfer_flags |= URB_ZERO_PACKET;
449                 }
450
451                 usb_anchor_urb(urb, &pipe->urb_submitted);
452                 ret = usb_submit_urb(urb, GFP_ATOMIC);
453                 if (ret) {
454                         ath10k_dbg(ar, ATH10K_DBG_USB_BULK,
455                                    "usb bulk transmit failed: %d\n", ret);
456                         usb_unanchor_urb(urb);
457                         usb_free_urb(urb);
458                         ret = -EINVAL;
459                         goto err_free_urb_to_pipe;
460                 }
461
462                 usb_free_urb(urb);
463         }
464
465         return 0;
466
467 err_free_urb_to_pipe:
468         ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context);
469 err:
470         return ret;
471 }
472
473 static void ath10k_usb_hif_stop(struct ath10k *ar)
474 {
475         ath10k_usb_flush_all(ar);
476 }
477
478 static u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id)
479 {
480         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
481
482         return ar_usb->pipes[pipe_id].urb_cnt;
483 }
484
485 static int ath10k_usb_submit_ctrl_out(struct ath10k *ar,
486                                       u8 req, u16 value, u16 index, void *data,
487                                       u32 size)
488 {
489         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
490         u8 *buf = NULL;
491         int ret;
492
493         if (size > 0) {
494                 buf = kmemdup(data, size, GFP_KERNEL);
495                 if (!buf)
496                         return -ENOMEM;
497         }
498
499         /* note: if successful returns number of bytes transferred */
500         ret = usb_control_msg(ar_usb->udev,
501                               usb_sndctrlpipe(ar_usb->udev, 0),
502                               req,
503                               USB_DIR_OUT | USB_TYPE_VENDOR |
504                               USB_RECIP_DEVICE, value, index, buf,
505                               size, 1000);
506
507         if (ret < 0) {
508                 ath10k_warn(ar, "Failed to submit usb control message: %d\n",
509                             ret);
510                 kfree(buf);
511                 return ret;
512         }
513
514         kfree(buf);
515
516         return 0;
517 }
518
519 static int ath10k_usb_submit_ctrl_in(struct ath10k *ar,
520                                      u8 req, u16 value, u16 index, void *data,
521                                      u32 size)
522 {
523         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
524         u8 *buf = NULL;
525         int ret;
526
527         if (size > 0) {
528                 buf = kmalloc(size, GFP_KERNEL);
529                 if (!buf)
530                         return -ENOMEM;
531         }
532
533         /* note: if successful returns number of bytes transferred */
534         ret = usb_control_msg(ar_usb->udev,
535                               usb_rcvctrlpipe(ar_usb->udev, 0),
536                               req,
537                               USB_DIR_IN | USB_TYPE_VENDOR |
538                               USB_RECIP_DEVICE, value, index, buf,
539                               size, 2000);
540
541         if (ret < 0) {
542                 ath10k_warn(ar, "Failed to read usb control message: %d\n",
543                             ret);
544                 kfree(buf);
545                 return ret;
546         }
547
548         memcpy((u8 *)data, buf, size);
549
550         kfree(buf);
551
552         return 0;
553 }
554
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,
558                                         u32 *resp_len)
559 {
560         int ret;
561
562         /* send command */
563         ret = ath10k_usb_submit_ctrl_out(ar, req_val, 0, 0,
564                                          req_buf, req_len);
565         if (ret)
566                 goto err;
567
568         /* get response */
569         if (resp_buf) {
570                 ret = ath10k_usb_submit_ctrl_in(ar, resp_val, 0, 0,
571                                                 resp_buf, *resp_len);
572                 if (ret)
573                         goto err;
574         }
575
576         return 0;
577 err:
578         return ret;
579 }
580
581 static int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
582                                     size_t buf_len)
583 {
584         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
585         struct ath10k_usb_ctrl_diag_cmd_read *cmd;
586         u32 resp_len;
587         int ret;
588
589         if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read))
590                 return -EINVAL;
591
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);
597
598         ret = ath10k_usb_ctrl_msg_exchange(ar,
599                                            ATH10K_USB_CONTROL_REQ_DIAG_CMD,
600                                            (u8 *)cmd,
601                                            sizeof(*cmd),
602                                            ATH10K_USB_CONTROL_REQ_DIAG_RESP,
603                                            ar_usb->diag_resp_buffer, &resp_len);
604         if (ret)
605                 return ret;
606
607         if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read))
608                 return -EMSGSIZE;
609
610         memcpy(buf, ar_usb->diag_resp_buffer,
611                sizeof(struct ath10k_usb_ctrl_diag_resp_read));
612
613         return 0;
614 }
615
616 static int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address,
617                                      const void *data, int nbytes)
618 {
619         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
620         struct ath10k_usb_ctrl_diag_cmd_write *cmd;
621         int ret;
622
623         if (nbytes != sizeof(cmd->value))
624                 return -EINVAL;
625
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);
631
632         ret = ath10k_usb_ctrl_msg_exchange(ar,
633                                            ATH10K_USB_CONTROL_REQ_DIAG_CMD,
634                                            (u8 *)cmd,
635                                            sizeof(*cmd),
636                                            0, NULL, NULL);
637         if (ret)
638                 return ret;
639
640         return 0;
641 }
642
643 static int ath10k_usb_bmi_exchange_msg(struct ath10k *ar,
644                                        void *req, u32 req_len,
645                                        void *resp, u32 *resp_len)
646 {
647         int ret;
648
649         if (req) {
650                 ret = ath10k_usb_submit_ctrl_out(ar,
651                                                  ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD,
652                                                  0, 0, req, req_len);
653                 if (ret) {
654                         ath10k_warn(ar,
655                                     "unable to send the bmi data to the device: %d\n",
656                                     ret);
657                         return ret;
658                 }
659         }
660
661         if (resp) {
662                 ret = ath10k_usb_submit_ctrl_in(ar,
663                                                 ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP,
664                                                 0, 0, resp, *resp_len);
665                 if (ret) {
666                         ath10k_warn(ar,
667                                     "Unable to read the bmi data from the device: %d\n",
668                                     ret);
669                         return ret;
670                 }
671         }
672
673         return 0;
674 }
675
676 static void ath10k_usb_hif_get_default_pipe(struct ath10k *ar,
677                                             u8 *ul_pipe, u8 *dl_pipe)
678 {
679         *ul_pipe = ATH10K_USB_PIPE_TX_CTRL;
680         *dl_pipe = ATH10K_USB_PIPE_RX_CTRL;
681 }
682
683 static int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id,
684                                               u8 *ul_pipe, u8 *dl_pipe)
685 {
686         switch (svc_id) {
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;
692                 break;
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
697                  */
698                 *dl_pipe = ATH10K_USB_PIPE_RX_DATA;
699                 break;
700         default:
701                 return -EPERM;
702         }
703
704         return 0;
705 }
706
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
711  * hif op wrapper.
712  */
713 static void ath10k_usb_hif_send_complete_check(struct ath10k *ar,
714                                                u8 pipe, int force)
715 {
716 }
717
718 static int ath10k_usb_hif_power_up(struct ath10k *ar)
719 {
720         return 0;
721 }
722
723 static void ath10k_usb_hif_power_down(struct ath10k *ar)
724 {
725         ath10k_usb_flush_all(ar);
726 }
727
728 #ifdef CONFIG_PM
729
730 static int ath10k_usb_hif_suspend(struct ath10k *ar)
731 {
732         return -EOPNOTSUPP;
733 }
734
735 static int ath10k_usb_hif_resume(struct ath10k *ar)
736 {
737         return -EOPNOTSUPP;
738 }
739 #endif
740
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,
754 #ifdef CONFIG_PM
755         .suspend                = ath10k_usb_hif_suspend,
756         .resume                 = ath10k_usb_hif_resume,
757 #endif
758 };
759
760 static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count)
761 {
762         u8 pipe_num = ATH10K_USB_PIPE_INVALID;
763
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;
768                 break;
769         case ATH10K_USB_EP_ADDR_APP_DATA_IN:
770                 pipe_num = ATH10K_USB_PIPE_RX_DATA;
771                 *urb_count = RX_URB_COUNT;
772                 break;
773         case ATH10K_USB_EP_ADDR_APP_INT_IN:
774                 pipe_num = ATH10K_USB_PIPE_RX_INT;
775                 *urb_count = RX_URB_COUNT;
776                 break;
777         case ATH10K_USB_EP_ADDR_APP_DATA2_IN:
778                 pipe_num = ATH10K_USB_PIPE_RX_DATA2;
779                 *urb_count = RX_URB_COUNT;
780                 break;
781         case ATH10K_USB_EP_ADDR_APP_CTRL_OUT:
782                 pipe_num = ATH10K_USB_PIPE_TX_CTRL;
783                 *urb_count = TX_URB_COUNT;
784                 break;
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;
788                 break;
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;
792                 break;
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;
796                 break;
797         default:
798                 /* note: there may be endpoints not currently used */
799                 break;
800         }
801
802         return pipe_num;
803 }
804
805 static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar,
806                                            struct ath10k_usb_pipe *pipe,
807                                            int urb_cnt)
808 {
809         struct ath10k_urb_context *urb_context;
810         int i;
811
812         INIT_LIST_HEAD(&pipe->urb_list_head);
813         init_usb_anchor(&pipe->urb_submitted);
814
815         for (i = 0; i < urb_cnt; i++) {
816                 urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL);
817                 if (!urb_context)
818                         return -ENOMEM;
819
820                 urb_context->pipe = pipe;
821
822                 /* we are only allocate the urb contexts here, the actual URB
823                  * is allocated from the kernel as needed to do a transaction
824                  */
825                 pipe->urb_alloc++;
826                 ath10k_usb_free_urb_to_pipe(pipe, urb_context);
827         }
828
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,
832                    pipe->urb_alloc);
833
834         return 0;
835 }
836
837 static int ath10k_usb_setup_pipe_resources(struct ath10k *ar,
838                                            struct usb_interface *interface)
839 {
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;
845         u8 pipe_num;
846
847         ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n");
848
849         /* walk decriptors and setup pipes */
850         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
851                 endpoint = &iface_desc->endpoint[i].desc;
852
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",
856                                    ATH10K_USB_IS_DIR_IN
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",
863                                    ATH10K_USB_IS_DIR_IN
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)) {
869                         /* TODO for ISO */
870                         ath10k_dbg(ar, ATH10K_DBG_USB,
871                                    "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n",
872                                    ATH10K_USB_IS_DIR_IN
873                                    (endpoint->bEndpointAddress) ?
874                                    "rx" : "tx", endpoint->bEndpointAddress,
875                                    le16_to_cpu(endpoint->wMaxPacketSize),
876                                    endpoint->bInterval);
877                 }
878
879                 /* Ignore broken descriptors. */
880                 if (usb_endpoint_maxp(endpoint) == 0)
881                         continue;
882
883                 urbcount = 0;
884
885                 pipe_num =
886                     ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress,
887                                                     &urbcount);
888                 if (pipe_num == ATH10K_USB_PIPE_INVALID)
889                         continue;
890
891                 pipe = &ar_usb->pipes[pipe_num];
892                 if (pipe->ar_usb)
893                         /* hmmm..pipe was already setup */
894                         continue;
895
896                 pipe->ar_usb = ar_usb;
897                 pipe->logical_pipe_num = pipe_num;
898                 pipe->ep_address = endpoint->bEndpointAddress;
899                 pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize);
900
901                 if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) {
902                         if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
903                                 pipe->usb_pipe_handle =
904                                     usb_rcvbulkpipe(ar_usb->udev,
905                                                     pipe->ep_address);
906                         } else {
907                                 pipe->usb_pipe_handle =
908                                     usb_sndbulkpipe(ar_usb->udev,
909                                                     pipe->ep_address);
910                         }
911                 } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) {
912                         if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
913                                 pipe->usb_pipe_handle =
914                                     usb_rcvintpipe(ar_usb->udev,
915                                                    pipe->ep_address);
916                         } else {
917                                 pipe->usb_pipe_handle =
918                                     usb_sndintpipe(ar_usb->udev,
919                                                    pipe->ep_address);
920                         }
921                 } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) {
922                         /* TODO for ISO */
923                         if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) {
924                                 pipe->usb_pipe_handle =
925                                     usb_rcvisocpipe(ar_usb->udev,
926                                                     pipe->ep_address);
927                         } else {
928                                 pipe->usb_pipe_handle =
929                                     usb_sndisocpipe(ar_usb->udev,
930                                                     pipe->ep_address);
931                         }
932                 }
933
934                 pipe->ep_desc = endpoint;
935
936                 if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address))
937                         pipe->flags |= ATH10K_USB_PIPE_FLAG_TX;
938
939                 ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount);
940                 if (ret)
941                         return ret;
942         }
943
944         return 0;
945 }
946
947 static int ath10k_usb_create(struct ath10k *ar,
948                              struct usb_interface *interface)
949 {
950         struct ath10k_usb *ar_usb = ath10k_usb_priv(ar);
951         struct usb_device *dev = interface_to_usbdev(interface);
952         struct ath10k_usb_pipe *pipe;
953         int ret, i;
954
955         usb_set_intfdata(interface, ar_usb);
956         spin_lock_init(&ar_usb->cs_lock);
957         ar_usb->udev = dev;
958         ar_usb->interface = interface;
959
960         for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) {
961                 pipe = &ar_usb->pipes[i];
962                 INIT_WORK(&pipe->io_complete_work,
963                           ath10k_usb_io_comp_work);
964                 skb_queue_head_init(&pipe->io_comp_queue);
965         }
966
967         ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL);
968         if (!ar_usb->diag_cmd_buffer) {
969                 ret = -ENOMEM;
970                 goto err;
971         }
972
973         ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP,
974                                            GFP_KERNEL);
975         if (!ar_usb->diag_resp_buffer) {
976                 ret = -ENOMEM;
977                 goto err;
978         }
979
980         ret = ath10k_usb_setup_pipe_resources(ar, interface);
981         if (ret)
982                 goto err;
983
984         return 0;
985
986 err:
987         ath10k_usb_destroy(ar);
988         return ret;
989 }
990
991 /* ath10k usb driver registered functions */
992 static int ath10k_usb_probe(struct usb_interface *interface,
993                             const struct usb_device_id *id)
994 {
995         struct ath10k *ar;
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         u32 chip_id;
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         usb_get_dev(dev);
1018         vendor_id = le16_to_cpu(dev->descriptor.idVendor);
1019         product_id = le16_to_cpu(dev->descriptor.idProduct);
1020
1021         ath10k_dbg(ar, ATH10K_DBG_BOOT,
1022                    "usb new func vendor 0x%04x product 0x%04x\n",
1023                    vendor_id, product_id);
1024
1025         ar_usb = ath10k_usb_priv(ar);
1026         ret = ath10k_usb_create(ar, interface);
1027         if (ret)
1028                 goto err;
1029         ar_usb->ar = ar;
1030
1031         ar->dev_id = product_id;
1032         ar->id.vendor = vendor_id;
1033         ar->id.device = product_id;
1034
1035         /* TODO: don't know yet how to get chip_id with USB */
1036         chip_id = 0;
1037         ret = ath10k_core_register(ar, chip_id);
1038         if (ret) {
1039                 ath10k_warn(ar, "failed to register driver core: %d\n", ret);
1040                 goto err_usb_destroy;
1041         }
1042
1043         /* TODO: remove this once USB support is fully implemented */
1044         ath10k_warn(ar, "Warning: ath10k USB support is incomplete, don't expect anything to work!\n");
1045
1046         return 0;
1047
1048 err_usb_destroy:
1049         ath10k_usb_destroy(ar);
1050
1051 err:
1052         ath10k_core_destroy(ar);
1053
1054         usb_put_dev(dev);
1055
1056         return ret;
1057 }
1058
1059 static void ath10k_usb_remove(struct usb_interface *interface)
1060 {
1061         struct ath10k_usb *ar_usb;
1062
1063         ar_usb = usb_get_intfdata(interface);
1064         if (!ar_usb)
1065                 return;
1066
1067         ath10k_core_unregister(ar_usb->ar);
1068         ath10k_usb_destroy(ar_usb->ar);
1069         usb_put_dev(interface_to_usbdev(interface));
1070         ath10k_core_destroy(ar_usb->ar);
1071 }
1072
1073 #ifdef CONFIG_PM
1074
1075 static int ath10k_usb_pm_suspend(struct usb_interface *interface,
1076                                  pm_message_t message)
1077 {
1078         struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1079
1080         ath10k_usb_flush_all(ar_usb->ar);
1081         return 0;
1082 }
1083
1084 static int ath10k_usb_pm_resume(struct usb_interface *interface)
1085 {
1086         struct ath10k_usb *ar_usb = usb_get_intfdata(interface);
1087         struct ath10k *ar = ar_usb->ar;
1088
1089         ath10k_usb_post_recv_transfers(ar,
1090                                        &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]);
1091
1092         return 0;
1093 }
1094
1095 #else
1096
1097 #define ath10k_usb_pm_suspend NULL
1098 #define ath10k_usb_pm_resume NULL
1099
1100 #endif
1101
1102 /* table of devices that work with this driver */
1103 static struct usb_device_id ath10k_usb_ids[] = {
1104         {USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */
1105         { /* Terminating entry */ },
1106 };
1107
1108 MODULE_DEVICE_TABLE(usb, ath10k_usb_ids);
1109
1110 static struct usb_driver ath10k_usb_driver = {
1111         .name = "ath10k_usb",
1112         .probe = ath10k_usb_probe,
1113         .suspend = ath10k_usb_pm_suspend,
1114         .resume = ath10k_usb_pm_resume,
1115         .disconnect = ath10k_usb_remove,
1116         .id_table = ath10k_usb_ids,
1117         .supports_autosuspend = true,
1118         .disable_hub_initiated_lpm = 1,
1119 };
1120
1121 module_usb_driver(ath10k_usb_driver);
1122
1123 MODULE_AUTHOR("Atheros Communications, Inc.");
1124 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices");
1125 MODULE_LICENSE("Dual BSD/GPL");