# surface_aggregator: clients

---

# Client Driver Documentation

_Peios Kernel / Hardware and drivers / Driver development / surface_aggregator: clients_

> This is the documentation for client drivers themselves. Refer to Documentation/driver-api/surface_aggregator/client.rst for documentation on how to write client drivers.

<a id="rst-client-driver-documentation"></a>

<!-- SPDX-License-Identifier: GPL-2.0+ -->

This is the documentation for client drivers themselves. Refer to [Documentation/driver-api/surface_aggregator/client.rst](/kernel/hardware/driver-api/surface-aggregator/client.md) for documentation on how to write client drivers.

- [User-Space EC Interface (cdev)](</kernel/hardware/driver-api/surface-aggregator-clients/cdev.md>)
- [User-Space DTX (Clipboard Detachment System) Interface](</kernel/hardware/driver-api/surface-aggregator-clients/dtx.md>)
- [Surface ACPI Notify](</kernel/hardware/driver-api/surface-aggregator-clients/san.md>)

---

# User-Space EC Interface (cdev)

_Peios Kernel / Hardware and drivers / Driver development / surface_aggregator: clients_

> The surface_aggregator_cdev module provides a misc-device for the SSAM controller to allow for a (more or less) direct connection from user-space to the SAM EC. It is intended to be used for development and debugging, and therefore should…

<a id="rst-user-space-ec-interface-cdev"></a>

<!-- SPDX-License-Identifier: GPL-2.0+ -->

The `surface_aggregator_cdev` module provides a misc-device for the SSAM controller to allow for a (more or less) direct connection from user-space to the SAM EC. It is intended to be used for development and debugging, and therefore should not be used or relied upon in any other way. Note that this module is not loaded automatically, but instead must be loaded manually.

The provided interface is accessible through the `/dev/surface/aggregator` device-file. All functionality of this interface is provided via IOCTLs. These IOCTLs and their respective input/output parameter structs are defined in `include/uapi/linux/surface_aggregator/cdev.h`.

