Skip to contentSkip to conversation

Input: surface3_spi: Surface Pen Support

PATCH Superseded Stephen Just 2 participants 9 messages May 23, 2016 – May 25, 2016

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Input: surface3_spi: Surface Pen Support

This series adds support for Surface Pen devices to the Surface 3 touchscreen input driver.

This series works with both styles of pen:
the old style with pressure sensitive tip, an eraser button, and a secondary input button, as well as the new style pen which does not have a dedicated eraser button.

The top button on the pen is not supported by this patch. Those buttons are a separate Bluetooth device.

Stephen Just (2):
Input - surface3_spi: Prepare to add support for Surface Pen Input - surface3_spi: add surface pen support for Surface 3

 drivers/input/touchscreen/surface3_spi.c | 140 ++++++++++++++++++++++++++++---
 1 file changed, 127 insertions(+), 13 deletions(-)
Signature · 1 line
2.8.2
#1 lore raw

PATCH 1/2 Input - surface3_spi: Prepare to add support for Surface Pen

Surface Pens can be supported by handling a second report type. Prepare for this change.

---
 drivers/input/touchscreen/surface3_spi.c | 38 +++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 13 deletions(-)
drivers/input/touchscreen/surface3_spi.c +25 -13
index 58631d1..204a162 100644
@@ -27,6 +27,8 @@
 
 #define SURFACE3_PACKET_SIZE	264
 
+#define SURFACE3_REPORT_TOUCH	0xd2
+
 struct surface3_ts_data {
 	struct spi_device *spi;
 	struct gpio_desc *gpiod_rst[2];
@@ -84,19 +86,10 @@ static void surface3_spi_report_touch(struct surface3_ts_data *ts_data,
 	}
 }
 
-static void surface3_spi_process(struct surface3_ts_data *ts_data)
+static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *data)
 {
-	const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e,
-			       0x01, 0xd2, 0x00, 0x80, 0x01, 0x03, 0x03};
-	u8 *data = ts_data->rd_buf;
 	u16 timestamp;
 	unsigned int i;
-
-	if (memcmp(header, data, sizeof(header)))
-		dev_err(&ts_data->spi->dev,
-			"%s header error: %*ph, ignoring...\n",
-			__func__, (int)sizeof(header), data);
-
 	timestamp = get_unaligned_le16(&data[15]);
 
 	for (i = 0; i < 13; i++) {
@@ -121,6 +114,25 @@ static void surface3_spi_process(struct surface3_ts_data *ts_data)
 	input_sync(ts_data->input_dev);
 }
 
+static void surface3_spi_process(struct surface3_ts_data *ts_data)
+{
+	const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01};
+
+	u8 *data = ts_data->rd_buf;
+
+	if (memcmp(header, data, sizeof(header)))
+		dev_err(&ts_data->spi->dev,
+			"%s header error: %*ph, ignoring...\n",
+			__func__, (int)sizeof(header), data);
+
+	if (data[9] == SURFACE3_REPORT_TOUCH)
+		surface3_spi_process_touch(ts_data, data);
+	else
+		dev_err(&ts_data->spi->dev,
+			"%s unknown packet type: %x, ignoring...\n",
+			__func__, data[9]);
+}
+
 static irqreturn_t surface3_spi_irq_handler(int irq, void *dev_id)
 {
 	struct surface3_ts_data *data = dev_id;
@@ -176,7 +188,7 @@ static int surface3_spi_get_gpio_config(struct surface3_ts_data *data)
 	return 0;
 }
 
-static int surface3_spi_create_input(struct surface3_ts_data *data)
+static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
 {
 	struct input_dev *input;
 	int error;
@@ -199,7 +211,7 @@ static int surface3_spi_create_input(struct surface3_ts_data *data)
 	input->phys = "input/ts";
 	input->id.bustype = BUS_SPI;
 	input->id.vendor = 0x045e;	/* Microsoft */
-	input->id.product = 0x0000;
+	input->id.product = 0x0001;
 	input->id.version = 0x0000;
 
 	error = input_register_device(input);
@@ -239,7 +251,7 @@ static int surface3_spi_probe(struct spi_device *spi)
 	surface3_spi_power(data, false);
 	surface3_spi_power(data, true);
 
-	error = surface3_spi_create_input(data);
+	error = surface3_spi_create_touch_input(data);
 	if (error)
 		return error;
 
Signature · 1 line
2.8.2
#2 lore raw

PATCH 2/2 Input - surface3_spi: add surface pen support for Surface 3

In reply to Stephen Just

This change creates a second input device which will handle input from a Surface Pen. The Surface Pen supplies a different packet header than touch events, so it is simple to handle one or the other.

This patch handles both the newer Surface Pen with one button, and the older variant with a second button to switch to Eraser mode.

---
 drivers/input/touchscreen/surface3_spi.c | 102 +++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)
