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.
19 /* identify firmware images */
20 #define FIRMWARE_AR7010_1_1 "htc_7010.fw"
21 #define FIRMWARE_AR9271 "htc_9271.fw"
23 MODULE_FIRMWARE(FIRMWARE_AR7010_1_1);
24 MODULE_FIRMWARE(FIRMWARE_AR9271);
26 static struct usb_device_id ath9k_hif_usb_ids[] = {
27 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
28 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
29 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
30 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
31 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
32 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
33 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
34 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
35 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
36 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
37 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
38 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
39 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
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 */
55 { USB_DEVICE(0x0cf3, 0x20ff),
56 .driver_info = STORAGE_DEVICE },
61 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
63 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
65 static void hif_usb_regout_cb(struct urb *urb)
67 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
69 switch (urb->status) {
82 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
93 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
100 urb = usb_alloc_urb(0, GFP_KERNEL);
104 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
111 cmd->hif_dev = hif_dev;
113 usb_fill_bulk_urb(urb, hif_dev->udev,
114 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
116 hif_usb_regout_cb, cmd);
118 usb_anchor_urb(urb, &hif_dev->regout_submitted);
119 ret = usb_submit_urb(urb, GFP_KERNEL);
121 usb_unanchor_urb(urb);
129 static void hif_usb_mgmt_cb(struct urb *urb)
131 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
132 struct hif_device_usb *hif_dev = cmd->hif_dev;
135 if (!cmd || !cmd->skb || !cmd->hif_dev)
138 switch (urb->status) {
148 * If the URBs are being flushed, no need to complete
151 spin_lock(&hif_dev->tx.tx_lock);
152 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
153 spin_unlock(&hif_dev->tx.tx_lock);
154 dev_kfree_skb_any(cmd->skb);
158 spin_unlock(&hif_dev->tx.tx_lock);
166 skb_pull(cmd->skb, 4);
167 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
172 static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
180 urb = usb_alloc_urb(0, GFP_ATOMIC);
184 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
191 cmd->hif_dev = hif_dev;
193 hdr = (__le16 *) skb_push(skb, 4);
194 *hdr++ = cpu_to_le16(skb->len - 4);
195 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
197 usb_fill_bulk_urb(urb, hif_dev->udev,
198 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
200 hif_usb_mgmt_cb, cmd);
202 usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
203 ret = usb_submit_urb(urb, GFP_ATOMIC);
205 usb_unanchor_urb(urb);
213 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
214 struct sk_buff_head *list)
218 while ((skb = __skb_dequeue(list)) != NULL) {
219 dev_kfree_skb_any(skb);
223 static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
224 struct sk_buff_head *queue,
229 while ((skb = __skb_dequeue(queue)) != NULL) {
230 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
233 TX_STAT_INC(skb_success);
235 TX_STAT_INC(skb_failed);
239 static void hif_usb_tx_cb(struct urb *urb)
241 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
242 struct hif_device_usb *hif_dev;
245 if (!tx_buf || !tx_buf->hif_dev)
248 hif_dev = tx_buf->hif_dev;
250 switch (urb->status) {
260 * If the URBs are being flushed, no need to add this
261 * URB to the free list.
263 spin_lock(&hif_dev->tx.tx_lock);
264 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
265 spin_unlock(&hif_dev->tx.tx_lock);
266 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
269 spin_unlock(&hif_dev->tx.tx_lock);
277 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
279 /* Re-initialize the SKB queue */
280 tx_buf->len = tx_buf->offset = 0;
281 __skb_queue_head_init(&tx_buf->skb_queue);
283 /* Add this TX buffer to the free list */
284 spin_lock(&hif_dev->tx.tx_lock);
285 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
286 hif_dev->tx.tx_buf_cnt++;
287 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
288 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
289 TX_STAT_INC(buf_completed);
290 spin_unlock(&hif_dev->tx.tx_lock);
293 /* TX lock has to be taken */
294 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
296 struct tx_buf *tx_buf = NULL;
297 struct sk_buff *nskb = NULL;
303 if (hif_dev->tx.tx_skb_cnt == 0)
306 /* Check if a free TX buffer is available */
307 if (list_empty(&hif_dev->tx.tx_buf))
310 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
311 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
312 hif_dev->tx.tx_buf_cnt--;
314 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
316 for (i = 0; i < tx_skb_cnt; i++) {
317 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
319 /* Should never be NULL */
322 hif_dev->tx.tx_skb_cnt--;
325 buf += tx_buf->offset;
327 *hdr++ = cpu_to_le16(nskb->len);
328 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
330 memcpy(buf, nskb->data, nskb->len);
331 tx_buf->len = nskb->len + 4;
333 if (i < (tx_skb_cnt - 1))
334 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
336 if (i == (tx_skb_cnt - 1))
337 tx_buf->len += tx_buf->offset;
339 __skb_queue_tail(&tx_buf->skb_queue, nskb);
340 TX_STAT_INC(skb_queued);
343 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
344 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
345 tx_buf->buf, tx_buf->len,
346 hif_usb_tx_cb, tx_buf);
348 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
350 tx_buf->len = tx_buf->offset = 0;
351 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
352 __skb_queue_head_init(&tx_buf->skb_queue);
353 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
354 hif_dev->tx.tx_buf_cnt++;
358 TX_STAT_INC(buf_queued);
363 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
365 struct ath9k_htc_tx_ctl *tx_ctl;
369 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
371 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
372 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
376 /* Check if the max queue count has been reached */
377 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
378 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
382 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
384 tx_ctl = HTC_SKB_CB(skb);
386 /* Mgmt/Beacon frames don't use the TX buffer pool */
387 if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
388 (tx_ctl->type == ATH9K_HTC_BEACON)) {
389 ret = hif_usb_send_mgmt(hif_dev, skb);
392 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
394 if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
395 (tx_ctl->type == ATH9K_HTC_AMPDU)) {
396 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
397 hif_dev->tx.tx_skb_cnt++;
400 /* Check if AMPDUs have to be sent immediately */
401 if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
402 (hif_dev->tx.tx_skb_cnt < 2)) {
403 __hif_usb_tx(hif_dev);
406 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
411 static void hif_usb_start(void *hif_handle)
413 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
416 hif_dev->flags |= HIF_USB_START;
418 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
419 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
420 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
423 static void hif_usb_stop(void *hif_handle)
425 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
426 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
429 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
430 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
431 hif_dev->tx.tx_skb_cnt = 0;
432 hif_dev->tx.flags |= HIF_USB_TX_STOP;
433 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
435 /* The pending URBs have to be canceled. */
436 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
437 &hif_dev->tx.tx_pending, list) {
438 usb_kill_urb(tx_buf->urb);
441 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
444 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
446 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
450 case USB_WLAN_TX_PIPE:
451 ret = hif_usb_send_tx(hif_dev, skb);
453 case USB_REG_OUT_PIPE:
454 ret = hif_usb_send_regout(hif_dev, skb);
457 dev_err(&hif_dev->udev->dev,
458 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
466 static inline bool check_index(struct sk_buff *skb, u8 idx)
468 struct ath9k_htc_tx_ctl *tx_ctl;
470 tx_ctl = HTC_SKB_CB(skb);
472 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
473 (tx_ctl->sta_idx == idx))
479 static void hif_usb_sta_drain(void *hif_handle, u8 idx)
481 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
482 struct sk_buff *skb, *tmp;
485 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
487 skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
488 if (check_index(skb, idx)) {
489 __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
490 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
492 hif_dev->tx.tx_skb_cnt--;
493 TX_STAT_INC(skb_failed);
497 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
500 static struct ath9k_htc_hif hif_usb = {
501 .transport = ATH9K_HIF_USB,
502 .name = "ath9k_hif_usb",
504 .control_ul_pipe = USB_REG_OUT_PIPE,
505 .control_dl_pipe = USB_REG_IN_PIPE,
507 .start = hif_usb_start,
508 .stop = hif_usb_stop,
509 .sta_drain = hif_usb_sta_drain,
510 .send = hif_usb_send,
513 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
516 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
517 int index = 0, i = 0, len = skb->len;
518 int rx_remain_len, rx_pkt_len;
522 spin_lock(&hif_dev->rx_lock);
524 rx_remain_len = hif_dev->rx_remain_len;
525 rx_pkt_len = hif_dev->rx_transfer_len;
527 if (rx_remain_len != 0) {
528 struct sk_buff *remain_skb = hif_dev->remain_skb;
531 ptr = (u8 *) remain_skb->data;
533 index = rx_remain_len;
534 rx_remain_len -= hif_dev->rx_pad_len;
537 memcpy(ptr, skb->data, rx_remain_len);
539 rx_pkt_len += rx_remain_len;
540 hif_dev->rx_remain_len = 0;
541 skb_put(remain_skb, rx_pkt_len);
543 skb_pool[pool_index++] = remain_skb;
546 index = rx_remain_len;
550 spin_unlock(&hif_dev->rx_lock);
552 while (index < len) {
558 ptr = (u8 *) skb->data;
560 pkt_len = ptr[index] + (ptr[index+1] << 8);
561 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
563 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
564 RX_STAT_INC(skb_dropped);
568 pad_len = 4 - (pkt_len & 0x3);
573 index = index + 4 + pkt_len + pad_len;
575 if (index > MAX_RX_BUF_SIZE) {
576 spin_lock(&hif_dev->rx_lock);
577 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
578 hif_dev->rx_transfer_len =
579 MAX_RX_BUF_SIZE - chk_idx - 4;
580 hif_dev->rx_pad_len = pad_len;
582 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
584 dev_err(&hif_dev->udev->dev,
585 "ath9k_htc: RX memory allocation error\n");
586 spin_unlock(&hif_dev->rx_lock);
589 skb_reserve(nskb, 32);
590 RX_STAT_INC(skb_allocated);
592 memcpy(nskb->data, &(skb->data[chk_idx+4]),
593 hif_dev->rx_transfer_len);
595 /* Record the buffer pointer */
596 hif_dev->remain_skb = nskb;
597 spin_unlock(&hif_dev->rx_lock);
599 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
601 dev_err(&hif_dev->udev->dev,
602 "ath9k_htc: RX memory allocation error\n");
605 skb_reserve(nskb, 32);
606 RX_STAT_INC(skb_allocated);
608 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
609 skb_put(nskb, pkt_len);
610 skb_pool[pool_index++] = nskb;
615 for (i = 0; i < pool_index; i++) {
616 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
617 skb_pool[i]->len, USB_WLAN_RX_PIPE);
618 RX_STAT_INC(skb_completed);
622 static void ath9k_hif_usb_rx_cb(struct urb *urb)
624 struct sk_buff *skb = (struct sk_buff *) urb->context;
625 struct hif_device_usb *hif_dev =
626 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
635 switch (urb->status) {
647 if (likely(urb->actual_length != 0)) {
648 skb_put(skb, urb->actual_length);
649 ath9k_hif_usb_rx_stream(hif_dev, skb);
653 skb_reset_tail_pointer(skb);
656 usb_anchor_urb(urb, &hif_dev->rx_submitted);
657 ret = usb_submit_urb(urb, GFP_ATOMIC);
659 usb_unanchor_urb(urb);
668 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
670 struct sk_buff *skb = (struct sk_buff *) urb->context;
671 struct sk_buff *nskb;
672 struct hif_device_usb *hif_dev =
673 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
682 switch (urb->status) {
691 skb_reset_tail_pointer(skb);
697 if (likely(urb->actual_length != 0)) {
698 skb_put(skb, urb->actual_length);
700 /* Process the command first */
701 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
702 skb->len, USB_REG_IN_PIPE);
705 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
707 dev_err(&hif_dev->udev->dev,
708 "ath9k_htc: REG_IN memory allocation failure\n");
713 usb_fill_bulk_urb(urb, hif_dev->udev,
714 usb_rcvbulkpipe(hif_dev->udev,
716 nskb->data, MAX_REG_IN_BUF_SIZE,
717 ath9k_hif_usb_reg_in_cb, nskb);
721 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
722 ret = usb_submit_urb(urb, GFP_ATOMIC);
724 usb_unanchor_urb(urb);
734 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
736 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
739 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
740 &hif_dev->tx.tx_buf, list) {
741 usb_kill_urb(tx_buf->urb);
742 list_del(&tx_buf->list);
743 usb_free_urb(tx_buf->urb);
748 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
749 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
750 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
752 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
753 &hif_dev->tx.tx_pending, list) {
754 usb_kill_urb(tx_buf->urb);
755 list_del(&tx_buf->list);
756 usb_free_urb(tx_buf->urb);
761 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
764 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
766 struct tx_buf *tx_buf;
769 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
770 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
771 spin_lock_init(&hif_dev->tx.tx_lock);
772 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
773 init_usb_anchor(&hif_dev->mgmt_submitted);
775 for (i = 0; i < MAX_TX_URB_NUM; i++) {
776 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
780 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
784 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
788 tx_buf->hif_dev = hif_dev;
789 __skb_queue_head_init(&tx_buf->skb_queue);
791 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
794 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
802 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
806 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
808 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
811 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
813 struct urb *urb = NULL;
814 struct sk_buff *skb = NULL;
817 init_usb_anchor(&hif_dev->rx_submitted);
818 spin_lock_init(&hif_dev->rx_lock);
820 for (i = 0; i < MAX_RX_URB_NUM; i++) {
823 urb = usb_alloc_urb(0, GFP_KERNEL);
829 /* Allocate buffer */
830 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
836 usb_fill_bulk_urb(urb, hif_dev->udev,
837 usb_rcvbulkpipe(hif_dev->udev,
839 skb->data, MAX_RX_BUF_SIZE,
840 ath9k_hif_usb_rx_cb, skb);
843 usb_anchor_urb(urb, &hif_dev->rx_submitted);
846 ret = usb_submit_urb(urb, GFP_KERNEL);
848 usb_unanchor_urb(urb);
853 * Drop reference count.
854 * This ensures that the URB is freed when killing them.
866 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
870 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
872 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
875 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
877 struct urb *urb = NULL;
878 struct sk_buff *skb = NULL;
881 init_usb_anchor(&hif_dev->reg_in_submitted);
883 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
886 urb = usb_alloc_urb(0, GFP_KERNEL);
892 /* Allocate buffer */
893 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
899 usb_fill_bulk_urb(urb, hif_dev->udev,
900 usb_rcvbulkpipe(hif_dev->udev,
902 skb->data, MAX_REG_IN_BUF_SIZE,
903 ath9k_hif_usb_reg_in_cb, skb);
906 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
909 ret = usb_submit_urb(urb, GFP_KERNEL);
911 usb_unanchor_urb(urb);
916 * Drop reference count.
917 * This ensures that the URB is freed when killing them.
929 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
933 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
936 init_usb_anchor(&hif_dev->regout_submitted);
939 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
943 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
947 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
952 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
954 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
959 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
961 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
962 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
963 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
964 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
967 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
971 const void *data = hif_dev->firmware->data;
972 size_t len = hif_dev->firmware->size;
973 u32 addr = AR9271_FIRMWARE;
974 u8 *buf = kzalloc(4096, GFP_KERNEL);
981 transfer = min_t(int, len, 4096);
982 memcpy(buf, data, transfer);
984 err = usb_control_msg(hif_dev->udev,
985 usb_sndctrlpipe(hif_dev->udev, 0),
986 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
987 addr >> 8, 0, buf, transfer, HZ);
999 if (IS_AR7010_DEVICE(drv_info))
1000 firm_offset = AR7010_FIRMWARE_TEXT;
1002 firm_offset = AR9271_FIRMWARE_TEXT;
1005 * Issue FW download complete command to firmware.
1007 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1008 FIRMWARE_DOWNLOAD_COMP,
1010 firm_offset >> 8, 0, NULL, 0, HZ);
1014 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1015 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
1020 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
1023 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
1024 struct usb_endpoint_descriptor *endp;
1026 /* Request firmware */
1027 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
1028 &hif_dev->udev->dev);
1030 dev_err(&hif_dev->udev->dev,
1031 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
1035 /* Download firmware */
1036 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
1038 dev_err(&hif_dev->udev->dev,
1039 "ath9k_htc: Firmware - %s download failed\n",
1041 goto err_fw_download;
1044 /* On downloading the firmware to the target, the USB descriptor of EP4
1045 * is 'patched' to change the type of the endpoint to Bulk. This will
1046 * bring down CPU usage during the scan period.
1048 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
1049 endp = &alt->endpoint[idx].desc;
1050 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1051 == USB_ENDPOINT_XFER_INT) {
1052 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
1053 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
1054 endp->bInterval = 0;
1059 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1061 dev_err(&hif_dev->udev->dev,
1062 "ath9k_htc: Unable to allocate URBs\n");
1063 goto err_fw_download;
1069 release_firmware(hif_dev->firmware);
1071 hif_dev->firmware = NULL;
1075 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1077 ath9k_hif_usb_dealloc_urbs(hif_dev);
1078 if (hif_dev->firmware)
1079 release_firmware(hif_dev->firmware);
1083 * An exact copy of the function from zd1211rw.
1085 static int send_eject_command(struct usb_interface *interface)
1087 struct usb_device *udev = interface_to_usbdev(interface);
1088 struct usb_host_interface *iface_desc = &interface->altsetting[0];
1089 struct usb_endpoint_descriptor *endpoint;
1094 /* Find bulk out endpoint */
1095 for (r = 1; r >= 0; r--) {
1096 endpoint = &iface_desc->endpoint[r].desc;
1097 if (usb_endpoint_dir_out(endpoint) &&
1098 usb_endpoint_xfer_bulk(endpoint)) {
1099 bulk_out_ep = endpoint->bEndpointAddress;
1105 "ath9k_htc: Could not find bulk out endpoint\n");
1109 cmd = kzalloc(31, GFP_KERNEL);
1113 /* USB bulk command block */
1114 cmd[0] = 0x55; /* bulk command signature */
1115 cmd[1] = 0x53; /* bulk command signature */
1116 cmd[2] = 0x42; /* bulk command signature */
1117 cmd[3] = 0x43; /* bulk command signature */
1118 cmd[14] = 6; /* command length */
1120 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1121 cmd[19] = 0x2; /* eject disc */
1123 dev_info(&udev->dev, "Ejecting storage device...\n");
1124 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1125 cmd, 31, NULL, 2000);
1130 /* At this point, the device disconnects and reconnects with the real
1133 usb_set_intfdata(interface, NULL);
1137 static int ath9k_hif_usb_probe(struct usb_interface *interface,
1138 const struct usb_device_id *id)
1140 struct usb_device *udev = interface_to_usbdev(interface);
1141 struct hif_device_usb *hif_dev;
1144 if (id->driver_info == STORAGE_DEVICE)
1145 return send_eject_command(interface);
1147 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1154 hif_dev->udev = udev;
1155 hif_dev->interface = interface;
1156 hif_dev->device_id = id->idProduct;
1158 udev->reset_resume = 1;
1160 usb_set_intfdata(interface, hif_dev);
1162 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1163 &hif_dev->udev->dev);
1164 if (hif_dev->htc_handle == NULL) {
1166 goto err_htc_hw_alloc;
1169 /* Find out which firmware to load */
1171 if (IS_AR7010_DEVICE(id->driver_info))
1172 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
1174 hif_dev->fw_name = FIRMWARE_AR9271;
1176 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
1179 goto err_hif_init_usb;
1182 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1183 &interface->dev, hif_dev->device_id,
1184 hif_dev->udev->product, id->driver_info);
1187 goto err_htc_hw_init;
1190 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1195 ath9k_hif_usb_dev_deinit(hif_dev);
1197 ath9k_htc_hw_free(hif_dev->htc_handle);
1199 usb_set_intfdata(interface, NULL);
1206 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1208 u32 reboot_cmd = 0xffffffff;
1212 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1216 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1219 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1224 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1226 struct usb_device *udev = interface_to_usbdev(interface);
1227 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1228 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1233 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1234 ath9k_htc_hw_free(hif_dev->htc_handle);
1235 ath9k_hif_usb_dev_deinit(hif_dev);
1236 usb_set_intfdata(interface, NULL);
1238 if (!unplugged && (hif_dev->flags & HIF_USB_START))
1239 ath9k_hif_usb_reboot(udev);
1242 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1247 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1248 pm_message_t message)
1250 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1253 * The device has to be set to FULLSLEEP mode in case no
1256 if (!(hif_dev->flags & HIF_USB_START))
1257 ath9k_htc_suspend(hif_dev->htc_handle);
1259 ath9k_hif_usb_dealloc_urbs(hif_dev);
1264 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1266 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1267 struct htc_target *htc_handle = hif_dev->htc_handle;
1270 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1274 if (hif_dev->firmware) {
1275 ret = ath9k_hif_usb_download_fw(hif_dev,
1276 htc_handle->drv_priv->ah->hw_version.usbdev);
1280 ath9k_hif_usb_dealloc_urbs(hif_dev);
1286 ret = ath9k_htc_resume(htc_handle);
1294 ath9k_hif_usb_dealloc_urbs(hif_dev);
1300 static struct usb_driver ath9k_hif_usb_driver = {
1301 .name = KBUILD_MODNAME,
1302 .probe = ath9k_hif_usb_probe,
1303 .disconnect = ath9k_hif_usb_disconnect,
1305 .suspend = ath9k_hif_usb_suspend,
1306 .resume = ath9k_hif_usb_resume,
1307 .reset_resume = ath9k_hif_usb_resume,
1309 .id_table = ath9k_hif_usb_ids,
1313 int ath9k_hif_usb_init(void)
1315 return usb_register(&ath9k_hif_usb_driver);
1318 void ath9k_hif_usb_exit(void)
1320 usb_deregister(&ath9k_hif_usb_driver);