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