Linux Input/HID development
 help / color / mirror / Atom feed
* Re: [PATCH v7 1/3] HID: nintendo: Add preliminary Switch 2 controller driver
From: sashiko-bot @ 2026-06-30  9:06 UTC (permalink / raw)
  To: Vicki Pfau; +Cc: dmitry.torokhov, linux-input
In-Reply-To: <20260630084838.2313379-2-vi@endrift.com>

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:
- [High] Missing lock release in URB error cleanup loops causes an immediate self-deadlock.
- [High] Incorrect teardown sequence in USB disconnect and probe error paths allows use-after-free of the controller and USB context.
- [High] Calling `ida_free()` with an unallocated ID (U32_MAX) on probe failure causes a BUG_ON / kernel panic.
- [High] Devres lifecycle violation for the input device and LEDs causes a severe Use-After-Free during teardown.
- [High] Overwriting the active configuration pointer if multiple matching USB interfaces are probed leads to a kernel panic on disconnect.
- [Medium] Type Confusion in LED brightness callback when casting an `input_dev->dev` pointer to a `hid_device`.
- [Medium] Early returns on unhandled URB error statuses permanently leak URB slots and fail to clean up state.
--

commit 7e33d97c8c5d7d9d33a04e0a368779204b06f73d
Author: Vicki Pfau <vi@endrift.com>

HID: nintendo: Add preliminary Switch 2 controller driver

This commit introduces a new driver for Nintendo Switch 2 controllers. It
implements a split-driver design with a main HID interface and a secondary
USB configuration interface. Support is included for basic input on Joy-Con 2,
Pro Controller 2, and GameCube NSO controllers.

