Skip to contentSkip to conversation

Add Apple SPI keyboard and trackpad driver

PATCH Superseded Ronald Tschalär 2 participants 8 messages Feb 21, 2019 – Mar 19, 2019

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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 is just a placeholder for now and is provided in case somebody wants to compile the driver while it's being reviewed here; the real patch has been submitted to dri-devel and is being discussed there, with the intent/hope that I can get an Ack and permission to merge it through the input subsystem tree here as part of this patch series.

Changes in v2:
Applied all feedback from review by Andy Shevchenko, including:
- reworked logging to use dev_xxx() everywhere
- split 16-bit model_id field into 2 8-bit fields
- factored out several pieces of code into separate functions
- many code style improvements and cleanups
- Kconfig dependency fixes
The full set of changes (except for the Kconfig) can be viewed at
https://github.com/roadrunner2/macbook12-spi-driver/ as individual commits a651bb9..f832caa in the upstreaming-review branch.

Ronald Tschalär (2):
drm/bridge: sil_sii8620: depend on INPUT instead of selecting it.
Input: add Apple SPI keyboard and trackpad driver.

 drivers/gpu/drm/bridge/Kconfig    |    2 +-
 drivers/input/keyboard/Kconfig    |   14 +
 drivers/input/keyboard/Makefile   |    1 +
 drivers/input/keyboard/applespi.c | 2003 +++++++++++++++++++++++++++++
 4 files changed, 2019 insertions(+), 1 deletion(-)
 create mode 100644 drivers/input/keyboard/applespi.c
Signature · 1 line
2.20.1
#1 lore raw

PATCH v2 1/2 drm/bridge: sil_sii8620: depend on INPUT instead of selecting it.

commit d6abe6df706c66d803e6dd4fe98c1b6b7f125a56 (drm/bridge:
sil_sii8620: do not have a dependency of RC_CORE) added a dependency on 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 future 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, select should only be used for non-visible symbols. Furthermore almost all other references to INPUT throughout the kernel config are depends, not selects. Hence this change.

---
 drivers/gpu/drm/bridge/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/bridge/Kconfig +1 -1
index 9eeb8ef0b174..bc838e7bb7c8 100644
@@ -83,9 +83,9 @@ config DRM_PARADE_PS8622
 config DRM_SIL_SII8620
 	tristate "Silicon Image SII8620 HDMI/MHL bridge"
 	depends on OF
+	depends on INPUT
 	select DRM_KMS_HELPER
 	imply EXTCON
-	select INPUT
 	select RC_CORE
 	help
 	  Silicon Image SII8620 HDMI/MHL bridge chip driver.
Signature · 1 line
2.20.1
#2 lore raw

PATCH v2 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    |   14 +
 drivers/input/keyboard/Makefile   |    1 +
 drivers/input/keyboard/applespi.c | 2003 +++++++++++++++++++++++++++++
 3 files changed, 2018 insertions(+)
 create mode 100644 drivers/input/keyboard/applespi.c
drivers/input/keyboard/Kconfig +14 -0
index 4713957b0cbb..395e5ce0bc2d 100644
@@ -70,6 +70,20 @@ config KEYBOARD_AMIGA
 config ATARI_KBD_CORE
 	bool
 
+config KEYBOARD_APPLESPI
+	tristate "Apple SPI keyboard and trackpad"
+	depends on ACPI && SPI && EFI
+	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 +2003 -0

2,003 lines not on this page · raw message

Signature · 1 line
2.20.1
#3 lore raw

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

Quoted 25 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.

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    |   14 +
 drivers/input/keyboard/Makefile   |    1 +
 drivers/input/keyboard/applespi.c | 2003 +++++++++++++++++++++++++++++
 3 files changed, 2018 insertions(+)
 create mode 100644 drivers/input/keyboard/applespi.c

[snip]

Quoted 5 lines
drivers/input/keyboard/applespi.c new file +0 -0
new file mode 100644
index 000000000000..2a8d1786011d

[snip]

