1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * USB Type-C Connector System Software Interface driver
4 *
5 * Copyright (C) 2017, Intel Corporation
6 * Author: Heikki Krogerus <heikki.krogerus@linux.intel.com>
7 */
8
9 #include <linux/completion.h>
10 #include <linux/property.h>
11 #include <linux/device.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/slab.h>
15 #include <linux/usb/typec_dp.h>
16
17 #include "ucsi.h"
18 #include "trace.h"
19
20 /*
21 * UCSI_TIMEOUT_MS - PPM communication timeout
22 *
23 * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI
24 * specification) here as reference, but unfortunately we can't. It is very
25 * difficult to estimate the time it takes for the system to process the command
26 * before it is actually passed to the PPM.
27 */
28 #define UCSI_TIMEOUT_MS 10000
29
30 /*
31 * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
32 *
33 * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even
34 * if the PPM does not generate Connector Change events before that with
35 * partners that do not support USB Power Delivery, this should still work.
36 */
37 #define UCSI_SWAP_TIMEOUT_MS 5000
38
ucsi_notify_common(struct ucsi * ucsi,u32 cci)39 void ucsi_notify_common(struct ucsi *ucsi, u32 cci)
40 {
41 /* Ignore bogus data in CCI if busy indicator is set. */
42 if (cci & UCSI_CCI_BUSY)
43 return;
44
45 if (UCSI_CCI_CONNECTOR(cci))
46 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
47
48 if (cci & UCSI_CCI_ACK_COMPLETE &&
49 test_and_clear_bit(ACK_PENDING, &ucsi->flags))
50 complete(&ucsi->complete);
51
52 if (cci & UCSI_CCI_COMMAND_COMPLETE &&
53 test_and_clear_bit(COMMAND_PENDING, &ucsi->flags))
54 complete(&ucsi->complete);
55 }
56 EXPORT_SYMBOL_GPL(ucsi_notify_common);
57
ucsi_sync_control_common(struct ucsi * ucsi,u64 command)58 int ucsi_sync_control_common(struct ucsi *ucsi, u64 command)
59 {
60 bool ack = UCSI_COMMAND(command) == UCSI_ACK_CC_CI;
61 int ret;
62
63 if (ack)
64 set_bit(ACK_PENDING, &ucsi->flags);
65 else
66 set_bit(COMMAND_PENDING, &ucsi->flags);
67
68 reinit_completion(&ucsi->complete);
69
70 ret = ucsi->ops->async_control(ucsi, command);
71 if (ret)
72 goto out_clear_bit;
73
74 if (!wait_for_completion_timeout(&ucsi->complete, 5 * HZ))
75 ret = -ETIMEDOUT;
76
77 out_clear_bit:
78 if (ack)
79 clear_bit(ACK_PENDING, &ucsi->flags);
80 else
81 clear_bit(COMMAND_PENDING, &ucsi->flags);
82
83 return ret;
84 }
85 EXPORT_SYMBOL_GPL(ucsi_sync_control_common);
86
ucsi_acknowledge(struct ucsi * ucsi,bool conn_ack)87 static int ucsi_acknowledge(struct ucsi *ucsi, bool conn_ack)
88 {
89 u64 ctrl;
90
91 ctrl = UCSI_ACK_CC_CI;
92 ctrl |= UCSI_ACK_COMMAND_COMPLETE;
93 if (conn_ack) {
94 clear_bit(EVENT_PENDING, &ucsi->flags);
95 ctrl |= UCSI_ACK_CONNECTOR_CHANGE;
96 }
97
98 return ucsi->ops->sync_control(ucsi, ctrl);
99 }
100
ucsi_run_command(struct ucsi * ucsi,u64 command,u32 * cci,void * data,size_t size,bool conn_ack)101 static int ucsi_run_command(struct ucsi *ucsi, u64 command, u32 *cci,
102 void *data, size_t size, bool conn_ack)
103 {
104 int ret, err;
105
106 *cci = 0;
107
108 if (size > UCSI_MAX_DATA_LENGTH(ucsi))
109 return -EINVAL;
110
111 ret = ucsi->ops->sync_control(ucsi, command);
112 if (ucsi->ops->read_cci(ucsi, cci))
113 return -EIO;
114
115 if (*cci & UCSI_CCI_BUSY)
116 return ucsi_run_command(ucsi, UCSI_CANCEL, cci, NULL, 0, false) ?: -EBUSY;
117 if (ret)
118 return ret;
119
120 if (!(*cci & UCSI_CCI_COMMAND_COMPLETE))
121 return -EIO;
122
123 if (*cci & UCSI_CCI_NOT_SUPPORTED)
124 err = -EOPNOTSUPP;
125 else if (*cci & UCSI_CCI_ERROR)
126 err = -EIO;
127 else
128 err = 0;
129
130 if (!err && data && UCSI_CCI_LENGTH(*cci))
131 err = ucsi->ops->read_message_in(ucsi, data, size);
132
133 /*
134 * Don't ACK connection change if there was an error.
135 */
136 ret = ucsi_acknowledge(ucsi, err ? false : conn_ack);
137 if (ret)
138 return ret;
139
140 return err ?: UCSI_CCI_LENGTH(*cci);
141 }
142
ucsi_read_error(struct ucsi * ucsi,u8 connector_num)143 static int ucsi_read_error(struct ucsi *ucsi, u8 connector_num)
144 {
145 u64 command;
146 u16 error;
147 u32 cci;
148 int ret;
149
150 command = UCSI_GET_ERROR_STATUS | UCSI_CONNECTOR_NUMBER(connector_num);
151 ret = ucsi_run_command(ucsi, command, &cci, &error, sizeof(error), false);
152 if (ret < 0)
153 return ret;
154
155 switch (error) {
156 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
157 return -EOPNOTSUPP;
158 case UCSI_ERROR_CC_COMMUNICATION_ERR:
159 return -ECOMM;
160 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
161 return -EPROTO;
162 case UCSI_ERROR_DEAD_BATTERY:
163 dev_warn(ucsi->dev, "Dead battery condition!\n");
164 return -EPERM;
165 case UCSI_ERROR_INVALID_CON_NUM:
166 case UCSI_ERROR_UNREGONIZED_CMD:
167 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
168 dev_err(ucsi->dev, "possible UCSI driver bug %u\n", error);
169 return -EINVAL;
170 case UCSI_ERROR_OVERCURRENT:
171 dev_warn(ucsi->dev, "Overcurrent condition\n");
172 break;
173 case UCSI_ERROR_PARTNER_REJECTED_SWAP:
174 dev_warn(ucsi->dev, "Partner rejected swap\n");
175 break;
176 case UCSI_ERROR_HARD_RESET:
177 dev_warn(ucsi->dev, "Hard reset occurred\n");
178 break;
179 case UCSI_ERROR_PPM_POLICY_CONFLICT:
180 dev_warn(ucsi->dev, "PPM Policy conflict\n");
181 break;
182 case UCSI_ERROR_SWAP_REJECTED:
183 dev_warn(ucsi->dev, "Swap rejected\n");
184 break;
185 case UCSI_ERROR_REVERSE_CURRENT_PROTECTION:
186 dev_warn(ucsi->dev, "Reverse Current Protection detected\n");
187 break;
188 case UCSI_ERROR_SET_SINK_PATH_REJECTED:
189 dev_warn(ucsi->dev, "Set Sink Path rejected\n");
190 break;
191 case UCSI_ERROR_UNDEFINED:
192 default:
193 dev_err(ucsi->dev, "unknown error %u\n", error);
194 break;
195 }
196
197 return -EIO;
198 }
199
ucsi_send_command_common(struct ucsi * ucsi,u64 cmd,void * data,size_t size,bool conn_ack)200 static int ucsi_send_command_common(struct ucsi *ucsi, u64 cmd,
201 void *data, size_t size, bool conn_ack)
202 {
203 u8 connector_num;
204 u32 cci;
205 int ret;
206
207 if (ucsi->version > UCSI_VERSION_1_2) {
208 switch (UCSI_COMMAND(cmd)) {
209 case UCSI_GET_ALTERNATE_MODES:
210 connector_num = UCSI_GET_ALTMODE_GET_CONNECTOR_NUMBER(cmd);
211 break;
212 case UCSI_PPM_RESET:
213 case UCSI_CANCEL:
214 case UCSI_ACK_CC_CI:
215 case UCSI_SET_NOTIFICATION_ENABLE:
216 case UCSI_GET_CAPABILITY:
217 connector_num = 0;
218 break;
219 default:
220 connector_num = UCSI_DEFAULT_GET_CONNECTOR_NUMBER(cmd);
221 break;
222 }
223 } else {
224 connector_num = 0;
225 }
226
227 mutex_lock(&ucsi->ppm_lock);
228
229 ret = ucsi_run_command(ucsi, cmd, &cci, data, size, conn_ack);
230
231 if (cci & UCSI_CCI_ERROR)
232 ret = ucsi_read_error(ucsi, connector_num);
233
234 mutex_unlock(&ucsi->ppm_lock);
235 return ret;
236 }
237
ucsi_send_command(struct ucsi * ucsi,u64 command,void * data,size_t size)238 int ucsi_send_command(struct ucsi *ucsi, u64 command,
239 void *data, size_t size)
240 {
241 return ucsi_send_command_common(ucsi, command, data, size, false);
242 }
243 EXPORT_SYMBOL_GPL(ucsi_send_command);
244
245 /* -------------------------------------------------------------------------- */
246
247 struct ucsi_work {
248 struct delayed_work work;
249 struct list_head node;
250 unsigned long delay;
251 unsigned int count;
252 struct ucsi_connector *con;
253 int (*cb)(struct ucsi_connector *);
254 };
255
ucsi_poll_worker(struct work_struct * work)256 static void ucsi_poll_worker(struct work_struct *work)
257 {
258 struct ucsi_work *uwork = container_of(work, struct ucsi_work, work.work);
259 struct ucsi_connector *con = uwork->con;
260 int ret;
261
262 mutex_lock(&con->lock);
263
264 if (!con->partner) {
265 list_del(&uwork->node);
266 mutex_unlock(&con->lock);
267 kfree(uwork);
268 return;
269 }
270
271 ret = uwork->cb(con);
272
273 if (uwork->count-- && (ret == -EBUSY || ret == -ETIMEDOUT)) {
274 queue_delayed_work(con->wq, &uwork->work, uwork->delay);
275 } else {
276 list_del(&uwork->node);
277 kfree(uwork);
278 }
279
280 mutex_unlock(&con->lock);
281 }
282
ucsi_partner_task(struct ucsi_connector * con,int (* cb)(struct ucsi_connector *),int retries,unsigned long delay)283 static int ucsi_partner_task(struct ucsi_connector *con,
284 int (*cb)(struct ucsi_connector *),
285 int retries, unsigned long delay)
286 {
287 struct ucsi_work *uwork;
288
289 if (!con->partner)
290 return 0;
291
292 uwork = kzalloc(sizeof(*uwork), GFP_KERNEL);
293 if (!uwork)
294 return -ENOMEM;
295
296 INIT_DELAYED_WORK(&uwork->work, ucsi_poll_worker);
297 uwork->count = retries;
298 uwork->delay = delay;
299 uwork->con = con;
300 uwork->cb = cb;
301
302 list_add_tail(&uwork->node, &con->partner_tasks);
303 queue_delayed_work(con->wq, &uwork->work, delay);
304
305 return 0;
306 }
307
308 /* -------------------------------------------------------------------------- */
309
ucsi_altmode_update_active(struct ucsi_connector * con)310 void ucsi_altmode_update_active(struct ucsi_connector *con)
311 {
312 const struct typec_altmode *altmode = NULL;
313 u64 command;
314 int ret;
315 u8 cur;
316 int i;
317
318 command = UCSI_GET_CURRENT_CAM | UCSI_CONNECTOR_NUMBER(con->num);
319 ret = ucsi_send_command(con->ucsi, command, &cur, sizeof(cur));
320 if (ret < 0) {
321 if (con->ucsi->version > 0x0100) {
322 dev_err(con->ucsi->dev,
323 "GET_CURRENT_CAM command failed\n");
324 return;
325 }
326 cur = 0xff;
327 }
328
329 if (cur < UCSI_MAX_ALTMODES)
330 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
331
332 for (i = 0; con->partner_altmode[i]; i++)
333 typec_altmode_update_active(con->partner_altmode[i],
334 con->partner_altmode[i] == altmode);
335 }
336
ucsi_altmode_next_mode(struct typec_altmode ** alt,u16 svid)337 static int ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
338 {
339 u8 mode = 1;
340 int i;
341
342 for (i = 0; alt[i]; i++) {
343 if (i > MODE_DISCOVERY_MAX)
344 return -ERANGE;
345
346 if (alt[i]->svid == svid)
347 mode++;
348 }
349
350 return mode;
351 }
352
ucsi_next_altmode(struct typec_altmode ** alt)353 static int ucsi_next_altmode(struct typec_altmode **alt)
354 {
355 int i = 0;
356
357 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
358 if (!alt[i])
359 return i;
360
361 return -ENOENT;
362 }
363
ucsi_get_num_altmode(struct typec_altmode ** alt)364 static int ucsi_get_num_altmode(struct typec_altmode **alt)
365 {
366 int i;
367
368 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
369 if (!alt[i])
370 break;
371
372 return i;
373 }
374
ucsi_register_altmode(struct ucsi_connector * con,struct typec_altmode_desc * desc,u8 recipient)375 static int ucsi_register_altmode(struct ucsi_connector *con,
376 struct typec_altmode_desc *desc,
377 u8 recipient)
378 {
379 struct typec_altmode *alt;
380 bool override;
381 int ret;
382 int i;
383
384 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
385
386 switch (recipient) {
387 case UCSI_RECIPIENT_CON:
388 i = ucsi_next_altmode(con->port_altmode);
389 if (i < 0) {
390 ret = i;
391 goto err;
392 }
393
394 ret = ucsi_altmode_next_mode(con->port_altmode, desc->svid);
395 if (ret < 0)
396 return ret;
397
398 desc->mode = ret;
399
400 switch (desc->svid) {
401 case USB_TYPEC_DP_SID:
402 alt = ucsi_register_displayport(con, override, i, desc);
403 break;
404 case USB_TYPEC_NVIDIA_VLINK_SID:
405 if (desc->vdo == USB_TYPEC_NVIDIA_VLINK_DBG_VDO)
406 alt = typec_port_register_altmode(con->port,
407 desc);
408 else
409 alt = ucsi_register_displayport(con, override,
410 i, desc);
411 break;
412 default:
413 alt = typec_port_register_altmode(con->port, desc);
414 break;
415 }
416
417 if (IS_ERR(alt)) {
418 ret = PTR_ERR(alt);
419 goto err;
420 }
421
422 con->port_altmode[i] = alt;
423 break;
424 case UCSI_RECIPIENT_SOP:
425 i = ucsi_next_altmode(con->partner_altmode);
426 if (i < 0) {
427 ret = i;
428 goto err;
429 }
430
431 ret = ucsi_altmode_next_mode(con->partner_altmode, desc->svid);
432 if (ret < 0)
433 return ret;
434
435 desc->mode = ret;
436
437 alt = typec_partner_register_altmode(con->partner, desc);
438 if (IS_ERR(alt)) {
439 ret = PTR_ERR(alt);
440 goto err;
441 }
442
443 con->partner_altmode[i] = alt;
444 break;
445 case UCSI_RECIPIENT_SOP_P:
446 i = ucsi_next_altmode(con->plug_altmode);
447 if (i < 0) {
448 ret = i;
449 goto err;
450 }
451
452 ret = ucsi_altmode_next_mode(con->plug_altmode, desc->svid);
453 if (ret < 0)
454 return ret;
455
456 desc->mode = ret;
457
458 alt = typec_plug_register_altmode(con->plug, desc);
459 if (IS_ERR(alt)) {
460 ret = PTR_ERR(alt);
461 goto err;
462 }
463
464 con->plug_altmode[i] = alt;
465 break;
466 default:
467 return -EINVAL;
468 }
469
470 trace_ucsi_register_altmode(recipient, alt);
471
472 return 0;
473
474 err:
475 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
476 desc->svid, desc->mode);
477
478 return ret;
479 }
480
481 static int
ucsi_register_altmodes_nvidia(struct ucsi_connector * con,u8 recipient)482 ucsi_register_altmodes_nvidia(struct ucsi_connector *con, u8 recipient)
483 {
484 int max_altmodes = UCSI_MAX_ALTMODES;
485 struct typec_altmode_desc desc;
486 struct ucsi_altmode alt;
487 struct ucsi_altmode orig[UCSI_MAX_ALTMODES];
488 struct ucsi_altmode updated[UCSI_MAX_ALTMODES];
489 struct ucsi *ucsi = con->ucsi;
490 bool multi_dp = false;
491 u64 command;
492 int ret;
493 int len;
494 int i;
495 int k = 0;
496
497 if (recipient == UCSI_RECIPIENT_CON)
498 max_altmodes = con->ucsi->cap.num_alt_modes;
499
500 memset(orig, 0, sizeof(orig));
501 memset(updated, 0, sizeof(updated));
502
503 /* First get all the alternate modes */
504 for (i = 0; i < max_altmodes; i++) {
505 memset(&alt, 0, sizeof(alt));
506 command = UCSI_GET_ALTERNATE_MODES;
507 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
508 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
509 command |= UCSI_GET_ALTMODE_OFFSET(i);
510 len = ucsi_send_command(con->ucsi, command, &alt, sizeof(alt));
511 /*
512 * We are collecting all altmodes first and then registering.
513 * Some type-C device will return zero length data beyond last
514 * alternate modes. We should not return if length is zero.
515 */
516 if (len < 0)
517 return len;
518
519 /* We got all altmodes, now break out and register them */
520 if (!len || !alt.svid)
521 break;
522
523 orig[k].mid = alt.mid;
524 orig[k].svid = alt.svid;
525 k++;
526 }
527 /*
528 * Update the original altmode table as some ppms may report
529 * multiple DP altmodes.
530 */
531 if (recipient == UCSI_RECIPIENT_CON)
532 multi_dp = ucsi->ops->update_altmodes(ucsi, orig, updated);
533
534 /* now register altmodes */
535 for (i = 0; i < max_altmodes; i++) {
536 memset(&desc, 0, sizeof(desc));
537 if (multi_dp && recipient == UCSI_RECIPIENT_CON) {
538 desc.svid = updated[i].svid;
539 desc.vdo = updated[i].mid;
540 } else {
541 desc.svid = orig[i].svid;
542 desc.vdo = orig[i].mid;
543 }
544 desc.roles = TYPEC_PORT_DRD;
545
546 if (!desc.svid)
547 return 0;
548
549 ret = ucsi_register_altmode(con, &desc, recipient);
550 if (ret)
551 return ret;
552 }
553
554 return 0;
555 }
556
ucsi_register_altmodes(struct ucsi_connector * con,u8 recipient)557 static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
558 {
559 int max_altmodes = UCSI_MAX_ALTMODES;
560 struct typec_altmode_desc desc;
561 struct ucsi_altmode alt[2];
562 u64 command;
563 int num;
564 int ret;
565 int len;
566 int j;
567 int i;
568
569 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
570 return 0;
571
572 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
573 return 0;
574
575 if (con->ucsi->ops->update_altmodes)
576 return ucsi_register_altmodes_nvidia(con, recipient);
577
578 if (recipient == UCSI_RECIPIENT_CON)
579 max_altmodes = con->ucsi->cap.num_alt_modes;
580
581 for (i = 0; i < max_altmodes;) {
582 memset(alt, 0, sizeof(alt));
583 command = UCSI_GET_ALTERNATE_MODES;
584 command |= UCSI_GET_ALTMODE_RECIPIENT(recipient);
585 command |= UCSI_GET_ALTMODE_CONNECTOR_NUMBER(con->num);
586 command |= UCSI_GET_ALTMODE_OFFSET(i);
587 len = ucsi_send_command(con->ucsi, command, alt, sizeof(alt));
588 if (len == -EBUSY)
589 continue;
590 if (len <= 0)
591 return len;
592
593 /*
594 * This code is requesting one alt mode at a time, but some PPMs
595 * may still return two. If that happens both alt modes need be
596 * registered and the offset for the next alt mode has to be
597 * incremented.
598 */
599 num = len / sizeof(alt[0]);
600 i += num;
601
602 for (j = 0; j < num; j++) {
603 if (!alt[j].svid)
604 return 0;
605
606 memset(&desc, 0, sizeof(desc));
607 desc.vdo = alt[j].mid;
608 desc.svid = alt[j].svid;
609 desc.roles = TYPEC_PORT_DRD;
610
611 ret = ucsi_register_altmode(con, &desc, recipient);
612 if (ret)
613 return ret;
614 }
615 }
616
617 return 0;
618 }
619
ucsi_unregister_altmodes(struct ucsi_connector * con,u8 recipient)620 static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
621 {
622 const struct typec_altmode *pdev;
623 struct typec_altmode **adev;
624 int i = 0;
625
626 switch (recipient) {
627 case UCSI_RECIPIENT_CON:
628 adev = con->port_altmode;
629 break;
630 case UCSI_RECIPIENT_SOP:
631 adev = con->partner_altmode;
632 break;
633 case UCSI_RECIPIENT_SOP_P:
634 adev = con->plug_altmode;
635 break;
636 default:
637 return;
638 }
639
640 while (adev[i]) {
641 if (recipient == UCSI_RECIPIENT_SOP &&
642 (adev[i]->svid == USB_TYPEC_DP_SID ||
643 (adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID &&
644 adev[i]->vdo != USB_TYPEC_NVIDIA_VLINK_DBG_VDO))) {
645 pdev = typec_altmode_get_partner(adev[i]);
646 ucsi_displayport_remove_partner((void *)pdev);
647 }
648 typec_unregister_altmode(adev[i]);
649 adev[i++] = NULL;
650 }
651 }
652
ucsi_read_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos,int offset,int num_pdos)653 static int ucsi_read_pdos(struct ucsi_connector *con,
654 enum typec_role role, int is_partner,
655 u32 *pdos, int offset, int num_pdos)
656 {
657 struct ucsi *ucsi = con->ucsi;
658 u64 command;
659 int ret;
660
661 if (is_partner &&
662 ucsi->quirks & UCSI_NO_PARTNER_PDOS &&
663 ((con->status.flags & UCSI_CONSTAT_PWR_DIR) ||
664 !is_source(role)))
665 return 0;
666
667 command = UCSI_COMMAND(UCSI_GET_PDOS) | UCSI_CONNECTOR_NUMBER(con->num);
668 command |= UCSI_GET_PDOS_PARTNER_PDO(is_partner);
669 command |= UCSI_GET_PDOS_PDO_OFFSET(offset);
670 command |= UCSI_GET_PDOS_NUM_PDOS(num_pdos - 1);
671 command |= is_source(role) ? UCSI_GET_PDOS_SRC_PDOS : 0;
672 ret = ucsi_send_command(ucsi, command, pdos + offset,
673 num_pdos * sizeof(u32));
674 if (ret < 0 && ret != -ETIMEDOUT)
675 dev_err(ucsi->dev, "UCSI_GET_PDOS failed (%d)\n", ret);
676
677 return ret;
678 }
679
ucsi_get_pdos(struct ucsi_connector * con,enum typec_role role,int is_partner,u32 * pdos)680 static int ucsi_get_pdos(struct ucsi_connector *con, enum typec_role role,
681 int is_partner, u32 *pdos)
682 {
683 struct ucsi *ucsi = con->ucsi;
684 u8 num_pdos;
685 int ret;
686
687 if (!(ucsi->cap.features & UCSI_CAP_PDO_DETAILS))
688 return 0;
689
690 /* UCSI max payload means only getting at most 4 PDOs at a time */
691 ret = ucsi_read_pdos(con, role, is_partner, pdos, 0, UCSI_MAX_PDOS);
692 if (ret < 0)
693 return ret;
694
695 num_pdos = ret / sizeof(u32); /* number of bytes to 32-bit PDOs */
696 if (num_pdos < UCSI_MAX_PDOS)
697 return num_pdos;
698
699 /* get the remaining PDOs, if any */
700 ret = ucsi_read_pdos(con, role, is_partner, pdos, UCSI_MAX_PDOS,
701 PDO_MAX_OBJECTS - UCSI_MAX_PDOS);
702 if (ret < 0)
703 return ret;
704
705 return ret / sizeof(u32) + num_pdos;
706 }
707
ucsi_get_src_pdos(struct ucsi_connector * con)708 static int ucsi_get_src_pdos(struct ucsi_connector *con)
709 {
710 int ret;
711
712 ret = ucsi_get_pdos(con, TYPEC_SOURCE, 1, con->src_pdos);
713 if (ret < 0)
714 return ret;
715
716 con->num_pdos = ret;
717
718 ucsi_port_psy_changed(con);
719
720 return ret;
721 }
722
ucsi_get_pd_caps(struct ucsi_connector * con,enum typec_role role,bool is_partner)723 static struct usb_power_delivery_capabilities *ucsi_get_pd_caps(struct ucsi_connector *con,
724 enum typec_role role,
725 bool is_partner)
726 {
727 struct usb_power_delivery_capabilities_desc pd_caps;
728 int ret;
729
730 ret = ucsi_get_pdos(con, role, is_partner, pd_caps.pdo);
731 if (ret <= 0)
732 return ERR_PTR(ret);
733
734 if (ret < PDO_MAX_OBJECTS)
735 pd_caps.pdo[ret] = 0;
736
737 pd_caps.role = role;
738
739 return usb_power_delivery_register_capabilities(is_partner ? con->partner_pd : con->pd,
740 &pd_caps);
741 }
742
ucsi_get_pd_message(struct ucsi_connector * con,u8 recipient,size_t bytes,void * data,u8 type)743 static int ucsi_get_pd_message(struct ucsi_connector *con, u8 recipient,
744 size_t bytes, void *data, u8 type)
745 {
746 size_t len = min(bytes, UCSI_MAX_DATA_LENGTH(con->ucsi));
747 u64 command;
748 u8 offset;
749 int ret;
750
751 for (offset = 0; offset < bytes; offset += len) {
752 len = min(len, bytes - offset);
753
754 command = UCSI_COMMAND(UCSI_GET_PD_MESSAGE) | UCSI_CONNECTOR_NUMBER(con->num);
755 command |= UCSI_GET_PD_MESSAGE_RECIPIENT(recipient);
756 command |= UCSI_GET_PD_MESSAGE_OFFSET(offset);
757 command |= UCSI_GET_PD_MESSAGE_BYTES(len);
758 command |= UCSI_GET_PD_MESSAGE_TYPE(type);
759
760 ret = ucsi_send_command(con->ucsi, command, data + offset, len);
761 if (ret < 0)
762 return ret;
763 }
764
765 return 0;
766 }
767
ucsi_get_partner_identity(struct ucsi_connector * con)768 static int ucsi_get_partner_identity(struct ucsi_connector *con)
769 {
770 u32 vdo[7] = {};
771 int ret;
772
773 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP, sizeof(vdo), vdo,
774 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
775 if (ret < 0)
776 return ret;
777
778 /* VDM Header is not part of struct usb_pd_identity, so dropping it. */
779 con->partner_identity = *(struct usb_pd_identity *)&vdo[1];
780
781 ret = typec_partner_set_identity(con->partner);
782 if (ret < 0)
783 dev_err(con->ucsi->dev, "Failed to set partner identity (%d)\n", ret);
784
785 return ret;
786 }
787
ucsi_get_cable_identity(struct ucsi_connector * con)788 static int ucsi_get_cable_identity(struct ucsi_connector *con)
789 {
790 u32 vdo[7] = {};
791 int ret;
792
793 ret = ucsi_get_pd_message(con, UCSI_RECIPIENT_SOP_P, sizeof(vdo), vdo,
794 UCSI_GET_PD_MESSAGE_TYPE_IDENTITY);
795 if (ret < 0)
796 return ret;
797
798 con->cable_identity = *(struct usb_pd_identity *)&vdo[1];
799
800 ret = typec_cable_set_identity(con->cable);
801 if (ret < 0)
802 dev_err(con->ucsi->dev, "Failed to set cable identity (%d)\n", ret);
803
804 return ret;
805 }
806
ucsi_check_altmodes(struct ucsi_connector * con)807 static int ucsi_check_altmodes(struct ucsi_connector *con)
808 {
809 int ret, num_partner_am;
810
811 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
812 if (ret && ret != -ETIMEDOUT)
813 dev_err(con->ucsi->dev,
814 "con%d: failed to register partner alt modes (%d)\n",
815 con->num, ret);
816
817 /* Ignoring the errors in this case. */
818 if (con->partner_altmode[0]) {
819 num_partner_am = ucsi_get_num_altmode(con->partner_altmode);
820 typec_partner_set_num_altmodes(con->partner, num_partner_am);
821 ucsi_altmode_update_active(con);
822 return 0;
823 } else {
824 typec_partner_set_num_altmodes(con->partner, 0);
825 }
826
827 return ret;
828 }
829
ucsi_register_device_pdos(struct ucsi_connector * con)830 static void ucsi_register_device_pdos(struct ucsi_connector *con)
831 {
832 struct ucsi *ucsi = con->ucsi;
833 struct usb_power_delivery_desc desc = { ucsi->cap.pd_version };
834 struct usb_power_delivery_capabilities *pd_cap;
835
836 if (con->pd)
837 return;
838
839 con->pd = usb_power_delivery_register(ucsi->dev, &desc);
840
841 pd_cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, false);
842 if (!IS_ERR(pd_cap))
843 con->port_source_caps = pd_cap;
844
845 pd_cap = ucsi_get_pd_caps(con, TYPEC_SINK, false);
846 if (!IS_ERR(pd_cap))
847 con->port_sink_caps = pd_cap;
848
849 typec_port_set_usb_power_delivery(con->port, con->pd);
850 }
851
ucsi_register_partner_pdos(struct ucsi_connector * con)852 static int ucsi_register_partner_pdos(struct ucsi_connector *con)
853 {
854 struct usb_power_delivery_desc desc = { con->ucsi->cap.pd_version };
855 struct usb_power_delivery_capabilities *cap;
856
857 if (con->partner_pd)
858 return 0;
859
860 con->partner_pd = typec_partner_usb_power_delivery_register(con->partner, &desc);
861 if (IS_ERR(con->partner_pd))
862 return PTR_ERR(con->partner_pd);
863
864 cap = ucsi_get_pd_caps(con, TYPEC_SOURCE, true);
865 if (IS_ERR(cap))
866 return PTR_ERR(cap);
867
868 con->partner_source_caps = cap;
869
870 cap = ucsi_get_pd_caps(con, TYPEC_SINK, true);
871 if (IS_ERR(cap))
872 return PTR_ERR(cap);
873
874 con->partner_sink_caps = cap;
875
876 return typec_partner_set_usb_power_delivery(con->partner, con->partner_pd);
877 }
878
ucsi_unregister_partner_pdos(struct ucsi_connector * con)879 static void ucsi_unregister_partner_pdos(struct ucsi_connector *con)
880 {
881 usb_power_delivery_unregister_capabilities(con->partner_sink_caps);
882 con->partner_sink_caps = NULL;
883 usb_power_delivery_unregister_capabilities(con->partner_source_caps);
884 con->partner_source_caps = NULL;
885 usb_power_delivery_unregister(con->partner_pd);
886 con->partner_pd = NULL;
887 }
888
ucsi_register_plug(struct ucsi_connector * con)889 static int ucsi_register_plug(struct ucsi_connector *con)
890 {
891 struct typec_plug *plug;
892 struct typec_plug_desc desc = {.index = TYPEC_PLUG_SOP_P};
893
894 plug = typec_register_plug(con->cable, &desc);
895 if (IS_ERR(plug)) {
896 dev_err(con->ucsi->dev,
897 "con%d: failed to register plug (%ld)\n", con->num,
898 PTR_ERR(plug));
899 return PTR_ERR(plug);
900 }
901
902 con->plug = plug;
903 return 0;
904 }
905
ucsi_unregister_plug(struct ucsi_connector * con)906 static void ucsi_unregister_plug(struct ucsi_connector *con)
907 {
908 if (!con->plug)
909 return;
910
911 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP_P);
912 typec_unregister_plug(con->plug);
913 con->plug = NULL;
914 }
915
ucsi_register_cable(struct ucsi_connector * con)916 static int ucsi_register_cable(struct ucsi_connector *con)
917 {
918 struct ucsi_cable_property cable_prop;
919 struct typec_cable *cable;
920 struct typec_cable_desc desc = {};
921 u64 command;
922 int ret;
923
924 command = UCSI_GET_CABLE_PROPERTY | UCSI_CONNECTOR_NUMBER(con->num);
925 ret = ucsi_send_command(con->ucsi, command, &cable_prop, sizeof(cable_prop));
926 if (ret < 0) {
927 dev_err(con->ucsi->dev, "GET_CABLE_PROPERTY failed (%d)\n", ret);
928 return ret;
929 }
930
931 switch (UCSI_CABLE_PROP_FLAG_PLUG_TYPE(cable_prop.flags)) {
932 case UCSI_CABLE_PROPERTY_PLUG_TYPE_A:
933 desc.type = USB_PLUG_TYPE_A;
934 break;
935 case UCSI_CABLE_PROPERTY_PLUG_TYPE_B:
936 desc.type = USB_PLUG_TYPE_B;
937 break;
938 case UCSI_CABLE_PROPERTY_PLUG_TYPE_C:
939 desc.type = USB_PLUG_TYPE_C;
940 break;
941 default:
942 desc.type = USB_PLUG_NONE;
943 break;
944 }
945
946 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
947 desc.identity = &con->cable_identity;
948 desc.active = !!(UCSI_CABLE_PROP_FLAG_ACTIVE_CABLE & cable_prop.flags);
949
950 if (con->ucsi->version >= UCSI_VERSION_2_1)
951 desc.pd_revision = UCSI_CABLE_PROP_FLAG_PD_MAJOR_REV_AS_BCD(cable_prop.flags);
952
953 cable = typec_register_cable(con->port, &desc);
954 if (IS_ERR(cable)) {
955 dev_err(con->ucsi->dev,
956 "con%d: failed to register cable (%ld)\n", con->num,
957 PTR_ERR(cable));
958 return PTR_ERR(cable);
959 }
960
961 con->cable = cable;
962 return 0;
963 }
964
ucsi_unregister_cable(struct ucsi_connector * con)965 static void ucsi_unregister_cable(struct ucsi_connector *con)
966 {
967 if (!con->cable)
968 return;
969
970 ucsi_unregister_plug(con);
971 typec_unregister_cable(con->cable);
972 memset(&con->cable_identity, 0, sizeof(con->cable_identity));
973 con->cable = NULL;
974 }
975
ucsi_check_connector_capability(struct ucsi_connector * con)976 static int ucsi_check_connector_capability(struct ucsi_connector *con)
977 {
978 u64 command;
979 int ret;
980
981 if (!con->partner || con->ucsi->version < UCSI_VERSION_2_1)
982 return 0;
983
984 command = UCSI_GET_CONNECTOR_CAPABILITY | UCSI_CONNECTOR_NUMBER(con->num);
985 ret = ucsi_send_command(con->ucsi, command, &con->cap, sizeof(con->cap));
986 if (ret < 0) {
987 dev_err(con->ucsi->dev, "GET_CONNECTOR_CAPABILITY failed (%d)\n", ret);
988 return ret;
989 }
990
991 typec_partner_set_pd_revision(con->partner,
992 UCSI_CONCAP_FLAG_PARTNER_PD_MAJOR_REV_AS_BCD(con->cap.flags));
993
994 return ret;
995 }
996
ucsi_pwr_opmode_change(struct ucsi_connector * con)997 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
998 {
999 switch (UCSI_CONSTAT_PWR_OPMODE(con->status.flags)) {
1000 case UCSI_CONSTAT_PWR_OPMODE_PD:
1001 con->rdo = con->status.request_data_obj;
1002 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
1003 ucsi_partner_task(con, ucsi_get_src_pdos, 30, 0);
1004 ucsi_partner_task(con, ucsi_check_altmodes, 30, HZ);
1005 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1006 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1007 break;
1008 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
1009 con->rdo = 0;
1010 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
1011 break;
1012 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
1013 con->rdo = 0;
1014 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
1015 break;
1016 default:
1017 con->rdo = 0;
1018 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
1019 break;
1020 }
1021 }
1022
ucsi_register_partner(struct ucsi_connector * con)1023 static int ucsi_register_partner(struct ucsi_connector *con)
1024 {
1025 u8 pwr_opmode = UCSI_CONSTAT_PWR_OPMODE(con->status.flags);
1026 struct typec_partner_desc desc;
1027 struct typec_partner *partner;
1028
1029 if (con->partner)
1030 return 0;
1031
1032 memset(&desc, 0, sizeof(desc));
1033
1034 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1035 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1036 desc.accessory = TYPEC_ACCESSORY_DEBUG;
1037 break;
1038 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1039 desc.accessory = TYPEC_ACCESSORY_AUDIO;
1040 break;
1041 default:
1042 break;
1043 }
1044
1045 if (pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD)
1046 ucsi_register_device_pdos(con);
1047
1048 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1049 desc.identity = &con->partner_identity;
1050 desc.usb_pd = pwr_opmode == UCSI_CONSTAT_PWR_OPMODE_PD;
1051
1052 partner = typec_register_partner(con->port, &desc);
1053 if (IS_ERR(partner)) {
1054 dev_err(con->ucsi->dev,
1055 "con%d: failed to register partner (%ld)\n", con->num,
1056 PTR_ERR(partner));
1057 return PTR_ERR(partner);
1058 }
1059
1060 con->partner = partner;
1061
1062 return 0;
1063 }
1064
ucsi_unregister_partner(struct ucsi_connector * con)1065 static void ucsi_unregister_partner(struct ucsi_connector *con)
1066 {
1067 if (!con->partner)
1068 return;
1069
1070 typec_set_mode(con->port, TYPEC_STATE_SAFE);
1071
1072 typec_partner_set_usb_power_delivery(con->partner, NULL);
1073 ucsi_unregister_partner_pdos(con);
1074 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
1075 ucsi_unregister_cable(con);
1076 typec_unregister_partner(con->partner);
1077 memset(&con->partner_identity, 0, sizeof(con->partner_identity));
1078 con->partner = NULL;
1079 }
1080
ucsi_partner_change(struct ucsi_connector * con)1081 static void ucsi_partner_change(struct ucsi_connector *con)
1082 {
1083 enum usb_role u_role = USB_ROLE_NONE;
1084 int ret;
1085
1086 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1087 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1088 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1089 u_role = USB_ROLE_HOST;
1090 fallthrough;
1091 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1092 typec_set_data_role(con->port, TYPEC_HOST);
1093 break;
1094 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1095 u_role = USB_ROLE_DEVICE;
1096 typec_set_data_role(con->port, TYPEC_DEVICE);
1097 break;
1098 default:
1099 break;
1100 }
1101
1102 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1103 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1104 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
1105 typec_set_mode(con->port, TYPEC_MODE_DEBUG);
1106 break;
1107 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
1108 typec_set_mode(con->port, TYPEC_MODE_AUDIO);
1109 break;
1110 default:
1111 if (UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) ==
1112 UCSI_CONSTAT_PARTNER_FLAG_USB)
1113 typec_set_mode(con->port, TYPEC_STATE_USB);
1114 }
1115 }
1116
1117 /* Only notify USB controller if partner supports USB data */
1118 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1119 u_role = USB_ROLE_NONE;
1120
1121 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1122 if (ret)
1123 dev_err(con->ucsi->dev, "con:%d: failed to set usb role:%d\n",
1124 con->num, u_role);
1125 }
1126
ucsi_check_connection(struct ucsi_connector * con)1127 static int ucsi_check_connection(struct ucsi_connector *con)
1128 {
1129 u8 prev_flags = con->status.flags;
1130 u64 command;
1131 int ret;
1132
1133 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1134 ret = ucsi_send_command(con->ucsi, command, &con->status, sizeof(con->status));
1135 if (ret < 0) {
1136 dev_err(con->ucsi->dev, "GET_CONNECTOR_STATUS failed (%d)\n", ret);
1137 return ret;
1138 }
1139
1140 if (con->status.flags == prev_flags)
1141 return 0;
1142
1143 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1144 ucsi_register_partner(con);
1145 ucsi_pwr_opmode_change(con);
1146 ucsi_partner_change(con);
1147 } else {
1148 ucsi_partner_change(con);
1149 ucsi_port_psy_changed(con);
1150 ucsi_unregister_partner(con);
1151 }
1152
1153 return 0;
1154 }
1155
ucsi_check_cable(struct ucsi_connector * con)1156 static int ucsi_check_cable(struct ucsi_connector *con)
1157 {
1158 int ret, num_plug_am;
1159
1160 if (con->cable)
1161 return 0;
1162
1163 ret = ucsi_register_cable(con);
1164 if (ret < 0)
1165 return ret;
1166
1167 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE) {
1168 ret = ucsi_get_cable_identity(con);
1169 if (ret < 0)
1170 return ret;
1171 }
1172
1173 if (con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS) {
1174 ret = ucsi_register_plug(con);
1175 if (ret < 0)
1176 return ret;
1177
1178 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP_P);
1179 if (ret < 0)
1180 return ret;
1181
1182 if (con->plug_altmode[0]) {
1183 num_plug_am = ucsi_get_num_altmode(con->plug_altmode);
1184 typec_plug_set_num_altmodes(con->plug, num_plug_am);
1185 } else {
1186 typec_plug_set_num_altmodes(con->plug, 0);
1187 }
1188 }
1189
1190 return 0;
1191 }
1192
ucsi_handle_connector_change(struct work_struct * work)1193 static void ucsi_handle_connector_change(struct work_struct *work)
1194 {
1195 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
1196 work);
1197 struct ucsi *ucsi = con->ucsi;
1198 enum typec_role role;
1199 u64 command;
1200 int ret;
1201
1202 mutex_lock(&con->lock);
1203
1204 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1205 dev_err_once(ucsi->dev, "%s entered without EVENT_PENDING\n",
1206 __func__);
1207
1208 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1209
1210 ret = ucsi_send_command_common(ucsi, command, &con->status,
1211 sizeof(con->status), true);
1212 if (ret < 0) {
1213 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
1214 __func__, ret);
1215 clear_bit(EVENT_PENDING, &con->ucsi->flags);
1216 goto out_unlock;
1217 }
1218
1219 trace_ucsi_connector_change(con->num, &con->status);
1220
1221 if (ucsi->ops->connector_status)
1222 ucsi->ops->connector_status(con);
1223
1224 role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1225
1226 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
1227 typec_set_pwr_role(con->port, role);
1228 ucsi_port_psy_changed(con);
1229
1230 /* Complete pending power role swap */
1231 if (!completion_done(&con->complete))
1232 complete(&con->complete);
1233 }
1234
1235 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
1236 typec_set_pwr_role(con->port, role);
1237 ucsi_port_psy_changed(con);
1238 ucsi_partner_change(con);
1239
1240 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1241 ucsi_register_partner(con);
1242 ucsi_partner_task(con, ucsi_check_connection, 1, HZ);
1243 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1244 ucsi_partner_task(con, ucsi_get_partner_identity, 1, HZ);
1245 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1246 ucsi_partner_task(con, ucsi_check_cable, 1, HZ);
1247
1248 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1249 UCSI_CONSTAT_PWR_OPMODE_PD) {
1250 ucsi_partner_task(con, ucsi_register_partner_pdos, 1, HZ);
1251 ucsi_partner_task(con, ucsi_check_connector_capability, 1, HZ);
1252 }
1253 } else {
1254 ucsi_unregister_partner(con);
1255 }
1256 }
1257
1258 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE ||
1259 con->status.change & UCSI_CONSTAT_POWER_LEVEL_CHANGE)
1260 ucsi_pwr_opmode_change(con);
1261
1262 if (con->partner && con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
1263 ucsi_partner_change(con);
1264
1265 /* Complete pending data role swap */
1266 if (!completion_done(&con->complete))
1267 complete(&con->complete);
1268 }
1269
1270 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE)
1271 ucsi_partner_task(con, ucsi_check_altmodes, 1, HZ);
1272
1273 if (con->status.change & UCSI_CONSTAT_BC_CHANGE)
1274 ucsi_port_psy_changed(con);
1275
1276 out_unlock:
1277 mutex_unlock(&con->lock);
1278 }
1279
1280 /**
1281 * ucsi_connector_change - Process Connector Change Event
1282 * @ucsi: UCSI Interface
1283 * @num: Connector number
1284 */
ucsi_connector_change(struct ucsi * ucsi,u8 num)1285 void ucsi_connector_change(struct ucsi *ucsi, u8 num)
1286 {
1287 struct ucsi_connector *con = &ucsi->connector[num - 1];
1288
1289 if (!(ucsi->ntfy & UCSI_ENABLE_NTFY_CONNECTOR_CHANGE)) {
1290 dev_dbg(ucsi->dev, "Early connector change event\n");
1291 return;
1292 }
1293
1294 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
1295 schedule_work(&con->work);
1296 }
1297 EXPORT_SYMBOL_GPL(ucsi_connector_change);
1298
1299 /* -------------------------------------------------------------------------- */
1300
1301 /*
1302 * Hard Reset bit field was defined with value 1 in UCSI spec version 1.0.
1303 * Starting with spec version 1.1, Hard Reset bit field was removed from the
1304 * CONNECTOR_RESET command, until spec 2.0 reintroduced it with value 0, so, in effect,
1305 * the value to pass in to the command for a Hard Reset is different depending
1306 * on the supported UCSI version by the LPM.
1307 *
1308 * For performing a Data Reset on LPMs supporting version 2.0 and greater,
1309 * this function needs to be called with the second argument set to 0.
1310 */
ucsi_reset_connector(struct ucsi_connector * con,bool hard)1311 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
1312 {
1313 u64 command;
1314
1315 command = UCSI_CONNECTOR_RESET | UCSI_CONNECTOR_NUMBER(con->num);
1316
1317 if (con->ucsi->version < UCSI_VERSION_1_1)
1318 command |= hard ? UCSI_CONNECTOR_RESET_HARD_VER_1_0 : 0;
1319 else if (con->ucsi->version >= UCSI_VERSION_2_0)
1320 command |= hard ? 0 : UCSI_CONNECTOR_RESET_DATA_VER_2_0;
1321
1322 return ucsi_send_command(con->ucsi, command, NULL, 0);
1323 }
1324
ucsi_reset_ppm(struct ucsi * ucsi)1325 static int ucsi_reset_ppm(struct ucsi *ucsi)
1326 {
1327 u64 command;
1328 unsigned long tmo;
1329 u32 cci;
1330 int ret;
1331
1332 mutex_lock(&ucsi->ppm_lock);
1333
1334 ret = ucsi->ops->poll_cci(ucsi, &cci);
1335 if (ret < 0)
1336 goto out;
1337
1338 /*
1339 * If UCSI_CCI_RESET_COMPLETE is already set we must clear
1340 * the flag before we start another reset. Send a
1341 * UCSI_SET_NOTIFICATION_ENABLE command to achieve this.
1342 * Ignore a timeout and try the reset anyway if this fails.
1343 */
1344 if (cci & UCSI_CCI_RESET_COMPLETE) {
1345 command = UCSI_SET_NOTIFICATION_ENABLE;
1346 ret = ucsi->ops->async_control(ucsi, command);
1347 if (ret < 0)
1348 goto out;
1349
1350 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1351 do {
1352 ret = ucsi->ops->poll_cci(ucsi, &cci);
1353 if (ret < 0)
1354 goto out;
1355 if (cci & UCSI_CCI_COMMAND_COMPLETE)
1356 break;
1357 if (time_is_before_jiffies(tmo))
1358 break;
1359 msleep(20);
1360 } while (1);
1361
1362 WARN_ON(cci & UCSI_CCI_RESET_COMPLETE);
1363 }
1364
1365 command = UCSI_PPM_RESET;
1366 ret = ucsi->ops->async_control(ucsi, command);
1367 if (ret < 0)
1368 goto out;
1369
1370 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
1371
1372 do {
1373 if (time_is_before_jiffies(tmo)) {
1374 ret = -ETIMEDOUT;
1375 goto out;
1376 }
1377
1378 /* Give the PPM time to process a reset before reading CCI */
1379 msleep(20);
1380
1381 ret = ucsi->ops->poll_cci(ucsi, &cci);
1382 if (ret)
1383 goto out;
1384
1385 /* If the PPM is still doing something else, reset it again. */
1386 if (cci & ~UCSI_CCI_RESET_COMPLETE) {
1387 ret = ucsi->ops->async_control(ucsi, command);
1388 if (ret < 0)
1389 goto out;
1390 }
1391
1392 } while (!(cci & UCSI_CCI_RESET_COMPLETE));
1393
1394 out:
1395 mutex_unlock(&ucsi->ppm_lock);
1396 return ret;
1397 }
1398
ucsi_role_cmd(struct ucsi_connector * con,u64 command)1399 static int ucsi_role_cmd(struct ucsi_connector *con, u64 command)
1400 {
1401 int ret;
1402
1403 ret = ucsi_send_command(con->ucsi, command, NULL, 0);
1404 if (ret == -ETIMEDOUT) {
1405 u64 c;
1406
1407 /* PPM most likely stopped responding. Resetting everything. */
1408 ucsi_reset_ppm(con->ucsi);
1409
1410 c = UCSI_SET_NOTIFICATION_ENABLE | con->ucsi->ntfy;
1411 ucsi_send_command(con->ucsi, c, NULL, 0);
1412
1413 ucsi_reset_connector(con, true);
1414 }
1415
1416 return ret;
1417 }
1418
ucsi_dr_swap(struct typec_port * port,enum typec_data_role role)1419 static int ucsi_dr_swap(struct typec_port *port, enum typec_data_role role)
1420 {
1421 struct ucsi_connector *con = typec_get_drvdata(port);
1422 u8 partner_type;
1423 u64 command;
1424 int ret = 0;
1425
1426 mutex_lock(&con->lock);
1427
1428 if (!con->partner) {
1429 ret = -ENOTCONN;
1430 goto out_unlock;
1431 }
1432
1433 partner_type = UCSI_CONSTAT_PARTNER_TYPE(con->status.flags);
1434 if ((partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
1435 role == TYPEC_DEVICE) ||
1436 (partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
1437 role == TYPEC_HOST))
1438 goto out_unlock;
1439
1440 reinit_completion(&con->complete);
1441
1442 command = UCSI_SET_UOR | UCSI_CONNECTOR_NUMBER(con->num);
1443 command |= UCSI_SET_UOR_ROLE(role);
1444 command |= UCSI_SET_UOR_ACCEPT_ROLE_SWAPS;
1445 ret = ucsi_role_cmd(con, command);
1446 if (ret < 0)
1447 goto out_unlock;
1448
1449 mutex_unlock(&con->lock);
1450
1451 if (!wait_for_completion_timeout(&con->complete,
1452 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1453 return -ETIMEDOUT;
1454
1455 return 0;
1456
1457 out_unlock:
1458 mutex_unlock(&con->lock);
1459
1460 return ret;
1461 }
1462
ucsi_pr_swap(struct typec_port * port,enum typec_role role)1463 static int ucsi_pr_swap(struct typec_port *port, enum typec_role role)
1464 {
1465 struct ucsi_connector *con = typec_get_drvdata(port);
1466 enum typec_role cur_role;
1467 u64 command;
1468 int ret = 0;
1469
1470 mutex_lock(&con->lock);
1471
1472 if (!con->partner) {
1473 ret = -ENOTCONN;
1474 goto out_unlock;
1475 }
1476
1477 cur_role = !!(con->status.flags & UCSI_CONSTAT_PWR_DIR);
1478
1479 if (cur_role == role)
1480 goto out_unlock;
1481
1482 reinit_completion(&con->complete);
1483
1484 command = UCSI_SET_PDR | UCSI_CONNECTOR_NUMBER(con->num);
1485 command |= UCSI_SET_PDR_ROLE(role);
1486 command |= UCSI_SET_PDR_ACCEPT_ROLE_SWAPS;
1487 ret = ucsi_role_cmd(con, command);
1488 if (ret < 0)
1489 goto out_unlock;
1490
1491 mutex_unlock(&con->lock);
1492
1493 if (!wait_for_completion_timeout(&con->complete,
1494 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
1495 return -ETIMEDOUT;
1496
1497 mutex_lock(&con->lock);
1498
1499 /* Something has gone wrong while swapping the role */
1500 if (UCSI_CONSTAT_PWR_OPMODE(con->status.flags) !=
1501 UCSI_CONSTAT_PWR_OPMODE_PD) {
1502 ucsi_reset_connector(con, true);
1503 ret = -EPROTO;
1504 }
1505
1506 out_unlock:
1507 mutex_unlock(&con->lock);
1508
1509 return ret;
1510 }
1511
1512 static const struct typec_operations ucsi_ops = {
1513 .dr_set = ucsi_dr_swap,
1514 .pr_set = ucsi_pr_swap
1515 };
1516
1517 /* Caller must call fwnode_handle_put() after use */
ucsi_find_fwnode(struct ucsi_connector * con)1518 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
1519 {
1520 struct fwnode_handle *fwnode;
1521 int i = 1;
1522
1523 device_for_each_child_node(con->ucsi->dev, fwnode)
1524 if (i++ == con->num)
1525 return fwnode;
1526 return NULL;
1527 }
1528
ucsi_register_port(struct ucsi * ucsi,struct ucsi_connector * con)1529 static int ucsi_register_port(struct ucsi *ucsi, struct ucsi_connector *con)
1530 {
1531 struct typec_capability *cap = &con->typec_cap;
1532 enum typec_accessory *accessory = cap->accessory;
1533 enum usb_role u_role = USB_ROLE_NONE;
1534 u64 command;
1535 char *name;
1536 int ret;
1537
1538 name = kasprintf(GFP_KERNEL, "%s-con%d", dev_name(ucsi->dev), con->num);
1539 if (!name)
1540 return -ENOMEM;
1541
1542 con->wq = create_singlethread_workqueue(name);
1543 kfree(name);
1544 if (!con->wq)
1545 return -ENOMEM;
1546
1547 INIT_WORK(&con->work, ucsi_handle_connector_change);
1548 init_completion(&con->complete);
1549 mutex_init(&con->lock);
1550 INIT_LIST_HEAD(&con->partner_tasks);
1551 con->ucsi = ucsi;
1552
1553 cap->fwnode = ucsi_find_fwnode(con);
1554 con->usb_role_sw = fwnode_usb_role_switch_get(cap->fwnode);
1555 if (IS_ERR(con->usb_role_sw))
1556 return dev_err_probe(ucsi->dev, PTR_ERR(con->usb_role_sw),
1557 "con%d: failed to get usb role switch\n", con->num);
1558
1559 /* Delay other interactions with the con until registration is complete */
1560 mutex_lock(&con->lock);
1561
1562 /* Get connector capability */
1563 command = UCSI_GET_CONNECTOR_CAPABILITY;
1564 command |= UCSI_CONNECTOR_NUMBER(con->num);
1565 ret = ucsi_send_command(ucsi, command, &con->cap, sizeof(con->cap));
1566 if (ret < 0)
1567 goto out_unlock;
1568
1569 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
1570 cap->data = TYPEC_PORT_DRD;
1571 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
1572 cap->data = TYPEC_PORT_DFP;
1573 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
1574 cap->data = TYPEC_PORT_UFP;
1575
1576 if ((con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER) &&
1577 (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER))
1578 cap->type = TYPEC_PORT_DRP;
1579 else if (con->cap.flags & UCSI_CONCAP_FLAG_PROVIDER)
1580 cap->type = TYPEC_PORT_SRC;
1581 else if (con->cap.flags & UCSI_CONCAP_FLAG_CONSUMER)
1582 cap->type = TYPEC_PORT_SNK;
1583
1584 cap->revision = ucsi->cap.typec_version;
1585 cap->pd_revision = ucsi->cap.pd_version;
1586 cap->svdm_version = SVDM_VER_2_0;
1587 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
1588
1589 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
1590 *accessory++ = TYPEC_ACCESSORY_AUDIO;
1591 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
1592 *accessory = TYPEC_ACCESSORY_DEBUG;
1593
1594 cap->driver_data = con;
1595 cap->ops = &ucsi_ops;
1596
1597 if (ucsi->ops->update_connector)
1598 ucsi->ops->update_connector(con);
1599
1600 ret = ucsi_register_port_psy(con);
1601 if (ret)
1602 goto out;
1603
1604 /* Register the connector */
1605 con->port = typec_register_port(ucsi->dev, cap);
1606 if (IS_ERR(con->port)) {
1607 ret = PTR_ERR(con->port);
1608 goto out;
1609 }
1610
1611 if (!(ucsi->quirks & UCSI_DELAY_DEVICE_PDOS))
1612 ucsi_register_device_pdos(con);
1613
1614 /* Alternate modes */
1615 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
1616 if (ret) {
1617 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
1618 con->num);
1619 goto out;
1620 }
1621
1622 /* Get the status */
1623 command = UCSI_GET_CONNECTOR_STATUS | UCSI_CONNECTOR_NUMBER(con->num);
1624 ret = ucsi_send_command(ucsi, command, &con->status, sizeof(con->status));
1625 if (ret < 0) {
1626 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
1627 ret = 0;
1628 goto out;
1629 }
1630 ret = 0; /* ucsi_send_command() returns length on success */
1631
1632 if (ucsi->ops->connector_status)
1633 ucsi->ops->connector_status(con);
1634
1635 switch (UCSI_CONSTAT_PARTNER_TYPE(con->status.flags)) {
1636 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
1637 case UCSI_CONSTAT_PARTNER_TYPE_CABLE_AND_UFP:
1638 u_role = USB_ROLE_HOST;
1639 fallthrough;
1640 case UCSI_CONSTAT_PARTNER_TYPE_CABLE:
1641 typec_set_data_role(con->port, TYPEC_HOST);
1642 break;
1643 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
1644 u_role = USB_ROLE_DEVICE;
1645 typec_set_data_role(con->port, TYPEC_DEVICE);
1646 break;
1647 default:
1648 break;
1649 }
1650
1651 /* Check if there is already something connected */
1652 if (con->status.flags & UCSI_CONSTAT_CONNECTED) {
1653 typec_set_pwr_role(con->port,
1654 !!(con->status.flags & UCSI_CONSTAT_PWR_DIR));
1655 ucsi_register_partner(con);
1656 ucsi_pwr_opmode_change(con);
1657 ucsi_port_psy_changed(con);
1658 if (con->ucsi->cap.features & UCSI_CAP_GET_PD_MESSAGE)
1659 ucsi_get_partner_identity(con);
1660 if (con->ucsi->cap.features & UCSI_CAP_CABLE_DETAILS)
1661 ucsi_check_cable(con);
1662 }
1663
1664 /* Only notify USB controller if partner supports USB data */
1665 if (!(UCSI_CONSTAT_PARTNER_FLAGS(con->status.flags) & UCSI_CONSTAT_PARTNER_FLAG_USB))
1666 u_role = USB_ROLE_NONE;
1667
1668 ret = usb_role_switch_set_role(con->usb_role_sw, u_role);
1669 if (ret) {
1670 dev_err(ucsi->dev, "con:%d: failed to set usb role:%d\n",
1671 con->num, u_role);
1672 ret = 0;
1673 }
1674
1675 if (con->partner &&
1676 UCSI_CONSTAT_PWR_OPMODE(con->status.flags) ==
1677 UCSI_CONSTAT_PWR_OPMODE_PD) {
1678 ucsi_register_device_pdos(con);
1679 ucsi_get_src_pdos(con);
1680 ucsi_check_altmodes(con);
1681 ucsi_check_connector_capability(con);
1682 }
1683
1684 trace_ucsi_register_port(con->num, &con->status);
1685
1686 out:
1687 fwnode_handle_put(cap->fwnode);
1688 out_unlock:
1689 mutex_unlock(&con->lock);
1690
1691 if (ret && con->wq) {
1692 destroy_workqueue(con->wq);
1693 con->wq = NULL;
1694 }
1695
1696 return ret;
1697 }
1698
ucsi_get_supported_notifications(struct ucsi * ucsi)1699 static u64 ucsi_get_supported_notifications(struct ucsi *ucsi)
1700 {
1701 u16 features = ucsi->cap.features;
1702 u64 ntfy = UCSI_ENABLE_NTFY_ALL;
1703
1704 if (!(features & UCSI_CAP_ALT_MODE_DETAILS))
1705 ntfy &= ~UCSI_ENABLE_NTFY_CAM_CHANGE;
1706
1707 if (!(features & UCSI_CAP_PDO_DETAILS))
1708 ntfy &= ~(UCSI_ENABLE_NTFY_PWR_LEVEL_CHANGE |
1709 UCSI_ENABLE_NTFY_CAP_CHANGE);
1710
1711 if (!(features & UCSI_CAP_EXT_SUPPLY_NOTIFICATIONS))
1712 ntfy &= ~UCSI_ENABLE_NTFY_EXT_PWR_SRC_CHANGE;
1713
1714 if (!(features & UCSI_CAP_PD_RESET))
1715 ntfy &= ~UCSI_ENABLE_NTFY_PD_RESET_COMPLETE;
1716
1717 if (ucsi->version <= UCSI_VERSION_1_2)
1718 return ntfy;
1719
1720 ntfy |= UCSI_ENABLE_NTFY_SINK_PATH_STS_CHANGE;
1721
1722 if (features & UCSI_CAP_GET_ATTENTION_VDO)
1723 ntfy |= UCSI_ENABLE_NTFY_ATTENTION;
1724
1725 if (features & UCSI_CAP_FW_UPDATE_REQUEST)
1726 ntfy |= UCSI_ENABLE_NTFY_LPM_FW_UPDATE_REQ;
1727
1728 if (features & UCSI_CAP_SECURITY_REQUEST)
1729 ntfy |= UCSI_ENABLE_NTFY_SECURITY_REQ_PARTNER;
1730
1731 if (features & UCSI_CAP_SET_RETIMER_MODE)
1732 ntfy |= UCSI_ENABLE_NTFY_SET_RETIMER_MODE;
1733
1734 return ntfy;
1735 }
1736
1737 /**
1738 * ucsi_init - Initialize UCSI interface
1739 * @ucsi: UCSI to be initialized
1740 *
1741 * Registers all ports @ucsi has and enables all notification events.
1742 */
ucsi_init(struct ucsi * ucsi)1743 static int ucsi_init(struct ucsi *ucsi)
1744 {
1745 struct ucsi_connector *con, *connector;
1746 u64 command, ntfy;
1747 u32 cci;
1748 int ret;
1749 int i;
1750
1751 /* Reset the PPM */
1752 ret = ucsi_reset_ppm(ucsi);
1753 if (ret) {
1754 dev_err(ucsi->dev, "failed to reset PPM!\n");
1755 goto err;
1756 }
1757
1758 /* Enable basic notifications */
1759 ntfy = UCSI_ENABLE_NTFY_CMD_COMPLETE | UCSI_ENABLE_NTFY_ERROR;
1760 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1761 ret = ucsi_send_command(ucsi, command, NULL, 0);
1762 if (ret < 0)
1763 goto err_reset;
1764
1765 /* Get PPM capabilities */
1766 command = UCSI_GET_CAPABILITY;
1767 ret = ucsi_send_command(ucsi, command, &ucsi->cap, sizeof(ucsi->cap));
1768 if (ret < 0)
1769 goto err_reset;
1770
1771 if (!ucsi->cap.num_connectors) {
1772 ret = -ENODEV;
1773 goto err_reset;
1774 }
1775
1776 /* Allocate the connectors. Released in ucsi_unregister() */
1777 connector = kcalloc(ucsi->cap.num_connectors + 1, sizeof(*connector), GFP_KERNEL);
1778 if (!connector) {
1779 ret = -ENOMEM;
1780 goto err_reset;
1781 }
1782
1783 /* Register all connectors */
1784 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1785 connector[i].num = i + 1;
1786 ret = ucsi_register_port(ucsi, &connector[i]);
1787 if (ret)
1788 goto err_unregister;
1789 }
1790
1791 /* Enable all supported notifications */
1792 ntfy = ucsi_get_supported_notifications(ucsi);
1793 command = UCSI_SET_NOTIFICATION_ENABLE | ntfy;
1794 ret = ucsi_send_command(ucsi, command, NULL, 0);
1795 if (ret < 0)
1796 goto err_unregister;
1797
1798 ucsi->connector = connector;
1799 ucsi->ntfy = ntfy;
1800
1801 mutex_lock(&ucsi->ppm_lock);
1802 ret = ucsi->ops->read_cci(ucsi, &cci);
1803 mutex_unlock(&ucsi->ppm_lock);
1804 if (ret)
1805 return ret;
1806 if (UCSI_CCI_CONNECTOR(cci))
1807 ucsi_connector_change(ucsi, UCSI_CCI_CONNECTOR(cci));
1808
1809 return 0;
1810
1811 err_unregister:
1812 for (con = connector; con->port; con++) {
1813 if (con->wq)
1814 destroy_workqueue(con->wq);
1815 ucsi_unregister_partner(con);
1816 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
1817 ucsi_unregister_port_psy(con);
1818
1819 usb_power_delivery_unregister_capabilities(con->port_sink_caps);
1820 con->port_sink_caps = NULL;
1821 usb_power_delivery_unregister_capabilities(con->port_source_caps);
1822 con->port_source_caps = NULL;
1823 usb_power_delivery_unregister(con->pd);
1824 con->pd = NULL;
1825 typec_unregister_port(con->port);
1826 con->port = NULL;
1827 }
1828 kfree(connector);
1829 err_reset:
1830 memset(&ucsi->cap, 0, sizeof(ucsi->cap));
1831 ucsi_reset_ppm(ucsi);
1832 err:
1833 return ret;
1834 }
1835
ucsi_resume_work(struct work_struct * work)1836 static void ucsi_resume_work(struct work_struct *work)
1837 {
1838 struct ucsi *ucsi = container_of(work, struct ucsi, resume_work);
1839 struct ucsi_connector *con;
1840 u64 command;
1841 int ret;
1842
1843 /* Restore UCSI notification enable mask after system resume */
1844 command = UCSI_SET_NOTIFICATION_ENABLE | ucsi->ntfy;
1845 ret = ucsi_send_command(ucsi, command, NULL, 0);
1846 if (ret < 0) {
1847 dev_err(ucsi->dev, "failed to re-enable notifications (%d)\n", ret);
1848 return;
1849 }
1850
1851 for (con = ucsi->connector; con->port; con++) {
1852 mutex_lock(&con->lock);
1853 ucsi_partner_task(con, ucsi_check_connection, 1, 0);
1854 mutex_unlock(&con->lock);
1855 }
1856 }
1857
ucsi_resume(struct ucsi * ucsi)1858 int ucsi_resume(struct ucsi *ucsi)
1859 {
1860 if (ucsi->connector)
1861 queue_work(system_long_wq, &ucsi->resume_work);
1862 return 0;
1863 }
1864 EXPORT_SYMBOL_GPL(ucsi_resume);
1865
ucsi_init_work(struct work_struct * work)1866 static void ucsi_init_work(struct work_struct *work)
1867 {
1868 struct ucsi *ucsi = container_of(work, struct ucsi, work.work);
1869 int ret;
1870
1871 ret = ucsi_init(ucsi);
1872 if (ret)
1873 dev_err_probe(ucsi->dev, ret, "PPM init failed\n");
1874
1875 if (ret == -EPROBE_DEFER) {
1876 if (ucsi->work_count++ > UCSI_ROLE_SWITCH_WAIT_COUNT) {
1877 dev_err(ucsi->dev, "PPM init failed, stop trying\n");
1878 return;
1879 }
1880
1881 queue_delayed_work(system_long_wq, &ucsi->work,
1882 UCSI_ROLE_SWITCH_INTERVAL);
1883 }
1884 }
1885
1886 /**
1887 * ucsi_get_drvdata - Return private driver data pointer
1888 * @ucsi: UCSI interface
1889 */
ucsi_get_drvdata(struct ucsi * ucsi)1890 void *ucsi_get_drvdata(struct ucsi *ucsi)
1891 {
1892 return ucsi->driver_data;
1893 }
1894 EXPORT_SYMBOL_GPL(ucsi_get_drvdata);
1895
1896 /**
1897 * ucsi_set_drvdata - Assign private driver data pointer
1898 * @ucsi: UCSI interface
1899 * @data: Private data pointer
1900 */
ucsi_set_drvdata(struct ucsi * ucsi,void * data)1901 void ucsi_set_drvdata(struct ucsi *ucsi, void *data)
1902 {
1903 ucsi->driver_data = data;
1904 }
1905 EXPORT_SYMBOL_GPL(ucsi_set_drvdata);
1906
1907 /**
1908 * ucsi_con_mutex_lock - Acquire the connector mutex
1909 * @con: The connector interface to lock
1910 *
1911 * Returns true on success, false if the connector is disconnected
1912 */
ucsi_con_mutex_lock(struct ucsi_connector * con)1913 bool ucsi_con_mutex_lock(struct ucsi_connector *con)
1914 {
1915 bool mutex_locked = false;
1916 bool connected = true;
1917
1918 while (connected && !mutex_locked) {
1919 mutex_locked = mutex_trylock(&con->lock) != 0;
1920 connected = con->status.flags & UCSI_CONSTAT_CONNECTED;
1921 if (connected && !mutex_locked)
1922 msleep(20);
1923 }
1924
1925 connected = connected && con->partner;
1926 if (!connected && mutex_locked)
1927 mutex_unlock(&con->lock);
1928
1929 return connected;
1930 }
1931
1932 /**
1933 * ucsi_con_mutex_unlock - Release the connector mutex
1934 * @con: The connector interface to unlock
1935 */
ucsi_con_mutex_unlock(struct ucsi_connector * con)1936 void ucsi_con_mutex_unlock(struct ucsi_connector *con)
1937 {
1938 mutex_unlock(&con->lock);
1939 }
1940
1941 /**
1942 * ucsi_create - Allocate UCSI instance
1943 * @dev: Device interface to the PPM (Platform Policy Manager)
1944 * @ops: I/O routines
1945 */
ucsi_create(struct device * dev,const struct ucsi_operations * ops)1946 struct ucsi *ucsi_create(struct device *dev, const struct ucsi_operations *ops)
1947 {
1948 struct ucsi *ucsi;
1949
1950 if (!ops ||
1951 !ops->read_version || !ops->read_cci || !ops->poll_cci ||
1952 !ops->read_message_in || !ops->sync_control || !ops->async_control)
1953 return ERR_PTR(-EINVAL);
1954
1955 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
1956 if (!ucsi)
1957 return ERR_PTR(-ENOMEM);
1958
1959 INIT_WORK(&ucsi->resume_work, ucsi_resume_work);
1960 INIT_DELAYED_WORK(&ucsi->work, ucsi_init_work);
1961 mutex_init(&ucsi->ppm_lock);
1962 init_completion(&ucsi->complete);
1963 ucsi->dev = dev;
1964 ucsi->ops = ops;
1965
1966 return ucsi;
1967 }
1968 EXPORT_SYMBOL_GPL(ucsi_create);
1969
1970 /**
1971 * ucsi_destroy - Free UCSI instance
1972 * @ucsi: UCSI instance to be freed
1973 */
ucsi_destroy(struct ucsi * ucsi)1974 void ucsi_destroy(struct ucsi *ucsi)
1975 {
1976 ucsi_debugfs_unregister(ucsi);
1977 kfree(ucsi);
1978 }
1979 EXPORT_SYMBOL_GPL(ucsi_destroy);
1980
1981 /**
1982 * ucsi_register - Register UCSI interface
1983 * @ucsi: UCSI instance
1984 */
ucsi_register(struct ucsi * ucsi)1985 int ucsi_register(struct ucsi *ucsi)
1986 {
1987 int ret;
1988
1989 ret = ucsi->ops->read_version(ucsi, &ucsi->version);
1990 if (ret)
1991 return ret;
1992
1993 if (!ucsi->version)
1994 return -ENODEV;
1995
1996 /*
1997 * Version format is JJ.M.N (JJ = Major version, M = Minor version,
1998 * N = sub-minor version).
1999 */
2000 dev_dbg(ucsi->dev, "Registered UCSI interface with version %x.%x.%x",
2001 UCSI_BCD_GET_MAJOR(ucsi->version),
2002 UCSI_BCD_GET_MINOR(ucsi->version),
2003 UCSI_BCD_GET_SUBMINOR(ucsi->version));
2004
2005 queue_delayed_work(system_long_wq, &ucsi->work, 0);
2006
2007 ucsi_debugfs_register(ucsi);
2008 return 0;
2009 }
2010 EXPORT_SYMBOL_GPL(ucsi_register);
2011
2012 /**
2013 * ucsi_unregister - Unregister UCSI interface
2014 * @ucsi: UCSI interface to be unregistered
2015 *
2016 * Unregister UCSI interface that was created with ucsi_register().
2017 */
ucsi_unregister(struct ucsi * ucsi)2018 void ucsi_unregister(struct ucsi *ucsi)
2019 {
2020 u64 cmd = UCSI_SET_NOTIFICATION_ENABLE;
2021 int i;
2022
2023 /* Make sure that we are not in the middle of driver initialization */
2024 cancel_delayed_work_sync(&ucsi->work);
2025 cancel_work_sync(&ucsi->resume_work);
2026
2027 /* Disable notifications */
2028 ucsi->ops->async_control(ucsi, cmd);
2029
2030 if (!ucsi->connector)
2031 return;
2032
2033 for (i = 0; i < ucsi->cap.num_connectors; i++) {
2034 cancel_work_sync(&ucsi->connector[i].work);
2035
2036 if (ucsi->connector[i].wq) {
2037 struct ucsi_work *uwork;
2038
2039 mutex_lock(&ucsi->connector[i].lock);
2040 /*
2041 * queue delayed items immediately so they can execute
2042 * and free themselves before the wq is destroyed
2043 */
2044 list_for_each_entry(uwork, &ucsi->connector[i].partner_tasks, node)
2045 mod_delayed_work(ucsi->connector[i].wq, &uwork->work, 0);
2046 mutex_unlock(&ucsi->connector[i].lock);
2047 destroy_workqueue(ucsi->connector[i].wq);
2048 }
2049
2050 ucsi_unregister_partner(&ucsi->connector[i]);
2051 ucsi_unregister_altmodes(&ucsi->connector[i],
2052 UCSI_RECIPIENT_CON);
2053 ucsi_unregister_port_psy(&ucsi->connector[i]);
2054
2055 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_sink_caps);
2056 ucsi->connector[i].port_sink_caps = NULL;
2057 usb_power_delivery_unregister_capabilities(ucsi->connector[i].port_source_caps);
2058 ucsi->connector[i].port_source_caps = NULL;
2059 usb_power_delivery_unregister(ucsi->connector[i].pd);
2060 ucsi->connector[i].pd = NULL;
2061 typec_unregister_port(ucsi->connector[i].port);
2062 }
2063
2064 kfree(ucsi->connector);
2065 }
2066 EXPORT_SYMBOL_GPL(ucsi_unregister);
2067
ucsi_module_init(void)2068 static int __init ucsi_module_init(void)
2069 {
2070 ucsi_debugfs_init();
2071 return 0;
2072 }
2073 module_init(ucsi_module_init);
2074
ucsi_module_exit(void)2075 static void __exit ucsi_module_exit(void)
2076 {
2077 ucsi_debugfs_exit();
2078 }
2079 module_exit(ucsi_module_exit);
2080
2081 MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
2082 MODULE_LICENSE("GPL v2");
2083 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
2084