Skip to content

Commit ac50b3a

Browse files
davids5xiaoxiang781216
authored andcommitted
ioexpander/gpio:Add gpio_pin_register_byname
1 parent bc60596 commit ac50b3a

File tree

2 files changed

+160
-3
lines changed

2 files changed

+160
-3
lines changed

drivers/ioexpander/gpio.c

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,14 +546,57 @@ static int gpio_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
546546
* Register GPIO pin device driver at /dev/gpioN, where N is the provided
547547
* minor number.
548548
*
549+
* Input Parameters:
550+
* dev - A pointer to a gpio_dev_s
551+
* minor - An integer value to be concatenated with '/dev/gpio'
552+
* to form the device name.
553+
*
554+
* Returned Value:
555+
* Zero on success; A negated errno value is returned on a failure
556+
* all error values returned by inode_reserve:
557+
*
558+
* EINVAL - 'path' is invalid for this operation
559+
* EEXIST - An inode already exists at 'path'
560+
* ENOMEM - Failed to allocate in-memory resources for the operation
561+
*
549562
****************************************************************************/
550563

551564
int gpio_pin_register(FAR struct gpio_dev_s *dev, int minor)
565+
{
566+
char devname[32];
567+
568+
snprintf(devname, sizeof(devname), "gpio%u", (unsigned int)minor);
569+
return gpio_pin_register_byname(dev, devname);
570+
}
571+
572+
/****************************************************************************
573+
* Name: gpio_pin_register_byname
574+
*
575+
* Description:
576+
* Register GPIO pin device driver with it's pin name.
577+
*
578+
* Input Parameters:
579+
* dev - A pointer to a gpio_dev_s
580+
* pinname - A pointer to the name to be concatenated with '/dev/'
581+
* to form the device name.
582+
*
583+
* Returned Value:
584+
* Zero on success; A negated errno value is returned on a failure
585+
* all error values returned by inode_reserve:
586+
*
587+
* EINVAL - 'path' is invalid for this operation
588+
* EEXIST - An inode already exists at 'path'
589+
* ENOMEM - Failed to allocate in-memory resources for the operation
590+
*
591+
****************************************************************************/
592+
593+
int gpio_pin_register_byname(FAR struct gpio_dev_s *dev,
594+
FAR const char *pinname)
552595
{
553596
char devname[32];
554597
int ret;
555598

556-
DEBUGASSERT(dev != NULL && dev->gp_ops != NULL);
599+
DEBUGASSERT(dev != NULL && dev->gp_ops != NULL && pinname != NULL);
557600

558601
switch (dev->gp_pintype)
559602
{
@@ -590,7 +633,8 @@ int gpio_pin_register(FAR struct gpio_dev_s *dev, int minor)
590633
break;
591634
}
592635

593-
snprintf(devname, sizeof(devname), "/dev/gpio%u", (unsigned int)minor);
636+
snprintf(devname, sizeof(devname), "/dev/%s", pinname);
637+
594638
gpioinfo("Registering %s\n", devname);
595639

596640
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)
603647
* Unregister GPIO pin device driver at /dev/gpioN, where N is the provided
604648
* minor number.
605649
*
650+
* Input Parameters:
651+
* dev - A pointer to a gpio_dev_s
652+
* minor - An integer value to be concatenated with '/dev/gpio'
653+
* to form the device name.
654+
*
655+
* Returned Value:
656+
* Zero on success; A negated value is returned on a failure
657+
* (all error values returned by inode_remove):
658+
*
659+
* ENOENT - path does not exist.
660+
* EBUSY - Ref count is not 0;
661+
*
606662
****************************************************************************/
607663

