cros_ec_sysfs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * cros_ec_sysfs - expose the Chrome OS EC through sysfs
  3. *
  4. * Copyright (C) 2014 Google, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define pr_fmt(fmt) "cros_ec_sysfs: " fmt
  20. #include <linux/ctype.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/fs.h>
  24. #include <linux/kobject.h>
  25. #include <linux/mfd/cros_ec.h>
  26. #include <linux/mfd/cros_ec_commands.h>
  27. #include <linux/module.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/printk.h>
  30. #include <linux/stat.h>
  31. #include <linux/types.h>
  32. #include <linux/uaccess.h>
  33. #include "cros_ec_dev.h"
  34. /* Accessor functions */
  35. static ssize_t show_ec_reboot(struct device *dev,
  36. struct device_attribute *attr, char *buf)
  37. {
  38. int count = 0;
  39. count += scnprintf(buf + count, PAGE_SIZE - count,
  40. "ro|rw|cancel|cold|disable-jump|hibernate");
  41. count += scnprintf(buf + count, PAGE_SIZE - count,
  42. " [at-shutdown]\n");
  43. return count;
  44. }
  45. static ssize_t store_ec_reboot(struct device *dev,
  46. struct device_attribute *attr,
  47. const char *buf, size_t count)
  48. {
  49. static const struct {
  50. const char * const str;
  51. uint8_t cmd;
  52. uint8_t flags;
  53. } words[] = {
  54. {"cancel", EC_REBOOT_CANCEL, 0},
  55. {"ro", EC_REBOOT_JUMP_RO, 0},
  56. {"rw", EC_REBOOT_JUMP_RW, 0},
  57. {"cold", EC_REBOOT_COLD, 0},
  58. {"disable-jump", EC_REBOOT_DISABLE_JUMP, 0},
  59. {"hibernate", EC_REBOOT_HIBERNATE, 0},
  60. {"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
  61. };
  62. struct cros_ec_command msg = { 0 };
  63. struct ec_params_reboot_ec *param =
  64. (struct ec_params_reboot_ec *)msg.outdata;
  65. int got_cmd = 0, offset = 0;
  66. int i;
  67. int ret;
  68. struct cros_ec_device *ec = dev_get_drvdata(dev);
  69. param->flags = 0;
  70. while (1) {
  71. /* Find word to start scanning */
  72. while (buf[offset] && isspace(buf[offset]))
  73. offset++;
  74. if (!buf[offset])
  75. break;
  76. for (i = 0; i < ARRAY_SIZE(words); i++) {
  77. if (!strncasecmp(words[i].str, buf+offset,
  78. strlen(words[i].str))) {
  79. if (words[i].flags) {
  80. param->flags |= words[i].flags;
  81. } else {
  82. param->cmd = words[i].cmd;
  83. got_cmd = 1;
  84. }
  85. break;
  86. }
  87. }
  88. /* On to the next word, if any */
  89. while (buf[offset] && !isspace(buf[offset]))
  90. offset++;
  91. }
  92. if (!got_cmd)
  93. return -EINVAL;
  94. msg.command = EC_CMD_REBOOT_EC;
  95. msg.outsize = sizeof(param);
  96. ret = cros_ec_cmd_xfer(ec, &msg);
  97. if (ret < 0)
  98. return ret;
  99. if (msg.result != EC_RES_SUCCESS) {
  100. dev_dbg(ec->dev, "EC result %d\n", msg.result);
  101. return -EINVAL;
  102. }
  103. return count;
  104. }
  105. static ssize_t show_ec_version(struct device *dev,
  106. struct device_attribute *attr, char *buf)
  107. {
  108. static const char * const image_names[] = {"unknown", "RO", "RW"};
  109. struct ec_response_get_version *r_ver;
  110. struct ec_response_get_chip_info *r_chip;
  111. struct ec_response_board_version *r_board;
  112. struct cros_ec_command msg = { 0 };
  113. int ret;
  114. int count = 0;
  115. struct cros_ec_device *ec = dev_get_drvdata(dev);
  116. /* Get versions. RW may change. */
  117. msg.command = EC_CMD_GET_VERSION;
  118. msg.insize = sizeof(*r_ver);
  119. ret = cros_ec_cmd_xfer(ec, &msg);
  120. if (ret < 0)
  121. return ret;
  122. if (msg.result != EC_RES_SUCCESS)
  123. return scnprintf(buf, PAGE_SIZE,
  124. "ERROR: EC returned %d\n", msg.result);
  125. r_ver = (struct ec_response_get_version *)msg.indata;
  126. /* Strings should be null-terminated, but let's be sure. */
  127. r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
  128. r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
  129. count += scnprintf(buf + count, PAGE_SIZE - count,
  130. "RO version: %s\n", r_ver->version_string_ro);
  131. count += scnprintf(buf + count, PAGE_SIZE - count,
  132. "RW version: %s\n", r_ver->version_string_rw);
  133. count += scnprintf(buf + count, PAGE_SIZE - count,
  134. "Firmware copy: %s\n",
  135. (r_ver->current_image < ARRAY_SIZE(image_names) ?
  136. image_names[r_ver->current_image] : "?"));
  137. /* Get build info. */
  138. msg.command = EC_CMD_GET_BUILD_INFO;
  139. msg.insize = sizeof(msg.indata);
  140. ret = cros_ec_cmd_xfer(ec, &msg);
  141. if (ret < 0)
  142. count += scnprintf(buf + count, PAGE_SIZE - count,
  143. "Build info: XFER ERROR %d\n", ret);
  144. else if (msg.result != EC_RES_SUCCESS)
  145. count += scnprintf(buf + count, PAGE_SIZE - count,
  146. "Build info: EC error %d\n", msg.result);
  147. else {
  148. msg.indata[sizeof(msg.indata) - 1] = '\0';
  149. count += scnprintf(buf + count, PAGE_SIZE - count,
  150. "Build info: %s\n", msg.indata);
  151. }
  152. /* Get chip info. */
  153. msg.command = EC_CMD_GET_CHIP_INFO;
  154. msg.insize = sizeof(*r_chip);
  155. ret = cros_ec_cmd_xfer(ec, &msg);
  156. if (ret < 0)
  157. count += scnprintf(buf + count, PAGE_SIZE - count,
  158. "Chip info: XFER ERROR %d\n", ret);
  159. else if (msg.result != EC_RES_SUCCESS)
  160. count += scnprintf(buf + count, PAGE_SIZE - count,
  161. "Chip info: EC error %d\n", msg.result);
  162. else {
  163. r_chip = (struct ec_response_get_chip_info *)msg.indata;
  164. r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
  165. r_chip->name[sizeof(r_chip->name) - 1] = '\0';
  166. r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
  167. count += scnprintf(buf + count, PAGE_SIZE - count,
  168. "Chip vendor: %s\n", r_chip->vendor);
  169. count += scnprintf(buf + count, PAGE_SIZE - count,
  170. "Chip name: %s\n", r_chip->name);
  171. count += scnprintf(buf + count, PAGE_SIZE - count,
  172. "Chip revision: %s\n", r_chip->revision);
  173. }
  174. /* Get board version */
  175. msg.command = EC_CMD_GET_BOARD_VERSION;
  176. msg.insize = sizeof(*r_board);
  177. ret = cros_ec_cmd_xfer(ec, &msg);
  178. if (ret < 0)
  179. count += scnprintf(buf + count, PAGE_SIZE - count,
  180. "Board version: XFER ERROR %d\n", ret);
  181. else if (msg.result != EC_RES_SUCCESS)
  182. count += scnprintf(buf + count, PAGE_SIZE - count,
  183. "Board version: EC error %d\n", msg.result);
  184. else {
  185. r_board = (struct ec_response_board_version *)msg.indata;
  186. count += scnprintf(buf + count, PAGE_SIZE - count,
  187. "Board version: %d\n",
  188. r_board->board_version);
  189. }
  190. return count;
  191. }
  192. static ssize_t show_ec_flashinfo(struct device *dev,
  193. struct device_attribute *attr, char *buf)
  194. {
  195. struct ec_response_flash_info *resp;
  196. struct cros_ec_command msg = { 0 };
  197. int ret;
  198. struct cros_ec_device *ec = dev_get_drvdata(dev);
  199. /* The flash info shouldn't ever change, but ask each time anyway. */
  200. msg.command = EC_CMD_FLASH_INFO;
  201. msg.insize = sizeof(*resp);
  202. ret = cros_ec_cmd_xfer(ec, &msg);
  203. if (ret < 0)
  204. return ret;
  205. if (msg.result != EC_RES_SUCCESS)
  206. return scnprintf(buf, PAGE_SIZE,
  207. "ERROR: EC returned %d\n", msg.result);
  208. resp = (struct ec_response_flash_info *)msg.indata;
  209. return scnprintf(buf, PAGE_SIZE,
  210. "FlashSize %d\nWriteSize %d\n"
  211. "EraseSize %d\nProtectSize %d\n",
  212. resp->flash_size, resp->write_block_size,
  213. resp->erase_block_size, resp->protect_block_size);
  214. }
  215. /* Module initialization */
  216. static DEVICE_ATTR(reboot, S_IWUSR | S_IRUGO, show_ec_reboot, store_ec_reboot);
  217. static DEVICE_ATTR(version, S_IRUGO, show_ec_version, NULL);
  218. static DEVICE_ATTR(flashinfo, S_IRUGO, show_ec_flashinfo, NULL);
  219. static struct attribute *__ec_attrs[] = {
  220. &dev_attr_reboot.attr,
  221. &dev_attr_version.attr,
  222. &dev_attr_flashinfo.attr,
  223. NULL,
  224. };
  225. static struct attribute_group ec_attr_group = {
  226. .attrs = __ec_attrs,
  227. };
  228. void ec_dev_sysfs_init(struct cros_ec_device *ec)
  229. {
  230. int error;
  231. error = sysfs_create_group(&ec->vdev->kobj, &ec_attr_group);
  232. if (error)
  233. pr_warn("failed to create group: %d\n", error);
  234. }
  235. void ec_dev_sysfs_remove(struct cros_ec_device *ec)
  236. {
  237. sysfs_remove_group(&ec->vdev->kobj, &ec_attr_group);
  238. }