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