drivers/input/touchscreen/surface3_spi.c +102 -0
index 204a162..52a2cde 100644
@@ -28,11 +28,13 @@
 #define SURFACE3_PACKET_SIZE	264
 
 #define SURFACE3_REPORT_TOUCH	0xd2
+#define SURFACE3_REPORT_PEN	0x16
 
 struct surface3_ts_data {
 	struct spi_device *spi;
 	struct gpio_desc *gpiod_rst[2];
 	struct input_dev *input_dev;
+	struct input_dev *pen_input_dev;
 
 	u8 rd_buf[SURFACE3_PACKET_SIZE]		____cacheline_aligned;
 };
@@ -49,6 +51,14 @@ struct surface3_ts_data_finger {
 	u32 padding;
 } __packed;
 
+struct surface3_ts_data_pen {
+	u8 status;
+	__le16 x;
+	__le16 y;
+	__le16 pressure;
+	u8 padding;
+} __packed;
+
 static int surface3_spi_read(struct surface3_ts_data *ts_data)
 {
 	struct spi_device *spi = ts_data->spi;
@@ -114,6 +124,52 @@ static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *dat
 	input_sync(ts_data->input_dev);
 }
 
+static void surface3_spi_report_pen(struct surface3_ts_data *ts_data,
+				    struct surface3_ts_data_pen *pen)
+{
+	int st = pen->status;
+	int prox = st & 0x01;
+	int rubber = st & 0x18;
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOUCH,
+			 (st & 0x12));
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOOL_PEN,
+			 prox && !rubber);
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOOL_RUBBER,
+			 prox && rubber);
+
+	if (st) {
+		input_report_key(ts_data->pen_input_dev,
+				 BTN_STYLUS,
+				 st & 0x04);
+
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_X,
+				 get_unaligned_le16(&pen->x));
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_Y,
+				 get_unaligned_le16(&pen->y));
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_PRESSURE,
+				 get_unaligned_le16(&pen->pressure));
+	}
+}
+
+static void surface3_spi_process_pen(struct surface3_ts_data *ts_data, u8 *data)
+{
+	struct surface3_ts_data_pen *pen;
+
+	pen = (struct surface3_ts_data_pen *)&data[15];
+
+	surface3_spi_report_pen(ts_data, pen);
+	input_sync(ts_data->pen_input_dev);
+}
+
 static void surface3_spi_process(struct surface3_ts_data *ts_data)
 {
 	const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01};
@@ -127,6 +183,8 @@ static void surface3_spi_process(struct surface3_ts_data *ts_data)
 
 	if (data[9] == SURFACE3_REPORT_TOUCH)
 		surface3_spi_process_touch(ts_data, data);
+	else if (data[9] == SURFACE3_REPORT_PEN)
+		surface3_spi_process_pen(ts_data, data);
 	else
 		dev_err(&ts_data->spi->dev,
 			"%s unknown packet type: %x, ignoring...\n",
@@ -224,6 +282,46 @@ static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
 	return 0;
 }
 
