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