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