summaryrefslogtreecommitdiff
path: root/sys/arch/dreamcast/dev/maple/mms.c
blob: 0f2899fa92043bbc40196708a68060a4b044adc3 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*	$NetBSD: mms.c,v 1.19 2021/08/07 16:18:48 thorpej Exp $	*/

/*-
 * Copyright (c) 2001 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jason R. Thorpe.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: mms.c,v 1.19 2021/08/07 16:18:48 thorpej Exp $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/systm.h>

#include "wsmouse.h"

#include <dev/wscons/wsconsio.h> 
#include <dev/wscons/wsmousevar.h>

#include <dreamcast/dev/maple/maple.h>
#include <dreamcast/dev/maple/mapleconf.h>

struct mms_condition {
	uint32_t func_code;	/* function code (big endian) */
	uint32_t buttons;
	uint16_t axis1;		/* X */
	uint16_t axis2;		/* Y */
	uint16_t axis3;		/* wheel */
	uint16_t axis4;
	uint16_t axis5;
	uint16_t axis6;
	uint16_t axis7;
	uint16_t axis8;
};

#define	MMS_BUTTON_C		0x01	/* middle */
#define	MMS_BUTTON_B		0x02	/* right */
#define	MMS_BUTTON_A		0x04	/* left */
#define	MMS_BUTTON_START	0x08	/* thumb */
#define	MMS_BUTTON_MASK		0x0f

#define	MMS_MOVEMENT_BASE	0x200
#define	MMS_MOVEMENT_MAX	0x3ff

#define	MMS_FUNCDATA_AXIS1	0x001
#define	MMS_FUNCDATA_AXIS2	0x002
#define	MMS_FUNCDATA_AXIS3	0x004
#define	MMS_FUNCDATA_AXIS4	0x008
#define	MMS_FUNCDATA_AXIS5	0x010
#define	MMS_FUNCDATA_AXIS6	0x020
#define	MMS_FUNCDATA_AXIS7	0x040
#define	MMS_FUNCDATA_AXIS8	0x080
#define	MMS_FUNCDATA_C		0x100
#define	MMS_FUNCDATA_B		0x200
#define	MMS_FUNCDATA_A		0x400
#define	MMS_FUNCDATA_START	0x800

struct mms_softc {
	device_t sc_dev;

	device_t sc_parent;
	struct maple_unit *sc_unit;

	uint32_t sc_oldbuttons;

	device_t sc_wsmousedev;
};

static int	mms_match(device_t, cfdata_t, void *);
static void	mms_attach(device_t, device_t, void *);
static int	mms_detach(device_t, int);

CFATTACH_DECL_NEW(mms, sizeof(struct mms_softc),
    mms_match, mms_attach, mms_detach, NULL);

static int	mms_enable(void *);
static int	mms_ioctl(void *, u_long, void *, int, struct lwp *);
static void	mms_disable(void *);

const struct wsmouse_accessops mms_accessops = {
	mms_enable,
	mms_ioctl,
	mms_disable,
};

static void	mms_intr(void *, struct maple_response *, int, int);

static int
mms_match(device_t parent, cfdata_t cf, void *aux)
{
	struct maple_attach_args *ma = aux;

	return ma->ma_function == MAPLE_FN_MOUSE ? MAPLE_MATCH_FUNC : 0;
}

static void
mms_attach(device_t parent, device_t self, void *aux)
{
	struct mms_softc *sc = device_private(self);
	struct maple_attach_args *ma = aux;
	struct wsmousedev_attach_args a;
	uint32_t data;

	printf(": SEGA Dreamcast Mouse\n");

	sc->sc_dev = self;
	sc->sc_parent = parent;
	sc->sc_unit = ma->ma_unit;

	data = maple_get_function_data(ma->ma_devinfo,
	    MAPLE_FN_MOUSE);

	printf("%s: buttons:", device_xname(self));
	if (data & MMS_FUNCDATA_A)
		printf(" left");
	if (data & MMS_FUNCDATA_C)
		printf(" middle");
	if (data & MMS_FUNCDATA_B)
		printf(" right");
	if (data & MMS_FUNCDATA_START)
		printf(" thumb");
	printf("\n");

	sc->sc_oldbuttons = 0;

	a.accessops = &mms_accessops;
	a.accesscookie = sc;

	/*
	 * Attach the mouse, saving a handle to it.
	 */
	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint, CFARGS_NONE);
	if (sc->sc_wsmousedev == NULL) {
		/* Nothing more to do here. */
		return;
	}

	maple_set_callback(parent, sc->sc_unit, MAPLE_FN_MOUSE, mms_intr, sc);
}

static int
mms_detach(device_t self, int flags)
{
	struct mms_softc *sc = device_private(self);
	int rv = 0;

	if (sc->sc_wsmousedev != NULL)
		rv = config_detach(sc->sc_wsmousedev, flags);

	return rv;
}

static int
mms_enable(void *v)
{
	struct mms_softc *sc = v;

	maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 1);
	return 0;
}

static void
mms_disable(void *v)
{
	struct mms_softc *sc = v;

	maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 0);
}

static int
mms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
{

	switch (cmd) {
	case WSMOUSEIO_GTYPE:
		*(u_int *) data = WSMOUSE_TYPE_MAPLE;
		break;

	case WSMOUSEIO_SRES:
		/* XXX */
		return EOPNOTSUPP;

	default:
		return EPASSTHROUGH;
	}

	return 0;
}

static void
mms_intr(void *arg, struct maple_response *response, int size, int flags)
{
	struct mms_softc *sc = arg;
	struct mms_condition *data = (void *) response->data;
	int dx = 0, dy = 0, dz = 0, buttons = 0;
	uint32_t buttonchg;

	if ((flags & MAPLE_FLAG_PERIODIC) == 0 ||
	    size < sizeof(*data))
		return;

	data->buttons &= MMS_BUTTON_MASK;
	buttonchg = sc->sc_oldbuttons ^ data->buttons;
	sc->sc_oldbuttons = data->buttons;

	dx = (data->axis1 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
	dy = (data->axis2 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
	dz = (data->axis3 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;

	if (dx || dy || dz || buttonchg) {
		if ((data->buttons & MMS_BUTTON_A) == 0)
			buttons |= 0x01;
		if ((data->buttons & MMS_BUTTON_C) == 0)
			buttons |= 0x02;
		if ((data->buttons & MMS_BUTTON_B) == 0)
			buttons |= 0x04;
		if ((data->buttons & MMS_BUTTON_START) == 0)
			buttons |= 0x08;

		wsmouse_input(sc->sc_wsmousedev,
				buttons,
				dx, -dy, dz, 0,
				WSMOUSE_INPUT_DELTA);
	}
}