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