Skip to content

Commit

Permalink
ftdi_gpio: accept new yaml config
Browse files Browse the repository at this point in the history
Now local_gpio has landed, also accept a more verbose
and cleaner yaml config syntax for ftdi_gpio.

For now keep support for the current configuratoin string.

Closes: #42

Signed-off-by: Neil Armstrong <[email protected]>
  • Loading branch information
superna9999 committed Oct 31, 2023
1 parent 418243c commit cf9f3d3
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 18 deletions.
22 changes: 22 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,25 @@ devices:
fastboot: cacafada
fastboot_set_active: true
fastboot_key_timeout: 2

- board: testboard2
console: /dev/serial/by-id/usb-1234-if00-port0
name: FTDI controller board
ftdi_gpio:
description: "s:0x0403:0x6011:FT7YWRL8"
interface: D
gpios:
- power:
line: 7
active_low: true
- fastboot_key:
line: 8
active_low: true
- power_key:
line: 3
active_low: true
- usb_disconnect:
line: 2
fastboot: cacafada
fastboot_set_active: true
fastboot_key_timeout: 2
8 changes: 5 additions & 3 deletions device_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ static void parse_board(struct device_parser *dp)
if (dev->control_options)
set_control_ops(dev, &local_gpio_ops);
continue;
} else if (!strcmp(key, "ftdi_gpio")) {
dev->control_options = ftdi_gpio_ops.parse_options(dp);
if (dev->control_options)
set_control_ops(dev, &ftdi_gpio_ops);
continue;
}

