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