OSDN Git Service

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