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