Quoted 15 lines
drivers/input/keyboard/applespi.c new file +15 -0
+/**
+ * This is a reduced version of print_hex_dump() that uses dev_printk().
+ */
+static void dev_print_hex_dump(const char *level, const struct device *dev,
+			       const char *prefix_str,
+			       int rowsize, int groupsize,
+			       const void *buf, size_t len, bool ascii)
+{
+	const u8 *ptr = buf;
+	int i, linelen, remaining = len;
+	unsigned char linebuf[32 * 3 + 2 + 32 + 1];
+
+	if (rowsize != 16 && rowsize != 32)
+		rowsize = 16;
+

drivers/input/keyboard/applespi.c line 558

Quoted 10 lines
drivers/input/keyboard/applespi.c new file +10 -0
+	for (i = 0; i < len; i += rowsize) {
+		linelen = min(remaining, rowsize);
+		remaining -= rowsize;
+
+		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
+				   linebuf, sizeof(linebuf), ascii);
+
+		dev_printk(level, dev, "%s%s\n", prefix_str, linebuf);
+	}
+}

Apologies, I should've have fixed this before posting v2: I'll introduce an additional patch to add this function to the core to avoid duplication and because I presume this may be useful for others too.

Cheers,

Ronald

#4 lore raw

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

Quoted 15 lines · and then you die
Quoted 12 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.

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
Quoted 3 lines

+/**
+ * This is a reduced version of print_hex_dump() that uses dev_printk().
+ */

...and this should follow kernel doc as stated by comment style.

Quoted 19 lines
Quoted 19 lines
drivers/input/keyboard/applespi.c new file +19 -0
+static void dev_print_hex_dump(const char *level, const struct device *dev,
+			       const char *prefix_str,
+			       int rowsize, int groupsize,
+			       const void *buf, size_t len, bool ascii)
+{
+	const u8 *ptr = buf;
+	int i, linelen, remaining = len;
+	unsigned char linebuf[32 * 3 + 2 + 32 + 1];
+
+	if (rowsize != 16 && rowsize != 32)
+		rowsize = 16;
+
+	for (i = 0; i < len; i += rowsize) {
+		linelen = min(remaining, rowsize);
+		remaining -= rowsize;
+
+		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
+				   linebuf, sizeof(linebuf), ascii);
+
Quoted 8 lines

drivers/input/keyboard/applespi.c line 558

Quoted 3 lines
drivers/input/keyboard/applespi.c new file +3 -0
+		dev_printk(level, dev, "%s%s\n", prefix_str, linebuf);
+	}
+}

Apologies, I should've have fixed this before posting v2: I'll introduce an additional patch to add this function to the core to avoid duplication and because I presume this may be useful for others too.

Yes, makes sense.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

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

In reply to Ronald Tschalär
Quoted 13 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.

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 config KEYBOARD_AMIGA · line 75

Quoted 4 lines
drivers/input/keyboard/Kconfig +3 -0
+config KEYBOARD_APPLESPI
+	tristate "Apple SPI keyboard and trackpad"
 
+	depends on ACPI && SPI && EFI

I would rather want to see separate line for SPI...

drivers/input/keyboard/Kconfig config KEYBOARD_AMIGA · line 76

Quoted 1 line
drivers/input/keyboard/Kconfig +1 -0
+	depends on X86 || COMPILE_TEST

...like here

depends on SPI

drivers/input/keyboard/Kconfig config KEYBOARD_AMIGA · line 85

Quoted 9 lines
drivers/input/keyboard/Kconfig +9 -0
+	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.

/* Perhaps a comment here that this mimics struct drm_rect */

drivers/input/keyboard/applespi.c line 388

Quoted 6 lines
drivers/input/keyboard/applespi.c new file +6 -0
+struct applespi_tp_info {
+	int	x_min;
+	int	y_min;
+	int	x_max;
+	int	y_max;
+};

...see above

drivers/input/keyboard/applespi.c line 663

Quoted 13 lines
drivers/input/keyboard/applespi.c new file +12 -0
+static inline bool applespi_check_write_status(struct applespi_data *applespi,
+					       int sts)
+{
+	static u8 status_ok[] = { 0xac, 0x27, 0x68, 0xd5 };
+
+	if (sts < 0) {
+		dev_warn(DEV(applespi), "Error writing to device: %d\n", sts);
+		return false;
+	}
+
+	if (memcmp(applespi->tx_status, status_ok, APPLESPI_STATUS_SIZE)) {
 
+		dev_warn(DEV(applespi), "Error writing to device: %*ph\n",

Hmm... DEV() is too generic name for custom macro. And frankly I don't think it's good to have in the first place.

Quoted 19 lines
drivers/input/keyboard/applespi.c new file +17 -0
+			 APPLESPI_STATUS_SIZE, applespi->tx_status);
+		return false;
+	}
+
+	return true;
+}
 
