📄 menelaus-rtc.c
字号:
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); menelaus_write_err(yrs, MENELAUS_RTC_YR, ret = -EIO); menelaus_write_err(mon, MENELAUS_RTC_MON, ret = -EIO); menelaus_write_err(day, MENELAUS_RTC_DAY, ret = -EIO); menelaus_write_err(hrs, MENELAUS_RTC_HR, ret = -EIO); menelaus_write_err(min, MENELAUS_RTC_MIN, ret = -EIO); menelaus_write_err(sec, MENELAUS_RTC_SEC, ret = -EIO); menelaus_write_err(0x8, MENELAUS_RTC_UPDATE, ret = -EIO); /* update everything */ spin_unlock_irq(&rtc_lock); return ret; } case RTC_EPOCH_READ: /* Read the epoch. */ { return put_user (epoch, (unsigned long *)arg); } case RTC_EPOCH_SET: /* Set the epoch. */ { /* * There were no RTC clocks before 1900. */ if (arg < 1900) return -EINVAL; if (!capable(CAP_SYS_TIME)) return -EACCES; epoch = arg; return 0; } default:#if !defined(CONFIG_ARCH_OMAP) return -ENOTTY;#else return -EINVAL;#endif } return copy_to_user((void *)arg, &wtime, sizeof wtime) ? -EFAULT : 0;}/* * We enforce only one user at a time here with the open/close. * Also clear the previous interrupt data on an open, and clean * up things on a close. */static int rtc_open(struct inode *inode, struct file *file){ spin_lock_irq (&rtc_lock); /* protect against concurrent opens */ if(rtc_status & RTC_IS_OPEN) goto out_busy; rtc_status |= RTC_IS_OPEN; rtc_irq_data = 0; spin_unlock_irq (&rtc_lock); /* enable rtc */ { s32 r; menelaus_read_err(&r, MENELAUS_RTC_CTRL, goto out_busy); menelaus_write_err(r | 1, MENELAUS_RTC_CTRL, goto out_busy); } return 0;out_busy: spin_unlock_irq (&rtc_lock); return -EBUSY;}static int rtc_fasync (int fd, struct file *filp, int on){ return fasync_helper (fd, filp, on, &rtc_async_queue);}static int rtc_release(struct inode *inode, struct file *file){ if (file->f_flags & FASYNC) { rtc_fasync (-1, file, 0); } spin_lock_irq (&rtc_lock); rtc_irq_data = 0; spin_unlock_irq (&rtc_lock); /* * No need for locking -- nobody else can do anything until this rmw * is committed, and we don't implement timer support in menelaus-rtc. */ rtc_status &= ~RTC_IS_OPEN; return 0;}static unsigned int rtc_poll(struct file *file, poll_table *wait){ unsigned long l; poll_wait(file, &rtc_wait, wait); spin_lock_irq (&rtc_lock); l = rtc_irq_data; spin_unlock_irq (&rtc_lock); if (l != 0) return POLLIN | POLLRDNORM; return 0;}static struct file_operations rtc_fops = { owner: THIS_MODULE, llseek: no_llseek, read: rtc_read, poll: rtc_poll, ioctl: rtc_ioctl, open: rtc_open, release: rtc_release, fasync: rtc_fasync,};static struct miscdevice rtc_dev = { minor: RTC_MINOR, name: "menelaus-rtc", fops: &rtc_fops};static int __init rtc_init(void){ u32 board = get_board_type();#if 0 if (request_irq(rtc_timer_irq, rtc_interrupt, SA_INTERRUPT, rtc_dev.name, NULL)) { printk(KERN_ERR "%s: RTC timer interrupt IRQ%d is not free.\n", rtc_dev.name, INT_RTC_TIMER); return -EIO; }#endif if(board != BOARD_H4_MENELAUS) return -ENODEV; if (request_irq(rtc_alarm_irq, rtc_interrupt, SA_INTERRUPT, "Menelaus RTC alarm", NULL)) { rtc_alarm_irq = NO_IRQ; } disable_irq(rtc_alarm_irq); if (request_irq(rtc_timer_irq, rtc_interrupt, SA_INTERRUPT, "Menelaus RTC timer", NULL)) { rtc_timer_irq = NO_IRQ; } disable_irq(rtc_timer_irq); if (misc_register(&rtc_dev)) { if (rtc_alarm_irq != NO_IRQ) free_irq (rtc_alarm_irq, NULL); if (rtc_timer_irq != NO_IRQ) free_irq (rtc_timer_irq, NULL); return -ENODEV; } if (!create_proc_read_entry ("driver/rtc", 0, NULL, rtc_read_proc, NULL)) { if (rtc_alarm_irq != NO_IRQ) free_irq (rtc_alarm_irq, NULL); if (rtc_timer_irq != NO_IRQ) free_irq (rtc_timer_irq, NULL); misc_deregister(&rtc_dev); return -ENOMEM; } return 0;}static void __exit rtc_exit (void){ s32 r; u32 board = get_board_type(); if (board != BOARD_H4_MENELAUS) return; menelaus_read_err(&r, MENELAUS_RTC_CTRL, ); menelaus_write_err(r & ~(1<<1 | 1), MENELAUS_RTC_CTRL, ); /* disable rtc and rtc alarms */ if (rtc_alarm_irq != NO_IRQ) free_irq (rtc_alarm_irq, NULL); if (rtc_timer_irq != NO_IRQ) free_irq (rtc_timer_irq, NULL); remove_proc_entry ("driver/rtc", NULL); misc_deregister(&rtc_dev);}/* * Info exported via "/proc/driver/rtc". */static int rtc_proc_output (char *buf){#define YN(value) ((value) ? "yes" : "no")#define NY(value) ((value) ? "no" : "yes") char *p; struct rtc_time tm; p = buf; if (get_rtc_time(&tm) == -1) return -1; /* * There is no way to tell if the luser has the RTC set for local * time or for Universal Standard Time (GMT). Probably local though. */ p += sprintf(p, "rtc_time\t: %02d:%02d:%02d\n" "rtc_date\t: %04d-%02d-%02d\n" "rtc_epoch\t: %04lu\n", tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, epoch); if (get_rtc_alm_time(&tm) == -1) return -1; /* * We implicitly assume 24hr mode here. Alarm values >= 0xc0 will * match any value for that particular field. Values that are * greater than a valid time, but less than 0xc0 shouldn't appear. */ p += sprintf(p, "alarm_time\t: %02d:%02d:%02d\n" "alarm_date\t: %04d-%02d-%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);#if 0 p += sprintf(p, "BCD\t\t: %s\n" "24hr\t\t: %s\n" "alarm_IRQ\t: %s\n" "update_IRQ\t: %s\n" "update_rate\t: %ud\n", YN(1), YN(1), YN(menelaus_read(OMAP_RTC_INTERRUPTS_REG) & OMAP_RTC_INTERRUPTS_IT_ALARM), YN(menelaus_read(OMAP_RTC_INTERRUPTS_REG) & OMAP_RTC_INTERRUPTS_IT_TIMER), menelaus_read(OMAP_RTC_INTERRUPTS_REG) & 3 /* REVISIT */);#endif return p - buf;#undef YN#undef NY}static int rtc_read_proc(char *page, char **start, off_t off, int count, int *eof, void *data){ int len = rtc_proc_output (page); if (len <= off+count) *eof = 1; *start = page + off; len -= off; if (len>count) len = count; if (len<0) len = 0; return len;}static int get_rtc_time(struct rtc_time *rtc_tm){ /* REVISIT: Fix this comment!!! * read RTC once any update in progress is done. The update * can take just over 2ms. We wait 10 to 20ms. There is no need to * to poll-wait (up to 1s - eeccch) for the falling edge of OMAP_RTC_STATUS_BUSY. * If you need to know *exactly* when a second has started, enable * periodic update complete interrupts, (via ioctl) and then * immediately read /dev/rtc which will block until you get the IRQ. * Once the read clears, read the RTC time (again via ioctl). Easy. */#if 0 /* REVISIT: This need to do as the TRM says. */ unsigned long uip_watchdog = jiffies; if (rtc_is_updating() != 0) while (jiffies - uip_watchdog < 2*HZ/100) { barrier(); cpu_relax(); }#endif /* * Only the values that we read from the RTC are set. We leave * tm_wday, tm_yday and tm_isdst untouched. Even though the * RTC has RTC_DAY_OF_WEEK, we ignore it, as it is only updated * by the RTC when initially set to a non-zero value. */ spin_lock_irq(&rtc_lock); { s32 v; menelaus_read_err(&v, MENELAUS_RTC_SEC, goto out_err); rtc_tm->tm_sec = v; menelaus_read_err(&v, MENELAUS_RTC_MIN, goto out_err); rtc_tm->tm_min = v; menelaus_read_err(&v, MENELAUS_RTC_HR, goto out_err); rtc_tm->tm_hour = v; menelaus_read_err(&v, MENELAUS_RTC_DAY, goto out_err); rtc_tm->tm_mday = v; menelaus_read_err(&v, MENELAUS_RTC_MON, goto out_err); rtc_tm->tm_mon = v; menelaus_read_err(&v, MENELAUS_RTC_YR, goto out_err); rtc_tm->tm_year = v; } spin_unlock_irq(&rtc_lock); BCD_TO_BIN(rtc_tm->tm_sec); BCD_TO_BIN(rtc_tm->tm_min); BCD_TO_BIN(rtc_tm->tm_hour); BCD_TO_BIN(rtc_tm->tm_mday); BCD_TO_BIN(rtc_tm->tm_mon); BCD_TO_BIN(rtc_tm->tm_year); /* * Account for differences between how the RTC uses the values * and how they are defined in a struct rtc_time; */ if ((rtc_tm->tm_year += (epoch - 1900)) <= 69) rtc_tm->tm_year += 100; rtc_tm->tm_mon--; return 0;out_err: spin_unlock_irq(&rtc_lock); return -1;}static int get_rtc_alm_time(struct rtc_time *alm_tm){ s32 v; spin_lock_irq(&rtc_lock); menelaus_read_err(&v, MENELAUS_RTC_AL_SEC, goto out_err); alm_tm->tm_sec = v; menelaus_read_err(&v, MENELAUS_RTC_AL_MIN, goto out_err); alm_tm->tm_min = v; menelaus_read_err(&v, MENELAUS_RTC_AL_HR, goto out_err); alm_tm->tm_hour = v; menelaus_read_err(&v, MENELAUS_RTC_AL_DAY, goto out_err); alm_tm->tm_mday = v; menelaus_read_err(&v, MENELAUS_RTC_AL_MON, goto out_err); alm_tm->tm_mon = v; menelaus_read_err(&v, MENELAUS_RTC_AL_YR, goto out_err); alm_tm->tm_year = v; spin_unlock_irq(&rtc_lock); BCD_TO_BIN(alm_tm->tm_sec); BCD_TO_BIN(alm_tm->tm_min); BCD_TO_BIN(alm_tm->tm_hour); BCD_TO_BIN(alm_tm->tm_mday); BCD_TO_BIN(alm_tm->tm_mon); BCD_TO_BIN(alm_tm->tm_year); /* * Account for differences between how the RTC uses the values * and how they are defined in a struct rtc_time; */ if ((alm_tm->tm_year += (epoch - 1900)) <= 69) alm_tm->tm_year += 100; alm_tm->tm_mon--; return 0;out_err: spin_unlock_irq(&rtc_lock); return -1;}MODULE_AUTHOR("Texas Instruments, Inc.");MODULE_LICENSE("GPL");module_init(rtc_init);module_exit(rtc_exit);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -