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