genrtc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*
  2. * Real Time Clock interface for
  3. * - q40 and other m68k machines,
  4. * - HP PARISC machines
  5. * - PowerPC machines
  6. * emulate some RTC irq capabilities in software
  7. *
  8. * Copyright (C) 1999 Richard Zidlicky
  9. *
  10. * based on Paul Gortmaker's rtc.c device and
  11. * Sam Creasey Generic rtc driver
  12. *
  13. * This driver allows use of the real time clock (built into
  14. * nearly all computers) from user space. It exports the /dev/rtc
  15. * interface supporting various ioctl() and also the /proc/dev/rtc
  16. * pseudo-file for status information.
  17. *
  18. * The ioctls can be used to set the interrupt behaviour where
  19. * supported.
  20. *
  21. * The /dev/rtc interface will block on reads until an interrupt
  22. * has been received. If a RTC interrupt has already happened,
  23. * it will output an unsigned long and then block. The output value
  24. * contains the interrupt status in the low byte and the number of
  25. * interrupts since the last read in the remaining high bytes. The
  26. * /dev/rtc interface can also be used with the select(2) call.
  27. *
  28. * This program is free software; you can redistribute it and/or
  29. * modify it under the terms of the GNU General Public License
  30. * as published by the Free Software Foundation; either version
  31. * 2 of the License, or (at your option) any later version.
  32. *
  33. * 1.01 fix for 2.3.X rz@linux-m68k.org
  34. * 1.02 merged with code from genrtc.c rz@linux-m68k.org
  35. * 1.03 make it more portable zippel@linux-m68k.org
  36. * 1.04 removed useless timer code rz@linux-m68k.org
  37. * 1.05 portable RTC_UIE emulation rz@linux-m68k.org
  38. * 1.06 set_rtc_time can return an error trini@kernel.crashing.org
  39. * 1.07 ported to HP PARISC (hppa) Helge Deller <deller@gmx.de>
  40. */
  41. #define RTC_VERSION "1.07"
  42. #include <linux/module.h>
  43. #include <linux/errno.h>
  44. #include <linux/miscdevice.h>
  45. #include <linux/fcntl.h>
  46. #include <linux/rtc.h>
  47. #include <linux/init.h>
  48. #include <linux/poll.h>
  49. #include <linux/proc_fs.h>
  50. #include <linux/workqueue.h>
  51. #include <asm/uaccess.h>
  52. #include <asm/system.h>
  53. #include <asm/rtc.h>
  54. /*
  55. * We sponge a minor off of the misc major. No need slurping
  56. * up another valuable major dev number for this. If you add
  57. * an ioctl, make sure you don't conflict with SPARC's RTC
  58. * ioctls.
  59. */
  60. static DECLARE_WAIT_QUEUE_HEAD(gen_rtc_wait);
  61. /*
  62. * Bits in gen_rtc_status.
  63. */
  64. #define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */
  65. static unsigned char gen_rtc_status; /* bitmapped status byte. */
  66. static unsigned long gen_rtc_irq_data; /* our output to the world */
  67. /* months start at 0 now */
  68. static unsigned char days_in_mo[] =
  69. {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  70. static int irq_active;
  71. #ifdef CONFIG_GEN_RTC_X
  72. static struct work_struct genrtc_task;
  73. static struct timer_list timer_task;
  74. static unsigned int oldsecs;
  75. static int lostint;
  76. static unsigned long tt_exp;
  77. static void gen_rtc_timer(unsigned long data);
  78. static volatile int stask_active; /* schedule_work */
  79. static volatile int ttask_active; /* timer_task */
  80. static int stop_rtc_timers; /* don't requeue tasks */
  81. static DEFINE_SPINLOCK(gen_rtc_lock);
  82. static void gen_rtc_interrupt(unsigned long arg);
  83. /*
  84. * Routine to poll RTC seconds field for change as often as possible,
  85. * after first RTC_UIE use timer to reduce polling
  86. */
  87. static void genrtc_troutine(void *data)
  88. {
  89. unsigned int tmp = get_rtc_ss();
  90. if (stop_rtc_timers) {
  91. stask_active = 0;
  92. return;
  93. }
  94. if (oldsecs != tmp){
  95. oldsecs = tmp;
  96. timer_task.function = gen_rtc_timer;
  97. timer_task.expires = jiffies + HZ - (HZ/10);
  98. tt_exp=timer_task.expires;
  99. ttask_active=1;
  100. stask_active=0;
  101. add_timer(&timer_task);
  102. gen_rtc_interrupt(0);
  103. } else if (schedule_work(&genrtc_task) == 0)
  104. stask_active = 0;
  105. }
  106. static void gen_rtc_timer(unsigned long data)
  107. {
  108. lostint = get_rtc_ss() - oldsecs ;
  109. if (lostint<0)
  110. lostint = 60 - lostint;
  111. if (time_after(jiffies, tt_exp))
  112. printk(KERN_INFO "genrtc: timer task delayed by %ld jiffies\n",
  113. jiffies-tt_exp);
  114. ttask_active=0;
  115. stask_active=1;
  116. if ((schedule_work(&genrtc_task) == 0))
  117. stask_active = 0;
  118. }
  119. /*
  120. * call gen_rtc_interrupt function to signal an RTC_UIE,
  121. * arg is unused.
  122. * Could be invoked either from a real interrupt handler or
  123. * from some routine that periodically (eg 100HZ) monitors
  124. * whether RTC_SECS changed
  125. */
  126. static void gen_rtc_interrupt(unsigned long arg)
  127. {
  128. /* We store the status in the low byte and the number of
  129. * interrupts received since the last read in the remainder
  130. * of rtc_irq_data. */
  131. gen_rtc_irq_data += 0x100;
  132. gen_rtc_irq_data &= ~0xff;
  133. gen_rtc_irq_data |= RTC_UIE;
  134. if (lostint){
  135. printk("genrtc: system delaying clock ticks?\n");
  136. /* increment count so that userspace knows something is wrong */
  137. gen_rtc_irq_data += ((lostint-1)<<8);
  138. lostint = 0;
  139. }
  140. wake_up_interruptible(&gen_rtc_wait);
  141. }
  142. /*
  143. * Now all the various file operations that we export.
  144. */
  145. static ssize_t gen_rtc_read(struct file *file, char __user *buf,
  146. size_t count, loff_t *ppos)
  147. {
  148. DECLARE_WAITQUEUE(wait, current);
  149. unsigned long data;
  150. ssize_t retval;
  151. if (count != sizeof (unsigned int) && count != sizeof (unsigned long))
  152. return -EINVAL;
  153. if (file->f_flags & O_NONBLOCK && !gen_rtc_irq_data)
  154. return -EAGAIN;
  155. add_wait_queue(&gen_rtc_wait, &wait);
  156. retval = -ERESTARTSYS;
  157. while (1) {
  158. set_current_state(TASK_INTERRUPTIBLE);
  159. data = xchg(&gen_rtc_irq_data, 0);
  160. if (data)
  161. break;
  162. if (signal_pending(current))
  163. goto out;
  164. schedule();
  165. }
  166. /* first test allows optimizer to nuke this case for 32-bit machines */
  167. if (sizeof (int) != sizeof (long) && count == sizeof (unsigned int)) {
  168. unsigned int uidata = data;
  169. retval = put_user(uidata, (unsigned int __user *)buf) ?:
  170. sizeof(unsigned int);
  171. }
  172. else {
  173. retval = put_user(data, (unsigned long __user *)buf) ?:
  174. sizeof(unsigned long);
  175. }
  176. out:
  177. current->state = TASK_RUNNING;
  178. remove_wait_queue(&gen_rtc_wait, &wait);
  179. return retval;
  180. }
  181. static unsigned int gen_rtc_poll(struct file *file,
  182. struct poll_table_struct *wait)
  183. {
  184. poll_wait(file, &gen_rtc_wait, wait);
  185. if (gen_rtc_irq_data != 0)
  186. return POLLIN | POLLRDNORM;
  187. return 0;
  188. }
  189. #endif
  190. /*
  191. * Used to disable/enable interrupts, only RTC_UIE supported
  192. * We also clear out any old irq data after an ioctl() that
  193. * meddles with the interrupt enable/disable bits.
  194. */
  195. static inline void gen_clear_rtc_irq_bit(unsigned char bit)
  196. {
  197. #ifdef CONFIG_GEN_RTC_X
  198. stop_rtc_timers = 1;
  199. if (ttask_active){
  200. del_timer_sync(&timer_task);
  201. ttask_active = 0;
  202. }
  203. while (stask_active)
  204. schedule();
  205. spin_lock(&gen_rtc_lock);
  206. irq_active = 0;
  207. spin_unlock(&gen_rtc_lock);
  208. #endif
  209. }
  210. static inline int gen_set_rtc_irq_bit(unsigned char bit)
  211. {
  212. #ifdef CONFIG_GEN_RTC_X
  213. spin_lock(&gen_rtc_lock);
  214. if ( !irq_active ) {
  215. irq_active = 1;
  216. stop_rtc_timers = 0;
  217. lostint = 0;
  218. INIT_WORK(&genrtc_task, genrtc_troutine, NULL);
  219. oldsecs = get_rtc_ss();
  220. init_timer(&timer_task);
  221. stask_active = 1;
  222. if (schedule_work(&genrtc_task) == 0){
  223. stask_active = 0;
  224. }
  225. }
  226. spin_unlock(&gen_rtc_lock);
  227. gen_rtc_irq_data = 0;
  228. return 0;
  229. #else
  230. return -EINVAL;
  231. #endif
  232. }
  233. static int gen_rtc_ioctl(struct inode *inode, struct file *file,
  234. unsigned int cmd, unsigned long arg)
  235. {
  236. struct rtc_time wtime;
  237. struct rtc_pll_info pll;
  238. void __user *argp = (void __user *)arg;
  239. switch (cmd) {
  240. case RTC_PLL_GET:
  241. if (get_rtc_pll(&pll))
  242. return -EINVAL;
  243. else
  244. return copy_to_user(argp, &pll, sizeof pll) ? -EFAULT : 0;
  245. case RTC_PLL_SET:
  246. if (!capable(CAP_SYS_TIME))
  247. return -EACCES;
  248. if (copy_from_user(&pll, argp, sizeof(pll)))
  249. return -EFAULT;
  250. return set_rtc_pll(&pll);
  251. case RTC_UIE_OFF: /* disable ints from RTC updates. */
  252. gen_clear_rtc_irq_bit(RTC_UIE);
  253. return 0;
  254. case RTC_UIE_ON: /* enable ints for RTC updates. */
  255. return gen_set_rtc_irq_bit(RTC_UIE);
  256. case RTC_RD_TIME: /* Read the time/date from RTC */
  257. /* this doesn't get week-day, who cares */
  258. memset(&wtime, 0, sizeof(wtime));
  259. get_rtc_time(&wtime);
  260. return copy_to_user(argp, &wtime, sizeof(wtime)) ? -EFAULT : 0;
  261. case RTC_SET_TIME: /* Set the RTC */
  262. {
  263. int year;
  264. unsigned char leap_yr;
  265. if (!capable(CAP_SYS_TIME))
  266. return -EACCES;
  267. if (copy_from_user(&wtime, argp, sizeof(wtime)))
  268. return -EFAULT;
  269. year = wtime.tm_year + 1900;
  270. leap_yr = ((!(year % 4) && (year % 100)) ||
  271. !(year % 400));
  272. if ((wtime.tm_mon < 0 || wtime.tm_mon > 11) || (wtime.tm_mday < 1))
  273. return -EINVAL;
  274. if (wtime.tm_mday < 0 || wtime.tm_mday >
  275. (days_in_mo[wtime.tm_mon] + ((wtime.tm_mon == 1) && leap_yr)))
  276. return -EINVAL;
  277. if (wtime.tm_hour < 0 || wtime.tm_hour >= 24 ||
  278. wtime.tm_min < 0 || wtime.tm_min >= 60 ||
  279. wtime.tm_sec < 0 || wtime.tm_sec >= 60)
  280. return -EINVAL;
  281. return set_rtc_time(&wtime);
  282. }
  283. }
  284. return -EINVAL;
  285. }
  286. /*
  287. * We enforce only one user at a time here with the open/close.
  288. * Also clear the previous interrupt data on an open, and clean
  289. * up things on a close.
  290. */
  291. static int gen_rtc_open(struct inode *inode, struct file *file)
  292. {
  293. if (gen_rtc_status & RTC_IS_OPEN)
  294. return -EBUSY;
  295. gen_rtc_status |= RTC_IS_OPEN;
  296. gen_rtc_irq_data = 0;
  297. irq_active = 0;
  298. return 0;
  299. }
  300. static int gen_rtc_release(struct inode *inode, struct file *file)
  301. {
  302. /*
  303. * Turn off all interrupts once the device is no longer
  304. * in use and clear the data.
  305. */
  306. gen_clear_rtc_irq_bit(RTC_PIE|RTC_AIE|RTC_UIE);
  307. gen_rtc_status &= ~RTC_IS_OPEN;
  308. return 0;
  309. }
  310. #ifdef CONFIG_PROC_FS
  311. /*
  312. * Info exported via "/proc/rtc".
  313. */
  314. static int gen_rtc_proc_output(char *buf)
  315. {
  316. char *p;
  317. struct rtc_time tm;
  318. unsigned int flags;
  319. struct rtc_pll_info pll;
  320. p = buf;
  321. flags = get_rtc_time(&tm);
  322. p += sprintf(p,
  323. "rtc_time\t: %02d:%02d:%02d\n"
  324. "rtc_date\t: %04d-%02d-%02d\n"
  325. "rtc_epoch\t: %04u\n",
  326. tm.tm_hour, tm.tm_min, tm.tm_sec,
  327. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, 1900);
  328. tm.tm_hour = tm.tm_min = tm.tm_sec = 0;
  329. p += sprintf(p, "alarm\t\t: ");
  330. if (tm.tm_hour <= 24)
  331. p += sprintf(p, "%02d:", tm.tm_hour);
  332. else
  333. p += sprintf(p, "**:");
  334. if (tm.tm_min <= 59)
  335. p += sprintf(p, "%02d:", tm.tm_min);
  336. else
  337. p += sprintf(p, "**:");
  338. if (tm.tm_sec <= 59)
  339. p += sprintf(p, "%02d\n", tm.tm_sec);
  340. else
  341. p += sprintf(p, "**\n");
  342. p += sprintf(p,
  343. "DST_enable\t: %s\n"
  344. "BCD\t\t: %s\n"
  345. "24hr\t\t: %s\n"
  346. "square_wave\t: %s\n"
  347. "alarm_IRQ\t: %s\n"
  348. "update_IRQ\t: %s\n"
  349. "periodic_IRQ\t: %s\n"
  350. "periodic_freq\t: %ld\n"
  351. "batt_status\t: %s\n",
  352. (flags & RTC_DST_EN) ? "yes" : "no",
  353. (flags & RTC_DM_BINARY) ? "no" : "yes",
  354. (flags & RTC_24H) ? "yes" : "no",
  355. (flags & RTC_SQWE) ? "yes" : "no",
  356. (flags & RTC_AIE) ? "yes" : "no",
  357. irq_active ? "yes" : "no",
  358. (flags & RTC_PIE) ? "yes" : "no",
  359. 0L /* freq */,
  360. (flags & RTC_BATT_BAD) ? "bad" : "okay");
  361. if (!get_rtc_pll(&pll))
  362. p += sprintf(p,
  363. "PLL adjustment\t: %d\n"
  364. "PLL max +ve adjustment\t: %d\n"
  365. "PLL max -ve adjustment\t: %d\n"
  366. "PLL +ve adjustment factor\t: %d\n"
  367. "PLL -ve adjustment factor\t: %d\n"
  368. "PLL frequency\t: %ld\n",
  369. pll.pll_value,
  370. pll.pll_max,
  371. pll.pll_min,
  372. pll.pll_posmult,
  373. pll.pll_negmult,
  374. pll.pll_clock);
  375. return p - buf;
  376. }
  377. static int gen_rtc_read_proc(char *page, char **start, off_t off,
  378. int count, int *eof, void *data)
  379. {
  380. int len = gen_rtc_proc_output (page);
  381. if (len <= off+count) *eof = 1;
  382. *start = page + off;
  383. len -= off;
  384. if (len>count) len = count;
  385. if (len<0) len = 0;
  386. return len;
  387. }
  388. static int __init gen_rtc_proc_init(void)
  389. {
  390. struct proc_dir_entry *r;
  391. r = create_proc_read_entry("driver/rtc", 0, NULL, gen_rtc_read_proc, NULL);
  392. if (!r)
  393. return -ENOMEM;
  394. return 0;
  395. }
  396. #else
  397. static inline int gen_rtc_proc_init(void) { return 0; }
  398. #endif /* CONFIG_PROC_FS */
  399. /*
  400. * The various file operations we support.
  401. */
  402. static const struct file_operations gen_rtc_fops = {
  403. .owner = THIS_MODULE,
  404. #ifdef CONFIG_GEN_RTC_X
  405. .read = gen_rtc_read,
  406. .poll = gen_rtc_poll,
  407. #endif
  408. .ioctl = gen_rtc_ioctl,
  409. .open = gen_rtc_open,
  410. .release = gen_rtc_release,
  411. };
  412. static struct miscdevice rtc_gen_dev =
  413. {
  414. .minor = RTC_MINOR,
  415. .name = "rtc",
  416. .fops = &gen_rtc_fops,
  417. };
  418. static int __init rtc_generic_init(void)
  419. {
  420. int retval;
  421. printk(KERN_INFO "Generic RTC Driver v%s\n", RTC_VERSION);
  422. retval = misc_register(&rtc_gen_dev);
  423. if (retval < 0)
  424. return retval;
  425. retval = gen_rtc_proc_init();
  426. if (retval) {
  427. misc_deregister(&rtc_gen_dev);
  428. return retval;
  429. }
  430. return 0;
  431. }
  432. static void __exit rtc_generic_exit(void)
  433. {
  434. remove_proc_entry ("driver/rtc", NULL);
  435. misc_deregister(&rtc_gen_dev);
  436. }
  437. module_init(rtc_generic_init);
  438. module_exit(rtc_generic_exit);
  439. MODULE_AUTHOR("Richard Zidlicky");
  440. MODULE_LICENSE("GPL");
  441. MODULE_ALIAS_MISCDEV(RTC_MINOR);