Skip to contentSkip to conversation

Add Apple SPI keyboard and trackpad driver

PATCH Superseded Ronald Tschalär 3 participants 8 messages Apr 7, 2019 – Apr 15, 2019

Subsystems (5)

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 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 and is provided for anybody wanting to compile the driver while it's being reviewed here. The patch is the latest version that has been submitted for review at dri-devel; the intent is to get an Ack and permission to merge it through the input subsystem tree here as part of this patch series.

The second patch contains the new applespi driver.

Changes in v4:
Applied all feedback from review by Andy Shevchenko and Greg Kroah-Hartman, including:
- minor include path fix
- moved debug logging to debugfs and tracepoints
The full set of changes to applespi can be viewed at
https://github.com/roadrunner2/macbook12-spi-driver/ as individual commits 3a6262e..daf60f8 in the upstreaming-review branch.

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    |   21 +
 drivers/input/keyboard/Kconfig          |   15 +
 drivers/input/keyboard/Makefile         |    1 +
 drivers/input/keyboard/applespi.c       | 1999 +++++++++++++++++++++++
 drivers/input/keyboard/applespi.h       |   29 +
 drivers/input/keyboard/applespi_trace.h |   94 ++
 7 files changed, 2160 insertions(+), 2 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 v4 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 | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 2 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 +21 -0
index a6e8f4591e63..f8dfbf01caa8 100644
@@ -66,7 +66,9 @@ enum sii8620_mt_state {
 struct sii8620 {
 	struct drm_bridge bridge;
 	struct device *dev;
+#if IS_ENABLED(CONFIG_RC_CORE)
 	struct rc_dev *rc_dev;
+#endif
 	struct clk *clk_xtal;
 	struct gpio_desc *gpio_reset;
 	struct gpio_desc *gpio_int;
@@ -1757,6 +1759,7 @@ static void sii8620_send_features(struct sii8620 *ctx)
 	sii8620_write_buf(ctx, REG_MDT_XMIT_WRITE_PORT, buf, ARRAY_SIZE(buf));
 }
 
+#if IS_ENABLED(CONFIG_RC_CORE)
 static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
 {
 	bool pressed = !(scancode & MHL_RCP_KEY_RELEASED_MASK);
@@ -1775,6 +1778,12 @@ static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
 
 	return true;
 }
+#else
+static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
+{
+	return false;
+}
+#endif
 
 static void sii8620_msc_mr_set_int(struct sii8620 *ctx)
 {
@@ -2098,6 +2107,7 @@ static void sii8620_cable_in(struct sii8620 *ctx)
 	enable_irq(to_i2c_client(ctx->dev)->irq);
 }
 
+#if IS_ENABLED(CONFIG_RC_CORE)
 static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
 {
 	struct rc_dev *rc_dev;
@@ -2127,6 +2137,11 @@ static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
 	}
 	ctx->rc_dev = rc_dev;
 }
+#else
+static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
+{
+}
+#endif
 
 static void sii8620_cable_out(struct sii8620 *ctx)
 {
@@ -2213,12 +2228,18 @@ static int sii8620_attach(struct drm_bridge *bridge)
 	return sii8620_clear_error(ctx);
 }
 
+#if IS_ENABLED(CONFIG_RC_CORE)
 static void sii8620_detach(struct drm_bridge *bridge)
 {
 	struct sii8620 *ctx = bridge_to_sii8620(bridge);
 
 	rc_unregister_device(ctx->rc_dev);
 }
+#else
+static void sii8620_detach(struct drm_bridge *bridge)
+{
+}
+#endif
 
 static int sii8620_is_packing_required(struct sii8620 *ctx,
 				       const struct drm_display_mode *mode)
Signature · 1 line
2.20.1
Reviewed-by on v4 1/2
#2 lore raw

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

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 5 lines
CC Inki Dae <[email protected]>
CC Andrzej Hajda <[email protected]>
CC Laurent Pinchart <[email protected]>
CC Dmitry Torokhov <[email protected]>

 --
Regards
Andrzej

#3 lore raw

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

In reply to Ronald Tschalär
Quoted 118 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]>
---
 drivers/gpu/drm/bridge/Kconfig       |  3 +--
 drivers/gpu/drm/bridge/sil-sii8620.c | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 2 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 +16 -0
