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_PHYS 42
32 #define W8001_MAX_LENGTH 13
33 #define W8001_LEAD_MASK 0x80
34 #define W8001_LEAD_BYTE 0x80
35 #define W8001_TAB_MASK 0x40
36 #define W8001_TAB_BYTE 0x40
37 /* set in first byte of touch data packets */
38 #define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
39 #define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
41 #define W8001_QUERY_PACKET 0x20
43 #define W8001_CMD_STOP '0'
44 #define W8001_CMD_START '1'
45 #define W8001_CMD_QUERY '*'
46 #define W8001_CMD_TOUCHQUERY '%'
48 /* length of data packets in bytes, depends on device. */
49 #define W8001_PKTLEN_TOUCH93 5
50 #define W8001_PKTLEN_TOUCH9A 7
51 #define W8001_PKTLEN_TPCPEN 9
52 #define W8001_PKTLEN_TPCCTL 11 /* control packet */
53 #define W8001_PKTLEN_TOUCH2FG 13
55 /* resolution in points/mm */
56 #define W8001_PEN_RESOLUTION 100
57 #define W8001_TOUCH_RESOLUTION 10
71 /* touch query reply packet */
72 struct w8001_touch_query
{
81 * Per-touchscreen data.
85 struct input_dev
*pen_dev
;
86 struct input_dev
*touch_dev
;
88 struct completion cmd_done
;
91 unsigned char response_type
;
92 unsigned char response
[W8001_MAX_LENGTH
];
93 unsigned char data
[W8001_MAX_LENGTH
];
94 char phys
[W8001_MAX_PHYS
];
107 static void parse_pen_data(u8
*data
, struct w8001_coord
*coord
)
109 memset(coord
, 0, sizeof(*coord
));
111 coord
->rdy
= data
[0] & 0x20;
112 coord
->tsw
= data
[0] & 0x01;
113 coord
->f1
= data
[0] & 0x02;
114 coord
->f2
= data
[0] & 0x04;
116 coord
->x
= (data
[1] & 0x7F) << 9;
117 coord
->x
|= (data
[2] & 0x7F) << 2;
118 coord
->x
|= (data
[6] & 0x60) >> 5;
120 coord
->y
= (data
[3] & 0x7F) << 9;
121 coord
->y
|= (data
[4] & 0x7F) << 2;
122 coord
->y
|= (data
[6] & 0x18) >> 3;
124 coord
->pen_pressure
= data
[5] & 0x7F;
125 coord
->pen_pressure
|= (data
[6] & 0x07) << 7 ;
127 coord
->tilt_x
= data
[7] & 0x7F;
128 coord
->tilt_y
= data
[8] & 0x7F;
131 static void parse_single_touch(u8
*data
, struct w8001_coord
*coord
)
133 coord
->x
= (data
[1] << 7) | data
[2];
134 coord
->y
= (data
[3] << 7) | data
[4];
135 coord
->tsw
= data
[0] & 0x01;
138 static void scale_touch_coordinates(struct w8001
*w8001
,
139 unsigned int *x
, unsigned int *y
)
141 if (w8001
->max_pen_x
&& w8001
->max_touch_x
)
142 *x
= *x
* w8001
->max_pen_x
/ w8001
->max_touch_x
;
144 if (w8001
->max_pen_y
&& w8001
->max_touch_y
)
145 *y
= *y
* w8001
->max_pen_y
/ w8001
->max_touch_y
;
148 static void parse_multi_touch(struct w8001
*w8001
)
150 struct input_dev
*dev
= w8001
->touch_dev
;
151 unsigned char *data
= w8001
->data
;
156 for (i
= 0; i
< 2; i
++) {
157 bool touch
= data
[0] & (1 << i
);
159 input_mt_slot(dev
, i
);
160 input_mt_report_slot_state(dev
, MT_TOOL_FINGER
, touch
);
162 x
= (data
[6 * i
+ 1] << 7) | data
[6 * i
+ 2];
163 y
= (data
[6 * i
+ 3] << 7) | data
[6 * i
+ 4];
164 /* data[5,6] and [11,12] is finger capacity */
166 /* scale to pen maximum */
167 scale_touch_coordinates(w8001
, &x
, &y
);
169 input_report_abs(dev
, ABS_MT_POSITION_X
, x
);
170 input_report_abs(dev
, ABS_MT_POSITION_Y
, y
);
175 /* emulate single touch events when stylus is out of proximity.
176 * This is to make single touch backward support consistent
177 * across all Wacom single touch devices.
179 if (w8001
->type
!= BTN_TOOL_PEN
&&
180 w8001
->type
!= BTN_TOOL_RUBBER
) {
181 w8001
->type
= count
== 1 ? BTN_TOOL_FINGER
: KEY_RESERVED
;
182 input_mt_report_pointer_emulation(dev
, true);
188 static void parse_touchquery(u8
*data
, struct w8001_touch_query
*query
)
190 memset(query
, 0, sizeof(*query
));
192 query
->panel_res
= data
[1];
193 query
->sensor_id
= data
[2] & 0x7;
194 query
->capacity_res
= data
[7];
196 query
->x
= data
[3] << 9;
197 query
->x
|= data
[4] << 2;
198 query
->x
|= (data
[2] >> 5) & 0x3;
200 query
->y
= data
[5] << 9;
201 query
->y
|= data
[6] << 2;
202 query
->y
|= (data
[2] >> 3) & 0x3;
204 /* Early days' single-finger touch models need the following defaults */
205 if (!query
->x
&& !query
->y
) {
208 if (query
->panel_res
)
209 query
->x
= query
->y
= (1 << query
->panel_res
);
210 query
->panel_res
= W8001_TOUCH_RESOLUTION
;
214 static void report_pen_events(struct w8001
*w8001
, struct w8001_coord
*coord
)
216 struct input_dev
*dev
= w8001
->pen_dev
;
219 * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
220 * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
222 * - if dev is already in proximity and f2 is toggled → pen + side2
223 * - if dev comes into proximity with f2 set → eraser
224 * If f2 disappears after assuming eraser, fake proximity out for
225 * eraser and in for pen.
228 switch (w8001
->type
) {
229 case BTN_TOOL_RUBBER
:
231 input_report_abs(dev
, ABS_PRESSURE
, 0);
232 input_report_key(dev
, BTN_TOUCH
, 0);
233 input_report_key(dev
, BTN_STYLUS
, 0);
234 input_report_key(dev
, BTN_STYLUS2
, 0);
235 input_report_key(dev
, BTN_TOOL_RUBBER
, 0);
237 w8001
->type
= BTN_TOOL_PEN
;
241 case BTN_TOOL_FINGER
:
243 w8001
->type
= coord
->f2
? BTN_TOOL_RUBBER
: BTN_TOOL_PEN
;
247 input_report_key(dev
, BTN_STYLUS2
, coord
->f2
);
251 input_report_abs(dev
, ABS_X
, coord
->x
);
252 input_report_abs(dev
, ABS_Y
, coord
->y
);
253 input_report_abs(dev
, ABS_PRESSURE
, coord
->pen_pressure
);
254 input_report_key(dev
, BTN_TOUCH
, coord
->tsw
);
255 input_report_key(dev
, BTN_STYLUS
, coord
->f1
);
256 input_report_key(dev
, w8001
->type
, coord
->rdy
);
260 w8001
->type
= KEY_RESERVED
;
263 static void report_single_touch(struct w8001
*w8001
, struct w8001_coord
*coord
)
265 struct input_dev
*dev
= w8001
->touch_dev
;
266 unsigned int x
= coord
->x
;
267 unsigned int y
= coord
->y
;
269 /* scale to pen maximum */
270 scale_touch_coordinates(w8001
, &x
, &y
);
272 input_report_abs(dev
, ABS_X
, x
);
273 input_report_abs(dev
, ABS_Y
, y
);
274 input_report_key(dev
, BTN_TOUCH
, coord
->tsw
);
278 w8001
->type
= coord
->tsw
? BTN_TOOL_FINGER
: KEY_RESERVED
;
281 static irqreturn_t
w8001_interrupt(struct serio
*serio
,
282 unsigned char data
, unsigned int flags
)
284 struct w8001
*w8001
= serio_get_drvdata(serio
);
285 struct w8001_coord coord
;
288 w8001
->data
[w8001
->idx
] = data
;
289 switch (w8001
->idx
++) {
291 if ((data
& W8001_LEAD_MASK
) != W8001_LEAD_BYTE
) {
292 pr_debug("w8001: unsynchronized data: 0x%02x\n", data
);
297 case W8001_PKTLEN_TOUCH93
- 1:
298 case W8001_PKTLEN_TOUCH9A
- 1:
299 tmp
= w8001
->data
[0] & W8001_TOUCH_BYTE
;
300 if (tmp
!= W8001_TOUCH_BYTE
)
303 if (w8001
->pktlen
== w8001
->idx
) {
305 if (w8001
->type
!= BTN_TOOL_PEN
&&
306 w8001
->type
!= BTN_TOOL_RUBBER
) {
307 parse_single_touch(w8001
->data
, &coord
);
308 report_single_touch(w8001
, &coord
);
313 /* Pen coordinates packet */
314 case W8001_PKTLEN_TPCPEN
- 1:
315 tmp
= w8001
->data
[0] & W8001_TAB_MASK
;
316 if (unlikely(tmp
== W8001_TAB_BYTE
))
319 tmp
= w8001
->data
[0] & W8001_TOUCH_BYTE
;
320 if (tmp
== W8001_TOUCH_BYTE
)
324 parse_pen_data(w8001
->data
, &coord
);
325 report_pen_events(w8001
, &coord
);
329 case W8001_PKTLEN_TPCCTL
- 1:
330 tmp
= w8001
->data
[0] & W8001_TOUCH_MASK
;
331 if (tmp
== W8001_TOUCH_BYTE
)
335 memcpy(w8001
->response
, w8001
->data
, W8001_MAX_LENGTH
);
336 w8001
->response_type
= W8001_QUERY_PACKET
;
337 complete(&w8001
->cmd_done
);
340 /* 2 finger touch packet */
341 case W8001_PKTLEN_TOUCH2FG
- 1:
343 parse_multi_touch(w8001
);
348 * ThinkPad X60 Tablet PC (pen only device) sometimes
349 * sends invalid data packets that are larger than
350 * W8001_PKTLEN_TPCPEN. Let's start over again.
352 if (!w8001
->touch_dev
&& w8001
->idx
> W8001_PKTLEN_TPCPEN
- 1)
359 static int w8001_command(struct w8001
*w8001
, unsigned char command
,
364 w8001
->response_type
= 0;
365 init_completion(&w8001
->cmd_done
);
367 rc
= serio_write(w8001
->serio
, command
);
368 if (rc
== 0 && wait_response
) {
370 wait_for_completion_timeout(&w8001
->cmd_done
, HZ
);
371 if (w8001
->response_type
!= W8001_QUERY_PACKET
)
378 static int w8001_open(struct input_dev
*dev
)
380 struct w8001
*w8001
= input_get_drvdata(dev
);
383 err
= mutex_lock_interruptible(&w8001
->mutex
);
387 if (w8001
->open_count
++ == 0) {
388 err
= w8001_command(w8001
, W8001_CMD_START
, false);
393 mutex_unlock(&w8001
->mutex
);
397 static void w8001_close(struct input_dev
*dev
)
399 struct w8001
*w8001
= input_get_drvdata(dev
);
401 mutex_lock(&w8001
->mutex
);
403 if (--w8001
->open_count
== 0)
404 w8001_command(w8001
, W8001_CMD_STOP
, false);
406 mutex_unlock(&w8001
->mutex
);
409 static int w8001_detect(struct w8001
*w8001
)
413 error
= w8001_command(w8001
, W8001_CMD_STOP
, false);
417 msleep(250); /* wait 250ms before querying the device */
422 static int w8001_setup_pen(struct w8001
*w8001
, char *basename
,
425 struct input_dev
*dev
= w8001
->pen_dev
;
426 struct w8001_coord coord
;
430 error
= w8001_command(w8001
, W8001_CMD_QUERY
, true);
434 __set_bit(EV_KEY
, dev
->evbit
);
435 __set_bit(EV_ABS
, dev
->evbit
);
436 __set_bit(BTN_TOUCH
, dev
->keybit
);
437 __set_bit(BTN_TOOL_PEN
, dev
->keybit
);
438 __set_bit(BTN_TOOL_RUBBER
, dev
->keybit
);
439 __set_bit(BTN_STYLUS
, dev
->keybit
);
440 __set_bit(BTN_STYLUS2
, dev
->keybit
);
441 __set_bit(INPUT_PROP_DIRECT
, dev
->propbit
);
443 parse_pen_data(w8001
->response
, &coord
);
444 w8001
->max_pen_x
= coord
.x
;
445 w8001
->max_pen_y
= coord
.y
;
447 input_set_abs_params(dev
, ABS_X
, 0, coord
.x
, 0, 0);
448 input_set_abs_params(dev
, ABS_Y
, 0, coord
.y
, 0, 0);
449 input_abs_set_res(dev
, ABS_X
, W8001_PEN_RESOLUTION
);
450 input_abs_set_res(dev
, ABS_Y
, W8001_PEN_RESOLUTION
);
451 input_set_abs_params(dev
, ABS_PRESSURE
, 0, coord
.pen_pressure
, 0, 0);
452 if (coord
.tilt_x
&& coord
.tilt_y
) {
453 input_set_abs_params(dev
, ABS_TILT_X
, 0, coord
.tilt_x
, 0, 0);
454 input_set_abs_params(dev
, ABS_TILT_Y
, 0, coord
.tilt_y
, 0, 0);
458 strlcat(basename
, " Penabled", basename_sz
);
463 static int w8001_setup_touch(struct w8001
*w8001
, char *basename
,
466 struct input_dev
*dev
= w8001
->touch_dev
;
467 struct w8001_touch_query touch
;
472 error
= w8001_command(w8001
, W8001_CMD_TOUCHQUERY
, true);
476 * Some non-touch devices may reply to the touch query. But their
477 * second byte is empty, which indicates touch is not supported.
479 if (!w8001
->response
[1])
482 __set_bit(EV_KEY
, dev
->evbit
);
483 __set_bit(EV_ABS
, dev
->evbit
);
484 __set_bit(BTN_TOUCH
, dev
->keybit
);
485 __set_bit(INPUT_PROP_DIRECT
, dev
->propbit
);
487 parse_touchquery(w8001
->response
, &touch
);
488 w8001
->max_touch_x
= touch
.x
;
489 w8001
->max_touch_y
= touch
.y
;
491 if (w8001
->max_pen_x
&& w8001
->max_pen_y
) {
492 /* if pen is supported scale to pen maximum */
493 touch
.x
= w8001
->max_pen_x
;
494 touch
.y
= w8001
->max_pen_y
;
495 touch
.panel_res
= W8001_PEN_RESOLUTION
;
498 input_set_abs_params(dev
, ABS_X
, 0, touch
.x
, 0, 0);
499 input_set_abs_params(dev
, ABS_Y
, 0, touch
.y
, 0, 0);
500 input_abs_set_res(dev
, ABS_X
, touch
.panel_res
);
501 input_abs_set_res(dev
, ABS_Y
, touch
.panel_res
);
503 switch (touch
.sensor_id
) {
506 w8001
->pktlen
= W8001_PKTLEN_TOUCH93
;
508 strlcat(basename
, " 1FG", basename_sz
);
514 w8001
->pktlen
= W8001_PKTLEN_TOUCH9A
;
515 strlcat(basename
, " 1FG", basename_sz
);
520 w8001
->pktlen
= W8001_PKTLEN_TOUCH2FG
;
522 __set_bit(BTN_TOOL_DOUBLETAP
, dev
->keybit
);
523 error
= input_mt_init_slots(dev
, 2, 0);
525 dev_err(&w8001
->serio
->dev
,
526 "failed to initialize MT slots: %d\n", error
);
530 input_set_abs_params(dev
, ABS_MT_POSITION_X
,
532 input_set_abs_params(dev
, ABS_MT_POSITION_Y
,
534 input_set_abs_params(dev
, ABS_MT_TOOL_TYPE
,
535 0, MT_TOOL_MAX
, 0, 0);
536 input_abs_set_res(dev
, ABS_MT_POSITION_X
, touch
.panel_res
);
537 input_abs_set_res(dev
, ABS_MT_POSITION_Y
, touch
.panel_res
);
539 strlcat(basename
, " 2FG", basename_sz
);
540 if (w8001
->max_pen_x
&& w8001
->max_pen_y
)
547 strlcat(basename
, " Touchscreen", basename_sz
);
552 static void w8001_set_devdata(struct input_dev
*dev
, struct w8001
*w8001
,
555 dev
->phys
= w8001
->phys
;
556 dev
->id
.bustype
= BUS_RS232
;
557 dev
->id
.product
= w8001
->id
;
558 dev
->id
.vendor
= 0x056a;
559 dev
->id
.version
= 0x0100;
560 dev
->open
= w8001_open
;
561 dev
->close
= w8001_close
;
563 dev
->dev
.parent
= &serio
->dev
;
565 input_set_drvdata(dev
, w8001
);
569 * w8001_disconnect() is the opposite of w8001_connect()
572 static void w8001_disconnect(struct serio
*serio
)
574 struct w8001
*w8001
= serio_get_drvdata(serio
);
579 input_unregister_device(w8001
->pen_dev
);
580 if (w8001
->touch_dev
)
581 input_unregister_device(w8001
->touch_dev
);
584 serio_set_drvdata(serio
, NULL
);
588 * w8001_connect() is the routine that is called when someone adds a
589 * new serio device that supports the w8001 protocol and registers it as
593 static int w8001_connect(struct serio
*serio
, struct serio_driver
*drv
)
596 struct input_dev
*input_dev_pen
;
597 struct input_dev
*input_dev_touch
;
599 int err
, err_pen
, err_touch
;
601 w8001
= kzalloc(sizeof(struct w8001
), GFP_KERNEL
);
602 input_dev_pen
= input_allocate_device();
603 input_dev_touch
= input_allocate_device();
604 if (!w8001
|| !input_dev_pen
|| !input_dev_touch
) {
609 w8001
->serio
= serio
;
610 w8001
->pen_dev
= input_dev_pen
;
611 w8001
->touch_dev
= input_dev_touch
;
612 mutex_init(&w8001
->mutex
);
613 init_completion(&w8001
->cmd_done
);
614 snprintf(w8001
->phys
, sizeof(w8001
->phys
), "%s/input0", serio
->phys
);
616 serio_set_drvdata(serio
, w8001
);
617 err
= serio_open(serio
, drv
);
621 err
= w8001_detect(w8001
);
625 /* For backwards-compatibility we compose the basename based on
626 * capabilities and then just append the tool type
628 strlcpy(basename
, "Wacom Serial", sizeof(basename
));
630 err_pen
= w8001_setup_pen(w8001
, basename
, sizeof(basename
));
631 err_touch
= w8001_setup_touch(w8001
, basename
, sizeof(basename
));
632 if (err_pen
&& err_touch
) {
638 strlcpy(w8001
->pen_name
, basename
, sizeof(w8001
->pen_name
));
639 strlcat(w8001
->pen_name
, " Pen", sizeof(w8001
->pen_name
));
640 input_dev_pen
->name
= w8001
->pen_name
;
642 w8001_set_devdata(input_dev_pen
, w8001
, serio
);
644 err
= input_register_device(w8001
->pen_dev
);
648 input_free_device(input_dev_pen
);
649 input_dev_pen
= NULL
;
650 w8001
->pen_dev
= NULL
;
654 strlcpy(w8001
->touch_name
, basename
, sizeof(w8001
->touch_name
));
655 strlcat(w8001
->touch_name
, " Finger",
656 sizeof(w8001
->touch_name
));
657 input_dev_touch
->name
= w8001
->touch_name
;
659 w8001_set_devdata(input_dev_touch
, w8001
, serio
);
661 err
= input_register_device(w8001
->touch_dev
);
665 input_free_device(input_dev_touch
);
666 input_dev_touch
= NULL
;
667 w8001
->touch_dev
= NULL
;
674 input_unregister_device(w8001
->pen_dev
);
678 serio_set_drvdata(serio
, NULL
);
680 input_free_device(input_dev_pen
);
681 input_free_device(input_dev_touch
);
686 static const struct serio_device_id w8001_serio_ids
[] = {
689 .proto
= SERIO_W8001
,
696 MODULE_DEVICE_TABLE(serio
, w8001_serio_ids
);
698 static struct serio_driver w8001_drv
= {
702 .description
= DRIVER_DESC
,
703 .id_table
= w8001_serio_ids
,
704 .interrupt
= w8001_interrupt
,
705 .connect
= w8001_connect
,
706 .disconnect
= w8001_disconnect
,
709 module_serio_driver(w8001_drv
);