1427 lines · 33.5 KB · C
RawDownload
/* *	Real Time Clock interface for Linux * *	Copyright (C) 1996 Paul Gortmaker * *	This driver allows use of the real time clock (built into *	nearly all computers) from user space. It exports the /dev/rtc *	interface supporting various ioctl() and also the *	/proc/driver/rtc pseudo-file for status information. * *	The ioctls can be used to set the interrupt behaviour and *	generation rate from the RTC via IRQ 8. Then the /dev/rtc *	interface can be used to make use of these timer interrupts, *	be they interval or alarm based. * *	The /dev/rtc interface will block on reads until an interrupt *	has been received. If a RTC interrupt has already happened, *	it will output an unsigned long and then block. The output value *	contains the interrupt status in the low byte and the number of *	interrupts since the last read in the remaining high bytes. The *	/dev/rtc interface can also be used with the select(2) call. * *	This program is free software; you can redistribute it and/or *	modify it under the terms of the GNU General Public License *	as published by the Free Software Foundation; either version *	2 of the License, or (at your option) any later version. * *	Based on other minimal char device drivers, like Alan's *	watchdog, Ted's random, etc. etc. * *	1.07	Paul Gortmaker. *	1.08	Miquel van Smoorenburg: disallow certain things on the *		DEC Alpha as the CMOS clock is also used for other things. *	1.09	Nikita Schmidt: epoch support and some Alpha cleanup. *	1.09a	Pete Zaitcev: Sun SPARC *	1.09b	Jeff Garzik: Modularize, init cleanup *	1.09c	Jeff Garzik: SMP cleanup *	1.10	Paul Barton-Davis: add support for async I/O *	1.10a	Andrea Arcangeli: Alpha updates *	1.10b	Andrew Morton: SMP lock fix *	1.10c	Cesar Barros: SMP locking fixes and cleanup *	1.10d	Paul Gortmaker: delete paranoia check in rtc_exit *	1.10e	Maciej W. Rozycki: Handle DECstation's year weirdness. *	1.11	Takashi Iwai: Kernel access functions *			      rtc_register/rtc_unregister/rtc_control *      1.11a   Daniele Bellucci: Audit create_proc_read_entry in rtc_init *	1.12	Venkatesh Pallipadi: Hooks for emulating rtc on HPET base-timer *		CONFIG_HPET_EMULATE_RTC *	1.12a	Maciej W. Rozycki: Handle memory-mapped chips properly. *	1.12ac	Alan Cox: Allow read access to the day of week register *	1.12b	David John: Remove calls to the BKL. */ #define RTC_VERSION		"1.12b" /* *	Note that *all* calls to CMOS_READ and CMOS_WRITE are done with *	interrupts disabled. Due to the index-port/data-port (0x70/0x71) *	design of the RTC, we don't want two different things trying to *	get to it at once. (e.g. the periodic 11 min sync from *      kernel/time/ntp.c vs. this driver.) */ #include <linux/interrupt.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/miscdevice.h>#include <linux/ioport.h>#include <linux/fcntl.h>#include <linux/mc146818rtc.h>#include <linux/init.h>#include <linux/poll.h>#include <linux/proc_fs.h>#include <linux/seq_file.h>#include <linux/spinlock.h>#include <linux/sched/signal.h>#include <linux/sysctl.h>#include <linux/wait.h>#include <linux/bcd.h>#include <linux/delay.h>#include <linux/uaccess.h>#include <linux/ratelimit.h> #include <asm/current.h> #ifdef CONFIG_X86#include <asm/hpet.h>#endif #ifdef CONFIG_SPARC32#include <linux/of.h>#include <linux/of_device.h>#include <asm/io.h> static unsigned long rtc_port;static int rtc_irq;#endif #ifdef	CONFIG_HPET_EMULATE_RTC#undef	RTC_IRQ#endif #ifdef RTC_IRQstatic int rtc_has_irq = 1;#endif #ifndef CONFIG_HPET_EMULATE_RTC#define is_hpet_enabled()			0#define hpet_set_alarm_time(hrs, min, sec)	0#define hpet_set_periodic_freq(arg)		0#define hpet_mask_rtc_irq_bit(arg)		0#define hpet_set_rtc_irq_bit(arg)		0#define hpet_rtc_timer_init()			do { } while (0)#define hpet_rtc_dropped_irq()			0#define hpet_register_irq_handler(h)		({ 0; })#define hpet_unregister_irq_handler(h)		({ 0; })#ifdef RTC_IRQstatic irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id){	return 0;}#endif#endif /* *	We sponge a minor off of the misc major. No need slurping *	up another valuable major dev number for this. If you add *	an ioctl, make sure you don't conflict with SPARC's RTC *	ioctls. */ static struct fasync_struct *rtc_async_queue; static DECLARE_WAIT_QUEUE_HEAD(rtc_wait); #ifdef RTC_IRQstatic void rtc_dropped_irq(unsigned long data); static DEFINE_TIMER(rtc_irq_timer, rtc_dropped_irq, 0, 0);#endif static ssize_t rtc_read(struct file *file, char __user *buf,			size_t count, loff_t *ppos); static long rtc_ioctl(struct file *file, unsigned int cmd, unsigned long arg);static void rtc_get_rtc_time(struct rtc_time *rtc_tm); #ifdef RTC_IRQstatic unsigned int rtc_poll(struct file *file, poll_table *wait);#endif static void get_rtc_alm_time(struct rtc_time *alm_tm);#ifdef RTC_IRQstatic void set_rtc_irq_bit_locked(unsigned char bit);static void mask_rtc_irq_bit_locked(unsigned char bit); static inline void set_rtc_irq_bit(unsigned char bit){	spin_lock_irq(&rtc_lock);	set_rtc_irq_bit_locked(bit);	spin_unlock_irq(&rtc_lock);} static void mask_rtc_irq_bit(unsigned char bit){	spin_lock_irq(&rtc_lock);	mask_rtc_irq_bit_locked(bit);	spin_unlock_irq(&rtc_lock);}#endif #ifdef CONFIG_PROC_FSstatic int rtc_proc_open(struct inode *inode, struct file *file);#endif /* *	Bits in rtc_status. (6 bits of room for future expansion) */ #define RTC_IS_OPEN		0x01	/* means /dev/rtc is in use	*/#define RTC_TIMER_ON		0x02	/* missed irq timer active	*/ /* * rtc_status is never changed by rtc_interrupt, and ioctl/open/close is * protected by the spin lock rtc_lock. However, ioctl can still disable the * timer in rtc_status and then with del_timer after the interrupt has read * rtc_status but before mod_timer is called, which would then reenable the * timer (but you would need to have an awful timing before you'd trip on it) */static unsigned long rtc_status;	/* bitmapped status byte.	*/static unsigned long rtc_freq;		/* Current periodic IRQ rate	*/static unsigned long rtc_irq_data;	/* our output to the world	*/static unsigned long rtc_max_user_freq = 64; /* > this, need CAP_SYS_RESOURCE */ #ifdef RTC_IRQ/* * rtc_task_lock nests inside rtc_lock. */static DEFINE_SPINLOCK(rtc_task_lock);static rtc_task_t *rtc_callback;#endif /* *	If this driver ever becomes modularised, it will be really nice *	to make the epoch retain its value across module reload... */ static unsigned long epoch = 1900;	/* year corresponding to 0x00	*/ static const unsigned char days_in_mo[] ={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; /* * Returns true if a clock update is in progress */static inline unsigned char rtc_is_updating(void){	unsigned long flags;	unsigned char uip; 	spin_lock_irqsave(&rtc_lock, flags);	uip = (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP);	spin_unlock_irqrestore(&rtc_lock, flags);	return uip;} #ifdef RTC_IRQ/* *	A very tiny interrupt handler. It runs with interrupts disabled, *	but there is possibility of conflicting with the set_rtc_mmss() *	call (the rtc irq and the timer irq can easily run at the same *	time in two different CPUs). So we need to serialize *	accesses to the chip with the rtc_lock spinlock that each *	architecture should implement in the timer code. *	(See ./arch/XXXX/kernel/time.c for the set_rtc_mmss() function.) */ static irqreturn_t rtc_interrupt(int irq, void *dev_id){	/*	 *	Can be an alarm interrupt, update complete interrupt,	 *	or a periodic interrupt. We store the status in the	 *	low byte and the number of interrupts received since	 *	the last read in the remainder of rtc_irq_data.	 */ 	spin_lock(&rtc_lock);	rtc_irq_data += 0x100;	rtc_irq_data &= ~0xff;	if (is_hpet_enabled()) {		/*		 * In this case it is HPET RTC interrupt handler		 * calling us, with the interrupt information		 * passed as arg1, instead of irq.		 */		rtc_irq_data |= (unsigned long)irq & 0xF0;	} else {		rtc_irq_data |= (CMOS_READ(RTC_INTR_FLAGS) & 0xF0);	} 	if (rtc_status & RTC_TIMER_ON)		mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq + 2*HZ/100); 	spin_unlock(&rtc_lock); 	/* Now do the rest of the actions */	spin_lock(&rtc_task_lock);	if (rtc_callback)		rtc_callback->func(rtc_callback->private_data);	spin_unlock(&rtc_task_lock);	wake_up_interruptible(&rtc_wait); 	kill_fasync(&rtc_async_queue, SIGIO, POLL_IN); 	return IRQ_HANDLED;}#endif /* * sysctl-tuning infrastructure. */static struct ctl_table rtc_table[] = {	{		.procname	= "max-user-freq",		.data		= &rtc_max_user_freq,		.maxlen		= sizeof(int),		.mode		= 0644,		.proc_handler	= proc_dointvec,	},	{ }}; static struct ctl_table rtc_root[] = {	{		.procname	= "rtc",		.mode		= 0555,		.child		= rtc_table,	},	{ }}; static struct ctl_table dev_root[] = {	{		.procname	= "dev",		.mode		= 0555,		.child		= rtc_root,	},	{ }}; static struct ctl_table_header *sysctl_header; static int __init init_sysctl(void){    sysctl_header = register_sysctl_table(dev_root);    return 0;} static void __exit cleanup_sysctl(void){    unregister_sysctl_table(sysctl_header);} /* *	Now all the various file operations that we export. */ static ssize_t rtc_read(struct file *file, char __user *buf,			size_t count, loff_t *ppos){#ifndef RTC_IRQ	return -EIO;#else	DECLARE_WAITQUEUE(wait, current);	unsigned long data;	ssize_t retval; 	if (rtc_has_irq == 0)		return -EIO; 	/*	 * Historically this function used to assume that sizeof(unsigned long)	 * is the same in userspace and kernelspace.  This lead to problems	 * for configurations with multiple ABIs such a the MIPS o32 and 64	 * ABIs supported on the same kernel.  So now we support read of both	 * 4 and 8 bytes and assume that's the sizeof(unsigned long) in the	 * userspace ABI.	 */	if (count != sizeof(unsigned int) && count !=  sizeof(unsigned long))		return -EINVAL; 	add_wait_queue(&rtc_wait, &wait); 	do {		/* First make it right. Then make it fast. Putting this whole		 * block within the parentheses of a while would be too		 * confusing. And no, xchg() is not the answer. */ 		__set_current_state(TASK_INTERRUPTIBLE); 		spin_lock_irq(&rtc_lock);		data = rtc_irq_data;		rtc_irq_data = 0;		spin_unlock_irq(&rtc_lock); 		if (data != 0)			break; 		if (file->f_flags & O_NONBLOCK) {			retval = -EAGAIN;			goto out;		}		if (signal_pending(current)) {			retval = -ERESTARTSYS;			goto out;		}		schedule();	} while (1); 	if (count == sizeof(unsigned int)) {		retval = put_user(data,				  (unsigned int __user *)buf) ?: sizeof(int);	} else {		retval = put_user(data,				  (unsigned long __user *)buf) ?: sizeof(long);	}	if (!retval)		retval = count; out:	__set_current_state(TASK_RUNNING);	remove_wait_queue(&rtc_wait, &wait); 	return retval;#endif} static int rtc_do_ioctl(unsigned int cmd, unsigned long arg, int kernel){	struct rtc_time wtime; #ifdef RTC_IRQ	if (rtc_has_irq == 0) {		switch (cmd) {		case RTC_AIE_OFF:		case RTC_AIE_ON:		case RTC_PIE_OFF:		case RTC_PIE_ON:		case RTC_UIE_OFF:		case RTC_UIE_ON:		case RTC_IRQP_READ:		case RTC_IRQP_SET:			return -EINVAL;		}	}#endif 	switch (cmd) {#ifdef RTC_IRQ	case RTC_AIE_OFF:	/* Mask alarm int. enab. bit	*/	{		mask_rtc_irq_bit(RTC_AIE);		return 0;	}	case RTC_AIE_ON:	/* Allow alarm interrupts.	*/	{		set_rtc_irq_bit(RTC_AIE);		return 0;	}	case RTC_PIE_OFF:	/* Mask periodic int. enab. bit	*/	{		/* can be called from isr via rtc_control() */		unsigned long flags; 		spin_lock_irqsave(&rtc_lock, flags);		mask_rtc_irq_bit_locked(RTC_PIE);		if (rtc_status & RTC_TIMER_ON) {			rtc_status &= ~RTC_TIMER_ON;			del_timer(&rtc_irq_timer);		}		spin_unlock_irqrestore(&rtc_lock, flags); 		return 0;	}	case RTC_PIE_ON:	/* Allow periodic ints		*/	{		/* can be called from isr via rtc_control() */		unsigned long flags; 		/*		 * We don't really want Joe User enabling more		 * than 64Hz of interrupts on a multi-user machine.		 */		if (!kernel && (rtc_freq > rtc_max_user_freq) &&						(!capable(CAP_SYS_RESOURCE)))			return -EACCES; 		spin_lock_irqsave(&rtc_lock, flags);		if (!(rtc_status & RTC_TIMER_ON)) {			mod_timer(&rtc_irq_timer, jiffies + HZ/rtc_freq +					2*HZ/100);			rtc_status |= RTC_TIMER_ON;		}		set_rtc_irq_bit_locked(RTC_PIE);		spin_unlock_irqrestore(&rtc_lock, flags); 		return 0;	}	case RTC_UIE_OFF:	/* Mask ints from RTC updates.	*/	{		mask_rtc_irq_bit(RTC_UIE);		return 0;	}	case RTC_UIE_ON:	/* Allow ints for RTC updates.	*/	{		set_rtc_irq_bit(RTC_UIE);		return 0;	}#endif	case RTC_ALM_READ:	/* Read the present alarm time */	{		/*		 * This returns a struct rtc_time. Reading >= 0xc0		 * means "don't care" or "match all". Only the tm_hour,		 * tm_min, and tm_sec values are filled in.		 */		memset(&wtime, 0, sizeof(struct rtc_time));		get_rtc_alm_time(&wtime);		break;	}	case RTC_ALM_SET:	/* Store a time into the alarm */	{		/*		 * This expects a struct rtc_time. Writing 0xff means		 * "don't care" or "match all". Only the tm_hour,		 * tm_min and tm_sec are used.		 */		unsigned char hrs, min, sec;		struct rtc_time alm_tm; 

Lines 1–500 of 1427.