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 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 28 static struct usb_device_id ath9k_hif_usb_ids[] = { 29 { USB_DEVICE(0x0cf3, 0x9271) }, /* Atheros */ 30 { USB_DEVICE(0x0cf3, 0x1006) }, /* Atheros */ 31 { USB_DEVICE(0x0846, 0x9030) }, /* Netgear N150 */ 32 { USB_DEVICE(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */ 33 { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */ 34 { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */ 35 { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */ 36 { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */ 37 { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */ 38 { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */ 39 { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */ 40 { USB_DEVICE(0x040D, 0x3801) }, /* VIA */ 41 { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */ 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 55 { }, 56 }; 57 58 MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids); 59 60 static int __hif_usb_tx(struct hif_device_usb *hif_dev); 61 62 static void hif_usb_regout_cb(struct urb *urb) 63 { 64 struct cmd_buf *cmd = (struct cmd_buf *)urb->context; 65 66 switch (urb->status) { 67 case 0: 68 break; 69 case -ENOENT: 70 case -ECONNRESET: 71 case -ENODEV: 72 case -ESHUTDOWN: 73 goto free; 74 default: 75 break; 76 } 77 78 if (cmd) { 79 ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle, 80 cmd->skb, 1); 81 kfree(cmd); 82 } 83 84 return; 85 free: 86 kfree_skb(cmd->skb); 87 kfree(cmd); 88 } 89 90 static int hif_usb_send_regout(struct hif_device_usb *hif_dev, 91 struct sk_buff *skb) 92 { 93 struct urb *urb; 94 struct cmd_buf *cmd; 95 int ret = 0; 96 97 urb = usb_alloc_urb(0, GFP_KERNEL); 98 if (urb == NULL) 99 return -ENOMEM; 100 101 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL); 102 if (cmd == NULL) { 103 usb_free_urb(urb); 104 return -ENOMEM; 105 } 106 107 cmd->skb = skb; 108 cmd->hif_dev = hif_dev; 109 110 usb_fill_bulk_urb(urb, hif_dev->udev, 111 usb_sndbulkpipe(hif_dev->udev, USB_REG_OUT_PIPE), 112 skb->data, skb->len, 113 hif_usb_regout_cb, cmd); 114 115 usb_anchor_urb(urb, &hif_dev->regout_submitted); 116 ret = usb_submit_urb(urb, GFP_KERNEL); 117 if (ret) { 118 usb_unanchor_urb(urb); 119 kfree(cmd); 120 } 121 usb_free_urb(urb); 122 123 return ret; 124 } 125 126 static inline void ath9k_skb_queue_purge(struct hif_device_usb *hif_dev, 127 struct sk_buff_head *list) 128 { 129 struct sk_buff *skb; 130 131 while ((skb = __skb_dequeue(list)) != NULL) { 132 dev_kfree_skb_any(skb); 133 TX_STAT_INC(skb_dropped); 134 } 135 } 136 137 static void hif_usb_tx_cb(struct urb *urb) 138 { 139 struct tx_buf *tx_buf = (struct tx_buf *) urb->context; 140 struct hif_device_usb *hif_dev; 141 struct sk_buff *skb; 142 143 if (!tx_buf || !tx_buf->hif_dev) 144 return; 145 146 hif_dev = tx_buf->hif_dev; 147 148 switch (urb->status) { 149 case 0: 150 break; 151 case -ENOENT: 152 case -ECONNRESET: 153 case -ENODEV: 154 case -ESHUTDOWN: 155 /* 156 * The URB has been killed, free the SKBs. 157 */ 158 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); 159 160 /* 161 * If the URBs are being flushed, no need to add this 162 * URB to the free list. 163 */ 164 spin_lock(&hif_dev->tx.tx_lock); 165 if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) { 166 spin_unlock(&hif_dev->tx.tx_lock); 167 return; 168 } 169 spin_unlock(&hif_dev->tx.tx_lock); 170 171 /* 172 * In the stop() case, this URB has to be added to 173 * the free list. 174 */ 175 goto add_free; 176 default: 177 break; 178 } 179 180 /* 181 * Check if TX has been stopped, this is needed because 182 * this CB could have been invoked just after the TX lock 183 * was released in hif_stop() and kill_urb() hasn't been 184 * called yet. 185 */ 186 spin_lock(&hif_dev->tx.tx_lock); 187 if (hif_dev->tx.flags & HIF_USB_TX_STOP) { 188 spin_unlock(&hif_dev->tx.tx_lock); 189 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); 190 goto add_free; 191 } 192 spin_unlock(&hif_dev->tx.tx_lock); 193 194 /* Complete the queued SKBs. */ 195 while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) { 196 ath9k_htc_txcompletion_cb(hif_dev->htc_handle, 197 skb, 1); 198 TX_STAT_INC(skb_completed); 199 } 200 201 add_free: 202 /* Re-initialize the SKB queue */ 203 tx_buf->len = tx_buf->offset = 0; 204 __skb_queue_head_init(&tx_buf->skb_queue); 205 206 /* Add this TX buffer to the free list */ 207 spin_lock(&hif_dev->tx.tx_lock); 208 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); 209 hif_dev->tx.tx_buf_cnt++; 210 if (!(hif_dev->tx.flags & HIF_USB_TX_STOP)) 211 __hif_usb_tx(hif_dev); /* Check for pending SKBs */ 212 TX_STAT_INC(buf_completed); 213 spin_unlock(&hif_dev->tx.tx_lock); 214 } 215 216 /* TX lock has to be taken */ 217 static int __hif_usb_tx(struct hif_device_usb *hif_dev) 218 { 219 struct tx_buf *tx_buf = NULL; 220 struct sk_buff *nskb = NULL; 221 int ret = 0, i; 222 u16 *hdr, tx_skb_cnt = 0; 223 u8 *buf; 224 225 if (hif_dev->tx.tx_skb_cnt == 0) 226 return 0; 227 228 /* Check if a free TX buffer is available */ 229 if (list_empty(&hif_dev->tx.tx_buf)) 230 return 0; 231 232 tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list); 233 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_pending); 234 hif_dev->tx.tx_buf_cnt--; 235 236 tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM); 237 238 for (i = 0; i < tx_skb_cnt; i++) { 239 nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue); 240 241 /* Should never be NULL */ 242 BUG_ON(!nskb); 243 244 hif_dev->tx.tx_skb_cnt--; 245 246 buf = tx_buf->buf; 247 buf += tx_buf->offset; 248 hdr = (u16 *)buf; 249 *hdr++ = nskb->len; 250 *hdr++ = ATH_USB_TX_STREAM_MODE_TAG; 251 buf += 4; 252 memcpy(buf, nskb->data, nskb->len); 253 tx_buf->len = nskb->len + 4; 254 255 if (i < (tx_skb_cnt - 1)) 256 tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4; 257 258 if (i == (tx_skb_cnt - 1)) 259 tx_buf->len += tx_buf->offset; 260 261 __skb_queue_tail(&tx_buf->skb_queue, nskb); 262 TX_STAT_INC(skb_queued); 263 } 264 265 usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev, 266 usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE), 267 tx_buf->buf, tx_buf->len, 268 hif_usb_tx_cb, tx_buf); 269 270 ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC); 271 if (ret) { 272 tx_buf->len = tx_buf->offset = 0; 273 ath9k_skb_queue_purge(hif_dev, &tx_buf->skb_queue); 274 __skb_queue_head_init(&tx_buf->skb_queue); 275 list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf); 276 hif_dev->tx.tx_buf_cnt++; 277 } 278 279 if (!ret) 280 TX_STAT_INC(buf_queued); 281 282 return ret; 283 } 284 285 static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb, 286 struct ath9k_htc_tx_ctl *tx_ctl) 287 { 288 unsigned long flags; 289 290 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 291 292 if (hif_dev->tx.flags & HIF_USB_TX_STOP) { 293 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 294 return -ENODEV; 295 } 296 297 /* Check if the max queue count has been reached */ 298 if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) { 299 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 300 return -ENOMEM; 301 } 302 303 __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb); 304 hif_dev->tx.tx_skb_cnt++; 305 306 /* Send normal frames immediately */ 307 if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL))) 308 __hif_usb_tx(hif_dev); 309 310 /* Check if AMPDUs have to be sent immediately */ 311 if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) && 312 (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) && 313 (hif_dev->tx.tx_skb_cnt < 2)) { 314 __hif_usb_tx(hif_dev); 315 } 316 317 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 318 319 return 0; 320 } 321 322 static void hif_usb_start(void *hif_handle, u8 pipe_id) 323 { 324 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; 325 unsigned long flags; 326 327 hif_dev->flags |= HIF_USB_START; 328 329 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 330 hif_dev->tx.flags &= ~HIF_USB_TX_STOP; 331 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 332 } 333 334 static void hif_usb_stop(void *hif_handle, u8 pipe_id) 335 { 336 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; 337 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; 338 unsigned long flags; 339 340 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 341 ath9k_skb_queue_purge(hif_dev, &hif_dev->tx.tx_skb_queue); 342 hif_dev->tx.tx_skb_cnt = 0; 343 hif_dev->tx.flags |= HIF_USB_TX_STOP; 344 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 345 346 /* The pending URBs have to be canceled. */ 347 list_for_each_entry_safe(tx_buf, tx_buf_tmp, 348 &hif_dev->tx.tx_pending, list) { 349 usb_kill_urb(tx_buf->urb); 350 } 351 } 352 353 static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb, 354 struct ath9k_htc_tx_ctl *tx_ctl) 355 { 356 struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle; 357 int ret = 0; 358 359 switch (pipe_id) { 360 case USB_WLAN_TX_PIPE: 361 ret = hif_usb_send_tx(hif_dev, skb, tx_ctl); 362 break; 363 case USB_REG_OUT_PIPE: 364 ret = hif_usb_send_regout(hif_dev, skb); 365 break; 366 default: 367 dev_err(&hif_dev->udev->dev, 368 "ath9k_htc: Invalid TX pipe: %d\n", pipe_id); 369 ret = -EINVAL; 370 break; 371 } 372 373 return ret; 374 } 375 376 static struct ath9k_htc_hif hif_usb = { 377 .transport = ATH9K_HIF_USB, 378 .name = "ath9k_hif_usb", 379 380 .control_ul_pipe = USB_REG_OUT_PIPE, 381 .control_dl_pipe = USB_REG_IN_PIPE, 382 383 .start = hif_usb_start, 384 .stop = hif_usb_stop, 385 .send = hif_usb_send, 386 }; 387 388 static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev, 389 struct sk_buff *skb) 390 { 391 struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER]; 392 int index = 0, i = 0, len = skb->len; 393 int rx_remain_len, rx_pkt_len; 394 u16 pool_index = 0; 395 u8 *ptr; 396 397 spin_lock(&hif_dev->rx_lock); 398 399 rx_remain_len = hif_dev->rx_remain_len; 400 rx_pkt_len = hif_dev->rx_transfer_len; 401 402 if (rx_remain_len != 0) { 403 struct sk_buff *remain_skb = hif_dev->remain_skb; 404 405 if (remain_skb) { 406 ptr = (u8 *) remain_skb->data; 407 408 index = rx_remain_len; 409 rx_remain_len -= hif_dev->rx_pad_len; 410 ptr += rx_pkt_len; 411 412 memcpy(ptr, skb->data, rx_remain_len); 413 414 rx_pkt_len += rx_remain_len; 415 hif_dev->rx_remain_len = 0; 416 skb_put(remain_skb, rx_pkt_len); 417 418 skb_pool[pool_index++] = remain_skb; 419 420 } else { 421 index = rx_remain_len; 422 } 423 } 424 425 spin_unlock(&hif_dev->rx_lock); 426 427 while (index < len) { 428 u16 pkt_len; 429 u16 pkt_tag; 430 u16 pad_len; 431 int chk_idx; 432 433 ptr = (u8 *) skb->data; 434 435 pkt_len = ptr[index] + (ptr[index+1] << 8); 436 pkt_tag = ptr[index+2] + (ptr[index+3] << 8); 437 438 if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) { 439 RX_STAT_INC(skb_dropped); 440 return; 441 } 442 443 pad_len = 4 - (pkt_len & 0x3); 444 if (pad_len == 4) 445 pad_len = 0; 446 447 chk_idx = index; 448 index = index + 4 + pkt_len + pad_len; 449 450 if (index > MAX_RX_BUF_SIZE) { 451 spin_lock(&hif_dev->rx_lock); 452 hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE; 453 hif_dev->rx_transfer_len = 454 MAX_RX_BUF_SIZE - chk_idx - 4; 455 hif_dev->rx_pad_len = pad_len; 456 457 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); 458 if (!nskb) { 459 dev_err(&hif_dev->udev->dev, 460 "ath9k_htc: RX memory allocation error\n"); 461 spin_unlock(&hif_dev->rx_lock); 462 goto err; 463 } 464 skb_reserve(nskb, 32); 465 RX_STAT_INC(skb_allocated); 466 467 memcpy(nskb->data, &(skb->data[chk_idx+4]), 468 hif_dev->rx_transfer_len); 469 470 /* Record the buffer pointer */ 471 hif_dev->remain_skb = nskb; 472 spin_unlock(&hif_dev->rx_lock); 473 } else { 474 nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC); 475 if (!nskb) { 476 dev_err(&hif_dev->udev->dev, 477 "ath9k_htc: RX memory allocation error\n"); 478 goto err; 479 } 480 skb_reserve(nskb, 32); 481 RX_STAT_INC(skb_allocated); 482 483 memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len); 484 skb_put(nskb, pkt_len); 485 skb_pool[pool_index++] = nskb; 486 } 487 } 488 489 err: 490 for (i = 0; i < pool_index; i++) { 491 ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i], 492 skb_pool[i]->len, USB_WLAN_RX_PIPE); 493 RX_STAT_INC(skb_completed); 494 } 495 } 496 497 static void ath9k_hif_usb_rx_cb(struct urb *urb) 498 { 499 struct sk_buff *skb = (struct sk_buff *) urb->context; 500 struct hif_device_usb *hif_dev = 501 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); 502 int ret; 503 504 if (!skb) 505 return; 506 507 if (!hif_dev) 508 goto free; 509 510 switch (urb->status) { 511 case 0: 512 break; 513 case -ENOENT: 514 case -ECONNRESET: 515 case -ENODEV: 516 case -ESHUTDOWN: 517 goto free; 518 default: 519 goto resubmit; 520 } 521 522 if (likely(urb->actual_length != 0)) { 523 skb_put(skb, urb->actual_length); 524 ath9k_hif_usb_rx_stream(hif_dev, skb); 525 } 526 527 resubmit: 528 skb_reset_tail_pointer(skb); 529 skb_trim(skb, 0); 530 531 usb_anchor_urb(urb, &hif_dev->rx_submitted); 532 ret = usb_submit_urb(urb, GFP_ATOMIC); 533 if (ret) { 534 usb_unanchor_urb(urb); 535 goto free; 536 } 537 538 return; 539 free: 540 kfree_skb(skb); 541 } 542 543 static void ath9k_hif_usb_reg_in_cb(struct urb *urb) 544 { 545 struct sk_buff *skb = (struct sk_buff *) urb->context; 546 struct sk_buff *nskb; 547 struct hif_device_usb *hif_dev = 548 usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0)); 549 int ret; 550 551 if (!skb) 552 return; 553 554 if (!hif_dev) 555 goto free; 556 557 switch (urb->status) { 558 case 0: 559 break; 560 case -ENOENT: 561 case -ECONNRESET: 562 case -ENODEV: 563 case -ESHUTDOWN: 564 goto free; 565 default: 566 goto resubmit; 567 } 568 569 if (likely(urb->actual_length != 0)) { 570 skb_put(skb, urb->actual_length); 571 572 /* Process the command first */ 573 ath9k_htc_rx_msg(hif_dev->htc_handle, skb, 574 skb->len, USB_REG_IN_PIPE); 575 576 577 nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC); 578 if (!nskb) { 579 dev_err(&hif_dev->udev->dev, 580 "ath9k_htc: REG_IN memory allocation failure\n"); 581 urb->context = NULL; 582 return; 583 } 584 585 usb_fill_bulk_urb(urb, hif_dev->udev, 586 usb_rcvbulkpipe(hif_dev->udev, 587 USB_REG_IN_PIPE), 588 nskb->data, MAX_REG_IN_BUF_SIZE, 589 ath9k_hif_usb_reg_in_cb, nskb); 590 591 ret = usb_submit_urb(urb, GFP_ATOMIC); 592 if (ret) { 593 kfree_skb(nskb); 594 urb->context = NULL; 595 } 596 597 return; 598 } 599 600 resubmit: 601 skb_reset_tail_pointer(skb); 602 skb_trim(skb, 0); 603 604 ret = usb_submit_urb(urb, GFP_ATOMIC); 605 if (ret) 606 goto free; 607 608 return; 609 free: 610 kfree_skb(skb); 611 urb->context = NULL; 612 } 613 614 static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev) 615 { 616 struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL; 617 unsigned long flags; 618 619 list_for_each_entry_safe(tx_buf, tx_buf_tmp, 620 &hif_dev->tx.tx_buf, list) { 621 usb_kill_urb(tx_buf->urb); 622 list_del(&tx_buf->list); 623 usb_free_urb(tx_buf->urb); 624 kfree(tx_buf->buf); 625 kfree(tx_buf); 626 } 627 628 spin_lock_irqsave(&hif_dev->tx.tx_lock, flags); 629 hif_dev->tx.flags |= HIF_USB_TX_FLUSH; 630 spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags); 631 632 list_for_each_entry_safe(tx_buf, tx_buf_tmp, 633 &hif_dev->tx.tx_pending, list) { 634 usb_kill_urb(tx_buf->urb); 635 list_del(&tx_buf->list); 636 usb_free_urb(tx_buf->urb); 637 kfree(tx_buf->buf); 638 kfree(tx_buf); 639 } 640 } 641 642 static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev) 643 { 644 struct tx_buf *tx_buf; 645 int i; 646 647 INIT_LIST_HEAD(&hif_dev->tx.tx_buf); 648 INIT_LIST_HEAD(&hif_dev->tx.tx_pending); 649 spin_lock_init(&hif_dev->tx.tx_lock); 650 __skb_queue_head_init(&hif_dev->tx.tx_skb_queue); 651 652 for (i = 0; i < MAX_TX_URB_NUM; i++) { 653 tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL); 654 if (!tx_buf) 655 goto err; 656 657 tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL); 658 if (!tx_buf->buf) 659 goto err; 660 661 tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL); 662 if (!tx_buf->urb) 663 goto err; 664 665 tx_buf->hif_dev = hif_dev; 666 __skb_queue_head_init(&tx_buf->skb_queue); 667 668 list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf); 669 } 670 671 hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM; 672 673 return 0; 674 err: 675 if (tx_buf) { 676 kfree(tx_buf->buf); 677 kfree(tx_buf); 678 } 679 ath9k_hif_usb_dealloc_tx_urbs(hif_dev); 680 return -ENOMEM; 681 } 682 683 static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev) 684 { 685 usb_kill_anchored_urbs(&hif_dev->rx_submitted); 686 } 687 688 static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev) 689 { 690 struct urb *urb = NULL; 691 struct sk_buff *skb = NULL; 692 int i, ret; 693 694 init_usb_anchor(&hif_dev->rx_submitted); 695 spin_lock_init(&hif_dev->rx_lock); 696 697 for (i = 0; i < MAX_RX_URB_NUM; i++) { 698 699 /* Allocate URB */ 700 urb = usb_alloc_urb(0, GFP_KERNEL); 701 if (urb == NULL) { 702 ret = -ENOMEM; 703 goto err_urb; 704 } 705 706 /* Allocate buffer */ 707 skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL); 708 if (!skb) { 709 ret = -ENOMEM; 710 goto err_skb; 711 } 712 713 usb_fill_bulk_urb(urb, hif_dev->udev, 714 usb_rcvbulkpipe(hif_dev->udev, 715 USB_WLAN_RX_PIPE), 716 skb->data, MAX_RX_BUF_SIZE, 717 ath9k_hif_usb_rx_cb, skb); 718 719 /* Anchor URB */ 720 usb_anchor_urb(urb, &hif_dev->rx_submitted); 721 722 /* Submit URB */ 723 ret = usb_submit_urb(urb, GFP_KERNEL); 724 if (ret) { 725 usb_unanchor_urb(urb); 726 goto err_submit; 727 } 728 729 /* 730 * Drop reference count. 731 * This ensures that the URB is freed when killing them. 732 */ 733 usb_free_urb(urb); 734 } 735 736 return 0; 737 738 err_submit: 739 kfree_skb(skb); 740 err_skb: 741 usb_free_urb(urb); 742 err_urb: 743 ath9k_hif_usb_dealloc_rx_urbs(hif_dev); 744 return ret; 745 } 746 747 static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev) 748 { 749 if (hif_dev->reg_in_urb) { 750 usb_kill_urb(hif_dev->reg_in_urb); 751 if (hif_dev->reg_in_urb->context) 752 kfree_skb((void *)hif_dev->reg_in_urb->context); 753 usb_free_urb(hif_dev->reg_in_urb); 754 hif_dev->reg_in_urb = NULL; 755 } 756 } 757 758 static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev) 759 { 760 struct sk_buff *skb; 761 762 hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL); 763 if (hif_dev->reg_in_urb == NULL) 764 return -ENOMEM; 765 766 skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL); 767 if (!skb) 768 goto err; 769 770 usb_fill_bulk_urb(hif_dev->reg_in_urb, hif_dev->udev, 771 usb_rcvbulkpipe(hif_dev->udev, 772 USB_REG_IN_PIPE), 773 skb->data, MAX_REG_IN_BUF_SIZE, 774 ath9k_hif_usb_reg_in_cb, skb); 775 776 if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0) 777 goto err; 778 779 return 0; 780 781 err: 782 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev); 783 return -ENOMEM; 784 } 785 786 static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev) 787 { 788 /* Register Write */ 789 init_usb_anchor(&hif_dev->regout_submitted); 790 791 /* TX */ 792 if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0) 793 goto err; 794 795 /* RX */ 796 if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0) 797 goto err_rx; 798 799 /* Register Read */ 800 if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0) 801 goto err_reg; 802 803 return 0; 804 err_reg: 805 ath9k_hif_usb_dealloc_rx_urbs(hif_dev); 806 err_rx: 807 ath9k_hif_usb_dealloc_tx_urbs(hif_dev); 808 err: 809 return -ENOMEM; 810 } 811 812 static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev) 813 { 814 usb_kill_anchored_urbs(&hif_dev->regout_submitted); 815 ath9k_hif_usb_dealloc_reg_in_urb(hif_dev); 816 ath9k_hif_usb_dealloc_tx_urbs(hif_dev); 817 ath9k_hif_usb_dealloc_rx_urbs(hif_dev); 818 } 819 820 static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev, 821 u32 drv_info) 822 { 823 int transfer, err; 824 const void *data = hif_dev->firmware->data; 825 size_t len = hif_dev->firmware->size; 826 u32 addr = AR9271_FIRMWARE; 827 u8 *buf = kzalloc(4096, GFP_KERNEL); 828 u32 firm_offset; 829 830 if (!buf) 831 return -ENOMEM; 832 833 while (len) { 834 transfer = min_t(int, len, 4096); 835 memcpy(buf, data, transfer); 836 837 err = usb_control_msg(hif_dev->udev, 838 usb_sndctrlpipe(hif_dev->udev, 0), 839 FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT, 840 addr >> 8, 0, buf, transfer, HZ); 841 if (err < 0) { 842 kfree(buf); 843 return err; 844 } 845 846 len -= transfer; 847 data += transfer; 848 addr += transfer; 849 } 850 kfree(buf); 851 852 if (IS_AR7010_DEVICE(drv_info)) 853 firm_offset = AR7010_FIRMWARE_TEXT; 854 else 855 firm_offset = AR9271_FIRMWARE_TEXT; 856 857 /* 858 * Issue FW download complete command to firmware. 859 */ 860 err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0), 861 FIRMWARE_DOWNLOAD_COMP, 862 0x40 | USB_DIR_OUT, 863 firm_offset >> 8, 0, NULL, 0, HZ); 864 if (err) 865 return -EIO; 866 867 dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n", 868 hif_dev->fw_name, (unsigned long) hif_dev->firmware->size); 869 870 return 0; 871 } 872 873 static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev, u32 drv_info) 874 { 875 int ret, idx; 876 struct usb_host_interface *alt = &hif_dev->interface->altsetting[0]; 877 struct usb_endpoint_descriptor *endp; 878 879 /* Request firmware */ 880 ret = request_firmware(&hif_dev->firmware, hif_dev->fw_name, 881 &hif_dev->udev->dev); 882 if (ret) { 883 dev_err(&hif_dev->udev->dev, 884 "ath9k_htc: Firmware - %s not found\n", hif_dev->fw_name); 885 goto err_fw_req; 886 } 887 888 /* Download firmware */ 889 ret = ath9k_hif_usb_download_fw(hif_dev, drv_info); 890 if (ret) { 891 dev_err(&hif_dev->udev->dev, 892 "ath9k_htc: Firmware - %s download failed\n", 893 hif_dev->fw_name); 894 goto err_fw_download; 895 } 896 897 /* On downloading the firmware to the target, the USB descriptor of EP4 898 * is 'patched' to change the type of the endpoint to Bulk. This will 899 * bring down CPU usage during the scan period. 900 */ 901 for (idx = 0; idx < alt->desc.bNumEndpoints; idx++) { 902 endp = &alt->endpoint[idx].desc; 903 if ((endp->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) 904 == USB_ENDPOINT_XFER_INT) { 905 endp->bmAttributes &= ~USB_ENDPOINT_XFERTYPE_MASK; 906 endp->bmAttributes |= USB_ENDPOINT_XFER_BULK; 907 endp->bInterval = 0; 908 } 909 } 910 911 /* Alloc URBs */ 912 ret = ath9k_hif_usb_alloc_urbs(hif_dev); 913 if (ret) { 914 dev_err(&hif_dev->udev->dev, 915 "ath9k_htc: Unable to allocate URBs\n"); 916 goto err_urb; 917 } 918 919 return 0; 920 921 err_urb: 922 ath9k_hif_usb_dealloc_urbs(hif_dev); 923 err_fw_download: 924 release_firmware(hif_dev->firmware); 925 err_fw_req: 926 hif_dev->firmware = NULL; 927 return ret; 928 } 929 930 static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev) 931 { 932 ath9k_hif_usb_dealloc_urbs(hif_dev); 933 if (hif_dev->firmware) 934 release_firmware(hif_dev->firmware); 935 } 936 937 static int ath9k_hif_usb_probe(struct usb_interface *interface, 938 const struct usb_device_id *id) 939 { 940 struct usb_device *udev = interface_to_usbdev(interface); 941 struct hif_device_usb *hif_dev; 942 int ret = 0; 943 944 hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL); 945 if (!hif_dev) { 946 ret = -ENOMEM; 947 goto err_alloc; 948 } 949 950 usb_get_dev(udev); 951 hif_dev->udev = udev; 952 hif_dev->interface = interface; 953 hif_dev->device_id = id->idProduct; 954 #ifdef CONFIG_PM 955 udev->reset_resume = 1; 956 #endif 957 usb_set_intfdata(interface, hif_dev); 958 959 hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb, 960 &hif_dev->udev->dev); 961 if (hif_dev->htc_handle == NULL) { 962 ret = -ENOMEM; 963 goto err_htc_hw_alloc; 964 } 965 966 /* Find out which firmware to load */ 967 968 if (IS_AR7010_DEVICE(id->driver_info)) 969 if (le16_to_cpu(udev->descriptor.bcdDevice) == 0x0202) 970 hif_dev->fw_name = FIRMWARE_AR7010_1_1; 971 else 972 hif_dev->fw_name = FIRMWARE_AR7010; 973 else 974 hif_dev->fw_name = FIRMWARE_AR9271; 975 976 ret = ath9k_hif_usb_dev_init(hif_dev, id->driver_info); 977 if (ret) { 978 ret = -EINVAL; 979 goto err_hif_init_usb; 980 } 981 982 ret = ath9k_htc_hw_init(hif_dev->htc_handle, 983 &hif_dev->udev->dev, hif_dev->device_id, 984 hif_dev->udev->product, id->driver_info); 985 if (ret) { 986 ret = -EINVAL; 987 goto err_htc_hw_init; 988 } 989 990 dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n"); 991 992 return 0; 993 994 err_htc_hw_init: 995 ath9k_hif_usb_dev_deinit(hif_dev); 996 err_hif_init_usb: 997 ath9k_htc_hw_free(hif_dev->htc_handle); 998 err_htc_hw_alloc: 999 usb_set_intfdata(interface, NULL); 1000 kfree(hif_dev); 1001 usb_put_dev(udev); 1002 err_alloc: 1003 return ret; 1004 } 1005 1006 static void ath9k_hif_usb_reboot(struct usb_device *udev) 1007 { 1008 u32 reboot_cmd = 0xffffffff; 1009 void *buf; 1010 int ret; 1011 1012 buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL); 1013 if (!buf) 1014 return; 1015 1016 ret = usb_bulk_msg(udev, usb_sndbulkpipe(udev, USB_REG_OUT_PIPE), 1017 buf, 4, NULL, HZ); 1018 if (ret) 1019 dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n"); 1020 1021 kfree(buf); 1022 } 1023 1024 static void ath9k_hif_usb_disconnect(struct usb_interface *interface) 1025 { 1026 struct usb_device *udev = interface_to_usbdev(interface); 1027 struct hif_device_usb *hif_dev = usb_get_intfdata(interface); 1028 bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false; 1029 1030 if (hif_dev) { 1031 ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged); 1032 ath9k_htc_hw_free(hif_dev->htc_handle); 1033 ath9k_hif_usb_dev_deinit(hif_dev); 1034 usb_set_intfdata(interface, NULL); 1035 } 1036 1037 if (!unplugged && (hif_dev->flags & HIF_USB_START)) 1038 ath9k_hif_usb_reboot(udev); 1039 1040 kfree(hif_dev); 1041 dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n"); 1042 usb_put_dev(udev); 1043 } 1044 1045 #ifdef CONFIG_PM 1046 static int ath9k_hif_usb_suspend(struct usb_interface *interface, 1047 pm_message_t message) 1048 { 1049 struct hif_device_usb *hif_dev = usb_get_intfdata(interface); 1050 1051 /* 1052 * The device has to be set to FULLSLEEP mode in case no 1053 * interface is up. 1054 */ 1055 if (!(hif_dev->flags & HIF_USB_START)) 1056 ath9k_htc_suspend(hif_dev->htc_handle); 1057 1058 ath9k_hif_usb_dealloc_urbs(hif_dev); 1059 1060 return 0; 1061 } 1062 1063 static int ath9k_hif_usb_resume(struct usb_interface *interface) 1064 { 1065 struct hif_device_usb *hif_dev = usb_get_intfdata(interface); 1066 struct htc_target *htc_handle = hif_dev->htc_handle; 1067 int ret; 1068 1069 ret = ath9k_hif_usb_alloc_urbs(hif_dev); 1070 if (ret) 1071 return ret; 1072 1073 if (hif_dev->firmware) { 1074 ret = ath9k_hif_usb_download_fw(hif_dev, 1075 htc_handle->drv_priv->ah->hw_version.usbdev); 1076 if (ret) 1077 goto fail_resume; 1078 } else { 1079 ath9k_hif_usb_dealloc_urbs(hif_dev); 1080 return -EIO; 1081 } 1082 1083 mdelay(100); 1084 1085 ret = ath9k_htc_resume(htc_handle); 1086 1087 if (ret) 1088 goto fail_resume; 1089 1090 return 0; 1091 1092 fail_resume: 1093 ath9k_hif_usb_dealloc_urbs(hif_dev); 1094 1095 return ret; 1096 } 1097 #endif 1098 1099 static struct usb_driver ath9k_hif_usb_driver = { 1100 .name = "ath9k_hif_usb", 1101 .probe = ath9k_hif_usb_probe, 1102 .disconnect = ath9k_hif_usb_disconnect, 1103 #ifdef CONFIG_PM 1104 .suspend = ath9k_hif_usb_suspend, 1105 .resume = ath9k_hif_usb_resume, 1106 .reset_resume = ath9k_hif_usb_resume, 1107 #endif 1108 .id_table = ath9k_hif_usb_ids, 1109 .soft_unbind = 1, 1110 }; 1111 1112 int ath9k_hif_usb_init(void) 1113 { 1114 return usb_register(&ath9k_hif_usb_driver); 1115 } 1116 1117 void ath9k_hif_usb_exit(void) 1118 { 1119 usb_deregister(&ath9k_hif_usb_driver); 1120 } 1121