+static void applespi_set_bl_level(struct led_classdev *led_cdev,
+				  enum led_brightness value)
+{
+	struct applespi_data *applespi =
+		container_of(led_cdev, struct applespi_data, backlight_info);
+	unsigned long flags;
+	int sts;
+
+	spin_lock_irqsave(&applespi->cmd_msg_lock, flags);
+
 
+	if (value == 0)

drivers/input/keyboard/applespi.c line 957

Quoted 10 lines
drivers/input/keyboard/applespi.c new file +10 -0
+		applespi->want_bl_level = value;
+	else
+		/*
+		 * The backlight does not turn on till level 32, so we scale
+		 * the range here so that from a user's perspective it turns
+		 * on at 1.
+		 */
+		applespi->want_bl_level =
+			((value * KBD_BL_LEVEL_ADJ) / KBD_BL_LEVEL_SCALE +
+			 KBD_BL_LEVEL_MIN);

Fir multi-line conditionals the style is

	if () {
	} else {
	}

drivers/input/keyboard/applespi.c line 1148

Quoted 13 lines
drivers/input/keyboard/applespi.c new file +11 -0
+
+	sts = applespi_send_cmd_msg(applespi);
+
+	spin_unlock_irqrestore(&applespi->cmd_msg_lock, flags);
+}
 
+static void
+applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)
+{
+	unsigned char tmp;
 
+	unsigned long *modifiers =
+			(unsigned long *)&keyboard_protocol->modifiers;

I would leave it on one online despite checkpatch warning (also, instead of (unsigned long *) the (void *) might be used as a small trick).

drivers/input/keyboard/applespi.c line 1159

Quoted 12 lines
drivers/input/keyboard/applespi.c new file +11 -0
+
+	if (!fnremap || fnremap > ARRAY_SIZE(applespi_controlcodes) ||
+	    !applespi_controlcodes[fnremap - 1])
+		return;
+
+	tmp = keyboard_protocol->fn_pressed;
+	keyboard_protocol->fn_pressed = test_bit(fnremap - 1, modifiers);
+	if (tmp)
 
+		__set_bit(fnremap - 1, modifiers);
+	else
+		__clear_bit(fnremap - 1, modifiers);

Oh, this is not good. modifiers should be really unsigned long bounary, otherwise it is potential overflow.

Best to fix is to define them as unsigned long in the first place.

drivers/input/keyboard/applespi.c line 1225

Quoted 12 lines
drivers/input/keyboard/applespi.c new file +9 -0
+}
 
+					   applespi->last_keys_fn_pressed[i]);
+		input_report_key(applespi->keyboard_input_dev, key, 0);
+		applespi->last_keys_fn_pressed[i] = 0;
+	}
 
