From ac50b3a473241504d61d52c2d370cf0239fe1110 Mon Sep 17 00:00:00 2001 From: David Sidrane Date: Tue, 4 Oct 2022 04:54:23 -0700 Subject: [PATCH] ioexpander/gpio:Add gpio_pin_register_byname --- drivers/ioexpander/gpio.c | 91 +++++++++++++++++++++++++++++++-- include/nuttx/ioexpander/gpio.h | 72 ++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 3 deletions(-) diff --git a/drivers/ioexpander/gpio.c b/drivers/ioexpander/gpio.c index 7d6547d86f..a78ae99058 100644 --- a/drivers/ioexpander/gpio.c +++ b/drivers/ioexpander/gpio.c @@ -546,14 +546,57 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg) * Register GPIO pin device driver at /dev/gpioN, where N is the provided * minor number. * + * Input Parameters: + * dev - A pointer to a gpio_dev_s + * minor - An integer value to be concatenated with '/dev/gpio' + * to form the device name. + * + * Returned Value: + * Zero on success; A negated errno value is returned on a failure + * all error values returned by inode_reserve: + * + * EINVAL - 'path' is invalid for this operation + * EEXIST - An inode already exists at 'path' + * ENOMEM - Failed to allocate in-memory resources for the operation + * ****************************************************************************/ int gpio_pin_register(FAR struct gpio_dev_s *dev, int minor) +{ + char devname[32]; + + snprintf(devname, sizeof(devname), "gpio%u", (unsigned int)minor); + return gpio_pin_register_byname(dev, devname); +} + +/**************************************************************************** + * Name: gpio_pin_register_byname + * + * Description: + * Register GPIO pin device driver with it's pin name. + * + * Input Parameters: + * dev - A pointer to a gpio_dev_s + * pinname - A pointer to the name to be concatenated with '/dev/' + * to form the device name. + * + * Returned Value: + * Zero on success; A negated errno value is returned on a failure + * all error values returned by inode_reserve: + * + * EINVAL - 'path' is invalid for this operation + * EEXIST - An inode already exists at 'path' + * ENOMEM - Failed to allocate in-memory resources for the operation + * + ****************************************************************************/ + +int gpio_pin_register_byname(FAR struct gpio_dev_s *dev, + FAR const char *pinname) { char devname[32]; int ret; - DEBUGASSERT(dev != NULL && dev->gp_ops != NULL); + DEBUGASSERT(dev != NULL && dev->gp_ops != NULL && pinname != NULL); switch (dev->gp_pintype) { @@ -590,7 +633,8 @@ int gpio_pin_register(FAR struct gpio_dev_s *dev, int minor) break; } - snprintf(devname, sizeof(devname), "/dev/gpio%u", (unsigned int)minor); + snprintf(devname, sizeof(devname), "/dev/%s", pinname); + gpioinfo("Registering %s\n", devname); return register_driver(devname, &g_gpio_drvrops, 0666, dev); @@ -603,13 +647,54 @@ int gpio_pin_register(FAR struct gpio_dev_s *dev, int minor) * Unregister GPIO pin device driver at /dev/gpioN, where N is the provided * minor number. * + * Input Parameters: + * dev - A pointer to a gpio_dev_s + * minor - An integer value to be concatenated with '/dev/gpio' + * to form the device name. + * + * Returned Value: + * Zero on success; A negated value is returned on a failure + * (all error values returned by inode_remove): + * + * ENOENT - path does not exist. + * EBUSY - Ref count is not 0; + * ****************************************************************************/ int gpio_pin_unregister(FAR struct gpio_dev_s *dev, int minor) { char devname[32]; + snprintf(devname, sizeof(devname), "gpio%u", (unsigned int)minor); + return gpio_pin_unregister_byname(dev, devname); +} + +/**************************************************************************** + * Name: gpio_pin_unregister_byname + * + * Description: + * Unregister GPIO pin device driver at /dev/pinname. + * + * Input Parameters: + * dev - A pointer to a gpio_dev_s + * pinname - A pointer to the name to be concatenated with '/dev/' + * to form the device name. + * + * + * Returned Value: + * Zero on success; A negated value is returned on a failure + * (all error values returned by inode_remove): + * + * ENOENT - path does not exist. + * EBUSY - Ref count is not 0; + ****************************************************************************/ + +int gpio_pin_unregister_byname(FAR struct gpio_dev_s *dev, + FAR const char *pinname) +{ + char devname[32]; + + snprintf(devname, sizeof(devname), "/dev/%s", pinname); - snprintf(devname, sizeof(devname), "/dev/gpio%u", (unsigned int)minor); gpioinfo("Unregistering %s\n", devname); return unregister_driver(devname); diff --git a/include/nuttx/ioexpander/gpio.h b/include/nuttx/ioexpander/gpio.h index 395d097821..4e8bae0783 100644 --- a/include/nuttx/ioexpander/gpio.h +++ b/include/nuttx/ioexpander/gpio.h @@ -184,10 +184,47 @@ extern "C" * Register GPIO pin device driver at /dev/gpioN, where N is the provided * minor number. * + * Input Parameters: + * dev - A pointer to a gpio_dev_s + * minor - An integer value to be concatenated with '/dev/gpio' + * to form the device name. + * + * Returned Value: + * Zero on success; A negated errno value is returned on a failure + * all error values returned by inode_reserve: + * + * EINVAL - 'path' is invalid for this operation + * EEXIST - An inode already exists at 'path' + * ENOMEM - Failed to allocate in-memory resources for the operation + * ****************************************************************************/ int gpio_pin_register(FAR struct gpio_dev_s *dev, int minor); +/**************************************************************************** + * Name: gpio_pin_register_byname + * + * Description: + * Register GPIO pin device driver with it's pin name. + * + * Input Parameters: + * dev - A pointer to a gpio_dev_s + * pinname - A pointer to the name to be concatenated with '/dev/' + * to form the device name. + * + * Returned Value: + * Zero on success; A negated errno value is returned on a failure + * all error values returned by inode_reserve: + * + * EINVAL - 'path' is invalid for this operation + * EEXIST - An inode already exists at 'path' + * ENOMEM - Failed to allocate in-memory resources for the operation + * + ****************************************************************************/ + +int gpio_pin_register_byname(FAR struct gpio_dev_s *dev, + FAR const char *pinname); + /**************************************************************************** * Name: gpio_pin_unregister * @@ -195,10 +232,45 @@ int gpio_pin_register(FAR struct gpio_dev_s *dev, int minor); * Unregister GPIO pin device driver at /dev/gpioN, where N is the provided * minor number. * + * Input Parameters: + * dev - A pointer to a gpio_dev_s + * minor - An integer value to be concatenated with '/dev/gpio' + * to form the device name. + * + * Returned Value: + * Zero on success; A negated value is returned on a failure + * (all error values returned by inode_remove): + * + * ENOENT - path does not exist. + * EBUSY - Ref count is not 0; + * ****************************************************************************/ int gpio_pin_unregister(FAR struct gpio_dev_s *dev, int minor); +/**************************************************************************** + * Name: gpio_pin_unregister_byname + * + * Description: + * Unregister GPIO pin device driver at /dev/pinname. + * + * Input Parameters: + * dev - A pointer to a gpio_dev_s + * pinname - A pointer to the name to be concatenated with '/dev/' + * to form the device name. + * + * + * Returned Value: + * Zero on success; A negated value is returned on a failure + * (all error values returned by inode_remove): + * + * ENOENT - path does not exist. + * EBUSY - Ref count is not 0; + ****************************************************************************/ + +int gpio_pin_unregister_byname(FAR struct gpio_dev_s *dev, + FAR const char *pinname); + /**************************************************************************** * Name: gpio_lower_half *