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 | |
Ping Cheng | 12afb34 | 2016-06-23 10:54:17 -0700 | [diff] [blame] | 30 | #define W8001_MAX_LENGTH 13 |
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 { |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 83 | struct input_dev *pen_dev; |
| 84 | struct input_dev *touch_dev; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 85 | struct serio *serio; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 86 | struct completion cmd_done; |
| 87 | int id; |
| 88 | int idx; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 89 | unsigned char response_type; |
| 90 | unsigned char response[W8001_MAX_LENGTH]; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 91 | unsigned char data[W8001_MAX_LENGTH]; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 92 | char phys[32]; |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 93 | int type; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 94 | unsigned int pktlen; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 95 | u16 max_touch_x; |
| 96 | u16 max_touch_y; |
| 97 | u16 max_pen_x; |
| 98 | u16 max_pen_y; |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 99 | char pen_name[64]; |
| 100 | char touch_name[64]; |
| 101 | int open_count; |
| 102 | struct mutex mutex; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 103 | }; |
| 104 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 105 | static void parse_pen_data(u8 *data, struct w8001_coord *coord) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 106 | { |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 107 | memset(coord, 0, sizeof(*coord)); |
| 108 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 109 | 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 Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 129 | static 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 | |
| 136 | static 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 | |
| 146 | static void parse_multi_touch(struct w8001 *w8001) |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 147 | { |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 148 | struct input_dev *dev = w8001->touch_dev; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 149 | unsigned char *data = w8001->data; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 150 | unsigned int x, y; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 151 | int i; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 152 | int count = 0; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 153 | |
| 154 | for (i = 0; i < 2; i++) { |
Henrik Rydberg | c5f4dec | 2010-12-15 13:50:34 +0100 | [diff] [blame] | 155 | bool touch = data[0] & (1 << i); |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 156 | |
Henrik Rydberg | c5f4dec | 2010-12-15 13:50:34 +0100 | [diff] [blame] | 157 | input_mt_slot(dev, i); |
Dmitry Torokhov | 3e9161b | 2016-07-14 09:33:41 -0700 | [diff] [blame] | 158 | input_mt_report_slot_state(dev, MT_TOOL_FINGER, touch); |
Henrik Rydberg | c5f4dec | 2010-12-15 13:50:34 +0100 | [diff] [blame] | 159 | if (touch) { |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 160 | x = (data[6 * i + 1] << 7) | data[6 * i + 2]; |
| 161 | y = (data[6 * i + 3] << 7) | data[6 * i + 4]; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 162 | /* data[5,6] and [11,12] is finger capacity */ |
| 163 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 164 | /* scale to pen maximum */ |
| 165 | scale_touch_coordinates(w8001, &x, &y); |
| 166 | |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 167 | input_report_abs(dev, ABS_MT_POSITION_X, x); |
| 168 | input_report_abs(dev, ABS_MT_POSITION_Y, y); |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 169 | count++; |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 170 | } |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 173 | /* emulate single touch events when stylus is out of proximity. |
| 174 | * This is to make single touch backward support consistent |
| 175 | * across all Wacom single touch devices. |
| 176 | */ |
| 177 | if (w8001->type != BTN_TOOL_PEN && |
| 178 | w8001->type != BTN_TOOL_RUBBER) { |
| 179 | w8001->type = count == 1 ? BTN_TOOL_FINGER : KEY_RESERVED; |
| 180 | input_mt_report_pointer_emulation(dev, true); |
| 181 | } |
| 182 | |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 183 | input_sync(dev); |
| 184 | } |
| 185 | |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 186 | static void parse_touchquery(u8 *data, struct w8001_touch_query *query) |
| 187 | { |
| 188 | memset(query, 0, sizeof(*query)); |
| 189 | |
| 190 | query->panel_res = data[1]; |
| 191 | query->sensor_id = data[2] & 0x7; |
| 192 | query->capacity_res = data[7]; |
| 193 | |
| 194 | query->x = data[3] << 9; |
| 195 | query->x |= data[4] << 2; |
| 196 | query->x |= (data[2] >> 5) & 0x3; |
| 197 | |
| 198 | query->y = data[5] << 9; |
| 199 | query->y |= data[6] << 2; |
| 200 | query->y |= (data[2] >> 3) & 0x3; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 201 | |
| 202 | /* Early days' single-finger touch models need the following defaults */ |
| 203 | if (!query->x && !query->y) { |
| 204 | query->x = 1024; |
| 205 | query->y = 1024; |
| 206 | if (query->panel_res) |
| 207 | query->x = query->y = (1 << query->panel_res); |
Ping Cheng | 28a1bc1 | 2011-01-31 21:06:38 -0800 | [diff] [blame] | 208 | query->panel_res = W8001_TOUCH_RESOLUTION; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 209 | } |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 212 | static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord) |
| 213 | { |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 214 | struct input_dev *dev = w8001->pen_dev; |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * We have 1 bit for proximity (rdy) and 3 bits for tip, side, |
| 218 | * 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] | 219 | * or eraser. Assume: |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 220 | * - if dev is already in proximity and f2 is toggled → pen + side2 |
| 221 | * - if dev comes into proximity with f2 set → eraser |
| 222 | * If f2 disappears after assuming eraser, fake proximity out for |
| 223 | * eraser and in for pen. |
| 224 | */ |
| 225 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 226 | switch (w8001->type) { |
| 227 | case BTN_TOOL_RUBBER: |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 228 | if (!coord->f2) { |
| 229 | input_report_abs(dev, ABS_PRESSURE, 0); |
| 230 | input_report_key(dev, BTN_TOUCH, 0); |
| 231 | input_report_key(dev, BTN_STYLUS, 0); |
| 232 | input_report_key(dev, BTN_STYLUS2, 0); |
| 233 | input_report_key(dev, BTN_TOOL_RUBBER, 0); |
| 234 | input_sync(dev); |
| 235 | w8001->type = BTN_TOOL_PEN; |
| 236 | } |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 237 | break; |
| 238 | |
| 239 | case BTN_TOOL_FINGER: |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 240 | case KEY_RESERVED: |
| 241 | w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN; |
| 242 | break; |
| 243 | |
| 244 | default: |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 245 | input_report_key(dev, BTN_STYLUS2, coord->f2); |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 246 | break; |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | input_report_abs(dev, ABS_X, coord->x); |
| 250 | input_report_abs(dev, ABS_Y, coord->y); |
| 251 | input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure); |
| 252 | input_report_key(dev, BTN_TOUCH, coord->tsw); |
| 253 | input_report_key(dev, BTN_STYLUS, coord->f1); |
| 254 | input_report_key(dev, w8001->type, coord->rdy); |
| 255 | input_sync(dev); |
| 256 | |
| 257 | if (!coord->rdy) |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 258 | w8001->type = KEY_RESERVED; |
| 259 | } |
| 260 | |
| 261 | static void report_single_touch(struct w8001 *w8001, struct w8001_coord *coord) |
| 262 | { |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 263 | struct input_dev *dev = w8001->touch_dev; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 264 | unsigned int x = coord->x; |
| 265 | unsigned int y = coord->y; |
| 266 | |
| 267 | /* scale to pen maximum */ |
| 268 | scale_touch_coordinates(w8001, &x, &y); |
| 269 | |
| 270 | input_report_abs(dev, ABS_X, x); |
| 271 | input_report_abs(dev, ABS_Y, y); |
| 272 | input_report_key(dev, BTN_TOUCH, coord->tsw); |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 273 | |
| 274 | input_sync(dev); |
| 275 | |
| 276 | w8001->type = coord->tsw ? BTN_TOOL_FINGER : KEY_RESERVED; |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 279 | static irqreturn_t w8001_interrupt(struct serio *serio, |
| 280 | unsigned char data, unsigned int flags) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 281 | { |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 282 | struct w8001 *w8001 = serio_get_drvdata(serio); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 283 | struct w8001_coord coord; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 284 | unsigned char tmp; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 285 | |
| 286 | w8001->data[w8001->idx] = data; |
| 287 | switch (w8001->idx++) { |
| 288 | case 0: |
| 289 | if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) { |
| 290 | pr_debug("w8001: unsynchronized data: 0x%02x\n", data); |
| 291 | w8001->idx = 0; |
| 292 | } |
| 293 | break; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 294 | |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 295 | case W8001_PKTLEN_TOUCH93 - 1: |
| 296 | case W8001_PKTLEN_TOUCH9A - 1: |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 297 | tmp = w8001->data[0] & W8001_TOUCH_BYTE; |
| 298 | if (tmp != W8001_TOUCH_BYTE) |
| 299 | break; |
| 300 | |
| 301 | if (w8001->pktlen == w8001->idx) { |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 302 | w8001->idx = 0; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 303 | if (w8001->type != BTN_TOOL_PEN && |
| 304 | w8001->type != BTN_TOOL_RUBBER) { |
| 305 | parse_single_touch(w8001->data, &coord); |
| 306 | report_single_touch(w8001, &coord); |
| 307 | } |
| 308 | } |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 309 | break; |
| 310 | |
| 311 | /* Pen coordinates packet */ |
| 312 | case W8001_PKTLEN_TPCPEN - 1: |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 313 | tmp = w8001->data[0] & W8001_TAB_MASK; |
| 314 | if (unlikely(tmp == W8001_TAB_BYTE)) |
| 315 | break; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 316 | |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 317 | tmp = w8001->data[0] & W8001_TOUCH_BYTE; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 318 | if (tmp == W8001_TOUCH_BYTE) |
| 319 | break; |
| 320 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 321 | w8001->idx = 0; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 322 | parse_pen_data(w8001->data, &coord); |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame] | 323 | report_pen_events(w8001, &coord); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 324 | break; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 325 | |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 326 | /* control packet */ |
| 327 | case W8001_PKTLEN_TPCCTL - 1: |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 328 | tmp = w8001->data[0] & W8001_TOUCH_MASK; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 329 | if (tmp == W8001_TOUCH_BYTE) |
| 330 | break; |
| 331 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 332 | w8001->idx = 0; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 333 | memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH); |
| 334 | w8001->response_type = W8001_QUERY_PACKET; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 335 | complete(&w8001->cmd_done); |
| 336 | break; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 337 | |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 338 | /* 2 finger touch packet */ |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 339 | case W8001_PKTLEN_TOUCH2FG - 1: |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 340 | w8001->idx = 0; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 341 | parse_multi_touch(w8001); |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 342 | break; |
Ping Cheng | 9e72ac7 | 2016-06-23 10:55:11 -0700 | [diff] [blame] | 343 | |
| 344 | default: |
| 345 | /* |
| 346 | * ThinkPad X60 Tablet PC (pen only device) sometimes |
| 347 | * sends invalid data packets that are larger than |
| 348 | * W8001_PKTLEN_TPCPEN. Let's start over again. |
| 349 | */ |
| 350 | if (!w8001->touch_dev && w8001->idx > W8001_PKTLEN_TPCPEN - 1) |
| 351 | w8001->idx = 0; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 352 | } |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 353 | |
| 354 | return IRQ_HANDLED; |
| 355 | } |
| 356 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 357 | static int w8001_command(struct w8001 *w8001, unsigned char command, |
| 358 | bool wait_response) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 359 | { |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 360 | int rc; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 361 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 362 | w8001->response_type = 0; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 363 | init_completion(&w8001->cmd_done); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 364 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 365 | rc = serio_write(w8001->serio, command); |
| 366 | if (rc == 0 && wait_response) { |
| 367 | |
| 368 | wait_for_completion_timeout(&w8001->cmd_done, HZ); |
| 369 | if (w8001->response_type != W8001_QUERY_PACKET) |
| 370 | rc = -EIO; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 371 | } |
| 372 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 373 | return rc; |
| 374 | } |
| 375 | |
Dmitry Torokhov | e949674 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 376 | static int w8001_open(struct input_dev *dev) |
| 377 | { |
| 378 | struct w8001 *w8001 = input_get_drvdata(dev); |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 379 | int err; |
Dmitry Torokhov | e949674 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 380 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 381 | err = mutex_lock_interruptible(&w8001->mutex); |
| 382 | if (err) |
| 383 | return err; |
| 384 | |
| 385 | if (w8001->open_count++ == 0) { |
| 386 | err = w8001_command(w8001, W8001_CMD_START, false); |
| 387 | if (err) |
| 388 | w8001->open_count--; |
| 389 | } |
| 390 | |
| 391 | mutex_unlock(&w8001->mutex); |
| 392 | return err; |
Dmitry Torokhov | e949674 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | static void w8001_close(struct input_dev *dev) |
| 396 | { |
| 397 | struct w8001 *w8001 = input_get_drvdata(dev); |
| 398 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 399 | mutex_lock(&w8001->mutex); |
| 400 | |
| 401 | if (--w8001->open_count == 0) |
| 402 | w8001_command(w8001, W8001_CMD_STOP, false); |
| 403 | |
| 404 | mutex_unlock(&w8001->mutex); |
Dmitry Torokhov | e949674 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 407 | static int w8001_detect(struct w8001 *w8001) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 408 | { |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 409 | int error; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 410 | |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 411 | error = w8001_command(w8001, W8001_CMD_STOP, false); |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 412 | if (error) |
| 413 | return error; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 414 | |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 415 | msleep(250); /* wait 250ms before querying the device */ |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 416 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 417 | return 0; |
| 418 | } |
| 419 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 420 | static int w8001_setup_pen(struct w8001 *w8001, char *basename, |
| 421 | size_t basename_sz) |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 422 | { |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 423 | struct input_dev *dev = w8001->pen_dev; |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 424 | struct w8001_coord coord; |
| 425 | int error; |
| 426 | |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 427 | /* penabled? */ |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 428 | error = w8001_command(w8001, W8001_CMD_QUERY, true); |
| 429 | if (error) |
| 430 | return error; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 431 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 432 | __set_bit(EV_KEY, dev->evbit); |
| 433 | __set_bit(EV_ABS, dev->evbit); |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 434 | __set_bit(BTN_TOUCH, dev->keybit); |
| 435 | __set_bit(BTN_TOOL_PEN, dev->keybit); |
| 436 | __set_bit(BTN_TOOL_RUBBER, dev->keybit); |
| 437 | __set_bit(BTN_STYLUS, dev->keybit); |
| 438 | __set_bit(BTN_STYLUS2, dev->keybit); |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 439 | __set_bit(INPUT_PROP_DIRECT, dev->propbit); |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 440 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 441 | parse_pen_data(w8001->response, &coord); |
| 442 | w8001->max_pen_x = coord.x; |
| 443 | w8001->max_pen_y = coord.y; |
| 444 | |
| 445 | input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0); |
| 446 | input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0); |
| 447 | input_abs_set_res(dev, ABS_X, W8001_PEN_RESOLUTION); |
| 448 | input_abs_set_res(dev, ABS_Y, W8001_PEN_RESOLUTION); |
| 449 | input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0); |
| 450 | if (coord.tilt_x && coord.tilt_y) { |
| 451 | input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0); |
| 452 | input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0); |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 453 | } |
| 454 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 455 | w8001->id = 0x90; |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 456 | strlcat(basename, " Penabled", basename_sz); |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 461 | static int w8001_setup_touch(struct w8001 *w8001, char *basename, |
| 462 | size_t basename_sz) |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 463 | { |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 464 | struct input_dev *dev = w8001->touch_dev; |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 465 | struct w8001_touch_query touch; |
| 466 | int error; |
| 467 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 468 | |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 469 | /* Touch enabled? */ |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 470 | error = w8001_command(w8001, W8001_CMD_TOUCHQUERY, true); |
| 471 | if (error) |
| 472 | return error; |
| 473 | /* |
| 474 | * Some non-touch devices may reply to the touch query. But their |
| 475 | * second byte is empty, which indicates touch is not supported. |
| 476 | */ |
| 477 | if (!w8001->response[1]) |
| 478 | return -ENXIO; |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 479 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 480 | __set_bit(EV_KEY, dev->evbit); |
| 481 | __set_bit(EV_ABS, dev->evbit); |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 482 | __set_bit(BTN_TOUCH, dev->keybit); |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 483 | __set_bit(INPUT_PROP_DIRECT, dev->propbit); |
Peter Hutterer | e171735 | 2015-12-03 12:24:26 -0800 | [diff] [blame] | 484 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 485 | parse_touchquery(w8001->response, &touch); |
| 486 | w8001->max_touch_x = touch.x; |
| 487 | w8001->max_touch_y = touch.y; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 488 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 489 | if (w8001->max_pen_x && w8001->max_pen_y) { |
| 490 | /* if pen is supported scale to pen maximum */ |
| 491 | touch.x = w8001->max_pen_x; |
| 492 | touch.y = w8001->max_pen_y; |
| 493 | touch.panel_res = W8001_PEN_RESOLUTION; |
| 494 | } |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 495 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 496 | input_set_abs_params(dev, ABS_X, 0, touch.x, 0, 0); |
| 497 | input_set_abs_params(dev, ABS_Y, 0, touch.y, 0, 0); |
| 498 | input_abs_set_res(dev, ABS_X, touch.panel_res); |
| 499 | input_abs_set_res(dev, ABS_Y, touch.panel_res); |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 500 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 501 | switch (touch.sensor_id) { |
| 502 | case 0: |
| 503 | case 2: |
| 504 | w8001->pktlen = W8001_PKTLEN_TOUCH93; |
| 505 | w8001->id = 0x93; |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 506 | strlcat(basename, " 1FG", basename_sz); |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 507 | break; |
Ping Cheng | a6d38f8 | 2010-12-24 13:16:53 -0800 | [diff] [blame] | 508 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 509 | case 1: |
| 510 | case 3: |
| 511 | case 4: |
| 512 | w8001->pktlen = W8001_PKTLEN_TOUCH9A; |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 513 | strlcat(basename, " 1FG", basename_sz); |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 514 | w8001->id = 0x9a; |
| 515 | break; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 516 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 517 | case 5: |
| 518 | w8001->pktlen = W8001_PKTLEN_TOUCH2FG; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 519 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 520 | __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit); |
Ping Cheng | 3746e56 | 2016-07-19 11:13:17 -0700 | [diff] [blame] | 521 | error = input_mt_init_slots(dev, 2, 0); |
| 522 | if (error) { |
| 523 | dev_err(&w8001->serio->dev, |
| 524 | "failed to initialize MT slots: %d\n", error); |
| 525 | return error; |
| 526 | } |
| 527 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 528 | input_set_abs_params(dev, ABS_MT_POSITION_X, |
| 529 | 0, touch.x, 0, 0); |
| 530 | input_set_abs_params(dev, ABS_MT_POSITION_Y, |
| 531 | 0, touch.y, 0, 0); |
Dmitry Torokhov | 3e9161b | 2016-07-14 09:33:41 -0700 | [diff] [blame] | 532 | input_set_abs_params(dev, ABS_MT_TOOL_TYPE, |
| 533 | 0, MT_TOOL_MAX, 0, 0); |
Ping Cheng | ae10850 | 2016-07-19 11:00:51 -0700 | [diff] [blame] | 534 | input_abs_set_res(dev, ABS_MT_POSITION_X, touch.panel_res); |
| 535 | input_abs_set_res(dev, ABS_MT_POSITION_Y, touch.panel_res); |
Peter Hutterer | 5e8b9140 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 536 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 537 | strlcat(basename, " 2FG", basename_sz); |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 538 | if (w8001->max_pen_x && w8001->max_pen_y) |
| 539 | w8001->id = 0xE3; |
| 540 | else |
| 541 | w8001->id = 0xE2; |
| 542 | break; |
Peter Hutterer | aaba933 | 2010-08-28 22:00:47 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 545 | strlcat(basename, " Touchscreen", basename_sz); |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 546 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 547 | return 0; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 548 | } |
| 549 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 550 | static void w8001_set_devdata(struct input_dev *dev, struct w8001 *w8001, |
| 551 | struct serio *serio) |
| 552 | { |
| 553 | dev->phys = w8001->phys; |
| 554 | dev->id.bustype = BUS_RS232; |
| 555 | dev->id.product = w8001->id; |
| 556 | dev->id.vendor = 0x056a; |
| 557 | dev->id.version = 0x0100; |
| 558 | dev->open = w8001_open; |
| 559 | dev->close = w8001_close; |
| 560 | |
| 561 | dev->dev.parent = &serio->dev; |
| 562 | |
| 563 | input_set_drvdata(dev, w8001); |
| 564 | } |
| 565 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 566 | /* |
| 567 | * w8001_disconnect() is the opposite of w8001_connect() |
| 568 | */ |
| 569 | |
| 570 | static void w8001_disconnect(struct serio *serio) |
| 571 | { |
| 572 | struct w8001 *w8001 = serio_get_drvdata(serio); |
| 573 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 574 | serio_close(serio); |
Dmitry Torokhov | 66fd938 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 575 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 576 | if (w8001->pen_dev) |
| 577 | input_unregister_device(w8001->pen_dev); |
| 578 | if (w8001->touch_dev) |
| 579 | input_unregister_device(w8001->touch_dev); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 580 | kfree(w8001); |
Dmitry Torokhov | 66fd938 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 581 | |
| 582 | serio_set_drvdata(serio, NULL); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /* |
| 586 | * w8001_connect() is the routine that is called when someone adds a |
| 587 | * new serio device that supports the w8001 protocol and registers it as |
| 588 | * an input device. |
| 589 | */ |
| 590 | |
| 591 | static int w8001_connect(struct serio *serio, struct serio_driver *drv) |
| 592 | { |
| 593 | struct w8001 *w8001; |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 594 | struct input_dev *input_dev_pen; |
| 595 | struct input_dev *input_dev_touch; |
| 596 | char basename[64]; |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 597 | int err, err_pen, err_touch; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 598 | |
| 599 | w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL); |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 600 | input_dev_pen = input_allocate_device(); |
| 601 | input_dev_touch = input_allocate_device(); |
| 602 | if (!w8001 || !input_dev_pen || !input_dev_touch) { |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 603 | err = -ENOMEM; |
| 604 | goto fail1; |
| 605 | } |
| 606 | |
| 607 | w8001->serio = serio; |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 608 | w8001->pen_dev = input_dev_pen; |
| 609 | w8001->touch_dev = input_dev_touch; |
| 610 | mutex_init(&w8001->mutex); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 611 | init_completion(&w8001->cmd_done); |
| 612 | snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys); |
| 613 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 614 | serio_set_drvdata(serio, w8001); |
| 615 | err = serio_open(serio, drv); |
| 616 | if (err) |
| 617 | goto fail2; |
| 618 | |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 619 | err = w8001_detect(w8001); |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 620 | if (err) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 621 | goto fail3; |
| 622 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 623 | /* For backwards-compatibility we compose the basename based on |
| 624 | * capabilities and then just append the tool type |
| 625 | */ |
| 626 | strlcpy(basename, "Wacom Serial", sizeof(basename)); |
| 627 | |
| 628 | err_pen = w8001_setup_pen(w8001, basename, sizeof(basename)); |
| 629 | err_touch = w8001_setup_touch(w8001, basename, sizeof(basename)); |
Peter Hutterer | 5d0a4fe | 2015-12-03 12:25:09 -0800 | [diff] [blame] | 630 | if (err_pen && err_touch) { |
| 631 | err = -ENXIO; |
| 632 | goto fail3; |
| 633 | } |
| 634 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 635 | if (!err_pen) { |
| 636 | strlcpy(w8001->pen_name, basename, sizeof(w8001->pen_name)); |
| 637 | strlcat(w8001->pen_name, " Pen", sizeof(w8001->pen_name)); |
| 638 | input_dev_pen->name = w8001->pen_name; |
Ping Cheng | 5fca6ca | 2011-01-11 01:06:58 -0800 | [diff] [blame] | 639 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 640 | w8001_set_devdata(input_dev_pen, w8001, serio); |
Dmitry Torokhov | e949674 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 641 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 642 | err = input_register_device(w8001->pen_dev); |
| 643 | if (err) |
| 644 | goto fail3; |
| 645 | } else { |
| 646 | input_free_device(input_dev_pen); |
| 647 | input_dev_pen = NULL; |
| 648 | w8001->pen_dev = NULL; |
| 649 | } |
Dmitry Torokhov | e949674 | 2011-08-23 22:57:00 -0700 | [diff] [blame] | 650 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 651 | if (!err_touch) { |
| 652 | strlcpy(w8001->touch_name, basename, sizeof(w8001->touch_name)); |
| 653 | strlcat(w8001->touch_name, " Finger", |
| 654 | sizeof(w8001->touch_name)); |
| 655 | input_dev_touch->name = w8001->touch_name; |
| 656 | |
| 657 | w8001_set_devdata(input_dev_touch, w8001, serio); |
| 658 | |
| 659 | err = input_register_device(w8001->touch_dev); |
| 660 | if (err) |
| 661 | goto fail4; |
| 662 | } else { |
| 663 | input_free_device(input_dev_touch); |
| 664 | input_dev_touch = NULL; |
| 665 | w8001->touch_dev = NULL; |
| 666 | } |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 667 | |
| 668 | return 0; |
| 669 | |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 670 | fail4: |
| 671 | if (w8001->pen_dev) |
| 672 | input_unregister_device(w8001->pen_dev); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 673 | fail3: |
| 674 | serio_close(serio); |
| 675 | fail2: |
| 676 | serio_set_drvdata(serio, NULL); |
| 677 | fail1: |
Peter Hutterer | e0361b7 | 2015-12-03 12:26:19 -0800 | [diff] [blame] | 678 | input_free_device(input_dev_pen); |
| 679 | input_free_device(input_dev_touch); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 680 | kfree(w8001); |
| 681 | return err; |
| 682 | } |
| 683 | |
| 684 | static struct serio_device_id w8001_serio_ids[] = { |
| 685 | { |
| 686 | .type = SERIO_RS232, |
| 687 | .proto = SERIO_W8001, |
| 688 | .id = SERIO_ANY, |
| 689 | .extra = SERIO_ANY, |
| 690 | }, |
| 691 | { 0 } |
| 692 | }; |
| 693 | |
| 694 | MODULE_DEVICE_TABLE(serio, w8001_serio_ids); |
| 695 | |
| 696 | static struct serio_driver w8001_drv = { |
| 697 | .driver = { |
| 698 | .name = "w8001", |
| 699 | }, |
| 700 | .description = DRIVER_DESC, |
| 701 | .id_table = w8001_serio_ids, |
| 702 | .interrupt = w8001_interrupt, |
| 703 | .connect = w8001_connect, |
| 704 | .disconnect = w8001_disconnect, |
| 705 | }; |
| 706 | |
Axel Lin | 65ac9f7 | 2012-04-03 23:50:17 -0700 | [diff] [blame] | 707 | module_serio_driver(w8001_drv); |