mms.c revision 1.5 1 /* $NetBSD: mms.c,v 1.5 2002/10/02 15:45:17 thorpej 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 CFATTACH_DECL(mms, sizeof(struct mms_softc),
101 mms_match, mms_attach, NULL, NULL);
102
103 int mms_enable(void *);
104 int mms_ioctl(void *, u_long, caddr_t, int, struct proc *);
105 void mms_disable(void *);
106
107 const struct wsmouse_accessops mms_accessops = {
108 mms_enable,
109 mms_ioctl,
110 mms_disable,
111 };
112
113 void mms_intr(void *, void *, int);
114
115 int
116 mms_match(struct device *parent, struct cfdata *cf, void *aux)
117 {
118 struct maple_attach_args *ma = aux;
119
120 return ((ma->ma_function & MAPLE_FUNC_MOUSE) != 0);
121 }
122
123 void
124 mms_attach(struct device *parent, struct device *self, void *aux)
125 {
126 struct mms_softc *sc = (void *) self;
127 struct maple_attach_args *ma = aux;
128 struct wsmousedev_attach_args a;
129 uint32_t data;
130
131 printf(": SEGA Dreamcast Mouse\n");
132
133 sc->sc_port = ma->ma_port;
134 sc->sc_subunit = ma->ma_subunit;
135
136 data = maple_get_function_data(ma->ma_devinfo,
137 MAPLE_FUNC_MOUSE);
138
139 printf("%s: buttons:", sc->sc_dev.dv_xname);
140 if (data & MMS_FUNCDATA_A)
141 printf(" left");
142 if (data & MMS_FUNCDATA_C)
143 printf(" middle");
144 if (data & MMS_FUNCDATA_B)
145 printf(" right");
146 if (data & MMS_FUNCDATA_START)
147 printf(" thumb");
148 printf("\n");
149
150 sc->sc_oldbuttons = 0;
151
152 a.accessops = &mms_accessops;
153 a.accesscookie = sc;
154
155 /*
156 * Attach the mouse, saving a handle to it.
157 */
158 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
159 if (sc->sc_wsmousedev == NULL) {
160 /* Nothing more to do here. */
161 return;
162 }
163
164 maple_set_condition_callback(parent, sc->sc_port, sc->sc_subunit,
165 MAPLE_FUNC_MOUSE, mms_intr, sc);
166 }
167
168 int
169 mms_enable(void *v)
170 {
171
172 return (0);
173 }
174
175 void
176 mms_disable(void *v)
177 {
178
179 /* Nothing to do here. */
180 }
181
182 int
183 mms_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
184 {
185
186 switch (cmd) {
187 case WSMOUSEIO_GTYPE:
188 *(u_int *) data = WSMOUSE_TYPE_USB; /* XXX */
189 break;
190
191 case WSMOUSEIO_SRES:
192 /* XXX */
193 return (EOPNOTSUPP);
194
195 default:
196 return (EPASSTHROUGH);
197 }
198
199 return (0);
200 }
201
202 void
203 mms_intr(void *arg, void *buf, int size)
204 {
205 struct mms_softc *sc = arg;
206 struct mms_condition *data = buf;
207 int dx = 0, dy = 0, dz = 0, buttons = 0;
208 uint32_t buttonchg;
209
210 if (size < sizeof(*data))
211 return;
212
213 data->buttons &= MMS_BUTTON_MASK;
214 buttonchg = sc->sc_oldbuttons ^ data->buttons;
215 sc->sc_oldbuttons = data->buttons;
216
217 dx = (data->axis1 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
218 dy = (data->axis2 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
219 dz = (data->axis3 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
220
221 if (dx || dy || dz || buttonchg) {
222 if ((data->buttons & MMS_BUTTON_A) == 0)
223 buttons |= 0x01;
224 if ((data->buttons & MMS_BUTTON_C) == 0)
225 buttons |= 0x02;
226 if ((data->buttons & MMS_BUTTON_B) == 0)
227 buttons |= 0x04;
228 if ((data->buttons & MMS_BUTTON_START) == 0)
229 buttons |= 0x08;
230
231 wsmouse_input(sc->sc_wsmousedev, buttons,
232 dx, dy, dz, WSMOUSE_INPUT_DELTA);
233 }
234 }
235