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