blob: 814583bd2fe709c1da0710808ed6f4cffbe8b50f [file] [log] [blame]
Alessandro Zummoe8242902006-03-27 01:16:41 -08001/*
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>
David Brownell5726fb22007-05-08 00:33:27 -070016#include "rtc-core.h"
Alessandro Zummoe8242902006-03-27 01:16:41 -080017
Alessandro Zummoe8242902006-03-27 01:16:41 -080018static 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 int err;
25 struct rtc_device *rtc = container_of(inode->i_cdev,
26 struct rtc_device, char_dev);
David Brownellff8371a2006-09-30 23:28:17 -070027 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -080028
29 /* We keep the lock as long as the device is in use
30 * and return immediately if busy
31 */
32 if (!(mutex_trylock(&rtc->char_lock)))
33 return -EBUSY;
34
David Brownellab6a2d72007-05-08 00:33:30 -070035 file->private_data = rtc;
Alessandro Zummoe8242902006-03-27 01:16:41 -080036
David Brownellcd966202007-05-08 00:33:40 -070037 err = ops->open ? ops->open(rtc->dev.parent) : 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -080038 if (err == 0) {
39 spin_lock_irq(&rtc->irq_lock);
40 rtc->irq_data = 0;
41 spin_unlock_irq(&rtc->irq_lock);
42
43 return 0;
44 }
45
46 /* something has gone wrong, release the lock */
47 mutex_unlock(&rtc->char_lock);
48 return err;
49}
50
Atsushi Nemoto655066c2006-06-25 05:48:17 -070051#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
52/*
53 * Routine to poll RTC seconds field for change as often as possible,
54 * after first RTC_UIE use timer to reduce polling
55 */
David Howellsc4028952006-11-22 14:57:56 +000056static void rtc_uie_task(struct work_struct *work)
Atsushi Nemoto655066c2006-06-25 05:48:17 -070057{
David Howellsc4028952006-11-22 14:57:56 +000058 struct rtc_device *rtc =
59 container_of(work, struct rtc_device, uie_task);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070060 struct rtc_time tm;
61 int num = 0;
62 int err;
63
David Brownellab6a2d72007-05-08 00:33:30 -070064 err = rtc_read_time(rtc, &tm);
David Brownelld728b1e2006-11-25 11:09:28 -080065
66 local_irq_disable();
67 spin_lock(&rtc->irq_lock);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070068 if (rtc->stop_uie_polling || err) {
69 rtc->uie_task_active = 0;
70 } else if (rtc->oldsecs != tm.tm_sec) {
71 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
72 rtc->oldsecs = tm.tm_sec;
73 rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
74 rtc->uie_timer_active = 1;
75 rtc->uie_task_active = 0;
76 add_timer(&rtc->uie_timer);
77 } else if (schedule_work(&rtc->uie_task) == 0) {
78 rtc->uie_task_active = 0;
79 }
David Brownelld728b1e2006-11-25 11:09:28 -080080 spin_unlock(&rtc->irq_lock);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070081 if (num)
David Brownellab6a2d72007-05-08 00:33:30 -070082 rtc_update_irq(rtc, num, RTC_UF | RTC_IRQF);
David Brownelld728b1e2006-11-25 11:09:28 -080083 local_irq_enable();
Atsushi Nemoto655066c2006-06-25 05:48:17 -070084}
Atsushi Nemoto655066c2006-06-25 05:48:17 -070085static void rtc_uie_timer(unsigned long data)
86{
87 struct rtc_device *rtc = (struct rtc_device *)data;
88 unsigned long flags;
89
90 spin_lock_irqsave(&rtc->irq_lock, flags);
91 rtc->uie_timer_active = 0;
92 rtc->uie_task_active = 1;
93 if ((schedule_work(&rtc->uie_task) == 0))
94 rtc->uie_task_active = 0;
95 spin_unlock_irqrestore(&rtc->irq_lock, flags);
96}
97
98static void clear_uie(struct rtc_device *rtc)
99{
100 spin_lock_irq(&rtc->irq_lock);
101 if (rtc->irq_active) {
102 rtc->stop_uie_polling = 1;
103 if (rtc->uie_timer_active) {
104 spin_unlock_irq(&rtc->irq_lock);
105 del_timer_sync(&rtc->uie_timer);
106 spin_lock_irq(&rtc->irq_lock);
107 rtc->uie_timer_active = 0;
108 }
109 if (rtc->uie_task_active) {
110 spin_unlock_irq(&rtc->irq_lock);
111 flush_scheduled_work();
112 spin_lock_irq(&rtc->irq_lock);
113 }
114 rtc->irq_active = 0;
115 }
116 spin_unlock_irq(&rtc->irq_lock);
117}
118
119static int set_uie(struct rtc_device *rtc)
120{
121 struct rtc_time tm;
122 int err;
123
David Brownellab6a2d72007-05-08 00:33:30 -0700124 err = rtc_read_time(rtc, &tm);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700125 if (err)
126 return err;
127 spin_lock_irq(&rtc->irq_lock);
128 if (!rtc->irq_active) {
129 rtc->irq_active = 1;
130 rtc->stop_uie_polling = 0;
131 rtc->oldsecs = tm.tm_sec;
132 rtc->uie_task_active = 1;
133 if (schedule_work(&rtc->uie_task) == 0)
134 rtc->uie_task_active = 0;
135 }
136 rtc->irq_data = 0;
137 spin_unlock_irq(&rtc->irq_lock);
138 return 0;
139}
140#endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
Alessandro Zummoe8242902006-03-27 01:16:41 -0800141
142static ssize_t
143rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
144{
Mark Zhan88efe132007-10-16 01:28:17 -0700145 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800146
147 DECLARE_WAITQUEUE(wait, current);
148 unsigned long data;
149 ssize_t ret;
150
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700151 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
Alessandro Zummoe8242902006-03-27 01:16:41 -0800152 return -EINVAL;
153
154 add_wait_queue(&rtc->irq_queue, &wait);
155 do {
156 __set_current_state(TASK_INTERRUPTIBLE);
157
158 spin_lock_irq(&rtc->irq_lock);
159 data = rtc->irq_data;
160 rtc->irq_data = 0;
161 spin_unlock_irq(&rtc->irq_lock);
162
163 if (data != 0) {
164 ret = 0;
165 break;
166 }
167 if (file->f_flags & O_NONBLOCK) {
168 ret = -EAGAIN;
169 break;
170 }
171 if (signal_pending(current)) {
172 ret = -ERESTARTSYS;
173 break;
174 }
175 schedule();
176 } while (1);
177 set_current_state(TASK_RUNNING);
178 remove_wait_queue(&rtc->irq_queue, &wait);
179
180 if (ret == 0) {
181 /* Check for any data updates */
182 if (rtc->ops->read_callback)
David Brownellcd966202007-05-08 00:33:40 -0700183 data = rtc->ops->read_callback(rtc->dev.parent,
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700184 data);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800185
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700186 if (sizeof(int) != sizeof(long) &&
187 count == sizeof(unsigned int))
188 ret = put_user(data, (unsigned int __user *)buf) ?:
189 sizeof(unsigned int);
190 else
191 ret = put_user(data, (unsigned long __user *)buf) ?:
192 sizeof(unsigned long);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800193 }
194 return ret;
195}
196
197static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
198{
Mark Zhan88efe132007-10-16 01:28:17 -0700199 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800200 unsigned long data;
201
202 poll_wait(file, &rtc->irq_queue, wait);
203
204 data = rtc->irq_data;
205
206 return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
207}
208
209static int rtc_dev_ioctl(struct inode *inode, struct file *file,
210 unsigned int cmd, unsigned long arg)
211{
212 int err = 0;
David Brownellab6a2d72007-05-08 00:33:30 -0700213 struct rtc_device *rtc = file->private_data;
David Brownellff8371a2006-09-30 23:28:17 -0700214 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800215 struct rtc_time tm;
216 struct rtc_wkalrm alarm;
217 void __user *uarg = (void __user *) arg;
218
David Brownell2601a462006-11-25 11:09:27 -0800219 /* check that the calling task has appropriate permissions
Alessandro Zummo110d6932006-06-25 05:48:20 -0700220 * for certain ioctls. doing this check here is useful
221 * to avoid duplicate code in each driver.
222 */
223 switch (cmd) {
224 case RTC_EPOCH_SET:
225 case RTC_SET_TIME:
226 if (!capable(CAP_SYS_TIME))
227 return -EACCES;
228 break;
229
230 case RTC_IRQP_SET:
231 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
232 return -EACCES;
233 break;
234
235 case RTC_PIE_ON:
Bryan Kadzban9d013d32007-10-16 01:28:23 -0700236 if (rtc->irq_freq > rtc->max_user_freq &&
237 !capable(CAP_SYS_RESOURCE))
Alessandro Zummo110d6932006-06-25 05:48:20 -0700238 return -EACCES;
239 break;
240 }
241
Alessandro Zummoe8242902006-03-27 01:16:41 -0800242 /* try the driver's ioctl interface */
243 if (ops->ioctl) {
David Brownellcd966202007-05-08 00:33:40 -0700244 err = ops->ioctl(rtc->dev.parent, cmd, arg);
Alessandro Zummob3969e52006-05-20 15:00:29 -0700245 if (err != -ENOIOCTLCMD)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800246 return err;
247 }
248
249 /* if the driver does not provide the ioctl interface
250 * or if that particular ioctl was not implemented
Alessandro Zummob3969e52006-05-20 15:00:29 -0700251 * (-ENOIOCTLCMD), we will try to emulate here.
Alessandro Zummoe8242902006-03-27 01:16:41 -0800252 */
253
254 switch (cmd) {
255 case RTC_ALM_READ:
David Brownellab6a2d72007-05-08 00:33:30 -0700256 err = rtc_read_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800257 if (err < 0)
258 return err;
259
260 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
261 return -EFAULT;
262 break;
263
264 case RTC_ALM_SET:
265 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
266 return -EFAULT;
267
268 alarm.enabled = 0;
269 alarm.pending = 0;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800270 alarm.time.tm_wday = -1;
271 alarm.time.tm_yday = -1;
272 alarm.time.tm_isdst = -1;
David Brownellf8245c22007-05-08 00:34:07 -0700273
274 /* RTC_ALM_SET alarms may be up to 24 hours in the future.
275 * Rather than expecting every RTC to implement "don't care"
276 * for day/month/year fields, just force the alarm to have
277 * the right values for those fields.
278 *
279 * RTC_WKALM_SET should be used instead. Not only does it
280 * eliminate the need for a separate RTC_AIE_ON call, it
281 * doesn't have the "alarm 23:59:59 in the future" race.
282 *
283 * NOTE: some legacy code may have used invalid fields as
284 * wildcards, exposing hardware "periodic alarm" capabilities.
285 * Not supported here.
286 */
287 {
288 unsigned long now, then;
289
290 err = rtc_read_time(rtc, &tm);
291 if (err < 0)
292 return err;
293 rtc_tm_to_time(&tm, &now);
294
295 alarm.time.tm_mday = tm.tm_mday;
296 alarm.time.tm_mon = tm.tm_mon;
297 alarm.time.tm_year = tm.tm_year;
298 err = rtc_valid_tm(&alarm.time);
299 if (err < 0)
300 return err;
301 rtc_tm_to_time(&alarm.time, &then);
302
303 /* alarm may need to wrap into tomorrow */
304 if (then < now) {
305 rtc_time_to_tm(now + 24 * 60 * 60, &tm);
306 alarm.time.tm_mday = tm.tm_mday;
307 alarm.time.tm_mon = tm.tm_mon;
308 alarm.time.tm_year = tm.tm_year;
309 }
310 }
311
David Brownellab6a2d72007-05-08 00:33:30 -0700312 err = rtc_set_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800313 break;
314
315 case RTC_RD_TIME:
David Brownellab6a2d72007-05-08 00:33:30 -0700316 err = rtc_read_time(rtc, &tm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800317 if (err < 0)
318 return err;
319
320 if (copy_to_user(uarg, &tm, sizeof(tm)))
321 return -EFAULT;
322 break;
323
324 case RTC_SET_TIME:
Alessandro Zummoe8242902006-03-27 01:16:41 -0800325 if (copy_from_user(&tm, uarg, sizeof(tm)))
326 return -EFAULT;
327
David Brownellab6a2d72007-05-08 00:33:30 -0700328 err = rtc_set_time(rtc, &tm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800329 break;
David Brownell2601a462006-11-25 11:09:27 -0800330
Alessandro Zummod691eb92007-10-16 01:28:15 -0700331 case RTC_PIE_ON:
332 err = rtc_irq_set_state(rtc, NULL, 1);
333 break;
334
335 case RTC_PIE_OFF:
336 err = rtc_irq_set_state(rtc, NULL, 0);
David Brownell2601a462006-11-25 11:09:27 -0800337 break;
338
339 case RTC_IRQP_SET:
Alessandro Zummod691eb92007-10-16 01:28:15 -0700340 err = rtc_irq_set_freq(rtc, NULL, arg);
341 break;
342
343 case RTC_IRQP_READ:
344 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
David Brownell2601a462006-11-25 11:09:27 -0800345 break;
346
Alessandro Zummoe8242902006-03-27 01:16:41 -0800347#if 0
348 case RTC_EPOCH_SET:
349#ifndef rtc_epoch
350 /*
351 * There were no RTC clocks before 1900.
352 */
353 if (arg < 1900) {
354 err = -EINVAL;
355 break;
356 }
Alessandro Zummoe8242902006-03-27 01:16:41 -0800357 rtc_epoch = arg;
358 err = 0;
359#endif
360 break;
361
362 case RTC_EPOCH_READ:
363 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
364 break;
365#endif
366 case RTC_WKALM_SET:
367 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
368 return -EFAULT;
369
David Brownellab6a2d72007-05-08 00:33:30 -0700370 err = rtc_set_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800371 break;
372
373 case RTC_WKALM_RD:
David Brownellab6a2d72007-05-08 00:33:30 -0700374 err = rtc_read_alarm(rtc, &alarm);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800375 if (err < 0)
376 return err;
377
378 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
379 return -EFAULT;
380 break;
381
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700382#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
383 case RTC_UIE_OFF:
384 clear_uie(rtc);
385 return 0;
386
387 case RTC_UIE_ON:
388 return set_uie(rtc);
389#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800390 default:
Alessandro Zummob3969e52006-05-20 15:00:29 -0700391 err = -ENOTTY;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800392 break;
393 }
394
395 return err;
396}
397
398static int rtc_dev_release(struct inode *inode, struct file *file)
399{
Mark Zhan88efe132007-10-16 01:28:17 -0700400 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800401
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700402#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
403 clear_uie(rtc);
404#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800405 if (rtc->ops->release)
David Brownellcd966202007-05-08 00:33:40 -0700406 rtc->ops->release(rtc->dev.parent);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800407
408 mutex_unlock(&rtc->char_lock);
409 return 0;
410}
411
412static int rtc_dev_fasync(int fd, struct file *file, int on)
413{
Mark Zhan88efe132007-10-16 01:28:17 -0700414 struct rtc_device *rtc = file->private_data;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800415 return fasync_helper(fd, file, on, &rtc->async_queue);
416}
417
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800418static const struct file_operations rtc_dev_fops = {
Alessandro Zummoe8242902006-03-27 01:16:41 -0800419 .owner = THIS_MODULE,
420 .llseek = no_llseek,
421 .read = rtc_dev_read,
422 .poll = rtc_dev_poll,
423 .ioctl = rtc_dev_ioctl,
424 .open = rtc_dev_open,
425 .release = rtc_dev_release,
426 .fasync = rtc_dev_fasync,
427};
428
429/* insertion/removal hooks */
430
David Brownellcb3a58d2007-05-08 00:33:46 -0700431void rtc_dev_prepare(struct rtc_device *rtc)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800432{
David Brownell5726fb22007-05-08 00:33:27 -0700433 if (!rtc_devt)
434 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800435
436 if (rtc->id >= RTC_DEV_MAX) {
David Brownell5726fb22007-05-08 00:33:27 -0700437 pr_debug("%s: too many RTC devices\n", rtc->name);
438 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800439 }
440
David Brownellcd966202007-05-08 00:33:40 -0700441 rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
David Brownell5726fb22007-05-08 00:33:27 -0700442
Alessandro Zummoe8242902006-03-27 01:16:41 -0800443 mutex_init(&rtc->char_lock);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700444#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
David Howellsc4028952006-11-22 14:57:56 +0000445 INIT_WORK(&rtc->uie_task, rtc_uie_task);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700446 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
447#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800448
449 cdev_init(&rtc->char_dev, &rtc_dev_fops);
450 rtc->char_dev.owner = rtc->owner;
David Brownellcb3a58d2007-05-08 00:33:46 -0700451}
Alessandro Zummoe8242902006-03-27 01:16:41 -0800452
David Brownellcb3a58d2007-05-08 00:33:46 -0700453void rtc_dev_add_device(struct rtc_device *rtc)
454{
David Brownellcd966202007-05-08 00:33:40 -0700455 if (cdev_add(&rtc->char_dev, rtc->dev.devt, 1))
David Brownell5726fb22007-05-08 00:33:27 -0700456 printk(KERN_WARNING "%s: failed to add char device %d:%d\n",
457 rtc->name, MAJOR(rtc_devt), rtc->id);
458 else
459 pr_debug("%s: dev (%d:%d)\n", rtc->name,
Alessandro Zummoe8242902006-03-27 01:16:41 -0800460 MAJOR(rtc_devt), rtc->id);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800461}
462
David Brownell5726fb22007-05-08 00:33:27 -0700463void rtc_dev_del_device(struct rtc_device *rtc)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800464{
David Brownellcd966202007-05-08 00:33:40 -0700465 if (rtc->dev.devt)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800466 cdev_del(&rtc->char_dev);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800467}
468
David Brownell5726fb22007-05-08 00:33:27 -0700469void __init rtc_dev_init(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800470{
471 int err;
472
Alessandro Zummoe8242902006-03-27 01:16:41 -0800473 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
David Brownell5726fb22007-05-08 00:33:27 -0700474 if (err < 0)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800475 printk(KERN_ERR "%s: failed to allocate char dev region\n",
476 __FILE__);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800477}
478
David Brownell5726fb22007-05-08 00:33:27 -0700479void __exit rtc_dev_exit(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800480{
David Brownell5726fb22007-05-08 00:33:27 -0700481 if (rtc_devt)
482 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800483}