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