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