// 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 <[email protected]> */ #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/pm.h> #include <linux/serdev.h> #include <linux/sysfs.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) { … } EXPORT_SYMBOL_GPL(…); /** * 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) { … } /** * 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) { … } /** * 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) { … } EXPORT_SYMBOL_GPL(…); /** * 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) { … } EXPORT_SYMBOL_GPL(…); /* -- Glue layer (serdev_device -> ssam_controller). ------------------------ */ static size_t ssam_receive_buf(struct serdev_device *dev, const u8 *buf, size_t n) { … } static void ssam_write_wakeup(struct serdev_device *dev) { … } static const struct serdev_device_ops ssam_serdev_ops = …; /* -- SysFS and misc. ------------------------------------------------------- */ static int ssam_log_firmware_version(struct ssam_controller *ctrl) { … } static ssize_t firmware_version_show(struct device *dev, struct device_attribute *attr, char *buf) { … } static DEVICE_ATTR_RO(firmware_version); static struct attribute *ssam_sam_attrs[] = …; static const struct attribute_group ssam_sam_group = …; /* -- ACPI based device setup. ---------------------------------------------- */ static acpi_status ssam_serdev_setup_via_acpi_crs(struct acpi_resource *rsc, void *ctx) { … } static acpi_status ssam_serdev_setup_via_acpi(acpi_handle handle, struct serdev_device *serdev) { … } /* -- Power management. ----------------------------------------------------- */ static void ssam_serial_hub_shutdown(struct device *dev) { … } #ifdef CONFIG_PM_SLEEP static int ssam_serial_hub_pm_prepare(struct device *dev) { … } static void ssam_serial_hub_pm_complete(struct device *dev) { … } static int ssam_serial_hub_pm_suspend(struct device *dev) { … } static int ssam_serial_hub_pm_resume(struct device *dev) { … } static int ssam_serial_hub_pm_freeze(struct device *dev) { … } static int ssam_serial_hub_pm_thaw(struct device *dev) { … } static int ssam_serial_hub_pm_poweroff(struct device *dev) { … } static int ssam_serial_hub_pm_restore(struct device *dev) { … } static const struct dev_pm_ops ssam_serial_hub_pm_ops = …; #else /* CONFIG_PM_SLEEP */ static const struct dev_pm_ops ssam_serial_hub_pm_ops = { }; #endif /* CONFIG_PM_SLEEP */ /* -- Device/driver setup. -------------------------------------------------- */ static const struct acpi_gpio_params gpio_ssam_wakeup_int = …; static const struct acpi_gpio_params gpio_ssam_wakeup = …; static const struct acpi_gpio_mapping ssam_acpi_gpios[] = …; static int ssam_serial_hub_probe(struct serdev_device *serdev) { … } static void ssam_serial_hub_remove(struct serdev_device *serdev) { … } static const struct acpi_device_id ssam_serial_hub_match[] = …; MODULE_DEVICE_TABLE(acpi, ssam_serial_hub_match); static struct serdev_device_driver ssam_serial_hub = …; /* -- Module setup. --------------------------------------------------------- */ static int __init ssam_core_init(void) { … } subsys_initcall(ssam_core_init); static void __exit ssam_core_exit(void) { … } module_exit(ssam_core_exit); MODULE_AUTHOR(…) …; MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;