+static int surface3_spi_create_pen_input(struct surface3_ts_data *data)
+{
+	struct input_dev *input;
+	int error;
+
+	input = devm_input_allocate_device(&data->spi->dev);
+	if (!input)
+		return -ENOMEM;
+
+	data->pen_input_dev = input;
+
+	__set_bit(INPUT_PROP_DIRECT, input->propbit);
+	__set_bit(INPUT_PROP_POINTER, input->propbit);
+	input_set_abs_params(input, ABS_X, 0, 9600, 0, 0);
+	input_abs_set_res(input, ABS_X, 40);
+	input_set_abs_params(input, ABS_Y, 0, 7200, 0, 0);
+	input_abs_set_res(input, ABS_Y, 48);
+	input_set_abs_params(input, ABS_PRESSURE, 0, 1024, 0, 0);
+	input_set_capability(input, EV_KEY, BTN_TOUCH);
+	input_set_capability(input, EV_KEY, BTN_STYLUS);
+	input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
+	input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
+
+	input->name = "Surface3 SPI Pen Input";
+	input->phys = "input/ts";
+	input->id.bustype = BUS_SPI;
+	input->id.vendor = 0x045e;     /* Microsoft */
+	input->id.product = 0x0002;
+	input->id.version = 0x0000;
+
+	error = input_register_device(input);
+	if (error) {
+		dev_err(&data->spi->dev,
+			"Failed to register input device: %d", error);
+		return error;
+	}
+
+	return 0;
+}
+
 static int surface3_spi_probe(struct spi_device *spi)
 {
 	struct surface3_ts_data *data;
@@ -255,6 +353,10 @@ static int surface3_spi_probe(struct spi_device *spi)
 	if (error)
 		return error;
 
+	error = surface3_spi_create_pen_input(data);
+	if (error)
+		return error;
+
 	error = devm_request_threaded_irq(&spi->dev, spi->irq,
 					  NULL, surface3_spi_irq_handler,
 					  IRQF_ONESHOT,
Signature · 1 line
2.8.2
Reviewed-by on v1 2/2
#3 lore raw

Re: [PATCH 2/2] Input - surface3_spi: add surface pen support for Surface 3

Hi Stephen,

thanks for the submission.

Quoted 6 lines · Stephen Just

This change creates a second input device which will handle input from a Surface Pen. The Surface Pen supplies a different packet header than touch events, so it is simple to handle one or the other.

This patch handles both the newer Surface Pen with one button, and the older variant with a second button to switch to Eraser mode.

Oh... Would you mind sharing an evemu-record of such older variant with the Eraser mode? We might need to deal with that differently in libinput...

Quoted 93 lines
---
 drivers/input/touchscreen/surface3_spi.c | 102 +++++++++++++++++++++++++++++++
 1 file changed, 102 insertions(+)
drivers/input/touchscreen/surface3_spi.c +56 -0
index 204a162..52a2cde 100644
@@ -28,11 +28,13 @@
 #define SURFACE3_PACKET_SIZE	264
 
 #define SURFACE3_REPORT_TOUCH	0xd2
+#define SURFACE3_REPORT_PEN	0x16
 
 struct surface3_ts_data {
 	struct spi_device *spi;
 	struct gpio_desc *gpiod_rst[2];
 	struct input_dev *input_dev;
+	struct input_dev *pen_input_dev;
 
 	u8 rd_buf[SURFACE3_PACKET_SIZE]		____cacheline_aligned;
 };
@@ -49,6 +51,14 @@ struct surface3_ts_data_finger {
 	u32 padding;
 } __packed;
 
+struct surface3_ts_data_pen {
+	u8 status;
+	__le16 x;
+	__le16 y;
+	__le16 pressure;
+	u8 padding;
+} __packed;
+
 static int surface3_spi_read(struct surface3_ts_data *ts_data)
 {
 	struct spi_device *spi = ts_data->spi;
@@ -114,6 +124,52 @@ static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *dat
 	input_sync(ts_data->input_dev);
 }
 
+static void surface3_spi_report_pen(struct surface3_ts_data *ts_data,
+				    struct surface3_ts_data_pen *pen)
+{
+	int st = pen->status;
+	int prox = st & 0x01;
+	int rubber = st & 0x18;
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOUCH,
+			 (st & 0x12));
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOOL_PEN,
+			 prox && !rubber);
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOOL_RUBBER,
+			 prox && rubber);
+
+	if (st) {
+		input_report_key(ts_data->pen_input_dev,
+				 BTN_STYLUS,
+				 st & 0x04);
+
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_X,
+				 get_unaligned_le16(&pen->x));
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_Y,
+				 get_unaligned_le16(&pen->y));
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_PRESSURE,
+				 get_unaligned_le16(&pen->pressure));
+	}
+}
+
+static void surface3_spi_process_pen(struct surface3_ts_data *ts_data, u8 *data)
+{
+	struct surface3_ts_data_pen *pen;
+
+	pen = (struct surface3_ts_data_pen *)&data[15];
+
+	surface3_spi_report_pen(ts_data, pen);
+	input_sync(ts_data->pen_input_dev);
+}
+
 static void surface3_spi_process(struct surface3_ts_data *ts_data)
 {
 	const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01};

drivers/input/touchscreen/surface3_spi.c static void surface3_spi_process(struct surface3_ts_data *ts_data) · line 187

Quoted 5 lines
drivers/input/touchscreen/surface3_spi.c +2 -0
@@ -127,6 +183,8 @@ static void surface3_spi_process(struct surface3_ts_data *ts_data)
 
 	if (data[9] == SURFACE3_REPORT_TOUCH)
 		surface3_spi_process_touch(ts_data, data);
