// SPDX-License-Identifier: GPL-2.0 /* * Telemetry communication for Wilco EC * * Copyright 2019 Google LLC * * The Wilco Embedded Controller is able to send telemetry data * which is useful for enterprise applications. A daemon running on * the OS sends a command to the EC via a write() to a char device, * and can read the response with a read(). The write() request is * verified by the driver to ensure that it is performing only one * of the allowlisted commands, and that no extraneous data is * being transmitted to the EC. The response is passed directly * back to the reader with no modification. * * The character device will appear as /dev/wilco_telemN, where N * is some small non-negative integer, starting with 0. Only one * process may have the file descriptor open at a time. The calling * userspace program needs to keep the device file descriptor open * between the calls to write() and read() in order to preserve the * response. Up to 32 bytes will be available for reading. * * For testing purposes, try requesting the EC's firmware build * date, by sending the WILCO_EC_TELEM_GET_VERSION command with * argument index=3. i.e. write [0x38, 0x00, 0x03] * to the device node. An ASCII string of the build date is * returned.
*/
struct telem_args_get_batt_ppid_info {
u8 always1; /* Should always be 1 */
} __packed;
/** * struct wilco_ec_telem_request - Telemetry command and arguments sent to EC. * @command: One of WILCO_EC_TELEM_GET_* command codes. * @reserved: Must be 0. * @args: The first N bytes are one of telem_args_get_* structs, the rest is 0.
*/ struct wilco_ec_telem_request {
u8 command;
u8 reserved; union {
u8 buf[TELEM_ARGS_SIZE_MAX]; struct telem_args_get_log get_log; struct telem_args_get_version get_version; struct telem_args_get_fan_info get_fan_info; struct telem_args_get_diag_info get_diag_info; struct telem_args_get_temp_info get_temp_info; struct telem_args_get_temp_read get_temp_read; struct telem_args_get_batt_ext_info get_batt_ext_info; struct telem_args_get_batt_ppid_info get_batt_ppid_info;
} args;
} __packed;
/** * check_telem_request() - Ensure that a request from userspace is valid. * @rq: Request buffer copied from userspace. * @size: Number of bytes copied from userspace. * * Return: 0 if valid, -EINVAL if bad command or reserved byte is non-zero, * -EMSGSIZE if the request is too long. * * We do not want to allow userspace to send arbitrary telemetry commands to * the EC. Therefore we check to ensure that * 1. The request follows the format of struct wilco_ec_telem_request. * 2. The supplied command code is one of the allowlisted commands. * 3. The request only contains the necessary data for the header and arguments.
*/ staticint check_telem_request(struct wilco_ec_telem_request *rq,
size_t size)
{
size_t max_size = offsetof(struct wilco_ec_telem_request, args);
if (rq->reserved) return -EINVAL;
switch (rq->command) { case WILCO_EC_TELEM_GET_LOG:
max_size += sizeof(rq->args.get_log); break; case WILCO_EC_TELEM_GET_VERSION:
max_size += sizeof(rq->args.get_version); break; case WILCO_EC_TELEM_GET_FAN_INFO:
max_size += sizeof(rq->args.get_fan_info); break; case WILCO_EC_TELEM_GET_DIAG_INFO:
max_size += sizeof(rq->args.get_diag_info); break; case WILCO_EC_TELEM_GET_TEMP_INFO:
max_size += sizeof(rq->args.get_temp_info); break; case WILCO_EC_TELEM_GET_TEMP_READ:
max_size += sizeof(rq->args.get_temp_read); break; case WILCO_EC_TELEM_GET_BATT_EXT_INFO:
max_size += sizeof(rq->args.get_batt_ext_info); break; case WILCO_EC_TELEM_GET_BATT_PPID_INFO: if (rq->args.get_batt_ppid_info.always1 != 1) return -EINVAL;
/** * struct telem_device_data - Data for a Wilco EC device that queries telemetry. * @cdev: Char dev that userspace reads and polls from. * @dev: Device associated with the %cdev. * @ec: Wilco EC that we will be communicating with using the mailbox interface. * @available: Boolean of if the device can be opened.
*/ struct telem_device_data { struct device dev; struct cdev cdev; struct wilco_ec_device *ec;
atomic_t available;
};
#define TELEM_RESPONSE_SIZE EC_MAILBOX_DATA_SIZE
/** * struct telem_session_data - Data that exists between open() and release(). * @dev_data: Pointer to get back to the device data and EC. * @request: Command and arguments sent to EC. * @response: Response buffer of data from EC. * @has_msg: Is there data available to read from a previous write?
*/ struct telem_session_data { struct telem_device_data *dev_data; struct wilco_ec_telem_request request;
u8 response[TELEM_RESPONSE_SIZE]; bool has_msg;
};
/** * telem_open() - Callback for when the device node is opened. * @inode: inode for this char device node. * @filp: file for this char device node. * * We need to ensure that after writing a command to the device, * the same userspace process reads the corresponding result. * Therefore, we increment a refcount on opening the device, so that * only one process can communicate with the EC at a time. * * Return: 0 on success, or negative error code on failure.
*/ staticint telem_open(struct inode *inode, struct file *filp)
{ struct telem_device_data *dev_data; struct telem_session_data *sess_data;
/** * telem_device_free() - Callback to free the telem_device_data structure. * @d: The device embedded in our device data, which we have been ref counting. * * Once all open file descriptors are closed and the device has been removed, * the refcount of the device will fall to 0 and this will be called.
*/ staticvoid telem_device_free(struct device *d)
{ struct telem_device_data *dev_data;
/** * telem_device_probe() - Callback when creating a new device. * @pdev: platform device that we will be receiving telems from. * * This finds a free minor number for the device, allocates and initializes * some device data, and creates a new device and char dev node. * * Return: 0 on success, negative error code on failure.
*/ staticint telem_device_probe(struct platform_device *pdev)
{ struct telem_device_data *dev_data; int error, minor;
/* Get the next available device number */
minor = ida_alloc_max(&telem_ida, TELEM_MAX_DEV-1, GFP_KERNEL); if (minor < 0) {
error = minor;
dev_err(&pdev->dev, "Failed to find minor number: %d\n", error); return error;
}
/* Initialize the character device and add it to userspace */;
cdev_init(&dev_data->cdev, &telem_fops);
error = cdev_device_add(&dev_data->cdev, &dev_data->dev); if (error) {
put_device(&dev_data->dev);
ida_free(&telem_ida, minor); return error;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.