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