pandora: defconfig: update
[pandora-kernel.git] / drivers / net / wireless / ath / ath9k / hif_usb.c
1 /*
2  * Copyright (c) 2010-2011 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 <asm/unaligned.h>
18 #include "htc.h"
19
20 /* identify firmware images */
21 #define FIRMWARE_AR7010_1_1     "htc_7010.fw"
22 #define FIRMWARE_AR9271         "htc_9271.fw"
23
24 MODULE_FIRMWARE(FIRMWARE_AR7010_1_1);
25 MODULE_FIRMWARE(FIRMWARE_AR9271);
26
27 static struct usb_device_id ath9k_hif_usb_ids[] = {
28         { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
29         { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
30         { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
31         { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
32         { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
33         { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
34         { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
35         { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
36         { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
37         { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
38         { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
39         { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
40         { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
41         { USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */
42         { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
43         { USB_DEVICE(0x0471, 0x209e) }, /* Philips (or NXP) PTA01 */
44         { USB_DEVICE(0x1eda, 0x2315) }, /* AirTies */
45
46         { USB_DEVICE(0x0cf3, 0x7015),
47           .driver_info = AR9287_USB },  /* Atheros */
48         { USB_DEVICE(0x1668, 0x1200),
49           .driver_info = AR9287_USB },  /* Verizon */
50
51         { USB_DEVICE(0x0cf3, 0x7010),
52           .driver_info = AR9280_USB },  /* Atheros */
53         { USB_DEVICE(0x0846, 0x9018),
54           .driver_info = AR9280_USB },  /* Netgear WNDA3200 */
55         { USB_DEVICE(0x083A, 0xA704),
56           .driver_info = AR9280_USB },  /* SMC Networks */
57         { USB_DEVICE(0x0411, 0x017f),
58           .driver_info = AR9280_USB },  /* Sony UWA-BR100 */
59         { USB_DEVICE(0x0411, 0x0197),
60           .driver_info = AR9280_USB },  /* Buffalo WLI-UV-AG300P */
61         { USB_DEVICE(0x04da, 0x3904),
62           .driver_info = AR9280_USB },
63         { USB_DEVICE(0x0930, 0x0a08),
64           .driver_info = AR9280_USB },  /* Toshiba WLM-20U2 and GN-1080 */
65
66         { USB_DEVICE(0x0cf3, 0x20ff),
67           .driver_info = STORAGE_DEVICE },
68
69         { },
70 };
71
72 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
73
74 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
75
76 static void hif_usb_regout_cb(struct urb *urb)
77 {
78         struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
79
80         switch (urb->status) {
81         case 0:
82                 break;
83         case -ENOENT:
84         case -ECONNRESET:
85         case -ENODEV:
86         case -ESHUTDOWN:
87                 goto free;
88         default:
89                 break;
90         }
91
92         if (cmd) {
93                 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
94                                           cmd->skb, true);
95                 kfree(cmd);
96         }
97
98         return;
99 free:
100         kfree_skb(cmd->skb);
101         kfree(cmd);
102 }
103
104 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
105                                struct sk_buff *skb)
106 {
107         struct urb *urb;
108         struct cmd_buf *cmd;
109         int ret = 0;
110
111         urb = usb_alloc_urb(0, GFP_KERNEL);
112         if (urb == NULL)
113                 return -ENOMEM;
114
115         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
116         if (cmd == NULL) {
117                 usb_free_urb(urb);
118                 return -ENOMEM;
119         }
120
121         cmd->skb = skb;
122         cmd->hif_dev = hif_dev;
123
124         usb_fill_bulk_urb(urb, hif_dev->udev,
125                          usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
126                          skb->data, skb->len,
127                          hif_usb_regout_cb, cmd);
128
129         usb_anchor_urb(urb, &hif_dev->regout_submitted);
130         ret = usb_submit_urb(urb, GFP_KERNEL);
131         if (ret) {
132                 usb_unanchor_urb(urb);
133                 kfree(cmd);
134         }
135         usb_free_urb(urb);
136
137         return ret;
138 }
139
140 static void hif_usb_mgmt_cb(struct urb *urb)
141 {
142         struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
143         struct hif_device_usb *hif_dev;
144         bool txok = true;
145
146         if (!cmd || !cmd->skb || !cmd->hif_dev)
147                 return;
148
149         hif_dev = cmd->hif_dev;
150
151         switch (urb->status) {
152         case 0:
153                 break;
154         case -ENOENT:
155         case -ECONNRESET:
156         case -ENODEV:
157         case -ESHUTDOWN:
158                 txok = false;
159
160                 /*
161                  * If the URBs are being flushed, no need to complete
162                  * this packet.
163                  */
164                 spin_lock(&hif_dev->tx.tx_lock);
165                 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
166                         spin_unlock(&hif_dev->tx.tx_lock);
167                         dev_kfree_skb_any(cmd->skb);
168                         kfree(cmd);
169                         return;
170                 }
171                 spin_unlock(&hif_dev->tx.tx_lock);
172
173                 break;
174         default:
175                 txok = false;
176                 break;
177         }
178
179         skb_pull(cmd->skb, 4);
180         ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
181                                   cmd->skb, txok);
182         kfree(cmd);
183 }
184
185 static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
186                              struct sk_buff *skb)
187 {
188         struct urb *urb;
189         struct cmd_buf *cmd;
190         int ret = 0;
191         __le16 *hdr;
192
193         urb = usb_alloc_urb(0, GFP_ATOMIC);
194         if (urb == NULL)
195                 return -ENOMEM;
196
197         cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
198         if (cmd == NULL) {
199                 usb_free_urb(urb);
200                 return -ENOMEM;
201         }
202
203         cmd->skb = skb;
204         cmd->hif_dev = hif_dev;
205
206         hdr = (__le16 *) skb_push(skb, 4);
207         *hdr++ = cpu_to_le16(skb->len - 4);
208         *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
209
210         usb_fill_bulk_urb(urb, hif_dev->udev,
211                          usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
212                          skb->data, skb->len,
213                          hif_usb_mgmt_cb, cmd);
214
215         usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
216         ret = usb_submit_urb(urb, GFP_ATOMIC);
217         if (ret) {
218                 usb_unanchor_urb(urb);
219                 kfree(cmd);
220         }
221         usb_free_urb(urb);
222
223         return ret;
224 }
225
226 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
227                                          struct sk_buff_head *list)
228 {
229         struct sk_buff *skb;
230
231         while ((skb = __skb_dequeue(list)) != NULL) {
232                 dev_kfree_skb_any(skb);
233         }
234 }
235
236 static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
237                                             struct sk_buff_head *queue,
238                                             bool txok)
239 {
240         struct sk_buff *skb;
241
242         while ((skb = __skb_dequeue(queue)) != NULL) {
243                 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
244                                           skb, txok);
245                 if (txok)
246                         TX_STAT_INC(skb_success);
247                 else
248                         TX_STAT_INC(skb_failed);
249         }
250 }
251
252 static void hif_usb_tx_cb(struct urb *urb)
253 {
254         struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
255         struct hif_device_usb *hif_dev;
256         bool txok = true;
257
258         if (!tx_buf || !tx_buf->hif_dev)
259                 return;
260
261         hif_dev = tx_buf->hif_dev;
262
263         switch (urb->status) {
264         case 0:
265                 break;
266         case -ENOENT:
267         case -ECONNRESET:
268         case -ENODEV:
269         case -ESHUTDOWN:
270                 txok = false;
271
272                 /*
273                  * If the URBs are being flushed, no need to add this
274                  * URB to the free list.
275                  */
276                 spin_lock(&hif_dev->tx.tx_lock);
277                 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
278                         spin_unlock(&hif_dev->tx.tx_lock);
279                         ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
280                         return;
281                 }
282                 spin_unlock(&hif_dev->tx.tx_lock);
283
284                 break;
285         default:
286                 txok = false;
287                 break;
288         }
289
290         ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
291
292         /* Re-initialize the SKB queue */
293         tx_buf->len = tx_buf->offset = 0;
294         __skb_queue_head_init(&tx_buf->skb_queue);
295
296         /* Add this TX buffer to the free list */
297         spin_lock(&hif_dev->tx.tx_lock);
298         list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
299         hif_dev->tx.tx_buf_cnt++;
300         if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
301                 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
302         TX_STAT_INC(buf_completed);
303         spin_unlock(&hif_dev->tx.tx_lock);
304 }
305
306 /* TX lock has to be taken */
307 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
308 {
309         struct tx_buf *tx_buf = NULL;
310         struct sk_buff *nskb = NULL;
311         int ret = 0, i;
312         u16 tx_skb_cnt = 0;
313         u8 *buf;
314         __le16 *hdr;
315
316         if (hif_dev->tx.tx_skb_cnt == 0)
317                 return 0;
318
319         /* Check if a free TX buffer is available */
320         if (list_empty(&hif_dev->tx.tx_buf))
321                 return 0;
322
323         tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
324         list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
325         hif_dev->tx.tx_buf_cnt--;
326
327         tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
328
329         for (i = 0; i < tx_skb_cnt; i++) {
330                 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
331
332                 /* Should never be NULL */
333                 BUG_ON(!nskb);
334
335                 hif_dev->tx.tx_skb_cnt--;
336
337                 buf = tx_buf->buf;
338                 buf += tx_buf->offset;
339                 hdr = (__le16 *)buf;
340                 *hdr++ = cpu_to_le16(nskb->len);
341                 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
342                 buf += 4;
343                 memcpy(buf, nskb->data, nskb->len);
344                 tx_buf->len = nskb->len + 4;
345
346                 if (i < (tx_skb_cnt - 1))
347                         tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
348
349                 if (i == (tx_skb_cnt - 1))
350                         tx_buf->len += tx_buf->offset;
351
352                 __skb_queue_tail(&tx_buf->skb_queue, nskb);
353                 TX_STAT_INC(skb_queued);
354         }
355
356         usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
357                           usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
358                           tx_buf->buf, tx_buf->len,
359                           hif_usb_tx_cb, tx_buf);
360
361         ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
362         if (ret) {
363                 tx_buf->len = tx_buf->offset = 0;
364                 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
365                 __skb_queue_head_init(&tx_buf->skb_queue);
366                 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
367                 hif_dev->tx.tx_buf_cnt++;
368         }
369
370         if (!ret)
371                 TX_STAT_INC(buf_queued);
372
373         return ret;
374 }
375
376 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
377 {
378         struct ath9k_htc_tx_ctl *tx_ctl;
379         unsigned long flags;
380         int ret = 0;
381
382         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
383
384         if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
385                 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
386                 return -ENODEV;
387         }
388
389         /* Check if the max queue count has been reached */
390         if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
391                 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
392                 return -ENOMEM;
393         }
394
395         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
396
397         tx_ctl = HTC_SKB_CB(skb);
398
399         /* Mgmt/Beacon frames don't use the TX buffer pool */
400         if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
401             (tx_ctl->type == ATH9K_HTC_BEACON)) {
402                 ret = hif_usb_send_mgmt(hif_dev, skb);
403         }
404
405         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
406
407         if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
408             (tx_ctl->type == ATH9K_HTC_AMPDU)) {
409                 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
410                 hif_dev->tx.tx_skb_cnt++;
411         }
412
413         /* Check if AMPDUs have to be sent immediately */
414         if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
415             (hif_dev->tx.tx_skb_cnt < 2)) {
416                 __hif_usb_tx(hif_dev);
417         }
418
419         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
420
421         return ret;
422 }
423
424 static void hif_usb_start(void *hif_handle)
425 {
426         struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
427         unsigned long flags;
428
429         hif_dev->flags |= HIF_USB_START;
430
431         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
432         hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
433         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
434 }
435
436 static void hif_usb_stop(void *hif_handle)
437 {
438         struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
439         struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
440         unsigned long flags;
441
442         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
443         ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
444         hif_dev->tx.tx_skb_cnt = 0;
445         hif_dev->tx.flags |= HIF_USB_TX_STOP;
446         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
447
448         /* The pending URBs have to be canceled. */
449         list_for_each_entry_safe(tx_buf, tx_buf_tmp,
450                                  &hif_dev->tx.tx_pending, list) {
451                 usb_kill_urb(tx_buf->urb);
452         }
453
454         usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
455 }
456
457 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
458 {
459         struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
460         int ret = 0;
461
462         switch (pipe_id) {
463         case USB_WLAN_TX_PIPE:
464                 ret = hif_usb_send_tx(hif_dev, skb);
465                 break;
466         case USB_REG_OUT_PIPE:
467                 ret = hif_usb_send_regout(hif_dev, skb);
468                 break;
469         default:
470                 dev_err(&hif_dev->udev->dev,
471                         "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
472                 ret = -EINVAL;
473                 break;
474         }
475
476         return ret;
477 }
478
479 static inline bool check_index(struct sk_buff *skb, u8 idx)
480 {
481         struct ath9k_htc_tx_ctl *tx_ctl;
482
483         tx_ctl = HTC_SKB_CB(skb);
484
485         if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
486             (tx_ctl->sta_idx == idx))
487                 return true;
488
489         return false;
490 }
491
492 static void hif_usb_sta_drain(void *hif_handle, u8 idx)
493 {
494         struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
495         struct sk_buff *skb, *tmp;
496         unsigned long flags;
497
498         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
499
500         skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
501                 if (check_index(skb, idx)) {
502                         __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
503                         ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
504                                                   skb, false);
505                         hif_dev->tx.tx_skb_cnt--;
506                         TX_STAT_INC(skb_failed);
507                 }
508         }
509
510         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
511 }
512
513 static struct ath9k_htc_hif hif_usb = {
514         .transport = ATH9K_HIF_USB,
515         .name = "ath9k_hif_usb",
516
517         .control_ul_pipe = USB_REG_OUT_PIPE,
518         .control_dl_pipe = USB_REG_IN_PIPE,
519
520         .start = hif_usb_start,
521         .stop = hif_usb_stop,
522         .sta_drain = hif_usb_sta_drain,
523         .send = hif_usb_send,
524 };
525
526 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
527                                     struct sk_buff *skb)
528 {
529         struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
530         int index = 0, i = 0, len = skb->len;
531         int rx_remain_len, rx_pkt_len;
532         u16 pool_index = 0;
533         u8 *ptr;
534
535         spin_lock(&hif_dev->rx_lock);
536
537         rx_remain_len = hif_dev->rx_remain_len;
538         rx_pkt_len = hif_dev->rx_transfer_len;
539
540         if (rx_remain_len != 0) {
541                 struct sk_buff *remain_skb = hif_dev->remain_skb;
542
543                 if (remain_skb) {
544                         ptr = (u8 *) remain_skb->data;
545
546                         index = rx_remain_len;
547                         rx_remain_len -= hif_dev->rx_pad_len;
548                         ptr += rx_pkt_len;
549
550                         memcpy(ptr, skb->data, rx_remain_len);
551
552                         rx_pkt_len += rx_remain_len;
553                         hif_dev->rx_remain_len = 0;
554                         skb_put(remain_skb, rx_pkt_len);
555
556                         skb_pool[pool_index++] = remain_skb;
557
558                 } else {
559                         index = rx_remain_len;
560                 }
561         }
562
563         spin_unlock(&hif_dev->rx_lock);
564
565         while (index < len) {
566                 u16 pkt_len;
567                 u16 pkt_tag;
568                 u16 pad_len;
569                 int chk_idx;
570
571                 ptr = (u8 *) skb->data;
572
573                 pkt_len = get_unaligned_le16(ptr + index);
574                 pkt_tag = get_unaligned_le16(ptr + index + 2);
575
576                 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
577                         RX_STAT_INC(skb_dropped);
578                         return;
579                 }
580
581                 pad_len = 4 - (pkt_len & 0x3);
582                 if (pad_len == 4)
583                         pad_len = 0;
584
585                 chk_idx = index;
586                 index = index + 4 + pkt_len + pad_len;
587
588                 if (index > MAX_RX_BUF_SIZE) {
589                         spin_lock(&hif_dev->rx_lock);
590                         hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
591                         hif_dev->rx_transfer_len =
592                                 MAX_RX_BUF_SIZE - chk_idx - 4;
593                         hif_dev->rx_pad_len = pad_len;
594
595                         nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
596                         if (!nskb) {
597                                 dev_err(&hif_dev->udev->dev,
598                                         "ath9k_htc: RX memory allocation error\n");
599                                 spin_unlock(&hif_dev->rx_lock);
600                                 goto err;
601                         }
602                         skb_reserve(nskb, 32);
603                         RX_STAT_INC(skb_allocated);
604
605                         memcpy(nskb->data, &(skb->data[chk_idx+4]),
606                                hif_dev->rx_transfer_len);
607
608                         /* Record the buffer pointer */
609                         hif_dev->remain_skb = nskb;
610                         spin_unlock(&hif_dev->rx_lock);
611                 } else {
612                         nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
613                         if (!nskb) {
614                                 dev_err(&hif_dev->udev->dev,
615                                         "ath9k_htc: RX memory allocation error\n");
616                                 goto err;
617                         }
618                         skb_reserve(nskb, 32);
619                         RX_STAT_INC(skb_allocated);
620
621                         memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
622                         skb_put(nskb, pkt_len);
623                         skb_pool[pool_index++] = nskb;
624                 }
625         }
626
627 err:
628         for (i = 0; i < pool_index; i++) {
629                 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
630                                  skb_pool[i]->len, USB_WLAN_RX_PIPE);
631                 RX_STAT_INC(skb_completed);
632         }
633 }
634
635 static void ath9k_hif_usb_rx_cb(struct urb *urb)
636 {
637         struct sk_buff *skb = (struct sk_buff *) urb->context;
638         struct hif_device_usb *hif_dev =
639                 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
640         int ret;
641
642         if (!skb)
643                 return;
644
645         if (!hif_dev)
646                 goto free;
647
648         switch (urb->status) {
649         case 0:
650                 break;
651         case -ENOENT:
652         case -ECONNRESET:
653         case -ENODEV:
654         case -ESHUTDOWN:
655                 goto free;
656         default:
657                 goto resubmit;
658         }
659
660         if (likely(urb->actual_length != 0)) {
661                 skb_put(skb, urb->actual_length);
662                 ath9k_hif_usb_rx_stream(hif_dev, skb);
663         }
664
665 resubmit:
666         skb_reset_tail_pointer(skb);
667         skb_trim(skb, 0);
668
669         usb_anchor_urb(urb, &hif_dev->rx_submitted);
670         ret = usb_submit_urb(urb, GFP_ATOMIC);
671         if (ret) {
672                 usb_unanchor_urb(urb);
673                 goto free;
674         }
675
676         return;
677 free:
678         kfree_skb(skb);
679 }
680
681 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
682 {
683         struct sk_buff *skb = (struct sk_buff *) urb->context;
684         struct sk_buff *nskb;
685         struct hif_device_usb *hif_dev =
686                 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
687         int ret;
688
689         if (!skb)
690                 return;
691
692         if (!hif_dev)
693                 goto free;
694
695         switch (urb->status) {
696         case 0:
697                 break;
698         case -ENOENT:
699         case -ECONNRESET:
700         case -ENODEV:
701         case -ESHUTDOWN:
702                 goto free;
703         default:
704                 skb_reset_tail_pointer(skb);
705                 skb_trim(skb, 0);
706
707                 goto resubmit;
708         }
709
710         if (likely(urb->actual_length != 0)) {
711                 skb_put(skb, urb->actual_length);
712
713                 /* Process the command first */
714                 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
715                                  skb->len, USB_REG_IN_PIPE);
716
717
718                 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
719                 if (!nskb) {
720                         dev_err(&hif_dev->udev->dev,
721                                 "ath9k_htc: REG_IN memory allocation failure\n");
722                         urb->context = NULL;
723                         return;
724                 }
725
726                 usb_fill_bulk_urb(urb, hif_dev->udev,
727                                  usb_rcvbulkpipe(hif_dev->udev,
728                                                  USB_REG_IN_PIPE),
729                                  nskb->data, MAX_REG_IN_BUF_SIZE,
730                                  ath9k_hif_usb_reg_in_cb, nskb);
731         }
732
733 resubmit:
734         usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
735         ret = usb_submit_urb(urb, GFP_ATOMIC);
736         if (ret) {
737                 usb_unanchor_urb(urb);
738                 goto free;
739         }
740
741         return;
742 free:
743         kfree_skb(skb);
744         urb->context = NULL;
745 }
746
747 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
748 {
749         struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
750         unsigned long flags;
751
752         list_for_each_entry_safe(tx_buf, tx_buf_tmp,
753                                  &hif_dev->tx.tx_buf, list) {
754                 usb_kill_urb(tx_buf->urb);
755                 list_del(&tx_buf->list);
756                 usb_free_urb(tx_buf->urb);
757                 kfree(tx_buf->buf);
758                 kfree(tx_buf);
759         }
760
761         spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
762         hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
763         spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
764
765         list_for_each_entry_safe(tx_buf, tx_buf_tmp,
766                                  &hif_dev->tx.tx_pending, list) {
767                 usb_kill_urb(tx_buf->urb);
768                 list_del(&tx_buf->list);
769                 usb_free_urb(tx_buf->urb);
770                 kfree(tx_buf->buf);
771                 kfree(tx_buf);
772         }
773
774         usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
775 }
776
777 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
778 {
779         struct tx_buf *tx_buf;
780         int i;
781
782         INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
783         INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
784         spin_lock_init(&hif_dev->tx.tx_lock);
785         __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
786         init_usb_anchor(&hif_dev->mgmt_submitted);
787
788         for (i = 0; i < MAX_TX_URB_NUM; i++) {
789                 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
790                 if (!tx_buf)
791                         goto err;
792
793                 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
794                 if (!tx_buf->buf)
795                         goto err;
796
797                 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
798                 if (!tx_buf->urb)
799                         goto err;
800
801                 tx_buf->hif_dev = hif_dev;
802                 __skb_queue_head_init(&tx_buf->skb_queue);
803
804                 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
805         }
806
807         hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
808
809         return 0;
810 err:
811         if (tx_buf) {
812                 kfree(tx_buf->buf);
813                 kfree(tx_buf);
814         }
815         ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
816         return -ENOMEM;
817 }
818
819 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
820 {
821         usb_kill_anchored_urbs(&hif_dev->rx_submitted);
822 }
823
824 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
825 {
826         struct urb *urb = NULL;
827         struct sk_buff *skb = NULL;
828         int i, ret;
829
830         init_usb_anchor(&hif_dev->rx_submitted);
831         spin_lock_init(&hif_dev->rx_lock);
832
833         for (i = 0; i < MAX_RX_URB_NUM; i++) {
834
835                 /* Allocate URB */
836                 urb = usb_alloc_urb(0, GFP_KERNEL);
837                 if (urb == NULL) {
838                         ret = -ENOMEM;
839                         goto err_urb;
840                 }
841
842                 /* Allocate buffer */
843                 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
844                 if (!skb) {
845                         ret = -ENOMEM;
846                         goto err_skb;
847                 }
848
849                 usb_fill_bulk_urb(urb, hif_dev->udev,
850                                   usb_rcvbulkpipe(hif_dev->udev,
851                                                   USB_WLAN_RX_PIPE),
852                                   skb->data, MAX_RX_BUF_SIZE,
853                                   ath9k_hif_usb_rx_cb, skb);
854
855                 /* Anchor URB */
856                 usb_anchor_urb(urb, &hif_dev->rx_submitted);
857
858                 /* Submit URB */
859                 ret = usb_submit_urb(urb, GFP_KERNEL);
860                 if (ret) {
861                         usb_unanchor_urb(urb);
862                         goto err_submit;
863                 }
864
865                 /*
866                  * Drop reference count.
867                  * This ensures that the URB is freed when killing them.
868                  */
869                 usb_free_urb(urb);
870         }
871
872         return 0;
873
874 err_submit:
875         kfree_skb(skb);
876 err_skb:
877         usb_free_urb(urb);
878 err_urb:
879         ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
880         return ret;
881 }
882
883 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
884 {
885         usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
886 }
887
888 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
889 {
890         struct urb *urb = NULL;
891         struct sk_buff *skb = NULL;
892         int i, ret;
893
894         init_usb_anchor(&hif_dev->reg_in_submitted);
895
896         for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
897
898                 /* Allocate URB */
899                 urb = usb_alloc_urb(0, GFP_KERNEL);
900                 if (urb == NULL) {
901                         ret = -ENOMEM;
902                         goto err_urb;
903                 }
904
905                 /* Allocate buffer */
906                 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
907                 if (!skb) {
908                         ret = -ENOMEM;
909                         goto err_skb;
910                 }
911
912                 usb_fill_bulk_urb(urb, hif_dev->udev,
913                                   usb_rcvbulkpipe(hif_dev->udev,
914                                                   USB_REG_IN_PIPE),
915                                   skb->data, MAX_REG_IN_BUF_SIZE,
916                                   ath9k_hif_usb_reg_in_cb, skb);
917
918                 /* Anchor URB */
919                 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
920
921                 /* Submit URB */
922                 ret = usb_submit_urb(urb, GFP_KERNEL);
923                 if (ret) {
924                         usb_unanchor_urb(urb);
925                         goto err_submit;
926                 }
927
928                 /*
929                  * Drop reference count.
930                  * This ensures that the URB is freed when killing them.
931                  */
932                 usb_free_urb(urb);
933         }
934
935         return 0;
936
937 err_submit:
938         kfree_skb(skb);
939 err_skb:
940         usb_free_urb(urb);
941 err_urb:
942         ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
943         return ret;
944 }
945
946 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
947 {
948         /* Register Write */
949         init_usb_anchor(&hif_dev->regout_submitted);
950
951         /* TX */
952         if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
953                 goto err;
954
955         /* RX */
956         if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
957                 goto err_rx;
958
959         /* Register Read */
960         if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
961                 goto err_reg;
962
963         return 0;
964 err_reg:
965         ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
966 err_rx:
967         ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
968 err:
969         return -ENOMEM;
970 }
971
972 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
973 {
974         usb_kill_anchored_urbs(&hif_dev->regout_submitted);
975         ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
976         ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
977         ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
978 }
979
980 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
981                                      u32 drv_info)
982 {
983         int transfer, err;
984         const void *data = hif_dev->firmware->data;
985         size_t len = hif_dev->firmware->size;
986         u32 addr = AR9271_FIRMWARE;
987         u8 *buf = kzalloc(4096, GFP_KERNEL);
988         u32 firm_offset;
989
990         if (!buf)
991                 return -ENOMEM;
992
993         while (len) {
994                 transfer = min_t(int, len, 4096);
995                 memcpy(buf, data, transfer);
996
997                 err = usb_control_msg(hif_dev->udev,
998                                       usb_sndctrlpipe(hif_dev->udev, 0),
999                                       FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
1000                                       addr >> 8, 0, buf, transfer, HZ);
1001                 if (err < 0) {
1002                         kfree(buf);
1003                         return err;
1004                 }
1005
1006                 len -= transfer;
1007                 data += transfer;
1008                 addr += transfer;
1009         }
1010         kfree(buf);
1011
1012         if (IS_AR7010_DEVICE(drv_info))
1013                 firm_offset = AR7010_FIRMWARE_TEXT;
1014         else
1015                 firm_offset = AR9271_FIRMWARE_TEXT;
1016
1017         /*
1018          * Issue FW download complete command to firmware.
1019          */
1020         err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1021                               FIRMWARE_DOWNLOAD_COMP,
1022                               0x40 | USB_DIR_OUT,
1023                               firm_offset >> 8, 0, NULL, 0, HZ);
1024         if (err)
1025                 return -EIO;
1026
1027         dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1028                  hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
1029
1030         return 0;
1031 }
1032
1033 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
1034 {
1035         int ret, idx;
1036         struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
1037         struct usb_endpoint_descriptor *endp;
1038
1039         /* Request firmware */
1040         ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
1041                                &hif_dev->udev->dev);
1042         if (ret) {
1043                 dev_err(&hif_dev->udev->dev,
1044                         "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
1045                 goto err_fw_req;
1046         }
1047
1048         /* Download firmware */
1049         ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
1050         if (ret) {
1051                 dev_err(&hif_dev->udev->dev,
1052                         "ath9k_htc: Firmware - %s download failed\n",
1053                         hif_dev->fw_name);
1054                 goto err_fw_download;
1055         }
1056
1057         /* On downloading the firmware to the target, the USB descriptor of EP4
1058          * is 'patched' to change the type of the endpoint to Bulk. This will
1059          * bring down CPU usage during the scan period.
1060          */
1061         for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
1062                 endp = &alt->endpoint[idx].desc;
1063                 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1064                                 == USB_ENDPOINT_XFER_INT) {
1065                         endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
1066                         endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
1067                         endp->bInterval = 0;
1068                 }
1069         }
1070
1071         /* Alloc URBs */
1072         ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1073         if (ret) {
1074                 dev_err(&hif_dev->udev->dev,
1075                         "ath9k_htc: Unable to allocate URBs\n");
1076                 goto err_fw_download;
1077         }
1078
1079         return 0;
1080
1081 err_fw_download:
1082         release_firmware(hif_dev->firmware);
1083 err_fw_req:
1084         hif_dev->firmware = NULL;
1085         return ret;
1086 }
1087
1088 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1089 {
1090         ath9k_hif_usb_dealloc_urbs(hif_dev);
1091         if (hif_dev->firmware)
1092                 release_firmware(hif_dev->firmware);
1093 }
1094
1095 /*
1096  * An exact copy of the function from zd1211rw.
1097  */
1098 static int send_eject_command(struct usb_interface *interface)
1099 {
1100         struct usb_device *udev = interface_to_usbdev(interface);
1101         struct usb_host_interface *iface_desc = &interface->altsetting[0];
1102         struct usb_endpoint_descriptor *endpoint;
1103         unsigned char *cmd;
1104         u8 bulk_out_ep;
1105         int r;
1106
1107         if (iface_desc->desc.bNumEndpoints < 2)
1108                 return -ENODEV;
1109
1110         /* Find bulk out endpoint */
1111         for (r = 1; r >= 0; r--) {
1112                 endpoint = &iface_desc->endpoint[r].desc;
1113                 if (usb_endpoint_dir_out(endpoint) &&
1114                     usb_endpoint_xfer_bulk(endpoint)) {
1115                         bulk_out_ep = endpoint->bEndpointAddress;
1116                         break;
1117                 }
1118         }
1119         if (r == -1) {
1120                 dev_err(&udev->dev,
1121                         "ath9k_htc: Could not find bulk out endpoint\n");
1122                 return -ENODEV;
1123         }
1124
1125         cmd = kzalloc(31, GFP_KERNEL);
1126         if (cmd == NULL)
1127                 return -ENODEV;
1128
1129         /* USB bulk command block */
1130         cmd[0] = 0x55;  /* bulk command signature */
1131         cmd[1] = 0x53;  /* bulk command signature */
1132         cmd[2] = 0x42;  /* bulk command signature */
1133         cmd[3] = 0x43;  /* bulk command signature */
1134         cmd[14] = 6;    /* command length */
1135
1136         cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1137         cmd[19] = 0x2;  /* eject disc */
1138
1139         dev_info(&udev->dev, "Ejecting storage device...\n");
1140         r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1141                 cmd, 31, NULL, 2000);
1142         kfree(cmd);
1143         if (r)
1144                 return r;
1145
1146         /* At this point, the device disconnects and reconnects with the real
1147          * ID numbers. */
1148
1149         usb_set_intfdata(interface, NULL);
1150         return 0;
1151 }
1152
1153 static int ath9k_hif_usb_probe(struct usb_interface *interface,
1154                                const struct usb_device_id *id)
1155 {
1156         struct usb_device *udev = interface_to_usbdev(interface);
1157         struct hif_device_usb *hif_dev;
1158         int ret = 0;
1159
1160         if (id->driver_info == STORAGE_DEVICE)
1161                 return send_eject_command(interface);
1162
1163         hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1164         if (!hif_dev) {
1165                 ret = -ENOMEM;
1166                 goto err_alloc;
1167         }
1168
1169         usb_get_dev(udev);
1170         hif_dev->udev = udev;
1171         hif_dev->interface = interface;
1172         hif_dev->device_id = id->idProduct;
1173 #ifdef CONFIG_PM
1174         udev->reset_resume = 1;
1175 #endif
1176         usb_set_intfdata(interface, hif_dev);
1177
1178         hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1179                                                  &hif_dev->udev->dev);
1180         if (hif_dev->htc_handle == NULL) {
1181                 ret = -ENOMEM;
1182                 goto err_htc_hw_alloc;
1183         }
1184
1185         /* Find out which firmware to load */
1186
1187         if (IS_AR7010_DEVICE(id->driver_info))
1188                 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
1189         else
1190                 hif_dev->fw_name = FIRMWARE_AR9271;
1191
1192         ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
1193         if (ret) {
1194                 ret = -EINVAL;
1195                 goto err_hif_init_usb;
1196         }
1197
1198         ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1199                                 &interface->dev, hif_dev->device_id,
1200                                 hif_dev->udev->product, id->driver_info);
1201         if (ret) {
1202                 ret = -EINVAL;
1203                 goto err_htc_hw_init;
1204         }
1205
1206         dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1207
1208         return 0;
1209
1210 err_htc_hw_init:
1211         ath9k_hif_usb_dev_deinit(hif_dev);
1212 err_hif_init_usb:
1213         ath9k_htc_hw_free(hif_dev->htc_handle);
1214 err_htc_hw_alloc:
1215         usb_set_intfdata(interface, NULL);
1216         kfree(hif_dev);
1217         usb_put_dev(udev);
1218 err_alloc:
1219         return ret;
1220 }
1221
1222 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1223 {
1224         u32 reboot_cmd = 0xffffffff;
1225         void *buf;
1226         int ret;
1227
1228         buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1229         if (!buf)
1230                 return;
1231
1232         ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1233                            buf, 4, NULL, HZ);
1234         if (ret)
1235                 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1236
1237         kfree(buf);
1238 }
1239
1240 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1241 {
1242         struct usb_device *udev = interface_to_usbdev(interface);
1243         struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1244         bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1245
1246         if (!hif_dev)
1247                 return;
1248
1249         ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1250         ath9k_htc_hw_free(hif_dev->htc_handle);
1251         ath9k_hif_usb_dev_deinit(hif_dev);
1252         usb_set_intfdata(interface, NULL);
1253
1254         if (!unplugged && (hif_dev->flags & HIF_USB_START))
1255                 ath9k_hif_usb_reboot(udev);
1256
1257         kfree(hif_dev);
1258         dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1259         usb_put_dev(udev);
1260 }
1261
1262 #ifdef CONFIG_PM
1263 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1264                                  pm_message_t message)
1265 {
1266         struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1267
1268         /*
1269          * The device has to be set to FULLSLEEP mode in case no
1270          * interface is up.
1271          */
1272         if (!(hif_dev->flags & HIF_USB_START))
1273                 ath9k_htc_suspend(hif_dev->htc_handle);
1274
1275         ath9k_hif_usb_dealloc_urbs(hif_dev);
1276
1277         return 0;
1278 }
1279
1280 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1281 {
1282         struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1283         struct htc_target *htc_handle = hif_dev->htc_handle;
1284         int ret;
1285
1286         ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1287         if (ret)
1288                 return ret;
1289
1290         if (hif_dev->firmware) {
1291                 ret = ath9k_hif_usb_download_fw(hif_dev,
1292                                 htc_handle->drv_priv->ah->hw_version.usbdev);
1293                 if (ret)
1294                         goto fail_resume;
1295         } else {
1296                 ath9k_hif_usb_dealloc_urbs(hif_dev);
1297                 return -EIO;
1298         }
1299
1300         mdelay(100);
1301
1302         ret = ath9k_htc_resume(htc_handle);
1303
1304         if (ret)
1305                 goto fail_resume;
1306
1307         return 0;
1308
1309 fail_resume:
1310         ath9k_hif_usb_dealloc_urbs(hif_dev);
1311
1312         return ret;
1313 }
1314 #endif
1315
1316 static struct usb_driver ath9k_hif_usb_driver = {
1317         .name = KBUILD_MODNAME,
1318         .probe = ath9k_hif_usb_probe,
1319         .disconnect = ath9k_hif_usb_disconnect,
1320 #ifdef CONFIG_PM
1321         .suspend = ath9k_hif_usb_suspend,
1322         .resume = ath9k_hif_usb_resume,
1323         .reset_resume = ath9k_hif_usb_resume,
1324 #endif
1325         .id_table = ath9k_hif_usb_ids,
1326         .soft_unbind = 1,
1327 };
1328
1329 int ath9k_hif_usb_init(void)
1330 {
1331         return usb_register(&ath9k_hif_usb_driver);
1332 }
1333
1334 void ath9k_hif_usb_exit(void)
1335 {
1336         usb_deregister(&ath9k_hif_usb_driver);
1337 }