894 lines · 24.1 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0+/* * Surface Serial Hub (SSH) driver for communication with the Surface/System * Aggregator Module (SSAM/SAM). * * Provides access to a SAM-over-SSH connected EC via a controller device. * Handles communication via requests as well as enabling, disabling, and * relaying of events. * * Copyright (C) 2019-2022 Maximilian Luz <luzmaximilian@gmail.com> */ #include <linux/acpi.h>#include <linux/atomic.h>#include <linux/completion.h>#include <linux/gpio/consumer.h>#include <linux/kernel.h>#include <linux/kref.h>#include <linux/module.h>#include <linux/of.h>#include <linux/platform_device.h>#include <linux/pm.h>#include <linux/serdev.h>#include <linux/sysfs.h>#include <linux/units.h> #include <linux/surface_aggregator/controller.h>#include <linux/surface_aggregator/device.h> #include "bus.h"#include "controller.h" #define CREATE_TRACE_POINTS#include "trace.h"  /* -- Static controller reference. ------------------------------------------ */ /* * Main controller reference. The corresponding lock must be held while * accessing (reading/writing) the reference. */static struct ssam_controller *__ssam_controller;static DEFINE_SPINLOCK(__ssam_controller_lock); /** * ssam_get_controller() - Get reference to SSAM controller. * * Returns a reference to the SSAM controller of the system or %NULL if there * is none, it hasn't been set up yet, or it has already been unregistered. * This function automatically increments the reference count of the * controller, thus the calling party must ensure that ssam_controller_put() * is called when it doesn't need the controller any more. */struct ssam_controller *ssam_get_controller(void){	struct ssam_controller *ctrl; 	spin_lock(&__ssam_controller_lock); 	ctrl = __ssam_controller;	if (!ctrl)		goto out; 	if (WARN_ON(!kref_get_unless_zero(&ctrl->kref)))		ctrl = NULL; out:	spin_unlock(&__ssam_controller_lock);	return ctrl;}EXPORT_SYMBOL_GPL(ssam_get_controller); /** * ssam_try_set_controller() - Try to set the main controller reference. * @ctrl: The controller to which the reference should point. * * Set the main controller reference to the given pointer if the reference * hasn't been set already. * * Return: Returns zero on success or %-EEXIST if the reference has already * been set. */static int ssam_try_set_controller(struct ssam_controller *ctrl){	int status = 0; 	spin_lock(&__ssam_controller_lock);	if (!__ssam_controller)		__ssam_controller = ctrl;	else		status = -EEXIST;	spin_unlock(&__ssam_controller_lock); 	return status;} /** * ssam_clear_controller() - Remove/clear the main controller reference. * * Clears the main controller reference, i.e. sets it to %NULL. This function * should be called before the controller is shut down. */static void ssam_clear_controller(void){	spin_lock(&__ssam_controller_lock);	__ssam_controller = NULL;	spin_unlock(&__ssam_controller_lock);} /** * ssam_client_link() - Link an arbitrary client device to the controller. * @c: The controller to link to. * @client: The client device. * * Link an arbitrary client device to the controller by creating a device link * between it as consumer and the controller device as provider. This function * can be used for non-SSAM devices (or SSAM devices not registered as child * under the controller) to guarantee that the controller is valid for as long * as the driver of the client device is bound, and that proper suspend and * resume ordering is guaranteed. * * The device link does not have to be destructed manually. It is removed * automatically once the driver of the client device unbinds. * * Return: Returns zero on success, %-ENODEV if the controller is not ready or * going to be removed soon, or %-ENOMEM if the device link could not be * created for other reasons. */int ssam_client_link(struct ssam_controller *c, struct device *client){	const u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;	struct device_link *link;	struct device *ctrldev; 	ssam_controller_statelock(c); 	if (c->state != SSAM_CONTROLLER_STARTED) {		ssam_controller_stateunlock(c);		return -ENODEV;	} 	ctrldev = ssam_controller_device(c);	if (!ctrldev) {		ssam_controller_stateunlock(c);		return -ENODEV;	} 	link = device_link_add(client, ctrldev, flags);	if (!link) {		ssam_controller_stateunlock(c);		return -ENOMEM;	} 	/*	 * Return -ENODEV if supplier driver is on its way to be removed. In	 * this case, the controller won't be around for much longer and the	 * device link is not going to save us any more, as unbinding is	 * already in progress.	 */	if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND) {		ssam_controller_stateunlock(c);		return -ENODEV;	} 	ssam_controller_stateunlock(c);	return 0;}EXPORT_SYMBOL_GPL(ssam_client_link); /** * ssam_client_bind() - Bind an arbitrary client device to the controller. * @client: The client device. * * Link an arbitrary client device to the controller by creating a device link * between it as consumer and the main controller device as provider. This * function can be used for non-SSAM devices to guarantee that the controller * returned by this function is valid for as long as the driver of the client * device is bound, and that proper suspend and resume ordering is guaranteed. * * This function does essentially the same as ssam_client_link(), except that * it first fetches the main controller reference, then creates the link, and * finally returns this reference. Note that this function does not increment * the reference counter of the controller, as, due to the link, the * controller lifetime is assured as long as the driver of the client device * is bound. * * It is not valid to use the controller reference obtained by this method * outside of the driver bound to the client device at the time of calling * this function, without first incrementing the reference count of the * controller via ssam_controller_get(). Even after doing this, care must be * taken that requests are only submitted and notifiers are only * (un-)registered when the controller is active and not suspended. In other * words: The device link only lives as long as the client driver is bound and * any guarantees enforced by this link (e.g. active controller state) can * only be relied upon as long as this link exists and may need to be enforced * in other ways afterwards. * * The created device link does not have to be destructed manually. It is * removed automatically once the driver of the client device unbinds. * * Return: Returns the controller on success, an error pointer with %-ENODEV * if the controller is not present, not ready or going to be removed soon, or * %-ENOMEM if the device link could not be created for other reasons. */struct ssam_controller *ssam_client_bind(struct device *client){	struct ssam_controller *c;	int status; 	c = ssam_get_controller();	if (!c)		return ERR_PTR(-ENODEV); 	status = ssam_client_link(c, client); 	/*	 * Note that we can drop our controller reference in both success and	 * failure cases: On success, we have bound the controller lifetime	 * inherently to the client driver lifetime, i.e. it the controller is	 * now guaranteed to outlive the client driver. On failure, we're not	 * going to use the controller any more.	 */	ssam_controller_put(c); 	return status >= 0 ? c : ERR_PTR(status);}EXPORT_SYMBOL_GPL(ssam_client_bind);  /* -- Glue layer (serdev_device -> ssam_controller). ------------------------ */ static size_t ssam_receive_buf(struct serdev_device *dev, const u8 *buf,			       size_t n){	struct ssam_controller *ctrl;	int ret; 	ctrl = serdev_device_get_drvdata(dev);	ret = ssam_controller_receive_buf(ctrl, buf, n); 	return ret < 0 ? 0 : ret;} static void ssam_write_wakeup(struct serdev_device *dev){	ssam_controller_write_wakeup(serdev_device_get_drvdata(dev));} static const struct serdev_device_ops ssam_serdev_ops = {	.receive_buf = ssam_receive_buf,	.write_wakeup = ssam_write_wakeup,};  /* -- SysFS and misc. ------------------------------------------------------- */ static int ssam_log_firmware_version(struct ssam_controller *ctrl){	u32 version, a, b, c;	int status; 	status = ssam_get_firmware_version(ctrl, &version);	if (status)		return status; 	a = (version >> 24) & 0xff;	b = ((version >> 8) & 0xffff);	c = version & 0xff; 	ssam_info(ctrl, "SAM firmware version: %u.%u.%u\n", a, b, c);	return 0;} static ssize_t firmware_version_show(struct device *dev,				     struct device_attribute *attr, char *buf){	struct ssam_controller *ctrl = dev_get_drvdata(dev);	u32 version, a, b, c;	int status; 	status = ssam_get_firmware_version(ctrl, &version);	if (status < 0)		return status; 	a = (version >> 24) & 0xff;	b = ((version >> 8) & 0xffff);	c = version & 0xff; 	return sysfs_emit(buf, "%u.%u.%u\n", a, b, c);}static DEVICE_ATTR_RO(firmware_version); static struct attribute *ssam_sam_attrs[] = {	&dev_attr_firmware_version.attr,	NULL}; static const struct attribute_group ssam_sam_group = {	.name = "sam",	.attrs = ssam_sam_attrs,};  /* -- Serial device setup. -------------------------------------------------- */ static acpi_status ssam_serdev_setup_via_acpi_crs(struct acpi_resource *rsc,						  void *ctx){	struct serdev_device *serdev = ctx;	struct acpi_resource_uart_serialbus *uart;	bool flow_control;	int status = 0; 	if (!serdev_acpi_get_uart_resource(rsc, &uart))		return AE_OK; 	/* Set up serdev device. */	serdev_device_set_baudrate(serdev, uart->default_baud_rate); 	/* serdev currently only supports RTSCTS flow control. */	if (uart->flow_control & (~((u8)ACPI_UART_FLOW_CONTROL_HW))) {		dev_warn(&serdev->dev, "setup: unsupported flow control (value: %#04x)\n",			 uart->flow_control);	} 	/* Set RTSCTS flow control. */	flow_control = uart->flow_control & ACPI_UART_FLOW_CONTROL_HW;	serdev_device_set_flow_control(serdev, flow_control); 	/* serdev currently only supports EVEN/ODD parity. */	switch (uart->parity) {	case ACPI_UART_PARITY_NONE:		status = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);		break;	case ACPI_UART_PARITY_EVEN:		status = serdev_device_set_parity(serdev, SERDEV_PARITY_EVEN);		break;	case ACPI_UART_PARITY_ODD:		status = serdev_device_set_parity(serdev, SERDEV_PARITY_ODD);		break;	default:		dev_warn(&serdev->dev, "setup: unsupported parity (value: %#04x)\n",			 uart->parity);		break;	} 	if (status) {		dev_err(&serdev->dev, "setup: failed to set parity (value: %#04x, error: %d)\n",			uart->parity, status);		return AE_ERROR;	} 	/* We've found the resource and are done. */	return AE_CTRL_TERMINATE;} static int ssam_serdev_setup_via_acpi(struct serdev_device *serdev, acpi_handle handle){	acpi_status status; 	status = acpi_walk_resources(handle, METHOD_NAME__CRS,				     ssam_serdev_setup_via_acpi_crs, serdev); 	return status ? -ENXIO : 0;} static int ssam_serdev_setup(struct acpi_device *ssh, struct serdev_device *serdev){	if (ssh)		return ssam_serdev_setup_via_acpi(serdev, ssh->handle); 	/* TODO: these values may differ per board/implementation */	serdev_device_set_baudrate(serdev, 4 * HZ_PER_MHZ);	serdev_device_set_flow_control(serdev, true);	serdev_device_set_parity(serdev, SERDEV_PARITY_NONE); 	return 0;} /* -- Power management. ----------------------------------------------------- */ static void ssam_serial_hub_shutdown(struct serdev_device *serdev){	struct ssam_controller *c = dev_get_drvdata(&serdev->dev);	int status; 	/*	 * Try to disable notifiers, signal display-off and D0-exit, ignore any	 * errors.	 *	 * Note: It has not been established yet if this is actually	 * necessary/useful for shutdown.	 */ 	status = ssam_notifier_disable_registered(c);	if (status) {		ssam_err(c, "pm: failed to disable notifiers for shutdown: %d\n",			 status);	} 	status = ssam_ctrl_notif_display_off(c);	if (status)		ssam_err(c, "pm: display-off notification failed: %d\n", status); 	status = ssam_ctrl_notif_d0_exit(c);	if (status)		ssam_err(c, "pm: D0-exit notification failed: %d\n", status);} #ifdef CONFIG_PM_SLEEP static int ssam_serial_hub_pm_prepare(struct device *dev){	struct ssam_controller *c = dev_get_drvdata(dev);	int status; 	/*	 * Try to signal display-off, This will quiesce events.	 *	 * Note: Signaling display-off/display-on should normally be done from	 * some sort of display state notifier. As that is not available,	 * signal it here.	 */ 	status = ssam_ctrl_notif_display_off(c);	if (status)		ssam_err(c, "pm: display-off notification failed: %d\n", status); 	return status;} static void ssam_serial_hub_pm_complete(struct device *dev){	struct ssam_controller *c = dev_get_drvdata(dev);	int status; 	/*	 * Try to signal display-on. This will restore events.	 *	 * Note: Signaling display-off/display-on should normally be done from	 * some sort of display state notifier. As that is not available,	 * signal it here.	 */ 	status = ssam_ctrl_notif_display_on(c);	if (status)		ssam_err(c, "pm: display-on notification failed: %d\n", status);} static int ssam_serial_hub_pm_suspend(struct device *dev){	struct ssam_controller *c = dev_get_drvdata(dev);	int status; 	/*	 * Try to signal D0-exit, enable IRQ wakeup if specified. Abort on	 * error.	 */ 	status = ssam_ctrl_notif_d0_exit(c);	if (status) {		ssam_err(c, "pm: D0-exit notification failed: %d\n", status);		goto err_notif;	} 	status = ssam_irq_arm_for_wakeup(c);	if (status)		goto err_irq; 	WARN_ON(ssam_controller_suspend(c));	return 0; err_irq:	ssam_ctrl_notif_d0_entry(c);err_notif:	ssam_ctrl_notif_display_on(c);	return status;} static int ssam_serial_hub_pm_resume(struct device *dev){	struct ssam_controller *c = dev_get_drvdata(dev);	int status; 	WARN_ON(ssam_controller_resume(c)); 	/*	 * Try to disable IRQ wakeup (if specified) and signal D0-entry. In	 * case of errors, log them and try to restore normal operation state	 * as far as possible.	 *	 * Note: Signaling display-off/display-on should normally be done from	 * some sort of display state notifier. As that is not available,	 * signal it here.	 */ 	ssam_irq_disarm_wakeup(c); 	status = ssam_ctrl_notif_d0_entry(c);

Lines 1–500 of 894.