+	for (i = 0; i < MAX_MODIFIERS; i++) {
 
+		u8 *modifiers = &keyboard_protocol->modifiers;
+
+		if (test_bit(i, (unsigned long *)modifiers))

Oh, this is not good idea, see above.

drivers/input/keyboard/applespi.c line 1281

Quoted 6 lines
drivers/input/keyboard/applespi.c new file +5 -0
+	if (touchpad_dimensions[0]) {
+		int x, y, w, h;
+
 
+		if (sscanf(touchpad_dimensions, "%dx%d+%u+%u", &x, &y, &w, &h)
+				== 4) {

I would leave this on one line. Or use temporary variable

	int ret;

	ret = sscanf();
	if (ret ...) {
Quoted 13 lines
drivers/input/keyboard/applespi.c new file +13 -0
+			dev_info(DEV(applespi),
+				 "Overriding touchpad dimensions from module param\n");
+			applespi->tp_info.x_min = x;
+			applespi->tp_info.y_min = y;
+			applespi->tp_info.x_max = x + w;
+			applespi->tp_info.y_max = y + h;
+		} else {
+			dev_warn(DEV(applespi),
+				 "Invalid touchpad dimensions '%s': must be in the form XxY+W+H\n",
+				 touchpad_dimensions);
+			touchpad_dimensions[0] = '\0';
+		}
+	}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

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

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

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 config KEYBOARD_AMIGA · line 75

Quoted 4 lines
drivers/input/keyboard/Kconfig +3 -0
+config KEYBOARD_APPLESPI
+	tristate "Apple SPI keyboard and trackpad"
 
+	depends on ACPI && SPI && EFI

I would rather want to see separate line for SPI...

Quoted 5 lines

drivers/input/keyboard/Kconfig config KEYBOARD_AMIGA · line 76

Quoted 1 line
drivers/input/keyboard/Kconfig +1 -0
+	depends on X86 || COMPILE_TEST

...like here

depends on SPI

Sure. Generally, what is the criteria/rule here for splitting conjunctions into separate 'depends'?

[snip]

Quoted 1 line

+ #define DEV(applespi) (&(applespi)->spi->dev)

[snip]

Quoted 6 lines

drivers/input/keyboard/applespi.c line 663

Quoted 3 lines
drivers/input/keyboard/applespi.c new file +2 -0
+	if (memcmp(applespi->tx_status, status_ok, APPLESPI_STATUS_SIZE)) {
 
+		dev_warn(DEV(applespi), "Error writing to device: %*ph\n",

Hmm... DEV() is too generic name for custom macro. And frankly I don't think it's good to have in the first place.

Yeah, I've been having trouble coming up with a better (but still succinct) name - CORE_DEV()? RAW_DEV()? DEV_OF()? However, because this expression is used in many places throughout the driver (mostly, but not only, for logging statements) I feel like it's good to factor it out. But I'll defer to your .

[snip]

Quoted 20 lines

drivers/input/keyboard/applespi.c line 1148

Quoted 7 lines
drivers/input/keyboard/applespi.c new file +6 -0
+static void
+applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)
+{
+	unsigned char tmp;
 
+	unsigned long *modifiers =
+			(unsigned long *)&keyboard_protocol->modifiers;

I would leave it on one online despite checkpatch warning (also, instead of (unsigned long *) the (void *) might be used as a small trick).

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

drivers/input/keyboard/applespi.c line 1159

Quoted 3 lines
drivers/input/keyboard/applespi.c new file +3 -0
+		__set_bit(fnremap - 1, modifiers);
+	else
+		__clear_bit(fnremap - 1, modifiers);

Oh, this is not good. modifiers should be really unsigned long bounary, otherwise it is potential overflow.

Best to fix is to define them as unsigned long in the first place.

Can't do that directly, because keyboard_protocol->modifiers is a field in the data received from the device, i.e. defined by that protocol. Instead I could make a copy of the modifiers and pass that around separately (i.e. in addition to the keyboard_protocol struct).

However, the implied size assertions here would basically still apply:

MAX_MODIFIERS == sizeof(keyboard_protocol->modifiers) * 8 ARRAY_SIZE(applespi_controlcodes) == sizeof(keyboard_protocol->modifiers) * 8

(hmm, MAX_MODIFIERS is really redundant - getting rid of it...)

Would using compiletime_assert()'s be an acceptable alternate approach here? It would serve to both document the size constraint and to protect against overflow due to an error in some future edit. E.g.

applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)

 {
        unsigned char tmp;
        unsigned long *modifiers = (void *)&keyboard_protocol->modifiers;

+
+ compiletime_assert(ARRAY_SIZE(applespi_controlcodes) ==
+ sizeof_field(struct keyboard_protocol, modifiers) * 8,
+ "applespi_controlcodes has wrong number of entries");

if (!fnremap || fnremap > ARRAY_SIZE(applespi_controlcodes) ||
!applespi_controlcodes[fnremap - 1])
return;

tmp = keyboard_protocol->fn_pressed; keyboard_protocol->fn_pressed = test_bit(fnremap - 1, modifiers); if (tmp)

__set_bit(fnremap - 1, modifiers);
else
__clear_bit(fnremap - 1, modifiers);
}

Quoted 7 lines
Quoted 6 lines
drivers/input/keyboard/applespi.c new file +5 -0
+}
 
+					   applespi->last_keys_fn_pressed[i]);
+		input_report_key(applespi->keyboard_input_dev, key, 0);
+		applespi->last_keys_fn_pressed[i] = 0;
+	}
Quoted 7 lines

drivers/input/keyboard/applespi.c line 1225

