Well, is it really that much? There are 579 files under /sys/class/tty. But
suppose it is too much (why isn't tty too much then?), then we can do 3.
The back end doesn't seem that big to me. Here's code for it. If anything,
the parsing code is simpler than what David has. It's certainly not huge.
David's code for parsing the control file plus code for generating a mapping
range file would certainly be larger.
/* Format: -?(chiplabel:)?number
* The optional leading - unexports the gpio, without it the gpio is exported.
* The optional chip label followed by a : gives you the Nth gpio of that
* chip. With no label you get gpio "number".
*/
static ssize_t control_store(struct class *class, const char *buf, size_t len)
{
const char *numstr;
unsigned long num;
int mode = 0;
if (buf[0] == '-') { /* un-export? */
mode = 1;
buf++;
}
numstr = strrchr(buf, ':');
/* Get GPIO number */
if (strict_strtoul(numstr ? numstr + 1 : buf, 0, &num))
return -EINVAL;
/* Match chip label, if one was specified */
if (numstr) {
/* No + 1 in len to not include the ':' at the end */
int i, len = numstr - buf;
const struct gpio_chip *chip = NULL;
for (i = 0; gpio_is_valid(i); i++) {
if (chip == gpio_desc[i].chip)
continue;
chip = gpio_desc[i].chip;
if (!chip)
continue;
if (!strncmp(buf, chip->label, len) &&
chip->label[len] == '\0')
goto found_chip;
}
return -EINVAL;
found_chip:
if (num >= chip->ngpio)
return -EINVAL;
num += chip->base;
}
if (mode) {
/* Unexport */
if (!gpio_is_valid(num))
return -EINVAL;
if (!test_and_clear_bit(FLAG_SYSFS, &gpio_desc[num].flags))
return -EINVAL;
gpio_free(num);
} else {
/* Export */
int status = gpio_request(num, "sysfs");
if (status < 0)
return status;
status = gpio_export(num);
if (status < 0) {
gpio_free(num);
return status;
}
set_bit(FLAG_SYSFS, &gpio_desc[num].flags);
}
return len;
}
--