2020-07-24 09:23:59 +00:00
|
|
|
From 0d17ea2d04af09c1689b6c892fc67b635b588423 Mon Sep 17 00:00:00 2001
|
2019-12-10 00:26:31 +00:00
|
|
|
From: qzed <qzed@users.noreply.github.com>
|
|
|
|
Date: Mon, 26 Aug 2019 01:11:08 +0200
|
2020-07-24 09:23:59 +00:00
|
|
|
Subject: [PATCH 3/5] surface-sam
|
2019-12-10 00:26:31 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
drivers/acpi/acpica/dsopcode.c | 2 +-
|
|
|
|
drivers/acpi/acpica/exfield.c | 12 +-
|
|
|
|
drivers/platform/x86/Kconfig | 1 +
|
|
|
|
drivers/platform/x86/Makefile | 1 +
|
2020-07-02 21:19:52 +00:00
|
|
|
drivers/platform/x86/surface_sam/Kconfig | 176 +
|
|
|
|
drivers/platform/x86/surface_sam/Makefile | 15 +
|
|
|
|
.../x86/surface_sam/surface_sam_dtx.c | 590 ++
|
2020-07-13 14:07:25 +00:00
|
|
|
.../x86/surface_sam/surface_sam_hps.c | 1297 +++++
|
2020-07-02 21:19:52 +00:00
|
|
|
.../x86/surface_sam/surface_sam_san.c | 913 +++
|
2020-05-16 22:20:04 +00:00
|
|
|
.../x86/surface_sam/surface_sam_san.h | 30 +
|
2020-07-15 20:26:02 +00:00
|
|
|
.../x86/surface_sam/surface_sam_sid.c | 281 +
|
2020-07-13 14:07:25 +00:00
|
|
|
.../x86/surface_sam/surface_sam_sid_gpelid.c | 232 +
|
2020-07-02 21:19:52 +00:00
|
|
|
.../surface_sam/surface_sam_sid_perfmode.c | 216 +
|
2020-07-13 14:07:25 +00:00
|
|
|
.../x86/surface_sam/surface_sam_sid_power.c | 1154 ++++
|
|
|
|
.../x86/surface_sam/surface_sam_sid_power.h | 15 +
|
|
|
|
.../x86/surface_sam/surface_sam_sid_vhf.c | 432 ++
|
|
|
|
.../x86/surface_sam/surface_sam_sid_vhf.h | 13 +
|
2020-07-15 20:26:02 +00:00
|
|
|
.../x86/surface_sam/surface_sam_ssh.c | 5111 +++++++++++++++++
|
2020-07-02 21:19:52 +00:00
|
|
|
.../x86/surface_sam/surface_sam_ssh.h | 488 ++
|
|
|
|
.../x86/surface_sam/surface_sam_ssh_trace.h | 536 ++
|
|
|
|
.../x86/surface_sam/surface_sam_vhf.c | 261 +
|
2019-12-10 00:26:31 +00:00
|
|
|
drivers/tty/serdev/core.c | 111 +-
|
2020-07-15 20:26:02 +00:00
|
|
|
22 files changed, 11871 insertions(+), 16 deletions(-)
|
2019-12-10 00:26:31 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/Kconfig
|
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/Makefile
|
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_dtx.c
|
2019-12-27 23:54:14 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_hps.c
|
2019-12-10 00:26:31 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_san.c
|
2019-12-27 23:54:14 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_san.h
|
2019-12-10 00:26:31 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid.c
|
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_gpelid.c
|
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_perfmode.c
|
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_power.c
|
2020-07-13 14:07:25 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_power.h
|
2019-12-10 00:26:31 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_vhf.c
|
2020-07-13 14:07:25 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_sid_vhf.h
|
2019-12-10 00:26:31 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_ssh.c
|
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_ssh.h
|
2020-07-02 21:19:52 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_ssh_trace.h
|
2019-12-10 00:26:31 +00:00
|
|
|
create mode 100644 drivers/platform/x86/surface_sam/surface_sam_vhf.c
|
|
|
|
|
|
|
|
diff --git a/drivers/acpi/acpica/dsopcode.c b/drivers/acpi/acpica/dsopcode.c
|
2020-07-24 09:23:59 +00:00
|
|
|
index 10f32b62608e..7b2a4987f050 100644
|
2019-12-10 00:26:31 +00:00
|
|
|
--- a/drivers/acpi/acpica/dsopcode.c
|
|
|
|
+++ b/drivers/acpi/acpica/dsopcode.c
|
|
|
|
@@ -123,7 +123,7 @@ acpi_ds_init_buffer_field(u16 aml_opcode,
|
|
|
|
|
|
|
|
/* Offset is in bits, count is in bits */
|
|
|
|
|
|
|
|
- field_flags = AML_FIELD_ACCESS_BYTE;
|
|
|
|
+ field_flags = AML_FIELD_ACCESS_BUFFER;
|
|
|
|
bit_offset = offset;
|
|
|
|
bit_count = (u32) length_desc->integer.value;
|
|
|
|
|
|
|
|
diff --git a/drivers/acpi/acpica/exfield.c b/drivers/acpi/acpica/exfield.c
|
2020-07-24 09:23:59 +00:00
|
|
|
index d3d2dbfba680..0b7f617a6e9b 100644
|
2019-12-10 00:26:31 +00:00
|
|
|
--- a/drivers/acpi/acpica/exfield.c
|
|
|
|
+++ b/drivers/acpi/acpica/exfield.c
|
|
|
|
@@ -109,6 +109,7 @@ acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
|
|
|
|
union acpi_operand_object *buffer_desc;
|
|
|
|
void *buffer;
|
|
|
|
u32 buffer_length;
|
|
|
|
+ u8 field_flags;
|
|
|
|
|
|
|
|
ACPI_FUNCTION_TRACE_PTR(ex_read_data_from_field, obj_desc);
|
|
|
|
|
|
|
|
@@ -157,11 +158,16 @@ acpi_ex_read_data_from_field(struct acpi_walk_state *walk_state,
|
|
|
|
* Note: Field.length is in bits.
|
|
|
|
*/
|
|
|
|
buffer_length =
|
|
|
|
- (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->field.bit_length);
|
|
|
|
+ (acpi_size)ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
|
|
|
|
+ field_flags = obj_desc->common_field.field_flags;
|
|
|
|
|
|
|
|
- if (buffer_length > acpi_gbl_integer_byte_width) {
|
|
|
|
+ if (buffer_length > acpi_gbl_integer_byte_width ||
|
|
|
|
+ (field_flags & AML_FIELD_ACCESS_TYPE_MASK) == AML_FIELD_ACCESS_BUFFER) {
|
|
|
|
|
|
|
|
- /* Field is too large for an Integer, create a Buffer instead */
|
|
|
|
+ /*
|
|
|
|
+ * Field is either too large for an Integer, or a actually of type
|
|
|
|
+ * buffer, so create a Buffer.
|
|
|
|
+ */
|
|
|
|
|
|
|
|
buffer_desc = acpi_ut_create_buffer_object(buffer_length);
|
|
|
|
if (!buffer_desc) {
|
|
|
|
diff --git a/drivers/platform/x86/Kconfig b/drivers/platform/x86/Kconfig
|
2020-07-24 09:23:59 +00:00
|
|
|
index 348c795019fa..d25067a838a3 100644
|
2019-12-10 00:26:31 +00:00
|
|
|
--- a/drivers/platform/x86/Kconfig
|
|
|
|
+++ b/drivers/platform/x86/Kconfig
|
2020-01-28 22:12:08 +00:00
|
|
|
@@ -1342,6 +1342,7 @@ config PCENGINES_APU2
|
2019-12-10 00:26:31 +00:00
|
|
|
will be called pcengines-apuv2.
|
|
|
|
|
|
|
|
source "drivers/platform/x86/intel_speed_select_if/Kconfig"
|
|
|
|
+source "drivers/platform/x86/surface_sam/Kconfig"
|
|
|
|
|
|
|
|
endif # X86_PLATFORM_DEVICES
|
|
|
|
|
|
|
|
diff --git a/drivers/platform/x86/Makefile b/drivers/platform/x86/Makefile
|
2020-07-24 09:23:59 +00:00
|
|
|
index 6dd955ad9bf1..19b56f2181eb 100644
|
2019-12-10 00:26:31 +00:00
|
|
|
--- a/drivers/platform/x86/Makefile
|
|
|
|
+++ b/drivers/platform/x86/Makefile
|
2020-01-28 22:12:08 +00:00
|
|
|
@@ -101,3 +101,4 @@ obj-$(CONFIG_I2C_MULTI_INSTANTIATE) += i2c-multi-instantiate.o
|
2019-12-10 00:26:31 +00:00
|
|
|
obj-$(CONFIG_INTEL_ATOMISP2_PM) += intel_atomisp2_pm.o
|
|
|
|
obj-$(CONFIG_PCENGINES_APU2) += pcengines-apuv2.o
|
|
|
|
obj-$(CONFIG_INTEL_SPEED_SELECT_INTERFACE) += intel_speed_select_if/
|
|
|
|
+obj-$(CONFIG_SURFACE_SAM) += surface_sam/
|
|
|
|
diff --git a/drivers/platform/x86/surface_sam/Kconfig b/drivers/platform/x86/surface_sam/Kconfig
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..7781c5cd932c
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/Kconfig
|
2020-07-02 21:19:52 +00:00
|
|
|
@@ -0,0 +1,176 @@
|
2019-12-10 00:26:31 +00:00
|
|
|
+menuconfig SURFACE_SAM
|
2020-05-16 22:20:04 +00:00
|
|
|
+ depends on ACPI
|
|
|
|
+ tristate "Microsoft Surface/System Aggregator Module and Platform Drivers"
|
|
|
|
+ help
|
|
|
|
+ Drivers for the Surface/System Aggregator Module (SAM) of Microsoft
|
|
|
|
+ Surface devices.
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ SAM is an embedded controller that provides access to various
|
|
|
|
+ functionalities on these devices, including battery status, keyboard
|
|
|
|
+ events (on the Laptops) and many more.
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ Say M/Y here if you have a Microsoft Surface device with a SAM device
|
|
|
|
+ (i.e. 5th generation or later).
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+config SURFACE_SAM_SSH
|
|
|
|
+ tristate "Surface Serial Hub Driver"
|
|
|
|
+ depends on SURFACE_SAM
|
|
|
|
+ depends on SERIAL_DEV_CTRL_TTYPORT
|
|
|
|
+ select CRC_CCITT
|
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-10 00:26:31 +00:00
|
|
|
+ Surface Serial Hub driver for 5th generation (or later) Microsoft
|
|
|
|
+ Surface devices.
|
|
|
|
+
|
|
|
|
+ This is the base driver for the embedded serial controller found on
|
|
|
|
+ 5th generation (and later) Microsoft Surface devices (e.g. Book 2,
|
|
|
|
+ Laptop, Laptop 2, Pro 2017, Pro 6, ...). This driver itself only
|
|
|
|
+ provides access to the embedded controller (SAM) and subsequent
|
|
|
|
+ drivers are required for the respective functionalities.
|
|
|
|
+
|
|
|
|
+ If you have a 5th generation (or later) Microsoft Surface device, say
|
|
|
|
+ Y or M here.
|
|
|
|
+
|
|
|
|
+config SURFACE_SAM_SSH_DEBUG_DEVICE
|
|
|
|
+ bool "Surface Serial Hub Debug Device"
|
|
|
|
+ depends on SURFACE_SAM_SSH
|
|
|
|
+ depends on SYSFS
|
|
|
|
+ default n
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-10 00:26:31 +00:00
|
|
|
+ Debug device for direct communication with the embedded controller
|
|
|
|
+ found on 5th generation (and later) Microsoft Surface devices (e.g.
|
|
|
|
+ Book 2, Laptop, Laptop 2, Pro 2017, Pro 6, ...) via sysfs.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say N here.
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+config SURFACE_SAM_SSH_ERROR_INJECTION
|
|
|
|
+ bool "Surface Serial Hub Error Injection Capabilities"
|
|
|
|
+ depends on SURFACE_SAM_SSH
|
|
|
|
+ depends on FUNCTION_ERROR_INJECTION
|
|
|
|
+ default n
|
|
|
|
+ help
|
|
|
|
+ Enable error injection capabilities for the Surface Serial Hub.
|
|
|
|
+ This is used to debug the driver, specifically the communication
|
|
|
|
+ interface. It is not required for normal use.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say N here.
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+config SURFACE_SAM_SAN
|
|
|
|
+ tristate "Surface ACPI Notify Driver"
|
|
|
|
+ depends on SURFACE_SAM_SSH
|
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-10 00:26:31 +00:00
|
|
|
+ Surface ACPI Notify driver for 5th generation (or later) Microsoft
|
|
|
|
+ Surface devices.
|
|
|
|
+
|
|
|
|
+ This driver enables basic ACPI events and requests, such as battery
|
|
|
|
+ status requests/events, thermal events, lid status, and possibly more,
|
|
|
|
+ which would otherwise not work on these devices.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say M here.
|
|
|
|
+
|
|
|
|
+config SURFACE_SAM_VHF
|
|
|
|
+ tristate "Surface Virtual HID Framework Driver"
|
|
|
|
+ depends on SURFACE_SAM_SSH
|
|
|
|
+ depends on HID
|
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-10 00:26:31 +00:00
|
|
|
+ Surface Virtual HID Framework driver for 5th generation (or later)
|
|
|
|
+ Microsoft Surface devices.
|
|
|
|
+
|
|
|
|
+ This driver provides support for the Microsoft Virtual HID framework,
|
|
|
|
+ which is required for keyboard support on the Surface Laptop 1 and 2.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say M here.
|
|
|
|
+
|
|
|
|
+config SURFACE_SAM_DTX
|
|
|
|
+ tristate "Surface Detachment System (DTX) Driver"
|
|
|
|
+ depends on SURFACE_SAM_SSH
|
|
|
|
+ depends on INPUT
|
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-10 00:26:31 +00:00
|
|
|
+ Surface Detachment System (DTX) driver for the Microsoft Surface Book
|
|
|
|
+ 2. This driver provides support for proper detachment handling in
|
|
|
|
+ user-space, status-events relating to the base and support for
|
|
|
|
+ the safe-guard keeping the base attached when the discrete GPU
|
|
|
|
+ contained in it is running via the special /dev/surface-dtx device.
|
|
|
|
+
|
|
|
|
+ Also provides a standard input device to provide SW_TABLET_MODE events
|
|
|
|
+ upon device mode change.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say M here.
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+config SURFACE_SAM_HPS
|
|
|
|
+ tristate "Surface dGPU Hot-Plug System (dGPU-HPS) Driver"
|
|
|
|
+ depends on SURFACE_SAM_SSH
|
|
|
|
+ depends on SURFACE_SAM_SAN
|
2020-05-16 22:20:04 +00:00
|
|
|
+ depends on GPIO_SYSFS
|
2019-12-27 23:54:14 +00:00
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-27 23:54:14 +00:00
|
|
|
+ Driver to properly handle hot-plugging and explicit power-on/power-off
|
2020-07-02 21:19:52 +00:00
|
|
|
+ of the discrete GPU (dGPU) on the Surface Book 2 and 3.
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+ If you are not sure, say M here.
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+config SURFACE_SAM_SID
|
|
|
|
+ tristate "Surface Platform Integration Driver"
|
|
|
|
+ depends on SURFACE_SAM_SSH
|
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-10 00:26:31 +00:00
|
|
|
+ Surface Platform Integration Driver for the Microsoft Surface Devices.
|
|
|
|
+ This driver loads various model-specific sub-drivers, including
|
|
|
|
+ battery and keyboard support on 7th generation Surface devices, proper
|
|
|
|
+ lid setup to enable device wakeup when the lid is opened on multiple
|
|
|
|
+ models, as well as performance mode setting support on the Surface
|
|
|
|
+ Book 2.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say M here.
|
|
|
|
+
|
|
|
|
+config SURFACE_SAM_SID_GPELID
|
|
|
|
+ tristate "Surface Lid Wakeup Driver"
|
|
|
|
+ depends on SURFACE_SAM_SID
|
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-10 00:26:31 +00:00
|
|
|
+ Driver to set up device wake-up via lid on Intel-based Microsoft
|
|
|
|
+ Surface devices. These devices do not wake up from sleep as their GPE
|
|
|
|
+ interrupt is not configured automatically. This driver solves that
|
|
|
|
+ problem.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say M here.
|
|
|
|
+
|
|
|
|
+config SURFACE_SAM_SID_PERFMODE
|
|
|
|
+ tristate "Surface Performance Mode Driver"
|
|
|
|
+ depends on SURFACE_SAM_SID
|
|
|
|
+ depends on SYSFS
|
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
|
|
|
+ This driver provides support for setting performance-modes on Surface
|
2019-12-10 00:26:31 +00:00
|
|
|
+ devices via the perf_mode sysfs attribute. Currently only supports the
|
|
|
|
+ Surface Book 2. Performance-modes directly influence the fan-profile
|
|
|
|
+ of the device, allowing to choose between higher performance or
|
|
|
|
+ quieter operation.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say M here.
|
|
|
|
+
|
|
|
|
+config SURFACE_SAM_SID_VHF
|
|
|
|
+ tristate "Surface SAM HID Driver"
|
|
|
|
+ depends on SURFACE_SAM_SID
|
|
|
|
+ depends on HID
|
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-10 00:26:31 +00:00
|
|
|
+ This driver provides support for HID devices connected via the Surface
|
|
|
|
+ SAM embedded controller. It provides support for keyboard and touchpad
|
|
|
|
+ on the Surface Laptop 3 models.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say M here.
|
|
|
|
+
|
|
|
|
+config SURFACE_SAM_SID_POWER
|
|
|
|
+ tristate "Surface SAM Battery/AC Driver"
|
|
|
|
+ depends on SURFACE_SAM_SID
|
|
|
|
+ select POWER_SUPPLY
|
|
|
|
+ default m
|
2020-05-16 22:20:04 +00:00
|
|
|
+ help
|
2019-12-10 00:26:31 +00:00
|
|
|
+ This driver provides support for the battery and AC on 7th generation
|
|
|
|
+ Surface devices.
|
|
|
|
+
|
|
|
|
+ If you are not sure, say M here.
|
|
|
|
diff --git a/drivers/platform/x86/surface_sam/Makefile b/drivers/platform/x86/surface_sam/Makefile
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..1a5c1260639d
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/Makefile
|
2020-07-02 21:19:52 +00:00
|
|
|
@@ -0,0 +1,15 @@
|
|
|
|
+# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
+
|
|
|
|
+# For include/trace/define_trace.h to include surface_sam_ssh_trace.h
|
|
|
|
+CFLAGS_surface_sam_ssh.o = -I$(src)
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+obj-$(CONFIG_SURFACE_SAM_SSH) += surface_sam_ssh.o
|
|
|
|
+obj-$(CONFIG_SURFACE_SAM_SAN) += surface_sam_san.o
|
|
|
|
+obj-$(CONFIG_SURFACE_SAM_DTX) += surface_sam_dtx.o
|
2019-12-27 23:54:14 +00:00
|
|
|
+obj-$(CONFIG_SURFACE_SAM_HPS) += surface_sam_hps.o
|
2019-12-10 00:26:31 +00:00
|
|
|
+obj-$(CONFIG_SURFACE_SAM_VHF) += surface_sam_vhf.o
|
|
|
|
+obj-$(CONFIG_SURFACE_SAM_SID) += surface_sam_sid.o
|
|
|
|
+obj-$(CONFIG_SURFACE_SAM_SID_GPELID) += surface_sam_sid_gpelid.o
|
|
|
|
+obj-$(CONFIG_SURFACE_SAM_SID_PERFMODE) += surface_sam_sid_perfmode.o
|
|
|
|
+obj-$(CONFIG_SURFACE_SAM_SID_POWER) += surface_sam_sid_power.o
|
|
|
|
+obj-$(CONFIG_SURFACE_SAM_SID_VHF) += surface_sam_sid_vhf.o
|
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_dtx.c b/drivers/platform/x86/surface_sam/surface_sam_dtx.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..88dba7bced3a
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_dtx.c
|
2020-07-02 21:19:52 +00:00
|
|
|
@@ -0,0 +1,590 @@
|
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
|
|
|
+ * Detachment system (DTX) driver for Microsoft Surface Book 2.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <linux/acpi.h>
|
|
|
|
+#include <linux/delay.h>
|
|
|
|
+#include <linux/fs.h>
|
|
|
|
+#include <linux/input.h>
|
|
|
|
+#include <linux/ioctl.h>
|
|
|
|
+#include <linux/kernel.h>
|
|
|
|
+#include <linux/miscdevice.h>
|
|
|
|
+#include <linux/module.h>
|
|
|
|
+#include <linux/poll.h>
|
|
|
|
+#include <linux/rculist.h>
|
|
|
|
+#include <linux/slab.h>
|
|
|
|
+#include <linux/spinlock.h>
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
+
|
|
|
|
+#include "surface_sam_ssh.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define USB_VENDOR_ID_MICROSOFT 0x045e
|
|
|
|
+#define USB_DEVICE_ID_MS_SURFACE_BASE_2_INTEGRATION 0x0922
|
|
|
|
+
|
|
|
|
+// name copied from MS device manager
|
|
|
|
+#define DTX_INPUT_NAME "Microsoft Surface Base 2 Integration Device"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define DTX_CMD_LATCH_LOCK _IO(0x11, 0x01)
|
|
|
|
+#define DTX_CMD_LATCH_UNLOCK _IO(0x11, 0x02)
|
|
|
|
+#define DTX_CMD_LATCH_REQUEST _IO(0x11, 0x03)
|
|
|
|
+#define DTX_CMD_LATCH_OPEN _IO(0x11, 0x04)
|
|
|
|
+#define DTX_CMD_GET_OPMODE _IOR(0x11, 0x05, int)
|
|
|
|
+
|
|
|
|
+#define SAM_RQST_DTX_TC 0x11
|
|
|
|
+#define SAM_RQST_DTX_CID_LATCH_LOCK 0x06
|
|
|
|
+#define SAM_RQST_DTX_CID_LATCH_UNLOCK 0x07
|
|
|
|
+#define SAM_RQST_DTX_CID_LATCH_REQUEST 0x08
|
|
|
|
+#define SAM_RQST_DTX_CID_LATCH_OPEN 0x09
|
|
|
|
+#define SAM_RQST_DTX_CID_GET_OPMODE 0x0D
|
|
|
|
+
|
|
|
|
+#define SAM_EVENT_DTX_CID_CONNECTION 0x0c
|
|
|
|
+#define SAM_EVENT_DTX_CID_BUTTON 0x0e
|
|
|
|
+#define SAM_EVENT_DTX_CID_ERROR 0x0f
|
|
|
|
+#define SAM_EVENT_DTX_CID_LATCH_STATUS 0x11
|
|
|
|
+
|
|
|
|
+#define DTX_OPMODE_TABLET 0x00
|
|
|
|
+#define DTX_OPMODE_LAPTOP 0x01
|
|
|
|
+#define DTX_OPMODE_STUDIO 0x02
|
|
|
|
+
|
|
|
|
+#define DTX_LATCH_CLOSED 0x00
|
|
|
|
+#define DTX_LATCH_OPENED 0x01
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// Warning: This must always be a power of 2!
|
|
|
|
+#define DTX_CLIENT_BUF_SIZE 16
|
|
|
|
+
|
|
|
|
+#define DTX_CONNECT_OPMODE_DELAY 1000
|
|
|
|
+
|
|
|
|
+#define DTX_ERR KERN_ERR "surface_sam_dtx: "
|
|
|
|
+#define DTX_WARN KERN_WARNING "surface_sam_dtx: "
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct surface_dtx_event {
|
|
|
|
+ u8 type;
|
|
|
|
+ u8 code;
|
|
|
|
+ u8 arg0;
|
|
|
|
+ u8 arg1;
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+struct surface_dtx_dev {
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct ssam_event_notifier notif;
|
|
|
|
+ struct delayed_work opmode_work;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ wait_queue_head_t waitq;
|
|
|
|
+ struct miscdevice mdev;
|
|
|
|
+ spinlock_t client_lock;
|
|
|
|
+ struct list_head client_list;
|
|
|
|
+ struct mutex mutex;
|
|
|
|
+ bool active;
|
|
|
|
+ spinlock_t input_lock;
|
|
|
|
+ struct input_dev *input_dev;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct surface_dtx_client {
|
|
|
|
+ struct list_head node;
|
|
|
|
+ struct surface_dtx_dev *ddev;
|
|
|
|
+ struct fasync_struct *fasync;
|
|
|
|
+ spinlock_t buffer_lock;
|
|
|
|
+ unsigned int buffer_head;
|
|
|
|
+ unsigned int buffer_tail;
|
|
|
|
+ struct surface_dtx_event buffer[DTX_CLIENT_BUF_SIZE];
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static struct surface_dtx_dev surface_dtx_dev;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_sam_query_opmpde(void)
|
|
|
|
+{
|
|
|
|
+ u8 result_buf[1];
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
|
|
|
+ .tc = SAM_RQST_DTX_TC,
|
|
|
|
+ .cid = SAM_RQST_DTX_CID_GET_OPMODE,
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .iid = 0x00,
|
|
|
|
+ .chn = 0x01,
|
|
|
|
+ .snc = 0x01,
|
|
|
|
+ .cdl = 0x00,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .pld = NULL,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_buf result = {
|
|
|
|
+ .cap = 1,
|
|
|
|
+ .len = 0,
|
|
|
|
+ .data = result_buf,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_rqst(&rqst, &result);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (result.len != 1)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -EFAULT;
|
|
|
|
+
|
|
|
|
+ return result.data[0];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int dtx_cmd_simple(u8 cid)
|
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
|
|
|
+ .tc = SAM_RQST_DTX_TC,
|
|
|
|
+ .cid = cid,
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .iid = 0x00,
|
|
|
|
+ .chn = 0x01,
|
|
|
|
+ .snc = 0x00,
|
|
|
|
+ .cdl = 0x00,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .pld = NULL,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, NULL);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int dtx_cmd_get_opmode(int __user *buf)
|
|
|
|
+{
|
2020-05-16 22:20:04 +00:00
|
|
|
+ int opmode;
|
|
|
|
+
|
|
|
|
+ opmode = surface_sam_query_opmpde();
|
|
|
|
+ if (opmode < 0)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return opmode;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (put_user(opmode, buf))
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -EACCES;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_dtx_open(struct inode *inode, struct file *file)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_dev *ddev = container_of(file->private_data, struct surface_dtx_dev, mdev);
|
|
|
|
+ struct surface_dtx_client *client;
|
|
|
|
+
|
|
|
|
+ // initialize client
|
|
|
|
+ client = kzalloc(sizeof(struct surface_dtx_client), GFP_KERNEL);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!client)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -ENOMEM;
|
|
|
|
+
|
|
|
|
+ spin_lock_init(&client->buffer_lock);
|
|
|
|
+ client->buffer_head = 0;
|
|
|
|
+ client->buffer_tail = 0;
|
|
|
|
+ client->ddev = ddev;
|
|
|
|
+
|
|
|
|
+ // attach client
|
|
|
|
+ spin_lock(&ddev->client_lock);
|
|
|
|
+ list_add_tail_rcu(&client->node, &ddev->client_list);
|
|
|
|
+ spin_unlock(&ddev->client_lock);
|
|
|
|
+
|
|
|
|
+ file->private_data = client;
|
|
|
|
+ nonseekable_open(inode, file);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_dtx_release(struct inode *inode, struct file *file)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_client *client = file->private_data;
|
|
|
|
+
|
|
|
|
+ // detach client
|
|
|
|
+ spin_lock(&client->ddev->client_lock);
|
|
|
|
+ list_del_rcu(&client->node);
|
|
|
|
+ spin_unlock(&client->ddev->client_lock);
|
|
|
|
+ synchronize_rcu();
|
|
|
|
+
|
|
|
|
+ kfree(client);
|
|
|
|
+ file->private_data = NULL;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static ssize_t surface_dtx_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_client *client = file->private_data;
|
|
|
|
+ struct surface_dtx_dev *ddev = client->ddev;
|
|
|
|
+ struct surface_dtx_event event;
|
|
|
|
+ size_t read = 0;
|
|
|
|
+ int status = 0;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (count != 0 && count < sizeof(struct surface_dtx_event))
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!ddev->active)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
|
|
|
+ // check availability
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (client->buffer_head == client->buffer_tail) {
|
|
|
|
+ if (file->f_flags & O_NONBLOCK)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -EAGAIN;
|
|
|
|
+
|
|
|
|
+ status = wait_event_interruptible(ddev->waitq,
|
|
|
|
+ client->buffer_head != client->buffer_tail ||
|
|
|
|
+ !ddev->active);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!ddev->active)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -ENODEV;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // copy events one by one
|
|
|
|
+ while (read + sizeof(struct surface_dtx_event) <= count) {
|
|
|
|
+ spin_lock_irq(&client->buffer_lock);
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (client->buffer_head == client->buffer_tail) {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ spin_unlock_irq(&client->buffer_lock);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // get one event
|
|
|
|
+ event = client->buffer[client->buffer_tail];
|
|
|
|
+ client->buffer_tail = (client->buffer_tail + 1) & (DTX_CLIENT_BUF_SIZE - 1);
|
|
|
|
+ spin_unlock_irq(&client->buffer_lock);
|
|
|
|
+
|
|
|
|
+ // copy to userspace
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (copy_to_user(buf, &event, sizeof(struct surface_dtx_event)))
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -EFAULT;
|
|
|
|
+
|
|
|
|
+ read += sizeof(struct surface_dtx_event);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return read;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static __poll_t surface_dtx_poll(struct file *file, struct poll_table_struct *pt)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_client *client = file->private_data;
|
|
|
|
+ int mask;
|
|
|
|
+
|
|
|
|
+ poll_wait(file, &client->ddev->waitq, pt);
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (client->ddev->active)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ mask = EPOLLOUT | EPOLLWRNORM;
|
2020-05-16 22:20:04 +00:00
|
|
|
+ else
|
2019-12-10 00:26:31 +00:00
|
|
|
+ mask = EPOLLHUP | EPOLLERR;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (client->buffer_head != client->buffer_tail)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ mask |= EPOLLIN | EPOLLRDNORM;
|
|
|
|
+
|
|
|
|
+ return mask;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_dtx_fasync(int fd, struct file *file, int on)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_client *client = file->private_data;
|
|
|
|
+
|
|
|
|
+ return fasync_helper(fd, file, on, &client->fasync);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static long surface_dtx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_client *client = file->private_data;
|
|
|
|
+ struct surface_dtx_dev *ddev = client->ddev;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = mutex_lock_interruptible(&ddev->mutex);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ if (!ddev->active) {
|
|
|
|
+ mutex_unlock(&ddev->mutex);
|
|
|
|
+ return -ENODEV;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ switch (cmd) {
|
|
|
|
+ case DTX_CMD_LATCH_LOCK:
|
|
|
|
+ status = dtx_cmd_simple(SAM_RQST_DTX_CID_LATCH_LOCK);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case DTX_CMD_LATCH_UNLOCK:
|
|
|
|
+ status = dtx_cmd_simple(SAM_RQST_DTX_CID_LATCH_UNLOCK);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case DTX_CMD_LATCH_REQUEST:
|
|
|
|
+ status = dtx_cmd_simple(SAM_RQST_DTX_CID_LATCH_REQUEST);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case DTX_CMD_LATCH_OPEN:
|
|
|
|
+ status = dtx_cmd_simple(SAM_RQST_DTX_CID_LATCH_OPEN);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case DTX_CMD_GET_OPMODE:
|
|
|
|
+ status = dtx_cmd_get_opmode((int __user *)arg);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ status = -EINVAL;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ mutex_unlock(&ddev->mutex);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static const struct file_operations surface_dtx_fops = {
|
|
|
|
+ .owner = THIS_MODULE,
|
|
|
|
+ .open = surface_dtx_open,
|
|
|
|
+ .release = surface_dtx_release,
|
|
|
|
+ .read = surface_dtx_read,
|
|
|
|
+ .poll = surface_dtx_poll,
|
|
|
|
+ .fasync = surface_dtx_fasync,
|
|
|
|
+ .unlocked_ioctl = surface_dtx_ioctl,
|
|
|
|
+ .llseek = no_llseek,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static struct surface_dtx_dev surface_dtx_dev = {
|
|
|
|
+ .mdev = {
|
|
|
|
+ .minor = MISC_DYNAMIC_MINOR,
|
|
|
|
+ .name = "surface_dtx",
|
|
|
|
+ .fops = &surface_dtx_fops,
|
|
|
|
+ },
|
|
|
|
+ .client_lock = __SPIN_LOCK_UNLOCKED(),
|
|
|
|
+ .input_lock = __SPIN_LOCK_UNLOCKED(),
|
|
|
|
+ .mutex = __MUTEX_INITIALIZER(surface_dtx_dev.mutex),
|
|
|
|
+ .active = false,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void surface_dtx_push_event(struct surface_dtx_dev *ddev, struct surface_dtx_event *event)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_client *client;
|
|
|
|
+
|
|
|
|
+ rcu_read_lock();
|
|
|
|
+ list_for_each_entry_rcu(client, &ddev->client_list, node) {
|
|
|
|
+ spin_lock(&client->buffer_lock);
|
|
|
|
+
|
|
|
|
+ client->buffer[client->buffer_head++] = *event;
|
|
|
|
+ client->buffer_head &= DTX_CLIENT_BUF_SIZE - 1;
|
|
|
|
+
|
|
|
|
+ if (unlikely(client->buffer_head == client->buffer_tail)) {
|
|
|
|
+ printk(DTX_WARN "event buffer overrun\n");
|
|
|
|
+ client->buffer_tail = (client->buffer_tail + 1) & (DTX_CLIENT_BUF_SIZE - 1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ spin_unlock(&client->buffer_lock);
|
|
|
|
+
|
|
|
|
+ kill_fasync(&client->fasync, SIGIO, POLL_IN);
|
|
|
|
+ }
|
|
|
|
+ rcu_read_unlock();
|
|
|
|
+
|
|
|
|
+ wake_up_interruptible(&ddev->waitq);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void surface_dtx_update_opmpde(struct surface_dtx_dev *ddev)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_event event;
|
|
|
|
+ int opmode;
|
|
|
|
+
|
|
|
|
+ // get operation mode
|
|
|
|
+ opmode = surface_sam_query_opmpde();
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (opmode < 0)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ printk(DTX_ERR "EC request failed with error %d\n", opmode);
|
|
|
|
+
|
|
|
|
+ // send DTX event
|
|
|
|
+ event.type = 0x11;
|
|
|
|
+ event.code = 0x0D;
|
|
|
|
+ event.arg0 = opmode;
|
|
|
|
+ event.arg1 = 0x00;
|
|
|
|
+
|
|
|
|
+ surface_dtx_push_event(ddev, &event);
|
|
|
|
+
|
|
|
|
+ // send SW_TABLET_MODE event
|
|
|
|
+ spin_lock(&ddev->input_lock);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ input_report_switch(ddev->input_dev, SW_TABLET_MODE, opmode != DTX_OPMODE_LAPTOP);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ input_sync(ddev->input_dev);
|
|
|
|
+ spin_unlock(&ddev->input_lock);
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void surface_dtx_opmode_workfn(struct work_struct *work)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_dev *ddev = container_of(work, struct surface_dtx_dev, opmode_work.work);
|
|
|
|
+
|
|
|
|
+ surface_dtx_update_opmpde(ddev);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static u32 surface_dtx_notification(struct ssam_notifier_block *nb, const struct ssam_event *in_event)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct surface_dtx_dev *ddev = container_of(nb, struct surface_dtx_dev, notif.base);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ struct surface_dtx_event event;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ unsigned long delay;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ switch (in_event->command_id) {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ case SAM_EVENT_DTX_CID_CONNECTION:
|
|
|
|
+ case SAM_EVENT_DTX_CID_BUTTON:
|
|
|
|
+ case SAM_EVENT_DTX_CID_ERROR:
|
|
|
|
+ case SAM_EVENT_DTX_CID_LATCH_STATUS:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (in_event->length > 2) {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ printk(DTX_ERR "unexpected payload size (cid: %x, len: %u)\n",
|
2020-07-02 21:19:52 +00:00
|
|
|
+ in_event->command_id, in_event->length);
|
|
|
|
+ return SSAM_NOTIF_HANDLED;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ event.type = in_event->target_category;
|
|
|
|
+ event.code = in_event->command_id;
|
|
|
|
+ event.arg0 = in_event->length >= 1 ? in_event->data[0] : 0x00;
|
|
|
|
+ event.arg1 = in_event->length >= 2 ? in_event->data[1] : 0x00;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ surface_dtx_push_event(ddev, &event);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // update device mode
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (in_event->command_id == SAM_EVENT_DTX_CID_CONNECTION) {
|
|
|
|
+ delay = event.arg0 ? DTX_CONNECT_OPMODE_DELAY : 0;
|
|
|
|
+ schedule_delayed_work(&ddev->opmode_work, delay);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return SSAM_NOTIF_HANDLED;
|
2019-12-27 23:54:14 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static struct input_dev *surface_dtx_register_inputdev(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct input_dev *input_dev;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ input_dev = input_allocate_device();
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!input_dev)
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return ERR_PTR(-ENOMEM);
|
|
|
|
+
|
|
|
|
+ input_dev->name = DTX_INPUT_NAME;
|
|
|
|
+ input_dev->dev.parent = &pdev->dev;
|
|
|
|
+ input_dev->id.bustype = BUS_VIRTUAL;
|
|
|
|
+ input_dev->id.vendor = USB_VENDOR_ID_MICROSOFT;
|
|
|
|
+ input_dev->id.product = USB_DEVICE_ID_MS_SURFACE_BASE_2_INTEGRATION;
|
|
|
|
+
|
|
|
|
+ input_set_capability(input_dev, EV_SW, SW_TABLET_MODE);
|
|
|
|
+
|
|
|
|
+ status = surface_sam_query_opmpde();
|
|
|
|
+ if (status < 0) {
|
|
|
|
+ input_free_device(input_dev);
|
|
|
|
+ return ERR_PTR(status);
|
|
|
|
+ }
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ input_report_switch(input_dev, SW_TABLET_MODE, status != DTX_OPMODE_LAPTOP);
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+ status = input_register_device(input_dev);
|
|
|
|
+ if (status) {
|
|
|
|
+ input_unregister_device(input_dev);
|
|
|
|
+ return ERR_PTR(status);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return input_dev;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_sam_dtx_probe(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_dev *ddev = &surface_dtx_dev;
|
|
|
|
+ struct input_dev *input_dev;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // link to ec
|
|
|
|
+ status = surface_sam_ssh_consumer_register(&pdev->dev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return status == -ENXIO ? -EPROBE_DEFER : status;
|
|
|
|
+
|
|
|
|
+ input_dev = surface_dtx_register_inputdev(pdev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (IS_ERR(input_dev))
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return PTR_ERR(input_dev);
|
|
|
|
+
|
|
|
|
+ // initialize device
|
|
|
|
+ mutex_lock(&ddev->mutex);
|
|
|
|
+ if (ddev->active) {
|
|
|
|
+ mutex_unlock(&ddev->mutex);
|
|
|
|
+ status = -ENODEV;
|
|
|
|
+ goto err_register;
|
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ INIT_DELAYED_WORK(&ddev->opmode_work, surface_dtx_opmode_workfn);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ INIT_LIST_HEAD(&ddev->client_list);
|
|
|
|
+ init_waitqueue_head(&ddev->waitq);
|
|
|
|
+ ddev->active = true;
|
|
|
|
+ ddev->input_dev = input_dev;
|
|
|
|
+ mutex_unlock(&ddev->mutex);
|
|
|
|
+
|
|
|
|
+ status = misc_register(&ddev->mdev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-27 23:54:14 +00:00
|
|
|
+ goto err_register;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // set up events
|
|
|
|
+ ddev->notif.base.priority = 1;
|
|
|
|
+ ddev->notif.base.fn = surface_dtx_notification;
|
|
|
|
+ ddev->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
|
|
|
|
+ ddev->notif.event.id.target_category = SSAM_SSH_TC_BAS;
|
|
|
|
+ ddev->notif.event.id.instance = 0;
|
|
|
|
+ ddev->notif.event.flags = SSAM_EVENT_SEQUENCED;
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_notifier_register(&ddev->notif);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-27 23:54:14 +00:00
|
|
|
+ goto err_events_setup;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+err_events_setup:
|
|
|
|
+ misc_deregister(&ddev->mdev);
|
|
|
|
+err_register:
|
|
|
|
+ input_unregister_device(ddev->input_dev);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_dtx_remove(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct surface_dtx_dev *ddev = &surface_dtx_dev;
|
|
|
|
+ struct surface_dtx_client *client;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&ddev->mutex);
|
|
|
|
+ if (!ddev->active) {
|
|
|
|
+ mutex_unlock(&ddev->mutex);
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // mark as inactive
|
|
|
|
+ ddev->active = false;
|
|
|
|
+ mutex_unlock(&ddev->mutex);
|
|
|
|
+
|
|
|
|
+ // After this call we're guaranteed that no more input events will arive
|
2020-07-02 21:19:52 +00:00
|
|
|
+ surface_sam_ssh_notifier_unregister(&ddev->notif);
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+ // wake up clients
|
|
|
|
+ spin_lock(&ddev->client_lock);
|
|
|
|
+ list_for_each_entry(client, &ddev->client_list, node) {
|
|
|
|
+ kill_fasync(&client->fasync, SIGIO, POLL_HUP);
|
|
|
|
+ }
|
|
|
|
+ spin_unlock(&ddev->client_lock);
|
|
|
|
+
|
|
|
|
+ wake_up_interruptible(&ddev->waitq);
|
|
|
|
+
|
|
|
|
+ // unregister user-space devices
|
|
|
|
+ input_unregister_device(ddev->input_dev);
|
|
|
|
+ misc_deregister(&ddev->mdev);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static const struct acpi_device_id surface_sam_dtx_match[] = {
|
|
|
|
+ { "MSHW0133", 0 },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+MODULE_DEVICE_TABLE(acpi, surface_sam_dtx_match);
|
|
|
|
+
|
|
|
|
+static struct platform_driver surface_sam_dtx = {
|
|
|
|
+ .probe = surface_sam_dtx_probe,
|
|
|
|
+ .remove = surface_sam_dtx_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_dtx",
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .acpi_match_table = surface_sam_dtx_match,
|
2019-12-27 23:54:14 +00:00
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+module_platform_driver(surface_sam_dtx);
|
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
|
|
|
|
+MODULE_DESCRIPTION("Surface Detachment System (DTX) Driver for 5th Generation Surface Devices");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-27 23:54:14 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_hps.c b/drivers/platform/x86/surface_sam/surface_sam_hps.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..40f39f29113c
|
2019-12-27 23:54:14 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_hps.c
|
2020-07-13 14:07:25 +00:00
|
|
|
@@ -0,0 +1,1297 @@
|
2020-07-02 21:19:52 +00:00
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-27 23:54:14 +00:00
|
|
|
+/*
|
|
|
|
+ * Surface dGPU hot-plug system driver.
|
|
|
|
+ * Supports explicit setting of the dGPU power-state on the Surface Book 2 and
|
|
|
|
+ * properly handles hot-plugging by detaching the base.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <linux/acpi.h>
|
|
|
|
+#include <linux/delay.h>
|
|
|
|
+#include <linux/gpio.h>
|
|
|
|
+#include <linux/kernel.h>
|
|
|
|
+#include <linux/module.h>
|
|
|
|
+#include <linux/mutex.h>
|
|
|
|
+#include <linux/pci.h>
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
+#include <linux/sysfs.h>
|
|
|
|
+
|
|
|
|
+#include "surface_sam_ssh.h"
|
|
|
|
+#include "surface_sam_san.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// TODO: vgaswitcheroo integration
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void dbg_dump_drvsta(struct platform_device *pdev, const char *prefix);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define SHPS_DSM_REVISION 1
|
|
|
|
+#define SHPS_DSM_GPU_ADDRS 0x02
|
|
|
|
+#define SHPS_DSM_GPU_POWER 0x05
|
|
|
|
+static const guid_t SHPS_DSM_UUID =
|
|
|
|
+ GUID_INIT(0x5515a847, 0xed55, 0x4b27, 0x83, 0x52, 0xcd,
|
|
|
|
+ 0x32, 0x0e, 0x10, 0x36, 0x0a);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define SAM_DGPU_TC 0x13
|
|
|
|
+#define SAM_DGPU_CID_POWERON 0x02
|
|
|
|
+
|
|
|
|
+#define SAM_DTX_TC 0x11
|
|
|
|
+#define SAM_DTX_CID_LATCH_LOCK 0x06
|
|
|
|
+#define SAM_DTX_CID_LATCH_UNLOCK 0x07
|
2020-07-02 21:19:52 +00:00
|
|
|
+#define ACPI_SGCP_NOTIFY_POWER_ON 0x81
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+#define SHPS_DSM_GPU_ADDRS_RP "RP5_PCIE"
|
|
|
|
+#define SHPS_DSM_GPU_ADDRS_DGPU "DGPU_PCIE"
|
2020-07-02 21:19:52 +00:00
|
|
|
+#define SHPS_PCI_GPU_ADDR_RP "\\_SB.PCI0.RP13._ADR"
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+static const struct acpi_gpio_params gpio_base_presence_int = { 0, 0, false };
|
|
|
|
+static const struct acpi_gpio_params gpio_base_presence = { 1, 0, false };
|
|
|
|
+static const struct acpi_gpio_params gpio_dgpu_power_int = { 2, 0, false };
|
|
|
|
+static const struct acpi_gpio_params gpio_dgpu_power = { 3, 0, false };
|
|
|
|
+static const struct acpi_gpio_params gpio_dgpu_presence_int = { 4, 0, false };
|
|
|
|
+static const struct acpi_gpio_params gpio_dgpu_presence = { 5, 0, false };
|
|
|
|
+
|
|
|
|
+static const struct acpi_gpio_mapping shps_acpi_gpios[] = {
|
|
|
|
+ { "base_presence-int-gpio", &gpio_base_presence_int, 1 },
|
|
|
|
+ { "base_presence-gpio", &gpio_base_presence, 1 },
|
|
|
|
+ { "dgpu_power-int-gpio", &gpio_dgpu_power_int, 1 },
|
|
|
|
+ { "dgpu_power-gpio", &gpio_dgpu_power, 1 },
|
|
|
|
+ { "dgpu_presence-int-gpio", &gpio_dgpu_presence_int, 1 },
|
|
|
|
+ { "dgpu_presence-gpio", &gpio_dgpu_presence, 1 },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+enum shps_dgpu_power {
|
|
|
|
+ SHPS_DGPU_POWER_OFF = 0,
|
|
|
|
+ SHPS_DGPU_POWER_ON = 1,
|
|
|
|
+ SHPS_DGPU_POWER_UNKNOWN = 2,
|
|
|
|
+};
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static const char *shps_dgpu_power_str(enum shps_dgpu_power power)
|
|
|
|
+{
|
2019-12-27 23:54:14 +00:00
|
|
|
+ if (power == SHPS_DGPU_POWER_OFF)
|
|
|
|
+ return "off";
|
|
|
|
+ else if (power == SHPS_DGPU_POWER_ON)
|
|
|
|
+ return "on";
|
|
|
|
+ else if (power == SHPS_DGPU_POWER_UNKNOWN)
|
|
|
|
+ return "unknown";
|
|
|
|
+ else
|
|
|
|
+ return "<invalid>";
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+enum shps_notification_method {
|
|
|
|
+ SHPS_NOTIFICATION_METHOD_SAN = 1,
|
|
|
|
+ SHPS_NOTIFICATION_METHOD_SGCP = 2
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct shps_hardware_traits {
|
|
|
|
+ enum shps_notification_method notification_method;
|
|
|
|
+ const char *dgpu_rp_pci_address;
|
|
|
|
+};
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+struct shps_driver_data {
|
|
|
|
+ struct mutex lock;
|
|
|
|
+ struct pci_dev *dgpu_root_port;
|
|
|
|
+ struct pci_saved_state *dgpu_root_port_state;
|
|
|
|
+ struct gpio_desc *gpio_dgpu_power;
|
|
|
|
+ struct gpio_desc *gpio_dgpu_presence;
|
|
|
|
+ struct gpio_desc *gpio_base_presence;
|
|
|
|
+ unsigned int irq_dgpu_presence;
|
|
|
|
+ unsigned int irq_base_presence;
|
|
|
|
+ unsigned long state;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ acpi_handle sgpc_handle;
|
|
|
|
+ struct shps_hardware_traits hardware_traits;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct shps_hardware_probe {
|
|
|
|
+ const char *hardware_id;
|
|
|
|
+ int generation;
|
|
|
|
+ struct shps_hardware_traits *hardware_traits;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static struct shps_hardware_traits shps_gen1_hwtraits = {
|
|
|
|
+ .notification_method = SHPS_NOTIFICATION_METHOD_SAN
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static struct shps_hardware_traits shps_gen2_hwtraits = {
|
|
|
|
+ .notification_method = SHPS_NOTIFICATION_METHOD_SGCP,
|
|
|
|
+ .dgpu_rp_pci_address = SHPS_PCI_GPU_ADDR_RP
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct shps_hardware_probe shps_hardware_probe_match[] = {
|
|
|
|
+ /* Surface Book 3 */
|
|
|
|
+ { "MSHW0117", 2, &shps_gen2_hwtraits },
|
|
|
|
+
|
|
|
|
+ /* Surface Book 2 (default, must be last entry) */
|
|
|
|
+ { NULL, 1, &shps_gen1_hwtraits }
|
2019-12-27 23:54:14 +00:00
|
|
|
+};
|
|
|
|
+
|
|
|
|
+#define SHPS_STATE_BIT_PWRTGT 0 /* desired power state: 1 for on, 0 for off */
|
|
|
|
+#define SHPS_STATE_BIT_RPPWRON_SYNC 1 /* synchronous/requested power-up in progress */
|
|
|
|
+#define SHPS_STATE_BIT_WAKE_ENABLED 2 /* wakeup via base-presence GPIO enabled */
|
|
|
|
+
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+#define SHPS_DGPU_PARAM_PERM 0644
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+enum shps_dgpu_power_mp {
|
|
|
|
+ SHPS_DGPU_MP_POWER_OFF = SHPS_DGPU_POWER_OFF,
|
|
|
|
+ SHPS_DGPU_MP_POWER_ON = SHPS_DGPU_POWER_ON,
|
|
|
|
+ SHPS_DGPU_MP_POWER_ASIS = -1,
|
|
|
|
+
|
|
|
|
+ __SHPS_DGPU_MP_POWER_START = -1,
|
|
|
|
+ __SHPS_DGPU_MP_POWER_END = 1,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static int param_dgpu_power_set(const char *val, const struct kernel_param *kp)
|
|
|
|
+{
|
|
|
|
+ int power = SHPS_DGPU_MP_POWER_OFF;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = kstrtoint(val, 0, &power);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (power < __SHPS_DGPU_MP_POWER_START || power > __SHPS_DGPU_MP_POWER_END)
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ return param_set_int(val, kp);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static const struct kernel_param_ops param_dgpu_power_ops = {
|
|
|
|
+ .set = param_dgpu_power_set,
|
|
|
|
+ .get = param_get_int,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static int param_dgpu_power_init = SHPS_DGPU_MP_POWER_OFF;
|
|
|
|
+static int param_dgpu_power_exit = SHPS_DGPU_MP_POWER_ON;
|
|
|
|
+static int param_dgpu_power_susp = SHPS_DGPU_MP_POWER_ASIS;
|
|
|
|
+static bool param_dtx_latch = true;
|
|
|
|
+
|
|
|
|
+module_param_cb(dgpu_power_init, ¶m_dgpu_power_ops, ¶m_dgpu_power_init, SHPS_DGPU_PARAM_PERM);
|
|
|
|
+module_param_cb(dgpu_power_exit, ¶m_dgpu_power_ops, ¶m_dgpu_power_exit, SHPS_DGPU_PARAM_PERM);
|
|
|
|
+module_param_cb(dgpu_power_susp, ¶m_dgpu_power_ops, ¶m_dgpu_power_susp, SHPS_DGPU_PARAM_PERM);
|
|
|
|
+module_param_named(dtx_latch, param_dtx_latch, bool, SHPS_DGPU_PARAM_PERM);
|
|
|
|
+
|
|
|
|
+MODULE_PARM_DESC(dgpu_power_init, "dGPU power state to be set on init (0: off / 1: on / 2: as-is, default: off)");
|
|
|
|
+MODULE_PARM_DESC(dgpu_power_exit, "dGPU power state to be set on exit (0: off / 1: on / 2: as-is, default: on)");
|
|
|
|
+MODULE_PARM_DESC(dgpu_power_susp, "dGPU power state to be set on exit (0: off / 1: on / 2: as-is, default: as-is)");
|
|
|
|
+MODULE_PARM_DESC(dtx_latch, "lock/unlock DTX base latch in accordance to power-state (Y/n)");
|
|
|
|
+
|
|
|
|
+static int dtx_cmd_simple(u8 cid)
|
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
|
|
|
+ .tc = SAM_DTX_TC,
|
|
|
|
+ .cid = cid,
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .iid = 0x00,
|
|
|
|
+ .chn = 0x01,
|
|
|
|
+ .snc = 0x00,
|
|
|
|
+ .cdl = 0x00,
|
2019-12-27 23:54:14 +00:00
|
|
|
+ .pld = NULL,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, NULL);
|
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int shps_dtx_latch_lock(void)
|
2019-12-27 23:54:14 +00:00
|
|
|
+{
|
|
|
|
+ return dtx_cmd_simple(SAM_DTX_CID_LATCH_LOCK);
|
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int shps_dtx_latch_unlock(void)
|
2019-12-27 23:54:14 +00:00
|
|
|
+{
|
|
|
|
+ return dtx_cmd_simple(SAM_DTX_CID_LATCH_UNLOCK);
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int shps_dgpu_dsm_get_pci_addr_from_adr(struct platform_device *pdev, const char *entry) {
|
|
|
|
+ acpi_handle handle = ACPI_HANDLE(&pdev->dev);
|
|
|
|
+ int status;
|
|
|
|
+ struct acpi_object_list input;
|
|
|
|
+ union acpi_object input_args[0];
|
|
|
|
+ u64 device_addr;
|
|
|
|
+ u8 bus, dev, fun;
|
|
|
|
+
|
|
|
|
+ input.count = 0;
|
|
|
|
+ input.pointer = input_args;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ status = acpi_evaluate_integer(handle, (acpi_string)entry, &input, &device_addr);
|
|
|
|
+ if (status) {
|
|
|
|
+ return -ENODEV;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bus = 0;
|
|
|
|
+ dev = (device_addr & 0xFF0000) >> 16;
|
|
|
|
+ fun = device_addr & 0xFF;
|
|
|
|
+
|
|
|
|
+ dev_info(&pdev->dev, "found pci device at bus = %d, dev = %x, fun = %x\n",
|
|
|
|
+ (u32)bus, (u32)dev, (u32)fun);
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return bus << 8 | PCI_DEVFN(dev, fun);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_dsm_get_pci_addr_from_dsm(struct platform_device *pdev, const char *entry)
|
2019-12-27 23:54:14 +00:00
|
|
|
+{
|
|
|
|
+ acpi_handle handle = ACPI_HANDLE(&pdev->dev);
|
|
|
|
+ union acpi_object *result;
|
|
|
|
+ union acpi_object *e0;
|
|
|
|
+ union acpi_object *e1;
|
|
|
|
+ union acpi_object *e2;
|
|
|
|
+ u64 device_addr = 0;
|
|
|
|
+ u8 bus, dev, fun;
|
|
|
|
+ int i;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ result = acpi_evaluate_dsm_typed(handle, &SHPS_DSM_UUID, SHPS_DSM_REVISION,
|
|
|
|
+ SHPS_DSM_GPU_ADDRS, NULL, ACPI_TYPE_PACKAGE);
|
|
|
|
+
|
|
|
|
+ if (IS_ERR_OR_NULL(result))
|
|
|
|
+ return result ? PTR_ERR(result) : -EIO;
|
|
|
|
+
|
|
|
|
+ // three entries per device: name, address, <integer>
|
|
|
|
+ for (i = 0; i + 2 < result->package.count; i += 3) {
|
|
|
|
+ e0 = &result->package.elements[i];
|
|
|
|
+ e1 = &result->package.elements[i + 1];
|
|
|
|
+ e2 = &result->package.elements[i + 2];
|
|
|
|
+
|
|
|
|
+ if (e0->type != ACPI_TYPE_STRING) {
|
|
|
|
+ ACPI_FREE(result);
|
|
|
|
+ return -EIO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (e1->type != ACPI_TYPE_INTEGER) {
|
|
|
|
+ ACPI_FREE(result);
|
|
|
|
+ return -EIO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (e2->type != ACPI_TYPE_INTEGER) {
|
|
|
|
+ ACPI_FREE(result);
|
|
|
|
+ return -EIO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (strncmp(e0->string.pointer, entry, 64) == 0)
|
|
|
|
+ device_addr = e1->integer.value;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ACPI_FREE(result);
|
|
|
|
+ if (device_addr == 0)
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ // convert address
|
|
|
|
+ bus = (device_addr & 0x0FF00000) >> 20;
|
|
|
|
+ dev = (device_addr & 0x000F8000) >> 15;
|
|
|
|
+ fun = (device_addr & 0x00007000) >> 12;
|
|
|
|
+
|
|
|
|
+ return bus << 8 | PCI_DEVFN(dev, fun);
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static struct pci_dev *shps_dgpu_dsm_get_pci_dev(struct platform_device *pdev)
|
2019-12-27 23:54:14 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ struct pci_dev *dev;
|
|
|
|
+ int addr;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
|
|
|
+ if (drvdata->hardware_traits.dgpu_rp_pci_address) {
|
|
|
|
+ addr = shps_dgpu_dsm_get_pci_addr_from_adr(pdev, drvdata->hardware_traits.dgpu_rp_pci_address);
|
|
|
|
+ } else {
|
|
|
|
+ addr = shps_dgpu_dsm_get_pci_addr_from_dsm(pdev, SHPS_DSM_GPU_ADDRS_RP);
|
|
|
|
+ }
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ if (addr < 0)
|
|
|
|
+ return ERR_PTR(addr);
|
|
|
|
+
|
|
|
|
+ dev = pci_get_domain_bus_and_slot(0, (addr & 0xFF00) >> 8, addr & 0xFF);
|
|
|
|
+ return dev ? dev : ERR_PTR(-ENODEV);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_dsm_get_power_unlocked(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ struct gpio_desc *gpio = drvdata->gpio_dgpu_power;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = gpiod_get_value_cansleep(gpio);
|
|
|
|
+ if (status < 0)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ return status == 0 ? SHPS_DGPU_POWER_OFF : SHPS_DGPU_POWER_ON;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_dsm_get_power(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&drvdata->lock);
|
|
|
|
+ status = shps_dgpu_dsm_get_power_unlocked(pdev);
|
|
|
|
+ mutex_unlock(&drvdata->lock);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int __shps_dgpu_dsm_set_power_unlocked(struct platform_device *pdev, enum shps_dgpu_power power)
|
|
|
|
+{
|
|
|
|
+ acpi_handle handle = ACPI_HANDLE(&pdev->dev);
|
|
|
|
+ union acpi_object *result;
|
|
|
|
+ union acpi_object param;
|
|
|
|
+
|
|
|
|
+ dev_info(&pdev->dev, "setting dGPU direct power to \'%s\'\n", shps_dgpu_power_str(power));
|
|
|
|
+
|
|
|
|
+ param.type = ACPI_TYPE_INTEGER;
|
|
|
|
+ param.integer.value = power == SHPS_DGPU_POWER_ON;
|
|
|
|
+
|
|
|
|
+ result = acpi_evaluate_dsm_typed(handle, &SHPS_DSM_UUID, SHPS_DSM_REVISION,
|
2020-05-16 22:20:04 +00:00
|
|
|
+ SHPS_DSM_GPU_POWER, ¶m, ACPI_TYPE_BUFFER);
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+ if (IS_ERR_OR_NULL(result))
|
|
|
|
+ return result ? PTR_ERR(result) : -EIO;
|
|
|
|
+
|
|
|
|
+ // check for the expected result
|
|
|
|
+ if (result->buffer.length != 1 || result->buffer.pointer[0] != 0) {
|
|
|
|
+ ACPI_FREE(result);
|
|
|
|
+ return -EIO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ACPI_FREE(result);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_dsm_set_power_unlocked(struct platform_device *pdev, enum shps_dgpu_power power)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ if (power != SHPS_DGPU_POWER_ON && power != SHPS_DGPU_POWER_OFF)
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ status = shps_dgpu_dsm_get_power_unlocked(pdev);
|
|
|
|
+ if (status < 0)
|
|
|
|
+ return status;
|
|
|
|
+ if (status == power)
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ return __shps_dgpu_dsm_set_power_unlocked(pdev, power);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_dsm_set_power(struct platform_device *pdev, enum shps_dgpu_power power)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&drvdata->lock);
|
|
|
|
+ status = shps_dgpu_dsm_set_power_unlocked(pdev, power);
|
|
|
|
+ mutex_unlock(&drvdata->lock);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static bool shps_rp_link_up(struct pci_dev *rp)
|
|
|
|
+{
|
|
|
|
+ u16 lnksta = 0, sltsta = 0;
|
|
|
|
+
|
|
|
|
+ pcie_capability_read_word(rp, PCI_EXP_LNKSTA, &lnksta);
|
|
|
|
+ pcie_capability_read_word(rp, PCI_EXP_SLTSTA, &sltsta);
|
|
|
|
+
|
|
|
|
+ return (lnksta & PCI_EXP_LNKSTA_DLLLA) || (sltsta & PCI_EXP_SLTSTA_PDS);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_rp_get_power_unlocked(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ struct pci_dev *rp = drvdata->dgpu_root_port;
|
|
|
|
+
|
|
|
|
+ if (rp->current_state == PCI_D3hot || rp->current_state == PCI_D3cold)
|
|
|
|
+ return SHPS_DGPU_POWER_OFF;
|
|
|
|
+ else if (rp->current_state == PCI_UNKNOWN || rp->current_state == PCI_POWER_ERROR)
|
|
|
|
+ return SHPS_DGPU_POWER_UNKNOWN;
|
|
|
|
+ else
|
|
|
|
+ return SHPS_DGPU_POWER_ON;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_rp_get_power(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&drvdata->lock);
|
|
|
|
+ status = shps_dgpu_rp_get_power_unlocked(pdev);
|
|
|
|
+ mutex_unlock(&drvdata->lock);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int __shps_dgpu_rp_set_power_unlocked(struct platform_device *pdev, enum shps_dgpu_power power)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ struct pci_dev *rp = drvdata->dgpu_root_port;
|
|
|
|
+ int status, i;
|
|
|
|
+
|
|
|
|
+ dev_info(&pdev->dev, "setting dGPU power state to \'%s\'\n", shps_dgpu_power_str(power));
|
|
|
|
+
|
|
|
|
+ dbg_dump_drvsta(pdev, "__shps_dgpu_rp_set_power_unlocked.1");
|
|
|
|
+ if (power == SHPS_DGPU_POWER_ON) {
|
|
|
|
+ set_bit(SHPS_STATE_BIT_RPPWRON_SYNC, &drvdata->state);
|
|
|
|
+ pci_set_power_state(rp, PCI_D0);
|
|
|
|
+
|
|
|
|
+ if (drvdata->dgpu_root_port_state)
|
|
|
|
+ pci_load_and_free_saved_state(rp, &drvdata->dgpu_root_port_state);
|
|
|
|
+
|
|
|
|
+ pci_restore_state(rp);
|
|
|
|
+
|
|
|
|
+ if (!pci_is_enabled(rp))
|
|
|
|
+ pci_enable_device(rp);
|
|
|
|
+
|
|
|
|
+ pci_set_master(rp);
|
|
|
|
+ clear_bit(SHPS_STATE_BIT_RPPWRON_SYNC, &drvdata->state);
|
|
|
|
+
|
|
|
|
+ set_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
|
|
|
|
+ } else {
|
|
|
|
+ if (!drvdata->dgpu_root_port_state) {
|
|
|
|
+ pci_save_state(rp);
|
|
|
|
+ drvdata->dgpu_root_port_state = pci_store_saved_state(rp);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * To properly update the hot-plug system we need to "remove" the dGPU
|
|
|
|
+ * before disabling it and sending it to D3cold. Following this, we
|
|
|
|
+ * need to wait for the link and slot status to actually change.
|
|
|
|
+ */
|
|
|
|
+ status = shps_dgpu_dsm_set_power_unlocked(pdev, SHPS_DGPU_POWER_OFF);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ for (i = 0; i < 20 && shps_rp_link_up(rp); i++)
|
|
|
|
+ msleep(50);
|
|
|
|
+
|
|
|
|
+ if (shps_rp_link_up(rp))
|
|
|
|
+ dev_err(&pdev->dev, "dGPU removal via DSM timed out\n");
|
|
|
|
+
|
|
|
|
+ pci_clear_master(rp);
|
|
|
|
+
|
|
|
|
+ if (pci_is_enabled(rp))
|
|
|
|
+ pci_disable_device(rp);
|
|
|
|
+
|
|
|
|
+ pci_set_power_state(rp, PCI_D3cold);
|
|
|
|
+
|
|
|
|
+ clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
|
|
|
|
+ }
|
|
|
|
+ dbg_dump_drvsta(pdev, "__shps_dgpu_rp_set_power_unlocked.2");
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_rp_set_power_unlocked(struct platform_device *pdev, enum shps_dgpu_power power)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ if (power != SHPS_DGPU_POWER_ON && power != SHPS_DGPU_POWER_OFF)
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ status = shps_dgpu_rp_get_power_unlocked(pdev);
|
|
|
|
+ if (status < 0)
|
|
|
|
+ return status;
|
|
|
|
+ if (status == power)
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ return __shps_dgpu_rp_set_power_unlocked(pdev, power);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_rp_set_power(struct platform_device *pdev, enum shps_dgpu_power power)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&drvdata->lock);
|
|
|
|
+ status = shps_dgpu_rp_set_power_unlocked(pdev, power);
|
|
|
|
+ mutex_unlock(&drvdata->lock);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_set_power(struct platform_device *pdev, enum shps_dgpu_power power)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ if (!param_dtx_latch)
|
|
|
|
+ return shps_dgpu_rp_set_power(pdev, power);
|
|
|
|
+
|
|
|
|
+ if (power == SHPS_DGPU_POWER_ON) {
|
|
|
|
+ status = shps_dtx_latch_lock();
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ status = shps_dgpu_rp_set_power(pdev, power);
|
|
|
|
+ if (status)
|
|
|
|
+ shps_dtx_latch_unlock();
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ status = shps_dgpu_rp_set_power(pdev, power);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ status = shps_dtx_latch_unlock();
|
2019-12-27 23:54:14 +00:00
|
|
|
+ }
|
2020-05-16 22:20:04 +00:00
|
|
|
+
|
|
|
|
+ return status;
|
2019-12-27 23:54:14 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_is_present(struct platform_device *pdev)
|
|
|
|
+{
|
2020-05-16 22:20:04 +00:00
|
|
|
+ struct shps_driver_data *drvdata;
|
|
|
|
+
|
|
|
|
+ drvdata = platform_get_drvdata(pdev);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return gpiod_get_value_cansleep(drvdata->gpio_dgpu_presence);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static ssize_t dgpu_power_show(struct device *dev, struct device_attribute *attr, char *data)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
+ int power = shps_dgpu_rp_get_power(pdev);
|
|
|
|
+
|
|
|
|
+ if (power < 0)
|
|
|
|
+ return power;
|
|
|
|
+
|
|
|
|
+ return sprintf(data, "%s\n", shps_dgpu_power_str(power));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static ssize_t dgpu_power_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
+ const char *data, size_t count)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
+ enum shps_dgpu_power power;
|
|
|
|
+ bool b = false;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = kstrtobool(data, &b);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ status = shps_dgpu_is_present(pdev);
|
|
|
|
+ if (status <= 0)
|
|
|
|
+ return status < 0 ? status : -EPERM;
|
|
|
|
+
|
|
|
|
+ power = b ? SHPS_DGPU_POWER_ON : SHPS_DGPU_POWER_OFF;
|
|
|
|
+ status = shps_dgpu_set_power(pdev, power);
|
|
|
|
+
|
|
|
|
+ return status < 0 ? status : count;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static ssize_t dgpu_power_dsm_show(struct device *dev, struct device_attribute *attr, char *data)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
+ int power = shps_dgpu_dsm_get_power(pdev);
|
|
|
|
+
|
|
|
|
+ if (power < 0)
|
|
|
|
+ return power;
|
|
|
|
+
|
|
|
|
+ return sprintf(data, "%s\n", shps_dgpu_power_str(power));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static ssize_t dgpu_power_dsm_store(struct device *dev, struct device_attribute *attr,
|
|
|
|
+ const char *data, size_t count)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
+ enum shps_dgpu_power power;
|
|
|
|
+ bool b = false;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = kstrtobool(data, &b);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ status = shps_dgpu_is_present(pdev);
|
|
|
|
+ if (status <= 0)
|
|
|
|
+ return status < 0 ? status : -EPERM;
|
|
|
|
+
|
|
|
|
+ power = b ? SHPS_DGPU_POWER_ON : SHPS_DGPU_POWER_OFF;
|
|
|
|
+ status = shps_dgpu_dsm_set_power(pdev, power);
|
|
|
|
+
|
|
|
|
+ return status < 0 ? status : count;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static DEVICE_ATTR_RW(dgpu_power);
|
|
|
|
+static DEVICE_ATTR_RW(dgpu_power_dsm);
|
|
|
|
+
|
|
|
|
+static struct attribute *shps_power_attrs[] = {
|
|
|
|
+ &dev_attr_dgpu_power.attr,
|
|
|
|
+ &dev_attr_dgpu_power_dsm.attr,
|
|
|
|
+ NULL,
|
|
|
|
+};
|
|
|
|
+ATTRIBUTE_GROUPS(shps_power);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void dbg_dump_power_states(struct platform_device *pdev, const char *prefix)
|
|
|
|
+{
|
|
|
|
+ enum shps_dgpu_power power_dsm;
|
|
|
|
+ enum shps_dgpu_power power_rp;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = shps_dgpu_rp_get_power_unlocked(pdev);
|
|
|
|
+ if (status < 0)
|
|
|
|
+ dev_err(&pdev->dev, "%s: failed to get root-port power state: %d\n", prefix, status);
|
|
|
|
+ power_rp = status;
|
|
|
|
+
|
|
|
|
+ status = shps_dgpu_rp_get_power_unlocked(pdev);
|
|
|
|
+ if (status < 0)
|
|
|
|
+ dev_err(&pdev->dev, "%s: failed to get direct power state: %d\n", prefix, status);
|
|
|
|
+ power_dsm = status;
|
|
|
|
+
|
|
|
|
+ dev_dbg(&pdev->dev, "%s: root-port power state: %d\n", prefix, power_rp);
|
|
|
|
+ dev_dbg(&pdev->dev, "%s: direct power state: %d\n", prefix, power_dsm);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void dbg_dump_pciesta(struct platform_device *pdev, const char *prefix)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ struct pci_dev *rp = drvdata->dgpu_root_port;
|
|
|
|
+ u16 lnksta, lnksta2, sltsta, sltsta2;
|
|
|
|
+
|
|
|
|
+ pcie_capability_read_word(rp, PCI_EXP_LNKSTA, &lnksta);
|
|
|
|
+ pcie_capability_read_word(rp, PCI_EXP_LNKSTA2, &lnksta2);
|
|
|
|
+ pcie_capability_read_word(rp, PCI_EXP_SLTSTA, &sltsta);
|
|
|
|
+ pcie_capability_read_word(rp, PCI_EXP_SLTSTA2, &sltsta2);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_dbg(&pdev->dev, "%s: LNKSTA: 0x%04x\n", prefix, lnksta);
|
|
|
|
+ dev_dbg(&pdev->dev, "%s: LNKSTA2: 0x%04x\n", prefix, lnksta2);
|
|
|
|
+ dev_dbg(&pdev->dev, "%s: SLTSTA: 0x%04x\n", prefix, sltsta);
|
|
|
|
+ dev_dbg(&pdev->dev, "%s: SLTSTA2: 0x%04x\n", prefix, sltsta2);
|
2019-12-27 23:54:14 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void dbg_dump_drvsta(struct platform_device *pdev, const char *prefix)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ struct pci_dev *rp = drvdata->dgpu_root_port;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_dbg(&pdev->dev, "%s: RP power: %d\n", prefix, rp->current_state);
|
|
|
|
+ dev_dbg(&pdev->dev, "%s: RP state saved: %d\n", prefix, rp->state_saved);
|
|
|
|
+ dev_dbg(&pdev->dev, "%s: RP state stored: %d\n", prefix, !!drvdata->dgpu_root_port_state);
|
|
|
|
+ dev_dbg(&pdev->dev, "%s: RP enabled: %d\n", prefix, atomic_read(&rp->enable_cnt));
|
|
|
|
+ dev_dbg(&pdev->dev, "%s: RP mastered: %d\n", prefix, rp->is_busmaster);
|
2019-12-27 23:54:14 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_pm_prepare(struct device *dev)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ bool pwrtgt;
|
|
|
|
+ int status = 0;
|
|
|
|
+
|
|
|
|
+ dbg_dump_power_states(pdev, "shps_pm_prepare");
|
|
|
|
+
|
|
|
|
+ if (param_dgpu_power_susp != SHPS_DGPU_MP_POWER_ASIS) {
|
|
|
|
+ pwrtgt = test_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
|
|
|
|
+
|
|
|
|
+ status = shps_dgpu_set_power(pdev, param_dgpu_power_susp);
|
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "failed to power %s dGPU: %d\n",
|
|
|
|
+ param_dgpu_power_susp == SHPS_DGPU_MP_POWER_OFF ? "off" : "on",
|
|
|
|
+ status);
|
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (pwrtgt)
|
|
|
|
+ set_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
|
|
|
|
+ else
|
|
|
|
+ clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void shps_pm_complete(struct device *dev)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ dbg_dump_power_states(pdev, "shps_pm_complete");
|
|
|
|
+ dbg_dump_pciesta(pdev, "shps_pm_complete");
|
|
|
|
+ dbg_dump_drvsta(pdev, "shps_pm_complete.1");
|
|
|
|
+
|
|
|
|
+ // update power target, dGPU may have been detached while suspended
|
|
|
|
+ status = shps_dgpu_is_present(pdev);
|
|
|
|
+ if (status < 0) {
|
|
|
|
+ dev_err(&pdev->dev, "failed to get dGPU presence: %d\n", status);
|
|
|
|
+ return;
|
|
|
|
+ } else if (status == 0) {
|
|
|
|
+ clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * During resume, the PCIe core will power on the root-port, which in turn
|
|
|
|
+ * will power on the dGPU. Most of the state synchronization is already
|
|
|
|
+ * handled via the SAN RQSG handler, so it is in a fully consistent
|
|
|
|
+ * on-state here. If requested, turn it off here.
|
|
|
|
+ *
|
|
|
|
+ * As there seem to be some synchronization issues turning off the dGPU
|
|
|
|
+ * directly after the power-on SAN RQSG notification during the resume
|
|
|
|
+ * process, let's do this here.
|
|
|
|
+ *
|
|
|
|
+ * TODO/FIXME:
|
|
|
|
+ * This does not combat unhandled power-ons when the device is not fully
|
|
|
|
+ * resumed, i.e. re-suspended before shps_pm_complete is called. Those
|
|
|
|
+ * should normally not be an issue, but the dGPU does get hot even though
|
|
|
|
+ * it is suspended, so ideally we want to keep it off.
|
|
|
|
+ */
|
|
|
|
+ if (!test_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state)) {
|
|
|
|
+ status = shps_dgpu_set_power(pdev, SHPS_DGPU_POWER_OFF);
|
|
|
|
+ if (status)
|
|
|
|
+ dev_err(&pdev->dev, "failed to power-off dGPU: %d\n", status);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dbg_dump_drvsta(pdev, "shps_pm_complete.2");
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_pm_suspend(struct device *dev)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ if (device_may_wakeup(dev)) {
|
|
|
|
+ status = enable_irq_wake(drvdata->irq_base_presence);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ set_bit(SHPS_STATE_BIT_WAKE_ENABLED, &drvdata->state);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_pm_resume(struct device *dev)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = to_platform_device(dev);
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ int status = 0;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (test_and_clear_bit(SHPS_STATE_BIT_WAKE_ENABLED, &drvdata->state))
|
2019-12-27 23:54:14 +00:00
|
|
|
+ status = disable_irq_wake(drvdata->irq_base_presence);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void shps_shutdown(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Turn on dGPU before shutting down. This allows the core drivers to
|
|
|
|
+ * properly shut down the device. If we don't do this, the pcieport driver
|
|
|
|
+ * will complain that the device has already been disabled.
|
|
|
|
+ */
|
|
|
|
+ status = shps_dgpu_set_power(pdev, SHPS_DGPU_POWER_ON);
|
|
|
|
+ if (status)
|
|
|
|
+ dev_err(&pdev->dev, "failed to turn on dGPU: %d\n", status);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_detached(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ dbg_dump_power_states(pdev, "shps_dgpu_detached");
|
|
|
|
+ return shps_dgpu_set_power(pdev, SHPS_DGPU_POWER_OFF);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_attached(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ dbg_dump_power_states(pdev, "shps_dgpu_attached");
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_powered_on(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ /*
|
|
|
|
+ * This function gets called directly after a power-state transition of
|
|
|
|
+ * the dGPU root port out of D3cold state, indicating a power-on of the
|
|
|
|
+ * dGPU. Specifically, this function is called from the RQSG handler of
|
|
|
|
+ * SAN, invoked by the ACPI _ON method of the dGPU root port. This means
|
|
|
|
+ * that this function is run inside `pci_set_power_state(rp, ...)`
|
|
|
|
+ * syncrhonously and thus returns before the `pci_set_power_state` call
|
|
|
|
+ * does.
|
|
|
|
+ *
|
|
|
|
+ * `pci_set_power_state` may either be called by us or when the PCI
|
|
|
|
+ * subsystem decides to power up the root port (e.g. during resume). Thus
|
|
|
|
+ * we should use this function to ensure that the dGPU and root port
|
|
|
|
+ * states are consistent when an unexpected power-up is encountered.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ struct pci_dev *rp = drvdata->dgpu_root_port;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ dbg_dump_drvsta(pdev, "shps_dgpu_powered_on.1");
|
|
|
|
+
|
|
|
|
+ // if we caused the root port to power-on, return
|
|
|
|
+ if (test_bit(SHPS_STATE_BIT_RPPWRON_SYNC, &drvdata->state))
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ // if dGPU is not present, force power-target to off and return
|
|
|
|
+ status = shps_dgpu_is_present(pdev);
|
|
|
|
+ if (status == 0)
|
|
|
|
+ clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
|
|
|
|
+ if (status <= 0)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&drvdata->lock);
|
|
|
|
+
|
|
|
|
+ dbg_dump_power_states(pdev, "shps_dgpu_powered_on.1");
|
|
|
|
+ dbg_dump_pciesta(pdev, "shps_dgpu_powered_on.1");
|
|
|
|
+ if (drvdata->dgpu_root_port_state)
|
|
|
|
+ pci_load_and_free_saved_state(rp, &drvdata->dgpu_root_port_state);
|
|
|
|
+ pci_restore_state(rp);
|
|
|
|
+ if (!pci_is_enabled(rp))
|
|
|
|
+ pci_enable_device(rp);
|
|
|
|
+ pci_set_master(rp);
|
|
|
|
+ dbg_dump_drvsta(pdev, "shps_dgpu_powered_on.2");
|
|
|
|
+ dbg_dump_power_states(pdev, "shps_dgpu_powered_on.2");
|
|
|
|
+ dbg_dump_pciesta(pdev, "shps_dgpu_powered_on.2");
|
|
|
|
+
|
|
|
|
+ mutex_unlock(&drvdata->lock);
|
|
|
|
+
|
|
|
|
+ if (!test_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state)) {
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_warn(&pdev->dev, "unexpected dGPU power-on detected\n");
|
2019-12-27 23:54:14 +00:00
|
|
|
+ // TODO: schedule state re-check and update
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_dgpu_handle_rqsg(struct surface_sam_san_rqsg *rqsg, void *data)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = data;
|
|
|
|
+
|
|
|
|
+ if (rqsg->tc == SAM_DGPU_TC && rqsg->cid == SAM_DGPU_CID_POWERON)
|
|
|
|
+ return shps_dgpu_powered_on(pdev);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_warn(&pdev->dev, "unimplemented dGPU request: RQSG(0x%02x, 0x%02x, 0x%02x)\n",
|
2019-12-27 23:54:14 +00:00
|
|
|
+ rqsg->tc, rqsg->cid, rqsg->iid);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static irqreturn_t shps_dgpu_presence_irq(int irq, void *data)
|
|
|
|
+{
|
|
|
|
+ struct platform_device *pdev = data;
|
|
|
|
+ bool dgpu_present;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = shps_dgpu_is_present(pdev);
|
|
|
|
+ if (status < 0) {
|
|
|
|
+ dev_err(&pdev->dev, "failed to check physical dGPU presence: %d\n", status);
|
|
|
|
+ return IRQ_HANDLED;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ dgpu_present = status != 0;
|
|
|
|
+ dev_info(&pdev->dev, "dGPU physically %s\n", dgpu_present ? "attached" : "detached");
|
|
|
|
+
|
|
|
|
+ if (dgpu_present)
|
|
|
|
+ status = shps_dgpu_attached(pdev);
|
|
|
|
+ else
|
|
|
|
+ status = shps_dgpu_detached(pdev);
|
|
|
|
+
|
|
|
|
+ if (status)
|
|
|
|
+ dev_err(&pdev->dev, "error handling dGPU interrupt: %d\n", status);
|
|
|
|
+
|
|
|
|
+ return IRQ_HANDLED;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+static irqreturn_t shps_base_presence_irq(int irq, void *data)
|
|
|
|
+{
|
|
|
|
+ return IRQ_HANDLED; // nothing to do, just wake
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int shps_gpios_setup(struct platform_device *pdev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2019-12-27 23:54:14 +00:00
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ struct gpio_desc *gpio_dgpu_power;
|
|
|
|
+ struct gpio_desc *gpio_dgpu_presence;
|
|
|
|
+ struct gpio_desc *gpio_base_presence;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ int status;
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ // get GPIOs
|
|
|
|
+ gpio_dgpu_power = devm_gpiod_get(&pdev->dev, "dgpu_power", GPIOD_IN);
|
|
|
|
+ if (IS_ERR(gpio_dgpu_power)) {
|
|
|
|
+ status = PTR_ERR(gpio_dgpu_power);
|
|
|
|
+ goto err_out;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ gpio_dgpu_presence = devm_gpiod_get(&pdev->dev, "dgpu_presence", GPIOD_IN);
|
|
|
|
+ if (IS_ERR(gpio_dgpu_presence)) {
|
|
|
|
+ status = PTR_ERR(gpio_dgpu_presence);
|
|
|
|
+ goto err_out;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ gpio_base_presence = devm_gpiod_get(&pdev->dev, "base_presence", GPIOD_IN);
|
|
|
|
+ if (IS_ERR(gpio_base_presence)) {
|
|
|
|
+ status = PTR_ERR(gpio_base_presence);
|
|
|
|
+ goto err_out;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ // export GPIOs
|
|
|
|
+ status = gpiod_export(gpio_dgpu_power, false);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_out;
|
|
|
|
+
|
|
|
|
+ status = gpiod_export(gpio_dgpu_presence, false);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_export_dgpu_presence;
|
|
|
|
+
|
|
|
|
+ status = gpiod_export(gpio_base_presence, false);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_export_base_presence;
|
|
|
|
+
|
|
|
|
+ // create sysfs links
|
|
|
|
+ status = gpiod_export_link(&pdev->dev, "gpio-dgpu_power", gpio_dgpu_power);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_link_dgpu_power;
|
|
|
|
+
|
|
|
|
+ status = gpiod_export_link(&pdev->dev, "gpio-dgpu_presence", gpio_dgpu_presence);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_link_dgpu_presence;
|
|
|
|
+
|
|
|
|
+ status = gpiod_export_link(&pdev->dev, "gpio-base_presence", gpio_base_presence);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_link_base_presence;
|
|
|
|
+
|
|
|
|
+ drvdata->gpio_dgpu_power = gpio_dgpu_power;
|
|
|
|
+ drvdata->gpio_dgpu_presence = gpio_dgpu_presence;
|
|
|
|
+ drvdata->gpio_base_presence = gpio_base_presence;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+err_link_base_presence:
|
|
|
|
+ sysfs_remove_link(&pdev->dev.kobj, "gpio-dgpu_presence");
|
|
|
|
+err_link_dgpu_presence:
|
|
|
|
+ sysfs_remove_link(&pdev->dev.kobj, "gpio-dgpu_power");
|
|
|
|
+err_link_dgpu_power:
|
|
|
|
+ gpiod_unexport(gpio_base_presence);
|
|
|
|
+err_export_base_presence:
|
|
|
|
+ gpiod_unexport(gpio_dgpu_presence);
|
|
|
|
+err_export_dgpu_presence:
|
|
|
|
+ gpiod_unexport(gpio_dgpu_power);
|
|
|
|
+err_out:
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+static void shps_gpios_remove(struct platform_device *pdev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2019-12-27 23:54:14 +00:00
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ sysfs_remove_link(&pdev->dev.kobj, "gpio-base_presence");
|
|
|
|
+ sysfs_remove_link(&pdev->dev.kobj, "gpio-dgpu_presence");
|
|
|
|
+ sysfs_remove_link(&pdev->dev.kobj, "gpio-dgpu_power");
|
|
|
|
+ gpiod_unexport(drvdata->gpio_base_presence);
|
|
|
|
+ gpiod_unexport(drvdata->gpio_dgpu_presence);
|
|
|
|
+ gpiod_unexport(drvdata->gpio_dgpu_power);
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+static int shps_gpios_setup_irq(struct platform_device *pdev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2019-12-27 23:54:14 +00:00
|
|
|
+ const int irqf_dgpu = IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ const int irqf_base = IRQF_SHARED;
|
2019-12-27 23:54:14 +00:00
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ int status;
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ status = gpiod_to_irq(drvdata->gpio_base_presence);
|
|
|
|
+ if (status < 0)
|
|
|
|
+ return status;
|
|
|
|
+ drvdata->irq_base_presence = status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ status = gpiod_to_irq(drvdata->gpio_dgpu_presence);
|
|
|
|
+ if (status < 0)
|
|
|
|
+ return status;
|
|
|
|
+ drvdata->irq_dgpu_presence = status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ status = request_irq(drvdata->irq_base_presence,
|
|
|
|
+ shps_base_presence_irq, irqf_base,
|
|
|
|
+ "shps_base_presence_irq", pdev);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "base irq failed: %d\n", status);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return status;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ status = request_threaded_irq(drvdata->irq_dgpu_presence,
|
|
|
|
+ NULL, shps_dgpu_presence_irq, irqf_dgpu,
|
|
|
|
+ "shps_dgpu_presence_irq", pdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ if (status) {
|
2019-12-27 23:54:14 +00:00
|
|
|
+ free_irq(drvdata->irq_base_presence, pdev);
|
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+static void shps_gpios_remove_irq(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ free_irq(drvdata->irq_base_presence, pdev);
|
|
|
|
+ free_irq(drvdata->irq_dgpu_presence, pdev);
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void shps_sgcp_notify(acpi_handle device, u32 value, void *context) {
|
|
|
|
+ struct platform_device *pdev = context;
|
|
|
|
+ switch (value) {
|
|
|
|
+ case ACPI_SGCP_NOTIFY_POWER_ON:
|
|
|
|
+ shps_dgpu_powered_on(pdev);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int shps_start_sgcp_notification(struct platform_device *pdev, acpi_handle *sgpc_handle) {
|
|
|
|
+ acpi_handle handle;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = acpi_get_handle(NULL, "\\_SB.SGPC", &handle);
|
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "error in get_handle %d\n", status);
|
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ status = acpi_install_notify_handler(handle, ACPI_DEVICE_NOTIFY, shps_sgcp_notify, pdev);
|
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "error in install notify %d\n", status);
|
|
|
|
+ *sgpc_handle = NULL;
|
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ *sgpc_handle = handle;
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void shps_remove_sgcp_notification(struct platform_device *pdev) {
|
|
|
|
+ int status;
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+
|
|
|
|
+ if (drvdata->sgpc_handle) {
|
|
|
|
+ status = acpi_remove_notify_handler(drvdata->sgpc_handle, ACPI_DEVICE_NOTIFY, shps_sgcp_notify);
|
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "failed to remove notify handler: %d\n", status);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct shps_hardware_traits shps_detect_hardware_traits(struct platform_device *pdev) {
|
|
|
|
+ const struct shps_hardware_probe *p;
|
|
|
|
+
|
|
|
|
+ for (p = shps_hardware_probe_match; p->hardware_id; ++p) {
|
|
|
|
+ if (acpi_dev_present(p->hardware_id, NULL, -1)) {
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dev_info(&pdev->dev,
|
|
|
|
+ "shps_detect_hardware_traits found device %s, generation %d\n",
|
|
|
|
+ p->hardware_id ? p->hardware_id : "SAN (default)",
|
|
|
|
+ p->generation);
|
|
|
|
+
|
|
|
|
+ return *p->hardware_traits;
|
|
|
|
+}
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+static int shps_probe(struct platform_device *pdev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2019-12-27 23:54:14 +00:00
|
|
|
+ struct acpi_device *shps_dev = ACPI_COMPANION(&pdev->dev);
|
|
|
|
+ struct shps_driver_data *drvdata;
|
|
|
|
+ struct device_link *link;
|
|
|
|
+ int power, status;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct shps_hardware_traits detected_traits;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (gpiod_count(&pdev->dev, NULL) < 0) {
|
|
|
|
+ dev_err(&pdev->dev, "gpiod_count returned < 0\n");
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return -ENODEV;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ }
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+ // link to SSH
|
2019-12-10 00:26:31 +00:00
|
|
|
+ status = surface_sam_ssh_consumer_register(&pdev->dev);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status) {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status == -ENXIO ? -EPROBE_DEFER : status;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // detect what kind of hardware we're running
|
|
|
|
+ detected_traits = shps_detect_hardware_traits(pdev);
|
|
|
|
+
|
|
|
|
+ if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SAN) {
|
|
|
|
+ // link to SAN
|
|
|
|
+ status = surface_sam_san_consumer_register(&pdev->dev, 0);
|
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "failed to register with san consumer: %d\n", status);
|
|
|
|
+ return status == -ENXIO ? -EPROBE_DEFER : status;
|
|
|
|
+ }
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ status = acpi_dev_add_driver_gpios(shps_dev, shps_acpi_gpios);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "failed to add gpios: %d\n", status);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ return status;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ drvdata = kzalloc(sizeof(struct shps_driver_data), GFP_KERNEL);
|
|
|
|
+ if (!drvdata) {
|
|
|
|
+ status = -ENOMEM;
|
|
|
|
+ goto err_drvdata;
|
|
|
|
+ }
|
|
|
|
+ mutex_init(&drvdata->lock);
|
|
|
|
+ platform_set_drvdata(pdev, drvdata);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ drvdata->hardware_traits = detected_traits;
|
|
|
|
+
|
|
|
|
+ drvdata->dgpu_root_port = shps_dgpu_dsm_get_pci_dev(pdev);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ if (IS_ERR(drvdata->dgpu_root_port)) {
|
|
|
|
+ status = PTR_ERR(drvdata->dgpu_root_port);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_err(&pdev->dev, "failed to get pci dev: %d\n", status);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ goto err_rp_lookup;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ status = shps_gpios_setup(pdev);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "unable to set up gpios, %d\n", status);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ goto err_gpio;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ }
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+ status = shps_gpios_setup_irq(pdev);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "unable to set up irqs %d\n", status);
|
2019-12-27 23:54:14 +00:00
|
|
|
+ goto err_gpio_irqs;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ }
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+ status = device_add_groups(&pdev->dev, shps_power_groups);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_devattr;
|
|
|
|
+
|
|
|
|
+ link = device_link_add(&pdev->dev, &drvdata->dgpu_root_port->dev,
|
|
|
|
+ DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER);
|
|
|
|
+ if (!link)
|
|
|
|
+ goto err_devlink;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SAN) {
|
|
|
|
+ status = surface_sam_san_set_rqsg_handler(shps_dgpu_handle_rqsg, pdev);
|
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "unable to set SAN notification handler (%d)\n", status);
|
|
|
|
+ goto err_devlink;
|
|
|
|
+ }
|
|
|
|
+ } else if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SGCP) {
|
|
|
|
+ status = shps_start_sgcp_notification(pdev, &drvdata->sgpc_handle);
|
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&pdev->dev, "unable to install SGCP notification handler (%d)\n", status);
|
|
|
|
+ goto err_devlink;
|
|
|
|
+ }
|
|
|
|
+ }
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+ // if dGPU is not present turn-off root-port, else obey module param
|
|
|
|
+ status = shps_dgpu_is_present(pdev);
|
|
|
|
+ if (status < 0)
|
2020-07-02 21:19:52 +00:00
|
|
|
+ goto err_post_notification;
|
2019-12-27 23:54:14 +00:00
|
|
|
+
|
|
|
|
+ power = status == 0 ? SHPS_DGPU_POWER_OFF : param_dgpu_power_init;
|
|
|
|
+ if (power != SHPS_DGPU_MP_POWER_ASIS) {
|
|
|
|
+ status = shps_dgpu_set_power(pdev, power);
|
|
|
|
+ if (status)
|
2020-07-02 21:19:52 +00:00
|
|
|
+ goto err_post_notification;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ // initialize power target
|
|
|
|
+ status = shps_dgpu_rp_get_power(pdev);
|
|
|
|
+ if (status < 0)
|
|
|
|
+ goto err_pwrtgt;
|
|
|
|
+
|
|
|
|
+ if (status)
|
|
|
|
+ set_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
|
|
|
|
+ else
|
|
|
|
+ clear_bit(SHPS_STATE_BIT_PWRTGT, &drvdata->state);
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ device_init_wakeup(&pdev->dev, true);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+err_pwrtgt:
|
|
|
|
+ if (param_dgpu_power_exit != SHPS_DGPU_MP_POWER_ASIS) {
|
|
|
|
+ status = shps_dgpu_set_power(pdev, param_dgpu_power_exit);
|
|
|
|
+ if (status)
|
|
|
|
+ dev_err(&pdev->dev, "failed to set dGPU power state: %d\n", status);
|
|
|
|
+ }
|
2020-07-02 21:19:52 +00:00
|
|
|
+err_post_notification:
|
|
|
|
+ if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SGCP) {
|
|
|
|
+ shps_remove_sgcp_notification(pdev);
|
|
|
|
+ } else if (detected_traits.notification_method == SHPS_NOTIFICATION_METHOD_SAN) {
|
|
|
|
+ surface_sam_san_set_rqsg_handler(NULL, NULL);
|
|
|
|
+ }
|
2019-12-27 23:54:14 +00:00
|
|
|
+err_devlink:
|
|
|
|
+ device_remove_groups(&pdev->dev, shps_power_groups);
|
|
|
|
+err_devattr:
|
|
|
|
+ shps_gpios_remove_irq(pdev);
|
|
|
|
+err_gpio_irqs:
|
|
|
|
+ shps_gpios_remove(pdev);
|
|
|
|
+err_gpio:
|
|
|
|
+ pci_dev_put(drvdata->dgpu_root_port);
|
|
|
|
+err_rp_lookup:
|
|
|
|
+ platform_set_drvdata(pdev, NULL);
|
|
|
|
+ kfree(drvdata);
|
|
|
|
+err_drvdata:
|
|
|
|
+ acpi_dev_remove_driver_gpios(shps_dev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+static int shps_remove(struct platform_device *pdev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2019-12-27 23:54:14 +00:00
|
|
|
+ struct acpi_device *shps_dev = ACPI_COMPANION(&pdev->dev);
|
|
|
|
+ struct shps_driver_data *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ if (param_dgpu_power_exit != SHPS_DGPU_MP_POWER_ASIS) {
|
|
|
|
+ status = shps_dgpu_set_power(pdev, param_dgpu_power_exit);
|
|
|
|
+ if (status)
|
|
|
|
+ dev_err(&pdev->dev, "failed to set dGPU power state: %d\n", status);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ device_set_wakeup_capable(&pdev->dev, false);
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
|
|
|
+ if (drvdata->hardware_traits.notification_method == SHPS_NOTIFICATION_METHOD_SGCP) {
|
|
|
|
+ shps_remove_sgcp_notification(pdev);
|
|
|
|
+ } else if (drvdata->hardware_traits.notification_method == SHPS_NOTIFICATION_METHOD_SAN) {
|
|
|
|
+ surface_sam_san_set_rqsg_handler(NULL, NULL);
|
|
|
|
+ }
|
2019-12-27 23:54:14 +00:00
|
|
|
+ device_remove_groups(&pdev->dev, shps_power_groups);
|
|
|
|
+ shps_gpios_remove_irq(pdev);
|
|
|
|
+ shps_gpios_remove(pdev);
|
|
|
|
+ pci_dev_put(drvdata->dgpu_root_port);
|
|
|
|
+ platform_set_drvdata(pdev, NULL);
|
|
|
|
+ kfree(drvdata);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ acpi_dev_remove_driver_gpios(shps_dev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+static const struct dev_pm_ops shps_pm_ops = {
|
|
|
|
+ .prepare = shps_pm_prepare,
|
|
|
|
+ .complete = shps_pm_complete,
|
|
|
|
+ .suspend = shps_pm_suspend,
|
|
|
|
+ .resume = shps_pm_resume,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct acpi_device_id shps_acpi_match[] = {
|
|
|
|
+ { "MSHW0153", 0 },
|
2019-12-10 00:26:31 +00:00
|
|
|
+ { },
|
|
|
|
+};
|
2019-12-27 23:54:14 +00:00
|
|
|
+MODULE_DEVICE_TABLE(acpi, shps_acpi_match);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static struct platform_driver surface_sam_hps = {
|
2019-12-27 23:54:14 +00:00
|
|
|
+ .probe = shps_probe,
|
|
|
|
+ .remove = shps_remove,
|
|
|
|
+ .shutdown = shps_shutdown,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .driver = {
|
2019-12-27 23:54:14 +00:00
|
|
|
+ .name = "surface_dgpu_hps",
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .acpi_match_table = shps_acpi_match,
|
2019-12-27 23:54:14 +00:00
|
|
|
+ .pm = &shps_pm_ops,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ },
|
|
|
|
+};
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+module_platform_driver(surface_sam_hps);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
|
2019-12-27 23:54:14 +00:00
|
|
|
+MODULE_DESCRIPTION("Surface Hot-Plug System (HPS) and dGPU power-state Driver for Surface Book 2");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-10 00:26:31 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_san.c b/drivers/platform/x86/surface_sam/surface_sam_san.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..11dd6daedc3d
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_san.c
|
2020-07-02 21:19:52 +00:00
|
|
|
@@ -0,0 +1,913 @@
|
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
|
|
|
+ * Surface ACPI Notify (SAN) and ACPI integration driver for SAM.
|
|
|
|
+ * Translates communication from ACPI to SSH and back.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <linux/acpi.h>
|
|
|
|
+#include <linux/delay.h>
|
|
|
|
+#include <linux/jiffies.h>
|
|
|
|
+#include <linux/kernel.h>
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
+
|
|
|
|
+#include "surface_sam_ssh.h"
|
2019-12-27 23:54:14 +00:00
|
|
|
+#include "surface_sam_san.h"
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define SAN_RQST_RETRY 5
|
|
|
|
+
|
|
|
|
+#define SAN_DSM_REVISION 0
|
|
|
|
+#define SAN_DSM_FN_NOTIFY_SENSOR_TRIP_POINT 0x09
|
|
|
|
+
|
|
|
|
+static const guid_t SAN_DSM_UUID =
|
|
|
|
+ GUID_INIT(0x93b666c5, 0x70c6, 0x469f, 0xa2, 0x15, 0x3d,
|
2020-05-16 22:20:04 +00:00
|
|
|
+ 0x48, 0x7c, 0x91, 0xab, 0x3c);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+#define SAM_EVENT_DELAY_PWR_ADAPTER msecs_to_jiffies(5000)
|
|
|
|
+#define SAM_EVENT_DELAY_PWR_BST msecs_to_jiffies(2500)
|
|
|
|
+
|
|
|
|
+#define SAM_EVENT_PWR_CID_BIX 0x15
|
|
|
|
+#define SAM_EVENT_PWR_CID_BST 0x16
|
|
|
|
+#define SAM_EVENT_PWR_CID_ADAPTER 0x17
|
|
|
|
+#define SAM_EVENT_PWR_CID_DPTF 0x4f
|
|
|
|
+
|
|
|
|
+#define SAM_EVENT_TEMP_CID_NOTIFY_SENSOR_TRIP_POINT 0x0b
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+#define SAN_RQST_TAG "surface_sam_san: rqst: "
|
|
|
|
+#define SAN_RQSG_TAG "surface_sam_san: rqsg: "
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+#define SAN_QUIRK_BASE_STATE_DELAY 1000
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct san_acpi_consumer {
|
|
|
|
+ char *path;
|
|
|
|
+ bool required;
|
|
|
|
+ u32 flags;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct san_opreg_context {
|
|
|
|
+ struct acpi_connection_info connection;
|
|
|
|
+ struct device *dev;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct san_consumer_link {
|
|
|
|
+ const struct san_acpi_consumer *properties;
|
2019-12-27 23:54:14 +00:00
|
|
|
+ struct device_link *link;
|
2019-12-10 00:26:31 +00:00
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct san_consumers {
|
2019-12-27 23:54:14 +00:00
|
|
|
+ u32 num;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ struct san_consumer_link *links;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct san_drvdata {
|
|
|
|
+ struct san_opreg_context opreg_ctx;
|
2019-12-27 23:54:14 +00:00
|
|
|
+ struct san_consumers consumers;
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
|
|
|
+ struct platform_device *dev;
|
|
|
|
+ struct ssam_event_notifier nf_bat;
|
|
|
|
+ struct ssam_event_notifier nf_tmp;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct san_event_work {
|
|
|
|
+ struct delayed_work work;
|
|
|
|
+ struct platform_device *dev;
|
|
|
|
+ struct ssam_event event; // must be last
|
2019-12-10 00:26:31 +00:00
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct gsb_data_in {
|
|
|
|
+ u8 cv;
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+struct gsb_data_rqsx {
|
|
|
|
+ u8 cv; // command value (should be 0x01 or 0x03)
|
|
|
|
+ u8 tc; // target controller
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u8 tid; // transport channnel ID?
|
2019-12-10 00:26:31 +00:00
|
|
|
+ u8 iid; // target sub-controller (e.g. primary vs. secondary battery)
|
|
|
|
+ u8 snc; // expect-response-flag
|
|
|
|
+ u8 cid; // command ID
|
|
|
|
+ u8 cdl; // payload length
|
|
|
|
+ u8 _pad; // padding
|
|
|
|
+ u8 pld[0]; // payload
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+struct gsb_data_etwl {
|
|
|
|
+ u8 cv; // command value (should be 0x02)
|
|
|
|
+ u8 etw3; // ?
|
|
|
|
+ u8 etw4; // ?
|
|
|
|
+ u8 msg[0]; // error message (ASCIIZ)
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+struct gsb_data_out {
|
|
|
|
+ u8 status; // _SSH communication status
|
|
|
|
+ u8 len; // _SSH payload length
|
|
|
|
+ u8 pld[0]; // _SSH payload
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+union gsb_buffer_data {
|
|
|
|
+ struct gsb_data_in in; // common input
|
|
|
|
+ struct gsb_data_rqsx rqsx; // RQSX input
|
|
|
|
+ struct gsb_data_etwl etwl; // ETWL input
|
|
|
|
+ struct gsb_data_out out; // output
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct gsb_buffer {
|
|
|
|
+ u8 status; // GSB AttribRawProcess status
|
|
|
|
+ u8 len; // GSB AttribRawProcess length
|
|
|
|
+ union gsb_buffer_data data;
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+enum san_pwr_event {
|
|
|
|
+ SAN_PWR_EVENT_BAT1_STAT = 0x03,
|
|
|
|
+ SAN_PWR_EVENT_BAT1_INFO = 0x04,
|
|
|
|
+ SAN_PWR_EVENT_ADP1_STAT = 0x05,
|
|
|
|
+ SAN_PWR_EVENT_ADP1_INFO = 0x06,
|
|
|
|
+ SAN_PWR_EVENT_BAT2_STAT = 0x07,
|
|
|
|
+ SAN_PWR_EVENT_BAT2_INFO = 0x08,
|
2020-07-02 21:19:52 +00:00
|
|
|
+ SAN_PWR_EVENT_DPTF = 0x0A,
|
2019-12-10 00:26:31 +00:00
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+static int sam_san_default_rqsg_handler(struct surface_sam_san_rqsg *rqsg, void *data);
|
|
|
|
+
|
|
|
|
+struct sam_san_rqsg_if {
|
|
|
|
+ struct mutex lock;
|
|
|
|
+ struct device *san_dev;
|
|
|
|
+ surface_sam_san_rqsg_handler_fn handler;
|
|
|
|
+ void *handler_data;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static struct sam_san_rqsg_if rqsg_if = {
|
|
|
|
+ .lock = __MUTEX_INITIALIZER(rqsg_if.lock),
|
|
|
|
+ .san_dev = NULL,
|
|
|
|
+ .handler = sam_san_default_rqsg_handler,
|
|
|
|
+ .handler_data = NULL,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+int surface_sam_san_consumer_register(struct device *consumer, u32 flags)
|
|
|
|
+{
|
|
|
|
+ const u32 valid = DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ if ((flags | valid) != valid)
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ flags |= DL_FLAG_AUTOREMOVE_CONSUMER;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&rqsg_if.lock);
|
|
|
|
+ if (rqsg_if.san_dev)
|
|
|
|
+ status = device_link_add(consumer, rqsg_if.san_dev, flags) ? 0 : -EINVAL;
|
|
|
|
+ else
|
|
|
|
+ status = -ENXIO;
|
|
|
|
+ mutex_unlock(&rqsg_if.lock);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+EXPORT_SYMBOL_GPL(surface_sam_san_consumer_register);
|
|
|
|
+
|
|
|
|
+int surface_sam_san_set_rqsg_handler(surface_sam_san_rqsg_handler_fn fn, void *data)
|
|
|
|
+{
|
|
|
|
+ int status = -EBUSY;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&rqsg_if.lock);
|
|
|
|
+
|
|
|
|
+ if (rqsg_if.handler == sam_san_default_rqsg_handler || !fn) {
|
|
|
|
+ rqsg_if.handler = fn ? fn : sam_san_default_rqsg_handler;
|
|
|
|
+ rqsg_if.handler_data = data;
|
|
|
|
+ status = 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ mutex_unlock(&rqsg_if.lock);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+EXPORT_SYMBOL_GPL(surface_sam_san_set_rqsg_handler);
|
|
|
|
+
|
|
|
|
+int san_call_rqsg_handler(struct surface_sam_san_rqsg *rqsg)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&rqsg_if.lock);
|
|
|
|
+ status = rqsg_if.handler(rqsg, rqsg_if.handler_data);
|
|
|
|
+ mutex_unlock(&rqsg_if.lock);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int sam_san_default_rqsg_handler(struct surface_sam_san_rqsg *rqsg, void *data)
|
|
|
|
+{
|
|
|
|
+ pr_warn(SAN_RQSG_TAG "unhandled request: RQSG(0x%02x, 0x%02x, 0x%02x)\n",
|
|
|
|
+ rqsg->tc, rqsg->cid, rqsg->iid);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static bool san_acpi_can_notify(struct device *dev, u64 func)
|
|
|
|
+{
|
|
|
|
+ acpi_handle san = ACPI_HANDLE(dev);
|
|
|
|
+ return acpi_check_dsm(san, &SAN_DSM_UUID, SAN_DSM_REVISION, 1 << func);
|
|
|
|
+}
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+static int san_acpi_notify_power_event(struct device *dev, enum san_pwr_event event)
|
|
|
|
+{
|
|
|
|
+ acpi_handle san = ACPI_HANDLE(dev);
|
|
|
|
+ union acpi_object *obj;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (!san_acpi_can_notify(dev, event))
|
|
|
|
+ return 0;
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+ dev_dbg(dev, "notify power event 0x%02x\n", event);
|
|
|
|
+ obj = acpi_evaluate_dsm_typed(san, &SAN_DSM_UUID, SAN_DSM_REVISION,
|
2020-07-02 21:19:52 +00:00
|
|
|
+ event, NULL, ACPI_TYPE_BUFFER);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (IS_ERR_OR_NULL(obj))
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return obj ? PTR_ERR(obj) : -ENXIO;
|
|
|
|
+
|
|
|
|
+ if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
|
|
|
|
+ dev_err(dev, "got unexpected result from _DSM\n");
|
|
|
|
+ return -EFAULT;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ACPI_FREE(obj);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int san_acpi_notify_sensor_trip_point(struct device *dev, u8 iid)
|
|
|
|
+{
|
|
|
|
+ acpi_handle san = ACPI_HANDLE(dev);
|
|
|
|
+ union acpi_object *obj;
|
|
|
|
+ union acpi_object param;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (!san_acpi_can_notify(dev, SAN_DSM_FN_NOTIFY_SENSOR_TRIP_POINT))
|
|
|
|
+ return 0;
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+ param.type = ACPI_TYPE_INTEGER;
|
|
|
|
+ param.integer.value = iid;
|
|
|
|
+
|
|
|
|
+ obj = acpi_evaluate_dsm_typed(san, &SAN_DSM_UUID, SAN_DSM_REVISION,
|
2020-05-16 22:20:04 +00:00
|
|
|
+ SAN_DSM_FN_NOTIFY_SENSOR_TRIP_POINT,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ ¶m, ACPI_TYPE_BUFFER);
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (IS_ERR_OR_NULL(obj))
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return obj ? PTR_ERR(obj) : -ENXIO;
|
|
|
|
+
|
|
|
|
+ if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
|
|
|
|
+ dev_err(dev, "got unexpected result from _DSM\n");
|
|
|
|
+ return -EFAULT;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ACPI_FREE(obj);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int san_evt_power_adapter(struct device *dev, const struct ssam_event *event)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = san_acpi_notify_power_event(dev, SAN_PWR_EVENT_ADP1_STAT);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Enusre that the battery states get updated correctly.
|
|
|
|
+ * When the battery is fully charged and an adapter is plugged in, it
|
|
|
|
+ * sometimes is not updated correctly, instead showing it as charging.
|
|
|
|
+ * Explicitly trigger battery updates to fix this.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ status = san_acpi_notify_power_event(dev, SAN_PWR_EVENT_BAT1_STAT);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return san_acpi_notify_power_event(dev, SAN_PWR_EVENT_BAT2_STAT);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int san_evt_power_bix(struct device *dev, const struct ssam_event *event)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ enum san_pwr_event evcode;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (event->instance_id == 0x02)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ evcode = SAN_PWR_EVENT_BAT2_INFO;
|
2020-05-16 22:20:04 +00:00
|
|
|
+ else
|
2019-12-10 00:26:31 +00:00
|
|
|
+ evcode = SAN_PWR_EVENT_BAT1_INFO;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return san_acpi_notify_power_event(dev, evcode);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int san_evt_power_bst(struct device *dev, const struct ssam_event *event)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ enum san_pwr_event evcode;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (event->instance_id == 0x02)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ evcode = SAN_PWR_EVENT_BAT2_STAT;
|
2020-05-16 22:20:04 +00:00
|
|
|
+ else
|
2019-12-10 00:26:31 +00:00
|
|
|
+ evcode = SAN_PWR_EVENT_BAT1_STAT;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return san_acpi_notify_power_event(dev, evcode);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline int san_evt_power_dptf(struct device *dev, const struct ssam_event *event)
|
|
|
|
+{
|
|
|
|
+ union acpi_object payload;
|
|
|
|
+ acpi_handle san = ACPI_HANDLE(dev);
|
|
|
|
+ union acpi_object *obj;
|
|
|
|
+
|
|
|
|
+ if (!san_acpi_can_notify(dev, SAN_PWR_EVENT_DPTF))
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * The Surface ACPI expects a buffer and not a package. It specifically
|
|
|
|
+ * checks for ObjectType (Arg3) == 0x03. This will cause a warning in
|
|
|
|
+ * acpica/nsarguments.c, but this can safely be ignored.
|
|
|
|
+ */
|
|
|
|
+ payload.type = ACPI_TYPE_BUFFER;
|
|
|
|
+ payload.buffer.length = event->length;
|
|
|
|
+ payload.buffer.pointer = (u8 *)&event->data[0];
|
|
|
|
+
|
|
|
|
+ dev_dbg(dev, "notify power event 0x%02x\n", event->command_id);
|
|
|
|
+ obj = acpi_evaluate_dsm_typed(san, &SAN_DSM_UUID, SAN_DSM_REVISION,
|
|
|
|
+ SAN_PWR_EVENT_DPTF, &payload,
|
|
|
|
+ ACPI_TYPE_BUFFER);
|
|
|
|
+
|
|
|
|
+ if (IS_ERR_OR_NULL(obj))
|
|
|
|
+ return obj ? PTR_ERR(obj) : -ENXIO;
|
|
|
|
+
|
|
|
|
+ if (obj->buffer.length != 1 || obj->buffer.pointer[0] != 0) {
|
|
|
|
+ dev_err(dev, "got unexpected result from _DSM\n");
|
|
|
|
+ return -EFAULT;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ACPI_FREE(obj);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static unsigned long san_evt_power_delay(u8 cid)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ switch (cid) {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ case SAM_EVENT_PWR_CID_ADAPTER:
|
|
|
|
+ /*
|
|
|
|
+ * Wait for battery state to update before signalling adapter change.
|
|
|
|
+ */
|
|
|
|
+ return SAM_EVENT_DELAY_PWR_ADAPTER;
|
|
|
|
+
|
|
|
|
+ case SAM_EVENT_PWR_CID_BST:
|
|
|
|
+ /*
|
|
|
|
+ * Ensure we do not miss anything important due to caching.
|
|
|
|
+ */
|
|
|
|
+ return SAM_EVENT_DELAY_PWR_BST;
|
|
|
|
+
|
|
|
|
+ case SAM_EVENT_PWR_CID_BIX:
|
|
|
|
+ case SAM_EVENT_PWR_CID_DPTF:
|
|
|
|
+ default:
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static bool san_evt_power(const struct ssam_event *event, struct device *dev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ switch (event->command_id) {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ case SAM_EVENT_PWR_CID_BIX:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = san_evt_power_bix(dev, event);
|
|
|
|
+ break;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ case SAM_EVENT_PWR_CID_BST:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = san_evt_power_bst(dev, event);
|
|
|
|
+ break;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ case SAM_EVENT_PWR_CID_ADAPTER:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = san_evt_power_adapter(dev, event);
|
|
|
|
+ break;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ case SAM_EVENT_PWR_CID_DPTF:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = san_evt_power_dptf(dev, event);
|
|
|
|
+ break;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ default:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return false;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status)
|
|
|
|
+ dev_err(dev, "error handling power event (cid = %x)\n",
|
|
|
|
+ event->command_id);
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void san_evt_power_workfn(struct work_struct *work)
|
|
|
|
+{
|
|
|
|
+ struct san_event_work *ev = container_of(work, struct san_event_work, work.work);
|
|
|
|
+
|
|
|
|
+ san_evt_power(&ev->event, &ev->dev->dev);
|
|
|
|
+ kfree(ev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static u32 san_evt_power_nb(struct ssam_notifier_block *nb, const struct ssam_event *event)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct san_drvdata *drvdata = container_of(nb, struct san_drvdata, nf_bat.base);
|
|
|
|
+ struct san_event_work *work;
|
|
|
|
+ unsigned long delay = san_evt_power_delay(event->command_id);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (delay == 0) {
|
|
|
|
+ if (san_evt_power(event, &drvdata->dev->dev))
|
|
|
|
+ return SSAM_NOTIF_HANDLED;
|
|
|
|
+ else
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ work = kzalloc(sizeof(struct san_event_work) + event->length, GFP_KERNEL);
|
|
|
|
+ if (!work)
|
|
|
|
+ return ssam_notifier_from_errno(-ENOMEM);
|
|
|
|
+
|
|
|
|
+ INIT_DELAYED_WORK(&work->work, san_evt_power_workfn);
|
|
|
|
+ work->dev = drvdata->dev;
|
|
|
|
+
|
|
|
|
+ memcpy(&work->event, event, sizeof(struct ssam_event) + event->length);
|
|
|
|
+
|
|
|
|
+ schedule_delayed_work(&work->work, delay);
|
|
|
|
+ return SSAM_NOTIF_HANDLED;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static inline int san_evt_thermal_notify(struct device *dev, const struct ssam_event *event)
|
|
|
|
+{
|
|
|
|
+ return san_acpi_notify_sensor_trip_point(dev, event->instance_id);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static bool san_evt_thermal(const struct ssam_event *event, struct device *dev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ switch (event->command_id) {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ case SAM_EVENT_TEMP_CID_NOTIFY_SENSOR_TRIP_POINT:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = san_evt_thermal_notify(dev, event);
|
|
|
|
+ break;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ default:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return false;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(dev, "error handling thermal event (cid = %x)\n",
|
|
|
|
+ event->command_id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static u32 san_evt_thermal_nb(struct ssam_notifier_block *nb, const struct ssam_event *event)
|
|
|
|
+{
|
|
|
|
+ struct san_drvdata *drvdata = container_of(nb, struct san_drvdata, nf_tmp.base);
|
|
|
|
+ struct platform_device *pdev = drvdata->dev;
|
|
|
|
+
|
|
|
|
+ if (san_evt_thermal(event, &pdev->dev))
|
|
|
|
+ return SSAM_NOTIF_HANDLED;
|
|
|
|
+ else
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static struct gsb_data_rqsx
|
|
|
|
+*san_validate_rqsx(struct device *dev, const char *type, struct gsb_buffer *buffer)
|
|
|
|
+{
|
|
|
|
+ struct gsb_data_rqsx *rqsx = &buffer->data.rqsx;
|
|
|
|
+
|
|
|
|
+ if (buffer->len < 8) {
|
|
|
|
+ dev_err(dev, "invalid %s package (len = %d)\n",
|
|
|
|
+ type, buffer->len);
|
|
|
|
+ return NULL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (rqsx->cdl != buffer->len - 8) {
|
|
|
|
+ dev_err(dev, "bogus %s package (len = %d, cdl = %d)\n",
|
|
|
|
+ type, buffer->len, rqsx->cdl);
|
|
|
|
+ return NULL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (rqsx->tid != 0x01) {
|
|
|
|
+ dev_warn(dev, "unsupported %s package (tid = 0x%02x)\n",
|
|
|
|
+ type, rqsx->tid);
|
|
|
|
+ return NULL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return rqsx;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static acpi_status
|
|
|
|
+san_etwl(struct san_opreg_context *ctx, struct gsb_buffer *buffer)
|
|
|
|
+{
|
|
|
|
+ struct gsb_data_etwl *etwl = &buffer->data.etwl;
|
|
|
|
+
|
|
|
|
+ if (buffer->len < 3) {
|
|
|
|
+ dev_err(ctx->dev, "invalid ETWL package (len = %d)\n", buffer->len);
|
|
|
|
+ return AE_OK;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dev_err(ctx->dev, "ETWL(0x%02x, 0x%02x): %.*s\n",
|
|
|
|
+ etwl->etw3, etwl->etw4,
|
|
|
|
+ buffer->len - 3, (char *)etwl->msg);
|
|
|
|
+
|
|
|
|
+ // indicate success
|
|
|
|
+ buffer->status = 0x00;
|
|
|
|
+ buffer->len = 0x00;
|
|
|
|
+
|
|
|
|
+ return AE_OK;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static acpi_status
|
|
|
|
+san_rqst(struct san_opreg_context *ctx, struct gsb_buffer *buffer)
|
|
|
|
+{
|
|
|
|
+ struct gsb_data_rqsx *gsb_rqst = san_validate_rqsx(ctx->dev, "RQST", buffer);
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {};
|
|
|
|
+ struct surface_sam_ssh_buf result = {};
|
|
|
|
+ int status = 0;
|
|
|
|
+ int try;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!gsb_rqst)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return AE_OK;
|
|
|
|
+
|
|
|
|
+ rqst.tc = gsb_rqst->tc;
|
|
|
|
+ rqst.cid = gsb_rqst->cid;
|
|
|
|
+ rqst.iid = gsb_rqst->iid;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ rqst.chn = gsb_rqst->tid;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = gsb_rqst->snc;
|
|
|
|
+ rqst.cdl = gsb_rqst->cdl;
|
|
|
|
+ rqst.pld = &gsb_rqst->pld[0];
|
|
|
|
+
|
|
|
|
+ result.cap = SURFACE_SAM_SSH_MAX_RQST_RESPONSE;
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = kzalloc(result.cap, GFP_KERNEL);
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!result.data)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return AE_NO_MEMORY;
|
|
|
|
+
|
|
|
|
+ for (try = 0; try < SAN_RQST_RETRY; try++) {
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (try)
|
|
|
|
+ dev_warn(ctx->dev, SAN_RQST_TAG "IO error occurred, trying again\n");
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_rqst(&rqst, &result);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status != -EIO)
|
|
|
|
+ break;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (rqst.tc == 0x11 && rqst.cid == 0x0D && status == -EPERM) {
|
|
|
|
+ /* Base state quirk:
|
|
|
|
+ * The base state may be queried from ACPI when the EC is still
|
|
|
|
+ * suspended. In this case it will return '-EPERM'. This query
|
|
|
|
+ * will only be triggered from the ACPI lid GPE interrupt, thus
|
|
|
|
+ * we are either in laptop or studio mode (base status 0x01 or
|
|
|
|
+ * 0x02). Furthermore, we will only get here if the device (and
|
|
|
|
+ * EC) have been suspended.
|
|
|
|
+ *
|
|
|
|
+ * We now assume that the device is in laptop mode (0x01). This
|
|
|
|
+ * has the drawback that it will wake the device when unfolding
|
|
|
|
+ * it in studio mode, but it also allows us to avoid actively
|
|
|
|
+ * waiting for the EC to wake up, which may incur a notable
|
|
|
|
+ * delay.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ buffer->status = 0x00;
|
|
|
|
+ buffer->len = 0x03;
|
|
|
|
+ buffer->data.out.status = 0x00;
|
|
|
|
+ buffer->data.out.len = 0x01;
|
|
|
|
+ buffer->data.out.pld[0] = 0x01;
|
|
|
|
+
|
|
|
|
+ } else if (!status) { // success
|
|
|
|
+ buffer->status = 0x00;
|
|
|
|
+ buffer->len = result.len + 2;
|
|
|
|
+ buffer->data.out.status = 0x00;
|
|
|
|
+ buffer->data.out.len = result.len;
|
|
|
|
+ memcpy(&buffer->data.out.pld[0], result.data, result.len);
|
|
|
|
+
|
|
|
|
+ } else { // failure
|
|
|
|
+ dev_err(ctx->dev, SAN_RQST_TAG "failed with error %d\n", status);
|
|
|
|
+ buffer->status = 0x00;
|
|
|
|
+ buffer->len = 0x02;
|
|
|
|
+ buffer->data.out.status = 0x01; // indicate _SSH error
|
|
|
|
+ buffer->data.out.len = 0x00;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ kfree(result.data);
|
|
|
|
+
|
|
|
|
+ return AE_OK;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static acpi_status
|
|
|
|
+san_rqsg(struct san_opreg_context *ctx, struct gsb_buffer *buffer)
|
|
|
|
+{
|
2019-12-27 23:54:14 +00:00
|
|
|
+ struct gsb_data_rqsx *gsb_rqsg = san_validate_rqsx(ctx->dev, "RQSG", buffer);
|
|
|
|
+ struct surface_sam_san_rqsg rqsg = {};
|
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!gsb_rqsg)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return AE_OK;
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ rqsg.tc = gsb_rqsg->tc;
|
|
|
|
+ rqsg.cid = gsb_rqsg->cid;
|
|
|
|
+ rqsg.iid = gsb_rqsg->iid;
|
|
|
|
+ rqsg.cdl = gsb_rqsg->cdl;
|
|
|
|
+ rqsg.pld = &gsb_rqsg->pld[0];
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ status = san_call_rqsg_handler(&rqsg);
|
|
|
|
+ if (!status) {
|
|
|
|
+ buffer->status = 0x00;
|
|
|
|
+ buffer->len = 0x02;
|
|
|
|
+ buffer->data.out.status = 0x00;
|
|
|
|
+ buffer->data.out.len = 0x00;
|
|
|
|
+ } else {
|
|
|
|
+ dev_err(ctx->dev, SAN_RQSG_TAG "failed with error %d\n", status);
|
|
|
|
+ buffer->status = 0x00;
|
|
|
|
+ buffer->len = 0x02;
|
|
|
|
+ buffer->data.out.status = 0x01; // indicate _SSH error
|
|
|
|
+ buffer->data.out.len = 0x00;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ return AE_OK;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static acpi_status
|
|
|
|
+san_opreg_handler(u32 function, acpi_physical_address command,
|
|
|
|
+ u32 bits, u64 *value64,
|
|
|
|
+ void *opreg_context, void *region_context)
|
|
|
|
+{
|
|
|
|
+ struct san_opreg_context *context = opreg_context;
|
|
|
|
+ struct gsb_buffer *buffer = (struct gsb_buffer *)value64;
|
|
|
|
+ int accessor_type = (0xFFFF0000 & function) >> 16;
|
|
|
|
+
|
|
|
|
+ if (command != 0) {
|
|
|
|
+ dev_warn(context->dev, "unsupported command: 0x%02llx\n", command);
|
|
|
|
+ return AE_OK;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (accessor_type != ACPI_GSB_ACCESS_ATTRIB_RAW_PROCESS) {
|
|
|
|
+ dev_err(context->dev, "invalid access type: 0x%02x\n", accessor_type);
|
|
|
|
+ return AE_OK;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // buffer must have at least contain the command-value
|
|
|
|
+ if (buffer->len == 0) {
|
|
|
|
+ dev_err(context->dev, "request-package too small\n");
|
|
|
|
+ return AE_OK;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ switch (buffer->data.in.cv) {
|
|
|
|
+ case 0x01: return san_rqst(context, buffer);
|
|
|
|
+ case 0x02: return san_etwl(context, buffer);
|
|
|
|
+ case 0x03: return san_rqsg(context, buffer);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ dev_warn(context->dev, "unsupported SAN0 request (cv: 0x%02x)\n", buffer->data.in.cv);
|
|
|
|
+ return AE_OK;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int san_events_register(struct platform_device *pdev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct san_drvdata *drvdata = platform_get_drvdata(pdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ int status;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ drvdata->nf_bat.base.priority = 1;
|
|
|
|
+ drvdata->nf_bat.base.fn = san_evt_power_nb;
|
|
|
|
+ drvdata->nf_bat.event.reg = SSAM_EVENT_REGISTRY_SAM;
|
|
|
|
+ drvdata->nf_bat.event.id.target_category = SSAM_SSH_TC_BAT;
|
|
|
|
+ drvdata->nf_bat.event.id.instance = 0;
|
|
|
|
+ drvdata->nf_bat.event.flags = SSAM_EVENT_SEQUENCED;
|
|
|
|
+
|
|
|
|
+ drvdata->nf_tmp.base.priority = 1;
|
|
|
|
+ drvdata->nf_tmp.base.fn = san_evt_thermal_nb;
|
|
|
|
+ drvdata->nf_tmp.event.reg = SSAM_EVENT_REGISTRY_SAM;
|
|
|
|
+ drvdata->nf_tmp.event.id.target_category = SSAM_SSH_TC_TMP;
|
|
|
|
+ drvdata->nf_tmp.event.id.instance = 0;
|
|
|
|
+ drvdata->nf_tmp.event.flags = SSAM_EVENT_SEQUENCED;
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_notifier_register(&drvdata->nf_bat);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = surface_sam_ssh_notifier_register(&drvdata->nf_tmp);
|
|
|
|
+ if (status)
|
|
|
|
+ surface_sam_ssh_notifier_unregister(&drvdata->nf_bat);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void san_events_unregister(struct platform_device *pdev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct san_drvdata *drvdata = platform_get_drvdata(pdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ surface_sam_ssh_notifier_unregister(&drvdata->nf_bat);
|
|
|
|
+ surface_sam_ssh_notifier_unregister(&drvdata->nf_tmp);
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int san_consumers_link(struct platform_device *pdev,
|
|
|
|
+ const struct san_acpi_consumer *cons,
|
|
|
|
+ struct san_consumers *out)
|
|
|
|
+{
|
|
|
|
+ const struct san_acpi_consumer *con;
|
|
|
|
+ struct san_consumer_link *links, *link;
|
|
|
|
+ struct acpi_device *adev;
|
|
|
|
+ acpi_handle handle;
|
|
|
|
+ u32 max_links = 0;
|
|
|
|
+ int status;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!cons)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ // count links
|
2020-05-16 22:20:04 +00:00
|
|
|
+ for (con = cons; con->path; ++con)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ max_links += 1;
|
|
|
|
+
|
|
|
|
+ // allocate
|
2020-05-16 22:20:04 +00:00
|
|
|
+ links = kcalloc(max_links, sizeof(struct san_consumer_link), GFP_KERNEL);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ link = &links[0];
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!links)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -ENOMEM;
|
|
|
|
+
|
|
|
|
+ // create links
|
|
|
|
+ for (con = cons; con->path; ++con) {
|
|
|
|
+ status = acpi_get_handle(NULL, con->path, &handle);
|
|
|
|
+ if (status) {
|
|
|
|
+ if (con->required || status != AE_NOT_FOUND) {
|
|
|
|
+ status = -ENXIO;
|
|
|
|
+ goto cleanup;
|
|
|
|
+ } else {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ status = acpi_bus_get_device(handle, &adev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto cleanup;
|
|
|
|
+
|
|
|
|
+ link->link = device_link_add(&adev->dev, &pdev->dev, con->flags);
|
|
|
|
+ if (!(link->link)) {
|
|
|
|
+ status = -EFAULT;
|
|
|
|
+ goto cleanup;
|
|
|
|
+ }
|
|
|
|
+ link->properties = con;
|
|
|
|
+
|
|
|
|
+ link += 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ out->num = link - links;
|
|
|
|
+ out->links = links;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+cleanup:
|
|
|
|
+ for (link = link - 1; link >= links; --link) {
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (link->properties->flags & DL_FLAG_STATELESS)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ device_link_del(link->link);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static void san_consumers_unlink(struct san_consumers *consumers)
|
|
|
|
+{
|
2019-12-10 00:26:31 +00:00
|
|
|
+ u32 i;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!consumers)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ for (i = 0; i < consumers->num; ++i) {
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (consumers->links[i].properties->flags & DL_FLAG_STATELESS)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ device_link_del(consumers->links[i].link);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ kfree(consumers->links);
|
|
|
|
+
|
|
|
|
+ consumers->num = 0;
|
|
|
|
+ consumers->links = NULL;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_san_probe(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ const struct san_acpi_consumer *cons;
|
|
|
|
+ struct san_drvdata *drvdata;
|
|
|
|
+ acpi_handle san = ACPI_HANDLE(&pdev->dev); // _SAN device node
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Defer probe if the _SSH driver has not set up the controller yet. This
|
|
|
|
+ * makes sure we do not fail any initial requests (e.g. _STA request without
|
|
|
|
+ * which the battery does not get set up correctly). Otherwise register as
|
|
|
|
+ * consumer to set up a device_link.
|
|
|
|
+ */
|
|
|
|
+ status = surface_sam_ssh_consumer_register(&pdev->dev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status == -ENXIO ? -EPROBE_DEFER : status;
|
|
|
|
+
|
|
|
|
+ drvdata = kzalloc(sizeof(struct san_drvdata), GFP_KERNEL);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!drvdata)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -ENOMEM;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ drvdata->dev = pdev;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ drvdata->opreg_ctx.dev = &pdev->dev;
|
|
|
|
+
|
|
|
|
+ cons = acpi_device_get_match_data(&pdev->dev);
|
|
|
|
+ status = san_consumers_link(pdev, cons, &drvdata->consumers);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_consumers;
|
|
|
|
+
|
|
|
|
+ platform_set_drvdata(pdev, drvdata);
|
|
|
|
+
|
|
|
|
+ status = acpi_install_address_space_handler(san,
|
|
|
|
+ ACPI_ADR_SPACE_GSBUS,
|
|
|
|
+ &san_opreg_handler,
|
|
|
|
+ NULL, &drvdata->opreg_ctx);
|
|
|
|
+
|
|
|
|
+ if (ACPI_FAILURE(status)) {
|
|
|
|
+ status = -ENODEV;
|
|
|
|
+ goto err_install_handler;
|
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = san_events_register(pdev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_enable_events;
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ mutex_lock(&rqsg_if.lock);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!rqsg_if.san_dev)
|
2019-12-27 23:54:14 +00:00
|
|
|
+ rqsg_if.san_dev = &pdev->dev;
|
2020-05-16 22:20:04 +00:00
|
|
|
+ else
|
2019-12-27 23:54:14 +00:00
|
|
|
+ status = -EBUSY;
|
|
|
|
+ mutex_unlock(&rqsg_if.lock);
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-27 23:54:14 +00:00
|
|
|
+ goto err_install_dev;
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+ acpi_walk_dep_device_list(san);
|
|
|
|
+ return 0;
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+err_install_dev:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ san_events_unregister(pdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+err_enable_events:
|
|
|
|
+ acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS, &san_opreg_handler);
|
|
|
|
+err_install_handler:
|
|
|
|
+ platform_set_drvdata(san, NULL);
|
|
|
|
+ san_consumers_unlink(&drvdata->consumers);
|
|
|
|
+err_consumers:
|
|
|
|
+ kfree(drvdata);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_san_remove(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct san_drvdata *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+ acpi_handle san = ACPI_HANDLE(&pdev->dev); // _SAN device node
|
|
|
|
+ acpi_status status = AE_OK;
|
|
|
|
+
|
2019-12-27 23:54:14 +00:00
|
|
|
+ mutex_lock(&rqsg_if.lock);
|
|
|
|
+ rqsg_if.san_dev = NULL;
|
|
|
|
+ mutex_unlock(&rqsg_if.lock);
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+ acpi_remove_address_space_handler(san, ACPI_ADR_SPACE_GSBUS, &san_opreg_handler);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ san_events_unregister(pdev);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * We have unregistered our event sources. Now we need to ensure that
|
|
|
|
+ * all delayed works they may have spawned are run to completion.
|
|
|
|
+ */
|
|
|
|
+ flush_scheduled_work();
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ san_consumers_unlink(&drvdata->consumers);
|
|
|
|
+ kfree(drvdata);
|
|
|
|
+
|
|
|
|
+ platform_set_drvdata(pdev, NULL);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static const struct san_acpi_consumer san_mshw0091_consumers[] = {
|
|
|
|
+ { "\\_SB.SRTC", true, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
|
|
|
|
+ { "\\ADP1", true, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
|
|
|
|
+ { "\\_SB.BAT1", true, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
|
|
|
|
+ { "\\_SB.BAT2", false, DL_FLAG_PM_RUNTIME | DL_FLAG_STATELESS },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct acpi_device_id surface_sam_san_match[] = {
|
2020-05-16 22:20:04 +00:00
|
|
|
+ { "MSHW0091", (unsigned long) san_mshw0091_consumers },
|
2019-12-10 00:26:31 +00:00
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+MODULE_DEVICE_TABLE(acpi, surface_sam_san_match);
|
|
|
|
+
|
|
|
|
+static struct platform_driver surface_sam_san = {
|
|
|
|
+ .probe = surface_sam_san_probe,
|
|
|
|
+ .remove = surface_sam_san_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_san",
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .acpi_match_table = surface_sam_san_match,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+module_platform_driver(surface_sam_san);
|
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
|
|
|
|
+MODULE_DESCRIPTION("Surface ACPI Notify Driver for 5th Generation Surface Devices");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-27 23:54:14 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_san.h b/drivers/platform/x86/surface_sam/surface_sam_san.h
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..2b9dee159bbb
|
2019-12-27 23:54:14 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_san.h
|
2020-05-16 22:20:04 +00:00
|
|
|
@@ -0,0 +1,30 @@
|
2020-07-02 21:19:52 +00:00
|
|
|
+/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2019-12-27 23:54:14 +00:00
|
|
|
+/*
|
|
|
|
+ * Interface for Surface ACPI/Notify (SAN).
|
|
|
|
+ *
|
|
|
|
+ * The SAN is the main interface between the Surface Serial Hub (SSH) and the
|
|
|
|
+ * Surface/System Aggregator Module (SAM). It allows requests to be translated
|
|
|
|
+ * from ACPI to SSH/SAM. It also interfaces with the discrete GPU hot-plug
|
|
|
|
+ * driver.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#ifndef _SURFACE_SAM_SAN_H
|
|
|
|
+#define _SURFACE_SAM_SAN_H
|
|
|
|
+
|
|
|
|
+#include <linux/types.h>
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct surface_sam_san_rqsg {
|
|
|
|
+ u8 tc; // target category
|
|
|
|
+ u8 cid; // command ID
|
|
|
|
+ u8 iid; // instance ID
|
2020-05-16 22:20:04 +00:00
|
|
|
+ u8 cdl; // command data length (length of payload)
|
2019-12-27 23:54:14 +00:00
|
|
|
+ u8 *pld; // pointer to payload of length cdl
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+typedef int (*surface_sam_san_rqsg_handler_fn)(struct surface_sam_san_rqsg *rqsg, void *data);
|
|
|
|
+
|
|
|
|
+int surface_sam_san_consumer_register(struct device *consumer, u32 flags);
|
|
|
|
+int surface_sam_san_set_rqsg_handler(surface_sam_san_rqsg_handler_fn fn, void *data);
|
|
|
|
+
|
|
|
|
+#endif /* _SURFACE_SAM_SAN_H */
|
2019-12-10 00:26:31 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid.c b/drivers/platform/x86/surface_sam/surface_sam_sid.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..caa2e6446b5f
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_sid.c
|
2020-07-15 20:26:02 +00:00
|
|
|
@@ -0,0 +1,281 @@
|
2020-07-02 21:19:52 +00:00
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
|
|
|
+ * Surface Integration Driver.
|
|
|
|
+ * MFD driver to provide device/model dependent functionality.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <linux/acpi.h>
|
|
|
|
+#include <linux/kernel.h>
|
|
|
|
+#include <linux/module.h>
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
+#include <linux/mfd/core.h>
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+#include "surface_sam_sid_power.h"
|
|
|
|
+#include "surface_sam_sid_vhf.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssam_battery_properties ssam_battery_props_bat1 = {
|
|
|
|
+ .registry = SSAM_EVENT_REGISTRY_SAM,
|
|
|
|
+ .num = 0,
|
|
|
|
+ .channel = 1,
|
|
|
|
+ .instance = 1,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssam_battery_properties ssam_battery_props_bat2_sb3 = {
|
|
|
|
+ .registry = SSAM_EVENT_REGISTRY_KIP,
|
|
|
|
+ .num = 1,
|
|
|
|
+ .channel = 2,
|
|
|
|
+ .instance = 1,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
2020-07-15 20:26:02 +00:00
|
|
|
+static const struct ssam_hid_properties ssam_hid_props_keyboard = {
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .registry = SSAM_EVENT_REGISTRY_REG,
|
|
|
|
+ .instance = 1,
|
|
|
|
+};
|
|
|
|
+
|
2020-07-15 20:26:02 +00:00
|
|
|
+static const struct ssam_hid_properties ssam_hid_props_touchpad = {
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .registry = SSAM_EVENT_REGISTRY_REG,
|
|
|
|
+ .instance = 3,
|
|
|
|
+};
|
|
|
|
+
|
2020-07-15 20:26:02 +00:00
|
|
|
+static const struct ssam_hid_properties ssam_hid_props_iid5 = {
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .registry = SSAM_EVENT_REGISTRY_REG,
|
|
|
|
+ .instance = 5,
|
|
|
|
+};
|
|
|
|
+
|
2020-07-15 20:26:02 +00:00
|
|
|
+static const struct ssam_hid_properties ssam_hid_props_iid6 = {
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .registry = SSAM_EVENT_REGISTRY_REG,
|
|
|
|
+ .instance = 6,
|
|
|
|
+};
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+static const struct mfd_cell sid_devs_sp4[] = {
|
2020-05-16 22:20:04 +00:00
|
|
|
+ { .name = "surface_sam_sid_gpelid", .id = -1 },
|
|
|
|
+ { .name = "surface_sam_sid_perfmode", .id = -1 },
|
2019-12-10 00:26:31 +00:00
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static const struct mfd_cell sid_devs_sp6[] = {
|
|
|
|
+ { .name = "surface_sam_sid_gpelid", .id = -1 },
|
|
|
|
+ { .name = "surface_sam_sid_perfmode", .id = -1 },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+static const struct mfd_cell sid_devs_sp7[] = {
|
2020-05-16 22:20:04 +00:00
|
|
|
+ { .name = "surface_sam_sid_gpelid", .id = -1 },
|
|
|
|
+ { .name = "surface_sam_sid_perfmode", .id = -1 },
|
2020-07-13 14:07:25 +00:00
|
|
|
+ { .name = "surface_sam_sid_ac", .id = -1 },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_battery",
|
|
|
|
+ .id = -1,
|
|
|
|
+ .platform_data = &ssam_battery_props_bat1,
|
|
|
|
+ .pdata_size = sizeof(struct ssam_battery_properties),
|
|
|
|
+ },
|
2019-12-10 00:26:31 +00:00
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct mfd_cell sid_devs_sb1[] = {
|
|
|
|
+ { .name = "surface_sam_sid_gpelid", .id = -1 },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct mfd_cell sid_devs_sb2[] = {
|
|
|
|
+ { .name = "surface_sam_sid_gpelid", .id = -1 },
|
|
|
|
+ { .name = "surface_sam_sid_perfmode", .id = -1 },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+static const struct mfd_cell sid_devs_sb3[] = {
|
|
|
|
+ { .name = "surface_sam_sid_gpelid", .id = -1 },
|
|
|
|
+ { .name = "surface_sam_sid_perfmode", .id = -1 },
|
|
|
|
+ { .name = "surface_sam_sid_ac", .id = -1 },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_battery",
|
|
|
|
+ .id = 1,
|
|
|
|
+ .platform_data = &ssam_battery_props_bat1,
|
|
|
|
+ .pdata_size = sizeof(struct ssam_battery_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_battery",
|
|
|
|
+ .id = 2,
|
|
|
|
+ .platform_data = &ssam_battery_props_bat2_sb3,
|
|
|
|
+ .pdata_size = sizeof(struct ssam_battery_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
|
|
|
+ .id = 1,
|
2020-07-15 20:26:02 +00:00
|
|
|
+ .platform_data = (void *)&ssam_hid_props_keyboard,
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
|
|
|
+ .id = 3,
|
2020-07-15 20:26:02 +00:00
|
|
|
+ .platform_data = (void *)&ssam_hid_props_touchpad,
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
|
|
|
+ .id = 5,
|
2020-07-15 20:26:02 +00:00
|
|
|
+ .platform_data = (void *)&ssam_hid_props_iid5,
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
|
|
|
+ .id = 6,
|
2020-07-15 20:26:02 +00:00
|
|
|
+ .platform_data = (void *)&ssam_hid_props_iid6,
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+static const struct mfd_cell sid_devs_sl1[] = {
|
|
|
|
+ { .name = "surface_sam_sid_gpelid", .id = -1 },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct mfd_cell sid_devs_sl2[] = {
|
|
|
|
+ { .name = "surface_sam_sid_gpelid", .id = -1 },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct mfd_cell sid_devs_sl3_13[] = {
|
2020-05-16 22:20:04 +00:00
|
|
|
+ { .name = "surface_sam_sid_gpelid", .id = -1 },
|
|
|
|
+ { .name = "surface_sam_sid_perfmode", .id = -1 },
|
2020-07-13 14:07:25 +00:00
|
|
|
+ { .name = "surface_sam_sid_ac", .id = -1 },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_battery",
|
|
|
|
+ .id = -1,
|
|
|
|
+ .platform_data = &ssam_battery_props_bat1,
|
|
|
|
+ .pdata_size = sizeof(struct ssam_battery_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
2020-07-15 20:26:02 +00:00
|
|
|
+ .id = 1,
|
|
|
|
+ .platform_data = (void *)&ssam_hid_props_keyboard,
|
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
|
|
|
+ .id = 3,
|
|
|
|
+ .platform_data = (void *)&ssam_hid_props_touchpad,
|
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
|
|
|
+ .id = 5,
|
|
|
|
+ .platform_data = (void *)&ssam_hid_props_iid5,
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
2019-12-10 00:26:31 +00:00
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct mfd_cell sid_devs_sl3_15[] = {
|
2020-07-02 21:19:52 +00:00
|
|
|
+ { .name = "surface_sam_sid_perfmode", .id = -1 },
|
2020-07-13 14:07:25 +00:00
|
|
|
+ { .name = "surface_sam_sid_ac", .id = -1 },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_battery",
|
|
|
|
+ .id = -1,
|
|
|
|
+ .platform_data = &ssam_battery_props_bat1,
|
|
|
|
+ .pdata_size = sizeof(struct ssam_battery_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
2020-07-15 20:26:02 +00:00
|
|
|
+ .id = 1,
|
|
|
|
+ .platform_data = (void *)&ssam_hid_props_keyboard,
|
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
|
|
|
+ .id = 3,
|
|
|
|
+ .platform_data = (void *)&ssam_hid_props_touchpad,
|
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
|
|
|
+ .id = 5,
|
|
|
|
+ .platform_data = (void *)&ssam_hid_props_iid5,
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .pdata_size = sizeof(struct ssam_hid_properties),
|
|
|
|
+ },
|
2019-12-10 00:26:31 +00:00
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct acpi_device_id surface_sam_sid_match[] = {
|
2020-05-16 22:20:04 +00:00
|
|
|
+ /* Surface Pro 4, 5, and 6 */
|
|
|
|
+ { "MSHW0081", (unsigned long)sid_devs_sp4 },
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ /* Surface Pro 6 (OMBR >= 0x10) */
|
|
|
|
+ { "MSHW0111", (unsigned long)sid_devs_sp6 },
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ /* Surface Pro 7 */
|
|
|
|
+ { "MSHW0116", (unsigned long)sid_devs_sp7 },
|
|
|
|
+
|
|
|
|
+ /* Surface Book 1 */
|
|
|
|
+ { "MSHW0080", (unsigned long)sid_devs_sb1 },
|
|
|
|
+
|
|
|
|
+ /* Surface Book 2 */
|
|
|
|
+ { "MSHW0107", (unsigned long)sid_devs_sb2 },
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ /* Surface Book 3 */
|
|
|
|
+ { "MSHW0117", (unsigned long)sid_devs_sb3 },
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ /* Surface Laptop 1 */
|
|
|
|
+ { "MSHW0086", (unsigned long)sid_devs_sl1 },
|
|
|
|
+
|
|
|
|
+ /* Surface Laptop 2 */
|
|
|
|
+ { "MSHW0112", (unsigned long)sid_devs_sl2 },
|
|
|
|
+
|
|
|
|
+ /* Surface Laptop 3 (13") */
|
|
|
|
+ { "MSHW0114", (unsigned long)sid_devs_sl3_13 },
|
|
|
|
+
|
|
|
|
+ /* Surface Laptop 3 (15") */
|
|
|
|
+ { "MSHW0110", (unsigned long)sid_devs_sl3_15 },
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+MODULE_DEVICE_TABLE(acpi, surface_sam_sid_match);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_probe(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ const struct acpi_device_id *match;
|
|
|
|
+ const struct mfd_cell *cells, *p;
|
|
|
|
+
|
|
|
|
+ match = acpi_match_device(surface_sam_sid_match, &pdev->dev);
|
|
|
|
+ if (!match)
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
|
|
|
+ cells = (struct mfd_cell *)match->driver_data;
|
|
|
|
+ if (!cells)
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
|
|
|
+ for (p = cells; p->name; ++p) {
|
|
|
|
+ /* just count */
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (p == cells)
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
|
|
|
+ return mfd_add_devices(&pdev->dev, 0, cells, p - cells, NULL, 0, NULL);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_remove(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ mfd_remove_devices(&pdev->dev);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct platform_driver surface_sam_sid = {
|
|
|
|
+ .probe = surface_sam_sid_probe,
|
|
|
|
+ .remove = surface_sam_sid_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_sid",
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .acpi_match_table = surface_sam_sid_match,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+module_platform_driver(surface_sam_sid);
|
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
|
|
|
|
+MODULE_DESCRIPTION("Surface Integration Driver for 5th Generation Surface Devices");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-10 00:26:31 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_gpelid.c b/drivers/platform/x86/surface_sam/surface_sam_sid_gpelid.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..f0cee43c859b
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_sid_gpelid.c
|
2020-07-13 14:07:25 +00:00
|
|
|
@@ -0,0 +1,232 @@
|
2020-07-02 21:19:52 +00:00
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
|
|
|
+ * Surface Lid driver to enable wakeup from suspend via the lid.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <linux/acpi.h>
|
|
|
|
+#include <linux/dmi.h>
|
|
|
|
+#include <linux/kernel.h>
|
|
|
|
+#include <linux/module.h>
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct sid_lid_device {
|
|
|
|
+ const char *acpi_path;
|
|
|
|
+ const u32 gpe_number;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static const struct sid_lid_device lid_device_l17 = {
|
|
|
|
+ .acpi_path = "\\_SB.LID0",
|
|
|
|
+ .gpe_number = 0x17,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct sid_lid_device lid_device_l4D = {
|
|
|
|
+ .acpi_path = "\\_SB.LID0",
|
|
|
|
+ .gpe_number = 0x4D,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct sid_lid_device lid_device_l4F = {
|
|
|
|
+ .acpi_path = "\\_SB.LID0",
|
|
|
|
+ .gpe_number = 0x4F,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static const struct sid_lid_device lid_device_l57 = {
|
|
|
|
+ .acpi_path = "\\_SB.LID0",
|
|
|
|
+ .gpe_number = 0x57,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static const struct dmi_system_id dmi_lid_device_table[] = {
|
|
|
|
+ {
|
|
|
|
+ .ident = "Surface Pro 4",
|
|
|
|
+ .matches = {
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 4"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l17,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .ident = "Surface Pro 5",
|
|
|
|
+ .matches = {
|
|
|
|
+ /* match for SKU here due to generic product name "Surface Pro" */
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1796"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l4F,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .ident = "Surface Pro 5 (LTE)",
|
|
|
|
+ .matches = {
|
|
|
|
+ /* match for SKU here due to generic product name "Surface Pro" */
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Pro_1807"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l4F,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .ident = "Surface Pro 6",
|
|
|
|
+ .matches = {
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 6"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l4F,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .ident = "Surface Pro 7",
|
|
|
|
+ .matches = {
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Pro 7"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l4D,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .ident = "Surface Book 1",
|
|
|
|
+ .matches = {
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l17,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .ident = "Surface Book 2",
|
|
|
|
+ .matches = {
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 2"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l17,
|
|
|
|
+ },
|
|
|
|
+ {
|
2020-07-13 14:07:25 +00:00
|
|
|
+ .ident = "Surface Book 3",
|
|
|
|
+ .matches = {
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Book 3"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l4D,
|
|
|
|
+ },
|
|
|
|
+ {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .ident = "Surface Laptop 1",
|
|
|
|
+ .matches = {
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l57,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .ident = "Surface Laptop 2",
|
|
|
|
+ .matches = {
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Surface Laptop 2"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l57,
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ .ident = "Surface Laptop 3 (13\")",
|
|
|
|
+ .matches = {
|
|
|
|
+ DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
|
|
|
|
+ DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "Surface_Laptop_3_1867:1868"),
|
|
|
|
+ },
|
|
|
|
+ .driver_data = (void *)&lid_device_l4D,
|
|
|
|
+ },
|
|
|
|
+ { }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int sid_lid_enable_wakeup(const struct sid_lid_device *dev, bool enable)
|
|
|
|
+{
|
|
|
|
+ int action = enable ? ACPI_GPE_ENABLE : ACPI_GPE_DISABLE;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = acpi_set_gpe_wake_mask(NULL, dev->gpe_number, action);
|
|
|
|
+ if (status)
|
|
|
|
+ return -EFAULT;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_gpelid_suspend(struct device *dev)
|
|
|
|
+{
|
2020-05-16 22:20:04 +00:00
|
|
|
+ const struct sid_lid_device *ldev;
|
|
|
|
+
|
|
|
|
+ ldev = dev_get_drvdata(dev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return sid_lid_enable_wakeup(ldev, true);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_gpelid_resume(struct device *dev)
|
|
|
|
+{
|
2020-05-16 22:20:04 +00:00
|
|
|
+ const struct sid_lid_device *ldev;
|
|
|
|
+
|
|
|
|
+ ldev = dev_get_drvdata(dev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return sid_lid_enable_wakeup(ldev, false);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static SIMPLE_DEV_PM_OPS(surface_sam_sid_gpelid_pm,
|
2020-05-16 22:20:04 +00:00
|
|
|
+ surface_sam_sid_gpelid_suspend,
|
|
|
|
+ surface_sam_sid_gpelid_resume);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_gpelid_probe(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ const struct dmi_system_id *match;
|
2020-05-16 22:20:04 +00:00
|
|
|
+ struct sid_lid_device *dev;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ acpi_handle lid_handle;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ match = dmi_first_match(dmi_lid_device_table);
|
|
|
|
+ if (!match)
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ dev = match->driver_data;
|
|
|
|
+ if (!dev)
|
|
|
|
+ return -ENODEV;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ status = acpi_get_handle(NULL, (acpi_string)dev->acpi_path, &lid_handle);
|
|
|
|
+ if (status)
|
|
|
|
+ return -EFAULT;
|
|
|
|
+
|
|
|
|
+ status = acpi_setup_gpe_for_wake(lid_handle, NULL, dev->gpe_number);
|
|
|
|
+ if (status)
|
|
|
|
+ return -EFAULT;
|
|
|
|
+
|
|
|
|
+ status = acpi_enable_gpe(NULL, dev->gpe_number);
|
|
|
|
+ if (status)
|
|
|
|
+ return -EFAULT;
|
|
|
|
+
|
|
|
|
+ status = sid_lid_enable_wakeup(dev, false);
|
|
|
|
+ if (status) {
|
|
|
|
+ acpi_disable_gpe(NULL, dev->gpe_number);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ return status;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ platform_set_drvdata(pdev, dev);
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_gpelid_remove(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct sid_lid_device *dev = platform_get_drvdata(pdev);
|
|
|
|
+
|
|
|
|
+ /* restore default behavior without this module */
|
|
|
|
+ sid_lid_enable_wakeup(dev, false);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ acpi_disable_gpe(NULL, dev->gpe_number);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ platform_set_drvdata(pdev, NULL);
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct platform_driver surface_sam_sid_gpelid = {
|
|
|
|
+ .probe = surface_sam_sid_gpelid_probe,
|
|
|
|
+ .remove = surface_sam_sid_gpelid_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_sid_gpelid",
|
|
|
|
+ .pm = &surface_sam_sid_gpelid_pm,
|
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+module_platform_driver(surface_sam_sid_gpelid);
|
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
|
|
|
|
+MODULE_DESCRIPTION("Surface Lid Driver for 5th Generation Surface Devices");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-10 00:26:31 +00:00
|
|
|
+MODULE_ALIAS("platform:surface_sam_sid_gpelid");
|
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_perfmode.c b/drivers/platform/x86/surface_sam/surface_sam_sid_perfmode.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..2e11efb166f2
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_sid_perfmode.c
|
2020-05-16 22:20:04 +00:00
|
|
|
@@ -0,0 +1,216 @@
|
2020-07-02 21:19:52 +00:00
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
|
|
|
+ * Surface Performance Mode Driver.
|
|
|
|
+ * Allows to change cooling capabilities based on user preference.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <asm/unaligned.h>
|
|
|
|
+#include <linux/kernel.h>
|
|
|
|
+#include <linux/module.h>
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
+
|
|
|
|
+#include "surface_sam_ssh.h"
|
|
|
|
+
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+#define SID_PARAM_PERM 0644
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+enum sam_perf_mode {
|
|
|
|
+ SAM_PERF_MODE_NORMAL = 1,
|
|
|
|
+ SAM_PERF_MODE_BATTERY = 2,
|
|
|
|
+ SAM_PERF_MODE_PERF1 = 3,
|
|
|
|
+ SAM_PERF_MODE_PERF2 = 4,
|
|
|
|
+
|
|
|
|
+ __SAM_PERF_MODE__START = 1,
|
|
|
|
+ __SAM_PERF_MODE__END = 4,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+enum sid_param_perf_mode {
|
|
|
|
+ SID_PARAM_PERF_MODE_AS_IS = 0,
|
|
|
|
+ SID_PARAM_PERF_MODE_NORMAL = SAM_PERF_MODE_NORMAL,
|
|
|
|
+ SID_PARAM_PERF_MODE_BATTERY = SAM_PERF_MODE_BATTERY,
|
|
|
|
+ SID_PARAM_PERF_MODE_PERF1 = SAM_PERF_MODE_PERF1,
|
|
|
|
+ SID_PARAM_PERF_MODE_PERF2 = SAM_PERF_MODE_PERF2,
|
|
|
|
+
|
|
|
|
+ __SID_PARAM_PERF_MODE__START = 0,
|
|
|
|
+ __SID_PARAM_PERF_MODE__END = 4,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_sam_perf_mode_get(void)
|
|
|
|
+{
|
|
|
|
+ u8 result_buf[8] = { 0 };
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
|
|
|
+ .tc = 0x03,
|
|
|
|
+ .cid = 0x02,
|
|
|
|
+ .iid = 0x00,
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .chn = 0x01,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .snc = 0x01,
|
|
|
|
+ .cdl = 0x00,
|
|
|
|
+ .pld = NULL,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_buf result = {
|
|
|
|
+ .cap = ARRAY_SIZE(result_buf),
|
|
|
|
+ .len = 0,
|
|
|
|
+ .data = result_buf,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_rqst(&rqst, &result);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (result.len != 8)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -EFAULT;
|
|
|
|
+
|
|
|
|
+ return get_unaligned_le32(&result.data[0]);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_perf_mode_set(int perf_mode)
|
|
|
|
+{
|
|
|
|
+ u8 payload[4] = { 0 };
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
|
|
|
+ .tc = 0x03,
|
|
|
|
+ .cid = 0x03,
|
|
|
|
+ .iid = 0x00,
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .chn = 0x01,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .snc = 0x00,
|
|
|
|
+ .cdl = ARRAY_SIZE(payload),
|
|
|
|
+ .pld = payload,
|
|
|
|
+ };
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (perf_mode < __SAM_PERF_MODE__START || perf_mode > __SAM_PERF_MODE__END)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ put_unaligned_le32(perf_mode, &rqst.pld[0]);
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, NULL);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int param_perf_mode_set(const char *val, const struct kernel_param *kp)
|
|
|
|
+{
|
|
|
|
+ int perf_mode;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = kstrtoint(val, 0, &perf_mode);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (perf_mode < __SID_PARAM_PERF_MODE__START || perf_mode > __SID_PARAM_PERF_MODE__END)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ return param_set_int(val, kp);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static const struct kernel_param_ops param_perf_mode_ops = {
|
|
|
|
+ .set = param_perf_mode_set,
|
|
|
|
+ .get = param_get_int,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static int param_perf_mode_init = SID_PARAM_PERF_MODE_AS_IS;
|
|
|
|
+static int param_perf_mode_exit = SID_PARAM_PERF_MODE_AS_IS;
|
|
|
|
+
|
|
|
|
+module_param_cb(perf_mode_init, ¶m_perf_mode_ops, ¶m_perf_mode_init, SID_PARAM_PERM);
|
|
|
|
+module_param_cb(perf_mode_exit, ¶m_perf_mode_ops, ¶m_perf_mode_exit, SID_PARAM_PERM);
|
|
|
|
+
|
|
|
|
+MODULE_PARM_DESC(perf_mode_init, "Performance-mode to be set on module initialization");
|
|
|
|
+MODULE_PARM_DESC(perf_mode_exit, "Performance-mode to be set on module exit");
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static ssize_t perf_mode_show(struct device *dev, struct device_attribute *attr, char *data)
|
|
|
|
+{
|
|
|
|
+ int perf_mode;
|
|
|
|
+
|
|
|
|
+ perf_mode = surface_sam_perf_mode_get();
|
|
|
|
+ if (perf_mode < 0) {
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_err(dev, "failed to get current performance mode: %d\n", perf_mode);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -EIO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return sprintf(data, "%d\n", perf_mode);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static ssize_t perf_mode_store(struct device *dev, struct device_attribute *attr,
|
2020-05-16 22:20:04 +00:00
|
|
|
+ const char *data, size_t count)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ int perf_mode;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = kstrtoint(data, 0, &perf_mode);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ status = surface_sam_perf_mode_set(perf_mode);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ // TODO: Should we notify ACPI here?
|
|
|
|
+ //
|
|
|
|
+ // There is a _DSM call described as
|
|
|
|
+ // WSID._DSM: Notify DPTF on Slider State change
|
|
|
|
+ // which calls
|
|
|
|
+ // ODV3 = ToInteger (Arg3)
|
|
|
|
+ // Notify(IETM, 0x88)
|
|
|
|
+ // IETM is an INT3400 Intel Dynamic Power Performance Management
|
|
|
|
+ // device, part of the DPTF framework. From the corresponding
|
|
|
|
+ // kernel driver, it looks like event 0x88 is being ignored. Also
|
|
|
|
+ // it is currently unknown what the consequecnes of setting ODV3
|
|
|
|
+ // are.
|
|
|
|
+
|
|
|
|
+ return count;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static const DEVICE_ATTR_RW(perf_mode);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_perfmode_probe(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // link to ec
|
|
|
|
+ status = surface_sam_ssh_consumer_register(&pdev->dev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status == -ENXIO ? -EPROBE_DEFER : status;
|
|
|
|
+
|
|
|
|
+ // set initial perf_mode
|
|
|
|
+ if (param_perf_mode_init != SID_PARAM_PERF_MODE_AS_IS) {
|
|
|
|
+ status = surface_sam_perf_mode_set(param_perf_mode_init);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // register perf_mode attribute
|
|
|
|
+ status = sysfs_create_file(&pdev->dev.kobj, &dev_attr_perf_mode.attr);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_sysfs;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+err_sysfs:
|
|
|
|
+ surface_sam_perf_mode_set(param_perf_mode_exit);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_perfmode_remove(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ sysfs_remove_file(&pdev->dev.kobj, &dev_attr_perf_mode.attr);
|
|
|
|
+ surface_sam_perf_mode_set(param_perf_mode_exit);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct platform_driver surface_sam_sid_perfmode = {
|
|
|
|
+ .probe = surface_sam_sid_perfmode_probe,
|
|
|
|
+ .remove = surface_sam_sid_perfmode_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_sid_perfmode",
|
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+module_platform_driver(surface_sam_sid_perfmode);
|
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
|
|
|
|
+MODULE_DESCRIPTION("Surface Performance Mode Driver for 5th Generation Surface Devices");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-10 00:26:31 +00:00
|
|
|
+MODULE_ALIAS("platform:surface_sam_sid_perfmode");
|
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_power.c b/drivers/platform/x86/surface_sam/surface_sam_sid_power.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..1d945c0a911a
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_sid_power.c
|
2020-07-13 14:07:25 +00:00
|
|
|
@@ -0,0 +1,1154 @@
|
2020-07-02 21:19:52 +00:00
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
|
|
|
+ * Surface SID Battery/AC Driver.
|
|
|
|
+ * Provides support for the battery and AC on 7th generation Surface devices.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <linux/kernel.h>
|
|
|
|
+#include <linux/delay.h>
|
|
|
|
+#include <linux/jiffies.h>
|
|
|
|
+#include <linux/module.h>
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
+#include <linux/power_supply.h>
|
|
|
|
+#include <linux/workqueue.h>
|
|
|
|
+
|
|
|
|
+#include "surface_sam_ssh.h"
|
2020-07-13 14:07:25 +00:00
|
|
|
+#include "surface_sam_sid_power.h"
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+#define SPWR_WARN KERN_WARNING KBUILD_MODNAME ": "
|
|
|
|
+#define SPWR_DEBUG KERN_DEBUG KBUILD_MODNAME ": "
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+// TODO: check BIX/BST for unknown/unsupported 0xffffffff entries
|
|
|
|
+// TODO: DPTF (/SAN notifications)?
|
|
|
|
+// TODO: other properties?
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static unsigned int cache_time = 1000;
|
|
|
|
+module_param(cache_time, uint, 0644);
|
|
|
|
+MODULE_PARM_DESC(cache_time, "battery state chaching time in milliseconds [default: 1000]");
|
|
|
|
+
|
|
|
|
+#define SPWR_AC_BAT_UPDATE_DELAY msecs_to_jiffies(5000)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * SAM Interface.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#define SAM_PWR_TC 0x02
|
|
|
|
+
|
|
|
|
+#define SAM_RQST_PWR_CID_STA 0x01
|
|
|
|
+#define SAM_RQST_PWR_CID_BIX 0x02
|
|
|
|
+#define SAM_RQST_PWR_CID_BST 0x03
|
|
|
|
+#define SAM_RQST_PWR_CID_BTP 0x04
|
|
|
|
+
|
|
|
|
+#define SAM_RQST_PWR_CID_PMAX 0x0b
|
|
|
|
+#define SAM_RQST_PWR_CID_PSOC 0x0c
|
|
|
|
+#define SAM_RQST_PWR_CID_PSRC 0x0d
|
|
|
|
+#define SAM_RQST_PWR_CID_CHGI 0x0e
|
|
|
|
+#define SAM_RQST_PWR_CID_ARTG 0x0f
|
|
|
|
+
|
|
|
|
+#define SAM_EVENT_PWR_CID_BIX 0x15
|
|
|
|
+#define SAM_EVENT_PWR_CID_BST 0x16
|
|
|
|
+#define SAM_EVENT_PWR_CID_ADAPTER 0x17
|
|
|
|
+
|
|
|
|
+#define SAM_BATTERY_STA_OK 0x0f
|
|
|
|
+#define SAM_BATTERY_STA_PRESENT 0x10
|
|
|
|
+
|
|
|
|
+#define SAM_BATTERY_STATE_DISCHARGING 0x01
|
|
|
|
+#define SAM_BATTERY_STATE_CHARGING 0x02
|
|
|
|
+#define SAM_BATTERY_STATE_CRITICAL 0x04
|
|
|
|
+
|
|
|
|
+#define SAM_BATTERY_POWER_UNIT_MA 1
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* Equivalent to data returned in ACPI _BIX method */
|
|
|
|
+struct spwr_bix {
|
|
|
|
+ u8 revision;
|
|
|
|
+ u32 power_unit;
|
|
|
|
+ u32 design_cap;
|
|
|
|
+ u32 last_full_charge_cap;
|
|
|
|
+ u32 technology;
|
|
|
|
+ u32 design_voltage;
|
|
|
|
+ u32 design_cap_warn;
|
|
|
|
+ u32 design_cap_low;
|
|
|
|
+ u32 cycle_count;
|
|
|
|
+ u32 measurement_accuracy;
|
|
|
|
+ u32 max_sampling_time;
|
|
|
|
+ u32 min_sampling_time;
|
|
|
|
+ u32 max_avg_interval;
|
|
|
|
+ u32 min_avg_interval;
|
|
|
|
+ u32 bat_cap_granularity_1;
|
|
|
|
+ u32 bat_cap_granularity_2;
|
|
|
|
+ u8 model[21];
|
|
|
|
+ u8 serial[11];
|
|
|
|
+ u8 type[5];
|
|
|
|
+ u8 oem_info[21];
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+/* Equivalent to data returned in ACPI _BST method */
|
|
|
|
+struct spwr_bst {
|
|
|
|
+ u32 state;
|
|
|
|
+ u32 present_rate;
|
|
|
|
+ u32 remaining_cap;
|
|
|
|
+ u32 present_voltage;
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+/* DPTF event payload */
|
|
|
|
+struct spwr_event_dptf {
|
|
|
|
+ u32 pmax;
|
|
|
|
+ u32 _1; /* currently unknown */
|
|
|
|
+ u32 _2; /* currently unknown */
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* Get battery status (_STA) */
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int sam_psy_get_sta(u8 channel, u8 instance, u32 *sta)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+ struct surface_sam_ssh_buf result;
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_PWR_TC;
|
|
|
|
+ rqst.cid = SAM_RQST_PWR_CID_STA;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = instance;
|
|
|
|
+ rqst.chn = channel;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = 0x01;
|
|
|
|
+ rqst.cdl = 0x00;
|
|
|
|
+ rqst.pld = NULL;
|
|
|
|
+
|
|
|
|
+ result.cap = sizeof(u32);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = (u8 *)sta;
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Get battery static information (_BIX) */
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int sam_psy_get_bix(u8 channel, u8 instance, struct spwr_bix *bix)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+ struct surface_sam_ssh_buf result;
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_PWR_TC;
|
|
|
|
+ rqst.cid = SAM_RQST_PWR_CID_BIX;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = instance;
|
|
|
|
+ rqst.chn = channel;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = 0x01;
|
|
|
|
+ rqst.cdl = 0x00;
|
|
|
|
+ rqst.pld = NULL;
|
|
|
|
+
|
|
|
|
+ result.cap = sizeof(struct spwr_bix);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = (u8 *)bix;
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Get battery dynamic information (_BST) */
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int sam_psy_get_bst(u8 channel, u8 instance, struct spwr_bst *bst)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+ struct surface_sam_ssh_buf result;
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_PWR_TC;
|
|
|
|
+ rqst.cid = SAM_RQST_PWR_CID_BST;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = instance;
|
|
|
|
+ rqst.chn = channel;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = 0x01;
|
|
|
|
+ rqst.cdl = 0x00;
|
|
|
|
+ rqst.pld = NULL;
|
|
|
|
+
|
|
|
|
+ result.cap = sizeof(struct spwr_bst);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = (u8 *)bst;
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Set battery trip point (_BTP) */
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int sam_psy_set_btp(u8 channel, u8 instance, u32 btp)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_PWR_TC;
|
|
|
|
+ rqst.cid = SAM_RQST_PWR_CID_BTP;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = instance;
|
|
|
|
+ rqst.chn = channel;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = 0x00;
|
|
|
|
+ rqst.cdl = sizeof(u32);
|
|
|
|
+ rqst.pld = (u8 *)&btp;
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, NULL);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Get platform power soruce for battery (DPTF PSRC) */
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int sam_psy_get_psrc(u8 channel, u8 instance, u32 *psrc)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+ struct surface_sam_ssh_buf result;
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_PWR_TC;
|
|
|
|
+ rqst.cid = SAM_RQST_PWR_CID_PSRC;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = instance;
|
|
|
|
+ rqst.chn = channel;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = 0x01;
|
|
|
|
+ rqst.cdl = 0x00;
|
|
|
|
+ rqst.pld = NULL;
|
|
|
|
+
|
|
|
|
+ result.cap = sizeof(u32);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = (u8 *)psrc;
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Get maximum platform power for battery (DPTF PMAX) */
|
|
|
|
+__always_unused
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int sam_psy_get_pmax(u8 channel, u8 instance, u32 *pmax)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+ struct surface_sam_ssh_buf result;
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_PWR_TC;
|
|
|
|
+ rqst.cid = SAM_RQST_PWR_CID_PMAX;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = instance;
|
|
|
|
+ rqst.chn = channel;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = 0x01;
|
|
|
|
+ rqst.cdl = 0x00;
|
|
|
|
+ rqst.pld = NULL;
|
|
|
|
+
|
|
|
|
+ result.cap = sizeof(u32);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = (u8 *)pmax;
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Get adapter rating (DPTF ARTG) */
|
|
|
|
+__always_unused
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int sam_psy_get_artg(u8 channel, u8 instance, u32 *artg)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+ struct surface_sam_ssh_buf result;
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_PWR_TC;
|
|
|
|
+ rqst.cid = SAM_RQST_PWR_CID_ARTG;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = instance;
|
|
|
|
+ rqst.chn = channel;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = 0x01;
|
|
|
|
+ rqst.cdl = 0x00;
|
|
|
|
+ rqst.pld = NULL;
|
|
|
|
+
|
|
|
|
+ result.cap = sizeof(u32);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = (u8 *)artg;
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Unknown (DPTF PSOC) */
|
|
|
|
+__always_unused
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int sam_psy_get_psoc(u8 channel, u8 instance, u32 *psoc)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+ struct surface_sam_ssh_buf result;
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_PWR_TC;
|
|
|
|
+ rqst.cid = SAM_RQST_PWR_CID_PSOC;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = instance;
|
|
|
|
+ rqst.chn = channel;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = 0x01;
|
|
|
|
+ rqst.cdl = 0x00;
|
|
|
|
+ rqst.pld = NULL;
|
|
|
|
+
|
|
|
|
+ result.cap = sizeof(u32);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = (u8 *)psoc;
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/* Unknown (DPTF CHGI/ INT3403 SPPC) */
|
|
|
|
+__always_unused
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int sam_psy_set_chgi(u8 channel, u8 instance, u32 chgi)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_PWR_TC;
|
|
|
|
+ rqst.cid = SAM_RQST_PWR_CID_CHGI;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = instance;
|
|
|
|
+ rqst.chn = channel;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.snc = 0x00;
|
|
|
|
+ rqst.cdl = sizeof(u32);
|
|
|
|
+ rqst.pld = (u8 *)&chgi;
|
|
|
|
+
|
|
|
|
+ return surface_sam_ssh_rqst(&rqst, NULL);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * Common Power-Subsystem Interface.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+struct spwr_battery_device {
|
|
|
|
+ struct platform_device *pdev;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ const struct ssam_battery_properties *p;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ char name[32];
|
|
|
|
+ struct power_supply *psy;
|
|
|
|
+ struct power_supply_desc psy_desc;
|
|
|
|
+
|
|
|
|
+ struct delayed_work update_work;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct ssam_event_notifier notif;
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+ struct mutex lock;
|
|
|
|
+ unsigned long timestamp;
|
|
|
|
+
|
|
|
|
+ u32 sta;
|
|
|
|
+ struct spwr_bix bix;
|
|
|
|
+ struct spwr_bst bst;
|
|
|
|
+ u32 alarm;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct spwr_ac_device {
|
|
|
|
+ struct platform_device *pdev;
|
|
|
|
+
|
|
|
|
+ char name[32];
|
|
|
|
+ struct power_supply *psy;
|
|
|
|
+ struct power_supply_desc psy_desc;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct ssam_event_notifier notif;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ struct mutex lock;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u32 state;
|
2019-12-10 00:26:31 +00:00
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static enum power_supply_property spwr_ac_props[] = {
|
|
|
|
+ POWER_SUPPLY_PROP_ONLINE,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static enum power_supply_property spwr_battery_props_chg[] = {
|
|
|
|
+ POWER_SUPPLY_PROP_STATUS,
|
|
|
|
+ POWER_SUPPLY_PROP_PRESENT,
|
|
|
|
+ POWER_SUPPLY_PROP_TECHNOLOGY,
|
|
|
|
+ POWER_SUPPLY_PROP_CYCLE_COUNT,
|
|
|
|
+ POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
|
|
|
|
+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
|
|
+ POWER_SUPPLY_PROP_CURRENT_NOW,
|
|
|
|
+ POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
|
|
|
|
+ POWER_SUPPLY_PROP_CHARGE_FULL,
|
|
|
|
+ POWER_SUPPLY_PROP_CHARGE_NOW,
|
|
|
|
+ POWER_SUPPLY_PROP_CAPACITY,
|
|
|
|
+ POWER_SUPPLY_PROP_CAPACITY_LEVEL,
|
|
|
|
+ POWER_SUPPLY_PROP_MODEL_NAME,
|
|
|
|
+ POWER_SUPPLY_PROP_MANUFACTURER,
|
|
|
|
+ POWER_SUPPLY_PROP_SERIAL_NUMBER,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static enum power_supply_property spwr_battery_props_eng[] = {
|
|
|
|
+ POWER_SUPPLY_PROP_STATUS,
|
|
|
|
+ POWER_SUPPLY_PROP_PRESENT,
|
|
|
|
+ POWER_SUPPLY_PROP_TECHNOLOGY,
|
|
|
|
+ POWER_SUPPLY_PROP_CYCLE_COUNT,
|
|
|
|
+ POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
|
|
|
|
+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
|
|
|
|
+ POWER_SUPPLY_PROP_POWER_NOW,
|
|
|
|
+ POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
|
|
|
|
+ POWER_SUPPLY_PROP_ENERGY_FULL,
|
|
|
|
+ POWER_SUPPLY_PROP_ENERGY_NOW,
|
|
|
|
+ POWER_SUPPLY_PROP_CAPACITY,
|
|
|
|
+ POWER_SUPPLY_PROP_CAPACITY_LEVEL,
|
|
|
|
+ POWER_SUPPLY_PROP_MODEL_NAME,
|
|
|
|
+ POWER_SUPPLY_PROP_MANUFACTURER,
|
|
|
|
+ POWER_SUPPLY_PROP_SERIAL_NUMBER,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int spwr_battery_register(struct spwr_battery_device *bat,
|
|
|
|
+ struct platform_device *pdev,
|
|
|
|
+ const struct ssam_battery_properties *p);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void spwr_battery_unregister(struct spwr_battery_device *bat);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline bool spwr_battery_present(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ return bat->sta & SAM_BATTERY_STA_PRESENT;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_load_sta(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-13 14:07:25 +00:00
|
|
|
+ return sam_psy_get_sta(bat->p->channel, bat->p->instance, &bat->sta);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_load_bix(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ if (!spwr_battery_present(bat))
|
|
|
|
+ return 0;
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ return sam_psy_get_bix(bat->p->channel, bat->p->instance, &bat->bix);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_load_bst(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ if (!spwr_battery_present(bat))
|
|
|
|
+ return 0;
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ return sam_psy_get_bst(bat->p->channel, bat->p->instance, &bat->bst);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_set_alarm_unlocked(struct spwr_battery_device *bat, u32 value)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ bat->alarm = value;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ return sam_psy_set_btp(bat->p->channel, bat->p->instance, bat->alarm);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_set_alarm(struct spwr_battery_device *bat, u32 value)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&bat->lock);
|
|
|
|
+ status = spwr_battery_set_alarm_unlocked(bat, value);
|
|
|
|
+ mutex_unlock(&bat->lock);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_update_bst_unlocked(struct spwr_battery_device *bat, bool cached)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ unsigned long cache_deadline = bat->timestamp + msecs_to_jiffies(cache_time);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ if (cached && bat->timestamp && time_is_after_jiffies(cache_deadline))
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_load_sta(bat);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_load_bst(bat);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ bat->timestamp = jiffies;
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int spwr_battery_update_bst(struct spwr_battery_device *bat, bool cached)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&bat->lock);
|
|
|
|
+ status = spwr_battery_update_bst_unlocked(bat, cached);
|
|
|
|
+ mutex_unlock(&bat->lock);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_update_bix_unlocked(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_load_sta(bat);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_load_bix(bat);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_load_bst(bat);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ bat->timestamp = jiffies;
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int spwr_battery_update_bix(struct spwr_battery_device *bat)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&bat->lock);
|
|
|
|
+ status = spwr_battery_update_bix_unlocked(bat);
|
|
|
|
+ mutex_unlock(&bat->lock);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_ac_update_unlocked(struct spwr_ac_device *ac)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-13 14:07:25 +00:00
|
|
|
+ return sam_psy_get_psrc(0x01, 0x01, &ac->state);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int spwr_ac_update(struct spwr_ac_device *ac)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&ac->lock);
|
|
|
|
+ status = spwr_ac_update_unlocked(ac);
|
|
|
|
+ mutex_unlock(&ac->lock);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int spwr_battery_recheck(struct spwr_battery_device *bat)
|
|
|
|
+{
|
|
|
|
+ bool present = spwr_battery_present(bat);
|
|
|
|
+ u32 unit = bat->bix.power_unit;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_update_bix(bat);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ // if battery has been attached, (re-)initialize alarm
|
|
|
|
+ if (!present && spwr_battery_present(bat)) {
|
|
|
|
+ status = spwr_battery_set_alarm(bat, bat->bix.design_cap_warn);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // if the unit has changed, re-add the battery
|
|
|
|
+ if (unit != bat->bix.power_unit) {
|
2020-07-02 21:19:52 +00:00
|
|
|
+ spwr_battery_unregister(bat);
|
2020-07-13 14:07:25 +00:00
|
|
|
+ status = spwr_battery_register(bat, bat->pdev, bat->p);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int spwr_notify_bix(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = spwr_battery_recheck(bat);
|
|
|
|
+ if (!status)
|
|
|
|
+ power_supply_changed(bat->psy);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int spwr_notify_bst(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = spwr_battery_update_bst(bat, false);
|
|
|
|
+ if (!status)
|
|
|
|
+ power_supply_changed(bat->psy);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int spwr_notify_adapter_bat(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ /*
|
|
|
|
+ * Handle battery update quirk:
|
|
|
|
+ * When the battery is fully charged and the adapter is plugged in or
|
|
|
|
+ * removed, the EC does not send a separate event for the state
|
|
|
|
+ * (charging/discharging) change. Furthermore it may take some time until
|
|
|
|
+ * the state is updated on the battery. Schedule an update to solve this.
|
|
|
|
+ */
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (bat->bst.remaining_cap >= bat->bix.last_full_charge_cap)
|
|
|
|
+ schedule_delayed_work(&bat->update_work, SPWR_AC_BAT_UPDATE_DELAY);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int spwr_notify_adapter_ac(struct spwr_ac_device *ac)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = spwr_ac_update(ac);
|
|
|
|
+ if (!status)
|
|
|
|
+ power_supply_changed(ac->psy);
|
|
|
|
+
|
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static u32 spwr_notify_bat(struct ssam_notifier_block *nb, const struct ssam_event *event)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct spwr_battery_device *bat = container_of(nb, struct spwr_battery_device, notif.base);
|
|
|
|
+ int status;
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ dev_dbg(&bat->pdev->dev, "power event (cid = 0x%02x, iid = %d, chn = %d)\n",
|
|
|
|
+ event->command_id, event->instance_id, event->channel);
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ // handled here, needs to be handled for all channels/instances
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (event->command_id == SAM_EVENT_PWR_CID_ADAPTER) {
|
|
|
|
+ status = spwr_notify_adapter_bat(bat);
|
|
|
|
+ return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
|
|
|
|
+ }
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ // check for the correct channel and instance ID
|
|
|
|
+ if (event->channel != bat->p->channel)
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ if (event->instance_id != bat->p->instance)
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ switch (event->command_id) {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ case SAM_EVENT_PWR_CID_BIX:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = spwr_notify_bix(bat);
|
|
|
|
+ break;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ case SAM_EVENT_PWR_CID_BST:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = spwr_notify_bst(bat);
|
|
|
|
+ break;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ default:
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static u32 spwr_notify_ac(struct ssam_notifier_block *nb, const struct ssam_event *event)
|
|
|
|
+{
|
|
|
|
+ struct spwr_ac_device *ac = container_of(nb, struct spwr_ac_device, notif.base);
|
|
|
|
+ int status;
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ dev_dbg(&ac->pdev->dev, "power event (cid = 0x%02x, iid = %d, chn = %d)\n",
|
|
|
|
+ event->command_id, event->instance_id, event->channel);
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
|
|
|
+ // AC has IID = 0
|
|
|
|
+ if (event->instance_id != 0)
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ switch (event->command_id) {
|
|
|
|
+ case SAM_EVENT_PWR_CID_ADAPTER:
|
|
|
|
+ status = spwr_notify_adapter_ac(ac);
|
|
|
|
+ return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void spwr_battery_update_bst_workfn(struct work_struct *work)
|
|
|
|
+{
|
|
|
|
+ struct delayed_work *dwork = to_delayed_work(work);
|
|
|
|
+ struct spwr_battery_device *bat = container_of(dwork, struct spwr_battery_device, update_work);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_update_bst(bat, false);
|
|
|
|
+ if (!status)
|
|
|
|
+ power_supply_changed(bat->psy);
|
|
|
|
+
|
|
|
|
+ if (status)
|
|
|
|
+ dev_err(&bat->pdev->dev, "failed to update battery state: %d\n", status);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_prop_status(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ if (bat->bst.state & SAM_BATTERY_STATE_DISCHARGING)
|
|
|
|
+ return POWER_SUPPLY_STATUS_DISCHARGING;
|
|
|
|
+
|
|
|
|
+ if (bat->bst.state & SAM_BATTERY_STATE_CHARGING)
|
|
|
|
+ return POWER_SUPPLY_STATUS_CHARGING;
|
|
|
|
+
|
|
|
|
+ if (bat->bix.last_full_charge_cap == bat->bst.remaining_cap)
|
|
|
|
+ return POWER_SUPPLY_STATUS_FULL;
|
|
|
|
+
|
|
|
|
+ if (bat->bst.present_rate == 0)
|
|
|
|
+ return POWER_SUPPLY_STATUS_NOT_CHARGING;
|
|
|
|
+
|
|
|
|
+ return POWER_SUPPLY_STATUS_UNKNOWN;
|
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_prop_technology(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ if (!strcasecmp("NiCd", bat->bix.type))
|
|
|
|
+ return POWER_SUPPLY_TECHNOLOGY_NiCd;
|
|
|
|
+
|
|
|
|
+ if (!strcasecmp("NiMH", bat->bix.type))
|
|
|
|
+ return POWER_SUPPLY_TECHNOLOGY_NiMH;
|
|
|
|
+
|
|
|
|
+ if (!strcasecmp("LION", bat->bix.type))
|
|
|
|
+ return POWER_SUPPLY_TECHNOLOGY_LION;
|
|
|
|
+
|
|
|
|
+ if (!strncasecmp("LI-ION", bat->bix.type, 6))
|
|
|
|
+ return POWER_SUPPLY_TECHNOLOGY_LION;
|
|
|
|
+
|
|
|
|
+ if (!strcasecmp("LiP", bat->bix.type))
|
|
|
|
+ return POWER_SUPPLY_TECHNOLOGY_LIPO;
|
|
|
|
+
|
|
|
|
+ return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
|
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_prop_capacity(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ if (bat->bst.remaining_cap && bat->bix.last_full_charge_cap)
|
|
|
|
+ return bat->bst.remaining_cap * 100 / bat->bix.last_full_charge_cap;
|
|
|
|
+ else
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+static inline int spwr_battery_prop_capacity_level(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ if (bat->bst.state & SAM_BATTERY_STATE_CRITICAL)
|
|
|
|
+ return POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
|
|
|
|
+
|
|
|
|
+ if (bat->bst.remaining_cap >= bat->bix.last_full_charge_cap)
|
|
|
|
+ return POWER_SUPPLY_CAPACITY_LEVEL_FULL;
|
|
|
|
+
|
|
|
|
+ if (bat->bst.remaining_cap <= bat->alarm)
|
|
|
|
+ return POWER_SUPPLY_CAPACITY_LEVEL_LOW;
|
|
|
|
+
|
|
|
|
+ return POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int spwr_ac_get_property(struct power_supply *psy,
|
|
|
|
+ enum power_supply_property psp,
|
|
|
|
+ union power_supply_propval *val)
|
|
|
|
+{
|
|
|
|
+ struct spwr_ac_device *ac = power_supply_get_drvdata(psy);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&ac->lock);
|
|
|
|
+
|
|
|
|
+ status = spwr_ac_update_unlocked(ac);
|
|
|
|
+ if (status)
|
|
|
|
+ goto out;
|
|
|
|
+
|
|
|
|
+ switch (psp) {
|
|
|
|
+ case POWER_SUPPLY_PROP_ONLINE:
|
|
|
|
+ val->intval = ac->state == 1;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ status = -EINVAL;
|
|
|
|
+ goto out;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+out:
|
|
|
|
+ mutex_unlock(&ac->lock);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int spwr_battery_get_property(struct power_supply *psy,
|
|
|
|
+ enum power_supply_property psp,
|
|
|
|
+ union power_supply_propval *val)
|
|
|
|
+{
|
|
|
|
+ struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ mutex_lock(&bat->lock);
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_update_bst_unlocked(bat, true);
|
|
|
|
+ if (status)
|
|
|
|
+ goto out;
|
|
|
|
+
|
|
|
|
+ // abort if battery is not present
|
|
|
|
+ if (!spwr_battery_present(bat) && psp != POWER_SUPPLY_PROP_PRESENT) {
|
|
|
|
+ status = -ENODEV;
|
|
|
|
+ goto out;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ switch (psp) {
|
|
|
|
+ case POWER_SUPPLY_PROP_STATUS:
|
|
|
|
+ val->intval = spwr_battery_prop_status(bat);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_PRESENT:
|
|
|
|
+ val->intval = spwr_battery_present(bat);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_TECHNOLOGY:
|
|
|
|
+ val->intval = spwr_battery_prop_technology(bat);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_CYCLE_COUNT:
|
|
|
|
+ val->intval = bat->bix.cycle_count;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
|
|
|
|
+ val->intval = bat->bix.design_voltage * 1000;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
|
|
|
|
+ val->intval = bat->bst.present_voltage * 1000;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
|
|
|
|
+ case POWER_SUPPLY_PROP_POWER_NOW:
|
|
|
|
+ val->intval = bat->bst.present_rate * 1000;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
|
|
|
|
+ case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
|
|
|
|
+ val->intval = bat->bix.design_cap * 1000;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_CHARGE_FULL:
|
|
|
|
+ case POWER_SUPPLY_PROP_ENERGY_FULL:
|
|
|
|
+ val->intval = bat->bix.last_full_charge_cap * 1000;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_CHARGE_NOW:
|
|
|
|
+ case POWER_SUPPLY_PROP_ENERGY_NOW:
|
|
|
|
+ val->intval = bat->bst.remaining_cap * 1000;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_CAPACITY:
|
|
|
|
+ val->intval = spwr_battery_prop_capacity(bat);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
|
|
|
|
+ val->intval = spwr_battery_prop_capacity_level(bat);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_MODEL_NAME:
|
|
|
|
+ val->strval = bat->bix.model;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_MANUFACTURER:
|
|
|
|
+ val->strval = bat->bix.oem_info;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case POWER_SUPPLY_PROP_SERIAL_NUMBER:
|
|
|
|
+ val->strval = bat->bix.serial;
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ status = -EINVAL;
|
|
|
|
+ goto out;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+out:
|
|
|
|
+ mutex_unlock(&bat->lock);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static ssize_t spwr_battery_alarm_show(struct device *dev,
|
|
|
|
+ struct device_attribute *attr,
|
|
|
|
+ char *buf)
|
|
|
|
+{
|
|
|
|
+ struct power_supply *psy = dev_get_drvdata(dev);
|
|
|
|
+ struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
|
|
|
|
+
|
|
|
|
+ return sprintf(buf, "%d\n", bat->alarm * 1000);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static ssize_t spwr_battery_alarm_store(struct device *dev,
|
|
|
|
+ struct device_attribute *attr,
|
|
|
|
+ const char *buf, size_t count)
|
|
|
|
+{
|
|
|
|
+ struct power_supply *psy = dev_get_drvdata(dev);
|
|
|
|
+ struct spwr_battery_device *bat = power_supply_get_drvdata(psy);
|
|
|
|
+ unsigned long value;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = kstrtoul(buf, 0, &value);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ if (!spwr_battery_present(bat))
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_set_alarm(bat, value / 1000);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ return count;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static const struct device_attribute alarm_attr = {
|
|
|
|
+ .attr = {.name = "alarm", .mode = 0644},
|
|
|
|
+ .show = spwr_battery_alarm_show,
|
|
|
|
+ .store = spwr_battery_alarm_store,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int spwr_ac_register(struct spwr_ac_device *ac, struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct power_supply_config psy_cfg = {};
|
|
|
|
+ u32 sta;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // make sure the device is there and functioning properly
|
2020-07-13 14:07:25 +00:00
|
|
|
+ status = sam_psy_get_sta(0x01, 0x01, &sta);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ if ((sta & SAM_BATTERY_STA_OK) != SAM_BATTERY_STA_OK)
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
|
|
|
+ psy_cfg.drv_data = ac;
|
|
|
|
+
|
|
|
|
+ ac->pdev = pdev;
|
|
|
|
+ mutex_init(&ac->lock);
|
|
|
|
+
|
|
|
|
+ snprintf(ac->name, ARRAY_SIZE(ac->name), "ADP0");
|
|
|
|
+
|
|
|
|
+ ac->psy_desc.name = ac->name;
|
|
|
|
+ ac->psy_desc.type = POWER_SUPPLY_TYPE_MAINS;
|
|
|
|
+ ac->psy_desc.properties = spwr_ac_props;
|
|
|
|
+ ac->psy_desc.num_properties = ARRAY_SIZE(spwr_ac_props);
|
|
|
|
+ ac->psy_desc.get_property = spwr_ac_get_property;
|
|
|
|
+
|
|
|
|
+ ac->psy = power_supply_register(&ac->pdev->dev, &ac->psy_desc, &psy_cfg);
|
|
|
|
+ if (IS_ERR(ac->psy)) {
|
|
|
|
+ status = PTR_ERR(ac->psy);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ goto err_psy;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ac->notif.base.priority = 1;
|
|
|
|
+ ac->notif.base.fn = spwr_notify_ac;
|
|
|
|
+ ac->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
|
|
|
|
+ ac->notif.event.id.target_category = SSAM_SSH_TC_BAT;
|
|
|
|
+ ac->notif.event.id.instance = 0;
|
|
|
|
+ ac->notif.event.flags = SSAM_EVENT_SEQUENCED;
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_notifier_register(&ac->notif);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_notif;
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+err_notif:
|
|
|
|
+ power_supply_unregister(ac->psy);
|
|
|
|
+err_psy:
|
2019-12-10 00:26:31 +00:00
|
|
|
+ mutex_destroy(&ac->lock);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int spwr_ac_unregister(struct spwr_ac_device *ac)
|
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ surface_sam_ssh_notifier_unregister(&ac->notif);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ power_supply_unregister(ac->psy);
|
|
|
|
+ mutex_destroy(&ac->lock);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+static int spwr_battery_register(struct spwr_battery_device *bat,
|
|
|
|
+ struct platform_device *pdev,
|
|
|
|
+ const struct ssam_battery_properties *p)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct power_supply_config psy_cfg = {};
|
|
|
|
+ u32 sta;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ bat->pdev = pdev;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ bat->p = p;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ // make sure the device is there and functioning properly
|
2020-07-13 14:07:25 +00:00
|
|
|
+ status = sam_psy_get_sta(bat->p->channel, bat->p->instance, &sta);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ if ((sta & SAM_BATTERY_STA_OK) != SAM_BATTERY_STA_OK)
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
|
|
|
+ status = spwr_battery_update_bix_unlocked(bat);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ if (spwr_battery_present(bat)) {
|
|
|
|
+ status = spwr_battery_set_alarm_unlocked(bat, bat->bix.design_cap_warn);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ snprintf(bat->name, ARRAY_SIZE(bat->name), "BAT%d", bat->p->num);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ bat->psy_desc.name = bat->name;
|
|
|
|
+ bat->psy_desc.type = POWER_SUPPLY_TYPE_BATTERY;
|
|
|
|
+
|
|
|
|
+ if (bat->bix.power_unit == SAM_BATTERY_POWER_UNIT_MA) {
|
|
|
|
+ bat->psy_desc.properties = spwr_battery_props_chg;
|
|
|
|
+ bat->psy_desc.num_properties = ARRAY_SIZE(spwr_battery_props_chg);
|
|
|
|
+ } else {
|
|
|
|
+ bat->psy_desc.properties = spwr_battery_props_eng;
|
|
|
|
+ bat->psy_desc.num_properties = ARRAY_SIZE(spwr_battery_props_eng);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bat->psy_desc.get_property = spwr_battery_get_property;
|
|
|
|
+
|
|
|
|
+ mutex_init(&bat->lock);
|
|
|
|
+ psy_cfg.drv_data = bat;
|
|
|
|
+
|
|
|
|
+ INIT_DELAYED_WORK(&bat->update_work, spwr_battery_update_bst_workfn);
|
|
|
|
+
|
|
|
|
+ bat->psy = power_supply_register(&bat->pdev->dev, &bat->psy_desc, &psy_cfg);
|
|
|
|
+ if (IS_ERR(bat->psy)) {
|
|
|
|
+ status = PTR_ERR(bat->psy);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ goto err_psy;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ bat->notif.base.priority = 1;
|
|
|
|
+ bat->notif.base.fn = spwr_notify_bat;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ bat->notif.event.reg = p->registry;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ bat->notif.event.id.target_category = SSAM_SSH_TC_BAT;
|
|
|
|
+ bat->notif.event.id.instance = 0;
|
|
|
|
+ bat->notif.event.flags = SSAM_EVENT_SEQUENCED;
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_notifier_register(&bat->notif);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_notif;
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+ status = device_create_file(&bat->psy->dev, &alarm_attr);
|
|
|
|
+ if (status)
|
2020-07-02 21:19:52 +00:00
|
|
|
+ goto err_file;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+err_file:
|
|
|
|
+ surface_sam_ssh_notifier_unregister(&bat->notif);
|
|
|
|
+err_notif:
|
2019-12-10 00:26:31 +00:00
|
|
|
+ power_supply_unregister(bat->psy);
|
2020-07-02 21:19:52 +00:00
|
|
|
+err_psy:
|
|
|
|
+ mutex_destroy(&bat->lock);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void spwr_battery_unregister(struct spwr_battery_device *bat)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ surface_sam_ssh_notifier_unregister(&bat->notif);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ cancel_delayed_work_sync(&bat->update_work);
|
|
|
|
+ device_remove_file(&bat->psy->dev, &alarm_attr);
|
|
|
|
+ power_supply_unregister(bat->psy);
|
|
|
|
+ mutex_destroy(&bat->lock);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * Battery Driver.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#ifdef CONFIG_PM_SLEEP
|
|
|
|
+static int surface_sam_sid_battery_resume(struct device *dev)
|
|
|
|
+{
|
2020-05-16 22:20:04 +00:00
|
|
|
+ struct spwr_battery_device *bat;
|
|
|
|
+
|
|
|
|
+ bat = dev_get_drvdata(dev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return spwr_battery_recheck(bat);
|
|
|
|
+}
|
|
|
|
+#else
|
|
|
|
+#define surface_sam_sid_battery_resume NULL
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+SIMPLE_DEV_PM_OPS(surface_sam_sid_battery_pm, NULL, surface_sam_sid_battery_resume);
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_battery_probe(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct spwr_battery_device *bat;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ // link to ec
|
|
|
|
+ status = surface_sam_ssh_consumer_register(&pdev->dev);
|
|
|
|
+ if (status)
|
|
|
|
+ return status == -ENXIO ? -EPROBE_DEFER : status;
|
|
|
|
+
|
|
|
|
+ bat = devm_kzalloc(&pdev->dev, sizeof(struct spwr_battery_device), GFP_KERNEL);
|
|
|
|
+ if (!bat)
|
|
|
|
+ return -ENOMEM;
|
|
|
|
+
|
|
|
|
+ platform_set_drvdata(pdev, bat);
|
2020-07-13 14:07:25 +00:00
|
|
|
+ return spwr_battery_register(bat, pdev, pdev->dev.platform_data);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_battery_remove(struct platform_device *pdev)
|
|
|
|
+{
|
2020-05-16 22:20:04 +00:00
|
|
|
+ struct spwr_battery_device *bat;
|
|
|
|
+
|
|
|
|
+ bat = platform_get_drvdata(pdev);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ spwr_battery_unregister(bat);
|
|
|
|
+
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct platform_driver surface_sam_sid_battery = {
|
|
|
|
+ .probe = surface_sam_sid_battery_probe,
|
|
|
|
+ .remove = surface_sam_sid_battery_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_sid_battery",
|
|
|
|
+ .pm = &surface_sam_sid_battery_pm,
|
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * AC Driver.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_ac_probe(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+ struct spwr_ac_device *ac;
|
|
|
|
+
|
|
|
|
+ // link to ec
|
|
|
|
+ status = surface_sam_ssh_consumer_register(&pdev->dev);
|
|
|
|
+ if (status)
|
|
|
|
+ return status == -ENXIO ? -EPROBE_DEFER : status;
|
|
|
|
+
|
|
|
|
+ ac = devm_kzalloc(&pdev->dev, sizeof(struct spwr_ac_device), GFP_KERNEL);
|
|
|
|
+ if (!ac)
|
|
|
|
+ return -ENOMEM;
|
|
|
|
+
|
|
|
|
+ status = spwr_ac_register(ac, pdev);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ platform_set_drvdata(pdev, ac);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_ac_remove(struct platform_device *pdev)
|
|
|
|
+{
|
2020-05-16 22:20:04 +00:00
|
|
|
+ struct spwr_ac_device *ac;
|
|
|
|
+
|
|
|
|
+ ac = platform_get_drvdata(pdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return spwr_ac_unregister(ac);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct platform_driver surface_sam_sid_ac = {
|
|
|
|
+ .probe = surface_sam_sid_ac_probe,
|
|
|
|
+ .remove = surface_sam_sid_ac_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_sid_ac",
|
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int __init surface_sam_sid_power_init(void)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = platform_driver_register(&surface_sam_sid_battery);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ status = platform_driver_register(&surface_sam_sid_ac);
|
|
|
|
+ if (status) {
|
|
|
|
+ platform_driver_unregister(&surface_sam_sid_battery);
|
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void __exit surface_sam_sid_power_exit(void)
|
|
|
|
+{
|
|
|
|
+ platform_driver_unregister(&surface_sam_sid_battery);
|
|
|
|
+ platform_driver_unregister(&surface_sam_sid_ac);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+module_init(surface_sam_sid_power_init);
|
|
|
|
+module_exit(surface_sam_sid_power_exit);
|
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
|
|
|
|
+MODULE_DESCRIPTION("Surface Battery/AC Driver for 7th Generation Surface Devices");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-27 23:54:14 +00:00
|
|
|
+MODULE_ALIAS("platform:surface_sam_sid_ac");
|
|
|
|
+MODULE_ALIAS("platform:surface_sam_sid_battery");
|
2020-07-13 14:07:25 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_power.h b/drivers/platform/x86/surface_sam/surface_sam_sid_power.h
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..2e8f212086e1
|
2020-07-13 14:07:25 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_sid_power.h
|
|
|
|
@@ -0,0 +1,15 @@
|
|
|
|
+#ifndef _SURFACE_SAM_SID_POWER_H
|
|
|
|
+#define _SURFACE_SAM_SID_POWER_H
|
|
|
|
+
|
|
|
|
+#include <linux/types.h>
|
|
|
|
+#include "surface_sam_ssh.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssam_battery_properties {
|
|
|
|
+ struct ssam_event_registry registry;
|
|
|
|
+ u8 num;
|
|
|
|
+ u8 channel;
|
|
|
|
+ u8 instance;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+#endif /* _SURFACE_SAM_SID_POWER_H */
|
2019-12-10 00:26:31 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.c b/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..474221097eaf
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.c
|
2020-07-13 14:07:25 +00:00
|
|
|
@@ -0,0 +1,432 @@
|
2020-07-02 21:19:52 +00:00
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
|
|
|
+ * Microsofs Surface HID (VHF) driver for HID input events via SAM.
|
|
|
|
+ * Used for keyboard input events on the 7th generation Surface Laptops.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <linux/acpi.h>
|
|
|
|
+#include <linux/hid.h>
|
|
|
|
+#include <linux/input.h>
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
+#include <linux/types.h>
|
|
|
|
+
|
|
|
|
+#include "surface_sam_ssh.h"
|
2020-07-13 14:07:25 +00:00
|
|
|
+#include "surface_sam_sid_vhf.h"
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+#define SID_VHF_INPUT_NAME "Microsoft Surface HID"
|
|
|
|
+
|
|
|
|
+#define SAM_EVENT_SID_VHF_TC 0x15
|
|
|
|
+
|
|
|
|
+#define VHF_HID_STARTED 0
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+struct sid_vhf {
|
2020-07-13 14:07:25 +00:00
|
|
|
+ const struct ssam_hid_properties *p;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct platform_device *dev;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ struct hid_device *hid;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct ssam_event_notifier notif;
|
|
|
|
+ unsigned long state;
|
2019-12-10 00:26:31 +00:00
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int sid_vhf_hid_start(struct hid_device *hid)
|
|
|
|
+{
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void sid_vhf_hid_stop(struct hid_device *hid)
|
|
|
|
+{
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int sid_vhf_hid_open(struct hid_device *hid)
|
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct sid_vhf *vhf = dev_get_drvdata(hid->dev.parent);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ set_bit(VHF_HID_STARTED, &vhf->state);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void sid_vhf_hid_close(struct hid_device *hid)
|
|
|
|
+{
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct sid_vhf *vhf = dev_get_drvdata(hid->dev.parent);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ clear_bit(VHF_HID_STARTED, &vhf->state);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+struct surface_sam_sid_vhf_meta_rqst {
|
|
|
|
+ u8 id;
|
|
|
|
+ u32 offset;
|
|
|
|
+ u32 length; // buffer limit on send, length of data received on receive
|
|
|
|
+ u8 end; // 0x01 if end was reached
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+struct vhf_device_metadata_info {
|
|
|
|
+ u8 len;
|
|
|
|
+ u8 _2;
|
|
|
|
+ u8 _3;
|
|
|
|
+ u8 _4;
|
|
|
|
+ u8 _5;
|
|
|
|
+ u8 _6;
|
|
|
|
+ u8 _7;
|
|
|
|
+ u16 hid_len; // hid descriptor length
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+struct vhf_device_metadata {
|
|
|
|
+ u32 len;
|
|
|
|
+ u16 vendor_id;
|
|
|
|
+ u16 product_id;
|
|
|
|
+ u8 _1[24];
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+union vhf_buffer_data {
|
|
|
|
+ struct vhf_device_metadata_info info;
|
|
|
|
+ u8 pld[0x76];
|
|
|
|
+ struct vhf_device_metadata meta;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct surface_sam_sid_vhf_meta_resp {
|
|
|
|
+ struct surface_sam_sid_vhf_meta_rqst rqst;
|
|
|
|
+ union vhf_buffer_data data;
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int vhf_get_metadata(u8 iid, struct vhf_device_metadata *meta)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ struct surface_sam_sid_vhf_meta_resp resp = {
|
|
|
|
+ .rqst = {
|
|
|
|
+ .id = 2,
|
|
|
|
+ .offset = 0,
|
|
|
|
+ .length = 0x76,
|
|
|
|
+ .end = 0
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .tc = 0x15,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .cid = 0x04,
|
|
|
|
+ .iid = iid,
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .chn = 0x02,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .snc = 0x01,
|
|
|
|
+ .cdl = sizeof(struct surface_sam_sid_vhf_meta_rqst),
|
2020-05-16 22:20:04 +00:00
|
|
|
+ .pld = (u8 *)&resp.rqst,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_buf result = {
|
|
|
|
+ .cap = sizeof(struct surface_sam_sid_vhf_meta_resp),
|
|
|
|
+ .len = 0,
|
2020-05-16 22:20:04 +00:00
|
|
|
+ .data = (u8 *)&resp,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_rqst(&rqst, &result);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ *meta = resp.data.meta;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int vhf_get_hid_descriptor(struct hid_device *hid, u8 iid, u8 **desc, int *size)
|
|
|
|
+{
|
|
|
|
+ int status, len;
|
|
|
|
+ u8 *buf;
|
|
|
|
+
|
|
|
|
+ struct surface_sam_sid_vhf_meta_resp resp = {
|
|
|
|
+ .rqst = {
|
|
|
|
+ .id = 0,
|
|
|
|
+ .offset = 0,
|
|
|
|
+ .length = 0x76,
|
|
|
|
+ .end = 0,
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .tc = 0x15,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .cid = 0x04,
|
|
|
|
+ .iid = iid,
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .chn = 0x02,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .snc = 0x01,
|
|
|
|
+ .cdl = sizeof(struct surface_sam_sid_vhf_meta_rqst),
|
2020-05-16 22:20:04 +00:00
|
|
|
+ .pld = (u8 *)&resp.rqst,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_buf result = {
|
|
|
|
+ .cap = sizeof(struct surface_sam_sid_vhf_meta_resp),
|
|
|
|
+ .len = 0,
|
2020-05-16 22:20:04 +00:00
|
|
|
+ .data = (u8 *)&resp,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ // first fetch 00 to get the total length
|
|
|
|
+ status = surface_sam_ssh_rqst(&rqst, &result);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ len = resp.data.info.hid_len;
|
|
|
|
+
|
|
|
|
+ // allocate a buffer for the descriptor
|
|
|
|
+ buf = kzalloc(len, GFP_KERNEL);
|
|
|
|
+
|
|
|
|
+ // then, iterate and write into buffer, copying out bytes
|
|
|
|
+ resp.rqst.id = 1;
|
|
|
|
+ resp.rqst.offset = 0;
|
|
|
|
+ resp.rqst.length = 0x76;
|
|
|
|
+ resp.rqst.end = 0;
|
|
|
|
+
|
|
|
|
+ while (!resp.rqst.end && resp.rqst.offset < len) {
|
|
|
|
+ status = surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+ if (status) {
|
|
|
|
+ kfree(buf);
|
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+ memcpy(buf + resp.rqst.offset, resp.data.pld, resp.rqst.length);
|
|
|
|
+
|
|
|
|
+ resp.rqst.offset += resp.rqst.length;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ *desc = buf;
|
|
|
|
+ *size = len;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int sid_vhf_hid_parse(struct hid_device *hid)
|
|
|
|
+{
|
2020-07-13 14:07:25 +00:00
|
|
|
+ struct sid_vhf *vhf = dev_get_drvdata(hid->dev.parent);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ int ret = 0, size;
|
|
|
|
+ u8 *buf;
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ ret = vhf_get_hid_descriptor(hid, vhf->p->instance, &buf, &size);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ if (ret != 0) {
|
|
|
|
+ hid_err(hid, "Failed to read HID descriptor from device: %d\n", ret);
|
|
|
|
+ return -EIO;
|
|
|
|
+ }
|
|
|
|
+ hid_dbg(hid, "HID descriptor of device:");
|
|
|
|
+ print_hex_dump_debug("descriptor:", DUMP_PREFIX_OFFSET, 16, 1, buf, size, false);
|
|
|
|
+
|
|
|
|
+ ret = hid_parse_report(hid, buf, size);
|
|
|
|
+ kfree(buf);
|
|
|
|
+ return ret;
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int sid_vhf_hid_raw_request(struct hid_device *hid, unsigned char
|
|
|
|
+ reportnum, u8 *buf, size_t len, unsigned char rtype, int
|
|
|
|
+ reqtype)
|
|
|
|
+{
|
2020-07-13 14:07:25 +00:00
|
|
|
+ struct sid_vhf *vhf = dev_get_drvdata(hid->dev.parent);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ int status;
|
|
|
|
+ u8 cid;
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {};
|
|
|
|
+ struct surface_sam_ssh_buf result = {};
|
|
|
|
+
|
|
|
|
+ hid_dbg(hid, "%s: reportnum=%#04x rtype=%i reqtype=%i\n", __func__, reportnum, rtype, reqtype);
|
|
|
|
+ print_hex_dump_debug("report:", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false);
|
|
|
|
+
|
|
|
|
+ // Byte 0 is the report number. Report data starts at byte 1.
|
|
|
|
+ buf[0] = reportnum;
|
|
|
|
+
|
|
|
|
+ switch (rtype) {
|
|
|
|
+ case HID_OUTPUT_REPORT:
|
|
|
|
+ cid = 0x01;
|
|
|
|
+ break;
|
|
|
|
+ case HID_FEATURE_REPORT:
|
|
|
|
+ switch (reqtype) {
|
|
|
|
+ case HID_REQ_GET_REPORT:
|
|
|
|
+ // The EC doesn't respond to GET FEATURE for these touchpad reports
|
|
|
|
+ // we immediately discard to avoid waiting for a timeout.
|
|
|
|
+ if (reportnum == 6 || reportnum == 7 || reportnum == 8 || reportnum == 9 || reportnum == 0x0b) {
|
|
|
|
+ hid_dbg(hid, "%s: skipping get feature report for 0x%02x\n", __func__, reportnum);
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cid = 0x02;
|
|
|
|
+ break;
|
|
|
|
+ case HID_REQ_SET_REPORT:
|
|
|
|
+ cid = 0x03;
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ hid_err(hid, "%s: unknown req type 0x%02x\n", __func__, rtype);
|
|
|
|
+ return -EIO;
|
|
|
|
+ }
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ hid_err(hid, "%s: unknown report type 0x%02x\n", __func__, reportnum);
|
|
|
|
+ return -EIO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ rqst.tc = SAM_EVENT_SID_VHF_TC;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ rqst.chn = 0x02;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ rqst.iid = vhf->p->instance;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.cid = cid;
|
2020-05-16 22:20:04 +00:00
|
|
|
+ rqst.snc = reqtype == HID_REQ_GET_REPORT ? 0x01 : 0x00;
|
|
|
|
+ rqst.cdl = reqtype == HID_REQ_GET_REPORT ? 0x01 : len;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ rqst.pld = buf;
|
|
|
|
+
|
|
|
|
+ result.cap = len;
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = buf;
|
|
|
|
+
|
|
|
|
+ hid_dbg(hid, "%s: sending to cid=%#04x snc=%#04x\n", __func__, cid, HID_REQ_GET_REPORT == reqtype);
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+ hid_dbg(hid, "%s: status %i\n", __func__, status);
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (result.len > 0)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ print_hex_dump_debug("response:", DUMP_PREFIX_OFFSET, 16, 1, result.data, result.len, false);
|
|
|
|
+
|
|
|
|
+ return result.len;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct hid_ll_driver sid_vhf_hid_ll_driver = {
|
|
|
|
+ .start = sid_vhf_hid_start,
|
|
|
|
+ .stop = sid_vhf_hid_stop,
|
|
|
|
+ .open = sid_vhf_hid_open,
|
|
|
|
+ .close = sid_vhf_hid_close,
|
|
|
|
+ .parse = sid_vhf_hid_parse,
|
|
|
|
+ .raw_request = sid_vhf_hid_raw_request,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static struct hid_device *sid_vhf_create_hid_device(struct platform_device *pdev, struct vhf_device_metadata *meta)
|
|
|
|
+{
|
|
|
|
+ struct hid_device *hid;
|
|
|
|
+
|
|
|
|
+ hid = hid_allocate_device();
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (IS_ERR(hid))
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return hid;
|
|
|
|
+
|
|
|
|
+ hid->dev.parent = &pdev->dev;
|
|
|
|
+
|
|
|
|
+ hid->bus = BUS_VIRTUAL;
|
|
|
|
+ hid->vendor = meta->vendor_id;
|
|
|
|
+ hid->product = meta->product_id;
|
|
|
|
+
|
|
|
|
+ hid->ll_driver = &sid_vhf_hid_ll_driver;
|
|
|
|
+
|
|
|
|
+ sprintf(hid->name, "%s", SID_VHF_INPUT_NAME);
|
|
|
|
+
|
|
|
|
+ return hid;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static u32 sid_vhf_event_handler(struct ssam_notifier_block *nb, const struct ssam_event *event)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct sid_vhf *vhf = container_of(nb, struct sid_vhf, notif.base);
|
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (event->target_category != SSAM_SSH_TC_HID)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ if (event->channel != 0x02)
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ if (event->instance_id != vhf->p->instance)
|
|
|
|
+ return 0;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (event->command_id != 0x00 && event->command_id != 0x03 && event->command_id != 0x04)
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // skip if HID hasn't started yet
|
|
|
|
+ if (!test_bit(VHF_HID_STARTED, &vhf->state))
|
|
|
|
+ return SSAM_NOTIF_HANDLED;
|
|
|
|
+
|
|
|
|
+ status = hid_input_report(vhf->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 0);
|
|
|
|
+ return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_vhf_probe(struct platform_device *pdev)
|
|
|
|
+{
|
2020-07-13 14:07:25 +00:00
|
|
|
+ const struct ssam_hid_properties *p = pdev->dev.platform_data;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct sid_vhf *vhf;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ struct vhf_device_metadata meta = {};
|
|
|
|
+ struct hid_device *hid;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // add device link to EC
|
|
|
|
+ status = surface_sam_ssh_consumer_register(&pdev->dev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status == -ENXIO ? -EPROBE_DEFER : status;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ vhf = kzalloc(sizeof(struct sid_vhf), GFP_KERNEL);
|
|
|
|
+ if (!vhf)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -ENOMEM;
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ status = vhf_get_metadata(p->instance, &meta);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_create_hid;
|
|
|
|
+
|
|
|
|
+ hid = sid_vhf_create_hid_device(pdev, &meta);
|
|
|
|
+ if (IS_ERR(hid)) {
|
|
|
|
+ status = PTR_ERR(hid);
|
|
|
|
+ goto err_create_hid;
|
|
|
|
+ }
|
|
|
|
+
|
2020-07-13 14:07:25 +00:00
|
|
|
+ vhf->p = pdev->dev.platform_data;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ vhf->dev = pdev;
|
|
|
|
+ vhf->hid = hid;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ vhf->notif.base.priority = 1;
|
|
|
|
+ vhf->notif.base.fn = sid_vhf_event_handler;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ vhf->notif.event.reg = p->registry;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ vhf->notif.event.id.target_category = SSAM_SSH_TC_HID;
|
2020-07-13 14:07:25 +00:00
|
|
|
+ vhf->notif.event.id.instance = p->instance;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ vhf->notif.event.flags = 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ platform_set_drvdata(pdev, vhf);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = surface_sam_ssh_notifier_register(&vhf->notif);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2020-07-02 21:19:52 +00:00
|
|
|
+ goto err_notif;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ status = hid_add_device(hid);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_add_hid;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+err_add_hid:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ surface_sam_ssh_notifier_unregister(&vhf->notif);
|
|
|
|
+err_notif:
|
2019-12-10 00:26:31 +00:00
|
|
|
+ hid_destroy_device(hid);
|
|
|
|
+ platform_set_drvdata(pdev, NULL);
|
|
|
|
+err_create_hid:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ kfree(vhf);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_sid_vhf_remove(struct platform_device *pdev)
|
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct sid_vhf *vhf = platform_get_drvdata(pdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ surface_sam_ssh_notifier_unregister(&vhf->notif);
|
|
|
|
+ hid_destroy_device(vhf->hid);
|
|
|
|
+ kfree(vhf);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ platform_set_drvdata(pdev, NULL);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct platform_driver surface_sam_sid_vhf = {
|
|
|
|
+ .probe = surface_sam_sid_vhf_probe,
|
|
|
|
+ .remove = surface_sam_sid_vhf_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_sid_vhf",
|
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+module_platform_driver(surface_sam_sid_vhf);
|
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Blaž Hrastnik <blaz@mxxn.io>");
|
|
|
|
+MODULE_DESCRIPTION("Driver for HID devices connected via Surface SAM");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-10 00:26:31 +00:00
|
|
|
+MODULE_ALIAS("platform:surface_sam_sid_vhf");
|
2020-07-13 14:07:25 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.h b/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.h
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..eb55485ccb11
|
2020-07-13 14:07:25 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_sid_vhf.h
|
|
|
|
@@ -0,0 +1,13 @@
|
|
|
|
+#ifndef _SURFACE_SAM_SID_VHF_H
|
|
|
|
+#define _SURFACE_SAM_SID_VHF_H
|
|
|
|
+
|
|
|
|
+#include <linux/types.h>
|
|
|
|
+#include "surface_sam_ssh.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssam_hid_properties {
|
|
|
|
+ struct ssam_event_registry registry;
|
|
|
|
+ u8 instance;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+#endif /* _SURFACE_SAM_SID_VHF_H */
|
2019-12-10 00:26:31 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_ssh.c b/drivers/platform/x86/surface_sam/surface_sam_ssh.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..9f44bdfbc4fd
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_ssh.c
|
2020-07-15 20:26:02 +00:00
|
|
|
@@ -0,0 +1,5111 @@
|
2020-07-02 21:19:52 +00:00
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
|
|
|
+ * Surface Serial Hub (SSH) driver for communication with the Surface/System
|
|
|
|
+ * Aggregator Module.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <asm/unaligned.h>
|
|
|
|
+#include <linux/acpi.h>
|
2020-07-02 21:19:52 +00:00
|
|
|
+#include <linux/atomic.h>
|
2019-12-10 00:26:31 +00:00
|
|
|
+#include <linux/completion.h>
|
|
|
|
+#include <linux/crc-ccitt.h>
|
|
|
|
+#include <linux/dmaengine.h>
|
|
|
|
+#include <linux/gpio/consumer.h>
|
|
|
|
+#include <linux/interrupt.h>
|
|
|
|
+#include <linux/jiffies.h>
|
|
|
|
+#include <linux/kernel.h>
|
|
|
|
+#include <linux/kfifo.h>
|
2020-07-02 21:19:52 +00:00
|
|
|
+#include <linux/kref.h>
|
|
|
|
+#include <linux/kthread.h>
|
|
|
|
+#include <linux/ktime.h>
|
|
|
|
+#include <linux/list.h>
|
2019-12-10 00:26:31 +00:00
|
|
|
+#include <linux/mutex.h>
|
|
|
|
+#include <linux/pm.h>
|
|
|
|
+#include <linux/refcount.h>
|
|
|
|
+#include <linux/serdev.h>
|
|
|
|
+#include <linux/spinlock.h>
|
2020-07-02 21:19:52 +00:00
|
|
|
+#include <linux/sysfs.h>
|
2019-12-10 00:26:31 +00:00
|
|
|
+#include <linux/workqueue.h>
|
|
|
|
+
|
|
|
|
+#include "surface_sam_ssh.h"
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+#define CREATE_TRACE_POINTS
|
|
|
|
+#include "surface_sam_ssh_trace.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- TODO. ----------------------------------------------------------------- */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+#define SSH_RQST_TAG_FULL "surface_sam_ssh_rqst: "
|
|
|
|
+#define SSH_RQST_TAG "rqst: "
|
|
|
|
+
|
|
|
|
+#define SSH_SUPPORTED_FLOW_CONTROL_MASK (~((u8) ACPI_UART_FLOW_CONTROL_HW))
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+/* -- Error injection helpers. ---------------------------------------------- */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+#ifdef CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION
|
|
|
|
+#define noinline_if_inject noinline
|
|
|
|
+#else /* CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION */
|
|
|
|
+#define noinline_if_inject inline
|
|
|
|
+#endif /* CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+/* -- Public interface. ----------------------------------------------------- */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+enum ssam_request_flags {
|
|
|
|
+ SSAM_REQUEST_HAS_RESPONSE = BIT(0),
|
|
|
|
+ SSAM_REQUEST_UNSEQUENCED = BIT(1),
|
2019-12-10 00:26:31 +00:00
|
|
|
+};
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+struct ssam_request {
|
|
|
|
+ u8 target_category;
|
|
|
|
+ u8 command_id;
|
|
|
|
+ u8 instance_id;
|
|
|
|
+ u8 channel;
|
|
|
|
+ u16 flags;
|
|
|
|
+ u16 length;
|
|
|
|
+ u8 *payload;
|
2019-12-10 00:26:31 +00:00
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+/* -- Common/utility functions. --------------------------------------------- */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline u16 ssh_crc(const u8 *buf, size_t len)
|
|
|
|
+{
|
|
|
|
+ return crc_ccitt_false(0xffff, buf, len);
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline u16 __ssh_rqid_next(u16 rqid)
|
|
|
|
+{
|
|
|
|
+ return rqid > 0 ? rqid + 1u : rqid + SURFACE_SAM_SSH_NUM_EVENTS + 1u;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline u16 ssh_event_to_rqid(u16 event)
|
|
|
|
+{
|
|
|
|
+ return event + 1u;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline u16 ssh_rqid_to_event(u16 rqid)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return rqid - 1u;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline bool ssh_rqid_is_event(u16 rqid)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return ssh_rqid_to_event(rqid) < SURFACE_SAM_SSH_NUM_EVENTS;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int ssh_tc_to_rqid(u8 tc)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+#if 0 // TODO: check if it works without this
|
|
|
|
+ /*
|
|
|
|
+ * TC=0x08 represents the input subsystem on Surface Laptop 1 and 2.
|
|
|
|
+ * This is mapped on Windows to RQID=0x0001. As input events seem to be
|
|
|
|
+ * somewhat special with regards to enabling/disabling (they seem to be
|
|
|
|
+ * enabled by default with a fixed RQID), let's do the same here.
|
|
|
|
+ */
|
|
|
|
+ if (tc == 0x08)
|
|
|
|
+ return 0x0001;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ /* Default path: Set RQID = TC. */
|
|
|
|
+#endif
|
|
|
|
+ return tc;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int ssh_tc_to_event(u8 tc)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return ssh_rqid_to_event(ssh_tc_to_rqid(tc));
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline u8 ssh_channel_to_index(u8 channel)
|
2020-05-16 22:20:04 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return channel - 1u;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline bool ssh_channel_is_valid(u8 channel)
|
2020-05-16 22:20:04 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return ssh_channel_to_index(channel) < SURFACE_SAM_SSH_NUM_CHANNELS;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+/* -- Safe counters. -------------------------------------------------------- */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+struct ssh_seq_counter {
|
|
|
|
+ u8 value;
|
|
|
|
+};
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+struct ssh_rqid_counter {
|
|
|
|
+ u16 value;
|
|
|
|
+};
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void ssh_seq_reset(struct ssh_seq_counter *c)
|
|
|
|
+{
|
|
|
|
+ WRITE_ONCE(c->value, 0);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline u8 ssh_seq_next(struct ssh_seq_counter *c)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u8 old = READ_ONCE(c->value);
|
|
|
|
+ u8 new = old + 1;
|
|
|
|
+ u8 ret;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
|
|
|
|
+ old = ret;
|
|
|
|
+ new = old + 1;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return old;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void ssh_rqid_reset(struct ssh_rqid_counter *c)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ WRITE_ONCE(c->value, 0);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline u16 ssh_rqid_next(struct ssh_rqid_counter *c)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u16 old = READ_ONCE(c->value);
|
|
|
|
+ u16 new = __ssh_rqid_next(old);
|
|
|
|
+ u16 ret;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ while (unlikely((ret = cmpxchg(&c->value, old, new)) != old)) {
|
|
|
|
+ old = ret;
|
|
|
|
+ new = __ssh_rqid_next(old);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return old;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+/* -- Builder functions for SAM-over-SSH messages. -------------------------- */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+struct msgbuf {
|
|
|
|
+ u8 *buffer;
|
|
|
|
+ u8 *end;
|
|
|
|
+ u8 *ptr;
|
|
|
|
+};
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_init(struct msgbuf *msgb, u8 *buffer, size_t cap)
|
|
|
|
+{
|
|
|
|
+ msgb->buffer = buffer;
|
|
|
|
+ msgb->end = buffer + cap;
|
|
|
|
+ msgb->ptr = buffer;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int msgb_alloc(struct msgbuf *msgb, size_t cap, gfp_t flags)
|
|
|
|
+{
|
|
|
|
+ u8 *buf;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ buf = kzalloc(cap, flags);
|
|
|
|
+ if (!buf)
|
|
|
|
+ return -ENOMEM;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ msgb_init(msgb, buf, cap);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_free(struct msgbuf *msgb)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ kfree(msgb->buffer);
|
|
|
|
+ msgb->buffer = NULL;
|
|
|
|
+ msgb->end = NULL;
|
|
|
|
+ msgb->ptr = NULL;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_reset(struct msgbuf *msgb)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ msgb->ptr = msgb->buffer;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline size_t msgb_bytes_used(const struct msgbuf *msgb)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return msgb->ptr - msgb->buffer;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_push_u16(struct msgbuf *msgb, u16 value)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ WARN_ON(msgb->ptr + sizeof(u16) > msgb->end);
|
|
|
|
+ if (msgb->ptr + sizeof(u16) > msgb->end)
|
|
|
|
+ return;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ put_unaligned_le16(value, msgb->ptr);
|
|
|
|
+ msgb->ptr += sizeof(u16);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_push_syn(struct msgbuf *msgb)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ msgb_push_u16(msgb, SSH_MSG_SYN);
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_push_buf(struct msgbuf *msgb, const u8 *buf, size_t len)
|
|
|
|
+{
|
|
|
|
+ msgb->ptr = memcpy(msgb->ptr, buf, len) + len;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_push_crc(struct msgbuf *msgb, const u8 *buf, size_t len)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ msgb_push_u16(msgb, ssh_crc(buf, len));
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_push_frame(struct msgbuf *msgb, u8 ty, u16 len, u8 seq)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct ssh_frame *frame = (struct ssh_frame *)msgb->ptr;
|
|
|
|
+ const u8 *const begin = msgb->ptr;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ WARN_ON(msgb->ptr + sizeof(*frame) > msgb->end);
|
|
|
|
+ if (msgb->ptr + sizeof(*frame) > msgb->end)
|
|
|
|
+ return;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ frame->type = ty;
|
|
|
|
+ put_unaligned_le16(len, &frame->len);
|
|
|
|
+ frame->seq = seq;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ msgb->ptr += sizeof(*frame);
|
|
|
|
+ msgb_push_crc(msgb, begin, msgb->ptr - begin);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_push_ack(struct msgbuf *msgb, u8 seq)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // SYN
|
|
|
|
+ msgb_push_syn(msgb);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // ACK-type frame + CRC
|
|
|
|
+ msgb_push_frame(msgb, SSH_FRAME_TYPE_ACK, 0x00, seq);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // payload CRC (ACK-type frames do not have a payload)
|
|
|
|
+ msgb_push_crc(msgb, msgb->ptr, 0);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_push_nak(struct msgbuf *msgb)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // SYN
|
|
|
|
+ msgb_push_syn(msgb);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // NAK-type frame + CRC
|
|
|
|
+ msgb_push_frame(msgb, SSH_FRAME_TYPE_NAK, 0x00, 0x00);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // payload CRC (ACK-type frames do not have a payload)
|
|
|
|
+ msgb_push_crc(msgb, msgb->ptr, 0);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void msgb_push_cmd(struct msgbuf *msgb, u8 seq,
|
|
|
|
+ const struct surface_sam_ssh_rqst *rqst,
|
|
|
|
+ u16 rqid)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct ssh_command *cmd;
|
|
|
|
+ const u8 *cmd_begin;
|
|
|
|
+ const u8 type = SSH_FRAME_TYPE_DATA_SEQ;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // SYN
|
|
|
|
+ msgb_push_syn(msgb);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // command frame + crc
|
|
|
|
+ msgb_push_frame(msgb, type, sizeof(*cmd) + rqst->cdl, seq);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // frame payload: command struct + payload
|
|
|
|
+ WARN_ON(msgb->ptr + sizeof(*cmd) > msgb->end);
|
|
|
|
+ if (msgb->ptr + sizeof(*cmd) > msgb->end)
|
|
|
|
+ return;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ cmd_begin = msgb->ptr;
|
|
|
|
+ cmd = (struct ssh_command *)msgb->ptr;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ cmd->type = SSH_PLD_TYPE_CMD;
|
|
|
|
+ cmd->tc = rqst->tc;
|
|
|
|
+ cmd->chn_out = rqst->chn;
|
|
|
|
+ cmd->chn_in = 0x00;
|
|
|
|
+ cmd->iid = rqst->iid;
|
|
|
|
+ put_unaligned_le16(rqid, &cmd->rqid);
|
|
|
|
+ cmd->cid = rqst->cid;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ msgb->ptr += sizeof(*cmd);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // command payload
|
|
|
|
+ msgb_push_buf(msgb, rqst->pld, rqst->cdl);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // crc for command struct + payload
|
|
|
|
+ msgb_push_crc(msgb, cmd_begin, msgb->ptr - cmd_begin);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+/* -- Parser functions and utilities for SAM-over-SSH messages. ------------- */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+struct sshp_buf {
|
|
|
|
+ u8 *ptr;
|
|
|
|
+ size_t len;
|
|
|
|
+ size_t cap;
|
|
|
|
+};
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline bool sshp_validate_crc(const struct sshp_span *src, const u8 *crc)
|
|
|
|
+{
|
|
|
|
+ u16 actual = ssh_crc(src->ptr, src->len);
|
|
|
|
+ u16 expected = get_unaligned_le16(crc);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return actual == expected;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static bool sshp_find_syn(const struct sshp_span *src, struct sshp_span *rem)
|
|
|
|
+{
|
|
|
|
+ size_t i;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ for (i = 0; i < src->len - 1; i++) {
|
|
|
|
+ if (likely(get_unaligned_le16(src->ptr + i) == SSH_MSG_SYN)) {
|
|
|
|
+ rem->ptr = src->ptr + i;
|
|
|
|
+ rem->len = src->len - i;
|
|
|
|
+ return true;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (unlikely(src->ptr[src->len - 1] == (SSH_MSG_SYN & 0xff))) {
|
|
|
|
+ rem->ptr = src->ptr + src->len - 1;
|
|
|
|
+ rem->len = 1;
|
|
|
|
+ return false;
|
|
|
|
+ } else {
|
|
|
|
+ rem->ptr = src->ptr + src->len;
|
|
|
|
+ rem->len = 0;
|
|
|
|
+ return false;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
2020-07-02 21:19:52 +00:00
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static bool sshp_starts_with_syn(const struct sshp_span *src)
|
|
|
|
+{
|
|
|
|
+ return src->len >= 2 && get_unaligned_le16(src->ptr) == SSH_MSG_SYN;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int sshp_parse_frame(const struct device *dev,
|
|
|
|
+ const struct sshp_span *source,
|
|
|
|
+ struct ssh_frame **frame,
|
|
|
|
+ struct sshp_span *payload,
|
|
|
|
+ size_t maxlen)
|
|
|
|
+{
|
|
|
|
+ struct sshp_span sf;
|
|
|
|
+ struct sshp_span sp;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // initialize output
|
|
|
|
+ *frame = NULL;
|
|
|
|
+ payload->ptr = NULL;
|
|
|
|
+ payload->len = 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (!sshp_starts_with_syn(source)) {
|
|
|
|
+ dev_warn(dev, "rx: parser: invalid start of frame\n");
|
|
|
|
+ return -ENOMSG;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // check for minumum packet length
|
|
|
|
+ if (unlikely(source->len < SSH_MESSAGE_LENGTH(0))) {
|
|
|
|
+ dev_dbg(dev, "rx: parser: not enough data for frame\n");
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // pin down frame
|
|
|
|
+ sf.ptr = source->ptr + sizeof(u16);
|
|
|
|
+ sf.len = sizeof(struct ssh_frame);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // validate frame CRC
|
|
|
|
+ if (unlikely(!sshp_validate_crc(&sf, sf.ptr + sf.len))) {
|
|
|
|
+ dev_warn(dev, "rx: parser: invalid frame CRC\n");
|
|
|
|
+ return -EBADMSG;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // ensure packet does not exceed maximum length
|
|
|
|
+ if (unlikely(((struct ssh_frame *)sf.ptr)->len > maxlen)) {
|
|
|
|
+ dev_warn(dev, "rx: parser: frame too large: %u bytes\n",
|
|
|
|
+ ((struct ssh_frame *)sf.ptr)->len);
|
|
|
|
+ return -EMSGSIZE;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // pin down payload
|
|
|
|
+ sp.ptr = sf.ptr + sf.len + sizeof(u16);
|
|
|
|
+ sp.len = get_unaligned_le16(&((struct ssh_frame *)sf.ptr)->len);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // check for frame + payload length
|
|
|
|
+ if (source->len < SSH_MESSAGE_LENGTH(sp.len)) {
|
|
|
|
+ dev_dbg(dev, "rx: parser: not enough data for payload\n");
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // validate payload crc
|
|
|
|
+ if (unlikely(!sshp_validate_crc(&sp, sp.ptr + sp.len))) {
|
|
|
|
+ dev_warn(dev, "rx: parser: invalid payload CRC\n");
|
|
|
|
+ return -EBADMSG;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ *frame = (struct ssh_frame *)sf.ptr;
|
|
|
|
+ *payload = sp;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_dbg(dev, "rx: parser: valid frame found (type: 0x%02x, len: %u)\n",
|
|
|
|
+ (*frame)->type, (*frame)->len);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return 0;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int sshp_parse_command(const struct device *dev,
|
|
|
|
+ const struct sshp_span *source,
|
|
|
|
+ struct ssh_command **command,
|
|
|
|
+ struct sshp_span *command_data)
|
|
|
|
+{
|
|
|
|
+ // check for minimum length
|
|
|
|
+ if (unlikely(source->len < sizeof(struct ssh_command))) {
|
|
|
|
+ *command = NULL;
|
|
|
|
+ command_data->ptr = NULL;
|
|
|
|
+ command_data->len = 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_err(dev, "rx: parser: command payload is too short\n");
|
|
|
|
+ return -ENOMSG;
|
|
|
|
+ }
|
2020-05-16 22:20:04 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ *command = (struct ssh_command *)source->ptr;
|
|
|
|
+ command_data->ptr = source->ptr + sizeof(struct ssh_command);
|
|
|
|
+ command_data->len = source->len - sizeof(struct ssh_command);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_dbg(dev, "rx: parser: valid command found (tc: 0x%02x,"
|
|
|
|
+ " cid: 0x%02x)\n", (*command)->tc, (*command)->cid);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void sshp_buf_init(struct sshp_buf *buf, u8 *ptr, size_t cap)
|
|
|
|
+{
|
|
|
|
+ buf->ptr = ptr;
|
|
|
|
+ buf->len = 0;
|
|
|
|
+ buf->cap = cap;
|
|
|
|
+}
|
2020-05-16 22:20:04 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline int sshp_buf_alloc(struct sshp_buf *buf, size_t cap, gfp_t flags)
|
|
|
|
+{
|
|
|
|
+ u8 *ptr;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ptr = kzalloc(cap, flags);
|
|
|
|
+ if (!ptr)
|
|
|
|
+ return -ENOMEM;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ sshp_buf_init(buf, ptr, cap);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void sshp_buf_free(struct sshp_buf *buf)
|
|
|
|
+{
|
|
|
|
+ kfree(buf->ptr);
|
|
|
|
+ buf->ptr = NULL;
|
|
|
|
+ buf->len = 0;
|
|
|
|
+ buf->cap = 0;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void sshp_buf_reset(struct sshp_buf *buf)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ buf->len = 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void sshp_buf_drop(struct sshp_buf *buf, size_t n)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ memmove(buf->ptr, buf->ptr + n, buf->len - n);
|
|
|
|
+ buf->len -= n;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline size_t sshp_buf_read_from_fifo(struct sshp_buf *buf,
|
|
|
|
+ struct kfifo *fifo)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ size_t n;
|
|
|
|
+
|
|
|
|
+ n = kfifo_out(fifo, buf->ptr + buf->len, buf->cap - buf->len);
|
|
|
|
+ buf->len += n;
|
2020-05-16 22:20:04 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return n;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline void sshp_buf_span_from(struct sshp_buf *buf, size_t offset,
|
|
|
|
+ struct sshp_span *span)
|
|
|
|
+{
|
|
|
|
+ span->ptr = buf->ptr + offset;
|
|
|
|
+ span->len = buf->len - offset;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Packet transport layer (ptl). ----------------------------------------- */
|
|
|
|
+/*
|
|
|
|
+ * To simplify reasoning about the code below, we define a few concepts. The
|
|
|
|
+ * system below is similar to a state-machine for packets, however, there are
|
|
|
|
+ * too many states to explicitly write them down. To (somewhat) manage the
|
|
|
|
+ * states and packets we rely on flags, reference counting, and some simple
|
|
|
|
+ * concepts. State transitions are triggered by actions.
|
|
|
|
+ *
|
|
|
|
+ * >> Actions <<
|
|
|
|
+ *
|
|
|
|
+ * - submit
|
|
|
|
+ * - transmission start (process next item in queue)
|
|
|
|
+ * - transmission finished (guaranteed to never be parallel to transmission
|
|
|
|
+ * start)
|
|
|
|
+ * - ACK received
|
|
|
|
+ * - NAK received (this is equivalent to issuing re-submit for all pending
|
|
|
|
+ * packets)
|
|
|
|
+ * - timeout (this is equivalent to re-issuing a submit or canceling)
|
|
|
|
+ * - cancel (non-pending and pending)
|
|
|
|
+ *
|
|
|
|
+ * >> Data Structures, Packet Ownership, General Overview <<
|
|
|
|
+ *
|
|
|
|
+ * The code below employs two main data structures: The packet queue, containing
|
|
|
|
+ * all packets scheduled for transmission, and the set of pending packets,
|
|
|
|
+ * containing all packets awaiting an ACK.
|
|
|
|
+ *
|
|
|
|
+ * Shared ownership of a packet is controlled via reference counting. Inside the
|
|
|
|
+ * transmission system are a total of five packet owners:
|
|
|
|
+ *
|
|
|
|
+ * - the packet queue,
|
|
|
|
+ * - the pending set,
|
|
|
|
+ * - the transmitter thread,
|
|
|
|
+ * - the receiver thread (via ACKing), and
|
|
|
|
+ * - the timeout work item.
|
|
|
|
+ *
|
|
|
|
+ * Normal operation is as follows: The initial reference of the packet is
|
|
|
|
+ * obtained by submitting the packet and queueing it. The receiver thread
|
|
|
|
+ * takes packets from the queue. By doing this, it does not increment the
|
|
|
|
+ * refcount but takes over the reference (removing it from the queue).
|
|
|
|
+ * If the packet is sequenced (i.e. needs to be ACKed by the client), the
|
|
|
|
+ * transmitter thread sets-up the timeout and adds the packet to the pending set
|
|
|
|
+ * before starting to transmit it. As the timeout is handled by a reaper task,
|
|
|
|
+ * no additional reference for it is needed. After the transmit is done, the
|
|
|
|
+ * reference hold by the transmitter thread is dropped. If the packet is
|
|
|
|
+ * unsequenced (i.e. does not need an ACK), the packet is completed by the
|
|
|
|
+ * transmitter thread before dropping that reference.
|
|
|
|
+ *
|
|
|
|
+ * On receial of an ACK, the receiver thread removes and obtains the refernce to
|
|
|
|
+ * the packet from the pending set. On succes, the receiver thread will then
|
|
|
|
+ * complete the packet and drop its reference.
|
|
|
|
+ *
|
|
|
|
+ * On error, the completion callback is immediately run by on thread on which
|
|
|
|
+ * the error was detected.
|
|
|
|
+ *
|
|
|
|
+ * To ensure that a packet eventually leaves the system it is marked as "locked"
|
|
|
|
+ * directly before it is going to be completed or when it is canceled. Marking a
|
|
|
|
+ * packet as "locked" has the effect that passing and creating new references
|
|
|
|
+ * of the packet will be blocked. This means that the packet cannot be added
|
|
|
|
+ * to the queue, the pending set, and the timeout, or be picked up by the
|
|
|
|
+ * transmitter thread or receiver thread. To remove a packet from the system it
|
|
|
|
+ * has to be marked as locked and subsequently all references from the data
|
|
|
|
+ * structures (queue, pending) have to be removed. References held by threads
|
|
|
|
+ * will eventually be dropped automatically as their execution progresses.
|
|
|
|
+ *
|
|
|
|
+ * Note that the packet completion callback is, in case of success and for a
|
|
|
|
+ * sequenced packet, guaranteed to run on the receiver thread, thus providing a
|
|
|
|
+ * way to reliably identify responses to the packet. The packet completion
|
|
|
|
+ * callback is only run once and it does not indicate that the packet has fully
|
|
|
|
+ * left the system. In case of re-submission (and with somewhat unlikely
|
|
|
|
+ * timing), it may be possible that the packet is being re-transmitted while the
|
|
|
|
+ * completion callback runs. Completion will occur both on success and internal
|
|
|
|
+ * error, as well as when the packet is canceled.
|
|
|
|
+ *
|
|
|
|
+ * >> Flags <<
|
|
|
|
+ *
|
|
|
|
+ * Flags are used to indicate the state and progression of a packet. Some flags
|
|
|
|
+ * have stricter guarantees than other:
|
|
|
|
+ *
|
|
|
|
+ * - locked
|
|
|
|
+ * Indicates if the packet is locked. If the packet is locked, passing and/or
|
|
|
|
+ * creating additional references to the packet is forbidden. The packet thus
|
|
|
|
+ * may not be queued, dequeued, or removed or added to the pending set. Note
|
|
|
|
+ * that the packet state flags may still change (e.g. it may be marked as
|
|
|
|
+ * ACKed, transmitted, ...).
|
|
|
|
+ *
|
|
|
|
+ * - completed
|
|
|
|
+ * Indicates if the packet completion has been run or is about to be run. This
|
|
|
|
+ * flag is used to ensure that the packet completion callback is only run
|
|
|
|
+ * once.
|
|
|
|
+ *
|
|
|
|
+ * - queued
|
|
|
|
+ * Indicates if a packet is present in the submission queue or not. This flag
|
|
|
|
+ * must only be modified with the queue lock held, and must be coherent
|
|
|
|
+ * presence of the packet in the queue.
|
|
|
|
+ *
|
|
|
|
+ * - pending
|
|
|
|
+ * Indicates if a packet is present in the set of pending packets or not.
|
|
|
|
+ * This flag must only be modified with the pending lock held, and must be
|
|
|
|
+ * coherent presence of the packet in the pending set.
|
|
|
|
+ *
|
|
|
|
+ * - transmitting
|
|
|
|
+ * Indicates if the packet is currently transmitting. In case of
|
|
|
|
+ * re-transmissions, it is only safe to wait on the "transmitted" completion
|
|
|
|
+ * after this flag has been set. The completion will be set both in success
|
|
|
|
+ * and error case.
|
|
|
|
+ *
|
|
|
|
+ * - transmitted
|
|
|
|
+ * Indicates if the packet has been transmitted. This flag is not cleared by
|
|
|
|
+ * the system, thus it indicates the first transmission only.
|
|
|
|
+ *
|
|
|
|
+ * - acked
|
|
|
|
+ * Indicates if the packet has been acknowledged by the client. There are no
|
|
|
|
+ * other guarantees given. For example, the packet may still be canceled
|
|
|
|
+ * and/or the completion may be triggered an error even though this bit is
|
|
|
|
+ * set. Rely on the status provided by completion instead.
|
|
|
|
+ *
|
|
|
|
+ * - canceled
|
|
|
|
+ * Indicates if the packet has been canceled from the outside. There are no
|
|
|
|
+ * other guarantees given. Specifically, the packet may be completed by
|
|
|
|
+ * another part of the system before the cancellation attempts to complete it.
|
|
|
|
+ *
|
|
|
|
+ * >> General Notes <<
|
|
|
|
+ *
|
|
|
|
+ * To avoid deadlocks, if both queue and pending locks are required, the pending
|
|
|
|
+ * lock must be acquired before the queue lock.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Maximum number transmission attempts per sequenced packet in case of
|
|
|
|
+ * time-outs. Must be smaller than 16.
|
|
|
|
+ */
|
|
|
|
+#define SSH_PTL_MAX_PACKET_TRIES 3
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Timeout as ktime_t delta for ACKs. If we have not received an ACK in this
|
|
|
|
+ * time-frame after starting transmission, the packet will be re-submitted.
|
|
|
|
+ */
|
|
|
|
+#define SSH_PTL_PACKET_TIMEOUT ms_to_ktime(1000)
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Maximum time resolution for timeouts. Currently set to max(2 jiffies, 50ms).
|
|
|
|
+ * Should be larger than one jiffy to avoid direct re-scheduling of reaper
|
|
|
|
+ * work_struct.
|
|
|
|
+ */
|
|
|
|
+#define SSH_PTL_PACKET_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Maximum number of sequenced packets concurrently waiting for an ACK.
|
|
|
|
+ * Packets marked as blocking will not be transmitted while this limit is
|
|
|
|
+ * reached.
|
|
|
|
+ */
|
|
|
|
+#define SSH_PTL_MAX_PENDING 1
|
|
|
|
+
|
|
|
|
+#define SSH_PTL_RX_BUF_LEN 4096
|
|
|
|
+
|
|
|
|
+#define SSH_PTL_RX_FIFO_LEN 4096
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+enum ssh_ptl_state_flags {
|
|
|
|
+ SSH_PTL_SF_SHUTDOWN_BIT,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssh_ptl_ops {
|
|
|
|
+ void (*data_received)(struct ssh_ptl *p, const struct sshp_span *data);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssh_ptl {
|
|
|
|
+ struct serdev_device *serdev;
|
|
|
|
+ unsigned long state;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ spinlock_t lock;
|
|
|
|
+ struct list_head head;
|
|
|
|
+ } queue;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ spinlock_t lock;
|
|
|
|
+ struct list_head head;
|
|
|
|
+ atomic_t count;
|
|
|
|
+ } pending;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ bool thread_signal;
|
|
|
|
+ struct task_struct *thread;
|
|
|
|
+ struct wait_queue_head thread_wq;
|
|
|
|
+ struct wait_queue_head packet_wq;
|
|
|
|
+ struct ssh_packet *packet;
|
|
|
|
+ size_t offset;
|
|
|
|
+ } tx;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ struct task_struct *thread;
|
|
|
|
+ struct wait_queue_head wq;
|
|
|
|
+ struct kfifo fifo;
|
|
|
|
+ struct sshp_buf buf;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ u16 seqs[8];
|
|
|
|
+ u16 offset;
|
|
|
|
+ } blacklist;
|
|
|
|
+ } rx;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ ktime_t timeout;
|
|
|
|
+ ktime_t expires;
|
|
|
|
+ struct delayed_work reaper;
|
|
|
|
+ } rtx_timeout;
|
|
|
|
+
|
|
|
|
+ struct ssh_ptl_ops ops;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define __ssam_prcond(func, p, fmt, ...) \
|
|
|
|
+ do { \
|
|
|
|
+ if ((p)) \
|
|
|
|
+ func((p), fmt, ##__VA_ARGS__); \
|
|
|
|
+ } while (0);
|
|
|
|
+
|
|
|
|
+#define ptl_dbg(p, fmt, ...) dev_dbg(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
|
|
|
|
+#define ptl_info(p, fmt, ...) dev_info(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
|
|
|
|
+#define ptl_warn(p, fmt, ...) dev_warn(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
|
|
|
|
+#define ptl_err(p, fmt, ...) dev_err(&(p)->serdev->dev, fmt, ##__VA_ARGS__)
|
|
|
|
+#define ptl_dbg_cond(p, fmt, ...) __ssam_prcond(ptl_dbg, p, fmt, ##__VA_ARGS__)
|
|
|
|
+
|
|
|
|
+#define to_ssh_packet(ptr, member) \
|
|
|
|
+ container_of(ptr, struct ssh_packet, member)
|
|
|
|
+
|
|
|
|
+#define to_ssh_ptl(ptr, member) \
|
|
|
|
+ container_of(ptr, struct ssh_ptl, member)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#ifdef CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_ptl_should_drop_ack_packet - error injection hook to drop ACK packets
|
|
|
|
+ *
|
|
|
|
+ * Useful to test detection and handling of automated re-transmits by the EC.
|
|
|
|
+ * Specifically of packets that the EC consideres not-ACKed but the driver
|
|
|
|
+ * already consideres ACKed (due to dropped ACK). In this case, the EC
|
|
|
|
+ * re-transmits the packet-to-be-ACKed and the driver should detect it as
|
|
|
|
+ * duplicate/already handled. Note that the driver should still send an ACK
|
|
|
|
+ * for the re-transmitted packet.
|
|
|
|
+ */
|
|
|
|
+static noinline bool ssh_ptl_should_drop_ack_packet(void)
|
|
|
|
+{
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_ack_packet, TRUE);
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_ptl_should_drop_nak_packet - error injection hook to drop NAK packets
|
|
|
|
+ *
|
|
|
|
+ * Useful to test/force automated (timeout-based) re-transmit by the EC.
|
|
|
|
+ * Specifically, packets that have not reached the driver completely/with valid
|
|
|
|
+ * checksums. Only useful in combination with receival of (injected) bad data.
|
|
|
|
+ */
|
|
|
|
+static noinline bool ssh_ptl_should_drop_nak_packet(void)
|
|
|
|
+{
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_nak_packet, TRUE);
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_ptl_should_drop_dsq_packet - error injection hook to drop sequenced data
|
|
|
|
+ * packet
|
|
|
|
+ *
|
|
|
|
+ * Useful to test re-transmit timeout of the driver. If the data packet has not
|
|
|
|
+ * been ACKed after a certain time, the driver should re-transmit the packet up
|
|
|
|
+ * to limited number of times defined in SSH_PTL_MAX_PACKET_TRIES.
|
|
|
|
+ */
|
|
|
|
+static noinline bool ssh_ptl_should_drop_dsq_packet(void)
|
|
|
|
+{
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+ALLOW_ERROR_INJECTION(ssh_ptl_should_drop_dsq_packet, TRUE);
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_ptl_should_fail_write - error injection hook to make serdev_device_write
|
|
|
|
+ * fail
|
|
|
|
+ *
|
|
|
|
+ * Hook to simulate errors in serdev_device_write when transmitting packets.
|
|
|
|
+ */
|
|
|
|
+static noinline int ssh_ptl_should_fail_write(void)
|
|
|
|
+{
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+ALLOW_ERROR_INJECTION(ssh_ptl_should_fail_write, ERRNO);
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_ptl_should_corrupt_tx_data - error injection hook to simualte invalid
|
|
|
|
+ * data being sent to the EC
|
|
|
|
+ *
|
|
|
|
+ * Hook to simulate corrupt/invalid data being sent from host (driver) to EC.
|
|
|
|
+ * Causes the package data to be actively corrupted by overwriting it with
|
|
|
|
+ * pre-defined values, such that it becomes invalid, causing the EC to respond
|
|
|
|
+ * with a NAK packet. Useful to test handling of NAK packets received by the
|
|
|
|
+ * driver.
|
|
|
|
+ */
|
|
|
|
+static noinline bool ssh_ptl_should_corrupt_tx_data(void)
|
|
|
|
+{
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_tx_data, TRUE);
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_ptl_should_corrupt_rx_syn - error injection hook to simulate invalid
|
|
|
|
+ * data being sent by the EC
|
|
|
|
+ *
|
|
|
|
+ * Hook to simulate invalid SYN bytes, i.e. an invalid start of messages and
|
|
|
|
+ * test handling thereof in the driver.
|
|
|
|
+ */
|
|
|
|
+static noinline bool ssh_ptl_should_corrupt_rx_syn(void)
|
|
|
|
+{
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_syn, TRUE);
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_ptl_should_corrupt_rx_data - error injection hook to simulate invalid
|
|
|
|
+ * data being sent by the EC
|
|
|
|
+ *
|
|
|
|
+ * Hook to simulate invalid data/checksum of the message frame and test handling
|
|
|
|
+ * thereof in the driver.
|
|
|
|
+ */
|
|
|
|
+static noinline bool ssh_ptl_should_corrupt_rx_data(void)
|
|
|
|
+{
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+ALLOW_ERROR_INJECTION(ssh_ptl_should_corrupt_rx_data, TRUE);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static inline bool __ssh_ptl_should_drop_ack_packet(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ if (likely(!ssh_ptl_should_drop_ack_packet()))
|
|
|
|
+ return false;
|
|
|
|
+
|
|
|
|
+ trace_ssam_ei_tx_drop_ack_packet(packet);
|
|
|
|
+ ptl_info(packet->ptl, "packet error injection: dropping ACK packet %p\n",
|
|
|
|
+ packet);
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline bool __ssh_ptl_should_drop_nak_packet(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ if (likely(!ssh_ptl_should_drop_nak_packet()))
|
|
|
|
+ return false;
|
|
|
|
+
|
|
|
|
+ trace_ssam_ei_tx_drop_nak_packet(packet);
|
|
|
|
+ ptl_info(packet->ptl, "packet error injection: dropping NAK packet %p\n",
|
|
|
|
+ packet);
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline bool __ssh_ptl_should_drop_dsq_packet(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ if (likely(!ssh_ptl_should_drop_dsq_packet()))
|
|
|
|
+ return false;
|
|
|
|
+
|
|
|
|
+ trace_ssam_ei_tx_drop_dsq_packet(packet);
|
|
|
|
+ ptl_info(packet->ptl,
|
|
|
|
+ "packet error injection: dropping sequenced data packet %p\n",
|
|
|
|
+ packet);
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ // ignore packets that don't carry any data (i.e. flush)
|
|
|
|
+ if (!packet->data || !packet->data_length)
|
|
|
|
+ return false;
|
|
|
|
+
|
|
|
|
+ switch (packet->data[SSH_MSGOFFSET_FRAME(type)]) {
|
|
|
|
+ case SSH_FRAME_TYPE_ACK:
|
|
|
|
+ return __ssh_ptl_should_drop_ack_packet(packet);
|
|
|
|
+
|
|
|
|
+ case SSH_FRAME_TYPE_NAK:
|
|
|
|
+ return __ssh_ptl_should_drop_nak_packet(packet);
|
|
|
|
+
|
|
|
|
+ case SSH_FRAME_TYPE_DATA_SEQ:
|
|
|
|
+ return __ssh_ptl_should_drop_dsq_packet(packet);
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline int ssh_ptl_write_buf(struct ssh_ptl *ptl,
|
|
|
|
+ struct ssh_packet *packet,
|
|
|
|
+ const unsigned char *buf,
|
|
|
|
+ size_t count)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = ssh_ptl_should_fail_write();
|
|
|
|
+ if (unlikely(status)) {
|
|
|
|
+ trace_ssam_ei_tx_fail_write(packet, status);
|
|
|
|
+ ptl_info(packet->ptl,
|
|
|
|
+ "packet error injection: simulating transmit error %d, packet %p\n",
|
|
|
|
+ status, packet);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return serdev_device_write_buf(ptl->serdev, buf, count);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ // ignore packets that don't carry any data (i.e. flush)
|
|
|
|
+ if (!packet->data || !packet->data_length)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ // only allow sequenced data packets to be modified
|
|
|
|
+ if (packet->data[SSH_MSGOFFSET_FRAME(type)] != SSH_FRAME_TYPE_DATA_SEQ)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (likely(!ssh_ptl_should_corrupt_tx_data()))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ trace_ssam_ei_tx_corrupt_data(packet);
|
|
|
|
+ ptl_info(packet->ptl,
|
|
|
|
+ "packet error injection: simulating invalid transmit data on packet %p\n",
|
|
|
|
+ packet);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * NB: The value 0xb3 has been chosen more or less randomly so that it
|
|
|
|
+ * doesn't have any (major) overlap with the SYN bytes (aa 55) and is
|
|
|
|
+ * non-trivial (i.e. non-zero, non-0xff).
|
|
|
|
+ */
|
|
|
|
+ memset(packet->data, 0xb3, packet->data_length);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
|
|
|
|
+ struct sshp_span *data)
|
|
|
|
+{
|
|
|
|
+ struct sshp_span frame;
|
|
|
|
+
|
|
|
|
+ // check if there actually is something to corrupt
|
|
|
|
+ if (!sshp_find_syn(data, &frame))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (likely(!ssh_ptl_should_corrupt_rx_syn()))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ trace_ssam_ei_rx_corrupt_syn("data_length", data->len);
|
|
|
|
+
|
|
|
|
+ data->ptr[1] = 0xb3; // set second byte of SYN to "random" value
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
|
|
|
|
+ struct sshp_span *frame)
|
|
|
|
+{
|
|
|
|
+ size_t payload_len, message_len;
|
|
|
|
+ struct ssh_frame *sshf;
|
|
|
|
+
|
|
|
|
+ // ignore incomplete messages, will get handled once it's complete
|
|
|
|
+ if (frame->len < SSH_MESSAGE_LENGTH(0))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ // ignore incomplete messages, part 2
|
|
|
|
+ payload_len = get_unaligned_le16(&frame->ptr[SSH_MSGOFFSET_FRAME(len)]);
|
|
|
|
+ message_len = SSH_MESSAGE_LENGTH(payload_len);
|
|
|
|
+ if (frame->len < message_len)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (likely(!ssh_ptl_should_corrupt_rx_data()))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ sshf = (struct ssh_frame *)&frame->ptr[SSH_MSGOFFSET_FRAME(type)];
|
|
|
|
+ trace_ssam_ei_rx_corrupt_data(sshf);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Flip bits in first byte of payload checksum. This is basically
|
|
|
|
+ * equivalent to a payload/frame data error without us having to worry
|
|
|
|
+ * about (the, arguably pretty small, probability of) accidental
|
|
|
|
+ * checksum collisions.
|
|
|
|
+ */
|
|
|
|
+ frame->ptr[frame->len - 2] = ~frame->ptr[frame->len - 2];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#else /* CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION */
|
|
|
|
+
|
|
|
|
+static inline bool ssh_ptl_should_drop_packet(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline int ssh_ptl_write_buf(struct ssh_ptl *ptl,
|
|
|
|
+ struct ssh_packet *packet,
|
|
|
|
+ const unsigned char *buf,
|
|
|
|
+ size_t count)
|
|
|
|
+{
|
|
|
|
+ return serdev_device_write_buf(ptl->serdev, buf, count);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_ptl_tx_inject_invalid_data(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_ptl_rx_inject_invalid_syn(struct ssh_ptl *ptl,
|
|
|
|
+ struct sshp_span *data)
|
|
|
|
+{
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_ptl_rx_inject_invalid_data(struct ssh_ptl *ptl,
|
|
|
|
+ struct sshp_span *frame)
|
|
|
|
+{
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#endif /* CONFIG_SURFACE_SAM_SSH_ERROR_INJECTION */
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void __ssh_ptl_packet_release(struct kref *kref)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet *p = to_ssh_packet(kref, refcnt);
|
|
|
|
+
|
|
|
|
+ trace_ssam_packet_release(p);
|
|
|
|
+
|
|
|
|
+ ptl_dbg_cond(p->ptl, "ptl: releasing packet %p\n", p);
|
|
|
|
+ p->ops->release(p);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_packet_get(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ kref_get(&packet->refcnt);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_packet_put(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ kref_put(&packet->refcnt, __ssh_ptl_packet_release);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static inline u8 ssh_packet_get_seq(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ return packet->data[SSH_MSGOFFSET_FRAME(seq)];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssh_packet_args {
|
|
|
|
+ u8 type;
|
|
|
|
+ u8 priority;
|
|
|
|
+ const struct ssh_packet_ops *ops;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static void ssh_packet_init(struct ssh_packet *packet,
|
|
|
|
+ const struct ssh_packet_args *args)
|
|
|
|
+{
|
|
|
|
+ kref_init(&packet->refcnt);
|
|
|
|
+
|
|
|
|
+ packet->ptl = NULL;
|
|
|
|
+ INIT_LIST_HEAD(&packet->queue_node);
|
|
|
|
+ INIT_LIST_HEAD(&packet->pending_node);
|
|
|
|
+
|
|
|
|
+ packet->type = args->type;
|
|
|
|
+ packet->priority = args->priority;
|
|
|
|
+ packet->state = 0;
|
|
|
|
+ packet->timestamp = KTIME_MAX;
|
|
|
|
+
|
|
|
|
+ packet->data_length = 0;
|
|
|
|
+ packet->data = NULL;
|
|
|
|
+
|
|
|
|
+ packet->ops = args->ops;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static struct ssh_packet *ptl_alloc_ctrl_packet(
|
|
|
|
+ struct ssh_ptl *ptl, const struct ssh_packet_args *args,
|
|
|
|
+ gfp_t flags)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet *packet;
|
|
|
|
+
|
|
|
|
+ // TODO: chache packets
|
|
|
|
+
|
|
|
|
+ packet = kzalloc(sizeof(struct ssh_packet) + SSH_MSG_LEN_CTRL, flags);
|
|
|
|
+ if (!packet)
|
|
|
|
+ return NULL;
|
|
|
|
+
|
|
|
|
+ ssh_packet_init(packet, args);
|
|
|
|
+ packet->data_length = SSH_MSG_LEN_CTRL;
|
|
|
|
+ packet->data = ((u8 *) packet) + sizeof(struct ssh_packet);
|
|
|
|
+
|
|
|
|
+ return packet;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ptl_free_ctrl_packet(struct ssh_packet *p)
|
|
|
|
+{
|
|
|
|
+ // TODO: chache packets
|
|
|
|
+
|
|
|
|
+ kfree(p);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static const struct ssh_packet_ops ssh_ptl_ctrl_packet_ops = {
|
|
|
|
+ .complete = NULL,
|
|
|
|
+ .release = ptl_free_ctrl_packet,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_timeout_reaper_mod(struct ssh_ptl *ptl, ktime_t now,
|
|
|
|
+ ktime_t expires)
|
|
|
|
+{
|
|
|
|
+ unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
|
|
|
|
+ ktime_t aexp = ktime_add(expires, SSH_PTL_PACKET_TIMEOUT_RESOLUTION);
|
|
|
|
+ ktime_t old;
|
|
|
|
+
|
|
|
|
+ // re-adjust / schedule reaper if it is above resolution delta
|
|
|
|
+ old = READ_ONCE(ptl->rtx_timeout.expires);
|
|
|
|
+ while (ktime_before(aexp, old))
|
|
|
|
+ old = cmpxchg64(&ptl->rtx_timeout.expires, old, expires);
|
|
|
|
+
|
|
|
|
+ // if we updated the reaper expiration, modify work timeout
|
|
|
|
+ if (old == expires)
|
|
|
|
+ mod_delayed_work(system_wq, &ptl->rtx_timeout.reaper, delta);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_timeout_start(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = packet->ptl;
|
|
|
|
+ ktime_t timestamp = ktime_get_coarse_boottime();
|
|
|
|
+ ktime_t timeout = ptl->rtx_timeout.timeout;
|
|
|
|
+
|
|
|
|
+ if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ WRITE_ONCE(packet->timestamp, timestamp);
|
|
|
|
+ smp_mb__after_atomic();
|
|
|
|
+
|
|
|
|
+ ssh_ptl_timeout_reaper_mod(packet->ptl, timestamp, timestamp + timeout);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static struct list_head *__ssh_ptl_queue_find_entrypoint(struct ssh_packet *p)
|
|
|
|
+{
|
|
|
|
+ struct list_head *head;
|
|
|
|
+ u8 priority = READ_ONCE(p->priority);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * We generally assume that there are less control (ACK/NAK) packets and
|
|
|
|
+ * re-submitted data packets as there are normal data packets (at least
|
|
|
|
+ * in situations in which many packets are queued; if there aren't many
|
|
|
|
+ * packets queued the decision on how to iterate should be basically
|
|
|
|
+ * irrellevant; the number of control/data packets is more or less
|
|
|
|
+ * limited via the maximum number of pending packets). Thus, when
|
|
|
|
+ * inserting a control or re-submitted data packet, (determined by their
|
|
|
|
+ * priority), we search from front to back. Normal data packets are,
|
|
|
|
+ * usually queued directly at the tail of the queue, so for those search
|
|
|
|
+ * from back to front.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ if (priority > SSH_PACKET_PRIORITY_DATA) {
|
|
|
|
+ list_for_each(head, &p->ptl->queue.head) {
|
|
|
|
+ p = list_entry(head, struct ssh_packet, queue_node);
|
|
|
|
+
|
|
|
|
+ if (READ_ONCE(p->priority) < priority)
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ list_for_each_prev(head, &p->ptl->queue.head) {
|
|
|
|
+ p = list_entry(head, struct ssh_packet, queue_node);
|
|
|
|
+
|
|
|
|
+ if (READ_ONCE(p->priority) >= priority) {
|
|
|
|
+ head = head->next;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return head;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_queue_push(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = packet->ptl;
|
|
|
|
+ struct list_head *head;
|
|
|
|
+
|
|
|
|
+ spin_lock(&ptl->queue.lock);
|
|
|
|
+
|
|
|
|
+ if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state)) {
|
|
|
|
+ spin_unlock(&ptl->queue.lock);
|
|
|
|
+ return -ESHUTDOWN;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // avoid further transitions when cancelling/completing
|
|
|
|
+ if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state)) {
|
|
|
|
+ spin_unlock(&ptl->queue.lock);
|
|
|
|
+ return -EINVAL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // if this packet has already been queued, do not add it
|
|
|
|
+ if (test_and_set_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state)) {
|
|
|
|
+ spin_unlock(&ptl->queue.lock);
|
|
|
|
+ return -EALREADY;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ head = __ssh_ptl_queue_find_entrypoint(packet);
|
|
|
|
+
|
|
|
|
+ ssh_packet_get(packet);
|
|
|
|
+ list_add_tail(&packet->queue_node, &ptl->queue.head);
|
|
|
|
+
|
|
|
|
+ spin_unlock(&ptl->queue.lock);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_queue_remove(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = packet->ptl;
|
|
|
|
+ bool remove;
|
|
|
|
+
|
|
|
|
+ spin_lock(&ptl->queue.lock);
|
|
|
|
+
|
|
|
|
+ remove = test_and_clear_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state);
|
|
|
|
+ if (remove)
|
|
|
|
+ list_del(&packet->queue_node);
|
|
|
|
+
|
|
|
|
+ spin_unlock(&ptl->queue.lock);
|
|
|
|
+
|
|
|
|
+ if (remove)
|
|
|
|
+ ssh_packet_put(packet);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_pending_push(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = packet->ptl;
|
|
|
|
+
|
|
|
|
+ spin_lock(&ptl->pending.lock);
|
|
|
|
+
|
|
|
|
+ // if we are cancelling/completing this packet, do not add it
|
|
|
|
+ if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state)) {
|
|
|
|
+ spin_unlock(&ptl->pending.lock);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // in case it is already pending (e.g. re-submission), do not add it
|
|
|
|
+ if (test_and_set_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state)) {
|
|
|
|
+ spin_unlock(&ptl->pending.lock);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ atomic_inc(&ptl->pending.count);
|
|
|
|
+ ssh_packet_get(packet);
|
|
|
|
+ list_add_tail(&packet->pending_node, &ptl->pending.head);
|
|
|
|
+
|
|
|
|
+ spin_unlock(&ptl->pending.lock);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_pending_remove(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = packet->ptl;
|
|
|
|
+ bool remove;
|
|
|
|
+
|
|
|
|
+ spin_lock(&ptl->pending.lock);
|
|
|
|
+
|
|
|
|
+ remove = test_and_clear_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state);
|
|
|
|
+ if (remove) {
|
|
|
|
+ list_del(&packet->pending_node);
|
|
|
|
+ atomic_dec(&ptl->pending.count);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ spin_unlock(&ptl->pending.lock);
|
|
|
|
+
|
|
|
|
+ if (remove)
|
|
|
|
+ ssh_packet_put(packet);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void __ssh_ptl_complete(struct ssh_packet *p, int status)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = READ_ONCE(p->ptl);
|
|
|
|
+
|
|
|
|
+ trace_ssam_packet_complete(p, status);
|
|
|
|
+
|
|
|
|
+ ptl_dbg_cond(ptl, "ptl: completing packet %p\n", p);
|
|
|
|
+ if (p->ops->complete)
|
|
|
|
+ p->ops->complete(p, status);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_remove_and_complete(struct ssh_packet *p, int status)
|
|
|
|
+{
|
|
|
|
+ /*
|
|
|
|
+ * A call to this function should in general be preceeded by
|
|
|
|
+ * set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->flags) to avoid re-adding the
|
|
|
|
+ * packet to the structures it's going to be removed from.
|
|
|
|
+ *
|
|
|
|
+ * The set_bit call does not need explicit memory barriers as the
|
|
|
|
+ * implicit barrier of the test_and_set_bit call below ensure that the
|
|
|
|
+ * flag is visible before we actually attempt to remove the packet.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ if (test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ ssh_ptl_queue_remove(p);
|
|
|
|
+ ssh_ptl_pending_remove(p);
|
|
|
|
+
|
|
|
|
+ __ssh_ptl_complete(p, status);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static bool ssh_ptl_tx_can_process(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = packet->ptl;
|
|
|
|
+
|
|
|
|
+ if (packet->type & SSH_PACKET_TY_FLUSH)
|
|
|
|
+ return !atomic_read(&ptl->pending.count);
|
|
|
|
+
|
|
|
|
+ // we can alwas process non-blocking packets
|
|
|
|
+ if (!(packet->type & SSH_PACKET_TY_BLOCKING))
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ // if we are already waiting for this packet, send it again
|
|
|
|
+ if (test_bit(SSH_PACKET_SF_PENDING_BIT, &packet->state))
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ // otherwise: check if we have the capacity to send
|
|
|
|
+ return atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct ssh_packet *ssh_ptl_tx_pop(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet *packet = ERR_PTR(-ENOENT);
|
|
|
|
+ struct ssh_packet *p, *n;
|
|
|
|
+
|
|
|
|
+ spin_lock(&ptl->queue.lock);
|
|
|
|
+ list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
|
|
|
|
+ /*
|
|
|
|
+ * If we are cancelling or completing this packet, ignore it.
|
|
|
|
+ * It's going to be removed from this queue shortly.
|
|
|
|
+ */
|
2020-07-15 20:26:02 +00:00
|
|
|
+ if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
|
2020-07-02 21:19:52 +00:00
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Packets should be ordered non-blocking/to-be-resent first.
|
|
|
|
+ * If we cannot process this packet, assume that we can't
|
|
|
|
+ * process any following packet either and abort.
|
|
|
|
+ */
|
|
|
|
+ if (!ssh_ptl_tx_can_process(p)) {
|
|
|
|
+ packet = ERR_PTR(-EBUSY);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * We are allowed to change the state now. Remove it from the
|
|
|
|
+ * queue and mark it as being transmitted. Note that we cannot
|
|
|
|
+ * add it to the set of pending packets yet, as queue locks must
|
|
|
|
+ * always be acquired before packet locks (otherwise we might
|
|
|
|
+ * run into a deadlock).
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ list_del(&p->queue_node);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Ensure that the "queued" bit gets cleared after setting the
|
|
|
|
+ * "transmitting" bit to guaranteee non-zero flags.
|
|
|
|
+ */
|
|
|
|
+ set_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &p->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
|
|
|
|
+
|
|
|
|
+ packet = p;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ spin_unlock(&ptl->queue.lock);
|
|
|
|
+
|
|
|
|
+ return packet;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct ssh_packet *ssh_ptl_tx_next(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet *p;
|
|
|
|
+
|
|
|
|
+ p = ssh_ptl_tx_pop(ptl);
|
|
|
|
+ if (IS_ERR(p))
|
|
|
|
+ return p;
|
|
|
|
+
|
|
|
|
+ if (p->type & SSH_PACKET_TY_SEQUENCED) {
|
|
|
|
+ ptl_dbg(ptl, "ptl: transmitting sequenced packet %p\n", p);
|
|
|
|
+ ssh_ptl_pending_push(p);
|
|
|
|
+ ssh_ptl_timeout_start(p);
|
|
|
|
+ } else {
|
|
|
|
+ ptl_dbg(ptl, "ptl: transmitting non-sequenced packet %p\n", p);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Update number of tries. This directly influences the priority in case
|
|
|
|
+ * the packet is re-submitted (e.g. via timeout/NAK). Note that this is
|
|
|
|
+ * the only place where we update the priority in-flight. As this runs
|
|
|
|
+ * only on the tx-thread, this read-modify-write procedure is safe.
|
|
|
|
+ */
|
|
|
|
+ WRITE_ONCE(p->priority, READ_ONCE(p->priority) + 1);
|
|
|
|
+
|
|
|
|
+ return p;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_tx_compl_success(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = packet->ptl;
|
|
|
|
+
|
|
|
|
+ ptl_dbg(ptl, "ptl: successfully transmitted packet %p\n", packet);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Transition to state to "transmitted". Ensure that the flags never get
|
|
|
|
+ * zero with barrier.
|
|
|
|
+ */
|
|
|
|
+ set_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
|
|
|
|
+
|
|
|
|
+ // if the packet is unsequenced, we're done: lock and complete
|
|
|
|
+ if (!(packet->type & SSH_PACKET_TY_SEQUENCED)) {
|
|
|
|
+ set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
|
|
|
|
+ ssh_ptl_remove_and_complete(packet, 0);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Notify that a packet transmission has finished. In general we're only
|
|
|
|
+ * waiting for one packet (if any), so wake_up_all should be fine.
|
|
|
|
+ */
|
|
|
|
+ wake_up_all(&ptl->tx.packet_wq);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_tx_compl_error(struct ssh_packet *packet, int status)
|
|
|
|
+{
|
|
|
|
+ /*
|
|
|
|
+ * Transmission failure: Lock the packet and try to complete it. Ensure
|
|
|
|
+ * that the flags never get zero with barrier.
|
|
|
|
+ */
|
|
|
|
+ set_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &packet->state);
|
|
|
|
+
|
|
|
|
+ ptl_err(packet->ptl, "ptl: transmission error: %d\n", status);
|
|
|
|
+ ptl_dbg(packet->ptl, "ptl: failed to transmit packet: %p\n", packet);
|
|
|
|
+
|
|
|
|
+ ssh_ptl_remove_and_complete(packet, status);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Notify that a packet transmission has finished. In general we're only
|
|
|
|
+ * waiting for one packet (if any), so wake_up_all should be fine.
|
|
|
|
+ */
|
|
|
|
+ wake_up_all(&packet->ptl->tx.packet_wq);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_tx_threadfn_wait(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ wait_event_interruptible(ptl->tx.thread_wq,
|
|
|
|
+ READ_ONCE(ptl->tx.thread_signal) || kthread_should_stop());
|
|
|
|
+ WRITE_ONCE(ptl->tx.thread_signal, false);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_tx_threadfn(void *data)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = data;
|
|
|
|
+
|
|
|
|
+ while (!kthread_should_stop()) {
|
|
|
|
+ unsigned char *buf;
|
|
|
|
+ bool drop = false;
|
|
|
|
+ size_t len = 0;
|
|
|
|
+ int status = 0;
|
|
|
|
+
|
|
|
|
+ // if we don't have a packet, get the next and add it to pending
|
|
|
|
+ if (IS_ERR_OR_NULL(ptl->tx.packet)) {
|
|
|
|
+ ptl->tx.packet = ssh_ptl_tx_next(ptl);
|
|
|
|
+ ptl->tx.offset = 0;
|
|
|
|
+
|
|
|
|
+ // if no packet is available, we are done
|
|
|
|
+ if (IS_ERR(ptl->tx.packet)) {
|
|
|
|
+ ssh_ptl_tx_threadfn_wait(ptl);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // error injection: drop packet to simulate transmission problem
|
|
|
|
+ if (ptl->tx.offset == 0)
|
|
|
|
+ drop = ssh_ptl_should_drop_packet(ptl->tx.packet);
|
|
|
|
+
|
|
|
|
+ // error injection: simulate invalid packet data
|
|
|
|
+ if (ptl->tx.offset == 0 && !drop)
|
|
|
|
+ ssh_ptl_tx_inject_invalid_data(ptl->tx.packet);
|
|
|
|
+
|
|
|
|
+ // flush-packets don't have any data
|
|
|
|
+ if (likely(ptl->tx.packet->data && !drop)) {
|
|
|
|
+ buf = ptl->tx.packet->data + ptl->tx.offset;
|
|
|
|
+ len = ptl->tx.packet->data_length - ptl->tx.offset;
|
|
|
|
+
|
|
|
|
+ ptl_dbg(ptl, "tx: sending data (length: %zu)\n", len);
|
|
|
|
+ print_hex_dump_debug("tx: ", DUMP_PREFIX_OFFSET, 16, 1,
|
|
|
|
+ buf, len, false);
|
|
|
|
+
|
|
|
|
+ status = ssh_ptl_write_buf(ptl, ptl->tx.packet, buf, len);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (status < 0) {
|
|
|
|
+ // complete packet with error
|
|
|
|
+ ssh_ptl_tx_compl_error(ptl->tx.packet, status);
|
|
|
|
+ ssh_packet_put(ptl->tx.packet);
|
|
|
|
+ ptl->tx.packet = NULL;
|
|
|
|
+
|
|
|
|
+ } else if (status == len) {
|
|
|
|
+ // complete packet and/or mark as transmitted
|
|
|
|
+ ssh_ptl_tx_compl_success(ptl->tx.packet);
|
|
|
|
+ ssh_packet_put(ptl->tx.packet);
|
|
|
|
+ ptl->tx.packet = NULL;
|
|
|
|
+
|
|
|
|
+ } else { // need more buffer space
|
|
|
|
+ ptl->tx.offset += status;
|
|
|
|
+ ssh_ptl_tx_threadfn_wait(ptl);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // cancel active packet before we actually stop
|
|
|
|
+ if (!IS_ERR_OR_NULL(ptl->tx.packet)) {
|
|
|
|
+ ssh_ptl_tx_compl_error(ptl->tx.packet, -ESHUTDOWN);
|
|
|
|
+ ssh_packet_put(ptl->tx.packet);
|
|
|
|
+ ptl->tx.packet = NULL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_ptl_tx_wakeup(struct ssh_ptl *ptl, bool force)
|
|
|
|
+{
|
|
|
|
+ if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (force || atomic_read(&ptl->pending.count) < SSH_PTL_MAX_PENDING) {
|
|
|
|
+ WRITE_ONCE(ptl->tx.thread_signal, true);
|
|
|
|
+ smp_mb__after_atomic();
|
|
|
|
+ wake_up(&ptl->tx.thread_wq);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_tx_start(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ ptl->tx.thread = kthread_run(ssh_ptl_tx_threadfn, ptl, "surface-sh-tx");
|
|
|
|
+ if (IS_ERR(ptl->tx.thread))
|
|
|
|
+ return PTR_ERR(ptl->tx.thread);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_tx_stop(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ int status = 0;
|
|
|
|
+
|
|
|
|
+ if (ptl->tx.thread) {
|
|
|
|
+ status = kthread_stop(ptl->tx.thread);
|
|
|
|
+ ptl->tx.thread = NULL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static struct ssh_packet *ssh_ptl_ack_pop(struct ssh_ptl *ptl, u8 seq_id)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet *packet = ERR_PTR(-ENOENT);
|
|
|
|
+ struct ssh_packet *p, *n;
|
|
|
|
+
|
|
|
|
+ spin_lock(&ptl->pending.lock);
|
|
|
|
+ list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
|
|
|
|
+ /*
|
|
|
|
+ * We generally expect packets to be in order, so first packet
|
|
|
|
+ * to be added to pending is first to be sent, is first to be
|
|
|
|
+ * ACKed.
|
|
|
|
+ */
|
|
|
|
+ if (unlikely(ssh_packet_get_seq(p) != seq_id))
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * In case we receive an ACK while handling a transmission error
|
|
|
|
+ * completion. The packet will be removed shortly.
|
|
|
|
+ */
|
|
|
|
+ if (unlikely(test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
|
|
|
|
+ packet = ERR_PTR(-EPERM);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Mark packet as ACKed and remove it from pending. Ensure that
|
|
|
|
+ * the flags never get zero with barrier.
|
|
|
|
+ */
|
|
|
|
+ set_bit(SSH_PACKET_SF_ACKED_BIT, &p->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
|
|
|
|
+
|
|
|
|
+ atomic_dec(&ptl->pending.count);
|
|
|
|
+ list_del(&p->pending_node);
|
|
|
|
+ packet = p;
|
|
|
|
+
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ spin_unlock(&ptl->pending.lock);
|
|
|
|
+
|
|
|
|
+ return packet;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_wait_until_transmitted(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ wait_event(packet->ptl->tx.packet_wq,
|
|
|
|
+ test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &packet->state)
|
|
|
|
+ || test_bit(SSH_PACKET_SF_LOCKED_BIT, &packet->state));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_acknowledge(struct ssh_ptl *ptl, u8 seq)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet *p;
|
|
|
|
+ int status = 0;
|
|
|
|
+
|
|
|
|
+ p = ssh_ptl_ack_pop(ptl, seq);
|
|
|
|
+ if (IS_ERR(p)) {
|
|
|
|
+ if (PTR_ERR(p) == -ENOENT) {
|
|
|
|
+ /*
|
|
|
|
+ * The packet has not been found in the set of pending
|
|
|
|
+ * packets.
|
|
|
|
+ */
|
|
|
|
+ ptl_warn(ptl, "ptl: received ACK for non-pending"
|
|
|
|
+ " packet\n");
|
|
|
|
+ } else {
|
|
|
|
+ /*
|
|
|
|
+ * The packet is pending, but we are not allowed to take
|
|
|
|
+ * it because it has been locked.
|
|
|
|
+ */
|
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ptl_dbg(ptl, "ptl: received ACK for packet %p\n", p);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * It is possible that the packet has been transmitted, but the state
|
|
|
|
+ * has not been updated from "transmitting" to "transmitted" yet.
|
|
|
|
+ * In that case, we need to wait for this transition to occur in order
|
|
|
|
+ * to determine between success or failure.
|
|
|
|
+ */
|
|
|
|
+ if (test_bit(SSH_PACKET_SF_TRANSMITTING_BIT, &p->state))
|
|
|
|
+ ssh_ptl_wait_until_transmitted(p);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * The packet will already be locked in case of a transmission error or
|
|
|
|
+ * cancellation. Let the transmitter or cancellation issuer complete the
|
|
|
|
+ * packet.
|
|
|
|
+ */
|
|
|
|
+ if (unlikely(test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))) {
|
|
|
|
+ ssh_packet_put(p);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (unlikely(!test_bit(SSH_PACKET_SF_TRANSMITTED_BIT, &p->state))) {
|
|
|
|
+ ptl_err(ptl, "ptl: received ACK before packet had been fully"
|
|
|
|
+ " transmitted\n");
|
|
|
|
+ status = -EREMOTEIO;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ssh_ptl_remove_and_complete(p, status);
|
|
|
|
+ ssh_packet_put(p);
|
|
|
|
+
|
|
|
|
+ ssh_ptl_tx_wakeup(ptl, false);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_submit(struct ssh_ptl *ptl, struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ trace_ssam_packet_submit(packet);
|
|
|
|
+
|
|
|
|
+ // validate packet fields
|
|
|
|
+ if (packet->type & SSH_PACKET_TY_FLUSH) {
|
|
|
|
+ if (packet->data || (packet->type & SSH_PACKET_TY_SEQUENCED))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+ } else if (!packet->data) {
|
|
|
|
+ return -EINVAL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * This function is currently not intended for re-submission. The ptl
|
|
|
|
+ * reference only gets set on the first submission. After the first
|
|
|
|
+ * submission, it has to be read-only.
|
|
|
|
+ *
|
|
|
|
+ * Use cmpxchg to ensure safety with regards to ssh_ptl_cancel and
|
|
|
|
+ * re-entry, where we can't guarantee that the packet has been submitted
|
|
|
|
+ * yet.
|
|
|
|
+ *
|
|
|
|
+ * The implicit barrier of cmpxchg is paired with barrier in
|
|
|
|
+ * ssh_ptl_cancel to guarantee cancelation in case the packet has never
|
|
|
|
+ * been submitted or is currently being submitted.
|
|
|
|
+ */
|
|
|
|
+ if (cmpxchg(&packet->ptl, NULL, ptl) != NULL)
|
|
|
|
+ return -EALREADY;
|
|
|
|
+
|
|
|
|
+ status = ssh_ptl_queue_push(packet);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ ssh_ptl_tx_wakeup(ptl, !(packet->type & SSH_PACKET_TY_BLOCKING));
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void __ssh_ptl_resubmit(struct ssh_packet *packet)
|
|
|
|
+{
|
|
|
|
+ struct list_head *head;
|
|
|
|
+
|
|
|
|
+ trace_ssam_packet_resubmit(packet);
|
|
|
|
+
|
|
|
|
+ spin_lock(&packet->ptl->queue.lock);
|
|
|
|
+
|
|
|
|
+ // if this packet has already been queued, do not add it
|
|
|
|
+ if (test_and_set_bit(SSH_PACKET_SF_QUEUED_BIT, &packet->state)) {
|
|
|
|
+ spin_unlock(&packet->ptl->queue.lock);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // find first node with lower priority
|
|
|
|
+ head = __ssh_ptl_queue_find_entrypoint(packet);
|
|
|
|
+
|
|
|
|
+ WRITE_ONCE(packet->timestamp, KTIME_MAX);
|
|
|
|
+ smp_mb__after_atomic();
|
|
|
|
+
|
|
|
|
+ // add packet
|
|
|
|
+ ssh_packet_get(packet);
|
|
|
|
+ list_add_tail(&packet->queue_node, head);
|
|
|
|
+
|
|
|
|
+ spin_unlock(&packet->ptl->queue.lock);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_resubmit_pending(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet *p;
|
|
|
|
+ bool resub = false;
|
|
|
|
+ u8 try;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Note: We deliberately do not remove/attempt to cancel and complete
|
|
|
|
+ * packets that are out of tires in this function. The packet will be
|
|
|
|
+ * eventually canceled and completed by the timeout. Removing the packet
|
|
|
|
+ * here could lead to overly eager cancelation if the packet has not
|
|
|
|
+ * been re-transmitted yet but the tries-counter already updated (i.e
|
|
|
|
+ * ssh_ptl_tx_next removed the packet from the queue and updated the
|
|
|
|
+ * counter, but re-transmission for the last try has not actually
|
|
|
|
+ * started yet).
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ spin_lock(&ptl->pending.lock);
|
|
|
|
+
|
|
|
|
+ // re-queue all pending packets
|
|
|
|
+ list_for_each_entry(p, &ptl->pending.head, pending_node) {
|
|
|
|
+ // avoid further transitions if locked
|
|
|
|
+ if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ // do not re-schedule if packet is out of tries
|
|
|
|
+ try = ssh_packet_priority_get_try(READ_ONCE(p->priority));
|
|
|
|
+ if (try >= SSH_PTL_MAX_PACKET_TRIES)
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ resub = true;
|
|
|
|
+ __ssh_ptl_resubmit(p);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ spin_unlock(&ptl->pending.lock);
|
|
|
|
+
|
|
|
|
+ ssh_ptl_tx_wakeup(ptl, resub);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_cancel(struct ssh_packet *p)
|
|
|
|
+{
|
|
|
|
+ if (test_and_set_bit(SSH_PACKET_SF_CANCELED_BIT, &p->state))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ trace_ssam_packet_cancel(p);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Lock packet and commit with memory barrier. If this packet has
|
|
|
|
+ * already been locked, it's going to be removed and completed by
|
|
|
|
+ * another party, which should have precedence.
|
|
|
|
+ */
|
|
|
|
+ if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * By marking the packet as locked and employing the implicit memory
|
|
|
|
+ * barrier of test_and_set_bit, we have guaranteed that, at this point,
|
|
|
|
+ * the packet cannot be added to the queue any more.
|
|
|
|
+ *
|
|
|
|
+ * In case the packet has never been submitted, packet->ptl is NULL. If
|
|
|
|
+ * the packet is currently being submitted, packet->ptl may be NULL or
|
|
|
|
+ * non-NULL. Due marking the packet as locked above and committing with
|
|
|
|
+ * the memory barrier, we have guaranteed that, if packet->ptl is NULL,
|
|
|
|
+ * the packet will never be added to the queue. If packet->ptl is
|
|
|
|
+ * non-NULL, we don't have any guarantees.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ if (READ_ONCE(p->ptl)) {
|
|
|
|
+ ssh_ptl_remove_and_complete(p, -ECANCELED);
|
|
|
|
+ ssh_ptl_tx_wakeup(p->ptl, false);
|
|
|
|
+ } else if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
|
|
|
|
+ __ssh_ptl_complete(p, -ECANCELED);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static ktime_t ssh_packet_get_expiration(struct ssh_packet *p, ktime_t timeout)
|
|
|
|
+{
|
|
|
|
+ ktime_t timestamp = READ_ONCE(p->timestamp);
|
|
|
|
+
|
|
|
|
+ if (timestamp != KTIME_MAX)
|
|
|
|
+ return ktime_add(timestamp, timeout);
|
|
|
|
+ else
|
|
|
|
+ return KTIME_MAX;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_timeout_reap(struct work_struct *work)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = to_ssh_ptl(work, rtx_timeout.reaper.work);
|
|
|
|
+ struct ssh_packet *p, *n;
|
|
|
|
+ LIST_HEAD(claimed);
|
|
|
|
+ ktime_t now = ktime_get_coarse_boottime();
|
|
|
|
+ ktime_t timeout = ptl->rtx_timeout.timeout;
|
|
|
|
+ ktime_t next = KTIME_MAX;
|
|
|
|
+ bool resub = false;
|
|
|
|
+
|
|
|
|
+ trace_ssam_ptl_timeout_reap("pending", atomic_read(&ptl->pending.count));
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Mark reaper as "not pending". This is done before checking any
|
|
|
|
+ * packets to avoid lost-update type problems.
|
|
|
|
+ */
|
|
|
|
+ WRITE_ONCE(ptl->rtx_timeout.expires, KTIME_MAX);
|
|
|
|
+ smp_mb__after_atomic();
|
|
|
|
+
|
|
|
|
+ spin_lock(&ptl->pending.lock);
|
|
|
|
+
|
|
|
|
+ list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
|
|
|
|
+ ktime_t expires = ssh_packet_get_expiration(p, timeout);
|
|
|
|
+ u8 try;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Check if the timeout hasn't expired yet. Find out next
|
|
|
|
+ * expiration date to be handled after this run.
|
|
|
|
+ */
|
|
|
|
+ if (ktime_after(expires, now)) {
|
|
|
|
+ next = ktime_before(expires, next) ? expires : next;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // avoid further transitions if locked
|
|
|
|
+ if (test_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ trace_ssam_packet_timeout(p);
|
|
|
|
+
|
|
|
|
+ // check if we still have some tries left
|
|
|
|
+ try = ssh_packet_priority_get_try(READ_ONCE(p->priority));
|
|
|
|
+ if (likely(try < SSH_PTL_MAX_PACKET_TRIES)) {
|
|
|
|
+ resub = true;
|
|
|
|
+ __ssh_ptl_resubmit(p);
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // no more tries left: cancel the packet
|
|
|
|
+
|
|
|
|
+ // if someone else has locked the packet already, don't use it
|
|
|
|
+ if (test_and_set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state))
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * We have now marked the packet as locked. Thus it cannot be
|
|
|
|
+ * added to the pending list again after we've removed it here.
|
|
|
|
+ * We can therefore re-use the pending_node of this packet
|
|
|
|
+ * temporarily.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
|
|
|
|
+
|
|
|
|
+ atomic_dec(&ptl->pending.count);
|
|
|
|
+ list_del(&p->pending_node);
|
|
|
|
+
|
|
|
|
+ list_add_tail(&p->pending_node, &claimed);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ spin_unlock(&ptl->pending.lock);
|
|
|
|
+
|
|
|
|
+ // cancel and complete the packet
|
|
|
|
+ list_for_each_entry_safe(p, n, &claimed, pending_node) {
|
|
|
|
+ if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state)) {
|
|
|
|
+ ssh_ptl_queue_remove(p);
|
|
|
|
+ __ssh_ptl_complete(p, -ETIMEDOUT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // drop the reference we've obtained by removing it from pending
|
|
|
|
+ list_del(&p->pending_node);
|
|
|
|
+ ssh_packet_put(p);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // ensure that reaper doesn't run again immediately
|
|
|
|
+ next = max(next, ktime_add(now, SSH_PTL_PACKET_TIMEOUT_RESOLUTION));
|
|
|
|
+ if (next != KTIME_MAX)
|
|
|
|
+ ssh_ptl_timeout_reaper_mod(ptl, now, next);
|
|
|
|
+
|
|
|
|
+ // force-wakeup to properly handle re-transmits if we've re-submitted
|
|
|
|
+ ssh_ptl_tx_wakeup(ptl, resub);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static bool ssh_ptl_rx_blacklist_check(struct ssh_ptl *ptl, u8 seq)
|
|
|
|
+{
|
|
|
|
+ int i;
|
|
|
|
+
|
|
|
|
+ // check if SEQ is blacklisted
|
|
|
|
+ for (i = 0; i < ARRAY_SIZE(ptl->rx.blacklist.seqs); i++) {
|
|
|
|
+ if (likely(ptl->rx.blacklist.seqs[i] != seq))
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ ptl_dbg(ptl, "ptl: ignoring repeated data packet\n");
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // update blacklist
|
|
|
|
+ ptl->rx.blacklist.seqs[ptl->rx.blacklist.offset] = seq;
|
|
|
|
+ ptl->rx.blacklist.offset = (ptl->rx.blacklist.offset + 1)
|
|
|
|
+ % ARRAY_SIZE(ptl->rx.blacklist.seqs);
|
|
|
|
+
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_rx_dataframe(struct ssh_ptl *ptl,
|
|
|
|
+ const struct ssh_frame *frame,
|
|
|
|
+ const struct sshp_span *payload)
|
|
|
|
+{
|
|
|
|
+ if (ssh_ptl_rx_blacklist_check(ptl, frame->seq))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ ptl->ops.data_received(ptl, payload);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_send_ack(struct ssh_ptl *ptl, u8 seq)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet_args args;
|
|
|
|
+ struct ssh_packet *packet;
|
|
|
|
+ struct msgbuf msgb;
|
|
|
|
+
|
|
|
|
+ args.type = 0;
|
|
|
|
+ args.priority = SSH_PACKET_PRIORITY(ACK, 0);
|
|
|
|
+ args.ops = &ssh_ptl_ctrl_packet_ops;
|
|
|
|
+
|
|
|
|
+ packet = ptl_alloc_ctrl_packet(ptl, &args, GFP_KERNEL);
|
|
|
|
+ if (!packet) {
|
|
|
|
+ ptl_err(ptl, "ptl: failed to allocate ACK packet\n");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ msgb_init(&msgb, packet->data, packet->data_length);
|
|
|
|
+ msgb_push_ack(&msgb, seq);
|
|
|
|
+ packet->data_length = msgb_bytes_used(&msgb);
|
|
|
|
+
|
|
|
|
+ ssh_ptl_submit(ptl, packet);
|
|
|
|
+ ssh_packet_put(packet);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_send_nak(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet_args args;
|
|
|
|
+ struct ssh_packet *packet;
|
|
|
|
+ struct msgbuf msgb;
|
|
|
|
+
|
|
|
|
+ args.type = 0;
|
|
|
|
+ args.priority = SSH_PACKET_PRIORITY(NAK, 0);
|
|
|
|
+ args.ops = &ssh_ptl_ctrl_packet_ops;
|
|
|
|
+
|
|
|
|
+ packet = ptl_alloc_ctrl_packet(ptl, &args, GFP_KERNEL);
|
|
|
|
+ if (!packet) {
|
|
|
|
+ ptl_err(ptl, "ptl: failed to allocate NAK packet\n");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ msgb_init(&msgb, packet->data, packet->data_length);
|
|
|
|
+ msgb_push_nak(&msgb);
|
|
|
|
+ packet->data_length = msgb_bytes_used(&msgb);
|
|
|
|
+
|
|
|
|
+ ssh_ptl_submit(ptl, packet);
|
|
|
|
+ ssh_packet_put(packet);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static size_t ssh_ptl_rx_eval(struct ssh_ptl *ptl, struct sshp_span *source)
|
|
|
|
+{
|
|
|
|
+ struct ssh_frame *frame;
|
|
|
|
+ struct sshp_span payload;
|
|
|
|
+ struct sshp_span aligned;
|
|
|
|
+ bool syn_found;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // error injection: modify data to simulate corrupt SYN bytes
|
|
|
|
+ ssh_ptl_rx_inject_invalid_syn(ptl, source);
|
|
|
|
+
|
|
|
|
+ // find SYN
|
|
|
|
+ syn_found = sshp_find_syn(source, &aligned);
|
|
|
|
+
|
|
|
|
+ if (unlikely(aligned.ptr - source->ptr) > 0) {
|
|
|
|
+ ptl_warn(ptl, "rx: parser: invalid start of frame, skipping\n");
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Notes:
|
|
|
|
+ * - This might send multiple NAKs in case the communication
|
|
|
|
+ * starts with an invalid SYN and is broken down into multiple
|
|
|
|
+ * pieces. This should generally be handled fine, we just
|
|
|
|
+ * might receive duplicate data in this case, which is
|
|
|
|
+ * detected when handling data frames.
|
|
|
|
+ * - This path will also be executed on invalid CRCs: When an
|
|
|
|
+ * invalid CRC is encountered, the code below will skip data
|
|
|
|
+ * until direclty after the SYN. This causes the search for
|
|
|
|
+ * the next SYN, which is generally not placed directly after
|
|
|
|
+ * the last one.
|
|
|
|
+ */
|
|
|
|
+ ssh_ptl_send_nak(ptl);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (unlikely(!syn_found))
|
|
|
|
+ return aligned.ptr - source->ptr;
|
|
|
|
+
|
|
|
|
+ // error injection: modify data to simulate corruption
|
|
|
|
+ ssh_ptl_rx_inject_invalid_data(ptl, &aligned);
|
|
|
|
+
|
|
|
|
+ // parse and validate frame
|
|
|
|
+ status = sshp_parse_frame(&ptl->serdev->dev, &aligned, &frame, &payload,
|
|
|
|
+ SSH_PTL_RX_BUF_LEN);
|
|
|
|
+ if (status) // invalid frame: skip to next syn
|
|
|
|
+ return aligned.ptr - source->ptr + sizeof(u16);
|
|
|
|
+ if (!frame) // not enough data
|
|
|
|
+ return aligned.ptr - source->ptr;
|
|
|
|
+
|
|
|
|
+ trace_ssam_rx_frame_received(frame);
|
|
|
|
+
|
|
|
|
+ switch (frame->type) {
|
|
|
|
+ case SSH_FRAME_TYPE_ACK:
|
|
|
|
+ ssh_ptl_acknowledge(ptl, frame->seq);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case SSH_FRAME_TYPE_NAK:
|
|
|
|
+ ssh_ptl_resubmit_pending(ptl);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ case SSH_FRAME_TYPE_DATA_SEQ:
|
|
|
|
+ ssh_ptl_send_ack(ptl, frame->seq);
|
|
|
|
+ /* fallthrough */
|
|
|
|
+
|
|
|
|
+ case SSH_FRAME_TYPE_DATA_NSQ:
|
|
|
|
+ ssh_ptl_rx_dataframe(ptl, frame, &payload);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ ptl_warn(ptl, "ptl: received frame with unknown type 0x%02x\n",
|
|
|
|
+ frame->type);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return aligned.ptr - source->ptr + SSH_MESSAGE_LENGTH(frame->len);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_rx_threadfn(void *data)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl *ptl = data;
|
|
|
|
+
|
|
|
|
+ while (true) {
|
|
|
|
+ struct sshp_span span;
|
|
|
|
+ size_t offs = 0;
|
|
|
|
+ size_t n;
|
|
|
|
+
|
|
|
|
+ wait_event_interruptible(ptl->rx.wq,
|
|
|
|
+ !kfifo_is_empty(&ptl->rx.fifo)
|
|
|
|
+ || kthread_should_stop());
|
|
|
|
+ if (kthread_should_stop())
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ // copy from fifo to evaluation buffer
|
|
|
|
+ n = sshp_buf_read_from_fifo(&ptl->rx.buf, &ptl->rx.fifo);
|
|
|
|
+
|
|
|
|
+ ptl_dbg(ptl, "rx: received data (size: %zu)\n", n);
|
|
|
|
+ print_hex_dump_debug("rx: ", DUMP_PREFIX_OFFSET, 16, 1,
|
|
|
|
+ ptl->rx.buf.ptr + ptl->rx.buf.len - n,
|
|
|
|
+ n, false);
|
|
|
|
+
|
|
|
|
+ // parse until we need more bytes or buffer is empty
|
|
|
|
+ while (offs < ptl->rx.buf.len) {
|
|
|
|
+ sshp_buf_span_from(&ptl->rx.buf, offs, &span);
|
|
|
|
+ n = ssh_ptl_rx_eval(ptl, &span);
|
|
|
|
+ if (n == 0)
|
|
|
|
+ break; // need more bytes
|
|
|
|
+
|
|
|
|
+ offs += n;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // throw away the evaluated parts
|
|
|
|
+ sshp_buf_drop(&ptl->rx.buf, offs);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_ptl_rx_wakeup(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ wake_up(&ptl->rx.wq);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_rx_start(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ if (ptl->rx.thread)
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ ptl->rx.thread = kthread_run(ssh_ptl_rx_threadfn, ptl, "surface-sh-rx");
|
|
|
|
+ if (IS_ERR(ptl->rx.thread))
|
|
|
|
+ return PTR_ERR(ptl->rx.thread);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_rx_stop(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ int status = 0;
|
|
|
|
+
|
|
|
|
+ if (ptl->rx.thread) {
|
|
|
|
+ status = kthread_stop(ptl->rx.thread);
|
|
|
|
+ ptl->rx.thread = NULL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_rx_rcvbuf(struct ssh_ptl *ptl, const u8 *buf, size_t n)
|
|
|
|
+{
|
|
|
|
+ int used;
|
|
|
|
+
|
|
|
|
+ if (test_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state))
|
|
|
|
+ return used;
|
|
|
|
+
|
|
|
|
+ used = kfifo_in(&ptl->rx.fifo, buf, n);
|
|
|
|
+ if (used)
|
|
|
|
+ ssh_ptl_rx_wakeup(ptl);
|
|
|
|
+
|
|
|
|
+ return used;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssh_flush_packet {
|
|
|
|
+ struct ssh_packet base;
|
|
|
|
+ struct completion completion;
|
|
|
|
+ int status;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_flush_complete(struct ssh_packet *p, int status)
|
|
|
|
+{
|
|
|
|
+ struct ssh_flush_packet *packet;
|
|
|
|
+
|
|
|
|
+ packet = container_of(p, struct ssh_flush_packet, base);
|
|
|
|
+ packet->status = status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_flush_release(struct ssh_packet *p)
|
|
|
|
+{
|
|
|
|
+ struct ssh_flush_packet *packet;
|
|
|
|
+
|
|
|
|
+ packet = container_of(p, struct ssh_flush_packet, base);
|
|
|
|
+ complete_all(&packet->completion);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static const struct ssh_packet_ops ssh_flush_packet_ops = {
|
|
|
|
+ .complete = ssh_ptl_flush_complete,
|
|
|
|
+ .release = ssh_ptl_flush_release,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_ptl_flush - flush the packet transmission layer
|
|
|
|
+ * @ptl: packet transmission layer
|
|
|
|
+ * @timeout: timeout for the flush operation in jiffies
|
|
|
|
+ *
|
|
|
|
+ * Queue a special flush-packet and wait for its completion. This packet will
|
|
|
|
+ * be completed after all other currently queued and pending packets have been
|
|
|
|
+ * completed. Flushing guarantees that all previously submitted data packets
|
|
|
|
+ * have been fully completed before this call returns. Additionally, flushing
|
|
|
|
+ * blocks execution of all later submitted data packets until the flush has been
|
|
|
|
+ * completed.
|
|
|
|
+ *
|
|
|
|
+ * Control (i.e. ACK/NAK) packets that have been submitted after this call will
|
|
|
|
+ * be placed before the flush packet in the queue, as long as the flush-packet
|
|
|
|
+ * has not been chosen for processing yet.
|
|
|
|
+ *
|
|
|
|
+ * Flushing, even when no new data packets are submitted after this call, does
|
|
|
|
+ * not guarantee that no more packets are scheduled. For example, incoming
|
|
|
|
+ * messages can promt automated submission of ACK or NAK type packets. If this
|
|
|
|
+ * happens while the flush-packet is being processed (i.e. after it has been
|
|
|
|
+ * taken from the queue), such packets may still be queued after this function
|
|
|
|
+ * returns.
|
|
|
|
+ *
|
|
|
|
+ * Return: Zero on success, -ETIMEDOUT if the flush timed out and has been
|
|
|
|
+ * canceled as a result of the timeout, or -ESHUTDOWN if the packet transmission
|
|
|
|
+ * layer has been shut down before this call. May also return -EINTR if the
|
|
|
|
+ * packet transmission has been interrupted.
|
|
|
|
+ */
|
|
|
|
+static int ssh_ptl_flush(struct ssh_ptl *ptl, unsigned long timeout)
|
|
|
|
+{
|
|
|
|
+ struct ssh_flush_packet packet;
|
|
|
|
+ struct ssh_packet_args args;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ args.type = SSH_PACKET_TY_FLUSH | SSH_PACKET_TY_BLOCKING;
|
|
|
|
+ args.priority = SSH_PACKET_PRIORITY(FLUSH, 0);
|
|
|
|
+ args.ops = &ssh_flush_packet_ops;
|
|
|
|
+
|
|
|
|
+ ssh_packet_init(&packet.base, &args);
|
|
|
|
+ init_completion(&packet.completion);
|
|
|
|
+
|
|
|
|
+ status = ssh_ptl_submit(ptl, &packet.base);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ ssh_packet_put(&packet.base);
|
|
|
|
+
|
|
|
|
+ if (wait_for_completion_timeout(&packet.completion, timeout))
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ ssh_ptl_cancel(&packet.base);
|
|
|
|
+ wait_for_completion(&packet.completion);
|
|
|
|
+
|
|
|
|
+ WARN_ON(packet.status != 0 && packet.status != -ECANCELED
|
|
|
|
+ && packet.status != -ESHUTDOWN && packet.status != -EINTR);
|
|
|
|
+
|
|
|
|
+ return packet.status == -ECANCELED ? -ETIMEDOUT : status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_ptl_shutdown - shut down the packet transmission layer
|
|
|
|
+ * @ptl: packet transmission layer
|
|
|
|
+ *
|
|
|
|
+ * Shuts down the packet transmission layer, removing and canceling all queued
|
|
|
|
+ * and pending packets. Packets canceled by this operation will be completed
|
|
|
|
+ * with -ESHUTDOWN as status.
|
|
|
|
+ *
|
|
|
|
+ * As a result of this function, the transmission layer will be marked as shut
|
|
|
|
+ * down. Submission of packets after the transmission layer has been shut down
|
|
|
|
+ * will fail with -ESHUTDOWN.
|
|
|
|
+ */
|
|
|
|
+static void ssh_ptl_shutdown(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ LIST_HEAD(complete_q);
|
|
|
|
+ LIST_HEAD(complete_p);
|
|
|
|
+ struct ssh_packet *p, *n;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // ensure that no new packets (including ACK/NAK) can be submitted
|
|
|
|
+ set_bit(SSH_PTL_SF_SHUTDOWN_BIT, &ptl->state);
|
|
|
|
+ smp_mb__after_atomic();
|
|
|
|
+
|
|
|
|
+ status = ssh_ptl_rx_stop(ptl);
|
|
|
|
+ if (status)
|
|
|
|
+ ptl_err(ptl, "ptl: failed to stop receiver thread\n");
|
|
|
|
+
|
|
|
|
+ status = ssh_ptl_tx_stop(ptl);
|
|
|
|
+ if (status)
|
|
|
|
+ ptl_err(ptl, "ptl: failed to stop transmitter thread\n");
|
|
|
|
+
|
|
|
|
+ cancel_delayed_work_sync(&ptl->rtx_timeout.reaper);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * At this point, all threads have been stopped. This means that the
|
|
|
|
+ * only references to packets from inside the system are in the queue
|
|
|
|
+ * and pending set.
|
|
|
|
+ *
|
|
|
|
+ * Note: We still need locks here because someone could still be
|
|
|
|
+ * cancelling packets.
|
|
|
|
+ *
|
|
|
|
+ * Note 2: We can re-use queue_node (or pending_node) if we mark the
|
|
|
|
+ * packet as locked an then remove it from the queue (or pending set
|
|
|
|
+ * respecitvely). Marking the packet as locked avoids re-queueing
|
|
|
|
+ * (which should already be prevented by having stopped the treads...)
|
|
|
|
+ * and not setting QUEUED_BIT (or PENDING_BIT) prevents removal from a
|
|
|
|
+ * new list via other threads (e.g. canellation).
|
|
|
|
+ *
|
|
|
|
+ * Note 3: There may be overlap between complete_p and complete_q.
|
|
|
|
+ * This is handled via test_and_set_bit on the "completed" flag
|
|
|
|
+ * (also handles cancelation).
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ // mark queued packets as locked and move them to complete_q
|
|
|
|
+ spin_lock(&ptl->queue.lock);
|
|
|
|
+ list_for_each_entry_safe(p, n, &ptl->queue.head, queue_node) {
|
|
|
|
+ set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_PACKET_SF_QUEUED_BIT, &p->state);
|
|
|
|
+
|
|
|
|
+ list_del(&p->queue_node);
|
|
|
|
+ list_add_tail(&p->queue_node, &complete_q);
|
|
|
|
+ }
|
|
|
|
+ spin_unlock(&ptl->queue.lock);
|
|
|
|
+
|
|
|
|
+ // mark pending packets as locked and move them to complete_p
|
|
|
|
+ spin_lock(&ptl->pending.lock);
|
|
|
|
+ list_for_each_entry_safe(p, n, &ptl->pending.head, pending_node) {
|
|
|
|
+ set_bit(SSH_PACKET_SF_LOCKED_BIT, &p->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_PACKET_SF_PENDING_BIT, &p->state);
|
|
|
|
+
|
|
|
|
+ list_del(&p->pending_node);
|
|
|
|
+ list_add_tail(&p->pending_node, &complete_q);
|
|
|
|
+ }
|
|
|
|
+ atomic_set(&ptl->pending.count, 0);
|
|
|
|
+ spin_unlock(&ptl->pending.lock);
|
|
|
|
+
|
|
|
|
+ // complete and drop packets on complete_q
|
|
|
|
+ list_for_each_entry(p, &complete_q, queue_node) {
|
|
|
|
+ if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
|
|
|
|
+ __ssh_ptl_complete(p, -ESHUTDOWN);
|
|
|
|
+
|
|
|
|
+ ssh_packet_put(p);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // complete and drop packets on complete_p
|
|
|
|
+ list_for_each_entry(p, &complete_p, pending_node) {
|
|
|
|
+ if (!test_and_set_bit(SSH_PACKET_SF_COMPLETED_BIT, &p->state))
|
|
|
|
+ __ssh_ptl_complete(p, -ESHUTDOWN);
|
|
|
|
+
|
|
|
|
+ ssh_packet_put(p);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * At this point we have guaranteed that the system doesn't reference
|
|
|
|
+ * any packets any more.
|
|
|
|
+ */
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline struct device *ssh_ptl_get_device(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ return &ptl->serdev->dev;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_ptl_init(struct ssh_ptl *ptl, struct serdev_device *serdev,
|
|
|
|
+ struct ssh_ptl_ops *ops)
|
|
|
|
+{
|
|
|
|
+ int i, status;
|
|
|
|
+
|
|
|
|
+ ptl->serdev = serdev;
|
|
|
|
+ ptl->state = 0;
|
|
|
|
+
|
|
|
|
+ spin_lock_init(&ptl->queue.lock);
|
|
|
|
+ INIT_LIST_HEAD(&ptl->queue.head);
|
|
|
|
+
|
|
|
|
+ spin_lock_init(&ptl->pending.lock);
|
|
|
|
+ INIT_LIST_HEAD(&ptl->pending.head);
|
|
|
|
+ atomic_set_release(&ptl->pending.count, 0);
|
|
|
|
+
|
|
|
|
+ ptl->tx.thread = NULL;
|
|
|
|
+ ptl->tx.thread_signal = false;
|
|
|
|
+ ptl->tx.packet = NULL;
|
|
|
|
+ ptl->tx.offset = 0;
|
|
|
|
+ init_waitqueue_head(&ptl->tx.thread_wq);
|
|
|
|
+ init_waitqueue_head(&ptl->tx.packet_wq);
|
|
|
|
+
|
|
|
|
+ ptl->rx.thread = NULL;
|
|
|
|
+ init_waitqueue_head(&ptl->rx.wq);
|
|
|
|
+
|
|
|
|
+ ptl->rtx_timeout.timeout = SSH_PTL_PACKET_TIMEOUT;
|
|
|
|
+ ptl->rtx_timeout.expires = KTIME_MAX;
|
|
|
|
+ INIT_DELAYED_WORK(&ptl->rtx_timeout.reaper, ssh_ptl_timeout_reap);
|
|
|
|
+
|
|
|
|
+ ptl->ops = *ops;
|
|
|
|
+
|
|
|
|
+ // initialize SEQ blacklist with invalid sequence IDs
|
|
|
|
+ for (i = 0; i < ARRAY_SIZE(ptl->rx.blacklist.seqs); i++)
|
|
|
|
+ ptl->rx.blacklist.seqs[i] = 0xFFFF;
|
|
|
|
+ ptl->rx.blacklist.offset = 0;
|
|
|
|
+
|
|
|
|
+ status = kfifo_alloc(&ptl->rx.fifo, SSH_PTL_RX_FIFO_LEN, GFP_KERNEL);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ status = sshp_buf_alloc(&ptl->rx.buf, SSH_PTL_RX_BUF_LEN, GFP_KERNEL);
|
|
|
|
+ if (status)
|
|
|
|
+ kfifo_free(&ptl->rx.fifo);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_ptl_destroy(struct ssh_ptl *ptl)
|
|
|
|
+{
|
|
|
|
+ kfifo_free(&ptl->rx.fifo);
|
|
|
|
+ sshp_buf_free(&ptl->rx.buf);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Request transport layer (rtl). ---------------------------------------- */
|
|
|
|
+
|
|
|
|
+#define SSH_RTL_REQUEST_TIMEOUT ms_to_ktime(1000)
|
|
|
|
+#define SSH_RTL_REQUEST_TIMEOUT_RESOLUTION ms_to_ktime(max(2000 / HZ, 50))
|
|
|
|
+
|
|
|
|
+#define SSH_RTL_MAX_PENDING 3
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+enum ssh_rtl_state_flags {
|
|
|
|
+ SSH_RTL_SF_SHUTDOWN_BIT,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssh_rtl_ops {
|
|
|
|
+ void (*handle_event)(struct ssh_rtl *rtl, const struct ssh_command *cmd,
|
|
|
|
+ const struct sshp_span *data);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssh_rtl {
|
|
|
|
+ struct ssh_ptl ptl;
|
|
|
|
+ unsigned long state;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ spinlock_t lock;
|
|
|
|
+ struct list_head head;
|
|
|
|
+ } queue;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ spinlock_t lock;
|
|
|
|
+ struct list_head head;
|
|
|
|
+ atomic_t count;
|
|
|
|
+ } pending;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ struct work_struct work;
|
|
|
|
+ } tx;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ ktime_t timeout;
|
|
|
|
+ ktime_t expires;
|
|
|
|
+ struct delayed_work reaper;
|
|
|
|
+ } rtx_timeout;
|
|
|
|
+
|
|
|
|
+ struct ssh_rtl_ops ops;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define rtl_dbg(r, fmt, ...) ptl_dbg(&(r)->ptl, fmt, ##__VA_ARGS__)
|
|
|
|
+#define rtl_info(p, fmt, ...) ptl_info(&(p)->ptl, fmt, ##__VA_ARGS__)
|
|
|
|
+#define rtl_warn(r, fmt, ...) ptl_warn(&(r)->ptl, fmt, ##__VA_ARGS__)
|
|
|
|
+#define rtl_err(r, fmt, ...) ptl_err(&(r)->ptl, fmt, ##__VA_ARGS__)
|
|
|
|
+#define rtl_dbg_cond(r, fmt, ...) __ssam_prcond(rtl_dbg, r, fmt, ##__VA_ARGS__)
|
|
|
|
+
|
|
|
|
+#define to_ssh_rtl(ptr, member) \
|
|
|
|
+ container_of(ptr, struct ssh_rtl, member)
|
|
|
|
+
|
|
|
|
+#define to_ssh_request(ptr, member) \
|
|
|
|
+ container_of(ptr, struct ssh_request, member)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_rtl_should_drop_response - error injection hook to drop request responses
|
|
|
|
+ *
|
|
|
|
+ * Useful to cause request transmission timeouts in the driver by dropping the
|
|
|
|
+ * response to a request.
|
|
|
|
+ */
|
|
|
|
+static noinline_if_inject bool ssh_rtl_should_drop_response(void)
|
|
|
|
+{
|
|
|
|
+ return false;
|
|
|
|
+}
|
|
|
|
+ALLOW_ERROR_INJECTION(ssh_rtl_should_drop_response, TRUE);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static inline void ssh_request_get(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ ssh_packet_get(&rqst->packet);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline void ssh_request_put(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ ssh_packet_put(&rqst->packet);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static inline u16 ssh_request_get_rqid(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ return get_unaligned_le16(rqst->packet.data
|
|
|
|
+ + SSH_MSGOFFSET_COMMAND(rqid));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline u32 ssh_request_get_rqid_safe(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ if (!rqst->packet.data)
|
|
|
|
+ return -1;
|
|
|
|
+
|
|
|
|
+ return ssh_request_get_rqid(rqst);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_queue_remove(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ bool remove;
|
|
|
|
+
|
|
|
|
+ spin_lock(&rqst->rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ remove = test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state);
|
|
|
|
+ if (remove)
|
|
|
|
+ list_del(&rqst->node);
|
|
|
|
+
|
|
|
|
+ spin_unlock(&rqst->rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ if (remove)
|
|
|
|
+ ssh_request_put(rqst);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_pending_remove(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ bool remove;
|
|
|
|
+
|
|
|
|
+ spin_lock(&rqst->rtl->pending.lock);
|
|
|
|
+
|
|
|
|
+ remove = test_and_clear_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state);
|
|
|
|
+ if (remove) {
|
|
|
|
+ atomic_dec(&rqst->rtl->pending.count);
|
|
|
|
+ list_del(&rqst->node);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ spin_unlock(&rqst->rtl->pending.lock);
|
|
|
|
+
|
|
|
|
+ if (remove)
|
|
|
|
+ ssh_request_put(rqst);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_complete_with_status(struct ssh_request *rqst, int status)
|
|
|
|
+{
|
|
|
|
+ struct ssh_rtl *rtl = READ_ONCE(rqst->rtl);
|
|
|
|
+
|
|
|
|
+ trace_ssam_request_complete(rqst, status);
|
|
|
|
+
|
|
|
|
+ // rqst->rtl may not be set if we're cancelling before submitting
|
|
|
|
+ rtl_dbg_cond(rtl, "rtl: completing request (rqid: 0x%04x,"
|
|
|
|
+ " status: %d)\n", ssh_request_get_rqid_safe(rqst), status);
|
|
|
|
+
|
|
|
|
+ rqst->ops->complete(rqst, NULL, NULL, status);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_complete_with_rsp(struct ssh_request *rqst,
|
|
|
|
+ const struct ssh_command *cmd,
|
|
|
|
+ const struct sshp_span *data)
|
|
|
|
+{
|
|
|
|
+ trace_ssam_request_complete(rqst, 0);
|
|
|
|
+
|
|
|
|
+ rtl_dbg(rqst->rtl, "rtl: completing request with response"
|
|
|
|
+ " (rqid: 0x%04x)\n", ssh_request_get_rqid(rqst));
|
|
|
|
+
|
|
|
|
+ rqst->ops->complete(rqst, cmd, data, 0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static bool ssh_rtl_tx_can_process(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ if (test_bit(SSH_REQUEST_TY_FLUSH_BIT, &rqst->state))
|
|
|
|
+ return !atomic_read(&rqst->rtl->pending.count);
|
|
|
|
+
|
|
|
|
+ return atomic_read(&rqst->rtl->pending.count) < SSH_RTL_MAX_PENDING;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct ssh_request *ssh_rtl_tx_next(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ struct ssh_request *rqst = ERR_PTR(-ENOENT);
|
|
|
|
+ struct ssh_request *p, *n;
|
|
|
|
+
|
|
|
|
+ spin_lock(&rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ // find first non-locked request and remove it
|
|
|
|
+ list_for_each_entry_safe(p, n, &rtl->queue.head, node) {
|
|
|
|
+ if (unlikely(test_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state)))
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ if (!ssh_rtl_tx_can_process(p)) {
|
|
|
|
+ rqst = ERR_PTR(-EBUSY);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Remove from queue and mark as transmitting. Ensure that the
|
|
|
|
+ * state does not get zero via memory barrier.
|
|
|
|
+ */
|
|
|
|
+ set_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &p->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &p->state);
|
|
|
|
+
|
|
|
|
+ list_del(&p->node);
|
|
|
|
+
|
|
|
|
+ rqst = p;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ spin_unlock(&rtl->queue.lock);
|
|
|
|
+ return rqst;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_rtl_tx_pending_push(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ struct ssh_rtl *rtl = rqst->rtl;
|
|
|
|
+
|
|
|
|
+ spin_lock(&rtl->pending.lock);
|
|
|
|
+
|
|
|
|
+ if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
|
|
|
|
+ spin_unlock(&rtl->pending.lock);
|
|
|
|
+ return -EINVAL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
|
|
|
|
+ spin_unlock(&rtl->pending.lock);
|
|
|
|
+ return -EALREADY;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ atomic_inc(&rtl->pending.count);
|
|
|
|
+ ssh_request_get(rqst);
|
|
|
|
+ list_add_tail(&rqst->node, &rtl->pending.head);
|
|
|
|
+
|
|
|
|
+ spin_unlock(&rtl->pending.lock);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_rtl_tx_try_process_one(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ struct ssh_request *rqst;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // get and prepare next request for transmit
|
|
|
|
+ rqst = ssh_rtl_tx_next(rtl);
|
|
|
|
+ if (IS_ERR(rqst))
|
|
|
|
+ return PTR_ERR(rqst);
|
|
|
|
+
|
|
|
|
+ // add to/mark as pending
|
|
|
|
+ status = ssh_rtl_tx_pending_push(rqst);
|
|
|
|
+ if (status) {
|
|
|
|
+ ssh_request_put(rqst);
|
|
|
|
+ return -EAGAIN;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // submit packet
|
|
|
|
+ status = ssh_ptl_submit(&rtl->ptl, &rqst->packet);
|
|
|
|
+ if (status == -ESHUTDOWN) {
|
|
|
|
+ /*
|
|
|
|
+ * Packet has been refused due to the packet layer shutting
|
|
|
|
+ * down. Complete it here.
|
|
|
|
+ */
|
|
|
|
+ set_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state);
|
|
|
|
+ smp_mb__after_atomic();
|
|
|
|
+
|
|
|
|
+ ssh_rtl_pending_remove(rqst);
|
|
|
|
+ ssh_rtl_complete_with_status(rqst, -ESHUTDOWN);
|
|
|
|
+
|
|
|
|
+ ssh_request_put(rqst);
|
|
|
|
+ return -ESHUTDOWN;
|
|
|
|
+
|
|
|
|
+ } else if (status) {
|
|
|
|
+ /*
|
|
|
|
+ * If submitting the packet failed and the packet layer isn't
|
|
|
|
+ * shutting down, the packet has either been submmitted/queued
|
|
|
|
+ * before (-EALREADY, which cannot happen as we have guaranteed
|
|
|
|
+ * that requests cannot be re-submitted), or the packet was
|
|
|
|
+ * marked as locked (-EINVAL). To mark the packet locked at this
|
|
|
|
+ * stage, the request, and thus the packets itself, had to have
|
|
|
|
+ * been canceled. Simply drop the reference. Cancellation itself
|
|
|
|
+ * will remove it from the set of pending requests.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ WARN_ON(status != -EINVAL);
|
|
|
|
+
|
|
|
|
+ ssh_request_put(rqst);
|
|
|
|
+ return -EAGAIN;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ssh_request_put(rqst);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static bool ssh_rtl_queue_empty(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ bool empty;
|
|
|
|
+
|
|
|
|
+ spin_lock(&rtl->queue.lock);
|
|
|
|
+ empty = list_empty(&rtl->queue.head);
|
|
|
|
+ spin_unlock(&rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ return empty;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static bool ssh_rtl_tx_schedule(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ if (atomic_read(&rtl->pending.count) >= SSH_RTL_MAX_PENDING)
|
|
|
|
+ return false;
|
|
|
|
+
|
|
|
|
+ if (ssh_rtl_queue_empty(rtl))
|
|
|
|
+ return false;
|
|
|
|
+
|
|
|
|
+ return schedule_work(&rtl->tx.work);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_tx_work_fn(struct work_struct *work)
|
|
|
|
+{
|
|
|
|
+ struct ssh_rtl *rtl = to_ssh_rtl(work, tx.work);
|
|
|
|
+ int i, status;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Try to be nice and not block the workqueue: Run a maximum of 10
|
|
|
|
+ * tries, then re-submit if necessary. This should not be neccesary,
|
|
|
|
+ * for normal execution, but guarantee it anyway.
|
|
|
|
+ */
|
|
|
|
+ for (i = 0; i < 10; i++) {
|
|
|
|
+ status = ssh_rtl_tx_try_process_one(rtl);
|
|
|
|
+ if (status == -ENOENT || status == -EBUSY)
|
|
|
|
+ return; // no more requests to process
|
|
|
|
+
|
|
|
|
+ if (status == -ESHUTDOWN) {
|
|
|
|
+ /*
|
|
|
|
+ * Packet system shutting down. No new packets can be
|
|
|
|
+ * transmitted. Return silently, the party initiating
|
|
|
|
+ * the shutdown should handle the rest.
|
|
|
|
+ */
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ WARN_ON(status != 0 && status != -EAGAIN);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // out of tries, reschedule
|
|
|
|
+ ssh_rtl_tx_schedule(rtl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ trace_ssam_request_submit(rqst);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Ensure that requests expecting a response are sequenced. If this
|
|
|
|
+ * invariant ever changes, see the comment in ssh_rtl_complete on what
|
|
|
|
+ * is required to be changed in the code.
|
|
|
|
+ */
|
|
|
|
+ if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &rqst->state))
|
|
|
|
+ if (!(rqst->packet.type & SSH_PACKET_TY_SEQUENCED))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ // try to set rtl and check if this request has already been submitted
|
|
|
|
+ if (cmpxchg(&rqst->rtl, NULL, rtl) != NULL)
|
|
|
|
+ return -EALREADY;
|
|
|
|
+
|
|
|
|
+ spin_lock(&rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ if (test_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state)) {
|
|
|
|
+ spin_unlock(&rtl->queue.lock);
|
|
|
|
+ return -ESHUTDOWN;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
|
|
|
|
+ spin_unlock(&rtl->queue.lock);
|
|
|
|
+ return -EINVAL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ssh_request_get(rqst);
|
|
|
|
+ set_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state);
|
|
|
|
+ list_add_tail(&rqst->node, &rtl->queue.head);
|
|
|
|
+
|
|
|
|
+ spin_unlock(&rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ ssh_rtl_tx_schedule(rtl);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_timeout_reaper_mod(struct ssh_rtl *rtl, ktime_t now,
|
|
|
|
+ ktime_t expires)
|
|
|
|
+{
|
|
|
|
+ unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
|
|
|
|
+ ktime_t aexp = ktime_add(expires, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION);
|
|
|
|
+ ktime_t old;
|
|
|
|
+
|
|
|
|
+ // re-adjust / schedule reaper if it is above resolution delta
|
|
|
|
+ old = READ_ONCE(rtl->rtx_timeout.expires);
|
|
|
|
+ while (ktime_before(aexp, old))
|
|
|
|
+ old = cmpxchg64(&rtl->rtx_timeout.expires, old, expires);
|
|
|
|
+
|
|
|
|
+ // if we updated the reaper expiration, modify work timeout
|
|
|
|
+ if (old == expires)
|
|
|
|
+ mod_delayed_work(system_wq, &rtl->rtx_timeout.reaper, delta);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_timeout_start(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ struct ssh_rtl *rtl = rqst->rtl;
|
|
|
|
+ ktime_t timestamp = ktime_get_coarse_boottime();
|
|
|
|
+ ktime_t timeout = rtl->rtx_timeout.timeout;
|
|
|
|
+
|
|
|
|
+ if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ WRITE_ONCE(rqst->timestamp, timestamp);
|
|
|
|
+ smp_mb__after_atomic();
|
|
|
|
+
|
|
|
|
+ ssh_rtl_timeout_reaper_mod(rqst->rtl, timestamp, timestamp + timeout);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_complete(struct ssh_rtl *rtl,
|
|
|
|
+ const struct ssh_command *command,
|
|
|
|
+ const struct sshp_span *command_data)
|
|
|
|
+{
|
|
|
|
+ struct ssh_request *r = NULL;
|
|
|
|
+ struct ssh_request *p, *n;
|
|
|
|
+ u16 rqid = get_unaligned_le16(&command->rqid);
|
|
|
|
+
|
|
|
|
+ trace_ssam_rx_response_received(command, command_data->len);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Get request from pending based on request ID and mark it as response
|
|
|
|
+ * received and locked.
|
|
|
|
+ */
|
|
|
|
+ spin_lock(&rtl->pending.lock);
|
|
|
|
+ list_for_each_entry_safe(p, n, &rtl->pending.head, node) {
|
|
|
|
+ // we generally expect requests to be processed in order
|
|
|
|
+ if (unlikely(ssh_request_get_rqid(p) != rqid))
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ // simulate response timeout
|
|
|
|
+ if (ssh_rtl_should_drop_response()) {
|
|
|
|
+ spin_unlock(&rtl->pending.lock);
|
|
|
|
+
|
|
|
|
+ trace_ssam_ei_rx_drop_response(p);
|
|
|
|
+ rtl_info(rtl, "request error injection: "
|
|
|
|
+ "dropping response for request %p\n",
|
|
|
|
+ &p->packet);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Mark as "response received" and "locked" as we're going to
|
|
|
|
+ * complete it. Ensure that the state doesn't get zero by
|
|
|
|
+ * employing a memory barrier.
|
|
|
|
+ */
|
|
|
|
+ set_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state);
|
|
|
|
+ set_bit(SSH_REQUEST_SF_RSPRCVD_BIT, &p->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_REQUEST_SF_PENDING_BIT, &p->state);
|
|
|
|
+
|
|
|
|
+ atomic_dec(&rtl->pending.count);
|
|
|
|
+ list_del(&p->node);
|
|
|
|
+
|
|
|
|
+ r = p;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ spin_unlock(&rtl->pending.lock);
|
|
|
|
+
|
|
|
|
+ if (!r) {
|
|
|
|
+ rtl_warn(rtl, "rtl: dropping unexpected command message"
|
|
|
|
+ " (rqid = 0x%04x)\n", rqid);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // if the request hasn't been completed yet, we will do this now
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state)) {
|
|
|
|
+ ssh_request_put(r);
|
|
|
|
+ ssh_rtl_tx_schedule(rtl);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Make sure the request has been transmitted. In case of a sequenced
|
|
|
|
+ * request, we are guaranteed that the completion callback will run on
|
|
|
|
+ * the receiver thread directly when the ACK for the packet has been
|
|
|
|
+ * received. Similarly, this function is guaranteed to run on the
|
|
|
|
+ * receiver thread. Thus we are guaranteed that if the packet has been
|
|
|
|
+ * successfully transmitted and received an ACK, the transmitted flag
|
|
|
|
+ * has been set and is visible here.
|
|
|
|
+ *
|
|
|
|
+ * We are currently not handling unsequenced packets here, as those
|
|
|
|
+ * should never expect a response as ensured in ssh_rtl_submit. If this
|
|
|
|
+ * ever changes, one would have to test for
|
|
|
|
+ *
|
|
|
|
+ * (r->state & (transmitting | transmitted))
|
|
|
|
+ *
|
|
|
|
+ * on unsequenced packets to determine if they could have been
|
|
|
|
+ * transmitted. There are no synchronization guarantees as in the
|
|
|
|
+ * sequenced case, since, in this case, the callback function will not
|
|
|
|
+ * run on the same thread. Thus an exact determination is impossible.
|
|
|
|
+ */
|
|
|
|
+ if (!test_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state)) {
|
|
|
|
+ rtl_err(rtl, "rtl: received response before ACK for request"
|
|
|
|
+ " (rqid = 0x%04x)\n", rqid);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * NB: Timeout has already been canceled, request already been
|
|
|
|
+ * removed from pending and marked as locked and completed. As
|
|
|
|
+ * we receive a "false" response, the packet might still be
|
|
|
|
+ * queued though.
|
|
|
|
+ */
|
|
|
|
+ ssh_rtl_queue_remove(r);
|
|
|
|
+
|
|
|
|
+ ssh_rtl_complete_with_status(r, -EREMOTEIO);
|
|
|
|
+ ssh_request_put(r);
|
|
|
|
+
|
|
|
|
+ ssh_rtl_tx_schedule(rtl);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * NB: Timeout has already been canceled, request already been
|
|
|
|
+ * removed from pending and marked as locked and completed. The request
|
|
|
|
+ * can also not be queued any more, as it has been marked as
|
|
|
|
+ * transmitting and later transmitted. Thus no need to remove it from
|
|
|
|
+ * anywhere.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ ssh_rtl_complete_with_rsp(r, command, command_data);
|
|
|
|
+ ssh_request_put(r);
|
|
|
|
+
|
|
|
|
+ ssh_rtl_tx_schedule(rtl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static bool ssh_rtl_cancel_nonpending(struct ssh_request *r)
|
|
|
|
+{
|
|
|
|
+ unsigned long state, fixed;
|
|
|
|
+ bool remove;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Handle unsubmitted request: Try to mark the packet as locked,
|
|
|
|
+ * expecting the state to be zero (i.e. unsubmitted). Note that, if
|
|
|
|
+ * setting the state worked, we might still be adding the packet to the
|
|
|
|
+ * queue in a currently executing submit call. In that case, however,
|
|
|
|
+ * rqst->rtl must have been set previously, as locked is checked after
|
|
|
|
+ * setting rqst->rtl. Thus only if we successfully lock this request and
|
|
|
|
+ * rqst->rtl is NULL, we have successfully removed the request.
|
|
|
|
+ * Otherwise we need to try and grab it from the queue.
|
|
|
|
+ *
|
|
|
|
+ * Note that if the CMPXCHG fails, we are guaranteed that rqst->rtl has
|
|
|
|
+ * been set and is non-NULL, as states can only be nonzero after this
|
|
|
|
+ * has been set. Also note that we need to fetch the static (type) flags
|
|
|
|
+ * to ensure that they don't cause the cmpxchg to fail.
|
|
|
|
+ */
|
|
|
|
+ fixed = READ_ONCE(r->state) & SSH_REQUEST_FLAGS_TY_MASK;
|
|
|
|
+ state = cmpxchg(&r->state, fixed, SSH_REQUEST_SF_LOCKED_BIT);
|
|
|
|
+ if (!state && !READ_ONCE(r->rtl)) {
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ ssh_rtl_complete_with_status(r, -ECANCELED);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ spin_lock(&r->rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Note: 1) Requests cannot be re-submitted. 2) If a request is queued,
|
|
|
|
+ * it cannot be "transmitting"/"pending" yet. Thus, if we successfully
|
|
|
|
+ * remove the the request here, we have removed all its occurences in
|
|
|
|
+ * the system.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ remove = test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
|
|
|
|
+ if (!remove) {
|
|
|
|
+ spin_unlock(&r->rtl->queue.lock);
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
|
|
|
|
+ list_del(&r->node);
|
|
|
|
+
|
|
|
|
+ spin_unlock(&r->rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ ssh_request_put(r); // drop reference obtained from queue
|
|
|
|
+
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ ssh_rtl_complete_with_status(r, -ECANCELED);
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static bool ssh_rtl_cancel_pending(struct ssh_request *r)
|
|
|
|
+{
|
|
|
|
+ // if the packet is already locked, it's going to be removed shortly
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Now that we have locked the packet, we have guaranteed that it can't
|
|
|
|
+ * be added to the system any more. If rqst->rtl is zero, the locked
|
|
|
|
+ * check in ssh_rtl_submit has not been run and any submission,
|
|
|
|
+ * currently in progress or called later, won't add the packet. Thus we
|
|
|
|
+ * can directly complete it.
|
|
|
|
+ */
|
|
|
|
+ if (!READ_ONCE(r->rtl)) {
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ ssh_rtl_complete_with_status(r, -ECANCELED);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Try to cancel the packet. If the packet has not been completed yet,
|
|
|
|
+ * this will subsequently (and synchronously) call the completion
|
|
|
|
+ * callback of the packet, which will complete the request.
|
|
|
|
+ */
|
|
|
|
+ ssh_ptl_cancel(&r->packet);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * If the packet has been completed with success, i.e. has not been
|
|
|
|
+ * canceled by the above call, the request may not have been completed
|
|
|
|
+ * yet (may be waiting for a response). Check if we need to do this
|
|
|
|
+ * here.
|
|
|
|
+ */
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ ssh_rtl_queue_remove(r);
|
|
|
|
+ ssh_rtl_pending_remove(r);
|
|
|
|
+ ssh_rtl_complete_with_status(r, -ECANCELED);
|
|
|
|
+
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending)
|
|
|
|
+{
|
|
|
|
+ struct ssh_rtl *rtl;
|
|
|
|
+ bool canceled;
|
|
|
|
+
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_CANCELED_BIT, &rqst->state))
|
|
|
|
+ return true;
|
|
|
|
+
|
|
|
|
+ trace_ssam_request_cancel(rqst);
|
|
|
|
+
|
|
|
|
+ if (pending)
|
|
|
|
+ canceled = ssh_rtl_cancel_pending(rqst);
|
|
|
|
+ else
|
|
|
|
+ canceled = ssh_rtl_cancel_nonpending(rqst);
|
|
|
|
+
|
|
|
|
+ // note: rqst->rtl may be NULL if request has not been submitted yet
|
|
|
|
+ rtl = READ_ONCE(rqst->rtl);
|
|
|
|
+ if (canceled && rtl)
|
|
|
|
+ ssh_rtl_tx_schedule(rtl);
|
|
|
|
+
|
|
|
|
+ return canceled;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_packet_callback(struct ssh_packet *p, int status)
|
|
|
|
+{
|
|
|
|
+ struct ssh_request *r = to_ssh_request(p, packet);
|
|
|
|
+
|
|
|
|
+ if (unlikely(status)) {
|
|
|
|
+ set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
|
|
|
|
+
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * The packet may get cancelled even though it has not been
|
|
|
|
+ * submitted yet. The request may still be queued. Check the
|
|
|
|
+ * queue and remove it if necessary. As the timeout would have
|
|
|
|
+ * been started in this function on success, there's no need to
|
|
|
|
+ * cancel it here.
|
|
|
|
+ */
|
|
|
|
+ ssh_rtl_queue_remove(r);
|
|
|
|
+ ssh_rtl_pending_remove(r);
|
|
|
|
+ ssh_rtl_complete_with_status(r, status);
|
|
|
|
+
|
|
|
|
+ ssh_rtl_tx_schedule(r->rtl);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Mark as transmitted, ensure that state doesn't get zero by inserting
|
|
|
|
+ * a memory barrier.
|
|
|
|
+ */
|
|
|
|
+ set_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &r->state);
|
|
|
|
+
|
|
|
|
+ // if we expect a response, we just need to start the timeout
|
|
|
|
+ if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &r->state)) {
|
|
|
|
+ ssh_rtl_timeout_start(r);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * If we don't expect a response, lock, remove, and complete the
|
|
|
|
+ * request. Note that, at this point, the request is guaranteed to have
|
|
|
|
+ * left the queue and no timeout has been started. Thus we only need to
|
|
|
|
+ * remove it from pending. If the request has already been completed (it
|
|
|
|
+ * may have been canceled) return.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ ssh_rtl_pending_remove(r);
|
|
|
|
+ ssh_rtl_complete_with_status(r, 0);
|
|
|
|
+
|
|
|
|
+ ssh_rtl_tx_schedule(r->rtl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static ktime_t ssh_request_get_expiration(struct ssh_request *r, ktime_t timeo)
|
|
|
|
+{
|
|
|
|
+ ktime_t timestamp = READ_ONCE(r->timestamp);
|
|
|
|
+
|
|
|
|
+ if (timestamp != KTIME_MAX)
|
|
|
|
+ return ktime_add(timestamp, timeo);
|
|
|
|
+ else
|
|
|
|
+ return KTIME_MAX;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_timeout_reap(struct work_struct *work)
|
|
|
|
+{
|
|
|
|
+ struct ssh_rtl *rtl = to_ssh_rtl(work, rtx_timeout.reaper.work);
|
|
|
|
+ struct ssh_request *r, *n;
|
|
|
|
+ LIST_HEAD(claimed);
|
|
|
|
+ ktime_t now = ktime_get_coarse_boottime();
|
|
|
|
+ ktime_t timeout = rtl->rtx_timeout.timeout;
|
|
|
|
+ ktime_t next = KTIME_MAX;
|
|
|
|
+
|
|
|
|
+ trace_ssam_rtl_timeout_reap("pending", atomic_read(&rtl->pending.count));
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Mark reaper as "not pending". This is done before checking any
|
|
|
|
+ * requests to avoid lost-update type problems.
|
|
|
|
+ */
|
|
|
|
+ WRITE_ONCE(rtl->rtx_timeout.expires, KTIME_MAX);
|
|
|
|
+ smp_mb__after_atomic();
|
|
|
|
+
|
|
|
|
+ spin_lock(&rtl->pending.lock);
|
|
|
|
+ list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
|
|
|
|
+ ktime_t expires = ssh_request_get_expiration(r, timeout);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Check if the timeout hasn't expired yet. Find out next
|
|
|
|
+ * expiration date to be handled after this run.
|
|
|
|
+ */
|
|
|
|
+ if (ktime_after(expires, now)) {
|
|
|
|
+ next = ktime_before(expires, next) ? expires : next;
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // avoid further transitions if locked
|
|
|
|
+ if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
|
|
|
|
+ continue;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * We have now marked the packet as locked. Thus it cannot be
|
|
|
|
+ * added to the pending or queued lists again after we've
|
|
|
|
+ * removed it here. We can therefore re-use the node of this
|
|
|
|
+ * packet temporarily.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
|
|
|
|
+
|
|
|
|
+ atomic_dec(&rtl->pending.count);
|
|
|
|
+ list_del(&r->node);
|
|
|
|
+
|
|
|
|
+ list_add_tail(&r->node, &claimed);
|
|
|
|
+ }
|
|
|
|
+ spin_unlock(&rtl->pending.lock);
|
|
|
|
+
|
|
|
|
+ // cancel and complete the request
|
|
|
|
+ list_for_each_entry_safe(r, n, &claimed, node) {
|
|
|
|
+ trace_ssam_request_timeout(r);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * At this point we've removed the packet from pending. This
|
|
|
|
+ * means that we've obtained the last (only) reference of the
|
|
|
|
+ * system to it. Thus we can just complete it.
|
|
|
|
+ */
|
|
|
|
+ if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
|
|
|
|
+ ssh_rtl_complete_with_status(r, -ETIMEDOUT);
|
|
|
|
+
|
|
|
|
+ // drop the reference we've obtained by removing it from pending
|
|
|
|
+ list_del(&r->node);
|
|
|
|
+ ssh_request_put(r);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // ensure that reaper doesn't run again immediately
|
|
|
|
+ next = max(next, ktime_add(now, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION));
|
|
|
|
+ if (next != KTIME_MAX)
|
|
|
|
+ ssh_rtl_timeout_reaper_mod(rtl, now, next);
|
|
|
|
+
|
|
|
|
+ ssh_rtl_tx_schedule(rtl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_rx_event(struct ssh_rtl *rtl, const struct ssh_command *cmd,
|
|
|
|
+ const struct sshp_span *data)
|
|
|
|
+{
|
|
|
|
+ trace_ssam_rx_event_received(cmd, data->len);
|
|
|
|
+
|
|
|
|
+ rtl_dbg(rtl, "rtl: handling event (rqid: 0x%04x)\n",
|
|
|
|
+ get_unaligned_le16(&cmd->rqid));
|
|
|
|
+
|
|
|
|
+ rtl->ops.handle_event(rtl, cmd, data);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_rx_command(struct ssh_ptl *p, const struct sshp_span *data)
|
|
|
|
+{
|
|
|
|
+ struct ssh_rtl *rtl = to_ssh_rtl(p, ptl);
|
|
|
|
+ struct device *dev = &p->serdev->dev;
|
|
|
|
+ struct ssh_command *command;
|
|
|
|
+ struct sshp_span command_data;
|
|
|
|
+
|
|
|
|
+ if (sshp_parse_command(dev, data, &command, &command_data))
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (ssh_rqid_is_event(get_unaligned_le16(&command->rqid)))
|
|
|
|
+ ssh_rtl_rx_event(rtl, command, &command_data);
|
|
|
|
+ else
|
|
|
|
+ ssh_rtl_complete(rtl, command, &command_data);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_rx_data(struct ssh_ptl *p, const struct sshp_span *data)
|
|
|
|
+{
|
|
|
|
+ switch (data->ptr[0]) {
|
|
|
|
+ case SSH_PLD_TYPE_CMD:
|
|
|
|
+ ssh_rtl_rx_command(p, data);
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ default:
|
|
|
|
+ ptl_err(p, "rtl: rx: unknown frame payload type"
|
|
|
|
+ " (type: 0x%02x)\n", data->ptr[0]);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static inline struct device *ssh_rtl_get_device(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ return ssh_ptl_get_device(&rtl->ptl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline bool ssh_rtl_tx_flush(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ return flush_work(&rtl->tx.work);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline int ssh_rtl_tx_start(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+ bool sched;
|
|
|
|
+
|
|
|
|
+ status = ssh_ptl_tx_start(&rtl->ptl);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * If the packet layer has been shut down and restarted without shutting
|
|
|
|
+ * down the request layer, there may still be requests queued and not
|
|
|
|
+ * handled.
|
|
|
|
+ */
|
|
|
|
+ spin_lock(&rtl->queue.lock);
|
|
|
|
+ sched = !list_empty(&rtl->queue.head);
|
|
|
|
+ spin_unlock(&rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ if (sched)
|
|
|
|
+ ssh_rtl_tx_schedule(rtl);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline int ssh_rtl_rx_start(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ return ssh_ptl_rx_start(&rtl->ptl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssh_rtl_init(struct ssh_rtl *rtl, struct serdev_device *serdev,
|
|
|
|
+ struct ssh_rtl_ops *ops)
|
|
|
|
+{
|
|
|
|
+ struct ssh_ptl_ops ptl_ops;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ ptl_ops.data_received = ssh_rtl_rx_data;
|
|
|
|
+
|
|
|
|
+ status = ssh_ptl_init(&rtl->ptl, serdev, &ptl_ops);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ spin_lock_init(&rtl->queue.lock);
|
|
|
|
+ INIT_LIST_HEAD(&rtl->queue.head);
|
|
|
|
+
|
|
|
|
+ spin_lock_init(&rtl->pending.lock);
|
|
|
|
+ INIT_LIST_HEAD(&rtl->pending.head);
|
|
|
|
+ atomic_set_release(&rtl->pending.count, 0);
|
|
|
|
+
|
|
|
|
+ INIT_WORK(&rtl->tx.work, ssh_rtl_tx_work_fn);
|
|
|
|
+
|
|
|
|
+ rtl->rtx_timeout.timeout = SSH_RTL_REQUEST_TIMEOUT;
|
|
|
|
+ rtl->rtx_timeout.expires = KTIME_MAX;
|
|
|
|
+ INIT_DELAYED_WORK(&rtl->rtx_timeout.reaper, ssh_rtl_timeout_reap);
|
|
|
|
+
|
|
|
|
+ rtl->ops = *ops;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_destroy(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ ssh_ptl_destroy(&rtl->ptl);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_packet_release(struct ssh_packet *p)
|
|
|
|
+{
|
|
|
|
+ struct ssh_request *rqst = to_ssh_request(p, packet);
|
|
|
|
+ rqst->ops->release(rqst);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static const struct ssh_packet_ops ssh_rtl_packet_ops = {
|
|
|
|
+ .complete = ssh_rtl_packet_callback,
|
|
|
|
+ .release = ssh_rtl_packet_release,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static void ssh_request_init(struct ssh_request *rqst,
|
|
|
|
+ enum ssam_request_flags flags,
|
|
|
|
+ const struct ssh_request_ops *ops)
|
|
|
|
+{
|
|
|
|
+ struct ssh_packet_args packet_args;
|
|
|
|
+
|
|
|
|
+ packet_args.type = SSH_PACKET_TY_BLOCKING;
|
|
|
|
+ if (!(flags & SSAM_REQUEST_UNSEQUENCED))
|
2020-07-15 20:26:02 +00:00
|
|
|
+ packet_args.type |= SSH_PACKET_TY_SEQUENCED;
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
|
|
|
+ packet_args.priority = SSH_PACKET_PRIORITY(DATA, 0);
|
|
|
|
+ packet_args.ops = &ssh_rtl_packet_ops;
|
|
|
|
+
|
|
|
|
+ ssh_packet_init(&rqst->packet, &packet_args);
|
|
|
|
+
|
|
|
|
+ rqst->rtl = NULL;
|
|
|
|
+ INIT_LIST_HEAD(&rqst->node);
|
|
|
|
+
|
|
|
|
+ rqst->state = 0;
|
|
|
|
+ if (flags & SSAM_REQUEST_HAS_RESPONSE)
|
|
|
|
+ rqst->state |= BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
|
|
|
|
+
|
|
|
|
+ rqst->timestamp = KTIME_MAX;
|
|
|
|
+ rqst->ops = ops;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssh_flush_request {
|
|
|
|
+ struct ssh_request base;
|
|
|
|
+ struct completion completion;
|
|
|
|
+ int status;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_flush_request_complete(struct ssh_request *r,
|
|
|
|
+ const struct ssh_command *cmd,
|
|
|
|
+ const struct sshp_span *data,
|
|
|
|
+ int status)
|
|
|
|
+{
|
|
|
|
+ struct ssh_flush_request *rqst;
|
|
|
|
+
|
|
|
|
+ rqst = container_of(r, struct ssh_flush_request, base);
|
|
|
|
+ rqst->status = status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_flush_request_release(struct ssh_request *r)
|
|
|
|
+{
|
|
|
|
+ struct ssh_flush_request *rqst;
|
|
|
|
+
|
|
|
|
+ rqst = container_of(r, struct ssh_flush_request, base);
|
|
|
|
+ complete_all(&rqst->completion);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static const struct ssh_request_ops ssh_rtl_flush_request_ops = {
|
|
|
|
+ .complete = ssh_rtl_flush_request_complete,
|
|
|
|
+ .release = ssh_rtl_flush_request_release,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * ssh_rtl_flush - flush the request transmission layer
|
|
|
|
+ * @rtl: request transmission layer
|
|
|
|
+ * @timeout: timeout for the flush operation in jiffies
|
|
|
|
+ *
|
|
|
|
+ * Queue a special flush request and wait for its completion. This request
|
|
|
|
+ * will be completed after all other currently queued and pending requests
|
|
|
|
+ * have been completed. Instead of a normal data packet, this request submits
|
|
|
|
+ * a special flush packet, meaning that upon completion, also the underlying
|
|
|
|
+ * packet transmission layer has been flushed.
|
|
|
|
+ *
|
|
|
|
+ * Flushing the request layer gurarantees that all previously submitted
|
|
|
|
+ * requests have been fully completed before this call returns. Additinally,
|
|
|
|
+ * flushing blocks execution of all later submitted requests until the flush
|
|
|
|
+ * has been completed.
|
|
|
|
+ *
|
|
|
|
+ * If the caller ensures that no new requests are submitted after a call to
|
|
|
|
+ * this function, the request transmission layer is guaranteed to have no
|
|
|
|
+ * remaining requests when this call returns. The same guarantee does not hold
|
|
|
|
+ * for the packet layer, on which control packets may still be queued after
|
|
|
|
+ * this call. See the documentation of ssh_ptl_flush for more details on
|
|
|
|
+ * packet layer flushing.
|
|
|
|
+ *
|
|
|
|
+ * Return: Zero on success, -ETIMEDOUT if the flush timed out and has been
|
|
|
|
+ * canceled as a result of the timeout, or -ESHUTDOWN if the packet and/or
|
|
|
|
+ * request transmission layer has been shut down before this call. May also
|
|
|
|
+ * return -EINTR if the underlying packet transmission has been interrupted.
|
|
|
|
+ */
|
|
|
|
+static int ssh_rtl_flush(struct ssh_rtl *rtl, unsigned long timeout)
|
|
|
|
+{
|
|
|
|
+ const unsigned init_flags = SSAM_REQUEST_UNSEQUENCED;
|
|
|
|
+ struct ssh_flush_request rqst;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ ssh_request_init(&rqst.base, init_flags, &ssh_rtl_flush_request_ops);
|
|
|
|
+ rqst.base.packet.type |= SSH_PACKET_TY_FLUSH;
|
|
|
|
+ rqst.base.packet.priority = SSH_PACKET_PRIORITY(FLUSH, 0);
|
|
|
|
+ rqst.base.state |= BIT(SSH_REQUEST_TY_FLUSH_BIT);
|
|
|
|
+
|
|
|
|
+ init_completion(&rqst.completion);
|
|
|
|
+
|
|
|
|
+ status = ssh_rtl_submit(rtl, &rqst.base);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ ssh_request_put(&rqst.base);
|
|
|
|
+
|
|
|
|
+ if (wait_for_completion_timeout(&rqst.completion, timeout))
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+ ssh_rtl_cancel(&rqst.base, true);
|
|
|
|
+ wait_for_completion(&rqst.completion);
|
|
|
|
+
|
|
|
|
+ WARN_ON(rqst.status != 0 && rqst.status != -ECANCELED
|
|
|
|
+ && rqst.status != -ESHUTDOWN && rqst.status != -EINTR);
|
|
|
|
+
|
|
|
|
+ return rqst.status == -ECANCELED ? -ETIMEDOUT : status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssh_rtl_shutdown(struct ssh_rtl *rtl)
|
|
|
|
+{
|
|
|
|
+ struct ssh_request *r, *n;
|
|
|
|
+ LIST_HEAD(claimed);
|
|
|
|
+ int pending;
|
|
|
|
+
|
|
|
|
+ set_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state);
|
|
|
|
+ smp_mb__after_atomic();
|
|
|
|
+
|
|
|
|
+ // remove requests from queue
|
|
|
|
+ spin_lock(&rtl->queue.lock);
|
|
|
|
+ list_for_each_entry_safe(r, n, &rtl->queue.head, node) {
|
|
|
|
+ set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
|
|
|
|
+
|
|
|
|
+ list_del(&r->node);
|
|
|
|
+ list_add_tail(&r->node, &claimed);
|
|
|
|
+ }
|
|
|
|
+ spin_unlock(&rtl->queue.lock);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * We have now guaranteed that the queue is empty and no more new
|
|
|
|
+ * requests can be submitted (i.e. it will stay empty). This means that
|
|
|
|
+ * calling ssh_rtl_tx_schedule will not schedule tx.work any more. So we
|
|
|
|
+ * can simply call cancel_work_sync on tx.work here and when that
|
|
|
|
+ * returns, we've locked it down. This also means that after this call,
|
|
|
|
+ * we don't submit any more packets to the underlying packet layer, so
|
|
|
|
+ * we can also shut that down.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ cancel_work_sync(&rtl->tx.work);
|
|
|
|
+ ssh_ptl_shutdown(&rtl->ptl);
|
|
|
|
+ cancel_delayed_work_sync(&rtl->rtx_timeout.reaper);
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * Shutting down the packet layer should also have caneled all requests.
|
|
|
|
+ * Thus the pending set should be empty. Attempt to handle this
|
|
|
|
+ * gracefully anyways, even though this should be dead code.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ pending = atomic_read(&rtl->pending.count);
|
|
|
|
+ WARN_ON(pending);
|
|
|
|
+
|
|
|
|
+ if (pending) {
|
|
|
|
+ spin_lock(&rtl->pending.lock);
|
|
|
|
+ list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
|
|
|
|
+ set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
|
|
|
|
+ smp_mb__before_atomic();
|
|
|
|
+ clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
|
|
|
|
+
|
|
|
|
+ list_del(&r->node);
|
|
|
|
+ list_add_tail(&r->node, &claimed);
|
|
|
|
+ }
|
|
|
|
+ spin_unlock(&rtl->pending.lock);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // finally cancel and complete requests
|
|
|
|
+ list_for_each_entry_safe(r, n, &claimed, node) {
|
|
|
|
+ // test_and_set because we still might compete with cancellation
|
|
|
|
+ if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
|
|
|
|
+ ssh_rtl_complete_with_status(r, -ESHUTDOWN);
|
|
|
|
+
|
|
|
|
+ // drop the reference we've obtained by removing it from list
|
|
|
|
+ list_del(&r->node);
|
|
|
|
+ ssh_request_put(r);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Event notifier/callbacks. --------------------------------------------- */
|
|
|
|
+/*
|
|
|
|
+ * The notifier system is based on linux/notifier.h, specifically the SRCU
|
|
|
|
+ * implementation. The difference to that is, that some bits of the notifier
|
|
|
|
+ * call return value can be tracked accross multiple calls. This is done so that
|
|
|
|
+ * handling of events can be tracked and a warning can be issued in case an
|
|
|
|
+ * event goes unhandled. The idea of that waring is that it should help discover
|
|
|
|
+ * and identify new/currently unimplemented features.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+struct ssam_nf_head {
|
|
|
|
+ struct srcu_struct srcu;
|
|
|
|
+ struct ssam_notifier_block __rcu *head;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+int ssam_nfblk_call_chain(struct ssam_nf_head *nh, struct ssam_event *event)
|
|
|
|
+{
|
|
|
|
+ struct ssam_notifier_block *nb, *next_nb;
|
|
|
|
+ int ret = 0, idx;
|
|
|
|
+
|
|
|
|
+ idx = srcu_read_lock(&nh->srcu);
|
|
|
|
+
|
|
|
|
+ nb = rcu_dereference_raw(nh->head);
|
|
|
|
+ while (nb) {
|
|
|
|
+ next_nb = rcu_dereference_raw(nb->next);
|
|
|
|
+
|
|
|
|
+ ret = (ret & SSAM_NOTIF_STATE_MASK) | nb->fn(nb, event);
|
|
|
|
+ if (ret & SSAM_NOTIF_STOP)
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ nb = next_nb;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ srcu_read_unlock(&nh->srcu, idx);
|
|
|
|
+ return ret;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * Note: This function must be synchronized by the caller with respect to other
|
|
|
|
+ * insert and/or remove calls.
|
|
|
|
+ */
|
|
|
|
+int __ssam_nfblk_insert(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
|
|
|
|
+{
|
|
|
|
+ struct ssam_notifier_block **link = &nh->head;
|
|
|
|
+
|
|
|
|
+ while ((*link) != NULL) {
|
|
|
|
+ if (unlikely((*link) == nb)) {
|
|
|
|
+ WARN(1, "double register detected");
|
|
|
|
+ return -EINVAL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (nb->priority > (*link)->priority)
|
|
|
|
+ break;
|
|
|
|
+
|
|
|
|
+ link = &((*link)->next);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ nb->next = *link;
|
|
|
|
+ rcu_assign_pointer(*link, nb);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * Note: This function must be synchronized by the caller with respect to other
|
|
|
|
+ * insert and/or remove calls. On success, the caller _must_ ensure SRCU
|
|
|
|
+ * synchronization by calling `synchronize_srcu(&nh->srcu)` after leaving the
|
|
|
|
+ * critical section, to ensure that the removed notifier block is not in use any
|
|
|
|
+ * more.
|
|
|
|
+ */
|
|
|
|
+int __ssam_nfblk_remove(struct ssam_nf_head *nh, struct ssam_notifier_block *nb)
|
|
|
|
+{
|
|
|
|
+ struct ssam_notifier_block **link = &nh->head;
|
|
|
|
+
|
|
|
|
+ while ((*link) != NULL) {
|
|
|
|
+ if ((*link) == nb) {
|
|
|
|
+ rcu_assign_pointer(*link, nb->next);
|
|
|
|
+ return 0;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ link = &((*link)->next);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return -ENOENT;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssam_nf_head_init(struct ssam_nf_head *nh)
|
|
|
|
+{
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = init_srcu_struct(&nh->srcu);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ nh->head = NULL;
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssam_nf_head_destroy(struct ssam_nf_head *nh)
|
|
|
|
+{
|
|
|
|
+ cleanup_srcu_struct(&nh->srcu);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Event/notification registry. ------------------------------------------ */
|
|
|
|
+
|
|
|
|
+struct ssam_nf_refcount_key {
|
|
|
|
+ struct ssam_event_registry reg;
|
|
|
|
+ struct ssam_event_id id;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssam_nf_refcount_entry {
|
|
|
|
+ struct rb_node node;
|
|
|
|
+ struct ssam_nf_refcount_key key;
|
|
|
|
+ int refcount;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssam_nf {
|
|
|
|
+ struct mutex lock;
|
|
|
|
+ struct rb_root refcount;
|
|
|
|
+ struct ssam_nf_head head[SURFACE_SAM_SSH_NUM_EVENTS];
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int ssam_nf_refcount_inc(struct ssam_nf *nf,
|
|
|
|
+ struct ssam_event_registry reg,
|
|
|
|
+ struct ssam_event_id id)
|
|
|
|
+{
|
|
|
|
+ struct ssam_nf_refcount_entry *entry;
|
|
|
|
+ struct ssam_nf_refcount_key key;
|
|
|
|
+ struct rb_node **link = &nf->refcount.rb_node;
|
|
|
|
+ struct rb_node *parent;
|
|
|
|
+ int cmp;
|
|
|
|
+
|
|
|
|
+ key.reg = reg;
|
|
|
|
+ key.id = id;
|
|
|
|
+
|
|
|
|
+ while (*link) {
|
|
|
|
+ entry = rb_entry(*link, struct ssam_nf_refcount_entry, node);
|
|
|
|
+ parent = *link;
|
|
|
|
+
|
|
|
|
+ cmp = memcmp(&key, &entry->key, sizeof(key));
|
|
|
|
+ if (cmp < 0) {
|
|
|
|
+ link = &(*link)->rb_left;
|
|
|
|
+ } else if (cmp > 0) {
|
|
|
|
+ link = &(*link)->rb_right;
|
|
|
|
+ } else if (entry->refcount < INT_MAX) {
|
|
|
|
+ return ++entry->refcount;
|
|
|
|
+ } else {
|
|
|
|
+ return -ENOSPC;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ entry = kzalloc(sizeof(*entry), GFP_KERNEL);
|
|
|
|
+ if (!entry)
|
|
|
|
+ return -ENOMEM;
|
|
|
|
+
|
|
|
|
+ entry->key = key;
|
|
|
|
+ entry->refcount = 1;
|
|
|
|
+
|
|
|
|
+ rb_link_node(&entry->node, parent, link);
|
|
|
|
+ rb_insert_color(&entry->node, &nf->refcount);
|
|
|
|
+
|
|
|
|
+ return entry->refcount;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssam_nf_refcount_dec(struct ssam_nf *nf,
|
|
|
|
+ struct ssam_event_registry reg,
|
|
|
|
+ struct ssam_event_id id)
|
|
|
|
+{
|
|
|
|
+ struct ssam_nf_refcount_entry *entry;
|
|
|
|
+ struct ssam_nf_refcount_key key;
|
|
|
|
+ struct rb_node *node = nf->refcount.rb_node;
|
|
|
|
+ int cmp, rc;
|
|
|
|
+
|
|
|
|
+ key.reg = reg;
|
|
|
|
+ key.id = id;
|
|
|
|
+
|
|
|
|
+ while (node) {
|
|
|
|
+ entry = rb_entry(node, struct ssam_nf_refcount_entry, node);
|
|
|
|
+
|
|
|
|
+ cmp = memcmp(&key, &entry->key, sizeof(key));
|
|
|
|
+ if (cmp < 0) {
|
|
|
|
+ node = node->rb_left;
|
|
|
|
+ } else if (cmp > 0) {
|
|
|
|
+ node = node->rb_right;
|
|
|
|
+ } else {
|
|
|
|
+ rc = --entry->refcount;
|
|
|
|
+
|
|
|
|
+ if (rc == 0) {
|
|
|
|
+ rb_erase(&entry->node, &nf->refcount);
|
|
|
|
+ kfree(entry);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return rc;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return -ENOENT;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssam_nf_call(struct ssam_nf *nf, struct device *dev, u16 rqid,
|
|
|
|
+ struct ssam_event *event)
|
|
|
|
+{
|
|
|
|
+ struct ssam_nf_head *nf_head;
|
|
|
|
+ int status, nf_ret;
|
|
|
|
+
|
|
|
|
+ if (!ssh_rqid_is_event(rqid)) {
|
|
|
|
+ dev_warn(dev, "event: unsupported rqid: 0x%04x\n", rqid);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ nf_head = &nf->head[ssh_rqid_to_event(rqid)];
|
|
|
|
+ nf_ret = ssam_nfblk_call_chain(nf_head, event);
|
|
|
|
+ status = ssam_notifier_to_errno(nf_ret);
|
|
|
|
+
|
|
|
|
+ if (status < 0) {
|
|
|
|
+ dev_err(dev, "event: error handling event: %d "
|
|
|
|
+ "(tc: 0x%02x, cid: 0x%02x, iid: 0x%02x, chn: 0x%02x)\n",
|
|
|
|
+ status, event->target_category, event->command_id,
|
|
|
|
+ event->instance_id, event->channel);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!(nf_ret & SSAM_NOTIF_HANDLED)) {
|
|
|
|
+ dev_warn(dev, "event: unhandled event (rqid: 0x%02x, "
|
|
|
|
+ "tc: 0x%02x, cid: 0x%02x, iid: 0x%02x, chn: 0x%02x)\n",
|
|
|
|
+ rqid, event->target_category, event->command_id,
|
|
|
|
+ event->instance_id, event->channel);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssam_nf_register(struct ssam_nf *nf, struct ssam_event_notifier *n)
|
|
|
|
+{
|
|
|
|
+ u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
|
|
|
|
+ struct ssam_nf_head *nf_head;
|
|
|
|
+ int rc, status;
|
|
|
|
+
|
|
|
|
+ if (!ssh_rqid_is_event(rqid))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ nf_head = &nf->head[ssh_rqid_to_event(rqid)];
|
|
|
|
+
|
|
|
|
+ mutex_lock(&nf->lock);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ rc = ssam_nf_refcount_inc(nf, n->event.reg, n->event.id);
|
|
|
|
+ if (rc < 0) {
|
|
|
|
+ mutex_lock(&nf->lock);
|
|
|
|
+ return rc;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ status = __ssam_nfblk_insert(nf_head, &n->base);
|
|
|
|
+ if (status)
|
|
|
|
+ ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
|
|
|
|
+
|
|
|
|
+ mutex_unlock(&nf->lock);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int ssam_nf_unregister(struct ssam_nf *nf, struct ssam_event_notifier *n)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u16 rqid = ssh_tc_to_rqid(n->event.id.target_category);
|
|
|
|
+ struct ssam_nf_head *nf_head;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ int status;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (!ssh_rqid_is_event(rqid))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ nf_head = &nf->head[ssh_rqid_to_event(rqid)];
|
|
|
|
+
|
|
|
|
+ mutex_lock(&nf->lock);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = __ssam_nfblk_remove(nf_head, &n->base);
|
|
|
|
+ if (status) {
|
|
|
|
+ mutex_unlock(&nf->lock);
|
|
|
|
+ return status;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ mutex_unlock(&nf->lock);
|
|
|
|
+ synchronize_srcu(&nf_head->srcu);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int ssam_nf_init(struct ssam_nf *nf)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ int i, status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ for (i = 0; i < SURFACE_SAM_SSH_NUM_EVENTS; i++) {
|
|
|
|
+ status = ssam_nf_head_init(&nf->head[i]);
|
|
|
|
+ if (status)
|
|
|
|
+ break;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status) {
|
|
|
|
+ for (i = i - 1; i >= 0; i--)
|
|
|
|
+ ssam_nf_head_destroy(&nf->head[i]);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ mutex_init(&nf->lock);
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void ssam_nf_destroy(struct ssam_nf *nf)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ int i;
|
|
|
|
+
|
|
|
|
+ for (i = 0; i < SURFACE_SAM_SSH_NUM_EVENTS; i++)
|
|
|
|
+ ssam_nf_head_destroy(&nf->head[i]);
|
|
|
|
+
|
|
|
|
+ mutex_destroy(&nf->lock);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Event/async request completion system. -------------------------------- */
|
|
|
|
+
|
|
|
|
+#define SSAM_CPLT_WQ_NAME "ssam_cpltq"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssam_cplt;
|
|
|
|
+
|
|
|
|
+struct ssam_event_item {
|
|
|
|
+ struct list_head node;
|
|
|
|
+ u16 rqid;
|
|
|
|
+ struct ssam_event event; // must be last
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssam_event_queue {
|
|
|
|
+ struct ssam_cplt *cplt;
|
|
|
|
+
|
|
|
|
+ spinlock_t lock;
|
|
|
|
+ struct list_head head;
|
|
|
|
+ struct work_struct work;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssam_event_channel {
|
|
|
|
+ struct ssam_event_queue queue[SURFACE_SAM_SSH_NUM_EVENTS];
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssam_cplt {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ struct device *dev;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct workqueue_struct *wq;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct {
|
|
|
|
+ struct ssam_event_channel channel[SURFACE_SAM_SSH_NUM_CHANNELS];
|
|
|
|
+ struct ssam_nf notif;
|
|
|
|
+ } event;
|
|
|
|
+};
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void ssam_event_queue_push(struct ssam_event_queue *q,
|
|
|
|
+ struct ssam_event_item *item)
|
|
|
|
+{
|
|
|
|
+ spin_lock(&q->lock);
|
|
|
|
+ list_add_tail(&item->node, &q->head);
|
|
|
|
+ spin_unlock(&q->lock);
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static struct ssam_event_item *ssam_event_queue_pop(struct ssam_event_queue *q)
|
|
|
|
+{
|
|
|
|
+ struct ssam_event_item *item;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ spin_lock(&q->lock);
|
|
|
|
+ item = list_first_entry_or_null(&q->head, struct ssam_event_item, node);
|
|
|
|
+ if (item)
|
|
|
|
+ list_del(&item->node);
|
|
|
|
+ spin_unlock(&q->lock);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return item;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static bool ssam_event_queue_is_empty(struct ssam_event_queue *q)
|
|
|
|
+{
|
|
|
|
+ bool empty;
|
|
|
|
+
|
|
|
|
+ spin_lock(&q->lock);
|
|
|
|
+ empty = list_empty(&q->head);
|
|
|
|
+ spin_unlock(&q->lock);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return empty;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static struct ssam_event_queue *ssam_cplt_get_event_queue(
|
|
|
|
+ struct ssam_cplt *cplt, u8 channel, u16 rqid)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u16 event = ssh_rqid_to_event(rqid);
|
|
|
|
+ u16 chidx = ssh_channel_to_index(channel);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (!ssh_rqid_is_event(rqid)) {
|
|
|
|
+ dev_err(cplt->dev, "event: unsupported rqid: 0x%04x\n", rqid);
|
|
|
|
+ return NULL;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!ssh_channel_is_valid(channel)) {
|
|
|
|
+ dev_warn(cplt->dev, "event: unsupported channel: %u\n",
|
|
|
|
+ channel);
|
|
|
|
+ chidx = 0;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return &cplt->event.channel[chidx].queue[event];
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static inline bool ssam_cplt_submit(struct ssam_cplt *cplt,
|
|
|
|
+ struct work_struct *work)
|
|
|
|
+{
|
|
|
|
+ return queue_work(cplt->wq, work);
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int ssam_cplt_submit_event(struct ssam_cplt *cplt,
|
|
|
|
+ struct ssam_event_item *item)
|
|
|
|
+{
|
|
|
|
+ struct ssam_event_queue *evq;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ evq = ssam_cplt_get_event_queue(cplt, item->event.channel, item->rqid);
|
|
|
|
+ if (!evq)
|
|
|
|
+ return -EINVAL;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssam_event_queue_push(evq, item);
|
|
|
|
+ ssam_cplt_submit(cplt, &evq->work);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssam_cplt_flush(struct ssam_cplt *cplt)
|
|
|
|
+{
|
|
|
|
+ flush_workqueue(cplt->wq);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssam_event_queue_work_fn(struct work_struct *work)
|
|
|
|
+{
|
|
|
|
+ struct ssam_event_queue *queue;
|
|
|
|
+ struct ssam_event_item *item;
|
|
|
|
+ struct ssam_nf *nf;
|
|
|
|
+ struct device *dev;
|
|
|
|
+ int i;
|
|
|
|
+
|
|
|
|
+ queue = container_of(work, struct ssam_event_queue, work);
|
|
|
|
+ nf = &queue->cplt->event.notif;
|
|
|
|
+ dev = queue->cplt->dev;
|
|
|
|
+
|
|
|
|
+ for (i = 0; i < 10; i++) {
|
|
|
|
+ item = ssam_event_queue_pop(queue);
|
|
|
|
+ if (item == NULL)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ ssam_nf_call(nf, dev, item->rqid, &item->event);
|
|
|
|
+ kfree(item);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
2020-07-02 21:19:52 +00:00
|
|
|
+
|
|
|
|
+ if (!ssam_event_queue_is_empty(queue))
|
|
|
|
+ ssam_cplt_submit(queue->cplt, &queue->work);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssam_event_queue_init(struct ssam_cplt *cplt,
|
|
|
|
+ struct ssam_event_queue *evq)
|
|
|
|
+{
|
|
|
|
+ evq->cplt = cplt;
|
|
|
|
+ spin_lock_init(&evq->lock);
|
|
|
|
+ INIT_LIST_HEAD(&evq->head);
|
|
|
|
+ INIT_WORK(&evq->work, ssam_event_queue_work_fn);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int ssam_cplt_init(struct ssam_cplt *cplt, struct device *dev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct ssam_event_channel *channel;
|
|
|
|
+ int status, c, i;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ cplt->dev = dev;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ cplt->wq = create_workqueue(SSAM_CPLT_WQ_NAME);
|
|
|
|
+ if (!cplt->wq)
|
|
|
|
+ return -ENOMEM;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ for (c = 0; c < ARRAY_SIZE(cplt->event.channel); c++) {
|
|
|
|
+ channel = &cplt->event.channel[c];
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ for (i = 0; i < ARRAY_SIZE(channel->queue); i++)
|
|
|
|
+ ssam_event_queue_init(cplt, &channel->queue[i]);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = ssam_nf_init(&cplt->event.notif);
|
|
|
|
+ if (status)
|
|
|
|
+ destroy_workqueue(cplt->wq);
|
|
|
|
+
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssam_cplt_destroy(struct ssam_cplt *cplt)
|
|
|
|
+{
|
|
|
|
+ destroy_workqueue(cplt->wq);
|
|
|
|
+ ssam_nf_destroy(&cplt->event.notif);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Top-Level Request Interface ------------------------------------------- */
|
|
|
|
+
|
|
|
|
+struct ssam_response {
|
|
|
|
+ int status;
|
|
|
|
+ u16 capacity;
|
|
|
|
+ u16 length;
|
|
|
|
+ u8 *pointer;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssam_request_sync {
|
|
|
|
+ struct ssh_request base;
|
|
|
|
+ struct completion comp;
|
|
|
|
+ struct ssam_response resp;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssam_request_sync_complete(struct ssh_request *rqst,
|
|
|
|
+ const struct ssh_command *cmd,
|
|
|
|
+ const struct sshp_span *data, int status)
|
|
|
|
+{
|
|
|
|
+ struct ssam_request_sync *r;
|
|
|
|
+ struct ssh_rtl *rtl = READ_ONCE(rqst->rtl);
|
|
|
|
+
|
|
|
|
+ r = container_of(rqst, struct ssam_request_sync, base);
|
|
|
|
+ r->resp.status = status;
|
|
|
|
+ r->resp.length = 0;
|
|
|
|
+
|
|
|
|
+ if (status) {
|
|
|
|
+ rtl_dbg_cond(rtl, "rsp: request failed: %d\n", status);
|
|
|
|
+ return;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (!data) // handle requests without a response
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ if (!r->resp.pointer && data->len) {
|
|
|
|
+ rtl_warn(rtl, "rsp: no response buffer provided, dropping data\n");
|
|
|
|
+ return;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (data->len > r->resp.capacity) {
|
|
|
|
+ rtl_err(rtl, "rsp: response buffer too small,"
|
|
|
|
+ " capacity: %u bytes, got: %zu bytes\n",
|
|
|
|
+ r->resp.capacity, data->len);
|
2020-07-13 14:07:25 +00:00
|
|
|
+ r->resp.status = -ENOSPC;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ r->resp.length = data->len;
|
|
|
|
+ memcpy(r->resp.pointer, data->ptr, data->len);
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void ssam_request_sync_release(struct ssh_request *rqst)
|
|
|
|
+{
|
|
|
|
+ complete_all(&container_of(rqst, struct ssam_request_sync, base)->comp);
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static const struct ssh_request_ops ssam_request_sync_ops = {
|
|
|
|
+ .release = ssam_request_sync_release,
|
|
|
|
+ .complete = ssam_request_sync_complete,
|
|
|
|
+};
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void ssam_request_sync_wait_complete(struct ssam_request_sync *rqst)
|
|
|
|
+{
|
|
|
|
+ wait_for_completion(&rqst->comp);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- TODO ------------------------------------------------------------------ */
|
|
|
|
+
|
|
|
|
+enum ssh_ec_state {
|
|
|
|
+ SSH_EC_UNINITIALIZED,
|
|
|
|
+ SSH_EC_INITIALIZED,
|
|
|
|
+ SSH_EC_SUSPENDED,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct sam_ssh_ec {
|
|
|
|
+ struct serdev_device *serdev;
|
|
|
|
+
|
|
|
|
+ struct ssh_rtl rtl;
|
|
|
|
+ struct ssam_cplt cplt;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ struct ssh_seq_counter seq;
|
|
|
|
+ struct ssh_rqid_counter rqid;
|
|
|
|
+ } counter;
|
|
|
|
+
|
|
|
|
+ enum ssh_ec_state state;
|
|
|
|
+
|
|
|
|
+ int irq;
|
|
|
|
+ bool irq_wakeup_enabled;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static struct sam_ssh_ec ssh_ec = {
|
|
|
|
+ .state = SSH_EC_UNINITIALIZED,
|
|
|
|
+ .serdev = NULL,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- TODO ------------------------------------------------------------------ */
|
|
|
|
+
|
|
|
|
+#define ssh_dbg(ec, fmt, ...) dev_dbg(&(ec)->serdev->dev, fmt, ##__VA_ARGS__)
|
|
|
|
+#define ssh_warn(ec, fmt, ...) dev_warn(&(ec)->serdev->dev, fmt, ##__VA_ARGS__)
|
|
|
|
+#define ssh_err(ec, fmt, ...) dev_err(&(ec)->serdev->dev, fmt, ##__VA_ARGS__)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static inline struct sam_ssh_ec *surface_sam_ssh_acquire(void)
|
|
|
|
+{
|
|
|
|
+ return &ssh_ec;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline struct sam_ssh_ec *surface_sam_ssh_acquire_init(void)
|
|
|
|
+{
|
|
|
|
+ struct sam_ssh_ec *ec = surface_sam_ssh_acquire();
|
|
|
|
+
|
|
|
|
+ if (smp_load_acquire(&ec->state) == SSH_EC_UNINITIALIZED)
|
|
|
|
+ return NULL;
|
|
|
|
+
|
|
|
|
+ return ec;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+int surface_sam_ssh_consumer_register(struct device *consumer)
|
|
|
|
+{
|
|
|
|
+ u32 flags = DL_FLAG_PM_RUNTIME | DL_FLAG_AUTOREMOVE_CONSUMER;
|
|
|
|
+ struct sam_ssh_ec *ec;
|
|
|
|
+ struct device_link *link;
|
|
|
|
+
|
|
|
|
+ ec = surface_sam_ssh_acquire_init();
|
|
|
|
+ if (!ec)
|
|
|
|
+ return -ENXIO;
|
|
|
|
+
|
|
|
|
+ link = device_link_add(consumer, &ec->serdev->dev, flags);
|
|
|
|
+ if (!link)
|
|
|
|
+ return -EFAULT;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+EXPORT_SYMBOL_GPL(surface_sam_ssh_consumer_register);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int __surface_sam_ssh_rqst(struct sam_ssh_ec *ec,
|
|
|
|
+ const struct surface_sam_ssh_rqst *rqst,
|
|
|
|
+ struct surface_sam_ssh_buf *result);
|
|
|
|
+
|
|
|
|
+static int surface_sam_ssh_event_enable(struct sam_ssh_ec *ec,
|
|
|
|
+ struct ssam_event_registry reg,
|
|
|
|
+ struct ssam_event_id id,
|
|
|
|
+ u8 flags)
|
|
|
|
+{
|
|
|
|
+ struct ssh_notification_params params;
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+ struct surface_sam_ssh_buf result;
|
|
|
|
+
|
|
|
|
+ u16 rqid = ssh_tc_to_rqid(id.target_category);
|
|
|
|
+ u8 buf[1] = { 0x00 };
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // only allow RQIDs that lie within event spectrum
|
|
|
|
+ if (!ssh_rqid_is_event(rqid))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ params.target_category = id.target_category;
|
|
|
|
+ params.instance_id = id.instance;
|
|
|
|
+ params.flags = flags;
|
|
|
|
+ put_unaligned_le16(rqid, ¶ms.request_id);
|
|
|
|
+
|
|
|
|
+ rqst.tc = reg.target_category;
|
|
|
|
+ rqst.cid = reg.cid_enable;
|
|
|
|
+ rqst.iid = 0x00;
|
|
|
|
+ rqst.chn = reg.channel;
|
|
|
|
+ rqst.snc = 0x01;
|
|
|
|
+ rqst.cdl = sizeof(params);
|
|
|
|
+ rqst.pld = (u8 *)¶ms;
|
|
|
|
+
|
|
|
|
+ result.cap = ARRAY_SIZE(buf);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = buf;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = __surface_sam_ssh_rqst(ec, &rqst, &result);
|
|
|
|
+
|
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&ec->serdev->dev, "failed to enable event source"
|
|
|
|
+ " (tc: 0x%02x, rqid: 0x%04x)\n",
|
|
|
|
+ id.target_category, rqid);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (buf[0] != 0x00) {
|
|
|
|
+ pr_warn(SSH_RQST_TAG_FULL
|
|
|
|
+ "unexpected result while enabling event source: "
|
|
|
|
+ "0x%02x\n", buf[0]);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int surface_sam_ssh_event_disable(struct sam_ssh_ec *ec,
|
|
|
|
+ struct ssam_event_registry reg,
|
|
|
|
+ struct ssam_event_id id,
|
|
|
|
+ u8 flags)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct ssh_notification_params params;
|
|
|
|
+ struct surface_sam_ssh_rqst rqst;
|
|
|
|
+ struct surface_sam_ssh_buf result;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u16 rqid = ssh_tc_to_rqid(id.target_category);
|
|
|
|
+ u8 buf[1] = { 0x00 };
|
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // only allow RQIDs that lie within event spectrum
|
|
|
|
+ if (!ssh_rqid_is_event(rqid))
|
|
|
|
+ return -EINVAL;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ params.target_category = id.target_category;
|
|
|
|
+ params.instance_id = id.instance;
|
|
|
|
+ params.flags = flags;
|
|
|
|
+ put_unaligned_le16(rqid, ¶ms.request_id);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ rqst.tc = reg.target_category;
|
|
|
|
+ rqst.cid = reg.cid_disable;
|
|
|
|
+ rqst.iid = 0x00;
|
|
|
|
+ rqst.chn = reg.channel;
|
|
|
|
+ rqst.snc = 0x01;
|
|
|
|
+ rqst.cdl = sizeof(params);
|
|
|
|
+ rqst.pld = (u8 *)¶ms;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ result.cap = ARRAY_SIZE(buf);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = buf;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = __surface_sam_ssh_rqst(ec, &rqst, &result);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status) {
|
|
|
|
+ dev_err(&ec->serdev->dev, "failed to disable event source"
|
|
|
|
+ " (tc: 0x%02x, rqid: 0x%04x)\n",
|
|
|
|
+ id.target_category, rqid);
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (buf[0] != 0x00) {
|
|
|
|
+ dev_warn(&ec->serdev->dev,
|
|
|
|
+ "unexpected result while disabling event source: "
|
|
|
|
+ "0x%02x\n", buf[0]);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+int surface_sam_ssh_notifier_register(struct ssam_event_notifier *n)
|
|
|
|
+{
|
|
|
|
+ struct ssam_nf_head *nf_head;
|
|
|
|
+ struct sam_ssh_ec *ec;
|
|
|
|
+ struct ssam_nf *nf;
|
|
|
|
+ u16 event = ssh_tc_to_event(n->event.id.target_category);
|
|
|
|
+ u16 rqid = ssh_event_to_rqid(event);
|
|
|
|
+ int rc, status;
|
|
|
|
+
|
|
|
|
+ if (!ssh_rqid_is_event(rqid))
|
|
|
|
+ return -EINVAL;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ec = surface_sam_ssh_acquire_init();
|
|
|
|
+ if (!ec)
|
|
|
|
+ return -ENXIO;
|
|
|
|
+
|
|
|
|
+ nf = &ec->cplt.event.notif;
|
|
|
|
+ nf_head = &nf->head[event];
|
|
|
|
+
|
|
|
|
+ mutex_lock(&nf->lock);
|
|
|
|
+
|
|
|
|
+ rc = ssam_nf_refcount_inc(nf, n->event.reg, n->event.id);
|
|
|
|
+ if (rc < 0) {
|
|
|
|
+ mutex_unlock(&nf->lock);
|
|
|
|
+ return rc;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssh_dbg(ec, "enabling event (tc: 0x%02x, rc: %d)\n", rqid, rc);
|
|
|
|
+
|
|
|
|
+ status = __ssam_nfblk_insert(nf_head, &n->base);
|
|
|
|
+ if (status) {
|
|
|
|
+ ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
|
|
|
|
+ mutex_unlock(&nf->lock);
|
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (rc == 1) {
|
|
|
|
+ status = surface_sam_ssh_event_enable(ec, n->event.reg, n->event.id, n->event.flags);
|
|
|
|
+ if (status) {
|
|
|
|
+ __ssam_nfblk_remove(nf_head, &n->base);
|
|
|
|
+ ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
|
|
|
|
+ mutex_unlock(&nf->lock);
|
|
|
|
+ return status;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ mutex_unlock(&nf->lock);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+EXPORT_SYMBOL_GPL(surface_sam_ssh_notifier_register);
|
|
|
|
+
|
|
|
|
+int surface_sam_ssh_notifier_unregister(struct ssam_event_notifier *n)
|
|
|
|
+{
|
|
|
|
+ struct ssam_nf_head *nf_head;
|
|
|
|
+ struct sam_ssh_ec *ec;
|
|
|
|
+ struct ssam_nf *nf;
|
|
|
|
+ u16 event = ssh_tc_to_event(n->event.id.target_category);
|
|
|
|
+ u16 rqid = ssh_event_to_rqid(event);
|
|
|
|
+ int rc, status = 0;
|
|
|
|
+
|
|
|
|
+ if (!ssh_rqid_is_event(rqid))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ ec = surface_sam_ssh_acquire_init();
|
|
|
|
+ if (!ec)
|
|
|
|
+ return -ENXIO;
|
|
|
|
+
|
|
|
|
+ nf = &ec->cplt.event.notif;
|
|
|
|
+ nf_head = &nf->head[event];
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ mutex_lock(&nf->lock);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ rc = ssam_nf_refcount_dec(nf, n->event.reg, n->event.id);
|
|
|
|
+ if (rc < 0) {
|
|
|
|
+ mutex_unlock(&nf->lock);
|
|
|
|
+ return rc;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssh_dbg(ec, "disabling event (tc: 0x%02x, rc: %d)\n", rqid, rc);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (rc == 0)
|
|
|
|
+ status = surface_sam_ssh_event_disable(ec, n->event.reg, n->event.id, n->event.flags);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ __ssam_nfblk_remove(nf_head, &n->base);
|
|
|
|
+ mutex_unlock(&nf->lock);
|
|
|
|
+ synchronize_srcu(&nf_head->srcu);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
2020-07-02 21:19:52 +00:00
|
|
|
+EXPORT_SYMBOL_GPL(surface_sam_ssh_notifier_unregister);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int __surface_sam_ssh_rqst(struct sam_ssh_ec *ec,
|
|
|
|
+ const struct surface_sam_ssh_rqst *rqst,
|
|
|
|
+ struct surface_sam_ssh_buf *result)
|
|
|
|
+{
|
|
|
|
+ struct ssam_request_sync actual;
|
|
|
|
+ struct msgbuf msgb;
|
|
|
|
+ size_t msglen = SSH_COMMAND_MESSAGE_LENGTH(rqst->cdl);
|
|
|
|
+ unsigned flags = 0;
|
|
|
|
+ u16 rqid;
|
|
|
|
+ u8 seq;
|
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // prevent overflow
|
|
|
|
+ if (rqst->cdl > SSH_COMMAND_MAX_PAYLOAD_SIZE) {
|
|
|
|
+ ssh_err(ec, SSH_RQST_TAG "request payload too large\n");
|
|
|
|
+ return -EINVAL;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (result && result->data && rqst->snc)
|
|
|
|
+ flags |= SSAM_REQUEST_HAS_RESPONSE;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssh_request_init(&actual.base, flags, &ssam_request_sync_ops);
|
|
|
|
+ init_completion(&actual.comp);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ actual.resp.pointer = NULL;
|
|
|
|
+ actual.resp.capacity = 0;
|
|
|
|
+ actual.resp.length = 0;
|
|
|
|
+ actual.resp.status = 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (result) {
|
|
|
|
+ actual.resp.pointer = result->data;
|
|
|
|
+ actual.resp.capacity = result->cap;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // alloc and create message
|
|
|
|
+ status = msgb_alloc(&msgb, msglen, GFP_KERNEL);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ seq = ssh_seq_next(&ec->counter.seq);
|
|
|
|
+ rqid = ssh_rqid_next(&ec->counter.rqid);
|
|
|
|
+ msgb_push_cmd(&msgb, seq, rqst, rqid);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ actual.base.packet.data = msgb.buffer;
|
|
|
|
+ actual.base.packet.data_length = msgb.ptr - msgb.buffer;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = ssh_rtl_submit(&ec->rtl, &actual.base);
|
|
|
|
+ if (status) {
|
|
|
|
+ msgb_free(&msgb);
|
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssh_request_put(&actual.base);
|
|
|
|
+ ssam_request_sync_wait_complete(&actual);
|
|
|
|
+ msgb_free(&msgb);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (result)
|
|
|
|
+ result->len = actual.resp.length;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return actual.resp.status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+int surface_sam_ssh_rqst(const struct surface_sam_ssh_rqst *rqst, struct surface_sam_ssh_buf *result)
|
|
|
|
+{
|
|
|
|
+ struct sam_ssh_ec *ec;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ec = surface_sam_ssh_acquire_init();
|
|
|
|
+ if (!ec) {
|
|
|
|
+ pr_warn(SSH_RQST_TAG_FULL "embedded controller is uninitialized\n");
|
|
|
|
+ return -ENXIO;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (smp_load_acquire(&ec->state) == SSH_EC_SUSPENDED) {
|
|
|
|
+ ssh_warn(ec, SSH_RQST_TAG "embedded controller is suspended\n");
|
|
|
|
+ return -EPERM;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return __surface_sam_ssh_rqst(ec, rqst, result);
|
|
|
|
+}
|
|
|
|
+EXPORT_SYMBOL_GPL(surface_sam_ssh_rqst);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+/**
|
|
|
|
+ * surface_sam_ssh_ec_resume - Resume the EC if it is in a suspended mode.
|
|
|
|
+ * @ec: the EC to resume
|
|
|
|
+ *
|
|
|
|
+ * Moves the EC from a suspended state to a normal state. See the
|
|
|
|
+ * `surface_sam_ssh_ec_suspend` function what the specific differences of
|
|
|
|
+ * these states are. Multiple repeated calls to this function seem to be
|
|
|
|
+ * handled fine by the EC, after the first call, the state will remain
|
|
|
|
+ * "normal".
|
|
|
|
+ *
|
|
|
|
+ * Must be called with the EC initialized and its lock held.
|
|
|
|
+ */
|
|
|
|
+static int surface_sam_ssh_ec_resume(struct sam_ssh_ec *ec)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u8 buf[1] = { 0x00 };
|
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
|
|
|
+ .tc = 0x01,
|
|
|
|
+ .cid = 0x16,
|
|
|
|
+ .iid = 0x00,
|
|
|
|
+ .chn = 0x01,
|
|
|
|
+ .snc = 0x01,
|
|
|
|
+ .cdl = 0x00,
|
|
|
|
+ .pld = NULL,
|
|
|
|
+ };
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct surface_sam_ssh_buf result = {
|
|
|
|
+ result.cap = ARRAY_SIZE(buf),
|
|
|
|
+ result.len = 0,
|
|
|
|
+ result.data = buf,
|
|
|
|
+ };
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssh_dbg(ec, "pm: resuming system aggregator module\n");
|
|
|
|
+ status = __surface_sam_ssh_rqst(ec, &rqst, &result);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ /*
|
|
|
|
+ * The purpose of the return value of this request is unknown. Based on
|
|
|
|
+ * logging and experience, we expect it to be zero. No other value has
|
|
|
|
+ * been observed so far.
|
|
|
|
+ */
|
|
|
|
+ if (buf[0] != 0x00) {
|
|
|
|
+ ssh_warn(ec, "unexpected result while trying to resume EC: "
|
|
|
|
+ "0x%02x\n", buf[0]);
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return 0;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+/**
|
|
|
|
+ * surface_sam_ssh_ec_suspend - Put the EC in a suspended mode:
|
|
|
|
+ * @ec: the EC to suspend
|
|
|
|
+ *
|
|
|
|
+ * Tells the EC to enter a suspended mode. In this mode, events are quiesced
|
|
|
|
+ * and the wake IRQ is armed (note that the wake IRQ does not fire if the EC
|
|
|
|
+ * has not been suspended via this request). On some devices, the keyboard
|
|
|
|
+ * backlight is turned off. Apart from this, the EC seems to continue to work
|
|
|
|
+ * as normal, meaning requests sent to it are acknowledged and seem to be
|
|
|
|
+ * correctly handled, including potential responses. Multiple repeated calls
|
|
|
|
+ * to this function seem to be handled fine by the EC, after the first call,
|
|
|
|
+ * the state will remain "suspended".
|
|
|
|
+ *
|
|
|
|
+ * Must be called with the EC initialized and its lock held.
|
|
|
|
+ */
|
|
|
|
+static int surface_sam_ssh_ec_suspend(struct sam_ssh_ec *ec)
|
|
|
|
+{
|
|
|
|
+ u8 buf[1] = { 0x00 };
|
|
|
|
+ int status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
|
|
|
+ .tc = 0x01,
|
|
|
|
+ .cid = 0x15,
|
|
|
|
+ .iid = 0x00,
|
|
|
|
+ .chn = 0x01,
|
|
|
|
+ .snc = 0x01,
|
|
|
|
+ .cdl = 0x00,
|
|
|
|
+ .pld = NULL,
|
|
|
|
+ };
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct surface_sam_ssh_buf result = {
|
|
|
|
+ result.cap = ARRAY_SIZE(buf),
|
|
|
|
+ result.len = 0,
|
|
|
|
+ result.data = buf,
|
|
|
|
+ };
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssh_dbg(ec, "pm: suspending system aggregator module\n");
|
|
|
|
+ status = __surface_sam_ssh_rqst(ec, &rqst, &result);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ /*
|
|
|
|
+ * The purpose of the return value of this request is unknown. Based on
|
|
|
|
+ * logging and experience, we expect it to be zero. No other value has
|
|
|
|
+ * been observed so far.
|
|
|
|
+ */
|
|
|
|
+ if (buf[0] != 0x00) {
|
|
|
|
+ ssh_warn(ec, "unexpected result while trying to suspend EC: "
|
|
|
|
+ "0x%02x\n", buf[0]);
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int surface_sam_ssh_get_controller_version(struct sam_ssh_ec *ec, u32 *version)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct surface_sam_ssh_rqst rqst = {
|
|
|
|
+ .tc = 0x01,
|
|
|
|
+ .cid = 0x13,
|
|
|
|
+ .iid = 0x00,
|
|
|
|
+ .chn = 0x01,
|
|
|
|
+ .snc = 0x01,
|
|
|
|
+ .cdl = 0x00,
|
|
|
|
+ .pld = NULL,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ struct surface_sam_ssh_buf result = {
|
|
|
|
+ result.cap = sizeof(*version),
|
|
|
|
+ result.len = 0,
|
|
|
|
+ result.data = (u8 *)version,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ *version = 0;
|
|
|
|
+ return __surface_sam_ssh_rqst(ec, &rqst, &result);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int surface_sam_ssh_log_controller_version(struct sam_ssh_ec *ec)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u32 version, a, b, c;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_get_controller_version(ec, &version);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ a = (version >> 24) & 0xff;
|
|
|
|
+ b = le16_to_cpu((version >> 8) & 0xffff);
|
|
|
|
+ c = version & 0xff;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_info(&ec->serdev->dev, "SAM controller version: %u.%u.%u\n",
|
|
|
|
+ a, b, c);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static const struct acpi_gpio_params gpio_ssh_wakeup_int = { 0, 0, false };
|
|
|
|
+static const struct acpi_gpio_params gpio_ssh_wakeup = { 1, 0, false };
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static const struct acpi_gpio_mapping ssh_acpi_gpios[] = {
|
|
|
|
+ { "ssh_wakeup-int-gpio", &gpio_ssh_wakeup_int, 1 },
|
|
|
|
+ { "ssh_wakeup-gpio", &gpio_ssh_wakeup, 1 },
|
2019-12-10 00:26:31 +00:00
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static irqreturn_t ssh_wake_irq_handler(int irq, void *dev_id)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
|
|
|
+ struct serdev_device *serdev = dev_id;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_dbg(&serdev->dev, "pm: wake irq triggered\n");
|
|
|
|
+
|
|
|
|
+ // TODO: Send GPIO callback command repeatedly to EC until callback
|
|
|
|
+ // returns 0x00. Return flag of callback is "has more events".
|
|
|
|
+ // Each time the command is sent, one event is "released". Once
|
|
|
|
+ // all events have been released (return = 0x00), the GPIO is
|
|
|
|
+ // re-armed.
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return IRQ_HANDLED;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int ssh_setup_irq(struct serdev_device *serdev)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2019-12-27 23:54:14 +00:00
|
|
|
+ const int irqf = IRQF_SHARED | IRQF_ONESHOT | IRQF_TRIGGER_RISING;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ struct gpio_desc *gpiod;
|
|
|
|
+ int irq;
|
|
|
|
+ int status;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ gpiod = gpiod_get(&serdev->dev, "ssh_wakeup-int", GPIOD_ASIS);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ if (IS_ERR(gpiod))
|
|
|
|
+ return PTR_ERR(gpiod);
|
|
|
|
+
|
|
|
|
+ irq = gpiod_to_irq(gpiod);
|
|
|
|
+ gpiod_put(gpiod);
|
|
|
|
+
|
|
|
|
+ if (irq < 0)
|
|
|
|
+ return irq;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = request_threaded_irq(irq, NULL, ssh_wake_irq_handler,
|
|
|
|
+ irqf, "surface_sam_sh_wakeup", serdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ return irq;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static acpi_status ssh_setup_from_resource(struct acpi_resource *rsc, void *ctx)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct serdev_device *serdev = ctx;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ struct acpi_resource_common_serialbus *serial;
|
|
|
|
+ struct acpi_resource_uart_serialbus *uart;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ bool flow_control;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ int status = 0;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (rsc->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return AE_OK;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ serial = &rsc->data.common_serial_bus;
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (serial->type != ACPI_RESOURCE_SERIAL_TYPE_UART)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return AE_OK;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ uart = &rsc->data.uart_serial_bus;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ // set up serdev device
|
|
|
|
+ serdev_device_set_baudrate(serdev, uart->default_baud_rate);
|
|
|
|
+
|
|
|
|
+ // serdev currently only supports RTSCTS flow control
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (uart->flow_control & SSH_SUPPORTED_FLOW_CONTROL_MASK) {
|
|
|
|
+ dev_warn(&serdev->dev, "setup: unsupported flow control"
|
|
|
|
+ " (value: 0x%02x)\n", uart->flow_control);
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ // set RTSCTS flow control
|
2020-07-02 21:19:52 +00:00
|
|
|
+ flow_control = uart->flow_control & ACPI_UART_FLOW_CONTROL_HW;
|
|
|
|
+ serdev_device_set_flow_control(serdev, flow_control);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ // 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:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_warn(&serdev->dev, "setup: unsupported parity"
|
|
|
|
+ " (value: 0x%02x)\n", uart->parity);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (status) {
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_err(&serdev->dev, "setup: failed to set parity"
|
|
|
|
+ " (value: 0x%02x)\n", uart->parity);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return AE_CTRL_TERMINATE; // we've found the resource and are done
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_sam_ssh_suspend(struct device *dev)
|
|
|
|
+{
|
|
|
|
+ struct sam_ssh_ec *ec;
|
|
|
|
+ int status;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ dev_dbg(dev, "pm: suspending\n");
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ ec = surface_sam_ssh_acquire_init();
|
|
|
|
+ if (ec) {
|
|
|
|
+ status = surface_sam_ssh_ec_suspend(ec);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ if (device_may_wakeup(dev)) {
|
|
|
|
+ status = enable_irq_wake(ec->irq);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ ec->irq_wakeup_enabled = true;
|
|
|
|
+ } else {
|
|
|
|
+ ec->irq_wakeup_enabled = false;
|
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ smp_store_release(&ec->state, SSH_EC_SUSPENDED);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_ssh_resume(struct device *dev)
|
|
|
|
+{
|
|
|
|
+ struct sam_ssh_ec *ec;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ dev_dbg(dev, "pm: resuming\n");
|
|
|
|
+
|
|
|
|
+ ec = surface_sam_ssh_acquire_init();
|
|
|
|
+ if (ec) {
|
|
|
|
+ smp_store_release(&ec->state, SSH_EC_INITIALIZED);
|
|
|
|
+
|
|
|
|
+ if (ec->irq_wakeup_enabled) {
|
|
|
|
+ status = disable_irq_wake(ec->irq);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
|
|
|
+ ec->irq_wakeup_enabled = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_ec_resume(ec);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static SIMPLE_DEV_PM_OPS(surface_sam_ssh_pm_ops, surface_sam_ssh_suspend,
|
|
|
|
+ surface_sam_ssh_resume);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static void ssam_handle_event(struct ssh_rtl *rtl,
|
|
|
|
+ const struct ssh_command *cmd,
|
|
|
|
+ const struct sshp_span *data)
|
|
|
|
+{
|
|
|
|
+ struct sam_ssh_ec *ec = container_of(rtl, struct sam_ssh_ec, rtl);
|
|
|
|
+ struct ssam_event_item *item;
|
|
|
|
+
|
|
|
|
+ item = kzalloc(sizeof(struct ssam_event_item) + data->len, GFP_KERNEL);
|
|
|
|
+ if (!item)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ item->rqid = get_unaligned_le16(&cmd->rqid);
|
|
|
|
+ item->event.target_category = cmd->tc;
|
|
|
|
+ item->event.command_id = cmd->cid;
|
|
|
|
+ item->event.instance_id = cmd->iid;
|
|
|
|
+ item->event.channel = cmd->chn_in;
|
|
|
|
+ item->event.length = data->len;
|
|
|
|
+ memcpy(&item->event.data[0], data->ptr, data->len);
|
|
|
|
+
|
|
|
|
+ ssam_cplt_submit_event(&ec->cplt, item);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct ssh_rtl_ops ssam_rtl_ops = {
|
|
|
|
+ .handle_event = ssam_handle_event,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int ssam_receive_buf(struct serdev_device *dev, const unsigned char *buf, size_t n)
|
|
|
|
+{
|
|
|
|
+ struct sam_ssh_ec *ec = serdev_device_get_drvdata(dev);
|
|
|
|
+ return ssh_ptl_rx_rcvbuf(&ec->rtl.ptl, buf, n);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void ssam_write_wakeup(struct serdev_device *dev)
|
|
|
|
+{
|
|
|
|
+ struct sam_ssh_ec *ec = serdev_device_get_drvdata(dev);
|
|
|
|
+ ssh_ptl_tx_wakeup(&ec->rtl.ptl, true);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+struct serdev_device_ops ssam_serdev_ops = {
|
|
|
|
+ .receive_buf = ssam_receive_buf,
|
|
|
|
+ .write_wakeup = ssam_write_wakeup,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#ifdef CONFIG_SURFACE_SAM_SSH_DEBUG_DEVICE
|
|
|
|
+
|
|
|
|
+static char sam_ssh_debug_rqst_buf_sysfs[256] = { 0 };
|
|
|
|
+static char sam_ssh_debug_rqst_buf_pld[255] = { 0 };
|
|
|
|
+static char sam_ssh_debug_rqst_buf_res[255] = { 0 };
|
|
|
|
+
|
|
|
|
+struct sysfs_rqst {
|
|
|
|
+ u8 tc;
|
|
|
|
+ u8 cid;
|
|
|
|
+ u8 iid;
|
|
|
|
+ u8 chn;
|
|
|
|
+ u8 snc;
|
|
|
|
+ u8 cdl;
|
|
|
|
+ u8 pld[0];
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+static ssize_t rqst_read(struct file *f, struct kobject *kobj, struct bin_attribute *attr,
|
|
|
|
+ char *buf, loff_t offs, size_t count)
|
|
|
|
+{
|
|
|
|
+ if (offs < 0 || count + offs > ARRAY_SIZE(sam_ssh_debug_rqst_buf_sysfs))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ memcpy(buf, sam_ssh_debug_rqst_buf_sysfs + offs, count);
|
|
|
|
+ return count;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static ssize_t rqst_write(struct file *f, struct kobject *kobj, struct bin_attribute *attr,
|
|
|
|
+ char *buf, loff_t offs, size_t count)
|
|
|
|
+{
|
|
|
|
+ struct sysfs_rqst *input;
|
|
|
|
+ struct surface_sam_ssh_rqst rqst = {};
|
|
|
|
+ struct surface_sam_ssh_buf result = {};
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // check basic write constriants
|
|
|
|
+ if (offs != 0 || count - sizeof(struct sysfs_rqst) > ARRAY_SIZE(sam_ssh_debug_rqst_buf_pld))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ if (count < sizeof(struct sysfs_rqst))
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ input = (struct sysfs_rqst *)buf;
|
|
|
|
+
|
|
|
|
+ // payload length should be consistent with data provided
|
|
|
|
+ if (input->cdl + sizeof(struct sysfs_rqst) != count)
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ rqst.tc = input->tc;
|
|
|
|
+ rqst.cid = input->cid;
|
|
|
|
+ rqst.iid = input->iid;
|
|
|
|
+ rqst.chn = input->chn;
|
|
|
|
+ rqst.snc = input->snc;
|
|
|
|
+ rqst.cdl = input->cdl;
|
|
|
|
+ rqst.pld = sam_ssh_debug_rqst_buf_pld;
|
|
|
|
+ memcpy(sam_ssh_debug_rqst_buf_pld, &input->pld[0], input->cdl);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ result.cap = ARRAY_SIZE(sam_ssh_debug_rqst_buf_res);
|
|
|
|
+ result.len = 0;
|
|
|
|
+ result.data = sam_ssh_debug_rqst_buf_res;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = surface_sam_ssh_rqst(&rqst, &result);
|
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ sam_ssh_debug_rqst_buf_sysfs[0] = result.len;
|
|
|
|
+ memcpy(sam_ssh_debug_rqst_buf_sysfs + 1, result.data, result.len);
|
|
|
|
+ memset(sam_ssh_debug_rqst_buf_sysfs + result.len + 1, 0,
|
|
|
|
+ ARRAY_SIZE(sam_ssh_debug_rqst_buf_sysfs) + 1 - result.len);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ return count;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static const BIN_ATTR_RW(rqst, ARRAY_SIZE(sam_ssh_debug_rqst_buf_sysfs));
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int surface_sam_ssh_sysfs_register(struct device *dev)
|
|
|
|
+{
|
|
|
|
+ return sysfs_create_bin_file(&dev->kobj, &bin_attr_rqst);
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void surface_sam_ssh_sysfs_unregister(struct device *dev)
|
|
|
|
+{
|
|
|
|
+ sysfs_remove_bin_file(&dev->kobj, &bin_attr_rqst);
|
2019-12-10 00:26:31 +00:00
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+#else /* CONFIG_SURFACE_SAM_SSH_DEBUG_DEVICE */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static int surface_sam_ssh_sysfs_register(struct device *dev)
|
|
|
|
+{
|
|
|
|
+ return 0;
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static void surface_sam_ssh_sysfs_unregister(struct device *dev)
|
|
|
|
+{
|
|
|
|
+}
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+#endif /* CONFIG_SURFACE_SAM_SSH_DEBUG_DEVICE */
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int surface_sam_ssh_probe(struct serdev_device *serdev)
|
|
|
|
+{
|
|
|
|
+ struct sam_ssh_ec *ec;
|
|
|
|
+ acpi_handle *ssh = ACPI_HANDLE(&serdev->dev);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ int status, irq;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ if (gpiod_count(&serdev->dev, NULL) < 0)
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = devm_acpi_dev_add_driver_gpios(&serdev->dev, ssh_acpi_gpios);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ if (status)
|
|
|
|
+ return status;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // setup IRQ
|
|
|
|
+ irq = ssh_setup_irq(serdev);
|
|
|
|
+ if (irq < 0)
|
|
|
|
+ return irq;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ // set up EC
|
|
|
|
+ ec = surface_sam_ssh_acquire();
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (smp_load_acquire(&ec->state) != SSH_EC_UNINITIALIZED) {
|
2019-12-10 00:26:31 +00:00
|
|
|
+ dev_err(&serdev->dev, "embedded controller already initialized\n");
|
|
|
|
+
|
|
|
|
+ status = -EBUSY;
|
2020-07-02 21:19:52 +00:00
|
|
|
+ goto err_ecinit;
|
2019-12-10 00:26:31 +00:00
|
|
|
+ }
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ec->serdev = serdev;
|
|
|
|
+ ec->irq = irq;
|
|
|
|
+ ssh_seq_reset(&ec->counter.seq);
|
|
|
|
+ ssh_rqid_reset(&ec->counter.rqid);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // initialize event/request completion system
|
|
|
|
+ status = ssam_cplt_init(&ec->cplt, &serdev->dev);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_ecinit;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // initialize request and packet transmission layers
|
|
|
|
+ status = ssh_rtl_init(&ec->rtl, serdev, &ssam_rtl_ops);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_rtl;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ serdev_device_set_drvdata(serdev, ec);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ serdev_device_set_client_ops(serdev, &ssam_serdev_ops);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ status = serdev_device_open(serdev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_open;
|
|
|
|
+
|
|
|
|
+ status = acpi_walk_resources(ssh, METHOD_NAME__CRS,
|
2020-05-16 22:20:04 +00:00
|
|
|
+ ssh_setup_from_resource, serdev);
|
|
|
|
+ if (ACPI_FAILURE(status))
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_devinit;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = ssh_rtl_tx_start(&ec->rtl);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_devinit;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = ssh_rtl_rx_start(&ec->rtl);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_devinit;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ smp_store_release(&ec->state, SSH_EC_INITIALIZED);
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_log_controller_version(ec);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_finalize;
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_ec_resume(ec);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_finalize;
|
|
|
|
+
|
|
|
|
+ status = surface_sam_ssh_sysfs_register(&serdev->dev);
|
|
|
|
+ if (status)
|
|
|
|
+ goto err_finalize;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ // TODO: The EC can wake up the system via the associated GPIO interrupt in
|
|
|
|
+ // multiple situations. One of which is the remaining battery capacity
|
|
|
|
+ // falling below a certain threshold. Normally, we should use the
|
|
|
|
+ // device_init_wakeup function, however, the EC also seems to have other
|
|
|
|
+ // reasons for waking up the system and it seems that Windows has
|
|
|
|
+ // additional checks whether the system should be resumed. In short, this
|
|
|
|
+ // causes some spourious unwanted wake-ups. For now let's thus default
|
|
|
|
+ // power/wakeup to false.
|
|
|
|
+ device_set_wakeup_capable(&serdev->dev, true);
|
|
|
|
+ acpi_walk_dep_device_list(ssh);
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+err_finalize:
|
|
|
|
+ smp_store_release(&ec->state, SSH_EC_UNINITIALIZED);
|
|
|
|
+ ssh_rtl_flush(&ec->rtl, msecs_to_jiffies(5000));
|
2019-12-10 00:26:31 +00:00
|
|
|
+err_devinit:
|
|
|
|
+ serdev_device_close(serdev);
|
|
|
|
+err_open:
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssh_rtl_shutdown(&ec->rtl);
|
|
|
|
+ ssh_rtl_destroy(&ec->rtl);
|
|
|
|
+err_rtl:
|
|
|
|
+ ssam_cplt_flush(&ec->cplt);
|
|
|
|
+ ssam_cplt_destroy(&ec->cplt);
|
|
|
|
+err_ecinit:
|
2019-12-10 00:26:31 +00:00
|
|
|
+ free_irq(irq, serdev);
|
2020-07-02 21:19:52 +00:00
|
|
|
+ serdev_device_set_drvdata(serdev, NULL);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void surface_sam_ssh_remove(struct serdev_device *serdev)
|
|
|
|
+{
|
|
|
|
+ struct sam_ssh_ec *ec;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ ec = surface_sam_ssh_acquire_init();
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!ec)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ free_irq(ec->irq, serdev);
|
|
|
|
+ surface_sam_ssh_sysfs_unregister(&serdev->dev);
|
|
|
|
+
|
|
|
|
+ // suspend EC and disable events
|
|
|
|
+ status = surface_sam_ssh_ec_suspend(ec);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ dev_err(&serdev->dev, "failed to suspend EC: %d\n", status);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // flush pending events and requests while everything still works
|
|
|
|
+ status = ssh_rtl_flush(&ec->rtl, msecs_to_jiffies(5000));
|
|
|
|
+ if (status)
|
|
|
|
+ dev_err(&serdev->dev, "failed to flush request transmission layer: %d\n", status);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssam_cplt_flush(&ec->cplt);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // mark device as uninitialized
|
|
|
|
+ smp_store_release(&ec->state, SSH_EC_UNINITIALIZED);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // cancel rem. requests, ensure no new ones can be queued, stop threads
|
|
|
|
+ ssh_rtl_tx_flush(&ec->rtl);
|
|
|
|
+ ssh_rtl_shutdown(&ec->rtl);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // shut down actual transport
|
|
|
|
+ serdev_device_wait_until_sent(ec->serdev, 0);
|
|
|
|
+ serdev_device_close(ec->serdev);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ /*
|
2020-07-02 21:19:52 +00:00
|
|
|
+ * Ensure _all_ events are completed. New ones could still have been
|
|
|
|
+ * received after the last flush, before the request transport layer
|
|
|
|
+ * has been shut down. At this point we can be sure that no requests
|
|
|
|
+ * will remain after this call.
|
2019-12-10 00:26:31 +00:00
|
|
|
+ */
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ssam_cplt_flush(&ec->cplt);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ // actually free resources
|
|
|
|
+ ssam_cplt_destroy(&ec->cplt);
|
|
|
|
+ ssh_rtl_destroy(&ec->rtl);
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ ec->serdev = NULL;
|
|
|
|
+ ec->irq = -1;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ device_set_wakeup_capable(&serdev->dev, false);
|
|
|
|
+ serdev_device_set_drvdata(serdev, NULL);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static const struct acpi_device_id surface_sam_ssh_match[] = {
|
|
|
|
+ { "MSHW0084", 0 },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+MODULE_DEVICE_TABLE(acpi, surface_sam_ssh_match);
|
|
|
|
+
|
|
|
|
+static struct serdev_device_driver surface_sam_ssh = {
|
|
|
|
+ .probe = surface_sam_ssh_probe,
|
|
|
|
+ .remove = surface_sam_ssh_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_ssh",
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .acpi_match_table = surface_sam_ssh_match,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .pm = &surface_sam_ssh_pm_ops,
|
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int __init surface_sam_ssh_init(void)
|
|
|
|
+{
|
|
|
|
+ return serdev_device_driver_register(&surface_sam_ssh);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void __exit surface_sam_ssh_exit(void)
|
|
|
|
+{
|
|
|
|
+ serdev_device_driver_unregister(&surface_sam_ssh);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * Ensure that the driver is loaded late due to some issues with the UART
|
|
|
|
+ * communication. Specifically, we want to ensure that DMA is ready and being
|
|
|
|
+ * used. Not using DMA can result in spurious communication failures,
|
|
|
|
+ * especially during boot, which among other things will result in wrong
|
|
|
|
+ * battery information (via ACPI _BIX) being displayed. Using a late init_call
|
|
|
|
+ * instead of the normal module_init gives the DMA subsystem time to
|
|
|
|
+ * initialize and via that results in a more stable communication, avoiding
|
|
|
|
+ * such failures.
|
|
|
|
+ */
|
|
|
|
+late_initcall(surface_sam_ssh_init);
|
|
|
|
+module_exit(surface_sam_ssh_exit);
|
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
|
|
|
|
+MODULE_DESCRIPTION("Surface Serial Hub Driver for 5th Generation Surface Devices");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-10 00:26:31 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_ssh.h b/drivers/platform/x86/surface_sam/surface_sam_ssh.h
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..25a3ae85fee7
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_ssh.h
|
2020-07-02 21:19:52 +00:00
|
|
|
@@ -0,0 +1,488 @@
|
|
|
|
+/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
|
|
|
+ * Interface for Surface Serial Hub (SSH).
|
|
|
|
+ *
|
|
|
|
+ * The SSH is the main communication hub for communication between host and
|
|
|
|
+ * the Surface/System Aggregator Module (SAM) on newer Microsoft Surface
|
|
|
|
+ * devices (Book 2, Pro 5, Laptops, ...). Also referred to as SAM-over-SSH.
|
2020-07-02 21:19:52 +00:00
|
|
|
+ * Older devices (Book 1, Pro 4) use SAM-over-HID (via I2C).
|
2019-12-10 00:26:31 +00:00
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#ifndef _SURFACE_SAM_SSH_H
|
|
|
|
+#define _SURFACE_SAM_SSH_H
|
|
|
|
+
|
|
|
|
+#include <linux/types.h>
|
|
|
|
+#include <linux/device.h>
|
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+/* -- Data structures for SAM-over-SSH communication. ----------------------- */
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * enum ssh_frame_type - Frame types for SSH frames.
|
|
|
|
+ * @SSH_FRAME_TYPE_DATA_SEQ: Indicates a data frame, followed by a payload with
|
|
|
|
+ * the length specified in the ssh_frame.len field. This
|
|
|
|
+ * frame is sequenced, meaning that an ACK is required.
|
|
|
|
+ * @SSH_FRAME_TYPE_DATA_NSQ: Same as SSH_FRAME_TYPE_DATA_SEQ, but unsequenced,
|
|
|
|
+ * meaning that the message does not have to be ACKed.
|
|
|
|
+ * @SSH_FRAME_TYPE_ACK: Indicates an ACK message.
|
|
|
|
+ * @SSH_FRAME_TYPE_NAK: Indicates an error response for previously sent
|
|
|
|
+ * frame. In general, this means that the frame and/or
|
|
|
|
+ * payload is malformed, e.g. a CRC is wrong. For command-
|
|
|
|
+ * type payloads, this can also mean that the command is
|
|
|
|
+ * invalid.
|
|
|
|
+ */
|
|
|
|
+enum ssh_frame_type {
|
|
|
|
+ SSH_FRAME_TYPE_DATA_SEQ = 0x80,
|
|
|
|
+ SSH_FRAME_TYPE_DATA_NSQ = 0x00,
|
|
|
|
+ SSH_FRAME_TYPE_ACK = 0x40,
|
|
|
|
+ SSH_FRAME_TYPE_NAK = 0x04,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * struct ssh_frame - SSH communication frame.
|
|
|
|
+ * @type: The type of the frame. See &enum ssh_frame_type.
|
|
|
|
+ * @len: The length of the frame payload directly following the CRC for this
|
|
|
|
+ * frame. Does not include the final CRC for that payload.
|
|
|
|
+ * @seq: The sequence number for this message/exchange.
|
|
|
|
+ */
|
|
|
|
+struct ssh_frame {
|
|
|
|
+ u8 type;
|
|
|
|
+ __le16 len;
|
|
|
|
+ u8 seq;
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+static_assert(sizeof(struct ssh_frame) == 4);
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
2020-07-02 21:19:52 +00:00
|
|
|
+ * Maximum SSH frame payload length in bytes. This is the physical maximum
|
|
|
|
+ * length of the protocol. Implementations may set a more constrained limit.
|
|
|
|
+ */
|
|
|
|
+#define SSH_FRAME_MAX_PAYLOAD_SIZE U16_MAX
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * enum ssh_payload_type - Type indicator for the SSH payload.
|
|
|
|
+ * @SSH_PLD_TYPE_CMD: The payload is a command structure with optional command
|
|
|
|
+ * payload.
|
|
|
|
+ */
|
|
|
|
+enum ssh_payload_type {
|
|
|
|
+ SSH_PLD_TYPE_CMD = 0x80,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * struct ssh_command - Payload of a command-type frame.
|
|
|
|
+ * @type: The type of the payload. See &enum ssh_payload_type. Should be
|
|
|
|
+ * SSH_PLD_TYPE_CMD for this struct.
|
|
|
|
+ * @tc: Command target category.
|
|
|
|
+ * @chn_out: Output channel. Should be zero if this an incoming (EC to host)
|
|
|
|
+ * message.
|
|
|
|
+ * @chn_in: Input channel. Should be zero if this is an outgoing (hos to EC)
|
|
|
|
+ * message.
|
|
|
|
+ * @iid: Instance ID.
|
|
|
|
+ * @rqid: Request ID. Used to match requests with responses and differentiate
|
|
|
|
+ * between responses and events.
|
|
|
|
+ * @cid: Command ID.
|
|
|
|
+ */
|
|
|
|
+struct ssh_command {
|
|
|
|
+ u8 type;
|
|
|
|
+ u8 tc;
|
|
|
|
+ u8 chn_out;
|
|
|
|
+ u8 chn_in;
|
|
|
|
+ u8 iid;
|
|
|
|
+ __le16 rqid;
|
|
|
|
+ u8 cid;
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+static_assert(sizeof(struct ssh_command) == 8);
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * Maximum SSH command payload length in bytes. This is the physical maximum
|
|
|
|
+ * length of the protocol. Implementations may set a more constrained limit.
|
|
|
|
+ */
|
|
|
|
+#define SSH_COMMAND_MAX_PAYLOAD_SIZE \
|
|
|
|
+ (SSH_FRAME_MAX_PAYLOAD_SIZE - sizeof(struct ssh_command))
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * struct ssh_notification_params - Command payload to enable/disable SSH
|
|
|
|
+ * notifications.
|
|
|
|
+ * @target_category: The target category for which notifications should be
|
|
|
|
+ * enabled/disabled.
|
|
|
|
+ * @flags: Flags determining how notifications are being sent.
|
|
|
|
+ * @request_id: The request ID that is used to send these notifications.
|
|
|
|
+ * @instance_id: The specific instance in the given target category for
|
|
|
|
+ * which notifications should be enabled.
|
|
|
|
+ */
|
|
|
|
+struct ssh_notification_params {
|
|
|
|
+ u8 target_category;
|
|
|
|
+ u8 flags;
|
|
|
|
+ __le16 request_id;
|
|
|
|
+ u8 instance_id;
|
|
|
|
+} __packed;
|
|
|
|
+
|
|
|
|
+static_assert(sizeof(struct ssh_notification_params) == 5);
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * SSH message syncrhonization (SYN) bytes.
|
|
|
|
+ */
|
|
|
|
+#define SSH_MSG_SYN ((u16)0x55aa)
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Base-length of a SSH message. This is the minimum number of bytes required
|
|
|
|
+ * to form a message. The actual message length is SSH_MSG_LEN_BASE plus the
|
|
|
|
+ * length of the frame payload.
|
|
|
|
+ */
|
|
|
|
+#define SSH_MSG_LEN_BASE (sizeof(struct ssh_frame) + 3ull * sizeof(u16))
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Length of a SSH control message.
|
|
|
|
+ */
|
|
|
|
+#define SSH_MSG_LEN_CTRL SSH_MSG_LEN_BASE
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Length of a SSH message with payload of specified size.
|
|
|
|
+ */
|
|
|
|
+#define SSH_MESSAGE_LENGTH(payload_size) (SSH_MSG_LEN_BASE + payload_size)
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Length of a SSH command message with command payload of specified size.
|
|
|
|
+ */
|
|
|
|
+#define SSH_COMMAND_MESSAGE_LENGTH(payload_size) \
|
|
|
|
+ SSH_MESSAGE_LENGTH(sizeof(struct ssh_command) + payload_size)
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Offset of the specified struct ssh_frame field in the raw SSH message data.
|
|
|
|
+ */
|
|
|
|
+#define SSH_MSGOFFSET_FRAME(field) \
|
|
|
|
+ (sizeof(u16) + offsetof(struct ssh_frame, field))
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Offset of the specified struct ssh_command field in the raw SSH message data.
|
|
|
|
+ */
|
|
|
|
+#define SSH_MSGOFFSET_COMMAND(field) \
|
|
|
|
+ (2ull * sizeof(u16) + sizeof(struct ssh_frame) \
|
|
|
|
+ + offsetof(struct ssh_command, field))
|
|
|
|
+
|
|
|
|
+struct sshp_span {
|
|
|
|
+ u8 *ptr;
|
|
|
|
+ size_t len;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Packet transport layer (ptl). ----------------------------------------- */
|
|
|
|
+
|
|
|
|
+enum ssh_packet_priority {
|
|
|
|
+ SSH_PACKET_PRIORITY_FLUSH = 0,
|
|
|
|
+ SSH_PACKET_PRIORITY_DATA = 0,
|
|
|
|
+ SSH_PACKET_PRIORITY_NAK = 1 << 4,
|
|
|
|
+ SSH_PACKET_PRIORITY_ACK = 2 << 4,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+#define SSH_PACKET_PRIORITY(base, try) \
|
|
|
|
+ ((SSH_PACKET_PRIORITY_##base) | ((try) & 0x0f))
|
|
|
|
+
|
|
|
|
+#define ssh_packet_priority_get_try(p) ((p) & 0x0f)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+enum ssh_packet_type_flags {
|
|
|
|
+ SSH_PACKET_TY_FLUSH_BIT,
|
|
|
|
+ SSH_PACKET_TY_SEQUENCED_BIT,
|
|
|
|
+ SSH_PACKET_TY_BLOCKING_BIT,
|
|
|
|
+
|
|
|
|
+ SSH_PACKET_TY_FLUSH = BIT(SSH_PACKET_TY_FLUSH_BIT),
|
|
|
|
+ SSH_PACKET_TY_SEQUENCED = BIT(SSH_PACKET_TY_SEQUENCED_BIT),
|
|
|
|
+ SSH_PACKET_TY_BLOCKING = BIT(SSH_PACKET_TY_BLOCKING_BIT),
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+enum ssh_packet_state_flags {
|
|
|
|
+ SSH_PACKET_SF_LOCKED_BIT,
|
|
|
|
+ SSH_PACKET_SF_QUEUED_BIT,
|
|
|
|
+ SSH_PACKET_SF_PENDING_BIT,
|
|
|
|
+ SSH_PACKET_SF_TRANSMITTING_BIT,
|
|
|
|
+ SSH_PACKET_SF_TRANSMITTED_BIT,
|
|
|
|
+ SSH_PACKET_SF_ACKED_BIT,
|
|
|
|
+ SSH_PACKET_SF_CANCELED_BIT,
|
|
|
|
+ SSH_PACKET_SF_COMPLETED_BIT,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssh_ptl;
|
|
|
|
+struct ssh_packet;
|
|
|
|
+
|
|
|
|
+struct ssh_packet_ops {
|
|
|
|
+ void (*release)(struct ssh_packet *packet);
|
|
|
|
+ void (*complete)(struct ssh_packet *packet, int status);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssh_packet {
|
|
|
|
+ struct ssh_ptl *ptl;
|
|
|
|
+ struct kref refcnt;
|
|
|
|
+
|
|
|
|
+ u8 type;
|
|
|
|
+ u8 priority;
|
|
|
|
+ u16 data_length;
|
|
|
|
+ u8 *data;
|
|
|
|
+
|
|
|
|
+ unsigned long state;
|
|
|
|
+ ktime_t timestamp;
|
|
|
|
+
|
|
|
|
+ struct list_head queue_node;
|
|
|
|
+ struct list_head pending_node;
|
|
|
|
+
|
|
|
|
+ const struct ssh_packet_ops *ops;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Request transport layer (rtl). ---------------------------------------- */
|
|
|
|
+
|
|
|
|
+enum ssh_request_flags {
|
|
|
|
+ SSH_REQUEST_SF_LOCKED_BIT,
|
|
|
|
+ SSH_REQUEST_SF_QUEUED_BIT,
|
|
|
|
+ SSH_REQUEST_SF_PENDING_BIT,
|
|
|
|
+ SSH_REQUEST_SF_TRANSMITTING_BIT,
|
|
|
|
+ SSH_REQUEST_SF_TRANSMITTED_BIT,
|
|
|
|
+ SSH_REQUEST_SF_RSPRCVD_BIT,
|
|
|
|
+ SSH_REQUEST_SF_CANCELED_BIT,
|
|
|
|
+ SSH_REQUEST_SF_COMPLETED_BIT,
|
|
|
|
+
|
|
|
|
+ SSH_REQUEST_TY_FLUSH_BIT,
|
|
|
|
+ SSH_REQUEST_TY_HAS_RESPONSE_BIT,
|
|
|
|
+
|
|
|
|
+ SSH_REQUEST_FLAGS_SF_MASK =
|
|
|
|
+ BIT(SSH_REQUEST_SF_LOCKED_BIT)
|
|
|
|
+ | BIT(SSH_REQUEST_SF_QUEUED_BIT)
|
|
|
|
+ | BIT(SSH_REQUEST_SF_PENDING_BIT)
|
|
|
|
+ | BIT(SSH_REQUEST_SF_TRANSMITTING_BIT)
|
|
|
|
+ | BIT(SSH_REQUEST_SF_TRANSMITTED_BIT)
|
|
|
|
+ | BIT(SSH_REQUEST_SF_RSPRCVD_BIT)
|
|
|
|
+ | BIT(SSH_REQUEST_SF_CANCELED_BIT)
|
|
|
|
+ | BIT(SSH_REQUEST_SF_COMPLETED_BIT),
|
|
|
|
+
|
|
|
|
+ SSH_REQUEST_FLAGS_TY_MASK =
|
|
|
|
+ BIT(SSH_REQUEST_TY_FLUSH_BIT)
|
|
|
|
+ | BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT),
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssh_rtl;
|
|
|
|
+struct ssh_request;
|
|
|
|
+
|
|
|
|
+struct ssh_request_ops {
|
|
|
|
+ void (*release)(struct ssh_request *rqst);
|
|
|
|
+ void (*complete)(struct ssh_request *rqst,
|
|
|
|
+ const struct ssh_command *cmd,
|
|
|
|
+ const struct sshp_span *data, int status);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssh_request {
|
|
|
|
+ struct ssh_rtl *rtl;
|
|
|
|
+ struct ssh_packet packet;
|
|
|
|
+ struct list_head node;
|
|
|
|
+
|
|
|
|
+ unsigned long state;
|
|
|
|
+ ktime_t timestamp;
|
|
|
|
+
|
|
|
|
+ const struct ssh_request_ops *ops;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Main data types and definitions --------------------------------------- */
|
|
|
|
+
|
|
|
|
+enum ssam_ssh_tc {
|
|
|
|
+ SSAM_SSH_TC_SAM = 0x01, // generic system functionality, real-time clock
|
|
|
|
+ SSAM_SSH_TC_BAT = 0x02, // battery/power subsystem
|
|
|
|
+ SSAM_SSH_TC_TMP = 0x03, // thermal subsystem
|
|
|
|
+ SSAM_SSH_TC_PMC = 0x04,
|
|
|
|
+ SSAM_SSH_TC_FAN = 0x05,
|
|
|
|
+ SSAM_SSH_TC_PoM = 0x06,
|
|
|
|
+ SSAM_SSH_TC_DBG = 0x07,
|
|
|
|
+ SSAM_SSH_TC_KBD = 0x08, // legacy keyboard (Laptop 1/2)
|
|
|
|
+ SSAM_SSH_TC_FWU = 0x09,
|
|
|
|
+ SSAM_SSH_TC_UNI = 0x0a,
|
|
|
|
+ SSAM_SSH_TC_LPC = 0x0b,
|
|
|
|
+ SSAM_SSH_TC_TCL = 0x0c,
|
|
|
|
+ SSAM_SSH_TC_SFL = 0x0d,
|
|
|
|
+ SSAM_SSH_TC_KIP = 0x0e,
|
|
|
|
+ SSAM_SSH_TC_EXT = 0x0f,
|
|
|
|
+ SSAM_SSH_TC_BLD = 0x10,
|
|
|
|
+ SSAM_SSH_TC_BAS = 0x11, // detachment system (Surface Book 2/3)
|
|
|
|
+ SSAM_SSH_TC_SEN = 0x12,
|
|
|
|
+ SSAM_SSH_TC_SRQ = 0x13,
|
|
|
|
+ SSAM_SSH_TC_MCU = 0x14,
|
|
|
|
+ SSAM_SSH_TC_HID = 0x15, // generic HID input subsystem
|
|
|
|
+ SSAM_SSH_TC_TCH = 0x16,
|
|
|
|
+ SSAM_SSH_TC_BKL = 0x17,
|
|
|
|
+ SSAM_SSH_TC_TAM = 0x18,
|
|
|
|
+ SSAM_SSH_TC_ACC = 0x19,
|
|
|
|
+ SSAM_SSH_TC_UFI = 0x1a,
|
|
|
|
+ SSAM_SSH_TC_USC = 0x1b,
|
|
|
|
+ SSAM_SSH_TC_PEN = 0x1c,
|
|
|
|
+ SSAM_SSH_TC_VID = 0x1d,
|
|
|
|
+ SSAM_SSH_TC_AUD = 0x1e,
|
|
|
|
+ SSAM_SSH_TC_SMC = 0x1f,
|
|
|
|
+ SSAM_SSH_TC_KPD = 0x20,
|
|
|
|
+ SSAM_SSH_TC_REG = 0x21,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * struct ssam_event_flags - Flags for enabling/disabling SAM-over-SSH events
|
|
|
|
+ * @SSAM_EVENT_SEQUENCED: The event will be sent via a sequenced data frame.
|
2019-12-10 00:26:31 +00:00
|
|
|
+ */
|
2020-07-02 21:19:52 +00:00
|
|
|
+enum ssam_event_flags {
|
|
|
|
+ SSAM_EVENT_SEQUENCED = BIT(0),
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssam_event {
|
|
|
|
+ u8 target_category;
|
|
|
|
+ u8 command_id;
|
|
|
|
+ u8 instance_id;
|
|
|
|
+ u8 channel;
|
|
|
|
+ u16 length;
|
|
|
|
+ u8 data[0];
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Event notifier/callbacks. --------------------------------------------- */
|
|
|
|
+
|
|
|
|
+#define SSAM_NOTIF_STATE_SHIFT 2
|
|
|
|
+#define SSAM_NOTIF_STATE_MASK ((1 << SSAM_NOTIF_STATE_SHIFT) - 1)
|
|
|
|
+
|
|
|
|
+#define SSAM_NOTIF_HANDLED BIT(0)
|
|
|
|
+#define SSAM_NOTIF_STOP BIT(1)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssam_notifier_block;
|
|
|
|
+
|
|
|
|
+typedef u32 (*ssam_notifier_fn_t)(struct ssam_notifier_block *nb,
|
|
|
|
+ const struct ssam_event *event);
|
|
|
|
+
|
|
|
|
+struct ssam_notifier_block {
|
|
|
|
+ struct ssam_notifier_block __rcu *next;
|
|
|
|
+ ssam_notifier_fn_t fn;
|
|
|
|
+ int priority;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static inline u32 ssam_notifier_from_errno(int err)
|
|
|
|
+{
|
|
|
|
+ WARN_ON(err > 0);
|
|
|
|
+
|
|
|
|
+ if (err >= 0)
|
|
|
|
+ return 0;
|
|
|
|
+ else
|
|
|
|
+ return ((-err) << SSAM_NOTIF_STATE_SHIFT) | SSAM_NOTIF_STOP;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline int ssam_notifier_to_errno(u32 ret)
|
|
|
|
+{
|
|
|
|
+ return -(ret >> SSAM_NOTIF_STATE_SHIFT);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- Event/notification registry. ------------------------------------------ */
|
|
|
|
+
|
|
|
|
+struct ssam_event_registry {
|
|
|
|
+ u8 target_category;
|
|
|
|
+ u8 channel;
|
|
|
|
+ u8 cid_enable;
|
|
|
|
+ u8 cid_disable;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct ssam_event_id {
|
|
|
|
+ u8 target_category;
|
|
|
|
+ u8 instance;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define SSAM_EVENT_REGISTRY(tc, chn, cid_en, cid_dis) \
|
|
|
|
+ ((struct ssam_event_registry) { \
|
|
|
|
+ .target_category = (tc), \
|
|
|
|
+ .channel = (chn), \
|
|
|
|
+ .cid_enable = (cid_en), \
|
|
|
|
+ .cid_disable = (cid_dis), \
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+#define SSAM_EVENT_ID(tc, iid) \
|
|
|
|
+ ((struct ssam_event_id) { \
|
|
|
|
+ .target_category = tc, \
|
|
|
|
+ .instance = iid, \
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define SSAM_EVENT_REGISTRY_SAM \
|
|
|
|
+ SSAM_EVENT_REGISTRY(SSAM_SSH_TC_SAM, 0x01, 0x0b, 0x0c)
|
|
|
|
+
|
|
|
|
+#define SSAM_EVENT_REGISTRY_KIP \
|
|
|
|
+ SSAM_EVENT_REGISTRY(SSAM_SSH_TC_KIP, 0x02, 0x27, 0x28)
|
|
|
|
+
|
|
|
|
+#define SSAM_EVENT_REGISTRY_REG \
|
|
|
|
+ SSAM_EVENT_REGISTRY(SSAM_SSH_TC_REG, 0x02, 0x01, 0x02)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct ssam_event_notifier {
|
|
|
|
+ struct ssam_notifier_block base;
|
|
|
|
+
|
|
|
|
+ struct {
|
|
|
|
+ struct ssam_event_registry reg;
|
|
|
|
+ struct ssam_event_id id;
|
|
|
|
+ u8 flags;
|
|
|
|
+ } event;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/* -- TODO -------------------------------------------------------------------*/
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * Maximum response payload size in bytes.
|
|
|
|
+ * Value based on ACPI (255 bytes minus header/status bytes).
|
|
|
|
+ */
|
|
|
|
+#define SURFACE_SAM_SSH_MAX_RQST_RESPONSE (255 - 4)
|
|
|
|
+
|
|
|
|
+/*
|
2020-07-02 21:19:52 +00:00
|
|
|
+ * The number of reserved event IDs, used for registering an SSH event
|
|
|
|
+ * handler. Valid event IDs are numbers below or equal to this value, with
|
|
|
|
+ * exception of zero, which is not an event ID. Thus, this is also the
|
|
|
|
+ * absolute maximum number of event handlers that can be registered.
|
2019-12-10 00:26:31 +00:00
|
|
|
+ */
|
2020-07-02 21:19:52 +00:00
|
|
|
+#define SURFACE_SAM_SSH_NUM_EVENTS 0x22
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+/*
|
2020-07-02 21:19:52 +00:00
|
|
|
+ * The number of communication channels used in the protocol.
|
2019-12-10 00:26:31 +00:00
|
|
|
+ */
|
2020-07-02 21:19:52 +00:00
|
|
|
+#define SURFACE_SAM_SSH_NUM_CHANNELS 2
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct surface_sam_ssh_buf {
|
|
|
|
+ u8 cap;
|
|
|
|
+ u8 len;
|
|
|
|
+ u8 *data;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+struct surface_sam_ssh_rqst {
|
|
|
|
+ u8 tc; // target category
|
|
|
|
+ u8 cid; // command ID
|
|
|
|
+ u8 iid; // instance ID
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u8 chn; // channel
|
|
|
|
+ u8 snc; // expect response flag (bool: 0/1)
|
|
|
|
+ u16 cdl; // command data length (length of payload)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ u8 *pld; // pointer to payload of length cdl
|
|
|
|
+};
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+// TODO: remove rqid on external api
|
2019-12-10 00:26:31 +00:00
|
|
|
+struct surface_sam_ssh_event {
|
|
|
|
+ u16 rqid; // event type/source ID
|
|
|
|
+ u8 tc; // target category
|
|
|
|
+ u8 cid; // command ID
|
|
|
|
+ u8 iid; // instance ID
|
2020-07-02 21:19:52 +00:00
|
|
|
+ u8 chn; // channel
|
2019-12-10 00:26:31 +00:00
|
|
|
+ u8 len; // length of payload
|
|
|
|
+ u8 *pld; // payload of length len
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+int surface_sam_ssh_consumer_register(struct device *consumer);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+int surface_sam_ssh_notifier_register(struct ssam_event_notifier *n);
|
|
|
|
+int surface_sam_ssh_notifier_unregister(struct ssam_event_notifier *n);
|
|
|
|
+
|
2019-12-10 00:26:31 +00:00
|
|
|
+int surface_sam_ssh_rqst(const struct surface_sam_ssh_rqst *rqst, struct surface_sam_ssh_buf *result);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+#endif /* _SURFACE_SAM_SSH_H */
|
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_ssh_trace.h b/drivers/platform/x86/surface_sam/surface_sam_ssh_trace.h
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..801c60205128
|
2020-07-02 21:19:52 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_ssh_trace.h
|
|
|
|
@@ -0,0 +1,536 @@
|
|
|
|
+#undef TRACE_SYSTEM
|
|
|
|
+#define TRACE_SYSTEM surface_sam_ssh
|
|
|
|
+
|
|
|
|
+#if !defined(_SURFACE_SAM_SSH_TRACE_H) || defined(TRACE_HEADER_MULTI_READ)
|
|
|
|
+#define _SURFACE_SAM_SSH_TRACE_H
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+#include <linux/tracepoint.h>
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+#include "surface_sam_ssh.h"
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_SEQ);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_DATA_NSQ);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_ACK);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_FRAME_TYPE_NAK);
|
|
|
|
+
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_TY_FLUSH_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_TY_SEQUENCED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_TY_BLOCKING_BIT);
|
|
|
|
+
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_TY_FLUSH);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_TY_SEQUENCED);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_TY_BLOCKING);
|
|
|
|
+
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_LOCKED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_QUEUED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_PENDING_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTING_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_TRANSMITTED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_ACKED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_CANCELED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_PACKET_SF_COMPLETED_BIT);
|
|
|
|
+
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_LOCKED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_QUEUED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_PENDING_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTING_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_TRANSMITTED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_RSPRCVD_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_CANCELED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_SF_COMPLETED_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_TY_FLUSH_BIT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
|
|
|
|
+
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_SF_MASK);
|
|
|
|
+TRACE_DEFINE_ENUM(SSH_REQUEST_FLAGS_TY_MASK);
|
|
|
|
+
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SAM);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_TMP);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_PMC);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_FAN);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_PoM);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_DBG);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_KBD);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_FWU);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_UNI);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_LPC);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCL);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SFL);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_KIP);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_EXT);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_BLD);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_BAS);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SEN);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SRQ);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_MCU);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_HID);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_TCH);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_BKL);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_TAM);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_ACC);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_UFI);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_USC);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_PEN);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_VID);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_AUD);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_SMC);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_KPD);
|
|
|
|
+TRACE_DEFINE_ENUM(SSAM_SSH_TC_REG);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define SSAM_PTR_UID_LEN 9
|
|
|
|
+#define SSAM_U8_FIELD_NOT_APPLICABLE ((u16)-1)
|
|
|
|
+#define SSAM_SEQ_NOT_APPLICABLE ((u16)-1)
|
|
|
|
+#define SSAM_RQID_NOT_APPLICABLE ((u32)-1)
|
|
|
|
+#define SSAM_SSH_TC_NOT_APPLICABLE 0
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#ifndef _SURFACE_SAM_SSH_TRACE_HELPERS
|
|
|
|
+#define _SURFACE_SAM_SSH_TRACE_HELPERS
|
|
|
|
+
|
|
|
|
+static inline void ssam_trace_ptr_uid(const void *ptr, char* uid_str)
|
|
|
|
+{
|
|
|
|
+ char buf[2 * sizeof(void*) + 1];
|
|
|
|
+
|
|
|
|
+ snprintf(buf, ARRAY_SIZE(buf), "%p", ptr);
|
|
|
|
+ memcpy(uid_str, &buf[ARRAY_SIZE(buf) - SSAM_PTR_UID_LEN],
|
|
|
|
+ SSAM_PTR_UID_LEN);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline u16 ssam_trace_get_packet_seq(const struct ssh_packet *p)
|
|
|
|
+{
|
|
|
|
+ if (!p->data || p->data_length < SSH_MESSAGE_LENGTH(0))
|
|
|
|
+ return SSAM_SEQ_NOT_APPLICABLE;
|
|
|
|
+
|
|
|
|
+ return p->data[SSH_MSGOFFSET_FRAME(seq)];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline u32 ssam_trace_get_request_id(const struct ssh_packet *p)
|
|
|
|
+{
|
|
|
|
+ if (!p->data || p->data_length < SSH_COMMAND_MESSAGE_LENGTH(0))
|
|
|
|
+ return SSAM_RQID_NOT_APPLICABLE;
|
|
|
|
+
|
|
|
|
+ return get_unaligned_le16(&p->data[SSH_MSGOFFSET_COMMAND(rqid)]);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static inline u32 ssam_trace_get_request_tc(const struct ssh_packet *p)
|
|
|
|
+{
|
|
|
|
+ if (!p->data || p->data_length < SSH_COMMAND_MESSAGE_LENGTH(0))
|
|
|
|
+ return SSAM_SSH_TC_NOT_APPLICABLE;
|
|
|
|
+
|
|
|
|
+ return get_unaligned_le16(&p->data[SSH_MSGOFFSET_COMMAND(tc)]);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#endif /* _SURFACE_SAM_SSH_TRACE_HELPERS */
|
|
|
|
+
|
|
|
|
+#define ssam_trace_get_command_field_u8(packet, field) \
|
|
|
|
+ ((!packet || packet->data_length < SSH_COMMAND_MESSAGE_LENGTH(0)) \
|
|
|
|
+ ? 0 : p->data[SSH_MSGOFFSET_COMMAND(field)])
|
|
|
|
+
|
|
|
|
+#define ssam_show_generic_u8_field(value) \
|
|
|
|
+ __print_symbolic(value, \
|
|
|
|
+ { SSAM_U8_FIELD_NOT_APPLICABLE, "N/A" } \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define ssam_show_frame_type(ty) \
|
|
|
|
+ __print_symbolic(ty, \
|
|
|
|
+ { SSH_FRAME_TYPE_DATA_SEQ, "DSEQ" }, \
|
|
|
|
+ { SSH_FRAME_TYPE_DATA_NSQ, "DNSQ" }, \
|
|
|
|
+ { SSH_FRAME_TYPE_ACK, "ACK" }, \
|
|
|
|
+ { SSH_FRAME_TYPE_NAK, "NAK" } \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+#define ssam_show_packet_type(type) \
|
|
|
|
+ __print_flags(type, "", \
|
|
|
|
+ { SSH_PACKET_TY_FLUSH, "F" }, \
|
|
|
|
+ { SSH_PACKET_TY_SEQUENCED, "S" }, \
|
|
|
|
+ { SSH_PACKET_TY_BLOCKING, "B" } \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+#define ssam_show_packet_state(state) \
|
|
|
|
+ __print_flags(state, "", \
|
|
|
|
+ { BIT(SSH_PACKET_SF_LOCKED_BIT), "L" }, \
|
|
|
|
+ { BIT(SSH_PACKET_SF_QUEUED_BIT), "Q" }, \
|
|
|
|
+ { BIT(SSH_PACKET_SF_PENDING_BIT), "P" }, \
|
|
|
|
+ { BIT(SSH_PACKET_SF_TRANSMITTING_BIT), "S" }, \
|
|
|
|
+ { BIT(SSH_PACKET_SF_TRANSMITTED_BIT), "T" }, \
|
|
|
|
+ { BIT(SSH_PACKET_SF_ACKED_BIT), "A" }, \
|
|
|
|
+ { BIT(SSH_PACKET_SF_CANCELED_BIT), "C" }, \
|
|
|
|
+ { BIT(SSH_PACKET_SF_COMPLETED_BIT), "F" } \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+#define ssam_show_packet_seq(seq) \
|
|
|
|
+ __print_symbolic(seq, \
|
|
|
|
+ { SSAM_SEQ_NOT_APPLICABLE, "N/A" } \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define ssam_show_request_type(flags) \
|
|
|
|
+ __print_flags(flags & SSH_REQUEST_FLAGS_TY_MASK, "", \
|
|
|
|
+ { BIT(SSH_REQUEST_TY_FLUSH_BIT), "F" }, \
|
|
|
|
+ { BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT), "R" } \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+#define ssam_show_request_state(flags) \
|
|
|
|
+ __print_flags(flags & SSH_REQUEST_FLAGS_SF_MASK, "", \
|
|
|
|
+ { BIT(SSH_REQUEST_SF_LOCKED_BIT), "L" }, \
|
|
|
|
+ { BIT(SSH_REQUEST_SF_QUEUED_BIT), "Q" }, \
|
|
|
|
+ { BIT(SSH_REQUEST_SF_PENDING_BIT), "P" }, \
|
|
|
|
+ { BIT(SSH_REQUEST_SF_TRANSMITTING_BIT), "S" }, \
|
|
|
|
+ { BIT(SSH_REQUEST_SF_TRANSMITTED_BIT), "T" }, \
|
|
|
|
+ { BIT(SSH_REQUEST_SF_RSPRCVD_BIT), "A" }, \
|
|
|
|
+ { BIT(SSH_REQUEST_SF_CANCELED_BIT), "C" }, \
|
|
|
|
+ { BIT(SSH_REQUEST_SF_COMPLETED_BIT), "F" } \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+#define ssam_show_request_id(rqid) \
|
|
|
|
+ __print_symbolic(rqid, \
|
|
|
|
+ { SSAM_RQID_NOT_APPLICABLE, "N/A" } \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+#define ssam_show_ssh_tc(rqid) \
|
|
|
|
+ __print_symbolic(rqid, \
|
|
|
|
+ { SSAM_SSH_TC_NOT_APPLICABLE, "N/A" }, \
|
|
|
|
+ { SSAM_SSH_TC_SAM, "SAM" }, \
|
|
|
|
+ { SSAM_SSH_TC_BAT, "BAT" }, \
|
|
|
|
+ { SSAM_SSH_TC_TMP, "TMP" }, \
|
|
|
|
+ { SSAM_SSH_TC_PMC, "PMC" }, \
|
|
|
|
+ { SSAM_SSH_TC_FAN, "FAN" }, \
|
|
|
|
+ { SSAM_SSH_TC_PoM, "PoM" }, \
|
|
|
|
+ { SSAM_SSH_TC_DBG, "DBG" }, \
|
|
|
|
+ { SSAM_SSH_TC_KBD, "KBD" }, \
|
|
|
|
+ { SSAM_SSH_TC_FWU, "FWU" }, \
|
|
|
|
+ { SSAM_SSH_TC_UNI, "UNI" }, \
|
|
|
|
+ { SSAM_SSH_TC_LPC, "LPC" }, \
|
|
|
|
+ { SSAM_SSH_TC_TCL, "TCL" }, \
|
|
|
|
+ { SSAM_SSH_TC_SFL, "SFL" }, \
|
|
|
|
+ { SSAM_SSH_TC_KIP, "KIP" }, \
|
|
|
|
+ { SSAM_SSH_TC_EXT, "EXT" }, \
|
|
|
|
+ { SSAM_SSH_TC_BLD, "BLD" }, \
|
|
|
|
+ { SSAM_SSH_TC_BAS, "BAS" }, \
|
|
|
|
+ { SSAM_SSH_TC_SEN, "SEN" }, \
|
|
|
|
+ { SSAM_SSH_TC_SRQ, "SRQ" }, \
|
|
|
|
+ { SSAM_SSH_TC_MCU, "MCU" }, \
|
|
|
|
+ { SSAM_SSH_TC_HID, "HID" }, \
|
|
|
|
+ { SSAM_SSH_TC_TCH, "TCH" }, \
|
|
|
|
+ { SSAM_SSH_TC_BKL, "BKL" }, \
|
|
|
|
+ { SSAM_SSH_TC_TAM, "TAM" }, \
|
|
|
|
+ { SSAM_SSH_TC_ACC, "ACC" }, \
|
|
|
|
+ { SSAM_SSH_TC_UFI, "UFI" }, \
|
|
|
|
+ { SSAM_SSH_TC_USC, "USC" }, \
|
|
|
|
+ { SSAM_SSH_TC_PEN, "PEN" }, \
|
|
|
|
+ { SSAM_SSH_TC_VID, "VID" }, \
|
|
|
|
+ { SSAM_SSH_TC_AUD, "AUD" }, \
|
|
|
|
+ { SSAM_SSH_TC_SMC, "SMC" }, \
|
|
|
|
+ { SSAM_SSH_TC_KPD, "KPD" }, \
|
|
|
|
+ { SSAM_SSH_TC_REG, "REG" } \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+DECLARE_EVENT_CLASS(ssam_frame_class,
|
|
|
|
+ TP_PROTO(const struct ssh_frame *frame),
|
|
|
|
+
|
|
|
|
+ TP_ARGS(frame),
|
|
|
|
+
|
|
|
|
+ TP_STRUCT__entry(
|
|
|
|
+ __field(u8, type)
|
|
|
|
+ __field(u8, seq)
|
|
|
|
+ __field(u16, len)
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_fast_assign(
|
|
|
|
+ __entry->type = frame->type;
|
|
|
|
+ __entry->seq = frame->seq;
|
|
|
|
+ __entry->len = get_unaligned_le16(&frame->len);
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_printk("ty=%s, seq=0x%02x, len=%u",
|
|
|
|
+ ssam_show_frame_type(__entry->type),
|
|
|
|
+ __entry->seq,
|
|
|
|
+ __entry->len
|
|
|
|
+ )
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+#define DEFINE_SSAM_FRAME_EVENT(name) \
|
|
|
|
+ DEFINE_EVENT(ssam_frame_class, ssam_##name, \
|
|
|
|
+ TP_PROTO(const struct ssh_frame *frame), \
|
|
|
|
+ TP_ARGS(frame) \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+DECLARE_EVENT_CLASS(ssam_command_class,
|
|
|
|
+ TP_PROTO(const struct ssh_command *cmd, u16 len),
|
|
|
|
+
|
|
|
|
+ TP_ARGS(cmd, len),
|
|
|
|
+
|
|
|
|
+ TP_STRUCT__entry(
|
|
|
|
+ __field(u16, rqid)
|
|
|
|
+ __field(u16, len)
|
|
|
|
+ __field(u8, tc)
|
|
|
|
+ __field(u8, cid)
|
|
|
|
+ __field(u8, iid)
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_fast_assign(
|
|
|
|
+ __entry->rqid = get_unaligned_le16(&cmd->rqid);
|
|
|
|
+ __entry->tc = cmd->tc;
|
|
|
|
+ __entry->cid = cmd->cid;
|
|
|
|
+ __entry->iid = cmd->iid;
|
|
|
|
+ __entry->len = len;
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_printk("rqid=0x%04x, tc=%s, cid=0x%02x, iid=0x%02x, len=%u",
|
|
|
|
+ __entry->rqid,
|
|
|
|
+ ssam_show_ssh_tc(__entry->tc),
|
|
|
|
+ __entry->cid,
|
|
|
|
+ __entry->iid,
|
|
|
|
+ __entry->len
|
|
|
|
+ )
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+#define DEFINE_SSAM_COMMAND_EVENT(name) \
|
|
|
|
+ DEFINE_EVENT(ssam_command_class, ssam_##name, \
|
|
|
|
+ TP_PROTO(const struct ssh_command *cmd, u16 len), \
|
|
|
|
+ TP_ARGS(cmd, len) \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+DECLARE_EVENT_CLASS(ssam_packet_class,
|
|
|
|
+ TP_PROTO(const struct ssh_packet *packet),
|
|
|
|
+
|
|
|
|
+ TP_ARGS(packet),
|
|
|
|
+
|
|
|
|
+ TP_STRUCT__entry(
|
|
|
|
+ __array(char, uid, SSAM_PTR_UID_LEN)
|
|
|
|
+ __field(u8, type)
|
|
|
|
+ __field(u8, priority)
|
|
|
|
+ __field(u16, length)
|
|
|
|
+ __field(unsigned long, state)
|
|
|
|
+ __field(u16, seq)
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_fast_assign(
|
|
|
|
+ ssam_trace_ptr_uid(packet, __entry->uid);
|
|
|
|
+ __entry->type = packet->type;
|
|
|
|
+ __entry->priority = READ_ONCE(packet->priority);
|
|
|
|
+ __entry->length = packet->data_length;
|
|
|
|
+ __entry->state = READ_ONCE(packet->state);
|
|
|
|
+ __entry->seq = ssam_trace_get_packet_seq(packet);
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_printk("uid=%s, seq=%s, ty=%s, pri=0x%02x, len=%u, sta=%s",
|
|
|
|
+ __entry->uid,
|
|
|
|
+ ssam_show_packet_seq(__entry->seq),
|
|
|
|
+ ssam_show_packet_type(__entry->type),
|
|
|
|
+ __entry->priority,
|
|
|
|
+ __entry->length,
|
|
|
|
+ ssam_show_packet_state(__entry->state)
|
|
|
|
+ )
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+#define DEFINE_SSAM_PACKET_EVENT(name) \
|
|
|
|
+ DEFINE_EVENT(ssam_packet_class, ssam_##name, \
|
|
|
|
+ TP_PROTO(const struct ssh_packet *packet), \
|
|
|
|
+ TP_ARGS(packet) \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+DECLARE_EVENT_CLASS(ssam_packet_status_class,
|
|
|
|
+ TP_PROTO(const struct ssh_packet *packet, int status),
|
|
|
|
+
|
|
|
|
+ TP_ARGS(packet, status),
|
|
|
|
+
|
|
|
|
+ TP_STRUCT__entry(
|
|
|
|
+ __array(char, uid, SSAM_PTR_UID_LEN)
|
|
|
|
+ __field(u8, type)
|
|
|
|
+ __field(u8, priority)
|
|
|
|
+ __field(u16, length)
|
|
|
|
+ __field(unsigned long, state)
|
|
|
|
+ __field(u16, seq)
|
|
|
|
+ __field(int, status)
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_fast_assign(
|
|
|
|
+ ssam_trace_ptr_uid(packet, __entry->uid);
|
|
|
|
+ __entry->type = packet->type;
|
|
|
|
+ __entry->priority = READ_ONCE(packet->priority);
|
|
|
|
+ __entry->length = packet->data_length;
|
|
|
|
+ __entry->state = READ_ONCE(packet->state);
|
|
|
|
+ __entry->seq = ssam_trace_get_packet_seq(packet);
|
|
|
|
+ __entry->status = status;
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_printk("uid=%s, seq=%s, ty=%s, pri=0x%02x, len=%u, sta=%s, status=%d",
|
|
|
|
+ __entry->uid,
|
|
|
|
+ ssam_show_packet_seq(__entry->seq),
|
|
|
|
+ ssam_show_packet_type(__entry->type),
|
|
|
|
+ __entry->priority,
|
|
|
|
+ __entry->length,
|
|
|
|
+ ssam_show_packet_state(__entry->state),
|
|
|
|
+ __entry->status
|
|
|
|
+ )
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+#define DEFINE_SSAM_PACKET_STATUS_EVENT(name) \
|
|
|
|
+ DEFINE_EVENT(ssam_packet_status_class, ssam_##name, \
|
|
|
|
+ TP_PROTO(const struct ssh_packet *packet, int status), \
|
|
|
|
+ TP_ARGS(packet, status) \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+DECLARE_EVENT_CLASS(ssam_request_class,
|
|
|
|
+ TP_PROTO(const struct ssh_request *request),
|
|
|
|
+
|
|
|
|
+ TP_ARGS(request),
|
|
|
|
+
|
|
|
|
+ TP_STRUCT__entry(
|
|
|
|
+ __array(char, uid, SSAM_PTR_UID_LEN)
|
|
|
|
+ __field(unsigned long, state)
|
|
|
|
+ __field(u32, rqid)
|
|
|
|
+ __field(u8, tc)
|
|
|
|
+ __field(u16, cid)
|
|
|
|
+ __field(u16, iid)
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_fast_assign(
|
|
|
|
+ const struct ssh_packet *p = &request->packet;
|
|
|
|
+
|
|
|
|
+ // use packet for UID so we can match requests to packets
|
|
|
|
+ ssam_trace_ptr_uid(p, __entry->uid);
|
|
|
|
+ __entry->state = READ_ONCE(request->state);
|
|
|
|
+ __entry->rqid = ssam_trace_get_request_id(p);
|
|
|
|
+ __entry->tc = ssam_trace_get_request_tc(p);
|
|
|
|
+ __entry->cid = ssam_trace_get_command_field_u8(p, cid);
|
|
|
|
+ __entry->iid = ssam_trace_get_command_field_u8(p, iid);
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_printk("uid=%s, rqid=%s, ty=%s, sta=%s, tc=%s, cid=%s, iid=%s",
|
|
|
|
+ __entry->uid,
|
|
|
|
+ ssam_show_request_id(__entry->rqid),
|
|
|
|
+ ssam_show_request_type(__entry->state),
|
|
|
|
+ ssam_show_request_state(__entry->state),
|
|
|
|
+ ssam_show_ssh_tc(__entry->tc),
|
|
|
|
+ ssam_show_generic_u8_field(__entry->cid),
|
|
|
|
+ ssam_show_generic_u8_field(__entry->iid)
|
|
|
|
+ )
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+#define DEFINE_SSAM_REQUEST_EVENT(name) \
|
|
|
|
+ DEFINE_EVENT(ssam_request_class, ssam_##name, \
|
|
|
|
+ TP_PROTO(const struct ssh_request *request), \
|
|
|
|
+ TP_ARGS(request) \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+DECLARE_EVENT_CLASS(ssam_request_status_class,
|
|
|
|
+ TP_PROTO(const struct ssh_request *request, int status),
|
|
|
|
+
|
|
|
|
+ TP_ARGS(request, status),
|
|
|
|
+
|
|
|
|
+ TP_STRUCT__entry(
|
|
|
|
+ __array(char, uid, SSAM_PTR_UID_LEN)
|
|
|
|
+ __field(unsigned long, state)
|
|
|
|
+ __field(u32, rqid)
|
|
|
|
+ __field(u8, tc)
|
|
|
|
+ __field(u16, cid)
|
|
|
|
+ __field(u16, iid)
|
|
|
|
+ __field(int, status)
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_fast_assign(
|
|
|
|
+ const struct ssh_packet *p = &request->packet;
|
|
|
|
+
|
|
|
|
+ // use packet for UID so we can match requests to packets
|
|
|
|
+ ssam_trace_ptr_uid(p, __entry->uid);
|
|
|
|
+ __entry->state = READ_ONCE(request->state);
|
|
|
|
+ __entry->rqid = ssam_trace_get_request_id(p);
|
|
|
|
+ __entry->tc = ssam_trace_get_request_tc(p);
|
|
|
|
+ __entry->cid = ssam_trace_get_command_field_u8(p, cid);
|
|
|
|
+ __entry->iid = ssam_trace_get_command_field_u8(p, iid);
|
|
|
|
+ __entry->status = status;
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_printk("uid=%s, rqid=%s, ty=%s, sta=%s, tc=%s, cid=%s, iid=%s, status=%d",
|
|
|
|
+ __entry->uid,
|
|
|
|
+ ssam_show_request_id(__entry->rqid),
|
|
|
|
+ ssam_show_request_type(__entry->state),
|
|
|
|
+ ssam_show_request_state(__entry->state),
|
|
|
|
+ ssam_show_ssh_tc(__entry->tc),
|
|
|
|
+ ssam_show_generic_u8_field(__entry->cid),
|
|
|
|
+ ssam_show_generic_u8_field(__entry->iid),
|
|
|
|
+ __entry->status
|
|
|
|
+ )
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+#define DEFINE_SSAM_REQUEST_STATUS_EVENT(name) \
|
|
|
|
+ DEFINE_EVENT(ssam_request_status_class, ssam_##name, \
|
|
|
|
+ TP_PROTO(const struct ssh_request *request, int status),\
|
|
|
|
+ TP_ARGS(request, status) \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+DECLARE_EVENT_CLASS(ssam_generic_uint_class,
|
|
|
|
+ TP_PROTO(const char* property, unsigned int value),
|
|
|
|
+
|
|
|
|
+ TP_ARGS(property, value),
|
|
|
|
+
|
|
|
|
+ TP_STRUCT__entry(
|
|
|
|
+ __string(property, property)
|
|
|
|
+ __field(unsigned int, value)
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_fast_assign(
|
|
|
|
+ __assign_str(property, property);
|
|
|
|
+ __entry->value = value;
|
|
|
|
+ ),
|
|
|
|
+
|
|
|
|
+ TP_printk("%s=%u", __get_str(property), __entry->value)
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+#define DEFINE_SSAM_GENERIC_UINT_EVENT(name) \
|
|
|
|
+ DEFINE_EVENT(ssam_generic_uint_class, ssam_##name, \
|
|
|
|
+ TP_PROTO(const char* property, unsigned int value), \
|
|
|
|
+ TP_ARGS(property, value) \
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+DEFINE_SSAM_FRAME_EVENT(rx_frame_received);
|
|
|
|
+DEFINE_SSAM_COMMAND_EVENT(rx_response_received);
|
|
|
|
+DEFINE_SSAM_COMMAND_EVENT(rx_event_received);
|
|
|
|
+
|
|
|
|
+DEFINE_SSAM_PACKET_EVENT(packet_release);
|
|
|
|
+DEFINE_SSAM_PACKET_EVENT(packet_submit);
|
|
|
|
+DEFINE_SSAM_PACKET_EVENT(packet_resubmit);
|
|
|
|
+DEFINE_SSAM_PACKET_EVENT(packet_timeout);
|
|
|
|
+DEFINE_SSAM_PACKET_EVENT(packet_cancel);
|
|
|
|
+DEFINE_SSAM_PACKET_STATUS_EVENT(packet_complete);
|
|
|
|
+DEFINE_SSAM_GENERIC_UINT_EVENT(ptl_timeout_reap);
|
|
|
|
+
|
|
|
|
+DEFINE_SSAM_REQUEST_EVENT(request_submit);
|
|
|
|
+DEFINE_SSAM_REQUEST_EVENT(request_timeout);
|
|
|
|
+DEFINE_SSAM_REQUEST_EVENT(request_cancel);
|
|
|
|
+DEFINE_SSAM_REQUEST_STATUS_EVENT(request_complete);
|
|
|
|
+DEFINE_SSAM_GENERIC_UINT_EVENT(rtl_timeout_reap);
|
|
|
|
+
|
|
|
|
+DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_ack_packet);
|
|
|
|
+DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_nak_packet);
|
|
|
|
+DEFINE_SSAM_PACKET_EVENT(ei_tx_drop_dsq_packet);
|
|
|
|
+DEFINE_SSAM_PACKET_STATUS_EVENT(ei_tx_fail_write);
|
|
|
|
+DEFINE_SSAM_PACKET_EVENT(ei_tx_corrupt_data);
|
|
|
|
+DEFINE_SSAM_GENERIC_UINT_EVENT(ei_rx_corrupt_syn);
|
|
|
|
+DEFINE_SSAM_FRAME_EVENT(ei_rx_corrupt_data);
|
|
|
|
+DEFINE_SSAM_REQUEST_EVENT(ei_rx_drop_response);
|
|
|
|
+
|
|
|
|
+#endif /* _SURFACE_SAM_SSH_TRACE_H */
|
|
|
|
+
|
|
|
|
+/* This part must be outside protection */
|
|
|
|
+#undef TRACE_INCLUDE_PATH
|
|
|
|
+#undef TRACE_INCLUDE_FILE
|
|
|
|
+
|
|
|
|
+#define TRACE_INCLUDE_PATH .
|
|
|
|
+#define TRACE_INCLUDE_FILE surface_sam_ssh_trace
|
|
|
|
+
|
|
|
|
+#include <trace/define_trace.h>
|
2019-12-10 00:26:31 +00:00
|
|
|
diff --git a/drivers/platform/x86/surface_sam/surface_sam_vhf.c b/drivers/platform/x86/surface_sam/surface_sam_vhf.c
|
|
|
|
new file mode 100644
|
2020-07-24 09:23:59 +00:00
|
|
|
index 000000000000..984035c55d63
|
2019-12-10 00:26:31 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/drivers/platform/x86/surface_sam/surface_sam_vhf.c
|
2020-07-02 21:19:52 +00:00
|
|
|
@@ -0,0 +1,261 @@
|
|
|
|
+// SPDX-License-Identifier: GPL-2.0-or-later
|
2019-12-10 00:26:31 +00:00
|
|
|
+/*
|
2020-05-16 22:20:04 +00:00
|
|
|
+ * Virtual HID Framework (VHF) driver for input events via SAM.
|
2019-12-10 00:26:31 +00:00
|
|
|
+ * Used for keyboard input events on the Surface Laptops.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+#include <linux/acpi.h>
|
|
|
|
+#include <linux/hid.h>
|
|
|
|
+#include <linux/input.h>
|
|
|
|
+#include <linux/platform_device.h>
|
|
|
|
+#include <linux/types.h>
|
|
|
|
+
|
|
|
|
+#include "surface_sam_ssh.h"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+#define USB_VENDOR_ID_MICROSOFT 0x045e
|
|
|
|
+#define USB_DEVICE_ID_MS_VHF 0xf001
|
|
|
|
+
|
|
|
|
+#define VHF_INPUT_NAME "Microsoft Virtual HID Framework Device"
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+struct vhf_drvdata {
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct platform_device *dev;
|
|
|
|
+ struct hid_device *hid;
|
|
|
|
+ struct ssam_event_notifier notif;
|
2019-12-10 00:26:31 +00:00
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+/*
|
|
|
|
+ * These report descriptors have been extracted from a Surface Book 2.
|
|
|
|
+ * They seems to be similar enough to be usable on the Surface Laptop.
|
|
|
|
+ */
|
|
|
|
+static const u8 vhf_hid_desc[] = {
|
|
|
|
+ // keyboard descriptor (event command ID 0x03)
|
|
|
|
+ 0x05, 0x01, /* Usage Page (Desktop), */
|
|
|
|
+ 0x09, 0x06, /* Usage (Keyboard), */
|
|
|
|
+ 0xA1, 0x01, /* Collection (Application), */
|
|
|
|
+ 0x85, 0x01, /* Report ID (1), */
|
|
|
|
+ 0x15, 0x00, /* Logical Minimum (0), */
|
|
|
|
+ 0x25, 0x01, /* Logical Maximum (1), */
|
|
|
|
+ 0x75, 0x01, /* Report Size (1), */
|
|
|
|
+ 0x95, 0x08, /* Report Count (8), */
|
|
|
|
+ 0x05, 0x07, /* Usage Page (Keyboard), */
|
|
|
|
+ 0x19, 0xE0, /* Usage Minimum (KB Leftcontrol), */
|
|
|
|
+ 0x29, 0xE7, /* Usage Maximum (KB Right GUI), */
|
|
|
|
+ 0x81, 0x02, /* Input (Variable), */
|
|
|
|
+ 0x75, 0x08, /* Report Size (8), */
|
|
|
|
+ 0x95, 0x0A, /* Report Count (10), */
|
|
|
|
+ 0x19, 0x00, /* Usage Minimum (None), */
|
|
|
|
+ 0x29, 0x91, /* Usage Maximum (KB LANG2), */
|
|
|
|
+ 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
|
|
|
|
+ 0x81, 0x00, /* Input, */
|
|
|
|
+ 0x05, 0x0C, /* Usage Page (Consumer), */
|
|
|
|
+ 0x0A, 0xC0, 0x02, /* Usage (02C0h), */
|
|
|
|
+ 0xA1, 0x02, /* Collection (Logical), */
|
|
|
|
+ 0x1A, 0xC1, 0x02, /* Usage Minimum (02C1h), */
|
|
|
|
+ 0x2A, 0xC6, 0x02, /* Usage Maximum (02C6h), */
|
|
|
|
+ 0x95, 0x06, /* Report Count (6), */
|
|
|
|
+ 0xB1, 0x03, /* Feature (Constant, Variable), */
|
|
|
|
+ 0xC0, /* End Collection, */
|
|
|
|
+ 0x05, 0x08, /* Usage Page (LED), */
|
|
|
|
+ 0x19, 0x01, /* Usage Minimum (01h), */
|
|
|
|
+ 0x29, 0x03, /* Usage Maximum (03h), */
|
|
|
|
+ 0x75, 0x01, /* Report Size (1), */
|
|
|
|
+ 0x95, 0x03, /* Report Count (3), */
|
|
|
|
+ 0x25, 0x01, /* Logical Maximum (1), */
|
|
|
|
+ 0x91, 0x02, /* Output (Variable), */
|
|
|
|
+ 0x95, 0x05, /* Report Count (5), */
|
|
|
|
+ 0x91, 0x01, /* Output (Constant), */
|
|
|
|
+ 0xC0, /* End Collection, */
|
|
|
|
+
|
|
|
|
+ // media key descriptor (event command ID 0x04)
|
|
|
|
+ 0x05, 0x0C, /* Usage Page (Consumer), */
|
|
|
|
+ 0x09, 0x01, /* Usage (Consumer Control), */
|
|
|
|
+ 0xA1, 0x01, /* Collection (Application), */
|
|
|
|
+ 0x85, 0x03, /* Report ID (3), */
|
|
|
|
+ 0x75, 0x10, /* Report Size (16), */
|
|
|
|
+ 0x15, 0x00, /* Logical Minimum (0), */
|
|
|
|
+ 0x26, 0xFF, 0x03, /* Logical Maximum (1023), */
|
|
|
|
+ 0x19, 0x00, /* Usage Minimum (00h), */
|
|
|
|
+ 0x2A, 0xFF, 0x03, /* Usage Maximum (03FFh), */
|
|
|
|
+ 0x81, 0x00, /* Input, */
|
|
|
|
+ 0xC0, /* End Collection, */
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static int vhf_hid_start(struct hid_device *hid)
|
|
|
|
+{
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void vhf_hid_stop(struct hid_device *hid)
|
|
|
|
+{
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int vhf_hid_open(struct hid_device *hid)
|
|
|
|
+{
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void vhf_hid_close(struct hid_device *hid)
|
|
|
|
+{
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int vhf_hid_parse(struct hid_device *hid)
|
|
|
|
+{
|
|
|
|
+ return hid_parse_report(hid, (u8 *)vhf_hid_desc, ARRAY_SIZE(vhf_hid_desc));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int vhf_hid_raw_request(struct hid_device *hid, unsigned char reportnum,
|
|
|
|
+ u8 *buf, size_t len, unsigned char rtype,
|
|
|
|
+ int reqtype)
|
|
|
|
+{
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int vhf_hid_output_report(struct hid_device *hid, u8 *buf, size_t len)
|
|
|
|
+{
|
|
|
|
+ hid_dbg(hid, "%s\n", __func__);
|
|
|
|
+ print_hex_dump_debug("report:", DUMP_PREFIX_OFFSET, 16, 1, buf, len, false);
|
|
|
|
+
|
|
|
|
+ return len;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static struct hid_ll_driver vhf_hid_ll_driver = {
|
|
|
|
+ .start = vhf_hid_start,
|
|
|
|
+ .stop = vhf_hid_stop,
|
|
|
|
+ .open = vhf_hid_open,
|
|
|
|
+ .close = vhf_hid_close,
|
|
|
|
+ .parse = vhf_hid_parse,
|
|
|
|
+ .raw_request = vhf_hid_raw_request,
|
|
|
|
+ .output_report = vhf_hid_output_report,
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static struct hid_device *vhf_create_hid_device(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct hid_device *hid;
|
|
|
|
+
|
|
|
|
+ hid = hid_allocate_device();
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (IS_ERR(hid))
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return hid;
|
|
|
|
+
|
|
|
|
+ hid->dev.parent = &pdev->dev;
|
|
|
|
+
|
|
|
|
+ hid->bus = BUS_VIRTUAL;
|
|
|
|
+ hid->vendor = USB_VENDOR_ID_MICROSOFT;
|
|
|
|
+ hid->product = USB_DEVICE_ID_MS_VHF;
|
|
|
|
+
|
|
|
|
+ hid->ll_driver = &vhf_hid_ll_driver;
|
|
|
|
+
|
|
|
|
+ sprintf(hid->name, "%s", VHF_INPUT_NAME);
|
|
|
|
+
|
|
|
|
+ return hid;
|
|
|
|
+}
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+static u32 vhf_event_handler(struct ssam_notifier_block *nb, const struct ssam_event *event)
|
2019-12-10 00:26:31 +00:00
|
|
|
+{
|
2020-07-02 21:19:52 +00:00
|
|
|
+ struct vhf_drvdata *drvdata = container_of(nb, struct vhf_drvdata, notif.base);
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ if (event->target_category != 0x08)
|
|
|
|
+ return 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ if (event->command_id == 0x03 || event->command_id == 0x04) {
|
|
|
|
+ status = hid_input_report(drvdata->hid, HID_INPUT_REPORT, (u8 *)&event->data[0], event->length, 1);
|
|
|
|
+ return ssam_notifier_from_errno(status) | SSAM_NOTIF_HANDLED;
|
|
|
|
+ }
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_vhf_probe(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct vhf_drvdata *drvdata;
|
|
|
|
+ struct hid_device *hid;
|
|
|
|
+ int status;
|
|
|
|
+
|
|
|
|
+ // add device link to EC
|
|
|
|
+ status = surface_sam_ssh_consumer_register(&pdev->dev);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return status == -ENXIO ? -EPROBE_DEFER : status;
|
|
|
|
+
|
|
|
|
+ drvdata = kzalloc(sizeof(struct vhf_drvdata), GFP_KERNEL);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (!drvdata)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ return -ENOMEM;
|
|
|
|
+
|
|
|
|
+ hid = vhf_create_hid_device(pdev);
|
|
|
|
+ if (IS_ERR(hid)) {
|
|
|
|
+ status = PTR_ERR(hid);
|
|
|
|
+ goto err_probe_hid;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ status = hid_add_device(hid);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_add_hid;
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ drvdata->dev = pdev;
|
|
|
|
+ drvdata->hid = hid;
|
|
|
|
+
|
|
|
|
+ drvdata->notif.base.priority = 1;
|
|
|
|
+ drvdata->notif.base.fn = vhf_event_handler;
|
|
|
|
+ drvdata->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
|
|
|
|
+ drvdata->notif.event.id.target_category = SSAM_SSH_TC_KBD;
|
|
|
|
+ drvdata->notif.event.id.instance = 0;
|
|
|
|
+ drvdata->notif.event.flags = 0;
|
2019-12-10 00:26:31 +00:00
|
|
|
+
|
|
|
|
+ platform_set_drvdata(pdev, drvdata);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ status = surface_sam_ssh_notifier_register(&drvdata->notif);
|
2020-05-16 22:20:04 +00:00
|
|
|
+ if (status)
|
2019-12-10 00:26:31 +00:00
|
|
|
+ goto err_add_hid;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+
|
|
|
|
+err_add_hid:
|
|
|
|
+ hid_destroy_device(hid);
|
|
|
|
+ platform_set_drvdata(pdev, NULL);
|
|
|
|
+err_probe_hid:
|
|
|
|
+ kfree(drvdata);
|
|
|
|
+ return status;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int surface_sam_vhf_remove(struct platform_device *pdev)
|
|
|
|
+{
|
|
|
|
+ struct vhf_drvdata *drvdata = platform_get_drvdata(pdev);
|
|
|
|
+
|
2020-07-02 21:19:52 +00:00
|
|
|
+ surface_sam_ssh_notifier_unregister(&drvdata->notif);
|
|
|
|
+ hid_destroy_device(drvdata->hid);
|
2019-12-10 00:26:31 +00:00
|
|
|
+ kfree(drvdata);
|
|
|
|
+
|
|
|
|
+ platform_set_drvdata(pdev, NULL);
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static const struct acpi_device_id surface_sam_vhf_match[] = {
|
|
|
|
+ { "MSHW0096" },
|
|
|
|
+ { },
|
|
|
|
+};
|
|
|
|
+MODULE_DEVICE_TABLE(acpi, surface_sam_vhf_match);
|
|
|
|
+
|
|
|
|
+static struct platform_driver surface_sam_vhf = {
|
|
|
|
+ .probe = surface_sam_vhf_probe,
|
|
|
|
+ .remove = surface_sam_vhf_remove,
|
|
|
|
+ .driver = {
|
|
|
|
+ .name = "surface_sam_vhf",
|
2020-07-02 21:19:52 +00:00
|
|
|
+ .acpi_match_table = surface_sam_vhf_match,
|
2019-12-10 00:26:31 +00:00
|
|
|
+ .probe_type = PROBE_PREFER_ASYNCHRONOUS,
|
|
|
|
+ },
|
|
|
|
+};
|
|
|
|
+module_platform_driver(surface_sam_vhf);
|
|
|
|
+
|
|
|
|
+MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
|
|
|
|
+MODULE_DESCRIPTION("Virtual HID Framework Driver for 5th Generation Surface Devices");
|
2020-07-02 21:19:52 +00:00
|
|
|
+MODULE_LICENSE("GPL");
|
2019-12-10 00:26:31 +00:00
|
|
|
diff --git a/drivers/tty/serdev/core.c b/drivers/tty/serdev/core.c
|
2020-07-24 09:23:59 +00:00
|
|
|
index a9719858c950..ce5309d00280 100644
|
2019-12-10 00:26:31 +00:00
|
|
|
--- a/drivers/tty/serdev/core.c
|
|
|
|
+++ b/drivers/tty/serdev/core.c
|
|
|
|
@@ -552,16 +552,97 @@ static int of_serdev_register_devices(struct serdev_controller *ctrl)
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_ACPI
|
|
|
|
+
|
|
|
|
+#define SERDEV_ACPI_MAX_SCAN_DEPTH 32
|
|
|
|
+
|
|
|
|
+struct acpi_serdev_lookup {
|
|
|
|
+ acpi_handle device_handle;
|
|
|
|
+ acpi_handle controller_handle;
|
|
|
|
+ int n;
|
|
|
|
+ int index;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+static int acpi_serdev_parse_resource(struct acpi_resource *ares, void *data)
|
|
|
|
+{
|
|
|
|
+ struct acpi_serdev_lookup *lookup = data;
|
|
|
|
+ struct acpi_resource_uart_serialbus *sb;
|
|
|
|
+ acpi_status status;
|
|
|
|
+
|
|
|
|
+ if (ares->type != ACPI_RESOURCE_TYPE_SERIAL_BUS)
|
|
|
|
+ return 1;
|
|
|
|
+
|
|
|
|
+ if (ares->data.common_serial_bus.type != ACPI_RESOURCE_SERIAL_TYPE_UART)
|
|
|
|
+ return 1;
|
|
|
|
+
|
|
|
|
+ if (lookup->index != -1 && lookup->n++ != lookup->index)
|
|
|
|
+ return 1;
|
|
|
|
+
|
|
|
|
+ sb = &ares->data.uart_serial_bus;
|
|
|
|
+
|
|
|
|
+ status = acpi_get_handle(lookup->device_handle,
|
|
|
|
+ sb->resource_source.string_ptr,
|
|
|
|
+ &lookup->controller_handle);
|
|
|
|
+ if (ACPI_FAILURE(status))
|
|
|
|
+ return 1;
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ * NOTE: Ideally, we would also want to retreive other properties here,
|
|
|
|
+ * once setting them before opening the device is supported by serdev.
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ return 1;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int acpi_serdev_do_lookup(struct acpi_device *adev,
|
|
|
|
+ struct acpi_serdev_lookup *lookup)
|
|
|
|
+{
|
|
|
|
+ struct list_head resource_list;
|
|
|
|
+ int ret;
|
|
|
|
+
|
|
|
|
+ lookup->device_handle = acpi_device_handle(adev);
|
|
|
|
+ lookup->controller_handle = NULL;
|
|
|
|
+ lookup->n = 0;
|
|
|
|
+
|
|
|
|
+ INIT_LIST_HEAD(&resource_list);
|
|
|
|
+ ret = acpi_dev_get_resources(adev, &resource_list,
|
|
|
|
+ acpi_serdev_parse_resource, lookup);
|
|
|
|
+ acpi_dev_free_resource_list(&resource_list);
|
|
|
|
+
|
|
|
|
+ if (ret < 0)
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static int acpi_serdev_check_resources(struct serdev_controller *ctrl,
|
|
|
|
+ struct acpi_device *adev)
|
|
|
|
+{
|
|
|
|
+ struct acpi_serdev_lookup lookup;
|
|
|
|
+ int ret;
|
|
|
|
+
|
|
|
|
+ if (acpi_bus_get_status(adev) || !adev->status.present)
|
|
|
|
+ return -EINVAL;
|
|
|
|
+
|
|
|
|
+ /* Look for UARTSerialBusV2 resource */
|
|
|
|
+ lookup.index = -1; // we only care for the last device
|
|
|
|
+
|
|
|
|
+ ret = acpi_serdev_do_lookup(adev, &lookup);
|
|
|
|
+ if (ret)
|
|
|
|
+ return ret;
|
|
|
|
+
|
|
|
|
+ /* Make sure controller and ResourceSource handle match */
|
|
|
|
+ if (ACPI_HANDLE(ctrl->dev.parent) != lookup.controller_handle)
|
|
|
|
+ return -ENODEV;
|
|
|
|
+
|
|
|
|
+ return 0;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
static acpi_status acpi_serdev_register_device(struct serdev_controller *ctrl,
|
|
|
|
- struct acpi_device *adev)
|
|
|
|
+ struct acpi_device *adev)
|
|
|
|
{
|
|
|
|
- struct serdev_device *serdev = NULL;
|
|
|
|
+ struct serdev_device *serdev;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
- if (acpi_bus_get_status(adev) || !adev->status.present ||
|
|
|
|
- acpi_device_enumerated(adev))
|
|
|
|
- return AE_OK;
|
|
|
|
-
|
|
|
|
serdev = serdev_device_alloc(ctrl);
|
|
|
|
if (!serdev) {
|
|
|
|
dev_err(&ctrl->dev, "failed to allocate serdev device for %s\n",
|
2020-01-28 22:12:08 +00:00
|
|
|
@@ -589,7 +670,7 @@ static const struct acpi_device_id serdev_acpi_devices_blacklist[] = {
|
|
|
|
};
|
2019-12-10 00:26:31 +00:00
|
|
|
|
|
|
|
static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
|
|
|
|
- void *data, void **return_value)
|
|
|
|
+ void *data, void **return_value)
|
|
|
|
{
|
|
|
|
struct serdev_controller *ctrl = data;
|
|
|
|
struct acpi_device *adev;
|
2020-01-28 22:12:08 +00:00
|
|
|
@@ -597,26 +678,32 @@ static acpi_status acpi_serdev_add_device(acpi_handle handle, u32 level,
|
2019-12-10 00:26:31 +00:00
|
|
|
if (acpi_bus_get_device(handle, &adev))
|
|
|
|
return AE_OK;
|
|
|
|
|
|
|
|
+ if (acpi_device_enumerated(adev))
|
|
|
|
+ return AE_OK;
|
|
|
|
+
|
2020-01-28 22:12:08 +00:00
|
|
|
/* Skip if black listed */
|
|
|
|
if (!acpi_match_device_ids(adev, serdev_acpi_devices_blacklist))
|
|
|
|
return AE_OK;
|
|
|
|
|
2019-12-10 00:26:31 +00:00
|
|
|
+ if (acpi_serdev_check_resources(ctrl, adev))
|
|
|
|
+ return AE_OK;
|
|
|
|
+
|
|
|
|
return acpi_serdev_register_device(ctrl, adev);
|
|
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
static int acpi_serdev_register_devices(struct serdev_controller *ctrl)
|
|
|
|
{
|
|
|
|
acpi_status status;
|
|
|
|
- acpi_handle handle;
|
|
|
|
|
|
|
|
- handle = ACPI_HANDLE(ctrl->dev.parent);
|
|
|
|
- if (!handle)
|
|
|
|
+ if (!has_acpi_companion(ctrl->dev.parent))
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
- status = acpi_walk_namespace(ACPI_TYPE_DEVICE, handle, 1,
|
|
|
|
+ status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
|
|
|
|
+ SERDEV_ACPI_MAX_SCAN_DEPTH,
|
|
|
|
acpi_serdev_add_device, NULL, ctrl, NULL);
|
|
|
|
if (ACPI_FAILURE(status))
|
|
|
|
- dev_dbg(&ctrl->dev, "failed to enumerate serdev slaves\n");
|
|
|
|
+ dev_warn(&ctrl->dev, "failed to enumerate serdev slaves\n");
|
|
|
|
|
|
|
|
if (!ctrl->serdev)
|
|
|
|
return -ENODEV;
|
|
|
|
--
|
2020-07-02 21:19:52 +00:00
|
|
|
2.27.0
|
2019-12-10 00:26:31 +00:00
|
|
|
|