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 bool has_pending_host_event; 48 bool probe_done; 49 }; 50 51 /** 52 * cros_ec_cmd_xfer_rpmsg - Transfer a message over rpmsg and receive the reply 53 * 54 * @ec_dev: ChromeOS EC device 55 * @ec_msg: Message to transfer 56 * 57 * This is only used for old EC proto version, and is not supported for this 58 * driver. 59 * 60 * Return: -EINVAL 61 */ 62 static int cros_ec_cmd_xfer_rpmsg(struct cros_ec_device *ec_dev, 63 struct cros_ec_command *ec_msg) 64 { 65 return -EINVAL; 66 } 67 68 /** 69 * cros_ec_pkt_xfer_rpmsg - Transfer a packet over rpmsg and receive the reply 70 * 71 * @ec_dev: ChromeOS EC device 72 * @ec_msg: Message to transfer 73 * 74 * Return: number of bytes of the reply on success or negative error code. 75 */ 76 static int cros_ec_pkt_xfer_rpmsg(struct cros_ec_device *ec_dev, 77 struct cros_ec_command *ec_msg) 78 { 79 struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv; 80 struct ec_host_response *response; 81 unsigned long timeout; 82 int len; 83 int ret; 84 u8 sum; 85 int i; 86 87 ec_msg->result = 0; 88 len = cros_ec_prepare_tx(ec_dev, ec_msg); 89 dev_dbg(ec_dev->dev, "prepared, len=%d\n", len); 90 91 reinit_completion(&ec_rpmsg->xfer_ack); 92 ret = rpmsg_send(ec_rpmsg->ept, ec_dev->dout, len); 93 if (ret) { 94 dev_err(ec_dev->dev, "rpmsg send failed\n"); 95 return ret; 96 } 97 98 timeout = msecs_to_jiffies(EC_MSG_TIMEOUT_MS); 99 ret = wait_for_completion_timeout(&ec_rpmsg->xfer_ack, timeout); 100 if (!ret) { 101 dev_err(ec_dev->dev, "rpmsg send timeout\n"); 102 return -EIO; 103 } 104 105 /* check response error code */ 106 response = (struct ec_host_response *)ec_dev->din; 107 ec_msg->result = response->result; 108 109 ret = cros_ec_check_result(ec_dev, ec_msg); 110 if (ret) 111 goto exit; 112 113 if (response->data_len > ec_msg->insize) { 114 dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", 115 response->data_len, ec_msg->insize); 116 ret = -EMSGSIZE; 117 goto exit; 118 } 119 120 /* copy response packet payload and compute checksum */ 121 memcpy(ec_msg->data, ec_dev->din + sizeof(*response), 122 response->data_len); 123 124 sum = 0; 125 for (i = 0; i < sizeof(*response) + response->data_len; i++) 126 sum += ec_dev->din[i]; 127 128 if (sum) { 129 dev_err(ec_dev->dev, "bad packet checksum, calculated %x\n", 130 sum); 131 ret = -EBADMSG; 132 goto exit; 133 } 134 135 ret = response->data_len; 136 exit: 137 if (ec_msg->command == EC_CMD_REBOOT_EC) 138 msleep(EC_REBOOT_DELAY_MS); 139 140 return ret; 141 } 142 143 static void 144 cros_ec_rpmsg_host_event_function(struct work_struct *host_event_work) 145 { 146 struct cros_ec_rpmsg *ec_rpmsg = container_of(host_event_work, 147 struct cros_ec_rpmsg, 148 host_event_work); 149 struct cros_ec_device *ec_dev = dev_get_drvdata(&ec_rpmsg->rpdev->dev); 150 bool ec_has_more_events; 151 152 do { 153 ec_has_more_events = cros_ec_handle_event(ec_dev); 154 } while (ec_has_more_events); 155 } 156 157 static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data, 158 int len, void *priv, u32 src) 159 { 160 struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev); 161 struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv; 162 struct cros_ec_rpmsg_response *resp; 163 164 if (!len) { 165 dev_warn(ec_dev->dev, "rpmsg received empty response"); 166 return -EINVAL; 167 } 168 169 resp = data; 170 len -= offsetof(struct cros_ec_rpmsg_response, data); 171 if (resp->type == HOST_COMMAND_MARK) { 172 if (len > ec_dev->din_size) { 173 dev_warn(ec_dev->dev, 174 "received length %d > din_size %d, truncating", 175 len, ec_dev->din_size); 176 len = ec_dev->din_size; 177 } 178 179 memcpy(ec_dev->din, resp->data, len); 180 complete(&ec_rpmsg->xfer_ack); 181 } else if (resp->type == HOST_EVENT_MARK) { 182 /* 183 * If the host event is sent before cros_ec_register is 184 * finished, queue the host event. 185 */ 186 if (ec_rpmsg->probe_done) 187 schedule_work(&ec_rpmsg->host_event_work); 188 else 189 ec_rpmsg->has_pending_host_event = true; 190 } else { 191 dev_warn(ec_dev->dev, "rpmsg received invalid type = %d", 192 resp->type); 193 return -EINVAL; 194 } 195 196 return 0; 197 } 198 199 static struct rpmsg_endpoint * 200 cros_ec_rpmsg_create_ept(struct rpmsg_device *rpdev) 201 { 202 struct rpmsg_channel_info chinfo = {}; 203 204 strscpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE); 205 chinfo.src = rpdev->src; 206 chinfo.dst = RPMSG_ADDR_ANY; 207 208 return rpmsg_create_ept(rpdev, cros_ec_rpmsg_callback, NULL, chinfo); 209 } 210 211 static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev) 212 { 213 struct device *dev = &rpdev->dev; 214 struct cros_ec_rpmsg *ec_rpmsg; 215 struct cros_ec_device *ec_dev; 216 int ret; 217 218 ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); 219 if (!ec_dev) 220 return -ENOMEM; 221 222 ec_rpmsg = devm_kzalloc(dev, sizeof(*ec_rpmsg), GFP_KERNEL); 223 if (!ec_rpmsg) 224 return -ENOMEM; 225 226 ec_dev->dev = dev; 227 ec_dev->priv = ec_rpmsg; 228 ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg; 229 ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg; 230 ec_dev->phys_name = dev_name(&rpdev->dev); 231 ec_dev->din_size = sizeof(struct ec_host_response) + 232 sizeof(struct ec_response_get_protocol_info); 233 ec_dev->dout_size = sizeof(struct ec_host_request); 234 dev_set_drvdata(dev, ec_dev); 235 236 ec_rpmsg->rpdev = rpdev; 237 init_completion(&ec_rpmsg->xfer_ack); 238 INIT_WORK(&ec_rpmsg->host_event_work, 239 cros_ec_rpmsg_host_event_function); 240 241 ec_rpmsg->ept = cros_ec_rpmsg_create_ept(rpdev); 242 if (!ec_rpmsg->ept) 243 return -ENOMEM; 244 245 ret = cros_ec_register(ec_dev); 246 if (ret < 0) { 247 rpmsg_destroy_ept(ec_rpmsg->ept); 248 cancel_work_sync(&ec_rpmsg->host_event_work); 249 return ret; 250 } 251 252 ec_rpmsg->probe_done = true; 253 254 if (ec_rpmsg->has_pending_host_event) 255 schedule_work(&ec_rpmsg->host_event_work); 256 257 return 0; 258 } 259 260 static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev) 261 { 262 struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev); 263 struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv; 264 265 cros_ec_unregister(ec_dev); 266 rpmsg_destroy_ept(ec_rpmsg->ept); 267 cancel_work_sync(&ec_rpmsg->host_event_work); 268 } 269 270 #ifdef CONFIG_PM_SLEEP 271 static int cros_ec_rpmsg_suspend(struct device *dev) 272 { 273 struct cros_ec_device *ec_dev = dev_get_drvdata(dev); 274 275 return cros_ec_suspend(ec_dev); 276 } 277 278 static int cros_ec_rpmsg_resume(struct device *dev) 279 { 280 struct cros_ec_device *ec_dev = dev_get_drvdata(dev); 281 282 return cros_ec_resume(ec_dev); 283 } 284 #endif 285 286 static SIMPLE_DEV_PM_OPS(cros_ec_rpmsg_pm_ops, cros_ec_rpmsg_suspend, 287 cros_ec_rpmsg_resume); 288 289 static const struct of_device_id cros_ec_rpmsg_of_match[] = { 290 { .compatible = "google,cros-ec-rpmsg", }, 291 { } 292 }; 293 MODULE_DEVICE_TABLE(of, cros_ec_rpmsg_of_match); 294 295 static struct rpmsg_driver cros_ec_driver_rpmsg = { 296 .drv = { 297 .name = "cros-ec-rpmsg", 298 .of_match_table = cros_ec_rpmsg_of_match, 299 .pm = &cros_ec_rpmsg_pm_ops, 300 }, 301 .probe = cros_ec_rpmsg_probe, 302 .remove = cros_ec_rpmsg_remove, 303 }; 304 305 module_rpmsg_driver(cros_ec_driver_rpmsg); 306 307 MODULE_LICENSE("GPL v2"); 308 MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)"); 309