+	else if (data[9] == SURFACE3_REPORT_PEN)
+		surface3_spi_process_pen(ts_data, data);

nitpicking: a switch/case would present better :)

The series is

Cheers,
Benjamin

Quoted 64 lines
drivers/input/touchscreen/surface3_spi.c +44 -0
 	else
 		dev_err(&ts_data->spi->dev,
 			"%s unknown packet type: %x, ignoring...\n",
@@ -224,6 +282,46 @@ static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
 	return 0;
 }
 
+static int surface3_spi_create_pen_input(struct surface3_ts_data *data)
+{
+	struct input_dev *input;
+	int error;
+
+	input = devm_input_allocate_device(&data->spi->dev);
+	if (!input)
+		return -ENOMEM;
+
+	data->pen_input_dev = input;
+
+	__set_bit(INPUT_PROP_DIRECT, input->propbit);
+	__set_bit(INPUT_PROP_POINTER, input->propbit);
+	input_set_abs_params(input, ABS_X, 0, 9600, 0, 0);
+	input_abs_set_res(input, ABS_X, 40);
+	input_set_abs_params(input, ABS_Y, 0, 7200, 0, 0);
+	input_abs_set_res(input, ABS_Y, 48);
+	input_set_abs_params(input, ABS_PRESSURE, 0, 1024, 0, 0);
+	input_set_capability(input, EV_KEY, BTN_TOUCH);
+	input_set_capability(input, EV_KEY, BTN_STYLUS);
+	input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
+	input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
+
+	input->name = "Surface3 SPI Pen Input";
+	input->phys = "input/ts";
+	input->id.bustype = BUS_SPI;
+	input->id.vendor = 0x045e;     /* Microsoft */
+	input->id.product = 0x0002;
+	input->id.version = 0x0000;
+
+	error = input_register_device(input);
+	if (error) {
+		dev_err(&data->spi->dev,
+			"Failed to register input device: %d", error);
+		return error;
+	}
+
+	return 0;
+}
+
 static int surface3_spi_probe(struct spi_device *spi)
 {
 	struct surface3_ts_data *data;
@@ -255,6 +353,10 @@ static int surface3_spi_probe(struct spi_device *spi)
 	if (error)
 		return error;
 
+	error = surface3_spi_create_pen_input(data);
+	if (error)
+		return error;
+
 	error = devm_request_threaded_irq(&spi->dev, spi->irq,
 					  NULL, surface3_spi_irq_handler,
 					  IRQF_ONESHOT,
Signature · 1 line
2.8.2
author
#4 lore raw

Re: [PATCH 2/2] Input - surface3_spi: add surface pen support for Surface 3

Quoted 9 lines · Benjamin Tissoires

Hi Stephen,

thanks for the submission.

Quoted 4 lines · Stephen Just

This change creates a second input device which will handle input from a Surface Pen. The Surface Pen supplies a different packet header than touch events, so it is simple to handle one or the other.

Quoted 7 lines
Quoted 2 lines

This patch handles both the newer Surface Pen with one button, and the older variant with a second button to switch to Eraser mode.

Oh... Would you mind sharing an evemu-record of such older variant with the Eraser mode? We might need to deal with that differently in libinput...

I have uploaded several evemu-record logs to
https://gist.github.com/stephenjust/6f00811a2b9a6eccca506ea3f9f5d870

I think the most notable thing is that the pen seems to be able to report its position at a further distance than it is able to determine which buttons are pressed, i.e. if you hold the eraser button down and bring the pen towards the screen, there will be a point where the pen is detected, and then as you get closer, it will change tool types. The same is true for detecting the other stylus button, you need to get closer for it to be detected.

Quoted 7 lines
Quoted 7 lines

[snip]

static void surface3_spi_process(struct surface3_ts_data *ts_data)

 {
      const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01};
(quoted diff) +0 -0
@@ -127,6 +183,8 @@ static void surface3_spi_process(struct surface3_ts_data *ts_data)
 
Quoted 7 lines

drivers/input/touchscreen/surface3_spi.c static void surface3_spi_process(struct surface3_ts_data *ts_data) · line 187

Quoted 4 lines
drivers/input/touchscreen/surface3_spi.c +2 -0
      if (data[9] == SURFACE3_REPORT_TOUCH)
              surface3_spi_process_touch(ts_data, data);
+     else if (data[9] == SURFACE3_REPORT_PEN)
+             surface3_spi_process_pen(ts_data, data);

nitpicking: a switch/case would present better :)

I will send out another revision with a switch/case shortly.

Regards,
Stephen Just

#5 lore raw

v2 cover Input: surface3_spi: Surface Pen Support

In reply to Stephen Just

This series adds support for Surface Pen devices to the Surface 3 touchscreen input driver.

This series works with both styles of pen:
the old style with pressure sensitive tip, an eraser button, and a secondary input button, as well as the new style pen which does not have a dedicated eraser button.

The top button on the pen is not supported by this patch. Those buttons are a separate Bluetooth device.

This is v2 of the series with a minor code cleanup to use a switch/case as suggested by Benjamin.

Regards,
Stephen Just

Stephen Just (2):
Input - surface3_spi: Prepare to add support for Surface Pen Input - surface3_spi: add surface pen support for Surface 3

 drivers/input/touchscreen/surface3_spi.c | 145 ++++++++++++++++++++++++++++---
 1 file changed, 132 insertions(+), 13 deletions(-)
Signature · 1 line
2.8.2
#6 lore raw

PATCH v2 1/2 Input - surface3_spi: Prepare to add support for Surface Pen

Surface Pens can be supported by handling a second report type. Prepare for this change.

---
 drivers/input/touchscreen/surface3_spi.c | 38 +++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 13 deletions(-)
drivers/input/touchscreen/surface3_spi.c +25 -13
index 58631d1..204a162 100644
@@ -27,6 +27,8 @@
 
 #define SURFACE3_PACKET_SIZE	264
 
+#define SURFACE3_REPORT_TOUCH	0xd2
+
 struct surface3_ts_data {
 	struct spi_device *spi;
 	struct gpio_desc *gpiod_rst[2];
@@ -84,19 +86,10 @@ static void surface3_spi_report_touch(struct surface3_ts_data *ts_data,
 	}
 }
 
-static void surface3_spi_process(struct surface3_ts_data *ts_data)
+static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *data)
 {
-	const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e,
-			       0x01, 0xd2, 0x00, 0x80, 0x01, 0x03, 0x03};
-	u8 *data = ts_data->rd_buf;
 	u16 timestamp;
 	unsigned int i;
