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
)
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
)))
35 file
->private_data
= &rtc
->class_dev
;
37 err
= ops
->open
? ops
->open(rtc
->class_dev
.dev
) : 0;
39 spin_lock_irq(&rtc
->irq_lock
);
41 spin_unlock_irq(&rtc
->irq_lock
);
46 /* something has gone wrong, release the lock */
47 mutex_unlock(&rtc
->char_lock
);
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(struct work_struct
*work
)
58 struct rtc_device
*rtc
=
59 container_of(work
, struct rtc_device
, uie_task
);
64 err
= rtc_read_time(&rtc
->class_dev
, &tm
);
67 spin_lock(&rtc
->irq_lock
);
68 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;
80 spin_unlock(&rtc
->irq_lock
);
82 rtc_update_irq(&rtc
->class_dev
, num
, RTC_UF
| RTC_IRQF
);
85 static void rtc_uie_timer(unsigned long data
)
87 struct rtc_device
*rtc
= (struct rtc_device
*)data
;
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
);
98 static void clear_uie(struct rtc_device
*rtc
)
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;
109 if (rtc
->uie_task_active
) {
110 spin_unlock_irq(&rtc
->irq_lock
);
111 flush_scheduled_work();
112 spin_lock_irq(&rtc
->irq_lock
);
116 spin_unlock_irq(&rtc
->irq_lock
);
119 static int set_uie(struct rtc_device
*rtc
)
124 err
= rtc_read_time(&rtc
->class_dev
, &tm
);
127 spin_lock_irq(&rtc
->irq_lock
);
128 if (!rtc
->irq_active
) {
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;
137 spin_unlock_irq(&rtc
->irq_lock
);
140 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
143 rtc_dev_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
145 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
147 DECLARE_WAITQUEUE(wait
, current
);
151 if (count
!= sizeof(unsigned int) && count
< sizeof(unsigned long))
154 add_wait_queue(&rtc
->irq_queue
, &wait
);
156 __set_current_state(TASK_INTERRUPTIBLE
);
158 spin_lock_irq(&rtc
->irq_lock
);
159 data
= rtc
->irq_data
;
161 spin_unlock_irq(&rtc
->irq_lock
);
167 if (file
->f_flags
& O_NONBLOCK
) {
171 if (signal_pending(current
)) {
177 set_current_state(TASK_RUNNING
);
178 remove_wait_queue(&rtc
->irq_queue
, &wait
);
181 /* Check for any data updates */
182 if (rtc
->ops
->read_callback
)
183 data
= rtc
->ops
->read_callback(rtc
->class_dev
.dev
,
186 if (sizeof(int) != sizeof(long) &&
187 count
== sizeof(unsigned int))
188 ret
= put_user(data
, (unsigned int __user
*)buf
) ?:
189 sizeof(unsigned int);
191 ret
= put_user(data
, (unsigned long __user
*)buf
) ?:
192 sizeof(unsigned long);
197 static unsigned int rtc_dev_poll(struct file
*file
, poll_table
*wait
)
199 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
202 poll_wait(file
, &rtc
->irq_queue
, wait
);
204 data
= rtc
->irq_data
;
206 return (data
!= 0) ? (POLLIN
| POLLRDNORM
) : 0;
209 static int rtc_dev_ioctl(struct inode
*inode
, struct file
*file
,
210 unsigned int cmd
, unsigned long arg
)
213 struct class_device
*class_dev
= file
->private_data
;
214 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
215 const struct rtc_class_ops
*ops
= rtc
->ops
;
217 struct rtc_wkalrm alarm
;
218 void __user
*uarg
= (void __user
*) arg
;
220 /* check that the calling task has appropriate permissions
221 * for certain ioctls. doing this check here is useful
222 * to avoid duplicate code in each driver.
227 if (!capable(CAP_SYS_TIME
))
232 if (arg
> rtc
->max_user_freq
&& !capable(CAP_SYS_RESOURCE
))
237 if (!capable(CAP_SYS_RESOURCE
))
242 /* avoid conflicting IRQ users */
243 if (cmd
== RTC_PIE_ON
|| cmd
== RTC_PIE_OFF
|| cmd
== RTC_IRQP_SET
) {
244 spin_lock_irq(&rtc
->irq_task_lock
);
247 spin_unlock_irq(&rtc
->irq_task_lock
);
253 /* try the driver's ioctl interface */
255 err
= ops
->ioctl(class_dev
->dev
, cmd
, arg
);
256 if (err
!= -ENOIOCTLCMD
)
260 /* if the driver does not provide the ioctl interface
261 * or if that particular ioctl was not implemented
262 * (-ENOIOCTLCMD), we will try to emulate here.
267 err
= rtc_read_alarm(class_dev
, &alarm
);
271 if (copy_to_user(uarg
, &alarm
.time
, sizeof(tm
)))
276 if (copy_from_user(&alarm
.time
, uarg
, sizeof(tm
)))
281 alarm
.time
.tm_mday
= -1;
282 alarm
.time
.tm_mon
= -1;
283 alarm
.time
.tm_year
= -1;
284 alarm
.time
.tm_wday
= -1;
285 alarm
.time
.tm_yday
= -1;
286 alarm
.time
.tm_isdst
= -1;
287 err
= rtc_set_alarm(class_dev
, &alarm
);
291 err
= rtc_read_time(class_dev
, &tm
);
295 if (copy_to_user(uarg
, &tm
, sizeof(tm
)))
300 if (copy_from_user(&tm
, uarg
, sizeof(tm
)))
303 err
= rtc_set_time(class_dev
, &tm
);
307 if (ops
->irq_set_freq
)
308 err
= put_user(rtc
->irq_freq
, (unsigned long *) arg
);
312 if (ops
->irq_set_freq
)
313 err
= rtc_irq_set_freq(class_dev
, rtc
->irq_task
, arg
);
320 * There were no RTC clocks before 1900.
332 err
= put_user(rtc_epoch
, (unsigned long __user
*)uarg
);
336 if (copy_from_user(&alarm
, uarg
, sizeof(alarm
)))
339 err
= rtc_set_alarm(class_dev
, &alarm
);
343 err
= rtc_read_alarm(class_dev
, &alarm
);
347 if (copy_to_user(uarg
, &alarm
, sizeof(alarm
)))
351 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
367 static int rtc_dev_release(struct inode
*inode
, struct file
*file
)
369 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
371 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
374 if (rtc
->ops
->release
)
375 rtc
->ops
->release(rtc
->class_dev
.dev
);
377 mutex_unlock(&rtc
->char_lock
);
381 static int rtc_dev_fasync(int fd
, struct file
*file
, int on
)
383 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
384 return fasync_helper(fd
, file
, on
, &rtc
->async_queue
);
387 static struct file_operations rtc_dev_fops
= {
388 .owner
= THIS_MODULE
,
390 .read
= rtc_dev_read
,
391 .poll
= rtc_dev_poll
,
392 .ioctl
= rtc_dev_ioctl
,
393 .open
= rtc_dev_open
,
394 .release
= rtc_dev_release
,
395 .fasync
= rtc_dev_fasync
,
398 /* insertion/removal hooks */
400 static int rtc_dev_add_device(struct class_device
*class_dev
,
401 struct class_interface
*class_intf
)
404 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
406 if (rtc
->id
>= RTC_DEV_MAX
) {
407 dev_err(class_dev
->dev
, "too many RTCs\n");
411 mutex_init(&rtc
->char_lock
);
412 spin_lock_init(&rtc
->irq_lock
);
413 init_waitqueue_head(&rtc
->irq_queue
);
414 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
415 INIT_WORK(&rtc
->uie_task
, rtc_uie_task
);
416 setup_timer(&rtc
->uie_timer
, rtc_uie_timer
, (unsigned long)rtc
);
419 cdev_init(&rtc
->char_dev
, &rtc_dev_fops
);
420 rtc
->char_dev
.owner
= rtc
->owner
;
422 if (cdev_add(&rtc
->char_dev
, MKDEV(MAJOR(rtc_devt
), rtc
->id
), 1)) {
423 dev_err(class_dev
->dev
,
424 "failed to add char device %d:%d\n",
425 MAJOR(rtc_devt
), rtc
->id
);
429 rtc
->rtc_dev
= class_device_create(rtc_dev_class
, NULL
,
430 MKDEV(MAJOR(rtc_devt
), rtc
->id
),
431 class_dev
->dev
, "rtc%d", rtc
->id
);
432 if (IS_ERR(rtc
->rtc_dev
)) {
433 dev_err(class_dev
->dev
, "cannot create rtc_dev device\n");
434 err
= PTR_ERR(rtc
->rtc_dev
);
438 dev_info(class_dev
->dev
, "rtc intf: dev (%d:%d)\n",
439 MAJOR(rtc
->rtc_dev
->devt
),
440 MINOR(rtc
->rtc_dev
->devt
));
446 cdev_del(&rtc
->char_dev
);
450 static void rtc_dev_remove_device(struct class_device
*class_dev
,
451 struct class_interface
*class_intf
)
453 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
456 dev_dbg(class_dev
->dev
, "removing char %d:%d\n",
457 MAJOR(rtc
->rtc_dev
->devt
),
458 MINOR(rtc
->rtc_dev
->devt
));
460 class_device_unregister(rtc
->rtc_dev
);
461 cdev_del(&rtc
->char_dev
);
465 /* interface registration */
467 static struct class_interface rtc_dev_interface
= {
468 .add
= &rtc_dev_add_device
,
469 .remove
= &rtc_dev_remove_device
,
472 static int __init
rtc_dev_init(void)
476 rtc_dev_class
= class_create(THIS_MODULE
, "rtc-dev");
477 if (IS_ERR(rtc_dev_class
))
478 return PTR_ERR(rtc_dev_class
);
480 err
= alloc_chrdev_region(&rtc_devt
, 0, RTC_DEV_MAX
, "rtc");
482 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
484 goto err_destroy_class
;
487 err
= rtc_interface_register(&rtc_dev_interface
);
489 printk(KERN_ERR
"%s: failed to register the interface\n",
491 goto err_unregister_chrdev
;
496 err_unregister_chrdev
:
497 unregister_chrdev_region(rtc_devt
, RTC_DEV_MAX
);
500 class_destroy(rtc_dev_class
);
505 static void __exit
rtc_dev_exit(void)
507 class_interface_unregister(&rtc_dev_interface
);
508 class_destroy(rtc_dev_class
);
509 unregister_chrdev_region(rtc_devt
, RTC_DEV_MAX
);
512 subsys_initcall(rtc_dev_init
);
513 module_exit(rtc_dev_exit
);
515 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
516 MODULE_DESCRIPTION("RTC class dev interface");
517 MODULE_LICENSE("GPL");