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