-
-	if (memcmp(header, data, sizeof(header)))
-		dev_err(&ts_data->spi->dev,
-			"%s header error: %*ph, ignoring...\n",
-			__func__, (int)sizeof(header), data);
-
 	timestamp = get_unaligned_le16(&data[15]);
 
 	for (i = 0; i < 13; i++) {
@@ -121,6 +114,25 @@ static void surface3_spi_process(struct surface3_ts_data *ts_data)
 	input_sync(ts_data->input_dev);
 }
 
+static void surface3_spi_process(struct surface3_ts_data *ts_data)
+{
+	const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01};
+
+	u8 *data = ts_data->rd_buf;
+
+	if (memcmp(header, data, sizeof(header)))
+		dev_err(&ts_data->spi->dev,
+			"%s header error: %*ph, ignoring...\n",
+			__func__, (int)sizeof(header), data);
+
+	if (data[9] == SURFACE3_REPORT_TOUCH)
+		surface3_spi_process_touch(ts_data, data);
+	else
+		dev_err(&ts_data->spi->dev,
+			"%s unknown packet type: %x, ignoring...\n",
+			__func__, data[9]);
+}
+
 static irqreturn_t surface3_spi_irq_handler(int irq, void *dev_id)
 {
 	struct surface3_ts_data *data = dev_id;
@@ -176,7 +188,7 @@ static int surface3_spi_get_gpio_config(struct surface3_ts_data *data)
 	return 0;
 }
 
