2 * Wacom W8001 penabled serial touchscreen driver
4 * Copyright (c) 2008 Jaya Kumar
5 * Copyright (c) 2010 Red Hat, Inc.
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
11 * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
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>
23 #define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
25 MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
26 MODULE_DESCRIPTION(DRIVER_DESC);
27 MODULE_LICENSE("GPL");
29 #define W8001_MAX_LENGTH 11
30 #define W8001_LEAD_MASK 0x80
31 #define W8001_LEAD_BYTE 0x80
32 #define W8001_TAB_MASK 0x40
33 #define W8001_TAB_BYTE 0x40
34 /* 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)
38 #define W8001_QUERY_PACKET 0x20
40 #define W8001_CMD_START '1'
41 #define W8001_CMD_QUERY '*'
42 #define W8001_CMD_TOUCHQUERY '%'
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
63 /* touch query reply packet */
64 struct w8001_touch_query {
73 * Per-touchscreen data.
77 struct input_dev *dev;
79 struct completion cmd_done;
82 unsigned char response_type;
83 unsigned char response[W8001_MAX_LENGTH];
84 unsigned char data[W8001_MAX_LENGTH];
90 static void parse_data(u8 *data, struct w8001_coord *coord)
92 memset(coord, 0, sizeof(*coord));
94 coord->rdy = data[0] & 0x20;
95 coord->tsw = data[0] & 0x01;
96 coord->f1 = data[0] & 0x02;
97 coord->f2 = data[0] & 0x04;
99 coord->x = (data[1] & 0x7F) << 9;
100 coord->x |= (data[2] & 0x7F) << 2;
101 coord->x |= (data[6] & 0x60) >> 5;
103 coord->y = (data[3] & 0x7F) << 9;
104 coord->y |= (data[4] & 0x7F) << 2;
105 coord->y |= (data[6] & 0x18) >> 3;
107 coord->pen_pressure = data[5] & 0x7F;
108 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
110 coord->tilt_x = data[7] & 0x7F;
111 coord->tilt_y = data[8] & 0x7F;
114 static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
116 memset(query, 0, sizeof(*query));
118 query->panel_res = data[1];
119 query->sensor_id = data[2] & 0x7;
120 query->capacity_res = data[7];
122 query->x = data[3] << 9;
123 query->x |= data[4] << 2;
124 query->x |= (data[2] >> 5) & 0x3;
126 query->y = data[5] << 9;
127 query->y |= data[6] << 2;
128 query->y |= (data[2] >> 3) & 0x3;
131 static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
133 struct input_dev *dev = w8001->dev;
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,
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.
146 w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
147 } else if (w8001->type == BTN_TOOL_RUBBER) {
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);
155 w8001->type = BTN_TOOL_PEN;
158 input_report_key(dev, BTN_STYLUS2, coord->f2);
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);
173 static irqreturn_t w8001_interrupt(struct serio *serio,
174 unsigned char data, unsigned int flags)
176 struct w8001 *w8001 = serio_get_drvdata(serio);
177 struct w8001_coord coord;
180 w8001->data[w8001->idx] = data;
181 switch (w8001->idx++) {
183 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
184 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
189 case W8001_PKTLEN_TOUCH93 - 1:
190 case W8001_PKTLEN_TOUCH9A - 1:
191 /* ignore one-finger touch packet. */
192 if (w8001->pktlen == w8001->idx)
196 /* Pen coordinates packet */
197 case W8001_PKTLEN_TPCPEN - 1:
198 tmp = w8001->data[0] & W8001_TAB_MASK;
199 if (unlikely(tmp == W8001_TAB_BYTE))
202 tmp = (w8001->data[0] & W8001_TOUCH_BYTE);
203 if (tmp == W8001_TOUCH_BYTE)
207 parse_data(w8001->data, &coord);
208 report_pen_events(w8001, &coord);
212 case W8001_PKTLEN_TPCCTL - 1:
213 tmp = (w8001->data[0] & W8001_TOUCH_MASK);
214 if (tmp == W8001_TOUCH_BYTE)
218 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
219 w8001->response_type = W8001_QUERY_PACKET;
220 complete(&w8001->cmd_done);
223 case W8001_PKTLEN_TOUCH2FG - 1:
224 /* ignore two-finger touch packet. */
225 if (w8001->pktlen == w8001->idx)
233 static int w8001_command(struct w8001 *w8001, unsigned char command,
238 w8001->response_type = 0;
239 init_completion(&w8001->cmd_done);
241 rc = serio_write(w8001->serio, command);
242 if (rc == 0 && wait_response) {
244 wait_for_completion_timeout(&w8001->cmd_done, HZ);
245 if (w8001->response_type != W8001_QUERY_PACKET)
252 static int w8001_setup(struct w8001 *w8001)
254 struct input_dev *dev = w8001->dev;
255 struct w8001_coord coord;
258 error = w8001_command(w8001, W8001_CMD_QUERY, true);
262 parse_data(w8001->response, &coord);
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);
270 error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
272 struct w8001_touch_query touch;
274 parse_touchquery(w8001->response, &touch);
276 switch (touch.sensor_id) {
279 w8001->pktlen = W8001_PKTLEN_TOUCH93;
284 w8001->pktlen = W8001_PKTLEN_TOUCH9A;
287 w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
292 return w8001_command(w8001, W8001_CMD_START, false);
296 * w8001_disconnect() is the opposite of w8001_connect()
299 static void w8001_disconnect(struct serio *serio)
301 struct w8001 *w8001 = serio_get_drvdata(serio);
303 input_get_device(w8001->dev);
304 input_unregister_device(w8001->dev);
306 serio_set_drvdata(serio, NULL);
307 input_put_device(w8001->dev);
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
317 static int w8001_connect(struct serio *serio, struct serio_driver *drv)
320 struct input_dev *input_dev;
323 w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
324 input_dev = input_allocate_device();
325 if (!w8001 || !input_dev) {
330 w8001->serio = serio;
331 w8001->id = serio->id.id;
332 w8001->dev = input_dev;
333 init_completion(&w8001->cmd_done);
334 snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
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;
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);
346 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);
351 serio_set_drvdata(serio, w8001);
352 err = serio_open(serio, drv);
356 err = w8001_setup(w8001);
360 err = input_register_device(w8001->dev);
369 serio_set_drvdata(serio, NULL);
371 input_free_device(input_dev);
376 static struct serio_device_id w8001_serio_ids[] = {
379 .proto = SERIO_W8001,
386 MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
388 static struct serio_driver w8001_drv = {
392 .description = DRIVER_DESC,
393 .id_table = w8001_serio_ids,
394 .interrupt = w8001_interrupt,
395 .connect = w8001_connect,
396 .disconnect = w8001_disconnect,
399 static int __init w8001_init(void)
401 return serio_register_driver(&w8001_drv);
404 static void __exit w8001_exit(void)
406 serio_unregister_driver(&w8001_drv);
409 module_init(w8001_init);
410 module_exit(w8001_exit);