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