Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Penmount serial touchscreen driver |
| 3 | * |
| 4 | * Copyright (c) 2006 Rick Koch <n1gp@hotmail.com> |
John Sung | 21ae508 | 2011-09-09 13:33:12 -0700 | [diff] [blame] | 5 | * Copyright (c) 2011 John Sung <penmount.touch@gmail.com> |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 6 | * |
| 7 | * Based on ELO driver (drivers/input/touchscreen/elo.c) |
| 8 | * Copyright (c) 2004 Vojtech Pavlik |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License version 2 as published |
| 14 | * by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/input.h> |
| 22 | #include <linux/serio.h> |
| 23 | #include <linux/init.h> |
| 24 | |
John Sung | 21ae508 | 2011-09-09 13:33:12 -0700 | [diff] [blame] | 25 | #define DRIVER_DESC "PenMount serial touchscreen driver" |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 26 | |
| 27 | MODULE_AUTHOR("Rick Koch <n1gp@hotmail.com>"); |
John Sung | 21ae508 | 2011-09-09 13:33:12 -0700 | [diff] [blame] | 28 | MODULE_AUTHOR("John Sung <penmount.touch@gmail.com>"); |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 29 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 30 | MODULE_LICENSE("GPL"); |
| 31 | |
| 32 | /* |
| 33 | * Definitions & global arrays. |
| 34 | */ |
| 35 | |
John Sung | c42e2e4 | 2011-09-09 13:33:12 -0700 | [diff] [blame^] | 36 | #define PM_MAX_LENGTH 6 |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 37 | |
| 38 | /* |
| 39 | * Per-touchscreen data. |
| 40 | */ |
| 41 | |
| 42 | struct pm { |
| 43 | struct input_dev *dev; |
| 44 | struct serio *serio; |
| 45 | int idx; |
| 46 | unsigned char data[PM_MAX_LENGTH]; |
| 47 | char phys[32]; |
John Sung | c42e2e4 | 2011-09-09 13:33:12 -0700 | [diff] [blame^] | 48 | unsigned char packetsize; |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 49 | }; |
| 50 | |
John Sung | c42e2e4 | 2011-09-09 13:33:12 -0700 | [diff] [blame^] | 51 | /* |
| 52 | * pm_checkpacket() checks if data packet is valid |
| 53 | */ |
| 54 | |
| 55 | static bool pm_checkpacket(unsigned char *packet) |
| 56 | { |
| 57 | int total = 0; |
| 58 | int i; |
| 59 | |
| 60 | for (i = 0; i < 5; i++) |
| 61 | total += packet[i]; |
| 62 | |
| 63 | return packet[5] == (unsigned char)~(total & 0xff); |
| 64 | } |
| 65 | |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 66 | static irqreturn_t pm_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 67 | unsigned char data, unsigned int flags) |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 68 | { |
| 69 | struct pm *pm = serio_get_drvdata(serio); |
| 70 | struct input_dev *dev = pm->dev; |
| 71 | |
| 72 | pm->data[pm->idx] = data; |
| 73 | |
John Sung | c42e2e4 | 2011-09-09 13:33:12 -0700 | [diff] [blame^] | 74 | switch (pm->dev->id.product) { |
| 75 | case 0x9000: |
| 76 | if (pm->data[0] & 0x80) { |
| 77 | if (pm->packetsize == ++pm->idx) { |
| 78 | input_report_abs(dev, ABS_X, pm->data[1] * 128 + pm->data[2]); |
| 79 | input_report_abs(dev, ABS_Y, pm->data[3] * 128 + pm->data[4]); |
| 80 | input_report_key(dev, BTN_TOUCH, !!(pm->data[0] & 0x40)); |
| 81 | input_sync(dev); |
| 82 | pm->idx = 0; |
| 83 | } |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 84 | } |
John Sung | c42e2e4 | 2011-09-09 13:33:12 -0700 | [diff] [blame^] | 85 | break; |
| 86 | |
| 87 | case 0x6000: |
| 88 | if ((pm->data[0] & 0xbf) == 0x30) { |
| 89 | if (pm->packetsize == ++pm->idx) { |
| 90 | if (pm_checkpacket(pm->data)) { |
| 91 | input_report_abs(dev, ABS_X, |
| 92 | pm->data[2] * 256 + pm->data[1]); |
| 93 | input_report_abs(dev, ABS_Y, |
| 94 | pm->data[4] * 256 + pm->data[3]); |
| 95 | input_report_key(dev, BTN_TOUCH, !!(pm->data[0] & 0x40)); |
| 96 | input_sync(dev); |
| 97 | } |
| 98 | pm->idx = 0; |
| 99 | } |
| 100 | } |
| 101 | break; |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return IRQ_HANDLED; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * pm_disconnect() is the opposite of pm_connect() |
| 109 | */ |
| 110 | |
| 111 | static void pm_disconnect(struct serio *serio) |
| 112 | { |
| 113 | struct pm *pm = serio_get_drvdata(serio); |
| 114 | |
| 115 | input_get_device(pm->dev); |
| 116 | input_unregister_device(pm->dev); |
| 117 | serio_close(serio); |
| 118 | serio_set_drvdata(serio, NULL); |
| 119 | input_put_device(pm->dev); |
| 120 | kfree(pm); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * pm_connect() is the routine that is called when someone adds a |
John Sung | 21ae508 | 2011-09-09 13:33:12 -0700 | [diff] [blame] | 125 | * new serio device that supports PenMount protocol and registers it as |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 126 | * an input device. |
| 127 | */ |
| 128 | |
| 129 | static int pm_connect(struct serio *serio, struct serio_driver *drv) |
| 130 | { |
| 131 | struct pm *pm; |
| 132 | struct input_dev *input_dev; |
| 133 | int err; |
| 134 | |
| 135 | pm = kzalloc(sizeof(struct pm), GFP_KERNEL); |
| 136 | input_dev = input_allocate_device(); |
| 137 | if (!pm || !input_dev) { |
| 138 | err = -ENOMEM; |
| 139 | goto fail1; |
| 140 | } |
| 141 | |
| 142 | pm->serio = serio; |
| 143 | pm->dev = input_dev; |
| 144 | snprintf(pm->phys, sizeof(pm->phys), "%s/input0", serio->phys); |
| 145 | |
John Sung | 21ae508 | 2011-09-09 13:33:12 -0700 | [diff] [blame] | 146 | input_dev->name = "PenMount Serial TouchScreen"; |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 147 | input_dev->phys = pm->phys; |
| 148 | input_dev->id.bustype = BUS_RS232; |
| 149 | input_dev->id.vendor = SERIO_PENMOUNT; |
| 150 | input_dev->id.product = 0; |
| 151 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | a5394fb0 | 2007-04-12 01:35:14 -0400 | [diff] [blame] | 152 | input_dev->dev.parent = &serio->dev; |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 153 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 154 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 155 | input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH); |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 156 | input_set_abs_params(pm->dev, ABS_X, 0, 0x3ff, 0, 0); |
| 157 | input_set_abs_params(pm->dev, ABS_Y, 0, 0x3ff, 0, 0); |
| 158 | |
John Sung | c42e2e4 | 2011-09-09 13:33:12 -0700 | [diff] [blame^] | 159 | switch (serio->id.id) { |
| 160 | default: |
| 161 | case 0: |
| 162 | pm->packetsize = 5; |
| 163 | input_dev->id.product = 0x9000; |
| 164 | break; |
| 165 | |
| 166 | case 1: |
| 167 | pm->packetsize = 6; |
| 168 | input_dev->id.product = 0x6000; |
| 169 | break; |
| 170 | } |
| 171 | |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 172 | serio_set_drvdata(serio, pm); |
| 173 | |
| 174 | err = serio_open(serio, drv); |
| 175 | if (err) |
| 176 | goto fail2; |
| 177 | |
| 178 | err = input_register_device(pm->dev); |
| 179 | if (err) |
| 180 | goto fail3; |
| 181 | |
| 182 | return 0; |
| 183 | |
| 184 | fail3: serio_close(serio); |
| 185 | fail2: serio_set_drvdata(serio, NULL); |
| 186 | fail1: input_free_device(input_dev); |
| 187 | kfree(pm); |
| 188 | return err; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * The serio driver structure. |
| 193 | */ |
| 194 | |
| 195 | static struct serio_device_id pm_serio_ids[] = { |
| 196 | { |
| 197 | .type = SERIO_RS232, |
| 198 | .proto = SERIO_PENMOUNT, |
| 199 | .id = SERIO_ANY, |
| 200 | .extra = SERIO_ANY, |
| 201 | }, |
| 202 | { 0 } |
| 203 | }; |
| 204 | |
| 205 | MODULE_DEVICE_TABLE(serio, pm_serio_ids); |
| 206 | |
| 207 | static struct serio_driver pm_drv = { |
| 208 | .driver = { |
John Sung | 21ae508 | 2011-09-09 13:33:12 -0700 | [diff] [blame] | 209 | .name = "serio-penmount", |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 210 | }, |
| 211 | .description = DRIVER_DESC, |
| 212 | .id_table = pm_serio_ids, |
| 213 | .interrupt = pm_interrupt, |
| 214 | .connect = pm_connect, |
| 215 | .disconnect = pm_disconnect, |
| 216 | }; |
| 217 | |
| 218 | /* |
| 219 | * The functions for inserting/removing us as a module. |
| 220 | */ |
| 221 | |
| 222 | static int __init pm_init(void) |
| 223 | { |
Akinobu Mita | 153a9df0 | 2006-11-23 23:35:10 -0500 | [diff] [blame] | 224 | return serio_register_driver(&pm_drv); |
Rick Koch | ee47999 | 2006-08-05 00:32:18 -0400 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static void __exit pm_exit(void) |
| 228 | { |
| 229 | serio_unregister_driver(&pm_drv); |
| 230 | } |
| 231 | |
| 232 | module_init(pm_init); |
| 233 | module_exit(pm_exit); |