devfakertc.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * raspberry pi doesn't have a realtime clock
  11. * fake a crude approximation from the kernel build time
  12. */
  13. #include "u.h"
  14. #include "../port/lib.h"
  15. #include "mem.h"
  16. #include "dat.h"
  17. #include "fns.h"
  18. #include "../port/error.h"
  19. enum{
  20. Qdir = 0,
  21. Qrtc,
  22. };
  23. Dirtab rtcdir[]={
  24. ".", {Qdir, 0, QTDIR}, 0, 0555,
  25. "rtc", {Qrtc, 0}, 0, 0664,
  26. };
  27. extern ulong kerndate;
  28. static ulong rtcsecs;
  29. static void
  30. rtctick(void)
  31. {
  32. rtcsecs++;
  33. }
  34. static void
  35. rtcinit(void)
  36. {
  37. rtcsecs = kerndate;
  38. addclock0link(rtctick, 1000);
  39. }
  40. static long
  41. rtcread(Chan *c, void *a, long n, vlong offset)
  42. {
  43. if(c->qid.type & QTDIR)
  44. return devdirread(c, a, n, rtcdir, nelem(rtcdir), devgen);
  45. switch((ulong)c->qid.path){
  46. case Qrtc:
  47. return readnum((ulong)offset, a, n, rtcsecs, 12);
  48. }
  49. error(Ebadarg);
  50. return 0;
  51. }
  52. static long
  53. rtcwrite(Chan*c, void *a, long n, vlong)
  54. {
  55. char b[13];
  56. ulong i;
  57. switch((ulong)c->qid.path){
  58. case Qrtc:
  59. if(n >= sizeof(b))
  60. error(Ebadarg);
  61. strncpy(b, (char*)a, n);
  62. i = strtol(b, 0, 0);
  63. if(i <= 0)
  64. error(Ebadarg);
  65. rtcsecs = i;
  66. return n;
  67. }
  68. error(Eperm);
  69. return 0;
  70. }
  71. static Chan*
  72. rtcattach(char* spec)
  73. {
  74. return devattach('r', spec);
  75. }
  76. static Walkqid*
  77. rtcwalk(Chan* c, Chan *nc, char** name, int nname)
  78. {
  79. return devwalk(c, nc, name, nname, rtcdir, nelem(rtcdir), devgen);
  80. }
  81. static int
  82. rtcstat(Chan* c, uchar* dp, int n)
  83. {
  84. return devstat(c, dp, n, rtcdir, nelem(rtcdir), devgen);
  85. }
  86. static Chan*
  87. rtcopen(Chan* c, int omode)
  88. {
  89. return devopen(c, omode, rtcdir, nelem(rtcdir), devgen);
  90. }
  91. static void
  92. rtcclose(Chan*)
  93. {
  94. }
  95. Dev fakertcdevtab = {
  96. 'r',
  97. "rtc",
  98. devreset,
  99. rtcinit,
  100. devshutdown,
  101. rtcattach,
  102. rtcwalk,
  103. rtcstat,
  104. rtcopen,
  105. devcreate,
  106. rtcclose,
  107. rtcread,
  108. devbread,
  109. rtcwrite,
  110. devbwrite,
  111. devremove,
  112. devwstat,
  113. };