blob: 56dc35c94bb12eb00102a0110044945574edbcf6 [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];
65};
66
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070067static void parse_data(u8 *data, struct w8001_coord *coord)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050068{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070069 memset(coord, 0, sizeof(*coord));
70
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050071 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;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050089}
90
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070091static irqreturn_t w8001_interrupt(struct serio *serio,
92 unsigned char data, unsigned int flags)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050093{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070094 struct w8001 *w8001 = serio_get_drvdata(serio);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050095 struct input_dev *dev = w8001->dev;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050096 struct w8001_coord coord;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070097 unsigned char tmp;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050098
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;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700107
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500108 case 8:
109 tmp = w8001->data[0] & W8001_TAB_MASK;
110 if (unlikely(tmp == W8001_TAB_BYTE))
111 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700112
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500113 w8001->idx = 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500114 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;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700121
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500122 case 10:
123 w8001->idx = 0;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700124 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
125 w8001->response_type = W8001_QUERY_PACKET;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500126 complete(&w8001->cmd_done);
127 break;
128 }
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500129
130 return IRQ_HANDLED;
131}
132
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700133static int w8001_command(struct w8001 *w8001, unsigned char command,
134 bool wait_response)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500135{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700136 int rc;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500137
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700138 w8001->response_type = 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500139 init_completion(&w8001->cmd_done);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500140
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700141 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;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500147 }
148
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500149 return rc;
150}
151
152static int w8001_setup(struct w8001 *w8001)
153{
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500154 struct input_dev *dev = w8001->dev;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700155 struct w8001_coord coord;
156 int error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500157
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700158 error = w8001_command(w8001, W8001_CMD_QUERY, true);
159 if (error)
160 return error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500161
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700162 parse_data(w8001->response, &coord);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500163
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
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700170 return w8001_command(w8001, W8001_CMD_START, false);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500171}
172
173/*
174 * w8001_disconnect() is the opposite of w8001_connect()
175 */
176
177static 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
195static 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;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500211 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
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700230 err = w8001_setup(w8001);
231 if (err)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500232 goto fail3;
233
234 err = input_register_device(w8001->dev);
235 if (err)
236 goto fail3;
237
238 return 0;
239
240fail3:
241 serio_close(serio);
242fail2:
243 serio_set_drvdata(serio, NULL);
244fail1:
245 input_free_device(input_dev);
246 kfree(w8001);
247 return err;
248}
249
250static 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
260MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
261
262static 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
273static int __init w8001_init(void)
274{
275 return serio_register_driver(&w8001_drv);
276}
277
278static void __exit w8001_exit(void)
279{
280 serio_unregister_driver(&w8001_drv);
281}
282
283module_init(w8001_init);
284module_exit(w8001_exit);