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