Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1 | /* |
Sujith Manoharan | 5b68138 | 2011-05-17 13:36:18 +0530 | [diff] [blame] | 2 | * Copyright (c) 2010-2011 Atheros Communications Inc. |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 3 | * |
| 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. |
| 7 | * |
| 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. |
| 15 | */ |
| 16 | |
Pavel Roskin | 78fa99a | 2011-07-15 19:06:33 -0400 | [diff] [blame] | 17 | #include <asm/unaligned.h> |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 18 | #include "htc.h" |
| 19 | |
John W. Linville | d0ee0eb | 2010-06-23 14:20:45 -0400 | [diff] [blame] | 20 | /* identify firmware images */ |
Sujith Manoharan | ce18f39 | 2011-04-13 11:22:42 +0530 | [diff] [blame] | 21 | #define FIRMWARE_AR7010_1_1 "htc_7010.fw" |
| 22 | #define FIRMWARE_AR9271 "htc_9271.fw" |
John W. Linville | d0ee0eb | 2010-06-23 14:20:45 -0400 | [diff] [blame] | 23 | |
John W. Linville | d0ee0eb | 2010-06-23 14:20:45 -0400 | [diff] [blame] | 24 | MODULE_FIRMWARE(FIRMWARE_AR7010_1_1); |
| 25 | MODULE_FIRMWARE(FIRMWARE_AR9271); |
| 26 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 27 | static struct usb_device_id ath9k_hif_usb_ids[] = { |
Sujith | 4e63f76 | 2010-06-17 10:29:01 +0530 | [diff] [blame] | 28 | { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */ |
| 29 | { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */ |
Sujith | 4e63f76 | 2010-06-17 10:29:01 +0530 | [diff] [blame] | 30 | { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */ |
Sujith | 4e63f76 | 2010-06-17 10:29:01 +0530 | [diff] [blame] | 31 | { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */ |
| 32 | { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */ |
| 33 | { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */ |
Haitao Zhang | ac618d7 | 2010-11-07 12:50:24 +0800 | [diff] [blame] | 34 | { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */ |
Rajkumar Manoharan | 32b0895 | 2010-11-10 17:51:24 +0530 | [diff] [blame] | 35 | { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */ |
| 36 | { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */ |
| 37 | { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */ |
Sujith | 4e63f76 | 2010-06-17 10:29:01 +0530 | [diff] [blame] | 38 | { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */ |
Rajkumar Manoharan | 32b0895 | 2010-11-10 17:51:24 +0530 | [diff] [blame] | 39 | { USB_DEVICE(0x040D, 0x3801) }, /* VIA */ |
Sujith Manoharan | 452d7dd | 2010-12-13 07:39:32 +0530 | [diff] [blame] | 40 | { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */ |
Luis R. Rodriguez | 8c34559 | 2011-09-29 10:42:19 -0700 | [diff] [blame^] | 41 | { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */ |
Sujith Manoharan | 0b5ead9 | 2010-12-07 16:31:38 +0530 | [diff] [blame] | 42 | |
| 43 | { USB_DEVICE(0x0cf3, 0x7015), |
| 44 | .driver_info = AR9287_USB }, /* Atheros */ |
Rajkumar Manoharan | 64f1217 | 2010-11-19 16:53:20 +0530 | [diff] [blame] | 45 | { USB_DEVICE(0x1668, 0x1200), |
Sujith Manoharan | 0b5ead9 | 2010-12-07 16:31:38 +0530 | [diff] [blame] | 46 | .driver_info = AR9287_USB }, /* Verizon */ |
| 47 | |
| 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 */ |
Mohammed Shafi Shajakhan | 5cf6fa7 | 2011-06-28 20:13:41 +0530 | [diff] [blame] | 54 | { USB_DEVICE(0x0411, 0x017f), |
| 55 | .driver_info = AR9280_USB }, /* Sony UWA-BR100 */ |
Sujith Manoharan | 0b5ead9 | 2010-12-07 16:31:38 +0530 | [diff] [blame] | 56 | |
Sujith Manoharan | 36bcce4 | 2011-02-21 07:47:52 +0530 | [diff] [blame] | 57 | { USB_DEVICE(0x0cf3, 0x20ff), |
| 58 | .driver_info = STORAGE_DEVICE }, |
| 59 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 60 | { }, |
| 61 | }; |
| 62 | |
| 63 | MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids); |
| 64 | |
| 65 | static int __hif_usb_tx(struct hif_device_usb *hif_dev); |
| 66 | |
| 67 | static void hif_usb_regout_cb(struct urb *urb) |
| 68 | { |
| 69 | struct cmd_buf *cmd = (struct cmd_buf *)urb->context; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 70 | |
| 71 | switch (urb->status) { |
| 72 | case 0: |
| 73 | break; |
| 74 | case -ENOENT: |
| 75 | case -ECONNRESET: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 76 | case -ENODEV: |
| 77 | case -ESHUTDOWN: |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 78 | goto free; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 79 | default: |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | if (cmd) { |
| 84 | ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle, |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 85 | cmd->skb, true); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 86 | kfree(cmd); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 87 | } |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 88 | |
| 89 | return; |
| 90 | free: |
Ming Lei | 0fa35a5 | 2010-04-13 00:29:15 +0800 | [diff] [blame] | 91 | kfree_skb(cmd->skb); |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 92 | kfree(cmd); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int hif_usb_send_regout(struct hif_device_usb *hif_dev, |
| 96 | struct sk_buff *skb) |
| 97 | { |
| 98 | struct urb *urb; |
| 99 | struct cmd_buf *cmd; |
| 100 | int ret = 0; |
| 101 | |
| 102 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 103 | if (urb == NULL) |
| 104 | return -ENOMEM; |
| 105 | |
| 106 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 107 | if (cmd == NULL) { |
| 108 | usb_free_urb(urb); |
| 109 | return -ENOMEM; |
| 110 | } |
| 111 | |
| 112 | cmd->skb = skb; |
| 113 | cmd->hif_dev = hif_dev; |
| 114 | |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 115 | usb_fill_bulk_urb(urb, hif_dev->udev, |
| 116 | usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE), |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 117 | skb->data, skb->len, |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 118 | hif_usb_regout_cb, cmd); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 119 | |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 120 | usb_anchor_urb(urb, &hif_dev->regout_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 121 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 122 | if (ret) { |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 123 | usb_unanchor_urb(urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 124 | kfree(cmd); |
| 125 | } |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 126 | usb_free_urb(urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 127 | |
| 128 | return ret; |
| 129 | } |
| 130 | |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 131 | static void hif_usb_mgmt_cb(struct urb *urb) |
| 132 | { |
| 133 | struct cmd_buf *cmd = (struct cmd_buf *)urb->context; |
Rajkumar Manoharan | 02c5172 | 2011-07-17 11:38:49 +0530 | [diff] [blame] | 134 | struct hif_device_usb *hif_dev; |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 135 | bool txok = true; |
| 136 | |
| 137 | if (!cmd || !cmd->skb || !cmd->hif_dev) |
| 138 | return; |
| 139 | |
Rajkumar Manoharan | 02c5172 | 2011-07-17 11:38:49 +0530 | [diff] [blame] | 140 | hif_dev = cmd->hif_dev; |
| 141 | |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 142 | switch (urb->status) { |
| 143 | case 0: |
| 144 | break; |
| 145 | case -ENOENT: |
| 146 | case -ECONNRESET: |
| 147 | case -ENODEV: |
| 148 | case -ESHUTDOWN: |
| 149 | txok = false; |
| 150 | |
| 151 | /* |
| 152 | * If the URBs are being flushed, no need to complete |
| 153 | * this packet. |
| 154 | */ |
| 155 | spin_lock(&hif_dev->tx.tx_lock); |
| 156 | if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) { |
| 157 | spin_unlock(&hif_dev->tx.tx_lock); |
| 158 | dev_kfree_skb_any(cmd->skb); |
| 159 | kfree(cmd); |
| 160 | return; |
| 161 | } |
| 162 | spin_unlock(&hif_dev->tx.tx_lock); |
| 163 | |
| 164 | break; |
| 165 | default: |
| 166 | txok = false; |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | skb_pull(cmd->skb, 4); |
| 171 | ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle, |
| 172 | cmd->skb, txok); |
| 173 | kfree(cmd); |
| 174 | } |
| 175 | |
| 176 | static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev, |
| 177 | struct sk_buff *skb) |
| 178 | { |
| 179 | struct urb *urb; |
| 180 | struct cmd_buf *cmd; |
| 181 | int ret = 0; |
| 182 | __le16 *hdr; |
| 183 | |
| 184 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 185 | if (urb == NULL) |
| 186 | return -ENOMEM; |
| 187 | |
| 188 | cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC); |
| 189 | if (cmd == NULL) { |
| 190 | usb_free_urb(urb); |
| 191 | return -ENOMEM; |
| 192 | } |
| 193 | |
| 194 | cmd->skb = skb; |
| 195 | cmd->hif_dev = hif_dev; |
| 196 | |
| 197 | hdr = (__le16 *) skb_push(skb, 4); |
| 198 | *hdr++ = cpu_to_le16(skb->len - 4); |
| 199 | *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG); |
| 200 | |
| 201 | usb_fill_bulk_urb(urb, hif_dev->udev, |
| 202 | usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE), |
| 203 | skb->data, skb->len, |
| 204 | hif_usb_mgmt_cb, cmd); |
| 205 | |
| 206 | usb_anchor_urb(urb, &hif_dev->mgmt_submitted); |
| 207 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
| 208 | if (ret) { |
| 209 | usb_unanchor_urb(urb); |
| 210 | kfree(cmd); |
| 211 | } |
| 212 | usb_free_urb(urb); |
| 213 | |
| 214 | return ret; |
| 215 | } |
| 216 | |
Sujith | eac8e38 | 2010-04-16 11:54:00 +0530 | [diff] [blame] | 217 | static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev, |
| 218 | struct sk_buff_head *list) |
Ming Lei | f8e1d08 | 2010-04-13 00:29:27 +0800 | [diff] [blame] | 219 | { |
| 220 | struct sk_buff *skb; |
Sujith | eac8e38 | 2010-04-16 11:54:00 +0530 | [diff] [blame] | 221 | |
| 222 | while ((skb = __skb_dequeue(list)) != NULL) { |
Ming Lei | f8e1d08 | 2010-04-13 00:29:27 +0800 | [diff] [blame] | 223 | dev_kfree_skb_any(skb); |
Sujith Manoharan | b587fc8 | 2011-04-13 11:25:59 +0530 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
| 227 | static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev, |
| 228 | struct sk_buff_head *queue, |
| 229 | bool txok) |
| 230 | { |
| 231 | struct sk_buff *skb; |
| 232 | |
| 233 | while ((skb = __skb_dequeue(queue)) != NULL) { |
| 234 | ath9k_htc_txcompletion_cb(hif_dev->htc_handle, |
| 235 | skb, txok); |
John W. Linville | dfa8fc6 | 2011-04-14 10:38:22 -0400 | [diff] [blame] | 236 | if (txok) |
| 237 | TX_STAT_INC(skb_success); |
| 238 | else |
| 239 | TX_STAT_INC(skb_failed); |
Sujith | eac8e38 | 2010-04-16 11:54:00 +0530 | [diff] [blame] | 240 | } |
Ming Lei | f8e1d08 | 2010-04-13 00:29:27 +0800 | [diff] [blame] | 241 | } |
| 242 | |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 243 | static void hif_usb_tx_cb(struct urb *urb) |
| 244 | { |
| 245 | struct tx_buf *tx_buf = (struct tx_buf *) urb->context; |
Dan Carpenter | 690e781 | 2010-05-14 16:50:56 +0200 | [diff] [blame] | 246 | struct hif_device_usb *hif_dev; |
Sujith Manoharan | b587fc8 | 2011-04-13 11:25:59 +0530 | [diff] [blame] | 247 | bool txok = true; |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 248 | |
Dan Carpenter | 690e781 | 2010-05-14 16:50:56 +0200 | [diff] [blame] | 249 | if (!tx_buf || !tx_buf->hif_dev) |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 250 | return; |
| 251 | |
Dan Carpenter | 690e781 | 2010-05-14 16:50:56 +0200 | [diff] [blame] | 252 | hif_dev = tx_buf->hif_dev; |
| 253 | |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 254 | switch (urb->status) { |
| 255 | case 0: |
| 256 | break; |
| 257 | case -ENOENT: |
| 258 | case -ECONNRESET: |
| 259 | case -ENODEV: |
| 260 | case -ESHUTDOWN: |
Sujith Manoharan | b587fc8 | 2011-04-13 11:25:59 +0530 | [diff] [blame] | 261 | txok = false; |
Sujith Manoharan | ff8f59b | 2010-12-28 14:28:05 +0530 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * If the URBs are being flushed, no need to add this |
| 265 | * URB to the free list. |
| 266 | */ |
| 267 | spin_lock(&hif_dev->tx.tx_lock); |
| 268 | if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) { |
| 269 | spin_unlock(&hif_dev->tx.tx_lock); |
Sujith Manoharan | b587fc8 | 2011-04-13 11:25:59 +0530 | [diff] [blame] | 270 | ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); |
Sujith Manoharan | ff8f59b | 2010-12-28 14:28:05 +0530 | [diff] [blame] | 271 | return; |
| 272 | } |
| 273 | spin_unlock(&hif_dev->tx.tx_lock); |
| 274 | |
Sujith Manoharan | b587fc8 | 2011-04-13 11:25:59 +0530 | [diff] [blame] | 275 | break; |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 276 | default: |
Sujith Manoharan | b587fc8 | 2011-04-13 11:25:59 +0530 | [diff] [blame] | 277 | txok = false; |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 278 | break; |
| 279 | } |
| 280 | |
Sujith Manoharan | b587fc8 | 2011-04-13 11:25:59 +0530 | [diff] [blame] | 281 | ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok); |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 282 | |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 283 | /* Re-initialize the SKB queue */ |
| 284 | tx_buf->len = tx_buf->offset = 0; |
| 285 | __skb_queue_head_init(&tx_buf->skb_queue); |
| 286 | |
| 287 | /* Add this TX buffer to the free list */ |
| 288 | spin_lock(&hif_dev->tx.tx_lock); |
| 289 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); |
| 290 | hif_dev->tx.tx_buf_cnt++; |
| 291 | if (!(hif_dev->tx.flags & HIF_USB_TX_STOP)) |
| 292 | __hif_usb_tx(hif_dev); /* Check for pending SKBs */ |
| 293 | TX_STAT_INC(buf_completed); |
| 294 | spin_unlock(&hif_dev->tx.tx_lock); |
| 295 | } |
| 296 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 297 | /* TX lock has to be taken */ |
| 298 | static int __hif_usb_tx(struct hif_device_usb *hif_dev) |
| 299 | { |
| 300 | struct tx_buf *tx_buf = NULL; |
| 301 | struct sk_buff *nskb = NULL; |
| 302 | int ret = 0, i; |
Sujith Manoharan | 2c27392 | 2011-02-27 09:23:52 +0530 | [diff] [blame] | 303 | u16 tx_skb_cnt = 0; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 304 | u8 *buf; |
Sujith Manoharan | 2c27392 | 2011-02-27 09:23:52 +0530 | [diff] [blame] | 305 | __le16 *hdr; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 306 | |
| 307 | if (hif_dev->tx.tx_skb_cnt == 0) |
| 308 | return 0; |
| 309 | |
| 310 | /* Check if a free TX buffer is available */ |
| 311 | if (list_empty(&hif_dev->tx.tx_buf)) |
| 312 | return 0; |
| 313 | |
| 314 | tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list); |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 315 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 316 | hif_dev->tx.tx_buf_cnt--; |
| 317 | |
| 318 | tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM); |
| 319 | |
| 320 | for (i = 0; i < tx_skb_cnt; i++) { |
| 321 | nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue); |
| 322 | |
| 323 | /* Should never be NULL */ |
| 324 | BUG_ON(!nskb); |
| 325 | |
| 326 | hif_dev->tx.tx_skb_cnt--; |
| 327 | |
| 328 | buf = tx_buf->buf; |
| 329 | buf += tx_buf->offset; |
Sujith Manoharan | 2c27392 | 2011-02-27 09:23:52 +0530 | [diff] [blame] | 330 | hdr = (__le16 *)buf; |
| 331 | *hdr++ = cpu_to_le16(nskb->len); |
| 332 | *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 333 | buf += 4; |
| 334 | memcpy(buf, nskb->data, nskb->len); |
| 335 | tx_buf->len = nskb->len + 4; |
| 336 | |
| 337 | if (i < (tx_skb_cnt - 1)) |
| 338 | tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4; |
| 339 | |
| 340 | if (i == (tx_skb_cnt - 1)) |
| 341 | tx_buf->len += tx_buf->offset; |
| 342 | |
| 343 | __skb_queue_tail(&tx_buf->skb_queue, nskb); |
| 344 | TX_STAT_INC(skb_queued); |
| 345 | } |
| 346 | |
| 347 | usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev, |
| 348 | usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE), |
| 349 | tx_buf->buf, tx_buf->len, |
| 350 | hif_usb_tx_cb, tx_buf); |
| 351 | |
| 352 | ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC); |
| 353 | if (ret) { |
| 354 | tx_buf->len = tx_buf->offset = 0; |
Sujith Manoharan | b587fc8 | 2011-04-13 11:25:59 +0530 | [diff] [blame] | 355 | ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 356 | __skb_queue_head_init(&tx_buf->skb_queue); |
| 357 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); |
| 358 | hif_dev->tx.tx_buf_cnt++; |
| 359 | } |
| 360 | |
| 361 | if (!ret) |
| 362 | TX_STAT_INC(buf_queued); |
| 363 | |
| 364 | return ret; |
| 365 | } |
| 366 | |
Sujith Manoharan | 40dc9e4 | 2011-04-13 11:24:31 +0530 | [diff] [blame] | 367 | static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 368 | { |
Sujith Manoharan | 40dc9e4 | 2011-04-13 11:24:31 +0530 | [diff] [blame] | 369 | struct ath9k_htc_tx_ctl *tx_ctl; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 370 | unsigned long flags; |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 371 | int ret = 0; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 372 | |
| 373 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
| 374 | |
| 375 | if (hif_dev->tx.flags & HIF_USB_TX_STOP) { |
| 376 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 377 | return -ENODEV; |
| 378 | } |
| 379 | |
| 380 | /* Check if the max queue count has been reached */ |
| 381 | if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) { |
| 382 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 383 | return -ENOMEM; |
| 384 | } |
| 385 | |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 386 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 387 | |
Sujith Manoharan | 40dc9e4 | 2011-04-13 11:24:31 +0530 | [diff] [blame] | 388 | tx_ctl = HTC_SKB_CB(skb); |
| 389 | |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 390 | /* Mgmt/Beacon frames don't use the TX buffer pool */ |
| 391 | if ((tx_ctl->type == ATH9K_HTC_MGMT) || |
| 392 | (tx_ctl->type == ATH9K_HTC_BEACON)) { |
| 393 | ret = hif_usb_send_mgmt(hif_dev, skb); |
| 394 | } |
| 395 | |
| 396 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
| 397 | |
| 398 | if ((tx_ctl->type == ATH9K_HTC_NORMAL) || |
| 399 | (tx_ctl->type == ATH9K_HTC_AMPDU)) { |
| 400 | __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb); |
| 401 | hif_dev->tx.tx_skb_cnt++; |
| 402 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 403 | |
| 404 | /* Check if AMPDUs have to be sent immediately */ |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 405 | if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) && |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 406 | (hif_dev->tx.tx_skb_cnt < 2)) { |
| 407 | __hif_usb_tx(hif_dev); |
| 408 | } |
| 409 | |
| 410 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 411 | |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 412 | return ret; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 413 | } |
| 414 | |
Sujith Manoharan | e1fe7c3 | 2011-04-13 11:26:06 +0530 | [diff] [blame] | 415 | static void hif_usb_start(void *hif_handle) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 416 | { |
| 417 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; |
| 418 | unsigned long flags; |
| 419 | |
| 420 | hif_dev->flags |= HIF_USB_START; |
| 421 | |
| 422 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
| 423 | hif_dev->tx.flags &= ~HIF_USB_TX_STOP; |
| 424 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 425 | } |
| 426 | |
Sujith Manoharan | e1fe7c3 | 2011-04-13 11:26:06 +0530 | [diff] [blame] | 427 | static void hif_usb_stop(void *hif_handle) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 428 | { |
| 429 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; |
Sujith Manoharan | ff8f59b | 2010-12-28 14:28:05 +0530 | [diff] [blame] | 430 | struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 431 | unsigned long flags; |
| 432 | |
| 433 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
Sujith Manoharan | b587fc8 | 2011-04-13 11:25:59 +0530 | [diff] [blame] | 434 | ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 435 | hif_dev->tx.tx_skb_cnt = 0; |
| 436 | hif_dev->tx.flags |= HIF_USB_TX_STOP; |
| 437 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
Sujith Manoharan | ff8f59b | 2010-12-28 14:28:05 +0530 | [diff] [blame] | 438 | |
| 439 | /* The pending URBs have to be canceled. */ |
| 440 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, |
| 441 | &hif_dev->tx.tx_pending, list) { |
| 442 | usb_kill_urb(tx_buf->urb); |
| 443 | } |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 444 | |
| 445 | usb_kill_anchored_urbs(&hif_dev->mgmt_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 446 | } |
| 447 | |
Sujith Manoharan | 40dc9e4 | 2011-04-13 11:24:31 +0530 | [diff] [blame] | 448 | static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 449 | { |
| 450 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; |
| 451 | int ret = 0; |
| 452 | |
| 453 | switch (pipe_id) { |
| 454 | case USB_WLAN_TX_PIPE: |
Sujith Manoharan | 40dc9e4 | 2011-04-13 11:24:31 +0530 | [diff] [blame] | 455 | ret = hif_usb_send_tx(hif_dev, skb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 456 | break; |
| 457 | case USB_REG_OUT_PIPE: |
| 458 | ret = hif_usb_send_regout(hif_dev, skb); |
| 459 | break; |
| 460 | default: |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 461 | dev_err(&hif_dev->udev->dev, |
| 462 | "ath9k_htc: Invalid TX pipe: %d\n", pipe_id); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 463 | ret = -EINVAL; |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | return ret; |
| 468 | } |
| 469 | |
Sujith Manoharan | 84c9e164 | 2011-04-13 11:26:11 +0530 | [diff] [blame] | 470 | static inline bool check_index(struct sk_buff *skb, u8 idx) |
| 471 | { |
| 472 | struct ath9k_htc_tx_ctl *tx_ctl; |
| 473 | |
| 474 | tx_ctl = HTC_SKB_CB(skb); |
| 475 | |
| 476 | if ((tx_ctl->type == ATH9K_HTC_AMPDU) && |
| 477 | (tx_ctl->sta_idx == idx)) |
| 478 | return true; |
| 479 | |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | static void hif_usb_sta_drain(void *hif_handle, u8 idx) |
| 484 | { |
| 485 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; |
| 486 | struct sk_buff *skb, *tmp; |
| 487 | unsigned long flags; |
| 488 | |
| 489 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
| 490 | |
| 491 | skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) { |
| 492 | if (check_index(skb, idx)) { |
| 493 | __skb_unlink(skb, &hif_dev->tx.tx_skb_queue); |
| 494 | ath9k_htc_txcompletion_cb(hif_dev->htc_handle, |
| 495 | skb, false); |
| 496 | hif_dev->tx.tx_skb_cnt--; |
| 497 | TX_STAT_INC(skb_failed); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 502 | } |
| 503 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 504 | static struct ath9k_htc_hif hif_usb = { |
| 505 | .transport = ATH9K_HIF_USB, |
| 506 | .name = "ath9k_hif_usb", |
| 507 | |
| 508 | .control_ul_pipe = USB_REG_OUT_PIPE, |
| 509 | .control_dl_pipe = USB_REG_IN_PIPE, |
| 510 | |
| 511 | .start = hif_usb_start, |
| 512 | .stop = hif_usb_stop, |
Sujith Manoharan | 84c9e164 | 2011-04-13 11:26:11 +0530 | [diff] [blame] | 513 | .sta_drain = hif_usb_sta_drain, |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 514 | .send = hif_usb_send, |
| 515 | }; |
| 516 | |
| 517 | static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev, |
| 518 | struct sk_buff *skb) |
| 519 | { |
Sujith | c503269 | 2010-04-06 15:28:15 +0530 | [diff] [blame] | 520 | struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER]; |
Joe Perches | 44b23b4 | 2010-11-30 12:19:11 -0800 | [diff] [blame] | 521 | int index = 0, i = 0, len = skb->len; |
| 522 | int rx_remain_len, rx_pkt_len; |
| 523 | u16 pool_index = 0; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 524 | u8 *ptr; |
| 525 | |
Sujith | 46baa1a | 2010-04-06 15:28:11 +0530 | [diff] [blame] | 526 | spin_lock(&hif_dev->rx_lock); |
| 527 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 528 | rx_remain_len = hif_dev->rx_remain_len; |
| 529 | rx_pkt_len = hif_dev->rx_transfer_len; |
| 530 | |
| 531 | if (rx_remain_len != 0) { |
| 532 | struct sk_buff *remain_skb = hif_dev->remain_skb; |
| 533 | |
| 534 | if (remain_skb) { |
| 535 | ptr = (u8 *) remain_skb->data; |
| 536 | |
| 537 | index = rx_remain_len; |
| 538 | rx_remain_len -= hif_dev->rx_pad_len; |
| 539 | ptr += rx_pkt_len; |
| 540 | |
| 541 | memcpy(ptr, skb->data, rx_remain_len); |
| 542 | |
| 543 | rx_pkt_len += rx_remain_len; |
| 544 | hif_dev->rx_remain_len = 0; |
| 545 | skb_put(remain_skb, rx_pkt_len); |
| 546 | |
| 547 | skb_pool[pool_index++] = remain_skb; |
| 548 | |
| 549 | } else { |
| 550 | index = rx_remain_len; |
| 551 | } |
| 552 | } |
| 553 | |
Sujith | 46baa1a | 2010-04-06 15:28:11 +0530 | [diff] [blame] | 554 | spin_unlock(&hif_dev->rx_lock); |
| 555 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 556 | while (index < len) { |
Joe Perches | 44b23b4 | 2010-11-30 12:19:11 -0800 | [diff] [blame] | 557 | u16 pkt_len; |
| 558 | u16 pkt_tag; |
| 559 | u16 pad_len; |
| 560 | int chk_idx; |
| 561 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 562 | ptr = (u8 *) skb->data; |
| 563 | |
Pavel Roskin | 78fa99a | 2011-07-15 19:06:33 -0400 | [diff] [blame] | 564 | pkt_len = get_unaligned_le16(ptr + index); |
| 565 | pkt_tag = get_unaligned_le16(ptr + index + 2); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 566 | |
Joe Perches | 44b23b4 | 2010-11-30 12:19:11 -0800 | [diff] [blame] | 567 | if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) { |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 568 | RX_STAT_INC(skb_dropped); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 569 | return; |
| 570 | } |
Joe Perches | 44b23b4 | 2010-11-30 12:19:11 -0800 | [diff] [blame] | 571 | |
| 572 | pad_len = 4 - (pkt_len & 0x3); |
| 573 | if (pad_len == 4) |
| 574 | pad_len = 0; |
| 575 | |
| 576 | chk_idx = index; |
| 577 | index = index + 4 + pkt_len + pad_len; |
| 578 | |
| 579 | if (index > MAX_RX_BUF_SIZE) { |
| 580 | spin_lock(&hif_dev->rx_lock); |
| 581 | hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE; |
| 582 | hif_dev->rx_transfer_len = |
| 583 | MAX_RX_BUF_SIZE - chk_idx - 4; |
| 584 | hif_dev->rx_pad_len = pad_len; |
| 585 | |
| 586 | nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); |
| 587 | if (!nskb) { |
| 588 | dev_err(&hif_dev->udev->dev, |
| 589 | "ath9k_htc: RX memory allocation error\n"); |
| 590 | spin_unlock(&hif_dev->rx_lock); |
| 591 | goto err; |
| 592 | } |
| 593 | skb_reserve(nskb, 32); |
| 594 | RX_STAT_INC(skb_allocated); |
| 595 | |
| 596 | memcpy(nskb->data, &(skb->data[chk_idx+4]), |
| 597 | hif_dev->rx_transfer_len); |
| 598 | |
| 599 | /* Record the buffer pointer */ |
| 600 | hif_dev->remain_skb = nskb; |
| 601 | spin_unlock(&hif_dev->rx_lock); |
| 602 | } else { |
| 603 | nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); |
| 604 | if (!nskb) { |
| 605 | dev_err(&hif_dev->udev->dev, |
| 606 | "ath9k_htc: RX memory allocation error\n"); |
| 607 | goto err; |
| 608 | } |
| 609 | skb_reserve(nskb, 32); |
| 610 | RX_STAT_INC(skb_allocated); |
| 611 | |
| 612 | memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len); |
| 613 | skb_put(nskb, pkt_len); |
| 614 | skb_pool[pool_index++] = nskb; |
| 615 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | err: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 619 | for (i = 0; i < pool_index; i++) { |
| 620 | ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i], |
| 621 | skb_pool[i]->len, USB_WLAN_RX_PIPE); |
| 622 | RX_STAT_INC(skb_completed); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | static void ath9k_hif_usb_rx_cb(struct urb *urb) |
| 627 | { |
| 628 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
Joe Perches | b276736 | 2010-11-30 13:42:08 -0800 | [diff] [blame] | 629 | struct hif_device_usb *hif_dev = |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 630 | usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); |
| 631 | int ret; |
| 632 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 633 | if (!skb) |
| 634 | return; |
| 635 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 636 | if (!hif_dev) |
| 637 | goto free; |
| 638 | |
| 639 | switch (urb->status) { |
| 640 | case 0: |
| 641 | break; |
| 642 | case -ENOENT: |
| 643 | case -ECONNRESET: |
| 644 | case -ENODEV: |
| 645 | case -ESHUTDOWN: |
| 646 | goto free; |
| 647 | default: |
| 648 | goto resubmit; |
| 649 | } |
| 650 | |
| 651 | if (likely(urb->actual_length != 0)) { |
| 652 | skb_put(skb, urb->actual_length); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 653 | ath9k_hif_usb_rx_stream(hif_dev, skb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | resubmit: |
| 657 | skb_reset_tail_pointer(skb); |
| 658 | skb_trim(skb, 0); |
| 659 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 660 | usb_anchor_urb(urb, &hif_dev->rx_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 661 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 662 | if (ret) { |
| 663 | usb_unanchor_urb(urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 664 | goto free; |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 665 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 666 | |
| 667 | return; |
| 668 | free: |
Ming Lei | f28a7b3 | 2010-04-13 00:28:53 +0800 | [diff] [blame] | 669 | kfree_skb(skb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | static void ath9k_hif_usb_reg_in_cb(struct urb *urb) |
| 673 | { |
| 674 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 675 | struct sk_buff *nskb; |
Joe Perches | b276736 | 2010-11-30 13:42:08 -0800 | [diff] [blame] | 676 | struct hif_device_usb *hif_dev = |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 677 | usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); |
| 678 | int ret; |
| 679 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 680 | if (!skb) |
| 681 | return; |
| 682 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 683 | if (!hif_dev) |
| 684 | goto free; |
| 685 | |
| 686 | switch (urb->status) { |
| 687 | case 0: |
| 688 | break; |
| 689 | case -ENOENT: |
| 690 | case -ECONNRESET: |
| 691 | case -ENODEV: |
| 692 | case -ESHUTDOWN: |
| 693 | goto free; |
| 694 | default: |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 695 | skb_reset_tail_pointer(skb); |
| 696 | skb_trim(skb, 0); |
| 697 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 698 | goto resubmit; |
| 699 | } |
| 700 | |
| 701 | if (likely(urb->actual_length != 0)) { |
| 702 | skb_put(skb, urb->actual_length); |
| 703 | |
Sujith | 5ab0af3 | 2010-04-23 10:28:17 +0530 | [diff] [blame] | 704 | /* Process the command first */ |
| 705 | ath9k_htc_rx_msg(hif_dev->htc_handle, skb, |
| 706 | skb->len, USB_REG_IN_PIPE); |
| 707 | |
| 708 | |
Ming Lei | e6c6d33 | 2010-04-13 00:29:05 +0800 | [diff] [blame] | 709 | nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC); |
Sujith | 5ab0af3 | 2010-04-23 10:28:17 +0530 | [diff] [blame] | 710 | if (!nskb) { |
| 711 | dev_err(&hif_dev->udev->dev, |
| 712 | "ath9k_htc: REG_IN memory allocation failure\n"); |
| 713 | urb->context = NULL; |
| 714 | return; |
| 715 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 716 | |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 717 | usb_fill_bulk_urb(urb, hif_dev->udev, |
Rajkumar Manoharan | 0f529e9 | 2010-09-16 19:56:55 +0530 | [diff] [blame] | 718 | usb_rcvbulkpipe(hif_dev->udev, |
| 719 | USB_REG_IN_PIPE), |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 720 | nskb->data, MAX_REG_IN_BUF_SIZE, |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 721 | ath9k_hif_usb_reg_in_cb, nskb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | resubmit: |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 725 | usb_anchor_urb(urb, &hif_dev->reg_in_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 726 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 727 | if (ret) { |
| 728 | usb_unanchor_urb(urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 729 | goto free; |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 730 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 731 | |
| 732 | return; |
| 733 | free: |
Ming Lei | e6c6d33 | 2010-04-13 00:29:05 +0800 | [diff] [blame] | 734 | kfree_skb(skb); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 735 | urb->context = NULL; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev) |
| 739 | { |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 740 | struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; |
Sujith Manoharan | ff8f59b | 2010-12-28 14:28:05 +0530 | [diff] [blame] | 741 | unsigned long flags; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 742 | |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 743 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, |
| 744 | &hif_dev->tx.tx_buf, list) { |
| 745 | usb_kill_urb(tx_buf->urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 746 | list_del(&tx_buf->list); |
| 747 | usb_free_urb(tx_buf->urb); |
| 748 | kfree(tx_buf->buf); |
| 749 | kfree(tx_buf); |
| 750 | } |
| 751 | |
Sujith Manoharan | ff8f59b | 2010-12-28 14:28:05 +0530 | [diff] [blame] | 752 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
| 753 | hif_dev->tx.flags |= HIF_USB_TX_FLUSH; |
| 754 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 755 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 756 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, |
| 757 | &hif_dev->tx.tx_pending, list) { |
| 758 | usb_kill_urb(tx_buf->urb); |
| 759 | list_del(&tx_buf->list); |
| 760 | usb_free_urb(tx_buf->urb); |
| 761 | kfree(tx_buf->buf); |
| 762 | kfree(tx_buf); |
| 763 | } |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 764 | |
| 765 | usb_kill_anchored_urbs(&hif_dev->mgmt_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev) |
| 769 | { |
| 770 | struct tx_buf *tx_buf; |
| 771 | int i; |
| 772 | |
| 773 | INIT_LIST_HEAD(&hif_dev->tx.tx_buf); |
| 774 | INIT_LIST_HEAD(&hif_dev->tx.tx_pending); |
| 775 | spin_lock_init(&hif_dev->tx.tx_lock); |
| 776 | __skb_queue_head_init(&hif_dev->tx.tx_skb_queue); |
Sujith Manoharan | 2f80194 | 2011-04-13 11:26:46 +0530 | [diff] [blame] | 777 | init_usb_anchor(&hif_dev->mgmt_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 778 | |
| 779 | for (i = 0; i < MAX_TX_URB_NUM; i++) { |
| 780 | tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL); |
| 781 | if (!tx_buf) |
| 782 | goto err; |
| 783 | |
| 784 | tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL); |
| 785 | if (!tx_buf->buf) |
| 786 | goto err; |
| 787 | |
| 788 | tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 789 | if (!tx_buf->urb) |
| 790 | goto err; |
| 791 | |
| 792 | tx_buf->hif_dev = hif_dev; |
| 793 | __skb_queue_head_init(&tx_buf->skb_queue); |
| 794 | |
| 795 | list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf); |
| 796 | } |
| 797 | |
| 798 | hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM; |
| 799 | |
| 800 | return 0; |
| 801 | err: |
Dan Carpenter | 7606688 | 2010-05-14 16:52:37 +0200 | [diff] [blame] | 802 | if (tx_buf) { |
| 803 | kfree(tx_buf->buf); |
| 804 | kfree(tx_buf); |
| 805 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 806 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); |
| 807 | return -ENOMEM; |
| 808 | } |
| 809 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 810 | static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev) |
| 811 | { |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 812 | usb_kill_anchored_urbs(&hif_dev->rx_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev) |
| 816 | { |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 817 | struct urb *urb = NULL; |
| 818 | struct sk_buff *skb = NULL; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 819 | int i, ret; |
| 820 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 821 | init_usb_anchor(&hif_dev->rx_submitted); |
Sujith | 46baa1a | 2010-04-06 15:28:11 +0530 | [diff] [blame] | 822 | spin_lock_init(&hif_dev->rx_lock); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 823 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 824 | for (i = 0; i < MAX_RX_URB_NUM; i++) { |
| 825 | |
| 826 | /* Allocate URB */ |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 827 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 828 | if (urb == NULL) { |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 829 | ret = -ENOMEM; |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 830 | goto err_urb; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /* Allocate buffer */ |
Ming Lei | f28a7b3 | 2010-04-13 00:28:53 +0800 | [diff] [blame] | 834 | skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 835 | if (!skb) { |
| 836 | ret = -ENOMEM; |
| 837 | goto err_skb; |
| 838 | } |
| 839 | |
| 840 | usb_fill_bulk_urb(urb, hif_dev->udev, |
| 841 | usb_rcvbulkpipe(hif_dev->udev, |
| 842 | USB_WLAN_RX_PIPE), |
| 843 | skb->data, MAX_RX_BUF_SIZE, |
| 844 | ath9k_hif_usb_rx_cb, skb); |
| 845 | |
| 846 | /* Anchor URB */ |
| 847 | usb_anchor_urb(urb, &hif_dev->rx_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 848 | |
| 849 | /* Submit URB */ |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 850 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 851 | if (ret) { |
| 852 | usb_unanchor_urb(urb); |
| 853 | goto err_submit; |
| 854 | } |
Sujith | 66b10e3 | 2010-04-06 15:28:13 +0530 | [diff] [blame] | 855 | |
| 856 | /* |
| 857 | * Drop reference count. |
| 858 | * This ensures that the URB is freed when killing them. |
| 859 | */ |
| 860 | usb_free_urb(urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | return 0; |
| 864 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 865 | err_submit: |
Ming Lei | f28a7b3 | 2010-04-13 00:28:53 +0800 | [diff] [blame] | 866 | kfree_skb(skb); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 867 | err_skb: |
| 868 | usb_free_urb(urb); |
| 869 | err_urb: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 870 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); |
| 871 | return ret; |
| 872 | } |
| 873 | |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 874 | static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 875 | { |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 876 | usb_kill_anchored_urbs(&hif_dev->reg_in_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 877 | } |
| 878 | |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 879 | static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 880 | { |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 881 | struct urb *urb = NULL; |
| 882 | struct sk_buff *skb = NULL; |
| 883 | int i, ret; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 884 | |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 885 | init_usb_anchor(&hif_dev->reg_in_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 886 | |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 887 | for (i = 0; i < MAX_REG_IN_URB_NUM; i++) { |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 888 | |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 889 | /* Allocate URB */ |
| 890 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 891 | if (urb == NULL) { |
| 892 | ret = -ENOMEM; |
| 893 | goto err_urb; |
| 894 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 895 | |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 896 | /* Allocate buffer */ |
| 897 | skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL); |
| 898 | if (!skb) { |
| 899 | ret = -ENOMEM; |
| 900 | goto err_skb; |
| 901 | } |
| 902 | |
| 903 | usb_fill_bulk_urb(urb, hif_dev->udev, |
| 904 | usb_rcvbulkpipe(hif_dev->udev, |
| 905 | USB_REG_IN_PIPE), |
| 906 | skb->data, MAX_REG_IN_BUF_SIZE, |
| 907 | ath9k_hif_usb_reg_in_cb, skb); |
| 908 | |
| 909 | /* Anchor URB */ |
| 910 | usb_anchor_urb(urb, &hif_dev->reg_in_submitted); |
| 911 | |
| 912 | /* Submit URB */ |
| 913 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 914 | if (ret) { |
| 915 | usb_unanchor_urb(urb); |
| 916 | goto err_submit; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | * Drop reference count. |
| 921 | * This ensures that the URB is freed when killing them. |
| 922 | */ |
| 923 | usb_free_urb(urb); |
| 924 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 925 | |
| 926 | return 0; |
| 927 | |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 928 | err_submit: |
| 929 | kfree_skb(skb); |
| 930 | err_skb: |
| 931 | usb_free_urb(urb); |
| 932 | err_urb: |
| 933 | ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev); |
| 934 | return ret; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev) |
| 938 | { |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 939 | /* Register Write */ |
| 940 | init_usb_anchor(&hif_dev->regout_submitted); |
| 941 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 942 | /* TX */ |
| 943 | if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0) |
| 944 | goto err; |
| 945 | |
| 946 | /* RX */ |
| 947 | if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0) |
Rajkumar Manoharan | f803696 | 2010-07-07 15:19:18 +0530 | [diff] [blame] | 948 | goto err_rx; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 949 | |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 950 | /* Register Read */ |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 951 | if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0) |
Rajkumar Manoharan | f803696 | 2010-07-07 15:19:18 +0530 | [diff] [blame] | 952 | goto err_reg; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 953 | |
| 954 | return 0; |
Rajkumar Manoharan | f803696 | 2010-07-07 15:19:18 +0530 | [diff] [blame] | 955 | err_reg: |
| 956 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); |
| 957 | err_rx: |
| 958 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 959 | err: |
| 960 | return -ENOMEM; |
| 961 | } |
| 962 | |
Sujith.Manoharan@atheros.com | 1d8af8c | 2010-05-11 16:24:40 +0530 | [diff] [blame] | 963 | static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev) |
| 964 | { |
| 965 | usb_kill_anchored_urbs(&hif_dev->regout_submitted); |
Sujith Manoharan | 3deff76 | 2011-04-13 11:25:23 +0530 | [diff] [blame] | 966 | ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev); |
Sujith.Manoharan@atheros.com | 1d8af8c | 2010-05-11 16:24:40 +0530 | [diff] [blame] | 967 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); |
| 968 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); |
| 969 | } |
| 970 | |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 971 | static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev, |
| 972 | u32 drv_info) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 973 | { |
| 974 | int transfer, err; |
| 975 | const void *data = hif_dev->firmware->data; |
| 976 | size_t len = hif_dev->firmware->size; |
| 977 | u32 addr = AR9271_FIRMWARE; |
| 978 | u8 *buf = kzalloc(4096, GFP_KERNEL); |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 979 | u32 firm_offset; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 980 | |
| 981 | if (!buf) |
| 982 | return -ENOMEM; |
| 983 | |
| 984 | while (len) { |
| 985 | transfer = min_t(int, len, 4096); |
| 986 | memcpy(buf, data, transfer); |
| 987 | |
| 988 | err = usb_control_msg(hif_dev->udev, |
| 989 | usb_sndctrlpipe(hif_dev->udev, 0), |
| 990 | FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT, |
| 991 | addr >> 8, 0, buf, transfer, HZ); |
| 992 | if (err < 0) { |
| 993 | kfree(buf); |
| 994 | return err; |
| 995 | } |
| 996 | |
| 997 | len -= transfer; |
| 998 | data += transfer; |
| 999 | addr += transfer; |
| 1000 | } |
| 1001 | kfree(buf); |
| 1002 | |
Sujith Manoharan | 0b5ead9 | 2010-12-07 16:31:38 +0530 | [diff] [blame] | 1003 | if (IS_AR7010_DEVICE(drv_info)) |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 1004 | firm_offset = AR7010_FIRMWARE_TEXT; |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 1005 | else |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 1006 | firm_offset = AR9271_FIRMWARE_TEXT; |
| 1007 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1008 | /* |
| 1009 | * Issue FW download complete command to firmware. |
| 1010 | */ |
| 1011 | err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0), |
| 1012 | FIRMWARE_DOWNLOAD_COMP, |
| 1013 | 0x40 | USB_DIR_OUT, |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 1014 | firm_offset >> 8, 0, NULL, 0, HZ); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1015 | if (err) |
| 1016 | return -EIO; |
| 1017 | |
| 1018 | dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n", |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 1019 | hif_dev->fw_name, (unsigned long) hif_dev->firmware->size); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 1024 | static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1025 | { |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 1026 | int ret, idx; |
| 1027 | struct usb_host_interface *alt = &hif_dev->interface->altsetting[0]; |
| 1028 | struct usb_endpoint_descriptor *endp; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1029 | |
| 1030 | /* Request firmware */ |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 1031 | ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name, |
| 1032 | &hif_dev->udev->dev); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1033 | if (ret) { |
| 1034 | dev_err(&hif_dev->udev->dev, |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 1035 | "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1036 | goto err_fw_req; |
| 1037 | } |
| 1038 | |
Sujith.Manoharan@atheros.com | 1d8af8c | 2010-05-11 16:24:40 +0530 | [diff] [blame] | 1039 | /* Download firmware */ |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 1040 | ret = ath9k_hif_usb_download_fw(hif_dev, drv_info); |
Sujith.Manoharan@atheros.com | 1d8af8c | 2010-05-11 16:24:40 +0530 | [diff] [blame] | 1041 | if (ret) { |
| 1042 | dev_err(&hif_dev->udev->dev, |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 1043 | "ath9k_htc: Firmware - %s download failed\n", |
| 1044 | hif_dev->fw_name); |
Sujith.Manoharan@atheros.com | 1d8af8c | 2010-05-11 16:24:40 +0530 | [diff] [blame] | 1045 | goto err_fw_download; |
| 1046 | } |
| 1047 | |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 1048 | /* On downloading the firmware to the target, the USB descriptor of EP4 |
| 1049 | * is 'patched' to change the type of the endpoint to Bulk. This will |
| 1050 | * bring down CPU usage during the scan period. |
| 1051 | */ |
| 1052 | for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) { |
| 1053 | endp = &alt->endpoint[idx].desc; |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 1054 | if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 1055 | == USB_ENDPOINT_XFER_INT) { |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 1056 | endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK; |
| 1057 | endp->bmAttributes |= USB_ENDPOINT_XFER_BULK; |
| 1058 | endp->bInterval = 0; |
| 1059 | } |
| 1060 | } |
| 1061 | |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 1062 | /* Alloc URBs */ |
| 1063 | ret = ath9k_hif_usb_alloc_urbs(hif_dev); |
| 1064 | if (ret) { |
| 1065 | dev_err(&hif_dev->udev->dev, |
| 1066 | "ath9k_htc: Unable to allocate URBs\n"); |
Sujith Manoharan | 512c044 | 2011-02-21 07:50:38 +0530 | [diff] [blame] | 1067 | goto err_fw_download; |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 1068 | } |
| 1069 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1070 | return 0; |
| 1071 | |
Sujith Manoharan | caa0a99 | 2010-12-07 16:32:02 +0530 | [diff] [blame] | 1072 | err_fw_download: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1073 | release_firmware(hif_dev->firmware); |
| 1074 | err_fw_req: |
| 1075 | hif_dev->firmware = NULL; |
| 1076 | return ret; |
| 1077 | } |
| 1078 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1079 | static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev) |
| 1080 | { |
| 1081 | ath9k_hif_usb_dealloc_urbs(hif_dev); |
| 1082 | if (hif_dev->firmware) |
| 1083 | release_firmware(hif_dev->firmware); |
| 1084 | } |
| 1085 | |
Sujith Manoharan | 36bcce4 | 2011-02-21 07:47:52 +0530 | [diff] [blame] | 1086 | /* |
| 1087 | * An exact copy of the function from zd1211rw. |
| 1088 | */ |
| 1089 | static int send_eject_command(struct usb_interface *interface) |
| 1090 | { |
| 1091 | struct usb_device *udev = interface_to_usbdev(interface); |
| 1092 | struct usb_host_interface *iface_desc = &interface->altsetting[0]; |
| 1093 | struct usb_endpoint_descriptor *endpoint; |
| 1094 | unsigned char *cmd; |
| 1095 | u8 bulk_out_ep; |
| 1096 | int r; |
| 1097 | |
| 1098 | /* Find bulk out endpoint */ |
| 1099 | for (r = 1; r >= 0; r--) { |
| 1100 | endpoint = &iface_desc->endpoint[r].desc; |
| 1101 | if (usb_endpoint_dir_out(endpoint) && |
| 1102 | usb_endpoint_xfer_bulk(endpoint)) { |
| 1103 | bulk_out_ep = endpoint->bEndpointAddress; |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
| 1107 | if (r == -1) { |
| 1108 | dev_err(&udev->dev, |
| 1109 | "ath9k_htc: Could not find bulk out endpoint\n"); |
| 1110 | return -ENODEV; |
| 1111 | } |
| 1112 | |
| 1113 | cmd = kzalloc(31, GFP_KERNEL); |
| 1114 | if (cmd == NULL) |
| 1115 | return -ENODEV; |
| 1116 | |
| 1117 | /* USB bulk command block */ |
| 1118 | cmd[0] = 0x55; /* bulk command signature */ |
| 1119 | cmd[1] = 0x53; /* bulk command signature */ |
| 1120 | cmd[2] = 0x42; /* bulk command signature */ |
| 1121 | cmd[3] = 0x43; /* bulk command signature */ |
| 1122 | cmd[14] = 6; /* command length */ |
| 1123 | |
| 1124 | cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ |
| 1125 | cmd[19] = 0x2; /* eject disc */ |
| 1126 | |
| 1127 | dev_info(&udev->dev, "Ejecting storage device...\n"); |
| 1128 | r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), |
| 1129 | cmd, 31, NULL, 2000); |
| 1130 | kfree(cmd); |
| 1131 | if (r) |
| 1132 | return r; |
| 1133 | |
| 1134 | /* At this point, the device disconnects and reconnects with the real |
| 1135 | * ID numbers. */ |
| 1136 | |
| 1137 | usb_set_intfdata(interface, NULL); |
| 1138 | return 0; |
| 1139 | } |
| 1140 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1141 | static int ath9k_hif_usb_probe(struct usb_interface *interface, |
| 1142 | const struct usb_device_id *id) |
| 1143 | { |
| 1144 | struct usb_device *udev = interface_to_usbdev(interface); |
| 1145 | struct hif_device_usb *hif_dev; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1146 | int ret = 0; |
| 1147 | |
Sujith Manoharan | 36bcce4 | 2011-02-21 07:47:52 +0530 | [diff] [blame] | 1148 | if (id->driver_info == STORAGE_DEVICE) |
| 1149 | return send_eject_command(interface); |
| 1150 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1151 | hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL); |
| 1152 | if (!hif_dev) { |
| 1153 | ret = -ENOMEM; |
| 1154 | goto err_alloc; |
| 1155 | } |
| 1156 | |
| 1157 | usb_get_dev(udev); |
| 1158 | hif_dev->udev = udev; |
| 1159 | hif_dev->interface = interface; |
| 1160 | hif_dev->device_id = id->idProduct; |
| 1161 | #ifdef CONFIG_PM |
| 1162 | udev->reset_resume = 1; |
| 1163 | #endif |
| 1164 | usb_set_intfdata(interface, hif_dev); |
| 1165 | |
Sujith.Manoharan@atheros.com | 47fce02 | 2010-05-11 16:24:41 +0530 | [diff] [blame] | 1166 | hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb, |
| 1167 | &hif_dev->udev->dev); |
| 1168 | if (hif_dev->htc_handle == NULL) { |
| 1169 | ret = -ENOMEM; |
| 1170 | goto err_htc_hw_alloc; |
| 1171 | } |
| 1172 | |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 1173 | /* Find out which firmware to load */ |
| 1174 | |
Sujith Manoharan | 0b5ead9 | 2010-12-07 16:31:38 +0530 | [diff] [blame] | 1175 | if (IS_AR7010_DEVICE(id->driver_info)) |
Sujith Manoharan | 9efabad | 2011-04-13 11:22:33 +0530 | [diff] [blame] | 1176 | hif_dev->fw_name = FIRMWARE_AR7010_1_1; |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 1177 | else |
John W. Linville | d0ee0eb | 2010-06-23 14:20:45 -0400 | [diff] [blame] | 1178 | hif_dev->fw_name = FIRMWARE_AR9271; |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 1179 | |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 1180 | ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1181 | if (ret) { |
| 1182 | ret = -EINVAL; |
| 1183 | goto err_hif_init_usb; |
| 1184 | } |
| 1185 | |
Sujith.Manoharan@atheros.com | 47fce02 | 2010-05-11 16:24:41 +0530 | [diff] [blame] | 1186 | ret = ath9k_htc_hw_init(hif_dev->htc_handle, |
Sujith Manoharan | 50f6871 | 2011-04-11 22:56:55 +0530 | [diff] [blame] | 1187 | &interface->dev, hif_dev->device_id, |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 1188 | hif_dev->udev->product, id->driver_info); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1189 | if (ret) { |
| 1190 | ret = -EINVAL; |
| 1191 | goto err_htc_hw_init; |
| 1192 | } |
| 1193 | |
| 1194 | dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n"); |
| 1195 | |
| 1196 | return 0; |
| 1197 | |
| 1198 | err_htc_hw_init: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1199 | ath9k_hif_usb_dev_deinit(hif_dev); |
| 1200 | err_hif_init_usb: |
Sujith.Manoharan@atheros.com | 47fce02 | 2010-05-11 16:24:41 +0530 | [diff] [blame] | 1201 | ath9k_htc_hw_free(hif_dev->htc_handle); |
| 1202 | err_htc_hw_alloc: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1203 | usb_set_intfdata(interface, NULL); |
| 1204 | kfree(hif_dev); |
| 1205 | usb_put_dev(udev); |
| 1206 | err_alloc: |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
Sujith | 62e4716 | 2010-04-23 10:28:16 +0530 | [diff] [blame] | 1210 | static void ath9k_hif_usb_reboot(struct usb_device *udev) |
| 1211 | { |
| 1212 | u32 reboot_cmd = 0xffffffff; |
| 1213 | void *buf; |
| 1214 | int ret; |
| 1215 | |
Julia Lawall | a465a2c | 2010-05-15 23:17:19 +0200 | [diff] [blame] | 1216 | buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL); |
Sujith | 62e4716 | 2010-04-23 10:28:16 +0530 | [diff] [blame] | 1217 | if (!buf) |
| 1218 | return; |
| 1219 | |
Sujith | 62e4716 | 2010-04-23 10:28:16 +0530 | [diff] [blame] | 1220 | ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE), |
| 1221 | buf, 4, NULL, HZ); |
| 1222 | if (ret) |
| 1223 | dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n"); |
| 1224 | |
| 1225 | kfree(buf); |
| 1226 | } |
| 1227 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1228 | static void ath9k_hif_usb_disconnect(struct usb_interface *interface) |
| 1229 | { |
| 1230 | struct usb_device *udev = interface_to_usbdev(interface); |
Joe Perches | b276736 | 2010-11-30 13:42:08 -0800 | [diff] [blame] | 1231 | struct hif_device_usb *hif_dev = usb_get_intfdata(interface); |
Sujith Manoharan | 97dcec5 | 2010-12-20 08:02:42 +0530 | [diff] [blame] | 1232 | bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1233 | |
Sujith Manoharan | 36bcce4 | 2011-02-21 07:47:52 +0530 | [diff] [blame] | 1234 | if (!hif_dev) |
| 1235 | return; |
| 1236 | |
| 1237 | ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged); |
| 1238 | ath9k_htc_hw_free(hif_dev->htc_handle); |
| 1239 | ath9k_hif_usb_dev_deinit(hif_dev); |
| 1240 | usb_set_intfdata(interface, NULL); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1241 | |
Sujith Manoharan | 97dcec5 | 2010-12-20 08:02:42 +0530 | [diff] [blame] | 1242 | if (!unplugged && (hif_dev->flags & HIF_USB_START)) |
Sujith | 62e4716 | 2010-04-23 10:28:16 +0530 | [diff] [blame] | 1243 | ath9k_hif_usb_reboot(udev); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1244 | |
| 1245 | kfree(hif_dev); |
| 1246 | dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n"); |
| 1247 | usb_put_dev(udev); |
| 1248 | } |
| 1249 | |
| 1250 | #ifdef CONFIG_PM |
| 1251 | static int ath9k_hif_usb_suspend(struct usb_interface *interface, |
| 1252 | pm_message_t message) |
| 1253 | { |
Joe Perches | b276736 | 2010-11-30 13:42:08 -0800 | [diff] [blame] | 1254 | struct hif_device_usb *hif_dev = usb_get_intfdata(interface); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1255 | |
Sujith Manoharan | f933ebe | 2010-12-01 12:30:27 +0530 | [diff] [blame] | 1256 | /* |
| 1257 | * The device has to be set to FULLSLEEP mode in case no |
| 1258 | * interface is up. |
| 1259 | */ |
| 1260 | if (!(hif_dev->flags & HIF_USB_START)) |
| 1261 | ath9k_htc_suspend(hif_dev->htc_handle); |
| 1262 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1263 | ath9k_hif_usb_dealloc_urbs(hif_dev); |
| 1264 | |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | static int ath9k_hif_usb_resume(struct usb_interface *interface) |
| 1269 | { |
Joe Perches | b276736 | 2010-11-30 13:42:08 -0800 | [diff] [blame] | 1270 | struct hif_device_usb *hif_dev = usb_get_intfdata(interface); |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 1271 | struct htc_target *htc_handle = hif_dev->htc_handle; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1272 | int ret; |
| 1273 | |
| 1274 | ret = ath9k_hif_usb_alloc_urbs(hif_dev); |
| 1275 | if (ret) |
| 1276 | return ret; |
| 1277 | |
| 1278 | if (hif_dev->firmware) { |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 1279 | ret = ath9k_hif_usb_download_fw(hif_dev, |
Sujith Manoharan | 0b5ead9 | 2010-12-07 16:31:38 +0530 | [diff] [blame] | 1280 | htc_handle->drv_priv->ah->hw_version.usbdev); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1281 | if (ret) |
| 1282 | goto fail_resume; |
| 1283 | } else { |
| 1284 | ath9k_hif_usb_dealloc_urbs(hif_dev); |
| 1285 | return -EIO; |
| 1286 | } |
| 1287 | |
| 1288 | mdelay(100); |
| 1289 | |
Rajkumar Manoharan | fa6e15e | 2010-11-19 16:53:22 +0530 | [diff] [blame] | 1290 | ret = ath9k_htc_resume(htc_handle); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1291 | |
| 1292 | if (ret) |
| 1293 | goto fail_resume; |
| 1294 | |
| 1295 | return 0; |
| 1296 | |
| 1297 | fail_resume: |
| 1298 | ath9k_hif_usb_dealloc_urbs(hif_dev); |
| 1299 | |
| 1300 | return ret; |
| 1301 | } |
| 1302 | #endif |
| 1303 | |
| 1304 | static struct usb_driver ath9k_hif_usb_driver = { |
Sujith Manoharan | 50f6871 | 2011-04-11 22:56:55 +0530 | [diff] [blame] | 1305 | .name = KBUILD_MODNAME, |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1306 | .probe = ath9k_hif_usb_probe, |
| 1307 | .disconnect = ath9k_hif_usb_disconnect, |
| 1308 | #ifdef CONFIG_PM |
| 1309 | .suspend = ath9k_hif_usb_suspend, |
| 1310 | .resume = ath9k_hif_usb_resume, |
| 1311 | .reset_resume = ath9k_hif_usb_resume, |
| 1312 | #endif |
| 1313 | .id_table = ath9k_hif_usb_ids, |
| 1314 | .soft_unbind = 1, |
| 1315 | }; |
| 1316 | |
| 1317 | int ath9k_hif_usb_init(void) |
| 1318 | { |
| 1319 | return usb_register(&ath9k_hif_usb_driver); |
| 1320 | } |
| 1321 | |
| 1322 | void ath9k_hif_usb_exit(void) |
| 1323 | { |
| 1324 | usb_deregister(&ath9k_hif_usb_driver); |
| 1325 | } |