Skip to contentSkip to conversation

Add Apple SPI keyboard and trackpad driver

PATCH In review Ronald Tschalär 3 participants 9 messages Apr 19, 2019 – Jul 17, 2019

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v7 cover Add Apple SPI keyboard and trackpad driver

This changeset adds a driver for the SPI keyboard and trackpad on recent MacBook's and MacBook Pro's. The driver has seen a fair amount of use over the last 2 years (basically anybody running linux on these machines), with only relatively small changes in the last year or so. For those interested, the driver development has been hosted at
https://github.com/cb22/macbook12-spi-driver/ (as well as my clone at
https://github.com/roadrunner2/macbook12-spi-driver/).

The first patch fixes a problem during config. While it affects the drm tree, Andrzej Hajda has given his ok for this patch to be taken via the input tree because the second patch here depends on it.

The second patch contains the new applespi driver.

Changes in v7:
- Fixed unused variable warning introduced in previous patch series and accidently overlooked

Ronald Tschalär (2):
drm/bridge: sil_sii8620: make remote control optional.
Input: add Apple SPI keyboard and trackpad driver.

 drivers/gpu/drm/bridge/Kconfig          |    3 +-
 drivers/gpu/drm/bridge/sil-sii8620.c    |   10 +-
 drivers/input/keyboard/Kconfig          |   15 +
 drivers/input/keyboard/Makefile         |    1 +
 drivers/input/keyboard/applespi.c       | 1975 +++++++++++++++++++++++
 drivers/input/keyboard/applespi.h       |   29 +
 drivers/input/keyboard/applespi_trace.h |   94 ++
 7 files changed, 2122 insertions(+), 5 deletions(-)
 create mode 100644 drivers/input/keyboard/applespi.c
 create mode 100644 drivers/input/keyboard/applespi.h
 create mode 100644 drivers/input/keyboard/applespi_trace.h
Signature · 1 line
2.20.1
#1 lore raw

PATCH v7 1/2 drm/bridge: sil_sii8620: make remote control optional.

commit d6abe6df706c (drm/bridge: sil_sii8620: do not have a dependency of RC_CORE) changed the driver to select both RC_CORE and INPUT. However, this causes problems with other drivers, in particular an input driver that depends on MFD_INTEL_LPSS_PCI (to be added in a separate commit):

drivers/clk/Kconfig:9:error: recursive dependency detected!

  drivers/clk/Kconfig:9:        symbol COMMON_CLK is selected by MFD_INTEL_LPSS
  drivers/mfd/Kconfig:566:      symbol MFD_INTEL_LPSS is selected by MFD_INTEL_LPSS_PCI
  drivers/mfd/Kconfig:580:      symbol MFD_INTEL_LPSS_PCI is implied by KEYBOARD_APPLESPI
  drivers/input/keyboard/Kconfig:73:    symbol KEYBOARD_APPLESPI depends on INPUT
  drivers/input/Kconfig:8:      symbol INPUT is selected by DRM_SIL_SII8620
  drivers/gpu/drm/bridge/Kconfig:83:    symbol DRM_SIL_SII8620 depends on DRM_BRIDGE
  drivers/gpu/drm/bridge/Kconfig:1:     symbol DRM_BRIDGE is selected by DRM_PL111
  drivers/gpu/drm/pl111/Kconfig:1:      symbol DRM_PL111 depends on COMMON_CLK

According to the docs and general consensus, select should only be used for non user-visible symbols, but both RC_CORE and INPUT are user-visible. Furthermore almost all other references to INPUT throughout the kernel config are depends, not selects. For this reason the first part of this change reverts commit d6abe6df706c.

In order to address the original reason for commit d6abe6df706c, namely that not all boards use the remote controller functionality and hence should not need have to deal with RC_CORE, the second part of this change now makes the remote control support in the driver optional and contingent on RC_CORE being defined. And with this the hard dependency on INPUT also goes away as that is only needed if RC_CORE is defined (which in turn already depends on INPUT).

CC Inki Dae <[email protected]>
CC Andrzej Hajda <[email protected]>
CC Laurent Pinchart <[email protected]>
CC Dmitry Torokhov <[email protected]>
---
 drivers/gpu/drm/bridge/Kconfig       |  3 +--
 drivers/gpu/drm/bridge/sil-sii8620.c | 10 +++++++---
 2 files changed, 8 insertions(+), 5 deletions(-)