index a6e8f4591e63..f8dfbf01caa8 100644
@@ -66,7 +66,9 @@ enum sii8620_mt_state {
 struct sii8620 {
 	struct drm_bridge bridge;
 	struct device *dev;
+#if IS_ENABLED(CONFIG_RC_CORE)
 	struct rc_dev *rc_dev;
+#endif
 	struct clk *clk_xtal;
 	struct gpio_desc *gpio_reset;
 	struct gpio_desc *gpio_int;
@@ -1757,6 +1759,7 @@ static void sii8620_send_features(struct sii8620 *ctx)
 	sii8620_write_buf(ctx, REG_MDT_XMIT_WRITE_PORT, buf, ARRAY_SIZE(buf));
 }
 
+#if IS_ENABLED(CONFIG_RC_CORE)
 static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
 {
 	bool pressed = !(scancode & MHL_RCP_KEY_RELEASED_MASK);
@@ -1775,6 +1778,12 @@ static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
 
 	return true;
 }
+#else
+static bool sii8620_rcp_consume(struct sii8620 *ctx, u8 scancode)
+{
+	return false;
+}
+#endif
 
 static void sii8620_msc_mr_set_int(struct sii8620 *ctx)
 {
@@ -2098,6 +2107,7 @@ static void sii8620_cable_in(struct sii8620 *ctx)
 	enable_irq(to_i2c_client(ctx->dev)->irq);
 }
 
+#if IS_ENABLED(CONFIG_RC_CORE)
 static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
 {
 	struct rc_dev *rc_dev;
@@ -2127,6 +2137,11 @@ static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
 	}
 	ctx->rc_dev = rc_dev;
 }
+#else
+static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
+{
+}
+#endif
 
 static void sii8620_cable_out(struct sii8620 *ctx)
 {
@@ -2213,12 +2228,18 @@ static int sii8620_attach(struct drm_bridge *bridge)
 	return sii8620_clear_error(ctx);
 }
 
+#if IS_ENABLED(CONFIG_RC_CORE)
 static void sii8620_detach(struct drm_bridge *bridge)
 {
 	struct sii8620 *ctx = bridge_to_sii8620(bridge);
 

drivers/gpu/drm/bridge/sil-sii8620.c static int sii8620_attach(struct drm_bridge *bridge) · line 2245

Quoted 10 lines
drivers/gpu/drm/bridge/sil-sii8620.c +5 -0
 	rc_unregister_device(ctx->rc_dev);
 }
+#else
+static void sii8620_detach(struct drm_bridge *bridge)
+{
+}
+#endif
 
 static int sii8620_is_packing_required(struct sii8620 *ctx,
 				       const struct drm_display_mode *mode)

What about:

---------

drivers/gpu/drm/bridge/Kconfig +0 -0
index 8840f396a7b6..298189067929 100644
@@ -86,8 +86,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.

(quoted diff) +0 -0

b/drivers/gpu/drm/bridge/sil-sii8620.c
index 0cc293a6ac24..df0f9dbbe839 100644

drivers/gpu/drm/bridge/sil-sii8620.c +0 -0
@@ -1762,10 +1762,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(RC_CORE) || !ctx->rc_dev)
                return false;
-       }

        if (pressed)
                rc_keydown(ctx->rc_dev, RC_PROTO_CEC, scancode, 0); @@ -2102,6 +2100,9 @@ static void sii8620_init_rcp_input_dev(struct sii8620 *ctx)
        struct rc_dev *rc_dev;
        int ret;

+       if (!IS_ENABLED(RC_CORE))
+               return;
+
        rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE);
        if (!rc_dev) {
                dev_err(ctx->dev, "Failed to allocate RC device\n"); @@ -2216,6 +2217,9 @@ static void sii8620_detach(struct drm_bridge *bridge) {
        struct sii8620 *ctx = bridge_to_sii8620(bridge);

+       if (!IS_ENABLED(RC_CORE))
+               return;
+
        rc_unregister_device(ctx->rc_dev);
 }
-----------

Less changes, no conditional compilation - better compiler coverage, more readable.

Regards

Andrzej

#4 lore raw

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

Hi Andrzej,

Quoted 30 lines · Andrzej Hajda
Quoted 29 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).

[snip]

Quoted 58 lines

What about:

---------

drivers/gpu/drm/bridge/Kconfig +0 -0
index 8840f396a7b6..298189067929 100644
@@ -86,8 +86,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 +0 -0
index 0cc293a6ac24..df0f9dbbe839 100644
@@ -1762,10 +1762,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(RC_CORE) || !ctx->rc_dev)
                return false;
