Merge tag 'ipvs-fixes-for-v4.12' of http://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / drivers / usb / typec / typec.c
1 /*
2  * USB Type-C Connector Class
3  *
4  * Copyright (C) 2017, Intel Corporation
5  * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 #include <linux/device.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <linux/usb/typec.h>
16
17 struct typec_mode {
18         int                             index;
19         u32                             vdo;
20         char                            *desc;
21         enum typec_port_type            roles;
22
23         struct typec_altmode            *alt_mode;
24
25         unsigned int                    active:1;
26
27         char                            group_name[6];
28         struct attribute_group          group;
29         struct attribute                *attrs[5];
30         struct device_attribute         vdo_attr;
31         struct device_attribute         desc_attr;
32         struct device_attribute         active_attr;
33         struct device_attribute         roles_attr;
34 };
35
36 struct typec_altmode {
37         struct device                   dev;
38         u16                             svid;
39         int                             n_modes;
40         struct typec_mode               modes[ALTMODE_MAX_MODES];
41         const struct attribute_group    *mode_groups[ALTMODE_MAX_MODES];
42 };
43
44 struct typec_plug {
45         struct device                   dev;
46         enum typec_plug_index           index;
47 };
48
49 struct typec_cable {
50         struct device                   dev;
51         enum typec_plug_type            type;
52         struct usb_pd_identity          *identity;
53         unsigned int                    active:1;
54 };
55
56 struct typec_partner {
57         struct device                   dev;
58         unsigned int                    usb_pd:1;
59         struct usb_pd_identity          *identity;
60         enum typec_accessory            accessory;
61 };
62
63 struct typec_port {
64         unsigned int                    id;
65         struct device                   dev;
66
67         int                             prefer_role;
68         enum typec_data_role            data_role;
69         enum typec_role                 pwr_role;
70         enum typec_role                 vconn_role;
71         enum typec_pwr_opmode           pwr_opmode;
72
73         const struct typec_capability   *cap;
74 };
75
76 #define to_typec_port(_dev_) container_of(_dev_, struct typec_port, dev)
77 #define to_typec_plug(_dev_) container_of(_dev_, struct typec_plug, dev)
78 #define to_typec_cable(_dev_) container_of(_dev_, struct typec_cable, dev)
79 #define to_typec_partner(_dev_) container_of(_dev_, struct typec_partner, dev)
80 #define to_altmode(_dev_) container_of(_dev_, struct typec_altmode, dev)
81
82 static const struct device_type typec_partner_dev_type;
83 static const struct device_type typec_cable_dev_type;
84 static const struct device_type typec_plug_dev_type;
85 static const struct device_type typec_port_dev_type;
86
87 #define is_typec_partner(_dev_) (_dev_->type == &typec_partner_dev_type)
88 #define is_typec_cable(_dev_) (_dev_->type == &typec_cable_dev_type)
89 #define is_typec_plug(_dev_) (_dev_->type == &typec_plug_dev_type)
90 #define is_typec_port(_dev_) (_dev_->type == &typec_port_dev_type)
91
92 static DEFINE_IDA(typec_index_ida);
93 static struct class *typec_class;
94
95 /* Common attributes */
96
97 static const char * const typec_accessory_modes[] = {
98         [TYPEC_ACCESSORY_NONE]          = "none",
99         [TYPEC_ACCESSORY_AUDIO]         = "analog_audio",
100         [TYPEC_ACCESSORY_DEBUG]         = "debug",
101 };
102
103 static struct usb_pd_identity *get_pd_identity(struct device *dev)
104 {
105         if (is_typec_partner(dev)) {
106                 struct typec_partner *partner = to_typec_partner(dev);
107
108                 return partner->identity;
109         } else if (is_typec_cable(dev)) {
110                 struct typec_cable *cable = to_typec_cable(dev);
111
112                 return cable->identity;
113         }
114         return NULL;
115 }
116
117 static ssize_t id_header_show(struct device *dev, struct device_attribute *attr,
118                               char *buf)
119 {
120         struct usb_pd_identity *id = get_pd_identity(dev);
121
122         return sprintf(buf, "0x%08x\n", id->id_header);
123 }
124 static DEVICE_ATTR_RO(id_header);
125
126 static ssize_t cert_stat_show(struct device *dev, struct device_attribute *attr,
127                               char *buf)
128 {
129         struct usb_pd_identity *id = get_pd_identity(dev);
130
131         return sprintf(buf, "0x%08x\n", id->cert_stat);
132 }
133 static DEVICE_ATTR_RO(cert_stat);
134
135 static ssize_t product_show(struct device *dev, struct device_attribute *attr,
136                             char *buf)
137 {
138         struct usb_pd_identity *id = get_pd_identity(dev);
139
140         return sprintf(buf, "0x%08x\n", id->product);
141 }
142 static DEVICE_ATTR_RO(product);
143
144 static struct attribute *usb_pd_id_attrs[] = {
145         &dev_attr_id_header.attr,
146         &dev_attr_cert_stat.attr,
147         &dev_attr_product.attr,
148         NULL
149 };
150
151 static const struct attribute_group usb_pd_id_group = {
152         .name = "identity",
153         .attrs = usb_pd_id_attrs,
154 };
155
156 static const struct attribute_group *usb_pd_id_groups[] = {
157         &usb_pd_id_group,
158         NULL,
159 };
160
161 static void typec_report_identity(struct device *dev)
162 {
163         sysfs_notify(&dev->kobj, "identity", "id_header");
164         sysfs_notify(&dev->kobj, "identity", "cert_stat");
165         sysfs_notify(&dev->kobj, "identity", "product");
166 }
167
168 /* ------------------------------------------------------------------------- */
169 /* Alternate Modes */
170
171 /**
172  * typec_altmode_update_active - Report Enter/Exit mode
173  * @alt: Handle to the alternate mode
174  * @mode: Mode index
175  * @active: True when the mode has been entered
176  *
177  * If a partner or cable plug executes Enter/Exit Mode command successfully, the
178  * drivers use this routine to report the updated state of the mode.
179  */
180 void typec_altmode_update_active(struct typec_altmode *alt, int mode,
181                                  bool active)
182 {
183         struct typec_mode *m = &alt->modes[mode];
184         char dir[6];
185
186         if (m->active == active)
187                 return;
188
189         m->active = active;
190         snprintf(dir, sizeof(dir), "mode%d", mode);
191         sysfs_notify(&alt->dev.kobj, dir, "active");
192         kobject_uevent(&alt->dev.kobj, KOBJ_CHANGE);
193 }
194 EXPORT_SYMBOL_GPL(typec_altmode_update_active);
195
196 /**
197  * typec_altmode2port - Alternate Mode to USB Type-C port
198  * @alt: The Alternate Mode
199  *
200  * Returns handle to the port that a cable plug or partner with @alt is
201  * connected to.
202  */
203 struct typec_port *typec_altmode2port(struct typec_altmode *alt)
204 {
205         if (is_typec_plug(alt->dev.parent))
206                 return to_typec_port(alt->dev.parent->parent->parent);
207         if (is_typec_partner(alt->dev.parent))
208                 return to_typec_port(alt->dev.parent->parent);
209         if (is_typec_port(alt->dev.parent))
210                 return to_typec_port(alt->dev.parent);
211
212         return NULL;
213 }
214 EXPORT_SYMBOL_GPL(typec_altmode2port);
215
216 static ssize_t
217 typec_altmode_vdo_show(struct device *dev, struct device_attribute *attr,
218                        char *buf)
219 {
220         struct typec_mode *mode = container_of(attr, struct typec_mode,
221                                                vdo_attr);
222
223         return sprintf(buf, "0x%08x\n", mode->vdo);
224 }
225
226 static ssize_t
227 typec_altmode_desc_show(struct device *dev, struct device_attribute *attr,
228                         char *buf)
229 {
230         struct typec_mode *mode = container_of(attr, struct typec_mode,
231                                                desc_attr);
232
233         return sprintf(buf, "%s\n", mode->desc ? mode->desc : "");
234 }
235
236 static ssize_t
237 typec_altmode_active_show(struct device *dev, struct device_attribute *attr,
238                           char *buf)
239 {
240         struct typec_mode *mode = container_of(attr, struct typec_mode,
241                                                active_attr);
242
243         return sprintf(buf, "%s\n", mode->active ? "yes" : "no");
244 }
245
246 static ssize_t
247 typec_altmode_active_store(struct device *dev, struct device_attribute *attr,
248                            const char *buf, size_t size)
249 {
250         struct typec_mode *mode = container_of(attr, struct typec_mode,
251                                                active_attr);
252         struct typec_port *port = typec_altmode2port(mode->alt_mode);
253         bool activate;
254         int ret;
255
256         if (!port->cap->activate_mode)
257                 return -EOPNOTSUPP;
258
259         ret = kstrtobool(buf, &activate);
260         if (ret)
261                 return ret;
262
263         ret = port->cap->activate_mode(port->cap, mode->index, activate);
264         if (ret)
265                 return ret;
266
267         return size;
268 }
269
270 static ssize_t
271 typec_altmode_roles_show(struct device *dev, struct device_attribute *attr,
272                          char *buf)
273 {
274         struct typec_mode *mode = container_of(attr, struct typec_mode,
275                                                roles_attr);
276         ssize_t ret;
277
278         switch (mode->roles) {
279         case TYPEC_PORT_DFP:
280                 ret = sprintf(buf, "source\n");
281                 break;
282         case TYPEC_PORT_UFP:
283                 ret = sprintf(buf, "sink\n");
284                 break;
285         case TYPEC_PORT_DRP:
286         default:
287                 ret = sprintf(buf, "source sink\n");
288                 break;
289         }
290         return ret;
291 }
292
293 static void typec_init_modes(struct typec_altmode *alt,
294                              struct typec_mode_desc *desc, bool is_port)
295 {
296         int i;
297
298         for (i = 0; i < alt->n_modes; i++, desc++) {
299                 struct typec_mode *mode = &alt->modes[i];
300
301                 /* Not considering the human readable description critical */
302                 mode->desc = kstrdup(desc->desc, GFP_KERNEL);
303                 if (desc->desc && !mode->desc)
304                         dev_err(&alt->dev, "failed to copy mode%d desc\n", i);
305
306                 mode->alt_mode = alt;
307                 mode->vdo = desc->vdo;
308                 mode->roles = desc->roles;
309                 mode->index = desc->index;
310                 sprintf(mode->group_name, "mode%d", desc->index);
311
312                 sysfs_attr_init(&mode->vdo_attr.attr);
313                 mode->vdo_attr.attr.name = "vdo";
314                 mode->vdo_attr.attr.mode = 0444;
315                 mode->vdo_attr.show = typec_altmode_vdo_show;
316
317                 sysfs_attr_init(&mode->desc_attr.attr);
318                 mode->desc_attr.attr.name = "description";
319                 mode->desc_attr.attr.mode = 0444;
320                 mode->desc_attr.show = typec_altmode_desc_show;
321
322                 sysfs_attr_init(&mode->active_attr.attr);
323                 mode->active_attr.attr.name = "active";
324                 mode->active_attr.attr.mode = 0644;
325                 mode->active_attr.show = typec_altmode_active_show;
326                 mode->active_attr.store = typec_altmode_active_store;
327
328                 mode->attrs[0] = &mode->vdo_attr.attr;
329                 mode->attrs[1] = &mode->desc_attr.attr;
330                 mode->attrs[2] = &mode->active_attr.attr;
331
332                 /* With ports, list the roles that the mode is supported with */
333                 if (is_port) {
334                         sysfs_attr_init(&mode->roles_attr.attr);
335                         mode->roles_attr.attr.name = "supported_roles";
336                         mode->roles_attr.attr.mode = 0444;
337                         mode->roles_attr.show = typec_altmode_roles_show;
338
339                         mode->attrs[3] = &mode->roles_attr.attr;
340                 }
341
342                 mode->group.attrs = mode->attrs;
343                 mode->group.name = mode->group_name;
344
345                 alt->mode_groups[i] = &mode->group;
346         }
347 }
348
349 static ssize_t svid_show(struct device *dev, struct device_attribute *attr,
350                          char *buf)
351 {
352         struct typec_altmode *alt = to_altmode(dev);
353
354         return sprintf(buf, "%04x\n", alt->svid);
355 }
356 static DEVICE_ATTR_RO(svid);
357
358 static struct attribute *typec_altmode_attrs[] = {
359         &dev_attr_svid.attr,
360         NULL
361 };
362 ATTRIBUTE_GROUPS(typec_altmode);
363
364 static void typec_altmode_release(struct device *dev)
365 {
366         struct typec_altmode *alt = to_altmode(dev);
367         int i;
368
369         for (i = 0; i < alt->n_modes; i++)
370                 kfree(alt->modes[i].desc);
371         kfree(alt);
372 }
373
374 static const struct device_type typec_altmode_dev_type = {
375         .name = "typec_alternate_mode",
376         .groups = typec_altmode_groups,
377         .release = typec_altmode_release,
378 };
379
380 static struct typec_altmode *
381 typec_register_altmode(struct device *parent, struct typec_altmode_desc *desc)
382 {
383         struct typec_altmode *alt;
384         int ret;
385
386         alt = kzalloc(sizeof(*alt), GFP_KERNEL);
387         if (!alt)
388                 return NULL;
389
390         alt->svid = desc->svid;
391         alt->n_modes = desc->n_modes;
392         typec_init_modes(alt, desc->modes, is_typec_port(parent));
393
394         alt->dev.parent = parent;
395         alt->dev.groups = alt->mode_groups;
396         alt->dev.type = &typec_altmode_dev_type;
397         dev_set_name(&alt->dev, "svid-%04x", alt->svid);
398
399         ret = device_register(&alt->dev);
400         if (ret) {
401                 dev_err(parent, "failed to register alternate mode (%d)\n",
402                         ret);
403                 put_device(&alt->dev);
404                 return NULL;
405         }
406
407         return alt;
408 }
409
410 /**
411  * typec_unregister_altmode - Unregister Alternate Mode
412  * @alt: The alternate mode to be unregistered
413  *
414  * Unregister device created with typec_partner_register_altmode(),
415  * typec_plug_register_altmode() or typec_port_register_altmode().
416  */
417 void typec_unregister_altmode(struct typec_altmode *alt)
418 {
419         if (alt)
420                 device_unregister(&alt->dev);
421 }
422 EXPORT_SYMBOL_GPL(typec_unregister_altmode);
423
424 /* ------------------------------------------------------------------------- */
425 /* Type-C Partners */
426
427 static ssize_t accessory_mode_show(struct device *dev,
428                                    struct device_attribute *attr,
429                                    char *buf)
430 {
431         struct typec_partner *p = to_typec_partner(dev);
432
433         return sprintf(buf, "%s\n", typec_accessory_modes[p->accessory]);
434 }
435 static DEVICE_ATTR_RO(accessory_mode);
436
437 static ssize_t supports_usb_power_delivery_show(struct device *dev,
438                                                 struct device_attribute *attr,
439                                                 char *buf)
440 {
441         struct typec_partner *p = to_typec_partner(dev);
442
443         return sprintf(buf, "%s\n", p->usb_pd ? "yes" : "no");
444 }
445 static DEVICE_ATTR_RO(supports_usb_power_delivery);
446
447 static struct attribute *typec_partner_attrs[] = {
448         &dev_attr_accessory_mode.attr,
449         &dev_attr_supports_usb_power_delivery.attr,
450         NULL
451 };
452 ATTRIBUTE_GROUPS(typec_partner);
453
454 static void typec_partner_release(struct device *dev)
455 {
456         struct typec_partner *partner = to_typec_partner(dev);
457
458         kfree(partner);
459 }
460
461 static const struct device_type typec_partner_dev_type = {
462         .name = "typec_partner",
463         .groups = typec_partner_groups,
464         .release = typec_partner_release,
465 };
466
467 /**
468  * typec_partner_set_identity - Report result from Discover Identity command
469  * @partner: The partner updated identity values
470  *
471  * This routine is used to report that the result of Discover Identity USB power
472  * delivery command has become available.
473  */
474 int typec_partner_set_identity(struct typec_partner *partner)
475 {
476         if (!partner->identity)
477                 return -EINVAL;
478
479         typec_report_identity(&partner->dev);
480         return 0;
481 }
482 EXPORT_SYMBOL_GPL(typec_partner_set_identity);
483
484 /**
485  * typec_partner_register_altmode - Register USB Type-C Partner Alternate Mode
486  * @partner: USB Type-C Partner that supports the alternate mode
487  * @desc: Description of the alternate mode
488  *
489  * This routine is used to register each alternate mode individually that
490  * @partner has listed in response to Discover SVIDs command. The modes for a
491  * SVID listed in response to Discover Modes command need to be listed in an
492  * array in @desc.
493  *
494  * Returns handle to the alternate mode on success or NULL on failure.
495  */
496 struct typec_altmode *
497 typec_partner_register_altmode(struct typec_partner *partner,
498                                struct typec_altmode_desc *desc)
499 {
500         return typec_register_altmode(&partner->dev, desc);
501 }
502 EXPORT_SYMBOL_GPL(typec_partner_register_altmode);
503
504 /**
505  * typec_register_partner - Register a USB Type-C Partner
506  * @port: The USB Type-C Port the partner is connected to
507  * @desc: Description of the partner
508  *
509  * Registers a device for USB Type-C Partner described in @desc.
510  *
511  * Returns handle to the partner on success or NULL on failure.
512  */
513 struct typec_partner *typec_register_partner(struct typec_port *port,
514                                              struct typec_partner_desc *desc)
515 {
516         struct typec_partner *partner;
517         int ret;
518
519         partner = kzalloc(sizeof(*partner), GFP_KERNEL);
520         if (!partner)
521                 return NULL;
522
523         partner->usb_pd = desc->usb_pd;
524         partner->accessory = desc->accessory;
525
526         if (desc->identity) {
527                 /*
528                  * Creating directory for the identity only if the driver is
529                  * able to provide data to it.
530                  */
531                 partner->dev.groups = usb_pd_id_groups;
532                 partner->identity = desc->identity;
533         }
534
535         partner->dev.class = typec_class;
536         partner->dev.parent = &port->dev;
537         partner->dev.type = &typec_partner_dev_type;
538         dev_set_name(&partner->dev, "%s-partner", dev_name(&port->dev));
539
540         ret = device_register(&partner->dev);
541         if (ret) {
542                 dev_err(&port->dev, "failed to register partner (%d)\n", ret);
543                 put_device(&partner->dev);
544                 return NULL;
545         }
546
547         return partner;
548 }
549 EXPORT_SYMBOL_GPL(typec_register_partner);
550
551 /**
552  * typec_unregister_partner - Unregister a USB Type-C Partner
553  * @partner: The partner to be unregistered
554  *
555  * Unregister device created with typec_register_partner().
556  */
557 void typec_unregister_partner(struct typec_partner *partner)
558 {
559         if (partner)
560                 device_unregister(&partner->dev);
561 }
562 EXPORT_SYMBOL_GPL(typec_unregister_partner);
563
564 /* ------------------------------------------------------------------------- */
565 /* Type-C Cable Plugs */
566
567 static void typec_plug_release(struct device *dev)
568 {
569         struct typec_plug *plug = to_typec_plug(dev);
570
571         kfree(plug);
572 }
573
574 static const struct device_type typec_plug_dev_type = {
575         .name = "typec_plug",
576         .release = typec_plug_release,
577 };
578
579 /**
580  * typec_plug_register_altmode - Register USB Type-C Cable Plug Alternate Mode
581  * @plug: USB Type-C Cable Plug that supports the alternate mode
582  * @desc: Description of the alternate mode
583  *
584  * This routine is used to register each alternate mode individually that @plug
585  * has listed in response to Discover SVIDs command. The modes for a SVID that
586  * the plug lists in response to Discover Modes command need to be listed in an
587  * array in @desc.
588  *
589  * Returns handle to the alternate mode on success or NULL on failure.
590  */
591 struct typec_altmode *
592 typec_plug_register_altmode(struct typec_plug *plug,
593                             struct typec_altmode_desc *desc)
594 {
595         return typec_register_altmode(&plug->dev, desc);
596 }
597 EXPORT_SYMBOL_GPL(typec_plug_register_altmode);
598
599 /**
600  * typec_register_plug - Register a USB Type-C Cable Plug
601  * @cable: USB Type-C Cable with the plug
602  * @desc: Description of the cable plug
603  *
604  * Registers a device for USB Type-C Cable Plug described in @desc. A USB Type-C
605  * Cable Plug represents a plug with electronics in it that can response to USB
606  * Power Delivery SOP Prime or SOP Double Prime packages.
607  *
608  * Returns handle to the cable plug on success or NULL on failure.
609  */
610 struct typec_plug *typec_register_plug(struct typec_cable *cable,
611                                        struct typec_plug_desc *desc)
612 {
613         struct typec_plug *plug;
614         char name[8];
615         int ret;
616
617         plug = kzalloc(sizeof(*plug), GFP_KERNEL);
618         if (!plug)
619                 return NULL;
620
621         sprintf(name, "plug%d", desc->index);
622
623         plug->index = desc->index;
624         plug->dev.class = typec_class;
625         plug->dev.parent = &cable->dev;
626         plug->dev.type = &typec_plug_dev_type;
627         dev_set_name(&plug->dev, "%s-%s", dev_name(cable->dev.parent), name);
628
629         ret = device_register(&plug->dev);
630         if (ret) {
631                 dev_err(&cable->dev, "failed to register plug (%d)\n", ret);
632                 put_device(&plug->dev);
633                 return NULL;
634         }
635
636         return plug;
637 }
638 EXPORT_SYMBOL_GPL(typec_register_plug);
639
640 /**
641  * typec_unregister_plug - Unregister a USB Type-C Cable Plug
642  * @plug: The cable plug to be unregistered
643  *
644  * Unregister device created with typec_register_plug().
645  */
646 void typec_unregister_plug(struct typec_plug *plug)
647 {
648         if (plug)
649                 device_unregister(&plug->dev);
650 }
651 EXPORT_SYMBOL_GPL(typec_unregister_plug);
652
653 /* Type-C Cables */
654
655 static ssize_t
656 type_show(struct device *dev, struct device_attribute *attr, char *buf)
657 {
658         struct typec_cable *cable = to_typec_cable(dev);
659
660         return sprintf(buf, "%s\n", cable->active ? "active" : "passive");
661 }
662 static DEVICE_ATTR_RO(type);
663
664 static const char * const typec_plug_types[] = {
665         [USB_PLUG_NONE]         = "unknown",
666         [USB_PLUG_TYPE_A]       = "type-a",
667         [USB_PLUG_TYPE_B]       = "type-b",
668         [USB_PLUG_TYPE_C]       = "type-c",
669         [USB_PLUG_CAPTIVE]      = "captive",
670 };
671
672 static ssize_t plug_type_show(struct device *dev,
673                               struct device_attribute *attr, char *buf)
674 {
675         struct typec_cable *cable = to_typec_cable(dev);
676
677         return sprintf(buf, "%s\n", typec_plug_types[cable->type]);
678 }
679 static DEVICE_ATTR_RO(plug_type);
680
681 static struct attribute *typec_cable_attrs[] = {
682         &dev_attr_type.attr,
683         &dev_attr_plug_type.attr,
684         NULL
685 };
686 ATTRIBUTE_GROUPS(typec_cable);
687
688 static void typec_cable_release(struct device *dev)
689 {
690         struct typec_cable *cable = to_typec_cable(dev);
691
692         kfree(cable);
693 }
694
695 static const struct device_type typec_cable_dev_type = {
696         .name = "typec_cable",
697         .groups = typec_cable_groups,
698         .release = typec_cable_release,
699 };
700
701 /**
702  * typec_cable_set_identity - Report result from Discover Identity command
703  * @cable: The cable updated identity values
704  *
705  * This routine is used to report that the result of Discover Identity USB power
706  * delivery command has become available.
707  */
708 int typec_cable_set_identity(struct typec_cable *cable)
709 {
710         if (!cable->identity)
711                 return -EINVAL;
712
713         typec_report_identity(&cable->dev);
714         return 0;
715 }
716 EXPORT_SYMBOL_GPL(typec_cable_set_identity);
717
718 /**
719  * typec_register_cable - Register a USB Type-C Cable
720  * @port: The USB Type-C Port the cable is connected to
721  * @desc: Description of the cable
722  *
723  * Registers a device for USB Type-C Cable described in @desc. The cable will be
724  * parent for the optional cable plug devises.
725  *
726  * Returns handle to the cable on success or NULL on failure.
727  */
728 struct typec_cable *typec_register_cable(struct typec_port *port,
729                                          struct typec_cable_desc *desc)
730 {
731         struct typec_cable *cable;
732         int ret;
733
734         cable = kzalloc(sizeof(*cable), GFP_KERNEL);
735         if (!cable)
736                 return NULL;
737
738         cable->type = desc->type;
739         cable->active = desc->active;
740
741         if (desc->identity) {
742                 /*
743                  * Creating directory for the identity only if the driver is
744                  * able to provide data to it.
745                  */
746                 cable->dev.groups = usb_pd_id_groups;
747                 cable->identity = desc->identity;
748         }
749
750         cable->dev.class = typec_class;
751         cable->dev.parent = &port->dev;
752         cable->dev.type = &typec_cable_dev_type;
753         dev_set_name(&cable->dev, "%s-cable", dev_name(&port->dev));
754
755         ret = device_register(&cable->dev);
756         if (ret) {
757                 dev_err(&port->dev, "failed to register cable (%d)\n", ret);
758                 put_device(&cable->dev);
759                 return NULL;
760         }
761
762         return cable;
763 }
764 EXPORT_SYMBOL_GPL(typec_register_cable);
765
766 /**
767  * typec_unregister_cable - Unregister a USB Type-C Cable
768  * @cable: The cable to be unregistered
769  *
770  * Unregister device created with typec_register_cable().
771  */
772 void typec_unregister_cable(struct typec_cable *cable)
773 {
774         if (cable)
775                 device_unregister(&cable->dev);
776 }
777 EXPORT_SYMBOL_GPL(typec_unregister_cable);
778
779 /* ------------------------------------------------------------------------- */
780 /* USB Type-C ports */
781
782 static const char * const typec_roles[] = {
783         [TYPEC_SINK]    = "sink",
784         [TYPEC_SOURCE]  = "source",
785 };
786
787 static const char * const typec_data_roles[] = {
788         [TYPEC_DEVICE]  = "device",
789         [TYPEC_HOST]    = "host",
790 };
791
792 static ssize_t
793 preferred_role_store(struct device *dev, struct device_attribute *attr,
794                      const char *buf, size_t size)
795 {
796         struct typec_port *port = to_typec_port(dev);
797         int role;
798         int ret;
799
800         if (port->cap->type != TYPEC_PORT_DRP) {
801                 dev_dbg(dev, "Preferred role only supported with DRP ports\n");
802                 return -EOPNOTSUPP;
803         }
804
805         if (!port->cap->try_role) {
806                 dev_dbg(dev, "Setting preferred role not supported\n");
807                 return -EOPNOTSUPP;
808         }
809
810         role = sysfs_match_string(typec_roles, buf);
811         if (role < 0) {
812                 if (sysfs_streq(buf, "none"))
813                         role = TYPEC_NO_PREFERRED_ROLE;
814                 else
815                         return -EINVAL;
816         }
817
818         ret = port->cap->try_role(port->cap, role);
819         if (ret)
820                 return ret;
821
822         port->prefer_role = role;
823         return size;
824 }
825
826 static ssize_t
827 preferred_role_show(struct device *dev, struct device_attribute *attr,
828                     char *buf)
829 {
830         struct typec_port *port = to_typec_port(dev);
831
832         if (port->cap->type != TYPEC_PORT_DRP)
833                 return 0;
834
835         if (port->prefer_role < 0)
836                 return 0;
837
838         return sprintf(buf, "%s\n", typec_roles[port->prefer_role]);
839 }
840 static DEVICE_ATTR_RW(preferred_role);
841
842 static ssize_t data_role_store(struct device *dev,
843                                struct device_attribute *attr,
844                                const char *buf, size_t size)
845 {
846         struct typec_port *port = to_typec_port(dev);
847         int ret;
848
849         if (port->cap->type != TYPEC_PORT_DRP) {
850                 dev_dbg(dev, "data role swap only supported with DRP ports\n");
851                 return -EOPNOTSUPP;
852         }
853
854         if (!port->cap->dr_set) {
855                 dev_dbg(dev, "data role swapping not supported\n");
856                 return -EOPNOTSUPP;
857         }
858
859         ret = sysfs_match_string(typec_data_roles, buf);
860         if (ret < 0)
861                 return ret;
862
863         ret = port->cap->dr_set(port->cap, ret);
864         if (ret)
865                 return ret;
866
867         return size;
868 }
869
870 static ssize_t data_role_show(struct device *dev,
871                               struct device_attribute *attr, char *buf)
872 {
873         struct typec_port *port = to_typec_port(dev);
874
875         if (port->cap->type == TYPEC_PORT_DRP)
876                 return sprintf(buf, "%s\n", port->data_role == TYPEC_HOST ?
877                                "[host] device" : "host [device]");
878
879         return sprintf(buf, "[%s]\n", typec_data_roles[port->data_role]);
880 }
881 static DEVICE_ATTR_RW(data_role);
882
883 static ssize_t power_role_store(struct device *dev,
884                                 struct device_attribute *attr,
885                                 const char *buf, size_t size)
886 {
887         struct typec_port *port = to_typec_port(dev);
888         int ret = size;
889
890         if (!port->cap->pd_revision) {
891                 dev_dbg(dev, "USB Power Delivery not supported\n");
892                 return -EOPNOTSUPP;
893         }
894
895         if (!port->cap->pr_set) {
896                 dev_dbg(dev, "power role swapping not supported\n");
897                 return -EOPNOTSUPP;
898         }
899
900         if (port->pwr_opmode != TYPEC_PWR_MODE_PD) {
901                 dev_dbg(dev, "partner unable to swap power role\n");
902                 return -EIO;
903         }
904
905         ret = sysfs_match_string(typec_roles, buf);
906         if (ret < 0)
907                 return ret;
908
909         ret = port->cap->pr_set(port->cap, ret);
910         if (ret)
911                 return ret;
912
913         return size;
914 }
915
916 static ssize_t power_role_show(struct device *dev,
917                                struct device_attribute *attr, char *buf)
918 {
919         struct typec_port *port = to_typec_port(dev);
920
921         if (port->cap->type == TYPEC_PORT_DRP)
922                 return sprintf(buf, "%s\n", port->pwr_role == TYPEC_SOURCE ?
923                                "[source] sink" : "source [sink]");
924
925         return sprintf(buf, "[%s]\n", typec_roles[port->pwr_role]);
926 }
927 static DEVICE_ATTR_RW(power_role);
928
929 static const char * const typec_pwr_opmodes[] = {
930         [TYPEC_PWR_MODE_USB]    = "default",
931         [TYPEC_PWR_MODE_1_5A]   = "1.5A",
932         [TYPEC_PWR_MODE_3_0A]   = "3.0A",
933         [TYPEC_PWR_MODE_PD]     = "usb_power_delivery",
934 };
935
936 static ssize_t power_operation_mode_show(struct device *dev,
937                                          struct device_attribute *attr,
938                                          char *buf)
939 {
940         struct typec_port *port = to_typec_port(dev);
941
942         return sprintf(buf, "%s\n", typec_pwr_opmodes[port->pwr_opmode]);
943 }
944 static DEVICE_ATTR_RO(power_operation_mode);
945
946 static ssize_t vconn_source_store(struct device *dev,
947                                   struct device_attribute *attr,
948                                   const char *buf, size_t size)
949 {
950         struct typec_port *port = to_typec_port(dev);
951         bool source;
952         int ret;
953
954         if (!port->cap->pd_revision) {
955                 dev_dbg(dev, "VCONN swap depends on USB Power Delivery\n");
956                 return -EOPNOTSUPP;
957         }
958
959         if (!port->cap->vconn_set) {
960                 dev_dbg(dev, "VCONN swapping not supported\n");
961                 return -EOPNOTSUPP;
962         }
963
964         ret = kstrtobool(buf, &source);
965         if (ret)
966                 return ret;
967
968         ret = port->cap->vconn_set(port->cap, (enum typec_role)source);
969         if (ret)
970                 return ret;
971
972         return size;
973 }
974
975 static ssize_t vconn_source_show(struct device *dev,
976                                  struct device_attribute *attr, char *buf)
977 {
978         struct typec_port *port = to_typec_port(dev);
979
980         return sprintf(buf, "%s\n",
981                        port->vconn_role == TYPEC_SOURCE ? "yes" : "no");
982 }
983 static DEVICE_ATTR_RW(vconn_source);
984
985 static ssize_t supported_accessory_modes_show(struct device *dev,
986                                               struct device_attribute *attr,
987                                               char *buf)
988 {
989         struct typec_port *port = to_typec_port(dev);
990         ssize_t ret = 0;
991         int i;
992
993         for (i = 0; i < ARRAY_SIZE(port->cap->accessory); i++) {
994                 if (port->cap->accessory[i])
995                         ret += sprintf(buf + ret, "%s ",
996                                typec_accessory_modes[port->cap->accessory[i]]);
997         }
998
999         if (!ret)
1000                 return sprintf(buf, "none\n");
1001
1002         buf[ret - 1] = '\n';
1003
1004         return ret;
1005 }
1006 static DEVICE_ATTR_RO(supported_accessory_modes);
1007
1008 static ssize_t usb_typec_revision_show(struct device *dev,
1009                                        struct device_attribute *attr,
1010                                        char *buf)
1011 {
1012         struct typec_port *port = to_typec_port(dev);
1013         u16 rev = port->cap->revision;
1014
1015         return sprintf(buf, "%d.%d\n", (rev >> 8) & 0xff, (rev >> 4) & 0xf);
1016 }
1017 static DEVICE_ATTR_RO(usb_typec_revision);
1018
1019 static ssize_t usb_power_delivery_revision_show(struct device *dev,
1020                                                 struct device_attribute *attr,
1021                                                 char *buf)
1022 {
1023         struct typec_port *p = to_typec_port(dev);
1024
1025         return sprintf(buf, "%d\n", (p->cap->pd_revision >> 8) & 0xff);
1026 }
1027 static DEVICE_ATTR_RO(usb_power_delivery_revision);
1028
1029 static struct attribute *typec_attrs[] = {
1030         &dev_attr_data_role.attr,
1031         &dev_attr_power_operation_mode.attr,
1032         &dev_attr_power_role.attr,
1033         &dev_attr_preferred_role.attr,
1034         &dev_attr_supported_accessory_modes.attr,
1035         &dev_attr_usb_power_delivery_revision.attr,
1036         &dev_attr_usb_typec_revision.attr,
1037         &dev_attr_vconn_source.attr,
1038         NULL,
1039 };
1040 ATTRIBUTE_GROUPS(typec);
1041
1042 static int typec_uevent(struct device *dev, struct kobj_uevent_env *env)
1043 {
1044         int ret;
1045
1046         ret = add_uevent_var(env, "TYPEC_PORT=%s", dev_name(dev));
1047         if (ret)
1048                 dev_err(dev, "failed to add uevent TYPEC_PORT\n");
1049
1050         return ret;
1051 }
1052
1053 static void typec_release(struct device *dev)
1054 {
1055         struct typec_port *port = to_typec_port(dev);
1056
1057         ida_simple_remove(&typec_index_ida, port->id);
1058         kfree(port);
1059 }
1060
1061 static const struct device_type typec_port_dev_type = {
1062         .name = "typec_port",
1063         .groups = typec_groups,
1064         .uevent = typec_uevent,
1065         .release = typec_release,
1066 };
1067
1068 /* --------------------------------------- */
1069 /* Driver callbacks to report role updates */
1070
1071 /**
1072  * typec_set_data_role - Report data role change
1073  * @port: The USB Type-C Port where the role was changed
1074  * @role: The new data role
1075  *
1076  * This routine is used by the port drivers to report data role changes.
1077  */
1078 void typec_set_data_role(struct typec_port *port, enum typec_data_role role)
1079 {
1080         if (port->data_role == role)
1081                 return;
1082
1083         port->data_role = role;
1084         sysfs_notify(&port->dev.kobj, NULL, "data_role");
1085         kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1086 }
1087 EXPORT_SYMBOL_GPL(typec_set_data_role);
1088
1089 /**
1090  * typec_set_pwr_role - Report power role change
1091  * @port: The USB Type-C Port where the role was changed
1092  * @role: The new data role
1093  *
1094  * This routine is used by the port drivers to report power role changes.
1095  */
1096 void typec_set_pwr_role(struct typec_port *port, enum typec_role role)
1097 {
1098         if (port->pwr_role == role)
1099                 return;
1100
1101         port->pwr_role = role;
1102         sysfs_notify(&port->dev.kobj, NULL, "power_role");
1103         kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1104 }
1105 EXPORT_SYMBOL_GPL(typec_set_pwr_role);
1106
1107 /**
1108  * typec_set_pwr_role - Report VCONN source change
1109  * @port: The USB Type-C Port which VCONN role changed
1110  * @role: Source when @port is sourcing VCONN, or Sink when it's not
1111  *
1112  * This routine is used by the port drivers to report if the VCONN source is
1113  * changes.
1114  */
1115 void typec_set_vconn_role(struct typec_port *port, enum typec_role role)
1116 {
1117         if (port->vconn_role == role)
1118                 return;
1119
1120         port->vconn_role = role;
1121         sysfs_notify(&port->dev.kobj, NULL, "vconn_source");
1122         kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1123 }
1124 EXPORT_SYMBOL_GPL(typec_set_vconn_role);
1125
1126 /**
1127  * typec_set_pwr_opmode - Report changed power operation mode
1128  * @port: The USB Type-C Port where the mode was changed
1129  * @opmode: New power operation mode
1130  *
1131  * This routine is used by the port drivers to report changed power operation
1132  * mode in @port. The modes are USB (default), 1.5A, 3.0A as defined in USB
1133  * Type-C specification, and "USB Power Delivery" when the power levels are
1134  * negotiated with methods defined in USB Power Delivery specification.
1135  */
1136 void typec_set_pwr_opmode(struct typec_port *port,
1137                           enum typec_pwr_opmode opmode)
1138 {
1139         if (port->pwr_opmode == opmode)
1140                 return;
1141
1142         port->pwr_opmode = opmode;
1143         sysfs_notify(&port->dev.kobj, NULL, "power_operation_mode");
1144         kobject_uevent(&port->dev.kobj, KOBJ_CHANGE);
1145 }
1146 EXPORT_SYMBOL_GPL(typec_set_pwr_opmode);
1147
1148 /* --------------------------------------- */
1149
1150 /**
1151  * typec_port_register_altmode - Register USB Type-C Port Alternate Mode
1152  * @port: USB Type-C Port that supports the alternate mode
1153  * @desc: Description of the alternate mode
1154  *
1155  * This routine is used to register an alternate mode that @port is capable of
1156  * supporting.
1157  *
1158  * Returns handle to the alternate mode on success or NULL on failure.
1159  */
1160 struct typec_altmode *
1161 typec_port_register_altmode(struct typec_port *port,
1162                             struct typec_altmode_desc *desc)
1163 {
1164         return typec_register_altmode(&port->dev, desc);
1165 }
1166 EXPORT_SYMBOL_GPL(typec_port_register_altmode);
1167
1168 /**
1169  * typec_register_port - Register a USB Type-C Port
1170  * @parent: Parent device
1171  * @cap: Description of the port
1172  *
1173  * Registers a device for USB Type-C Port described in @cap.
1174  *
1175  * Returns handle to the port on success or NULL on failure.
1176  */
1177 struct typec_port *typec_register_port(struct device *parent,
1178                                        const struct typec_capability *cap)
1179 {
1180         struct typec_port *port;
1181         int role;
1182         int ret;
1183         int id;
1184
1185         port = kzalloc(sizeof(*port), GFP_KERNEL);
1186         if (!port)
1187                 return NULL;
1188
1189         id = ida_simple_get(&typec_index_ida, 0, 0, GFP_KERNEL);
1190         if (id < 0) {
1191                 kfree(port);
1192                 return NULL;
1193         }
1194
1195         if (cap->type == TYPEC_PORT_DFP)
1196                 role = TYPEC_SOURCE;
1197         else if (cap->type == TYPEC_PORT_UFP)
1198                 role = TYPEC_SINK;
1199         else
1200                 role = cap->prefer_role;
1201
1202         if (role == TYPEC_SOURCE) {
1203                 port->data_role = TYPEC_HOST;
1204                 port->pwr_role = TYPEC_SOURCE;
1205                 port->vconn_role = TYPEC_SOURCE;
1206         } else {
1207                 port->data_role = TYPEC_DEVICE;
1208                 port->pwr_role = TYPEC_SINK;
1209                 port->vconn_role = TYPEC_SINK;
1210         }
1211
1212         port->id = id;
1213         port->cap = cap;
1214         port->prefer_role = cap->prefer_role;
1215
1216         port->dev.class = typec_class;
1217         port->dev.parent = parent;
1218         port->dev.fwnode = cap->fwnode;
1219         port->dev.type = &typec_port_dev_type;
1220         dev_set_name(&port->dev, "port%d", id);
1221
1222         ret = device_register(&port->dev);
1223         if (ret) {
1224                 dev_err(parent, "failed to register port (%d)\n", ret);
1225                 put_device(&port->dev);
1226                 return NULL;
1227         }
1228
1229         return port;
1230 }
1231 EXPORT_SYMBOL_GPL(typec_register_port);
1232
1233 /**
1234  * typec_unregister_port - Unregister a USB Type-C Port
1235  * @port: The port to be unregistered
1236  *
1237  * Unregister device created with typec_register_port().
1238  */
1239 void typec_unregister_port(struct typec_port *port)
1240 {
1241         if (port)
1242                 device_unregister(&port->dev);
1243 }
1244 EXPORT_SYMBOL_GPL(typec_unregister_port);
1245
1246 static int __init typec_init(void)
1247 {
1248         typec_class = class_create(THIS_MODULE, "typec");
1249         return PTR_ERR_OR_ZERO(typec_class);
1250 }
1251 subsys_initcall(typec_init);
1252
1253 static void __exit typec_exit(void)
1254 {
1255         class_destroy(typec_class);
1256         ida_destroy(&typec_index_ida);
1257 }
1258 module_exit(typec_exit);
1259
1260 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1261 MODULE_LICENSE("GPL v2");
1262 MODULE_DESCRIPTION("USB Type-C Connector Class");