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