drivers/gpu/drm/bridge/Kconfig +1 -2
index 2fee47b0d50b..9cf07105b73a 100644
@@ -85,8 +85,7 @@ config DRM_SIL_SII8620
 	depends on OF
 	select DRM_KMS_HELPER
 	imply EXTCON
-	select INPUT
-	select RC_CORE
+	imply RC_CORE
 	help
 	  Silicon Image SII8620 HDMI/MHL bridge chip driver.
 
drivers/gpu/drm/bridge/sil-sii8620.c +7 -3
index a6e8f4591e63..cff3131aae6c 100644
@@ -1763,10 +1763,8 @@ static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
 
 	scancode &= MHL_RCP_KEY_ID_MASK;
 
-	if (!ctx->rc_dev) {
-		dev_dbg(ctx->dev, "RCP input device not initialized\n");
+	if (!IS_ENABLED(CONFIG_RC_CORE) || !ctx->rc_dev)
 		return false;
-	}
 
 	if (pressed)
 		rc_keydown(ctx->rc_dev, RC_PROTO_CEC, scancode, 0);
@@ -2103,6 +2101,9 @@ static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
 	struct rc_dev *rc_dev;
 	int ret;
 
+	if (!IS_ENABLED(CONFIG_RC_CORE))
+		return;
+
 	rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
 	if (!rc_dev) {
 		dev_err(ctx->dev, "Failed to allocate RC device\n");
@@ -2217,6 +2218,9 @@ static void sii8620_detach(struct drm_bridge *bridge)
 {
 	struct sii8620 *ctx = bridge_to_sii8620(bridge);
 
+	if (!IS_ENABLED(CONFIG_RC_CORE))
+		return;
+
 	rc_unregister_device(ctx->rc_dev);
 }
 
Signature · 1 line
2.20.1
#6 lore raw

PATCH v7 2/2 Input: add Apple SPI keyboard and trackpad driver.

In reply to Ronald Tschalär

The keyboard and trackpad on recent MacBook's (since 8,1) and MacBookPro's (13,* and 14,*) are attached to an SPI controller instead of USB, as previously. The higher level protocol is not publicly documented and hence has been reverse engineered. As a consequence there are still a number of unknown fields and commands. However, the known parts have been working well and received extensive testing and use.

In order for this driver to work, the proper SPI drivers need to be loaded too; for MB8,1 these are spi_pxa2xx_platform and spi_pxa2xx_pci; for all others they are spi_pxa2xx_platform and intel_lpss_pci. For this reason enabling this driver in the config implies enabling the above drivers.

---
 drivers/input/keyboard/Kconfig          |   15 +
 drivers/input/keyboard/Makefile         |    1 +
 drivers/input/keyboard/applespi.c       | 1975 +++++++++++++++++++++++
 drivers/input/keyboard/applespi.h       |   29 +
 drivers/input/keyboard/applespi_trace.h |   94 ++
 5 files changed, 2114 insertions(+)
 create mode 100644 drivers/input/keyboard/applespi.c
 create mode 100644 drivers/input/keyboard/applespi.h
 create mode 100644 drivers/input/keyboard/applespi_trace.h
drivers/input/keyboard/Kconfig +15 -0
index a878351f1643..d0a9e7fa2508 100644
@@ -70,6 +70,21 @@ config KEYBOARD_AMIGA
 config ATARI_KBD_CORE
 	bool
 
+config KEYBOARD_APPLESPI
+	tristate "Apple SPI keyboard and trackpad"
+	depends on ACPI && EFI
+	depends on SPI
+	depends on X86 || COMPILE_TEST
+	imply SPI_PXA2XX
+	imply SPI_PXA2XX_PCI
+	imply MFD_INTEL_LPSS_PCI
+	help
+	  Say Y here if you are running Linux on any Apple MacBook8,1 or later,
+	  or any MacBookPro13,* or MacBookPro14,*.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called applespi.
+
 config KEYBOARD_ATARI
 	tristate "Atari keyboard"
 	depends on ATARI
drivers/input/keyboard/Makefile +1 -0
index 182e92985dbf..9283fee2505a 100644
@@ -10,6 +10,7 @@ obj-$(CONFIG_KEYBOARD_ADP5520)		+= adp5520-keys.o
 obj-$(CONFIG_KEYBOARD_ADP5588)		+= adp5588-keys.o
 obj-$(CONFIG_KEYBOARD_ADP5589)		+= adp5589-keys.o
 obj-$(CONFIG_KEYBOARD_AMIGA)		+= amikbd.o
+obj-$(CONFIG_KEYBOARD_APPLESPI)		+= applespi.o
 obj-$(CONFIG_KEYBOARD_ATARI)		+= atakbd.o
 obj-$(CONFIG_KEYBOARD_ATKBD)		+= atkbd.o
 obj-$(CONFIG_KEYBOARD_BCM)		+= bcm-keypad.o
drivers/input/keyboard/applespi.c new file +1975 -0

1,975 lines not on this page · raw message

drivers/input/keyboard/applespi.h new file +29 -0
new file mode 100644
index 000000000000..7f5ab10c597a
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * MacBook (Pro) SPI keyboard and touchpad driver
+ *
+ * Copyright (c) 2015-2019 Federico Lorenzi
+ * Copyright (c) 2017-2019 Ronald Tschalär
+ */
+
+#ifndef _APPLESPI_H_
+#define _APPLESPI_H_
+
+enum applespi_evt_type {
+	ET_CMD_TP_INI = BIT(0),
+	ET_CMD_BL = BIT(1),
+	ET_CMD_CL = BIT(2),
+	ET_RD_KEYB = BIT(8),
+	ET_RD_TPAD = BIT(9),
+	ET_RD_UNKN = BIT(10),
+	ET_RD_IRQ = BIT(11),
+	ET_RD_CRC = BIT(12),
+};
+
+enum applespi_pkt_type {
+	PT_READ,
+	PT_WRITE,
+	PT_STATUS,
+};
+
+#endif /* _APPLESPI_H_ */
drivers/input/keyboard/applespi_trace.h new file +94 -0

94 lines not on this page · raw message

Signature · 1 line
2.20.1
#2 lore raw

Re: [PATCH v7 1/2] drm/bridge: sil_sii8620: make remote control optional.

In reply to Ronald Tschalär
Quoted 30 lines · Ronald Tschalär

commit d6abe6df706c (drm/bridge: sil_sii8620: do not have a dependency of RC_CORE) changed the driver to select both RC_CORE and INPUT. However, this causes problems with other drivers, in particular an input driver that depends on MFD_INTEL_LPSS_PCI (to be added in a separate commit):

drivers/clk/Kconfig:9:error: recursive dependency detected!

  drivers/clk/Kconfig:9:        symbol COMMON_CLK is selected by MFD_INTEL_LPSS
  drivers/mfd/Kconfig:566:      symbol MFD_INTEL_LPSS is selected by MFD_INTEL_LPSS_PCI
  drivers/mfd/Kconfig:580:      symbol MFD_INTEL_LPSS_PCI is implied by KEYBOARD_APPLESPI
  drivers/input/keyboard/Kconfig:73:    symbol KEYBOARD_APPLESPI depends on INPUT
  drivers/input/Kconfig:8:      symbol INPUT is selected by DRM_SIL_SII8620
  drivers/gpu/drm/bridge/Kconfig:83:    symbol DRM_SIL_SII8620 depends on DRM_BRIDGE
  drivers/gpu/drm/bridge/Kconfig:1:     symbol DRM_BRIDGE is selected by DRM_PL111
  drivers/gpu/drm/pl111/Kconfig:1:      symbol DRM_PL111 depends on COMMON_CLK

According to the docs and general consensus, select should only be used for non user-visible symbols, but both RC_CORE and INPUT are user-visible. Furthermore almost all other references to INPUT throughout the kernel config are depends, not selects. For this reason the first part of this change reverts commit d6abe6df706c.

In order to address the original reason for commit d6abe6df706c, namely that not all boards use the remote controller functionality and hence should not need have to deal with RC_CORE, the second part of this change now makes the remote control support in the driver optional and contingent on RC_CORE being defined. And with this the hard dependency on INPUT also goes away as that is only needed if RC_CORE is defined (which in turn already depends on INPUT).

Quoted 6 lines
CC Inki Dae <[email protected]>
CC Andrzej Hajda <[email protected]>
CC Laurent Pinchart <[email protected]>
CC Dmitry Torokhov <[email protected]>

Apparently this patch was not queued to kernel yet. If there are no objections I will queue it via drm-misc-next tree tomorrow.

Regards
Andrzej

#3 lore raw

Re: [PATCH v7 1/2] drm/bridge: sil_sii8620: make remote control optional.

Quoted 39 lines · Andrzej Hajda
Quoted 30 lines · Ronald Tschalär

commit d6abe6df706c (drm/bridge: sil_sii8620: do not have a dependency of RC_CORE) changed the driver to select both RC_CORE and INPUT. However, this causes problems with other drivers, in particular an input driver that depends on MFD_INTEL_LPSS_PCI (to be added in a separate commit):

drivers/clk/Kconfig:9:error: recursive dependency detected!

  drivers/clk/Kconfig:9:        symbol COMMON_CLK is selected by MFD_INTEL_LPSS
  drivers/mfd/Kconfig:566:      symbol MFD_INTEL_LPSS is selected by MFD_INTEL_LPSS_PCI
  drivers/mfd/Kconfig:580:      symbol MFD_INTEL_LPSS_PCI is implied by KEYBOARD_APPLESPI
  drivers/input/keyboard/Kconfig:73:    symbol KEYBOARD_APPLESPI depends on INPUT
  drivers/input/Kconfig:8:      symbol INPUT is selected by DRM_SIL_SII8620
  drivers/gpu/drm/bridge/Kconfig:83:    symbol DRM_SIL_SII8620 depends on DRM_BRIDGE
  drivers/gpu/drm/bridge/Kconfig:1:     symbol DRM_BRIDGE is selected by DRM_PL111
  drivers/gpu/drm/pl111/Kconfig:1:      symbol DRM_PL111 depends on COMMON_CLK

According to the docs and general consensus, select should only be used for non user-visible symbols, but both RC_CORE and INPUT are user-visible. Furthermore almost all other references to INPUT throughout the kernel config are depends, not selects. For this reason the first part of this change reverts commit d6abe6df706c.

In order to address the original reason for commit d6abe6df706c, namely that not all boards use the remote controller functionality and hence should not need have to deal with RC_CORE, the second part of this change now makes the remote control support in the driver optional and contingent on RC_CORE being defined. And with this the hard dependency on INPUT also goes away as that is only needed if RC_CORE is defined (which in turn already depends on INPUT).

Quoted 6 lines
CC Inki Dae <[email protected]>
CC Andrzej Hajda <[email protected]>
CC Laurent Pinchart <[email protected]>
CC Dmitry Torokhov <[email protected]>
Quoted 2 lines

Apparently this patch was not queued to kernel yet. If there are no objections I will queue it via drm-misc-next tree tomorrow.

If this patch set won't be queued for 5.3 then I guess that would be a good idea.

But may I ask what is preventing this patch set from being queued for upstream, so I can try and fix whatever the issue is?

Cheers,

Ronald

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

maintainer
#4 lore raw

Re: [PATCH v7 1/2] drm/bridge: sil_sii8620: make remote control optional.

Hi,

Quoted 41 lines · and then you die
Quoted 39 lines · Andrzej Hajda
Quoted 36 lines · Ronald Tschalär

commit d6abe6df706c (drm/bridge: sil_sii8620: do not have a dependency of RC_CORE) changed the driver to select both RC_CORE and INPUT. However, this causes problems with other drivers, in particular an input driver that depends on MFD_INTEL_LPSS_PCI (to be added in a separate commit):

drivers/clk/Kconfig:9:error: recursive dependency detected!

  drivers/clk/Kconfig:9:        symbol COMMON_CLK is selected by MFD_INTEL_LPSS
  drivers/mfd/Kconfig:566:      symbol MFD_INTEL_LPSS is selected by MFD_INTEL_LPSS_PCI
  drivers/mfd/Kconfig:580:      symbol MFD_INTEL_LPSS_PCI is implied by KEYBOARD_APPLESPI
  drivers/input/keyboard/Kconfig:73:    symbol KEYBOARD_APPLESPI depends on INPUT
  drivers/input/Kconfig:8:      symbol INPUT is selected by DRM_SIL_SII8620
  drivers/gpu/drm/bridge/Kconfig:83:    symbol DRM_SIL_SII8620 depends on DRM_BRIDGE
  drivers/gpu/drm/bridge/Kconfig:1:     symbol DRM_BRIDGE is selected by DRM_PL111
  drivers/gpu/drm/pl111/Kconfig:1:      symbol DRM_PL111 depends on COMMON_CLK

According to the docs and general consensus, select should only be used for non user-visible symbols, but both RC_CORE and INPUT are user-visible. Furthermore almost all other references to INPUT throughout the kernel config are depends, not selects. For this reason the first part of this change reverts commit d6abe6df706c.

In order to address the original reason for commit d6abe6df706c, namely that not all boards use the remote controller functionality and hence should not need have to deal with RC_CORE, the second part of this change now makes the remote control support in the driver optional and contingent on RC_CORE being defined. And with this the hard dependency on INPUT also goes away as that is only needed if RC_CORE is defined (which in turn already depends on INPUT).

CC Inki Dae <[email protected]>
CC Andrzej Hajda <[email protected]>
CC Laurent Pinchart <[email protected]>
CC Dmitry Torokhov <[email protected]>
Quoted 8 lines
Quoted 2 lines

Apparently this patch was not queued to kernel yet. If there are no objections I will queue it via drm-misc-next tree tomorrow.

If this patch set won't be queued for 5.3 then I guess that would be a good idea.

But may I ask what is preventing this patch set from being queued for upstream, so I can try and fix whatever the issue is?

As I mentioned in my pull request to Linux I will be picking up the Apple keyboard driver for this merge window even though it was not in next (my fault).

I created and immutable branch for this change if you'd like to pull it in so we do not duplicate commit and risk the conflicts (but I believe git should resolve it either way).

https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git id/5.2-sil_sii8620-rc-optional

Thanks.

Signature · 1 line
Dmitry
maintainer
#5 lore raw

Re: [PATCH v7 1/2] drm/bridge: sil_sii8620: make remote control optional.

Quoted 58 lines · Dmitry Torokhov

Hi,

Quoted 41 lines · and then you die
Quoted 39 lines · Andrzej Hajda
Quoted 36 lines · Ronald Tschalär

commit d6abe6df706c (drm/bridge: sil_sii8620: do not have a dependency of RC_CORE) changed the driver to select both RC_CORE and INPUT. However, this causes problems with other drivers, in particular an input driver that depends on MFD_INTEL_LPSS_PCI (to be added in a separate commit):

drivers/clk/Kconfig:9:error: recursive dependency detected!

  drivers/clk/Kconfig:9:        symbol COMMON_CLK is selected by MFD_INTEL_LPSS
  drivers/mfd/Kconfig:566:      symbol MFD_INTEL_LPSS is selected by MFD_INTEL_LPSS_PCI
  drivers/mfd/Kconfig:580:      symbol MFD_INTEL_LPSS_PCI is implied by KEYBOARD_APPLESPI
  drivers/input/keyboard/Kconfig:73:    symbol KEYBOARD_APPLESPI depends on INPUT
  drivers/input/Kconfig:8:      symbol INPUT is selected by DRM_SIL_SII8620
  drivers/gpu/drm/bridge/Kconfig:83:    symbol DRM_SIL_SII8620 depends on DRM_BRIDGE
  drivers/gpu/drm/bridge/Kconfig:1:     symbol DRM_BRIDGE is selected by DRM_PL111
  drivers/gpu/drm/pl111/Kconfig:1:      symbol DRM_PL111 depends on COMMON_CLK

According to the docs and general consensus, select should only be used for non user-visible symbols, but both RC_CORE and INPUT are user-visible. Furthermore almost all other references to INPUT throughout the kernel config are depends, not selects. For this reason the first part of this change reverts commit d6abe6df706c.

In order to address the original reason for commit d6abe6df706c, namely that not all boards use the remote controller functionality and hence should not need have to deal with RC_CORE, the second part of this change now makes the remote control support in the driver optional and contingent on RC_CORE being defined. And with this the hard dependency on INPUT also goes away as that is only needed if RC_CORE is defined (which in turn already depends on INPUT).

CC Inki Dae <[email protected]>
CC Andrzej Hajda <[email protected]>
CC Laurent Pinchart <[email protected]>
CC Dmitry Torokhov <[email protected]>
Quoted 8 lines
Quoted 2 lines

Apparently this patch was not queued to kernel yet. If there are no objections I will queue it via drm-misc-next tree tomorrow.

If this patch set won't be queued for 5.3 then I guess that would be a good idea.

But may I ask what is preventing this patch set from being queued for upstream, so I can try and fix whatever the issue is?

As I mentioned in my pull request to Linux I will be picking up the Apple keyboard driver for this merge window even though it was not in next (my fault).

Quoted 5 lines

I created and immutable branch for this change if you'd like to pull it in so we do not duplicate commit and risk the conflicts (but I believe git should resolve it either way).

https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git id/5.2-sil_sii8620-rc-optional

Sorry, that should have read:

https://git.kernel.org/pub/scm/linux/kernel/git/dtor/input.git
ib/5.2-sil_sii8620-rc-optional

Thanks.

Signature · 1 line
Dmitry
maintainer
#7 lore raw

Re: [PATCH v7 2/2] Input: add Apple SPI keyboard and trackpad driver.

In reply to Ronald Tschalär

Hi Ronald,

Quoted 7 lines · Ronald Tschalär

The keyboard and trackpad on recent MacBook's (since 8,1) and MacBookPro's (13,* and 14,*) are attached to an SPI controller instead of USB, as previously. The higher level protocol is not publicly documented and hence has been reverse engineered. As a consequence there are still a number of unknown fields and commands. However, the known parts have been working well and received extensive testing and use.

Quoted 5 lines

In order for this driver to work, the proper SPI drivers need to be loaded too; for MB8,1 these are spi_pxa2xx_platform and spi_pxa2xx_pci; for all others they are spi_pxa2xx_platform and intel_lpss_pci. For this reason enabling this driver in the config implies enabling the above drivers.

I applied the patch (but changed __u8 to u8 as that's preferred form for inside the kernel, and added error handling for input_mt_init_slots) but we need to do some more work on the driver.

My main issue is with registering touchpad device asynchronously, independent from the probe() function. This means (as far as I can tell) that any error is not really appropriately handled (as by that time it is too late to signal errors from probe()) and devm functions are not going to be called, leaving remnants of the resources in memory on driver unload. It also brings in issues with suspend/resume (what happens if you suspend really quickly while device is not registered yet?), etc, etc.

Can we switch to calling DEV_INFO command synchronously from probe()? If we are concerned about it taking relatively long time we can always annotate the driver as having probe_type = PROBE_PREFER_ASYNCHRONOUS so that other devices can be probed simultaneously with applespi.

Thanks.

Signature · 1 line
Dmitry
#8 lore raw

Re: [PATCH v7 2/2] Input: add Apple SPI keyboard and trackpad driver.

Hi Dmitry,

thanks for taking a look at this!

Quoted 16 lines · Dmitry Torokhov

Hi Ronald,

Quoted 7 lines · Ronald Tschalär

The keyboard and trackpad on recent MacBook's (since 8,1) and MacBookPro's (13,* and 14,*) are attached to an SPI controller instead of USB, as previously. The higher level protocol is not publicly documented and hence has been reverse engineered. As a consequence there are still a number of unknown fields and commands. However, the known parts have been working well and received extensive testing and use.

Quoted 5 lines

In order for this driver to work, the proper SPI drivers need to be loaded too; for MB8,1 these are spi_pxa2xx_platform and spi_pxa2xx_pci; for all others they are spi_pxa2xx_platform and intel_lpss_pci. For this reason enabling this driver in the config implies enabling the above drivers.

Quoted 3 lines

I applied the patch (but changed __u8 to u8 as that's preferred form for inside the kernel, and added error handling for input_mt_init_slots) but we need to do some more work on the driver.

Looks good.

Quoted 8 lines

My main issue is with registering touchpad device asynchronously, independent from the probe() function. This means (as far as I can tell) that any error is not really appropriately handled (as by that time it is too late to signal errors from probe()) and devm functions are not going to be called, leaving remnants of the resources in memory on driver unload. It also brings in issues with suspend/resume (what happens if you suspend really quickly while device is not registered yet?), etc, etc.

Yes, the lack of error propagation also bothered me a bit when I introduced the dev-info command, but I thought doing synchronous I/O operations in a probe function wasn't kosher. Happy to rectify that though.

Quoted 4 lines

Can we switch to calling DEV_INFO command synchronously from probe()? If we are concerned about it taking relatively long time we can always annotate the driver as having probe_type = PROBE_PREFER_ASYNCHRONOUS so that other devices can be probed simultaneously with applespi.

Normally the dev-info retrieval takes about 15ms - I presume that's fast enough?

Attached is a patch that does this now (on top of your changes above). The cancelling of outstanding spi requests in the error case is a bit ugly (I wish there were an exported spi-flush/spi-wait-for-queue-empty function or similar), but otherwise it's fairly straightforward.

Cheers,

Ronald

Quoted 1 line

From a03a7b42ac6ea0707165c8beba04c722984064be Mon Sep 17 00:00:00 2001

From: =?UTF-8?q?Ronald=20Tschal=C3=A4r?= <[email protected]>
Date: Wed, 17 Jul 2019 02:46:57 -0700
Subject: [PATCH] Input: applespi - register touchpad device synchronously in
probe.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This allows errors during registration to properly fail the probe function.

Doing this requires waiting for a response from the device inside the probe function. While this generally takes about 15ms, in case of errors it could be arbitrarily long, and hence a 3 second timeout is used.

This also adds 3 second timeouts to the drain functions to avoid the potential for suspend or remove hanging forever.

---
 drivers/input/keyboard/applespi.c | 125 ++++++++++++++++++++++++------
 1 file changed, 100 insertions(+), 25 deletions(-)
drivers/input/keyboard/applespi.c +100 -25
index 548737e7aeda..81a733a6ba1a 100644
@@ -48,12 +48,12 @@
 #include <linux/efi.h>
 #include <linux/input.h>
 #include <linux/input/mt.h>
+#include <linux/jiffies.h>
 #include <linux/leds.h>
 #include <linux/module.h>
 #include <linux/spinlock.h>
 #include <linux/spi/spi.h>
 #include <linux/wait.h>
-#include <linux/workqueue.h>
 
 #include <asm/barrier.h>
 #include <asm/unaligned.h>
@@ -411,7 +411,13 @@ struct applespi_data {
 	bool				read_active;
 	bool				write_active;
 
-	struct work_struct		work;
+	struct applespi_complete_info {
+		void				(*complete)(void *context);
+		struct applespi_data		*applespi;
+	}				spi_complete[2];
+	bool				cancel_spi;
+
+	wait_queue_head_t		tp_info_complete;
 	struct touchpad_info_protocol	rcvd_tp_info;
 
 	struct dentry			*debugfs_root;
@@ -593,13 +599,61 @@ static void applespi_setup_write_txfrs(struct applespi_data *applespi)
 	spi_message_add_tail(st_t, msg);
 }
 
+static bool applespi_async_outstanding(struct applespi_data *applespi)
+{
+	return applespi->spi_complete[0].complete ||
+	       applespi->spi_complete[1].complete;
+}
+
+static void applespi_async_complete(void *context)
+{
+	struct applespi_complete_info *info = context;
+	struct applespi_data *applespi = info->applespi;
+	unsigned long flags;
+
+	info->complete(applespi);
+
+	spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+
+	info->complete = NULL;
+
+	if (applespi->cancel_spi && !applespi_async_outstanding(applespi))
+		wake_up_all(&applespi->drain_complete);
+
+	spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
+}
+
 static int applespi_async(struct applespi_data *applespi,
 			  struct spi_message *message, void (*complete)(void *))
 {
-	message->complete = complete;
-	message->context = applespi;
+	struct applespi_complete_info *info;
+	int sts;
+
+	if (applespi->cancel_spi) {
+		if (!applespi_async_outstanding(applespi))
+			wake_up_all(&applespi->drain_complete);
+		return -ESHUTDOWN;
+	}
+
+	/*
+	 * There can only be at max 2 spi requests in flight, one for "reads"
+	 * and one for "writes".
+	 */
+	if (!applespi->spi_complete[0].complete)
+		info = &applespi->spi_complete[0];
+	else
+		info = &applespi->spi_complete[1];
+	info->complete = complete;
+	info->applespi = applespi;
+
+	message->complete = applespi_async_complete;
+	message->context = info;
+
+	sts = spi_async(applespi->spi, message);
+	if (sts)
+		info->complete = NULL;
 
-	return spi_async(applespi->spi, message);
+	return sts;
 }
 
 static inline bool applespi_check_write_status(struct applespi_data *applespi,
@@ -664,6 +718,7 @@ static int applespi_setup_spi(struct applespi_data *applespi)
 
 	spin_lock_init(&applespi->cmd_msg_lock);
 	init_waitqueue_head(&applespi->drain_complete);
+	init_waitqueue_head(&applespi->tp_info_complete);
 
 	return 0;
 }
@@ -1011,7 +1066,7 @@ static void report_tp_state(struct applespi_data *applespi,
 	const struct applespi_tp_info *tp_info = &applespi->tp_info;
 	int i, n;
 
-	/* touchpad_input_dev is set async in worker */
+	/* touchpad_input_dev is set async in probe */
 	input = smp_load_acquire(&applespi->touchpad_input_dev);
 	if (!input)
 		return;	/* touchpad isn't initialized yet */
@@ -1315,26 +1370,14 @@ applespi_register_touchpad_device(struct applespi_data *applespi,
 	return 0;
 }
 
-static void applespi_worker(struct work_struct *work)
-{
-	struct applespi_data *applespi =
-		container_of(work, struct applespi_data, work);
-
-	applespi_register_touchpad_device(applespi, &applespi->rcvd_tp_info);
-}
-
 static void applespi_handle_cmd_response(struct applespi_data *applespi,
 					 struct spi_packet *packet,
 					 struct message *message)
 {
 	if (packet->device == PACKET_DEV_INFO &&
 	    le16_to_cpu(message->type) == 0x1020) {
-		/*
-		 * We're not allowed to sleep here, but registering an input
-		 * device can sleep.
-		 */
 		applespi->rcvd_tp_info = message->tp_info;
-		schedule_work(&applespi->work);
+		wake_up_all(&applespi->tp_info_complete);
 		return;
 	}
 
@@ -1623,6 +1666,7 @@ static int applespi_probe(struct spi_device *spi)
 	struct applespi_data *applespi;
 	acpi_handle spi_handle = ACPI_HANDLE(&spi->dev);
 	acpi_status acpi_sts;
+	unsigned long flags;
 	int sts, i;
 	unsigned long long gpe, usb_status;
 
@@ -1641,8 +1685,6 @@ static int applespi_probe(struct spi_device *spi)
 
 	applespi->spi = spi;
 
-	INIT_WORK(&applespi->work, applespi_worker);
-
 	/* store the driver data */
 	spi_set_drvdata(spi, applespi);
 
@@ -1770,6 +1812,22 @@ static int applespi_probe(struct spi_device *spi)
 	/* trigger touchpad setup */
 	applespi_init(applespi, false);
 
+	/* set up the touchpad as a separate input device */
+	sts = wait_event_timeout(applespi->tp_info_complete,
+				 applespi->rcvd_tp_info.model_no,
+				 msecs_to_jiffies(3000));
+	if (!sts) {
+		dev_err(&applespi->spi->dev,
+			"Timed out waiting for device info\n");
+		sts = -ETIMEDOUT;
+		goto cancel_spi;
+	}
+
+	sts = applespi_register_touchpad_device(applespi,
+						&applespi->rcvd_tp_info);
+	if (sts)
+		goto cancel_spi;
+
 	/*
 	 * By default this device is not enabled for wakeup; but USB keyboards
 	 * generally are, so the expectation is that by default the keyboard
@@ -1820,6 +1878,19 @@ static int applespi_probe(struct spi_device *spi)
 	}
 
 	return 0;
+
+cancel_spi:
+	acpi_disable_gpe(NULL, applespi->gpe);
+	acpi_remove_gpe_handler(NULL, applespi->gpe, applespi_notify);
+
+	spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+	applespi->cancel_spi = true;
+	wait_event_lock_irq(applespi->drain_complete,
+			    !applespi_async_outstanding(applespi),
+			    applespi->cmd_msg_lock);
+	spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
+
+	return sts;
 }
 
 static void applespi_drain_writes(struct applespi_data *applespi)
@@ -1829,8 +1900,10 @@ static void applespi_drain_writes(struct applespi_data *applespi)
 	spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
 
 	applespi->drain = true;
-	wait_event_lock_irq(applespi->drain_complete, !applespi->write_active,
-			    applespi->cmd_msg_lock);
+	wait_event_lock_irq_timeout(applespi->drain_complete,
+				    !applespi->write_active,
+				    applespi->cmd_msg_lock,
+				    msecs_to_jiffies(3000));
 
 	spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
 }
@@ -1841,8 +1914,10 @@ static void applespi_drain_reads(struct applespi_data *applespi)
 
 	spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
 
-	wait_event_lock_irq(applespi->drain_complete, !applespi->read_active,
-			    applespi->cmd_msg_lock);
+	wait_event_lock_irq_timeout(applespi->drain_complete,
+				    !applespi->read_active,
+				    applespi->cmd_msg_lock,
+				    msecs_to_jiffies(3000));
 
 	applespi->suspended = true;
 
Signature · 1 line
2.21.0