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