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