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