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