cros_ec_vbc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 = to_cros_ec_dev(dev);
  31. struct cros_ec_device *ecdev = ec->ec_dev;
  32. struct ec_params_vbnvcontext *params;
  33. struct cros_ec_command *msg;
  34. int err;
  35. const size_t para_sz = sizeof(params->op);
  36. const size_t resp_sz = sizeof(struct ec_response_vbnvcontext);
  37. const size_t payload = max(para_sz, resp_sz);
  38. msg = kmalloc(sizeof(*msg) + payload, GFP_KERNEL);
  39. if (!msg)
  40. return -ENOMEM;
  41. /* NB: we only kmalloc()ated enough space for the op field */
  42. params = (struct ec_params_vbnvcontext *)msg->data;
  43. params->op = EC_VBNV_CONTEXT_OP_READ;
  44. msg->version = EC_VER_VBNV_CONTEXT;
  45. msg->command = EC_CMD_VBNV_CONTEXT;
  46. msg->outsize = para_sz;
  47. msg->insize = resp_sz;
  48. err = cros_ec_cmd_xfer(ecdev, msg);
  49. if (err < 0) {
  50. dev_err(dev, "Error sending read request: %d\n", err);
  51. kfree(msg);
  52. return err;
  53. }
  54. memcpy(buf, msg->data, resp_sz);
  55. kfree(msg);
  56. return resp_sz;
  57. }
  58. static ssize_t vboot_context_write(struct file *filp, struct kobject *kobj,
  59. struct bin_attribute *attr, char *buf,
  60. loff_t pos, size_t count)
  61. {
  62. struct device *dev = container_of(kobj, struct device, kobj);
  63. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  64. struct cros_ec_device *ecdev = ec->ec_dev;
  65. struct ec_params_vbnvcontext *params;
  66. struct cros_ec_command *msg;
  67. int err;
  68. const size_t para_sz = sizeof(*params);
  69. const size_t data_sz = sizeof(params->block);
  70. /* Only write full values */
  71. if (count != data_sz)
  72. return -EINVAL;
  73. msg = kmalloc(sizeof(*msg) + para_sz, GFP_KERNEL);
  74. if (!msg)
  75. return -ENOMEM;
  76. params = (struct ec_params_vbnvcontext *)msg->data;
  77. params->op = EC_VBNV_CONTEXT_OP_WRITE;
  78. memcpy(params->block, buf, data_sz);
  79. msg->version = EC_VER_VBNV_CONTEXT;
  80. msg->command = EC_CMD_VBNV_CONTEXT;
  81. msg->outsize = para_sz;
  82. msg->insize = 0;
  83. err = cros_ec_cmd_xfer(ecdev, msg);
  84. if (err < 0) {
  85. dev_err(dev, "Error sending write request: %d\n", err);
  86. kfree(msg);
  87. return err;
  88. }
  89. kfree(msg);
  90. return data_sz;
  91. }
  92. static umode_t cros_ec_vbc_is_visible(struct kobject *kobj,
  93. struct bin_attribute *a, int n)
  94. {
  95. struct device *dev = container_of(kobj, struct device, kobj);
  96. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  97. struct device_node *np = ec->ec_dev->dev->of_node;
  98. if (IS_ENABLED(CONFIG_OF) && np) {
  99. if (of_property_read_bool(np, "google,has-vbc-nvram"))
  100. return a->attr.mode;
  101. }
  102. return 0;
  103. }
  104. static BIN_ATTR_RW(vboot_context, 16);
  105. static struct bin_attribute *cros_ec_vbc_bin_attrs[] = {
  106. &bin_attr_vboot_context,
  107. NULL
  108. };
  109. struct attribute_group cros_ec_vbc_attr_group = {
  110. .name = "vbc",
  111. .bin_attrs = cros_ec_vbc_bin_attrs,
  112. .is_bin_visible = cros_ec_vbc_is_visible,
  113. };
  114. EXPORT_SYMBOL(cros_ec_vbc_attr_group);