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