]> Git Repo - linux.git/blob - drivers/usb/typec/ucsi/ucsi.c
Merge tag 'acpi-fix-4.15-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/rafael...
[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.h>
16
17 #include "ucsi.h"
18 #include "trace.h"
19
20 #define to_ucsi_connector(_cap_) container_of(_cap_, struct ucsi_connector, \
21                                               typec_cap)
22
23 /*
24  * UCSI_TIMEOUT_MS - PPM communication timeout
25  *
26  * Ideally we could use MIN_TIME_TO_RESPOND_WITH_BUSY (which is defined in UCSI
27  * specification) here as reference, but unfortunately we can't. It is very
28  * difficult to estimate the time it takes for the system to process the command
29  * before it is actually passed to the PPM.
30  */
31 #define UCSI_TIMEOUT_MS         1000
32
33 /*
34  * UCSI_SWAP_TIMEOUT_MS - Timeout for role swap requests
35  *
36  * 5 seconds is close to the time it takes for CapsCounter to reach 0, so even
37  * if the PPM does not generate Connector Change events before that with
38  * partners that do not support USB Power Delivery, this should still work.
39  */
40 #define UCSI_SWAP_TIMEOUT_MS    5000
41
42 enum ucsi_status {
43         UCSI_IDLE = 0,
44         UCSI_BUSY,
45         UCSI_ERROR,
46 };
47
48 struct ucsi_connector {
49         int num;
50
51         struct ucsi *ucsi;
52         struct work_struct work;
53         struct completion complete;
54
55         struct typec_port *port;
56         struct typec_partner *partner;
57
58         struct typec_capability typec_cap;
59
60         struct ucsi_connector_status status;
61         struct ucsi_connector_capability cap;
62 };
63
64 struct ucsi {
65         struct device *dev;
66         struct ucsi_ppm *ppm;
67
68         enum ucsi_status status;
69         struct completion complete;
70         struct ucsi_capability cap;
71         struct ucsi_connector *connector;
72
73         struct work_struct work;
74
75         /* PPM Communication lock */
76         struct mutex ppm_lock;
77
78         /* PPM communication flags */
79         unsigned long flags;
80 #define EVENT_PENDING   0
81 #define COMMAND_PENDING 1
82 #define ACK_PENDING     2
83 };
84
85 static inline int ucsi_sync(struct ucsi *ucsi)
86 {
87         if (ucsi->ppm && ucsi->ppm->sync)
88                 return ucsi->ppm->sync(ucsi->ppm);
89         return 0;
90 }
91
92 static int ucsi_command(struct ucsi *ucsi, struct ucsi_control *ctrl)
93 {
94         int ret;
95
96         trace_ucsi_command(ctrl);
97
98         set_bit(COMMAND_PENDING, &ucsi->flags);
99
100         ret = ucsi->ppm->cmd(ucsi->ppm, ctrl);
101         if (ret)
102                 goto err_clear_flag;
103
104         if (!wait_for_completion_timeout(&ucsi->complete,
105                                          msecs_to_jiffies(UCSI_TIMEOUT_MS))) {
106                 dev_warn(ucsi->dev, "PPM NOT RESPONDING\n");
107                 ret = -ETIMEDOUT;
108         }
109
110 err_clear_flag:
111         clear_bit(COMMAND_PENDING, &ucsi->flags);
112
113         return ret;
114 }
115
116 static int ucsi_ack(struct ucsi *ucsi, u8 ack)
117 {
118         struct ucsi_control ctrl;
119         int ret;
120
121         trace_ucsi_ack(ack);
122
123         set_bit(ACK_PENDING, &ucsi->flags);
124
125         UCSI_CMD_ACK(ctrl, ack);
126         ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
127         if (ret)
128                 goto out_clear_bit;
129
130         /* Waiting for ACK with ACK CMD, but not with EVENT for now */
131         if (ack == UCSI_ACK_EVENT)
132                 goto out_clear_bit;
133
134         if (!wait_for_completion_timeout(&ucsi->complete,
135                                          msecs_to_jiffies(UCSI_TIMEOUT_MS)))
136                 ret = -ETIMEDOUT;
137
138 out_clear_bit:
139         clear_bit(ACK_PENDING, &ucsi->flags);
140
141         if (ret)
142                 dev_err(ucsi->dev, "%s: failed\n", __func__);
143
144         return ret;
145 }
146
147 static int ucsi_run_command(struct ucsi *ucsi, struct ucsi_control *ctrl,
148                             void *data, size_t size)
149 {
150         struct ucsi_control _ctrl;
151         u8 data_length;
152         u16 error;
153         int ret;
154
155         ret = ucsi_command(ucsi, ctrl);
156         if (ret)
157                 goto err;
158
159         switch (ucsi->status) {
160         case UCSI_IDLE:
161                 ret = ucsi_sync(ucsi);
162                 if (ret)
163                         dev_warn(ucsi->dev, "%s: sync failed\n", __func__);
164
165                 if (data)
166                         memcpy(data, ucsi->ppm->data->message_in, size);
167
168                 data_length = ucsi->ppm->data->cci.data_length;
169
170                 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
171                 if (!ret)
172                         ret = data_length;
173                 break;
174         case UCSI_BUSY:
175                 /* The caller decides whether to cancel or not */
176                 ret = -EBUSY;
177                 break;
178         case UCSI_ERROR:
179                 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
180                 if (ret)
181                         break;
182
183                 _ctrl.raw_cmd = 0;
184                 _ctrl.cmd.cmd = UCSI_GET_ERROR_STATUS;
185                 ret = ucsi_command(ucsi, &_ctrl);
186                 if (ret) {
187                         dev_err(ucsi->dev, "reading error failed!\n");
188                         break;
189                 }
190
191                 memcpy(&error, ucsi->ppm->data->message_in, sizeof(error));
192
193                 /* Something has really gone wrong */
194                 if (WARN_ON(ucsi->status == UCSI_ERROR)) {
195                         ret = -ENODEV;
196                         break;
197                 }
198
199                 ret = ucsi_ack(ucsi, UCSI_ACK_CMD);
200                 if (ret)
201                         break;
202
203                 switch (error) {
204                 case UCSI_ERROR_INCOMPATIBLE_PARTNER:
205                         ret = -EOPNOTSUPP;
206                         break;
207                 case UCSI_ERROR_CC_COMMUNICATION_ERR:
208                         ret = -ECOMM;
209                         break;
210                 case UCSI_ERROR_CONTRACT_NEGOTIATION_FAIL:
211                         ret = -EPROTO;
212                         break;
213                 case UCSI_ERROR_DEAD_BATTERY:
214                         dev_warn(ucsi->dev, "Dead battery condition!\n");
215                         ret = -EPERM;
216                         break;
217                 /* The following mean a bug in this driver */
218                 case UCSI_ERROR_INVALID_CON_NUM:
219                 case UCSI_ERROR_UNREGONIZED_CMD:
220                 case UCSI_ERROR_INVALID_CMD_ARGUMENT:
221                         dev_warn(ucsi->dev,
222                                  "%s: possible UCSI driver bug - error 0x%x\n",
223                                  __func__, error);
224                         ret = -EINVAL;
225                         break;
226                 default:
227                         dev_warn(ucsi->dev,
228                                  "%s: error without status\n", __func__);
229                         ret = -EIO;
230                         break;
231                 }
232                 break;
233         }
234
235 err:
236         trace_ucsi_run_command(ctrl, ret);
237
238         return ret;
239 }
240
241 /* -------------------------------------------------------------------------- */
242
243 static void ucsi_pwr_opmode_change(struct ucsi_connector *con)
244 {
245         switch (con->status.pwr_op_mode) {
246         case UCSI_CONSTAT_PWR_OPMODE_PD:
247                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_PD);
248                 break;
249         case UCSI_CONSTAT_PWR_OPMODE_TYPEC1_5:
250                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_1_5A);
251                 break;
252         case UCSI_CONSTAT_PWR_OPMODE_TYPEC3_0:
253                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_3_0A);
254                 break;
255         default:
256                 typec_set_pwr_opmode(con->port, TYPEC_PWR_MODE_USB);
257                 break;
258         }
259 }
260
261 static int ucsi_register_partner(struct ucsi_connector *con)
262 {
263         struct typec_partner_desc partner;
264
265         if (con->partner)
266                 return 0;
267
268         memset(&partner, 0, sizeof(partner));
269
270         switch (con->status.partner_type) {
271         case UCSI_CONSTAT_PARTNER_TYPE_DEBUG:
272                 partner.accessory = TYPEC_ACCESSORY_DEBUG;
273                 break;
274         case UCSI_CONSTAT_PARTNER_TYPE_AUDIO:
275                 partner.accessory = TYPEC_ACCESSORY_AUDIO;
276                 break;
277         default:
278                 break;
279         }
280
281         partner.usb_pd = con->status.pwr_op_mode == UCSI_CONSTAT_PWR_OPMODE_PD;
282
283         con->partner = typec_register_partner(con->port, &partner);
284         if (!con->partner) {
285                 dev_err(con->ucsi->dev, "con%d: failed to register partner\n",
286                         con->num);
287                 return -ENODEV;
288         }
289
290         return 0;
291 }
292
293 static void ucsi_unregister_partner(struct ucsi_connector *con)
294 {
295         typec_unregister_partner(con->partner);
296         con->partner = NULL;
297 }
298
299 static void ucsi_connector_change(struct work_struct *work)
300 {
301         struct ucsi_connector *con = container_of(work, struct ucsi_connector,
302                                                   work);
303         struct ucsi *ucsi = con->ucsi;
304         struct ucsi_control ctrl;
305         int ret;
306
307         mutex_lock(&ucsi->ppm_lock);
308
309         UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
310         ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
311         if (ret < 0) {
312                 dev_err(ucsi->dev, "%s: GET_CONNECTOR_STATUS failed (%d)\n",
313                         __func__, ret);
314                 goto out_unlock;
315         }
316
317         if (con->status.change & UCSI_CONSTAT_POWER_OPMODE_CHANGE)
318                 ucsi_pwr_opmode_change(con);
319
320         if (con->status.change & UCSI_CONSTAT_POWER_DIR_CHANGE) {
321                 typec_set_pwr_role(con->port, con->status.pwr_dir);
322
323                 /* Complete pending power role swap */
324                 if (!completion_done(&con->complete))
325                         complete(&con->complete);
326         }
327
328         if (con->status.change & UCSI_CONSTAT_PARTNER_CHANGE) {
329                 switch (con->status.partner_type) {
330                 case UCSI_CONSTAT_PARTNER_TYPE_UFP:
331                         typec_set_data_role(con->port, TYPEC_HOST);
332                         break;
333                 case UCSI_CONSTAT_PARTNER_TYPE_DFP:
334                         typec_set_data_role(con->port, TYPEC_DEVICE);
335                         break;
336                 default:
337                         break;
338                 }
339
340                 /* Complete pending data role swap */
341                 if (!completion_done(&con->complete))
342                         complete(&con->complete);
343         }
344
345         if (con->status.change & UCSI_CONSTAT_CONNECT_CHANGE) {
346                 if (con->status.connected)
347                         ucsi_register_partner(con);
348                 else
349                         ucsi_unregister_partner(con);
350         }
351
352         ret = ucsi_ack(ucsi, UCSI_ACK_EVENT);
353         if (ret)
354                 dev_err(ucsi->dev, "%s: ACK failed (%d)", __func__, ret);
355
356         trace_ucsi_connector_change(con->num, &con->status);
357
358 out_unlock:
359         clear_bit(EVENT_PENDING, &ucsi->flags);
360         mutex_unlock(&ucsi->ppm_lock);
361 }
362
363 /**
364  * ucsi_notify - PPM notification handler
365  * @ucsi: Source UCSI Interface for the notifications
366  *
367  * Handle notifications from PPM of @ucsi.
368  */
369 void ucsi_notify(struct ucsi *ucsi)
370 {
371         struct ucsi_cci *cci;
372
373         /* There is no requirement to sync here, but no harm either. */
374         ucsi_sync(ucsi);
375
376         cci = &ucsi->ppm->data->cci;
377
378         if (cci->error)
379                 ucsi->status = UCSI_ERROR;
380         else if (cci->busy)
381                 ucsi->status = UCSI_BUSY;
382         else
383                 ucsi->status = UCSI_IDLE;
384
385         if (cci->cmd_complete && test_bit(COMMAND_PENDING, &ucsi->flags)) {
386                 complete(&ucsi->complete);
387         } else if (cci->ack_complete && test_bit(ACK_PENDING, &ucsi->flags)) {
388                 complete(&ucsi->complete);
389         } else if (cci->connector_change) {
390                 struct ucsi_connector *con;
391
392                 con = &ucsi->connector[cci->connector_change - 1];
393
394                 if (!test_and_set_bit(EVENT_PENDING, &ucsi->flags))
395                         schedule_work(&con->work);
396         }
397
398         trace_ucsi_notify(ucsi->ppm->data->raw_cci);
399 }
400 EXPORT_SYMBOL_GPL(ucsi_notify);
401
402 /* -------------------------------------------------------------------------- */
403
404 static int ucsi_reset_connector(struct ucsi_connector *con, bool hard)
405 {
406         struct ucsi_control ctrl;
407
408         UCSI_CMD_CONNECTOR_RESET(ctrl, con, hard);
409
410         return ucsi_run_command(con->ucsi, &ctrl, NULL, 0);
411 }
412
413 static int ucsi_reset_ppm(struct ucsi *ucsi)
414 {
415         struct ucsi_control ctrl;
416         unsigned long tmo;
417         int ret;
418
419         ctrl.raw_cmd = 0;
420         ctrl.cmd.cmd = UCSI_PPM_RESET;
421         trace_ucsi_command(&ctrl);
422         ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
423         if (ret)
424                 goto err;
425
426         tmo = jiffies + msecs_to_jiffies(UCSI_TIMEOUT_MS);
427
428         do {
429                 /* Here sync is critical. */
430                 ret = ucsi_sync(ucsi);
431                 if (ret)
432                         goto err;
433
434                 if (ucsi->ppm->data->cci.reset_complete)
435                         break;
436
437                 /* If the PPM is still doing something else, reset it again. */
438                 if (ucsi->ppm->data->raw_cci) {
439                         dev_warn_ratelimited(ucsi->dev,
440                                 "Failed to reset PPM! Trying again..\n");
441
442                         trace_ucsi_command(&ctrl);
443                         ret = ucsi->ppm->cmd(ucsi->ppm, &ctrl);
444                         if (ret)
445                                 goto err;
446                 }
447
448                 /* Letting the PPM settle down. */
449                 msleep(20);
450
451                 ret = -ETIMEDOUT;
452         } while (time_is_after_jiffies(tmo));
453
454 err:
455         trace_ucsi_reset_ppm(&ctrl, ret);
456
457         return ret;
458 }
459
460 static int ucsi_role_cmd(struct ucsi_connector *con, struct ucsi_control *ctrl)
461 {
462         int ret;
463
464         ret = ucsi_run_command(con->ucsi, ctrl, NULL, 0);
465         if (ret == -ETIMEDOUT) {
466                 struct ucsi_control c;
467
468                 /* PPM most likely stopped responding. Resetting everything. */
469                 ucsi_reset_ppm(con->ucsi);
470
471                 UCSI_CMD_SET_NTFY_ENABLE(c, UCSI_ENABLE_NTFY_ALL);
472                 ucsi_run_command(con->ucsi, &c, NULL, 0);
473
474                 ucsi_reset_connector(con, true);
475         }
476
477         return ret;
478 }
479
480 static int
481 ucsi_dr_swap(const struct typec_capability *cap, enum typec_data_role role)
482 {
483         struct ucsi_connector *con = to_ucsi_connector(cap);
484         struct ucsi_control ctrl;
485         int ret = 0;
486
487         if (!con->partner)
488                 return -ENOTCONN;
489
490         mutex_lock(&con->ucsi->ppm_lock);
491
492         if ((con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_DFP &&
493              role == TYPEC_DEVICE) ||
494             (con->status.partner_type == UCSI_CONSTAT_PARTNER_TYPE_UFP &&
495              role == TYPEC_HOST))
496                 goto out_unlock;
497
498         UCSI_CMD_SET_UOR(ctrl, con, role);
499         ret = ucsi_role_cmd(con, &ctrl);
500         if (ret < 0)
501                 goto out_unlock;
502
503         mutex_unlock(&con->ucsi->ppm_lock);
504
505         if (!wait_for_completion_timeout(&con->complete,
506                                         msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
507                 return -ETIMEDOUT;
508
509         return 0;
510
511 out_unlock:
512         mutex_unlock(&con->ucsi->ppm_lock);
513
514         return ret;
515 }
516
517 static int
518 ucsi_pr_swap(const struct typec_capability *cap, enum typec_role role)
519 {
520         struct ucsi_connector *con = to_ucsi_connector(cap);
521         struct ucsi_control ctrl;
522         int ret = 0;
523
524         if (!con->partner)
525                 return -ENOTCONN;
526
527         mutex_lock(&con->ucsi->ppm_lock);
528
529         if (con->status.pwr_dir == role)
530                 goto out_unlock;
531
532         UCSI_CMD_SET_PDR(ctrl, con, role);
533         ret = ucsi_role_cmd(con, &ctrl);
534         if (ret < 0)
535                 goto out_unlock;
536
537         mutex_unlock(&con->ucsi->ppm_lock);
538
539         if (!wait_for_completion_timeout(&con->complete,
540                                         msecs_to_jiffies(UCSI_SWAP_TIMEOUT_MS)))
541                 return -ETIMEDOUT;
542
543         mutex_lock(&con->ucsi->ppm_lock);
544
545         /* Something has gone wrong while swapping the role */
546         if (con->status.pwr_op_mode != UCSI_CONSTAT_PWR_OPMODE_PD) {
547                 ucsi_reset_connector(con, true);
548                 ret = -EPROTO;
549         }
550
551 out_unlock:
552         mutex_unlock(&con->ucsi->ppm_lock);
553
554         return ret;
555 }
556
557 static struct fwnode_handle *ucsi_find_fwnode(struct ucsi_connector *con)
558 {
559         struct fwnode_handle *fwnode;
560         int i = 1;
561
562         device_for_each_child_node(con->ucsi->dev, fwnode)
563                 if (i++ == con->num)
564                         return fwnode;
565         return NULL;
566 }
567
568 static int ucsi_register_port(struct ucsi *ucsi, int index)
569 {
570         struct ucsi_connector *con = &ucsi->connector[index];
571         struct typec_capability *cap = &con->typec_cap;
572         enum typec_accessory *accessory = cap->accessory;
573         struct ucsi_control ctrl;
574         int ret;
575
576         INIT_WORK(&con->work, ucsi_connector_change);
577         init_completion(&con->complete);
578         con->num = index + 1;
579         con->ucsi = ucsi;
580
581         /* Get connector capability */
582         UCSI_CMD_GET_CONNECTOR_CAPABILITY(ctrl, con->num);
583         ret = ucsi_run_command(ucsi, &ctrl, &con->cap, sizeof(con->cap));
584         if (ret < 0)
585                 return ret;
586
587         if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DRP)
588                 cap->type = TYPEC_PORT_DRP;
589         else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DFP)
590                 cap->type = TYPEC_PORT_DFP;
591         else if (con->cap.op_mode & UCSI_CONCAP_OPMODE_UFP)
592                 cap->type = TYPEC_PORT_UFP;
593
594         cap->revision = ucsi->cap.typec_version;
595         cap->pd_revision = ucsi->cap.pd_version;
596         cap->prefer_role = TYPEC_NO_PREFERRED_ROLE;
597
598         if (con->cap.op_mode & UCSI_CONCAP_OPMODE_AUDIO_ACCESSORY)
599                 *accessory++ = TYPEC_ACCESSORY_AUDIO;
600         if (con->cap.op_mode & UCSI_CONCAP_OPMODE_DEBUG_ACCESSORY)
601                 *accessory = TYPEC_ACCESSORY_DEBUG;
602
603         cap->fwnode = ucsi_find_fwnode(con);
604         cap->dr_set = ucsi_dr_swap;
605         cap->pr_set = ucsi_pr_swap;
606
607         /* Register the connector */
608         con->port = typec_register_port(ucsi->dev, cap);
609         if (!con->port)
610                 return -ENODEV;
611
612         /* Get the status */
613         UCSI_CMD_GET_CONNECTOR_STATUS(ctrl, con->num);
614         ret = ucsi_run_command(ucsi, &ctrl, &con->status, sizeof(con->status));
615         if (ret < 0) {
616                 dev_err(ucsi->dev, "con%d: failed to get status\n", con->num);
617                 return 0;
618         }
619
620         ucsi_pwr_opmode_change(con);
621         typec_set_pwr_role(con->port, con->status.pwr_dir);
622
623         switch (con->status.partner_type) {
624         case UCSI_CONSTAT_PARTNER_TYPE_UFP:
625                 typec_set_data_role(con->port, TYPEC_HOST);
626                 break;
627         case UCSI_CONSTAT_PARTNER_TYPE_DFP:
628                 typec_set_data_role(con->port, TYPEC_DEVICE);
629                 break;
630         default:
631                 break;
632         }
633
634         /* Check if there is already something connected */
635         if (con->status.connected)
636                 ucsi_register_partner(con);
637
638         trace_ucsi_register_port(con->num, &con->status);
639
640         return 0;
641 }
642
643 static void ucsi_init(struct work_struct *work)
644 {
645         struct ucsi *ucsi = container_of(work, struct ucsi, work);
646         struct ucsi_connector *con;
647         struct ucsi_control ctrl;
648         int ret;
649         int i;
650
651         mutex_lock(&ucsi->ppm_lock);
652
653         /* Reset the PPM */
654         ret = ucsi_reset_ppm(ucsi);
655         if (ret) {
656                 dev_err(ucsi->dev, "failed to reset PPM!\n");
657                 goto err;
658         }
659
660         /* Enable basic notifications */
661         UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE |
662                                         UCSI_ENABLE_NTFY_ERROR);
663         ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
664         if (ret < 0)
665                 goto err_reset;
666
667         /* Get PPM capabilities */
668         UCSI_CMD_GET_CAPABILITY(ctrl);
669         ret = ucsi_run_command(ucsi, &ctrl, &ucsi->cap, sizeof(ucsi->cap));
670         if (ret < 0)
671                 goto err_reset;
672
673         if (!ucsi->cap.num_connectors) {
674                 ret = -ENODEV;
675                 goto err_reset;
676         }
677
678         /* Allocate the connectors. Released in ucsi_unregister_ppm() */
679         ucsi->connector = kcalloc(ucsi->cap.num_connectors + 1,
680                                   sizeof(*ucsi->connector), GFP_KERNEL);
681         if (!ucsi->connector) {
682                 ret = -ENOMEM;
683                 goto err_reset;
684         }
685
686         /* Register all connectors */
687         for (i = 0; i < ucsi->cap.num_connectors; i++) {
688                 ret = ucsi_register_port(ucsi, i);
689                 if (ret)
690                         goto err_unregister;
691         }
692
693         /* Enable all notifications */
694         UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_ALL);
695         ret = ucsi_run_command(ucsi, &ctrl, NULL, 0);
696         if (ret < 0)
697                 goto err_unregister;
698
699         mutex_unlock(&ucsi->ppm_lock);
700
701         return;
702
703 err_unregister:
704         for (con = ucsi->connector; con->port; con++) {
705                 ucsi_unregister_partner(con);
706                 typec_unregister_port(con->port);
707                 con->port = NULL;
708         }
709
710 err_reset:
711         ucsi_reset_ppm(ucsi);
712 err:
713         mutex_unlock(&ucsi->ppm_lock);
714         dev_err(ucsi->dev, "PPM init failed (%d)\n", ret);
715 }
716
717 /**
718  * ucsi_register_ppm - Register UCSI PPM Interface
719  * @dev: Device interface to the PPM
720  * @ppm: The PPM interface
721  *
722  * Allocates UCSI instance, associates it with @ppm and returns it to the
723  * caller, and schedules initialization of the interface.
724  */
725 struct ucsi *ucsi_register_ppm(struct device *dev, struct ucsi_ppm *ppm)
726 {
727         struct ucsi *ucsi;
728
729         ucsi = kzalloc(sizeof(*ucsi), GFP_KERNEL);
730         if (!ucsi)
731                 return ERR_PTR(-ENOMEM);
732
733         INIT_WORK(&ucsi->work, ucsi_init);
734         init_completion(&ucsi->complete);
735         mutex_init(&ucsi->ppm_lock);
736
737         ucsi->dev = dev;
738         ucsi->ppm = ppm;
739
740         /*
741          * Communication with the PPM takes a lot of time. It is not reasonable
742          * to initialize the driver here. Using a work for now.
743          */
744         queue_work(system_long_wq, &ucsi->work);
745
746         return ucsi;
747 }
748 EXPORT_SYMBOL_GPL(ucsi_register_ppm);
749
750 /**
751  * ucsi_unregister_ppm - Unregister UCSI PPM Interface
752  * @ucsi: struct ucsi associated with the PPM
753  *
754  * Unregister UCSI PPM that was created with ucsi_register().
755  */
756 void ucsi_unregister_ppm(struct ucsi *ucsi)
757 {
758         struct ucsi_control ctrl;
759         int i;
760
761         /* Make sure that we are not in the middle of driver initialization */
762         cancel_work_sync(&ucsi->work);
763
764         mutex_lock(&ucsi->ppm_lock);
765
766         /* Disable everything except command complete notification */
767         UCSI_CMD_SET_NTFY_ENABLE(ctrl, UCSI_ENABLE_NTFY_CMD_COMPLETE)
768         ucsi_run_command(ucsi, &ctrl, NULL, 0);
769
770         mutex_unlock(&ucsi->ppm_lock);
771
772         for (i = 0; i < ucsi->cap.num_connectors; i++) {
773                 cancel_work_sync(&ucsi->connector[i].work);
774                 ucsi_unregister_partner(&ucsi->connector[i]);
775                 typec_unregister_port(ucsi->connector[i].port);
776         }
777
778         ucsi_reset_ppm(ucsi);
779
780         kfree(ucsi->connector);
781         kfree(ucsi);
782 }
783 EXPORT_SYMBOL_GPL(ucsi_unregister_ppm);
784
785 MODULE_AUTHOR("Heikki Krogerus <[email protected]>");
786 MODULE_LICENSE("GPL v2");
787 MODULE_DESCRIPTION("USB Type-C Connector System Software Interface driver");
This page took 0.078612 seconds and 4 git commands to generate.