Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Wacom W8001 penabled serial touchscreen driver |
| 3 | * |
| 4 | * Copyright (c) 2008 Jaya Kumar |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 5 | * Copyright (c) 2010 Red Hat, Inc. |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 6 | * Copyright (c) 2010 - 2011 Ping Cheng, Wacom. <pingc@wacom.com> |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 7 | * |
| 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 Rydberg | 47c78e8 | 2010-11-27 09:16:48 +0100 | [diff] [blame] | 19 | #include <linux/input/mt.h> |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 20 | #include <linux/serio.h> |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 21 | #include <linux/ctype.h> |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 22 | #include <linux/delay.h> |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 23 | |
| 24 | #define DRIVER_DESC "Wacom W8001 serial touchscreen driver" |
| 25 | |
| 26 | MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>"); |
| 27 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 28 | MODULE_LICENSE("GPL"); |
| 29 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 30 | #define W8001_MAX_LENGTH 11 |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 31 | #define W8001_LEAD_MASK 0x80 |
| 32 | #define W8001_LEAD_BYTE 0x80 |
| 33 | #define W8001_TAB_MASK 0x40 |
| 34 | #define W8001_TAB_BYTE 0x40 |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 35 | /* 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 Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 38 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 39 | #define W8001_QUERY_PACKET 0x20 |
| 40 | |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 41 | #define W8001_CMD_STOP '0' |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 42 | #define W8001_CMD_START '1' |
| 43 | #define W8001_CMD_QUERY '*' |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 44 | #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 Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 52 | |
Ping Cheng | 28a1bc1 | 2011-01-31 21:06:38 -0800 | [diff] [blame] | 53 | /* resolution in points/mm */ |
| 54 | #define W8001_PEN_RESOLUTION 100 |
| 55 | #define W8001_TOUCH_RESOLUTION 10 |
| 56 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 57 | struct 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 Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 69 | /* touch query reply packet */ |
| 70 | struct w8001_touch_query { |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 71 | u16 x; |
| 72 | u16 y; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 73 | u8 panel_res; |
| 74 | u8 capacity_res; |
| 75 | u8 sensor_id; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 78 | /* |
| 79 | * Per-touchscreen data. |
| 80 | */ |
| 81 | |
| 82 | struct w8001 { |
| 83 | struct input_dev *dev; |
| 84 | struct serio *serio; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 85 | struct completion cmd_done; |
| 86 | int id; |
| 87 | int idx; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 88 | unsigned char response_type; |
| 89 | unsigned char response[W8001_MAX_LENGTH]; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 90 | unsigned char data[W8001_MAX_LENGTH]; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 91 | char phys[32]; |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 92 | int type; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 93 | unsigned int pktlen; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 94 | u16 max_touch_x; |
| 95 | u16 max_touch_y; |
| 96 | u16 max_pen_x; |
| 97 | u16 max_pen_y; |
| 98 | char name[64]; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 99 | }; |
| 100 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 101 | static void parse_pen_data(u8 *data, struct w8001_coord *coord) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 102 | { |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 103 | memset(coord, 0, sizeof(*coord)); |
| 104 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 105 | coord->rdy = data[0] & 0x20; |
| 106 | coord->tsw = data[0] & 0x01; |
| 107 | coord->f1 = data[0] & 0x02; |
| 108 | coord->f2 = data[0] & 0x04; |
| 109 | |
| 110 | coord->x = (data[1] & 0x7F) << 9; |
| 111 | coord->x |= (data[2] & 0x7F) << 2; |
| 112 | coord->x |= (data[6] & 0x60) >> 5; |
| 113 | |
| 114 | coord->y = (data[3] & 0x7F) << 9; |
| 115 | coord->y |= (data[4] & 0x7F) << 2; |
| 116 | coord->y |= (data[6] & 0x18) >> 3; |
| 117 | |
| 118 | coord->pen_pressure = data[5] & 0x7F; |
| 119 | coord->pen_pressure |= (data[6] & 0x07) << 7 ; |
| 120 | |
| 121 | coord->tilt_x = data[7] & 0x7F; |
| 122 | coord->tilt_y = data[8] & 0x7F; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 125 | static void parse_single_touch(u8 *data, struct w8001_coord *coord) |
| 126 | { |
| 127 | coord->x = (data[1] << 7) | data[2]; |
| 128 | coord->y = (data[3] << 7) | data[4]; |
| 129 | coord->tsw = data[0] & 0x01; |
| 130 | } |
| 131 | |
| 132 | static void scale_touch_coordinates(struct w8001 *w8001, |
| 133 | unsigned int *x, unsigned int *y) |
| 134 | { |
| 135 | if (w8001->max_pen_x && w8001->max_touch_x) |
| 136 | *x = *x * w8001->max_pen_x / w8001->max_touch_x; |
| 137 | |
| 138 | if (w8001->max_pen_y && w8001->max_touch_y) |
| 139 | *y = *y * w8001->max_pen_y / w8001->max_touch_y; |
| 140 | } |
| 141 | |
| 142 | static void parse_multi_touch(struct w8001 *w8001) |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 143 | { |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 144 | struct input_dev *dev = w8001->dev; |
| 145 | unsigned char *data = w8001->data; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 146 | unsigned int x, y; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 147 | int i; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 148 | int count = 0; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 149 | |
| 150 | for (i = 0; i < 2; i++) { |
Henrik Rydberg | c5f4dec | 2010-12-15 13:50:34 +0100 | [diff] [blame] | 151 | bool touch = data[0] & (1 << i); |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 152 | |
Henrik Rydberg | c5f4dec | 2010-12-15 13:50:34 +0100 | [diff] [blame] | 153 | input_mt_slot(dev, i); |
| 154 | input_mt_report_slot_state(dev, MT_TOOL_FINGER, touch); |
| 155 | if (touch) { |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 156 | x = (data[6 * i + 1] << 7) | data[6 * i + 2]; |
| 157 | y = (data[6 * i + 3] << 7) | data[6 * i + 4]; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 158 | /* data[5,6] and [11,12] is finger capacity */ |
| 159 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 160 | /* scale to pen maximum */ |
| 161 | scale_touch_coordinates(w8001, &x, &y); |
| 162 | |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 163 | input_report_abs(dev, ABS_MT_POSITION_X, x); |
| 164 | input_report_abs(dev, ABS_MT_POSITION_Y, y); |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 165 | count++; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 166 | } |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 169 | /* emulate single touch events when stylus is out of proximity. |
| 170 | * This is to make single touch backward support consistent |
| 171 | * across all Wacom single touch devices. |
| 172 | */ |
| 173 | if (w8001->type != BTN_TOOL_PEN && |
| 174 | w8001->type != BTN_TOOL_RUBBER) { |
| 175 | w8001->type = count == 1 ? BTN_TOOL_FINGER : KEY_RESERVED; |
| 176 | input_mt_report_pointer_emulation(dev, true); |
| 177 | } |
| 178 | |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 179 | input_sync(dev); |
| 180 | } |
| 181 | |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 182 | static void parse_touchquery(u8 *data, struct w8001_touch_query *query) |
| 183 | { |
| 184 | memset(query, 0, sizeof(*query)); |
| 185 | |
| 186 | query->panel_res = data[1]; |
| 187 | query->sensor_id = data[2] & 0x7; |
| 188 | query->capacity_res = data[7]; |
| 189 | |
| 190 | query->x = data[3] << 9; |
| 191 | query->x |= data[4] << 2; |
| 192 | query->x |= (data[2] >> 5) & 0x3; |
| 193 | |
| 194 | query->y = data[5] << 9; |
| 195 | query->y |= data[6] << 2; |
| 196 | query->y |= (data[2] >> 3) & 0x3; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 197 | |
| 198 | /* Early days' single-finger touch models need the following defaults */ |
| 199 | if (!query->x && !query->y) { |
| 200 | query->x = 1024; |
| 201 | query->y = 1024; |
| 202 | if (query->panel_res) |
| 203 | query->x = query->y = (1 << query->panel_res); |
Ping Cheng | 28a1bc1 | 2011-01-31 21:06:38 -0800 | [diff] [blame] | 204 | query->panel_res = W8001_TOUCH_RESOLUTION; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 205 | } |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 208 | static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord) |
| 209 | { |
| 210 | struct input_dev *dev = w8001->dev; |
| 211 | |
| 212 | /* |
| 213 | * We have 1 bit for proximity (rdy) and 3 bits for tip, side, |
| 214 | * side2/eraser. If rdy && f2 are set, this can be either pen + side2, |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 215 | * or eraser. Assume: |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 216 | * - if dev is already in proximity and f2 is toggled → pen + side2 |
| 217 | * - if dev comes into proximity with f2 set → eraser |
| 218 | * If f2 disappears after assuming eraser, fake proximity out for |
| 219 | * eraser and in for pen. |
| 220 | */ |
| 221 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 222 | switch (w8001->type) { |
| 223 | case BTN_TOOL_RUBBER: |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 224 | if (!coord->f2) { |
| 225 | input_report_abs(dev, ABS_PRESSURE, 0); |
| 226 | input_report_key(dev, BTN_TOUCH, 0); |
| 227 | input_report_key(dev, BTN_STYLUS, 0); |
| 228 | input_report_key(dev, BTN_STYLUS2, 0); |
| 229 | input_report_key(dev, BTN_TOOL_RUBBER, 0); |
| 230 | input_sync(dev); |
| 231 | w8001->type = BTN_TOOL_PEN; |
| 232 | } |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 233 | break; |
| 234 | |
| 235 | case BTN_TOOL_FINGER: |
| 236 | input_report_key(dev, BTN_TOUCH, 0); |
| 237 | input_report_key(dev, BTN_TOOL_FINGER, 0); |
| 238 | input_sync(dev); |
| 239 | /* fall through */ |
| 240 | |
| 241 | case KEY_RESERVED: |
| 242 | w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN; |
| 243 | break; |
| 244 | |
| 245 | default: |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 246 | input_report_key(dev, BTN_STYLUS2, coord->f2); |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 247 | break; |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | input_report_abs(dev, ABS_X, coord->x); |
| 251 | input_report_abs(dev, ABS_Y, coord->y); |
| 252 | input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure); |
| 253 | input_report_key(dev, BTN_TOUCH, coord->tsw); |
| 254 | input_report_key(dev, BTN_STYLUS, coord->f1); |
| 255 | input_report_key(dev, w8001->type, coord->rdy); |
| 256 | input_sync(dev); |
| 257 | |
| 258 | if (!coord->rdy) |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 259 | w8001->type = KEY_RESERVED; |
| 260 | } |
| 261 | |
| 262 | static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord) |
| 263 | { |
| 264 | struct input_dev *dev = w8001->dev; |
| 265 | unsigned int x = coord->x; |
| 266 | unsigned int y = coord->y; |
| 267 | |
| 268 | /* scale to pen maximum */ |
| 269 | scale_touch_coordinates(w8001, &x, &y); |
| 270 | |
| 271 | input_report_abs(dev, ABS_X, x); |
| 272 | input_report_abs(dev, ABS_Y, y); |
| 273 | input_report_key(dev, BTN_TOUCH, coord->tsw); |
| 274 | input_report_key(dev, BTN_TOOL_FINGER, coord->tsw); |
| 275 | |
| 276 | input_sync(dev); |
| 277 | |
| 278 | w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED; |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 281 | static irqreturn_t w8001_interrupt(struct serio *serio, |
| 282 | unsigned char data, unsigned int flags) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 283 | { |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 284 | struct w8001 *w8001 = serio_get_drvdata(serio); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 285 | struct w8001_coord coord; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 286 | unsigned char tmp; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 287 | |
| 288 | w8001->data[w8001->idx] = data; |
| 289 | switch (w8001->idx++) { |
| 290 | case 0: |
| 291 | if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) { |
| 292 | pr_debug("w8001: unsynchronized data: 0x%02x\n", data); |
| 293 | w8001->idx = 0; |
| 294 | } |
| 295 | break; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 296 | |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 297 | case W8001_PKTLEN_TOUCH93 - 1: |
| 298 | case W8001_PKTLEN_TOUCH9A - 1: |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 299 | tmp = w8001->data[0] & W8001_TOUCH_BYTE; |
| 300 | if (tmp != W8001_TOUCH_BYTE) |
| 301 | break; |
| 302 | |
| 303 | if (w8001->pktlen == w8001->idx) { |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 304 | w8001->idx = 0; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 305 | if (w8001->type != BTN_TOOL_PEN && |
| 306 | w8001->type != BTN_TOOL_RUBBER) { |
| 307 | parse_single_touch(w8001->data, &coord); |
| 308 | report_single_touch(w8001, &coord); |
| 309 | } |
| 310 | } |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 311 | break; |
| 312 | |
| 313 | /* Pen coordinates packet */ |
| 314 | case W8001_PKTLEN_TPCPEN - 1: |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 315 | tmp = w8001->data[0] & W8001_TAB_MASK; |
| 316 | if (unlikely(tmp == W8001_TAB_BYTE)) |
| 317 | break; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 318 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 319 | tmp = w8001->data[0] & W8001_TOUCH_BYTE; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 320 | if (tmp == W8001_TOUCH_BYTE) |
| 321 | break; |
| 322 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 323 | w8001->idx = 0; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 324 | parse_pen_data(w8001->data, &coord); |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 325 | report_pen_events(w8001, &coord); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 326 | break; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 327 | |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 328 | /* control packet */ |
| 329 | case W8001_PKTLEN_TPCCTL - 1: |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 330 | tmp = w8001->data[0] & W8001_TOUCH_MASK; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 331 | if (tmp == W8001_TOUCH_BYTE) |
| 332 | break; |
| 333 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 334 | w8001->idx = 0; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 335 | memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH); |
| 336 | w8001->response_type = W8001_QUERY_PACKET; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 337 | complete(&w8001->cmd_done); |
| 338 | break; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 339 | |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 340 | /* 2 finger touch packet */ |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 341 | case W8001_PKTLEN_TOUCH2FG - 1: |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 342 | w8001->idx = 0; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 343 | parse_multi_touch(w8001); |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 344 | break; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 345 | } |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 346 | |
| 347 | return IRQ_HANDLED; |
| 348 | } |
| 349 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 350 | static int w8001_command(struct w8001 *w8001, unsigned char command, |
| 351 | bool wait_response) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 352 | { |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 353 | int rc; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 354 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 355 | w8001->response_type = 0; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 356 | init_completion(&w8001->cmd_done); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 357 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 358 | rc = serio_write(w8001->serio, command); |
| 359 | if (rc == 0 && wait_response) { |
| 360 | |
| 361 | wait_for_completion_timeout(&w8001->cmd_done, HZ); |
| 362 | if (w8001->response_type != W8001_QUERY_PACKET) |
| 363 | rc = -EIO; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 364 | } |
| 365 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 366 | return rc; |
| 367 | } |
| 368 | |
Dmitry Torokhov | e949674 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 369 | static int w8001_open(struct input_dev *dev) |
| 370 | { |
| 371 | struct w8001 *w8001 = input_get_drvdata(dev); |
| 372 | |
| 373 | return w8001_command(w8001, W8001_CMD_START, false); |
| 374 | } |
| 375 | |
| 376 | static void w8001_close(struct input_dev *dev) |
| 377 | { |
| 378 | struct w8001 *w8001 = input_get_drvdata(dev); |
| 379 | |
| 380 | w8001_command(w8001, W8001_CMD_STOP, false); |
| 381 | } |
| 382 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 383 | static int w8001_setup(struct w8001 *w8001) |
| 384 | { |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 385 | struct input_dev *dev = w8001->dev; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 386 | struct w8001_coord coord; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 387 | struct w8001_touch_query touch; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 388 | int error; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 389 | |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 390 | error = w8001_command(w8001, W8001_CMD_STOP, false); |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 391 | if (error) |
| 392 | return error; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 393 | |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 394 | msleep(250); /* wait 250ms before querying the device */ |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 395 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 396 | dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 397 | strlcat(w8001->name, "Wacom Serial", sizeof(w8001->name)); |
| 398 | |
Jason Gerecke | 3512069 | 2011-09-08 09:38:14 -0700 | [diff] [blame] | 399 | __set_bit(INPUT_PROP_DIRECT, dev->propbit); |
| 400 | |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 401 | /* penabled? */ |
| 402 | error = w8001_command(w8001, W8001_CMD_QUERY, true); |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 403 | if (!error) { |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 404 | __set_bit(BTN_TOUCH, dev->keybit); |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 405 | __set_bit(BTN_TOOL_PEN, dev->keybit); |
| 406 | __set_bit(BTN_TOOL_RUBBER, dev->keybit); |
| 407 | __set_bit(BTN_STYLUS, dev->keybit); |
| 408 | __set_bit(BTN_STYLUS2, dev->keybit); |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 409 | |
| 410 | parse_pen_data(w8001->response, &coord); |
| 411 | w8001->max_pen_x = coord.x; |
| 412 | w8001->max_pen_y = coord.y; |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 413 | |
| 414 | input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0); |
| 415 | input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0); |
Ping Cheng | 28a1bc1 | 2011-01-31 21:06:38 -0800 | [diff] [blame] | 416 | input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION); |
| 417 | input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION); |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 418 | input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0); |
| 419 | if (coord.tilt_x && coord.tilt_y) { |
| 420 | input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0); |
| 421 | input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0); |
| 422 | } |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 423 | w8001->id = 0x90; |
| 424 | strlcat(w8001->name, " Penabled", sizeof(w8001->name)); |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* Touch enabled? */ |
| 428 | error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true); |
| 429 | |
| 430 | /* |
| 431 | * Some non-touch devices may reply to the touch query. But their |
| 432 | * second byte is empty, which indicates touch is not supported. |
| 433 | */ |
| 434 | if (!error && w8001->response[1]) { |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 435 | __set_bit(BTN_TOUCH, dev->keybit); |
| 436 | __set_bit(BTN_TOOL_FINGER, dev->keybit); |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 437 | |
| 438 | parse_touchquery(w8001->response, &touch); |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 439 | w8001->max_touch_x = touch.x; |
| 440 | w8001->max_touch_y = touch.y; |
| 441 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 442 | if (w8001->max_pen_x && w8001->max_pen_y) { |
Ping Cheng | 28a1bc1 | 2011-01-31 21:06:38 -0800 | [diff] [blame] | 443 | /* if pen is supported scale to pen maximum */ |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 444 | touch.x = w8001->max_pen_x; |
| 445 | touch.y = w8001->max_pen_y; |
Ping Cheng | 28a1bc1 | 2011-01-31 21:06:38 -0800 | [diff] [blame] | 446 | touch.panel_res = W8001_PEN_RESOLUTION; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 447 | } |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 448 | |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 449 | input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0); |
| 450 | input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0); |
Ping Cheng | 28a1bc1 | 2011-01-31 21:06:38 -0800 | [diff] [blame] | 451 | input_abs_set_res(dev, ABS_X, touch.panel_res); |
| 452 | input_abs_set_res(dev, ABS_Y, touch.panel_res); |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 453 | |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 454 | switch (touch.sensor_id) { |
| 455 | case 0: |
| 456 | case 2: |
| 457 | w8001->pktlen = W8001_PKTLEN_TOUCH93; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 458 | w8001->id = 0x93; |
| 459 | strlcat(w8001->name, " 1FG", sizeof(w8001->name)); |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 460 | break; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 461 | |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 462 | case 1: |
| 463 | case 3: |
| 464 | case 4: |
| 465 | w8001->pktlen = W8001_PKTLEN_TOUCH9A; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 466 | strlcat(w8001->name, " 1FG", sizeof(w8001->name)); |
| 467 | w8001->id = 0x9a; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 468 | break; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 469 | |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 470 | case 5: |
| 471 | w8001->pktlen = W8001_PKTLEN_TOUCH2FG; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 472 | |
Henrik Rydberg | b4adbbe | 2012-08-11 22:07:55 +0200 | [diff] [blame] | 473 | input_mt_init_slots(dev, 2, 0); |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 474 | input_set_abs_params(dev, ABS_MT_POSITION_X, |
| 475 | 0, touch.x, 0, 0); |
| 476 | input_set_abs_params(dev, ABS_MT_POSITION_Y, |
| 477 | 0, touch.y, 0, 0); |
| 478 | input_set_abs_params(dev, ABS_MT_TOOL_TYPE, |
Henrik Rydberg | c5f4dec | 2010-12-15 13:50:34 +0100 | [diff] [blame] | 479 | 0, MT_TOOL_MAX, 0, 0); |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 480 | |
| 481 | strlcat(w8001->name, " 2FG", sizeof(w8001->name)); |
| 482 | if (w8001->max_pen_x && w8001->max_pen_y) |
| 483 | w8001->id = 0xE3; |
| 484 | else |
| 485 | w8001->id = 0xE2; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 486 | break; |
| 487 | } |
| 488 | } |
| 489 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 490 | strlcat(w8001->name, " Touchscreen", sizeof(w8001->name)); |
| 491 | |
Dmitry Torokhov | e949674 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 492 | return 0; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | /* |
| 496 | * w8001_disconnect() is the opposite of w8001_connect() |
| 497 | */ |
| 498 | |
| 499 | static void w8001_disconnect(struct serio *serio) |
| 500 | { |
| 501 | struct w8001 *w8001 = serio_get_drvdata(serio); |
| 502 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 503 | serio_close(serio); |
Dmitry Torokhov | 66fd938 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 504 | |
| 505 | input_unregister_device(w8001->dev); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 506 | kfree(w8001); |
Dmitry Torokhov | 66fd938 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 507 | |
| 508 | serio_set_drvdata(serio, NULL); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* |
| 512 | * w8001_connect() is the routine that is called when someone adds a |
| 513 | * new serio device that supports the w8001 protocol and registers it as |
| 514 | * an input device. |
| 515 | */ |
| 516 | |
| 517 | static int w8001_connect(struct serio *serio, struct serio_driver *drv) |
| 518 | { |
| 519 | struct w8001 *w8001; |
| 520 | struct input_dev *input_dev; |
| 521 | int err; |
| 522 | |
| 523 | w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL); |
| 524 | input_dev = input_allocate_device(); |
| 525 | if (!w8001 || !input_dev) { |
| 526 | err = -ENOMEM; |
| 527 | goto fail1; |
| 528 | } |
| 529 | |
| 530 | w8001->serio = serio; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 531 | w8001->dev = input_dev; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 532 | init_completion(&w8001->cmd_done); |
| 533 | snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys); |
| 534 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 535 | serio_set_drvdata(serio, w8001); |
| 536 | err = serio_open(serio, drv); |
| 537 | if (err) |
| 538 | goto fail2; |
| 539 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 540 | err = w8001_setup(w8001); |
| 541 | if (err) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 542 | goto fail3; |
| 543 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 544 | input_dev->name = w8001->name; |
| 545 | input_dev->phys = w8001->phys; |
| 546 | input_dev->id.product = w8001->id; |
| 547 | input_dev->id.bustype = BUS_RS232; |
| 548 | input_dev->id.vendor = 0x056a; |
| 549 | input_dev->id.version = 0x0100; |
| 550 | input_dev->dev.parent = &serio->dev; |
| 551 | |
Dmitry Torokhov | e949674 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 552 | input_dev->open = w8001_open; |
| 553 | input_dev->close = w8001_close; |
| 554 | |
| 555 | input_set_drvdata(input_dev, w8001); |
| 556 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 557 | err = input_register_device(w8001->dev); |
| 558 | if (err) |
| 559 | goto fail3; |
| 560 | |
| 561 | return 0; |
| 562 | |
| 563 | fail3: |
| 564 | serio_close(serio); |
| 565 | fail2: |
| 566 | serio_set_drvdata(serio, NULL); |
| 567 | fail1: |
| 568 | input_free_device(input_dev); |
| 569 | kfree(w8001); |
| 570 | return err; |
| 571 | } |
| 572 | |
| 573 | static struct serio_device_id w8001_serio_ids[] = { |
| 574 | { |
| 575 | .type = SERIO_RS232, |
| 576 | .proto = SERIO_W8001, |
| 577 | .id = SERIO_ANY, |
| 578 | .extra = SERIO_ANY, |
| 579 | }, |
| 580 | { 0 } |
| 581 | }; |
| 582 | |
| 583 | MODULE_DEVICE_TABLE(serio, w8001_serio_ids); |
| 584 | |
| 585 | static struct serio_driver w8001_drv = { |
| 586 | .driver = { |
| 587 | .name = "w8001", |
| 588 | }, |
| 589 | .description = DRIVER_DESC, |
| 590 | .id_table = w8001_serio_ids, |
| 591 | .interrupt = w8001_interrupt, |
| 592 | .connect = w8001_connect, |
| 593 | .disconnect = w8001_disconnect, |
| 594 | }; |
| 595 | |
Axel Lin | 65ac9f7 | 2012-04-03 23:50:17 -0700 | [diff] [blame] | 596 | module_serio_driver(w8001_drv); |