2 * Copyright (c) 2010-2011 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 <asm/unaligned.h>
20 /* identify firmware images */
21 #define FIRMWARE_AR7010_1_1 "htc_7010.fw"
22 #define FIRMWARE_AR9271 "htc_9271.fw"
24 MODULE_FIRMWARE(FIRMWARE_AR7010_1_1
);
25 MODULE_FIRMWARE(FIRMWARE_AR9271
);
27 static struct usb_device_id ath9k_hif_usb_ids
[] = {
28 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
29 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
30 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
31 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
32 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
33 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
34 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
35 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
36 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
37 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
38 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
39 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
40 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
41 { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
43 { USB_DEVICE(0x0cf3, 0x7015),
44 .driver_info
= AR9287_USB
}, /* Atheros */
45 { USB_DEVICE(0x1668, 0x1200),
46 .driver_info
= AR9287_USB
}, /* Verizon */
48 { USB_DEVICE(0x0cf3, 0x7010),
49 .driver_info
= AR9280_USB
}, /* Atheros */
50 { USB_DEVICE(0x0846, 0x9018),
51 .driver_info
= AR9280_USB
}, /* Netgear WNDA3200 */
52 { USB_DEVICE(0x083A, 0xA704),
53 .driver_info
= AR9280_USB
}, /* SMC Networks */
54 { USB_DEVICE(0x0411, 0x017f),
55 .driver_info
= AR9280_USB
}, /* Sony UWA-BR100 */
57 { USB_DEVICE(0x0cf3, 0x20ff),
58 .driver_info
= STORAGE_DEVICE
},
63 MODULE_DEVICE_TABLE(usb
, ath9k_hif_usb_ids
);
65 static int __hif_usb_tx(struct hif_device_usb
*hif_dev
);
67 static void hif_usb_regout_cb(struct urb
*urb
)
69 struct cmd_buf
*cmd
= (struct cmd_buf
*)urb
->context
;
71 switch (urb
->status
) {
84 ath9k_htc_txcompletion_cb(cmd
->hif_dev
->htc_handle
,
95 static int hif_usb_send_regout(struct hif_device_usb
*hif_dev
,
102 urb
= usb_alloc_urb(0, GFP_KERNEL
);
106 cmd
= kzalloc(sizeof(*cmd
), GFP_KERNEL
);
113 cmd
->hif_dev
= hif_dev
;
115 usb_fill_bulk_urb(urb
, hif_dev
->udev
,
116 usb_sndbulkpipe(hif_dev
->udev
, USB_REG_OUT_PIPE
),
118 hif_usb_regout_cb
, cmd
);
120 usb_anchor_urb(urb
, &hif_dev
->regout_submitted
);
121 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
123 usb_unanchor_urb(urb
);
131 static void hif_usb_mgmt_cb(struct urb
*urb
)
133 struct cmd_buf
*cmd
= (struct cmd_buf
*)urb
->context
;
134 struct hif_device_usb
*hif_dev
;
137 if (!cmd
|| !cmd
->skb
|| !cmd
->hif_dev
)
140 hif_dev
= cmd
->hif_dev
;
142 switch (urb
->status
) {
152 * If the URBs are being flushed, no need to complete
155 spin_lock(&hif_dev
->tx
.tx_lock
);
156 if (hif_dev
->tx
.flags
& HIF_USB_TX_FLUSH
) {
157 spin_unlock(&hif_dev
->tx
.tx_lock
);
158 dev_kfree_skb_any(cmd
->skb
);
162 spin_unlock(&hif_dev
->tx
.tx_lock
);
170 skb_pull(cmd
->skb
, 4);
171 ath9k_htc_txcompletion_cb(cmd
->hif_dev
->htc_handle
,
176 static int hif_usb_send_mgmt(struct hif_device_usb
*hif_dev
,
184 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
188 cmd
= kzalloc(sizeof(*cmd
), GFP_ATOMIC
);
195 cmd
->hif_dev
= hif_dev
;
197 hdr
= (__le16
*) skb_push(skb
, 4);
198 *hdr
++ = cpu_to_le16(skb
->len
- 4);
199 *hdr
++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG
);
201 usb_fill_bulk_urb(urb
, hif_dev
->udev
,
202 usb_sndbulkpipe(hif_dev
->udev
, USB_WLAN_TX_PIPE
),
204 hif_usb_mgmt_cb
, cmd
);
206 usb_anchor_urb(urb
, &hif_dev
->mgmt_submitted
);
207 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
209 usb_unanchor_urb(urb
);
217 static inline void ath9k_skb_queue_purge(struct hif_device_usb
*hif_dev
,
218 struct sk_buff_head
*list
)
222 while ((skb
= __skb_dequeue(list
)) != NULL
) {
223 dev_kfree_skb_any(skb
);
227 static inline void ath9k_skb_queue_complete(struct hif_device_usb
*hif_dev
,
228 struct sk_buff_head
*queue
,
233 while ((skb
= __skb_dequeue(queue
)) != NULL
) {
234 ath9k_htc_txcompletion_cb(hif_dev
->htc_handle
,
237 TX_STAT_INC(skb_success
);
239 TX_STAT_INC(skb_failed
);
243 static void hif_usb_tx_cb(struct urb
*urb
)
245 struct tx_buf
*tx_buf
= (struct tx_buf
*) urb
->context
;
246 struct hif_device_usb
*hif_dev
;
249 if (!tx_buf
|| !tx_buf
->hif_dev
)
252 hif_dev
= tx_buf
->hif_dev
;
254 switch (urb
->status
) {
264 * If the URBs are being flushed, no need to add this
265 * URB to the free list.
267 spin_lock(&hif_dev
->tx
.tx_lock
);
268 if (hif_dev
->tx
.flags
& HIF_USB_TX_FLUSH
) {
269 spin_unlock(&hif_dev
->tx
.tx_lock
);
270 ath9k_skb_queue_purge(hif_dev
, &tx_buf
->skb_queue
);
273 spin_unlock(&hif_dev
->tx
.tx_lock
);
281 ath9k_skb_queue_complete(hif_dev
, &tx_buf
->skb_queue
, txok
);
283 /* Re-initialize the SKB queue */
284 tx_buf
->len
= tx_buf
->offset
= 0;
285 __skb_queue_head_init(&tx_buf
->skb_queue
);
287 /* Add this TX buffer to the free list */
288 spin_lock(&hif_dev
->tx
.tx_lock
);
289 list_move_tail(&tx_buf
->list
, &hif_dev
->tx
.tx_buf
);
290 hif_dev
->tx
.tx_buf_cnt
++;
291 if (!(hif_dev
->tx
.flags
& HIF_USB_TX_STOP
))
292 __hif_usb_tx(hif_dev
); /* Check for pending SKBs */
293 TX_STAT_INC(buf_completed
);
294 spin_unlock(&hif_dev
->tx
.tx_lock
);
297 /* TX lock has to be taken */
298 static int __hif_usb_tx(struct hif_device_usb
*hif_dev
)
300 struct tx_buf
*tx_buf
= NULL
;
301 struct sk_buff
*nskb
= NULL
;
307 if (hif_dev
->tx
.tx_skb_cnt
== 0)
310 /* Check if a free TX buffer is available */
311 if (list_empty(&hif_dev
->tx
.tx_buf
))
314 tx_buf
= list_first_entry(&hif_dev
->tx
.tx_buf
, struct tx_buf
, list
);
315 list_move_tail(&tx_buf
->list
, &hif_dev
->tx
.tx_pending
);
316 hif_dev
->tx
.tx_buf_cnt
--;
318 tx_skb_cnt
= min_t(u16
, hif_dev
->tx
.tx_skb_cnt
, MAX_TX_AGGR_NUM
);
320 for (i
= 0; i
< tx_skb_cnt
; i
++) {
321 nskb
= __skb_dequeue(&hif_dev
->tx
.tx_skb_queue
);
323 /* Should never be NULL */
326 hif_dev
->tx
.tx_skb_cnt
--;
329 buf
+= tx_buf
->offset
;
331 *hdr
++ = cpu_to_le16(nskb
->len
);
332 *hdr
++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG
);
334 memcpy(buf
, nskb
->data
, nskb
->len
);
335 tx_buf
->len
= nskb
->len
+ 4;
337 if (i
< (tx_skb_cnt
- 1))
338 tx_buf
->offset
+= (((tx_buf
->len
- 1) / 4) + 1) * 4;
340 if (i
== (tx_skb_cnt
- 1))
341 tx_buf
->len
+= tx_buf
->offset
;
343 __skb_queue_tail(&tx_buf
->skb_queue
, nskb
);
344 TX_STAT_INC(skb_queued
);
347 usb_fill_bulk_urb(tx_buf
->urb
, hif_dev
->udev
,
348 usb_sndbulkpipe(hif_dev
->udev
, USB_WLAN_TX_PIPE
),
349 tx_buf
->buf
, tx_buf
->len
,
350 hif_usb_tx_cb
, tx_buf
);
352 ret
= usb_submit_urb(tx_buf
->urb
, GFP_ATOMIC
);
354 tx_buf
->len
= tx_buf
->offset
= 0;
355 ath9k_skb_queue_complete(hif_dev
, &tx_buf
->skb_queue
, false);
356 __skb_queue_head_init(&tx_buf
->skb_queue
);
357 list_move_tail(&tx_buf
->list
, &hif_dev
->tx
.tx_buf
);
358 hif_dev
->tx
.tx_buf_cnt
++;
362 TX_STAT_INC(buf_queued
);
367 static int hif_usb_send_tx(struct hif_device_usb
*hif_dev
, struct sk_buff
*skb
)
369 struct ath9k_htc_tx_ctl
*tx_ctl
;
373 spin_lock_irqsave(&hif_dev
->tx
.tx_lock
, flags
);
375 if (hif_dev
->tx
.flags
& HIF_USB_TX_STOP
) {
376 spin_unlock_irqrestore(&hif_dev
->tx
.tx_lock
, flags
);
380 /* Check if the max queue count has been reached */
381 if (hif_dev
->tx
.tx_skb_cnt
> MAX_TX_BUF_NUM
) {
382 spin_unlock_irqrestore(&hif_dev
->tx
.tx_lock
, flags
);
386 spin_unlock_irqrestore(&hif_dev
->tx
.tx_lock
, flags
);
388 tx_ctl
= HTC_SKB_CB(skb
);
390 /* Mgmt/Beacon frames don't use the TX buffer pool */
391 if ((tx_ctl
->type
== ATH9K_HTC_MGMT
) ||
392 (tx_ctl
->type
== ATH9K_HTC_BEACON
)) {
393 ret
= hif_usb_send_mgmt(hif_dev
, skb
);
396 spin_lock_irqsave(&hif_dev
->tx
.tx_lock
, flags
);
398 if ((tx_ctl
->type
== ATH9K_HTC_NORMAL
) ||
399 (tx_ctl
->type
== ATH9K_HTC_AMPDU
)) {
400 __skb_queue_tail(&hif_dev
->tx
.tx_skb_queue
, skb
);
401 hif_dev
->tx
.tx_skb_cnt
++;
404 /* Check if AMPDUs have to be sent immediately */
405 if ((hif_dev
->tx
.tx_buf_cnt
== MAX_TX_URB_NUM
) &&
406 (hif_dev
->tx
.tx_skb_cnt
< 2)) {
407 __hif_usb_tx(hif_dev
);
410 spin_unlock_irqrestore(&hif_dev
->tx
.tx_lock
, flags
);
415 static void hif_usb_start(void *hif_handle
)
417 struct hif_device_usb
*hif_dev
= (struct hif_device_usb
*)hif_handle
;
420 hif_dev
->flags
|= HIF_USB_START
;
422 spin_lock_irqsave(&hif_dev
->tx
.tx_lock
, flags
);
423 hif_dev
->tx
.flags
&= ~HIF_USB_TX_STOP
;
424 spin_unlock_irqrestore(&hif_dev
->tx
.tx_lock
, flags
);
427 static void hif_usb_stop(void *hif_handle
)
429 struct hif_device_usb
*hif_dev
= (struct hif_device_usb
*)hif_handle
;
430 struct tx_buf
*tx_buf
= NULL
, *tx_buf_tmp
= NULL
;
433 spin_lock_irqsave(&hif_dev
->tx
.tx_lock
, flags
);
434 ath9k_skb_queue_complete(hif_dev
, &hif_dev
->tx
.tx_skb_queue
, false);
435 hif_dev
->tx
.tx_skb_cnt
= 0;
436 hif_dev
->tx
.flags
|= HIF_USB_TX_STOP
;
437 spin_unlock_irqrestore(&hif_dev
->tx
.tx_lock
, flags
);
439 /* The pending URBs have to be canceled. */
440 list_for_each_entry_safe(tx_buf
, tx_buf_tmp
,
441 &hif_dev
->tx
.tx_pending
, list
) {
442 usb_kill_urb(tx_buf
->urb
);
445 usb_kill_anchored_urbs(&hif_dev
->mgmt_submitted
);
448 static int hif_usb_send(void *hif_handle
, u8 pipe_id
, struct sk_buff
*skb
)
450 struct hif_device_usb
*hif_dev
= (struct hif_device_usb
*)hif_handle
;
454 case USB_WLAN_TX_PIPE
:
455 ret
= hif_usb_send_tx(hif_dev
, skb
);
457 case USB_REG_OUT_PIPE
:
458 ret
= hif_usb_send_regout(hif_dev
, skb
);
461 dev_err(&hif_dev
->udev
->dev
,
462 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id
);
470 static inline bool check_index(struct sk_buff
*skb
, u8 idx
)
472 struct ath9k_htc_tx_ctl
*tx_ctl
;
474 tx_ctl
= HTC_SKB_CB(skb
);
476 if ((tx_ctl
->type
== ATH9K_HTC_AMPDU
) &&
477 (tx_ctl
->sta_idx
== idx
))
483 static void hif_usb_sta_drain(void *hif_handle
, u8 idx
)
485 struct hif_device_usb
*hif_dev
= (struct hif_device_usb
*)hif_handle
;
486 struct sk_buff
*skb
, *tmp
;
489 spin_lock_irqsave(&hif_dev
->tx
.tx_lock
, flags
);
491 skb_queue_walk_safe(&hif_dev
->tx
.tx_skb_queue
, skb
, tmp
) {
492 if (check_index(skb
, idx
)) {
493 __skb_unlink(skb
, &hif_dev
->tx
.tx_skb_queue
);
494 ath9k_htc_txcompletion_cb(hif_dev
->htc_handle
,
496 hif_dev
->tx
.tx_skb_cnt
--;
497 TX_STAT_INC(skb_failed
);
501 spin_unlock_irqrestore(&hif_dev
->tx
.tx_lock
, flags
);
504 static struct ath9k_htc_hif hif_usb
= {
505 .transport
= ATH9K_HIF_USB
,
506 .name
= "ath9k_hif_usb",
508 .control_ul_pipe
= USB_REG_OUT_PIPE
,
509 .control_dl_pipe
= USB_REG_IN_PIPE
,
511 .start
= hif_usb_start
,
512 .stop
= hif_usb_stop
,
513 .sta_drain
= hif_usb_sta_drain
,
514 .send
= hif_usb_send
,
517 static void ath9k_hif_usb_rx_stream(struct hif_device_usb
*hif_dev
,
520 struct sk_buff
*nskb
, *skb_pool
[MAX_PKT_NUM_IN_TRANSFER
];
521 int index
= 0, i
= 0, len
= skb
->len
;
522 int rx_remain_len
, rx_pkt_len
;
526 spin_lock(&hif_dev
->rx_lock
);
528 rx_remain_len
= hif_dev
->rx_remain_len
;
529 rx_pkt_len
= hif_dev
->rx_transfer_len
;
531 if (rx_remain_len
!= 0) {
532 struct sk_buff
*remain_skb
= hif_dev
->remain_skb
;
535 ptr
= (u8
*) remain_skb
->data
;
537 index
= rx_remain_len
;
538 rx_remain_len
-= hif_dev
->rx_pad_len
;
541 memcpy(ptr
, skb
->data
, rx_remain_len
);
543 rx_pkt_len
+= rx_remain_len
;
544 hif_dev
->rx_remain_len
= 0;
545 skb_put(remain_skb
, rx_pkt_len
);
547 skb_pool
[pool_index
++] = remain_skb
;
550 index
= rx_remain_len
;
554 spin_unlock(&hif_dev
->rx_lock
);
556 while (index
< len
) {
562 ptr
= (u8
*) skb
->data
;
564 pkt_len
= get_unaligned_le16(ptr
+ index
);
565 pkt_tag
= get_unaligned_le16(ptr
+ index
+ 2);
567 if (pkt_tag
!= ATH_USB_RX_STREAM_MODE_TAG
) {
568 RX_STAT_INC(skb_dropped
);
572 pad_len
= 4 - (pkt_len
& 0x3);
577 index
= index
+ 4 + pkt_len
+ pad_len
;
579 if (index
> MAX_RX_BUF_SIZE
) {
580 spin_lock(&hif_dev
->rx_lock
);
581 hif_dev
->rx_remain_len
= index
- MAX_RX_BUF_SIZE
;
582 hif_dev
->rx_transfer_len
=
583 MAX_RX_BUF_SIZE
- chk_idx
- 4;
584 hif_dev
->rx_pad_len
= pad_len
;
586 nskb
= __dev_alloc_skb(pkt_len
+ 32, GFP_ATOMIC
);
588 dev_err(&hif_dev
->udev
->dev
,
589 "ath9k_htc: RX memory allocation error\n");
590 spin_unlock(&hif_dev
->rx_lock
);
593 skb_reserve(nskb
, 32);
594 RX_STAT_INC(skb_allocated
);
596 memcpy(nskb
->data
, &(skb
->data
[chk_idx
+4]),
597 hif_dev
->rx_transfer_len
);
599 /* Record the buffer pointer */
600 hif_dev
->remain_skb
= nskb
;
601 spin_unlock(&hif_dev
->rx_lock
);
603 nskb
= __dev_alloc_skb(pkt_len
+ 32, GFP_ATOMIC
);
605 dev_err(&hif_dev
->udev
->dev
,
606 "ath9k_htc: RX memory allocation error\n");
609 skb_reserve(nskb
, 32);
610 RX_STAT_INC(skb_allocated
);
612 memcpy(nskb
->data
, &(skb
->data
[chk_idx
+4]), pkt_len
);
613 skb_put(nskb
, pkt_len
);
614 skb_pool
[pool_index
++] = nskb
;
619 for (i
= 0; i
< pool_index
; i
++) {
620 ath9k_htc_rx_msg(hif_dev
->htc_handle
, skb_pool
[i
],
621 skb_pool
[i
]->len
, USB_WLAN_RX_PIPE
);
622 RX_STAT_INC(skb_completed
);
626 static void ath9k_hif_usb_rx_cb(struct urb
*urb
)
628 struct sk_buff
*skb
= (struct sk_buff
*) urb
->context
;
629 struct hif_device_usb
*hif_dev
=
630 usb_get_intfdata(usb_ifnum_to_if(urb
->dev
, 0));
639 switch (urb
->status
) {
651 if (likely(urb
->actual_length
!= 0)) {
652 skb_put(skb
, urb
->actual_length
);
653 ath9k_hif_usb_rx_stream(hif_dev
, skb
);
657 skb_reset_tail_pointer(skb
);
660 usb_anchor_urb(urb
, &hif_dev
->rx_submitted
);
661 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
663 usb_unanchor_urb(urb
);
672 static void ath9k_hif_usb_reg_in_cb(struct urb
*urb
)
674 struct sk_buff
*skb
= (struct sk_buff
*) urb
->context
;
675 struct sk_buff
*nskb
;
676 struct hif_device_usb
*hif_dev
=
677 usb_get_intfdata(usb_ifnum_to_if(urb
->dev
, 0));
686 switch (urb
->status
) {
695 skb_reset_tail_pointer(skb
);
701 if (likely(urb
->actual_length
!= 0)) {
702 skb_put(skb
, urb
->actual_length
);
704 /* Process the command first */
705 ath9k_htc_rx_msg(hif_dev
->htc_handle
, skb
,
706 skb
->len
, USB_REG_IN_PIPE
);
709 nskb
= alloc_skb(MAX_REG_IN_BUF_SIZE
, GFP_ATOMIC
);
711 dev_err(&hif_dev
->udev
->dev
,
712 "ath9k_htc: REG_IN memory allocation failure\n");
717 usb_fill_bulk_urb(urb
, hif_dev
->udev
,
718 usb_rcvbulkpipe(hif_dev
->udev
,
720 nskb
->data
, MAX_REG_IN_BUF_SIZE
,
721 ath9k_hif_usb_reg_in_cb
, nskb
);
725 usb_anchor_urb(urb
, &hif_dev
->reg_in_submitted
);
726 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
728 usb_unanchor_urb(urb
);
738 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb
*hif_dev
)
740 struct tx_buf
*tx_buf
= NULL
, *tx_buf_tmp
= NULL
;
743 list_for_each_entry_safe(tx_buf
, tx_buf_tmp
,
744 &hif_dev
->tx
.tx_buf
, list
) {
745 usb_kill_urb(tx_buf
->urb
);
746 list_del(&tx_buf
->list
);
747 usb_free_urb(tx_buf
->urb
);
752 spin_lock_irqsave(&hif_dev
->tx
.tx_lock
, flags
);
753 hif_dev
->tx
.flags
|= HIF_USB_TX_FLUSH
;
754 spin_unlock_irqrestore(&hif_dev
->tx
.tx_lock
, flags
);
756 list_for_each_entry_safe(tx_buf
, tx_buf_tmp
,
757 &hif_dev
->tx
.tx_pending
, list
) {
758 usb_kill_urb(tx_buf
->urb
);
759 list_del(&tx_buf
->list
);
760 usb_free_urb(tx_buf
->urb
);
765 usb_kill_anchored_urbs(&hif_dev
->mgmt_submitted
);
768 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb
*hif_dev
)
770 struct tx_buf
*tx_buf
;
773 INIT_LIST_HEAD(&hif_dev
->tx
.tx_buf
);
774 INIT_LIST_HEAD(&hif_dev
->tx
.tx_pending
);
775 spin_lock_init(&hif_dev
->tx
.tx_lock
);
776 __skb_queue_head_init(&hif_dev
->tx
.tx_skb_queue
);
777 init_usb_anchor(&hif_dev
->mgmt_submitted
);
779 for (i
= 0; i
< MAX_TX_URB_NUM
; i
++) {
780 tx_buf
= kzalloc(sizeof(struct tx_buf
), GFP_KERNEL
);
784 tx_buf
->buf
= kzalloc(MAX_TX_BUF_SIZE
, GFP_KERNEL
);
788 tx_buf
->urb
= usb_alloc_urb(0, GFP_KERNEL
);
792 tx_buf
->hif_dev
= hif_dev
;
793 __skb_queue_head_init(&tx_buf
->skb_queue
);
795 list_add_tail(&tx_buf
->list
, &hif_dev
->tx
.tx_buf
);
798 hif_dev
->tx
.tx_buf_cnt
= MAX_TX_URB_NUM
;
806 ath9k_hif_usb_dealloc_tx_urbs(hif_dev
);
810 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb
*hif_dev
)
812 usb_kill_anchored_urbs(&hif_dev
->rx_submitted
);
815 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb
*hif_dev
)
817 struct urb
*urb
= NULL
;
818 struct sk_buff
*skb
= NULL
;
821 init_usb_anchor(&hif_dev
->rx_submitted
);
822 spin_lock_init(&hif_dev
->rx_lock
);
824 for (i
= 0; i
< MAX_RX_URB_NUM
; i
++) {
827 urb
= usb_alloc_urb(0, GFP_KERNEL
);
833 /* Allocate buffer */
834 skb
= alloc_skb(MAX_RX_BUF_SIZE
, GFP_KERNEL
);
840 usb_fill_bulk_urb(urb
, hif_dev
->udev
,
841 usb_rcvbulkpipe(hif_dev
->udev
,
843 skb
->data
, MAX_RX_BUF_SIZE
,
844 ath9k_hif_usb_rx_cb
, skb
);
847 usb_anchor_urb(urb
, &hif_dev
->rx_submitted
);
850 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
852 usb_unanchor_urb(urb
);
857 * Drop reference count.
858 * This ensures that the URB is freed when killing them.
870 ath9k_hif_usb_dealloc_rx_urbs(hif_dev
);
874 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb
*hif_dev
)
876 usb_kill_anchored_urbs(&hif_dev
->reg_in_submitted
);
879 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb
*hif_dev
)
881 struct urb
*urb
= NULL
;
882 struct sk_buff
*skb
= NULL
;
885 init_usb_anchor(&hif_dev
->reg_in_submitted
);
887 for (i
= 0; i
< MAX_REG_IN_URB_NUM
; i
++) {
890 urb
= usb_alloc_urb(0, GFP_KERNEL
);
896 /* Allocate buffer */
897 skb
= alloc_skb(MAX_REG_IN_BUF_SIZE
, GFP_KERNEL
);
903 usb_fill_bulk_urb(urb
, hif_dev
->udev
,
904 usb_rcvbulkpipe(hif_dev
->udev
,
906 skb
->data
, MAX_REG_IN_BUF_SIZE
,
907 ath9k_hif_usb_reg_in_cb
, skb
);
910 usb_anchor_urb(urb
, &hif_dev
->reg_in_submitted
);
913 ret
= usb_submit_urb(urb
, GFP_KERNEL
);
915 usb_unanchor_urb(urb
);
920 * Drop reference count.
921 * This ensures that the URB is freed when killing them.
933 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev
);
937 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb
*hif_dev
)
940 init_usb_anchor(&hif_dev
->regout_submitted
);
943 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev
) < 0)
947 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev
) < 0)
951 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev
) < 0)
956 ath9k_hif_usb_dealloc_rx_urbs(hif_dev
);
958 ath9k_hif_usb_dealloc_tx_urbs(hif_dev
);
963 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb
*hif_dev
)
965 usb_kill_anchored_urbs(&hif_dev
->regout_submitted
);
966 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev
);
967 ath9k_hif_usb_dealloc_tx_urbs(hif_dev
);
968 ath9k_hif_usb_dealloc_rx_urbs(hif_dev
);
971 static int ath9k_hif_usb_download_fw(struct hif_device_usb
*hif_dev
)
974 const void *data
= hif_dev
->firmware
->data
;
975 size_t len
= hif_dev
->firmware
->size
;
976 u32 addr
= AR9271_FIRMWARE
;
977 u8
*buf
= kzalloc(4096, GFP_KERNEL
);
984 transfer
= min_t(size_t, len
, 4096);
985 memcpy(buf
, data
, transfer
);
987 err
= usb_control_msg(hif_dev
->udev
,
988 usb_sndctrlpipe(hif_dev
->udev
, 0),
989 FIRMWARE_DOWNLOAD
, 0x40 | USB_DIR_OUT
,
990 addr
>> 8, 0, buf
, transfer
, HZ
);
1002 if (IS_AR7010_DEVICE(hif_dev
->usb_device_id
->driver_info
))
1003 firm_offset
= AR7010_FIRMWARE_TEXT
;
1005 firm_offset
= AR9271_FIRMWARE_TEXT
;
1008 * Issue FW download complete command to firmware.
1010 err
= usb_control_msg(hif_dev
->udev
, usb_sndctrlpipe(hif_dev
->udev
, 0),
1011 FIRMWARE_DOWNLOAD_COMP
,
1013 firm_offset
>> 8, 0, NULL
, 0, HZ
);
1017 dev_info(&hif_dev
->udev
->dev
, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1018 hif_dev
->fw_name
, (unsigned long) hif_dev
->firmware
->size
);
1023 static int ath9k_hif_usb_dev_init(struct hif_device_usb
*hif_dev
)
1025 struct usb_host_interface
*alt
= &hif_dev
->interface
->altsetting
[0];
1026 struct usb_endpoint_descriptor
*endp
;
1029 ret
= ath9k_hif_usb_download_fw(hif_dev
);
1031 dev_err(&hif_dev
->udev
->dev
,
1032 "ath9k_htc: Firmware - %s download failed\n",
1037 /* On downloading the firmware to the target, the USB descriptor of EP4
1038 * is 'patched' to change the type of the endpoint to Bulk. This will
1039 * bring down CPU usage during the scan period.
1041 for (idx
= 0; idx
< alt
->desc
.bNumEndpoints
; idx
++) {
1042 endp
= &alt
->endpoint
[idx
].desc
;
1043 if ((endp
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
)
1044 == USB_ENDPOINT_XFER_INT
) {
1045 endp
->bmAttributes
&= ~USB_ENDPOINT_XFERTYPE_MASK
;
1046 endp
->bmAttributes
|= USB_ENDPOINT_XFER_BULK
;
1047 endp
->bInterval
= 0;
1052 ret
= ath9k_hif_usb_alloc_urbs(hif_dev
);
1054 dev_err(&hif_dev
->udev
->dev
,
1055 "ath9k_htc: Unable to allocate URBs\n");
1062 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb
*hif_dev
)
1064 ath9k_hif_usb_dealloc_urbs(hif_dev
);
1068 * If initialization fails or the FW cannot be retrieved,
1069 * detach the device.
1071 static void ath9k_hif_usb_firmware_fail(struct hif_device_usb
*hif_dev
)
1073 struct device
*parent
= hif_dev
->udev
->dev
.parent
;
1075 complete(&hif_dev
->fw_done
);
1078 device_lock(parent
);
1080 device_release_driver(&hif_dev
->udev
->dev
);
1083 device_unlock(parent
);
1086 static void ath9k_hif_usb_firmware_cb(const struct firmware
*fw
, void *context
)
1088 struct hif_device_usb
*hif_dev
= context
;
1092 dev_err(&hif_dev
->udev
->dev
,
1093 "ath9k_htc: Failed to get firmware %s\n",
1098 hif_dev
->htc_handle
= ath9k_htc_hw_alloc(hif_dev
, &hif_usb
,
1099 &hif_dev
->udev
->dev
);
1100 if (hif_dev
->htc_handle
== NULL
) {
1104 hif_dev
->firmware
= fw
;
1106 /* Proceed with initialization */
1108 ret
= ath9k_hif_usb_dev_init(hif_dev
);
1112 ret
= ath9k_htc_hw_init(hif_dev
->htc_handle
,
1113 &hif_dev
->interface
->dev
,
1114 hif_dev
->usb_device_id
->idProduct
,
1115 hif_dev
->udev
->product
,
1116 hif_dev
->usb_device_id
->driver_info
);
1119 goto err_htc_hw_init
;
1122 complete(&hif_dev
->fw_done
);
1127 ath9k_hif_usb_dev_deinit(hif_dev
);
1129 ath9k_htc_hw_free(hif_dev
->htc_handle
);
1130 release_firmware(fw
);
1131 hif_dev
->firmware
= NULL
;
1133 ath9k_hif_usb_firmware_fail(hif_dev
);
1137 * An exact copy of the function from zd1211rw.
1139 static int send_eject_command(struct usb_interface
*interface
)
1141 struct usb_device
*udev
= interface_to_usbdev(interface
);
1142 struct usb_host_interface
*iface_desc
= &interface
->altsetting
[0];
1143 struct usb_endpoint_descriptor
*endpoint
;
1148 /* Find bulk out endpoint */
1149 for (r
= 1; r
>= 0; r
--) {
1150 endpoint
= &iface_desc
->endpoint
[r
].desc
;
1151 if (usb_endpoint_dir_out(endpoint
) &&
1152 usb_endpoint_xfer_bulk(endpoint
)) {
1153 bulk_out_ep
= endpoint
->bEndpointAddress
;
1159 "ath9k_htc: Could not find bulk out endpoint\n");
1163 cmd
= kzalloc(31, GFP_KERNEL
);
1167 /* USB bulk command block */
1168 cmd
[0] = 0x55; /* bulk command signature */
1169 cmd
[1] = 0x53; /* bulk command signature */
1170 cmd
[2] = 0x42; /* bulk command signature */
1171 cmd
[3] = 0x43; /* bulk command signature */
1172 cmd
[14] = 6; /* command length */
1174 cmd
[15] = 0x1b; /* SCSI command: START STOP UNIT */
1175 cmd
[19] = 0x2; /* eject disc */
1177 dev_info(&udev
->dev
, "Ejecting storage device...\n");
1178 r
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, bulk_out_ep
),
1179 cmd
, 31, NULL
, 2000);
1184 /* At this point, the device disconnects and reconnects with the real
1187 usb_set_intfdata(interface
, NULL
);
1191 static int ath9k_hif_usb_probe(struct usb_interface
*interface
,
1192 const struct usb_device_id
*id
)
1194 struct usb_device
*udev
= interface_to_usbdev(interface
);
1195 struct hif_device_usb
*hif_dev
;
1198 if (id
->driver_info
== STORAGE_DEVICE
)
1199 return send_eject_command(interface
);
1201 hif_dev
= kzalloc(sizeof(struct hif_device_usb
), GFP_KERNEL
);
1209 hif_dev
->udev
= udev
;
1210 hif_dev
->interface
= interface
;
1211 hif_dev
->usb_device_id
= id
;
1213 udev
->reset_resume
= 1;
1215 usb_set_intfdata(interface
, hif_dev
);
1217 init_completion(&hif_dev
->fw_done
);
1219 /* Find out which firmware to load */
1221 if (IS_AR7010_DEVICE(id
->driver_info
))
1222 hif_dev
->fw_name
= FIRMWARE_AR7010_1_1
;
1224 hif_dev
->fw_name
= FIRMWARE_AR9271
;
1226 ret
= request_firmware_nowait(THIS_MODULE
, true, hif_dev
->fw_name
,
1227 &hif_dev
->udev
->dev
, GFP_KERNEL
,
1228 hif_dev
, ath9k_hif_usb_firmware_cb
);
1230 dev_err(&hif_dev
->udev
->dev
,
1231 "ath9k_htc: Async request for firmware %s failed\n",
1236 dev_info(&hif_dev
->udev
->dev
, "ath9k_htc: Firmware %s requested\n",
1242 usb_set_intfdata(interface
, NULL
);
1249 static void ath9k_hif_usb_reboot(struct usb_device
*udev
)
1251 u32 reboot_cmd
= 0xffffffff;
1255 buf
= kmemdup(&reboot_cmd
, 4, GFP_KERNEL
);
1259 ret
= usb_bulk_msg(udev
, usb_sndbulkpipe(udev
, USB_REG_OUT_PIPE
),
1262 dev_err(&udev
->dev
, "ath9k_htc: USB reboot failed\n");
1267 static void ath9k_hif_usb_disconnect(struct usb_interface
*interface
)
1269 struct usb_device
*udev
= interface_to_usbdev(interface
);
1270 struct hif_device_usb
*hif_dev
= usb_get_intfdata(interface
);
1271 bool unplugged
= (udev
->state
== USB_STATE_NOTATTACHED
) ? true : false;
1276 wait_for_completion(&hif_dev
->fw_done
);
1278 if (hif_dev
->firmware
) {
1279 ath9k_htc_hw_deinit(hif_dev
->htc_handle
, unplugged
);
1280 ath9k_htc_hw_free(hif_dev
->htc_handle
);
1281 ath9k_hif_usb_dev_deinit(hif_dev
);
1282 release_firmware(hif_dev
->firmware
);
1285 usb_set_intfdata(interface
, NULL
);
1287 if (!unplugged
&& (hif_dev
->flags
& HIF_USB_START
))
1288 ath9k_hif_usb_reboot(udev
);
1291 dev_info(&udev
->dev
, "ath9k_htc: USB layer deinitialized\n");
1296 static int ath9k_hif_usb_suspend(struct usb_interface
*interface
,
1297 pm_message_t message
)
1299 struct hif_device_usb
*hif_dev
= usb_get_intfdata(interface
);
1302 * The device has to be set to FULLSLEEP mode in case no
1305 if (!(hif_dev
->flags
& HIF_USB_START
))
1306 ath9k_htc_suspend(hif_dev
->htc_handle
);
1308 ath9k_hif_usb_dealloc_urbs(hif_dev
);
1313 static int ath9k_hif_usb_resume(struct usb_interface
*interface
)
1315 struct hif_device_usb
*hif_dev
= usb_get_intfdata(interface
);
1316 struct htc_target
*htc_handle
= hif_dev
->htc_handle
;
1319 ret
= ath9k_hif_usb_alloc_urbs(hif_dev
);
1323 if (hif_dev
->firmware
) {
1324 ret
= ath9k_hif_usb_download_fw(hif_dev
);
1328 ath9k_hif_usb_dealloc_urbs(hif_dev
);
1334 ret
= ath9k_htc_resume(htc_handle
);
1342 ath9k_hif_usb_dealloc_urbs(hif_dev
);
1348 static struct usb_driver ath9k_hif_usb_driver
= {
1349 .name
= KBUILD_MODNAME
,
1350 .probe
= ath9k_hif_usb_probe
,
1351 .disconnect
= ath9k_hif_usb_disconnect
,
1353 .suspend
= ath9k_hif_usb_suspend
,
1354 .resume
= ath9k_hif_usb_resume
,
1355 .reset_resume
= ath9k_hif_usb_resume
,
1357 .id_table
= ath9k_hif_usb_ids
,
1361 int ath9k_hif_usb_init(void)
1363 return usb_register(&ath9k_hif_usb_driver
);
1366 void ath9k_hif_usb_exit(void)
1368 usb_deregister(&ath9k_hif_usb_driver
);