-static int surface3_spi_create_input(struct surface3_ts_data *data)
+static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
 {
 	struct input_dev *input;
 	int error;
@@ -199,7 +211,7 @@ static int surface3_spi_create_input(struct surface3_ts_data *data)
 	input->phys = "input/ts";
 	input->id.bustype = BUS_SPI;
 	input->id.vendor = 0x045e;	/* Microsoft */
-	input->id.product = 0x0000;
+	input->id.product = 0x0001;
 	input->id.version = 0x0000;
 
 	error = input_register_device(input);
@@ -239,7 +251,7 @@ static int surface3_spi_probe(struct spi_device *spi)
 	surface3_spi_power(data, false);
 	surface3_spi_power(data, true);
 
-	error = surface3_spi_create_input(data);
+	error = surface3_spi_create_touch_input(data);
 	if (error)
 		return error;
 
Signature · 1 line
2.8.2
#7 lore raw

PATCH v2 2/2 Input - surface3_spi: add surface pen support for Surface 3

In reply to Stephen Just

This change creates a second input device which will handle input from a Surface Pen. The Surface Pen supplies a different packet header than touch events, so it is simple to handle one or the other.

This patch handles both the newer Surface Pen with one button, and the older variant with a second button to switch to Eraser mode.

---
 drivers/input/touchscreen/surface3_spi.c | 111 ++++++++++++++++++++++++++++++-
 1 file changed, 109 insertions(+), 2 deletions(-)
drivers/input/touchscreen/surface3_spi.c +109 -2
index 204a162..665b02c 100644
@@ -28,11 +28,13 @@
 #define SURFACE3_PACKET_SIZE	264
 
 #define SURFACE3_REPORT_TOUCH	0xd2
+#define SURFACE3_REPORT_PEN	0x16
 
 struct surface3_ts_data {
 	struct spi_device *spi;
 	struct gpio_desc *gpiod_rst[2];
 	struct input_dev *input_dev;
+	struct input_dev *pen_input_dev;
 
 	u8 rd_buf[SURFACE3_PACKET_SIZE]		____cacheline_aligned;
 };
@@ -49,6 +51,14 @@ struct surface3_ts_data_finger {
 	u32 padding;
 } __packed;
 
+struct surface3_ts_data_pen {
+	u8 status;
+	__le16 x;
+	__le16 y;
+	__le16 pressure;
+	u8 padding;
+} __packed;
+
 static int surface3_spi_read(struct surface3_ts_data *ts_data)
 {
 	struct spi_device *spi = ts_data->spi;
@@ -114,6 +124,52 @@ static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *dat
 	input_sync(ts_data->input_dev);
 }
 
+static void surface3_spi_report_pen(struct surface3_ts_data *ts_data,
+				    struct surface3_ts_data_pen *pen)
+{
+	int st = pen->status;
+	int prox = st & 0x01;
+	int rubber = st & 0x18;
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOUCH,
+			 (st & 0x12));
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOOL_PEN,
+			 prox && !rubber);
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOOL_RUBBER,
+			 prox && rubber);
+
+	if (st) {
+		input_report_key(ts_data->pen_input_dev,
+				 BTN_STYLUS,
+				 st & 0x04);
+
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_X,
+				 get_unaligned_le16(&pen->x));
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_Y,
+				 get_unaligned_le16(&pen->y));
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_PRESSURE,
+				 get_unaligned_le16(&pen->pressure));
+	}
+}
+
+static void surface3_spi_process_pen(struct surface3_ts_data *ts_data, u8 *data)
+{
+	struct surface3_ts_data_pen *pen;
+
+	pen = (struct surface3_ts_data_pen *)&data[15];
+
+	surface3_spi_report_pen(ts_data, pen);
+	input_sync(ts_data->pen_input_dev);
+}
+
 static void surface3_spi_process(struct surface3_ts_data *ts_data)
 {
 	const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01};
@@ -125,12 +181,19 @@ static void surface3_spi_process(struct surface3_ts_data *ts_data)
 			"%s header error: %*ph, ignoring...\n",
 			__func__, (int)sizeof(header), data);
 
-	if (data[9] == SURFACE3_REPORT_TOUCH)
+	switch (data[9]) {
+	case SURFACE3_REPORT_TOUCH:
 		surface3_spi_process_touch(ts_data, data);
-	else
+		break;
+	case SURFACE3_REPORT_PEN:
+		surface3_spi_process_pen(ts_data, data);
+		break;
+	default:
 		dev_err(&ts_data->spi->dev,
 			"%s unknown packet type: %x, ignoring...\n",
 			__func__, data[9]);
+		break;
+	}
 }
 
 static irqreturn_t surface3_spi_irq_handler(int irq, void *dev_id)
@@ -224,6 +287,46 @@ static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
 	return 0;
 }
 
