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 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
, data
);
95 ret
= put_user(data
, (unsigned long __user
*)buf
);
97 ret
= sizeof(unsigned long);
102 static unsigned int rtc_dev_poll(struct file
*file
, poll_table
*wait
)
104 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
107 poll_wait(file
, &rtc
->irq_queue
, wait
);
109 data
= rtc
->irq_data
;
111 return (data
!= 0) ? (POLLIN
| POLLRDNORM
) : 0;
114 static int rtc_dev_ioctl(struct inode
*inode
, struct file
*file
,
115 unsigned int cmd
, unsigned long arg
)
118 struct class_device
*class_dev
= file
->private_data
;
119 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
120 struct rtc_class_ops
*ops
= rtc
->ops
;
122 struct rtc_wkalrm alarm
;
123 void __user
*uarg
= (void __user
*) arg
;
125 /* avoid conflicting IRQ users */
126 if (cmd
== RTC_PIE_ON
|| cmd
== RTC_PIE_OFF
|| cmd
== RTC_IRQP_SET
) {
127 spin_lock(&rtc
->irq_task_lock
);
130 spin_unlock(&rtc
->irq_task_lock
);
136 /* try the driver's ioctl interface */
138 err
= ops
->ioctl(class_dev
->dev
, cmd
, arg
);
143 /* if the driver does not provide the ioctl interface
144 * or if that particular ioctl was not implemented
145 * (-EINVAL), we will try to emulate here.
150 err
= rtc_read_alarm(class_dev
, &alarm
);
154 if (copy_to_user(uarg
, &alarm
.time
, sizeof(tm
)))
159 if (copy_from_user(&alarm
.time
, uarg
, sizeof(tm
)))
164 alarm
.time
.tm_mday
= -1;
165 alarm
.time
.tm_mon
= -1;
166 alarm
.time
.tm_year
= -1;
167 alarm
.time
.tm_wday
= -1;
168 alarm
.time
.tm_yday
= -1;
169 alarm
.time
.tm_isdst
= -1;
170 err
= rtc_set_alarm(class_dev
, &alarm
);
174 err
= rtc_read_time(class_dev
, &tm
);
178 if (copy_to_user(uarg
, &tm
, sizeof(tm
)))
183 if (!capable(CAP_SYS_TIME
))
186 if (copy_from_user(&tm
, uarg
, sizeof(tm
)))
189 err
= rtc_set_time(class_dev
, &tm
);
195 * There were no RTC clocks before 1900.
201 if (!capable(CAP_SYS_TIME
)) {
211 err
= put_user(rtc_epoch
, (unsigned long __user
*)uarg
);
215 if (copy_from_user(&alarm
, uarg
, sizeof(alarm
)))
218 err
= rtc_set_alarm(class_dev
, &alarm
);
222 err
= rtc_read_alarm(class_dev
, &alarm
);
226 if (copy_to_user(uarg
, &alarm
, sizeof(alarm
)))
238 static int rtc_dev_release(struct inode
*inode
, struct file
*file
)
240 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
242 if (rtc
->ops
->release
)
243 rtc
->ops
->release(rtc
->class_dev
.dev
);
245 mutex_unlock(&rtc
->char_lock
);
249 static int rtc_dev_fasync(int fd
, struct file
*file
, int on
)
251 struct rtc_device
*rtc
= to_rtc_device(file
->private_data
);
252 return fasync_helper(fd
, file
, on
, &rtc
->async_queue
);
255 static struct file_operations rtc_dev_fops
= {
256 .owner
= THIS_MODULE
,
258 .read
= rtc_dev_read
,
259 .poll
= rtc_dev_poll
,
260 .ioctl
= rtc_dev_ioctl
,
261 .open
= rtc_dev_open
,
262 .release
= rtc_dev_release
,
263 .fasync
= rtc_dev_fasync
,
266 /* insertion/removal hooks */
268 static int rtc_dev_add_device(struct class_device
*class_dev
,
269 struct class_interface
*class_intf
)
272 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
274 if (rtc
->id
>= RTC_DEV_MAX
) {
275 dev_err(class_dev
->dev
, "too many RTCs\n");
279 mutex_init(&rtc
->char_lock
);
280 spin_lock_init(&rtc
->irq_lock
);
281 init_waitqueue_head(&rtc
->irq_queue
);
283 cdev_init(&rtc
->char_dev
, &rtc_dev_fops
);
284 rtc
->char_dev
.owner
= rtc
->owner
;
286 if (cdev_add(&rtc
->char_dev
, MKDEV(MAJOR(rtc_devt
), rtc
->id
), 1)) {
287 cdev_del(&rtc
->char_dev
);
288 dev_err(class_dev
->dev
,
289 "failed to add char device %d:%d\n",
290 MAJOR(rtc_devt
), rtc
->id
);
294 rtc
->rtc_dev
= class_device_create(rtc_dev_class
, NULL
,
295 MKDEV(MAJOR(rtc_devt
), rtc
->id
),
296 class_dev
->dev
, "rtc%d", rtc
->id
);
297 if (IS_ERR(rtc
->rtc_dev
)) {
298 dev_err(class_dev
->dev
, "cannot create rtc_dev device\n");
299 err
= PTR_ERR(rtc
->rtc_dev
);
303 dev_info(class_dev
->dev
, "rtc intf: dev (%d:%d)\n",
304 MAJOR(rtc
->rtc_dev
->devt
),
305 MINOR(rtc
->rtc_dev
->devt
));
311 cdev_del(&rtc
->char_dev
);
315 static void rtc_dev_remove_device(struct class_device
*class_dev
,
316 struct class_interface
*class_intf
)
318 struct rtc_device
*rtc
= to_rtc_device(class_dev
);
321 dev_dbg(class_dev
->dev
, "removing char %d:%d\n",
322 MAJOR(rtc
->rtc_dev
->devt
),
323 MINOR(rtc
->rtc_dev
->devt
));
325 class_device_unregister(rtc
->rtc_dev
);
326 cdev_del(&rtc
->char_dev
);
330 /* interface registration */
332 static struct class_interface rtc_dev_interface
= {
333 .add
= &rtc_dev_add_device
,
334 .remove
= &rtc_dev_remove_device
,
337 static int __init
rtc_dev_init(void)
341 rtc_dev_class
= class_create(THIS_MODULE
, "rtc-dev");
342 if (IS_ERR(rtc_dev_class
))
343 return PTR_ERR(rtc_dev_class
);
345 err
= alloc_chrdev_region(&rtc_devt
, 0, RTC_DEV_MAX
, "rtc");
347 printk(KERN_ERR
"%s: failed to allocate char dev region\n",
349 goto err_destroy_class
;
352 err
= rtc_interface_register(&rtc_dev_interface
);
354 printk(KERN_ERR
"%s: failed to register the interface\n",
356 goto err_unregister_chrdev
;
361 err_unregister_chrdev
:
362 unregister_chrdev_region(rtc_devt
, RTC_DEV_MAX
);
365 class_destroy(rtc_dev_class
);
370 static void __exit
rtc_dev_exit(void)
372 class_interface_unregister(&rtc_dev_interface
);
373 class_destroy(rtc_dev_class
);
374 unregister_chrdev_region(rtc_devt
, RTC_DEV_MAX
);
377 module_init(rtc_dev_init
);
378 module_exit(rtc_dev_exit
);
380 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
381 MODULE_DESCRIPTION("RTC class dev interface");
382 MODULE_LICENSE("GPL");