📄 menelaus-rtc.c
字号:
/* * Menelaus Real Time Clock interface for Linux * * Based on drivers/char/omap-rtc.c: * * Copyright (C) 2003 MontaVista Software, Inc. * Author: George G. Davis <gdavis@mvista.com> or <source@mvista.com> * * Initially based on linux-2.4.20/drivers/char/rtc.c * 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 from the * RTC via IRQs. Then the /dev/rtc interface can be used to make * use of RTC interrupts, be they time update 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. */#include <linux/config.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/init.h>#include <linux/poll.h>#include <linux/proc_fs.h>#include <linux/spinlock.h>#include <linux/rtc.h>#include <linux/interrupt.h>#include <asm/io.h>#include <asm/uaccess.h>#include <asm/system.h>#include <asm/hardware.h>#include <asm/irq.h>#include <asm/mach/irq.h>#include <asm/arch/menelaus.h>#include <asm/arch/sys_info.h>extern spinlock_t rtc_lock;/* Local BCD/BIN conversion macros: */#ifdef BCD_TO_BIN#undef BCD_TO_BIN#endif#define BCD_TO_BIN(val) ((val)=((val)&15) + ((val)>>4)*10) #ifdef BIN_TO_BCD#undef BIN_TO_BCD#endif#define BIN_TO_BCD(val) ((val)=(((val)/10)<<4) + (val)%10)#define menelaus_read_err(r, reg, act_if_fail) \ do { \ if ((*r = menelaus_read(reg)) == -1) { \ act_if_fail; \ } \ } while (0);#define menelaus_write_err(w, reg, act_if_fail) \ do { \ if (menelaus_write(w, reg) == -1) { \ act_if_fail; \ } \ } while (0);static int rtc_alarm_irq = IH_MENELAUS_BASE + 9;static int rtc_timer_irq = IH_MENELAUS_BASE + 8;static struct fasync_struct *rtc_async_queue;static DECLARE_WAIT_QUEUE_HEAD(rtc_wait);static ssize_t rtc_read(struct file *file, char *buf, size_t count, loff_t *ppos);static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg);static unsigned int rtc_poll(struct file *file, poll_table *wait);static int get_rtc_time (struct rtc_time *rtc_tm);static int get_rtc_alm_time (struct rtc_time *alm_tm);static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data);/* * Bits in rtc_status. (7 bits of room for future expansion) */#define RTC_IS_OPEN 0x01 /* means /dev/rtc is in use */#define RTC_AL_EN 0x02 /* means alarm interrupts are enabled */#define RTC_TIMER_EN 0x03 /* means timer interrupts are enabled */static unsigned long rtc_freq = 0; /* Current periodic IRQ rate: bit 0: every second bit 1: every minute bit 2: every hour bit 3: every day */static unsigned long rtc_status = 0; /* bitmapped status byte. */static unsigned long rtc_irq_data = 0; /* our output to the world *//* * 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};static irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs){ /* * Alarm or timer interrupt */ spin_lock (&rtc_lock); rtc_irq_data += 0x100; rtc_irq_data &= ~0xff;// rtc_irq_data |= menelaus_read(OMAP_RTC_STATUS_REG); spin_unlock (&rtc_lock); /* Now do the rest of the actions */ wake_up_interruptible(&rtc_wait); kill_fasync (&rtc_async_queue, SIGIO, POLL_IN); return IRQ_HANDLED;}static ssize_t rtc_read(struct file *file, char *buf, size_t count, loff_t *ppos){ DECLARE_WAITQUEUE(wait, current); unsigned long data; ssize_t retval; if (count < sizeof(unsigned long)) return -EINVAL; add_wait_queue(&rtc_wait, &wait); set_current_state(TASK_INTERRUPTIBLE); for (;;) { spin_lock_irq (&rtc_lock); data = rtc_irq_data; if (data != 0) { rtc_irq_data = 0; break; } spin_unlock_irq (&rtc_lock); if (file->f_flags & O_NONBLOCK) { retval = -EAGAIN; goto out; } if (signal_pending(current)) { retval = -ERESTARTSYS; goto out; } schedule(); } spin_unlock_irq (&rtc_lock); retval = put_user(data, (unsigned long *)buf); if (!retval) retval = sizeof(unsigned long); out: set_current_state(TASK_RUNNING); remove_wait_queue(&rtc_wait, &wait); return retval;}static int rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ struct rtc_time wtime; switch (cmd) { case RTC_AIE_OFF: /* Disable alarm interrupts */ { int en; if (rtc_alarm_irq == NO_IRQ) return -EINVAL; spin_lock_irq (&rtc_lock); en = rtc_status & RTC_AL_EN; spin_unlock_irq (&rtc_lock); if (en) disable_irq(rtc_alarm_irq); return 0; } case RTC_AIE_ON: /* Enable alarm interrupts */ { int en; if (rtc_alarm_irq == NO_IRQ) return -EINVAL; spin_lock_irq (&rtc_lock); en = rtc_status & RTC_AL_EN; spin_unlock_irq (&rtc_lock); if (!en) enable_irq(rtc_alarm_irq); return 0; } 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)); if (get_rtc_alm_time(&wtime) == -1) return -1; break; } case RTC_ALM_SET: /* Store a time into the alarm */ { struct rtc_time alm_tm; unsigned char mon, day, hrs, min, sec, leap_yr; unsigned int yrs; if (copy_from_user(&alm_tm, (struct rtc_time*)arg, sizeof(struct rtc_time))) return -EFAULT; yrs = alm_tm.tm_year + 1900; mon = alm_tm.tm_mon + 1; day = alm_tm.tm_mday; hrs = alm_tm.tm_hour; min = alm_tm.tm_min; sec = alm_tm.tm_sec; if (yrs < 1970) return -EINVAL; leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); if ((mon > 12) || (day == 0)) return -EINVAL; if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr))) return -EINVAL; if ((hrs >= 24) || (min >= 60) || (sec >= 60)) return -EINVAL; if ((yrs -= epoch) > 255) /* They are unsigned */ return -EINVAL; if (yrs > 169) { return -EINVAL; } if (yrs >= 100) yrs -= 100; BIN_TO_BCD(sec); BIN_TO_BCD(min); BIN_TO_BCD(hrs); BIN_TO_BCD(day); BIN_TO_BCD(mon); BIN_TO_BCD(yrs); spin_lock_irq(&rtc_lock); { s32 r; menelaus_read_err(&r, MENELAUS_RTC_CTRL, return -EIO); menelaus_write_err(r | 1<<1, MENELAUS_RTC_CTRL, return -EIO); menelaus_write_err(yrs, MENELAUS_RTC_AL_YR, return -EIO); menelaus_write_err(mon, MENELAUS_RTC_AL_MON, return -EIO); menelaus_write_err(day, MENELAUS_RTC_AL_DAY, return -EIO); menelaus_write_err(hrs, MENELAUS_RTC_AL_HR, return -EIO); menelaus_write_err(min, MENELAUS_RTC_AL_MIN, return -EIO); menelaus_write_err(sec, MENELAUS_RTC_AL_SEC, return -EIO); } spin_unlock_irq(&rtc_lock); return 0; } case RTC_PIE_OFF: /* Disable timer interrupts */ { int en; if (rtc_timer_irq == NO_IRQ) return -EINVAL; spin_lock_irq (&rtc_lock); en = rtc_status & RTC_TIMER_EN; spin_unlock_irq (&rtc_lock); if (en) disable_irq(rtc_timer_irq); return 0; } case RTC_PIE_ON: /* Enable timer interrupts */ { int en; if (rtc_timer_irq == NO_IRQ) return -EINVAL; spin_lock_irq (&rtc_lock); en = rtc_status & RTC_AL_EN; spin_unlock_irq (&rtc_lock); if (!en) enable_irq(rtc_timer_irq); return 0; } case RTC_IRQP_READ: /* Read the periodic IRQ rate. */ { int r; if (rtc_timer_irq == NO_IRQ) return -EINVAL; spin_lock_irq(&rtc_lock); r = (rtc_freq == 0) ? 1 : 0; spin_unlock_irq(&rtc_lock); return put_user(r, (unsigned long __user *)arg); } case RTC_IRQP_SET: /* Set periodic IRQ rate. */ { if (arg != 1) return -EINVAL; if (rtc_timer_irq == NO_IRQ) return -EINVAL; spin_lock_irq(&rtc_lock); rtc_freq = 0; spin_unlock_irq(&rtc_lock); return 0; } case RTC_RD_TIME: /* Read the time/date from RTC */ { memset(&wtime, 0, sizeof(struct rtc_time)); if (get_rtc_time(&wtime) == -1) return -1; break; } case RTC_SET_TIME: /* Set the RTC */ { struct rtc_time rtc_tm; unsigned char mon, day, hrs, min, sec, leap_yr; unsigned int yrs; int ret = 0; if (!capable(CAP_SYS_TIME)) return -EACCES; if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time))) return -EFAULT; yrs = rtc_tm.tm_year + 1900; mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */ day = rtc_tm.tm_mday; hrs = rtc_tm.tm_hour; min = rtc_tm.tm_min; sec = rtc_tm.tm_sec; if (yrs < 1970) return -EINVAL; leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); if ((mon > 12) || (day == 0))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -