13eb1aa43SJaya Kumar /*
23eb1aa43SJaya Kumar * Wacom W8001 penabled serial touchscreen driver
33eb1aa43SJaya Kumar *
43eb1aa43SJaya Kumar * Copyright (c) 2008 Jaya Kumar
5aaba933eSPeter Hutterer * Copyright (c) 2010 Red Hat, Inc.
65fca6cacSPing Cheng * Copyright (c) 2010 - 2011 Ping Cheng, Wacom. <pingc@wacom.com>
73eb1aa43SJaya Kumar *
83eb1aa43SJaya Kumar * This file is subject to the terms and conditions of the GNU General Public
93eb1aa43SJaya Kumar * License. See the file COPYING in the main directory of this archive for
103eb1aa43SJaya Kumar * more details.
113eb1aa43SJaya Kumar *
123eb1aa43SJaya Kumar * Layout based on Elo serial touchscreen driver by Vojtech Pavlik
133eb1aa43SJaya Kumar */
143eb1aa43SJaya Kumar
153eb1aa43SJaya Kumar #include <linux/errno.h>
163eb1aa43SJaya Kumar #include <linux/kernel.h>
173eb1aa43SJaya Kumar #include <linux/module.h>
183eb1aa43SJaya Kumar #include <linux/slab.h>
1947c78e89SHenrik Rydberg #include <linux/input/mt.h>
203eb1aa43SJaya Kumar #include <linux/serio.h>
213eb1aa43SJaya Kumar #include <linux/ctype.h>
22a6d38f88SPing Cheng #include <linux/delay.h>
233eb1aa43SJaya Kumar
243eb1aa43SJaya Kumar #define DRIVER_DESC "Wacom W8001 serial touchscreen driver"
253eb1aa43SJaya Kumar
263eb1aa43SJaya Kumar MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>");
273eb1aa43SJaya Kumar MODULE_DESCRIPTION(DRIVER_DESC);
283eb1aa43SJaya Kumar MODULE_LICENSE("GPL");
293eb1aa43SJaya Kumar
302da9d2b5SJason Gerecke #define W8001_MAX_PHYS 42
312da9d2b5SJason Gerecke
3212afb344SPing Cheng #define W8001_MAX_LENGTH 13
333eb1aa43SJaya Kumar #define W8001_LEAD_MASK 0x80
343eb1aa43SJaya Kumar #define W8001_LEAD_BYTE 0x80
353eb1aa43SJaya Kumar #define W8001_TAB_MASK 0x40
363eb1aa43SJaya Kumar #define W8001_TAB_BYTE 0x40
37aaba933eSPeter Hutterer /* set in first byte of touch data packets */
38aaba933eSPeter Hutterer #define W8001_TOUCH_MASK (0x10 | W8001_LEAD_MASK)
39aaba933eSPeter Hutterer #define W8001_TOUCH_BYTE (0x10 | W8001_LEAD_BYTE)
403eb1aa43SJaya Kumar
413eb1aa43SJaya Kumar #define W8001_QUERY_PACKET 0x20
423eb1aa43SJaya Kumar
43a6d38f88SPing Cheng #define W8001_CMD_STOP '0'
4441c372dcSDmitry Torokhov #define W8001_CMD_START '1'
4541c372dcSDmitry Torokhov #define W8001_CMD_QUERY '*'
46aaba933eSPeter Hutterer #define W8001_CMD_TOUCHQUERY '%'
47aaba933eSPeter Hutterer
48aaba933eSPeter Hutterer /* length of data packets in bytes, depends on device. */
49aaba933eSPeter Hutterer #define W8001_PKTLEN_TOUCH93 5
50aaba933eSPeter Hutterer #define W8001_PKTLEN_TOUCH9A 7
51aaba933eSPeter Hutterer #define W8001_PKTLEN_TPCPEN 9
52aaba933eSPeter Hutterer #define W8001_PKTLEN_TPCCTL 11 /* control packet */
53aaba933eSPeter Hutterer #define W8001_PKTLEN_TOUCH2FG 13
5441c372dcSDmitry Torokhov
5528a1bc1cSPing Cheng /* resolution in points/mm */
5628a1bc1cSPing Cheng #define W8001_PEN_RESOLUTION 100
5728a1bc1cSPing Cheng #define W8001_TOUCH_RESOLUTION 10
5828a1bc1cSPing Cheng
593eb1aa43SJaya Kumar struct w8001_coord {
603eb1aa43SJaya Kumar u8 rdy;
613eb1aa43SJaya Kumar u8 tsw;
623eb1aa43SJaya Kumar u8 f1;
633eb1aa43SJaya Kumar u8 f2;
643eb1aa43SJaya Kumar u16 x;
653eb1aa43SJaya Kumar u16 y;
663eb1aa43SJaya Kumar u16 pen_pressure;
673eb1aa43SJaya Kumar u8 tilt_x;
683eb1aa43SJaya Kumar u8 tilt_y;
693eb1aa43SJaya Kumar };
703eb1aa43SJaya Kumar
71aaba933eSPeter Hutterer /* touch query reply packet */
72aaba933eSPeter Hutterer struct w8001_touch_query {
735fca6cacSPing Cheng u16 x;
745fca6cacSPing Cheng u16 y;
75aaba933eSPeter Hutterer u8 panel_res;
76aaba933eSPeter Hutterer u8 capacity_res;
77aaba933eSPeter Hutterer u8 sensor_id;
78aaba933eSPeter Hutterer };
79aaba933eSPeter Hutterer
803eb1aa43SJaya Kumar /*
813eb1aa43SJaya Kumar * Per-touchscreen data.
823eb1aa43SJaya Kumar */
833eb1aa43SJaya Kumar
843eb1aa43SJaya Kumar struct w8001 {
85e0361b70SPeter Hutterer struct input_dev *pen_dev;
86e0361b70SPeter Hutterer struct input_dev *touch_dev;
873eb1aa43SJaya Kumar struct serio *serio;
883eb1aa43SJaya Kumar struct completion cmd_done;
893eb1aa43SJaya Kumar int id;
903eb1aa43SJaya Kumar int idx;
9141c372dcSDmitry Torokhov unsigned char response_type;
9241c372dcSDmitry Torokhov unsigned char response[W8001_MAX_LENGTH];
933eb1aa43SJaya Kumar unsigned char data[W8001_MAX_LENGTH];
942da9d2b5SJason Gerecke char phys[W8001_MAX_PHYS];
952072f8dbSPeter Hutterer int type;
96aaba933eSPeter Hutterer unsigned int pktlen;
975fca6cacSPing Cheng u16 max_touch_x;
985fca6cacSPing Cheng u16 max_touch_y;
995fca6cacSPing Cheng u16 max_pen_x;
1005fca6cacSPing Cheng u16 max_pen_y;
101e0361b70SPeter Hutterer char pen_name[64];
102e0361b70SPeter Hutterer char touch_name[64];
103e0361b70SPeter Hutterer int open_count;
104e0361b70SPeter Hutterer struct mutex mutex;
1053eb1aa43SJaya Kumar };
1063eb1aa43SJaya Kumar
parse_pen_data(u8 * data,struct w8001_coord * coord)1075fca6cacSPing Cheng static void parse_pen_data(u8 *data, struct w8001_coord *coord)
1083eb1aa43SJaya Kumar {
10941c372dcSDmitry Torokhov memset(coord, 0, sizeof(*coord));
11041c372dcSDmitry Torokhov
1113eb1aa43SJaya Kumar coord->rdy = data[0] & 0x20;
1123eb1aa43SJaya Kumar coord->tsw = data[0] & 0x01;
1133eb1aa43SJaya Kumar coord->f1 = data[0] & 0x02;
1143eb1aa43SJaya Kumar coord->f2 = data[0] & 0x04;
1153eb1aa43SJaya Kumar
1163eb1aa43SJaya Kumar coord->x = (data[1] & 0x7F) << 9;
1173eb1aa43SJaya Kumar coord->x |= (data[2] & 0x7F) << 2;
1183eb1aa43SJaya Kumar coord->x |= (data[6] & 0x60) >> 5;
1193eb1aa43SJaya Kumar
1203eb1aa43SJaya Kumar coord->y = (data[3] & 0x7F) << 9;
1213eb1aa43SJaya Kumar coord->y |= (data[4] & 0x7F) << 2;
1223eb1aa43SJaya Kumar coord->y |= (data[6] & 0x18) >> 3;
1233eb1aa43SJaya Kumar
1243eb1aa43SJaya Kumar coord->pen_pressure = data[5] & 0x7F;
1253eb1aa43SJaya Kumar coord->pen_pressure |= (data[6] & 0x07) << 7 ;
1263eb1aa43SJaya Kumar
1273eb1aa43SJaya Kumar coord->tilt_x = data[7] & 0x7F;
1283eb1aa43SJaya Kumar coord->tilt_y = data[8] & 0x7F;
1293eb1aa43SJaya Kumar }
1303eb1aa43SJaya Kumar
parse_single_touch(u8 * data,struct w8001_coord * coord)1315fca6cacSPing Cheng static void parse_single_touch(u8 *data, struct w8001_coord *coord)
1325fca6cacSPing Cheng {
1335fca6cacSPing Cheng coord->x = (data[1] << 7) | data[2];
1345fca6cacSPing Cheng coord->y = (data[3] << 7) | data[4];
1355fca6cacSPing Cheng coord->tsw = data[0] & 0x01;
1365fca6cacSPing Cheng }
1375fca6cacSPing Cheng
scale_touch_coordinates(struct w8001 * w8001,unsigned int * x,unsigned int * y)1385fca6cacSPing Cheng static void scale_touch_coordinates(struct w8001 *w8001,
1395fca6cacSPing Cheng unsigned int *x, unsigned int *y)
1405fca6cacSPing Cheng {
1415fca6cacSPing Cheng if (w8001->max_pen_x && w8001->max_touch_x)
1425fca6cacSPing Cheng *x = *x * w8001->max_pen_x / w8001->max_touch_x;
1435fca6cacSPing Cheng
1445fca6cacSPing Cheng if (w8001->max_pen_y && w8001->max_touch_y)
1455fca6cacSPing Cheng *y = *y * w8001->max_pen_y / w8001->max_touch_y;
1465fca6cacSPing Cheng }
1475fca6cacSPing Cheng
parse_multi_touch(struct w8001 * w8001)1485fca6cacSPing Cheng static void parse_multi_touch(struct w8001 *w8001)
1495e8b9140SPeter Hutterer {
150e0361b70SPeter Hutterer struct input_dev *dev = w8001->touch_dev;
1515e8b9140SPeter Hutterer unsigned char *data = w8001->data;
1525fca6cacSPing Cheng unsigned int x, y;
1535e8b9140SPeter Hutterer int i;
1545fca6cacSPing Cheng int count = 0;
1555e8b9140SPeter Hutterer
1565e8b9140SPeter Hutterer for (i = 0; i < 2; i++) {
157c5f4dec1SHenrik Rydberg bool touch = data[0] & (1 << i);
1585e8b9140SPeter Hutterer
159c5f4dec1SHenrik Rydberg input_mt_slot(dev, i);
1603e9161bfSDmitry Torokhov input_mt_report_slot_state(dev, MT_TOOL_FINGER, touch);
161c5f4dec1SHenrik Rydberg if (touch) {
1625fca6cacSPing Cheng x = (data[6 * i + 1] << 7) | data[6 * i + 2];
1635fca6cacSPing Cheng y = (data[6 * i + 3] << 7) | data[6 * i + 4];
1645e8b9140SPeter Hutterer /* data[5,6] and [11,12] is finger capacity */
1655e8b9140SPeter Hutterer
1665fca6cacSPing Cheng /* scale to pen maximum */
1675fca6cacSPing Cheng scale_touch_coordinates(w8001, &x, &y);
1685fca6cacSPing Cheng
1695e8b9140SPeter Hutterer input_report_abs(dev, ABS_MT_POSITION_X, x);
1705e8b9140SPeter Hutterer input_report_abs(dev, ABS_MT_POSITION_Y, y);
1715fca6cacSPing Cheng count++;
1725e8b9140SPeter Hutterer }
1735e8b9140SPeter Hutterer }
1745e8b9140SPeter Hutterer
1755fca6cacSPing Cheng /* emulate single touch events when stylus is out of proximity.
1765fca6cacSPing Cheng * This is to make single touch backward support consistent
1775fca6cacSPing Cheng * across all Wacom single touch devices.
1785fca6cacSPing Cheng */
1795fca6cacSPing Cheng if (w8001->type != BTN_TOOL_PEN &&
1805fca6cacSPing Cheng w8001->type != BTN_TOOL_RUBBER) {
1815fca6cacSPing Cheng w8001->type = count == 1 ? BTN_TOOL_FINGER : KEY_RESERVED;
1825fca6cacSPing Cheng input_mt_report_pointer_emulation(dev, true);
1835fca6cacSPing Cheng }
1845fca6cacSPing Cheng
1855e8b9140SPeter Hutterer input_sync(dev);
1865e8b9140SPeter Hutterer }
1875e8b9140SPeter Hutterer
parse_touchquery(u8 * data,struct w8001_touch_query * query)188aaba933eSPeter Hutterer static void parse_touchquery(u8 *data, struct w8001_touch_query *query)
189aaba933eSPeter Hutterer {
190aaba933eSPeter Hutterer memset(query, 0, sizeof(*query));
191aaba933eSPeter Hutterer
192aaba933eSPeter Hutterer query->panel_res = data[1];
193aaba933eSPeter Hutterer query->sensor_id = data[2] & 0x7;
194aaba933eSPeter Hutterer query->capacity_res = data[7];
195aaba933eSPeter Hutterer
196aaba933eSPeter Hutterer query->x = data[3] << 9;
197aaba933eSPeter Hutterer query->x |= data[4] << 2;
198aaba933eSPeter Hutterer query->x |= (data[2] >> 5) & 0x3;
199aaba933eSPeter Hutterer
200aaba933eSPeter Hutterer query->y = data[5] << 9;
201aaba933eSPeter Hutterer query->y |= data[6] << 2;
202aaba933eSPeter Hutterer query->y |= (data[2] >> 3) & 0x3;
2035fca6cacSPing Cheng
2045fca6cacSPing Cheng /* Early days' single-finger touch models need the following defaults */
2055fca6cacSPing Cheng if (!query->x && !query->y) {
2065fca6cacSPing Cheng query->x = 1024;
2075fca6cacSPing Cheng query->y = 1024;
2085fca6cacSPing Cheng if (query->panel_res)
2095fca6cacSPing Cheng query->x = query->y = (1 << query->panel_res);
21028a1bc1cSPing Cheng query->panel_res = W8001_TOUCH_RESOLUTION;
2115fca6cacSPing Cheng }
212aaba933eSPeter Hutterer }
213aaba933eSPeter Hutterer
report_pen_events(struct w8001 * w8001,struct w8001_coord * coord)2142072f8dbSPeter Hutterer static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord)
2152072f8dbSPeter Hutterer {
216e0361b70SPeter Hutterer struct input_dev *dev = w8001->pen_dev;
2172072f8dbSPeter Hutterer
2182072f8dbSPeter Hutterer /*
2192072f8dbSPeter Hutterer * We have 1 bit for proximity (rdy) and 3 bits for tip, side,
2202072f8dbSPeter Hutterer * side2/eraser. If rdy && f2 are set, this can be either pen + side2,
2215fca6cacSPing Cheng * or eraser. Assume:
2222072f8dbSPeter Hutterer * - if dev is already in proximity and f2 is toggled → pen + side2
2232072f8dbSPeter Hutterer * - if dev comes into proximity with f2 set → eraser
2242072f8dbSPeter Hutterer * If f2 disappears after assuming eraser, fake proximity out for
2252072f8dbSPeter Hutterer * eraser and in for pen.
2262072f8dbSPeter Hutterer */
2272072f8dbSPeter Hutterer
2285fca6cacSPing Cheng switch (w8001->type) {
2295fca6cacSPing Cheng case BTN_TOOL_RUBBER:
2302072f8dbSPeter Hutterer if (!coord->f2) {
2312072f8dbSPeter Hutterer input_report_abs(dev, ABS_PRESSURE, 0);
2322072f8dbSPeter Hutterer input_report_key(dev, BTN_TOUCH, 0);
2332072f8dbSPeter Hutterer input_report_key(dev, BTN_STYLUS, 0);
2342072f8dbSPeter Hutterer input_report_key(dev, BTN_STYLUS2, 0);
2352072f8dbSPeter Hutterer input_report_key(dev, BTN_TOOL_RUBBER, 0);
2362072f8dbSPeter Hutterer input_sync(dev);
2372072f8dbSPeter Hutterer w8001->type = BTN_TOOL_PEN;
2382072f8dbSPeter Hutterer }
2395fca6cacSPing Cheng break;
2405fca6cacSPing Cheng
2415fca6cacSPing Cheng case BTN_TOOL_FINGER:
2425fca6cacSPing Cheng case KEY_RESERVED:
2435fca6cacSPing Cheng w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN;
2445fca6cacSPing Cheng break;
2455fca6cacSPing Cheng
2465fca6cacSPing Cheng default:
2472072f8dbSPeter Hutterer input_report_key(dev, BTN_STYLUS2, coord->f2);
2485fca6cacSPing Cheng break;
2492072f8dbSPeter Hutterer }
2502072f8dbSPeter Hutterer
2512072f8dbSPeter Hutterer input_report_abs(dev, ABS_X, coord->x);
2522072f8dbSPeter Hutterer input_report_abs(dev, ABS_Y, coord->y);
2532072f8dbSPeter Hutterer input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure);
2542072f8dbSPeter Hutterer input_report_key(dev, BTN_TOUCH, coord->tsw);
2552072f8dbSPeter Hutterer input_report_key(dev, BTN_STYLUS, coord->f1);
2562072f8dbSPeter Hutterer input_report_key(dev, w8001->type, coord->rdy);
2572072f8dbSPeter Hutterer input_sync(dev);
2582072f8dbSPeter Hutterer
2592072f8dbSPeter Hutterer if (!coord->rdy)
2605fca6cacSPing Cheng w8001->type = KEY_RESERVED;
2615fca6cacSPing Cheng }
2625fca6cacSPing Cheng
report_single_touch(struct w8001 * w8001,struct w8001_coord * coord)2635fca6cacSPing Cheng static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord)
2645fca6cacSPing Cheng {
265e0361b70SPeter Hutterer struct input_dev *dev = w8001->touch_dev;
2665fca6cacSPing Cheng unsigned int x = coord->x;
2675fca6cacSPing Cheng unsigned int y = coord->y;
2685fca6cacSPing Cheng
2695fca6cacSPing Cheng /* scale to pen maximum */
2705fca6cacSPing Cheng scale_touch_coordinates(w8001, &x, &y);
2715fca6cacSPing Cheng
2725fca6cacSPing Cheng input_report_abs(dev, ABS_X, x);
2735fca6cacSPing Cheng input_report_abs(dev, ABS_Y, y);
2745fca6cacSPing Cheng input_report_key(dev, BTN_TOUCH, coord->tsw);
2755fca6cacSPing Cheng
2765fca6cacSPing Cheng input_sync(dev);
2775fca6cacSPing Cheng
2785fca6cacSPing Cheng w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED;
2792072f8dbSPeter Hutterer }
2802072f8dbSPeter Hutterer
w8001_interrupt(struct serio * serio,unsigned char data,unsigned int flags)28141c372dcSDmitry Torokhov static irqreturn_t w8001_interrupt(struct serio *serio,
28241c372dcSDmitry Torokhov unsigned char data, unsigned int flags)
2833eb1aa43SJaya Kumar {
28441c372dcSDmitry Torokhov struct w8001 *w8001 = serio_get_drvdata(serio);
2853eb1aa43SJaya Kumar struct w8001_coord coord;
28641c372dcSDmitry Torokhov unsigned char tmp;
2873eb1aa43SJaya Kumar
2883eb1aa43SJaya Kumar w8001->data[w8001->idx] = data;
2893eb1aa43SJaya Kumar switch (w8001->idx++) {
2903eb1aa43SJaya Kumar case 0:
2913eb1aa43SJaya Kumar if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) {
2923eb1aa43SJaya Kumar pr_debug("w8001: unsynchronized data: 0x%02x\n", data);
2933eb1aa43SJaya Kumar w8001->idx = 0;
2943eb1aa43SJaya Kumar }
2953eb1aa43SJaya Kumar break;
29641c372dcSDmitry Torokhov
297aaba933eSPeter Hutterer case W8001_PKTLEN_TOUCH93 - 1:
298aaba933eSPeter Hutterer case W8001_PKTLEN_TOUCH9A - 1:
2995fca6cacSPing Cheng tmp = w8001->data[0] & W8001_TOUCH_BYTE;
3005fca6cacSPing Cheng if (tmp != W8001_TOUCH_BYTE)
3015fca6cacSPing Cheng break;
3025fca6cacSPing Cheng
3035fca6cacSPing Cheng if (w8001->pktlen == w8001->idx) {
304aaba933eSPeter Hutterer w8001->idx = 0;
3055fca6cacSPing Cheng if (w8001->type != BTN_TOOL_PEN &&
3065fca6cacSPing Cheng w8001->type != BTN_TOOL_RUBBER) {
3075fca6cacSPing Cheng parse_single_touch(w8001->data, &coord);
3085fca6cacSPing Cheng report_single_touch(w8001, &coord);
3095fca6cacSPing Cheng }
3105fca6cacSPing Cheng }
311aaba933eSPeter Hutterer break;
312aaba933eSPeter Hutterer
313aaba933eSPeter Hutterer /* Pen coordinates packet */
314aaba933eSPeter Hutterer case W8001_PKTLEN_TPCPEN - 1:
3153eb1aa43SJaya Kumar tmp = w8001->data[0] & W8001_TAB_MASK;
3163eb1aa43SJaya Kumar if (unlikely(tmp == W8001_TAB_BYTE))
3173eb1aa43SJaya Kumar break;
31841c372dcSDmitry Torokhov
3195fca6cacSPing Cheng tmp = w8001->data[0] & W8001_TOUCH_BYTE;
320aaba933eSPeter Hutterer if (tmp == W8001_TOUCH_BYTE)
321aaba933eSPeter Hutterer break;
322aaba933eSPeter Hutterer
3233eb1aa43SJaya Kumar w8001->idx = 0;
3245fca6cacSPing Cheng parse_pen_data(w8001->data, &coord);
3252072f8dbSPeter Hutterer report_pen_events(w8001, &coord);
3263eb1aa43SJaya Kumar break;
32741c372dcSDmitry Torokhov
328aaba933eSPeter Hutterer /* control packet */
329aaba933eSPeter Hutterer case W8001_PKTLEN_TPCCTL - 1:
3305fca6cacSPing Cheng tmp = w8001->data[0] & W8001_TOUCH_MASK;
331aaba933eSPeter Hutterer if (tmp == W8001_TOUCH_BYTE)
332aaba933eSPeter Hutterer break;
333aaba933eSPeter Hutterer
3343eb1aa43SJaya Kumar w8001->idx = 0;
33541c372dcSDmitry Torokhov memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH);
33641c372dcSDmitry Torokhov w8001->response_type = W8001_QUERY_PACKET;
3373eb1aa43SJaya Kumar complete(&w8001->cmd_done);
3383eb1aa43SJaya Kumar break;
339aaba933eSPeter Hutterer
3405e8b9140SPeter Hutterer /* 2 finger touch packet */
341aaba933eSPeter Hutterer case W8001_PKTLEN_TOUCH2FG - 1:
342aaba933eSPeter Hutterer w8001->idx = 0;
3435fca6cacSPing Cheng parse_multi_touch(w8001);
344aaba933eSPeter Hutterer break;
3459e72ac74SPing Cheng
3469e72ac74SPing Cheng default:
3479e72ac74SPing Cheng /*
3489e72ac74SPing Cheng * ThinkPad X60 Tablet PC (pen only device) sometimes
3499e72ac74SPing Cheng * sends invalid data packets that are larger than
3509e72ac74SPing Cheng * W8001_PKTLEN_TPCPEN. Let's start over again.
3519e72ac74SPing Cheng */
3529e72ac74SPing Cheng if (!w8001->touch_dev && w8001->idx > W8001_PKTLEN_TPCPEN - 1)
3539e72ac74SPing Cheng w8001->idx = 0;
3543eb1aa43SJaya Kumar }
3553eb1aa43SJaya Kumar
3563eb1aa43SJaya Kumar return IRQ_HANDLED;
3573eb1aa43SJaya Kumar }
3583eb1aa43SJaya Kumar
w8001_command(struct w8001 * w8001,unsigned char command,bool wait_response)35941c372dcSDmitry Torokhov static int w8001_command(struct w8001 *w8001, unsigned char command,
36041c372dcSDmitry Torokhov bool wait_response)
3613eb1aa43SJaya Kumar {
36241c372dcSDmitry Torokhov int rc;
3633eb1aa43SJaya Kumar
36441c372dcSDmitry Torokhov w8001->response_type = 0;
3653eb1aa43SJaya Kumar init_completion(&w8001->cmd_done);
3663eb1aa43SJaya Kumar
36741c372dcSDmitry Torokhov rc = serio_write(w8001->serio, command);
36841c372dcSDmitry Torokhov if (rc == 0 && wait_response) {
3693eb1aa43SJaya Kumar
3703eb1aa43SJaya Kumar wait_for_completion_timeout(&w8001->cmd_done, HZ);
37141c372dcSDmitry Torokhov if (w8001->response_type != W8001_QUERY_PACKET)
37241c372dcSDmitry Torokhov rc = -EIO;
3733eb1aa43SJaya Kumar }
3743eb1aa43SJaya Kumar
3753eb1aa43SJaya Kumar return rc;
3763eb1aa43SJaya Kumar }
3773eb1aa43SJaya Kumar
w8001_open(struct input_dev * dev)378e9496746SDmitry Torokhov static int w8001_open(struct input_dev *dev)
379e9496746SDmitry Torokhov {
380e9496746SDmitry Torokhov struct w8001 *w8001 = input_get_drvdata(dev);
381e0361b70SPeter Hutterer int err;
382e9496746SDmitry Torokhov
383e0361b70SPeter Hutterer err = mutex_lock_interruptible(&w8001->mutex);
384e0361b70SPeter Hutterer if (err)
385e0361b70SPeter Hutterer return err;
386e0361b70SPeter Hutterer
387e0361b70SPeter Hutterer if (w8001->open_count++ == 0) {
388e0361b70SPeter Hutterer err = w8001_command(w8001, W8001_CMD_START, false);
389e0361b70SPeter Hutterer if (err)
390e0361b70SPeter Hutterer w8001->open_count--;
391e0361b70SPeter Hutterer }
392e0361b70SPeter Hutterer
393e0361b70SPeter Hutterer mutex_unlock(&w8001->mutex);
394e0361b70SPeter Hutterer return err;
395e9496746SDmitry Torokhov }
396e9496746SDmitry Torokhov
w8001_close(struct input_dev * dev)397e9496746SDmitry Torokhov static void w8001_close(struct input_dev *dev)
398e9496746SDmitry Torokhov {
399e9496746SDmitry Torokhov struct w8001 *w8001 = input_get_drvdata(dev);
400e9496746SDmitry Torokhov
401e0361b70SPeter Hutterer mutex_lock(&w8001->mutex);
402e0361b70SPeter Hutterer
403e0361b70SPeter Hutterer if (--w8001->open_count == 0)
404e9496746SDmitry Torokhov w8001_command(w8001, W8001_CMD_STOP, false);
405e0361b70SPeter Hutterer
406e0361b70SPeter Hutterer mutex_unlock(&w8001->mutex);
407e9496746SDmitry Torokhov }
408e9496746SDmitry Torokhov
w8001_detect(struct w8001 * w8001)4095d0a4fe2SPeter Hutterer static int w8001_detect(struct w8001 *w8001)
4103eb1aa43SJaya Kumar {
4115d0a4fe2SPeter Hutterer int error;
4123eb1aa43SJaya Kumar
413a6d38f88SPing Cheng error = w8001_command(w8001, W8001_CMD_STOP, false);
41441c372dcSDmitry Torokhov if (error)
41541c372dcSDmitry Torokhov return error;
4163eb1aa43SJaya Kumar
417a6d38f88SPing Cheng msleep(250); /* wait 250ms before querying the device */
418a6d38f88SPing Cheng
4195d0a4fe2SPeter Hutterer return 0;
4205d0a4fe2SPeter Hutterer }
4215d0a4fe2SPeter Hutterer
w8001_setup_pen(struct w8001 * w8001,char * basename,size_t basename_sz)422e0361b70SPeter Hutterer static int w8001_setup_pen(struct w8001 *w8001, char *basename,
423e0361b70SPeter Hutterer size_t basename_sz)
4245d0a4fe2SPeter Hutterer {
425e0361b70SPeter Hutterer struct input_dev *dev = w8001->pen_dev;
4265d0a4fe2SPeter Hutterer struct w8001_coord coord;
4275d0a4fe2SPeter Hutterer int error;
4285d0a4fe2SPeter Hutterer
429a6d38f88SPing Cheng /* penabled? */
4305d0a4fe2SPeter Hutterer error = w8001_command(w8001, W8001_CMD_QUERY, true);
4315d0a4fe2SPeter Hutterer if (error)
4325d0a4fe2SPeter Hutterer return error;
4335d0a4fe2SPeter Hutterer
434e0361b70SPeter Hutterer __set_bit(EV_KEY, dev->evbit);
435e0361b70SPeter Hutterer __set_bit(EV_ABS, dev->evbit);
4365fca6cacSPing Cheng __set_bit(BTN_TOUCH, dev->keybit);
437a6d38f88SPing Cheng __set_bit(BTN_TOOL_PEN, dev->keybit);
438a6d38f88SPing Cheng __set_bit(BTN_TOOL_RUBBER, dev->keybit);
439a6d38f88SPing Cheng __set_bit(BTN_STYLUS, dev->keybit);
440a6d38f88SPing Cheng __set_bit(BTN_STYLUS2, dev->keybit);
441e0361b70SPeter Hutterer __set_bit(INPUT_PROP_DIRECT, dev->propbit);
4425fca6cacSPing Cheng
4435fca6cacSPing Cheng parse_pen_data(w8001->response, &coord);
4445fca6cacSPing Cheng w8001->max_pen_x = coord.x;
4455fca6cacSPing Cheng w8001->max_pen_y = coord.y;
4463eb1aa43SJaya Kumar
4473eb1aa43SJaya Kumar input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0);
4483eb1aa43SJaya Kumar input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0);
44928a1bc1cSPing Cheng input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION);
45028a1bc1cSPing Cheng input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION);
4513eb1aa43SJaya Kumar input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0);
452a6d38f88SPing Cheng if (coord.tilt_x && coord.tilt_y) {
4533eb1aa43SJaya Kumar input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0);
4543eb1aa43SJaya Kumar input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0);
455a6d38f88SPing Cheng }
4565d0a4fe2SPeter Hutterer
4575fca6cacSPing Cheng w8001->id = 0x90;
458e0361b70SPeter Hutterer strlcat(basename, " Penabled", basename_sz);
4595d0a4fe2SPeter Hutterer
4605d0a4fe2SPeter Hutterer return 0;
461a6d38f88SPing Cheng }
4623eb1aa43SJaya Kumar
w8001_setup_touch(struct w8001 * w8001,char * basename,size_t basename_sz)463e0361b70SPeter Hutterer static int w8001_setup_touch(struct w8001 *w8001, char *basename,
464e0361b70SPeter Hutterer size_t basename_sz)
4655d0a4fe2SPeter Hutterer {
466e0361b70SPeter Hutterer struct input_dev *dev = w8001->touch_dev;
4675d0a4fe2SPeter Hutterer struct w8001_touch_query touch;
4685d0a4fe2SPeter Hutterer int error;
4695d0a4fe2SPeter Hutterer
470e0361b70SPeter Hutterer
471a6d38f88SPing Cheng /* Touch enabled? */
4725d0a4fe2SPeter Hutterer error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true);
4735d0a4fe2SPeter Hutterer if (error)
4745d0a4fe2SPeter Hutterer return error;
4755d0a4fe2SPeter Hutterer /*
4765d0a4fe2SPeter Hutterer * Some non-touch devices may reply to the touch query. But their
4775d0a4fe2SPeter Hutterer * second byte is empty, which indicates touch is not supported.
4785d0a4fe2SPeter Hutterer */
4795d0a4fe2SPeter Hutterer if (!w8001->response[1])
4805d0a4fe2SPeter Hutterer return -ENXIO;
481a6d38f88SPing Cheng
482e0361b70SPeter Hutterer __set_bit(EV_KEY, dev->evbit);
483e0361b70SPeter Hutterer __set_bit(EV_ABS, dev->evbit);
4845fca6cacSPing Cheng __set_bit(BTN_TOUCH, dev->keybit);
485e0361b70SPeter Hutterer __set_bit(INPUT_PROP_DIRECT, dev->propbit);
486aaba933eSPeter Hutterer
487aaba933eSPeter Hutterer parse_touchquery(w8001->response, &touch);
4885fca6cacSPing Cheng w8001->max_touch_x = touch.x;
4895fca6cacSPing Cheng w8001->max_touch_y = touch.y;
4905fca6cacSPing Cheng
4915fca6cacSPing Cheng if (w8001->max_pen_x && w8001->max_pen_y) {
49228a1bc1cSPing Cheng /* if pen is supported scale to pen maximum */
4935fca6cacSPing Cheng touch.x = w8001->max_pen_x;
4945fca6cacSPing Cheng touch.y = w8001->max_pen_y;
49528a1bc1cSPing Cheng touch.panel_res = W8001_PEN_RESOLUTION;
4965fca6cacSPing Cheng }
497aaba933eSPeter Hutterer
498a6d38f88SPing Cheng input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0);
499a6d38f88SPing Cheng input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0);
50028a1bc1cSPing Cheng input_abs_set_res(dev, ABS_X, touch.panel_res);
50128a1bc1cSPing Cheng input_abs_set_res(dev, ABS_Y, touch.panel_res);
502a6d38f88SPing Cheng
503aaba933eSPeter Hutterer switch (touch.sensor_id) {
504aaba933eSPeter Hutterer case 0:
505aaba933eSPeter Hutterer case 2:
506aaba933eSPeter Hutterer w8001->pktlen = W8001_PKTLEN_TOUCH93;
5075fca6cacSPing Cheng w8001->id = 0x93;
508e0361b70SPeter Hutterer strlcat(basename, " 1FG", basename_sz);
509aaba933eSPeter Hutterer break;
5105fca6cacSPing Cheng
511aaba933eSPeter Hutterer case 1:
512aaba933eSPeter Hutterer case 3:
513aaba933eSPeter Hutterer case 4:
514aaba933eSPeter Hutterer w8001->pktlen = W8001_PKTLEN_TOUCH9A;
515e0361b70SPeter Hutterer strlcat(basename, " 1FG", basename_sz);
5165fca6cacSPing Cheng w8001->id = 0x9a;
517aaba933eSPeter Hutterer break;
5185fca6cacSPing Cheng
519aaba933eSPeter Hutterer case 5:
520aaba933eSPeter Hutterer w8001->pktlen = W8001_PKTLEN_TOUCH2FG;
5215e8b9140SPeter Hutterer
522ec9acda7SPeter Hutterer __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit);
5233746e567SPing Cheng error = input_mt_init_slots(dev, 2, 0);
5243746e567SPing Cheng if (error) {
5253746e567SPing Cheng dev_err(&w8001->serio->dev,
5263746e567SPing Cheng "failed to initialize MT slots: %d\n", error);
5273746e567SPing Cheng return error;
5283746e567SPing Cheng }
5293746e567SPing Cheng
5305e8b9140SPeter Hutterer input_set_abs_params(dev, ABS_MT_POSITION_X,
5315e8b9140SPeter Hutterer 0, touch.x, 0, 0);
5325e8b9140SPeter Hutterer input_set_abs_params(dev, ABS_MT_POSITION_Y,
5335e8b9140SPeter Hutterer 0, touch.y, 0, 0);
5343e9161bfSDmitry Torokhov input_set_abs_params(dev, ABS_MT_TOOL_TYPE,
5353e9161bfSDmitry Torokhov 0, MT_TOOL_MAX, 0, 0);
536ae10850cSPing Cheng input_abs_set_res(dev, ABS_MT_POSITION_X, touch.panel_res);
537ae10850cSPing Cheng input_abs_set_res(dev, ABS_MT_POSITION_Y, touch.panel_res);
5385fca6cacSPing Cheng
539e0361b70SPeter Hutterer strlcat(basename, " 2FG", basename_sz);
5405fca6cacSPing Cheng if (w8001->max_pen_x && w8001->max_pen_y)
5415fca6cacSPing Cheng w8001->id = 0xE3;
5425fca6cacSPing Cheng else
5435fca6cacSPing Cheng w8001->id = 0xE2;
544aaba933eSPeter Hutterer break;
545aaba933eSPeter Hutterer }
546aaba933eSPeter Hutterer
547e0361b70SPeter Hutterer strlcat(basename, " Touchscreen", basename_sz);
5485fca6cacSPing Cheng
5495d0a4fe2SPeter Hutterer return 0;
5503eb1aa43SJaya Kumar }
5513eb1aa43SJaya Kumar
w8001_set_devdata(struct input_dev * dev,struct w8001 * w8001,struct serio * serio)552e0361b70SPeter Hutterer static void w8001_set_devdata(struct input_dev *dev, struct w8001 *w8001,
553e0361b70SPeter Hutterer struct serio *serio)
554e0361b70SPeter Hutterer {
555e0361b70SPeter Hutterer dev->phys = w8001->phys;
556e0361b70SPeter Hutterer dev->id.bustype = BUS_RS232;
557e0361b70SPeter Hutterer dev->id.product = w8001->id;
558e0361b70SPeter Hutterer dev->id.vendor = 0x056a;
559e0361b70SPeter Hutterer dev->id.version = 0x0100;
560e0361b70SPeter Hutterer dev->open = w8001_open;
561e0361b70SPeter Hutterer dev->close = w8001_close;
562e0361b70SPeter Hutterer
563e0361b70SPeter Hutterer dev->dev.parent = &serio->dev;
564e0361b70SPeter Hutterer
565e0361b70SPeter Hutterer input_set_drvdata(dev, w8001);
566e0361b70SPeter Hutterer }
567e0361b70SPeter Hutterer
5683eb1aa43SJaya Kumar /*
5693eb1aa43SJaya Kumar * w8001_disconnect() is the opposite of w8001_connect()
5703eb1aa43SJaya Kumar */
5713eb1aa43SJaya Kumar
w8001_disconnect(struct serio * serio)5723eb1aa43SJaya Kumar static void w8001_disconnect(struct serio *serio)
5733eb1aa43SJaya Kumar {
5743eb1aa43SJaya Kumar struct w8001 *w8001 = serio_get_drvdata(serio);
5753eb1aa43SJaya Kumar
5763eb1aa43SJaya Kumar serio_close(serio);
57766fd9385SDmitry Torokhov
578e0361b70SPeter Hutterer if (w8001->pen_dev)
579e0361b70SPeter Hutterer input_unregister_device(w8001->pen_dev);
580e0361b70SPeter Hutterer if (w8001->touch_dev)
581e0361b70SPeter Hutterer input_unregister_device(w8001->touch_dev);
5823eb1aa43SJaya Kumar kfree(w8001);
58366fd9385SDmitry Torokhov
58466fd9385SDmitry Torokhov serio_set_drvdata(serio, NULL);
5853eb1aa43SJaya Kumar }
5863eb1aa43SJaya Kumar
5873eb1aa43SJaya Kumar /*
5883eb1aa43SJaya Kumar * w8001_connect() is the routine that is called when someone adds a
5893eb1aa43SJaya Kumar * new serio device that supports the w8001 protocol and registers it as
5903eb1aa43SJaya Kumar * an input device.
5913eb1aa43SJaya Kumar */
5923eb1aa43SJaya Kumar
w8001_connect(struct serio * serio,struct serio_driver * drv)5933eb1aa43SJaya Kumar static int w8001_connect(struct serio *serio, struct serio_driver *drv)
5943eb1aa43SJaya Kumar {
5953eb1aa43SJaya Kumar struct w8001 *w8001;
596e0361b70SPeter Hutterer struct input_dev *input_dev_pen;
597e0361b70SPeter Hutterer struct input_dev *input_dev_touch;
598e0361b70SPeter Hutterer char basename[64];
5995d0a4fe2SPeter Hutterer int err, err_pen, err_touch;
6003eb1aa43SJaya Kumar
6013eb1aa43SJaya Kumar w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL);
602e0361b70SPeter Hutterer input_dev_pen = input_allocate_device();
603e0361b70SPeter Hutterer input_dev_touch = input_allocate_device();
604e0361b70SPeter Hutterer if (!w8001 || !input_dev_pen || !input_dev_touch) {
6053eb1aa43SJaya Kumar err = -ENOMEM;
6063eb1aa43SJaya Kumar goto fail1;
6073eb1aa43SJaya Kumar }
6083eb1aa43SJaya Kumar
6093eb1aa43SJaya Kumar w8001->serio = serio;
610e0361b70SPeter Hutterer w8001->pen_dev = input_dev_pen;
611e0361b70SPeter Hutterer w8001->touch_dev = input_dev_touch;
612e0361b70SPeter Hutterer mutex_init(&w8001->mutex);
6133eb1aa43SJaya Kumar init_completion(&w8001->cmd_done);
6143eb1aa43SJaya Kumar snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys);
6153eb1aa43SJaya Kumar
6163eb1aa43SJaya Kumar serio_set_drvdata(serio, w8001);
6173eb1aa43SJaya Kumar err = serio_open(serio, drv);
6183eb1aa43SJaya Kumar if (err)
6193eb1aa43SJaya Kumar goto fail2;
6203eb1aa43SJaya Kumar
6215d0a4fe2SPeter Hutterer err = w8001_detect(w8001);
62241c372dcSDmitry Torokhov if (err)
6233eb1aa43SJaya Kumar goto fail3;
6243eb1aa43SJaya Kumar
625e0361b70SPeter Hutterer /* For backwards-compatibility we compose the basename based on
626e0361b70SPeter Hutterer * capabilities and then just append the tool type
627e0361b70SPeter Hutterer */
628*a9f08ad7SWolfram Sang strscpy(basename, "Wacom Serial", sizeof(basename));
629e0361b70SPeter Hutterer
630e0361b70SPeter Hutterer err_pen = w8001_setup_pen(w8001, basename, sizeof(basename));
631e0361b70SPeter Hutterer err_touch = w8001_setup_touch(w8001, basename, sizeof(basename));
6325d0a4fe2SPeter Hutterer if (err_pen && err_touch) {
6335d0a4fe2SPeter Hutterer err = -ENXIO;
6345d0a4fe2SPeter Hutterer goto fail3;
6355d0a4fe2SPeter Hutterer }
6365d0a4fe2SPeter Hutterer
637e0361b70SPeter Hutterer if (!err_pen) {
638*a9f08ad7SWolfram Sang strscpy(w8001->pen_name, basename, sizeof(w8001->pen_name));
639e0361b70SPeter Hutterer strlcat(w8001->pen_name, " Pen", sizeof(w8001->pen_name));
640e0361b70SPeter Hutterer input_dev_pen->name = w8001->pen_name;
6415fca6cacSPing Cheng
642e0361b70SPeter Hutterer w8001_set_devdata(input_dev_pen, w8001, serio);
643e9496746SDmitry Torokhov
644e0361b70SPeter Hutterer err = input_register_device(w8001->pen_dev);
6453eb1aa43SJaya Kumar if (err)
6463eb1aa43SJaya Kumar goto fail3;
647e0361b70SPeter Hutterer } else {
648e0361b70SPeter Hutterer input_free_device(input_dev_pen);
649e0361b70SPeter Hutterer input_dev_pen = NULL;
650e0361b70SPeter Hutterer w8001->pen_dev = NULL;
651e0361b70SPeter Hutterer }
652e0361b70SPeter Hutterer
653e0361b70SPeter Hutterer if (!err_touch) {
654*a9f08ad7SWolfram Sang strscpy(w8001->touch_name, basename, sizeof(w8001->touch_name));
655e0361b70SPeter Hutterer strlcat(w8001->touch_name, " Finger",
656e0361b70SPeter Hutterer sizeof(w8001->touch_name));
657e0361b70SPeter Hutterer input_dev_touch->name = w8001->touch_name;
658e0361b70SPeter Hutterer
659e0361b70SPeter Hutterer w8001_set_devdata(input_dev_touch, w8001, serio);
660e0361b70SPeter Hutterer
661e0361b70SPeter Hutterer err = input_register_device(w8001->touch_dev);
662e0361b70SPeter Hutterer if (err)
663e0361b70SPeter Hutterer goto fail4;
664e0361b70SPeter Hutterer } else {
665e0361b70SPeter Hutterer input_free_device(input_dev_touch);
666e0361b70SPeter Hutterer input_dev_touch = NULL;
667e0361b70SPeter Hutterer w8001->touch_dev = NULL;
668e0361b70SPeter Hutterer }
6693eb1aa43SJaya Kumar
6703eb1aa43SJaya Kumar return 0;
6713eb1aa43SJaya Kumar
672e0361b70SPeter Hutterer fail4:
673e0361b70SPeter Hutterer if (w8001->pen_dev)
674e0361b70SPeter Hutterer input_unregister_device(w8001->pen_dev);
6753eb1aa43SJaya Kumar fail3:
6763eb1aa43SJaya Kumar serio_close(serio);
6773eb1aa43SJaya Kumar fail2:
6783eb1aa43SJaya Kumar serio_set_drvdata(serio, NULL);
6793eb1aa43SJaya Kumar fail1:
680e0361b70SPeter Hutterer input_free_device(input_dev_pen);
681e0361b70SPeter Hutterer input_free_device(input_dev_touch);
6823eb1aa43SJaya Kumar kfree(w8001);
6833eb1aa43SJaya Kumar return err;
6843eb1aa43SJaya Kumar }
6853eb1aa43SJaya Kumar
686663c8b55SArvind Yadav static const struct serio_device_id w8001_serio_ids[] = {
6873eb1aa43SJaya Kumar {
6883eb1aa43SJaya Kumar .type = SERIO_RS232,
6893eb1aa43SJaya Kumar .proto = SERIO_W8001,
6903eb1aa43SJaya Kumar .id = SERIO_ANY,
6913eb1aa43SJaya Kumar .extra = SERIO_ANY,
6923eb1aa43SJaya Kumar },
6933eb1aa43SJaya Kumar { 0 }
6943eb1aa43SJaya Kumar };
6953eb1aa43SJaya Kumar
6963eb1aa43SJaya Kumar MODULE_DEVICE_TABLE(serio, w8001_serio_ids);
6973eb1aa43SJaya Kumar
6983eb1aa43SJaya Kumar static struct serio_driver w8001_drv = {
6993eb1aa43SJaya Kumar .driver = {
7003eb1aa43SJaya Kumar .name = "w8001",
7013eb1aa43SJaya Kumar },
7023eb1aa43SJaya Kumar .description = DRIVER_DESC,
7033eb1aa43SJaya Kumar .id_table = w8001_serio_ids,
7043eb1aa43SJaya Kumar .interrupt = w8001_interrupt,
7053eb1aa43SJaya Kumar .connect = w8001_connect,
7063eb1aa43SJaya Kumar .disconnect = w8001_disconnect,
7073eb1aa43SJaya Kumar };
7083eb1aa43SJaya Kumar
70965ac9f7aSAxel Lin module_serio_driver(w8001_drv);
710