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 MODULE_FIRMWARE(HTC_7010_MODULE_FW);
21 MODULE_FIRMWARE(HTC_9271_MODULE_FW);
23 static const struct usb_device_id ath9k_hif_usb_ids[] = {
24 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
25 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
26 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
27 { USB_DEVICE(0x07b8, 0x9271) }, /* Altai WA1011N-GU */
28 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
29 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
30 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
31 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
32 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
33 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
34 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
35 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
36 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
37 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
38 { USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */
39 { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
40 { USB_DEVICE(0x0471, 0x209e) }, /* Philips (or NXP) PTA01 */
41 { USB_DEVICE(0x1eda, 0x2315) }, /* AirTies */
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 */
56 { USB_DEVICE(0x0411, 0x0197),
57 .driver_info = AR9280_USB }, /* Buffalo WLI-UV-AG300P */
58 { USB_DEVICE(0x04da, 0x3904),
59 .driver_info = AR9280_USB },
60 { USB_DEVICE(0x0930, 0x0a08),
61 .driver_info = AR9280_USB }, /* Toshiba WLM-20U2 and GN-1080 */
63 { USB_DEVICE(0x0cf3, 0x20ff),
64 .driver_info = STORAGE_DEVICE },
69 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
71 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
73 static void hif_usb_regout_cb(struct urb *urb)
75 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
77 switch (urb->status) {
90 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
101 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
108 urb = usb_alloc_urb(0, GFP_KERNEL);
112 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
119 cmd->hif_dev = hif_dev;
121 usb_fill_int_urb(urb, hif_dev->udev,
122 usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
124 hif_usb_regout_cb, cmd, 1);
126 usb_anchor_urb(urb, &hif_dev->regout_submitted);
127 ret = usb_submit_urb(urb, GFP_KERNEL);
129 usb_unanchor_urb(urb);
137 static void hif_usb_mgmt_cb(struct urb *urb)
139 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
140 struct hif_device_usb *hif_dev;
144 if (!cmd || !cmd->skb || !cmd->hif_dev)
147 hif_dev = cmd->hif_dev;
149 switch (urb->status) {
159 * If the URBs are being flushed, no need to complete
162 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
163 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
164 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
165 dev_kfree_skb_any(cmd->skb);
169 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
177 skb_pull(cmd->skb, 4);
178 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
183 static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
191 urb = usb_alloc_urb(0, GFP_ATOMIC);
195 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
202 cmd->hif_dev = hif_dev;
204 hdr = skb_push(skb, 4);
205 *hdr++ = cpu_to_le16(skb->len - 4);
206 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
208 usb_fill_bulk_urb(urb, hif_dev->udev,
209 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
211 hif_usb_mgmt_cb, cmd);
213 usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
214 ret = usb_submit_urb(urb, GFP_ATOMIC);
216 usb_unanchor_urb(urb);
224 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
225 struct sk_buff_head *list)
229 while ((skb = __skb_dequeue(list)) != NULL) {
230 dev_kfree_skb_any(skb);
234 static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
235 struct sk_buff_head *queue,
240 while ((skb = __skb_dequeue(queue)) != NULL) {
241 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
244 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
247 TX_STAT_INC(hif_dev, skb_success);
248 TX_STAT_ADD(hif_dev, skb_success_bytes, ln);
251 TX_STAT_INC(hif_dev, skb_failed);
255 static void hif_usb_tx_cb(struct urb *urb)
257 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
258 struct hif_device_usb *hif_dev;
261 if (!tx_buf || !tx_buf->hif_dev)
264 hif_dev = tx_buf->hif_dev;
266 switch (urb->status) {
276 * If the URBs are being flushed, no need to add this
277 * URB to the free list.
279 spin_lock(&hif_dev->tx.tx_lock);
280 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
281 spin_unlock(&hif_dev->tx.tx_lock);
282 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
285 spin_unlock(&hif_dev->tx.tx_lock);
293 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
295 /* Re-initialize the SKB queue */
296 tx_buf->len = tx_buf->offset = 0;
297 __skb_queue_head_init(&tx_buf->skb_queue);
299 /* Add this TX buffer to the free list */
300 spin_lock(&hif_dev->tx.tx_lock);
301 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
302 hif_dev->tx.tx_buf_cnt++;
303 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
304 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
305 TX_STAT_INC(hif_dev, buf_completed);
306 spin_unlock(&hif_dev->tx.tx_lock);
309 /* TX lock has to be taken */
310 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
312 struct tx_buf *tx_buf = NULL;
313 struct sk_buff *nskb = NULL;
319 if (hif_dev->tx.tx_skb_cnt == 0)
322 /* Check if a free TX buffer is available */
323 if (list_empty(&hif_dev->tx.tx_buf))
326 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
327 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
328 hif_dev->tx.tx_buf_cnt--;
330 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
332 for (i = 0; i < tx_skb_cnt; i++) {
333 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
335 /* Should never be NULL */
338 hif_dev->tx.tx_skb_cnt--;
341 buf += tx_buf->offset;
343 *hdr++ = cpu_to_le16(nskb->len);
344 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
346 memcpy(buf, nskb->data, nskb->len);
347 tx_buf->len = nskb->len + 4;
349 if (i < (tx_skb_cnt - 1))
350 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
352 if (i == (tx_skb_cnt - 1))
353 tx_buf->len += tx_buf->offset;
355 __skb_queue_tail(&tx_buf->skb_queue, nskb);
356 TX_STAT_INC(hif_dev, skb_queued);
359 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
360 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
361 tx_buf->buf, tx_buf->len,
362 hif_usb_tx_cb, tx_buf);
364 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
366 tx_buf->len = tx_buf->offset = 0;
367 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
368 __skb_queue_head_init(&tx_buf->skb_queue);
369 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
370 hif_dev->tx.tx_buf_cnt++;
372 TX_STAT_INC(hif_dev, buf_queued);
378 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
380 struct ath9k_htc_tx_ctl *tx_ctl;
384 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
386 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
387 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
391 /* Check if the max queue count has been reached */
392 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
393 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
397 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
399 tx_ctl = HTC_SKB_CB(skb);
401 /* Mgmt/Beacon frames don't use the TX buffer pool */
402 if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
403 (tx_ctl->type == ATH9K_HTC_BEACON)) {
404 ret = hif_usb_send_mgmt(hif_dev, skb);
407 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
409 if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
410 (tx_ctl->type == ATH9K_HTC_AMPDU)) {
411 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
412 hif_dev->tx.tx_skb_cnt++;
415 /* Check if AMPDUs have to be sent immediately */
416 if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
417 (hif_dev->tx.tx_skb_cnt < 2)) {
418 __hif_usb_tx(hif_dev);
421 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
426 static void hif_usb_start(void *hif_handle)
428 struct hif_device_usb *hif_dev = hif_handle;
431 hif_dev->flags |= HIF_USB_START;
433 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
434 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
435 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
438 static void hif_usb_stop(void *hif_handle)
440 struct hif_device_usb *hif_dev = hif_handle;
441 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
444 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
445 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
446 hif_dev->tx.tx_skb_cnt = 0;
447 hif_dev->tx.flags |= HIF_USB_TX_STOP;
448 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
450 /* The pending URBs have to be canceled. */
451 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
452 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
453 &hif_dev->tx.tx_pending, list) {
454 usb_get_urb(tx_buf->urb);
455 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
456 usb_kill_urb(tx_buf->urb);
457 list_del(&tx_buf->list);
458 usb_free_urb(tx_buf->urb);
461 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
463 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
465 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
468 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
470 struct hif_device_usb *hif_dev = hif_handle;
474 case USB_WLAN_TX_PIPE:
475 ret = hif_usb_send_tx(hif_dev, skb);
477 case USB_REG_OUT_PIPE:
478 ret = hif_usb_send_regout(hif_dev, skb);
481 dev_err(&hif_dev->udev->dev,
482 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
490 static inline bool check_index(struct sk_buff *skb, u8 idx)
492 struct ath9k_htc_tx_ctl *tx_ctl;
494 tx_ctl = HTC_SKB_CB(skb);
496 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
497 (tx_ctl->sta_idx == idx))
503 static void hif_usb_sta_drain(void *hif_handle, u8 idx)
505 struct hif_device_usb *hif_dev = hif_handle;
506 struct sk_buff *skb, *tmp;
509 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
511 skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
512 if (check_index(skb, idx)) {
513 __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
514 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
516 hif_dev->tx.tx_skb_cnt--;
517 TX_STAT_INC(hif_dev, skb_failed);
521 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
524 static struct ath9k_htc_hif hif_usb = {
525 .transport = ATH9K_HIF_USB,
526 .name = "ath9k_hif_usb",
528 .control_ul_pipe = USB_REG_OUT_PIPE,
529 .control_dl_pipe = USB_REG_IN_PIPE,
531 .start = hif_usb_start,
532 .stop = hif_usb_stop,
533 .sta_drain = hif_usb_sta_drain,
534 .send = hif_usb_send,
537 /* Need to free remain_skb allocated in ath9k_hif_usb_rx_stream
538 * in case ath9k_hif_usb_rx_stream wasn't called next time to
539 * process the buffer and subsequently free it.
541 static void ath9k_hif_usb_free_rx_remain_skb(struct hif_device_usb *hif_dev)
545 spin_lock_irqsave(&hif_dev->rx_lock, flags);
546 if (hif_dev->remain_skb) {
547 dev_kfree_skb_any(hif_dev->remain_skb);
548 hif_dev->remain_skb = NULL;
549 hif_dev->rx_remain_len = 0;
550 RX_STAT_INC(hif_dev, skb_dropped);
552 spin_unlock_irqrestore(&hif_dev->rx_lock, flags);
555 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
558 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
559 int index = 0, i, len = skb->len;
560 int rx_remain_len, rx_pkt_len;
564 spin_lock(&hif_dev->rx_lock);
566 rx_remain_len = hif_dev->rx_remain_len;
567 rx_pkt_len = hif_dev->rx_transfer_len;
569 if (rx_remain_len != 0) {
570 struct sk_buff *remain_skb = hif_dev->remain_skb;
573 ptr = (u8 *) remain_skb->data;
575 index = rx_remain_len;
576 rx_remain_len -= hif_dev->rx_pad_len;
579 memcpy(ptr, skb->data, rx_remain_len);
581 rx_pkt_len += rx_remain_len;
582 skb_put(remain_skb, rx_pkt_len);
584 skb_pool[pool_index++] = remain_skb;
585 hif_dev->remain_skb = NULL;
586 hif_dev->rx_remain_len = 0;
588 index = rx_remain_len;
592 spin_unlock(&hif_dev->rx_lock);
594 while (index < len) {
600 ptr = (u8 *) skb->data;
602 pkt_len = get_unaligned_le16(ptr + index);
603 pkt_tag = get_unaligned_le16(ptr + index + 2);
605 /* It is supposed that if we have an invalid pkt_tag or
606 * pkt_len then the whole input SKB is considered invalid
607 * and dropped; the associated packets already in skb_pool
610 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
611 RX_STAT_INC(hif_dev, skb_dropped);
615 if (pkt_len > 2 * MAX_RX_BUF_SIZE) {
616 dev_err(&hif_dev->udev->dev,
617 "ath9k_htc: invalid pkt_len (%x)\n", pkt_len);
618 RX_STAT_INC(hif_dev, skb_dropped);
622 pad_len = 4 - (pkt_len & 0x3);
627 index = index + 4 + pkt_len + pad_len;
629 if (index > MAX_RX_BUF_SIZE) {
630 spin_lock(&hif_dev->rx_lock);
631 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
633 dev_err(&hif_dev->udev->dev,
634 "ath9k_htc: RX memory allocation error\n");
635 spin_unlock(&hif_dev->rx_lock);
639 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
640 hif_dev->rx_transfer_len =
641 MAX_RX_BUF_SIZE - chk_idx - 4;
642 hif_dev->rx_pad_len = pad_len;
644 skb_reserve(nskb, 32);
645 RX_STAT_INC(hif_dev, skb_allocated);
647 memcpy(nskb->data, &(skb->data[chk_idx+4]),
648 hif_dev->rx_transfer_len);
650 /* Record the buffer pointer */
651 hif_dev->remain_skb = nskb;
652 spin_unlock(&hif_dev->rx_lock);
654 if (pool_index == MAX_PKT_NUM_IN_TRANSFER) {
655 dev_err(&hif_dev->udev->dev,
656 "ath9k_htc: over RX MAX_PKT_NUM\n");
659 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
661 dev_err(&hif_dev->udev->dev,
662 "ath9k_htc: RX memory allocation error\n");
665 skb_reserve(nskb, 32);
666 RX_STAT_INC(hif_dev, skb_allocated);
668 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
669 skb_put(nskb, pkt_len);
670 skb_pool[pool_index++] = nskb;
675 for (i = 0; i < pool_index; i++) {
676 RX_STAT_ADD(hif_dev, skb_completed_bytes, skb_pool[i]->len);
677 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
678 skb_pool[i]->len, USB_WLAN_RX_PIPE);
679 RX_STAT_INC(hif_dev, skb_completed);
683 for (i = 0; i < pool_index; i++) {
684 dev_kfree_skb_any(skb_pool[i]);
685 RX_STAT_INC(hif_dev, skb_dropped);
690 static void ath9k_hif_usb_rx_cb(struct urb *urb)
692 struct rx_buf *rx_buf = (struct rx_buf *)urb->context;
693 struct hif_device_usb *hif_dev = rx_buf->hif_dev;
694 struct sk_buff *skb = rx_buf->skb;
703 switch (urb->status) {
715 if (likely(urb->actual_length != 0)) {
716 skb_put(skb, urb->actual_length);
717 ath9k_hif_usb_rx_stream(hif_dev, skb);
721 skb_reset_tail_pointer(skb);
724 usb_anchor_urb(urb, &hif_dev->rx_submitted);
725 ret = usb_submit_urb(urb, GFP_ATOMIC);
727 usb_unanchor_urb(urb);
737 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
739 struct rx_buf *rx_buf = (struct rx_buf *)urb->context;
740 struct hif_device_usb *hif_dev = rx_buf->hif_dev;
741 struct sk_buff *skb = rx_buf->skb;
750 switch (urb->status) {
759 skb_reset_tail_pointer(skb);
765 if (likely(urb->actual_length != 0)) {
766 skb_put(skb, urb->actual_length);
769 * Process the command first.
770 * skb is either freed here or passed to be
771 * managed to another callback function.
773 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
774 skb->len, USB_REG_IN_PIPE);
776 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
778 dev_err(&hif_dev->udev->dev,
779 "ath9k_htc: REG_IN memory allocation failure\n");
785 usb_fill_int_urb(urb, hif_dev->udev,
786 usb_rcvintpipe(hif_dev->udev,
788 skb->data, MAX_REG_IN_BUF_SIZE,
789 ath9k_hif_usb_reg_in_cb, rx_buf, 1);
793 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
794 ret = usb_submit_urb(urb, GFP_ATOMIC);
796 usb_unanchor_urb(urb);
808 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
810 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
813 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
814 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
815 &hif_dev->tx.tx_buf, list) {
816 list_del(&tx_buf->list);
817 usb_free_urb(tx_buf->urb);
821 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
823 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
824 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
825 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
827 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
828 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
829 &hif_dev->tx.tx_pending, list) {
830 usb_get_urb(tx_buf->urb);
831 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
832 usb_kill_urb(tx_buf->urb);
833 list_del(&tx_buf->list);
834 usb_free_urb(tx_buf->urb);
837 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
839 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
841 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
844 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
846 struct tx_buf *tx_buf;
849 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
850 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
851 spin_lock_init(&hif_dev->tx.tx_lock);
852 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
853 init_usb_anchor(&hif_dev->mgmt_submitted);
855 for (i = 0; i < MAX_TX_URB_NUM; i++) {
856 tx_buf = kzalloc(sizeof(*tx_buf), GFP_KERNEL);
860 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
864 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
868 tx_buf->hif_dev = hif_dev;
869 __skb_queue_head_init(&tx_buf->skb_queue);
871 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
874 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
882 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
886 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
888 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
889 ath9k_hif_usb_free_rx_remain_skb(hif_dev);
892 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
894 struct rx_buf *rx_buf = NULL;
895 struct sk_buff *skb = NULL;
896 struct urb *urb = NULL;
899 init_usb_anchor(&hif_dev->rx_submitted);
900 spin_lock_init(&hif_dev->rx_lock);
902 for (i = 0; i < MAX_RX_URB_NUM; i++) {
904 rx_buf = kzalloc(sizeof(*rx_buf), GFP_KERNEL);
911 urb = usb_alloc_urb(0, GFP_KERNEL);
917 /* Allocate buffer */
918 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
924 rx_buf->hif_dev = hif_dev;
927 usb_fill_bulk_urb(urb, hif_dev->udev,
928 usb_rcvbulkpipe(hif_dev->udev,
930 skb->data, MAX_RX_BUF_SIZE,
931 ath9k_hif_usb_rx_cb, rx_buf);
934 usb_anchor_urb(urb, &hif_dev->rx_submitted);
937 ret = usb_submit_urb(urb, GFP_KERNEL);
939 usb_unanchor_urb(urb);
944 * Drop reference count.
945 * This ensures that the URB is freed when killing them.
959 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
963 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
965 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
968 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
970 struct rx_buf *rx_buf = NULL;
971 struct sk_buff *skb = NULL;
972 struct urb *urb = NULL;
975 init_usb_anchor(&hif_dev->reg_in_submitted);
977 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
979 rx_buf = kzalloc(sizeof(*rx_buf), GFP_KERNEL);
986 urb = usb_alloc_urb(0, GFP_KERNEL);
992 /* Allocate buffer */
993 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
999 rx_buf->hif_dev = hif_dev;
1002 usb_fill_int_urb(urb, hif_dev->udev,
1003 usb_rcvintpipe(hif_dev->udev,
1005 skb->data, MAX_REG_IN_BUF_SIZE,
1006 ath9k_hif_usb_reg_in_cb, rx_buf, 1);
1009 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
1012 ret = usb_submit_urb(urb, GFP_KERNEL);
1014 usb_unanchor_urb(urb);
1019 * Drop reference count.
1020 * This ensures that the URB is freed when killing them.
1034 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
1038 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
1040 /* Register Write */
1041 init_usb_anchor(&hif_dev->regout_submitted);
1044 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
1048 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
1052 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
1057 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
1059 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
1064 void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
1066 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
1067 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
1068 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
1069 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
1072 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
1075 const void *data = hif_dev->fw_data;
1076 size_t len = hif_dev->fw_size;
1077 u32 addr = AR9271_FIRMWARE;
1078 u8 *buf = kzalloc(4096, GFP_KERNEL);
1085 transfer = min_t(size_t, len, 4096);
1086 memcpy(buf, data, transfer);
1088 err = usb_control_msg(hif_dev->udev,
1089 usb_sndctrlpipe(hif_dev->udev, 0),
1090 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
1091 addr >> 8, 0, buf, transfer,
1104 if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1105 firm_offset = AR7010_FIRMWARE_TEXT;
1107 firm_offset = AR9271_FIRMWARE_TEXT;
1110 * Issue FW download complete command to firmware.
1112 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1113 FIRMWARE_DOWNLOAD_COMP,
1115 firm_offset >> 8, 0, NULL, 0, USB_MSG_TIMEOUT);
1119 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1120 hif_dev->fw_name, (unsigned long) hif_dev->fw_size);
1125 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
1129 ret = ath9k_hif_usb_download_fw(hif_dev);
1131 dev_err(&hif_dev->udev->dev,
1132 "ath9k_htc: Firmware - %s download failed\n",
1138 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1140 dev_err(&hif_dev->udev->dev,
1141 "ath9k_htc: Unable to allocate URBs\n");
1148 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1150 ath9k_hif_usb_dealloc_urbs(hif_dev);
1154 * If initialization fails or the FW cannot be retrieved,
1155 * detach the device.
1157 static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev)
1159 struct device *dev = &hif_dev->udev->dev;
1160 struct device *parent = dev->parent;
1162 complete_all(&hif_dev->fw_done);
1165 device_lock(parent);
1167 device_release_driver(dev);
1170 device_unlock(parent);
1173 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context);
1175 /* taken from iwlwifi */
1176 static int ath9k_hif_request_firmware(struct hif_device_usb *hif_dev,
1179 char index[8], *chip;
1183 if (htc_use_dev_fw) {
1184 hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX + 1;
1185 sprintf(index, "%s", "dev");
1187 hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX;
1188 sprintf(index, "%d", hif_dev->fw_minor_index);
1191 hif_dev->fw_minor_index--;
1192 sprintf(index, "%d", hif_dev->fw_minor_index);
1195 /* test for FW 1.3 */
1196 if (MAJOR_VERSION_REQ == 1 && hif_dev->fw_minor_index == 3) {
1197 const char *filename;
1199 if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1200 filename = FIRMWARE_AR7010_1_1;
1202 filename = FIRMWARE_AR9271;
1204 /* expected fw locations:
1205 * - htc_9271.fw (stable version 1.3, depricated)
1207 snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1210 } else if (hif_dev->fw_minor_index < FIRMWARE_MINOR_IDX_MIN) {
1211 dev_err(&hif_dev->udev->dev, "no suitable firmware found!\n");
1215 if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1220 /* expected fw locations:
1221 * - ath9k_htc/htc_9271-1.dev.0.fw (development version)
1222 * - ath9k_htc/htc_9271-1.4.0.fw (stable version)
1224 snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1225 "%s/htc_%s-%d.%s.0.fw", HTC_FW_PATH,
1226 chip, MAJOR_VERSION_REQ, index);
1229 ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name,
1230 &hif_dev->udev->dev, GFP_KERNEL,
1231 hif_dev, ath9k_hif_usb_firmware_cb);
1233 dev_err(&hif_dev->udev->dev,
1234 "ath9k_htc: Async request for firmware %s failed\n",
1239 dev_info(&hif_dev->udev->dev, "ath9k_htc: Firmware %s requested\n",
1245 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context)
1247 struct hif_device_usb *hif_dev = context;
1251 ret = ath9k_hif_request_firmware(hif_dev, false);
1255 dev_err(&hif_dev->udev->dev,
1256 "ath9k_htc: Failed to get firmware %s\n",
1261 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1262 &hif_dev->udev->dev);
1263 if (hif_dev->htc_handle == NULL)
1266 hif_dev->fw_data = fw->data;
1267 hif_dev->fw_size = fw->size;
1269 /* Proceed with initialization */
1271 ret = ath9k_hif_usb_dev_init(hif_dev);
1275 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1276 &hif_dev->interface->dev,
1277 hif_dev->usb_device_id->idProduct,
1278 hif_dev->udev->product,
1279 hif_dev->usb_device_id->driver_info);
1282 goto err_htc_hw_init;
1285 release_firmware(fw);
1286 hif_dev->flags |= HIF_USB_READY;
1287 complete_all(&hif_dev->fw_done);
1292 ath9k_hif_usb_dev_deinit(hif_dev);
1294 ath9k_htc_hw_free(hif_dev->htc_handle);
1296 release_firmware(fw);
1298 ath9k_hif_usb_firmware_fail(hif_dev);
1302 * An exact copy of the function from zd1211rw.
1304 static int send_eject_command(struct usb_interface *interface)
1306 struct usb_device *udev = interface_to_usbdev(interface);
1307 struct usb_host_interface *iface_desc = interface->cur_altsetting;
1308 struct usb_endpoint_descriptor *endpoint;
1313 if (iface_desc->desc.bNumEndpoints < 2)
1316 /* Find bulk out endpoint */
1317 for (r = 1; r >= 0; r--) {
1318 endpoint = &iface_desc->endpoint[r].desc;
1319 if (usb_endpoint_dir_out(endpoint) &&
1320 usb_endpoint_xfer_bulk(endpoint)) {
1321 bulk_out_ep = endpoint->bEndpointAddress;
1327 "ath9k_htc: Could not find bulk out endpoint\n");
1331 cmd = kzalloc(31, GFP_KERNEL);
1335 /* USB bulk command block */
1336 cmd[0] = 0x55; /* bulk command signature */
1337 cmd[1] = 0x53; /* bulk command signature */
1338 cmd[2] = 0x42; /* bulk command signature */
1339 cmd[3] = 0x43; /* bulk command signature */
1340 cmd[14] = 6; /* command length */
1342 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1343 cmd[19] = 0x2; /* eject disc */
1345 dev_info(&udev->dev, "Ejecting storage device...\n");
1346 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1347 cmd, 31, NULL, 2 * USB_MSG_TIMEOUT);
1352 /* At this point, the device disconnects and reconnects with the real
1355 usb_set_intfdata(interface, NULL);
1359 static int ath9k_hif_usb_probe(struct usb_interface *interface,
1360 const struct usb_device_id *id)
1362 struct usb_endpoint_descriptor *bulk_in, *bulk_out, *int_in, *int_out;
1363 struct usb_device *udev = interface_to_usbdev(interface);
1364 struct usb_host_interface *alt;
1365 struct hif_device_usb *hif_dev;
1368 /* Verify the expected endpoints are present */
1369 alt = interface->cur_altsetting;
1370 if (usb_find_common_endpoints(alt, &bulk_in, &bulk_out, &int_in, &int_out) < 0 ||
1371 usb_endpoint_num(bulk_in) != USB_WLAN_RX_PIPE ||
1372 usb_endpoint_num(bulk_out) != USB_WLAN_TX_PIPE ||
1373 usb_endpoint_num(int_in) != USB_REG_IN_PIPE ||
1374 usb_endpoint_num(int_out) != USB_REG_OUT_PIPE) {
1376 "ath9k_htc: Device endpoint numbers are not the expected ones\n");
1380 if (id->driver_info == STORAGE_DEVICE)
1381 return send_eject_command(interface);
1383 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1391 hif_dev->udev = udev;
1392 hif_dev->interface = interface;
1393 hif_dev->usb_device_id = id;
1395 udev->reset_resume = 1;
1397 usb_set_intfdata(interface, hif_dev);
1399 init_completion(&hif_dev->fw_done);
1401 ret = ath9k_hif_request_firmware(hif_dev, true);
1408 usb_set_intfdata(interface, NULL);
1415 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1417 u32 reboot_cmd = 0xffffffff;
1421 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1425 ret = usb_interrupt_msg(udev, usb_sndintpipe(udev, USB_REG_OUT_PIPE),
1426 buf, 4, NULL, USB_MSG_TIMEOUT);
1428 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1433 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1435 struct usb_device *udev = interface_to_usbdev(interface);
1436 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1437 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1442 wait_for_completion(&hif_dev->fw_done);
1444 if (hif_dev->flags & HIF_USB_READY) {
1445 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1446 ath9k_hif_usb_dev_deinit(hif_dev);
1447 ath9k_destoy_wmi(hif_dev->htc_handle->drv_priv);
1448 ath9k_htc_hw_free(hif_dev->htc_handle);
1451 usb_set_intfdata(interface, NULL);
1453 /* If firmware was loaded we should drop it
1454 * go back to first stage bootloader. */
1455 if (!unplugged && (hif_dev->flags & HIF_USB_READY))
1456 ath9k_hif_usb_reboot(udev);
1459 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1464 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1465 pm_message_t message)
1467 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1470 * The device has to be set to FULLSLEEP mode in case no
1473 if (!(hif_dev->flags & HIF_USB_START))
1474 ath9k_htc_suspend(hif_dev->htc_handle);
1476 wait_for_completion(&hif_dev->fw_done);
1478 if (hif_dev->flags & HIF_USB_READY)
1479 ath9k_hif_usb_dealloc_urbs(hif_dev);
1484 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1486 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1487 struct htc_target *htc_handle = hif_dev->htc_handle;
1489 const struct firmware *fw;
1491 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1495 if (hif_dev->flags & HIF_USB_READY) {
1496 /* request cached firmware during suspend/resume cycle */
1497 ret = request_firmware(&fw, hif_dev->fw_name,
1498 &hif_dev->udev->dev);
1502 hif_dev->fw_data = fw->data;
1503 hif_dev->fw_size = fw->size;
1504 ret = ath9k_hif_usb_download_fw(hif_dev);
1505 release_firmware(fw);
1509 ath9k_hif_usb_dealloc_urbs(hif_dev);
1515 ret = ath9k_htc_resume(htc_handle);
1523 ath9k_hif_usb_dealloc_urbs(hif_dev);
1529 static struct usb_driver ath9k_hif_usb_driver = {
1530 .name = KBUILD_MODNAME,
1531 .probe = ath9k_hif_usb_probe,
1532 .disconnect = ath9k_hif_usb_disconnect,
1534 .suspend = ath9k_hif_usb_suspend,
1535 .resume = ath9k_hif_usb_resume,
1536 .reset_resume = ath9k_hif_usb_resume,
1538 .id_table = ath9k_hif_usb_ids,
1540 .disable_hub_initiated_lpm = 1,
1543 int ath9k_hif_usb_init(void)
1545 return usb_register(&ath9k_hif_usb_driver);
1548 void ath9k_hif_usb_exit(void)
1550 usb_deregister(&ath9k_hif_usb_driver);