1 /* 2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved. 3 * 4 * This software is licensed under the terms of the GNU General Public 5 * License version 2, as published by the Free Software Foundation, and 6 * may be copied, distributed, and modified under those terms. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 */ 13 14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 15 16 #include <linux/module.h> 17 #include <linux/kernel.h> 18 #include <linux/usb.h> 19 #include <linux/sched.h> 20 #include <linux/kthread.h> 21 #include <linux/usb/cdc.h> 22 #include <linux/wait.h> 23 #include <linux/if_ether.h> 24 #include <linux/pm_runtime.h> 25 26 #include "gdm_usb.h" 27 #include "gdm_lte.h" 28 #include "hci.h" 29 #include "hci_packet.h" 30 #include "gdm_endian.h" 31 32 #define USB_DEVICE_CDC_DATA(vid, pid) \ 33 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 34 USB_DEVICE_ID_MATCH_INT_CLASS | \ 35 USB_DEVICE_ID_MATCH_INT_SUBCLASS,\ 36 .idVendor = vid,\ 37 .idProduct = pid,\ 38 .bInterfaceClass = USB_CLASS_COMM,\ 39 .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET 40 41 #define USB_DEVICE_MASS_DATA(vid, pid) \ 42 .match_flags = USB_DEVICE_ID_MATCH_DEVICE | \ 43 USB_DEVICE_ID_MATCH_INT_INFO,\ 44 .idVendor = vid,\ 45 .idProduct = pid,\ 46 .bInterfaceSubClass = USB_SC_SCSI, \ 47 .bInterfaceClass = USB_CLASS_MASS_STORAGE,\ 48 .bInterfaceProtocol = USB_PR_BULK 49 50 static const struct usb_device_id id_table[] = { 51 { USB_DEVICE_CDC_DATA(VID_GCT, PID_GDM7240) }, /* GCT GDM7240 */ 52 { USB_DEVICE_CDC_DATA(VID_GCT, PID_GDM7243) }, /* GCT GDM7243 */ 53 { } 54 }; 55 56 MODULE_DEVICE_TABLE(usb, id_table); 57 58 static struct workqueue_struct *usb_tx_wq; 59 static struct workqueue_struct *usb_rx_wq; 60 61 static void do_tx(struct work_struct *work); 62 static void do_rx(struct work_struct *work); 63 64 static int gdm_usb_recv(void *priv_dev, 65 int (*cb)(void *cb_data, 66 void *data, int len, int context), 67 void *cb_data, 68 int context); 69 70 static int request_mac_address(struct lte_udev *udev) 71 { 72 u8 buf[16] = {0,}; 73 struct hci_packet *hci = (struct hci_packet *)buf; 74 struct usb_device *usbdev = udev->usbdev; 75 int actual; 76 int ret = -1; 77 78 hci->cmd_evt = gdm_cpu_to_dev16(&udev->gdm_ed, LTE_GET_INFORMATION); 79 hci->len = gdm_cpu_to_dev16(&udev->gdm_ed, 1); 80 hci->data[0] = MAC_ADDRESS; 81 82 ret = usb_bulk_msg(usbdev, usb_sndbulkpipe(usbdev, 2), buf, 5, 83 &actual, 1000); 84 85 udev->request_mac_addr = 1; 86 87 return ret; 88 } 89 90 static struct usb_tx *alloc_tx_struct(int len) 91 { 92 struct usb_tx *t = NULL; 93 int ret = 0; 94 95 t = kzalloc(sizeof(struct usb_tx), GFP_ATOMIC); 96 if (!t) { 97 ret = -ENOMEM; 98 goto out; 99 } 100 101 t->urb = usb_alloc_urb(0, GFP_ATOMIC); 102 if (!(len % 512)) 103 len++; 104 105 t->buf = kmalloc(len, GFP_ATOMIC); 106 if (!t->urb || !t->buf) { 107 ret = -ENOMEM; 108 goto out; 109 } 110 111 out: 112 if (ret < 0) { 113 if (t) { 114 usb_free_urb(t->urb); 115 kfree(t->buf); 116 kfree(t); 117 } 118 return NULL; 119 } 120 121 return t; 122 } 123 124 static struct usb_tx_sdu *alloc_tx_sdu_struct(void) 125 { 126 struct usb_tx_sdu *t_sdu; 127 128 t_sdu = kzalloc(sizeof(struct usb_tx_sdu), GFP_ATOMIC); 129 if (!t_sdu) 130 return NULL; 131 132 t_sdu->buf = kmalloc(SDU_BUF_SIZE, GFP_ATOMIC); 133 if (!t_sdu->buf) { 134 kfree(t_sdu); 135 return NULL; 136 } 137 138 return t_sdu; 139 } 140 141 static void free_tx_struct(struct usb_tx *t) 142 { 143 if (t) { 144 usb_free_urb(t->urb); 145 kfree(t->buf); 146 kfree(t); 147 } 148 } 149 150 static void free_tx_sdu_struct(struct usb_tx_sdu *t_sdu) 151 { 152 if (t_sdu) { 153 kfree(t_sdu->buf); 154 kfree(t_sdu); 155 } 156 } 157 158 static struct usb_tx_sdu *get_tx_sdu_struct(struct tx_cxt *tx, int *no_spc) 159 { 160 struct usb_tx_sdu *t_sdu; 161 162 if (list_empty(&tx->free_list)) 163 return NULL; 164 165 t_sdu = list_entry(tx->free_list.next, struct usb_tx_sdu, list); 166 list_del(&t_sdu->list); 167 168 tx->avail_count--; 169 170 *no_spc = list_empty(&tx->free_list) ? 1 : 0; 171 172 return t_sdu; 173 } 174 175 static void put_tx_struct(struct tx_cxt *tx, struct usb_tx_sdu *t_sdu) 176 { 177 list_add_tail(&t_sdu->list, &tx->free_list); 178 tx->avail_count++; 179 } 180 181 static struct usb_rx *alloc_rx_struct(void) 182 { 183 struct usb_rx *r = NULL; 184 int ret = 0; 185 186 r = kmalloc(sizeof(struct usb_rx), GFP_ATOMIC); 187 if (!r) { 188 ret = -ENOMEM; 189 goto out; 190 } 191 192 r->urb = usb_alloc_urb(0, GFP_ATOMIC); 193 r->buf = kmalloc(RX_BUF_SIZE, GFP_ATOMIC); 194 if (!r->urb || !r->buf) { 195 ret = -ENOMEM; 196 goto out; 197 } 198 out: 199 200 if (ret < 0) { 201 if (r) { 202 usb_free_urb(r->urb); 203 kfree(r->buf); 204 kfree(r); 205 } 206 return NULL; 207 } 208 209 return r; 210 } 211 212 static void free_rx_struct(struct usb_rx *r) 213 { 214 if (r) { 215 usb_free_urb(r->urb); 216 kfree(r->buf); 217 kfree(r); 218 } 219 } 220 221 static struct usb_rx *get_rx_struct(struct rx_cxt *rx, int *no_spc) 222 { 223 struct usb_rx *r; 224 unsigned long flags; 225 226 spin_lock_irqsave(&rx->rx_lock, flags); 227 228 if (list_empty(&rx->free_list)) { 229 spin_unlock_irqrestore(&rx->rx_lock, flags); 230 return NULL; 231 } 232 233 r = list_entry(rx->free_list.next, struct usb_rx, free_list); 234 list_del(&r->free_list); 235 236 rx->avail_count--; 237 238 *no_spc = list_empty(&rx->free_list) ? 1 : 0; 239 240 spin_unlock_irqrestore(&rx->rx_lock, flags); 241 242 return r; 243 } 244 245 static void put_rx_struct(struct rx_cxt *rx, struct usb_rx *r) 246 { 247 unsigned long flags; 248 249 spin_lock_irqsave(&rx->rx_lock, flags); 250 251 list_add_tail(&r->free_list, &rx->free_list); 252 rx->avail_count++; 253 254 spin_unlock_irqrestore(&rx->rx_lock, flags); 255 } 256 257 static void release_usb(struct lte_udev *udev) 258 { 259 struct rx_cxt *rx = &udev->rx; 260 struct tx_cxt *tx = &udev->tx; 261 struct usb_tx *t, *t_next; 262 struct usb_rx *r, *r_next; 263 struct usb_tx_sdu *t_sdu, *t_sdu_next; 264 unsigned long flags; 265 266 spin_lock_irqsave(&tx->lock, flags); 267 list_for_each_entry_safe(t_sdu, t_sdu_next, &tx->sdu_list, list) 268 { 269 list_del(&t_sdu->list); 270 free_tx_sdu_struct(t_sdu); 271 } 272 273 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) 274 { 275 list_del(&t->list); 276 free_tx_struct(t); 277 } 278 279 list_for_each_entry_safe(t_sdu, t_sdu_next, &tx->free_list, list) 280 { 281 list_del(&t_sdu->list); 282 free_tx_sdu_struct(t_sdu); 283 } 284 spin_unlock_irqrestore(&tx->lock, flags); 285 286 spin_lock_irqsave(&rx->submit_lock, flags); 287 list_for_each_entry_safe(r, r_next, &rx->rx_submit_list, rx_submit_list) 288 { 289 spin_unlock_irqrestore(&rx->submit_lock, flags); 290 usb_kill_urb(r->urb); 291 spin_lock_irqsave(&rx->submit_lock, flags); 292 } 293 spin_unlock_irqrestore(&rx->submit_lock, flags); 294 295 spin_lock_irqsave(&rx->rx_lock, flags); 296 list_for_each_entry_safe(r, r_next, &rx->free_list, free_list) 297 { 298 list_del(&r->free_list); 299 free_rx_struct(r); 300 } 301 spin_unlock_irqrestore(&rx->rx_lock, flags); 302 303 spin_lock_irqsave(&rx->to_host_lock, flags); 304 list_for_each_entry_safe(r, r_next, &rx->to_host_list, to_host_list) 305 { 306 if (r->index == (void *)udev) { 307 list_del(&r->to_host_list); 308 free_rx_struct(r); 309 } 310 } 311 spin_unlock_irqrestore(&rx->to_host_lock, flags); 312 } 313 314 static int init_usb(struct lte_udev *udev) 315 { 316 int ret = 0; 317 int i; 318 struct tx_cxt *tx = &udev->tx; 319 struct rx_cxt *rx = &udev->rx; 320 struct usb_tx_sdu *t_sdu = NULL; 321 struct usb_rx *r = NULL; 322 323 udev->send_complete = 1; 324 udev->tx_stop = 0; 325 udev->request_mac_addr = 0; 326 udev->usb_state = PM_NORMAL; 327 328 INIT_LIST_HEAD(&tx->sdu_list); 329 INIT_LIST_HEAD(&tx->hci_list); 330 INIT_LIST_HEAD(&tx->free_list); 331 INIT_LIST_HEAD(&rx->rx_submit_list); 332 INIT_LIST_HEAD(&rx->free_list); 333 INIT_LIST_HEAD(&rx->to_host_list); 334 spin_lock_init(&tx->lock); 335 spin_lock_init(&rx->rx_lock); 336 spin_lock_init(&rx->submit_lock); 337 spin_lock_init(&rx->to_host_lock); 338 339 tx->avail_count = 0; 340 rx->avail_count = 0; 341 342 udev->rx_cb = NULL; 343 344 for (i = 0; i < MAX_NUM_SDU_BUF; i++) { 345 t_sdu = alloc_tx_sdu_struct(); 346 if (t_sdu == NULL) { 347 ret = -ENOMEM; 348 goto fail; 349 } 350 351 list_add(&t_sdu->list, &tx->free_list); 352 tx->avail_count++; 353 } 354 355 for (i = 0; i < MAX_RX_SUBMIT_COUNT*2; i++) { 356 r = alloc_rx_struct(); 357 if (r == NULL) { 358 ret = -ENOMEM; 359 goto fail; 360 } 361 362 list_add(&r->free_list, &rx->free_list); 363 rx->avail_count++; 364 } 365 INIT_DELAYED_WORK(&udev->work_tx, do_tx); 366 INIT_DELAYED_WORK(&udev->work_rx, do_rx); 367 return 0; 368 fail: 369 return ret; 370 } 371 372 static int set_mac_address(u8 *data, void *arg) 373 { 374 struct phy_dev *phy_dev = (struct phy_dev *)arg; 375 struct lte_udev *udev = phy_dev->priv_dev; 376 struct tlv *tlv = (struct tlv *)data; 377 u8 mac_address[ETH_ALEN] = {0, }; 378 379 if (tlv->type == MAC_ADDRESS && udev->request_mac_addr) { 380 memcpy(mac_address, tlv->data, tlv->len); 381 382 if (register_lte_device(phy_dev, 383 &udev->intf->dev, mac_address) < 0) 384 pr_err("register lte device failed\n"); 385 386 udev->request_mac_addr = 0; 387 388 return 1; 389 } 390 391 return 0; 392 } 393 394 static void do_rx(struct work_struct *work) 395 { 396 struct lte_udev *udev = 397 container_of(work, struct lte_udev, work_rx.work); 398 struct rx_cxt *rx = &udev->rx; 399 struct usb_rx *r; 400 struct hci_packet *hci; 401 struct phy_dev *phy_dev; 402 u16 cmd_evt; 403 int ret; 404 unsigned long flags; 405 406 while (1) { 407 spin_lock_irqsave(&rx->to_host_lock, flags); 408 if (list_empty(&rx->to_host_list)) { 409 spin_unlock_irqrestore(&rx->to_host_lock, flags); 410 break; 411 } 412 r = list_entry(rx->to_host_list.next, 413 struct usb_rx, to_host_list); 414 list_del(&r->to_host_list); 415 spin_unlock_irqrestore(&rx->to_host_lock, flags); 416 417 phy_dev = (struct phy_dev *)r->cb_data; 418 udev = (struct lte_udev *)phy_dev->priv_dev; 419 hci = (struct hci_packet *)r->buf; 420 cmd_evt = gdm_dev16_to_cpu(&udev->gdm_ed, hci->cmd_evt); 421 422 switch (cmd_evt) { 423 case LTE_GET_INFORMATION_RESULT: 424 if (set_mac_address(hci->data, r->cb_data) == 0) { 425 ret = r->callback(r->cb_data, 426 r->buf, 427 r->urb->actual_length, 428 KERNEL_THREAD); 429 } 430 break; 431 432 default: 433 if (r->callback) { 434 ret = r->callback(r->cb_data, 435 r->buf, 436 r->urb->actual_length, 437 KERNEL_THREAD); 438 439 if (ret == -EAGAIN) 440 pr_err("failed to send received data\n"); 441 } 442 break; 443 } 444 445 put_rx_struct(rx, r); 446 447 gdm_usb_recv(udev, 448 r->callback, 449 r->cb_data, 450 USB_COMPLETE); 451 } 452 } 453 454 static void remove_rx_submit_list(struct usb_rx *r, struct rx_cxt *rx) 455 { 456 unsigned long flags; 457 struct usb_rx *r_remove, *r_remove_next; 458 459 spin_lock_irqsave(&rx->submit_lock, flags); 460 list_for_each_entry_safe(r_remove, 461 r_remove_next, &rx->rx_submit_list, rx_submit_list) 462 { 463 if (r == r_remove) { 464 list_del(&r->rx_submit_list); 465 break; 466 } 467 } 468 spin_unlock_irqrestore(&rx->submit_lock, flags); 469 } 470 471 static void gdm_usb_rcv_complete(struct urb *urb) 472 { 473 struct usb_rx *r = urb->context; 474 struct rx_cxt *rx = r->rx; 475 unsigned long flags; 476 struct lte_udev *udev = container_of(r->rx, struct lte_udev, rx); 477 struct usb_device *usbdev = udev->usbdev; 478 479 remove_rx_submit_list(r, rx); 480 481 if (!urb->status && r->callback) { 482 spin_lock_irqsave(&rx->to_host_lock, flags); 483 list_add_tail(&r->to_host_list, &rx->to_host_list); 484 queue_work(usb_rx_wq, &udev->work_rx.work); 485 spin_unlock_irqrestore(&rx->to_host_lock, flags); 486 } else { 487 if (urb->status && udev->usb_state == PM_NORMAL) 488 pr_err("%s: urb status error %d\n", 489 __func__, urb->status); 490 491 put_rx_struct(rx, r); 492 } 493 494 usb_mark_last_busy(usbdev); 495 } 496 497 static int gdm_usb_recv(void *priv_dev, 498 int (*cb)(void *cb_data, 499 void *data, int len, int context), 500 void *cb_data, 501 int context) 502 { 503 struct lte_udev *udev = priv_dev; 504 struct usb_device *usbdev = udev->usbdev; 505 struct rx_cxt *rx = &udev->rx; 506 struct usb_rx *r; 507 int no_spc; 508 int ret; 509 unsigned long flags; 510 511 if (!udev->usbdev) { 512 pr_err("invalid device\n"); 513 return -ENODEV; 514 } 515 516 r = get_rx_struct(rx, &no_spc); 517 if (!r) { 518 pr_err("Out of Memory\n"); 519 return -ENOMEM; 520 } 521 522 udev->rx_cb = cb; 523 r->callback = cb; 524 r->cb_data = cb_data; 525 r->index = (void *)udev; 526 r->rx = rx; 527 528 usb_fill_bulk_urb(r->urb, 529 usbdev, 530 usb_rcvbulkpipe(usbdev, 0x83), 531 r->buf, 532 RX_BUF_SIZE, 533 gdm_usb_rcv_complete, 534 r); 535 536 spin_lock_irqsave(&rx->submit_lock, flags); 537 list_add_tail(&r->rx_submit_list, &rx->rx_submit_list); 538 spin_unlock_irqrestore(&rx->submit_lock, flags); 539 540 if (context == KERNEL_THREAD) 541 ret = usb_submit_urb(r->urb, GFP_KERNEL); 542 else 543 ret = usb_submit_urb(r->urb, GFP_ATOMIC); 544 545 if (ret) { 546 spin_lock_irqsave(&rx->submit_lock, flags); 547 list_del(&r->rx_submit_list); 548 spin_unlock_irqrestore(&rx->submit_lock, flags); 549 550 pr_err("usb_submit_urb failed (%p)\n", r); 551 put_rx_struct(rx, r); 552 } 553 554 return ret; 555 } 556 557 static void gdm_usb_send_complete(struct urb *urb) 558 { 559 struct usb_tx *t = urb->context; 560 struct tx_cxt *tx = t->tx; 561 struct lte_udev *udev = container_of(tx, struct lte_udev, tx); 562 unsigned long flags; 563 564 if (urb->status == -ECONNRESET) { 565 pr_info("CONNRESET\n"); 566 return; 567 } 568 569 if (t->callback) 570 t->callback(t->cb_data); 571 572 free_tx_struct(t); 573 574 spin_lock_irqsave(&tx->lock, flags); 575 udev->send_complete = 1; 576 queue_work(usb_tx_wq, &udev->work_tx.work); 577 spin_unlock_irqrestore(&tx->lock, flags); 578 } 579 580 static int send_tx_packet(struct usb_device *usbdev, struct usb_tx *t, u32 len) 581 { 582 int ret = 0; 583 584 if (!(len%512)) 585 len++; 586 587 usb_fill_bulk_urb(t->urb, 588 usbdev, 589 usb_sndbulkpipe(usbdev, 2), 590 t->buf, 591 len, 592 gdm_usb_send_complete, 593 t); 594 595 ret = usb_submit_urb(t->urb, GFP_ATOMIC); 596 597 if (ret) 598 pr_err("usb_submit_urb failed: %d\n", ret); 599 600 usb_mark_last_busy(usbdev); 601 602 return ret; 603 } 604 605 static u32 packet_aggregation(struct lte_udev *udev, u8 *send_buf) 606 { 607 struct tx_cxt *tx = &udev->tx; 608 struct usb_tx_sdu *t_sdu = NULL; 609 struct multi_sdu *multi_sdu = (struct multi_sdu *)send_buf; 610 u16 send_len = 0; 611 u16 num_packet = 0; 612 unsigned long flags; 613 614 multi_sdu->cmd_evt = gdm_cpu_to_dev16(&udev->gdm_ed, LTE_TX_MULTI_SDU); 615 616 while (num_packet < MAX_PACKET_IN_MULTI_SDU) { 617 spin_lock_irqsave(&tx->lock, flags); 618 if (list_empty(&tx->sdu_list)) { 619 spin_unlock_irqrestore(&tx->lock, flags); 620 break; 621 } 622 623 t_sdu = list_entry(tx->sdu_list.next, struct usb_tx_sdu, list); 624 if (send_len + t_sdu->len > MAX_SDU_SIZE) { 625 spin_unlock_irqrestore(&tx->lock, flags); 626 break; 627 } 628 629 list_del(&t_sdu->list); 630 spin_unlock_irqrestore(&tx->lock, flags); 631 632 memcpy(multi_sdu->data + send_len, t_sdu->buf, t_sdu->len); 633 634 send_len += (t_sdu->len + 3) & 0xfffc; 635 num_packet++; 636 637 if (tx->avail_count > 10) 638 t_sdu->callback(t_sdu->cb_data); 639 640 spin_lock_irqsave(&tx->lock, flags); 641 put_tx_struct(tx, t_sdu); 642 spin_unlock_irqrestore(&tx->lock, flags); 643 } 644 645 multi_sdu->len = gdm_cpu_to_dev16(&udev->gdm_ed, send_len); 646 multi_sdu->num_packet = gdm_cpu_to_dev16(&udev->gdm_ed, num_packet); 647 648 return send_len + offsetof(struct multi_sdu, data); 649 } 650 651 static void do_tx(struct work_struct *work) 652 { 653 struct lte_udev *udev = 654 container_of(work, struct lte_udev, work_tx.work); 655 struct usb_device *usbdev = udev->usbdev; 656 struct tx_cxt *tx = &udev->tx; 657 struct usb_tx *t = NULL; 658 int is_send = 0; 659 u32 len = 0; 660 unsigned long flags; 661 662 if (!usb_autopm_get_interface(udev->intf)) 663 usb_autopm_put_interface(udev->intf); 664 665 if (udev->usb_state == PM_SUSPEND) 666 return; 667 668 spin_lock_irqsave(&tx->lock, flags); 669 if (!udev->send_complete) { 670 spin_unlock_irqrestore(&tx->lock, flags); 671 return; 672 } else { 673 udev->send_complete = 0; 674 } 675 676 if (!list_empty(&tx->hci_list)) { 677 t = list_entry(tx->hci_list.next, struct usb_tx, list); 678 list_del(&t->list); 679 len = t->len; 680 t->is_sdu = 0; 681 is_send = 1; 682 } else if (!list_empty(&tx->sdu_list)) { 683 if (udev->tx_stop) { 684 udev->send_complete = 1; 685 spin_unlock_irqrestore(&tx->lock, flags); 686 return; 687 } 688 689 t = alloc_tx_struct(TX_BUF_SIZE); 690 t->callback = NULL; 691 t->tx = tx; 692 t->is_sdu = 1; 693 is_send = 1; 694 } 695 696 if (!is_send) { 697 udev->send_complete = 1; 698 spin_unlock_irqrestore(&tx->lock, flags); 699 return; 700 } 701 spin_unlock_irqrestore(&tx->lock, flags); 702 703 if (t->is_sdu) 704 len = packet_aggregation(udev, t->buf); 705 706 if (send_tx_packet(usbdev, t, len)) { 707 pr_err("send_tx_packet failed\n"); 708 t->callback = NULL; 709 gdm_usb_send_complete(t->urb); 710 } 711 } 712 713 #define SDU_PARAM_LEN 12 714 static int gdm_usb_sdu_send(void *priv_dev, void *data, int len, 715 unsigned int dftEpsId, unsigned int epsId, 716 void (*cb)(void *data), void *cb_data, 717 int dev_idx, int nic_type) 718 { 719 struct lte_udev *udev = priv_dev; 720 struct tx_cxt *tx = &udev->tx; 721 struct usb_tx_sdu *t_sdu; 722 struct sdu *sdu = NULL; 723 unsigned long flags; 724 int no_spc = 0; 725 u16 send_len; 726 727 if (!udev->usbdev) { 728 pr_err("sdu send - invalid device\n"); 729 return TX_NO_DEV; 730 } 731 732 spin_lock_irqsave(&tx->lock, flags); 733 t_sdu = get_tx_sdu_struct(tx, &no_spc); 734 spin_unlock_irqrestore(&tx->lock, flags); 735 736 if (t_sdu == NULL) { 737 pr_err("sdu send - free list empty\n"); 738 return TX_NO_SPC; 739 } 740 741 sdu = (struct sdu *)t_sdu->buf; 742 sdu->cmd_evt = gdm_cpu_to_dev16(&udev->gdm_ed, LTE_TX_SDU); 743 if (nic_type == NIC_TYPE_ARP) { 744 send_len = len + SDU_PARAM_LEN; 745 memcpy(sdu->data, data, len); 746 } else { 747 send_len = len - ETH_HLEN; 748 send_len += SDU_PARAM_LEN; 749 memcpy(sdu->data, data+ETH_HLEN, len-ETH_HLEN); 750 } 751 752 sdu->len = gdm_cpu_to_dev16(&udev->gdm_ed, send_len); 753 sdu->dftEpsId = gdm_cpu_to_dev32(&udev->gdm_ed, dftEpsId); 754 sdu->bearer_ID = gdm_cpu_to_dev32(&udev->gdm_ed, epsId); 755 sdu->nic_type = gdm_cpu_to_dev32(&udev->gdm_ed, nic_type); 756 757 t_sdu->len = send_len + HCI_HEADER_SIZE; 758 t_sdu->callback = cb; 759 t_sdu->cb_data = cb_data; 760 761 spin_lock_irqsave(&tx->lock, flags); 762 list_add_tail(&t_sdu->list, &tx->sdu_list); 763 queue_work(usb_tx_wq, &udev->work_tx.work); 764 spin_unlock_irqrestore(&tx->lock, flags); 765 766 if (no_spc) 767 return TX_NO_BUFFER; 768 769 return 0; 770 } 771 772 static int gdm_usb_hci_send(void *priv_dev, void *data, int len, 773 void (*cb)(void *data), void *cb_data) 774 { 775 struct lte_udev *udev = priv_dev; 776 struct tx_cxt *tx = &udev->tx; 777 struct usb_tx *t; 778 unsigned long flags; 779 780 if (!udev->usbdev) { 781 pr_err("hci send - invalid device\n"); 782 return -ENODEV; 783 } 784 785 t = alloc_tx_struct(len); 786 if (t == NULL) { 787 pr_err("hci_send - out of memory\n"); 788 return -ENOMEM; 789 } 790 791 memcpy(t->buf, data, len); 792 t->callback = cb; 793 t->cb_data = cb_data; 794 t->len = len; 795 t->tx = tx; 796 t->is_sdu = 0; 797 798 spin_lock_irqsave(&tx->lock, flags); 799 list_add_tail(&t->list, &tx->hci_list); 800 queue_work(usb_tx_wq, &udev->work_tx.work); 801 spin_unlock_irqrestore(&tx->lock, flags); 802 803 return 0; 804 } 805 806 static struct gdm_endian *gdm_usb_get_endian(void *priv_dev) 807 { 808 struct lte_udev *udev = priv_dev; 809 810 return &udev->gdm_ed; 811 } 812 813 static int gdm_usb_probe(struct usb_interface *intf, 814 const struct usb_device_id *id) 815 { 816 int ret = 0; 817 struct phy_dev *phy_dev = NULL; 818 struct lte_udev *udev = NULL; 819 u16 idVendor, idProduct; 820 int bInterfaceNumber; 821 struct usb_device *usbdev = interface_to_usbdev(intf); 822 823 bInterfaceNumber = intf->cur_altsetting->desc.bInterfaceNumber; 824 idVendor = __le16_to_cpu(usbdev->descriptor.idVendor); 825 idProduct = __le16_to_cpu(usbdev->descriptor.idProduct); 826 827 pr_info("net vid = 0x%04x pid = 0x%04x\n", idVendor, idProduct); 828 829 if (bInterfaceNumber > NETWORK_INTERFACE) { 830 pr_info("not a network device\n"); 831 return -ENODEV; 832 } 833 834 phy_dev = kzalloc(sizeof(struct phy_dev), GFP_KERNEL); 835 if (!phy_dev) 836 return -ENOMEM; 837 838 udev = kzalloc(sizeof(struct lte_udev), GFP_KERNEL); 839 if (!udev) { 840 ret = -ENOMEM; 841 goto err_udev; 842 } 843 844 phy_dev->priv_dev = (void *)udev; 845 phy_dev->send_hci_func = gdm_usb_hci_send; 846 phy_dev->send_sdu_func = gdm_usb_sdu_send; 847 phy_dev->rcv_func = gdm_usb_recv; 848 phy_dev->get_endian = gdm_usb_get_endian; 849 850 udev->usbdev = usbdev; 851 ret = init_usb(udev); 852 if (ret < 0) { 853 pr_err("init_usb func failed\n"); 854 goto err_init_usb; 855 } 856 udev->intf = intf; 857 858 intf->needs_remote_wakeup = 1; 859 usb_enable_autosuspend(usbdev); 860 pm_runtime_set_autosuspend_delay(&usbdev->dev, AUTO_SUSPEND_TIMER); 861 862 /* List up hosts with big endians, otherwise, 863 * defaults to little endian 864 */ 865 if (idProduct == PID_GDM7243) 866 gdm_set_endian(&udev->gdm_ed, ENDIANNESS_BIG); 867 else 868 gdm_set_endian(&udev->gdm_ed, ENDIANNESS_LITTLE); 869 870 ret = request_mac_address(udev); 871 if (ret < 0) { 872 pr_err("request Mac address failed\n"); 873 goto err_mac_address; 874 } 875 876 start_rx_proc(phy_dev); 877 usb_get_dev(usbdev); 878 usb_set_intfdata(intf, phy_dev); 879 880 return 0; 881 882 err_mac_address: 883 release_usb(udev); 884 err_init_usb: 885 kfree(udev); 886 err_udev: 887 kfree(phy_dev); 888 889 return ret; 890 } 891 892 static void gdm_usb_disconnect(struct usb_interface *intf) 893 { 894 struct phy_dev *phy_dev; 895 struct lte_udev *udev; 896 u16 idVendor, idProduct; 897 struct usb_device *usbdev; 898 usbdev = interface_to_usbdev(intf); 899 900 idVendor = __le16_to_cpu(usbdev->descriptor.idVendor); 901 idProduct = __le16_to_cpu(usbdev->descriptor.idProduct); 902 903 phy_dev = usb_get_intfdata(intf); 904 905 udev = phy_dev->priv_dev; 906 unregister_lte_device(phy_dev); 907 908 release_usb(udev); 909 910 kfree(udev); 911 udev = NULL; 912 913 kfree(phy_dev); 914 phy_dev = NULL; 915 916 usb_put_dev(usbdev); 917 } 918 919 static int gdm_usb_suspend(struct usb_interface *intf, pm_message_t pm_msg) 920 { 921 struct phy_dev *phy_dev; 922 struct lte_udev *udev; 923 struct rx_cxt *rx; 924 struct usb_rx *r; 925 struct usb_rx *r_next; 926 unsigned long flags; 927 928 phy_dev = usb_get_intfdata(intf); 929 udev = phy_dev->priv_dev; 930 rx = &udev->rx; 931 if (udev->usb_state != PM_NORMAL) { 932 pr_err("usb suspend - invalid state\n"); 933 return -1; 934 } 935 936 udev->usb_state = PM_SUSPEND; 937 938 spin_lock_irqsave(&rx->submit_lock, flags); 939 list_for_each_entry_safe(r, r_next, &rx->rx_submit_list, rx_submit_list) 940 { 941 spin_unlock_irqrestore(&rx->submit_lock, flags); 942 usb_kill_urb(r->urb); 943 spin_lock_irqsave(&rx->submit_lock, flags); 944 } 945 spin_unlock_irqrestore(&rx->submit_lock, flags); 946 947 return 0; 948 } 949 950 static int gdm_usb_resume(struct usb_interface *intf) 951 { 952 struct phy_dev *phy_dev; 953 struct lte_udev *udev; 954 struct tx_cxt *tx; 955 struct rx_cxt *rx; 956 unsigned long flags; 957 int issue_count; 958 int i; 959 960 phy_dev = usb_get_intfdata(intf); 961 udev = phy_dev->priv_dev; 962 rx = &udev->rx; 963 964 if (udev->usb_state != PM_SUSPEND) { 965 pr_err("usb resume - invalid state\n"); 966 return -1; 967 } 968 udev->usb_state = PM_NORMAL; 969 970 spin_lock_irqsave(&rx->rx_lock, flags); 971 issue_count = rx->avail_count - MAX_RX_SUBMIT_COUNT; 972 spin_unlock_irqrestore(&rx->rx_lock, flags); 973 974 if (issue_count >= 0) { 975 for (i = 0; i < issue_count; i++) 976 gdm_usb_recv(phy_dev->priv_dev, 977 udev->rx_cb, 978 phy_dev, 979 USB_COMPLETE); 980 } 981 982 tx = &udev->tx; 983 spin_lock_irqsave(&tx->lock, flags); 984 queue_work(usb_tx_wq, &udev->work_tx.work); 985 spin_unlock_irqrestore(&tx->lock, flags); 986 987 return 0; 988 } 989 990 static struct usb_driver gdm_usb_lte_driver = { 991 .name = "gdm_lte", 992 .probe = gdm_usb_probe, 993 .disconnect = gdm_usb_disconnect, 994 .id_table = id_table, 995 .supports_autosuspend = 1, 996 .suspend = gdm_usb_suspend, 997 .resume = gdm_usb_resume, 998 .reset_resume = gdm_usb_resume, 999 }; 1000 1001 static int __init gdm_usb_lte_init(void) 1002 { 1003 if (gdm_lte_event_init() < 0) { 1004 pr_err("error creating event\n"); 1005 return -1; 1006 } 1007 1008 usb_tx_wq = create_workqueue("usb_tx_wq"); 1009 if (usb_tx_wq == NULL) 1010 return -1; 1011 1012 usb_rx_wq = create_workqueue("usb_rx_wq"); 1013 if (usb_rx_wq == NULL) 1014 return -1; 1015 1016 return usb_register(&gdm_usb_lte_driver); 1017 } 1018 1019 static void __exit gdm_usb_lte_exit(void) 1020 { 1021 gdm_lte_event_exit(); 1022 1023 usb_deregister(&gdm_usb_lte_driver); 1024 1025 if (usb_tx_wq) { 1026 flush_workqueue(usb_tx_wq); 1027 destroy_workqueue(usb_tx_wq); 1028 } 1029 1030 if (usb_rx_wq) { 1031 flush_workqueue(usb_rx_wq); 1032 destroy_workqueue(usb_rx_wq); 1033 } 1034 } 1035 1036 module_init(gdm_usb_lte_init); 1037 module_exit(gdm_usb_lte_exit); 1038 1039 MODULE_VERSION(DRIVER_VERSION); 1040 MODULE_DESCRIPTION("GCT LTE USB Device Driver"); 1041 MODULE_LICENSE("GPL"); 1042