genrtc.c 13 KB

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