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 "ar7010.fw"
21 #define FIRMWARE_AR7010_1_1 "ar7010_1_1.fw"
22 #define FIRMWARE_AR9271 "ar9271.fw"
24 MODULE_FIRMWARE(FIRMWARE_AR7010);
25 MODULE_FIRMWARE(FIRMWARE_AR7010_1_1);
26 MODULE_FIRMWARE(FIRMWARE_AR9271);
28 static struct usb_device_id ath9k_hif_usb_ids[] = {
29 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
30 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
31 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
32 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
33 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
34 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
35 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
36 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
37 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
38 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
39 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
40 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
41 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
43 { USB_DEVICE(0x0cf3, 0x7015),
44 .driver_info = AR9287_USB }, /* Atheros */
45 { USB_DEVICE(0x1668, 0x1200),
46 .driver_info = AR9287_USB }, /* Verizon */
48 { USB_DEVICE(0x0cf3, 0x7010),
49 .driver_info = AR9280_USB }, /* Atheros */
50 { USB_DEVICE(0x0846, 0x9018),
51 .driver_info = AR9280_USB }, /* Netgear WNDA3200 */
52 { USB_DEVICE(0x083A, 0xA704),
53 .driver_info = AR9280_USB }, /* SMC Networks */
58 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
60 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
62 static void hif_usb_regout_cb(struct urb *urb)
64 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
66 switch (urb->status) {
79 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
90 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
97 urb = usb_alloc_urb(0, GFP_KERNEL);
101 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
108 cmd->hif_dev = hif_dev;
110 usb_fill_bulk_urb(urb, hif_dev->udev,
111 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
113 hif_usb_regout_cb, cmd);
115 usb_anchor_urb(urb, &hif_dev->regout_submitted);
116 ret = usb_submit_urb(urb, GFP_KERNEL);
118 usb_unanchor_urb(urb);
126 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
127 struct sk_buff_head *list)
131 while ((skb = __skb_dequeue(list)) != NULL) {
132 dev_kfree_skb_any(skb);
133 TX_STAT_INC(skb_dropped);
137 static void hif_usb_tx_cb(struct urb *urb)
139 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
140 struct hif_device_usb *hif_dev;
143 if (!tx_buf || !tx_buf->hif_dev)
146 hif_dev = tx_buf->hif_dev;
148 switch (urb->status) {
156 * The URB has been killed, free the SKBs.
158 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
161 * If the URBs are being flushed, no need to add this
162 * URB to the free list.
164 spin_lock(&hif_dev->tx.tx_lock);
165 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
166 spin_unlock(&hif_dev->tx.tx_lock);
169 spin_unlock(&hif_dev->tx.tx_lock);
172 * In the stop() case, this URB has to be added to
181 * Check if TX has been stopped, this is needed because
182 * this CB could have been invoked just after the TX lock
183 * was released in hif_stop() and kill_urb() hasn't been
186 spin_lock(&hif_dev->tx.tx_lock);
187 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
188 spin_unlock(&hif_dev->tx.tx_lock);
189 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
192 spin_unlock(&hif_dev->tx.tx_lock);
194 /* Complete the queued SKBs. */
195 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
196 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
198 TX_STAT_INC(skb_completed);
202 /* Re-initialize the SKB queue */
203 tx_buf->len = tx_buf->offset = 0;
204 __skb_queue_head_init(&tx_buf->skb_queue);
206 /* Add this TX buffer to the free list */
207 spin_lock(&hif_dev->tx.tx_lock);
208 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
209 hif_dev->tx.tx_buf_cnt++;
210 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
211 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
212 TX_STAT_INC(buf_completed);
213 spin_unlock(&hif_dev->tx.tx_lock);
216 /* TX lock has to be taken */
217 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
219 struct tx_buf *tx_buf = NULL;
220 struct sk_buff *nskb = NULL;
226 if (hif_dev->tx.tx_skb_cnt == 0)
229 /* Check if a free TX buffer is available */
230 if (list_empty(&hif_dev->tx.tx_buf))
233 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
234 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
235 hif_dev->tx.tx_buf_cnt--;
237 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
239 for (i = 0; i < tx_skb_cnt; i++) {
240 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
242 /* Should never be NULL */
245 hif_dev->tx.tx_skb_cnt--;
248 buf += tx_buf->offset;
250 *hdr++ = cpu_to_le16(nskb->len);
251 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
253 memcpy(buf, nskb->data, nskb->len);
254 tx_buf->len = nskb->len + 4;
256 if (i < (tx_skb_cnt - 1))
257 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
259 if (i == (tx_skb_cnt - 1))
260 tx_buf->len += tx_buf->offset;
262 __skb_queue_tail(&tx_buf->skb_queue, nskb);
263 TX_STAT_INC(skb_queued);
266 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
267 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
268 tx_buf->buf, tx_buf->len,
269 hif_usb_tx_cb, tx_buf);
271 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
273 tx_buf->len = tx_buf->offset = 0;
274 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
275 __skb_queue_head_init(&tx_buf->skb_queue);
276 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
277 hif_dev->tx.tx_buf_cnt++;
281 TX_STAT_INC(buf_queued);
286 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
287 struct ath9k_htc_tx_ctl *tx_ctl)
291 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
293 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
294 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
298 /* Check if the max queue count has been reached */
299 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
300 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
304 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
305 hif_dev->tx.tx_skb_cnt++;
307 /* Send normal frames immediately */
308 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
309 __hif_usb_tx(hif_dev);
311 /* Check if AMPDUs have to be sent immediately */
312 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
313 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
314 (hif_dev->tx.tx_skb_cnt < 2)) {
315 __hif_usb_tx(hif_dev);
318 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
323 static void hif_usb_start(void *hif_handle, u8 pipe_id)
325 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
328 hif_dev->flags |= HIF_USB_START;
330 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
331 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
332 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
335 static void hif_usb_stop(void *hif_handle, u8 pipe_id)
337 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
338 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
341 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
342 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
343 hif_dev->tx.tx_skb_cnt = 0;
344 hif_dev->tx.flags |= HIF_USB_TX_STOP;
345 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
347 /* The pending URBs have to be canceled. */
348 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
349 &hif_dev->tx.tx_pending, list) {
350 usb_kill_urb(tx_buf->urb);
354 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
355 struct ath9k_htc_tx_ctl *tx_ctl)
357 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
361 case USB_WLAN_TX_PIPE:
362 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
364 case USB_REG_OUT_PIPE:
365 ret = hif_usb_send_regout(hif_dev, skb);
368 dev_err(&hif_dev->udev->dev,
369 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
377 static struct ath9k_htc_hif hif_usb = {
378 .transport = ATH9K_HIF_USB,
379 .name = "ath9k_hif_usb",
381 .control_ul_pipe = USB_REG_OUT_PIPE,
382 .control_dl_pipe = USB_REG_IN_PIPE,
384 .start = hif_usb_start,
385 .stop = hif_usb_stop,
386 .send = hif_usb_send,
389 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
392 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
393 int index = 0, i = 0, len = skb->len;
394 int rx_remain_len, rx_pkt_len;
398 spin_lock(&hif_dev->rx_lock);
400 rx_remain_len = hif_dev->rx_remain_len;
401 rx_pkt_len = hif_dev->rx_transfer_len;
403 if (rx_remain_len != 0) {
404 struct sk_buff *remain_skb = hif_dev->remain_skb;
407 ptr = (u8 *) remain_skb->data;
409 index = rx_remain_len;
410 rx_remain_len -= hif_dev->rx_pad_len;
413 memcpy(ptr, skb->data, rx_remain_len);
415 rx_pkt_len += rx_remain_len;
416 hif_dev->rx_remain_len = 0;
417 skb_put(remain_skb, rx_pkt_len);
419 skb_pool[pool_index++] = remain_skb;
422 index = rx_remain_len;
426 spin_unlock(&hif_dev->rx_lock);
428 while (index < len) {
434 ptr = (u8 *) skb->data;
436 pkt_len = ptr[index] + (ptr[index+1] << 8);
437 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
439 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
440 RX_STAT_INC(skb_dropped);
444 pad_len = 4 - (pkt_len & 0x3);
449 index = index + 4 + pkt_len + pad_len;
451 if (index > MAX_RX_BUF_SIZE) {
452 spin_lock(&hif_dev->rx_lock);
453 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
454 hif_dev->rx_transfer_len =
455 MAX_RX_BUF_SIZE - chk_idx - 4;
456 hif_dev->rx_pad_len = pad_len;
458 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
460 dev_err(&hif_dev->udev->dev,
461 "ath9k_htc: RX memory allocation error\n");
462 spin_unlock(&hif_dev->rx_lock);
465 skb_reserve(nskb, 32);
466 RX_STAT_INC(skb_allocated);
468 memcpy(nskb->data, &(skb->data[chk_idx+4]),
469 hif_dev->rx_transfer_len);
471 /* Record the buffer pointer */
472 hif_dev->remain_skb = nskb;
473 spin_unlock(&hif_dev->rx_lock);
475 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
477 dev_err(&hif_dev->udev->dev,
478 "ath9k_htc: RX memory allocation error\n");
481 skb_reserve(nskb, 32);
482 RX_STAT_INC(skb_allocated);
484 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
485 skb_put(nskb, pkt_len);
486 skb_pool[pool_index++] = nskb;
491 for (i = 0; i < pool_index; i++) {
492 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
493 skb_pool[i]->len, USB_WLAN_RX_PIPE);
494 RX_STAT_INC(skb_completed);
498 static void ath9k_hif_usb_rx_cb(struct urb *urb)
500 struct sk_buff *skb = (struct sk_buff *) urb->context;
501 struct hif_device_usb *hif_dev =
502 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
511 switch (urb->status) {
523 if (likely(urb->actual_length != 0)) {
524 skb_put(skb, urb->actual_length);
525 ath9k_hif_usb_rx_stream(hif_dev, skb);
529 skb_reset_tail_pointer(skb);
532 usb_anchor_urb(urb, &hif_dev->rx_submitted);
533 ret = usb_submit_urb(urb, GFP_ATOMIC);
535 usb_unanchor_urb(urb);
544 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
546 struct sk_buff *skb = (struct sk_buff *) urb->context;
547 struct sk_buff *nskb;
548 struct hif_device_usb *hif_dev =
549 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
558 switch (urb->status) {
570 if (likely(urb->actual_length != 0)) {
571 skb_put(skb, urb->actual_length);
573 /* Process the command first */
574 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
575 skb->len, USB_REG_IN_PIPE);
578 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
580 dev_err(&hif_dev->udev->dev,
581 "ath9k_htc: REG_IN memory allocation failure\n");
586 usb_fill_bulk_urb(urb, hif_dev->udev,
587 usb_rcvbulkpipe(hif_dev->udev,
589 nskb->data, MAX_REG_IN_BUF_SIZE,
590 ath9k_hif_usb_reg_in_cb, nskb);
592 ret = usb_submit_urb(urb, GFP_ATOMIC);
602 skb_reset_tail_pointer(skb);
605 ret = usb_submit_urb(urb, GFP_ATOMIC);
615 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
617 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
620 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
621 &hif_dev->tx.tx_buf, list) {
622 usb_kill_urb(tx_buf->urb);
623 list_del(&tx_buf->list);
624 usb_free_urb(tx_buf->urb);
629 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
630 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
631 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
633 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
634 &hif_dev->tx.tx_pending, list) {
635 usb_kill_urb(tx_buf->urb);
636 list_del(&tx_buf->list);
637 usb_free_urb(tx_buf->urb);
643 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
645 struct tx_buf *tx_buf;
648 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
649 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
650 spin_lock_init(&hif_dev->tx.tx_lock);
651 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
653 for (i = 0; i < MAX_TX_URB_NUM; i++) {
654 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
658 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
662 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
666 tx_buf->hif_dev = hif_dev;
667 __skb_queue_head_init(&tx_buf->skb_queue);
669 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
672 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
680 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
684 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
686 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
689 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
691 struct urb *urb = NULL;
692 struct sk_buff *skb = NULL;
695 init_usb_anchor(&hif_dev->rx_submitted);
696 spin_lock_init(&hif_dev->rx_lock);
698 for (i = 0; i < MAX_RX_URB_NUM; i++) {
701 urb = usb_alloc_urb(0, GFP_KERNEL);
707 /* Allocate buffer */
708 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
714 usb_fill_bulk_urb(urb, hif_dev->udev,
715 usb_rcvbulkpipe(hif_dev->udev,
717 skb->data, MAX_RX_BUF_SIZE,
718 ath9k_hif_usb_rx_cb, skb);
721 usb_anchor_urb(urb, &hif_dev->rx_submitted);
724 ret = usb_submit_urb(urb, GFP_KERNEL);
726 usb_unanchor_urb(urb);
731 * Drop reference count.
732 * This ensures that the URB is freed when killing them.
744 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
748 static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
750 if (hif_dev->reg_in_urb) {
751 usb_kill_urb(hif_dev->reg_in_urb);
752 if (hif_dev->reg_in_urb->context)
753 kfree_skb((void *)hif_dev->reg_in_urb->context);
754 usb_free_urb(hif_dev->reg_in_urb);
755 hif_dev->reg_in_urb = NULL;
759 static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
763 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
764 if (hif_dev->reg_in_urb == NULL)
767 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
771 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
772 usb_rcvbulkpipe(hif_dev->udev,
774 skb->data, MAX_REG_IN_BUF_SIZE,
775 ath9k_hif_usb_reg_in_cb, skb);
777 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
783 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
787 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
790 init_usb_anchor(&hif_dev->regout_submitted);
793 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
797 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
801 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
806 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
808 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
813 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
815 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
816 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
817 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
818 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
821 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
825 const void *data = hif_dev->firmware->data;
826 size_t len = hif_dev->firmware->size;
827 u32 addr = AR9271_FIRMWARE;
828 u8 *buf = kzalloc(4096, GFP_KERNEL);
835 transfer = min_t(int, len, 4096);
836 memcpy(buf, data, transfer);
838 err = usb_control_msg(hif_dev->udev,
839 usb_sndctrlpipe(hif_dev->udev, 0),
840 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
841 addr >> 8, 0, buf, transfer, HZ);
853 if (IS_AR7010_DEVICE(drv_info))
854 firm_offset = AR7010_FIRMWARE_TEXT;
856 firm_offset = AR9271_FIRMWARE_TEXT;
859 * Issue FW download complete command to firmware.
861 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
862 FIRMWARE_DOWNLOAD_COMP,
864 firm_offset >> 8, 0, NULL, 0, HZ);
868 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
869 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
874 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
877 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
878 struct usb_endpoint_descriptor *endp;
880 /* Request firmware */
881 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
882 &hif_dev->udev->dev);
884 dev_err(&hif_dev->udev->dev,
885 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
889 /* Download firmware */
890 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
892 dev_err(&hif_dev->udev->dev,
893 "ath9k_htc: Firmware - %s download failed\n",
895 goto err_fw_download;
898 /* On downloading the firmware to the target, the USB descriptor of EP4
899 * is 'patched' to change the type of the endpoint to Bulk. This will
900 * bring down CPU usage during the scan period.
902 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
903 endp = &alt->endpoint[idx].desc;
904 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
905 == USB_ENDPOINT_XFER_INT) {
906 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
907 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
913 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
915 dev_err(&hif_dev->udev->dev,
916 "ath9k_htc: Unable to allocate URBs\n");
923 ath9k_hif_usb_dealloc_urbs(hif_dev);
925 release_firmware(hif_dev->firmware);
927 hif_dev->firmware = NULL;
931 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
933 ath9k_hif_usb_dealloc_urbs(hif_dev);
934 if (hif_dev->firmware)
935 release_firmware(hif_dev->firmware);
938 static int ath9k_hif_usb_probe(struct usb_interface *interface,
939 const struct usb_device_id *id)
941 struct usb_device *udev = interface_to_usbdev(interface);
942 struct hif_device_usb *hif_dev;
945 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
952 hif_dev->udev = udev;
953 hif_dev->interface = interface;
954 hif_dev->device_id = id->idProduct;
956 udev->reset_resume = 1;
958 usb_set_intfdata(interface, hif_dev);
960 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
961 &hif_dev->udev->dev);
962 if (hif_dev->htc_handle == NULL) {
964 goto err_htc_hw_alloc;
967 /* Find out which firmware to load */
969 if (IS_AR7010_DEVICE(id->driver_info))
970 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
971 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
973 hif_dev->fw_name = FIRMWARE_AR7010;
975 hif_dev->fw_name = FIRMWARE_AR9271;
977 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
980 goto err_hif_init_usb;
983 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
984 &hif_dev->udev->dev, hif_dev->device_id,
985 hif_dev->udev->product, id->driver_info);
988 goto err_htc_hw_init;
991 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
996 ath9k_hif_usb_dev_deinit(hif_dev);
998 ath9k_htc_hw_free(hif_dev->htc_handle);
1000 usb_set_intfdata(interface, NULL);
1007 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1009 u32 reboot_cmd = 0xffffffff;
1013 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1017 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1020 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1025 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1027 struct usb_device *udev = interface_to_usbdev(interface);
1028 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1029 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1032 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1033 ath9k_htc_hw_free(hif_dev->htc_handle);
1034 ath9k_hif_usb_dev_deinit(hif_dev);
1035 usb_set_intfdata(interface, NULL);
1038 if (!unplugged && (hif_dev->flags & HIF_USB_START))
1039 ath9k_hif_usb_reboot(udev);
1042 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1047 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1048 pm_message_t message)
1050 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1053 * The device has to be set to FULLSLEEP mode in case no
1056 if (!(hif_dev->flags & HIF_USB_START))
1057 ath9k_htc_suspend(hif_dev->htc_handle);
1059 ath9k_hif_usb_dealloc_urbs(hif_dev);
1064 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1066 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1067 struct htc_target *htc_handle = hif_dev->htc_handle;
1070 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1074 if (hif_dev->firmware) {
1075 ret = ath9k_hif_usb_download_fw(hif_dev,
1076 htc_handle->drv_priv->ah->hw_version.usbdev);
1080 ath9k_hif_usb_dealloc_urbs(hif_dev);
1086 ret = ath9k_htc_resume(htc_handle);
1094 ath9k_hif_usb_dealloc_urbs(hif_dev);
1100 static struct usb_driver ath9k_hif_usb_driver = {
1101 .name = "ath9k_hif_usb",
1102 .probe = ath9k_hif_usb_probe,
1103 .disconnect = ath9k_hif_usb_disconnect,
1105 .suspend = ath9k_hif_usb_suspend,
1106 .resume = ath9k_hif_usb_resume,
1107 .reset_resume = ath9k_hif_usb_resume,
1109 .id_table = ath9k_hif_usb_ids,
1113 int ath9k_hif_usb_init(void)
1115 return usb_register(&ath9k_hif_usb_driver);
1118 void ath9k_hif_usb_exit(void)
1120 usb_deregister(&ath9k_hif_usb_driver);