Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/vegard...
[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  *
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
24 MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
25 MODULE_DESCRIPTION(DRIVER_DESC);
26 MODULE_LICENSE("GPL");
27
28 #define W8001_MAX_LENGTH        11
29 #define W8001_LEAD_MASK         0x80
30 #define W8001_LEAD_BYTE         0x80
31 #define W8001_TAB_MASK          0x40
32 #define W8001_TAB_BYTE          0x40
33
34 #define W8001_QUERY_PACKET      0x20
35
36 #define W8001_CMD_START         '1'
37 #define W8001_CMD_QUERY         '*'
38
39 struct 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
55 struct w8001 {
56         struct input_dev *dev;
57         struct serio *serio;
58         struct completion cmd_done;
59         int id;
60         int idx;
61         unsigned char response_type;
62         unsigned char response[W8001_MAX_LENGTH];
63         unsigned char data[W8001_MAX_LENGTH];
64         char phys[32];
65 };
66
67 static void parse_data(u8 *data, struct w8001_coord *coord)
68 {
69         memset(coord, 0, sizeof(*coord));
70
71         coord->rdy = data[0] & 0x20;
72         coord->tsw = data[0] & 0x01;
73         coord->f1 = data[0] & 0x02;
74         coord->f2 = data[0] & 0x04;
75
76         coord->x = (data[1] & 0x7F) << 9;
77         coord->x |= (data[2] & 0x7F) << 2;
78         coord->x |= (data[6] & 0x60) >> 5;
79
80         coord->y = (data[3] & 0x7F) << 9;
81         coord->y |= (data[4] & 0x7F) << 2;
82         coord->y |= (data[6] & 0x18) >> 3;
83
84         coord->pen_pressure = data[5] & 0x7F;
85         coord->pen_pressure |= (data[6] & 0x07) << 7 ;
86
87         coord->tilt_x = data[7] & 0x7F;
88         coord->tilt_y = data[8] & 0x7F;
89 }
90
91 static irqreturn_t w8001_interrupt(struct serio *serio,
92                                    unsigned char data, unsigned int flags)
93 {
94         struct w8001 *w8001 = serio_get_drvdata(serio);
95         struct input_dev *dev = w8001->dev;
96         struct w8001_coord coord;
97         unsigned char tmp;
98
99         w8001->data[w8001->idx] = data;
100         switch (w8001->idx++) {
101         case 0:
102                 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
103                         pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
104                         w8001->idx = 0;
105                 }
106                 break;
107
108         case 8:
109                 tmp = w8001->data[0] & W8001_TAB_MASK;
110                 if (unlikely(tmp == W8001_TAB_BYTE))
111                         break;
112
113                 w8001->idx = 0;
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);
119                 input_sync(dev);
120                 break;
121
122         case 10:
123                 w8001->idx = 0;
124                 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
125                 w8001->response_type = W8001_QUERY_PACKET;
126                 complete(&w8001->cmd_done);
127                 break;
128         }
129
130         return IRQ_HANDLED;
131 }
132
133 static int w8001_command(struct w8001 *w8001, unsigned char command,
134                          bool wait_response)
135 {
136         int rc;
137
138         w8001->response_type = 0;
139         init_completion(&w8001->cmd_done);
140
141         rc = serio_write(w8001->serio, command);
142         if (rc == 0 && wait_response) {
143
144                 wait_for_completion_timeout(&w8001->cmd_done, HZ);
145                 if (w8001->response_type != W8001_QUERY_PACKET)
146                         rc = -EIO;
147         }
148
149         return rc;
150 }
151
152 static int w8001_setup(struct w8001 *w8001)
153 {
154         struct input_dev *dev = w8001->dev;
155         struct w8001_coord coord;
156         int error;
157
158         error = w8001_command(w8001, W8001_CMD_QUERY, true);
159         if (error)
160                 return error;
161
162         parse_data(w8001->response, &coord);
163
164         input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
165         input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
166         input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
167         input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
168         input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
169
170         return w8001_command(w8001, W8001_CMD_START, false);
171 }
172
173 /*
174  * w8001_disconnect() is the opposite of w8001_connect()
175  */
176
177 static void w8001_disconnect(struct serio *serio)
178 {
179         struct w8001 *w8001 = serio_get_drvdata(serio);
180
181         input_get_device(w8001->dev);
182         input_unregister_device(w8001->dev);
183         serio_close(serio);
184         serio_set_drvdata(serio, NULL);
185         input_put_device(w8001->dev);
186         kfree(w8001);
187 }
188
189 /*
190  * w8001_connect() is the routine that is called when someone adds a
191  * new serio device that supports the w8001 protocol and registers it as
192  * an input device.
193  */
194
195 static int w8001_connect(struct serio *serio, struct serio_driver *drv)
196 {
197         struct w8001 *w8001;
198         struct input_dev *input_dev;
199         int err;
200
201         w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
202         input_dev = input_allocate_device();
203         if (!w8001 || !input_dev) {
204                 err = -ENOMEM;
205                 goto fail1;
206         }
207
208         w8001->serio = serio;
209         w8001->id = serio->id.id;
210         w8001->dev = input_dev;
211         init_completion(&w8001->cmd_done);
212         snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
213
214         input_dev->name = "Wacom W8001 Penabled Serial TouchScreen";
215         input_dev->phys = w8001->phys;
216         input_dev->id.bustype = BUS_RS232;
217         input_dev->id.vendor = SERIO_W8001;
218         input_dev->id.product = w8001->id;
219         input_dev->id.version = 0x0100;
220         input_dev->dev.parent = &serio->dev;
221
222         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
223         input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
224
225         serio_set_drvdata(serio, w8001);
226         err = serio_open(serio, drv);
227         if (err)
228                 goto fail2;
229
230         err = w8001_setup(w8001);
231         if (err)
232                 goto fail3;
233
234         err = input_register_device(w8001->dev);
235         if (err)
236                 goto fail3;
237
238         return 0;
239
240 fail3:
241         serio_close(serio);
242 fail2:
243         serio_set_drvdata(serio, NULL);
244 fail1:
245         input_free_device(input_dev);
246         kfree(w8001);
247         return err;
248 }
249
250 static struct serio_device_id w8001_serio_ids[] = {
251         {
252                 .type   = SERIO_RS232,
253                 .proto  = SERIO_W8001,
254                 .id     = SERIO_ANY,
255                 .extra  = SERIO_ANY,
256         },
257         { 0 }
258 };
259
260 MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
261
262 static struct serio_driver w8001_drv = {
263         .driver         = {
264                 .name   = "w8001",
265         },
266         .description    = DRIVER_DESC,
267         .id_table       = w8001_serio_ids,
268         .interrupt      = w8001_interrupt,
269         .connect        = w8001_connect,
270         .disconnect     = w8001_disconnect,
271 };
272
273 static int __init w8001_init(void)
274 {
275         return serio_register_driver(&w8001_drv);
276 }
277
278 static void __exit w8001_exit(void)
279 {
280         serio_unregister_driver(&w8001_drv);
281 }
282
283 module_init(w8001_init);
284 module_exit(w8001_exit);