rtc-dev.c 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * RTC subsystem, dev interface
 *
 * Copyright (C) 2005 Tower Technologies
 * Author: Alessandro Zummo <a.zummo@towertech.it>
 *
 * based on arch/arm/common/rtctime.c
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

#include <linux/module.h>
#include <linux/rtc.h>
16
#include "rtc-core.h"
17 18 19 20 21 22 23 24 25 26

static dev_t rtc_devt;

#define RTC_DEV_MAX 16 /* 16 RTCs should be enough for everyone... */

static int rtc_dev_open(struct inode *inode, struct file *file)
{
	int err;
	struct rtc_device *rtc = container_of(inode->i_cdev,
					struct rtc_device, char_dev);
27
	const struct rtc_class_ops *ops = rtc->ops;
28 29 30 31 32 33 34

	/* We keep the lock as long as the device is in use
	 * and return immediately if busy
	 */
	if (!(mutex_trylock(&rtc->char_lock)))
		return -EBUSY;

35
	file->private_data = rtc;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

	err = ops->open ? ops->open(rtc->class_dev.dev) : 0;
	if (err == 0) {
		spin_lock_irq(&rtc->irq_lock);
		rtc->irq_data = 0;
		spin_unlock_irq(&rtc->irq_lock);

		return 0;
	}

	/* something has gone wrong, release the lock */
	mutex_unlock(&rtc->char_lock);
	return err;
}

51 52 53 54 55
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
/*
 * Routine to poll RTC seconds field for change as often as possible,
 * after first RTC_UIE use timer to reduce polling
 */
D
David Howells 已提交
56
static void rtc_uie_task(struct work_struct *work)
57
{
D
David Howells 已提交
58 59
	struct rtc_device *rtc =
		container_of(work, struct rtc_device, uie_task);
60 61 62 63
	struct rtc_time tm;
	int num = 0;
	int err;

64
	err = rtc_read_time(rtc, &tm);
65 66 67

	local_irq_disable();
	spin_lock(&rtc->irq_lock);
68 69 70 71 72 73 74 75 76 77 78 79
	if (rtc->stop_uie_polling || err) {
		rtc->uie_task_active = 0;
	} else if (rtc->oldsecs != tm.tm_sec) {
		num = (tm.tm_sec + 60 - rtc->oldsecs) % 60;
		rtc->oldsecs = tm.tm_sec;
		rtc->uie_timer.expires = jiffies + HZ - (HZ/10);
		rtc->uie_timer_active = 1;
		rtc->uie_task_active = 0;
		add_timer(&rtc->uie_timer);
	} else if (schedule_work(&rtc->uie_task) == 0) {
		rtc->uie_task_active = 0;
	}
80
	spin_unlock(&rtc->irq_lock);
81
	if (num)
82
		rtc_update_irq(rtc, num, RTC_UF | RTC_IRQF);
83
	local_irq_enable();
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}
static void rtc_uie_timer(unsigned long data)
{
	struct rtc_device *rtc = (struct rtc_device *)data;
	unsigned long flags;

	spin_lock_irqsave(&rtc->irq_lock, flags);
	rtc->uie_timer_active = 0;
	rtc->uie_task_active = 1;
	if ((schedule_work(&rtc->uie_task) == 0))
		rtc->uie_task_active = 0;
	spin_unlock_irqrestore(&rtc->irq_lock, flags);
}

static void clear_uie(struct rtc_device *rtc)
{
	spin_lock_irq(&rtc->irq_lock);
	if (rtc->irq_active) {
		rtc->stop_uie_polling = 1;
		if (rtc->uie_timer_active) {
			spin_unlock_irq(&rtc->irq_lock);
			del_timer_sync(&rtc->uie_timer);
			spin_lock_irq(&rtc->irq_lock);
			rtc->uie_timer_active = 0;
		}
		if (rtc->uie_task_active) {
			spin_unlock_irq(&rtc->irq_lock);
			flush_scheduled_work();
			spin_lock_irq(&rtc->irq_lock);
		}
		rtc->irq_active = 0;
	}
	spin_unlock_irq(&rtc->irq_lock);
}

static int set_uie(struct rtc_device *rtc)
{
	struct rtc_time tm;
	int err;

124
	err = rtc_read_time(rtc, &tm);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	if (err)
		return err;
	spin_lock_irq(&rtc->irq_lock);
	if (!rtc->irq_active) {
		rtc->irq_active = 1;
		rtc->stop_uie_polling = 0;
		rtc->oldsecs = tm.tm_sec;
		rtc->uie_task_active = 1;
		if (schedule_work(&rtc->uie_task) == 0)
			rtc->uie_task_active = 0;
	}
	rtc->irq_data = 0;
	spin_unlock_irq(&rtc->irq_lock);
	return 0;
}
#endif /* CONFIG_RTC_INTF_DEV_UIE_EMUL */
141 142 143 144 145 146 147 148 149 150

