iwlwifi: make scan antenna forcing more generic
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / ath / ath9k / hif_usb.c
1 /*
2  * Copyright (c) 2010 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "htc.h"
18
19 #define ATH9K_FW_USB_DEV(devid, fw)                                     \
20         { USB_DEVICE(0x0cf3, devid), .driver_info = (unsigned long) fw }
21
22 static struct usb_device_id ath9k_hif_usb_ids[] = {
23         ATH9K_FW_USB_DEV(0x9271, "ar9271.fw"),
24         ATH9K_FW_USB_DEV(0x1006, "ar9271.fw"),
25         { },
26 };
27
28 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
29
30 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
31
32 static void hif_usb_regout_cb(struct urb *urb)
33 {
34         struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
35
36         switch (urb->status) {
37         case 0:
38                 break;
39         case -ENOENT:
40         case -ECONNRESET:
41         case -ENODEV:
42         case -ESHUTDOWN:
43                 goto free;
44         default:
45                 break;
46         }
47
48         if (cmd) {
49                 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
50                                           cmd->skb, 1);
51                 kfree(cmd);
52         }
53
54         return;
55 free:
56         kfree_skb(cmd->skb);
57         kfree(cmd);
58 }
59
60 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
61                                struct sk_buff *skb)
62 {
63         struct urb *urb;
64         struct cmd_buf *cmd;
65         int ret = 0;
66
67         urb = usb_alloc_urb(0, GFP_KERNEL);
68         if (urb == NULL)
69                 return -ENOMEM;
70
71         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
72         if (cmd == NULL) {
73                 usb_free_urb(urb);
74                 return -ENOMEM;
75         }
76
77         cmd->skb = skb;
78         cmd->hif_dev = hif_dev;
79
80         usb_fill_int_urb(urb, hif_dev->udev,
81                          usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
82                          skb->data, skb->len,
83                          hif_usb_regout_cb, cmd, 1);
84
85         usb_anchor_urb(urb, &hif_dev->regout_submitted);
86         ret = usb_submit_urb(urb, GFP_KERNEL);
87         if (ret) {
88                 usb_unanchor_urb(urb);
89                 kfree(cmd);
90         }
91         usb_free_urb(urb);
92
93         return ret;
94 }
95
96 static void hif_usb_tx_cb(struct urb *urb)
97 {
98         struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
99         struct hif_device_usb *hif_dev = tx_buf->hif_dev;
100         struct sk_buff *skb;
101         bool drop, flush;
102
103         if (!hif_dev)
104                 return;
105
106         switch (urb->status) {
107         case 0:
108                 break;
109         case -ENOENT:
110         case -ECONNRESET:
111                 break;
112         case -ENODEV:
113         case -ESHUTDOWN:
114                 return;
115         default:
116                 break;
117         }
118
119         if (tx_buf) {
120                 spin_lock(&hif_dev->tx.tx_lock);
121                 drop = !!(hif_dev->tx.flags & HIF_USB_TX_STOP);
122                 flush = !!(hif_dev->tx.flags & HIF_USB_TX_FLUSH);
123                 spin_unlock(&hif_dev->tx.tx_lock);
124
125                 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
126                         if (!drop && !flush) {
127                                 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
128                                                           skb, 1);
129                                 TX_STAT_INC(skb_completed);
130                         } else {
131                                 dev_kfree_skb_any(skb);
132                         }
133                 }
134
135                 if (flush)
136                         return;
137
138                 tx_buf->len = tx_buf->offset = 0;
139                 __skb_queue_head_init(&tx_buf->skb_queue);
140
141                 spin_lock(&hif_dev->tx.tx_lock);
142                 list_del(&tx_buf->list);
143                 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
144                 hif_dev->tx.tx_buf_cnt++;
145                 if (!drop)
146                         __hif_usb_tx(hif_dev); /* Check for pending SKBs */
147                 TX_STAT_INC(buf_completed);
148                 spin_unlock(&hif_dev->tx.tx_lock);
149         }
150 }
151
152 static inline void ath9k_skb_queue_purge(struct sk_buff_head *list)
153 {
154         struct sk_buff *skb;
155         while ((skb = __skb_dequeue(list)) != NULL)
156                 dev_kfree_skb_any(skb);
157 }
158
159 /* TX lock has to be taken */
160 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
161 {
162         struct tx_buf *tx_buf = NULL;
163         struct sk_buff *nskb = NULL;
164         int ret = 0, i;
165         u16 *hdr, tx_skb_cnt = 0;
166         u8 *buf;
167
168         if (hif_dev->tx.tx_skb_cnt == 0)
169                 return 0;
170
171         /* Check if a free TX buffer is available */
172         if (list_empty(&hif_dev->tx.tx_buf))
173                 return 0;
174
175         tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
176         list_del(&tx_buf->list);
177         list_add_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
178         hif_dev->tx.tx_buf_cnt--;
179
180         tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
181
182         for (i = 0; i < tx_skb_cnt; i++) {
183                 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
184
185                 /* Should never be NULL */
186                 BUG_ON(!nskb);
187
188                 hif_dev->tx.tx_skb_cnt--;
189
190                 buf = tx_buf->buf;
191                 buf += tx_buf->offset;
192                 hdr = (u16 *)buf;
193                 *hdr++ = nskb->len;
194                 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
195                 buf += 4;
196                 memcpy(buf, nskb->data, nskb->len);
197                 tx_buf->len = nskb->len + 4;
198
199                 if (i < (tx_skb_cnt - 1))
200                         tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
201
202                 if (i == (tx_skb_cnt - 1))
203                         tx_buf->len += tx_buf->offset;
204
205                 __skb_queue_tail(&tx_buf->skb_queue, nskb);
206                 TX_STAT_INC(skb_queued);
207         }
208
209         usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
210                           usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
211                           tx_buf->buf, tx_buf->len,
212                           hif_usb_tx_cb, tx_buf);
213
214         ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
215         if (ret) {
216                 tx_buf->len = tx_buf->offset = 0;
217                 ath9k_skb_queue_purge(&tx_buf->skb_queue);
218                 __skb_queue_head_init(&tx_buf->skb_queue);
219                 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
220                 hif_dev->tx.tx_buf_cnt++;
221         }
222
223         if (!ret)
224                 TX_STAT_INC(buf_queued);
225
226         return ret;
227 }
228
229 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
230                            struct ath9k_htc_tx_ctl *tx_ctl)
231 {
232         unsigned long flags;
233
234         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
235
236         if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
237                 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
238                 return -ENODEV;
239         }
240
241         /* Check if the max queue count has been reached */
242         if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
243                 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
244                 return -ENOMEM;
245         }
246
247         __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
248         hif_dev->tx.tx_skb_cnt++;
249
250         /* Send normal frames immediately */
251         if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
252                 __hif_usb_tx(hif_dev);
253
254         /* Check if AMPDUs have to be sent immediately */
255         if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
256             (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
257             (hif_dev->tx.tx_skb_cnt < 2)) {
258                 __hif_usb_tx(hif_dev);
259         }
260
261         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
262
263         return 0;
264 }
265
266 static void hif_usb_start(void *hif_handle, u8 pipe_id)
267 {
268         struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
269         unsigned long flags;
270
271         hif_dev->flags |= HIF_USB_START;
272
273         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
274         hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
275         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
276 }
277
278 static void hif_usb_stop(void *hif_handle, u8 pipe_id)
279 {
280         struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
281         unsigned long flags;
282
283         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
284         ath9k_skb_queue_purge(&hif_dev->tx.tx_skb_queue);
285         hif_dev->tx.tx_skb_cnt = 0;
286         hif_dev->tx.flags |= HIF_USB_TX_STOP;
287         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
288 }
289
290 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
291                         struct ath9k_htc_tx_ctl *tx_ctl)
292 {
293         struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
294         int ret = 0;
295
296         switch (pipe_id) {
297         case USB_WLAN_TX_PIPE:
298                 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
299                 break;
300         case USB_REG_OUT_PIPE:
301                 ret = hif_usb_send_regout(hif_dev, skb);
302                 break;
303         default:
304                 dev_err(&hif_dev->udev->dev,
305                         "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
306                 ret = -EINVAL;
307                 break;
308         }
309
310         return ret;
311 }
312
313 static struct ath9k_htc_hif hif_usb = {
314         .transport = ATH9K_HIF_USB,
315         .name = "ath9k_hif_usb",
316
317         .control_ul_pipe = USB_REG_OUT_PIPE,
318         .control_dl_pipe = USB_REG_IN_PIPE,
319
320         .start = hif_usb_start,
321         .stop = hif_usb_stop,
322         .send = hif_usb_send,
323 };
324
325 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
326                                     struct sk_buff *skb)
327 {
328         struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
329         int index = 0, i = 0, chk_idx, len = skb->len;
330         int rx_remain_len = 0, rx_pkt_len = 0;
331         u16 pkt_len, pkt_tag, pool_index = 0;
332         u8 *ptr;
333
334         spin_lock(&hif_dev->rx_lock);
335
336         rx_remain_len = hif_dev->rx_remain_len;
337         rx_pkt_len = hif_dev->rx_transfer_len;
338
339         if (rx_remain_len != 0) {
340                 struct sk_buff *remain_skb = hif_dev->remain_skb;
341
342                 if (remain_skb) {
343                         ptr = (u8 *) remain_skb->data;
344
345                         index = rx_remain_len;
346                         rx_remain_len -= hif_dev->rx_pad_len;
347                         ptr += rx_pkt_len;
348
349                         memcpy(ptr, skb->data, rx_remain_len);
350
351                         rx_pkt_len += rx_remain_len;
352                         hif_dev->rx_remain_len = 0;
353                         skb_put(remain_skb, rx_pkt_len);
354
355                         skb_pool[pool_index++] = remain_skb;
356
357                 } else {
358                         index = rx_remain_len;
359                 }
360         }
361
362         spin_unlock(&hif_dev->rx_lock);
363
364         while (index < len) {
365                 ptr = (u8 *) skb->data;
366
367                 pkt_len = ptr[index] + (ptr[index+1] << 8);
368                 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
369
370                 if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) {
371                         u16 pad_len;
372
373                         pad_len = 4 - (pkt_len & 0x3);
374                         if (pad_len == 4)
375                                 pad_len = 0;
376
377                         chk_idx = index;
378                         index = index + 4 + pkt_len + pad_len;
379
380                         if (index > MAX_RX_BUF_SIZE) {
381                                 spin_lock(&hif_dev->rx_lock);
382                                 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
383                                 hif_dev->rx_transfer_len =
384                                         MAX_RX_BUF_SIZE - chk_idx - 4;
385                                 hif_dev->rx_pad_len = pad_len;
386
387                                 nskb = __dev_alloc_skb(pkt_len + 32,
388                                                        GFP_ATOMIC);
389                                 if (!nskb) {
390                                         dev_err(&hif_dev->udev->dev,
391                                         "ath9k_htc: RX memory allocation"
392                                         " error\n");
393                                         spin_unlock(&hif_dev->rx_lock);
394                                         goto err;
395                                 }
396                                 skb_reserve(nskb, 32);
397                                 RX_STAT_INC(skb_allocated);
398
399                                 memcpy(nskb->data, &(skb->data[chk_idx+4]),
400                                        hif_dev->rx_transfer_len);
401
402                                 /* Record the buffer pointer */
403                                 hif_dev->remain_skb = nskb;
404                                 spin_unlock(&hif_dev->rx_lock);
405                         } else {
406                                 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
407                                 if (!nskb) {
408                                         dev_err(&hif_dev->udev->dev,
409                                         "ath9k_htc: RX memory allocation"
410                                         " error\n");
411                                         goto err;
412                                 }
413                                 skb_reserve(nskb, 32);
414                                 RX_STAT_INC(skb_allocated);
415
416                                 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
417                                 skb_put(nskb, pkt_len);
418                                 skb_pool[pool_index++] = nskb;
419                         }
420                 } else {
421                         RX_STAT_INC(skb_dropped);
422                         return;
423                 }
424         }
425
426 err:
427         for (i = 0; i < pool_index; i++) {
428                 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
429                                  skb_pool[i]->len, USB_WLAN_RX_PIPE);
430                 RX_STAT_INC(skb_completed);
431         }
432 }
433
434 static void ath9k_hif_usb_rx_cb(struct urb *urb)
435 {
436         struct sk_buff *skb = (struct sk_buff *) urb->context;
437         struct hif_device_usb *hif_dev = (struct hif_device_usb *)
438                 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
439         int ret;
440
441         if (!skb)
442                 return;
443
444         if (!hif_dev)
445                 goto free;
446
447         switch (urb->status) {
448         case 0:
449                 break;
450         case -ENOENT:
451         case -ECONNRESET:
452         case -ENODEV:
453         case -ESHUTDOWN:
454                 goto free;
455         default:
456                 goto resubmit;
457         }
458
459         if (likely(urb->actual_length != 0)) {
460                 skb_put(skb, urb->actual_length);
461                 ath9k_hif_usb_rx_stream(hif_dev, skb);
462         }
463
464 resubmit:
465         skb_reset_tail_pointer(skb);
466         skb_trim(skb, 0);
467
468         usb_anchor_urb(urb, &hif_dev->rx_submitted);
469         ret = usb_submit_urb(urb, GFP_ATOMIC);
470         if (ret) {
471                 usb_unanchor_urb(urb);
472                 goto free;
473         }
474
475         return;
476 free:
477         kfree_skb(skb);
478 }
479
480 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
481 {
482         struct sk_buff *skb = (struct sk_buff *) urb->context;
483         struct sk_buff *nskb;
484         struct hif_device_usb *hif_dev = (struct hif_device_usb *)
485                 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
486         int ret;
487
488         if (!skb)
489                 return;
490
491         if (!hif_dev)
492                 goto free;
493
494         switch (urb->status) {
495         case 0:
496                 break;
497         case -ENOENT:
498         case -ECONNRESET:
499         case -ENODEV:
500         case -ESHUTDOWN:
501                 goto free;
502         default:
503                 goto resubmit;
504         }
505
506         if (likely(urb->actual_length != 0)) {
507                 skb_put(skb, urb->actual_length);
508
509                 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
510                 if (!nskb)
511                         goto resubmit;
512
513                 usb_fill_int_urb(urb, hif_dev->udev,
514                                  usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
515                                  nskb->data, MAX_REG_IN_BUF_SIZE,
516                                  ath9k_hif_usb_reg_in_cb, nskb, 1);
517
518                 ret = usb_submit_urb(urb, GFP_ATOMIC);
519                 if (ret) {
520                         kfree_skb(nskb);
521                         goto free;
522                 }
523
524                 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
525                                  skb->len, USB_REG_IN_PIPE);
526
527                 return;
528         }
529
530 resubmit:
531         skb_reset_tail_pointer(skb);
532         skb_trim(skb, 0);
533
534         ret = usb_submit_urb(urb, GFP_ATOMIC);
535         if (ret)
536                 goto free;
537
538         return;
539 free:
540         kfree_skb(skb);
541         urb->context = NULL;
542 }
543
544 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
545 {
546         unsigned long flags;
547         struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
548
549         list_for_each_entry_safe(tx_buf, tx_buf_tmp, &hif_dev->tx.tx_buf, list) {
550                 list_del(&tx_buf->list);
551                 usb_free_urb(tx_buf->urb);
552                 kfree(tx_buf->buf);
553                 kfree(tx_buf);
554         }
555
556         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
557         hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
558         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
559
560         list_for_each_entry_safe(tx_buf, tx_buf_tmp,
561                                  &hif_dev->tx.tx_pending, list) {
562                 usb_kill_urb(tx_buf->urb);
563                 list_del(&tx_buf->list);
564                 usb_free_urb(tx_buf->urb);
565                 kfree(tx_buf->buf);
566                 kfree(tx_buf);
567         }
568
569         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
570         hif_dev->tx.flags &= ~HIF_USB_TX_FLUSH;
571         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
572 }
573
574 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
575 {
576         struct tx_buf *tx_buf;
577         int i;
578
579         INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
580         INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
581         spin_lock_init(&hif_dev->tx.tx_lock);
582         __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
583
584         for (i = 0; i < MAX_TX_URB_NUM; i++) {
585                 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
586                 if (!tx_buf)
587                         goto err;
588
589                 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
590                 if (!tx_buf->buf)
591                         goto err;
592
593                 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
594                 if (!tx_buf->urb)
595                         goto err;
596
597                 tx_buf->hif_dev = hif_dev;
598                 __skb_queue_head_init(&tx_buf->skb_queue);
599
600                 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
601         }
602
603         hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
604
605         return 0;
606 err:
607         ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
608         return -ENOMEM;
609 }
610
611 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
612 {
613         usb_kill_anchored_urbs(&hif_dev->rx_submitted);
614 }
615
616 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
617 {
618         struct urb *urb = NULL;
619         struct sk_buff *skb = NULL;
620         int i, ret;
621
622         init_usb_anchor(&hif_dev->rx_submitted);
623         spin_lock_init(&hif_dev->rx_lock);
624
625         for (i = 0; i < MAX_RX_URB_NUM; i++) {
626
627                 /* Allocate URB */
628                 urb = usb_alloc_urb(0, GFP_KERNEL);
629                 if (urb == NULL) {
630                         ret = -ENOMEM;
631                         goto err_urb;
632                 }
633
634                 /* Allocate buffer */
635                 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
636                 if (!skb) {
637                         ret = -ENOMEM;
638                         goto err_skb;
639                 }
640
641                 usb_fill_bulk_urb(urb, hif_dev->udev,
642                                   usb_rcvbulkpipe(hif_dev->udev,
643                                                   USB_WLAN_RX_PIPE),
644                                   skb->data, MAX_RX_BUF_SIZE,
645                                   ath9k_hif_usb_rx_cb, skb);
646
647                 /* Anchor URB */
648                 usb_anchor_urb(urb, &hif_dev->rx_submitted);
649
650                 /* Submit URB */
651                 ret = usb_submit_urb(urb, GFP_KERNEL);
652                 if (ret) {
653                         usb_unanchor_urb(urb);
654                         goto err_submit;
655                 }
656
657                 /*
658                  * Drop reference count.
659                  * This ensures that the URB is freed when killing them.
660                  */
661                 usb_free_urb(urb);
662         }
663
664         return 0;
665
666 err_submit:
667         kfree_skb(skb);
668 err_skb:
669         usb_free_urb(urb);
670 err_urb:
671         ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
672         return ret;
673 }
674
675 static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
676 {
677         if (hif_dev->reg_in_urb) {
678                 usb_kill_urb(hif_dev->reg_in_urb);
679                 if (hif_dev->reg_in_urb->context)
680                         kfree_skb((void *)hif_dev->reg_in_urb->context);
681                 usb_free_urb(hif_dev->reg_in_urb);
682                 hif_dev->reg_in_urb = NULL;
683         }
684 }
685
686 static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
687 {
688         struct sk_buff *skb;
689
690         hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
691         if (hif_dev->reg_in_urb == NULL)
692                 return -ENOMEM;
693
694         skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
695         if (!skb)
696                 goto err;
697
698         usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev,
699                          usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
700                          skb->data, MAX_REG_IN_BUF_SIZE,
701                          ath9k_hif_usb_reg_in_cb, skb, 1);
702
703         if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
704                 goto err;
705
706         return 0;
707
708 err:
709         ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
710         return -ENOMEM;
711 }
712
713 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
714 {
715         /* Register Write */
716         init_usb_anchor(&hif_dev->regout_submitted);
717
718         /* TX */
719         if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
720                 goto err;
721
722         /* RX */
723         if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
724                 goto err;
725
726         /* Register Read */
727         if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
728                 goto err;
729
730         return 0;
731 err:
732         return -ENOMEM;
733 }
734
735 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
736 {
737         int transfer, err;
738         const void *data = hif_dev->firmware->data;
739         size_t len = hif_dev->firmware->size;
740         u32 addr = AR9271_FIRMWARE;
741         u8 *buf = kzalloc(4096, GFP_KERNEL);
742
743         if (!buf)
744                 return -ENOMEM;
745
746         while (len) {
747                 transfer = min_t(int, len, 4096);
748                 memcpy(buf, data, transfer);
749
750                 err = usb_control_msg(hif_dev->udev,
751                                       usb_sndctrlpipe(hif_dev->udev, 0),
752                                       FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
753                                       addr >> 8, 0, buf, transfer, HZ);
754                 if (err < 0) {
755                         kfree(buf);
756                         return err;
757                 }
758
759                 len -= transfer;
760                 data += transfer;
761                 addr += transfer;
762         }
763         kfree(buf);
764
765         /*
766          * Issue FW download complete command to firmware.
767          */
768         err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
769                               FIRMWARE_DOWNLOAD_COMP,
770                               0x40 | USB_DIR_OUT,
771                               AR9271_FIRMWARE_TEXT >> 8, 0, NULL, 0, HZ);
772         if (err)
773                 return -EIO;
774
775         dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
776                  "ar9271.fw", (unsigned long) hif_dev->firmware->size);
777
778         return 0;
779 }
780
781 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev,
782                                   const char *fw_name)
783 {
784         int ret;
785
786         /* Request firmware */
787         ret = request_firmware(&hif_dev->firmware, fw_name, &hif_dev->udev->dev);
788         if (ret) {
789                 dev_err(&hif_dev->udev->dev,
790                         "ath9k_htc: Firmware - %s not found\n", fw_name);
791                 goto err_fw_req;
792         }
793
794         /* Download firmware */
795         ret = ath9k_hif_usb_download_fw(hif_dev);
796         if (ret) {
797                 dev_err(&hif_dev->udev->dev,
798                         "ath9k_htc: Firmware - %s download failed\n", fw_name);
799                 goto err_fw_download;
800         }
801
802         /* Alloc URBs */
803         ret = ath9k_hif_usb_alloc_urbs(hif_dev);
804         if (ret) {
805                 dev_err(&hif_dev->udev->dev,
806                         "ath9k_htc: Unable to allocate URBs\n");
807                 goto err_urb;
808         }
809
810         return 0;
811
812 err_urb:
813         /* Nothing */
814 err_fw_download:
815         release_firmware(hif_dev->firmware);
816 err_fw_req:
817         hif_dev->firmware = NULL;
818         return ret;
819 }
820
821 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
822 {
823         usb_kill_anchored_urbs(&hif_dev->regout_submitted);
824         ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
825         ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
826         ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
827 }
828
829 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
830 {
831         ath9k_hif_usb_dealloc_urbs(hif_dev);
832         if (hif_dev->firmware)
833                 release_firmware(hif_dev->firmware);
834 }
835
836 static int ath9k_hif_usb_probe(struct usb_interface *interface,
837                                const struct usb_device_id *id)
838 {
839         struct usb_device *udev = interface_to_usbdev(interface);
840         struct hif_device_usb *hif_dev;
841         const char *fw_name = (const char *) id->driver_info;
842         int ret = 0;
843
844         hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
845         if (!hif_dev) {
846                 ret = -ENOMEM;
847                 goto err_alloc;
848         }
849
850         usb_get_dev(udev);
851         hif_dev->udev = udev;
852         hif_dev->interface = interface;
853         hif_dev->device_id = id->idProduct;
854 #ifdef CONFIG_PM
855         udev->reset_resume = 1;
856 #endif
857         usb_set_intfdata(interface, hif_dev);
858
859         ret = ath9k_hif_usb_dev_init(hif_dev, fw_name);
860         if (ret) {
861                 ret = -EINVAL;
862                 goto err_hif_init_usb;
863         }
864
865         hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev);
866         if (hif_dev->htc_handle == NULL) {
867                 ret = -ENOMEM;
868                 goto err_htc_hw_alloc;
869         }
870
871         ret = ath9k_htc_hw_init(&hif_usb, hif_dev->htc_handle, hif_dev,
872                                 &hif_dev->udev->dev, hif_dev->device_id,
873                                 ATH9K_HIF_USB);
874         if (ret) {
875                 ret = -EINVAL;
876                 goto err_htc_hw_init;
877         }
878
879         dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
880
881         return 0;
882
883 err_htc_hw_init:
884         ath9k_htc_hw_free(hif_dev->htc_handle);
885 err_htc_hw_alloc:
886         ath9k_hif_usb_dev_deinit(hif_dev);
887 err_hif_init_usb:
888         usb_set_intfdata(interface, NULL);
889         kfree(hif_dev);
890         usb_put_dev(udev);
891 err_alloc:
892         return ret;
893 }
894
895 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
896 {
897         struct usb_device *udev = interface_to_usbdev(interface);
898         struct hif_device_usb *hif_dev =
899                 (struct hif_device_usb *) usb_get_intfdata(interface);
900
901         if (hif_dev) {
902                 ath9k_htc_hw_deinit(hif_dev->htc_handle, true);
903                 ath9k_htc_hw_free(hif_dev->htc_handle);
904                 ath9k_hif_usb_dev_deinit(hif_dev);
905                 usb_set_intfdata(interface, NULL);
906         }
907
908         if (hif_dev->flags & HIF_USB_START)
909                 usb_reset_device(udev);
910
911         kfree(hif_dev);
912         dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
913         usb_put_dev(udev);
914 }
915
916 #ifdef CONFIG_PM
917 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
918                                  pm_message_t message)
919 {
920         struct hif_device_usb *hif_dev =
921                 (struct hif_device_usb *) usb_get_intfdata(interface);
922
923         ath9k_hif_usb_dealloc_urbs(hif_dev);
924
925         return 0;
926 }
927
928 static int ath9k_hif_usb_resume(struct usb_interface *interface)
929 {
930         struct hif_device_usb *hif_dev =
931                 (struct hif_device_usb *) usb_get_intfdata(interface);
932         int ret;
933
934         ret = ath9k_hif_usb_alloc_urbs(hif_dev);
935         if (ret)
936                 return ret;
937
938         if (hif_dev->firmware) {
939                 ret = ath9k_hif_usb_download_fw(hif_dev);
940                 if (ret)
941                         goto fail_resume;
942         } else {
943                 ath9k_hif_usb_dealloc_urbs(hif_dev);
944                 return -EIO;
945         }
946
947         mdelay(100);
948
949         ret = ath9k_htc_resume(hif_dev->htc_handle);
950
951         if (ret)
952                 goto fail_resume;
953
954         return 0;
955
956 fail_resume:
957         ath9k_hif_usb_dealloc_urbs(hif_dev);
958
959         return ret;
960 }
961 #endif
962
963 static struct usb_driver ath9k_hif_usb_driver = {
964         .name = "ath9k_hif_usb",
965         .probe = ath9k_hif_usb_probe,
966         .disconnect = ath9k_hif_usb_disconnect,
967 #ifdef CONFIG_PM
968         .suspend = ath9k_hif_usb_suspend,
969         .resume = ath9k_hif_usb_resume,
970         .reset_resume = ath9k_hif_usb_resume,
971 #endif
972         .id_table = ath9k_hif_usb_ids,
973         .soft_unbind = 1,
974 };
975
976 int ath9k_hif_usb_init(void)
977 {
978         return usb_register(&ath9k_hif_usb_driver);
979 }
980
981 void ath9k_hif_usb_exit(void)
982 {
983         usb_deregister(&ath9k_hif_usb_driver);
984 }