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