1 /* 2 * Copyright (c) 2010-2011 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 <linux/unaligned.h> 18 #include "htc.h" 19 20 MODULE_FIRMWARE(HTC_7010_MODULE_FW); 21 MODULE_FIRMWARE(HTC_9271_MODULE_FW); 22 23 static const struct usb_device_id ath9k_hif_usb_ids[] = { 24 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */ 25 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */ 26 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */ 27 { USB_DEVICE(0x07b8, 0x9271) }, /* Altai WA1011N-GU */ 28 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */ 29 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */ 30 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */ 31 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */ 32 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */ 33 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */ 34 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */ 35 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */ 36 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */ 37 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */ 38 { USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */ 39 { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */ 40 { USB_DEVICE(0x0471, 0x209e) }, /* Philips (or NXP) PTA01 */ 41 { USB_DEVICE(0x1eda, 0x2315) }, /* AirTies */ 42 43 { USB_DEVICE(0x0cf3, 0x7015), 44 .driver_info = AR9287_USB }, /* Atheros */ 45 46 { USB_DEVICE(0x0cf3, 0x7010), 47 .driver_info = AR9280_USB }, /* Atheros */ 48 { USB_DEVICE(0x0846, 0x9018), 49 .driver_info = AR9280_USB }, /* Netgear WNDA3200 */ 50 { USB_DEVICE(0x083A, 0xA704), 51 .driver_info = AR9280_USB }, /* SMC Networks */ 52 { USB_DEVICE(0x0411, 0x017f), 53 .driver_info = AR9280_USB }, /* Sony UWA-BR100 */ 54 { USB_DEVICE(0x0411, 0x0197), 55 .driver_info = AR9280_USB }, /* Buffalo WLI-UV-AG300P */ 56 { USB_DEVICE(0x04da, 0x3904), 57 .driver_info = AR9280_USB }, 58 { USB_DEVICE(0x0930, 0x0a08), 59 .driver_info = AR9280_USB }, /* Toshiba WLM-20U2 and GN-1080 */ 60 61 { USB_DEVICE(0x0cf3, 0x20ff), 62 .driver_info = STORAGE_DEVICE }, 63 64 { }, 65 }; 66 67 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids); 68 69 static int __hif_usb_tx(struct hif_device_usb *hif_dev); 70 71 static void hif_usb_regout_cb(struct urb *urb) 72 { 73 struct cmd_buf *cmd = urb->context; 74 75 switch (urb->status) { 76 case 0: 77 break; 78 case -ENOENT: 79 case -ECONNRESET: 80 case -ENODEV: 81 case -ESHUTDOWN: 82 goto free; 83 default: 84 break; 85 } 86 87 if (cmd) { 88 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle, 89 cmd->skb, true); 90 kfree(cmd); 91 } 92 93 return; 94 free: 95 kfree_skb(cmd->skb); 96 kfree(cmd); 97 } 98 99 static int hif_usb_send_regout(struct hif_device_usb *hif_dev, 100 struct sk_buff *skb) 101 { 102 struct urb *urb; 103 struct cmd_buf *cmd; 104 int ret = 0; 105 106 urb = usb_alloc_urb(0, GFP_KERNEL); 107 if (urb == NULL) 108 return -ENOMEM; 109 110 cmd = kzalloc_obj(*cmd); 111 if (cmd == NULL) { 112 usb_free_urb(urb); 113 return -ENOMEM; 114 } 115 116 cmd->skb = skb; 117 cmd->hif_dev = hif_dev; 118 119 usb_fill_int_urb(urb, hif_dev->udev, 120 usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE), 121 skb->data, skb->len, 122 hif_usb_regout_cb, cmd, 1); 123 124 usb_anchor_urb(urb, &hif_dev->regout_submitted); 125 ret = usb_submit_urb(urb, GFP_KERNEL); 126 if (ret) { 127 usb_unanchor_urb(urb); 128 kfree(cmd); 129 } 130 usb_free_urb(urb); 131 132 return ret; 133 } 134 135 static void hif_usb_mgmt_cb(struct urb *urb) 136 { 137 struct cmd_buf *cmd = urb->context; 138 struct hif_device_usb *hif_dev; 139 unsigned long flags; 140 bool txok = true; 141 142 if (!cmd || !cmd->skb || !cmd->hif_dev) 143 return; 144 145 hif_dev = cmd->hif_dev; 146 147 switch (urb->status) { 148 case 0: 149 break; 150 case -ENOENT: 151 case -ECONNRESET: 152 case -ENODEV: 153 case -ESHUTDOWN: 154 txok = false; 155 156 /* 157 * If the URBs are being flushed, no need to complete 158 * this packet. 159 */ 160 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 161 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) { 162 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 163 dev_kfree_skb_any(cmd->skb); 164 kfree(cmd); 165 return; 166 } 167 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 168 169 break; 170 default: 171 txok = false; 172 break; 173 } 174 175 skb_pull(cmd->skb, 4); 176 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle, 177 cmd->skb, txok); 178 kfree(cmd); 179 } 180 181 static int hif_usb_send_mgmt(struct hif_device_usb *hif_dev, 182 struct sk_buff *skb) 183 { 184 struct urb *urb; 185 struct cmd_buf *cmd; 186 int ret = 0; 187 __le16 *hdr; 188 189 urb = usb_alloc_urb(0, GFP_ATOMIC); 190 if (urb == NULL) 191 return -ENOMEM; 192 193 cmd = kzalloc_obj(*cmd, GFP_ATOMIC); 194 if (cmd == NULL) { 195 usb_free_urb(urb); 196 return -ENOMEM; 197 } 198 199 cmd->skb = skb; 200 cmd->hif_dev = hif_dev; 201 202 hdr = skb_push(skb, 4); 203 *hdr++ = cpu_to_le16(skb->len - 4); 204 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG); 205 206 usb_fill_bulk_urb(urb, hif_dev->udev, 207 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE), 208 skb->data, skb->len, 209 hif_usb_mgmt_cb, cmd); 210 211 usb_anchor_urb(urb, &hif_dev->mgmt_submitted); 212 ret = usb_submit_urb(urb, GFP_ATOMIC); 213 if (ret) { 214 usb_unanchor_urb(urb); 215 kfree(cmd); 216 } 217 usb_free_urb(urb); 218 219 return ret; 220 } 221 222 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev, 223 struct sk_buff_head *list) 224 { 225 struct sk_buff *skb; 226 227 while ((skb = __skb_dequeue(list)) != NULL) { 228 dev_kfree_skb_any(skb); 229 } 230 } 231 232 static inline void ath9k_skb_queue_complete(struct hif_device_usb *hif_dev, 233 struct sk_buff_head *queue, 234 bool txok) 235 { 236 struct sk_buff *skb; 237 238 while ((skb = __skb_dequeue(queue)) != NULL) { 239 #ifdef CONFIG_ATH9K_HTC_DEBUGFS 240 int ln = skb->len; 241 #endif 242 ath9k_htc_txcompletion_cb(hif_dev->htc_handle, 243 skb, txok); 244 if (txok) { 245 TX_STAT_INC(hif_dev, skb_success); 246 TX_STAT_ADD(hif_dev, skb_success_bytes, ln); 247 } 248 else 249 TX_STAT_INC(hif_dev, skb_failed); 250 } 251 } 252 253 static void hif_usb_tx_cb(struct urb *urb) 254 { 255 struct tx_buf *tx_buf = urb->context; 256 struct hif_device_usb *hif_dev; 257 bool txok = true; 258 259 if (!tx_buf || !tx_buf->hif_dev) 260 return; 261 262 hif_dev = tx_buf->hif_dev; 263 264 switch (urb->status) { 265 case 0: 266 break; 267 case -ENOENT: 268 case -ECONNRESET: 269 case -ENODEV: 270 case -ESHUTDOWN: 271 txok = false; 272 273 /* 274 * If the URBs are being flushed, no need to add this 275 * URB to the free list. 276 */ 277 spin_lock(&hif_dev->tx.tx_lock); 278 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) { 279 spin_unlock(&hif_dev->tx.tx_lock); 280 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); 281 return; 282 } 283 spin_unlock(&hif_dev->tx.tx_lock); 284 285 break; 286 default: 287 txok = false; 288 break; 289 } 290 291 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, txok); 292 293 /* Re-initialize the SKB queue */ 294 tx_buf->len = tx_buf->offset = 0; 295 __skb_queue_head_init(&tx_buf->skb_queue); 296 297 /* Add this TX buffer to the free list */ 298 spin_lock(&hif_dev->tx.tx_lock); 299 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); 300 hif_dev->tx.tx_buf_cnt++; 301 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP)) 302 __hif_usb_tx(hif_dev); /* Check for pending SKBs */ 303 TX_STAT_INC(hif_dev, buf_completed); 304 spin_unlock(&hif_dev->tx.tx_lock); 305 } 306 307 /* TX lock has to be taken */ 308 static int __hif_usb_tx(struct hif_device_usb *hif_dev) 309 { 310 struct tx_buf *tx_buf = NULL; 311 struct sk_buff *nskb = NULL; 312 int ret = 0, i; 313 u16 tx_skb_cnt = 0; 314 u8 *buf; 315 __le16 *hdr; 316 317 if (hif_dev->tx.tx_skb_cnt == 0) 318 return 0; 319 320 /* Check if a free TX buffer is available */ 321 if (list_empty(&hif_dev->tx.tx_buf)) 322 return 0; 323 324 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list); 325 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending); 326 hif_dev->tx.tx_buf_cnt--; 327 328 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM); 329 330 for (i = 0; i < tx_skb_cnt; i++) { 331 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue); 332 333 /* Should never be NULL */ 334 BUG_ON(!nskb); 335 336 hif_dev->tx.tx_skb_cnt--; 337 338 buf = tx_buf->buf; 339 buf += tx_buf->offset; 340 hdr = (__le16 *)buf; 341 *hdr++ = cpu_to_le16(nskb->len); 342 *hdr++ = cpu_to_le16(ATH_USB_TX_STREAM_MODE_TAG); 343 buf += 4; 344 memcpy(buf, nskb->data, nskb->len); 345 tx_buf->len = nskb->len + 4; 346 347 if (i < (tx_skb_cnt - 1)) 348 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4; 349 350 if (i == (tx_skb_cnt - 1)) 351 tx_buf->len += tx_buf->offset; 352 353 __skb_queue_tail(&tx_buf->skb_queue, nskb); 354 TX_STAT_INC(hif_dev, skb_queued); 355 } 356 357 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev, 358 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE), 359 tx_buf->buf, tx_buf->len, 360 hif_usb_tx_cb, tx_buf); 361 362 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC); 363 if (ret) { 364 tx_buf->len = tx_buf->offset = 0; 365 ath9k_skb_queue_complete(hif_dev, &tx_buf->skb_queue, false); 366 __skb_queue_head_init(&tx_buf->skb_queue); 367 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); 368 hif_dev->tx.tx_buf_cnt++; 369 } else { 370 TX_STAT_INC(hif_dev, buf_queued); 371 } 372 373 return ret; 374 } 375 376 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb) 377 { 378 struct ath9k_htc_tx_ctl *tx_ctl; 379 unsigned long flags; 380 int ret = 0; 381 382 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 383 384 if (hif_dev->tx.flags & HIF_USB_TX_STOP) { 385 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 386 return -ENODEV; 387 } 388 389 /* Check if the max queue count has been reached */ 390 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) { 391 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 392 return -ENOMEM; 393 } 394 395 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 396 397 tx_ctl = HTC_SKB_CB(skb); 398 399 /* Mgmt/Beacon frames don't use the TX buffer pool */ 400 if ((tx_ctl->type == ATH9K_HTC_MGMT) || 401 (tx_ctl->type == ATH9K_HTC_BEACON)) { 402 ret = hif_usb_send_mgmt(hif_dev, skb); 403 } 404 405 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 406 407 if ((tx_ctl->type == ATH9K_HTC_NORMAL) || 408 (tx_ctl->type == ATH9K_HTC_AMPDU)) { 409 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb); 410 hif_dev->tx.tx_skb_cnt++; 411 } 412 413 /* Check if AMPDUs have to be sent immediately */ 414 if ((hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) && 415 (hif_dev->tx.tx_skb_cnt < 2)) { 416 __hif_usb_tx(hif_dev); 417 } 418 419 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 420 421 return ret; 422 } 423 424 static void hif_usb_start(void *hif_handle) 425 { 426 struct hif_device_usb *hif_dev = hif_handle; 427 unsigned long flags; 428 429 hif_dev->flags |= HIF_USB_START; 430 431 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 432 hif_dev->tx.flags &= ~HIF_USB_TX_STOP; 433 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 434 } 435 436 static void hif_usb_stop(void *hif_handle) 437 { 438 struct hif_device_usb *hif_dev = hif_handle; 439 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; 440 unsigned long flags; 441 442 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 443 ath9k_skb_queue_complete(hif_dev, &hif_dev->tx.tx_skb_queue, false); 444 hif_dev->tx.tx_skb_cnt = 0; 445 hif_dev->tx.flags |= HIF_USB_TX_STOP; 446 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 447 448 /* The pending URBs have to be canceled. */ 449 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 450 list_for_each_entry_safe(tx_buf, tx_buf_tmp, 451 &hif_dev->tx.tx_pending, list) { 452 usb_get_urb(tx_buf->urb); 453 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 454 usb_kill_urb(tx_buf->urb); 455 list_del(&tx_buf->list); 456 usb_free_urb(tx_buf->urb); 457 kfree(tx_buf); 458 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 459 } 460 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 461 462 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted); 463 } 464 465 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb) 466 { 467 struct hif_device_usb *hif_dev = hif_handle; 468 int ret = 0; 469 470 switch (pipe_id) { 471 case USB_WLAN_TX_PIPE: 472 ret = hif_usb_send_tx(hif_dev, skb); 473 break; 474 case USB_REG_OUT_PIPE: 475 ret = hif_usb_send_regout(hif_dev, skb); 476 break; 477 default: 478 dev_err(&hif_dev->udev->dev, 479 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id); 480 ret = -EINVAL; 481 break; 482 } 483 484 return ret; 485 } 486 487 static inline bool check_index(struct sk_buff *skb, u8 idx) 488 { 489 struct ath9k_htc_tx_ctl *tx_ctl; 490 491 tx_ctl = HTC_SKB_CB(skb); 492 493 if ((tx_ctl->type == ATH9K_HTC_AMPDU) && 494 (tx_ctl->sta_idx == idx)) 495 return true; 496 497 return false; 498 } 499 500 static void hif_usb_sta_drain(void *hif_handle, u8 idx) 501 { 502 struct hif_device_usb *hif_dev = hif_handle; 503 struct sk_buff *skb, *tmp; 504 unsigned long flags; 505 506 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 507 508 skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) { 509 if (check_index(skb, idx)) { 510 __skb_unlink(skb, &hif_dev->tx.tx_skb_queue); 511 ath9k_htc_txcompletion_cb(hif_dev->htc_handle, 512 skb, false); 513 hif_dev->tx.tx_skb_cnt--; 514 TX_STAT_INC(hif_dev, skb_failed); 515 } 516 } 517 518 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 519 } 520 521 static struct ath9k_htc_hif hif_usb = { 522 .transport = ATH9K_HIF_USB, 523 .name = "ath9k_hif_usb", 524 525 .control_ul_pipe = USB_REG_OUT_PIPE, 526 .control_dl_pipe = USB_REG_IN_PIPE, 527 528 .start = hif_usb_start, 529 .stop = hif_usb_stop, 530 .sta_drain = hif_usb_sta_drain, 531 .send = hif_usb_send, 532 }; 533 534 /* Need to free remain_skb allocated in ath9k_hif_usb_rx_stream 535 * in case ath9k_hif_usb_rx_stream wasn't called next time to 536 * process the buffer and subsequently free it. 537 */ 538 static void ath9k_hif_usb_free_rx_remain_skb(struct hif_device_usb *hif_dev) 539 { 540 unsigned long flags; 541 542 spin_lock_irqsave(&hif_dev->rx_lock, flags); 543 if (hif_dev->remain_skb) { 544 dev_kfree_skb_any(hif_dev->remain_skb); 545 hif_dev->remain_skb = NULL; 546 hif_dev->rx_remain_len = 0; 547 RX_STAT_INC(hif_dev, skb_dropped); 548 } 549 spin_unlock_irqrestore(&hif_dev->rx_lock, flags); 550 } 551 552 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev, 553 struct sk_buff *skb) 554 { 555 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER]; 556 int index = 0, i, len = skb->len; 557 int rx_remain_len, rx_pkt_len; 558 u16 pool_index = 0; 559 u8 *ptr; 560 561 spin_lock(&hif_dev->rx_lock); 562 563 rx_remain_len = hif_dev->rx_remain_len; 564 rx_pkt_len = hif_dev->rx_transfer_len; 565 566 if (rx_remain_len != 0) { 567 struct sk_buff *remain_skb = hif_dev->remain_skb; 568 569 if (remain_skb) { 570 ptr = (u8 *) remain_skb->data; 571 572 index = rx_remain_len; 573 rx_remain_len -= hif_dev->rx_pad_len; 574 ptr += rx_pkt_len; 575 576 memcpy(ptr, skb->data, rx_remain_len); 577 578 rx_pkt_len += rx_remain_len; 579 skb_put(remain_skb, rx_pkt_len); 580 581 skb_pool[pool_index++] = remain_skb; 582 hif_dev->remain_skb = NULL; 583 hif_dev->rx_remain_len = 0; 584 } else { 585 index = rx_remain_len; 586 } 587 } 588 589 spin_unlock(&hif_dev->rx_lock); 590 591 while (index < len) { 592 u16 pkt_len; 593 u16 pkt_tag; 594 u16 pad_len; 595 int chk_idx; 596 597 ptr = (u8 *) skb->data; 598 599 pkt_len = get_unaligned_le16(ptr + index); 600 pkt_tag = get_unaligned_le16(ptr + index + 2); 601 602 /* It is supposed that if we have an invalid pkt_tag or 603 * pkt_len then the whole input SKB is considered invalid 604 * and dropped; the associated packets already in skb_pool 605 * are dropped, too. 606 */ 607 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) { 608 RX_STAT_INC(hif_dev, skb_dropped); 609 goto invalid_pkt; 610 } 611 612 if (pkt_len > 2 * MAX_RX_BUF_SIZE) { 613 dev_err(&hif_dev->udev->dev, 614 "ath9k_htc: invalid pkt_len (%x)\n", pkt_len); 615 RX_STAT_INC(hif_dev, skb_dropped); 616 goto invalid_pkt; 617 } 618 619 pad_len = 4 - (pkt_len & 0x3); 620 if (pad_len == 4) 621 pad_len = 0; 622 623 chk_idx = index; 624 index = index + 4 + pkt_len + pad_len; 625 626 if (index > MAX_RX_BUF_SIZE) { 627 spin_lock(&hif_dev->rx_lock); 628 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); 629 if (!nskb) { 630 dev_err(&hif_dev->udev->dev, 631 "ath9k_htc: RX memory allocation error\n"); 632 spin_unlock(&hif_dev->rx_lock); 633 goto err; 634 } 635 636 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE; 637 hif_dev->rx_transfer_len = 638 MAX_RX_BUF_SIZE - chk_idx - 4; 639 hif_dev->rx_pad_len = pad_len; 640 641 skb_reserve(nskb, 32); 642 RX_STAT_INC(hif_dev, skb_allocated); 643 644 memcpy(nskb->data, &(skb->data[chk_idx+4]), 645 hif_dev->rx_transfer_len); 646 647 /* Record the buffer pointer */ 648 hif_dev->remain_skb = nskb; 649 spin_unlock(&hif_dev->rx_lock); 650 } else { 651 if (pool_index == MAX_PKT_NUM_IN_TRANSFER) { 652 dev_err(&hif_dev->udev->dev, 653 "ath9k_htc: over RX MAX_PKT_NUM\n"); 654 goto err; 655 } 656 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); 657 if (!nskb) { 658 dev_err(&hif_dev->udev->dev, 659 "ath9k_htc: RX memory allocation error\n"); 660 goto err; 661 } 662 skb_reserve(nskb, 32); 663 RX_STAT_INC(hif_dev, skb_allocated); 664 665 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len); 666 skb_put(nskb, pkt_len); 667 skb_pool[pool_index++] = nskb; 668 } 669 } 670 671 err: 672 for (i = 0; i < pool_index; i++) { 673 RX_STAT_ADD(hif_dev, skb_completed_bytes, skb_pool[i]->len); 674 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i], 675 skb_pool[i]->len, USB_WLAN_RX_PIPE); 676 RX_STAT_INC(hif_dev, skb_completed); 677 } 678 return; 679 invalid_pkt: 680 for (i = 0; i < pool_index; i++) { 681 dev_kfree_skb_any(skb_pool[i]); 682 RX_STAT_INC(hif_dev, skb_dropped); 683 } 684 return; 685 } 686 687 static void ath9k_hif_usb_rx_cb(struct urb *urb) 688 { 689 struct rx_buf *rx_buf = urb->context; 690 struct hif_device_usb *hif_dev = rx_buf->hif_dev; 691 struct sk_buff *skb = rx_buf->skb; 692 int ret; 693 694 if (!skb) 695 return; 696 697 if (!hif_dev) 698 goto free; 699 700 switch (urb->status) { 701 case 0: 702 break; 703 case -ENOENT: 704 case -ECONNRESET: 705 case -ENODEV: 706 case -ESHUTDOWN: 707 goto free; 708 default: 709 goto resubmit; 710 } 711 712 if (likely(urb->actual_length != 0)) { 713 skb_put(skb, urb->actual_length); 714 ath9k_hif_usb_rx_stream(hif_dev, skb); 715 } 716 717 resubmit: 718 __skb_set_length(skb, 0); 719 720 usb_anchor_urb(urb, &hif_dev->rx_submitted); 721 ret = usb_submit_urb(urb, GFP_ATOMIC); 722 if (ret) { 723 usb_unanchor_urb(urb); 724 goto free; 725 } 726 727 return; 728 free: 729 kfree_skb(skb); 730 kfree(rx_buf); 731 } 732 733 static void ath9k_hif_usb_reg_in_cb(struct urb *urb) 734 { 735 struct rx_buf *rx_buf = urb->context; 736 struct hif_device_usb *hif_dev = rx_buf->hif_dev; 737 struct sk_buff *skb = rx_buf->skb; 738 int ret; 739 740 if (!skb) 741 return; 742 743 if (!hif_dev) 744 goto free_skb; 745 746 switch (urb->status) { 747 case 0: 748 break; 749 case -ENOENT: 750 case -ECONNRESET: 751 case -ENODEV: 752 case -ESHUTDOWN: 753 goto free_skb; 754 default: 755 __skb_set_length(skb, 0); 756 757 goto resubmit; 758 } 759 760 if (likely(urb->actual_length != 0)) { 761 skb_put(skb, urb->actual_length); 762 763 /* 764 * Process the command first. 765 * skb is either freed here or passed to be 766 * managed to another callback function. 767 */ 768 ath9k_htc_rx_msg(hif_dev->htc_handle, skb, 769 skb->len, USB_REG_IN_PIPE); 770 771 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC); 772 if (!skb) { 773 dev_err(&hif_dev->udev->dev, 774 "ath9k_htc: REG_IN memory allocation failure\n"); 775 goto free_rx_buf; 776 } 777 778 rx_buf->skb = skb; 779 780 usb_fill_int_urb(urb, hif_dev->udev, 781 usb_rcvintpipe(hif_dev->udev, 782 USB_REG_IN_PIPE), 783 skb->data, MAX_REG_IN_BUF_SIZE, 784 ath9k_hif_usb_reg_in_cb, rx_buf, 1); 785 } 786 787 resubmit: 788 usb_anchor_urb(urb, &hif_dev->reg_in_submitted); 789 ret = usb_submit_urb(urb, GFP_ATOMIC); 790 if (ret) { 791 usb_unanchor_urb(urb); 792 goto free_skb; 793 } 794 795 return; 796 free_skb: 797 kfree_skb(skb); 798 free_rx_buf: 799 kfree(rx_buf); 800 urb->context = NULL; 801 } 802 803 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev) 804 { 805 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; 806 unsigned long flags; 807 808 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 809 list_for_each_entry_safe(tx_buf, tx_buf_tmp, 810 &hif_dev->tx.tx_buf, list) { 811 list_del(&tx_buf->list); 812 usb_free_urb(tx_buf->urb); 813 kfree(tx_buf); 814 } 815 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 816 817 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 818 hif_dev->tx.flags |= HIF_USB_TX_FLUSH; 819 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 820 821 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 822 list_for_each_entry_safe(tx_buf, tx_buf_tmp, 823 &hif_dev->tx.tx_pending, list) { 824 usb_get_urb(tx_buf->urb); 825 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 826 usb_kill_urb(tx_buf->urb); 827 list_del(&tx_buf->list); 828 usb_free_urb(tx_buf->urb); 829 kfree(tx_buf); 830 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 831 } 832 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 833 834 usb_kill_anchored_urbs(&hif_dev->mgmt_submitted); 835 } 836 837 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev) 838 { 839 struct tx_buf *tx_buf; 840 int i; 841 842 INIT_LIST_HEAD(&hif_dev->tx.tx_buf); 843 INIT_LIST_HEAD(&hif_dev->tx.tx_pending); 844 spin_lock_init(&hif_dev->tx.tx_lock); 845 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue); 846 init_usb_anchor(&hif_dev->mgmt_submitted); 847 848 for (i = 0; i < MAX_TX_URB_NUM; i++) { 849 tx_buf = kzalloc_flex(*tx_buf, buf, MAX_TX_BUF_SIZE); 850 if (!tx_buf) 851 goto err; 852 853 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL); 854 if (!tx_buf->urb) 855 goto err; 856 857 tx_buf->hif_dev = hif_dev; 858 __skb_queue_head_init(&tx_buf->skb_queue); 859 860 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf); 861 } 862 863 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM; 864 865 return 0; 866 err: 867 kfree(tx_buf); 868 ath9k_hif_usb_dealloc_tx_urbs(hif_dev); 869 return -ENOMEM; 870 } 871 872 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev) 873 { 874 usb_kill_anchored_urbs(&hif_dev->rx_submitted); 875 ath9k_hif_usb_free_rx_remain_skb(hif_dev); 876 } 877 878 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev) 879 { 880 struct rx_buf *rx_buf = NULL; 881 struct sk_buff *skb = NULL; 882 struct urb *urb = NULL; 883 int i, ret; 884 885 init_usb_anchor(&hif_dev->rx_submitted); 886 spin_lock_init(&hif_dev->rx_lock); 887 888 for (i = 0; i < MAX_RX_URB_NUM; i++) { 889 890 rx_buf = kzalloc_obj(*rx_buf); 891 if (!rx_buf) { 892 ret = -ENOMEM; 893 goto err_rxb; 894 } 895 896 /* Allocate URB */ 897 urb = usb_alloc_urb(0, GFP_KERNEL); 898 if (urb == NULL) { 899 ret = -ENOMEM; 900 goto err_urb; 901 } 902 903 /* Allocate buffer */ 904 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL); 905 if (!skb) { 906 ret = -ENOMEM; 907 goto err_skb; 908 } 909 910 rx_buf->hif_dev = hif_dev; 911 rx_buf->skb = skb; 912 913 usb_fill_bulk_urb(urb, hif_dev->udev, 914 usb_rcvbulkpipe(hif_dev->udev, 915 USB_WLAN_RX_PIPE), 916 skb->data, MAX_RX_BUF_SIZE, 917 ath9k_hif_usb_rx_cb, rx_buf); 918 919 /* Anchor URB */ 920 usb_anchor_urb(urb, &hif_dev->rx_submitted); 921 922 /* Submit URB */ 923 ret = usb_submit_urb(urb, GFP_KERNEL); 924 if (ret) { 925 usb_unanchor_urb(urb); 926 goto err_submit; 927 } 928 929 /* 930 * Drop reference count. 931 * This ensures that the URB is freed when killing them. 932 */ 933 usb_free_urb(urb); 934 } 935 936 return 0; 937 938 err_submit: 939 kfree_skb(skb); 940 err_skb: 941 usb_free_urb(urb); 942 err_urb: 943 kfree(rx_buf); 944 err_rxb: 945 ath9k_hif_usb_dealloc_rx_urbs(hif_dev); 946 return ret; 947 } 948 949 static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev) 950 { 951 usb_kill_anchored_urbs(&hif_dev->reg_in_submitted); 952 } 953 954 static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev) 955 { 956 struct rx_buf *rx_buf = NULL; 957 struct sk_buff *skb = NULL; 958 struct urb *urb = NULL; 959 int i, ret; 960 961 init_usb_anchor(&hif_dev->reg_in_submitted); 962 963 for (i = 0; i < MAX_REG_IN_URB_NUM; i++) { 964 965 rx_buf = kzalloc_obj(*rx_buf); 966 if (!rx_buf) { 967 ret = -ENOMEM; 968 goto err_rxb; 969 } 970 971 /* Allocate URB */ 972 urb = usb_alloc_urb(0, GFP_KERNEL); 973 if (urb == NULL) { 974 ret = -ENOMEM; 975 goto err_urb; 976 } 977 978 /* Allocate buffer */ 979 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL); 980 if (!skb) { 981 ret = -ENOMEM; 982 goto err_skb; 983 } 984 985 rx_buf->hif_dev = hif_dev; 986 rx_buf->skb = skb; 987 988 usb_fill_int_urb(urb, hif_dev->udev, 989 usb_rcvintpipe(hif_dev->udev, 990 USB_REG_IN_PIPE), 991 skb->data, MAX_REG_IN_BUF_SIZE, 992 ath9k_hif_usb_reg_in_cb, rx_buf, 1); 993 994 /* Anchor URB */ 995 usb_anchor_urb(urb, &hif_dev->reg_in_submitted); 996 997 /* Submit URB */ 998 ret = usb_submit_urb(urb, GFP_KERNEL); 999 if (ret) { 1000 usb_unanchor_urb(urb); 1001 goto err_submit; 1002 } 1003 1004 /* 1005 * Drop reference count. 1006 * This ensures that the URB is freed when killing them. 1007 */ 1008 usb_free_urb(urb); 1009 } 1010 1011 return 0; 1012 1013 err_submit: 1014 kfree_skb(skb); 1015 err_skb: 1016 usb_free_urb(urb); 1017 err_urb: 1018 kfree(rx_buf); 1019 err_rxb: 1020 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev); 1021 return ret; 1022 } 1023 1024 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev) 1025 { 1026 /* Register Write */ 1027 init_usb_anchor(&hif_dev->regout_submitted); 1028 1029 /* TX */ 1030 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0) 1031 goto err; 1032 1033 /* RX */ 1034 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0) 1035 goto err_rx; 1036 1037 /* Register Read */ 1038 if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0) 1039 goto err_reg; 1040 1041 return 0; 1042 err_reg: 1043 ath9k_hif_usb_dealloc_rx_urbs(hif_dev); 1044 err_rx: 1045 ath9k_hif_usb_dealloc_tx_urbs(hif_dev); 1046 err: 1047 return -ENOMEM; 1048 } 1049 1050 void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev) 1051 { 1052 usb_kill_anchored_urbs(&hif_dev->regout_submitted); 1053 ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev); 1054 ath9k_hif_usb_dealloc_tx_urbs(hif_dev); 1055 ath9k_hif_usb_dealloc_rx_urbs(hif_dev); 1056 } 1057 1058 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev) 1059 { 1060 int transfer, err; 1061 const void *data = hif_dev->fw_data; 1062 size_t len = hif_dev->fw_size; 1063 u32 addr = AR9271_FIRMWARE; 1064 u8 *buf = kzalloc(4096, GFP_KERNEL); 1065 u32 firm_offset; 1066 1067 if (!buf) 1068 return -ENOMEM; 1069 1070 while (len) { 1071 transfer = min_t(size_t, len, 4096); 1072 memcpy(buf, data, transfer); 1073 1074 err = usb_control_msg(hif_dev->udev, 1075 usb_sndctrlpipe(hif_dev->udev, 0), 1076 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT, 1077 addr >> 8, 0, buf, transfer, 1078 USB_MSG_TIMEOUT); 1079 if (err < 0) { 1080 kfree(buf); 1081 return err; 1082 } 1083 1084 len -= transfer; 1085 data += transfer; 1086 addr += transfer; 1087 } 1088 kfree(buf); 1089 1090 if (IS_AR7010_DEVICE(hif_dev->id_info)) 1091 firm_offset = AR7010_FIRMWARE_TEXT; 1092 else 1093 firm_offset = AR9271_FIRMWARE_TEXT; 1094 1095 /* 1096 * Issue FW download complete command to firmware. 1097 */ 1098 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0), 1099 FIRMWARE_DOWNLOAD_COMP, 1100 0x40 | USB_DIR_OUT, 1101 firm_offset >> 8, 0, NULL, 0, USB_MSG_TIMEOUT); 1102 if (err) 1103 return -EIO; 1104 1105 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n", 1106 hif_dev->fw_name, (unsigned long) hif_dev->fw_size); 1107 1108 return 0; 1109 } 1110 1111 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev) 1112 { 1113 int ret; 1114 1115 ret = ath9k_hif_usb_download_fw(hif_dev); 1116 if (ret) { 1117 dev_err(&hif_dev->udev->dev, 1118 "ath9k_htc: Firmware - %s download failed\n", 1119 hif_dev->fw_name); 1120 return ret; 1121 } 1122 1123 /* Alloc URBs */ 1124 ret = ath9k_hif_usb_alloc_urbs(hif_dev); 1125 if (ret) { 1126 dev_err(&hif_dev->udev->dev, 1127 "ath9k_htc: Unable to allocate URBs\n"); 1128 return ret; 1129 } 1130 1131 return 0; 1132 } 1133 1134 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev) 1135 { 1136 ath9k_hif_usb_dealloc_urbs(hif_dev); 1137 } 1138 1139 /* 1140 * If initialization fails or the FW cannot be retrieved, 1141 * detach the device. 1142 */ 1143 static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev) 1144 { 1145 struct device *dev = &hif_dev->udev->dev; 1146 struct device *parent = dev->parent; 1147 1148 complete_all(&hif_dev->fw_done); 1149 1150 if (parent) 1151 device_lock(parent); 1152 1153 device_release_driver(dev); 1154 1155 if (parent) 1156 device_unlock(parent); 1157 } 1158 1159 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context); 1160 1161 /* taken from iwlwifi */ 1162 static int ath9k_hif_request_firmware(struct hif_device_usb *hif_dev, 1163 bool first) 1164 { 1165 char index[8], *chip; 1166 int ret; 1167 1168 if (first) { 1169 if (htc_use_dev_fw) { 1170 hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX + 1; 1171 sprintf(index, "%s", "dev"); 1172 } else { 1173 hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX; 1174 sprintf(index, "%d", hif_dev->fw_minor_index); 1175 } 1176 } else { 1177 hif_dev->fw_minor_index--; 1178 sprintf(index, "%d", hif_dev->fw_minor_index); 1179 } 1180 1181 /* test for FW 1.3 */ 1182 if (MAJOR_VERSION_REQ == 1 && hif_dev->fw_minor_index == 3) { 1183 const char *filename; 1184 1185 if (IS_AR7010_DEVICE(hif_dev->id_info)) 1186 filename = FIRMWARE_AR7010_1_1; 1187 else 1188 filename = FIRMWARE_AR9271; 1189 1190 /* expected fw locations: 1191 * - htc_9271.fw (stable version 1.3, deprecated) 1192 */ 1193 snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name), 1194 "%s", filename); 1195 1196 } else if (hif_dev->fw_minor_index < FIRMWARE_MINOR_IDX_MIN) { 1197 dev_err(&hif_dev->udev->dev, "no suitable firmware found!\n"); 1198 1199 return -ENOENT; 1200 } else { 1201 if (IS_AR7010_DEVICE(hif_dev->id_info)) 1202 chip = "7010"; 1203 else 1204 chip = "9271"; 1205 1206 /* expected fw locations: 1207 * - ath9k_htc/htc_9271-1.dev.0.fw (development version) 1208 * - ath9k_htc/htc_9271-1.4.0.fw (stable version) 1209 */ 1210 snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name), 1211 "%s/htc_%s-%d.%s.0.fw", HTC_FW_PATH, 1212 chip, MAJOR_VERSION_REQ, index); 1213 } 1214 1215 ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name, 1216 &hif_dev->udev->dev, GFP_KERNEL, 1217 hif_dev, ath9k_hif_usb_firmware_cb); 1218 if (ret) 1219 dev_err(&hif_dev->udev->dev, 1220 "ath9k_htc: Async request for firmware %s failed\n", 1221 hif_dev->fw_name); 1222 1223 return ret; 1224 } 1225 1226 static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context) 1227 { 1228 struct hif_device_usb *hif_dev = context; 1229 int ret; 1230 1231 if (!fw) { 1232 ret = ath9k_hif_request_firmware(hif_dev, false); 1233 if (!ret) 1234 return; 1235 1236 dev_err(&hif_dev->udev->dev, 1237 "ath9k_htc: Failed to get firmware %s\n", 1238 hif_dev->fw_name); 1239 goto err_fw; 1240 } 1241 1242 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb, 1243 &hif_dev->udev->dev); 1244 if (hif_dev->htc_handle == NULL) 1245 goto err_dev_alloc; 1246 1247 hif_dev->fw_data = fw->data; 1248 hif_dev->fw_size = fw->size; 1249 1250 /* Proceed with initialization */ 1251 1252 ret = ath9k_hif_usb_dev_init(hif_dev); 1253 if (ret) 1254 goto err_dev_init; 1255 1256 ret = ath9k_htc_hw_init(hif_dev->htc_handle, 1257 &hif_dev->interface->dev, 1258 le16_to_cpu(hif_dev->udev->descriptor.idProduct), 1259 hif_dev->udev->product, 1260 hif_dev->id_info); 1261 if (ret) { 1262 ret = -EINVAL; 1263 goto err_htc_hw_init; 1264 } 1265 1266 release_firmware(fw); 1267 hif_dev->flags |= HIF_USB_READY; 1268 complete_all(&hif_dev->fw_done); 1269 1270 return; 1271 1272 err_htc_hw_init: 1273 ath9k_hif_usb_dev_deinit(hif_dev); 1274 err_dev_init: 1275 ath9k_htc_hw_free(hif_dev->htc_handle); 1276 err_dev_alloc: 1277 release_firmware(fw); 1278 err_fw: 1279 ath9k_hif_usb_firmware_fail(hif_dev); 1280 } 1281 1282 /* 1283 * An exact copy of the function from zd1211rw. 1284 */ 1285 static int send_eject_command(struct usb_interface *interface) 1286 { 1287 struct usb_device *udev = interface_to_usbdev(interface); 1288 struct usb_host_interface *iface_desc = interface->cur_altsetting; 1289 struct usb_endpoint_descriptor *endpoint; 1290 unsigned char *cmd; 1291 u8 bulk_out_ep; 1292 int r; 1293 1294 if (iface_desc->desc.bNumEndpoints < 2) 1295 return -ENODEV; 1296 1297 /* Find bulk out endpoint */ 1298 for (r = 1; r >= 0; r--) { 1299 endpoint = &iface_desc->endpoint[r].desc; 1300 if (usb_endpoint_dir_out(endpoint) && 1301 usb_endpoint_xfer_bulk(endpoint)) { 1302 bulk_out_ep = endpoint->bEndpointAddress; 1303 break; 1304 } 1305 } 1306 if (r == -1) { 1307 dev_err(&udev->dev, 1308 "ath9k_htc: Could not find bulk out endpoint\n"); 1309 return -ENODEV; 1310 } 1311 1312 cmd = kzalloc(31, GFP_KERNEL); 1313 if (cmd == NULL) 1314 return -ENODEV; 1315 1316 /* USB bulk command block */ 1317 cmd[0] = 0x55; /* bulk command signature */ 1318 cmd[1] = 0x53; /* bulk command signature */ 1319 cmd[2] = 0x42; /* bulk command signature */ 1320 cmd[3] = 0x43; /* bulk command signature */ 1321 cmd[14] = 6; /* command length */ 1322 1323 cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */ 1324 cmd[19] = 0x2; /* eject disc */ 1325 1326 dev_info(&udev->dev, "Ejecting storage device...\n"); 1327 r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep), 1328 cmd, 31, NULL, 2 * USB_MSG_TIMEOUT); 1329 kfree(cmd); 1330 if (r) 1331 return r; 1332 1333 /* At this point, the device disconnects and reconnects with the real 1334 * ID numbers. */ 1335 1336 usb_set_intfdata(interface, NULL); 1337 return 0; 1338 } 1339 1340 static int ath9k_hif_usb_probe(struct usb_interface *interface, 1341 const struct usb_device_id *id) 1342 { 1343 struct usb_endpoint_descriptor *bulk_in, *bulk_out, *int_in, *int_out; 1344 struct usb_device *udev = interface_to_usbdev(interface); 1345 struct usb_host_interface *alt; 1346 struct hif_device_usb *hif_dev; 1347 int ret = 0; 1348 1349 /* Verify the expected endpoints are present */ 1350 alt = interface->cur_altsetting; 1351 if (usb_find_common_endpoints(alt, &bulk_in, &bulk_out, &int_in, &int_out) < 0 || 1352 usb_endpoint_num(bulk_in) != USB_WLAN_RX_PIPE || 1353 usb_endpoint_num(bulk_out) != USB_WLAN_TX_PIPE || 1354 usb_endpoint_num(int_in) != USB_REG_IN_PIPE || 1355 usb_endpoint_num(int_out) != USB_REG_OUT_PIPE) { 1356 dev_err(&udev->dev, 1357 "ath9k_htc: Device endpoint numbers are not the expected ones\n"); 1358 return -ENODEV; 1359 } 1360 1361 if (id->driver_info == STORAGE_DEVICE) 1362 return send_eject_command(interface); 1363 1364 hif_dev = kzalloc_obj(struct hif_device_usb); 1365 if (!hif_dev) { 1366 ret = -ENOMEM; 1367 goto err_alloc; 1368 } 1369 1370 hif_dev->udev = udev; 1371 hif_dev->interface = interface; 1372 hif_dev->id_info = id->driver_info; 1373 #ifdef CONFIG_PM 1374 udev->reset_resume = 1; 1375 #endif 1376 usb_set_intfdata(interface, hif_dev); 1377 1378 init_completion(&hif_dev->fw_done); 1379 1380 ret = ath9k_hif_request_firmware(hif_dev, true); 1381 if (ret) 1382 goto err_fw_req; 1383 1384 return ret; 1385 1386 err_fw_req: 1387 usb_set_intfdata(interface, NULL); 1388 kfree(hif_dev); 1389 err_alloc: 1390 return ret; 1391 } 1392 1393 static void ath9k_hif_usb_reboot(struct usb_device *udev) 1394 { 1395 u32 reboot_cmd = 0xffffffff; 1396 void *buf; 1397 int ret; 1398 1399 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL); 1400 if (!buf) 1401 return; 1402 1403 ret = usb_interrupt_msg(udev, usb_sndintpipe(udev, USB_REG_OUT_PIPE), 1404 buf, 4, NULL, USB_MSG_TIMEOUT); 1405 if (ret) 1406 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n"); 1407 1408 kfree(buf); 1409 } 1410 1411 static void ath9k_hif_usb_disconnect(struct usb_interface *interface) 1412 { 1413 struct usb_device *udev = interface_to_usbdev(interface); 1414 struct hif_device_usb *hif_dev = usb_get_intfdata(interface); 1415 bool unplugged = udev->state == USB_STATE_NOTATTACHED; 1416 1417 if (!hif_dev) 1418 return; 1419 1420 wait_for_completion(&hif_dev->fw_done); 1421 1422 if (hif_dev->flags & HIF_USB_READY) { 1423 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged); 1424 ath9k_htc_hw_free(hif_dev->htc_handle); 1425 } 1426 1427 usb_set_intfdata(interface, NULL); 1428 1429 /* If firmware was loaded we should drop it 1430 * go back to first stage bootloader. */ 1431 if (!unplugged && (hif_dev->flags & HIF_USB_READY)) 1432 ath9k_hif_usb_reboot(udev); 1433 1434 kfree(hif_dev); 1435 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n"); 1436 } 1437 1438 #ifdef CONFIG_PM 1439 static int ath9k_hif_usb_suspend(struct usb_interface *interface, 1440 pm_message_t message) 1441 { 1442 struct hif_device_usb *hif_dev = usb_get_intfdata(interface); 1443 1444 /* 1445 * The device has to be set to FULLSLEEP mode in case no 1446 * interface is up. 1447 */ 1448 if (!(hif_dev->flags & HIF_USB_START)) 1449 ath9k_htc_suspend(hif_dev->htc_handle); 1450 1451 wait_for_completion(&hif_dev->fw_done); 1452 1453 if (hif_dev->flags & HIF_USB_READY) 1454 ath9k_hif_usb_dealloc_urbs(hif_dev); 1455 1456 return 0; 1457 } 1458 1459 static int ath9k_hif_usb_resume(struct usb_interface *interface) 1460 { 1461 struct hif_device_usb *hif_dev = usb_get_intfdata(interface); 1462 struct htc_target *htc_handle = hif_dev->htc_handle; 1463 const struct firmware *fw; 1464 int ret; 1465 1466 ret = ath9k_hif_usb_alloc_urbs(hif_dev); 1467 if (ret) 1468 return ret; 1469 1470 if (!(hif_dev->flags & HIF_USB_READY)) { 1471 ret = -EIO; 1472 goto fail_resume; 1473 } 1474 1475 /* request cached firmware during suspend/resume cycle */ 1476 ret = request_firmware(&fw, hif_dev->fw_name, 1477 &hif_dev->udev->dev); 1478 if (ret) 1479 goto fail_resume; 1480 1481 hif_dev->fw_data = fw->data; 1482 hif_dev->fw_size = fw->size; 1483 ret = ath9k_hif_usb_download_fw(hif_dev); 1484 release_firmware(fw); 1485 if (ret) 1486 goto fail_resume; 1487 1488 mdelay(100); 1489 1490 ret = ath9k_htc_resume(htc_handle); 1491 1492 if (ret) 1493 goto fail_resume; 1494 1495 return 0; 1496 1497 fail_resume: 1498 ath9k_hif_usb_dealloc_urbs(hif_dev); 1499 1500 return ret; 1501 } 1502 #endif 1503 1504 static struct usb_driver ath9k_hif_usb_driver = { 1505 .name = KBUILD_MODNAME, 1506 .probe = ath9k_hif_usb_probe, 1507 .disconnect = ath9k_hif_usb_disconnect, 1508 #ifdef CONFIG_PM 1509 .suspend = ath9k_hif_usb_suspend, 1510 .resume = ath9k_hif_usb_resume, 1511 .reset_resume = ath9k_hif_usb_resume, 1512 #endif 1513 .id_table = ath9k_hif_usb_ids, 1514 .soft_unbind = 1, 1515 .disable_hub_initiated_lpm = 1, 1516 }; 1517 1518 module_usb_driver(ath9k_hif_usb_driver); 1519