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