mms.c revision 1.13.40.1 1 /* $NetBSD: mms.c,v 1.13.40.1 2008/06/02 13:22:00 mjf 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mms.c,v 1.13.40.1 2008/06/02 13:22:00 mjf Exp $");
34
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39
40 #include "wsmouse.h"
41
42 #include <dev/wscons/wsconsio.h>
43 #include <dev/wscons/wsmousevar.h>
44
45 #include <dreamcast/dev/maple/maple.h>
46 #include <dreamcast/dev/maple/mapleconf.h>
47
48 struct mms_condition {
49 uint32_t func_code; /* function code (big endian) */
50 uint32_t buttons;
51 uint16_t axis1; /* X */
52 uint16_t axis2; /* Y */
53 uint16_t axis3; /* wheel */
54 uint16_t axis4;
55 uint16_t axis5;
56 uint16_t axis6;
57 uint16_t axis7;
58 uint16_t axis8;
59 };
60
61 #define MMS_BUTTON_C 0x01 /* middle */
62 #define MMS_BUTTON_B 0x02 /* right */
63 #define MMS_BUTTON_A 0x04 /* left */
64 #define MMS_BUTTON_START 0x08 /* thumb */
65 #define MMS_BUTTON_MASK 0x0f
66
67 #define MMS_MOVEMENT_BASE 0x200
68 #define MMS_MOVEMENT_MAX 0x3ff
69
70 #define MMS_FUNCDATA_AXIS1 0x001
71 #define MMS_FUNCDATA_AXIS2 0x002
72 #define MMS_FUNCDATA_AXIS3 0x004
73 #define MMS_FUNCDATA_AXIS4 0x008
74 #define MMS_FUNCDATA_AXIS5 0x010
75 #define MMS_FUNCDATA_AXIS6 0x020
76 #define MMS_FUNCDATA_AXIS7 0x040
77 #define MMS_FUNCDATA_AXIS8 0x080
78 #define MMS_FUNCDATA_C 0x100
79 #define MMS_FUNCDATA_B 0x200
80 #define MMS_FUNCDATA_A 0x400
81 #define MMS_FUNCDATA_START 0x800
82
83 struct mms_softc {
84 struct device sc_dev;
85
86 struct device *sc_parent;
87 struct maple_unit *sc_unit;
88
89 uint32_t sc_oldbuttons;
90
91 struct device *sc_wsmousedev;
92 };
93
94 int mms_match(struct device *, struct cfdata *, void *);
95 void mms_attach(struct device *, struct device *, void *);
96 int mms_detach(struct device *, int);
97
98 CFATTACH_DECL(mms, sizeof(struct mms_softc),
99 mms_match, mms_attach, mms_detach, NULL);
100
101 int mms_enable(void *);
102 int mms_ioctl(void *, u_long, void *, int, struct lwp *);
103 void mms_disable(void *);
104
105 const struct wsmouse_accessops mms_accessops = {
106 mms_enable,
107 mms_ioctl,
108 mms_disable,
109 };
110
111 void mms_intr(void *, struct maple_response *, int, int);
112
113 int
114 mms_match(struct device *parent, struct cfdata *cf, void *aux)
115 {
116 struct maple_attach_args *ma = aux;
117
118 return ma->ma_function == MAPLE_FN_MOUSE ? MAPLE_MATCH_FUNC : 0;
119 }
120
121 void
122 mms_attach(struct device *parent, struct device *self, void *aux)
123 {
124 struct mms_softc *sc = (void *) self;
125 struct maple_attach_args *ma = aux;
126 struct wsmousedev_attach_args a;
127 uint32_t data;
128
129 printf(": SEGA Dreamcast Mouse\n");
130
131 sc->sc_parent = parent;
132 sc->sc_unit = ma->ma_unit;
133
134 data = maple_get_function_data(ma->ma_devinfo,
135 MAPLE_FN_MOUSE);
136
137 printf("%s: buttons:", sc->sc_dev.dv_xname);
138 if (data & MMS_FUNCDATA_A)
139 printf(" left");
140 if (data & MMS_FUNCDATA_C)
141 printf(" middle");
142 if (data & MMS_FUNCDATA_B)
143 printf(" right");
144 if (data & MMS_FUNCDATA_START)
145 printf(" thumb");
146 printf("\n");
147
148 sc->sc_oldbuttons = 0;
149
150 a.accessops = &mms_accessops;
151 a.accesscookie = sc;
152
153 /*
154 * Attach the mouse, saving a handle to it.
155 */
156 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
157 if (sc->sc_wsmousedev == NULL) {
158 /* Nothing more to do here. */
159 return;
160 }
161
162 maple_set_callback(parent, sc->sc_unit, MAPLE_FN_MOUSE, mms_intr, sc);
163 }
164
165 int
166 mms_detach(struct device *self, int flags)
167 {
168 struct mms_softc *sc = (void *) self;
169 int rv = 0;
170
171 if (sc->sc_wsmousedev != NULL)
172 rv = config_detach(sc->sc_wsmousedev, flags);
173
174 return rv;
175 }
176
177 int
178 mms_enable(void *v)
179 {
180 struct mms_softc *sc = v;
181
182 maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 1);
183 return 0;
184 }
185
186 void
187 mms_disable(void *v)
188 {
189 struct mms_softc *sc = v;
190
191 maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 0);
192 }
193
194 int
195 mms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
196 {
197
198 switch (cmd) {
199 case WSMOUSEIO_GTYPE:
200 *(u_int *) data = WSMOUSE_TYPE_MAPLE;
201 break;
202
203 case WSMOUSEIO_SRES:
204 /* XXX */
205 return EOPNOTSUPP;
206
207 default:
208 return EPASSTHROUGH;
209 }
210
211 return 0;
212 }
213
214 void
215 mms_intr(void *arg, struct maple_response *response, int size, int flags)
216 {
217 struct mms_softc *sc = arg;
218 struct mms_condition *data = (void *) response->data;
219 int dx = 0, dy = 0, dz = 0, buttons = 0;
220 uint32_t buttonchg;
221
222 if ((flags & MAPLE_FLAG_PERIODIC) == 0 ||
223 size < sizeof(*data))
224 return;
225
226 data->buttons &= MMS_BUTTON_MASK;
227 buttonchg = sc->sc_oldbuttons ^ data->buttons;
228 sc->sc_oldbuttons = data->buttons;
229
230 dx = (data->axis1 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
231 dy = (data->axis2 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
232 dz = (data->axis3 & MMS_MOVEMENT_MAX) - MMS_MOVEMENT_BASE;
233
234 if (dx || dy || dz || buttonchg) {
235 if ((data->buttons & MMS_BUTTON_A) == 0)
236 buttons |= 0x01;
237 if ((data->buttons & MMS_BUTTON_C) == 0)
238 buttons |= 0x02;
239 if ((data->buttons & MMS_BUTTON_B) == 0)
240 buttons |= 0x04;
241 if ((data->buttons & MMS_BUTTON_START) == 0)
242 buttons |= 0x08;
243
244 wsmouse_input(sc->sc_wsmousedev,
245 buttons,
246 dx, -dy, dz, 0,
247 WSMOUSE_INPUT_DELTA);
248 }
249 }
250