17 #include <asm/unaligned.h>
21 #define FIRMWARE_AR7010_1_1 "htc_7010.fw"
22 #define FIRMWARE_AR9271 "htc_9271.fw"
28 { USB_DEVICE(0x0cf3, 0x9271) },
29 { USB_DEVICE(0x0cf3, 0x1006) },
30 { USB_DEVICE(0x0846, 0x9030) },
31 { USB_DEVICE(0x07D1, 0x3A10) },
32 { USB_DEVICE(0x13D3, 0x3327) },
33 { USB_DEVICE(0x13D3, 0x3328) },
34 { USB_DEVICE(0x13D3, 0x3346) },
35 { USB_DEVICE(0x13D3, 0x3348) },
36 { USB_DEVICE(0x13D3, 0x3349) },
37 { USB_DEVICE(0x13D3, 0x3350) },
38 { USB_DEVICE(0x04CA, 0x4605) },
39 { USB_DEVICE(0x040D, 0x3801) },
40 { USB_DEVICE(0x0cf3, 0xb003) },
41 { USB_DEVICE(0x0cf3, 0xb002) },
42 { USB_DEVICE(0x057c, 0x8403) },
44 { USB_DEVICE(0x0cf3, 0x7015),
46 { USB_DEVICE(0x1668, 0x1200),
49 { USB_DEVICE(0x0cf3, 0x7010),
51 { USB_DEVICE(0x0846, 0x9018),
53 { USB_DEVICE(0x083A, 0xA704),
55 { USB_DEVICE(0x0411, 0x017f),
57 { USB_DEVICE(0x04da, 0x3904),
60 { USB_DEVICE(0x0cf3, 0x20ff),
70 static void hif_usb_regout_cb(
struct urb *
urb)
74 switch (urb->status) {
118 usb_fill_bulk_urb(urb, hif_dev->
udev,
121 hif_usb_regout_cb, cmd);
134 static void hif_usb_mgmt_cb(
struct urb *urb)
145 switch (urb->status) {
158 spin_lock(&hif_dev->
tx.tx_lock);
160 spin_unlock(&hif_dev->
tx.tx_lock);
165 spin_unlock(&hif_dev->
tx.tx_lock);
204 usb_fill_bulk_urb(urb, hif_dev->
udev,
207 hif_usb_mgmt_cb, cmd);
220 static inline void ath9k_skb_queue_purge(
struct hif_device_usb *hif_dev,
225 while ((skb = __skb_dequeue(list)) !=
NULL) {
230 static inline void ath9k_skb_queue_complete(
struct hif_device_usb *hif_dev,
236 while ((skb = __skb_dequeue(queue)) !=
NULL) {
246 static void hif_usb_tx_cb(
struct urb *urb)
257 switch (urb->status) {
270 spin_lock(&hif_dev->
tx.tx_lock);
272 spin_unlock(&hif_dev->
tx.tx_lock);
273 ath9k_skb_queue_purge(hif_dev, &tx_buf->
skb_queue);
276 spin_unlock(&hif_dev->
tx.tx_lock);
284 ath9k_skb_queue_complete(hif_dev, &tx_buf->
skb_queue, txok);
288 __skb_queue_head_init(&tx_buf->
skb_queue);
291 spin_lock(&hif_dev->
tx.tx_lock);
292 list_move_tail(&tx_buf->
list, &hif_dev->
tx.tx_buf);
293 hif_dev->
tx.tx_buf_cnt++;
295 __hif_usb_tx(hif_dev);
297 spin_unlock(&hif_dev->
tx.tx_lock);
303 struct tx_buf *tx_buf =
NULL;
310 if (hif_dev->
tx.tx_skb_cnt == 0)
314 if (list_empty(&hif_dev->
tx.tx_buf))
318 list_move_tail(&tx_buf->
list, &hif_dev->
tx.tx_pending);
319 hif_dev->
tx.tx_buf_cnt--;
323 for (
i = 0;
i < tx_skb_cnt;
i++) {
324 nskb = __skb_dequeue(&hif_dev->
tx.tx_skb_queue);
329 hif_dev->
tx.tx_skb_cnt--;
338 tx_buf->
len = nskb->
len + 4;
340 if (
i < (tx_skb_cnt - 1))
341 tx_buf->
offset += (((tx_buf->
len - 1) / 4) + 1) * 4;
343 if (
i == (tx_skb_cnt - 1))
346 __skb_queue_tail(&tx_buf->
skb_queue, nskb);
350 usb_fill_bulk_urb(tx_buf->
urb, hif_dev->
udev,
353 hif_usb_tx_cb, tx_buf);
358 ath9k_skb_queue_complete(hif_dev, &tx_buf->
skb_queue,
false);
359 __skb_queue_head_init(&tx_buf->
skb_queue);
360 list_move_tail(&tx_buf->
list, &hif_dev->
tx.tx_buf);
361 hif_dev->
tx.tx_buf_cnt++;
379 spin_unlock_irqrestore(&hif_dev->
tx.tx_lock, flags);
385 spin_unlock_irqrestore(&hif_dev->
tx.tx_lock, flags);
389 spin_unlock_irqrestore(&hif_dev->
tx.tx_lock, flags);
391 tx_ctl = HTC_SKB_CB(skb);
396 ret = hif_usb_send_mgmt(hif_dev, skb);
403 __skb_queue_tail(&hif_dev->
tx.tx_skb_queue, skb);
404 hif_dev->
tx.tx_skb_cnt++;
409 (hif_dev->
tx.tx_skb_cnt < 2)) {
410 __hif_usb_tx(hif_dev);
413 spin_unlock_irqrestore(&hif_dev->
tx.tx_lock, flags);
418 static void hif_usb_start(
void *hif_handle)
427 spin_unlock_irqrestore(&hif_dev->
tx.tx_lock, flags);
430 static void hif_usb_stop(
void *hif_handle)
433 struct tx_buf *tx_buf =
NULL, *tx_buf_tmp =
NULL;
437 ath9k_skb_queue_complete(hif_dev, &hif_dev->
tx.tx_skb_queue,
false);
438 hif_dev->
tx.tx_skb_cnt = 0;
440 spin_unlock_irqrestore(&hif_dev->
tx.tx_lock, flags);
444 &hif_dev->
tx.tx_pending, list) {
451 static int hif_usb_send(
void *hif_handle,
u8 pipe_id,
struct sk_buff *skb)
458 ret = hif_usb_send_tx(hif_dev, skb);
461 ret = hif_usb_send_regout(hif_dev, skb);
465 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
473 static inline bool check_index(
struct sk_buff *skb,
u8 idx)
477 tx_ctl = HTC_SKB_CB(skb);
486 static void hif_usb_sta_drain(
void *hif_handle,
u8 idx)
494 skb_queue_walk_safe(&hif_dev->
tx.tx_skb_queue, skb, tmp) {
495 if (check_index(skb, idx)) {
496 __skb_unlink(skb, &hif_dev->
tx.tx_skb_queue);
499 hif_dev->
tx.tx_skb_cnt--;
504 spin_unlock_irqrestore(&hif_dev->
tx.tx_lock, flags);
509 .name =
"ath9k_hif_usb",
514 .start = hif_usb_start,
515 .stop = hif_usb_stop,
516 .sta_drain = hif_usb_sta_drain,
517 .send = hif_usb_send,
520 static void ath9k_hif_usb_rx_stream(
struct hif_device_usb *hif_dev,
525 int rx_remain_len, rx_pkt_len;
534 if (rx_remain_len != 0) {
538 ptr = (
u8 *) remain_skb->
data;
540 index = rx_remain_len;
546 rx_pkt_len += rx_remain_len;
548 skb_put(remain_skb, rx_pkt_len);
550 skb_pool[pool_index++] = remain_skb;
553 index = rx_remain_len;
557 spin_unlock(&hif_dev->
rx_lock);
559 while (index <
len) {
567 pkt_len = get_unaligned_le16(ptr + index);
568 pkt_tag = get_unaligned_le16(ptr + index + 2);
575 pad_len = 4 - (pkt_len & 0x3);
580 index = index + 4 + pkt_len +
pad_len;
586 MAX_RX_BUF_SIZE - chk_idx - 4;
589 nskb = __dev_alloc_skb(pkt_len + 32,
GFP_ATOMIC);
592 "ath9k_htc: RX memory allocation error\n");
593 spin_unlock(&hif_dev->
rx_lock);
596 skb_reserve(nskb, 32);
604 spin_unlock(&hif_dev->
rx_lock);
606 nskb = __dev_alloc_skb(pkt_len + 32,
GFP_ATOMIC);
609 "ath9k_htc: RX memory allocation error\n");
612 skb_reserve(nskb, 32);
617 skb_pool[pool_index++] = nskb;
629 static void ath9k_hif_usb_rx_cb(
struct urb *urb)
642 switch (urb->status) {
654 if (
likely(urb->actual_length != 0)) {
655 skb_put(skb, urb->actual_length);
656 ath9k_hif_usb_rx_stream(hif_dev, skb);
660 skb_reset_tail_pointer(skb);
675 static void ath9k_hif_usb_reg_in_cb(
struct urb *urb)
689 switch (urb->status) {
698 skb_reset_tail_pointer(skb);
704 if (
likely(urb->actual_length != 0)) {
705 skb_put(skb, urb->actual_length);
715 "ath9k_htc: REG_IN memory allocation failure\n");
720 usb_fill_bulk_urb(urb, hif_dev->
udev,
721 usb_rcvbulkpipe(hif_dev->
udev,
724 ath9k_hif_usb_reg_in_cb, nskb);
741 static void ath9k_hif_usb_dealloc_tx_urbs(
struct hif_device_usb *hif_dev)
743 struct tx_buf *tx_buf =
NULL, *tx_buf_tmp =
NULL;
747 &hif_dev->
tx.tx_buf, list) {
757 spin_unlock_irqrestore(&hif_dev->
tx.tx_lock, flags);
760 &hif_dev->
tx.tx_pending, list) {
771 static int ath9k_hif_usb_alloc_tx_urbs(
struct hif_device_usb *hif_dev)
773 struct tx_buf *tx_buf;
776 INIT_LIST_HEAD(&hif_dev->
tx.tx_buf);
777 INIT_LIST_HEAD(&hif_dev->
tx.tx_pending);
779 __skb_queue_head_init(&hif_dev->
tx.tx_skb_queue);
783 tx_buf = kzalloc(
sizeof(
struct tx_buf),
GFP_KERNEL);
796 __skb_queue_head_init(&tx_buf->
skb_queue);
809 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
813 static void ath9k_hif_usb_dealloc_rx_urbs(
struct hif_device_usb *hif_dev)
818 static int ath9k_hif_usb_alloc_rx_urbs(
struct hif_device_usb *hif_dev)
820 struct urb *urb =
NULL;
843 usb_fill_bulk_urb(urb, hif_dev->
udev,
844 usb_rcvbulkpipe(hif_dev->
udev,
846 skb->
data, MAX_RX_BUF_SIZE,
847 ath9k_hif_usb_rx_cb, skb);
873 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
877 static void ath9k_hif_usb_dealloc_reg_in_urbs(
struct hif_device_usb *hif_dev)
882 static int ath9k_hif_usb_alloc_reg_in_urbs(
struct hif_device_usb *hif_dev)
884 struct urb *urb =
NULL;
906 usb_fill_bulk_urb(urb, hif_dev->
udev,
907 usb_rcvbulkpipe(hif_dev->
udev,
910 ath9k_hif_usb_reg_in_cb, skb);
936 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
940 static int ath9k_hif_usb_alloc_urbs(
struct hif_device_usb *hif_dev)
946 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
950 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
954 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
959 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
961 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
966 static void ath9k_hif_usb_dealloc_urbs(
struct hif_device_usb *hif_dev)
969 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
970 ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
971 ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
974 static int ath9k_hif_usb_download_fw(
struct hif_device_usb *hif_dev)
987 transfer =
min_t(
size_t, len, 4096);
988 memcpy(buf, data, transfer);
991 usb_sndctrlpipe(hif_dev->
udev, 0),
993 addr >> 8, 0, buf, transfer,
HZ);
1016 firm_offset >> 8, 0,
NULL, 0,
HZ);
1020 dev_info(&hif_dev->
udev->dev,
"ath9k_htc: Transferred FW: %s, size: %ld\n",
1026 static int ath9k_hif_usb_dev_init(
struct hif_device_usb *hif_dev)
1028 struct usb_host_interface *alt = &hif_dev->
interface->altsetting[0];
1032 ret = ath9k_hif_usb_download_fw(hif_dev);
1035 "ath9k_htc: Firmware - %s download failed\n",
1044 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) {
1045 endp = &alt->endpoint[
idx].desc;
1055 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1058 "ath9k_htc: Unable to allocate URBs\n");
1065 static void ath9k_hif_usb_dev_deinit(
struct hif_device_usb *hif_dev)
1067 ath9k_hif_usb_dealloc_urbs(hif_dev);
1074 static void ath9k_hif_usb_firmware_fail(
struct hif_device_usb *hif_dev)
1082 device_lock(parent);
1087 device_unlock(parent);
1097 "ath9k_htc: Failed to get firmware %s\n",
1103 &hif_dev->
udev->dev);
1112 ret = ath9k_hif_usb_dev_init(hif_dev);
1119 hif_dev->
udev->product,
1123 goto err_htc_hw_init;
1133 ath9k_hif_usb_dev_deinit(hif_dev);
1139 ath9k_hif_usb_firmware_fail(hif_dev);
1147 struct usb_device *
udev = interface_to_usbdev(interface);
1148 struct usb_host_interface *iface_desc = &interface->altsetting[0];
1155 for (r = 1; r >= 0; r--) {
1156 endpoint = &iface_desc->endpoint[
r].desc;
1157 if (usb_endpoint_dir_out(endpoint) &&
1158 usb_endpoint_xfer_bulk(endpoint)) {
1165 "ath9k_htc: Could not find bulk out endpoint\n");
1183 dev_info(&udev->dev,
"Ejecting storage device...\n");
1184 r =
usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
1185 cmd, 31,
NULL, 2000);
1193 usb_set_intfdata(interface,
NULL);
1197 static int ath9k_hif_usb_probe(
struct usb_interface *interface,
1200 struct usb_device *udev = interface_to_usbdev(interface);
1205 return send_eject_command(interface);
1219 udev->reset_resume = 1;
1221 usb_set_intfdata(interface, hif_dev);
1223 init_completion(&hif_dev->
fw_done);
1234 hif_dev, ath9k_hif_usb_firmware_cb);
1237 "ath9k_htc: Async request for firmware %s failed\n",
1242 dev_info(&hif_dev->
udev->dev,
"ath9k_htc: Firmware %s requested\n",
1248 usb_set_intfdata(interface,
NULL);
1255 static void ath9k_hif_usb_reboot(
struct usb_device *udev)
1257 u32 reboot_cmd = 0xffffffff;
1268 dev_err(&udev->dev,
"ath9k_htc: USB reboot failed\n");
1273 static void ath9k_hif_usb_disconnect(
struct usb_interface *interface)
1275 struct usb_device *udev = interface_to_usbdev(interface);
1287 ath9k_hif_usb_dev_deinit(hif_dev);
1290 usb_set_intfdata(interface,
NULL);
1293 ath9k_hif_usb_reboot(udev);
1296 dev_info(&udev->dev,
"ath9k_htc: USB layer deinitialized\n");
1301 static int ath9k_hif_usb_suspend(
struct usb_interface *interface,
1313 ath9k_hif_usb_dealloc_urbs(hif_dev);
1318 static int ath9k_hif_usb_resume(
struct usb_interface *interface)
1325 ret = ath9k_hif_usb_alloc_urbs(hif_dev);
1332 &hif_dev->
udev->dev);
1338 ret = ath9k_hif_usb_download_fw(hif_dev);
1343 ath9k_hif_usb_dealloc_urbs(hif_dev);
1349 ret = ath9k_htc_resume(htc_handle);
1357 ath9k_hif_usb_dealloc_urbs(hif_dev);
1363 static struct usb_driver ath9k_hif_usb_driver = {
1364 .name = KBUILD_MODNAME,
1365 .probe = ath9k_hif_usb_probe,
1366 .disconnect = ath9k_hif_usb_disconnect,
1368 .suspend = ath9k_hif_usb_suspend,
1369 .resume = ath9k_hif_usb_resume,
1370 .reset_resume = ath9k_hif_usb_resume,
1372 .id_table = ath9k_hif_usb_ids,
1374 .disable_hub_initiated_lpm = 1,
1379 return usb_register(&ath9k_hif_usb_driver);