xref: /openbsd/sys/dev/usb/udcf.c (revision 404b540a)
1 /*	$OpenBSD: udcf.c,v 1.47 2009/10/13 19:33:17 pirofti Exp $ */
2 
3 /*
4  * Copyright (c) 2006, 2007, 2008 Marc Balmer <mbalmer@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/kernel.h>
22 #include <sys/conf.h>
23 #include <sys/file.h>
24 #include <sys/select.h>
25 #include <sys/proc.h>
26 #include <sys/vnode.h>
27 #include <sys/device.h>
28 #include <sys/poll.h>
29 #include <sys/time.h>
30 #include <sys/sensors.h>
31 
32 #include <dev/usb/usb.h>
33 #include <dev/usb/usbdi.h>
34 #include <dev/usb/usbdi_util.h>
35 #include <dev/usb/usbdevs.h>
36 
37 #ifdef UDCF_DEBUG
38 #define DPRINTFN(n, x)	do { if (udcfdebug > (n)) printf x; } while (0)
39 int udcfdebug = 0;
40 #else
41 #define DPRINTFN(n, x)
42 #endif
43 #define DPRINTF(x)	DPRINTFN(0, x)
44 
45 #define UDCF_READ_IDX	0x1f
46 
47 #define UDCF_CTRL_IDX	0x33
48 #define UDCF_CTRL_VAL	0x98
49 
50 #define FT232R_RESET	0x00	/* reset USB request */
51 #define FT232R_STATUS	0x05	/* get modem status USB request */
52 #define FT232R_RI	0x40	/* ring indicator */
53 
54 #define DPERIOD1	((long) 5 * 60)		/* degrade OK -> WARN */
55 #define DPERIOD2	((long) 15 * 60)	/* degrade WARN -> CRIT */
56 
57 /* max. skew of received time diff vs. measured time diff in percent. */
58 #define MAX_SKEW	5
59 
60 #define CLOCK_DCF77	0
61 #define CLOCK_HBG	1
62 
63 static const char	*clockname[2] = {
64 	"DCF77",
65 	"HBG" };
66 
67 struct udcf_softc {
68 	struct device		sc_dev;		/* base device */
69 	usbd_device_handle	sc_udev;	/* USB device */
70 	usbd_interface_handle	sc_iface;	/* data interface */
71 	u_char			sc_dying;	/* disconnecting */
72 
73 	struct timeout		sc_to;
74 	struct usb_task		sc_task;
75 
76 	struct timeout		sc_bv_to;	/* bit-value detect */
77 	struct timeout		sc_db_to;	/* debounce */
78 	struct timeout		sc_mg_to;	/* minute-gap detect */
79 	struct timeout		sc_sl_to;	/* signal-loss detect */
80 	struct timeout		sc_it_to;	/* invalidate time */
81 	struct timeout		sc_ct_to;	/* detect clock type */
82 	struct usb_task		sc_bv_task;
83 	struct usb_task		sc_mg_task;
84 	struct usb_task		sc_sl_task;
85 	struct usb_task		sc_ct_task;
86 
87 	usb_device_request_t	sc_req;
88 
89 	int			sc_detect_ct;	/* != 0: autodetect type */
90 	int			sc_clocktype;	/* DCF77 or HBG */
91 	int			sc_sync;	/* 1 during sync */
92 	u_int64_t		sc_mask;	/* 64 bit mask */
93 	u_int64_t		sc_tbits;	/* Time bits */
94 	int			sc_minute;
95 	int			sc_level;
96 	time_t			sc_last_mg;
97 	int			(*sc_signal)(struct udcf_softc *);
98 
99 	time_t			sc_current;	/* current time */
100 	time_t			sc_next;	/* time to become valid next */
101 	time_t			sc_last;
102 	int			sc_nrecv;	/* consecutive valid times */
103 	struct timeval		sc_last_tv;	/* uptime of last valid time */
104 	struct ksensor		sc_sensor;
105 #ifdef UDCF_DEBUG
106 	struct ksensor		sc_skew;	/* recv vs local skew */
107 #endif
108 	struct ksensordev	sc_sensordev;
109 };
110 
111 /*
112  * timeouts being used in hz:
113  * t_bv		bit value detection (150ms)
114  * t_ct		detect clocktype (250ms)
115  * t_sync	sync (950ms)
116  * t_mg		minute gap detection (1500ms)
117  * t_mgsync	resync after a minute gap (450ms)
118  * t_sl		detect signal loss (3sec)
119  * t_wait	wait (5sec)
120  * t_warn	degrade sensor status to warning (5min)
121  * t_crit	degrade sensor status to critical (15min)
122  */
123 static int t_bv, t_ct, t_sync, t_mg, t_sl, t_mgsync, t_wait, t_warn, t_crit;
124 
125 void	udcf_intr(void *);
126 void	udcf_probe(void *);
127 
128 void	udcf_bv_intr(void *);
129 void	udcf_mg_intr(void *);
130 void	udcf_sl_intr(void *);
131 void	udcf_it_intr(void *);
132 void	udcf_ct_intr(void *);
133 void	udcf_bv_probe(void *);
134 void	udcf_mg_probe(void *);
135 void	udcf_sl_probe(void *);
136 void	udcf_ct_probe(void *);
137 
138 int udcf_match(struct device *, void *, void *);
139 void udcf_attach(struct device *, struct device *, void *);
140 int udcf_detach(struct device *, int);
141 int udcf_activate(struct device *, int);
142 
143 int udcf_nc_signal(struct udcf_softc *);
144 int udcf_nc_init_hw(struct udcf_softc *);
145 int udcf_ft232r_signal(struct udcf_softc *);
146 int udcf_ft232r_init_hw(struct udcf_softc *);
147 
148 struct cfdriver udcf_cd = {
149 	NULL, "udcf", DV_DULL
150 };
151 
152 const struct cfattach udcf_ca = {
153 	sizeof(struct udcf_softc),
154 	udcf_match,
155 	udcf_attach,
156 	udcf_detach,
157 	udcf_activate
158 };
159 
160 static const struct usb_devno udcf_devs[] = {
161 	{ USB_VENDOR_GUDE, USB_PRODUCT_GUDE_DCF },
162 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_DCF },
163 	{ USB_VENDOR_FTDI, USB_PRODUCT_FTDI_HBG }
164 };
165 
166 int
167 udcf_match(struct device *parent, void *match, void *aux)
168 {
169 	struct usb_attach_arg		*uaa = aux;
170 
171 	if (uaa->iface != NULL)
172 		return UMATCH_NONE;
173 
174 	if (usb_lookup(udcf_devs, uaa->vendor, uaa->product) == NULL)
175 		return UMATCH_NONE;
176 
177 	return UMATCH_VENDOR_PRODUCT;
178 }
179 
180 void
181 udcf_attach(struct device *parent, struct device *self, void *aux)
182 {
183 	struct udcf_softc		*sc = (struct udcf_softc *)self;
184 	struct usb_attach_arg		*uaa = aux;
185 	usbd_device_handle		 dev = uaa->device;
186 	usbd_interface_handle		 iface;
187 	struct timeval			 t;
188 	usbd_status			 err;
189 
190 	switch (uaa->product) {
191 	case USB_PRODUCT_GUDE_DCF:
192 		sc->sc_detect_ct = 1;
193 		sc->sc_signal = udcf_nc_signal;
194 		strlcpy(sc->sc_sensor.desc, "Unknown",
195 		    sizeof(sc->sc_sensor.desc));
196 		break;
197 	case USB_PRODUCT_FTDI_DCF:
198 		sc->sc_signal = udcf_ft232r_signal;
199 		strlcpy(sc->sc_sensor.desc, clockname[CLOCK_DCF77],
200 		    sizeof(sc->sc_sensor.desc));
201 		break;
202 	case USB_PRODUCT_FTDI_HBG:
203 		sc->sc_signal = udcf_ft232r_signal;
204 		strlcpy(sc->sc_sensor.desc, clockname[CLOCK_HBG],
205 		    sizeof(sc->sc_sensor.desc));
206 		break;
207 	}
208 
209 	usb_init_task(&sc->sc_task, udcf_probe, sc);
210 	usb_init_task(&sc->sc_bv_task, udcf_bv_probe, sc);
211 	usb_init_task(&sc->sc_mg_task, udcf_mg_probe, sc);
212 	usb_init_task(&sc->sc_sl_task, udcf_sl_probe, sc);
213 
214 	timeout_set(&sc->sc_to, udcf_intr, sc);
215 	timeout_set(&sc->sc_bv_to, udcf_bv_intr, sc);
216 	timeout_set(&sc->sc_mg_to, udcf_mg_intr, sc);
217 	timeout_set(&sc->sc_sl_to, udcf_sl_intr, sc);
218 	timeout_set(&sc->sc_it_to, udcf_it_intr, sc);
219 
220 	if (sc->sc_detect_ct) {
221 		usb_init_task(&sc->sc_ct_task, udcf_ct_probe, sc);
222 		timeout_set(&sc->sc_ct_to, udcf_ct_intr, sc);
223 	}
224 	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
225 	    sizeof(sc->sc_sensordev.xname));
226 
227 	sc->sc_sensor.type = SENSOR_TIMEDELTA;
228 	sc->sc_sensor.status = SENSOR_S_UNKNOWN;
229 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
230 
231 #ifdef UDCF_DEBUG
232 	sc->sc_skew.type = SENSOR_TIMEDELTA;
233 	sc->sc_skew.status = SENSOR_S_UNKNOWN;
234 	strlcpy(sc->sc_skew.desc, "local clock skew",
235 	    sizeof(sc->sc_skew.desc));
236 	sensor_attach(&sc->sc_sensordev, &sc->sc_skew);
237 #endif
238 	sensordev_install(&sc->sc_sensordev);
239 
240 	sc->sc_udev = dev;
241 	if ((err = usbd_set_config_index(dev, 0, 1))) {
242 		DPRINTF(("%s: failed to set configuration, err=%s\n",
243 		    sc->sc_dev.dv_xname, usbd_errstr(err)));
244 		goto fishy;
245 	}
246 
247 	if ((err = usbd_device2interface_handle(dev, 0, &iface))) {
248 		DPRINTF(("%s: failed to get interface, err=%s\n",
249 		    sc->sc_dev.dv_xname, usbd_errstr(err)));
250 		goto fishy;
251 	}
252 
253 	sc->sc_iface = iface;
254 
255 	sc->sc_clocktype = -1;
256 	sc->sc_level = 0;
257 	sc->sc_minute = 0;
258 	sc->sc_last_mg = 0L;
259 
260 	sc->sc_sync = 1;
261 
262 	sc->sc_current = 0L;
263 	sc->sc_next = 0L;
264 	sc->sc_nrecv = 0;
265 	sc->sc_last = 0L;
266 	sc->sc_last_tv.tv_sec = 0L;
267 
268 	switch (uaa->product) {
269 	case USB_PRODUCT_GUDE_DCF:
270 		if (udcf_nc_init_hw(sc))
271 			goto fishy;
272 		break;
273 	case USB_PRODUCT_FTDI_DCF:	/* FALLTHROUGH */
274 	case USB_PRODUCT_FTDI_HBG:
275 		if (udcf_ft232r_init_hw(sc))
276 			goto fishy;
277 		break;
278 	}
279 
280 	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
281 	    &sc->sc_dev);
282 
283 	/* convert timevals to hz */
284 	t.tv_sec = 0L;
285 	t.tv_usec = 150000L;
286 	t_bv = tvtohz(&t);
287 
288 	t.tv_usec = 450000L;
289 	t_mgsync = tvtohz(&t);
290 
291 	t.tv_usec = 950000L;
292 	t_sync = tvtohz(&t);
293 
294 	t.tv_sec = 1L;
295 	t.tv_usec = 500000L;
296 	t_mg = tvtohz(&t);
297 
298 	t.tv_sec = 3L;
299 	t.tv_usec = 0L;
300 	t_sl = tvtohz(&t);
301 
302 	t.tv_sec = 5L;
303 	t_wait = tvtohz(&t);
304 
305 	t.tv_sec = DPERIOD1;
306 	t_warn = tvtohz(&t);
307 
308 	t.tv_sec = DPERIOD2;
309 	t_crit = tvtohz(&t);
310 
311 	if (sc->sc_detect_ct) {
312 		t.tv_sec = 0L;
313 		t.tv_usec = 250000L;
314 		t_ct = tvtohz(&t);
315 	}
316 
317 	/* Give the receiver some slack to stabilize */
318 	timeout_add(&sc->sc_to, t_wait);
319 
320 	/* Detect signal loss */
321 	timeout_add(&sc->sc_sl_to, t_wait + t_sl);
322 
323 	DPRINTF(("synchronizing\n"));
324 	return;
325 
326 fishy:
327 	DPRINTF(("udcf_attach failed\n"));
328 	sc->sc_dying = 1;
329 }
330 
331 int
332 udcf_detach(struct device *self, int flags)
333 {
334 	struct udcf_softc	*sc = (struct udcf_softc *)self;
335 
336 	sc->sc_dying = 1;
337 
338 	timeout_del(&sc->sc_to);
339 	timeout_del(&sc->sc_bv_to);
340 	timeout_del(&sc->sc_mg_to);
341 	timeout_del(&sc->sc_sl_to);
342 	timeout_del(&sc->sc_it_to);
343 	if (sc->sc_detect_ct)
344 		timeout_del(&sc->sc_ct_to);
345 
346 	/* Unregister the clock with the kernel */
347 	sensordev_deinstall(&sc->sc_sensordev);
348 	usb_rem_task(sc->sc_udev, &sc->sc_task);
349 	usb_rem_task(sc->sc_udev, &sc->sc_bv_task);
350 	usb_rem_task(sc->sc_udev, &sc->sc_mg_task);
351 	usb_rem_task(sc->sc_udev, &sc->sc_sl_task);
352 	if (sc->sc_detect_ct)
353 		usb_rem_task(sc->sc_udev, &sc->sc_ct_task);
354 
355 	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
356 	    &sc->sc_dev);
357 	return 0;
358 }
359 
360 /* udcf_intr runs in an interrupt context */
361 void
362 udcf_intr(void *xsc)
363 {
364 	struct udcf_softc *sc = xsc;
365 	usb_add_task(sc->sc_udev, &sc->sc_task);
366 }
367 
368 /* bit value detection */
369 void
370 udcf_bv_intr(void *xsc)
371 {
372 	struct udcf_softc *sc = xsc;
373 	usb_add_task(sc->sc_udev, &sc->sc_bv_task);
374 }
375 
376 /* minute gap detection */
377 void
378 udcf_mg_intr(void *xsc)
379 {
380 	struct udcf_softc *sc = xsc;
381 	usb_add_task(sc->sc_udev, &sc->sc_mg_task);
382 }
383 
384 /* signal loss detection */
385 void
386 udcf_sl_intr(void *xsc)
387 {
388 	struct udcf_softc *sc = xsc;
389 	usb_add_task(sc->sc_udev, &sc->sc_sl_task);
390 }
391 
392 /* detect the clock type (DCF77 or HBG) */
393 void
394 udcf_ct_intr(void *xsc)
395 {
396 	struct udcf_softc *sc = xsc;
397 	usb_add_task(sc->sc_udev, &sc->sc_ct_task);
398 }
399 
400 /*
401  * initialize the Expert mouseCLOCK USB devices, they use a NetCologne
402  * chip to interface the receiver.  Power must be supplied to the
403  * receiver and the receiver must be turned on.
404  */
405 int
406 udcf_nc_init_hw(struct udcf_softc *sc)
407 {
408 	usbd_status			 err;
409 	usb_device_request_t		 req;
410 	uWord				 result;
411 	int				 actlen;
412 
413 	/* Prepare the USB request to probe the value */
414 	sc->sc_req.bmRequestType = UT_READ_VENDOR_DEVICE;
415 	sc->sc_req.bRequest = 1;
416 	USETW(sc->sc_req.wValue, 0);
417 	USETW(sc->sc_req.wIndex, UDCF_READ_IDX);
418 	USETW(sc->sc_req.wLength, 1);
419 
420 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
421 	req.bRequest = 0;
422 	USETW(req.wValue, 0);
423 	USETW(req.wIndex, 0);
424 	USETW(req.wLength, 0);
425 	if ((err = usbd_do_request_flags(sc->sc_udev, &req, &result,
426 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT))) {
427 		DPRINTF(("failed to turn on power for receiver\n"));
428 		return -1;
429 	}
430 
431 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
432 	req.bRequest = 0;
433 	USETW(req.wValue, UDCF_CTRL_VAL);
434 	USETW(req.wIndex, UDCF_CTRL_IDX);
435 	USETW(req.wLength, 0);
436 	if ((err = usbd_do_request_flags(sc->sc_udev, &req, &result,
437 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT))) {
438 		DPRINTF(("failed to turn on receiver\n"));
439 		return -1;
440 	}
441 	return 0;
442 }
443 
444 /*
445  * initialize the Expert mouseCLOCK USB II devices, they use an FTDI
446  * FT232R chip to interface the receiver.  Only reset the chip.
447  */
448 int
449 udcf_ft232r_init_hw(struct udcf_softc *sc)
450 {
451 	usbd_status		err;
452 	usb_device_request_t	req;
453 
454 	req.bmRequestType = UT_WRITE_VENDOR_DEVICE;
455 	req.bRequest = FT232R_RESET;
456 	/* 0 resets the SIO */
457 	USETW(req.wValue,FT232R_RESET);
458 	USETW(req.wIndex, 0);
459 	USETW(req.wLength, 0);
460 	err = usbd_do_request(sc->sc_udev, &req, NULL);
461 	if (err) {
462 		DPRINTF(("failed to reset ftdi\n"));
463 		return -1;
464 	}
465 	return 0;
466 }
467 
468 /*
469  * return 1 during high-power-, 0 during low-power-emission
470  * If bit 0 is set, the transmitter emits at full power.
471  * During the low-power emission we decode a zero bit.
472  */
473 int
474 udcf_nc_signal(struct udcf_softc *sc)
475 {
476 	int		actlen;
477 	unsigned char	data;
478 
479 	if (usbd_do_request_flags(sc->sc_udev, &sc->sc_req, &data,
480 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT))
481 		/* This happens if we pull the receiver */
482 		return -1;
483 	return data & 0x01;
484 }
485 
486 /* pick up the signal level through the FTDI FT232R chip */
487 int
488 udcf_ft232r_signal(struct udcf_softc *sc)
489 {
490 	usb_device_request_t	req;
491 	int			actlen;
492 	u_int16_t		data;
493 
494 	req.bmRequestType = UT_READ_VENDOR_DEVICE;
495 	req.bRequest = FT232R_STATUS;
496 	USETW(req.wValue, 0);
497 	USETW(req.wIndex, 0);
498 	USETW(req.wLength, 2);
499 	if (usbd_do_request_flags(sc->sc_udev, &req, &data,
500 	    USBD_SHORT_XFER_OK, &actlen, USBD_DEFAULT_TIMEOUT)) {
501 		DPRINTFN(2, ("error reading ftdi modem status\n"));
502 		return -1;
503 	}
504 	DPRINTFN(2, ("ftdi status 0x%04x\n", data));
505 	return data & FT232R_RI ? 0 : 1;
506 }
507 
508 /* udcf_probe runs in a process context. */
509 void
510 udcf_probe(void *xsc)
511 {
512 	struct udcf_softc	*sc = xsc;
513 	struct timespec		 now;
514 	int			 data;
515 
516 	if (sc->sc_dying)
517 		return;
518 
519 	data = sc->sc_signal(sc);
520 	if (data == -1)
521 		return;
522 
523 	if (data) {
524 		sc->sc_level = 1;
525 		timeout_add(&sc->sc_to, 1);
526 		return;
527 	}
528 
529 	if (sc->sc_level == 0)
530 		return;
531 
532 	/* the beginning of a second */
533 	sc->sc_level = 0;
534 	if (sc->sc_minute == 1) {
535 		if (sc->sc_sync) {
536 			DPRINTF(("start collecting bits\n"));
537 			sc->sc_sync = 0;
538 			if (sc->sc_sensor.status == SENSOR_S_UNKNOWN &&
539 			    sc->sc_detect_ct)
540 				sc->sc_clocktype = -1;
541 		} else {
542 			/* provide the timedelta */
543 			microtime(&sc->sc_sensor.tv);
544 			nanotime(&now);
545 			sc->sc_current = sc->sc_next;
546 			sc->sc_sensor.value = (int64_t)(now.tv_sec -
547 			    sc->sc_current) * 1000000000LL + now.tv_nsec;
548 
549 			/* set the clocktype and make sensor valid */
550 			if (sc->sc_sensor.status == SENSOR_S_UNKNOWN &&
551 			    sc->sc_detect_ct) {
552 				strlcpy(sc->sc_sensor.desc, sc->sc_clocktype ?
553 				    clockname[CLOCK_HBG] :
554 				    clockname[CLOCK_DCF77],
555 				    sizeof(sc->sc_sensor.desc));
556 			}
557 			sc->sc_sensor.status = SENSOR_S_OK;
558 
559 			/*
560 			 * if no valid time information is received
561 			 * during the next 5 minutes, the sensor state
562 			 * will be degraded to SENSOR_S_WARN
563 			 */
564 			timeout_add(&sc->sc_it_to, t_warn);
565 		}
566 		sc->sc_minute = 0;
567 	}
568 
569 	timeout_add(&sc->sc_to, t_sync);	/* resync in 950 ms */
570 
571 	/* no clock and bit detection during sync */
572 	if (!sc->sc_sync) {
573 		/* detect bit value */
574 		timeout_add(&sc->sc_bv_to, t_bv);
575 
576 		/* detect clocktype */
577 		if (sc->sc_detect_ct && sc->sc_clocktype == -1)
578 			timeout_add(&sc->sc_ct_to, t_ct);
579 	}
580 	timeout_add(&sc->sc_mg_to, t_mg);	/* detect minute gap */
581 	timeout_add(&sc->sc_sl_to, t_sl);	/* detect signal loss */
582 }
583 
584 /* detect the bit value */
585 void
586 udcf_bv_probe(void *xsc)
587 {
588 	struct udcf_softc	*sc = xsc;
589 	int			 data;
590 
591 	if (sc->sc_dying)
592 		return;
593 
594 	data = sc->sc_signal(sc);
595 	if (data == -1) {
596 		DPRINTF(("bit detection failed\n"));
597 		return;
598 	}
599 
600 	DPRINTFN(1, (data ? "0" : "1"));
601 	if (!(data))
602 		sc->sc_tbits |= sc->sc_mask;
603 	sc->sc_mask <<= 1;
604 }
605 
606 /* detect the minute gap */
607 void
608 udcf_mg_probe(void *xsc)
609 {
610 	struct udcf_softc	*sc = xsc;
611 	struct clock_ymdhms	 ymdhm;
612 	struct timeval		 monotime;
613 	int			 tdiff_recv, tdiff_local;
614 	int			 skew;
615 	int			 minute_bits, hour_bits, day_bits;
616 	int			 month_bits, year_bits, wday;
617 	int			 p1, p2, p3;
618 	int			 p1_bit, p2_bit, p3_bit;
619 	int			 r_bit, a1_bit, a2_bit, z1_bit, z2_bit;
620 	int			 s_bit, m_bit;
621 	u_int32_t		 parity = 0x6996;
622 
623 	if (sc->sc_sync) {
624 		sc->sc_minute = 1;
625 		goto cleanbits;
626 	}
627 
628 	if (time_second - sc->sc_last_mg < 57) {
629 		DPRINTF(("\nunexpected gap, resync\n"));
630 		sc->sc_sync = sc->sc_minute = 1;
631 		goto cleanbits;
632 	}
633 
634 	/* extract bits w/o parity */
635 	m_bit = sc->sc_tbits & 1;
636 	r_bit = sc->sc_tbits >> 15 & 1;
637 	a1_bit = sc->sc_tbits >> 16 & 1;
638 	z1_bit = sc->sc_tbits >> 17 & 1;
639 	z2_bit = sc->sc_tbits >> 18 & 1;
640 	a2_bit = sc->sc_tbits >> 19 & 1;
641 	s_bit = sc->sc_tbits >> 20 & 1;
642 	p1_bit = sc->sc_tbits >> 28 & 1;
643 	p2_bit = sc->sc_tbits >> 35 & 1;
644 	p3_bit = sc->sc_tbits >> 58 & 1;
645 
646 	minute_bits = sc->sc_tbits >> 21 & 0x7f;
647 	hour_bits = sc->sc_tbits >> 29 & 0x3f;
648 	day_bits = sc->sc_tbits >> 36 & 0x3f;
649 	wday = (sc->sc_tbits >> 42) & 0x07;
650 	month_bits = sc->sc_tbits >> 45 & 0x1f;
651 	year_bits = sc->sc_tbits >> 50 & 0xff;
652 
653 	/* validate time information */
654 	p1 = (parity >> (minute_bits & 0x0f) & 1) ^
655 	    (parity >> (minute_bits >> 4) & 1);
656 
657 	p2 = (parity >> (hour_bits & 0x0f) & 1) ^
658 	    (parity >> (hour_bits >> 4) & 1);
659 
660 	p3 = (parity >> (day_bits & 0x0f) & 1) ^
661 	    (parity >> (day_bits >> 4) & 1) ^
662 	    ((parity >> wday) & 1) ^ (parity >> (month_bits & 0x0f) & 1) ^
663 	    (parity >> (month_bits >> 4) & 1) ^
664 	    (parity >> (year_bits & 0x0f) & 1) ^
665 	    (parity >> (year_bits >> 4) & 1);
666 
667 	if (m_bit == 0 && s_bit == 1 && p1 == p1_bit && p2 == p2_bit &&
668 	    p3 == p3_bit && (z1_bit ^ z2_bit)) {
669 
670 		/* Decode time */
671 		if ((ymdhm.dt_year = 2000 + FROMBCD(year_bits)) > 2037) {
672 			DPRINTF(("year out of range, resync\n"));
673 			sc->sc_sync = 1;
674 			goto cleanbits;
675 		}
676 		ymdhm.dt_min = FROMBCD(minute_bits);
677 		ymdhm.dt_hour = FROMBCD(hour_bits);
678 		ymdhm.dt_day = FROMBCD(day_bits);
679 		ymdhm.dt_mon = FROMBCD(month_bits);
680 		ymdhm.dt_sec = 0;
681 
682 		sc->sc_next = clock_ymdhms_to_secs(&ymdhm);
683 		getmicrouptime(&monotime);
684 
685 		/* convert to coordinated universal time */
686 		sc->sc_next -= z1_bit ? 7200 : 3600;
687 
688 		DPRINTF(("\n%02d.%02d.%04d %02d:%02d:00 %s",
689 		    ymdhm.dt_day, ymdhm.dt_mon, ymdhm.dt_year,
690 		    ymdhm.dt_hour, ymdhm.dt_min, z1_bit ? "CEST" : "CET"));
691 		DPRINTF((r_bit ? ", call bit" : ""));
692 		DPRINTF((a1_bit ? ", dst chg ann." : ""));
693 		DPRINTF((a2_bit ? ", leap sec ann." : ""));
694 		DPRINTF(("\n"));
695 
696 		if (sc->sc_last) {
697 			tdiff_recv = sc->sc_next - sc->sc_last;
698 			tdiff_local = monotime.tv_sec - sc->sc_last_tv.tv_sec;
699 			skew = abs(tdiff_local - tdiff_recv);
700 #ifdef UDCF_DEBUG
701 			if (sc->sc_skew.status == SENSOR_S_UNKNOWN)
702 				sc->sc_skew.status = SENSOR_S_CRIT;
703 			sc->sc_skew.value = skew * 1000000000LL;
704 			getmicrotime(&sc->sc_skew.tv);
705 #endif
706 			DPRINTF(("local = %d, recv = %d, skew = %d\n",
707 			    tdiff_local, tdiff_recv, skew));
708 
709 			if (skew && skew * 100LL / tdiff_local > MAX_SKEW) {
710 				DPRINTF(("skew out of tolerated range\n"));
711 				goto cleanbits;
712 			} else {
713 				if (sc->sc_nrecv < 2) {
714 					sc->sc_nrecv++;
715 					DPRINTF(("got frame %d\n",
716 					    sc->sc_nrecv));
717 				} else {
718 					DPRINTF(("data is valid\n"));
719 					sc->sc_minute = 1;
720 				}
721 			}
722 		} else {
723 			DPRINTF(("received the first frame\n"));
724 			sc->sc_nrecv = 1;
725 		}
726 
727 		/* record the time received and when it was received */
728 		sc->sc_last = sc->sc_next;
729 		sc->sc_last_tv.tv_sec = monotime.tv_sec;
730 	} else {
731 		DPRINTF(("\nparity error, resync\n"));
732 		sc->sc_sync = sc->sc_minute = 1;
733 	}
734 
735 cleanbits:
736 	timeout_add(&sc->sc_to, t_mgsync);	/* re-sync in 450 ms */
737 	sc->sc_last_mg = time_second;
738 	sc->sc_tbits = 0LL;
739 	sc->sc_mask = 1LL;
740 }
741 
742 /* detect signal loss */
743 void
744 udcf_sl_probe(void *xsc)
745 {
746 	struct udcf_softc *sc = xsc;
747 
748 	if (sc->sc_dying)
749 		return;
750 
751 	DPRINTF(("no signal\n"));
752 	sc->sc_sync = 1;
753 	timeout_add(&sc->sc_to, t_wait);
754 	timeout_add(&sc->sc_sl_to, t_wait + t_sl);
755 }
756 
757 /* invalidate timedelta (called in an interrupt context) */
758 void
759 udcf_it_intr(void *xsc)
760 {
761 	struct udcf_softc *sc = xsc;
762 
763 	if (sc->sc_dying)
764 		return;
765 
766 	if (sc->sc_sensor.status == SENSOR_S_OK) {
767 		sc->sc_sensor.status = SENSOR_S_WARN;
768 		/*
769 		 * further degrade in 15 minutes if we dont receive any new
770 		 * time information
771 		 */
772 		timeout_add(&sc->sc_it_to, t_crit);
773 	} else {
774 		sc->sc_sensor.status = SENSOR_S_CRIT;
775 		sc->sc_nrecv = 0;
776 	}
777 }
778 
779 /* detect clock type.  used for older devices only. */
780 void
781 udcf_ct_probe(void *xsc)
782 {
783 	struct udcf_softc	*sc = xsc;
784 	int			 data;
785 
786 	if (sc->sc_dying)
787 		return;
788 
789 	data = sc->sc_signal(sc);
790 	if (data == -1) {
791 		DPRINTF(("clocktype detection failed\n"));
792 		return;
793 	}
794 
795 	sc->sc_clocktype = data ? 0 : 1;
796 	DPRINTF(("\nclocktype is %s\n", sc->sc_clocktype ?
797 		clockname[CLOCK_HBG] : clockname[CLOCK_DCF77]));
798 }
799 
800 int
801 udcf_activate(struct device *self, int act)
802 {
803 	struct udcf_softc *sc = (struct udcf_softc *)self;
804 
805 	switch (act) {
806 	case DVACT_ACTIVATE:
807 		break;
808 	case DVACT_DEACTIVATE:
809 		sc->sc_dying = 1;
810 		break;
811 	}
812 	return 0;
813 }
814