Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010 Atheros Communications Inc. |
| 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 | |
| 17 | #include "htc.h" |
| 18 | |
John W. Linville | d0ee0eb | 2010-06-23 14:20:45 -0400 | [diff] [blame] | 19 | /* identify firmware images */ |
| 20 | #define FIRMWARE_AR7010 "ar7010.fw" |
| 21 | #define FIRMWARE_AR7010_1_1 "ar7010_1_1.fw" |
| 22 | #define FIRMWARE_AR9271 "ar9271.fw" |
| 23 | |
| 24 | MODULE_FIRMWARE(FIRMWARE_AR7010); |
| 25 | MODULE_FIRMWARE(FIRMWARE_AR7010_1_1); |
| 26 | MODULE_FIRMWARE(FIRMWARE_AR9271); |
| 27 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 28 | static struct usb_device_id ath9k_hif_usb_ids[] = { |
Sujith | 4e63f76 | 2010-06-17 10:29:01 +0530 | [diff] [blame] | 29 | { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */ |
| 30 | { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */ |
Rajkumar Manoharan | 64f1217 | 2010-11-19 16:53:20 +0530 | [diff] [blame^] | 31 | { USB_DEVICE(0x0cf3, 0x7010), |
| 32 | .driver_info = AR7010_DEVICE }, |
| 33 | /* Atheros */ |
| 34 | { USB_DEVICE(0x0cf3, 0x7015), |
| 35 | .driver_info = AR7010_DEVICE | AR9287_DEVICE }, |
| 36 | /* Atheros */ |
Sujith | 4e63f76 | 2010-06-17 10:29:01 +0530 | [diff] [blame] | 37 | { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */ |
Rajkumar Manoharan | 64f1217 | 2010-11-19 16:53:20 +0530 | [diff] [blame^] | 38 | { USB_DEVICE(0x0846, 0x9018), |
| 39 | .driver_info = AR7010_DEVICE }, |
| 40 | /* Netgear WNDA3200 */ |
Sujith | 4e63f76 | 2010-06-17 10:29:01 +0530 | [diff] [blame] | 41 | { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */ |
| 42 | { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */ |
| 43 | { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */ |
Haitao Zhang | ac618d7 | 2010-11-07 12:50:24 +0800 | [diff] [blame] | 44 | { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */ |
Rajkumar Manoharan | 32b0895 | 2010-11-10 17:51:24 +0530 | [diff] [blame] | 45 | { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */ |
| 46 | { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */ |
| 47 | { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */ |
Sujith | 4e63f76 | 2010-06-17 10:29:01 +0530 | [diff] [blame] | 48 | { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */ |
Rajkumar Manoharan | 64f1217 | 2010-11-19 16:53:20 +0530 | [diff] [blame^] | 49 | { USB_DEVICE(0x083A, 0xA704), |
| 50 | .driver_info = AR7010_DEVICE }, |
| 51 | /* SMC Networks */ |
Rajkumar Manoharan | 32b0895 | 2010-11-10 17:51:24 +0530 | [diff] [blame] | 52 | { USB_DEVICE(0x040D, 0x3801) }, /* VIA */ |
Rajkumar Manoharan | 64f1217 | 2010-11-19 16:53:20 +0530 | [diff] [blame^] | 53 | { USB_DEVICE(0x1668, 0x1200), |
| 54 | .driver_info = AR7010_DEVICE | AR9287_DEVICE }, |
| 55 | /* Verizon */ |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 56 | { }, |
| 57 | }; |
| 58 | |
| 59 | MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids); |
| 60 | |
| 61 | static int __hif_usb_tx(struct hif_device_usb *hif_dev); |
| 62 | |
| 63 | static void hif_usb_regout_cb(struct urb *urb) |
| 64 | { |
| 65 | struct cmd_buf *cmd = (struct cmd_buf *)urb->context; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 66 | |
| 67 | switch (urb->status) { |
| 68 | case 0: |
| 69 | break; |
| 70 | case -ENOENT: |
| 71 | case -ECONNRESET: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 72 | case -ENODEV: |
| 73 | case -ESHUTDOWN: |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 74 | goto free; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 75 | default: |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | if (cmd) { |
| 80 | ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle, |
| 81 | cmd->skb, 1); |
| 82 | kfree(cmd); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 83 | } |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 84 | |
| 85 | return; |
| 86 | free: |
Ming Lei | 0fa35a5 | 2010-04-13 00:29:15 +0800 | [diff] [blame] | 87 | kfree_skb(cmd->skb); |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 88 | kfree(cmd); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static int hif_usb_send_regout(struct hif_device_usb *hif_dev, |
| 92 | struct sk_buff *skb) |
| 93 | { |
| 94 | struct urb *urb; |
| 95 | struct cmd_buf *cmd; |
| 96 | int ret = 0; |
| 97 | |
| 98 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 99 | if (urb == NULL) |
| 100 | return -ENOMEM; |
| 101 | |
| 102 | cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); |
| 103 | if (cmd == NULL) { |
| 104 | usb_free_urb(urb); |
| 105 | return -ENOMEM; |
| 106 | } |
| 107 | |
| 108 | cmd->skb = skb; |
| 109 | cmd->hif_dev = hif_dev; |
| 110 | |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 111 | usb_fill_bulk_urb(urb, hif_dev->udev, |
| 112 | usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE), |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 113 | skb->data, skb->len, |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 114 | hif_usb_regout_cb, cmd); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 115 | |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 116 | usb_anchor_urb(urb, &hif_dev->regout_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 117 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 118 | if (ret) { |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 119 | usb_unanchor_urb(urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 120 | kfree(cmd); |
| 121 | } |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 122 | usb_free_urb(urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
Sujith | eac8e38 | 2010-04-16 11:54:00 +0530 | [diff] [blame] | 127 | static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev, |
| 128 | struct sk_buff_head *list) |
Ming Lei | f8e1d08 | 2010-04-13 00:29:27 +0800 | [diff] [blame] | 129 | { |
| 130 | struct sk_buff *skb; |
Sujith | eac8e38 | 2010-04-16 11:54:00 +0530 | [diff] [blame] | 131 | |
| 132 | while ((skb = __skb_dequeue(list)) != NULL) { |
Ming Lei | f8e1d08 | 2010-04-13 00:29:27 +0800 | [diff] [blame] | 133 | dev_kfree_skb_any(skb); |
Sujith | eac8e38 | 2010-04-16 11:54:00 +0530 | [diff] [blame] | 134 | TX_STAT_INC(skb_dropped); |
| 135 | } |
Ming Lei | f8e1d08 | 2010-04-13 00:29:27 +0800 | [diff] [blame] | 136 | } |
| 137 | |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 138 | static void hif_usb_tx_cb(struct urb *urb) |
| 139 | { |
| 140 | struct tx_buf *tx_buf = (struct tx_buf *) urb->context; |
Dan Carpenter | 690e781 | 2010-05-14 16:50:56 +0200 | [diff] [blame] | 141 | struct hif_device_usb *hif_dev; |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 142 | struct sk_buff *skb; |
| 143 | |
Dan Carpenter | 690e781 | 2010-05-14 16:50:56 +0200 | [diff] [blame] | 144 | if (!tx_buf || !tx_buf->hif_dev) |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 145 | return; |
| 146 | |
Dan Carpenter | 690e781 | 2010-05-14 16:50:56 +0200 | [diff] [blame] | 147 | hif_dev = tx_buf->hif_dev; |
| 148 | |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 149 | switch (urb->status) { |
| 150 | case 0: |
| 151 | break; |
| 152 | case -ENOENT: |
| 153 | case -ECONNRESET: |
| 154 | case -ENODEV: |
| 155 | case -ESHUTDOWN: |
| 156 | /* |
| 157 | * The URB has been killed, free the SKBs |
| 158 | * and return. |
| 159 | */ |
| 160 | ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); |
| 161 | return; |
| 162 | default: |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | /* Check if TX has been stopped */ |
| 167 | spin_lock(&hif_dev->tx.tx_lock); |
| 168 | if (hif_dev->tx.flags & HIF_USB_TX_STOP) { |
| 169 | spin_unlock(&hif_dev->tx.tx_lock); |
| 170 | ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); |
| 171 | goto add_free; |
| 172 | } |
| 173 | spin_unlock(&hif_dev->tx.tx_lock); |
| 174 | |
| 175 | /* Complete the queued SKBs. */ |
| 176 | while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) { |
| 177 | ath9k_htc_txcompletion_cb(hif_dev->htc_handle, |
| 178 | skb, 1); |
| 179 | TX_STAT_INC(skb_completed); |
| 180 | } |
| 181 | |
| 182 | add_free: |
| 183 | /* Re-initialize the SKB queue */ |
| 184 | tx_buf->len = tx_buf->offset = 0; |
| 185 | __skb_queue_head_init(&tx_buf->skb_queue); |
| 186 | |
| 187 | /* Add this TX buffer to the free list */ |
| 188 | spin_lock(&hif_dev->tx.tx_lock); |
| 189 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); |
| 190 | hif_dev->tx.tx_buf_cnt++; |
| 191 | if (!(hif_dev->tx.flags & HIF_USB_TX_STOP)) |
| 192 | __hif_usb_tx(hif_dev); /* Check for pending SKBs */ |
| 193 | TX_STAT_INC(buf_completed); |
| 194 | spin_unlock(&hif_dev->tx.tx_lock); |
| 195 | } |
| 196 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 197 | /* TX lock has to be taken */ |
| 198 | static int __hif_usb_tx(struct hif_device_usb *hif_dev) |
| 199 | { |
| 200 | struct tx_buf *tx_buf = NULL; |
| 201 | struct sk_buff *nskb = NULL; |
| 202 | int ret = 0, i; |
| 203 | u16 *hdr, tx_skb_cnt = 0; |
| 204 | u8 *buf; |
| 205 | |
| 206 | if (hif_dev->tx.tx_skb_cnt == 0) |
| 207 | return 0; |
| 208 | |
| 209 | /* Check if a free TX buffer is available */ |
| 210 | if (list_empty(&hif_dev->tx.tx_buf)) |
| 211 | return 0; |
| 212 | |
| 213 | 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] | 214 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 215 | hif_dev->tx.tx_buf_cnt--; |
| 216 | |
| 217 | tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM); |
| 218 | |
| 219 | for (i = 0; i < tx_skb_cnt; i++) { |
| 220 | nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue); |
| 221 | |
| 222 | /* Should never be NULL */ |
| 223 | BUG_ON(!nskb); |
| 224 | |
| 225 | hif_dev->tx.tx_skb_cnt--; |
| 226 | |
| 227 | buf = tx_buf->buf; |
| 228 | buf += tx_buf->offset; |
| 229 | hdr = (u16 *)buf; |
| 230 | *hdr++ = nskb->len; |
| 231 | *hdr++ = ATH_USB_TX_STREAM_MODE_TAG; |
| 232 | buf += 4; |
| 233 | memcpy(buf, nskb->data, nskb->len); |
| 234 | tx_buf->len = nskb->len + 4; |
| 235 | |
| 236 | if (i < (tx_skb_cnt - 1)) |
| 237 | tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4; |
| 238 | |
| 239 | if (i == (tx_skb_cnt - 1)) |
| 240 | tx_buf->len += tx_buf->offset; |
| 241 | |
| 242 | __skb_queue_tail(&tx_buf->skb_queue, nskb); |
| 243 | TX_STAT_INC(skb_queued); |
| 244 | } |
| 245 | |
| 246 | usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev, |
| 247 | usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE), |
| 248 | tx_buf->buf, tx_buf->len, |
| 249 | hif_usb_tx_cb, tx_buf); |
| 250 | |
| 251 | ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC); |
| 252 | if (ret) { |
| 253 | tx_buf->len = tx_buf->offset = 0; |
Sujith | eac8e38 | 2010-04-16 11:54:00 +0530 | [diff] [blame] | 254 | ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 255 | __skb_queue_head_init(&tx_buf->skb_queue); |
| 256 | list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); |
| 257 | hif_dev->tx.tx_buf_cnt++; |
| 258 | } |
| 259 | |
| 260 | if (!ret) |
| 261 | TX_STAT_INC(buf_queued); |
| 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb, |
| 267 | struct ath9k_htc_tx_ctl *tx_ctl) |
| 268 | { |
| 269 | unsigned long flags; |
| 270 | |
| 271 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
| 272 | |
| 273 | if (hif_dev->tx.flags & HIF_USB_TX_STOP) { |
| 274 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 275 | return -ENODEV; |
| 276 | } |
| 277 | |
| 278 | /* Check if the max queue count has been reached */ |
| 279 | if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) { |
| 280 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 281 | return -ENOMEM; |
| 282 | } |
| 283 | |
| 284 | __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb); |
| 285 | hif_dev->tx.tx_skb_cnt++; |
| 286 | |
| 287 | /* Send normal frames immediately */ |
| 288 | if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL))) |
| 289 | __hif_usb_tx(hif_dev); |
| 290 | |
| 291 | /* Check if AMPDUs have to be sent immediately */ |
| 292 | if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) && |
| 293 | (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) && |
| 294 | (hif_dev->tx.tx_skb_cnt < 2)) { |
| 295 | __hif_usb_tx(hif_dev); |
| 296 | } |
| 297 | |
| 298 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static void hif_usb_start(void *hif_handle, u8 pipe_id) |
| 304 | { |
| 305 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; |
| 306 | unsigned long flags; |
| 307 | |
| 308 | hif_dev->flags |= HIF_USB_START; |
| 309 | |
| 310 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
| 311 | hif_dev->tx.flags &= ~HIF_USB_TX_STOP; |
| 312 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 313 | } |
| 314 | |
| 315 | static void hif_usb_stop(void *hif_handle, u8 pipe_id) |
| 316 | { |
| 317 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; |
| 318 | unsigned long flags; |
| 319 | |
| 320 | spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); |
Sujith | eac8e38 | 2010-04-16 11:54:00 +0530 | [diff] [blame] | 321 | ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 322 | hif_dev->tx.tx_skb_cnt = 0; |
| 323 | hif_dev->tx.flags |= HIF_USB_TX_STOP; |
| 324 | spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); |
| 325 | } |
| 326 | |
| 327 | static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb, |
| 328 | struct ath9k_htc_tx_ctl *tx_ctl) |
| 329 | { |
| 330 | struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; |
| 331 | int ret = 0; |
| 332 | |
| 333 | switch (pipe_id) { |
| 334 | case USB_WLAN_TX_PIPE: |
| 335 | ret = hif_usb_send_tx(hif_dev, skb, tx_ctl); |
| 336 | break; |
| 337 | case USB_REG_OUT_PIPE: |
| 338 | ret = hif_usb_send_regout(hif_dev, skb); |
| 339 | break; |
| 340 | default: |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 341 | dev_err(&hif_dev->udev->dev, |
| 342 | "ath9k_htc: Invalid TX pipe: %d\n", pipe_id); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 343 | ret = -EINVAL; |
| 344 | break; |
| 345 | } |
| 346 | |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | static struct ath9k_htc_hif hif_usb = { |
| 351 | .transport = ATH9K_HIF_USB, |
| 352 | .name = "ath9k_hif_usb", |
| 353 | |
| 354 | .control_ul_pipe = USB_REG_OUT_PIPE, |
| 355 | .control_dl_pipe = USB_REG_IN_PIPE, |
| 356 | |
| 357 | .start = hif_usb_start, |
| 358 | .stop = hif_usb_stop, |
| 359 | .send = hif_usb_send, |
| 360 | }; |
| 361 | |
| 362 | static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev, |
| 363 | struct sk_buff *skb) |
| 364 | { |
Sujith | c503269 | 2010-04-06 15:28:15 +0530 | [diff] [blame] | 365 | struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER]; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 366 | int index = 0, i = 0, chk_idx, len = skb->len; |
| 367 | int rx_remain_len = 0, rx_pkt_len = 0; |
| 368 | u16 pkt_len, pkt_tag, pool_index = 0; |
| 369 | u8 *ptr; |
| 370 | |
Sujith | 46baa1a | 2010-04-06 15:28:11 +0530 | [diff] [blame] | 371 | spin_lock(&hif_dev->rx_lock); |
| 372 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 373 | rx_remain_len = hif_dev->rx_remain_len; |
| 374 | rx_pkt_len = hif_dev->rx_transfer_len; |
| 375 | |
| 376 | if (rx_remain_len != 0) { |
| 377 | struct sk_buff *remain_skb = hif_dev->remain_skb; |
| 378 | |
| 379 | if (remain_skb) { |
| 380 | ptr = (u8 *) remain_skb->data; |
| 381 | |
| 382 | index = rx_remain_len; |
| 383 | rx_remain_len -= hif_dev->rx_pad_len; |
| 384 | ptr += rx_pkt_len; |
| 385 | |
| 386 | memcpy(ptr, skb->data, rx_remain_len); |
| 387 | |
| 388 | rx_pkt_len += rx_remain_len; |
| 389 | hif_dev->rx_remain_len = 0; |
| 390 | skb_put(remain_skb, rx_pkt_len); |
| 391 | |
| 392 | skb_pool[pool_index++] = remain_skb; |
| 393 | |
| 394 | } else { |
| 395 | index = rx_remain_len; |
| 396 | } |
| 397 | } |
| 398 | |
Sujith | 46baa1a | 2010-04-06 15:28:11 +0530 | [diff] [blame] | 399 | spin_unlock(&hif_dev->rx_lock); |
| 400 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 401 | while (index < len) { |
| 402 | ptr = (u8 *) skb->data; |
| 403 | |
| 404 | pkt_len = ptr[index] + (ptr[index+1] << 8); |
| 405 | pkt_tag = ptr[index+2] + (ptr[index+3] << 8); |
| 406 | |
| 407 | if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) { |
| 408 | u16 pad_len; |
| 409 | |
| 410 | pad_len = 4 - (pkt_len & 0x3); |
| 411 | if (pad_len == 4) |
| 412 | pad_len = 0; |
| 413 | |
| 414 | chk_idx = index; |
| 415 | index = index + 4 + pkt_len + pad_len; |
| 416 | |
| 417 | if (index > MAX_RX_BUF_SIZE) { |
Sujith | 46baa1a | 2010-04-06 15:28:11 +0530 | [diff] [blame] | 418 | spin_lock(&hif_dev->rx_lock); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 419 | hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE; |
| 420 | hif_dev->rx_transfer_len = |
| 421 | MAX_RX_BUF_SIZE - chk_idx - 4; |
| 422 | hif_dev->rx_pad_len = pad_len; |
| 423 | |
| 424 | nskb = __dev_alloc_skb(pkt_len + 32, |
| 425 | GFP_ATOMIC); |
| 426 | if (!nskb) { |
| 427 | dev_err(&hif_dev->udev->dev, |
| 428 | "ath9k_htc: RX memory allocation" |
| 429 | " error\n"); |
Sujith | 46baa1a | 2010-04-06 15:28:11 +0530 | [diff] [blame] | 430 | spin_unlock(&hif_dev->rx_lock); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 431 | goto err; |
| 432 | } |
| 433 | skb_reserve(nskb, 32); |
| 434 | RX_STAT_INC(skb_allocated); |
| 435 | |
| 436 | memcpy(nskb->data, &(skb->data[chk_idx+4]), |
| 437 | hif_dev->rx_transfer_len); |
| 438 | |
| 439 | /* Record the buffer pointer */ |
| 440 | hif_dev->remain_skb = nskb; |
Sujith | 46baa1a | 2010-04-06 15:28:11 +0530 | [diff] [blame] | 441 | spin_unlock(&hif_dev->rx_lock); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 442 | } else { |
| 443 | nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); |
| 444 | if (!nskb) { |
| 445 | dev_err(&hif_dev->udev->dev, |
| 446 | "ath9k_htc: RX memory allocation" |
| 447 | " error\n"); |
| 448 | goto err; |
| 449 | } |
| 450 | skb_reserve(nskb, 32); |
| 451 | RX_STAT_INC(skb_allocated); |
| 452 | |
| 453 | memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len); |
| 454 | skb_put(nskb, pkt_len); |
| 455 | skb_pool[pool_index++] = nskb; |
| 456 | } |
| 457 | } else { |
| 458 | RX_STAT_INC(skb_dropped); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | err: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 464 | for (i = 0; i < pool_index; i++) { |
| 465 | ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i], |
| 466 | skb_pool[i]->len, USB_WLAN_RX_PIPE); |
| 467 | RX_STAT_INC(skb_completed); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | static void ath9k_hif_usb_rx_cb(struct urb *urb) |
| 472 | { |
| 473 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 474 | struct hif_device_usb *hif_dev = (struct hif_device_usb *) |
| 475 | usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); |
| 476 | int ret; |
| 477 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 478 | if (!skb) |
| 479 | return; |
| 480 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 481 | if (!hif_dev) |
| 482 | goto free; |
| 483 | |
| 484 | switch (urb->status) { |
| 485 | case 0: |
| 486 | break; |
| 487 | case -ENOENT: |
| 488 | case -ECONNRESET: |
| 489 | case -ENODEV: |
| 490 | case -ESHUTDOWN: |
| 491 | goto free; |
| 492 | default: |
| 493 | goto resubmit; |
| 494 | } |
| 495 | |
| 496 | if (likely(urb->actual_length != 0)) { |
| 497 | skb_put(skb, urb->actual_length); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 498 | ath9k_hif_usb_rx_stream(hif_dev, skb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | resubmit: |
| 502 | skb_reset_tail_pointer(skb); |
| 503 | skb_trim(skb, 0); |
| 504 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 505 | usb_anchor_urb(urb, &hif_dev->rx_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 506 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 507 | if (ret) { |
| 508 | usb_unanchor_urb(urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 509 | goto free; |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 510 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 511 | |
| 512 | return; |
| 513 | free: |
Ming Lei | f28a7b3 | 2010-04-13 00:28:53 +0800 | [diff] [blame] | 514 | kfree_skb(skb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static void ath9k_hif_usb_reg_in_cb(struct urb *urb) |
| 518 | { |
| 519 | struct sk_buff *skb = (struct sk_buff *) urb->context; |
| 520 | struct sk_buff *nskb; |
| 521 | struct hif_device_usb *hif_dev = (struct hif_device_usb *) |
| 522 | usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); |
| 523 | int ret; |
| 524 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 525 | if (!skb) |
| 526 | return; |
| 527 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 528 | if (!hif_dev) |
| 529 | goto free; |
| 530 | |
| 531 | switch (urb->status) { |
| 532 | case 0: |
| 533 | break; |
| 534 | case -ENOENT: |
| 535 | case -ECONNRESET: |
| 536 | case -ENODEV: |
| 537 | case -ESHUTDOWN: |
| 538 | goto free; |
| 539 | default: |
| 540 | goto resubmit; |
| 541 | } |
| 542 | |
| 543 | if (likely(urb->actual_length != 0)) { |
| 544 | skb_put(skb, urb->actual_length); |
| 545 | |
Sujith | 5ab0af3 | 2010-04-23 10:28:17 +0530 | [diff] [blame] | 546 | /* Process the command first */ |
| 547 | ath9k_htc_rx_msg(hif_dev->htc_handle, skb, |
| 548 | skb->len, USB_REG_IN_PIPE); |
| 549 | |
| 550 | |
Ming Lei | e6c6d33 | 2010-04-13 00:29:05 +0800 | [diff] [blame] | 551 | nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC); |
Sujith | 5ab0af3 | 2010-04-23 10:28:17 +0530 | [diff] [blame] | 552 | if (!nskb) { |
| 553 | dev_err(&hif_dev->udev->dev, |
| 554 | "ath9k_htc: REG_IN memory allocation failure\n"); |
| 555 | urb->context = NULL; |
| 556 | return; |
| 557 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 558 | |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 559 | usb_fill_bulk_urb(urb, hif_dev->udev, |
Rajkumar Manoharan | 0f529e9 | 2010-09-16 19:56:55 +0530 | [diff] [blame] | 560 | usb_rcvbulkpipe(hif_dev->udev, |
| 561 | USB_REG_IN_PIPE), |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 562 | nskb->data, MAX_REG_IN_BUF_SIZE, |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 563 | ath9k_hif_usb_reg_in_cb, nskb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 564 | |
| 565 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
| 566 | if (ret) { |
Ming Lei | e6c6d33 | 2010-04-13 00:29:05 +0800 | [diff] [blame] | 567 | kfree_skb(nskb); |
Sujith | 5ab0af3 | 2010-04-23 10:28:17 +0530 | [diff] [blame] | 568 | urb->context = NULL; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 569 | } |
| 570 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 571 | return; |
| 572 | } |
| 573 | |
| 574 | resubmit: |
| 575 | skb_reset_tail_pointer(skb); |
| 576 | skb_trim(skb, 0); |
| 577 | |
| 578 | ret = usb_submit_urb(urb, GFP_ATOMIC); |
| 579 | if (ret) |
| 580 | goto free; |
| 581 | |
| 582 | return; |
| 583 | free: |
Ming Lei | e6c6d33 | 2010-04-13 00:29:05 +0800 | [diff] [blame] | 584 | kfree_skb(skb); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 585 | urb->context = NULL; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev) |
| 589 | { |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 590 | struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; |
| 591 | |
Sujith | c11d8f8 | 2010-04-23 10:28:09 +0530 | [diff] [blame] | 592 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, |
| 593 | &hif_dev->tx.tx_buf, list) { |
| 594 | usb_kill_urb(tx_buf->urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 595 | list_del(&tx_buf->list); |
| 596 | usb_free_urb(tx_buf->urb); |
| 597 | kfree(tx_buf->buf); |
| 598 | kfree(tx_buf); |
| 599 | } |
| 600 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 601 | list_for_each_entry_safe(tx_buf, tx_buf_tmp, |
| 602 | &hif_dev->tx.tx_pending, list) { |
| 603 | usb_kill_urb(tx_buf->urb); |
| 604 | list_del(&tx_buf->list); |
| 605 | usb_free_urb(tx_buf->urb); |
| 606 | kfree(tx_buf->buf); |
| 607 | kfree(tx_buf); |
| 608 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev) |
| 612 | { |
| 613 | struct tx_buf *tx_buf; |
| 614 | int i; |
| 615 | |
| 616 | INIT_LIST_HEAD(&hif_dev->tx.tx_buf); |
| 617 | INIT_LIST_HEAD(&hif_dev->tx.tx_pending); |
| 618 | spin_lock_init(&hif_dev->tx.tx_lock); |
| 619 | __skb_queue_head_init(&hif_dev->tx.tx_skb_queue); |
| 620 | |
| 621 | for (i = 0; i < MAX_TX_URB_NUM; i++) { |
| 622 | tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL); |
| 623 | if (!tx_buf) |
| 624 | goto err; |
| 625 | |
| 626 | tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL); |
| 627 | if (!tx_buf->buf) |
| 628 | goto err; |
| 629 | |
| 630 | tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL); |
| 631 | if (!tx_buf->urb) |
| 632 | goto err; |
| 633 | |
| 634 | tx_buf->hif_dev = hif_dev; |
| 635 | __skb_queue_head_init(&tx_buf->skb_queue); |
| 636 | |
| 637 | list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf); |
| 638 | } |
| 639 | |
| 640 | hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM; |
| 641 | |
| 642 | return 0; |
| 643 | err: |
Dan Carpenter | 7606688 | 2010-05-14 16:52:37 +0200 | [diff] [blame] | 644 | if (tx_buf) { |
| 645 | kfree(tx_buf->buf); |
| 646 | kfree(tx_buf); |
| 647 | } |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 648 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); |
| 649 | return -ENOMEM; |
| 650 | } |
| 651 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 652 | static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev) |
| 653 | { |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 654 | usb_kill_anchored_urbs(&hif_dev->rx_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev) |
| 658 | { |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 659 | struct urb *urb = NULL; |
| 660 | struct sk_buff *skb = NULL; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 661 | int i, ret; |
| 662 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 663 | init_usb_anchor(&hif_dev->rx_submitted); |
Sujith | 46baa1a | 2010-04-06 15:28:11 +0530 | [diff] [blame] | 664 | spin_lock_init(&hif_dev->rx_lock); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 665 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 666 | for (i = 0; i < MAX_RX_URB_NUM; i++) { |
| 667 | |
| 668 | /* Allocate URB */ |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 669 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 670 | if (urb == NULL) { |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 671 | ret = -ENOMEM; |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 672 | goto err_urb; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* Allocate buffer */ |
Ming Lei | f28a7b3 | 2010-04-13 00:28:53 +0800 | [diff] [blame] | 676 | skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 677 | if (!skb) { |
| 678 | ret = -ENOMEM; |
| 679 | goto err_skb; |
| 680 | } |
| 681 | |
| 682 | usb_fill_bulk_urb(urb, hif_dev->udev, |
| 683 | usb_rcvbulkpipe(hif_dev->udev, |
| 684 | USB_WLAN_RX_PIPE), |
| 685 | skb->data, MAX_RX_BUF_SIZE, |
| 686 | ath9k_hif_usb_rx_cb, skb); |
| 687 | |
| 688 | /* Anchor URB */ |
| 689 | usb_anchor_urb(urb, &hif_dev->rx_submitted); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 690 | |
| 691 | /* Submit URB */ |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 692 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 693 | if (ret) { |
| 694 | usb_unanchor_urb(urb); |
| 695 | goto err_submit; |
| 696 | } |
Sujith | 66b10e3 | 2010-04-06 15:28:13 +0530 | [diff] [blame] | 697 | |
| 698 | /* |
| 699 | * Drop reference count. |
| 700 | * This ensures that the URB is freed when killing them. |
| 701 | */ |
| 702 | usb_free_urb(urb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | return 0; |
| 706 | |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 707 | err_submit: |
Ming Lei | f28a7b3 | 2010-04-13 00:28:53 +0800 | [diff] [blame] | 708 | kfree_skb(skb); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 709 | err_skb: |
| 710 | usb_free_urb(urb); |
| 711 | err_urb: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 712 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); |
| 713 | return ret; |
| 714 | } |
| 715 | |
| 716 | static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev) |
| 717 | { |
| 718 | if (hif_dev->reg_in_urb) { |
| 719 | usb_kill_urb(hif_dev->reg_in_urb); |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 720 | if (hif_dev->reg_in_urb->context) |
Ming Lei | e6c6d33 | 2010-04-13 00:29:05 +0800 | [diff] [blame] | 721 | kfree_skb((void *)hif_dev->reg_in_urb->context); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 722 | usb_free_urb(hif_dev->reg_in_urb); |
| 723 | hif_dev->reg_in_urb = NULL; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev) |
| 728 | { |
| 729 | struct sk_buff *skb; |
| 730 | |
| 731 | hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 732 | if (hif_dev->reg_in_urb == NULL) |
| 733 | return -ENOMEM; |
| 734 | |
Ming Lei | e6c6d33 | 2010-04-13 00:29:05 +0800 | [diff] [blame] | 735 | skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 736 | if (!skb) |
| 737 | goto err; |
| 738 | |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 739 | usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev, |
Rajkumar Manoharan | 0f529e9 | 2010-09-16 19:56:55 +0530 | [diff] [blame] | 740 | usb_rcvbulkpipe(hif_dev->udev, |
| 741 | USB_REG_IN_PIPE), |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 742 | skb->data, MAX_REG_IN_BUF_SIZE, |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 743 | ath9k_hif_usb_reg_in_cb, skb); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 744 | |
| 745 | if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0) |
Sujith | 6335ed0 | 2010-03-29 16:07:15 +0530 | [diff] [blame] | 746 | goto err; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 747 | |
| 748 | return 0; |
| 749 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 750 | err: |
| 751 | ath9k_hif_usb_dealloc_reg_in_urb(hif_dev); |
| 752 | return -ENOMEM; |
| 753 | } |
| 754 | |
| 755 | static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev) |
| 756 | { |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 757 | /* Register Write */ |
| 758 | init_usb_anchor(&hif_dev->regout_submitted); |
| 759 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 760 | /* TX */ |
| 761 | if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0) |
| 762 | goto err; |
| 763 | |
| 764 | /* RX */ |
| 765 | if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0) |
Rajkumar Manoharan | f803696 | 2010-07-07 15:19:18 +0530 | [diff] [blame] | 766 | goto err_rx; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 767 | |
Sujith | 6f0f266 | 2010-04-06 15:28:17 +0530 | [diff] [blame] | 768 | /* Register Read */ |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 769 | if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0) |
Rajkumar Manoharan | f803696 | 2010-07-07 15:19:18 +0530 | [diff] [blame] | 770 | goto err_reg; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 771 | |
| 772 | return 0; |
Rajkumar Manoharan | f803696 | 2010-07-07 15:19:18 +0530 | [diff] [blame] | 773 | err_reg: |
| 774 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); |
| 775 | err_rx: |
| 776 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 777 | err: |
| 778 | return -ENOMEM; |
| 779 | } |
| 780 | |
Sujith.Manoharan@atheros.com | 1d8af8c | 2010-05-11 16:24:40 +0530 | [diff] [blame] | 781 | static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev) |
| 782 | { |
| 783 | usb_kill_anchored_urbs(&hif_dev->regout_submitted); |
| 784 | ath9k_hif_usb_dealloc_reg_in_urb(hif_dev); |
| 785 | ath9k_hif_usb_dealloc_tx_urbs(hif_dev); |
| 786 | ath9k_hif_usb_dealloc_rx_urbs(hif_dev); |
| 787 | } |
| 788 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 789 | static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev) |
| 790 | { |
| 791 | int transfer, err; |
| 792 | const void *data = hif_dev->firmware->data; |
| 793 | size_t len = hif_dev->firmware->size; |
| 794 | u32 addr = AR9271_FIRMWARE; |
| 795 | u8 *buf = kzalloc(4096, GFP_KERNEL); |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 796 | u32 firm_offset; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 797 | |
| 798 | if (!buf) |
| 799 | return -ENOMEM; |
| 800 | |
| 801 | while (len) { |
| 802 | transfer = min_t(int, len, 4096); |
| 803 | memcpy(buf, data, transfer); |
| 804 | |
| 805 | err = usb_control_msg(hif_dev->udev, |
| 806 | usb_sndctrlpipe(hif_dev->udev, 0), |
| 807 | FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT, |
| 808 | addr >> 8, 0, buf, transfer, HZ); |
| 809 | if (err < 0) { |
| 810 | kfree(buf); |
| 811 | return err; |
| 812 | } |
| 813 | |
| 814 | len -= transfer; |
| 815 | data += transfer; |
| 816 | addr += transfer; |
| 817 | } |
| 818 | kfree(buf); |
| 819 | |
Rajkumar Manoharan | d654567 | 2010-10-27 12:02:54 +0530 | [diff] [blame] | 820 | switch (hif_dev->device_id) { |
| 821 | case 0x7010: |
| 822 | case 0x7015: |
| 823 | case 0x9018: |
Rajkumar Manoharan | 7cbf261 | 2010-11-10 17:51:25 +0530 | [diff] [blame] | 824 | case 0xA704: |
| 825 | case 0x1200: |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 826 | firm_offset = AR7010_FIRMWARE_TEXT; |
Rajkumar Manoharan | d654567 | 2010-10-27 12:02:54 +0530 | [diff] [blame] | 827 | break; |
| 828 | default: |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 829 | firm_offset = AR9271_FIRMWARE_TEXT; |
Rajkumar Manoharan | d654567 | 2010-10-27 12:02:54 +0530 | [diff] [blame] | 830 | break; |
| 831 | } |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 832 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 833 | /* |
| 834 | * Issue FW download complete command to firmware. |
| 835 | */ |
| 836 | err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0), |
| 837 | FIRMWARE_DOWNLOAD_COMP, |
| 838 | 0x40 | USB_DIR_OUT, |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 839 | firm_offset >> 8, 0, NULL, 0, HZ); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 840 | if (err) |
| 841 | return -EIO; |
| 842 | |
| 843 | 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] | 844 | hif_dev->fw_name, (unsigned long) hif_dev->firmware->size); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 845 | |
| 846 | return 0; |
| 847 | } |
| 848 | |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 849 | static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev) |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 850 | { |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 851 | int ret, idx; |
| 852 | struct usb_host_interface *alt = &hif_dev->interface->altsetting[0]; |
| 853 | struct usb_endpoint_descriptor *endp; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 854 | |
| 855 | /* Request firmware */ |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 856 | ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name, |
| 857 | &hif_dev->udev->dev); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 858 | if (ret) { |
| 859 | dev_err(&hif_dev->udev->dev, |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 860 | "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 861 | goto err_fw_req; |
| 862 | } |
| 863 | |
Sujith.Manoharan@atheros.com | 1d8af8c | 2010-05-11 16:24:40 +0530 | [diff] [blame] | 864 | /* Download firmware */ |
| 865 | ret = ath9k_hif_usb_download_fw(hif_dev); |
| 866 | if (ret) { |
| 867 | dev_err(&hif_dev->udev->dev, |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 868 | "ath9k_htc: Firmware - %s download failed\n", |
| 869 | hif_dev->fw_name); |
Sujith.Manoharan@atheros.com | 1d8af8c | 2010-05-11 16:24:40 +0530 | [diff] [blame] | 870 | goto err_fw_download; |
| 871 | } |
| 872 | |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 873 | /* On downloading the firmware to the target, the USB descriptor of EP4 |
| 874 | * is 'patched' to change the type of the endpoint to Bulk. This will |
| 875 | * bring down CPU usage during the scan period. |
| 876 | */ |
| 877 | for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) { |
| 878 | endp = &alt->endpoint[idx].desc; |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 879 | if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) |
| 880 | == USB_ENDPOINT_XFER_INT) { |
Rajkumar Manoharan | 4a0e8ecc | 2010-09-14 14:35:55 +0530 | [diff] [blame] | 881 | endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK; |
| 882 | endp->bmAttributes |= USB_ENDPOINT_XFER_BULK; |
| 883 | endp->bInterval = 0; |
| 884 | } |
| 885 | } |
| 886 | |
Rajkumar Manoharan | 490b3f4 | 2010-11-08 12:49:12 +0530 | [diff] [blame] | 887 | /* Alloc URBs */ |
| 888 | ret = ath9k_hif_usb_alloc_urbs(hif_dev); |
| 889 | if (ret) { |
| 890 | dev_err(&hif_dev->udev->dev, |
| 891 | "ath9k_htc: Unable to allocate URBs\n"); |
| 892 | goto err_urb; |
| 893 | } |
| 894 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 895 | return 0; |
| 896 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 897 | err_fw_download: |
Sujith.Manoharan@atheros.com | 1d8af8c | 2010-05-11 16:24:40 +0530 | [diff] [blame] | 898 | ath9k_hif_usb_dealloc_urbs(hif_dev); |
| 899 | err_urb: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 900 | release_firmware(hif_dev->firmware); |
| 901 | err_fw_req: |
| 902 | hif_dev->firmware = NULL; |
| 903 | return ret; |
| 904 | } |
| 905 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 906 | static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev) |
| 907 | { |
| 908 | ath9k_hif_usb_dealloc_urbs(hif_dev); |
| 909 | if (hif_dev->firmware) |
| 910 | release_firmware(hif_dev->firmware); |
| 911 | } |
| 912 | |
| 913 | static int ath9k_hif_usb_probe(struct usb_interface *interface, |
| 914 | const struct usb_device_id *id) |
| 915 | { |
| 916 | struct usb_device *udev = interface_to_usbdev(interface); |
| 917 | struct hif_device_usb *hif_dev; |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 918 | int ret = 0; |
| 919 | |
| 920 | hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL); |
| 921 | if (!hif_dev) { |
| 922 | ret = -ENOMEM; |
| 923 | goto err_alloc; |
| 924 | } |
| 925 | |
| 926 | usb_get_dev(udev); |
| 927 | hif_dev->udev = udev; |
| 928 | hif_dev->interface = interface; |
| 929 | hif_dev->device_id = id->idProduct; |
| 930 | #ifdef CONFIG_PM |
| 931 | udev->reset_resume = 1; |
| 932 | #endif |
| 933 | usb_set_intfdata(interface, hif_dev); |
| 934 | |
Sujith.Manoharan@atheros.com | 47fce02 | 2010-05-11 16:24:41 +0530 | [diff] [blame] | 935 | hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb, |
| 936 | &hif_dev->udev->dev); |
| 937 | if (hif_dev->htc_handle == NULL) { |
| 938 | ret = -ENOMEM; |
| 939 | goto err_htc_hw_alloc; |
| 940 | } |
| 941 | |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 942 | /* Find out which firmware to load */ |
| 943 | |
| 944 | switch(hif_dev->device_id) { |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 945 | case 0x7010: |
Rajkumar Manoharan | ca6cff1 | 2010-08-13 18:36:40 +0530 | [diff] [blame] | 946 | case 0x7015: |
Sujith | 4e63f76 | 2010-06-17 10:29:01 +0530 | [diff] [blame] | 947 | case 0x9018: |
Rajkumar Manoharan | 7cbf261 | 2010-11-10 17:51:25 +0530 | [diff] [blame] | 948 | case 0xA704: |
| 949 | case 0x1200: |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 950 | if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202) |
John W. Linville | d0ee0eb | 2010-06-23 14:20:45 -0400 | [diff] [blame] | 951 | hif_dev->fw_name = FIRMWARE_AR7010_1_1; |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 952 | else |
John W. Linville | d0ee0eb | 2010-06-23 14:20:45 -0400 | [diff] [blame] | 953 | hif_dev->fw_name = FIRMWARE_AR7010; |
Sujith | b176286 | 2010-06-02 15:53:34 +0530 | [diff] [blame] | 954 | break; |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 955 | default: |
John W. Linville | d0ee0eb | 2010-06-23 14:20:45 -0400 | [diff] [blame] | 956 | hif_dev->fw_name = FIRMWARE_AR9271; |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 957 | break; |
| 958 | } |
| 959 | |
Sujith | ce43cee | 2010-06-02 15:53:30 +0530 | [diff] [blame] | 960 | ret = ath9k_hif_usb_dev_init(hif_dev); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 961 | if (ret) { |
| 962 | ret = -EINVAL; |
| 963 | goto err_hif_init_usb; |
| 964 | } |
| 965 | |
Sujith.Manoharan@atheros.com | 47fce02 | 2010-05-11 16:24:41 +0530 | [diff] [blame] | 966 | ret = ath9k_htc_hw_init(hif_dev->htc_handle, |
Vivek Natarajan | 21cb987 | 2010-08-18 19:57:49 +0530 | [diff] [blame] | 967 | &hif_dev->udev->dev, hif_dev->device_id, |
| 968 | hif_dev->udev->product); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 969 | if (ret) { |
| 970 | ret = -EINVAL; |
| 971 | goto err_htc_hw_init; |
| 972 | } |
| 973 | |
| 974 | dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n"); |
| 975 | |
| 976 | return 0; |
| 977 | |
| 978 | err_htc_hw_init: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 979 | ath9k_hif_usb_dev_deinit(hif_dev); |
| 980 | err_hif_init_usb: |
Sujith.Manoharan@atheros.com | 47fce02 | 2010-05-11 16:24:41 +0530 | [diff] [blame] | 981 | ath9k_htc_hw_free(hif_dev->htc_handle); |
| 982 | err_htc_hw_alloc: |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 983 | usb_set_intfdata(interface, NULL); |
| 984 | kfree(hif_dev); |
| 985 | usb_put_dev(udev); |
| 986 | err_alloc: |
| 987 | return ret; |
| 988 | } |
| 989 | |
Sujith | 62e4716 | 2010-04-23 10:28:16 +0530 | [diff] [blame] | 990 | static void ath9k_hif_usb_reboot(struct usb_device *udev) |
| 991 | { |
| 992 | u32 reboot_cmd = 0xffffffff; |
| 993 | void *buf; |
| 994 | int ret; |
| 995 | |
Julia Lawall | a465a2c | 2010-05-15 23:17:19 +0200 | [diff] [blame] | 996 | buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL); |
Sujith | 62e4716 | 2010-04-23 10:28:16 +0530 | [diff] [blame] | 997 | if (!buf) |
| 998 | return; |
| 999 | |
Sujith | 62e4716 | 2010-04-23 10:28:16 +0530 | [diff] [blame] | 1000 | ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE), |
| 1001 | buf, 4, NULL, HZ); |
| 1002 | if (ret) |
| 1003 | dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n"); |
| 1004 | |
| 1005 | kfree(buf); |
| 1006 | } |
| 1007 | |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1008 | static void ath9k_hif_usb_disconnect(struct usb_interface *interface) |
| 1009 | { |
| 1010 | struct usb_device *udev = interface_to_usbdev(interface); |
| 1011 | struct hif_device_usb *hif_dev = |
| 1012 | (struct hif_device_usb *) usb_get_intfdata(interface); |
| 1013 | |
| 1014 | if (hif_dev) { |
Sujith | d8f996f | 2010-04-23 10:28:20 +0530 | [diff] [blame] | 1015 | ath9k_htc_hw_deinit(hif_dev->htc_handle, |
| 1016 | (udev->state == USB_STATE_NOTATTACHED) ? true : false); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1017 | ath9k_htc_hw_free(hif_dev->htc_handle); |
| 1018 | ath9k_hif_usb_dev_deinit(hif_dev); |
| 1019 | usb_set_intfdata(interface, NULL); |
| 1020 | } |
| 1021 | |
| 1022 | if (hif_dev->flags & HIF_USB_START) |
Sujith | 62e4716 | 2010-04-23 10:28:16 +0530 | [diff] [blame] | 1023 | ath9k_hif_usb_reboot(udev); |
Sujith | fb9987d | 2010-03-17 14:25:25 +0530 | [diff] [blame] | 1024 | |
| 1025 | kfree(hif_dev); |
| 1026 | dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n"); |
| 1027 | usb_put_dev(udev); |
| 1028 | } |
| 1029 | |
| 1030 | #ifdef CONFIG_PM |
| 1031 | static int ath9k_hif_usb_suspend(struct usb_interface *interface, |
| 1032 | pm_message_t message) |
| 1033 | { |
| 1034 | struct hif_device_usb *hif_dev = |
| 1035 | (struct hif_device_usb *) usb_get_intfdata(interface); |
| 1036 | |
| 1037 | ath9k_hif_usb_dealloc_urbs(hif_dev); |
| 1038 | |
| 1039 | return 0; |
| 1040 | } |
| 1041 | |
| 1042 | static int ath9k_hif_usb_resume(struct usb_interface *interface) |
| 1043 | { |
| 1044 | struct hif_device_usb *hif_dev = |
| 1045 | (struct hif_device_usb *) usb_get_intfdata(interface); |
| 1046 | int ret; |
| 1047 | |
| 1048 | ret = ath9k_hif_usb_alloc_urbs(hif_dev); |
| 1049 | if (ret) |
| 1050 | return ret; |
| 1051 | |
| 1052 | if (hif_dev->firmware) { |
| 1053 | ret = ath9k_hif_usb_download_fw(hif_dev); |
| 1054 | if (ret) |
| 1055 | goto fail_resume; |
| 1056 | } else { |
| 1057 | ath9k_hif_usb_dealloc_urbs(hif_dev); |
| 1058 | return -EIO; |
| 1059 | } |
| 1060 | |
| 1061 | mdelay(100); |
| 1062 | |
| 1063 | ret = ath9k_htc_resume(hif_dev->htc_handle); |
| 1064 | |
| 1065 | if (ret) |
| 1066 | goto fail_resume; |
| 1067 | |
| 1068 | return 0; |
| 1069 | |
| 1070 | fail_resume: |
| 1071 | ath9k_hif_usb_dealloc_urbs(hif_dev); |
| 1072 | |
| 1073 | return ret; |
| 1074 | } |
| 1075 | #endif |
| 1076 | |
| 1077 | static struct usb_driver ath9k_hif_usb_driver = { |
| 1078 | .name = "ath9k_hif_usb", |
| 1079 | .probe = ath9k_hif_usb_probe, |
| 1080 | .disconnect = ath9k_hif_usb_disconnect, |
| 1081 | #ifdef CONFIG_PM |
| 1082 | .suspend = ath9k_hif_usb_suspend, |
| 1083 | .resume = ath9k_hif_usb_resume, |
| 1084 | .reset_resume = ath9k_hif_usb_resume, |
| 1085 | #endif |
| 1086 | .id_table = ath9k_hif_usb_ids, |
| 1087 | .soft_unbind = 1, |
| 1088 | }; |
| 1089 | |
| 1090 | int ath9k_hif_usb_init(void) |
| 1091 | { |
| 1092 | return usb_register(&ath9k_hif_usb_driver); |
| 1093 | } |
| 1094 | |
| 1095 | void ath9k_hif_usb_exit(void) |
| 1096 | { |
| 1097 | usb_deregister(&ath9k_hif_usb_driver); |
| 1098 | } |