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;
222 u16 *hdr, tx_skb_cnt = 0;
225 if (hif_dev->tx.tx_skb_cnt == 0)
228 /* Check if a free TX buffer is available */
229 if (list_empty(&hif_dev->tx.tx_buf))
232 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
233 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
234 hif_dev->tx.tx_buf_cnt--;
236 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
238 for (i = 0; i < tx_skb_cnt; i++) {
239 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
241 /* Should never be NULL */
244 hif_dev->tx.tx_skb_cnt--;
247 buf += tx_buf->offset;
250 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
252 memcpy(buf, nskb->data, nskb->len);
253 tx_buf->len = nskb->len + 4;
255 if (i < (tx_skb_cnt - 1))
256 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
258 if (i == (tx_skb_cnt - 1))
259 tx_buf->len += tx_buf->offset;
261 __skb_queue_tail(&tx_buf->skb_queue, nskb);
262 TX_STAT_INC(skb_queued);
265 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
266 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
267 tx_buf->buf, tx_buf->len,
268 hif_usb_tx_cb, tx_buf);
270 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
272 tx_buf->len = tx_buf->offset = 0;
273 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
274 __skb_queue_head_init(&tx_buf->skb_queue);
275 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
276 hif_dev->tx.tx_buf_cnt++;
280 TX_STAT_INC(buf_queued);
285 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
286 struct ath9k_htc_tx_ctl *tx_ctl)
290 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
292 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
293 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
297 /* Check if the max queue count has been reached */
298 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
299 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
303 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
304 hif_dev->tx.tx_skb_cnt++;
306 /* Send normal frames immediately */
307 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
308 __hif_usb_tx(hif_dev);
310 /* Check if AMPDUs have to be sent immediately */
311 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
312 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
313 (hif_dev->tx.tx_skb_cnt < 2)) {
314 __hif_usb_tx(hif_dev);
317 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
322 static void hif_usb_start(void *hif_handle, u8 pipe_id)
324 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
327 hif_dev->flags |= HIF_USB_START;
329 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
330 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
331 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
334 static void hif_usb_stop(void *hif_handle, u8 pipe_id)
336 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
337 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
340 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
341 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
342 hif_dev->tx.tx_skb_cnt = 0;
343 hif_dev->tx.flags |= HIF_USB_TX_STOP;
344 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
346 /* The pending URBs have to be canceled. */
347 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
348 &hif_dev->tx.tx_pending, list) {
349 usb_kill_urb(tx_buf->urb);
353 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
354 struct ath9k_htc_tx_ctl *tx_ctl)
356 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
360 case USB_WLAN_TX_PIPE:
361 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
363 case USB_REG_OUT_PIPE:
364 ret = hif_usb_send_regout(hif_dev, skb);
367 dev_err(&hif_dev->udev->dev,
368 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
376 static struct ath9k_htc_hif hif_usb = {
377 .transport = ATH9K_HIF_USB,
378 .name = "ath9k_hif_usb",
380 .control_ul_pipe = USB_REG_OUT_PIPE,
381 .control_dl_pipe = USB_REG_IN_PIPE,
383 .start = hif_usb_start,
384 .stop = hif_usb_stop,
385 .send = hif_usb_send,
388 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
391 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
392 int index = 0, i = 0, len = skb->len;
393 int rx_remain_len, rx_pkt_len;
397 spin_lock(&hif_dev->rx_lock);
399 rx_remain_len = hif_dev->rx_remain_len;
400 rx_pkt_len = hif_dev->rx_transfer_len;
402 if (rx_remain_len != 0) {
403 struct sk_buff *remain_skb = hif_dev->remain_skb;
406 ptr = (u8 *) remain_skb->data;
408 index = rx_remain_len;
409 rx_remain_len -= hif_dev->rx_pad_len;
412 memcpy(ptr, skb->data, rx_remain_len);
414 rx_pkt_len += rx_remain_len;
415 hif_dev->rx_remain_len = 0;
416 skb_put(remain_skb, rx_pkt_len);
418 skb_pool[pool_index++] = remain_skb;
421 index = rx_remain_len;
425 spin_unlock(&hif_dev->rx_lock);
427 while (index < len) {
433 ptr = (u8 *) skb->data;
435 pkt_len = ptr[index] + (ptr[index+1] << 8);
436 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
438 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
439 RX_STAT_INC(skb_dropped);
443 pad_len = 4 - (pkt_len & 0x3);
448 index = index + 4 + pkt_len + pad_len;
450 if (index > MAX_RX_BUF_SIZE) {
451 spin_lock(&hif_dev->rx_lock);
452 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
453 hif_dev->rx_transfer_len =
454 MAX_RX_BUF_SIZE - chk_idx - 4;
455 hif_dev->rx_pad_len = pad_len;
457 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
459 dev_err(&hif_dev->udev->dev,
460 "ath9k_htc: RX memory allocation error\n");
461 spin_unlock(&hif_dev->rx_lock);
464 skb_reserve(nskb, 32);
465 RX_STAT_INC(skb_allocated);
467 memcpy(nskb->data, &(skb->data[chk_idx+4]),
468 hif_dev->rx_transfer_len);
470 /* Record the buffer pointer */
471 hif_dev->remain_skb = nskb;
472 spin_unlock(&hif_dev->rx_lock);
474 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
476 dev_err(&hif_dev->udev->dev,
477 "ath9k_htc: RX memory allocation error\n");
480 skb_reserve(nskb, 32);
481 RX_STAT_INC(skb_allocated);
483 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
484 skb_put(nskb, pkt_len);
485 skb_pool[pool_index++] = nskb;
490 for (i = 0; i < pool_index; i++) {
491 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
492 skb_pool[i]->len, USB_WLAN_RX_PIPE);
493 RX_STAT_INC(skb_completed);
497 static void ath9k_hif_usb_rx_cb(struct urb *urb)
499 struct sk_buff *skb = (struct sk_buff *) urb->context;
500 struct hif_device_usb *hif_dev =
501 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
510 switch (urb->status) {
522 if (likely(urb->actual_length != 0)) {
523 skb_put(skb, urb->actual_length);
524 ath9k_hif_usb_rx_stream(hif_dev, skb);
528 skb_reset_tail_pointer(skb);
531 usb_anchor_urb(urb, &hif_dev->rx_submitted);
532 ret = usb_submit_urb(urb, GFP_ATOMIC);
534 usb_unanchor_urb(urb);
543 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
545 struct sk_buff *skb = (struct sk_buff *) urb->context;
546 struct sk_buff *nskb;
547 struct hif_device_usb *hif_dev =
548 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
557 switch (urb->status) {
569 if (likely(urb->actual_length != 0)) {
570 skb_put(skb, urb->actual_length);
572 /* Process the command first */
573 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
574 skb->len, USB_REG_IN_PIPE);
577 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
579 dev_err(&hif_dev->udev->dev,
580 "ath9k_htc: REG_IN memory allocation failure\n");
585 usb_fill_bulk_urb(urb, hif_dev->udev,
586 usb_rcvbulkpipe(hif_dev->udev,
588 nskb->data, MAX_REG_IN_BUF_SIZE,
589 ath9k_hif_usb_reg_in_cb, nskb);
591 ret = usb_submit_urb(urb, GFP_ATOMIC);
601 skb_reset_tail_pointer(skb);
604 ret = usb_submit_urb(urb, GFP_ATOMIC);
614 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
616 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
619 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
620 &hif_dev->tx.tx_buf, list) {
621 usb_kill_urb(tx_buf->urb);
622 list_del(&tx_buf->list);
623 usb_free_urb(tx_buf->urb);
628 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
629 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
630 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
632 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
633 &hif_dev->tx.tx_pending, list) {
634 usb_kill_urb(tx_buf->urb);
635 list_del(&tx_buf->list);
636 usb_free_urb(tx_buf->urb);
642 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
644 struct tx_buf *tx_buf;
647 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
648 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
649 spin_lock_init(&hif_dev->tx.tx_lock);
650 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
652 for (i = 0; i < MAX_TX_URB_NUM; i++) {
653 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
657 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
661 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
665 tx_buf->hif_dev = hif_dev;
666 __skb_queue_head_init(&tx_buf->skb_queue);
668 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
671 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
679 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
683 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
685 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
688 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
690 struct urb *urb = NULL;
691 struct sk_buff *skb = NULL;
694 init_usb_anchor(&hif_dev->rx_submitted);
695 spin_lock_init(&hif_dev->rx_lock);
697 for (i = 0; i < MAX_RX_URB_NUM; i++) {
700 urb = usb_alloc_urb(0, GFP_KERNEL);
706 /* Allocate buffer */
707 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
713 usb_fill_bulk_urb(urb, hif_dev->udev,
714 usb_rcvbulkpipe(hif_dev->udev,
716 skb->data, MAX_RX_BUF_SIZE,
717 ath9k_hif_usb_rx_cb, skb);
720 usb_anchor_urb(urb, &hif_dev->rx_submitted);
723 ret = usb_submit_urb(urb, GFP_KERNEL);
725 usb_unanchor_urb(urb);
730 * Drop reference count.
731 * This ensures that the URB is freed when killing them.
743 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
747 static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
749 if (hif_dev->reg_in_urb) {
750 usb_kill_urb(hif_dev->reg_in_urb);
751 if (hif_dev->reg_in_urb->context)
752 kfree_skb((void *)hif_dev->reg_in_urb->context);
753 usb_free_urb(hif_dev->reg_in_urb);
754 hif_dev->reg_in_urb = NULL;
758 static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
762 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
763 if (hif_dev->reg_in_urb == NULL)
766 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
770 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
771 usb_rcvbulkpipe(hif_dev->udev,
773 skb->data, MAX_REG_IN_BUF_SIZE,
774 ath9k_hif_usb_reg_in_cb, skb);
776 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
782 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
786 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
789 init_usb_anchor(&hif_dev->regout_submitted);
792 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
796 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
800 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
805 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
807 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
812 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
814 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
815 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
816 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
817 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
820 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
824 const void *data = hif_dev->firmware->data;
825 size_t len = hif_dev->firmware->size;
826 u32 addr = AR9271_FIRMWARE;
827 u8 *buf = kzalloc(4096, GFP_KERNEL);
834 transfer = min_t(int, len, 4096);
835 memcpy(buf, data, transfer);
837 err = usb_control_msg(hif_dev->udev,
838 usb_sndctrlpipe(hif_dev->udev, 0),
839 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
840 addr >> 8, 0, buf, transfer, HZ);
852 if (IS_AR7010_DEVICE(drv_info))
853 firm_offset = AR7010_FIRMWARE_TEXT;
855 firm_offset = AR9271_FIRMWARE_TEXT;
858 * Issue FW download complete command to firmware.
860 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
861 FIRMWARE_DOWNLOAD_COMP,
863 firm_offset >> 8, 0, NULL, 0, HZ);
867 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
868 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
873 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
876 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
877 struct usb_endpoint_descriptor *endp;
879 /* Request firmware */
880 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
881 &hif_dev->udev->dev);
883 dev_err(&hif_dev->udev->dev,
884 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
888 /* Download firmware */
889 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
891 dev_err(&hif_dev->udev->dev,
892 "ath9k_htc: Firmware - %s download failed\n",
894 goto err_fw_download;
897 /* On downloading the firmware to the target, the USB descriptor of EP4
898 * is 'patched' to change the type of the endpoint to Bulk. This will
899 * bring down CPU usage during the scan period.
901 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
902 endp = &alt->endpoint[idx].desc;
903 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
904 == USB_ENDPOINT_XFER_INT) {
905 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
906 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
912 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
914 dev_err(&hif_dev->udev->dev,
915 "ath9k_htc: Unable to allocate URBs\n");
922 ath9k_hif_usb_dealloc_urbs(hif_dev);
924 release_firmware(hif_dev->firmware);
926 hif_dev->firmware = NULL;
930 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
932 ath9k_hif_usb_dealloc_urbs(hif_dev);
933 if (hif_dev->firmware)
934 release_firmware(hif_dev->firmware);
937 static int ath9k_hif_usb_probe(struct usb_interface *interface,
938 const struct usb_device_id *id)
940 struct usb_device *udev = interface_to_usbdev(interface);
941 struct hif_device_usb *hif_dev;
944 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
951 hif_dev->udev = udev;
952 hif_dev->interface = interface;
953 hif_dev->device_id = id->idProduct;
955 udev->reset_resume = 1;
957 usb_set_intfdata(interface, hif_dev);
959 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
960 &hif_dev->udev->dev);
961 if (hif_dev->htc_handle == NULL) {
963 goto err_htc_hw_alloc;
966 /* Find out which firmware to load */
968 if (IS_AR7010_DEVICE(id->driver_info))
969 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
970 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
972 hif_dev->fw_name = FIRMWARE_AR7010;
974 hif_dev->fw_name = FIRMWARE_AR9271;
976 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
979 goto err_hif_init_usb;
982 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
983 &hif_dev->udev->dev, hif_dev->device_id,
984 hif_dev->udev->product, id->driver_info);
987 goto err_htc_hw_init;
990 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
995 ath9k_hif_usb_dev_deinit(hif_dev);
997 ath9k_htc_hw_free(hif_dev->htc_handle);
999 usb_set_intfdata(interface, NULL);
1006 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1008 u32 reboot_cmd = 0xffffffff;
1012 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1016 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1019 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1024 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1026 struct usb_device *udev = interface_to_usbdev(interface);
1027 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1028 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1031 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1032 ath9k_htc_hw_free(hif_dev->htc_handle);
1033 ath9k_hif_usb_dev_deinit(hif_dev);
1034 usb_set_intfdata(interface, NULL);
1037 if (!unplugged && (hif_dev->flags & HIF_USB_START))
1038 ath9k_hif_usb_reboot(udev);
1041 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1046 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1047 pm_message_t message)
1049 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1052 * The device has to be set to FULLSLEEP mode in case no
1055 if (!(hif_dev->flags & HIF_USB_START))
1056 ath9k_htc_suspend(hif_dev->htc_handle);
1058 ath9k_hif_usb_dealloc_urbs(hif_dev);
1063 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1065 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1066 struct htc_target *htc_handle = hif_dev->htc_handle;
1069 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1073 if (hif_dev->firmware) {
1074 ret = ath9k_hif_usb_download_fw(hif_dev,
1075 htc_handle->drv_priv->ah->hw_version.usbdev);
1079 ath9k_hif_usb_dealloc_urbs(hif_dev);
1085 ret = ath9k_htc_resume(htc_handle);
1093 ath9k_hif_usb_dealloc_urbs(hif_dev);
1099 static struct usb_driver ath9k_hif_usb_driver = {
1100 .name = "ath9k_hif_usb",
1101 .probe = ath9k_hif_usb_probe,
1102 .disconnect = ath9k_hif_usb_disconnect,
1104 .suspend = ath9k_hif_usb_suspend,
1105 .resume = ath9k_hif_usb_resume,
1106 .reset_resume = ath9k_hif_usb_resume,
1108 .id_table = ath9k_hif_usb_ids,
1112 int ath9k_hif_usb_init(void)
1114 return usb_register(&ath9k_hif_usb_driver);
1117 void ath9k_hif_usb_exit(void)
1119 usb_deregister(&ath9k_hif_usb_driver);