drivers/gpio: save memory if dont support signal

Signed-off-by: dongjiuzhu1 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2024-01-22 14:13:48 +08:00 committed by Xiang Xiao
parent 28815fb7c5
commit 1bad603fe7
3 changed files with 36 additions and 7 deletions

View File

@ -499,6 +499,14 @@ config DEV_GPIO
Enables a simple GPIO input/output driver to support application-
space testing of hardware.
config DEV_GPIO_NPOLLWAITERS
int "Max number of polls"
default 1
depends on DEV_GPIO
---help---
The maximum number of polls that can be registered with the GPIO
driver
config DEV_GPIO_NSIGNALS
int "Max number of signals"
default 1

View File

@ -87,14 +87,19 @@ static const struct file_operations g_gpio_drvrops =
static int gpio_handler(FAR struct gpio_dev_s *dev, uint8_t pin)
{
#if CONFIG_DEV_GPIO_NSIGNALS > 0
int i;
#endif
DEBUGASSERT(dev != NULL);
dev->int_count++;
poll_notify(dev->fds, CONFIG_DEV_GPIO_NSIGNALS, POLLIN);
#if CONFIG_DEV_GPIO_NPOLLWAITERS > 0
poll_notify(dev->fds, CONFIG_DEV_GPIO_NPOLLWAITERS, POLLIN);
#endif
#if CONFIG_DEV_GPIO_NSIGNALS > 0
for (i = 0; i < CONFIG_DEV_GPIO_NSIGNALS; i++)
{
FAR struct gpio_signal_s *signal = &dev->gp_signals[i];
@ -107,6 +112,7 @@ static int gpio_handler(FAR struct gpio_dev_s *dev, uint8_t pin)
nxsig_notification(signal->gp_pid, &signal->gp_event,
SI_QUEUE, &signal->gp_work);
}
#endif
return OK;
}
@ -305,10 +311,12 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
FAR struct inode *inode;
FAR struct gpio_dev_s *dev;
irqstate_t flags;
pid_t pid;
int ret = OK;
#if CONFIG_DEV_GPIO_NSIGNALS > 0
pid_t pid;
int i;
int j;
#endif
inode = filep->f_inode;
DEBUGASSERT(inode->i_private != NULL);
@ -383,6 +391,7 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
if (dev->gp_pintype >= GPIO_INTERRUPT_PIN)
{
flags = enter_critical_section();
#if CONFIG_DEV_GPIO_NSIGNALS > 0
if (arg)
{
pid = nxsched_getpid();
@ -406,6 +415,7 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
break;
}
}
#endif
if (dev->register_count++ > 0)
{
@ -442,8 +452,9 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
case GPIOC_UNREGISTER:
if (dev->gp_pintype >= GPIO_INTERRUPT_PIN)
{
pid = nxsched_getpid();
flags = enter_critical_section();
#if CONFIG_DEV_GPIO_NSIGNALS > 0
pid = nxsched_getpid();
for (i = 0; i < CONFIG_DEV_GPIO_NSIGNALS; i++)
{
if (pid == dev->gp_signals[i].gp_pid)
@ -467,6 +478,7 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
break;
}
}
#endif
if (--dev->register_count > 0)
{
@ -583,22 +595,26 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
static int gpio_poll(FAR struct file *filep,
FAR struct pollfd *fds, bool setup)
{
#if CONFIG_DEV_GPIO_NPOLLWAITERS > 0
FAR struct inode *inode = filep->f_inode;
FAR struct gpio_dev_s *dev = inode->i_private;
int i;
#endif
irqstate_t flags;
int ret = OK;
int i;
/* Are we setting up the poll? Or tearing it down? */
flags = enter_critical_section();
if (setup)
{
#if CONFIG_DEV_GPIO_NPOLLWAITERS > 0
/* This is a request to set up the poll. Find an available
* slot for the poll structure reference
*/
for (i = 0; i < CONFIG_DEV_GPIO_NSIGNALS; i++)
for (i = 0; i < CONFIG_DEV_GPIO_NPOLLWAITERS; i++)
{
/* Find an available slot */
@ -620,7 +636,8 @@ static int gpio_poll(FAR struct file *filep,
}
}
if (i >= CONFIG_DEV_GPIO_NSIGNALS)
if (i >= CONFIG_DEV_GPIO_NPOLLWAITERS)
#endif
{
fds->priv = NULL;
ret = -EBUSY;

View File

@ -162,7 +162,9 @@ struct gpio_dev_s
/* Writable storage used by the upper half driver */
#if CONFIG_DEV_GPIO_NSIGNALS > 0
struct gpio_signal_s gp_signals[CONFIG_DEV_GPIO_NSIGNALS];
#endif
/* Read-only pointer to GPIO device operations (also provided by the
* lower half driver).
@ -170,7 +172,9 @@ struct gpio_dev_s
FAR const struct gpio_operations_s *gp_ops;
FAR struct pollfd *fds[CONFIG_DEV_GPIO_NSIGNALS];
#if CONFIG_DEV_GPIO_NPOLLWAITERS > 0
FAR struct pollfd *fds[CONFIG_DEV_GPIO_NPOLLWAITERS];
#endif
/* Device specific, lower-half information may follow. */
};