1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * USB Type-C Connector System Software Interface driver 4 * 5 * Copyright (C) 2017, Intel Corporation 6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com> 7 */ 8 9 #include <linux/completion.h> 10 #include <linux/property.h> 11 #include <linux/device.h> 12 #include <linux/module.h> 13 #include <linux/delay.h> 14 #include <linux/slab.h> 15 #include <linux/usb/typec_dp.h> 16 #include <linux/usb/typec_tbt.h> 17 18 #include "ucsi.h" 19 #include "trace.h" 20 21 /* 22 * UCSI_TIMEOUT_MS - PPM communication timeout 23 * 24 * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI 25 * specification) here as reference, but unfortunately we can't. It is very 26 * difficult to estimate the time it takes for the system to process the command 27 * before it is actually passed to the PPM. 28 */ 29 #define UCSI_TIMEOUT_MS 10000 30 31 /* 32 * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests 33 * 34 * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even 35 * if the PPM does not generate Connector Change events before that with 36 * partners that do not support USB Power Delivery, this should still work. 37 */ 38 #define UCSI_SWAP_TIMEOUT_MS 5000 39 40 void ucsi_notify_common(struct ucsi *ucsi, u32 cci) 41 { 42 /* Ignore bogus data in CCI if busy indicator is set. */ 43 if (cci & UCSI_CCI_BUSY) 44 return; 45 46 if (UCSI_CCI_CONNECTOR(cci)) { 47 if (!ucsi->cap.num_connectors || 48 UCSI_CCI_CONNECTOR(cci) <= ucsi->cap.num_connectors) 49 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci)); 50 else 51 dev_err(ucsi->dev, "bogus connector number in CCI: %lu\n", 52 UCSI_CCI_CONNECTOR(cci)); 53 } 54 55 if (cci & UCSI_CCI_ACK_COMPLETE && 56 test_and_clear_bit(ACK_PENDING, &ucsi->flags)) 57 complete(&ucsi->complete); 58 59 if (cci & UCSI_CCI_COMMAND_COMPLETE && 60 test_and_clear_bit(COMMAND_PENDING, &ucsi->flags)) 61 complete(&ucsi->complete); 62 } 63 EXPORT_SYMBOL_GPL(ucsi_notify_common); 64 65 int ucsi_sync_control_common(struct ucsi *ucsi, u64 command, u32 *cci, 66 void *data, size_t size, void *msg_out, 67 size_t msg_out_size) 68 { 69 bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI; 70 int ret; 71 72 if (ack) 73 set_bit(ACK_PENDING, &ucsi->flags); 74 else 75 set_bit(COMMAND_PENDING, &ucsi->flags); 76 77 reinit_completion(&ucsi->complete); 78 79 if (msg_out && msg_out_size) { 80 if (!ucsi->ops->write_message_out) { 81 ret = -EOPNOTSUPP; 82 goto out_clear_bit; 83 } 84 85 ret = ucsi->ops->write_message_out(ucsi, msg_out, msg_out_size); 86 if (ret) 87 goto out_clear_bit; 88 } 89 90 ret = ucsi->ops->async_control(ucsi, command); 91 if (ret) 92 goto out_clear_bit; 93 94 if (!wait_for_completion_timeout(&ucsi->complete, 95 msecs_to_jiffies(UCSI_TIMEOUT_MS))) 96 ret = -ETIMEDOUT; 97 98 out_clear_bit: 99 if (ack) 100 clear_bit(ACK_PENDING, &ucsi->flags); 101 else 102 clear_bit(COMMAND_PENDING, &ucsi->flags); 103 104 if (!ret && cci) 105 ret = ucsi->ops->read_cci(ucsi, cci); 106 107 if (!ret && data && 108 (*cci & UCSI_CCI_COMMAND_COMPLETE)) 109 ret = ucsi->ops->read_message_in(ucsi, data, size); 110 111 return ret; 112 } 113 EXPORT_SYMBOL_GPL(ucsi_sync_control_common); 114 115 static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack) 116 { 117 u64 ctrl; 118 119 ctrl = UCSI_ACK_CC_CI; 120 ctrl |= UCSI_ACK_COMMAND_COMPLETE; 121 if (conn_ack) { 122 clear_bit(EVENT_PENDING, &ucsi->flags); 123 ctrl |= UCSI_ACK_CONNECTOR_CHANGE; 124 } 125 126 return ucsi->ops->sync_control(ucsi, ctrl, NULL, NULL, 0, NULL, 0); 127 } 128 129 static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci, 130 void *data, size_t size, void *msg_out, 131 size_t msg_out_size, bool conn_ack) 132 { 133 int ret, err; 134 135 *cci = 0; 136 137 if (size > UCSI_MAX_DATA_LENGTH(ucsi)) 138 return -EINVAL; 139 140 ret = ucsi->ops->sync_control(ucsi, command, cci, data, size, 141 msg_out, msg_out_size); 142 143 if (*cci & UCSI_CCI_BUSY) 144 return ucsi_run_command(ucsi, UCSI_CANCEL, cci, 145 NULL, 0, NULL, 0, false) ?: -EBUSY; 146 if (ret) 147 return ret; 148 149 if (!(*cci & UCSI_CCI_COMMAND_COMPLETE)) 150 return -EIO; 151 152 if (*cci & UCSI_CCI_NOT_SUPPORTED) 153 err = -EOPNOTSUPP; 154 else if (*cci & UCSI_CCI_ERROR) 155 err = -EIO; 156 else 157 err = 0; 158 159 /* 160 * Don't ACK connection change if there was an error. 161 */ 162 ret = ucsi_acknowledge(ucsi, err ? false : conn_ack); 163 if (ret) 164 return ret; 165 166 return err ?: UCSI_CCI_LENGTH(*cci); 167 } 168 169 static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num) 170 { 171 u64 command; 172 u16 error; 173 u32 cci; 174 int ret; 175 176 command = UCSI_GET_ERROR_STATUS | UCSI_CONNECTOR_NUMBER(connector_num); 177 ret = ucsi_run_command(ucsi, command, &cci, &error, 178 sizeof(error), NULL, 0, false); 179 if (ret < 0) 180 return ret; 181 182 switch (error) { 183 case UCSI_ERROR_INCOMPATIBLE_PARTNER: 184 return -EOPNOTSUPP; 185 case UCSI_ERROR_CC_COMMUNICATION_ERR: 186 return -ECOMM; 187 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL: 188 return -EPROTO; 189 case UCSI_ERROR_DEAD_BATTERY: 190 dev_warn(ucsi->dev, "Dead battery condition!\n"); 191 return -EPERM; 192 case UCSI_ERROR_INVALID_CON_NUM: 193 case UCSI_ERROR_UNREGONIZED_CMD: 194 case UCSI_ERROR_INVALID_CMD_ARGUMENT: 195 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error); 196 return -EINVAL; 197 case UCSI_ERROR_OVERCURRENT: 198 dev_warn(ucsi->dev, "Overcurrent condition\n"); 199 break; 200 case UCSI_ERROR_PARTNER_REJECTED_SWAP: 201 dev_warn(ucsi->dev, "Partner rejected swap\n"); 202 break; 203 case UCSI_ERROR_HARD_RESET: 204 dev_warn(ucsi->dev, "Hard reset occurred\n"); 205 break; 206 case UCSI_ERROR_PPM_POLICY_CONFLICT: 207 dev_warn(ucsi->dev, "PPM Policy conflict\n"); 208 break; 209 case UCSI_ERROR_SWAP_REJECTED: 210 dev_warn(ucsi->dev, "Swap rejected\n"); 211 break; 212 case UCSI_ERROR_REVERSE_CURRENT_PROTECTION: 213 dev_warn(ucsi->dev, "Reverse Current Protection detected\n"); 214 break; 215 case UCSI_ERROR_SET_SINK_PATH_REJECTED: 216 dev_warn(ucsi->dev, "Set Sink Path rejected\n"); 217 break; 218 case UCSI_ERROR_UNDEFINED: 219 default: 220 dev_err(ucsi->dev, "unknown error %u\n", error); 221 break; 222 } 223 224 return -EIO; 225 } 226 227 static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd, 228 void *data, size_t size, void *msg_out, 229 size_t msg_out_size, bool conn_ack) 230 { 231 u8 connector_num; 232 u32 cci; 233 int ret; 234 235 if (ucsi->version > UCSI_VERSION_1_2) { 236 switch (UCSI_COMMAND(cmd)) { 237 case UCSI_GET_ALTERNATE_MODES: 238 connector_num = UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(cmd); 239 break; 240 case UCSI_PPM_RESET: 241 case UCSI_CANCEL: 242 case UCSI_ACK_CC_CI: 243 case UCSI_SET_NOTIFICATION_ENABLE: 244 case UCSI_GET_CAPABILITY: 245 connector_num = 0; 246 break; 247 default: 248 connector_num = UCSI_DEFAULT_GET_CONNECTOR_NUMBER(cmd); 249 break; 250 } 251 } else { 252 connector_num = 0; 253 } 254 255 mutex_lock(&ucsi->ppm_lock); 256 257 ret = ucsi_run_command(ucsi, cmd, &cci, data, size, 258 msg_out, msg_out_size, conn_ack); 259 260 if (cci & UCSI_CCI_ERROR) 261 ret = ucsi_read_error(ucsi, connector_num); 262 263 trace_ucsi_run_command(cmd, ret); 264 265 mutex_unlock(&ucsi->ppm_lock); 266 return ret; 267 } 268 269 int ucsi_send_command(struct ucsi *ucsi, u64 command, 270 void *data, size_t size) 271 { 272 return ucsi_send_command_common(ucsi, command, data, 273 size, NULL, 0, false); 274 } 275 EXPORT_SYMBOL_GPL(ucsi_send_command); 276 277 int ucsi_write_message_out_command(struct ucsi *ucsi, u64 command, 278 void *data, size_t size, void *msg_out, 279 size_t msg_out_size) 280 { 281 if (msg_out_size > UCSI_MAX_MSG_OUT_DATA_LEN(ucsi)) 282 return -EINVAL; 283 284 return ucsi_send_command_common(ucsi, command, data, 285 size, msg_out, msg_out_size, false); 286 } 287 EXPORT_SYMBOL_GPL(ucsi_write_message_out_command); 288 289 /* -------------------------------------------------------------------------- */ 290 291 struct ucsi_work { 292 struct delayed_work work; 293 struct list_head node; 294 unsigned long delay; 295 unsigned int count; 296 struct ucsi_connector *con; 297 int (*cb)(struct ucsi_connector *); 298 }; 299 300 static void ucsi_poll_worker(struct work_struct *work) 301 { 302 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work); 303 struct ucsi_connector *con = uwork->con; 304 int ret; 305 306 mutex_lock(&con->lock); 307 308 if (!con->partner) { 309 list_del(&uwork->node); 310 mutex_unlock(&con->lock); 311 kfree(uwork); 312 return; 313 } 314 315 ret = uwork->cb(con); 316 317 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) { 318 queue_delayed_work(con->wq, &uwork->work, uwork->delay); 319 } else { 320 list_del(&uwork->node); 321 kfree(uwork); 322 } 323 324 mutex_unlock(&con->lock); 325 } 326 327 static int ucsi_partner_task(struct ucsi_connector *con, 328 int (*cb)(struct ucsi_connector *), 329 int retries, unsigned long delay) 330 { 331 struct ucsi_work *uwork; 332 333 if (!con->partner) 334 return 0; 335 336 uwork = kzalloc_obj(*uwork); 337 if (!uwork) 338 return -ENOMEM; 339 340 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker); 341 uwork->count = retries; 342 uwork->delay = delay; 343 uwork->con = con; 344 uwork->cb = cb; 345 346 list_add_tail(&uwork->node, &con->partner_tasks); 347 queue_delayed_work(con->wq, &uwork->work, delay); 348 349 return 0; 350 } 351 352 /* -------------------------------------------------------------------------- */ 353 354 void ucsi_altmode_update_active(struct ucsi_connector *con) 355 { 356 const struct typec_altmode *altmode = NULL; 357 u64 command; 358 u16 svid = 0; 359 int ret; 360 u8 cur; 361 int i; 362 363 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num); 364 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur)); 365 if (ret < 0) { 366 if (con->ucsi->version > 0x0100) { 367 dev_err(con->ucsi->dev, 368 "GET_CURRENT_CAM command failed\n"); 369 return; 370 } 371 cur = 0xff; 372 } 373 374 if (cur < UCSI_MAX_ALTMODES) 375 altmode = typec_altmode_get_partner(con->port_altmode[cur]); 376 377 for (i = 0; con->partner_altmode[i]; i++) 378 typec_altmode_update_active(con->partner_altmode[i], 379 con->partner_altmode[i] == altmode); 380 381 if (altmode) 382 svid = altmode->svid; 383 typec_altmode_state_update(con->partner, svid, 0); 384 } 385 386 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid) 387 { 388 u8 mode = 1; 389 int i; 390 391 for (i = 0; alt[i]; i++) { 392 if (i > MODE_DISCOVERY_MAX) 393 return -ERANGE; 394 395 if (alt[i]->svid == svid) 396 mode++; 397 } 398 399 return mode; 400 } 401 402 static int ucsi_next_altmode(struct typec_altmode **alt) 403 { 404 int i = 0; 405 406 for (i = 0; i < UCSI_MAX_ALTMODES; i++) 407 if (!alt[i]) 408 return i; 409 410 return -ENOENT; 411 } 412 413 static int ucsi_get_num_altmode(struct typec_altmode **alt) 414 { 415 int i; 416 417 for (i = 0; i < UCSI_MAX_ALTMODES; i++) 418 if (!alt[i]) 419 break; 420 421 return i; 422 } 423 424 static int ucsi_register_altmode(struct ucsi_connector *con, 425 struct typec_altmode_desc *desc, 426 u8 recipient) 427 { 428 struct typec_altmode *alt; 429 bool override; 430 int ret; 431 int i; 432 433 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE); 434 435 switch (recipient) { 436 case UCSI_RECIPIENT_CON: 437 i = ucsi_next_altmode(con->port_altmode); 438 if (i < 0) { 439 ret = i; 440 goto err; 441 } 442 443 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid); 444 if (ret < 0) 445 return ret; 446 447 desc->mode = ret; 448 449 switch (desc->svid) { 450 case USB_TYPEC_DP_SID: 451 alt = ucsi_register_displayport(con, override, i, desc); 452 break; 453 case USB_TYPEC_NVIDIA_VLINK_SID: 454 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO) 455 alt = typec_port_register_altmode(con->port, 456 desc); 457 else 458 alt = ucsi_register_displayport(con, override, 459 i, desc); 460 break; 461 case USB_TYPEC_TBT_SID: 462 alt = ucsi_register_thunderbolt(con, override, i, desc); 463 break; 464 default: 465 alt = typec_port_register_altmode(con->port, desc); 466 break; 467 } 468 469 if (IS_ERR(alt)) { 470 ret = PTR_ERR(alt); 471 goto err; 472 } 473 474 con->port_altmode[i] = alt; 475 break; 476 case UCSI_RECIPIENT_SOP: 477 i = ucsi_next_altmode(con->partner_altmode); 478 if (i < 0) { 479 ret = i; 480 goto err; 481 } 482 483 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid); 484 if (ret < 0) 485 return ret; 486 487 desc->mode = ret; 488 489 alt = typec_partner_register_altmode(con->partner, desc); 490 if (IS_ERR(alt)) { 491 ret = PTR_ERR(alt); 492 goto err; 493 } 494 495 con->partner_altmode[i] = alt; 496 break; 497 case UCSI_RECIPIENT_SOP_P: 498 i = ucsi_next_altmode(con->plug_altmode); 499 if (i < 0) { 500 ret = i; 501 goto err; 502 } 503 504 ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid); 505 if (ret < 0) 506 return ret; 507 508 desc->mode = ret; 509 510 alt = typec_plug_register_altmode(con->plug, desc); 511 if (IS_ERR(alt)) { 512 ret = PTR_ERR(alt); 513 goto err; 514 } 515 516 con->plug_altmode[i] = alt; 517 break; 518 default: 519 return -EINVAL; 520 } 521 522 trace_ucsi_register_altmode(recipient, alt); 523 524 return 0; 525 526 err: 527 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n", 528 desc->svid, desc->mode); 529 530 return ret; 531 } 532 533 static void ucsi_dump_duplicate_altmode(struct ucsi_connector *con, 534 u8 recipient, u16 svid, 535 u32 existing_vdo, u32 new_vdo, 536 int offset) 537 { 538 static const char * const recipient_names[] = { 539 [UCSI_RECIPIENT_CON] = "port", 540 [UCSI_RECIPIENT_SOP] = "partner", 541 [UCSI_RECIPIENT_SOP_P] = "plug", 542 [UCSI_RECIPIENT_SOP_PP] = "cable plug prime", 543 }; 544 545 dev_warn(con->ucsi->dev, 546 "con%d: Firmware bug: duplicate %s altmode SVID 0x%04x at offset %d, ignoring but please contact the BIOS vendor to fix this issue.\n", 547 con->num, recipient_names[recipient], svid, offset); 548 549 if (existing_vdo != new_vdo) 550 dev_warn(con->ucsi->dev, 551 "con%d: VDO mismatch: 0x%08x vs 0x%08x\n", 552 con->num, existing_vdo, new_vdo); 553 } 554 555 /* Count altmodes in @altmodes that advertise @svid. */ 556 static int ucsi_altmode_count_svid(struct typec_altmode **altmodes, u16 svid) 557 { 558 int count = 0; 559 int k; 560 561 for (k = 0; k < UCSI_MAX_ALTMODES; k++) { 562 if (!altmodes[k]) 563 break; 564 if (altmodes[k]->svid == svid) 565 count++; 566 } 567 568 return count; 569 } 570 571 /* 572 * Check if an altmode is a duplicate. Some firmware implementations 573 * incorrectly return the same altmode multiple times, causing sysfs errors. 574 * Returns true if the altmode should be skipped. 575 * 576 * The matching rules differ by recipient: 577 * 578 * - UCSI_RECIPIENT_CON (port) and UCSI_RECIPIENT_SOP_P (plug): 579 * Two altmodes with identical SVID and VDO are byte-for-byte duplicates 580 * and the second has no observable function. Drop them. 581 * 582 * - UCSI_RECIPIENT_SOP (partner): 583 * The typec class binds each partner altmode to a port altmode of the 584 * same SVID via altmode_match()/device_find_child(), which returns the 585 * first port altmode with a matching SVID. If the partner advertises 586 * more altmodes for SVID X than the port advertises, the surplus 587 * partner altmode(s) collapse onto an already-paired port altmode and 588 * trigger a "duplicate filename .../partner" sysfs error during 589 * typec_altmode_create_links(). Use the port-side altmode count for 590 * SVID X as the authoritative cap and reject any partner altmode that 591 * would exceed it. This preserves legitimate multi-Mode partner 592 * altmodes (e.g. vendor SVIDs that the port really does advertise 593 * twice) while filtering the firmware-generated duplicates that have 594 * no port counterpart. 595 */ 596 static bool ucsi_altmode_is_duplicate(struct ucsi_connector *con, u8 recipient, 597 const struct ucsi_altmode *alt_batch, int batch_idx, 598 u16 svid, u32 vdo, int offset) 599 { 600 struct typec_altmode **altmodes; 601 int port_count, partner_count; 602 int k; 603 604 /* Check for duplicates within the current batch first */ 605 for (k = 0; k < batch_idx; k++) { 606 if (alt_batch[k].svid == svid && alt_batch[k].mid == vdo) { 607 ucsi_dump_duplicate_altmode(con, recipient, svid, 608 vdo, vdo, offset); 609 return true; 610 } 611 } 612 613 switch (recipient) { 614 case UCSI_RECIPIENT_SOP: 615 /* 616 * Cap partner altmodes per SVID by the port-side count: 617 * any further partner altmode for that SVID would alias an 618 * already-paired port altmode and break typec sysfs. 619 */ 620 port_count = ucsi_altmode_count_svid(con->port_altmode, svid); 621 partner_count = ucsi_altmode_count_svid(con->partner_altmode, 622 svid); 623 if (port_count && partner_count >= port_count) { 624 ucsi_dump_duplicate_altmode(con, recipient, svid, 625 con->partner_altmode[partner_count - 1]->vdo, 626 vdo, offset); 627 return true; 628 } 629 return false; 630 case UCSI_RECIPIENT_CON: 631 altmodes = con->port_altmode; 632 break; 633 case UCSI_RECIPIENT_SOP_P: 634 altmodes = con->plug_altmode; 635 break; 636 default: 637 return false; 638 } 639 640 /* CON and SOP_P: drop only exact SVID+VDO duplicates. */ 641 for (k = 0; k < UCSI_MAX_ALTMODES; k++) { 642 if (!altmodes[k]) 643 break; 644 645 if (altmodes[k]->svid != svid || altmodes[k]->vdo != vdo) 646 continue; 647 648 ucsi_dump_duplicate_altmode(con, recipient, svid, 649 altmodes[k]->vdo, vdo, offset); 650 return true; 651 } 652 653 return false; 654 } 655 656 static int 657 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient) 658 { 659 int max_altmodes = UCSI_MAX_ALTMODES; 660 struct typec_altmode_desc desc; 661 struct ucsi_altmode alt; 662 struct ucsi_altmode orig[UCSI_MAX_ALTMODES]; 663 struct ucsi_altmode updated[UCSI_MAX_ALTMODES]; 664 struct ucsi *ucsi = con->ucsi; 665 bool multi_dp = false; 666 u64 command; 667 int ret; 668 int len; 669 int i; 670 int k = 0; 671 672 if (recipient == UCSI_RECIPIENT_CON) 673 max_altmodes = con->ucsi->cap.num_alt_modes; 674 675 memset(orig, 0, sizeof(orig)); 676 memset(updated, 0, sizeof(updated)); 677 678 /* First get all the alternate modes */ 679 for (i = 0; i < max_altmodes; i++) { 680 memset(&alt, 0, sizeof(alt)); 681 command = UCSI_GET_ALTERNATE_MODES; 682 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient); 683 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num); 684 command |= UCSI_GET_ALTMODE_OFFSET(i); 685 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt)); 686 /* 687 * We are collecting all altmodes first and then registering. 688 * Some type-C device will return zero length data beyond last 689 * alternate modes. We should not return if length is zero. 690 */ 691 if (len < 0) 692 return len; 693 694 /* We got all altmodes, now break out and register them */ 695 if (!len || !alt.svid) 696 break; 697 698 orig[k].mid = alt.mid; 699 orig[k].svid = alt.svid; 700 k++; 701 } 702 /* 703 * Update the original altmode table as some ppms may report 704 * multiple DP altmodes. 705 */ 706 multi_dp = ucsi->ops->update_altmodes(ucsi, recipient, orig, updated); 707 708 /* now register altmodes */ 709 for (i = 0; i < max_altmodes; i++) { 710 struct ucsi_altmode *altmode_array = multi_dp ? updated : orig; 711 712 if (!altmode_array[i].svid) 713 return 0; 714 715 /* 716 * Check for duplicates in current array and already 717 * registered altmodes. Skip if duplicate found. 718 */ 719 if (ucsi_altmode_is_duplicate(con, recipient, altmode_array, i, 720 altmode_array[i].svid, 721 altmode_array[i].mid, i)) 722 continue; 723 724 memset(&desc, 0, sizeof(desc)); 725 desc.svid = altmode_array[i].svid; 726 desc.vdo = altmode_array[i].mid; 727 desc.roles = TYPEC_PORT_DRD; 728 729 ret = ucsi_register_altmode(con, &desc, recipient); 730 if (ret) 731 return ret; 732 } 733 734 return 0; 735 } 736 737 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient) 738 { 739 int max_altmodes = UCSI_MAX_ALTMODES; 740 struct typec_altmode_desc desc; 741 struct ucsi_altmode alt[2]; 742 u64 command; 743 int num; 744 int ret; 745 int len; 746 int j; 747 int i; 748 749 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS)) 750 return 0; 751 752 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0]) 753 return 0; 754 755 if (con->ucsi->ops->update_altmodes) 756 return ucsi_register_altmodes_nvidia(con, recipient); 757 758 if (recipient == UCSI_RECIPIENT_CON) 759 max_altmodes = con->ucsi->cap.num_alt_modes; 760 761 for (i = 0; i < max_altmodes;) { 762 memset(alt, 0, sizeof(alt)); 763 command = UCSI_GET_ALTERNATE_MODES; 764 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient); 765 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num); 766 command |= UCSI_GET_ALTMODE_OFFSET(i); 767 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt)); 768 if (len == -EBUSY) 769 continue; 770 if (len <= 0) 771 return len; 772 773 /* 774 * This code is requesting one alt mode at a time, but some PPMs 775 * may still return two. If that happens both alt modes need be 776 * registered and the offset for the next alt mode has to be 777 * incremented. 778 */ 779 num = len / sizeof(alt[0]); 780 i += num; 781 782 for (j = 0; j < num; j++) { 783 if (!alt[j].svid) 784 return 0; 785 786 /* 787 * Check for duplicates in current batch and already 788 * registered altmodes. Skip if duplicate found. 789 */ 790 if (ucsi_altmode_is_duplicate(con, recipient, alt, j, 791 alt[j].svid, alt[j].mid, 792 i - num + j)) 793 continue; 794 795 memset(&desc, 0, sizeof(desc)); 796 desc.vdo = alt[j].mid; 797 desc.svid = alt[j].svid; 798 desc.roles = TYPEC_PORT_DRD; 799 desc.mode_selection = con->ucsi->ops->add_partner_altmodes && 800 !con->typec_cap.no_mode_control; 801 802 ret = ucsi_register_altmode(con, &desc, recipient); 803 if (ret) 804 return ret; 805 } 806 } 807 808 return 0; 809 } 810 811 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient) 812 { 813 const struct typec_altmode *pdev; 814 struct typec_altmode **adev; 815 int i = 0; 816 817 switch (recipient) { 818 case UCSI_RECIPIENT_CON: 819 adev = con->port_altmode; 820 break; 821 case UCSI_RECIPIENT_SOP: 822 adev = con->partner_altmode; 823 break; 824 case UCSI_RECIPIENT_SOP_P: 825 adev = con->plug_altmode; 826 break; 827 default: 828 return; 829 } 830 831 while (adev[i]) { 832 if (recipient == UCSI_RECIPIENT_SOP) { 833 pdev = typec_altmode_get_partner(adev[i]); 834 835 if (adev[i]->svid == USB_TYPEC_DP_SID || 836 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID && 837 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO)) 838 ucsi_displayport_remove_partner((void *)pdev); 839 else if (adev[i]->svid == USB_TYPEC_TBT_SID) 840 ucsi_thunderbolt_remove_partner((void *)pdev); 841 } 842 typec_unregister_altmode(adev[i]); 843 adev[i++] = NULL; 844 } 845 } 846 847 static int ucsi_get_connector_status(struct ucsi_connector *con, bool conn_ack) 848 { 849 u64 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num); 850 size_t size = min(sizeof(con->status), 851 UCSI_MAX_DATA_LENGTH(con->ucsi)); 852 int ret; 853 854 ret = ucsi_send_command_common(con->ucsi, command, &con->status, size, 855 NULL, 0, conn_ack); 856 857 return ret < 0 ? ret : 0; 858 } 859 860 static int ucsi_read_pdos(struct ucsi_connector *con, 861 enum typec_role role, int is_partner, 862 u32 *pdos, int offset, int num_pdos) 863 { 864 struct ucsi *ucsi = con->ucsi; 865 u64 command; 866 int ret; 867 868 if (is_partner && 869 ucsi->quirks & UCSI_NO_PARTNER_PDOS && 870 (UCSI_CONSTAT(con, PWR_DIR) || !is_source(role))) 871 return 0; 872 873 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num); 874 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner); 875 command |= UCSI_GET_PDOS_PDO_OFFSET(offset); 876 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1); 877 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0; 878 ret = ucsi_send_command(ucsi, command, pdos + offset, 879 num_pdos * sizeof(u32)); 880 if (ret < 0 && ret != -ETIMEDOUT) 881 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret); 882 883 return ret; 884 } 885 886 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role, 887 int is_partner, u32 *pdos) 888 { 889 struct ucsi *ucsi = con->ucsi; 890 u8 num_pdos; 891 int ret; 892 893 if (!(ucsi->cap.features & UCSI_CAP_PDO_DETAILS)) 894 return 0; 895 896 /* UCSI max payload means only getting at most 4 PDOs at a time */ 897 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS); 898 if (ret < 0) 899 return ret; 900 901 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */ 902 if (num_pdos < UCSI_MAX_PDOS) 903 return num_pdos; 904 905 /* get the remaining PDOs, if any */ 906 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS, 907 PDO_MAX_OBJECTS - UCSI_MAX_PDOS); 908 if (ret < 0) 909 return ret; 910 911 return ret / sizeof(u32) + num_pdos; 912 } 913 914 static int ucsi_get_src_pdos(struct ucsi_connector *con) 915 { 916 int ret; 917 918 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos); 919 if (ret < 0) 920 return ret; 921 922 con->num_pdos = ret; 923 924 ucsi_port_psy_changed(con); 925 926 return ret; 927 } 928 929 static struct usb_power_delivery_capabilities *ucsi_get_pd_caps(struct ucsi_connector *con, 930 enum typec_role role, 931 bool is_partner) 932 { 933 struct usb_power_delivery_capabilities_desc pd_caps; 934 int ret; 935 936 ret = ucsi_get_pdos(con, role, is_partner, pd_caps.pdo); 937 if (ret <= 0) 938 return ERR_PTR(ret); 939 940 if (ret < PDO_MAX_OBJECTS) 941 pd_caps.pdo[ret] = 0; 942 943 pd_caps.role = role; 944 945 return usb_power_delivery_register_capabilities(is_partner ? con->partner_pd : con->pd, 946 &pd_caps); 947 } 948 949 static int ucsi_get_pd_message(struct ucsi_connector *con, u8 recipient, 950 size_t bytes, void *data, u8 type) 951 { 952 size_t len = min(bytes, UCSI_MAX_DATA_LENGTH(con->ucsi)); 953 u64 command; 954 u8 offset; 955 int ret; 956 957 for (offset = 0; offset < bytes; offset += len) { 958 len = min(len, bytes - offset); 959 960 command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | UCSI_CONNECTOR_NUMBER(con->num); 961 command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient); 962 command |= UCSI_GET_PD_MESSAGE_OFFSET(offset); 963 command |= UCSI_GET_PD_MESSAGE_BYTES(len); 964 command |= UCSI_GET_PD_MESSAGE_TYPE(type); 965 966 ret = ucsi_send_command(con->ucsi, command, data + offset, len); 967 if (ret < 0) 968 return ret; 969 } 970 971 return 0; 972 } 973 974 static int ucsi_get_partner_identity(struct ucsi_connector *con) 975 { 976 u32 vdo[7] = {}; 977 int ret; 978 979 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP, sizeof(vdo), vdo, 980 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY); 981 if (ret < 0) 982 return ret; 983 984 /* VDM Header is not part of struct usb_pd_identity, so dropping it. */ 985 con->partner_identity = *(struct usb_pd_identity *)&vdo[1]; 986 987 ret = typec_partner_set_identity(con->partner); 988 if (ret < 0) 989 dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n", ret); 990 991 return ret; 992 } 993 994 static int ucsi_get_cable_identity(struct ucsi_connector *con) 995 { 996 u32 vdo[7] = {}; 997 int ret; 998 999 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP_P, sizeof(vdo), vdo, 1000 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY); 1001 if (ret < 0) 1002 return ret; 1003 1004 con->cable_identity = *(struct usb_pd_identity *)&vdo[1]; 1005 1006 ret = typec_cable_set_identity(con->cable); 1007 if (ret < 0) 1008 dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n", ret); 1009 1010 return ret; 1011 } 1012 1013 static int ucsi_check_altmodes(struct ucsi_connector *con) 1014 { 1015 int ret, num_partner_am; 1016 1017 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP); 1018 if (ret && ret != -ETIMEDOUT) 1019 dev_err(con->ucsi->dev, 1020 "con%d: failed to register partner alt modes (%d)\n", 1021 con->num, ret); 1022 1023 /* Ignoring the errors in this case. */ 1024 if (con->partner_altmode[0]) { 1025 num_partner_am = ucsi_get_num_altmode(con->partner_altmode); 1026 typec_partner_set_num_altmodes(con->partner, num_partner_am); 1027 if (con->ucsi->ops->add_partner_altmodes) 1028 con->ucsi->ops->add_partner_altmodes(con); 1029 ucsi_altmode_update_active(con); 1030 return 0; 1031 } else { 1032 typec_partner_set_num_altmodes(con->partner, 0); 1033 } 1034 1035 return ret; 1036 } 1037 1038 static void ucsi_register_device_pdos(struct ucsi_connector *con) 1039 { 1040 struct ucsi *ucsi = con->ucsi; 1041 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version }; 1042 struct usb_power_delivery_capabilities *pd_cap; 1043 1044 if (con->pd) 1045 return; 1046 1047 con->pd = usb_power_delivery_register(ucsi->dev, &desc); 1048 1049 pd_cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, false); 1050 if (!IS_ERR(pd_cap)) 1051 con->port_source_caps = pd_cap; 1052 1053 pd_cap = ucsi_get_pd_caps(con, TYPEC_SINK, false); 1054 if (!IS_ERR(pd_cap)) 1055 con->port_sink_caps = pd_cap; 1056 1057 typec_port_set_usb_power_delivery(con->port, con->pd); 1058 } 1059 1060 static int ucsi_register_partner_pdos(struct ucsi_connector *con) 1061 { 1062 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version }; 1063 struct usb_power_delivery_capabilities *cap; 1064 1065 if (con->partner_pd) 1066 return 0; 1067 1068 con->partner_pd = typec_partner_usb_power_delivery_register(con->partner, &desc); 1069 if (IS_ERR(con->partner_pd)) 1070 return PTR_ERR(con->partner_pd); 1071 1072 cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, true); 1073 if (IS_ERR(cap)) 1074 return PTR_ERR(cap); 1075 1076 con->partner_source_caps = cap; 1077 1078 cap = ucsi_get_pd_caps(con, TYPEC_SINK, true); 1079 if (IS_ERR(cap)) 1080 return PTR_ERR(cap); 1081 1082 con->partner_sink_caps = cap; 1083 1084 return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd); 1085 } 1086 1087 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con) 1088 { 1089 usb_power_delivery_unregister_capabilities(con->partner_sink_caps); 1090 con->partner_sink_caps = NULL; 1091 usb_power_delivery_unregister_capabilities(con->partner_source_caps); 1092 con->partner_source_caps = NULL; 1093 usb_power_delivery_unregister(con->partner_pd); 1094 con->partner_pd = NULL; 1095 } 1096 1097 static int ucsi_register_plug(struct ucsi_connector *con) 1098 { 1099 struct typec_plug *plug; 1100 struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P}; 1101 1102 plug = typec_register_plug(con->cable, &desc); 1103 if (IS_ERR(plug)) { 1104 dev_err(con->ucsi->dev, 1105 "con%d: failed to register plug (%pe)\n", con->num, 1106 plug); 1107 return PTR_ERR(plug); 1108 } 1109 1110 con->plug = plug; 1111 return 0; 1112 } 1113 1114 static void ucsi_unregister_plug(struct ucsi_connector *con) 1115 { 1116 if (!con->plug) 1117 return; 1118 1119 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P); 1120 typec_unregister_plug(con->plug); 1121 con->plug = NULL; 1122 } 1123 1124 static int ucsi_register_cable(struct ucsi_connector *con) 1125 { 1126 struct ucsi_cable_property cable_prop; 1127 struct typec_cable *cable; 1128 struct typec_cable_desc desc = {}; 1129 u64 command; 1130 int ret; 1131 1132 command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num); 1133 ret = ucsi_send_command(con->ucsi, command, &cable_prop, sizeof(cable_prop)); 1134 if (ret < 0) { 1135 dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", ret); 1136 return ret; 1137 } 1138 1139 switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(cable_prop.flags)) { 1140 case UCSI_CABLE_PROPERTY_PLUG_TYPE_A: 1141 desc.type = USB_PLUG_TYPE_A; 1142 break; 1143 case UCSI_CABLE_PROPERTY_PLUG_TYPE_B: 1144 desc.type = USB_PLUG_TYPE_B; 1145 break; 1146 case UCSI_CABLE_PROPERTY_PLUG_TYPE_C: 1147 desc.type = USB_PLUG_TYPE_C; 1148 break; 1149 default: 1150 desc.type = USB_PLUG_NONE; 1151 break; 1152 } 1153 1154 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) 1155 desc.identity = &con->cable_identity; 1156 desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & cable_prop.flags); 1157 1158 if (con->ucsi->version >= UCSI_VERSION_2_1) 1159 desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(cable_prop.flags); 1160 1161 cable = typec_register_cable(con->port, &desc); 1162 if (IS_ERR(cable)) { 1163 dev_err(con->ucsi->dev, 1164 "con%d: failed to register cable (%pe)\n", con->num, 1165 cable); 1166 return PTR_ERR(cable); 1167 } 1168 1169 con->cable = cable; 1170 return 0; 1171 } 1172 1173 static void ucsi_unregister_cable(struct ucsi_connector *con) 1174 { 1175 if (!con->cable) 1176 return; 1177 1178 ucsi_unregister_plug(con); 1179 typec_unregister_cable(con->cable); 1180 memset(&con->cable_identity, 0, sizeof(con->cable_identity)); 1181 con->cable = NULL; 1182 } 1183 1184 static int ucsi_check_connector_capability(struct ucsi_connector *con) 1185 { 1186 u64 pd_revision; 1187 u64 command; 1188 int ret; 1189 1190 if (!con->partner || con->ucsi->version < UCSI_VERSION_2_1) 1191 return 0; 1192 1193 command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num); 1194 ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap)); 1195 if (ret < 0) { 1196 dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret); 1197 return ret; 1198 } 1199 1200 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1); 1201 typec_partner_set_pd_revision(con->partner, UCSI_SPEC_REVISION_TO_BCD(pd_revision)); 1202 1203 return ret; 1204 } 1205 1206 static void ucsi_orientation(struct ucsi_connector *con) 1207 { 1208 if (con->ucsi->version < UCSI_VERSION_2_0) 1209 return; 1210 1211 if (!UCSI_CONSTAT(con, CONNECTED)) { 1212 typec_set_orientation(con->port, TYPEC_ORIENTATION_NONE); 1213 return; 1214 } 1215 1216 switch (UCSI_CONSTAT(con, ORIENTATION)) { 1217 case UCSI_CONSTAT_ORIENTATION_NORMAL: 1218 typec_set_orientation(con->port, TYPEC_ORIENTATION_NORMAL); 1219 break; 1220 case UCSI_CONSTAT_ORIENTATION_REVERSE: 1221 typec_set_orientation(con->port, TYPEC_ORIENTATION_REVERSE); 1222 break; 1223 default: 1224 break; 1225 } 1226 } 1227 1228 static void ucsi_pwr_opmode_change(struct ucsi_connector *con) 1229 { 1230 switch (UCSI_CONSTAT(con, PWR_OPMODE)) { 1231 case UCSI_CONSTAT_PWR_OPMODE_PD: 1232 con->rdo = UCSI_CONSTAT(con, RDO); 1233 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD); 1234 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0); 1235 ucsi_partner_task(con, ucsi_check_altmodes, 30, HZ); 1236 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ); 1237 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ); 1238 break; 1239 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5: 1240 con->rdo = 0; 1241 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A); 1242 ucsi_port_psy_changed(con); 1243 break; 1244 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0: 1245 con->rdo = 0; 1246 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A); 1247 ucsi_port_psy_changed(con); 1248 break; 1249 default: 1250 con->rdo = 0; 1251 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB); 1252 ucsi_port_psy_changed(con); 1253 break; 1254 } 1255 } 1256 1257 static int ucsi_register_partner(struct ucsi_connector *con) 1258 { 1259 u8 pwr_opmode = UCSI_CONSTAT(con, PWR_OPMODE); 1260 struct typec_partner_desc desc; 1261 struct typec_partner *partner; 1262 1263 if (con->partner) 1264 return 0; 1265 1266 memset(&desc, 0, sizeof(desc)); 1267 1268 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { 1269 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG: 1270 desc.accessory = TYPEC_ACCESSORY_DEBUG; 1271 break; 1272 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO: 1273 desc.accessory = TYPEC_ACCESSORY_AUDIO; 1274 break; 1275 default: 1276 break; 1277 } 1278 1279 if (pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD) 1280 ucsi_register_device_pdos(con); 1281 1282 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) 1283 desc.identity = &con->partner_identity; 1284 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD; 1285 1286 if (con->ucsi->version >= UCSI_VERSION_2_1) { 1287 u64 pd_revision = UCSI_CONCAP(con, PARTNER_PD_REVISION_V2_1); 1288 desc.pd_revision = UCSI_SPEC_REVISION_TO_BCD(pd_revision); 1289 } 1290 1291 partner = typec_register_partner(con->port, &desc); 1292 if (IS_ERR(partner)) { 1293 dev_err(con->ucsi->dev, 1294 "con%d: failed to register partner (%pe)\n", con->num, 1295 partner); 1296 return PTR_ERR(partner); 1297 } 1298 1299 con->partner = partner; 1300 1301 if (con->ucsi->version >= UCSI_VERSION_3_0 && 1302 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN4)) 1303 typec_partner_set_usb_mode(partner, USB_MODE_USB4); 1304 else if (con->ucsi->version >= UCSI_VERSION_2_0 && 1305 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN3)) 1306 typec_partner_set_usb_mode(partner, USB_MODE_USB4); 1307 1308 return 0; 1309 } 1310 1311 static void ucsi_unregister_partner(struct ucsi_connector *con) 1312 { 1313 if (!con->partner) 1314 return; 1315 1316 typec_set_mode(con->port, TYPEC_STATE_SAFE); 1317 if (con->ucsi->ops->remove_partner_altmodes) 1318 con->ucsi->ops->remove_partner_altmodes(con); 1319 1320 typec_partner_set_usb_power_delivery(con->partner, NULL); 1321 ucsi_unregister_partner_pdos(con); 1322 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP); 1323 ucsi_unregister_cable(con); 1324 typec_unregister_partner(con->partner); 1325 memset(&con->partner_identity, 0, sizeof(con->partner_identity)); 1326 con->partner = NULL; 1327 } 1328 1329 static void ucsi_partner_change(struct ucsi_connector *con) 1330 { 1331 enum usb_role u_role = USB_ROLE_NONE; 1332 int ret; 1333 1334 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { 1335 case UCSI_CONSTAT_PARTNER_TYPE_UFP: 1336 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: 1337 u_role = USB_ROLE_HOST; 1338 fallthrough; 1339 case UCSI_CONSTAT_PARTNER_TYPE_CABLE: 1340 typec_set_data_role(con->port, TYPEC_HOST); 1341 break; 1342 case UCSI_CONSTAT_PARTNER_TYPE_DFP: 1343 u_role = USB_ROLE_DEVICE; 1344 typec_set_data_role(con->port, TYPEC_DEVICE); 1345 break; 1346 default: 1347 break; 1348 } 1349 1350 if (UCSI_CONSTAT(con, CONNECTED)) { 1351 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { 1352 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG: 1353 typec_set_mode(con->port, TYPEC_MODE_DEBUG); 1354 break; 1355 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO: 1356 typec_set_mode(con->port, TYPEC_MODE_AUDIO); 1357 break; 1358 default: 1359 if (UCSI_CONSTAT(con, PARTNER_FLAG_USB)) 1360 typec_set_mode(con->port, TYPEC_STATE_USB); 1361 } 1362 1363 if (((con->ucsi->version >= UCSI_VERSION_3_0 && 1364 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN4)) || 1365 (con->ucsi->version >= UCSI_VERSION_2_0 && 1366 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN3))) && con->partner) 1367 typec_partner_set_usb_mode(con->partner, USB_MODE_USB4); 1368 } 1369 1370 if ((!UCSI_CONSTAT(con, PARTNER_FLAG_USB)) && 1371 ((con->ucsi->quirks & UCSI_USB4_IMPLIES_USB) && 1372 (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN3) || 1373 UCSI_CONSTAT(con, PARTNER_FLAG_USB4_GEN4))))) 1374 u_role = USB_ROLE_NONE; 1375 1376 ret = usb_role_switch_set_role(con->usb_role_sw, u_role); 1377 if (ret) 1378 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n", 1379 con->num, u_role); 1380 } 1381 1382 static int ucsi_check_connection(struct ucsi_connector *con) 1383 { 1384 u8 prev_state = UCSI_CONSTAT(con, CONNECTED); 1385 int ret; 1386 1387 ret = ucsi_get_connector_status(con, false); 1388 if (ret) { 1389 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret); 1390 return ret; 1391 } 1392 1393 if (UCSI_CONSTAT(con, CONNECTED)) { 1394 if (prev_state) 1395 return 0; 1396 ucsi_register_partner(con); 1397 ucsi_pwr_opmode_change(con); 1398 ucsi_partner_change(con); 1399 } else { 1400 ucsi_partner_change(con); 1401 ucsi_port_psy_changed(con); 1402 ucsi_unregister_partner(con); 1403 } 1404 1405 return 0; 1406 } 1407 1408 static int ucsi_check_cable(struct ucsi_connector *con) 1409 { 1410 int ret, num_plug_am; 1411 1412 if (con->cable) 1413 return 0; 1414 1415 ret = ucsi_register_cable(con); 1416 if (ret < 0) 1417 return ret; 1418 1419 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) { 1420 ret = ucsi_get_cable_identity(con); 1421 if (ret < 0) 1422 return ret; 1423 } 1424 1425 if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) { 1426 ret = ucsi_register_plug(con); 1427 if (ret < 0) 1428 return ret; 1429 1430 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P); 1431 if (ret < 0) 1432 return ret; 1433 1434 if (con->plug_altmode[0]) { 1435 num_plug_am = ucsi_get_num_altmode(con->plug_altmode); 1436 typec_plug_set_num_altmodes(con->plug, num_plug_am); 1437 } else { 1438 typec_plug_set_num_altmodes(con->plug, 0); 1439 } 1440 } 1441 1442 return 0; 1443 } 1444 1445 static void ucsi_handle_connector_change(struct work_struct *work) 1446 { 1447 struct ucsi_connector *con = container_of(work, struct ucsi_connector, 1448 work); 1449 struct ucsi *ucsi = con->ucsi; 1450 u8 curr_scale, volt_scale; 1451 enum typec_role role, prev_role; 1452 u16 change; 1453 int ret; 1454 u32 val; 1455 1456 mutex_lock(&con->lock); 1457 1458 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags)) 1459 dev_err_once(ucsi->dev, "%s entered without EVENT_PENDING\n", 1460 __func__); 1461 1462 prev_role = UCSI_CONSTAT(con, PWR_DIR); 1463 1464 ret = ucsi_get_connector_status(con, true); 1465 if (ret) { 1466 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n", 1467 __func__, ret); 1468 clear_bit(EVENT_PENDING, &con->ucsi->flags); 1469 goto out_unlock; 1470 } 1471 1472 trace_ucsi_connector_change(con->num, con); 1473 1474 if (ucsi->ops->connector_status) 1475 ucsi->ops->connector_status(con); 1476 1477 change = UCSI_CONSTAT(con, CHANGE); 1478 role = UCSI_CONSTAT(con, PWR_DIR); 1479 1480 if ((change & UCSI_CONSTAT_POWER_DIR_CHANGE) && role != prev_role) { 1481 typec_set_pwr_role(con->port, role); 1482 1483 /* Some power_supply properties vary depending on the power direction when 1484 * connected 1485 */ 1486 if (UCSI_CONSTAT(con, CONNECTED)) 1487 ucsi_port_psy_changed(con); 1488 1489 /* Complete pending power role swap */ 1490 if (!completion_done(&con->complete)) 1491 complete(&con->complete); 1492 } 1493 1494 if (change & UCSI_CONSTAT_CONNECT_CHANGE) { 1495 typec_set_pwr_role(con->port, role); 1496 ucsi_port_psy_changed(con); 1497 ucsi_partner_change(con); 1498 ucsi_orientation(con); 1499 1500 if (UCSI_CONSTAT(con, CONNECTED)) { 1501 ucsi_register_partner(con); 1502 ucsi_partner_task(con, ucsi_check_connection, 1, HZ); 1503 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) 1504 ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ); 1505 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS) 1506 ucsi_partner_task(con, ucsi_check_cable, 1, HZ); 1507 1508 if (UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) { 1509 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ); 1510 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ); 1511 } 1512 } else { 1513 ucsi_unregister_partner(con); 1514 } 1515 } 1516 1517 if (change & (UCSI_CONSTAT_POWER_OPMODE_CHANGE | UCSI_CONSTAT_POWER_LEVEL_CHANGE)) 1518 ucsi_pwr_opmode_change(con); 1519 1520 if (con->partner && (change & UCSI_CONSTAT_PARTNER_CHANGE)) { 1521 ucsi_partner_change(con); 1522 if (con->partner_altmode[0]) 1523 ucsi_altmode_update_active(con); 1524 1525 /* Complete pending data role swap */ 1526 if (!completion_done(&con->complete)) 1527 complete(&con->complete); 1528 } 1529 1530 if (change & UCSI_CONSTAT_CAM_CHANGE) 1531 ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ); 1532 1533 if (change & (UCSI_CONSTAT_BC_CHANGE | UCSI_CONSTAT_SINK_PATH_CHANGE)) 1534 ucsi_port_psy_changed(con); 1535 1536 if (con->ucsi->version >= UCSI_VERSION_2_1 && 1537 UCSI_CONSTAT(con, PWR_READING_READY_V2_1)) { 1538 curr_scale = UCSI_CONSTAT(con, CURRENT_SCALE_V2_1); 1539 volt_scale = UCSI_CONSTAT(con, VOLTAGE_SCALE_V2_1); 1540 1541 val = UCSI_CONSTAT(con, PEAK_CURRENT_V2_1); 1542 con->peak_current = UCSI_CONSTAT_CURR_SCALE_MULT * curr_scale * val; 1543 1544 val = UCSI_CONSTAT(con, AVG_CURRENT_V2_1); 1545 con->avg_current = UCSI_CONSTAT_CURR_SCALE_MULT * curr_scale * val; 1546 1547 val = UCSI_CONSTAT(con, VBUS_VOLTAGE_V2_1); 1548 con->vbus_voltage = UCSI_CONSTAT_VOLT_SCALE_MULT * volt_scale * val; 1549 } 1550 1551 out_unlock: 1552 mutex_unlock(&con->lock); 1553 } 1554 1555 /** 1556 * ucsi_connector_change - Process Connector Change Event 1557 * @ucsi: UCSI Interface 1558 * @num: Connector number 1559 */ 1560 void ucsi_connector_change(struct ucsi *ucsi, u8 num) 1561 { 1562 struct ucsi_connector *con; 1563 1564 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) { 1565 dev_dbg(ucsi->dev, "Early connector change event\n"); 1566 return; 1567 } 1568 1569 if (!num || num > ucsi->cap.num_connectors) { 1570 dev_warn_ratelimited(ucsi->dev, 1571 "Bogus connector change on %u (max %u)\n", 1572 num, ucsi->cap.num_connectors); 1573 return; 1574 } 1575 1576 con = &ucsi->connector[num - 1]; 1577 1578 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags)) 1579 schedule_work(&con->work); 1580 } 1581 EXPORT_SYMBOL_GPL(ucsi_connector_change); 1582 1583 /* -------------------------------------------------------------------------- */ 1584 1585 /* 1586 * Hard Reset bit field was defined with value 1 in UCSI spec version 1.0. 1587 * Starting with spec version 1.1, Hard Reset bit field was removed from the 1588 * CONNECTOR_RESET command, until spec 2.0 reintroduced it with value 0, so, in effect, 1589 * the value to pass in to the command for a Hard Reset is different depending 1590 * on the supported UCSI version by the LPM. 1591 * 1592 * For performing a Data Reset on LPMs supporting version 2.0 and greater, 1593 * this function needs to be called with the second argument set to 0. 1594 */ 1595 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard) 1596 { 1597 u64 command; 1598 1599 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num); 1600 1601 if (con->ucsi->version < UCSI_VERSION_1_1) 1602 command |= hard ? UCSI_CONNECTOR_RESET_HARD_VER_1_0 : 0; 1603 else if (con->ucsi->version >= UCSI_VERSION_2_0) 1604 command |= hard ? 0 : UCSI_CONNECTOR_RESET_DATA_VER_2_0; 1605 1606 return ucsi_send_command(con->ucsi, command, NULL, 0); 1607 } 1608 1609 static int ucsi_reset_ppm(struct ucsi *ucsi) 1610 { 1611 u64 command; 1612 unsigned long tmo; 1613 u32 cci; 1614 int ret; 1615 1616 mutex_lock(&ucsi->ppm_lock); 1617 1618 ret = ucsi->ops->poll_cci(ucsi, &cci); 1619 if (ret < 0) 1620 goto out; 1621 1622 /* 1623 * If UCSI_CCI_RESET_COMPLETE is already set we must clear 1624 * the flag before we start another reset. Send a 1625 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this. 1626 * Ignore a timeout and try the reset anyway if this fails. 1627 */ 1628 if (cci & UCSI_CCI_RESET_COMPLETE) { 1629 command = UCSI_SET_NOTIFICATION_ENABLE; 1630 ret = ucsi->ops->async_control(ucsi, command); 1631 if (ret < 0) 1632 goto out; 1633 1634 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS); 1635 do { 1636 ret = ucsi->ops->poll_cci(ucsi, &cci); 1637 if (ret < 0) 1638 goto out; 1639 if (cci & UCSI_CCI_COMMAND_COMPLETE) 1640 break; 1641 if (time_is_before_jiffies(tmo)) 1642 break; 1643 msleep(20); 1644 } while (1); 1645 1646 WARN_ON(cci & UCSI_CCI_RESET_COMPLETE); 1647 } 1648 1649 command = UCSI_PPM_RESET; 1650 ret = ucsi->ops->async_control(ucsi, command); 1651 if (ret < 0) 1652 goto out; 1653 1654 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS); 1655 1656 do { 1657 if (time_is_before_jiffies(tmo)) { 1658 ret = -ETIMEDOUT; 1659 goto out; 1660 } 1661 1662 /* Give the PPM time to process a reset before reading CCI */ 1663 msleep(20); 1664 1665 ret = ucsi->ops->poll_cci(ucsi, &cci); 1666 if (ret) 1667 goto out; 1668 1669 /* If the PPM is still doing something else, reset it again. */ 1670 if (cci & ~UCSI_CCI_RESET_COMPLETE) { 1671 ret = ucsi->ops->async_control(ucsi, command); 1672 if (ret < 0) 1673 goto out; 1674 } 1675 1676 } while (!(cci & UCSI_CCI_RESET_COMPLETE)); 1677 1678 out: 1679 mutex_unlock(&ucsi->ppm_lock); 1680 return ret; 1681 } 1682 1683 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command) 1684 { 1685 int ret; 1686 1687 ret = ucsi_send_command(con->ucsi, command, NULL, 0); 1688 if (ret == -ETIMEDOUT) { 1689 u64 c; 1690 1691 /* PPM most likely stopped responding. Resetting everything. */ 1692 ucsi_reset_ppm(con->ucsi); 1693 1694 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy; 1695 ucsi_send_command(con->ucsi, c, NULL, 0); 1696 1697 ucsi_reset_connector(con, true); 1698 } 1699 1700 return ret; 1701 } 1702 1703 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role) 1704 { 1705 struct ucsi_connector *con = typec_get_drvdata(port); 1706 u8 partner_type; 1707 u64 command; 1708 int ret = 0; 1709 1710 mutex_lock(&con->lock); 1711 1712 if (!con->partner) { 1713 ret = -ENOTCONN; 1714 goto out_unlock; 1715 } 1716 1717 partner_type = UCSI_CONSTAT(con, PARTNER_TYPE); 1718 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP && 1719 role == TYPEC_DEVICE) || 1720 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP && 1721 role == TYPEC_HOST)) 1722 goto out_unlock; 1723 1724 reinit_completion(&con->complete); 1725 1726 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num); 1727 command |= UCSI_SET_UOR_ROLE(role); 1728 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS; 1729 ret = ucsi_role_cmd(con, command); 1730 if (ret < 0) 1731 goto out_unlock; 1732 1733 mutex_unlock(&con->lock); 1734 1735 if (!wait_for_completion_timeout(&con->complete, 1736 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) 1737 return -ETIMEDOUT; 1738 1739 return 0; 1740 1741 out_unlock: 1742 mutex_unlock(&con->lock); 1743 1744 return ret; 1745 } 1746 1747 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role) 1748 { 1749 struct ucsi_connector *con = typec_get_drvdata(port); 1750 enum typec_role cur_role; 1751 u64 command; 1752 int ret = 0; 1753 1754 mutex_lock(&con->lock); 1755 1756 if (!con->partner) { 1757 ret = -ENOTCONN; 1758 goto out_unlock; 1759 } 1760 1761 cur_role = UCSI_CONSTAT(con, PWR_DIR); 1762 1763 if (cur_role == role) 1764 goto out_unlock; 1765 1766 reinit_completion(&con->complete); 1767 1768 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num); 1769 command |= UCSI_SET_PDR_ROLE(role); 1770 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS; 1771 ret = ucsi_role_cmd(con, command); 1772 if (ret < 0) 1773 goto out_unlock; 1774 1775 mutex_unlock(&con->lock); 1776 1777 if (!wait_for_completion_timeout(&con->complete, 1778 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) 1779 return -ETIMEDOUT; 1780 1781 mutex_lock(&con->lock); 1782 1783 /* Something has gone wrong while swapping the role */ 1784 if (UCSI_CONSTAT(con, PWR_OPMODE) != UCSI_CONSTAT_PWR_OPMODE_PD) { 1785 ucsi_reset_connector(con, true); 1786 ret = -EPROTO; 1787 } 1788 1789 out_unlock: 1790 mutex_unlock(&con->lock); 1791 1792 return ret; 1793 } 1794 1795 static const struct typec_operations ucsi_ops = { 1796 .dr_set = ucsi_dr_swap, 1797 .pr_set = ucsi_pr_swap 1798 }; 1799 1800 /* Caller must call fwnode_handle_put() after use */ 1801 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con) 1802 { 1803 struct fwnode_handle *fwnode; 1804 int i = 1; 1805 int ret; 1806 u32 port; 1807 1808 device_for_each_child_node(con->ucsi->dev, fwnode) { 1809 ret = fwnode_property_read_u32(fwnode, "reg", &port); 1810 if (ret < 0) { 1811 if (i == con->num) 1812 return fwnode; 1813 } else { 1814 if (port == con->num - 1) 1815 return fwnode; 1816 } 1817 1818 i++; 1819 } 1820 return NULL; 1821 } 1822 1823 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con) 1824 { 1825 struct typec_capability *cap = &con->typec_cap; 1826 enum typec_accessory *accessory = cap->accessory; 1827 enum usb_role u_role = USB_ROLE_NONE; 1828 u64 command; 1829 char *name; 1830 int ret; 1831 1832 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num); 1833 if (!name) 1834 return -ENOMEM; 1835 1836 con->wq = create_singlethread_workqueue(name); 1837 kfree(name); 1838 if (!con->wq) 1839 return -ENOMEM; 1840 1841 INIT_WORK(&con->work, ucsi_handle_connector_change); 1842 init_completion(&con->complete); 1843 mutex_init(&con->lock); 1844 lockdep_set_class(&con->lock, &con->lock_key); 1845 INIT_LIST_HEAD(&con->partner_tasks); 1846 con->ucsi = ucsi; 1847 1848 cap->fwnode = ucsi_find_fwnode(con); 1849 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode); 1850 if (IS_ERR(con->usb_role_sw)) 1851 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw), 1852 "con%d: failed to get usb role switch\n", con->num); 1853 1854 /* Delay other interactions with the con until registration is complete */ 1855 mutex_lock(&con->lock); 1856 1857 /* Get connector capability */ 1858 command = UCSI_GET_CONNECTOR_CAPABILITY; 1859 command |= UCSI_CONNECTOR_NUMBER(con->num); 1860 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap)); 1861 if (ret < 0) 1862 goto out_unlock; 1863 1864 if (UCSI_CONCAP(con, OPMODE_DRP)) 1865 cap->data = TYPEC_PORT_DRD; 1866 else if (UCSI_CONCAP(con, OPMODE_DFP)) 1867 cap->data = TYPEC_PORT_DFP; 1868 else if (UCSI_CONCAP(con, OPMODE_UFP)) 1869 cap->data = TYPEC_PORT_UFP; 1870 1871 if (UCSI_CONCAP(con, PROVIDER) && UCSI_CONCAP(con, CONSUMER)) 1872 cap->type = TYPEC_PORT_DRP; 1873 else if (UCSI_CONCAP(con, PROVIDER)) 1874 cap->type = TYPEC_PORT_SRC; 1875 else if (UCSI_CONCAP(con, CONSUMER)) 1876 cap->type = TYPEC_PORT_SNK; 1877 1878 cap->revision = ucsi->cap.typec_version; 1879 cap->pd_revision = ucsi->cap.pd_version; 1880 cap->svdm_version = SVDM_VER_2_0; 1881 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE; 1882 1883 if (UCSI_CONCAP(con, OPMODE_AUDIO_ACCESSORY)) 1884 *accessory++ = TYPEC_ACCESSORY_AUDIO; 1885 if (UCSI_CONCAP(con, OPMODE_DEBUG_ACCESSORY)) 1886 *accessory = TYPEC_ACCESSORY_DEBUG; 1887 1888 if (UCSI_CONCAP_USB2_SUPPORT(con)) 1889 cap->usb_capability |= USB_CAPABILITY_USB2; 1890 if (UCSI_CONCAP_USB3_SUPPORT(con)) 1891 cap->usb_capability |= USB_CAPABILITY_USB3; 1892 if (UCSI_CONCAP_USB4_SUPPORT(con)) 1893 cap->usb_capability |= USB_CAPABILITY_USB4; 1894 1895 cap->driver_data = con; 1896 cap->ops = &ucsi_ops; 1897 cap->no_mode_control = !(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE); 1898 1899 if (ucsi->version >= UCSI_VERSION_2_0) 1900 con->typec_cap.orientation_aware = true; 1901 1902 if (ucsi->ops->update_connector) 1903 ucsi->ops->update_connector(con); 1904 1905 ret = ucsi_register_port_psy(con); 1906 if (ret) 1907 goto out; 1908 1909 /* Register the connector */ 1910 con->port = typec_register_port(ucsi->dev, cap); 1911 if (IS_ERR(con->port)) { 1912 ret = PTR_ERR(con->port); 1913 goto out; 1914 } 1915 1916 if (!(ucsi->quirks & UCSI_DELAY_DEVICE_PDOS)) 1917 ucsi_register_device_pdos(con); 1918 1919 /* Alternate modes */ 1920 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON); 1921 if (ret) { 1922 dev_err(ucsi->dev, "con%d: failed to register alt modes\n", 1923 con->num); 1924 goto out; 1925 } 1926 1927 /* Get the status */ 1928 ret = ucsi_get_connector_status(con, false); 1929 if (ret) { 1930 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num); 1931 goto out; 1932 } 1933 1934 if (ucsi->ops->connector_status) 1935 ucsi->ops->connector_status(con); 1936 1937 switch (UCSI_CONSTAT(con, PARTNER_TYPE)) { 1938 case UCSI_CONSTAT_PARTNER_TYPE_UFP: 1939 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP: 1940 u_role = USB_ROLE_HOST; 1941 fallthrough; 1942 case UCSI_CONSTAT_PARTNER_TYPE_CABLE: 1943 typec_set_data_role(con->port, TYPEC_HOST); 1944 break; 1945 case UCSI_CONSTAT_PARTNER_TYPE_DFP: 1946 u_role = USB_ROLE_DEVICE; 1947 typec_set_data_role(con->port, TYPEC_DEVICE); 1948 break; 1949 default: 1950 break; 1951 } 1952 1953 /* Check if there is already something connected */ 1954 if (UCSI_CONSTAT(con, CONNECTED)) { 1955 typec_set_pwr_role(con->port, UCSI_CONSTAT(con, PWR_DIR)); 1956 ucsi_register_partner(con); 1957 ucsi_pwr_opmode_change(con); 1958 ucsi_orientation(con); 1959 ucsi_port_psy_changed(con); 1960 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) 1961 ucsi_get_partner_identity(con); 1962 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS) 1963 ucsi_check_cable(con); 1964 } 1965 1966 /* Only notify USB controller if partner supports USB data */ 1967 if (!(UCSI_CONSTAT(con, PARTNER_FLAG_USB))) 1968 u_role = USB_ROLE_NONE; 1969 1970 ret = usb_role_switch_set_role(con->usb_role_sw, u_role); 1971 if (ret) { 1972 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n", 1973 con->num, u_role); 1974 ret = 0; 1975 } 1976 1977 if (con->partner && UCSI_CONSTAT(con, PWR_OPMODE) == UCSI_CONSTAT_PWR_OPMODE_PD) { 1978 ucsi_register_device_pdos(con); 1979 ucsi_get_src_pdos(con); 1980 ucsi_check_altmodes(con); 1981 ucsi_check_connector_capability(con); 1982 } 1983 1984 trace_ucsi_register_port(con->num, con); 1985 1986 out: 1987 fwnode_handle_put(cap->fwnode); 1988 out_unlock: 1989 mutex_unlock(&con->lock); 1990 1991 if (ret && con->wq) { 1992 destroy_workqueue(con->wq); 1993 con->wq = NULL; 1994 } 1995 1996 return ret; 1997 } 1998 1999 static void ucsi_unregister_port(struct ucsi_connector *con) 2000 { 2001 struct ucsi_work *uwork; 2002 2003 if (con->wq) { 2004 mutex_lock(&con->lock); 2005 ucsi_unregister_partner(con); 2006 /* 2007 * queue delayed items immediately so they can execute 2008 * and free themselves before the wq is destroyed 2009 */ 2010 list_for_each_entry(uwork, &con->partner_tasks, node) { 2011 if (cancel_delayed_work(&uwork->work)) 2012 queue_delayed_work(con->wq, &uwork->work, 0); 2013 } 2014 mutex_unlock(&con->lock); 2015 2016 destroy_workqueue(con->wq); 2017 con->wq = NULL; 2018 } else { 2019 ucsi_unregister_partner(con); 2020 } 2021 2022 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON); 2023 ucsi_unregister_port_psy(con); 2024 2025 usb_power_delivery_unregister_capabilities(con->port_sink_caps); 2026 con->port_sink_caps = NULL; 2027 usb_power_delivery_unregister_capabilities(con->port_source_caps); 2028 con->port_source_caps = NULL; 2029 usb_power_delivery_unregister(con->pd); 2030 con->pd = NULL; 2031 typec_unregister_port(con->port); 2032 con->port = NULL; 2033 } 2034 2035 static u64 ucsi_get_supported_notifications(struct ucsi *ucsi) 2036 { 2037 u16 features = ucsi->cap.features; 2038 u64 ntfy = UCSI_ENABLE_NTFY_ALL; 2039 2040 if (!(features & UCSI_CAP_ALT_MODE_DETAILS)) 2041 ntfy &= ~UCSI_ENABLE_NTFY_CAM_CHANGE; 2042 2043 if (!(features & UCSI_CAP_PDO_DETAILS)) 2044 ntfy &= ~(UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE | 2045 UCSI_ENABLE_NTFY_CAP_CHANGE); 2046 2047 if (!(features & UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS)) 2048 ntfy &= ~UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE; 2049 2050 if (!(features & UCSI_CAP_PD_RESET)) 2051 ntfy &= ~UCSI_ENABLE_NTFY_PD_RESET_COMPLETE; 2052 2053 if (ucsi->version <= UCSI_VERSION_1_2) 2054 return ntfy; 2055 2056 ntfy |= UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE; 2057 2058 if (features & UCSI_CAP_GET_ATTENTION_VDO) 2059 ntfy |= UCSI_ENABLE_NTFY_ATTENTION; 2060 2061 if (features & UCSI_CAP_FW_UPDATE_REQUEST) 2062 ntfy |= UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ; 2063 2064 if (features & UCSI_CAP_SECURITY_REQUEST) 2065 ntfy |= UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER; 2066 2067 if (features & UCSI_CAP_SET_RETIMER_MODE) 2068 ntfy |= UCSI_ENABLE_NTFY_SET_RETIMER_MODE; 2069 2070 return ntfy; 2071 } 2072 2073 /** 2074 * ucsi_init - Initialize UCSI interface 2075 * @ucsi: UCSI to be initialized 2076 * 2077 * Registers all ports @ucsi has and enables all notification events. 2078 */ 2079 static int ucsi_init(struct ucsi *ucsi) 2080 { 2081 struct ucsi_connector *con, *connector; 2082 u64 command, ntfy; 2083 u32 cci; 2084 int ret; 2085 int i; 2086 2087 /* Reset the PPM */ 2088 ret = ucsi_reset_ppm(ucsi); 2089 if (ret) { 2090 dev_err(ucsi->dev, "failed to reset PPM!\n"); 2091 goto err; 2092 } 2093 2094 /* Enable basic notifications */ 2095 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR; 2096 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy; 2097 ret = ucsi_send_command(ucsi, command, NULL, 0); 2098 if (ret < 0) 2099 goto err_reset; 2100 2101 /* Get PPM capabilities */ 2102 command = UCSI_GET_CAPABILITY; 2103 ret = ucsi_send_command(ucsi, command, &ucsi->cap, 2104 BITS_TO_BYTES(UCSI_GET_CAPABILITY_SIZE)); 2105 if (ret < 0) 2106 goto err_reset; 2107 2108 if (!ucsi->cap.num_connectors) { 2109 ret = -EAGAIN; 2110 goto err_reset; 2111 } 2112 /* Check if reserved bit set. This is out of spec but happens in buggy FW */ 2113 if (ucsi->cap.num_connectors & 0x80) { 2114 dev_warn(ucsi->dev, "UCSI: Invalid num_connectors %d. Likely buggy FW\n", 2115 ucsi->cap.num_connectors); 2116 ucsi->cap.num_connectors &= 0x7f; // clear bit and carry on 2117 } 2118 2119 /* Allocate the connectors. Released in ucsi_unregister() */ 2120 connector = kzalloc_objs(*connector, ucsi->cap.num_connectors + 1); 2121 if (!connector) { 2122 ret = -ENOMEM; 2123 goto err_reset; 2124 } 2125 2126 for (i = 0; i < ucsi->cap.num_connectors; i++) 2127 lockdep_register_key(&connector[i].lock_key); 2128 2129 /* Register all connectors */ 2130 for (i = 0; i < ucsi->cap.num_connectors; i++) { 2131 connector[i].num = i + 1; 2132 ret = ucsi_register_port(ucsi, &connector[i]); 2133 if (ret) 2134 goto err_unregister; 2135 } 2136 2137 /* 2138 * Set ucsi->connector before enabling notifications. A connector 2139 * change event can be handled as soon as the notifications are 2140 * enabled below, and ucsi_connector_change() indexes into 2141 * ucsi->connector, so it must not be NULL at that point. 2142 */ 2143 ucsi->connector = connector; 2144 2145 /* Enable all supported notifications */ 2146 ntfy = ucsi_get_supported_notifications(ucsi); 2147 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy; 2148 ret = ucsi_send_command(ucsi, command, NULL, 0); 2149 if (ret < 0) 2150 goto err_unregister; 2151 2152 ucsi->ntfy = ntfy; 2153 2154 mutex_lock(&ucsi->ppm_lock); 2155 ret = ucsi->ops->read_cci(ucsi, &cci); 2156 mutex_unlock(&ucsi->ppm_lock); 2157 if (ret) 2158 return ret; 2159 if (UCSI_CCI_CONNECTOR(cci)) 2160 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci)); 2161 2162 return 0; 2163 2164 err_unregister: 2165 ucsi->connector = NULL; 2166 for (con = connector; con->port; con++) 2167 ucsi_unregister_port(con); 2168 for (i = 0; i < ucsi->cap.num_connectors; i++) 2169 lockdep_unregister_key(&connector[i].lock_key); 2170 2171 kfree(connector); 2172 err_reset: 2173 memset(&ucsi->cap, 0, sizeof(ucsi->cap)); 2174 ucsi_reset_ppm(ucsi); 2175 2176 /* 2177 * The only -EINVAL expected on this path is ucsi_read_error()'s: 2178 * the PPM rejected a standard command. Some PPMs do that, or 2179 * report zero connectors above, for a short while after boot 2180 * before they are ready. Report both as -EAGAIN so that 2181 * ucsi_init_work() can retry them, and only them. 2182 */ 2183 if (ret == -EINVAL) 2184 ret = -EAGAIN; 2185 err: 2186 return ret; 2187 } 2188 2189 static void ucsi_resume_work(struct work_struct *work) 2190 { 2191 struct ucsi *ucsi = container_of(to_delayed_work(work), struct ucsi, resume_work); 2192 struct ucsi_connector *con; 2193 u64 command; 2194 int ret; 2195 2196 /* Restore UCSI notification enable mask after system resume */ 2197 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy; 2198 ret = ucsi_send_command(ucsi, command, NULL, 0); 2199 if (ret < 0) { 2200 if (++ucsi->resume_retries < 5) { 2201 queue_delayed_work(system_dfl_long_wq, &ucsi->resume_work, 2202 msecs_to_jiffies(500)); 2203 return; 2204 } 2205 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret); 2206 return; 2207 } 2208 2209 for (con = ucsi->connector; con->port; con++) { 2210 mutex_lock(&con->lock); 2211 ucsi_partner_task(con, ucsi_check_connection, 1, 0); 2212 mutex_unlock(&con->lock); 2213 } 2214 } 2215 2216 int ucsi_suspend(struct ucsi *ucsi) 2217 { 2218 int i; 2219 2220 /* 2221 * Cancel pending work so it cannot access the firmware after the ACPI 2222 * EC is stopped for suspend; state is re-read on resume. 2223 */ 2224 cancel_delayed_work_sync(&ucsi->work); 2225 cancel_delayed_work_sync(&ucsi->resume_work); 2226 2227 if (!ucsi->connector) 2228 return 0; 2229 2230 for (i = 0; i < ucsi->cap.num_connectors; i++) 2231 cancel_work_sync(&ucsi->connector[i].work); 2232 2233 return 0; 2234 } 2235 EXPORT_SYMBOL_GPL(ucsi_suspend); 2236 2237 int ucsi_resume(struct ucsi *ucsi) 2238 { 2239 if (ucsi->connector) { 2240 ucsi->resume_retries = 0; 2241 queue_delayed_work(system_dfl_long_wq, &ucsi->resume_work, 0); 2242 } 2243 return 0; 2244 } 2245 EXPORT_SYMBOL_GPL(ucsi_resume); 2246 2247 static void ucsi_init_work(struct work_struct *work) 2248 { 2249 struct ucsi *ucsi = container_of(work, struct ucsi, work.work); 2250 int ret; 2251 2252 ret = ucsi_init(ucsi); 2253 if (!ret) { 2254 if (ucsi->work_count) 2255 dev_info(ucsi->dev, 2256 "PPM init succeeded after %u attempts\n", 2257 ucsi->work_count + 1); 2258 return; 2259 } 2260 2261 /* 2262 * -EPROBE_DEFER: the USB role switch is not registered yet. Wait 2263 * up to ten seconds for it. 2264 * 2265 * -EAGAIN: the PPM is not ready to answer commands correctly yet. 2266 * On some platforms that lasts for a short window during boot 2267 * (observed on Lenovo ThinkPad X1 Carbon Gen 14, where it affects 2268 * roughly half of all boots and a retry moments later succeeds). 2269 * One second is plenty for that. 2270 * 2271 * Anything else is not going to change with a retry, so report it 2272 * once and stop. 2273 */ 2274 if (ret == -EPROBE_DEFER || ret == -EAGAIN) { 2275 int limit = ret == -EPROBE_DEFER ? UCSI_ROLE_SWITCH_WAIT_COUNT : 2276 UCSI_PPM_READY_WAIT_COUNT; 2277 2278 if (ucsi->work_count++ < limit) { 2279 dev_dbg(ucsi->dev, "PPM init failed (%pe), retrying\n", 2280 ERR_PTR(ret)); 2281 queue_delayed_work(system_dfl_long_wq, &ucsi->work, 2282 UCSI_ROLE_SWITCH_INTERVAL); 2283 return; 2284 } 2285 dev_err(ucsi->dev, "PPM init failed, stop trying\n"); 2286 return; 2287 } 2288 2289 dev_err_probe(ucsi->dev, ret, "PPM init failed\n"); 2290 } 2291 2292 /** 2293 * ucsi_get_drvdata - Return private driver data pointer 2294 * @ucsi: UCSI interface 2295 */ 2296 void *ucsi_get_drvdata(struct ucsi *ucsi) 2297 { 2298 return ucsi->driver_data; 2299 } 2300 EXPORT_SYMBOL_GPL(ucsi_get_drvdata); 2301 2302 /** 2303 * ucsi_set_drvdata - Assign private driver data pointer 2304 * @ucsi: UCSI interface 2305 * @data: Private data pointer 2306 */ 2307 void ucsi_set_drvdata(struct ucsi *ucsi, void *data) 2308 { 2309 ucsi->driver_data = data; 2310 } 2311 EXPORT_SYMBOL_GPL(ucsi_set_drvdata); 2312 2313 /** 2314 * ucsi_con_mutex_lock - Acquire the connector mutex 2315 * @con: The connector interface to lock 2316 * 2317 * Returns true on success, false if the connector is disconnected 2318 */ 2319 bool ucsi_con_mutex_lock(struct ucsi_connector *con) 2320 { 2321 bool mutex_locked = false; 2322 bool connected = true; 2323 2324 while (connected && !mutex_locked) { 2325 mutex_locked = mutex_trylock(&con->lock) != 0; 2326 connected = UCSI_CONSTAT(con, CONNECTED); 2327 if (connected && !mutex_locked) 2328 msleep(20); 2329 } 2330 2331 connected = connected && con->partner; 2332 if (!connected && mutex_locked) 2333 mutex_unlock(&con->lock); 2334 2335 return connected; 2336 } 2337 2338 /** 2339 * ucsi_con_mutex_unlock - Release the connector mutex 2340 * @con: The connector interface to unlock 2341 */ 2342 void ucsi_con_mutex_unlock(struct ucsi_connector *con) 2343 { 2344 mutex_unlock(&con->lock); 2345 } 2346 2347 /** 2348 * ucsi_create - Allocate UCSI instance 2349 * @dev: Device interface to the PPM (Platform Policy Manager) 2350 * @ops: I/O routines 2351 */ 2352 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops) 2353 { 2354 struct ucsi *ucsi; 2355 2356 if (!ops || 2357 !ops->read_version || !ops->read_cci || !ops->poll_cci || 2358 !ops->read_message_in || !ops->sync_control || !ops->async_control) 2359 return ERR_PTR(-EINVAL); 2360 2361 ucsi = kzalloc_obj(*ucsi); 2362 if (!ucsi) 2363 return ERR_PTR(-ENOMEM); 2364 2365 INIT_DELAYED_WORK(&ucsi->resume_work, ucsi_resume_work); 2366 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work); 2367 mutex_init(&ucsi->ppm_lock); 2368 init_completion(&ucsi->complete); 2369 ucsi->dev = dev; 2370 ucsi->ops = ops; 2371 2372 return ucsi; 2373 } 2374 EXPORT_SYMBOL_GPL(ucsi_create); 2375 2376 /** 2377 * ucsi_destroy - Free UCSI instance 2378 * @ucsi: UCSI instance to be freed 2379 */ 2380 void ucsi_destroy(struct ucsi *ucsi) 2381 { 2382 ucsi_debugfs_unregister(ucsi); 2383 kfree(ucsi); 2384 } 2385 EXPORT_SYMBOL_GPL(ucsi_destroy); 2386 2387 /** 2388 * ucsi_register - Register UCSI interface 2389 * @ucsi: UCSI instance 2390 */ 2391 int ucsi_register(struct ucsi *ucsi) 2392 { 2393 int ret; 2394 2395 ret = ucsi->ops->read_version(ucsi, &ucsi->version); 2396 if (ret) 2397 return ret; 2398 2399 if (!ucsi->version) 2400 return -ENODEV; 2401 2402 /* 2403 * Version format is JJ.M.N (JJ = Major version, M = Minor version, 2404 * N = sub-minor version). 2405 */ 2406 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x", 2407 UCSI_BCD_GET_MAJOR(ucsi->version), 2408 UCSI_BCD_GET_MINOR(ucsi->version), 2409 UCSI_BCD_GET_SUBMINOR(ucsi->version)); 2410 2411 queue_delayed_work(system_dfl_long_wq, &ucsi->work, 0); 2412 2413 ucsi_debugfs_register(ucsi); 2414 return 0; 2415 } 2416 EXPORT_SYMBOL_GPL(ucsi_register); 2417 2418 /** 2419 * ucsi_unregister - Unregister UCSI interface 2420 * @ucsi: UCSI interface to be unregistered 2421 * 2422 * Unregister UCSI interface that was created with ucsi_register(). 2423 */ 2424 void ucsi_unregister(struct ucsi *ucsi) 2425 { 2426 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE; 2427 int i; 2428 2429 /* Make sure that we are not in the middle of driver initialization */ 2430 cancel_delayed_work_sync(&ucsi->work); 2431 cancel_delayed_work_sync(&ucsi->resume_work); 2432 2433 ucsi_debugfs_unregister(ucsi); 2434 2435 /* Disable notifications */ 2436 ucsi->ops->async_control(ucsi, cmd); 2437 2438 if (!ucsi->connector) 2439 return; 2440 2441 for (i = 0; i < ucsi->cap.num_connectors; i++) { 2442 cancel_work_sync(&ucsi->connector[i].work); 2443 ucsi_unregister_port(&ucsi->connector[i]); 2444 lockdep_unregister_key(&ucsi->connector[i].lock_key); 2445 } 2446 2447 kfree(ucsi->connector); 2448 } 2449 EXPORT_SYMBOL_GPL(ucsi_unregister); 2450 2451 static int __init ucsi_module_init(void) 2452 { 2453 ucsi_debugfs_init(); 2454 return 0; 2455 } 2456 module_init(ucsi_module_init); 2457 2458 static void __exit ucsi_module_exit(void) 2459 { 2460 ucsi_debugfs_exit(); 2461 } 2462 module_exit(ucsi_module_exit); 2463 2464 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>"); 2465 MODULE_LICENSE("GPL v2"); 2466 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver"); 2467