blob: bc642f1be3767f4ed873af47522dc3b658b1b5ab [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
5 *
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
8 * more details.
9 *
10 * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
11 */
12
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>
21
22#define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
23
24MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
25MODULE_DESCRIPTION(DRIVER_DESC);
26MODULE_LICENSE("GPL");
27
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050028#define W8001_MAX_LENGTH 11
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070029#define W8001_LEAD_MASK 0x80
30#define W8001_LEAD_BYTE 0x80
31#define W8001_TAB_MASK 0x40
32#define W8001_TAB_BYTE 0x40
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050033
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070034#define W8001_QUERY_PACKET 0x20
35
36#define W8001_CMD_START '1'
37#define W8001_CMD_QUERY '*'
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050038
39struct w8001_coord {
40 u8 rdy;
41 u8 tsw;
42 u8 f1;
43 u8 f2;
44 u16 x;
45 u16 y;
46 u16 pen_pressure;
47 u8 tilt_x;
48 u8 tilt_y;
49};
50
51/*
52 * Per-touchscreen data.
53 */
54
55struct w8001 {
56 struct input_dev *dev;
57 struct serio *serio;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050058 struct completion cmd_done;
59 int id;
60 int idx;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070061 unsigned char response_type;
62 unsigned char response[W8001_MAX_LENGTH];
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050063 unsigned char data[W8001_MAX_LENGTH];
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050064 char phys[32];
Peter Hutterer2072f8d2010-08-28 22:00:05 -070065 int type;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050066};
67
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070068static void parse_data(u8 *data, struct w8001_coord *coord)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050069{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070070 memset(coord, 0, sizeof(*coord));
71
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050072 coord->rdy = data[0] & 0x20;
73 coord->tsw = data[0] & 0x01;
74 coord->f1 = data[0] & 0x02;
75 coord->f2 = data[0] & 0x04;
76
77 coord->x = (data[1] & 0x7F) << 9;
78 coord->x |= (data[2] & 0x7F) << 2;
79 coord->x |= (data[6] & 0x60) >> 5;
80
81 coord->y = (data[3] & 0x7F) << 9;
82 coord->y |= (data[4] & 0x7F) << 2;
83 coord->y |= (data[6] & 0x18) >> 3;
84
85 coord->pen_pressure = data[5] & 0x7F;
86 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
87
88 coord->tilt_x = data[7] & 0x7F;
89 coord->tilt_y = data[8] & 0x7F;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050090}
91
Peter Hutterer2072f8d2010-08-28 22:00:05 -070092static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
93{
94 struct input_dev *dev = w8001->dev;
95
96 /*
97 * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
98 * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
99 * or eraser. assume
100 * - if dev is already in proximity and f2 is toggled → pen + side2
101 * - if dev comes into proximity with f2 set → eraser
102 * If f2 disappears after assuming eraser, fake proximity out for
103 * eraser and in for pen.
104 */
105
106 if (!w8001->type) {
107 w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
108 } else if (w8001->type == BTN_TOOL_RUBBER) {
109 if (!coord->f2) {
110 input_report_abs(dev, ABS_PRESSURE, 0);
111 input_report_key(dev, BTN_TOUCH, 0);
112 input_report_key(dev, BTN_STYLUS, 0);
113 input_report_key(dev, BTN_STYLUS2, 0);
114 input_report_key(dev, BTN_TOOL_RUBBER, 0);
115 input_sync(dev);
116 w8001->type = BTN_TOOL_PEN;
117 }
118 } else {
119 input_report_key(dev, BTN_STYLUS2, coord->f2);
120 }
121
122 input_report_abs(dev, ABS_X, coord->x);
123 input_report_abs(dev, ABS_Y, coord->y);
124 input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure);
125 input_report_key(dev, BTN_TOUCH, coord->tsw);
126 input_report_key(dev, BTN_STYLUS, coord->f1);
127 input_report_key(dev, w8001->type, coord->rdy);
128 input_sync(dev);
129
130 if (!coord->rdy)
131 w8001->type = 0;
132}
133
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700134static irqreturn_t w8001_interrupt(struct serio *serio,
135 unsigned char data, unsigned int flags)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500136{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700137 struct w8001 *w8001 = serio_get_drvdata(serio);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500138 struct w8001_coord coord;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700139 unsigned char tmp;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500140
141 w8001->data[w8001->idx] = data;
142 switch (w8001->idx++) {
143 case 0:
144 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
145 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
146 w8001->idx = 0;
147 }
148 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700149
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500150 case 8:
151 tmp = w8001->data[0] & W8001_TAB_MASK;
152 if (unlikely(tmp == W8001_TAB_BYTE))
153 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700154
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500155 w8001->idx = 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500156 parse_data(w8001->data, &coord);
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700157 report_pen_events(w8001, &coord);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500158 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700159
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500160 case 10:
161 w8001->idx = 0;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700162 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
163 w8001->response_type = W8001_QUERY_PACKET;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500164 complete(&w8001->cmd_done);
165 break;
166 }
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500167
168 return IRQ_HANDLED;
169}
170
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700171static int w8001_command(struct w8001 *w8001, unsigned char command,
172 bool wait_response)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500173{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700174 int rc;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500175
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700176 w8001->response_type = 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500177 init_completion(&w8001->cmd_done);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500178
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700179 rc = serio_write(w8001->serio, command);
180 if (rc == 0 && wait_response) {
181
182 wait_for_completion_timeout(&w8001->cmd_done, HZ);
183 if (w8001->response_type != W8001_QUERY_PACKET)
184 rc = -EIO;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500185 }
186
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500187 return rc;
188}
189
190static int w8001_setup(struct w8001 *w8001)
191{
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500192 struct input_dev *dev = w8001->dev;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700193 struct w8001_coord coord;
194 int error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500195
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700196 error = w8001_command(w8001, W8001_CMD_QUERY, true);
197 if (error)
198 return error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500199
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700200 parse_data(w8001->response, &coord);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500201
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);
207
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700208 return w8001_command(w8001, W8001_CMD_START, false);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500209}
210
211/*
212 * w8001_disconnect() is the opposite of w8001_connect()
213 */
214
215static void w8001_disconnect(struct serio *serio)
216{
217 struct w8001 *w8001 = serio_get_drvdata(serio);
218
219 input_get_device(w8001->dev);
220 input_unregister_device(w8001->dev);
221 serio_close(serio);
222 serio_set_drvdata(serio, NULL);
223 input_put_device(w8001->dev);
224 kfree(w8001);
225}
226
227/*
228 * w8001_connect() is the routine that is called when someone adds a
229 * new serio device that supports the w8001 protocol and registers it as
230 * an input device.
231 */
232
233static int w8001_connect(struct serio *serio, struct serio_driver *drv)
234{
235 struct w8001 *w8001;
236 struct input_dev *input_dev;
237 int err;
238
239 w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
240 input_dev = input_allocate_device();
241 if (!w8001 || !input_dev) {
242 err = -ENOMEM;
243 goto fail1;
244 }
245
246 w8001->serio = serio;
247 w8001->id = serio->id.id;
248 w8001->dev = input_dev;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500249 init_completion(&w8001->cmd_done);
250 snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
251
252 input_dev->name = "Wacom W8001 Penabled Serial TouchScreen";
253 input_dev->phys = w8001->phys;
254 input_dev->id.bustype = BUS_RS232;
255 input_dev->id.vendor = SERIO_W8001;
256 input_dev->id.product = w8001->id;
257 input_dev->id.version = 0x0100;
258 input_dev->dev.parent = &serio->dev;
259
260 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
261 input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700262 input_dev->keybit[BIT_WORD(BTN_TOOL_PEN)] |= BIT_MASK(BTN_TOOL_PEN);
263 input_dev->keybit[BIT_WORD(BTN_TOOL_RUBBER)] |= BIT_MASK(BTN_TOOL_RUBBER);
264 input_dev->keybit[BIT_WORD(BTN_STYLUS)] |= BIT_MASK(BTN_STYLUS);
265 input_dev->keybit[BIT_WORD(BTN_STYLUS2)] |= BIT_MASK(BTN_STYLUS2);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500266
267 serio_set_drvdata(serio, w8001);
268 err = serio_open(serio, drv);
269 if (err)
270 goto fail2;
271
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700272 err = w8001_setup(w8001);
273 if (err)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500274 goto fail3;
275
276 err = input_register_device(w8001->dev);
277 if (err)
278 goto fail3;
279
280 return 0;
281
282fail3:
283 serio_close(serio);
284fail2:
285 serio_set_drvdata(serio, NULL);
286fail1:
287 input_free_device(input_dev);
288 kfree(w8001);
289 return err;
290}
291
292static struct serio_device_id w8001_serio_ids[] = {
293 {
294 .type = SERIO_RS232,
295 .proto = SERIO_W8001,
296 .id = SERIO_ANY,
297 .extra = SERIO_ANY,
298 },
299 { 0 }
300};
301
302MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
303
304static struct serio_driver w8001_drv = {
305 .driver = {
306 .name = "w8001",
307 },
308 .description = DRIVER_DESC,
309 .id_table = w8001_serio_ids,
310 .interrupt = w8001_interrupt,
311 .connect = w8001_connect,
312 .disconnect = w8001_disconnect,
313};
314
315static int __init w8001_init(void)
316{
317 return serio_register_driver(&w8001_drv);
318}
319
320static void __exit w8001_exit(void)
321{
322 serio_unregister_driver(&w8001_drv);
323}
324
325module_init(w8001_init);
326module_exit(w8001_exit);