dev.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * RTC subsystem, dev interface
  4. *
  5. * Copyright (C) 2005 Tower Technologies
  6. * Author: Alessandro Zummo <[email protected]>
  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_work(&rtc->uie_task);
  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. struct rtc_param param;
  181. void __user *uarg = (void __user *)arg;
  182. err = mutex_lock_interruptible(&rtc->ops_lock);
  183. if (err)
  184. return err;
  185. /* check that the calling task has appropriate permissions
  186. * for certain ioctls. doing this check here is useful
  187. * to avoid duplicate code in each driver.
  188. */
  189. switch (cmd) {
  190. case RTC_EPOCH_SET:
  191. case RTC_SET_TIME:
  192. case RTC_PARAM_SET:
  193. if (!capable(CAP_SYS_TIME))
  194. err = -EACCES;
  195. break;
  196. case RTC_IRQP_SET:
  197. if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
  198. err = -EACCES;
  199. break;
  200. case RTC_PIE_ON:
  201. if (rtc->irq_freq > rtc->max_user_freq &&
  202. !capable(CAP_SYS_RESOURCE))
  203. err = -EACCES;
  204. break;
  205. }
  206. if (err)
  207. goto done;
  208. /*
  209. * Drivers *SHOULD NOT* provide ioctl implementations
  210. * for these requests. Instead, provide methods to
  211. * support the following code, so that the RTC's main
  212. * features are accessible without using ioctls.
  213. *
  214. * RTC and alarm times will be in UTC, by preference,
  215. * but dual-booting with MS-Windows implies RTCs must
  216. * use the local wall clock time.
  217. */
  218. switch (cmd) {
  219. case RTC_ALM_READ:
  220. mutex_unlock(&rtc->ops_lock);
  221. err = rtc_read_alarm(rtc, &alarm);
  222. if (err < 0)
  223. return err;
  224. if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
  225. err = -EFAULT;
  226. return err;
  227. case RTC_ALM_SET:
  228. mutex_unlock(&rtc->ops_lock);
  229. if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
  230. return -EFAULT;
  231. alarm.enabled = 0;
  232. alarm.pending = 0;
  233. alarm.time.tm_wday = -1;
  234. alarm.time.tm_yday = -1;
  235. alarm.time.tm_isdst = -1;
  236. /* RTC_ALM_SET alarms may be up to 24 hours in the future.
  237. * Rather than expecting every RTC to implement "don't care"
  238. * for day/month/year fields, just force the alarm to have
  239. * the right values for those fields.
  240. *
  241. * RTC_WKALM_SET should be used instead. Not only does it
  242. * eliminate the need for a separate RTC_AIE_ON call, it
  243. * doesn't have the "alarm 23:59:59 in the future" race.
  244. *
  245. * NOTE: some legacy code may have used invalid fields as
  246. * wildcards, exposing hardware "periodic alarm" capabilities.
  247. * Not supported here.
  248. */
  249. {
  250. time64_t now, then;
  251. err = rtc_read_time(rtc, &tm);
  252. if (err < 0)
  253. return err;
  254. now = rtc_tm_to_time64(&tm);
  255. alarm.time.tm_mday = tm.tm_mday;
  256. alarm.time.tm_mon = tm.tm_mon;
  257. alarm.time.tm_year = tm.tm_year;
  258. err = rtc_valid_tm(&alarm.time);
  259. if (err < 0)
  260. return err;
  261. then = rtc_tm_to_time64(&alarm.time);
  262. /* alarm may need to wrap into tomorrow */
  263. if (then < now) {
  264. rtc_time64_to_tm(now + 24 * 60 * 60, &tm);
  265. alarm.time.tm_mday = tm.tm_mday;
  266. alarm.time.tm_mon = tm.tm_mon;
  267. alarm.time.tm_year = tm.tm_year;
  268. }
  269. }
  270. return rtc_set_alarm(rtc, &alarm);
  271. case RTC_RD_TIME:
  272. mutex_unlock(&rtc->ops_lock);
  273. err = rtc_read_time(rtc, &tm);
  274. if (err < 0)
  275. return err;
  276. if (copy_to_user(uarg, &tm, sizeof(tm)))
  277. err = -EFAULT;
  278. return err;
  279. case RTC_SET_TIME:
  280. mutex_unlock(&rtc->ops_lock);
  281. if (copy_from_user(&tm, uarg, sizeof(tm)))
  282. return -EFAULT;
  283. return rtc_set_time(rtc, &tm);
  284. case RTC_PIE_ON:
  285. err = rtc_irq_set_state(rtc, 1);
  286. break;
  287. case RTC_PIE_OFF:
  288. err = rtc_irq_set_state(rtc, 0);
  289. break;
  290. case RTC_AIE_ON:
  291. mutex_unlock(&rtc->ops_lock);
  292. return rtc_alarm_irq_enable(rtc, 1);
  293. case RTC_AIE_OFF:
  294. mutex_unlock(&rtc->ops_lock);
  295. return rtc_alarm_irq_enable(rtc, 0);
  296. case RTC_UIE_ON:
  297. mutex_unlock(&rtc->ops_lock);
  298. return rtc_update_irq_enable(rtc, 1);
  299. case RTC_UIE_OFF:
  300. mutex_unlock(&rtc->ops_lock);
  301. return rtc_update_irq_enable(rtc, 0);
  302. case RTC_IRQP_SET:
  303. err = rtc_irq_set_freq(rtc, arg);
  304. break;
  305. case RTC_IRQP_READ:
  306. err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
  307. break;
  308. case RTC_WKALM_SET:
  309. mutex_unlock(&rtc->ops_lock);
  310. if (copy_from_user(&alarm, uarg, sizeof(alarm)))
  311. return -EFAULT;
  312. return rtc_set_alarm(rtc, &alarm);
  313. case RTC_WKALM_RD:
  314. mutex_unlock(&rtc->ops_lock);
  315. err = rtc_read_alarm(rtc, &alarm);
  316. if (err < 0)
  317. return err;
  318. if (copy_to_user(uarg, &alarm, sizeof(alarm)))
  319. err = -EFAULT;
  320. return err;
  321. case RTC_PARAM_GET:
  322. if (copy_from_user(&param, uarg, sizeof(param))) {
  323. mutex_unlock(&rtc->ops_lock);
  324. return -EFAULT;
  325. }
  326. switch(param.param) {
  327. case RTC_PARAM_FEATURES:
  328. if (param.index != 0)
  329. err = -EINVAL;
  330. param.uvalue = rtc->features[0];
  331. break;
  332. case RTC_PARAM_CORRECTION: {
  333. long offset;
  334. mutex_unlock(&rtc->ops_lock);
  335. if (param.index != 0)
  336. return -EINVAL;
  337. err = rtc_read_offset(rtc, &offset);
  338. mutex_lock(&rtc->ops_lock);
  339. if (err == 0)
  340. param.svalue = offset;
  341. break;
  342. }
  343. default:
  344. if (rtc->ops->param_get)
  345. err = rtc->ops->param_get(rtc->dev.parent, &param);
  346. else
  347. err = -EINVAL;
  348. }
  349. if (!err)
  350. if (copy_to_user(uarg, &param, sizeof(param)))
  351. err = -EFAULT;
  352. break;
  353. case RTC_PARAM_SET:
  354. if (copy_from_user(&param, uarg, sizeof(param))) {
  355. mutex_unlock(&rtc->ops_lock);
  356. return -EFAULT;
  357. }
  358. switch(param.param) {
  359. case RTC_PARAM_FEATURES:
  360. err = -EINVAL;
  361. break;
  362. case RTC_PARAM_CORRECTION:
  363. mutex_unlock(&rtc->ops_lock);
  364. if (param.index != 0)
  365. return -EINVAL;
  366. return rtc_set_offset(rtc, param.svalue);
  367. default:
  368. if (rtc->ops->param_set)
  369. err = rtc->ops->param_set(rtc->dev.parent, &param);
  370. else
  371. err = -EINVAL;
  372. }
  373. break;
  374. default:
  375. /* Finally try the driver's ioctl interface */
  376. if (ops->ioctl) {
  377. err = ops->ioctl(rtc->dev.parent, cmd, arg);
  378. if (err == -ENOIOCTLCMD)
  379. err = -ENOTTY;
  380. } else {
  381. err = -ENOTTY;
  382. }
  383. break;
  384. }
  385. done:
  386. mutex_unlock(&rtc->ops_lock);
  387. return err;
  388. }
  389. #ifdef CONFIG_COMPAT
  390. #define RTC_IRQP_SET32 _IOW('p', 0x0c, __u32)
  391. #define RTC_IRQP_READ32 _IOR('p', 0x0b, __u32)
  392. #define RTC_EPOCH_SET32 _IOW('p', 0x0e, __u32)
  393. static long rtc_dev_compat_ioctl(struct file *file,
  394. unsigned int cmd, unsigned long arg)
  395. {
  396. struct rtc_device *rtc = file->private_data;
  397. void __user *uarg = compat_ptr(arg);
  398. switch (cmd) {
  399. case RTC_IRQP_READ32:
  400. return put_user(rtc->irq_freq, (__u32 __user *)uarg);
  401. case RTC_IRQP_SET32:
  402. /* arg is a plain integer, not pointer */
  403. return rtc_dev_ioctl(file, RTC_IRQP_SET, arg);
  404. case RTC_EPOCH_SET32:
  405. /* arg is a plain integer, not pointer */
  406. return rtc_dev_ioctl(file, RTC_EPOCH_SET, arg);
  407. }
  408. return rtc_dev_ioctl(file, cmd, (unsigned long)uarg);
  409. }
  410. #endif
  411. static int rtc_dev_fasync(int fd, struct file *file, int on)
  412. {
  413. struct rtc_device *rtc = file->private_data;
  414. return fasync_helper(fd, file, on, &rtc->async_queue);
  415. }
  416. static int rtc_dev_release(struct inode *inode, struct file *file)
  417. {
  418. struct rtc_device *rtc = file->private_data;
  419. /* We shut down the repeating IRQs that userspace enabled,
  420. * since nothing is listening to them.
  421. * - Update (UIE) ... currently only managed through ioctls
  422. * - Periodic (PIE) ... also used through rtc_*() interface calls
  423. *
  424. * Leave the alarm alone; it may be set to trigger a system wakeup
  425. * later, or be used by kernel code, and is a one-shot event anyway.
  426. */
  427. /* Keep ioctl until all drivers are converted */
  428. rtc_dev_ioctl(file, RTC_UIE_OFF, 0);
  429. rtc_update_irq_enable(rtc, 0);
  430. rtc_irq_set_state(rtc, 0);
  431. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  432. return 0;
  433. }
  434. static const struct file_operations rtc_dev_fops = {
  435. .owner = THIS_MODULE,
  436. .llseek = no_llseek,
  437. .read = rtc_dev_read,
  438. .poll = rtc_dev_poll,
  439. .unlocked_ioctl = rtc_dev_ioctl,
  440. #ifdef CONFIG_COMPAT
  441. .compat_ioctl = rtc_dev_compat_ioctl,
  442. #endif
  443. .open = rtc_dev_open,
  444. .release = rtc_dev_release,
  445. .fasync = rtc_dev_fasync,
  446. };
  447. /* insertion/removal hooks */
  448. void rtc_dev_prepare(struct rtc_device *rtc)
  449. {
  450. if (!rtc_devt)
  451. return;
  452. if (rtc->id >= RTC_DEV_MAX) {
  453. dev_dbg(&rtc->dev, "too many RTC devices\n");
  454. return;
  455. }
  456. rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
  457. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  458. INIT_WORK(&rtc->uie_task, rtc_uie_task);
  459. timer_setup(&rtc->uie_timer, rtc_uie_timer, 0);
  460. #endif
  461. cdev_init(&rtc->char_dev, &rtc_dev_fops);
  462. rtc->char_dev.owner = rtc->owner;
  463. }
  464. void __init rtc_dev_init(void)
  465. {
  466. int err;
  467. err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
  468. if (err < 0)
  469. pr_err("failed to allocate char dev region\n");
  470. }