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_1_1 "htc_7010.fw"
21 #define FIRMWARE_AR9271 "htc_9271.fw"
23 MODULE_FIRMWARE(FIRMWARE_AR7010_1_1);
24 MODULE_FIRMWARE(FIRMWARE_AR9271);
26 static struct usb_device_id ath9k_hif_usb_ids[] = {
27 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */
28 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */
29 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */
30 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
31 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
32 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
33 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
34 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
35 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
36 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
37 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
38 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
39 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
41 { USB_DEVICE(0x0cf3, 0x7015),
42 .driver_info = AR9287_USB }, /* Atheros */
43 { USB_DEVICE(0x1668, 0x1200),
44 .driver_info = AR9287_USB }, /* Verizon */
46 { USB_DEVICE(0x0cf3, 0x7010),
47 .driver_info = AR9280_USB }, /* Atheros */
48 { USB_DEVICE(0x0846, 0x9018),
49 .driver_info = AR9280_USB }, /* Netgear WNDA3200 */
50 { USB_DEVICE(0x083A, 0xA704),
51 .driver_info = AR9280_USB }, /* SMC Networks */
53 { USB_DEVICE(0x0cf3, 0x20ff),
54 .driver_info = STORAGE_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 void hif_usb_mgmt_cb(struct urb *urb)
129 struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
130 struct hif_device_usb *hif_dev = cmd->hif_dev;
133 if (!cmd || !cmd->skb || !cmd->hif_dev)
136 switch (urb->status) {
146 * If the URBs are being flushed, no need to complete
149 spin_lock(&hif_dev->tx.tx_lock);
150 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
151 spin_unlock(&hif_dev->tx.tx_lock);
152 dev_kfree_skb_any(cmd->skb);
156 spin_unlock(&hif_dev->tx.tx_lock);
164 skb_pull(cmd->skb, 4);
165 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
170 static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev,
178 urb = usb_alloc_urb(0, GFP_ATOMIC);
182 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
189 cmd->hif_dev = hif_dev;
191 hdr = (__le16 *) skb_push(skb, 4);
192 *hdr++ = cpu_to_le16(skb->len - 4);
193 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
195 usb_fill_bulk_urb(urb, hif_dev->udev,
196 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
198 hif_usb_mgmt_cb, cmd);
200 usb_anchor_urb(urb, &hif_dev->mgmt_submitted);
201 ret = usb_submit_urb(urb, GFP_ATOMIC);
203 usb_unanchor_urb(urb);
211 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev,
212 struct sk_buff_head *list)
216 while ((skb = __skb_dequeue(list)) != NULL) {
217 dev_kfree_skb_any(skb);
221 static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev,
222 struct sk_buff_head *queue,
227 while ((skb = __skb_dequeue(queue)) != NULL) {
228 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
231 TX_STAT_INC(skb_success);
233 TX_STAT_INC(skb_failed);
237 static void hif_usb_tx_cb(struct urb *urb)
239 struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
240 struct hif_device_usb *hif_dev;
243 if (!tx_buf || !tx_buf->hif_dev)
246 hif_dev = tx_buf->hif_dev;
248 switch (urb->status) {
258 * If the URBs are being flushed, no need to add this
259 * URB to the free list.
261 spin_lock(&hif_dev->tx.tx_lock);
262 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
263 spin_unlock(&hif_dev->tx.tx_lock);
264 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue);
267 spin_unlock(&hif_dev->tx.tx_lock);
275 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok);
277 /* Re-initialize the SKB queue */
278 tx_buf->len = tx_buf->offset = 0;
279 __skb_queue_head_init(&tx_buf->skb_queue);
281 /* Add this TX buffer to the free list */
282 spin_lock(&hif_dev->tx.tx_lock);
283 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
284 hif_dev->tx.tx_buf_cnt++;
285 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP))
286 __hif_usb_tx(hif_dev); /* Check for pending SKBs */
287 TX_STAT_INC(buf_completed);
288 spin_unlock(&hif_dev->tx.tx_lock);
291 /* TX lock has to be taken */
292 static int __hif_usb_tx(struct hif_device_usb *hif_dev)
294 struct tx_buf *tx_buf = NULL;
295 struct sk_buff *nskb = NULL;
301 if (hif_dev->tx.tx_skb_cnt == 0)
304 /* Check if a free TX buffer is available */
305 if (list_empty(&hif_dev->tx.tx_buf))
308 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
309 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
310 hif_dev->tx.tx_buf_cnt--;
312 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
314 for (i = 0; i < tx_skb_cnt; i++) {
315 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
317 /* Should never be NULL */
320 hif_dev->tx.tx_skb_cnt--;
323 buf += tx_buf->offset;
325 *hdr++ = cpu_to_le16(nskb->len);
326 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG);
328 memcpy(buf, nskb->data, nskb->len);
329 tx_buf->len = nskb->len + 4;
331 if (i < (tx_skb_cnt - 1))
332 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
334 if (i == (tx_skb_cnt - 1))
335 tx_buf->len += tx_buf->offset;
337 __skb_queue_tail(&tx_buf->skb_queue, nskb);
338 TX_STAT_INC(skb_queued);
341 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
342 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
343 tx_buf->buf, tx_buf->len,
344 hif_usb_tx_cb, tx_buf);
346 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
348 tx_buf->len = tx_buf->offset = 0;
349 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false);
350 __skb_queue_head_init(&tx_buf->skb_queue);
351 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
352 hif_dev->tx.tx_buf_cnt++;
356 TX_STAT_INC(buf_queued);
361 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb)
363 struct ath9k_htc_tx_ctl *tx_ctl;
367 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
369 if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
370 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
374 /* Check if the max queue count has been reached */
375 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
376 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
380 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
382 tx_ctl = HTC_SKB_CB(skb);
384 /* Mgmt/Beacon frames don't use the TX buffer pool */
385 if ((tx_ctl->type == ATH9K_HTC_MGMT) ||
386 (tx_ctl->type == ATH9K_HTC_BEACON)) {
387 ret = hif_usb_send_mgmt(hif_dev, skb);
390 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
392 if ((tx_ctl->type == ATH9K_HTC_NORMAL) ||
393 (tx_ctl->type == ATH9K_HTC_AMPDU)) {
394 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
395 hif_dev->tx.tx_skb_cnt++;
398 /* Check if AMPDUs have to be sent immediately */
399 if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
400 (hif_dev->tx.tx_skb_cnt < 2)) {
401 __hif_usb_tx(hif_dev);
404 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
409 static void hif_usb_start(void *hif_handle)
411 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
414 hif_dev->flags |= HIF_USB_START;
416 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
417 hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
418 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
421 static void hif_usb_stop(void *hif_handle)
423 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
424 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
427 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
428 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false);
429 hif_dev->tx.tx_skb_cnt = 0;
430 hif_dev->tx.flags |= HIF_USB_TX_STOP;
431 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
433 /* The pending URBs have to be canceled. */
434 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
435 &hif_dev->tx.tx_pending, list) {
436 usb_kill_urb(tx_buf->urb);
439 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
442 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
444 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
448 case USB_WLAN_TX_PIPE:
449 ret = hif_usb_send_tx(hif_dev, skb);
451 case USB_REG_OUT_PIPE:
452 ret = hif_usb_send_regout(hif_dev, skb);
455 dev_err(&hif_dev->udev->dev,
456 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
464 static inline bool check_index(struct sk_buff *skb, u8 idx)
466 struct ath9k_htc_tx_ctl *tx_ctl;
468 tx_ctl = HTC_SKB_CB(skb);
470 if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
471 (tx_ctl->sta_idx == idx))
477 static void hif_usb_sta_drain(void *hif_handle, u8 idx)
479 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
480 struct sk_buff *skb, *tmp;
483 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
485 skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
486 if (check_index(skb, idx)) {
487 __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
488 ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
490 hif_dev->tx.tx_skb_cnt--;
491 TX_STAT_INC(skb_failed);
495 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
498 static struct ath9k_htc_hif hif_usb = {
499 .transport = ATH9K_HIF_USB,
500 .name = "ath9k_hif_usb",
502 .control_ul_pipe = USB_REG_OUT_PIPE,
503 .control_dl_pipe = USB_REG_IN_PIPE,
505 .start = hif_usb_start,
506 .stop = hif_usb_stop,
507 .sta_drain = hif_usb_sta_drain,
508 .send = hif_usb_send,
511 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
514 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
515 int index = 0, i = 0, len = skb->len;
516 int rx_remain_len, rx_pkt_len;
520 spin_lock(&hif_dev->rx_lock);
522 rx_remain_len = hif_dev->rx_remain_len;
523 rx_pkt_len = hif_dev->rx_transfer_len;
525 if (rx_remain_len != 0) {
526 struct sk_buff *remain_skb = hif_dev->remain_skb;
529 ptr = (u8 *) remain_skb->data;
531 index = rx_remain_len;
532 rx_remain_len -= hif_dev->rx_pad_len;
535 memcpy(ptr, skb->data, rx_remain_len);
537 rx_pkt_len += rx_remain_len;
538 hif_dev->rx_remain_len = 0;
539 skb_put(remain_skb, rx_pkt_len);
541 skb_pool[pool_index++] = remain_skb;
544 index = rx_remain_len;
548 spin_unlock(&hif_dev->rx_lock);
550 while (index < len) {
556 ptr = (u8 *) skb->data;
558 pkt_len = ptr[index] + (ptr[index+1] << 8);
559 pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
561 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
562 RX_STAT_INC(skb_dropped);
566 pad_len = 4 - (pkt_len & 0x3);
571 index = index + 4 + pkt_len + pad_len;
573 if (index > MAX_RX_BUF_SIZE) {
574 spin_lock(&hif_dev->rx_lock);
575 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
576 hif_dev->rx_transfer_len =
577 MAX_RX_BUF_SIZE - chk_idx - 4;
578 hif_dev->rx_pad_len = pad_len;
580 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
582 dev_err(&hif_dev->udev->dev,
583 "ath9k_htc: RX memory allocation error\n");
584 spin_unlock(&hif_dev->rx_lock);
587 skb_reserve(nskb, 32);
588 RX_STAT_INC(skb_allocated);
590 memcpy(nskb->data, &(skb->data[chk_idx+4]),
591 hif_dev->rx_transfer_len);
593 /* Record the buffer pointer */
594 hif_dev->remain_skb = nskb;
595 spin_unlock(&hif_dev->rx_lock);
597 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
599 dev_err(&hif_dev->udev->dev,
600 "ath9k_htc: RX memory allocation error\n");
603 skb_reserve(nskb, 32);
604 RX_STAT_INC(skb_allocated);
606 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
607 skb_put(nskb, pkt_len);
608 skb_pool[pool_index++] = nskb;
613 for (i = 0; i < pool_index; i++) {
614 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
615 skb_pool[i]->len, USB_WLAN_RX_PIPE);
616 RX_STAT_INC(skb_completed);
620 static void ath9k_hif_usb_rx_cb(struct urb *urb)
622 struct sk_buff *skb = (struct sk_buff *) urb->context;
623 struct hif_device_usb *hif_dev =
624 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
633 switch (urb->status) {
645 if (likely(urb->actual_length != 0)) {
646 skb_put(skb, urb->actual_length);
647 ath9k_hif_usb_rx_stream(hif_dev, skb);
651 skb_reset_tail_pointer(skb);
654 usb_anchor_urb(urb, &hif_dev->rx_submitted);
655 ret = usb_submit_urb(urb, GFP_ATOMIC);
657 usb_unanchor_urb(urb);
666 static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
668 struct sk_buff *skb = (struct sk_buff *) urb->context;
669 struct sk_buff *nskb;
670 struct hif_device_usb *hif_dev =
671 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
680 switch (urb->status) {
689 skb_reset_tail_pointer(skb);
695 if (likely(urb->actual_length != 0)) {
696 skb_put(skb, urb->actual_length);
698 /* Process the command first */
699 ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
700 skb->len, USB_REG_IN_PIPE);
703 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
705 dev_err(&hif_dev->udev->dev,
706 "ath9k_htc: REG_IN memory allocation failure\n");
711 usb_fill_bulk_urb(urb, hif_dev->udev,
712 usb_rcvbulkpipe(hif_dev->udev,
714 nskb->data, MAX_REG_IN_BUF_SIZE,
715 ath9k_hif_usb_reg_in_cb, nskb);
719 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
720 ret = usb_submit_urb(urb, GFP_ATOMIC);
722 usb_unanchor_urb(urb);
732 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
734 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
737 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
738 &hif_dev->tx.tx_buf, list) {
739 usb_kill_urb(tx_buf->urb);
740 list_del(&tx_buf->list);
741 usb_free_urb(tx_buf->urb);
746 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
747 hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
748 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
750 list_for_each_entry_safe(tx_buf, tx_buf_tmp,
751 &hif_dev->tx.tx_pending, list) {
752 usb_kill_urb(tx_buf->urb);
753 list_del(&tx_buf->list);
754 usb_free_urb(tx_buf->urb);
759 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
762 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
764 struct tx_buf *tx_buf;
767 INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
768 INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
769 spin_lock_init(&hif_dev->tx.tx_lock);
770 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
771 init_usb_anchor(&hif_dev->mgmt_submitted);
773 for (i = 0; i < MAX_TX_URB_NUM; i++) {
774 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
778 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
782 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
786 tx_buf->hif_dev = hif_dev;
787 __skb_queue_head_init(&tx_buf->skb_queue);
789 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
792 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
800 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
804 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
806 usb_kill_anchored_urbs(&hif_dev->rx_submitted);
809 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
811 struct urb *urb = NULL;
812 struct sk_buff *skb = NULL;
815 init_usb_anchor(&hif_dev->rx_submitted);
816 spin_lock_init(&hif_dev->rx_lock);
818 for (i = 0; i < MAX_RX_URB_NUM; i++) {
821 urb = usb_alloc_urb(0, GFP_KERNEL);
827 /* Allocate buffer */
828 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
834 usb_fill_bulk_urb(urb, hif_dev->udev,
835 usb_rcvbulkpipe(hif_dev->udev,
837 skb->data, MAX_RX_BUF_SIZE,
838 ath9k_hif_usb_rx_cb, skb);
841 usb_anchor_urb(urb, &hif_dev->rx_submitted);
844 ret = usb_submit_urb(urb, GFP_KERNEL);
846 usb_unanchor_urb(urb);
851 * Drop reference count.
852 * This ensures that the URB is freed when killing them.
864 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
868 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
870 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
873 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
875 struct urb *urb = NULL;
876 struct sk_buff *skb = NULL;
879 init_usb_anchor(&hif_dev->reg_in_submitted);
881 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
884 urb = usb_alloc_urb(0, GFP_KERNEL);
890 /* Allocate buffer */
891 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
897 usb_fill_bulk_urb(urb, hif_dev->udev,
898 usb_rcvbulkpipe(hif_dev->udev,
900 skb->data, MAX_REG_IN_BUF_SIZE,
901 ath9k_hif_usb_reg_in_cb, skb);
904 usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
907 ret = usb_submit_urb(urb, GFP_KERNEL);
909 usb_unanchor_urb(urb);
914 * Drop reference count.
915 * This ensures that the URB is freed when killing them.
927 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
931 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
934 init_usb_anchor(&hif_dev->regout_submitted);
937 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
941 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
945 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
950 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
952 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
957 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
959 usb_kill_anchored_urbs(&hif_dev->regout_submitted);
960 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
961 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
962 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
965 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev,
969 const void *data = hif_dev->firmware->data;
970 size_t len = hif_dev->firmware->size;
971 u32 addr = AR9271_FIRMWARE;
972 u8 *buf = kzalloc(4096, GFP_KERNEL);
979 transfer = min_t(int, len, 4096);
980 memcpy(buf, data, transfer);
982 err = usb_control_msg(hif_dev->udev,
983 usb_sndctrlpipe(hif_dev->udev, 0),
984 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
985 addr >> 8, 0, buf, transfer, HZ);
997 if (IS_AR7010_DEVICE(drv_info))
998 firm_offset = AR7010_FIRMWARE_TEXT;
1000 firm_offset = AR9271_FIRMWARE_TEXT;
1003 * Issue FW download complete command to firmware.
1005 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
1006 FIRMWARE_DOWNLOAD_COMP,
1008 firm_offset >> 8, 0, NULL, 0, HZ);
1012 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
1013 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size);
1018 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info)
1021 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0];
1022 struct usb_endpoint_descriptor *endp;
1024 /* Request firmware */
1025 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name,
1026 &hif_dev->udev->dev);
1028 dev_err(&hif_dev->udev->dev,
1029 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name);
1033 /* Download firmware */
1034 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info);
1036 dev_err(&hif_dev->udev->dev,
1037 "ath9k_htc: Firmware - %s download failed\n",
1039 goto err_fw_download;
1042 /* On downloading the firmware to the target, the USB descriptor of EP4
1043 * is 'patched' to change the type of the endpoint to Bulk. This will
1044 * bring down CPU usage during the scan period.
1046 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
1047 endp = &alt->endpoint[idx].desc;
1048 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
1049 == USB_ENDPOINT_XFER_INT) {
1050 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK;
1051 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK;
1052 endp->bInterval = 0;
1057 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1059 dev_err(&hif_dev->udev->dev,
1060 "ath9k_htc: Unable to allocate URBs\n");
1061 goto err_fw_download;
1067 release_firmware(hif_dev->firmware);
1069 hif_dev->firmware = NULL;
1073 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
1075 ath9k_hif_usb_dealloc_urbs(hif_dev);
1076 if (hif_dev->firmware)
1077 release_firmware(hif_dev->firmware);
1081 * An exact copy of the function from zd1211rw.
1083 static int send_eject_command(struct usb_interface *interface)
1085 struct usb_device *udev = interface_to_usbdev(interface);
1086 struct usb_host_interface *iface_desc = &interface->altsetting[0];
1087 struct usb_endpoint_descriptor *endpoint;
1092 /* Find bulk out endpoint */
1093 for (r = 1; r >= 0; r--) {
1094 endpoint = &iface_desc->endpoint[r].desc;
1095 if (usb_endpoint_dir_out(endpoint) &&
1096 usb_endpoint_xfer_bulk(endpoint)) {
1097 bulk_out_ep = endpoint->bEndpointAddress;
1103 "ath9k_htc: Could not find bulk out endpoint\n");
1107 cmd = kzalloc(31, GFP_KERNEL);
1111 /* USB bulk command block */
1112 cmd[0] = 0x55; /* bulk command signature */
1113 cmd[1] = 0x53; /* bulk command signature */
1114 cmd[2] = 0x42; /* bulk command signature */
1115 cmd[3] = 0x43; /* bulk command signature */
1116 cmd[14] = 6; /* command length */
1118 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
1119 cmd[19] = 0x2; /* eject disc */
1121 dev_info(&udev->dev, "Ejecting storage device...\n");
1122 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1123 cmd, 31, NULL, 2000);
1128 /* At this point, the device disconnects and reconnects with the real
1131 usb_set_intfdata(interface, NULL);
1135 static int ath9k_hif_usb_probe(struct usb_interface *interface,
1136 const struct usb_device_id *id)
1138 struct usb_device *udev = interface_to_usbdev(interface);
1139 struct hif_device_usb *hif_dev;
1142 if (id->driver_info == STORAGE_DEVICE)
1143 return send_eject_command(interface);
1145 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
1152 hif_dev->udev = udev;
1153 hif_dev->interface = interface;
1154 hif_dev->device_id = id->idProduct;
1156 udev->reset_resume = 1;
1158 usb_set_intfdata(interface, hif_dev);
1160 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
1161 &hif_dev->udev->dev);
1162 if (hif_dev->htc_handle == NULL) {
1164 goto err_htc_hw_alloc;
1167 /* Find out which firmware to load */
1169 if (IS_AR7010_DEVICE(id->driver_info))
1170 hif_dev->fw_name = FIRMWARE_AR7010_1_1;
1172 hif_dev->fw_name = FIRMWARE_AR9271;
1174 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info);
1177 goto err_hif_init_usb;
1180 ret = ath9k_htc_hw_init(hif_dev->htc_handle,
1181 &interface->dev, hif_dev->device_id,
1182 hif_dev->udev->product, id->driver_info);
1185 goto err_htc_hw_init;
1188 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
1193 ath9k_hif_usb_dev_deinit(hif_dev);
1195 ath9k_htc_hw_free(hif_dev->htc_handle);
1197 usb_set_intfdata(interface, NULL);
1204 static void ath9k_hif_usb_reboot(struct usb_device *udev)
1206 u32 reboot_cmd = 0xffffffff;
1210 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
1214 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE),
1217 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
1222 static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
1224 struct usb_device *udev = interface_to_usbdev(interface);
1225 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1226 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
1231 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
1232 ath9k_htc_hw_free(hif_dev->htc_handle);
1233 ath9k_hif_usb_dev_deinit(hif_dev);
1234 usb_set_intfdata(interface, NULL);
1236 if (!unplugged && (hif_dev->flags & HIF_USB_START))
1237 ath9k_hif_usb_reboot(udev);
1240 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
1245 static int ath9k_hif_usb_suspend(struct usb_interface *interface,
1246 pm_message_t message)
1248 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1251 * The device has to be set to FULLSLEEP mode in case no
1254 if (!(hif_dev->flags & HIF_USB_START))
1255 ath9k_htc_suspend(hif_dev->htc_handle);
1257 ath9k_hif_usb_dealloc_urbs(hif_dev);
1262 static int ath9k_hif_usb_resume(struct usb_interface *interface)
1264 struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
1265 struct htc_target *htc_handle = hif_dev->htc_handle;
1268 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1272 if (hif_dev->firmware) {
1273 ret = ath9k_hif_usb_download_fw(hif_dev,
1274 htc_handle->drv_priv->ah->hw_version.usbdev);
1278 ath9k_hif_usb_dealloc_urbs(hif_dev);
1284 ret = ath9k_htc_resume(htc_handle);
1292 ath9k_hif_usb_dealloc_urbs(hif_dev);
1298 static struct usb_driver ath9k_hif_usb_driver = {
1299 .name = KBUILD_MODNAME,
1300 .probe = ath9k_hif_usb_probe,
1301 .disconnect = ath9k_hif_usb_disconnect,
1303 .suspend = ath9k_hif_usb_suspend,
1304 .resume = ath9k_hif_usb_resume,
1305 .reset_resume = ath9k_hif_usb_resume,
1307 .id_table = ath9k_hif_usb_ids,
1311 int ath9k_hif_usb_init(void)
1313 return usb_register(&ath9k_hif_usb_driver);
1316 void ath9k_hif_usb_exit(void)
1318 usb_deregister(&ath9k_hif_usb_driver);