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