]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/platform/chrome/cros_ec_rpmsg.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux.git] / drivers / platform / chrome / cros_ec_rpmsg.c
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright 2018 Google LLC.
4
5 #include <linux/completion.h>
6 #include <linux/delay.h>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/of.h>
10 #include <linux/platform_data/cros_ec_commands.h>
11 #include <linux/platform_data/cros_ec_proto.h>
12 #include <linux/platform_device.h>
13 #include <linux/rpmsg.h>
14 #include <linux/slab.h>
15
16 #include "cros_ec.h"
17
18 #define EC_MSG_TIMEOUT_MS       200
19 #define HOST_COMMAND_MARK       1
20 #define HOST_EVENT_MARK         2
21
22 /**
23  * struct cros_ec_rpmsg_response - rpmsg message format from from EC.
24  *
25  * @type:       The type of message, should be either HOST_COMMAND_MARK or
26  *              HOST_EVENT_MARK, representing that the message is a response to
27  *              host command, or a host event.
28  * @data:       ec_host_response for host command.
29  */
30 struct cros_ec_rpmsg_response {
31         u8 type;
32         u8 data[] __aligned(4);
33 };
34
35 /**
36  * struct cros_ec_rpmsg - information about a EC over rpmsg.
37  *
38  * @rpdev:      rpmsg device we are connected to
39  * @xfer_ack:   completion for host command transfer.
40  * @host_event_work:    Work struct for pending host event.
41  */
42 struct cros_ec_rpmsg {
43         struct rpmsg_device *rpdev;
44         struct completion xfer_ack;
45         struct work_struct host_event_work;
46         struct rpmsg_endpoint *ept;
47 };
48
49 /**
50  * cros_ec_cmd_xfer_rpmsg - Transfer a message over rpmsg and receive the reply
51  *
52  * @ec_dev: ChromeOS EC device
53  * @ec_msg: Message to transfer
54  *
55  * This is only used for old EC proto version, and is not supported for this
56  * driver.
57  *
58  * Return: -EINVAL
59  */
60 static int cros_ec_cmd_xfer_rpmsg(struct cros_ec_device *ec_dev,
61                                   struct cros_ec_command *ec_msg)
62 {
63         return -EINVAL;
64 }
65
66 /**
67  * cros_ec_pkt_xfer_rpmsg - Transfer a packet over rpmsg and receive the reply
68  *
69  * @ec_dev: ChromeOS EC device
70  * @ec_msg: Message to transfer
71  *
72  * Return: number of bytes of the reply on success or negative error code.
73  */
74 static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev,
75                                   struct cros_ec_command *ec_msg)
76 {
77         struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
78         struct ec_host_response *response;
79         unsigned long timeout;
80         int len;
81         int ret;
82         u8 sum;
83         int i;
84
85         ec_msg->result = 0;
86         len = cros_ec_prepare_tx(ec_dev, ec_msg);
87         dev_dbg(ec_dev->dev, "prepared, len=%d\n", len);
88
89         reinit_completion(&ec_rpmsg->xfer_ack);
90         ret = rpmsg_send(ec_rpmsg->ept, ec_dev->dout, len);
91         if (ret) {
92                 dev_err(ec_dev->dev, "rpmsg send failed\n");
93                 return ret;
94         }
95
96         timeout = msecs_to_jiffies(EC_MSG_TIMEOUT_MS);
97         ret = wait_for_completion_timeout(&ec_rpmsg->xfer_ack, timeout);
98         if (!ret) {
99                 dev_err(ec_dev->dev, "rpmsg send timeout\n");
100                 return -EIO;
101         }
102
103         /* check response error code */
104         response = (struct ec_host_response *)ec_dev->din;
105         ec_msg->result = response->result;
106
107         ret = cros_ec_check_result(ec_dev, ec_msg);
108         if (ret)
109                 goto exit;
110
111         if (response->data_len > ec_msg->insize) {
112                 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)",
113                         response->data_len, ec_msg->insize);
114                 ret = -EMSGSIZE;
115                 goto exit;
116         }
117
118         /* copy response packet payload and compute checksum */
119         memcpy(ec_msg->data, ec_dev->din + sizeof(*response),
120                response->data_len);
121
122         sum = 0;
123         for (i = 0; i < sizeof(*response) + response->data_len; i++)
124                 sum += ec_dev->din[i];
125
126         if (sum) {
127                 dev_err(ec_dev->dev, "bad packet checksum, calculated %x\n",
128                         sum);
129                 ret = -EBADMSG;
130                 goto exit;
131         }
132
133         ret = response->data_len;
134 exit:
135         if (ec_msg->command == EC_CMD_REBOOT_EC)
136                 msleep(EC_REBOOT_DELAY_MS);
137
138         return ret;
139 }
140
141 static void
142 cros_ec_rpmsg_host_event_function(struct work_struct *host_event_work)
143 {
144         struct cros_ec_rpmsg *ec_rpmsg = container_of(host_event_work,
145                                                       struct cros_ec_rpmsg,
146                                                       host_event_work);
147         struct cros_ec_device *ec_dev = dev_get_drvdata(&ec_rpmsg->rpdev->dev);
148         bool ec_has_more_events;
149
150         do {
151                 ec_has_more_events = cros_ec_handle_event(ec_dev);
152         } while (ec_has_more_events);
153 }
154
155 static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
156                                   int len, void *priv, u32 src)
157 {
158         struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
159         struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
160         struct cros_ec_rpmsg_response *resp;
161
162         if (!len) {
163                 dev_warn(ec_dev->dev, "rpmsg received empty response");
164                 return -EINVAL;
165         }
166
167         resp = data;
168         len -= offsetof(struct cros_ec_rpmsg_response, data);
169         if (resp->type == HOST_COMMAND_MARK) {
170                 if (len > ec_dev->din_size) {
171                         dev_warn(ec_dev->dev,
172                                  "received length %d > din_size %d, truncating",
173                                  len, ec_dev->din_size);
174                         len = ec_dev->din_size;
175                 }
176
177                 memcpy(ec_dev->din, resp->data, len);
178                 complete(&ec_rpmsg->xfer_ack);
179         } else if (resp->type == HOST_EVENT_MARK) {
180                 schedule_work(&ec_rpmsg->host_event_work);
181         } else {
182                 dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
183                          resp->type);
184                 return -EINVAL;
185         }
186
187         return 0;
188 }
189
190 static struct rpmsg_endpoint *
191 cros_ec_rpmsg_create_ept(struct rpmsg_device *rpdev)
192 {
193         struct rpmsg_channel_info chinfo = {};
194
195         strscpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
196         chinfo.src = rpdev->src;
197         chinfo.dst = RPMSG_ADDR_ANY;
198
199         return rpmsg_create_ept(rpdev, cros_ec_rpmsg_callback, NULL, chinfo);
200 }
201
202 static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
203 {
204         struct device *dev = &rpdev->dev;
205         struct cros_ec_rpmsg *ec_rpmsg;
206         struct cros_ec_device *ec_dev;
207         int ret;
208
209         ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
210         if (!ec_dev)
211                 return -ENOMEM;
212
213         ec_rpmsg = devm_kzalloc(dev, sizeof(*ec_rpmsg), GFP_KERNEL);
214         if (!ec_rpmsg)
215                 return -ENOMEM;
216
217         ec_dev->dev = dev;
218         ec_dev->priv = ec_rpmsg;
219         ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg;
220         ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg;
221         ec_dev->phys_name = dev_name(&rpdev->dev);
222         ec_dev->din_size = sizeof(struct ec_host_response) +
223                            sizeof(struct ec_response_get_protocol_info);
224         ec_dev->dout_size = sizeof(struct ec_host_request);
225         dev_set_drvdata(dev, ec_dev);
226
227         ec_rpmsg->rpdev = rpdev;
228         init_completion(&ec_rpmsg->xfer_ack);
229         INIT_WORK(&ec_rpmsg->host_event_work,
230                   cros_ec_rpmsg_host_event_function);
231
232         ec_rpmsg->ept = cros_ec_rpmsg_create_ept(rpdev);
233         if (!ec_rpmsg->ept)
234                 return -ENOMEM;
235
236         ret = cros_ec_register(ec_dev);
237         if (ret < 0) {
238                 rpmsg_destroy_ept(ec_rpmsg->ept);
239                 cancel_work_sync(&ec_rpmsg->host_event_work);
240                 return ret;
241         }
242
243         return 0;
244 }
245
246 static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev)
247 {
248         struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
249         struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
250
251         cros_ec_unregister(ec_dev);
252         rpmsg_destroy_ept(ec_rpmsg->ept);
253         cancel_work_sync(&ec_rpmsg->host_event_work);
254 }
255
256 #ifdef CONFIG_PM_SLEEP
257 static int cros_ec_rpmsg_suspend(struct device *dev)
258 {
259         struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
260
261         return cros_ec_suspend(ec_dev);
262 }
263
264 static int cros_ec_rpmsg_resume(struct device *dev)
265 {
266         struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
267
268         return cros_ec_resume(ec_dev);
269 }
270 #endif
271
272 static SIMPLE_DEV_PM_OPS(cros_ec_rpmsg_pm_ops, cros_ec_rpmsg_suspend,
273                          cros_ec_rpmsg_resume);
274
275 static const struct of_device_id cros_ec_rpmsg_of_match[] = {
276         { .compatible = "google,cros-ec-rpmsg", },
277         { }
278 };
279 MODULE_DEVICE_TABLE(of, cros_ec_rpmsg_of_match);
280
281 static struct rpmsg_driver cros_ec_driver_rpmsg = {
282         .drv = {
283                 .name   = "cros-ec-rpmsg",
284                 .of_match_table = cros_ec_rpmsg_of_match,
285                 .pm     = &cros_ec_rpmsg_pm_ops,
286         },
287         .probe          = cros_ec_rpmsg_probe,
288         .remove         = cros_ec_rpmsg_remove,
289 };
290
291 module_rpmsg_driver(cros_ec_driver_rpmsg);
292
293 MODULE_LICENSE("GPL v2");
294 MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)");