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