blob: 814b9e1873f55ef8061b4abfc66ab5776da38dc1 [file] [log] [blame]
Alessandro Zummoe8242902006-03-27 01:16:41 -08001/*
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
17static struct class *rtc_dev_class;
18static dev_t rtc_devt;
19
20#define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */
21
22static 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);
David Brownellff8371a2006-09-30 23:28:17 -070027 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -080028
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
Atsushi Nemoto655066c2006-06-25 05:48:17 -070051#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 */
56static 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);
David Brownelld728b1e2006-11-25 11:09:28 -080064
65 local_irq_disable();
66 spin_lock(&rtc->irq_lock);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070067 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;
78 }
David Brownelld728b1e2006-11-25 11:09:28 -080079 spin_unlock(&rtc->irq_lock);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070080 if (num)
81 rtc_update_irq(&rtc->class_dev, num, RTC_UF | RTC_IRQF);
David Brownelld728b1e2006-11-25 11:09:28 -080082 local_irq_enable();
Atsushi Nemoto655066c2006-06-25 05:48:17 -070083}
Atsushi Nemoto655066c2006-06-25 05:48:17 -070084static void rtc_uie_timer(unsigned long data)
85{
86 struct rtc_device *rtc = (struct rtc_device *)data;
87 unsigned long flags;
88
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);
95}
96
97static void clear_uie(struct rtc_device *rtc)
98{
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;
107 }
108 if (rtc->uie_task_active) {
109 spin_unlock_irq(&rtc->irq_lock);
110 flush_scheduled_work();
111 spin_lock_irq(&rtc->irq_lock);
112 }
113 rtc->irq_active = 0;
114 }
115 spin_unlock_irq(&rtc->irq_lock);
116}
117
118static int set_uie(struct rtc_device *rtc)
119{
120 struct rtc_time tm;
121 int err;
122
123 err = rtc_read_time(&rtc->class_dev, &tm);
124 if (err)
125 return err;
126 spin_lock_irq(&rtc->irq_lock);
127 if (!rtc->irq_active) {
128 rtc->irq_active = 1;
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;
134 }
135 rtc->irq_data = 0;
136 spin_unlock_irq(&rtc->irq_lock);
137 return 0;
138}
139#endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
Alessandro Zummoe8242902006-03-27 01:16:41 -0800140
141static ssize_t
142rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
143{
144 struct rtc_device *rtc = to_rtc_device(file->private_data);
145
146 DECLARE_WAITQUEUE(wait, current);
147 unsigned long data;
148 ssize_t ret;
149
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700150 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
Alessandro Zummoe8242902006-03-27 01:16:41 -0800151 return -EINVAL;
152
153 add_wait_queue(&rtc->irq_queue, &wait);
154 do {
155 __set_current_state(TASK_INTERRUPTIBLE);
156
157 spin_lock_irq(&rtc->irq_lock);
158 data = rtc->irq_data;
159 rtc->irq_data = 0;
160 spin_unlock_irq(&rtc->irq_lock);
161
162 if (data != 0) {
163 ret = 0;
164 break;
165 }
166 if (file->f_flags & O_NONBLOCK) {
167 ret = -EAGAIN;
168 break;
169 }
170 if (signal_pending(current)) {
171 ret = -ERESTARTSYS;
172 break;
173 }
174 schedule();
175 } while (1);
176 set_current_state(TASK_RUNNING);
177 remove_wait_queue(&rtc->irq_queue, &wait);
178
179 if (ret == 0) {
180 /* Check for any data updates */
181 if (rtc->ops->read_callback)
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700182 data = rtc->ops->read_callback(rtc->class_dev.dev,
183 data);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800184
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700185 if (sizeof(int) != sizeof(long) &&
186 count == sizeof(unsigned int))
187 ret = put_user(data, (unsigned int __user *)buf) ?:
188 sizeof(unsigned int);
189 else
190 ret = put_user(data, (unsigned long __user *)buf) ?:
191 sizeof(unsigned long);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800192 }
193 return ret;
194}
195
196static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
197{
198 struct rtc_device *rtc = to_rtc_device(file->private_data);
199 unsigned long data;
200
201 poll_wait(file, &rtc->irq_queue, wait);
202
203 data = rtc->irq_data;
204
205 return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
206}
207
208static int rtc_dev_ioctl(struct inode *inode, struct file *file,
209 unsigned int cmd, unsigned long arg)
210{
211 int err = 0;
212 struct class_device *class_dev = file->private_data;
213 struct rtc_device *rtc = to_rtc_device(class_dev);
David Brownellff8371a2006-09-30 23:28:17 -0700214 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800215 struct rtc_time tm;
216 struct rtc_wkalrm alarm;
217 void __user *uarg = (void __user *) arg;
218
David Brownell2601a462006-11-25 11:09:27 -0800219 /* check that the calling task has appropriate permissions
Alessandro Zummo110d6932006-06-25 05:48:20 -0700220 * for certain ioctls. doing this check here is useful
221 * to avoid duplicate code in each driver.
222 */
223 switch (cmd) {
224 case RTC_EPOCH_SET:
225 case RTC_SET_TIME:
226 if (!capable(CAP_SYS_TIME))
227 return -EACCES;
228 break;
229
230 case RTC_IRQP_SET:
231 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
232 return -EACCES;
233 break;
234
235 case RTC_PIE_ON:
236 if (!capable(CAP_SYS_RESOURCE))
237 return -EACCES;
238 break;
239 }
240
Alessandro Zummoe8242902006-03-27 01:16:41 -0800241 /* avoid conflicting IRQ users */
242 if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) {
David Brownelld728b1e2006-11-25 11:09:28 -0800243 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800244 if (rtc->irq_task)
245 err = -EBUSY;
David Brownelld728b1e2006-11-25 11:09:28 -0800246 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800247
248 if (err < 0)
249 return err;
250 }
251
252 /* try the driver's ioctl interface */
253 if (ops->ioctl) {
254 err = ops->ioctl(class_dev->dev, cmd, arg);
Alessandro Zummob3969e52006-05-20 15:00:29 -0700255 if (err != -ENOIOCTLCMD)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800256 return err;
257 }
258
259 /* if the driver does not provide the ioctl interface
260 * or if that particular ioctl was not implemented
Alessandro Zummob3969e52006-05-20 15:00:29 -0700261 * (-ENOIOCTLCMD), we will try to emulate here.
Alessandro Zummoe8242902006-03-27 01:16:41 -0800262 */
263
264 switch (cmd) {
265 case RTC_ALM_READ:
266 err = rtc_read_alarm(class_dev, &alarm);
267 if (err < 0)
268 return err;
269
270 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
271 return -EFAULT;
272 break;
273
274 case RTC_ALM_SET:
275 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
276 return -EFAULT;
277
278 alarm.enabled = 0;
279 alarm.pending = 0;
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);
287 break;
288
289 case RTC_RD_TIME:
290 err = rtc_read_time(class_dev, &tm);
291 if (err < 0)
292 return err;
293
294 if (copy_to_user(uarg, &tm, sizeof(tm)))
295 return -EFAULT;
296 break;
297
298 case RTC_SET_TIME:
Alessandro Zummoe8242902006-03-27 01:16:41 -0800299 if (copy_from_user(&tm, uarg, sizeof(tm)))
300 return -EFAULT;
301
302 err = rtc_set_time(class_dev, &tm);
303 break;
David Brownell2601a462006-11-25 11:09:27 -0800304
305 case RTC_IRQP_READ:
306 if (ops->irq_set_freq)
307 err = put_user(rtc->irq_freq, (unsigned long *) arg);
308 break;
309
310 case RTC_IRQP_SET:
311 if (ops->irq_set_freq)
312 err = rtc_irq_set_freq(class_dev, rtc->irq_task, arg);
313 break;
314
Alessandro Zummoe8242902006-03-27 01:16:41 -0800315#if 0
316 case RTC_EPOCH_SET:
317#ifndef rtc_epoch
318 /*
319 * There were no RTC clocks before 1900.
320 */
321 if (arg < 1900) {
322 err = -EINVAL;
323 break;
324 }
Alessandro Zummoe8242902006-03-27 01:16:41 -0800325 rtc_epoch = arg;
326 err = 0;
327#endif
328 break;
329
330 case RTC_EPOCH_READ:
331 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
332 break;
333#endif
334 case RTC_WKALM_SET:
335 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
336 return -EFAULT;
337
338 err = rtc_set_alarm(class_dev, &alarm);
339 break;
340
341 case RTC_WKALM_RD:
342 err = rtc_read_alarm(class_dev, &alarm);
343 if (err < 0)
344 return err;
345
346 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
347 return -EFAULT;
348 break;
349
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700350#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
351 case RTC_UIE_OFF:
352 clear_uie(rtc);
353 return 0;
354
355 case RTC_UIE_ON:
356 return set_uie(rtc);
357#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800358 default:
Alessandro Zummob3969e52006-05-20 15:00:29 -0700359 err = -ENOTTY;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800360 break;
361 }
362
363 return err;
364}
365
366static int rtc_dev_release(struct inode *inode, struct file *file)
367{
368 struct rtc_device *rtc = to_rtc_device(file->private_data);
369
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700370#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
371 clear_uie(rtc);
372#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800373 if (rtc->ops->release)
374 rtc->ops->release(rtc->class_dev.dev);
375
376 mutex_unlock(&rtc->char_lock);
377 return 0;
378}
379
380static int rtc_dev_fasync(int fd, struct file *file, int on)
381{
382 struct rtc_device *rtc = to_rtc_device(file->private_data);
383 return fasync_helper(fd, file, on, &rtc->async_queue);
384}
385
386static struct file_operations rtc_dev_fops = {
387 .owner = THIS_MODULE,
388 .llseek = no_llseek,
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,
395};
396
397/* insertion/removal hooks */
398
399static int rtc_dev_add_device(struct class_device *class_dev,
400 struct class_interface *class_intf)
401{
402 int err = 0;
403 struct rtc_device *rtc = to_rtc_device(class_dev);
404
405 if (rtc->id >= RTC_DEV_MAX) {
406 dev_err(class_dev->dev, "too many RTCs\n");
407 return -EINVAL;
408 }
409
410 mutex_init(&rtc->char_lock);
411 spin_lock_init(&rtc->irq_lock);
412 init_waitqueue_head(&rtc->irq_queue);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700413#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);
416#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800417
418 cdev_init(&rtc->char_dev, &rtc_dev_fops);
419 rtc->char_dev.owner = rtc->owner;
420
421 if (cdev_add(&rtc->char_dev, MKDEV(MAJOR(rtc_devt), rtc->id), 1)) {
Alessandro Zummoe8242902006-03-27 01:16:41 -0800422 dev_err(class_dev->dev,
423 "failed to add char device %d:%d\n",
424 MAJOR(rtc_devt), rtc->id);
425 return -ENODEV;
426 }
427
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);
434 goto err_cdev_del;
435 }
436
437 dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n",
438 MAJOR(rtc->rtc_dev->devt),
439 MINOR(rtc->rtc_dev->devt));
440
441 return 0;
442
443err_cdev_del:
444
445 cdev_del(&rtc->char_dev);
446 return err;
447}
448
449static void rtc_dev_remove_device(struct class_device *class_dev,
450 struct class_interface *class_intf)
451{
452 struct rtc_device *rtc = to_rtc_device(class_dev);
453
454 if (rtc->rtc_dev) {
455 dev_dbg(class_dev->dev, "removing char %d:%d\n",
456 MAJOR(rtc->rtc_dev->devt),
457 MINOR(rtc->rtc_dev->devt));
458
459 class_device_unregister(rtc->rtc_dev);
460 cdev_del(&rtc->char_dev);
461 }
462}
463
464/* interface registration */
465
466static struct class_interface rtc_dev_interface = {
467 .add = &rtc_dev_add_device,
468 .remove = &rtc_dev_remove_device,
469};
470
471static int __init rtc_dev_init(void)
472{
473 int err;
474
475 rtc_dev_class = class_create(THIS_MODULE, "rtc-dev");
476 if (IS_ERR(rtc_dev_class))
477 return PTR_ERR(rtc_dev_class);
478
479 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
480 if (err < 0) {
481 printk(KERN_ERR "%s: failed to allocate char dev region\n",
482 __FILE__);
483 goto err_destroy_class;
484 }
485
486 err = rtc_interface_register(&rtc_dev_interface);
487 if (err < 0) {
488 printk(KERN_ERR "%s: failed to register the interface\n",
489 __FILE__);
490 goto err_unregister_chrdev;
491 }
492
493 return 0;
494
495err_unregister_chrdev:
496 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
497
498err_destroy_class:
499 class_destroy(rtc_dev_class);
500
501 return err;
502}
503
504static void __exit rtc_dev_exit(void)
505{
506 class_interface_unregister(&rtc_dev_interface);
507 class_destroy(rtc_dev_class);
508 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
509}
510
David Brownell818a8672006-09-30 23:28:15 -0700511subsys_initcall(rtc_dev_init);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800512module_exit(rtc_dev_exit);
513
514MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
515MODULE_DESCRIPTION("RTC class dev interface");
516MODULE_LICENSE("GPL");