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(0x0cf3, 0x7010),
32 .driver_info = AR7010_DEVICE },
34 { USB_DEVICE(0x0cf3, 0x7015),
35 .driver_info = AR7010_DEVICE | AR9287_DEVICE },
37 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
38 { USB_DEVICE(0x0846, 0x9018),
39 .driver_info = AR7010_DEVICE },
40 /* Netgear WNDA3200 */
41 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
42 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
43 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
44 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
45 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
46 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
47 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
48 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
49 { USB_DEVICE(0x083A, 0xA704),
50 .driver_info = AR7010_DEVICE },
52 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
53 { USB_DEVICE(0x1668, 0x1200),
54 .driver_info = AR7010_DEVICE | AR9287_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);
134 TX_STAT_INC(skb_dropped);
138 static void hif_usb_tx_cb(struct urb *urb)
140 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
141 struct hif_device_usb *hif_dev;
144 if (!tx_buf || !tx_buf->hif_dev)
147 hif_dev = tx_buf->hif_dev;
149 switch (urb->status) {
157 * The URB has been killed, free the SKBs
160 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
166 /* Check if TX has been stopped */
167 spin_lock(&hif_dev->tx.tx_lock);
168 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
169 spin_unlock(&hif_dev->tx.tx_lock);
170 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
173 spin_unlock(&hif_dev->tx.tx_lock);
175 /* Complete the queued SKBs. */
176 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
177 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
179 TX_STAT_INC(skb_completed);
183 /* Re-initialize the SKB queue */
184 tx_buf->len = tx_buf->offset = 0;
185 __skb_queue_head_init(&tx_buf->skb_queue);
187 /* Add this TX buffer to the free list */
188 spin_lock(&hif_dev->tx.tx_lock);
189 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
190 hif_dev->tx.tx_buf_cnt++;
191 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
192 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
193 TX_STAT_INC(buf_completed);
194 spin_unlock(&hif_dev->tx.tx_lock);
197 /* TX lock has to be taken */
198 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
200 struct tx_buf *tx_buf = NULL;
201 struct sk_buff *nskb = NULL;
203 u16 *hdr, tx_skb_cnt = 0;
206 if (hif_dev->tx.tx_skb_cnt == 0)
209 /* Check if a free TX buffer is available */
210 if (list_empty(&hif_dev->tx.tx_buf))
213 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
214 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
215 hif_dev->tx.tx_buf_cnt--;
217 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
219 for (i = 0; i < tx_skb_cnt; i++) {
220 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
222 /* Should never be NULL */
225 hif_dev->tx.tx_skb_cnt--;
228 buf += tx_buf->offset;
231 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
233 memcpy(buf, nskb->data, nskb->len);
234 tx_buf->len = nskb->len + 4;
236 if (i < (tx_skb_cnt - 1))
237 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
239 if (i == (tx_skb_cnt - 1))
240 tx_buf->len += tx_buf->offset;
242 __skb_queue_tail(&tx_buf->skb_queue, nskb);
243 TX_STAT_INC(skb_queued);
246 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
247 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
248 tx_buf->buf, tx_buf->len,
249 hif_usb_tx_cb, tx_buf);
251 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
253 tx_buf->len = tx_buf->offset = 0;
254 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
255 __skb_queue_head_init(&tx_buf->skb_queue);
256 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
257 hif_dev->tx.tx_buf_cnt++;
261 TX_STAT_INC(buf_queued);
266 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
267 struct ath9k_htc_tx_ctl *tx_ctl)
271 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
273 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
274 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
278 /* Check if the max queue count has been reached */
279 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
280 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
284 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
285 hif_dev->tx.tx_skb_cnt++;
287 /* Send normal frames immediately */
288 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
289 __hif_usb_tx(hif_dev);
291 /* Check if AMPDUs have to be sent immediately */
292 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
293 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
294 (hif_dev->tx.tx_skb_cnt < 2)) {
295 __hif_usb_tx(hif_dev);
298 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
303 static void hif_usb_start(void *hif_handle, u8 pipe_id)
305 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
308 hif_dev->flags |= HIF_USB_START;
310 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
311 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
312 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
315 static void hif_usb_stop(void *hif_handle, u8 pipe_id)
317 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
320 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
321 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
322 hif_dev->tx.tx_skb_cnt = 0;
323 hif_dev->tx.flags |= HIF_USB_TX_STOP;
324 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
327 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
328 struct ath9k_htc_tx_ctl *tx_ctl)
330 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
334 case USB_WLAN_TX_PIPE:
335 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
337 case USB_REG_OUT_PIPE:
338 ret = hif_usb_send_regout(hif_dev, skb);
341 dev_err(&hif_dev->udev->dev,
342 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
350 static struct ath9k_htc_hif hif_usb = {
351 .transport = ATH9K_HIF_USB,
352 .name = "ath9k_hif_usb",
354 .control_ul_pipe = USB_REG_OUT_PIPE,
355 .control_dl_pipe = USB_REG_IN_PIPE,
357 .start = hif_usb_start,
358 .stop = hif_usb_stop,
359 .send = hif_usb_send,
362 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
365 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
366 int index = 0, i = 0, len = skb->len;
367 int rx_remain_len, rx_pkt_len;
371 spin_lock(&hif_dev->rx_lock);
373 rx_remain_len = hif_dev->rx_remain_len;
374 rx_pkt_len = hif_dev->rx_transfer_len;
376 if (rx_remain_len != 0) {
377 struct sk_buff *remain_skb = hif_dev->remain_skb;
380 ptr = (u8 *) remain_skb->data;
382 index = rx_remain_len;
383 rx_remain_len -= hif_dev->rx_pad_len;
386 memcpy(ptr, skb->data, rx_remain_len);
388 rx_pkt_len += rx_remain_len;
389 hif_dev->rx_remain_len = 0;
390 skb_put(remain_skb, rx_pkt_len);
392 skb_pool[pool_index++] = remain_skb;
395 index = rx_remain_len;
399 spin_unlock(&hif_dev->rx_lock);
401 while (index < len) {
407 ptr = (u8 *) skb->data;
409 pkt_len = ptr[index] + (ptr[index+1] << 8);
410 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
412 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
413 RX_STAT_INC(skb_dropped);
417 pad_len = 4 - (pkt_len & 0x3);
422 index = index + 4 + pkt_len + pad_len;
424 if (index > MAX_RX_BUF_SIZE) {
425 spin_lock(&hif_dev->rx_lock);
426 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
427 hif_dev->rx_transfer_len =
428 MAX_RX_BUF_SIZE - chk_idx - 4;
429 hif_dev->rx_pad_len = pad_len;
431 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
433 dev_err(&hif_dev->udev->dev,
434 "ath9k_htc: RX memory allocation error\n");
435 spin_unlock(&hif_dev->rx_lock);
438 skb_reserve(nskb, 32);
439 RX_STAT_INC(skb_allocated);
441 memcpy(nskb->data, &(skb->data[chk_idx+4]),
442 hif_dev->rx_transfer_len);
444 /* Record the buffer pointer */
445 hif_dev->remain_skb = nskb;
446 spin_unlock(&hif_dev->rx_lock);
448 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
450 dev_err(&hif_dev->udev->dev,
451 "ath9k_htc: RX memory allocation error\n");
454 skb_reserve(nskb, 32);
455 RX_STAT_INC(skb_allocated);
457 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
458 skb_put(nskb, pkt_len);
459 skb_pool[pool_index++] = nskb;
464 for (i = 0; i < pool_index; i++) {
465 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
466 skb_pool[i]->len, USB_WLAN_RX_PIPE);
467 RX_STAT_INC(skb_completed);
471 static void ath9k_hif_usb_rx_cb(struct urb *urb)
473 struct sk_buff *skb = (struct sk_buff *) urb->context;
474 struct hif_device_usb *hif_dev =
475 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
484 switch (urb->status) {
496 if (likely(urb->actual_length != 0)) {
497 skb_put(skb, urb->actual_length);
498 ath9k_hif_usb_rx_stream(hif_dev, skb);
502 skb_reset_tail_pointer(skb);
505 usb_anchor_urb(urb, &hif_dev->rx_submitted);
506 ret = usb_submit_urb(urb, GFP_ATOMIC);
508 usb_unanchor_urb(urb);
517 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
519 struct sk_buff *skb = (struct sk_buff *) urb->context;
520 struct sk_buff *nskb;
521 struct hif_device_usb *hif_dev =
522 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
531 switch (urb->status) {
543 if (likely(urb->actual_length != 0)) {
544 skb_put(skb, urb->actual_length);
546 /* Process the command first */
547 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
548 skb->len, USB_REG_IN_PIPE);
551 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
553 dev_err(&hif_dev->udev->dev,
554 "ath9k_htc: REG_IN memory allocation failure\n");
559 usb_fill_bulk_urb(urb, hif_dev->udev,
560 usb_rcvbulkpipe(hif_dev->udev,
562 nskb->data, MAX_REG_IN_BUF_SIZE,
563 ath9k_hif_usb_reg_in_cb, nskb);
565 ret = usb_submit_urb(urb, GFP_ATOMIC);
575 skb_reset_tail_pointer(skb);
578 ret = usb_submit_urb(urb, GFP_ATOMIC);
588 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
590 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
592 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
593 &hif_dev->tx.tx_buf, list) {
594 usb_kill_urb(tx_buf->urb);
595 list_del(&tx_buf->list);
596 usb_free_urb(tx_buf->urb);
601 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
602 &hif_dev->tx.tx_pending, list) {
603 usb_kill_urb(tx_buf->urb);
604 list_del(&tx_buf->list);
605 usb_free_urb(tx_buf->urb);
611 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
613 struct tx_buf *tx_buf;
616 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
617 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
618 spin_lock_init(&hif_dev->tx.tx_lock);
619 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
621 for (i = 0; i < MAX_TX_URB_NUM; i++) {
622 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
626 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
630 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
634 tx_buf->hif_dev = hif_dev;
635 __skb_queue_head_init(&tx_buf->skb_queue);
637 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
640 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
648 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
652 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
654 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
657 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
659 struct urb *urb = NULL;
660 struct sk_buff *skb = NULL;
663 init_usb_anchor(&hif_dev->rx_submitted);
664 spin_lock_init(&hif_dev->rx_lock);
666 for (i = 0; i < MAX_RX_URB_NUM; i++) {
669 urb = usb_alloc_urb(0, GFP_KERNEL);
675 /* Allocate buffer */
676 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
682 usb_fill_bulk_urb(urb, hif_dev->udev,
683 usb_rcvbulkpipe(hif_dev->udev,
685 skb->data, MAX_RX_BUF_SIZE,
686 ath9k_hif_usb_rx_cb, skb);
689 usb_anchor_urb(urb, &hif_dev->rx_submitted);
692 ret = usb_submit_urb(urb, GFP_KERNEL);
694 usb_unanchor_urb(urb);
699 * Drop reference count.
700 * This ensures that the URB is freed when killing them.
712 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
716 static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
718 if (hif_dev->reg_in_urb) {
719 usb_kill_urb(hif_dev->reg_in_urb);
720 if (hif_dev->reg_in_urb->context)
721 kfree_skb((void *)hif_dev->reg_in_urb->context);
722 usb_free_urb(hif_dev->reg_in_urb);
723 hif_dev->reg_in_urb = NULL;
727 static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
731 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
732 if (hif_dev->reg_in_urb == NULL)
735 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
739 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
740 usb_rcvbulkpipe(hif_dev->udev,
742 skb->data, MAX_REG_IN_BUF_SIZE,
743 ath9k_hif_usb_reg_in_cb, skb);
745 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
751 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
755 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
758 init_usb_anchor(&hif_dev->regout_submitted);
761 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
765 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
769 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
774 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
776 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
781 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
783 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
784 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
785 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
786 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
789 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
793 const void *data = hif_dev->firmware->data;
794 size_t len = hif_dev->firmware->size;
795 u32 addr = AR9271_FIRMWARE;
796 u8 *buf = kzalloc(4096, GFP_KERNEL);
803 transfer = min_t(int, len, 4096);
804 memcpy(buf, data, transfer);
806 err = usb_control_msg(hif_dev->udev,
807 usb_sndctrlpipe(hif_dev->udev, 0),
808 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
809 addr >> 8, 0, buf, transfer, HZ);
821 if (drv_info & AR7010_DEVICE)
822 firm_offset = AR7010_FIRMWARE_TEXT;
824 firm_offset = AR9271_FIRMWARE_TEXT;
827 * Issue FW download complete command to firmware.
829 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
830 FIRMWARE_DOWNLOAD_COMP,
832 firm_offset >> 8, 0, NULL, 0, HZ);
836 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
837 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
842 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
845 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
846 struct usb_endpoint_descriptor *endp;
848 /* Request firmware */
849 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
850 &hif_dev->udev->dev);
852 dev_err(&hif_dev->udev->dev,
853 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
857 /* Download firmware */
858 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
860 dev_err(&hif_dev->udev->dev,
861 "ath9k_htc: Firmware - %s download failed\n",
863 goto err_fw_download;
866 /* On downloading the firmware to the target, the USB descriptor of EP4
867 * is 'patched' to change the type of the endpoint to Bulk. This will
868 * bring down CPU usage during the scan period.
870 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
871 endp = &alt->endpoint[idx].desc;
872 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
873 == USB_ENDPOINT_XFER_INT) {
874 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
875 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
881 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
883 dev_err(&hif_dev->udev->dev,
884 "ath9k_htc: Unable to allocate URBs\n");
891 ath9k_hif_usb_dealloc_urbs(hif_dev);
893 release_firmware(hif_dev->firmware);
895 hif_dev->firmware = NULL;
899 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
901 ath9k_hif_usb_dealloc_urbs(hif_dev);
902 if (hif_dev->firmware)
903 release_firmware(hif_dev->firmware);
906 static int ath9k_hif_usb_probe(struct usb_interface *interface,
907 const struct usb_device_id *id)
909 struct usb_device *udev = interface_to_usbdev(interface);
910 struct hif_device_usb *hif_dev;
913 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
920 hif_dev->udev = udev;
921 hif_dev->interface = interface;
922 hif_dev->device_id = id->idProduct;
924 udev->reset_resume = 1;
926 usb_set_intfdata(interface, hif_dev);
928 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
929 &hif_dev->udev->dev);
930 if (hif_dev->htc_handle == NULL) {
932 goto err_htc_hw_alloc;
935 /* Find out which firmware to load */
937 if (id->driver_info & AR7010_DEVICE)
938 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
939 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
941 hif_dev->fw_name = FIRMWARE_AR7010;
943 hif_dev->fw_name = FIRMWARE_AR9271;
945 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
948 goto err_hif_init_usb;
951 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
952 &hif_dev->udev->dev, hif_dev->device_id,
953 hif_dev->udev->product, id->driver_info);
956 goto err_htc_hw_init;
959 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
964 ath9k_hif_usb_dev_deinit(hif_dev);
966 ath9k_htc_hw_free(hif_dev->htc_handle);
968 usb_set_intfdata(interface, NULL);
975 static void ath9k_hif_usb_reboot(struct usb_device *udev)
977 u32 reboot_cmd = 0xffffffff;
981 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
985 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
988 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
993 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
995 struct usb_device *udev = interface_to_usbdev(interface);
996 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
999 ath9k_htc_hw_deinit(hif_dev->htc_handle,
1000 (udev->state == USB_STATE_NOTATTACHED) ? true : false);
1001 ath9k_htc_hw_free(hif_dev->htc_handle);
1002 ath9k_hif_usb_dev_deinit(hif_dev);
1003 usb_set_intfdata(interface, NULL);
1006 if (hif_dev->flags & HIF_USB_START)
1007 ath9k_hif_usb_reboot(udev);
1010 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1015 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1016 pm_message_t message)
1018 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1020 ath9k_hif_usb_dealloc_urbs(hif_dev);
1025 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1027 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1028 struct htc_target *htc_handle = hif_dev->htc_handle;
1031 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1035 if (hif_dev->firmware) {
1036 ret = ath9k_hif_usb_download_fw(hif_dev,
1037 htc_handle->drv_priv->ah->common.driver_info);
1041 ath9k_hif_usb_dealloc_urbs(hif_dev);
1047 ret = ath9k_htc_resume(htc_handle);
1055 ath9k_hif_usb_dealloc_urbs(hif_dev);
1061 static struct usb_driver ath9k_hif_usb_driver = {
1062 .name = "ath9k_hif_usb",
1063 .probe = ath9k_hif_usb_probe,
1064 .disconnect = ath9k_hif_usb_disconnect,
1066 .suspend = ath9k_hif_usb_suspend,
1067 .resume = ath9k_hif_usb_resume,
1068 .reset_resume = ath9k_hif_usb_resume,
1070 .id_table = ath9k_hif_usb_ids,
1074 int ath9k_hif_usb_init(void)
1076 return usb_register(&ath9k_hif_usb_driver);
1079 void ath9k_hif_usb_exit(void)
1081 usb_deregister(&ath9k_hif_usb_driver);