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