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