2 * Wacom W8001 penabled serial touchscreen driver
4 * Copyright (c) 2008 Jaya Kumar
5 * Copyright (c) 2010 Red Hat, Inc.
6 * Copyright (c) 2010 - 2011 Ping Cheng, Wacom. <pingc@wacom.com>
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive for
12 * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
15 #include <linux/errno.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/slab.h>
19 #include <linux/input/mt.h>
20 #include <linux/serio.h>
21 #include <linux/ctype.h>
22 #include <linux/delay.h>
24 #define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
26 MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
27 MODULE_DESCRIPTION(DRIVER_DESC);
28 MODULE_LICENSE("GPL");
30 #define W8001_MAX_LENGTH 13
31 #define W8001_LEAD_MASK 0x80
32 #define W8001_LEAD_BYTE 0x80
33 #define W8001_TAB_MASK 0x40
34 #define W8001_TAB_BYTE 0x40
35 /* set in first byte of touch data packets */
36 #define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
37 #define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
39 #define W8001_QUERY_PACKET 0x20
41 #define W8001_CMD_STOP '0'
42 #define W8001_CMD_START '1'
43 #define W8001_CMD_QUERY '*'
44 #define W8001_CMD_TOUCHQUERY '%'
46 /* length of data packets in bytes, depends on device. */
47 #define W8001_PKTLEN_TOUCH93 5
48 #define W8001_PKTLEN_TOUCH9A 7
49 #define W8001_PKTLEN_TPCPEN 9
50 #define W8001_PKTLEN_TPCCTL 11 /* control packet */
51 #define W8001_PKTLEN_TOUCH2FG 13
53 /* resolution in points/mm */
54 #define W8001_PEN_RESOLUTION 100
55 #define W8001_TOUCH_RESOLUTION 10
69 /* touch query reply packet */
70 struct w8001_touch_query {
79 * Per-touchscreen data.
83 struct input_dev *pen_dev;
84 struct input_dev *touch_dev;
86 struct completion cmd_done;
89 unsigned char response_type;
90 unsigned char response[W8001_MAX_LENGTH];
91 unsigned char data[W8001_MAX_LENGTH];
105 static void parse_pen_data(u8 *data, struct w8001_coord *coord)
107 memset(coord, 0, sizeof(*coord));
109 coord->rdy = data[0] & 0x20;
110 coord->tsw = data[0] & 0x01;
111 coord->f1 = data[0] & 0x02;
112 coord->f2 = data[0] & 0x04;
114 coord->x = (data[1] & 0x7F) << 9;
115 coord->x |= (data[2] & 0x7F) << 2;
116 coord->x |= (data[6] & 0x60) >> 5;
118 coord->y = (data[3] & 0x7F) << 9;
119 coord->y |= (data[4] & 0x7F) << 2;
120 coord->y |= (data[6] & 0x18) >> 3;
122 coord->pen_pressure = data[5] & 0x7F;
123 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
125 coord->tilt_x = data[7] & 0x7F;
126 coord->tilt_y = data[8] & 0x7F;
129 static void parse_single_touch(u8 *data, struct w8001_coord *coord)
131 coord->x = (data[1] << 7) | data[2];
132 coord->y = (data[3] << 7) | data[4];
133 coord->tsw = data[0] & 0x01;
136 static void scale_touch_coordinates(struct w8001 *w8001,
137 unsigned int *x, unsigned int *y)
139 if (w8001->max_pen_x && w8001->max_touch_x)
140 *x = *x * w8001->max_pen_x / w8001->max_touch_x;
142 if (w8001->max_pen_y && w8001->max_touch_y)
143 *y = *y * w8001->max_pen_y / w8001->max_touch_y;
146 static void parse_multi_touch(struct w8001 *w8001)
148 struct input_dev *dev = w8001->touch_dev;
149 unsigned char *data = w8001->data;
154 for (i = 0; i < 2; i++) {
155 bool touch = data[0] & (1 << i);
157 input_mt_slot(dev, i);
159 x = (data[6 * i + 1] << 7) | data[6 * i + 2];
160 y = (data[6 * i + 3] << 7) | data[6 * i + 4];
161 /* data[5,6] and [11,12] is finger capacity */
163 /* scale to pen maximum */
164 scale_touch_coordinates(w8001, &x, &y);
166 input_report_abs(dev, ABS_MT_POSITION_X, x);
167 input_report_abs(dev, ABS_MT_POSITION_Y, y);
172 /* emulate single touch events when stylus is out of proximity.
173 * This is to make single touch backward support consistent
174 * across all Wacom single touch devices.
176 if (w8001->type != BTN_TOOL_PEN &&
177 w8001->type != BTN_TOOL_RUBBER) {
178 w8001->type = count == 1 ? BTN_TOOL_FINGER : KEY_RESERVED;
179 input_mt_report_pointer_emulation(dev, true);
185 static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
187 memset(query, 0, sizeof(*query));
189 query->panel_res = data[1];
190 query->sensor_id = data[2] & 0x7;
191 query->capacity_res = data[7];
193 query->x = data[3] << 9;
194 query->x |= data[4] << 2;
195 query->x |= (data[2] >> 5) & 0x3;
197 query->y = data[5] << 9;
198 query->y |= data[6] << 2;
199 query->y |= (data[2] >> 3) & 0x3;
201 /* Early days' single-finger touch models need the following defaults */
202 if (!query->x && !query->y) {
205 if (query->panel_res)
206 query->x = query->y = (1 << query->panel_res);
207 query->panel_res = W8001_TOUCH_RESOLUTION;
211 static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
213 struct input_dev *dev = w8001->pen_dev;
216 * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
217 * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
219 * - if dev is already in proximity and f2 is toggled → pen + side2
220 * - if dev comes into proximity with f2 set → eraser
221 * If f2 disappears after assuming eraser, fake proximity out for
222 * eraser and in for pen.
225 switch (w8001->type) {
226 case BTN_TOOL_RUBBER:
228 input_report_abs(dev, ABS_PRESSURE, 0);
229 input_report_key(dev, BTN_TOUCH, 0);
230 input_report_key(dev, BTN_STYLUS, 0);
231 input_report_key(dev, BTN_STYLUS2, 0);
232 input_report_key(dev, BTN_TOOL_RUBBER, 0);
234 w8001->type = BTN_TOOL_PEN;
238 case BTN_TOOL_FINGER:
240 w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
244 input_report_key(dev, BTN_STYLUS2, coord->f2);
248 input_report_abs(dev, ABS_X, coord->x);
249 input_report_abs(dev, ABS_Y, coord->y);
250 input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure);
251 input_report_key(dev, BTN_TOUCH, coord->tsw);
252 input_report_key(dev, BTN_STYLUS, coord->f1);
253 input_report_key(dev, w8001->type, coord->rdy);
257 w8001->type = KEY_RESERVED;
260 static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord)
262 struct input_dev *dev = w8001->touch_dev;
263 unsigned int x = coord->x;
264 unsigned int y = coord->y;
266 /* scale to pen maximum */
267 scale_touch_coordinates(w8001, &x, &y);
269 input_report_abs(dev, ABS_X, x);
270 input_report_abs(dev, ABS_Y, y);
271 input_report_key(dev, BTN_TOUCH, coord->tsw);
275 w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED;
278 static irqreturn_t w8001_interrupt(struct serio *serio,
279 unsigned char data, unsigned int flags)
281 struct w8001 *w8001 = serio_get_drvdata(serio);
282 struct w8001_coord coord;
285 w8001->data[w8001->idx] = data;
286 switch (w8001->idx++) {
288 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
289 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
294 case W8001_PKTLEN_TOUCH93 - 1:
295 case W8001_PKTLEN_TOUCH9A - 1:
296 tmp = w8001->data[0] & W8001_TOUCH_BYTE;
297 if (tmp != W8001_TOUCH_BYTE)
300 if (w8001->pktlen == w8001->idx) {
302 if (w8001->type != BTN_TOOL_PEN &&
303 w8001->type != BTN_TOOL_RUBBER) {
304 parse_single_touch(w8001->data, &coord);
305 report_single_touch(w8001, &coord);
310 /* Pen coordinates packet */
311 case W8001_PKTLEN_TPCPEN - 1:
312 tmp = w8001->data[0] & W8001_TAB_MASK;
313 if (unlikely(tmp == W8001_TAB_BYTE))
316 tmp = w8001->data[0] & W8001_TOUCH_BYTE;
317 if (tmp == W8001_TOUCH_BYTE)
321 parse_pen_data(w8001->data, &coord);
322 report_pen_events(w8001, &coord);
326 case W8001_PKTLEN_TPCCTL - 1:
327 tmp = w8001->data[0] & W8001_TOUCH_MASK;
328 if (tmp == W8001_TOUCH_BYTE)
332 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
333 w8001->response_type = W8001_QUERY_PACKET;
334 complete(&w8001->cmd_done);
337 /* 2 finger touch packet */
338 case W8001_PKTLEN_TOUCH2FG - 1:
340 parse_multi_touch(w8001);
345 * ThinkPad X60 Tablet PC (pen only device) sometimes
346 * sends invalid data packets that are larger than
347 * W8001_PKTLEN_TPCPEN. Let's start over again.
349 if (!w8001->touch_dev && w8001->idx > W8001_PKTLEN_TPCPEN - 1)
356 static int w8001_command(struct w8001 *w8001, unsigned char command,
361 w8001->response_type = 0;
362 init_completion(&w8001->cmd_done);
364 rc = serio_write(w8001->serio, command);
365 if (rc == 0 && wait_response) {
367 wait_for_completion_timeout(&w8001->cmd_done, HZ);
368 if (w8001->response_type != W8001_QUERY_PACKET)
375 static int w8001_open(struct input_dev *dev)
377 struct w8001 *w8001 = input_get_drvdata(dev);
380 err = mutex_lock_interruptible(&w8001->mutex);
384 if (w8001->open_count++ == 0) {
385 err = w8001_command(w8001, W8001_CMD_START, false);
390 mutex_unlock(&w8001->mutex);
394 static void w8001_close(struct input_dev *dev)
396 struct w8001 *w8001 = input_get_drvdata(dev);
398 mutex_lock(&w8001->mutex);
400 if (--w8001->open_count == 0)
401 w8001_command(w8001, W8001_CMD_STOP, false);
403 mutex_unlock(&w8001->mutex);
406 static int w8001_detect(struct w8001 *w8001)
410 error = w8001_command(w8001, W8001_CMD_STOP, false);
414 msleep(250); /* wait 250ms before querying the device */
419 static int w8001_setup_pen(struct w8001 *w8001, char *basename,
422 struct input_dev *dev = w8001->pen_dev;
423 struct w8001_coord coord;
427 error = w8001_command(w8001, W8001_CMD_QUERY, true);
431 __set_bit(EV_KEY, dev->evbit);
432 __set_bit(EV_ABS, dev->evbit);
433 __set_bit(BTN_TOUCH, dev->keybit);
434 __set_bit(BTN_TOOL_PEN, dev->keybit);
435 __set_bit(BTN_TOOL_RUBBER, dev->keybit);
436 __set_bit(BTN_STYLUS, dev->keybit);
437 __set_bit(BTN_STYLUS2, dev->keybit);
438 __set_bit(INPUT_PROP_DIRECT, dev->propbit);
440 parse_pen_data(w8001->response, &coord);
441 w8001->max_pen_x = coord.x;
442 w8001->max_pen_y = coord.y;
444 input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
445 input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
446 input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION);
447 input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION);
448 input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
449 if (coord.tilt_x && coord.tilt_y) {
450 input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
451 input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
455 strlcat(basename, " Penabled", basename_sz);
460 static int w8001_setup_touch(struct w8001 *w8001, char *basename,
463 struct input_dev *dev = w8001->touch_dev;
464 struct w8001_touch_query touch;
469 error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
473 * Some non-touch devices may reply to the touch query. But their
474 * second byte is empty, which indicates touch is not supported.
476 if (!w8001->response[1])
479 __set_bit(EV_KEY, dev->evbit);
480 __set_bit(EV_ABS, dev->evbit);
481 __set_bit(BTN_TOUCH, dev->keybit);
482 __set_bit(INPUT_PROP_DIRECT, dev->propbit);
484 parse_touchquery(w8001->response, &touch);
485 w8001->max_touch_x = touch.x;
486 w8001->max_touch_y = touch.y;
488 if (w8001->max_pen_x && w8001->max_pen_y) {
489 /* if pen is supported scale to pen maximum */
490 touch.x = w8001->max_pen_x;
491 touch.y = w8001->max_pen_y;
492 touch.panel_res = W8001_PEN_RESOLUTION;
495 input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0);
496 input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0);
497 input_abs_set_res(dev, ABS_X, touch.panel_res);
498 input_abs_set_res(dev, ABS_Y, touch.panel_res);
500 switch (touch.sensor_id) {
503 w8001->pktlen = W8001_PKTLEN_TOUCH93;
505 strlcat(basename, " 1FG", basename_sz);
511 w8001->pktlen = W8001_PKTLEN_TOUCH9A;
512 strlcat(basename, " 1FG", basename_sz);
517 w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
519 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
520 input_mt_init_slots(dev, 2, 0);
521 input_set_abs_params(dev, ABS_MT_POSITION_X,
523 input_set_abs_params(dev, ABS_MT_POSITION_Y,
526 strlcat(basename, " 2FG", basename_sz);
527 if (w8001->max_pen_x && w8001->max_pen_y)
534 strlcat(basename, " Touchscreen", basename_sz);
539 static void w8001_set_devdata(struct input_dev *dev, struct w8001 *w8001,
542 dev->phys = w8001->phys;
543 dev->id.bustype = BUS_RS232;
544 dev->id.product = w8001->id;
545 dev->id.vendor = 0x056a;
546 dev->id.version = 0x0100;
547 dev->open = w8001_open;
548 dev->close = w8001_close;
550 dev->dev.parent = &serio->dev;
552 input_set_drvdata(dev, w8001);
556 * w8001_disconnect() is the opposite of w8001_connect()
559 static void w8001_disconnect(struct serio *serio)
561 struct w8001 *w8001 = serio_get_drvdata(serio);
566 input_unregister_device(w8001->pen_dev);
567 if (w8001->touch_dev)
568 input_unregister_device(w8001->touch_dev);
571 serio_set_drvdata(serio, NULL);
575 * w8001_connect() is the routine that is called when someone adds a
576 * new serio device that supports the w8001 protocol and registers it as
580 static int w8001_connect(struct serio *serio, struct serio_driver *drv)
583 struct input_dev *input_dev_pen;
584 struct input_dev *input_dev_touch;
586 int err, err_pen, err_touch;
588 w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
589 input_dev_pen = input_allocate_device();
590 input_dev_touch = input_allocate_device();
591 if (!w8001 || !input_dev_pen || !input_dev_touch) {
596 w8001->serio = serio;
597 w8001->pen_dev = input_dev_pen;
598 w8001->touch_dev = input_dev_touch;
599 mutex_init(&w8001->mutex);
600 init_completion(&w8001->cmd_done);
601 snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
603 serio_set_drvdata(serio, w8001);
604 err = serio_open(serio, drv);
608 err = w8001_detect(w8001);
612 /* For backwards-compatibility we compose the basename based on
613 * capabilities and then just append the tool type
615 strlcpy(basename, "Wacom Serial", sizeof(basename));
617 err_pen = w8001_setup_pen(w8001, basename, sizeof(basename));
618 err_touch = w8001_setup_touch(w8001, basename, sizeof(basename));
619 if (err_pen && err_touch) {
625 strlcpy(w8001->pen_name, basename, sizeof(w8001->pen_name));
626 strlcat(w8001->pen_name, " Pen", sizeof(w8001->pen_name));
627 input_dev_pen->name = w8001->pen_name;
629 w8001_set_devdata(input_dev_pen, w8001, serio);
631 err = input_register_device(w8001->pen_dev);
635 input_free_device(input_dev_pen);
636 input_dev_pen = NULL;
637 w8001->pen_dev = NULL;
641 strlcpy(w8001->touch_name, basename, sizeof(w8001->touch_name));
642 strlcat(w8001->touch_name, " Finger",
643 sizeof(w8001->touch_name));
644 input_dev_touch->name = w8001->touch_name;
646 w8001_set_devdata(input_dev_touch, w8001, serio);
648 err = input_register_device(w8001->touch_dev);
652 input_free_device(input_dev_touch);
653 input_dev_touch = NULL;
654 w8001->touch_dev = NULL;
661 input_unregister_device(w8001->pen_dev);
665 serio_set_drvdata(serio, NULL);
667 input_free_device(input_dev_pen);
668 input_free_device(input_dev_touch);
673 static struct serio_device_id w8001_serio_ids[] = {
676 .proto = SERIO_W8001,
683 MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
685 static struct serio_driver w8001_drv = {
689 .description = DRIVER_DESC,
690 .id_table = w8001_serio_ids,
691 .interrupt = w8001_interrupt,
692 .connect = w8001_connect,
693 .disconnect = w8001_disconnect,
696 module_serio_driver(w8001_drv);