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