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