A small python library and scripts for accessing this interface can be found at [https://github.com/linux-surface/surface-aggregator-module/tree/master/scripts/ssam](<https://github.com/linux-surface/surface-aggregator-module/tree/master/scripts/ssam>).

<a id="rst-contents"></a>



<a id="rst-receiving-events"></a>

## Receiving Events

Events can be received by reading from the device-file. The are represented by the `struct ssam_cdev_event <ssam_cdev_event>` datatype.

Before events are available to be read, however, the desired notifiers must be registered via the `SSAM_CDEV_NOTIF_REGISTER` IOCTL. Notifiers are, in essence, callbacks, called when the EC sends an event. They are, in this interface, associated with a specific target category and device-file-instance. They forward any event of this category to the buffer of the corresponding instance, from which it can then be read.

Notifiers themselves do not enable events on the EC. Thus, it may additionally be necessary to enable events via the `SSAM_CDEV_EVENT_ENABLE` IOCTL. While notifiers work per-client (i.e. per-device-file-instance), events are enabled globally, for the EC and all of its clients (regardless of userspace or non-userspace). The `SSAM_CDEV_EVENT_ENABLE` and `SSAM_CDEV_EVENT_DISABLE` IOCTLs take care of reference counting the events, such that an event is enabled as long as there is a client that has requested it.

Note that enabled events are not automatically disabled once the client instance is closed. Therefore any client process (or group of processes) should balance their event enable calls with the corresponding event disable calls. It is, however, perfectly valid to enable and disable events on different client instances. For example, it is valid to set up notifiers and read events on client instance `A`, enable those events on instance `B` (note that these will also be received by A since events are enabled/disabled globally), and after no more events are desired, disable the previously enabled events via instance `C`.

<a id="rst-controller-ioctls"></a>

## Controller IOCTLs

The following IOCTLs are provided:


<table>
<caption>Controller IOCTLs</caption>
<thead>
<tr>
<th>


Type

</th>
<th>


Number

</th>
<th>


Direction

</th>
<th>


Name

</th>
<th>


Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`0xA5`

</td>
<td>


`1`

</td>
<td>


`WR`

</td>
<td>


`REQUEST`

</td>
<td>


Perform synchronous SAM request.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`2`

</td>
<td>


`W`

</td>
<td>


`NOTIF_REGISTER`

</td>
<td>


Register event notifier.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`3`

</td>
<td>


`W`

</td>
<td>


`NOTIF_UNREGISTER`

</td>
<td>


Unregister event notifier.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`4`

</td>
<td>


`W`

</td>
<td>


`EVENT_ENABLE`

</td>
<td>


Enable event source.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`5`

</td>
<td>


`W`

</td>
<td>


`EVENT_DISABLE`

</td>
<td>


Disable event source.

</td>
</tr>
</tbody>
</table>

<a id="rst-ssam-cdev-request"></a>

### `SSAM_CDEV_REQUEST`

Defined as `_IOWR(0xA5, 1, struct ssam_cdev_request)`.

Executes a synchronous SAM request. The request specification is passed in as argument of type `struct ssam_cdev_request <ssam_cdev_request>`, which is then written to/modified by the IOCTL to return status and result of the request.

Request payload data must be allocated separately and is passed in via the `payload.data` and `payload.length` members. If a response is required, the response buffer must be allocated by the caller and passed in via the `response.data` member. The `response.length` member must be set to the capacity of this buffer, or if no response is required, zero. Upon completion of the request, the call will write the response to the response buffer (if its capacity allows it) and overwrite the length field with the actual size of the response, in bytes.

Additionally, if the request has a response, this must be indicated via the request flags, as is done with in-kernel requests. Request flags can be set via the `flags` member and the values correspond to the values found in `enum ssam_cdev_request_flags <ssam_cdev_request_flags>`.

Finally, the status of the request itself is returned in the `status` member (a negative errno value indicating failure). Note that failure indication of the IOCTL is separated from failure indication of the request: The IOCTL returns a negative status code if anything failed during setup of the request (`-EFAULT`) or if the provided argument or any of its fields are invalid (`-EINVAL`). In this case, the status value of the request argument may be set, providing more detail on what went wrong (e.g. `-ENOMEM` for out-of-memory), but this value may also be zero. The IOCTL will return with a zero status code in case the request has been set up, submitted, and completed (i.e. handed back to user-space) successfully from inside the IOCTL, but the request `status` member may still be negative in case the actual execution of the request failed after it has been submitted.

A full definition of the argument struct is provided below.

<a id="rst-ssam-cdev-notif-register"></a>

### `SSAM_CDEV_NOTIF_REGISTER`

Defined as `_IOW(0xA5, 2, struct ssam_cdev_notifier_desc)`.

Register a notifier for the event target category specified in the given notifier description with the specified priority. Notifiers registration is required to receive events, but does not enable events themselves. After a notifier for a specific target category has been registered, all events of that category will be forwarded to the userspace client and can then be read from the device file instance. Note that events may have to be enabled, e.g. via the `SSAM_CDEV_EVENT_ENABLE` IOCTL, before the EC will send them.

Only one notifier can be registered per target category and client instance. If a notifier has already been registered, this IOCTL will fail with `-EEXIST`.

Notifiers will automatically be removed when the device file instance is closed.

<a id="rst-ssam-cdev-notif-unregister"></a>

### `SSAM_CDEV_NOTIF_UNREGISTER`

Defined as `_IOW(0xA5, 3, struct ssam_cdev_notifier_desc)`.

Unregisters the notifier associated with the specified target category. The priority field will be ignored by this IOCTL. If no notifier has been registered for this client instance and the given category, this IOCTL will fail with `-ENOENT`.

<a id="rst-ssam-cdev-event-enable"></a>

### `SSAM_CDEV_EVENT_ENABLE`

Defined as `_IOW(0xA5, 4, struct ssam_cdev_event_desc)`.

Enable the event associated with the given event descriptor.

Note that this call will not register a notifier itself, it will only enable events on the controller. If you want to receive events by reading from the device file, you will need to register the corresponding notifier(s) on that instance.

Events are not automatically disabled when the device file is closed. This must be done manually, via a call to the `SSAM_CDEV_EVENT_DISABLE` IOCTL.

<a id="rst-ssam-cdev-event-disable"></a>

### `SSAM_CDEV_EVENT_DISABLE`

Defined as `_IOW(0xA5, 5, struct ssam_cdev_event_desc)`.

Disable the event associated with the given event descriptor.

Note that this will not unregister any notifiers. Events may still be received and forwarded to user-space after this call. The only safe way of stopping events from being received is unregistering all previously registered notifiers.

<a id="rst-structures-and-enums"></a>

## Structures and Enums


#### <a id="c-enum-ssam_cdev_request_flags"></a>`enum ssam_cdev_request_flags`

Request flags for SSAM cdev request IOCTL.

**Constants**

- `SSAM_CDEV_REQUEST_HAS_RESPONSE`: Specifies that the request expects a response. If not set, the request
  will be directly completed after its underlying packet has been
  transmitted. If set, the request transport system waits for a response
  of the request.
- `SSAM_CDEV_REQUEST_UNSEQUENCED`: Specifies that the request should be transmitted via an unsequenced
  packet. If set, the request must not have a response, meaning that this
  flag and the `SSAM_CDEV_REQUEST_HAS_RESPONSE` flag are mutually
  exclusive.

#### <a id="c-struct-ssam_cdev_request"></a>`struct ssam_cdev_request`

Controller request IOCTL argument.

**Definition**

```c
struct ssam_cdev_request {
    __u8 target_category;
    __u8 target_id;
    __u8 command_id;
    __u8 instance_id;
    __u16 flags;
    __s16 status;
    struct {
        __u64 data;
        __u16 length;
        __u8 __pad[6];
    } payload;
    struct {
        __u64 data;
        __u16 length;
        __u8 __pad[6];
    } response;
};
```

**Members**

- `target_category`: Target category of the SAM request.
- `target_id`: Target ID of the SAM request.
- `command_id`: Command ID of the SAM request.
- `instance_id`: Instance ID of the SAM request.
- `flags`: Request flags (see [`enum ssam_cdev_request_flags`](#c-enum-ssam_cdev_request_flags)).
- `status`: Request status (output).
- `payload`: Request payload (input data).
- `payload.data`: Pointer to request payload data.
- `payload.length`: Length of request payload data (in bytes).
- `response`: Request response (output data).
- `response.data`: Pointer to response buffer.
- `response.length`: On input: Capacity of response buffer (in bytes).
  On output: Length of request response (number of bytes
  in the buffer that are actually used).

#### <a id="c-struct-ssam_cdev_notifier_desc"></a>`struct ssam_cdev_notifier_desc`

Notifier descriptor.

**Definition**

```c
struct ssam_cdev_notifier_desc {
    __s32 priority;
    __u8 target_category;
};
```

**Members**

- `priority`: Priority value determining the order in which notifier
  callbacks will be called. A higher value means higher
  priority, i.e. the associated callback will be executed
  earlier than other (lower priority) callbacks.
- `target_category`: The event target category for which this notifier should
  receive events.

**Description**

Specifies the notifier that should be registered or unregistered,
specifically with which priority and for which target category of events.

#### <a id="c-struct-ssam_cdev_event_desc"></a>`struct ssam_cdev_event_desc`

Event descriptor.

**Definition**

```c
struct ssam_cdev_event_desc {
    struct {
        __u8 target_category;
        __u8 target_id;
        __u8 cid_enable;
        __u8 cid_disable;
    } reg;
    struct {
        __u8 target_category;
        __u8 instance;
    } id;
    __u8 flags;
};
```

**Members**

- `reg`: Registry via which the event will be enabled/disabled.
- `reg.target_category`: Target category for the event registry requests.
- `reg.target_id`: Target ID for the event registry requests.
- `reg.cid_enable`: Command ID for the event-enable request.
- `reg.cid_disable`: Command ID for the event-disable request.
- `id`: ID specifying the event.
- `id.target_category`: Target category of the event source.
- `id.instance`: Instance ID of the event source.
- `flags`: Flags used for enabling the event.

**Description**

Specifies which event should be enabled/disabled and how to do that.

#### <a id="c-struct-ssam_cdev_event"></a>`struct ssam_cdev_event`

SSAM event sent by the EC.

**Definition**

```c
struct ssam_cdev_event {
    __u8 target_category;
    __u8 target_id;
    __u8 command_id;
    __u8 instance_id;
    __u16 length;
    __u8 data[];
};
```

**Members**

- `target_category`: Target category of the event source. See `enum ssam_ssh_tc`.
- `target_id`: Target ID of the event source.
- `command_id`: Command ID of the event.
- `instance_id`: Instance ID of the event source.
- `length`: Length of the event payload in bytes.
- `data`: Event payload data.

---

# User-Space DTX (Clipboard Detachment System) Interface

_Peios Kernel / Hardware and drivers / Driver development / surface_aggregator: clients_

> The surface_dtx driver is responsible for proper clipboard detachment and re-attachment handling. To this end, it provides the /dev/surface/dtx device file, through which it can interface with a user-space daemon. This daemon is then…

<a id="rst-user-space-dtx-clipboard-detachment-system-interface"></a>

<!-- SPDX-License-Identifier: GPL-2.0+ -->

The `surface_dtx` driver is responsible for proper clipboard detachment and re-attachment handling. To this end, it provides the `/dev/surface/dtx` device file, through which it can interface with a user-space daemon. This daemon is then ultimately responsible for determining and taking necessary actions, such as unmounting devices attached to the base, unloading/reloading the graphics-driver, user-notifications, etc.

There are two basic communication principles used in this driver: Commands (in other parts of the documentation also referred to as requests) and events. Commands are sent to the EC and may have a different implications in different contexts. Events are sent by the EC upon some internal state change. Commands are always driver-initiated, whereas events are always initiated by the EC.

<a id="rst-contents"></a>



<a id="rst-nomenclature"></a>

## Nomenclature

- **Clipboard:** The detachable upper part of the Surface Book, housing the screen and CPU.

- **Base:** The lower part of the Surface Book from which the clipboard can be detached, optionally (model dependent) housing the discrete GPU (dGPU).

- **Latch:** The mechanism keeping the clipboard attached to the base in normal operation and allowing it to be detached when requested.

- **Silently ignored commands:** The command is accepted by the EC as a valid command and acknowledged (following the standard communication protocol), but the EC does not act upon it, i.e. ignores it.e upper part of the

<a id="rst-detachment-process"></a>

## Detachment Process

Warning: This part of the documentation is based on reverse engineering and testing and thus may contain errors or be incomplete.

<a id="rst-latch-states"></a>

### Latch States

The latch mechanism has two major states: *open* and *closed*. In the *closed* state (default), the clipboard is secured to the base, whereas in the *open* state, the clipboard can be removed by a user.

The latch can additionally be locked and, correspondingly, unlocked, which can influence the detachment procedure. Specifically, this locking mechanism is intended to prevent the dGPU, positioned in the base of the device, from being hot-unplugged while in use. More details can be found in the documentation for the detachment procedure below. By default, the latch is unlocked.

<a id="rst-detachment-procedure"></a>

### Detachment Procedure

Note that the detachment process is governed fully by the EC. The `surface_dtx` driver only relays events from the EC to user-space and commands from user-space to the EC, i.e. it does not influence this process.

The detachment process is started with the user pressing the *detach* button on the base of the device or executing the `SDTX_IOCTL_LATCH_REQUEST` IOCTL. Following that:

1. The EC turns on the indicator led on the detach-button, sends a *detach-request* event (`SDTX_EVENT_REQUEST`), and awaits further instructions/commands. In case the latch is unlocked, the led will flash green. If the latch has been locked, the led will be solid red

2. The event is, via the `surface_dtx` driver, relayed to user-space, where an appropriate user-space daemon can handle it and send instructions back to the EC via IOCTLs provided by this driver.

3. The EC waits for instructions from user-space and acts according to them. If the EC does not receive any instructions in a given period, it will time out and continue as follows:

   - If the latch is unlocked, the EC will open the latch and the clipboard can be detached from the base. This is the exact behavior as without this driver or any user-space daemon. See the `SDTX_IOCTL_LATCH_CONFIRM` description below for more details on the follow-up behavior of the EC.

   - If the latch is locked, the EC will *not* open the latch, meaning the clipboard cannot be detached from the base. Furthermore, the EC sends an cancel event (`SDTX_EVENT_CANCEL`) detailing this with the cancel reason `SDTX_DETACH_TIMEDOUT` (see [Events](<#rst-events-1>) for details).

Valid responses by a user-space daemon to a detachment request event are:

- Execute `SDTX_IOCTL_LATCH_REQUEST`. This will immediately abort the detachment process. Furthermore, the EC will send a detach-request event, similar to the user pressing the detach-button to cancel said process (see below).

- Execute `SDTX_IOCTL_LATCH_CONFIRM`. This will cause the EC to open the latch, after which the user can separate clipboard and base.

  As this changes the latch state, a *latch-status* event (`SDTX_EVENT_LATCH_STATUS`) will be sent once the latch has been opened successfully. If the EC fails to open the latch, e.g. due to hardware error or low battery, a latch-cancel event (`SDTX_EVENT_CANCEL`) will be sent with the cancel reason indicating the specific failure.

  If the latch is currently locked, the latch will automatically be unlocked before it is opened.

- Execute `SDTX_IOCTL_LATCH_HEARTBEAT`. This will reset the internal timeout. No other actions will be performed, i.e. the detachment process will neither be completed nor canceled, and the EC will still be waiting for further responses.

- Execute `SDTX_IOCTL_LATCH_CANCEL`. This will abort the detachment process, similar to `SDTX_IOCTL_LATCH_REQUEST`, described above, or the button press, described below. A *generic request* event (`SDTX_EVENT_REQUEST`) is send in response to this. In contrast to those, however, this command does not trigger a new detachment process if none is currently in progress.

- Do nothing. The detachment process eventually times out as described in point 3.

See [IOCTLs](<#rst-ioctls-1>) for more details on these responses.

It is important to note that, if the user presses the detach button at any point when a detachment operation is in progress (i.e. after the EC has sent the initial *detach-request* event (`SDTX_EVENT_REQUEST`) and before it received the corresponding response concluding the process), the detachment process is canceled on the EC-level and an identical event is being sent. Thus a *detach-request* event, by itself, does not signal the start of the detachment process.

The detachment process may further be canceled by the EC due to hardware failures or a low clipboard battery. This is done via a cancel event (`SDTX_EVENT_CANCEL`) with the corresponding cancel reason.

<a id="rst-user-space-interface-documentation"></a>

## User-Space Interface Documentation

<a id="rst-error-codes-and-status-values"></a>

### Error Codes and Status Values

Error and status codes are divided into different categories, which can be used to determine if the status code is an error, and, if it is, the severity and type of that error. The current categories are:


<table>
<caption>Overview of Status/Error Categories.</caption>
<thead>
<tr>
<th>


Name

</th>
<th>


Value

</th>
<th>


Short Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`STATUS`

</td>
<td>


`0x0000`

</td>
<td>


Non-error status codes.

</td>
</tr>
<tr>
<td>


`RUNTIME_ERROR`

</td>
<td>


`0x1000`

</td>
<td>


Non-critical runtime errors.

</td>
</tr>
<tr>
<td>


`HARDWARE_ERROR`

</td>
<td>


`0x2000`

</td>
<td>


Critical hardware failures.

</td>
</tr>
<tr>
<td>


`UNKNOWN`

</td>
<td>


`0xF000`

</td>
<td>


Unknown error codes.

</td>
</tr>
</tbody>
</table>

Other categories are reserved for future use. The `SDTX_CATEGORY()` macro can be used to determine the category of any status value. The `SDTX_SUCCESS()` macro can be used to check if the status value is a success value (`SDTX_CATEGORY_STATUS`) or if it indicates a failure.

Unknown status or error codes sent by the EC are assigned to the `UNKNOWN` category by the driver and may be implemented via their own code in the future.

Currently used error codes are:


<table>
<caption>Overview of Error Codes.</caption>
<thead>
<tr>
<th>


Name

</th>
<th>


Category

</th>
<th>


Value

</th>
<th>


Short Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`SDTX_DETACH_NOT_FEASIBLE`

</td>
<td>


`RUNTIME`

</td>
<td>


`0x1001`

</td>
<td>


Detachment not feasible due to low clipboard battery.

</td>
</tr>
<tr>
<td>


`SDTX_DETACH_TIMEDOUT`

</td>
<td>


`RUNTIME`

</td>
<td>


`0x1002`

</td>
<td>


Detachment process timed out while the latch was locked.

</td>
</tr>
<tr>
<td>


`SDTX_ERR_FAILED_TO_OPEN`

</td>
<td>


`HARDWARE`

</td>
<td>


`0x2001`

</td>
<td>


Failed to open latch.

</td>
</tr>
<tr>
<td>


`SDTX_ERR_FAILED_TO_REMAIN_OPEN`

</td>
<td>


`HARDWARE`

</td>
<td>


`0x2002`

</td>
<td>


Failed to keep latch open.

</td>
</tr>
<tr>
<td>


`SDTX_ERR_FAILED_TO_CLOSE`

</td>
<td>


`HARDWARE`

</td>
<td>


`0x2003`

</td>
<td>


Failed to close latch.

</td>
</tr>
</tbody>
</table>

Other error codes are reserved for future use. Non-error status codes may overlap and are generally only unique within their use-case:


<table>
<caption>Latch Status Codes.</caption>
<thead>
<tr>
<th>


Name

</th>
<th>


Category

</th>
<th>


Value

</th>
<th>


Short Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`SDTX_LATCH_CLOSED`

</td>
<td>


`STATUS`

</td>
<td>


`0x0000`

</td>
<td>


Latch is closed/has been closed.

</td>
</tr>
<tr>
<td>


`SDTX_LATCH_OPENED`

</td>
<td>


`STATUS`

</td>
<td>


`0x0001`

</td>
<td>


Latch is open/has been opened.

</td>
</tr>
</tbody>
</table>


<table>
<caption>Base State Codes.</caption>
<thead>
<tr>
<th>


Name

</th>
<th>


Category

</th>
<th>


Value

</th>
<th>


Short Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`SDTX_BASE_DETACHED`

</td>
<td>


`STATUS`

</td>
<td>


`0x0000`

</td>
<td>


Base has been detached/is not present.

</td>
</tr>
<tr>
<td>


`SDTX_BASE_ATTACHED`

</td>
<td>


`STATUS`

</td>
<td>


`0x0001`

</td>
<td>


Base has been attached/is present.

</td>
</tr>
</tbody>
</table>

Again, other codes are reserved for future use.

<a id="rst-events-1"></a>
<a id="rst-events"></a>

### Events

Events can be received by reading from the device file. They are disabled by default and have to be enabled by executing `SDTX_IOCTL_EVENTS_ENABLE` first. All events follow the layout prescribed by `struct sdtx_event <sdtx_event>`. Specific event types can be identified by their event code, described in `enum sdtx_event_code <sdtx_event_code>`. Note that other event codes are reserved for future use, thus an event parser must be able to handle any unknown/unsupported event types gracefully, by relying on the payload length given in the event header.

Currently provided event types are:


<table>
<caption>Overview of DTX events.</caption>
<thead>
<tr>
<th>


Name

</th>
<th>


Code

</th>
<th>


Payload

</th>
<th>


Short Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`SDTX_EVENT_REQUEST`

</td>
<td>


`1`

</td>
<td>


`0` bytes

</td>
<td>


Detachment process initiated/aborted.

</td>
</tr>
<tr>
<td>


`SDTX_EVENT_CANCEL`

</td>
<td>


`2`

</td>
<td>


`2` bytes

</td>
<td>


EC canceled detachment process.

</td>
</tr>
<tr>
<td>


`SDTX_EVENT_BASE_CONNECTION`

</td>
<td>


`3`

</td>
<td>


`4` bytes

</td>
<td>


Base connection state changed.

</td>
</tr>
<tr>
<td>


`SDTX_EVENT_LATCH_STATUS`

</td>
<td>


`4`

</td>
<td>


`2` bytes

</td>
<td>


Latch status changed.

</td>
</tr>
<tr>
<td>


`SDTX_EVENT_DEVICE_MODE`

</td>
<td>


`5`

</td>
<td>


`2` bytes

</td>
<td>


Device mode changed.

</td>
</tr>
</tbody>
</table>

Individual events in more detail:

<a id="rst-sdtx-event-request"></a>

#### `SDTX_EVENT_REQUEST`

Sent when a detachment process is started or, if in progress, aborted by the user, either via a detach button press or a detach request (`SDTX_IOCTL_LATCH_REQUEST`) being sent from user-space.

Does not have any payload.

<a id="rst-sdtx-event-cancel"></a>

#### `SDTX_EVENT_CANCEL`

Sent when a detachment process is canceled by the EC due to unfulfilled preconditions (e.g. clipboard battery too low to detach) or hardware failure. The reason for cancellation is given in the event payload detailed below and can be one of

- `SDTX_DETACH_TIMEDOUT`: Detachment timed out while the latch was locked. The latch has neither been opened nor unlocked.

- `SDTX_DETACH_NOT_FEASIBLE`: Detachment not feasible due to low clipboard battery.

- `SDTX_ERR_FAILED_TO_OPEN`: Could not open the latch (hardware failure).

- `SDTX_ERR_FAILED_TO_REMAIN_OPEN`: Could not keep the latch open (hardware failure).

- `SDTX_ERR_FAILED_TO_CLOSE`: Could not close the latch (hardware failure).

Other error codes in this context are reserved for future use.

These codes can be classified via the `SDTX_CATEGORY()` macro to discern between critical hardware errors (`SDTX_CATEGORY_HARDWARE_ERROR`) or runtime errors (`SDTX_CATEGORY_RUNTIME_ERROR`), the latter of which may happen during normal operation if certain preconditions for detachment are not given.


<table>
<caption>Detachment Cancel Event Payload</caption>
<thead>
<tr>
<th>


Field

</th>
<th>


Type

</th>
<th>


Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`reason`

</td>
<td>


`__u16 <__u16>`

</td>
<td>


Reason for cancellation.

</td>
</tr>
</tbody>
</table>

<a id="rst-sdtx-event-base-connection"></a>

#### `SDTX_EVENT_BASE_CONNECTION`

Sent when the base connection state has changed, i.e. when the base has been attached, detached, or detachment has become infeasible due to low clipboard battery. The new state and, if a base is connected, ID of the base is provided as payload of type `struct sdtx_base_info <sdtx_base_info>` with its layout presented below:


<table>
<caption>Base-Connection-Change Event Payload</caption>
<thead>
<tr>
<th>


Field

</th>
<th>


Type

</th>
<th>


Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`state`

</td>
<td>


`__u16 <__u16>`

</td>
<td>


Base connection state.

</td>
</tr>
<tr>
<td>


`base_id`

</td>
<td>


`__u16 <__u16>`

</td>
<td>


Type of base connected (zero if none).

</td>
</tr>
</tbody>
</table>

Possible values for `state` are:

- `SDTX_BASE_DETACHED`,

- `SDTX_BASE_ATTACHED`, and

- `SDTX_DETACH_NOT_FEASIBLE`.

Other values are reserved for future use.

<a id="rst-sdtx-event-latch-status"></a>

#### `SDTX_EVENT_LATCH_STATUS`

Sent when the latch status has changed, i.e. when the latch has been opened, closed, or an error occurred. The current status is provided as payload:


<table>
<caption>Latch-Status-Change Event Payload</caption>
<thead>
<tr>
<th>


Field

</th>
<th>


Type

</th>
<th>


Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`status`

</td>
<td>


`__u16 <__u16>`

</td>
<td>


Latch status.

</td>
</tr>
</tbody>
</table>

Possible values for `status` are:

- `SDTX_LATCH_CLOSED`,

- `SDTX_LATCH_OPENED`,

- `SDTX_ERR_FAILED_TO_OPEN`,

- `SDTX_ERR_FAILED_TO_REMAIN_OPEN`, and

- `SDTX_ERR_FAILED_TO_CLOSE`.

Other values are reserved for future use.

<a id="rst-sdtx-event-device-mode"></a>

#### `SDTX_EVENT_DEVICE_MODE`

Sent when the device mode has changed. The new device mode is provided as payload:


<table>
<caption>Device-Mode-Change Event Payload</caption>
<thead>
<tr>
<th>


Field

</th>
<th>


Type

</th>
<th>


Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`mode`

</td>
<td>


`__u16 <__u16>`

</td>
<td>


Device operation mode.

</td>
</tr>
</tbody>
</table>

Possible values for `mode` are:

- `SDTX_DEVICE_MODE_TABLET`,

- `SDTX_DEVICE_MODE_LAPTOP`, and

- `SDTX_DEVICE_MODE_STUDIO`.

Other values are reserved for future use.

<a id="rst-ioctls-1"></a>
<a id="rst-ioctls"></a>

### IOCTLs

The following IOCTLs are provided:


<table>
<caption>Overview of DTX IOCTLs</caption>
<thead>
<tr>
<th>


Type

</th>
<th>


Number

</th>
<th>


Direction

</th>
<th>


Name

</th>
<th>


Description

</th>
</tr>
</thead>
<tbody>
<tr>
<td>


`0xA5`

</td>
<td>


`0x21`

</td>
<td>


`-`

</td>
<td>


`EVENTS_ENABLE`

</td>
<td>


Enable events for the current file descriptor.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x22`

</td>
<td>


`-`

</td>
<td>


`EVENTS_DISABLE`

</td>
<td>


Disable events for the current file descriptor.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x23`

</td>
<td>


`-`

</td>
<td>


`LATCH_LOCK`

</td>
<td>


Lock the latch.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x24`

</td>
<td>


`-`

</td>
<td>


`LATCH_UNLOCK`

</td>
<td>


Unlock the latch.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x25`

</td>
<td>


`-`

</td>
<td>


`LATCH_REQUEST`

</td>
<td>


Request clipboard detachment.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x26`

</td>
<td>


`-`

</td>
<td>


`LATCH_CONFIRM`

</td>
<td>


Confirm clipboard detachment request.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x27`

</td>
<td>


`-`

</td>
<td>


`LATCH_HEARTBEAT`

</td>
<td>


Send heartbeat signal to EC.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x28`

</td>
<td>


`-`

</td>
<td>


`LATCH_CANCEL`

</td>
<td>


Cancel detachment process.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x29`

</td>
<td>


`R`

</td>
<td>


`GET_BASE_INFO`

</td>
<td>


Get current base/connection information.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x2A`

</td>
<td>


`R`

</td>
<td>


`GET_DEVICE_MODE`

</td>
<td>


Get current device operation mode.

</td>
</tr>
<tr>
<td>


`0xA5`

</td>
<td>


`0x2B`

</td>
<td>


`R`

</td>
<td>


`GET_LATCH_STATUS`

</td>
<td>


Get current device latch status.

</td>
</tr>
</tbody>
</table>

<a id="rst-sdtx-ioctl-events-enable"></a>

#### `SDTX_IOCTL_EVENTS_ENABLE`

Defined as `_IO(0xA5, 0x22)`.

Enable events for the current file descriptor. Events can be obtained by reading from the device, if enabled. Events are disabled by default.

<a id="rst-sdtx-ioctl-events-disable"></a>

#### `SDTX_IOCTL_EVENTS_DISABLE`

Defined as `_IO(0xA5, 0x22)`.

Disable events for the current file descriptor. Events can be obtained by reading from the device, if enabled. Events are disabled by default.

<a id="rst-sdtx-ioctl-latch-lock"></a>

#### `SDTX_IOCTL_LATCH_LOCK`

Defined as `_IO(0xA5, 0x23)`.

Locks the latch, causing the detachment procedure to abort without opening the latch on timeout. The latch is unlocked by default. This command will be silently ignored if the latch is already locked.

<a id="rst-sdtx-ioctl-latch-unlock"></a>

#### `SDTX_IOCTL_LATCH_UNLOCK`

Defined as `_IO(0xA5, 0x24)`.

Unlocks the latch, causing the detachment procedure to open the latch on timeout. The latch is unlocked by default. This command will not open the latch when sent during an ongoing detachment process. It will be silently ignored if the latch is already unlocked.

<a id="rst-sdtx-ioctl-latch-request"></a>

#### `SDTX_IOCTL_LATCH_REQUEST`

Defined as `_IO(0xA5, 0x25)`.

Generic latch request. Behavior depends on the context: If no detachment-process is active, detachment is requested. Otherwise the currently active detachment-process will be aborted.

If a detachment process is canceled by this operation, a generic detachment request event (`SDTX_EVENT_REQUEST`) will be sent.

This essentially behaves the same as a detachment button press.

<a id="rst-sdtx-ioctl-latch-confirm"></a>

#### `SDTX_IOCTL_LATCH_CONFIRM`

Defined as `_IO(0xA5, 0x26)`.

Acknowledges and confirms a latch request. If sent during an ongoing detachment process, this command causes the latch to be opened immediately. The latch will also be opened if it has been locked. In this case, the latch lock is reset to the unlocked state.

This command will be silently ignored if there is currently no detachment procedure in progress.

<a id="rst-sdtx-ioctl-latch-heartbeat"></a>

#### `SDTX_IOCTL_LATCH_HEARTBEAT`

Defined as `_IO(0xA5, 0x27)`.

Sends a heartbeat, essentially resetting the detachment timeout. This command can be used to keep the detachment process alive while work required for the detachment to succeed is still in progress.

This command will be silently ignored if there is currently no detachment procedure in progress.

<a id="rst-sdtx-ioctl-latch-cancel"></a>

#### `SDTX_IOCTL_LATCH_CANCEL`

Defined as `_IO(0xA5, 0x28)`.

Cancels detachment in progress (if any). If a detachment process is canceled by this operation, a generic detachment request event (`SDTX_EVENT_REQUEST`) will be sent.

This command will be silently ignored if there is currently no detachment procedure in progress.

<a id="rst-sdtx-ioctl-get-base-info"></a>

#### `SDTX_IOCTL_GET_BASE_INFO`

Defined as `_IOR(0xA5, 0x29, struct sdtx_base_info)`.

Get the current base connection state (i.e. attached/detached) and the type of the base connected to the clipboard. This is command essentially provides a way to query the information provided by the base connection change event (`SDTX_EVENT_BASE_CONNECTION`).

Possible values for `struct sdtx_base_info.state` are:

- `SDTX_BASE_DETACHED`,

- `SDTX_BASE_ATTACHED`, and

- `SDTX_DETACH_NOT_FEASIBLE`.

Other values are reserved for future use.

<a id="rst-sdtx-ioctl-get-device-mode"></a>

#### `SDTX_IOCTL_GET_DEVICE_MODE`

Defined as `_IOR(0xA5, 0x2A, __u16)`.

Returns the device operation mode, indicating if and how the base is attached to the clipboard. This is command essentially provides a way to query the information provided by the device mode change event (`SDTX_EVENT_DEVICE_MODE`).

Returned values are:

- `SDTX_DEVICE_MODE_LAPTOP`

- `SDTX_DEVICE_MODE_TABLET`

- `SDTX_DEVICE_MODE_STUDIO`

See `struct sdtx_device_mode <sdtx_device_mode>` for details. Other values are reserved for future use.

<a id="rst-sdtx-ioctl-get-latch-status"></a>

#### `SDTX_IOCTL_GET_LATCH_STATUS`

Defined as `_IOR(0xA5, 0x2B, __u16)`.

Get the current latch status or (presumably) the last error encountered when trying to open/close the latch. This is command essentially provides a way to query the information provided by the latch status change event (`SDTX_EVENT_LATCH_STATUS`).

Returned values are:

- `SDTX_LATCH_CLOSED`,

- `SDTX_LATCH_OPENED`,

- `SDTX_ERR_FAILED_TO_OPEN`,

- `SDTX_ERR_FAILED_TO_REMAIN_OPEN`, and

- `SDTX_ERR_FAILED_TO_CLOSE`.

Other values are reserved for future use.

<a id="rst-a-note-on-base-ids"></a>

### A Note on Base IDs

Base types/IDs provided via `SDTX_EVENT_BASE_CONNECTION` or `SDTX_IOCTL_GET_BASE_INFO` are directly forwarded from the EC in the lower byte of the combined `__u16 <__u16>` value, with the driver storing the EC type from which this ID comes in the high byte (without this, base IDs over different types of ECs may be overlapping).

The `SDTX_DEVICE_TYPE()` macro can be used to determine the EC device type. This can be one of

- `SDTX_DEVICE_TYPE_HID`, for Surface Aggregator Module over HID, and

- `SDTX_DEVICE_TYPE_SSH`, for Surface Aggregator Module over Surface Serial Hub.

Note that currently only the `SSH` type EC is supported, however `HID` type is reserved for future use.

<a id="rst-structures-and-enums"></a>

### Structures and Enums


#### <a id="c-enum-sdtx_device_mode"></a>`enum sdtx_device_mode`

Mode describing how (and if) the clipboard is attached to the base of the device.

**Constants**

- `SDTX_DEVICE_MODE_TABLET`: The clipboard is detached from the base and the
  device operates as tablet.
- `SDTX_DEVICE_MODE_LAPTOP`: The clipboard is attached normally to the base
  and the device operates as laptop.
- `SDTX_DEVICE_MODE_STUDIO`: The clipboard is attached to the base in reverse.
  The device operates as tablet with keyboard and
  touchpad deactivated, however, the base battery
  and, if present in the specific device model, dGPU
  are available to the system.

#### <a id="c-struct-sdtx_event"></a>`struct sdtx_event`

Event provided by reading from the DTX device file.

**Definition**

```c
struct sdtx_event {
    __u16 length;
    __u16 code;
    __u8 data[];
};
```

**Members**

- `length`: Length of the event payload, in bytes.
- `code`: Event code, detailing what type of event this is.
- `data`: Payload of the event, containing **length** bytes.

**Description**

See [`enum sdtx_event_code`](#c-enum-sdtx_event_code) for currently valid event codes.

#### <a id="c-enum-sdtx_event_code"></a>`enum sdtx_event_code`

Code describing the type of an event.

**Constants**

- `SDTX_EVENT_REQUEST`: Detachment request event type.
- `SDTX_EVENT_CANCEL`: Cancel detachment process event type.
- `SDTX_EVENT_BASE_CONNECTION`: Base/clipboard connection change event type.
- `SDTX_EVENT_LATCH_STATUS`: Latch status change event type.
- `SDTX_EVENT_DEVICE_MODE`: Device mode change event type.

**Description**

Used in [`struct sdtx_event`](#c-struct-sdtx_event) to describe the type of the event. Further event
codes are reserved for future use. Any event parser should be able to
gracefully handle unknown events, i.e. by simply skipping them.

Consult the DTX user-space interface documentation for details regarding
the individual event types.

#### <a id="c-struct-sdtx_base_info"></a>`struct sdtx_base_info`

Describes if and what type of base is connected.

**Definition**

```c
struct sdtx_base_info {
    __u16 state;
    __u16 base_id;
};
```

**Members**

- `state`: The state of the connection. Valid values are `SDTX_BASE_DETACHED`,
  `SDTX_BASE_ATTACHED`, and `SDTX_DETACH_NOT_FEASIBLE` (in case a base
  is attached but low clipboard battery prevents detachment). Other
  values are currently reserved.
- `base_id`: The type of base connected. Zero if no base is connected.


<a id="rst-api-users"></a>

## API Users

A user-space daemon utilizing this API can be found at [https://github.com/linux-surface/surface-dtx-daemon](<https://github.com/linux-surface/surface-dtx-daemon>).

---

# Surface ACPI Notify

_Peios Kernel / Hardware and drivers / Driver development / surface_aggregator: clients_

> The Surface ACPI Notify (SAN) device provides the bridge between ACPI and SAM controller. Specifically, ACPI code can execute requests and handle battery and thermal events via this interface. In addition to this, events relating to the…

<a id="rst-surface-acpi-notify"></a>

<!-- SPDX-License-Identifier: GPL-2.0+ -->

The Surface ACPI Notify (SAN) device provides the bridge between ACPI and SAM controller. Specifically, ACPI code can execute requests and handle battery and thermal events via this interface. In addition to this, events relating to the discrete GPU (dGPU) of the Surface Book 2 can be sent from ACPI code (note: the Surface Book 3 uses a different method for this). The only currently known event sent via this interface is a dGPU power-on notification. While this driver handles the former part internally, it only relays the dGPU events to any other driver interested via its public API and does not handle them.

The public interface of this driver is split into two parts: Client registration and notifier-block registration.

A client to the SAN interface can be linked as consumer to the SAN device via `san_client_link()`. This can be used to ensure that the a client receiving dGPU events does not miss any events due to the SAN interface not being set up as this forces the client driver to unbind once the SAN driver is unbound.

Notifier-blocks can be registered by any device for as long as the module is loaded, regardless of being linked as client or not. Registration is done with `san_dgpu_notifier_register()`. If the notifier is not needed any more, it should be unregistered via `san_dgpu_notifier_unregister()`.

Consult the API documentation below for more details.

<a id="rst-api-documentation"></a>

## API Documentation


#### <a id="c-struct-san_dgpu_event"></a>`struct san_dgpu_event`

Discrete GPU ACPI event.

**Definition**

```c
struct san_dgpu_event {
    u8 category;
    u8 target;
    u8 command;
    u8 instance;
    u16 length;
    u8 *payload;
};
```

**Members**

- `category`: Category of the event.
- `target`: Target ID of the event source.
- `command`: Command ID of the event.
- `instance`: Instance ID of the event source.
- `length`: Length of the event's payload data (in bytes).
- `payload`: Pointer to the event's payload data.



#### <a id="c-san_client_link"></a>`san_client_link()`

```c
int san_client_link(struct device *client)
```

Link client as consumer to SAN device.

**Parameters**

- `struct device *client`: The client to link.

**Description**

Sets up a device link between the provided client device as consumer and
the SAN device as provider. This function can be used to ensure that the
SAN interface has been set up and will be set up for as long as the driver
of the client device is bound. This guarantees that, during that time, all
dGPU events will be received by any registered notifier.

The link will be automatically removed once the client device's driver is
unbound.

**Return**

Returns zero on success, `-ENXIO` if the SAN interface has not been
set up yet, and `-ENOMEM` if device link creation failed.

#### <a id="c-san_dgpu_notifier_register"></a>`san_dgpu_notifier_register()`

```c
int san_dgpu_notifier_register(struct notifier_block *nb)
```

Register a SAN dGPU notifier.

**Parameters**

- `struct notifier_block *nb`: The notifier-block to register.

**Description**

Registers a SAN dGPU notifier, receiving any new SAN dGPU events sent from
ACPI. The registered notifier will be called with [`struct san_dgpu_event`](#c-struct-san_dgpu_event)
as notifier data and the command ID of that event as notifier action.

#### <a id="c-san_dgpu_notifier_unregister"></a>`san_dgpu_notifier_unregister()`

```c
int san_dgpu_notifier_unregister(struct notifier_block *nb)
```

Unregister a SAN dGPU notifier.

**Parameters**

- `struct notifier_block *nb`: The notifier-block to unregister.
