driver/sensors: add open/close api and struct file for sensor_ops

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Jiuzhu Dong 2022-04-13 14:00:08 +08:00 committed by Xiang Xiao
parent 3fdd036ba6
commit bb07354198
2 changed files with 103 additions and 17 deletions

View File

@ -196,7 +196,8 @@ static bool sensor_is_updated(unsigned long generation,
return generation > ugeneration; return generation > ugeneration;
} }
static int sensor_update_interval(FAR struct sensor_upperhalf_s *upper, static int sensor_update_interval(FAR struct file *filep,
FAR struct sensor_upperhalf_s *upper,
FAR struct sensor_user_s *user, FAR struct sensor_user_s *user,
unsigned long interval) unsigned long interval)
{ {
@ -237,7 +238,7 @@ update:
if (min_interval != ULONG_MAX && lower->ops->set_interval) if (min_interval != ULONG_MAX && lower->ops->set_interval)
{ {
ret = lower->ops->set_interval(lower, &min_interval); ret = lower->ops->set_interval(lower, filep, &min_interval);
if (ret < 0) if (ret < 0)
{ {
return ret; return ret;
@ -250,7 +251,8 @@ update:
return ret; return ret;
} }
static int sensor_update_latency(FAR struct sensor_upperhalf_s *upper, static int sensor_update_latency(FAR struct file *filep,
FAR struct sensor_upperhalf_s *upper,
FAR struct sensor_user_s *user, FAR struct sensor_user_s *user,
unsigned long latency) unsigned long latency)
{ {
@ -296,7 +298,7 @@ update:
if (min_latency != ULONG_MAX && lower->ops->batch) if (min_latency != ULONG_MAX && lower->ops->batch)
{ {
ret = lower->ops->batch(lower, &min_latency); ret = lower->ops->batch(lower, filep, &min_latency);
if (ret < 0) if (ret < 0)
{ {
return ret; return ret;
@ -368,14 +370,23 @@ static int sensor_open(FAR struct file *filep)
goto errout_with_sem; goto errout_with_sem;
} }
if (lower->ops->open)
{
ret = lower->ops->open(lower, filep);
if (ret < 0)
{
goto errout_with_user;
}
}
if (filep->f_oflags & O_RDOK) if (filep->f_oflags & O_RDOK)
{ {
if (upper->state.nsubscribers == 0 && lower->ops->activate) if (upper->state.nsubscribers == 0 && lower->ops->activate)
{ {
ret = lower->ops->activate(lower, true); ret = lower->ops->activate(lower, filep, true);
if (ret < 0) if (ret < 0)
{ {
goto errout_with_user; goto errout_with_open;
} }
} }
@ -401,6 +412,12 @@ static int sensor_open(FAR struct file *filep)
filep->f_priv = user; filep->f_priv = user;
goto errout_with_sem; goto errout_with_sem;
errout_with_open:
if (lower->ops->close)
{
lower->ops->close(lower, filep);
}
errout_with_user: errout_with_user:
kmm_free(user); kmm_free(user);
errout_with_sem: errout_with_sem:
@ -422,12 +439,22 @@ static int sensor_close(FAR struct file *filep)
return ret; return ret;
} }
if (lower->ops->close)
{
ret = lower->ops->close(lower, filep);
if (ret < 0)
{
nxsem_post(&upper->exclsem);
return ret;
}
}
if (filep->f_oflags & O_RDOK) if (filep->f_oflags & O_RDOK)
{ {
upper->state.nsubscribers--; upper->state.nsubscribers--;
if (upper->state.nsubscribers == 0 && lower->ops->activate) if (upper->state.nsubscribers == 0 && lower->ops->activate)
{ {
lower->ops->activate(lower, false); lower->ops->activate(lower, filep, false);
} }
} }
@ -437,8 +464,8 @@ static int sensor_close(FAR struct file *filep)
} }
list_delete(&user->node); list_delete(&user->node);
sensor_update_latency(upper, user, ULONG_MAX); sensor_update_latency(filep, upper, user, ULONG_MAX);
sensor_update_interval(upper, user, ULONG_MAX); sensor_update_interval(filep, upper, user, ULONG_MAX);
nxsem_destroy(&user->buffersem); nxsem_destroy(&user->buffersem);
/* The user is closed, notify to other users */ /* The user is closed, notify to other users */
@ -494,7 +521,7 @@ static ssize_t sensor_read(FAR struct file *filep, FAR char *buffer,
goto out; goto out;
} }
ret = lower->ops->fetch(lower, buffer, len); ret = lower->ops->fetch(lower, filep, buffer, len);
} }
else else
{ {
@ -604,13 +631,13 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
case SNIOC_SET_INTERVAL: case SNIOC_SET_INTERVAL:
{ {
ret = sensor_update_interval(upper, user, arg); ret = sensor_update_interval(filep, upper, user, arg);
} }
break; break;
case SNIOC_BATCH: case SNIOC_BATCH:
{ {
ret = sensor_update_latency(upper, user, arg); ret = sensor_update_latency(filep, upper, user, arg);
} }
break; break;
@ -622,7 +649,7 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
break; break;
} }
ret = lower->ops->selftest(lower, arg); ret = lower->ops->selftest(lower, filep, arg);
} }
break; break;
@ -634,7 +661,7 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
break; break;
} }
ret = lower->ops->set_calibvalue(lower, arg); ret = lower->ops->set_calibvalue(lower, filep, arg);
} }
break; break;
@ -646,7 +673,7 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
break; break;
} }
ret = lower->ops->calibrate(lower, arg); ret = lower->ops->calibrate(lower, filep, arg);
} }
break; break;
@ -682,7 +709,7 @@ static int sensor_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
if (lower->ops->control) if (lower->ops->control)
{ {
ret = lower->ops->control(lower, cmd, arg); ret = lower->ops->control(lower, filep, cmd, arg);
} }
else else
{ {

View File

@ -32,6 +32,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
#include <nuttx/fs/fs.h>
#include <nuttx/sensors/ioctl.h> #include <nuttx/sensors/ioctl.h>
/**************************************************************************** /****************************************************************************
@ -584,6 +585,48 @@ struct sensor_cap /* Type: Capacitance */
struct sensor_lowerhalf_s; struct sensor_lowerhalf_s;
struct sensor_ops_s struct sensor_ops_s
{ {
/**************************************************************************
* Name: open
*
* Description:
* The open method differs from the activate method with true because
* it's called and turned off every times, and it receives the pointer
* of file and the instance of lowerhalf sensor driver. It uses to do
* something about initialize for every user.
*
* Input Parameters:
* lower - The instance of lower half sensor driver
* filep - The pointer of file, represents each user using the sensor
*
* Returned Value:
* Zero (OK) or positive on success; a negated errno value on failure.
*
**************************************************************************/
CODE int (*open)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep);
/**************************************************************************
* Name: close
*
* Description:
* The close method differs from the activate method with false because
* it's called and turned off every times, and it receives the pointer
* of file and the instance of lowerhalf sensor driver. It uses to do
* something about uninitialize for every user.
*
* Input Parameters:
* lower - The instance of lower half sensor driver.
* filep - The pointer of file, represents each user using the sensor.
*
* Returned Value:
* Zero (OK) or positive on success; a negated errno value on failure.
*
**************************************************************************/
CODE int (*close)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep);
/************************************************************************** /**************************************************************************
* Name: activate * Name: activate
* *
@ -594,6 +637,7 @@ struct sensor_ops_s
* *
* Input Parameters: * Input Parameters:
* lower - The instance of lower half sensor driver * lower - The instance of lower half sensor driver
* filep - The pointer of file, represents each user using the sensor.
* enable - true(enable) and false(disable) * enable - true(enable) and false(disable)
* *
* Returned Value: * Returned Value:
@ -601,7 +645,8 @@ struct sensor_ops_s
* *
**************************************************************************/ **************************************************************************/
CODE int (*activate)(FAR struct sensor_lowerhalf_s *lower, bool enable); CODE int (*activate)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep, bool enable);
/************************************************************************** /**************************************************************************
* Name: set_interval * Name: set_interval
@ -619,6 +664,7 @@ struct sensor_ops_s
* *
* Input Parameters: * Input Parameters:
* lower - The instance of lower half sensor driver. * lower - The instance of lower half sensor driver.
* filep - The pointer of file, represents each user using sensor.
* period_us - the time between samples, in us, it may be overwrite by * period_us - the time between samples, in us, it may be overwrite by
* lower half driver. * lower half driver.
* *
@ -628,6 +674,7 @@ struct sensor_ops_s
**************************************************************************/ **************************************************************************/
CODE int (*set_interval)(FAR struct sensor_lowerhalf_s *lower, CODE int (*set_interval)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
FAR unsigned long *period_us); FAR unsigned long *period_us);
/************************************************************************** /**************************************************************************
@ -663,6 +710,7 @@ struct sensor_ops_s
* *
* Input Parameters: * Input Parameters:
* lower - The instance of lower half sensor driver. * lower - The instance of lower half sensor driver.
* filep - The pointer of file, represents each user using sensor.
* latency_us - the time between batch data, in us. It may by overwrite * latency_us - the time between batch data, in us. It may by overwrite
* by lower half driver. * by lower half driver.
* *
@ -672,6 +720,7 @@ struct sensor_ops_s
**************************************************************************/ **************************************************************************/
CODE int (*batch)(FAR struct sensor_lowerhalf_s *lower, CODE int (*batch)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
FAR unsigned long *latency_us); FAR unsigned long *latency_us);
/************************************************************************** /**************************************************************************
@ -693,6 +742,7 @@ struct sensor_ops_s
* *
* Input Parameters: * Input Parameters:
* lower - The instance of lower half sensor driver. * lower - The instance of lower half sensor driver.
* filep - The pointer of file, represents each user using sensor.
* buffer - The buffer of receive sensor event, it's provided by * buffer - The buffer of receive sensor event, it's provided by
* file_operation::sensor_read. * file_operation::sensor_read.
* buflen - The size of buffer. * buflen - The size of buffer.
@ -704,6 +754,7 @@ struct sensor_ops_s
**************************************************************************/ **************************************************************************/
CODE int (*fetch)(FAR struct sensor_lowerhalf_s *lower, CODE int (*fetch)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
FAR char *buffer, size_t buflen); FAR char *buffer, size_t buflen);
/************************************************************************** /**************************************************************************
@ -718,6 +769,7 @@ struct sensor_ops_s
* *
* Input Parameters: * Input Parameters:
* lower - The instance of lower half sensor driver. * lower - The instance of lower half sensor driver.
* filep - The pointer of file, represents each user using sensor.
* arg - The parameters associated with selftest. * arg - The parameters associated with selftest.
* *
* Returned Value: * Returned Value:
@ -726,6 +778,7 @@ struct sensor_ops_s
**************************************************************************/ **************************************************************************/
CODE int (*selftest)(FAR struct sensor_lowerhalf_s *lower, CODE int (*selftest)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
unsigned long arg); unsigned long arg);
/************************************************************************** /**************************************************************************
@ -738,6 +791,7 @@ struct sensor_ops_s
* *
* Input Parameters: * Input Parameters:
* lower - The instance of lower half sensor driver. * lower - The instance of lower half sensor driver.
* filep - The pointer of file, represents each user using sensor.
* arg - The parameters associated with calibration value. * arg - The parameters associated with calibration value.
* *
* Returned Value: * Returned Value:
@ -746,6 +800,7 @@ struct sensor_ops_s
**************************************************************************/ **************************************************************************/
CODE int (*set_calibvalue)(FAR struct sensor_lowerhalf_s *lower, CODE int (*set_calibvalue)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
unsigned long arg); unsigned long arg);
/**************************************************************************** /****************************************************************************
@ -762,6 +817,7 @@ struct sensor_ops_s
* *
* Input Parameters: * Input Parameters:
* lower - The instance of lower half sensor driver. * lower - The instance of lower half sensor driver.
* filep - The pointer of file, represents each user using sensor.
* arg - The parameters associated with calibration value. * arg - The parameters associated with calibration value.
* *
* Returned Value: * Returned Value:
@ -770,6 +826,7 @@ struct sensor_ops_s
**************************************************************************/ **************************************************************************/
CODE int (*calibrate)(FAR struct sensor_lowerhalf_s *lower, CODE int (*calibrate)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
unsigned long arg); unsigned long arg);
/************************************************************************** /**************************************************************************
@ -781,6 +838,7 @@ struct sensor_ops_s
* *
* Input Parameters: * Input Parameters:
* lower - The instance of lower half sensor driver. * lower - The instance of lower half sensor driver.
* filep - The pointer of file, represents each user using sensor.
* cmd - The special cmd for sensor. * cmd - The special cmd for sensor.
* arg - The parameters associated with cmd. * arg - The parameters associated with cmd.
* *
@ -791,6 +849,7 @@ struct sensor_ops_s
**************************************************************************/ **************************************************************************/
CODE int (*control)(FAR struct sensor_lowerhalf_s *lower, CODE int (*control)(FAR struct sensor_lowerhalf_s *lower,
FAR struct file *filep,
int cmd, unsigned long arg); int cmd, unsigned long arg);
}; };