Commit | Line | Data |
---|---|---|
e824290e AZ |
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> | |
5726fb20 | 16 | #include "rtc-core.h" |
e824290e | 17 | |
e824290e AZ |
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); | |
ff8371ac | 27 | const struct rtc_class_ops *ops = rtc->ops; |
e824290e AZ |
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 | ||
ab6a2d70 | 35 | file->private_data = rtc; |
e824290e | 36 | |
cd966209 | 37 | err = ops->open ? ops->open(rtc->dev.parent) : 0; |
e824290e AZ |
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 | ||
655066c3 AN |
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 | */ | |
c4028958 | 56 | static void rtc_uie_task(struct work_struct *work) |
655066c3 | 57 | { |
c4028958 DH |
58 | struct rtc_device *rtc = |
59 | container_of(work, struct rtc_device, uie_task); | |
655066c3 AN |
60 | struct rtc_time tm; |
61 | int num = 0; | |
62 | int err; | |
63 | ||
ab6a2d70 | 64 | err = rtc_read_time(rtc, &tm); |
d728b1e6 DB |
65 | |
66 | local_irq_disable(); | |
67 | spin_lock(&rtc->irq_lock); | |
655066c3 AN |
68 | 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 | } | |
d728b1e6 | 80 | spin_unlock(&rtc->irq_lock); |
655066c3 | 81 | if (num) |
ab6a2d70 | 82 | rtc_update_irq(rtc, num, RTC_UF | RTC_IRQF); |
d728b1e6 | 83 | local_irq_enable(); |
655066c3 | 84 | } |
655066c3 AN |
85 | static 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 | ||
98 | static 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 | ||
119 | static int set_uie(struct rtc_device *rtc) | |
120 | { | |
121 | struct rtc_time tm; | |
122 | int err; | |
123 | ||
ab6a2d70 | 124 | err = rtc_read_time(rtc, &tm); |
655066c3 AN |
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 */ | |
e824290e AZ |
141 | |
142 | static ssize_t | |
143 | rtc_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 | ||
3418ff76 | 151 | if (count != sizeof(unsigned int) && count < sizeof(unsigned long)) |
e824290e AZ |
152 | 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) | |
cd966209 | 183 | data = rtc->ops->read_callback(rtc->dev.parent, |
3418ff76 AN |
184 | data); |
185 | ||
186 | 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); | |
e824290e AZ |
193 | } |
194 | return ret; | |
195 | } | |
196 | ||
197 | static 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 | ||
209 | static int rtc_dev_ioctl(struct inode *inode, struct file *file, | |
210 | unsigned int cmd, unsigned long arg) | |
211 | { | |
212 | int err = 0; | |
ab6a2d70 | 213 | struct rtc_device *rtc = file->private_data; |
ff8371ac | 214 | const struct rtc_class_ops *ops = rtc->ops; |
e824290e AZ |
215 | struct rtc_time tm; |
216 | struct rtc_wkalrm alarm; | |
217 | void __user *uarg = (void __user *) arg; | |
218 | ||
2601a464 | 219 | /* check that the calling task has appropriate permissions |
110d693d AZ |
220 | * 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 | ||
e824290e AZ |
241 | /* avoid conflicting IRQ users */ |
242 | if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) { | |
d728b1e6 | 243 | spin_lock_irq(&rtc->irq_task_lock); |
e824290e AZ |
244 | if (rtc->irq_task) |
245 | err = -EBUSY; | |
d728b1e6 | 246 | spin_unlock_irq(&rtc->irq_task_lock); |
e824290e AZ |
247 | |
248 | if (err < 0) | |
249 | return err; | |
250 | } | |
251 | ||
252 | /* try the driver's ioctl interface */ | |
253 | if (ops->ioctl) { | |
cd966209 | 254 | err = ops->ioctl(rtc->dev.parent, cmd, arg); |
b3969e58 | 255 | if (err != -ENOIOCTLCMD) |
e824290e AZ |
256 | return err; |
257 | } | |
258 | ||
259 | /* if the driver does not provide the ioctl interface | |
260 | * or if that particular ioctl was not implemented | |
b3969e58 | 261 | * (-ENOIOCTLCMD), we will try to emulate here. |
e824290e AZ |
262 | */ |
263 | ||
264 | switch (cmd) { | |
265 | case RTC_ALM_READ: | |
ab6a2d70 | 266 | err = rtc_read_alarm(rtc, &alarm); |
e824290e AZ |
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; | |
ab6a2d70 | 286 | err = rtc_set_alarm(rtc, &alarm); |
e824290e AZ |
287 | break; |
288 | ||
289 | case RTC_RD_TIME: | |
ab6a2d70 | 290 | err = rtc_read_time(rtc, &tm); |
e824290e AZ |
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: | |
e824290e AZ |
299 | if (copy_from_user(&tm, uarg, sizeof(tm))) |
300 | return -EFAULT; | |
301 | ||
ab6a2d70 | 302 | err = rtc_set_time(rtc, &tm); |
e824290e | 303 | break; |
2601a464 DB |
304 | |
305 | case RTC_IRQP_READ: | |
306 | if (ops->irq_set_freq) | |
d76fdf75 | 307 | err = put_user(rtc->irq_freq, (unsigned long __user *)uarg); |
2601a464 DB |
308 | break; |
309 | ||
310 | case RTC_IRQP_SET: | |
311 | if (ops->irq_set_freq) | |
ab6a2d70 | 312 | err = rtc_irq_set_freq(rtc, rtc->irq_task, arg); |
2601a464 DB |
313 | break; |
314 | ||
e824290e AZ |
315 | #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 | } | |
e824290e AZ |
325 | 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 | ||
ab6a2d70 | 338 | err = rtc_set_alarm(rtc, &alarm); |
e824290e AZ |
339 | break; |
340 | ||
341 | case RTC_WKALM_RD: | |
ab6a2d70 | 342 | err = rtc_read_alarm(rtc, &alarm); |
e824290e AZ |
343 | if (err < 0) |
344 | return err; | |
345 | ||
346 | if (copy_to_user(uarg, &alarm, sizeof(alarm))) | |
347 | return -EFAULT; | |
348 | break; | |
349 | ||
655066c3 AN |
350 | #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 | |
e824290e | 358 | default: |
b3969e58 | 359 | err = -ENOTTY; |
e824290e AZ |
360 | break; |
361 | } | |
362 | ||
363 | return err; | |
364 | } | |
365 | ||
366 | static int rtc_dev_release(struct inode *inode, struct file *file) | |
367 | { | |
368 | struct rtc_device *rtc = to_rtc_device(file->private_data); | |
369 | ||
655066c3 AN |
370 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
371 | clear_uie(rtc); | |
372 | #endif | |
e824290e | 373 | if (rtc->ops->release) |
cd966209 | 374 | rtc->ops->release(rtc->dev.parent); |
e824290e AZ |
375 | |
376 | mutex_unlock(&rtc->char_lock); | |
377 | return 0; | |
378 | } | |
379 | ||
380 | static 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 | ||
d54b1fdb | 386 | static const struct file_operations rtc_dev_fops = { |
e824290e AZ |
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 | ||
cb3a58d2 | 399 | void rtc_dev_prepare(struct rtc_device *rtc) |
e824290e | 400 | { |
5726fb20 DB |
401 | if (!rtc_devt) |
402 | return; | |
e824290e AZ |
403 | |
404 | if (rtc->id >= RTC_DEV_MAX) { | |
5726fb20 DB |
405 | pr_debug("%s: too many RTC devices\n", rtc->name); |
406 | return; | |
e824290e AZ |
407 | } |
408 | ||
cd966209 | 409 | rtc->dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id); |
5726fb20 | 410 | |
e824290e AZ |
411 | mutex_init(&rtc->char_lock); |
412 | spin_lock_init(&rtc->irq_lock); | |
413 | init_waitqueue_head(&rtc->irq_queue); | |
655066c3 | 414 | #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL |
c4028958 | 415 | INIT_WORK(&rtc->uie_task, rtc_uie_task); |
655066c3 AN |
416 | setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc); |
417 | #endif | |
e824290e AZ |
418 | |
419 | cdev_init(&rtc->char_dev, &rtc_dev_fops); | |
420 | rtc->char_dev.owner = rtc->owner; | |
cb3a58d2 | 421 | } |
e824290e | 422 | |
cb3a58d2 DB |
423 | void rtc_dev_add_device(struct rtc_device *rtc) |
424 | { | |
cd966209 | 425 | if (cdev_add(&rtc->char_dev, rtc->dev.devt, 1)) |
5726fb20 DB |
426 | printk(KERN_WARNING "%s: failed to add char device %d:%d\n", |
427 | rtc->name, MAJOR(rtc_devt), rtc->id); | |
428 | else | |
429 | pr_debug("%s: dev (%d:%d)\n", rtc->name, | |
e824290e | 430 | MAJOR(rtc_devt), rtc->id); |
e824290e AZ |
431 | } |
432 | ||
5726fb20 | 433 | void rtc_dev_del_device(struct rtc_device *rtc) |
e824290e | 434 | { |
cd966209 | 435 | if (rtc->dev.devt) |
e824290e | 436 | cdev_del(&rtc->char_dev); |
e824290e AZ |
437 | } |
438 | ||
5726fb20 | 439 | void __init rtc_dev_init(void) |
e824290e AZ |
440 | { |
441 | int err; | |
442 | ||
e824290e | 443 | err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc"); |
5726fb20 | 444 | if (err < 0) |
e824290e AZ |
445 | printk(KERN_ERR "%s: failed to allocate char dev region\n", |
446 | __FILE__); | |
e824290e AZ |
447 | } |
448 | ||
5726fb20 | 449 | void __exit rtc_dev_exit(void) |
e824290e | 450 | { |
5726fb20 DB |
451 | if (rtc_devt) |
452 | unregister_chrdev_region(rtc_devt, RTC_DEV_MAX); | |
e824290e | 453 | } |