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