1 // SPDX-License-Identifier: ISC 2 /* 3 * Copyright (c) 2007-2011 Atheros Communications Inc. 4 * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc. 5 * Copyright (c) 2016-2017 Erik Stromdahl <erik.stromdahl@gmail.com> 6 */ 7 8 #include <linux/module.h> 9 #include <linux/usb.h> 10 11 #include "debug.h" 12 #include "core.h" 13 #include "bmi.h" 14 #include "hif.h" 15 #include "htc.h" 16 #include "usb.h" 17 18 static void ath10k_usb_post_recv_transfers(struct ath10k *ar, 19 struct ath10k_usb_pipe *recv_pipe); 20 21 /* inlined helper functions */ 22 23 static inline enum ath10k_htc_ep_id 24 eid_from_htc_hdr(struct ath10k_htc_hdr *htc_hdr) 25 { 26 return (enum ath10k_htc_ep_id)htc_hdr->eid; 27 } 28 29 static inline bool is_trailer_only_msg(struct ath10k_htc_hdr *htc_hdr) 30 { 31 return __le16_to_cpu(htc_hdr->len) == htc_hdr->trailer_len; 32 } 33 34 /* pipe/urb operations */ 35 static struct ath10k_urb_context * 36 ath10k_usb_alloc_urb_from_pipe(struct ath10k_usb_pipe *pipe) 37 { 38 struct ath10k_urb_context *urb_context = NULL; 39 unsigned long flags; 40 41 spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags); 42 if (!list_empty(&pipe->urb_list_head)) { 43 urb_context = list_first_entry(&pipe->urb_list_head, 44 struct ath10k_urb_context, link); 45 list_del(&urb_context->link); 46 pipe->urb_cnt--; 47 } 48 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags); 49 50 return urb_context; 51 } 52 53 static void ath10k_usb_free_urb_to_pipe(struct ath10k_usb_pipe *pipe, 54 struct ath10k_urb_context *urb_context) 55 { 56 unsigned long flags; 57 58 spin_lock_irqsave(&pipe->ar_usb->cs_lock, flags); 59 60 pipe->urb_cnt++; 61 list_add(&urb_context->link, &pipe->urb_list_head); 62 63 spin_unlock_irqrestore(&pipe->ar_usb->cs_lock, flags); 64 } 65 66 static void ath10k_usb_cleanup_recv_urb(struct ath10k_urb_context *urb_context) 67 { 68 dev_kfree_skb(urb_context->skb); 69 urb_context->skb = NULL; 70 71 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context); 72 } 73 74 static void ath10k_usb_free_pipe_resources(struct ath10k *ar, 75 struct ath10k_usb_pipe *pipe) 76 { 77 struct ath10k_urb_context *urb_context; 78 79 if (!pipe->ar_usb) { 80 /* nothing allocated for this pipe */ 81 return; 82 } 83 84 ath10k_dbg(ar, ATH10K_DBG_USB, 85 "usb free resources lpipe %d hpipe 0x%x urbs %d avail %d\n", 86 pipe->logical_pipe_num, pipe->usb_pipe_handle, 87 pipe->urb_alloc, pipe->urb_cnt); 88 89 if (pipe->urb_alloc != pipe->urb_cnt) { 90 ath10k_dbg(ar, ATH10K_DBG_USB, 91 "usb urb leak lpipe %d hpipe 0x%x urbs %d avail %d\n", 92 pipe->logical_pipe_num, pipe->usb_pipe_handle, 93 pipe->urb_alloc, pipe->urb_cnt); 94 } 95 96 for (;;) { 97 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe); 98 99 if (!urb_context) 100 break; 101 102 kfree(urb_context); 103 } 104 } 105 106 static void ath10k_usb_cleanup_pipe_resources(struct ath10k *ar) 107 { 108 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 109 int i; 110 111 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) 112 ath10k_usb_free_pipe_resources(ar, &ar_usb->pipes[i]); 113 } 114 115 /* hif usb rx/tx completion functions */ 116 117 static void ath10k_usb_recv_complete(struct urb *urb) 118 { 119 struct ath10k_urb_context *urb_context = urb->context; 120 struct ath10k_usb_pipe *pipe = urb_context->pipe; 121 struct ath10k *ar = pipe->ar_usb->ar; 122 struct sk_buff *skb; 123 int status = 0; 124 125 ath10k_dbg(ar, ATH10K_DBG_USB_BULK, 126 "usb recv pipe %d stat %d len %d urb 0x%pK\n", 127 pipe->logical_pipe_num, urb->status, urb->actual_length, 128 urb); 129 130 if (urb->status != 0) { 131 status = -EIO; 132 switch (urb->status) { 133 case -ECONNRESET: 134 case -ENOENT: 135 case -ESHUTDOWN: 136 /* no need to spew these errors when device 137 * removed or urb killed due to driver shutdown 138 */ 139 status = -ECANCELED; 140 break; 141 default: 142 ath10k_dbg(ar, ATH10K_DBG_USB_BULK, 143 "usb recv pipe %d ep 0x%2.2x failed: %d\n", 144 pipe->logical_pipe_num, 145 pipe->ep_address, urb->status); 146 break; 147 } 148 goto cleanup_recv_urb; 149 } 150 151 if (urb->actual_length == 0) 152 goto cleanup_recv_urb; 153 154 skb = urb_context->skb; 155 156 /* we are going to pass it up */ 157 urb_context->skb = NULL; 158 skb_put(skb, urb->actual_length); 159 160 /* note: queue implements a lock */ 161 skb_queue_tail(&pipe->io_comp_queue, skb); 162 schedule_work(&pipe->io_complete_work); 163 164 cleanup_recv_urb: 165 ath10k_usb_cleanup_recv_urb(urb_context); 166 167 if (status == 0 && 168 pipe->urb_cnt >= pipe->urb_cnt_thresh) { 169 /* our free urbs are piling up, post more transfers */ 170 ath10k_usb_post_recv_transfers(ar, pipe); 171 } 172 } 173 174 static void ath10k_usb_transmit_complete(struct urb *urb) 175 { 176 struct ath10k_urb_context *urb_context = urb->context; 177 struct ath10k_usb_pipe *pipe = urb_context->pipe; 178 struct ath10k *ar = pipe->ar_usb->ar; 179 struct sk_buff *skb; 180 181 if (urb->status != 0) { 182 ath10k_dbg(ar, ATH10K_DBG_USB_BULK, 183 "pipe: %d, failed:%d\n", 184 pipe->logical_pipe_num, urb->status); 185 } 186 187 skb = urb_context->skb; 188 urb_context->skb = NULL; 189 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context); 190 191 /* note: queue implements a lock */ 192 skb_queue_tail(&pipe->io_comp_queue, skb); 193 schedule_work(&pipe->io_complete_work); 194 } 195 196 /* pipe operations */ 197 static void ath10k_usb_post_recv_transfers(struct ath10k *ar, 198 struct ath10k_usb_pipe *recv_pipe) 199 { 200 struct ath10k_urb_context *urb_context; 201 struct urb *urb; 202 int usb_status; 203 204 for (;;) { 205 urb_context = ath10k_usb_alloc_urb_from_pipe(recv_pipe); 206 if (!urb_context) 207 break; 208 209 urb_context->skb = dev_alloc_skb(ATH10K_USB_RX_BUFFER_SIZE); 210 if (!urb_context->skb) 211 goto err; 212 213 urb = usb_alloc_urb(0, GFP_ATOMIC); 214 if (!urb) 215 goto err; 216 217 usb_fill_bulk_urb(urb, 218 recv_pipe->ar_usb->udev, 219 recv_pipe->usb_pipe_handle, 220 urb_context->skb->data, 221 ATH10K_USB_RX_BUFFER_SIZE, 222 ath10k_usb_recv_complete, urb_context); 223 224 ath10k_dbg(ar, ATH10K_DBG_USB_BULK, 225 "usb bulk recv submit %d 0x%x ep 0x%2.2x len %d buf 0x%pK\n", 226 recv_pipe->logical_pipe_num, 227 recv_pipe->usb_pipe_handle, recv_pipe->ep_address, 228 ATH10K_USB_RX_BUFFER_SIZE, urb_context->skb); 229 230 usb_anchor_urb(urb, &recv_pipe->urb_submitted); 231 usb_status = usb_submit_urb(urb, GFP_ATOMIC); 232 233 if (usb_status) { 234 ath10k_dbg(ar, ATH10K_DBG_USB_BULK, 235 "usb bulk recv failed: %d\n", 236 usb_status); 237 usb_unanchor_urb(urb); 238 usb_free_urb(urb); 239 goto err; 240 } 241 usb_free_urb(urb); 242 } 243 244 return; 245 246 err: 247 ath10k_usb_cleanup_recv_urb(urb_context); 248 } 249 250 static void ath10k_usb_flush_all(struct ath10k *ar) 251 { 252 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 253 int i; 254 255 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) { 256 if (ar_usb->pipes[i].ar_usb) { 257 usb_kill_anchored_urbs(&ar_usb->pipes[i].urb_submitted); 258 cancel_work_sync(&ar_usb->pipes[i].io_complete_work); 259 } 260 } 261 } 262 263 static void ath10k_usb_start_recv_pipes(struct ath10k *ar) 264 { 265 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 266 267 ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA].urb_cnt_thresh = 1; 268 269 ath10k_usb_post_recv_transfers(ar, 270 &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]); 271 } 272 273 static void ath10k_usb_tx_complete(struct ath10k *ar, struct sk_buff *skb) 274 { 275 struct ath10k_htc_hdr *htc_hdr; 276 struct ath10k_htc_ep *ep; 277 278 htc_hdr = (struct ath10k_htc_hdr *)skb->data; 279 ep = &ar->htc.endpoint[htc_hdr->eid]; 280 ath10k_htc_notify_tx_completion(ep, skb); 281 /* The TX complete handler now owns the skb... */ 282 } 283 284 static void ath10k_usb_rx_complete(struct ath10k *ar, struct sk_buff *skb) 285 { 286 struct ath10k_htc *htc = &ar->htc; 287 struct ath10k_htc_hdr *htc_hdr; 288 enum ath10k_htc_ep_id eid; 289 struct ath10k_htc_ep *ep; 290 u16 payload_len; 291 u8 *trailer; 292 int ret; 293 294 htc_hdr = (struct ath10k_htc_hdr *)skb->data; 295 eid = eid_from_htc_hdr(htc_hdr); 296 ep = &ar->htc.endpoint[eid]; 297 298 if (ep->service_id == 0) { 299 ath10k_warn(ar, "ep %d is not connected\n", eid); 300 goto out_free_skb; 301 } 302 303 payload_len = le16_to_cpu(htc_hdr->len); 304 if (!payload_len) { 305 ath10k_warn(ar, "zero length frame received, firmware crashed?\n"); 306 goto out_free_skb; 307 } 308 309 if (payload_len < htc_hdr->trailer_len) { 310 ath10k_warn(ar, "malformed frame received, firmware crashed?\n"); 311 goto out_free_skb; 312 } 313 314 if (htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT) { 315 trailer = skb->data + sizeof(*htc_hdr) + payload_len - 316 htc_hdr->trailer_len; 317 318 ret = ath10k_htc_process_trailer(htc, 319 trailer, 320 htc_hdr->trailer_len, 321 eid, 322 NULL, 323 NULL); 324 if (ret) 325 goto out_free_skb; 326 327 if (is_trailer_only_msg(htc_hdr)) 328 goto out_free_skb; 329 330 /* strip off the trailer from the skb since it should not 331 * be passed on to upper layers 332 */ 333 skb_trim(skb, skb->len - htc_hdr->trailer_len); 334 } 335 336 skb_pull(skb, sizeof(*htc_hdr)); 337 ep->ep_ops.ep_rx_complete(ar, skb); 338 /* The RX complete handler now owns the skb... */ 339 340 return; 341 342 out_free_skb: 343 dev_kfree_skb(skb); 344 } 345 346 static void ath10k_usb_io_comp_work(struct work_struct *work) 347 { 348 struct ath10k_usb_pipe *pipe = container_of(work, 349 struct ath10k_usb_pipe, 350 io_complete_work); 351 struct ath10k *ar = pipe->ar_usb->ar; 352 struct sk_buff *skb; 353 354 while ((skb = skb_dequeue(&pipe->io_comp_queue))) { 355 if (pipe->flags & ATH10K_USB_PIPE_FLAG_TX) 356 ath10k_usb_tx_complete(ar, skb); 357 else 358 ath10k_usb_rx_complete(ar, skb); 359 } 360 } 361 362 #define ATH10K_USB_MAX_DIAG_CMD (sizeof(struct ath10k_usb_ctrl_diag_cmd_write)) 363 #define ATH10K_USB_MAX_DIAG_RESP (sizeof(struct ath10k_usb_ctrl_diag_resp_read)) 364 365 static void ath10k_usb_destroy(struct ath10k *ar) 366 { 367 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 368 369 ath10k_usb_flush_all(ar); 370 ath10k_usb_cleanup_pipe_resources(ar); 371 usb_set_intfdata(ar_usb->interface, NULL); 372 373 kfree(ar_usb->diag_cmd_buffer); 374 kfree(ar_usb->diag_resp_buffer); 375 } 376 377 static int ath10k_usb_hif_start(struct ath10k *ar) 378 { 379 int i; 380 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 381 382 ath10k_usb_start_recv_pipes(ar); 383 384 /* set the TX resource avail threshold for each TX pipe */ 385 for (i = ATH10K_USB_PIPE_TX_CTRL; 386 i <= ATH10K_USB_PIPE_TX_DATA_HP; i++) { 387 ar_usb->pipes[i].urb_cnt_thresh = 388 ar_usb->pipes[i].urb_alloc / 2; 389 } 390 391 return 0; 392 } 393 394 static int ath10k_usb_hif_tx_sg(struct ath10k *ar, u8 pipe_id, 395 struct ath10k_hif_sg_item *items, int n_items) 396 { 397 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 398 struct ath10k_usb_pipe *pipe = &ar_usb->pipes[pipe_id]; 399 struct ath10k_urb_context *urb_context; 400 struct sk_buff *skb; 401 struct urb *urb; 402 int ret, i; 403 404 for (i = 0; i < n_items; i++) { 405 urb_context = ath10k_usb_alloc_urb_from_pipe(pipe); 406 if (!urb_context) { 407 ret = -ENOMEM; 408 goto err; 409 } 410 411 skb = items[i].transfer_context; 412 urb_context->skb = skb; 413 414 urb = usb_alloc_urb(0, GFP_ATOMIC); 415 if (!urb) { 416 ret = -ENOMEM; 417 goto err_free_urb_to_pipe; 418 } 419 420 usb_fill_bulk_urb(urb, 421 ar_usb->udev, 422 pipe->usb_pipe_handle, 423 skb->data, 424 skb->len, 425 ath10k_usb_transmit_complete, urb_context); 426 427 if (!(skb->len % pipe->max_packet_size)) { 428 /* hit a max packet boundary on this pipe */ 429 urb->transfer_flags |= URB_ZERO_PACKET; 430 } 431 432 usb_anchor_urb(urb, &pipe->urb_submitted); 433 ret = usb_submit_urb(urb, GFP_ATOMIC); 434 if (ret) { 435 ath10k_dbg(ar, ATH10K_DBG_USB_BULK, 436 "usb bulk transmit failed: %d\n", ret); 437 usb_unanchor_urb(urb); 438 usb_free_urb(urb); 439 ret = -EINVAL; 440 goto err_free_urb_to_pipe; 441 } 442 443 usb_free_urb(urb); 444 } 445 446 return 0; 447 448 err_free_urb_to_pipe: 449 ath10k_usb_free_urb_to_pipe(urb_context->pipe, urb_context); 450 err: 451 return ret; 452 } 453 454 static void ath10k_usb_hif_stop(struct ath10k *ar) 455 { 456 ath10k_usb_flush_all(ar); 457 } 458 459 static u16 ath10k_usb_hif_get_free_queue_number(struct ath10k *ar, u8 pipe_id) 460 { 461 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 462 463 return ar_usb->pipes[pipe_id].urb_cnt; 464 } 465 466 static int ath10k_usb_submit_ctrl_out(struct ath10k *ar, 467 u8 req, u16 value, u16 index, void *data, 468 u32 size) 469 { 470 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 471 u8 *buf = NULL; 472 int ret; 473 474 if (size > 0) { 475 buf = kmemdup(data, size, GFP_KERNEL); 476 if (!buf) 477 return -ENOMEM; 478 } 479 480 /* note: if successful returns number of bytes transferred */ 481 ret = usb_control_msg(ar_usb->udev, 482 usb_sndctrlpipe(ar_usb->udev, 0), 483 req, 484 USB_DIR_OUT | USB_TYPE_VENDOR | 485 USB_RECIP_DEVICE, value, index, buf, 486 size, 1000); 487 488 if (ret < 0) { 489 ath10k_warn(ar, "Failed to submit usb control message: %d\n", 490 ret); 491 kfree(buf); 492 return ret; 493 } 494 495 kfree(buf); 496 497 return 0; 498 } 499 500 static int ath10k_usb_submit_ctrl_in(struct ath10k *ar, 501 u8 req, u16 value, u16 index, void *data, 502 u32 size) 503 { 504 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 505 u8 *buf = NULL; 506 int ret; 507 508 if (size > 0) { 509 buf = kmalloc(size, GFP_KERNEL); 510 if (!buf) 511 return -ENOMEM; 512 } 513 514 /* note: if successful returns number of bytes transferred */ 515 ret = usb_control_msg(ar_usb->udev, 516 usb_rcvctrlpipe(ar_usb->udev, 0), 517 req, 518 USB_DIR_IN | USB_TYPE_VENDOR | 519 USB_RECIP_DEVICE, value, index, buf, 520 size, 2 * HZ); 521 522 if (ret < 0) { 523 ath10k_warn(ar, "Failed to read usb control message: %d\n", 524 ret); 525 kfree(buf); 526 return ret; 527 } 528 529 memcpy((u8 *)data, buf, size); 530 531 kfree(buf); 532 533 return 0; 534 } 535 536 static int ath10k_usb_ctrl_msg_exchange(struct ath10k *ar, 537 u8 req_val, u8 *req_buf, u32 req_len, 538 u8 resp_val, u8 *resp_buf, 539 u32 *resp_len) 540 { 541 int ret; 542 543 /* send command */ 544 ret = ath10k_usb_submit_ctrl_out(ar, req_val, 0, 0, 545 req_buf, req_len); 546 if (ret) 547 goto err; 548 549 /* get response */ 550 if (resp_buf) { 551 ret = ath10k_usb_submit_ctrl_in(ar, resp_val, 0, 0, 552 resp_buf, *resp_len); 553 if (ret) 554 goto err; 555 } 556 557 return 0; 558 err: 559 return ret; 560 } 561 562 static int ath10k_usb_hif_diag_read(struct ath10k *ar, u32 address, void *buf, 563 size_t buf_len) 564 { 565 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 566 struct ath10k_usb_ctrl_diag_cmd_read *cmd; 567 u32 resp_len; 568 int ret; 569 570 if (buf_len < sizeof(struct ath10k_usb_ctrl_diag_resp_read)) 571 return -EINVAL; 572 573 cmd = (struct ath10k_usb_ctrl_diag_cmd_read *)ar_usb->diag_cmd_buffer; 574 memset(cmd, 0, sizeof(*cmd)); 575 cmd->cmd = ATH10K_USB_CTRL_DIAG_CC_READ; 576 cmd->address = cpu_to_le32(address); 577 resp_len = sizeof(struct ath10k_usb_ctrl_diag_resp_read); 578 579 ret = ath10k_usb_ctrl_msg_exchange(ar, 580 ATH10K_USB_CONTROL_REQ_DIAG_CMD, 581 (u8 *)cmd, 582 sizeof(*cmd), 583 ATH10K_USB_CONTROL_REQ_DIAG_RESP, 584 ar_usb->diag_resp_buffer, &resp_len); 585 if (ret) 586 return ret; 587 588 if (resp_len != sizeof(struct ath10k_usb_ctrl_diag_resp_read)) 589 return -EMSGSIZE; 590 591 memcpy(buf, ar_usb->diag_resp_buffer, 592 sizeof(struct ath10k_usb_ctrl_diag_resp_read)); 593 594 return 0; 595 } 596 597 static int ath10k_usb_hif_diag_write(struct ath10k *ar, u32 address, 598 const void *data, int nbytes) 599 { 600 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 601 struct ath10k_usb_ctrl_diag_cmd_write *cmd; 602 int ret; 603 604 if (nbytes != sizeof(cmd->value)) 605 return -EINVAL; 606 607 cmd = (struct ath10k_usb_ctrl_diag_cmd_write *)ar_usb->diag_cmd_buffer; 608 memset(cmd, 0, sizeof(*cmd)); 609 cmd->cmd = cpu_to_le32(ATH10K_USB_CTRL_DIAG_CC_WRITE); 610 cmd->address = cpu_to_le32(address); 611 memcpy(&cmd->value, data, nbytes); 612 613 ret = ath10k_usb_ctrl_msg_exchange(ar, 614 ATH10K_USB_CONTROL_REQ_DIAG_CMD, 615 (u8 *)cmd, 616 sizeof(*cmd), 617 0, NULL, NULL); 618 if (ret) 619 return ret; 620 621 return 0; 622 } 623 624 static int ath10k_usb_bmi_exchange_msg(struct ath10k *ar, 625 void *req, u32 req_len, 626 void *resp, u32 *resp_len) 627 { 628 int ret; 629 630 if (req) { 631 ret = ath10k_usb_submit_ctrl_out(ar, 632 ATH10K_USB_CONTROL_REQ_SEND_BMI_CMD, 633 0, 0, req, req_len); 634 if (ret) { 635 ath10k_warn(ar, 636 "unable to send the bmi data to the device: %d\n", 637 ret); 638 return ret; 639 } 640 } 641 642 if (resp) { 643 ret = ath10k_usb_submit_ctrl_in(ar, 644 ATH10K_USB_CONTROL_REQ_RECV_BMI_RESP, 645 0, 0, resp, *resp_len); 646 if (ret) { 647 ath10k_warn(ar, 648 "Unable to read the bmi data from the device: %d\n", 649 ret); 650 return ret; 651 } 652 } 653 654 return 0; 655 } 656 657 static void ath10k_usb_hif_get_default_pipe(struct ath10k *ar, 658 u8 *ul_pipe, u8 *dl_pipe) 659 { 660 *ul_pipe = ATH10K_USB_PIPE_TX_CTRL; 661 *dl_pipe = ATH10K_USB_PIPE_RX_CTRL; 662 } 663 664 static int ath10k_usb_hif_map_service_to_pipe(struct ath10k *ar, u16 svc_id, 665 u8 *ul_pipe, u8 *dl_pipe) 666 { 667 switch (svc_id) { 668 case ATH10K_HTC_SVC_ID_RSVD_CTRL: 669 case ATH10K_HTC_SVC_ID_WMI_CONTROL: 670 *ul_pipe = ATH10K_USB_PIPE_TX_CTRL; 671 /* due to large control packets, shift to data pipe */ 672 *dl_pipe = ATH10K_USB_PIPE_RX_DATA; 673 break; 674 case ATH10K_HTC_SVC_ID_HTT_DATA_MSG: 675 *ul_pipe = ATH10K_USB_PIPE_TX_DATA_LP; 676 /* Disable rxdata2 directly, it will be enabled 677 * if FW enable rxdata2 678 */ 679 *dl_pipe = ATH10K_USB_PIPE_RX_DATA; 680 break; 681 default: 682 return -EPERM; 683 } 684 685 return 0; 686 } 687 688 /* This op is currently only used by htc_wait_target if the HTC ready 689 * message times out. It is not applicable for USB since there is nothing 690 * we can do if the HTC ready message does not arrive in time. 691 * TODO: Make this op non mandatory by introducing a NULL check in the 692 * hif op wrapper. 693 */ 694 static void ath10k_usb_hif_send_complete_check(struct ath10k *ar, 695 u8 pipe, int force) 696 { 697 } 698 699 static int ath10k_usb_hif_power_up(struct ath10k *ar, 700 enum ath10k_firmware_mode fw_mode) 701 { 702 return 0; 703 } 704 705 static void ath10k_usb_hif_power_down(struct ath10k *ar) 706 { 707 ath10k_usb_flush_all(ar); 708 } 709 710 #ifdef CONFIG_PM 711 712 static int ath10k_usb_hif_suspend(struct ath10k *ar) 713 { 714 return -EOPNOTSUPP; 715 } 716 717 static int ath10k_usb_hif_resume(struct ath10k *ar) 718 { 719 return -EOPNOTSUPP; 720 } 721 #endif 722 723 static const struct ath10k_hif_ops ath10k_usb_hif_ops = { 724 .tx_sg = ath10k_usb_hif_tx_sg, 725 .diag_read = ath10k_usb_hif_diag_read, 726 .diag_write = ath10k_usb_hif_diag_write, 727 .exchange_bmi_msg = ath10k_usb_bmi_exchange_msg, 728 .start = ath10k_usb_hif_start, 729 .stop = ath10k_usb_hif_stop, 730 .map_service_to_pipe = ath10k_usb_hif_map_service_to_pipe, 731 .get_default_pipe = ath10k_usb_hif_get_default_pipe, 732 .send_complete_check = ath10k_usb_hif_send_complete_check, 733 .get_free_queue_number = ath10k_usb_hif_get_free_queue_number, 734 .power_up = ath10k_usb_hif_power_up, 735 .power_down = ath10k_usb_hif_power_down, 736 #ifdef CONFIG_PM 737 .suspend = ath10k_usb_hif_suspend, 738 .resume = ath10k_usb_hif_resume, 739 #endif 740 }; 741 742 static u8 ath10k_usb_get_logical_pipe_num(u8 ep_address, int *urb_count) 743 { 744 u8 pipe_num = ATH10K_USB_PIPE_INVALID; 745 746 switch (ep_address) { 747 case ATH10K_USB_EP_ADDR_APP_CTRL_IN: 748 pipe_num = ATH10K_USB_PIPE_RX_CTRL; 749 *urb_count = RX_URB_COUNT; 750 break; 751 case ATH10K_USB_EP_ADDR_APP_DATA_IN: 752 pipe_num = ATH10K_USB_PIPE_RX_DATA; 753 *urb_count = RX_URB_COUNT; 754 break; 755 case ATH10K_USB_EP_ADDR_APP_INT_IN: 756 pipe_num = ATH10K_USB_PIPE_RX_INT; 757 *urb_count = RX_URB_COUNT; 758 break; 759 case ATH10K_USB_EP_ADDR_APP_DATA2_IN: 760 pipe_num = ATH10K_USB_PIPE_RX_DATA2; 761 *urb_count = RX_URB_COUNT; 762 break; 763 case ATH10K_USB_EP_ADDR_APP_CTRL_OUT: 764 pipe_num = ATH10K_USB_PIPE_TX_CTRL; 765 *urb_count = TX_URB_COUNT; 766 break; 767 case ATH10K_USB_EP_ADDR_APP_DATA_LP_OUT: 768 pipe_num = ATH10K_USB_PIPE_TX_DATA_LP; 769 *urb_count = TX_URB_COUNT; 770 break; 771 case ATH10K_USB_EP_ADDR_APP_DATA_MP_OUT: 772 pipe_num = ATH10K_USB_PIPE_TX_DATA_MP; 773 *urb_count = TX_URB_COUNT; 774 break; 775 case ATH10K_USB_EP_ADDR_APP_DATA_HP_OUT: 776 pipe_num = ATH10K_USB_PIPE_TX_DATA_HP; 777 *urb_count = TX_URB_COUNT; 778 break; 779 default: 780 /* note: there may be endpoints not currently used */ 781 break; 782 } 783 784 return pipe_num; 785 } 786 787 static int ath10k_usb_alloc_pipe_resources(struct ath10k *ar, 788 struct ath10k_usb_pipe *pipe, 789 int urb_cnt) 790 { 791 struct ath10k_urb_context *urb_context; 792 int i; 793 794 INIT_LIST_HEAD(&pipe->urb_list_head); 795 init_usb_anchor(&pipe->urb_submitted); 796 797 for (i = 0; i < urb_cnt; i++) { 798 urb_context = kzalloc(sizeof(*urb_context), GFP_KERNEL); 799 if (!urb_context) 800 return -ENOMEM; 801 802 urb_context->pipe = pipe; 803 804 /* we are only allocate the urb contexts here, the actual URB 805 * is allocated from the kernel as needed to do a transaction 806 */ 807 pipe->urb_alloc++; 808 ath10k_usb_free_urb_to_pipe(pipe, urb_context); 809 } 810 811 ath10k_dbg(ar, ATH10K_DBG_USB, 812 "usb alloc resources lpipe %d hpipe 0x%x urbs %d\n", 813 pipe->logical_pipe_num, pipe->usb_pipe_handle, 814 pipe->urb_alloc); 815 816 return 0; 817 } 818 819 static int ath10k_usb_setup_pipe_resources(struct ath10k *ar, 820 struct usb_interface *interface) 821 { 822 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 823 struct usb_host_interface *iface_desc = interface->cur_altsetting; 824 struct usb_endpoint_descriptor *endpoint; 825 struct ath10k_usb_pipe *pipe; 826 int ret, i, urbcount; 827 u8 pipe_num; 828 829 ath10k_dbg(ar, ATH10K_DBG_USB, "usb setting up pipes using interface\n"); 830 831 /* walk decriptors and setup pipes */ 832 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { 833 endpoint = &iface_desc->endpoint[i].desc; 834 835 if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) { 836 ath10k_dbg(ar, ATH10K_DBG_USB, 837 "usb %s bulk ep 0x%2.2x maxpktsz %d\n", 838 ATH10K_USB_IS_DIR_IN 839 (endpoint->bEndpointAddress) ? 840 "rx" : "tx", endpoint->bEndpointAddress, 841 le16_to_cpu(endpoint->wMaxPacketSize)); 842 } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) { 843 ath10k_dbg(ar, ATH10K_DBG_USB, 844 "usb %s int ep 0x%2.2x maxpktsz %d interval %d\n", 845 ATH10K_USB_IS_DIR_IN 846 (endpoint->bEndpointAddress) ? 847 "rx" : "tx", endpoint->bEndpointAddress, 848 le16_to_cpu(endpoint->wMaxPacketSize), 849 endpoint->bInterval); 850 } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) { 851 /* TODO for ISO */ 852 ath10k_dbg(ar, ATH10K_DBG_USB, 853 "usb %s isoc ep 0x%2.2x maxpktsz %d interval %d\n", 854 ATH10K_USB_IS_DIR_IN 855 (endpoint->bEndpointAddress) ? 856 "rx" : "tx", endpoint->bEndpointAddress, 857 le16_to_cpu(endpoint->wMaxPacketSize), 858 endpoint->bInterval); 859 } 860 urbcount = 0; 861 862 pipe_num = 863 ath10k_usb_get_logical_pipe_num(endpoint->bEndpointAddress, 864 &urbcount); 865 if (pipe_num == ATH10K_USB_PIPE_INVALID) 866 continue; 867 868 pipe = &ar_usb->pipes[pipe_num]; 869 if (pipe->ar_usb) 870 /* hmmm..pipe was already setup */ 871 continue; 872 873 pipe->ar_usb = ar_usb; 874 pipe->logical_pipe_num = pipe_num; 875 pipe->ep_address = endpoint->bEndpointAddress; 876 pipe->max_packet_size = le16_to_cpu(endpoint->wMaxPacketSize); 877 878 if (ATH10K_USB_IS_BULK_EP(endpoint->bmAttributes)) { 879 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) { 880 pipe->usb_pipe_handle = 881 usb_rcvbulkpipe(ar_usb->udev, 882 pipe->ep_address); 883 } else { 884 pipe->usb_pipe_handle = 885 usb_sndbulkpipe(ar_usb->udev, 886 pipe->ep_address); 887 } 888 } else if (ATH10K_USB_IS_INT_EP(endpoint->bmAttributes)) { 889 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) { 890 pipe->usb_pipe_handle = 891 usb_rcvintpipe(ar_usb->udev, 892 pipe->ep_address); 893 } else { 894 pipe->usb_pipe_handle = 895 usb_sndintpipe(ar_usb->udev, 896 pipe->ep_address); 897 } 898 } else if (ATH10K_USB_IS_ISOC_EP(endpoint->bmAttributes)) { 899 /* TODO for ISO */ 900 if (ATH10K_USB_IS_DIR_IN(pipe->ep_address)) { 901 pipe->usb_pipe_handle = 902 usb_rcvisocpipe(ar_usb->udev, 903 pipe->ep_address); 904 } else { 905 pipe->usb_pipe_handle = 906 usb_sndisocpipe(ar_usb->udev, 907 pipe->ep_address); 908 } 909 } 910 911 pipe->ep_desc = endpoint; 912 913 if (!ATH10K_USB_IS_DIR_IN(pipe->ep_address)) 914 pipe->flags |= ATH10K_USB_PIPE_FLAG_TX; 915 916 ret = ath10k_usb_alloc_pipe_resources(ar, pipe, urbcount); 917 if (ret) 918 return ret; 919 } 920 921 return 0; 922 } 923 924 static int ath10k_usb_create(struct ath10k *ar, 925 struct usb_interface *interface) 926 { 927 struct ath10k_usb *ar_usb = ath10k_usb_priv(ar); 928 struct usb_device *dev = interface_to_usbdev(interface); 929 struct ath10k_usb_pipe *pipe; 930 int ret, i; 931 932 usb_set_intfdata(interface, ar_usb); 933 spin_lock_init(&ar_usb->cs_lock); 934 ar_usb->udev = dev; 935 ar_usb->interface = interface; 936 937 for (i = 0; i < ATH10K_USB_PIPE_MAX; i++) { 938 pipe = &ar_usb->pipes[i]; 939 INIT_WORK(&pipe->io_complete_work, 940 ath10k_usb_io_comp_work); 941 skb_queue_head_init(&pipe->io_comp_queue); 942 } 943 944 ar_usb->diag_cmd_buffer = kzalloc(ATH10K_USB_MAX_DIAG_CMD, GFP_KERNEL); 945 if (!ar_usb->diag_cmd_buffer) { 946 ret = -ENOMEM; 947 goto err; 948 } 949 950 ar_usb->diag_resp_buffer = kzalloc(ATH10K_USB_MAX_DIAG_RESP, 951 GFP_KERNEL); 952 if (!ar_usb->diag_resp_buffer) { 953 ret = -ENOMEM; 954 goto err; 955 } 956 957 ret = ath10k_usb_setup_pipe_resources(ar, interface); 958 if (ret) 959 goto err; 960 961 return 0; 962 963 err: 964 ath10k_usb_destroy(ar); 965 return ret; 966 } 967 968 /* ath10k usb driver registered functions */ 969 static int ath10k_usb_probe(struct usb_interface *interface, 970 const struct usb_device_id *id) 971 { 972 struct ath10k *ar; 973 struct ath10k_usb *ar_usb; 974 struct usb_device *dev = interface_to_usbdev(interface); 975 int ret, vendor_id, product_id; 976 enum ath10k_hw_rev hw_rev; 977 struct ath10k_bus_params bus_params = {}; 978 979 /* Assumption: All USB based chipsets (so far) are QCA9377 based. 980 * If there will be newer chipsets that does not use the hw reg 981 * setup as defined in qca6174_regs and qca6174_values, this 982 * assumption is no longer valid and hw_rev must be setup differently 983 * depending on chipset. 984 */ 985 hw_rev = ATH10K_HW_QCA9377; 986 987 ar = ath10k_core_create(sizeof(*ar_usb), &dev->dev, ATH10K_BUS_USB, 988 hw_rev, &ath10k_usb_hif_ops); 989 if (!ar) { 990 dev_err(&dev->dev, "failed to allocate core\n"); 991 return -ENOMEM; 992 } 993 994 usb_get_dev(dev); 995 vendor_id = le16_to_cpu(dev->descriptor.idVendor); 996 product_id = le16_to_cpu(dev->descriptor.idProduct); 997 998 ath10k_dbg(ar, ATH10K_DBG_BOOT, 999 "usb new func vendor 0x%04x product 0x%04x\n", 1000 vendor_id, product_id); 1001 1002 ar_usb = ath10k_usb_priv(ar); 1003 ret = ath10k_usb_create(ar, interface); 1004 ar_usb->ar = ar; 1005 1006 ar->dev_id = product_id; 1007 ar->id.vendor = vendor_id; 1008 ar->id.device = product_id; 1009 1010 bus_params.dev_type = ATH10K_DEV_TYPE_HL; 1011 /* TODO: don't know yet how to get chip_id with USB */ 1012 bus_params.chip_id = 0; 1013 ret = ath10k_core_register(ar, &bus_params); 1014 if (ret) { 1015 ath10k_warn(ar, "failed to register driver core: %d\n", ret); 1016 goto err; 1017 } 1018 1019 /* TODO: remove this once USB support is fully implemented */ 1020 ath10k_warn(ar, "Warning: ath10k USB support is incomplete, don't expect anything to work!\n"); 1021 1022 return 0; 1023 1024 err: 1025 ath10k_core_destroy(ar); 1026 1027 usb_put_dev(dev); 1028 1029 return ret; 1030 } 1031 1032 static void ath10k_usb_remove(struct usb_interface *interface) 1033 { 1034 struct ath10k_usb *ar_usb; 1035 1036 ar_usb = usb_get_intfdata(interface); 1037 if (!ar_usb) 1038 return; 1039 1040 ath10k_core_unregister(ar_usb->ar); 1041 ath10k_usb_destroy(ar_usb->ar); 1042 usb_put_dev(interface_to_usbdev(interface)); 1043 ath10k_core_destroy(ar_usb->ar); 1044 } 1045 1046 #ifdef CONFIG_PM 1047 1048 static int ath10k_usb_pm_suspend(struct usb_interface *interface, 1049 pm_message_t message) 1050 { 1051 struct ath10k_usb *ar_usb = usb_get_intfdata(interface); 1052 1053 ath10k_usb_flush_all(ar_usb->ar); 1054 return 0; 1055 } 1056 1057 static int ath10k_usb_pm_resume(struct usb_interface *interface) 1058 { 1059 struct ath10k_usb *ar_usb = usb_get_intfdata(interface); 1060 struct ath10k *ar = ar_usb->ar; 1061 1062 ath10k_usb_post_recv_transfers(ar, 1063 &ar_usb->pipes[ATH10K_USB_PIPE_RX_DATA]); 1064 1065 return 0; 1066 } 1067 1068 #else 1069 1070 #define ath10k_usb_pm_suspend NULL 1071 #define ath10k_usb_pm_resume NULL 1072 1073 #endif 1074 1075 /* table of devices that work with this driver */ 1076 static struct usb_device_id ath10k_usb_ids[] = { 1077 {USB_DEVICE(0x13b1, 0x0042)}, /* Linksys WUSB6100M */ 1078 { /* Terminating entry */ }, 1079 }; 1080 1081 MODULE_DEVICE_TABLE(usb, ath10k_usb_ids); 1082 1083 static struct usb_driver ath10k_usb_driver = { 1084 .name = "ath10k_usb", 1085 .probe = ath10k_usb_probe, 1086 .suspend = ath10k_usb_pm_suspend, 1087 .resume = ath10k_usb_pm_resume, 1088 .disconnect = ath10k_usb_remove, 1089 .id_table = ath10k_usb_ids, 1090 .supports_autosuspend = true, 1091 .disable_hub_initiated_lpm = 1, 1092 }; 1093 1094 module_usb_driver(ath10k_usb_driver); 1095 1096 MODULE_AUTHOR("Atheros Communications, Inc."); 1097 MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN USB devices"); 1098 MODULE_LICENSE("Dual BSD/GPL"); 1099