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