Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * $Id: sermouse.c,v 1.17 2002/03/13 10:03:43 vojtech Exp $ |
| 3 | * |
| 4 | * Copyright (c) 1999-2001 Vojtech Pavlik |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Serial mouse driver for Linux |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | * |
| 26 | * Should you need to contact me, the author, you can do so either by |
| 27 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
| 28 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 29 | */ |
| 30 | |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/slab.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include <linux/input.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/serio.h> |
| 37 | #include <linux/init.h> |
| 38 | |
| 39 | #define DRIVER_DESC "Serial mouse driver" |
| 40 | |
| 41 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 42 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 43 | MODULE_LICENSE("GPL"); |
| 44 | |
Helge Deller | e38de67 | 2006-09-10 21:54:39 -0400 | [diff] [blame] | 45 | static const char *sermouse_protocols[] = { "None", "Mouse Systems Mouse", "Sun Mouse", "Microsoft Mouse", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | "Logitech M+ Mouse", "Microsoft MZ Mouse", "Logitech MZ+ Mouse", |
| 47 | "Logitech MZ++ Mouse"}; |
| 48 | |
| 49 | struct sermouse { |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 50 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | signed char buf[8]; |
| 52 | unsigned char count; |
| 53 | unsigned char type; |
| 54 | unsigned long last; |
| 55 | char phys[32]; |
| 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * sermouse_process_msc() analyzes the incoming MSC/Sun bytestream and |
| 60 | * applies some prediction to the data, resulting in 96 updates per |
| 61 | * second, which is as good as a PS/2 or USB mouse. |
| 62 | */ |
| 63 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 64 | static void sermouse_process_msc(struct sermouse *sermouse, signed char data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 66 | struct input_dev *dev = sermouse->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | signed char *buf = sermouse->buf; |
| 68 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | switch (sermouse->count) { |
| 70 | |
| 71 | case 0: |
Peter Osterlund | 867d268 | 2007-03-16 00:58:37 -0400 | [diff] [blame] | 72 | if ((data & 0xf8) != 0x80) |
| 73 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | input_report_key(dev, BTN_LEFT, !(data & 4)); |
| 75 | input_report_key(dev, BTN_RIGHT, !(data & 1)); |
| 76 | input_report_key(dev, BTN_MIDDLE, !(data & 2)); |
| 77 | break; |
| 78 | |
| 79 | case 1: |
| 80 | case 3: |
| 81 | input_report_rel(dev, REL_X, data / 2); |
| 82 | input_report_rel(dev, REL_Y, -buf[1]); |
| 83 | buf[0] = data - data / 2; |
| 84 | break; |
| 85 | |
| 86 | case 2: |
| 87 | case 4: |
| 88 | input_report_rel(dev, REL_X, buf[0]); |
| 89 | input_report_rel(dev, REL_Y, buf[1] - data); |
| 90 | buf[1] = data / 2; |
| 91 | break; |
| 92 | } |
| 93 | |
| 94 | input_sync(dev); |
| 95 | |
David S. Miller | 68ca243 | 2005-12-28 13:27:04 -0800 | [diff] [blame] | 96 | if (++sermouse->count == 5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | sermouse->count = 0; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * sermouse_process_ms() anlyzes the incoming MS(Z/+/++) bytestream and |
| 102 | * generates events. With prediction it gets 80 updates/sec, assuming |
| 103 | * standard 3-byte packets and 1200 bps. |
| 104 | */ |
| 105 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 106 | static void sermouse_process_ms(struct sermouse *sermouse, signed char data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 108 | struct input_dev *dev = sermouse->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | signed char *buf = sermouse->buf; |
| 110 | |
Peter Osterlund | 867d268 | 2007-03-16 00:58:37 -0400 | [diff] [blame] | 111 | if (data & 0x40) |
| 112 | sermouse->count = 0; |
| 113 | else if (sermouse->count == 0) |
| 114 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | switch (sermouse->count) { |
| 117 | |
| 118 | case 0: |
| 119 | buf[1] = data; |
| 120 | input_report_key(dev, BTN_LEFT, (data >> 5) & 1); |
| 121 | input_report_key(dev, BTN_RIGHT, (data >> 4) & 1); |
| 122 | break; |
| 123 | |
| 124 | case 1: |
| 125 | buf[2] = data; |
| 126 | data = (signed char) (((buf[1] << 6) & 0xc0) | (data & 0x3f)); |
| 127 | input_report_rel(dev, REL_X, data / 2); |
| 128 | input_report_rel(dev, REL_Y, buf[4]); |
| 129 | buf[3] = data - data / 2; |
| 130 | break; |
| 131 | |
| 132 | case 2: |
| 133 | /* Guessing the state of the middle button on 3-button MS-protocol mice - ugly. */ |
| 134 | if ((sermouse->type == SERIO_MS) && !data && !buf[2] && !((buf[0] & 0xf0) ^ buf[1])) |
| 135 | input_report_key(dev, BTN_MIDDLE, !test_bit(BTN_MIDDLE, dev->key)); |
| 136 | buf[0] = buf[1]; |
| 137 | |
| 138 | data = (signed char) (((buf[1] << 4) & 0xc0) | (data & 0x3f)); |
| 139 | input_report_rel(dev, REL_X, buf[3]); |
| 140 | input_report_rel(dev, REL_Y, data - buf[4]); |
| 141 | buf[4] = data / 2; |
| 142 | break; |
| 143 | |
| 144 | case 3: |
| 145 | |
| 146 | switch (sermouse->type) { |
| 147 | |
| 148 | case SERIO_MS: |
| 149 | sermouse->type = SERIO_MP; |
| 150 | |
| 151 | case SERIO_MP: |
| 152 | if ((data >> 2) & 3) break; /* M++ Wireless Extension packet. */ |
| 153 | input_report_key(dev, BTN_MIDDLE, (data >> 5) & 1); |
| 154 | input_report_key(dev, BTN_SIDE, (data >> 4) & 1); |
| 155 | break; |
| 156 | |
| 157 | case SERIO_MZP: |
| 158 | case SERIO_MZPP: |
| 159 | input_report_key(dev, BTN_SIDE, (data >> 5) & 1); |
| 160 | |
| 161 | case SERIO_MZ: |
| 162 | input_report_key(dev, BTN_MIDDLE, (data >> 4) & 1); |
| 163 | input_report_rel(dev, REL_WHEEL, (data & 8) - (data & 7)); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | break; |
| 168 | |
| 169 | case 4: |
| 170 | case 6: /* MZ++ packet type. We can get these bytes for M++ too but we ignore them later. */ |
| 171 | buf[1] = (data >> 2) & 0x0f; |
| 172 | break; |
| 173 | |
| 174 | case 5: |
| 175 | case 7: /* Ignore anything besides MZ++ */ |
Peter Osterlund | 867d268 | 2007-03-16 00:58:37 -0400 | [diff] [blame] | 176 | if (sermouse->type != SERIO_MZPP) |
| 177 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | switch (buf[1]) { |
| 180 | |
| 181 | case 1: /* Extra mouse info */ |
| 182 | |
| 183 | input_report_key(dev, BTN_SIDE, (data >> 4) & 1); |
| 184 | input_report_key(dev, BTN_EXTRA, (data >> 5) & 1); |
| 185 | input_report_rel(dev, data & 0x80 ? REL_HWHEEL : REL_WHEEL, (data & 7) - (data & 8)); |
| 186 | |
| 187 | break; |
| 188 | |
| 189 | default: /* We don't decode anything else yet. */ |
| 190 | |
| 191 | printk(KERN_WARNING |
| 192 | "sermouse.c: Received MZ++ packet %x, don't know how to handle.\n", buf[1]); |
| 193 | break; |
| 194 | } |
| 195 | |
| 196 | break; |
| 197 | } |
| 198 | |
| 199 | input_sync(dev); |
| 200 | |
| 201 | sermouse->count++; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * sermouse_interrupt() handles incoming characters, either gathering them into |
| 206 | * packets or passing them to the command routine as command output. |
| 207 | */ |
| 208 | |
| 209 | static irqreturn_t sermouse_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 210 | unsigned char data, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
| 212 | struct sermouse *sermouse = serio_get_drvdata(serio); |
| 213 | |
Peter Osterlund | 867d268 | 2007-03-16 00:58:37 -0400 | [diff] [blame] | 214 | if (time_after(jiffies, sermouse->last + HZ/10)) |
| 215 | sermouse->count = 0; |
| 216 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | sermouse->last = jiffies; |
| 218 | |
| 219 | if (sermouse->type > SERIO_SUN) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 220 | sermouse_process_ms(sermouse, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | else |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 222 | sermouse_process_msc(sermouse, data); |
Peter Osterlund | 867d268 | 2007-03-16 00:58:37 -0400 | [diff] [blame] | 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | return IRQ_HANDLED; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | * sermouse_disconnect() cleans up after we don't want talk |
| 229 | * to the mouse anymore. |
| 230 | */ |
| 231 | |
| 232 | static void sermouse_disconnect(struct serio *serio) |
| 233 | { |
| 234 | struct sermouse *sermouse = serio_get_drvdata(serio); |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | serio_close(serio); |
| 237 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 238 | input_unregister_device(sermouse->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | kfree(sermouse); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * sermouse_connect() is a callback form the serio module when |
| 244 | * an unhandled serio port is found. |
| 245 | */ |
| 246 | |
| 247 | static int sermouse_connect(struct serio *serio, struct serio_driver *drv) |
| 248 | { |
| 249 | struct sermouse *sermouse; |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 250 | struct input_dev *input_dev; |
| 251 | unsigned char c = serio->id.extra; |
| 252 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 254 | sermouse = kzalloc(sizeof(struct sermouse), GFP_KERNEL); |
| 255 | input_dev = input_allocate_device(); |
| 256 | if (!sermouse || !input_dev) |
Dmitry Torokhov | 7215561 | 2006-11-05 22:40:19 -0500 | [diff] [blame] | 257 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 259 | sermouse->dev = input_dev; |
Dmitry Torokhov | 08ffce4 | 2006-06-26 01:45:10 -0400 | [diff] [blame] | 260 | snprintf(sermouse->phys, sizeof(sermouse->phys), "%s/input0", serio->phys); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 261 | sermouse->type = serio->id.proto; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 263 | input_dev->name = sermouse_protocols[sermouse->type]; |
| 264 | input_dev->phys = sermouse->phys; |
| 265 | input_dev->id.bustype = BUS_RS232; |
| 266 | input_dev->id.vendor = sermouse->type; |
| 267 | input_dev->id.product = c; |
| 268 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 28aa7f1 | 2007-04-12 01:35:09 -0400 | [diff] [blame^] | 269 | input_dev->dev.parent = &serio->dev; |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 270 | |
| 271 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL); |
| 272 | input_dev->keybit[LONG(BTN_MOUSE)] = BIT(BTN_LEFT) | BIT(BTN_RIGHT); |
| 273 | input_dev->relbit[0] = BIT(REL_X) | BIT(REL_Y); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 274 | |
| 275 | if (c & 0x01) set_bit(BTN_MIDDLE, input_dev->keybit); |
| 276 | if (c & 0x02) set_bit(BTN_SIDE, input_dev->keybit); |
| 277 | if (c & 0x04) set_bit(BTN_EXTRA, input_dev->keybit); |
| 278 | if (c & 0x10) set_bit(REL_WHEEL, input_dev->relbit); |
| 279 | if (c & 0x20) set_bit(REL_HWHEEL, input_dev->relbit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
| 281 | serio_set_drvdata(serio, sermouse); |
| 282 | |
| 283 | err = serio_open(serio, drv); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 284 | if (err) |
Dmitry Torokhov | 7215561 | 2006-11-05 22:40:19 -0500 | [diff] [blame] | 285 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | |
Dmitry Torokhov | 7215561 | 2006-11-05 22:40:19 -0500 | [diff] [blame] | 287 | err = input_register_device(sermouse->dev); |
| 288 | if (err) |
| 289 | goto fail3; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | return 0; |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 292 | |
Dmitry Torokhov | 7215561 | 2006-11-05 22:40:19 -0500 | [diff] [blame] | 293 | fail3: serio_close(serio); |
| 294 | fail2: serio_set_drvdata(serio, NULL); |
| 295 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | 2e5b636 | 2005-09-15 02:01:44 -0500 | [diff] [blame] | 296 | kfree(sermouse); |
| 297 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static struct serio_device_id sermouse_serio_ids[] = { |
| 301 | { |
| 302 | .type = SERIO_RS232, |
| 303 | .proto = SERIO_MSC, |
| 304 | .id = SERIO_ANY, |
| 305 | .extra = SERIO_ANY, |
| 306 | }, |
| 307 | { |
| 308 | .type = SERIO_RS232, |
| 309 | .proto = SERIO_SUN, |
| 310 | .id = SERIO_ANY, |
| 311 | .extra = SERIO_ANY, |
| 312 | }, |
| 313 | { |
| 314 | .type = SERIO_RS232, |
| 315 | .proto = SERIO_MS, |
| 316 | .id = SERIO_ANY, |
| 317 | .extra = SERIO_ANY, |
| 318 | }, |
| 319 | { |
| 320 | .type = SERIO_RS232, |
| 321 | .proto = SERIO_MP, |
| 322 | .id = SERIO_ANY, |
| 323 | .extra = SERIO_ANY, |
| 324 | }, |
| 325 | { |
| 326 | .type = SERIO_RS232, |
| 327 | .proto = SERIO_MZ, |
| 328 | .id = SERIO_ANY, |
| 329 | .extra = SERIO_ANY, |
| 330 | }, |
| 331 | { |
| 332 | .type = SERIO_RS232, |
| 333 | .proto = SERIO_MZP, |
| 334 | .id = SERIO_ANY, |
| 335 | .extra = SERIO_ANY, |
| 336 | }, |
| 337 | { |
| 338 | .type = SERIO_RS232, |
| 339 | .proto = SERIO_MZPP, |
| 340 | .id = SERIO_ANY, |
| 341 | .extra = SERIO_ANY, |
| 342 | }, |
| 343 | { 0 } |
| 344 | }; |
| 345 | |
| 346 | MODULE_DEVICE_TABLE(serio, sermouse_serio_ids); |
| 347 | |
| 348 | static struct serio_driver sermouse_drv = { |
| 349 | .driver = { |
| 350 | .name = "sermouse", |
| 351 | }, |
| 352 | .description = DRIVER_DESC, |
| 353 | .id_table = sermouse_serio_ids, |
| 354 | .interrupt = sermouse_interrupt, |
| 355 | .connect = sermouse_connect, |
| 356 | .disconnect = sermouse_disconnect, |
| 357 | }; |
| 358 | |
| 359 | static int __init sermouse_init(void) |
| 360 | { |
Akinobu Mita | 153a9df0 | 2006-11-23 23:35:10 -0500 | [diff] [blame] | 361 | return serio_register_driver(&sermouse_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | static void __exit sermouse_exit(void) |
| 365 | { |
| 366 | serio_unregister_driver(&sermouse_drv); |
| 367 | } |
| 368 | |
| 369 | module_init(sermouse_init); |
| 370 | module_exit(sermouse_exit); |