Merge from vendor branch LESS:
[dragonfly.git] / sys / dev / powermng / pc98 / canbepm / canbepm.c
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: src/sys/pc98/pc98/canbepm.c,v 1.3.2.1 2003/02/10 13:11:51 nyan Exp $
28  * $DragonFly: src/sys/dev/powermng/pc98/canbepm/Attic/canbepm.c,v 1.3 2003/11/20 22:07:33 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/eventhandler.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/reboot.h>
38
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42
43 #include <pc98/pc98/canbusvars.h>
44 #include "canbus_if.h"
45
46
47 /* canbepm softc */
48 struct canbepm_softc {
49         device_t canbepm_dev;                   /* canbepm device */
50
51         eventhandler_tag canbepm_tag;           /* event handler tag */
52 };
53
54
55 static void     canbepm_soft_off (void *, int);
56 static void     canbepm_identify (driver_t *, device_t);
57 static int      canbepm_probe (device_t);
58 static int      canbepm_attach (device_t);
59 static int      canbepm_detach (device_t);
60
61
62 static device_method_t canbepm_methods[] = { 
63         DEVMETHOD(device_identify,      canbepm_identify),
64         DEVMETHOD(device_probe,         canbepm_probe),
65         DEVMETHOD(device_attach,        canbepm_attach),
66         DEVMETHOD(device_detach,        canbepm_detach),
67         {0, 0}
68 };
69
70 static driver_t canbepm_driver = {
71         "canbepm",
72         canbepm_methods,
73         sizeof(struct canbepm_softc),
74 };
75
76 devclass_t canbepm_devclass;
77
78 DECLARE_DUMMY_MODULE(canbepm);
79 DRIVER_MODULE(canbepm, canbus, canbepm_driver, canbepm_devclass, 0, 0);
80 MODULE_DEPEND(canbepm, canbus, 1, 1, 1);
81
82
83 static void
84 canbepm_soft_off (void *data, int howto)
85 {
86         struct canbepm_softc *sc = data;
87         u_int8_t poweroff_data[] = CANBE_POWEROFF_DATA;
88
89         if (!(howto & RB_POWEROFF))
90                 return;
91
92         CANBUS_WRITE_MULTI(device_get_parent(sc->canbepm_dev), sc->canbepm_dev,
93             CANBE_POWER_CTRL, sizeof (poweroff_data), poweroff_data);
94 }
95
96
97 static void
98 canbepm_identify(driver_t *drv, device_t parent)
99 {
100         if (device_find_child(parent, "canbepm", 0) == NULL) {
101                 if (BUS_ADD_CHILD(parent, 33, "canbepm", 0) == NULL)
102                         device_printf(parent, "canbepm cannot attach\n");
103         }
104 }
105
106
107 static int
108 canbepm_probe(device_t dev)
109 {
110         device_set_desc(dev, "CanBe Power Management Controller");
111
112         return (0);     
113 }
114
115 static int
116 canbepm_attach(device_t dev)
117 {
118         struct canbepm_softc *sc = device_get_softc(dev);
119
120         /* eventhandler regist */
121         sc->canbepm_tag = EVENTHANDLER_REGISTER(
122             shutdown_final, canbepm_soft_off, sc, SHUTDOWN_PRI_LAST);
123
124         sc->canbepm_dev = dev;
125
126         return (0);
127 }
128
129
130 static int
131 canbepm_detach(device_t dev)
132 {
133         struct canbepm_softc *sc = device_get_softc(dev);
134
135         /* eventhandler deregist */
136         EVENTHANDLER_DEREGISTER(shutdown_final, sc->canbepm_tag);
137         BUS_CHILD_DETACHED(device_get_parent(dev), dev);
138
139         return (0);
140 }