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 |
| 5 | * |
| 6 | * This file is subject to the terms and conditions of the GNU General Public |
| 7 | * License. See the file COPYING in the main directory of this archive for |
| 8 | * more details. |
| 9 | * |
| 10 | * Layout based on Elo serial touchscreen driver by Vojtech Pavlik |
| 11 | */ |
| 12 | |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/input.h> |
| 18 | #include <linux/serio.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/ctype.h> |
| 21 | |
| 22 | #define DRIVER_DESC "Wacom W8001 serial touchscreen driver" |
| 23 | |
| 24 | MODULE_AUTHOR("Jaya Kumar <jayakumar.lkml@gmail.com>"); |
| 25 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 28 | #define W8001_MAX_LENGTH 11 |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 29 | #define W8001_LEAD_MASK 0x80 |
| 30 | #define W8001_LEAD_BYTE 0x80 |
| 31 | #define W8001_TAB_MASK 0x40 |
| 32 | #define W8001_TAB_BYTE 0x40 |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 33 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 34 | #define W8001_QUERY_PACKET 0x20 |
| 35 | |
| 36 | #define W8001_CMD_START '1' |
| 37 | #define W8001_CMD_QUERY '*' |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 38 | |
| 39 | struct w8001_coord { |
| 40 | u8 rdy; |
| 41 | u8 tsw; |
| 42 | u8 f1; |
| 43 | u8 f2; |
| 44 | u16 x; |
| 45 | u16 y; |
| 46 | u16 pen_pressure; |
| 47 | u8 tilt_x; |
| 48 | u8 tilt_y; |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * Per-touchscreen data. |
| 53 | */ |
| 54 | |
| 55 | struct w8001 { |
| 56 | struct input_dev *dev; |
| 57 | struct serio *serio; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 58 | struct completion cmd_done; |
| 59 | int id; |
| 60 | int idx; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 61 | unsigned char response_type; |
| 62 | unsigned char response[W8001_MAX_LENGTH]; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 63 | unsigned char data[W8001_MAX_LENGTH]; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 64 | char phys[32]; |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame^] | 65 | int type; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 66 | }; |
| 67 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 68 | static void parse_data(u8 *data, struct w8001_coord *coord) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 69 | { |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 70 | memset(coord, 0, sizeof(*coord)); |
| 71 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 72 | coord->rdy = data[0] & 0x20; |
| 73 | coord->tsw = data[0] & 0x01; |
| 74 | coord->f1 = data[0] & 0x02; |
| 75 | coord->f2 = data[0] & 0x04; |
| 76 | |
| 77 | coord->x = (data[1] & 0x7F) << 9; |
| 78 | coord->x |= (data[2] & 0x7F) << 2; |
| 79 | coord->x |= (data[6] & 0x60) >> 5; |
| 80 | |
| 81 | coord->y = (data[3] & 0x7F) << 9; |
| 82 | coord->y |= (data[4] & 0x7F) << 2; |
| 83 | coord->y |= (data[6] & 0x18) >> 3; |
| 84 | |
| 85 | coord->pen_pressure = data[5] & 0x7F; |
| 86 | coord->pen_pressure |= (data[6] & 0x07) << 7 ; |
| 87 | |
| 88 | coord->tilt_x = data[7] & 0x7F; |
| 89 | coord->tilt_y = data[8] & 0x7F; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 90 | } |
| 91 | |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame^] | 92 | static void report_pen_events(struct w8001 *w8001, struct w8001_coord *coord) |
| 93 | { |
| 94 | struct input_dev *dev = w8001->dev; |
| 95 | |
| 96 | /* |
| 97 | * We have 1 bit for proximity (rdy) and 3 bits for tip, side, |
| 98 | * side2/eraser. If rdy && f2 are set, this can be either pen + side2, |
| 99 | * or eraser. assume |
| 100 | * - if dev is already in proximity and f2 is toggled → pen + side2 |
| 101 | * - if dev comes into proximity with f2 set → eraser |
| 102 | * If f2 disappears after assuming eraser, fake proximity out for |
| 103 | * eraser and in for pen. |
| 104 | */ |
| 105 | |
| 106 | if (!w8001->type) { |
| 107 | w8001->type = coord->f2 ? BTN_TOOL_RUBBER : BTN_TOOL_PEN; |
| 108 | } else if (w8001->type == BTN_TOOL_RUBBER) { |
| 109 | if (!coord->f2) { |
| 110 | input_report_abs(dev, ABS_PRESSURE, 0); |
| 111 | input_report_key(dev, BTN_TOUCH, 0); |
| 112 | input_report_key(dev, BTN_STYLUS, 0); |
| 113 | input_report_key(dev, BTN_STYLUS2, 0); |
| 114 | input_report_key(dev, BTN_TOOL_RUBBER, 0); |
| 115 | input_sync(dev); |
| 116 | w8001->type = BTN_TOOL_PEN; |
| 117 | } |
| 118 | } else { |
| 119 | input_report_key(dev, BTN_STYLUS2, coord->f2); |
| 120 | } |
| 121 | |
| 122 | input_report_abs(dev, ABS_X, coord->x); |
| 123 | input_report_abs(dev, ABS_Y, coord->y); |
| 124 | input_report_abs(dev, ABS_PRESSURE, coord->pen_pressure); |
| 125 | input_report_key(dev, BTN_TOUCH, coord->tsw); |
| 126 | input_report_key(dev, BTN_STYLUS, coord->f1); |
| 127 | input_report_key(dev, w8001->type, coord->rdy); |
| 128 | input_sync(dev); |
| 129 | |
| 130 | if (!coord->rdy) |
| 131 | w8001->type = 0; |
| 132 | } |
| 133 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 134 | static irqreturn_t w8001_interrupt(struct serio *serio, |
| 135 | unsigned char data, unsigned int flags) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 136 | { |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 137 | struct w8001 *w8001 = serio_get_drvdata(serio); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 138 | struct w8001_coord coord; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 139 | unsigned char tmp; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 140 | |
| 141 | w8001->data[w8001->idx] = data; |
| 142 | switch (w8001->idx++) { |
| 143 | case 0: |
| 144 | if ((data & W8001_LEAD_MASK) != W8001_LEAD_BYTE) { |
| 145 | pr_debug("w8001: unsynchronized data: 0x%02x\n", data); |
| 146 | w8001->idx = 0; |
| 147 | } |
| 148 | break; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 149 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 150 | case 8: |
| 151 | tmp = w8001->data[0] & W8001_TAB_MASK; |
| 152 | if (unlikely(tmp == W8001_TAB_BYTE)) |
| 153 | break; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 154 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 155 | w8001->idx = 0; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 156 | parse_data(w8001->data, &coord); |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame^] | 157 | report_pen_events(w8001, &coord); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 158 | break; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 159 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 160 | case 10: |
| 161 | w8001->idx = 0; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 162 | memcpy(w8001->response, w8001->data, W8001_MAX_LENGTH); |
| 163 | w8001->response_type = W8001_QUERY_PACKET; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 164 | complete(&w8001->cmd_done); |
| 165 | break; |
| 166 | } |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 167 | |
| 168 | return IRQ_HANDLED; |
| 169 | } |
| 170 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 171 | static int w8001_command(struct w8001 *w8001, unsigned char command, |
| 172 | bool wait_response) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 173 | { |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 174 | int rc; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 175 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 176 | w8001->response_type = 0; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 177 | init_completion(&w8001->cmd_done); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 178 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 179 | rc = serio_write(w8001->serio, command); |
| 180 | if (rc == 0 && wait_response) { |
| 181 | |
| 182 | wait_for_completion_timeout(&w8001->cmd_done, HZ); |
| 183 | if (w8001->response_type != W8001_QUERY_PACKET) |
| 184 | rc = -EIO; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 187 | return rc; |
| 188 | } |
| 189 | |
| 190 | static int w8001_setup(struct w8001 *w8001) |
| 191 | { |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 192 | struct input_dev *dev = w8001->dev; |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 193 | struct w8001_coord coord; |
| 194 | int error; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 195 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 196 | error = w8001_command(w8001, W8001_CMD_QUERY, true); |
| 197 | if (error) |
| 198 | return error; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 199 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 200 | parse_data(w8001->response, &coord); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 201 | |
| 202 | input_set_abs_params(dev, ABS_X, 0, coord.x, 0, 0); |
| 203 | input_set_abs_params(dev, ABS_Y, 0, coord.y, 0, 0); |
| 204 | input_set_abs_params(dev, ABS_PRESSURE, 0, coord.pen_pressure, 0, 0); |
| 205 | input_set_abs_params(dev, ABS_TILT_X, 0, coord.tilt_x, 0, 0); |
| 206 | input_set_abs_params(dev, ABS_TILT_Y, 0, coord.tilt_y, 0, 0); |
| 207 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 208 | return w8001_command(w8001, W8001_CMD_START, false); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* |
| 212 | * w8001_disconnect() is the opposite of w8001_connect() |
| 213 | */ |
| 214 | |
| 215 | static void w8001_disconnect(struct serio *serio) |
| 216 | { |
| 217 | struct w8001 *w8001 = serio_get_drvdata(serio); |
| 218 | |
| 219 | input_get_device(w8001->dev); |
| 220 | input_unregister_device(w8001->dev); |
| 221 | serio_close(serio); |
| 222 | serio_set_drvdata(serio, NULL); |
| 223 | input_put_device(w8001->dev); |
| 224 | kfree(w8001); |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * w8001_connect() is the routine that is called when someone adds a |
| 229 | * new serio device that supports the w8001 protocol and registers it as |
| 230 | * an input device. |
| 231 | */ |
| 232 | |
| 233 | static int w8001_connect(struct serio *serio, struct serio_driver *drv) |
| 234 | { |
| 235 | struct w8001 *w8001; |
| 236 | struct input_dev *input_dev; |
| 237 | int err; |
| 238 | |
| 239 | w8001 = kzalloc(sizeof(struct w8001), GFP_KERNEL); |
| 240 | input_dev = input_allocate_device(); |
| 241 | if (!w8001 || !input_dev) { |
| 242 | err = -ENOMEM; |
| 243 | goto fail1; |
| 244 | } |
| 245 | |
| 246 | w8001->serio = serio; |
| 247 | w8001->id = serio->id.id; |
| 248 | w8001->dev = input_dev; |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 249 | init_completion(&w8001->cmd_done); |
| 250 | snprintf(w8001->phys, sizeof(w8001->phys), "%s/input0", serio->phys); |
| 251 | |
| 252 | input_dev->name = "Wacom W8001 Penabled Serial TouchScreen"; |
| 253 | input_dev->phys = w8001->phys; |
| 254 | input_dev->id.bustype = BUS_RS232; |
| 255 | input_dev->id.vendor = SERIO_W8001; |
| 256 | input_dev->id.product = w8001->id; |
| 257 | input_dev->id.version = 0x0100; |
| 258 | input_dev->dev.parent = &serio->dev; |
| 259 | |
| 260 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 261 | input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
Peter Hutterer | 2072f8d | 2010-08-28 22:00:05 -0700 | [diff] [blame^] | 262 | input_dev->keybit[BIT_WORD(BTN_TOOL_PEN)] |= BIT_MASK(BTN_TOOL_PEN); |
| 263 | input_dev->keybit[BIT_WORD(BTN_TOOL_RUBBER)] |= BIT_MASK(BTN_TOOL_RUBBER); |
| 264 | input_dev->keybit[BIT_WORD(BTN_STYLUS)] |= BIT_MASK(BTN_STYLUS); |
| 265 | input_dev->keybit[BIT_WORD(BTN_STYLUS2)] |= BIT_MASK(BTN_STYLUS2); |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 266 | |
| 267 | serio_set_drvdata(serio, w8001); |
| 268 | err = serio_open(serio, drv); |
| 269 | if (err) |
| 270 | goto fail2; |
| 271 | |
Dmitry Torokhov | 41c372d | 2009-09-03 17:22:03 -0700 | [diff] [blame] | 272 | err = w8001_setup(w8001); |
| 273 | if (err) |
Jaya Kumar | 3eb1aa4 | 2008-11-19 16:58:50 -0500 | [diff] [blame] | 274 | goto fail3; |
| 275 | |
| 276 | err = input_register_device(w8001->dev); |
| 277 | if (err) |
| 278 | goto fail3; |
| 279 | |
| 280 | return 0; |
| 281 | |
| 282 | fail3: |
| 283 | serio_close(serio); |
| 284 | fail2: |
| 285 | serio_set_drvdata(serio, NULL); |
| 286 | fail1: |
| 287 | input_free_device(input_dev); |
| 288 | kfree(w8001); |
| 289 | return err; |
| 290 | } |
| 291 | |
| 292 | static struct serio_device_id w8001_serio_ids[] = { |
| 293 | { |
| 294 | .type = SERIO_RS232, |
| 295 | .proto = SERIO_W8001, |
| 296 | .id = SERIO_ANY, |
| 297 | .extra = SERIO_ANY, |
| 298 | }, |
| 299 | { 0 } |
| 300 | }; |
| 301 | |
| 302 | MODULE_DEVICE_TABLE(serio, w8001_serio_ids); |
| 303 | |
| 304 | static struct serio_driver w8001_drv = { |
| 305 | .driver = { |
| 306 | .name = "w8001", |
| 307 | }, |
| 308 | .description = DRIVER_DESC, |
| 309 | .id_table = w8001_serio_ids, |
| 310 | .interrupt = w8001_interrupt, |
| 311 | .connect = w8001_connect, |
| 312 | .disconnect = w8001_disconnect, |
| 313 | }; |
| 314 | |
| 315 | static int __init w8001_init(void) |
| 316 | { |
| 317 | return serio_register_driver(&w8001_drv); |
| 318 | } |
| 319 | |
| 320 | static void __exit w8001_exit(void) |
| 321 | { |
| 322 | serio_unregister_driver(&w8001_drv); |
| 323 | } |
| 324 | |
| 325 | module_init(w8001_init); |
| 326 | module_exit(w8001_exit); |