MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / rtc / rtc-dev.c
blob814b9e1873f55ef8061b4abfc66ab5776da38dc1
1 /*
2 * RTC subsystem, dev interface
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
7 * based on arch/arm/common/rtctime.c
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.
14 #include <linux/module.h>
15 #include <linux/rtc.h>
17 static struct class *rtc_dev_class;
18 static dev_t rtc_devt;
20 #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
22 static int rtc_dev_open(struct inode *inode, struct file *file)
24 int err;
25 struct rtc_device *rtc = container_of(inode->i_cdev,
26 struct rtc_device, char_dev);
27 const struct rtc_class_ops *ops = rtc->ops;
29 /* We keep the lock as long as the device is in use
30 * and return immediately if busy
32 if (!(mutex_trylock(&rtc->char_lock)))
33 return -EBUSY;
35 file->private_data = &rtc->class_dev;
37 err = ops->open ? ops->open(rtc->class_dev.dev) : 0;
38 if (err == 0) {
39 spin_lock_irq(&rtc->irq_lock);
40 rtc->irq_data = 0;
41 spin_unlock_irq(&rtc->irq_lock);
43 return 0;
46 /* something has gone wrong, release the lock */
47 mutex_unlock(&rtc->char_lock);
48 return err;
51 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
53 * Routine to poll RTC seconds field for change as often as possible,
54 * after first RTC_UIE use timer to reduce polling
56 static void rtc_uie_task(void *data)
58 struct rtc_device *rtc = data;
59 struct rtc_time tm;
60 int num = 0;
61 int err;
63 err = rtc_read_time(&rtc->class_dev, &tm);
65 local_irq_disable();
66 spin_lock(&rtc->irq_lock);
67 if (rtc->stop_uie_polling || err) {
68 rtc->uie_task_active = 0;
69 } else if (rtc->oldsecs != tm.tm_sec) {
70 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
71 rtc->oldsecs = tm.tm_sec;
72 rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
73 rtc->uie_timer_active = 1;
74 rtc->uie_task_active = 0;
75 add_timer(&rtc->uie_timer);
76 } else if (schedule_work(&rtc->uie_task) == 0) {
77 rtc->uie_task_active = 0;
79 spin_unlock(&rtc->irq_lock);
80 if (num)
81 rtc_update_irq(&rtc->class_dev, num, RTC_UF | RTC_IRQF);
82 local_irq_enable();
84 static void rtc_uie_timer(unsigned long data)
86 struct rtc_device *rtc = (struct rtc_device *)data;
87 unsigned long flags;
89 spin_lock_irqsave(&rtc->irq_lock, flags);
90 rtc->uie_timer_active = 0;
91 rtc->uie_task_active = 1;
92 if ((schedule_work(&rtc->uie_task) == 0))
93 rtc->uie_task_active = 0;
94 spin_unlock_irqrestore(&rtc->irq_lock, flags);
97 static void clear_uie(struct rtc_device *rtc)
99 spin_lock_irq(&rtc->irq_lock);
100 if (rtc->irq_active) {
101 rtc->stop_uie_polling = 1;
102 if (rtc->uie_timer_active) {
103 spin_unlock_irq(&rtc->irq_lock);
104 del_timer_sync(&rtc->uie_timer);
105 spin_lock_irq(&rtc->irq_lock);
106 rtc->uie_timer_active = 0;
108 if (rtc->uie_task_active) {
109 spin_unlock_irq(&rtc->irq_lock);
110 flush_scheduled_work();
111 spin_lock_irq(&rtc->irq_lock);
113 rtc->irq_active = 0;
115 spin_unlock_irq(&rtc->irq_lock);
118 static int set_uie(struct rtc_device *rtc)
120 struct rtc_time tm;
121 int err;
123 err = rtc_read_time(&rtc->class_dev, &tm);
124 if (err)
125 return err;
126 spin_lock_irq(&rtc->irq_lock);
127 if (!rtc->irq_active) {
128 rtc->irq_active = 1;
129 rtc->stop_uie_polling = 0;
130 rtc->oldsecs = tm.tm_sec;
131 rtc->uie_task_active = 1;
132 if (schedule_work(&rtc->uie_task) == 0)
133 rtc->uie_task_active = 0;
135 rtc->irq_data = 0;
136 spin_unlock_irq(&rtc->irq_lock);
137 return 0;
139 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
141 static ssize_t
142 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
144 struct rtc_device *rtc = to_rtc_device(file->private_data);
146 DECLARE_WAITQUEUE(wait, current);
147 unsigned long data;
148 ssize_t ret;
150 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
151 return -EINVAL;
153 add_wait_queue(&rtc->irq_queue, &wait);
154 do {
155 __set_current_state(TASK_INTERRUPTIBLE);
157 spin_lock_irq(&rtc->irq_lock);
158 data = rtc->irq_data;
159 rtc->irq_data = 0;
160 spin_unlock_irq(&rtc->irq_lock);
162 if (data != 0) {
163 ret = 0;
164 break;
166 if (file->f_flags & O_NONBLOCK) {
167 ret = -EAGAIN;
168 break;
170 if (signal_pending(current)) {
171 ret = -ERESTARTSYS;
172 break;
174 schedule();
175 } while (1);
176 set_current_state(TASK_RUNNING);
177 remove_wait_queue(&rtc->irq_queue, &wait);
179 if (ret == 0) {
180 /* Check for any data updates */
181 if (rtc->ops->read_callback)
182 data = rtc->ops->read_callback(rtc->class_dev.dev,
183 data);
185 if (sizeof(int) != sizeof(long) &&
186 count == sizeof(unsigned int))
187 ret = put_user(data, (unsigned int __user *)buf) ?:
188 sizeof(unsigned int);
189 else
190 ret = put_user(data, (unsigned long __user *)buf) ?:
191 sizeof(unsigned long);
193 return ret;
196 static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
198 struct rtc_device *rtc = to_rtc_device(file->private_data);
199 unsigned long data;
201 poll_wait(file, &rtc->irq_queue, wait);
203 data = rtc->irq_data;
205 return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
208 static int rtc_dev_ioctl(struct inode *inode, struct file *file,
209 unsigned int cmd, unsigned long arg)
211 int err = 0;
212 struct class_device *class_dev = file->private_data;
213 struct rtc_device *rtc = to_rtc_device(class_dev);
214 const struct rtc_class_ops *ops = rtc->ops;
215 struct rtc_time tm;
216 struct rtc_wkalrm alarm;
217 void __user *uarg = (void __user *) arg;
219 /* check that the calling task has appropriate permissions
220 * for certain ioctls. doing this check here is useful
221 * to avoid duplicate code in each driver.
223 switch (cmd) {
224 case RTC_EPOCH_SET:
225 case RTC_SET_TIME:
226 if (!capable(CAP_SYS_TIME))
227 return -EACCES;
228 break;
230 case RTC_IRQP_SET:
231 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
232 return -EACCES;
233 break;
235 case RTC_PIE_ON:
236 if (!capable(CAP_SYS_RESOURCE))
237 return -EACCES;
238 break;
241 /* avoid conflicting IRQ users */
242 if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) {
243 spin_lock_irq(&rtc->irq_task_lock);
244 if (rtc->irq_task)
245 err = -EBUSY;
246 spin_unlock_irq(&rtc->irq_task_lock);
248 if (err < 0)
249 return err;
252 /* try the driver's ioctl interface */
253 if (ops->ioctl) {
254 err = ops->ioctl(class_dev->dev, cmd, arg);
255 if (err != -ENOIOCTLCMD)
256 return err;
259 /* if the driver does not provide the ioctl interface
260 * or if that particular ioctl was not implemented
261 * (-ENOIOCTLCMD), we will try to emulate here.
264 switch (cmd) {
265 case RTC_ALM_READ:
266 err = rtc_read_alarm(class_dev, &alarm);
267 if (err < 0)
268 return err;
270 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
271 return -EFAULT;
272 break;
274 case RTC_ALM_SET:
275 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
276 return -EFAULT;
278 alarm.enabled = 0;
279 alarm.pending = 0;
280 alarm.time.tm_mday = -1;
281 alarm.time.tm_mon = -1;
282 alarm.time.tm_year = -1;
283 alarm.time.tm_wday = -1;
284 alarm.time.tm_yday = -1;
285 alarm.time.tm_isdst = -1;
286 err = rtc_set_alarm(class_dev, &alarm);
287 break;
289 case RTC_RD_TIME:
290 err = rtc_read_time(class_dev, &tm);
291 if (err < 0)
292 return err;
294 if (copy_to_user(uarg, &tm, sizeof(tm)))
295 return -EFAULT;
296 break;
298 case RTC_SET_TIME:
299 if (copy_from_user(&tm, uarg, sizeof(tm)))
300 return -EFAULT;
302 err = rtc_set_time(class_dev, &tm);
303 break;
305 case RTC_IRQP_READ:
306 if (ops->irq_set_freq)
307 err = put_user(rtc->irq_freq, (unsigned long *) arg);
308 break;
310 case RTC_IRQP_SET:
311 if (ops->irq_set_freq)
312 err = rtc_irq_set_freq(class_dev, rtc->irq_task, arg);
313 break;
315 #if 0
316 case RTC_EPOCH_SET:
317 #ifndef rtc_epoch
319 * There were no RTC clocks before 1900.
321 if (arg < 1900) {
322 err = -EINVAL;
323 break;
325 rtc_epoch = arg;
326 err = 0;
327 #endif
328 break;
330 case RTC_EPOCH_READ:
331 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
332 break;
333 #endif
334 case RTC_WKALM_SET:
335 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
336 return -EFAULT;
338 err = rtc_set_alarm(class_dev, &alarm);
339 break;
341 case RTC_WKALM_RD:
342 err = rtc_read_alarm(class_dev, &alarm);
343 if (err < 0)
344 return err;
346 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
347 return -EFAULT;
348 break;
350 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
351 case RTC_UIE_OFF:
352 clear_uie(rtc);
353 return 0;
355 case RTC_UIE_ON:
356 return set_uie(rtc);
357 #endif
358 default:
359 err = -ENOTTY;
360 break;
363 return err;
366 static int rtc_dev_release(struct inode *inode, struct file *file)
368 struct rtc_device *rtc = to_rtc_device(file->private_data);
370 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
371 clear_uie(rtc);
372 #endif
373 if (rtc->ops->release)
374 rtc->ops->release(rtc->class_dev.dev);
376 mutex_unlock(&rtc->char_lock);
377 return 0;
380 static int rtc_dev_fasync(int fd, struct file *file, int on)
382 struct rtc_device *rtc = to_rtc_device(file->private_data);
383 return fasync_helper(fd, file, on, &rtc->async_queue);
386 static struct file_operations rtc_dev_fops = {
387 .owner = THIS_MODULE,
388 .llseek = no_llseek,
389 .read = rtc_dev_read,
390 .poll = rtc_dev_poll,
391 .ioctl = rtc_dev_ioctl,
392 .open = rtc_dev_open,
393 .release = rtc_dev_release,
394 .fasync = rtc_dev_fasync,
397 /* insertion/removal hooks */
399 static int rtc_dev_add_device(struct class_device *class_dev,
400 struct class_interface *class_intf)
402 int err = 0;
403 struct rtc_device *rtc = to_rtc_device(class_dev);
405 if (rtc->id >= RTC_DEV_MAX) {
406 dev_err(class_dev->dev, "too many RTCs\n");
407 return -EINVAL;
410 mutex_init(&rtc->char_lock);
411 spin_lock_init(&rtc->irq_lock);
412 init_waitqueue_head(&rtc->irq_queue);
413 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
414 INIT_WORK(&rtc->uie_task, rtc_uie_task, rtc);
415 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
416 #endif
418 cdev_init(&rtc->char_dev, &rtc_dev_fops);
419 rtc->char_dev.owner = rtc->owner;
421 if (cdev_add(&rtc->char_dev, MKDEV(MAJOR(rtc_devt), rtc->id), 1)) {
422 dev_err(class_dev->dev,
423 "failed to add char device %d:%d\n",
424 MAJOR(rtc_devt), rtc->id);
425 return -ENODEV;
428 rtc->rtc_dev = class_device_create(rtc_dev_class, NULL,
429 MKDEV(MAJOR(rtc_devt), rtc->id),
430 class_dev->dev, "rtc%d", rtc->id);
431 if (IS_ERR(rtc->rtc_dev)) {
432 dev_err(class_dev->dev, "cannot create rtc_dev device\n");
433 err = PTR_ERR(rtc->rtc_dev);
434 goto err_cdev_del;
437 dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n",
438 MAJOR(rtc->rtc_dev->devt),
439 MINOR(rtc->rtc_dev->devt));
441 return 0;
443 err_cdev_del:
445 cdev_del(&rtc->char_dev);
446 return err;
449 static void rtc_dev_remove_device(struct class_device *class_dev,
450 struct class_interface *class_intf)
452 struct rtc_device *rtc = to_rtc_device(class_dev);
454 if (rtc->rtc_dev) {
455 dev_dbg(class_dev->dev, "removing char %d:%d\n",
456 MAJOR(rtc->rtc_dev->devt),
457 MINOR(rtc->rtc_dev->devt));
459 class_device_unregister(rtc->rtc_dev);
460 cdev_del(&rtc->char_dev);
464 /* interface registration */
466 static struct class_interface rtc_dev_interface = {
467 .add = &rtc_dev_add_device,
468 .remove = &rtc_dev_remove_device,
471 static int __init rtc_dev_init(void)
473 int err;
475 rtc_dev_class = class_create(THIS_MODULE, "rtc-dev");
476 if (IS_ERR(rtc_dev_class))
477 return PTR_ERR(rtc_dev_class);
479 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
480 if (err < 0) {
481 printk(KERN_ERR "%s: failed to allocate char dev region\n",
482 __FILE__);
483 goto err_destroy_class;
486 err = rtc_interface_register(&rtc_dev_interface);
487 if (err < 0) {
488 printk(KERN_ERR "%s: failed to register the interface\n",
489 __FILE__);
490 goto err_unregister_chrdev;
493 return 0;
495 err_unregister_chrdev:
496 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
498 err_destroy_class:
499 class_destroy(rtc_dev_class);
501 return err;
504 static void __exit rtc_dev_exit(void)
506 class_interface_unregister(&rtc_dev_interface);
507 class_destroy(rtc_dev_class);
508 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
511 subsys_initcall(rtc_dev_init);
512 module_exit(rtc_dev_exit);
514 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
515 MODULE_DESCRIPTION("RTC class dev interface");
516 MODULE_LICENSE("GPL");