Loading...
1/*
2 * Wacom W8001 penabled serial touchscreen driver
3 *
4 * Copyright (c) 2008 Jaya Kumar
5 * Copyright (c) 2010 Red Hat, Inc.
6 * Copyright (c) 2010 - 2011 Ping Cheng, Wacom. <pingc@wacom.com>
7 *
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
10 * more details.
11 *
12 * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
13 */
14
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>
23
24#define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
25
26MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
27MODULE_DESCRIPTION(DRIVER_DESC);
28MODULE_LICENSE("GPL");
29
30#define W8001_MAX_PHYS 42
31
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)
40
41#define W8001_QUERY_PACKET 0x20
42
43#define W8001_CMD_STOP '0'
44#define W8001_CMD_START '1'
45#define W8001_CMD_QUERY '*'
46#define W8001_CMD_TOUCHQUERY '%'
47
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
54
55/* resolution in points/mm */
56#define W8001_PEN_RESOLUTION 100
57#define W8001_TOUCH_RESOLUTION 10
58
59struct w8001_coord {
60 u8 rdy;
61 u8 tsw;
62 u8 f1;
63 u8 f2;
64 u16 x;
65 u16 y;
66 u16 pen_pressure;
67 u8 tilt_x;
68 u8 tilt_y;
69};
70
71/* touch query reply packet */
72struct w8001_touch_query {
73 u16 x;
74 u16 y;
75 u8 panel_res;
76 u8 capacity_res;
77 u8 sensor_id;
78};
79
80/*
81 * Per-touchscreen data.
82 */
83
84struct w8001 {
85 struct input_dev *pen_dev;
86 struct input_dev *touch_dev;
87 struct serio *serio;
88 struct completion cmd_done;
89 int id;
90 int idx;
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];
95 int type;
96 unsigned int pktlen;
97 u16 max_touch_x;
98 u16 max_touch_y;
99 u16 max_pen_x;
100 u16 max_pen_y;
101 char pen_name[64];
102 char touch_name[64];
103 int open_count;
104 struct mutex mutex;
105};
106
107static void parse_pen_data(u8 *data, struct w8001_coord *coord)
108{
109 memset(coord, 0, sizeof(*coord));
110
111 coord->rdy = data[0] & 0x20;
112 coord->tsw = data[0] & 0x01;
113 coord->f1 = data[0] & 0x02;
114 coord->f2 = data[0] & 0x04;
115
116 coord->x = (data[1] & 0x7F) << 9;
117 coord->x |= (data[2] & 0x7F) << 2;
118 coord->x |= (data[6] & 0x60) >> 5;
119
120 coord->y = (data[3] & 0x7F) << 9;
121 coord->y |= (data[4] & 0x7F) << 2;
122 coord->y |= (data[6] & 0x18) >> 3;
123
124 coord->pen_pressure = data[5] & 0x7F;
125 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
126
127 coord->tilt_x = data[7] & 0x7F;
128 coord->tilt_y = data[8] & 0x7F;
129}
130
131static void parse_single_touch(u8 *data, struct w8001_coord *coord)
132{
133 coord->x = (data[1] << 7) | data[2];
134 coord->y = (data[3] << 7) | data[4];
135 coord->tsw = data[0] & 0x01;
136}
137
138static void scale_touch_coordinates(struct w8001 *w8001,
139 unsigned int *x, unsigned int *y)
140{
141 if (w8001->max_pen_x && w8001->max_touch_x)
142 *x = *x * w8001->max_pen_x / w8001->max_touch_x;
143
144 if (w8001->max_pen_y && w8001->max_touch_y)
145 *y = *y * w8001->max_pen_y / w8001->max_touch_y;
146}
147
148static void parse_multi_touch(struct w8001 *w8001)
149{
150 struct input_dev *dev = w8001->touch_dev;
151 unsigned char *data = w8001->data;
152 unsigned int x, y;
153 int i;
154 int count = 0;
155
156 for (i = 0; i < 2; i++) {
157 bool touch = data[0] & (1 << i);
158
159 input_mt_slot(dev, i);
160 input_mt_report_slot_state(dev, MT_TOOL_FINGER, touch);
161 if (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 */
165
166 /* scale to pen maximum */
167 scale_touch_coordinates(w8001, &x, &y);
168
169 input_report_abs(dev, ABS_MT_POSITION_X, x);
170 input_report_abs(dev, ABS_MT_POSITION_Y, y);
171 count++;
172 }
173 }
174
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.
178 */
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);
183 }
184
185 input_sync(dev);
186}
187
188static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
189{
190 memset(query, 0, sizeof(*query));
191
192 query->panel_res = data[1];
193 query->sensor_id = data[2] & 0x7;
194 query->capacity_res = data[7];
195
196 query->x = data[3] << 9;
197 query->x |= data[4] << 2;
198 query->x |= (data[2] >> 5) & 0x3;
199
200 query->y = data[5] << 9;
201 query->y |= data[6] << 2;
202 query->y |= (data[2] >> 3) & 0x3;
203
204 /* Early days' single-finger touch models need the following defaults */
205 if (!query->x && !query->y) {
206 query->x = 1024;
207 query->y = 1024;
208 if (query->panel_res)
209 query->x = query->y = (1 << query->panel_res);
210 query->panel_res = W8001_TOUCH_RESOLUTION;
211 }
212}
213
214static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
215{
216 struct input_dev *dev = w8001->pen_dev;
217
218 /*
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,
221 * or eraser. Assume:
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.
226 */
227
228 switch (w8001->type) {
229 case BTN_TOOL_RUBBER:
230 if (!coord->f2) {
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);
236 input_sync(dev);
237 w8001->type = BTN_TOOL_PEN;
238 }
239 break;
240
241 case BTN_TOOL_FINGER:
242 case KEY_RESERVED:
243 w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
244 break;
245
246 default:
247 input_report_key(dev, BTN_STYLUS2, coord->f2);
248 break;
249 }
250
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);
257 input_sync(dev);
258
259 if (!coord->rdy)
260 w8001->type = KEY_RESERVED;
261}
262
263static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord)
264{
265 struct input_dev *dev = w8001->touch_dev;
266 unsigned int x = coord->x;
267 unsigned int y = coord->y;
268
269 /* scale to pen maximum */
270 scale_touch_coordinates(w8001, &x, &y);
271
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
276 input_sync(dev);
277
278 w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED;
279}
280
281static irqreturn_t w8001_interrupt(struct serio *serio,
282 unsigned char data, unsigned int flags)
283{
284 struct w8001 *w8001 = serio_get_drvdata(serio);
285 struct w8001_coord coord;
286 unsigned char tmp;
287
288 w8001->data[w8001->idx] = data;
289 switch (w8001->idx++) {
290 case 0:
291 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
292 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
293 w8001->idx = 0;
294 }
295 break;
296
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)
301 break;
302
303 if (w8001->pktlen == w8001->idx) {
304 w8001->idx = 0;
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);
309 }
310 }
311 break;
312
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))
317 break;
318
319 tmp = w8001->data[0] & W8001_TOUCH_BYTE;
320 if (tmp == W8001_TOUCH_BYTE)
321 break;
322
323 w8001->idx = 0;
324 parse_pen_data(w8001->data, &coord);
325 report_pen_events(w8001, &coord);
326 break;
327
328 /* control packet */
329 case W8001_PKTLEN_TPCCTL - 1:
330 tmp = w8001->data[0] & W8001_TOUCH_MASK;
331 if (tmp == W8001_TOUCH_BYTE)
332 break;
333
334 w8001->idx = 0;
335 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
336 w8001->response_type = W8001_QUERY_PACKET;
337 complete(&w8001->cmd_done);
338 break;
339
340 /* 2 finger touch packet */
341 case W8001_PKTLEN_TOUCH2FG - 1:
342 w8001->idx = 0;
343 parse_multi_touch(w8001);
344 break;
345
346 default:
347 /*
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.
351 */
352 if (!w8001->touch_dev && w8001->idx > W8001_PKTLEN_TPCPEN - 1)
353 w8001->idx = 0;
354 }
355
356 return IRQ_HANDLED;
357}
358
359static int w8001_command(struct w8001 *w8001, unsigned char command,
360 bool wait_response)
361{
362 int rc;
363
364 w8001->response_type = 0;
365 init_completion(&w8001->cmd_done);
366
367 rc = serio_write(w8001->serio, command);
368 if (rc == 0 && wait_response) {
369
370 wait_for_completion_timeout(&w8001->cmd_done, HZ);
371 if (w8001->response_type != W8001_QUERY_PACKET)
372 rc = -EIO;
373 }
374
375 return rc;
376}
377
378static int w8001_open(struct input_dev *dev)
379{
380 struct w8001 *w8001 = input_get_drvdata(dev);
381 int err;
382
383 scoped_guard(mutex_intr, &w8001->mutex) {
384 if (w8001->open_count == 0) {
385 err = w8001_command(w8001, W8001_CMD_START, false);
386 if (err)
387 return err;
388 }
389
390 w8001->open_count++;
391 return 0;
392 }
393
394 return -EINTR;
395}
396
397static void w8001_close(struct input_dev *dev)
398{
399 struct w8001 *w8001 = input_get_drvdata(dev);
400
401 guard(mutex)(&w8001->mutex);
402
403 if (--w8001->open_count == 0)
404 w8001_command(w8001, W8001_CMD_STOP, false);
405}
406
407static int w8001_detect(struct w8001 *w8001)
408{
409 int error;
410
411 error = w8001_command(w8001, W8001_CMD_STOP, false);
412 if (error)
413 return error;
414
415 msleep(250); /* wait 250ms before querying the device */
416
417 return 0;
418}
419
420static int w8001_setup_pen(struct w8001 *w8001, char *basename,
421 size_t basename_sz)
422{
423 struct input_dev *dev = w8001->pen_dev;
424 struct w8001_coord coord;
425 int error;
426
427 /* penabled? */
428 error = w8001_command(w8001, W8001_CMD_QUERY, true);
429 if (error)
430 return error;
431
432 __set_bit(EV_KEY, dev->evbit);
433 __set_bit(EV_ABS, dev->evbit);
434 __set_bit(BTN_TOUCH, dev->keybit);
435 __set_bit(BTN_TOOL_PEN, dev->keybit);
436 __set_bit(BTN_TOOL_RUBBER, dev->keybit);
437 __set_bit(BTN_STYLUS, dev->keybit);
438 __set_bit(BTN_STYLUS2, dev->keybit);
439 __set_bit(INPUT_PROP_DIRECT, dev->propbit);
440
441 parse_pen_data(w8001->response, &coord);
442 w8001->max_pen_x = coord.x;
443 w8001->max_pen_y = coord.y;
444
445 input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
446 input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
447 input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION);
448 input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION);
449 input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
450 if (coord.tilt_x && coord.tilt_y) {
451 input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
452 input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
453 }
454
455 w8001->id = 0x90;
456 strlcat(basename, " Penabled", basename_sz);
457
458 return 0;
459}
460
461static int w8001_setup_touch(struct w8001 *w8001, char *basename,
462 size_t basename_sz)
463{
464 struct input_dev *dev = w8001->touch_dev;
465 struct w8001_touch_query touch;
466 int error;
467
468
469 /* Touch enabled? */
470 error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
471 if (error)
472 return error;
473 /*
474 * Some non-touch devices may reply to the touch query. But their
475 * second byte is empty, which indicates touch is not supported.
476 */
477 if (!w8001->response[1])
478 return -ENXIO;
479
480 __set_bit(EV_KEY, dev->evbit);
481 __set_bit(EV_ABS, dev->evbit);
482 __set_bit(BTN_TOUCH, dev->keybit);
483 __set_bit(INPUT_PROP_DIRECT, dev->propbit);
484
485 parse_touchquery(w8001->response, &touch);
486 w8001->max_touch_x = touch.x;
487 w8001->max_touch_y = touch.y;
488
489 if (w8001->max_pen_x && w8001->max_pen_y) {
490 /* if pen is supported scale to pen maximum */
491 touch.x = w8001->max_pen_x;
492 touch.y = w8001->max_pen_y;
493 touch.panel_res = W8001_PEN_RESOLUTION;
494 }
495
496 input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0);
497 input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0);
498 input_abs_set_res(dev, ABS_X, touch.panel_res);
499 input_abs_set_res(dev, ABS_Y, touch.panel_res);
500
501 switch (touch.sensor_id) {
502 case 0:
503 case 2:
504 w8001->pktlen = W8001_PKTLEN_TOUCH93;
505 w8001->id = 0x93;
506 strlcat(basename, " 1FG", basename_sz);
507 break;
508
509 case 1:
510 case 3:
511 case 4:
512 w8001->pktlen = W8001_PKTLEN_TOUCH9A;
513 strlcat(basename, " 1FG", basename_sz);
514 w8001->id = 0x9a;
515 break;
516
517 case 5:
518 w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
519
520 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
521 error = input_mt_init_slots(dev, 2, 0);
522 if (error) {
523 dev_err(&w8001->serio->dev,
524 "failed to initialize MT slots: %d\n", error);
525 return error;
526 }
527
528 input_set_abs_params(dev, ABS_MT_POSITION_X,
529 0, touch.x, 0, 0);
530 input_set_abs_params(dev, ABS_MT_POSITION_Y,
531 0, touch.y, 0, 0);
532 input_set_abs_params(dev, ABS_MT_TOOL_TYPE,
533 0, MT_TOOL_MAX, 0, 0);
534 input_abs_set_res(dev, ABS_MT_POSITION_X, touch.panel_res);
535 input_abs_set_res(dev, ABS_MT_POSITION_Y, touch.panel_res);
536
537 strlcat(basename, " 2FG", basename_sz);
538 if (w8001->max_pen_x && w8001->max_pen_y)
539 w8001->id = 0xE3;
540 else
541 w8001->id = 0xE2;
542 break;
543 }
544
545 strlcat(basename, " Touchscreen", basename_sz);
546
547 return 0;
548}
549
550static void w8001_set_devdata(struct input_dev *dev, struct w8001 *w8001,
551 struct serio *serio)
552{
553 dev->phys = w8001->phys;
554 dev->id.bustype = BUS_RS232;
555 dev->id.product = w8001->id;
556 dev->id.vendor = 0x056a;
557 dev->id.version = 0x0100;
558 dev->open = w8001_open;
559 dev->close = w8001_close;
560
561 dev->dev.parent = &serio->dev;
562
563 input_set_drvdata(dev, w8001);
564}
565
566/*
567 * w8001_disconnect() is the opposite of w8001_connect()
568 */
569
570static void w8001_disconnect(struct serio *serio)
571{
572 struct w8001 *w8001 = serio_get_drvdata(serio);
573
574 serio_close(serio);
575
576 if (w8001->pen_dev)
577 input_unregister_device(w8001->pen_dev);
578 if (w8001->touch_dev)
579 input_unregister_device(w8001->touch_dev);
580 kfree(w8001);
581
582 serio_set_drvdata(serio, NULL);
583}
584
585/*
586 * w8001_connect() is the routine that is called when someone adds a
587 * new serio device that supports the w8001 protocol and registers it as
588 * an input device.
589 */
590
591static int w8001_connect(struct serio *serio, struct serio_driver *drv)
592{
593 struct w8001 *w8001;
594 struct input_dev *input_dev_pen;
595 struct input_dev *input_dev_touch;
596 char basename[64] = "Wacom Serial";
597 int err, err_pen, err_touch;
598
599 w8001 = kzalloc(sizeof(*w8001), GFP_KERNEL);
600 input_dev_pen = input_allocate_device();
601 input_dev_touch = input_allocate_device();
602 if (!w8001 || !input_dev_pen || !input_dev_touch) {
603 err = -ENOMEM;
604 goto fail1;
605 }
606
607 w8001->serio = serio;
608 w8001->pen_dev = input_dev_pen;
609 w8001->touch_dev = input_dev_touch;
610 mutex_init(&w8001->mutex);
611 init_completion(&w8001->cmd_done);
612 snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
613
614 serio_set_drvdata(serio, w8001);
615 err = serio_open(serio, drv);
616 if (err)
617 goto fail2;
618
619 err = w8001_detect(w8001);
620 if (err)
621 goto fail3;
622
623 /* For backwards-compatibility we compose the basename based on
624 * capabilities and then just append the tool type
625 */
626 err_pen = w8001_setup_pen(w8001, basename, sizeof(basename));
627 err_touch = w8001_setup_touch(w8001, basename, sizeof(basename));
628 if (err_pen && err_touch) {
629 err = -ENXIO;
630 goto fail3;
631 }
632
633 if (!err_pen) {
634 snprintf(w8001->pen_name, sizeof(w8001->pen_name),
635 "%s Pen", basename);
636 input_dev_pen->name = w8001->pen_name;
637
638 w8001_set_devdata(input_dev_pen, w8001, serio);
639
640 err = input_register_device(w8001->pen_dev);
641 if (err)
642 goto fail3;
643 } else {
644 input_free_device(input_dev_pen);
645 input_dev_pen = NULL;
646 w8001->pen_dev = NULL;
647 }
648
649 if (!err_touch) {
650 snprintf(w8001->touch_name, sizeof(w8001->touch_name),
651 "%s Finger", basename);
652 input_dev_touch->name = w8001->touch_name;
653
654 w8001_set_devdata(input_dev_touch, w8001, serio);
655
656 err = input_register_device(w8001->touch_dev);
657 if (err)
658 goto fail4;
659 } else {
660 input_free_device(input_dev_touch);
661 input_dev_touch = NULL;
662 w8001->touch_dev = NULL;
663 }
664
665 return 0;
666
667fail4:
668 if (w8001->pen_dev)
669 input_unregister_device(w8001->pen_dev);
670fail3:
671 serio_close(serio);
672fail2:
673 serio_set_drvdata(serio, NULL);
674fail1:
675 input_free_device(input_dev_pen);
676 input_free_device(input_dev_touch);
677 kfree(w8001);
678 return err;
679}
680
681static const struct serio_device_id w8001_serio_ids[] = {
682 {
683 .type = SERIO_RS232,
684 .proto = SERIO_W8001,
685 .id = SERIO_ANY,
686 .extra = SERIO_ANY,
687 },
688 { 0 }
689};
690
691MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
692
693static struct serio_driver w8001_drv = {
694 .driver = {
695 .name = "w8001",
696 },
697 .description = DRIVER_DESC,
698 .id_table = w8001_serio_ids,
699 .interrupt = w8001_interrupt,
700 .connect = w8001_connect,
701 .disconnect = w8001_disconnect,
702};
703
704module_serio_driver(w8001_drv);
1/*
2 * Wacom W8001 penabled serial touchscreen driver
3 *
4 * Copyright (c) 2008 Jaya Kumar
5 * Copyright (c) 2010 Red Hat, Inc.
6 * Copyright (c) 2010 - 2011 Ping Cheng, Wacom. <pingc@wacom.com>
7 *
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
10 * more details.
11 *
12 * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
13 */
14
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>
24
25#define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
26
27MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
28MODULE_DESCRIPTION(DRIVER_DESC);
29MODULE_LICENSE("GPL");
30
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)
39
40#define W8001_QUERY_PACKET 0x20
41
42#define W8001_CMD_STOP '0'
43#define W8001_CMD_START '1'
44#define W8001_CMD_QUERY '*'
45#define W8001_CMD_TOUCHQUERY '%'
46
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
53
54/* resolution in points/mm */
55#define W8001_PEN_RESOLUTION 100
56#define W8001_TOUCH_RESOLUTION 10
57
58struct w8001_coord {
59 u8 rdy;
60 u8 tsw;
61 u8 f1;
62 u8 f2;
63 u16 x;
64 u16 y;
65 u16 pen_pressure;
66 u8 tilt_x;
67 u8 tilt_y;
68};
69
70/* touch query reply packet */
71struct w8001_touch_query {
72 u16 x;
73 u16 y;
74 u8 panel_res;
75 u8 capacity_res;
76 u8 sensor_id;
77};
78
79/*
80 * Per-touchscreen data.
81 */
82
83struct w8001 {
84 struct input_dev *dev;
85 struct serio *serio;
86 struct completion cmd_done;
87 int id;
88 int idx;
89 unsigned char response_type;
90 unsigned char response[W8001_MAX_LENGTH];
91 unsigned char data[W8001_MAX_LENGTH];
92 char phys[32];
93 int type;
94 unsigned int pktlen;
95 u16 max_touch_x;
96 u16 max_touch_y;
97 u16 max_pen_x;
98 u16 max_pen_y;
99 char name[64];
100};
101
102static void parse_pen_data(u8 *data, struct w8001_coord *coord)
103{
104 memset(coord, 0, sizeof(*coord));
105
106 coord->rdy = data[0] & 0x20;
107 coord->tsw = data[0] & 0x01;
108 coord->f1 = data[0] & 0x02;
109 coord->f2 = data[0] & 0x04;
110
111 coord->x = (data[1] & 0x7F) << 9;
112 coord->x |= (data[2] & 0x7F) << 2;
113 coord->x |= (data[6] & 0x60) >> 5;
114
115 coord->y = (data[3] & 0x7F) << 9;
116 coord->y |= (data[4] & 0x7F) << 2;
117 coord->y |= (data[6] & 0x18) >> 3;
118
119 coord->pen_pressure = data[5] & 0x7F;
120 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
121
122 coord->tilt_x = data[7] & 0x7F;
123 coord->tilt_y = data[8] & 0x7F;
124}
125
126static void parse_single_touch(u8 *data, struct w8001_coord *coord)
127{
128 coord->x = (data[1] << 7) | data[2];
129 coord->y = (data[3] << 7) | data[4];
130 coord->tsw = data[0] & 0x01;
131}
132
133static void scale_touch_coordinates(struct w8001 *w8001,
134 unsigned int *x, unsigned int *y)
135{
136 if (w8001->max_pen_x && w8001->max_touch_x)
137 *x = *x * w8001->max_pen_x / w8001->max_touch_x;
138
139 if (w8001->max_pen_y && w8001->max_touch_y)
140 *y = *y * w8001->max_pen_y / w8001->max_touch_y;
141}
142
143static void parse_multi_touch(struct w8001 *w8001)
144{
145 struct input_dev *dev = w8001->dev;
146 unsigned char *data = w8001->data;
147 unsigned int x, y;
148 int i;
149 int count = 0;
150
151 for (i = 0; i < 2; i++) {
152 bool touch = data[0] & (1 << i);
153
154 input_mt_slot(dev, i);
155 input_mt_report_slot_state(dev, MT_TOOL_FINGER, touch);
156 if (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 */
160
161 /* scale to pen maximum */
162 scale_touch_coordinates(w8001, &x, &y);
163
164 input_report_abs(dev, ABS_MT_POSITION_X, x);
165 input_report_abs(dev, ABS_MT_POSITION_Y, y);
166 count++;
167 }
168 }
169
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.
173 */
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);
178 }
179
180 input_sync(dev);
181}
182
183static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
184{
185 memset(query, 0, sizeof(*query));
186
187 query->panel_res = data[1];
188 query->sensor_id = data[2] & 0x7;
189 query->capacity_res = data[7];
190
191 query->x = data[3] << 9;
192 query->x |= data[4] << 2;
193 query->x |= (data[2] >> 5) & 0x3;
194
195 query->y = data[5] << 9;
196 query->y |= data[6] << 2;
197 query->y |= (data[2] >> 3) & 0x3;
198
199 /* Early days' single-finger touch models need the following defaults */
200 if (!query->x && !query->y) {
201 query->x = 1024;
202 query->y = 1024;
203 if (query->panel_res)
204 query->x = query->y = (1 << query->panel_res);
205 query->panel_res = W8001_TOUCH_RESOLUTION;
206 }
207}
208
209static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
210{
211 struct input_dev *dev = w8001->dev;
212
213 /*
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,
216 * or eraser. Assume:
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.
221 */
222
223 switch (w8001->type) {
224 case BTN_TOOL_RUBBER:
225 if (!coord->f2) {
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);
231 input_sync(dev);
232 w8001->type = BTN_TOOL_PEN;
233 }
234 break;
235
236 case BTN_TOOL_FINGER:
237 input_report_key(dev, BTN_TOUCH, 0);
238 input_report_key(dev, BTN_TOOL_FINGER, 0);
239 input_sync(dev);
240 /* fall through */
241
242 case KEY_RESERVED:
243 w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
244 break;
245
246 default:
247 input_report_key(dev, BTN_STYLUS2, coord->f2);
248 break;
249 }
250
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);
257 input_sync(dev);
258
259 if (!coord->rdy)
260 w8001->type = KEY_RESERVED;
261}
262
263static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord)
264{
265 struct input_dev *dev = w8001->dev;
266 unsigned int x = coord->x;
267 unsigned int y = coord->y;
268
269 /* scale to pen maximum */
270 scale_touch_coordinates(w8001, &x, &y);
271
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);
276
277 input_sync(dev);
278
279 w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED;
280}
281
282static irqreturn_t w8001_interrupt(struct serio *serio,
283 unsigned char data, unsigned int flags)
284{
285 struct w8001 *w8001 = serio_get_drvdata(serio);
286 struct w8001_coord coord;
287 unsigned char tmp;
288
289 w8001->data[w8001->idx] = data;
290 switch (w8001->idx++) {
291 case 0:
292 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
293 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
294 w8001->idx = 0;
295 }
296 break;
297
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)
302 break;
303
304 if (w8001->pktlen == w8001->idx) {
305 w8001->idx = 0;
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);
310 }
311 }
312 break;
313
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))
318 break;
319
320 tmp = w8001->data[0] & W8001_TOUCH_BYTE;
321 if (tmp == W8001_TOUCH_BYTE)
322 break;
323
324 w8001->idx = 0;
325 parse_pen_data(w8001->data, &coord);
326 report_pen_events(w8001, &coord);
327 break;
328
329 /* control packet */
330 case W8001_PKTLEN_TPCCTL - 1:
331 tmp = w8001->data[0] & W8001_TOUCH_MASK;
332 if (tmp == W8001_TOUCH_BYTE)
333 break;
334
335 w8001->idx = 0;
336 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
337 w8001->response_type = W8001_QUERY_PACKET;
338 complete(&w8001->cmd_done);
339 break;
340
341 /* 2 finger touch packet */
342 case W8001_PKTLEN_TOUCH2FG - 1:
343 w8001->idx = 0;
344 parse_multi_touch(w8001);
345 break;
346 }
347
348 return IRQ_HANDLED;
349}
350
351static int w8001_command(struct w8001 *w8001, unsigned char command,
352 bool wait_response)
353{
354 int rc;
355
356 w8001->response_type = 0;
357 init_completion(&w8001->cmd_done);
358
359 rc = serio_write(w8001->serio, command);
360 if (rc == 0 && wait_response) {
361
362 wait_for_completion_timeout(&w8001->cmd_done, HZ);
363 if (w8001->response_type != W8001_QUERY_PACKET)
364 rc = -EIO;
365 }
366
367 return rc;
368}
369
370static int w8001_setup(struct w8001 *w8001)
371{
372 struct input_dev *dev = w8001->dev;
373 struct w8001_coord coord;
374 struct w8001_touch_query touch;
375 int error;
376
377 error = w8001_command(w8001, W8001_CMD_STOP, false);
378 if (error)
379 return error;
380
381 msleep(250); /* wait 250ms before querying the device */
382
383 dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
384 strlcat(w8001->name, "Wacom Serial", sizeof(w8001->name));
385
386 __set_bit(INPUT_PROP_DIRECT, dev->propbit);
387
388 /* penabled? */
389 error = w8001_command(w8001, W8001_CMD_QUERY, true);
390 if (!error) {
391 __set_bit(BTN_TOUCH, dev->keybit);
392 __set_bit(BTN_TOOL_PEN, dev->keybit);
393 __set_bit(BTN_TOOL_RUBBER, dev->keybit);
394 __set_bit(BTN_STYLUS, dev->keybit);
395 __set_bit(BTN_STYLUS2, dev->keybit);
396
397 parse_pen_data(w8001->response, &coord);
398 w8001->max_pen_x = coord.x;
399 w8001->max_pen_y = coord.y;
400
401 input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
402 input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
403 input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION);
404 input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION);
405 input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
406 if (coord.tilt_x && coord.tilt_y) {
407 input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
408 input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
409 }
410 w8001->id = 0x90;
411 strlcat(w8001->name, " Penabled", sizeof(w8001->name));
412 }
413
414 /* Touch enabled? */
415 error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
416
417 /*
418 * Some non-touch devices may reply to the touch query. But their
419 * second byte is empty, which indicates touch is not supported.
420 */
421 if (!error && w8001->response[1]) {
422 __set_bit(BTN_TOUCH, dev->keybit);
423 __set_bit(BTN_TOOL_FINGER, dev->keybit);
424
425 parse_touchquery(w8001->response, &touch);
426 w8001->max_touch_x = touch.x;
427 w8001->max_touch_y = touch.y;
428
429 if (w8001->max_pen_x && w8001->max_pen_y) {
430 /* if pen is supported scale to pen maximum */
431 touch.x = w8001->max_pen_x;
432 touch.y = w8001->max_pen_y;
433 touch.panel_res = W8001_PEN_RESOLUTION;
434 }
435
436 input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0);
437 input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0);
438 input_abs_set_res(dev, ABS_X, touch.panel_res);
439 input_abs_set_res(dev, ABS_Y, touch.panel_res);
440
441 switch (touch.sensor_id) {
442 case 0:
443 case 2:
444 w8001->pktlen = W8001_PKTLEN_TOUCH93;
445 w8001->id = 0x93;
446 strlcat(w8001->name, " 1FG", sizeof(w8001->name));
447 break;
448
449 case 1:
450 case 3:
451 case 4:
452 w8001->pktlen = W8001_PKTLEN_TOUCH9A;
453 strlcat(w8001->name, " 1FG", sizeof(w8001->name));
454 w8001->id = 0x9a;
455 break;
456
457 case 5:
458 w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
459
460 input_mt_init_slots(dev, 2);
461 input_set_abs_params(dev, ABS_MT_POSITION_X,
462 0, touch.x, 0, 0);
463 input_set_abs_params(dev, ABS_MT_POSITION_Y,
464 0, touch.y, 0, 0);
465 input_set_abs_params(dev, ABS_MT_TOOL_TYPE,
466 0, MT_TOOL_MAX, 0, 0);
467
468 strlcat(w8001->name, " 2FG", sizeof(w8001->name));
469 if (w8001->max_pen_x && w8001->max_pen_y)
470 w8001->id = 0xE3;
471 else
472 w8001->id = 0xE2;
473 break;
474 }
475 }
476
477 strlcat(w8001->name, " Touchscreen", sizeof(w8001->name));
478
479 return w8001_command(w8001, W8001_CMD_START, false);
480}
481
482/*
483 * w8001_disconnect() is the opposite of w8001_connect()
484 */
485
486static void w8001_disconnect(struct serio *serio)
487{
488 struct w8001 *w8001 = serio_get_drvdata(serio);
489
490 input_get_device(w8001->dev);
491 input_unregister_device(w8001->dev);
492 serio_close(serio);
493 serio_set_drvdata(serio, NULL);
494 input_put_device(w8001->dev);
495 kfree(w8001);
496}
497
498/*
499 * w8001_connect() is the routine that is called when someone adds a
500 * new serio device that supports the w8001 protocol and registers it as
501 * an input device.
502 */
503
504static int w8001_connect(struct serio *serio, struct serio_driver *drv)
505{
506 struct w8001 *w8001;
507 struct input_dev *input_dev;
508 int err;
509
510 w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
511 input_dev = input_allocate_device();
512 if (!w8001 || !input_dev) {
513 err = -ENOMEM;
514 goto fail1;
515 }
516
517 w8001->serio = serio;
518 w8001->dev = input_dev;
519 init_completion(&w8001->cmd_done);
520 snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
521
522 serio_set_drvdata(serio, w8001);
523 err = serio_open(serio, drv);
524 if (err)
525 goto fail2;
526
527 err = w8001_setup(w8001);
528 if (err)
529 goto fail3;
530
531 input_dev->name = w8001->name;
532 input_dev->phys = w8001->phys;
533 input_dev->id.product = w8001->id;
534 input_dev->id.bustype = BUS_RS232;
535 input_dev->id.vendor = 0x056a;
536 input_dev->id.version = 0x0100;
537 input_dev->dev.parent = &serio->dev;
538
539 err = input_register_device(w8001->dev);
540 if (err)
541 goto fail3;
542
543 return 0;
544
545fail3:
546 serio_close(serio);
547fail2:
548 serio_set_drvdata(serio, NULL);
549fail1:
550 input_free_device(input_dev);
551 kfree(w8001);
552 return err;
553}
554
555static struct serio_device_id w8001_serio_ids[] = {
556 {
557 .type = SERIO_RS232,
558 .proto = SERIO_W8001,
559 .id = SERIO_ANY,
560 .extra = SERIO_ANY,
561 },
562 { 0 }
563};
564
565MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
566
567static struct serio_driver w8001_drv = {
568 .driver = {
569 .name = "w8001",
570 },
571 .description = DRIVER_DESC,
572 .id_table = w8001_serio_ids,
573 .interrupt = w8001_interrupt,
574 .connect = w8001_connect,
575 .disconnect = w8001_disconnect,
576};
577
578static int __init w8001_init(void)
579{
580 return serio_register_driver(&w8001_drv);
581}
582
583static void __exit w8001_exit(void)
584{
585 serio_unregister_driver(&w8001_drv);
586}
587
588module_init(w8001_init);
589module_exit(w8001_exit);