static ssize_t
rtc_dev_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
	struct rtc_device *rtc = to_rtc_device(file->private_data);

	DECLARE_WAITQUEUE(wait, current);
	unsigned long data;
	ssize_t ret;

151
	if (count != sizeof(unsigned int) && count < sizeof(unsigned long))
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
		return -EINVAL;

	add_wait_queue(&rtc->irq_queue, &wait);
	do {
		__set_current_state(TASK_INTERRUPTIBLE);

		spin_lock_irq(&rtc->irq_lock);
		data = rtc->irq_data;
		rtc->irq_data = 0;
		spin_unlock_irq(&rtc->irq_lock);

		if (data != 0) {
			ret = 0;
			break;
		}
		if (file->f_flags & O_NONBLOCK) {
			ret = -EAGAIN;
			break;
		}
		if (signal_pending(current)) {
			ret = -ERESTARTSYS;
			break;
		}
		schedule();
	} while (1);
	set_current_state(TASK_RUNNING);
	remove_wait_queue(&rtc->irq_queue, &wait);

	if (ret == 0) {
		/* Check for any data updates */
		if (rtc->ops->read_callback)
183 184 185 186 187 188 189 190 191 192
			data = rtc->ops->read_callback(rtc->class_dev.dev,
						       data);

		if (sizeof(int) != sizeof(long) &&
		    count == sizeof(unsigned int))
			ret = put_user(data, (unsigned int __user *)buf) ?:
				sizeof(unsigned int);
		else
			ret = put_user(data, (unsigned long __user *)buf) ?:
				sizeof(unsigned long);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
	}
	return ret;
}

static unsigned int rtc_dev_poll(struct file *file, poll_table *wait)
{
	struct rtc_device *rtc = to_rtc_device(file->private_data);
	unsigned long data;

	poll_wait(file, &rtc->irq_queue, wait);

	data = rtc->irq_data;

	return (data != 0) ? (POLLIN | POLLRDNORM) : 0;
}

static int rtc_dev_ioctl(struct inode *inode, struct file *file,
		unsigned int cmd, unsigned long arg)
{
	int err = 0;
213
	struct rtc_device *rtc = file->private_data;
214
	const struct rtc_class_ops *ops = rtc->ops;
215 216 217 218
	struct rtc_time tm;
	struct rtc_wkalrm alarm;
	void __user *uarg = (void __user *) arg;

219
	/* check that the calling task has appropriate permissions
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
	 * for certain ioctls. doing this check here is useful
	 * to avoid duplicate code in each driver.
	 */
	switch (cmd) {
	case RTC_EPOCH_SET:
	case RTC_SET_TIME:
		if (!capable(CAP_SYS_TIME))
			return -EACCES;
		break;

	case RTC_IRQP_SET:
		if (arg > rtc->max_user_freq && !capable(CAP_SYS_RESOURCE))
			return -EACCES;
		break;

	case RTC_PIE_ON:
		if (!capable(CAP_SYS_RESOURCE))
			return -EACCES;
		break;
	}

241 242
	/* avoid conflicting IRQ users */
	if (cmd == RTC_PIE_ON || cmd == RTC_PIE_OFF || cmd == RTC_IRQP_SET) {
243
		spin_lock_irq(&rtc->irq_task_lock);
244 245
		if (rtc->irq_task)
			err = -EBUSY;
246
		spin_unlock_irq(&rtc->irq_task_lock);
247 248 249 250 251 252 253

		if (err < 0)
			return err;
	}

	/* try the driver's ioctl interface */
	if (ops->ioctl) {
254
		err = ops->ioctl(rtc->class_dev.dev, cmd, arg);
255
		if (err != -ENOIOCTLCMD)
256 257 258 259 260
			return err;
	}

	/* if the driver does not provide the ioctl interface
	 * or if that particular ioctl was not implemented
261
	 * (-ENOIOCTLCMD), we will try to emulate here.
262 263 264 265
	 */

	switch (cmd) {
	case RTC_ALM_READ:
266
		err = rtc_read_alarm(rtc, &alarm);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
		if (err < 0)
			return err;

		if (copy_to_user(uarg, &alarm.time, sizeof(tm)))
			return -EFAULT;
		break;

	case RTC_ALM_SET:
		if (copy_from_user(&alarm.time, uarg, sizeof(tm)))
			return -EFAULT;

		alarm.enabled = 0;
		alarm.pending = 0;
		alarm.time.tm_mday = -1;
		alarm.time.tm_mon = -1;
		alarm.time.tm_year = -1;
		alarm.time.tm_wday = -1;
		alarm.time.tm_yday = -1;
		alarm.time.tm_isdst = -1;
286
		err = rtc_set_alarm(rtc, &alarm);
287 288 289
		break;

	case RTC_RD_TIME:
290
		err = rtc_read_time(rtc, &tm);
291 292 293 294 295 296 297 298 299 300 301
		if (err < 0)
			return err;

		if (copy_to_user(uarg, &tm, sizeof(tm)))
			return -EFAULT;
		break;

	case RTC_SET_TIME:
		if (copy_from_user(&tm, uarg, sizeof(tm)))
			return -EFAULT;

302
		err = rtc_set_time(rtc, &tm);
303
		break;
304 305 306

	case RTC_IRQP_READ:
		if (ops->irq_set_freq)
307
			err = put_user(rtc->irq_freq, (unsigned long __user *)uarg);
308 309 310 311
		break;

	case RTC_IRQP_SET:
		if (ops->irq_set_freq)
312
			err = rtc_irq_set_freq(rtc, rtc->irq_task, arg);
313 314
		break;

315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
#if 0
	case RTC_EPOCH_SET:
#ifndef rtc_epoch
		/*
		 * There were no RTC clocks before 1900.
		 */
		if (arg < 1900) {
			err = -EINVAL;
			break;
		}
		rtc_epoch = arg;
		err = 0;
#endif
		break;

	case RTC_EPOCH_READ:
		err = put_user(rtc_epoch, (unsigned long __user *)uarg);
		break;
#endif
	case RTC_WKALM_SET:
		if (copy_from_user(&alarm, uarg, sizeof(alarm)))
			return -EFAULT;

338
		err = rtc_set_alarm(rtc, &alarm);
339 340 341
		break;

	case RTC_WKALM_RD:
342
		err = rtc_read_alarm(rtc, &alarm);
343 344 345 346 347 348 349
		if (err < 0)
			return err;

		if (copy_to_user(uarg, &alarm, sizeof(alarm)))
			return -EFAULT;
		break;

350 351 352 353 354 355 356 357
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
	case RTC_UIE_OFF:
		clear_uie(rtc);
		return 0;

	case RTC_UIE_ON:
		return set_uie(rtc);
#endif
358
	default:
359
		err = -ENOTTY;
360 361 362 363 364 365 366 367 368 369
		break;
	}

	return err;
}

