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 */
40 { USB_DEVICE(0x1eda, 0x2315) }, /* AirTies */
42 { USB_DEVICE(0x0cf3, 0x7015),
43 .driver_info = AR9287_USB }, /* Atheros */
44 { USB_DEVICE(0x1668, 0x1200),
45 .driver_info = AR9287_USB }, /* Verizon */
47 { USB_DEVICE(0x0cf3, 0x7010),
48 .driver_info = AR9280_USB }, /* Atheros */
49 { USB_DEVICE(0x0846, 0x9018),
50 .driver_info = AR9280_USB }, /* Netgear WNDA3200 */
51 { USB_DEVICE(0x083A, 0xA704),
52 .driver_info = AR9280_USB }, /* SMC Networks */
53 { USB_DEVICE(0x0411, 0x017f),
54 .driver_info = AR9280_USB }, /* Sony UWA-BR100 */
55 { USB_DEVICE(0x0411, 0x0197),
56 .driver_info = AR9280_USB }, /* Buffalo WLI-UV-AG300P */
57 { USB_DEVICE(0x04da, 0x3904),
58 .driver_info = AR9280_USB },
60 { USB_DEVICE(0x0cf3, 0x20ff),
61 .driver_info = STORAGE_DEVICE },
66 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
68 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
70 static void hif_usb_regout_cb(struct urb *urb)
72 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
74 switch (urb->status) {
87 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
98 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
105 urb = usb_alloc_urb(0, GFP_KERNEL);
109 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
116 cmd->hif_dev = hif_dev;
118 usb_fill_int_urb(urb, hif_dev->udev,
119 usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
121 hif_usb_regout_cb, cmd, 1);
123 usb_anchor_urb(urb, &hif_dev->regout_submitted);
124 ret = usb_submit_urb(urb, GFP_KERNEL);
126 usb_unanchor_urb(urb);
134 static void hif_usb_mgmt_cb(struct urb *urb)
136 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
137 struct hif_device_usb *hif_dev;
140 if (!cmd || !cmd->skb || !cmd->hif_dev)
143 hif_dev = cmd->hif_dev;
145 switch (urb->status) {
155 * If the URBs are being flushed, no need to complete
158 spin_lock(&hif_dev->tx.tx_lock);
159 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
160 spin_unlock(&hif_dev->tx.tx_lock);
161 dev_kfree_skb_any(cmd->skb);
165 spin_unlock(&hif_dev->tx.tx_lock);
173 skb_pull(cmd->skb, 4);
174 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
179 static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
187 urb = usb_alloc_urb(0, GFP_ATOMIC);
191 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
198 cmd->hif_dev = hif_dev;
200 hdr = (__le16 *) skb_push(skb, 4);
201 *hdr++ = cpu_to_le16(skb->len - 4);
202 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
204 usb_fill_bulk_urb(urb, hif_dev->udev,
205 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
207 hif_usb_mgmt_cb, cmd);
209 usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
210 ret = usb_submit_urb(urb, GFP_ATOMIC);
212 usb_unanchor_urb(urb);
220 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
221 struct sk_buff_head *list)
225 while ((skb = __skb_dequeue(list)) != NULL) {
226 dev_kfree_skb_any(skb);
230 static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
231 struct sk_buff_head *queue,
236 while ((skb = __skb_dequeue(queue)) != NULL) {
237 #ifdef CONFIG_ATH9K_HTC_DEBUGFS
240 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
243 TX_STAT_INC(skb_success);
244 TX_STAT_ADD(skb_success_bytes, ln);
247 TX_STAT_INC(skb_failed);
251 static void hif_usb_tx_cb(struct urb *urb)
253 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
254 struct hif_device_usb *hif_dev;
257 if (!tx_buf || !tx_buf->hif_dev)
260 hif_dev = tx_buf->hif_dev;
262 switch (urb->status) {
272 * If the URBs are being flushed, no need to add this
273 * URB to the free list.
275 spin_lock(&hif_dev->tx.tx_lock);
276 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
277 spin_unlock(&hif_dev->tx.tx_lock);
278 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
281 spin_unlock(&hif_dev->tx.tx_lock);
289 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
291 /* Re-initialize the SKB queue */
292 tx_buf->len = tx_buf->offset = 0;
293 __skb_queue_head_init(&tx_buf->skb_queue);
295 /* Add this TX buffer to the free list */
296 spin_lock(&hif_dev->tx.tx_lock);
297 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
298 hif_dev->tx.tx_buf_cnt++;
299 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
300 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
301 TX_STAT_INC(buf_completed);
302 spin_unlock(&hif_dev->tx.tx_lock);
305 /* TX lock has to be taken */
306 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
308 struct tx_buf *tx_buf = NULL;
309 struct sk_buff *nskb = NULL;
315 if (hif_dev->tx.tx_skb_cnt == 0)
318 /* Check if a free TX buffer is available */
319 if (list_empty(&hif_dev->tx.tx_buf))
322 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
323 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
324 hif_dev->tx.tx_buf_cnt--;
326 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
328 for (i = 0; i < tx_skb_cnt; i++) {
329 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
331 /* Should never be NULL */
334 hif_dev->tx.tx_skb_cnt--;
337 buf += tx_buf->offset;
339 *hdr++ = cpu_to_le16(nskb->len);
340 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
342 memcpy(buf, nskb->data, nskb->len);
343 tx_buf->len = nskb->len + 4;
345 if (i < (tx_skb_cnt - 1))
346 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
348 if (i == (tx_skb_cnt - 1))
349 tx_buf->len += tx_buf->offset;
351 __skb_queue_tail(&tx_buf->skb_queue, nskb);
352 TX_STAT_INC(skb_queued);
355 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
356 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
357 tx_buf->buf, tx_buf->len,
358 hif_usb_tx_cb, tx_buf);
360 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
362 tx_buf->len = tx_buf->offset = 0;
363 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
364 __skb_queue_head_init(&tx_buf->skb_queue);
365 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
366 hif_dev->tx.tx_buf_cnt++;
370 TX_STAT_INC(buf_queued);
375 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
377 struct ath9k_htc_tx_ctl *tx_ctl;
381 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
383 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
384 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
388 /* Check if the max queue count has been reached */
389 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
390 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
394 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
396 tx_ctl = HTC_SKB_CB(skb);
398 /* Mgmt/Beacon frames don't use the TX buffer pool */
399 if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
400 (tx_ctl->type == ATH9K_HTC_BEACON)) {
401 ret = hif_usb_send_mgmt(hif_dev, skb);
404 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
406 if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
407 (tx_ctl->type == ATH9K_HTC_AMPDU)) {
408 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
409 hif_dev->tx.tx_skb_cnt++;
412 /* Check if AMPDUs have to be sent immediately */
413 if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
414 (hif_dev->tx.tx_skb_cnt < 2)) {
415 __hif_usb_tx(hif_dev);
418 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
423 static void hif_usb_start(void *hif_handle)
425 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
428 hif_dev->flags |= HIF_USB_START;
430 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
431 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
432 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
435 static void hif_usb_stop(void *hif_handle)
437 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
438 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
441 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
442 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
443 hif_dev->tx.tx_skb_cnt = 0;
444 hif_dev->tx.flags |= HIF_USB_TX_STOP;
445 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
447 /* The pending URBs have to be canceled. */
448 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
449 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
450 &hif_dev->tx.tx_pending, list) {
451 usb_get_urb(tx_buf->urb);
452 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
453 usb_kill_urb(tx_buf->urb);
454 list_del(&tx_buf->list);
455 usb_free_urb(tx_buf->urb);
458 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
460 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
462 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
465 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
467 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
471 case USB_WLAN_TX_PIPE:
472 ret = hif_usb_send_tx(hif_dev, skb);
474 case USB_REG_OUT_PIPE:
475 ret = hif_usb_send_regout(hif_dev, skb);
478 dev_err(&hif_dev->udev->dev,
479 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
487 static inline bool check_index(struct sk_buff *skb, u8 idx)
489 struct ath9k_htc_tx_ctl *tx_ctl;
491 tx_ctl = HTC_SKB_CB(skb);
493 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
494 (tx_ctl->sta_idx == idx))
500 static void hif_usb_sta_drain(void *hif_handle, u8 idx)
502 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
503 struct sk_buff *skb, *tmp;
506 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
508 skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
509 if (check_index(skb, idx)) {
510 __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
511 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
513 hif_dev->tx.tx_skb_cnt--;
514 TX_STAT_INC(skb_failed);
518 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
521 static struct ath9k_htc_hif hif_usb = {
522 .transport = ATH9K_HIF_USB,
523 .name = "ath9k_hif_usb",
525 .control_ul_pipe = USB_REG_OUT_PIPE,
526 .control_dl_pipe = USB_REG_IN_PIPE,
528 .start = hif_usb_start,
529 .stop = hif_usb_stop,
530 .sta_drain = hif_usb_sta_drain,
531 .send = hif_usb_send,
534 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
537 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
538 int index = 0, i = 0, len = skb->len;
539 int rx_remain_len, rx_pkt_len;
543 spin_lock(&hif_dev->rx_lock);
545 rx_remain_len = hif_dev->rx_remain_len;
546 rx_pkt_len = hif_dev->rx_transfer_len;
548 if (rx_remain_len != 0) {
549 struct sk_buff *remain_skb = hif_dev->remain_skb;
552 ptr = (u8 *) remain_skb->data;
554 index = rx_remain_len;
555 rx_remain_len -= hif_dev->rx_pad_len;
558 memcpy(ptr, skb->data, rx_remain_len);
560 rx_pkt_len += rx_remain_len;
561 hif_dev->rx_remain_len = 0;
562 skb_put(remain_skb, rx_pkt_len);
564 skb_pool[pool_index++] = remain_skb;
567 index = rx_remain_len;
571 spin_unlock(&hif_dev->rx_lock);
573 while (index < len) {
579 ptr = (u8 *) skb->data;
581 pkt_len = get_unaligned_le16(ptr + index);
582 pkt_tag = get_unaligned_le16(ptr + index + 2);
584 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
585 RX_STAT_INC(skb_dropped);
589 pad_len = 4 - (pkt_len & 0x3);
594 index = index + 4 + pkt_len + pad_len;
596 if (index > MAX_RX_BUF_SIZE) {
597 spin_lock(&hif_dev->rx_lock);
598 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
599 hif_dev->rx_transfer_len =
600 MAX_RX_BUF_SIZE - chk_idx - 4;
601 hif_dev->rx_pad_len = pad_len;
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");
607 spin_unlock(&hif_dev->rx_lock);
610 skb_reserve(nskb, 32);
611 RX_STAT_INC(skb_allocated);
613 memcpy(nskb->data, &(skb->data[chk_idx+4]),
614 hif_dev->rx_transfer_len);
616 /* Record the buffer pointer */
617 hif_dev->remain_skb = nskb;
618 spin_unlock(&hif_dev->rx_lock);
620 if (pool_index == MAX_PKT_NUM_IN_TRANSFER) {
621 dev_err(&hif_dev->udev->dev,
622 "ath9k_htc: over RX MAX_PKT_NUM\n");
625 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
627 dev_err(&hif_dev->udev->dev,
628 "ath9k_htc: RX memory allocation error\n");
631 skb_reserve(nskb, 32);
632 RX_STAT_INC(skb_allocated);
634 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
635 skb_put(nskb, pkt_len);
636 skb_pool[pool_index++] = nskb;
641 for (i = 0; i < pool_index; i++) {
642 RX_STAT_ADD(skb_completed_bytes, skb_pool[i]->len);
643 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
644 skb_pool[i]->len, USB_WLAN_RX_PIPE);
645 RX_STAT_INC(skb_completed);
649 static void ath9k_hif_usb_rx_cb(struct urb *urb)
651 struct rx_buf *rx_buf = (struct rx_buf *)urb->context;
652 struct hif_device_usb *hif_dev = rx_buf->hif_dev;
653 struct sk_buff *skb = rx_buf->skb;
662 switch (urb->status) {
674 if (likely(urb->actual_length != 0)) {
675 skb_put(skb, urb->actual_length);
676 ath9k_hif_usb_rx_stream(hif_dev, skb);
680 skb_reset_tail_pointer(skb);
683 usb_anchor_urb(urb, &hif_dev->rx_submitted);
684 ret = usb_submit_urb(urb, GFP_ATOMIC);
686 usb_unanchor_urb(urb);
696 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
698 struct rx_buf *rx_buf = (struct rx_buf *)urb->context;
699 struct hif_device_usb *hif_dev = rx_buf->hif_dev;
700 struct sk_buff *skb = rx_buf->skb;
701 struct sk_buff *nskb;
710 switch (urb->status) {
719 skb_reset_tail_pointer(skb);
725 if (likely(urb->actual_length != 0)) {
726 skb_put(skb, urb->actual_length);
728 /* Process the command first */
729 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
730 skb->len, USB_REG_IN_PIPE);
733 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
735 dev_err(&hif_dev->udev->dev,
736 "ath9k_htc: REG_IN memory allocation failure\n");
743 usb_fill_int_urb(urb, hif_dev->udev,
744 usb_rcvintpipe(hif_dev->udev,
746 nskb->data, MAX_REG_IN_BUF_SIZE,
747 ath9k_hif_usb_reg_in_cb, rx_buf, 1);
751 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
752 ret = usb_submit_urb(urb, GFP_ATOMIC);
754 usb_unanchor_urb(urb);
765 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
767 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
770 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
771 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
772 &hif_dev->tx.tx_buf, list) {
773 usb_get_urb(tx_buf->urb);
774 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
775 usb_kill_urb(tx_buf->urb);
776 list_del(&tx_buf->list);
777 usb_free_urb(tx_buf->urb);
780 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
782 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
784 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
785 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
786 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
788 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
789 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
790 &hif_dev->tx.tx_pending, list) {
791 usb_get_urb(tx_buf->urb);
792 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
793 usb_kill_urb(tx_buf->urb);
794 list_del(&tx_buf->list);
795 usb_free_urb(tx_buf->urb);
798 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
800 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
802 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
805 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
807 struct tx_buf *tx_buf;
810 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
811 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
812 spin_lock_init(&hif_dev->tx.tx_lock);
813 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
814 init_usb_anchor(&hif_dev->mgmt_submitted);
816 for (i = 0; i < MAX_TX_URB_NUM; i++) {
817 tx_buf = kzalloc(sizeof(*tx_buf), GFP_KERNEL);
821 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
825 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
829 tx_buf->hif_dev = hif_dev;
830 __skb_queue_head_init(&tx_buf->skb_queue);
832 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
835 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
843 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
847 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
849 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
852 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
854 struct rx_buf *rx_buf = NULL;
855 struct sk_buff *skb = NULL;
856 struct urb *urb = NULL;
859 init_usb_anchor(&hif_dev->rx_submitted);
860 spin_lock_init(&hif_dev->rx_lock);
862 for (i = 0; i < MAX_RX_URB_NUM; i++) {
864 rx_buf = kzalloc(sizeof(*rx_buf), GFP_KERNEL);
871 urb = usb_alloc_urb(0, GFP_KERNEL);
877 /* Allocate buffer */
878 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
884 rx_buf->hif_dev = hif_dev;
887 usb_fill_bulk_urb(urb, hif_dev->udev,
888 usb_rcvbulkpipe(hif_dev->udev,
890 skb->data, MAX_RX_BUF_SIZE,
891 ath9k_hif_usb_rx_cb, rx_buf);
894 usb_anchor_urb(urb, &hif_dev->rx_submitted);
897 ret = usb_submit_urb(urb, GFP_KERNEL);
899 usb_unanchor_urb(urb);
904 * Drop reference count.
905 * This ensures that the URB is freed when killing them.
919 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
923 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
925 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
928 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
930 struct rx_buf *rx_buf = NULL;
931 struct sk_buff *skb = NULL;
932 struct urb *urb = NULL;
935 init_usb_anchor(&hif_dev->reg_in_submitted);
937 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
939 rx_buf = kzalloc(sizeof(*rx_buf), GFP_KERNEL);
946 urb = usb_alloc_urb(0, GFP_KERNEL);
952 /* Allocate buffer */
953 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
959 rx_buf->hif_dev = hif_dev;
962 usb_fill_int_urb(urb, hif_dev->udev,
963 usb_rcvintpipe(hif_dev->udev,
965 skb->data, MAX_REG_IN_BUF_SIZE,
966 ath9k_hif_usb_reg_in_cb, rx_buf, 1);
969 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
972 ret = usb_submit_urb(urb, GFP_KERNEL);
974 usb_unanchor_urb(urb);
979 * Drop reference count.
980 * This ensures that the URB is freed when killing them.
994 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
998 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
1000 /* Register Write */
1001 init_usb_anchor(&hif_dev->regout_submitted);
1004 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
1008 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
1012 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
1017 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
1019 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
1024 void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
1026 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
1027 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
1028 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
1029 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
1032 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
1035 const void *data = hif_dev->fw_data;
1036 size_t len = hif_dev->fw_size;
1037 u32 addr = AR9271_FIRMWARE;
1038 u8 *buf = kzalloc(4096, GFP_KERNEL);
1045 transfer = min_t(size_t, len, 4096);
1046 memcpy(buf, data, transfer);
1048 err = usb_control_msg(hif_dev->udev,
1049 usb_sndctrlpipe(hif_dev->udev, 0),
1050 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
1051 addr >> 8, 0, buf, transfer, HZ);
1063 if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1064 firm_offset = AR7010_FIRMWARE_TEXT;
1066 firm_offset = AR9271_FIRMWARE_TEXT;
1069 * Issue FW download complete command to firmware.
1071 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1072 FIRMWARE_DOWNLOAD_COMP,
1074 firm_offset >> 8, 0, NULL, 0, HZ);
1078 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1079 hif_dev->fw_name, (unsigned long) hif_dev->fw_size);
1084 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
1088 ret = ath9k_hif_usb_download_fw(hif_dev);
1090 dev_err(&hif_dev->udev->dev,
1091 "ath9k_htc: Firmware - %s download failed\n",
1097 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1099 dev_err(&hif_dev->udev->dev,
1100 "ath9k_htc: Unable to allocate URBs\n");
1107 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1109 ath9k_hif_usb_dealloc_urbs(hif_dev);
1113 * If initialization fails or the FW cannot be retrieved,
1114 * detach the device.
1116 static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev)
1118 struct device *dev = &hif_dev->udev->dev;
1119 struct device *parent = dev->parent;
1121 complete_all(&hif_dev->fw_done);
1124 device_lock(parent);
1126 device_release_driver(dev);
1129 device_unlock(parent);
1132 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context);
1134 /* taken from iwlwifi */
1135 static int ath9k_hif_request_firmware(struct hif_device_usb *hif_dev,
1138 char index[8], *chip;
1142 if (htc_use_dev_fw) {
1143 hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX + 1;
1144 sprintf(index, "%s", "dev");
1146 hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX;
1147 sprintf(index, "%d", hif_dev->fw_minor_index);
1150 hif_dev->fw_minor_index--;
1151 sprintf(index, "%d", hif_dev->fw_minor_index);
1154 /* test for FW 1.3 */
1155 if (MAJOR_VERSION_REQ == 1 && hif_dev->fw_minor_index == 3) {
1156 const char *filename;
1158 if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1159 filename = FIRMWARE_AR7010_1_1;
1161 filename = FIRMWARE_AR9271;
1163 /* expected fw locations:
1164 * - htc_9271.fw (stable version 1.3, depricated)
1166 snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1169 } else if (hif_dev->fw_minor_index < FIRMWARE_MINOR_IDX_MIN) {
1170 dev_err(&hif_dev->udev->dev, "no suitable firmware found!\n");
1174 if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
1179 /* expected fw locations:
1180 * - ath9k_htc/htc_9271-1.dev.0.fw (development version)
1181 * - ath9k_htc/htc_9271-1.4.0.fw (stable version)
1183 snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
1184 "%s/htc_%s-%d.%s.0.fw", HTC_FW_PATH,
1185 chip, MAJOR_VERSION_REQ, index);
1188 ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name,
1189 &hif_dev->udev->dev, GFP_KERNEL,
1190 hif_dev, ath9k_hif_usb_firmware_cb);
1192 dev_err(&hif_dev->udev->dev,
1193 "ath9k_htc: Async request for firmware %s failed\n",
1198 dev_info(&hif_dev->udev->dev, "ath9k_htc: Firmware %s requested\n",
1204 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context)
1206 struct hif_device_usb *hif_dev = context;
1210 ret = ath9k_hif_request_firmware(hif_dev, false);
1214 dev_err(&hif_dev->udev->dev,
1215 "ath9k_htc: Failed to get firmware %s\n",
1220 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1221 &hif_dev->udev->dev);
1222 if (hif_dev->htc_handle == NULL)
1225 hif_dev->fw_data = fw->data;
1226 hif_dev->fw_size = fw->size;
1228 /* Proceed with initialization */
1230 ret = ath9k_hif_usb_dev_init(hif_dev);
1234 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1235 &hif_dev->interface->dev,
1236 hif_dev->usb_device_id->idProduct,
1237 hif_dev->udev->product,
1238 hif_dev->usb_device_id->driver_info);
1241 goto err_htc_hw_init;
1244 release_firmware(fw);
1245 hif_dev->flags |= HIF_USB_READY;
1246 complete_all(&hif_dev->fw_done);
1251 ath9k_hif_usb_dev_deinit(hif_dev);
1253 ath9k_htc_hw_free(hif_dev->htc_handle);
1255 release_firmware(fw);
1257 ath9k_hif_usb_firmware_fail(hif_dev);
1261 * An exact copy of the function from zd1211rw.
1263 static int send_eject_command(struct usb_interface *interface)
1265 struct usb_device *udev = interface_to_usbdev(interface);
1266 struct usb_host_interface *iface_desc = interface->cur_altsetting;
1267 struct usb_endpoint_descriptor *endpoint;
1272 if (iface_desc->desc.bNumEndpoints < 2)
1275 /* Find bulk out endpoint */
1276 for (r = 1; r >= 0; r--) {
1277 endpoint = &iface_desc->endpoint[r].desc;
1278 if (usb_endpoint_dir_out(endpoint) &&
1279 usb_endpoint_xfer_bulk(endpoint)) {
1280 bulk_out_ep = endpoint->bEndpointAddress;
1286 "ath9k_htc: Could not find bulk out endpoint\n");
1290 cmd = kzalloc(31, GFP_KERNEL);
1294 /* USB bulk command block */
1295 cmd[0] = 0x55; /* bulk command signature */
1296 cmd[1] = 0x53; /* bulk command signature */
1297 cmd[2] = 0x42; /* bulk command signature */
1298 cmd[3] = 0x43; /* bulk command signature */
1299 cmd[14] = 6; /* command length */
1301 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1302 cmd[19] = 0x2; /* eject disc */
1304 dev_info(&udev->dev, "Ejecting storage device...\n");
1305 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1306 cmd, 31, NULL, 2000);
1311 /* At this point, the device disconnects and reconnects with the real
1314 usb_set_intfdata(interface, NULL);
1318 static int ath9k_hif_usb_probe(struct usb_interface *interface,
1319 const struct usb_device_id *id)
1321 struct usb_device *udev = interface_to_usbdev(interface);
1322 struct hif_device_usb *hif_dev;
1325 if (id->driver_info == STORAGE_DEVICE)
1326 return send_eject_command(interface);
1328 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1336 hif_dev->udev = udev;
1337 hif_dev->interface = interface;
1338 hif_dev->usb_device_id = id;
1340 udev->reset_resume = 1;
1342 usb_set_intfdata(interface, hif_dev);
1344 init_completion(&hif_dev->fw_done);
1346 ret = ath9k_hif_request_firmware(hif_dev, true);
1353 usb_set_intfdata(interface, NULL);
1360 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1362 u32 reboot_cmd = 0xffffffff;
1366 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1370 ret = usb_interrupt_msg(udev, usb_sndintpipe(udev, USB_REG_OUT_PIPE),
1373 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1378 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1380 struct usb_device *udev = interface_to_usbdev(interface);
1381 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1382 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1387 wait_for_completion(&hif_dev->fw_done);
1389 if (hif_dev->flags & HIF_USB_READY) {
1390 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1391 ath9k_hif_usb_dev_deinit(hif_dev);
1392 ath9k_destoy_wmi(hif_dev->htc_handle->drv_priv);
1393 ath9k_htc_hw_free(hif_dev->htc_handle);
1396 usb_set_intfdata(interface, NULL);
1398 /* If firmware was loaded we should drop it
1399 * go back to first stage bootloader. */
1400 if (!unplugged && (hif_dev->flags & HIF_USB_READY))
1401 ath9k_hif_usb_reboot(udev);
1404 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1409 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1410 pm_message_t message)
1412 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1415 * The device has to be set to FULLSLEEP mode in case no
1418 if (!(hif_dev->flags & HIF_USB_START))
1419 ath9k_htc_suspend(hif_dev->htc_handle);
1421 wait_for_completion(&hif_dev->fw_done);
1423 if (hif_dev->flags & HIF_USB_READY)
1424 ath9k_hif_usb_dealloc_urbs(hif_dev);
1429 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1431 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1432 struct htc_target *htc_handle = hif_dev->htc_handle;
1434 const struct firmware *fw;
1436 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1440 if (hif_dev->flags & HIF_USB_READY) {
1441 /* request cached firmware during suspend/resume cycle */
1442 ret = request_firmware(&fw, hif_dev->fw_name,
1443 &hif_dev->udev->dev);
1447 hif_dev->fw_data = fw->data;
1448 hif_dev->fw_size = fw->size;
1449 ret = ath9k_hif_usb_download_fw(hif_dev);
1450 release_firmware(fw);
1454 ath9k_hif_usb_dealloc_urbs(hif_dev);
1460 ret = ath9k_htc_resume(htc_handle);
1468 ath9k_hif_usb_dealloc_urbs(hif_dev);
1474 static struct usb_driver ath9k_hif_usb_driver = {
1475 .name = KBUILD_MODNAME,
1476 .probe = ath9k_hif_usb_probe,
1477 .disconnect = ath9k_hif_usb_disconnect,
1479 .suspend = ath9k_hif_usb_suspend,
1480 .resume = ath9k_hif_usb_resume,
1481 .reset_resume = ath9k_hif_usb_resume,
1483 .id_table = ath9k_hif_usb_ids,
1485 .disable_hub_initiated_lpm = 1,
1488 int ath9k_hif_usb_init(void)
1490 return usb_register(&ath9k_hif_usb_driver);
1493 void ath9k_hif_usb_exit(void)
1495 usb_deregister(&ath9k_hif_usb_driver);