1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Wacom protocol 4 serial tablet driver
8 * Many thanks to Bill Seremetis, without whom PenPartner support
9 * would not have been possible. Thanks to Patrick Mahoney.
11 * This driver was developed with reference to much code written by others,
15 * - the USB wacom input driver, credited to many people
16 * (see drivers/input/tablet/wacom.h);
17 * - new and old versions of linuxwacom / xf86-input-wacom credited to
20 * - and xf86wacom.c (a presumably ancient version of the linuxwacom code),
24 * - support pad buttons; (requires access to a model with pad buttons)
25 * - support (protocol 4-style) tilt (requires access to a > 1.4 rom model)
29 * Wacom serial protocol 4 documentation taken from linuxwacom-0.9.9 code,
30 * protocol 4 uses 7 or 9 byte of data in the following format:
33 * bit 7 Sync bit always 1
34 * bit 6 Pointing device detected
35 * bit 5 Cursor = 0 / Stylus = 1
37 * bit 3 1 if a button on the pointing device has been pressed
70 * bit 6 Sign of pressure data; or wheel-rel for cursor tool
71 * bit 5 P7; or REL1 for cursor tool
72 * bit 4 P6; or REL0 for cursor tool
78 * byte 8 and 9 are optional and present only
83 * bit 6 Sign of tilt X
93 * bit 6 Sign of tilt Y
102 #include <linux/completion.h>
103 #include <linux/init.h>
104 #include <linux/input.h>
105 #include <linux/interrupt.h>
106 #include <linux/kernel.h>
107 #include <linux/module.h>
108 #include <linux/serio.h>
109 #include <linux/slab.h>
110 #include <linux/string.h>
113 MODULE_DESCRIPTION("Wacom protocol 4 serial tablet driver");
114 MODULE_LICENSE("GPL");
116 #define REQUEST_MODEL_AND_ROM_VERSION "~#"
117 #define REQUEST_MAX_COORDINATES "~C\r"
118 #define REQUEST_CONFIGURATION_STRING "~R\r"
119 #define REQUEST_RESET_TO_PROTOCOL_IV "\r#"
121 * Note: sending "\r$\r" causes at least the Digitizer II to send
122 * packets in ASCII instead of binary. "\r#" seems to undo that.
125 #define COMMAND_START_SENDING_PACKETS "ST\r"
126 #define COMMAND_STOP_SENDING_PACKETS "SP\r"
127 #define COMMAND_MULTI_MODE_INPUT "MU1\r"
128 #define COMMAND_ORIGIN_IN_UPPER_LEFT "OC1\r"
129 #define COMMAND_ENABLE_ALL_MACRO_BUTTONS "~M0\r"
130 #define COMMAND_DISABLE_GROUP_1_MACRO_BUTTONS "~M1\r"
131 #define COMMAND_TRANSMIT_AT_MAX_RATE "IT0\r"
132 #define COMMAND_DISABLE_INCREMENTAL_MODE "IN0\r"
133 #define COMMAND_ENABLE_CONTINUOUS_MODE "SR\r"
134 #define COMMAND_ENABLE_PRESSURE_MODE "PH1\r"
135 #define COMMAND_Z_FILTER "ZF1\r"
137 /* Note that this is a protocol 4 packet without tilt information. */
138 #define PACKET_LENGTH 7
142 #define F_COVERS_SCREEN 0x01
143 #define F_HAS_STYLUS2 0x02
144 #define F_HAS_SCROLLWHEEL 0x04
147 #define STYLUS_DEVICE_ID 0x02
148 #define CURSOR_DEVICE_ID 0x06
149 #define ERASER_DEVICE_ID 0x0A
151 enum { STYLUS = 1, ERASER, CURSOR };
153 static const struct {
158 { STYLUS_DEVICE_ID, BTN_TOOL_PEN },
159 { ERASER_DEVICE_ID, BTN_TOOL_RUBBER },
160 { CURSOR_DEVICE_ID, BTN_TOOL_MOUSE },
164 struct input_dev *dev;
165 struct completion cmd_done;
169 unsigned int extra_z_bits;
171 unsigned int res_x, res_y;
172 unsigned int max_x, max_y;
180 MODEL_CINTIQ = 0x504C, /* PL */
181 MODEL_CINTIQ2 = 0x4454, /* DT */
182 MODEL_DIGITIZER_II = 0x5544, /* UD */
183 MODEL_GRAPHIRE = 0x4554, /* ET */
184 MODEL_PENPARTNER = 0x4354, /* CT */
185 MODEL_ARTPAD_II = 0x4B54, /* KT */
188 static void wacom_handle_model_response(struct wacom *wacom)
190 int major_v, minor_v, r = 0;
193 p = strrchr(wacom->data, 'V');
195 r = sscanf(p + 1, "%u.%u", &major_v, &minor_v);
197 major_v = minor_v = 0;
199 switch (wacom->data[2] << 8 | wacom->data[3]) {
200 case MODEL_CINTIQ: /* UNTESTED */
202 if ((wacom->data[2] << 8 | wacom->data[3]) == MODEL_CINTIQ) {
203 wacom->dev->name = "Wacom Cintiq";
204 wacom->dev->id.version = MODEL_CINTIQ;
206 wacom->dev->name = "Wacom Cintiq II";
207 wacom->dev->id.version = MODEL_CINTIQ2;
212 switch (wacom->data[5] << 8 | wacom->data[6]) {
213 case 0x3731: /* PL-710 */
217 case 0x3535: /* PL-550 */
218 case 0x3830: /* PL-800 */
219 wacom->extra_z_bits = 2;
222 wacom->flags = F_COVERS_SCREEN;
225 case MODEL_PENPARTNER:
226 wacom->dev->name = "Wacom Penpartner";
227 wacom->dev->id.version = MODEL_PENPARTNER;
233 wacom->dev->name = "Wacom Graphire";
234 wacom->dev->id.version = MODEL_GRAPHIRE;
239 wacom->extra_z_bits = 2;
240 wacom->eraser_mask = 0x08;
241 wacom->flags = F_HAS_STYLUS2 | F_HAS_SCROLLWHEEL;
244 case MODEL_ARTPAD_II:
245 case MODEL_DIGITIZER_II:
246 wacom->dev->name = "Wacom Digitizer II";
247 wacom->dev->id.version = MODEL_DIGITIZER_II;
248 if (major_v == 1 && minor_v <= 2)
249 wacom->extra_z_bits = 0; /* UNTESTED */
253 dev_err(&wacom->dev->dev, "Unsupported Wacom model %s\n",
255 wacom->result = -ENODEV;
259 dev_info(&wacom->dev->dev, "%s tablet, version %u.%u\n",
260 wacom->dev->name, major_v, minor_v);
263 static void wacom_handle_configuration_response(struct wacom *wacom)
267 dev_dbg(&wacom->dev->dev, "Configuration string: %s\n", wacom->data);
268 r = sscanf(wacom->data, "~R%x,%u,%u,%u,%u", &skip, &skip, &skip,
269 &wacom->res_x, &wacom->res_y);
271 dev_warn(&wacom->dev->dev, "could not get resolution\n");
274 static void wacom_handle_coordinates_response(struct wacom *wacom)
278 dev_dbg(&wacom->dev->dev, "Coordinates string: %s\n", wacom->data);
279 r = sscanf(wacom->data, "~C%u,%u", &wacom->max_x, &wacom->max_y);
281 dev_warn(&wacom->dev->dev, "could not get max coordinates\n");
284 static void wacom_handle_response(struct wacom *wacom)
286 if (wacom->data[0] != '~' || wacom->data[1] != wacom->expect) {
287 dev_err(&wacom->dev->dev,
288 "Wacom got an unexpected response: %s\n", wacom->data);
289 wacom->result = -EIO;
293 switch (wacom->data[1]) {
295 wacom_handle_model_response(wacom);
298 wacom_handle_configuration_response(wacom);
301 wacom_handle_coordinates_response(wacom);
306 complete(&wacom->cmd_done);
309 static void wacom_handle_packet(struct wacom *wacom)
311 u8 in_proximity_p, stylus_p, button;
315 in_proximity_p = wacom->data[0] & 0x40;
316 stylus_p = wacom->data[0] & 0x20;
317 button = (wacom->data[3] & 0x78) >> 3;
318 x = (wacom->data[0] & 3) << 14 | wacom->data[1]<<7 | wacom->data[2];
319 y = (wacom->data[3] & 3) << 14 | wacom->data[4]<<7 | wacom->data[5];
321 if (in_proximity_p && stylus_p) {
322 z = wacom->data[6] & 0x7f;
323 if (wacom->extra_z_bits >= 1)
324 z = z << 1 | (wacom->data[3] & 0x4) >> 2;
325 if (wacom->extra_z_bits > 1)
326 z = z << 1 | (wacom->data[0] & 0x4) >> 2;
327 z = z ^ (0x40 << wacom->extra_z_bits);
333 tool = (button & wacom->eraser_mask) ? ERASER : STYLUS;
337 if (tool != wacom->tool && wacom->tool != 0) {
338 input_report_key(wacom->dev, tools[wacom->tool].input_id, 0);
339 input_sync(wacom->dev);
343 input_report_key(wacom->dev, tools[tool].input_id, in_proximity_p);
344 input_report_abs(wacom->dev, ABS_MISC,
345 in_proximity_p ? tools[tool].device_id : 0);
346 input_report_abs(wacom->dev, ABS_X, x);
347 input_report_abs(wacom->dev, ABS_Y, y);
348 input_report_abs(wacom->dev, ABS_PRESSURE, z);
350 input_report_key(wacom->dev, BTN_TOUCH, button & 1);
351 input_report_key(wacom->dev, BTN_STYLUS, button & 2);
352 input_report_key(wacom->dev, BTN_STYLUS2, button & 4);
354 input_report_key(wacom->dev, BTN_LEFT, button & 1);
355 input_report_key(wacom->dev, BTN_RIGHT, button & 2);
356 input_report_key(wacom->dev, BTN_MIDDLE, button & 4);
357 /* handle relative wheel for non-stylus device */
358 z = (wacom->data[6] & 0x30) >> 4;
359 if (wacom->data[6] & 0x40)
361 input_report_rel(wacom->dev, REL_WHEEL, z);
363 input_sync(wacom->dev);
366 static void wacom_clear_data_buf(struct wacom *wacom)
368 memset(wacom->data, 0, DATA_SIZE);
372 static irqreturn_t wacom_interrupt(struct serio *serio, unsigned char data,
375 struct wacom *wacom = serio_get_drvdata(serio);
381 * We're either expecting a carriage return-terminated ASCII
382 * response string, or a seven-byte packet with the MSB set on
385 * Note however that some tablets (the PenPartner, for
386 * example) don't send a carriage return at the end of a
387 * command. We handle these by waiting for timeout.
389 if (data == '\r' && !(wacom->data[0] & 0x80)) {
390 wacom_handle_response(wacom);
391 wacom_clear_data_buf(wacom);
395 /* Leave place for 0 termination */
396 if (wacom->idx > (DATA_SIZE - 2)) {
397 dev_dbg(&wacom->dev->dev,
398 "throwing away %d bytes of garbage\n", wacom->idx);
399 wacom_clear_data_buf(wacom);
401 wacom->data[wacom->idx++] = data;
403 if (wacom->idx == PACKET_LENGTH && (wacom->data[0] & 0x80)) {
404 wacom_handle_packet(wacom);
405 wacom_clear_data_buf(wacom);
411 static void wacom_disconnect(struct serio *serio)
413 struct wacom *wacom = serio_get_drvdata(serio);
416 serio_set_drvdata(serio, NULL);
417 input_unregister_device(wacom->dev);
421 static int wacom_send(struct serio *serio, const u8 *command)
425 for (; !err && *command; command++)
426 err = serio_write(serio, *command);
431 static int wacom_send_setup_string(struct wacom *wacom, struct serio *serio)
435 switch (wacom->dev->id.version) {
436 case MODEL_CINTIQ: /* UNTESTED */
437 cmd = COMMAND_ORIGIN_IN_UPPER_LEFT
438 COMMAND_TRANSMIT_AT_MAX_RATE
439 COMMAND_ENABLE_CONTINUOUS_MODE
440 COMMAND_START_SENDING_PACKETS;
443 case MODEL_PENPARTNER:
444 cmd = COMMAND_ENABLE_PRESSURE_MODE
445 COMMAND_START_SENDING_PACKETS;
449 cmd = COMMAND_MULTI_MODE_INPUT
450 COMMAND_ORIGIN_IN_UPPER_LEFT
451 COMMAND_ENABLE_ALL_MACRO_BUTTONS
452 COMMAND_DISABLE_GROUP_1_MACRO_BUTTONS
453 COMMAND_TRANSMIT_AT_MAX_RATE
454 COMMAND_DISABLE_INCREMENTAL_MODE
455 COMMAND_ENABLE_CONTINUOUS_MODE
457 COMMAND_START_SENDING_PACKETS;
461 return wacom_send(serio, cmd);
464 static int wacom_send_and_wait(struct wacom *wacom, struct serio *serio,
465 const u8 *cmd, const char *desc)
470 wacom->expect = cmd[1];
471 init_completion(&wacom->cmd_done);
473 err = wacom_send(serio, cmd);
477 u = wait_for_completion_timeout(&wacom->cmd_done, HZ);
479 /* Timeout, process what we've received. */
480 wacom_handle_response(wacom);
484 return wacom->result;
487 static int wacom_setup(struct wacom *wacom, struct serio *serio)
491 /* Note that setting the link speed is the job of inputattach.
492 * We assume that reset negotiation has already happened,
494 err = wacom_send_and_wait(wacom, serio, REQUEST_MODEL_AND_ROM_VERSION,
495 "model and version");
499 if (!(wacom->res_x && wacom->res_y)) {
500 err = wacom_send_and_wait(wacom, serio,
501 REQUEST_CONFIGURATION_STRING,
502 "configuration string");
507 if (!(wacom->max_x && wacom->max_y)) {
508 err = wacom_send_and_wait(wacom, serio,
509 REQUEST_MAX_COORDINATES,
510 "coordinates string");
515 return wacom_send_setup_string(wacom, serio);
518 static int wacom_connect(struct serio *serio, struct serio_driver *drv)
521 struct input_dev *input_dev;
524 wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
525 input_dev = input_allocate_device();
526 if (!wacom || !input_dev)
529 wacom->dev = input_dev;
530 wacom->extra_z_bits = 1;
531 wacom->eraser_mask = 0x04;
532 wacom->tool = wacom->idx = 0;
533 snprintf(wacom->phys, sizeof(wacom->phys), "%s/input0", serio->phys);
534 input_dev->phys = wacom->phys;
535 input_dev->id.bustype = BUS_RS232;
536 input_dev->id.vendor = SERIO_WACOM_IV;
537 input_dev->id.product = serio->id.extra;
538 input_dev->dev.parent = &serio->dev;
540 input_dev->evbit[0] =
541 BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) | BIT_MASK(EV_REL);
542 set_bit(ABS_MISC, input_dev->absbit);
543 set_bit(BTN_TOOL_PEN, input_dev->keybit);
544 set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
545 set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
546 set_bit(BTN_TOUCH, input_dev->keybit);
547 set_bit(BTN_STYLUS, input_dev->keybit);
548 set_bit(BTN_LEFT, input_dev->keybit);
549 set_bit(BTN_RIGHT, input_dev->keybit);
550 set_bit(BTN_MIDDLE, input_dev->keybit);
552 serio_set_drvdata(serio, wacom);
554 err = serio_open(serio, drv);
558 err = wacom_setup(wacom, serio);
562 set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
563 if (!(wacom->flags & F_COVERS_SCREEN))
564 __set_bit(INPUT_PROP_POINTER, input_dev->propbit);
566 if (wacom->flags & F_HAS_STYLUS2)
567 __set_bit(BTN_STYLUS2, input_dev->keybit);
569 if (wacom->flags & F_HAS_SCROLLWHEEL)
570 __set_bit(REL_WHEEL, input_dev->relbit);
572 input_abs_set_res(wacom->dev, ABS_X, wacom->res_x);
573 input_abs_set_res(wacom->dev, ABS_Y, wacom->res_y);
574 input_set_abs_params(wacom->dev, ABS_X, 0, wacom->max_x, 0, 0);
575 input_set_abs_params(wacom->dev, ABS_Y, 0, wacom->max_y, 0, 0);
576 input_set_abs_params(wacom->dev, ABS_PRESSURE, -1,
577 (1 << (7 + wacom->extra_z_bits)) - 1, 0, 0);
579 err = input_register_device(wacom->dev);
588 serio_set_drvdata(serio, NULL);
589 input_free_device(input_dev);
594 static const struct serio_device_id wacom_serio_ids[] = {
597 .proto = SERIO_WACOM_IV,
604 MODULE_DEVICE_TABLE(serio, wacom_serio_ids);
606 static struct serio_driver wacom_drv = {
608 .name = "wacom_serial4",
610 .description = "Wacom protocol 4 serial tablet driver",
611 .id_table = wacom_serio_ids,
612 .interrupt = wacom_interrupt,
613 .connect = wacom_connect,
614 .disconnect = wacom_disconnect,
617 module_serio_driver(wacom_drv);