Quoted 5 lines
drivers/input/keyboard/applespi.c new file +4 -0
+	for (i = 0; i < MAX_MODIFIERS; i++) {
 
+		u8 *modifiers = &keyboard_protocol->modifiers;
+
+		if (test_bit(i, (unsigned long *)modifiers))

Oh, this is not good idea, see above.

See above. (I presume duplicating the compiletime_assert() here isn't necessary, if going that route?)

Cheers,

Ronald

#7 lore raw

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

Quoted 10 lines · and then you die
Quoted 9 lines · Andy Shevchenko

drivers/input/keyboard/Kconfig config KEYBOARD_AMIGA · line 75

Quoted 4 lines · Ronald Tschalär
drivers/input/keyboard/Kconfig +3 -0
+config KEYBOARD_APPLESPI
+	tristate "Apple SPI keyboard and trackpad"
 
+	depends on ACPI && SPI && EFI

I would rather want to see separate line for SPI...

Quoted 8 lines
Quoted 5 lines

drivers/input/keyboard/Kconfig config KEYBOARD_AMIGA · line 76

Quoted 1 line
drivers/input/keyboard/Kconfig +1 -0
+	depends on X86 || COMPILE_TEST

...like here

depends on SPI

Sure. Generally, what is the criteria/rule here for splitting conjunctions into separate 'depends'?

Rule of common sense.

For example UEFI and ACPI may have some relations, SPI and ACPI kinda orthogonal.

Quoted 9 lines
Quoted 8 lines

+ #define DEV(applespi) (&(applespi)->spi->dev)

drivers/input/keyboard/applespi.c line 663

Quoted 3 lines
drivers/input/keyboard/applespi.c new file +2 -0
+	if (memcmp(applespi->tx_status, status_ok, APPLESPI_STATUS_SIZE)) {
 
+		dev_warn(DEV(applespi), "Error writing to device: %*ph\n",

Hmm... DEV() is too generic name for custom macro. And frankly I don't think it's good to have in the first place.

Quoted 5 lines

Yeah, I've been having trouble coming up with a better (but still succinct) name - CORE_DEV()? RAW_DEV()? DEV_OF()? However, because this expression is used in many places throughout the driver (mostly, but not only, for logging statements) I feel like it's good to factor it out. But I'll defer to your .

Please remove this macro for good. Otherwise big subsystems / drivers usually do something like

#define foo_err(...) dev_err(...)
...

Don't know if it would help here, the driver is standalone and not so big.

Quoted 56 lines
Quoted 20 lines

drivers/input/keyboard/applespi.c line 1148

Quoted 7 lines
drivers/input/keyboard/applespi.c new file +6 -0
+static void
+applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)
+{
+	unsigned char tmp;
 
+	unsigned long *modifiers =
+			(unsigned long *)&keyboard_protocol->modifiers;

I would leave it on one online despite checkpatch warning (also, instead of (unsigned long *) the (void *) might be used as a small trick).

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

drivers/input/keyboard/applespi.c line 1159

Quoted 3 lines
drivers/input/keyboard/applespi.c new file +3 -0
+		__set_bit(fnremap - 1, modifiers);
+	else
+		__clear_bit(fnremap - 1, modifiers);

Oh, this is not good. modifiers should be really unsigned long bounary, otherwise it is potential overflow.

Best to fix is to define them as unsigned long in the first place.

Can't do that directly, because keyboard_protocol->modifiers is a field in the data received from the device, i.e. defined by that protocol. Instead I could make a copy of the modifiers and pass that around separately (i.e. in addition to the keyboard_protocol struct).

However, the implied size assertions here would basically still apply:

MAX_MODIFIERS == sizeof(keyboard_protocol->modifiers) * 8 ARRAY_SIZE(applespi_controlcodes) == sizeof(keyboard_protocol->modifiers) * 8

(hmm, MAX_MODIFIERS is really redundant - getting rid of it...)

Would using compiletime_assert()'s be an acceptable alternate approach here? It would serve to both document the size constraint and to protect against overflow due to an error in some future edit. E.g.

applespi_remap_fn_key(struct keyboard_protocol *keyboard_protocol)

 {
        unsigned char tmp;
        unsigned long *modifiers = (void *)&keyboard_protocol->modifiers;
(quoted diff) +4 -0
+
+       compiletime_assert(ARRAY_SIZE(applespi_controlcodes) ==
+                          sizeof_field(struct keyboard_protocol, modifiers) * 8,
+                          "applespi_controlcodes has wrong number of entries");
 
        if (!fnremap || fnremap > ARRAY_SIZE(applespi_controlcodes) ||
            !applespi_controlcodes[fnremap - 1])

Review

Quoted 10 lines
(quoted diff) +0 -0
 		return;
 
 	tmp = keyboard_protocol->fn_pressed;
 	keyboard_protocol->fn_pressed = test_bit(fnremap - 1, modifiers);
 	if (tmp)
 
 		__set_bit(fnremap - 1, modifiers);
 	else
 		__clear_bit(fnremap - 1, modifiers);
 }

Perhaps, simple

	__set_bit(b, x)		->	x |= BIT(b);
	__clear_bit(b, x)	->	x &= ~BIT(b);

?

Quoted 1 line
Quoted 1 line
Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko