blob: 8ed53aded2d32b0ea2202f89cc4d916f53239596 [file] [log] [blame]
Jaya Kumar3eb1aa42008-11-19 16:58:50 -05001/*
2 * Wacom W8001 penabled serial touchscreen driver
3 *
4 * Copyright (c) 2008 Jaya Kumar
Peter Huttereraaba9332010-08-28 22:00:47 -07005 * Copyright (c) 2010 Red Hat, Inc.
Jaya Kumar3eb1aa42008-11-19 16:58:50 -05006 *
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file COPYING in the main directory of this archive for
9 * more details.
10 *
11 * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
12 */
13
14#include <linux/errno.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/slab.h>
Henrik Rydberg47c78e82010-11-27 09:16:48 +010018#include <linux/input/mt.h>
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050019#include <linux/serio.h>
20#include <linux/init.h>
21#include <linux/ctype.h>
Ping Chenga6d38f82010-12-24 13:16:53 -080022#include <linux/delay.h>
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050023
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
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050030#define W8001_MAX_LENGTH 11
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070031#define W8001_LEAD_MASK 0x80
32#define W8001_LEAD_BYTE 0x80
33#define W8001_TAB_MASK 0x40
34#define W8001_TAB_BYTE 0x40
Peter Huttereraaba9332010-08-28 22:00:47 -070035/* set in first byte of touch data packets */
36#define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
37#define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050038
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070039#define W8001_QUERY_PACKET 0x20
40
Ping Chenga6d38f82010-12-24 13:16:53 -080041#define W8001_CMD_STOP '0'
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070042#define W8001_CMD_START '1'
43#define W8001_CMD_QUERY '*'
Peter Huttereraaba9332010-08-28 22:00:47 -070044#define W8001_CMD_TOUCHQUERY '%'
45
46/* length of data packets in bytes, depends on device. */
47#define W8001_PKTLEN_TOUCH93 5
48#define W8001_PKTLEN_TOUCH9A 7
49#define W8001_PKTLEN_TPCPEN 9
50#define W8001_PKTLEN_TPCCTL 11 /* control packet */
51#define W8001_PKTLEN_TOUCH2FG 13
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050052
53struct w8001_coord {
54 u8 rdy;
55 u8 tsw;
56 u8 f1;
57 u8 f2;
58 u16 x;
59 u16 y;
60 u16 pen_pressure;
61 u8 tilt_x;
62 u8 tilt_y;
63};
64
Peter Huttereraaba9332010-08-28 22:00:47 -070065/* touch query reply packet */
66struct w8001_touch_query {
67 u8 panel_res;
68 u8 capacity_res;
69 u8 sensor_id;
70 u16 x;
71 u16 y;
72};
73
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050074/*
75 * Per-touchscreen data.
76 */
77
78struct w8001 {
79 struct input_dev *dev;
80 struct serio *serio;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050081 struct completion cmd_done;
82 int id;
83 int idx;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070084 unsigned char response_type;
85 unsigned char response[W8001_MAX_LENGTH];
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050086 unsigned char data[W8001_MAX_LENGTH];
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050087 char phys[32];
Peter Hutterer2072f8d2010-08-28 22:00:05 -070088 int type;
Peter Huttereraaba9332010-08-28 22:00:47 -070089 unsigned int pktlen;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050090};
91
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070092static void parse_data(u8 *data, struct w8001_coord *coord)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050093{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070094 memset(coord, 0, sizeof(*coord));
95
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050096 coord->rdy = data[0] & 0x20;
97 coord->tsw = data[0] & 0x01;
98 coord->f1 = data[0] & 0x02;
99 coord->f2 = data[0] & 0x04;
100
101 coord->x = (data[1] & 0x7F) << 9;
102 coord->x |= (data[2] & 0x7F) << 2;
103 coord->x |= (data[6] & 0x60) >> 5;
104
105 coord->y = (data[3] & 0x7F) << 9;
106 coord->y |= (data[4] & 0x7F) << 2;
107 coord->y |= (data[6] & 0x18) >> 3;
108
109 coord->pen_pressure = data[5] & 0x7F;
110 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
111
112 coord->tilt_x = data[7] & 0x7F;
113 coord->tilt_y = data[8] & 0x7F;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500114}
115
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700116static void parse_touch(struct w8001 *w8001)
117{
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700118 struct input_dev *dev = w8001->dev;
119 unsigned char *data = w8001->data;
120 int i;
121
122 for (i = 0; i < 2; i++) {
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100123 bool touch = data[0] & (1 << i);
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700124
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100125 input_mt_slot(dev, i);
126 input_mt_report_slot_state(dev, MT_TOOL_FINGER, touch);
127 if (touch) {
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700128 int x = (data[6 * i + 1] << 7) | (data[6 * i + 2]);
129 int y = (data[6 * i + 3] << 7) | (data[6 * i + 4]);
130 /* data[5,6] and [11,12] is finger capacity */
131
132 input_report_abs(dev, ABS_MT_POSITION_X, x);
133 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700134 }
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700135 }
136
137 input_sync(dev);
138}
139
Peter Huttereraaba9332010-08-28 22:00:47 -0700140static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
141{
142 memset(query, 0, sizeof(*query));
143
144 query->panel_res = data[1];
145 query->sensor_id = data[2] & 0x7;
146 query->capacity_res = data[7];
147
148 query->x = data[3] << 9;
149 query->x |= data[4] << 2;
150 query->x |= (data[2] >> 5) & 0x3;
151
152 query->y = data[5] << 9;
153 query->y |= data[6] << 2;
154 query->y |= (data[2] >> 3) & 0x3;
155}
156
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700157static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
158{
159 struct input_dev *dev = w8001->dev;
160
161 /*
162 * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
163 * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
164 * or eraser. assume
165 * - if dev is already in proximity and f2 is toggled → pen + side2
166 * - if dev comes into proximity with f2 set → eraser
167 * If f2 disappears after assuming eraser, fake proximity out for
168 * eraser and in for pen.
169 */
170
171 if (!w8001->type) {
172 w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
173 } else if (w8001->type == BTN_TOOL_RUBBER) {
174 if (!coord->f2) {
175 input_report_abs(dev, ABS_PRESSURE, 0);
176 input_report_key(dev, BTN_TOUCH, 0);
177 input_report_key(dev, BTN_STYLUS, 0);
178 input_report_key(dev, BTN_STYLUS2, 0);
179 input_report_key(dev, BTN_TOOL_RUBBER, 0);
180 input_sync(dev);
181 w8001->type = BTN_TOOL_PEN;
182 }
183 } else {
184 input_report_key(dev, BTN_STYLUS2, coord->f2);
185 }
186
187 input_report_abs(dev, ABS_X, coord->x);
188 input_report_abs(dev, ABS_Y, coord->y);
189 input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure);
190 input_report_key(dev, BTN_TOUCH, coord->tsw);
191 input_report_key(dev, BTN_STYLUS, coord->f1);
192 input_report_key(dev, w8001->type, coord->rdy);
193 input_sync(dev);
194
195 if (!coord->rdy)
196 w8001->type = 0;
197}
198
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700199static irqreturn_t w8001_interrupt(struct serio *serio,
200 unsigned char data, unsigned int flags)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500201{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700202 struct w8001 *w8001 = serio_get_drvdata(serio);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500203 struct w8001_coord coord;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700204 unsigned char tmp;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500205
206 w8001->data[w8001->idx] = data;
207 switch (w8001->idx++) {
208 case 0:
209 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
210 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
211 w8001->idx = 0;
212 }
213 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700214
Peter Huttereraaba9332010-08-28 22:00:47 -0700215 case W8001_PKTLEN_TOUCH93 - 1:
216 case W8001_PKTLEN_TOUCH9A - 1:
217 /* ignore one-finger touch packet. */
218 if (w8001->pktlen == w8001->idx)
219 w8001->idx = 0;
220 break;
221
222 /* Pen coordinates packet */
223 case W8001_PKTLEN_TPCPEN - 1:
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500224 tmp = w8001->data[0] & W8001_TAB_MASK;
225 if (unlikely(tmp == W8001_TAB_BYTE))
226 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700227
Peter Huttereraaba9332010-08-28 22:00:47 -0700228 tmp = (w8001->data[0] & W8001_TOUCH_BYTE);
229 if (tmp == W8001_TOUCH_BYTE)
230 break;
231
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500232 w8001->idx = 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500233 parse_data(w8001->data, &coord);
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700234 report_pen_events(w8001, &coord);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500235 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700236
Peter Huttereraaba9332010-08-28 22:00:47 -0700237 /* control packet */
238 case W8001_PKTLEN_TPCCTL - 1:
239 tmp = (w8001->data[0] & W8001_TOUCH_MASK);
240 if (tmp == W8001_TOUCH_BYTE)
241 break;
242
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500243 w8001->idx = 0;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700244 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
245 w8001->response_type = W8001_QUERY_PACKET;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500246 complete(&w8001->cmd_done);
247 break;
Peter Huttereraaba9332010-08-28 22:00:47 -0700248
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700249 /* 2 finger touch packet */
Peter Huttereraaba9332010-08-28 22:00:47 -0700250 case W8001_PKTLEN_TOUCH2FG - 1:
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700251 w8001->idx = 0;
252 parse_touch(w8001);
Peter Huttereraaba9332010-08-28 22:00:47 -0700253 break;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500254 }
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500255
256 return IRQ_HANDLED;
257}
258
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700259static int w8001_command(struct w8001 *w8001, unsigned char command,
260 bool wait_response)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500261{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700262 int rc;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500263
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700264 w8001->response_type = 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500265 init_completion(&w8001->cmd_done);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500266
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700267 rc = serio_write(w8001->serio, command);
268 if (rc == 0 && wait_response) {
269
270 wait_for_completion_timeout(&w8001->cmd_done, HZ);
271 if (w8001->response_type != W8001_QUERY_PACKET)
272 rc = -EIO;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500273 }
274
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500275 return rc;
276}
277
278static int w8001_setup(struct w8001 *w8001)
279{
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500280 struct input_dev *dev = w8001->dev;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700281 struct w8001_coord coord;
282 int error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500283
Ping Chenga6d38f82010-12-24 13:16:53 -0800284 error = w8001_command(w8001, W8001_CMD_STOP, false);
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700285 if (error)
286 return error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500287
Ping Chenga6d38f82010-12-24 13:16:53 -0800288 msleep(250); /* wait 250ms before querying the device */
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500289
Ping Chenga6d38f82010-12-24 13:16:53 -0800290 /* penabled? */
291 error = w8001_command(w8001, W8001_CMD_QUERY, true);
Peter Huttereraaba9332010-08-28 22:00:47 -0700292 if (!error) {
Ping Chenga6d38f82010-12-24 13:16:53 -0800293 __set_bit(BTN_TOOL_PEN, dev->keybit);
294 __set_bit(BTN_TOOL_RUBBER, dev->keybit);
295 __set_bit(BTN_STYLUS, dev->keybit);
296 __set_bit(BTN_STYLUS2, dev->keybit);
297 parse_data(w8001->response, &coord);
298
299 input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
300 input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
301 input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
302 if (coord.tilt_x && coord.tilt_y) {
303 input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
304 input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
305 }
306 }
307
308 /* Touch enabled? */
309 error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
310
311 /*
312 * Some non-touch devices may reply to the touch query. But their
313 * second byte is empty, which indicates touch is not supported.
314 */
315 if (!error && w8001->response[1]) {
Peter Huttereraaba9332010-08-28 22:00:47 -0700316 struct w8001_touch_query touch;
317
318 parse_touchquery(w8001->response, &touch);
319
Ping Chenga6d38f82010-12-24 13:16:53 -0800320 input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0);
321 input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0);
322 __set_bit(BTN_TOOL_FINGER, dev->keybit);
323
Peter Huttereraaba9332010-08-28 22:00:47 -0700324 switch (touch.sensor_id) {
325 case 0:
326 case 2:
327 w8001->pktlen = W8001_PKTLEN_TOUCH93;
328 break;
329 case 1:
330 case 3:
331 case 4:
332 w8001->pktlen = W8001_PKTLEN_TOUCH9A;
333 break;
334 case 5:
335 w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700336
Henrik Rydberg8cde8102010-11-27 10:50:54 +0100337 input_mt_init_slots(dev, 2);
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700338 input_set_abs_params(dev, ABS_MT_POSITION_X,
339 0, touch.x, 0, 0);
340 input_set_abs_params(dev, ABS_MT_POSITION_Y,
341 0, touch.y, 0, 0);
342 input_set_abs_params(dev, ABS_MT_TOOL_TYPE,
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100343 0, MT_TOOL_MAX, 0, 0);
Peter Huttereraaba9332010-08-28 22:00:47 -0700344 break;
345 }
346 }
347
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700348 return w8001_command(w8001, W8001_CMD_START, false);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500349}
350
351/*
352 * w8001_disconnect() is the opposite of w8001_connect()
353 */
354
355static void w8001_disconnect(struct serio *serio)
356{
357 struct w8001 *w8001 = serio_get_drvdata(serio);
358
359 input_get_device(w8001->dev);
360 input_unregister_device(w8001->dev);
361 serio_close(serio);
362 serio_set_drvdata(serio, NULL);
363 input_put_device(w8001->dev);
364 kfree(w8001);
365}
366
367/*
368 * w8001_connect() is the routine that is called when someone adds a
369 * new serio device that supports the w8001 protocol and registers it as
370 * an input device.
371 */
372
373static int w8001_connect(struct serio *serio, struct serio_driver *drv)
374{
375 struct w8001 *w8001;
376 struct input_dev *input_dev;
377 int err;
378
379 w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
380 input_dev = input_allocate_device();
381 if (!w8001 || !input_dev) {
382 err = -ENOMEM;
383 goto fail1;
384 }
385
386 w8001->serio = serio;
387 w8001->id = serio->id.id;
388 w8001->dev = input_dev;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500389 init_completion(&w8001->cmd_done);
390 snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
391
392 input_dev->name = "Wacom W8001 Penabled Serial TouchScreen";
393 input_dev->phys = w8001->phys;
394 input_dev->id.bustype = BUS_RS232;
395 input_dev->id.vendor = SERIO_W8001;
396 input_dev->id.product = w8001->id;
397 input_dev->id.version = 0x0100;
398 input_dev->dev.parent = &serio->dev;
399
400 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Ping Cheng202b6ca2010-12-24 13:13:25 -0800401 __set_bit(BTN_TOUCH, input_dev->keybit);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500402
403 serio_set_drvdata(serio, w8001);
404 err = serio_open(serio, drv);
405 if (err)
406 goto fail2;
407
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700408 err = w8001_setup(w8001);
409 if (err)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500410 goto fail3;
411
412 err = input_register_device(w8001->dev);
413 if (err)
414 goto fail3;
415
416 return 0;
417
418fail3:
419 serio_close(serio);
420fail2:
421 serio_set_drvdata(serio, NULL);
422fail1:
423 input_free_device(input_dev);
424 kfree(w8001);
425 return err;
426}
427
428static struct serio_device_id w8001_serio_ids[] = {
429 {
430 .type = SERIO_RS232,
431 .proto = SERIO_W8001,
432 .id = SERIO_ANY,
433 .extra = SERIO_ANY,
434 },
435 { 0 }
436};
437
438MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
439
440static struct serio_driver w8001_drv = {
441 .driver = {
442 .name = "w8001",
443 },
444 .description = DRIVER_DESC,
445 .id_table = w8001_serio_ids,
446 .interrupt = w8001_interrupt,
447 .connect = w8001_connect,
448 .disconnect = w8001_disconnect,
449};
450
451static int __init w8001_init(void)
452{
453 return serio_register_driver(&w8001_drv);
454}
455
456static void __exit w8001_exit(void)
457{
458 serio_unregister_driver(&w8001_drv);
459}
460
461module_init(w8001_init);
462module_exit(w8001_exit);