From 1bad603fe7554021df0e8cd10b5586fdb4d4c779 Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Mon, 22 Jan 2024 14:13:48 +0800 Subject: [PATCH] drivers/gpio: save memory if dont support signal Signed-off-by: dongjiuzhu1 --- drivers/ioexpander/Kconfig | 8 ++++++++ drivers/ioexpander/gpio.c | 29 +++++++++++++++++++++++------ include/nuttx/ioexpander/gpio.h | 6 +++++- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/drivers/ioexpander/Kconfig b/drivers/ioexpander/Kconfig index ed69d67ac6..6f982b3fbb 100644 --- a/drivers/ioexpander/Kconfig +++ b/drivers/ioexpander/Kconfig @@ -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 diff --git a/drivers/ioexpander/gpio.c b/drivers/ioexpander/gpio.c index 2791df2156..be76f71ac2 100644 --- a/drivers/ioexpander/gpio.c +++ b/drivers/ioexpander/gpio.c @@ -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; diff --git a/include/nuttx/ioexpander/gpio.h b/include/nuttx/ioexpander/gpio.h index fd45ab7822..fcfc0f217f 100644 --- a/include/nuttx/ioexpander/gpio.h +++ b/include/nuttx/ioexpander/gpio.h @@ -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. */ };