cros_ec_sysfs.c 8.5 KB

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