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(void *data
)
58 struct rtc_device
*rtc
= data
;
63 err
= rtc_read_time(&rtc
->class_dev
, &tm
);
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
);
81 rtc_update_irq(&rtc
->class_dev
, num
, RTC_UF
| RTC_IRQF
);
84 static void rtc_uie_timer(unsigned long data
)
86 struct rtc_device
*rtc
= (struct rtc_device
*)data
;
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
);
115 spin_unlock_irq(&rtc
->irq_lock
);
118 static int set_uie(struct rtc_device
*rtc
)
123 err
= rtc_read_time(&rtc
->class_dev
, &tm
);
126 spin_lock_irq(&rtc
->irq_lock
);
127 if (!rtc
->irq_active
) {
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;
136 spin_unlock_irq(&rtc
->irq_lock
);
139 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
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
);
150 if (count
!= sizeof(unsigned int) && count
< sizeof(unsigned long))
153 add_wait_queue(&rtc
->irq_queue
, &wait
);
155 __set_current_state(TASK_INTERRUPTIBLE
);
157 spin_lock_irq(&rtc
->irq_lock
);
158 data
= rtc
->irq_data
;
160 spin_unlock_irq(&rtc
->irq_lock
);
166 if (file
->f_flags
& O_NONBLOCK
) {
170 if (signal_pending(current
)) {
176 set_current_state(TASK_RUNNING
);
177 remove_wait_queue(&rtc
->irq_queue
, &wait
);
180 /* Check for any data updates */
181 if (rtc
->ops
->read_callback
)
182 data
= rtc
->ops
->read_callback(rtc
->class_dev
.dev
,
185 if (sizeof(int) != sizeof(long) &&
186 count
== sizeof(unsigned int))
187 ret
= put_user(data
, (unsigned int __user
*)buf
) ?:
188 sizeof(unsigned int);
190 ret
= put_user(data
, (unsigned long __user
*)buf
) ?:
191 sizeof(unsigned long);
196 static unsigned int rtc_dev_poll(struct file
*file
, poll_table
*wait
)
198 struct rtc_device
*rtc
= to_rtc_device(file
->private_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
)
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
;
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.
226 if (!capable(CAP_SYS_TIME
))
231 if (arg
> rtc
->max_user_freq
&& !capable(CAP_SYS_RESOURCE
))
236 if (!capable(CAP_SYS_RESOURCE
))
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
);
246 spin_unlock_irq(&rtc
->irq_task_lock
);
252 /* try the driver's ioctl interface */
254 err
= ops
->ioctl(class_dev
->dev
, cmd
, arg
);
255 if (err
!= -ENOIOCTLCMD
)
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.
266 err
= rtc_read_alarm(class_dev
, &alarm
);
270 if (copy_to_user(uarg
, &alarm
.time
, sizeof(tm
)))
275 if (copy_from_user(&alarm
.time
, uarg
, sizeof(tm
)))
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
);
290 err
= rtc_read_time(class_dev
, &tm
);
294 if (copy_to_user(uarg
, &tm
, sizeof(tm
)))
299 if (copy_from_user(&tm
, uarg
, sizeof(tm
)))
302 err
= rtc_set_time(class_dev
, &tm
);
306 if (ops
->irq_set_freq
)
307 err
= put_user(rtc
->irq_freq
, (unsigned long *) arg
);
311 if (ops
->irq_set_freq
)
312 err
= rtc_irq_set_freq(class_dev
, rtc
->irq_task
, arg
);
319 * There were no RTC clocks before 1900.
331 err
= put_user(rtc_epoch
, (unsigned long __user
*)uarg
);
335 if (copy_from_user(&alarm
, uarg
, sizeof(alarm
)))
338 err
= rtc_set_alarm(class_dev
, &alarm
);
342 err
= rtc_read_alarm(class_dev
, &alarm
);
346 if (copy_to_user(uarg
, &alarm
, sizeof(alarm
)))
350 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
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
373 if (rtc
->ops
->release
)
374 rtc
->ops
->release(rtc
->class_dev
.dev
);
376 mutex_unlock(&rtc
->char_lock
);
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
,
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
)
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");
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
);
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
);
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
);
437 dev_info(class_dev
->dev
, "rtc intf: dev (%d:%d)\n",
438 MAJOR(rtc
->rtc_dev
->devt
),
439 MINOR(rtc
->rtc_dev
->devt
));
445 cdev_del(&rtc
->char_dev
);
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
);
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)
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");
481 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
483 goto err_destroy_class
;
486 err
= rtc_interface_register(&rtc_dev_interface
);
488 printk(KERN_ERR
"%s: failed to register the interface\n",
490 goto err_unregister_chrdev
;
495 err_unregister_chrdev
:
496 unregister_chrdev_region(rtc_devt
, RTC_DEV_MAX
);
499 class_destroy(rtc_dev_class
);
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");