xref: /netbsd/sys/arch/dreamcast/dev/maple/mms.c (revision bf9ec67e)
1 /*	$NetBSD: mms.c,v 1.2 2002/03/17 19:40:37 atatat Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/device.h>
41 #include <sys/proc.h>
42 #include <sys/systm.h>
43 
44 #include "wsmouse.h"
45 
46 #include <dev/wscons/wsconsio.h>
47 #include <dev/wscons/wsmousevar.h>
48 
49 #include <dreamcast/dev/maple/maple.h>
50 #include <dreamcast/dev/maple/mapleconf.h>
51 
52 struct mms_condition {
53 	uint32_t buttons;
54 	uint16_t axis1;		/* X */
55 	uint16_t axis2;		/* Y */
56 	uint16_t axis3;		/* wheel */
57 	uint16_t axis4;
58 	uint16_t axis5;
59 	uint16_t axis6;
60 	uint16_t axis7;
61 	uint16_t axis8;
62 };
63 
64 #define	MMS_BUTTON_C		0x01	/* middle */
65 #define	MMS_BUTTON_B		0x02	/* right */
66 #define	MMS_BUTTON_A		0x04	/* left */
67 #define	MMS_BUTTON_START	0x08	/* thumb */
68 #define	MMS_BUTTON_MASK		0x0f
69 
70 #define	MMS_MOVEMENT_BASE	0x200
71 #define	MMS_MOVEMENT_MAX	0x3ff
72 
73 #define	MMS_FUNCDATA_AXIS1	0x001
74 #define	MMS_FUNCDATA_AXIS2	0x002
75 #define	MMS_FUNCDATA_AXIS3	0x004
76 #define	MMS_FUNCDATA_AXIS4	0x008
77 #define	MMS_FUNCDATA_AXIS5	0x010
78 #define	MMS_FUNCDATA_AXIS6	0x020
79 #define	MMS_FUNCDATA_AXIS7	0x040
80 #define	MMS_FUNCDATA_AXIS8	0x080
81 #define	MMS_FUNCDATA_C		0x100
82 #define	MMS_FUNCDATA_B		0x200
83 #define	MMS_FUNCDATA_A		0x400
84 #define	MMS_FUNCDATA_START	0x800
85 
86 struct mms_softc {
87 	struct device sc_dev;
88 
89 	int sc_port;
90 	int sc_subunit;
91 
92 	uint32_t sc_oldbuttons;
93 
94 	struct device *sc_wsmousedev;
95 };
96 
97 int	mms_match(struct device *, struct cfdata *, void *);
98 void	mms_attach(struct device *, struct device *, void *);
99 
100 struct cfattach mms_ca = {
101 	sizeof(struct mms_softc), mms_match, mms_attach,
102 };
103 
104 int	mms_enable(void *);
105 int	mms_ioctl(void *, u_long, caddr_t, int, struct proc *);
106 void	mms_disable(void *);
107 
108 const struct wsmouse_accessops mms_accessops = {
109 	mms_enable,
110 	mms_ioctl,
111 	mms_disable,
112 };
113 
114 void	mms_intr(void *, void *, int);
115 
116 int
117 mms_match(struct device *parent, struct cfdata *cf, void *aux)
118 {
119 	struct maple_attach_args *ma = aux;
120 
121 	return ((ma->ma_function & MAPLE_FUNC_MOUSE) != 0);
122 }
123 
124 void
125 mms_attach(struct device *parent, struct device *self, void *aux)
126 {
127 	struct mms_softc *sc = (void *) self;
128 	struct maple_attach_args *ma = aux;
129 	struct wsmousedev_attach_args a;
130 	uint32_t data;
131 
132 	printf(": SEGA Dreamcast Mouse\n");
133 
134 	sc->sc_port = ma->ma_port;
135 	sc->sc_subunit = ma->ma_subunit;
136 
137 	data = maple_get_function_data(ma->ma_devinfo,
138 	    MAPLE_FUNC_MOUSE);
139 
140 	printf("%s: buttons:", sc->sc_dev.dv_xname);
141 	if (data & MMS_FUNCDATA_A)
142 		printf(" left");
143 	if (data & MMS_FUNCDATA_C)
144 		printf(" middle");
145 	if (data & MMS_FUNCDATA_B)
146 		printf(" right");
147 	if (data & MMS_FUNCDATA_START)
148 		printf(" thumb");
149 	printf("\n");
150 
151 	sc->sc_oldbuttons = 0;
152 
153 	a.accessops = &mms_accessops;
154 	a.accesscookie = sc;
155 
156 	/*
157 	 * Attach the mouse, saving a handle to it.
158 	 */
159 	sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
160 	if (sc->sc_wsmousedev == NULL) {
161 		/* Nothing more to do here. */
162 		return;
163 	}
164 
165 	maple_set_condition_callback(parent, sc->sc_port, sc->sc_subunit,
166 	    MAPLE_FUNC_MOUSE, mms_intr, sc);
167 }
168 
169 int
170 mms_enable(void *v)
171 {
172 
173 	return (0);
174 }
175 
176 void
177 mms_disable(void *v)
178 {
179 
180 	/* Nothing to do here. */
181 }
182 
183 int
184 mms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
185 {
186 
187 	switch (cmd) {
188 	case WSMOUSEIO_GTYPE:
189 		*(u_int *) data = WSMOUSE_TYPE_USB;	/* XXX */
190 		break;
191 
192 	case WSMOUSEIO_SRES:
193 		/* XXX */
194 		return (EOPNOTSUPP);
195 
196 	default:
197 		return (EPASSTHROUGH);
198 	}
199 
200 	return (0);
201 }
202 
203 void
204 mms_intr(void *arg, void *buf, int size)
205 {
206 	struct mms_softc *sc = arg;
207 	struct mms_condition *data = buf;
208 	int dx = 0, dy = 0, dz = 0, buttons = 0;
209 	uint32_t buttonchg;
210 
211 	if (size < sizeof(*data))
212 		return;
213 
214 	data->buttons &= MMS_BUTTON_MASK;
215 	buttonchg = sc->sc_oldbuttons ^ data->buttons;
216 	sc->sc_oldbuttons = data->buttons;
217 
218 	dx = (data->axis1 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
219 	dy = (data->axis2 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
220 	dz = (data->axis3 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
221 
222 	if (dx || dy || dz || buttonchg) {
223 		if ((data->buttons & MMS_BUTTON_A) == 0)
224 			buttons |= 0x01;
225 		if ((data->buttons & MMS_BUTTON_C) == 0)
226 			buttons |= 0x02;
227 		if ((data->buttons & MMS_BUTTON_B) == 0)
228 			buttons |= 0x04;
229 		if ((data->buttons & MMS_BUTTON_START) == 0)
230 			buttons |= 0x08;
231 
232 		wsmouse_input(sc->sc_wsmousedev, buttons,
233 		    dx, dy, dz, WSMOUSE_INPUT_DELTA);
234 	}
235 }
236