> diff --git a/drivers/input/joystick/nintendo-switch2-usb.c b/drivers/input/joystick/nintendo-switch2-usb.c
> index 0000000000000..632af1a8638a7
> --- /dev/null
> +++ b/drivers/input/joystick/nintendo-switch2-usb.c
[ ... ]
> +static void switch2_bulk_out(struct urb *urb)
> +{
[ ... ]
> +	switch (urb->status) {
> +	case 0:
> +		break;
> +	case -ECONNRESET:
> +	case -ENOENT:
> +	case -ESHUTDOWN:
> +		dev_dbg(&ns2_usb->udev->dev, "shutting down output urb: %d\n", urb->status);
> +		return;
> +	default:
> +		dev_dbg(&ns2_usb->udev->dev, "unknown output urb status: %d\n", urb->status);
> +		return;
> +	}
> +
> +	for (i = 0; i < NS2_OUT_URBS; i++) {
> +		if (ns2_usb->bulk_out[i].urb != urb)

[Severity: Medium]
If the URB status falls into the default case, this function prints a debug
message and returns immediately. Does this skip the loop below that is supposed
to free the URB slot by setting its state to NS2_URB_FREE? If this occurs
multiple times, it seems all URB slots will remain permanently in-use, leading
to -ENOBUFS errors on future attempts.

[ ... ]
> +static int switch2_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
> +{
[ ... ]
> +	if (ret < 0) {
> +		cancel_work_sync(&ns2_usb->message_in_work);
> +		goto err_free_out;
> +	}
> +
> +	return 0;
> +
> +err_free_out:

[Severity: High]
In this error path, cancel_work_sync() is called before the IN URBs are
killed at err_free_in. If an IN URB completes immediately after
cancel_work_sync(), could it requeue the work? This would leave the work
active while the USB device context is being destroyed, potentially leading to
a use-after-free.

[ ... ]
> +	usb_kill_anchored_urbs(&ns2_usb->bulk_out_anchor);
> +	for (i = 0; i < NS2_OUT_URBS; i++) {
> +		spin_lock_irqsave(&ns2_usb->bulk_out_lock, flags);
> +		urb = ns2_usb->bulk_out[i].urb;
> +		data = ns2_usb->bulk_out[i].data;
> +		if (!urb)
> +			continue;
> +
> +		ns2_usb->bulk_out[i].urb = NULL;

[Severity: High]
When urb is NULL, this loop executes the continue statement while the 
bulk_out_lock is held. Will this cause the next iteration to attempt to
acquire the same spinlock and immediately deadlock the CPU?

[ ... ]
> +err_free_in:
> +	usb_kill_anchored_urbs(&ns2_usb->bulk_in_anchor);
> +	for (i = 0; i < NS2_IN_URBS; i++) {
> +		spin_lock_irqsave(&ns2_usb->bulk_in_lock, flags);
> +		urb = ns2_usb->bulk_in[i].urb;
> +		data = ns2_usb->bulk_in[i].data;
> +		if (!urb)
> +			continue;
> +
> +		ns2_usb->bulk_in[i].urb = NULL;

[Severity: High]
Similarly, does this continue statement skip unlocking bulk_in_lock, causing
a deadlock on the next iteration?

[ ... ]
> +static void switch2_usb_disconnect(struct usb_interface *intf)
> +{
[ ... ]
> +	switch2_controller_detach_cfg(ns2_usb->cfg.parent);
> +
> +	usb_kill_anchored_urbs(&ns2_usb->bulk_out_anchor);
[ ... ]
> +	cancel_work_sync(&ns2_usb->message_in_work);
> +	usb_kill_anchored_urbs(&ns2_usb->bulk_in_anchor);

[Severity: High]
Is this teardown sequence safe? switch2_controller_detach_cfg() drops the
refcount on the controller, potentially freeing it while message_in_work is
still active. Furthermore, cancel_work_sync() is called before killing the IN
URBs, which might allow the IN URB completion handler to reschedule the work
after we've attempted to cancel it. This seems to create multiple race windows
for a use-after-free.

> diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c
> index e7302ec01ff1f..b9e2e708e28f3 100644
> --- a/drivers/hid/hid-nintendo.c
> +++ b/drivers/hid/hid-nintendo.c
[ ... ]
> +static int switch2_player_led_brightness_set(struct led_classdev *led,
> +					    enum led_brightness brightness)
> +{
> +	struct device *dev = led->dev->parent;
> +	struct hid_device *hdev = to_hid_device(dev);
> +	struct switch2_controller *ns2 = hid_get_drvdata(hdev);

[Severity: Medium]
The LEDs are registered with the input device as their parent, so
led->dev->parent points to the input_dev's device struct. Does using
to_hid_device() here perform an invalid cast? While hid_get_drvdata() might
accidentally work due to struct layout overlaps for the driver_data pointer,
casting an input_dev device to a hid_device looks like a strict-aliasing
violation.

[ ... ]
> +static int switch2_init_input(struct switch2_controller *ns2)
> +{
[ ... ]
> +	input = devm_input_allocate_device(&hdev->dev);
[ ... ]
> +		ret = devm_led_classdev_register(&input->dev, led);

[Severity: High]
The input device and LEDs are allocated using devm, tying their lifecycle to
the HID device. However, the physical memory for the leds array is embedded
inside the ns2 structure, which is managed via manual krefs and freed in
switch2_kref_put(). When the controller is removed, ns2 is freed immediately,
but the devres cleanup for the LEDs will run later when the HID device unbinds.
Could this lead to the LED core accessing the already-freed ns2 memory?

[ ... ]
> +static int switch2_probe(struct hid_device *hdev, const struct hid_device_id *id)
> +{
[ ... ]
> +	ns2->player_id = U32_MAX;
> +	ret = ida_alloc(&nintendo_player_id_allocator, GFP_KERNEL);
> +	if (ret < 0)
> +		hid_warn(hdev, "Failed to allocate player ID, skipping; ret=%d\n", ret);
> +	else
> +		ns2->player_id = ret;
[ ... ]
> +err_cleanup:
> +	ida_free(&nintendo_player_id_allocator, ns2->player_id);

[Severity: High]
If ida_alloc() fails, ns2->player_id remains U32_MAX. If probe later fails
and jumps to err_cleanup, ida_free() is called unconditionally. Since
ida_free() expects a valid signed integer ID, passing U32_MAX (-1) will
trigger a BUG_ON. Should there be a check before freeing?

[ ... ]
> +static void switch2_remove(struct hid_device *hdev)
> +{
> +	struct switch2_controller *ns2 = hid_get_drvdata(hdev);
> +
> +	hid_hw_close(hdev);
> +	hid_hw_stop(hdev);
> +	mutex_lock(&ns2->lock);
> +	ida_free(&nintendo_player_id_allocator, ns2->player_id);

[Severity: High]
Similar to the probe error path, if the device was successfully probed but
ida_alloc() had initially failed, removing the device will call ida_free() on
U32_MAX, triggering a BUG_ON.

[ ... ]
> +int switch2_controller_attach_cfg(const char *phys, struct switch2_cfg_intf *cfg)
> +{
[ ... ]
> +	mutex_lock(&ns2->lock);
> +	WARN_ON(ns2->cfg);
> +	ns2->cfg = cfg;

[Severity: High]
If multiple USB interfaces from the same device match and trigger probes,
this function emits a warning but proceeds to overwrite ns2->cfg. This breaks
the one-to-one linkage. Should this return -EBUSY instead of overwriting?

[ ... ]
> +void switch2_controller_detach_cfg(struct switch2_controller *ns2)
> +{
> +	mutex_lock(&ns2->lock);

[Severity: High]
If the active configuration pointer was overwritten due to multiple interfaces,
when the first interface disconnects, it will clear the parent pointer on the
second interface's config. When the second interface then disconnects, it will
pass a NULL ns2 pointer to this function, causing an immediate NULL pointer
dereference when attempting to lock ns2->lock.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260630084838.2313379-1-vi@endrift.com?part=1

^ permalink raw reply

* Re: [PATCH v7 2/3] HID: nintendo: Add rumble support for Switch 2 controllers
From: sashiko-bot @ 2026-06-30  9:07 UTC (permalink / raw)
  To: Vicki Pfau; +Cc: dmitry.torokhov, linux-input
In-Reply-To: <20260630084838.2313379-3-vi@endrift.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The delay calculation in `switch2_rumble_work` causes an unbounded timer catch-up loop, flooding the USB bus with back-to-back output reports if the worker is delayed.
- [High] Sending an embedded, non-cacheline-aligned struct member (`ns2->rumble_buffer`) to `hid_hw_output_report()` can cause memory corruption during DMA.
- [High] The teardown sequence calls `hid_hw_stop()` before cancelling the rumble work and unregistering the input device, allowing output reports to be sent to a stopped device.
--

commit 6d9250862d78279cc28f8c0b3c447d3a7da2e69c
Author: Vicki Pfau <vi@endrift.com>

HID: nintendo: Add rumble support for Switch 2 controllers

This adds rumble support for both the "HD Rumble" linear resonant actuator
type as used in the Joy-Cons and Pro Controller, as well as the eccentric
rotating mass type used in the GameCube controller. Note that since there's
currently no API for exposing full control of LRAs with evdev, it only
simulates a basic rumble for now.

> diff --git a/drivers/hid/hid-nintendo.c b/drivers/hid/hid-nintendo.c
> index b9e2e708e28f3..a53c554a54ae3 100644
> --- a/drivers/hid/hid-nintendo.c
> +++ b/drivers/hid/hid-nintendo.c
[ ... ]
> @@ -3043,8 +3056,45 @@ struct switch2_controller {
>  
>  	uint32_t player_id;
>  	struct led_classdev leds[4];
> +
> +#if IS_ENABLED(CONFIG_NINTENDO_FF)
> +	spinlock_t rumble_lock;
> +	uint8_t rumble_seq;
> +	union {
> +		struct switch2_hd_rumble hd;
> +		struct switch2_erm_rumble sd;
> +	} rumble;
> +	uint64_t last_rumble_work;
> +	struct delayed_work rumble_work;
> +	uint8_t rumble_buffer[64];

[Severity: High]
Does this pass an embedded, non-cacheline-aligned array to the DMA layer?

The USB DMA API requires that data buffers passed to it are dynamically
allocated (e.g., via kmalloc()) to ensure they are cache-line aligned.
Because rumble_buffer is embedded directly in struct switch2_controller, it
shares a cache line with other members like rumble_work.

Could this lead to memory corruption of adjacent struct members during
DMA cache invalidation on non-coherent architectures when passed to
hid_hw_output_report() in switch2_rumble_work()?

> +#endif
>  };
[ ... ]
> +static void switch2_rumble_work(struct work_struct *work)
> +{
[ ... ]
> +	if (active) {
> +		unsigned long interval = msecs_to_jiffies(2);
> +		uint64_t current_jiffies = get_jiffies_64();
> +
> +		if (!ns2->last_rumble_work)
> +			ns2->last_rumble_work = current_jiffies;
> +		else
> +			ns2->last_rumble_work += interval;
> +
> +		if (ns2->last_rumble_work + interval >= current_jiffies)
> +			schedule_delayed_work(&ns2->rumble_work,
> +				ns2->last_rumble_work + interval - current_jiffies);
> +		else
> +			schedule_delayed_work(&ns2->rumble_work, 0);

[Severity: High]
Can this delay calculation result in an unbounded timer catch-up loop?

If the switch2_rumble_work() worker is delayed (e.g., due to system load
or slow synchronous transfers), last_rumble_work will fall behind
current_jiffies. The function appears to attempt to catch up by continually
adding the fixed 2ms interval to last_rumble_work and scheduling the next
execution with 0 delay.

For example, a 100ms stall might result in 50 back-to-back executions.
Could this flood the USB bus with zero-delay output reports, wasting CPU
resources and potentially degrading the device or bus?

> +	} else {
> +		ns2->last_rumble_work = 0;
> +	}
> +
> +	mutex_lock(&ns2->lock);
> +	if (!ns2->hdev) {
> +		cancel_delayed_work(&ns2->rumble_work);
> +	} else {
> +		ret = hid_hw_output_report(ns2->hdev, ns2->rumble_buffer, 64);
[ ... ]
> @@ -4009,6 +4210,9 @@ static void switch2_remove(struct hid_device *hdev)
>  
>  	hid_hw_close(hdev);
>  	hid_hw_stop(hdev);
> +#if IS_ENABLED(CONFIG_NINTENDO_FF)
> +	cancel_delayed_work_sync(&ns2->rumble_work);
> +#endif
>  	mutex_lock(&ns2->lock);
>  	ida_free(&nintendo_player_id_allocator, ns2->player_id);
>  	WARN_ON(ns2->hdev != hdev);
> +	ns2->hdev = NULL;

[Severity: High]
Does this teardown sequence allow output reports to be sent to a stopped
device?

Here, hid_hw_stop() is called to stop the underlying HID transport,
followed by cancel_delayed_work_sync(&ns2->rumble_work). If the worker
thread was preempted just before acquiring ns2->lock in
switch2_rumble_work():

Worker thread:
    mutex_lock(&ns2->lock);
    // preempted before lock acquisition

Teardown:
    hid_hw_stop(hdev);
    cancel_delayed_work_sync(&ns2->rumble_work); // waits for worker

Worker thread resumes:
    if (!ns2->hdev) // hdev is still valid here, set to NULL later
        ...
    else
        ret = hid_hw_output_report(ns2->hdev, ns2->rumble_buffer, 64);

Could the worker thread then execute hid_hw_output_report() on the stopped
device, potentially causing undefined behavior in the transport drivers?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260630084838.2313379-1-vi@endrift.com?part=2

^ permalink raw reply

* Re: [PATCH] Input: zinitix: iterate contact slots by finger count
From: Linus Walleij @ 2026-06-30  9:11 UTC (permalink / raw)
  To: Thanh Nguyen, Nikita Travkin
  Cc: dmitry.torokhov, linux-input, linux-kernel, linus.walleij,
	timon37
In-Reply-To: <20260408051907.792-1-thanhnguyxn07@gmail.com>

Hi Thanh,

sorry for slow reply!

On Wed, Apr 8, 2026 at 6:19 AM Thanh Nguyen <thanhnguyxn07@gmail.com> wrote:
>
> On affected devices (for example Samsung A3 2015), the value in
> touch_event.finger_mask appears to behave as a count of reported slots
> rather than a bitmask. Using for_each_set_bit() can then skip valid
> contacts and break multitouch gestures.
>
> Keep filtering by SUB_BIT_EXIST to avoid reporting shadow contacts, but
> iterate from slot 0 up to min(finger_mask, MAX_SUPPORTED_FINGER_NUM).
> This follows the maintainer feedback to treat the field as a possible
> count while preserving the anti-shadow check.
>
> Fixes: e941dc13fd37 ("Input: zinitix - do not report shadow fingers")
> Link: https://bugzilla.kernel.org/show_bug.cgi?id=221278
> Signed-off-by: Thanh Nguyen <thanhnguyxn07@gmail.com>
> ---
> v2:
>  - Address maintainer feedback: do not revert e941dc13fd37.
>  - Keep SUB_BIT_EXIST filtering to avoid shadow contacts.
>  - Treat finger_mask as a slot-count bound and iterate 0..min(mask, max).

I can test this if need be, but maybe Nikita want to check it
first?

Yours,
Linus Walleij

^ permalink raw reply

* [PATCH v3] HID: logitech-hidpp: Add support for HID++ Multi-Platform feature (0x4531)
From: DevExalt @ 2026-06-30  9:48 UTC (permalink / raw)
  To: jikos, bentiss
  Cc: lains, hadess, linux-input, linux-kernel, sari.kreitem, hbarnor,
	Baraa Atta (Dev Exalt)
In-Reply-To: <20260625080807.74157-1-exalt.dev.team@gmail.com>

From: "Baraa Atta (Dev Exalt)" <exalt.dev.team@gmail.com>

Add support in the Logitech HID++ driver for the HID++ Multi-Platform
feature (0x4531), which enables HID++ devices to adjust their behavior
based on the host operating system.

This patch:
 * Adds device IDs for MX Keys S (046d) and Casa Keys (046d).
 * Introduces the per-device sysfs attribute "platform" to allow selecting
   a target platform.
 * Detects whether a device implements feature 0x4531.
 * Validates that the requested platform is supported by the device.
 * Applies the selected platform when valid.
 * Leaves the device unchanged when an unsupported platform is requested.

Supported values for the platform sysfs attribute:

 windows, winemb, linux, chrome, android,
 macos, ios, webos, tizen

TEST=Pair MX Keys S and Casa Keys over Bluetooth and verify:
 * Feature 0x4531 is detected.
 * Valid platform values written through sysfs are accepted and applied.
 * Invalid platform values result in no update.
 * Devices without 0x4531 retain default behavior.
 * Platform-specific key behavior is observed once applied.

Signed-off-by: Baraa Atta (Dev Exalt) <exalt.dev.team@gmail.com>
---
Changes in v2:
  * Replace the global hidpp_platform module parameter with a per-device
    sysfs attribute
  * Expose all platforms  supported by the HID++ Multi-Platform feature
  * Update documentation and testing description

Changes in v3:
  * Address Sashiko review comments.
  * Switch to devm_mutex_init() to handle mutex lifecycle management
    automatically.
  * Move hidpp_multiplatform_init() to the end of hidpp_probe() after
    hid_device_io_start() to guarantee that the hardware I/O loop is fully
    active when the sysfs attribute becomes visible.
 
 .../testing/sysfs-driver-hid-logitech-hidpp   |  29 ++
 drivers/hid/hid-ids.h                         |   2 +
 drivers/hid/hid-logitech-hidpp.c              | 384 ++++++++++++++++++
 drivers/hid/hid-quirks.c                      |   2 +
 4 files changed, 417 insertions(+)

diff --git a/Documentation/ABI/testing/sysfs-driver-hid-logitech-hidpp b/Documentation/ABI/testing/sysfs-driver-hid-logitech-hidpp
index d8f831f2d6b5..9b963407a975 100644
--- a/Documentation/ABI/testing/sysfs-driver-hid-logitech-hidpp
+++ b/Documentation/ABI/testing/sysfs-driver-hid-logitech-hidpp
@@ -17,3 +17,32 @@ Description:
 		handling battery properties in the kernel. This way, upower can
 		add a udev rule to decide whether or not it should use the
 		internal unifying support or the generic kernel one.
+
+What:		/sys/bus/hid/drivers/logitech-hidpp-device/<dev>/platform
+Date:		Jun, 2026
+KernelVersion:	7.2
+Contact:	linux-input@vger.kernel.org
+Description:
+		(WO) This attribute is present only on Logitech HID++ 2.0 devices
+		that implement feature 0x4531 (Multi-Platform). It allows the host
+		to select which operating-system platform the device should emulate,
+		altering its key mapping and behaviour accordingly.
+
+		Writing one of the following platform names programs the device:
+
+		  ===========  ======================================================
+		  windows       Standard Windows key layout
+		  winemb        Windows Embedded key layout
+		  linux         Linux key layout
+		  chrome        ChromeOS key layout
+		  android       Android key layout
+		  macos         macOS key layout
+		  ios           iOS key layout
+		  webos         webOS key layout
+		  tizen         Tizen key layout
+		  ===========  ======================================================
+
+		Only platforms advertised by the device's own descriptors are
+		accepted. The input is case-insensitive. Writing an unknown
+		platform name returns -EINVAL; writing a valid name that the
+		device does not expose in its descriptors returns -EOPNOTSUPP.
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 1059922baaac..a6c2dea79397 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -913,6 +913,8 @@
 #define USB_DEVICE_ID_LOGITECH_T651	0xb00c
 #define USB_DEVICE_ID_LOGITECH_DINOVO_EDGE_KBD	0xb309
 #define USB_DEVICE_ID_LOGITECH_CASA_TOUCHPAD	0xbb00
+#define USB_DEVICE_ID_LOGITECH_CASA_KEYS_KEYBOARD	0xb371
+#define USB_DEVICE_ID_LOGITECH_MX_KEYS_S_KEYBOARD	0xb378
 #define USB_DEVICE_ID_LOGITECH_C007	0xc007
 #define USB_DEVICE_ID_LOGITECH_C077	0xc077
 #define USB_DEVICE_ID_LOGITECH_RECEIVER	0xc101
diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid-logitech-hidpp.c
index 90b0184df777..33904f309380 100644
--- a/drivers/hid/hid-logitech-hidpp.c
+++ b/drivers/hid/hid-logitech-hidpp.c
@@ -209,6 +209,9 @@ struct hidpp_device {
 	int hires_wheel_multiplier;
 	u8 hires_wheel_feature_index;
 
+	u8 multiplatform_feature_index;
+	struct mutex multiplatform_lock;
+
 	bool connected_once;
 };
 
@@ -4423,6 +4426,385 @@ static bool hidpp_application_equals(struct hid_device *hdev,
 	return report && report->application == application;
 }
 
+/* -------------------------------------------------------------------------- */
+/* 0x4531: Multi-Platform Support                                             */
+/* -------------------------------------------------------------------------- */
+
+/*
+ * Some Logitech devices expose the HID++ feature 0x4531 (Multi-Platform) allowing
+ * the host to specify which operating system platform to use on the device. Changing device's
+ * platform may alter the behavior of the device to match the specified platform.
+ *
+ * Devices that implement this feature expose a per-device sysfs attribute
+ * "platform". Writing one of (windows|winemb|linux|chrome|android|
+ * macos|ios|webos|tizen) selects the matching platform descriptor on the device.
+ */
+
+#define HIDPP_MULTIPLATFORM_FEAT_ID			0x4531
+#define HIDPP_MULTIPLATFORM_GET_FEATURE_INFO		0x0F
+#define HIDPP_MULTIPLATFORM_GET_PLATFORM_DESCRIPTOR	0x1F
+#define HIDPP_MULTIPLATFORM_SET_CURRENT_PLATFORM	0x3F
+
+#define HIDPP_MULTIPLATFORM_PLATFORM_MASK_TIZEN		BIT(0)
+#define HIDPP_MULTIPLATFORM_PLATFORM_MASK_WINDOWS	BIT(8)
+#define HIDPP_MULTIPLATFORM_PLATFORM_MASK_WINEMB	BIT(9)
+#define HIDPP_MULTIPLATFORM_PLATFORM_MASK_LINUX		BIT(10)
+#define HIDPP_MULTIPLATFORM_PLATFORM_MASK_CHROME	BIT(11)
+#define HIDPP_MULTIPLATFORM_PLATFORM_MASK_ANDROID	BIT(12)
+#define HIDPP_MULTIPLATFORM_PLATFORM_MASK_MACOS		BIT(13)
+#define HIDPP_MULTIPLATFORM_PLATFORM_MASK_IOS		BIT(14)
+#define HIDPP_MULTIPLATFORM_PLATFORM_MASK_WEBOS		BIT(15)
+
+struct hidpp_platform_desc {
+	u8 plat_idx;
+	u8 desc_idx;
+	u16 plat_mask;
+};
+
+/*
+ * Platform names exposed through the "platform" sysfs attribute. The order of
+ * this array must stay in sync with multiplatform_masks[] below, as the index
+ * returned by sysfs_match_string() is used to look up the matching mask.
+ */
+static const char * const multiplatform_names[] = {
+	"windows", "winemb", "linux", "chrome",
+	"android", "macos", "ios", "webos", "tizen", NULL
+};
+
+static const u16 multiplatform_masks[] = {
+	HIDPP_MULTIPLATFORM_PLATFORM_MASK_WINDOWS,
+	HIDPP_MULTIPLATFORM_PLATFORM_MASK_WINEMB,
+	HIDPP_MULTIPLATFORM_PLATFORM_MASK_LINUX,
+	HIDPP_MULTIPLATFORM_PLATFORM_MASK_CHROME,
+	HIDPP_MULTIPLATFORM_PLATFORM_MASK_ANDROID,
+	HIDPP_MULTIPLATFORM_PLATFORM_MASK_MACOS,
+	HIDPP_MULTIPLATFORM_PLATFORM_MASK_IOS,
+	HIDPP_MULTIPLATFORM_PLATFORM_MASK_WEBOS,
+	HIDPP_MULTIPLATFORM_PLATFORM_MASK_TIZEN,
+};
+
+/**
+ * hidpp_multiplatform_errno() - Convert HID++ protocol error codes to Linux errno
+ * @err: HID++ protocol error code (positive) or Linux errno (negative or zero)
+ *
+ * Converts a HID++ protocol error code to the corresponding Linux errno. If @err is
+ * already a negative or zero Linux errno, it is returned unchanged. Otherwise, if @err
+ * is a positive HID++ error code, it is mapped to the appropriate negative Linux errno
+ * based on the HID++ specification error codes.
+ *
+ * This is used to ensure that functions interacting with the Multi-Platform feature can
+ * return consistent Linux error codes even when they encounter errors defined by the HID++
+ * protocol when the platform is set from the sysfs attribute.
+ *
+ * Return: Negative Linux errno corresponding to the HID++ error code, or @err if it is
+ * already a Linux errno.
+ */
+static int hidpp_multiplatform_errno(int err)
+{
+	if (err <= 0)
+		return err;
+
+	switch (err) {
+	case HIDPP20_ERROR_INVALID_ARGS:
+	case HIDPP20_ERROR_OUT_OF_RANGE:
+	case HIDPP20_ERROR_INVALID_FEATURE_INDEX:
+	case HIDPP20_ERROR_INVALID_FUNCTION_ID:
+		return -EINVAL;
+	case HIDPP20_ERROR_NOT_ALLOWED:
+		return -EPERM;
+	case HIDPP20_ERROR_BUSY:
+		return -EBUSY;
+	case HIDPP20_ERROR_UNSUPPORTED:
+		return -EOPNOTSUPP;
+	case HIDPP20_ERROR_HW_ERROR:
+	case HIDPP20_ERROR_UNKNOWN:
+	default:
+		return -EIO;
+	}
+}
+
+/**
+ * hidpp_multiplatform_get_num_pdesc() - Retrieve number of platform descriptors
+ * @hidpp: Pointer to the hidpp_device instance
+ * @feat_index: Feature index of the Multi-Platform feature
+ * @num_desc: Pointer to store the number of platform descriptors
+ *
+ * Retrieves the number of platform descriptors supported by the device through
+ * the Multi-Platform feature and stores it in @num_desc.
+ *
+ * Return: 0 on success, or a negative Linux errno on failure.
+ */
+static int hidpp_multiplatform_get_num_pdesc(struct hidpp_device *hidpp,
+					     u8 feat_index, u8 *num_desc)
+{
+	int ret;
+	struct hidpp_report response;
+	struct hid_device *hdev = hidpp->hid_dev;
+
+	ret = hidpp_send_fap_command_sync(hidpp, feat_index,
+					  HIDPP_MULTIPLATFORM_GET_FEATURE_INFO,
+					  NULL, 0, &response);
+	if (ret) {
+		hid_warn(hdev, "Multiplatform: GET_FEATURE_INFO failed (err=%d)", ret);
+		return hidpp_multiplatform_errno(ret);
+	}
+
+	*num_desc = response.fap.params[3];
+	hid_dbg(hdev, "Multiplatform: Device supports %d platform descriptors", *num_desc);
+
+	return 0;
+}
+
+/**
+ * hidpp_multiplatform_get_platform_desc() - Retrieve a platform descriptor entry
+ * @hidpp: Pointer to the hidpp_device instance
+ * @feat_index: Feature index of the Multi-Platform feature
+ * @platform_idx: Index of the platform descriptor to retrieve
+ * @pdesc: Pointer to store the retrieved platform descriptor
+ *
+ * Retrieves a single platform descriptor identified by @platform_idx from the
+ * device and stores the parsed descriptor fields in @pdesc.
+ *
+ * Return: 0 on success, or a negative Linux errno on failure.
+ */
+static int hidpp_multiplatform_get_platform_desc(struct hidpp_device *hidpp, u8 feat_index,
+						 u8 platform_idx, struct hidpp_platform_desc *pdesc)
+{
+	int ret;
+	struct hidpp_report response;
+	u8 params[1] = { platform_idx };
+	struct hid_device *hdev = hidpp->hid_dev;
+
+	ret = hidpp_send_fap_command_sync(hidpp, feat_index,
+					  HIDPP_MULTIPLATFORM_GET_PLATFORM_DESCRIPTOR,
+					  params, sizeof(params), &response);
+
+	if (ret) {
+		hid_warn(hdev,
+			 "Multiplatform: GET_PLATFORM_DESCRIPTOR failed for index %d (err=%d)",
+			 platform_idx, ret);
+		return hidpp_multiplatform_errno(ret);
+	}
+
+	pdesc->plat_idx = response.fap.params[0];
+	pdesc->desc_idx = response.fap.params[1];
+	pdesc->plat_mask = get_unaligned_be16(&response.fap.params[2]);
+
+	hid_dbg(hdev,
+		"Multiplatform: descriptor %d: plat_idx=%d, desc_idx=%d, plat_mask=0x%04x",
+		platform_idx, pdesc->plat_idx, pdesc->desc_idx, pdesc->plat_mask);
+
+	return 0;
+}
+
+/**
+ * hidpp_multiplatform_get_platform_index() - Find platform index for a mask
+ * @hidpp: Pointer to the hidpp_device instance
+ * @feat_index: Feature index of the Multi-Platform feature
+ * @plat_mask: Platform mask to search for
+ * @plat_index: Pointer to store the matched platform index
+ *
+ * Iterates through all platform descriptors exposed by the device via the
+ * Multi-Platform feature, retrieving each descriptor and comparing its
+ * platform mask to @plat_mask. A descriptor matches if its mask overlaps with
+ * the requested @plat_mask (i.e. (pdesc.plat_mask & plat_mask) is non-zero).
+ *
+ * When a matching descriptor is found, its platform index (plat_idx) is
+ * written to @plat_index and the function returns success.
+ *
+ * Return: 0 on success; -EOPNOTSUPP if the device exposes no descriptor
+ *         matching @plat_mask; or another negative Linux errno on transport
+ *         failure.
+ */
+static int hidpp_multiplatform_get_platform_index(struct hidpp_device *hidpp,
+						  u8 feat_index, u16 plat_mask,
+						  u8 *plat_index)
+{
+	int i;
+	int ret;
+	u8 num_desc;
+	struct hidpp_platform_desc pdesc;
+	struct hid_device *hdev = hidpp->hid_dev;
+
+	ret = hidpp_multiplatform_get_num_pdesc(hidpp, feat_index, &num_desc);
+	if (ret)
+		return ret;
+
+	for (i = 0; i < num_desc; i++) {
+		ret = hidpp_multiplatform_get_platform_desc(hidpp, feat_index, i, &pdesc);
+		if (ret)
+			return ret;
+
+		if (pdesc.plat_mask & plat_mask) {
+			*plat_index = pdesc.plat_idx;
+			hid_dbg(hdev,
+				"Multiplatform: Selected platform index %d for mask 0x%04x",
+				*plat_index, plat_mask);
+			return 0;
+		}
+	}
+
+	hid_dbg(hdev,
+		"Multiplatform: No matching platform descriptor for mask 0x%04x",
+		plat_mask);
+	return -EOPNOTSUPP;
+}
+
+/**
+ * hidpp_multiplatform_update_device_platform() - Update the device platform
+ * @hidpp: Pointer to the hidpp_device instance
+ * @feat_index: Feature index of the Multi-Platform feature
+ * @plat_index: Platform index to set on the device
+ *
+ * Sends the HID++ Multi-Platform 'SET_CURRENT_PLATFORM' command to the device to
+ * update its platform index to @plat_index.
+ *
+ * Return: 0 on success, or a negative Linux errno on failure.
+ */
+static int hidpp_multiplatform_update_device_platform(struct hidpp_device *hidpp,
+						      u8 feat_index, u8 plat_index)
+{
+	int ret;
+	struct hidpp_report response;
+	/* Byte 0 (hostIndex): 0xFF selects the current host. */
+	u8 params[2] = { 0xFF, plat_index };
+
+	ret = hidpp_send_fap_command_sync(hidpp, feat_index,
+					  HIDPP_MULTIPLATFORM_SET_CURRENT_PLATFORM,
+					  params, sizeof(params), &response);
+
+	if (ret)
+		hid_warn(hidpp->hid_dev,
+			 "Multiplatform: SET_CURRENT_PLATFORM failed for index %d (err=%d)",
+			 plat_index, ret);
+
+	return hidpp_multiplatform_errno(ret);
+}
+
+/**
+ * hidpp_multiplatform_set_platform() - Apply a platform to the device
+ * @hidpp: Pointer to the hidpp_device instance
+ * @mask: A single HIDPP_MULTIPLATFORM_PLATFORM_MASK_* bit to apply
+ *
+ * Looks up the device's platform descriptor whose platform mask matches @mask
+ * and instructs the device to switch to it via SET_CURRENT_PLATFORM.
+ *
+ * Return: 0 on success, -EOPNOTSUPP if the device does not implement feature
+ *         0x4531 or exposes no descriptor matching @mask, or another negative
+ *         Linux errno from the underlying HID++ command.
+ */
+static int hidpp_multiplatform_set_platform(struct hidpp_device *hidpp, u16 mask)
+{
+	u8 plat_index;
+	int ret;
+
+	if (!hidpp->multiplatform_feature_index)
+		return -EOPNOTSUPP;
+
+	ret = hidpp_multiplatform_get_platform_index(hidpp,
+			hidpp->multiplatform_feature_index, mask, &plat_index);
+	if (ret)
+		return ret;
+
+	ret = hidpp_multiplatform_update_device_platform(hidpp,
+			hidpp->multiplatform_feature_index, plat_index);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+/**
+ * platform_store() - Set the device platform based on user input
+ * @dev: Pointer to the device instance
+ * @attr: Pointer to the device attribute
+ * @buf: Buffer containing the platform name string
+ * @count: Size of the input buffer
+ *
+ * Parses the platform name from the input buffer, converts it to a platform mask,
+ * and applies it to the device using the HID++ Multi-Platform feature. The function
+ * handles errors gracefully, returning appropriate Linux errno values if the input
+ * is invalid or if the device does not support the requested platform.
+ *
+ * Return: Number of bytes consumed from the input buffer on success, or a negative
+ * Linux errno on failure.
+ */
+static ssize_t platform_store(struct device *dev,
+			      struct device_attribute *attr,
+			      const char *buf, size_t count)
+{
+	struct hid_device *hdev = to_hid_device(dev);
+	struct hidpp_device *hidpp = hid_get_drvdata(hdev);
+	char platform[16];
+	int idx;
+	int ret;
+
+	strscpy(platform, buf, sizeof(platform));
+	string_lower(platform, platform);
+
+	idx = sysfs_match_string(multiplatform_names, platform);
+	if (idx < 0)
+		return idx;
+
+	mutex_lock(&hidpp->multiplatform_lock);
+	ret = hidpp_multiplatform_set_platform(hidpp, multiplatform_masks[idx]);
+	mutex_unlock(&hidpp->multiplatform_lock);
+	if (ret)
+		return ret;
+
+	hid_dbg(hdev, "Multiplatform: Device platform set to '%s'\n",
+		multiplatform_names[idx]);
+
+	return count;
+}
+
+static DEVICE_ATTR_WO(platform);
+
+static struct attribute *multiplatform_attrs[] = {
+	&dev_attr_platform.attr,
+	NULL
+};
+
+static const struct attribute_group multiplatform_attribute_group = {
+	.attrs = multiplatform_attrs,
+};
+
+/**
+ * hidpp_multiplatform_init() - Initialize HID++ Multi-Platform support
+ * @hidpp: Pointer to the hidpp_device instance
+ *
+ * Checks if the device supports the HID++ Multi-Platform feature (0x4531) and, if so,
+ * initializes the hidpp_device structure to track the feature index and creates the
+ * corresponding sysfs attribute group for platform selection.
+ */
+static void hidpp_multiplatform_init(struct hidpp_device *hidpp)
+{
+	struct hid_device *hdev = hidpp->hid_dev;
+	u8 feat_index;
+	int ret;
+
+	ret = hidpp_root_get_feature(hidpp, HIDPP_MULTIPLATFORM_FEAT_ID, &feat_index);
+	if (ret)
+		return;
+
+	hidpp->multiplatform_feature_index = feat_index;
+
+	ret = devm_mutex_init(&hdev->dev, &hidpp->multiplatform_lock);
+	if (ret) {
+		hid_warn(hdev,
+			 "Multiplatform: Failed to init mutex (err=%d)\n", ret);
+		hidpp->multiplatform_feature_index = 0;
+		return;
+	}
+
+	ret = devm_device_add_group(&hdev->dev, &multiplatform_attribute_group);
+	if (ret) {
+		hid_warn(hdev,
+			 "Multiplatform: Failed to create sysfs group (err=%d)\n", ret);
+		hidpp->multiplatform_feature_index = 0;
+	}
+}
+
 static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
 {
 	struct hidpp_device *hidpp;
@@ -4545,6 +4927,8 @@ static int hidpp_probe(struct hid_device *hdev, const struct hid_device_id *id)
 		}
 	}
 
+	hidpp_multiplatform_init(hidpp);
+
 	/*
 	 * This relies on logi_dj_ll_close() being a no-op so that DJ connection
 	 * events will still be received.
diff --git a/drivers/hid/hid-quirks.c b/drivers/hid/hid-quirks.c
index 57d8efdd9b89..577af0075e27 100644
--- a/drivers/hid/hid-quirks.c
+++ b/drivers/hid/hid-quirks.c
@@ -537,6 +537,8 @@ static const struct hid_device_id hid_have_special_driver[] = {
 #endif
 #if IS_ENABLED(CONFIG_HID_LOGITECH_HIDPP)
 	{ HID_USB_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_G920_WHEEL) },
+	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_CASA_KEYS_KEYBOARD) },
+	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_LOGITECH, USB_DEVICE_ID_LOGITECH_MX_KEYS_S_KEYBOARD) },
 #endif
 #if IS_ENABLED(CONFIG_HID_MAGICMOUSE)
 	{ HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MAGICMOUSE) },
-- 
2.34.1


^ permalink raw reply related

* Re: [PATCH v3] HID: logitech-hidpp: Add support for HID++ Multi-Platform feature (0x4531)
From: sashiko-bot @ 2026-06-30 10:13 UTC (permalink / raw)
  To: DevExalt; +Cc: dmitry.torokhov, linux-input
In-Reply-To: <20260630094838.50471-1-exalt.dev.team@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Use-after-destroy of `hidpp->send_mutex` and operations on stopped hardware due to delayed sysfs group removal.
--

commit 55433f57873d505e62b905c239d0015edd306ae5
Author: Baraa Atta (Dev Exalt) <exalt.dev.team@gmail.com>

HID: logitech-hidpp: Add support for HID++ Multi-Platform feature (0x4531)

Add support in the Logitech HID++ driver for the HID++ Multi-Platform
feature (0x4531), which enables HID++ devices to adjust their behavior
based on the host operating system.

> diff --git a/drivers/hid/hid-logitech-hidpp.c b/drivers/hid/hid-logitech-hidpp.c
> index 90b0184df777f..33904f3093803 100644
> --- a/drivers/hid/hid-logitech-hidpp.c
> +++ b/drivers/hid/hid-logitech-hidpp.c
[ ... ]
> @@ -4780,6 +4780,26 @@ static void hidpp_multiplatform_init(struct hidpp_device *hidpp)
>  {
>  	struct hid_device *hdev = hidpp->hid_dev;
>  	u8 feat_index;
>  	int ret;
>  
>  	ret = hidpp_root_get_feature(hidpp, HIDPP_MULTIPLATFORM_FEAT_ID, &feat_index);
>  	if (ret)
>  		return;
>  
>  	hidpp->multiplatform_feature_index = feat_index;
>  
>  	ret = devm_mutex_init(&hdev->dev, &hidpp->multiplatform_lock);
>  	if (ret) {
>  		hid_warn(hdev,
>  			 "Multiplatform: Failed to init mutex (err=%d)\n", ret);
>  		hidpp->multiplatform_feature_index = 0;
>  		return;
>  	}
>  
>  	ret = devm_device_add_group(&hdev->dev, &multiplatform_attribute_group);

[Severity: High]
Does this create a use-after-destroy race condition during device unbind?

Because devm_device_add_group() is used, the sysfs group is scheduled for
removal during the devres phase, which runs after the driver's remove callback
returns.

In hidpp_remove(), the hardware state is explicitly torn down and the mutex is
destroyed:

drivers/hid/hid-logitech-hidpp.c:hidpp_remove() {
	...
	hid_hw_stop(hdev);
	cancel_work_sync(&hidpp->work);
	cancel_work_sync(&hidpp->reset_hi_res_work);
	mutex_destroy(&hidpp->send_mutex);
}

The sysfs attribute remains accessible to userspace during and shortly after
hidpp_remove() runs. A concurrent write to the platform sysfs attribute could
trigger platform_store(), which eventually attempts to lock the destroyed
hidpp->send_mutex and perform I/O on stopped hardware.

>  	if (ret) {
>  		hid_warn(hdev,
>  			 "Multiplatform: Failed to create sysfs group (err=%d)\n", ret);
>  		hidpp->multiplatform_feature_index = 0;
>  	}
>  }

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260630094838.50471-1-exalt.dev.team@gmail.com?part=1

^ permalink raw reply

* [PATCH 00/10] Add support for Apple Silicon DockChannel internal keyboards
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves, Sasha Finkelstein

This series introduces support for the internal keyboards on Apple
Silicon M2 and M3 MacBook models.

On these platforms, built-in input devices are managed by a dedicated
coprocessor running an RTKit-based operating system. Communication
between this coprocessor and the main processor is carried out over a
low-latency hardware byte FIFO interface called DockChannel.

To support this input path, the series introduces a few new components:
- An apple-dockchannel mailbox driver to handle the low-level
  byte-stream FIFO.
- A DockChannel HID transport driver (apple-hid) that boots the
  coprocessor using the RTKit framework and encapsulates the HID
  protocol over the mailbox.
- Minor additions to the apple-rtkit and hid-apple drivers to support
  the TraceKit endpoint and integrate the keyboards into the existing
  input-quirks framework.
- Devicetree bindings and DTS updates to describe the nodes on M2 and
  M3 laptops.

The transport driver is based on an original out-of-tree implementation
by Hector Martin, but it has been significantly rewritten for upstream
inclusion to use the standard Linux mailbox framework and align better
with upstream HID design patterns and reduce reliance on hacks.

While the coprocessor manages both the keyboard and the trackpad, this
series only enables keyboard support. The keyboard can be initialised
without loading external firmware, whereas the trackpad requires
firmware. Trackpad support will be submitted in a subsequent series
once these base transport layers are established.

Tested on: MacBook Air M3 (J613).

Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
Michael Reeves (9):
      dt-bindings: mailbox: Add Apple t8122 ASC mailbox
      dt-bindings: mailbox: apple: Add DockChannel mailbox
      dt-bindings: iommu: apple,dart: Add t8122 compatible
      dt-bindings: input: apple: Add DockChannel HID transport
      mailbox: apple: Add DockChannel FIFO controller
      HID: apple: Add support for DockChannel HID keyboards
      HID: apple: Add DockChannel HID transport driver
      arm64: dts: apple: Add MTP DockChannel HID nodes
      arm64: dts: apple: Enable DockChannel HID on M2 and M3 laptops

Sasha Finkelstein (1):
      soc: apple: rtkit: Add tracekit endpoint

 .../bindings/input/apple,dockchannel-hid.yaml      |   91 ++
 .../devicetree/bindings/iommu/apple,dart.yaml      |    4 +-
 .../bindings/mailbox/apple,dockchannel.yaml        |   75 ++
 .../devicetree/bindings/mailbox/apple,mailbox.yaml |    1 +
 MAINTAINERS                                        |    5 +
 arch/arm64/boot/dts/apple/t602x-die0.dtsi          |   46 +
 arch/arm64/boot/dts/apple/t602x-j414-j416.dtsi     |   25 +
 arch/arm64/boot/dts/apple/t8112-j413.dts           |   20 +
 arch/arm64/boot/dts/apple/t8112-j415.dts           |   20 +
 arch/arm64/boot/dts/apple/t8112-j493.dts           |   22 +-
 arch/arm64/boot/dts/apple/t8112.dtsi               |   46 +
 arch/arm64/boot/dts/apple/t8122-j504.dts           |   22 +
 arch/arm64/boot/dts/apple/t8122-j613.dts           |   23 +
 arch/arm64/boot/dts/apple/t8122-j615.dts           |   23 +
 arch/arm64/boot/dts/apple/t8122.dtsi               |   47 +
 drivers/hid/Kconfig                                |    2 +
 drivers/hid/Makefile                               |    2 +
 drivers/hid/dockchannel/Kconfig                    |   15 +
 drivers/hid/dockchannel/Makefile                   |    3 +
 drivers/hid/dockchannel/apple-hid.c                | 1130 ++++++++++++++++++++
 drivers/hid/hid-apple.c                            |  139 ++-
 drivers/mailbox/Kconfig                            |   12 +
 drivers/mailbox/Makefile                           |    2 +
 drivers/mailbox/apple-dockchannel.c                |  380 +++++++
 drivers/soc/apple/rtkit.c                          |    2 +
 include/linux/hid.h                                |    1 +
 include/linux/mailbox/apple-dockchannel.h          |   29 +
 27 files changed, 2139 insertions(+), 48 deletions(-)
---
base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482
change-id: 20260629-apple-mtp-keyboard-final-91cb5a6ff4fc

Best regards,
--  
Michael Reeves <michael.reeves077@gmail.com>



^ permalink raw reply

* [PATCH 01/10] dt-bindings: mailbox: Add Apple t8122 ASC mailbox
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Michael Reeves <michael.reeves077@gmail.com>

The ASC mailbox on t8122 is compatible with the v4 mailbox block used
by other Apple Silicon SoCs.

Document the t8122 compatible in the v4 mailbox section so M3 device
trees can describe their coprocessor mailboxes.

Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml b/Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml
index 28985cc62c25..946c909c6922 100644
--- a/Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml
+++ b/Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml
@@ -30,6 +30,7 @@ properties:
           - enum:
               - apple,t8103-asc-mailbox
               - apple,t8112-asc-mailbox
+              - apple,t8122-asc-mailbox
               - apple,t6000-asc-mailbox
               - apple,t6020-asc-mailbox
           - const: apple,asc-mailbox-v4

-- 
2.51.2



^ permalink raw reply related

* [PATCH 02/10] dt-bindings: mailbox: apple: Add DockChannel mailbox
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Michael Reeves <michael.reeves077@gmail.com>

DockChannel is a FIFO and interrupt block used by Apple coprocessors
to exchange byte-stream traffic with the AP.

Describe it as a mailbox provider so HID and future serial clients can
use the common mailbox API.

Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 .../bindings/mailbox/apple,dockchannel.yaml        | 75 ++++++++++++++++++++++
 MAINTAINERS                                        |  1 +
 2 files changed, 76 insertions(+)

diff --git a/Documentation/devicetree/bindings/mailbox/apple,dockchannel.yaml b/Documentation/devicetree/bindings/mailbox/apple,dockchannel.yaml
new file mode 100644
index 000000000000..4f326d8f3d5a
--- /dev/null
+++ b/Documentation/devicetree/bindings/mailbox/apple,dockchannel.yaml
@@ -0,0 +1,75 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mailbox/apple,dockchannel.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Apple DockChannel FIFO Mailbox
+
+maintainers:
+  - Michael Reeves <michael.reeves077@gmail.com>
+
+description:
+  DockChannel is a hardware FIFO and interrupt block used on Apple SoCs for
+  low-latency byte stream communication with co-processors.
+
+properties:
+  compatible:
+    oneOf:
+      - items:
+          - const: apple,t8112-dockchannel
+      - items:
+          - enum:
+              - apple,t6020-dockchannel
+              - apple,t8122-dockchannel
+          - const: apple,t8112-dockchannel
+
+  reg:
+    items:
+      - description: Interrupt control registers
+      - description: FIFO configuration registers
+      - description: FIFO data registers
+
+  reg-names:
+    items:
+      - const: irq
+      - const: config
+      - const: data
+
+  interrupts:
+    maxItems: 1
+
+  "#mbox-cells":
+    const: 0
+
+  nonposted-mmio: true
+
+required:
+  - compatible
+  - reg
+  - reg-names
+  - interrupts
+  - "#mbox-cells"
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/apple-aic.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    soc {
+        #address-cells = <2>;
+        #size-cells = <2>;
+
+        mailbox@24eb14000 {
+            compatible = "apple,t8112-dockchannel";
+            reg = <0x2 0x4eb14000 0x0 0x4000>,
+                  <0x2 0x4eb30000 0x0 0x4000>,
+                  <0x2 0x4eb34000 0x0 0x4000>;
+            reg-names = "irq", "config", "data";
+            interrupt-parent = <&aic>;
+            interrupts = <AIC_IRQ 850 IRQ_TYPE_LEVEL_HIGH>;
+            #mbox-cells = <0>;
+        };
+    };
diff --git a/MAINTAINERS b/MAINTAINERS
index 15011f5752a9..741974f0f326 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2594,6 +2594,7 @@ F:	Documentation/devicetree/bindings/interrupt-controller/apple,*
 F:	Documentation/devicetree/bindings/iommu/apple,dart.yaml
 F:	Documentation/devicetree/bindings/iommu/apple,sart.yaml
 F:	Documentation/devicetree/bindings/leds/backlight/apple,dwi-bl.yaml
+F:	Documentation/devicetree/bindings/mailbox/apple,dockchannel.yaml
 F:	Documentation/devicetree/bindings/mailbox/apple,mailbox.yaml
 F:	Documentation/devicetree/bindings/mfd/apple,smc.yaml
 F:	Documentation/devicetree/bindings/net/bluetooth/brcm,bcm4377-bluetooth.yaml

-- 
2.51.2



^ permalink raw reply related

* [PATCH 03/10] dt-bindings: iommu: apple,dart: Add t8122 compatible
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Michael Reeves <michael.reeves077@gmail.com>

The MTP block on t8122 has its own DART instance for coprocessor DMA.

Document the t8122 compatible with the existing t8110 fallback because
the programming model is shared.

Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 Documentation/devicetree/bindings/iommu/apple,dart.yaml | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/iommu/apple,dart.yaml b/Documentation/devicetree/bindings/iommu/apple,dart.yaml
index 47ec7fa52c3a..e179199dbd3b 100644
--- a/Documentation/devicetree/bindings/iommu/apple,dart.yaml
+++ b/Documentation/devicetree/bindings/iommu/apple,dart.yaml
@@ -29,7 +29,9 @@ properties:
           - apple,t8110-dart
           - apple,t6000-dart
       - items:
-          - const: apple,t6020-dart
+          - enum:
+              - apple,t6020-dart
+              - apple,t8122-dart
           - const: apple,t8110-dart
 
   reg:

-- 
2.51.2



^ permalink raw reply related

* [PATCH 04/10] dt-bindings: input: apple: Add DockChannel HID transport
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Michael Reeves <michael.reeves077@gmail.com>

Apple internal keyboards and trackpads behind MTP are exposed through a
DockChannel HID transport.

Add the client binding tying together the RTKit ASC mailbox,
DockChannel mailbox, and MTP DART. The keyboard child can provide the
HID country code used by hid-apple to distinguish layout variants.

Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 .../bindings/input/apple,dockchannel-hid.yaml      | 91 ++++++++++++++++++++++
 MAINTAINERS                                        |  1 +
 2 files changed, 92 insertions(+)

diff --git a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml
new file mode 100644
index 000000000000..dbba4fc38971
--- /dev/null
+++ b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml
@@ -0,0 +1,91 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/input/apple,dockchannel-hid.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Apple DockChannel HID Transport
+
+maintainers:
+  - Michael Reeves <michael.reeves077@gmail.com>
+
+description:
+  HID transport for keyboard and trackpad devices connected via the
+  DockChannel FIFO mailbox on Apple Silicon SoCs. The endpoint includes a
+  small RTKit coprocessor called MTP which must be booted before the HID
+  transport becomes available.
+
+properties:
+  compatible:
+    oneOf:
+      - items:
+          - const: apple,t8112-dockchannel-hid
+      - items:
+          - enum:
+              - apple,t6020-dockchannel-hid
+              - apple,t8122-dockchannel-hid
+          - const: apple,t8112-dockchannel-hid
+
+  reg:
+    items:
+      - description: Coprocessor ASC registers
+      - description: Coprocessor SRAM/mailbox registers
+
+  reg-names:
+    items:
+      - const: coproc-asc
+      - const: coproc-sram
+
+  mboxes:
+    items:
+      - description: ASC mailbox used for RTKit control
+      - description: DockChannel FIFO mailbox used for HID packets
+
+  mbox-names:
+    items:
+      - const: asc
+      - const: dockchannel
+
+  iommus:
+    maxItems: 1
+
+  keyboard:
+    type: object
+    properties:
+      hid-country-code:
+        $ref: /schemas/types.yaml#/definitions/uint32
+        description:
+          HID country code for the keyboard layout variant.
+
+    additionalProperties: false
+
+required:
+  - compatible
+  - reg
+  - reg-names
+  - mboxes
+  - mbox-names
+  - iommus
+
+additionalProperties: false
+
+examples:
+  - |
+    soc {
+        #address-cells = <2>;
+        #size-cells = <2>;
+
+        hid@24e400000 {
+            compatible = "apple,t8112-dockchannel-hid";
+            reg = <0x2 0x4e400000 0x0 0x4000>,
+                  <0x2 0x4ec00000 0x0 0x100000>;
+            reg-names = "coproc-asc", "coproc-sram";
+            mboxes = <&mtp_mbox>, <&mtp_dockchannel>;
+            mbox-names = "asc", "dockchannel";
+            iommus = <&mtp_dart 1>;
+
+            keyboard {
+                hid-country-code = <0>;
+            };
+        };
+    };
diff --git a/MAINTAINERS b/MAINTAINERS
index 741974f0f326..1f3c2cdb6e19 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2589,6 +2589,7 @@ F:	Documentation/devicetree/bindings/gpio/apple,smc-gpio.yaml
 F:	Documentation/devicetree/bindings/gpu/apple,agx.yaml
 F:	Documentation/devicetree/bindings/hwmon/apple,smc-hwmon.yaml
 F:	Documentation/devicetree/bindings/i2c/apple,i2c.yaml
+F:	Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml
 F:	Documentation/devicetree/bindings/input/touchscreen/apple,z2-multitouch.yaml
 F:	Documentation/devicetree/bindings/interrupt-controller/apple,*
 F:	Documentation/devicetree/bindings/iommu/apple,dart.yaml

-- 
2.51.2



^ permalink raw reply related

* [PATCH 07/10] HID: apple: Add support for DockChannel HID keyboards
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Michael Reeves <michael.reeves077@gmail.com>

DockChannel keyboards are registered as host-bus Apple HID devices
instead of USB or Bluetooth devices.

Match them in hid-apple, use the modern Magic Keyboard function-key
table, and fix up the oversized report-size descriptor pattern before
parsing.

Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 drivers/hid/hid-apple.c | 139 ++++++++++++++++++++++++++++++++----------------
 include/linux/hid.h     |   1 +
 2 files changed, 94 insertions(+), 46 deletions(-)

diff --git a/drivers/hid/hid-apple.c b/drivers/hid/hid-apple.c
index bf7dd0fbf249..47c6ec09d5fa 100644
--- a/drivers/hid/hid-apple.c
+++ b/drivers/hid/hid-apple.c
@@ -390,6 +390,12 @@ static bool apple_is_omoton_kb066(struct hid_device *hdev)
 		strcmp(hdev->name, "Bluetooth Keyboard") == 0;
 }
 
+static bool apple_is_dockchannel_keyboard(struct hid_device *hdev)
+{
+	return hdev->bus == BUS_HOST &&
+	       hdev->group == HID_GROUP_APPLE_DOCKCHANNEL;
+}
+
 static inline void apple_setup_key_translation(struct input_dev *input,
 		const struct apple_key_translation *table)
 {
@@ -477,53 +483,57 @@ static int hidinput_apple_event(struct hid_device *hid, struct input_dev *input,
 		asc->fn_on = !!value;
 
 	if (real_fnmode) {
-		switch (hid->product) {
-		case USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI:
-		case USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO:
-		case USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS:
-		case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI:
-		case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO:
-		case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS:
-		case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI:
-		case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO:
-		case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS:
-			table = magic_keyboard_alu_fn_keys;
-			break;
-		case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015:
-		case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015:
-			table = magic_keyboard_2015_fn_keys;
-			break;
-		case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021:
-		case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021:
-		case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021:
-		case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2024:
-		case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2024:
-		case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2024:
+		if (apple_is_dockchannel_keyboard(hid)) {
 			table = magic_keyboard_2021_and_2024_fn_keys;
-			break;
-		case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132:
-		case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213:
-		case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680:
-		case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680_ALT:
-			table = macbookpro_no_esc_fn_keys;
-			break;
-		case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F:
-		case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K:
-		case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223:
-			table = macbookpro_dedicated_esc_fn_keys;
-			break;
-		case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K:
-		case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K:
-			table = apple_fn_keys;
-			break;
-		default:
-			if (hid->product >= USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI &&
-			    hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS)
-				table = macbookair_fn_keys;
-			else if (hid->product < 0x21d || hid->product >= 0x300)
-				table = powerbook_fn_keys;
-			else
+		} else {
+			switch (hid->product) {
+			case USB_DEVICE_ID_APPLE_ALU_WIRELESS_ANSI:
+			case USB_DEVICE_ID_APPLE_ALU_WIRELESS_ISO:
+			case USB_DEVICE_ID_APPLE_ALU_WIRELESS_JIS:
+			case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ANSI:
+			case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_ISO:
+			case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2009_JIS:
+			case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ANSI:
+			case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_ISO:
+			case USB_DEVICE_ID_APPLE_ALU_WIRELESS_2011_JIS:
+				table = magic_keyboard_alu_fn_keys;
+				break;
+			case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2015:
+			case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2015:
+				table = magic_keyboard_2015_fn_keys;
+				break;
+			case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2021:
+			case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2021:
+			case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2021:
+			case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_2024:
+			case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_FINGERPRINT_2024:
+			case USB_DEVICE_ID_APPLE_MAGIC_KEYBOARD_NUMPAD_2024:
+				table = magic_keyboard_2021_and_2024_fn_keys;
+				break;
+			case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J132:
+			case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J213:
+			case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680:
+			case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J680_ALT:
+				table = macbookpro_no_esc_fn_keys;
+				break;
+			case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J152F:
+			case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J214K:
+			case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J223:
+				table = macbookpro_dedicated_esc_fn_keys;
+				break;
+			case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J140K:
+			case USB_DEVICE_ID_APPLE_WELLSPRINGT2_J230K:
 				table = apple_fn_keys;
+				break;
+			default:
+				if (hid->product >= USB_DEVICE_ID_APPLE_WELLSPRING4_ANSI &&
+				    hid->product <= USB_DEVICE_ID_APPLE_WELLSPRING4A_JIS)
+					table = macbookair_fn_keys;
+				else if (hid->product < 0x21d || hid->product >= 0x300)
+					table = powerbook_fn_keys;
+				else
+					table = apple_fn_keys;
+			}
 		}
 
 		trans = apple_find_translation(table, code);
@@ -659,6 +669,7 @@ static void apple_battery_timer_tick(struct timer_list *t)
 /*
  * MacBook JIS keyboard has wrong logical maximum
  * Magic Keyboard JIS has wrong logical maximum
+ * Internal DockChannel keyboards can advertise oversized report sizes
  */
 static const __u8 *apple_report_fixup(struct hid_device *hdev, __u8 *rdesc,
 		unsigned int *rsize)
@@ -699,6 +710,27 @@ static const __u8 *apple_report_fixup(struct hid_device *hdev, __u8 *rdesc,
 		rdesc[3] = 0x06;
 	}
 
+	if (apple_is_dockchannel_keyboard(hdev) && *rsize >= 5) {
+		int i;
+
+		for (i = 0; i <= *rsize - 5; i++) {
+			if (rdesc[i] == 0x76 && rdesc[i + 1] == 0x00 &&
+			    rdesc[i + 2] == 0x40 && rdesc[i + 3] == 0x95) {
+				u8 count = rdesc[i + 4];
+
+				if (count > 0 && count < 32) {
+					hid_info(hdev,
+						 "fixing up DockChannel report size\n");
+					rdesc[i] = 0x75;
+					rdesc[i + 1] = 0x08;
+					rdesc[i + 2] = 0x96;
+					rdesc[i + 3] = 0x00;
+					rdesc[i + 4] = count * 8;
+				}
+			}
+		}
+	}
+
 	return rdesc;
 }
 
@@ -763,7 +795,7 @@ static int apple_input_configured(struct hid_device *hdev,
 	struct apple_sc *asc = hid_get_drvdata(hdev);
 
 	if (((asc->quirks & APPLE_HAS_FN) && !asc->fn_found) || apple_is_omoton_kb066(hdev)) {
-		hid_info(hdev, "Fn key not found (Apple Wireless Keyboard clone?), disabling Fn key handling\n");
+		hid_info(hdev, "Disabling function quirk for device without function key\n");
 		asc->quirks &= ~APPLE_HAS_FN;
 	}
 
@@ -1003,6 +1035,17 @@ static void apple_remove(struct hid_device *hdev)
 	hid_hw_stop(hdev);
 }
 
+static bool apple_match(struct hid_device *hdev, bool ignore_special_driver)
+{
+	if (ignore_special_driver)
+		return false;
+
+	if (hdev->group == HID_GROUP_APPLE_DOCKCHANNEL)
+		return apple_is_dockchannel_keyboard(hdev);
+
+	return true;
+}
+
 static const struct hid_device_id apple_devices[] = {
 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_MIGHTYMOUSE),
 		.driver_data = APPLE_MIGHTYMOUSE | APPLE_INVERT_HWHEEL },
@@ -1224,6 +1267,9 @@ static const struct hid_device_id apple_devices[] = {
 		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
 	{ HID_USB_DEVICE(USB_VENDOR_ID_APPLE, USB_DEVICE_ID_APPLE_TOUCHBAR_BACKLIGHT),
 		.driver_data = APPLE_MAGIC_BACKLIGHT },
+	{ HID_DEVICE(BUS_HOST, HID_GROUP_APPLE_DOCKCHANNEL,
+		     HID_ANY_ID, HID_ANY_ID),
+		.driver_data = APPLE_HAS_FN | APPLE_ISO_TILDE_QUIRK },
 
 	{ }
 };
@@ -1232,6 +1278,7 @@ MODULE_DEVICE_TABLE(hid, apple_devices);
 static struct hid_driver apple_driver = {
 	.name = "apple",
 	.id_table = apple_devices,
+	.match = apple_match,
 	.report_fixup = apple_report_fixup,
 	.probe = apple_probe,
 	.remove = apple_remove,
diff --git a/include/linux/hid.h b/include/linux/hid.h
index 47dc0bc89fa4..0d40deec6295 100644
--- a/include/linux/hid.h
+++ b/include/linux/hid.h
@@ -441,6 +441,7 @@ struct hid_item {
 #define HID_GROUP_STEAM				0x0103
 #define HID_GROUP_LOGITECH_27MHZ_DEVICE		0x0104
 #define HID_GROUP_VIVALDI			0x0105
+#define HID_GROUP_APPLE_DOCKCHANNEL		0x0106
 
 /*
  * HID protocol status

-- 
2.51.2



^ permalink raw reply related

* [PATCH 08/10] HID: apple: Add DockChannel HID transport driver
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Michael Reeves <michael.reeves077@gmail.com>

Apple MTP exposes internal keyboard and trackpad interfaces over a HID
transport carried by DockChannel.

Add a transport driver that boots the MTP RTKit coprocessor, exchanges
HID packets through the DockChannel mailbox, and registers child HID
interfaces from devicetree.

Co-developed-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 MAINTAINERS                         |    1 +
 drivers/hid/Kconfig                 |    2 +
 drivers/hid/Makefile                |    2 +
 drivers/hid/dockchannel/Kconfig     |   15 +
 drivers/hid/dockchannel/Makefile    |    3 +
 drivers/hid/dockchannel/apple-hid.c | 1130 +++++++++++++++++++++++++++++++++++
 6 files changed, 1153 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index ed68452c0ad6..0063276f0349 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2620,6 +2620,7 @@ F:	drivers/clk/clk-apple-nco.c
 F:	drivers/cpufreq/apple-soc-cpufreq.c
 F:	drivers/dma/apple-admac.c
 F:	drivers/gpio/gpio-macsmc.c
+F:	drivers/hid/dockchannel/
 F:	drivers/hwmon/macsmc-hwmon.c
 F:	drivers/pmdomain/apple/
 F:	drivers/i2c/busses/i2c-pasemi-core.c
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index f9bcaeb66385..f27cda601ede 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -1488,6 +1488,8 @@ source "drivers/hid/surface-hid/Kconfig"
 
 source "drivers/hid/intel-thc-hid/Kconfig"
 
+source "drivers/hid/dockchannel/Kconfig"
+
 endif # HID
 
 # USB support may be used with HID disabled
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 23e6e3dd0c56..c9b4b1aff247 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -182,3 +182,5 @@ obj-$(CONFIG_AMD_SFH_HID)       += amd-sfh-hid/
 obj-$(CONFIG_SURFACE_HID_CORE)  += surface-hid/
 
 obj-$(CONFIG_INTEL_THC_HID)     += intel-thc-hid/
+
+obj-$(CONFIG_APPLE_DOCKCHANNEL_HID)	+= dockchannel/
diff --git a/drivers/hid/dockchannel/Kconfig b/drivers/hid/dockchannel/Kconfig
new file mode 100644
index 000000000000..fca09ef74403
--- /dev/null
+++ b/drivers/hid/dockchannel/Kconfig
@@ -0,0 +1,15 @@
+# SPDX-License-Identifier: GPL-2.0-only OR MIT
+
+config APPLE_DOCKCHANNEL_HID
+	tristate "HID over Apple DockChannel"
+	depends on APPLE_DOCKCHANNEL
+	depends on APPLE_RTKIT
+	depends on HID
+	depends on INPUT
+	depends on OF
+	help
+	  This provides a HID transport layer over the Apple DockChannel
+	  mailbox interface. It is required to support the internal keyboard
+	  and trackpad on M2 and later MacBook models.
+
+	  Say Y here if you have an M2 or later Apple MacBook.
diff --git a/drivers/hid/dockchannel/Makefile b/drivers/hid/dockchannel/Makefile
new file mode 100644
index 000000000000..d1a82aa57a69
--- /dev/null
+++ b/drivers/hid/dockchannel/Makefile
@@ -0,0 +1,3 @@
+# SPDX-License-Identifier: GPL-2.0-only OR MIT
+
+obj-$(CONFIG_APPLE_DOCKCHANNEL_HID)	+= apple-hid.o
diff --git a/drivers/hid/dockchannel/apple-hid.c b/drivers/hid/dockchannel/apple-hid.c
new file mode 100644
index 000000000000..162fcfb5ab1c
--- /dev/null
+++ b/drivers/hid/dockchannel/apple-hid.c
@@ -0,0 +1,1130 @@
+// SPDX-License-Identifier: GPL-2.0-only OR MIT
+/*
+ * Apple DockChannel HID transport driver
+ *
+ * Copyright The Asahi Linux Contributors
+ */
+
+#include <linux/bitfield.h>
+#include <linux/completion.h>
+#include <linux/ctype.h>
+#include <linux/delay.h>
+#include <linux/device.h>
+#include <linux/dma-mapping.h>
+#include <linux/hid.h>
+#include <linux/mailbox/apple-dockchannel.h>
+#include <linux/mailbox_client.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/property.h>
+#include <linux/slab.h>
+#include <linux/soc/apple/rtkit.h>
+#include <linux/spinlock.h>
+#include <linux/string.h>
+#include <linux/unaligned.h>
+#include <linux/workqueue.h>
+
+#define APPLE_ASC_CPU_CONTROL		0x44
+#define APPLE_ASC_CPU_CONTROL_RUN	BIT(4)
+
+#define COMMAND_TIMEOUT_MS	1000
+#define START_TIMEOUT_MS	2000
+
+#define MAX_INTERFACES		16
+
+#define DCHID_MAX_PAYLOAD	0xffff
+#define DCHID_CHECKSUM_LEN	4
+#define DCHID_RX_BUF_SIZE	(sizeof(struct dchid_hdr) + DCHID_MAX_PAYLOAD + \
+				 DCHID_CHECKSUM_LEN)
+
+#define DCHID_CHANNEL_CMD	0x11
+#define DCHID_CHANNEL_REPORT	0x12
+#define DCHID_CHECKSUM_SEED	0xffffffff
+
+struct dchid_hdr {
+	u8 hdr_len;
+	u8 channel;
+	__le16 length;
+	u8 seq;
+	u8 iface;
+	__le16 pad;
+} __packed;
+
+#define IFACE_COMM	0
+
+#define FLAGS_GROUP	GENMASK(7, 6)
+#define FLAGS_REQ	GENMASK(5, 0)
+
+#define REQ_SET_REPORT	0
+#define REQ_GET_REPORT	1
+
+struct dchid_subhdr {
+	u8 flags;
+	u8 unk;
+	__le16 length;
+	__le32 retcode;
+} __packed;
+
+#define EVENT_INIT	0xf0
+#define EVENT_READY	0xf1
+
+struct dchid_init_hdr {
+	u8 type;
+	u8 unk1;
+	u8 unk2;
+	u8 iface;
+	char name[16];
+	u8 more_packets;
+	u8 unkpad;
+} __packed;
+
+#define INIT_HID_DESCRIPTOR	0
+#define INIT_TERMINATOR		2
+#define INIT_PRODUCT_NAME	7
+
+#define CMD_RESET_INTERFACE	0x40
+#define CMD_RESET_INTERFACE_SUB	1
+#define CMD_ENABLE_INTERFACE	0xb4
+
+struct dchid_init_block_hdr {
+	__le16 type;
+	__le16 length;
+} __packed;
+
+#define STM_REPORT_ID		0x10
+#define STM_REPORT_SERIAL	0x11
+
+struct dchid_stm_id {
+	u8 unk;
+	__le16 vendor_id;
+	__le16 product_id;
+	__le16 version_number;
+	u8 unk2;
+	u8 unk3;
+	u8 keyboard_type;
+	u8 serial_length;
+	/* Serial follows, but we grab it with a different report. */
+} __packed;
+
+struct dchid_work {
+	struct work_struct work;
+	struct dchid_iface *iface;
+
+	struct dchid_hdr hdr;
+	u8 data[];
+};
+
+struct dchid_iface {
+	struct dchid_dev *dchid;
+	struct hid_device *hid;
+	struct workqueue_struct *wq;
+
+	bool creating;
+	struct work_struct create_work;
+
+	int index;
+	const char *name;
+	struct fwnode_handle *fwnode;
+
+	u8 tx_seq;
+	bool deferred;
+	bool starting;
+	bool open;
+	struct completion ready;
+
+	void *hid_desc;
+	size_t hid_desc_len;
+
+	/* Lock for command submission state below */
+	spinlock_t out_lock;
+	u32 out_flags;
+	int out_report;
+	u32 retcode;
+	void *resp_buf;
+	size_t resp_size;
+	struct completion out_complete;
+};
+
+struct dchid_dev {
+	struct device *dev;
+	struct mbox_client dc_mbox_client;
+	struct mbox_chan *dc_mbox;
+
+	struct apple_rtkit *rtk;
+	void __iomem *asc_base;
+	void __iomem *sram_base;
+	struct resource sram_res;
+
+	bool id_ready;
+	struct dchid_stm_id device_id;
+	char serial[64];
+
+	u8 *rx_buf;
+	size_t rx_len;
+
+	struct dchid_iface *comm;
+	struct mutex ifaces_lock; /* protects ifaces array */
+	struct dchid_iface *ifaces[MAX_INTERFACES];
+
+	/* Workqueue to asynchronously create HID devices */
+	struct workqueue_struct *new_iface_wq;
+};
+
+static void dchid_destroy_wq(void *data)
+{
+	struct workqueue_struct *wq = data;
+
+	destroy_workqueue(wq);
+}
+
+static void dchid_fwnode_release(void *data)
+{
+	fwnode_handle_put(data);
+}
+
+static void dchid_free_mbox(void *data)
+{
+	mbox_free_channel(data);
+}
+
+static u32 dchid_checksum(const void *data, size_t len)
+{
+	const u8 *p = data;
+	u32 sum = 0;
+	int i;
+
+	while (len >= sizeof(u32)) {
+		sum += get_unaligned_le32(p);
+		p += sizeof(u32);
+		len -= sizeof(u32);
+	}
+
+	if (len) {
+		u32 tmp = 0;
+
+		for (i = 0; i < len; i++)
+			tmp |= p[i] << (i * 8);
+		sum += tmp;
+	}
+
+	return sum;
+}
+
+static struct dchid_iface *
+dchid_get_interface(struct dchid_dev *dchid, int index, const char *name)
+{
+	struct dchid_iface *iface;
+	struct fwnode_handle *fwnode;
+	int ret;
+
+	if (index >= MAX_INTERFACES) {
+		dev_err(dchid->dev, "interface index %d out of range\n", index);
+		return NULL;
+	}
+
+	mutex_lock(&dchid->ifaces_lock);
+	if (dchid->ifaces[index]) {
+		iface = dchid->ifaces[index];
+		mutex_unlock(&dchid->ifaces_lock);
+		return iface;
+	}
+
+	iface = devm_kzalloc(dchid->dev, sizeof(*iface), GFP_KERNEL);
+	if (!iface) {
+		mutex_unlock(&dchid->ifaces_lock);
+		return NULL;
+	}
+
+	iface->index = index;
+	iface->name = devm_kstrdup(dchid->dev, name, GFP_KERNEL);
+	if (!iface->name) {
+		mutex_unlock(&dchid->ifaces_lock);
+		return NULL;
+	}
+
+	iface->dchid = dchid;
+	iface->out_report = -1;
+	init_completion(&iface->out_complete);
+	init_completion(&iface->ready);
+	spin_lock_init(&iface->out_lock);
+
+	iface->wq = alloc_ordered_workqueue("dchid-%s", 0, iface->name);
+	if (!iface->wq) {
+		mutex_unlock(&dchid->ifaces_lock);
+		return NULL;
+	}
+
+	ret = devm_add_action_or_reset(dchid->dev, dchid_destroy_wq, iface->wq);
+	if (ret) {
+		mutex_unlock(&dchid->ifaces_lock);
+		return NULL;
+	}
+
+	if (!strcmp(name, "comm")) {
+		/* Comm is not a HID subdevice */
+		dchid->ifaces[index] = iface;
+		mutex_unlock(&dchid->ifaces_lock);
+		return iface;
+	}
+
+	fwnode = device_get_named_child_node(dchid->dev, name);
+	if (fwnode) {
+		iface->fwnode = fwnode;
+		ret = devm_add_action_or_reset(dchid->dev, dchid_fwnode_release,
+					       iface->fwnode);
+		if (ret) {
+			mutex_unlock(&dchid->ifaces_lock);
+			return NULL;
+		}
+	} else {
+		iface->fwnode = dev_fwnode(dchid->dev);
+	}
+
+	dchid->ifaces[index] = iface;
+	mutex_unlock(&dchid->ifaces_lock);
+	return iface;
+}
+
+static int dchid_send(struct dchid_iface *iface, u32 flags, const void *msg,
+		      size_t size)
+{
+	struct dchid_dev *dchid = iface->dchid;
+	size_t payload_padded = round_up(size, sizeof(u32));
+	size_t total_len = sizeof(struct dchid_hdr) + sizeof(struct dchid_subhdr) +
+			   payload_padded + DCHID_CHECKSUM_LEN;
+	struct apple_dockchannel_msg dc_msg;
+	struct dchid_hdr *hdr;
+	struct dchid_subhdr *sub;
+	u32 *checksum_ptr;
+	u8 *buf;
+	int ret;
+
+	if (total_len > DCHID_RX_BUF_SIZE)
+		return -EINVAL;
+
+	buf = kzalloc(total_len, GFP_KERNEL);
+	if (!buf)
+		return -ENOMEM;
+
+	hdr = (struct dchid_hdr *)buf;
+	sub = (struct dchid_subhdr *)(buf + sizeof(*hdr));
+	checksum_ptr = (u32 *)(buf + total_len - DCHID_CHECKSUM_LEN);
+
+	hdr->hdr_len = sizeof(*hdr);
+	hdr->channel = DCHID_CHANNEL_CMD;
+	hdr->length = cpu_to_le16(payload_padded + sizeof(*sub));
+	hdr->seq = iface->tx_seq;
+	hdr->iface = iface->index;
+
+	sub->flags = (u8)flags;
+	sub->length = cpu_to_le16(size);
+
+	memcpy(buf + sizeof(*hdr) + sizeof(*sub), msg, size);
+
+	*checksum_ptr = 0xffffffff - dchid_checksum(buf, total_len - DCHID_CHECKSUM_LEN);
+
+	dc_msg.data = buf;
+	dc_msg.len = total_len;
+	ret = mbox_send_message(dchid->dc_mbox, &dc_msg);
+	kfree(buf);
+
+	return ret < 0 ? ret : 0;
+}
+
+static int dchid_cmd(struct dchid_iface *iface, u32 type, u32 req,
+		     void *data, size_t size, void *resp_buf, size_t resp_size)
+{
+	unsigned long flags;
+	int ret;
+	int report_id;
+	bool timed_out = false;
+	u32 out_flags;
+
+	if (size < 1)
+		return -EINVAL;
+
+	report_id = *(u8 *)data;
+	out_flags = FIELD_PREP(FLAGS_GROUP, type) | FIELD_PREP(FLAGS_REQ, req);
+
+	spin_lock_irqsave(&iface->out_lock, flags);
+
+	/* Only one command can be in flight per interface */
+	if (WARN_ON(iface->out_report != -1)) {
+		spin_unlock_irqrestore(&iface->out_lock, flags);
+		return -EBUSY;
+	}
+
+	iface->out_report = report_id;
+	iface->out_flags = out_flags;
+	iface->retcode = 0;
+	iface->resp_buf = resp_buf;
+	iface->resp_size = resp_size;
+	reinit_completion(&iface->out_complete);
+
+	spin_unlock_irqrestore(&iface->out_lock, flags);
+
+	ret = dchid_send(iface, out_flags, data, size);
+	if (ret < 0) {
+		spin_lock_irqsave(&iface->out_lock, flags);
+		iface->out_report = -1;
+		iface->resp_buf = NULL;
+		iface->resp_size = 0;
+		spin_unlock_irqrestore(&iface->out_lock, flags);
+		return ret;
+	}
+
+	if (!wait_for_completion_timeout(&iface->out_complete,
+					 msecs_to_jiffies(COMMAND_TIMEOUT_MS))) {
+		dev_err(iface->dchid->dev, "command 0x%x to iface %d (%s) timed out\n",
+			report_id, iface->index, iface->name);
+		timed_out = true;
+	}
+
+	spin_lock_irqsave(&iface->out_lock, flags);
+
+	if (timed_out && iface->out_report == report_id) {
+		ret = -ETIMEDOUT;
+	} else if (iface->retcode) {
+		dev_err(iface->dchid->dev,
+			"command 0x%x to iface %d (%s) failed with err 0x%x\n",
+			report_id, iface->index, iface->name, iface->retcode);
+		ret = -EIO;
+	} else {
+		ret = iface->resp_size;
+	}
+
+	iface->tx_seq++;
+	iface->out_report = -1;
+	iface->resp_buf = NULL;
+	iface->resp_size = 0;
+	spin_unlock_irqrestore(&iface->out_lock, flags);
+
+	return ret;
+}
+
+static int dchid_comm_cmd(struct dchid_dev *dchid, void *cmd, size_t size)
+{
+	return dchid_cmd(dchid->comm, HID_FEATURE_REPORT, REQ_SET_REPORT,
+			 cmd, size, NULL, 0);
+}
+
+static int dchid_enable_interface(struct dchid_iface *iface)
+{
+	u8 cmd[] = { CMD_ENABLE_INTERFACE, iface->index };
+
+	return dchid_comm_cmd(iface->dchid, cmd, sizeof(cmd));
+}
+
+static int dchid_reset_interface(struct dchid_iface *iface, int state)
+{
+	u8 cmd[] = { CMD_RESET_INTERFACE, CMD_RESET_INTERFACE_SUB, iface->index,
+		     (u8)state };
+
+	return dchid_comm_cmd(iface->dchid, cmd, sizeof(cmd));
+}
+
+static int dchid_start_interface(struct dchid_iface *iface)
+{
+	if (iface->starting)
+		return -EINPROGRESS;
+
+	dev_dbg(iface->dchid->dev, "starting interface %s\n", iface->name);
+
+	iface->starting = true;
+	dchid_reset_interface(iface, 0);
+	dchid_reset_interface(iface, 2);
+
+	return 0;
+}
+
+static int dchid_start(struct hid_device *hdev)
+{
+	return 0;
+}
+
+static int dchid_open(struct hid_device *hdev)
+{
+	struct dchid_iface *iface = hdev->driver_data;
+	int ret;
+
+	if (!completion_done(&iface->ready)) {
+		ret = dchid_start_interface(iface);
+		if (ret < 0)
+			return ret;
+
+		if (!wait_for_completion_timeout(&iface->ready,
+						 msecs_to_jiffies(START_TIMEOUT_MS))) {
+			dev_err(iface->dchid->dev, "iface %s start timed out\n",
+				iface->name);
+			return -ETIMEDOUT;
+		}
+	}
+
+	iface->open = true;
+	return 0;
+}
+
+static void dchid_close(struct hid_device *hdev)
+{
+	struct dchid_iface *iface = hdev->driver_data;
+
+	iface->open = false;
+}
+
+static int dchid_parse(struct hid_device *hdev)
+{
+	struct dchid_iface *iface = hdev->driver_data;
+
+	return hid_parse_report(hdev, iface->hid_desc, iface->hid_desc_len);
+}
+
+/* Note: buf excludes report number. */
+static int dchid_get_report_cmd(struct dchid_iface *iface, u8 reportnum,
+				void *buf, size_t len)
+{
+	int ret;
+
+	ret = dchid_cmd(iface, HID_FEATURE_REPORT, REQ_GET_REPORT, &reportnum, 1,
+			buf, len);
+
+	return ret <= 0 ? ret : ret - 1;
+}
+
+/* Note: buf includes report number. */
+static int dchid_set_report(struct dchid_iface *iface, void *buf, size_t len)
+{
+	return dchid_cmd(iface, HID_OUTPUT_REPORT, REQ_SET_REPORT, buf, len,
+			 NULL, 0);
+}
+
+static int dchid_raw_request(struct hid_device *hdev, unsigned char reportnum,
+			     __u8 *buf, size_t len, unsigned char rtype,
+			     int reqtype)
+{
+	struct dchid_iface *iface = hdev->driver_data;
+
+	switch (reqtype) {
+	case HID_REQ_GET_REPORT:
+		if (len < 1)
+			return -EINVAL;
+
+		buf[0] = reportnum;
+		return dchid_cmd(iface, rtype, REQ_GET_REPORT, &reportnum, 1,
+				 buf + 1, len - 1);
+	case HID_REQ_SET_REPORT:
+		return dchid_set_report(iface, buf, len);
+	default:
+		return -EIO;
+	}
+}
+
+static const struct hid_ll_driver dchid_ll = {
+	.start = dchid_start,
+	.open = dchid_open,
+	.close = dchid_close,
+	.parse = dchid_parse,
+	.raw_request = dchid_raw_request,
+};
+
+static void dchid_create_interface_work(struct work_struct *ws)
+{
+	struct dchid_iface *iface = container_of(ws, struct dchid_iface, create_work);
+	struct dchid_dev *dchid = iface->dchid;
+	struct hid_device *hid;
+	char cap_name[16];
+	int ret;
+
+	if (iface->hid) {
+		dev_warn(dchid->dev, "interface %s already created\n", iface->name);
+		goto done;
+	}
+
+	ret = dchid_enable_interface(iface);
+	if (ret < 0) {
+		dev_warn(dchid->dev, "failed to enable %s: %d\n", iface->name, ret);
+		goto done;
+	}
+
+	iface->deferred = false;
+
+	hid = hid_allocate_device();
+	if (IS_ERR(hid))
+		goto done;
+
+	strscpy(cap_name, iface->name, sizeof(cap_name));
+	if (cap_name[0])
+		cap_name[0] = toupper(cap_name[0]);
+	snprintf(hid->name, sizeof(hid->name), "Apple DockChannel %s", cap_name);
+
+	snprintf(hid->phys, sizeof(hid->phys), "%s.%d", dev_name(dchid->dev),
+		 iface->index);
+	strscpy(hid->uniq, dchid->serial, sizeof(hid->uniq));
+
+	hid->ll_driver = &dchid_ll;
+	hid->bus = BUS_HOST;
+	hid->vendor = le16_to_cpu(dchid->device_id.vendor_id);
+	hid->product = le16_to_cpu(dchid->device_id.product_id);
+	hid->version = le16_to_cpu(dchid->device_id.version_number);
+	hid->type = HID_TYPE_OTHER;
+	if (!strcmp(iface->name, "keyboard")) {
+		u32 country_code;
+
+		hid->group = HID_GROUP_APPLE_DOCKCHANNEL;
+
+		/*
+		 * The device provides no reliable way to get the keyboard
+		 * country code, so board devicetrees provide it instead,
+		 * filled by the bootloader.
+		 */
+		if (!fwnode_property_read_u32(iface->fwnode, "hid-country-code",
+					      &country_code))
+			hid->country = country_code;
+	}
+
+	hid->dev.parent = iface->dchid->dev;
+	hid->driver_data = iface;
+	iface->hid = hid;
+
+	ret = hid_add_device(hid);
+	if (ret < 0) {
+		iface->hid = NULL;
+		hid_destroy_device(hid);
+		dev_warn(iface->dchid->dev, "failed to register HID device %s\n",
+			 iface->name);
+	}
+
+done:
+	iface->creating = false;
+}
+
+static int dchid_create_interface(struct dchid_iface *iface)
+{
+	if (iface->creating)
+		return -EBUSY;
+
+	iface->creating = true;
+	INIT_WORK(&iface->create_work, dchid_create_interface_work);
+	return queue_work(iface->dchid->new_iface_wq, &iface->create_work);
+}
+
+static void dchid_handle_descriptor(struct dchid_iface *iface, void *hid_desc,
+				    size_t desc_len)
+{
+	u8 *rdesc;
+	int i;
+
+	if (iface->hid)
+		return;
+
+	rdesc = devm_kmemdup(iface->dchid->dev, hid_desc, desc_len,
+			     GFP_KERNEL);
+	if (!rdesc)
+		return;
+
+	/* Fix up oversized report sizes in DockChannel report descriptors */
+	if (desc_len >= 5) {
+		for (i = 0; i <= (int)desc_len - 5; i++) {
+			if (rdesc[i] == 0x76 && rdesc[i + 1] == 0x00 &&
+			    rdesc[i + 2] == 0x40 && rdesc[i + 3] == 0x95) {
+				u8 count = rdesc[i + 4];
+
+				if (count > 0 && count < 32) {
+					dev_info(iface->dchid->dev,
+						 "fixing up interface %s (%d) report size\n",
+						 iface->name, iface->index);
+					rdesc[i] = 0x75;
+					rdesc[i + 1] = 0x08;
+					rdesc[i + 2] = 0x96;
+					rdesc[i + 3] = 0x00;
+					rdesc[i + 4] = count * 8;
+				}
+			}
+		}
+	}
+
+	iface->hid_desc = rdesc;
+	iface->hid_desc_len = desc_len;
+}
+
+static void dchid_handle_ready(struct dchid_dev *dchid, void *data, size_t length)
+{
+	struct dchid_iface *iface;
+	u8 *pkt = data;
+	u8 index;
+	int i;
+	int ret;
+
+	if (length < 2)
+		return;
+
+	index = pkt[1];
+	if (index >= MAX_INTERFACES)
+		return;
+
+	iface = dchid->ifaces[index];
+	if (!iface)
+		return;
+
+	dev_dbg(dchid->dev, "interface %s is now ready\n", iface->name);
+	complete_all(&iface->ready);
+
+	/* When STM is ready, grab global device info */
+	if (!strcmp(iface->name, "stm")) {
+		ret = dchid_get_report_cmd(iface, STM_REPORT_ID, &dchid->device_id,
+					   sizeof(dchid->device_id));
+		if (ret < (int)sizeof(dchid->device_id)) {
+			dev_warn(iface->dchid->dev, "failed to get device ID from STM\n");
+			/* Fake it and keep going. Things might still work. */
+			memset(&dchid->device_id, 0, sizeof(dchid->device_id));
+		}
+
+		ret = dchid_get_report_cmd(iface, STM_REPORT_SERIAL, dchid->serial,
+					   sizeof(dchid->serial) - 1);
+		if (ret < 0) {
+			dev_warn(iface->dchid->dev, "failed to get serial from STM\n");
+			dchid->serial[0] = 0;
+		}
+
+		dchid->id_ready = true;
+		for (i = 0; i < MAX_INTERFACES; i++) {
+			if (!dchid->ifaces[i] || !dchid->ifaces[i]->deferred)
+				continue;
+			dchid_create_interface(dchid->ifaces[i]);
+		}
+	}
+}
+
+static void dchid_handle_init(struct dchid_dev *dchid, void *data, size_t length)
+{
+	struct dchid_init_hdr *hdr = data;
+	struct dchid_init_block_hdr *blk;
+	struct dchid_iface *iface;
+	u8 *p = data;
+
+	if (length < sizeof(*hdr))
+		return;
+
+	iface = dchid_get_interface(dchid, hdr->iface, hdr->name);
+	if (!iface)
+		return;
+
+	p += sizeof(*hdr);
+	length -= sizeof(*hdr);
+
+	while (length >= sizeof(*blk)) {
+		u16 blk_len;
+
+		blk = (struct dchid_init_block_hdr *)p;
+		p += sizeof(*blk);
+		length -= sizeof(*blk);
+
+		blk_len = le16_to_cpu(blk->length);
+		if (blk_len > length)
+			break;
+
+		switch (le16_to_cpu(blk->type)) {
+		case INIT_HID_DESCRIPTOR:
+			dchid_handle_descriptor(iface, p, blk_len);
+			break;
+		case INIT_PRODUCT_NAME:
+			if (blk_len > 0 && p[blk_len - 1] != 0)
+				dev_warn(dchid->dev, "unterminated product name for %s\n",
+					 iface->name);
+			break;
+		}
+
+		p += blk_len;
+		length -= blk_len;
+
+		if (le16_to_cpu(blk->type) == INIT_TERMINATOR)
+			break;
+	}
+
+	if (hdr->more_packets)
+		return;
+
+	/*
+	 * Prefer to enable STM first, since it provides device IDs. Some
+	 * firmware versions do not expose STM, so let the keyboard start
+	 * without it.
+	 */
+	if (iface->dchid->id_ready || !strcmp(iface->name, "stm") ||
+	    !strcmp(iface->name, "keyboard"))
+		dchid_create_interface(iface);
+	else
+		iface->deferred = true;
+}
+
+static void dchid_handle_event(struct dchid_dev *dchid, void *data, size_t length)
+{
+	u8 *p = data;
+
+	if (!length)
+		return;
+
+	switch (*p) {
+	case EVENT_INIT:
+		dchid_handle_init(dchid, data, length);
+		break;
+	case EVENT_READY:
+		dchid_handle_ready(dchid, data, length);
+		break;
+	}
+}
+
+static void dchid_handle_report(struct dchid_iface *iface, void *data, size_t length)
+{
+	if (!iface->hid || !iface->open)
+		return;
+
+	hid_input_report(iface->hid, HID_INPUT_REPORT, data, length, 1);
+}
+
+static void dchid_packet_work(struct work_struct *ws)
+{
+	struct dchid_work *work = container_of(ws, struct dchid_work, work);
+	struct dchid_subhdr *shdr = (void *)work->data;
+	struct dchid_dev *dchid = work->iface->dchid;
+	u16 hdr_len = le16_to_cpu(work->hdr.length);
+	u16 sub_len;
+	int type;
+	u8 *payload;
+
+	if (hdr_len < sizeof(*shdr)) {
+		dev_err(dchid->dev, "bad subheader length\n");
+		goto done;
+	}
+
+	sub_len = le16_to_cpu(shdr->length);
+	if (sub_len > hdr_len - sizeof(*shdr)) {
+		dev_err(dchid->dev, "bad subheader length\n");
+		goto done;
+	}
+
+	type = FIELD_GET(FLAGS_GROUP, shdr->flags);
+	payload = work->data + sizeof(*shdr);
+
+	switch (type) {
+	case HID_INPUT_REPORT:
+		if (work->hdr.iface == IFACE_COMM)
+			dchid_handle_event(dchid, payload, sub_len);
+		else
+			dchid_handle_report(work->iface, payload, sub_len);
+		break;
+	}
+
+done:
+	kfree(work);
+}
+
+static void dchid_handle_ack(struct dchid_iface *iface, struct dchid_hdr *hdr,
+			     void *data)
+{
+	struct dchid_subhdr *shdr = data;
+	u8 *payload = data + sizeof(*shdr);
+	u16 hdr_len = le16_to_cpu(hdr->length);
+	u16 sub_len = le16_to_cpu(shdr->length);
+	unsigned long flags;
+	bool complete_cmd = false;
+
+	if (hdr_len < sizeof(*shdr) || sub_len > hdr_len - sizeof(*shdr) ||
+	    sub_len < 1)
+		return;
+
+	spin_lock_irqsave(&iface->out_lock, flags);
+
+	if (shdr->flags == iface->out_flags && iface->tx_seq == hdr->seq &&
+	    iface->out_report == payload[0]) {
+		if (iface->resp_buf && iface->resp_size)
+			memcpy(iface->resp_buf, payload + 1,
+			       min_t(size_t, sub_len - 1, iface->resp_size));
+
+		iface->resp_size = sub_len;
+		iface->out_report = -1;
+		iface->retcode = le32_to_cpu(shdr->retcode);
+		complete_cmd = true;
+	}
+
+	spin_unlock_irqrestore(&iface->out_lock, flags);
+
+	if (complete_cmd)
+		complete(&iface->out_complete);
+}
+
+static void dchid_process_packet(struct dchid_dev *dchid, struct dchid_hdr *hdr,
+				 u8 *payload, size_t payload_len, u8 *packet,
+				 size_t packet_len)
+{
+	struct dchid_work *work;
+
+	if (dchid_checksum(packet, packet_len) != DCHID_CHECKSUM_SEED) {
+		dev_err_ratelimited(dchid->dev, "checksum error\n");
+		return;
+	}
+
+	if (payload_len < sizeof(struct dchid_subhdr))
+		return;
+
+	if (hdr->iface >= MAX_INTERFACES || !dchid->ifaces[hdr->iface])
+		return;
+
+	if (hdr->channel == DCHID_CHANNEL_CMD) {
+		dchid_handle_ack(dchid->ifaces[hdr->iface], hdr, payload);
+		return;
+	}
+
+	if (hdr->channel != DCHID_CHANNEL_REPORT)
+		return;
+
+	work = kzalloc(sizeof(*work) + payload_len, GFP_ATOMIC);
+	if (!work)
+		return;
+
+	work->hdr = *hdr;
+	work->iface = dchid->ifaces[hdr->iface];
+	memcpy(work->data, payload, payload_len);
+	INIT_WORK(&work->work, dchid_packet_work);
+
+	queue_work(work->iface->wq, &work->work);
+}
+
+static void dchid_consume_rx(struct dchid_dev *dchid)
+{
+	while (dchid->rx_len >= sizeof(struct dchid_hdr)) {
+		struct dchid_hdr *hdr = (struct dchid_hdr *)dchid->rx_buf;
+		size_t payload_len;
+		size_t packet_len;
+
+		if (hdr->hdr_len != sizeof(*hdr)) {
+			dev_err_ratelimited(dchid->dev, "bad header length %u\n",
+					    hdr->hdr_len);
+			dchid->rx_len = 0;
+			return;
+		}
+
+		payload_len = le16_to_cpu(hdr->length);
+		packet_len = sizeof(*hdr) + payload_len + DCHID_CHECKSUM_LEN;
+		if (packet_len > DCHID_RX_BUF_SIZE) {
+			dev_err_ratelimited(dchid->dev, "oversized packet %zu\n",
+					    packet_len);
+			dchid->rx_len = 0;
+			return;
+		}
+
+		if (dchid->rx_len < packet_len)
+			return;
+
+		dchid_process_packet(dchid, hdr, dchid->rx_buf + sizeof(*hdr),
+				     payload_len, dchid->rx_buf, packet_len);
+
+		dchid->rx_len -= packet_len;
+		memmove(dchid->rx_buf, dchid->rx_buf + packet_len, dchid->rx_len);
+	}
+}
+
+static void dchid_rx_callback(struct mbox_client *cl, void *mssg)
+{
+	struct dchid_dev *dchid = container_of(cl, struct dchid_dev, dc_mbox_client);
+	struct apple_dockchannel_msg *msg = mssg;
+
+	if (!msg || !msg->data || !msg->len)
+		return;
+
+	if (msg->len > DCHID_RX_BUF_SIZE - dchid->rx_len) {
+		dev_err_ratelimited(dchid->dev, "RX buffer overflow\n");
+		dchid->rx_len = 0;
+		return;
+	}
+
+	memcpy(dchid->rx_buf + dchid->rx_len, msg->data, msg->len);
+	dchid->rx_len += msg->len;
+
+	dchid_consume_rx(dchid);
+}
+
+static int dchid_rtkit_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr)
+{
+	struct dchid_dev *dchid = cookie;
+	struct resource res = {
+		.start = bfr->iova,
+		.end = bfr->iova + bfr->size - 1,
+		.name = "rtkit_map",
+	};
+
+	if (!bfr->iova) {
+		bfr->buffer = dma_alloc_coherent(dchid->dev, bfr->size,
+						 &bfr->iova, GFP_KERNEL);
+		if (!bfr->buffer)
+			return -ENOMEM;
+		return 0;
+	}
+
+	if (!dchid->sram_res.start)
+		return -EFAULT;
+
+	res.flags = dchid->sram_res.flags;
+	if (res.end < res.start || !resource_contains(&dchid->sram_res, &res))
+		return -EFAULT;
+
+	bfr->iomem = dchid->sram_base + (res.start - dchid->sram_res.start);
+	bfr->is_mapped = true;
+
+	return 0;
+}
+
+static void dchid_rtkit_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr)
+{
+	struct dchid_dev *dchid = cookie;
+
+	if (bfr->buffer)
+		dma_free_coherent(dchid->dev, bfr->size, bfr->buffer, bfr->iova);
+}
+
+static const struct apple_rtkit_ops dchid_rtkit_ops = {
+	.shmem_setup = dchid_rtkit_shmem_setup,
+	.shmem_destroy = dchid_rtkit_shmem_destroy,
+};
+
+static int dchid_map_helper_cpu(struct platform_device *pdev, struct dchid_dev *dchid)
+{
+	struct resource *res;
+
+	dchid->asc_base = devm_platform_ioremap_resource_byname(pdev, "coproc-asc");
+	if (IS_ERR(dchid->asc_base))
+		return PTR_ERR(dchid->asc_base);
+
+	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "coproc-sram");
+	if (!res)
+		return -EINVAL;
+
+	dchid->sram_res = *res;
+
+	dchid->sram_base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(dchid->sram_base))
+		return PTR_ERR(dchid->sram_base);
+
+	return 0;
+}
+
+static int dchid_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct dchid_dev *dchid;
+	int ret;
+
+	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(44));
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to set DMA mask\n");
+
+	dchid = devm_kzalloc(dev, sizeof(*dchid), GFP_KERNEL);
+	if (!dchid)
+		return -ENOMEM;
+
+	dchid->rx_buf = devm_kmalloc(dev, DCHID_RX_BUF_SIZE, GFP_KERNEL);
+	if (!dchid->rx_buf)
+		return -ENOMEM;
+
+	dchid->dev = dev;
+	mutex_init(&dchid->ifaces_lock);
+	platform_set_drvdata(pdev, dchid);
+
+	ret = dchid_map_helper_cpu(pdev, dchid);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to map helper CPU\n");
+
+	dchid->dc_mbox_client.dev = dev;
+	dchid->dc_mbox_client.tx_block = true;
+	dchid->dc_mbox_client.rx_callback = dchid_rx_callback;
+
+	dchid->dc_mbox = mbox_request_channel_byname(&dchid->dc_mbox_client,
+						     "dockchannel");
+	if (IS_ERR(dchid->dc_mbox))
+		return dev_err_probe(dev, PTR_ERR(dchid->dc_mbox),
+				     "failed to request DockChannel mailbox\n");
+
+	ret = devm_add_action_or_reset(dev, dchid_free_mbox, dchid->dc_mbox);
+	if (ret)
+		return ret;
+
+	dchid->rtk = devm_apple_rtkit_init(dev, dchid, "asc", 0, &dchid_rtkit_ops);
+	if (IS_ERR(dchid->rtk))
+		return dev_err_probe(dev, PTR_ERR(dchid->rtk), "failed to init RTKit\n");
+
+	writel_relaxed(APPLE_ASC_CPU_CONTROL_RUN,
+		       dchid->asc_base + APPLE_ASC_CPU_CONTROL);
+
+	ret = apple_rtkit_wake(dchid->rtk);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to wake coprocessor\n");
+
+	dchid->new_iface_wq = alloc_ordered_workqueue("dchid-new", 0);
+	if (!dchid->new_iface_wq)
+		return dev_err_probe(dev, -ENOMEM, "failed to allocate workqueue\n");
+
+	ret = devm_add_action_or_reset(dev, dchid_destroy_wq, dchid->new_iface_wq);
+	if (ret)
+		return ret;
+
+	dchid->comm = dchid_get_interface(dchid, IFACE_COMM, "comm");
+	if (!dchid->comm)
+		return dev_err_probe(dev, -EIO, "failed to init comm interface\n");
+
+	return 0;
+}
+
+static void dchid_remove(struct platform_device *pdev)
+{
+	struct dchid_dev *dchid = platform_get_drvdata(pdev);
+	int i;
+
+	if (dchid->dc_mbox) {
+		devm_release_action(&pdev->dev, dchid_free_mbox, dchid->dc_mbox);
+		dchid->dc_mbox = NULL;
+	}
+
+	if (dchid->rtk && apple_rtkit_is_running(dchid->rtk))
+		apple_rtkit_quiesce(dchid->rtk);
+
+	if (dchid->asc_base)
+		writel_relaxed(0, dchid->asc_base + APPLE_ASC_CPU_CONTROL);
+
+	for (i = 0; i < MAX_INTERFACES; i++) {
+		struct dchid_iface *iface = dchid->ifaces[i];
+
+		if (!iface)
+			continue;
+
+		cancel_work_sync(&iface->create_work);
+		flush_workqueue(iface->wq);
+
+		if (iface->hid)
+			hid_destroy_device(iface->hid);
+	}
+
+	if (dchid->new_iface_wq)
+		flush_workqueue(dchid->new_iface_wq);
+}
+
+static const struct of_device_id dchid_of_match[] = {
+	{ .compatible = "apple,t8122-dockchannel-hid" },
+	{ .compatible = "apple,t8112-dockchannel-hid" },
+	{},
+};
+MODULE_DEVICE_TABLE(of, dchid_of_match);
+
+static struct platform_driver dchid_platform_driver = {
+	.driver = {
+		.name = "dockchannel-hid",
+		.of_match_table = dchid_of_match,
+	},
+	.probe = dchid_probe,
+	.remove = dchid_remove,
+};
+module_platform_driver(dchid_platform_driver);
+
+MODULE_DESCRIPTION("Apple DockChannel HID transport driver");
+MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");
+MODULE_AUTHOR("Michael Reeves <michael.reeves077@gmail.com>");
+MODULE_LICENSE("Dual MIT/GPL");

-- 
2.51.2



^ permalink raw reply related

* [PATCH 06/10] soc: apple: rtkit: Add tracekit endpoint
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves, Sasha Finkelstein
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Sasha Finkelstein <fnkl.kernel@gmail.com>

The TraceKit endpoint is a system endpoint used by MTP, AOP, and
potentially other Apple RTKit coprocessors.

Start it automatically when it appears in the endpoint map, like the
other known system endpoints, to avoid warnings about an unknown
endpoint.

Signed-off-by: Sasha Finkelstein <fnkl.kernel@gmail.com>
Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 drivers/soc/apple/rtkit.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c
index a3fdac8f6f06..1059b4bd8732 100644
--- a/drivers/soc/apple/rtkit.c
+++ b/drivers/soc/apple/rtkit.c
@@ -22,6 +22,7 @@ enum {
 	APPLE_RTKIT_EP_DEBUG = 3,
 	APPLE_RTKIT_EP_IOREPORT = 4,
 	APPLE_RTKIT_EP_OSLOG = 8,
+	APPLE_RTKIT_EP_TRACEKIT = 0xa,
 };
 
 #define APPLE_RTKIT_MGMT_TYPE GENMASK_ULL(59, 52)
@@ -191,6 +192,7 @@ static void apple_rtkit_management_rx_epmap(struct apple_rtkit *rtk, u64 msg)
 		case APPLE_RTKIT_EP_DEBUG:
 		case APPLE_RTKIT_EP_IOREPORT:
 		case APPLE_RTKIT_EP_OSLOG:
+		case APPLE_RTKIT_EP_TRACEKIT:
 			dev_dbg(rtk->dev,
 				"RTKit: Starting system endpoint 0x%02x\n", ep);
 			apple_rtkit_start_ep(rtk, ep);

-- 
2.51.2



^ permalink raw reply related

* [PATCH 05/10] mailbox: apple: Add DockChannel FIFO controller
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Michael Reeves <michael.reeves077@gmail.com>

DockChannel is a hardware FIFO used by Apple coprocessors for
low-latency byte-stream communication with the AP.

Add a mailbox controller that preallocates RX storage, tracks IRQ
enable state in software, and reports TX completion from the TX-empty
interrupt.

Reject messages larger than the FIFO and return -EBUSY while the
previous message is still pending. This keeps the provider usable for
future small-message clients such as serial transports without a TX
worker.

Co-developed-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 MAINTAINERS                               |   2 +
 drivers/mailbox/Kconfig                   |  12 +
 drivers/mailbox/Makefile                  |   2 +
 drivers/mailbox/apple-dockchannel.c       | 380 ++++++++++++++++++++++++++++++
 include/linux/mailbox/apple-dockchannel.h |  29 +++
 5 files changed, 425 insertions(+)

diff --git a/MAINTAINERS b/MAINTAINERS
index 1f3c2cdb6e19..ed68452c0ad6 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2628,6 +2628,7 @@ F:	drivers/input/touchscreen/apple_z2.c
 F:	drivers/iommu/apple-dart.c
 F:	drivers/iommu/io-pgtable-dart.c
 F:	drivers/irqchip/irq-apple-aic.c
+F:	drivers/mailbox/apple-dockchannel.c
 F:	drivers/mfd/macsmc.c
 F:	drivers/nvme/host/apple.c
 F:	drivers/nvmem/apple-efuses.c
@@ -2646,6 +2647,7 @@ F:	drivers/video/backlight/apple_dwi_bl.c
 F:	drivers/watchdog/apple_wdt.c
 F:	include/dt-bindings/interrupt-controller/apple-aic.h
 F:	include/dt-bindings/pinctrl/apple.h
+F:	include/linux/mailbox/apple-dockchannel.h
 F:	include/linux/mfd/macsmc.h
 F:	include/linux/soc/apple/*
 F:	include/uapi/drm/asahi_drm.h
diff --git a/drivers/mailbox/Kconfig b/drivers/mailbox/Kconfig
index 3062ee352f78..f1af76d19f1e 100644
--- a/drivers/mailbox/Kconfig
+++ b/drivers/mailbox/Kconfig
@@ -36,6 +36,18 @@ config ARM_MHU_V3
 	  that provides different means of transports: supported extensions
 	  will be discovered and possibly managed at probe-time.
 
+config APPLE_DOCKCHANNEL
+	tristate "Apple DockChannel FIFO mailbox"
+	depends on ARCH_APPLE || COMPILE_TEST
+	depends on HAS_IOMEM
+	depends on OF
+	help
+	  DockChannel is a hardware FIFO used on Apple Silicon SoCs for
+	  communication between the application processor and co-processors.
+	  This driver exposes DockChannel FIFOs through the mailbox framework.
+
+	  Say Y here if you have an M2 or later Apple MacBook.
+
 config AST2700_MBOX
 	tristate "ASPEED AST2700 IPC driver"
 	depends on ARCH_ASPEED || COMPILE_TEST
diff --git a/drivers/mailbox/Makefile b/drivers/mailbox/Makefile
index 944d8ea39f34..4f3405064269 100644
--- a/drivers/mailbox/Makefile
+++ b/drivers/mailbox/Makefile
@@ -11,6 +11,8 @@ obj-$(CONFIG_ARM_MHU_V2)	+= arm_mhuv2.o
 
 obj-$(CONFIG_ARM_MHU_V3)	+= arm_mhuv3.o
 
+obj-$(CONFIG_APPLE_DOCKCHANNEL)	+= apple-dockchannel.o
+
 obj-$(CONFIG_AST2700_MBOX)	+= ast2700-mailbox.o
 
 obj-$(CONFIG_CV1800_MBOX)	+= cv1800-mailbox.o
diff --git a/drivers/mailbox/apple-dockchannel.c b/drivers/mailbox/apple-dockchannel.c
new file mode 100644
index 000000000000..bae183db1307
--- /dev/null
+++ b/drivers/mailbox/apple-dockchannel.c
@@ -0,0 +1,380 @@
+// SPDX-License-Identifier: GPL-2.0-only OR MIT
+/*
+ * Apple DockChannel mailbox controller
+ *
+ * Copyright The Asahi Linux Contributors
+ *
+ * DockChannel is a byte FIFO used by Apple co-processors. This driver exposes a
+ * single FIFO pair as a Linux mailbox channel and moves payload bytes with PIO.
+ * There is no DMA involved, so relaxed MMIO accessors are sufficient for the
+ * FIFO accesses themselves.
+ */
+
+#include <linux/device.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/mailbox/apple-dockchannel.h>
+#include <linux/mailbox_controller.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/spinlock.h>
+#include <linux/unaligned.h>
+
+#define APPLE_DOCKCHANNEL_FIFO_SIZE	0x800
+
+#define IRQ_MASK			0x0
+#define IRQ_FLAG			0x4
+
+#define IRQ_TX				BIT(2)
+#define IRQ_RX				BIT(3)
+
+#define CONFIG_TX_THRESH		0x0
+#define CONFIG_RX_THRESH		0x4
+
+#define DATA_TX8			0x4
+#define DATA_TX32			0x10
+#define DATA_TX_FREE			0x14
+#define DATA_RX8			0x1c
+#define DATA_RX32			0x28
+#define DATA_RX_COUNT			0x2c
+
+struct apple_dockchannel {
+	struct device *dev;
+	struct mbox_controller controller;
+	struct mbox_chan chan;
+
+	void __iomem *irq_base;
+	void __iomem *config_base;
+	void __iomem *data_base;
+	int irq;
+
+	spinlock_t lock; /* protects IRQ mask and TX state */
+	u32 irq_mask;
+
+	const u8 *tx_buf;
+	size_t tx_len;
+	size_t tx_pos;
+	bool tx_active;
+
+	u8 rx_buf[APPLE_DOCKCHANNEL_FIFO_SIZE];
+};
+
+static void apple_dockchannel_irq_update(struct apple_dockchannel *dc,
+					 u32 bits, bool enable)
+{
+	if (enable)
+		dc->irq_mask |= bits;
+	else
+		dc->irq_mask &= ~bits;
+	writel_relaxed(dc->irq_mask, dc->irq_base + IRQ_MASK);
+}
+
+static void apple_dockchannel_irq_enable(struct apple_dockchannel *dc, u32 bits)
+{
+	/*
+	 * IRQ_FLAG is write-to-clear. Clear stale latched flags before
+	 * unmasking so the next interrupt reflects current FIFO state.
+	 */
+	writel_relaxed(bits, dc->irq_base + IRQ_FLAG);
+	apple_dockchannel_irq_update(dc, bits, true);
+}
+
+static void apple_dockchannel_irq_disable(struct apple_dockchannel *dc, u32 bits)
+{
+	apple_dockchannel_irq_update(dc, bits, false);
+}
+
+static bool apple_dockchannel_tx_empty(struct apple_dockchannel *dc)
+{
+	return readl_relaxed(dc->data_base + DATA_TX_FREE) ==
+	       APPLE_DOCKCHANNEL_FIFO_SIZE;
+}
+
+static void apple_dockchannel_write_pending(struct apple_dockchannel *dc)
+{
+	size_t left = dc->tx_len - dc->tx_pos;
+	const u8 *p = dc->tx_buf + dc->tx_pos;
+
+	while (left) {
+		size_t avail;
+		size_t block;
+
+		avail = readl_relaxed(dc->data_base + DATA_TX_FREE);
+		if (!avail)
+			break;
+
+		block = min(left, avail);
+
+		while (block >= sizeof(u32)) {
+			writel_relaxed(get_unaligned_le32(p),
+				       dc->data_base + DATA_TX32);
+			p += sizeof(u32);
+			left -= sizeof(u32);
+			block -= sizeof(u32);
+		}
+
+		while (block) {
+			writeb_relaxed(*p++, dc->data_base + DATA_TX8);
+			left--;
+			block--;
+		}
+	}
+
+	dc->tx_pos = dc->tx_len - left;
+}
+
+static void apple_dockchannel_read(struct apple_dockchannel *dc, void *buf,
+				   size_t count)
+{
+	u8 *p = buf;
+	size_t left = count;
+
+	while (left >= sizeof(u32)) {
+		put_unaligned_le32(readl_relaxed(dc->data_base + DATA_RX32), p);
+		p += sizeof(u32);
+		left -= sizeof(u32);
+	}
+
+	while (left) {
+		/*
+		 * The byte FIFO register returns the byte in bits [15:8] on
+		 * these instances.
+		 */
+		*p++ = readl_relaxed(dc->data_base + DATA_RX8) >> 8;
+		left--;
+	}
+}
+
+static int apple_dockchannel_send_data(struct mbox_chan *chan, void *data)
+{
+	struct apple_dockchannel *dc = chan->con_priv;
+	struct apple_dockchannel_msg *msg = data;
+	unsigned long flags;
+
+	if (!msg || !msg->data || !msg->len)
+		return -EINVAL;
+
+	if (msg->len > APPLE_DOCKCHANNEL_FIFO_SIZE)
+		return -EMSGSIZE;
+
+	spin_lock_irqsave(&dc->lock, flags);
+
+	if (dc->tx_active || !apple_dockchannel_tx_empty(dc)) {
+		spin_unlock_irqrestore(&dc->lock, flags);
+		return -EBUSY;
+	}
+
+	dc->tx_buf = msg->data;
+	dc->tx_len = msg->len;
+	dc->tx_pos = 0;
+	dc->tx_active = true;
+
+	apple_dockchannel_write_pending(dc);
+	writel_relaxed(APPLE_DOCKCHANNEL_FIFO_SIZE,
+		       dc->config_base + CONFIG_TX_THRESH);
+	apple_dockchannel_irq_enable(dc, IRQ_TX);
+
+	spin_unlock_irqrestore(&dc->lock, flags);
+
+	return 0;
+}
+
+static int apple_dockchannel_startup(struct mbox_chan *chan)
+{
+	struct apple_dockchannel *dc = chan->con_priv;
+	unsigned long flags;
+
+	spin_lock_irqsave(&dc->lock, flags);
+	/*
+	 * The mailbox framework has no per-client RX threshold. Use byte
+	 * granularity because UART-style DockChannel clients require it.
+	 */
+	writel_relaxed(1, dc->config_base + CONFIG_RX_THRESH);
+	apple_dockchannel_irq_enable(dc, IRQ_RX);
+	spin_unlock_irqrestore(&dc->lock, flags);
+
+	enable_irq(dc->irq);
+
+	return 0;
+}
+
+static void apple_dockchannel_shutdown(struct mbox_chan *chan)
+{
+	struct apple_dockchannel *dc = chan->con_priv;
+	unsigned long flags;
+
+	disable_irq(dc->irq);
+
+	spin_lock_irqsave(&dc->lock, flags);
+	apple_dockchannel_irq_disable(dc, IRQ_TX | IRQ_RX);
+	dc->tx_active = false;
+	spin_unlock_irqrestore(&dc->lock, flags);
+}
+
+static const struct mbox_chan_ops apple_dockchannel_mbox_ops = {
+	.send_data = apple_dockchannel_send_data,
+	.startup = apple_dockchannel_startup,
+	.shutdown = apple_dockchannel_shutdown,
+};
+
+static irqreturn_t apple_dockchannel_irq(int irq, void *data)
+{
+	struct apple_dockchannel *dc = data;
+	u32 flags;
+	u32 pending;
+	bool tx_done = false;
+
+	flags = readl_relaxed(dc->irq_base + IRQ_FLAG);
+
+	spin_lock(&dc->lock);
+
+	pending = flags & dc->irq_mask & (IRQ_TX | IRQ_RX);
+	if (!pending)
+		goto out_unlock_none;
+
+	if (pending & IRQ_TX) {
+		if (apple_dockchannel_tx_empty(dc)) {
+			apple_dockchannel_irq_disable(dc, IRQ_TX);
+			tx_done = dc->tx_active;
+			dc->tx_active = false;
+		} else {
+			pending &= ~IRQ_TX;
+		}
+	}
+
+	writel_relaxed(pending, dc->irq_base + IRQ_FLAG);
+
+	spin_unlock(&dc->lock);
+
+	if (tx_done)
+		mbox_chan_txdone(&dc->chan, 0);
+
+	if (pending & IRQ_RX)
+		return IRQ_WAKE_THREAD;
+
+	if (pending)
+		return IRQ_HANDLED;
+
+	return IRQ_NONE;
+
+out_unlock_none:
+	spin_unlock(&dc->lock);
+
+	if (flags & (IRQ_TX | IRQ_RX))
+		writel_relaxed(flags & (IRQ_TX | IRQ_RX),
+			       dc->irq_base + IRQ_FLAG);
+
+	return IRQ_NONE;
+}
+
+static irqreturn_t apple_dockchannel_irq_thread(int irq, void *data)
+{
+	struct apple_dockchannel *dc = data;
+
+	for (;;) {
+		struct apple_dockchannel_msg msg;
+		size_t avail;
+
+		avail = readl_relaxed(dc->data_base + DATA_RX_COUNT);
+		if (!avail)
+			break;
+
+		avail = min_t(size_t, avail, APPLE_DOCKCHANNEL_FIFO_SIZE);
+
+		apple_dockchannel_read(dc, dc->rx_buf, avail);
+
+		msg.data = dc->rx_buf;
+		msg.len = avail;
+		mbox_chan_received_data(&dc->chan, &msg);
+	}
+
+	return IRQ_HANDLED;
+}
+
+static struct mbox_chan *
+apple_dockchannel_of_xlate(struct mbox_controller *mbox,
+			   const struct of_phandle_args *spec)
+{
+	if (spec->args_count != 0)
+		return ERR_PTR(-EINVAL);
+
+	return &mbox->chans[0];
+}
+
+static int apple_dockchannel_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct apple_dockchannel *dc;
+	int ret;
+
+	dc = devm_kzalloc(dev, sizeof(*dc), GFP_KERNEL);
+	if (!dc)
+		return -ENOMEM;
+
+	dc->dev = dev;
+	spin_lock_init(&dc->lock);
+	platform_set_drvdata(pdev, dc);
+
+	dc->irq_base = devm_platform_ioremap_resource_byname(pdev, "irq");
+	if (IS_ERR(dc->irq_base))
+		return PTR_ERR(dc->irq_base);
+
+	dc->config_base = devm_platform_ioremap_resource_byname(pdev, "config");
+	if (IS_ERR(dc->config_base))
+		return PTR_ERR(dc->config_base);
+
+	dc->data_base = devm_platform_ioremap_resource_byname(pdev, "data");
+	if (IS_ERR(dc->data_base))
+		return PTR_ERR(dc->data_base);
+
+	writel_relaxed(0, dc->irq_base + IRQ_MASK);
+	writel_relaxed(~0, dc->irq_base + IRQ_FLAG);
+
+	dc->irq = platform_get_irq(pdev, 0);
+	if (dc->irq < 0)
+		return dc->irq;
+
+	ret = devm_request_threaded_irq(dev, dc->irq, apple_dockchannel_irq,
+					apple_dockchannel_irq_thread, IRQF_ONESHOT,
+					dev_name(dev), dc);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to request IRQ\n");
+
+	disable_irq(dc->irq);
+
+	dc->chan.con_priv = dc;
+	dc->controller.dev = dev;
+	dc->controller.ops = &apple_dockchannel_mbox_ops;
+	dc->controller.chans = &dc->chan;
+	dc->controller.num_chans = 1;
+	dc->controller.txdone_irq = true;
+	dc->controller.of_xlate = apple_dockchannel_of_xlate;
+
+	ret = devm_mbox_controller_register(dev, &dc->controller);
+	if (ret)
+		return dev_err_probe(dev, ret, "failed to register mailbox\n");
+
+	return 0;
+}
+
+static const struct of_device_id apple_dockchannel_of_match[] = {
+	{ .compatible = "apple,t8122-dockchannel" },
+	{ .compatible = "apple,t8112-dockchannel" },
+	{},
+};
+MODULE_DEVICE_TABLE(of, apple_dockchannel_of_match);
+
+static struct platform_driver apple_dockchannel_driver = {
+	.driver = {
+		.name = "apple-dockchannel",
+		.of_match_table = apple_dockchannel_of_match,
+	},
+	.probe = apple_dockchannel_probe,
+};
+module_platform_driver(apple_dockchannel_driver);
+
+MODULE_DESCRIPTION("Apple DockChannel mailbox controller");
+MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");
+MODULE_AUTHOR("Michael Reeves <michael.reeves077@gmail.com>");
+MODULE_LICENSE("Dual MIT/GPL");
diff --git a/include/linux/mailbox/apple-dockchannel.h b/include/linux/mailbox/apple-dockchannel.h
new file mode 100644
index 000000000000..04d2fc44f12f
--- /dev/null
+++ b/include/linux/mailbox/apple-dockchannel.h
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
+/*
+ * Apple DockChannel mailbox message format.
+ *
+ * Copyright The Asahi Linux Contributors
+ */
+
+#ifndef _LINUX_MAILBOX_APPLE_DOCKCHANNEL_H_
+#define _LINUX_MAILBOX_APPLE_DOCKCHANNEL_H_
+
+#include <linux/types.h>
+
+/**
+ * struct apple_dockchannel_msg - DockChannel mailbox payload
+ * @data: Pointer to the byte stream payload
+ * @len: Number of payload bytes
+ *
+ * For TX, @data must remain valid until mbox_send_message() completes or the
+ * client receives tx_done in non-blocking mode.
+ *
+ * For RX, @data is owned by the controller and is valid only for the duration
+ * of the rx_callback.
+ */
+struct apple_dockchannel_msg {
+	void *data;
+	size_t len;
+};
+
+#endif /* _LINUX_MAILBOX_APPLE_DOCKCHANNEL_H_ */

-- 
2.51.2



^ permalink raw reply related

* [PATCH 09/10] arm64: dts: apple: Add MTP DockChannel HID nodes
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Michael Reeves <michael.reeves077@gmail.com>

Add disabled MTP nodes for the Apple SoCs used by M2 and M3 systems.

Each instance describes the RTKit ASC mailbox, MTP DART, DockChannel
mailbox, and DockChannel HID client. Board files can enable them on
machines that route internal input through MTP.

Co-developed-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 arch/arm64/boot/dts/apple/t602x-die0.dtsi | 46 ++++++++++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112.dtsi      | 46 ++++++++++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8122.dtsi      | 47 +++++++++++++++++++++++++++++++
 3 files changed, 139 insertions(+)

diff --git a/arch/arm64/boot/dts/apple/t602x-die0.dtsi b/arch/arm64/boot/dts/apple/t602x-die0.dtsi
index 8622ddea7b44..edc73682fd22 100644
--- a/arch/arm64/boot/dts/apple/t602x-die0.dtsi
+++ b/arch/arm64/boot/dts/apple/t602x-die0.dtsi
@@ -150,6 +150,52 @@ pinctrl_smc: pinctrl@2a2820000 {
 				<AIC_IRQ 0 857 IRQ_TYPE_LEVEL_HIGH>;
 	};
 
+	mtp_mbox: mbox@2a9408000 {
+		compatible = "apple,t6020-asc-mailbox", "apple,asc-mailbox-v4";
+		reg = <0x2 0xa9408000 0x0 0x4000>;
+		interrupt-parent = <&aic>;
+		interrupts = <AIC_IRQ 0 693 IRQ_TYPE_LEVEL_HIGH>,
+			     <AIC_IRQ 0 694 IRQ_TYPE_LEVEL_HIGH>,
+			     <AIC_IRQ 0 695 IRQ_TYPE_LEVEL_HIGH>,
+			     <AIC_IRQ 0 696 IRQ_TYPE_LEVEL_HIGH>;
+		interrupt-names = "send-empty", "send-not-empty",
+				  "recv-empty", "recv-not-empty";
+		#mbox-cells = <0>;
+		status = "disabled";
+	};
+
+	mtp_dart: iommu@2a9808000 {
+		compatible = "apple,t6020-dart", "apple,t8110-dart";
+		reg = <0x2 0xa9808000 0x0 0x4000>;
+		interrupt-parent = <&aic>;
+		interrupts = <AIC_IRQ 0 676 IRQ_TYPE_LEVEL_HIGH>;
+		#iommu-cells = <1>;
+		status = "disabled";
+	};
+
+	mtp_dockchannel: mailbox@2a9b14000 {
+		compatible = "apple,t6020-dockchannel", "apple,t8112-dockchannel";
+		reg = <0x2 0xa9b14000 0x0 0x4000>,
+		      <0x2 0xa9b30000 0x0 0x4000>,
+		      <0x2 0xa9b34000 0x0 0x4000>;
+		reg-names = "irq", "config", "data";
+		interrupt-parent = <&aic>;
+		interrupts = <AIC_IRQ 0 677 IRQ_TYPE_LEVEL_HIGH>;
+		#mbox-cells = <0>;
+		status = "disabled";
+	};
+
+	mtp_hid: hid@2a9400000 {
+		compatible = "apple,t6020-dockchannel-hid", "apple,t8112-dockchannel-hid";
+		reg = <0x2 0xa9400000 0x0 0x4000>,
+		      <0x2 0xa9c00000 0x0 0x100000>;
+		reg-names = "coproc-asc", "coproc-sram";
+		mboxes = <&mtp_mbox>, <&mtp_dockchannel>;
+		mbox-names = "asc", "dockchannel";
+		iommus = <&mtp_dart 1>;
+		status = "disabled";
+	};
+
 	sio_dart: iommu@39b008000 {
 		compatible = "apple,t6020-dart", "apple,t8110-dart";
 		reg = <0x3 0x9b008000 0x0 0x8000>;
diff --git a/arch/arm64/boot/dts/apple/t8112.dtsi b/arch/arm64/boot/dts/apple/t8112.dtsi
index 85c47422d4e8..8411828c0772 100644
--- a/arch/arm64/boot/dts/apple/t8112.dtsi
+++ b/arch/arm64/boot/dts/apple/t8112.dtsi
@@ -983,6 +983,52 @@ pinctrl_aop: pinctrl@24a820000 {
 				     <AIC_IRQ 307 IRQ_TYPE_LEVEL_HIGH>;
 		};
 
+		mtp_mbox: mbox@24e408000 {
+			compatible = "apple,t8112-asc-mailbox", "apple,asc-mailbox-v4";
+			reg = <0x2 0x4e408000 0x0 0x4000>;
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 864 IRQ_TYPE_LEVEL_HIGH>,
+				     <AIC_IRQ 865 IRQ_TYPE_LEVEL_HIGH>,
+				     <AIC_IRQ 866 IRQ_TYPE_LEVEL_HIGH>,
+				     <AIC_IRQ 867 IRQ_TYPE_LEVEL_HIGH>;
+			interrupt-names = "send-empty", "send-not-empty",
+					  "recv-empty", "recv-not-empty";
+			#mbox-cells = <0>;
+			status = "disabled";
+		};
+
+		mtp_dart: iommu@24e808000 {
+			compatible = "apple,t8110-dart";
+			reg = <0x2 0x4e808000 0x0 0x4000>;
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 848 IRQ_TYPE_LEVEL_HIGH>;
+			#iommu-cells = <1>;
+			status = "disabled";
+		};
+
+		mtp_dockchannel: mailbox@24eb14000 {
+			compatible = "apple,t8112-dockchannel";
+			reg = <0x2 0x4eb14000 0x0 0x4000>,
+			      <0x2 0x4eb30000 0x0 0x4000>,
+			      <0x2 0x4eb34000 0x0 0x4000>;
+			reg-names = "irq", "config", "data";
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 850 IRQ_TYPE_LEVEL_HIGH>;
+			#mbox-cells = <0>;
+			status = "disabled";
+		};
+
+		mtp_hid: hid@24e400000 {
+			compatible = "apple,t8112-dockchannel-hid";
+			reg = <0x2 0x4e400000 0x0 0x4000>,
+			      <0x2 0x4ec00000 0x0 0x100000>;
+			reg-names = "coproc-asc", "coproc-sram";
+			mboxes = <&mtp_mbox>, <&mtp_dockchannel>;
+			mbox-names = "asc", "dockchannel";
+			iommus = <&mtp_dart 1>;
+			status = "disabled";
+		};
+
 		ans_mbox: mbox@277408000 {
 			compatible = "apple,t8112-asc-mailbox", "apple,asc-mailbox-v4";
 			reg = <0x2 0x77408000 0x0 0x4000>;
diff --git a/arch/arm64/boot/dts/apple/t8122.dtsi b/arch/arm64/boot/dts/apple/t8122.dtsi
index c6196225e96e..ec1f47d15ec9 100644
--- a/arch/arm64/boot/dts/apple/t8122.dtsi
+++ b/arch/arm64/boot/dts/apple/t8122.dtsi
@@ -438,6 +438,53 @@ pinctrl_aop: pinctrl@2f4824000 {
 				     <AIC_IRQ 351 IRQ_TYPE_LEVEL_HIGH>,
 				     <AIC_IRQ 352 IRQ_TYPE_LEVEL_HIGH>;
 		};
+
+		mtp_mbox: mbox@2fa408000 {
+			compatible = "apple,t8122-asc-mailbox", "apple,asc-mailbox-v4";
+			reg = <0x2 0xfa408000 0x0 0x4000>;
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 838 IRQ_TYPE_LEVEL_HIGH>,
+				     <AIC_IRQ 839 IRQ_TYPE_LEVEL_HIGH>,
+				     <AIC_IRQ 840 IRQ_TYPE_LEVEL_HIGH>,
+				     <AIC_IRQ 841 IRQ_TYPE_LEVEL_HIGH>;
+			interrupt-names = "send-empty", "send-not-empty",
+					  "recv-empty", "recv-not-empty";
+			#mbox-cells = <0>;
+			status = "disabled";
+		};
+
+		mtp_dart: iommu@2fa808000 {
+			compatible = "apple,t8122-dart", "apple,t8110-dart";
+			reg = <0x2 0xfa808000 0x0 0x4000>;
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 822 IRQ_TYPE_LEVEL_HIGH>;
+			#iommu-cells = <1>;
+			status = "disabled";
+		};
+
+		mtp_dockchannel: mailbox@2fab14000 {
+			compatible = "apple,t8122-dockchannel", "apple,t8112-dockchannel";
+			reg = <0x2 0xfab14000 0x0 0x4000>,
+			      <0x2 0xfab30000 0x0 0x4000>,
+			      <0x2 0xfab34000 0x0 0x4000>;
+			reg-names = "irq", "config", "data";
+			interrupt-parent = <&aic>;
+			interrupts = <AIC_IRQ 824 IRQ_TYPE_LEVEL_HIGH>;
+			#mbox-cells = <0>;
+			status = "disabled";
+		};
+
+		mtp_hid: hid@2fa400000 {
+			compatible = "apple,t8122-dockchannel-hid",
+				     "apple,t8112-dockchannel-hid";
+			reg = <0x2 0xfa400000 0x0 0x4000>,
+			      <0x2 0xfac00000 0x0 0x100000>;
+			reg-names = "coproc-asc", "coproc-sram";
+			mboxes = <&mtp_mbox>, <&mtp_dockchannel>;
+			mbox-names = "asc", "dockchannel";
+			iommus = <&mtp_dart 1>;
+			status = "disabled";
+		};
 	};
 };
 

-- 
2.51.2



^ permalink raw reply related

* [PATCH 10/10] arm64: dts: apple: Enable DockChannel HID on M2 and M3 laptops
From: Michael Reeves via B4 Relay @ 2026-06-30 12:54 UTC (permalink / raw)
  To: Sven Peter, Janne Grunau, Neal Gompa, Jassi Brar, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Hector Martin,
	Joerg Roedel (AMD), Will Deacon, Robin Murphy, Dmitry Torokhov,
	Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input, Michael Reeves
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com>

From: Michael Reeves <michael.reeves077@gmail.com>

Enable the MTP mailbox, DART, DockChannel mailbox, and HID transport on
the M2 and M3 laptop device trees using this internal input path.

Add a keyboard alias and keyboard child node for each machine so the
transport can expose the internal keyboard.

Co-developed-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Hector Martin <marcan@marcan.st>
Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
---
 arch/arm64/boot/dts/apple/t602x-j414-j416.dtsi | 25 +++++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112-j413.dts       | 20 ++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112-j415.dts       | 20 ++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8112-j493.dts       | 22 +++++++++++++++++++++-
 arch/arm64/boot/dts/apple/t8122-j504.dts       | 22 ++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8122-j613.dts       | 23 +++++++++++++++++++++++
 arch/arm64/boot/dts/apple/t8122-j615.dts       | 23 +++++++++++++++++++++++
 7 files changed, 154 insertions(+), 1 deletion(-)

diff --git a/arch/arm64/boot/dts/apple/t602x-j414-j416.dtsi b/arch/arm64/boot/dts/apple/t602x-j414-j416.dtsi
index 0e806d8ddf81..46ed5ea86242 100644
--- a/arch/arm64/boot/dts/apple/t602x-j414-j416.dtsi
+++ b/arch/arm64/boot/dts/apple/t602x-j414-j416.dtsi
@@ -16,6 +16,12 @@
 
 #include "t600x-j314-j316.dtsi"
 
+/ {
+	aliases {
+		keyboard = &keyboard;
+	};
+};
+
 &framebuffer0 {
 	power-domains = <&ps_disp0_cpu0>, <&ps_dptx_phy_ps>;
 };
@@ -43,3 +49,22 @@ &wifi0 {
 &bluetooth0 {
 	compatible = "pci14e4,5f72";
 };
+
+&mtp_mbox {
+	status = "okay";
+};
+
+&mtp_dart {
+	status = "okay";
+};
+
+&mtp_dockchannel {
+	status = "okay";
+};
+
+&mtp_hid {
+	status = "okay";
+
+	keyboard: keyboard {
+	};
+};
diff --git a/arch/arm64/boot/dts/apple/t8112-j413.dts b/arch/arm64/boot/dts/apple/t8112-j413.dts
index 1a08a41f369b..1256e7cd9876 100644
--- a/arch/arm64/boot/dts/apple/t8112-j413.dts
+++ b/arch/arm64/boot/dts/apple/t8112-j413.dts
@@ -20,6 +20,7 @@ / {
 
 	aliases {
 		bluetooth0 = &bluetooth0;
+		keyboard = &keyboard;
 		wifi0 = &wifi0;
 	};
 
@@ -91,3 +92,22 @@ &i2c4 {
 &fpwm1 {
 	status = "okay";
 };
+
+&mtp_mbox {
+	status = "okay";
+};
+
+&mtp_dart {
+	status = "okay";
+};
+
+&mtp_dockchannel {
+	status = "okay";
+};
+
+&mtp_hid {
+	status = "okay";
+
+	keyboard: keyboard {
+	};
+};
diff --git a/arch/arm64/boot/dts/apple/t8112-j415.dts b/arch/arm64/boot/dts/apple/t8112-j415.dts
index e37c56d9fb4d..1db3500e991f 100644
--- a/arch/arm64/boot/dts/apple/t8112-j415.dts
+++ b/arch/arm64/boot/dts/apple/t8112-j415.dts
@@ -20,6 +20,7 @@ / {
 
 	aliases {
 		bluetooth0 = &bluetooth0;
+		keyboard = &keyboard;
 		wifi0 = &wifi0;
 	};
 
@@ -91,3 +92,22 @@ &i2c4 {
 &fpwm1 {
 	status = "okay";
 };
+
+&mtp_mbox {
+	status = "okay";
+};
+
+&mtp_dart {
+	status = "okay";
+};
+
+&mtp_dockchannel {
+	status = "okay";
+};
+
+&mtp_hid {
+	status = "okay";
+
+	keyboard: keyboard {
+	};
+};
diff --git a/arch/arm64/boot/dts/apple/t8112-j493.dts b/arch/arm64/boot/dts/apple/t8112-j493.dts
index ec116da3e4dd..5deb2dd9802a 100644
--- a/arch/arm64/boot/dts/apple/t8112-j493.dts
+++ b/arch/arm64/boot/dts/apple/t8112-j493.dts
@@ -1,6 +1,6 @@
 // SPDX-License-Identifier: GPL-2.0+ OR MIT
 /*
- * Apple MacBook Pro (13-inch, M1, 2022)
+ * Apple MacBook Pro (13-inch, M2, 2022)
  *
  * target-type: J493
  *
@@ -24,6 +24,7 @@ / {
 	 */
 	aliases {
 		bluetooth0 = &bluetooth0;
+		keyboard = &keyboard;
 		touchbar0 = &touchbar0;
 		wifi0 = &wifi0;
 	};
@@ -146,3 +147,22 @@ touchbar0: touchbar@0 {
 		touchscreen-inverted-y;
 	};
 };
+
+&mtp_mbox {
+	status = "okay";
+};
+
+&mtp_dart {
+	status = "okay";
+};
+
+&mtp_dockchannel {
+	status = "okay";
+};
+
+&mtp_hid {
+	status = "okay";
+
+	keyboard: keyboard {
+	};
+};
diff --git a/arch/arm64/boot/dts/apple/t8122-j504.dts b/arch/arm64/boot/dts/apple/t8122-j504.dts
index 464491b55b01..0d93ac72fff9 100644
--- a/arch/arm64/boot/dts/apple/t8122-j504.dts
+++ b/arch/arm64/boot/dts/apple/t8122-j504.dts
@@ -18,6 +18,10 @@ / {
 	compatible = "apple,j504", "apple,t8122", "apple,arm-platform";
 	model = "Apple MacBook Pro (14-inch, M3, 2023)";
 
+	aliases {
+		keyboard = &keyboard;
+	};
+
 	led-controller {
 		compatible = "pwm-leds";
 		led-0 {
@@ -35,3 +39,21 @@ &fpwm1 {
 	status = "okay";
 };
 
+&mtp_mbox {
+	status = "okay";
+};
+
+&mtp_dart {
+	status = "okay";
+};
+
+&mtp_dockchannel {
+	status = "okay";
+};
+
+&mtp_hid {
+	status = "okay";
+
+	keyboard: keyboard {
+	};
+};
diff --git a/arch/arm64/boot/dts/apple/t8122-j613.dts b/arch/arm64/boot/dts/apple/t8122-j613.dts
index 51894ea705e7..e77b1ad869eb 100644
--- a/arch/arm64/boot/dts/apple/t8122-j613.dts
+++ b/arch/arm64/boot/dts/apple/t8122-j613.dts
@@ -17,6 +17,10 @@ / {
 	compatible = "apple,j613", "apple,t8122", "apple,arm-platform";
 	model = "Apple MacBook Air (13-inch, M3, 2024)";
 
+	aliases {
+		keyboard = &keyboard;
+	};
+
 	led-controller {
 		compatible = "pwm-leds";
 		led-0 {
@@ -33,3 +37,22 @@ led-0 {
 &fpwm1 {
 	status = "okay";
 };
+
+&mtp_mbox {
+	status = "okay";
+};
+
+&mtp_dart {
+	status = "okay";
+};
+
+&mtp_dockchannel {
+	status = "okay";
+};
+
+&mtp_hid {
+	status = "okay";
+
+	keyboard: keyboard {
+	};
+};
diff --git a/arch/arm64/boot/dts/apple/t8122-j615.dts b/arch/arm64/boot/dts/apple/t8122-j615.dts
index 2a1970c1bc90..5da0021d40f8 100644
--- a/arch/arm64/boot/dts/apple/t8122-j615.dts
+++ b/arch/arm64/boot/dts/apple/t8122-j615.dts
@@ -17,6 +17,10 @@ / {
 	compatible = "apple,j615", "apple,t8122", "apple,arm-platform";
 	model = "Apple MacBook Air (15-inch, M3, 2024)";
 
+	aliases {
+		keyboard = &keyboard;
+	};
+
 	led-controller {
 		compatible = "pwm-leds";
 		led-0 {
@@ -33,3 +37,22 @@ led-0 {
 &fpwm1 {
 	status = "okay";
 };
+
+&mtp_mbox {
+	status = "okay";
+};
+
+&mtp_dart {
+	status = "okay";
+};
+
+&mtp_dockchannel {
+	status = "okay";
+};
+
+&mtp_hid {
+	status = "okay";
+
+	keyboard: keyboard {
+	};
+};

-- 
2.51.2



^ permalink raw reply related

* Re: [PATCH 04/10] dt-bindings: input: apple: Add DockChannel HID transport
From: sashiko-bot @ 2026-06-30 13:10 UTC (permalink / raw)
  To: Michael Reeves via B4 Relay
  Cc: conor+dt, linux-input, devicetree, robh, dmitry.torokhov
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-4-506d936a1707@gmail.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The custom property `hid-country-code` lacks a vendor prefix.
--

commit 0afec3dbb1ad0bf9e9d68a29699cf412f978e322
Author: Michael Reeves <michael.reeves077@gmail.com>

dt-bindings: input: apple: Add DockChannel HID transport

This commit adds the device tree binding schema for the Apple DockChannel HID
transport. It defines the required properties including mailboxes, IOMMUs,
and an optional keyboard child node for layout variants.

> diff --git a/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml
> new file mode 100644
> index 0000000000000..dbba4fc389710
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/input/apple,dockchannel-hid.yaml
[ ... ]
> +  keyboard:
> +    type: object
> +    properties:
> +      hid-country-code:

[Severity: Low]
Should this custom property include a vendor prefix? Hardware-specific
properties not defined in a generic schema generally require a vendor prefix
(like apple,hid-country-code) to prevent future namespace collisions.

> +        $ref: /schemas/types.yaml#/definitions/uint32
> +        description:
> +          HID country code for the keyboard layout variant.
> +
> +    additionalProperties: false

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260630-apple-mtp-keyboard-final-v1-0-506d936a1707@gmail.com?part=4

^ permalink raw reply

* Re: [PATCH 08/10] HID: apple: Add DockChannel HID transport driver
From: Yureka Lilian @ 2026-06-30 13:21 UTC (permalink / raw)
  To: michael.reeves077, Sven Peter, Janne Grunau, Neal Gompa,
	Jassi Brar, Rob Herring, Krzysztof Kozlowski, Conor Dooley,
	Hector Martin, Joerg Roedel (AMD), Will Deacon, Robin Murphy,
	Dmitry Torokhov, Jiri Kosina, Benjamin Tissoires
  Cc: asahi, linux-arm-kernel, linux-kernel, devicetree, iommu,
	linux-input
In-Reply-To: <20260630-apple-mtp-keyboard-final-v1-8-506d936a1707@gmail.com>

On 6/30/26 14:54, Michael Reeves via B4 Relay wrote:
> From: Michael Reeves <michael.reeves077@gmail.com>
>
> Apple MTP exposes internal keyboard and trackpad interfaces over a HID
> transport carried by DockChannel.
>
> Add a transport driver that boots the MTP RTKit coprocessor, exchanges
> HID packets through the DockChannel mailbox, and registers child HID
> interfaces from devicetree.
>
> Co-developed-by: Hector Martin <marcan@marcan.st>
> Signed-off-by: Hector Martin <marcan@marcan.st>
> Signed-off-by: Michael Reeves <michael.reeves077@gmail.com>
> ---
>   MAINTAINERS                         |    1 +
>   drivers/hid/Kconfig                 |    2 +
>   drivers/hid/Makefile                |    2 +
>   drivers/hid/dockchannel/Kconfig     |   15 +
>   drivers/hid/dockchannel/Makefile    |    3 +
>   drivers/hid/dockchannel/apple-hid.c | 1130 +++++++++++++++++++++++++++++++++++
>   6 files changed, 1153 insertions(+)
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index ed68452c0ad6..0063276f0349 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -2620,6 +2620,7 @@ F:	drivers/clk/clk-apple-nco.c
>   F:	drivers/cpufreq/apple-soc-cpufreq.c
>   F:	drivers/dma/apple-admac.c
>   F:	drivers/gpio/gpio-macsmc.c
> +F:	drivers/hid/dockchannel/
>   F:	drivers/hwmon/macsmc-hwmon.c
>   F:	drivers/pmdomain/apple/
>   F:	drivers/i2c/busses/i2c-pasemi-core.c
> diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
> index f9bcaeb66385..f27cda601ede 100644
> --- a/drivers/hid/Kconfig
> +++ b/drivers/hid/Kconfig
> @@ -1488,6 +1488,8 @@ source "drivers/hid/surface-hid/Kconfig"
>   
>   source "drivers/hid/intel-thc-hid/Kconfig"
>   
> +source "drivers/hid/dockchannel/Kconfig"
> +
>   endif # HID
>   
>   # USB support may be used with HID disabled
> diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
> index 23e6e3dd0c56..c9b4b1aff247 100644
> --- a/drivers/hid/Makefile
> +++ b/drivers/hid/Makefile
> @@ -182,3 +182,5 @@ obj-$(CONFIG_AMD_SFH_HID)       += amd-sfh-hid/
>   obj-$(CONFIG_SURFACE_HID_CORE)  += surface-hid/
>   
>   obj-$(CONFIG_INTEL_THC_HID)     += intel-thc-hid/
> +
> +obj-$(CONFIG_APPLE_DOCKCHANNEL_HID)	+= dockchannel/
> diff --git a/drivers/hid/dockchannel/Kconfig b/drivers/hid/dockchannel/Kconfig
> new file mode 100644
> index 000000000000..fca09ef74403
> --- /dev/null
> +++ b/drivers/hid/dockchannel/Kconfig
> @@ -0,0 +1,15 @@
> +# SPDX-License-Identifier: GPL-2.0-only OR MIT
> +
> +config APPLE_DOCKCHANNEL_HID
> +	tristate "HID over Apple DockChannel"
> +	depends on APPLE_DOCKCHANNEL
> +	depends on APPLE_RTKIT
> +	depends on HID
> +	depends on INPUT
> +	depends on OF
> +	help
> +	  This provides a HID transport layer over the Apple DockChannel
> +	  mailbox interface. It is required to support the internal keyboard
> +	  and trackpad on M2 and later MacBook models.
> +
> +	  Say Y here if you have an M2 or later Apple MacBook.
> diff --git a/drivers/hid/dockchannel/Makefile b/drivers/hid/dockchannel/Makefile
> new file mode 100644
> index 000000000000..d1a82aa57a69
> --- /dev/null
> +++ b/drivers/hid/dockchannel/Makefile
> @@ -0,0 +1,3 @@
> +# SPDX-License-Identifier: GPL-2.0-only OR MIT
> +
> +obj-$(CONFIG_APPLE_DOCKCHANNEL_HID)	+= apple-hid.o
> diff --git a/drivers/hid/dockchannel/apple-hid.c b/drivers/hid/dockchannel/apple-hid.c
> new file mode 100644
> index 000000000000..162fcfb5ab1c
> --- /dev/null
> +++ b/drivers/hid/dockchannel/apple-hid.c
> @@ -0,0 +1,1130 @@
> +// SPDX-License-Identifier: GPL-2.0-only OR MIT
> +/*
> + * Apple DockChannel HID transport driver
> + *
> + * Copyright The Asahi Linux Contributors
> + */
> +
> +#include <linux/bitfield.h>
> +#include <linux/completion.h>
> +#include <linux/ctype.h>
> +#include <linux/delay.h>
> +#include <linux/device.h>
> +#include <linux/dma-mapping.h>
> +#include <linux/hid.h>
> +#include <linux/mailbox/apple-dockchannel.h>
> +#include <linux/mailbox_client.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/property.h>
> +#include <linux/slab.h>
> +#include <linux/soc/apple/rtkit.h>
> +#include <linux/spinlock.h>
> +#include <linux/string.h>
> +#include <linux/unaligned.h>
> +#include <linux/workqueue.h>
> +
> +#define APPLE_ASC_CPU_CONTROL		0x44
> +#define APPLE_ASC_CPU_CONTROL_RUN	BIT(4)
> +
> +#define COMMAND_TIMEOUT_MS	1000
> +#define START_TIMEOUT_MS	2000
> +
> +#define MAX_INTERFACES		16
> +
> +#define DCHID_MAX_PAYLOAD	0xffff
> +#define DCHID_CHECKSUM_LEN	4
> +#define DCHID_RX_BUF_SIZE	(sizeof(struct dchid_hdr) + DCHID_MAX_PAYLOAD + \
> +				 DCHID_CHECKSUM_LEN)
> +
> +#define DCHID_CHANNEL_CMD	0x11
> +#define DCHID_CHANNEL_REPORT	0x12
> +#define DCHID_CHECKSUM_SEED	0xffffffff
> +
> +struct dchid_hdr {
> +	u8 hdr_len;
> +	u8 channel;
> +	__le16 length;
> +	u8 seq;
> +	u8 iface;
> +	__le16 pad;
> +} __packed;
> +
> +#define IFACE_COMM	0
> +
> +#define FLAGS_GROUP	GENMASK(7, 6)
> +#define FLAGS_REQ	GENMASK(5, 0)
> +
> +#define REQ_SET_REPORT	0
> +#define REQ_GET_REPORT	1
> +
> +struct dchid_subhdr {
> +	u8 flags;
> +	u8 unk;
> +	__le16 length;
> +	__le32 retcode;
> +} __packed;
> +
> +#define EVENT_INIT	0xf0
> +#define EVENT_READY	0xf1
> +
> +struct dchid_init_hdr {
> +	u8 type;
> +	u8 unk1;
> +	u8 unk2;
> +	u8 iface;
> +	char name[16];
> +	u8 more_packets;
> +	u8 unkpad;
> +} __packed;
> +
> +#define INIT_HID_DESCRIPTOR	0
> +#define INIT_TERMINATOR		2
> +#define INIT_PRODUCT_NAME	7
> +
> +#define CMD_RESET_INTERFACE	0x40
> +#define CMD_RESET_INTERFACE_SUB	1
> +#define CMD_ENABLE_INTERFACE	0xb4
> +
> +struct dchid_init_block_hdr {
> +	__le16 type;
> +	__le16 length;
> +} __packed;
> +
> +#define STM_REPORT_ID		0x10
> +#define STM_REPORT_SERIAL	0x11
> +
> +struct dchid_stm_id {
> +	u8 unk;
> +	__le16 vendor_id;
> +	__le16 product_id;
> +	__le16 version_number;
> +	u8 unk2;
> +	u8 unk3;
> +	u8 keyboard_type;
> +	u8 serial_length;
> +	/* Serial follows, but we grab it with a different report. */
> +} __packed;
> +
> +struct dchid_work {
> +	struct work_struct work;
> +	struct dchid_iface *iface;
> +
> +	struct dchid_hdr hdr;
> +	u8 data[];
> +};
> +
> +struct dchid_iface {
> +	struct dchid_dev *dchid;
> +	struct hid_device *hid;
> +	struct workqueue_struct *wq;
> +
> +	bool creating;
> +	struct work_struct create_work;
> +
> +	int index;
> +	const char *name;
> +	struct fwnode_handle *fwnode;
> +
> +	u8 tx_seq;
> +	bool deferred;
> +	bool starting;
> +	bool open;
> +	struct completion ready;
> +
> +	void *hid_desc;
> +	size_t hid_desc_len;
> +
> +	/* Lock for command submission state below */
> +	spinlock_t out_lock;
> +	u32 out_flags;
> +	int out_report;
> +	u32 retcode;
> +	void *resp_buf;
> +	size_t resp_size;
> +	struct completion out_complete;
> +};
> +
> +struct dchid_dev {
> +	struct device *dev;
> +	struct mbox_client dc_mbox_client;
> +	struct mbox_chan *dc_mbox;
> +
> +	struct apple_rtkit *rtk;
> +	void __iomem *asc_base;
> +	void __iomem *sram_base;
> +	struct resource sram_res;
> +
> +	bool id_ready;
> +	struct dchid_stm_id device_id;
> +	char serial[64];
> +
> +	u8 *rx_buf;
> +	size_t rx_len;
> +
> +	struct dchid_iface *comm;
> +	struct mutex ifaces_lock; /* protects ifaces array */
> +	struct dchid_iface *ifaces[MAX_INTERFACES];
> +
> +	/* Workqueue to asynchronously create HID devices */
> +	struct workqueue_struct *new_iface_wq;
> +};
> +
> +static void dchid_destroy_wq(void *data)
> +{
> +	struct workqueue_struct *wq = data;
> +
> +	destroy_workqueue(wq);
> +}
> +
> +static void dchid_fwnode_release(void *data)
> +{
> +	fwnode_handle_put(data);
> +}
> +
> +static void dchid_free_mbox(void *data)
> +{
> +	mbox_free_channel(data);
> +}
> +
> +static u32 dchid_checksum(const void *data, size_t len)
> +{
> +	const u8 *p = data;
> +	u32 sum = 0;
> +	int i;
> +
> +	while (len >= sizeof(u32)) {
> +		sum += get_unaligned_le32(p);
> +		p += sizeof(u32);
> +		len -= sizeof(u32);
> +	}
> +
> +	if (len) {
> +		u32 tmp = 0;
> +
> +		for (i = 0; i < len; i++)
> +			tmp |= p[i] << (i * 8);
> +		sum += tmp;
> +	}
> +
> +	return sum;
> +}
> +
> +static struct dchid_iface *
> +dchid_get_interface(struct dchid_dev *dchid, int index, const char *name)
> +{
> +	struct dchid_iface *iface;
> +	struct fwnode_handle *fwnode;
> +	int ret;
> +
> +	if (index >= MAX_INTERFACES) {
> +		dev_err(dchid->dev, "interface index %d out of range\n", index);
> +		return NULL;
> +	}
> +
> +	mutex_lock(&dchid->ifaces_lock);
> +	if (dchid->ifaces[index]) {
> +		iface = dchid->ifaces[index];
> +		mutex_unlock(&dchid->ifaces_lock);
> +		return iface;
> +	}
> +
> +	iface = devm_kzalloc(dchid->dev, sizeof(*iface), GFP_KERNEL);
> +	if (!iface) {
> +		mutex_unlock(&dchid->ifaces_lock);
> +		return NULL;
> +	}
> +
> +	iface->index = index;
> +	iface->name = devm_kstrdup(dchid->dev, name, GFP_KERNEL);
> +	if (!iface->name) {
> +		mutex_unlock(&dchid->ifaces_lock);
> +		return NULL;
> +	}
> +
> +	iface->dchid = dchid;
> +	iface->out_report = -1;
> +	init_completion(&iface->out_complete);
> +	init_completion(&iface->ready);
> +	spin_lock_init(&iface->out_lock);
> +
> +	iface->wq = alloc_ordered_workqueue("dchid-%s", 0, iface->name);
> +	if (!iface->wq) {
> +		mutex_unlock(&dchid->ifaces_lock);
> +		return NULL;
> +	}
> +
> +	ret = devm_add_action_or_reset(dchid->dev, dchid_destroy_wq, iface->wq);
> +	if (ret) {
> +		mutex_unlock(&dchid->ifaces_lock);
> +		return NULL;
> +	}
> +
> +	if (!strcmp(name, "comm")) {
> +		/* Comm is not a HID subdevice */
> +		dchid->ifaces[index] = iface;
> +		mutex_unlock(&dchid->ifaces_lock);
> +		return iface;
> +	}
> +
> +	fwnode = device_get_named_child_node(dchid->dev, name);
> +	if (fwnode) {
> +		iface->fwnode = fwnode;
> +		ret = devm_add_action_or_reset(dchid->dev, dchid_fwnode_release,
> +					       iface->fwnode);
> +		if (ret) {
> +			mutex_unlock(&dchid->ifaces_lock);
> +			return NULL;
> +		}
> +	} else {
> +		iface->fwnode = dev_fwnode(dchid->dev);
> +	}
> +
> +	dchid->ifaces[index] = iface;
> +	mutex_unlock(&dchid->ifaces_lock);
> +	return iface;
> +}
> +
> +static int dchid_send(struct dchid_iface *iface, u32 flags, const void *msg,
> +		      size_t size)
> +{
> +	struct dchid_dev *dchid = iface->dchid;
> +	size_t payload_padded = round_up(size, sizeof(u32));
> +	size_t total_len = sizeof(struct dchid_hdr) + sizeof(struct dchid_subhdr) +
> +			   payload_padded + DCHID_CHECKSUM_LEN;
> +	struct apple_dockchannel_msg dc_msg;
> +	struct dchid_hdr *hdr;
> +	struct dchid_subhdr *sub;
> +	u32 *checksum_ptr;
> +	u8 *buf;
> +	int ret;
> +
> +	if (total_len > DCHID_RX_BUF_SIZE)
> +		return -EINVAL;
> +
> +	buf = kzalloc(total_len, GFP_KERNEL);
> +	if (!buf)
> +		return -ENOMEM;
> +
> +	hdr = (struct dchid_hdr *)buf;
> +	sub = (struct dchid_subhdr *)(buf + sizeof(*hdr));
> +	checksum_ptr = (u32 *)(buf + total_len - DCHID_CHECKSUM_LEN);
> +
> +	hdr->hdr_len = sizeof(*hdr);
> +	hdr->channel = DCHID_CHANNEL_CMD;
> +	hdr->length = cpu_to_le16(payload_padded + sizeof(*sub));
> +	hdr->seq = iface->tx_seq;
> +	hdr->iface = iface->index;
> +
> +	sub->flags = (u8)flags;
> +	sub->length = cpu_to_le16(size);
> +
> +	memcpy(buf + sizeof(*hdr) + sizeof(*sub), msg, size);
> +
> +	*checksum_ptr = 0xffffffff - dchid_checksum(buf, total_len - DCHID_CHECKSUM_LEN);
> +
> +	dc_msg.data = buf;
> +	dc_msg.len = total_len;
> +	ret = mbox_send_message(dchid->dc_mbox, &dc_msg);
> +	kfree(buf);
> +
> +	return ret < 0 ? ret : 0;
> +}
> +
> +static int dchid_cmd(struct dchid_iface *iface, u32 type, u32 req,
> +		     void *data, size_t size, void *resp_buf, size_t resp_size)
> +{
> +	unsigned long flags;
> +	int ret;
> +	int report_id;
> +	bool timed_out = false;
> +	u32 out_flags;
> +
> +	if (size < 1)
> +		return -EINVAL;
> +
> +	report_id = *(u8 *)data;
> +	out_flags = FIELD_PREP(FLAGS_GROUP, type) | FIELD_PREP(FLAGS_REQ, req);
> +
> +	spin_lock_irqsave(&iface->out_lock, flags);
> +
> +	/* Only one command can be in flight per interface */
> +	if (WARN_ON(iface->out_report != -1)) {
> +		spin_unlock_irqrestore(&iface->out_lock, flags);
> +		return -EBUSY;
> +	}
> +
> +	iface->out_report = report_id;
> +	iface->out_flags = out_flags;
> +	iface->retcode = 0;
> +	iface->resp_buf = resp_buf;
> +	iface->resp_size = resp_size;
> +	reinit_completion(&iface->out_complete);
> +
> +	spin_unlock_irqrestore(&iface->out_lock, flags);
> +
> +	ret = dchid_send(iface, out_flags, data, size);
> +	if (ret < 0) {
> +		spin_lock_irqsave(&iface->out_lock, flags);
> +		iface->out_report = -1;
> +		iface->resp_buf = NULL;
> +		iface->resp_size = 0;
> +		spin_unlock_irqrestore(&iface->out_lock, flags);
> +		return ret;
> +	}
> +
> +	if (!wait_for_completion_timeout(&iface->out_complete,
> +					 msecs_to_jiffies(COMMAND_TIMEOUT_MS))) {
> +		dev_err(iface->dchid->dev, "command 0x%x to iface %d (%s) timed out\n",
> +			report_id, iface->index, iface->name);
> +		timed_out = true;
> +	}
> +
> +	spin_lock_irqsave(&iface->out_lock, flags);
> +
> +	if (timed_out && iface->out_report == report_id) {
> +		ret = -ETIMEDOUT;
> +	} else if (iface->retcode) {
> +		dev_err(iface->dchid->dev,
> +			"command 0x%x to iface %d (%s) failed with err 0x%x\n",
> +			report_id, iface->index, iface->name, iface->retcode);
> +		ret = -EIO;
> +	} else {
> +		ret = iface->resp_size;
> +	}
> +
> +	iface->tx_seq++;
> +	iface->out_report = -1;
> +	iface->resp_buf = NULL;
> +	iface->resp_size = 0;
> +	spin_unlock_irqrestore(&iface->out_lock, flags);
> +
> +	return ret;
> +}
> +
> +static int dchid_comm_cmd(struct dchid_dev *dchid, void *cmd, size_t size)
> +{
> +	return dchid_cmd(dchid->comm, HID_FEATURE_REPORT, REQ_SET_REPORT,
> +			 cmd, size, NULL, 0);
> +}
> +
> +static int dchid_enable_interface(struct dchid_iface *iface)
> +{
> +	u8 cmd[] = { CMD_ENABLE_INTERFACE, iface->index };
> +
> +	return dchid_comm_cmd(iface->dchid, cmd, sizeof(cmd));
> +}
> +
> +static int dchid_reset_interface(struct dchid_iface *iface, int state)
> +{
> +	u8 cmd[] = { CMD_RESET_INTERFACE, CMD_RESET_INTERFACE_SUB, iface->index,
> +		     (u8)state };
> +
> +	return dchid_comm_cmd(iface->dchid, cmd, sizeof(cmd));
> +}
> +
> +static int dchid_start_interface(struct dchid_iface *iface)
> +{
> +	if (iface->starting)
> +		return -EINPROGRESS;
> +
> +	dev_dbg(iface->dchid->dev, "starting interface %s\n", iface->name);
> +
> +	iface->starting = true;
> +	dchid_reset_interface(iface, 0);
> +	dchid_reset_interface(iface, 2);
> +
> +	return 0;
> +}
> +
> +static int dchid_start(struct hid_device *hdev)
> +{
> +	return 0;
> +}
> +
> +static int dchid_open(struct hid_device *hdev)
> +{
> +	struct dchid_iface *iface = hdev->driver_data;
> +	int ret;
> +
> +	if (!completion_done(&iface->ready)) {
> +		ret = dchid_start_interface(iface);
> +		if (ret < 0)
> +			return ret;
> +
> +		if (!wait_for_completion_timeout(&iface->ready,
> +						 msecs_to_jiffies(START_TIMEOUT_MS))) {
> +			dev_err(iface->dchid->dev, "iface %s start timed out\n",
> +				iface->name);
> +			return -ETIMEDOUT;
> +		}
> +	}
> +
> +	iface->open = true;
> +	return 0;
> +}
> +
> +static void dchid_close(struct hid_device *hdev)
> +{
> +	struct dchid_iface *iface = hdev->driver_data;
> +
> +	iface->open = false;
> +}
> +
> +static int dchid_parse(struct hid_device *hdev)
> +{
> +	struct dchid_iface *iface = hdev->driver_data;
> +
> +	return hid_parse_report(hdev, iface->hid_desc, iface->hid_desc_len);
> +}
> +
> +/* Note: buf excludes report number. */
> +static int dchid_get_report_cmd(struct dchid_iface *iface, u8 reportnum,
> +				void *buf, size_t len)
> +{
> +	int ret;
> +
> +	ret = dchid_cmd(iface, HID_FEATURE_REPORT, REQ_GET_REPORT, &reportnum, 1,
> +			buf, len);
> +
> +	return ret <= 0 ? ret : ret - 1;
> +}
> +
> +/* Note: buf includes report number. */
> +static int dchid_set_report(struct dchid_iface *iface, void *buf, size_t len)
> +{
> +	return dchid_cmd(iface, HID_OUTPUT_REPORT, REQ_SET_REPORT, buf, len,
> +			 NULL, 0);
> +}
> +
> +static int dchid_raw_request(struct hid_device *hdev, unsigned char reportnum,
> +			     __u8 *buf, size_t len, unsigned char rtype,
> +			     int reqtype)
> +{
> +	struct dchid_iface *iface = hdev->driver_data;
> +
> +	switch (reqtype) {
> +	case HID_REQ_GET_REPORT:
> +		if (len < 1)
> +			return -EINVAL;
> +
> +		buf[0] = reportnum;
> +		return dchid_cmd(iface, rtype, REQ_GET_REPORT, &reportnum, 1,
> +				 buf + 1, len - 1);
> +	case HID_REQ_SET_REPORT:
> +		return dchid_set_report(iface, buf, len);
> +	default:
> +		return -EIO;
> +	}
> +}
> +
> +static const struct hid_ll_driver dchid_ll = {
> +	.start = dchid_start,
> +	.open = dchid_open,
> +	.close = dchid_close,
> +	.parse = dchid_parse,
> +	.raw_request = dchid_raw_request,
> +};
> +
> +static void dchid_create_interface_work(struct work_struct *ws)
> +{
> +	struct dchid_iface *iface = container_of(ws, struct dchid_iface, create_work);
> +	struct dchid_dev *dchid = iface->dchid;
> +	struct hid_device *hid;
> +	char cap_name[16];
> +	int ret;
> +
> +	if (iface->hid) {
> +		dev_warn(dchid->dev, "interface %s already created\n", iface->name);
> +		goto done;
> +	}
> +
> +	ret = dchid_enable_interface(iface);
> +	if (ret < 0) {
> +		dev_warn(dchid->dev, "failed to enable %s: %d\n", iface->name, ret);
> +		goto done;
> +	}
> +
> +	iface->deferred = false;
> +
> +	hid = hid_allocate_device();
> +	if (IS_ERR(hid))
> +		goto done;
> +
> +	strscpy(cap_name, iface->name, sizeof(cap_name));
> +	if (cap_name[0])
> +		cap_name[0] = toupper(cap_name[0]);
> +	snprintf(hid->name, sizeof(hid->name), "Apple DockChannel %s", cap_name);
> +
> +	snprintf(hid->phys, sizeof(hid->phys), "%s.%d", dev_name(dchid->dev),
> +		 iface->index);
> +	strscpy(hid->uniq, dchid->serial, sizeof(hid->uniq));
If the keyboard appeared before stm, dchip->serial might be uninitialized.
> +
> +	hid->ll_driver = &dchid_ll;
> +	hid->bus = BUS_HOST;
> +	hid->vendor = le16_to_cpu(dchid->device_id.vendor_id);
> +	hid->product = le16_to_cpu(dchid->device_id.product_id);
> +	hid->version = le16_to_cpu(dchid->device_id.version_number);
> +	hid->type = HID_TYPE_OTHER;
> +	if (!strcmp(iface->name, "keyboard")) {
> +		u32 country_code;
> +
> +		hid->group = HID_GROUP_APPLE_DOCKCHANNEL;
> +
> +		/*
> +		 * The device provides no reliable way to get the keyboard
> +		 * country code, so board devicetrees provide it instead,
> +		 * filled by the bootloader.
> +		 */
> +		if (!fwnode_property_read_u32(iface->fwnode, "hid-country-code",
> +					      &country_code))
> +			hid->country = country_code;
> +	}
> +
> +	hid->dev.parent = iface->dchid->dev;
> +	hid->driver_data = iface;
> +	iface->hid = hid;
> +
> +	ret = hid_add_device(hid);
> +	if (ret < 0) {
> +		iface->hid = NULL;
> +		hid_destroy_device(hid);
> +		dev_warn(iface->dchid->dev, "failed to register HID device %s\n",
> +			 iface->name);
> +	}
> +
> +done:
> +	iface->creating = false;
> +}
> +
> +static int dchid_create_interface(struct dchid_iface *iface)
> +{
> +	if (iface->creating)
> +		return -EBUSY;
> +
> +	iface->creating = true;
> +	INIT_WORK(&iface->create_work, dchid_create_interface_work);
> +	return queue_work(iface->dchid->new_iface_wq, &iface->create_work);
> +}
> +
> +static void dchid_handle_descriptor(struct dchid_iface *iface, void *hid_desc,
> +				    size_t desc_len)
> +{
> +	u8 *rdesc;
> +	int i;
> +
> +	if (iface->hid)
> +		return;
> +
> +	rdesc = devm_kmemdup(iface->dchid->dev, hid_desc, desc_len,
> +			     GFP_KERNEL);
> +	if (!rdesc)
> +		return;
> +
> +	/* Fix up oversized report sizes in DockChannel report descriptors */
> +	if (desc_len >= 5) {
> +		for (i = 0; i <= (int)desc_len - 5; i++) {
> +			if (rdesc[i] == 0x76 && rdesc[i + 1] == 0x00 &&
> +			    rdesc[i + 2] == 0x40 && rdesc[i + 3] == 0x95) {
> +				u8 count = rdesc[i + 4];
> +
> +				if (count > 0 && count < 32) {
> +					dev_info(iface->dchid->dev,
> +						 "fixing up interface %s (%d) report size\n",
> +						 iface->name, iface->index);
> +					rdesc[i] = 0x75;
> +					rdesc[i + 1] = 0x08;
> +					rdesc[i + 2] = 0x96;
> +					rdesc[i + 3] = 0x00;
> +					rdesc[i + 4] = count * 8;
> +				}
> +			}
> +		}
> +	}
> +
> +	iface->hid_desc = rdesc;
> +	iface->hid_desc_len = desc_len;
> +}
> +
> +static void dchid_handle_ready(struct dchid_dev *dchid, void *data, size_t length)
> +{
> +	struct dchid_iface *iface;
> +	u8 *pkt = data;
> +	u8 index;
> +	int i;
> +	int ret;
> +
> +	if (length < 2)
> +		return;
> +
> +	index = pkt[1];
> +	if (index >= MAX_INTERFACES)
> +		return;
> +
> +	iface = dchid->ifaces[index];
> +	if (!iface)
> +		return;
> +
> +	dev_dbg(dchid->dev, "interface %s is now ready\n", iface->name);
> +	complete_all(&iface->ready);
> +
> +	/* When STM is ready, grab global device info */
> +	if (!strcmp(iface->name, "stm")) {
> +		ret = dchid_get_report_cmd(iface, STM_REPORT_ID, &dchid->device_id,
> +					   sizeof(dchid->device_id));
> +		if (ret < (int)sizeof(dchid->device_id)) {
> +			dev_warn(iface->dchid->dev, "failed to get device ID from STM\n");
> +			/* Fake it and keep going. Things might still work. */
> +			memset(&dchid->device_id, 0, sizeof(dchid->device_id));
> +		}
> +
> +		ret = dchid_get_report_cmd(iface, STM_REPORT_SERIAL, dchid->serial,
> +					   sizeof(dchid->serial) - 1);
> +		if (ret < 0) {
> +			dev_warn(iface->dchid->dev, "failed to get serial from STM\n");
> +			dchid->serial[0] = 0;
> +		}
> +
> +		dchid->id_ready = true;
> +		for (i = 0; i < MAX_INTERFACES; i++) {
> +			if (!dchid->ifaces[i] || !dchid->ifaces[i]->deferred)
> +				continue;
> +			dchid_create_interface(dchid->ifaces[i]);
> +		}
> +	}
> +}
> +
> +static void dchid_handle_init(struct dchid_dev *dchid, void *data, size_t length)
> +{
> +	struct dchid_init_hdr *hdr = data;
> +	struct dchid_init_block_hdr *blk;
> +	struct dchid_iface *iface;
> +	u8 *p = data;
> +
> +	if (length < sizeof(*hdr))
> +		return;
> +
> +	iface = dchid_get_interface(dchid, hdr->iface, hdr->name);
> +	if (!iface)
> +		return;
> +
> +	p += sizeof(*hdr);
> +	length -= sizeof(*hdr);
> +
> +	while (length >= sizeof(*blk)) {
> +		u16 blk_len;
> +
> +		blk = (struct dchid_init_block_hdr *)p;
> +		p += sizeof(*blk);
> +		length -= sizeof(*blk);
> +
> +		blk_len = le16_to_cpu(blk->length);
> +		if (blk_len > length)
> +			break;
> +
> +		switch (le16_to_cpu(blk->type)) {
> +		case INIT_HID_DESCRIPTOR:
> +			dchid_handle_descriptor(iface, p, blk_len);
> +			break;
> +		case INIT_PRODUCT_NAME:
> +			if (blk_len > 0 && p[blk_len - 1] != 0)
> +				dev_warn(dchid->dev, "unterminated product name for %s\n",
> +					 iface->name);
> +			break;
> +		}
> +
> +		p += blk_len;
> +		length -= blk_len;
> +
> +		if (le16_to_cpu(blk->type) == INIT_TERMINATOR)
> +			break;
> +	}
> +
> +	if (hdr->more_packets)
> +		return;
> +
> +	/*
> +	 * Prefer to enable STM first, since it provides device IDs. Some
> +	 * firmware versions do not expose STM, so let the keyboard start
> +	 * without it.
> +	 */
> +	if (iface->dchid->id_ready || !strcmp(iface->name, "stm") ||
> +	    !strcmp(iface->name, "keyboard"))

I specifically asked for a mechanism to let the keyboard probe even on 
devices which do not expose/have stm. Thanks for adding that!
However, I think this might need some more sophisticated mechanism to 
decide whether the stm is still going to appear after the keyboard, or 
not at all. I'm not sure if there is a way to tell this at this point, 
or we need to add a timeout for the stm to appearing, which needs to 
expire before we create the other interfaces with fake serials.

> +		dchid_create_interface(iface);
> +	else
> +		iface->deferred = true;
> +}
> +
> +static void dchid_handle_event(struct dchid_dev *dchid, void *data, size_t length)
> +{
> +	u8 *p = data;
> +
> +	if (!length)
> +		return;
> +
> +	switch (*p) {
> +	case EVENT_INIT:
> +		dchid_handle_init(dchid, data, length);
> +		break;
> +	case EVENT_READY:
> +		dchid_handle_ready(dchid, data, length);
> +		break;
> +	}
> +}
> +
> +static void dchid_handle_report(struct dchid_iface *iface, void *data, size_t length)
> +{
> +	if (!iface->hid || !iface->open)
> +		return;
> +
> +	hid_input_report(iface->hid, HID_INPUT_REPORT, data, length, 1);
> +}
> +
> +static void dchid_packet_work(struct work_struct *ws)
> +{
> +	struct dchid_work *work = container_of(ws, struct dchid_work, work);
> +	struct dchid_subhdr *shdr = (void *)work->data;
> +	struct dchid_dev *dchid = work->iface->dchid;
> +	u16 hdr_len = le16_to_cpu(work->hdr.length);
> +	u16 sub_len;
> +	int type;
> +	u8 *payload;
> +
> +	if (hdr_len < sizeof(*shdr)) {
> +		dev_err(dchid->dev, "bad subheader length\n");
> +		goto done;
> +	}
> +
> +	sub_len = le16_to_cpu(shdr->length);
> +	if (sub_len > hdr_len - sizeof(*shdr)) {
> +		dev_err(dchid->dev, "bad subheader length\n");
> +		goto done;
> +	}
> +
> +	type = FIELD_GET(FLAGS_GROUP, shdr->flags);
> +	payload = work->data + sizeof(*shdr);
> +
> +	switch (type) {
> +	case HID_INPUT_REPORT:
> +		if (work->hdr.iface == IFACE_COMM)
> +			dchid_handle_event(dchid, payload, sub_len);
> +		else
> +			dchid_handle_report(work->iface, payload, sub_len);
> +		break;
> +	}
> +
> +done:
> +	kfree(work);
> +}
> +
> +static void dchid_handle_ack(struct dchid_iface *iface, struct dchid_hdr *hdr,
> +			     void *data)
> +{
> +	struct dchid_subhdr *shdr = data;
> +	u8 *payload = data + sizeof(*shdr);
> +	u16 hdr_len = le16_to_cpu(hdr->length);
> +	u16 sub_len = le16_to_cpu(shdr->length);
> +	unsigned long flags;
> +	bool complete_cmd = false;
> +
> +	if (hdr_len < sizeof(*shdr) || sub_len > hdr_len - sizeof(*shdr) ||
> +	    sub_len < 1)
> +		return;
> +
> +	spin_lock_irqsave(&iface->out_lock, flags);
> +
> +	if (shdr->flags == iface->out_flags && iface->tx_seq == hdr->seq &&
> +	    iface->out_report == payload[0]) {
> +		if (iface->resp_buf && iface->resp_size)
> +			memcpy(iface->resp_buf, payload + 1,
> +			       min_t(size_t, sub_len - 1, iface->resp_size));
> +
> +		iface->resp_size = sub_len;
> +		iface->out_report = -1;
> +		iface->retcode = le32_to_cpu(shdr->retcode);
> +		complete_cmd = true;
> +	}
> +
> +	spin_unlock_irqrestore(&iface->out_lock, flags);
> +
> +	if (complete_cmd)
> +		complete(&iface->out_complete);
> +}
> +
> +static void dchid_process_packet(struct dchid_dev *dchid, struct dchid_hdr *hdr,
> +				 u8 *payload, size_t payload_len, u8 *packet,
> +				 size_t packet_len)
> +{
> +	struct dchid_work *work;
> +
> +	if (dchid_checksum(packet, packet_len) != DCHID_CHECKSUM_SEED) {
> +		dev_err_ratelimited(dchid->dev, "checksum error\n");
> +		return;
> +	}
> +
> +	if (payload_len < sizeof(struct dchid_subhdr))
> +		return;
> +
> +	if (hdr->iface >= MAX_INTERFACES || !dchid->ifaces[hdr->iface])
> +		return;
> +
> +	if (hdr->channel == DCHID_CHANNEL_CMD) {
> +		dchid_handle_ack(dchid->ifaces[hdr->iface], hdr, payload);
> +		return;
> +	}
> +
> +	if (hdr->channel != DCHID_CHANNEL_REPORT)
> +		return;
> +
> +	work = kzalloc(sizeof(*work) + payload_len, GFP_ATOMIC);
> +	if (!work)
> +		return;
> +
> +	work->hdr = *hdr;
> +	work->iface = dchid->ifaces[hdr->iface];
> +	memcpy(work->data, payload, payload_len);
> +	INIT_WORK(&work->work, dchid_packet_work);
> +
> +	queue_work(work->iface->wq, &work->work);
> +}
> +
> +static void dchid_consume_rx(struct dchid_dev *dchid)
> +{
> +	while (dchid->rx_len >= sizeof(struct dchid_hdr)) {
> +		struct dchid_hdr *hdr = (struct dchid_hdr *)dchid->rx_buf;
> +		size_t payload_len;
> +		size_t packet_len;
> +
> +		if (hdr->hdr_len != sizeof(*hdr)) {
> +			dev_err_ratelimited(dchid->dev, "bad header length %u\n",
> +					    hdr->hdr_len);
> +			dchid->rx_len = 0;
> +			return;
> +		}
> +
> +		payload_len = le16_to_cpu(hdr->length);
> +		packet_len = sizeof(*hdr) + payload_len + DCHID_CHECKSUM_LEN;
> +		if (packet_len > DCHID_RX_BUF_SIZE) {
> +			dev_err_ratelimited(dchid->dev, "oversized packet %zu\n",
> +					    packet_len);
> +			dchid->rx_len = 0;
> +			return;
> +		}
> +
> +		if (dchid->rx_len < packet_len)
> +			return;
> +
> +		dchid_process_packet(dchid, hdr, dchid->rx_buf + sizeof(*hdr),
> +				     payload_len, dchid->rx_buf, packet_len);
> +
> +		dchid->rx_len -= packet_len;
> +		memmove(dchid->rx_buf, dchid->rx_buf + packet_len, dchid->rx_len);
> +	}
> +}
> +
> +static void dchid_rx_callback(struct mbox_client *cl, void *mssg)
> +{
> +	struct dchid_dev *dchid = container_of(cl, struct dchid_dev, dc_mbox_client);
> +	struct apple_dockchannel_msg *msg = mssg;
> +
> +	if (!msg || !msg->data || !msg->len)
> +		return;
> +
> +	if (msg->len > DCHID_RX_BUF_SIZE - dchid->rx_len) {
> +		dev_err_ratelimited(dchid->dev, "RX buffer overflow\n");
> +		dchid->rx_len = 0;
> +		return;
> +	}
> +
> +	memcpy(dchid->rx_buf + dchid->rx_len, msg->data, msg->len);
> +	dchid->rx_len += msg->len;
> +
> +	dchid_consume_rx(dchid);
> +}
> +
> +static int dchid_rtkit_shmem_setup(void *cookie, struct apple_rtkit_shmem *bfr)
> +{
> +	struct dchid_dev *dchid = cookie;
> +	struct resource res = {
> +		.start = bfr->iova,
> +		.end = bfr->iova + bfr->size - 1,
> +		.name = "rtkit_map",
> +	};
> +
> +	if (!bfr->iova) {
> +		bfr->buffer = dma_alloc_coherent(dchid->dev, bfr->size,
> +						 &bfr->iova, GFP_KERNEL);
> +		if (!bfr->buffer)
> +			return -ENOMEM;
> +		return 0;
> +	}
> +
> +	if (!dchid->sram_res.start)
> +		return -EFAULT;
> +
> +	res.flags = dchid->sram_res.flags;
> +	if (res.end < res.start || !resource_contains(&dchid->sram_res, &res))
> +		return -EFAULT;
> +
> +	bfr->iomem = dchid->sram_base + (res.start - dchid->sram_res.start);
> +	bfr->is_mapped = true;
> +
> +	return 0;
> +}
> +
> +static void dchid_rtkit_shmem_destroy(void *cookie, struct apple_rtkit_shmem *bfr)
> +{
> +	struct dchid_dev *dchid = cookie;
> +
> +	if (bfr->buffer)
> +		dma_free_coherent(dchid->dev, bfr->size, bfr->buffer, bfr->iova);
> +}
> +
> +static const struct apple_rtkit_ops dchid_rtkit_ops = {
> +	.shmem_setup = dchid_rtkit_shmem_setup,
> +	.shmem_destroy = dchid_rtkit_shmem_destroy,
> +};
> +
> +static int dchid_map_helper_cpu(struct platform_device *pdev, struct dchid_dev *dchid)
> +{
> +	struct resource *res;
> +
> +	dchid->asc_base = devm_platform_ioremap_resource_byname(pdev, "coproc-asc");
> +	if (IS_ERR(dchid->asc_base))
> +		return PTR_ERR(dchid->asc_base);
> +
> +	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "coproc-sram");
> +	if (!res)
> +		return -EINVAL;
> +
> +	dchid->sram_res = *res;
> +
> +	dchid->sram_base = devm_ioremap_resource(&pdev->dev, res);
> +	if (IS_ERR(dchid->sram_base))
> +		return PTR_ERR(dchid->sram_base);
> +
> +	return 0;
> +}
> +
> +static int dchid_probe(struct platform_device *pdev)
> +{
> +	struct device *dev = &pdev->dev;
> +	struct dchid_dev *dchid;
> +	int ret;
> +
> +	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(44));
> +	if (ret)
> +		return dev_err_probe(dev, ret, "failed to set DMA mask\n");
> +
> +	dchid = devm_kzalloc(dev, sizeof(*dchid), GFP_KERNEL);
> +	if (!dchid)
> +		return -ENOMEM;
> +
> +	dchid->rx_buf = devm_kmalloc(dev, DCHID_RX_BUF_SIZE, GFP_KERNEL);
> +	if (!dchid->rx_buf)
> +		return -ENOMEM;
> +
> +	dchid->dev = dev;
> +	mutex_init(&dchid->ifaces_lock);
> +	platform_set_drvdata(pdev, dchid);
> +
> +	ret = dchid_map_helper_cpu(pdev, dchid);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "failed to map helper CPU\n");
> +
> +	dchid->dc_mbox_client.dev = dev;
> +	dchid->dc_mbox_client.tx_block = true;
> +	dchid->dc_mbox_client.rx_callback = dchid_rx_callback;
> +
> +	dchid->dc_mbox = mbox_request_channel_byname(&dchid->dc_mbox_client,
> +						     "dockchannel");
> +	if (IS_ERR(dchid->dc_mbox))
> +		return dev_err_probe(dev, PTR_ERR(dchid->dc_mbox),
> +				     "failed to request DockChannel mailbox\n");
> +
> +	ret = devm_add_action_or_reset(dev, dchid_free_mbox, dchid->dc_mbox);
> +	if (ret)
> +		return ret;
> +
> +	dchid->rtk = devm_apple_rtkit_init(dev, dchid, "asc", 0, &dchid_rtkit_ops);
> +	if (IS_ERR(dchid->rtk))
> +		return dev_err_probe(dev, PTR_ERR(dchid->rtk), "failed to init RTKit\n");
> +
> +	writel_relaxed(APPLE_ASC_CPU_CONTROL_RUN,
> +		       dchid->asc_base + APPLE_ASC_CPU_CONTROL);
> +
> +	ret = apple_rtkit_wake(dchid->rtk);
> +	if (ret)
> +		return dev_err_probe(dev, ret, "failed to wake coprocessor\n");
> +
> +	dchid->new_iface_wq = alloc_ordered_workqueue("dchid-new", 0);
> +	if (!dchid->new_iface_wq)
> +		return dev_err_probe(dev, -ENOMEM, "failed to allocate workqueue\n");
> +
> +	ret = devm_add_action_or_reset(dev, dchid_destroy_wq, dchid->new_iface_wq);
> +	if (ret)
> +		return ret;
> +
> +	dchid->comm = dchid_get_interface(dchid, IFACE_COMM, "comm");
> +	if (!dchid->comm)
> +		return dev_err_probe(dev, -EIO, "failed to init comm interface\n");
> +
> +	return 0;
> +}
> +
> +static void dchid_remove(struct platform_device *pdev)
> +{
> +	struct dchid_dev *dchid = platform_get_drvdata(pdev);
> +	int i;
> +
> +	if (dchid->dc_mbox) {
> +		devm_release_action(&pdev->dev, dchid_free_mbox, dchid->dc_mbox);
> +		dchid->dc_mbox = NULL;
> +	}
> +
> +	if (dchid->rtk && apple_rtkit_is_running(dchid->rtk))
> +		apple_rtkit_quiesce(dchid->rtk);
> +
> +	if (dchid->asc_base)
> +		writel_relaxed(0, dchid->asc_base + APPLE_ASC_CPU_CONTROL);
> +
> +	for (i = 0; i < MAX_INTERFACES; i++) {
> +		struct dchid_iface *iface = dchid->ifaces[i];
> +
> +		if (!iface)
> +			continue;
> +
> +		cancel_work_sync(&iface->create_work);
> +		flush_workqueue(iface->wq);
> +
> +		if (iface->hid)
> +			hid_destroy_device(iface->hid);
> +	}
> +
> +	if (dchid->new_iface_wq)
> +		flush_workqueue(dchid->new_iface_wq);
> +}
> +
> +static const struct of_device_id dchid_of_match[] = {
> +	{ .compatible = "apple,t8122-dockchannel-hid" },
> +	{ .compatible = "apple,t8112-dockchannel-hid" },
> +	{},
> +};
> +MODULE_DEVICE_TABLE(of, dchid_of_match);
> +
> +static struct platform_driver dchid_platform_driver = {
> +	.driver = {
> +		.name = "dockchannel-hid",
> +		.of_match_table = dchid_of_match,
> +	},
> +	.probe = dchid_probe,
> +	.remove = dchid_remove,
> +};
> +module_platform_driver(dchid_platform_driver);
> +
> +MODULE_DESCRIPTION("Apple DockChannel HID transport driver");
> +MODULE_AUTHOR("Hector Martin <marcan@marcan.st>");
> +MODULE_AUTHOR("Michael Reeves <michael.reeves077@gmail.com>");
> +MODULE_LICENSE("Dual MIT/GPL");
>

^ permalink raw reply

* Re: [PATCH 05/13] mfd: replace linux/gpio.h inclusions
From: sashiko-bot @ 2026-06-30 13:28 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: olteanv, linux-phy, dmitry.torokhov, linux-input, vkoul,
	neil.armstrong, linux-sunxi
In-Reply-To: <20260629132633.1300009-6-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch introduces a duplicate `#include <linux/gpio/consumer.h>` in `include/linux/mfd/ti-lmu.h`.
--

commit 19751af1f4af7549cf9f140e38427847c8babbd4
Author: Arnd Bergmann <arnd@arndb.de>

mfd: replace linux/gpio.h inclusions

This commit replaces the deprecated linux/gpio.h inclusion across various MFD
drivers and headers, converting them to use either linux/gpio/consumer.h or
linux/gpio/legacy.h as needed.

[ ... ]

> diff --git a/include/linux/mfd/ti-lmu.h b/include/linux/mfd/ti-lmu.h
> index 0bc0e81997987..5040c7d1e1b92 100644
> --- a/include/linux/mfd/ti-lmu.h
> +++ b/include/linux/mfd/ti-lmu.h
> @@ -10,7 +10,7 @@
>  #ifndef __MFD_TI_LMU_H__
>  #define __MFD_TI_LMU_H__
>  
> -#include <linux/gpio.h>
> +#include <linux/gpio/consumer.h>
>  #include <linux/notifier.h>
>  #include <linux/regmap.h>
>  #include <linux/gpio/consumer.h>

[Severity: Low]
This isn't a bug, but <linux/gpio/consumer.h> is now included twice. Would it
be better to just drop the replaced include here since the consumer header is
already present a few lines below?

[ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260629132633.1300009-1-arnd@kernel.org?part=5

^ permalink raw reply

* Re: [PATCH 07/13] ASoC: replace linux/gpio.h inclusions
From: sashiko-bot @ 2026-06-30 13:28 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: linux-sunxi, linux-phy, linux-input, olteanv, vkoul,
	neil.armstrong, dmitry.torokhov
In-Reply-To: <20260629132633.1300009-8-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The patch introduces duplicate `#include <linux/gpio/consumer.h>` directives in `cs42l84.c` and `dmic.c`.
--

commit 5e88ad9a5863dbafbe414c688b31a85827de20da
Author: Arnd Bergmann <arnd@arndb.de>

ASoC: replace linux/gpio.h inclusions

This commit updates multiple ASoC codecs to use linux/gpio/consumer.h instead
of the deprecated linux/gpio.h header.

> diff --git a/sound/soc/codecs/cs42l84.c b/sound/soc/codecs/cs42l84.c
> index f2a58163de0ed..36c3abc21fedc 100644
> --- a/sound/soc/codecs/cs42l84.c
> +++ b/sound/soc/codecs/cs42l84.c
> @@ -16,7 +16,7 @@
>  #include <linux/init.h>
>  #include <linux/delay.h>
>  #include <linux/i2c.h>
> -#include <linux/gpio.h>
> +#include <linux/gpio/consumer.h>

[Severity: Low]
This isn't a bug, but does this create a duplicate include? It appears
linux/gpio/consumer.h is already included further down in this file. Could we
simply drop the linux/gpio.h include entirely?

>  #include <linux/regmap.h>
>  #include <linux/slab.h>
>  #include <linux/acpi.h>

[ ... ]

> diff --git a/sound/soc/codecs/dmic.c b/sound/soc/codecs/dmic.c
> index 61e1bf1b3c9e6..8b05d6f9b429b 100644
> --- a/sound/soc/codecs/dmic.c
> +++ b/sound/soc/codecs/dmic.c
> @@ -6,7 +6,7 @@
>   */
>  
>  #include <linux/delay.h>
> -#include <linux/gpio.h>
> +#include <linux/gpio/consumer.h>
>  #include <linux/gpio/consumer.h>

[Severity: Low]
This isn't a bug, but should we just delete the old include line? Replacing
it leaves two identical includes back-to-back.

>  #include <linux/platform_device.h>
>  #include <linux/regulator/consumer.h>

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260629132633.1300009-1-arnd@kernel.org?part=7

^ permalink raw reply

* Re: [PATCH 08/13] pcmcia: replace linux/gpio.h inclusions
From: sashiko-bot @ 2026-06-30 13:28 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: olteanv, linux-phy, linux-sunxi, vkoul, dmitry.torokhov,
	linux-input, neil.armstrong
In-Reply-To: <20260629132633.1300009-9-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Invalid resource cleanup on error path leading to potential double-free of a devres-managed GPIO descriptor.
--

commit a1a20647914d78d6a15655dea889e9d53c734136
Author: Arnd Bergmann <arnd@arndb.de>

pcmcia: replace linux/gpio.h inclusions

The pcmcia drivers all use the legacy interfaces, so convert to
include linux/gpio/legacy.h instead.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>

[ ... ]

> diff --git a/drivers/pcmcia/sa1100_h3600.c b/drivers/pcmcia/sa1100_h3600.c
> index 10cb99c20a7f5..6c52d90795eb4 100644
> --- a/drivers/pcmcia/sa1100_h3600.c
> +++ b/drivers/pcmcia/sa1100_h3600.c
> @@ -11,7 +11,7 @@
>  #include <linux/interrupt.h>
>  #include <linux/init.h>
>  #include <linux/delay.h>
> -#include <linux/gpio.h>
> +#include <linux/gpio/legacy.h>

[Severity: High]
This isn't a bug introduced by this patch, but I noticed a pre-existing
issue while reviewing the GPIO include changes.

Does the error path in h3600_pcmcia_hw_init() cause a double-free of a
devres-managed GPIO descriptor?

If gpio_request(H3XXX_EGPIO_OPT_NVRAM_ON) or subsequent legacy GPIO requests
fail, the code jumps to or falls through to the err01 label:

drivers/pcmcia/sa1100_h3600.c:h3600_pcmcia_hw_init() {
    ...
err06:  gpio_free(H3XXX_EGPIO_CARD_RESET);
err05:  gpio_free(H3XXX_EGPIO_OPT_RESET);
err04:  gpio_free(H3XXX_EGPIO_OPT_ON);
err03:  gpio_free(H3XXX_EGPIO_OPT_NVRAM_ON);
err01:  gpio_free(H3XXX_GPIO_PCMCIA_IRQ0);
        return err;
}

The err01 label incorrectly frees H3XXX_GPIO_PCMCIA_IRQ0. This GPIO is
managed by devres and was not explicitly requested using gpio_request() in
this function. 

When devres cleans up the failed probe, it will attempt to free the GPIO
again. This could lead to a double-free, gpiolib state corruption, kernel
warnings, or accidentally freeing a GPIO acquired by a concurrent driver.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260629132633.1300009-1-arnd@kernel.org?part=8

^ permalink raw reply

* Re: [PATCH 11/13] Input: matrix_keyboard - replace linux/gpio.h inclusion
From: sashiko-bot @ 2026-06-30 13:28 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: olteanv, linux-phy, linux-sunxi, dmitry.torokhov, vkoul,
	neil.armstrong, linux-input
In-Reply-To: <20260629132633.1300009-12-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Redundant header inclusion of <linux/gpio/consumer.h>
--

commit b60f4c5bf1d6c83cc34b3ab6030725489bd7c3e5
Author: Arnd Bergmann <arnd@arndb.de>

Input: matrix_keyboard - replace linux/gpio.h inclusion

This commit replaces the inclusion of the deprecated linux/gpio.h header
with linux/gpio/consumer.h in the matrix keypad driver. It prepares the
code for the removal of linux/gpio.h.

> diff --git a/drivers/input/keyboard/matrix_keypad.c b/drivers/input/keyboard/matrix_keypad.c
> index e50a6fea9a602..98d0269a978fe 100644
> --- a/drivers/input/keyboard/matrix_keypad.c
> +++ b/drivers/input/keyboard/matrix_keypad.c
> @@ -16,7 +16,7 @@
>  #include <linux/interrupt.h>
>  #include <linux/jiffies.h>
>  #include <linux/module.h>
> -#include <linux/gpio.h>
> +#include <linux/gpio/consumer.h>

[Severity: Low]
Does this result in a redundant inclusion? Looking at matrix_keypad.c,
<linux/gpio/consumer.h> is already included earlier in the file:

#include <linux/types.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/platform_device.h>

Could the old <linux/gpio.h> inclusion just be removed entirely instead of
being replaced?

>  #include <linux/input/matrix_keypad.h>
>  #include <linux/slab.h>
>  #include <linux/of.h>

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260629132633.1300009-1-arnd@kernel.org?part=11

^ permalink raw reply

* Re: [PATCH 12/13] gpib: gpio: replace linux/gpio.h inclusion
From: sashiko-bot @ 2026-06-30 13:28 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: linux-phy, linux-sunxi, neil.armstrong, vkoul, linux-input,
	dmitry.torokhov, olteanv
In-Reply-To: <20260629132633.1300009-13-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [Low] The patch introduces redundant duplicate `#include <linux/gpio/consumer.h>` statements in multiple files.

Pre-existing issues:
- [High] Missing bounds check on `priv->count` in `bb_DAV_interrupt()` can lead to a buffer overflow.
--

commit 198cff3fadd8f7ab5db98a0ff2a666f4ac7f1bf0
Author: Arnd Bergmann <arnd@arndb.de>

gpib: gpio: replace linux/gpio.h inclusion

linux/gpio.h is going away, so use linux/gpio/consumer.h instead.

> diff --git a/drivers/gpib/gpio/gpib_bitbang.c b/drivers/gpib/gpio/gpib_bitbang.c
> index 0e227980b493a..2e8d895db06a5 100644
> --- a/drivers/gpib/gpio/gpib_bitbang.c
> +++ b/drivers/gpib/gpio/gpib_bitbang.c
> @@ -64,7 +64,7 @@
>  #include <linux/gpio/consumer.h>
>  #include <linux/gpio/driver.h>
>  #include <linux/gpio/machine.h>
> -#include <linux/gpio.h>
> +#include <linux/gpio/consumer.h>

[Severity: Low]
This isn't a bug, but does this introduce a duplicate include of
linux/gpio/consumer.h? It appears to already be included a few lines above.

Similar duplicate includes might have been added to other files like
drivers/input/keyboard/matrix_keypad.c and drivers/net/phy/mdio_device.c
if they were also part of this series.

>  #include <linux/irq.h>
>  
>  static int sn7516x_used = 1, sn7516x;

While reviewing this file, I noticed a separate issue in bb_DAV_interrupt().

[Severity: High]
This is a pre-existing issue, but does this lack of a bounds check on
priv->count lead to a buffer overflow?

drivers/gpib/gpio/gpib_bitbang.c:bb_DAV_interrupt() {
        ...
        priv->rbuf[priv->count++] = get_data_lines();
        priv->end = !gpiod_get_value(EOI);
        ...
}

If the hardware line DAV goes low multiple times while priv->r_busy is set
without waiting for the normal NRFD/NDAC handshake, could priv->count exceed
the allocated buffer size and corrupt memory?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260629132633.1300009-1-arnd@kernel.org?part=12

^ permalink raw reply

* Re: [PATCH 13/13] gpiolib: remove linux/gpio.h
From: sashiko-bot @ 2026-06-30 13:28 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: linux-sunxi, dmitry.torokhov, neil.armstrong, olteanv, vkoul,
	linux-phy, linux-input
In-Reply-To: <20260629132633.1300009-14-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] Removing `include/linux/gpio.h` breaks the build for multiple drivers across the kernel tree that still include it and rely on it for legacy GPIO declarations.
- [Low] Typographical error in `drivers/gpio/TODO` where `legacy.h` is misspelled as `lagacy.h`.
--

commit eb8b9c8709d4fc96f518f3578deb6f0d05993604
Author: Arnd Bergmann <arnd@arndb.de>

gpiolib: remove linux/gpio.h

This commit removes the include/linux/gpio.h header entirely, cleaning up
leftover bits under the assumption that all drivers have been converted to
consumer.h or legacy.h. It also updates internal gpiolib includes and a 
TODO file accordingly.

> diff --git a/drivers/gpio/TODO b/drivers/gpio/TODO
> index 7ce80fde1f17e..894c7e1af252a 100644
> --- a/drivers/gpio/TODO
> +++ b/drivers/gpio/TODO
> @@ -52,9 +52,7 @@ Work items:
>    base can be made dynamic (set to -1) if CONFIG_GPIO_SYSFS is disabled.
>  
>  - When this work is complete (will require some of the items in the
> -  following ongoing work as well) we can delete the old global
> -  numberspace accessors from <linux/gpio.h> and eventually delete
> -  <linux/gpio.h> altogether.
> +  following ongoing work as well) we can delete <linux/gpio/lagacy.h>.
                                                                ^^^^^^^^
[Severity: Low]
This isn't a bug, but there is a typo here (lagacy.h instead of legacy.h).

[ ... ]

> diff --git a/include/linux/gpio.h b/include/linux/gpio.h
> deleted file mode 100644
> index b0d4942a65de2..0000000000000
> --- a/include/linux/gpio.h
> +++ /dev/null
> @@ -1,22 +0,0 @@
[ ... ]
> -#ifdef CONFIG_GPIOLIB_LEGACY
> -#include <linux/gpio/legacy.h>
> -#endif /* CONFIG_GPIOLIB_LEGACY */
> -
> -#endif /* __LINUX_GPIO_H */

[Severity: Low]
Does removing this header break the build for remaining unconverted legacy
GPIO consumers?

For example, drivers/input/keyboard/gpio_keys.c:gpio_keys_setup_key() still
appears to rely on this header for devm_gpio_request_one():

    error = devm_gpio_request_one(dev, button->gpio, GPIOF_IN, desc);

Similarly, drivers/leds/leds-gpio.c:gpio_led_get_gpiod() also relies on it:

    ret = devm_gpio_request_one(dev, template->gpio, GPIOF_OUT_INIT_LOW,
                                template->name);

And arch/x86/platform/olpc/olpc-xo1-sci.c:setup_ec_sci() still uses
gpio_request():

    r = gpio_request(OLPC_GPIO_ECSCI, "OLPC-ECSCI");

Will these drivers fail to compile since functions like devm_gpio_request_one()
and gpio_request() will now be missing or implicitly declared?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260629132633.1300009-1-arnd@kernel.org?part=13

^ permalink raw reply

* Re: [PATCH 06/13] [net-next] net: replace linux/gpio.h inclusions
From: sashiko-bot @ 2026-06-30 13:28 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: vkoul, dmitry.torokhov, linux-input, linux-sunxi, neil.armstrong,
	olteanv, linux-phy
In-Reply-To: <20260629132633.1300009-7-arnd@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Duplicate inclusion of `<linux/gpio/consumer.h>` caused by an unchecked find-and-replace.
--

commit cb55ce48e23e6753d04ac5f664585feb999f1fcd
Author: Arnd Bergmann <arnd@arndb.de>

net: replace linux/gpio.h inclusions

This commit replaces obsolete linux/gpio.h includes with linux/gpio/consumer.h
or linux/gpio/legacy.h across various network drivers where possible.

> diff --git a/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c b/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
> index 48b94ce774906..88c5c52e0e381 100644
> --- a/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
> +++ b/drivers/net/ethernet/oki-semi/pch_gbe/pch_gbe_main.c
> @@ -16,7 +16,7 @@
>  #include <linux/net_tstamp.h>
>  #include <linux/ptp_classify.h>
>  #include <linux/ptp_pch.h>
> -#include <linux/gpio.h>
> +#include <linux/gpio/consumer.h>

[Severity: Low]
Does this create a duplicate include in pch_gbe_main.c?

Since linux/gpio/consumer.h is already included earlier in the file alongside
linux/gpio/machine.h, changing this include results in it being included
twice. Can this replaced include simply be removed?

>  
>  #define PCH_GBE_MAR_ENTRIES		16

[ ... ]

> diff --git a/drivers/net/phy/mdio_device.c b/drivers/net/phy/mdio_device.c
> index 56080d3d2d25e..a18263d5bb029 100644
> --- a/drivers/net/phy/mdio_device.c
> +++ b/drivers/net/phy/mdio_device.c
> @@ -8,7 +8,7 @@
>  
>  #include <linux/delay.h>
>  #include <linux/errno.h>
> -#include <linux/gpio.h>
> +#include <linux/gpio/consumer.h>
>  #include <linux/gpio/consumer.h>

[Severity: Low]
Did the automated replacement result in consecutive duplicate includes here?

It looks like mdio_device.c already included linux/gpio/consumer.h right below
the old linux/gpio.h include. Should we just drop the old include entirely
instead of replacing it?

>  #include <linux/init.h>
>  #include <linux/interrupt.h>

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260629132633.1300009-1-arnd@kernel.org?part=6

^ permalink raw reply


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox