telemetry.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Telemetry communication for Wilco EC
  4. *
  5. * Copyright 2019 Google LLC
  6. *
  7. * The Wilco Embedded Controller is able to send telemetry data
  8. * which is useful for enterprise applications. A daemon running on
  9. * the OS sends a command to the EC via a write() to a char device,
  10. * and can read the response with a read(). The write() request is
  11. * verified by the driver to ensure that it is performing only one
  12. * of the allowlisted commands, and that no extraneous data is
  13. * being transmitted to the EC. The response is passed directly
  14. * back to the reader with no modification.
  15. *
  16. * The character device will appear as /dev/wilco_telemN, where N
  17. * is some small non-negative integer, starting with 0. Only one
  18. * process may have the file descriptor open at a time. The calling
  19. * userspace program needs to keep the device file descriptor open
  20. * between the calls to write() and read() in order to preserve the
  21. * response. Up to 32 bytes will be available for reading.
  22. *
  23. * For testing purposes, try requesting the EC's firmware build
  24. * date, by sending the WILCO_EC_TELEM_GET_VERSION command with
  25. * argument index=3. i.e. write [0x38, 0x00, 0x03]
  26. * to the device node. An ASCII string of the build date is
  27. * returned.
  28. */
  29. #include <linux/cdev.h>
  30. #include <linux/device.h>
  31. #include <linux/fs.h>
  32. #include <linux/mod_devicetable.h>
  33. #include <linux/module.h>
  34. #include <linux/platform_data/wilco-ec.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/slab.h>
  37. #include <linux/types.h>
  38. #include <linux/uaccess.h>
  39. #define TELEM_DEV_NAME "wilco_telem"
  40. #define TELEM_CLASS_NAME TELEM_DEV_NAME
  41. #define DRV_NAME TELEM_DEV_NAME
  42. #define TELEM_DEV_NAME_FMT (TELEM_DEV_NAME "%d")
  43. static struct class telem_class = {
  44. .name = TELEM_CLASS_NAME,
  45. };
  46. /* Keep track of all the device numbers used. */
  47. #define TELEM_MAX_DEV 128
  48. static int telem_major;
  49. static DEFINE_IDA(telem_ida);
  50. /* EC telemetry command codes */
  51. #define WILCO_EC_TELEM_GET_LOG 0x99
  52. #define WILCO_EC_TELEM_GET_VERSION 0x38
  53. #define WILCO_EC_TELEM_GET_FAN_INFO 0x2E
  54. #define WILCO_EC_TELEM_GET_DIAG_INFO 0xFA
  55. #define WILCO_EC_TELEM_GET_TEMP_INFO 0x95
  56. #define WILCO_EC_TELEM_GET_TEMP_READ 0x2C
  57. #define WILCO_EC_TELEM_GET_BATT_EXT_INFO 0x07
  58. #define WILCO_EC_TELEM_GET_BATT_PPID_INFO 0x8A
  59. #define TELEM_ARGS_SIZE_MAX 30
  60. /*
  61. * The following telem_args_get_* structs are embedded within the |args| field
  62. * of wilco_ec_telem_request.
  63. */
  64. struct telem_args_get_log {
  65. u8 log_type;
  66. u8 log_index;
  67. } __packed;
  68. /*
  69. * Get a piece of info about the EC firmware version:
  70. * 0 = label
  71. * 1 = svn_rev
  72. * 2 = model_no
  73. * 3 = build_date
  74. * 4 = frio_version
  75. */
  76. struct telem_args_get_version {
  77. u8 index;
  78. } __packed;
  79. struct telem_args_get_fan_info {
  80. u8 command;
  81. u8 fan_number;
  82. u8 arg;
  83. } __packed;
  84. struct telem_args_get_diag_info {
  85. u8 type;
  86. u8 sub_type;
  87. } __packed;
  88. struct telem_args_get_temp_info {
  89. u8 command;
  90. u8 index;
  91. u8 field;
  92. u8 zone;
  93. } __packed;
  94. struct telem_args_get_temp_read {
  95. u8 sensor_index;
  96. } __packed;
  97. struct telem_args_get_batt_ext_info {
  98. u8 var_args[5];
  99. } __packed;
  100. struct telem_args_get_batt_ppid_info {
  101. u8 always1; /* Should always be 1 */
  102. } __packed;
  103. /**
  104. * struct wilco_ec_telem_request - Telemetry command and arguments sent to EC.
  105. * @command: One of WILCO_EC_TELEM_GET_* command codes.
  106. * @reserved: Must be 0.
  107. * @args: The first N bytes are one of telem_args_get_* structs, the rest is 0.
  108. */
  109. struct wilco_ec_telem_request {
  110. u8 command;
  111. u8 reserved;
  112. union {
  113. u8 buf[TELEM_ARGS_SIZE_MAX];
  114. struct telem_args_get_log get_log;
  115. struct telem_args_get_version get_version;
  116. struct telem_args_get_fan_info get_fan_info;
  117. struct telem_args_get_diag_info get_diag_info;
  118. struct telem_args_get_temp_info get_temp_info;
  119. struct telem_args_get_temp_read get_temp_read;
  120. struct telem_args_get_batt_ext_info get_batt_ext_info;
  121. struct telem_args_get_batt_ppid_info get_batt_ppid_info;
  122. } args;
  123. } __packed;
  124. /**
  125. * check_telem_request() - Ensure that a request from userspace is valid.
  126. * @rq: Request buffer copied from userspace.
  127. * @size: Number of bytes copied from userspace.
  128. *
  129. * Return: 0 if valid, -EINVAL if bad command or reserved byte is non-zero,
  130. * -EMSGSIZE if the request is too long.
  131. *
  132. * We do not want to allow userspace to send arbitrary telemetry commands to
  133. * the EC. Therefore we check to ensure that
  134. * 1. The request follows the format of struct wilco_ec_telem_request.
  135. * 2. The supplied command code is one of the allowlisted commands.
  136. * 3. The request only contains the necessary data for the header and arguments.
  137. */
  138. static int check_telem_request(struct wilco_ec_telem_request *rq,
  139. size_t size)
  140. {
  141. size_t max_size = offsetof(struct wilco_ec_telem_request, args);
  142. if (rq->reserved)
  143. return -EINVAL;
  144. switch (rq->command) {
  145. case WILCO_EC_TELEM_GET_LOG:
  146. max_size += sizeof(rq->args.get_log);
  147. break;
  148. case WILCO_EC_TELEM_GET_VERSION:
  149. max_size += sizeof(rq->args.get_version);
  150. break;
  151. case WILCO_EC_TELEM_GET_FAN_INFO:
  152. max_size += sizeof(rq->args.get_fan_info);
  153. break;
  154. case WILCO_EC_TELEM_GET_DIAG_INFO:
  155. max_size += sizeof(rq->args.get_diag_info);
  156. break;
  157. case WILCO_EC_TELEM_GET_TEMP_INFO:
  158. max_size += sizeof(rq->args.get_temp_info);
  159. break;
  160. case WILCO_EC_TELEM_GET_TEMP_READ:
  161. max_size += sizeof(rq->args.get_temp_read);
  162. break;
  163. case WILCO_EC_TELEM_GET_BATT_EXT_INFO:
  164. max_size += sizeof(rq->args.get_batt_ext_info);
  165. break;
  166. case WILCO_EC_TELEM_GET_BATT_PPID_INFO:
  167. if (rq->args.get_batt_ppid_info.always1 != 1)
  168. return -EINVAL;
  169. max_size += sizeof(rq->args.get_batt_ppid_info);
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. return (size <= max_size) ? 0 : -EMSGSIZE;
  175. }
  176. /**
  177. * struct telem_device_data - Data for a Wilco EC device that queries telemetry.
  178. * @cdev: Char dev that userspace reads and polls from.
  179. * @dev: Device associated with the %cdev.
  180. * @ec: Wilco EC that we will be communicating with using the mailbox interface.
  181. * @available: Boolean of if the device can be opened.
  182. */
  183. struct telem_device_data {
  184. struct device dev;
  185. struct cdev cdev;
  186. struct wilco_ec_device *ec;
  187. atomic_t available;
  188. };
  189. #define TELEM_RESPONSE_SIZE EC_MAILBOX_DATA_SIZE
  190. /**
  191. * struct telem_session_data - Data that exists between open() and release().
  192. * @dev_data: Pointer to get back to the device data and EC.
  193. * @request: Command and arguments sent to EC.
  194. * @response: Response buffer of data from EC.
  195. * @has_msg: Is there data available to read from a previous write?
  196. */
  197. struct telem_session_data {
  198. struct telem_device_data *dev_data;
  199. struct wilco_ec_telem_request request;
  200. u8 response[TELEM_RESPONSE_SIZE];
  201. bool has_msg;
  202. };
  203. /**
  204. * telem_open() - Callback for when the device node is opened.
  205. * @inode: inode for this char device node.
  206. * @filp: file for this char device node.
  207. *
  208. * We need to ensure that after writing a command to the device,
  209. * the same userspace process reads the corresponding result.
  210. * Therefore, we increment a refcount on opening the device, so that
  211. * only one process can communicate with the EC at a time.
  212. *
  213. * Return: 0 on success, or negative error code on failure.
  214. */
  215. static int telem_open(struct inode *inode, struct file *filp)
  216. {
  217. struct telem_device_data *dev_data;
  218. struct telem_session_data *sess_data;
  219. /* Ensure device isn't already open */
  220. dev_data = container_of(inode->i_cdev, struct telem_device_data, cdev);
  221. if (atomic_cmpxchg(&dev_data->available, 1, 0) == 0)
  222. return -EBUSY;
  223. get_device(&dev_data->dev);
  224. sess_data = kzalloc(sizeof(*sess_data), GFP_KERNEL);
  225. if (!sess_data) {
  226. atomic_set(&dev_data->available, 1);
  227. return -ENOMEM;
  228. }
  229. sess_data->dev_data = dev_data;
  230. sess_data->has_msg = false;
  231. stream_open(inode, filp);
  232. filp->private_data = sess_data;
  233. return 0;
  234. }
  235. static ssize_t telem_write(struct file *filp, const char __user *buf,
  236. size_t count, loff_t *pos)
  237. {
  238. struct telem_session_data *sess_data = filp->private_data;
  239. struct wilco_ec_message msg = {};
  240. int ret;
  241. if (count > sizeof(sess_data->request))
  242. return -EMSGSIZE;
  243. memset(&sess_data->request, 0, sizeof(sess_data->request));
  244. if (copy_from_user(&sess_data->request, buf, count))
  245. return -EFAULT;
  246. ret = check_telem_request(&sess_data->request, count);
  247. if (ret < 0)
  248. return ret;
  249. memset(sess_data->response, 0, sizeof(sess_data->response));
  250. msg.type = WILCO_EC_MSG_TELEMETRY;
  251. msg.request_data = &sess_data->request;
  252. msg.request_size = sizeof(sess_data->request);
  253. msg.response_data = sess_data->response;
  254. msg.response_size = sizeof(sess_data->response);
  255. ret = wilco_ec_mailbox(sess_data->dev_data->ec, &msg);
  256. if (ret < 0)
  257. return ret;
  258. if (ret != sizeof(sess_data->response))
  259. return -EMSGSIZE;
  260. sess_data->has_msg = true;
  261. return count;
  262. }
  263. static ssize_t telem_read(struct file *filp, char __user *buf, size_t count,
  264. loff_t *pos)
  265. {
  266. struct telem_session_data *sess_data = filp->private_data;
  267. if (!sess_data->has_msg)
  268. return -ENODATA;
  269. if (count > sizeof(sess_data->response))
  270. return -EINVAL;
  271. if (copy_to_user(buf, sess_data->response, count))
  272. return -EFAULT;
  273. sess_data->has_msg = false;
  274. return count;
  275. }
  276. static int telem_release(struct inode *inode, struct file *filp)
  277. {
  278. struct telem_session_data *sess_data = filp->private_data;
  279. atomic_set(&sess_data->dev_data->available, 1);
  280. put_device(&sess_data->dev_data->dev);
  281. kfree(sess_data);
  282. return 0;
  283. }
  284. static const struct file_operations telem_fops = {
  285. .open = telem_open,
  286. .write = telem_write,
  287. .read = telem_read,
  288. .release = telem_release,
  289. .owner = THIS_MODULE,
  290. };
  291. /**
  292. * telem_device_free() - Callback to free the telem_device_data structure.
  293. * @d: The device embedded in our device data, which we have been ref counting.
  294. *
  295. * Once all open file descriptors are closed and the device has been removed,
  296. * the refcount of the device will fall to 0 and this will be called.
  297. */
  298. static void telem_device_free(struct device *d)
  299. {
  300. struct telem_device_data *dev_data;
  301. dev_data = container_of(d, struct telem_device_data, dev);
  302. kfree(dev_data);
  303. }
  304. /**
  305. * telem_device_probe() - Callback when creating a new device.
  306. * @pdev: platform device that we will be receiving telems from.
  307. *
  308. * This finds a free minor number for the device, allocates and initializes
  309. * some device data, and creates a new device and char dev node.
  310. *
  311. * Return: 0 on success, negative error code on failure.
  312. */
  313. static int telem_device_probe(struct platform_device *pdev)
  314. {
  315. struct telem_device_data *dev_data;
  316. int error, minor;
  317. /* Get the next available device number */
  318. minor = ida_alloc_max(&telem_ida, TELEM_MAX_DEV-1, GFP_KERNEL);
  319. if (minor < 0) {
  320. error = minor;
  321. dev_err(&pdev->dev, "Failed to find minor number: %d\n", error);
  322. return error;
  323. }
  324. dev_data = kzalloc(sizeof(*dev_data), GFP_KERNEL);
  325. if (!dev_data) {
  326. ida_free(&telem_ida, minor);
  327. return -ENOMEM;
  328. }
  329. /* Initialize the device data */
  330. dev_data->ec = dev_get_platdata(&pdev->dev);
  331. atomic_set(&dev_data->available, 1);
  332. platform_set_drvdata(pdev, dev_data);
  333. /* Initialize the device */
  334. dev_data->dev.devt = MKDEV(telem_major, minor);
  335. dev_data->dev.class = &telem_class;
  336. dev_data->dev.release = telem_device_free;
  337. dev_set_name(&dev_data->dev, TELEM_DEV_NAME_FMT, minor);
  338. device_initialize(&dev_data->dev);
  339. /* Initialize the character device and add it to userspace */;
  340. cdev_init(&dev_data->cdev, &telem_fops);
  341. error = cdev_device_add(&dev_data->cdev, &dev_data->dev);
  342. if (error) {
  343. put_device(&dev_data->dev);
  344. ida_free(&telem_ida, minor);
  345. return error;
  346. }
  347. return 0;
  348. }
  349. static void telem_device_remove(struct platform_device *pdev)
  350. {
  351. struct telem_device_data *dev_data = platform_get_drvdata(pdev);
  352. cdev_device_del(&dev_data->cdev, &dev_data->dev);
  353. ida_free(&telem_ida, MINOR(dev_data->dev.devt));
  354. put_device(&dev_data->dev);
  355. }
  356. static const struct platform_device_id telem_id[] = {
  357. { DRV_NAME, 0 },
  358. {}
  359. };
  360. MODULE_DEVICE_TABLE(platform, telem_id);
  361. static struct platform_driver telem_driver = {
  362. .probe = telem_device_probe,
  363. .remove_new = telem_device_remove,
  364. .driver = {
  365. .name = DRV_NAME,
  366. },
  367. .id_table = telem_id,
  368. };
  369. static int __init telem_module_init(void)
  370. {
  371. dev_t dev_num = 0;
  372. int ret;
  373. ret = class_register(&telem_class);
  374. if (ret) {
  375. pr_err(DRV_NAME ": Failed registering class: %d\n", ret);
  376. return ret;
  377. }
  378. /* Request the kernel for device numbers, starting with minor=0 */
  379. ret = alloc_chrdev_region(&dev_num, 0, TELEM_MAX_DEV, TELEM_DEV_NAME);
  380. if (ret) {
  381. pr_err(DRV_NAME ": Failed allocating dev numbers: %d\n", ret);
  382. goto destroy_class;
  383. }
  384. telem_major = MAJOR(dev_num);
  385. ret = platform_driver_register(&telem_driver);
  386. if (ret < 0) {
  387. pr_err(DRV_NAME ": Failed registering driver: %d\n", ret);
  388. goto unregister_region;
  389. }
  390. return 0;
  391. unregister_region:
  392. unregister_chrdev_region(MKDEV(telem_major, 0), TELEM_MAX_DEV);
  393. destroy_class:
  394. class_unregister(&telem_class);
  395. ida_destroy(&telem_ida);
  396. return ret;
  397. }
  398. static void __exit telem_module_exit(void)
  399. {
  400. platform_driver_unregister(&telem_driver);
  401. unregister_chrdev_region(MKDEV(telem_major, 0), TELEM_MAX_DEV);
  402. class_unregister(&telem_class);
  403. ida_destroy(&telem_ida);
  404. }
  405. module_init(telem_module_init);
  406. module_exit(telem_module_exit);
  407. MODULE_AUTHOR("Nick Crews <ncrews@chromium.org>");
  408. MODULE_DESCRIPTION("Wilco EC telemetry driver");
  409. MODULE_LICENSE("GPL");