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