device_parser_expect(dp, YAML_SCALAR_EVENT, value, TOKEN_LENGTH);
Expand All @@ -154,9 +159,6 @@ static void parse_board(struct device_parser *dp)
} else if (!strcmp(key, "alpaca")) {
dev->control_dev = strdup(value);
set_control_ops(dev, &alpaca_ops);
} else if (!strcmp(key, "ftdi_gpio")) {
dev->control_dev = strdup(value);
set_control_ops(dev, &ftdi_gpio_ops);
} else if (!strcmp(key, "external")) {
dev->control_dev = strdup(value);
set_control_ops(dev, &external_ops);
Expand Down
131 changes: 116 additions & 15 deletions ftdi-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <yaml.h>

#include "cdba-server.h"
#include "device.h"
#include "device_parser.h"

#define TOKEN_LENGTH 16384

#include <ftdi.h>

Expand All @@ -53,7 +57,7 @@ enum {
GPIO_COUNT
};

struct ftdi_gpio {
struct ftdi_gpio_options {
struct {
char *description;
unsigned int interface;
Expand All @@ -63,6 +67,10 @@ struct ftdi_gpio {
unsigned int offset;
bool active_low;
} gpios[GPIO_COUNT];
};

struct ftdi_gpio {
struct ftdi_gpio_options *options;
struct ftdi_context *gpio;
unsigned char gpio_lines;
};
Expand All @@ -82,7 +90,7 @@ static void ftdi_gpio_device_usb(struct ftdi_gpio *ftdi_gpio, bool on);
* Example: s:0xVEND:0xPROD:SERIAL;D;POWER,0,ACTIVE_LOW;FASTBOOT_KEY,1,ACTIVE_HIGH;POWER_KEY,2,ACTIVE_HIGH;USB_DISCONNECT,3,ACTIVE_LOW
*/

static void ftdi_gpio_parse_config(struct ftdi_gpio *ftdi_gpio, char *value)
static void ftdi_gpio_parse_config(struct ftdi_gpio_options *options, char *value)
{
char *c, *interface;
size_t device_len;
Expand All @@ -94,7 +102,7 @@ static void ftdi_gpio_parse_config(struct ftdi_gpio *ftdi_gpio, char *value)
else
device_len = c - value;

ftdi_gpio->ftdi.description = strndup(value, device_len);
options->ftdi.description = strndup(value, device_len);

if (!c)
return;
Expand All @@ -107,7 +115,7 @@ static void ftdi_gpio_parse_config(struct ftdi_gpio *ftdi_gpio, char *value)
*interface != 'D') {
errx(1, "Invalid interface '%c'", *interface);
}
ftdi_gpio->ftdi.interface = *interface - 'A';
options->ftdi.interface = *interface - 'A';

c = strchr(interface, ';');

Expand Down Expand Up @@ -152,10 +160,102 @@ static void ftdi_gpio_parse_config(struct ftdi_gpio *ftdi_gpio, char *value)
else
errx(1, "GPIOs polarity invalid: '%s'", pol);

ftdi_gpio->gpios[gpio_type].present = true;
ftdi_gpio->gpios[gpio_type].offset = gpio_offset;
ftdi_gpio->gpios[gpio_type].active_low = active_low;
options->gpios[gpio_type].present = true;
options->gpios[gpio_type].offset = gpio_offset;
options->gpios[gpio_type].active_low = active_low;
}
}

void *ftdi_gpio_parse_options(struct device_parser *dp)
{
struct ftdi_gpio_options *options;
char value[TOKEN_LENGTH];
char key[TOKEN_LENGTH];

options = calloc(1, sizeof(*options));

/* Still accept legacy string */
if (device_parser_accept(dp, YAML_SCALAR_EVENT, value, TOKEN_LENGTH)) {
warnx("Please switch to yaml config for ftdi_gpio configuration");
ftdi_gpio_parse_config(options, value);
return options;
}

device_parser_accept(dp, YAML_MAPPING_START_EVENT, NULL, 0);

device_parser_expect(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH);
if (strcmp(key, "description")) {
errx(1, "%s: expected 'description' instead of \"%s\"", __func__, key);
exit(1);
}

device_parser_expect(dp, YAML_SCALAR_EVENT, value, TOKEN_LENGTH);
options->ftdi.description = strdup(value);

device_parser_expect(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH);
if (strcmp(key, "interface")) {
errx(1, "%s: expected 'interface' instead of \"%s\"", __func__, key);
exit(1);
}

device_parser_expect(dp, YAML_SCALAR_EVENT, value, TOKEN_LENGTH);
options->ftdi.interface = *value - 'A';

device_parser_expect(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH);
if (strcmp(key, "gpios")) {
errx(1, "%s: expected 'gpios' instead of \"%s\"", __func__, key);
exit(1);
}

device_parser_expect(dp, YAML_SEQUENCE_START_EVENT, NULL, 0);

/* Loop over sub-properties */
while (device_parser_accept(dp, YAML_MAPPING_START_EVENT, NULL, 0)) {
int gpio_id;

device_parser_accept(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH);

if (!strcmp(key, "power")) {
gpio_id = GPIO_POWER;
} else if (!strcmp(key, "fastboot_key")) {
gpio_id = GPIO_FASTBOOT_KEY;
} else if (!strcmp(key, "power_key")) {
gpio_id = GPIO_POWER_KEY;
} else if (!strcmp(key, "usb_disconnect")) {
gpio_id = GPIO_USB_DISCONNECT;
} else {
errx(1, "%s: unknown type \"%s\"", __func__, key);
exit(1);
}

device_parser_accept(dp, YAML_MAPPING_START_EVENT, NULL, 0);

while (device_parser_accept(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH)) {
device_parser_accept(dp, YAML_SCALAR_EVENT, value, TOKEN_LENGTH);

if (!strcmp(key, "line")) {
options->gpios[gpio_id].offset = strtoul(value, NULL, 0);
} else if (!strcmp(key, "active_low")) {
if (!strcmp(value, "true"))
options->gpios[gpio_id].active_low = true;
} else {
errx(1, "%s: unknown option \"%s\"", __func__, key);
exit(1);
}
}

device_parser_expect(dp, YAML_MAPPING_END_EVENT, NULL, 0);

options->gpios[gpio_id].present = true;

device_parser_expect(dp, YAML_MAPPING_END_EVENT, NULL, 0);
}

device_parser_expect(dp, YAML_SEQUENCE_END_EVENT, NULL, 0);

device_parser_expect(dp, YAML_MAPPING_END_EVENT, NULL, 0);

return options;
}

static void *ftdi_gpio_open(struct device *dev)
Expand All @@ -165,21 +265,21 @@ static void *ftdi_gpio_open(struct device *dev)

ftdi_gpio = calloc(1, sizeof(*ftdi_gpio));

ftdi_gpio_parse_config(ftdi_gpio, dev->control_dev);
ftdi_gpio->options = dev->control_options;

if ((ftdi_gpio->gpio = ftdi_new()) == 0)
errx(1, "failed to allocate ftdi gpio struct");

ftdi_set_interface(ftdi_gpio->gpio, INTERFACE_A + ftdi_gpio->ftdi.interface);
ftdi_set_interface(ftdi_gpio->gpio, INTERFACE_A + ftdi_gpio->options->ftdi.interface);

ret = ftdi_usb_open_string(ftdi_gpio->gpio, ftdi_gpio->ftdi.description);
ret = ftdi_usb_open_string(ftdi_gpio->gpio, ftdi_gpio->options->ftdi.description);
if (ret < 0)
errx(1, "failed to open ftdi gpio device '%s' (%d)",
ftdi_gpio->ftdi.description, ret);
ftdi_gpio->options->ftdi.description, ret);

ftdi_set_bitmode(ftdi_gpio->gpio, 0xFF, BITMODE_BITBANG);

if (ftdi_gpio->gpios[GPIO_POWER_KEY].present)
if (ftdi_gpio->options->gpios[GPIO_POWER_KEY].present)
dev->has_power_key = true;

ftdi_gpio_device_power(ftdi_gpio, 0);
Expand All @@ -198,12 +298,12 @@ static int ftdi_gpio_toggle_io(struct ftdi_gpio *ftdi_gpio, unsigned int gpio, b
{
unsigned int bit;

if (!ftdi_gpio->gpios[gpio].present)
if (!ftdi_gpio->options->gpios[gpio].present)
return -EINVAL;

bit = ftdi_gpio->gpios[gpio].offset;
bit = ftdi_gpio->options->gpios[gpio].offset;

if (ftdi_gpio->gpios[gpio].active_low)
if (ftdi_gpio->options->gpios[gpio].active_low)
on = !on;

if (on)
Expand Down Expand Up @@ -253,6 +353,7 @@ static void ftdi_gpio_key(struct device *dev, int key, bool asserted)
}

const struct control_ops ftdi_gpio_ops = {
.parse_options = ftdi_gpio_parse_options,
.open = ftdi_gpio_open,
.power = ftdi_gpio_power,
.usb = ftdi_gpio_usb,
Expand Down

0 comments on commit cf9f3d3

Please sign in to comment.