1/*	$NetBSD$	*/
2
3/*-
4 * Copyright (c) 1996, 1998, 2008 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 *
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.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD$");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/device.h>
39
40#include <sys/bus.h>
41
42#include <dev/ofw/openfirm.h>
43#include <dev/isa/isavar.h>
44#include <dev/ofisa/ofisavar.h>
45
46#include <dev/ic/joyvar.h>
47
48#define	JOY_NPORTS	1	/* XXX should be in a header file */
49
50struct joy_ofisa_softc {
51	struct joy_softc sc_joy;
52	kmutex_t sc_lock;
53};
54
55static int	joy_ofisa_match(device_t, cfdata_t, void *);
56static void	joy_ofisa_attach(device_t, device_t, void *);
57
58CFATTACH_DECL_NEW(joy_ofisa, sizeof(struct joy_ofisa_softc),
59    joy_ofisa_match, joy_ofisa_attach, NULL, NULL);
60
61static int
62joy_ofisa_match(device_t parent, cfdata_t match, void *aux)
63{
64	struct ofisa_attach_args *aa = aux;
65	static const char *const compatible_strings[] = {
66		"pnpPNP,b02f",			/* generic joystick */
67		NULL,
68	};
69	int rv = 0;
70
71	if (of_compatible(aa->oba.oba_phandle, compatible_strings) != -1)
72		rv = 1;
73	return rv;
74}
75
76static void
77joy_ofisa_attach(device_t parent, device_t self, void *aux)
78{
79	struct joy_ofisa_softc *osc = device_private(self);
80	struct joy_softc *sc = &osc->sc_joy;
81	struct ofisa_attach_args *aa = aux;
82	struct ofisa_reg_desc reg;
83	char *model = NULL;
84	int n;
85
86	/*
87	 * We're living on an OFW.  We have to ask the OFW what our
88	 * register property looks like.
89	 *
90	 * We expect:
91	 *
92	 *	1 i/o register region
93	 */
94
95	n = ofisa_reg_get(aa->oba.oba_phandle, &reg, 1);
96	if (n != 1) {
97		aprint_error(": error getting register data\n");
98		return;
99	}
100	if (reg.type != OFISA_REG_TYPE_IO) {
101		aprint_error(": register type not i/o\n");
102		return;
103	}
104	if (reg.len != JOY_NPORTS) {
105		aprint_error(": weird register size (%lu, expected %d)\n",
106		    (unsigned long)reg.len, JOY_NPORTS);
107		return;
108	}
109
110	sc->sc_iot = aa->iot;
111	sc->sc_dev = self;
112
113	if (bus_space_map(sc->sc_iot, reg.addr, reg.len, 0, &sc->sc_ioh)) {
114		aprint_error(": unable to map register space\n");
115		return;
116	}
117
118	n = OF_getproplen(aa->oba.oba_phandle, "model");
119	if (n > 0) {
120		model = alloca(n);
121		if (OF_getprop(aa->oba.oba_phandle, "model", model, n) != n)
122			model = NULL;	/* safe; alloca */
123	}
124	if (model != NULL)
125		aprint_normal(": %s", model);
126	aprint_normal("\n");
127
128	mutex_init(&osc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
129	sc->sc_lock = &osc->sc_lock;
130
131	joyattach(sc);
132}
133