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