Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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