2 * Copyright (c) 2010 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 */
53 { USB_DEVICE(0x0cf3, 0x20ff),
54 .driver_info = STORAGE_DEVICE },
59 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
61 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
63 static void hif_usb_regout_cb(struct urb *urb)
65 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
67 switch (urb->status) {
80 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
91 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
98 urb = usb_alloc_urb(0, GFP_KERNEL);
102 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
109 cmd->hif_dev = hif_dev;
111 usb_fill_bulk_urb(urb, hif_dev->udev,
112 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
114 hif_usb_regout_cb, cmd);
116 usb_anchor_urb(urb, &hif_dev->regout_submitted);
117 ret = usb_submit_urb(urb, GFP_KERNEL);
119 usb_unanchor_urb(urb);
127 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
128 struct sk_buff_head *list)
132 while ((skb = __skb_dequeue(list)) != NULL) {
133 dev_kfree_skb_any(skb);
137 static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
138 struct sk_buff_head *queue,
143 while ((skb = __skb_dequeue(queue)) != NULL) {
144 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
146 (txok) ? TX_STAT_INC(skb_success) : TX_STAT_INC(skb_failed);
150 static void hif_usb_tx_cb(struct urb *urb)
152 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
153 struct hif_device_usb *hif_dev;
156 if (!tx_buf || !tx_buf->hif_dev)
159 hif_dev = tx_buf->hif_dev;
161 switch (urb->status) {
171 * If the URBs are being flushed, no need to add this
172 * URB to the free list.
174 spin_lock(&hif_dev->tx.tx_lock);
175 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
176 spin_unlock(&hif_dev->tx.tx_lock);
177 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
180 spin_unlock(&hif_dev->tx.tx_lock);
188 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
190 /* Re-initialize the SKB queue */
191 tx_buf->len = tx_buf->offset = 0;
192 __skb_queue_head_init(&tx_buf->skb_queue);
194 /* Add this TX buffer to the free list */
195 spin_lock(&hif_dev->tx.tx_lock);
196 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
197 hif_dev->tx.tx_buf_cnt++;
198 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
199 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
200 TX_STAT_INC(buf_completed);
201 spin_unlock(&hif_dev->tx.tx_lock);
204 /* TX lock has to be taken */
205 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
207 struct tx_buf *tx_buf = NULL;
208 struct sk_buff *nskb = NULL;
214 if (hif_dev->tx.tx_skb_cnt == 0)
217 /* Check if a free TX buffer is available */
218 if (list_empty(&hif_dev->tx.tx_buf))
221 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
222 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
223 hif_dev->tx.tx_buf_cnt--;
225 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
227 for (i = 0; i < tx_skb_cnt; i++) {
228 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
230 /* Should never be NULL */
233 hif_dev->tx.tx_skb_cnt--;
236 buf += tx_buf->offset;
238 *hdr++ = cpu_to_le16(nskb->len);
239 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
241 memcpy(buf, nskb->data, nskb->len);
242 tx_buf->len = nskb->len + 4;
244 if (i < (tx_skb_cnt - 1))
245 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
247 if (i == (tx_skb_cnt - 1))
248 tx_buf->len += tx_buf->offset;
250 __skb_queue_tail(&tx_buf->skb_queue, nskb);
251 TX_STAT_INC(skb_queued);
254 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
255 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
256 tx_buf->buf, tx_buf->len,
257 hif_usb_tx_cb, tx_buf);
259 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
261 tx_buf->len = tx_buf->offset = 0;
262 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
263 __skb_queue_head_init(&tx_buf->skb_queue);
264 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
265 hif_dev->tx.tx_buf_cnt++;
269 TX_STAT_INC(buf_queued);
274 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
276 struct ath9k_htc_tx_ctl *tx_ctl;
279 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
281 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
282 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
286 /* Check if the max queue count has been reached */
287 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
288 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
292 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
293 hif_dev->tx.tx_skb_cnt++;
295 tx_ctl = HTC_SKB_CB(skb);
297 /* Send normal/mgmt/beacon frames immediately */
298 if (tx_ctl->type != ATH9K_HTC_AMPDU)
299 __hif_usb_tx(hif_dev);
301 /* Check if AMPDUs have to be sent immediately */
302 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
303 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
304 (hif_dev->tx.tx_skb_cnt < 2)) {
305 __hif_usb_tx(hif_dev);
308 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
313 static void hif_usb_start(void *hif_handle)
315 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
318 hif_dev->flags |= HIF_USB_START;
320 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
321 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
322 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
325 static void hif_usb_stop(void *hif_handle)
327 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
328 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
331 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
332 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
333 hif_dev->tx.tx_skb_cnt = 0;
334 hif_dev->tx.flags |= HIF_USB_TX_STOP;
335 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
337 /* The pending URBs have to be canceled. */
338 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
339 &hif_dev->tx.tx_pending, list) {
340 usb_kill_urb(tx_buf->urb);
344 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
346 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
350 case USB_WLAN_TX_PIPE:
351 ret = hif_usb_send_tx(hif_dev, skb);
353 case USB_REG_OUT_PIPE:
354 ret = hif_usb_send_regout(hif_dev, skb);
357 dev_err(&hif_dev->udev->dev,
358 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
366 static struct ath9k_htc_hif hif_usb = {
367 .transport = ATH9K_HIF_USB,
368 .name = "ath9k_hif_usb",
370 .control_ul_pipe = USB_REG_OUT_PIPE,
371 .control_dl_pipe = USB_REG_IN_PIPE,
373 .start = hif_usb_start,
374 .stop = hif_usb_stop,
375 .send = hif_usb_send,
378 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
381 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
382 int index = 0, i = 0, len = skb->len;
383 int rx_remain_len, rx_pkt_len;
387 spin_lock(&hif_dev->rx_lock);
389 rx_remain_len = hif_dev->rx_remain_len;
390 rx_pkt_len = hif_dev->rx_transfer_len;
392 if (rx_remain_len != 0) {
393 struct sk_buff *remain_skb = hif_dev->remain_skb;
396 ptr = (u8 *) remain_skb->data;
398 index = rx_remain_len;
399 rx_remain_len -= hif_dev->rx_pad_len;
402 memcpy(ptr, skb->data, rx_remain_len);
404 rx_pkt_len += rx_remain_len;
405 hif_dev->rx_remain_len = 0;
406 skb_put(remain_skb, rx_pkt_len);
408 skb_pool[pool_index++] = remain_skb;
411 index = rx_remain_len;
415 spin_unlock(&hif_dev->rx_lock);
417 while (index < len) {
423 ptr = (u8 *) skb->data;
425 pkt_len = ptr[index] + (ptr[index+1] << 8);
426 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
428 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
429 RX_STAT_INC(skb_dropped);
433 pad_len = 4 - (pkt_len & 0x3);
438 index = index + 4 + pkt_len + pad_len;
440 if (index > MAX_RX_BUF_SIZE) {
441 spin_lock(&hif_dev->rx_lock);
442 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
443 hif_dev->rx_transfer_len =
444 MAX_RX_BUF_SIZE - chk_idx - 4;
445 hif_dev->rx_pad_len = pad_len;
447 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
449 dev_err(&hif_dev->udev->dev,
450 "ath9k_htc: RX memory allocation error\n");
451 spin_unlock(&hif_dev->rx_lock);
454 skb_reserve(nskb, 32);
455 RX_STAT_INC(skb_allocated);
457 memcpy(nskb->data, &(skb->data[chk_idx+4]),
458 hif_dev->rx_transfer_len);
460 /* Record the buffer pointer */
461 hif_dev->remain_skb = nskb;
462 spin_unlock(&hif_dev->rx_lock);
464 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
466 dev_err(&hif_dev->udev->dev,
467 "ath9k_htc: RX memory allocation error\n");
470 skb_reserve(nskb, 32);
471 RX_STAT_INC(skb_allocated);
473 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
474 skb_put(nskb, pkt_len);
475 skb_pool[pool_index++] = nskb;
480 for (i = 0; i < pool_index; i++) {
481 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
482 skb_pool[i]->len, USB_WLAN_RX_PIPE);
483 RX_STAT_INC(skb_completed);
487 static void ath9k_hif_usb_rx_cb(struct urb *urb)
489 struct sk_buff *skb = (struct sk_buff *) urb->context;
490 struct hif_device_usb *hif_dev =
491 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
500 switch (urb->status) {
512 if (likely(urb->actual_length != 0)) {
513 skb_put(skb, urb->actual_length);
514 ath9k_hif_usb_rx_stream(hif_dev, skb);
518 skb_reset_tail_pointer(skb);
521 usb_anchor_urb(urb, &hif_dev->rx_submitted);
522 ret = usb_submit_urb(urb, GFP_ATOMIC);
524 usb_unanchor_urb(urb);
533 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
535 struct sk_buff *skb = (struct sk_buff *) urb->context;
536 struct sk_buff *nskb;
537 struct hif_device_usb *hif_dev =
538 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
547 switch (urb->status) {
556 skb_reset_tail_pointer(skb);
562 if (likely(urb->actual_length != 0)) {
563 skb_put(skb, urb->actual_length);
565 /* Process the command first */
566 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
567 skb->len, USB_REG_IN_PIPE);
570 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
572 dev_err(&hif_dev->udev->dev,
573 "ath9k_htc: REG_IN memory allocation failure\n");
578 usb_fill_bulk_urb(urb, hif_dev->udev,
579 usb_rcvbulkpipe(hif_dev->udev,
581 nskb->data, MAX_REG_IN_BUF_SIZE,
582 ath9k_hif_usb_reg_in_cb, nskb);
586 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
587 ret = usb_submit_urb(urb, GFP_ATOMIC);
589 usb_unanchor_urb(urb);
599 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
601 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
604 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
605 &hif_dev->tx.tx_buf, list) {
606 usb_kill_urb(tx_buf->urb);
607 list_del(&tx_buf->list);
608 usb_free_urb(tx_buf->urb);
613 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
614 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
615 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
617 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
618 &hif_dev->tx.tx_pending, list) {
619 usb_kill_urb(tx_buf->urb);
620 list_del(&tx_buf->list);
621 usb_free_urb(tx_buf->urb);
627 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
629 struct tx_buf *tx_buf;
632 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
633 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
634 spin_lock_init(&hif_dev->tx.tx_lock);
635 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
637 for (i = 0; i < MAX_TX_URB_NUM; i++) {
638 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
642 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
646 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
650 tx_buf->hif_dev = hif_dev;
651 __skb_queue_head_init(&tx_buf->skb_queue);
653 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
656 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
664 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
668 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
670 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
673 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
675 struct urb *urb = NULL;
676 struct sk_buff *skb = NULL;
679 init_usb_anchor(&hif_dev->rx_submitted);
680 spin_lock_init(&hif_dev->rx_lock);
682 for (i = 0; i < MAX_RX_URB_NUM; i++) {
685 urb = usb_alloc_urb(0, GFP_KERNEL);
691 /* Allocate buffer */
692 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
698 usb_fill_bulk_urb(urb, hif_dev->udev,
699 usb_rcvbulkpipe(hif_dev->udev,
701 skb->data, MAX_RX_BUF_SIZE,
702 ath9k_hif_usb_rx_cb, skb);
705 usb_anchor_urb(urb, &hif_dev->rx_submitted);
708 ret = usb_submit_urb(urb, GFP_KERNEL);
710 usb_unanchor_urb(urb);
715 * Drop reference count.
716 * This ensures that the URB is freed when killing them.
728 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
732 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
734 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
737 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
739 struct urb *urb = NULL;
740 struct sk_buff *skb = NULL;
743 init_usb_anchor(&hif_dev->reg_in_submitted);
745 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
748 urb = usb_alloc_urb(0, GFP_KERNEL);
754 /* Allocate buffer */
755 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
761 usb_fill_bulk_urb(urb, hif_dev->udev,
762 usb_rcvbulkpipe(hif_dev->udev,
764 skb->data, MAX_REG_IN_BUF_SIZE,
765 ath9k_hif_usb_reg_in_cb, skb);
768 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
771 ret = usb_submit_urb(urb, GFP_KERNEL);
773 usb_unanchor_urb(urb);
778 * Drop reference count.
779 * This ensures that the URB is freed when killing them.
791 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
795 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
798 init_usb_anchor(&hif_dev->regout_submitted);
801 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
805 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
809 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
814 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
816 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
821 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
823 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
824 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
825 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
826 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
829 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
833 const void *data = hif_dev->firmware->data;
834 size_t len = hif_dev->firmware->size;
835 u32 addr = AR9271_FIRMWARE;
836 u8 *buf = kzalloc(4096, GFP_KERNEL);
843 transfer = min_t(int, len, 4096);
844 memcpy(buf, data, transfer);
846 err = usb_control_msg(hif_dev->udev,
847 usb_sndctrlpipe(hif_dev->udev, 0),
848 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
849 addr >> 8, 0, buf, transfer, HZ);
861 if (IS_AR7010_DEVICE(drv_info))
862 firm_offset = AR7010_FIRMWARE_TEXT;
864 firm_offset = AR9271_FIRMWARE_TEXT;
867 * Issue FW download complete command to firmware.
869 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
870 FIRMWARE_DOWNLOAD_COMP,
872 firm_offset >> 8, 0, NULL, 0, HZ);
876 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
877 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
882 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
885 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
886 struct usb_endpoint_descriptor *endp;
888 /* Request firmware */
889 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
890 &hif_dev->udev->dev);
892 dev_err(&hif_dev->udev->dev,
893 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
897 /* Download firmware */
898 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
900 dev_err(&hif_dev->udev->dev,
901 "ath9k_htc: Firmware - %s download failed\n",
903 goto err_fw_download;
906 /* On downloading the firmware to the target, the USB descriptor of EP4
907 * is 'patched' to change the type of the endpoint to Bulk. This will
908 * bring down CPU usage during the scan period.
910 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
911 endp = &alt->endpoint[idx].desc;
912 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
913 == USB_ENDPOINT_XFER_INT) {
914 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
915 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
921 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
923 dev_err(&hif_dev->udev->dev,
924 "ath9k_htc: Unable to allocate URBs\n");
925 goto err_fw_download;
931 release_firmware(hif_dev->firmware);
933 hif_dev->firmware = NULL;
937 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
939 ath9k_hif_usb_dealloc_urbs(hif_dev);
940 if (hif_dev->firmware)
941 release_firmware(hif_dev->firmware);
945 * An exact copy of the function from zd1211rw.
947 static int send_eject_command(struct usb_interface *interface)
949 struct usb_device *udev = interface_to_usbdev(interface);
950 struct usb_host_interface *iface_desc = &interface->altsetting[0];
951 struct usb_endpoint_descriptor *endpoint;
956 /* Find bulk out endpoint */
957 for (r = 1; r >= 0; r--) {
958 endpoint = &iface_desc->endpoint[r].desc;
959 if (usb_endpoint_dir_out(endpoint) &&
960 usb_endpoint_xfer_bulk(endpoint)) {
961 bulk_out_ep = endpoint->bEndpointAddress;
967 "ath9k_htc: Could not find bulk out endpoint\n");
971 cmd = kzalloc(31, GFP_KERNEL);
975 /* USB bulk command block */
976 cmd[0] = 0x55; /* bulk command signature */
977 cmd[1] = 0x53; /* bulk command signature */
978 cmd[2] = 0x42; /* bulk command signature */
979 cmd[3] = 0x43; /* bulk command signature */
980 cmd[14] = 6; /* command length */
982 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
983 cmd[19] = 0x2; /* eject disc */
985 dev_info(&udev->dev, "Ejecting storage device...\n");
986 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
987 cmd, 31, NULL, 2000);
992 /* At this point, the device disconnects and reconnects with the real
995 usb_set_intfdata(interface, NULL);
999 static int ath9k_hif_usb_probe(struct usb_interface *interface,
1000 const struct usb_device_id *id)
1002 struct usb_device *udev = interface_to_usbdev(interface);
1003 struct hif_device_usb *hif_dev;
1006 if (id->driver_info == STORAGE_DEVICE)
1007 return send_eject_command(interface);
1009 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1016 hif_dev->udev = udev;
1017 hif_dev->interface = interface;
1018 hif_dev->device_id = id->idProduct;
1020 udev->reset_resume = 1;
1022 usb_set_intfdata(interface, hif_dev);
1024 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1025 &hif_dev->udev->dev);
1026 if (hif_dev->htc_handle == NULL) {
1028 goto err_htc_hw_alloc;
1031 /* Find out which firmware to load */
1033 if (IS_AR7010_DEVICE(id->driver_info))
1034 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
1036 hif_dev->fw_name = FIRMWARE_AR9271;
1038 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
1041 goto err_hif_init_usb;
1044 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1045 &hif_dev->udev->dev, hif_dev->device_id,
1046 hif_dev->udev->product, id->driver_info);
1049 goto err_htc_hw_init;
1052 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1057 ath9k_hif_usb_dev_deinit(hif_dev);
1059 ath9k_htc_hw_free(hif_dev->htc_handle);
1061 usb_set_intfdata(interface, NULL);
1068 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1070 u32 reboot_cmd = 0xffffffff;
1074 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1078 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1081 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1086 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1088 struct usb_device *udev = interface_to_usbdev(interface);
1089 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1090 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1095 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1096 ath9k_htc_hw_free(hif_dev->htc_handle);
1097 ath9k_hif_usb_dev_deinit(hif_dev);
1098 usb_set_intfdata(interface, NULL);
1100 if (!unplugged && (hif_dev->flags & HIF_USB_START))
1101 ath9k_hif_usb_reboot(udev);
1104 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1109 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1110 pm_message_t message)
1112 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1115 * The device has to be set to FULLSLEEP mode in case no
1118 if (!(hif_dev->flags & HIF_USB_START))
1119 ath9k_htc_suspend(hif_dev->htc_handle);
1121 ath9k_hif_usb_dealloc_urbs(hif_dev);
1126 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1128 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1129 struct htc_target *htc_handle = hif_dev->htc_handle;
1132 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1136 if (hif_dev->firmware) {
1137 ret = ath9k_hif_usb_download_fw(hif_dev,
1138 htc_handle->drv_priv->ah->hw_version.usbdev);
1142 ath9k_hif_usb_dealloc_urbs(hif_dev);
1148 ret = ath9k_htc_resume(htc_handle);
1156 ath9k_hif_usb_dealloc_urbs(hif_dev);
1162 static struct usb_driver ath9k_hif_usb_driver = {
1163 .name = "ath9k_hif_usb",
1164 .probe = ath9k_hif_usb_probe,
1165 .disconnect = ath9k_hif_usb_disconnect,
1167 .suspend = ath9k_hif_usb_suspend,
1168 .resume = ath9k_hif_usb_resume,
1169 .reset_resume = ath9k_hif_usb_resume,
1171 .id_table = ath9k_hif_usb_ids,
1175 int ath9k_hif_usb_init(void)
1177 return usb_register(&ath9k_hif_usb_driver);
1180 void ath9k_hif_usb_exit(void)
1182 usb_deregister(&ath9k_hif_usb_driver);