1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for the Diolan DLN-2 USB adapter 4 * 5 * Copyright (c) 2014 Intel Corporation 6 * 7 * Derived from: 8 * i2c-diolan-u2c.c 9 * Copyright (c) 2010-2011 Ericsson AB 10 */ 11 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/types.h> 15 #include <linux/slab.h> 16 #include <linux/usb.h> 17 #include <linux/mutex.h> 18 #include <linux/platform_device.h> 19 #include <linux/mfd/core.h> 20 #include <linux/mfd/dln2.h> 21 #include <linux/rculist.h> 22 23 struct dln2_header { 24 __le16 size; 25 __le16 id; 26 __le16 echo; 27 __le16 handle; 28 }; 29 30 struct dln2_response { 31 struct dln2_header hdr; 32 __le16 result; 33 }; 34 35 #define DLN2_GENERIC_MODULE_ID 0x00 36 #define DLN2_GENERIC_CMD(cmd) DLN2_CMD(cmd, DLN2_GENERIC_MODULE_ID) 37 #define CMD_GET_DEVICE_VER DLN2_GENERIC_CMD(0x30) 38 #define CMD_GET_DEVICE_SN DLN2_GENERIC_CMD(0x31) 39 40 #define DLN2_HW_ID 0x200 41 #define DLN2_USB_TIMEOUT 200 /* in ms */ 42 #define DLN2_MAX_RX_SLOTS 16 43 #define DLN2_MAX_URBS 16 44 #define DLN2_RX_BUF_SIZE 512 45 46 enum dln2_handle { 47 DLN2_HANDLE_EVENT = 0, /* don't change, hardware defined */ 48 DLN2_HANDLE_CTRL, 49 DLN2_HANDLE_GPIO, 50 DLN2_HANDLE_I2C, 51 DLN2_HANDLE_SPI, 52 DLN2_HANDLE_ADC, 53 DLN2_HANDLES 54 }; 55 56 /* 57 * Receive context used between the receive demultiplexer and the transfer 58 * routine. While sending a request the transfer routine will look for a free 59 * receive context and use it to wait for a response and to receive the URB and 60 * thus the response data. 61 */ 62 struct dln2_rx_context { 63 /* completion used to wait for a response */ 64 struct completion done; 65 66 /* if non-NULL the URB contains the response */ 67 struct urb *urb; 68 69 /* if true then this context is used to wait for a response */ 70 bool in_use; 71 }; 72 73 /* 74 * Receive contexts for a particular DLN2 module (i2c, gpio, etc.). We use the 75 * handle header field to identify the module in dln2_dev.mod_rx_slots and then 76 * the echo header field to index the slots field and find the receive context 77 * for a particular request. 78 */ 79 struct dln2_mod_rx_slots { 80 /* RX slots bitmap */ 81 DECLARE_BITMAP(bmap, DLN2_MAX_RX_SLOTS); 82 83 /* used to wait for a free RX slot */ 84 wait_queue_head_t wq; 85 86 /* used to wait for an RX operation to complete */ 87 struct dln2_rx_context slots[DLN2_MAX_RX_SLOTS]; 88 89 /* avoid races between alloc/free_rx_slot and dln2_rx_transfer */ 90 spinlock_t lock; 91 }; 92 93 struct dln2_dev { 94 struct usb_device *usb_dev; 95 struct usb_interface *interface; 96 u8 ep_in; 97 u8 ep_out; 98 99 struct urb *rx_urb[DLN2_MAX_URBS]; 100 void *rx_buf[DLN2_MAX_URBS]; 101 102 struct dln2_mod_rx_slots mod_rx_slots[DLN2_HANDLES]; 103 104 struct list_head event_cb_list; 105 spinlock_t event_cb_lock; 106 107 bool disconnect; 108 int active_transfers; 109 wait_queue_head_t disconnect_wq; 110 spinlock_t disconnect_lock; 111 }; 112 113 struct dln2_event_cb_entry { 114 struct list_head list; 115 u16 id; 116 struct platform_device *pdev; 117 dln2_event_cb_t callback; 118 }; 119 120 int dln2_register_event_cb(struct platform_device *pdev, u16 id, 121 dln2_event_cb_t event_cb) 122 { 123 struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent); 124 struct dln2_event_cb_entry *i, *entry; 125 unsigned long flags; 126 int ret = 0; 127 128 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 129 if (!entry) 130 return -ENOMEM; 131 132 entry->id = id; 133 entry->callback = event_cb; 134 entry->pdev = pdev; 135 136 spin_lock_irqsave(&dln2->event_cb_lock, flags); 137 138 list_for_each_entry(i, &dln2->event_cb_list, list) { 139 if (i->id == id) { 140 ret = -EBUSY; 141 break; 142 } 143 } 144 145 if (!ret) 146 list_add_rcu(&entry->list, &dln2->event_cb_list); 147 148 spin_unlock_irqrestore(&dln2->event_cb_lock, flags); 149 150 if (ret) 151 kfree(entry); 152 153 return ret; 154 } 155 EXPORT_SYMBOL(dln2_register_event_cb); 156 157 void dln2_unregister_event_cb(struct platform_device *pdev, u16 id) 158 { 159 struct dln2_dev *dln2 = dev_get_drvdata(pdev->dev.parent); 160 struct dln2_event_cb_entry *i; 161 unsigned long flags; 162 bool found = false; 163 164 spin_lock_irqsave(&dln2->event_cb_lock, flags); 165 166 list_for_each_entry(i, &dln2->event_cb_list, list) { 167 if (i->id == id) { 168 list_del_rcu(&i->list); 169 found = true; 170 break; 171 } 172 } 173 174 spin_unlock_irqrestore(&dln2->event_cb_lock, flags); 175 176 if (found) { 177 synchronize_rcu(); 178 kfree(i); 179 } 180 } 181 EXPORT_SYMBOL(dln2_unregister_event_cb); 182 183 /* 184 * Returns true if a valid transfer slot is found. In this case the URB must not 185 * be resubmitted immediately in dln2_rx as we need the data when dln2_transfer 186 * is woke up. It will be resubmitted there. 187 */ 188 static bool dln2_transfer_complete(struct dln2_dev *dln2, struct urb *urb, 189 u16 handle, u16 rx_slot) 190 { 191 struct device *dev = &dln2->interface->dev; 192 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle]; 193 struct dln2_rx_context *rxc; 194 unsigned long flags; 195 bool valid_slot = false; 196 197 if (rx_slot >= DLN2_MAX_RX_SLOTS) 198 goto out; 199 200 rxc = &rxs->slots[rx_slot]; 201 202 spin_lock_irqsave(&rxs->lock, flags); 203 if (rxc->in_use && !rxc->urb) { 204 rxc->urb = urb; 205 complete(&rxc->done); 206 valid_slot = true; 207 } 208 spin_unlock_irqrestore(&rxs->lock, flags); 209 210 out: 211 if (!valid_slot) 212 dev_warn(dev, "bad/late response %d/%d\n", handle, rx_slot); 213 214 return valid_slot; 215 } 216 217 static void dln2_run_event_callbacks(struct dln2_dev *dln2, u16 id, u16 echo, 218 void *data, int len) 219 { 220 struct dln2_event_cb_entry *i; 221 222 rcu_read_lock(); 223 224 list_for_each_entry_rcu(i, &dln2->event_cb_list, list) { 225 if (i->id == id) { 226 i->callback(i->pdev, echo, data, len); 227 break; 228 } 229 } 230 231 rcu_read_unlock(); 232 } 233 234 static void dln2_rx(struct urb *urb) 235 { 236 struct dln2_dev *dln2 = urb->context; 237 struct dln2_header *hdr = urb->transfer_buffer; 238 struct device *dev = &dln2->interface->dev; 239 u16 id, echo, handle, size; 240 u8 *data; 241 int len; 242 int err; 243 244 switch (urb->status) { 245 case 0: 246 /* success */ 247 break; 248 case -ECONNRESET: 249 case -ENOENT: 250 case -ESHUTDOWN: 251 case -EPIPE: 252 /* this urb is terminated, clean up */ 253 dev_dbg(dev, "urb shutting down with status %d\n", urb->status); 254 return; 255 default: 256 dev_dbg(dev, "nonzero urb status received %d\n", urb->status); 257 goto out; 258 } 259 260 if (urb->actual_length < sizeof(struct dln2_header)) { 261 dev_err(dev, "short response: %d\n", urb->actual_length); 262 goto out; 263 } 264 265 handle = le16_to_cpu(hdr->handle); 266 id = le16_to_cpu(hdr->id); 267 echo = le16_to_cpu(hdr->echo); 268 size = le16_to_cpu(hdr->size); 269 270 if (size != urb->actual_length) { 271 dev_err(dev, "size mismatch: handle %x cmd %x echo %x size %d actual %d\n", 272 handle, id, echo, size, urb->actual_length); 273 goto out; 274 } 275 276 if (handle >= DLN2_HANDLES) { 277 dev_warn(dev, "invalid handle %d\n", handle); 278 goto out; 279 } 280 281 data = urb->transfer_buffer + sizeof(struct dln2_header); 282 len = urb->actual_length - sizeof(struct dln2_header); 283 284 if (handle == DLN2_HANDLE_EVENT) { 285 unsigned long flags; 286 287 spin_lock_irqsave(&dln2->event_cb_lock, flags); 288 dln2_run_event_callbacks(dln2, id, echo, data, len); 289 spin_unlock_irqrestore(&dln2->event_cb_lock, flags); 290 } else { 291 /* URB will be re-submitted in _dln2_transfer (free_rx_slot) */ 292 if (dln2_transfer_complete(dln2, urb, handle, echo)) 293 return; 294 } 295 296 out: 297 err = usb_submit_urb(urb, GFP_ATOMIC); 298 if (err < 0) 299 dev_err(dev, "failed to resubmit RX URB: %d\n", err); 300 } 301 302 static void *dln2_prep_buf(u16 handle, u16 cmd, u16 echo, const void *obuf, 303 int *obuf_len, gfp_t gfp) 304 { 305 int len; 306 void *buf; 307 struct dln2_header *hdr; 308 309 len = *obuf_len + sizeof(*hdr); 310 buf = kmalloc(len, gfp); 311 if (!buf) 312 return NULL; 313 314 hdr = (struct dln2_header *)buf; 315 hdr->id = cpu_to_le16(cmd); 316 hdr->size = cpu_to_le16(len); 317 hdr->echo = cpu_to_le16(echo); 318 hdr->handle = cpu_to_le16(handle); 319 320 memcpy(buf + sizeof(*hdr), obuf, *obuf_len); 321 322 *obuf_len = len; 323 324 return buf; 325 } 326 327 static int dln2_send_wait(struct dln2_dev *dln2, u16 handle, u16 cmd, u16 echo, 328 const void *obuf, int obuf_len) 329 { 330 int ret = 0; 331 int len = obuf_len; 332 void *buf; 333 int actual; 334 335 buf = dln2_prep_buf(handle, cmd, echo, obuf, &len, GFP_KERNEL); 336 if (!buf) 337 return -ENOMEM; 338 339 ret = usb_bulk_msg(dln2->usb_dev, 340 usb_sndbulkpipe(dln2->usb_dev, dln2->ep_out), 341 buf, len, &actual, DLN2_USB_TIMEOUT); 342 343 kfree(buf); 344 345 return ret; 346 } 347 348 static bool find_free_slot(struct dln2_dev *dln2, u16 handle, int *slot) 349 { 350 struct dln2_mod_rx_slots *rxs; 351 unsigned long flags; 352 353 if (dln2->disconnect) { 354 *slot = -ENODEV; 355 return true; 356 } 357 358 rxs = &dln2->mod_rx_slots[handle]; 359 360 spin_lock_irqsave(&rxs->lock, flags); 361 362 *slot = find_first_zero_bit(rxs->bmap, DLN2_MAX_RX_SLOTS); 363 364 if (*slot < DLN2_MAX_RX_SLOTS) { 365 struct dln2_rx_context *rxc = &rxs->slots[*slot]; 366 367 set_bit(*slot, rxs->bmap); 368 rxc->in_use = true; 369 } 370 371 spin_unlock_irqrestore(&rxs->lock, flags); 372 373 return *slot < DLN2_MAX_RX_SLOTS; 374 } 375 376 static int alloc_rx_slot(struct dln2_dev *dln2, u16 handle) 377 { 378 int ret; 379 int slot; 380 381 /* 382 * No need to timeout here, the wait is bounded by the timeout in 383 * _dln2_transfer. 384 */ 385 ret = wait_event_interruptible(dln2->mod_rx_slots[handle].wq, 386 find_free_slot(dln2, handle, &slot)); 387 if (ret < 0) 388 return ret; 389 390 return slot; 391 } 392 393 static void free_rx_slot(struct dln2_dev *dln2, u16 handle, int slot) 394 { 395 struct dln2_mod_rx_slots *rxs; 396 struct urb *urb = NULL; 397 unsigned long flags; 398 struct dln2_rx_context *rxc; 399 400 rxs = &dln2->mod_rx_slots[handle]; 401 402 spin_lock_irqsave(&rxs->lock, flags); 403 404 clear_bit(slot, rxs->bmap); 405 406 rxc = &rxs->slots[slot]; 407 rxc->in_use = false; 408 urb = rxc->urb; 409 rxc->urb = NULL; 410 reinit_completion(&rxc->done); 411 412 spin_unlock_irqrestore(&rxs->lock, flags); 413 414 if (urb) { 415 int err; 416 struct device *dev = &dln2->interface->dev; 417 418 err = usb_submit_urb(urb, GFP_KERNEL); 419 if (err < 0) 420 dev_err(dev, "failed to resubmit RX URB: %d\n", err); 421 } 422 423 wake_up_interruptible(&rxs->wq); 424 } 425 426 static int _dln2_transfer(struct dln2_dev *dln2, u16 handle, u16 cmd, 427 const void *obuf, unsigned obuf_len, 428 void *ibuf, unsigned *ibuf_len) 429 { 430 int ret = 0; 431 int rx_slot; 432 struct dln2_response *rsp; 433 struct dln2_rx_context *rxc; 434 struct device *dev = &dln2->interface->dev; 435 const unsigned long timeout = msecs_to_jiffies(DLN2_USB_TIMEOUT); 436 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[handle]; 437 int size; 438 439 spin_lock(&dln2->disconnect_lock); 440 if (!dln2->disconnect) 441 dln2->active_transfers++; 442 else 443 ret = -ENODEV; 444 spin_unlock(&dln2->disconnect_lock); 445 446 if (ret) 447 return ret; 448 449 rx_slot = alloc_rx_slot(dln2, handle); 450 if (rx_slot < 0) { 451 ret = rx_slot; 452 goto out_decr; 453 } 454 455 ret = dln2_send_wait(dln2, handle, cmd, rx_slot, obuf, obuf_len); 456 if (ret < 0) { 457 dev_err(dev, "USB write failed: %d\n", ret); 458 goto out_free_rx_slot; 459 } 460 461 rxc = &rxs->slots[rx_slot]; 462 463 ret = wait_for_completion_interruptible_timeout(&rxc->done, timeout); 464 if (ret <= 0) { 465 if (!ret) 466 ret = -ETIMEDOUT; 467 goto out_free_rx_slot; 468 } else { 469 ret = 0; 470 } 471 472 if (dln2->disconnect) { 473 ret = -ENODEV; 474 goto out_free_rx_slot; 475 } 476 477 /* if we got here we know that the response header has been checked */ 478 rsp = rxc->urb->transfer_buffer; 479 size = le16_to_cpu(rsp->hdr.size); 480 481 if (size < sizeof(*rsp)) { 482 ret = -EPROTO; 483 goto out_free_rx_slot; 484 } 485 486 if (le16_to_cpu(rsp->result) > 0x80) { 487 dev_dbg(dev, "%d received response with error %d\n", 488 handle, le16_to_cpu(rsp->result)); 489 ret = -EREMOTEIO; 490 goto out_free_rx_slot; 491 } 492 493 if (!ibuf) 494 goto out_free_rx_slot; 495 496 if (*ibuf_len > size - sizeof(*rsp)) 497 *ibuf_len = size - sizeof(*rsp); 498 499 memcpy(ibuf, rsp + 1, *ibuf_len); 500 501 out_free_rx_slot: 502 free_rx_slot(dln2, handle, rx_slot); 503 out_decr: 504 spin_lock(&dln2->disconnect_lock); 505 dln2->active_transfers--; 506 spin_unlock(&dln2->disconnect_lock); 507 if (dln2->disconnect) 508 wake_up(&dln2->disconnect_wq); 509 510 return ret; 511 } 512 513 int dln2_transfer(struct platform_device *pdev, u16 cmd, 514 const void *obuf, unsigned obuf_len, 515 void *ibuf, unsigned *ibuf_len) 516 { 517 struct dln2_platform_data *dln2_pdata; 518 struct dln2_dev *dln2; 519 u16 handle; 520 521 dln2 = dev_get_drvdata(pdev->dev.parent); 522 dln2_pdata = dev_get_platdata(&pdev->dev); 523 handle = dln2_pdata->handle; 524 525 return _dln2_transfer(dln2, handle, cmd, obuf, obuf_len, ibuf, 526 ibuf_len); 527 } 528 EXPORT_SYMBOL(dln2_transfer); 529 530 static int dln2_check_hw(struct dln2_dev *dln2) 531 { 532 int ret; 533 __le32 hw_type; 534 int len = sizeof(hw_type); 535 536 ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_VER, 537 NULL, 0, &hw_type, &len); 538 if (ret < 0) 539 return ret; 540 if (len < sizeof(hw_type)) 541 return -EREMOTEIO; 542 543 if (le32_to_cpu(hw_type) != DLN2_HW_ID) { 544 dev_err(&dln2->interface->dev, "Device ID 0x%x not supported\n", 545 le32_to_cpu(hw_type)); 546 return -ENODEV; 547 } 548 549 return 0; 550 } 551 552 static int dln2_print_serialno(struct dln2_dev *dln2) 553 { 554 int ret; 555 __le32 serial_no; 556 int len = sizeof(serial_no); 557 struct device *dev = &dln2->interface->dev; 558 559 ret = _dln2_transfer(dln2, DLN2_HANDLE_CTRL, CMD_GET_DEVICE_SN, NULL, 0, 560 &serial_no, &len); 561 if (ret < 0) 562 return ret; 563 if (len < sizeof(serial_no)) 564 return -EREMOTEIO; 565 566 dev_info(dev, "Diolan DLN2 serial %u\n", le32_to_cpu(serial_no)); 567 568 return 0; 569 } 570 571 static int dln2_hw_init(struct dln2_dev *dln2) 572 { 573 int ret; 574 575 ret = dln2_check_hw(dln2); 576 if (ret < 0) 577 return ret; 578 579 return dln2_print_serialno(dln2); 580 } 581 582 static void dln2_free_rx_urbs(struct dln2_dev *dln2) 583 { 584 int i; 585 586 for (i = 0; i < DLN2_MAX_URBS; i++) { 587 usb_free_urb(dln2->rx_urb[i]); 588 kfree(dln2->rx_buf[i]); 589 } 590 } 591 592 static void dln2_stop_rx_urbs(struct dln2_dev *dln2) 593 { 594 int i; 595 596 for (i = 0; i < DLN2_MAX_URBS; i++) 597 usb_kill_urb(dln2->rx_urb[i]); 598 } 599 600 static void dln2_free(struct dln2_dev *dln2) 601 { 602 dln2_free_rx_urbs(dln2); 603 usb_put_dev(dln2->usb_dev); 604 kfree(dln2); 605 } 606 607 static int dln2_setup_rx_urbs(struct dln2_dev *dln2, 608 struct usb_host_interface *hostif) 609 { 610 int i; 611 const int rx_max_size = DLN2_RX_BUF_SIZE; 612 613 for (i = 0; i < DLN2_MAX_URBS; i++) { 614 dln2->rx_buf[i] = kmalloc(rx_max_size, GFP_KERNEL); 615 if (!dln2->rx_buf[i]) 616 return -ENOMEM; 617 618 dln2->rx_urb[i] = usb_alloc_urb(0, GFP_KERNEL); 619 if (!dln2->rx_urb[i]) 620 return -ENOMEM; 621 622 usb_fill_bulk_urb(dln2->rx_urb[i], dln2->usb_dev, 623 usb_rcvbulkpipe(dln2->usb_dev, dln2->ep_in), 624 dln2->rx_buf[i], rx_max_size, dln2_rx, dln2); 625 } 626 627 return 0; 628 } 629 630 static int dln2_start_rx_urbs(struct dln2_dev *dln2, gfp_t gfp) 631 { 632 struct device *dev = &dln2->interface->dev; 633 int ret; 634 int i; 635 636 for (i = 0; i < DLN2_MAX_URBS; i++) { 637 ret = usb_submit_urb(dln2->rx_urb[i], gfp); 638 if (ret < 0) { 639 dev_err(dev, "failed to submit RX URB: %d\n", ret); 640 return ret; 641 } 642 } 643 644 return 0; 645 } 646 647 enum { 648 DLN2_ACPI_MATCH_GPIO = 0, 649 DLN2_ACPI_MATCH_I2C = 1, 650 DLN2_ACPI_MATCH_SPI = 2, 651 DLN2_ACPI_MATCH_ADC = 3, 652 }; 653 654 static struct dln2_platform_data dln2_pdata_gpio = { 655 .handle = DLN2_HANDLE_GPIO, 656 }; 657 658 static struct mfd_cell_acpi_match dln2_acpi_match_gpio = { 659 .adr = DLN2_ACPI_MATCH_GPIO, 660 }; 661 662 /* Only one I2C port seems to be supported on current hardware */ 663 static struct dln2_platform_data dln2_pdata_i2c = { 664 .handle = DLN2_HANDLE_I2C, 665 .port = 0, 666 }; 667 668 static struct mfd_cell_acpi_match dln2_acpi_match_i2c = { 669 .adr = DLN2_ACPI_MATCH_I2C, 670 }; 671 672 /* Only one SPI port supported */ 673 static struct dln2_platform_data dln2_pdata_spi = { 674 .handle = DLN2_HANDLE_SPI, 675 .port = 0, 676 }; 677 678 static struct mfd_cell_acpi_match dln2_acpi_match_spi = { 679 .adr = DLN2_ACPI_MATCH_SPI, 680 }; 681 682 /* Only one ADC port supported */ 683 static struct dln2_platform_data dln2_pdata_adc = { 684 .handle = DLN2_HANDLE_ADC, 685 .port = 0, 686 }; 687 688 static struct mfd_cell_acpi_match dln2_acpi_match_adc = { 689 .adr = DLN2_ACPI_MATCH_ADC, 690 }; 691 692 static const struct mfd_cell dln2_devs[] = { 693 { 694 .name = "dln2-gpio", 695 .acpi_match = &dln2_acpi_match_gpio, 696 .platform_data = &dln2_pdata_gpio, 697 .pdata_size = sizeof(struct dln2_platform_data), 698 }, 699 { 700 .name = "dln2-i2c", 701 .acpi_match = &dln2_acpi_match_i2c, 702 .platform_data = &dln2_pdata_i2c, 703 .pdata_size = sizeof(struct dln2_platform_data), 704 }, 705 { 706 .name = "dln2-spi", 707 .acpi_match = &dln2_acpi_match_spi, 708 .platform_data = &dln2_pdata_spi, 709 .pdata_size = sizeof(struct dln2_platform_data), 710 }, 711 { 712 .name = "dln2-adc", 713 .acpi_match = &dln2_acpi_match_adc, 714 .platform_data = &dln2_pdata_adc, 715 .pdata_size = sizeof(struct dln2_platform_data), 716 }, 717 }; 718 719 static void dln2_stop(struct dln2_dev *dln2) 720 { 721 int i, j; 722 723 /* don't allow starting new transfers */ 724 spin_lock(&dln2->disconnect_lock); 725 dln2->disconnect = true; 726 spin_unlock(&dln2->disconnect_lock); 727 728 /* cancel in progress transfers */ 729 for (i = 0; i < DLN2_HANDLES; i++) { 730 struct dln2_mod_rx_slots *rxs = &dln2->mod_rx_slots[i]; 731 unsigned long flags; 732 733 spin_lock_irqsave(&rxs->lock, flags); 734 735 /* cancel all response waiters */ 736 for (j = 0; j < DLN2_MAX_RX_SLOTS; j++) { 737 struct dln2_rx_context *rxc = &rxs->slots[j]; 738 739 if (rxc->in_use) 740 complete(&rxc->done); 741 } 742 743 spin_unlock_irqrestore(&rxs->lock, flags); 744 } 745 746 /* wait for transfers to end */ 747 wait_event(dln2->disconnect_wq, !dln2->active_transfers); 748 749 dln2_stop_rx_urbs(dln2); 750 } 751 752 static void dln2_disconnect(struct usb_interface *interface) 753 { 754 struct dln2_dev *dln2 = usb_get_intfdata(interface); 755 756 dln2_stop(dln2); 757 758 mfd_remove_devices(&interface->dev); 759 760 dln2_free(dln2); 761 } 762 763 static int dln2_probe(struct usb_interface *interface, 764 const struct usb_device_id *usb_id) 765 { 766 struct usb_host_interface *hostif = interface->cur_altsetting; 767 struct usb_endpoint_descriptor *epin; 768 struct usb_endpoint_descriptor *epout; 769 struct device *dev = &interface->dev; 770 struct dln2_dev *dln2; 771 int ret; 772 int i, j; 773 774 if (hostif->desc.bInterfaceNumber != 0) 775 return -ENODEV; 776 777 ret = usb_find_common_endpoints(hostif, &epin, &epout, NULL, NULL); 778 if (ret) 779 return ret; 780 781 dln2 = kzalloc(sizeof(*dln2), GFP_KERNEL); 782 if (!dln2) 783 return -ENOMEM; 784 785 dln2->ep_out = epout->bEndpointAddress; 786 dln2->ep_in = epin->bEndpointAddress; 787 dln2->usb_dev = usb_get_dev(interface_to_usbdev(interface)); 788 dln2->interface = interface; 789 usb_set_intfdata(interface, dln2); 790 init_waitqueue_head(&dln2->disconnect_wq); 791 792 for (i = 0; i < DLN2_HANDLES; i++) { 793 init_waitqueue_head(&dln2->mod_rx_slots[i].wq); 794 spin_lock_init(&dln2->mod_rx_slots[i].lock); 795 for (j = 0; j < DLN2_MAX_RX_SLOTS; j++) 796 init_completion(&dln2->mod_rx_slots[i].slots[j].done); 797 } 798 799 spin_lock_init(&dln2->event_cb_lock); 800 spin_lock_init(&dln2->disconnect_lock); 801 INIT_LIST_HEAD(&dln2->event_cb_list); 802 803 ret = dln2_setup_rx_urbs(dln2, hostif); 804 if (ret) 805 goto out_free; 806 807 ret = dln2_start_rx_urbs(dln2, GFP_KERNEL); 808 if (ret) 809 goto out_stop_rx; 810 811 ret = dln2_hw_init(dln2); 812 if (ret < 0) { 813 dev_err(dev, "failed to initialize hardware\n"); 814 goto out_stop_rx; 815 } 816 817 ret = mfd_add_hotplug_devices(dev, dln2_devs, ARRAY_SIZE(dln2_devs)); 818 if (ret != 0) { 819 dev_err(dev, "failed to add mfd devices to core\n"); 820 goto out_stop_rx; 821 } 822 823 return 0; 824 825 out_stop_rx: 826 dln2_stop_rx_urbs(dln2); 827 828 out_free: 829 dln2_free(dln2); 830 831 return ret; 832 } 833 834 static int dln2_suspend(struct usb_interface *iface, pm_message_t message) 835 { 836 struct dln2_dev *dln2 = usb_get_intfdata(iface); 837 838 dln2_stop(dln2); 839 840 return 0; 841 } 842 843 static int dln2_resume(struct usb_interface *iface) 844 { 845 struct dln2_dev *dln2 = usb_get_intfdata(iface); 846 847 dln2->disconnect = false; 848 849 return dln2_start_rx_urbs(dln2, GFP_NOIO); 850 } 851 852 static const struct usb_device_id dln2_table[] = { 853 { USB_DEVICE(0xa257, 0x2013) }, 854 { } 855 }; 856 857 MODULE_DEVICE_TABLE(usb, dln2_table); 858 859 static struct usb_driver dln2_driver = { 860 .name = "dln2", 861 .probe = dln2_probe, 862 .disconnect = dln2_disconnect, 863 .id_table = dln2_table, 864 .suspend = dln2_suspend, 865 .resume = dln2_resume, 866 }; 867 868 module_usb_driver(dln2_driver); 869 870 MODULE_AUTHOR("Octavian Purdila <octavian.purdila@intel.com>"); 871 MODULE_DESCRIPTION("Core driver for the Diolan DLN2 interface adapter"); 872 MODULE_LICENSE("GPL v2"); 873