blob: 89abda06db740c9edaf294cf5970555d775f5616 [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>
18#include <linux/input.h>
19#include <linux/serio.h>
20#include <linux/init.h>
21#include <linux/ctype.h>
22
23#define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
24
25MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
26MODULE_DESCRIPTION(DRIVER_DESC);
27MODULE_LICENSE("GPL");
28
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050029#define W8001_MAX_LENGTH 11
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070030#define W8001_LEAD_MASK 0x80
31#define W8001_LEAD_BYTE 0x80
32#define W8001_TAB_MASK 0x40
33#define W8001_TAB_BYTE 0x40
Peter Huttereraaba9332010-08-28 22:00:47 -070034/* set in first byte of touch data packets */
35#define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
36#define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050037
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070038#define W8001_QUERY_PACKET 0x20
39
40#define W8001_CMD_START '1'
41#define W8001_CMD_QUERY '*'
Peter Huttereraaba9332010-08-28 22:00:47 -070042#define W8001_CMD_TOUCHQUERY '%'
43
44/* length of data packets in bytes, depends on device. */
45#define W8001_PKTLEN_TOUCH93 5
46#define W8001_PKTLEN_TOUCH9A 7
47#define W8001_PKTLEN_TPCPEN 9
48#define W8001_PKTLEN_TPCCTL 11 /* control packet */
49#define W8001_PKTLEN_TOUCH2FG 13
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050050
51struct w8001_coord {
52 u8 rdy;
53 u8 tsw;
54 u8 f1;
55 u8 f2;
56 u16 x;
57 u16 y;
58 u16 pen_pressure;
59 u8 tilt_x;
60 u8 tilt_y;
61};
62
Peter Huttereraaba9332010-08-28 22:00:47 -070063/* touch query reply packet */
64struct w8001_touch_query {
65 u8 panel_res;
66 u8 capacity_res;
67 u8 sensor_id;
68 u16 x;
69 u16 y;
70};
71
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050072/*
73 * Per-touchscreen data.
74 */
75
76struct w8001 {
77 struct input_dev *dev;
78 struct serio *serio;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050079 struct completion cmd_done;
80 int id;
81 int idx;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070082 unsigned char response_type;
83 unsigned char response[W8001_MAX_LENGTH];
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050084 unsigned char data[W8001_MAX_LENGTH];
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050085 char phys[32];
Peter Hutterer2072f8d2010-08-28 22:00:05 -070086 int type;
Peter Huttereraaba9332010-08-28 22:00:47 -070087 unsigned int pktlen;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050088};
89
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070090static void parse_data(u8 *data, struct w8001_coord *coord)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050091{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070092 memset(coord, 0, sizeof(*coord));
93
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050094 coord->rdy = data[0] & 0x20;
95 coord->tsw = data[0] & 0x01;
96 coord->f1 = data[0] & 0x02;
97 coord->f2 = data[0] & 0x04;
98
99 coord->x = (data[1] & 0x7F) << 9;
100 coord->x |= (data[2] & 0x7F) << 2;
101 coord->x |= (data[6] & 0x60) >> 5;
102
103 coord->y = (data[3] & 0x7F) << 9;
104 coord->y |= (data[4] & 0x7F) << 2;
105 coord->y |= (data[6] & 0x18) >> 3;
106
107 coord->pen_pressure = data[5] & 0x7F;
108 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
109
110 coord->tilt_x = data[7] & 0x7F;
111 coord->tilt_y = data[8] & 0x7F;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500112}
113
Peter Huttereraaba9332010-08-28 22:00:47 -0700114static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
115{
116 memset(query, 0, sizeof(*query));
117
118 query->panel_res = data[1];
119 query->sensor_id = data[2] & 0x7;
120 query->capacity_res = data[7];
121
122 query->x = data[3] << 9;
123 query->x |= data[4] << 2;
124 query->x |= (data[2] >> 5) & 0x3;
125
126 query->y = data[5] << 9;
127 query->y |= data[6] << 2;
128 query->y |= (data[2] >> 3) & 0x3;
129}
130
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700131static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
132{
133 struct input_dev *dev = w8001->dev;
134
135 /*
136 * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
137 * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
138 * or eraser. assume
139 * - if dev is already in proximity and f2 is toggled → pen + side2
140 * - if dev comes into proximity with f2 set → eraser
141 * If f2 disappears after assuming eraser, fake proximity out for
142 * eraser and in for pen.
143 */
144
145 if (!w8001->type) {
146 w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
147 } else if (w8001->type == BTN_TOOL_RUBBER) {
148 if (!coord->f2) {
149 input_report_abs(dev, ABS_PRESSURE, 0);
150 input_report_key(dev, BTN_TOUCH, 0);
151 input_report_key(dev, BTN_STYLUS, 0);
152 input_report_key(dev, BTN_STYLUS2, 0);
153 input_report_key(dev, BTN_TOOL_RUBBER, 0);
154 input_sync(dev);
155 w8001->type = BTN_TOOL_PEN;
156 }
157 } else {
158 input_report_key(dev, BTN_STYLUS2, coord->f2);
159 }
160
161 input_report_abs(dev, ABS_X, coord->x);
162 input_report_abs(dev, ABS_Y, coord->y);
163 input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure);
164 input_report_key(dev, BTN_TOUCH, coord->tsw);
165 input_report_key(dev, BTN_STYLUS, coord->f1);
166 input_report_key(dev, w8001->type, coord->rdy);
167 input_sync(dev);
168
169 if (!coord->rdy)
170 w8001->type = 0;
171}
172
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700173static irqreturn_t w8001_interrupt(struct serio *serio,
174 unsigned char data, unsigned int flags)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500175{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700176 struct w8001 *w8001 = serio_get_drvdata(serio);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500177 struct w8001_coord coord;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700178 unsigned char tmp;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500179
180 w8001->data[w8001->idx] = data;
181 switch (w8001->idx++) {
182 case 0:
183 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
184 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
185 w8001->idx = 0;
186 }
187 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700188
Peter Huttereraaba9332010-08-28 22:00:47 -0700189 case W8001_PKTLEN_TOUCH93 - 1:
190 case W8001_PKTLEN_TOUCH9A - 1:
191 /* ignore one-finger touch packet. */
192 if (w8001->pktlen == w8001->idx)
193 w8001->idx = 0;
194 break;
195
196 /* Pen coordinates packet */
197 case W8001_PKTLEN_TPCPEN - 1:
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500198 tmp = w8001->data[0] & W8001_TAB_MASK;
199 if (unlikely(tmp == W8001_TAB_BYTE))
200 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700201
Peter Huttereraaba9332010-08-28 22:00:47 -0700202 tmp = (w8001->data[0] & W8001_TOUCH_BYTE);
203 if (tmp == W8001_TOUCH_BYTE)
204 break;
205
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500206 w8001->idx = 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500207 parse_data(w8001->data, &coord);
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700208 report_pen_events(w8001, &coord);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500209 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700210
Peter Huttereraaba9332010-08-28 22:00:47 -0700211 /* control packet */
212 case W8001_PKTLEN_TPCCTL - 1:
213 tmp = (w8001->data[0] & W8001_TOUCH_MASK);
214 if (tmp == W8001_TOUCH_BYTE)
215 break;
216
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500217 w8001->idx = 0;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700218 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
219 w8001->response_type = W8001_QUERY_PACKET;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500220 complete(&w8001->cmd_done);
221 break;
Peter Huttereraaba9332010-08-28 22:00:47 -0700222
223 case W8001_PKTLEN_TOUCH2FG - 1:
224 /* ignore two-finger touch packet. */
225 if (w8001->pktlen == w8001->idx)
226 w8001->idx = 0;
227 break;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500228 }
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500229
230 return IRQ_HANDLED;
231}
232
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700233static int w8001_command(struct w8001 *w8001, unsigned char command,
234 bool wait_response)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500235{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700236 int rc;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500237
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700238 w8001->response_type = 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500239 init_completion(&w8001->cmd_done);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500240
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700241 rc = serio_write(w8001->serio, command);
242 if (rc == 0 && wait_response) {
243
244 wait_for_completion_timeout(&w8001->cmd_done, HZ);
245 if (w8001->response_type != W8001_QUERY_PACKET)
246 rc = -EIO;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500247 }
248
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500249 return rc;
250}
251
252static int w8001_setup(struct w8001 *w8001)
253{
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500254 struct input_dev *dev = w8001->dev;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700255 struct w8001_coord coord;
256 int error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500257
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700258 error = w8001_command(w8001, W8001_CMD_QUERY, true);
259 if (error)
260 return error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500261
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700262 parse_data(w8001->response, &coord);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500263
264 input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
265 input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
266 input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
267 input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
268 input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
269
Peter Huttereraaba9332010-08-28 22:00:47 -0700270 error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
271 if (!error) {
272 struct w8001_touch_query touch;
273
274 parse_touchquery(w8001->response, &touch);
275
276 switch (touch.sensor_id) {
277 case 0:
278 case 2:
279 w8001->pktlen = W8001_PKTLEN_TOUCH93;
280 break;
281 case 1:
282 case 3:
283 case 4:
284 w8001->pktlen = W8001_PKTLEN_TOUCH9A;
285 break;
286 case 5:
287 w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
288 break;
289 }
290 }
291
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700292 return w8001_command(w8001, W8001_CMD_START, false);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500293}
294
295/*
296 * w8001_disconnect() is the opposite of w8001_connect()
297 */
298
299static void w8001_disconnect(struct serio *serio)
300{
301 struct w8001 *w8001 = serio_get_drvdata(serio);
302
303 input_get_device(w8001->dev);
304 input_unregister_device(w8001->dev);
305 serio_close(serio);
306 serio_set_drvdata(serio, NULL);
307 input_put_device(w8001->dev);
308 kfree(w8001);
309}
310
311/*
312 * w8001_connect() is the routine that is called when someone adds a
313 * new serio device that supports the w8001 protocol and registers it as
314 * an input device.
315 */
316
317static int w8001_connect(struct serio *serio, struct serio_driver *drv)
318{
319 struct w8001 *w8001;
320 struct input_dev *input_dev;
321 int err;
322
323 w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
324 input_dev = input_allocate_device();
325 if (!w8001 || !input_dev) {
326 err = -ENOMEM;
327 goto fail1;
328 }
329
330 w8001->serio = serio;
331 w8001->id = serio->id.id;
332 w8001->dev = input_dev;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500333 init_completion(&w8001->cmd_done);
334 snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
335
336 input_dev->name = "Wacom W8001 Penabled Serial TouchScreen";
337 input_dev->phys = w8001->phys;
338 input_dev->id.bustype = BUS_RS232;
339 input_dev->id.vendor = SERIO_W8001;
340 input_dev->id.product = w8001->id;
341 input_dev->id.version = 0x0100;
342 input_dev->dev.parent = &serio->dev;
343
344 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
345 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700346 input_dev->keybit[BIT_WORD(BTN_TOOL_PEN)] |= BIT_MASK(BTN_TOOL_PEN);
347 input_dev->keybit[BIT_WORD(BTN_TOOL_RUBBER)] |= BIT_MASK(BTN_TOOL_RUBBER);
348 input_dev->keybit[BIT_WORD(BTN_STYLUS)] |= BIT_MASK(BTN_STYLUS);
349 input_dev->keybit[BIT_WORD(BTN_STYLUS2)] |= BIT_MASK(BTN_STYLUS2);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500350
351 serio_set_drvdata(serio, w8001);
352 err = serio_open(serio, drv);
353 if (err)
354 goto fail2;
355
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700356 err = w8001_setup(w8001);
357 if (err)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500358 goto fail3;
359
360 err = input_register_device(w8001->dev);
361 if (err)
362 goto fail3;
363
364 return 0;
365
366fail3:
367 serio_close(serio);
368fail2:
369 serio_set_drvdata(serio, NULL);
370fail1:
371 input_free_device(input_dev);
372 kfree(w8001);
373 return err;
374}
375
376static struct serio_device_id w8001_serio_ids[] = {
377 {
378 .type = SERIO_RS232,
379 .proto = SERIO_W8001,
380 .id = SERIO_ANY,
381 .extra = SERIO_ANY,
382 },
383 { 0 }
384};
385
386MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
387
388static struct serio_driver w8001_drv = {
389 .driver = {
390 .name = "w8001",
391 },
392 .description = DRIVER_DESC,
393 .id_table = w8001_serio_ids,
394 .interrupt = w8001_interrupt,
395 .connect = w8001_connect,
396 .disconnect = w8001_disconnect,
397};
398
399static int __init w8001_init(void)
400{
401 return serio_register_driver(&w8001_drv);
402}
403
404static void __exit w8001_exit(void)
405{
406 serio_unregister_driver(&w8001_drv);
407}
408
409module_init(w8001_init);
410module_exit(w8001_exit);