1
2
3
4
5
6
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#define to_ucsi_connector(_cap_) container_of(_cap_, struct ucsi_connector, \
21 typec_cap)
22
23
24
25
26
27
28
29
30
31#define UCSI_TIMEOUT_MS 5000
32
33
34
35
36
37
38
39
40#define UCSI_SWAP_TIMEOUT_MS 5000
41
42static inline int ucsi_sync(struct ucsi *ucsi)
43{
44 if (ucsi->ppm && ucsi->ppm->sync)
45 return ucsi->ppm->sync(ucsi->ppm);
46 return 0;
47}
48
49static int ucsi_command(struct ucsi *ucsi, struct ucsi_control *ctrl)
50{
51 int ret;
52
53 trace_ucsi_command(ctrl);
54
55 set_bit(COMMAND_PENDING, &ucsi->flags);
56
57 ret = ucsi->ppm->cmd(ucsi->ppm, ctrl);
58 if (ret)
59 goto err_clear_flag;
60
61 if (!wait_for_completion_timeout(&ucsi->complete,
62 msecs_to_jiffies(UCSI_TIMEOUT_MS))) {
63 dev_warn(ucsi->dev, "PPM NOT RESPONDING\n");
64 ret = -ETIMEDOUT;
65 }
66
67err_clear_flag:
68 clear_bit(COMMAND_PENDING, &ucsi->flags);
69
70 return ret;
71}
72
73static int ucsi_ack(struct ucsi *ucsi, u8 ack)
74{
75 struct ucsi_control ctrl;
76 int ret;
77
78 trace_ucsi_ack(ack);
79
80 set_bit(ACK_PENDING, &ucsi->flags);
81
82 UCSI_CMD_ACK(ctrl, ack);
83 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
84 if (ret)
85 goto out_clear_bit;
86
87
88 if (ack == UCSI_ACK_EVENT)
89 goto out_clear_bit;
90
91 if (!wait_for_completion_timeout(&ucsi->complete,
92 msecs_to_jiffies(UCSI_TIMEOUT_MS)))
93 ret = -ETIMEDOUT;
94
95out_clear_bit:
96 clear_bit(ACK_PENDING, &ucsi->flags);
97
98 if (ret)
99 dev_err(ucsi->dev, "%s: failed\n", __func__);
100
101 return ret;
102}
103
104static int ucsi_run_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
105 void *data, size_t size)
106{
107 struct ucsi_control _ctrl;
108 u8 data_length;
109 u16 error;
110 int ret;
111
112 ret = ucsi_command(ucsi, ctrl);
113 if (ret)
114 goto err;
115
116 switch (ucsi->status) {
117 case UCSI_IDLE:
118 ret = ucsi_sync(ucsi);
119 if (ret)
120 dev_warn(ucsi->dev, "%s: sync failed\n", __func__);
121
122 if (data)
123 memcpy(data, ucsi->ppm->data->message_in, size);
124
125 data_length = ucsi->ppm->data->cci.data_length;
126
127 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
128 if (!ret)
129 ret = data_length;
130 break;
131 case UCSI_BUSY:
132
133 ret = -EBUSY;
134 break;
135 case UCSI_ERROR:
136 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
137 if (ret)
138 break;
139
140 _ctrl.raw_cmd = 0;
141 _ctrl.cmd.cmd = UCSI_GET_ERROR_STATUS;
142 ret = ucsi_command(ucsi, &_ctrl);
143 if (ret) {
144 dev_err(ucsi->dev, "reading error failed!\n");
145 break;
146 }
147
148 memcpy(&error, ucsi->ppm->data->message_in, sizeof(error));
149
150
151 if (WARN_ON(ucsi->status == UCSI_ERROR)) {
152 ret = -ENODEV;
153 break;
154 }
155
156 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
157 if (ret)
158 break;
159
160 switch (error) {
161 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
162 ret = -EOPNOTSUPP;
163 break;
164 case UCSI_ERROR_CC_COMMUNICATION_ERR:
165 ret = -ECOMM;
166 break;
167 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
168 ret = -EPROTO;
169 break;
170 case UCSI_ERROR_DEAD_BATTERY:
171 dev_warn(ucsi->dev, "Dead battery condition!\n");
172 ret = -EPERM;
173 break;
174
175 case UCSI_ERROR_INVALID_CON_NUM:
176 case UCSI_ERROR_UNREGONIZED_CMD:
177 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
178 dev_warn(ucsi->dev,
179 "%s: possible UCSI driver bug - error 0x%x\n",
180 __func__, error);
181 ret = -EINVAL;
182 break;
183 default:
184 dev_warn(ucsi->dev,
185 "%s: error without status\n", __func__);
186 ret = -EIO;
187 break;
188 }
189 break;
190 }
191
192err:
193 trace_ucsi_run_command(ctrl, ret);
194
195 return ret;
196}
197
198int ucsi_send_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
199 void *retval, size_t size)
200{
201 int ret;
202
203 mutex_lock(&ucsi->ppm_lock);
204 ret = ucsi_run_command(ucsi, ctrl, retval, size);
205 mutex_unlock(&ucsi->ppm_lock);
206
207 return ret;
208}
209
210
211
212void ucsi_altmode_update_active(struct ucsi_connector *con)
213{
214 const struct typec_altmode *altmode = NULL;
215 struct ucsi_control ctrl;
216 int ret;
217 u8 cur;
218 int i;
219
220 UCSI_CMD_GET_CURRENT_CAM(ctrl, con->num);
221 ret = ucsi_run_command(con->ucsi, &ctrl, &cur, sizeof(cur));
222 if (ret < 0) {
223 if (con->ucsi->ppm->data->version > 0x0100) {
224 dev_err(con->ucsi->dev,
225 "GET_CURRENT_CAM command failed\n");
226 return;
227 }
228 cur = 0xff;
229 }
230
231 if (cur < UCSI_MAX_ALTMODES)
232 altmode = typec_altmode_get_partner(con->port_altmode[cur]);
233
234 for (i = 0; con->partner_altmode[i]; i++)
235 typec_altmode_update_active(con->partner_altmode[i],
236 con->partner_altmode[i] == altmode);
237}
238
239static u8 ucsi_altmode_next_mode(struct typec_altmode **alt, u16 svid)
240{
241 u8 mode = 1;
242 int i;
243
244 for (i = 0; alt[i]; i++)
245 if (alt[i]->svid == svid)
246 mode++;
247
248 return mode;
249}
250
251static int ucsi_next_altmode(struct typec_altmode **alt)
252{
253 int i = 0;
254
255 for (i = 0; i < UCSI_MAX_ALTMODES; i++)
256 if (!alt[i])
257 return i;
258
259 return -ENOENT;
260}
261
262static int ucsi_register_altmode(struct ucsi_connector *con,
263 struct typec_altmode_desc *desc,
264 u8 recipient)
265{
266 struct typec_altmode *alt;
267 bool override;
268 int ret;
269 int i;
270
271 override = !!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_OVERRIDE);
272
273 switch (recipient) {
274 case UCSI_RECIPIENT_CON:
275 i = ucsi_next_altmode(con->port_altmode);
276 if (i < 0) {
277 ret = i;
278 goto err;
279 }
280
281 desc->mode = ucsi_altmode_next_mode(con->port_altmode,
282 desc->svid);
283
284 switch (desc->svid) {
285 case USB_TYPEC_DP_SID:
286 case USB_TYPEC_NVIDIA_VLINK_SID:
287 alt = ucsi_register_displayport(con, override, i, desc);
288 break;
289 default:
290 alt = typec_port_register_altmode(con->port, desc);
291 break;
292 }
293
294 if (IS_ERR(alt)) {
295 ret = PTR_ERR(alt);
296 goto err;
297 }
298
299 con->port_altmode[i] = alt;
300 break;
301 case UCSI_RECIPIENT_SOP:
302 i = ucsi_next_altmode(con->partner_altmode);
303 if (i < 0) {
304 ret = i;
305 goto err;
306 }
307
308 desc->mode = ucsi_altmode_next_mode(con->partner_altmode,
309 desc->svid);
310
311 alt = typec_partner_register_altmode(con->partner, desc);
312 if (IS_ERR(alt)) {
313 ret = PTR_ERR(alt);
314 goto err;
315 }
316
317 con->partner_altmode[i] = alt;
318 break;
319 default:
320 return -EINVAL;
321 }
322
323 trace_ucsi_register_altmode(recipient, alt);
324
325 return 0;
326
327err:
328 dev_err(con->ucsi->dev, "failed to registers svid 0x%04x mode %d\n",
329 desc->svid, desc->mode);
330
331 return ret;
332}
333
334static int ucsi_register_altmodes(struct ucsi_connector *con, u8 recipient)
335{
336 int max_altmodes = UCSI_MAX_ALTMODES;
337 struct typec_altmode_desc desc;
338 struct ucsi_altmode alt[2];
339 struct ucsi_control ctrl;
340 int num = 1;
341 int ret;
342 int len;
343 int j;
344 int i;
345
346 if (!(con->ucsi->cap.features & UCSI_CAP_ALT_MODE_DETAILS))
347 return 0;
348
349 if (recipient == UCSI_RECIPIENT_SOP && con->partner_altmode[0])
350 return 0;
351
352 if (recipient == UCSI_RECIPIENT_CON)
353 max_altmodes = con->ucsi->cap.num_alt_modes;
354
355 for (i = 0; i < max_altmodes;) {
356 memset(alt, 0, sizeof(alt));
357 UCSI_CMD_GET_ALTERNATE_MODES(ctrl, recipient, con->num, i, 1);
358 len = ucsi_run_command(con->ucsi, &ctrl, alt, sizeof(alt));
359 if (len <= 0)
360 return len;
361
362
363
364
365
366
367
368 num = len / sizeof(alt[0]);
369 i += num;
370
371 for (j = 0; j < num; j++) {
372 if (!alt[j].svid)
373 return 0;
374
375 memset(&desc, 0, sizeof(desc));
376 desc.vdo = alt[j].mid;
377 desc.svid = alt[j].svid;
378 desc.roles = TYPEC_PORT_DRD;
379
380 ret = ucsi_register_altmode(con, &desc, recipient);
381 if (ret)
382 return ret;
383 }
384 }
385
386 return 0;
387}
388
389static void ucsi_unregister_altmodes(struct ucsi_connector *con, u8 recipient)
390{
391 const struct typec_altmode *pdev;
392 struct typec_altmode **adev;
393 int i = 0;
394
395 switch (recipient) {
396 case UCSI_RECIPIENT_CON:
397 adev = con->port_altmode;
398 break;
399 case UCSI_RECIPIENT_SOP:
400 adev = con->partner_altmode;
401 break;
402 default:
403 return;
404 }
405
406 while (adev[i]) {
407 if (recipient == UCSI_RECIPIENT_SOP &&
408 (adev[i]->svid == USB_TYPEC_DP_SID ||
409 adev[i]->svid == USB_TYPEC_NVIDIA_VLINK_SID)) {
410 pdev = typec_altmode_get_partner(adev[i]);
411 ucsi_displayport_remove_partner((void *)pdev);
412 }
413 typec_unregister_altmode(adev[i]);
414 adev[i++] = NULL;
415 }
416}
417
418static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
419{
420 switch (con->status.pwr_op_mode) {
421 case UCSI_CONSTAT_PWR_OPMODE_PD:
422 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
423 break;
424 case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
425 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
426 break;
427 case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
428 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
429 break;
430 default:
431 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
432 break;
433 }
434}
435
436static int ucsi_register_partner(struct ucsi_connector *con)
437{
438 struct typec_partner_desc desc;
439 struct typec_partner *partner;
440
441 if (con->partner)
442 return 0;
443
444 memset(&desc, 0, sizeof(desc));
445
446 switch (con->status.partner_type) {
447 case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
448 desc.accessory = TYPEC_ACCESSORY_DEBUG;
449 break;
450 case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
451 desc.accessory = TYPEC_ACCESSORY_AUDIO;
452 break;
453 default:
454 break;
455 }
456
457 desc.usb_pd = con->status.pwr_op_mode == UCSI_CONSTAT_PWR_OPMODE_PD;
458
459 partner = typec_register_partner(con->port, &desc);
460 if (IS_ERR(partner)) {
461 dev_err(con->ucsi->dev,
462 "con%d: failed to register partner (%ld)\n", con->num,
463 PTR_ERR(partner));
464 return PTR_ERR(partner);
465 }
466
467 con->partner = partner;
468
469 return 0;
470}
471
472static void ucsi_unregister_partner(struct ucsi_connector *con)
473{
474 if (!con->partner)
475 return;
476
477 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_SOP);
478 typec_unregister_partner(con->partner);
479 con->partner = NULL;
480}
481
482static void ucsi_partner_change(struct ucsi_connector *con)
483{
484 int ret;
485
486 if (!con->partner)
487 return;
488
489 switch (con->status.partner_type) {
490 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
491 typec_set_data_role(con->port, TYPEC_HOST);
492 break;
493 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
494 typec_set_data_role(con->port, TYPEC_DEVICE);
495 break;
496 default:
497 break;
498 }
499
500
501 if (!completion_done(&con->complete))
502 complete(&con->complete);
503
504
505 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
506 if (ret)
507 dev_err(con->ucsi->dev,
508 "con%d: failed to register partner alternate modes\n",
509 con->num);
510 else
511 ucsi_altmode_update_active(con);
512}
513
514static void ucsi_connector_change(struct work_struct *work)
515{
516 struct ucsi_connector *con = container_of(work, struct ucsi_connector,
517 work);
518 struct ucsi *ucsi = con->ucsi;
519 struct ucsi_control ctrl;
520 int ret;
521
522 mutex_lock(&con->lock);
523
524 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
525 ret = ucsi_send_command(ucsi, &ctrl, &con->status, sizeof(con->status));
526 if (ret < 0) {
527 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
528 __func__, ret);
529 goto out_unlock;
530 }
531
532 if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE)
533 ucsi_pwr_opmode_change(con);
534
535 if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
536 typec_set_pwr_role(con->port, con->status.pwr_dir);
537
538
539 if (!completion_done(&con->complete))
540 complete(&con->complete);
541 }
542
543 if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
544 typec_set_pwr_role(con->port, con->status.pwr_dir);
545
546 switch (con->status.partner_type) {
547 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
548 typec_set_data_role(con->port, TYPEC_HOST);
549 break;
550 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
551 typec_set_data_role(con->port, TYPEC_DEVICE);
552 break;
553 default:
554 break;
555 }
556
557 if (con->status.connected)
558 ucsi_register_partner(con);
559 else
560 ucsi_unregister_partner(con);
561 }
562
563 if (con->status.change & UCSI_CONSTAT_CAM_CHANGE) {
564
565
566
567
568
569 UCSI_CMD_GET_CAM_SUPPORTED(ctrl, con->num);
570 ucsi_run_command(con->ucsi, &ctrl, NULL, 0);
571 }
572
573 if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE)
574 ucsi_partner_change(con);
575
576 ret = ucsi_ack(ucsi, UCSI_ACK_EVENT);
577 if (ret)
578 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
579
580 trace_ucsi_connector_change(con->num, &con->status);
581
582out_unlock:
583 clear_bit(EVENT_PENDING, &ucsi->flags);
584 mutex_unlock(&con->lock);
585}
586
587
588
589
590
591
592
593void ucsi_notify(struct ucsi *ucsi)
594{
595 struct ucsi_cci *cci;
596
597
598 ucsi_sync(ucsi);
599
600 cci = &ucsi->ppm->data->cci;
601
602 if (cci->error)
603 ucsi->status = UCSI_ERROR;
604 else if (cci->busy)
605 ucsi->status = UCSI_BUSY;
606 else
607 ucsi->status = UCSI_IDLE;
608
609 if (cci->cmd_complete && test_bit(COMMAND_PENDING, &ucsi->flags)) {
610 complete(&ucsi->complete);
611 } else if (cci->ack_complete && test_bit(ACK_PENDING, &ucsi->flags)) {
612 complete(&ucsi->complete);
613 } else if (cci->connector_change) {
614 struct ucsi_connector *con;
615
616 con = &ucsi->connector[cci->connector_change - 1];
617
618 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
619 schedule_work(&con->work);
620 }
621
622 trace_ucsi_notify(ucsi->ppm->data->raw_cci);
623}
624EXPORT_SYMBOL_GPL(ucsi_notify);
625
626
627
628static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
629{
630 struct ucsi_control ctrl;
631
632 UCSI_CMD_CONNECTOR_RESET(ctrl, con, hard);
633
634 return ucsi_send_command(con->ucsi, &ctrl, NULL, 0);
635}
636
637static int ucsi_reset_ppm(struct ucsi *ucsi)
638{
639 struct ucsi_control ctrl;
640 unsigned long tmo;
641 int ret;
642
643 ctrl.raw_cmd = 0;
644 ctrl.cmd.cmd = UCSI_PPM_RESET;
645 trace_ucsi_command(&ctrl);
646 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
647 if (ret)
648 goto err;
649
650 tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
651
652 do {
653
654 ret = ucsi_sync(ucsi);
655 if (ret)
656 goto err;
657
658 if (ucsi->ppm->data->cci.reset_complete)
659 break;
660
661
662 if (ucsi->ppm->data->raw_cci) {
663 dev_warn_ratelimited(ucsi->dev,
664 "Failed to reset PPM! Trying again..\n");
665
666 trace_ucsi_command(&ctrl);
667 ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
668 if (ret)
669 goto err;
670 }
671
672
673 msleep(20);
674
675 ret = -ETIMEDOUT;
676 } while (time_is_after_jiffies(tmo));
677
678err:
679 trace_ucsi_reset_ppm(&ctrl, ret);
680
681 return ret;
682}
683
684static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
685{
686 int ret;
687
688 ret = ucsi_send_command(con->ucsi, ctrl, NULL, 0);
689 if (ret == -ETIMEDOUT) {
690 struct ucsi_control c;
691
692
693 mutex_lock(&con->ucsi->ppm_lock);
694 ucsi_reset_ppm(con->ucsi);
695 mutex_unlock(&con->ucsi->ppm_lock);
696
697 UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
698 ucsi_send_command(con->ucsi, &c, NULL, 0);
699
700 ucsi_reset_connector(con, true);
701 }
702
703 return ret;
704}
705
706static int
707ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
708{
709 struct ucsi_connector *con = to_ucsi_connector(cap);
710 struct ucsi_control ctrl;
711 int ret = 0;
712
713 mutex_lock(&con->lock);
714
715 if (!con->partner) {
716 ret = -ENOTCONN;
717 goto out_unlock;
718 }
719
720 if ((con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
721 role == TYPEC_DEVICE) ||
722 (con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
723 role == TYPEC_HOST))
724 goto out_unlock;
725
726 UCSI_CMD_SET_UOR(ctrl, con, role);
727 ret = ucsi_role_cmd(con, &ctrl);
728 if (ret < 0)
729 goto out_unlock;
730
731 if (!wait_for_completion_timeout(&con->complete,
732 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
733 ret = -ETIMEDOUT;
734
735out_unlock:
736 mutex_unlock(&con->lock);
737
738 return ret < 0 ? ret : 0;
739}
740
741static int
742ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
743{
744 struct ucsi_connector *con = to_ucsi_connector(cap);
745 struct ucsi_control ctrl;
746 int ret = 0;
747
748 mutex_lock(&con->lock);
749
750 if (!con->partner) {
751 ret = -ENOTCONN;
752 goto out_unlock;
753 }
754
755 if (con->status.pwr_dir == role)
756 goto out_unlock;
757
758 UCSI_CMD_SET_PDR(ctrl, con, role);
759 ret = ucsi_role_cmd(con, &ctrl);
760 if (ret < 0)
761 goto out_unlock;
762
763 if (!wait_for_completion_timeout(&con->complete,
764 msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS))) {
765 ret = -ETIMEDOUT;
766 goto out_unlock;
767 }
768
769
770 if (con->status.pwr_op_mode != UCSI_CONSTAT_PWR_OPMODE_PD) {
771 ucsi_reset_connector(con, true);
772 ret = -EPROTO;
773 }
774
775out_unlock:
776 mutex_unlock(&con->lock);
777
778 return ret;
779}
780
781static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
782{
783 struct fwnode_handle *fwnode;
784 int i = 1;
785
786 device_for_each_child_node(con->ucsi->dev, fwnode)
787 if (i++ == con->num)
788 return fwnode;
789 return NULL;
790}
791
792static int ucsi_register_port(struct ucsi *ucsi, int index)
793{
794 struct ucsi_connector *con = &ucsi->connector[index];
795 struct typec_capability *cap = &con->typec_cap;
796 enum typec_accessory *accessory = cap->accessory;
797 struct ucsi_control ctrl;
798 int ret;
799
800 INIT_WORK(&con->work, ucsi_connector_change);
801 init_completion(&con->complete);
802 mutex_init(&con->lock);
803 con->num = index + 1;
804 con->ucsi = ucsi;
805
806
807 UCSI_CMD_GET_CONNECTOR_CAPABILITY(ctrl, con->num);
808 ret = ucsi_run_command(ucsi, &ctrl, &con->cap, sizeof(con->cap));
809 if (ret < 0)
810 return ret;
811
812 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
813 cap->data = TYPEC_PORT_DRD;
814 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
815 cap->data = TYPEC_PORT_DFP;
816 else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
817 cap->data = TYPEC_PORT_UFP;
818
819 if (con->cap.provider && con->cap.consumer)
820 cap->type = TYPEC_PORT_DRP;
821 else if (con->cap.provider)
822 cap->type = TYPEC_PORT_SRC;
823 else if (con->cap.consumer)
824 cap->type = TYPEC_PORT_SNK;
825
826 cap->revision = ucsi->cap.typec_version;
827 cap->pd_revision = ucsi->cap.pd_version;
828 cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
829
830 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
831 *accessory++ = TYPEC_ACCESSORY_AUDIO;
832 if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
833 *accessory = TYPEC_ACCESSORY_DEBUG;
834
835 cap->fwnode = ucsi_find_fwnode(con);
836 cap->dr_set = ucsi_dr_swap;
837 cap->pr_set = ucsi_pr_swap;
838
839
840 con->port = typec_register_port(ucsi->dev, cap);
841 if (IS_ERR(con->port))
842 return PTR_ERR(con->port);
843
844
845 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_CON);
846 if (ret)
847 dev_err(ucsi->dev, "con%d: failed to register alt modes\n",
848 con->num);
849
850
851 UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
852 ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
853 if (ret < 0) {
854 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
855 return 0;
856 }
857
858 ucsi_pwr_opmode_change(con);
859 typec_set_pwr_role(con->port, con->status.pwr_dir);
860
861 switch (con->status.partner_type) {
862 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
863 typec_set_data_role(con->port, TYPEC_HOST);
864 break;
865 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
866 typec_set_data_role(con->port, TYPEC_DEVICE);
867 break;
868 default:
869 break;
870 }
871
872
873 if (con->status.connected)
874 ucsi_register_partner(con);
875
876 if (con->partner) {
877 ret = ucsi_register_altmodes(con, UCSI_RECIPIENT_SOP);
878 if (ret)
879 dev_err(ucsi->dev,
880 "con%d: failed to register alternate modes\n",
881 con->num);
882 else
883 ucsi_altmode_update_active(con);
884 }
885
886 trace_ucsi_register_port(con->num, &con->status);
887
888 return 0;
889}
890
891static void ucsi_init(struct work_struct *work)
892{
893 struct ucsi *ucsi = container_of(work, struct ucsi, work);
894 struct ucsi_connector *con;
895 struct ucsi_control ctrl;
896 int ret;
897 int i;
898
899 mutex_lock(&ucsi->ppm_lock);
900
901
902 ret = ucsi_reset_ppm(ucsi);
903 if (ret) {
904 dev_err(ucsi->dev, "failed to reset PPM!\n");
905 goto err;
906 }
907
908
909 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE |
910 UCSI_ENABLE_NTFY_ERROR);
911 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
912 if (ret < 0)
913 goto err_reset;
914
915
916 UCSI_CMD_GET_CAPABILITY(ctrl);
917 ret = ucsi_run_command(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
918 if (ret < 0)
919 goto err_reset;
920
921 if (!ucsi->cap.num_connectors) {
922 ret = -ENODEV;
923 goto err_reset;
924 }
925
926
927 ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
928 sizeof(*ucsi->connector), GFP_KERNEL);
929 if (!ucsi->connector) {
930 ret = -ENOMEM;
931 goto err_reset;
932 }
933
934
935 for (i = 0; i < ucsi->cap.num_connectors; i++) {
936 ret = ucsi_register_port(ucsi, i);
937 if (ret)
938 goto err_unregister;
939 }
940
941
942 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_ALL);
943 ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
944 if (ret < 0)
945 goto err_unregister;
946
947 mutex_unlock(&ucsi->ppm_lock);
948
949 return;
950
951err_unregister:
952 for (con = ucsi->connector; con->port; con++) {
953 ucsi_unregister_partner(con);
954 ucsi_unregister_altmodes(con, UCSI_RECIPIENT_CON);
955 typec_unregister_port(con->port);
956 con->port = NULL;
957 }
958
959err_reset:
960 ucsi_reset_ppm(ucsi);
961err:
962 mutex_unlock(&ucsi->ppm_lock);
963 dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
964}
965
966
967
968
969
970
971
972
973
974struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm)
975{
976 struct ucsi *ucsi;
977
978 ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
979 if (!ucsi)
980 return ERR_PTR(-ENOMEM);
981
982 INIT_WORK(&ucsi->work, ucsi_init);
983 init_completion(&ucsi->complete);
984 mutex_init(&ucsi->ppm_lock);
985
986 ucsi->dev = dev;
987 ucsi->ppm = ppm;
988
989
990
991
992
993 queue_work(system_long_wq, &ucsi->work);
994
995 return ucsi;
996}
997EXPORT_SYMBOL_GPL(ucsi_register_ppm);
998
999
1000
1001
1002
1003
1004
1005void ucsi_unregister_ppm(struct ucsi *ucsi)
1006{
1007 struct ucsi_control ctrl;
1008 int i;
1009
1010
1011 cancel_work_sync(&ucsi->work);
1012
1013
1014 UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE)
1015 ucsi_send_command(ucsi, &ctrl, NULL, 0);
1016
1017 for (i = 0; i < ucsi->cap.num_connectors; i++) {
1018 cancel_work_sync(&ucsi->connector[i].work);
1019 ucsi_unregister_partner(&ucsi->connector[i]);
1020 ucsi_unregister_altmodes(&ucsi->connector[i],
1021 UCSI_RECIPIENT_CON);
1022 typec_unregister_port(ucsi->connector[i].port);
1023 }
1024
1025 ucsi_reset_ppm(ucsi);
1026
1027 kfree(ucsi->connector);
1028 kfree(ucsi);
1029}
1030EXPORT_SYMBOL_GPL(ucsi_unregister_ppm);
1031
1032MODULE_AUTHOR("Heikki Krogerus <heikki.krogerus@linux.intel.com>");
1033MODULE_LICENSE("GPL v2");
1034MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
1035