1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * USB Type-C Connector Class 4 * 5 * Copyright (C) 2017, Intel Corporation 6 * Author: Heikki Krogerus <[email protected]> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/mutex.h> 11 #include <linux/property.h> 12 #include <linux/slab.h> 13 #include <linux/usb/pd_vdo.h> 14 #include <linux/usb/typec_mux.h> 15 #include <linux/usb/typec_retimer.h> 16 #include <linux/usb.h> 17 18 #include "bus.h" 19 #include "class.h" 20 #include "pd.h" 21 22 static DEFINE_IDA(typec_index_ida); 23 24 const struct class typec_class = { 25 .name = "typec", 26 }; 27 28 /* ------------------------------------------------------------------------- */ 29 /* Common attributes */ 30 31 static const char * const typec_accessory_modes[] = { 32 [TYPEC_ACCESSORY_NONE] = "none", 33 [TYPEC_ACCESSORY_AUDIO] = "analog_audio", 34 [TYPEC_ACCESSORY_DEBUG] = "debug", 35 }; 36 37 /* Product types defined in USB PD Specification R3.0 V2.0 */ 38 static const char * const product_type_ufp[8] = { 39 [IDH_PTYPE_NOT_UFP] = "not_ufp", 40 [IDH_PTYPE_HUB] = "hub", 41 [IDH_PTYPE_PERIPH] = "peripheral", 42 [IDH_PTYPE_PSD] = "psd", 43 [IDH_PTYPE_AMA] = "ama", 44 }; 45 46 static const char * const product_type_dfp[8] = { 47 [IDH_PTYPE_NOT_DFP] = "not_dfp", 48 [IDH_PTYPE_DFP_HUB] = "hub", 49 [IDH_PTYPE_DFP_HOST] = "host", 50 [IDH_PTYPE_DFP_PB] = "power_brick", 51 }; 52 53 static const char * const product_type_cable[8] = { 54 [IDH_PTYPE_NOT_CABLE] = "not_cable", 55 [IDH_PTYPE_PCABLE] = "passive", 56 [IDH_PTYPE_ACABLE] = "active", 57 [IDH_PTYPE_VPD] = "vpd", 58 }; 59 60 static struct usb_pd_identity *get_pd_identity(struct device *dev) 61 { 62 if (is_typec_partner(dev)) { 63 struct typec_partner *partner = to_typec_partner(dev); 64 65 return partner->identity; 66 } else if (is_typec_cable(dev)) { 67 struct typec_cable *cable = to_typec_cable(dev); 68 69 return cable->identity; 70 } 71 return NULL; 72 } 73 74 static const char *get_pd_product_type(struct device *dev) 75 { 76 struct typec_port *port = to_typec_port(dev->parent); 77 struct usb_pd_identity *id = get_pd_identity(dev); 78 const char *ptype = NULL; 79 80 if (is_typec_partner(dev)) { 81 if (!id) 82 return NULL; 83 84 if (port->data_role == TYPEC_HOST) 85 ptype = product_type_ufp[PD_IDH_PTYPE(id->id_header)]; 86 else 87 ptype = product_type_dfp[PD_IDH_DFP_PTYPE(id->id_header)]; 88 } else if (is_typec_cable(dev)) { 89 if (id) 90 ptype = product_type_cable[PD_IDH_PTYPE(id->id_header)]; 91 else 92 ptype = to_typec_cable(dev)->active ? 93 product_type_cable[IDH_PTYPE_ACABLE] : 94 product_type_cable[IDH_PTYPE_PCABLE]; 95 } 96 97 return ptype; 98 } 99 100 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr, 101 char *buf) 102 { 103 struct usb_pd_identity *id = get_pd_identity(dev); 104 105 return sprintf(buf, "0x%08x\n", id->id_header); 106 } 107 static DEVICE_ATTR_RO(id_header); 108 109 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr, 110 char *buf) 111 { 112 struct usb_pd_identity *id = get_pd_identity(dev); 113 114 return sprintf(buf, "0x%08x\n", id->cert_stat); 115 } 116 static DEVICE_ATTR_RO(cert_stat); 117 118 static ssize_t product_show(struct device *dev, struct device_attribute *attr, 119 char *buf) 120 { 121 struct usb_pd_identity *id = get_pd_identity(dev); 122 123 return sprintf(buf, "0x%08x\n", id->product); 124 } 125 static DEVICE_ATTR_RO(product); 126 127 static ssize_t product_type_vdo1_show(struct device *dev, struct device_attribute *attr, 128 char *buf) 129 { 130 struct usb_pd_identity *id = get_pd_identity(dev); 131 132 return sysfs_emit(buf, "0x%08x\n", id->vdo[0]); 133 } 134 static DEVICE_ATTR_RO(product_type_vdo1); 135 136 static ssize_t product_type_vdo2_show(struct device *dev, struct device_attribute *attr, 137 char *buf) 138 { 139 struct usb_pd_identity *id = get_pd_identity(dev); 140 141 return sysfs_emit(buf, "0x%08x\n", id->vdo[1]); 142 } 143 static DEVICE_ATTR_RO(product_type_vdo2); 144 145 static ssize_t product_type_vdo3_show(struct device *dev, struct device_attribute *attr, 146 char *buf) 147 { 148 struct usb_pd_identity *id = get_pd_identity(dev); 149 150 return sysfs_emit(buf, "0x%08x\n", id->vdo[2]); 151 } 152 static DEVICE_ATTR_RO(product_type_vdo3); 153 154 static struct attribute *usb_pd_id_attrs[] = { 155 &dev_attr_id_header.attr, 156 &dev_attr_cert_stat.attr, 157 &dev_attr_product.attr, 158 &dev_attr_product_type_vdo1.attr, 159 &dev_attr_product_type_vdo2.attr, 160 &dev_attr_product_type_vdo3.attr, 161 NULL 162 }; 163 164 static const struct attribute_group usb_pd_id_group = { 165 .name = "identity", 166 .attrs = usb_pd_id_attrs, 167 }; 168 169 static const struct attribute_group *usb_pd_id_groups[] = { 170 &usb_pd_id_group, 171 NULL, 172 }; 173 174 static void typec_product_type_notify(struct device *dev) 175 { 176 char *envp[2] = { }; 177 const char *ptype; 178 179 ptype = get_pd_product_type(dev); 180 if (!ptype) 181 return; 182 183 sysfs_notify(&dev->kobj, NULL, "type"); 184 185 envp[0] = kasprintf(GFP_KERNEL, "PRODUCT_TYPE=%s", ptype); 186 if (!envp[0]) 187 return; 188 189 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); 190 kfree(envp[0]); 191 } 192 193 static void typec_report_identity(struct device *dev) 194 { 195 sysfs_notify(&dev->kobj, "identity", "id_header"); 196 sysfs_notify(&dev->kobj, "identity", "cert_stat"); 197 sysfs_notify(&dev->kobj, "identity", "product"); 198 sysfs_notify(&dev->kobj, "identity", "product_type_vdo1"); 199 sysfs_notify(&dev->kobj, "identity", "product_type_vdo2"); 200 sysfs_notify(&dev->kobj, "identity", "product_type_vdo3"); 201 typec_product_type_notify(dev); 202 } 203 204 static ssize_t 205 type_show(struct device *dev, struct device_attribute *attr, char *buf) 206 { 207 const char *ptype; 208 209 ptype = get_pd_product_type(dev); 210 if (!ptype) 211 return 0; 212 213 return sysfs_emit(buf, "%s\n", ptype); 214 } 215 static DEVICE_ATTR_RO(type); 216 217 static ssize_t usb_power_delivery_revision_show(struct device *dev, 218 struct device_attribute *attr, 219 char *buf); 220 static DEVICE_ATTR_RO(usb_power_delivery_revision); 221 222 static const char * const usb_modes[] = { 223 [USB_MODE_NONE] = "none", 224 [USB_MODE_USB2] = "usb2", 225 [USB_MODE_USB3] = "usb3", 226 [USB_MODE_USB4] = "usb4" 227 }; 228 229 /* ------------------------------------------------------------------------- */ 230 /* Alternate Modes */ 231 232 static int altmode_match(struct device *dev, const void *data) 233 { 234 struct typec_altmode *adev = to_typec_altmode(dev); 235 const struct typec_device_id *id = data; 236 237 if (!is_typec_altmode(dev)) 238 return 0; 239 240 return ((adev->svid == id->svid) && (adev->mode == id->mode)); 241 } 242 243 static void typec_altmode_set_partner(struct altmode *altmode) 244 { 245 struct typec_altmode *adev = &altmode->adev; 246 struct typec_device_id id = { adev->svid, adev->mode, }; 247 struct typec_port *port = typec_altmode2port(adev); 248 struct altmode *partner; 249 struct device *dev; 250 251 dev = device_find_child(&port->dev, &id, altmode_match); 252 if (!dev) 253 return; 254 255 /* Bind the port alt mode to the partner/plug alt mode. */ 256 partner = to_altmode(to_typec_altmode(dev)); 257 altmode->partner = partner; 258 259 /* Bind the partner/plug alt mode to the port alt mode. */ 260 if (is_typec_plug(adev->dev.parent)) { 261 struct typec_plug *plug = to_typec_plug(adev->dev.parent); 262 263 partner->plug[plug->index] = altmode; 264 } else { 265 partner->partner = altmode; 266 } 267 } 268 269 static void typec_altmode_put_partner(struct altmode *altmode) 270 { 271 struct altmode *partner = altmode->partner; 272 struct typec_altmode *adev; 273 struct typec_altmode *partner_adev; 274 275 if (!partner) 276 return; 277 278 adev = &altmode->adev; 279 partner_adev = &partner->adev; 280 281 if (is_typec_plug(adev->dev.parent)) { 282 struct typec_plug *plug = to_typec_plug(adev->dev.parent); 283 284 partner->plug[plug->index] = NULL; 285 } else { 286 partner->partner = NULL; 287 } 288 put_device(&partner_adev->dev); 289 } 290 291 /** 292 * typec_altmode_update_active - Report Enter/Exit mode 293 * @adev: Handle to the alternate mode 294 * @active: True when the mode has been entered 295 * 296 * If a partner or cable plug executes Enter/Exit Mode command successfully, the 297 * drivers use this routine to report the updated state of the mode. 298 */ 299 void typec_altmode_update_active(struct typec_altmode *adev, bool active) 300 { 301 char dir[6]; 302 303 if (adev->active == active) 304 return; 305 306 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) { 307 if (!active) 308 module_put(adev->dev.driver->owner); 309 else 310 WARN_ON(!try_module_get(adev->dev.driver->owner)); 311 } 312 313 adev->active = active; 314 snprintf(dir, sizeof(dir), "mode%d", adev->mode); 315 sysfs_notify(&adev->dev.kobj, dir, "active"); 316 sysfs_notify(&adev->dev.kobj, NULL, "active"); 317 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE); 318 } 319 EXPORT_SYMBOL_GPL(typec_altmode_update_active); 320 321 /** 322 * typec_altmode2port - Alternate Mode to USB Type-C port 323 * @alt: The Alternate Mode 324 * 325 * Returns handle to the port that a cable plug or partner with @alt is 326 * connected to. 327 */ 328 struct typec_port *typec_altmode2port(struct typec_altmode *alt) 329 { 330 if (is_typec_plug(alt->dev.parent)) 331 return to_typec_port(alt->dev.parent->parent->parent); 332 if (is_typec_partner(alt->dev.parent)) 333 return to_typec_port(alt->dev.parent->parent); 334 if (is_typec_port(alt->dev.parent)) 335 return to_typec_port(alt->dev.parent); 336 337 return NULL; 338 } 339 EXPORT_SYMBOL_GPL(typec_altmode2port); 340 341 static ssize_t 342 vdo_show(struct device *dev, struct device_attribute *attr, char *buf) 343 { 344 struct typec_altmode *alt = to_typec_altmode(dev); 345 346 return sprintf(buf, "0x%08x\n", alt->vdo); 347 } 348 static DEVICE_ATTR_RO(vdo); 349 350 static ssize_t 351 description_show(struct device *dev, struct device_attribute *attr, char *buf) 352 { 353 struct typec_altmode *alt = to_typec_altmode(dev); 354 355 return sprintf(buf, "%s\n", alt->desc ? alt->desc : ""); 356 } 357 static DEVICE_ATTR_RO(description); 358 359 static ssize_t 360 active_show(struct device *dev, struct device_attribute *attr, char *buf) 361 { 362 struct typec_altmode *alt = to_typec_altmode(dev); 363 364 return sprintf(buf, "%s\n", alt->active ? "yes" : "no"); 365 } 366 367 static ssize_t active_store(struct device *dev, struct device_attribute *attr, 368 const char *buf, size_t size) 369 { 370 struct typec_altmode *adev = to_typec_altmode(dev); 371 struct altmode *altmode = to_altmode(adev); 372 bool enter; 373 int ret; 374 375 ret = kstrtobool(buf, &enter); 376 if (ret) 377 return ret; 378 379 if (adev->active == enter) 380 return size; 381 382 if (is_typec_port(adev->dev.parent)) { 383 typec_altmode_update_active(adev, enter); 384 385 /* Make sure that the partner exits the mode before disabling */ 386 if (altmode->partner && !enter && altmode->partner->adev.active) 387 typec_altmode_exit(&altmode->partner->adev); 388 } else if (altmode->partner) { 389 if (enter && !altmode->partner->adev.active) { 390 dev_warn(dev, "port has the mode disabled\n"); 391 return -EPERM; 392 } 393 } 394 395 /* Note: If there is no driver, the mode will not be entered */ 396 if (adev->ops && adev->ops->activate) { 397 ret = adev->ops->activate(adev, enter); 398 if (ret) 399 return ret; 400 } 401 402 return size; 403 } 404 static DEVICE_ATTR_RW(active); 405 406 static ssize_t 407 supported_roles_show(struct device *dev, struct device_attribute *attr, 408 char *buf) 409 { 410 struct altmode *alt = to_altmode(to_typec_altmode(dev)); 411 ssize_t ret; 412 413 switch (alt->roles) { 414 case TYPEC_PORT_SRC: 415 ret = sprintf(buf, "source\n"); 416 break; 417 case TYPEC_PORT_SNK: 418 ret = sprintf(buf, "sink\n"); 419 break; 420 case TYPEC_PORT_DRP: 421 default: 422 ret = sprintf(buf, "source sink\n"); 423 break; 424 } 425 return ret; 426 } 427 static DEVICE_ATTR_RO(supported_roles); 428 429 static ssize_t 430 mode_show(struct device *dev, struct device_attribute *attr, char *buf) 431 { 432 struct typec_altmode *adev = to_typec_altmode(dev); 433 434 return sprintf(buf, "%u\n", adev->mode); 435 } 436 static DEVICE_ATTR_RO(mode); 437 438 static ssize_t 439 svid_show(struct device *dev, struct device_attribute *attr, char *buf) 440 { 441 struct typec_altmode *adev = to_typec_altmode(dev); 442 443 return sprintf(buf, "%04x\n", adev->svid); 444 } 445 static DEVICE_ATTR_RO(svid); 446 447 static struct attribute *typec_altmode_attrs[] = { 448 &dev_attr_active.attr, 449 &dev_attr_mode.attr, 450 &dev_attr_svid.attr, 451 &dev_attr_vdo.attr, 452 NULL 453 }; 454 455 static umode_t typec_altmode_attr_is_visible(struct kobject *kobj, 456 struct attribute *attr, int n) 457 { 458 struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj)); 459 460 if (attr == &dev_attr_active.attr) 461 if (!adev->ops || !adev->ops->activate) 462 return 0444; 463 464 return attr->mode; 465 } 466 467 static const struct attribute_group typec_altmode_group = { 468 .is_visible = typec_altmode_attr_is_visible, 469 .attrs = typec_altmode_attrs, 470 }; 471 472 static const struct attribute_group *typec_altmode_groups[] = { 473 &typec_altmode_group, 474 NULL 475 }; 476 477 /** 478 * typec_altmode_set_ops - Set ops for altmode 479 * @adev: Handle to the alternate mode 480 * @ops: Ops for the alternate mode 481 * 482 * After setting ops, attribute visiblity needs to be refreshed if the alternate 483 * mode can be activated. 484 */ 485 void typec_altmode_set_ops(struct typec_altmode *adev, 486 const struct typec_altmode_ops *ops) 487 { 488 adev->ops = ops; 489 sysfs_update_group(&adev->dev.kobj, &typec_altmode_group); 490 } 491 EXPORT_SYMBOL_GPL(typec_altmode_set_ops); 492 493 static int altmode_id_get(struct device *dev) 494 { 495 struct ida *ids; 496 497 if (is_typec_partner(dev)) 498 ids = &to_typec_partner(dev)->mode_ids; 499 else if (is_typec_plug(dev)) 500 ids = &to_typec_plug(dev)->mode_ids; 501 else 502 ids = &to_typec_port(dev)->mode_ids; 503 504 return ida_alloc(ids, GFP_KERNEL); 505 } 506 507 static void altmode_id_remove(struct device *dev, int id) 508 { 509 struct ida *ids; 510 511 if (is_typec_partner(dev)) 512 ids = &to_typec_partner(dev)->mode_ids; 513 else if (is_typec_plug(dev)) 514 ids = &to_typec_plug(dev)->mode_ids; 515 else 516 ids = &to_typec_port(dev)->mode_ids; 517 518 ida_free(ids, id); 519 } 520 521 static void typec_altmode_release(struct device *dev) 522 { 523 struct altmode *alt = to_altmode(to_typec_altmode(dev)); 524 525 if (!is_typec_port(dev->parent)) 526 typec_altmode_put_partner(alt); 527 528 altmode_id_remove(alt->adev.dev.parent, alt->id); 529 put_device(alt->adev.dev.parent); 530 kfree(alt); 531 } 532 533 const struct device_type typec_altmode_dev_type = { 534 .name = "typec_alternate_mode", 535 .groups = typec_altmode_groups, 536 .release = typec_altmode_release, 537 }; 538 539 static struct typec_altmode * 540 typec_register_altmode(struct device *parent, 541 const struct typec_altmode_desc *desc) 542 { 543 unsigned int id = altmode_id_get(parent); 544 bool is_port = is_typec_port(parent); 545 struct altmode *alt; 546 int ret; 547 548 alt = kzalloc(sizeof(*alt), GFP_KERNEL); 549 if (!alt) { 550 altmode_id_remove(parent, id); 551 return ERR_PTR(-ENOMEM); 552 } 553 554 alt->adev.svid = desc->svid; 555 alt->adev.mode = desc->mode; 556 alt->adev.vdo = desc->vdo; 557 alt->roles = desc->roles; 558 alt->id = id; 559 560 alt->attrs[0] = &dev_attr_vdo.attr; 561 alt->attrs[1] = &dev_attr_description.attr; 562 alt->attrs[2] = &dev_attr_active.attr; 563 564 if (is_port) { 565 alt->attrs[3] = &dev_attr_supported_roles.attr; 566 alt->adev.active = true; /* Enabled by default */ 567 } 568 569 sprintf(alt->group_name, "mode%d", desc->mode); 570 alt->group.name = alt->group_name; 571 alt->group.attrs = alt->attrs; 572 alt->groups[0] = &alt->group; 573 574 alt->adev.dev.parent = parent; 575 alt->adev.dev.groups = alt->groups; 576 alt->adev.dev.type = &typec_altmode_dev_type; 577 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id); 578 579 get_device(alt->adev.dev.parent); 580 581 /* Link partners and plugs with the ports */ 582 if (!is_port) 583 typec_altmode_set_partner(alt); 584 585 /* The partners are bind to drivers */ 586 if (is_typec_partner(parent)) 587 alt->adev.dev.bus = &typec_bus; 588 589 /* Plug alt modes need a class to generate udev events. */ 590 if (is_typec_plug(parent)) 591 alt->adev.dev.class = &typec_class; 592 593 ret = device_register(&alt->adev.dev); 594 if (ret) { 595 dev_err(parent, "failed to register alternate mode (%d)\n", 596 ret); 597 put_device(&alt->adev.dev); 598 return ERR_PTR(ret); 599 } 600 601 return &alt->adev; 602 } 603 604 /** 605 * typec_unregister_altmode - Unregister Alternate Mode 606 * @adev: The alternate mode to be unregistered 607 * 608 * Unregister device created with typec_partner_register_altmode(), 609 * typec_plug_register_altmode() or typec_port_register_altmode(). 610 */ 611 void typec_unregister_altmode(struct typec_altmode *adev) 612 { 613 if (IS_ERR_OR_NULL(adev)) 614 return; 615 typec_retimer_put(to_altmode(adev)->retimer); 616 typec_mux_put(to_altmode(adev)->mux); 617 device_unregister(&adev->dev); 618 } 619 EXPORT_SYMBOL_GPL(typec_unregister_altmode); 620 621 /* ------------------------------------------------------------------------- */ 622 /* Type-C Partners */ 623 624 /** 625 * typec_partner_set_usb_mode - Assign active USB Mode for the partner 626 * @partner: USB Type-C partner 627 * @mode: USB Mode (USB2, USB3 or USB4) 628 * 629 * The port drivers can use this function to assign the active USB Mode to 630 * @partner. The USB Mode can change for example due to Data Reset. 631 */ 632 void typec_partner_set_usb_mode(struct typec_partner *partner, enum usb_mode mode) 633 { 634 if (!partner || partner->usb_mode == mode) 635 return; 636 637 partner->usb_capability |= BIT(mode - 1); 638 partner->usb_mode = mode; 639 sysfs_notify(&partner->dev.kobj, NULL, "usb_mode"); 640 } 641 EXPORT_SYMBOL_GPL(typec_partner_set_usb_mode); 642 643 static ssize_t 644 usb_mode_show(struct device *dev, struct device_attribute *attr, char *buf) 645 { 646 struct typec_partner *partner = to_typec_partner(dev); 647 int len = 0; 648 int i; 649 650 for (i = USB_MODE_USB2; i < USB_MODE_USB4 + 1; i++) { 651 if (!(BIT(i - 1) & partner->usb_capability)) 652 continue; 653 654 if (i == partner->usb_mode) 655 len += sysfs_emit_at(buf, len, "[%s] ", usb_modes[i]); 656 else 657 len += sysfs_emit_at(buf, len, "%s ", usb_modes[i]); 658 } 659 660 sysfs_emit_at(buf, len - 1, "\n"); 661 662 return len; 663 } 664 665 static ssize_t usb_mode_store(struct device *dev, struct device_attribute *attr, 666 const char *buf, size_t size) 667 { 668 struct typec_partner *partner = to_typec_partner(dev); 669 struct typec_port *port = to_typec_port(dev->parent); 670 int mode; 671 int ret; 672 673 if (!port->ops || !port->ops->enter_usb_mode) 674 return -EOPNOTSUPP; 675 676 mode = sysfs_match_string(usb_modes, buf); 677 if (mode < 0) 678 return mode; 679 680 if (mode == partner->usb_mode) 681 return size; 682 683 ret = port->ops->enter_usb_mode(port, mode); 684 if (ret) 685 return ret; 686 687 typec_partner_set_usb_mode(partner, mode); 688 689 return size; 690 } 691 static DEVICE_ATTR_RW(usb_mode); 692 693 static ssize_t accessory_mode_show(struct device *dev, 694 struct device_attribute *attr, 695 char *buf) 696 { 697 struct typec_partner *p = to_typec_partner(dev); 698 699 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]); 700 } 701 static DEVICE_ATTR_RO(accessory_mode); 702 703 static ssize_t supports_usb_power_delivery_show(struct device *dev, 704 struct device_attribute *attr, 705 char *buf) 706 { 707 struct typec_partner *p = to_typec_partner(dev); 708 709 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no"); 710 } 711 static DEVICE_ATTR_RO(supports_usb_power_delivery); 712 713 static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr, 714 char *buf) 715 { 716 struct typec_partner *partner; 717 struct typec_plug *plug; 718 int num_altmodes; 719 720 if (is_typec_partner(dev)) { 721 partner = to_typec_partner(dev); 722 num_altmodes = partner->num_altmodes; 723 } else if (is_typec_plug(dev)) { 724 plug = to_typec_plug(dev); 725 num_altmodes = plug->num_altmodes; 726 } else { 727 return 0; 728 } 729 730 return sysfs_emit(buf, "%d\n", num_altmodes); 731 } 732 static DEVICE_ATTR_RO(number_of_alternate_modes); 733 734 static struct attribute *typec_partner_attrs[] = { 735 &dev_attr_accessory_mode.attr, 736 &dev_attr_supports_usb_power_delivery.attr, 737 &dev_attr_number_of_alternate_modes.attr, 738 &dev_attr_type.attr, 739 &dev_attr_usb_mode.attr, 740 &dev_attr_usb_power_delivery_revision.attr, 741 NULL 742 }; 743 744 static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n) 745 { 746 struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj)); 747 struct typec_port *port = to_typec_port(partner->dev.parent); 748 749 if (attr == &dev_attr_usb_mode.attr) { 750 if (!partner->usb_capability) 751 return 0; 752 if (!port->ops || !port->ops->enter_usb_mode) 753 return 0444; 754 } 755 756 if (attr == &dev_attr_number_of_alternate_modes.attr) { 757 if (partner->num_altmodes < 0) 758 return 0; 759 } 760 761 if (attr == &dev_attr_type.attr) 762 if (!get_pd_product_type(kobj_to_dev(kobj))) 763 return 0; 764 765 return attr->mode; 766 } 767 768 static const struct attribute_group typec_partner_group = { 769 .is_visible = typec_partner_attr_is_visible, 770 .attrs = typec_partner_attrs 771 }; 772 773 static const struct attribute_group *typec_partner_groups[] = { 774 &typec_partner_group, 775 NULL 776 }; 777 778 static void typec_partner_release(struct device *dev) 779 { 780 struct typec_partner *partner = to_typec_partner(dev); 781 782 ida_destroy(&partner->mode_ids); 783 kfree(partner); 784 } 785 786 const struct device_type typec_partner_dev_type = { 787 .name = "typec_partner", 788 .groups = typec_partner_groups, 789 .release = typec_partner_release, 790 }; 791 792 static void typec_partner_link_device(struct typec_partner *partner, struct device *dev) 793 { 794 int ret; 795 796 ret = sysfs_create_link(&dev->kobj, &partner->dev.kobj, "typec"); 797 if (ret) 798 return; 799 800 ret = sysfs_create_link(&partner->dev.kobj, &dev->kobj, dev_name(dev)); 801 if (ret) { 802 sysfs_remove_link(&dev->kobj, "typec"); 803 return; 804 } 805 806 if (partner->attach) 807 partner->attach(partner, dev); 808 } 809 810 static void typec_partner_unlink_device(struct typec_partner *partner, struct device *dev) 811 { 812 sysfs_remove_link(&partner->dev.kobj, dev_name(dev)); 813 sysfs_remove_link(&dev->kobj, "typec"); 814 815 if (partner->deattach) 816 partner->deattach(partner, dev); 817 } 818 819 /** 820 * typec_partner_set_identity - Report result from Discover Identity command 821 * @partner: The partner updated identity values 822 * 823 * This routine is used to report that the result of Discover Identity USB power 824 * delivery command has become available. 825 */ 826 int typec_partner_set_identity(struct typec_partner *partner) 827 { 828 u8 usb_capability = partner->usb_capability; 829 struct device *dev = &partner->dev; 830 struct usb_pd_identity *id; 831 832 id = get_pd_identity(dev); 833 if (!id) 834 return -EINVAL; 835 836 if (to_typec_port(dev->parent)->data_role == TYPEC_HOST) { 837 u32 devcap = PD_VDO_UFP_DEVCAP(id->vdo[0]); 838 839 if (devcap & (DEV_USB2_CAPABLE | DEV_USB2_BILLBOARD)) 840 usb_capability |= USB_CAPABILITY_USB2; 841 if (devcap & DEV_USB3_CAPABLE) 842 usb_capability |= USB_CAPABILITY_USB3; 843 if (devcap & DEV_USB4_CAPABLE) 844 usb_capability |= USB_CAPABILITY_USB4; 845 } else { 846 usb_capability = PD_VDO_DFP_HOSTCAP(id->vdo[0]); 847 } 848 849 if (partner->usb_capability != usb_capability) { 850 partner->usb_capability = usb_capability; 851 sysfs_notify(&dev->kobj, NULL, "usb_mode"); 852 } 853 854 typec_report_identity(dev); 855 return 0; 856 } 857 EXPORT_SYMBOL_GPL(typec_partner_set_identity); 858 859 /** 860 * typec_partner_set_pd_revision - Set the PD revision supported by the partner 861 * @partner: The partner to be updated. 862 * @pd_revision: USB Power Delivery Specification Revision supported by partner 863 * 864 * This routine is used to report that the PD revision of the port partner has 865 * become available. 866 */ 867 void typec_partner_set_pd_revision(struct typec_partner *partner, u16 pd_revision) 868 { 869 if (partner->pd_revision == pd_revision) 870 return; 871 872 partner->pd_revision = pd_revision; 873 sysfs_notify(&partner->dev.kobj, NULL, "usb_power_delivery_revision"); 874 if (pd_revision != 0 && !partner->usb_pd) { 875 partner->usb_pd = 1; 876 sysfs_notify(&partner->dev.kobj, NULL, 877 "supports_usb_power_delivery"); 878 } 879 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE); 880 } 881 EXPORT_SYMBOL_GPL(typec_partner_set_pd_revision); 882 883 /** 884 * typec_partner_set_usb_power_delivery - Declare USB Power Delivery Contract. 885 * @partner: The partner device. 886 * @pd: The USB PD instance. 887 * 888 * This routine can be used to declare USB Power Delivery Contract with @partner 889 * by linking @partner to @pd which contains the objects that were used during the 890 * negotiation of the contract. 891 * 892 * If @pd is NULL, the link is removed and the contract with @partner has ended. 893 */ 894 int typec_partner_set_usb_power_delivery(struct typec_partner *partner, 895 struct usb_power_delivery *pd) 896 { 897 int ret; 898 899 if (IS_ERR_OR_NULL(partner) || partner->pd == pd) 900 return 0; 901 902 if (pd) { 903 ret = usb_power_delivery_link_device(pd, &partner->dev); 904 if (ret) 905 return ret; 906 } else { 907 usb_power_delivery_unlink_device(partner->pd, &partner->dev); 908 } 909 910 partner->pd = pd; 911 912 return 0; 913 } 914 EXPORT_SYMBOL_GPL(typec_partner_set_usb_power_delivery); 915 916 /** 917 * typec_partner_set_num_altmodes - Set the number of available partner altmodes 918 * @partner: The partner to be updated. 919 * @num_altmodes: The number of altmodes we want to specify as available. 920 * 921 * This routine is used to report the number of alternate modes supported by the 922 * partner. This value is *not* enforced in alternate mode registration routines. 923 * 924 * @partner.num_altmodes is set to -1 on partner registration, denoting that 925 * a valid value has not been set for it yet. 926 * 927 * Returns 0 on success or negative error number on failure. 928 */ 929 int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes) 930 { 931 int ret; 932 933 if (num_altmodes < 0) 934 return -EINVAL; 935 936 partner->num_altmodes = num_altmodes; 937 ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group); 938 if (ret < 0) 939 return ret; 940 941 sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes"); 942 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE); 943 944 return 0; 945 } 946 EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes); 947 948 /** 949 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode 950 * @partner: USB Type-C Partner that supports the alternate mode 951 * @desc: Description of the alternate mode 952 * 953 * This routine is used to register each alternate mode individually that 954 * @partner has listed in response to Discover SVIDs command. The modes for a 955 * SVID listed in response to Discover Modes command need to be listed in an 956 * array in @desc. 957 * 958 * Returns handle to the alternate mode on success or ERR_PTR on failure. 959 */ 960 struct typec_altmode * 961 typec_partner_register_altmode(struct typec_partner *partner, 962 const struct typec_altmode_desc *desc) 963 { 964 return typec_register_altmode(&partner->dev, desc); 965 } 966 EXPORT_SYMBOL_GPL(typec_partner_register_altmode); 967 968 /** 969 * typec_partner_set_svdm_version - Set negotiated Structured VDM (SVDM) Version 970 * @partner: USB Type-C Partner that supports SVDM 971 * @svdm_version: Negotiated SVDM Version 972 * 973 * This routine is used to save the negotiated SVDM Version. 974 */ 975 void typec_partner_set_svdm_version(struct typec_partner *partner, 976 enum usb_pd_svdm_ver svdm_version) 977 { 978 partner->svdm_version = svdm_version; 979 } 980 EXPORT_SYMBOL_GPL(typec_partner_set_svdm_version); 981 982 /** 983 * typec_partner_usb_power_delivery_register - Register Type-C partner USB Power Delivery Support 984 * @partner: Type-C partner device. 985 * @desc: Description of the USB PD contract. 986 * 987 * This routine is a wrapper around usb_power_delivery_register(). It registers 988 * USB Power Delivery Capabilities for a Type-C partner device. Specifically, 989 * it sets the Type-C partner device as a parent for the resulting USB Power Delivery object. 990 * 991 * Returns handle to struct usb_power_delivery or ERR_PTR. 992 */ 993 struct usb_power_delivery * 994 typec_partner_usb_power_delivery_register(struct typec_partner *partner, 995 struct usb_power_delivery_desc *desc) 996 { 997 return usb_power_delivery_register(&partner->dev, desc); 998 } 999 EXPORT_SYMBOL_GPL(typec_partner_usb_power_delivery_register); 1000 1001 /** 1002 * typec_register_partner - Register a USB Type-C Partner 1003 * @port: The USB Type-C Port the partner is connected to 1004 * @desc: Description of the partner 1005 * 1006 * Registers a device for USB Type-C Partner described in @desc. 1007 * 1008 * Returns handle to the partner on success or ERR_PTR on failure. 1009 */ 1010 struct typec_partner *typec_register_partner(struct typec_port *port, 1011 struct typec_partner_desc *desc) 1012 { 1013 struct typec_partner *partner; 1014 int ret; 1015 1016 partner = kzalloc(sizeof(*partner), GFP_KERNEL); 1017 if (!partner) 1018 return ERR_PTR(-ENOMEM); 1019 1020 ida_init(&partner->mode_ids); 1021 partner->usb_pd = desc->usb_pd; 1022 partner->accessory = desc->accessory; 1023 partner->num_altmodes = -1; 1024 partner->usb_capability = desc->usb_capability; 1025 partner->pd_revision = desc->pd_revision; 1026 partner->svdm_version = port->cap->svdm_version; 1027 partner->attach = desc->attach; 1028 partner->deattach = desc->deattach; 1029 1030 if (desc->identity) { 1031 /* 1032 * Creating directory for the identity only if the driver is 1033 * able to provide data to it. 1034 */ 1035 partner->dev.groups = usb_pd_id_groups; 1036 partner->identity = desc->identity; 1037 } 1038 1039 partner->dev.class = &typec_class; 1040 partner->dev.parent = &port->dev; 1041 partner->dev.type = &typec_partner_dev_type; 1042 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev)); 1043 1044 if (port->usb2_dev) { 1045 partner->usb_capability |= USB_CAPABILITY_USB2; 1046 partner->usb_mode = USB_MODE_USB2; 1047 } 1048 if (port->usb3_dev) { 1049 partner->usb_capability |= USB_CAPABILITY_USB2 | USB_CAPABILITY_USB3; 1050 partner->usb_mode = USB_MODE_USB3; 1051 } 1052 1053 ret = device_register(&partner->dev); 1054 if (ret) { 1055 dev_err(&port->dev, "failed to register partner (%d)\n", ret); 1056 put_device(&partner->dev); 1057 return ERR_PTR(ret); 1058 } 1059 1060 if (port->usb2_dev) 1061 typec_partner_link_device(partner, port->usb2_dev); 1062 if (port->usb3_dev) 1063 typec_partner_link_device(partner, port->usb3_dev); 1064 1065 return partner; 1066 } 1067 EXPORT_SYMBOL_GPL(typec_register_partner); 1068 1069 /** 1070 * typec_unregister_partner - Unregister a USB Type-C Partner 1071 * @partner: The partner to be unregistered 1072 * 1073 * Unregister device created with typec_register_partner(). 1074 */ 1075 void typec_unregister_partner(struct typec_partner *partner) 1076 { 1077 struct typec_port *port; 1078 1079 if (IS_ERR_OR_NULL(partner)) 1080 return; 1081 1082 port = to_typec_port(partner->dev.parent); 1083 1084 if (port->usb2_dev) 1085 typec_partner_unlink_device(partner, port->usb2_dev); 1086 if (port->usb3_dev) 1087 typec_partner_unlink_device(partner, port->usb3_dev); 1088 1089 device_unregister(&partner->dev); 1090 } 1091 EXPORT_SYMBOL_GPL(typec_unregister_partner); 1092 1093 /* ------------------------------------------------------------------------- */ 1094 /* Type-C Cable Plugs */ 1095 1096 static void typec_plug_release(struct device *dev) 1097 { 1098 struct typec_plug *plug = to_typec_plug(dev); 1099 1100 ida_destroy(&plug->mode_ids); 1101 kfree(plug); 1102 } 1103 1104 static struct attribute *typec_plug_attrs[] = { 1105 &dev_attr_number_of_alternate_modes.attr, 1106 NULL 1107 }; 1108 1109 static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n) 1110 { 1111 struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj)); 1112 1113 if (attr == &dev_attr_number_of_alternate_modes.attr) { 1114 if (plug->num_altmodes < 0) 1115 return 0; 1116 } 1117 1118 return attr->mode; 1119 } 1120 1121 static const struct attribute_group typec_plug_group = { 1122 .is_visible = typec_plug_attr_is_visible, 1123 .attrs = typec_plug_attrs 1124 }; 1125 1126 static const struct attribute_group *typec_plug_groups[] = { 1127 &typec_plug_group, 1128 NULL 1129 }; 1130 1131 const struct device_type typec_plug_dev_type = { 1132 .name = "typec_plug", 1133 .groups = typec_plug_groups, 1134 .release = typec_plug_release, 1135 }; 1136 1137 /** 1138 * typec_plug_set_num_altmodes - Set the number of available plug altmodes 1139 * @plug: The plug to be updated. 1140 * @num_altmodes: The number of altmodes we want to specify as available. 1141 * 1142 * This routine is used to report the number of alternate modes supported by the 1143 * plug. This value is *not* enforced in alternate mode registration routines. 1144 * 1145 * @plug.num_altmodes is set to -1 on plug registration, denoting that 1146 * a valid value has not been set for it yet. 1147 * 1148 * Returns 0 on success or negative error number on failure. 1149 */ 1150 int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes) 1151 { 1152 int ret; 1153 1154 if (num_altmodes < 0) 1155 return -EINVAL; 1156 1157 plug->num_altmodes = num_altmodes; 1158 ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group); 1159 if (ret < 0) 1160 return ret; 1161 1162 sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes"); 1163 kobject_uevent(&plug->dev.kobj, KOBJ_CHANGE); 1164 1165 return 0; 1166 } 1167 EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes); 1168 1169 /** 1170 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode 1171 * @plug: USB Type-C Cable Plug that supports the alternate mode 1172 * @desc: Description of the alternate mode 1173 * 1174 * This routine is used to register each alternate mode individually that @plug 1175 * has listed in response to Discover SVIDs command. The modes for a SVID that 1176 * the plug lists in response to Discover Modes command need to be listed in an 1177 * array in @desc. 1178 * 1179 * Returns handle to the alternate mode on success or ERR_PTR on failure. 1180 */ 1181 struct typec_altmode * 1182 typec_plug_register_altmode(struct typec_plug *plug, 1183 const struct typec_altmode_desc *desc) 1184 { 1185 return typec_register_altmode(&plug->dev, desc); 1186 } 1187 EXPORT_SYMBOL_GPL(typec_plug_register_altmode); 1188 1189 /** 1190 * typec_register_plug - Register a USB Type-C Cable Plug 1191 * @cable: USB Type-C Cable with the plug 1192 * @desc: Description of the cable plug 1193 * 1194 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C 1195 * Cable Plug represents a plug with electronics in it that can response to USB 1196 * Power Delivery SOP Prime or SOP Double Prime packages. 1197 * 1198 * Returns handle to the cable plug on success or ERR_PTR on failure. 1199 */ 1200 struct typec_plug *typec_register_plug(struct typec_cable *cable, 1201 struct typec_plug_desc *desc) 1202 { 1203 struct typec_plug *plug; 1204 char name[8]; 1205 int ret; 1206 1207 plug = kzalloc(sizeof(*plug), GFP_KERNEL); 1208 if (!plug) 1209 return ERR_PTR(-ENOMEM); 1210 1211 sprintf(name, "plug%d", desc->index); 1212 1213 ida_init(&plug->mode_ids); 1214 plug->num_altmodes = -1; 1215 plug->index = desc->index; 1216 plug->dev.class = &typec_class; 1217 plug->dev.parent = &cable->dev; 1218 plug->dev.type = &typec_plug_dev_type; 1219 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name); 1220 1221 ret = device_register(&plug->dev); 1222 if (ret) { 1223 dev_err(&cable->dev, "failed to register plug (%d)\n", ret); 1224 put_device(&plug->dev); 1225 return ERR_PTR(ret); 1226 } 1227 1228 return plug; 1229 } 1230 EXPORT_SYMBOL_GPL(typec_register_plug); 1231 1232 /** 1233 * typec_unregister_plug - Unregister a USB Type-C Cable Plug 1234 * @plug: The cable plug to be unregistered 1235 * 1236 * Unregister device created with typec_register_plug(). 1237 */ 1238 void typec_unregister_plug(struct typec_plug *plug) 1239 { 1240 if (!IS_ERR_OR_NULL(plug)) 1241 device_unregister(&plug->dev); 1242 } 1243 EXPORT_SYMBOL_GPL(typec_unregister_plug); 1244 1245 /* Type-C Cables */ 1246 1247 static const char * const typec_plug_types[] = { 1248 [USB_PLUG_NONE] = "unknown", 1249 [USB_PLUG_TYPE_A] = "type-a", 1250 [USB_PLUG_TYPE_B] = "type-b", 1251 [USB_PLUG_TYPE_C] = "type-c", 1252 [USB_PLUG_CAPTIVE] = "captive", 1253 }; 1254 1255 static ssize_t plug_type_show(struct device *dev, 1256 struct device_attribute *attr, char *buf) 1257 { 1258 struct typec_cable *cable = to_typec_cable(dev); 1259 1260 return sprintf(buf, "%s\n", typec_plug_types[cable->type]); 1261 } 1262 static DEVICE_ATTR_RO(plug_type); 1263 1264 static struct attribute *typec_cable_attrs[] = { 1265 &dev_attr_type.attr, 1266 &dev_attr_plug_type.attr, 1267 &dev_attr_usb_power_delivery_revision.attr, 1268 NULL 1269 }; 1270 ATTRIBUTE_GROUPS(typec_cable); 1271 1272 static void typec_cable_release(struct device *dev) 1273 { 1274 struct typec_cable *cable = to_typec_cable(dev); 1275 1276 kfree(cable); 1277 } 1278 1279 const struct device_type typec_cable_dev_type = { 1280 .name = "typec_cable", 1281 .groups = typec_cable_groups, 1282 .release = typec_cable_release, 1283 }; 1284 1285 /** 1286 * typec_cable_get - Get a reference to the USB Type-C cable 1287 * @port: The USB Type-C Port the cable is connected to 1288 * 1289 * The caller must decrement the reference count with typec_cable_put() after 1290 * use. 1291 */ 1292 struct typec_cable *typec_cable_get(struct typec_port *port) 1293 { 1294 struct device *dev; 1295 1296 dev = device_find_child(&port->dev, &typec_cable_dev_type, 1297 device_match_type); 1298 if (!dev) 1299 return NULL; 1300 1301 return to_typec_cable(dev); 1302 } 1303 EXPORT_SYMBOL_GPL(typec_cable_get); 1304 1305 /** 1306 * typec_cable_put - Decrement the reference count on USB Type-C cable 1307 * @cable: The USB Type-C cable 1308 */ 1309 void typec_cable_put(struct typec_cable *cable) 1310 { 1311 put_device(&cable->dev); 1312 } 1313 EXPORT_SYMBOL_GPL(typec_cable_put); 1314 1315 /** 1316 * typec_cable_is_active - Check is the USB Type-C cable active or passive 1317 * @cable: The USB Type-C Cable 1318 * 1319 * Return 1 if the cable is active or 0 if it's passive. 1320 */ 1321 int typec_cable_is_active(struct typec_cable *cable) 1322 { 1323 return cable->active; 1324 } 1325 EXPORT_SYMBOL_GPL(typec_cable_is_active); 1326 1327 /** 1328 * typec_cable_set_identity - Report result from Discover Identity command 1329 * @cable: The cable updated identity values 1330 * 1331 * This routine is used to report that the result of Discover Identity USB power 1332 * delivery command has become available. 1333 */ 1334 int typec_cable_set_identity(struct typec_cable *cable) 1335 { 1336 if (!cable->identity) 1337 return -EINVAL; 1338 1339 typec_report_identity(&cable->dev); 1340 return 0; 1341 } 1342 EXPORT_SYMBOL_GPL(typec_cable_set_identity); 1343 1344 /** 1345 * typec_register_cable - Register a USB Type-C Cable 1346 * @port: The USB Type-C Port the cable is connected to 1347 * @desc: Description of the cable 1348 * 1349 * Registers a device for USB Type-C Cable described in @desc. The cable will be 1350 * parent for the optional cable plug devises. 1351 * 1352 * Returns handle to the cable on success or ERR_PTR on failure. 1353 */ 1354 struct typec_cable *typec_register_cable(struct typec_port *port, 1355 struct typec_cable_desc *desc) 1356 { 1357 struct typec_cable *cable; 1358 int ret; 1359 1360 cable = kzalloc(sizeof(*cable), GFP_KERNEL); 1361 if (!cable) 1362 return ERR_PTR(-ENOMEM); 1363 1364 cable->type = desc->type; 1365 cable->active = desc->active; 1366 cable->pd_revision = desc->pd_revision; 1367 1368 if (desc->identity) { 1369 /* 1370 * Creating directory for the identity only if the driver is 1371 * able to provide data to it. 1372 */ 1373 cable->dev.groups = usb_pd_id_groups; 1374 cable->identity = desc->identity; 1375 } 1376 1377 cable->dev.class = &typec_class; 1378 cable->dev.parent = &port->dev; 1379 cable->dev.type = &typec_cable_dev_type; 1380 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev)); 1381 1382 ret = device_register(&cable->dev); 1383 if (ret) { 1384 dev_err(&port->dev, "failed to register cable (%d)\n", ret); 1385 put_device(&cable->dev); 1386 return ERR_PTR(ret); 1387 } 1388 1389 return cable; 1390 } 1391 EXPORT_SYMBOL_GPL(typec_register_cable); 1392 1393 /** 1394 * typec_unregister_cable - Unregister a USB Type-C Cable 1395 * @cable: The cable to be unregistered 1396 * 1397 * Unregister device created with typec_register_cable(). 1398 */ 1399 void typec_unregister_cable(struct typec_cable *cable) 1400 { 1401 if (!IS_ERR_OR_NULL(cable)) 1402 device_unregister(&cable->dev); 1403 } 1404 EXPORT_SYMBOL_GPL(typec_unregister_cable); 1405 1406 /* ------------------------------------------------------------------------- */ 1407 /* USB Type-C ports */ 1408 1409 /** 1410 * typec_port_set_usb_mode - Set the operational USB mode for the port 1411 * @port: USB Type-C port 1412 * @mode: USB Mode (USB2, USB3 or USB4) 1413 * 1414 * @mode will be used with the next Enter_USB message. Existing connections are 1415 * not affected. 1416 */ 1417 void typec_port_set_usb_mode(struct typec_port *port, enum usb_mode mode) 1418 { 1419 port->usb_mode = mode; 1420 } 1421 EXPORT_SYMBOL_GPL(typec_port_set_usb_mode); 1422 1423 static ssize_t 1424 usb_capability_show(struct device *dev, struct device_attribute *attr, char *buf) 1425 { 1426 struct typec_port *port = to_typec_port(dev); 1427 int len = 0; 1428 int i; 1429 1430 for (i = USB_MODE_USB2; i < USB_MODE_USB4 + 1; i++) { 1431 if (!(BIT(i - 1) & port->cap->usb_capability)) 1432 continue; 1433 1434 if (i == port->usb_mode) 1435 len += sysfs_emit_at(buf, len, "[%s] ", usb_modes[i]); 1436 else 1437 len += sysfs_emit_at(buf, len, "%s ", usb_modes[i]); 1438 } 1439 1440 sysfs_emit_at(buf, len - 1, "\n"); 1441 1442 return len; 1443 } 1444 1445 static ssize_t 1446 usb_capability_store(struct device *dev, struct device_attribute *attr, 1447 const char *buf, size_t size) 1448 { 1449 struct typec_port *port = to_typec_port(dev); 1450 int ret = 0; 1451 int mode; 1452 1453 if (!port->ops || !port->ops->default_usb_mode_set) 1454 return -EOPNOTSUPP; 1455 1456 mode = sysfs_match_string(usb_modes, buf); 1457 if (mode < 0) 1458 return mode; 1459 1460 ret = port->ops->default_usb_mode_set(port, mode); 1461 if (ret) 1462 return ret; 1463 1464 port->usb_mode = mode; 1465 1466 return size; 1467 } 1468 static DEVICE_ATTR_RW(usb_capability); 1469 1470 /** 1471 * typec_port_set_usb_power_delivery - Assign USB PD for port. 1472 * @port: USB Type-C port. 1473 * @pd: USB PD instance. 1474 * 1475 * This routine can be used to set the USB Power Delivery Capabilities for @port 1476 * that it will advertise to the partner. 1477 * 1478 * If @pd is NULL, the assignment is removed. 1479 */ 1480 int typec_port_set_usb_power_delivery(struct typec_port *port, struct usb_power_delivery *pd) 1481 { 1482 int ret; 1483 1484 if (IS_ERR_OR_NULL(port) || port->pd == pd) 1485 return 0; 1486 1487 if (pd) { 1488 ret = usb_power_delivery_link_device(pd, &port->dev); 1489 if (ret) 1490 return ret; 1491 } else { 1492 usb_power_delivery_unlink_device(port->pd, &port->dev); 1493 } 1494 1495 port->pd = pd; 1496 1497 return 0; 1498 } 1499 EXPORT_SYMBOL_GPL(typec_port_set_usb_power_delivery); 1500 1501 static ssize_t select_usb_power_delivery_store(struct device *dev, 1502 struct device_attribute *attr, 1503 const char *buf, size_t size) 1504 { 1505 struct typec_port *port = to_typec_port(dev); 1506 struct usb_power_delivery *pd; 1507 int ret; 1508 1509 if (!port->ops || !port->ops->pd_set) 1510 return -EOPNOTSUPP; 1511 1512 pd = usb_power_delivery_find(buf); 1513 if (!pd) 1514 return -EINVAL; 1515 1516 ret = port->ops->pd_set(port, pd); 1517 if (ret) 1518 return ret; 1519 1520 return size; 1521 } 1522 1523 static ssize_t select_usb_power_delivery_show(struct device *dev, 1524 struct device_attribute *attr, char *buf) 1525 { 1526 struct typec_port *port = to_typec_port(dev); 1527 struct usb_power_delivery **pds; 1528 int i, ret = 0; 1529 1530 if (!port->ops || !port->ops->pd_get) 1531 return -EOPNOTSUPP; 1532 1533 pds = port->ops->pd_get(port); 1534 if (!pds) 1535 return 0; 1536 1537 for (i = 0; pds[i]; i++) { 1538 if (pds[i] == port->pd) 1539 ret += sysfs_emit_at(buf, ret, "[%s] ", dev_name(&pds[i]->dev)); 1540 else 1541 ret += sysfs_emit_at(buf, ret, "%s ", dev_name(&pds[i]->dev)); 1542 } 1543 1544 buf[ret - 1] = '\n'; 1545 1546 return ret; 1547 } 1548 static DEVICE_ATTR_RW(select_usb_power_delivery); 1549 1550 static struct attribute *port_attrs[] = { 1551 &dev_attr_select_usb_power_delivery.attr, 1552 NULL 1553 }; 1554 1555 static umode_t port_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n) 1556 { 1557 struct typec_port *port = to_typec_port(kobj_to_dev(kobj)); 1558 1559 if (!port->pd || !port->ops || !port->ops->pd_get) 1560 return 0; 1561 if (!port->ops->pd_set) 1562 return 0444; 1563 1564 return attr->mode; 1565 } 1566 1567 static const struct attribute_group pd_group = { 1568 .is_visible = port_attr_is_visible, 1569 .attrs = port_attrs, 1570 }; 1571 1572 static const char * const typec_orientations[] = { 1573 [TYPEC_ORIENTATION_NONE] = "unknown", 1574 [TYPEC_ORIENTATION_NORMAL] = "normal", 1575 [TYPEC_ORIENTATION_REVERSE] = "reverse", 1576 }; 1577 1578 static const char * const typec_roles[] = { 1579 [TYPEC_SINK] = "sink", 1580 [TYPEC_SOURCE] = "source", 1581 }; 1582 1583 static const char * const typec_data_roles[] = { 1584 [TYPEC_DEVICE] = "device", 1585 [TYPEC_HOST] = "host", 1586 }; 1587 1588 static const char * const typec_port_power_roles[] = { 1589 [TYPEC_PORT_SRC] = "source", 1590 [TYPEC_PORT_SNK] = "sink", 1591 [TYPEC_PORT_DRP] = "dual", 1592 }; 1593 1594 static const char * const typec_port_data_roles[] = { 1595 [TYPEC_PORT_DFP] = "host", 1596 [TYPEC_PORT_UFP] = "device", 1597 [TYPEC_PORT_DRD] = "dual", 1598 }; 1599 1600 static const char * const typec_port_types_drp[] = { 1601 [TYPEC_PORT_SRC] = "dual [source] sink", 1602 [TYPEC_PORT_SNK] = "dual source [sink]", 1603 [TYPEC_PORT_DRP] = "[dual] source sink", 1604 }; 1605 1606 static ssize_t 1607 preferred_role_store(struct device *dev, struct device_attribute *attr, 1608 const char *buf, size_t size) 1609 { 1610 struct typec_port *port = to_typec_port(dev); 1611 int role; 1612 int ret; 1613 1614 if (port->cap->type != TYPEC_PORT_DRP) { 1615 dev_dbg(dev, "Preferred role only supported with DRP ports\n"); 1616 return -EOPNOTSUPP; 1617 } 1618 1619 if (!port->ops || !port->ops->try_role) { 1620 dev_dbg(dev, "Setting preferred role not supported\n"); 1621 return -EOPNOTSUPP; 1622 } 1623 1624 role = sysfs_match_string(typec_roles, buf); 1625 if (role < 0) { 1626 if (sysfs_streq(buf, "none")) 1627 role = TYPEC_NO_PREFERRED_ROLE; 1628 else 1629 return -EINVAL; 1630 } 1631 1632 ret = port->ops->try_role(port, role); 1633 if (ret) 1634 return ret; 1635 1636 port->prefer_role = role; 1637 return size; 1638 } 1639 1640 static ssize_t 1641 preferred_role_show(struct device *dev, struct device_attribute *attr, 1642 char *buf) 1643 { 1644 struct typec_port *port = to_typec_port(dev); 1645 1646 if (port->cap->type != TYPEC_PORT_DRP) 1647 return 0; 1648 1649 if (port->prefer_role < 0) 1650 return 0; 1651 1652 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]); 1653 } 1654 static DEVICE_ATTR_RW(preferred_role); 1655 1656 static ssize_t data_role_store(struct device *dev, 1657 struct device_attribute *attr, 1658 const char *buf, size_t size) 1659 { 1660 struct typec_port *port = to_typec_port(dev); 1661 int ret; 1662 1663 if (!port->ops || !port->ops->dr_set) { 1664 dev_dbg(dev, "data role swapping not supported\n"); 1665 return -EOPNOTSUPP; 1666 } 1667 1668 ret = sysfs_match_string(typec_data_roles, buf); 1669 if (ret < 0) 1670 return ret; 1671 1672 mutex_lock(&port->port_type_lock); 1673 if (port->cap->data != TYPEC_PORT_DRD) { 1674 ret = -EOPNOTSUPP; 1675 goto unlock_and_ret; 1676 } 1677 1678 ret = port->ops->dr_set(port, ret); 1679 if (ret) 1680 goto unlock_and_ret; 1681 1682 ret = size; 1683 unlock_and_ret: 1684 mutex_unlock(&port->port_type_lock); 1685 return ret; 1686 } 1687 1688 static ssize_t data_role_show(struct device *dev, 1689 struct device_attribute *attr, char *buf) 1690 { 1691 struct typec_port *port = to_typec_port(dev); 1692 1693 if (port->cap->data == TYPEC_PORT_DRD) 1694 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ? 1695 "[host] device" : "host [device]"); 1696 1697 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]); 1698 } 1699 static DEVICE_ATTR_RW(data_role); 1700 1701 static ssize_t power_role_store(struct device *dev, 1702 struct device_attribute *attr, 1703 const char *buf, size_t size) 1704 { 1705 struct typec_port *port = to_typec_port(dev); 1706 int ret; 1707 1708 if (!port->ops || !port->ops->pr_set) { 1709 dev_dbg(dev, "power role swapping not supported\n"); 1710 return -EOPNOTSUPP; 1711 } 1712 1713 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) { 1714 dev_dbg(dev, "partner unable to swap power role\n"); 1715 return -EIO; 1716 } 1717 1718 ret = sysfs_match_string(typec_roles, buf); 1719 if (ret < 0) 1720 return ret; 1721 1722 mutex_lock(&port->port_type_lock); 1723 if (port->port_type != TYPEC_PORT_DRP) { 1724 dev_dbg(dev, "port type fixed at \"%s\"", 1725 typec_port_power_roles[port->port_type]); 1726 ret = -EOPNOTSUPP; 1727 goto unlock_and_ret; 1728 } 1729 1730 ret = port->ops->pr_set(port, ret); 1731 if (ret) 1732 goto unlock_and_ret; 1733 1734 ret = size; 1735 unlock_and_ret: 1736 mutex_unlock(&port->port_type_lock); 1737 return ret; 1738 } 1739 1740 static ssize_t power_role_show(struct device *dev, 1741 struct device_attribute *attr, char *buf) 1742 { 1743 struct typec_port *port = to_typec_port(dev); 1744 1745 if (port->cap->type == TYPEC_PORT_DRP) 1746 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ? 1747 "[source] sink" : "source [sink]"); 1748 1749 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]); 1750 } 1751 static DEVICE_ATTR_RW(power_role); 1752 1753 static ssize_t 1754 port_type_store(struct device *dev, struct device_attribute *attr, 1755 const char *buf, size_t size) 1756 { 1757 struct typec_port *port = to_typec_port(dev); 1758 int ret; 1759 enum typec_port_type type; 1760 1761 if (port->cap->type != TYPEC_PORT_DRP || 1762 !port->ops || !port->ops->port_type_set) { 1763 dev_dbg(dev, "changing port type not supported\n"); 1764 return -EOPNOTSUPP; 1765 } 1766 1767 ret = sysfs_match_string(typec_port_power_roles, buf); 1768 if (ret < 0) 1769 return ret; 1770 1771 type = ret; 1772 mutex_lock(&port->port_type_lock); 1773 1774 if (port->port_type == type) { 1775 ret = size; 1776 goto unlock_and_ret; 1777 } 1778 1779 ret = port->ops->port_type_set(port, type); 1780 if (ret) 1781 goto unlock_and_ret; 1782 1783 port->port_type = type; 1784 ret = size; 1785 1786 unlock_and_ret: 1787 mutex_unlock(&port->port_type_lock); 1788 return ret; 1789 } 1790 1791 static ssize_t 1792 port_type_show(struct device *dev, struct device_attribute *attr, 1793 char *buf) 1794 { 1795 struct typec_port *port = to_typec_port(dev); 1796 1797 if (port->cap->type == TYPEC_PORT_DRP) 1798 return sprintf(buf, "%s\n", 1799 typec_port_types_drp[port->port_type]); 1800 1801 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]); 1802 } 1803 static DEVICE_ATTR_RW(port_type); 1804 1805 static const char * const typec_pwr_opmodes[] = { 1806 [TYPEC_PWR_MODE_USB] = "default", 1807 [TYPEC_PWR_MODE_1_5A] = "1.5A", 1808 [TYPEC_PWR_MODE_3_0A] = "3.0A", 1809 [TYPEC_PWR_MODE_PD] = "usb_power_delivery", 1810 }; 1811 1812 static ssize_t power_operation_mode_show(struct device *dev, 1813 struct device_attribute *attr, 1814 char *buf) 1815 { 1816 struct typec_port *port = to_typec_port(dev); 1817 1818 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]); 1819 } 1820 static DEVICE_ATTR_RO(power_operation_mode); 1821 1822 static ssize_t vconn_source_store(struct device *dev, 1823 struct device_attribute *attr, 1824 const char *buf, size_t size) 1825 { 1826 struct typec_port *port = to_typec_port(dev); 1827 bool source; 1828 int ret; 1829 1830 if (!port->cap->pd_revision) { 1831 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n"); 1832 return -EOPNOTSUPP; 1833 } 1834 1835 if (!port->ops || !port->ops->vconn_set) { 1836 dev_dbg(dev, "VCONN swapping not supported\n"); 1837 return -EOPNOTSUPP; 1838 } 1839 1840 ret = kstrtobool(buf, &source); 1841 if (ret) 1842 return ret; 1843 1844 ret = port->ops->vconn_set(port, (enum typec_role)source); 1845 if (ret) 1846 return ret; 1847 1848 return size; 1849 } 1850 1851 static ssize_t vconn_source_show(struct device *dev, 1852 struct device_attribute *attr, char *buf) 1853 { 1854 struct typec_port *port = to_typec_port(dev); 1855 1856 return sprintf(buf, "%s\n", 1857 port->vconn_role == TYPEC_SOURCE ? "yes" : "no"); 1858 } 1859 static DEVICE_ATTR_RW(vconn_source); 1860 1861 static ssize_t supported_accessory_modes_show(struct device *dev, 1862 struct device_attribute *attr, 1863 char *buf) 1864 { 1865 struct typec_port *port = to_typec_port(dev); 1866 ssize_t ret = 0; 1867 int i; 1868 1869 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) { 1870 if (port->cap->accessory[i]) 1871 ret += sprintf(buf + ret, "%s ", 1872 typec_accessory_modes[port->cap->accessory[i]]); 1873 } 1874 1875 if (!ret) 1876 return sprintf(buf, "none\n"); 1877 1878 buf[ret - 1] = '\n'; 1879 1880 return ret; 1881 } 1882 static DEVICE_ATTR_RO(supported_accessory_modes); 1883 1884 static ssize_t usb_typec_revision_show(struct device *dev, 1885 struct device_attribute *attr, 1886 char *buf) 1887 { 1888 struct typec_port *port = to_typec_port(dev); 1889 u16 rev = port->cap->revision; 1890 1891 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf); 1892 } 1893 static DEVICE_ATTR_RO(usb_typec_revision); 1894 1895 static ssize_t usb_power_delivery_revision_show(struct device *dev, 1896 struct device_attribute *attr, 1897 char *buf) 1898 { 1899 u16 rev = 0; 1900 1901 if (is_typec_partner(dev)) { 1902 struct typec_partner *partner = to_typec_partner(dev); 1903 1904 rev = partner->pd_revision; 1905 } else if (is_typec_cable(dev)) { 1906 struct typec_cable *cable = to_typec_cable(dev); 1907 1908 rev = cable->pd_revision; 1909 } else if (is_typec_port(dev)) { 1910 struct typec_port *p = to_typec_port(dev); 1911 1912 rev = p->cap->pd_revision; 1913 } 1914 return sysfs_emit(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf); 1915 } 1916 1917 static ssize_t orientation_show(struct device *dev, 1918 struct device_attribute *attr, 1919 char *buf) 1920 { 1921 struct typec_port *port = to_typec_port(dev); 1922 1923 return sprintf(buf, "%s\n", typec_orientations[port->orientation]); 1924 } 1925 static DEVICE_ATTR_RO(orientation); 1926 1927 static struct attribute *typec_attrs[] = { 1928 &dev_attr_data_role.attr, 1929 &dev_attr_power_operation_mode.attr, 1930 &dev_attr_power_role.attr, 1931 &dev_attr_preferred_role.attr, 1932 &dev_attr_supported_accessory_modes.attr, 1933 &dev_attr_usb_power_delivery_revision.attr, 1934 &dev_attr_usb_typec_revision.attr, 1935 &dev_attr_vconn_source.attr, 1936 &dev_attr_port_type.attr, 1937 &dev_attr_orientation.attr, 1938 &dev_attr_usb_capability.attr, 1939 NULL, 1940 }; 1941 1942 static umode_t typec_attr_is_visible(struct kobject *kobj, 1943 struct attribute *attr, int n) 1944 { 1945 struct typec_port *port = to_typec_port(kobj_to_dev(kobj)); 1946 1947 if (attr == &dev_attr_data_role.attr) { 1948 if (port->cap->data != TYPEC_PORT_DRD || 1949 !port->ops || !port->ops->dr_set) 1950 return 0444; 1951 } else if (attr == &dev_attr_power_role.attr) { 1952 if (port->cap->type != TYPEC_PORT_DRP || 1953 !port->ops || !port->ops->pr_set) 1954 return 0444; 1955 } else if (attr == &dev_attr_vconn_source.attr) { 1956 if (!port->cap->pd_revision || 1957 !port->ops || !port->ops->vconn_set) 1958 return 0444; 1959 } else if (attr == &dev_attr_preferred_role.attr) { 1960 if (port->cap->type != TYPEC_PORT_DRP || 1961 !port->ops || !port->ops->try_role) 1962 return 0444; 1963 } else if (attr == &dev_attr_port_type.attr) { 1964 if (!port->ops || !port->ops->port_type_set) 1965 return 0; 1966 if (port->cap->type != TYPEC_PORT_DRP) 1967 return 0444; 1968 } else if (attr == &dev_attr_orientation.attr) { 1969 if (port->cap->orientation_aware) 1970 return 0444; 1971 return 0; 1972 } else if (attr == &dev_attr_usb_capability.attr) { 1973 if (!port->cap->usb_capability) 1974 return 0; 1975 if (!port->ops || !port->ops->default_usb_mode_set) 1976 return 0444; 1977 } 1978 1979 return attr->mode; 1980 } 1981 1982 static const struct attribute_group typec_group = { 1983 .is_visible = typec_attr_is_visible, 1984 .attrs = typec_attrs, 1985 }; 1986 1987 static const struct attribute_group *typec_groups[] = { 1988 &typec_group, 1989 &pd_group, 1990 NULL 1991 }; 1992 1993 static int typec_uevent(const struct device *dev, struct kobj_uevent_env *env) 1994 { 1995 int ret; 1996 1997 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev)); 1998 if (ret) 1999 dev_err(dev, "failed to add uevent TYPEC_PORT\n"); 2000 2001 return ret; 2002 } 2003 2004 static void typec_release(struct device *dev) 2005 { 2006 struct typec_port *port = to_typec_port(dev); 2007 2008 ida_free(&typec_index_ida, port->id); 2009 ida_destroy(&port->mode_ids); 2010 typec_switch_put(port->sw); 2011 typec_mux_put(port->mux); 2012 typec_retimer_put(port->retimer); 2013 kfree(port->cap); 2014 kfree(port); 2015 } 2016 2017 const struct device_type typec_port_dev_type = { 2018 .name = "typec_port", 2019 .groups = typec_groups, 2020 .uevent = typec_uevent, 2021 .release = typec_release, 2022 }; 2023 2024 /* --------------------------------------- */ 2025 /* Driver callbacks to report role updates */ 2026 2027 static struct typec_partner *typec_get_partner(struct typec_port *port) 2028 { 2029 struct device *dev; 2030 2031 dev = device_find_child(&port->dev, &typec_partner_dev_type, 2032 device_match_type); 2033 if (!dev) 2034 return NULL; 2035 2036 return to_typec_partner(dev); 2037 } 2038 2039 static void typec_partner_attach(struct typec_connector *con, struct device *dev) 2040 { 2041 struct typec_port *port = container_of(con, struct typec_port, con); 2042 struct typec_partner *partner = typec_get_partner(port); 2043 struct usb_device *udev = to_usb_device(dev); 2044 enum usb_mode usb_mode; 2045 2046 if (udev->speed < USB_SPEED_SUPER) { 2047 usb_mode = USB_MODE_USB2; 2048 port->usb2_dev = dev; 2049 } else { 2050 usb_mode = USB_MODE_USB3; 2051 port->usb3_dev = dev; 2052 } 2053 2054 if (partner) { 2055 typec_partner_set_usb_mode(partner, usb_mode); 2056 typec_partner_link_device(partner, dev); 2057 put_device(&partner->dev); 2058 } 2059 } 2060 2061 static void typec_partner_deattach(struct typec_connector *con, struct device *dev) 2062 { 2063 struct typec_port *port = container_of(con, struct typec_port, con); 2064 struct typec_partner *partner = typec_get_partner(port); 2065 2066 if (partner) { 2067 typec_partner_unlink_device(partner, dev); 2068 put_device(&partner->dev); 2069 } 2070 2071 if (port->usb2_dev == dev) 2072 port->usb2_dev = NULL; 2073 else if (port->usb3_dev == dev) 2074 port->usb3_dev = NULL; 2075 } 2076 2077 /** 2078 * typec_set_data_role - Report data role change 2079 * @port: The USB Type-C Port where the role was changed 2080 * @role: The new data role 2081 * 2082 * This routine is used by the port drivers to report data role changes. 2083 */ 2084 void typec_set_data_role(struct typec_port *port, enum typec_data_role role) 2085 { 2086 struct typec_partner *partner; 2087 2088 if (port->data_role == role) 2089 return; 2090 2091 port->data_role = role; 2092 sysfs_notify(&port->dev.kobj, NULL, "data_role"); 2093 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 2094 2095 partner = typec_get_partner(port); 2096 if (!partner) 2097 return; 2098 2099 if (partner->identity) 2100 typec_product_type_notify(&partner->dev); 2101 2102 put_device(&partner->dev); 2103 } 2104 EXPORT_SYMBOL_GPL(typec_set_data_role); 2105 2106 /** 2107 * typec_set_pwr_role - Report power role change 2108 * @port: The USB Type-C Port where the role was changed 2109 * @role: The new data role 2110 * 2111 * This routine is used by the port drivers to report power role changes. 2112 */ 2113 void typec_set_pwr_role(struct typec_port *port, enum typec_role role) 2114 { 2115 if (port->pwr_role == role) 2116 return; 2117 2118 port->pwr_role = role; 2119 sysfs_notify(&port->dev.kobj, NULL, "power_role"); 2120 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 2121 } 2122 EXPORT_SYMBOL_GPL(typec_set_pwr_role); 2123 2124 /** 2125 * typec_set_vconn_role - Report VCONN source change 2126 * @port: The USB Type-C Port which VCONN role changed 2127 * @role: Source when @port is sourcing VCONN, or Sink when it's not 2128 * 2129 * This routine is used by the port drivers to report if the VCONN source is 2130 * changes. 2131 */ 2132 void typec_set_vconn_role(struct typec_port *port, enum typec_role role) 2133 { 2134 if (port->vconn_role == role) 2135 return; 2136 2137 port->vconn_role = role; 2138 sysfs_notify(&port->dev.kobj, NULL, "vconn_source"); 2139 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 2140 } 2141 EXPORT_SYMBOL_GPL(typec_set_vconn_role); 2142 2143 /** 2144 * typec_set_pwr_opmode - Report changed power operation mode 2145 * @port: The USB Type-C Port where the mode was changed 2146 * @opmode: New power operation mode 2147 * 2148 * This routine is used by the port drivers to report changed power operation 2149 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB 2150 * Type-C specification, and "USB Power Delivery" when the power levels are 2151 * negotiated with methods defined in USB Power Delivery specification. 2152 */ 2153 void typec_set_pwr_opmode(struct typec_port *port, 2154 enum typec_pwr_opmode opmode) 2155 { 2156 struct device *partner_dev; 2157 2158 if (port->pwr_opmode == opmode) 2159 return; 2160 2161 port->pwr_opmode = opmode; 2162 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode"); 2163 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 2164 2165 partner_dev = device_find_child(&port->dev, 2166 &typec_partner_dev_type, 2167 device_match_type); 2168 if (partner_dev) { 2169 struct typec_partner *partner = to_typec_partner(partner_dev); 2170 2171 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) { 2172 partner->usb_pd = 1; 2173 sysfs_notify(&partner_dev->kobj, NULL, 2174 "supports_usb_power_delivery"); 2175 kobject_uevent(&partner_dev->kobj, KOBJ_CHANGE); 2176 } 2177 put_device(partner_dev); 2178 } 2179 } 2180 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode); 2181 2182 /** 2183 * typec_find_pwr_opmode - Get the typec power operation mode capability 2184 * @name: power operation mode string 2185 * 2186 * This routine is used to find the typec_pwr_opmode by its string @name. 2187 * 2188 * Returns typec_pwr_opmode if success, otherwise negative error code. 2189 */ 2190 int typec_find_pwr_opmode(const char *name) 2191 { 2192 return match_string(typec_pwr_opmodes, 2193 ARRAY_SIZE(typec_pwr_opmodes), name); 2194 } 2195 EXPORT_SYMBOL_GPL(typec_find_pwr_opmode); 2196 2197 /** 2198 * typec_find_orientation - Convert orientation string to enum typec_orientation 2199 * @name: Orientation string 2200 * 2201 * This routine is used to find the typec_orientation by its string name @name. 2202 * 2203 * Returns the orientation value on success, otherwise negative error code. 2204 */ 2205 int typec_find_orientation(const char *name) 2206 { 2207 return match_string(typec_orientations, ARRAY_SIZE(typec_orientations), 2208 name); 2209 } 2210 EXPORT_SYMBOL_GPL(typec_find_orientation); 2211 2212 /** 2213 * typec_find_port_power_role - Get the typec port power capability 2214 * @name: port power capability string 2215 * 2216 * This routine is used to find the typec_port_type by its string name. 2217 * 2218 * Returns typec_port_type if success, otherwise negative error code. 2219 */ 2220 int typec_find_port_power_role(const char *name) 2221 { 2222 return match_string(typec_port_power_roles, 2223 ARRAY_SIZE(typec_port_power_roles), name); 2224 } 2225 EXPORT_SYMBOL_GPL(typec_find_port_power_role); 2226 2227 /** 2228 * typec_find_power_role - Find the typec one specific power role 2229 * @name: power role string 2230 * 2231 * This routine is used to find the typec_role by its string name. 2232 * 2233 * Returns typec_role if success, otherwise negative error code. 2234 */ 2235 int typec_find_power_role(const char *name) 2236 { 2237 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name); 2238 } 2239 EXPORT_SYMBOL_GPL(typec_find_power_role); 2240 2241 /** 2242 * typec_find_port_data_role - Get the typec port data capability 2243 * @name: port data capability string 2244 * 2245 * This routine is used to find the typec_port_data by its string name. 2246 * 2247 * Returns typec_port_data if success, otherwise negative error code. 2248 */ 2249 int typec_find_port_data_role(const char *name) 2250 { 2251 return match_string(typec_port_data_roles, 2252 ARRAY_SIZE(typec_port_data_roles), name); 2253 } 2254 EXPORT_SYMBOL_GPL(typec_find_port_data_role); 2255 2256 /* ------------------------------------------ */ 2257 /* API for Multiplexer/DeMultiplexer Switches */ 2258 2259 /** 2260 * typec_set_orientation - Set USB Type-C cable plug orientation 2261 * @port: USB Type-C Port 2262 * @orientation: USB Type-C cable plug orientation 2263 * 2264 * Set cable plug orientation for @port. 2265 */ 2266 int typec_set_orientation(struct typec_port *port, 2267 enum typec_orientation orientation) 2268 { 2269 int ret; 2270 2271 ret = typec_switch_set(port->sw, orientation); 2272 if (ret) 2273 return ret; 2274 2275 port->orientation = orientation; 2276 sysfs_notify(&port->dev.kobj, NULL, "orientation"); 2277 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 2278 2279 return 0; 2280 } 2281 EXPORT_SYMBOL_GPL(typec_set_orientation); 2282 2283 /** 2284 * typec_get_orientation - Get USB Type-C cable plug orientation 2285 * @port: USB Type-C Port 2286 * 2287 * Get current cable plug orientation for @port. 2288 */ 2289 enum typec_orientation typec_get_orientation(struct typec_port *port) 2290 { 2291 return port->orientation; 2292 } 2293 EXPORT_SYMBOL_GPL(typec_get_orientation); 2294 2295 /** 2296 * typec_set_mode - Set mode of operation for USB Type-C connector 2297 * @port: USB Type-C connector 2298 * @mode: Accessory Mode, USB Operation or Safe State 2299 * 2300 * Configure @port for Accessory Mode @mode. This function will configure the 2301 * muxes needed for @mode. 2302 */ 2303 int typec_set_mode(struct typec_port *port, int mode) 2304 { 2305 struct typec_mux_state state = { }; 2306 2307 state.mode = mode; 2308 2309 return typec_mux_set(port->mux, &state); 2310 } 2311 EXPORT_SYMBOL_GPL(typec_set_mode); 2312 2313 /* --------------------------------------- */ 2314 2315 /** 2316 * typec_get_negotiated_svdm_version - Get negotiated SVDM Version 2317 * @port: USB Type-C Port. 2318 * 2319 * Get the negotiated SVDM Version. The Version is set to the port default 2320 * value stored in typec_capability on partner registration, and updated after 2321 * a successful Discover Identity if the negotiated value is less than the 2322 * default value. 2323 * 2324 * Returns usb_pd_svdm_ver if the partner has been registered otherwise -ENODEV. 2325 */ 2326 int typec_get_negotiated_svdm_version(struct typec_port *port) 2327 { 2328 enum usb_pd_svdm_ver svdm_version; 2329 struct device *partner_dev; 2330 2331 partner_dev = device_find_child(&port->dev, 2332 &typec_partner_dev_type, 2333 device_match_type); 2334 if (!partner_dev) 2335 return -ENODEV; 2336 2337 svdm_version = to_typec_partner(partner_dev)->svdm_version; 2338 put_device(partner_dev); 2339 2340 return svdm_version; 2341 } 2342 EXPORT_SYMBOL_GPL(typec_get_negotiated_svdm_version); 2343 2344 /** 2345 * typec_get_cable_svdm_version - Get cable negotiated SVDM Version 2346 * @port: USB Type-C Port. 2347 * 2348 * Get the negotiated SVDM Version for the cable. The Version is set to the port 2349 * default value based on the PD Revision during cable registration, and updated 2350 * after a successful Discover Identity if the negotiated value is less than the 2351 * default. 2352 * 2353 * Returns usb_pd_svdm_ver if the cable has been registered otherwise -ENODEV. 2354 */ 2355 int typec_get_cable_svdm_version(struct typec_port *port) 2356 { 2357 enum usb_pd_svdm_ver svdm_version; 2358 struct device *cable_dev; 2359 2360 cable_dev = device_find_child(&port->dev, &typec_cable_dev_type, 2361 device_match_type); 2362 if (!cable_dev) 2363 return -ENODEV; 2364 2365 svdm_version = to_typec_cable(cable_dev)->svdm_version; 2366 put_device(cable_dev); 2367 2368 return svdm_version; 2369 } 2370 EXPORT_SYMBOL_GPL(typec_get_cable_svdm_version); 2371 2372 /** 2373 * typec_cable_set_svdm_version - Set negotiated Structured VDM (SVDM) Version 2374 * @cable: USB Type-C Active Cable that supports SVDM 2375 * @svdm_version: Negotiated SVDM Version 2376 * 2377 * This routine is used to save the negotiated SVDM Version. 2378 */ 2379 void typec_cable_set_svdm_version(struct typec_cable *cable, enum usb_pd_svdm_ver svdm_version) 2380 { 2381 cable->svdm_version = svdm_version; 2382 } 2383 EXPORT_SYMBOL_GPL(typec_cable_set_svdm_version); 2384 2385 /** 2386 * typec_get_drvdata - Return private driver data pointer 2387 * @port: USB Type-C port 2388 */ 2389 void *typec_get_drvdata(struct typec_port *port) 2390 { 2391 return dev_get_drvdata(&port->dev); 2392 } 2393 EXPORT_SYMBOL_GPL(typec_get_drvdata); 2394 2395 int typec_get_fw_cap(struct typec_capability *cap, 2396 struct fwnode_handle *fwnode) 2397 { 2398 const char *cap_str; 2399 int ret; 2400 2401 cap->fwnode = fwnode; 2402 2403 ret = fwnode_property_read_string(fwnode, "power-role", &cap_str); 2404 if (ret < 0) 2405 return ret; 2406 2407 ret = typec_find_port_power_role(cap_str); 2408 if (ret < 0) 2409 return ret; 2410 cap->type = ret; 2411 2412 /* USB data support is optional */ 2413 ret = fwnode_property_read_string(fwnode, "data-role", &cap_str); 2414 if (ret == 0) { 2415 ret = typec_find_port_data_role(cap_str); 2416 if (ret < 0) 2417 return ret; 2418 cap->data = ret; 2419 } 2420 2421 /* Get the preferred power role for a DRP */ 2422 if (cap->type == TYPEC_PORT_DRP) { 2423 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE; 2424 2425 ret = fwnode_property_read_string(fwnode, "try-power-role", &cap_str); 2426 if (ret == 0) { 2427 ret = typec_find_power_role(cap_str); 2428 if (ret < 0) 2429 return ret; 2430 cap->prefer_role = ret; 2431 } 2432 } 2433 2434 return 0; 2435 } 2436 EXPORT_SYMBOL_GPL(typec_get_fw_cap); 2437 2438 /** 2439 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode 2440 * @port: USB Type-C Port that supports the alternate mode 2441 * @desc: Description of the alternate mode 2442 * 2443 * This routine is used to register an alternate mode that @port is capable of 2444 * supporting. 2445 * 2446 * Returns handle to the alternate mode on success or ERR_PTR on failure. 2447 */ 2448 struct typec_altmode * 2449 typec_port_register_altmode(struct typec_port *port, 2450 const struct typec_altmode_desc *desc) 2451 { 2452 struct typec_altmode *adev; 2453 struct typec_mux *mux; 2454 struct typec_retimer *retimer; 2455 2456 mux = typec_mux_get(&port->dev); 2457 if (IS_ERR(mux)) 2458 return ERR_CAST(mux); 2459 2460 retimer = typec_retimer_get(&port->dev); 2461 if (IS_ERR(retimer)) { 2462 typec_mux_put(mux); 2463 return ERR_CAST(retimer); 2464 } 2465 2466 adev = typec_register_altmode(&port->dev, desc); 2467 if (IS_ERR(adev)) { 2468 typec_retimer_put(retimer); 2469 typec_mux_put(mux); 2470 } else { 2471 to_altmode(adev)->mux = mux; 2472 to_altmode(adev)->retimer = retimer; 2473 } 2474 2475 return adev; 2476 } 2477 EXPORT_SYMBOL_GPL(typec_port_register_altmode); 2478 2479 void typec_port_register_altmodes(struct typec_port *port, 2480 const struct typec_altmode_ops *ops, void *drvdata, 2481 struct typec_altmode **altmodes, size_t n) 2482 { 2483 struct fwnode_handle *child; 2484 struct typec_altmode_desc desc; 2485 struct typec_altmode *alt; 2486 size_t index = 0; 2487 u16 svid; 2488 u32 vdo; 2489 int ret; 2490 2491 struct fwnode_handle *altmodes_node __free(fwnode_handle) = 2492 device_get_named_child_node(&port->dev, "altmodes"); 2493 2494 if (!altmodes_node) 2495 return; /* No altmodes specified */ 2496 2497 fwnode_for_each_child_node(altmodes_node, child) { 2498 ret = fwnode_property_read_u16(child, "svid", &svid); 2499 if (ret) { 2500 dev_err(&port->dev, "Error reading svid for altmode %s\n", 2501 fwnode_get_name(child)); 2502 continue; 2503 } 2504 2505 ret = fwnode_property_read_u32(child, "vdo", &vdo); 2506 if (ret) { 2507 dev_err(&port->dev, "Error reading vdo for altmode %s\n", 2508 fwnode_get_name(child)); 2509 continue; 2510 } 2511 2512 if (index >= n) { 2513 dev_err(&port->dev, "Error not enough space for altmode %s\n", 2514 fwnode_get_name(child)); 2515 continue; 2516 } 2517 2518 desc.svid = svid; 2519 desc.vdo = vdo; 2520 desc.mode = index + 1; 2521 alt = typec_port_register_altmode(port, &desc); 2522 if (IS_ERR(alt)) { 2523 dev_err(&port->dev, "Error registering altmode %s\n", 2524 fwnode_get_name(child)); 2525 continue; 2526 } 2527 2528 typec_altmode_set_ops(alt, ops); 2529 typec_altmode_set_drvdata(alt, drvdata); 2530 altmodes[index] = alt; 2531 index++; 2532 } 2533 } 2534 EXPORT_SYMBOL_GPL(typec_port_register_altmodes); 2535 2536 /** 2537 * typec_port_register_cable_ops - Register typec_cable_ops to port altmodes 2538 * @altmodes: USB Type-C Port's altmode vector 2539 * @max_altmodes: The maximum number of alt modes supported by the port 2540 * @ops: Cable alternate mode vector 2541 */ 2542 void typec_port_register_cable_ops(struct typec_altmode **altmodes, int max_altmodes, 2543 const struct typec_cable_ops *ops) 2544 { 2545 int i; 2546 2547 for (i = 0; i < max_altmodes; i++) { 2548 if (!altmodes[i]) 2549 return; 2550 altmodes[i]->cable_ops = ops; 2551 } 2552 } 2553 EXPORT_SYMBOL_GPL(typec_port_register_cable_ops); 2554 2555 /** 2556 * typec_register_port - Register a USB Type-C Port 2557 * @parent: Parent device 2558 * @cap: Description of the port 2559 * 2560 * Registers a device for USB Type-C Port described in @cap. 2561 * 2562 * Returns handle to the port on success or ERR_PTR on failure. 2563 */ 2564 struct typec_port *typec_register_port(struct device *parent, 2565 const struct typec_capability *cap) 2566 { 2567 struct typec_port *port; 2568 int ret; 2569 int id; 2570 2571 port = kzalloc(sizeof(*port), GFP_KERNEL); 2572 if (!port) 2573 return ERR_PTR(-ENOMEM); 2574 2575 id = ida_alloc(&typec_index_ida, GFP_KERNEL); 2576 if (id < 0) { 2577 kfree(port); 2578 return ERR_PTR(id); 2579 } 2580 2581 switch (cap->type) { 2582 case TYPEC_PORT_SRC: 2583 port->pwr_role = TYPEC_SOURCE; 2584 port->vconn_role = TYPEC_SOURCE; 2585 break; 2586 case TYPEC_PORT_SNK: 2587 port->pwr_role = TYPEC_SINK; 2588 port->vconn_role = TYPEC_SINK; 2589 break; 2590 case TYPEC_PORT_DRP: 2591 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE) 2592 port->pwr_role = cap->prefer_role; 2593 else 2594 port->pwr_role = TYPEC_SINK; 2595 break; 2596 } 2597 2598 switch (cap->data) { 2599 case TYPEC_PORT_DFP: 2600 port->data_role = TYPEC_HOST; 2601 break; 2602 case TYPEC_PORT_UFP: 2603 port->data_role = TYPEC_DEVICE; 2604 break; 2605 case TYPEC_PORT_DRD: 2606 if (cap->prefer_role == TYPEC_SOURCE) 2607 port->data_role = TYPEC_HOST; 2608 else 2609 port->data_role = TYPEC_DEVICE; 2610 break; 2611 } 2612 2613 ida_init(&port->mode_ids); 2614 mutex_init(&port->port_type_lock); 2615 2616 port->id = id; 2617 port->ops = cap->ops; 2618 port->port_type = cap->type; 2619 port->prefer_role = cap->prefer_role; 2620 port->con.attach = typec_partner_attach; 2621 port->con.deattach = typec_partner_deattach; 2622 2623 if (cap->usb_capability & USB_CAPABILITY_USB4) 2624 port->usb_mode = USB_MODE_USB4; 2625 else if (cap->usb_capability & USB_CAPABILITY_USB3) 2626 port->usb_mode = USB_MODE_USB3; 2627 else if (cap->usb_capability & USB_CAPABILITY_USB2) 2628 port->usb_mode = USB_MODE_USB2; 2629 2630 device_initialize(&port->dev); 2631 port->dev.class = &typec_class; 2632 port->dev.parent = parent; 2633 port->dev.fwnode = cap->fwnode; 2634 port->dev.type = &typec_port_dev_type; 2635 dev_set_name(&port->dev, "port%d", id); 2636 dev_set_drvdata(&port->dev, cap->driver_data); 2637 2638 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL); 2639 if (!port->cap) { 2640 put_device(&port->dev); 2641 return ERR_PTR(-ENOMEM); 2642 } 2643 2644 port->sw = typec_switch_get(&port->dev); 2645 if (IS_ERR(port->sw)) { 2646 ret = PTR_ERR(port->sw); 2647 put_device(&port->dev); 2648 return ERR_PTR(ret); 2649 } 2650 2651 port->mux = typec_mux_get(&port->dev); 2652 if (IS_ERR(port->mux)) { 2653 ret = PTR_ERR(port->mux); 2654 put_device(&port->dev); 2655 return ERR_PTR(ret); 2656 } 2657 2658 port->retimer = typec_retimer_get(&port->dev); 2659 if (IS_ERR(port->retimer)) { 2660 ret = PTR_ERR(port->retimer); 2661 put_device(&port->dev); 2662 return ERR_PTR(ret); 2663 } 2664 2665 port->pd = cap->pd; 2666 2667 ret = device_add(&port->dev); 2668 if (ret) { 2669 dev_err(parent, "failed to register port (%d)\n", ret); 2670 put_device(&port->dev); 2671 return ERR_PTR(ret); 2672 } 2673 2674 ret = usb_power_delivery_link_device(port->pd, &port->dev); 2675 if (ret) { 2676 dev_err(&port->dev, "failed to link pd\n"); 2677 device_unregister(&port->dev); 2678 return ERR_PTR(ret); 2679 } 2680 2681 ret = typec_link_ports(port); 2682 if (ret) 2683 dev_warn(&port->dev, "failed to create symlinks (%d)\n", ret); 2684 2685 return port; 2686 } 2687 EXPORT_SYMBOL_GPL(typec_register_port); 2688 2689 /** 2690 * typec_unregister_port - Unregister a USB Type-C Port 2691 * @port: The port to be unregistered 2692 * 2693 * Unregister device created with typec_register_port(). 2694 */ 2695 void typec_unregister_port(struct typec_port *port) 2696 { 2697 if (!IS_ERR_OR_NULL(port)) { 2698 typec_unlink_ports(port); 2699 typec_port_set_usb_power_delivery(port, NULL); 2700 device_unregister(&port->dev); 2701 } 2702 } 2703 EXPORT_SYMBOL_GPL(typec_unregister_port); 2704 2705 static int __init typec_init(void) 2706 { 2707 int ret; 2708 2709 ret = bus_register(&typec_bus); 2710 if (ret) 2711 return ret; 2712 2713 ret = class_register(&typec_mux_class); 2714 if (ret) 2715 goto err_unregister_bus; 2716 2717 ret = class_register(&retimer_class); 2718 if (ret) 2719 goto err_unregister_mux_class; 2720 2721 ret = class_register(&typec_class); 2722 if (ret) 2723 goto err_unregister_retimer_class; 2724 2725 ret = usb_power_delivery_init(); 2726 if (ret) 2727 goto err_unregister_class; 2728 2729 return 0; 2730 2731 err_unregister_class: 2732 class_unregister(&typec_class); 2733 2734 err_unregister_retimer_class: 2735 class_unregister(&retimer_class); 2736 2737 err_unregister_mux_class: 2738 class_unregister(&typec_mux_class); 2739 2740 err_unregister_bus: 2741 bus_unregister(&typec_bus); 2742 2743 return ret; 2744 } 2745 subsys_initcall(typec_init); 2746 2747 static void __exit typec_exit(void) 2748 { 2749 usb_power_delivery_exit(); 2750 class_unregister(&typec_class); 2751 ida_destroy(&typec_index_ida); 2752 bus_unregister(&typec_bus); 2753 class_unregister(&typec_mux_class); 2754 class_unregister(&retimer_class); 2755 } 2756 module_exit(typec_exit); 2757 2758 MODULE_AUTHOR("Heikki Krogerus <[email protected]>"); 2759 MODULE_LICENSE("GPL v2"); 2760 MODULE_DESCRIPTION("USB Type-C Connector Class"); 2761