Search code examples
clinuxembedded-linuxgpio

Linux Gpio V2 request line ioctl failing


I'm trying to implement the new linux gpio api. Using the v1 api, I was able to confirm that this code works:

// req is part of larger code
struct gpiohandle_request lreq;
memset(lreq.default_values, 0, sizeof(lreq.default_values));
strcpy(lreq.consumer_label, "TESTIO");
lreq.lines = req.bank_count[bank];
lreq.flags = GPIOHANDLE_REQUEST_OUTPUT;

for (int line = 0; line < lreq.lines; line++)
  lreq.lineoffsets[line] = req.pins[bank][line];

if (ioctl(bank_fd[bank], GPIO_GET_LINEHANDLE_IOCTL, &lreq) < 0) {
  std::cerr << "Error on chip io\n";
  return -1;
}

However, when I try to switch to v2:

struct gpio_v2_line_request lreq;
lreq.config.flags = GPIO_V2_LINE_FLAG_OUTPUT;
lreq.config.num_attrs = 0;
strcpy(lreq.consumer, "TESTIO");
lreq.num_lines = req.bank_count[bank];

for (int line = 0; line < lreq.num_lines; line++) {
  lreq.offsets[line] = req.pins[bank][line];
}

if (ioctl(bank_fd[bank], GPIO_V2_GET_LINE_IOCTL, &lreq) < 0) {
  std::cerr << "Error on chip io\n";
  return -1;
}

my ioctl always fails with errno 22

I'm not sure what the equivalent of memset(lreq.default_values, 0, sizeof(lreq.default_values)) is


Solution

  • Solution was to simply do this first:

    memset(&lreq, 0, sizeof(lreq));
    

    I guess that is the equivalent of the default values. Found in the master branch of libgpiod here