at v5.11-rc5 2050 lines 53 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/device.h> 10#include <linux/module.h> 11#include <linux/mutex.h> 12#include <linux/property.h> 13#include <linux/slab.h> 14#include <linux/usb/pd_vdo.h> 15 16#include "bus.h" 17 18struct typec_plug { 19 struct device dev; 20 enum typec_plug_index index; 21 struct ida mode_ids; 22 int num_altmodes; 23}; 24 25struct typec_cable { 26 struct device dev; 27 enum typec_plug_type type; 28 struct usb_pd_identity *identity; 29 unsigned int active:1; 30}; 31 32struct typec_partner { 33 struct device dev; 34 unsigned int usb_pd:1; 35 struct usb_pd_identity *identity; 36 enum typec_accessory accessory; 37 struct ida mode_ids; 38 int num_altmodes; 39}; 40 41struct typec_port { 42 unsigned int id; 43 struct device dev; 44 struct ida mode_ids; 45 46 int prefer_role; 47 enum typec_data_role data_role; 48 enum typec_role pwr_role; 49 enum typec_role vconn_role; 50 enum typec_pwr_opmode pwr_opmode; 51 enum typec_port_type port_type; 52 struct mutex port_type_lock; 53 54 enum typec_orientation orientation; 55 struct typec_switch *sw; 56 struct typec_mux *mux; 57 58 const struct typec_capability *cap; 59 const struct typec_operations *ops; 60}; 61 62#define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev) 63#define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev) 64#define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev) 65#define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev) 66 67static const struct device_type typec_partner_dev_type; 68static const struct device_type typec_cable_dev_type; 69static const struct device_type typec_plug_dev_type; 70 71#define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type) 72#define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type) 73#define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type) 74 75static DEFINE_IDA(typec_index_ida); 76static struct class *typec_class; 77 78/* ------------------------------------------------------------------------- */ 79/* Common attributes */ 80 81static const char * const typec_accessory_modes[] = { 82 [TYPEC_ACCESSORY_NONE] = "none", 83 [TYPEC_ACCESSORY_AUDIO] = "analog_audio", 84 [TYPEC_ACCESSORY_DEBUG] = "debug", 85}; 86 87/* Product types defined in USB PD Specification R3.0 V2.0 */ 88static const char * const product_type_ufp[8] = { 89 [IDH_PTYPE_UNDEF] = "undefined", 90 [IDH_PTYPE_HUB] = "hub", 91 [IDH_PTYPE_PERIPH] = "peripheral", 92 [IDH_PTYPE_PSD] = "psd", 93 [IDH_PTYPE_AMA] = "ama", 94}; 95 96static const char * const product_type_dfp[8] = { 97 [IDH_PTYPE_DFP_UNDEF] = "undefined", 98 [IDH_PTYPE_DFP_HUB] = "hub", 99 [IDH_PTYPE_DFP_HOST] = "host", 100 [IDH_PTYPE_DFP_PB] = "power_brick", 101 [IDH_PTYPE_DFP_AMC] = "amc", 102}; 103 104static const char * const product_type_cable[8] = { 105 [IDH_PTYPE_UNDEF] = "undefined", 106 [IDH_PTYPE_PCABLE] = "passive", 107 [IDH_PTYPE_ACABLE] = "active", 108}; 109 110static struct usb_pd_identity *get_pd_identity(struct device *dev) 111{ 112 if (is_typec_partner(dev)) { 113 struct typec_partner *partner = to_typec_partner(dev); 114 115 return partner->identity; 116 } else if (is_typec_cable(dev)) { 117 struct typec_cable *cable = to_typec_cable(dev); 118 119 return cable->identity; 120 } 121 return NULL; 122} 123 124static const char *get_pd_product_type(struct device *dev) 125{ 126 struct typec_port *port = to_typec_port(dev->parent); 127 struct usb_pd_identity *id = get_pd_identity(dev); 128 const char *ptype = NULL; 129 130 if (is_typec_partner(dev)) { 131 if (!id) 132 return NULL; 133 134 if (port->data_role == TYPEC_HOST) 135 ptype = product_type_ufp[PD_IDH_PTYPE(id->id_header)]; 136 else 137 ptype = product_type_dfp[PD_IDH_DFP_PTYPE(id->id_header)]; 138 } else if (is_typec_cable(dev)) { 139 if (id) 140 ptype = product_type_cable[PD_IDH_PTYPE(id->id_header)]; 141 else 142 ptype = to_typec_cable(dev)->active ? 143 product_type_cable[IDH_PTYPE_ACABLE] : 144 product_type_cable[IDH_PTYPE_PCABLE]; 145 } 146 147 return ptype; 148} 149 150static ssize_t id_header_show(struct device *dev, struct device_attribute *attr, 151 char *buf) 152{ 153 struct usb_pd_identity *id = get_pd_identity(dev); 154 155 return sprintf(buf, "0x%08x\n", id->id_header); 156} 157static DEVICE_ATTR_RO(id_header); 158 159static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr, 160 char *buf) 161{ 162 struct usb_pd_identity *id = get_pd_identity(dev); 163 164 return sprintf(buf, "0x%08x\n", id->cert_stat); 165} 166static DEVICE_ATTR_RO(cert_stat); 167 168static ssize_t product_show(struct device *dev, struct device_attribute *attr, 169 char *buf) 170{ 171 struct usb_pd_identity *id = get_pd_identity(dev); 172 173 return sprintf(buf, "0x%08x\n", id->product); 174} 175static DEVICE_ATTR_RO(product); 176 177static ssize_t product_type_vdo1_show(struct device *dev, struct device_attribute *attr, 178 char *buf) 179{ 180 struct usb_pd_identity *id = get_pd_identity(dev); 181 182 return sysfs_emit(buf, "0x%08x\n", id->vdo[0]); 183} 184static DEVICE_ATTR_RO(product_type_vdo1); 185 186static ssize_t product_type_vdo2_show(struct device *dev, struct device_attribute *attr, 187 char *buf) 188{ 189 struct usb_pd_identity *id = get_pd_identity(dev); 190 191 return sysfs_emit(buf, "0x%08x\n", id->vdo[1]); 192} 193static DEVICE_ATTR_RO(product_type_vdo2); 194 195static ssize_t product_type_vdo3_show(struct device *dev, struct device_attribute *attr, 196 char *buf) 197{ 198 struct usb_pd_identity *id = get_pd_identity(dev); 199 200 return sysfs_emit(buf, "0x%08x\n", id->vdo[2]); 201} 202static DEVICE_ATTR_RO(product_type_vdo3); 203 204static struct attribute *usb_pd_id_attrs[] = { 205 &dev_attr_id_header.attr, 206 &dev_attr_cert_stat.attr, 207 &dev_attr_product.attr, 208 &dev_attr_product_type_vdo1.attr, 209 &dev_attr_product_type_vdo2.attr, 210 &dev_attr_product_type_vdo3.attr, 211 NULL 212}; 213 214static const struct attribute_group usb_pd_id_group = { 215 .name = "identity", 216 .attrs = usb_pd_id_attrs, 217}; 218 219static const struct attribute_group *usb_pd_id_groups[] = { 220 &usb_pd_id_group, 221 NULL, 222}; 223 224static void typec_product_type_notify(struct device *dev) 225{ 226 char *envp[2] = { }; 227 const char *ptype; 228 229 ptype = get_pd_product_type(dev); 230 if (!ptype) 231 return; 232 233 sysfs_notify(&dev->kobj, NULL, "type"); 234 235 envp[0] = kasprintf(GFP_KERNEL, "PRODUCT_TYPE=%s", ptype); 236 if (!envp[0]) 237 return; 238 239 kobject_uevent_env(&dev->kobj, KOBJ_CHANGE, envp); 240 kfree(envp[0]); 241} 242 243static void typec_report_identity(struct device *dev) 244{ 245 sysfs_notify(&dev->kobj, "identity", "id_header"); 246 sysfs_notify(&dev->kobj, "identity", "cert_stat"); 247 sysfs_notify(&dev->kobj, "identity", "product"); 248 sysfs_notify(&dev->kobj, "identity", "product_type_vdo1"); 249 sysfs_notify(&dev->kobj, "identity", "product_type_vdo2"); 250 sysfs_notify(&dev->kobj, "identity", "product_type_vdo3"); 251 typec_product_type_notify(dev); 252} 253 254static ssize_t 255type_show(struct device *dev, struct device_attribute *attr, char *buf) 256{ 257 const char *ptype; 258 259 ptype = get_pd_product_type(dev); 260 if (!ptype) 261 return 0; 262 263 return sysfs_emit(buf, "%s\n", ptype); 264} 265static DEVICE_ATTR_RO(type); 266 267/* ------------------------------------------------------------------------- */ 268/* Alternate Modes */ 269 270static int altmode_match(struct device *dev, void *data) 271{ 272 struct typec_altmode *adev = to_typec_altmode(dev); 273 struct typec_device_id *id = data; 274 275 if (!is_typec_altmode(dev)) 276 return 0; 277 278 return ((adev->svid == id->svid) && (adev->mode == id->mode)); 279} 280 281static void typec_altmode_set_partner(struct altmode *altmode) 282{ 283 struct typec_altmode *adev = &altmode->adev; 284 struct typec_device_id id = { adev->svid, adev->mode, }; 285 struct typec_port *port = typec_altmode2port(adev); 286 struct altmode *partner; 287 struct device *dev; 288 289 dev = device_find_child(&port->dev, &id, altmode_match); 290 if (!dev) 291 return; 292 293 /* Bind the port alt mode to the partner/plug alt mode. */ 294 partner = to_altmode(to_typec_altmode(dev)); 295 altmode->partner = partner; 296 297 /* Bind the partner/plug alt mode to the port alt mode. */ 298 if (is_typec_plug(adev->dev.parent)) { 299 struct typec_plug *plug = to_typec_plug(adev->dev.parent); 300 301 partner->plug[plug->index] = altmode; 302 } else { 303 partner->partner = altmode; 304 } 305} 306 307static void typec_altmode_put_partner(struct altmode *altmode) 308{ 309 struct altmode *partner = altmode->partner; 310 struct typec_altmode *adev; 311 312 if (!partner) 313 return; 314 315 adev = &partner->adev; 316 317 if (is_typec_plug(adev->dev.parent)) { 318 struct typec_plug *plug = to_typec_plug(adev->dev.parent); 319 320 partner->plug[plug->index] = NULL; 321 } else { 322 partner->partner = NULL; 323 } 324 put_device(&adev->dev); 325} 326 327/** 328 * typec_altmode_update_active - Report Enter/Exit mode 329 * @adev: Handle to the alternate mode 330 * @active: True when the mode has been entered 331 * 332 * If a partner or cable plug executes Enter/Exit Mode command successfully, the 333 * drivers use this routine to report the updated state of the mode. 334 */ 335void typec_altmode_update_active(struct typec_altmode *adev, bool active) 336{ 337 char dir[6]; 338 339 if (adev->active == active) 340 return; 341 342 if (!is_typec_port(adev->dev.parent) && adev->dev.driver) { 343 if (!active) 344 module_put(adev->dev.driver->owner); 345 else 346 WARN_ON(!try_module_get(adev->dev.driver->owner)); 347 } 348 349 adev->active = active; 350 snprintf(dir, sizeof(dir), "mode%d", adev->mode); 351 sysfs_notify(&adev->dev.kobj, dir, "active"); 352 sysfs_notify(&adev->dev.kobj, NULL, "active"); 353 kobject_uevent(&adev->dev.kobj, KOBJ_CHANGE); 354} 355EXPORT_SYMBOL_GPL(typec_altmode_update_active); 356 357/** 358 * typec_altmode2port - Alternate Mode to USB Type-C port 359 * @alt: The Alternate Mode 360 * 361 * Returns handle to the port that a cable plug or partner with @alt is 362 * connected to. 363 */ 364struct typec_port *typec_altmode2port(struct typec_altmode *alt) 365{ 366 if (is_typec_plug(alt->dev.parent)) 367 return to_typec_port(alt->dev.parent->parent->parent); 368 if (is_typec_partner(alt->dev.parent)) 369 return to_typec_port(alt->dev.parent->parent); 370 if (is_typec_port(alt->dev.parent)) 371 return to_typec_port(alt->dev.parent); 372 373 return NULL; 374} 375EXPORT_SYMBOL_GPL(typec_altmode2port); 376 377static ssize_t 378vdo_show(struct device *dev, struct device_attribute *attr, char *buf) 379{ 380 struct typec_altmode *alt = to_typec_altmode(dev); 381 382 return sprintf(buf, "0x%08x\n", alt->vdo); 383} 384static DEVICE_ATTR_RO(vdo); 385 386static ssize_t 387description_show(struct device *dev, struct device_attribute *attr, char *buf) 388{ 389 struct typec_altmode *alt = to_typec_altmode(dev); 390 391 return sprintf(buf, "%s\n", alt->desc ? alt->desc : ""); 392} 393static DEVICE_ATTR_RO(description); 394 395static ssize_t 396active_show(struct device *dev, struct device_attribute *attr, char *buf) 397{ 398 struct typec_altmode *alt = to_typec_altmode(dev); 399 400 return sprintf(buf, "%s\n", alt->active ? "yes" : "no"); 401} 402 403static ssize_t active_store(struct device *dev, struct device_attribute *attr, 404 const char *buf, size_t size) 405{ 406 struct typec_altmode *adev = to_typec_altmode(dev); 407 struct altmode *altmode = to_altmode(adev); 408 bool enter; 409 int ret; 410 411 ret = kstrtobool(buf, &enter); 412 if (ret) 413 return ret; 414 415 if (adev->active == enter) 416 return size; 417 418 if (is_typec_port(adev->dev.parent)) { 419 typec_altmode_update_active(adev, enter); 420 421 /* Make sure that the partner exits the mode before disabling */ 422 if (altmode->partner && !enter && altmode->partner->adev.active) 423 typec_altmode_exit(&altmode->partner->adev); 424 } else if (altmode->partner) { 425 if (enter && !altmode->partner->adev.active) { 426 dev_warn(dev, "port has the mode disabled\n"); 427 return -EPERM; 428 } 429 } 430 431 /* Note: If there is no driver, the mode will not be entered */ 432 if (adev->ops && adev->ops->activate) { 433 ret = adev->ops->activate(adev, enter); 434 if (ret) 435 return ret; 436 } 437 438 return size; 439} 440static DEVICE_ATTR_RW(active); 441 442static ssize_t 443supported_roles_show(struct device *dev, struct device_attribute *attr, 444 char *buf) 445{ 446 struct altmode *alt = to_altmode(to_typec_altmode(dev)); 447 ssize_t ret; 448 449 switch (alt->roles) { 450 case TYPEC_PORT_SRC: 451 ret = sprintf(buf, "source\n"); 452 break; 453 case TYPEC_PORT_SNK: 454 ret = sprintf(buf, "sink\n"); 455 break; 456 case TYPEC_PORT_DRP: 457 default: 458 ret = sprintf(buf, "source sink\n"); 459 break; 460 } 461 return ret; 462} 463static DEVICE_ATTR_RO(supported_roles); 464 465static ssize_t 466mode_show(struct device *dev, struct device_attribute *attr, char *buf) 467{ 468 struct typec_altmode *adev = to_typec_altmode(dev); 469 470 return sprintf(buf, "%u\n", adev->mode); 471} 472static DEVICE_ATTR_RO(mode); 473 474static ssize_t 475svid_show(struct device *dev, struct device_attribute *attr, char *buf) 476{ 477 struct typec_altmode *adev = to_typec_altmode(dev); 478 479 return sprintf(buf, "%04x\n", adev->svid); 480} 481static DEVICE_ATTR_RO(svid); 482 483static struct attribute *typec_altmode_attrs[] = { 484 &dev_attr_active.attr, 485 &dev_attr_mode.attr, 486 &dev_attr_svid.attr, 487 &dev_attr_vdo.attr, 488 NULL 489}; 490 491static umode_t typec_altmode_attr_is_visible(struct kobject *kobj, 492 struct attribute *attr, int n) 493{ 494 struct typec_altmode *adev = to_typec_altmode(kobj_to_dev(kobj)); 495 496 if (attr == &dev_attr_active.attr) 497 if (!adev->ops || !adev->ops->activate) 498 return 0444; 499 500 return attr->mode; 501} 502 503static const struct attribute_group typec_altmode_group = { 504 .is_visible = typec_altmode_attr_is_visible, 505 .attrs = typec_altmode_attrs, 506}; 507 508static const struct attribute_group *typec_altmode_groups[] = { 509 &typec_altmode_group, 510 NULL 511}; 512 513static int altmode_id_get(struct device *dev) 514{ 515 struct ida *ids; 516 517 if (is_typec_partner(dev)) 518 ids = &to_typec_partner(dev)->mode_ids; 519 else if (is_typec_plug(dev)) 520 ids = &to_typec_plug(dev)->mode_ids; 521 else 522 ids = &to_typec_port(dev)->mode_ids; 523 524 return ida_simple_get(ids, 0, 0, GFP_KERNEL); 525} 526 527static void altmode_id_remove(struct device *dev, int id) 528{ 529 struct ida *ids; 530 531 if (is_typec_partner(dev)) 532 ids = &to_typec_partner(dev)->mode_ids; 533 else if (is_typec_plug(dev)) 534 ids = &to_typec_plug(dev)->mode_ids; 535 else 536 ids = &to_typec_port(dev)->mode_ids; 537 538 ida_simple_remove(ids, id); 539} 540 541static void typec_altmode_release(struct device *dev) 542{ 543 struct altmode *alt = to_altmode(to_typec_altmode(dev)); 544 545 typec_altmode_put_partner(alt); 546 547 altmode_id_remove(alt->adev.dev.parent, alt->id); 548 kfree(alt); 549} 550 551const struct device_type typec_altmode_dev_type = { 552 .name = "typec_alternate_mode", 553 .groups = typec_altmode_groups, 554 .release = typec_altmode_release, 555}; 556 557static struct typec_altmode * 558typec_register_altmode(struct device *parent, 559 const struct typec_altmode_desc *desc) 560{ 561 unsigned int id = altmode_id_get(parent); 562 bool is_port = is_typec_port(parent); 563 struct altmode *alt; 564 int ret; 565 566 alt = kzalloc(sizeof(*alt), GFP_KERNEL); 567 if (!alt) 568 return ERR_PTR(-ENOMEM); 569 570 alt->adev.svid = desc->svid; 571 alt->adev.mode = desc->mode; 572 alt->adev.vdo = desc->vdo; 573 alt->roles = desc->roles; 574 alt->id = id; 575 576 alt->attrs[0] = &dev_attr_vdo.attr; 577 alt->attrs[1] = &dev_attr_description.attr; 578 alt->attrs[2] = &dev_attr_active.attr; 579 580 if (is_port) { 581 alt->attrs[3] = &dev_attr_supported_roles.attr; 582 alt->adev.active = true; /* Enabled by default */ 583 } 584 585 sprintf(alt->group_name, "mode%d", desc->mode); 586 alt->group.name = alt->group_name; 587 alt->group.attrs = alt->attrs; 588 alt->groups[0] = &alt->group; 589 590 alt->adev.dev.parent = parent; 591 alt->adev.dev.groups = alt->groups; 592 alt->adev.dev.type = &typec_altmode_dev_type; 593 dev_set_name(&alt->adev.dev, "%s.%u", dev_name(parent), id); 594 595 /* Link partners and plugs with the ports */ 596 if (!is_port) 597 typec_altmode_set_partner(alt); 598 599 /* The partners are bind to drivers */ 600 if (is_typec_partner(parent)) 601 alt->adev.dev.bus = &typec_bus; 602 603 /* Plug alt modes need a class to generate udev events. */ 604 if (is_typec_plug(parent)) 605 alt->adev.dev.class = typec_class; 606 607 ret = device_register(&alt->adev.dev); 608 if (ret) { 609 dev_err(parent, "failed to register alternate mode (%d)\n", 610 ret); 611 put_device(&alt->adev.dev); 612 return ERR_PTR(ret); 613 } 614 615 return &alt->adev; 616} 617 618/** 619 * typec_unregister_altmode - Unregister Alternate Mode 620 * @adev: The alternate mode to be unregistered 621 * 622 * Unregister device created with typec_partner_register_altmode(), 623 * typec_plug_register_altmode() or typec_port_register_altmode(). 624 */ 625void typec_unregister_altmode(struct typec_altmode *adev) 626{ 627 if (IS_ERR_OR_NULL(adev)) 628 return; 629 typec_mux_put(to_altmode(adev)->mux); 630 device_unregister(&adev->dev); 631} 632EXPORT_SYMBOL_GPL(typec_unregister_altmode); 633 634/* ------------------------------------------------------------------------- */ 635/* Type-C Partners */ 636 637static ssize_t accessory_mode_show(struct device *dev, 638 struct device_attribute *attr, 639 char *buf) 640{ 641 struct typec_partner *p = to_typec_partner(dev); 642 643 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]); 644} 645static DEVICE_ATTR_RO(accessory_mode); 646 647static ssize_t supports_usb_power_delivery_show(struct device *dev, 648 struct device_attribute *attr, 649 char *buf) 650{ 651 struct typec_partner *p = to_typec_partner(dev); 652 653 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no"); 654} 655static DEVICE_ATTR_RO(supports_usb_power_delivery); 656 657static ssize_t number_of_alternate_modes_show(struct device *dev, struct device_attribute *attr, 658 char *buf) 659{ 660 struct typec_partner *partner; 661 struct typec_plug *plug; 662 int num_altmodes; 663 664 if (is_typec_partner(dev)) { 665 partner = to_typec_partner(dev); 666 num_altmodes = partner->num_altmodes; 667 } else if (is_typec_plug(dev)) { 668 plug = to_typec_plug(dev); 669 num_altmodes = plug->num_altmodes; 670 } else { 671 return 0; 672 } 673 674 return sysfs_emit(buf, "%d\n", num_altmodes); 675} 676static DEVICE_ATTR_RO(number_of_alternate_modes); 677 678static struct attribute *typec_partner_attrs[] = { 679 &dev_attr_accessory_mode.attr, 680 &dev_attr_supports_usb_power_delivery.attr, 681 &dev_attr_number_of_alternate_modes.attr, 682 &dev_attr_type.attr, 683 NULL 684}; 685 686static umode_t typec_partner_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n) 687{ 688 struct typec_partner *partner = to_typec_partner(kobj_to_dev(kobj)); 689 690 if (attr == &dev_attr_number_of_alternate_modes.attr) { 691 if (partner->num_altmodes < 0) 692 return 0; 693 } 694 695 if (attr == &dev_attr_type.attr) 696 if (!get_pd_product_type(kobj_to_dev(kobj))) 697 return 0; 698 699 return attr->mode; 700} 701 702static const struct attribute_group typec_partner_group = { 703 .is_visible = typec_partner_attr_is_visible, 704 .attrs = typec_partner_attrs 705}; 706 707static const struct attribute_group *typec_partner_groups[] = { 708 &typec_partner_group, 709 NULL 710}; 711 712static void typec_partner_release(struct device *dev) 713{ 714 struct typec_partner *partner = to_typec_partner(dev); 715 716 ida_destroy(&partner->mode_ids); 717 kfree(partner); 718} 719 720static const struct device_type typec_partner_dev_type = { 721 .name = "typec_partner", 722 .groups = typec_partner_groups, 723 .release = typec_partner_release, 724}; 725 726/** 727 * typec_partner_set_identity - Report result from Discover Identity command 728 * @partner: The partner updated identity values 729 * 730 * This routine is used to report that the result of Discover Identity USB power 731 * delivery command has become available. 732 */ 733int typec_partner_set_identity(struct typec_partner *partner) 734{ 735 if (!partner->identity) 736 return -EINVAL; 737 738 typec_report_identity(&partner->dev); 739 return 0; 740} 741EXPORT_SYMBOL_GPL(typec_partner_set_identity); 742 743/** 744 * typec_partner_set_num_altmodes - Set the number of available partner altmodes 745 * @partner: The partner to be updated. 746 * @num_altmodes: The number of altmodes we want to specify as available. 747 * 748 * This routine is used to report the number of alternate modes supported by the 749 * partner. This value is *not* enforced in alternate mode registration routines. 750 * 751 * @partner.num_altmodes is set to -1 on partner registration, denoting that 752 * a valid value has not been set for it yet. 753 * 754 * Returns 0 on success or negative error number on failure. 755 */ 756int typec_partner_set_num_altmodes(struct typec_partner *partner, int num_altmodes) 757{ 758 int ret; 759 760 if (num_altmodes < 0) 761 return -EINVAL; 762 763 partner->num_altmodes = num_altmodes; 764 ret = sysfs_update_group(&partner->dev.kobj, &typec_partner_group); 765 if (ret < 0) 766 return ret; 767 768 sysfs_notify(&partner->dev.kobj, NULL, "number_of_alternate_modes"); 769 kobject_uevent(&partner->dev.kobj, KOBJ_CHANGE); 770 771 return 0; 772} 773EXPORT_SYMBOL_GPL(typec_partner_set_num_altmodes); 774 775/** 776 * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode 777 * @partner: USB Type-C Partner that supports the alternate mode 778 * @desc: Description of the alternate mode 779 * 780 * This routine is used to register each alternate mode individually that 781 * @partner has listed in response to Discover SVIDs command. The modes for a 782 * SVID listed in response to Discover Modes command need to be listed in an 783 * array in @desc. 784 * 785 * Returns handle to the alternate mode on success or ERR_PTR on failure. 786 */ 787struct typec_altmode * 788typec_partner_register_altmode(struct typec_partner *partner, 789 const struct typec_altmode_desc *desc) 790{ 791 return typec_register_altmode(&partner->dev, desc); 792} 793EXPORT_SYMBOL_GPL(typec_partner_register_altmode); 794 795/** 796 * typec_register_partner - Register a USB Type-C Partner 797 * @port: The USB Type-C Port the partner is connected to 798 * @desc: Description of the partner 799 * 800 * Registers a device for USB Type-C Partner described in @desc. 801 * 802 * Returns handle to the partner on success or ERR_PTR on failure. 803 */ 804struct typec_partner *typec_register_partner(struct typec_port *port, 805 struct typec_partner_desc *desc) 806{ 807 struct typec_partner *partner; 808 int ret; 809 810 partner = kzalloc(sizeof(*partner), GFP_KERNEL); 811 if (!partner) 812 return ERR_PTR(-ENOMEM); 813 814 ida_init(&partner->mode_ids); 815 partner->usb_pd = desc->usb_pd; 816 partner->accessory = desc->accessory; 817 partner->num_altmodes = -1; 818 819 if (desc->identity) { 820 /* 821 * Creating directory for the identity only if the driver is 822 * able to provide data to it. 823 */ 824 partner->dev.groups = usb_pd_id_groups; 825 partner->identity = desc->identity; 826 } 827 828 partner->dev.class = typec_class; 829 partner->dev.parent = &port->dev; 830 partner->dev.type = &typec_partner_dev_type; 831 dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev)); 832 833 ret = device_register(&partner->dev); 834 if (ret) { 835 dev_err(&port->dev, "failed to register partner (%d)\n", ret); 836 put_device(&partner->dev); 837 return ERR_PTR(ret); 838 } 839 840 return partner; 841} 842EXPORT_SYMBOL_GPL(typec_register_partner); 843 844/** 845 * typec_unregister_partner - Unregister a USB Type-C Partner 846 * @partner: The partner to be unregistered 847 * 848 * Unregister device created with typec_register_partner(). 849 */ 850void typec_unregister_partner(struct typec_partner *partner) 851{ 852 if (!IS_ERR_OR_NULL(partner)) 853 device_unregister(&partner->dev); 854} 855EXPORT_SYMBOL_GPL(typec_unregister_partner); 856 857/* ------------------------------------------------------------------------- */ 858/* Type-C Cable Plugs */ 859 860static void typec_plug_release(struct device *dev) 861{ 862 struct typec_plug *plug = to_typec_plug(dev); 863 864 ida_destroy(&plug->mode_ids); 865 kfree(plug); 866} 867 868static struct attribute *typec_plug_attrs[] = { 869 &dev_attr_number_of_alternate_modes.attr, 870 NULL 871}; 872 873static umode_t typec_plug_attr_is_visible(struct kobject *kobj, struct attribute *attr, int n) 874{ 875 struct typec_plug *plug = to_typec_plug(kobj_to_dev(kobj)); 876 877 if (attr == &dev_attr_number_of_alternate_modes.attr) { 878 if (plug->num_altmodes < 0) 879 return 0; 880 } 881 882 return attr->mode; 883} 884 885static const struct attribute_group typec_plug_group = { 886 .is_visible = typec_plug_attr_is_visible, 887 .attrs = typec_plug_attrs 888}; 889 890static const struct attribute_group *typec_plug_groups[] = { 891 &typec_plug_group, 892 NULL 893}; 894 895static const struct device_type typec_plug_dev_type = { 896 .name = "typec_plug", 897 .groups = typec_plug_groups, 898 .release = typec_plug_release, 899}; 900 901/** 902 * typec_plug_set_num_altmodes - Set the number of available plug altmodes 903 * @plug: The plug to be updated. 904 * @num_altmodes: The number of altmodes we want to specify as available. 905 * 906 * This routine is used to report the number of alternate modes supported by the 907 * plug. This value is *not* enforced in alternate mode registration routines. 908 * 909 * @plug.num_altmodes is set to -1 on plug registration, denoting that 910 * a valid value has not been set for it yet. 911 * 912 * Returns 0 on success or negative error number on failure. 913 */ 914int typec_plug_set_num_altmodes(struct typec_plug *plug, int num_altmodes) 915{ 916 int ret; 917 918 if (num_altmodes < 0) 919 return -EINVAL; 920 921 plug->num_altmodes = num_altmodes; 922 ret = sysfs_update_group(&plug->dev.kobj, &typec_plug_group); 923 if (ret < 0) 924 return ret; 925 926 sysfs_notify(&plug->dev.kobj, NULL, "number_of_alternate_modes"); 927 kobject_uevent(&plug->dev.kobj, KOBJ_CHANGE); 928 929 return 0; 930} 931EXPORT_SYMBOL_GPL(typec_plug_set_num_altmodes); 932 933/** 934 * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode 935 * @plug: USB Type-C Cable Plug that supports the alternate mode 936 * @desc: Description of the alternate mode 937 * 938 * This routine is used to register each alternate mode individually that @plug 939 * has listed in response to Discover SVIDs command. The modes for a SVID that 940 * the plug lists in response to Discover Modes command need to be listed in an 941 * array in @desc. 942 * 943 * Returns handle to the alternate mode on success or ERR_PTR on failure. 944 */ 945struct typec_altmode * 946typec_plug_register_altmode(struct typec_plug *plug, 947 const struct typec_altmode_desc *desc) 948{ 949 return typec_register_altmode(&plug->dev, desc); 950} 951EXPORT_SYMBOL_GPL(typec_plug_register_altmode); 952 953/** 954 * typec_register_plug - Register a USB Type-C Cable Plug 955 * @cable: USB Type-C Cable with the plug 956 * @desc: Description of the cable plug 957 * 958 * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C 959 * Cable Plug represents a plug with electronics in it that can response to USB 960 * Power Delivery SOP Prime or SOP Double Prime packages. 961 * 962 * Returns handle to the cable plug on success or ERR_PTR on failure. 963 */ 964struct typec_plug *typec_register_plug(struct typec_cable *cable, 965 struct typec_plug_desc *desc) 966{ 967 struct typec_plug *plug; 968 char name[8]; 969 int ret; 970 971 plug = kzalloc(sizeof(*plug), GFP_KERNEL); 972 if (!plug) 973 return ERR_PTR(-ENOMEM); 974 975 sprintf(name, "plug%d", desc->index); 976 977 ida_init(&plug->mode_ids); 978 plug->num_altmodes = -1; 979 plug->index = desc->index; 980 plug->dev.class = typec_class; 981 plug->dev.parent = &cable->dev; 982 plug->dev.type = &typec_plug_dev_type; 983 dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name); 984 985 ret = device_register(&plug->dev); 986 if (ret) { 987 dev_err(&cable->dev, "failed to register plug (%d)\n", ret); 988 put_device(&plug->dev); 989 return ERR_PTR(ret); 990 } 991 992 return plug; 993} 994EXPORT_SYMBOL_GPL(typec_register_plug); 995 996/** 997 * typec_unregister_plug - Unregister a USB Type-C Cable Plug 998 * @plug: The cable plug to be unregistered 999 * 1000 * Unregister device created with typec_register_plug(). 1001 */ 1002void typec_unregister_plug(struct typec_plug *plug) 1003{ 1004 if (!IS_ERR_OR_NULL(plug)) 1005 device_unregister(&plug->dev); 1006} 1007EXPORT_SYMBOL_GPL(typec_unregister_plug); 1008 1009/* Type-C Cables */ 1010 1011static const char * const typec_plug_types[] = { 1012 [USB_PLUG_NONE] = "unknown", 1013 [USB_PLUG_TYPE_A] = "type-a", 1014 [USB_PLUG_TYPE_B] = "type-b", 1015 [USB_PLUG_TYPE_C] = "type-c", 1016 [USB_PLUG_CAPTIVE] = "captive", 1017}; 1018 1019static ssize_t plug_type_show(struct device *dev, 1020 struct device_attribute *attr, char *buf) 1021{ 1022 struct typec_cable *cable = to_typec_cable(dev); 1023 1024 return sprintf(buf, "%s\n", typec_plug_types[cable->type]); 1025} 1026static DEVICE_ATTR_RO(plug_type); 1027 1028static struct attribute *typec_cable_attrs[] = { 1029 &dev_attr_type.attr, 1030 &dev_attr_plug_type.attr, 1031 NULL 1032}; 1033ATTRIBUTE_GROUPS(typec_cable); 1034 1035static void typec_cable_release(struct device *dev) 1036{ 1037 struct typec_cable *cable = to_typec_cable(dev); 1038 1039 kfree(cable); 1040} 1041 1042static const struct device_type typec_cable_dev_type = { 1043 .name = "typec_cable", 1044 .groups = typec_cable_groups, 1045 .release = typec_cable_release, 1046}; 1047 1048static int cable_match(struct device *dev, void *data) 1049{ 1050 return is_typec_cable(dev); 1051} 1052 1053/** 1054 * typec_cable_get - Get a reference to the USB Type-C cable 1055 * @port: The USB Type-C Port the cable is connected to 1056 * 1057 * The caller must decrement the reference count with typec_cable_put() after 1058 * use. 1059 */ 1060struct typec_cable *typec_cable_get(struct typec_port *port) 1061{ 1062 struct device *dev; 1063 1064 dev = device_find_child(&port->dev, NULL, cable_match); 1065 if (!dev) 1066 return NULL; 1067 1068 return to_typec_cable(dev); 1069} 1070EXPORT_SYMBOL_GPL(typec_cable_get); 1071 1072/** 1073 * typec_cable_put - Decrement the reference count on USB Type-C cable 1074 * @cable: The USB Type-C cable 1075 */ 1076void typec_cable_put(struct typec_cable *cable) 1077{ 1078 put_device(&cable->dev); 1079} 1080EXPORT_SYMBOL_GPL(typec_cable_put); 1081 1082/** 1083 * typec_cable_is_active - Check is the USB Type-C cable active or passive 1084 * @cable: The USB Type-C Cable 1085 * 1086 * Return 1 if the cable is active or 0 if it's passive. 1087 */ 1088int typec_cable_is_active(struct typec_cable *cable) 1089{ 1090 return cable->active; 1091} 1092EXPORT_SYMBOL_GPL(typec_cable_is_active); 1093 1094/** 1095 * typec_cable_set_identity - Report result from Discover Identity command 1096 * @cable: The cable updated identity values 1097 * 1098 * This routine is used to report that the result of Discover Identity USB power 1099 * delivery command has become available. 1100 */ 1101int typec_cable_set_identity(struct typec_cable *cable) 1102{ 1103 if (!cable->identity) 1104 return -EINVAL; 1105 1106 typec_report_identity(&cable->dev); 1107 return 0; 1108} 1109EXPORT_SYMBOL_GPL(typec_cable_set_identity); 1110 1111/** 1112 * typec_register_cable - Register a USB Type-C Cable 1113 * @port: The USB Type-C Port the cable is connected to 1114 * @desc: Description of the cable 1115 * 1116 * Registers a device for USB Type-C Cable described in @desc. The cable will be 1117 * parent for the optional cable plug devises. 1118 * 1119 * Returns handle to the cable on success or ERR_PTR on failure. 1120 */ 1121struct typec_cable *typec_register_cable(struct typec_port *port, 1122 struct typec_cable_desc *desc) 1123{ 1124 struct typec_cable *cable; 1125 int ret; 1126 1127 cable = kzalloc(sizeof(*cable), GFP_KERNEL); 1128 if (!cable) 1129 return ERR_PTR(-ENOMEM); 1130 1131 cable->type = desc->type; 1132 cable->active = desc->active; 1133 1134 if (desc->identity) { 1135 /* 1136 * Creating directory for the identity only if the driver is 1137 * able to provide data to it. 1138 */ 1139 cable->dev.groups = usb_pd_id_groups; 1140 cable->identity = desc->identity; 1141 } 1142 1143 cable->dev.class = typec_class; 1144 cable->dev.parent = &port->dev; 1145 cable->dev.type = &typec_cable_dev_type; 1146 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev)); 1147 1148 ret = device_register(&cable->dev); 1149 if (ret) { 1150 dev_err(&port->dev, "failed to register cable (%d)\n", ret); 1151 put_device(&cable->dev); 1152 return ERR_PTR(ret); 1153 } 1154 1155 return cable; 1156} 1157EXPORT_SYMBOL_GPL(typec_register_cable); 1158 1159/** 1160 * typec_unregister_cable - Unregister a USB Type-C Cable 1161 * @cable: The cable to be unregistered 1162 * 1163 * Unregister device created with typec_register_cable(). 1164 */ 1165void typec_unregister_cable(struct typec_cable *cable) 1166{ 1167 if (!IS_ERR_OR_NULL(cable)) 1168 device_unregister(&cable->dev); 1169} 1170EXPORT_SYMBOL_GPL(typec_unregister_cable); 1171 1172/* ------------------------------------------------------------------------- */ 1173/* USB Type-C ports */ 1174 1175static const char * const typec_orientations[] = { 1176 [TYPEC_ORIENTATION_NONE] = "unknown", 1177 [TYPEC_ORIENTATION_NORMAL] = "normal", 1178 [TYPEC_ORIENTATION_REVERSE] = "reverse", 1179}; 1180 1181static const char * const typec_roles[] = { 1182 [TYPEC_SINK] = "sink", 1183 [TYPEC_SOURCE] = "source", 1184}; 1185 1186static const char * const typec_data_roles[] = { 1187 [TYPEC_DEVICE] = "device", 1188 [TYPEC_HOST] = "host", 1189}; 1190 1191static const char * const typec_port_power_roles[] = { 1192 [TYPEC_PORT_SRC] = "source", 1193 [TYPEC_PORT_SNK] = "sink", 1194 [TYPEC_PORT_DRP] = "dual", 1195}; 1196 1197static const char * const typec_port_data_roles[] = { 1198 [TYPEC_PORT_DFP] = "host", 1199 [TYPEC_PORT_UFP] = "device", 1200 [TYPEC_PORT_DRD] = "dual", 1201}; 1202 1203static const char * const typec_port_types_drp[] = { 1204 [TYPEC_PORT_SRC] = "dual [source] sink", 1205 [TYPEC_PORT_SNK] = "dual source [sink]", 1206 [TYPEC_PORT_DRP] = "[dual] source sink", 1207}; 1208 1209static ssize_t 1210preferred_role_store(struct device *dev, struct device_attribute *attr, 1211 const char *buf, size_t size) 1212{ 1213 struct typec_port *port = to_typec_port(dev); 1214 int role; 1215 int ret; 1216 1217 if (port->cap->type != TYPEC_PORT_DRP) { 1218 dev_dbg(dev, "Preferred role only supported with DRP ports\n"); 1219 return -EOPNOTSUPP; 1220 } 1221 1222 if (!port->ops || !port->ops->try_role) { 1223 dev_dbg(dev, "Setting preferred role not supported\n"); 1224 return -EOPNOTSUPP; 1225 } 1226 1227 role = sysfs_match_string(typec_roles, buf); 1228 if (role < 0) { 1229 if (sysfs_streq(buf, "none")) 1230 role = TYPEC_NO_PREFERRED_ROLE; 1231 else 1232 return -EINVAL; 1233 } 1234 1235 ret = port->ops->try_role(port, role); 1236 if (ret) 1237 return ret; 1238 1239 port->prefer_role = role; 1240 return size; 1241} 1242 1243static ssize_t 1244preferred_role_show(struct device *dev, struct device_attribute *attr, 1245 char *buf) 1246{ 1247 struct typec_port *port = to_typec_port(dev); 1248 1249 if (port->cap->type != TYPEC_PORT_DRP) 1250 return 0; 1251 1252 if (port->prefer_role < 0) 1253 return 0; 1254 1255 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]); 1256} 1257static DEVICE_ATTR_RW(preferred_role); 1258 1259static ssize_t data_role_store(struct device *dev, 1260 struct device_attribute *attr, 1261 const char *buf, size_t size) 1262{ 1263 struct typec_port *port = to_typec_port(dev); 1264 int ret; 1265 1266 if (!port->ops || !port->ops->dr_set) { 1267 dev_dbg(dev, "data role swapping not supported\n"); 1268 return -EOPNOTSUPP; 1269 } 1270 1271 ret = sysfs_match_string(typec_data_roles, buf); 1272 if (ret < 0) 1273 return ret; 1274 1275 mutex_lock(&port->port_type_lock); 1276 if (port->cap->data != TYPEC_PORT_DRD) { 1277 ret = -EOPNOTSUPP; 1278 goto unlock_and_ret; 1279 } 1280 1281 ret = port->ops->dr_set(port, ret); 1282 if (ret) 1283 goto unlock_and_ret; 1284 1285 ret = size; 1286unlock_and_ret: 1287 mutex_unlock(&port->port_type_lock); 1288 return ret; 1289} 1290 1291static ssize_t data_role_show(struct device *dev, 1292 struct device_attribute *attr, char *buf) 1293{ 1294 struct typec_port *port = to_typec_port(dev); 1295 1296 if (port->cap->data == TYPEC_PORT_DRD) 1297 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ? 1298 "[host] device" : "host [device]"); 1299 1300 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]); 1301} 1302static DEVICE_ATTR_RW(data_role); 1303 1304static ssize_t power_role_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 int ret; 1310 1311 if (!port->ops || !port->ops->pr_set) { 1312 dev_dbg(dev, "power role swapping not supported\n"); 1313 return -EOPNOTSUPP; 1314 } 1315 1316 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) { 1317 dev_dbg(dev, "partner unable to swap power role\n"); 1318 return -EIO; 1319 } 1320 1321 ret = sysfs_match_string(typec_roles, buf); 1322 if (ret < 0) 1323 return ret; 1324 1325 mutex_lock(&port->port_type_lock); 1326 if (port->port_type != TYPEC_PORT_DRP) { 1327 dev_dbg(dev, "port type fixed at \"%s\"", 1328 typec_port_power_roles[port->port_type]); 1329 ret = -EOPNOTSUPP; 1330 goto unlock_and_ret; 1331 } 1332 1333 ret = port->ops->pr_set(port, ret); 1334 if (ret) 1335 goto unlock_and_ret; 1336 1337 ret = size; 1338unlock_and_ret: 1339 mutex_unlock(&port->port_type_lock); 1340 return ret; 1341} 1342 1343static ssize_t power_role_show(struct device *dev, 1344 struct device_attribute *attr, char *buf) 1345{ 1346 struct typec_port *port = to_typec_port(dev); 1347 1348 if (port->cap->type == TYPEC_PORT_DRP) 1349 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ? 1350 "[source] sink" : "source [sink]"); 1351 1352 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]); 1353} 1354static DEVICE_ATTR_RW(power_role); 1355 1356static ssize_t 1357port_type_store(struct device *dev, struct device_attribute *attr, 1358 const char *buf, size_t size) 1359{ 1360 struct typec_port *port = to_typec_port(dev); 1361 int ret; 1362 enum typec_port_type type; 1363 1364 if (port->cap->type != TYPEC_PORT_DRP || 1365 !port->ops || !port->ops->port_type_set) { 1366 dev_dbg(dev, "changing port type not supported\n"); 1367 return -EOPNOTSUPP; 1368 } 1369 1370 ret = sysfs_match_string(typec_port_power_roles, buf); 1371 if (ret < 0) 1372 return ret; 1373 1374 type = ret; 1375 mutex_lock(&port->port_type_lock); 1376 1377 if (port->port_type == type) { 1378 ret = size; 1379 goto unlock_and_ret; 1380 } 1381 1382 ret = port->ops->port_type_set(port, type); 1383 if (ret) 1384 goto unlock_and_ret; 1385 1386 port->port_type = type; 1387 ret = size; 1388 1389unlock_and_ret: 1390 mutex_unlock(&port->port_type_lock); 1391 return ret; 1392} 1393 1394static ssize_t 1395port_type_show(struct device *dev, struct device_attribute *attr, 1396 char *buf) 1397{ 1398 struct typec_port *port = to_typec_port(dev); 1399 1400 if (port->cap->type == TYPEC_PORT_DRP) 1401 return sprintf(buf, "%s\n", 1402 typec_port_types_drp[port->port_type]); 1403 1404 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]); 1405} 1406static DEVICE_ATTR_RW(port_type); 1407 1408static const char * const typec_pwr_opmodes[] = { 1409 [TYPEC_PWR_MODE_USB] = "default", 1410 [TYPEC_PWR_MODE_1_5A] = "1.5A", 1411 [TYPEC_PWR_MODE_3_0A] = "3.0A", 1412 [TYPEC_PWR_MODE_PD] = "usb_power_delivery", 1413}; 1414 1415static ssize_t power_operation_mode_show(struct device *dev, 1416 struct device_attribute *attr, 1417 char *buf) 1418{ 1419 struct typec_port *port = to_typec_port(dev); 1420 1421 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]); 1422} 1423static DEVICE_ATTR_RO(power_operation_mode); 1424 1425static ssize_t vconn_source_store(struct device *dev, 1426 struct device_attribute *attr, 1427 const char *buf, size_t size) 1428{ 1429 struct typec_port *port = to_typec_port(dev); 1430 bool source; 1431 int ret; 1432 1433 if (!port->cap->pd_revision) { 1434 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n"); 1435 return -EOPNOTSUPP; 1436 } 1437 1438 if (!port->ops || !port->ops->vconn_set) { 1439 dev_dbg(dev, "VCONN swapping not supported\n"); 1440 return -EOPNOTSUPP; 1441 } 1442 1443 ret = kstrtobool(buf, &source); 1444 if (ret) 1445 return ret; 1446 1447 ret = port->ops->vconn_set(port, (enum typec_role)source); 1448 if (ret) 1449 return ret; 1450 1451 return size; 1452} 1453 1454static ssize_t vconn_source_show(struct device *dev, 1455 struct device_attribute *attr, char *buf) 1456{ 1457 struct typec_port *port = to_typec_port(dev); 1458 1459 return sprintf(buf, "%s\n", 1460 port->vconn_role == TYPEC_SOURCE ? "yes" : "no"); 1461} 1462static DEVICE_ATTR_RW(vconn_source); 1463 1464static ssize_t supported_accessory_modes_show(struct device *dev, 1465 struct device_attribute *attr, 1466 char *buf) 1467{ 1468 struct typec_port *port = to_typec_port(dev); 1469 ssize_t ret = 0; 1470 int i; 1471 1472 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) { 1473 if (port->cap->accessory[i]) 1474 ret += sprintf(buf + ret, "%s ", 1475 typec_accessory_modes[port->cap->accessory[i]]); 1476 } 1477 1478 if (!ret) 1479 return sprintf(buf, "none\n"); 1480 1481 buf[ret - 1] = '\n'; 1482 1483 return ret; 1484} 1485static DEVICE_ATTR_RO(supported_accessory_modes); 1486 1487static ssize_t usb_typec_revision_show(struct device *dev, 1488 struct device_attribute *attr, 1489 char *buf) 1490{ 1491 struct typec_port *port = to_typec_port(dev); 1492 u16 rev = port->cap->revision; 1493 1494 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf); 1495} 1496static DEVICE_ATTR_RO(usb_typec_revision); 1497 1498static ssize_t usb_power_delivery_revision_show(struct device *dev, 1499 struct device_attribute *attr, 1500 char *buf) 1501{ 1502 struct typec_port *p = to_typec_port(dev); 1503 1504 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff); 1505} 1506static DEVICE_ATTR_RO(usb_power_delivery_revision); 1507 1508static ssize_t orientation_show(struct device *dev, 1509 struct device_attribute *attr, 1510 char *buf) 1511{ 1512 struct typec_port *port = to_typec_port(dev); 1513 1514 return sprintf(buf, "%s\n", typec_orientations[port->orientation]); 1515} 1516static DEVICE_ATTR_RO(orientation); 1517 1518static struct attribute *typec_attrs[] = { 1519 &dev_attr_data_role.attr, 1520 &dev_attr_power_operation_mode.attr, 1521 &dev_attr_power_role.attr, 1522 &dev_attr_preferred_role.attr, 1523 &dev_attr_supported_accessory_modes.attr, 1524 &dev_attr_usb_power_delivery_revision.attr, 1525 &dev_attr_usb_typec_revision.attr, 1526 &dev_attr_vconn_source.attr, 1527 &dev_attr_port_type.attr, 1528 &dev_attr_orientation.attr, 1529 NULL, 1530}; 1531 1532static umode_t typec_attr_is_visible(struct kobject *kobj, 1533 struct attribute *attr, int n) 1534{ 1535 struct typec_port *port = to_typec_port(kobj_to_dev(kobj)); 1536 1537 if (attr == &dev_attr_data_role.attr) { 1538 if (port->cap->data != TYPEC_PORT_DRD || 1539 !port->ops || !port->ops->dr_set) 1540 return 0444; 1541 } else if (attr == &dev_attr_power_role.attr) { 1542 if (port->cap->type != TYPEC_PORT_DRP || 1543 !port->ops || !port->ops->pr_set) 1544 return 0444; 1545 } else if (attr == &dev_attr_vconn_source.attr) { 1546 if (!port->cap->pd_revision || 1547 !port->ops || !port->ops->vconn_set) 1548 return 0444; 1549 } else if (attr == &dev_attr_preferred_role.attr) { 1550 if (port->cap->type != TYPEC_PORT_DRP || 1551 !port->ops || !port->ops->try_role) 1552 return 0444; 1553 } else if (attr == &dev_attr_port_type.attr) { 1554 if (!port->ops || !port->ops->port_type_set) 1555 return 0; 1556 if (port->cap->type != TYPEC_PORT_DRP) 1557 return 0444; 1558 } else if (attr == &dev_attr_orientation.attr) { 1559 if (port->cap->orientation_aware) 1560 return 0444; 1561 return 0; 1562 } 1563 1564 return attr->mode; 1565} 1566 1567static const struct attribute_group typec_group = { 1568 .is_visible = typec_attr_is_visible, 1569 .attrs = typec_attrs, 1570}; 1571 1572static const struct attribute_group *typec_groups[] = { 1573 &typec_group, 1574 NULL 1575}; 1576 1577static int typec_uevent(struct device *dev, struct kobj_uevent_env *env) 1578{ 1579 int ret; 1580 1581 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev)); 1582 if (ret) 1583 dev_err(dev, "failed to add uevent TYPEC_PORT\n"); 1584 1585 return ret; 1586} 1587 1588static void typec_release(struct device *dev) 1589{ 1590 struct typec_port *port = to_typec_port(dev); 1591 1592 ida_simple_remove(&typec_index_ida, port->id); 1593 ida_destroy(&port->mode_ids); 1594 typec_switch_put(port->sw); 1595 typec_mux_put(port->mux); 1596 kfree(port->cap); 1597 kfree(port); 1598} 1599 1600const struct device_type typec_port_dev_type = { 1601 .name = "typec_port", 1602 .groups = typec_groups, 1603 .uevent = typec_uevent, 1604 .release = typec_release, 1605}; 1606 1607/* --------------------------------------- */ 1608/* Driver callbacks to report role updates */ 1609 1610static int partner_match(struct device *dev, void *data) 1611{ 1612 return is_typec_partner(dev); 1613} 1614 1615/** 1616 * typec_set_data_role - Report data role change 1617 * @port: The USB Type-C Port where the role was changed 1618 * @role: The new data role 1619 * 1620 * This routine is used by the port drivers to report data role changes. 1621 */ 1622void typec_set_data_role(struct typec_port *port, enum typec_data_role role) 1623{ 1624 struct device *partner_dev; 1625 1626 if (port->data_role == role) 1627 return; 1628 1629 port->data_role = role; 1630 sysfs_notify(&port->dev.kobj, NULL, "data_role"); 1631 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 1632 1633 partner_dev = device_find_child(&port->dev, NULL, partner_match); 1634 if (!partner_dev) 1635 return; 1636 1637 if (to_typec_partner(partner_dev)->identity) 1638 typec_product_type_notify(partner_dev); 1639 1640 put_device(partner_dev); 1641} 1642EXPORT_SYMBOL_GPL(typec_set_data_role); 1643 1644/** 1645 * typec_set_pwr_role - Report power role change 1646 * @port: The USB Type-C Port where the role was changed 1647 * @role: The new data role 1648 * 1649 * This routine is used by the port drivers to report power role changes. 1650 */ 1651void typec_set_pwr_role(struct typec_port *port, enum typec_role role) 1652{ 1653 if (port->pwr_role == role) 1654 return; 1655 1656 port->pwr_role = role; 1657 sysfs_notify(&port->dev.kobj, NULL, "power_role"); 1658 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 1659} 1660EXPORT_SYMBOL_GPL(typec_set_pwr_role); 1661 1662/** 1663 * typec_set_vconn_role - Report VCONN source change 1664 * @port: The USB Type-C Port which VCONN role changed 1665 * @role: Source when @port is sourcing VCONN, or Sink when it's not 1666 * 1667 * This routine is used by the port drivers to report if the VCONN source is 1668 * changes. 1669 */ 1670void typec_set_vconn_role(struct typec_port *port, enum typec_role role) 1671{ 1672 if (port->vconn_role == role) 1673 return; 1674 1675 port->vconn_role = role; 1676 sysfs_notify(&port->dev.kobj, NULL, "vconn_source"); 1677 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 1678} 1679EXPORT_SYMBOL_GPL(typec_set_vconn_role); 1680 1681/** 1682 * typec_set_pwr_opmode - Report changed power operation mode 1683 * @port: The USB Type-C Port where the mode was changed 1684 * @opmode: New power operation mode 1685 * 1686 * This routine is used by the port drivers to report changed power operation 1687 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB 1688 * Type-C specification, and "USB Power Delivery" when the power levels are 1689 * negotiated with methods defined in USB Power Delivery specification. 1690 */ 1691void typec_set_pwr_opmode(struct typec_port *port, 1692 enum typec_pwr_opmode opmode) 1693{ 1694 struct device *partner_dev; 1695 1696 if (port->pwr_opmode == opmode) 1697 return; 1698 1699 port->pwr_opmode = opmode; 1700 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode"); 1701 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 1702 1703 partner_dev = device_find_child(&port->dev, NULL, partner_match); 1704 if (partner_dev) { 1705 struct typec_partner *partner = to_typec_partner(partner_dev); 1706 1707 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) { 1708 partner->usb_pd = 1; 1709 sysfs_notify(&partner_dev->kobj, NULL, 1710 "supports_usb_power_delivery"); 1711 } 1712 put_device(partner_dev); 1713 } 1714} 1715EXPORT_SYMBOL_GPL(typec_set_pwr_opmode); 1716 1717/** 1718 * typec_find_pwr_opmode - Get the typec power operation mode capability 1719 * @name: power operation mode string 1720 * 1721 * This routine is used to find the typec_pwr_opmode by its string @name. 1722 * 1723 * Returns typec_pwr_opmode if success, otherwise negative error code. 1724 */ 1725int typec_find_pwr_opmode(const char *name) 1726{ 1727 return match_string(typec_pwr_opmodes, 1728 ARRAY_SIZE(typec_pwr_opmodes), name); 1729} 1730EXPORT_SYMBOL_GPL(typec_find_pwr_opmode); 1731 1732/** 1733 * typec_find_orientation - Convert orientation string to enum typec_orientation 1734 * @name: Orientation string 1735 * 1736 * This routine is used to find the typec_orientation by its string name @name. 1737 * 1738 * Returns the orientation value on success, otherwise negative error code. 1739 */ 1740int typec_find_orientation(const char *name) 1741{ 1742 return match_string(typec_orientations, ARRAY_SIZE(typec_orientations), 1743 name); 1744} 1745EXPORT_SYMBOL_GPL(typec_find_orientation); 1746 1747/** 1748 * typec_find_port_power_role - Get the typec port power capability 1749 * @name: port power capability string 1750 * 1751 * This routine is used to find the typec_port_type by its string name. 1752 * 1753 * Returns typec_port_type if success, otherwise negative error code. 1754 */ 1755int typec_find_port_power_role(const char *name) 1756{ 1757 return match_string(typec_port_power_roles, 1758 ARRAY_SIZE(typec_port_power_roles), name); 1759} 1760EXPORT_SYMBOL_GPL(typec_find_port_power_role); 1761 1762/** 1763 * typec_find_power_role - Find the typec one specific power role 1764 * @name: power role string 1765 * 1766 * This routine is used to find the typec_role by its string name. 1767 * 1768 * Returns typec_role if success, otherwise negative error code. 1769 */ 1770int typec_find_power_role(const char *name) 1771{ 1772 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name); 1773} 1774EXPORT_SYMBOL_GPL(typec_find_power_role); 1775 1776/** 1777 * typec_find_port_data_role - Get the typec port data capability 1778 * @name: port data capability string 1779 * 1780 * This routine is used to find the typec_port_data by its string name. 1781 * 1782 * Returns typec_port_data if success, otherwise negative error code. 1783 */ 1784int typec_find_port_data_role(const char *name) 1785{ 1786 return match_string(typec_port_data_roles, 1787 ARRAY_SIZE(typec_port_data_roles), name); 1788} 1789EXPORT_SYMBOL_GPL(typec_find_port_data_role); 1790 1791/* ------------------------------------------ */ 1792/* API for Multiplexer/DeMultiplexer Switches */ 1793 1794/** 1795 * typec_set_orientation - Set USB Type-C cable plug orientation 1796 * @port: USB Type-C Port 1797 * @orientation: USB Type-C cable plug orientation 1798 * 1799 * Set cable plug orientation for @port. 1800 */ 1801int typec_set_orientation(struct typec_port *port, 1802 enum typec_orientation orientation) 1803{ 1804 int ret; 1805 1806 ret = typec_switch_set(port->sw, orientation); 1807 if (ret) 1808 return ret; 1809 1810 port->orientation = orientation; 1811 sysfs_notify(&port->dev.kobj, NULL, "orientation"); 1812 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE); 1813 1814 return 0; 1815} 1816EXPORT_SYMBOL_GPL(typec_set_orientation); 1817 1818/** 1819 * typec_get_orientation - Get USB Type-C cable plug orientation 1820 * @port: USB Type-C Port 1821 * 1822 * Get current cable plug orientation for @port. 1823 */ 1824enum typec_orientation typec_get_orientation(struct typec_port *port) 1825{ 1826 return port->orientation; 1827} 1828EXPORT_SYMBOL_GPL(typec_get_orientation); 1829 1830/** 1831 * typec_set_mode - Set mode of operation for USB Type-C connector 1832 * @port: USB Type-C connector 1833 * @mode: Accessory Mode, USB Operation or Safe State 1834 * 1835 * Configure @port for Accessory Mode @mode. This function will configure the 1836 * muxes needed for @mode. 1837 */ 1838int typec_set_mode(struct typec_port *port, int mode) 1839{ 1840 struct typec_mux_state state = { }; 1841 1842 state.mode = mode; 1843 1844 return typec_mux_set(port->mux, &state); 1845} 1846EXPORT_SYMBOL_GPL(typec_set_mode); 1847 1848/* --------------------------------------- */ 1849 1850/** 1851 * typec_get_drvdata - Return private driver data pointer 1852 * @port: USB Type-C port 1853 */ 1854void *typec_get_drvdata(struct typec_port *port) 1855{ 1856 return dev_get_drvdata(&port->dev); 1857} 1858EXPORT_SYMBOL_GPL(typec_get_drvdata); 1859 1860/** 1861 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode 1862 * @port: USB Type-C Port that supports the alternate mode 1863 * @desc: Description of the alternate mode 1864 * 1865 * This routine is used to register an alternate mode that @port is capable of 1866 * supporting. 1867 * 1868 * Returns handle to the alternate mode on success or ERR_PTR on failure. 1869 */ 1870struct typec_altmode * 1871typec_port_register_altmode(struct typec_port *port, 1872 const struct typec_altmode_desc *desc) 1873{ 1874 struct typec_altmode *adev; 1875 struct typec_mux *mux; 1876 1877 mux = typec_mux_get(&port->dev, desc); 1878 if (IS_ERR(mux)) 1879 return ERR_CAST(mux); 1880 1881 adev = typec_register_altmode(&port->dev, desc); 1882 if (IS_ERR(adev)) 1883 typec_mux_put(mux); 1884 else 1885 to_altmode(adev)->mux = mux; 1886 1887 return adev; 1888} 1889EXPORT_SYMBOL_GPL(typec_port_register_altmode); 1890 1891/** 1892 * typec_register_port - Register a USB Type-C Port 1893 * @parent: Parent device 1894 * @cap: Description of the port 1895 * 1896 * Registers a device for USB Type-C Port described in @cap. 1897 * 1898 * Returns handle to the port on success or ERR_PTR on failure. 1899 */ 1900struct typec_port *typec_register_port(struct device *parent, 1901 const struct typec_capability *cap) 1902{ 1903 struct typec_port *port; 1904 int ret; 1905 int id; 1906 1907 port = kzalloc(sizeof(*port), GFP_KERNEL); 1908 if (!port) 1909 return ERR_PTR(-ENOMEM); 1910 1911 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL); 1912 if (id < 0) { 1913 kfree(port); 1914 return ERR_PTR(id); 1915 } 1916 1917 switch (cap->type) { 1918 case TYPEC_PORT_SRC: 1919 port->pwr_role = TYPEC_SOURCE; 1920 port->vconn_role = TYPEC_SOURCE; 1921 break; 1922 case TYPEC_PORT_SNK: 1923 port->pwr_role = TYPEC_SINK; 1924 port->vconn_role = TYPEC_SINK; 1925 break; 1926 case TYPEC_PORT_DRP: 1927 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE) 1928 port->pwr_role = cap->prefer_role; 1929 else 1930 port->pwr_role = TYPEC_SINK; 1931 break; 1932 } 1933 1934 switch (cap->data) { 1935 case TYPEC_PORT_DFP: 1936 port->data_role = TYPEC_HOST; 1937 break; 1938 case TYPEC_PORT_UFP: 1939 port->data_role = TYPEC_DEVICE; 1940 break; 1941 case TYPEC_PORT_DRD: 1942 if (cap->prefer_role == TYPEC_SOURCE) 1943 port->data_role = TYPEC_HOST; 1944 else 1945 port->data_role = TYPEC_DEVICE; 1946 break; 1947 } 1948 1949 ida_init(&port->mode_ids); 1950 mutex_init(&port->port_type_lock); 1951 1952 port->id = id; 1953 port->ops = cap->ops; 1954 port->port_type = cap->type; 1955 port->prefer_role = cap->prefer_role; 1956 1957 device_initialize(&port->dev); 1958 port->dev.class = typec_class; 1959 port->dev.parent = parent; 1960 port->dev.fwnode = cap->fwnode; 1961 port->dev.type = &typec_port_dev_type; 1962 dev_set_name(&port->dev, "port%d", id); 1963 dev_set_drvdata(&port->dev, cap->driver_data); 1964 1965 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL); 1966 if (!port->cap) { 1967 put_device(&port->dev); 1968 return ERR_PTR(-ENOMEM); 1969 } 1970 1971 port->sw = typec_switch_get(&port->dev); 1972 if (IS_ERR(port->sw)) { 1973 ret = PTR_ERR(port->sw); 1974 put_device(&port->dev); 1975 return ERR_PTR(ret); 1976 } 1977 1978 port->mux = typec_mux_get(&port->dev, NULL); 1979 if (IS_ERR(port->mux)) { 1980 ret = PTR_ERR(port->mux); 1981 put_device(&port->dev); 1982 return ERR_PTR(ret); 1983 } 1984 1985 ret = device_add(&port->dev); 1986 if (ret) { 1987 dev_err(parent, "failed to register port (%d)\n", ret); 1988 put_device(&port->dev); 1989 return ERR_PTR(ret); 1990 } 1991 1992 return port; 1993} 1994EXPORT_SYMBOL_GPL(typec_register_port); 1995 1996/** 1997 * typec_unregister_port - Unregister a USB Type-C Port 1998 * @port: The port to be unregistered 1999 * 2000 * Unregister device created with typec_register_port(). 2001 */ 2002void typec_unregister_port(struct typec_port *port) 2003{ 2004 if (!IS_ERR_OR_NULL(port)) 2005 device_unregister(&port->dev); 2006} 2007EXPORT_SYMBOL_GPL(typec_unregister_port); 2008 2009static int __init typec_init(void) 2010{ 2011 int ret; 2012 2013 ret = bus_register(&typec_bus); 2014 if (ret) 2015 return ret; 2016 2017 ret = class_register(&typec_mux_class); 2018 if (ret) 2019 goto err_unregister_bus; 2020 2021 typec_class = class_create(THIS_MODULE, "typec"); 2022 if (IS_ERR(typec_class)) { 2023 ret = PTR_ERR(typec_class); 2024 goto err_unregister_mux_class; 2025 } 2026 2027 return 0; 2028 2029err_unregister_mux_class: 2030 class_unregister(&typec_mux_class); 2031 2032err_unregister_bus: 2033 bus_unregister(&typec_bus); 2034 2035 return ret; 2036} 2037subsys_initcall(typec_init); 2038 2039static void __exit typec_exit(void) 2040{ 2041 class_destroy(typec_class); 2042 ida_destroy(&typec_index_ida); 2043 bus_unregister(&typec_bus); 2044 class_unregister(&typec_mux_class); 2045} 2046module_exit(typec_exit); 2047 2048MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>"); 2049MODULE_LICENSE("GPL v2"); 2050MODULE_DESCRIPTION("USB Type-C Connector Class");