1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Intel La Jolla Cove Adapter USB driver 4 * 5 * Copyright (c) 2023, Intel Corporation. 6 */ 7 8 #include <linux/acpi.h> 9 #include <linux/auxiliary_bus.h> 10 #include <linux/dev_printk.h> 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/slab.h> 15 #include <linux/spinlock.h> 16 #include <linux/types.h> 17 #include <linux/usb.h> 18 #include <linux/usb/ljca.h> 19 20 #include <linux/unaligned.h> 21 22 /* command flags */ 23 #define LJCA_ACK_FLAG BIT(0) 24 #define LJCA_RESP_FLAG BIT(1) 25 #define LJCA_CMPL_FLAG BIT(2) 26 27 #define LJCA_MAX_PACKET_SIZE 64u 28 #define LJCA_MAX_PAYLOAD_SIZE \ 29 (LJCA_MAX_PACKET_SIZE - sizeof(struct ljca_msg)) 30 31 #define LJCA_WRITE_TIMEOUT_MS 200 32 #define LJCA_WRITE_ACK_TIMEOUT_MS 500 33 #define LJCA_ENUM_CLIENT_TIMEOUT_MS 20 34 35 /* ljca client type */ 36 enum ljca_client_type { 37 LJCA_CLIENT_MNG = 1, 38 LJCA_CLIENT_GPIO = 3, 39 LJCA_CLIENT_I2C = 4, 40 LJCA_CLIENT_SPI = 5, 41 }; 42 43 /* MNG client commands */ 44 enum ljca_mng_cmd { 45 LJCA_MNG_RESET = 2, 46 LJCA_MNG_ENUM_GPIO = 4, 47 LJCA_MNG_ENUM_I2C = 5, 48 LJCA_MNG_ENUM_SPI = 8, 49 }; 50 51 /* ljca client acpi _ADR */ 52 enum ljca_client_acpi_adr { 53 LJCA_GPIO_ACPI_ADR, 54 LJCA_I2C1_ACPI_ADR, 55 LJCA_I2C2_ACPI_ADR, 56 LJCA_SPI1_ACPI_ADR, 57 LJCA_SPI2_ACPI_ADR, 58 LJCA_CLIENT_ACPI_ADR_MAX, 59 }; 60 61 /* ljca cmd message structure */ 62 struct ljca_msg { 63 u8 type; 64 u8 cmd; 65 u8 flags; 66 u8 len; 67 u8 data[] __counted_by(len); 68 } __packed; 69 70 struct ljca_i2c_ctr_info { 71 u8 id; 72 u8 capacity; 73 u8 intr_pin; 74 } __packed; 75 76 struct ljca_i2c_descriptor { 77 u8 num; 78 struct ljca_i2c_ctr_info info[] __counted_by(num); 79 } __packed; 80 81 struct ljca_spi_ctr_info { 82 u8 id; 83 u8 capacity; 84 u8 intr_pin; 85 } __packed; 86 87 struct ljca_spi_descriptor { 88 u8 num; 89 struct ljca_spi_ctr_info info[] __counted_by(num); 90 } __packed; 91 92 struct ljca_bank_descriptor { 93 u8 bank_id; 94 u8 pin_num; 95 96 /* 1 bit for each gpio, 1 means valid */ 97 __le32 valid_pins; 98 } __packed; 99 100 struct ljca_gpio_descriptor { 101 u8 pins_per_bank; 102 u8 bank_num; 103 struct ljca_bank_descriptor bank_desc[] __counted_by(bank_num); 104 } __packed; 105 106 /** 107 * struct ljca_adapter - represent a ljca adapter 108 * 109 * @intf: the usb interface for this ljca adapter 110 * @usb_dev: the usb device for this ljca adapter 111 * @dev: the specific device info of the usb interface 112 * @rx_pipe: bulk in pipe for receive data from firmware 113 * @tx_pipe: bulk out pipe for send data to firmware 114 * @rx_urb: urb used for the bulk in pipe 115 * @rx_buf: buffer used to receive command response and event 116 * @rx_len: length of rx buffer 117 * @ex_buf: external buffer to save command response 118 * @ex_buf_len: length of external buffer 119 * @actual_length: actual length of data copied to external buffer 120 * @tx_buf: buffer used to download command to firmware 121 * @tx_buf_len: length of tx buffer 122 * @lock: spinlock to protect tx_buf and ex_buf 123 * @cmd_completion: completion object as the command receives ack 124 * @mutex: mutex to avoid command download concurrently 125 * @client_list: client device list 126 * @disconnect: usb disconnect ongoing or not 127 * @reset_id: used to reset firmware 128 */ 129 struct ljca_adapter { 130 struct usb_interface *intf; 131 struct usb_device *usb_dev; 132 struct device *dev; 133 134 unsigned int rx_pipe; 135 unsigned int tx_pipe; 136 137 struct urb *rx_urb; 138 void *rx_buf; 139 unsigned int rx_len; 140 141 u8 *ex_buf; 142 u8 ex_buf_len; 143 u8 actual_length; 144 145 void *tx_buf; 146 u8 tx_buf_len; 147 148 spinlock_t lock; 149 150 struct completion cmd_completion; 151 struct mutex mutex; 152 153 struct list_head client_list; 154 155 bool disconnect; 156 157 u32 reset_id; 158 }; 159 160 struct ljca_match_ids_walk_data { 161 const struct acpi_device_id *ids; 162 const char *uid; 163 struct acpi_device *adev; 164 }; 165 166 /* 167 * ACPI hardware IDs for LJCA client devices. 168 * 169 * [1] Some BIOS implementations use these IDs for denoting LJCA client devices 170 * even though the IDs have been allocated for USBIO. This isn't a problem 171 * as the usb-ljca driver is probed based on the USB device's vendor and 172 * product IDs and its client drivers are probed based on auxiliary device 173 * names, not these ACPI _HIDs. List of such systems: 174 * 175 * Dell Precision 5490 176 */ 177 static const struct acpi_device_id ljca_gpio_hids[] = { 178 { "INTC100B" }, /* RPL LJCA GPIO */ 179 { "INTC1074" }, /* CVF LJCA GPIO */ 180 { "INTC1096" }, /* ADL LJCA GPIO */ 181 { "INTC10B5" }, /* LNL LJCA GPIO */ 182 { "INTC10D1" }, /* MTL (CVF VSC) USBIO GPIO [1] */ 183 {}, 184 }; 185 186 static const struct acpi_device_id ljca_i2c_hids[] = { 187 { "INTC100C" }, /* RPL LJCA I2C */ 188 { "INTC1075" }, /* CVF LJCA I2C */ 189 { "INTC1097" }, /* ADL LJCA I2C */ 190 { "INTC10D2" }, /* MTL (CVF VSC) USBIO I2C [1] */ 191 {}, 192 }; 193 194 static const struct acpi_device_id ljca_spi_hids[] = { 195 { "INTC100D" }, /* RPL LJCA SPI */ 196 { "INTC1091" }, /* TGL/ADL LJCA SPI */ 197 { "INTC1098" }, /* ADL LJCA SPI */ 198 { "INTC10D3" }, /* MTL (CVF VSC) USBIO SPI [1] */ 199 {}, 200 }; 201 202 static void ljca_handle_event(struct ljca_adapter *adap, 203 struct ljca_msg *header) 204 { 205 struct ljca_client *client; 206 207 list_for_each_entry(client, &adap->client_list, link) { 208 /* 209 * Currently only GPIO register event callback, but 210 * firmware message structure should include id when 211 * multiple same type clients register event callback. 212 */ 213 if (client->type == header->type) { 214 unsigned long flags; 215 216 spin_lock_irqsave(&client->event_cb_lock, flags); 217 client->event_cb(client->context, header->cmd, 218 header->data, header->len); 219 spin_unlock_irqrestore(&client->event_cb_lock, flags); 220 221 break; 222 } 223 } 224 } 225 226 /* process command ack and received data if available */ 227 static void ljca_handle_cmd_ack(struct ljca_adapter *adap, struct ljca_msg *header) 228 { 229 struct ljca_msg *tx_header = adap->tx_buf; 230 u8 ibuf_len, actual_len = 0; 231 unsigned long flags; 232 u8 *ibuf; 233 234 spin_lock_irqsave(&adap->lock, flags); 235 236 if (tx_header->type != header->type || tx_header->cmd != header->cmd) { 237 spin_unlock_irqrestore(&adap->lock, flags); 238 dev_err(adap->dev, "cmd ack mismatch error\n"); 239 return; 240 } 241 242 ibuf_len = adap->ex_buf_len; 243 ibuf = adap->ex_buf; 244 245 if (ibuf && ibuf_len) { 246 actual_len = min(header->len, ibuf_len); 247 248 /* copy received data to external buffer */ 249 memcpy(ibuf, header->data, actual_len); 250 } 251 /* update copied data length */ 252 adap->actual_length = actual_len; 253 254 spin_unlock_irqrestore(&adap->lock, flags); 255 256 complete(&adap->cmd_completion); 257 } 258 259 static void ljca_recv(struct urb *urb) 260 { 261 struct ljca_msg *header = urb->transfer_buffer; 262 struct ljca_adapter *adap = urb->context; 263 int ret; 264 265 switch (urb->status) { 266 case 0: 267 /* success */ 268 break; 269 case -ENOENT: 270 /* 271 * directly complete the possible ongoing transfer 272 * during disconnect 273 */ 274 if (adap->disconnect) 275 complete(&adap->cmd_completion); 276 return; 277 case -ECONNRESET: 278 case -ESHUTDOWN: 279 case -EPIPE: 280 /* rx urb is terminated */ 281 dev_dbg(adap->dev, "rx urb terminated with status: %d\n", 282 urb->status); 283 return; 284 default: 285 dev_dbg(adap->dev, "rx urb error: %d\n", urb->status); 286 goto resubmit; 287 } 288 289 if (header->len + sizeof(*header) != urb->actual_length) 290 goto resubmit; 291 292 if (header->flags & LJCA_ACK_FLAG) 293 ljca_handle_cmd_ack(adap, header); 294 else 295 ljca_handle_event(adap, header); 296 297 resubmit: 298 ret = usb_submit_urb(urb, GFP_ATOMIC); 299 if (ret && ret != -EPERM) 300 dev_err(adap->dev, "resubmit rx urb error %d\n", ret); 301 } 302 303 static int ljca_send(struct ljca_adapter *adap, u8 type, u8 cmd, 304 const u8 *obuf, u8 obuf_len, u8 *ibuf, u8 ibuf_len, 305 bool ack, unsigned long timeout) 306 { 307 unsigned int msg_len = sizeof(struct ljca_msg) + obuf_len; 308 struct ljca_msg *header = adap->tx_buf; 309 unsigned int transferred; 310 unsigned long flags; 311 int ret; 312 313 if (adap->disconnect) 314 return -ENODEV; 315 316 if (msg_len > adap->tx_buf_len) 317 return -EINVAL; 318 319 mutex_lock(&adap->mutex); 320 321 spin_lock_irqsave(&adap->lock, flags); 322 323 header->type = type; 324 header->cmd = cmd; 325 header->len = obuf_len; 326 if (obuf) 327 memcpy(header->data, obuf, obuf_len); 328 329 header->flags = LJCA_CMPL_FLAG | (ack ? LJCA_ACK_FLAG : 0); 330 331 adap->ex_buf = ibuf; 332 adap->ex_buf_len = ibuf_len; 333 adap->actual_length = 0; 334 335 spin_unlock_irqrestore(&adap->lock, flags); 336 337 reinit_completion(&adap->cmd_completion); 338 339 ret = usb_autopm_get_interface(adap->intf); 340 if (ret < 0) 341 goto out; 342 343 ret = usb_bulk_msg(adap->usb_dev, adap->tx_pipe, header, 344 msg_len, &transferred, LJCA_WRITE_TIMEOUT_MS); 345 if (ret < 0) 346 goto out_put; 347 if (transferred != msg_len) { 348 ret = -EIO; 349 goto out_put; 350 } 351 352 if (ack) { 353 ret = wait_for_completion_timeout(&adap->cmd_completion, 354 timeout); 355 if (!ret) { 356 ret = -ETIMEDOUT; 357 goto out_put; 358 } 359 } 360 ret = adap->actual_length; 361 362 out_put: 363 usb_autopm_put_interface(adap->intf); 364 365 out: 366 spin_lock_irqsave(&adap->lock, flags); 367 adap->ex_buf = NULL; 368 adap->ex_buf_len = 0; 369 370 memset(header, 0, sizeof(*header)); 371 spin_unlock_irqrestore(&adap->lock, flags); 372 373 mutex_unlock(&adap->mutex); 374 375 return ret; 376 } 377 378 int ljca_transfer(struct ljca_client *client, u8 cmd, const u8 *obuf, 379 u8 obuf_len, u8 *ibuf, u8 ibuf_len) 380 { 381 return ljca_send(client->adapter, client->type, cmd, 382 obuf, obuf_len, ibuf, ibuf_len, true, 383 LJCA_WRITE_ACK_TIMEOUT_MS); 384 } 385 EXPORT_SYMBOL_NS_GPL(ljca_transfer, "LJCA"); 386 387 int ljca_transfer_noack(struct ljca_client *client, u8 cmd, const u8 *obuf, 388 u8 obuf_len) 389 { 390 return ljca_send(client->adapter, client->type, cmd, obuf, 391 obuf_len, NULL, 0, false, LJCA_WRITE_ACK_TIMEOUT_MS); 392 } 393 EXPORT_SYMBOL_NS_GPL(ljca_transfer_noack, "LJCA"); 394 395 int ljca_register_event_cb(struct ljca_client *client, ljca_event_cb_t event_cb, 396 void *context) 397 { 398 unsigned long flags; 399 400 if (!event_cb) 401 return -EINVAL; 402 403 spin_lock_irqsave(&client->event_cb_lock, flags); 404 405 if (client->event_cb) { 406 spin_unlock_irqrestore(&client->event_cb_lock, flags); 407 return -EALREADY; 408 } 409 410 client->event_cb = event_cb; 411 client->context = context; 412 413 spin_unlock_irqrestore(&client->event_cb_lock, flags); 414 415 return 0; 416 } 417 EXPORT_SYMBOL_NS_GPL(ljca_register_event_cb, "LJCA"); 418 419 void ljca_unregister_event_cb(struct ljca_client *client) 420 { 421 unsigned long flags; 422 423 spin_lock_irqsave(&client->event_cb_lock, flags); 424 425 client->event_cb = NULL; 426 client->context = NULL; 427 428 spin_unlock_irqrestore(&client->event_cb_lock, flags); 429 } 430 EXPORT_SYMBOL_NS_GPL(ljca_unregister_event_cb, "LJCA"); 431 432 static int ljca_match_device_ids(struct acpi_device *adev, void *data) 433 { 434 struct ljca_match_ids_walk_data *wd = data; 435 const char *uid = acpi_device_uid(adev); 436 437 if (acpi_match_device_ids(adev, wd->ids)) 438 return 0; 439 440 if (!wd->uid) 441 goto match; 442 443 if (!uid) 444 /* 445 * Some DSDTs have only one ACPI companion for the two I2C 446 * controllers and they don't set a UID at all (e.g. Dell 447 * Latitude 9420). On these platforms only the first I2C 448 * controller is used, so if a HID match has no UID we use 449 * "0" as the UID and assign ACPI companion to the first 450 * I2C controller. 451 */ 452 uid = "0"; 453 else 454 uid = strchr(uid, wd->uid[0]); 455 456 if (!uid || strcmp(uid, wd->uid)) 457 return 0; 458 459 match: 460 wd->adev = adev; 461 462 return 1; 463 } 464 465 /* bind auxiliary device to acpi device */ 466 static void ljca_auxdev_acpi_bind(struct ljca_adapter *adap, 467 struct auxiliary_device *auxdev, 468 u64 adr, u8 id) 469 { 470 struct ljca_match_ids_walk_data wd = { 0 }; 471 struct device *dev = adap->dev; 472 struct acpi_device *parent; 473 char uid[4]; 474 475 parent = ACPI_COMPANION(dev); 476 if (!parent) 477 return; 478 479 /* 480 * Currently LJCA hw doesn't use _ADR instead the shipped 481 * platforms use _HID to distinguish children devices. 482 */ 483 switch (adr) { 484 case LJCA_GPIO_ACPI_ADR: 485 wd.ids = ljca_gpio_hids; 486 break; 487 case LJCA_I2C1_ACPI_ADR: 488 case LJCA_I2C2_ACPI_ADR: 489 snprintf(uid, sizeof(uid), "%d", id); 490 wd.uid = uid; 491 wd.ids = ljca_i2c_hids; 492 break; 493 case LJCA_SPI1_ACPI_ADR: 494 case LJCA_SPI2_ACPI_ADR: 495 wd.ids = ljca_spi_hids; 496 break; 497 default: 498 dev_warn(dev, "unsupported _ADR\n"); 499 return; 500 } 501 502 acpi_dev_for_each_child(parent, ljca_match_device_ids, &wd); 503 if (wd.adev) { 504 ACPI_COMPANION_SET(&auxdev->dev, wd.adev); 505 return; 506 } 507 508 parent = ACPI_COMPANION(dev->parent->parent); 509 if (!parent) 510 return; 511 512 acpi_dev_for_each_child(parent, ljca_match_device_ids, &wd); 513 if (wd.adev) 514 ACPI_COMPANION_SET(&auxdev->dev, wd.adev); 515 } 516 517 static void ljca_auxdev_release(struct device *dev) 518 { 519 struct auxiliary_device *auxdev = to_auxiliary_dev(dev); 520 521 kfree(auxdev->dev.platform_data); 522 } 523 524 static int ljca_new_client_device(struct ljca_adapter *adap, u8 type, u8 id, 525 char *name, void *data, u64 adr) 526 { 527 struct auxiliary_device *auxdev; 528 struct ljca_client *client; 529 int ret; 530 531 client = kzalloc_obj(*client); 532 if (!client) { 533 kfree(data); 534 return -ENOMEM; 535 } 536 537 client->type = type; 538 client->id = id; 539 client->adapter = adap; 540 spin_lock_init(&client->event_cb_lock); 541 542 auxdev = &client->auxdev; 543 auxdev->name = name; 544 auxdev->id = id; 545 546 auxdev->dev.parent = adap->dev; 547 auxdev->dev.platform_data = data; 548 auxdev->dev.release = ljca_auxdev_release; 549 550 ret = auxiliary_device_init(auxdev); 551 if (ret) { 552 kfree(data); 553 goto err_free; 554 } 555 556 ljca_auxdev_acpi_bind(adap, auxdev, adr, id); 557 558 ret = auxiliary_device_add(auxdev); 559 if (ret) 560 goto err_uninit; 561 562 list_add_tail(&client->link, &adap->client_list); 563 564 return 0; 565 566 err_uninit: 567 auxiliary_device_uninit(auxdev); 568 569 err_free: 570 kfree(client); 571 572 return ret; 573 } 574 575 static int ljca_enumerate_gpio(struct ljca_adapter *adap) 576 { 577 u32 valid_pin[LJCA_MAX_GPIO_NUM / BITS_PER_TYPE(u32)]; 578 struct ljca_gpio_descriptor *desc; 579 struct ljca_gpio_info *gpio_info; 580 u8 buf[LJCA_MAX_PAYLOAD_SIZE]; 581 int ret, gpio_num; 582 unsigned int i; 583 584 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_GPIO, NULL, 0, buf, 585 sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS); 586 if (ret < 0) 587 return ret; 588 589 /* check firmware response */ 590 desc = (struct ljca_gpio_descriptor *)buf; 591 if (ret != struct_size(desc, bank_desc, desc->bank_num)) 592 return -EINVAL; 593 594 gpio_num = desc->pins_per_bank * desc->bank_num; 595 if (gpio_num > LJCA_MAX_GPIO_NUM) 596 return -EINVAL; 597 598 /* construct platform data */ 599 gpio_info = kzalloc_obj(*gpio_info); 600 if (!gpio_info) 601 return -ENOMEM; 602 gpio_info->num = gpio_num; 603 604 for (i = 0; i < desc->bank_num; i++) 605 valid_pin[i] = get_unaligned_le32(&desc->bank_desc[i].valid_pins); 606 bitmap_from_arr32(gpio_info->valid_pin_map, valid_pin, gpio_num); 607 608 return ljca_new_client_device(adap, LJCA_CLIENT_GPIO, 0, "ljca-gpio", 609 gpio_info, LJCA_GPIO_ACPI_ADR); 610 } 611 612 static int ljca_enumerate_i2c(struct ljca_adapter *adap) 613 { 614 struct ljca_i2c_descriptor *desc; 615 struct ljca_i2c_info *i2c_info; 616 u8 buf[LJCA_MAX_PAYLOAD_SIZE]; 617 unsigned int i; 618 int ret; 619 620 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_I2C, NULL, 0, buf, 621 sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS); 622 if (ret < 0) 623 return ret; 624 625 /* check firmware response */ 626 desc = (struct ljca_i2c_descriptor *)buf; 627 if (ret != struct_size(desc, info, desc->num)) 628 return -EINVAL; 629 630 for (i = 0; i < desc->num; i++) { 631 /* construct platform data */ 632 i2c_info = kzalloc_obj(*i2c_info); 633 if (!i2c_info) 634 return -ENOMEM; 635 636 i2c_info->id = desc->info[i].id; 637 i2c_info->capacity = desc->info[i].capacity; 638 i2c_info->intr_pin = desc->info[i].intr_pin; 639 640 ret = ljca_new_client_device(adap, LJCA_CLIENT_I2C, i, 641 "ljca-i2c", i2c_info, 642 LJCA_I2C1_ACPI_ADR + i); 643 if (ret) 644 return ret; 645 } 646 647 return 0; 648 } 649 650 static int ljca_enumerate_spi(struct ljca_adapter *adap) 651 { 652 struct ljca_spi_descriptor *desc; 653 struct ljca_spi_info *spi_info; 654 u8 buf[LJCA_MAX_PAYLOAD_SIZE]; 655 unsigned int i; 656 int ret; 657 658 /* Not all LJCA chips implement SPI, a timeout reading the descriptors is normal */ 659 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_ENUM_SPI, NULL, 0, buf, 660 sizeof(buf), true, LJCA_ENUM_CLIENT_TIMEOUT_MS); 661 if (ret < 0) 662 return (ret == -ETIMEDOUT) ? 0 : ret; 663 664 /* check firmware response */ 665 desc = (struct ljca_spi_descriptor *)buf; 666 if (ret != struct_size(desc, info, desc->num)) 667 return -EINVAL; 668 669 for (i = 0; i < desc->num; i++) { 670 /* construct platform data */ 671 spi_info = kzalloc_obj(*spi_info); 672 if (!spi_info) 673 return -ENOMEM; 674 675 spi_info->id = desc->info[i].id; 676 spi_info->capacity = desc->info[i].capacity; 677 678 ret = ljca_new_client_device(adap, LJCA_CLIENT_SPI, i, 679 "ljca-spi", spi_info, 680 LJCA_SPI1_ACPI_ADR + i); 681 if (ret) 682 return ret; 683 } 684 685 return 0; 686 } 687 688 static int ljca_reset_handshake(struct ljca_adapter *adap) 689 { 690 __le32 reset_id = cpu_to_le32(adap->reset_id); 691 __le32 reset_id_ret = 0; 692 int ret; 693 694 adap->reset_id++; 695 696 ret = ljca_send(adap, LJCA_CLIENT_MNG, LJCA_MNG_RESET, (u8 *)&reset_id, 697 sizeof(__le32), (u8 *)&reset_id_ret, sizeof(__le32), 698 true, LJCA_WRITE_ACK_TIMEOUT_MS); 699 if (ret < 0) 700 return ret; 701 702 if (reset_id_ret != reset_id) 703 return -EINVAL; 704 705 return 0; 706 } 707 708 static int ljca_enumerate_clients(struct ljca_adapter *adap) 709 { 710 struct ljca_client *client, *next; 711 int ret; 712 713 ret = ljca_reset_handshake(adap); 714 if (ret) 715 goto err_kill; 716 717 ret = ljca_enumerate_gpio(adap); 718 if (ret) { 719 dev_err(adap->dev, "enumerate GPIO error\n"); 720 goto err_kill; 721 } 722 723 ret = ljca_enumerate_i2c(adap); 724 if (ret) { 725 dev_err(adap->dev, "enumerate I2C error\n"); 726 goto err_kill; 727 } 728 729 ret = ljca_enumerate_spi(adap); 730 if (ret) { 731 dev_err(adap->dev, "enumerate SPI error\n"); 732 goto err_kill; 733 } 734 735 return 0; 736 737 err_kill: 738 adap->disconnect = true; 739 740 usb_kill_urb(adap->rx_urb); 741 742 list_for_each_entry_safe_reverse(client, next, &adap->client_list, link) { 743 auxiliary_device_delete(&client->auxdev); 744 auxiliary_device_uninit(&client->auxdev); 745 746 list_del_init(&client->link); 747 kfree(client); 748 } 749 750 return ret; 751 } 752 753 static int ljca_probe(struct usb_interface *interface, 754 const struct usb_device_id *id) 755 { 756 struct usb_device *usb_dev = interface_to_usbdev(interface); 757 struct usb_host_interface *alt = interface->cur_altsetting; 758 struct usb_endpoint_descriptor *ep_in, *ep_out; 759 struct device *dev = &interface->dev; 760 struct ljca_adapter *adap; 761 int ret; 762 763 adap = devm_kzalloc(dev, sizeof(*adap), GFP_KERNEL); 764 if (!adap) 765 return -ENOMEM; 766 767 /* separate tx buffer allocation for alignment */ 768 adap->tx_buf = devm_kzalloc(dev, LJCA_MAX_PACKET_SIZE, GFP_KERNEL); 769 if (!adap->tx_buf) 770 return -ENOMEM; 771 adap->tx_buf_len = LJCA_MAX_PACKET_SIZE; 772 773 mutex_init(&adap->mutex); 774 spin_lock_init(&adap->lock); 775 init_completion(&adap->cmd_completion); 776 INIT_LIST_HEAD(&adap->client_list); 777 778 adap->intf = interface; 779 adap->usb_dev = usb_dev; 780 adap->dev = dev; 781 782 /* 783 * find the first bulk in and out endpoints. 784 * ignore any others. 785 */ 786 ret = usb_find_common_endpoints(alt, &ep_in, &ep_out, NULL, NULL); 787 if (ret) { 788 dev_err(dev, "bulk endpoints not found\n"); 789 goto err_destroy_mutex; 790 } 791 adap->rx_pipe = usb_rcvbulkpipe(usb_dev, usb_endpoint_num(ep_in)); 792 adap->tx_pipe = usb_sndbulkpipe(usb_dev, usb_endpoint_num(ep_out)); 793 794 /* setup rx buffer */ 795 adap->rx_len = usb_endpoint_maxp(ep_in); 796 adap->rx_buf = devm_kzalloc(dev, adap->rx_len, GFP_KERNEL); 797 if (!adap->rx_buf) { 798 ret = -ENOMEM; 799 goto err_destroy_mutex; 800 } 801 802 /* alloc rx urb */ 803 adap->rx_urb = usb_alloc_urb(0, GFP_KERNEL); 804 if (!adap->rx_urb) { 805 ret = -ENOMEM; 806 goto err_destroy_mutex; 807 } 808 usb_fill_bulk_urb(adap->rx_urb, usb_dev, adap->rx_pipe, 809 adap->rx_buf, adap->rx_len, ljca_recv, adap); 810 811 usb_set_intfdata(interface, adap); 812 813 /* submit rx urb before enumerate clients */ 814 ret = usb_submit_urb(adap->rx_urb, GFP_KERNEL); 815 if (ret) { 816 dev_err(dev, "submit rx urb failed: %d\n", ret); 817 goto err_free; 818 } 819 820 ret = ljca_enumerate_clients(adap); 821 if (ret) 822 goto err_free; 823 824 /* 825 * This works around problems with ov2740 initialization on some 826 * Lenovo platforms. The autosuspend delay, has to be smaller than 827 * the delay after setting the reset_gpio line in ov2740_resume(). 828 * Otherwise the sensor randomly fails to initialize. 829 */ 830 pm_runtime_set_autosuspend_delay(&usb_dev->dev, 10); 831 832 usb_enable_autosuspend(usb_dev); 833 834 return 0; 835 836 err_free: 837 usb_free_urb(adap->rx_urb); 838 err_destroy_mutex: 839 mutex_destroy(&adap->mutex); 840 841 return ret; 842 } 843 844 static void ljca_disconnect(struct usb_interface *interface) 845 { 846 struct ljca_adapter *adap = usb_get_intfdata(interface); 847 struct ljca_client *client, *next; 848 849 adap->disconnect = true; 850 851 usb_kill_urb(adap->rx_urb); 852 853 list_for_each_entry_safe_reverse(client, next, &adap->client_list, link) { 854 auxiliary_device_delete(&client->auxdev); 855 auxiliary_device_uninit(&client->auxdev); 856 857 list_del_init(&client->link); 858 kfree(client); 859 } 860 861 usb_free_urb(adap->rx_urb); 862 863 mutex_destroy(&adap->mutex); 864 } 865 866 static int ljca_suspend(struct usb_interface *interface, pm_message_t message) 867 { 868 struct ljca_adapter *adap = usb_get_intfdata(interface); 869 870 usb_kill_urb(adap->rx_urb); 871 872 return 0; 873 } 874 875 static int ljca_resume(struct usb_interface *interface) 876 { 877 struct ljca_adapter *adap = usb_get_intfdata(interface); 878 879 return usb_submit_urb(adap->rx_urb, GFP_KERNEL); 880 } 881 882 static const struct usb_device_id ljca_table[] = { 883 { USB_DEVICE(0x8086, 0x0b63) }, 884 { /* sentinel */ } 885 }; 886 MODULE_DEVICE_TABLE(usb, ljca_table); 887 888 static struct usb_driver ljca_driver = { 889 .name = "ljca", 890 .id_table = ljca_table, 891 .probe = ljca_probe, 892 .disconnect = ljca_disconnect, 893 .suspend = ljca_suspend, 894 .resume = ljca_resume, 895 .supports_autosuspend = 1, 896 }; 897 module_usb_driver(ljca_driver); 898 899 MODULE_AUTHOR("Wentong Wu"); 900 MODULE_AUTHOR("Zhifeng Wang <zhifeng.wang@intel.com>"); 901 MODULE_AUTHOR("Lixu Zhang <lixu.zhang@intel.com>"); 902 MODULE_DESCRIPTION("Intel La Jolla Cove Adapter USB driver"); 903 MODULE_LICENSE("GPL"); 904