[PATCH] RTC: rtc-dev UIE emulation
[deliverable/linux.git] / drivers / rtc / rtc-dev.c
1 /*
2 * RTC subsystem, dev interface
3 *
4 * Copyright (C) 2005 Tower Technologies
5 * Author: Alessandro Zummo <a.zummo@towertech.it>
6 *
7 * based on arch/arm/common/rtctime.c
8 *
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.
12 */
13
14 #include <linux/module.h>
15 #include <linux/rtc.h>
16
17 static struct class *rtc_dev_class;
18 static dev_t rtc_devt;
19
20 #define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
21
22 static int rtc_dev_open(struct inode *inode, struct file *file)
23 {
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;
28
29 /* We keep the lock as long as the device is in use
30 * and return immediately if busy
31 */
32 if (!(mutex_trylock(&rtc->char_lock)))
33 return -EBUSY;
34
35 file->private_data = &rtc->class_dev;
36
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);
42
43 return 0;
44 }
45
46 /* something has gone wrong, release the lock */
47 mutex_unlock(&rtc->char_lock);
48 return err;
49 }
50
51 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
52 /*
53 * Routine to poll RTC seconds field for change as often as possible,
54 * after first RTC_UIE use timer to reduce polling
55 */
56 static void rtc_uie_task(void *data)
57 {
58 struct rtc_device *rtc = data;
59 struct rtc_time tm;
60 int num = 0;
61 int err;
62
63 err = rtc_read_time(&rtc->class_dev, &tm);
64 spin_lock_irq(&rtc->irq_lock);
65 if (rtc->stop_uie_polling || err) {
66 rtc->uie_task_active = 0;
67 } else if (rtc->oldsecs != tm.tm_sec) {
68 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
69 rtc->oldsecs = tm.tm_sec;
70 rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
71 rtc->uie_timer_active = 1;
72 rtc->uie_task_active = 0;
73 add_timer(&rtc->uie_timer);
74 } else if (schedule_work(&rtc->uie_task) == 0) {
75 rtc->uie_task_active = 0;
76 }
77 spin_unlock_irq(&rtc->irq_lock);
78 if (num)
79 rtc_update_irq(&rtc->class_dev, num, RTC_UF | RTC_IRQF);
80 }
81
82 static void rtc_uie_timer(unsigned long data)
83 {
84 struct rtc_device *rtc = (struct rtc_device *)data;
85 unsigned long flags;
86
87 spin_lock_irqsave(&rtc->irq_lock, flags);
88 rtc->uie_timer_active = 0;
89 rtc->uie_task_active = 1;
90 if ((schedule_work(&rtc->uie_task) == 0))
91 rtc->uie_task_active = 0;
92 spin_unlock_irqrestore(&rtc->irq_lock, flags);
93 }
94
95 static void clear_uie(struct rtc_device *rtc)
96 {
97 spin_lock_irq(&rtc->irq_lock);
98 if (rtc->irq_active) {
99 rtc->stop_uie_polling = 1;
100 if (rtc->uie_timer_active) {
101 spin_unlock_irq(&rtc->irq_lock);
102 del_timer_sync(&rtc->uie_timer);
103 spin_lock_irq(&rtc->irq_lock);
104 rtc->uie_timer_active = 0;
105 }
106 if (rtc->uie_task_active) {
107 spin_unlock_irq(&rtc->irq_lock);
108 flush_scheduled_work();
109 spin_lock_irq(&rtc->irq_lock);
110 }
111 rtc->irq_active = 0;
112 }
113 spin_unlock_irq(&rtc->irq_lock);
114 }
115
116 static int set_uie(struct rtc_device *rtc)
117 {
118 struct rtc_time tm;
119 int err;
120
121 err = rtc_read_time(&rtc->class_dev, &tm);
122 if (err)
123 return err;
124 spin_lock_irq(&rtc->irq_lock);
125 if (!rtc->irq_active) {
126 rtc->irq_active = 1;
127 rtc->stop_uie_polling = 0;
128 rtc->oldsecs = tm.tm_sec;
129 rtc->uie_task_active = 1;
130 if (schedule_work(&rtc->uie_task) == 0)
131 rtc->uie_task_active = 0;
132 }
133 rtc->irq_data = 0;
134 spin_unlock_irq(&rtc->irq_lock);
135 return 0;
136 }
137 #endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
138
139 static ssize_t
140 rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
141 {
142 struct rtc_device *rtc = to_rtc_device(file->private_data);
143
144 DECLARE_WAITQUEUE(wait, current);
145 unsigned long data;
146 ssize_t ret;
147
148 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
149 return -EINVAL;
150
151 add_wait_queue(&rtc->irq_queue, &wait);
152 do {
153 __set_current_state(TASK_INTERRUPTIBLE);
154
155 spin_lock_irq(&rtc->irq_lock);
156 data = rtc->irq_data;
157 rtc->irq_data = 0;
158 spin_unlock_irq(&rtc->irq_lock);
159
160 if (data != 0) {
161 ret = 0;
162 break;
163 }
164 if (file->f_flags & O_NONBLOCK) {
165 ret = -EAGAIN;
166 break;
167 }
168 if (signal_pending(current)) {
169 ret = -ERESTARTSYS;
170 break;
171 }
172 schedule();
173 } while (1);
174 set_current_state(TASK_RUNNING);
175 remove_wait_queue(&rtc->irq_queue, &wait);
176
177 if (ret == 0) {
178 /* Check for any data updates */
179 if (rtc->ops->read_callback)
180 data = rtc->ops->read_callback(rtc->class_dev.dev,
181 data);
182
183 if (sizeof(int) != sizeof(long) &&
184 count == sizeof(unsigned int))
185 ret = put_user(data, (unsigned int __user *)buf) ?:
186 sizeof(unsigned int);
187 else
188 ret = put_user(data, (unsigned long __user *)buf) ?:
189 sizeof(unsigned long);
190 }
191 return ret;
192 }
193
194 static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
195 {
196 struct rtc_device *rtc = to_rtc_device(file->private_data);
197 unsigned long data;
198
199 poll_wait(file, &rtc->irq_queue, wait);
200
201 data = rtc->irq_data;
202
203 return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
204 }
205
206 static int rtc_dev_ioctl(struct inode *inode, struct file *file,
207 unsigned int cmd, unsigned long arg)
208 {
209 int err = 0;
210 struct class_device *class_dev = file->private_data;
211 struct rtc_device *rtc = to_rtc_device(class_dev);
212 struct rtc_class_ops *ops = rtc->ops;
213 struct rtc_time tm;
214 struct rtc_wkalrm alarm;
215 void __user *uarg = (void __user *) arg;
216
217 /* avoid conflicting IRQ users */
218 if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) {
219 spin_lock(&rtc->irq_task_lock);
220 if (rtc->irq_task)
221 err = -EBUSY;
222 spin_unlock(&rtc->irq_task_lock);
223
224 if (err < 0)
225 return err;
226 }
227
228 /* try the driver's ioctl interface */
229 if (ops->ioctl) {
230 err = ops->ioctl(class_dev->dev, cmd, arg);
231 if (err != -ENOIOCTLCMD)
232 return err;
233 }
234
235 /* if the driver does not provide the ioctl interface
236 * or if that particular ioctl was not implemented
237 * (-ENOIOCTLCMD), we will try to emulate here.
238 */
239
240 switch (cmd) {
241 case RTC_ALM_READ:
242 err = rtc_read_alarm(class_dev, &alarm);
243 if (err < 0)
244 return err;
245
246 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
247 return -EFAULT;
248 break;
249
250 case RTC_ALM_SET:
251 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
252 return -EFAULT;
253
254 alarm.enabled = 0;
255 alarm.pending = 0;
256 alarm.time.tm_mday = -1;
257 alarm.time.tm_mon = -1;
258 alarm.time.tm_year = -1;
259 alarm.time.tm_wday = -1;
260 alarm.time.tm_yday = -1;
261 alarm.time.tm_isdst = -1;
262 err = rtc_set_alarm(class_dev, &alarm);
263 break;
264
265 case RTC_RD_TIME:
266 err = rtc_read_time(class_dev, &tm);
267 if (err < 0)
268 return err;
269
270 if (copy_to_user(uarg, &tm, sizeof(tm)))
271 return -EFAULT;
272 break;
273
274 case RTC_SET_TIME:
275 if (!capable(CAP_SYS_TIME))
276 return -EACCES;
277
278 if (copy_from_user(&tm, uarg, sizeof(tm)))
279 return -EFAULT;
280
281 err = rtc_set_time(class_dev, &tm);
282 break;
283 #if 0
284 case RTC_EPOCH_SET:
285 #ifndef rtc_epoch
286 /*
287 * There were no RTC clocks before 1900.
288 */
289 if (arg < 1900) {
290 err = -EINVAL;
291 break;
292 }
293 if (!capable(CAP_SYS_TIME)) {
294 err = -EACCES;
295 break;
296 }
297 rtc_epoch = arg;
298 err = 0;
299 #endif
300 break;
301
302 case RTC_EPOCH_READ:
303 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
304 break;
305 #endif
306 case RTC_WKALM_SET:
307 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
308 return -EFAULT;
309
310 err = rtc_set_alarm(class_dev, &alarm);
311 break;
312
313 case RTC_WKALM_RD:
314 err = rtc_read_alarm(class_dev, &alarm);
315 if (err < 0)
316 return err;
317
318 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
319 return -EFAULT;
320 break;
321
322 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
323 case RTC_UIE_OFF:
324 clear_uie(rtc);
325 return 0;
326
327 case RTC_UIE_ON:
328 return set_uie(rtc);
329 #endif
330 default:
331 err = -ENOTTY;
332 break;
333 }
334
335 return err;
336 }
337
338 static int rtc_dev_release(struct inode *inode, struct file *file)
339 {
340 struct rtc_device *rtc = to_rtc_device(file->private_data);
341
342 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
343 clear_uie(rtc);
344 #endif
345 if (rtc->ops->release)
346 rtc->ops->release(rtc->class_dev.dev);
347
348 mutex_unlock(&rtc->char_lock);
349 return 0;
350 }
351
352 static int rtc_dev_fasync(int fd, struct file *file, int on)
353 {
354 struct rtc_device *rtc = to_rtc_device(file->private_data);
355 return fasync_helper(fd, file, on, &rtc->async_queue);
356 }
357
358 static struct file_operations rtc_dev_fops = {
359 .owner = THIS_MODULE,
360 .llseek = no_llseek,
361 .read = rtc_dev_read,
362 .poll = rtc_dev_poll,
363 .ioctl = rtc_dev_ioctl,
364 .open = rtc_dev_open,
365 .release = rtc_dev_release,
366 .fasync = rtc_dev_fasync,
367 };
368
369 /* insertion/removal hooks */
370
371 static int rtc_dev_add_device(struct class_device *class_dev,
372 struct class_interface *class_intf)
373 {
374 int err = 0;
375 struct rtc_device *rtc = to_rtc_device(class_dev);
376
377 if (rtc->id >= RTC_DEV_MAX) {
378 dev_err(class_dev->dev, "too many RTCs\n");
379 return -EINVAL;
380 }
381
382 mutex_init(&rtc->char_lock);
383 spin_lock_init(&rtc->irq_lock);
384 init_waitqueue_head(&rtc->irq_queue);
385 #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
386 INIT_WORK(&rtc->uie_task, rtc_uie_task, rtc);
387 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
388 #endif
389
390 cdev_init(&rtc->char_dev, &rtc_dev_fops);
391 rtc->char_dev.owner = rtc->owner;
392
393 if (cdev_add(&rtc->char_dev, MKDEV(MAJOR(rtc_devt), rtc->id), 1)) {
394 cdev_del(&rtc->char_dev);
395 dev_err(class_dev->dev,
396 "failed to add char device %d:%d\n",
397 MAJOR(rtc_devt), rtc->id);
398 return -ENODEV;
399 }
400
401 rtc->rtc_dev = class_device_create(rtc_dev_class, NULL,
402 MKDEV(MAJOR(rtc_devt), rtc->id),
403 class_dev->dev, "rtc%d", rtc->id);
404 if (IS_ERR(rtc->rtc_dev)) {
405 dev_err(class_dev->dev, "cannot create rtc_dev device\n");
406 err = PTR_ERR(rtc->rtc_dev);
407 goto err_cdev_del;
408 }
409
410 dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n",
411 MAJOR(rtc->rtc_dev->devt),
412 MINOR(rtc->rtc_dev->devt));
413
414 return 0;
415
416 err_cdev_del:
417
418 cdev_del(&rtc->char_dev);
419 return err;
420 }
421
422 static void rtc_dev_remove_device(struct class_device *class_dev,
423 struct class_interface *class_intf)
424 {
425 struct rtc_device *rtc = to_rtc_device(class_dev);
426
427 if (rtc->rtc_dev) {
428 dev_dbg(class_dev->dev, "removing char %d:%d\n",
429 MAJOR(rtc->rtc_dev->devt),
430 MINOR(rtc->rtc_dev->devt));
431
432 class_device_unregister(rtc->rtc_dev);
433 cdev_del(&rtc->char_dev);
434 }
435 }
436
437 /* interface registration */
438
439 static struct class_interface rtc_dev_interface = {
440 .add = &rtc_dev_add_device,
441 .remove = &rtc_dev_remove_device,
442 };
443
444 static int __init rtc_dev_init(void)
445 {
446 int err;
447
448 rtc_dev_class = class_create(THIS_MODULE, "rtc-dev");
449 if (IS_ERR(rtc_dev_class))
450 return PTR_ERR(rtc_dev_class);
451
452 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
453 if (err < 0) {
454 printk(KERN_ERR "%s: failed to allocate char dev region\n",
455 __FILE__);
456 goto err_destroy_class;
457 }
458
459 err = rtc_interface_register(&rtc_dev_interface);
460 if (err < 0) {
461 printk(KERN_ERR "%s: failed to register the interface\n",
462 __FILE__);
463 goto err_unregister_chrdev;
464 }
465
466 return 0;
467
468 err_unregister_chrdev:
469 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
470
471 err_destroy_class:
472 class_destroy(rtc_dev_class);
473
474 return err;
475 }
476
477 static void __exit rtc_dev_exit(void)
478 {
479 class_interface_unregister(&rtc_dev_interface);
480 class_destroy(rtc_dev_class);
481 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
482 }
483
484 module_init(rtc_dev_init);
485 module_exit(rtc_dev_exit);
486
487 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
488 MODULE_DESCRIPTION("RTC class dev interface");
489 MODULE_LICENSE("GPL");
This page took 0.040964 seconds and 6 git commands to generate.