-       }

        if (pressed)
                rc_keydown(ctx->rc_dev, RC_PROTO_CEC, scancode, 0);

drivers/gpu/drm/bridge/sil-sii8620.c +0 -0
@@ -2102,6 +2100,9 @@ static void sii8620_init_rcp_input_dev(struct
 sii8620 *ctx)
         struct rc_dev *rc_dev;
         int ret;


+       if (!IS_ENABLED(RC_CORE))
+               return;
+
        rc_dev = rc_allocate_device(RC_DRIVER_SCANCODE); if (!rc_dev) {
                dev_err(ctx->dev, "Failed to allocate RC device\n");

drivers/gpu/drm/bridge/sil-sii8620.c +0 -0
@@ -2216,6 +2217,9 @@ static void sii8620_detach(struct drm_bridge *bridge)
  {
         struct sii8620 *ctx = bridge_to_sii8620(bridge);


+       if (!IS_ENABLED(RC_CORE))
+               return;
+
        rc_unregister_device(ctx->rc_dev);
 }
-----------

Quoted 2 lines

Less changes, no conditional compilation - better compiler coverage, more readable.

This works for me (with RC_CORE replaced with CONFIG_RC_CORE). Sending a new patch that includes this shortly.

Cheers,

Ronald

#5 lore raw

PATCH v4 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       | 1999 +++++++++++++++++++++++
 drivers/input/keyboard/applespi.h       |   29 +
 drivers/input/keyboard/applespi_trace.h |   94 ++
 5 files changed, 2138 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 +1999 -0

1,999 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
#6 lore raw

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

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.

Thank you for an update, my comments below.

drivers/input/keyboard/applespi.c line 954

Quoted 8 lines
drivers/input/keyboard/applespi.c new file +8 -0
+static void applespi_debug_update_dimensions(struct applespi_data *applespi,
+					     const struct tp_finger *f)
+{
+	#define UPDATE_DIMENSIONS(val, op, last) \
+		do { \
+			if (le16_to_int(val) op last) \
+				last = le16_to_int(val); \
+		} while (0)

I do not see how this is better than min()/max()/min_t()/max_t().

drivers/input/keyboard/applespi.c

Quoted 13 lines
drivers/input/keyboard/applespi.c new file +12 -0
+
+	UPDATE_DIMENSIONS(f->abs_x, <, applespi->tp_dim_min_x);
+	UPDATE_DIMENSIONS(f->abs_x, >, applespi->tp_dim_max_x);
+	UPDATE_DIMENSIONS(f->abs_y, <, applespi->tp_dim_min_y);
+	UPDATE_DIMENSIONS(f->abs_y, >, applespi->tp_dim_max_y);
+
+	#undef UPDATE_DIMENSIONS
+}
 
+static void
+applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)
+{
+	unsigned char tmp;

What about

	u8 bit = BIT(fnremap - 1);

drivers/input/keyboard/applespi.c line 1122

Quoted 8 lines
drivers/input/keyboard/applespi.c new file +7 -0
+	if (!fnremap || fnremap > ARRAY_SIZE(applespi_controlcodes) ||
+	    !applespi_controlcodes[fnremap - 1])
 
+		return;
+
+	tmp = keyboard_protocol->fn_pressed;
+	keyboard_protocol->fn_pressed =
+			!!(keyboard_protocol->modifiers & BIT(fnremap - 1));
	... & bit);

and so on?

Quoted 4 lines
drivers/input/keyboard/applespi.c new file +4 -0
+	if (tmp)
+		keyboard_protocol->modifiers |= BIT(fnremap - 1);
+	else
+		keyboard_protocol->modifiers &= ~BIT(fnremap - 1);

drivers/input/keyboard/applespi.c line 1136

Quoted 10 lines
drivers/input/keyboard/applespi.c new file +9 -0
+}
 
