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 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
);
53 rtc_dev_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*ppos
)
55 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
57 DECLARE_WAITQUEUE(wait
, current
);
61 if (count
!= sizeof(unsigned int) && count
< sizeof(unsigned long))
64 add_wait_queue(&rtc
->irq_queue
, &wait
);
66 __set_current_state(TASK_INTERRUPTIBLE
);
68 spin_lock_irq(&rtc
->irq_lock
);
71 spin_unlock_irq(&rtc
->irq_lock
);
77 if (file
->f_flags
& O_NONBLOCK
) {
81 if (signal_pending(current
)) {
87 set_current_state(TASK_RUNNING
);
88 remove_wait_queue(&rtc
->irq_queue
, &wait
);
91 /* Check for any data updates */
92 if (rtc
->ops
->read_callback
)
93 data
= rtc
->ops
->read_callback(rtc
->class_dev
.dev
,
96 if (sizeof(int) != sizeof(long) &&
97 count
== sizeof(unsigned int))
98 ret
= put_user(data
, (unsigned int __user
*)buf
) ?:
101 ret
= put_user(data
, (unsigned long __user
*)buf
) ?:
102 sizeof(unsigned long);
107 static unsigned int rtc_dev_poll(struct file
*file
, poll_table
*wait
)
109 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
112 poll_wait(file
, &rtc
->irq_queue
, wait
);
114 data
= rtc
->irq_data
;
116 return (data
!= 0) ? (POLLIN
| POLLRDNORM
) : 0;
119 static int rtc_dev_ioctl(struct inode
*inode
, struct file
*file
,
120 unsigned int cmd
, unsigned long arg
)
123 struct class_device
*class_dev
= file
->private_data
;
124 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
125 struct rtc_class_ops
*ops
= rtc
->ops
;
127 struct rtc_wkalrm alarm
;
128 void __user
*uarg
= (void __user
*) arg
;
130 /* avoid conflicting IRQ users */
131 if (cmd
== RTC_PIE_ON
|| cmd
== RTC_PIE_OFF
|| cmd
== RTC_IRQP_SET
) {
132 spin_lock(&rtc
->irq_task_lock
);
135 spin_unlock(&rtc
->irq_task_lock
);
141 /* try the driver's ioctl interface */
143 err
= ops
->ioctl(class_dev
->dev
, cmd
, arg
);
144 if (err
!= -ENOIOCTLCMD
)
148 /* if the driver does not provide the ioctl interface
149 * or if that particular ioctl was not implemented
150 * (-ENOIOCTLCMD), we will try to emulate here.
155 err
= rtc_read_alarm(class_dev
, &alarm
);
159 if (copy_to_user(uarg
, &alarm
.time
, sizeof(tm
)))
164 if (copy_from_user(&alarm
.time
, uarg
, sizeof(tm
)))
169 alarm
.time
.tm_mday
= -1;
170 alarm
.time
.tm_mon
= -1;
171 alarm
.time
.tm_year
= -1;
172 alarm
.time
.tm_wday
= -1;
173 alarm
.time
.tm_yday
= -1;
174 alarm
.time
.tm_isdst
= -1;
175 err
= rtc_set_alarm(class_dev
, &alarm
);
179 err
= rtc_read_time(class_dev
, &tm
);
183 if (copy_to_user(uarg
, &tm
, sizeof(tm
)))
188 if (!capable(CAP_SYS_TIME
))
191 if (copy_from_user(&tm
, uarg
, sizeof(tm
)))
194 err
= rtc_set_time(class_dev
, &tm
);
200 * There were no RTC clocks before 1900.
206 if (!capable(CAP_SYS_TIME
)) {
216 err
= put_user(rtc_epoch
, (unsigned long __user
*)uarg
);
220 if (copy_from_user(&alarm
, uarg
, sizeof(alarm
)))
223 err
= rtc_set_alarm(class_dev
, &alarm
);
227 err
= rtc_read_alarm(class_dev
, &alarm
);
231 if (copy_to_user(uarg
, &alarm
, sizeof(alarm
)))
243 static int rtc_dev_release(struct inode
*inode
, struct file
*file
)
245 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
247 if (rtc
->ops
->release
)
248 rtc
->ops
->release(rtc
->class_dev
.dev
);
250 mutex_unlock(&rtc
->char_lock
);
254 static int rtc_dev_fasync(int fd
, struct file
*file
, int on
)
256 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
257 return fasync_helper(fd
, file
, on
, &rtc
->async_queue
);
260 static struct file_operations rtc_dev_fops
= {
261 .owner
= THIS_MODULE
,
263 .read
= rtc_dev_read
,
264 .poll
= rtc_dev_poll
,
265 .ioctl
= rtc_dev_ioctl
,
266 .open
= rtc_dev_open
,
267 .release
= rtc_dev_release
,
268 .fasync
= rtc_dev_fasync
,
271 /* insertion/removal hooks */
273 static int rtc_dev_add_device(struct class_device
*class_dev
,
274 struct class_interface
*class_intf
)
277 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
279 if (rtc
->id
>= RTC_DEV_MAX
) {
280 dev_err(class_dev
->dev
, "too many RTCs\n");
284 mutex_init(&rtc
->char_lock
);
285 spin_lock_init(&rtc
->irq_lock
);
286 init_waitqueue_head(&rtc
->irq_queue
);
288 cdev_init(&rtc
->char_dev
, &rtc_dev_fops
);
289 rtc
->char_dev
.owner
= rtc
->owner
;
291 if (cdev_add(&rtc
->char_dev
, MKDEV(MAJOR(rtc_devt
), rtc
->id
), 1)) {
292 cdev_del(&rtc
->char_dev
);
293 dev_err(class_dev
->dev
,
294 "failed to add char device %d:%d\n",
295 MAJOR(rtc_devt
), rtc
->id
);
299 rtc
->rtc_dev
= class_device_create(rtc_dev_class
, NULL
,
300 MKDEV(MAJOR(rtc_devt
), rtc
->id
),
301 class_dev
->dev
, "rtc%d", rtc
->id
);
302 if (IS_ERR(rtc
->rtc_dev
)) {
303 dev_err(class_dev
->dev
, "cannot create rtc_dev device\n");
304 err
= PTR_ERR(rtc
->rtc_dev
);
308 dev_info(class_dev
->dev
, "rtc intf: dev (%d:%d)\n",
309 MAJOR(rtc
->rtc_dev
->devt
),
310 MINOR(rtc
->rtc_dev
->devt
));
316 cdev_del(&rtc
->char_dev
);
320 static void rtc_dev_remove_device(struct class_device
*class_dev
,
321 struct class_interface
*class_intf
)
323 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
326 dev_dbg(class_dev
->dev
, "removing char %d:%d\n",
327 MAJOR(rtc
->rtc_dev
->devt
),
328 MINOR(rtc
->rtc_dev
->devt
));
330 class_device_unregister(rtc
->rtc_dev
);
331 cdev_del(&rtc
->char_dev
);
335 /* interface registration */
337 static struct class_interface rtc_dev_interface
= {
338 .add
= &rtc_dev_add_device
,
339 .remove
= &rtc_dev_remove_device
,
342 static int __init
rtc_dev_init(void)
346 rtc_dev_class
= class_create(THIS_MODULE
, "rtc-dev");
347 if (IS_ERR(rtc_dev_class
))
348 return PTR_ERR(rtc_dev_class
);
350 err
= alloc_chrdev_region(&rtc_devt
, 0, RTC_DEV_MAX
, "rtc");
352 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
354 goto err_destroy_class
;
357 err
= rtc_interface_register(&rtc_dev_interface
);
359 printk(KERN_ERR
"%s: failed to register the interface\n",
361 goto err_unregister_chrdev
;
366 err_unregister_chrdev
:
367 unregister_chrdev_region(rtc_devt
, RTC_DEV_MAX
);
370 class_destroy(rtc_dev_class
);
375 static void __exit
rtc_dev_exit(void)
377 class_interface_unregister(&rtc_dev_interface
);
378 class_destroy(rtc_dev_class
);
379 unregister_chrdev_region(rtc_devt
, RTC_DEV_MAX
);
382 module_init(rtc_dev_init
);
383 module_exit(rtc_dev_exit
);
385 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
386 MODULE_DESCRIPTION("RTC class dev interface");
387 MODULE_LICENSE("GPL");