static int rtc_dev_release(struct inode *inode, struct file *file)
{
	struct rtc_device *rtc = to_rtc_device(file->private_data);

370 371 372
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
	clear_uie(rtc);
#endif
373 374 375 376 377 378 379 380 381 382 383 384 385
	if (rtc->ops->release)
		rtc->ops->release(rtc->class_dev.dev);

	mutex_unlock(&rtc->char_lock);
	return 0;
}

static int rtc_dev_fasync(int fd, struct file *file, int on)
{
	struct rtc_device *rtc = to_rtc_device(file->private_data);
	return fasync_helper(fd, file, on, &rtc->async_queue);
}

386
static const struct file_operations rtc_dev_fops = {
387 388 389 390 391 392 393 394 395 396 397 398
	.owner		= THIS_MODULE,
	.llseek		= no_llseek,
	.read		= rtc_dev_read,
	.poll		= rtc_dev_poll,
	.ioctl		= rtc_dev_ioctl,
	.open		= rtc_dev_open,
	.release	= rtc_dev_release,
	.fasync		= rtc_dev_fasync,
};

/* insertion/removal hooks */

399
void rtc_dev_add_device(struct rtc_device *rtc)
400
{
401 402
	if (!rtc_devt)
		return;
403 404

	if (rtc->id >= RTC_DEV_MAX) {
405 406
		pr_debug("%s: too many RTC devices\n", rtc->name);
		return;
407 408
	}

409 410
	rtc->class_dev.devt = MKDEV(MAJOR(rtc_devt), rtc->id);

411 412 413
	mutex_init(&rtc->char_lock);
	spin_lock_init(&rtc->irq_lock);
	init_waitqueue_head(&rtc->irq_queue);
414
#ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
D
David Howells 已提交
415
	INIT_WORK(&rtc->uie_task, rtc_uie_task);
416 417
	setup_timer(&rtc->uie_timer, rtc_uie_timer, (unsigned long)rtc);
#endif
418 419 420 421

	cdev_init(&rtc->char_dev, &rtc_dev_fops);
	rtc->char_dev.owner = rtc->owner;

422 423 424 425 426
	if (cdev_add(&rtc->char_dev, rtc->class_dev.devt, 1))
		printk(KERN_WARNING "%s: failed to add char device %d:%d\n",
			rtc->name, MAJOR(rtc_devt), rtc->id);
	else
		pr_debug("%s: dev (%d:%d)\n", rtc->name,
427 428 429
			MAJOR(rtc_devt), rtc->id);
}

430
void rtc_dev_del_device(struct rtc_device *rtc)
431
{
432
	if (rtc->class_dev.devt)
433 434 435
		cdev_del(&rtc->char_dev);
}

436
void __init rtc_dev_init(void)
437 438 439 440
{
	int err;

	err = alloc_chrdev_region(&rtc_devt, 0, RTC_DEV_MAX, "rtc");
441
	if (err < 0)
442 443 444 445
		printk(KERN_ERR "%s: failed to allocate char dev region\n",
			__FILE__);
}

446
void __exit rtc_dev_exit(void)
447
{
448 449
	if (rtc_devt)
		unregister_chrdev_region(rtc_devt, RTC_DEV_MAX);
450
}