2015-02-13 18:49:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/timers/rtc.c
|
|
|
|
*
|
2017-10-07 18:57:09 +02:00
|
|
|
* Copyright (C) 2015, 2017 Gregory Nutt. All rights reserved.
|
2015-02-13 18:49:04 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2019-01-27 17:08:07 +01:00
|
|
|
#include <semaphore.h>
|
2015-02-13 18:49:04 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
2017-10-07 18:57:09 +02:00
|
|
|
#include <nuttx/signal.h>
|
2015-02-13 18:49:04 +01:00
|
|
|
#include <nuttx/fs/fs.h>
|
2015-04-01 20:37:44 +02:00
|
|
|
#include <nuttx/timers/rtc.h>
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-10-20 19:15:17 +02:00
|
|
|
#if defined(CONFIG_RTC_ALARM) || defined(CONFIG_RTC_PERIODIC)
|
2016-04-02 21:01:02 +02:00
|
|
|
struct rtc_alarminfo_s
|
|
|
|
{
|
|
|
|
bool active; /* True: alarm is active */
|
|
|
|
pid_t pid; /* Identifies task to be notified */
|
2019-01-27 15:53:12 +01:00
|
|
|
struct sigevent event; /* Describe the way a task is to be notified */
|
2019-01-27 16:28:59 +01:00
|
|
|
struct sigwork_s work; /* Signal work */
|
2016-04-02 21:01:02 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 18:49:04 +01:00
|
|
|
struct rtc_upperhalf_s
|
|
|
|
{
|
|
|
|
FAR struct rtc_lowerhalf_s *lower; /* Contained lower half driver */
|
2019-01-27 17:08:07 +01:00
|
|
|
sem_t exclsem; /* Supports mutual exclusion */
|
2016-04-02 21:01:02 +02:00
|
|
|
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
uint8_t crefs; /* Number of open references */
|
|
|
|
bool unlinked; /* True if the driver has been unlinked */
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2016-04-02 21:01:02 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_RTC_ALARM
|
|
|
|
/* This is an array, indexed by the alarm ID, that provides information
|
|
|
|
* needed to map an alarm expiration to a signal event.
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct rtc_alarminfo_s alarminfo[CONFIG_RTC_NALARMS];
|
|
|
|
#endif
|
2017-10-20 19:15:17 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_RTC_PERIODIC
|
|
|
|
/* Currently only one periodic wakeup is supported. */
|
|
|
|
|
|
|
|
struct rtc_alarminfo_s periodicinfo;
|
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* Internal logic */
|
|
|
|
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
static void rtc_destroy(FAR struct rtc_upperhalf_s *upper);
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
#ifdef CONFIG_RTC_ALARM
|
|
|
|
static void rtc_alarm_callback(FAR void *priv, int id);
|
|
|
|
#endif
|
|
|
|
|
2017-10-20 19:15:17 +02:00
|
|
|
#ifdef CONFIG_RTC_PERIODIC
|
|
|
|
static void rtc_periodic_callback(FAR void *priv, int id);
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 18:49:04 +01:00
|
|
|
/* Character driver methods */
|
|
|
|
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
static int rtc_open(FAR struct file *filep);
|
|
|
|
static int rtc_close(FAR struct file *filep);
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
|
|
|
|
2015-10-10 18:41:00 +02:00
|
|
|
static ssize_t rtc_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
2015-02-13 18:49:04 +01:00
|
|
|
static ssize_t rtc_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static int rtc_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
|
2015-02-18 16:34:58 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
static int rtc_unlink(FAR struct inode *inode);
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations rtc_fops =
|
|
|
|
{
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
rtc_open, /* open */
|
|
|
|
rtc_close, /* close */
|
2015-02-18 16:34:58 +01:00
|
|
|
#else
|
|
|
|
0, /* open */
|
|
|
|
0, /* close */
|
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
rtc_read, /* read */
|
|
|
|
rtc_write, /* write */
|
|
|
|
0, /* seek */
|
|
|
|
rtc_ioctl, /* ioctl */
|
|
|
|
#ifndef CONFIG_DISABLE_POLL
|
|
|
|
0, /* poll */
|
|
|
|
#endif
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
rtc_unlink /* unlink */
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2016-04-02 21:01:02 +02:00
|
|
|
* Name: rtc_destroy
|
2015-02-13 18:49:04 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
static void rtc_destroy(FAR struct rtc_upperhalf_s *upper)
|
|
|
|
{
|
|
|
|
/* If the lower half driver provided a destroy method, then call that
|
2018-12-22 15:12:34 +01:00
|
|
|
* method now in order to clean up resources used by the lower-half driver.
|
2015-02-13 18:49:04 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(upper->lower && upper->lower->ops);
|
|
|
|
if (upper->lower->ops->destroy)
|
|
|
|
{
|
|
|
|
upper->lower->ops->destroy(upper->lower);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* And free our container */
|
|
|
|
|
2019-01-27 17:08:07 +01:00
|
|
|
nxsem_destroy(&upper->exclsem);
|
2015-02-13 18:49:04 +01:00
|
|
|
kmm_free(upper);
|
|
|
|
}
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: rtc_alarm_callback
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_RTC_ALARM
|
|
|
|
static void rtc_alarm_callback(FAR void *priv, int alarmid)
|
|
|
|
{
|
|
|
|
FAR struct rtc_upperhalf_s *upper = (FAR struct rtc_upperhalf_s *)priv;
|
|
|
|
FAR struct rtc_alarminfo_s *alarminfo;
|
|
|
|
|
2016-04-03 17:52:25 +02:00
|
|
|
DEBUGASSERT(upper != NULL && alarmid >= 0 && alarmid < CONFIG_RTC_NALARMS);
|
2016-04-02 21:01:02 +02:00
|
|
|
alarminfo = &upper->alarminfo[alarmid];
|
|
|
|
|
2017-10-17 18:45:48 +02:00
|
|
|
/* Do we think that the alarm is active? It might be due to some
|
2016-04-02 21:01:02 +02:00
|
|
|
* race condition between a cancellation event and the alarm
|
|
|
|
* expiration.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (alarminfo->active)
|
|
|
|
{
|
2017-10-17 18:45:48 +02:00
|
|
|
/* Yes.. signal the alarm expiration */
|
2016-04-02 21:01:02 +02:00
|
|
|
|
2019-01-27 16:28:59 +01:00
|
|
|
nxsig_notification(alarminfo->pid, &alarminfo->event,
|
|
|
|
SI_QUEUE, &alarminfo->work);
|
2016-04-02 21:01:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The alarm is no longer active */
|
|
|
|
|
|
|
|
alarminfo->active = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-20 19:15:17 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: rtc_periodic_callback
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_RTC_PERIODIC
|
|
|
|
static void rtc_periodic_callback(FAR void *priv, int alarmid)
|
|
|
|
{
|
|
|
|
FAR struct rtc_upperhalf_s *upper = (FAR struct rtc_upperhalf_s *)priv;
|
|
|
|
FAR struct rtc_alarminfo_s *alarminfo;
|
|
|
|
|
|
|
|
DEBUGASSERT(upper != NULL && alarmid >= 0);
|
|
|
|
alarminfo = &upper->periodicinfo;
|
|
|
|
|
|
|
|
/* Do we think that the alarm is active? It might be due to some
|
|
|
|
* race condition between a cancellation event and the alarm
|
|
|
|
* expiration.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (alarminfo->active)
|
|
|
|
{
|
|
|
|
/* Yes.. signal the alarm expiration */
|
|
|
|
|
2019-01-27 16:28:59 +01:00
|
|
|
nxsig_notification(alarminfo->pid, &alarminfo->event,
|
|
|
|
SI_QUEUE, &alarminfo->work);
|
2017-10-20 19:15:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The alarm is no longer active */
|
|
|
|
|
|
|
|
alarminfo->active = false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-13 18:49:04 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: rtc_open
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
static int rtc_open(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct rtc_upperhalf_s *upper;
|
2019-01-27 17:08:07 +01:00
|
|
|
int ret;
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/* Get the reference to our internal state structure from the inode
|
|
|
|
* structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
upper = inode->i_private;
|
|
|
|
|
2019-01-27 17:08:07 +01:00
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&upper->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-13 18:49:04 +01:00
|
|
|
/* Increment the count of open references on the RTC driver */
|
|
|
|
|
|
|
|
upper->crefs++;
|
|
|
|
DEBUGASSERT(upper->crefs > 0);
|
2019-01-27 17:08:07 +01:00
|
|
|
nxsem_post(&upper->exclsem);
|
2015-02-13 18:49:04 +01:00
|
|
|
return OK;
|
|
|
|
}
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: rtc_close
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
static int rtc_close(FAR struct file *filep)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct rtc_upperhalf_s *upper;
|
2019-01-27 17:08:07 +01:00
|
|
|
int ret;
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/* Get the reference to our internal state structure from the inode
|
|
|
|
* structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
upper = inode->i_private;
|
|
|
|
|
2019-01-27 17:08:07 +01:00
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&upper->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-13 18:49:04 +01:00
|
|
|
/* Decrement the count of open references on the RTC driver */
|
|
|
|
|
|
|
|
DEBUGASSERT(upper->crefs > 0);
|
|
|
|
upper->crefs--;
|
2019-01-27 17:08:07 +01:00
|
|
|
nxsem_post(&upper->exclsem);
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/* If the count has decremented to zero and the driver has been unlinked,
|
|
|
|
* then commit Hara-Kiri now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (upper->crefs == 0 && upper->unlinked)
|
|
|
|
{
|
|
|
|
rtc_destroy(upper);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: rtc_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t rtc_read(FAR struct file *filep, FAR char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
return 0; /* Return EOF */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: rtc_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-01-27 15:53:12 +01:00
|
|
|
static ssize_t rtc_write(FAR struct file *filep, FAR const char *buffer,
|
|
|
|
size_t len)
|
2015-02-13 18:49:04 +01:00
|
|
|
{
|
|
|
|
return len; /* Say that everything was written */
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: rtc_ioctl
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int rtc_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
|
|
|
|
{
|
|
|
|
FAR struct inode *inode;
|
|
|
|
FAR struct rtc_upperhalf_s *upper;
|
|
|
|
FAR const struct rtc_ops_s *ops;
|
|
|
|
int ret = -ENOSYS;
|
|
|
|
|
|
|
|
/* Get the reference to our internal state structure from the inode
|
|
|
|
* structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(filep);
|
|
|
|
inode = filep->f_inode;
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
upper = inode->i_private;
|
|
|
|
DEBUGASSERT(upper->lower && upper->lower->ops);
|
|
|
|
|
2019-01-27 17:08:07 +01:00
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&upper->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-13 18:49:04 +01:00
|
|
|
/* We simply forward all ioctl() commands to the lower half. The upper
|
|
|
|
* half is nothing more than a thin driver shell over the lower level
|
|
|
|
* RTC implementation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ops = upper->lower->ops;
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
/* RTC_RD_TIME returns the current RTC time.
|
|
|
|
*
|
|
|
|
* Argument: A writeable reference to a struct rtc_time to receive the
|
|
|
|
* RTC's time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case RTC_RD_TIME:
|
|
|
|
{
|
2019-01-27 15:53:12 +01:00
|
|
|
FAR struct rtc_time *rtctime =
|
|
|
|
(FAR struct rtc_time *)((uintptr_t)arg);
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
if (ops->rdtime)
|
|
|
|
{
|
|
|
|
ret = ops->rdtime(upper->lower, rtctime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* RTC_SET_TIME sets the RTC's time
|
|
|
|
*
|
2017-04-06 17:53:11 +02:00
|
|
|
* Argument: A read-only reference to a struct rtc_time containing
|
2015-02-13 18:49:04 +01:00
|
|
|
* the new time to be set.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case RTC_SET_TIME:
|
|
|
|
{
|
|
|
|
FAR const struct rtc_time *rtctime =
|
|
|
|
(FAR const struct rtc_time *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
if (ops->settime)
|
|
|
|
{
|
|
|
|
ret = ops->settime(upper->lower, rtctime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-04-06 17:53:11 +02:00
|
|
|
/* RTC_HAVE_SET_TIME checks if RTC's time had been set
|
|
|
|
*
|
|
|
|
* Argument: A writable reference to a bool to receive true/false return
|
|
|
|
* value of the check.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case RTC_HAVE_SET_TIME:
|
|
|
|
{
|
|
|
|
FAR bool *have_set_time = (FAR bool *)((uintptr_t)arg);
|
|
|
|
|
|
|
|
if (ops->havesettime)
|
|
|
|
{
|
|
|
|
*have_set_time = ops->havesettime(upper->lower);
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-02-15 15:19:23 +01:00
|
|
|
#ifdef CONFIG_RTC_ALARM
|
2016-04-02 21:01:02 +02:00
|
|
|
/* RTC_SET_ALARM sets the alarm time.
|
2015-02-13 18:49:04 +01:00
|
|
|
*
|
2016-04-02 21:01:02 +02:00
|
|
|
* Argument: A read-only reference to a struct rtc_time containing the
|
|
|
|
* new alarm time to be set.
|
2015-02-13 18:49:04 +01:00
|
|
|
*/
|
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
case RTC_SET_ALARM:
|
2015-02-13 18:49:04 +01:00
|
|
|
{
|
2016-04-02 21:01:02 +02:00
|
|
|
FAR const struct rtc_setalarm_s *alarminfo =
|
|
|
|
(FAR const struct rtc_setalarm_s *)((uintptr_t)arg);
|
|
|
|
FAR struct rtc_alarminfo_s *upperinfo;
|
|
|
|
struct lower_setalarm_s lowerinfo;
|
|
|
|
int alarmid;
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
DEBUGASSERT(alarminfo != NULL);
|
|
|
|
alarmid = alarminfo->id;
|
2016-04-03 17:52:25 +02:00
|
|
|
DEBUGASSERT(alarmid >= 0 && alarmid < CONFIG_RTC_NALARMS);
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
/* Is the alarm active? */
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
upperinfo = &upper->alarminfo[alarmid];
|
|
|
|
if (upperinfo->active)
|
2015-02-13 18:49:04 +01:00
|
|
|
{
|
2016-04-02 21:01:02 +02:00
|
|
|
/* Yes, cancel the alarm */
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
if (ops->cancelalarm)
|
|
|
|
{
|
2016-04-02 21:55:58 +02:00
|
|
|
(void)ops->cancelalarm(upper->lower, alarmid);
|
2016-04-02 21:01:02 +02:00
|
|
|
}
|
2016-04-02 21:55:58 +02:00
|
|
|
|
|
|
|
upperinfo->active = false;
|
2015-02-13 18:49:04 +01:00
|
|
|
}
|
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
if (ops->setalarm)
|
2015-02-13 18:49:04 +01:00
|
|
|
{
|
2016-04-09 01:44:24 +02:00
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
/* A PID of zero means to signal the calling task */
|
|
|
|
|
|
|
|
pid = alarminfo->pid;
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
pid = getpid();
|
|
|
|
}
|
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
/* Save the signal info to be used to notify the caller when the
|
|
|
|
* alarm expires.
|
|
|
|
*/
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:55:58 +02:00
|
|
|
upperinfo->active = true;
|
2016-04-09 01:44:24 +02:00
|
|
|
upperinfo->pid = pid;
|
2019-01-27 15:53:12 +01:00
|
|
|
upperinfo->event = alarminfo->event;
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
/* Format the alarm info needed by the lower half driver */
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
lowerinfo.id = alarmid;
|
|
|
|
lowerinfo.cb = rtc_alarm_callback;
|
|
|
|
lowerinfo.priv = (FAR void *)upper;
|
|
|
|
lowerinfo.time = alarminfo->time;
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
/* Then set the alarm */
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
ret = ops->setalarm(upper->lower, &lowerinfo);
|
2016-04-02 21:55:58 +02:00
|
|
|
if (ret < 0)
|
2016-04-02 21:01:02 +02:00
|
|
|
{
|
2016-04-02 21:55:58 +02:00
|
|
|
upperinfo->active = false;
|
2016-04-02 21:01:02 +02:00
|
|
|
}
|
2015-02-13 18:49:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-04-03 02:18:48 +02:00
|
|
|
/* RTC_SET_RELATIVE sets the alarm time relative to the current time.
|
|
|
|
*
|
2019-01-27 15:53:12 +01:00
|
|
|
* Argument: A read-only reference to a struct rtc_setrelative_s
|
|
|
|
* containing the new relative alarm time to be set.
|
2016-04-03 02:18:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case RTC_SET_RELATIVE:
|
|
|
|
{
|
|
|
|
FAR const struct rtc_setrelative_s *alarminfo =
|
|
|
|
(FAR const struct rtc_setrelative_s *)((uintptr_t)arg);
|
|
|
|
FAR struct rtc_alarminfo_s *upperinfo;
|
|
|
|
struct lower_setrelative_s lowerinfo;
|
|
|
|
int alarmid;
|
|
|
|
|
|
|
|
DEBUGASSERT(alarminfo != NULL);
|
|
|
|
alarmid = alarminfo->id;
|
2016-04-03 17:52:25 +02:00
|
|
|
DEBUGASSERT(alarmid >= 0 && alarmid < CONFIG_RTC_NALARMS);
|
2016-04-03 02:18:48 +02:00
|
|
|
|
|
|
|
/* Is the alarm active? */
|
|
|
|
|
|
|
|
upperinfo = &upper->alarminfo[alarmid];
|
|
|
|
if (upperinfo->active)
|
|
|
|
{
|
|
|
|
/* Yes, cancel the alarm */
|
|
|
|
|
|
|
|
if (ops->cancelalarm)
|
|
|
|
{
|
|
|
|
(void)ops->cancelalarm(upper->lower, alarmid);
|
|
|
|
}
|
|
|
|
|
|
|
|
upperinfo->active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ops->setrelative)
|
|
|
|
{
|
2016-04-09 01:44:24 +02:00
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
/* A PID of zero means to signal the calling task */
|
|
|
|
|
|
|
|
pid = alarminfo->pid;
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
pid = getpid();
|
|
|
|
}
|
|
|
|
|
2016-04-03 02:18:48 +02:00
|
|
|
/* Save the signal info to be used to notify the caller when the
|
|
|
|
* alarm expires.
|
|
|
|
*/
|
|
|
|
|
|
|
|
upperinfo->active = true;
|
2016-04-09 01:44:24 +02:00
|
|
|
upperinfo->pid = pid;
|
2019-01-27 15:53:12 +01:00
|
|
|
upperinfo->event = alarminfo->event;
|
2016-04-03 02:18:48 +02:00
|
|
|
|
|
|
|
/* Format the alarm info needed by the lower half driver */
|
|
|
|
|
|
|
|
lowerinfo.id = alarmid;
|
|
|
|
lowerinfo.cb = rtc_alarm_callback;
|
|
|
|
lowerinfo.priv = (FAR void *)upper;
|
|
|
|
lowerinfo.reltime = alarminfo->reltime;
|
|
|
|
|
|
|
|
/* Then set the alarm */
|
|
|
|
|
|
|
|
ret = ops->setrelative(upper->lower, &lowerinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
upperinfo->active = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-10-17 18:45:48 +02:00
|
|
|
/* RTC_CANCEL_ALARM cancel the alarm.
|
2015-02-13 18:49:04 +01:00
|
|
|
*
|
2016-04-02 21:01:02 +02:00
|
|
|
* Argument: An ALARM ID value that indicates which alarm should be
|
|
|
|
* canceled.
|
2015-02-13 18:49:04 +01:00
|
|
|
*/
|
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
case RTC_CANCEL_ALARM:
|
2015-02-13 18:49:04 +01:00
|
|
|
{
|
2017-10-17 18:45:48 +02:00
|
|
|
FAR struct rtc_alarminfo_s *upperinfo;
|
2016-04-02 21:01:02 +02:00
|
|
|
int alarmid = (int)arg;
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2016-04-03 17:52:25 +02:00
|
|
|
DEBUGASSERT(alarmid >= 0 && alarmid < CONFIG_RTC_NALARMS);
|
2017-10-17 18:45:48 +02:00
|
|
|
|
|
|
|
upperinfo = &upper->alarminfo[alarmid];
|
|
|
|
|
2016-04-02 21:01:02 +02:00
|
|
|
if (ops->cancelalarm)
|
2015-02-13 18:49:04 +01:00
|
|
|
{
|
2016-04-02 21:01:02 +02:00
|
|
|
ret = ops->cancelalarm(upper->lower, alarmid);
|
2017-10-17 18:45:48 +02:00
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
upperinfo->active = false;
|
2019-01-27 16:28:59 +01:00
|
|
|
nxsig_cancel_notification(&upperinfo->work);
|
2017-10-17 18:45:48 +02:00
|
|
|
}
|
2015-02-13 18:49:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-09-03 14:55:38 +02:00
|
|
|
|
|
|
|
/* RTC_RD_ALARM query the alarm.
|
|
|
|
*
|
|
|
|
* Argument: A writable reference to the queried alarm.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
case RTC_RD_ALARM:
|
|
|
|
{
|
|
|
|
FAR struct rtc_rdalarm_s *alarmquery =
|
|
|
|
(FAR struct rtc_rdalarm_s *)((uintptr_t)arg);
|
|
|
|
FAR struct rtc_alarminfo_s *upperinfo;
|
|
|
|
struct lower_rdalarm_s lowerinfo;
|
|
|
|
int alarmid;
|
|
|
|
|
|
|
|
DEBUGASSERT(alarmquery != NULL);
|
|
|
|
alarmid = alarmquery->id;
|
|
|
|
DEBUGASSERT(alarmid >= 0 && alarmid < CONFIG_RTC_NALARMS);
|
|
|
|
|
|
|
|
/* Is the alarm active? */
|
|
|
|
|
|
|
|
upperinfo = &upper->alarminfo[alarmid];
|
|
|
|
alarmquery->active = upperinfo->active;
|
|
|
|
|
|
|
|
lowerinfo.id = alarmid;
|
|
|
|
lowerinfo.priv = (FAR void *)upper;
|
|
|
|
lowerinfo.time = (FAR struct rtc_time *)&alarmquery->time;
|
|
|
|
|
|
|
|
if (ops->rdalarm)
|
|
|
|
{
|
|
|
|
ret = ops->rdalarm(upper->lower, &lowerinfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2015-02-15 15:19:23 +01:00
|
|
|
#endif /* CONFIG_RTC_ALARM */
|
2015-02-13 18:49:04 +01:00
|
|
|
|
2017-10-20 19:15:17 +02:00
|
|
|
#ifdef CONFIG_RTC_PERIODIC
|
|
|
|
/* RTC_SET_PERIODIC set a periodic wakeup.
|
|
|
|
*
|
2019-01-27 15:53:12 +01:00
|
|
|
* Argument: A read-only reference to a struct rtc_setperiodic_s
|
|
|
|
* containing the new wakeup period to be set.
|
2017-10-20 19:15:17 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
case RTC_SET_PERIODIC:
|
|
|
|
{
|
|
|
|
FAR const struct rtc_setperiodic_s *alarminfo =
|
|
|
|
(FAR const struct rtc_setperiodic_s *)((uintptr_t)arg);
|
|
|
|
FAR struct rtc_alarminfo_s *upperinfo;
|
|
|
|
struct lower_setperiodic_s lowerinfo;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
DEBUGASSERT(alarminfo != NULL);
|
|
|
|
id = alarminfo->id;
|
|
|
|
DEBUGASSERT(id >= 0);
|
|
|
|
|
|
|
|
/* Is the alarm active? */
|
|
|
|
|
|
|
|
upperinfo = &upper->periodicinfo;
|
|
|
|
if (upperinfo->active)
|
|
|
|
{
|
|
|
|
/* Yes, cancel it */
|
|
|
|
|
|
|
|
if (ops->cancelperiodic)
|
|
|
|
{
|
|
|
|
(void)ops->cancelperiodic(upper->lower, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
upperinfo->active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ops->setperiodic)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
/* A PID of zero means to signal the calling task */
|
|
|
|
|
|
|
|
pid = alarminfo->pid;
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
pid = getpid();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the signal info to be used to notify the caller when the
|
|
|
|
* alarm expires.
|
|
|
|
*/
|
|
|
|
|
|
|
|
upperinfo->active = true;
|
|
|
|
upperinfo->pid = pid;
|
2019-01-27 15:53:12 +01:00
|
|
|
upperinfo->event = alarminfo->event;
|
2017-10-20 19:15:17 +02:00
|
|
|
|
|
|
|
/* Format the alarm info needed by the lower half driver. */
|
|
|
|
|
|
|
|
lowerinfo.id = id;
|
|
|
|
lowerinfo.cb = rtc_periodic_callback;
|
|
|
|
lowerinfo.priv = (FAR void *)upper;
|
|
|
|
lowerinfo.period = alarminfo->period;
|
|
|
|
|
|
|
|
/* Then set the periodic wakeup. */
|
|
|
|
|
|
|
|
ret = ops->setperiodic(upper->lower, &lowerinfo);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
upperinfo->active = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* RTC_CANCEL_PERIODIC cancel the periodic wakeup.
|
|
|
|
*
|
|
|
|
* Argument: An ID value that indicates which wakeup should be canceled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
case RTC_CANCEL_PERIODIC:
|
|
|
|
{
|
|
|
|
FAR struct rtc_alarminfo_s *upperinfo;
|
|
|
|
int id = (int)arg;
|
|
|
|
|
|
|
|
DEBUGASSERT(id >= 0);
|
|
|
|
|
|
|
|
upperinfo = &upper->periodicinfo;
|
|
|
|
|
|
|
|
if (ops->cancelperiodic)
|
|
|
|
{
|
|
|
|
ret = ops->cancelperiodic(upper->lower, id);
|
|
|
|
if (ret == OK)
|
|
|
|
{
|
|
|
|
upperinfo->active = false;
|
2019-01-27 16:28:59 +01:00
|
|
|
nxsig_cancel_notification(&upperinfo->work);
|
2017-10-20 19:15:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* CONFIG_RTC_PERIODIC */
|
|
|
|
|
2015-02-18 15:04:25 +01:00
|
|
|
/* Forward any unrecognized IOCTLs to the lower half driver... they
|
|
|
|
* may represent some architecture-specific command.
|
|
|
|
*/
|
|
|
|
|
2015-02-13 18:49:04 +01:00
|
|
|
default:
|
2015-02-18 15:04:25 +01:00
|
|
|
{
|
|
|
|
ret = -ENOTTY;
|
2015-02-18 15:23:10 +01:00
|
|
|
#ifdef CONFIG_RTC_IOCTL
|
2015-02-18 15:04:25 +01:00
|
|
|
if (ops->ioctl)
|
|
|
|
{
|
|
|
|
ret = ops->ioctl(upper->lower, cmd, arg);
|
|
|
|
}
|
2015-02-18 15:23:10 +01:00
|
|
|
#endif
|
2015-02-18 15:04:25 +01:00
|
|
|
}
|
2015-02-13 18:49:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-01-27 17:08:07 +01:00
|
|
|
nxsem_post(&upper->exclsem);
|
2015-02-13 18:49:04 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: rtc_unlink
|
|
|
|
****************************************************************************/
|
|
|
|
|
2015-02-18 16:34:58 +01:00
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
static int rtc_unlink(FAR struct inode *inode)
|
|
|
|
{
|
|
|
|
FAR struct rtc_upperhalf_s *upper;
|
2019-01-27 17:08:07 +01:00
|
|
|
int ret;
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/* Get the reference to our internal state structure from the inode
|
|
|
|
* structure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
DEBUGASSERT(inode && inode->i_private);
|
|
|
|
upper = inode->i_private;
|
|
|
|
|
2019-01-27 17:08:07 +01:00
|
|
|
/* Get exclusive access to the device structures */
|
|
|
|
|
|
|
|
ret = nxsem_wait(&upper->exclsem);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-13 18:49:04 +01:00
|
|
|
/* Indicate that the driver has been unlinked */
|
|
|
|
|
|
|
|
upper->unlinked = true;
|
2019-01-27 17:08:07 +01:00
|
|
|
nxsem_post(&upper->exclsem);
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/* If there are no further open references to the driver, then commit
|
|
|
|
* Hara-Kiri now.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (upper->crefs == 0)
|
|
|
|
{
|
|
|
|
rtc_destroy(upper);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: rtc_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Create an RTC driver by binding to the lower half RTC driver instance
|
|
|
|
* provided to this function. The resulting RTC driver will be registered
|
|
|
|
* at /dev/rtcN where N is the minor number provided to this function.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2015-02-13 18:49:04 +01:00
|
|
|
* minor - The minor number of the RTC device. The N in /dev/rtcN
|
|
|
|
* lower - The lower half driver instance.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; A negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int rtc_initialize(int minor, FAR struct rtc_lowerhalf_s *lower)
|
|
|
|
{
|
|
|
|
FAR struct rtc_upperhalf_s *upper;
|
|
|
|
char devpath[16];
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
DEBUGASSERT(lower && lower->ops && minor >= 0 && minor < 1000);
|
|
|
|
|
|
|
|
/* Allocate an upper half container structure */
|
|
|
|
|
2019-01-27 15:53:12 +01:00
|
|
|
upper = (FAR struct rtc_upperhalf_s *)
|
|
|
|
kmm_zalloc(sizeof(struct rtc_upperhalf_s));
|
|
|
|
|
2015-02-13 18:49:04 +01:00
|
|
|
if (!upper)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the upper half container */
|
|
|
|
|
|
|
|
upper->lower = lower; /* Contain lower half driver */
|
2019-01-27 17:08:07 +01:00
|
|
|
nxsem_init(&upper->exclsem, 0, 1);
|
2015-02-18 16:34:58 +01:00
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
|
2015-02-13 18:49:04 +01:00
|
|
|
upper->crefs = 0; /* No open references */
|
|
|
|
upper->unlinked = false; /* Driver is not unlinked */
|
2015-02-18 16:34:58 +01:00
|
|
|
#endif
|
2015-02-13 18:49:04 +01:00
|
|
|
|
|
|
|
/* Create the driver name. There is space for the a minor number up to 6
|
|
|
|
* characters
|
|
|
|
*/
|
|
|
|
|
|
|
|
snprintf(devpath, 16, "/dev/rtc%d", minor);
|
|
|
|
|
|
|
|
/* And, finally, register the new RTC driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &rtc_fops, 0666, upper);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-01-27 17:08:07 +01:00
|
|
|
nxsem_destroy(&upper->exclsem);
|
2015-02-13 18:49:04 +01:00
|
|
|
kmm_free(upper);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|