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