blob: a50e5035483231591e03d7b037fbe487f998560d [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Amiga mouse driver for Linux/m68k
4 *
5 * Copyright (c) 2000-2002 Vojtech Pavlik
6 *
7 * Based on the work of:
8 * Michael Rausch James Banks
9 * Matther Dillon David Giller
10 * Nathan Laredo Linus Torvalds
11 * Johan Myreen Jes Sorensen
12 * Russell King
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/input.h>
19#include <linux/interrupt.h>
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020020#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <asm/irq.h>
23#include <asm/setup.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080024#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/amigahw.h>
26#include <asm/amigaints.h>
27
28MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29MODULE_DESCRIPTION("Amiga mouse driver");
30MODULE_LICENSE("GPL");
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032static int amimouse_lastx, amimouse_lasty;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020034static irqreturn_t amimouse_interrupt(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020036 struct input_dev *dev = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 unsigned short joy0dat, potgor;
38 int nx, ny, dx, dy;
39
Al Virob4290a22006-01-12 01:06:12 -080040 joy0dat = amiga_custom.joy0dat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42 nx = joy0dat & 0xff;
43 ny = joy0dat >> 8;
44
45 dx = nx - amimouse_lastx;
46 dy = ny - amimouse_lasty;
47
48 if (dx < -127) dx = (256 + nx) - amimouse_lastx;
49 if (dx > 127) dx = (nx - 256) - amimouse_lastx;
50 if (dy < -127) dy = (256 + ny) - amimouse_lasty;
51 if (dy > 127) dy = (ny - 256) - amimouse_lasty;
52
53 amimouse_lastx = nx;
54 amimouse_lasty = ny;
55
Al Virob4290a22006-01-12 01:06:12 -080056 potgor = amiga_custom.potgor;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020058 input_report_rel(dev, REL_X, dx);
59 input_report_rel(dev, REL_Y, dy);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020061 input_report_key(dev, BTN_LEFT, ciaa.pra & 0x40);
62 input_report_key(dev, BTN_MIDDLE, potgor & 0x0100);
63 input_report_key(dev, BTN_RIGHT, potgor & 0x0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020065 input_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 return IRQ_HANDLED;
68}
69
70static int amimouse_open(struct input_dev *dev)
71{
72 unsigned short joy0dat;
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020073 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Al Virob4290a22006-01-12 01:06:12 -080075 joy0dat = amiga_custom.joy0dat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 amimouse_lastx = joy0dat & 0xff;
78 amimouse_lasty = joy0dat >> 8;
79
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020080 error = request_irq(IRQ_AMIGA_VERTB, amimouse_interrupt, 0, "amimouse",
81 dev);
82 if (error)
83 dev_err(&dev->dev, "Can't allocate irq %d\n", IRQ_AMIGA_VERTB);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020085 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88static void amimouse_close(struct input_dev *dev)
89{
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020090 free_irq(IRQ_AMIGA_VERTB, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020093static int __init amimouse_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Dmitry Torokhov72155612006-11-05 22:40:19 -050095 int err;
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020096 struct input_dev *dev;
Dmitry Torokhov72155612006-11-05 22:40:19 -050097
Geert Uytterhoeven314c9262009-04-05 13:11:28 +020098 dev = input_allocate_device();
99 if (!dev)
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500100 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200102 dev->name = pdev->name;
103 dev->phys = "amimouse/input0";
104 dev->id.bustype = BUS_AMIGA;
105 dev->id.vendor = 0x0001;
106 dev->id.product = 0x0002;
107 dev->id.version = 0x0100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200109 dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
110 dev->relbit[0] = BIT_MASK(REL_X) | BIT_MASK(REL_Y);
111 dev->keybit[BIT_WORD(BTN_LEFT)] = BIT_MASK(BTN_LEFT) |
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700112 BIT_MASK(BTN_MIDDLE) | BIT_MASK(BTN_RIGHT);
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200113 dev->open = amimouse_open;
114 dev->close = amimouse_close;
115 dev->dev.parent = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200117 err = input_register_device(dev);
Dmitry Torokhov72155612006-11-05 22:40:19 -0500118 if (err) {
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200119 input_free_device(dev);
Dmitry Torokhov72155612006-11-05 22:40:19 -0500120 return err;
121 }
Dmitry Torokhov2e5b6362005-09-15 02:01:44 -0500122
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200123 platform_set_drvdata(pdev, dev);
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126}
127
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200128static int __exit amimouse_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200130 struct input_dev *dev = platform_get_drvdata(pdev);
131
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200132 input_unregister_device(dev);
133 return 0;
134}
135
136static struct platform_driver amimouse_driver = {
137 .remove = __exit_p(amimouse_remove),
138 .driver = {
139 .name = "amiga-mouse",
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200140 },
141};
Dmitry Torokhovd3d25802012-01-10 15:08:01 -0800142
Sachin Kamate492fe272013-03-17 21:28:00 -0700143module_platform_driver_probe(amimouse_driver, amimouse_probe);
Geert Uytterhoeven314c9262009-04-05 13:11:28 +0200144
145MODULE_ALIAS("platform:amiga-mouse");