gpiodcf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /* $OpenBSD: gpiodcf.c,v 1.5 2015/06/07 20:11:52 claudio Exp $ */
  2. /*
  3. * Copyright (c) 2008 Marc Balmer <mbalmer@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/param.h>
  18. #include <sys/systm.h>
  19. #include <sys/kernel.h>
  20. #include <sys/conf.h>
  21. #include <sys/file.h>
  22. #include <sys/select.h>
  23. #include <sys/proc.h>
  24. #include <sys/vnode.h>
  25. #include <sys/device.h>
  26. #include <sys/poll.h>
  27. #include <sys/time.h>
  28. #include <sys/sensors.h>
  29. #include <sys/gpio.h>
  30. #include <dev/gpio/gpiovar.h>
  31. #ifdef GPIODCF_DEBUG
  32. #define DPRINTFN(n, x) do { if (gpiodcfdebug > (n)) printf x; } while (0)
  33. int gpiodcfdebug = 0;
  34. #else
  35. #define DPRINTFN(n, x)
  36. #endif
  37. #define DPRINTF(x) DPRINTFN(0, x)
  38. #define DPERIOD1 ((long) 5 * 60) /* degrade OK -> WARN */
  39. #define DPERIOD2 ((long) 15 * 60) /* degrade WARN -> CRIT */
  40. /* max. skew of received time diff vs. measured time diff in percent. */
  41. #define MAX_SKEW 5
  42. #define GPIODCF_NPINS 1
  43. #define GPIODCF_PIN_DATA 0
  44. struct gpiodcf_softc {
  45. struct device sc_dev; /* base device */
  46. void *sc_gpio;
  47. struct gpio_pinmap sc_map;
  48. int __map[GPIODCF_NPINS];
  49. u_char sc_dying; /* disconnecting */
  50. int sc_data;
  51. struct timeout sc_to;
  52. struct timeout sc_bv_to; /* bit-value detect */
  53. struct timeout sc_db_to; /* debounce */
  54. struct timeout sc_mg_to; /* minute-gap detect */
  55. struct timeout sc_sl_to; /* signal-loss detect */
  56. struct timeout sc_it_to; /* invalidate time */
  57. int sc_sync; /* 1 during sync */
  58. u_int64_t sc_mask; /* 64 bit mask */
  59. u_int64_t sc_tbits; /* Time bits */
  60. int sc_minute;
  61. int sc_level;
  62. time_t sc_last_mg;
  63. time_t sc_current; /* current time */
  64. time_t sc_next; /* time to become valid next */
  65. time_t sc_last;
  66. int sc_nrecv; /* consecutive valid times */
  67. struct timeval sc_last_tv; /* uptime of last valid time */
  68. struct ksensor sc_sensor;
  69. #ifdef GPIODCF_DEBUG
  70. struct ksensor sc_skew; /* recv vs local skew */
  71. #endif
  72. struct ksensordev sc_sensordev;
  73. };
  74. /*
  75. * timeouts being used in hz:
  76. * t_bv bit value detection (150ms)
  77. * t_sync sync (950ms)
  78. * t_mg minute gap detection (1500ms)
  79. * t_mgsync resync after a minute gap (450ms)
  80. * t_sl detect signal loss (3sec)
  81. * t_wait wait (5sec)
  82. * t_warn degrade sensor status to warning (5min)
  83. * t_crit degrade sensor status to critical (15min)
  84. */
  85. static int t_bv, t_sync, t_mg, t_sl, t_mgsync, t_wait, t_warn, t_crit;
  86. void gpiodcf_intr(void *);
  87. void gpiodcf_probe(void *);
  88. void gpiodcf_bv_probe(void *);
  89. void gpiodcf_mg_probe(void *);
  90. void gpiodcf_sl_probe(void *);
  91. void gpiodcf_invalidate(void *);
  92. int gpiodcf_match(struct device *, void *, void *);
  93. void gpiodcf_attach(struct device *, struct device *, void *);
  94. int gpiodcf_detach(struct device *, int);
  95. int gpiodcf_activate(struct device *, int);
  96. int gpiodcf_signal(struct gpiodcf_softc *);
  97. struct cfdriver gpiodcf_cd = {
  98. NULL, "gpiodcf", DV_DULL
  99. };
  100. const struct cfattach gpiodcf_ca = {
  101. sizeof(struct gpiodcf_softc),
  102. gpiodcf_match,
  103. gpiodcf_attach,
  104. gpiodcf_detach,
  105. gpiodcf_activate
  106. };
  107. int
  108. gpiodcf_match(struct device *parent, void *match, void *aux)
  109. {
  110. struct cfdata *cf = match;
  111. struct gpio_attach_args *ga = aux;
  112. if (ga->ga_offset == -1)
  113. return 0;
  114. return (strcmp(cf->cf_driver->cd_name, "gpiodcf") == 0);
  115. }
  116. void
  117. gpiodcf_attach(struct device *parent, struct device *self, void *aux)
  118. {
  119. struct gpiodcf_softc *sc = (struct gpiodcf_softc *)self;
  120. struct gpio_attach_args *ga = aux;
  121. struct timeval t;
  122. int caps;
  123. if (gpio_npins(ga->ga_mask) != GPIODCF_NPINS) {
  124. printf(": invalid pin mask\n");
  125. return;
  126. }
  127. sc->sc_gpio = ga->ga_gpio;
  128. sc->sc_map.pm_map = sc->__map;
  129. if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
  130. &sc->sc_map)) {
  131. printf(": can't map pins\n");
  132. return;
  133. }
  134. caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIODCF_PIN_DATA);
  135. if (!(caps & GPIO_PIN_INPUT)) {
  136. printf(": data pin is unable to receive input\n");
  137. goto fishy;
  138. }
  139. printf(": DATA[%d]", sc->sc_map.pm_map[GPIODCF_PIN_DATA]);
  140. sc->sc_data = GPIO_PIN_INPUT;
  141. gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIODCF_PIN_DATA, sc->sc_data);
  142. printf("\n");
  143. strlcpy(sc->sc_sensor.desc, "DCF77", sizeof(sc->sc_sensor.desc));
  144. timeout_set(&sc->sc_to, gpiodcf_probe, sc);
  145. timeout_set(&sc->sc_bv_to, gpiodcf_bv_probe, sc);
  146. timeout_set(&sc->sc_mg_to, gpiodcf_mg_probe, sc);
  147. timeout_set(&sc->sc_sl_to, gpiodcf_sl_probe, sc);
  148. timeout_set(&sc->sc_it_to, gpiodcf_invalidate, sc);
  149. strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
  150. sizeof(sc->sc_sensordev.xname));
  151. sc->sc_sensor.type = SENSOR_TIMEDELTA;
  152. sc->sc_sensor.status = SENSOR_S_UNKNOWN;
  153. sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
  154. #ifdef GPIODCF_DEBUG
  155. sc->sc_skew.type = SENSOR_TIMEDELTA;
  156. sc->sc_skew.status = SENSOR_S_UNKNOWN;
  157. strlcpy(sc->sc_skew.desc, "local clock skew",
  158. sizeof(sc->sc_skew.desc));
  159. sensor_attach(&sc->sc_sensordev, &sc->sc_skew);
  160. #endif
  161. sensordev_install(&sc->sc_sensordev);
  162. sc->sc_level = 0;
  163. sc->sc_minute = 0;
  164. sc->sc_last_mg = 0L;
  165. sc->sc_sync = 1;
  166. sc->sc_current = 0L;
  167. sc->sc_next = 0L;
  168. sc->sc_nrecv = 0;
  169. sc->sc_last = 0L;
  170. sc->sc_last_tv.tv_sec = 0L;
  171. /* convert timevals to hz */
  172. t.tv_sec = 0L;
  173. t.tv_usec = 150000L;
  174. t_bv = tvtohz(&t);
  175. t.tv_usec = 450000L;
  176. t_mgsync = tvtohz(&t);
  177. t.tv_usec = 950000L;
  178. t_sync = tvtohz(&t);
  179. t.tv_sec = 1L;
  180. t.tv_usec = 500000L;
  181. t_mg = tvtohz(&t);
  182. t.tv_sec = 3L;
  183. t.tv_usec = 0L;
  184. t_sl = tvtohz(&t);
  185. t.tv_sec = 5L;
  186. t_wait = tvtohz(&t);
  187. t.tv_sec = DPERIOD1;
  188. t_warn = tvtohz(&t);
  189. t.tv_sec = DPERIOD2;
  190. t_crit = tvtohz(&t);
  191. /* Give the receiver some slack to stabilize */
  192. timeout_add(&sc->sc_to, t_wait);
  193. /* Detect signal loss */
  194. timeout_add(&sc->sc_sl_to, t_wait + t_sl);
  195. DPRINTF(("synchronizing\n"));
  196. return;
  197. fishy:
  198. DPRINTF(("gpiodcf_attach failed\n"));
  199. gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
  200. sc->sc_dying = 1;
  201. }
  202. int
  203. gpiodcf_detach(struct device *self, int flags)
  204. {
  205. struct gpiodcf_softc *sc = (struct gpiodcf_softc *)self;
  206. sc->sc_dying = 1;
  207. timeout_del(&sc->sc_to);
  208. timeout_del(&sc->sc_bv_to);
  209. timeout_del(&sc->sc_mg_to);
  210. timeout_del(&sc->sc_sl_to);
  211. timeout_del(&sc->sc_it_to);
  212. /* Unregister the clock with the kernel */
  213. sensordev_deinstall(&sc->sc_sensordev);
  214. /* Finally unmap the GPIO pin */
  215. gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
  216. return 0;
  217. }
  218. /*
  219. * return 1 during high-power-, 0 during low-power-emission
  220. * If bit 0 is set, the transmitter emits at full power.
  221. * During the low-power emission we decode a zero bit.
  222. */
  223. int
  224. gpiodcf_signal(struct gpiodcf_softc *sc)
  225. {
  226. return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIODCF_PIN_DATA) ==
  227. GPIO_PIN_HIGH ? 1 : 0);
  228. }
  229. /* gpiodcf_probe runs in a process context. */
  230. void
  231. gpiodcf_probe(void *xsc)
  232. {
  233. struct gpiodcf_softc *sc = xsc;
  234. struct timespec now;
  235. int data;
  236. if (sc->sc_dying)
  237. return;
  238. data = gpiodcf_signal(sc);
  239. if (data == -1)
  240. return;
  241. if (data) {
  242. sc->sc_level = 1;
  243. timeout_add(&sc->sc_to, 1);
  244. return;
  245. }
  246. if (sc->sc_level == 0)
  247. return;
  248. /* the beginning of a second */
  249. sc->sc_level = 0;
  250. if (sc->sc_minute == 1) {
  251. if (sc->sc_sync) {
  252. DPRINTF(("start collecting bits\n"));
  253. sc->sc_sync = 0;
  254. } else {
  255. /* provide the timedelta */
  256. microtime(&sc->sc_sensor.tv);
  257. nanotime(&now);
  258. sc->sc_current = sc->sc_next;
  259. sc->sc_sensor.value = (int64_t)(now.tv_sec -
  260. sc->sc_current) * 1000000000LL + now.tv_nsec;
  261. sc->sc_sensor.status = SENSOR_S_OK;
  262. /*
  263. * if no valid time information is received
  264. * during the next 5 minutes, the sensor state
  265. * will be degraded to SENSOR_S_WARN
  266. */
  267. timeout_add(&sc->sc_it_to, t_warn);
  268. }
  269. sc->sc_minute = 0;
  270. }
  271. timeout_add(&sc->sc_to, t_sync); /* resync in 950 ms */
  272. /* no clock and bit detection during sync */
  273. if (!sc->sc_sync) {
  274. /* detect bit value */
  275. timeout_add(&sc->sc_bv_to, t_bv);
  276. }
  277. timeout_add(&sc->sc_mg_to, t_mg); /* detect minute gap */
  278. timeout_add(&sc->sc_sl_to, t_sl); /* detect signal loss */
  279. }
  280. /* detect the bit value */
  281. void
  282. gpiodcf_bv_probe(void *xsc)
  283. {
  284. struct gpiodcf_softc *sc = xsc;
  285. int data;
  286. if (sc->sc_dying)
  287. return;
  288. data = gpiodcf_signal(sc);
  289. if (data == -1) {
  290. DPRINTF(("bit detection failed\n"));
  291. return;
  292. }
  293. DPRINTFN(1, (data ? "0" : "1"));
  294. if (!(data))
  295. sc->sc_tbits |= sc->sc_mask;
  296. sc->sc_mask <<= 1;
  297. }
  298. /* detect the minute gap */
  299. void
  300. gpiodcf_mg_probe(void *xsc)
  301. {
  302. struct gpiodcf_softc *sc = xsc;
  303. struct clock_ymdhms ymdhm;
  304. struct timeval monotime;
  305. int tdiff_recv, tdiff_local;
  306. int skew;
  307. int minute_bits, hour_bits, day_bits;
  308. int month_bits, year_bits, wday;
  309. int p1, p2, p3;
  310. int p1_bit, p2_bit, p3_bit;
  311. int r_bit, a1_bit, a2_bit, z1_bit, z2_bit;
  312. int s_bit, m_bit;
  313. u_int32_t parity = 0x6996;
  314. if (sc->sc_sync) {
  315. sc->sc_minute = 1;
  316. goto cleanbits;
  317. }
  318. if (time_second - sc->sc_last_mg < 57) {
  319. DPRINTF(("\nunexpected gap, resync\n"));
  320. sc->sc_sync = sc->sc_minute = 1;
  321. goto cleanbits;
  322. }
  323. /* extract bits w/o parity */
  324. m_bit = sc->sc_tbits & 1;
  325. r_bit = sc->sc_tbits >> 15 & 1;
  326. a1_bit = sc->sc_tbits >> 16 & 1;
  327. z1_bit = sc->sc_tbits >> 17 & 1;
  328. z2_bit = sc->sc_tbits >> 18 & 1;
  329. a2_bit = sc->sc_tbits >> 19 & 1;
  330. s_bit = sc->sc_tbits >> 20 & 1;
  331. p1_bit = sc->sc_tbits >> 28 & 1;
  332. p2_bit = sc->sc_tbits >> 35 & 1;
  333. p3_bit = sc->sc_tbits >> 58 & 1;
  334. minute_bits = sc->sc_tbits >> 21 & 0x7f;
  335. hour_bits = sc->sc_tbits >> 29 & 0x3f;
  336. day_bits = sc->sc_tbits >> 36 & 0x3f;
  337. wday = (sc->sc_tbits >> 42) & 0x07;
  338. month_bits = sc->sc_tbits >> 45 & 0x1f;
  339. year_bits = sc->sc_tbits >> 50 & 0xff;
  340. /* validate time information */
  341. p1 = (parity >> (minute_bits & 0x0f) & 1) ^
  342. (parity >> (minute_bits >> 4) & 1);
  343. p2 = (parity >> (hour_bits & 0x0f) & 1) ^
  344. (parity >> (hour_bits >> 4) & 1);
  345. p3 = (parity >> (day_bits & 0x0f) & 1) ^
  346. (parity >> (day_bits >> 4) & 1) ^
  347. ((parity >> wday) & 1) ^ (parity >> (month_bits & 0x0f) & 1) ^
  348. (parity >> (month_bits >> 4) & 1) ^
  349. (parity >> (year_bits & 0x0f) & 1) ^
  350. (parity >> (year_bits >> 4) & 1);
  351. if (m_bit == 0 && s_bit == 1 && p1 == p1_bit && p2 == p2_bit &&
  352. p3 == p3_bit && (z1_bit ^ z2_bit)) {
  353. /* Decode time */
  354. if ((ymdhm.dt_year = 2000 + FROMBCD(year_bits)) > 2037) {
  355. DPRINTF(("year out of range, resync\n"));
  356. sc->sc_sync = 1;
  357. goto cleanbits;
  358. }
  359. ymdhm.dt_min = FROMBCD(minute_bits);
  360. ymdhm.dt_hour = FROMBCD(hour_bits);
  361. ymdhm.dt_day = FROMBCD(day_bits);
  362. ymdhm.dt_mon = FROMBCD(month_bits);
  363. ymdhm.dt_sec = 0;
  364. sc->sc_next = clock_ymdhms_to_secs(&ymdhm);
  365. getmicrouptime(&monotime);
  366. /* convert to coordinated universal time */
  367. sc->sc_next -= z1_bit ? 7200 : 3600;
  368. DPRINTF(("\n%02d.%02d.%04d %02d:%02d:00 %s",
  369. ymdhm.dt_day, ymdhm.dt_mon, ymdhm.dt_year,
  370. ymdhm.dt_hour, ymdhm.dt_min, z1_bit ? "CEST" : "CET"));
  371. DPRINTF((r_bit ? ", call bit" : ""));
  372. DPRINTF((a1_bit ? ", dst chg ann." : ""));
  373. DPRINTF((a2_bit ? ", leap sec ann." : ""));
  374. DPRINTF(("\n"));
  375. if (sc->sc_last) {
  376. tdiff_recv = sc->sc_next - sc->sc_last;
  377. tdiff_local = monotime.tv_sec - sc->sc_last_tv.tv_sec;
  378. skew = abs(tdiff_local - tdiff_recv);
  379. #ifdef GPIODCF_DEBUG
  380. if (sc->sc_skew.status == SENSOR_S_UNKNOWN)
  381. sc->sc_skew.status = SENSOR_S_CRIT;
  382. sc->sc_skew.value = skew * 1000000000LL;
  383. getmicrotime(&sc->sc_skew.tv);
  384. #endif
  385. DPRINTF(("local = %d, recv = %d, skew = %d\n",
  386. tdiff_local, tdiff_recv, skew));
  387. if (skew && skew * 100LL / tdiff_local > MAX_SKEW) {
  388. DPRINTF(("skew out of tolerated range\n"));
  389. goto cleanbits;
  390. } else {
  391. if (sc->sc_nrecv < 2) {
  392. sc->sc_nrecv++;
  393. DPRINTF(("got frame %d\n",
  394. sc->sc_nrecv));
  395. } else {
  396. DPRINTF(("data is valid\n"));
  397. sc->sc_minute = 1;
  398. }
  399. }
  400. } else {
  401. DPRINTF(("received the first frame\n"));
  402. sc->sc_nrecv = 1;
  403. }
  404. /* record the time received and when it was received */
  405. sc->sc_last = sc->sc_next;
  406. sc->sc_last_tv.tv_sec = monotime.tv_sec;
  407. } else {
  408. DPRINTF(("\nparity error, resync\n"));
  409. sc->sc_sync = sc->sc_minute = 1;
  410. }
  411. cleanbits:
  412. timeout_add(&sc->sc_to, t_mgsync); /* re-sync in 450 ms */
  413. sc->sc_last_mg = time_second;
  414. sc->sc_tbits = 0LL;
  415. sc->sc_mask = 1LL;
  416. }
  417. /* detect signal loss */
  418. void
  419. gpiodcf_sl_probe(void *xsc)
  420. {
  421. struct gpiodcf_softc *sc = xsc;
  422. if (sc->sc_dying)
  423. return;
  424. DPRINTF(("no signal\n"));
  425. sc->sc_sync = 1;
  426. timeout_add(&sc->sc_to, t_wait);
  427. timeout_add(&sc->sc_sl_to, t_wait + t_sl);
  428. }
  429. /* invalidate timedelta (called in an interrupt context) */
  430. void
  431. gpiodcf_invalidate(void *xsc)
  432. {
  433. struct gpiodcf_softc *sc = xsc;
  434. if (sc->sc_dying)
  435. return;
  436. if (sc->sc_sensor.status == SENSOR_S_OK) {
  437. sc->sc_sensor.status = SENSOR_S_WARN;
  438. /*
  439. * further degrade in 15 minutes if we dont receive any new
  440. * time information
  441. */
  442. timeout_add(&sc->sc_it_to, t_crit);
  443. } else {
  444. sc->sc_sensor.status = SENSOR_S_CRIT;
  445. sc->sc_nrecv = 0;
  446. }
  447. }
  448. int
  449. gpiodcf_activate(struct device *self, int act)
  450. {
  451. struct gpiodcf_softc *sc = (struct gpiodcf_softc *)self;
  452. switch (act) {
  453. case DVACT_DEACTIVATE:
  454. sc->sc_dying = 1;
  455. break;
  456. }
  457. return 0;
  458. }