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