From 50608efa1d692a4a9547945d1056921354636eb5 Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Wed, 13 Nov 2024 21:05:19 +0800 Subject: [PATCH 1/3] drivers/sensors: add sensor_device_info when sensor_regsiter by usensor. The logic for obtaining the info and configuring the user buffer have been optimized. Signed-off-by: dongjiuzhu1 Signed-off-by: likun17 --- drivers/sensors/usensor.c | 27 ++++----------------------- include/nuttx/sensors/ioctl.h | 13 ++----------- include/nuttx/uorb.h | 35 ++++++++++++++++++----------------- 3 files changed, 24 insertions(+), 51 deletions(-) diff --git a/drivers/sensors/usensor.c b/drivers/sensors/usensor.c index dab1c7670dfaa..99fa16bdec754 100644 --- a/drivers/sensors/usensor.c +++ b/drivers/sensors/usensor.c @@ -53,9 +53,6 @@ static int usensor_ioctl(FAR struct file *filep, int cmd, static int usensor_get_info(FAR struct sensor_lowerhalf_s *lower, FAR struct file *filep, FAR struct sensor_device_info_s *info); -static int usensor_control(FAR struct sensor_lowerhalf_s *lower, - FAR struct file *filep, - int cmd, unsigned long arg); /**************************************************************************** * Private Types @@ -75,7 +72,7 @@ struct usensor_lowerhalf_s { struct list_node node; /* node in all usensor list */ struct sensor_lowerhalf_s driver; /* The lowerhalf driver of user sensor */ - struct sensor_device_info_s info; /* Virtual sensor information */ + struct sensor_device_info_s devinfo; /* Virtual sensor information */ char path[1]; /* The path of usensor character device */ }; @@ -99,7 +96,6 @@ static const struct file_operations g_usensor_fops = static const struct sensor_ops_s g_usensor_ops = { .get_info = usensor_get_info, - .control = usensor_control }; /**************************************************************************** @@ -124,6 +120,8 @@ static int usensor_register(FAR struct usensor_context_s *usensor, lower->driver.persist = info->persist; lower->driver.ops = &g_usensor_ops; strlcpy(lower->path, info->path, size + 1); + memcpy(&lower->devinfo, &info->devinfo, sizeof(lower->devinfo)); + ret = sensor_custom_register(&lower->driver, lower->path, info->esize); if (ret < 0) { @@ -232,27 +230,10 @@ static int usensor_get_info(FAR struct sensor_lowerhalf_s *lower, struct usensor_lowerhalf_s, driver); - *info = ulower->info; + *info = ulower->devinfo; return 0; } -static int usensor_control(FAR struct sensor_lowerhalf_s *lower, - FAR struct file *filep, - int cmd, unsigned long arg) -{ - FAR struct usensor_lowerhalf_s *ulower = container_of(lower, - struct usensor_lowerhalf_s, - driver); - - if (cmd == SNIOC_SET_INFO) - { - ulower->info = *(FAR struct sensor_device_info_s *)(uintptr_t)arg; - return 0; - } - - return -ENOTTY; -} - /**************************************************************************** * Public Functions ****************************************************************************/ diff --git a/include/nuttx/sensors/ioctl.h b/include/nuttx/sensors/ioctl.h index ff2e6b7d0ab51..9bb198c290277 100644 --- a/include/nuttx/sensors/ioctl.h +++ b/include/nuttx/sensors/ioctl.h @@ -444,27 +444,18 @@ #define SNIOC_GET_INFO _SNIOC(0x009B) -#ifdef CONFIG_USENSOR -/* Command: SNIOC_SET_INFO - * Description: Set device information. Only used by user space. - * Argument: This is the device info pointer. - */ - -# define SNIOC_SET_INFO _SNIOC(0x009C) -#endif - /* Command: SNIOC_FLUSH * Description: Flush sensor hardware fifo buffer. */ -#define SNIOC_FLUSH _SNIOC(0x009D) +#define SNIOC_FLUSH _SNIOC(0x009C) /* Command: SNIOC_GET_EVENTS * Description: Get events of the sensor device. * Argument: The events pointer, (unsigned int *) */ -#define SNIOC_GET_EVENTS _SNIOC(0x009E) +#define SNIOC_GET_EVENTS _SNIOC(0x009D) /* Command: SNIOC_SET_THERMO * Description: Set the thermocouple type. diff --git a/include/nuttx/uorb.h b/include/nuttx/uorb.h index 067e997d5dba5..cd768145e6412 100644 --- a/include/nuttx/uorb.h +++ b/include/nuttx/uorb.h @@ -1220,23 +1220,6 @@ struct sensor_ustate_s uint64_t generation; /* The recent generation of circular buffer */ }; -/* This structure describes the register info for the user sensor */ - -#ifdef CONFIG_USENSOR -struct sensor_reginfo_s -{ - char path[NAME_MAX]; /* The path of user sensor */ - uint32_t esize; /* The element size of user sensor */ - uint32_t nbuffer; /* The number of queue buffered elements */ - - /* The flag is used to indicate that the validity of sensor data - * is persistent. - */ - - int persist; -}; -#endif - /* This structure describes the context custom ioctl for device */ struct sensor_ioctl_s @@ -1311,4 +1294,22 @@ struct sensor_device_info_s char vendor[SENSOR_INFO_NAME_SIZE]; }; +/* This structure describes the register info for the user sensor */ + +#ifdef CONFIG_USENSOR +struct sensor_reginfo_s +{ + char path[NAME_MAX]; /* The path of user sensor */ + uint32_t esize; /* The element size of user sensor */ + uint32_t nbuffer; /* The number of queue buffered elements */ + struct sensor_device_info_s devinfo; /* The device info. */ + + /* The flag is used to indicate that the validity of sensor data + * is persistent. + */ + + int persist; +}; +#endif + #endif /* __INCLUDE_NUTTX_SENSORS_SENSOR_H */ From f90e25fa20a322f878074f0c26547fa5b0afb44c Mon Sep 17 00:00:00 2001 From: likun17 Date: Tue, 3 Dec 2024 14:42:28 +0800 Subject: [PATCH 2/3] drivers/sensors: Fixed O_DIRECT flag triggering rpmsg cross-core. Fixes the issue of cross-core IPC being triggered when using O_DIRECT in non-cross-core scenarios. Signed-off-by: likun17 --- drivers/sensors/sensor.c | 64 +++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/drivers/sensors/sensor.c b/drivers/sensors/sensor.c index 5bfdaba006356..f56b95236302a 100644 --- a/drivers/sensors/sensor.c +++ b/drivers/sensors/sensor.c @@ -614,7 +614,7 @@ static int sensor_open(FAR struct file *filep) goto errout_with_lock; } - if (lower->ops->open) + if ((filep->f_oflags & O_DIRECT) == 0 && lower->ops->open) { ret = lower->ops->open(lower, filep); if (ret < 0) @@ -623,31 +623,32 @@ static int sensor_open(FAR struct file *filep) } } - if ((filep->f_oflags & O_DIRECT) == 0) + /* Using the O_DIRECT flag will prevent cross-core operations, + * allowing for direct I/O operations. + */ + + 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, filep, true); + if (ret < 0) { - ret = lower->ops->activate(lower, filep, true); - if (ret < 0) - { - goto errout_with_open; - } + goto errout_with_open; } - - user->role |= SENSOR_ROLE_RD; - upper->state.nsubscribers++; } - if (filep->f_oflags & O_WROK) + user->role |= SENSOR_ROLE_RD; + upper->state.nsubscribers++; + } + + if (filep->f_oflags & O_WROK) + { + user->role |= SENSOR_ROLE_WR; + upper->state.nadvertisers++; + if (filep->f_oflags & SENSOR_PERSIST) { - user->role |= SENSOR_ROLE_WR; - upper->state.nadvertisers++; - if (filep->f_oflags & SENSOR_PERSIST) - { - lower->persist = true; - } + lower->persist = true; } } @@ -696,7 +697,7 @@ static int sensor_close(FAR struct file *filep) int ret = 0; nxrmutex_lock(&upper->lock); - if (lower->ops->close) + if ((filep->f_oflags & O_DIRECT) == 0 && lower->ops->close) { ret = lower->ops->close(lower, filep); if (ret < 0) @@ -706,21 +707,22 @@ static int sensor_close(FAR struct file *filep) } } - if ((filep->f_oflags & O_DIRECT) == 0) + /* Using the O_DIRECT flag will prevent cross-core operations, + * allowing for direct I/O operations. + */ + + if (filep->f_oflags & O_RDOK) { - if (filep->f_oflags & O_RDOK) + upper->state.nsubscribers--; + if (upper->state.nsubscribers == 0 && lower->ops->activate) { - upper->state.nsubscribers--; - if (upper->state.nsubscribers == 0 && lower->ops->activate) - { - lower->ops->activate(lower, filep, false); - } + lower->ops->activate(lower, filep, false); } + } - if (filep->f_oflags & O_WROK) - { - upper->state.nadvertisers--; - } + if (filep->f_oflags & O_WROK) + { + upper->state.nadvertisers--; } list_delete(&user->node); From e5d54d9ca949224dbad96b76ea2c6e1b71e033d2 Mon Sep 17 00:00:00 2001 From: likun17 Date: Wed, 20 Nov 2024 15:35:41 +0800 Subject: [PATCH 3/3] drivers/sensors: Added virtual sensor flag to prioritize reading of driver information. When the local core is a virtual sensor, attempt to retrieve information from a remote core. Signed-off-by: likun17 --- drivers/sensors/sensor_rpmsg.c | 3 ++- drivers/sensors/usensor.c | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/sensors/sensor_rpmsg.c b/drivers/sensors/sensor_rpmsg.c index d5f798c2e1193..548570650fd12 100644 --- a/drivers/sensors/sensor_rpmsg.c +++ b/drivers/sensors/sensor_rpmsg.c @@ -774,7 +774,8 @@ static int sensor_rpmsg_get_info(FAR struct sensor_lowerhalf_s *lower, { ret = drv->ops->get_info(drv, filep, info); } - else if (!(filep->f_oflags & SENSOR_REMOTE)) + + if (ret == -ENOTTY && !(filep->f_oflags & SENSOR_REMOTE)) { ret = sensor_rpmsg_ioctl(dev, SNIOC_GET_INFO, (unsigned long)(uintptr_t)info, diff --git a/drivers/sensors/usensor.c b/drivers/sensors/usensor.c index 99fa16bdec754..4a97149a10338 100644 --- a/drivers/sensors/usensor.c +++ b/drivers/sensors/usensor.c @@ -229,6 +229,10 @@ static int usensor_get_info(FAR struct sensor_lowerhalf_s *lower, FAR struct usensor_lowerhalf_s *ulower = container_of(lower, struct usensor_lowerhalf_s, driver); + if (ulower->devinfo.name[0] == '\0') + { + return -ENOTTY; + } *info = ulower->devinfo; return 0;