treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / usb / typec / class.c
blob7c44e930602ff59eb37b77ed87fa8db31ce2834c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Type-C Connector Class
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
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>
15 #include "bus.h"
17 struct typec_plug {
18 struct device dev;
19 enum typec_plug_index index;
20 struct ida mode_ids;
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;
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;
38 struct typec_port {
39 unsigned int id;
40 struct device dev;
41 struct ida mode_ids;
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;
51 enum typec_orientation orientation;
52 struct typec_switch *sw;
53 struct typec_mux *mux;
55 const struct typec_capability *cap;
56 const struct typec_operations *ops;
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)
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;
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)
72 static DEFINE_IDA(typec_index_ida);
73 static struct class *typec_class;
75 /* ------------------------------------------------------------------------- */
76 /* Common attributes */
78 static const char * const typec_accessory_modes[] = {
79 [TYPEC_ACCESSORY_NONE] = "none",
80 [TYPEC_ACCESSORY_AUDIO] = "analog_audio",
81 [TYPEC_ACCESSORY_DEBUG] = "debug",
84 static struct usb_pd_identity *get_pd_identity(struct device *dev)
86 if (is_typec_partner(dev)) {
87 struct typec_partner *partner = to_typec_partner(dev);
89 return partner->identity;
90 } else if (is_typec_cable(dev)) {
91 struct typec_cable *cable = to_typec_cable(dev);
93 return cable->identity;
95 return NULL;
98 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
99 char *buf)
101 struct usb_pd_identity *id = get_pd_identity(dev);
103 return sprintf(buf, "0x%08x\n", id->id_header);
105 static DEVICE_ATTR_RO(id_header);
107 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
108 char *buf)
110 struct usb_pd_identity *id = get_pd_identity(dev);
112 return sprintf(buf, "0x%08x\n", id->cert_stat);
114 static DEVICE_ATTR_RO(cert_stat);
116 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
117 char *buf)
119 struct usb_pd_identity *id = get_pd_identity(dev);
121 return sprintf(buf, "0x%08x\n", id->product);
123 static DEVICE_ATTR_RO(product);
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
132 static const struct attribute_group usb_pd_id_group = {
133 .name = "identity",
134 .attrs = usb_pd_id_attrs,
137 static const struct attribute_group *usb_pd_id_groups[] = {
138 &usb_pd_id_group,
139 NULL,
142 static void typec_report_identity(struct device *dev)
144 sysfs_notify(&dev->kobj, "identity", "id_header");
145 sysfs_notify(&dev->kobj, "identity", "cert_stat");
146 sysfs_notify(&dev->kobj, "identity", "product");
149 /* ------------------------------------------------------------------------- */
150 /* Alternate Modes */
152 static int altmode_match(struct device *dev, void *data)
154 struct typec_altmode *adev = to_typec_altmode(dev);
155 struct typec_device_id *id = data;
157 if (!is_typec_altmode(dev))
158 return 0;
160 return ((adev->svid == id->svid) && (adev->mode == id->mode));
163 static void typec_altmode_set_partner(struct altmode *altmode)
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;
171 dev = device_find_child(&port->dev, &id, altmode_match);
172 if (!dev)
173 return;
175 /* Bind the port alt mode to the partner/plug alt mode. */
176 partner = to_altmode(to_typec_altmode(dev));
177 altmode->partner = partner;
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);
183 partner->plug[plug->index] = altmode;
184 } else {
185 partner->partner = altmode;
189 static void typec_altmode_put_partner(struct altmode *altmode)
191 struct altmode *partner = altmode->partner;
192 struct typec_altmode *adev;
194 if (!partner)
195 return;
197 adev = &partner->adev;
199 if (is_typec_plug(adev->dev.parent)) {
200 struct typec_plug *plug = to_typec_plug(adev->dev.parent);
202 partner->plug[plug->index] = NULL;
203 } else {
204 partner->partner = NULL;
206 put_device(&adev->dev);
209 static void *typec_port_match(struct device_connection *con, int ep, void *data)
211 struct device *dev;
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.
217 if (con->fwnode)
218 return class_find_device_by_fwnode(typec_class, con->fwnode);
220 dev = class_find_device_by_name(typec_class, con->endpoint[ep]);
222 return dev ? dev : ERR_PTR(-EPROBE_DEFER);
225 struct typec_altmode *
226 typec_altmode_register_notifier(struct device *dev, u16 svid, u8 mode,
227 struct notifier_block *nb)
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;
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);
241 /* Find the altmode with matching svid */
242 altmode_dev = device_find_child(port_dev, &id, altmode_match);
244 put_device(port_dev);
246 if (!altmode_dev)
247 return ERR_PTR(-ENODEV);
249 altmode = to_altmode(to_typec_altmode(altmode_dev));
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);
258 return &altmode->adev;
260 EXPORT_SYMBOL_GPL(typec_altmode_register_notifier);
262 void typec_altmode_unregister_notifier(struct typec_altmode *adev,
263 struct notifier_block *nb)
265 struct altmode *altmode = to_altmode(adev);
267 blocking_notifier_chain_unregister(&altmode->nh, nb);
268 put_device(&adev->dev);
270 EXPORT_SYMBOL_GPL(typec_altmode_unregister_notifier);
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
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.
280 void typec_altmode_update_active(struct typec_altmode *adev, bool active)
282 char dir[6];
284 if (adev->active == active)
285 return;
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));
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);
300 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
303 * typec_altmode2port - Alternate Mode to USB Type-C port
304 * @alt: The Alternate Mode
306 * Returns handle to the port that a cable plug or partner with @alt is
307 * connected to.
309 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
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);
318 return NULL;
320 EXPORT_SYMBOL_GPL(typec_altmode2port);
322 static ssize_t
323 vdo_show(struct device *dev, struct device_attribute *attr, char *buf)
325 struct typec_altmode *alt = to_typec_altmode(dev);
327 return sprintf(buf, "0x%08x\n", alt->vdo);
329 static DEVICE_ATTR_RO(vdo);
331 static ssize_t
332 description_show(struct device *dev, struct device_attribute *attr, char *buf)
334 struct typec_altmode *alt = to_typec_altmode(dev);
336 return sprintf(buf, "%s\n", alt->desc ? alt->desc : "");
338 static DEVICE_ATTR_RO(description);
340 static ssize_t
341 active_show(struct device *dev, struct device_attribute *attr, char *buf)
343 struct typec_altmode *alt = to_typec_altmode(dev);
345 return sprintf(buf, "%s\n", alt->active ? "yes" : "no");
348 static ssize_t active_store(struct device *dev, struct device_attribute *attr,
349 const char *buf, size_t size)
351 struct typec_altmode *adev = to_typec_altmode(dev);
352 struct altmode *altmode = to_altmode(adev);
353 bool enter;
354 int ret;
356 ret = kstrtobool(buf, &enter);
357 if (ret)
358 return ret;
360 if (adev->active == enter)
361 return size;
363 if (is_typec_port(adev->dev.parent)) {
364 typec_altmode_update_active(adev, enter);
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;
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;
383 return size;
385 static DEVICE_ATTR_RW(active);
387 static ssize_t
388 supported_roles_show(struct device *dev, struct device_attribute *attr,
389 char *buf)
391 struct altmode *alt = to_altmode(to_typec_altmode(dev));
392 ssize_t ret;
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;
406 return ret;
408 static DEVICE_ATTR_RO(supported_roles);
410 static ssize_t
411 mode_show(struct device *dev, struct device_attribute *attr, char *buf)
413 struct typec_altmode *adev = to_typec_altmode(dev);
415 return sprintf(buf, "%u\n", adev->mode);
417 static DEVICE_ATTR_RO(mode);
419 static ssize_t
420 svid_show(struct device *dev, struct device_attribute *attr, char *buf)
422 struct typec_altmode *adev = to_typec_altmode(dev);
424 return sprintf(buf, "%04x\n", adev->svid);
426 static DEVICE_ATTR_RO(svid);
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
435 ATTRIBUTE_GROUPS(typec_altmode);
437 static int altmode_id_get(struct device *dev)
439 struct ida *ids;
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;
448 return ida_simple_get(ids, 0, 0, GFP_KERNEL);
451 static void altmode_id_remove(struct device *dev, int id)
453 struct ida *ids;
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;
462 ida_simple_remove(ids, id);
465 static void typec_altmode_release(struct device *dev)
467 struct altmode *alt = to_altmode(to_typec_altmode(dev));
469 typec_altmode_put_partner(alt);
471 altmode_id_remove(alt->adev.dev.parent, alt->id);
472 kfree(alt);
475 const struct device_type typec_altmode_dev_type = {
476 .name = "typec_alternate_mode",
477 .groups = typec_altmode_groups,
478 .release = typec_altmode_release,
481 static struct typec_altmode *
482 typec_register_altmode(struct device *parent,
483 const struct typec_altmode_desc *desc)
485 unsigned int id = altmode_id_get(parent);
486 bool is_port = is_typec_port(parent);
487 struct altmode *alt;
488 int ret;
490 alt = kzalloc(sizeof(*alt), GFP_KERNEL);
491 if (!alt)
492 return ERR_PTR(-ENOMEM);
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;
500 alt->attrs[0] = &dev_attr_vdo.attr;
501 alt->attrs[1] = &dev_attr_description.attr;
502 alt->attrs[2] = &dev_attr_active.attr;
504 if (is_port) {
505 alt->attrs[3] = &dev_attr_supported_roles.attr;
506 alt->adev.active = true; /* Enabled by default */
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;
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);
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);
525 /* The partners are bind to drivers */
526 if (is_typec_partner(parent))
527 alt->adev.dev.bus = &typec_bus;
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);
537 return &alt->adev;
541 * typec_unregister_altmode - Unregister Alternate Mode
542 * @adev: The alternate mode to be unregistered
544 * Unregister device created with typec_partner_register_altmode(),
545 * typec_plug_register_altmode() or typec_port_register_altmode().
547 void typec_unregister_altmode(struct typec_altmode *adev)
549 if (IS_ERR_OR_NULL(adev))
550 return;
551 typec_mux_put(to_altmode(adev)->mux);
552 device_unregister(&adev->dev);
554 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
556 /* ------------------------------------------------------------------------- */
557 /* Type-C Partners */
559 static ssize_t accessory_mode_show(struct device *dev,
560 struct device_attribute *attr,
561 char *buf)
563 struct typec_partner *p = to_typec_partner(dev);
565 return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
567 static DEVICE_ATTR_RO(accessory_mode);
569 static ssize_t supports_usb_power_delivery_show(struct device *dev,
570 struct device_attribute *attr,
571 char *buf)
573 struct typec_partner *p = to_typec_partner(dev);
575 return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
577 static DEVICE_ATTR_RO(supports_usb_power_delivery);
579 static struct attribute *typec_partner_attrs[] = {
580 &dev_attr_accessory_mode.attr,
581 &dev_attr_supports_usb_power_delivery.attr,
582 NULL
584 ATTRIBUTE_GROUPS(typec_partner);
586 static void typec_partner_release(struct device *dev)
588 struct typec_partner *partner = to_typec_partner(dev);
590 ida_destroy(&partner->mode_ids);
591 kfree(partner);
594 static const struct device_type typec_partner_dev_type = {
595 .name = "typec_partner",
596 .groups = typec_partner_groups,
597 .release = typec_partner_release,
601 * typec_partner_set_identity - Report result from Discover Identity command
602 * @partner: The partner updated identity values
604 * This routine is used to report that the result of Discover Identity USB power
605 * delivery command has become available.
607 int typec_partner_set_identity(struct typec_partner *partner)
609 if (!partner->identity)
610 return -EINVAL;
612 typec_report_identity(&partner->dev);
613 return 0;
615 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
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
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.
627 * Returns handle to the alternate mode on success or NULL on failure.
629 struct typec_altmode *
630 typec_partner_register_altmode(struct typec_partner *partner,
631 const struct typec_altmode_desc *desc)
633 return typec_register_altmode(&partner->dev, desc);
635 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
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
642 * Registers a device for USB Type-C Partner described in @desc.
644 * Returns handle to the partner on success or ERR_PTR on failure.
646 struct typec_partner *typec_register_partner(struct typec_port *port,
647 struct typec_partner_desc *desc)
649 struct typec_partner *partner;
650 int ret;
652 partner = kzalloc(sizeof(*partner), GFP_KERNEL);
653 if (!partner)
654 return ERR_PTR(-ENOMEM);
656 ida_init(&partner->mode_ids);
657 partner->usb_pd = desc->usb_pd;
658 partner->accessory = desc->accessory;
660 if (desc->identity) {
662 * Creating directory for the identity only if the driver is
663 * able to provide data to it.
665 partner->dev.groups = usb_pd_id_groups;
666 partner->identity = desc->identity;
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));
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);
681 return partner;
683 EXPORT_SYMBOL_GPL(typec_register_partner);
686 * typec_unregister_partner - Unregister a USB Type-C Partner
687 * @partner: The partner to be unregistered
689 * Unregister device created with typec_register_partner().
691 void typec_unregister_partner(struct typec_partner *partner)
693 if (!IS_ERR_OR_NULL(partner))
694 device_unregister(&partner->dev);
696 EXPORT_SYMBOL_GPL(typec_unregister_partner);
698 /* ------------------------------------------------------------------------- */
699 /* Type-C Cable Plugs */
701 static void typec_plug_release(struct device *dev)
703 struct typec_plug *plug = to_typec_plug(dev);
705 ida_destroy(&plug->mode_ids);
706 kfree(plug);
709 static const struct device_type typec_plug_dev_type = {
710 .name = "typec_plug",
711 .release = typec_plug_release,
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
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.
724 * Returns handle to the alternate mode on success or ERR_PTR on failure.
726 struct typec_altmode *
727 typec_plug_register_altmode(struct typec_plug *plug,
728 const struct typec_altmode_desc *desc)
730 return typec_register_altmode(&plug->dev, desc);
732 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
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
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.
743 * Returns handle to the cable plug on success or ERR_PTR on failure.
745 struct typec_plug *typec_register_plug(struct typec_cable *cable,
746 struct typec_plug_desc *desc)
748 struct typec_plug *plug;
749 char name[8];
750 int ret;
752 plug = kzalloc(sizeof(*plug), GFP_KERNEL);
753 if (!plug)
754 return ERR_PTR(-ENOMEM);
756 sprintf(name, "plug%d", desc->index);
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);
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);
772 return plug;
774 EXPORT_SYMBOL_GPL(typec_register_plug);
777 * typec_unregister_plug - Unregister a USB Type-C Cable Plug
778 * @plug: The cable plug to be unregistered
780 * Unregister device created with typec_register_plug().
782 void typec_unregister_plug(struct typec_plug *plug)
784 if (!IS_ERR_OR_NULL(plug))
785 device_unregister(&plug->dev);
787 EXPORT_SYMBOL_GPL(typec_unregister_plug);
789 /* Type-C Cables */
791 static ssize_t
792 type_show(struct device *dev, struct device_attribute *attr, char *buf)
794 struct typec_cable *cable = to_typec_cable(dev);
796 return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
798 static DEVICE_ATTR_RO(type);
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",
808 static ssize_t plug_type_show(struct device *dev,
809 struct device_attribute *attr, char *buf)
811 struct typec_cable *cable = to_typec_cable(dev);
813 return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
815 static DEVICE_ATTR_RO(plug_type);
817 static struct attribute *typec_cable_attrs[] = {
818 &dev_attr_type.attr,
819 &dev_attr_plug_type.attr,
820 NULL
822 ATTRIBUTE_GROUPS(typec_cable);
824 static void typec_cable_release(struct device *dev)
826 struct typec_cable *cable = to_typec_cable(dev);
828 kfree(cable);
831 static const struct device_type typec_cable_dev_type = {
832 .name = "typec_cable",
833 .groups = typec_cable_groups,
834 .release = typec_cable_release,
837 static int cable_match(struct device *dev, void *data)
839 return is_typec_cable(dev);
843 * typec_cable_get - Get a reference to the USB Type-C cable
844 * @port: The USB Type-C Port the cable is connected to
846 * The caller must decrement the reference count with typec_cable_put() after
847 * use.
849 struct typec_cable *typec_cable_get(struct typec_port *port)
851 struct device *dev;
853 dev = device_find_child(&port->dev, NULL, cable_match);
854 if (!dev)
855 return NULL;
857 return to_typec_cable(dev);
859 EXPORT_SYMBOL_GPL(typec_cable_get);
862 * typec_cable_get - Decrement the reference count on USB Type-C cable
863 * @cable: The USB Type-C cable
865 void typec_cable_put(struct typec_cable *cable)
867 put_device(&cable->dev);
869 EXPORT_SYMBOL_GPL(typec_cable_put);
872 * typec_cable_is_active - Check is the USB Type-C cable active or passive
873 * @cable: The USB Type-C Cable
875 * Return 1 if the cable is active or 0 if it's passive.
877 int typec_cable_is_active(struct typec_cable *cable)
879 return cable->active;
881 EXPORT_SYMBOL_GPL(typec_cable_is_active);
884 * typec_cable_set_identity - Report result from Discover Identity command
885 * @cable: The cable updated identity values
887 * This routine is used to report that the result of Discover Identity USB power
888 * delivery command has become available.
890 int typec_cable_set_identity(struct typec_cable *cable)
892 if (!cable->identity)
893 return -EINVAL;
895 typec_report_identity(&cable->dev);
896 return 0;
898 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
901 * typec_register_cable - Register a USB Type-C Cable
902 * @port: The USB Type-C Port the cable is connected to
903 * @desc: Description of the cable
905 * Registers a device for USB Type-C Cable described in @desc. The cable will be
906 * parent for the optional cable plug devises.
908 * Returns handle to the cable on success or ERR_PTR on failure.
910 struct typec_cable *typec_register_cable(struct typec_port *port,
911 struct typec_cable_desc *desc)
913 struct typec_cable *cable;
914 int ret;
916 cable = kzalloc(sizeof(*cable), GFP_KERNEL);
917 if (!cable)
918 return ERR_PTR(-ENOMEM);
920 cable->type = desc->type;
921 cable->active = desc->active;
923 if (desc->identity) {
925 * Creating directory for the identity only if the driver is
926 * able to provide data to it.
928 cable->dev.groups = usb_pd_id_groups;
929 cable->identity = desc->identity;
932 cable->dev.class = typec_class;
933 cable->dev.parent = &port->dev;
934 cable->dev.type = &typec_cable_dev_type;
935 dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
937 ret = device_register(&cable->dev);
938 if (ret) {
939 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
940 put_device(&cable->dev);
941 return ERR_PTR(ret);
944 return cable;
946 EXPORT_SYMBOL_GPL(typec_register_cable);
949 * typec_unregister_cable - Unregister a USB Type-C Cable
950 * @cable: The cable to be unregistered
952 * Unregister device created with typec_register_cable().
954 void typec_unregister_cable(struct typec_cable *cable)
956 if (!IS_ERR_OR_NULL(cable))
957 device_unregister(&cable->dev);
959 EXPORT_SYMBOL_GPL(typec_unregister_cable);
961 /* ------------------------------------------------------------------------- */
962 /* USB Type-C ports */
964 static const char * const typec_roles[] = {
965 [TYPEC_SINK] = "sink",
966 [TYPEC_SOURCE] = "source",
969 static const char * const typec_data_roles[] = {
970 [TYPEC_DEVICE] = "device",
971 [TYPEC_HOST] = "host",
974 static const char * const typec_port_power_roles[] = {
975 [TYPEC_PORT_SRC] = "source",
976 [TYPEC_PORT_SNK] = "sink",
977 [TYPEC_PORT_DRP] = "dual",
980 static const char * const typec_port_data_roles[] = {
981 [TYPEC_PORT_DFP] = "host",
982 [TYPEC_PORT_UFP] = "device",
983 [TYPEC_PORT_DRD] = "dual",
986 static const char * const typec_port_types_drp[] = {
987 [TYPEC_PORT_SRC] = "dual [source] sink",
988 [TYPEC_PORT_SNK] = "dual source [sink]",
989 [TYPEC_PORT_DRP] = "[dual] source sink",
992 static ssize_t
993 preferred_role_store(struct device *dev, struct device_attribute *attr,
994 const char *buf, size_t size)
996 struct typec_port *port = to_typec_port(dev);
997 int role;
998 int ret;
1000 if (port->cap->type != TYPEC_PORT_DRP) {
1001 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
1002 return -EOPNOTSUPP;
1005 if (!port->ops || !port->ops->try_role) {
1006 dev_dbg(dev, "Setting preferred role not supported\n");
1007 return -EOPNOTSUPP;
1010 role = sysfs_match_string(typec_roles, buf);
1011 if (role < 0) {
1012 if (sysfs_streq(buf, "none"))
1013 role = TYPEC_NO_PREFERRED_ROLE;
1014 else
1015 return -EINVAL;
1018 ret = port->ops->try_role(port, role);
1019 if (ret)
1020 return ret;
1022 port->prefer_role = role;
1023 return size;
1026 static ssize_t
1027 preferred_role_show(struct device *dev, struct device_attribute *attr,
1028 char *buf)
1030 struct typec_port *port = to_typec_port(dev);
1032 if (port->cap->type != TYPEC_PORT_DRP)
1033 return 0;
1035 if (port->prefer_role < 0)
1036 return 0;
1038 return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
1040 static DEVICE_ATTR_RW(preferred_role);
1042 static ssize_t data_role_store(struct device *dev,
1043 struct device_attribute *attr,
1044 const char *buf, size_t size)
1046 struct typec_port *port = to_typec_port(dev);
1047 int ret;
1049 if (!port->ops || !port->ops->dr_set) {
1050 dev_dbg(dev, "data role swapping not supported\n");
1051 return -EOPNOTSUPP;
1054 ret = sysfs_match_string(typec_data_roles, buf);
1055 if (ret < 0)
1056 return ret;
1058 mutex_lock(&port->port_type_lock);
1059 if (port->cap->data != TYPEC_PORT_DRD) {
1060 ret = -EOPNOTSUPP;
1061 goto unlock_and_ret;
1064 ret = port->ops->dr_set(port, ret);
1065 if (ret)
1066 goto unlock_and_ret;
1068 ret = size;
1069 unlock_and_ret:
1070 mutex_unlock(&port->port_type_lock);
1071 return ret;
1074 static ssize_t data_role_show(struct device *dev,
1075 struct device_attribute *attr, char *buf)
1077 struct typec_port *port = to_typec_port(dev);
1079 if (port->cap->data == TYPEC_PORT_DRD)
1080 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
1081 "[host] device" : "host [device]");
1083 return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
1085 static DEVICE_ATTR_RW(data_role);
1087 static ssize_t power_role_store(struct device *dev,
1088 struct device_attribute *attr,
1089 const char *buf, size_t size)
1091 struct typec_port *port = to_typec_port(dev);
1092 int ret;
1094 if (!port->cap->pd_revision) {
1095 dev_dbg(dev, "USB Power Delivery not supported\n");
1096 return -EOPNOTSUPP;
1099 if (!port->ops || !port->ops->pr_set) {
1100 dev_dbg(dev, "power role swapping not supported\n");
1101 return -EOPNOTSUPP;
1104 if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
1105 dev_dbg(dev, "partner unable to swap power role\n");
1106 return -EIO;
1109 ret = sysfs_match_string(typec_roles, buf);
1110 if (ret < 0)
1111 return ret;
1113 mutex_lock(&port->port_type_lock);
1114 if (port->port_type != TYPEC_PORT_DRP) {
1115 dev_dbg(dev, "port type fixed at \"%s\"",
1116 typec_port_power_roles[port->port_type]);
1117 ret = -EOPNOTSUPP;
1118 goto unlock_and_ret;
1121 ret = port->ops->pr_set(port, ret);
1122 if (ret)
1123 goto unlock_and_ret;
1125 ret = size;
1126 unlock_and_ret:
1127 mutex_unlock(&port->port_type_lock);
1128 return ret;
1131 static ssize_t power_role_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1134 struct typec_port *port = to_typec_port(dev);
1136 if (port->cap->type == TYPEC_PORT_DRP)
1137 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
1138 "[source] sink" : "source [sink]");
1140 return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
1142 static DEVICE_ATTR_RW(power_role);
1144 static ssize_t
1145 port_type_store(struct device *dev, struct device_attribute *attr,
1146 const char *buf, size_t size)
1148 struct typec_port *port = to_typec_port(dev);
1149 int ret;
1150 enum typec_port_type type;
1152 if (port->cap->type != TYPEC_PORT_DRP ||
1153 !port->ops || !port->ops->port_type_set) {
1154 dev_dbg(dev, "changing port type not supported\n");
1155 return -EOPNOTSUPP;
1158 ret = sysfs_match_string(typec_port_power_roles, buf);
1159 if (ret < 0)
1160 return ret;
1162 type = ret;
1163 mutex_lock(&port->port_type_lock);
1165 if (port->port_type == type) {
1166 ret = size;
1167 goto unlock_and_ret;
1170 ret = port->ops->port_type_set(port, type);
1171 if (ret)
1172 goto unlock_and_ret;
1174 port->port_type = type;
1175 ret = size;
1177 unlock_and_ret:
1178 mutex_unlock(&port->port_type_lock);
1179 return ret;
1182 static ssize_t
1183 port_type_show(struct device *dev, struct device_attribute *attr,
1184 char *buf)
1186 struct typec_port *port = to_typec_port(dev);
1188 if (port->cap->type == TYPEC_PORT_DRP)
1189 return sprintf(buf, "%s\n",
1190 typec_port_types_drp[port->port_type]);
1192 return sprintf(buf, "[%s]\n", typec_port_power_roles[port->cap->type]);
1194 static DEVICE_ATTR_RW(port_type);
1196 static const char * const typec_pwr_opmodes[] = {
1197 [TYPEC_PWR_MODE_USB] = "default",
1198 [TYPEC_PWR_MODE_1_5A] = "1.5A",
1199 [TYPEC_PWR_MODE_3_0A] = "3.0A",
1200 [TYPEC_PWR_MODE_PD] = "usb_power_delivery",
1203 static ssize_t power_operation_mode_show(struct device *dev,
1204 struct device_attribute *attr,
1205 char *buf)
1207 struct typec_port *port = to_typec_port(dev);
1209 return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
1211 static DEVICE_ATTR_RO(power_operation_mode);
1213 static ssize_t vconn_source_store(struct device *dev,
1214 struct device_attribute *attr,
1215 const char *buf, size_t size)
1217 struct typec_port *port = to_typec_port(dev);
1218 bool source;
1219 int ret;
1221 if (!port->cap->pd_revision) {
1222 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
1223 return -EOPNOTSUPP;
1226 if (!port->ops || !port->ops->vconn_set) {
1227 dev_dbg(dev, "VCONN swapping not supported\n");
1228 return -EOPNOTSUPP;
1231 ret = kstrtobool(buf, &source);
1232 if (ret)
1233 return ret;
1235 ret = port->ops->vconn_set(port, (enum typec_role)source);
1236 if (ret)
1237 return ret;
1239 return size;
1242 static ssize_t vconn_source_show(struct device *dev,
1243 struct device_attribute *attr, char *buf)
1245 struct typec_port *port = to_typec_port(dev);
1247 return sprintf(buf, "%s\n",
1248 port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
1250 static DEVICE_ATTR_RW(vconn_source);
1252 static ssize_t supported_accessory_modes_show(struct device *dev,
1253 struct device_attribute *attr,
1254 char *buf)
1256 struct typec_port *port = to_typec_port(dev);
1257 ssize_t ret = 0;
1258 int i;
1260 for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
1261 if (port->cap->accessory[i])
1262 ret += sprintf(buf + ret, "%s ",
1263 typec_accessory_modes[port->cap->accessory[i]]);
1266 if (!ret)
1267 return sprintf(buf, "none\n");
1269 buf[ret - 1] = '\n';
1271 return ret;
1273 static DEVICE_ATTR_RO(supported_accessory_modes);
1275 static ssize_t usb_typec_revision_show(struct device *dev,
1276 struct device_attribute *attr,
1277 char *buf)
1279 struct typec_port *port = to_typec_port(dev);
1280 u16 rev = port->cap->revision;
1282 return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1284 static DEVICE_ATTR_RO(usb_typec_revision);
1286 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1287 struct device_attribute *attr,
1288 char *buf)
1290 struct typec_port *p = to_typec_port(dev);
1292 return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1294 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1296 static struct attribute *typec_attrs[] = {
1297 &dev_attr_data_role.attr,
1298 &dev_attr_power_operation_mode.attr,
1299 &dev_attr_power_role.attr,
1300 &dev_attr_preferred_role.attr,
1301 &dev_attr_supported_accessory_modes.attr,
1302 &dev_attr_usb_power_delivery_revision.attr,
1303 &dev_attr_usb_typec_revision.attr,
1304 &dev_attr_vconn_source.attr,
1305 &dev_attr_port_type.attr,
1306 NULL,
1308 ATTRIBUTE_GROUPS(typec);
1310 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1312 int ret;
1314 ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1315 if (ret)
1316 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1318 return ret;
1321 static void typec_release(struct device *dev)
1323 struct typec_port *port = to_typec_port(dev);
1325 ida_simple_remove(&typec_index_ida, port->id);
1326 ida_destroy(&port->mode_ids);
1327 typec_switch_put(port->sw);
1328 typec_mux_put(port->mux);
1329 kfree(port->cap);
1330 kfree(port);
1333 const struct device_type typec_port_dev_type = {
1334 .name = "typec_port",
1335 .groups = typec_groups,
1336 .uevent = typec_uevent,
1337 .release = typec_release,
1340 /* --------------------------------------- */
1341 /* Driver callbacks to report role updates */
1344 * typec_set_data_role - Report data role change
1345 * @port: The USB Type-C Port where the role was changed
1346 * @role: The new data role
1348 * This routine is used by the port drivers to report data role changes.
1350 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1352 if (port->data_role == role)
1353 return;
1355 port->data_role = role;
1356 sysfs_notify(&port->dev.kobj, NULL, "data_role");
1357 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1359 EXPORT_SYMBOL_GPL(typec_set_data_role);
1362 * typec_set_pwr_role - Report power role change
1363 * @port: The USB Type-C Port where the role was changed
1364 * @role: The new data role
1366 * This routine is used by the port drivers to report power role changes.
1368 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1370 if (port->pwr_role == role)
1371 return;
1373 port->pwr_role = role;
1374 sysfs_notify(&port->dev.kobj, NULL, "power_role");
1375 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1377 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1380 * typec_set_vconn_role - Report VCONN source change
1381 * @port: The USB Type-C Port which VCONN role changed
1382 * @role: Source when @port is sourcing VCONN, or Sink when it's not
1384 * This routine is used by the port drivers to report if the VCONN source is
1385 * changes.
1387 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1389 if (port->vconn_role == role)
1390 return;
1392 port->vconn_role = role;
1393 sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1394 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1396 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1398 static int partner_match(struct device *dev, void *data)
1400 return is_typec_partner(dev);
1404 * typec_set_pwr_opmode - Report changed power operation mode
1405 * @port: The USB Type-C Port where the mode was changed
1406 * @opmode: New power operation mode
1408 * This routine is used by the port drivers to report changed power operation
1409 * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1410 * Type-C specification, and "USB Power Delivery" when the power levels are
1411 * negotiated with methods defined in USB Power Delivery specification.
1413 void typec_set_pwr_opmode(struct typec_port *port,
1414 enum typec_pwr_opmode opmode)
1416 struct device *partner_dev;
1418 if (port->pwr_opmode == opmode)
1419 return;
1421 port->pwr_opmode = opmode;
1422 sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1423 kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1425 partner_dev = device_find_child(&port->dev, NULL, partner_match);
1426 if (partner_dev) {
1427 struct typec_partner *partner = to_typec_partner(partner_dev);
1429 if (opmode == TYPEC_PWR_MODE_PD && !partner->usb_pd) {
1430 partner->usb_pd = 1;
1431 sysfs_notify(&partner_dev->kobj, NULL,
1432 "supports_usb_power_delivery");
1434 put_device(partner_dev);
1437 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1440 * typec_find_port_power_role - Get the typec port power capability
1441 * @name: port power capability string
1443 * This routine is used to find the typec_port_type by its string name.
1445 * Returns typec_port_type if success, otherwise negative error code.
1447 int typec_find_port_power_role(const char *name)
1449 return match_string(typec_port_power_roles,
1450 ARRAY_SIZE(typec_port_power_roles), name);
1452 EXPORT_SYMBOL_GPL(typec_find_port_power_role);
1455 * typec_find_power_role - Find the typec one specific power role
1456 * @name: power role string
1458 * This routine is used to find the typec_role by its string name.
1460 * Returns typec_role if success, otherwise negative error code.
1462 int typec_find_power_role(const char *name)
1464 return match_string(typec_roles, ARRAY_SIZE(typec_roles), name);
1466 EXPORT_SYMBOL_GPL(typec_find_power_role);
1469 * typec_find_port_data_role - Get the typec port data capability
1470 * @name: port data capability string
1472 * This routine is used to find the typec_port_data by its string name.
1474 * Returns typec_port_data if success, otherwise negative error code.
1476 int typec_find_port_data_role(const char *name)
1478 return match_string(typec_port_data_roles,
1479 ARRAY_SIZE(typec_port_data_roles), name);
1481 EXPORT_SYMBOL_GPL(typec_find_port_data_role);
1483 /* ------------------------------------------ */
1484 /* API for Multiplexer/DeMultiplexer Switches */
1487 * typec_set_orientation - Set USB Type-C cable plug orientation
1488 * @port: USB Type-C Port
1489 * @orientation: USB Type-C cable plug orientation
1491 * Set cable plug orientation for @port.
1493 int typec_set_orientation(struct typec_port *port,
1494 enum typec_orientation orientation)
1496 int ret;
1498 if (port->sw) {
1499 ret = port->sw->set(port->sw, orientation);
1500 if (ret)
1501 return ret;
1504 port->orientation = orientation;
1506 return 0;
1508 EXPORT_SYMBOL_GPL(typec_set_orientation);
1511 * typec_get_orientation - Get USB Type-C cable plug orientation
1512 * @port: USB Type-C Port
1514 * Get current cable plug orientation for @port.
1516 enum typec_orientation typec_get_orientation(struct typec_port *port)
1518 return port->orientation;
1520 EXPORT_SYMBOL_GPL(typec_get_orientation);
1523 * typec_set_mode - Set mode of operation for USB Type-C connector
1524 * @port: USB Type-C connector
1525 * @mode: Accessory Mode, USB Operation or Safe State
1527 * Configure @port for Accessory Mode @mode. This function will configure the
1528 * muxes needed for @mode.
1530 int typec_set_mode(struct typec_port *port, int mode)
1532 struct typec_mux_state state = { };
1534 state.mode = mode;
1536 return port->mux ? port->mux->set(port->mux, &state) : 0;
1538 EXPORT_SYMBOL_GPL(typec_set_mode);
1540 /* --------------------------------------- */
1543 * typec_get_drvdata - Return private driver data pointer
1544 * @port: USB Type-C port
1546 void *typec_get_drvdata(struct typec_port *port)
1548 return dev_get_drvdata(&port->dev);
1550 EXPORT_SYMBOL_GPL(typec_get_drvdata);
1553 * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1554 * @port: USB Type-C Port that supports the alternate mode
1555 * @desc: Description of the alternate mode
1557 * This routine is used to register an alternate mode that @port is capable of
1558 * supporting.
1560 * Returns handle to the alternate mode on success or ERR_PTR on failure.
1562 struct typec_altmode *
1563 typec_port_register_altmode(struct typec_port *port,
1564 const struct typec_altmode_desc *desc)
1566 struct typec_altmode *adev;
1567 struct typec_mux *mux;
1569 mux = typec_mux_get(&port->dev, desc);
1570 if (IS_ERR(mux))
1571 return ERR_CAST(mux);
1573 adev = typec_register_altmode(&port->dev, desc);
1574 if (IS_ERR(adev))
1575 typec_mux_put(mux);
1576 else
1577 to_altmode(adev)->mux = mux;
1579 return adev;
1581 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1584 * typec_register_port - Register a USB Type-C Port
1585 * @parent: Parent device
1586 * @cap: Description of the port
1588 * Registers a device for USB Type-C Port described in @cap.
1590 * Returns handle to the port on success or ERR_PTR on failure.
1592 struct typec_port *typec_register_port(struct device *parent,
1593 const struct typec_capability *cap)
1595 struct typec_port *port;
1596 int ret;
1597 int id;
1599 port = kzalloc(sizeof(*port), GFP_KERNEL);
1600 if (!port)
1601 return ERR_PTR(-ENOMEM);
1603 id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1604 if (id < 0) {
1605 kfree(port);
1606 return ERR_PTR(id);
1609 switch (cap->type) {
1610 case TYPEC_PORT_SRC:
1611 port->pwr_role = TYPEC_SOURCE;
1612 port->vconn_role = TYPEC_SOURCE;
1613 break;
1614 case TYPEC_PORT_SNK:
1615 port->pwr_role = TYPEC_SINK;
1616 port->vconn_role = TYPEC_SINK;
1617 break;
1618 case TYPEC_PORT_DRP:
1619 if (cap->prefer_role != TYPEC_NO_PREFERRED_ROLE)
1620 port->pwr_role = cap->prefer_role;
1621 else
1622 port->pwr_role = TYPEC_SINK;
1623 break;
1626 switch (cap->data) {
1627 case TYPEC_PORT_DFP:
1628 port->data_role = TYPEC_HOST;
1629 break;
1630 case TYPEC_PORT_UFP:
1631 port->data_role = TYPEC_DEVICE;
1632 break;
1633 case TYPEC_PORT_DRD:
1634 if (cap->prefer_role == TYPEC_SOURCE)
1635 port->data_role = TYPEC_HOST;
1636 else
1637 port->data_role = TYPEC_DEVICE;
1638 break;
1641 ida_init(&port->mode_ids);
1642 mutex_init(&port->port_type_lock);
1644 port->id = id;
1645 port->ops = cap->ops;
1646 port->port_type = cap->type;
1647 port->prefer_role = cap->prefer_role;
1649 device_initialize(&port->dev);
1650 port->dev.class = typec_class;
1651 port->dev.parent = parent;
1652 port->dev.fwnode = cap->fwnode;
1653 port->dev.type = &typec_port_dev_type;
1654 dev_set_name(&port->dev, "port%d", id);
1655 dev_set_drvdata(&port->dev, cap->driver_data);
1657 port->cap = kmemdup(cap, sizeof(*cap), GFP_KERNEL);
1658 if (!port->cap) {
1659 put_device(&port->dev);
1660 return ERR_PTR(-ENOMEM);
1663 port->sw = typec_switch_get(&port->dev);
1664 if (IS_ERR(port->sw)) {
1665 ret = PTR_ERR(port->sw);
1666 put_device(&port->dev);
1667 return ERR_PTR(ret);
1670 port->mux = typec_mux_get(&port->dev, NULL);
1671 if (IS_ERR(port->mux)) {
1672 ret = PTR_ERR(port->mux);
1673 put_device(&port->dev);
1674 return ERR_PTR(ret);
1677 ret = device_add(&port->dev);
1678 if (ret) {
1679 dev_err(parent, "failed to register port (%d)\n", ret);
1680 put_device(&port->dev);
1681 return ERR_PTR(ret);
1684 return port;
1686 EXPORT_SYMBOL_GPL(typec_register_port);
1689 * typec_unregister_port - Unregister a USB Type-C Port
1690 * @port: The port to be unregistered
1692 * Unregister device created with typec_register_port().
1694 void typec_unregister_port(struct typec_port *port)
1696 if (!IS_ERR_OR_NULL(port))
1697 device_unregister(&port->dev);
1699 EXPORT_SYMBOL_GPL(typec_unregister_port);
1701 static int __init typec_init(void)
1703 int ret;
1705 ret = bus_register(&typec_bus);
1706 if (ret)
1707 return ret;
1709 ret = class_register(&typec_mux_class);
1710 if (ret)
1711 goto err_unregister_bus;
1713 typec_class = class_create(THIS_MODULE, "typec");
1714 if (IS_ERR(typec_class)) {
1715 ret = PTR_ERR(typec_class);
1716 goto err_unregister_mux_class;
1719 return 0;
1721 err_unregister_mux_class:
1722 class_unregister(&typec_mux_class);
1724 err_unregister_bus:
1725 bus_unregister(&typec_bus);
1727 return ret;
1729 subsys_initcall(typec_init);
1731 static void __exit typec_exit(void)
1733 class_destroy(typec_class);
1734 ida_destroy(&typec_index_ida);
1735 bus_unregister(&typec_bus);
1736 class_unregister(&typec_mux_class);
1738 module_exit(typec_exit);
1740 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1741 MODULE_LICENSE("GPL v2");
1742 MODULE_DESCRIPTION("USB Type-C Connector Class");