2 * Wacom W8001 penabled serial touchscreen driver
4 * Copyright (c) 2008 Jaya Kumar
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive for
10 * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/input.h>
18 #include <linux/serio.h>
19 #include <linux/init.h>
20 #include <linux/ctype.h>
22 #define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
24 MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
25 MODULE_DESCRIPTION(DRIVER_DESC);
26 MODULE_LICENSE("GPL");
29 * Definitions & global arrays.
32 #define W8001_MAX_LENGTH 11
33 #define W8001_PACKET_LEN 11
34 #define W8001_LEAD_MASK 0x80
35 #define W8001_LEAD_BYTE 0x80
36 #define W8001_TAB_MASK 0x40
37 #define W8001_TAB_BYTE 0x40
39 #define W8001_QUERY_PACKET 0x20
54 * Per-touchscreen data.
58 struct input_dev *dev;
60 struct mutex cmd_mutex;
61 struct completion cmd_done;
64 unsigned char expected_packet;
65 unsigned char data[W8001_MAX_LENGTH];
66 unsigned char response[W8001_PACKET_LEN];
70 static int parse_data(u8 *data, struct w8001_coord *coord)
72 coord->rdy = data[0] & 0x20;
73 coord->tsw = data[0] & 0x01;
74 coord->f1 = data[0] & 0x02;
75 coord->f2 = data[0] & 0x04;
77 coord->x = (data[1] & 0x7F) << 9;
78 coord->x |= (data[2] & 0x7F) << 2;
79 coord->x |= (data[6] & 0x60) >> 5;
81 coord->y = (data[3] & 0x7F) << 9;
82 coord->y |= (data[4] & 0x7F) << 2;
83 coord->y |= (data[6] & 0x18) >> 3;
85 coord->pen_pressure = data[5] & 0x7F;
86 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
88 coord->tilt_x = data[7] & 0x7F;
89 coord->tilt_y = data[8] & 0x7F;
94 static void w8001_process_data(struct w8001 *w8001, unsigned char data)
96 struct input_dev *dev = w8001->dev;
98 struct w8001_coord coord;
100 w8001->data[w8001->idx] = data;
101 switch (w8001->idx++) {
103 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
104 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
109 tmp = w8001->data[0] & W8001_TAB_MASK;
110 if (unlikely(tmp == W8001_TAB_BYTE))
113 memset(&coord, 0, sizeof(coord));
114 parse_data(w8001->data, &coord);
115 input_report_abs(dev, ABS_X, coord.x);
116 input_report_abs(dev, ABS_Y, coord.y);
117 input_report_abs(dev, ABS_PRESSURE, coord.pen_pressure);
118 input_report_key(dev, BTN_TOUCH, coord.tsw);
123 memcpy(w8001->response, &w8001->data, W8001_PACKET_LEN);
124 w8001->expected_packet = W8001_QUERY_PACKET;
125 complete(&w8001->cmd_done);
131 static irqreturn_t w8001_interrupt(struct serio *serio,
132 unsigned char data, unsigned int flags)
134 struct w8001 *w8001 = serio_get_drvdata(serio);
136 w8001_process_data(w8001, data);
141 static int w8001_async_command(struct w8001 *w8001, unsigned char *packet,
147 mutex_lock(&w8001->cmd_mutex);
149 for (i = 0; i < len; i++) {
150 if (serio_write(w8001->serio, packet[i]))
156 mutex_unlock(&w8001->cmd_mutex);
160 static int w8001_command(struct w8001 *w8001, unsigned char *packet, int len)
165 mutex_lock(&w8001->cmd_mutex);
167 serio_pause_rx(w8001->serio);
168 init_completion(&w8001->cmd_done);
169 serio_continue_rx(w8001->serio);
171 for (i = 0; i < len; i++) {
172 if (serio_write(w8001->serio, packet[i]))
176 wait_for_completion_timeout(&w8001->cmd_done, HZ);
178 if (w8001->expected_packet == W8001_QUERY_PACKET) {
179 /* We are back in reporting mode, the query was ACKed */
180 memcpy(packet, w8001->response, W8001_PACKET_LEN);
185 mutex_unlock(&w8001->cmd_mutex);
189 static int w8001_setup(struct w8001 *w8001)
191 struct w8001_coord coord;
192 struct input_dev *dev = w8001->dev;
193 unsigned char start[1] = { '1' };
194 unsigned char query[11] = { '*' };
196 if (w8001_command(w8001, query, 1))
199 memset(&coord, 0, sizeof(coord));
200 parse_data(query, &coord);
202 input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
203 input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
204 input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
205 input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
206 input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
208 if (w8001_async_command(w8001, start, 1))
215 * w8001_disconnect() is the opposite of w8001_connect()
218 static void w8001_disconnect(struct serio *serio)
220 struct w8001 *w8001 = serio_get_drvdata(serio);
222 input_get_device(w8001->dev);
223 input_unregister_device(w8001->dev);
225 serio_set_drvdata(serio, NULL);
226 input_put_device(w8001->dev);
231 * w8001_connect() is the routine that is called when someone adds a
232 * new serio device that supports the w8001 protocol and registers it as
236 static int w8001_connect(struct serio *serio, struct serio_driver *drv)
239 struct input_dev *input_dev;
242 w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
243 input_dev = input_allocate_device();
244 if (!w8001 || !input_dev) {
249 w8001->serio = serio;
250 w8001->id = serio->id.id;
251 w8001->dev = input_dev;
252 mutex_init(&w8001->cmd_mutex);
253 init_completion(&w8001->cmd_done);
254 snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
256 input_dev->name = "Wacom W8001 Penabled Serial TouchScreen";
257 input_dev->phys = w8001->phys;
258 input_dev->id.bustype = BUS_RS232;
259 input_dev->id.vendor = SERIO_W8001;
260 input_dev->id.product = w8001->id;
261 input_dev->id.version = 0x0100;
262 input_dev->dev.parent = &serio->dev;
264 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
265 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
267 serio_set_drvdata(serio, w8001);
268 err = serio_open(serio, drv);
272 if (w8001_setup(w8001))
275 err = input_register_device(w8001->dev);
284 serio_set_drvdata(serio, NULL);
286 input_free_device(input_dev);
291 static struct serio_device_id w8001_serio_ids[] = {
294 .proto = SERIO_W8001,
301 MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
303 static struct serio_driver w8001_drv = {
307 .description = DRIVER_DESC,
308 .id_table = w8001_serio_ids,
309 .interrupt = w8001_interrupt,
310 .connect = w8001_connect,
311 .disconnect = w8001_disconnect,
314 static int __init w8001_init(void)
316 return serio_register_driver(&w8001_drv);
319 static void __exit w8001_exit(void)
321 serio_unregister_driver(&w8001_drv);
324 module_init(w8001_init);
325 module_exit(w8001_exit);