FreeBSD/Linux Kernel Cross Reference
sys/dev/ofw/ofrtc.c
1 /* $NetBSD: ofrtc.c,v 1.15 2002/10/23 09:13:30 jdolecek Exp $ */
2
3 /*
4 * Copyright (C) 1996 Wolfgang Solfrank.
5 * Copyright (C) 1996 TooLs GmbH.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by TooLs GmbH.
19 * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: ofrtc.c,v 1.15 2002/10/23 09:13:30 jdolecek Exp $");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/conf.h>
41 #include <sys/event.h>
42
43 #include <dev/ofw/openfirm.h>
44
45 struct ofrtc_softc {
46 struct device sc_dev;
47 int sc_phandle;
48 int sc_ihandle;
49 };
50
51 static int ofrtc_match __P((struct device *, struct cfdata *, void *));
52 static void ofrtc_attach __P((struct device *, struct device *, void *));
53
54 CFATTACH_DECL(ofrtc, sizeof(struct ofrtc_softc),
55 ofrtc_match, ofrtc_attach, NULL, NULL);
56
57 extern struct cfdriver ofrtc_cd;
58
59 dev_type_open(ofrtc_open);
60 dev_type_read(ofrtc_read);
61 dev_type_write(ofrtc_write);
62
63 const struct cdevsw ofrtc_cdevsw = {
64 ofrtc_open, nullclose, ofrtc_read, ofrtc_write, noioctl,
65 nostop, notty, nopoll, nommap, nokqfilter,
66 };
67
68 static int
69 ofrtc_match(struct device *parent, struct cfdata *match, void *aux)
70 {
71 struct ofbus_attach_args *oba = aux;
72 char type[8];
73 int l;
74
75 if (strcmp(oba->oba_busname, "ofw"))
76 return (0);
77 if ((l = OF_getprop(oba->oba_phandle, "device_type", type,
78 sizeof type - 1)) < 0 ||
79 l >= sizeof type)
80 return 0;
81
82 return !strcmp(type, "rtc");
83 }
84
85 static void
86 ofrtc_attach(struct device *parent, struct device *self, void *aux)
87 {
88 struct ofrtc_softc *of = (void *)self;
89 struct ofbus_attach_args *oba = aux;
90 char name[32];
91 int l;
92
93 of->sc_phandle = oba->oba_phandle;
94 of->sc_ihandle = 0;
95 if ((l = OF_getprop(of->sc_phandle, "name", name,
96 sizeof name - 1)) < 0)
97 panic("Device without name?");
98 if (l >= sizeof name)
99 l = sizeof name - 1;
100 name[l] = 0;
101 printf(": %s\n", name);
102 }
103
104 int
105 ofrtc_open(dev_t dev, int flags, int fmt, struct proc *p)
106 {
107 struct ofrtc_softc *of;
108 int unit = minor(dev);
109 char path[256];
110 int l;
111
112 if (unit >= ofrtc_cd.cd_ndevs)
113 return ENXIO;
114 if (!(of = ofrtc_cd.cd_devs[unit]))
115 return ENXIO;
116 if (!of->sc_ihandle) {
117 if ((l = OF_package_to_path(of->sc_phandle, path,
118 sizeof path - 1)) < 0 ||
119 l >= sizeof path)
120 return ENXIO;
121 path[l] = 0;
122
123 if (!(of->sc_ihandle = OF_open(path))) {
124 if (of->sc_ihandle) {
125 OF_close(of->sc_ihandle);
126 of->sc_ihandle = 0;
127 }
128 return ENXIO;
129 }
130
131 }
132
133 return 0;
134 }
135
136 static void
137 twodigit(char *bp, int i)
138 {
139 *bp++ = i / 10 + '';
140 *bp = i % 10 + '';
141 }
142
143 static int
144 twodigits(char *bp)
145 {
146 int i;
147
148 i = *bp++ - '';
149 return i * 10 + *bp++ - '';
150 }
151
152 int
153 ofrtc_read(dev_t dev, struct uio *uio, int flag)
154 {
155 struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
156 int date[6];
157 char buf[14];
158 int xlen;
159
160 if (uio->uio_offset >= sizeof buf)
161 return 0;
162
163 if (OF_call_method("get-time", of->sc_ihandle, 0, 6,
164 date, date + 1, date + 2,
165 date + 3, date + 4, date + 5))
166 return EIO;
167
168 twodigit(buf, date[5] % 100);
169 twodigit(buf + 2, date[4]);
170 twodigit(buf + 4, date[3]);
171 twodigit(buf + 6, date[2]);
172 twodigit(buf + 8, date[1]);
173 buf[10] = '.';
174 twodigit(buf + 11, date[0]);
175 buf[13] = '\n';
176
177 xlen = sizeof(buf) - uio->uio_offset;
178 if (xlen > uio->uio_resid)
179 xlen = uio->uio_resid;
180
181 return uiomove((caddr_t)buf, xlen, uio);
182 }
183
184 int
185 ofrtc_write(dev_t dev, struct uio *uio, int flag)
186 {
187 struct ofrtc_softc *of = ofrtc_cd.cd_devs[minor(dev)];
188 char buf[14];
189 int cnt, year, error;
190
191 /*
192 * We require atomic updates!
193 */
194 cnt = uio->uio_resid;
195 if (uio->uio_offset || (cnt != sizeof buf && cnt != sizeof buf - 1))
196 return EINVAL;
197
198 if ((error = uiomove((caddr_t)buf, sizeof buf, uio)) != 0)
199 return error;
200
201 if (cnt == sizeof buf && buf[sizeof buf - 1] != '\n')
202 return EINVAL;
203
204 year = twodigits(buf) + 1900;
205 if (year < 1970)
206 year += 100;
207 if (OF_call_method("set-time", of->sc_ihandle, 6, 0,
208 twodigits(buf + 11), twodigits(buf + 8), twodigits(buf + 6),
209 twodigits(buf + 4), twodigits(buf + 2), year))
210 return EIO;
211 return 0;
212 }
Cache object: fcaedb73ead6ae0c9cf802d75917d6c2
|