[PATCH] IPMI: fix devinit placement
[linux-2.6/x86.git] / drivers / rtc / rtc-dev.c
blobb1e3e6179e569f81d207b286bc4ec290195ff330
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 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;
52 static ssize_t
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);
58 unsigned long data;
59 ssize_t ret;
61 if (count < sizeof(unsigned long))
62 return -EINVAL;
64 add_wait_queue(&rtc->irq_queue, &wait);
65 do {
66 __set_current_state(TASK_INTERRUPTIBLE);
68 spin_lock_irq(&rtc->irq_lock);
69 data = rtc->irq_data;
70 rtc->irq_data = 0;
71 spin_unlock_irq(&rtc->irq_lock);
73 if (data != 0) {
74 ret = 0;
75 break;
77 if (file->f_flags & O_NONBLOCK) {
78 ret = -EAGAIN;
79 break;
81 if (signal_pending(current)) {
82 ret = -ERESTARTSYS;
83 break;
85 schedule();
86 } while (1);
87 set_current_state(TASK_RUNNING);
88 remove_wait_queue(&rtc->irq_queue, &wait);
90 if (ret == 0) {
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);
96 if (ret == 0)
97 ret = sizeof(unsigned long);
99 return ret;
102 static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
104 struct rtc_device *rtc = to_rtc_device(file->private_data);
105 unsigned long 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)
117 int err = 0;
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;
121 struct rtc_time tm;
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);
128 if (rtc->irq_task)
129 err = -EBUSY;
130 spin_unlock(&rtc->irq_task_lock);
132 if (err < 0)
133 return err;
136 /* try the driver's ioctl interface */
137 if (ops->ioctl) {
138 err = ops->ioctl(class_dev->dev, cmd, arg);
139 if (err != -EINVAL)
140 return err;
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.
148 switch (cmd) {
149 case RTC_ALM_READ:
150 err = rtc_read_alarm(class_dev, &alarm);
151 if (err < 0)
152 return err;
154 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
155 return -EFAULT;
156 break;
158 case RTC_ALM_SET:
159 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
160 return -EFAULT;
162 alarm.enabled = 0;
163 alarm.pending = 0;
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);
171 break;
173 case RTC_RD_TIME:
174 err = rtc_read_time(class_dev, &tm);
175 if (err < 0)
176 return err;
178 if (copy_to_user(uarg, &tm, sizeof(tm)))
179 return -EFAULT;
180 break;
182 case RTC_SET_TIME:
183 if (!capable(CAP_SYS_TIME))
184 return -EACCES;
186 if (copy_from_user(&tm, uarg, sizeof(tm)))
187 return -EFAULT;
189 err = rtc_set_time(class_dev, &tm);
190 break;
191 #if 0
192 case RTC_EPOCH_SET:
193 #ifndef rtc_epoch
195 * There were no RTC clocks before 1900.
197 if (arg < 1900) {
198 err = -EINVAL;
199 break;
201 if (!capable(CAP_SYS_TIME)) {
202 err = -EACCES;
203 break;
205 rtc_epoch = arg;
206 err = 0;
207 #endif
208 break;
210 case RTC_EPOCH_READ:
211 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
212 break;
213 #endif
214 case RTC_WKALM_SET:
215 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
216 return -EFAULT;
218 err = rtc_set_alarm(class_dev, &alarm);
219 break;
221 case RTC_WKALM_RD:
222 err = rtc_read_alarm(class_dev, &alarm);
223 if (err < 0)
224 return err;
226 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
227 return -EFAULT;
228 break;
230 default:
231 err = -EINVAL;
232 break;
235 return err;
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);
246 return 0;
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,
257 .llseek = no_llseek,
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)
271 int err = 0;
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");
276 return -EINVAL;
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);
291 return -ENODEV;
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);
300 goto err_cdev_del;
303 dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n",
304 MAJOR(rtc->rtc_dev->devt),
305 MINOR(rtc->rtc_dev->devt));
307 return 0;
309 err_cdev_del:
311 cdev_del(&rtc->char_dev);
312 return err;
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);
320 if (rtc->rtc_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)
339 int err;
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");
346 if (err < 0) {
347 printk(KERN_ERR "%s: failed to allocate char dev region\n",
348 __FILE__);
349 goto err_destroy_class;
352 err = rtc_interface_register(&rtc_dev_interface);
353 if (err < 0) {
354 printk(KERN_ERR "%s: failed to register the interface\n",
355 __FILE__);
356 goto err_unregister_chrdev;
359 return 0;
361 err_unregister_chrdev:
362 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
364 err_destroy_class:
365 class_destroy(rtc_dev_class);
367 return err;
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");