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