Input: wacom_w8001 - support (and ignore) touch tablets
[sfrench/cifs-2.6.git] / drivers / input / touchscreen / wacom_w8001.c
1 /*
2  * Wacom W8001 penabled serial touchscreen driver
3  *
4  * Copyright (c) 2008 Jaya Kumar
5  * Copyright (c) 2010 Red Hat, Inc.
6  *
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
25 MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
26 MODULE_DESCRIPTION(DRIVER_DESC);
27 MODULE_LICENSE("GPL");
28
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)
37
38 #define W8001_QUERY_PACKET      0x20
39
40 #define W8001_CMD_START         '1'
41 #define W8001_CMD_QUERY         '*'
42 #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
50
51 struct 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
63 /* touch query reply packet */
64 struct w8001_touch_query {
65         u8 panel_res;
66         u8 capacity_res;
67         u8 sensor_id;
68         u16 x;
69         u16 y;
70 };
71
72 /*
73  * Per-touchscreen data.
74  */
75
76 struct w8001 {
77         struct input_dev *dev;
78         struct serio *serio;
79         struct completion cmd_done;
80         int id;
81         int idx;
82         unsigned char response_type;
83         unsigned char response[W8001_MAX_LENGTH];
84         unsigned char data[W8001_MAX_LENGTH];
85         char phys[32];
86         int type;
87         unsigned int pktlen;
88 };
89
90 static void parse_data(u8 *data, struct w8001_coord *coord)
91 {
92         memset(coord, 0, sizeof(*coord));
93
94         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;
112 }
113
114 static 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
131 static 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
173 static irqreturn_t w8001_interrupt(struct serio *serio,
174                                    unsigned char data, unsigned int flags)
175 {
176         struct w8001 *w8001 = serio_get_drvdata(serio);
177         struct w8001_coord coord;
178         unsigned char tmp;
179
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;
188
189         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:
198                 tmp = w8001->data[0] & W8001_TAB_MASK;
199                 if (unlikely(tmp == W8001_TAB_BYTE))
200                         break;
201
202                 tmp = (w8001->data[0] & W8001_TOUCH_BYTE);
203                 if (tmp == W8001_TOUCH_BYTE)
204                         break;
205
206                 w8001->idx = 0;
207                 parse_data(w8001->data, &coord);
208                 report_pen_events(w8001, &coord);
209                 break;
210
211         /* 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
217                 w8001->idx = 0;
218                 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
219                 w8001->response_type = W8001_QUERY_PACKET;
220                 complete(&w8001->cmd_done);
221                 break;
222
223         case W8001_PKTLEN_TOUCH2FG - 1:
224                 /* ignore two-finger touch packet. */
225                 if (w8001->pktlen == w8001->idx)
226                         w8001->idx = 0;
227                 break;
228         }
229
230         return IRQ_HANDLED;
231 }
232
233 static int w8001_command(struct w8001 *w8001, unsigned char command,
234                          bool wait_response)
235 {
236         int rc;
237
238         w8001->response_type = 0;
239         init_completion(&w8001->cmd_done);
240
241         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;
247         }
248
249         return rc;
250 }
251
252 static int w8001_setup(struct w8001 *w8001)
253 {
254         struct input_dev *dev = w8001->dev;
255         struct w8001_coord coord;
256         int error;
257
258         error = w8001_command(w8001, W8001_CMD_QUERY, true);
259         if (error)
260                 return error;
261
262         parse_data(w8001->response, &coord);
263
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
270         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
292         return w8001_command(w8001, W8001_CMD_START, false);
293 }
294
295 /*
296  * w8001_disconnect() is the opposite of w8001_connect()
297  */
298
299 static 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
317 static 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;
333         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);
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);
350
351         serio_set_drvdata(serio, w8001);
352         err = serio_open(serio, drv);
353         if (err)
354                 goto fail2;
355
356         err = w8001_setup(w8001);
357         if (err)
358                 goto fail3;
359
360         err = input_register_device(w8001->dev);
361         if (err)
362                 goto fail3;
363
364         return 0;
365
366 fail3:
367         serio_close(serio);
368 fail2:
369         serio_set_drvdata(serio, NULL);
370 fail1:
371         input_free_device(input_dev);
372         kfree(w8001);
373         return err;
374 }
375
376 static 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
386 MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
387
388 static 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
399 static int __init w8001_init(void)
400 {
401         return serio_register_driver(&w8001_drv);
402 }
403
404 static void __exit w8001_exit(void)
405 {
406         serio_unregister_driver(&w8001_drv);
407 }
408
409 module_init(w8001_init);
410 module_exit(w8001_exit);