cros_ec_sysfs.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/slab.h>
  31. #include <linux/stat.h>
  32. #include <linux/types.h>
  33. #include <linux/uaccess.h>
  34. #include "cros_ec_dev.h"
  35. /* Accessor functions */
  36. static ssize_t show_ec_reboot(struct device *dev,
  37. struct device_attribute *attr, char *buf)
  38. {
  39. int count = 0;
  40. count += scnprintf(buf + count, PAGE_SIZE - count,
  41. "ro|rw|cancel|cold|disable-jump|hibernate");
  42. count += scnprintf(buf + count, PAGE_SIZE - count,
  43. " [at-shutdown]\n");
  44. return count;
  45. }
  46. static ssize_t store_ec_reboot(struct device *dev,
  47. struct device_attribute *attr,
  48. const char *buf, size_t count)
  49. {
  50. static const struct {
  51. const char * const str;
  52. uint8_t cmd;
  53. uint8_t flags;
  54. } words[] = {
  55. {"cancel", EC_REBOOT_CANCEL, 0},
  56. {"ro", EC_REBOOT_JUMP_RO, 0},
  57. {"rw", EC_REBOOT_JUMP_RW, 0},
  58. {"cold", EC_REBOOT_COLD, 0},
  59. {"disable-jump", EC_REBOOT_DISABLE_JUMP, 0},
  60. {"hibernate", EC_REBOOT_HIBERNATE, 0},
  61. {"at-shutdown", -1, EC_REBOOT_FLAG_ON_AP_SHUTDOWN},
  62. };
  63. struct cros_ec_command *msg;
  64. struct ec_params_reboot_ec *param;
  65. int got_cmd = 0, offset = 0;
  66. int i;
  67. int ret;
  68. struct cros_ec_dev *ec = container_of(dev,
  69. struct cros_ec_dev, class_dev);
  70. msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
  71. if (!msg)
  72. return -ENOMEM;
  73. param = (struct ec_params_reboot_ec *)msg->data;
  74. param->flags = 0;
  75. while (1) {
  76. /* Find word to start scanning */
  77. while (buf[offset] && isspace(buf[offset]))
  78. offset++;
  79. if (!buf[offset])
  80. break;
  81. for (i = 0; i < ARRAY_SIZE(words); i++) {
  82. if (!strncasecmp(words[i].str, buf+offset,
  83. strlen(words[i].str))) {
  84. if (words[i].flags) {
  85. param->flags |= words[i].flags;
  86. } else {
  87. param->cmd = words[i].cmd;
  88. got_cmd = 1;
  89. }
  90. break;
  91. }
  92. }
  93. /* On to the next word, if any */
  94. while (buf[offset] && !isspace(buf[offset]))
  95. offset++;
  96. }
  97. if (!got_cmd) {
  98. count = -EINVAL;
  99. goto exit;
  100. }
  101. msg->version = 0;
  102. msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset;
  103. msg->outsize = sizeof(*param);
  104. msg->insize = 0;
  105. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  106. if (ret < 0) {
  107. count = ret;
  108. goto exit;
  109. }
  110. if (msg->result != EC_RES_SUCCESS) {
  111. dev_dbg(ec->dev, "EC result %d\n", msg->result);
  112. count = -EINVAL;
  113. }
  114. exit:
  115. kfree(msg);
  116. return count;
  117. }
  118. static ssize_t show_ec_version(struct device *dev,
  119. struct device_attribute *attr, char *buf)
  120. {
  121. static const char * const image_names[] = {"unknown", "RO", "RW"};
  122. struct ec_response_get_version *r_ver;
  123. struct ec_response_get_chip_info *r_chip;
  124. struct ec_response_board_version *r_board;
  125. struct cros_ec_command *msg;
  126. int ret;
  127. int count = 0;
  128. struct cros_ec_dev *ec = container_of(dev,
  129. struct cros_ec_dev, class_dev);
  130. msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
  131. if (!msg)
  132. return -ENOMEM;
  133. /* Get versions. RW may change. */
  134. msg->version = 0;
  135. msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
  136. msg->insize = sizeof(*r_ver);
  137. msg->outsize = 0;
  138. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  139. if (ret < 0) {
  140. count = ret;
  141. goto exit;
  142. }
  143. if (msg->result != EC_RES_SUCCESS) {
  144. count = scnprintf(buf, PAGE_SIZE,
  145. "ERROR: EC returned %d\n", msg->result);
  146. goto exit;
  147. }
  148. r_ver = (struct ec_response_get_version *)msg->data;
  149. /* Strings should be null-terminated, but let's be sure. */
  150. r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
  151. r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
  152. count += scnprintf(buf + count, PAGE_SIZE - count,
  153. "RO version: %s\n", r_ver->version_string_ro);
  154. count += scnprintf(buf + count, PAGE_SIZE - count,
  155. "RW version: %s\n", r_ver->version_string_rw);
  156. count += scnprintf(buf + count, PAGE_SIZE - count,
  157. "Firmware copy: %s\n",
  158. (r_ver->current_image < ARRAY_SIZE(image_names) ?
  159. image_names[r_ver->current_image] : "?"));
  160. /* Get build info. */
  161. msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset;
  162. msg->insize = EC_HOST_PARAM_SIZE;
  163. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  164. if (ret < 0)
  165. count += scnprintf(buf + count, PAGE_SIZE - count,
  166. "Build info: XFER ERROR %d\n", ret);
  167. else if (msg->result != EC_RES_SUCCESS)
  168. count += scnprintf(buf + count, PAGE_SIZE - count,
  169. "Build info: EC error %d\n", msg->result);
  170. else {
  171. msg->data[EC_HOST_PARAM_SIZE - 1] = '\0';
  172. count += scnprintf(buf + count, PAGE_SIZE - count,
  173. "Build info: %s\n", msg->data);
  174. }
  175. /* Get chip info. */
  176. msg->command = EC_CMD_GET_CHIP_INFO + ec->cmd_offset;
  177. msg->insize = sizeof(*r_chip);
  178. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  179. if (ret < 0)
  180. count += scnprintf(buf + count, PAGE_SIZE - count,
  181. "Chip info: XFER ERROR %d\n", ret);
  182. else if (msg->result != EC_RES_SUCCESS)
  183. count += scnprintf(buf + count, PAGE_SIZE - count,
  184. "Chip info: EC error %d\n", msg->result);
  185. else {
  186. r_chip = (struct ec_response_get_chip_info *)msg->data;
  187. r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
  188. r_chip->name[sizeof(r_chip->name) - 1] = '\0';
  189. r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
  190. count += scnprintf(buf + count, PAGE_SIZE - count,
  191. "Chip vendor: %s\n", r_chip->vendor);
  192. count += scnprintf(buf + count, PAGE_SIZE - count,
  193. "Chip name: %s\n", r_chip->name);
  194. count += scnprintf(buf + count, PAGE_SIZE - count,
  195. "Chip revision: %s\n", r_chip->revision);
  196. }
  197. /* Get board version */
  198. msg->command = EC_CMD_GET_BOARD_VERSION + ec->cmd_offset;
  199. msg->insize = sizeof(*r_board);
  200. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  201. if (ret < 0)
  202. count += scnprintf(buf + count, PAGE_SIZE - count,
  203. "Board version: XFER ERROR %d\n", ret);
  204. else if (msg->result != EC_RES_SUCCESS)
  205. count += scnprintf(buf + count, PAGE_SIZE - count,
  206. "Board version: EC error %d\n", msg->result);
  207. else {
  208. r_board = (struct ec_response_board_version *)msg->data;
  209. count += scnprintf(buf + count, PAGE_SIZE - count,
  210. "Board version: %d\n",
  211. r_board->board_version);
  212. }
  213. exit:
  214. kfree(msg);
  215. return count;
  216. }
  217. static ssize_t show_ec_flashinfo(struct device *dev,
  218. struct device_attribute *attr, char *buf)
  219. {
  220. struct ec_response_flash_info *resp;
  221. struct cros_ec_command *msg;
  222. int ret;
  223. struct cros_ec_dev *ec = container_of(dev,
  224. struct cros_ec_dev, class_dev);
  225. msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
  226. if (!msg)
  227. return -ENOMEM;
  228. /* The flash info shouldn't ever change, but ask each time anyway. */
  229. msg->version = 0;
  230. msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset;
  231. msg->insize = sizeof(*resp);
  232. msg->outsize = 0;
  233. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  234. if (ret < 0)
  235. goto exit;
  236. if (msg->result != EC_RES_SUCCESS) {
  237. ret = scnprintf(buf, PAGE_SIZE,
  238. "ERROR: EC returned %d\n", msg->result);
  239. goto exit;
  240. }
  241. resp = (struct ec_response_flash_info *)msg->data;
  242. ret = scnprintf(buf, PAGE_SIZE,
  243. "FlashSize %d\nWriteSize %d\n"
  244. "EraseSize %d\nProtectSize %d\n",
  245. resp->flash_size, resp->write_block_size,
  246. resp->erase_block_size, resp->protect_block_size);
  247. exit:
  248. kfree(msg);
  249. return ret;
  250. }
  251. /* Module initialization */
  252. static DEVICE_ATTR(reboot, S_IWUSR | S_IRUGO, show_ec_reboot, store_ec_reboot);
  253. static DEVICE_ATTR(version, S_IRUGO, show_ec_version, NULL);
  254. static DEVICE_ATTR(flashinfo, S_IRUGO, show_ec_flashinfo, NULL);
  255. static struct attribute *__ec_attrs[] = {
  256. &dev_attr_reboot.attr,
  257. &dev_attr_version.attr,
  258. &dev_attr_flashinfo.attr,
  259. NULL,
  260. };
  261. struct attribute_group cros_ec_attr_group = {
  262. .attrs = __ec_attrs,
  263. };