1 /*-
2 * Copyright (c) 2004 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
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 AUTHORS ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: releng/8.4/sys/dev/watchdog/watchdog.c 221939 2011-05-15 01:08:51Z attilio $");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/conf.h>
34 #include <sys/uio.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/watchdog.h>
39 #include <sys/bus.h>
40 #include <machine/bus.h>
41
42 static struct cdev *wd_dev;
43 static volatile u_int wd_last_u;
44
45 static int
46 kern_do_pat(u_int utim)
47 {
48 int error;
49
50 if ((utim & WD_LASTVAL) != 0 && (utim & WD_INTERVAL) > 0)
51 return (EINVAL);
52
53 if ((utim & WD_LASTVAL) != 0) {
54 MPASS((wd_last_u & ~WD_INTERVAL) == 0);
55 utim &= ~WD_LASTVAL;
56 utim |= wd_last_u;
57 } else
58 wd_last_u = (utim & WD_INTERVAL);
59 if ((utim & WD_INTERVAL) == WD_TO_NEVER) {
60 utim = 0;
61
62 /* Assume all is well; watchdog signals failure. */
63 error = 0;
64 } else {
65 /* Assume no watchdog available; watchdog flags success */
66 error = EOPNOTSUPP;
67 }
68 EVENTHANDLER_INVOKE(watchdog_list, utim, &error);
69 return (error);
70 }
71
72 static int
73 wd_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
74 int flags __unused, struct thread *td)
75 {
76 u_int u;
77
78 if (cmd != WDIOCPATPAT)
79 return (ENOIOCTL);
80 u = *(u_int *)data;
81 if (u & ~(WD_ACTIVE | WD_PASSIVE | WD_LASTVAL | WD_INTERVAL))
82 return (EINVAL);
83 if ((u & (WD_ACTIVE | WD_PASSIVE)) == (WD_ACTIVE | WD_PASSIVE))
84 return (EINVAL);
85 if ((u & (WD_ACTIVE | WD_PASSIVE)) == 0 && ((u & WD_INTERVAL) > 0 ||
86 (u & WD_LASTVAL) != 0))
87 return (EINVAL);
88 if (u & WD_PASSIVE)
89 return (ENOSYS); /* XXX Not implemented yet */
90 u &= ~(WD_ACTIVE | WD_PASSIVE);
91
92 return (kern_do_pat(u));
93 }
94
95 u_int
96 wdog_kern_last_timeout(void)
97 {
98
99 return (wd_last_u);
100 }
101
102 int
103 wdog_kern_pat(u_int utim)
104 {
105
106 if (utim & ~(WD_LASTVAL | WD_INTERVAL))
107 return (EINVAL);
108
109 return (kern_do_pat(utim));
110 }
111
112 static struct cdevsw wd_cdevsw = {
113 .d_version = D_VERSION,
114 .d_ioctl = wd_ioctl,
115 .d_name = "watchdog",
116 };
117
118 static int
119 watchdog_modevent(module_t mod __unused, int type, void *data __unused)
120 {
121 switch(type) {
122 case MOD_LOAD:
123 wd_dev = make_dev(&wd_cdevsw, 0,
124 UID_ROOT, GID_WHEEL, 0600, _PATH_WATCHDOG);
125 return 0;
126 case MOD_UNLOAD:
127 destroy_dev(wd_dev);
128 return 0;
129 case MOD_SHUTDOWN:
130 return 0;
131 default:
132 return EOPNOTSUPP;
133 }
134 }
135
136 DEV_MODULE(watchdog, watchdog_modevent, NULL);
Cache object: 6e3f3e5062d5a95825126361a388883a
|