struct gpiodev_data { struct list_head exported_lines; struct gpio_device *gdev; struct device *cdev_id; /* Class device by GPIO device ID */ #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY) struct device *cdev_base; /* Class device by GPIO base */ #endif/* CONFIG_GPIO_SYSFS_LEGACY */
};
/* * Lock to serialise gpiod export and unexport, and prevent re-export of * gpiod whose chip is being unregistered.
*/ static DEFINE_MUTEX(sysfs_lock);
/* * /sys/class/gpio/gpioN... only for GPIOs that are exported * /direction * * MAY BE OMITTED if kernel won't allow direction changes * * is read/write as "in" or "out" * * may also be written as "high" or "low", initializing * output value as specified ("out" implies "low") * /value * * always readable, subject to hardware behavior * * may be writable, as zero/nonzero * /edge * * configures behavior of poll(2) on /value * * available only if pin can generate IRQs on input * * is read/write as "none", "falling", "rising", or "both" * /active_low * * configures polarity of /value * * is read/write as zero/nonzero * * also affects existing and subsequent "falling" and "rising" * /edge configuration
*/
/* * FIXME: This should be done in the irq_request_resources callback * when the irq is requested, but a few drivers currently fail to do * so. * * Remove this redundant call (along with the corresponding unlock) * when those drivers have been fixed.
*/
ret = gpiochip_lock_as_irq(guard.gc, gpio_chip_hwgpio(desc)); if (ret < 0) goto err_clr_bits;
ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags, "gpiolib", data); if (ret < 0) goto err_unlock;
/* Caller holds gpiod-data mutex. */ staticint gpio_sysfs_set_active_low(struct gpiod_data *data, int value)
{ unsignedint flags = data->irq_flags; struct gpio_desc *desc = data->desc; int status = 0;
if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value) return 0;
assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
/* reconfigure poll(2) support if enabled on one edge only */ if (flags == GPIO_IRQF_TRIGGER_FALLING ||
flags == GPIO_IRQF_TRIGGER_RISING) {
gpio_sysfs_free_irq(data);
status = gpio_sysfs_request_irq(data, flags);
}
/* * No extra locking here; FLAG_SYSFS just signifies that the * request and export were done by on behalf of userspace, so * they may be undone on its behalf too.
*/
ret = gpiod_request_user(desc, "sysfs"); if (ret) return ret;
ret = gpiod_set_transitory(desc, false); if (ret) {
gpiod_free(desc); return ret;
}
ret = gpiod_export(desc, true); if (ret < 0) {
gpiod_free(desc);
} else {
set_bit(FLAG_SYSFS, &desc->flags);
gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);
}
return ret;
}
staticint unexport_gpio_desc(struct gpio_desc *desc)
{ /* * No extra locking here; FLAG_SYSFS just signifies that the * request and export were done by on behalf of userspace, so * they may be undone on its behalf too.
*/ if (!test_and_clear_bit(FLAG_SYSFS, &desc->flags)) return -EINVAL;
staticstruct gpiodev_data *
gdev_get_data(struct gpio_device *gdev) __must_hold(&sysfs_lock)
{ /* * Find the first device in GPIO class that matches. Whether that's * the one indexed by GPIO base or device ID doesn't matter, it has * the same address set as driver data.
*/ struct device *cdev __free(put_device) = class_find_device(&gpio_class,
NULL, gdev,
match_gdev); if (!cdev) return NULL;
/** * gpiod_export - export a GPIO through sysfs * @desc: GPIO to make available, already requested * @direction_may_change: true if userspace may change GPIO direction * Context: arch_initcall or later * * When drivers want to make a GPIO accessible to userspace after they * have requested it -- perhaps while debugging, or as part of their * public interface -- they may use this routine. If the GPIO can * change direction (some can't) and the caller allows it, userspace * will see "direction" sysfs attribute which may be used to change * the gpio's direction. A "value" attribute will always be provided. * * Returns: * 0 on success, or negative errno on failure.
*/ int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
{ char *path __free(kfree) = NULL; struct gpiodev_data *gdev_data; struct gpiod_data *desc_data; struct gpio_device *gdev; struct attribute **attrs; int status;
/* can't export until sysfs is available ... */ if (!class_is_registered(&gpio_class)) {
pr_debug("%s: called too early!\n", __func__); return -ENOENT;
}
/* * Note: we need to continue passing desc_data here as there's still * at least one known user of gpiod_export_link() in the tree. This * function still uses class_find_device() internally.
*/
desc_data->dev = device_create_with_groups(&gpio_class, &gdev->dev,
MKDEV(0, 0), desc_data,
desc_data->class_attr_groups, "gpio%u",
desc_to_gpio(desc)); if (IS_ERR(desc_data->dev)) {
status = PTR_ERR(desc_data->dev); goto err_free_data;
}
desc_data->value_kn = sysfs_get_dirent(desc_data->dev->kobj.sd, "value"); if (!desc_data->value_kn) {
status = -ENODEV; goto err_unregister_device;
} #endif/* CONFIG_GPIO_SYSFS_LEGACY */
gdev_data = gdev_get_data(gdev); if (!gdev_data) {
status = -ENODEV; goto err_put_dirent;
}
desc_data->chip_attr_group.name = kasprintf(GFP_KERNEL, "gpio%u",
gpio_chip_hwgpio(desc)); if (!desc_data->chip_attr_group.name) {
status = -ENOMEM; goto err_put_dirent;
}
/** * gpiod_export_link - create a sysfs link to an exported GPIO node * @dev: device under which to create symlink * @name: name of the symlink * @desc: GPIO to create symlink to, already exported * * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN * node. Caller is responsible for unlinking. * * Returns: * 0 on success, or negative errno on failure.
*/ int gpiod_export_link(struct device *dev, constchar *name, struct gpio_desc *desc)
{ #if IS_ENABLED(CONFIG_GPIO_SYSFS_LEGACY) struct device *cdev; int ret;
if (!desc) {
pr_warn("%s: invalid GPIO\n", __func__); return -EINVAL;
}
cdev = class_find_device(&gpio_class, NULL, desc, match_export); if (!cdev) return -ENODEV;
ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
put_device(cdev);
/* * Release irq after deregistration to prevent race with * edge_store.
*/ if (desc_data->irq_flags)
gpio_sysfs_free_irq(desc_data); #endif/* CONFIG_GPIO_SYSFS_LEGACY */
int gpiochip_sysfs_register(struct gpio_device *gdev)
{ struct gpiodev_data *data; struct gpio_chip *chip; struct device *parent; int err;
/* * Many systems add gpio chips for SOC support very early, * before driver model support is available. In those cases we * register later, in gpiolib_sysfs_init() ... here we just * verify that _some_ field of gpio_class got initialized.
*/ if (!class_is_registered(&gpio_class)) return 0;
guard(srcu)(&gdev->srcu);
chip = srcu_dereference(gdev->chip, &gdev->srcu); if (!chip) return -ENODEV;
/* * For sysfs backward compatibility we need to preserve this * preferred parenting to the gpio_chip parent field, if set.
*/ if (chip->parent)
parent = chip->parent; else
parent = &gdev->dev;
data = kmalloc(sizeof(*data), GFP_KERNEL); if (!data) return -ENOMEM;
chip = srcu_dereference(gdev->chip, &gdev->srcu); if (!chip) return;
/* unregister gpiod class devices owned by sysfs */
for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) {
gpiod_unexport(desc);
gpiod_free(desc);
}
}
/* * We're not really looking for a device - we just want to iterate over the * list and call this callback for each GPIO device. This is why this function * always returns 0.
*/ staticint gpiofind_sysfs_register(struct gpio_chip *gc, constvoid *data)
{ struct gpio_device *gdev = gc->gpiodev; int ret;
ret = gpiochip_sysfs_register(gdev); if (ret)
chip_err(gc, "failed to register the sysfs entry: %d\n", ret);
return 0;
}
staticint __init gpiolib_sysfs_init(void)
{ int status;
status = class_register(&gpio_class); if (status < 0) return status;
/* Scan and register the gpio_chips which registered very * early (e.g. before the class_register above was called). * * We run before arch_initcall() so chip->dev nodes can have * registered, and so arch_initcall() can always gpiod_export().
*/
(void)gpio_device_find(NULL, gpiofind_sysfs_register);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.