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