1/*-
2 * Copyright (c) 2000 KIYOHARA Takashi <kiyohara@kk.iij4u.ne.jp>
3 * Copyright (c) 2000 Takanori Watanabe <takawata@jp.FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD$
28 */
29
30#include <sys/param.h>
31#include <sys/systm.h>
32#include <sys/bus.h>
33#include <sys/eventhandler.h>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#include <sys/reboot.h>
37
38#include <machine/bus.h>
39#include <machine/resource.h>
40#include <sys/rman.h>
41
42#include <pc98/pc98/canbusvars.h>
43#include "canbus_if.h"
44
45
46/* canbepm softc */
47struct canbepm_softc {
48	device_t canbepm_dev;			/* canbepm device */
49
50	eventhandler_tag canbepm_tag;		/* event handler tag */
51};
52
53
54static void	canbepm_soft_off (void *, int);
55static void	canbepm_identify (driver_t *, device_t);
56static int	canbepm_probe (device_t);
57static int	canbepm_attach (device_t);
58static int	canbepm_detach (device_t);
59
60
61static device_method_t canbepm_methods[] = {
62	DEVMETHOD(device_identify,	canbepm_identify),
63	DEVMETHOD(device_probe,		canbepm_probe),
64	DEVMETHOD(device_attach,	canbepm_attach),
65	DEVMETHOD(device_detach,	canbepm_detach),
66	{0, 0}
67};
68
69static driver_t canbepm_driver = {
70	"canbepm",
71	canbepm_methods,
72	sizeof(struct canbepm_softc),
73};
74
75devclass_t canbepm_devclass;
76DRIVER_MODULE(canbepm, canbus, canbepm_driver, canbepm_devclass, 0, 0);
77MODULE_DEPEND(canbepm, canbus, 1, 1, 1);
78
79
80static void
81canbepm_soft_off (void *data, int howto)
82{
83	struct canbepm_softc *sc = data;
84	u_int8_t poweroff_data[] = CANBE_POWEROFF_DATA;
85
86	if (!(howto & RB_POWEROFF))
87		return;
88
89	CANBUS_WRITE_MULTI(device_get_parent(sc->canbepm_dev), sc->canbepm_dev,
90	    CANBE_POWER_CTRL, sizeof (poweroff_data), poweroff_data);
91}
92
93
94static void
95canbepm_identify(driver_t *drv, device_t parent)
96{
97	if (device_find_child(parent, "canbepm", 0) == NULL) {
98		if (BUS_ADD_CHILD(parent, 33, "canbepm", 0) == NULL)
99			device_printf(parent, "canbepm cannot attach\n");
100	}
101}
102
103
104static int
105canbepm_probe(device_t dev)
106{
107	device_set_desc(dev, "CanBe Power Management Controller");
108
109	return (0);
110}
111
112static int
113canbepm_attach(device_t dev)
114{
115	struct canbepm_softc *sc = device_get_softc(dev);
116
117	/* eventhandler regist */
118	sc->canbepm_tag = EVENTHANDLER_REGISTER(
119	    shutdown_final, canbepm_soft_off, sc, SHUTDOWN_PRI_LAST);
120
121	sc->canbepm_dev = dev;
122
123	return (0);
124}
125
126
127static int
128canbepm_detach(device_t dev)
129{
130	struct canbepm_softc *sc = device_get_softc(dev);
131
132	/* eventhandler deregist */
133	EVENTHANDLER_DEREGISTER(shutdown_final, sc->canbepm_tag);
134	BUS_CHILD_DETACHED(device_get_parent(dev), dev);
135
136	return (0);
137}
138