+static void
+applespi_handle_keyboard_event(struct applespi_data *applespi,
+			       struct keyboard_protocol *keyboard_protocol)
+{
+	int i, j;
+	unsigned int key;
+	bool still_pressed;
+	bool is_overflow;

Perhaps reversed xmas tree order?

Quoted 8 lines
drivers/input/keyboard/applespi.c new file +8 -0
+
+	compiletime_assert(ARRAY_SIZE(applespi_controlcodes) ==
+			   sizeof_field(struct keyboard_protocol, modifiers) * 8,
+			   "applespi_controlcodes has wrong number of entries");
+
+	/* check for rollover overflow, which is signalled by all keys == 1 */
+	is_overflow = true;
+

drivers/input/keyboard/applespi.c line 1153

Quoted 10 lines
drivers/input/keyboard/applespi.c new file +9 -0
+	for (i = 0; i < MAX_ROLLOVER; i++) {
+		if (keyboard_protocol->keys_pressed[i] != 1) {
+			is_overflow = false;
+			break;
+		}
+	}
+
 
+	if (is_overflow)
+		return;

Isn't it simple

	if (i == MAX_ROLLOVER)

w/o need of additional variable?

Quoted 6 lines
drivers/input/keyboard/applespi.c new file +6 -0
+	/* remap fn key if desired */
+	applespi_remap_fn_key(keyboard_protocol);
+
+	/* check released keys */
+	for (i = 0; i < MAX_ROLLOVER; i++) {
+		still_pressed = false;

drivers/input/keyboard/applespi.c line 1170

Quoted 10 lines
drivers/input/keyboard/applespi.c new file +10 -0
+		for (j = 0; j < MAX_ROLLOVER; j++) {
+			if (applespi->last_keys_pressed[i] ==
+			    keyboard_protocol->keys_pressed[j]) {
+				still_pressed = true;
+				break;
+			}
+		}
+
+		if (still_pressed)
+			continue;

Similar idea here.

Quoted 47 lines
drivers/input/keyboard/applespi.c new file +45 -0
+
+		key = applespi_code_to_key(applespi->last_keys_pressed[i],
+					   applespi->last_keys_fn_pressed[i]);
+		input_report_key(applespi->keyboard_input_dev, key, 0);
+		applespi->last_keys_fn_pressed[i] = 0;
+	}
 
+	/* check pressed keys */
+	for (i = 0; i < MAX_ROLLOVER; i++) {
+		if (keyboard_protocol->keys_pressed[i] <
+				ARRAY_SIZE(applespi_scancodes) &&
+		    keyboard_protocol->keys_pressed[i] > 0) {
+			key = applespi_code_to_key(
+					keyboard_protocol->keys_pressed[i],
+					keyboard_protocol->fn_pressed);
+			input_report_key(applespi->keyboard_input_dev, key, 1);
+			applespi->last_keys_fn_pressed[i] =
+					keyboard_protocol->fn_pressed;
+		}
+	}
+
+	/* check control keys */
+	for (i = 0; i < ARRAY_SIZE(applespi_controlcodes); i++) {
+		if (keyboard_protocol->modifiers & BIT(i))
+			input_report_key(applespi->keyboard_input_dev,
+					 applespi_controlcodes[i], 1);
+		else
+			input_report_key(applespi->keyboard_input_dev,
+					 applespi_controlcodes[i], 0);
+	}
+
+	/* check function key */
+	if (keyboard_protocol->fn_pressed && !applespi->last_fn_pressed)
+		input_report_key(applespi->keyboard_input_dev, KEY_FN, 1);
+	else if (!keyboard_protocol->fn_pressed && applespi->last_fn_pressed)
+		input_report_key(applespi->keyboard_input_dev, KEY_FN, 0);
+	applespi->last_fn_pressed = keyboard_protocol->fn_pressed;
+
+	/* done */
+	input_sync(applespi->keyboard_input_dev);
+	memcpy(&applespi->last_keys_pressed, keyboard_protocol->keys_pressed,
+	       sizeof(applespi->last_keys_pressed));
+}
 
+static int
+applespi_register_touchpad_device(struct applespi_data *applespi,
+				  struct touchpad_info_protocol *rcvd_tp_info)

drivers/input/keyboard/applespi.c line 1238

Quoted 10 lines
drivers/input/keyboard/applespi.c new file +9 -0
+{
+	const struct applespi_tp_info *tp_info;
+	struct input_dev *touchpad_input_dev;
+	int sts;
+
+	/* set up touchpad dimensions */
+	tp_info = applespi_find_touchpad_info(rcvd_tp_info->model_no);
+	if (!tp_info) {
 
+		dev_warn(&(applespi)->spi->dev,

I'm not sure what parens are helping with (here and everywhere with device parameter)?

Quoted 9 lines
drivers/input/keyboard/applespi.c new file +7 -0
+			 "Unknown touchpad model %x - falling back to MB8 touchpad\n",
+			 rcvd_tp_info->model_no);
+		tp_info = &applespi_tp_models[0].tp_info;
+	}
 
+	return 0;
+}
 
