root/drivers/platform/chrome/cros_ec_rpmsg.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. cros_ec_cmd_xfer_rpmsg
  2. cros_ec_pkt_xfer_rpmsg
  3. cros_ec_rpmsg_host_event_function
  4. cros_ec_rpmsg_callback
  5. cros_ec_rpmsg_create_ept
  6. cros_ec_rpmsg_probe
  7. cros_ec_rpmsg_remove
  8. cros_ec_rpmsg_suspend
  9. cros_ec_rpmsg_resume

   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         bool has_pending_host_event;
  46         bool probe_done;
  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 wake_event = true;
 149         int ret;
 150 
 151         ret = cros_ec_get_next_event(ec_dev, &wake_event);
 152 
 153         /*
 154          * Signal only if wake host events or any interrupt if
 155          * cros_ec_get_next_event() returned an error (default value for
 156          * wake_event is true)
 157          */
 158         if (wake_event && device_may_wakeup(ec_dev->dev))
 159                 pm_wakeup_event(ec_dev->dev, 0);
 160 
 161         if (ret > 0)
 162                 blocking_notifier_call_chain(&ec_dev->event_notifier,
 163                                              0, ec_dev);
 164 }
 165 
 166 static int cros_ec_rpmsg_callback(struct rpmsg_device *rpdev, void *data,
 167                                   int len, void *priv, u32 src)
 168 {
 169         struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
 170         struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
 171         struct cros_ec_rpmsg_response *resp;
 172 
 173         if (!len) {
 174                 dev_warn(ec_dev->dev, "rpmsg received empty response");
 175                 return -EINVAL;
 176         }
 177 
 178         resp = data;
 179         len -= offsetof(struct cros_ec_rpmsg_response, data);
 180         if (resp->type == HOST_COMMAND_MARK) {
 181                 if (len > ec_dev->din_size) {
 182                         dev_warn(ec_dev->dev,
 183                                  "received length %d > din_size %d, truncating",
 184                                  len, ec_dev->din_size);
 185                         len = ec_dev->din_size;
 186                 }
 187 
 188                 memcpy(ec_dev->din, resp->data, len);
 189                 complete(&ec_rpmsg->xfer_ack);
 190         } else if (resp->type == HOST_EVENT_MARK) {
 191                 /*
 192                  * If the host event is sent before cros_ec_register is
 193                  * finished, queue the host event.
 194                  */
 195                 if (ec_rpmsg->probe_done)
 196                         schedule_work(&ec_rpmsg->host_event_work);
 197                 else
 198                         ec_rpmsg->has_pending_host_event = true;
 199         } else {
 200                 dev_warn(ec_dev->dev, "rpmsg received invalid type = %d",
 201                          resp->type);
 202                 return -EINVAL;
 203         }
 204 
 205         return 0;
 206 }
 207 
 208 static struct rpmsg_endpoint *
 209 cros_ec_rpmsg_create_ept(struct rpmsg_device *rpdev)
 210 {
 211         struct rpmsg_channel_info chinfo = {};
 212 
 213         strscpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
 214         chinfo.src = rpdev->src;
 215         chinfo.dst = RPMSG_ADDR_ANY;
 216 
 217         return rpmsg_create_ept(rpdev, cros_ec_rpmsg_callback, NULL, chinfo);
 218 }
 219 
 220 static int cros_ec_rpmsg_probe(struct rpmsg_device *rpdev)
 221 {
 222         struct device *dev = &rpdev->dev;
 223         struct cros_ec_rpmsg *ec_rpmsg;
 224         struct cros_ec_device *ec_dev;
 225         int ret;
 226 
 227         ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
 228         if (!ec_dev)
 229                 return -ENOMEM;
 230 
 231         ec_rpmsg = devm_kzalloc(dev, sizeof(*ec_rpmsg), GFP_KERNEL);
 232         if (!ec_rpmsg)
 233                 return -ENOMEM;
 234 
 235         ec_dev->dev = dev;
 236         ec_dev->priv = ec_rpmsg;
 237         ec_dev->cmd_xfer = cros_ec_cmd_xfer_rpmsg;
 238         ec_dev->pkt_xfer = cros_ec_pkt_xfer_rpmsg;
 239         ec_dev->phys_name = dev_name(&rpdev->dev);
 240         ec_dev->din_size = sizeof(struct ec_host_response) +
 241                            sizeof(struct ec_response_get_protocol_info);
 242         ec_dev->dout_size = sizeof(struct ec_host_request);
 243         dev_set_drvdata(dev, ec_dev);
 244 
 245         ec_rpmsg->rpdev = rpdev;
 246         init_completion(&ec_rpmsg->xfer_ack);
 247         INIT_WORK(&ec_rpmsg->host_event_work,
 248                   cros_ec_rpmsg_host_event_function);
 249 
 250         ec_rpmsg->ept = cros_ec_rpmsg_create_ept(rpdev);
 251         if (!ec_rpmsg->ept)
 252                 return -ENOMEM;
 253 
 254         ret = cros_ec_register(ec_dev);
 255         if (ret < 0) {
 256                 rpmsg_destroy_ept(ec_rpmsg->ept);
 257                 cancel_work_sync(&ec_rpmsg->host_event_work);
 258                 return ret;
 259         }
 260 
 261         ec_rpmsg->probe_done = true;
 262 
 263         if (ec_rpmsg->has_pending_host_event)
 264                 schedule_work(&ec_rpmsg->host_event_work);
 265 
 266         return 0;
 267 }
 268 
 269 static void cros_ec_rpmsg_remove(struct rpmsg_device *rpdev)
 270 {
 271         struct cros_ec_device *ec_dev = dev_get_drvdata(&rpdev->dev);
 272         struct cros_ec_rpmsg *ec_rpmsg = ec_dev->priv;
 273 
 274         cros_ec_unregister(ec_dev);
 275         rpmsg_destroy_ept(ec_rpmsg->ept);
 276         cancel_work_sync(&ec_rpmsg->host_event_work);
 277 }
 278 
 279 #ifdef CONFIG_PM_SLEEP
 280 static int cros_ec_rpmsg_suspend(struct device *dev)
 281 {
 282         struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
 283 
 284         return cros_ec_suspend(ec_dev);
 285 }
 286 
 287 static int cros_ec_rpmsg_resume(struct device *dev)
 288 {
 289         struct cros_ec_device *ec_dev = dev_get_drvdata(dev);
 290 
 291         return cros_ec_resume(ec_dev);
 292 }
 293 #endif
 294 
 295 static SIMPLE_DEV_PM_OPS(cros_ec_rpmsg_pm_ops, cros_ec_rpmsg_suspend,
 296                          cros_ec_rpmsg_resume);
 297 
 298 static const struct of_device_id cros_ec_rpmsg_of_match[] = {
 299         { .compatible = "google,cros-ec-rpmsg", },
 300         { }
 301 };
 302 MODULE_DEVICE_TABLE(of, cros_ec_rpmsg_of_match);
 303 
 304 static struct rpmsg_driver cros_ec_driver_rpmsg = {
 305         .drv = {
 306                 .name   = "cros-ec-rpmsg",
 307                 .of_match_table = cros_ec_rpmsg_of_match,
 308                 .pm     = &cros_ec_rpmsg_pm_ops,
 309         },
 310         .probe          = cros_ec_rpmsg_probe,
 311         .remove         = cros_ec_rpmsg_remove,
 312 };
 313 
 314 module_rpmsg_driver(cros_ec_driver_rpmsg);
 315 
 316 MODULE_LICENSE("GPL v2");
 317 MODULE_DESCRIPTION("ChromeOS EC multi function device (rpmsg)");

/* [<][>][^][v][top][bottom][index][help] */