blob: bab3c6acf6a228f13fcb44756b43374293bf030f [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
Peter Huttereraaba9332010-08-28 22:00:47 -07005 * Copyright (c) 2010 Red Hat, Inc.
Ping Cheng5fca6ca2011-01-11 01:06:58 -08006 * Copyright (c) 2010 - 2011 Ping Cheng, Wacom. <pingc@wacom.com>
Jaya Kumar3eb1aa42008-11-19 16:58:50 -05007 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive for
10 * more details.
11 *
12 * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
13 */
14
15#include <linux/errno.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/slab.h>
Henrik Rydberg47c78e82010-11-27 09:16:48 +010019#include <linux/input/mt.h>
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050020#include <linux/serio.h>
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050021#include <linux/ctype.h>
Ping Chenga6d38f82010-12-24 13:16:53 -080022#include <linux/delay.h>
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050023
24#define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
25
26MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
27MODULE_DESCRIPTION(DRIVER_DESC);
28MODULE_LICENSE("GPL");
29
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050030#define W8001_MAX_LENGTH 11
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070031#define W8001_LEAD_MASK 0x80
32#define W8001_LEAD_BYTE 0x80
33#define W8001_TAB_MASK 0x40
34#define W8001_TAB_BYTE 0x40
Peter Huttereraaba9332010-08-28 22:00:47 -070035/* set in first byte of touch data packets */
36#define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
37#define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050038
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070039#define W8001_QUERY_PACKET 0x20
40
Ping Chenga6d38f82010-12-24 13:16:53 -080041#define W8001_CMD_STOP '0'
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070042#define W8001_CMD_START '1'
43#define W8001_CMD_QUERY '*'
Peter Huttereraaba9332010-08-28 22:00:47 -070044#define W8001_CMD_TOUCHQUERY '%'
45
46/* length of data packets in bytes, depends on device. */
47#define W8001_PKTLEN_TOUCH93 5
48#define W8001_PKTLEN_TOUCH9A 7
49#define W8001_PKTLEN_TPCPEN 9
50#define W8001_PKTLEN_TPCCTL 11 /* control packet */
51#define W8001_PKTLEN_TOUCH2FG 13
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050052
Ping Cheng28a1bc12011-01-31 21:06:38 -080053/* resolution in points/mm */
54#define W8001_PEN_RESOLUTION 100
55#define W8001_TOUCH_RESOLUTION 10
56
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050057struct w8001_coord {
58 u8 rdy;
59 u8 tsw;
60 u8 f1;
61 u8 f2;
62 u16 x;
63 u16 y;
64 u16 pen_pressure;
65 u8 tilt_x;
66 u8 tilt_y;
67};
68
Peter Huttereraaba9332010-08-28 22:00:47 -070069/* touch query reply packet */
70struct w8001_touch_query {
Ping Cheng5fca6ca2011-01-11 01:06:58 -080071 u16 x;
72 u16 y;
Peter Huttereraaba9332010-08-28 22:00:47 -070073 u8 panel_res;
74 u8 capacity_res;
75 u8 sensor_id;
Peter Huttereraaba9332010-08-28 22:00:47 -070076};
77
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050078/*
79 * Per-touchscreen data.
80 */
81
82struct w8001 {
Peter Hutterere0361b72015-12-03 12:26:19 -080083 struct input_dev *pen_dev;
84 struct input_dev *touch_dev;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050085 struct serio *serio;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050086 struct completion cmd_done;
87 int id;
88 int idx;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -070089 unsigned char response_type;
90 unsigned char response[W8001_MAX_LENGTH];
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050091 unsigned char data[W8001_MAX_LENGTH];
Jaya Kumar3eb1aa42008-11-19 16:58:50 -050092 char phys[32];
Peter Hutterer2072f8d2010-08-28 22:00:05 -070093 int type;
Peter Huttereraaba9332010-08-28 22:00:47 -070094 unsigned int pktlen;
Ping Cheng5fca6ca2011-01-11 01:06:58 -080095 u16 max_touch_x;
96 u16 max_touch_y;
97 u16 max_pen_x;
98 u16 max_pen_y;
Peter Hutterere0361b72015-12-03 12:26:19 -080099 char pen_name[64];
100 char touch_name[64];
101 int open_count;
102 struct mutex mutex;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500103};
104
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800105static void parse_pen_data(u8 *data, struct w8001_coord *coord)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500106{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700107 memset(coord, 0, sizeof(*coord));
108
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500109 coord->rdy = data[0] & 0x20;
110 coord->tsw = data[0] & 0x01;
111 coord->f1 = data[0] & 0x02;
112 coord->f2 = data[0] & 0x04;
113
114 coord->x = (data[1] & 0x7F) << 9;
115 coord->x |= (data[2] & 0x7F) << 2;
116 coord->x |= (data[6] & 0x60) >> 5;
117
118 coord->y = (data[3] & 0x7F) << 9;
119 coord->y |= (data[4] & 0x7F) << 2;
120 coord->y |= (data[6] & 0x18) >> 3;
121
122 coord->pen_pressure = data[5] & 0x7F;
123 coord->pen_pressure |= (data[6] & 0x07) << 7 ;
124
125 coord->tilt_x = data[7] & 0x7F;
126 coord->tilt_y = data[8] & 0x7F;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500127}
128
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800129static void parse_single_touch(u8 *data, struct w8001_coord *coord)
130{
131 coord->x = (data[1] << 7) | data[2];
132 coord->y = (data[3] << 7) | data[4];
133 coord->tsw = data[0] & 0x01;
134}
135
136static void scale_touch_coordinates(struct w8001 *w8001,
137 unsigned int *x, unsigned int *y)
138{
139 if (w8001->max_pen_x && w8001->max_touch_x)
140 *x = *x * w8001->max_pen_x / w8001->max_touch_x;
141
142 if (w8001->max_pen_y && w8001->max_touch_y)
143 *y = *y * w8001->max_pen_y / w8001->max_touch_y;
144}
145
146static void parse_multi_touch(struct w8001 *w8001)
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700147{
Peter Hutterere0361b72015-12-03 12:26:19 -0800148 struct input_dev *dev = w8001->touch_dev;
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700149 unsigned char *data = w8001->data;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800150 unsigned int x, y;
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700151 int i;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800152 int count = 0;
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700153
154 for (i = 0; i < 2; i++) {
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100155 bool touch = data[0] & (1 << i);
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700156
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100157 input_mt_slot(dev, i);
Henrik Rydbergc5f4dec2010-12-15 13:50:34 +0100158 if (touch) {
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800159 x = (data[6 * i + 1] << 7) | data[6 * i + 2];
160 y = (data[6 * i + 3] << 7) | data[6 * i + 4];
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700161 /* data[5,6] and [11,12] is finger capacity */
162
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800163 /* scale to pen maximum */
164 scale_touch_coordinates(w8001, &x, &y);
165
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700166 input_report_abs(dev, ABS_MT_POSITION_X, x);
167 input_report_abs(dev, ABS_MT_POSITION_Y, y);
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800168 count++;
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700169 }
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700170 }
171
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800172 /* emulate single touch events when stylus is out of proximity.
173 * This is to make single touch backward support consistent
174 * across all Wacom single touch devices.
175 */
176 if (w8001->type != BTN_TOOL_PEN &&
177 w8001->type != BTN_TOOL_RUBBER) {
178 w8001->type = count == 1 ? BTN_TOOL_FINGER : KEY_RESERVED;
179 input_mt_report_pointer_emulation(dev, true);
180 }
181
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700182 input_sync(dev);
183}
184
Peter Huttereraaba9332010-08-28 22:00:47 -0700185static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
186{
187 memset(query, 0, sizeof(*query));
188
189 query->panel_res = data[1];
190 query->sensor_id = data[2] & 0x7;
191 query->capacity_res = data[7];
192
193 query->x = data[3] << 9;
194 query->x |= data[4] << 2;
195 query->x |= (data[2] >> 5) & 0x3;
196
197 query->y = data[5] << 9;
198 query->y |= data[6] << 2;
199 query->y |= (data[2] >> 3) & 0x3;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800200
201 /* Early days' single-finger touch models need the following defaults */
202 if (!query->x && !query->y) {
203 query->x = 1024;
204 query->y = 1024;
205 if (query->panel_res)
206 query->x = query->y = (1 << query->panel_res);
Ping Cheng28a1bc12011-01-31 21:06:38 -0800207 query->panel_res = W8001_TOUCH_RESOLUTION;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800208 }
Peter Huttereraaba9332010-08-28 22:00:47 -0700209}
210
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700211static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
212{
Peter Hutterere0361b72015-12-03 12:26:19 -0800213 struct input_dev *dev = w8001->pen_dev;
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700214
215 /*
216 * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
217 * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800218 * or eraser. Assume:
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700219 * - if dev is already in proximity and f2 is toggled → pen + side2
220 * - if dev comes into proximity with f2 set → eraser
221 * If f2 disappears after assuming eraser, fake proximity out for
222 * eraser and in for pen.
223 */
224
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800225 switch (w8001->type) {
226 case BTN_TOOL_RUBBER:
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700227 if (!coord->f2) {
228 input_report_abs(dev, ABS_PRESSURE, 0);
229 input_report_key(dev, BTN_TOUCH, 0);
230 input_report_key(dev, BTN_STYLUS, 0);
231 input_report_key(dev, BTN_STYLUS2, 0);
232 input_report_key(dev, BTN_TOOL_RUBBER, 0);
233 input_sync(dev);
234 w8001->type = BTN_TOOL_PEN;
235 }
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800236 break;
237
238 case BTN_TOOL_FINGER:
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800239 case KEY_RESERVED:
240 w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
241 break;
242
243 default:
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700244 input_report_key(dev, BTN_STYLUS2, coord->f2);
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800245 break;
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700246 }
247
248 input_report_abs(dev, ABS_X, coord->x);
249 input_report_abs(dev, ABS_Y, coord->y);
250 input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure);
251 input_report_key(dev, BTN_TOUCH, coord->tsw);
252 input_report_key(dev, BTN_STYLUS, coord->f1);
253 input_report_key(dev, w8001->type, coord->rdy);
254 input_sync(dev);
255
256 if (!coord->rdy)
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800257 w8001->type = KEY_RESERVED;
258}
259
260static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord)
261{
Peter Hutterere0361b72015-12-03 12:26:19 -0800262 struct input_dev *dev = w8001->touch_dev;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800263 unsigned int x = coord->x;
264 unsigned int y = coord->y;
265
266 /* scale to pen maximum */
267 scale_touch_coordinates(w8001, &x, &y);
268
269 input_report_abs(dev, ABS_X, x);
270 input_report_abs(dev, ABS_Y, y);
271 input_report_key(dev, BTN_TOUCH, coord->tsw);
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800272
273 input_sync(dev);
274
275 w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED;
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700276}
277
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700278static irqreturn_t w8001_interrupt(struct serio *serio,
279 unsigned char data, unsigned int flags)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500280{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700281 struct w8001 *w8001 = serio_get_drvdata(serio);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500282 struct w8001_coord coord;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700283 unsigned char tmp;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500284
285 w8001->data[w8001->idx] = data;
286 switch (w8001->idx++) {
287 case 0:
288 if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
289 pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
290 w8001->idx = 0;
291 }
292 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700293
Peter Huttereraaba9332010-08-28 22:00:47 -0700294 case W8001_PKTLEN_TOUCH93 - 1:
295 case W8001_PKTLEN_TOUCH9A - 1:
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800296 tmp = w8001->data[0] & W8001_TOUCH_BYTE;
297 if (tmp != W8001_TOUCH_BYTE)
298 break;
299
300 if (w8001->pktlen == w8001->idx) {
Peter Huttereraaba9332010-08-28 22:00:47 -0700301 w8001->idx = 0;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800302 if (w8001->type != BTN_TOOL_PEN &&
303 w8001->type != BTN_TOOL_RUBBER) {
304 parse_single_touch(w8001->data, &coord);
305 report_single_touch(w8001, &coord);
306 }
307 }
Peter Huttereraaba9332010-08-28 22:00:47 -0700308 break;
309
310 /* Pen coordinates packet */
311 case W8001_PKTLEN_TPCPEN - 1:
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500312 tmp = w8001->data[0] & W8001_TAB_MASK;
313 if (unlikely(tmp == W8001_TAB_BYTE))
314 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700315
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800316 tmp = w8001->data[0] & W8001_TOUCH_BYTE;
Peter Huttereraaba9332010-08-28 22:00:47 -0700317 if (tmp == W8001_TOUCH_BYTE)
318 break;
319
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500320 w8001->idx = 0;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800321 parse_pen_data(w8001->data, &coord);
Peter Hutterer2072f8d2010-08-28 22:00:05 -0700322 report_pen_events(w8001, &coord);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500323 break;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700324
Peter Huttereraaba9332010-08-28 22:00:47 -0700325 /* control packet */
326 case W8001_PKTLEN_TPCCTL - 1:
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800327 tmp = w8001->data[0] & W8001_TOUCH_MASK;
Peter Huttereraaba9332010-08-28 22:00:47 -0700328 if (tmp == W8001_TOUCH_BYTE)
329 break;
330
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500331 w8001->idx = 0;
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700332 memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
333 w8001->response_type = W8001_QUERY_PACKET;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500334 complete(&w8001->cmd_done);
335 break;
Peter Huttereraaba9332010-08-28 22:00:47 -0700336
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700337 /* 2 finger touch packet */
Peter Huttereraaba9332010-08-28 22:00:47 -0700338 case W8001_PKTLEN_TOUCH2FG - 1:
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700339 w8001->idx = 0;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800340 parse_multi_touch(w8001);
Peter Huttereraaba9332010-08-28 22:00:47 -0700341 break;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500342 }
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500343
344 return IRQ_HANDLED;
345}
346
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700347static int w8001_command(struct w8001 *w8001, unsigned char command,
348 bool wait_response)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500349{
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700350 int rc;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500351
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700352 w8001->response_type = 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500353 init_completion(&w8001->cmd_done);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500354
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700355 rc = serio_write(w8001->serio, command);
356 if (rc == 0 && wait_response) {
357
358 wait_for_completion_timeout(&w8001->cmd_done, HZ);
359 if (w8001->response_type != W8001_QUERY_PACKET)
360 rc = -EIO;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500361 }
362
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500363 return rc;
364}
365
Dmitry Torokhove9496742011-08-23 22:57:00 -0700366static int w8001_open(struct input_dev *dev)
367{
368 struct w8001 *w8001 = input_get_drvdata(dev);
Peter Hutterere0361b72015-12-03 12:26:19 -0800369 int err;
Dmitry Torokhove9496742011-08-23 22:57:00 -0700370
Peter Hutterere0361b72015-12-03 12:26:19 -0800371 err = mutex_lock_interruptible(&w8001->mutex);
372 if (err)
373 return err;
374
375 if (w8001->open_count++ == 0) {
376 err = w8001_command(w8001, W8001_CMD_START, false);
377 if (err)
378 w8001->open_count--;
379 }
380
381 mutex_unlock(&w8001->mutex);
382 return err;
Dmitry Torokhove9496742011-08-23 22:57:00 -0700383}
384
385static void w8001_close(struct input_dev *dev)
386{
387 struct w8001 *w8001 = input_get_drvdata(dev);
388
Peter Hutterere0361b72015-12-03 12:26:19 -0800389 mutex_lock(&w8001->mutex);
390
391 if (--w8001->open_count == 0)
392 w8001_command(w8001, W8001_CMD_STOP, false);
393
394 mutex_unlock(&w8001->mutex);
Dmitry Torokhove9496742011-08-23 22:57:00 -0700395}
396
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800397static int w8001_detect(struct w8001 *w8001)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500398{
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800399 int error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500400
Ping Chenga6d38f82010-12-24 13:16:53 -0800401 error = w8001_command(w8001, W8001_CMD_STOP, false);
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700402 if (error)
403 return error;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500404
Ping Chenga6d38f82010-12-24 13:16:53 -0800405 msleep(250); /* wait 250ms before querying the device */
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500406
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800407 return 0;
408}
409
Peter Hutterere0361b72015-12-03 12:26:19 -0800410static int w8001_setup_pen(struct w8001 *w8001, char *basename,
411 size_t basename_sz)
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800412{
Peter Hutterere0361b72015-12-03 12:26:19 -0800413 struct input_dev *dev = w8001->pen_dev;
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800414 struct w8001_coord coord;
415 int error;
416
Ping Chenga6d38f82010-12-24 13:16:53 -0800417 /* penabled? */
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800418 error = w8001_command(w8001, W8001_CMD_QUERY, true);
419 if (error)
420 return error;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800421
Peter Hutterere0361b72015-12-03 12:26:19 -0800422 __set_bit(EV_KEY, dev->evbit);
423 __set_bit(EV_ABS, dev->evbit);
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800424 __set_bit(BTN_TOUCH, dev->keybit);
425 __set_bit(BTN_TOOL_PEN, dev->keybit);
426 __set_bit(BTN_TOOL_RUBBER, dev->keybit);
427 __set_bit(BTN_STYLUS, dev->keybit);
428 __set_bit(BTN_STYLUS2, dev->keybit);
Peter Hutterere0361b72015-12-03 12:26:19 -0800429 __set_bit(INPUT_PROP_DIRECT, dev->propbit);
Ping Chenga6d38f82010-12-24 13:16:53 -0800430
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800431 parse_pen_data(w8001->response, &coord);
432 w8001->max_pen_x = coord.x;
433 w8001->max_pen_y = coord.y;
434
435 input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
436 input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
437 input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION);
438 input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION);
439 input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
440 if (coord.tilt_x && coord.tilt_y) {
441 input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
442 input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
Ping Chenga6d38f82010-12-24 13:16:53 -0800443 }
444
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800445 w8001->id = 0x90;
Peter Hutterere0361b72015-12-03 12:26:19 -0800446 strlcat(basename, " Penabled", basename_sz);
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800447
448 return 0;
449}
450
Peter Hutterere0361b72015-12-03 12:26:19 -0800451static int w8001_setup_touch(struct w8001 *w8001, char *basename,
452 size_t basename_sz)
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800453{
Peter Hutterere0361b72015-12-03 12:26:19 -0800454 struct input_dev *dev = w8001->touch_dev;
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800455 struct w8001_touch_query touch;
456 int error;
457
Peter Hutterere0361b72015-12-03 12:26:19 -0800458
Ping Chenga6d38f82010-12-24 13:16:53 -0800459 /* Touch enabled? */
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800460 error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
461 if (error)
462 return error;
463 /*
464 * Some non-touch devices may reply to the touch query. But their
465 * second byte is empty, which indicates touch is not supported.
466 */
467 if (!w8001->response[1])
468 return -ENXIO;
Ping Chenga6d38f82010-12-24 13:16:53 -0800469
Peter Hutterere0361b72015-12-03 12:26:19 -0800470 __set_bit(EV_KEY, dev->evbit);
471 __set_bit(EV_ABS, dev->evbit);
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800472 __set_bit(BTN_TOUCH, dev->keybit);
Peter Hutterere0361b72015-12-03 12:26:19 -0800473 __set_bit(INPUT_PROP_DIRECT, dev->propbit);
Peter Hutterere1717352015-12-03 12:24:26 -0800474
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800475 parse_touchquery(w8001->response, &touch);
476 w8001->max_touch_x = touch.x;
477 w8001->max_touch_y = touch.y;
Peter Huttereraaba9332010-08-28 22:00:47 -0700478
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800479 if (w8001->max_pen_x && w8001->max_pen_y) {
480 /* if pen is supported scale to pen maximum */
481 touch.x = w8001->max_pen_x;
482 touch.y = w8001->max_pen_y;
483 touch.panel_res = W8001_PEN_RESOLUTION;
484 }
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800485
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800486 input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0);
487 input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0);
488 input_abs_set_res(dev, ABS_X, touch.panel_res);
489 input_abs_set_res(dev, ABS_Y, touch.panel_res);
Peter Huttereraaba9332010-08-28 22:00:47 -0700490
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800491 switch (touch.sensor_id) {
492 case 0:
493 case 2:
494 w8001->pktlen = W8001_PKTLEN_TOUCH93;
495 w8001->id = 0x93;
Peter Hutterere0361b72015-12-03 12:26:19 -0800496 strlcat(basename, " 1FG", basename_sz);
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800497 break;
Ping Chenga6d38f82010-12-24 13:16:53 -0800498
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800499 case 1:
500 case 3:
501 case 4:
502 w8001->pktlen = W8001_PKTLEN_TOUCH9A;
Peter Hutterere0361b72015-12-03 12:26:19 -0800503 strlcat(basename, " 1FG", basename_sz);
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800504 w8001->id = 0x9a;
505 break;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800506
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800507 case 5:
508 w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800509
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800510 __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
511 input_mt_init_slots(dev, 2, 0);
512 input_set_abs_params(dev, ABS_MT_POSITION_X,
513 0, touch.x, 0, 0);
514 input_set_abs_params(dev, ABS_MT_POSITION_Y,
515 0, touch.y, 0, 0);
Peter Hutterer5e8b91402010-08-28 22:00:47 -0700516
Peter Hutterere0361b72015-12-03 12:26:19 -0800517 strlcat(basename, " 2FG", basename_sz);
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800518 if (w8001->max_pen_x && w8001->max_pen_y)
519 w8001->id = 0xE3;
520 else
521 w8001->id = 0xE2;
522 break;
Peter Huttereraaba9332010-08-28 22:00:47 -0700523 }
524
Peter Hutterere0361b72015-12-03 12:26:19 -0800525 strlcat(basename, " Touchscreen", basename_sz);
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800526
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800527 return 0;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500528}
529
Peter Hutterere0361b72015-12-03 12:26:19 -0800530static void w8001_set_devdata(struct input_dev *dev, struct w8001 *w8001,
531 struct serio *serio)
532{
533 dev->phys = w8001->phys;
534 dev->id.bustype = BUS_RS232;
535 dev->id.product = w8001->id;
536 dev->id.vendor = 0x056a;
537 dev->id.version = 0x0100;
538 dev->open = w8001_open;
539 dev->close = w8001_close;
540
541 dev->dev.parent = &serio->dev;
542
543 input_set_drvdata(dev, w8001);
544}
545
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500546/*
547 * w8001_disconnect() is the opposite of w8001_connect()
548 */
549
550static void w8001_disconnect(struct serio *serio)
551{
552 struct w8001 *w8001 = serio_get_drvdata(serio);
553
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500554 serio_close(serio);
Dmitry Torokhov66fd9382011-08-23 22:57:00 -0700555
Peter Hutterere0361b72015-12-03 12:26:19 -0800556 if (w8001->pen_dev)
557 input_unregister_device(w8001->pen_dev);
558 if (w8001->touch_dev)
559 input_unregister_device(w8001->touch_dev);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500560 kfree(w8001);
Dmitry Torokhov66fd9382011-08-23 22:57:00 -0700561
562 serio_set_drvdata(serio, NULL);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500563}
564
565/*
566 * w8001_connect() is the routine that is called when someone adds a
567 * new serio device that supports the w8001 protocol and registers it as
568 * an input device.
569 */
570
571static int w8001_connect(struct serio *serio, struct serio_driver *drv)
572{
573 struct w8001 *w8001;
Peter Hutterere0361b72015-12-03 12:26:19 -0800574 struct input_dev *input_dev_pen;
575 struct input_dev *input_dev_touch;
576 char basename[64];
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800577 int err, err_pen, err_touch;
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500578
579 w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
Peter Hutterere0361b72015-12-03 12:26:19 -0800580 input_dev_pen = input_allocate_device();
581 input_dev_touch = input_allocate_device();
582 if (!w8001 || !input_dev_pen || !input_dev_touch) {
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500583 err = -ENOMEM;
584 goto fail1;
585 }
586
587 w8001->serio = serio;
Peter Hutterere0361b72015-12-03 12:26:19 -0800588 w8001->pen_dev = input_dev_pen;
589 w8001->touch_dev = input_dev_touch;
590 mutex_init(&w8001->mutex);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500591 init_completion(&w8001->cmd_done);
592 snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
593
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500594 serio_set_drvdata(serio, w8001);
595 err = serio_open(serio, drv);
596 if (err)
597 goto fail2;
598
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800599 err = w8001_detect(w8001);
Dmitry Torokhov41c372d2009-09-03 17:22:03 -0700600 if (err)
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500601 goto fail3;
602
Peter Hutterere0361b72015-12-03 12:26:19 -0800603 /* For backwards-compatibility we compose the basename based on
604 * capabilities and then just append the tool type
605 */
606 strlcpy(basename, "Wacom Serial", sizeof(basename));
607
608 err_pen = w8001_setup_pen(w8001, basename, sizeof(basename));
609 err_touch = w8001_setup_touch(w8001, basename, sizeof(basename));
Peter Hutterer5d0a4fe2015-12-03 12:25:09 -0800610 if (err_pen && err_touch) {
611 err = -ENXIO;
612 goto fail3;
613 }
614
Peter Hutterere0361b72015-12-03 12:26:19 -0800615 if (!err_pen) {
616 strlcpy(w8001->pen_name, basename, sizeof(w8001->pen_name));
617 strlcat(w8001->pen_name, " Pen", sizeof(w8001->pen_name));
618 input_dev_pen->name = w8001->pen_name;
Ping Cheng5fca6ca2011-01-11 01:06:58 -0800619
Peter Hutterere0361b72015-12-03 12:26:19 -0800620 w8001_set_devdata(input_dev_pen, w8001, serio);
Dmitry Torokhove9496742011-08-23 22:57:00 -0700621
Peter Hutterere0361b72015-12-03 12:26:19 -0800622 err = input_register_device(w8001->pen_dev);
623 if (err)
624 goto fail3;
625 } else {
626 input_free_device(input_dev_pen);
627 input_dev_pen = NULL;
628 w8001->pen_dev = NULL;
629 }
Dmitry Torokhove9496742011-08-23 22:57:00 -0700630
Peter Hutterere0361b72015-12-03 12:26:19 -0800631 if (!err_touch) {
632 strlcpy(w8001->touch_name, basename, sizeof(w8001->touch_name));
633 strlcat(w8001->touch_name, " Finger",
634 sizeof(w8001->touch_name));
635 input_dev_touch->name = w8001->touch_name;
636
637 w8001_set_devdata(input_dev_touch, w8001, serio);
638
639 err = input_register_device(w8001->touch_dev);
640 if (err)
641 goto fail4;
642 } else {
643 input_free_device(input_dev_touch);
644 input_dev_touch = NULL;
645 w8001->touch_dev = NULL;
646 }
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500647
648 return 0;
649
Peter Hutterere0361b72015-12-03 12:26:19 -0800650fail4:
651 if (w8001->pen_dev)
652 input_unregister_device(w8001->pen_dev);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500653fail3:
654 serio_close(serio);
655fail2:
656 serio_set_drvdata(serio, NULL);
657fail1:
Peter Hutterere0361b72015-12-03 12:26:19 -0800658 input_free_device(input_dev_pen);
659 input_free_device(input_dev_touch);
Jaya Kumar3eb1aa42008-11-19 16:58:50 -0500660 kfree(w8001);
661 return err;
662}
663
664static struct serio_device_id w8001_serio_ids[] = {
665 {
666 .type = SERIO_RS232,
667 .proto = SERIO_W8001,
668 .id = SERIO_ANY,
669 .extra = SERIO_ANY,
670 },
671 { 0 }
672};
673
674MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
675
676static struct serio_driver w8001_drv = {
677 .driver = {
678 .name = "w8001",
679 },
680 .description = DRIVER_DESC,
681 .id_table = w8001_serio_ids,
682 .interrupt = w8001_interrupt,
683 .connect = w8001_connect,
684 .disconnect = w8001_disconnect,
685};
686
Axel Lin65ac9f72012-04-03 23:50:17 -0700687module_serio_driver(w8001_drv);