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