1 /* $NetBSD: mms.c,v 1.13 2007/03/04 05:59:44 christos Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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>
38 #include <sys/systm.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) */
51 uint16_t axis1
; /* X */
52 uint16_t axis2
; /* Y */
53 uint16_t axis3
; /* wheel */
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
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
= {
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;
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
;
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
,
137 printf("%s: buttons:", sc
->sc_dev
.dv_xname
);
138 if (data
& MMS_FUNCDATA_A
)
140 if (data
& MMS_FUNCDATA_C
)
142 if (data
& MMS_FUNCDATA_B
)
144 if (data
& MMS_FUNCDATA_START
)
148 sc
->sc_oldbuttons
= 0;
150 a
.accessops
= &mms_accessops
;
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. */
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
;
171 if (sc
->sc_wsmousedev
!= NULL
)
172 rv
= config_detach(sc
->sc_wsmousedev
, flags
);
180 struct mms_softc
*sc
= v
;
182 maple_enable_periodic(sc
->sc_parent
, sc
->sc_unit
, MAPLE_FN_MOUSE
, 1);
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
)
199 case WSMOUSEIO_GTYPE
:
200 *(u_int
*) data
= WSMOUSE_TYPE_MAPLE
;
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;
222 if ((flags
& MAPLE_FLAG_PERIODIC
) == 0 ||
223 size
< sizeof(*data
))
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)
237 if ((data
->buttons
& MMS_BUTTON_C
) == 0)
239 if ((data
->buttons
& MMS_BUTTON_B
) == 0)
241 if ((data
->buttons
& MMS_BUTTON_START
) == 0)
244 wsmouse_input(sc
->sc_wsmousedev
,
247 WSMOUSE_INPUT_DELTA
);