ath9k_htc: Fix RX URB reference count
[linux-2.6/libata-dev.git] / drivers / net / wireless / ath / ath9k / hif_usb.c
blob4528df4a5bdc7863ac64491b8f19e55fc688f802
1 /*
2 * Copyright (c) 2010 Atheros Communications Inc.
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.
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.
17 #include "htc.h"
19 #define ATH9K_FW_USB_DEV(devid, fw) \
20 { USB_DEVICE(0x0cf3, devid), .driver_info = (unsigned long) fw }
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 { },
28 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
30 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
32 static void hif_usb_regout_cb(struct urb *urb)
34 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
35 struct hif_device_usb *hif_dev = cmd->hif_dev;
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);
44 return;
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;
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);
68 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
69 struct sk_buff *skb)
71 struct urb *urb;
72 struct cmd_buf *cmd;
73 int ret = 0;
75 urb = usb_alloc_urb(0, GFP_KERNEL);
76 if (urb == NULL)
77 return -ENOMEM;
79 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
80 if (cmd == NULL) {
81 usb_free_urb(urb);
82 return -ENOMEM;
85 cmd->skb = skb;
86 cmd->hif_dev = hif_dev;
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);
93 ret = usb_submit_urb(urb, GFP_KERNEL);
94 if (ret) {
95 usb_free_urb(urb);
96 kfree(cmd);
99 return ret;
102 static void hif_usb_tx_cb(struct urb *urb)
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;
109 if (!hif_dev)
110 return;
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;
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);
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);
141 if (flush)
142 return;
144 tx_buf->len = tx_buf->offset = 0;
145 __skb_queue_head_init(&tx_buf->skb_queue);
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);
158 /* TX lock has to be taken */
159 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
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;
167 if (hif_dev->tx.tx_skb_cnt == 0)
168 return 0;
170 /* Check if a free TX buffer is available */
171 if (list_empty(&hif_dev->tx.tx_buf))
172 return 0;
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--;
179 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
181 for (i = 0; i < tx_skb_cnt; i++) {
182 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
184 /* Should never be NULL */
185 BUG_ON(!nskb);
187 hif_dev->tx.tx_skb_cnt--;
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;
198 if (i < (tx_skb_cnt - 1))
199 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
201 if (i == (tx_skb_cnt - 1))
202 tx_buf->len += tx_buf->offset;
204 __skb_queue_tail(&tx_buf->skb_queue, nskb);
205 TX_STAT_INC(skb_queued);
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);
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++;
222 if (!ret)
223 TX_STAT_INC(buf_queued);
225 return ret;
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)
231 unsigned long flags;
233 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
235 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
236 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
237 return -ENODEV;
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;
246 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
247 hif_dev->tx.tx_skb_cnt++;
249 /* Send normal frames immediately */
250 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
251 __hif_usb_tx(hif_dev);
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);
260 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
262 return 0;
265 static void hif_usb_start(void *hif_handle, u8 pipe_id)
267 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
268 unsigned long flags;
270 hif_dev->flags |= HIF_USB_START;
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);
277 static void hif_usb_stop(void *hif_handle, u8 pipe_id)
279 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
280 unsigned long flags;
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);
289 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
290 struct ath9k_htc_tx_ctl *tx_ctl)
292 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
293 int ret = 0;
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;
309 return ret;
312 static struct ath9k_htc_hif hif_usb = {
313 .transport = ATH9K_HIF_USB,
314 .name = "ath9k_hif_usb",
316 .control_ul_pipe = USB_REG_OUT_PIPE,
317 .control_dl_pipe = USB_REG_IN_PIPE,
319 .start = hif_usb_start,
320 .stop = hif_usb_stop,
321 .send = hif_usb_send,
324 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
325 struct sk_buff *skb)
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;
333 spin_lock(&hif_dev->rx_lock);
335 rx_remain_len = hif_dev->rx_remain_len;
336 rx_pkt_len = hif_dev->rx_transfer_len;
338 if (rx_remain_len != 0) {
339 struct sk_buff *remain_skb = hif_dev->remain_skb;
341 if (remain_skb) {
342 ptr = (u8 *) remain_skb->data;
344 index = rx_remain_len;
345 rx_remain_len -= hif_dev->rx_pad_len;
346 ptr += rx_pkt_len;
348 memcpy(ptr, skb->data, rx_remain_len);
350 rx_pkt_len += rx_remain_len;
351 hif_dev->rx_remain_len = 0;
352 skb_put(remain_skb, rx_pkt_len);
354 skb_pool[pool_index++] = remain_skb;
356 } else {
357 index = rx_remain_len;
361 spin_unlock(&hif_dev->rx_lock);
363 while (index < len) {
364 ptr = (u8 *) skb->data;
366 pkt_len = ptr[index] + (ptr[index+1] << 8);
367 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
369 if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) {
370 u16 pad_len;
372 pad_len = 4 - (pkt_len & 0x3);
373 if (pad_len == 4)
374 pad_len = 0;
376 chk_idx = index;
377 index = index + 4 + pkt_len + pad_len;
379 if (index > MAX_RX_BUF_SIZE) {
380 spin_lock(&hif_dev->rx_lock);
381 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
382 hif_dev->rx_transfer_len =
383 MAX_RX_BUF_SIZE - chk_idx - 4;
384 hif_dev->rx_pad_len = pad_len;
386 nskb = __dev_alloc_skb(pkt_len + 32,
387 GFP_ATOMIC);
388 if (!nskb) {
389 dev_err(&hif_dev->udev->dev,
390 "ath9k_htc: RX memory allocation"
391 " error\n");
392 spin_unlock(&hif_dev->rx_lock);
393 goto err;
395 skb_reserve(nskb, 32);
396 RX_STAT_INC(skb_allocated);
398 memcpy(nskb->data, &(skb->data[chk_idx+4]),
399 hif_dev->rx_transfer_len);
401 /* Record the buffer pointer */
402 hif_dev->remain_skb = nskb;
403 spin_unlock(&hif_dev->rx_lock);
404 } else {
405 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
406 if (!nskb) {
407 dev_err(&hif_dev->udev->dev,
408 "ath9k_htc: RX memory allocation"
409 " error\n");
410 goto err;
412 skb_reserve(nskb, 32);
413 RX_STAT_INC(skb_allocated);
415 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
416 skb_put(nskb, pkt_len);
417 skb_pool[pool_index++] = nskb;
419 } else {
420 RX_STAT_INC(skb_dropped);
421 return;
425 err:
426 for (i = 0; i < pool_index; i++) {
427 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
428 skb_pool[i]->len, USB_WLAN_RX_PIPE);
429 RX_STAT_INC(skb_completed);
433 static void ath9k_hif_usb_rx_cb(struct urb *urb)
435 struct sk_buff *skb = (struct sk_buff *) urb->context;
436 struct hif_device_usb *hif_dev = (struct hif_device_usb *)
437 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
438 int ret;
440 if (!skb)
441 return;
443 if (!hif_dev)
444 goto free;
446 switch (urb->status) {
447 case 0:
448 break;
449 case -ENOENT:
450 case -ECONNRESET:
451 case -ENODEV:
452 case -ESHUTDOWN:
453 goto free;
454 default:
455 goto resubmit;
458 if (likely(urb->actual_length != 0)) {
459 skb_put(skb, urb->actual_length);
460 ath9k_hif_usb_rx_stream(hif_dev, skb);
463 resubmit:
464 skb_reset_tail_pointer(skb);
465 skb_trim(skb, 0);
467 usb_anchor_urb(urb, &hif_dev->rx_submitted);
468 ret = usb_submit_urb(urb, GFP_ATOMIC);
469 if (ret) {
470 usb_unanchor_urb(urb);
471 goto free;
474 return;
475 free:
476 dev_kfree_skb_any(skb);
479 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
481 struct sk_buff *skb = (struct sk_buff *) urb->context;
482 struct sk_buff *nskb;
483 struct hif_device_usb *hif_dev = (struct hif_device_usb *)
484 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
485 int ret;
487 if (!skb)
488 return;
490 if (!hif_dev)
491 goto free;
493 switch (urb->status) {
494 case 0:
495 break;
496 case -ENOENT:
497 case -ECONNRESET:
498 case -ENODEV:
499 case -ESHUTDOWN:
500 goto free;
501 default:
502 goto resubmit;
505 if (likely(urb->actual_length != 0)) {
506 skb_put(skb, urb->actual_length);
508 nskb = __dev_alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
509 if (!nskb)
510 goto resubmit;
512 usb_fill_int_urb(urb, hif_dev->udev,
513 usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
514 nskb->data, MAX_REG_IN_BUF_SIZE,
515 ath9k_hif_usb_reg_in_cb, nskb, 1);
517 ret = usb_submit_urb(urb, GFP_ATOMIC);
518 if (ret) {
519 dev_kfree_skb_any(nskb);
520 goto free;
523 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
524 skb->len, USB_REG_IN_PIPE);
526 return;
529 resubmit:
530 skb_reset_tail_pointer(skb);
531 skb_trim(skb, 0);
533 ret = usb_submit_urb(urb, GFP_ATOMIC);
534 if (ret)
535 goto free;
537 return;
538 free:
539 dev_kfree_skb_any(skb);
540 urb->context = NULL;
543 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
545 unsigned long flags;
546 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
548 list_for_each_entry_safe(tx_buf, tx_buf_tmp, &hif_dev->tx.tx_buf, list) {
549 list_del(&tx_buf->list);
550 usb_free_urb(tx_buf->urb);
551 kfree(tx_buf->buf);
552 kfree(tx_buf);
555 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
556 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
557 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
559 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
560 &hif_dev->tx.tx_pending, list) {
561 usb_kill_urb(tx_buf->urb);
562 list_del(&tx_buf->list);
563 usb_free_urb(tx_buf->urb);
564 kfree(tx_buf->buf);
565 kfree(tx_buf);
568 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
569 hif_dev->tx.flags &= ~HIF_USB_TX_FLUSH;
570 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
573 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
575 struct tx_buf *tx_buf;
576 int i;
578 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
579 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
580 spin_lock_init(&hif_dev->tx.tx_lock);
581 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
583 for (i = 0; i < MAX_TX_URB_NUM; i++) {
584 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
585 if (!tx_buf)
586 goto err;
588 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
589 if (!tx_buf->buf)
590 goto err;
592 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
593 if (!tx_buf->urb)
594 goto err;
596 tx_buf->hif_dev = hif_dev;
597 __skb_queue_head_init(&tx_buf->skb_queue);
599 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
602 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
604 return 0;
605 err:
606 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
607 return -ENOMEM;
610 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
612 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
615 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
617 struct urb *urb = NULL;
618 struct sk_buff *skb = NULL;
619 int i, ret;
621 init_usb_anchor(&hif_dev->rx_submitted);
622 spin_lock_init(&hif_dev->rx_lock);
624 for (i = 0; i < MAX_RX_URB_NUM; i++) {
626 /* Allocate URB */
627 urb = usb_alloc_urb(0, GFP_KERNEL);
628 if (urb == NULL) {
629 ret = -ENOMEM;
630 goto err_urb;
633 /* Allocate buffer */
634 skb = __dev_alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
635 if (!skb) {
636 ret = -ENOMEM;
637 goto err_skb;
640 usb_fill_bulk_urb(urb, hif_dev->udev,
641 usb_rcvbulkpipe(hif_dev->udev,
642 USB_WLAN_RX_PIPE),
643 skb->data, MAX_RX_BUF_SIZE,
644 ath9k_hif_usb_rx_cb, skb);
646 /* Anchor URB */
647 usb_anchor_urb(urb, &hif_dev->rx_submitted);
649 /* Submit URB */
650 ret = usb_submit_urb(urb, GFP_KERNEL);
651 if (ret) {
652 usb_unanchor_urb(urb);
653 goto err_submit;
657 * Drop reference count.
658 * This ensures that the URB is freed when killing them.
660 usb_free_urb(urb);
663 return 0;
665 err_submit:
666 dev_kfree_skb_any(skb);
667 err_skb:
668 usb_free_urb(urb);
669 err_urb:
670 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
671 return ret;
674 static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
676 if (hif_dev->reg_in_urb) {
677 usb_kill_urb(hif_dev->reg_in_urb);
678 if (hif_dev->reg_in_urb->context)
679 dev_kfree_skb_any((void *)hif_dev->reg_in_urb->context);
680 usb_free_urb(hif_dev->reg_in_urb);
681 hif_dev->reg_in_urb = NULL;
685 static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
687 struct sk_buff *skb;
689 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
690 if (hif_dev->reg_in_urb == NULL)
691 return -ENOMEM;
693 skb = __dev_alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
694 if (!skb)
695 goto err;
697 usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev,
698 usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
699 skb->data, MAX_REG_IN_BUF_SIZE,
700 ath9k_hif_usb_reg_in_cb, skb, 1);
702 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
703 goto err;
705 return 0;
707 err:
708 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
709 return -ENOMEM;
712 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
714 /* TX */
715 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
716 goto err;
718 /* RX */
719 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
720 goto err;
722 /* Register Read/Write */
723 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
724 goto err;
726 return 0;
727 err:
728 return -ENOMEM;
731 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
733 int transfer, err;
734 const void *data = hif_dev->firmware->data;
735 size_t len = hif_dev->firmware->size;
736 u32 addr = AR9271_FIRMWARE;
737 u8 *buf = kzalloc(4096, GFP_KERNEL);
739 if (!buf)
740 return -ENOMEM;
742 while (len) {
743 transfer = min_t(int, len, 4096);
744 memcpy(buf, data, transfer);
746 err = usb_control_msg(hif_dev->udev,
747 usb_sndctrlpipe(hif_dev->udev, 0),
748 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
749 addr >> 8, 0, buf, transfer, HZ);
750 if (err < 0) {
751 kfree(buf);
752 return err;
755 len -= transfer;
756 data += transfer;
757 addr += transfer;
759 kfree(buf);
762 * Issue FW download complete command to firmware.
764 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
765 FIRMWARE_DOWNLOAD_COMP,
766 0x40 | USB_DIR_OUT,
767 AR9271_FIRMWARE_TEXT >> 8, 0, NULL, 0, HZ);
768 if (err)
769 return -EIO;
771 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
772 "ar9271.fw", (unsigned long) hif_dev->firmware->size);
774 return 0;
777 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev,
778 const char *fw_name)
780 int ret;
782 /* Request firmware */
783 ret = request_firmware(&hif_dev->firmware, fw_name, &hif_dev->udev->dev);
784 if (ret) {
785 dev_err(&hif_dev->udev->dev,
786 "ath9k_htc: Firmware - %s not found\n", fw_name);
787 goto err_fw_req;
790 /* Download firmware */
791 ret = ath9k_hif_usb_download_fw(hif_dev);
792 if (ret) {
793 dev_err(&hif_dev->udev->dev,
794 "ath9k_htc: Firmware - %s download failed\n", fw_name);
795 goto err_fw_download;
798 /* Alloc URBs */
799 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
800 if (ret) {
801 dev_err(&hif_dev->udev->dev,
802 "ath9k_htc: Unable to allocate URBs\n");
803 goto err_urb;
806 return 0;
808 err_urb:
809 /* Nothing */
810 err_fw_download:
811 release_firmware(hif_dev->firmware);
812 err_fw_req:
813 hif_dev->firmware = NULL;
814 return ret;
817 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
819 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
820 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
821 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
824 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
826 ath9k_hif_usb_dealloc_urbs(hif_dev);
827 if (hif_dev->firmware)
828 release_firmware(hif_dev->firmware);
831 static int ath9k_hif_usb_probe(struct usb_interface *interface,
832 const struct usb_device_id *id)
834 struct usb_device *udev = interface_to_usbdev(interface);
835 struct hif_device_usb *hif_dev;
836 const char *fw_name = (const char *) id->driver_info;
837 int ret = 0;
839 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
840 if (!hif_dev) {
841 ret = -ENOMEM;
842 goto err_alloc;
845 usb_get_dev(udev);
846 hif_dev->udev = udev;
847 hif_dev->interface = interface;
848 hif_dev->device_id = id->idProduct;
849 #ifdef CONFIG_PM
850 udev->reset_resume = 1;
851 #endif
852 usb_set_intfdata(interface, hif_dev);
854 ret = ath9k_hif_usb_dev_init(hif_dev, fw_name);
855 if (ret) {
856 ret = -EINVAL;
857 goto err_hif_init_usb;
860 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev);
861 if (hif_dev->htc_handle == NULL) {
862 ret = -ENOMEM;
863 goto err_htc_hw_alloc;
866 ret = ath9k_htc_hw_init(&hif_usb, hif_dev->htc_handle, hif_dev,
867 &hif_dev->udev->dev, hif_dev->device_id,
868 ATH9K_HIF_USB);
869 if (ret) {
870 ret = -EINVAL;
871 goto err_htc_hw_init;
874 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
876 return 0;
878 err_htc_hw_init:
879 ath9k_htc_hw_free(hif_dev->htc_handle);
880 err_htc_hw_alloc:
881 ath9k_hif_usb_dev_deinit(hif_dev);
882 err_hif_init_usb:
883 usb_set_intfdata(interface, NULL);
884 kfree(hif_dev);
885 usb_put_dev(udev);
886 err_alloc:
887 return ret;
890 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
892 struct usb_device *udev = interface_to_usbdev(interface);
893 struct hif_device_usb *hif_dev =
894 (struct hif_device_usb *) usb_get_intfdata(interface);
896 if (hif_dev) {
897 ath9k_htc_hw_deinit(hif_dev->htc_handle, true);
898 ath9k_htc_hw_free(hif_dev->htc_handle);
899 ath9k_hif_usb_dev_deinit(hif_dev);
900 usb_set_intfdata(interface, NULL);
903 if (hif_dev->flags & HIF_USB_START)
904 usb_reset_device(udev);
906 kfree(hif_dev);
907 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
908 usb_put_dev(udev);
911 #ifdef CONFIG_PM
912 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
913 pm_message_t message)
915 struct hif_device_usb *hif_dev =
916 (struct hif_device_usb *) usb_get_intfdata(interface);
918 ath9k_hif_usb_dealloc_urbs(hif_dev);
920 return 0;
923 static int ath9k_hif_usb_resume(struct usb_interface *interface)
925 struct hif_device_usb *hif_dev =
926 (struct hif_device_usb *) usb_get_intfdata(interface);
927 int ret;
929 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
930 if (ret)
931 return ret;
933 if (hif_dev->firmware) {
934 ret = ath9k_hif_usb_download_fw(hif_dev);
935 if (ret)
936 goto fail_resume;
937 } else {
938 ath9k_hif_usb_dealloc_urbs(hif_dev);
939 return -EIO;
942 mdelay(100);
944 ret = ath9k_htc_resume(hif_dev->htc_handle);
946 if (ret)
947 goto fail_resume;
949 return 0;
951 fail_resume:
952 ath9k_hif_usb_dealloc_urbs(hif_dev);
954 return ret;
956 #endif
958 static struct usb_driver ath9k_hif_usb_driver = {
959 .name = "ath9k_hif_usb",
960 .probe = ath9k_hif_usb_probe,
961 .disconnect = ath9k_hif_usb_disconnect,
962 #ifdef CONFIG_PM
963 .suspend = ath9k_hif_usb_suspend,
964 .resume = ath9k_hif_usb_resume,
965 .reset_resume = ath9k_hif_usb_resume,
966 #endif
967 .id_table = ath9k_hif_usb_ids,
968 .soft_unbind = 1,
971 int ath9k_hif_usb_init(void)
973 return usb_register(&ath9k_hif_usb_driver);
976 void ath9k_hif_usb_exit(void)
978 usb_deregister(&ath9k_hif_usb_driver);