+static int applespi_probe(struct spi_device *spi)

drivers/input/keyboard/applespi.c line 1656

Quoted 10 lines
drivers/input/keyboard/applespi.c new file +9 -0
+{
+	struct applespi_data *applespi;
+	acpi_status acpi_sts;
+	int sts, i;
+	unsigned long long gpe, usb_status;
+
+	/* check if the USB interface is present and enabled already */
 
+	acpi_sts = acpi_evaluate_integer(ACPI_HANDLE(&spi->dev), "UIST", NULL,
+					 &usb_status);

... see below ...

Quoted 4 lines
drivers/input/keyboard/applespi.c new file +4 -0
+	if (ACPI_SUCCESS(acpi_sts) && usb_status) {
+		/* let the USB driver take over instead */
+		dev_info(&spi->dev, "USB interface already enabled\n");
+		return -ENODEV;

drivers/input/keyboard/applespi.c line 1669

Quoted 10 lines
drivers/input/keyboard/applespi.c new file +9 -0
+	}
+
+	/* allocate driver data */
+	applespi = devm_kzalloc(&spi->dev, sizeof(*applespi), GFP_KERNEL);
+	if (!applespi)
+		return -ENOMEM;
+
+	applespi->spi = spi;
 
+	applespi->handle = ACPI_HANDLE(&spi->dev);

AFAICS this is local variable, why put it in the data structure? And above you are not using this, btw.

drivers/input/keyboard/applespi.c line 1831

Quoted 11 lines
drivers/input/keyboard/applespi.c new file +10 -0
+	} else {
+		struct dentry *ret;
+
+		ret = debugfs_create_bool("enable_tp_dim", 0600,
+					  applespi->debugfs_root,
+					  &applespi->debug_tp_dim);
+		if (IS_ERR(ret))
 
+			dev_warn(&(applespi)->spi->dev,
+				 "Error creating debugfs entry enable_tp_dim (%ld)\n",
+				 PTR_ERR(ret));

Can ret be NULL?

dev_dbg() looks more appropriate.

drivers/input/keyboard/applespi.c line 1839

Quoted 9 lines
drivers/input/keyboard/applespi.c new file +8 -0
+
+		ret = debugfs_create_file("tp_dim", 0400,
+					  applespi->debugfs_root, applespi,
+					  &applespi_tp_dim_fops);
+		if (IS_ERR(ret))
 
+			dev_warn(&(applespi)->spi->dev,
+				 "Error creating debugfs entry tp_dim (%ld)\n",
+				 PTR_ERR(ret));

Ditto.

Quoted 4 lines
drivers/input/keyboard/applespi.c new file +4 -0
+	}
+
+	return 0;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

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

Quoted 8 lines · Andy Shevchenko
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 7 lines
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.

Thank you for an update, my comments below.

Thank you again for your review.

[snip]

Quoted 4 lines
Quoted 4 lines
drivers/input/keyboard/applespi.c new file +4 -0
+	} else {
+		struct dentry *ret;
+
+		ret = debugfs_create_bool("enable_tp_dim", 0600,
Quoted 8 lines

drivers/input/keyboard/applespi.c line 1831

Quoted 6 lines
drivers/input/keyboard/applespi.c new file +6 -0
+					  applespi->debugfs_root,
+					  &applespi->debug_tp_dim);
+		if (IS_ERR(ret))
+			dev_warn(&(applespi)->spi->dev,
+				 "Error creating debugfs entry enable_tp_dim (%ld)\n",
+				 PTR_ERR(ret));

Can ret be NULL?

No, it actually can't (I manually traced all code paths to be sure):
the documentation for these helper functions is wrong in this respect. However, I note that a lot of existing kernel code also has this wrong (i.e. it's checking for NULL). Digging a bit further and looking at the history, it appears this was changed just recently (commit ff9fb72b "debugfs: return error values, not NULL"), which would explain the existing code and documentation. I'll submit a patch to update the docs.

Quoted 1 line

dev_dbg() looks more appropriate.

Hmm, ok, I guess I find this a bit odd, though: true, this only affects code used for debugging, but it's nevertheless an error that shouldn't normally occur.

Cheers,

Ronald