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