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