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