diff --git a/Documentation/gpio.txt b/Documentation/gpio.txt index e4b6985044a263249976cc0ca985529555007462..566edaa56a530899fecb85ca67b8815089661b20 100644 --- a/Documentation/gpio.txt +++ b/Documentation/gpio.txt @@ -555,6 +555,11 @@ requested using gpio_request(): /* reverse gpio_export() */ void gpio_unexport(); + /* create a sysfs link to an exported GPIO node */ + int gpio_export_link(struct device *dev, const char *name, + unsigned gpio) + + After a kernel driver requests a GPIO, it may only be made available in the sysfs interface by gpio_export(). The driver can control whether the signal direction may change. This helps drivers prevent userspace code @@ -563,3 +568,8 @@ from accidentally clobbering important system state. This explicit exporting can help with debugging (by making some kinds of experiments easier), or can provide an always-there interface that's suitable for documenting as part of a board support package. + +After the GPIO has been exported, gpio_export_link() allows creating +symlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can +use this to provide the interface under their own device in sysfs with +a descriptive name. diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c index 51a8d4103be53beb3dd90bd1328d89935cf6573c..aef6b3d8e2cf32f637cbdaeb2d4557f73e976361 100644 --- a/drivers/gpio/gpiolib.c +++ b/drivers/gpio/gpiolib.c @@ -504,6 +504,51 @@ static int match_export(struct device *dev, void *data) return dev_get_drvdata(dev) == data; } +/** + * gpio_export_link - create a sysfs link to an exported GPIO node + * @dev: device under which to create symlink + * @name: name of the symlink + * @gpio: 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 zero on success, else an error. + */ +int gpio_export_link(struct device *dev, const char *name, unsigned gpio) +{ + struct gpio_desc *desc; + int status = -EINVAL; + + if (!gpio_is_valid(gpio)) + goto done; + + mutex_lock(&sysfs_lock); + + desc = &gpio_desc[gpio]; + + if (test_bit(FLAG_EXPORT, &desc->flags)) { + struct device *tdev; + + tdev = class_find_device(&gpio_class, NULL, desc, match_export); + if (tdev != NULL) { + status = sysfs_create_link(&dev->kobj, &tdev->kobj, + name); + } else { + status = -ENODEV; + } + } + + mutex_unlock(&sysfs_lock); + +done: + if (status) + pr_debug("%s: gpio%d status %d\n", __func__, gpio, status); + + return status; +} +EXPORT_SYMBOL_GPL(gpio_export_link); + /** * gpio_unexport - reverse effect of gpio_export() * @gpio: gpio to make unavailable diff --git a/include/asm-generic/gpio.h b/include/asm-generic/gpio.h index d6c379dc64fac63797364c7e385dc91778df5ac4..9cca3785cab881c854236b00a37fa5e478f06a3a 100644 --- a/include/asm-generic/gpio.h +++ b/include/asm-generic/gpio.h @@ -141,6 +141,8 @@ extern int __gpio_to_irq(unsigned gpio); * but more typically is configured entirely from userspace. */ extern int gpio_export(unsigned gpio, bool direction_may_change); +extern int gpio_export_link(struct device *dev, const char *name, + unsigned gpio); extern void gpio_unexport(unsigned gpio); #endif /* CONFIG_GPIO_SYSFS */ @@ -185,6 +187,12 @@ static inline int gpio_export(unsigned gpio, bool direction_may_change) return -ENOSYS; } +static inline int gpio_export_link(struct device *dev, const char *name, + unsigned gpio) +{ + return -ENOSYS; +} + static inline void gpio_unexport(unsigned gpio) { } diff --git a/include/linux/gpio.h b/include/linux/gpio.h index e10c49a5b96e9ed300d670871af2edbaaf23add1..059bd189d35ddd1d1e6931b0aa4ddc2c95a9c9a3 100644 --- a/include/linux/gpio.h +++ b/include/linux/gpio.h @@ -12,6 +12,8 @@ #include #include +struct device; + /* * Some platforms don't support the GPIO programming interface. * @@ -89,6 +91,15 @@ static inline int gpio_export(unsigned gpio, bool direction_may_change) return -EINVAL; } +static inline int gpio_export_link(struct device *dev, const char *name, + unsigned gpio) +{ + /* GPIO can never have been exported */ + WARN_ON(1); + return -EINVAL; +} + + static inline void gpio_unexport(unsigned gpio) { /* GPIO can never have been exported */