blob: 583789c66cdb9d2404157f0cda029c9e8dfc7c60 [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);
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
82static 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
95static 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
116static 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 */
Alessandro Zummoe8242902006-03-27 01:16:41 -0800138
139static ssize_t
140rtc_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
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700148 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
Alessandro Zummoe8242902006-03-27 01:16:41 -0800149 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)
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700180 data = rtc->ops->read_callback(rtc->class_dev.dev,
181 data);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800182
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700183 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);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800190 }
191 return ret;
192}
193
194static 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
206static 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);
David Brownellff8371a2006-09-30 23:28:17 -0700212 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800213 struct rtc_time tm;
214 struct rtc_wkalrm alarm;
215 void __user *uarg = (void __user *) arg;
216
Alessandro Zummo110d6932006-06-25 05:48:20 -0700217 /* check that the calles has appropriate permissions
218 * for certain ioctls. doing this check here is useful
219 * to avoid duplicate code in each driver.
220 */
221 switch (cmd) {
222 case RTC_EPOCH_SET:
223 case RTC_SET_TIME:
224 if (!capable(CAP_SYS_TIME))
225 return -EACCES;
226 break;
227
228 case RTC_IRQP_SET:
229 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
230 return -EACCES;
231 break;
232
233 case RTC_PIE_ON:
234 if (!capable(CAP_SYS_RESOURCE))
235 return -EACCES;
236 break;
237 }
238
Alessandro Zummoe8242902006-03-27 01:16:41 -0800239 /* avoid conflicting IRQ users */
240 if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) {
241 spin_lock(&rtc->irq_task_lock);
242 if (rtc->irq_task)
243 err = -EBUSY;
244 spin_unlock(&rtc->irq_task_lock);
245
246 if (err < 0)
247 return err;
248 }
249
250 /* try the driver's ioctl interface */
251 if (ops->ioctl) {
252 err = ops->ioctl(class_dev->dev, cmd, arg);
Alessandro Zummob3969e52006-05-20 15:00:29 -0700253 if (err != -ENOIOCTLCMD)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800254 return err;
255 }
256
257 /* if the driver does not provide the ioctl interface
258 * or if that particular ioctl was not implemented
Alessandro Zummob3969e52006-05-20 15:00:29 -0700259 * (-ENOIOCTLCMD), we will try to emulate here.
Alessandro Zummoe8242902006-03-27 01:16:41 -0800260 */
261
262 switch (cmd) {
263 case RTC_ALM_READ:
264 err = rtc_read_alarm(class_dev, &alarm);
265 if (err < 0)
266 return err;
267
268 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
269 return -EFAULT;
270 break;
271
272 case RTC_ALM_SET:
273 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
274 return -EFAULT;
275
276 alarm.enabled = 0;
277 alarm.pending = 0;
278 alarm.time.tm_mday = -1;
279 alarm.time.tm_mon = -1;
280 alarm.time.tm_year = -1;
281 alarm.time.tm_wday = -1;
282 alarm.time.tm_yday = -1;
283 alarm.time.tm_isdst = -1;
284 err = rtc_set_alarm(class_dev, &alarm);
285 break;
286
287 case RTC_RD_TIME:
288 err = rtc_read_time(class_dev, &tm);
289 if (err < 0)
290 return err;
291
292 if (copy_to_user(uarg, &tm, sizeof(tm)))
293 return -EFAULT;
294 break;
295
296 case RTC_SET_TIME:
Alessandro Zummoe8242902006-03-27 01:16:41 -0800297 if (copy_from_user(&tm, uarg, sizeof(tm)))
298 return -EFAULT;
299
300 err = rtc_set_time(class_dev, &tm);
301 break;
302#if 0
303 case RTC_EPOCH_SET:
304#ifndef rtc_epoch
305 /*
306 * There were no RTC clocks before 1900.
307 */
308 if (arg < 1900) {
309 err = -EINVAL;
310 break;
311 }
Alessandro Zummoe8242902006-03-27 01:16:41 -0800312 rtc_epoch = arg;
313 err = 0;
314#endif
315 break;
316
317 case RTC_EPOCH_READ:
318 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
319 break;
320#endif
321 case RTC_WKALM_SET:
322 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
323 return -EFAULT;
324
325 err = rtc_set_alarm(class_dev, &alarm);
326 break;
327
328 case RTC_WKALM_RD:
329 err = rtc_read_alarm(class_dev, &alarm);
330 if (err < 0)
331 return err;
332
333 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
334 return -EFAULT;
335 break;
336
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700337#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
338 case RTC_UIE_OFF:
339 clear_uie(rtc);
340 return 0;
341
342 case RTC_UIE_ON:
343 return set_uie(rtc);
344#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800345 default:
Alessandro Zummob3969e52006-05-20 15:00:29 -0700346 err = -ENOTTY;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800347 break;
348 }
349
350 return err;
351}
352
353static int rtc_dev_release(struct inode *inode, struct file *file)
354{
355 struct rtc_device *rtc = to_rtc_device(file->private_data);
356
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700357#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
358 clear_uie(rtc);
359#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800360 if (rtc->ops->release)
361 rtc->ops->release(rtc->class_dev.dev);
362
363 mutex_unlock(&rtc->char_lock);
364 return 0;
365}
366
367static int rtc_dev_fasync(int fd, struct file *file, int on)
368{
369 struct rtc_device *rtc = to_rtc_device(file->private_data);
370 return fasync_helper(fd, file, on, &rtc->async_queue);
371}
372
373static struct file_operations rtc_dev_fops = {
374 .owner = THIS_MODULE,
375 .llseek = no_llseek,
376 .read = rtc_dev_read,
377 .poll = rtc_dev_poll,
378 .ioctl = rtc_dev_ioctl,
379 .open = rtc_dev_open,
380 .release = rtc_dev_release,
381 .fasync = rtc_dev_fasync,
382};
383
384/* insertion/removal hooks */
385
386static int rtc_dev_add_device(struct class_device *class_dev,
387 struct class_interface *class_intf)
388{
389 int err = 0;
390 struct rtc_device *rtc = to_rtc_device(class_dev);
391
392 if (rtc->id >= RTC_DEV_MAX) {
393 dev_err(class_dev->dev, "too many RTCs\n");
394 return -EINVAL;
395 }
396
397 mutex_init(&rtc->char_lock);
398 spin_lock_init(&rtc->irq_lock);
399 init_waitqueue_head(&rtc->irq_queue);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700400#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
401 INIT_WORK(&rtc->uie_task, rtc_uie_task, rtc);
402 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
403#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800404
405 cdev_init(&rtc->char_dev, &rtc_dev_fops);
406 rtc->char_dev.owner = rtc->owner;
407
408 if (cdev_add(&rtc->char_dev, MKDEV(MAJOR(rtc_devt), rtc->id), 1)) {
Alessandro Zummoe8242902006-03-27 01:16:41 -0800409 dev_err(class_dev->dev,
410 "failed to add char device %d:%d\n",
411 MAJOR(rtc_devt), rtc->id);
412 return -ENODEV;
413 }
414
415 rtc->rtc_dev = class_device_create(rtc_dev_class, NULL,
416 MKDEV(MAJOR(rtc_devt), rtc->id),
417 class_dev->dev, "rtc%d", rtc->id);
418 if (IS_ERR(rtc->rtc_dev)) {
419 dev_err(class_dev->dev, "cannot create rtc_dev device\n");
420 err = PTR_ERR(rtc->rtc_dev);
421 goto err_cdev_del;
422 }
423
424 dev_info(class_dev->dev, "rtc intf: dev (%d:%d)\n",
425 MAJOR(rtc->rtc_dev->devt),
426 MINOR(rtc->rtc_dev->devt));
427
428 return 0;
429
430err_cdev_del:
431
432 cdev_del(&rtc->char_dev);
433 return err;
434}
435
436static void rtc_dev_remove_device(struct class_device *class_dev,
437 struct class_interface *class_intf)
438{
439 struct rtc_device *rtc = to_rtc_device(class_dev);
440
441 if (rtc->rtc_dev) {
442 dev_dbg(class_dev->dev, "removing char %d:%d\n",
443 MAJOR(rtc->rtc_dev->devt),
444 MINOR(rtc->rtc_dev->devt));
445
446 class_device_unregister(rtc->rtc_dev);
447 cdev_del(&rtc->char_dev);
448 }
449}
450
451/* interface registration */
452
453static struct class_interface rtc_dev_interface = {
454 .add = &rtc_dev_add_device,
455 .remove = &rtc_dev_remove_device,
456};
457
458static int __init rtc_dev_init(void)
459{
460 int err;
461
462 rtc_dev_class = class_create(THIS_MODULE, "rtc-dev");
463 if (IS_ERR(rtc_dev_class))
464 return PTR_ERR(rtc_dev_class);
465
466 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
467 if (err < 0) {
468 printk(KERN_ERR "%s: failed to allocate char dev region\n",
469 __FILE__);
470 goto err_destroy_class;
471 }
472
473 err = rtc_interface_register(&rtc_dev_interface);
474 if (err < 0) {
475 printk(KERN_ERR "%s: failed to register the interface\n",
476 __FILE__);
477 goto err_unregister_chrdev;
478 }
479
480 return 0;
481
482err_unregister_chrdev:
483 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
484
485err_destroy_class:
486 class_destroy(rtc_dev_class);
487
488 return err;
489}
490
491static void __exit rtc_dev_exit(void)
492{
493 class_interface_unregister(&rtc_dev_interface);
494 class_destroy(rtc_dev_class);
495 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
496}
497
David Brownell818a8672006-09-30 23:28:15 -0700498subsys_initcall(rtc_dev_init);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800499module_exit(rtc_dev_exit);
500
501MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
502MODULE_DESCRIPTION("RTC class dev interface");
503MODULE_LICENSE("GPL");