Staging: Merge branch 'tidspbridge-for-2.6.39' of git://dev.omapzoom.org/pub/scm...
[deliverable/linux.git] / drivers / input / touchscreen / wacom_w8001.c
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
27 MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
28 MODULE_DESCRIPTION(DRIVER_DESC);
29 MODULE_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 struct w8001_coord {
55 u8 rdy;
56 u8 tsw;
57 u8 f1;
58 u8 f2;
59 u16 x;
60 u16 y;
61 u16 pen_pressure;
62 u8 tilt_x;
63 u8 tilt_y;
64 };
65
66 /* touch query reply packet */
67 struct w8001_touch_query {
68 u16 x;
69 u16 y;
70 u8 panel_res;
71 u8 capacity_res;
72 u8 sensor_id;
73 };
74
75 /*
76 * Per-touchscreen data.
77 */
78
79 struct w8001 {
80 struct input_dev *dev;
81 struct serio *serio;
82 struct completion cmd_done;
83 int id;
84 int idx;
85 unsigned char response_type;
86 unsigned char response[W8001_MAX_LENGTH];
87 unsigned char data[W8001_MAX_LENGTH];
88 char phys[32];
89 int type;
90 unsigned int pktlen;
91 u16 max_touch_x;
92 u16 max_touch_y;
93 u16 max_pen_x;
94 u16 max_pen_y;
95 char name[64];
96 };
97
98 static void parse_pen_data(u8 *data, struct w8001_coord *coord)
99 {
100 memset(coord, 0, sizeof(*coord));
101
102 coord->rdy = data[0] & 0x20;
103 coord->tsw = data[0] & 0x01;
104 coord->f1 = data[0] & 0x02;
105 coord->f2 = data[0] & 0x04;
106
107 coord->x = (data[1] & 0x7F) << 9;
108 coord->x |= (data[2] & 0x7F) << 2;
109 coord->x |= (data[6] & 0x60) >> 5;
110
111 coord->y = (data[3] & 0x7F) << 9;
112 coord->y |= (data[4] & 0x7F) << 2;
113 coord->y |= (data[6] & 0x18) >> 3;
114
115 coord->pen_pressure = data[5] & 0x7F;
116 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
117
118 coord->tilt_x = data[7] & 0x7F;
119 coord->tilt_y = data[8] & 0x7F;
120 }
121
122 static void parse_single_touch(u8 *data, struct w8001_coord *coord)
123 {
124 coord->x = (data[1] << 7) | data[2];
125 coord->y = (data[3] << 7) | data[4];
126 coord->tsw = data[0] & 0x01;
127 }
128
129 static void scale_touch_coordinates(struct w8001 *w8001,
130 unsigned int *x, unsigned int *y)
131 {
132 if (w8001->max_pen_x && w8001->max_touch_x)
133 *x = *x * w8001->max_pen_x / w8001->max_touch_x;
134
135 if (w8001->max_pen_y && w8001->max_touch_y)
136 *y = *y * w8001->max_pen_y / w8001->max_touch_y;
137 }
138
139 static void parse_multi_touch(struct w8001 *w8001)
140 {
141 struct input_dev *dev = w8001->dev;
142 unsigned char *data = w8001->data;
143 unsigned int x, y;
144 int i;
145 int count = 0;
146
147 for (i = 0; i < 2; i++) {
148 bool touch = data[0] & (1 << i);
149
150 input_mt_slot(dev, i);
151 input_mt_report_slot_state(dev, MT_TOOL_FINGER, touch);
152 if (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 */
156
157 /* scale to pen maximum */
158 scale_touch_coordinates(w8001, &x, &y);
159
160 input_report_abs(dev, ABS_MT_POSITION_X, x);
161 input_report_abs(dev, ABS_MT_POSITION_Y, y);
162 count++;
163 }
164 }
165
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.
169 */
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);
174 }
175
176 input_sync(dev);
177 }
178
179 static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
180 {
181 memset(query, 0, sizeof(*query));
182
183 query->panel_res = data[1];
184 query->sensor_id = data[2] & 0x7;
185 query->capacity_res = data[7];
186
187 query->x = data[3] << 9;
188 query->x |= data[4] << 2;
189 query->x |= (data[2] >> 5) & 0x3;
190
191 query->y = data[5] << 9;
192 query->y |= data[6] << 2;
193 query->y |= (data[2] >> 3) & 0x3;
194
195 /* Early days' single-finger touch models need the following defaults */
196 if (!query->x && !query->y) {
197 query->x = 1024;
198 query->y = 1024;
199 if (query->panel_res)
200 query->x = query->y = (1 << query->panel_res);
201 query->panel_res = 10;
202 }
203 }
204
205 static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
206 {
207 struct input_dev *dev = w8001->dev;
208
209 /*
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,
212 * or eraser. Assume:
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.
217 */
218
219 switch (w8001->type) {
220 case BTN_TOOL_RUBBER:
221 if (!coord->f2) {
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);
227 input_sync(dev);
228 w8001->type = BTN_TOOL_PEN;
229 }
230 break;
231
232 case BTN_TOOL_FINGER:
233 input_report_key(dev, BTN_TOUCH, 0);
234 input_report_key(dev, BTN_TOOL_FINGER, 0);
235 input_sync(dev);
236 /* fall through */
237
238 case KEY_RESERVED:
239 w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
240 break;
241
242 default:
243 input_report_key(dev, BTN_STYLUS2, coord->f2);
244 break;
245 }
246
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);
253 input_sync(dev);
254
255 if (!coord->rdy)
256 w8001->type = KEY_RESERVED;
257 }
258
259 static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord)
260 {
261 struct input_dev *dev = w8001->dev;
262 unsigned int x = coord->x;
263 unsigned int y = coord->y;
264
265 /* scale to pen maximum */
266 scale_touch_coordinates(w8001, &x, &y);
267
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);
272
273 input_sync(dev);
274
275 w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED;
276 }
277
278 static irqreturn_t w8001_interrupt(struct serio *serio,
279 unsigned char data, unsigned int flags)
280 {
281 struct w8001 *w8001 = serio_get_drvdata(serio);
282 struct w8001_coord coord;
283 unsigned char tmp;
284
285 w8001->data[w8001->idx] = data;
286 switch (w8001->idx++) {
287 case 0:
288 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
289 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
290 w8001->idx = 0;
291 }
292 break;
293
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)
298 break;
299
300 if (w8001->pktlen == w8001->idx) {
301 w8001->idx = 0;
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);
306 }
307 }
308 break;
309
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))
314 break;
315
316 tmp = w8001->data[0] & W8001_TOUCH_BYTE;
317 if (tmp == W8001_TOUCH_BYTE)
318 break;
319
320 w8001->idx = 0;
321 parse_pen_data(w8001->data, &coord);
322 report_pen_events(w8001, &coord);
323 break;
324
325 /* control packet */
326 case W8001_PKTLEN_TPCCTL - 1:
327 tmp = w8001->data[0] & W8001_TOUCH_MASK;
328 if (tmp == W8001_TOUCH_BYTE)
329 break;
330
331 w8001->idx = 0;
332 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
333 w8001->response_type = W8001_QUERY_PACKET;
334 complete(&w8001->cmd_done);
335 break;
336
337 /* 2 finger touch packet */
338 case W8001_PKTLEN_TOUCH2FG - 1:
339 w8001->idx = 0;
340 parse_multi_touch(w8001);
341 break;
342 }
343
344 return IRQ_HANDLED;
345 }
346
347 static int w8001_command(struct w8001 *w8001, unsigned char command,
348 bool wait_response)
349 {
350 int rc;
351
352 w8001->response_type = 0;
353 init_completion(&w8001->cmd_done);
354
355 rc = serio_write(w8001->serio, command);
356 if (rc == 0 && wait_response) {
357
358 wait_for_completion_timeout(&w8001->cmd_done, HZ);
359 if (w8001->response_type != W8001_QUERY_PACKET)
360 rc = -EIO;
361 }
362
363 return rc;
364 }
365
366 static int w8001_setup(struct w8001 *w8001)
367 {
368 struct input_dev *dev = w8001->dev;
369 struct w8001_coord coord;
370 struct w8001_touch_query touch;
371 int error;
372
373 error = w8001_command(w8001, W8001_CMD_STOP, false);
374 if (error)
375 return error;
376
377 msleep(250); /* wait 250ms before querying the device */
378
379 dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
380 strlcat(w8001->name, "Wacom Serial", sizeof(w8001->name));
381
382 /* penabled? */
383 error = w8001_command(w8001, W8001_CMD_QUERY, true);
384 if (!error) {
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);
390
391 parse_pen_data(w8001->response, &coord);
392 w8001->max_pen_x = coord.x;
393 w8001->max_pen_y = coord.y;
394
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);
401 }
402 w8001->id = 0x90;
403 strlcat(w8001->name, " Penabled", sizeof(w8001->name));
404 }
405
406 /* Touch enabled? */
407 error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
408
409 /*
410 * Some non-touch devices may reply to the touch query. But their
411 * second byte is empty, which indicates touch is not supported.
412 */
413 if (!error && w8001->response[1]) {
414 __set_bit(BTN_TOUCH, dev->keybit);
415 __set_bit(BTN_TOOL_FINGER, dev->keybit);
416
417 parse_touchquery(w8001->response, &touch);
418 w8001->max_touch_x = touch.x;
419 w8001->max_touch_y = touch.y;
420
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;
425 }
426
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);
429
430 switch (touch.sensor_id) {
431 case 0:
432 case 2:
433 w8001->pktlen = W8001_PKTLEN_TOUCH93;
434 w8001->id = 0x93;
435 strlcat(w8001->name, " 1FG", sizeof(w8001->name));
436 break;
437
438 case 1:
439 case 3:
440 case 4:
441 w8001->pktlen = W8001_PKTLEN_TOUCH9A;
442 strlcat(w8001->name, " 1FG", sizeof(w8001->name));
443 w8001->id = 0x9a;
444 break;
445
446 case 5:
447 w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
448
449 input_mt_init_slots(dev, 2);
450 input_set_abs_params(dev, ABS_MT_POSITION_X,
451 0, touch.x, 0, 0);
452 input_set_abs_params(dev, ABS_MT_POSITION_Y,
453 0, touch.y, 0, 0);
454 input_set_abs_params(dev, ABS_MT_TOOL_TYPE,
455 0, MT_TOOL_MAX, 0, 0);
456
457 strlcat(w8001->name, " 2FG", sizeof(w8001->name));
458 if (w8001->max_pen_x && w8001->max_pen_y)
459 w8001->id = 0xE3;
460 else
461 w8001->id = 0xE2;
462 break;
463 }
464 }
465
466 strlcat(w8001->name, " Touchscreen", sizeof(w8001->name));
467
468 return w8001_command(w8001, W8001_CMD_START, false);
469 }
470
471 /*
472 * w8001_disconnect() is the opposite of w8001_connect()
473 */
474
475 static void w8001_disconnect(struct serio *serio)
476 {
477 struct w8001 *w8001 = serio_get_drvdata(serio);
478
479 input_get_device(w8001->dev);
480 input_unregister_device(w8001->dev);
481 serio_close(serio);
482 serio_set_drvdata(serio, NULL);
483 input_put_device(w8001->dev);
484 kfree(w8001);
485 }
486
487 /*
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
490 * an input device.
491 */
492
493 static int w8001_connect(struct serio *serio, struct serio_driver *drv)
494 {
495 struct w8001 *w8001;
496 struct input_dev *input_dev;
497 int err;
498
499 w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
500 input_dev = input_allocate_device();
501 if (!w8001 || !input_dev) {
502 err = -ENOMEM;
503 goto fail1;
504 }
505
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);
510
511 serio_set_drvdata(serio, w8001);
512 err = serio_open(serio, drv);
513 if (err)
514 goto fail2;
515
516 err = w8001_setup(w8001);
517 if (err)
518 goto fail3;
519
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;
527
528 err = input_register_device(w8001->dev);
529 if (err)
530 goto fail3;
531
532 return 0;
533
534 fail3:
535 serio_close(serio);
536 fail2:
537 serio_set_drvdata(serio, NULL);
538 fail1:
539 input_free_device(input_dev);
540 kfree(w8001);
541 return err;
542 }
543
544 static struct serio_device_id w8001_serio_ids[] = {
545 {
546 .type = SERIO_RS232,
547 .proto = SERIO_W8001,
548 .id = SERIO_ANY,
549 .extra = SERIO_ANY,
550 },
551 { 0 }
552 };
553
554 MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
555
556 static struct serio_driver w8001_drv = {
557 .driver = {
558 .name = "w8001",
559 },
560 .description = DRIVER_DESC,
561 .id_table = w8001_serio_ids,
562 .interrupt = w8001_interrupt,
563 .connect = w8001_connect,
564 .disconnect = w8001_disconnect,
565 };
566
567 static int __init w8001_init(void)
568 {
569 return serio_register_driver(&w8001_drv);
570 }
571
572 static void __exit w8001_exit(void)
573 {
574 serio_unregister_driver(&w8001_drv);
575 }
576
577 module_init(w8001_init);
578 module_exit(w8001_exit);
This page took 0.0522 seconds and 5 git commands to generate.