blob: 3fa6138523b5c29525842c18e39562dbbd7a309b [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>
David Brownell5726fb22007-05-08 00:33:27 -070016#include "rtc-core.h"
Alessandro Zummoe8242902006-03-27 01:16:41 -080017
Alessandro Zummoe8242902006-03-27 01:16:41 -080018static 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 */
David Howellsc4028952006-11-22 14:57:56 +000056static void rtc_uie_task(struct work_struct *work)
Atsushi Nemoto655066c2006-06-25 05:48:17 -070057{
David Howellsc4028952006-11-22 14:57:56 +000058 struct rtc_device *rtc =
59 container_of(work, struct rtc_device, uie_task);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070060 struct rtc_time tm;
61 int num = 0;
62 int err;
63
64 err = rtc_read_time(&rtc->class_dev, &tm);
David Brownelld728b1e2006-11-25 11:09:28 -080065
66 local_irq_disable();
67 spin_lock(&rtc->irq_lock);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070068 if (rtc->stop_uie_polling || err) {
69 rtc->uie_task_active = 0;
70 } else if (rtc->oldsecs != tm.tm_sec) {
71 num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
72 rtc->oldsecs = tm.tm_sec;
73 rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
74 rtc->uie_timer_active = 1;
75 rtc->uie_task_active = 0;
76 add_timer(&rtc->uie_timer);
77 } else if (schedule_work(&rtc->uie_task) == 0) {
78 rtc->uie_task_active = 0;
79 }
David Brownelld728b1e2006-11-25 11:09:28 -080080 spin_unlock(&rtc->irq_lock);
Atsushi Nemoto655066c2006-06-25 05:48:17 -070081 if (num)
82 rtc_update_irq(&rtc->class_dev, num, RTC_UF | RTC_IRQF);
David Brownelld728b1e2006-11-25 11:09:28 -080083 local_irq_enable();
Atsushi Nemoto655066c2006-06-25 05:48:17 -070084}
Atsushi Nemoto655066c2006-06-25 05:48:17 -070085static void rtc_uie_timer(unsigned long data)
86{
87 struct rtc_device *rtc = (struct rtc_device *)data;
88 unsigned long flags;
89
90 spin_lock_irqsave(&rtc->irq_lock, flags);
91 rtc->uie_timer_active = 0;
92 rtc->uie_task_active = 1;
93 if ((schedule_work(&rtc->uie_task) == 0))
94 rtc->uie_task_active = 0;
95 spin_unlock_irqrestore(&rtc->irq_lock, flags);
96}
97
98static void clear_uie(struct rtc_device *rtc)
99{
100 spin_lock_irq(&rtc->irq_lock);
101 if (rtc->irq_active) {
102 rtc->stop_uie_polling = 1;
103 if (rtc->uie_timer_active) {
104 spin_unlock_irq(&rtc->irq_lock);
105 del_timer_sync(&rtc->uie_timer);
106 spin_lock_irq(&rtc->irq_lock);
107 rtc->uie_timer_active = 0;
108 }
109 if (rtc->uie_task_active) {
110 spin_unlock_irq(&rtc->irq_lock);
111 flush_scheduled_work();
112 spin_lock_irq(&rtc->irq_lock);
113 }
114 rtc->irq_active = 0;
115 }
116 spin_unlock_irq(&rtc->irq_lock);
117}
118
119static int set_uie(struct rtc_device *rtc)
120{
121 struct rtc_time tm;
122 int err;
123
124 err = rtc_read_time(&rtc->class_dev, &tm);
125 if (err)
126 return err;
127 spin_lock_irq(&rtc->irq_lock);
128 if (!rtc->irq_active) {
129 rtc->irq_active = 1;
130 rtc->stop_uie_polling = 0;
131 rtc->oldsecs = tm.tm_sec;
132 rtc->uie_task_active = 1;
133 if (schedule_work(&rtc->uie_task) == 0)
134 rtc->uie_task_active = 0;
135 }
136 rtc->irq_data = 0;
137 spin_unlock_irq(&rtc->irq_lock);
138 return 0;
139}
140#endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
Alessandro Zummoe8242902006-03-27 01:16:41 -0800141
142static ssize_t
143rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
144{
145 struct rtc_device *rtc = to_rtc_device(file->private_data);
146
147 DECLARE_WAITQUEUE(wait, current);
148 unsigned long data;
149 ssize_t ret;
150
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700151 if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
Alessandro Zummoe8242902006-03-27 01:16:41 -0800152 return -EINVAL;
153
154 add_wait_queue(&rtc->irq_queue, &wait);
155 do {
156 __set_current_state(TASK_INTERRUPTIBLE);
157
158 spin_lock_irq(&rtc->irq_lock);
159 data = rtc->irq_data;
160 rtc->irq_data = 0;
161 spin_unlock_irq(&rtc->irq_lock);
162
163 if (data != 0) {
164 ret = 0;
165 break;
166 }
167 if (file->f_flags & O_NONBLOCK) {
168 ret = -EAGAIN;
169 break;
170 }
171 if (signal_pending(current)) {
172 ret = -ERESTARTSYS;
173 break;
174 }
175 schedule();
176 } while (1);
177 set_current_state(TASK_RUNNING);
178 remove_wait_queue(&rtc->irq_queue, &wait);
179
180 if (ret == 0) {
181 /* Check for any data updates */
182 if (rtc->ops->read_callback)
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700183 data = rtc->ops->read_callback(rtc->class_dev.dev,
184 data);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800185
Atsushi Nemoto3418ff72006-05-01 12:16:16 -0700186 if (sizeof(int) != sizeof(long) &&
187 count == sizeof(unsigned int))
188 ret = put_user(data, (unsigned int __user *)buf) ?:
189 sizeof(unsigned int);
190 else
191 ret = put_user(data, (unsigned long __user *)buf) ?:
192 sizeof(unsigned long);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800193 }
194 return ret;
195}
196
197static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
198{
199 struct rtc_device *rtc = to_rtc_device(file->private_data);
200 unsigned long data;
201
202 poll_wait(file, &rtc->irq_queue, wait);
203
204 data = rtc->irq_data;
205
206 return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
207}
208
209static int rtc_dev_ioctl(struct inode *inode, struct file *file,
210 unsigned int cmd, unsigned long arg)
211{
212 int err = 0;
213 struct class_device *class_dev = file->private_data;
214 struct rtc_device *rtc = to_rtc_device(class_dev);
David Brownellff8371a2006-09-30 23:28:17 -0700215 const struct rtc_class_ops *ops = rtc->ops;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800216 struct rtc_time tm;
217 struct rtc_wkalrm alarm;
218 void __user *uarg = (void __user *) arg;
219
David Brownell2601a462006-11-25 11:09:27 -0800220 /* check that the calling task has appropriate permissions
Alessandro Zummo110d6932006-06-25 05:48:20 -0700221 * for certain ioctls. doing this check here is useful
222 * to avoid duplicate code in each driver.
223 */
224 switch (cmd) {
225 case RTC_EPOCH_SET:
226 case RTC_SET_TIME:
227 if (!capable(CAP_SYS_TIME))
228 return -EACCES;
229 break;
230
231 case RTC_IRQP_SET:
232 if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
233 return -EACCES;
234 break;
235
236 case RTC_PIE_ON:
237 if (!capable(CAP_SYS_RESOURCE))
238 return -EACCES;
239 break;
240 }
241
Alessandro Zummoe8242902006-03-27 01:16:41 -0800242 /* avoid conflicting IRQ users */
243 if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) {
David Brownelld728b1e2006-11-25 11:09:28 -0800244 spin_lock_irq(&rtc->irq_task_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800245 if (rtc->irq_task)
246 err = -EBUSY;
David Brownelld728b1e2006-11-25 11:09:28 -0800247 spin_unlock_irq(&rtc->irq_task_lock);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800248
249 if (err < 0)
250 return err;
251 }
252
253 /* try the driver's ioctl interface */
254 if (ops->ioctl) {
255 err = ops->ioctl(class_dev->dev, cmd, arg);
Alessandro Zummob3969e52006-05-20 15:00:29 -0700256 if (err != -ENOIOCTLCMD)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800257 return err;
258 }
259
260 /* if the driver does not provide the ioctl interface
261 * or if that particular ioctl was not implemented
Alessandro Zummob3969e52006-05-20 15:00:29 -0700262 * (-ENOIOCTLCMD), we will try to emulate here.
Alessandro Zummoe8242902006-03-27 01:16:41 -0800263 */
264
265 switch (cmd) {
266 case RTC_ALM_READ:
267 err = rtc_read_alarm(class_dev, &alarm);
268 if (err < 0)
269 return err;
270
271 if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
272 return -EFAULT;
273 break;
274
275 case RTC_ALM_SET:
276 if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
277 return -EFAULT;
278
279 alarm.enabled = 0;
280 alarm.pending = 0;
281 alarm.time.tm_mday = -1;
282 alarm.time.tm_mon = -1;
283 alarm.time.tm_year = -1;
284 alarm.time.tm_wday = -1;
285 alarm.time.tm_yday = -1;
286 alarm.time.tm_isdst = -1;
287 err = rtc_set_alarm(class_dev, &alarm);
288 break;
289
290 case RTC_RD_TIME:
291 err = rtc_read_time(class_dev, &tm);
292 if (err < 0)
293 return err;
294
295 if (copy_to_user(uarg, &tm, sizeof(tm)))
296 return -EFAULT;
297 break;
298
299 case RTC_SET_TIME:
Alessandro Zummoe8242902006-03-27 01:16:41 -0800300 if (copy_from_user(&tm, uarg, sizeof(tm)))
301 return -EFAULT;
302
303 err = rtc_set_time(class_dev, &tm);
304 break;
David Brownell2601a462006-11-25 11:09:27 -0800305
306 case RTC_IRQP_READ:
307 if (ops->irq_set_freq)
Al Virod76fdf72007-02-09 16:38:05 +0000308 err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
David Brownell2601a462006-11-25 11:09:27 -0800309 break;
310
311 case RTC_IRQP_SET:
312 if (ops->irq_set_freq)
313 err = rtc_irq_set_freq(class_dev, rtc->irq_task, arg);
314 break;
315
Alessandro Zummoe8242902006-03-27 01:16:41 -0800316#if 0
317 case RTC_EPOCH_SET:
318#ifndef rtc_epoch
319 /*
320 * There were no RTC clocks before 1900.
321 */
322 if (arg < 1900) {
323 err = -EINVAL;
324 break;
325 }
Alessandro Zummoe8242902006-03-27 01:16:41 -0800326 rtc_epoch = arg;
327 err = 0;
328#endif
329 break;
330
331 case RTC_EPOCH_READ:
332 err = put_user(rtc_epoch, (unsigned long __user *)uarg);
333 break;
334#endif
335 case RTC_WKALM_SET:
336 if (copy_from_user(&alarm, uarg, sizeof(alarm)))
337 return -EFAULT;
338
339 err = rtc_set_alarm(class_dev, &alarm);
340 break;
341
342 case RTC_WKALM_RD:
343 err = rtc_read_alarm(class_dev, &alarm);
344 if (err < 0)
345 return err;
346
347 if (copy_to_user(uarg, &alarm, sizeof(alarm)))
348 return -EFAULT;
349 break;
350
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700351#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
352 case RTC_UIE_OFF:
353 clear_uie(rtc);
354 return 0;
355
356 case RTC_UIE_ON:
357 return set_uie(rtc);
358#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800359 default:
Alessandro Zummob3969e52006-05-20 15:00:29 -0700360 err = -ENOTTY;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800361 break;
362 }
363
364 return err;
365}
366
367static int rtc_dev_release(struct inode *inode, struct file *file)
368{
369 struct rtc_device *rtc = to_rtc_device(file->private_data);
370
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700371#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
372 clear_uie(rtc);
373#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800374 if (rtc->ops->release)
375 rtc->ops->release(rtc->class_dev.dev);
376
377 mutex_unlock(&rtc->char_lock);
378 return 0;
379}
380
381static int rtc_dev_fasync(int fd, struct file *file, int on)
382{
383 struct rtc_device *rtc = to_rtc_device(file->private_data);
384 return fasync_helper(fd, file, on, &rtc->async_queue);
385}
386
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800387static const struct file_operations rtc_dev_fops = {
Alessandro Zummoe8242902006-03-27 01:16:41 -0800388 .owner = THIS_MODULE,
389 .llseek = no_llseek,
390 .read = rtc_dev_read,
391 .poll = rtc_dev_poll,
392 .ioctl = rtc_dev_ioctl,
393 .open = rtc_dev_open,
394 .release = rtc_dev_release,
395 .fasync = rtc_dev_fasync,
396};
397
398/* insertion/removal hooks */
399
David Brownell5726fb22007-05-08 00:33:27 -0700400void rtc_dev_add_device(struct rtc_device *rtc)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800401{
David Brownell5726fb22007-05-08 00:33:27 -0700402 if (!rtc_devt)
403 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800404
405 if (rtc->id >= RTC_DEV_MAX) {
David Brownell5726fb22007-05-08 00:33:27 -0700406 pr_debug("%s: too many RTC devices\n", rtc->name);
407 return;
Alessandro Zummoe8242902006-03-27 01:16:41 -0800408 }
409
David Brownell5726fb22007-05-08 00:33:27 -0700410 rtc->class_dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);
411
Alessandro Zummoe8242902006-03-27 01:16:41 -0800412 mutex_init(&rtc->char_lock);
413 spin_lock_init(&rtc->irq_lock);
414 init_waitqueue_head(&rtc->irq_queue);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700415#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
David Howellsc4028952006-11-22 14:57:56 +0000416 INIT_WORK(&rtc->uie_task, rtc_uie_task);
Atsushi Nemoto655066c2006-06-25 05:48:17 -0700417 setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
418#endif
Alessandro Zummoe8242902006-03-27 01:16:41 -0800419
420 cdev_init(&rtc->char_dev, &rtc_dev_fops);
421 rtc->char_dev.owner = rtc->owner;
422
David Brownell5726fb22007-05-08 00:33:27 -0700423 if (cdev_add(&rtc->char_dev, rtc->class_dev.devt, 1))
424 printk(KERN_WARNING "%s: failed to add char device %d:%d\n",
425 rtc->name, MAJOR(rtc_devt), rtc->id);
426 else
427 pr_debug("%s: dev (%d:%d)\n", rtc->name,
Alessandro Zummoe8242902006-03-27 01:16:41 -0800428 MAJOR(rtc_devt), rtc->id);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800429}
430
David Brownell5726fb22007-05-08 00:33:27 -0700431void rtc_dev_del_device(struct rtc_device *rtc)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800432{
David Brownell5726fb22007-05-08 00:33:27 -0700433 if (rtc->class_dev.devt)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800434 cdev_del(&rtc->char_dev);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800435}
436
David Brownell5726fb22007-05-08 00:33:27 -0700437void __init rtc_dev_init(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800438{
439 int err;
440
Alessandro Zummoe8242902006-03-27 01:16:41 -0800441 err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
David Brownell5726fb22007-05-08 00:33:27 -0700442 if (err < 0)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800443 printk(KERN_ERR "%s: failed to allocate char dev region\n",
444 __FILE__);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800445}
446
David Brownell5726fb22007-05-08 00:33:27 -0700447void __exit rtc_dev_exit(void)
Alessandro Zummoe8242902006-03-27 01:16:41 -0800448{
David Brownell5726fb22007-05-08 00:33:27 -0700449 if (rtc_devt)
450 unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
Alessandro Zummoe8242902006-03-27 01:16:41 -0800451}