Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Elo serial touchscreen driver |
| 3 | * |
| 4 | * Copyright (c) 2004 Vojtech Pavlik |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published by |
| 10 | * the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * This driver can handle serial Elo touchscreens using either the Elo standard |
| 15 | * 'E271-2210' 10-byte protocol, Elo legacy 'E281A-4002' 6-byte protocol, Elo |
| 16 | * legacy 'E271-140' 4-byte protocol and Elo legacy 'E261-280' 3-byte protocol. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/errno.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/input.h> |
| 24 | #include <linux/serio.h> |
| 25 | #include <linux/init.h> |
| 26 | |
| 27 | #define DRIVER_DESC "Elo serial touchscreen driver" |
| 28 | |
| 29 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 30 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 31 | MODULE_LICENSE("GPL"); |
| 32 | |
| 33 | /* |
| 34 | * Definitions & global arrays. |
| 35 | */ |
| 36 | |
| 37 | #define ELO_MAX_LENGTH 10 |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | /* |
| 40 | * Per-touchscreen data. |
| 41 | */ |
| 42 | |
| 43 | struct elo { |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 44 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | struct serio *serio; |
| 46 | int id; |
| 47 | int idx; |
| 48 | unsigned char csum; |
| 49 | unsigned char data[ELO_MAX_LENGTH]; |
| 50 | char phys[32]; |
| 51 | }; |
| 52 | |
| 53 | static void elo_process_data_10(struct elo* elo, unsigned char data, struct pt_regs *regs) |
| 54 | { |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 55 | struct input_dev *dev = elo->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | elo->csum += elo->data[elo->idx] = data; |
| 58 | |
| 59 | switch (elo->idx++) { |
| 60 | |
| 61 | case 0: |
| 62 | if (data != 'U') { |
| 63 | elo->idx = 0; |
| 64 | elo->csum = 0; |
| 65 | } |
| 66 | break; |
| 67 | |
| 68 | case 1: |
| 69 | if (data != 'T') { |
| 70 | elo->idx = 0; |
| 71 | elo->csum = 0; |
| 72 | } |
| 73 | break; |
| 74 | |
| 75 | case 9: |
| 76 | if (elo->csum) { |
| 77 | input_regs(dev, regs); |
| 78 | input_report_abs(dev, ABS_X, (elo->data[4] << 8) | elo->data[3]); |
| 79 | input_report_abs(dev, ABS_Y, (elo->data[6] << 8) | elo->data[5]); |
| 80 | input_report_abs(dev, ABS_PRESSURE, (elo->data[8] << 8) | elo->data[7]); |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 81 | input_report_key(dev, BTN_TOUCH, elo->data[8] || elo->data[7]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | input_sync(dev); |
| 83 | } |
| 84 | elo->idx = 0; |
| 85 | elo->csum = 0; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void elo_process_data_6(struct elo* elo, unsigned char data, struct pt_regs *regs) |
| 91 | { |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 92 | struct input_dev *dev = elo->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | elo->data[elo->idx] = data; |
| 95 | |
| 96 | switch (elo->idx++) { |
| 97 | |
| 98 | case 0: if ((data & 0xc0) != 0xc0) elo->idx = 0; break; |
| 99 | case 1: if ((data & 0xc0) != 0x80) elo->idx = 0; break; |
| 100 | case 2: if ((data & 0xc0) != 0x40) elo->idx = 0; break; |
| 101 | |
| 102 | case 3: |
| 103 | if (data & 0xc0) { |
| 104 | elo->idx = 0; |
| 105 | break; |
| 106 | } |
| 107 | |
| 108 | input_regs(dev, regs); |
| 109 | input_report_abs(dev, ABS_X, ((elo->data[0] & 0x3f) << 6) | (elo->data[1] & 0x3f)); |
| 110 | input_report_abs(dev, ABS_Y, ((elo->data[2] & 0x3f) << 6) | (elo->data[3] & 0x3f)); |
| 111 | |
| 112 | if (elo->id == 2) { |
| 113 | input_report_key(dev, BTN_TOUCH, 1); |
| 114 | input_sync(dev); |
| 115 | elo->idx = 0; |
| 116 | } |
| 117 | |
| 118 | break; |
| 119 | |
| 120 | case 4: |
| 121 | if (data) { |
| 122 | input_sync(dev); |
| 123 | elo->idx = 0; |
| 124 | } |
| 125 | break; |
| 126 | |
| 127 | case 5: |
| 128 | if ((data & 0xf0) == 0) { |
| 129 | input_report_abs(dev, ABS_PRESSURE, elo->data[5]); |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 130 | input_report_key(dev, BTN_TOUCH, !!elo->data[5]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | input_sync(dev); |
| 133 | elo->idx = 0; |
| 134 | break; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | static void elo_process_data_3(struct elo* elo, unsigned char data, struct pt_regs *regs) |
| 139 | { |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 140 | struct input_dev *dev = elo->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | elo->data[elo->idx] = data; |
| 143 | |
| 144 | switch (elo->idx++) { |
| 145 | |
| 146 | case 0: |
| 147 | if ((data & 0x7f) != 0x01) |
| 148 | elo->idx = 0; |
| 149 | break; |
| 150 | case 2: |
| 151 | input_regs(dev, regs); |
| 152 | input_report_key(dev, BTN_TOUCH, !(elo->data[1] & 0x80)); |
| 153 | input_report_abs(dev, ABS_X, elo->data[1]); |
| 154 | input_report_abs(dev, ABS_Y, elo->data[2]); |
| 155 | input_sync(dev); |
| 156 | elo->idx = 0; |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static irqreturn_t elo_interrupt(struct serio *serio, |
| 162 | unsigned char data, unsigned int flags, struct pt_regs *regs) |
| 163 | { |
| 164 | struct elo* elo = serio_get_drvdata(serio); |
| 165 | |
| 166 | switch(elo->id) { |
| 167 | case 0: |
| 168 | elo_process_data_10(elo, data, regs); |
| 169 | break; |
| 170 | |
| 171 | case 1: |
| 172 | case 2: |
| 173 | elo_process_data_6(elo, data, regs); |
| 174 | break; |
| 175 | |
| 176 | case 3: |
| 177 | elo_process_data_3(elo, data, regs); |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | return IRQ_HANDLED; |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * elo_disconnect() is the opposite of elo_connect() |
| 186 | */ |
| 187 | |
| 188 | static void elo_disconnect(struct serio *serio) |
| 189 | { |
| 190 | struct elo* elo = serio_get_drvdata(serio); |
| 191 | |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 192 | input_unregister_device(elo->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | serio_close(serio); |
| 194 | serio_set_drvdata(serio, NULL); |
| 195 | kfree(elo); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * elo_connect() is the routine that is called when someone adds a |
| 200 | * new serio device that supports Gunze protocol and registers it as |
| 201 | * an input device. |
| 202 | */ |
| 203 | |
| 204 | static int elo_connect(struct serio *serio, struct serio_driver *drv) |
| 205 | { |
| 206 | struct elo *elo; |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 207 | struct input_dev *input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | int err; |
| 209 | |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 210 | elo = kzalloc(sizeof(struct elo), GFP_KERNEL); |
| 211 | input_dev = input_allocate_device(); |
| 212 | if (!elo || !input_dev) { |
| 213 | err = -ENOMEM; |
| 214 | goto fail; |
| 215 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 217 | elo->serio = serio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | elo->id = serio->id.id; |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 219 | elo->dev = input_dev; |
| 220 | snprintf(elo->phys, sizeof(elo->phys), "%s/input0", serio->phys); |
| 221 | |
| 222 | input_dev->private = elo; |
| 223 | input_dev->name = "Elo Serial TouchScreen"; |
| 224 | input_dev->phys = elo->phys; |
| 225 | input_dev->id.bustype = BUS_RS232; |
| 226 | input_dev->id.vendor = SERIO_ELO; |
| 227 | input_dev->id.product = elo->id; |
| 228 | input_dev->id.version = 0x0100; |
| 229 | input_dev->cdev.dev = &serio->dev; |
| 230 | |
| 231 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
| 232 | input_dev->keybit[LONG(BTN_TOUCH)] = BIT(BTN_TOUCH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | switch (elo->id) { |
| 235 | |
| 236 | case 0: /* 10-byte protocol */ |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 237 | input_set_abs_params(input_dev, ABS_X, 96, 4000, 0, 0); |
| 238 | input_set_abs_params(input_dev, ABS_Y, 96, 4000, 0, 0); |
| 239 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, 255, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | break; |
Dmitry Torokhov | de1b963 | 2005-05-29 02:28:50 -0500 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | case 1: /* 6-byte protocol */ |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 243 | input_set_abs_params(input_dev, ABS_PRESSURE, 0, 15, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
| 245 | case 2: /* 4-byte protocol */ |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 246 | input_set_abs_params(input_dev, ABS_X, 96, 4000, 0, 0); |
| 247 | input_set_abs_params(input_dev, ABS_Y, 96, 4000, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | break; |
| 249 | |
| 250 | case 3: /* 3-byte protocol */ |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 251 | input_set_abs_params(input_dev, ABS_X, 0, 255, 0, 0); |
| 252 | input_set_abs_params(input_dev, ABS_Y, 0, 255, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | break; |
| 254 | } |
| 255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | serio_set_drvdata(serio, elo); |
| 257 | |
| 258 | err = serio_open(serio, drv); |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 259 | if (err) |
| 260 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 262 | input_register_device(elo->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | return 0; |
Dmitry Torokhov | eca1ed1 | 2005-09-15 02:01:46 -0500 | [diff] [blame] | 264 | |
| 265 | fail: serio_set_drvdata(serio, NULL); |
| 266 | input_free_device(input_dev); |
| 267 | kfree(elo); |
| 268 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
| 272 | * The serio driver structure. |
| 273 | */ |
| 274 | |
| 275 | static struct serio_device_id elo_serio_ids[] = { |
| 276 | { |
| 277 | .type = SERIO_RS232, |
| 278 | .proto = SERIO_ELO, |
| 279 | .id = SERIO_ANY, |
| 280 | .extra = SERIO_ANY, |
| 281 | }, |
| 282 | { 0 } |
| 283 | }; |
| 284 | |
| 285 | MODULE_DEVICE_TABLE(serio, elo_serio_ids); |
| 286 | |
| 287 | static struct serio_driver elo_drv = { |
| 288 | .driver = { |
| 289 | .name = "elo", |
| 290 | }, |
| 291 | .description = DRIVER_DESC, |
| 292 | .id_table = elo_serio_ids, |
| 293 | .interrupt = elo_interrupt, |
| 294 | .connect = elo_connect, |
| 295 | .disconnect = elo_disconnect, |
| 296 | }; |
| 297 | |
| 298 | /* |
| 299 | * The functions for inserting/removing us as a module. |
| 300 | */ |
| 301 | |
| 302 | static int __init elo_init(void) |
| 303 | { |
| 304 | serio_register_driver(&elo_drv); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static void __exit elo_exit(void) |
| 309 | { |
| 310 | serio_unregister_driver(&elo_drv); |
| 311 | } |
| 312 | |
| 313 | module_init(elo_init); |
| 314 | module_exit(elo_exit); |