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 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(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
28 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
29 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
30 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
31 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
32 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
33 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
34 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
35 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
36 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
37 { USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */
38 { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
39 { USB_DEVICE(0x0471, 0x209e) }, /* Philips (or NXP) PTA01 */
41 { USB_DEVICE(0x0cf3, 0x7015),
42 .driver_info = AR9287_USB }, /* Atheros */
43 { USB_DEVICE(0x1668, 0x1200),
44 .driver_info = AR9287_USB }, /* Verizon */
46 { USB_DEVICE(0x0cf3, 0x7010),
47 .driver_info = AR9280_USB }, /* Atheros */
48 { USB_DEVICE(0x0846, 0x9018),
49 .driver_info = AR9280_USB }, /* Netgear WNDA3200 */
50 { USB_DEVICE(0x083A, 0xA704),
51 .driver_info = AR9280_USB }, /* SMC Networks */
52 { USB_DEVICE(0x0411, 0x017f),
53 .driver_info = AR9280_USB }, /* Sony UWA-BR100 */
54 { USB_DEVICE(0x0411, 0x0197),
55 .driver_info = AR9280_USB }, /* Buffalo WLI-UV-AG300P */
56 { USB_DEVICE(0x04da, 0x3904),
57 .driver_info = AR9280_USB },
59 { USB_DEVICE(0x0cf3, 0x20ff),
60 .driver_info = STORAGE_DEVICE },
65 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
67 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
69 static void hif_usb_regout_cb(struct urb *urb)
71 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
73 switch (urb->status) {
86 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
97 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
104 urb = usb_alloc_urb(0, GFP_KERNEL);
108 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
115 cmd->hif_dev = hif_dev;
117 usb_fill_int_urb(urb, hif_dev->udev,
118 usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
120 hif_usb_regout_cb, cmd, 1);
122 usb_anchor_urb(urb, &hif_dev->regout_submitted);
123 ret = usb_submit_urb(urb, GFP_KERNEL);
125 usb_unanchor_urb(urb);
133 static void hif_usb_mgmt_cb(struct urb *urb)
135 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
136 struct hif_device_usb *hif_dev;
139 if (!cmd || !cmd->skb || !cmd->hif_dev)
142 hif_dev = cmd->hif_dev;
144 switch (urb->status) {
154 * If the URBs are being flushed, no need to complete
157 spin_lock(&hif_dev->tx.tx_lock);
158 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
159 spin_unlock(&hif_dev->tx.tx_lock);
160 dev_kfree_skb_any(cmd->skb);
164 spin_unlock(&hif_dev->tx.tx_lock);
172 skb_pull(cmd->skb, 4);
173 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
178 static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
186 urb = usb_alloc_urb(0, GFP_ATOMIC);
190 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
197 cmd->hif_dev = hif_dev;
199 hdr = (__le16 *) skb_push(skb, 4);
200 *hdr++ = cpu_to_le16(skb->len - 4);
201 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
203 usb_fill_bulk_urb(urb, hif_dev->udev,
204 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
206 hif_usb_mgmt_cb, cmd);
208 usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
209 ret = usb_submit_urb(urb, GFP_ATOMIC);
211 usb_unanchor_urb(urb);
219 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
220 struct sk_buff_head *list)
224 while ((skb = __skb_dequeue(list)) != NULL) {
225 dev_kfree_skb_any(skb);
229 static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
230 struct sk_buff_head *queue,
235 while ((skb = __skb_dequeue(queue)) != NULL) {
236 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
239 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
242 TX_STAT_INC(skb_success);
243 TX_STAT_ADD(skb_success_bytes, ln);
246 TX_STAT_INC(skb_failed);
250 static void hif_usb_tx_cb(struct urb *urb)
252 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
253 struct hif_device_usb *hif_dev;
256 if (!tx_buf || !tx_buf->hif_dev)
259 hif_dev = tx_buf->hif_dev;
261 switch (urb->status) {
271 * If the URBs are being flushed, no need to add this
272 * URB to the free list.
274 spin_lock(&hif_dev->tx.tx_lock);
275 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
276 spin_unlock(&hif_dev->tx.tx_lock);
277 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
280 spin_unlock(&hif_dev->tx.tx_lock);
288 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
290 /* Re-initialize the SKB queue */
291 tx_buf->len = tx_buf->offset = 0;
292 __skb_queue_head_init(&tx_buf->skb_queue);
294 /* Add this TX buffer to the free list */
295 spin_lock(&hif_dev->tx.tx_lock);
296 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
297 hif_dev->tx.tx_buf_cnt++;
298 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
299 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
300 TX_STAT_INC(buf_completed);
301 spin_unlock(&hif_dev->tx.tx_lock);
304 /* TX lock has to be taken */
305 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
307 struct tx_buf *tx_buf = NULL;
308 struct sk_buff *nskb = NULL;
314 if (hif_dev->tx.tx_skb_cnt == 0)
317 /* Check if a free TX buffer is available */
318 if (list_empty(&hif_dev->tx.tx_buf))
321 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
322 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
323 hif_dev->tx.tx_buf_cnt--;
325 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
327 for (i = 0; i < tx_skb_cnt; i++) {
328 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
330 /* Should never be NULL */
333 hif_dev->tx.tx_skb_cnt--;
336 buf += tx_buf->offset;
338 *hdr++ = cpu_to_le16(nskb->len);
339 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
341 memcpy(buf, nskb->data, nskb->len);
342 tx_buf->len = nskb->len + 4;
344 if (i < (tx_skb_cnt - 1))
345 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
347 if (i == (tx_skb_cnt - 1))
348 tx_buf->len += tx_buf->offset;
350 __skb_queue_tail(&tx_buf->skb_queue, nskb);
351 TX_STAT_INC(skb_queued);
354 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
355 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
356 tx_buf->buf, tx_buf->len,
357 hif_usb_tx_cb, tx_buf);
359 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
361 tx_buf->len = tx_buf->offset = 0;
362 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
363 __skb_queue_head_init(&tx_buf->skb_queue);
364 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
365 hif_dev->tx.tx_buf_cnt++;
369 TX_STAT_INC(buf_queued);
374 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
376 struct ath9k_htc_tx_ctl *tx_ctl;
380 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
382 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
383 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
387 /* Check if the max queue count has been reached */
388 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
389 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
393 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
395 tx_ctl = HTC_SKB_CB(skb);
397 /* Mgmt/Beacon frames don't use the TX buffer pool */
398 if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
399 (tx_ctl->type == ATH9K_HTC_BEACON)) {
400 ret = hif_usb_send_mgmt(hif_dev, skb);
403 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
405 if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
406 (tx_ctl->type == ATH9K_HTC_AMPDU)) {
407 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
408 hif_dev->tx.tx_skb_cnt++;
411 /* Check if AMPDUs have to be sent immediately */
412 if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
413 (hif_dev->tx.tx_skb_cnt < 2)) {
414 __hif_usb_tx(hif_dev);
417 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
422 static void hif_usb_start(void *hif_handle)
424 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
427 hif_dev->flags |= HIF_USB_START;
429 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
430 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
431 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
434 static void hif_usb_stop(void *hif_handle)
436 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
437 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
440 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
441 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
442 hif_dev->tx.tx_skb_cnt = 0;
443 hif_dev->tx.flags |= HIF_USB_TX_STOP;
444 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
446 /* The pending URBs have to be canceled. */
447 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
448 &hif_dev->tx.tx_pending, list) {
449 usb_kill_urb(tx_buf->urb);
452 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
455 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
457 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
461 case USB_WLAN_TX_PIPE:
462 ret = hif_usb_send_tx(hif_dev, skb);
464 case USB_REG_OUT_PIPE:
465 ret = hif_usb_send_regout(hif_dev, skb);
468 dev_err(&hif_dev->udev->dev,
469 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
477 static inline bool check_index(struct sk_buff *skb, u8 idx)
479 struct ath9k_htc_tx_ctl *tx_ctl;
481 tx_ctl = HTC_SKB_CB(skb);
483 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
484 (tx_ctl->sta_idx == idx))
490 static void hif_usb_sta_drain(void *hif_handle, u8 idx)
492 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
493 struct sk_buff *skb, *tmp;
496 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
498 skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
499 if (check_index(skb, idx)) {
500 __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
501 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
503 hif_dev->tx.tx_skb_cnt--;
504 TX_STAT_INC(skb_failed);
508 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
511 static struct ath9k_htc_hif hif_usb = {
512 .transport = ATH9K_HIF_USB,
513 .name = "ath9k_hif_usb",
515 .control_ul_pipe = USB_REG_OUT_PIPE,
516 .control_dl_pipe = USB_REG_IN_PIPE,
518 .start = hif_usb_start,
519 .stop = hif_usb_stop,
520 .sta_drain = hif_usb_sta_drain,
521 .send = hif_usb_send,
524 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
527 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
528 int index = 0, i = 0, len = skb->len;
529 int rx_remain_len, rx_pkt_len;
533 spin_lock(&hif_dev->rx_lock);
535 rx_remain_len = hif_dev->rx_remain_len;
536 rx_pkt_len = hif_dev->rx_transfer_len;
538 if (rx_remain_len != 0) {
539 struct sk_buff *remain_skb = hif_dev->remain_skb;
542 ptr = (u8 *) remain_skb->data;
544 index = rx_remain_len;
545 rx_remain_len -= hif_dev->rx_pad_len;
548 memcpy(ptr, skb->data, rx_remain_len);
550 rx_pkt_len += rx_remain_len;
551 hif_dev->rx_remain_len = 0;
552 skb_put(remain_skb, rx_pkt_len);
554 skb_pool[pool_index++] = remain_skb;
557 index = rx_remain_len;
561 spin_unlock(&hif_dev->rx_lock);
563 while (index < len) {
569 ptr = (u8 *) skb->data;
571 pkt_len = get_unaligned_le16(ptr + index);
572 pkt_tag = get_unaligned_le16(ptr + index + 2);
574 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
575 RX_STAT_INC(skb_dropped);
579 pad_len = 4 - (pkt_len & 0x3);
584 index = index + 4 + pkt_len + pad_len;
586 if (index > MAX_RX_BUF_SIZE) {
587 spin_lock(&hif_dev->rx_lock);
588 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
589 hif_dev->rx_transfer_len =
590 MAX_RX_BUF_SIZE - chk_idx - 4;
591 hif_dev->rx_pad_len = pad_len;
593 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
595 dev_err(&hif_dev->udev->dev,
596 "ath9k_htc: RX memory allocation error\n");
597 spin_unlock(&hif_dev->rx_lock);
600 skb_reserve(nskb, 32);
601 RX_STAT_INC(skb_allocated);
603 memcpy(nskb->data, &(skb->data[chk_idx+4]),
604 hif_dev->rx_transfer_len);
606 /* Record the buffer pointer */
607 hif_dev->remain_skb = nskb;
608 spin_unlock(&hif_dev->rx_lock);
610 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
612 dev_err(&hif_dev->udev->dev,
613 "ath9k_htc: RX memory allocation error\n");
616 skb_reserve(nskb, 32);
617 RX_STAT_INC(skb_allocated);
619 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
620 skb_put(nskb, pkt_len);
621 skb_pool[pool_index++] = nskb;
626 for (i = 0; i < pool_index; i++) {
627 RX_STAT_ADD(skb_completed_bytes, skb_pool[i]->len);
628 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
629 skb_pool[i]->len, USB_WLAN_RX_PIPE);
630 RX_STAT_INC(skb_completed);
634 static void ath9k_hif_usb_rx_cb(struct urb *urb)
636 struct sk_buff *skb = (struct sk_buff *) urb->context;
637 struct hif_device_usb *hif_dev =
638 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
647 switch (urb->status) {
659 if (likely(urb->actual_length != 0)) {
660 skb_put(skb, urb->actual_length);
661 ath9k_hif_usb_rx_stream(hif_dev, skb);
665 skb_reset_tail_pointer(skb);
668 usb_anchor_urb(urb, &hif_dev->rx_submitted);
669 ret = usb_submit_urb(urb, GFP_ATOMIC);
671 usb_unanchor_urb(urb);
680 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
682 struct sk_buff *skb = (struct sk_buff *) urb->context;
683 struct sk_buff *nskb;
684 struct hif_device_usb *hif_dev =
685 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
694 switch (urb->status) {
703 skb_reset_tail_pointer(skb);
709 if (likely(urb->actual_length != 0)) {
710 skb_put(skb, urb->actual_length);
712 /* Process the command first */
713 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
714 skb->len, USB_REG_IN_PIPE);
717 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
719 dev_err(&hif_dev->udev->dev,
720 "ath9k_htc: REG_IN memory allocation failure\n");
725 usb_fill_int_urb(urb, hif_dev->udev,
726 usb_rcvintpipe(hif_dev->udev,
728 nskb->data, MAX_REG_IN_BUF_SIZE,
729 ath9k_hif_usb_reg_in_cb, nskb, 1);
733 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
734 ret = usb_submit_urb(urb, GFP_ATOMIC);
736 usb_unanchor_urb(urb);
746 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
748 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
751 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
752 &hif_dev->tx.tx_buf, list) {
753 usb_kill_urb(tx_buf->urb);
754 list_del(&tx_buf->list);
755 usb_free_urb(tx_buf->urb);
760 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
761 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
762 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
764 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
765 &hif_dev->tx.tx_pending, list) {
766 usb_kill_urb(tx_buf->urb);
767 list_del(&tx_buf->list);
768 usb_free_urb(tx_buf->urb);
773 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
776 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
778 struct tx_buf *tx_buf;
781 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
782 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
783 spin_lock_init(&hif_dev->tx.tx_lock);
784 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
785 init_usb_anchor(&hif_dev->mgmt_submitted);
787 for (i = 0; i < MAX_TX_URB_NUM; i++) {
788 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
792 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
796 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
800 tx_buf->hif_dev = hif_dev;
801 __skb_queue_head_init(&tx_buf->skb_queue);
803 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
806 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
814 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
818 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
820 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
823 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
825 struct urb *urb = NULL;
826 struct sk_buff *skb = NULL;
829 init_usb_anchor(&hif_dev->rx_submitted);
830 spin_lock_init(&hif_dev->rx_lock);
832 for (i = 0; i < MAX_RX_URB_NUM; i++) {
835 urb = usb_alloc_urb(0, GFP_KERNEL);
841 /* Allocate buffer */
842 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
848 usb_fill_bulk_urb(urb, hif_dev->udev,
849 usb_rcvbulkpipe(hif_dev->udev,
851 skb->data, MAX_RX_BUF_SIZE,
852 ath9k_hif_usb_rx_cb, skb);
855 usb_anchor_urb(urb, &hif_dev->rx_submitted);
858 ret = usb_submit_urb(urb, GFP_KERNEL);
860 usb_unanchor_urb(urb);
865 * Drop reference count.
866 * This ensures that the URB is freed when killing them.
878 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
882 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
884 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
887 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
889 struct urb *urb = NULL;
890 struct sk_buff *skb = NULL;
893 init_usb_anchor(&hif_dev->reg_in_submitted);
895 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
898 urb = usb_alloc_urb(0, GFP_KERNEL);
904 /* Allocate buffer */
905 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
911 usb_fill_int_urb(urb, hif_dev->udev,
912 usb_rcvintpipe(hif_dev->udev,
914 skb->data, MAX_REG_IN_BUF_SIZE,
915 ath9k_hif_usb_reg_in_cb, skb, 1);
918 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
921 ret = usb_submit_urb(urb, GFP_KERNEL);
923 usb_unanchor_urb(urb);
928 * Drop reference count.
929 * This ensures that the URB is freed when killing them.
941 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
945 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
948 init_usb_anchor(&hif_dev->regout_submitted);
951 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
955 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
959 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
964 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
966 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
971 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
973 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
974 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
975 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
976 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
979 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
982 const void *data = hif_dev->fw_data;
983 size_t len = hif_dev->fw_size;
984 u32 addr = AR9271_FIRMWARE;
985 u8 *buf = kzalloc(4096, GFP_KERNEL);
992 transfer = min_t(size_t, len, 4096);
993 memcpy(buf, data, transfer);
995 err = usb_control_msg(hif_dev->udev,
996 usb_sndctrlpipe(hif_dev->udev, 0),
997 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
998 addr >> 8, 0, buf, transfer, HZ);
1010 if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1011 firm_offset = AR7010_FIRMWARE_TEXT;
1013 firm_offset = AR9271_FIRMWARE_TEXT;
1016 * Issue FW download complete command to firmware.
1018 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1019 FIRMWARE_DOWNLOAD_COMP,
1021 firm_offset >> 8, 0, NULL, 0, HZ);
1025 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1026 hif_dev->fw_name, (unsigned long) hif_dev->fw_size);
1031 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
1035 ret = ath9k_hif_usb_download_fw(hif_dev);
1037 dev_err(&hif_dev->udev->dev,
1038 "ath9k_htc: Firmware - %s download failed\n",
1044 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1046 dev_err(&hif_dev->udev->dev,
1047 "ath9k_htc: Unable to allocate URBs\n");
1054 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1056 ath9k_hif_usb_dealloc_urbs(hif_dev);
1060 * If initialization fails or the FW cannot be retrieved,
1061 * detach the device.
1063 static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev)
1065 struct device *dev = &hif_dev->udev->dev;
1066 struct device *parent = dev->parent;
1068 complete_all(&hif_dev->fw_done);
1071 device_lock(parent);
1073 device_release_driver(dev);
1076 device_unlock(parent);
1079 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context);
1081 /* taken from iwlwifi */
1082 static int ath9k_hif_request_firmware(struct hif_device_usb *hif_dev,
1085 char index[8], *chip;
1089 if (htc_use_dev_fw) {
1090 hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX + 1;
1091 sprintf(index, "%s", "dev");
1093 hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX;
1094 sprintf(index, "%d", hif_dev->fw_minor_index);
1097 hif_dev->fw_minor_index--;
1098 sprintf(index, "%d", hif_dev->fw_minor_index);
1101 /* test for FW 1.3 */
1102 if (MAJOR_VERSION_REQ == 1 && hif_dev->fw_minor_index == 3) {
1103 const char *filename;
1105 if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1106 filename = FIRMWARE_AR7010_1_1;
1108 filename = FIRMWARE_AR9271;
1110 /* expected fw locations:
1111 * - htc_9271.fw (stable version 1.3, depricated)
1113 snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1116 } else if (hif_dev->fw_minor_index < FIRMWARE_MINOR_IDX_MIN) {
1117 dev_err(&hif_dev->udev->dev, "no suitable firmware found!\n");
1121 if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1126 /* expected fw locations:
1127 * - ath9k_htc/htc_9271-1.dev.0.fw (development version)
1128 * - ath9k_htc/htc_9271-1.4.0.fw (stable version)
1130 snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1131 "%s/htc_%s-%d.%s.0.fw", HTC_FW_PATH,
1132 chip, MAJOR_VERSION_REQ, index);
1135 ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name,
1136 &hif_dev->udev->dev, GFP_KERNEL,
1137 hif_dev, ath9k_hif_usb_firmware_cb);
1139 dev_err(&hif_dev->udev->dev,
1140 "ath9k_htc: Async request for firmware %s failed\n",
1145 dev_info(&hif_dev->udev->dev, "ath9k_htc: Firmware %s requested\n",
1151 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context)
1153 struct hif_device_usb *hif_dev = context;
1157 ret = ath9k_hif_request_firmware(hif_dev, false);
1161 dev_err(&hif_dev->udev->dev,
1162 "ath9k_htc: Failed to get firmware %s\n",
1167 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1168 &hif_dev->udev->dev);
1169 if (hif_dev->htc_handle == NULL)
1172 hif_dev->fw_data = fw->data;
1173 hif_dev->fw_size = fw->size;
1175 /* Proceed with initialization */
1177 ret = ath9k_hif_usb_dev_init(hif_dev);
1181 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1182 &hif_dev->interface->dev,
1183 hif_dev->usb_device_id->idProduct,
1184 hif_dev->udev->product,
1185 hif_dev->usb_device_id->driver_info);
1188 goto err_htc_hw_init;
1191 release_firmware(fw);
1192 hif_dev->flags |= HIF_USB_READY;
1193 complete_all(&hif_dev->fw_done);
1198 ath9k_hif_usb_dev_deinit(hif_dev);
1200 ath9k_htc_hw_free(hif_dev->htc_handle);
1202 release_firmware(fw);
1204 ath9k_hif_usb_firmware_fail(hif_dev);
1208 * An exact copy of the function from zd1211rw.
1210 static int send_eject_command(struct usb_interface *interface)
1212 struct usb_device *udev = interface_to_usbdev(interface);
1213 struct usb_host_interface *iface_desc = &interface->altsetting[0];
1214 struct usb_endpoint_descriptor *endpoint;
1219 /* Find bulk out endpoint */
1220 for (r = 1; r >= 0; r--) {
1221 endpoint = &iface_desc->endpoint[r].desc;
1222 if (usb_endpoint_dir_out(endpoint) &&
1223 usb_endpoint_xfer_bulk(endpoint)) {
1224 bulk_out_ep = endpoint->bEndpointAddress;
1230 "ath9k_htc: Could not find bulk out endpoint\n");
1234 cmd = kzalloc(31, GFP_KERNEL);
1238 /* USB bulk command block */
1239 cmd[0] = 0x55; /* bulk command signature */
1240 cmd[1] = 0x53; /* bulk command signature */
1241 cmd[2] = 0x42; /* bulk command signature */
1242 cmd[3] = 0x43; /* bulk command signature */
1243 cmd[14] = 6; /* command length */
1245 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1246 cmd[19] = 0x2; /* eject disc */
1248 dev_info(&udev->dev, "Ejecting storage device...\n");
1249 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1250 cmd, 31, NULL, 2000);
1255 /* At this point, the device disconnects and reconnects with the real
1258 usb_set_intfdata(interface, NULL);
1262 static int ath9k_hif_usb_probe(struct usb_interface *interface,
1263 const struct usb_device_id *id)
1265 struct usb_device *udev = interface_to_usbdev(interface);
1266 struct hif_device_usb *hif_dev;
1269 if (id->driver_info == STORAGE_DEVICE)
1270 return send_eject_command(interface);
1272 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1280 hif_dev->udev = udev;
1281 hif_dev->interface = interface;
1282 hif_dev->usb_device_id = id;
1284 udev->reset_resume = 1;
1286 usb_set_intfdata(interface, hif_dev);
1288 init_completion(&hif_dev->fw_done);
1290 ret = ath9k_hif_request_firmware(hif_dev, true);
1297 usb_set_intfdata(interface, NULL);
1304 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1306 u32 reboot_cmd = 0xffffffff;
1310 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1314 ret = usb_interrupt_msg(udev, usb_sndintpipe(udev, USB_REG_OUT_PIPE),
1317 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1322 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1324 struct usb_device *udev = interface_to_usbdev(interface);
1325 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1326 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1331 wait_for_completion(&hif_dev->fw_done);
1333 if (hif_dev->flags & HIF_USB_READY) {
1334 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1335 ath9k_htc_hw_free(hif_dev->htc_handle);
1336 ath9k_hif_usb_dev_deinit(hif_dev);
1339 usb_set_intfdata(interface, NULL);
1341 /* If firmware was loaded we should drop it
1342 * go back to first stage bootloader. */
1343 if (!unplugged && (hif_dev->flags & HIF_USB_READY))
1344 ath9k_hif_usb_reboot(udev);
1347 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1352 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1353 pm_message_t message)
1355 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1358 * The device has to be set to FULLSLEEP mode in case no
1361 if (!(hif_dev->flags & HIF_USB_START))
1362 ath9k_htc_suspend(hif_dev->htc_handle);
1364 wait_for_completion(&hif_dev->fw_done);
1366 if (hif_dev->flags & HIF_USB_READY)
1367 ath9k_hif_usb_dealloc_urbs(hif_dev);
1372 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1374 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1375 struct htc_target *htc_handle = hif_dev->htc_handle;
1377 const struct firmware *fw;
1379 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1383 if (hif_dev->flags & HIF_USB_READY) {
1384 /* request cached firmware during suspend/resume cycle */
1385 ret = request_firmware(&fw, hif_dev->fw_name,
1386 &hif_dev->udev->dev);
1390 hif_dev->fw_data = fw->data;
1391 hif_dev->fw_size = fw->size;
1392 ret = ath9k_hif_usb_download_fw(hif_dev);
1393 release_firmware(fw);
1397 ath9k_hif_usb_dealloc_urbs(hif_dev);
1403 ret = ath9k_htc_resume(htc_handle);
1411 ath9k_hif_usb_dealloc_urbs(hif_dev);
1417 static struct usb_driver ath9k_hif_usb_driver = {
1418 .name = KBUILD_MODNAME,
1419 .probe = ath9k_hif_usb_probe,
1420 .disconnect = ath9k_hif_usb_disconnect,
1422 .suspend = ath9k_hif_usb_suspend,
1423 .resume = ath9k_hif_usb_resume,
1424 .reset_resume = ath9k_hif_usb_resume,
1426 .id_table = ath9k_hif_usb_ids,
1428 .disable_hub_initiated_lpm = 1,
1431 int ath9k_hif_usb_init(void)
1433 return usb_register(&ath9k_hif_usb_driver);
1436 void ath9k_hif_usb_exit(void)
1438 usb_deregister(&ath9k_hif_usb_driver);