ath9k_htc: Simplify TX URB management
[linux-2.6.git] / drivers / net / wireless / ath / ath9k / hif_usb.c
blobcee5febb8a4d8aa8d867fa3d4cc55d7296a8e95e
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;
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;
48 if (cmd) {
49 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
50 cmd->skb, 1);
51 kfree(cmd);
54 return;
55 free:
56 kfree_skb(cmd->skb);
57 kfree(cmd);
60 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
61 struct sk_buff *skb)
63 struct urb *urb;
64 struct cmd_buf *cmd;
65 int ret = 0;
67 urb = usb_alloc_urb(0, GFP_KERNEL);
68 if (urb == NULL)
69 return -ENOMEM;
71 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
72 if (cmd == NULL) {
73 usb_free_urb(urb);
74 return -ENOMEM;
77 cmd->skb = skb;
78 cmd->hif_dev = hif_dev;
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);
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);
91 usb_free_urb(urb);
93 return ret;
96 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
97 struct sk_buff_head *list)
99 struct sk_buff *skb;
101 while ((skb = __skb_dequeue(list)) != NULL) {
102 dev_kfree_skb_any(skb);
103 TX_STAT_INC(skb_dropped);
107 static void hif_usb_tx_cb(struct urb *urb)
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;
113 if (!hif_dev || !tx_buf)
114 return;
116 switch (urb->status) {
117 case 0:
118 break;
119 case -ENOENT:
120 case -ECONNRESET:
121 case -ENODEV:
122 case -ESHUTDOWN:
124 * The URB has been killed, free the SKBs
125 * and return.
127 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
128 return;
129 default:
130 break;
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;
140 spin_unlock(&hif_dev->tx.tx_lock);
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);
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);
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);
164 /* TX lock has to be taken */
165 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
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;
173 if (hif_dev->tx.tx_skb_cnt == 0)
174 return 0;
176 /* Check if a free TX buffer is available */
177 if (list_empty(&hif_dev->tx.tx_buf))
178 return 0;
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--;
184 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
186 for (i = 0; i < tx_skb_cnt; i++) {
187 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
189 /* Should never be NULL */
190 BUG_ON(!nskb);
192 hif_dev->tx.tx_skb_cnt--;
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;
203 if (i < (tx_skb_cnt - 1))
204 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
206 if (i == (tx_skb_cnt - 1))
207 tx_buf->len += tx_buf->offset;
209 __skb_queue_tail(&tx_buf->skb_queue, nskb);
210 TX_STAT_INC(skb_queued);
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);
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++;
227 if (!ret)
228 TX_STAT_INC(buf_queued);
230 return ret;
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)
236 unsigned long flags;
238 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
240 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
241 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
242 return -ENODEV;
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;
251 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
252 hif_dev->tx.tx_skb_cnt++;
254 /* Send normal frames immediately */
255 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
256 __hif_usb_tx(hif_dev);
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);
265 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
267 return 0;
270 static void hif_usb_start(void *hif_handle, u8 pipe_id)
272 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
273 unsigned long flags;
275 hif_dev->flags |= HIF_USB_START;
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);
282 static void hif_usb_stop(void *hif_handle, u8 pipe_id)
284 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
285 unsigned long flags;
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);
294 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
295 struct ath9k_htc_tx_ctl *tx_ctl)
297 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
298 int ret = 0;
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;
314 return ret;
317 static struct ath9k_htc_hif hif_usb = {
318 .transport = ATH9K_HIF_USB,
319 .name = "ath9k_hif_usb",
321 .control_ul_pipe = USB_REG_OUT_PIPE,
322 .control_dl_pipe = USB_REG_IN_PIPE,
324 .start = hif_usb_start,
325 .stop = hif_usb_stop,
326 .send = hif_usb_send,
329 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
330 struct sk_buff *skb)
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;
338 spin_lock(&hif_dev->rx_lock);
340 rx_remain_len = hif_dev->rx_remain_len;
341 rx_pkt_len = hif_dev->rx_transfer_len;
343 if (rx_remain_len != 0) {
344 struct sk_buff *remain_skb = hif_dev->remain_skb;
346 if (remain_skb) {
347 ptr = (u8 *) remain_skb->data;
349 index = rx_remain_len;
350 rx_remain_len -= hif_dev->rx_pad_len;
351 ptr += rx_pkt_len;
353 memcpy(ptr, skb->data, rx_remain_len);
355 rx_pkt_len += rx_remain_len;
356 hif_dev->rx_remain_len = 0;
357 skb_put(remain_skb, rx_pkt_len);
359 skb_pool[pool_index++] = remain_skb;
361 } else {
362 index = rx_remain_len;
366 spin_unlock(&hif_dev->rx_lock);
368 while (index < len) {
369 ptr = (u8 *) skb->data;
371 pkt_len = ptr[index] + (ptr[index+1] << 8);
372 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
374 if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) {
375 u16 pad_len;
377 pad_len = 4 - (pkt_len & 0x3);
378 if (pad_len == 4)
379 pad_len = 0;
381 chk_idx = index;
382 index = index + 4 + pkt_len + pad_len;
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;
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;
400 skb_reserve(nskb, 32);
401 RX_STAT_INC(skb_allocated);
403 memcpy(nskb->data, &(skb->data[chk_idx+4]),
404 hif_dev->rx_transfer_len);
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;
417 skb_reserve(nskb, 32);
418 RX_STAT_INC(skb_allocated);
420 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
421 skb_put(nskb, pkt_len);
422 skb_pool[pool_index++] = nskb;
424 } else {
425 RX_STAT_INC(skb_dropped);
426 return;
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);
438 static void ath9k_hif_usb_rx_cb(struct urb *urb)
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;
445 if (!skb)
446 return;
448 if (!hif_dev)
449 goto free;
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;
463 if (likely(urb->actual_length != 0)) {
464 skb_put(skb, urb->actual_length);
465 ath9k_hif_usb_rx_stream(hif_dev, skb);
468 resubmit:
469 skb_reset_tail_pointer(skb);
470 skb_trim(skb, 0);
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;
479 return;
480 free:
481 kfree_skb(skb);
484 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
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;
492 if (!skb)
493 return;
495 if (!hif_dev)
496 goto free;
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;
510 if (likely(urb->actual_length != 0)) {
511 skb_put(skb, urb->actual_length);
513 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
514 if (!nskb)
515 goto resubmit;
517 usb_fill_int_urb(urb, hif_dev->udev,
518 usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
519 nskb->data, MAX_REG_IN_BUF_SIZE,
520 ath9k_hif_usb_reg_in_cb, nskb, 1);
522 ret = usb_submit_urb(urb, GFP_ATOMIC);
523 if (ret) {
524 kfree_skb(nskb);
525 goto free;
528 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
529 skb->len, USB_REG_IN_PIPE);
531 return;
534 resubmit:
535 skb_reset_tail_pointer(skb);
536 skb_trim(skb, 0);
538 ret = usb_submit_urb(urb, GFP_ATOMIC);
539 if (ret)
540 goto free;
542 return;
543 free:
544 kfree_skb(skb);
545 urb->context = NULL;
548 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
550 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
552 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
553 &hif_dev->tx.tx_buf, list) {
554 usb_kill_urb(tx_buf->urb);
555 list_del(&tx_buf->list);
556 usb_free_urb(tx_buf->urb);
557 kfree(tx_buf->buf);
558 kfree(tx_buf);
561 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
562 &hif_dev->tx.tx_pending, list) {
563 usb_kill_urb(tx_buf->urb);
564 list_del(&tx_buf->list);
565 usb_free_urb(tx_buf->urb);
566 kfree(tx_buf->buf);
567 kfree(tx_buf);
571 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
573 struct tx_buf *tx_buf;
574 int i;
576 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
577 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
578 spin_lock_init(&hif_dev->tx.tx_lock);
579 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
581 for (i = 0; i < MAX_TX_URB_NUM; i++) {
582 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
583 if (!tx_buf)
584 goto err;
586 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
587 if (!tx_buf->buf)
588 goto err;
590 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
591 if (!tx_buf->urb)
592 goto err;
594 tx_buf->hif_dev = hif_dev;
595 __skb_queue_head_init(&tx_buf->skb_queue);
597 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
600 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
602 return 0;
603 err:
604 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
605 return -ENOMEM;
608 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
610 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
613 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
615 struct urb *urb = NULL;
616 struct sk_buff *skb = NULL;
617 int i, ret;
619 init_usb_anchor(&hif_dev->rx_submitted);
620 spin_lock_init(&hif_dev->rx_lock);
622 for (i = 0; i < MAX_RX_URB_NUM; i++) {
624 /* Allocate URB */
625 urb = usb_alloc_urb(0, GFP_KERNEL);
626 if (urb == NULL) {
627 ret = -ENOMEM;
628 goto err_urb;
631 /* Allocate buffer */
632 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
633 if (!skb) {
634 ret = -ENOMEM;
635 goto err_skb;
638 usb_fill_bulk_urb(urb, hif_dev->udev,
639 usb_rcvbulkpipe(hif_dev->udev,
640 USB_WLAN_RX_PIPE),
641 skb->data, MAX_RX_BUF_SIZE,
642 ath9k_hif_usb_rx_cb, skb);
644 /* Anchor URB */
645 usb_anchor_urb(urb, &hif_dev->rx_submitted);
647 /* Submit URB */
648 ret = usb_submit_urb(urb, GFP_KERNEL);
649 if (ret) {
650 usb_unanchor_urb(urb);
651 goto err_submit;
655 * Drop reference count.
656 * This ensures that the URB is freed when killing them.
658 usb_free_urb(urb);
661 return 0;
663 err_submit:
664 kfree_skb(skb);
665 err_skb:
666 usb_free_urb(urb);
667 err_urb:
668 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
669 return ret;
672 static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
674 if (hif_dev->reg_in_urb) {
675 usb_kill_urb(hif_dev->reg_in_urb);
676 if (hif_dev->reg_in_urb->context)
677 kfree_skb((void *)hif_dev->reg_in_urb->context);
678 usb_free_urb(hif_dev->reg_in_urb);
679 hif_dev->reg_in_urb = NULL;
683 static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
685 struct sk_buff *skb;
687 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
688 if (hif_dev->reg_in_urb == NULL)
689 return -ENOMEM;
691 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
692 if (!skb)
693 goto err;
695 usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev,
696 usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
697 skb->data, MAX_REG_IN_BUF_SIZE,
698 ath9k_hif_usb_reg_in_cb, skb, 1);
700 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
701 goto err;
703 return 0;
705 err:
706 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
707 return -ENOMEM;
710 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
712 /* Register Write */
713 init_usb_anchor(&hif_dev->regout_submitted);
715 /* TX */
716 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
717 goto err;
719 /* RX */
720 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
721 goto err;
723 /* Register Read */
724 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
725 goto err;
727 return 0;
728 err:
729 return -ENOMEM;
732 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
734 int transfer, err;
735 const void *data = hif_dev->firmware->data;
736 size_t len = hif_dev->firmware->size;
737 u32 addr = AR9271_FIRMWARE;
738 u8 *buf = kzalloc(4096, GFP_KERNEL);
740 if (!buf)
741 return -ENOMEM;
743 while (len) {
744 transfer = min_t(int, len, 4096);
745 memcpy(buf, data, transfer);
747 err = usb_control_msg(hif_dev->udev,
748 usb_sndctrlpipe(hif_dev->udev, 0),
749 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
750 addr >> 8, 0, buf, transfer, HZ);
751 if (err < 0) {
752 kfree(buf);
753 return err;
756 len -= transfer;
757 data += transfer;
758 addr += transfer;
760 kfree(buf);
763 * Issue FW download complete command to firmware.
765 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
766 FIRMWARE_DOWNLOAD_COMP,
767 0x40 | USB_DIR_OUT,
768 AR9271_FIRMWARE_TEXT >> 8, 0, NULL, 0, HZ);
769 if (err)
770 return -EIO;
772 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
773 "ar9271.fw", (unsigned long) hif_dev->firmware->size);
775 return 0;
778 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev,
779 const char *fw_name)
781 int ret;
783 /* Request firmware */
784 ret = request_firmware(&hif_dev->firmware, fw_name, &hif_dev->udev->dev);
785 if (ret) {
786 dev_err(&hif_dev->udev->dev,
787 "ath9k_htc: Firmware - %s not found\n", fw_name);
788 goto err_fw_req;
791 /* Download firmware */
792 ret = ath9k_hif_usb_download_fw(hif_dev);
793 if (ret) {
794 dev_err(&hif_dev->udev->dev,
795 "ath9k_htc: Firmware - %s download failed\n", fw_name);
796 goto err_fw_download;
799 /* Alloc URBs */
800 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
801 if (ret) {
802 dev_err(&hif_dev->udev->dev,
803 "ath9k_htc: Unable to allocate URBs\n");
804 goto err_urb;
807 return 0;
809 err_urb:
810 /* Nothing */
811 err_fw_download:
812 release_firmware(hif_dev->firmware);
813 err_fw_req:
814 hif_dev->firmware = NULL;
815 return ret;
818 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
820 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
821 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
822 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
823 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
826 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
828 ath9k_hif_usb_dealloc_urbs(hif_dev);
829 if (hif_dev->firmware)
830 release_firmware(hif_dev->firmware);
833 static int ath9k_hif_usb_probe(struct usb_interface *interface,
834 const struct usb_device_id *id)
836 struct usb_device *udev = interface_to_usbdev(interface);
837 struct hif_device_usb *hif_dev;
838 const char *fw_name = (const char *) id->driver_info;
839 int ret = 0;
841 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
842 if (!hif_dev) {
843 ret = -ENOMEM;
844 goto err_alloc;
847 usb_get_dev(udev);
848 hif_dev->udev = udev;
849 hif_dev->interface = interface;
850 hif_dev->device_id = id->idProduct;
851 #ifdef CONFIG_PM
852 udev->reset_resume = 1;
853 #endif
854 usb_set_intfdata(interface, hif_dev);
856 ret = ath9k_hif_usb_dev_init(hif_dev, fw_name);
857 if (ret) {
858 ret = -EINVAL;
859 goto err_hif_init_usb;
862 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev);
863 if (hif_dev->htc_handle == NULL) {
864 ret = -ENOMEM;
865 goto err_htc_hw_alloc;
868 ret = ath9k_htc_hw_init(&hif_usb, hif_dev->htc_handle, hif_dev,
869 &hif_dev->udev->dev, hif_dev->device_id,
870 ATH9K_HIF_USB);
871 if (ret) {
872 ret = -EINVAL;
873 goto err_htc_hw_init;
876 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
878 return 0;
880 err_htc_hw_init:
881 ath9k_htc_hw_free(hif_dev->htc_handle);
882 err_htc_hw_alloc:
883 ath9k_hif_usb_dev_deinit(hif_dev);
884 err_hif_init_usb:
885 usb_set_intfdata(interface, NULL);
886 kfree(hif_dev);
887 usb_put_dev(udev);
888 err_alloc:
889 return ret;
892 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
894 struct usb_device *udev = interface_to_usbdev(interface);
895 struct hif_device_usb *hif_dev =
896 (struct hif_device_usb *) usb_get_intfdata(interface);
898 if (hif_dev) {
899 ath9k_htc_hw_deinit(hif_dev->htc_handle, true);
900 ath9k_htc_hw_free(hif_dev->htc_handle);
901 ath9k_hif_usb_dev_deinit(hif_dev);
902 usb_set_intfdata(interface, NULL);
905 if (hif_dev->flags & HIF_USB_START)
906 usb_reset_device(udev);
908 kfree(hif_dev);
909 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
910 usb_put_dev(udev);
913 #ifdef CONFIG_PM
914 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
915 pm_message_t message)
917 struct hif_device_usb *hif_dev =
918 (struct hif_device_usb *) usb_get_intfdata(interface);
920 ath9k_hif_usb_dealloc_urbs(hif_dev);
922 return 0;
925 static int ath9k_hif_usb_resume(struct usb_interface *interface)
927 struct hif_device_usb *hif_dev =
928 (struct hif_device_usb *) usb_get_intfdata(interface);
929 int ret;
931 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
932 if (ret)
933 return ret;
935 if (hif_dev->firmware) {
936 ret = ath9k_hif_usb_download_fw(hif_dev);
937 if (ret)
938 goto fail_resume;
939 } else {
940 ath9k_hif_usb_dealloc_urbs(hif_dev);
941 return -EIO;
944 mdelay(100);
946 ret = ath9k_htc_resume(hif_dev->htc_handle);
948 if (ret)
949 goto fail_resume;
951 return 0;
953 fail_resume:
954 ath9k_hif_usb_dealloc_urbs(hif_dev);
956 return ret;
958 #endif
960 static struct usb_driver ath9k_hif_usb_driver = {
961 .name = "ath9k_hif_usb",
962 .probe = ath9k_hif_usb_probe,
963 .disconnect = ath9k_hif_usb_disconnect,
964 #ifdef CONFIG_PM
965 .suspend = ath9k_hif_usb_suspend,
966 .resume = ath9k_hif_usb_resume,
967 .reset_resume = ath9k_hif_usb_resume,
968 #endif
969 .id_table = ath9k_hif_usb_ids,
970 .soft_unbind = 1,
973 int ath9k_hif_usb_init(void)
975 return usb_register(&ath9k_hif_usb_driver);
978 void ath9k_hif_usb_exit(void)
980 usb_deregister(&ath9k_hif_usb_driver);