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