608664
int gpio_pin_unregister(FAR struct gpio_dev_s *dev, int minor)
609665
{
610666
char devname[32];
667+
snprintf(devname, sizeof(devname), "gpio%u", (unsigned int)minor);
668+
return gpio_pin_unregister_byname(dev, devname);
669+
}
670+
671+
/****************************************************************************
672+
* Name: gpio_pin_unregister_byname
673+
*
674+
* Description:
675+
* Unregister GPIO pin device driver at /dev/pinname.
676+
*
677+
* Input Parameters:
678+
* dev - A pointer to a gpio_dev_s
679+
* pinname - A pointer to the name to be concatenated with '/dev/'
680+
* to form the device name.
681+
*
682+
*
683+
* Returned Value:
684+
* Zero on success; A negated value is returned on a failure
685+
* (all error values returned by inode_remove):
686+
*
687+
* ENOENT - path does not exist.
688+
* EBUSY - Ref count is not 0;
689+
****************************************************************************/
690+
691+
int gpio_pin_unregister_byname(FAR struct gpio_dev_s *dev,
692+
FAR const char *pinname)
693+
{
694+
char devname[32];
695+
696+
snprintf(devname, sizeof(devname), "/dev/%s", pinname);
611697

612-
snprintf(devname, sizeof(devname), "/dev/gpio%u", (unsigned int)minor);
613698
gpioinfo("Unregistering %s\n", devname);
614699

615700
return unregister_driver(devname);

include/nuttx/ioexpander/gpio.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,21 +184,93 @@ extern "C"
184184
* Register GPIO pin device driver at /dev/gpioN, where N is the provided
185185
* minor number.
186186
*
187+
* Input Parameters:
188+
* dev - A pointer to a gpio_dev_s
189+
* minor - An integer value to be concatenated with '/dev/gpio'
190+
* to form the device name.
191+
*
192+
* Returned Value:
193+
* Zero on success; A negated errno value is returned on a failure
194+
* all error values returned by inode_reserve:
195+
*
196+
* EINVAL - 'path' is invalid for this operation
197+
* EEXIST - An inode already exists at 'path'
198+
* ENOMEM - Failed to allocate in-memory resources for the operation
199+
*
187200
****************************************************************************/
188201

189202
int gpio_pin_register(FAR struct gpio_dev_s *dev, int minor);
190203

204+
/****************************************************************************
205+
* Name: gpio_pin_register_byname
206+
*
207+
* Description:
208+
* Register GPIO pin device driver with it's pin name.
209+
*
210+
* Input Parameters:
211+
* dev - A pointer to a gpio_dev_s
212+
* pinname - A pointer to the name to be concatenated with '/dev/'
213+
* to form the device name.
214+
*
215+
* Returned Value:
216+
* Zero on success; A negated errno value is returned on a failure
217+
* all error values returned by inode_reserve:
218+
*
219+
* EINVAL - 'path' is invalid for this operation
220+
* EEXIST - An inode already exists at 'path'
221+
* ENOMEM - Failed to allocate in-memory resources for the operation
222+
*
223+
****************************************************************************/
224+
225+
int gpio_pin_register_byname(FAR struct gpio_dev_s *dev,
226+
FAR const char *pinname);
227+
191228
/****************************************************************************
192229
* Name: gpio_pin_unregister
193230
*
194231
* Description:
195232
* Unregister GPIO pin device driver at /dev/gpioN, where N is the provided
196233
* minor number.
197234
*
235+
* Input Parameters:
236+
* dev - A pointer to a gpio_dev_s
237+
* minor - An integer value to be concatenated with '/dev/gpio'
238+
* to form the device name.
239+
*
240+
* Returned Value:
241+
* Zero on success; A negated value is returned on a failure
242+
* (all error values returned by inode_remove):
243+
*
244+
* ENOENT - path does not exist.
245+
* EBUSY - Ref count is not 0;
246+
*
198247
****************************************************************************/
199248

200249
int gpio_pin_unregister(FAR struct gpio_dev_s *dev, int minor);
201250

251+
/****************************************************************************
252+
* Name: gpio_pin_unregister_byname
253+
*
254+
* Description:
255+
* Unregister GPIO pin device driver at /dev/pinname.
256+
*
257+
* Input Parameters:
258+
* dev - A pointer to a gpio_dev_s
259+
* pinname - A pointer to the name to be concatenated with '/dev/'
260+
* to form the device name.
261+
*
262+
*
263+
* Returned Value:
264+
* Zero on success; A negated value is returned on a failure
265+
* (all error values returned by inode_remove):
266+
*
267+
* ENOENT - path does not exist.
268+
* EBUSY - Ref count is not 0;
269+
****************************************************************************/
270+
271+
int gpio_pin_unregister_byname(FAR struct gpio_dev_s *dev,
272+
FAR const char *pinname);
273+
202274
/****************************************************************************
203275
* Name: gpio_lower_half
204276
*

0 commit comments

Comments
 (0)