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/init.h>
22 #include <linux/ctype.h>
23 #include <linux/delay.h>
25 #define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
27 MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
28 MODULE_DESCRIPTION(DRIVER_DESC
);
29 MODULE_LICENSE("GPL");
31 #define W8001_MAX_LENGTH 11
32 #define W8001_LEAD_MASK 0x80
33 #define W8001_LEAD_BYTE 0x80
34 #define W8001_TAB_MASK 0x40
35 #define W8001_TAB_BYTE 0x40
36 /* set in first byte of touch data packets */
37 #define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
38 #define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
40 #define W8001_QUERY_PACKET 0x20
42 #define W8001_CMD_STOP '0'
43 #define W8001_CMD_START '1'
44 #define W8001_CMD_QUERY '*'
45 #define W8001_CMD_TOUCHQUERY '%'
47 /* length of data packets in bytes, depends on device. */
48 #define W8001_PKTLEN_TOUCH93 5
49 #define W8001_PKTLEN_TOUCH9A 7
50 #define W8001_PKTLEN_TPCPEN 9
51 #define W8001_PKTLEN_TPCCTL 11 /* control packet */
52 #define W8001_PKTLEN_TOUCH2FG 13
66 /* touch query reply packet */
67 struct w8001_touch_query
{
76 * Per-touchscreen data.
80 struct input_dev
*dev
;
82 struct completion cmd_done
;
85 unsigned char response_type
;
86 unsigned char response
[W8001_MAX_LENGTH
];
87 unsigned char data
[W8001_MAX_LENGTH
];
98 static void parse_pen_data(u8
*data
, struct w8001_coord
*coord
)
100 memset(coord
, 0, sizeof(*coord
));
102 coord
->rdy
= data
[0] & 0x20;
103 coord
->tsw
= data
[0] & 0x01;
104 coord
->f1
= data
[0] & 0x02;
105 coord
->f2
= data
[0] & 0x04;
107 coord
->x
= (data
[1] & 0x7F) << 9;
108 coord
->x
|= (data
[2] & 0x7F) << 2;
109 coord
->x
|= (data
[6] & 0x60) >> 5;
111 coord
->y
= (data
[3] & 0x7F) << 9;
112 coord
->y
|= (data
[4] & 0x7F) << 2;
113 coord
->y
|= (data
[6] & 0x18) >> 3;
115 coord
->pen_pressure
= data
[5] & 0x7F;
116 coord
->pen_pressure
|= (data
[6] & 0x07) << 7 ;
118 coord
->tilt_x
= data
[7] & 0x7F;
119 coord
->tilt_y
= data
[8] & 0x7F;
122 static void parse_single_touch(u8
*data
, struct w8001_coord
*coord
)
124 coord
->x
= (data
[1] << 7) | data
[2];
125 coord
->y
= (data
[3] << 7) | data
[4];
126 coord
->tsw
= data
[0] & 0x01;
129 static void scale_touch_coordinates(struct w8001
*w8001
,
130 unsigned int *x
, unsigned int *y
)
132 if (w8001
->max_pen_x
&& w8001
->max_touch_x
)
133 *x
= *x
* w8001
->max_pen_x
/ w8001
->max_touch_x
;
135 if (w8001
->max_pen_y
&& w8001
->max_touch_y
)
136 *y
= *y
* w8001
->max_pen_y
/ w8001
->max_touch_y
;
139 static void parse_multi_touch(struct w8001
*w8001
)
141 struct input_dev
*dev
= w8001
->dev
;
142 unsigned char *data
= w8001
->data
;
147 for (i
= 0; i
< 2; i
++) {
148 bool touch
= data
[0] & (1 << i
);
150 input_mt_slot(dev
, i
);
151 input_mt_report_slot_state(dev
, MT_TOOL_FINGER
, touch
);
153 x
= (data
[6 * i
+ 1] << 7) | data
[6 * i
+ 2];
154 y
= (data
[6 * i
+ 3] << 7) | data
[6 * i
+ 4];
155 /* data[5,6] and [11,12] is finger capacity */
157 /* scale to pen maximum */
158 scale_touch_coordinates(w8001
, &x
, &y
);
160 input_report_abs(dev
, ABS_MT_POSITION_X
, x
);
161 input_report_abs(dev
, ABS_MT_POSITION_Y
, y
);
166 /* emulate single touch events when stylus is out of proximity.
167 * This is to make single touch backward support consistent
168 * across all Wacom single touch devices.
170 if (w8001
->type
!= BTN_TOOL_PEN
&&
171 w8001
->type
!= BTN_TOOL_RUBBER
) {
172 w8001
->type
= count
== 1 ? BTN_TOOL_FINGER
: KEY_RESERVED
;
173 input_mt_report_pointer_emulation(dev
, true);
179 static void parse_touchquery(u8
*data
, struct w8001_touch_query
*query
)
181 memset(query
, 0, sizeof(*query
));
183 query
->panel_res
= data
[1];
184 query
->sensor_id
= data
[2] & 0x7;
185 query
->capacity_res
= data
[7];
187 query
->x
= data
[3] << 9;
188 query
->x
|= data
[4] << 2;
189 query
->x
|= (data
[2] >> 5) & 0x3;
191 query
->y
= data
[5] << 9;
192 query
->y
|= data
[6] << 2;
193 query
->y
|= (data
[2] >> 3) & 0x3;
195 /* Early days' single-finger touch models need the following defaults */
196 if (!query
->x
&& !query
->y
) {
199 if (query
->panel_res
)
200 query
->x
= query
->y
= (1 << query
->panel_res
);
201 query
->panel_res
= 10;
205 static void report_pen_events(struct w8001
*w8001
, struct w8001_coord
*coord
)
207 struct input_dev
*dev
= w8001
->dev
;
210 * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
211 * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
213 * - if dev is already in proximity and f2 is toggled → pen + side2
214 * - if dev comes into proximity with f2 set → eraser
215 * If f2 disappears after assuming eraser, fake proximity out for
216 * eraser and in for pen.
219 switch (w8001
->type
) {
220 case BTN_TOOL_RUBBER
:
222 input_report_abs(dev
, ABS_PRESSURE
, 0);
223 input_report_key(dev
, BTN_TOUCH
, 0);
224 input_report_key(dev
, BTN_STYLUS
, 0);
225 input_report_key(dev
, BTN_STYLUS2
, 0);
226 input_report_key(dev
, BTN_TOOL_RUBBER
, 0);
228 w8001
->type
= BTN_TOOL_PEN
;
232 case BTN_TOOL_FINGER
:
233 input_report_key(dev
, BTN_TOUCH
, 0);
234 input_report_key(dev
, BTN_TOOL_FINGER
, 0);
239 w8001
->type
= coord
->f2
? BTN_TOOL_RUBBER
: BTN_TOOL_PEN
;
243 input_report_key(dev
, BTN_STYLUS2
, coord
->f2
);
247 input_report_abs(dev
, ABS_X
, coord
->x
);
248 input_report_abs(dev
, ABS_Y
, coord
->y
);
249 input_report_abs(dev
, ABS_PRESSURE
, coord
->pen_pressure
);
250 input_report_key(dev
, BTN_TOUCH
, coord
->tsw
);
251 input_report_key(dev
, BTN_STYLUS
, coord
->f1
);
252 input_report_key(dev
, w8001
->type
, coord
->rdy
);
256 w8001
->type
= KEY_RESERVED
;
259 static void report_single_touch(struct w8001
*w8001
, struct w8001_coord
*coord
)
261 struct input_dev
*dev
= w8001
->dev
;
262 unsigned int x
= coord
->x
;
263 unsigned int y
= coord
->y
;
265 /* scale to pen maximum */
266 scale_touch_coordinates(w8001
, &x
, &y
);
268 input_report_abs(dev
, ABS_X
, x
);
269 input_report_abs(dev
, ABS_Y
, y
);
270 input_report_key(dev
, BTN_TOUCH
, coord
->tsw
);
271 input_report_key(dev
, BTN_TOOL_FINGER
, 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
);
347 static int w8001_command(struct w8001
*w8001
, unsigned char command
,
352 w8001
->response_type
= 0;
353 init_completion(&w8001
->cmd_done
);
355 rc
= serio_write(w8001
->serio
, command
);
356 if (rc
== 0 && wait_response
) {
358 wait_for_completion_timeout(&w8001
->cmd_done
, HZ
);
359 if (w8001
->response_type
!= W8001_QUERY_PACKET
)
366 static int w8001_setup(struct w8001
*w8001
)
368 struct input_dev
*dev
= w8001
->dev
;
369 struct w8001_coord coord
;
370 struct w8001_touch_query touch
;
373 error
= w8001_command(w8001
, W8001_CMD_STOP
, false);
377 msleep(250); /* wait 250ms before querying the device */
379 dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
380 strlcat(w8001
->name
, "Wacom Serial", sizeof(w8001
->name
));
383 error
= w8001_command(w8001
, W8001_CMD_QUERY
, true);
385 __set_bit(BTN_TOUCH
, dev
->keybit
);
386 __set_bit(BTN_TOOL_PEN
, dev
->keybit
);
387 __set_bit(BTN_TOOL_RUBBER
, dev
->keybit
);
388 __set_bit(BTN_STYLUS
, dev
->keybit
);
389 __set_bit(BTN_STYLUS2
, dev
->keybit
);
391 parse_pen_data(w8001
->response
, &coord
);
392 w8001
->max_pen_x
= coord
.x
;
393 w8001
->max_pen_y
= coord
.y
;
395 input_set_abs_params(dev
, ABS_X
, 0, coord
.x
, 0, 0);
396 input_set_abs_params(dev
, ABS_Y
, 0, coord
.y
, 0, 0);
397 input_set_abs_params(dev
, ABS_PRESSURE
, 0, coord
.pen_pressure
, 0, 0);
398 if (coord
.tilt_x
&& coord
.tilt_y
) {
399 input_set_abs_params(dev
, ABS_TILT_X
, 0, coord
.tilt_x
, 0, 0);
400 input_set_abs_params(dev
, ABS_TILT_Y
, 0, coord
.tilt_y
, 0, 0);
403 strlcat(w8001
->name
, " Penabled", sizeof(w8001
->name
));
407 error
= w8001_command(w8001
, W8001_CMD_TOUCHQUERY
, true);
410 * Some non-touch devices may reply to the touch query. But their
411 * second byte is empty, which indicates touch is not supported.
413 if (!error
&& w8001
->response
[1]) {
414 __set_bit(BTN_TOUCH
, dev
->keybit
);
415 __set_bit(BTN_TOOL_FINGER
, dev
->keybit
);
417 parse_touchquery(w8001
->response
, &touch
);
418 w8001
->max_touch_x
= touch
.x
;
419 w8001
->max_touch_y
= touch
.y
;
421 /* scale to pen maximum */
422 if (w8001
->max_pen_x
&& w8001
->max_pen_y
) {
423 touch
.x
= w8001
->max_pen_x
;
424 touch
.y
= w8001
->max_pen_y
;
427 input_set_abs_params(dev
, ABS_X
, 0, touch
.x
, 0, 0);
428 input_set_abs_params(dev
, ABS_Y
, 0, touch
.y
, 0, 0);
430 switch (touch
.sensor_id
) {
433 w8001
->pktlen
= W8001_PKTLEN_TOUCH93
;
435 strlcat(w8001
->name
, " 1FG", sizeof(w8001
->name
));
441 w8001
->pktlen
= W8001_PKTLEN_TOUCH9A
;
442 strlcat(w8001
->name
, " 1FG", sizeof(w8001
->name
));
447 w8001
->pktlen
= W8001_PKTLEN_TOUCH2FG
;
449 input_mt_init_slots(dev
, 2);
450 input_set_abs_params(dev
, ABS_MT_POSITION_X
,
452 input_set_abs_params(dev
, ABS_MT_POSITION_Y
,
454 input_set_abs_params(dev
, ABS_MT_TOOL_TYPE
,
455 0, MT_TOOL_MAX
, 0, 0);
457 strlcat(w8001
->name
, " 2FG", sizeof(w8001
->name
));
458 if (w8001
->max_pen_x
&& w8001
->max_pen_y
)
466 strlcat(w8001
->name
, " Touchscreen", sizeof(w8001
->name
));
468 return w8001_command(w8001
, W8001_CMD_START
, false);
472 * w8001_disconnect() is the opposite of w8001_connect()
475 static void w8001_disconnect(struct serio
*serio
)
477 struct w8001
*w8001
= serio_get_drvdata(serio
);
479 input_get_device(w8001
->dev
);
480 input_unregister_device(w8001
->dev
);
482 serio_set_drvdata(serio
, NULL
);
483 input_put_device(w8001
->dev
);
488 * w8001_connect() is the routine that is called when someone adds a
489 * new serio device that supports the w8001 protocol and registers it as
493 static int w8001_connect(struct serio
*serio
, struct serio_driver
*drv
)
496 struct input_dev
*input_dev
;
499 w8001
= kzalloc(sizeof(struct w8001
), GFP_KERNEL
);
500 input_dev
= input_allocate_device();
501 if (!w8001
|| !input_dev
) {
506 w8001
->serio
= serio
;
507 w8001
->dev
= input_dev
;
508 init_completion(&w8001
->cmd_done
);
509 snprintf(w8001
->phys
, sizeof(w8001
->phys
), "%s/input0", serio
->phys
);
511 serio_set_drvdata(serio
, w8001
);
512 err
= serio_open(serio
, drv
);
516 err
= w8001_setup(w8001
);
520 input_dev
->name
= w8001
->name
;
521 input_dev
->phys
= w8001
->phys
;
522 input_dev
->id
.product
= w8001
->id
;
523 input_dev
->id
.bustype
= BUS_RS232
;
524 input_dev
->id
.vendor
= 0x056a;
525 input_dev
->id
.version
= 0x0100;
526 input_dev
->dev
.parent
= &serio
->dev
;
528 err
= input_register_device(w8001
->dev
);
537 serio_set_drvdata(serio
, NULL
);
539 input_free_device(input_dev
);
544 static struct serio_device_id w8001_serio_ids
[] = {
547 .proto
= SERIO_W8001
,
554 MODULE_DEVICE_TABLE(serio
, w8001_serio_ids
);
556 static struct serio_driver w8001_drv
= {
560 .description
= DRIVER_DESC
,
561 .id_table
= w8001_serio_ids
,
562 .interrupt
= w8001_interrupt
,
563 .connect
= w8001_connect
,
564 .disconnect
= w8001_disconnect
,
567 static int __init
w8001_init(void)
569 return serio_register_driver(&w8001_drv
);
572 static void __exit
w8001_exit(void)
574 serio_unregister_driver(&w8001_drv
);
577 module_init(w8001_init
);
578 module_exit(w8001_exit
);