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
54 /* resolution in points/mm */
55 #define W8001_PEN_RESOLUTION 100
56 #define W8001_TOUCH_RESOLUTION 10
70 /* touch query reply packet */
71 struct w8001_touch_query
{
80 * Per-touchscreen data.
84 struct input_dev
*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
];
102 static void parse_pen_data(u8
*data
, struct w8001_coord
*coord
)
104 memset(coord
, 0, sizeof(*coord
));
106 coord
->rdy
= data
[0] & 0x20;
107 coord
->tsw
= data
[0] & 0x01;
108 coord
->f1
= data
[0] & 0x02;
109 coord
->f2
= data
[0] & 0x04;
111 coord
->x
= (data
[1] & 0x7F) << 9;
112 coord
->x
|= (data
[2] & 0x7F) << 2;
113 coord
->x
|= (data
[6] & 0x60) >> 5;
115 coord
->y
= (data
[3] & 0x7F) << 9;
116 coord
->y
|= (data
[4] & 0x7F) << 2;
117 coord
->y
|= (data
[6] & 0x18) >> 3;
119 coord
->pen_pressure
= data
[5] & 0x7F;
120 coord
->pen_pressure
|= (data
[6] & 0x07) << 7 ;
122 coord
->tilt_x
= data
[7] & 0x7F;
123 coord
->tilt_y
= data
[8] & 0x7F;
126 static void parse_single_touch(u8
*data
, struct w8001_coord
*coord
)
128 coord
->x
= (data
[1] << 7) | data
[2];
129 coord
->y
= (data
[3] << 7) | data
[4];
130 coord
->tsw
= data
[0] & 0x01;
133 static void scale_touch_coordinates(struct w8001
*w8001
,
134 unsigned int *x
, unsigned int *y
)
136 if (w8001
->max_pen_x
&& w8001
->max_touch_x
)
137 *x
= *x
* w8001
->max_pen_x
/ w8001
->max_touch_x
;
139 if (w8001
->max_pen_y
&& w8001
->max_touch_y
)
140 *y
= *y
* w8001
->max_pen_y
/ w8001
->max_touch_y
;
143 static void parse_multi_touch(struct w8001
*w8001
)
145 struct input_dev
*dev
= w8001
->dev
;
146 unsigned char *data
= w8001
->data
;
151 for (i
= 0; i
< 2; i
++) {
152 bool touch
= data
[0] & (1 << i
);
154 input_mt_slot(dev
, i
);
155 input_mt_report_slot_state(dev
, MT_TOOL_FINGER
, touch
);
157 x
= (data
[6 * i
+ 1] << 7) | data
[6 * i
+ 2];
158 y
= (data
[6 * i
+ 3] << 7) | data
[6 * i
+ 4];
159 /* data[5,6] and [11,12] is finger capacity */
161 /* scale to pen maximum */
162 scale_touch_coordinates(w8001
, &x
, &y
);
164 input_report_abs(dev
, ABS_MT_POSITION_X
, x
);
165 input_report_abs(dev
, ABS_MT_POSITION_Y
, y
);
170 /* emulate single touch events when stylus is out of proximity.
171 * This is to make single touch backward support consistent
172 * across all Wacom single touch devices.
174 if (w8001
->type
!= BTN_TOOL_PEN
&&
175 w8001
->type
!= BTN_TOOL_RUBBER
) {
176 w8001
->type
= count
== 1 ? BTN_TOOL_FINGER
: KEY_RESERVED
;
177 input_mt_report_pointer_emulation(dev
, true);
183 static void parse_touchquery(u8
*data
, struct w8001_touch_query
*query
)
185 memset(query
, 0, sizeof(*query
));
187 query
->panel_res
= data
[1];
188 query
->sensor_id
= data
[2] & 0x7;
189 query
->capacity_res
= data
[7];
191 query
->x
= data
[3] << 9;
192 query
->x
|= data
[4] << 2;
193 query
->x
|= (data
[2] >> 5) & 0x3;
195 query
->y
= data
[5] << 9;
196 query
->y
|= data
[6] << 2;
197 query
->y
|= (data
[2] >> 3) & 0x3;
199 /* Early days' single-finger touch models need the following defaults */
200 if (!query
->x
&& !query
->y
) {
203 if (query
->panel_res
)
204 query
->x
= query
->y
= (1 << query
->panel_res
);
205 query
->panel_res
= W8001_TOUCH_RESOLUTION
;
209 static void report_pen_events(struct w8001
*w8001
, struct w8001_coord
*coord
)
211 struct input_dev
*dev
= w8001
->dev
;
214 * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
215 * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
217 * - if dev is already in proximity and f2 is toggled → pen + side2
218 * - if dev comes into proximity with f2 set → eraser
219 * If f2 disappears after assuming eraser, fake proximity out for
220 * eraser and in for pen.
223 switch (w8001
->type
) {
224 case BTN_TOOL_RUBBER
:
226 input_report_abs(dev
, ABS_PRESSURE
, 0);
227 input_report_key(dev
, BTN_TOUCH
, 0);
228 input_report_key(dev
, BTN_STYLUS
, 0);
229 input_report_key(dev
, BTN_STYLUS2
, 0);
230 input_report_key(dev
, BTN_TOOL_RUBBER
, 0);
232 w8001
->type
= BTN_TOOL_PEN
;
236 case BTN_TOOL_FINGER
:
237 input_report_key(dev
, BTN_TOUCH
, 0);
238 input_report_key(dev
, BTN_TOOL_FINGER
, 0);
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
->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
);
275 input_report_key(dev
, BTN_TOOL_FINGER
, coord
->tsw
);
279 w8001
->type
= coord
->tsw
? BTN_TOOL_FINGER
: KEY_RESERVED
;
282 static irqreturn_t
w8001_interrupt(struct serio
*serio
,
283 unsigned char data
, unsigned int flags
)
285 struct w8001
*w8001
= serio_get_drvdata(serio
);
286 struct w8001_coord coord
;
289 w8001
->data
[w8001
->idx
] = data
;
290 switch (w8001
->idx
++) {
292 if ((data
& W8001_LEAD_MASK
) != W8001_LEAD_BYTE
) {
293 pr_debug("w8001: unsynchronized data: 0x%02x\n", data
);
298 case W8001_PKTLEN_TOUCH93
- 1:
299 case W8001_PKTLEN_TOUCH9A
- 1:
300 tmp
= w8001
->data
[0] & W8001_TOUCH_BYTE
;
301 if (tmp
!= W8001_TOUCH_BYTE
)
304 if (w8001
->pktlen
== w8001
->idx
) {
306 if (w8001
->type
!= BTN_TOOL_PEN
&&
307 w8001
->type
!= BTN_TOOL_RUBBER
) {
308 parse_single_touch(w8001
->data
, &coord
);
309 report_single_touch(w8001
, &coord
);
314 /* Pen coordinates packet */
315 case W8001_PKTLEN_TPCPEN
- 1:
316 tmp
= w8001
->data
[0] & W8001_TAB_MASK
;
317 if (unlikely(tmp
== W8001_TAB_BYTE
))
320 tmp
= w8001
->data
[0] & W8001_TOUCH_BYTE
;
321 if (tmp
== W8001_TOUCH_BYTE
)
325 parse_pen_data(w8001
->data
, &coord
);
326 report_pen_events(w8001
, &coord
);
330 case W8001_PKTLEN_TPCCTL
- 1:
331 tmp
= w8001
->data
[0] & W8001_TOUCH_MASK
;
332 if (tmp
== W8001_TOUCH_BYTE
)
336 memcpy(w8001
->response
, w8001
->data
, W8001_MAX_LENGTH
);
337 w8001
->response_type
= W8001_QUERY_PACKET
;
338 complete(&w8001
->cmd_done
);
341 /* 2 finger touch packet */
342 case W8001_PKTLEN_TOUCH2FG
- 1:
344 parse_multi_touch(w8001
);
351 static int w8001_command(struct w8001
*w8001
, unsigned char command
,
356 w8001
->response_type
= 0;
357 init_completion(&w8001
->cmd_done
);
359 rc
= serio_write(w8001
->serio
, command
);
360 if (rc
== 0 && wait_response
) {
362 wait_for_completion_timeout(&w8001
->cmd_done
, HZ
);
363 if (w8001
->response_type
!= W8001_QUERY_PACKET
)
370 static int w8001_setup(struct w8001
*w8001
)
372 struct input_dev
*dev
= w8001
->dev
;
373 struct w8001_coord coord
;
374 struct w8001_touch_query touch
;
377 error
= w8001_command(w8001
, W8001_CMD_STOP
, false);
381 msleep(250); /* wait 250ms before querying the device */
383 dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
384 strlcat(w8001
->name
, "Wacom Serial", sizeof(w8001
->name
));
387 error
= w8001_command(w8001
, W8001_CMD_QUERY
, true);
389 __set_bit(BTN_TOUCH
, dev
->keybit
);
390 __set_bit(BTN_TOOL_PEN
, dev
->keybit
);
391 __set_bit(BTN_TOOL_RUBBER
, dev
->keybit
);
392 __set_bit(BTN_STYLUS
, dev
->keybit
);
393 __set_bit(BTN_STYLUS2
, dev
->keybit
);
395 parse_pen_data(w8001
->response
, &coord
);
396 w8001
->max_pen_x
= coord
.x
;
397 w8001
->max_pen_y
= coord
.y
;
399 input_set_abs_params(dev
, ABS_X
, 0, coord
.x
, 0, 0);
400 input_set_abs_params(dev
, ABS_Y
, 0, coord
.y
, 0, 0);
401 input_abs_set_res(dev
, ABS_X
, W8001_PEN_RESOLUTION
);
402 input_abs_set_res(dev
, ABS_Y
, W8001_PEN_RESOLUTION
);
403 input_set_abs_params(dev
, ABS_PRESSURE
, 0, coord
.pen_pressure
, 0, 0);
404 if (coord
.tilt_x
&& coord
.tilt_y
) {
405 input_set_abs_params(dev
, ABS_TILT_X
, 0, coord
.tilt_x
, 0, 0);
406 input_set_abs_params(dev
, ABS_TILT_Y
, 0, coord
.tilt_y
, 0, 0);
409 strlcat(w8001
->name
, " Penabled", sizeof(w8001
->name
));
413 error
= w8001_command(w8001
, W8001_CMD_TOUCHQUERY
, true);
416 * Some non-touch devices may reply to the touch query. But their
417 * second byte is empty, which indicates touch is not supported.
419 if (!error
&& w8001
->response
[1]) {
420 __set_bit(BTN_TOUCH
, dev
->keybit
);
421 __set_bit(BTN_TOOL_FINGER
, dev
->keybit
);
423 parse_touchquery(w8001
->response
, &touch
);
424 w8001
->max_touch_x
= touch
.x
;
425 w8001
->max_touch_y
= touch
.y
;
427 if (w8001
->max_pen_x
&& w8001
->max_pen_y
) {
428 /* if pen is supported scale to pen maximum */
429 touch
.x
= w8001
->max_pen_x
;
430 touch
.y
= w8001
->max_pen_y
;
431 touch
.panel_res
= W8001_PEN_RESOLUTION
;
434 input_set_abs_params(dev
, ABS_X
, 0, touch
.x
, 0, 0);
435 input_set_abs_params(dev
, ABS_Y
, 0, touch
.y
, 0, 0);
436 input_abs_set_res(dev
, ABS_X
, touch
.panel_res
);
437 input_abs_set_res(dev
, ABS_Y
, touch
.panel_res
);
439 switch (touch
.sensor_id
) {
442 w8001
->pktlen
= W8001_PKTLEN_TOUCH93
;
444 strlcat(w8001
->name
, " 1FG", sizeof(w8001
->name
));
450 w8001
->pktlen
= W8001_PKTLEN_TOUCH9A
;
451 strlcat(w8001
->name
, " 1FG", sizeof(w8001
->name
));
456 w8001
->pktlen
= W8001_PKTLEN_TOUCH2FG
;
458 input_mt_init_slots(dev
, 2);
459 input_set_abs_params(dev
, ABS_MT_POSITION_X
,
461 input_set_abs_params(dev
, ABS_MT_POSITION_Y
,
463 input_set_abs_params(dev
, ABS_MT_TOOL_TYPE
,
464 0, MT_TOOL_MAX
, 0, 0);
466 strlcat(w8001
->name
, " 2FG", sizeof(w8001
->name
));
467 if (w8001
->max_pen_x
&& w8001
->max_pen_y
)
475 strlcat(w8001
->name
, " Touchscreen", sizeof(w8001
->name
));
477 return w8001_command(w8001
, W8001_CMD_START
, false);
481 * w8001_disconnect() is the opposite of w8001_connect()
484 static void w8001_disconnect(struct serio
*serio
)
486 struct w8001
*w8001
= serio_get_drvdata(serio
);
488 input_get_device(w8001
->dev
);
489 input_unregister_device(w8001
->dev
);
491 serio_set_drvdata(serio
, NULL
);
492 input_put_device(w8001
->dev
);
497 * w8001_connect() is the routine that is called when someone adds a
498 * new serio device that supports the w8001 protocol and registers it as
502 static int w8001_connect(struct serio
*serio
, struct serio_driver
*drv
)
505 struct input_dev
*input_dev
;
508 w8001
= kzalloc(sizeof(struct w8001
), GFP_KERNEL
);
509 input_dev
= input_allocate_device();
510 if (!w8001
|| !input_dev
) {
515 w8001
->serio
= serio
;
516 w8001
->dev
= input_dev
;
517 init_completion(&w8001
->cmd_done
);
518 snprintf(w8001
->phys
, sizeof(w8001
->phys
), "%s/input0", serio
->phys
);
520 serio_set_drvdata(serio
, w8001
);
521 err
= serio_open(serio
, drv
);
525 err
= w8001_setup(w8001
);
529 input_dev
->name
= w8001
->name
;
530 input_dev
->phys
= w8001
->phys
;
531 input_dev
->id
.product
= w8001
->id
;
532 input_dev
->id
.bustype
= BUS_RS232
;
533 input_dev
->id
.vendor
= 0x056a;
534 input_dev
->id
.version
= 0x0100;
535 input_dev
->dev
.parent
= &serio
->dev
;
537 err
= input_register_device(w8001
->dev
);
546 serio_set_drvdata(serio
, NULL
);
548 input_free_device(input_dev
);
553 static struct serio_device_id w8001_serio_ids
[] = {
556 .proto
= SERIO_W8001
,
563 MODULE_DEVICE_TABLE(serio
, w8001_serio_ids
);
565 static struct serio_driver w8001_drv
= {
569 .description
= DRIVER_DESC
,
570 .id_table
= w8001_serio_ids
,
571 .interrupt
= w8001_interrupt
,
572 .connect
= w8001_connect
,
573 .disconnect
= w8001_disconnect
,
576 static int __init
w8001_init(void)
578 return serio_register_driver(&w8001_drv
);
581 static void __exit
w8001_exit(void)
583 serio_unregister_driver(&w8001_drv
);
586 module_init(w8001_init
);
587 module_exit(w8001_exit
);