Linux Audio

Check our new training course

Yocto distribution development and maintenance

Need a Yocto distribution for your embedded project?
Loading...
v4.6
 
  1/*
  2 * Wacom protocol 4 serial tablet driver
  3 *
  4 * Copyright 2014      Hans de Goede <hdegoede@redhat.com>
  5 * Copyright 2011-2012 Julian Squires <julian@cipht.net>
  6 *
  7 * This program is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU General Public License as published by the
  9 * Free Software Foundation; either version of 2 of the License, or (at your
 10 * option) any later version. See the file COPYING in the main directory of
 11 * this archive for more details.
 12 *
 13 * Many thanks to Bill Seremetis, without whom PenPartner support
 14 * would not have been possible. Thanks to Patrick Mahoney.
 15 *
 16 * This driver was developed with reference to much code written by others,
 17 * particularly:
 18 *  - elo, gunze drivers by Vojtech Pavlik <vojtech@ucw.cz>;
 19 *  - wacom_w8001 driver by Jaya Kumar <jayakumar.lkml@gmail.com>;
 20 *  - the USB wacom input driver, credited to many people
 21 *    (see drivers/input/tablet/wacom.h);
 22 *  - new and old versions of linuxwacom / xf86-input-wacom credited to
 23 *    Frederic Lepied, France. <Lepied@XFree86.org> and
 24 *    Ping Cheng, Wacom. <pingc@wacom.com>;
 25 *  - and xf86wacom.c (a presumably ancient version of the linuxwacom code),
 26 *    by Frederic Lepied and Raph Levien <raph@gtk.org>.
 27 *
 28 * To do:
 29 *  - support pad buttons; (requires access to a model with pad buttons)
 30 *  - support (protocol 4-style) tilt (requires access to a > 1.4 rom model)
 31 */
 32
 33/*
 34 * Wacom serial protocol 4 documentation taken from linuxwacom-0.9.9 code,
 35 * protocol 4 uses 7 or 9 byte of data in the following format:
 36 *
 37 *	Byte 1
 38 *	bit 7  Sync bit always 1
 39 *	bit 6  Pointing device detected
 40 *	bit 5  Cursor = 0 / Stylus = 1
 41 *	bit 4  Reserved
 42 *	bit 3  1 if a button on the pointing device has been pressed
 43 *	bit 2  P0 (optional)
 44 *	bit 1  X15
 45 *	bit 0  X14
 46 *
 47 *	Byte 2
 48 *	bit 7  Always 0
 49 *	bits 6-0 = X13 - X7
 50 *
 51 *	Byte 3
 52 *	bit 7  Always 0
 53 *	bits 6-0 = X6 - X0
 54 *
 55 *	Byte 4
 56 *	bit 7  Always 0
 57 *	bit 6  B3
 58 *	bit 5  B2
 59 *	bit 4  B1
 60 *	bit 3  B0
 61 *	bit 2  P1 (optional)
 62 *	bit 1  Y15
 63 *	bit 0  Y14
 64 *
 65 *	Byte 5
 66 *	bit 7  Always 0
 67 *	bits 6-0 = Y13 - Y7
 68 *
 69 *	Byte 6
 70 *	bit 7  Always 0
 71 *	bits 6-0 = Y6 - Y0
 72 *
 73 *	Byte 7
 74 *	bit 7 Always 0
 75 *	bit 6  Sign of pressure data; or wheel-rel for cursor tool
 76 *	bit 5  P7; or REL1 for cursor tool
 77 *	bit 4  P6; or REL0 for cursor tool
 78 *	bit 3  P5
 79 *	bit 2  P4
 80 *	bit 1  P3
 81 *	bit 0  P2
 82 *
 83 *	byte 8 and 9 are optional and present only
 84 *	in tilt mode.
 85 *
 86 *	Byte 8
 87 *	bit 7 Always 0
 88 *	bit 6 Sign of tilt X
 89 *	bit 5  Xt6
 90 *	bit 4  Xt5
 91 *	bit 3  Xt4
 92 *	bit 2  Xt3
 93 *	bit 1  Xt2
 94 *	bit 0  Xt1
 95 *
 96 *	Byte 9
 97 *	bit 7 Always 0
 98 *	bit 6 Sign of tilt Y
 99 *	bit 5  Yt6
100 *	bit 4  Yt5
101 *	bit 3  Yt4
102 *	bit 2  Yt3
103 *	bit 1  Yt2
104 *	bit 0  Yt1
105 */
106
107#include <linux/completion.h>
108#include <linux/init.h>
109#include <linux/input.h>
110#include <linux/interrupt.h>
111#include <linux/kernel.h>
112#include <linux/module.h>
113#include <linux/serio.h>
114#include <linux/slab.h>
115#include <linux/string.h>
116
117MODULE_AUTHOR("Julian Squires <julian@cipht.net>, Hans de Goede <hdegoede@redhat.com>");
118MODULE_DESCRIPTION("Wacom protocol 4 serial tablet driver");
119MODULE_LICENSE("GPL");
120
121#define REQUEST_MODEL_AND_ROM_VERSION	"~#"
122#define REQUEST_MAX_COORDINATES		"~C\r"
123#define REQUEST_CONFIGURATION_STRING	"~R\r"
124#define REQUEST_RESET_TO_PROTOCOL_IV	"\r#"
125/*
126 * Note: sending "\r$\r" causes at least the Digitizer II to send
127 * packets in ASCII instead of binary.  "\r#" seems to undo that.
128 */
129
130#define COMMAND_START_SENDING_PACKETS		"ST\r"
131#define COMMAND_STOP_SENDING_PACKETS		"SP\r"
132#define COMMAND_MULTI_MODE_INPUT		"MU1\r"
133#define COMMAND_ORIGIN_IN_UPPER_LEFT		"OC1\r"
134#define COMMAND_ENABLE_ALL_MACRO_BUTTONS	"~M0\r"
135#define COMMAND_DISABLE_GROUP_1_MACRO_BUTTONS	"~M1\r"
136#define COMMAND_TRANSMIT_AT_MAX_RATE		"IT0\r"
137#define COMMAND_DISABLE_INCREMENTAL_MODE	"IN0\r"
138#define COMMAND_ENABLE_CONTINUOUS_MODE		"SR\r"
139#define COMMAND_ENABLE_PRESSURE_MODE		"PH1\r"
140#define COMMAND_Z_FILTER			"ZF1\r"
141
142/* Note that this is a protocol 4 packet without tilt information. */
143#define PACKET_LENGTH		7
144#define DATA_SIZE		32
145
146/* flags */
147#define F_COVERS_SCREEN		0x01
148#define F_HAS_STYLUS2		0x02
149#define F_HAS_SCROLLWHEEL	0x04
150
151/* device IDs */
152#define STYLUS_DEVICE_ID	0x02
153#define CURSOR_DEVICE_ID	0x06
154#define ERASER_DEVICE_ID	0x0A
155
156enum { STYLUS = 1, ERASER, CURSOR };
157
158static const struct {
159	int device_id;
160	int input_id;
161} tools[] = {
162	{ 0, 0 },
163	{ STYLUS_DEVICE_ID, BTN_TOOL_PEN },
164	{ ERASER_DEVICE_ID, BTN_TOOL_RUBBER },
165	{ CURSOR_DEVICE_ID, BTN_TOOL_MOUSE },
166};
167
168struct wacom {
169	struct input_dev *dev;
170	struct completion cmd_done;
171	int result;
172	u8 expect;
173	u8 eraser_mask;
174	unsigned int extra_z_bits;
175	unsigned int flags;
176	unsigned int res_x, res_y;
177	unsigned int max_x, max_y;
178	unsigned int tool;
179	unsigned int idx;
180	u8 data[DATA_SIZE];
181	char phys[32];
182};
183
184enum {
185	MODEL_CINTIQ		= 0x504C, /* PL */
186	MODEL_CINTIQ2		= 0x4454, /* DT */
187	MODEL_DIGITIZER_II	= 0x5544, /* UD */
188	MODEL_GRAPHIRE		= 0x4554, /* ET */
189	MODEL_PENPARTNER	= 0x4354, /* CT */
 
190};
191
192static void wacom_handle_model_response(struct wacom *wacom)
193{
194	int major_v, minor_v, r = 0;
195	char *p;
196
197	p = strrchr(wacom->data, 'V');
198	if (p)
199		r = sscanf(p + 1, "%u.%u", &major_v, &minor_v);
200	if (r != 2)
201		major_v = minor_v = 0;
202
203	switch (wacom->data[2] << 8 | wacom->data[3]) {
204	case MODEL_CINTIQ:	/* UNTESTED */
205	case MODEL_CINTIQ2:
206		if ((wacom->data[2] << 8 | wacom->data[3]) == MODEL_CINTIQ) {
207			wacom->dev->name = "Wacom Cintiq";
208			wacom->dev->id.version = MODEL_CINTIQ;
209		} else {
210			wacom->dev->name = "Wacom Cintiq II";
211			wacom->dev->id.version = MODEL_CINTIQ2;
212		}
213		wacom->res_x = 508;
214		wacom->res_y = 508;
215
216		switch (wacom->data[5] << 8 | wacom->data[6]) {
217		case 0x3731: /* PL-710 */
218			wacom->res_x = 2540;
219			wacom->res_y = 2540;
220			/* fall through */
221		case 0x3535: /* PL-550 */
222		case 0x3830: /* PL-800 */
223			wacom->extra_z_bits = 2;
224		}
225
226		wacom->flags = F_COVERS_SCREEN;
227		break;
228
229	case MODEL_PENPARTNER:
230		wacom->dev->name = "Wacom Penpartner";
231		wacom->dev->id.version = MODEL_PENPARTNER;
232		wacom->res_x = 1000;
233		wacom->res_y = 1000;
234		break;
235
236	case MODEL_GRAPHIRE:
237		wacom->dev->name = "Wacom Graphire";
238		wacom->dev->id.version = MODEL_GRAPHIRE;
239		wacom->res_x = 1016;
240		wacom->res_y = 1016;
241		wacom->max_x = 5103;
242		wacom->max_y = 3711;
243		wacom->extra_z_bits = 2;
244		wacom->eraser_mask = 0x08;
245		wacom->flags = F_HAS_STYLUS2 | F_HAS_SCROLLWHEEL;
246		break;
247
 
248	case MODEL_DIGITIZER_II:
249		wacom->dev->name = "Wacom Digitizer II";
250		wacom->dev->id.version = MODEL_DIGITIZER_II;
251		if (major_v == 1 && minor_v <= 2)
252			wacom->extra_z_bits = 0; /* UNTESTED */
253		break;
254
255	default:
256		dev_err(&wacom->dev->dev, "Unsupported Wacom model %s\n",
257			wacom->data);
258		wacom->result = -ENODEV;
259		return;
260	}
261
262	dev_info(&wacom->dev->dev, "%s tablet, version %u.%u\n",
263		 wacom->dev->name, major_v, minor_v);
264}
265
266static void wacom_handle_configuration_response(struct wacom *wacom)
267{
268	int r, skip;
269
270	dev_dbg(&wacom->dev->dev, "Configuration string: %s\n", wacom->data);
271	r = sscanf(wacom->data, "~R%x,%u,%u,%u,%u", &skip, &skip, &skip,
272		   &wacom->res_x, &wacom->res_y);
273	if (r != 5)
274		dev_warn(&wacom->dev->dev, "could not get resolution\n");
275}
276
277static void wacom_handle_coordinates_response(struct wacom *wacom)
278{
279	int r;
280
281	dev_dbg(&wacom->dev->dev, "Coordinates string: %s\n", wacom->data);
282	r = sscanf(wacom->data, "~C%u,%u", &wacom->max_x, &wacom->max_y);
283	if (r != 2)
284		dev_warn(&wacom->dev->dev, "could not get max coordinates\n");
285}
286
287static void wacom_handle_response(struct wacom *wacom)
288{
289	if (wacom->data[0] != '~' || wacom->data[1] != wacom->expect) {
290		dev_err(&wacom->dev->dev,
291			"Wacom got an unexpected response: %s\n", wacom->data);
292		wacom->result = -EIO;
293	} else {
294		wacom->result = 0;
295
296		switch (wacom->data[1]) {
297		case '#':
298			wacom_handle_model_response(wacom);
299			break;
300		case 'R':
301			wacom_handle_configuration_response(wacom);
302			break;
303		case 'C':
304			wacom_handle_coordinates_response(wacom);
305			break;
306		}
307	}
308
309	complete(&wacom->cmd_done);
310}
311
312static void wacom_handle_packet(struct wacom *wacom)
313{
314	u8 in_proximity_p, stylus_p, button;
315	unsigned int tool;
316	int x, y, z;
317
318	in_proximity_p = wacom->data[0] & 0x40;
319	stylus_p = wacom->data[0] & 0x20;
320	button = (wacom->data[3] & 0x78) >> 3;
321	x = (wacom->data[0] & 3) << 14 | wacom->data[1]<<7 | wacom->data[2];
322	y = (wacom->data[3] & 3) << 14 | wacom->data[4]<<7 | wacom->data[5];
323
324	if (in_proximity_p && stylus_p) {
325		z = wacom->data[6] & 0x7f;
326		if (wacom->extra_z_bits >= 1)
327			z = z << 1 | (wacom->data[3] & 0x4) >> 2;
328		if (wacom->extra_z_bits > 1)
329			z = z << 1 | (wacom->data[0] & 0x4) >> 2;
330		z = z ^ (0x40 << wacom->extra_z_bits);
331	} else {
332		z = -1;
333	}
334
335	if (stylus_p)
336		tool = (button & wacom->eraser_mask) ? ERASER : STYLUS;
337	else
338		tool = CURSOR;
339
340	if (tool != wacom->tool && wacom->tool != 0) {
341		input_report_key(wacom->dev, tools[wacom->tool].input_id, 0);
342		input_sync(wacom->dev);
343	}
344	wacom->tool = tool;
345
346	input_report_key(wacom->dev, tools[tool].input_id, in_proximity_p);
347	input_report_abs(wacom->dev, ABS_MISC,
348			 in_proximity_p ? tools[tool].device_id : 0);
349	input_report_abs(wacom->dev, ABS_X, x);
350	input_report_abs(wacom->dev, ABS_Y, y);
351	input_report_abs(wacom->dev, ABS_PRESSURE, z);
352	if (stylus_p) {
353		input_report_key(wacom->dev, BTN_TOUCH, button & 1);
354		input_report_key(wacom->dev, BTN_STYLUS, button & 2);
355		input_report_key(wacom->dev, BTN_STYLUS2, button & 4);
356	} else {
357		input_report_key(wacom->dev, BTN_LEFT, button & 1);
358		input_report_key(wacom->dev, BTN_RIGHT, button & 2);
359		input_report_key(wacom->dev, BTN_MIDDLE, button & 4);
360		/* handle relative wheel for non-stylus device */
361		z = (wacom->data[6] & 0x30) >> 4;
362		if (wacom->data[6] & 0x40)
363			z = -z;
364		input_report_rel(wacom->dev, REL_WHEEL, z);
365	}
366	input_sync(wacom->dev);
367}
368
369static void wacom_clear_data_buf(struct wacom *wacom)
370{
371	memset(wacom->data, 0, DATA_SIZE);
372	wacom->idx = 0;
373}
374
375static irqreturn_t wacom_interrupt(struct serio *serio, unsigned char data,
376				   unsigned int flags)
377{
378	struct wacom *wacom = serio_get_drvdata(serio);
379
380	if (data & 0x80)
381		wacom->idx = 0;
382
383	/*
384	 * We're either expecting a carriage return-terminated ASCII
385	 * response string, or a seven-byte packet with the MSB set on
386	 * the first byte.
387	 *
388	 * Note however that some tablets (the PenPartner, for
389	 * example) don't send a carriage return at the end of a
390	 * command.  We handle these by waiting for timeout.
391	 */
392	if (data == '\r' && !(wacom->data[0] & 0x80)) {
393		wacom_handle_response(wacom);
394		wacom_clear_data_buf(wacom);
395		return IRQ_HANDLED;
396	}
397
398	/* Leave place for 0 termination */
399	if (wacom->idx > (DATA_SIZE - 2)) {
400		dev_dbg(&wacom->dev->dev,
401			"throwing away %d bytes of garbage\n", wacom->idx);
402		wacom_clear_data_buf(wacom);
403	}
404	wacom->data[wacom->idx++] = data;
405
406	if (wacom->idx == PACKET_LENGTH && (wacom->data[0] & 0x80)) {
407		wacom_handle_packet(wacom);
408		wacom_clear_data_buf(wacom);
409	}
410
411	return IRQ_HANDLED;
412}
413
414static void wacom_disconnect(struct serio *serio)
415{
416	struct wacom *wacom = serio_get_drvdata(serio);
417
418	serio_close(serio);
419	serio_set_drvdata(serio, NULL);
420	input_unregister_device(wacom->dev);
421	kfree(wacom);
422}
423
424static int wacom_send(struct serio *serio, const u8 *command)
425{
426	int err = 0;
427
428	for (; !err && *command; command++)
429		err = serio_write(serio, *command);
430
431	return err;
432}
433
434static int wacom_send_setup_string(struct wacom *wacom, struct serio *serio)
435{
436	const u8 *cmd;
437
438	switch (wacom->dev->id.version) {
439	case MODEL_CINTIQ:	/* UNTESTED */
440		cmd = COMMAND_ORIGIN_IN_UPPER_LEFT
441			COMMAND_TRANSMIT_AT_MAX_RATE
442			COMMAND_ENABLE_CONTINUOUS_MODE
443			COMMAND_START_SENDING_PACKETS;
444		break;
445
446	case MODEL_PENPARTNER:
447		cmd = COMMAND_ENABLE_PRESSURE_MODE
448			COMMAND_START_SENDING_PACKETS;
449		break;
450
451	default:
452		cmd = COMMAND_MULTI_MODE_INPUT
453			COMMAND_ORIGIN_IN_UPPER_LEFT
454			COMMAND_ENABLE_ALL_MACRO_BUTTONS
455			COMMAND_DISABLE_GROUP_1_MACRO_BUTTONS
456			COMMAND_TRANSMIT_AT_MAX_RATE
457			COMMAND_DISABLE_INCREMENTAL_MODE
458			COMMAND_ENABLE_CONTINUOUS_MODE
459			COMMAND_Z_FILTER
460			COMMAND_START_SENDING_PACKETS;
461		break;
462	}
463
464	return wacom_send(serio, cmd);
465}
466
467static int wacom_send_and_wait(struct wacom *wacom, struct serio *serio,
468			       const u8 *cmd, const char *desc)
469{
470	int err;
471	unsigned long u;
472
473	wacom->expect = cmd[1];
474	init_completion(&wacom->cmd_done);
475
476	err = wacom_send(serio, cmd);
477	if (err)
478		return err;
479
480	u = wait_for_completion_timeout(&wacom->cmd_done, HZ);
481	if (u == 0) {
482		/* Timeout, process what we've received. */
483		wacom_handle_response(wacom);
484	}
485
486	wacom->expect = 0;
487	return wacom->result;
488}
489
490static int wacom_setup(struct wacom *wacom, struct serio *serio)
491{
492	int err;
493
494	/* Note that setting the link speed is the job of inputattach.
495	 * We assume that reset negotiation has already happened,
496	 * here. */
497	err = wacom_send_and_wait(wacom, serio, REQUEST_MODEL_AND_ROM_VERSION,
498				  "model and version");
499	if (err)
500		return err;
501
502	if (!(wacom->res_x && wacom->res_y)) {
503		err = wacom_send_and_wait(wacom, serio,
504					  REQUEST_CONFIGURATION_STRING,
505					  "configuration string");
506		if (err)
507			return err;
508	}
509
510	if (!(wacom->max_x && wacom->max_y)) {
511		err = wacom_send_and_wait(wacom, serio,
512					  REQUEST_MAX_COORDINATES,
513					  "coordinates string");
514		if (err)
515			return err;
516	}
517
518	return wacom_send_setup_string(wacom, serio);
519}
520
521static int wacom_connect(struct serio *serio, struct serio_driver *drv)
522{
523	struct wacom *wacom;
524	struct input_dev *input_dev;
525	int err = -ENOMEM;
526
527	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
528	input_dev = input_allocate_device();
529	if (!wacom || !input_dev)
530		goto free_device;
531
532	wacom->dev = input_dev;
533	wacom->extra_z_bits = 1;
534	wacom->eraser_mask = 0x04;
535	wacom->tool = wacom->idx = 0;
536	snprintf(wacom->phys, sizeof(wacom->phys), "%s/input0", serio->phys);
537	input_dev->phys = wacom->phys;
538	input_dev->id.bustype = BUS_RS232;
539	input_dev->id.vendor  = SERIO_WACOM_IV;
540	input_dev->id.product = serio->id.extra;
541	input_dev->dev.parent = &serio->dev;
542
543	input_dev->evbit[0] =
544		BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) | BIT_MASK(EV_REL);
545	set_bit(ABS_MISC, input_dev->absbit);
546	set_bit(BTN_TOOL_PEN, input_dev->keybit);
547	set_bit(BTN_TOOL_RUBBER, input_dev->keybit);
548	set_bit(BTN_TOOL_MOUSE, input_dev->keybit);
549	set_bit(BTN_TOUCH, input_dev->keybit);
550	set_bit(BTN_STYLUS, input_dev->keybit);
551	set_bit(BTN_LEFT, input_dev->keybit);
552	set_bit(BTN_RIGHT, input_dev->keybit);
553	set_bit(BTN_MIDDLE, input_dev->keybit);
554
555	serio_set_drvdata(serio, wacom);
556
557	err = serio_open(serio, drv);
558	if (err)
559		goto free_device;
560
561	err = wacom_setup(wacom, serio);
562	if (err)
563		goto close_serio;
564
565	set_bit(INPUT_PROP_DIRECT, input_dev->propbit);
566	if (!(wacom->flags & F_COVERS_SCREEN))
567		__set_bit(INPUT_PROP_POINTER, input_dev->propbit);
568
569	if (wacom->flags & F_HAS_STYLUS2)
570		__set_bit(BTN_STYLUS2, input_dev->keybit);
571
572	if (wacom->flags & F_HAS_SCROLLWHEEL)
573		__set_bit(REL_WHEEL, input_dev->relbit);
574
575	input_abs_set_res(wacom->dev, ABS_X, wacom->res_x);
576	input_abs_set_res(wacom->dev, ABS_Y, wacom->res_y);
577	input_set_abs_params(wacom->dev, ABS_X, 0, wacom->max_x, 0, 0);
578	input_set_abs_params(wacom->dev, ABS_Y, 0, wacom->max_y, 0, 0);
579	input_set_abs_params(wacom->dev, ABS_PRESSURE, -1,
580			     (1 << (7 + wacom->extra_z_bits)) - 1, 0, 0);
581
582	err = input_register_device(wacom->dev);
583	if (err)
584		goto close_serio;
585
586	return 0;
587
588close_serio:
589	serio_close(serio);
590free_device:
591	serio_set_drvdata(serio, NULL);
592	input_free_device(input_dev);
593	kfree(wacom);
594	return err;
595}
596
597static struct serio_device_id wacom_serio_ids[] = {
598	{
599		.type	= SERIO_RS232,
600		.proto	= SERIO_WACOM_IV,
601		.id	= SERIO_ANY,
602		.extra	= SERIO_ANY,
603	},
604	{ 0 }
605};
606
607MODULE_DEVICE_TABLE(serio, wacom_serio_ids);
608
609static struct serio_driver wacom_drv = {
610	.driver		= {
611		.name	= "wacom_serial4",
612	},
613	.description	= "Wacom protocol 4 serial tablet driver",
614	.id_table	= wacom_serio_ids,
615	.interrupt	= wacom_interrupt,
616	.connect	= wacom_connect,
617	.disconnect	= wacom_disconnect,
618};
619
620module_serio_driver(wacom_drv);
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Wacom protocol 4 serial tablet driver
  4 *
  5 * Copyright 2014      Hans de Goede <hdegoede@redhat.com>
  6 * Copyright 2011-2012 Julian Squires <julian@cipht.net>
  7 *
 
 
 
 
 
 
  8 * Many thanks to Bill Seremetis, without whom PenPartner support
  9 * would not have been possible. Thanks to Patrick Mahoney.
 10 *
 11 * This driver was developed with reference to much code written by others,
 12 * particularly:
 13 *  - elo, gunze drivers by Vojtech Pavlik <vojtech@ucw.cz>;
 14 *  - wacom_w8001 driver by Jaya Kumar <jayakumar.lkml@gmail.com>;
 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
 18 *    Frederic Lepied, France. <Lepied@XFree86.org> and
 19 *    Ping Cheng, Wacom. <pingc@wacom.com>;
 20 *  - and xf86wacom.c (a presumably ancient version of the linuxwacom code),
 21 *    by Frederic Lepied and Raph Levien <raph@gtk.org>.
 22 *
 23 * To do:
 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)
 26 */
 27
 28/*
 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:
 31 *
 32 *	Byte 1
 33 *	bit 7  Sync bit always 1
 34 *	bit 6  Pointing device detected
 35 *	bit 5  Cursor = 0 / Stylus = 1
 36 *	bit 4  Reserved
 37 *	bit 3  1 if a button on the pointing device has been pressed
 38 *	bit 2  P0 (optional)
 39 *	bit 1  X15
 40 *	bit 0  X14
 41 *
 42 *	Byte 2
 43 *	bit 7  Always 0
 44 *	bits 6-0 = X13 - X7
 45 *
 46 *	Byte 3
 47 *	bit 7  Always 0
 48 *	bits 6-0 = X6 - X0
 49 *
 50 *	Byte 4
 51 *	bit 7  Always 0
 52 *	bit 6  B3
 53 *	bit 5  B2
 54 *	bit 4  B1
 55 *	bit 3  B0
 56 *	bit 2  P1 (optional)
 57 *	bit 1  Y15
 58 *	bit 0  Y14
 59 *
 60 *	Byte 5
 61 *	bit 7  Always 0
 62 *	bits 6-0 = Y13 - Y7
 63 *
 64 *	Byte 6
 65 *	bit 7  Always 0
 66 *	bits 6-0 = Y6 - Y0
 67 *
 68 *	Byte 7
 69 *	bit 7 Always 0
 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
 73 *	bit 3  P5
 74 *	bit 2  P4
 75 *	bit 1  P3
 76 *	bit 0  P2
 77 *
 78 *	byte 8 and 9 are optional and present only
 79 *	in tilt mode.
 80 *
 81 *	Byte 8
 82 *	bit 7 Always 0
 83 *	bit 6 Sign of tilt X
 84 *	bit 5  Xt6
 85 *	bit 4  Xt5
 86 *	bit 3  Xt4
 87 *	bit 2  Xt3
 88 *	bit 1  Xt2
 89 *	bit 0  Xt1
 90 *
 91 *	Byte 9
 92 *	bit 7 Always 0
 93 *	bit 6 Sign of tilt Y
 94 *	bit 5  Yt6
 95 *	bit 4  Yt5
 96 *	bit 3  Yt4
 97 *	bit 2  Yt3
 98 *	bit 1  Yt2
 99 *	bit 0  Yt1
100 */
101
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>
111
112MODULE_AUTHOR("Julian Squires <julian@cipht.net>, Hans de Goede <hdegoede@redhat.com>");
113MODULE_DESCRIPTION("Wacom protocol 4 serial tablet driver");
114MODULE_LICENSE("GPL");
115
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#"
120/*
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.
123 */
124
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"
136
137/* Note that this is a protocol 4 packet without tilt information. */
138#define PACKET_LENGTH		7
139#define DATA_SIZE		32
140
141/* flags */
142#define F_COVERS_SCREEN		0x01
143#define F_HAS_STYLUS2		0x02
144#define F_HAS_SCROLLWHEEL	0x04
145
146/* device IDs */
147#define STYLUS_DEVICE_ID	0x02
148#define CURSOR_DEVICE_ID	0x06
149#define ERASER_DEVICE_ID	0x0A
150
151enum { STYLUS = 1, ERASER, CURSOR };
152
153static const struct {
154	int device_id;
155	int input_id;
156} tools[] = {
157	{ 0, 0 },
158	{ STYLUS_DEVICE_ID, BTN_TOOL_PEN },
159	{ ERASER_DEVICE_ID, BTN_TOOL_RUBBER },
160	{ CURSOR_DEVICE_ID, BTN_TOOL_MOUSE },
161};
162
163struct wacom {
164	struct input_dev *dev;
165	struct completion cmd_done;
166	int result;
167	u8 expect;
168	u8 eraser_mask;
169	unsigned int extra_z_bits;
170	unsigned int flags;
171	unsigned int res_x, res_y;
172	unsigned int max_x, max_y;
173	unsigned int tool;
174	unsigned int idx;
175	u8 data[DATA_SIZE];
176	char phys[32];
177};
178
179enum {
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 */
186};
187
188static void wacom_handle_model_response(struct wacom *wacom)
189{
190	int major_v, minor_v, r = 0;
191	char *p;
192
193	p = strrchr(wacom->data, 'V');
194	if (p)
195		r = sscanf(p + 1, "%u.%u", &major_v, &minor_v);
196	if (r != 2)
197		major_v = minor_v = 0;
198
199	switch (wacom->data[2] << 8 | wacom->data[3]) {
200	case MODEL_CINTIQ:	/* UNTESTED */
201	case MODEL_CINTIQ2:
202		if ((wacom->data[2] << 8 | wacom->data[3]) == MODEL_CINTIQ) {
203			wacom->dev->name = "Wacom Cintiq";
204			wacom->dev->id.version = MODEL_CINTIQ;
205		} else {
206			wacom->dev->name = "Wacom Cintiq II";
207			wacom->dev->id.version = MODEL_CINTIQ2;
208		}
209		wacom->res_x = 508;
210		wacom->res_y = 508;
211
212		switch (wacom->data[5] << 8 | wacom->data[6]) {
213		case 0x3731: /* PL-710 */
214			wacom->res_x = 2540;
215			wacom->res_y = 2540;
216			fallthrough;
217		case 0x3535: /* PL-550 */
218		case 0x3830: /* PL-800 */
219			wacom->extra_z_bits = 2;
220		}
221
222		wacom->flags = F_COVERS_SCREEN;
223		break;
224
225	case MODEL_PENPARTNER:
226		wacom->dev->name = "Wacom Penpartner";
227		wacom->dev->id.version = MODEL_PENPARTNER;
228		wacom->res_x = 1000;
229		wacom->res_y = 1000;
230		break;
231
232	case MODEL_GRAPHIRE:
233		wacom->dev->name = "Wacom Graphire";
234		wacom->dev->id.version = MODEL_GRAPHIRE;
235		wacom->res_x = 1016;
236		wacom->res_y = 1016;
237		wacom->max_x = 5103;
238		wacom->max_y = 3711;
239		wacom->extra_z_bits = 2;
240		wacom->eraser_mask = 0x08;
241		wacom->flags = F_HAS_STYLUS2 | F_HAS_SCROLLWHEEL;
242		break;
243
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 */
250		break;
251
252	default:
253		dev_err(&wacom->dev->dev, "Unsupported Wacom model %s\n",
254			wacom->data);
255		wacom->result = -ENODEV;
256		return;
257	}
258
259	dev_info(&wacom->dev->dev, "%s tablet, version %u.%u\n",
260		 wacom->dev->name, major_v, minor_v);
261}
262
263static void wacom_handle_configuration_response(struct wacom *wacom)
264{
265	int r, skip;
266
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);
270	if (r != 5)
271		dev_warn(&wacom->dev->dev, "could not get resolution\n");
272}
273
274static void wacom_handle_coordinates_response(struct wacom *wacom)
275{
276	int r;
277
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);
280	if (r != 2)
281		dev_warn(&wacom->dev->dev, "could not get max coordinates\n");
282}
283
284static void wacom_handle_response(struct wacom *wacom)
285{
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;
290	} else {
291		wacom->result = 0;
292
293		switch (wacom->data[1]) {
294		case '#':
295			wacom_handle_model_response(wacom);
296			break;
297		case 'R':
298			wacom_handle_configuration_response(wacom);
299			break;
300		case 'C':
301			wacom_handle_coordinates_response(wacom);
302			break;
303		}
304	}
305
306	complete(&wacom->cmd_done);
307}
308
309static void wacom_handle_packet(struct wacom *wacom)
310{
311	u8 in_proximity_p, stylus_p, button;
312	unsigned int tool;
313	int x, y, z;
314
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];
320
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);
328	} else {
329		z = -1;
330	}
331
332	if (stylus_p)
333		tool = (button & wacom->eraser_mask) ? ERASER : STYLUS;
334	else
335		tool = CURSOR;
336
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);
340	}
341	wacom->tool = tool;
342
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);
349	if (stylus_p) {
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);
353	} else {
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)
360			z = -z;
361		input_report_rel(wacom->dev, REL_WHEEL, z);
362	}
363	input_sync(wacom->dev);
364}
365
366static void wacom_clear_data_buf(struct wacom *wacom)
367{
368	memset(wacom->data, 0, DATA_SIZE);
369	wacom->idx = 0;
370}
371
372static irqreturn_t wacom_interrupt(struct serio *serio, unsigned char data,
373				   unsigned int flags)
374{
375	struct wacom *wacom = serio_get_drvdata(serio);
376
377	if (data & 0x80)
378		wacom->idx = 0;
379
380	/*
381	 * We're either expecting a carriage return-terminated ASCII
382	 * response string, or a seven-byte packet with the MSB set on
383	 * the first byte.
384	 *
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.
388	 */
389	if (data == '\r' && !(wacom->data[0] & 0x80)) {
390		wacom_handle_response(wacom);
391		wacom_clear_data_buf(wacom);
392		return IRQ_HANDLED;
393	}
394
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);
400	}
401	wacom->data[wacom->idx++] = data;
402
403	if (wacom->idx == PACKET_LENGTH && (wacom->data[0] & 0x80)) {
404		wacom_handle_packet(wacom);
405		wacom_clear_data_buf(wacom);
406	}
407
408	return IRQ_HANDLED;
409}
410
411static void wacom_disconnect(struct serio *serio)
412{
413	struct wacom *wacom = serio_get_drvdata(serio);
414
415	serio_close(serio);
416	serio_set_drvdata(serio, NULL);
417	input_unregister_device(wacom->dev);
418	kfree(wacom);
419}
420
421static int wacom_send(struct serio *serio, const u8 *command)
422{
423	int err = 0;
424
425	for (; !err && *command; command++)
426		err = serio_write(serio, *command);
427
428	return err;
429}
430
431static int wacom_send_setup_string(struct wacom *wacom, struct serio *serio)
432{
433	const u8 *cmd;
434
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;
441		break;
442
443	case MODEL_PENPARTNER:
444		cmd = COMMAND_ENABLE_PRESSURE_MODE
445			COMMAND_START_SENDING_PACKETS;
446		break;
447
448	default:
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
456			COMMAND_Z_FILTER
457			COMMAND_START_SENDING_PACKETS;
458		break;
459	}
460
461	return wacom_send(serio, cmd);
462}
463
464static int wacom_send_and_wait(struct wacom *wacom, struct serio *serio,
465			       const u8 *cmd, const char *desc)
466{
467	int err;
468	unsigned long u;
469
470	wacom->expect = cmd[1];
471	init_completion(&wacom->cmd_done);
472
473	err = wacom_send(serio, cmd);
474	if (err)
475		return err;
476
477	u = wait_for_completion_timeout(&wacom->cmd_done, HZ);
478	if (u == 0) {
479		/* Timeout, process what we've received. */
480		wacom_handle_response(wacom);
481	}
482
483	wacom->expect = 0;
484	return wacom->result;
485}
486
487static int wacom_setup(struct wacom *wacom, struct serio *serio)
488{
489	int err;
490
491	/* Note that setting the link speed is the job of inputattach.
492	 * We assume that reset negotiation has already happened,
493	 * here. */
494	err = wacom_send_and_wait(wacom, serio, REQUEST_MODEL_AND_ROM_VERSION,
495				  "model and version");
496	if (err)
497		return err;
498
499	if (!(wacom->res_x && wacom->res_y)) {
500		err = wacom_send_and_wait(wacom, serio,
501					  REQUEST_CONFIGURATION_STRING,
502					  "configuration string");
503		if (err)
504			return err;
505	}
506
507	if (!(wacom->max_x && wacom->max_y)) {
508		err = wacom_send_and_wait(wacom, serio,
509					  REQUEST_MAX_COORDINATES,
510					  "coordinates string");
511		if (err)
512			return err;
513	}
514
515	return wacom_send_setup_string(wacom, serio);
516}
517
518static int wacom_connect(struct serio *serio, struct serio_driver *drv)
519{
520	struct wacom *wacom;
521	struct input_dev *input_dev;
522	int err = -ENOMEM;
523
524	wacom = kzalloc(sizeof(struct wacom), GFP_KERNEL);
525	input_dev = input_allocate_device();
526	if (!wacom || !input_dev)
527		goto free_device;
528
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;
539
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);
551
552	serio_set_drvdata(serio, wacom);
553
554	err = serio_open(serio, drv);
555	if (err)
556		goto free_device;
557
558	err = wacom_setup(wacom, serio);
559	if (err)
560		goto close_serio;
561
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);
565
566	if (wacom->flags & F_HAS_STYLUS2)
567		__set_bit(BTN_STYLUS2, input_dev->keybit);
568
569	if (wacom->flags & F_HAS_SCROLLWHEEL)
570		__set_bit(REL_WHEEL, input_dev->relbit);
571
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);
578
579	err = input_register_device(wacom->dev);
580	if (err)
581		goto close_serio;
582
583	return 0;
584
585close_serio:
586	serio_close(serio);
587free_device:
588	serio_set_drvdata(serio, NULL);
589	input_free_device(input_dev);
590	kfree(wacom);
591	return err;
592}
593
594static const struct serio_device_id wacom_serio_ids[] = {
595	{
596		.type	= SERIO_RS232,
597		.proto	= SERIO_WACOM_IV,
598		.id	= SERIO_ANY,
599		.extra	= SERIO_ANY,
600	},
601	{ 0 }
602};
603
604MODULE_DEVICE_TABLE(serio, wacom_serio_ids);
605
606static struct serio_driver wacom_drv = {
607	.driver		= {
608		.name	= "wacom_serial4",
609	},
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,
615};
616
617module_serio_driver(wacom_drv);