pcfclock.c (184130) | pcfclock.c (185003) |
---|---|
1/*- 2 * Copyright (c) 2000 Sascha Schumann. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. | 1/*- 2 * Copyright (c) 2000 Sascha Schumann. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY SASCHA SCHUMANN ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 16 * EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 18 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 19 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 20 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 22 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * 24 * 25 */ 26 27#include <sys/cdefs.h> | 13 * THIS SOFTWARE IS PROVIDED BY SASCHA SCHUMANN ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 16 * EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 18 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 19 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 20 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 22 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * 24 * 25 */ 26 27#include <sys/cdefs.h> |
28__FBSDID("$FreeBSD: head/sys/dev/ppbus/pcfclock.c 184130 2008-10-21 18:30:10Z jhb $"); | 28__FBSDID("$FreeBSD: head/sys/dev/ppbus/pcfclock.c 185003 2008-11-16 17:42:02Z jhb $"); |
29 30#include "opt_pcfclock.h" 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/bus.h> 35#include <sys/sockio.h> 36#include <sys/mbuf.h> --- 63 unchanged lines hidden (view full) --- 100 NR(buf, 14) <= 99 && \ 101 NR(buf, 12) <= 12 && \ 102 NR(buf, 10) <= 31 && \ 103 NR(buf, 6) <= 23 && \ 104 NR(buf, 4) <= 59 && \ 105 NR(buf, 2) <= 59) 106 107#define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4) | 29 30#include "opt_pcfclock.h" 31 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/bus.h> 35#include <sys/sockio.h> 36#include <sys/mbuf.h> --- 63 unchanged lines hidden (view full) --- 100 NR(buf, 14) <= 99 && \ 101 NR(buf, 12) <= 12 && \ 102 NR(buf, 10) <= 31 && \ 103 NR(buf, 6) <= 23 && \ 104 NR(buf, 4) <= 59 && \ 105 NR(buf, 2) <= 59) 106 107#define PCFCLOCK_BATTERY_STATUS_LOW(buf) (buf[8] & 4) |
108 | 108 |
109#define PCFCLOCK_CMD_TIME 0 /* send current time */ 110#define PCFCLOCK_CMD_COPY 7 /* copy received signal to PC */ 111 112static void 113pcfclock_identify(driver_t *driver, device_t parent) 114{ 115 116 device_t dev; --- 11 unchanged lines hidden (view full) --- 128 return (0); 129} 130 131static int 132pcfclock_attach(device_t dev) 133{ 134 struct pcfclock_data *sc = device_get_softc(dev); 135 int unit; | 109#define PCFCLOCK_CMD_TIME 0 /* send current time */ 110#define PCFCLOCK_CMD_COPY 7 /* copy received signal to PC */ 111 112static void 113pcfclock_identify(driver_t *driver, device_t parent) 114{ 115 116 device_t dev; --- 11 unchanged lines hidden (view full) --- 128 return (0); 129} 130 131static int 132pcfclock_attach(device_t dev) 133{ 134 struct pcfclock_data *sc = device_get_softc(dev); 135 int unit; |
136 | 136 |
137 unit = device_get_unit(dev); 138 139 sc->dev = dev; 140 sc->cdev = make_dev(&pcfclock_cdevsw, unit, 141 UID_ROOT, GID_WHEEL, 0400, PCFCLOCK_NAME "%d", unit); 142 if (sc->cdev == NULL) { 143 device_printf(dev, "Failed to create character device\n"); 144 return (ENXIO); 145 } 146 sc->cdev->si_drv1 = sc; 147 148 return (0); 149} 150 | 137 unit = device_get_unit(dev); 138 139 sc->dev = dev; 140 sc->cdev = make_dev(&pcfclock_cdevsw, unit, 141 UID_ROOT, GID_WHEEL, 0400, PCFCLOCK_NAME "%d", unit); 142 if (sc->cdev == NULL) { 143 device_printf(dev, "Failed to create character device\n"); 144 return (ENXIO); 145 } 146 sc->cdev->si_drv1 = sc; 147 148 return (0); 149} 150 |
151static int | 151static int |
152pcfclock_open(struct cdev *dev, int flag, int fms, struct thread *td) 153{ 154 struct pcfclock_data *sc = dev->si_drv1; 155 device_t pcfclockdev = sc->dev; 156 device_t ppbus = device_get_parent(pcfclockdev); 157 int res; | 152pcfclock_open(struct cdev *dev, int flag, int fms, struct thread *td) 153{ 154 struct pcfclock_data *sc = dev->si_drv1; 155 device_t pcfclockdev = sc->dev; 156 device_t ppbus = device_get_parent(pcfclockdev); 157 int res; |
158 | 158 |
159 if (!sc) 160 return (ENXIO); 161 162 if ((res = ppb_request_bus(ppbus, pcfclockdev, 163 (flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT))) 164 return (res); 165 166 sc->count++; | 159 if (!sc) 160 return (ENXIO); 161 162 if ((res = ppb_request_bus(ppbus, pcfclockdev, 163 (flag & O_NONBLOCK) ? PPB_DONTWAIT : PPB_WAIT))) 164 return (res); 165 166 sc->count++; |
167 | 167 |
168 return (0); 169} 170 171static int 172pcfclock_close(struct cdev *dev, int flags, int fmt, struct thread *td) 173{ 174 struct pcfclock_data *sc = dev->si_drv1; 175 device_t pcfclockdev = sc->dev; --- 6 unchanged lines hidden (view full) --- 182 return (0); 183} 184 185static void 186pcfclock_write_cmd(struct cdev *dev, unsigned char command) 187{ 188 struct pcfclock_data *sc = dev->si_drv1; 189 device_t pcfclockdev = sc->dev; | 168 return (0); 169} 170 171static int 172pcfclock_close(struct cdev *dev, int flags, int fmt, struct thread *td) 173{ 174 struct pcfclock_data *sc = dev->si_drv1; 175 device_t pcfclockdev = sc->dev; --- 6 unchanged lines hidden (view full) --- 182 return (0); 183} 184 185static void 186pcfclock_write_cmd(struct cdev *dev, unsigned char command) 187{ 188 struct pcfclock_data *sc = dev->si_drv1; 189 device_t pcfclockdev = sc->dev; |
190 device_t ppbus = device_get_parent(pcfclockdev); | 190 device_t ppbus = device_get_parent(pcfclockdev); |
191 unsigned char ctr = 14; 192 char i; | 191 unsigned char ctr = 14; 192 char i; |
193 | 193 |
194 for (i = 0; i <= 7; i++) { 195 ppb_wdtr(ppbus, i); 196 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO); 197 DELAY(3000); 198 } 199 ppb_wdtr(ppbus, command); 200 AUTOFEED_CLOCK(AFC_LO); 201 DELAY(3000); 202 AUTOFEED_CLOCK(AFC_HI); 203} 204 205static void | 194 for (i = 0; i <= 7; i++) { 195 ppb_wdtr(ppbus, i); 196 AUTOFEED_CLOCK(i & 1 ? AFC_HI : AFC_LO); 197 DELAY(3000); 198 } 199 ppb_wdtr(ppbus, command); 200 AUTOFEED_CLOCK(AFC_LO); 201 DELAY(3000); 202 AUTOFEED_CLOCK(AFC_HI); 203} 204 205static void |
206pcfclock_display_data(struct cdev *dev, char buf[18]) | 206pcfclock_display_data(struct cdev *dev, char buf[18]) |
207{ 208 struct pcfclock_data *sc = dev->si_drv1; 209#ifdef PCFCLOCK_VERBOSE 210 int year; 211 212 year = NR(buf, 14); 213 if (year < 70) 214 year += 100; --- 4 unchanged lines hidden (view full) --- 219 NR(buf, 6), NR(buf, 4), NR(buf, 2), 220 PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok"); 221#else 222 if (PCFCLOCK_BATTERY_STATUS_LOW(buf)) 223 device_printf(sc->dev, "BATTERY STATUS LOW ON\n"); 224#endif 225} 226 | 207{ 208 struct pcfclock_data *sc = dev->si_drv1; 209#ifdef PCFCLOCK_VERBOSE 210 int year; 211 212 year = NR(buf, 14); 213 if (year < 70) 214 year += 100; --- 4 unchanged lines hidden (view full) --- 219 NR(buf, 6), NR(buf, 4), NR(buf, 2), 220 PCFCLOCK_BATTERY_STATUS_LOW(buf) ? "LOW" : "ok"); 221#else 222 if (PCFCLOCK_BATTERY_STATUS_LOW(buf)) 223 device_printf(sc->dev, "BATTERY STATUS LOW ON\n"); 224#endif 225} 226 |
227static int | 227static int |
228pcfclock_read_data(struct cdev *dev, char *buf, ssize_t bits) 229{ 230 struct pcfclock_data *sc = dev->si_drv1; 231 device_t pcfclockdev = sc->dev; | 228pcfclock_read_data(struct cdev *dev, char *buf, ssize_t bits) 229{ 230 struct pcfclock_data *sc = dev->si_drv1; 231 device_t pcfclockdev = sc->dev; |
232 device_t ppbus = device_get_parent(pcfclockdev); | 232 device_t ppbus = device_get_parent(pcfclockdev); |
233 int i; 234 char waitfor; 235 int offset; 236 237 /* one byte per four bits */ 238 bzero(buf, ((bits + 3) >> 2) + 1); | 233 int i; 234 char waitfor; 235 int offset; 236 237 /* one byte per four bits */ 238 bzero(buf, ((bits + 3) >> 2) + 1); |
239 | 239 |
240 waitfor = 100; 241 for (i = 0; i <= bits; i++) { 242 /* wait for clock, maximum (waitfor*100) usec */ 243 while(!CLOCK_OK && --waitfor > 0) 244 DELAY(100); 245 246 /* timed out? */ | 240 waitfor = 100; 241 for (i = 0; i <= bits; i++) { 242 /* wait for clock, maximum (waitfor*100) usec */ 243 while(!CLOCK_OK && --waitfor > 0) 244 DELAY(100); 245 246 /* timed out? */ |
247 if (!waitfor) | 247 if (!waitfor) |
248 return (EIO); | 248 return (EIO); |
249 | 249 |
250 waitfor = 100; /* reload */ | 250 waitfor = 100; /* reload */ |
251 | 251 |
252 /* give it some time */ 253 DELAY(500); 254 255 /* calculate offset into buffer */ 256 offset = i >> 2; 257 buf[offset] <<= 1; 258 259 if (BIT_SET) 260 buf[offset] |= 1; 261 } 262 263 return (0); 264} 265 | 252 /* give it some time */ 253 DELAY(500); 254 255 /* calculate offset into buffer */ 256 offset = i >> 2; 257 buf[offset] <<= 1; 258 259 if (BIT_SET) 260 buf[offset] |= 1; 261 } 262 263 return (0); 264} 265 |
266static int 267pcfclock_read_dev(struct cdev *dev, char *buf, int maxretries) | 266static int 267pcfclock_read_dev(struct cdev *dev, char *buf, int maxretries) |
268{ 269 struct pcfclock_data *sc = dev->si_drv1; 270 device_t pcfclockdev = sc->dev; | 268{ 269 struct pcfclock_data *sc = dev->si_drv1; 270 device_t pcfclockdev = sc->dev; |
271 device_t ppbus = device_get_parent(pcfclockdev); | 271 device_t ppbus = device_get_parent(pcfclockdev); |
272 int error = 0; 273 274 ppb_set_mode(ppbus, PPB_COMPATIBLE); 275 276 while (--maxretries > 0) { 277 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME); 278 if (pcfclock_read_data(dev, buf, 68)) 279 continue; | 272 int error = 0; 273 274 ppb_set_mode(ppbus, PPB_COMPATIBLE); 275 276 while (--maxretries > 0) { 277 pcfclock_write_cmd(dev, PCFCLOCK_CMD_TIME); 278 if (pcfclock_read_data(dev, buf, 68)) 279 continue; |
280 | 280 |
281 if (!PCFCLOCK_CORRECT_SYNC(buf)) 282 continue; 283 284 if (!PCFCLOCK_CORRECT_FORMAT(buf)) 285 continue; 286 287 break; 288 } 289 290 if (!maxretries) 291 error = EIO; | 281 if (!PCFCLOCK_CORRECT_SYNC(buf)) 282 continue; 283 284 if (!PCFCLOCK_CORRECT_FORMAT(buf)) 285 continue; 286 287 break; 288 } 289 290 if (!maxretries) 291 error = EIO; |
292 | 292 |
293 return (error); 294} 295 296static int 297pcfclock_read(struct cdev *dev, struct uio *uio, int ioflag) 298{ 299 struct pcfclock_data *sc = dev->si_drv1; 300 char buf[18]; 301 int error = 0; 302 303 if (uio->uio_resid < 18) 304 return (ERANGE); 305 306 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES); | 293 return (error); 294} 295 296static int 297pcfclock_read(struct cdev *dev, struct uio *uio, int ioflag) 298{ 299 struct pcfclock_data *sc = dev->si_drv1; 300 char buf[18]; 301 int error = 0; 302 303 if (uio->uio_resid < 18) 304 return (ERANGE); 305 306 error = pcfclock_read_dev(dev, buf, PCFCLOCK_MAX_RETRIES); |
307 | 307 |
308 if (error) { 309 device_printf(sc->dev, "no PCF found\n"); 310 } else { 311 pcfclock_display_data(dev, buf); | 308 if (error) { 309 device_printf(sc->dev, "no PCF found\n"); 310 } else { 311 pcfclock_display_data(dev, buf); |
312 | 312 |
313 uiomove(buf, 18, uio); 314 } | 313 uiomove(buf, 18, uio); 314 } |
315 | 315 |
316 return (error); 317} 318 319static device_method_t pcfclock_methods[] = { 320 /* device interface */ 321 DEVMETHOD(device_identify, pcfclock_identify), 322 DEVMETHOD(device_probe, pcfclock_probe), 323 DEVMETHOD(device_attach, pcfclock_attach), 324 325 { 0, 0 } 326}; 327 328static driver_t pcfclock_driver = { 329 PCFCLOCK_NAME, 330 pcfclock_methods, 331 sizeof(struct pcfclock_data), 332}; 333 334DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0); | 316 return (error); 317} 318 319static device_method_t pcfclock_methods[] = { 320 /* device interface */ 321 DEVMETHOD(device_identify, pcfclock_identify), 322 DEVMETHOD(device_probe, pcfclock_probe), 323 DEVMETHOD(device_attach, pcfclock_attach), 324 325 { 0, 0 } 326}; 327 328static driver_t pcfclock_driver = { 329 PCFCLOCK_NAME, 330 pcfclock_methods, 331 sizeof(struct pcfclock_data), 332}; 333 334DRIVER_MODULE(pcfclock, ppbus, pcfclock_driver, pcfclock_devclass, 0, 0); |