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