cros_ec_sysfs.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 reboot_show(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 reboot_store(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 = to_cros_ec_dev(dev);
  68. msg = kmalloc(sizeof(*msg) + sizeof(*param), GFP_KERNEL);
  69. if (!msg)
  70. return -ENOMEM;
  71. param = (struct ec_params_reboot_ec *)msg->data;
  72. param->flags = 0;
  73. while (1) {
  74. /* Find word to start scanning */
  75. while (buf[offset] && isspace(buf[offset]))
  76. offset++;
  77. if (!buf[offset])
  78. break;
  79. for (i = 0; i < ARRAY_SIZE(words); i++) {
  80. if (!strncasecmp(words[i].str, buf+offset,
  81. strlen(words[i].str))) {
  82. if (words[i].flags) {
  83. param->flags |= words[i].flags;
  84. } else {
  85. param->cmd = words[i].cmd;
  86. got_cmd = 1;
  87. }
  88. break;
  89. }
  90. }
  91. /* On to the next word, if any */
  92. while (buf[offset] && !isspace(buf[offset]))
  93. offset++;
  94. }
  95. if (!got_cmd) {
  96. count = -EINVAL;
  97. goto exit;
  98. }
  99. msg->version = 0;
  100. msg->command = EC_CMD_REBOOT_EC + ec->cmd_offset;
  101. msg->outsize = sizeof(*param);
  102. msg->insize = 0;
  103. ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
  104. if (ret < 0)
  105. count = ret;
  106. exit:
  107. kfree(msg);
  108. return count;
  109. }
  110. static ssize_t version_show(struct device *dev,
  111. struct device_attribute *attr, char *buf)
  112. {
  113. static const char * const image_names[] = {"unknown", "RO", "RW"};
  114. struct ec_response_get_version *r_ver;
  115. struct ec_response_get_chip_info *r_chip;
  116. struct ec_response_board_version *r_board;
  117. struct cros_ec_command *msg;
  118. int ret;
  119. int count = 0;
  120. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  121. msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
  122. if (!msg)
  123. return -ENOMEM;
  124. /* Get versions. RW may change. */
  125. msg->version = 0;
  126. msg->command = EC_CMD_GET_VERSION + ec->cmd_offset;
  127. msg->insize = sizeof(*r_ver);
  128. msg->outsize = 0;
  129. ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
  130. if (ret < 0) {
  131. count = ret;
  132. goto exit;
  133. }
  134. r_ver = (struct ec_response_get_version *)msg->data;
  135. /* Strings should be null-terminated, but let's be sure. */
  136. r_ver->version_string_ro[sizeof(r_ver->version_string_ro) - 1] = '\0';
  137. r_ver->version_string_rw[sizeof(r_ver->version_string_rw) - 1] = '\0';
  138. count += scnprintf(buf + count, PAGE_SIZE - count,
  139. "RO version: %s\n", r_ver->version_string_ro);
  140. count += scnprintf(buf + count, PAGE_SIZE - count,
  141. "RW version: %s\n", r_ver->version_string_rw);
  142. count += scnprintf(buf + count, PAGE_SIZE - count,
  143. "Firmware copy: %s\n",
  144. (r_ver->current_image < ARRAY_SIZE(image_names) ?
  145. image_names[r_ver->current_image] : "?"));
  146. /* Get build info. */
  147. msg->command = EC_CMD_GET_BUILD_INFO + ec->cmd_offset;
  148. msg->insize = EC_HOST_PARAM_SIZE;
  149. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  150. if (ret < 0)
  151. count += scnprintf(buf + count, PAGE_SIZE - count,
  152. "Build info: XFER ERROR %d\n", ret);
  153. else if (msg->result != EC_RES_SUCCESS)
  154. count += scnprintf(buf + count, PAGE_SIZE - count,
  155. "Build info: EC error %d\n", msg->result);
  156. else {
  157. msg->data[EC_HOST_PARAM_SIZE - 1] = '\0';
  158. count += scnprintf(buf + count, PAGE_SIZE - count,
  159. "Build info: %s\n", msg->data);
  160. }
  161. /* Get chip info. */
  162. msg->command = EC_CMD_GET_CHIP_INFO + ec->cmd_offset;
  163. msg->insize = sizeof(*r_chip);
  164. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  165. if (ret < 0)
  166. count += scnprintf(buf + count, PAGE_SIZE - count,
  167. "Chip info: XFER ERROR %d\n", ret);
  168. else if (msg->result != EC_RES_SUCCESS)
  169. count += scnprintf(buf + count, PAGE_SIZE - count,
  170. "Chip info: EC error %d\n", msg->result);
  171. else {
  172. r_chip = (struct ec_response_get_chip_info *)msg->data;
  173. r_chip->vendor[sizeof(r_chip->vendor) - 1] = '\0';
  174. r_chip->name[sizeof(r_chip->name) - 1] = '\0';
  175. r_chip->revision[sizeof(r_chip->revision) - 1] = '\0';
  176. count += scnprintf(buf + count, PAGE_SIZE - count,
  177. "Chip vendor: %s\n", r_chip->vendor);
  178. count += scnprintf(buf + count, PAGE_SIZE - count,
  179. "Chip name: %s\n", r_chip->name);
  180. count += scnprintf(buf + count, PAGE_SIZE - count,
  181. "Chip revision: %s\n", r_chip->revision);
  182. }
  183. /* Get board version */
  184. msg->command = EC_CMD_GET_BOARD_VERSION + ec->cmd_offset;
  185. msg->insize = sizeof(*r_board);
  186. ret = cros_ec_cmd_xfer(ec->ec_dev, msg);
  187. if (ret < 0)
  188. count += scnprintf(buf + count, PAGE_SIZE - count,
  189. "Board version: XFER ERROR %d\n", ret);
  190. else if (msg->result != EC_RES_SUCCESS)
  191. count += scnprintf(buf + count, PAGE_SIZE - count,
  192. "Board version: EC error %d\n", msg->result);
  193. else {
  194. r_board = (struct ec_response_board_version *)msg->data;
  195. count += scnprintf(buf + count, PAGE_SIZE - count,
  196. "Board version: %d\n",
  197. r_board->board_version);
  198. }
  199. exit:
  200. kfree(msg);
  201. return count;
  202. }
  203. static ssize_t flashinfo_show(struct device *dev,
  204. struct device_attribute *attr, char *buf)
  205. {
  206. struct ec_response_flash_info *resp;
  207. struct cros_ec_command *msg;
  208. int ret;
  209. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  210. msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL);
  211. if (!msg)
  212. return -ENOMEM;
  213. /* The flash info shouldn't ever change, but ask each time anyway. */
  214. msg->version = 0;
  215. msg->command = EC_CMD_FLASH_INFO + ec->cmd_offset;
  216. msg->insize = sizeof(*resp);
  217. msg->outsize = 0;
  218. ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
  219. if (ret < 0)
  220. goto exit;
  221. resp = (struct ec_response_flash_info *)msg->data;
  222. ret = scnprintf(buf, PAGE_SIZE,
  223. "FlashSize %d\nWriteSize %d\n"
  224. "EraseSize %d\nProtectSize %d\n",
  225. resp->flash_size, resp->write_block_size,
  226. resp->erase_block_size, resp->protect_block_size);
  227. exit:
  228. kfree(msg);
  229. return ret;
  230. }
  231. /* Keyboard wake angle control */
  232. static ssize_t kb_wake_angle_show(struct device *dev,
  233. struct device_attribute *attr, char *buf)
  234. {
  235. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  236. struct ec_response_motion_sense *resp;
  237. struct ec_params_motion_sense *param;
  238. struct cros_ec_command *msg;
  239. int ret;
  240. msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
  241. if (!msg)
  242. return -ENOMEM;
  243. param = (struct ec_params_motion_sense *)msg->data;
  244. msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
  245. msg->version = 2;
  246. param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
  247. param->kb_wake_angle.data = EC_MOTION_SENSE_NO_VALUE;
  248. msg->outsize = sizeof(*param);
  249. msg->insize = sizeof(*resp);
  250. ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
  251. if (ret < 0)
  252. goto exit;
  253. resp = (struct ec_response_motion_sense *)msg->data;
  254. ret = scnprintf(buf, PAGE_SIZE, "%d\n", resp->kb_wake_angle.ret);
  255. exit:
  256. kfree(msg);
  257. return ret;
  258. }
  259. static ssize_t kb_wake_angle_store(struct device *dev,
  260. struct device_attribute *attr,
  261. const char *buf, size_t count)
  262. {
  263. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  264. struct ec_params_motion_sense *param;
  265. struct cros_ec_command *msg;
  266. u16 angle;
  267. int ret;
  268. ret = kstrtou16(buf, 0, &angle);
  269. if (ret)
  270. return ret;
  271. msg = kmalloc(sizeof(*msg) + EC_HOST_PARAM_SIZE, GFP_KERNEL);
  272. if (!msg)
  273. return -ENOMEM;
  274. param = (struct ec_params_motion_sense *)msg->data;
  275. msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
  276. msg->version = 2;
  277. param->cmd = MOTIONSENSE_CMD_KB_WAKE_ANGLE;
  278. param->kb_wake_angle.data = angle;
  279. msg->outsize = sizeof(*param);
  280. msg->insize = sizeof(struct ec_response_motion_sense);
  281. ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
  282. kfree(msg);
  283. if (ret < 0)
  284. return ret;
  285. return count;
  286. }
  287. /* Module initialization */
  288. static DEVICE_ATTR_RW(reboot);
  289. static DEVICE_ATTR_RO(version);
  290. static DEVICE_ATTR_RO(flashinfo);
  291. static DEVICE_ATTR_RW(kb_wake_angle);
  292. static struct attribute *__ec_attrs[] = {
  293. &dev_attr_kb_wake_angle.attr,
  294. &dev_attr_reboot.attr,
  295. &dev_attr_version.attr,
  296. &dev_attr_flashinfo.attr,
  297. NULL,
  298. };
  299. static umode_t cros_ec_ctrl_visible(struct kobject *kobj,
  300. struct attribute *a, int n)
  301. {
  302. struct device *dev = container_of(kobj, struct device, kobj);
  303. struct cros_ec_dev *ec = to_cros_ec_dev(dev);
  304. if (a == &dev_attr_kb_wake_angle.attr && !ec->has_kb_wake_angle)
  305. return 0;
  306. return a->mode;
  307. }
  308. struct attribute_group cros_ec_attr_group = {
  309. .attrs = __ec_attrs,
  310. .is_visible = cros_ec_ctrl_visible,
  311. };
  312. EXPORT_SYMBOL(cros_ec_attr_group);
  313. MODULE_LICENSE("GPL");
  314. MODULE_DESCRIPTION("ChromeOS EC control driver");