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