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 */
55 { USB_DEVICE(0x0cf3, 0x20ff),
56 .driver_info = STORAGE_DEVICE },
61 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
63 static int __hif_usb_tx(struct hif_device_usb *hif_dev);
65 static void hif_usb_regout_cb(struct urb *urb)
67 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
69 switch (urb->status) {
82 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
93 static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
100 urb = usb_alloc_urb(0, GFP_KERNEL);
104 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
111 cmd->hif_dev = hif_dev;
113 usb_fill_bulk_urb(urb, hif_dev->udev,
114 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE),
116 hif_usb_regout_cb, cmd);
118 usb_anchor_urb(urb, &hif_dev->regout_submitted);
119 ret = usb_submit_urb(urb, GFP_KERNEL);
121 usb_unanchor_urb(urb);
129 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
130 struct sk_buff_head *list)
134 while ((skb = __skb_dequeue(list)) != NULL) {
135 dev_kfree_skb_any(skb);
136 TX_STAT_INC(skb_dropped);
140 static void hif_usb_tx_cb(struct urb *urb)
142 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
143 struct hif_device_usb *hif_dev;
146 if (!tx_buf || !tx_buf->hif_dev)
149 hif_dev = tx_buf->hif_dev;
151 switch (urb->status) {
159 * The URB has been killed, free the SKBs.
161 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
164 * If the URBs are being flushed, no need to add this
165 * URB to the free list.
167 spin_lock(&hif_dev->tx.tx_lock);
168 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
169 spin_unlock(&hif_dev->tx.tx_lock);
172 spin_unlock(&hif_dev->tx.tx_lock);
175 * In the stop() case, this URB has to be added to
184 * Check if TX has been stopped, this is needed because
185 * this CB could have been invoked just after the TX lock
186 * was released in hif_stop() and kill_urb() hasn't been
189 spin_lock(&hif_dev->tx.tx_lock);
190 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
191 spin_unlock(&hif_dev->tx.tx_lock);
192 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
195 spin_unlock(&hif_dev->tx.tx_lock);
197 /* Complete the queued SKBs. */
198 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
199 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
201 TX_STAT_INC(skb_completed);
205 /* Re-initialize the SKB queue */
206 tx_buf->len = tx_buf->offset = 0;
207 __skb_queue_head_init(&tx_buf->skb_queue);
209 /* Add this TX buffer to the free list */
210 spin_lock(&hif_dev->tx.tx_lock);
211 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
212 hif_dev->tx.tx_buf_cnt++;
213 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
214 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
215 TX_STAT_INC(buf_completed);
216 spin_unlock(&hif_dev->tx.tx_lock);
219 /* TX lock has to be taken */
220 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
222 struct tx_buf *tx_buf = NULL;
223 struct sk_buff *nskb = NULL;
229 if (hif_dev->tx.tx_skb_cnt == 0)
232 /* Check if a free TX buffer is available */
233 if (list_empty(&hif_dev->tx.tx_buf))
236 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
237 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
238 hif_dev->tx.tx_buf_cnt--;
240 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
242 for (i = 0; i < tx_skb_cnt; i++) {
243 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
245 /* Should never be NULL */
248 hif_dev->tx.tx_skb_cnt--;
251 buf += tx_buf->offset;
253 *hdr++ = cpu_to_le16(nskb->len);
254 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
256 memcpy(buf, nskb->data, nskb->len);
257 tx_buf->len = nskb->len + 4;
259 if (i < (tx_skb_cnt - 1))
260 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
262 if (i == (tx_skb_cnt - 1))
263 tx_buf->len += tx_buf->offset;
265 __skb_queue_tail(&tx_buf->skb_queue, nskb);
266 TX_STAT_INC(skb_queued);
269 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
270 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
271 tx_buf->buf, tx_buf->len,
272 hif_usb_tx_cb, tx_buf);
274 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
276 tx_buf->len = tx_buf->offset = 0;
277 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
278 __skb_queue_head_init(&tx_buf->skb_queue);
279 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
280 hif_dev->tx.tx_buf_cnt++;
284 TX_STAT_INC(buf_queued);
289 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
290 struct ath9k_htc_tx_ctl *tx_ctl)
294 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
296 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
297 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
301 /* Check if the max queue count has been reached */
302 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
303 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
307 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
308 hif_dev->tx.tx_skb_cnt++;
310 /* Send normal frames immediately */
311 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
312 __hif_usb_tx(hif_dev);
314 /* Check if AMPDUs have to be sent immediately */
315 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
316 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
317 (hif_dev->tx.tx_skb_cnt < 2)) {
318 __hif_usb_tx(hif_dev);
321 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
326 static void hif_usb_start(void *hif_handle, u8 pipe_id)
328 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
331 hif_dev->flags |= HIF_USB_START;
333 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
334 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
335 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
338 static void hif_usb_stop(void *hif_handle, u8 pipe_id)
340 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
341 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
344 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
345 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue);
346 hif_dev->tx.tx_skb_cnt = 0;
347 hif_dev->tx.flags |= HIF_USB_TX_STOP;
348 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
350 /* The pending URBs have to be canceled. */
351 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
352 &hif_dev->tx.tx_pending, list) {
353 usb_kill_urb(tx_buf->urb);
357 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
358 struct ath9k_htc_tx_ctl *tx_ctl)
360 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
364 case USB_WLAN_TX_PIPE:
365 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
367 case USB_REG_OUT_PIPE:
368 ret = hif_usb_send_regout(hif_dev, skb);
371 dev_err(&hif_dev->udev->dev,
372 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
380 static struct ath9k_htc_hif hif_usb = {
381 .transport = ATH9K_HIF_USB,
382 .name = "ath9k_hif_usb",
384 .control_ul_pipe = USB_REG_OUT_PIPE,
385 .control_dl_pipe = USB_REG_IN_PIPE,
387 .start = hif_usb_start,
388 .stop = hif_usb_stop,
389 .send = hif_usb_send,
392 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
395 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
396 int index = 0, i = 0, len = skb->len;
397 int rx_remain_len, rx_pkt_len;
401 spin_lock(&hif_dev->rx_lock);
403 rx_remain_len = hif_dev->rx_remain_len;
404 rx_pkt_len = hif_dev->rx_transfer_len;
406 if (rx_remain_len != 0) {
407 struct sk_buff *remain_skb = hif_dev->remain_skb;
410 ptr = (u8 *) remain_skb->data;
412 index = rx_remain_len;
413 rx_remain_len -= hif_dev->rx_pad_len;
416 memcpy(ptr, skb->data, rx_remain_len);
418 rx_pkt_len += rx_remain_len;
419 hif_dev->rx_remain_len = 0;
420 skb_put(remain_skb, rx_pkt_len);
422 skb_pool[pool_index++] = remain_skb;
425 index = rx_remain_len;
429 spin_unlock(&hif_dev->rx_lock);
431 while (index < len) {
437 ptr = (u8 *) skb->data;
439 pkt_len = ptr[index] + (ptr[index+1] << 8);
440 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
442 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
443 RX_STAT_INC(skb_dropped);
447 pad_len = 4 - (pkt_len & 0x3);
452 index = index + 4 + pkt_len + pad_len;
454 if (index > MAX_RX_BUF_SIZE) {
455 spin_lock(&hif_dev->rx_lock);
456 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
457 hif_dev->rx_transfer_len =
458 MAX_RX_BUF_SIZE - chk_idx - 4;
459 hif_dev->rx_pad_len = pad_len;
461 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
463 dev_err(&hif_dev->udev->dev,
464 "ath9k_htc: RX memory allocation error\n");
465 spin_unlock(&hif_dev->rx_lock);
468 skb_reserve(nskb, 32);
469 RX_STAT_INC(skb_allocated);
471 memcpy(nskb->data, &(skb->data[chk_idx+4]),
472 hif_dev->rx_transfer_len);
474 /* Record the buffer pointer */
475 hif_dev->remain_skb = nskb;
476 spin_unlock(&hif_dev->rx_lock);
478 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
480 dev_err(&hif_dev->udev->dev,
481 "ath9k_htc: RX memory allocation error\n");
484 skb_reserve(nskb, 32);
485 RX_STAT_INC(skb_allocated);
487 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
488 skb_put(nskb, pkt_len);
489 skb_pool[pool_index++] = nskb;
494 for (i = 0; i < pool_index; i++) {
495 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
496 skb_pool[i]->len, USB_WLAN_RX_PIPE);
497 RX_STAT_INC(skb_completed);
501 static void ath9k_hif_usb_rx_cb(struct urb *urb)
503 struct sk_buff *skb = (struct sk_buff *) urb->context;
504 struct hif_device_usb *hif_dev =
505 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
514 switch (urb->status) {
526 if (likely(urb->actual_length != 0)) {
527 skb_put(skb, urb->actual_length);
528 ath9k_hif_usb_rx_stream(hif_dev, skb);
532 skb_reset_tail_pointer(skb);
535 usb_anchor_urb(urb, &hif_dev->rx_submitted);
536 ret = usb_submit_urb(urb, GFP_ATOMIC);
538 usb_unanchor_urb(urb);
547 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
549 struct sk_buff *skb = (struct sk_buff *) urb->context;
550 struct sk_buff *nskb;
551 struct hif_device_usb *hif_dev =
552 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
561 switch (urb->status) {
573 if (likely(urb->actual_length != 0)) {
574 skb_put(skb, urb->actual_length);
576 /* Process the command first */
577 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
578 skb->len, USB_REG_IN_PIPE);
581 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
583 dev_err(&hif_dev->udev->dev,
584 "ath9k_htc: REG_IN memory allocation failure\n");
589 usb_fill_bulk_urb(urb, hif_dev->udev,
590 usb_rcvbulkpipe(hif_dev->udev,
592 nskb->data, MAX_REG_IN_BUF_SIZE,
593 ath9k_hif_usb_reg_in_cb, nskb);
595 ret = usb_submit_urb(urb, GFP_ATOMIC);
605 skb_reset_tail_pointer(skb);
608 ret = usb_submit_urb(urb, GFP_ATOMIC);
618 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
620 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
623 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
624 &hif_dev->tx.tx_buf, list) {
625 usb_kill_urb(tx_buf->urb);
626 list_del(&tx_buf->list);
627 usb_free_urb(tx_buf->urb);
632 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
633 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
634 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
636 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
637 &hif_dev->tx.tx_pending, list) {
638 usb_kill_urb(tx_buf->urb);
639 list_del(&tx_buf->list);
640 usb_free_urb(tx_buf->urb);
646 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
648 struct tx_buf *tx_buf;
651 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
652 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
653 spin_lock_init(&hif_dev->tx.tx_lock);
654 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
656 for (i = 0; i < MAX_TX_URB_NUM; i++) {
657 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
661 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
665 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
669 tx_buf->hif_dev = hif_dev;
670 __skb_queue_head_init(&tx_buf->skb_queue);
672 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
675 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
683 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
687 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
689 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
692 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
694 struct urb *urb = NULL;
695 struct sk_buff *skb = NULL;
698 init_usb_anchor(&hif_dev->rx_submitted);
699 spin_lock_init(&hif_dev->rx_lock);
701 for (i = 0; i < MAX_RX_URB_NUM; i++) {
704 urb = usb_alloc_urb(0, GFP_KERNEL);
710 /* Allocate buffer */
711 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
717 usb_fill_bulk_urb(urb, hif_dev->udev,
718 usb_rcvbulkpipe(hif_dev->udev,
720 skb->data, MAX_RX_BUF_SIZE,
721 ath9k_hif_usb_rx_cb, skb);
724 usb_anchor_urb(urb, &hif_dev->rx_submitted);
727 ret = usb_submit_urb(urb, GFP_KERNEL);
729 usb_unanchor_urb(urb);
734 * Drop reference count.
735 * This ensures that the URB is freed when killing them.
747 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
751 static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
753 if (hif_dev->reg_in_urb) {
754 usb_kill_urb(hif_dev->reg_in_urb);
755 if (hif_dev->reg_in_urb->context)
756 kfree_skb((void *)hif_dev->reg_in_urb->context);
757 usb_free_urb(hif_dev->reg_in_urb);
758 hif_dev->reg_in_urb = NULL;
762 static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
766 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
767 if (hif_dev->reg_in_urb == NULL)
770 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
774 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev,
775 usb_rcvbulkpipe(hif_dev->udev,
777 skb->data, MAX_REG_IN_BUF_SIZE,
778 ath9k_hif_usb_reg_in_cb, skb);
780 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
786 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
790 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
793 init_usb_anchor(&hif_dev->regout_submitted);
796 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
800 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
804 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
809 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
811 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
816 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
818 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
819 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
820 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
821 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
824 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
828 const void *data = hif_dev->firmware->data;
829 size_t len = hif_dev->firmware->size;
830 u32 addr = AR9271_FIRMWARE;
831 u8 *buf = kzalloc(4096, GFP_KERNEL);
838 transfer = min_t(int, len, 4096);
839 memcpy(buf, data, transfer);
841 err = usb_control_msg(hif_dev->udev,
842 usb_sndctrlpipe(hif_dev->udev, 0),
843 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
844 addr >> 8, 0, buf, transfer, HZ);
856 if (IS_AR7010_DEVICE(drv_info))
857 firm_offset = AR7010_FIRMWARE_TEXT;
859 firm_offset = AR9271_FIRMWARE_TEXT;
862 * Issue FW download complete command to firmware.
864 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
865 FIRMWARE_DOWNLOAD_COMP,
867 firm_offset >> 8, 0, NULL, 0, HZ);
871 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
872 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
877 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
880 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
881 struct usb_endpoint_descriptor *endp;
883 /* Request firmware */
884 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
885 &hif_dev->udev->dev);
887 dev_err(&hif_dev->udev->dev,
888 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
892 /* Download firmware */
893 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
895 dev_err(&hif_dev->udev->dev,
896 "ath9k_htc: Firmware - %s download failed\n",
898 goto err_fw_download;
901 /* On downloading the firmware to the target, the USB descriptor of EP4
902 * is 'patched' to change the type of the endpoint to Bulk. This will
903 * bring down CPU usage during the scan period.
905 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
906 endp = &alt->endpoint[idx].desc;
907 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
908 == USB_ENDPOINT_XFER_INT) {
909 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
910 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
916 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
918 dev_err(&hif_dev->udev->dev,
919 "ath9k_htc: Unable to allocate URBs\n");
920 goto err_fw_download;
926 release_firmware(hif_dev->firmware);
928 hif_dev->firmware = NULL;
932 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
934 ath9k_hif_usb_dealloc_urbs(hif_dev);
935 if (hif_dev->firmware)
936 release_firmware(hif_dev->firmware);
940 * An exact copy of the function from zd1211rw.
942 static int send_eject_command(struct usb_interface *interface)
944 struct usb_device *udev = interface_to_usbdev(interface);
945 struct usb_host_interface *iface_desc = &interface->altsetting[0];
946 struct usb_endpoint_descriptor *endpoint;
951 /* Find bulk out endpoint */
952 for (r = 1; r >= 0; r--) {
953 endpoint = &iface_desc->endpoint[r].desc;
954 if (usb_endpoint_dir_out(endpoint) &&
955 usb_endpoint_xfer_bulk(endpoint)) {
956 bulk_out_ep = endpoint->bEndpointAddress;
962 "ath9k_htc: Could not find bulk out endpoint\n");
966 cmd = kzalloc(31, GFP_KERNEL);
970 /* USB bulk command block */
971 cmd[0] = 0x55; /* bulk command signature */
972 cmd[1] = 0x53; /* bulk command signature */
973 cmd[2] = 0x42; /* bulk command signature */
974 cmd[3] = 0x43; /* bulk command signature */
975 cmd[14] = 6; /* command length */
977 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
978 cmd[19] = 0x2; /* eject disc */
980 dev_info(&udev->dev, "Ejecting storage device...\n");
981 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
982 cmd, 31, NULL, 2000);
987 /* At this point, the device disconnects and reconnects with the real
990 usb_set_intfdata(interface, NULL);
994 static int ath9k_hif_usb_probe(struct usb_interface *interface,
995 const struct usb_device_id *id)
997 struct usb_device *udev = interface_to_usbdev(interface);
998 struct hif_device_usb *hif_dev;
1001 if (id->driver_info == STORAGE_DEVICE)
1002 return send_eject_command(interface);
1004 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1011 hif_dev->udev = udev;
1012 hif_dev->interface = interface;
1013 hif_dev->device_id = id->idProduct;
1015 udev->reset_resume = 1;
1017 usb_set_intfdata(interface, hif_dev);
1019 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1020 &hif_dev->udev->dev);
1021 if (hif_dev->htc_handle == NULL) {
1023 goto err_htc_hw_alloc;
1026 /* Find out which firmware to load */
1028 if (IS_AR7010_DEVICE(id->driver_info))
1029 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202)
1030 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
1032 hif_dev->fw_name = FIRMWARE_AR7010;
1034 hif_dev->fw_name = FIRMWARE_AR9271;
1036 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
1039 goto err_hif_init_usb;
1042 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1043 &hif_dev->udev->dev, hif_dev->device_id,
1044 hif_dev->udev->product, id->driver_info);
1047 goto err_htc_hw_init;
1050 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1055 ath9k_hif_usb_dev_deinit(hif_dev);
1057 ath9k_htc_hw_free(hif_dev->htc_handle);
1059 usb_set_intfdata(interface, NULL);
1066 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1068 u32 reboot_cmd = 0xffffffff;
1072 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1076 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1079 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1084 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1086 struct usb_device *udev = interface_to_usbdev(interface);
1087 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1088 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1093 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1094 ath9k_htc_hw_free(hif_dev->htc_handle);
1095 ath9k_hif_usb_dev_deinit(hif_dev);
1096 usb_set_intfdata(interface, NULL);
1098 if (!unplugged && (hif_dev->flags & HIF_USB_START))
1099 ath9k_hif_usb_reboot(udev);
1102 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1107 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1108 pm_message_t message)
1110 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1113 * The device has to be set to FULLSLEEP mode in case no
1116 if (!(hif_dev->flags & HIF_USB_START))
1117 ath9k_htc_suspend(hif_dev->htc_handle);
1119 ath9k_hif_usb_dealloc_urbs(hif_dev);
1124 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1126 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1127 struct htc_target *htc_handle = hif_dev->htc_handle;
1130 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1134 if (hif_dev->firmware) {
1135 ret = ath9k_hif_usb_download_fw(hif_dev,
1136 htc_handle->drv_priv->ah->hw_version.usbdev);
1140 ath9k_hif_usb_dealloc_urbs(hif_dev);
1146 ret = ath9k_htc_resume(htc_handle);
1154 ath9k_hif_usb_dealloc_urbs(hif_dev);
1160 static struct usb_driver ath9k_hif_usb_driver = {
1161 .name = "ath9k_hif_usb",
1162 .probe = ath9k_hif_usb_probe,
1163 .disconnect = ath9k_hif_usb_disconnect,
1165 .suspend = ath9k_hif_usb_suspend,
1166 .resume = ath9k_hif_usb_resume,
1167 .reset_resume = ath9k_hif_usb_resume,
1169 .id_table = ath9k_hif_usb_ids,
1173 int ath9k_hif_usb_init(void)
1175 return usb_register(&ath9k_hif_usb_driver);
1178 void ath9k_hif_usb_exit(void)
1180 usb_deregister(&ath9k_hif_usb_driver);