+static int surface3_spi_create_pen_input(struct surface3_ts_data *data)
+{
+	struct input_dev *input;
+	int error;
+
+	input = devm_input_allocate_device(&data->spi->dev);
+	if (!input)
+		return -ENOMEM;
+
+	data->pen_input_dev = input;
+
+	__set_bit(INPUT_PROP_DIRECT, input->propbit);
+	__set_bit(INPUT_PROP_POINTER, input->propbit);
+	input_set_abs_params(input, ABS_X, 0, 9600, 0, 0);
+	input_abs_set_res(input, ABS_X, 40);
+	input_set_abs_params(input, ABS_Y, 0, 7200, 0, 0);
+	input_abs_set_res(input, ABS_Y, 48);
+	input_set_abs_params(input, ABS_PRESSURE, 0, 1024, 0, 0);
+	input_set_capability(input, EV_KEY, BTN_TOUCH);
+	input_set_capability(input, EV_KEY, BTN_STYLUS);
+	input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
+	input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
+
+	input->name = "Surface3 SPI Pen Input";
+	input->phys = "input/ts";
+	input->id.bustype = BUS_SPI;
+	input->id.vendor = 0x045e;     /* Microsoft */
+	input->id.product = 0x0002;
+	input->id.version = 0x0000;
+
+	error = input_register_device(input);
+	if (error) {
+		dev_err(&data->spi->dev,
+			"Failed to register input device: %d", error);
+		return error;
+	}
+
+	return 0;
+}
+
 static int surface3_spi_probe(struct spi_device *spi)
 {
 	struct surface3_ts_data *data;
@@ -255,6 +358,10 @@ static int surface3_spi_probe(struct spi_device *spi)
 	if (error)
 		return error;
 
+	error = surface3_spi_create_pen_input(data);
+	if (error)
+		return error;
+
 	error = devm_request_threaded_irq(&spi->dev, spi->irq,
 					  NULL, surface3_spi_irq_handler,
 					  IRQF_ONESHOT,
Signature · 1 line
2.8.2
#8 lore raw

Re: [PATCH v2 2/2] Input - surface3_spi: add surface pen support for Surface 3

Hi Stephen,

Quoted 57 lines · Stephen Just

This change creates a second input device which will handle input from a Surface Pen. The Surface Pen supplies a different packet header than touch events, so it is simple to handle one or the other.

This patch handles both the newer Surface Pen with one button, and the older variant with a second button to switch to Eraser mode.

---
 drivers/input/touchscreen/surface3_spi.c | 111 ++++++++++++++++++++++++++++++-
 1 file changed, 109 insertions(+), 2 deletions(-)
drivers/input/touchscreen/surface3_spi.c +18 -0
index 204a162..665b02c 100644
@@ -28,11 +28,13 @@
 #define SURFACE3_PACKET_SIZE	264
 
 #define SURFACE3_REPORT_TOUCH	0xd2
+#define SURFACE3_REPORT_PEN	0x16
 
 struct surface3_ts_data {
 	struct spi_device *spi;
 	struct gpio_desc *gpiod_rst[2];
 	struct input_dev *input_dev;
+	struct input_dev *pen_input_dev;
 
 	u8 rd_buf[SURFACE3_PACKET_SIZE]		____cacheline_aligned;
 };
@@ -49,6 +51,14 @@ struct surface3_ts_data_finger {
 	u32 padding;
 } __packed;
 
+struct surface3_ts_data_pen {
+	u8 status;
+	__le16 x;
+	__le16 y;
+	__le16 pressure;
+	u8 padding;
+} __packed;
+
 static int surface3_spi_read(struct surface3_ts_data *ts_data)
 {
 	struct spi_device *spi = ts_data->spi;
@@ -114,6 +124,52 @@ static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *dat
 	input_sync(ts_data->input_dev);
 }
 
+static void surface3_spi_report_pen(struct surface3_ts_data *ts_data,
+				    struct surface3_ts_data_pen *pen)
+{
+	int st = pen->status;
+	int prox = st & 0x01;
+	int rubber = st & 0x18;
+
+	input_report_key(ts_data->pen_input_dev,

drivers/input/touchscreen/surface3_spi.c static void surface3_spi_process_touch(struct surface3_ts_data *ts_data, u8 *dat · line 144

Quoted 10 lines
drivers/input/touchscreen/surface3_spi.c +10 -0
+			 BTN_TOUCH,
+			 (st & 0x12));
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOOL_PEN,
+			 prox && !rubber);
+
+	input_report_key(ts_data->pen_input_dev,
+			 BTN_TOOL_RUBBER,
+			 prox && rubber);

I just had a call with Peter, and I noticed a small issue here (see
https://bugs.freedesktop.org/show_bug.cgi?id=96182)

On the out of proximity event, we switches back from tool_pen and release the tool_rubber in the very same frame, and this messes things in user space.

I first thought it was easier to fix libinput, but this behavior (having a tool in/out prox in its own prox) is the de facto standard and the current stylus (new style) behavior will break xorg-evdev, xorg-wacom and libinput.

Would you mind adding a check in the driver if the tool changed and add the input_sync() accordingly?

Cheers,
Benjamin

Quoted 114 lines
drivers/input/touchscreen/surface3_spi.c +81 -2
+
+	if (st) {
+		input_report_key(ts_data->pen_input_dev,
+				 BTN_STYLUS,
+				 st & 0x04);
+
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_X,
+				 get_unaligned_le16(&pen->x));
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_Y,
+				 get_unaligned_le16(&pen->y));
+		input_report_abs(ts_data->pen_input_dev,
+				 ABS_PRESSURE,
+				 get_unaligned_le16(&pen->pressure));
+	}
+}
+
+static void surface3_spi_process_pen(struct surface3_ts_data *ts_data, u8 *data)
+{
+	struct surface3_ts_data_pen *pen;
+
+	pen = (struct surface3_ts_data_pen *)&data[15];
+
+	surface3_spi_report_pen(ts_data, pen);
+	input_sync(ts_data->pen_input_dev);
+}
+
 static void surface3_spi_process(struct surface3_ts_data *ts_data)
 {
 	const char header[] = {0xff, 0xff, 0xff, 0xff, 0xa5, 0x5a, 0xe7, 0x7e, 0x01};
@@ -125,12 +181,19 @@ static void surface3_spi_process(struct surface3_ts_data *ts_data)
 			"%s header error: %*ph, ignoring...\n",
 			__func__, (int)sizeof(header), data);
 
-	if (data[9] == SURFACE3_REPORT_TOUCH)
+	switch (data[9]) {
+	case SURFACE3_REPORT_TOUCH:
 		surface3_spi_process_touch(ts_data, data);
-	else
+		break;
+	case SURFACE3_REPORT_PEN:
+		surface3_spi_process_pen(ts_data, data);
+		break;
+	default:
 		dev_err(&ts_data->spi->dev,
 			"%s unknown packet type: %x, ignoring...\n",
 			__func__, data[9]);
+		break;
+	}
 }
 
 static irqreturn_t surface3_spi_irq_handler(int irq, void *dev_id)
