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