Manual browser: gpio(4)

GPIO(4) Kernel Interfaces Manual GPIO(4)


gpioGeneral Purpose Input/Output


gpio* at elansc?
gpio* at epgpio?
gpio* at gcscpcib?
gpio* at gpiosim?
gpio* at gscpcib?
gpio* at ichlpcib?
gpio* at nsclpcsio?
gpio* at soekrisgpio?
gpio* at ppbus?
gpio* at ptcd?

#include <sys/types.h>
#include <sys/gpio.h>
#include <sys/ioctl.h>


The gpio device attaches to the GPIO controller and provides a uniform programming interface to its pins.

Each GPIO controller with an attached gpio device has an associated device file under the /dev directory, e.g. /dev/gpio0. Access from userland is performed through ioctl(2) calls on these devices.

Whether the layout of the GPIO device can be configured is subject to authorization by the kauth(9) framework.

If for example secmodel_securelevel(9) is active, the layout of the GPIO device is defined at a securelevel less than 1, i.e. typically during system boot, and cannot be changed later. GPIO pins can be configured and given a symbolic name and device drivers that use GPIO pins can be attached to the gpio device at a securelevel less than 1. All other pins will not be accessible once the runlevel has been raised.


The following structures and constants are defined in the <sys/gpio.h> header file:

GPIOINFO (struct gpio_info)
Returns information about the GPIO controller in the gpio_info structure:

struct gpio_info { 
	int gpio_npins;		/* total number of pins available */ 

GPIOREAD (struct gpio_req)
Returns the input pin value in the gpio_pin_op structure:

#define GPIOMAXNAME		64 
struct gpio_req { 
	char gp_name[GPIOMAXNAME];	/* pin name */ 
	int gp_pin;			/* pin number */ 
	int gp_value;			/* value */ 

The gp_name or gp_pin field must be set before calling.

GPIOWRITE (struct gpio_req)
Writes the output value to the pin. The value set in the gp_value field must be either GPIO_PIN_LOW (logical 0) or GPIO_PIN_HIGH (logical 1). On return, the gp_value field contains the old pin state.

GPIOTOGGLE (struct gpio_req)
Toggles the pin output value, i.e. changes it to the opposite. gp_value field is ignored and on return contains the old pin state.

GPIOSET (struct gpio_set)
Changes pin configuration flags with the new ones provided in the gpio_set structure:

#define GPIOMAXNAME          64 
struct gpio_set { 
        char gp_name[GPIOMAXNAME];   /* pin name */ 
        int gp_pin;                     /* pin number */ 
        int gp_caps;                    /* pin capabilities (ro) */ 
        int gp_flags;                   /* pin configuration flags */ 
        char gp_name2[GPIOMAXNAME];  /* new name */ 

The gp_flags field is a combination of the following flags:

input direction
output direction
open-drain output
push-pull output
output disabled
internal pull-up enabled
internal pull-down enabled
invert input
invert output
pulsate output

Note that the GPIO controller may not support all of these flags. On return the gp_caps field contains flags that are supported. If no flags are specified, the pin configuration stays unchanged.

Only GPIO pins that have been set using GPIOSET will be accessible at securelevels greater than 0.

GPIOUNSET (struct gpio_set)
Unset the specified pin, i.e. clear its name and make it unaccessible at securelevels greater than 0.

GPIOATTACH (struct gpio_attach)
Attach the device described in the gpio_attach structure on this gpio device.

struct gpio_attach { 
        char ga_dvname[16];     /* device name */ 
        int ga_offset;          /* pin number */ 
        uint32_t ga_mask;       /* binary mask */ 
        uint32_t ga_flags;      /* driver dependent */ 

The drvctl(8) command can be used to detach a device from a gpio pin.


GPIO device unit u file.


The gpio device first appeared in OpenBSD 3.6 and NetBSD 4.0.


The gpio driver was written by Alexander Yurchenko <>. gpio and was ported to NetBSD by Jared D. McNeill <>. Runtime device attachment was added by Marc Balmer <>.


Event capabilities are not supported.
June 10, 2013 NetBSD 7.0