@@ -224,6 +287,46 @@ static int surface3_spi_create_touch_input(struct surface3_ts_data *data)
 	return 0;
 }
 
+static int surface3_spi_create_pen_input(struct surface3_ts_data *data)
+{
+	struct input_dev *input;
+	int error;
+
+	input = devm_input_allocate_device(&data->spi->dev);
+	if (!input)
+		return -ENOMEM;
+
+	data->pen_input_dev = input;
+
+	__set_bit(INPUT_PROP_DIRECT, input->propbit);
+	__set_bit(INPUT_PROP_POINTER, input->propbit);
+	input_set_abs_params(input, ABS_X, 0, 9600, 0, 0);
+	input_abs_set_res(input, ABS_X, 40);
+	input_set_abs_params(input, ABS_Y, 0, 7200, 0, 0);
+	input_abs_set_res(input, ABS_Y, 48);
+	input_set_abs_params(input, ABS_PRESSURE, 0, 1024, 0, 0);
+	input_set_capability(input, EV_KEY, BTN_TOUCH);
+	input_set_capability(input, EV_KEY, BTN_STYLUS);
+	input_set_capability(input, EV_KEY, BTN_TOOL_PEN);
+	input_set_capability(input, EV_KEY, BTN_TOOL_RUBBER);
+
+	input->name = "Surface3 SPI Pen Input";
+	input->phys = "input/ts";
+	input->id.bustype = BUS_SPI;
+	input->id.vendor = 0x045e;     /* Microsoft */
+	input->id.product = 0x0002;
+	input->id.version = 0x0000;
+
+	error = input_register_device(input);
+	if (error) {
+		dev_err(&data->spi->dev,
+			"Failed to register input device: %d", error);
+		return error;
+	}
+
+	return 0;
+}
+
 static int surface3_spi_probe(struct spi_device *spi)
 {
 	struct surface3_ts_data *data;
@@ -255,6 +358,10 @@ static int surface3_spi_probe(struct spi_device *spi)
 	if (error)
 		return error;
 
+	error = surface3_spi_create_pen_input(data);
+	if (error)
+		return error;
+
 	error = devm_request_threaded_irq(&spi->dev, spi->irq,
 					  NULL, surface3_spi_irq_handler,
 					  IRQF_ONESHOT,
Signature · 1 line
2.8.2