162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Surface DTX (clipboard detachment system driver) user-space interface.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Definitions, structs, and IOCTLs for the /dev/surface/dtx misc device. This
662306a36Sopenharmony_ci * device allows user-space to control the clipboard detachment process on
762306a36Sopenharmony_ci * Surface Book series devices.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Copyright (C) 2020-2021 Maximilian Luz <luzmaximilian@gmail.com>
1062306a36Sopenharmony_ci */
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#ifndef _UAPI_LINUX_SURFACE_AGGREGATOR_DTX_H
1362306a36Sopenharmony_ci#define _UAPI_LINUX_SURFACE_AGGREGATOR_DTX_H
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include <linux/ioctl.h>
1662306a36Sopenharmony_ci#include <linux/types.h>
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci/* Status/error categories */
1962306a36Sopenharmony_ci#define SDTX_CATEGORY_STATUS		0x0000
2062306a36Sopenharmony_ci#define SDTX_CATEGORY_RUNTIME_ERROR	0x1000
2162306a36Sopenharmony_ci#define SDTX_CATEGORY_HARDWARE_ERROR	0x2000
2262306a36Sopenharmony_ci#define SDTX_CATEGORY_UNKNOWN		0xf000
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define SDTX_CATEGORY_MASK		0xf000
2562306a36Sopenharmony_ci#define SDTX_CATEGORY(value)		((value) & SDTX_CATEGORY_MASK)
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define SDTX_STATUS(code)		((code) | SDTX_CATEGORY_STATUS)
2862306a36Sopenharmony_ci#define SDTX_ERR_RT(code)		((code) | SDTX_CATEGORY_RUNTIME_ERROR)
2962306a36Sopenharmony_ci#define SDTX_ERR_HW(code)		((code) | SDTX_CATEGORY_HARDWARE_ERROR)
3062306a36Sopenharmony_ci#define SDTX_UNKNOWN(code)		((code) | SDTX_CATEGORY_UNKNOWN)
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define SDTX_SUCCESS(value)		(SDTX_CATEGORY(value) == SDTX_CATEGORY_STATUS)
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci/* Latch status values */
3562306a36Sopenharmony_ci#define SDTX_LATCH_CLOSED		SDTX_STATUS(0x00)
3662306a36Sopenharmony_ci#define SDTX_LATCH_OPENED		SDTX_STATUS(0x01)
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/* Base state values */
3962306a36Sopenharmony_ci#define SDTX_BASE_DETACHED		SDTX_STATUS(0x00)
4062306a36Sopenharmony_ci#define SDTX_BASE_ATTACHED		SDTX_STATUS(0x01)
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci/* Runtime errors (non-critical) */
4362306a36Sopenharmony_ci#define SDTX_DETACH_NOT_FEASIBLE	SDTX_ERR_RT(0x01)
4462306a36Sopenharmony_ci#define SDTX_DETACH_TIMEDOUT		SDTX_ERR_RT(0x02)
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci/* Hardware errors (critical) */
4762306a36Sopenharmony_ci#define SDTX_ERR_FAILED_TO_OPEN		SDTX_ERR_HW(0x01)
4862306a36Sopenharmony_ci#define SDTX_ERR_FAILED_TO_REMAIN_OPEN	SDTX_ERR_HW(0x02)
4962306a36Sopenharmony_ci#define SDTX_ERR_FAILED_TO_CLOSE	SDTX_ERR_HW(0x03)
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* Base types */
5262306a36Sopenharmony_ci#define SDTX_DEVICE_TYPE_HID		0x0100
5362306a36Sopenharmony_ci#define SDTX_DEVICE_TYPE_SSH		0x0200
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define SDTX_DEVICE_TYPE_MASK		0x0f00
5662306a36Sopenharmony_ci#define SDTX_DEVICE_TYPE(value)		((value) & SDTX_DEVICE_TYPE_MASK)
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#define SDTX_BASE_TYPE_HID(id)		((id) | SDTX_DEVICE_TYPE_HID)
5962306a36Sopenharmony_ci#define SDTX_BASE_TYPE_SSH(id)		((id) | SDTX_DEVICE_TYPE_SSH)
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci/**
6262306a36Sopenharmony_ci * enum sdtx_device_mode - Mode describing how (and if) the clipboard is
6362306a36Sopenharmony_ci * attached to the base of the device.
6462306a36Sopenharmony_ci * @SDTX_DEVICE_MODE_TABLET: The clipboard is detached from the base and the
6562306a36Sopenharmony_ci *                           device operates as tablet.
6662306a36Sopenharmony_ci * @SDTX_DEVICE_MODE_LAPTOP: The clipboard is attached normally to the base
6762306a36Sopenharmony_ci *                           and the device operates as laptop.
6862306a36Sopenharmony_ci * @SDTX_DEVICE_MODE_STUDIO: The clipboard is attached to the base in reverse.
6962306a36Sopenharmony_ci *                           The device operates as tablet with keyboard and
7062306a36Sopenharmony_ci *                           touchpad deactivated, however, the base battery
7162306a36Sopenharmony_ci *                           and, if present in the specific device model, dGPU
7262306a36Sopenharmony_ci *                           are available to the system.
7362306a36Sopenharmony_ci */
7462306a36Sopenharmony_cienum sdtx_device_mode {
7562306a36Sopenharmony_ci	SDTX_DEVICE_MODE_TABLET		= 0x00,
7662306a36Sopenharmony_ci	SDTX_DEVICE_MODE_LAPTOP		= 0x01,
7762306a36Sopenharmony_ci	SDTX_DEVICE_MODE_STUDIO		= 0x02,
7862306a36Sopenharmony_ci};
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci/**
8162306a36Sopenharmony_ci * struct sdtx_event - Event provided by reading from the DTX device file.
8262306a36Sopenharmony_ci * @length: Length of the event payload, in bytes.
8362306a36Sopenharmony_ci * @code:   Event code, detailing what type of event this is.
8462306a36Sopenharmony_ci * @data:   Payload of the event, containing @length bytes.
8562306a36Sopenharmony_ci *
8662306a36Sopenharmony_ci * See &enum sdtx_event_code for currently valid event codes.
8762306a36Sopenharmony_ci */
8862306a36Sopenharmony_cistruct sdtx_event {
8962306a36Sopenharmony_ci	__u16 length;
9062306a36Sopenharmony_ci	__u16 code;
9162306a36Sopenharmony_ci	__u8 data[];
9262306a36Sopenharmony_ci} __attribute__((__packed__));
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci/**
9562306a36Sopenharmony_ci * enum sdtx_event_code - Code describing the type of an event.
9662306a36Sopenharmony_ci * @SDTX_EVENT_REQUEST:         Detachment request event type.
9762306a36Sopenharmony_ci * @SDTX_EVENT_CANCEL:          Cancel detachment process event type.
9862306a36Sopenharmony_ci * @SDTX_EVENT_BASE_CONNECTION: Base/clipboard connection change event type.
9962306a36Sopenharmony_ci * @SDTX_EVENT_LATCH_STATUS:    Latch status change event type.
10062306a36Sopenharmony_ci * @SDTX_EVENT_DEVICE_MODE:     Device mode change event type.
10162306a36Sopenharmony_ci *
10262306a36Sopenharmony_ci * Used in &struct sdtx_event to describe the type of the event. Further event
10362306a36Sopenharmony_ci * codes are reserved for future use. Any event parser should be able to
10462306a36Sopenharmony_ci * gracefully handle unknown events, i.e. by simply skipping them.
10562306a36Sopenharmony_ci *
10662306a36Sopenharmony_ci * Consult the DTX user-space interface documentation for details regarding
10762306a36Sopenharmony_ci * the individual event types.
10862306a36Sopenharmony_ci */
10962306a36Sopenharmony_cienum sdtx_event_code {
11062306a36Sopenharmony_ci	SDTX_EVENT_REQUEST		= 1,
11162306a36Sopenharmony_ci	SDTX_EVENT_CANCEL		= 2,
11262306a36Sopenharmony_ci	SDTX_EVENT_BASE_CONNECTION	= 3,
11362306a36Sopenharmony_ci	SDTX_EVENT_LATCH_STATUS		= 4,
11462306a36Sopenharmony_ci	SDTX_EVENT_DEVICE_MODE		= 5,
11562306a36Sopenharmony_ci};
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci/**
11862306a36Sopenharmony_ci * struct sdtx_base_info - Describes if and what type of base is connected.
11962306a36Sopenharmony_ci * @state:   The state of the connection. Valid values are %SDTX_BASE_DETACHED,
12062306a36Sopenharmony_ci *           %SDTX_BASE_ATTACHED, and %SDTX_DETACH_NOT_FEASIBLE (in case a base
12162306a36Sopenharmony_ci *           is attached but low clipboard battery prevents detachment). Other
12262306a36Sopenharmony_ci *           values are currently reserved.
12362306a36Sopenharmony_ci * @base_id: The type of base connected. Zero if no base is connected.
12462306a36Sopenharmony_ci */
12562306a36Sopenharmony_cistruct sdtx_base_info {
12662306a36Sopenharmony_ci	__u16 state;
12762306a36Sopenharmony_ci	__u16 base_id;
12862306a36Sopenharmony_ci} __attribute__((__packed__));
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci/* IOCTLs */
13162306a36Sopenharmony_ci#define SDTX_IOCTL_EVENTS_ENABLE	_IO(0xa5, 0x21)
13262306a36Sopenharmony_ci#define SDTX_IOCTL_EVENTS_DISABLE	_IO(0xa5, 0x22)
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#define SDTX_IOCTL_LATCH_LOCK		_IO(0xa5, 0x23)
13562306a36Sopenharmony_ci#define SDTX_IOCTL_LATCH_UNLOCK		_IO(0xa5, 0x24)
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci#define SDTX_IOCTL_LATCH_REQUEST	_IO(0xa5, 0x25)
13862306a36Sopenharmony_ci#define SDTX_IOCTL_LATCH_CONFIRM	_IO(0xa5, 0x26)
13962306a36Sopenharmony_ci#define SDTX_IOCTL_LATCH_HEARTBEAT	_IO(0xa5, 0x27)
14062306a36Sopenharmony_ci#define SDTX_IOCTL_LATCH_CANCEL		_IO(0xa5, 0x28)
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci#define SDTX_IOCTL_GET_BASE_INFO	_IOR(0xa5, 0x29, struct sdtx_base_info)
14362306a36Sopenharmony_ci#define SDTX_IOCTL_GET_DEVICE_MODE	_IOR(0xa5, 0x2a, __u16)
14462306a36Sopenharmony_ci#define SDTX_IOCTL_GET_LATCH_STATUS	_IOR(0xa5, 0x2b, __u16)
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#endif /* _UAPI_LINUX_SURFACE_AGGREGATOR_DTX_H */
147