dev.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * RTC subsystem, dev interface
  4. *
  5. * Copyright (C) 2005 Tower Technologies
  6. * Author: Alessandro Zummo <a.zummo@towertech.it>
  7. *
  8. * based on arch/arm/common/rtctime.c
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/compat.h>
  12. #include <linux/module.h>
  13. #include <linux/rtc.h>
  14. #include <linux/sched/signal.h>
  15. #include "rtc-core.h"
  16. static dev_t rtc_devt;
  17. #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
  18. static int rtc_dev_open(struct inode *inode, struct file *file)
  19. {
  20. struct rtc_device *rtc = container_of(inode->i_cdev,
  21. struct rtc_device, char_dev);
  22. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  23. return -EBUSY;
  24. file->private_data = rtc;
  25. spin_lock_irq(&rtc->irq_lock);
  26. rtc->irq_data = 0;
  27. spin_unlock_irq(&rtc->irq_lock);
  28. return 0;
  29. }
  30. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  31. /*
  32. * Routine to poll RTC seconds field for change as often as possible,
  33. * after first RTC_UIE use timer to reduce polling
  34. */
  35. static void rtc_uie_task(struct work_struct *work)
  36. {
  37. struct rtc_device *rtc =
  38. container_of(work, struct rtc_device, uie_task);
  39. struct rtc_time tm;
  40. int num = 0;
  41. int err;
  42. err = rtc_read_time(rtc, &tm);
  43. spin_lock_irq(&rtc->irq_lock);
  44. if (rtc->stop_uie_polling || err) {
  45. rtc->uie_task_active = 0;
  46. } else if (rtc->oldsecs != tm.tm_sec) {
  47. num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
  48. rtc->oldsecs = tm.tm_sec;
  49. rtc->uie_timer.expires = jiffies + HZ - (HZ / 10);
  50. rtc->uie_timer_active = 1;
  51. rtc->uie_task_active = 0;
  52. add_timer(&rtc->uie_timer);
  53. } else if (schedule_work(&rtc->uie_task) == 0) {
  54. rtc->uie_task_active = 0;
  55. }
  56. spin_unlock_irq(&rtc->irq_lock);
  57. if (num)
  58. rtc_handle_legacy_irq(rtc, num, RTC_UF);
  59. }
  60. static void rtc_uie_timer(struct timer_list *t)
  61. {
  62. struct rtc_device *rtc = from_timer(rtc, t, uie_timer);
  63. unsigned long flags;
  64. spin_lock_irqsave(&rtc->irq_lock, flags);
  65. rtc->uie_timer_active = 0;
  66. rtc->uie_task_active = 1;
  67. if ((schedule_work(&rtc->uie_task) == 0))
  68. rtc->uie_task_active = 0;
  69. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  70. }
  71. static int clear_uie(struct rtc_device *rtc)
  72. {
  73. spin_lock_irq(&rtc->irq_lock);
  74. if (rtc->uie_irq_active) {
  75. rtc->stop_uie_polling = 1;
  76. if (rtc->uie_timer_active) {
  77. spin_unlock_irq(&rtc->irq_lock);
  78. del_timer_sync(&rtc->uie_timer);
  79. spin_lock_irq(&rtc->irq_lock);
  80. rtc->uie_timer_active = 0;
  81. }
  82. if (rtc->uie_task_active) {
  83. spin_unlock_irq(&rtc->irq_lock);
  84. flush_scheduled_work();
  85. spin_lock_irq(&rtc->irq_lock);
  86. }
  87. rtc->uie_irq_active = 0;
  88. }
  89. spin_unlock_irq(&rtc->irq_lock);
  90. return 0;
  91. }
  92. static int set_uie(struct rtc_device *rtc)
  93. {
  94. struct rtc_time tm;
  95. int err;
  96. err = rtc_read_time(rtc, &tm);
  97. if (err)
  98. return err;
  99. spin_lock_irq(&rtc->irq_lock);
  100. if (!rtc->uie_irq_active) {
  101. rtc->uie_irq_active = 1;
  102. rtc->stop_uie_polling = 0;
  103. rtc->oldsecs = tm.tm_sec;
  104. rtc->uie_task_active = 1;
  105. if (schedule_work(&rtc->uie_task) == 0)
  106. rtc->uie_task_active = 0;
  107. }
  108. rtc->irq_data = 0;
  109. spin_unlock_irq(&rtc->irq_lock);
  110. return 0;
  111. }
  112. int rtc_dev_update_irq_enable_emul(struct rtc_device *rtc, unsigned int enabled)
  113. {
  114. if (enabled)
  115. return set_uie(rtc);
  116. else
  117. return clear_uie(rtc);
  118. }
  119. EXPORT_SYMBOL(rtc_dev_update_irq_enable_emul);
  120. #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
  121. static ssize_t
  122. rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  123. {
  124. struct rtc_device *rtc = file->private_data;
  125. DECLARE_WAITQUEUE(wait, current);
  126. unsigned long data;
  127. ssize_t ret;
  128. if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
  129. return -EINVAL;
  130. add_wait_queue(&rtc->irq_queue, &wait);
  131. do {
  132. __set_current_state(TASK_INTERRUPTIBLE);
  133. spin_lock_irq(&rtc->irq_lock);
  134. data = rtc->irq_data;
  135. rtc->irq_data = 0;
  136. spin_unlock_irq(&rtc->irq_lock);
  137. if (data != 0) {
  138. ret = 0;
  139. break;
  140. }
  141. if (file->f_flags & O_NONBLOCK) {
  142. ret = -EAGAIN;
  143. break;
  144. }
  145. if (signal_pending(current)) {
  146. ret = -ERESTARTSYS;
  147. break;
  148. }
  149. schedule();
  150. } while (1);
  151. set_current_state(TASK_RUNNING);
  152. remove_wait_queue(&rtc->irq_queue, &wait);
  153. if (ret == 0) {
  154. if (sizeof(int) != sizeof(long) &&
  155. count == sizeof(unsigned int))
  156. ret = put_user(data, (unsigned int __user *)buf) ?:
  157. sizeof(unsigned int);
  158. else
  159. ret = put_user(data, (unsigned long __user *)buf) ?:
  160. sizeof(unsigned long);
  161. }
  162. return ret;
  163. }
  164. static __poll_t rtc_dev_poll(struct file *file, poll_table *wait)
  165. {
  166. struct rtc_device *rtc = file->private_data;
  167. unsigned long data;
  168. poll_wait(file, &rtc->irq_queue, wait);
  169. data = rtc->irq_data;
  170. return (data != 0) ? (EPOLLIN | EPOLLRDNORM) : 0;
  171. }
  172. static long rtc_dev_ioctl(struct file *file,
  173. unsigned int cmd, unsigned long arg)
  174. {
  175. int err = 0;
  176. struct rtc_device *rtc = file->private_data;
  177. const struct rtc_class_ops *ops = rtc->ops;
  178. struct rtc_time tm;
  179. struct rtc_wkalrm alarm;
  180. void __user *uarg = (void __user *)arg;
  181. err = mutex_lock_interruptible(&rtc->ops_lock);
  182. if (err)
  183. return err;
  184. /* check that the calling task has appropriate permissions
  185. * for certain ioctls. doing this check here is useful
  186. * to avoid duplicate code in each driver.
  187. */
  188. switch (cmd) {
  189. case RTC_EPOCH_SET:
  190. case RTC_SET_TIME:
  191. if (!capable(CAP_SYS_TIME))
  192. err = -EACCES;
  193. break;
  194. case RTC_IRQP_SET:
  195. if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
  196. err = -EACCES;
  197. break;
  198. case RTC_PIE_ON:
  199. if (rtc->irq_freq > rtc->max_user_freq &&
  200. !capable(CAP_SYS_RESOURCE))
  201. err = -EACCES;
  202. break;
  203. }
  204. if (err)
  205. goto done;
  206. /*
  207. * Drivers *SHOULD NOT* provide ioctl implementations
  208. * for these requests. Instead, provide methods to
  209. * support the following code, so that the RTC's main
  210. * features are accessible without using ioctls.
  211. *
  212. * RTC and alarm times will be in UTC, by preference,
  213. * but dual-booting with MS-Windows implies RTCs must
  214. * use the local wall clock time.
  215. */
  216. switch (cmd) {
  217. case RTC_ALM_READ:
  218. mutex_unlock(&rtc->ops_lock);
  219. err = rtc_read_alarm(rtc, &alarm);
  220. if (err < 0)
  221. return err;
  222. if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
  223. err = -EFAULT;
  224. return err;
  225. case RTC_ALM_SET:
  226. mutex_unlock(&rtc->ops_lock);
  227. if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
  228. return -EFAULT;
  229. alarm.enabled = 0;
  230. alarm.pending = 0;
  231. alarm.time.tm_wday = -1;
  232. alarm.time.tm_yday = -1;
  233. alarm.time.tm_isdst = -1;
  234. /* RTC_ALM_SET alarms may be up to 24 hours in the future.
  235. * Rather than expecting every RTC to implement "don't care"
  236. * for day/month/year fields, just force the alarm to have
  237. * the right values for those fields.
  238. *
  239. * RTC_WKALM_SET should be used instead. Not only does it
  240. * eliminate the need for a separate RTC_AIE_ON call, it
  241. * doesn't have the "alarm 23:59:59 in the future" race.
  242. *
  243. * NOTE: some legacy code may have used invalid fields as
  244. * wildcards, exposing hardware "periodic alarm" capabilities.
  245. * Not supported here.
  246. */
  247. {
  248. time64_t now, then;
  249. err = rtc_read_time(rtc, &tm);
  250. if (err < 0)
  251. return err;
  252. now = rtc_tm_to_time64(&tm);
  253. alarm.time.tm_mday = tm.tm_mday;
  254. alarm.time.tm_mon = tm.tm_mon;
  255. alarm.time.tm_year = tm.tm_year;
  256. err = rtc_valid_tm(&alarm.time);
  257. if (err < 0)
  258. return err;
  259. then = rtc_tm_to_time64(&alarm.time);
  260. /* alarm may need to wrap into tomorrow */
  261. if (then < now) {
  262. rtc_time64_to_tm(now + 24 * 60 * 60, &tm);
  263. alarm.time.tm_mday = tm.tm_mday;
  264. alarm.time.tm_mon = tm.tm_mon;
  265. alarm.time.tm_year = tm.tm_year;
  266. }
  267. }
  268. return rtc_set_alarm(rtc, &alarm);
  269. case RTC_RD_TIME:
  270. mutex_unlock(&rtc->ops_lock);
  271. err = rtc_read_time(rtc, &tm);
  272. if (err < 0)
  273. return err;
  274. if (copy_to_user(uarg, &tm, sizeof(tm)))
  275. err = -EFAULT;
  276. return err;
  277. case RTC_SET_TIME:
  278. mutex_unlock(&rtc->ops_lock);
  279. if (copy_from_user(&tm, uarg, sizeof(tm)))
  280. return -EFAULT;
  281. return rtc_set_time(rtc, &tm);
  282. case RTC_PIE_ON:
  283. err = rtc_irq_set_state(rtc, 1);
  284. break;
  285. case RTC_PIE_OFF:
  286. err = rtc_irq_set_state(rtc, 0);
  287. break;
  288. case RTC_AIE_ON:
  289. mutex_unlock(&rtc->ops_lock);
  290. return rtc_alarm_irq_enable(rtc, 1);
  291. case RTC_AIE_OFF:
  292. mutex_unlock(&rtc->ops_lock);
  293. return rtc_alarm_irq_enable(rtc, 0);
  294. case RTC_UIE_ON:
  295. mutex_unlock(&rtc->ops_lock);
  296. return rtc_update_irq_enable(rtc, 1);
  297. case RTC_UIE_OFF:
  298. mutex_unlock(&rtc->ops_lock);
  299. return rtc_update_irq_enable(rtc, 0);
  300. case RTC_IRQP_SET:
  301. err = rtc_irq_set_freq(rtc, arg);
  302. break;
  303. case RTC_IRQP_READ:
  304. err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
  305. break;
  306. case RTC_WKALM_SET:
  307. mutex_unlock(&rtc->ops_lock);
  308. if (copy_from_user(&alarm, uarg, sizeof(alarm)))
  309. return -EFAULT;
  310. return rtc_set_alarm(rtc, &alarm);
  311. case RTC_WKALM_RD:
  312. mutex_unlock(&rtc->ops_lock);
  313. err = rtc_read_alarm(rtc, &alarm);
  314. if (err < 0)
  315. return err;
  316. if (copy_to_user(uarg, &alarm, sizeof(alarm)))
  317. err = -EFAULT;
  318. return err;
  319. default:
  320. /* Finally try the driver's ioctl interface */
  321. if (ops->ioctl) {
  322. err = ops->ioctl(rtc->dev.parent, cmd, arg);
  323. if (err == -ENOIOCTLCMD)
  324. err = -ENOTTY;
  325. } else {
  326. err = -ENOTTY;
  327. }
  328. break;
  329. }
  330. done:
  331. mutex_unlock(&rtc->ops_lock);
  332. return err;
  333. }
  334. #ifdef CONFIG_COMPAT
  335. #define RTC_IRQP_SET32 _IOW('p', 0x0c, __u32)
  336. #define RTC_IRQP_READ32 _IOR('p', 0x0b, __u32)
  337. #define RTC_EPOCH_SET32 _IOW('p', 0x0e, __u32)
  338. static long rtc_dev_compat_ioctl(struct file *file,
  339. unsigned int cmd, unsigned long arg)
  340. {
  341. struct rtc_device *rtc = file->private_data;
  342. void __user *uarg = compat_ptr(arg);
  343. switch (cmd) {
  344. case RTC_IRQP_READ32:
  345. return put_user(rtc->irq_freq, (__u32 __user *)uarg);
  346. case RTC_IRQP_SET32:
  347. /* arg is a plain integer, not pointer */
  348. return rtc_dev_ioctl(file, RTC_IRQP_SET, arg);
  349. case RTC_EPOCH_SET32:
  350. /* arg is a plain integer, not pointer */
  351. return rtc_dev_ioctl(file, RTC_EPOCH_SET, arg);
  352. }
  353. return rtc_dev_ioctl(file, cmd, (unsigned long)uarg);
  354. }
  355. #endif
  356. static int rtc_dev_fasync(int fd, struct file *file, int on)
  357. {
  358. struct rtc_device *rtc = file->private_data;
  359. return fasync_helper(fd, file, on, &rtc->async_queue);
  360. }
  361. static int rtc_dev_release(struct inode *inode, struct file *file)
  362. {
  363. struct rtc_device *rtc = file->private_data;
  364. /* We shut down the repeating IRQs that userspace enabled,
  365. * since nothing is listening to them.
  366. * - Update (UIE) ... currently only managed through ioctls
  367. * - Periodic (PIE) ... also used through rtc_*() interface calls
  368. *
  369. * Leave the alarm alone; it may be set to trigger a system wakeup
  370. * later, or be used by kernel code, and is a one-shot event anyway.
  371. */
  372. /* Keep ioctl until all drivers are converted */
  373. rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
  374. rtc_update_irq_enable(rtc, 0);
  375. rtc_irq_set_state(rtc, 0);
  376. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  377. return 0;
  378. }
  379. static const struct file_operations rtc_dev_fops = {
  380. .owner = THIS_MODULE,
  381. .llseek = no_llseek,
  382. .read = rtc_dev_read,
  383. .poll = rtc_dev_poll,
  384. .unlocked_ioctl = rtc_dev_ioctl,
  385. #ifdef CONFIG_COMPAT
  386. .compat_ioctl = rtc_dev_compat_ioctl,
  387. #endif
  388. .open = rtc_dev_open,
  389. .release = rtc_dev_release,
  390. .fasync = rtc_dev_fasync,
  391. };
  392. /* insertion/removal hooks */
  393. void rtc_dev_prepare(struct rtc_device *rtc)
  394. {
  395. if (!rtc_devt)
  396. return;
  397. if (rtc->id >= RTC_DEV_MAX) {
  398. dev_dbg(&rtc->dev, "too many RTC devices\n");
  399. return;
  400. }
  401. rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
  402. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  403. INIT_WORK(&rtc->uie_task, rtc_uie_task);
  404. timer_setup(&rtc->uie_timer, rtc_uie_timer, 0);
  405. #endif
  406. cdev_init(&rtc->char_dev, &rtc_dev_fops);
  407. rtc->char_dev.owner = rtc->owner;
  408. }
  409. void __init rtc_dev_init(void)
  410. {
  411. int err;
  412. err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
  413. if (err < 0)
  414. pr_err("failed to allocate char dev region\n");
  415. }
  416. void __exit rtc_dev_exit(void)
  417. {
  418. if (rtc_devt)
  419. unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
  420. }