No empty .Rs/.Re
[netbsd-mini2440.git] / sys / arch / dreamcast / dev / maple / mms.c
blob5f43470083f7f44675f784d94cf8841f03f0ff82
1 /* $NetBSD: mms.c,v 1.13 2007/03/04 05:59:44 christos Exp $ */
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
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.
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.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: mms.c,v 1.13 2007/03/04 05:59:44 christos Exp $");
35 #include <sys/param.h>
36 #include <sys/device.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
40 #include "wsmouse.h"
42 #include <dev/wscons/wsconsio.h>
43 #include <dev/wscons/wsmousevar.h>
45 #include <dreamcast/dev/maple/maple.h>
46 #include <dreamcast/dev/maple/mapleconf.h>
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;
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
67 #define MMS_MOVEMENT_BASE 0x200
68 #define MMS_MOVEMENT_MAX 0x3ff
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
83 struct mms_softc {
84 struct device sc_dev;
86 struct device *sc_parent;
87 struct maple_unit *sc_unit;
89 uint32_t sc_oldbuttons;
91 struct device *sc_wsmousedev;
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);
98 CFATTACH_DECL(mms, sizeof(struct mms_softc),
99 mms_match, mms_attach, mms_detach, NULL);
101 int mms_enable(void *);
102 int mms_ioctl(void *, u_long, void *, int, struct lwp *);
103 void mms_disable(void *);
105 const struct wsmouse_accessops mms_accessops = {
106 mms_enable,
107 mms_ioctl,
108 mms_disable,
111 void mms_intr(void *, struct maple_response *, int, int);
114 mms_match(struct device *parent, struct cfdata *cf, void *aux)
116 struct maple_attach_args *ma = aux;
118 return ma->ma_function == MAPLE_FN_MOUSE ? MAPLE_MATCH_FUNC : 0;
121 void
122 mms_attach(struct device *parent, struct device *self, void *aux)
124 struct mms_softc *sc = (void *) self;
125 struct maple_attach_args *ma = aux;
126 struct wsmousedev_attach_args a;
127 uint32_t data;
129 printf(": SEGA Dreamcast Mouse\n");
131 sc->sc_parent = parent;
132 sc->sc_unit = ma->ma_unit;
134 data = maple_get_function_data(ma->ma_devinfo,
135 MAPLE_FN_MOUSE);
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");
148 sc->sc_oldbuttons = 0;
150 a.accessops = &mms_accessops;
151 a.accesscookie = sc;
154 * Attach the mouse, saving a handle to it.
156 sc->sc_wsmousedev = config_found(self, &a, wsmousedevprint);
157 if (sc->sc_wsmousedev == NULL) {
158 /* Nothing more to do here. */
159 return;
162 maple_set_callback(parent, sc->sc_unit, MAPLE_FN_MOUSE, mms_intr, sc);
166 mms_detach(struct device *self, int flags)
168 struct mms_softc *sc = (void *) self;
169 int rv = 0;
171 if (sc->sc_wsmousedev != NULL)
172 rv = config_detach(sc->sc_wsmousedev, flags);
174 return rv;
178 mms_enable(void *v)
180 struct mms_softc *sc = v;
182 maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 1);
183 return 0;
186 void
187 mms_disable(void *v)
189 struct mms_softc *sc = v;
191 maple_enable_periodic(sc->sc_parent, sc->sc_unit, MAPLE_FN_MOUSE, 0);
195 mms_ioctl(void *v, u_long cmd, void *data, int flag, struct lwp *l)
198 switch (cmd) {
199 case WSMOUSEIO_GTYPE:
200 *(u_int *) data = WSMOUSE_TYPE_MAPLE;
201 break;
203 case WSMOUSEIO_SRES:
204 /* XXX */
205 return EOPNOTSUPP;
207 default:
208 return EPASSTHROUGH;
211 return 0;
214 void
215 mms_intr(void *arg, struct maple_response *response, int size, int flags)
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;
222 if ((flags & MAPLE_FLAG_PERIODIC) == 0 ||
223 size < sizeof(*data))
224 return;
226 data->buttons &= MMS_BUTTON_MASK;
227 buttonchg = sc->sc_oldbuttons ^ data->buttons;
228 sc->sc_oldbuttons = data->buttons;
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;
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;
244 wsmouse_input(sc->sc_wsmousedev,
245 buttons,
246 dx, -dy, dz, 0,
247 WSMOUSE_INPUT_DELTA);