cros_ec_sysfs.c 10 KB

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