cros_ec_vbc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * cros_ec_vbc - Expose the vboot context nvram to userspace
  3. *
  4. * Copyright (C) 2015 Collabora Ltd.
  5. *
  6. * based on vendor driver,
  7. *
  8. * Copyright (C) 2012 The Chromium OS Authors
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/of.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/mfd/cros_ec.h>
  23. #include <linux/mfd/cros_ec_commands.h>
  24. #include <linux/slab.h>
  25. static ssize_t vboot_context_read(struct file *filp, struct kobject *kobj,
  26. struct bin_attribute *att, char *buf,
  27. loff_t pos, size_t count)
  28. {
  29. struct device *dev = container_of(kobj, struct device, kobj);
  30. struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
  31. class_dev);
  32. struct cros_ec_device *ecdev = ec->ec_dev;
  33. struct ec_params_vbnvcontext *params;
  34. struct cros_ec_command *msg;
  35. int err;
  36. const size_t para_sz = sizeof(params->op);
  37. const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
  38. const size_t payload = max(para_sz, resp_sz);
  39. msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL);
  40. if (!msg)
  41. return -ENOMEM;
  42. /* NB: we only kmalloc()ated enough space for the op field */
  43. params = (struct ec_params_vbnvcontext *)msg->data;
  44. params->op = EC_VBNV_CONTEXT_OP_READ;
  45. msg->version = EC_VER_VBNV_CONTEXT;
  46. msg->command = EC_CMD_VBNV_CONTEXT;
  47. msg->outsize = para_sz;
  48. msg->insize = resp_sz;
  49. err = cros_ec_cmd_xfer(ecdev, msg);
  50. if (err < 0) {
  51. dev_err(dev, "Error sending read request: %d\n", err);
  52. kfree(msg);
  53. return err;
  54. }
  55. memcpy(buf, msg->data, resp_sz);
  56. kfree(msg);
  57. return resp_sz;
  58. }
  59. static ssize_t vboot_context_write(struct file *filp, struct kobject *kobj,
  60. struct bin_attribute *attr, char *buf,
  61. loff_t pos, size_t count)
  62. {
  63. struct device *dev = container_of(kobj, struct device, kobj);
  64. struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
  65. class_dev);
  66. struct cros_ec_device *ecdev = ec->ec_dev;
  67. struct ec_params_vbnvcontext *params;
  68. struct cros_ec_command *msg;
  69. int err;
  70. const size_t para_sz = sizeof(*params);
  71. const size_t data_sz = sizeof(params->block);
  72. /* Only write full values */
  73. if (count != data_sz)
  74. return -EINVAL;
  75. msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL);
  76. if (!msg)
  77. return -ENOMEM;
  78. params = (struct ec_params_vbnvcontext *)msg->data;
  79. params->op = EC_VBNV_CONTEXT_OP_WRITE;
  80. memcpy(params->block, buf, data_sz);
  81. msg->version = EC_VER_VBNV_CONTEXT;
  82. msg->command = EC_CMD_VBNV_CONTEXT;
  83. msg->outsize = para_sz;
  84. msg->insize = 0;
  85. err = cros_ec_cmd_xfer(ecdev, msg);
  86. if (err < 0) {
  87. dev_err(dev, "Error sending write request: %d\n", err);
  88. kfree(msg);
  89. return err;
  90. }
  91. kfree(msg);
  92. return data_sz;
  93. }
  94. static umode_t cros_ec_vbc_is_visible(struct kobject *kobj,
  95. struct bin_attribute *a, int n)
  96. {
  97. struct device *dev = container_of(kobj, struct device, kobj);
  98. struct cros_ec_dev *ec = container_of(dev, struct cros_ec_dev,
  99. class_dev);
  100. struct device_node *np = ec->ec_dev->dev->of_node;
  101. if (IS_ENABLED(CONFIG_OF) && np) {
  102. if (of_property_read_bool(np, "google,has-vbc-nvram"))
  103. return a->attr.mode;
  104. }
  105. return 0;
  106. }
  107. static BIN_ATTR_RW(vboot_context, 16);
  108. static struct bin_attribute *cros_ec_vbc_bin_attrs[] = {
  109. &bin_attr_vboot_context,
  110. NULL
  111. };
  112. struct attribute_group cros_ec_vbc_attr_group = {
  113. .name = "vbc",
  114. .bin_attrs = cros_ec_vbc_bin_attrs,
  115. .is_bin_visible = cros_ec_vbc_is_visible,
  116. };