1 /* $OpenBSD: umbg.c,v 1.9 2009/04/26 02:20:58 cnst Exp $ */ 2 3 /* 4 * Copyright (c) 2007 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/syslog.h> 30 #include <sys/time.h> 31 #include <sys/sensors.h> 32 33 #include <dev/usb/usb.h> 34 #include <dev/usb/usbdi.h> 35 #include <dev/usb/usbdi_util.h> 36 #include <dev/usb/usbdevs.h> 37 38 #ifdef UMBG_DEBUG 39 #define DPRINTFN(n, x) do { if (umbgdebug > (n)) printf x; } while (0) 40 int umbgdebug = 0; 41 #else 42 #define DPRINTFN(n, x) 43 #endif 44 #define DPRINTF(x) DPRINTFN(0, x) 45 46 #ifdef UMBG_DEBUG 47 #define TRUSTTIME ((long) 60) 48 #else 49 #define TRUSTTIME ((long) 12 * 60 * 60) /* degrade OK > WARN > CRIT */ 50 #endif 51 52 struct umbg_softc { 53 struct device sc_dev; /* base device */ 54 usbd_device_handle sc_udev; /* USB device */ 55 usbd_interface_handle sc_iface; /* data interface */ 56 u_char sc_dying; /* disconnecting */ 57 58 int sc_bulkin_no; 59 usbd_pipe_handle sc_bulkin_pipe; 60 int sc_bulkout_no; 61 usbd_pipe_handle sc_bulkout_pipe; 62 63 struct timeout sc_to; /* get time from device */ 64 struct usb_task sc_task; 65 66 struct timeout sc_it_to; /* invalidate sensor */ 67 68 usb_device_request_t sc_req; 69 70 struct ksensor sc_timedelta; /* timedelta */ 71 struct ksensor sc_signal; /* signal quality and status */ 72 struct ksensordev sc_sensordev; 73 }; 74 75 struct mbg_time { 76 u_int8_t hundreds; 77 u_int8_t sec; 78 u_int8_t min; 79 u_int8_t hour; 80 u_int8_t mday; 81 u_int8_t wday; /* 1 (monday) - 7 (sunday) */ 82 u_int8_t mon; 83 u_int8_t year; /* 0 - 99 */ 84 u_int8_t status; 85 u_int8_t signal; 86 int8_t utc_off; 87 }; 88 89 struct mbg_time_hr { 90 u_int32_t sec; /* always UTC */ 91 u_int32_t frac; /* fractions of second */ 92 int32_t utc_off; /* informal only, in seconds */ 93 u_int16_t status; 94 u_int8_t signal; 95 }; 96 97 /* mbg_time.status bits */ 98 #define MBG_FREERUN 0x01 /* clock running on xtal */ 99 #define MBG_DST_ENA 0x02 /* DST enabled */ 100 #define MBG_SYNC 0x04 /* clock synced at least once */ 101 #define MBG_DST_CHG 0x08 /* DST change announcement */ 102 #define MBG_UTC 0x10 /* special UTC firmware is installed */ 103 #define MBG_LEAP 0x20 /* announcement of a leap second */ 104 #define MBG_IFTM 0x40 /* current time was set from host */ 105 #define MBG_INVALID 0x80 /* time invalid, batt. was disconn. */ 106 107 /* commands */ 108 #define MBG_GET_TIME 0x00 109 #define MBG_GET_SYNC_TIME 0x02 110 #define MBG_GET_TIME_HR 0x03 111 #define MBG_SET_TIME 0x10 112 #define MBG_GET_TZCODE 0x32 113 #define MBG_SET_TZCODE 0x33 114 #define MBG_GET_FW_ID_1 0x40 115 #define MBG_GET_FW_ID_2 0x41 116 #define MBG_GET_SERNUM 0x42 117 118 /* timezone codes (for MBG_{GET|SET}_TZCODE) */ 119 #define MBG_TZCODE_CET_CEST 0x00 120 #define MBG_TZCODE_CET 0x01 121 #define MBG_TZCODE_UTC 0x02 122 #define MBG_TZCODE_EET_EEST 0x03 123 124 /* misc. constants */ 125 #define MBG_FIFO_LEN 16 126 #define MBG_ID_LEN (2 * MBG_FIFO_LEN + 1) 127 #define MBG_BUSY 0x01 128 #define MBG_SIG_BIAS 55 129 #define MBG_SIG_MAX 68 130 #define NSECPERSEC 1000000000LL /* nanoseconds per second */ 131 #define HRDIVISOR 0x100000000LL /* for hi-res timestamp */ 132 133 static int t_wait, t_trust; 134 135 void umbg_intr(void *); 136 void umbg_it_intr(void *); 137 138 int umbg_match(struct device *, void *, void *); 139 void umbg_attach(struct device *, struct device *, void *); 140 int umbg_detach(struct device *, int); 141 int umbg_activate(struct device *, enum devact); 142 143 void umbg_task(void *); 144 145 int umbg_read(struct umbg_softc *, u_int8_t cmd, char *buf, size_t len, 146 struct timespec *tstamp); 147 148 struct cfdriver umbg_cd = { 149 NULL, "umbg", DV_DULL 150 }; 151 152 const struct cfattach umbg_ca = { 153 sizeof(struct umbg_softc), 154 umbg_match, 155 umbg_attach, 156 umbg_detach, 157 umbg_activate 158 }; 159 160 int 161 umbg_match(struct device *parent, void *match, void *aux) 162 { 163 struct usb_attach_arg *uaa = aux; 164 165 if (uaa->iface != NULL) 166 return UMATCH_NONE; 167 168 return uaa->vendor == USB_VENDOR_MEINBERG && 169 uaa->product == USB_PRODUCT_MEINBERG_USB5131 ? 170 UMATCH_VENDOR_PRODUCT : UMATCH_NONE; 171 } 172 173 void 174 umbg_attach(struct device *parent, struct device *self, void *aux) 175 { 176 struct umbg_softc *sc = (struct umbg_softc *)self; 177 struct usb_attach_arg *uaa = aux; 178 usbd_device_handle dev = uaa->device; 179 usbd_interface_handle iface = uaa->iface; 180 struct mbg_time tframe; 181 usb_endpoint_descriptor_t *ed; 182 usbd_status err; 183 int signal; 184 #ifdef UMBG_DEBUG 185 char fw_id[MBG_ID_LEN]; 186 #endif 187 sc->sc_udev = dev; 188 189 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname, 190 sizeof(sc->sc_sensordev.xname)); 191 192 sc->sc_timedelta.type = SENSOR_TIMEDELTA; 193 sc->sc_timedelta.status = SENSOR_S_UNKNOWN; 194 strlcpy(sc->sc_timedelta.desc, "USB5131", 195 sizeof(sc->sc_timedelta.desc)); 196 sensor_attach(&sc->sc_sensordev, &sc->sc_timedelta); 197 198 sc->sc_signal.type = SENSOR_PERCENT; 199 strlcpy(sc->sc_signal.desc, "Signal", sizeof(sc->sc_signal.desc)); 200 sensor_attach(&sc->sc_sensordev, &sc->sc_signal); 201 sensordev_install(&sc->sc_sensordev); 202 203 usb_init_task(&sc->sc_task, umbg_task, sc); 204 timeout_set(&sc->sc_to, umbg_intr, sc); 205 timeout_set(&sc->sc_it_to, umbg_it_intr, sc); 206 207 if ((err = usbd_set_config_index(dev, 0, 1))) { 208 printf("%s: failed to set configuration, err=%s\n", 209 sc->sc_dev.dv_xname, usbd_errstr(err)); 210 goto fishy; 211 } 212 213 if ((err = usbd_device2interface_handle(dev, 0, &iface))) { 214 printf("%s: failed to get interface, err=%s\n", 215 sc->sc_dev.dv_xname, usbd_errstr(err)); 216 goto fishy; 217 } 218 219 ed = usbd_interface2endpoint_descriptor(iface, 0); 220 sc->sc_bulkin_no = ed->bEndpointAddress; 221 ed = usbd_interface2endpoint_descriptor(iface, 1); 222 sc->sc_bulkout_no = ed->bEndpointAddress; 223 224 sc->sc_iface = iface; 225 226 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkin_no, 227 USBD_EXCLUSIVE_USE, &sc->sc_bulkin_pipe); 228 if (err) { 229 printf("%s: open rx pipe failed: %s\n", sc->sc_dev.dv_xname, 230 usbd_errstr(err)); 231 goto fishy; 232 } 233 234 err = usbd_open_pipe(sc->sc_iface, sc->sc_bulkout_no, 235 USBD_EXCLUSIVE_USE, &sc->sc_bulkout_pipe); 236 if (err) { 237 printf("%s: open tx pipe failed: %s\n", sc->sc_dev.dv_xname, 238 usbd_errstr(err)); 239 goto fishy; 240 } 241 242 printf("%s: ", sc->sc_dev.dv_xname); 243 if (umbg_read(sc, MBG_GET_TIME, (char *)&tframe, 244 sizeof(struct mbg_time), NULL)) { 245 sc->sc_signal.status = SENSOR_S_CRIT; 246 printf("unknown status"); 247 } else { 248 sc->sc_signal.status = SENSOR_S_OK; 249 signal = tframe.signal - MBG_SIG_BIAS; 250 if (signal < 0) 251 signal = 0; 252 else if (signal > MBG_SIG_MAX) 253 signal = MBG_SIG_MAX; 254 sc->sc_signal.value = signal; 255 256 if (tframe.status & MBG_SYNC) 257 printf("synchronized"); 258 else 259 printf("not synchronized"); 260 if (tframe.status & MBG_FREERUN) { 261 sc->sc_signal.status = SENSOR_S_WARN; 262 printf(", freerun"); 263 } 264 if (tframe.status & MBG_IFTM) 265 printf(", time set from host"); 266 } 267 #ifdef UMBG_DEBUG 268 if (umbg_read(sc, MBG_GET_FW_ID_1, fw_id, MBG_FIFO_LEN, NULL) || 269 umbg_read(sc, MBG_GET_FW_ID_2, &fw_id[MBG_FIFO_LEN], MBG_FIFO_LEN, 270 NULL)) 271 printf(", firmware unknown"); 272 else { 273 fw_id[MBG_ID_LEN - 1] = '\0'; 274 printf(", firmware %s", fw_id); 275 } 276 #endif 277 printf("\n"); 278 279 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, 280 &sc->sc_dev); 281 282 t_wait = 5; 283 284 t_trust = TRUSTTIME; 285 286 usb_add_task(sc->sc_udev, &sc->sc_task); 287 return; 288 289 fishy: 290 sc->sc_dying = 1; 291 } 292 293 int 294 umbg_detach(struct device *self, int flags) 295 { 296 struct umbg_softc *sc = (struct umbg_softc *)self; 297 usbd_status err; 298 299 sc->sc_dying = 1; 300 301 timeout_del(&sc->sc_to); 302 timeout_del(&sc->sc_it_to); 303 304 if (sc->sc_bulkin_pipe != NULL) { 305 err = usbd_abort_pipe(sc->sc_bulkin_pipe); 306 if (err) 307 printf("%s: abort rx pipe failed: %s\n", 308 sc->sc_dev.dv_xname, usbd_errstr(err)); 309 err = usbd_close_pipe(sc->sc_bulkin_pipe); 310 if (err) 311 printf("%s: close rx pipe failed: %s\n", 312 sc->sc_dev.dv_xname, usbd_errstr(err)); 313 sc->sc_bulkin_pipe = NULL; 314 } 315 if (sc->sc_bulkout_pipe != NULL) { 316 err = usbd_abort_pipe(sc->sc_bulkout_pipe); 317 if (err) 318 printf("%s: abort tx pipe failed: %s\n", 319 sc->sc_dev.dv_xname, usbd_errstr(err)); 320 err = usbd_close_pipe(sc->sc_bulkout_pipe); 321 if (err) 322 printf("%s: close tx pipe failed: %s\n", 323 sc->sc_dev.dv_xname, usbd_errstr(err)); 324 sc->sc_bulkout_pipe = NULL; 325 } 326 327 usb_rem_task(sc->sc_udev, &sc->sc_task); 328 329 /* Unregister the clock with the kernel */ 330 sensordev_deinstall(&sc->sc_sensordev); 331 332 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev, &sc->sc_dev); 333 return 0; 334 } 335 336 void 337 umbg_intr(void *xsc) 338 { 339 struct umbg_softc *sc = xsc; 340 usb_add_task(sc->sc_udev, &sc->sc_task); 341 } 342 343 /* umbg_task_hr() read a high resolution timestamp from the device. */ 344 void 345 umbg_task(void *arg) 346 { 347 struct umbg_softc *sc = (struct umbg_softc *)arg; 348 struct mbg_time_hr tframe; 349 struct timespec tstamp; 350 int64_t tlocal, trecv; 351 int signal; 352 353 if (sc->sc_dying) 354 return; 355 356 if (umbg_read(sc, MBG_GET_TIME_HR, (char *)&tframe, sizeof(tframe), 357 &tstamp)) { 358 sc->sc_signal.status = SENSOR_S_CRIT; 359 goto bail_out; 360 } 361 if (tframe.status & MBG_INVALID) { 362 sc->sc_signal.status = SENSOR_S_CRIT; 363 goto bail_out; 364 } 365 366 tlocal = tstamp.tv_sec * NSECPERSEC + tstamp.tv_nsec; 367 trecv = letoh32(tframe.sec) * NSECPERSEC + 368 (letoh32(tframe.frac) * NSECPERSEC >> 32); 369 370 sc->sc_timedelta.value = tlocal - trecv; 371 if (sc->sc_timedelta.status == SENSOR_S_UNKNOWN || 372 !(letoh16(tframe.status) & MBG_FREERUN)) { 373 sc->sc_timedelta.status = SENSOR_S_OK; 374 timeout_add_sec(&sc->sc_it_to, t_trust); 375 } 376 377 sc->sc_timedelta.tv.tv_sec = tstamp.tv_sec; 378 sc->sc_timedelta.tv.tv_usec = tstamp.tv_nsec / 1000; 379 380 signal = tframe.signal - MBG_SIG_BIAS; 381 if (signal < 0) 382 signal = 0; 383 else if (signal > MBG_SIG_MAX) 384 signal = MBG_SIG_MAX; 385 386 sc->sc_signal.value = signal * 100000 / MBG_SIG_MAX; 387 sc->sc_signal.status = letoh16(tframe.status) & MBG_FREERUN ? 388 SENSOR_S_WARN : SENSOR_S_OK; 389 sc->sc_signal.tv.tv_sec = sc->sc_timedelta.tv.tv_sec; 390 sc->sc_signal.tv.tv_usec = sc->sc_timedelta.tv.tv_usec; 391 392 bail_out: 393 timeout_add_sec(&sc->sc_to, t_wait); 394 395 } 396 397 /* send a command and read back results */ 398 int 399 umbg_read(struct umbg_softc *sc, u_int8_t cmd, char *buf, size_t len, 400 struct timespec *tstamp) 401 { 402 usbd_status err; 403 usbd_xfer_handle xfer; 404 405 xfer = usbd_alloc_xfer(sc->sc_udev); 406 if (xfer == NULL) { 407 DPRINTF(("%s: alloc xfer failed\n", sc->sc_dev.dv_xname)); 408 return -1; 409 } 410 411 usbd_setup_xfer(xfer, sc->sc_bulkout_pipe, NULL, &cmd, sizeof(cmd), 412 USBD_SHORT_XFER_OK, USBD_DEFAULT_TIMEOUT, NULL); 413 if (tstamp) 414 nanotime(tstamp); 415 err = usbd_sync_transfer(xfer); 416 if (err) { 417 DPRINTF(("%s: sending of command failed: %s\n", 418 sc->sc_dev.dv_xname, usbd_errstr(err))); 419 usbd_free_xfer(xfer); 420 return -1; 421 } 422 423 usbd_setup_xfer(xfer, sc->sc_bulkin_pipe, NULL, buf, len, 424 USBD_SHORT_XFER_OK, USBD_DEFAULT_TIMEOUT, NULL); 425 426 err = usbd_sync_transfer(xfer); 427 usbd_free_xfer(xfer); 428 if (err) { 429 DPRINTF(("%s: reading data failed: %s\n", 430 sc->sc_dev.dv_xname, usbd_errstr(err))); 431 return -1; 432 } 433 return 0; 434 } 435 436 void 437 umbg_it_intr(void *xsc) 438 { 439 struct umbg_softc *sc = xsc; 440 441 if (sc->sc_dying) 442 return; 443 444 if (sc->sc_timedelta.status == SENSOR_S_OK) { 445 sc->sc_timedelta.status = SENSOR_S_WARN; 446 /* 447 * further degrade in TRUSTTIME seconds if the clocks remains 448 * free running. 449 */ 450 timeout_add_sec(&sc->sc_it_to, t_trust); 451 } else 452 sc->sc_timedelta.status = SENSOR_S_CRIT; 453 } 454 455 int 456 umbg_activate(struct device *self, enum devact act) 457 { 458 struct umbg_softc *sc = (struct umbg_softc *)self; 459 460 switch (act) { 461 case DVACT_ACTIVATE: 462 break; 463 case DVACT_DEACTIVATE: 464 sc->sc_dying = 1; 465 break; 466 } 467 return 0; 468 } 469