aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/mouse/amimouse.c
blob: 7baa09cca7c5341a5422a84fbceca005657bf2b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 *  Amiga mouse driver for Linux/m68k
 *
 *  Copyright (c) 2000-2002 Vojtech Pavlik
 *
 *  Based on the work of:
 *	Michael Rausch		James Banks
 *	Matther Dillon		David Giller
 *	Nathan Laredo		Linus Torvalds
 *	Johan Myreen		Jes Sorensen
 *	Russell King
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/input.h>
#include <linux/interrupt.h>

#include <asm/irq.h>
#include <asm/setup.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/amigahw.h>
#include <asm/amigaints.h>

MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
MODULE_DESCRIPTION("Amiga mouse driver");
MODULE_LICENSE("GPL");

static int amimouse_used = 0;
static int amimouse_lastx, amimouse_lasty;
static struct input_dev amimouse_dev;

static char *amimouse_name = "Amiga mouse";
static char *amimouse_phys = "amimouse/input0";

static irqreturn_t amimouse_interrupt(int irq, void *dummy, struct pt_regs *fp)
{
	unsigned short joy0dat, potgor;
	int nx, ny, dx, dy;

	joy0dat = custom.joy0dat;

	nx = joy0dat & 0xff;
	ny = joy0dat >> 8;

	dx = nx - amimouse_lastx;
	dy = ny - amimouse_lasty;

	if (dx < -127) dx = (256 + nx) - amimouse_lastx;
	if (dx >  127) dx = (nx - 256) - amimouse_lastx;
	if (dy < -127) dy = (256 + ny) - amimouse_lasty;
	if (dy >  127) dy = (ny - 256) - amimouse_lasty;

	amimouse_lastx = nx;
	amimouse_lasty = ny;

	potgor = custom.potgor;

	input_regs(&amimouse_dev, fp);

	input_report_rel(&amimouse_dev, REL_X, dx);
	input_report_rel(&amimouse_dev, REL_Y, dy);

	input_report_key(&amimouse_dev, BTN_LEFT,   ciaa.pra & 0x40);
	input_report_key(&amimouse_dev, BTN_MIDDLE, potgor & 0x0100);
	input_report_key(&amimouse_dev, BTN_RIGHT,  potgor & 0x0400);

	input_sync(&amimouse_dev);

	return IRQ_HANDLED;
}

static int amimouse_open(struct input_dev *dev)
{
	unsigned short joy0dat;

        if (amimouse_used++)
                return 0;

	joy0dat = custom.joy0dat;

	amimouse_lastx = joy0dat & 0xff;
	amimouse_lasty = joy0dat >> 8;

	if (request_irq(IRQ_AMIGA_VERTB, amimouse_interrupt, 0, "amimouse", amimouse_interrupt)) {
                amimouse_used--;
                printk(KERN_ERR "amimouse.c: Can't allocate irq %d\n", IRQ_AMIGA_VERTB);
                return -EBUSY;
        }

        return 0;
}

static void amimouse_close(struct input_dev *dev)
{
        if (!--amimouse_used)
		free_irq(IRQ_AMIGA_VERTB, amimouse_interrupt);
}

static int __init amimouse_init(void)
{
	if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(AMI_MOUSE))
		return -ENODEV;

	amimouse_dev.evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
	amimouse_dev.relbit[0] = BIT(REL_X) | BIT(REL_Y);
	amimouse_dev.keybit[LONG(BTN_LEFT)] = BIT(BTN_LEFT) | BIT(BTN_MIDDLE) | BIT(BTN_RIGHT);
	amimouse_dev.open = amimouse_open;
	amimouse_dev.close = amimouse_close;

	amimouse_dev.name = amimouse_name;
	amimouse_dev.phys = amimouse_phys;
	amimouse_dev.id.bustype = BUS_AMIGA;
	amimouse_dev.id.vendor = 0x0001;
	amimouse_dev.id.product = 0x0002;
	amimouse_dev.id.version = 0x0100;

	input_register_device(&amimouse_dev);

        printk(KERN_INFO "input: %s at joy0dat\n", amimouse_name);
	return 0;
}

static void __exit amimouse_exit(void)
{
        input_unregister_device(&amimouse_dev);
}

module_init(amimouse_init);
module_exit(amimouse_exit);