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