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 */
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 */
57 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
59 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
61 static void hif_usb_regout_cb(struct urb *urb)
63 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
65 switch (urb->status) {
78 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
89 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
96 urb = usb_alloc_urb(0, GFP_KERNEL);
100 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
107 cmd->hif_dev = hif_dev;
109 usb_fill_bulk_urb(urb, hif_dev->udev,
110 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
112 hif_usb_regout_cb, cmd);
114 usb_anchor_urb(urb, &hif_dev->regout_submitted);
115 ret = usb_submit_urb(urb, GFP_KERNEL);
117 usb_unanchor_urb(urb);
125 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
126 struct sk_buff_head *list)
130 while ((skb = __skb_dequeue(list)) != NULL) {
131 dev_kfree_skb_any(skb);
132 TX_STAT_INC(skb_dropped);
136 static void hif_usb_tx_cb(struct urb *urb)
138 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
139 struct hif_device_usb *hif_dev;
142 if (!tx_buf || !tx_buf->hif_dev)
145 hif_dev = tx_buf->hif_dev;
147 switch (urb->status) {
155 * The URB has been killed, free the SKBs
158 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
164 /* Check if TX has been stopped */
165 spin_lock(&hif_dev->tx.tx_lock);
166 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
167 spin_unlock(&hif_dev->tx.tx_lock);
168 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
171 spin_unlock(&hif_dev->tx.tx_lock);
173 /* Complete the queued SKBs. */
174 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
175 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
177 TX_STAT_INC(skb_completed);
181 /* Re-initialize the SKB queue */
182 tx_buf->len = tx_buf->offset = 0;
183 __skb_queue_head_init(&tx_buf->skb_queue);
185 /* Add this TX buffer to the free list */
186 spin_lock(&hif_dev->tx.tx_lock);
187 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
188 hif_dev->tx.tx_buf_cnt++;
189 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
190 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
191 TX_STAT_INC(buf_completed);
192 spin_unlock(&hif_dev->tx.tx_lock);
195 /* TX lock has to be taken */
196 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
198 struct tx_buf *tx_buf = NULL;
199 struct sk_buff *nskb = NULL;
201 u16 *hdr, tx_skb_cnt = 0;
204 if (hif_dev->tx.tx_skb_cnt == 0)
207 /* Check if a free TX buffer is available */
208 if (list_empty(&hif_dev->tx.tx_buf))
211 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
212 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
213 hif_dev->tx.tx_buf_cnt--;
215 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
217 for (i = 0; i < tx_skb_cnt; i++) {
218 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
220 /* Should never be NULL */
223 hif_dev->tx.tx_skb_cnt--;
226 buf += tx_buf->offset;
229 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
231 memcpy(buf, nskb->data, nskb->len);
232 tx_buf->len = nskb->len + 4;
234 if (i < (tx_skb_cnt - 1))
235 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
237 if (i == (tx_skb_cnt - 1))
238 tx_buf->len += tx_buf->offset;
240 __skb_queue_tail(&tx_buf->skb_queue, nskb);
241 TX_STAT_INC(skb_queued);
244 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
245 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
246 tx_buf->buf, tx_buf->len,
247 hif_usb_tx_cb, tx_buf);
249 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
251 tx_buf->len = tx_buf->offset = 0;
252 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
253 __skb_queue_head_init(&tx_buf->skb_queue);
254 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
255 hif_dev->tx.tx_buf_cnt++;
259 TX_STAT_INC(buf_queued);
264 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
265 struct ath9k_htc_tx_ctl *tx_ctl)
269 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
271 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
272 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
276 /* Check if the max queue count has been reached */
277 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
278 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
282 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
283 hif_dev->tx.tx_skb_cnt++;
285 /* Send normal frames immediately */
286 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
287 __hif_usb_tx(hif_dev);
289 /* Check if AMPDUs have to be sent immediately */
290 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
291 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
292 (hif_dev->tx.tx_skb_cnt < 2)) {
293 __hif_usb_tx(hif_dev);
296 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
301 static void hif_usb_start(void *hif_handle, u8 pipe_id)
303 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
306 hif_dev->flags |= HIF_USB_START;
308 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
309 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
310 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
313 static void hif_usb_stop(void *hif_handle, u8 pipe_id)
315 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
318 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
319 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
320 hif_dev->tx.tx_skb_cnt = 0;
321 hif_dev->tx.flags |= HIF_USB_TX_STOP;
322 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
325 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
326 struct ath9k_htc_tx_ctl *tx_ctl)
328 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
332 case USB_WLAN_TX_PIPE:
333 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
335 case USB_REG_OUT_PIPE:
336 ret = hif_usb_send_regout(hif_dev, skb);
339 dev_err(&hif_dev->udev->dev,
340 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
348 static struct ath9k_htc_hif hif_usb = {
349 .transport = ATH9K_HIF_USB,
350 .name = "ath9k_hif_usb",
352 .control_ul_pipe = USB_REG_OUT_PIPE,
353 .control_dl_pipe = USB_REG_IN_PIPE,
355 .start = hif_usb_start,
356 .stop = hif_usb_stop,
357 .send = hif_usb_send,
360 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
363 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
364 int index = 0, i = 0, len = skb->len;
365 int rx_remain_len, rx_pkt_len;
369 spin_lock(&hif_dev->rx_lock);
371 rx_remain_len = hif_dev->rx_remain_len;
372 rx_pkt_len = hif_dev->rx_transfer_len;
374 if (rx_remain_len != 0) {
375 struct sk_buff *remain_skb = hif_dev->remain_skb;
378 ptr = (u8 *) remain_skb->data;
380 index = rx_remain_len;
381 rx_remain_len -= hif_dev->rx_pad_len;
384 memcpy(ptr, skb->data, rx_remain_len);
386 rx_pkt_len += rx_remain_len;
387 hif_dev->rx_remain_len = 0;
388 skb_put(remain_skb, rx_pkt_len);
390 skb_pool[pool_index++] = remain_skb;
393 index = rx_remain_len;
397 spin_unlock(&hif_dev->rx_lock);
399 while (index < len) {
405 ptr = (u8 *) skb->data;
407 pkt_len = ptr[index] + (ptr[index+1] << 8);
408 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
410 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
411 RX_STAT_INC(skb_dropped);
415 pad_len = 4 - (pkt_len & 0x3);
420 index = index + 4 + pkt_len + pad_len;
422 if (index > MAX_RX_BUF_SIZE) {
423 spin_lock(&hif_dev->rx_lock);
424 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
425 hif_dev->rx_transfer_len =
426 MAX_RX_BUF_SIZE - chk_idx - 4;
427 hif_dev->rx_pad_len = pad_len;
429 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
431 dev_err(&hif_dev->udev->dev,
432 "ath9k_htc: RX memory allocation error\n");
433 spin_unlock(&hif_dev->rx_lock);
436 skb_reserve(nskb, 32);
437 RX_STAT_INC(skb_allocated);
439 memcpy(nskb->data, &(skb->data[chk_idx+4]),
440 hif_dev->rx_transfer_len);
442 /* Record the buffer pointer */
443 hif_dev->remain_skb = nskb;
444 spin_unlock(&hif_dev->rx_lock);
446 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
448 dev_err(&hif_dev->udev->dev,
449 "ath9k_htc: RX memory allocation error\n");
452 skb_reserve(nskb, 32);
453 RX_STAT_INC(skb_allocated);
455 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
456 skb_put(nskb, pkt_len);
457 skb_pool[pool_index++] = nskb;
462 for (i = 0; i < pool_index; i++) {
463 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
464 skb_pool[i]->len, USB_WLAN_RX_PIPE);
465 RX_STAT_INC(skb_completed);
469 static void ath9k_hif_usb_rx_cb(struct urb *urb)
471 struct sk_buff *skb = (struct sk_buff *) urb->context;
472 struct hif_device_usb *hif_dev =
473 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
482 switch (urb->status) {
494 if (likely(urb->actual_length != 0)) {
495 skb_put(skb, urb->actual_length);
496 ath9k_hif_usb_rx_stream(hif_dev, skb);
500 skb_reset_tail_pointer(skb);
503 usb_anchor_urb(urb, &hif_dev->rx_submitted);
504 ret = usb_submit_urb(urb, GFP_ATOMIC);
506 usb_unanchor_urb(urb);
515 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
517 struct sk_buff *skb = (struct sk_buff *) urb->context;
518 struct sk_buff *nskb;
519 struct hif_device_usb *hif_dev =
520 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
529 switch (urb->status) {
541 if (likely(urb->actual_length != 0)) {
542 skb_put(skb, urb->actual_length);
544 /* Process the command first */
545 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
546 skb->len, USB_REG_IN_PIPE);
549 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
551 dev_err(&hif_dev->udev->dev,
552 "ath9k_htc: REG_IN memory allocation failure\n");
557 usb_fill_bulk_urb(urb, hif_dev->udev,
558 usb_rcvbulkpipe(hif_dev->udev,
560 nskb->data, MAX_REG_IN_BUF_SIZE,
561 ath9k_hif_usb_reg_in_cb, nskb);
563 ret = usb_submit_urb(urb, GFP_ATOMIC);
573 skb_reset_tail_pointer(skb);
576 ret = usb_submit_urb(urb, GFP_ATOMIC);
586 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
588 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
590 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
591 &hif_dev->tx.tx_buf, list) {
592 usb_kill_urb(tx_buf->urb);
593 list_del(&tx_buf->list);
594 usb_free_urb(tx_buf->urb);
599 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
600 &hif_dev->tx.tx_pending, list) {
601 usb_kill_urb(tx_buf->urb);
602 list_del(&tx_buf->list);
603 usb_free_urb(tx_buf->urb);
609 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
611 struct tx_buf *tx_buf;
614 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
615 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
616 spin_lock_init(&hif_dev->tx.tx_lock);
617 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
619 for (i = 0; i < MAX_TX_URB_NUM; i++) {
620 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
624 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
628 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
632 tx_buf->hif_dev = hif_dev;
633 __skb_queue_head_init(&tx_buf->skb_queue);
635 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
638 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
646 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
650 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
652 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
655 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
657 struct urb *urb = NULL;
658 struct sk_buff *skb = NULL;
661 init_usb_anchor(&hif_dev->rx_submitted);
662 spin_lock_init(&hif_dev->rx_lock);
664 for (i = 0; i < MAX_RX_URB_NUM; i++) {
667 urb = usb_alloc_urb(0, GFP_KERNEL);
673 /* Allocate buffer */
674 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
680 usb_fill_bulk_urb(urb, hif_dev->udev,
681 usb_rcvbulkpipe(hif_dev->udev,
683 skb->data, MAX_RX_BUF_SIZE,
684 ath9k_hif_usb_rx_cb, skb);
687 usb_anchor_urb(urb, &hif_dev->rx_submitted);
690 ret = usb_submit_urb(urb, GFP_KERNEL);
692 usb_unanchor_urb(urb);
697 * Drop reference count.
698 * This ensures that the URB is freed when killing them.
710 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
714 static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
716 if (hif_dev->reg_in_urb) {
717 usb_kill_urb(hif_dev->reg_in_urb);
718 if (hif_dev->reg_in_urb->context)
719 kfree_skb((void *)hif_dev->reg_in_urb->context);
720 usb_free_urb(hif_dev->reg_in_urb);
721 hif_dev->reg_in_urb = NULL;
725 static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
729 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
730 if (hif_dev->reg_in_urb == NULL)
733 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
737 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
738 usb_rcvbulkpipe(hif_dev->udev,
740 skb->data, MAX_REG_IN_BUF_SIZE,
741 ath9k_hif_usb_reg_in_cb, skb);
743 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
749 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
753 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
756 init_usb_anchor(&hif_dev->regout_submitted);
759 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
763 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
767 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
772 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
774 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
779 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
781 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
782 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
783 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
784 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
787 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
791 const void *data = hif_dev->firmware->data;
792 size_t len = hif_dev->firmware->size;
793 u32 addr = AR9271_FIRMWARE;
794 u8 *buf = kzalloc(4096, GFP_KERNEL);
801 transfer = min_t(int, len, 4096);
802 memcpy(buf, data, transfer);
804 err = usb_control_msg(hif_dev->udev,
805 usb_sndctrlpipe(hif_dev->udev, 0),
806 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
807 addr >> 8, 0, buf, transfer, HZ);
819 if (IS_AR7010_DEVICE(drv_info))
820 firm_offset = AR7010_FIRMWARE_TEXT;
822 firm_offset = AR9271_FIRMWARE_TEXT;
825 * Issue FW download complete command to firmware.
827 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
828 FIRMWARE_DOWNLOAD_COMP,
830 firm_offset >> 8, 0, NULL, 0, HZ);
834 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
835 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
840 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
843 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
844 struct usb_endpoint_descriptor *endp;
846 /* Request firmware */
847 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
848 &hif_dev->udev->dev);
850 dev_err(&hif_dev->udev->dev,
851 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
855 /* Download firmware */
856 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
858 dev_err(&hif_dev->udev->dev,
859 "ath9k_htc: Firmware - %s download failed\n",
861 goto err_fw_download;
864 /* On downloading the firmware to the target, the USB descriptor of EP4
865 * is 'patched' to change the type of the endpoint to Bulk. This will
866 * bring down CPU usage during the scan period.
868 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
869 endp = &alt->endpoint[idx].desc;
870 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
871 == USB_ENDPOINT_XFER_INT) {
872 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
873 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
879 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
881 dev_err(&hif_dev->udev->dev,
882 "ath9k_htc: Unable to allocate URBs\n");
889 ath9k_hif_usb_dealloc_urbs(hif_dev);
891 release_firmware(hif_dev->firmware);
893 hif_dev->firmware = NULL;
897 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
899 ath9k_hif_usb_dealloc_urbs(hif_dev);
900 if (hif_dev->firmware)
901 release_firmware(hif_dev->firmware);
904 static int ath9k_hif_usb_probe(struct usb_interface *interface,
905 const struct usb_device_id *id)
907 struct usb_device *udev = interface_to_usbdev(interface);
908 struct hif_device_usb *hif_dev;
911 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
918 hif_dev->udev = udev;
919 hif_dev->interface = interface;
920 hif_dev->device_id = id->idProduct;
922 udev->reset_resume = 1;
924 usb_set_intfdata(interface, hif_dev);
926 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
927 &hif_dev->udev->dev);
928 if (hif_dev->htc_handle == NULL) {
930 goto err_htc_hw_alloc;
933 /* Find out which firmware to load */
935 if (IS_AR7010_DEVICE(id->driver_info))
936 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
937 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
939 hif_dev->fw_name = FIRMWARE_AR7010;
941 hif_dev->fw_name = FIRMWARE_AR9271;
943 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
946 goto err_hif_init_usb;
949 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
950 &hif_dev->udev->dev, hif_dev->device_id,
951 hif_dev->udev->product, id->driver_info);
954 goto err_htc_hw_init;
957 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
962 ath9k_hif_usb_dev_deinit(hif_dev);
964 ath9k_htc_hw_free(hif_dev->htc_handle);
966 usb_set_intfdata(interface, NULL);
973 static void ath9k_hif_usb_reboot(struct usb_device *udev)
975 u32 reboot_cmd = 0xffffffff;
979 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
983 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
986 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
991 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
993 struct usb_device *udev = interface_to_usbdev(interface);
994 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
997 ath9k_htc_hw_deinit(hif_dev->htc_handle,
998 (udev->state == USB_STATE_NOTATTACHED) ? true : false);
999 ath9k_htc_hw_free(hif_dev->htc_handle);
1000 ath9k_hif_usb_dev_deinit(hif_dev);
1001 usb_set_intfdata(interface, NULL);
1004 if (hif_dev->flags & HIF_USB_START)
1005 ath9k_hif_usb_reboot(udev);
1008 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1013 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1014 pm_message_t message)
1016 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1019 * The device has to be set to FULLSLEEP mode in case no
1022 if (!(hif_dev->flags & HIF_USB_START))
1023 ath9k_htc_suspend(hif_dev->htc_handle);
1025 ath9k_hif_usb_dealloc_urbs(hif_dev);
1030 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1032 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1033 struct htc_target *htc_handle = hif_dev->htc_handle;
1036 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1040 if (hif_dev->firmware) {
1041 ret = ath9k_hif_usb_download_fw(hif_dev,
1042 htc_handle->drv_priv->ah->hw_version.usbdev);
1046 ath9k_hif_usb_dealloc_urbs(hif_dev);
1052 ret = ath9k_htc_resume(htc_handle);
1060 ath9k_hif_usb_dealloc_urbs(hif_dev);
1066 static struct usb_driver ath9k_hif_usb_driver = {
1067 .name = "ath9k_hif_usb",
1068 .probe = ath9k_hif_usb_probe,
1069 .disconnect = ath9k_hif_usb_disconnect,
1071 .suspend = ath9k_hif_usb_suspend,
1072 .resume = ath9k_hif_usb_resume,
1073 .reset_resume = ath9k_hif_usb_resume,
1075 .id_table = ath9k_hif_usb_ids,
1079 int ath9k_hif_usb_init(void)
1081 return usb_register(&ath9k_hif_usb_driver);
1084 void ath9k_hif_usb_exit(void)
1086 usb_deregister(&ath9k_hif_usb_driver);