xref: /openbmc/linux/drivers/gpio/gpiolib-sysfs.c (revision 53b2f353)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/bitops.h>
4 #include <linux/cleanup.h>
5 #include <linux/device.h>
6 #include <linux/idr.h>
7 #include <linux/init.h>
8 #include <linux/interrupt.h>
9 #include <linux/kdev_t.h>
10 #include <linux/kstrtox.h>
11 #include <linux/list.h>
12 #include <linux/mutex.h>
13 #include <linux/printk.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/string.h>
17 #include <linux/sysfs.h>
18 #include <linux/types.h>
19 
20 #include <linux/gpio/consumer.h>
21 #include <linux/gpio/driver.h>
22 
23 #include "gpiolib.h"
24 #include "gpiolib-sysfs.h"
25 
26 struct kernfs_node;
27 
28 #define GPIO_IRQF_TRIGGER_NONE		0
29 #define GPIO_IRQF_TRIGGER_FALLING	BIT(0)
30 #define GPIO_IRQF_TRIGGER_RISING	BIT(1)
31 #define GPIO_IRQF_TRIGGER_BOTH		(GPIO_IRQF_TRIGGER_FALLING | \
32 					 GPIO_IRQF_TRIGGER_RISING)
33 
34 struct gpiod_data {
35 	struct gpio_desc *desc;
36 
37 	struct mutex mutex;
38 	struct kernfs_node *value_kn;
39 	int irq;
40 	unsigned char irq_flags;
41 
42 	bool direction_can_change;
43 };
44 
45 /*
46  * Lock to serialise gpiod export and unexport, and prevent re-export of
47  * gpiod whose chip is being unregistered.
48  */
49 static DEFINE_MUTEX(sysfs_lock);
50 
51 /*
52  * /sys/class/gpio/gpioN... only for GPIOs that are exported
53  *   /direction
54  *      * MAY BE OMITTED if kernel won't allow direction changes
55  *      * is read/write as "in" or "out"
56  *      * may also be written as "high" or "low", initializing
57  *        output value as specified ("out" implies "low")
58  *   /value
59  *      * always readable, subject to hardware behavior
60  *      * may be writable, as zero/nonzero
61  *   /edge
62  *      * configures behavior of poll(2) on /value
63  *      * available only if pin can generate IRQs on input
64  *      * is read/write as "none", "falling", "rising", or "both"
65  *   /active_low
66  *      * configures polarity of /value
67  *      * is read/write as zero/nonzero
68  *      * also affects existing and subsequent "falling" and "rising"
69  *        /edge configuration
70  */
71 
direction_show(struct device * dev,struct device_attribute * attr,char * buf)72 static ssize_t direction_show(struct device *dev,
73 		struct device_attribute *attr, char *buf)
74 {
75 	struct gpiod_data *data = dev_get_drvdata(dev);
76 	struct gpio_desc *desc = data->desc;
77 	int value;
78 
79 	mutex_lock(&data->mutex);
80 
81 	gpiod_get_direction(desc);
82 	value = !!test_bit(FLAG_IS_OUT, &desc->flags);
83 
84 	mutex_unlock(&data->mutex);
85 
86 	return sysfs_emit(buf, "%s\n", value ? "out" : "in");
87 }
88 
direction_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)89 static ssize_t direction_store(struct device *dev,
90 		struct device_attribute *attr, const char *buf, size_t size)
91 {
92 	struct gpiod_data *data = dev_get_drvdata(dev);
93 	struct gpio_desc *desc = data->desc;
94 	ssize_t			status;
95 
96 	mutex_lock(&data->mutex);
97 
98 	if (sysfs_streq(buf, "high"))
99 		status = gpiod_direction_output_raw(desc, 1);
100 	else if (sysfs_streq(buf, "out") || sysfs_streq(buf, "low"))
101 		status = gpiod_direction_output_raw(desc, 0);
102 	else if (sysfs_streq(buf, "in"))
103 		status = gpiod_direction_input(desc);
104 	else
105 		status = -EINVAL;
106 
107 	mutex_unlock(&data->mutex);
108 
109 	return status ? : size;
110 }
111 static DEVICE_ATTR_RW(direction);
112 
value_show(struct device * dev,struct device_attribute * attr,char * buf)113 static ssize_t value_show(struct device *dev,
114 		struct device_attribute *attr, char *buf)
115 {
116 	struct gpiod_data *data = dev_get_drvdata(dev);
117 	struct gpio_desc *desc = data->desc;
118 	ssize_t			status;
119 
120 	mutex_lock(&data->mutex);
121 
122 	status = gpiod_get_value_cansleep(desc);
123 
124 	mutex_unlock(&data->mutex);
125 
126 	if (status < 0)
127 		return status;
128 
129 	return sysfs_emit(buf, "%zd\n", status);
130 }
131 
value_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)132 static ssize_t value_store(struct device *dev,
133 		struct device_attribute *attr, const char *buf, size_t size)
134 {
135 	struct gpiod_data *data = dev_get_drvdata(dev);
136 	struct gpio_desc *desc = data->desc;
137 	ssize_t status;
138 	long value;
139 
140 	status = kstrtol(buf, 0, &value);
141 
142 	mutex_lock(&data->mutex);
143 
144 	if (!test_bit(FLAG_IS_OUT, &desc->flags)) {
145 		status = -EPERM;
146 	} else if (status == 0) {
147 		gpiod_set_value_cansleep(desc, value);
148 		status = size;
149 	}
150 
151 	mutex_unlock(&data->mutex);
152 
153 	return status;
154 }
155 static DEVICE_ATTR_PREALLOC(value, S_IWUSR | S_IRUGO, value_show, value_store);
156 
gpio_sysfs_irq(int irq,void * priv)157 static irqreturn_t gpio_sysfs_irq(int irq, void *priv)
158 {
159 	struct gpiod_data *data = priv;
160 
161 	sysfs_notify_dirent(data->value_kn);
162 
163 	return IRQ_HANDLED;
164 }
165 
166 /* Caller holds gpiod-data mutex. */
gpio_sysfs_request_irq(struct device * dev,unsigned char flags)167 static int gpio_sysfs_request_irq(struct device *dev, unsigned char flags)
168 {
169 	struct gpiod_data	*data = dev_get_drvdata(dev);
170 	struct gpio_desc	*desc = data->desc;
171 	unsigned long		irq_flags;
172 	int			ret;
173 
174 	data->irq = gpiod_to_irq(desc);
175 	if (data->irq < 0)
176 		return -EIO;
177 
178 	data->value_kn = sysfs_get_dirent(dev->kobj.sd, "value");
179 	if (!data->value_kn)
180 		return -ENODEV;
181 
182 	irq_flags = IRQF_SHARED;
183 	if (flags & GPIO_IRQF_TRIGGER_FALLING)
184 		irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
185 			IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
186 	if (flags & GPIO_IRQF_TRIGGER_RISING)
187 		irq_flags |= test_bit(FLAG_ACTIVE_LOW, &desc->flags) ?
188 			IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
189 
190 	/*
191 	 * FIXME: This should be done in the irq_request_resources callback
192 	 *        when the irq is requested, but a few drivers currently fail
193 	 *        to do so.
194 	 *
195 	 *        Remove this redundant call (along with the corresponding
196 	 *        unlock) when those drivers have been fixed.
197 	 */
198 	ret = gpiochip_lock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
199 	if (ret < 0)
200 		goto err_put_kn;
201 
202 	ret = request_any_context_irq(data->irq, gpio_sysfs_irq, irq_flags,
203 				"gpiolib", data);
204 	if (ret < 0)
205 		goto err_unlock;
206 
207 	data->irq_flags = flags;
208 
209 	return 0;
210 
211 err_unlock:
212 	gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
213 err_put_kn:
214 	sysfs_put(data->value_kn);
215 
216 	return ret;
217 }
218 
219 /*
220  * Caller holds gpiod-data mutex (unless called after class-device
221  * deregistration).
222  */
gpio_sysfs_free_irq(struct device * dev)223 static void gpio_sysfs_free_irq(struct device *dev)
224 {
225 	struct gpiod_data *data = dev_get_drvdata(dev);
226 	struct gpio_desc *desc = data->desc;
227 
228 	data->irq_flags = 0;
229 	free_irq(data->irq, data);
230 	gpiochip_unlock_as_irq(desc->gdev->chip, gpio_chip_hwgpio(desc));
231 	sysfs_put(data->value_kn);
232 }
233 
234 static const char * const trigger_names[] = {
235 	[GPIO_IRQF_TRIGGER_NONE]	= "none",
236 	[GPIO_IRQF_TRIGGER_FALLING]	= "falling",
237 	[GPIO_IRQF_TRIGGER_RISING]	= "rising",
238 	[GPIO_IRQF_TRIGGER_BOTH]	= "both",
239 };
240 
edge_show(struct device * dev,struct device_attribute * attr,char * buf)241 static ssize_t edge_show(struct device *dev,
242 		struct device_attribute *attr, char *buf)
243 {
244 	struct gpiod_data *data = dev_get_drvdata(dev);
245 	int flags;
246 
247 	mutex_lock(&data->mutex);
248 
249 	flags = data->irq_flags;
250 
251 	mutex_unlock(&data->mutex);
252 
253 	if (flags >= ARRAY_SIZE(trigger_names))
254 		return 0;
255 
256 	return sysfs_emit(buf, "%s\n", trigger_names[flags]);
257 }
258 
edge_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)259 static ssize_t edge_store(struct device *dev,
260 		struct device_attribute *attr, const char *buf, size_t size)
261 {
262 	struct gpiod_data *data = dev_get_drvdata(dev);
263 	ssize_t	status = size;
264 	int flags;
265 
266 	flags = sysfs_match_string(trigger_names, buf);
267 	if (flags < 0)
268 		return flags;
269 
270 	mutex_lock(&data->mutex);
271 
272 	if (flags == data->irq_flags) {
273 		status = size;
274 		goto out_unlock;
275 	}
276 
277 	if (data->irq_flags)
278 		gpio_sysfs_free_irq(dev);
279 
280 	if (flags) {
281 		status = gpio_sysfs_request_irq(dev, flags);
282 		if (!status)
283 			status = size;
284 	}
285 
286 out_unlock:
287 	mutex_unlock(&data->mutex);
288 
289 	return status;
290 }
291 static DEVICE_ATTR_RW(edge);
292 
293 /* Caller holds gpiod-data mutex. */
gpio_sysfs_set_active_low(struct device * dev,int value)294 static int gpio_sysfs_set_active_low(struct device *dev, int value)
295 {
296 	struct gpiod_data	*data = dev_get_drvdata(dev);
297 	struct gpio_desc	*desc = data->desc;
298 	int			status = 0;
299 	unsigned int		flags = data->irq_flags;
300 
301 	if (!!test_bit(FLAG_ACTIVE_LOW, &desc->flags) == !!value)
302 		return 0;
303 
304 	assign_bit(FLAG_ACTIVE_LOW, &desc->flags, value);
305 
306 	/* reconfigure poll(2) support if enabled on one edge only */
307 	if (flags == GPIO_IRQF_TRIGGER_FALLING ||
308 					flags == GPIO_IRQF_TRIGGER_RISING) {
309 		gpio_sysfs_free_irq(dev);
310 		status = gpio_sysfs_request_irq(dev, flags);
311 	}
312 
313 	return status;
314 }
315 
active_low_show(struct device * dev,struct device_attribute * attr,char * buf)316 static ssize_t active_low_show(struct device *dev,
317 		struct device_attribute *attr, char *buf)
318 {
319 	struct gpiod_data *data = dev_get_drvdata(dev);
320 	struct gpio_desc *desc = data->desc;
321 	int value;
322 
323 	mutex_lock(&data->mutex);
324 
325 	value = !!test_bit(FLAG_ACTIVE_LOW, &desc->flags);
326 
327 	mutex_unlock(&data->mutex);
328 
329 	return sysfs_emit(buf, "%d\n", value);
330 }
331 
active_low_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)332 static ssize_t active_low_store(struct device *dev,
333 		struct device_attribute *attr, const char *buf, size_t size)
334 {
335 	struct gpiod_data	*data = dev_get_drvdata(dev);
336 	ssize_t			status;
337 	long			value;
338 
339 	status = kstrtol(buf, 0, &value);
340 	if (status)
341 		return status;
342 
343 	mutex_lock(&data->mutex);
344 
345 	status = gpio_sysfs_set_active_low(dev, value);
346 
347 	mutex_unlock(&data->mutex);
348 
349 	return status ? : size;
350 }
351 static DEVICE_ATTR_RW(active_low);
352 
gpio_is_visible(struct kobject * kobj,struct attribute * attr,int n)353 static umode_t gpio_is_visible(struct kobject *kobj, struct attribute *attr,
354 			       int n)
355 {
356 	struct device *dev = kobj_to_dev(kobj);
357 	struct gpiod_data *data = dev_get_drvdata(dev);
358 	struct gpio_desc *desc = data->desc;
359 	umode_t mode = attr->mode;
360 	bool show_direction = data->direction_can_change;
361 
362 	if (attr == &dev_attr_direction.attr) {
363 		if (!show_direction)
364 			mode = 0;
365 	} else if (attr == &dev_attr_edge.attr) {
366 		if (gpiod_to_irq(desc) < 0)
367 			mode = 0;
368 		if (!show_direction && test_bit(FLAG_IS_OUT, &desc->flags))
369 			mode = 0;
370 	}
371 
372 	return mode;
373 }
374 
375 static struct attribute *gpio_attrs[] = {
376 	&dev_attr_direction.attr,
377 	&dev_attr_edge.attr,
378 	&dev_attr_value.attr,
379 	&dev_attr_active_low.attr,
380 	NULL,
381 };
382 
383 static const struct attribute_group gpio_group = {
384 	.attrs = gpio_attrs,
385 	.is_visible = gpio_is_visible,
386 };
387 
388 static const struct attribute_group *gpio_groups[] = {
389 	&gpio_group,
390 	NULL
391 };
392 
393 /*
394  * /sys/class/gpio/gpiochipN/
395  *   /base ... matching gpio_chip.base (N)
396  *   /label ... matching gpio_chip.label
397  *   /ngpio ... matching gpio_chip.ngpio
398  */
399 
base_show(struct device * dev,struct device_attribute * attr,char * buf)400 static ssize_t base_show(struct device *dev,
401 			       struct device_attribute *attr, char *buf)
402 {
403 	const struct gpio_chip	*chip = dev_get_drvdata(dev);
404 
405 	return sysfs_emit(buf, "%d\n", chip->base);
406 }
407 static DEVICE_ATTR_RO(base);
408 
label_show(struct device * dev,struct device_attribute * attr,char * buf)409 static ssize_t label_show(struct device *dev,
410 			       struct device_attribute *attr, char *buf)
411 {
412 	const struct gpio_chip	*chip = dev_get_drvdata(dev);
413 
414 	return sysfs_emit(buf, "%s\n", chip->label ?: "");
415 }
416 static DEVICE_ATTR_RO(label);
417 
ngpio_show(struct device * dev,struct device_attribute * attr,char * buf)418 static ssize_t ngpio_show(struct device *dev,
419 			       struct device_attribute *attr, char *buf)
420 {
421 	const struct gpio_chip	*chip = dev_get_drvdata(dev);
422 
423 	return sysfs_emit(buf, "%u\n", chip->ngpio);
424 }
425 static DEVICE_ATTR_RO(ngpio);
426 
427 static struct attribute *gpiochip_attrs[] = {
428 	&dev_attr_base.attr,
429 	&dev_attr_label.attr,
430 	&dev_attr_ngpio.attr,
431 	NULL,
432 };
433 ATTRIBUTE_GROUPS(gpiochip);
434 
435 /*
436  * /sys/class/gpio/export ... write-only
437  *	integer N ... number of GPIO to export (full access)
438  * /sys/class/gpio/unexport ... write-only
439  *	integer N ... number of GPIO to unexport
440  */
export_store(const struct class * class,const struct class_attribute * attr,const char * buf,size_t len)441 static ssize_t export_store(const struct class *class,
442 				const struct class_attribute *attr,
443 				const char *buf, size_t len)
444 {
445 	long			gpio;
446 	struct gpio_desc	*desc;
447 	int			status;
448 	struct gpio_chip	*gc;
449 	int			offset;
450 
451 	status = kstrtol(buf, 0, &gpio);
452 	if (status < 0)
453 		goto done;
454 
455 	desc = gpio_to_desc(gpio);
456 	/* reject invalid GPIOs */
457 	if (!desc) {
458 		pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
459 		return -EINVAL;
460 	}
461 	gc = desc->gdev->chip;
462 	offset = gpio_chip_hwgpio(desc);
463 	if (!gpiochip_line_is_valid(gc, offset)) {
464 		pr_warn("%s: GPIO %ld masked\n", __func__, gpio);
465 		return -EINVAL;
466 	}
467 
468 	/* No extra locking here; FLAG_SYSFS just signifies that the
469 	 * request and export were done by on behalf of userspace, so
470 	 * they may be undone on its behalf too.
471 	 */
472 
473 	status = gpiod_request_user(desc, "sysfs");
474 	if (status)
475 		goto done;
476 
477 	status = gpiod_set_transitory(desc, false);
478 	if (status) {
479 		gpiod_free(desc);
480 		goto done;
481 	}
482 
483 	status = gpiod_export(desc, true);
484 	if (status < 0)
485 		gpiod_free(desc);
486 	else
487 		set_bit(FLAG_SYSFS, &desc->flags);
488 
489 done:
490 	if (status)
491 		pr_debug("%s: status %d\n", __func__, status);
492 	return status ? : len;
493 }
494 static CLASS_ATTR_WO(export);
495 
unexport_store(const struct class * class,const struct class_attribute * attr,const char * buf,size_t len)496 static ssize_t unexport_store(const struct class *class,
497 				const struct class_attribute *attr,
498 				const char *buf, size_t len)
499 {
500 	long			gpio;
501 	struct gpio_desc	*desc;
502 	int			status;
503 
504 	status = kstrtol(buf, 0, &gpio);
505 	if (status < 0)
506 		goto done;
507 
508 	desc = gpio_to_desc(gpio);
509 	/* reject bogus commands (gpiod_unexport() ignores them) */
510 	if (!desc) {
511 		pr_warn("%s: invalid GPIO %ld\n", __func__, gpio);
512 		return -EINVAL;
513 	}
514 
515 	status = -EINVAL;
516 
517 	/* No extra locking here; FLAG_SYSFS just signifies that the
518 	 * request and export were done by on behalf of userspace, so
519 	 * they may be undone on its behalf too.
520 	 */
521 	if (test_and_clear_bit(FLAG_SYSFS, &desc->flags)) {
522 		gpiod_unexport(desc);
523 		gpiod_free(desc);
524 		status = 0;
525 	}
526 done:
527 	if (status)
528 		pr_debug("%s: status %d\n", __func__, status);
529 	return status ? : len;
530 }
531 static CLASS_ATTR_WO(unexport);
532 
533 static struct attribute *gpio_class_attrs[] = {
534 	&class_attr_export.attr,
535 	&class_attr_unexport.attr,
536 	NULL,
537 };
538 ATTRIBUTE_GROUPS(gpio_class);
539 
540 static struct class gpio_class = {
541 	.name =		"gpio",
542 	.class_groups = gpio_class_groups,
543 };
544 
545 
546 /**
547  * gpiod_export - export a GPIO through sysfs
548  * @desc: GPIO to make available, already requested
549  * @direction_may_change: true if userspace may change GPIO direction
550  * Context: arch_initcall or later
551  *
552  * When drivers want to make a GPIO accessible to userspace after they
553  * have requested it -- perhaps while debugging, or as part of their
554  * public interface -- they may use this routine.  If the GPIO can
555  * change direction (some can't) and the caller allows it, userspace
556  * will see "direction" sysfs attribute which may be used to change
557  * the gpio's direction.  A "value" attribute will always be provided.
558  *
559  * Returns zero on success, else an error.
560  */
gpiod_export(struct gpio_desc * desc,bool direction_may_change)561 int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
562 {
563 	struct gpio_chip	*chip;
564 	struct gpio_device	*gdev;
565 	struct gpiod_data	*data;
566 	unsigned long		flags;
567 	int			status;
568 	const char		*ioname = NULL;
569 	struct device		*dev;
570 	int			offset;
571 
572 	/* can't export until sysfs is available ... */
573 	if (!class_is_registered(&gpio_class)) {
574 		pr_debug("%s: called too early!\n", __func__);
575 		return -ENOENT;
576 	}
577 
578 	if (!desc) {
579 		pr_debug("%s: invalid gpio descriptor\n", __func__);
580 		return -EINVAL;
581 	}
582 
583 	gdev = desc->gdev;
584 	chip = gdev->chip;
585 
586 	mutex_lock(&sysfs_lock);
587 
588 	/* check if chip is being removed */
589 	if (!chip || !gdev->mockdev) {
590 		status = -ENODEV;
591 		goto err_unlock;
592 	}
593 
594 	spin_lock_irqsave(&gpio_lock, flags);
595 	if (!test_bit(FLAG_REQUESTED, &desc->flags) ||
596 	     test_bit(FLAG_EXPORT, &desc->flags)) {
597 		spin_unlock_irqrestore(&gpio_lock, flags);
598 		gpiod_dbg(desc, "%s: unavailable (requested=%d, exported=%d)\n",
599 				__func__,
600 				test_bit(FLAG_REQUESTED, &desc->flags),
601 				test_bit(FLAG_EXPORT, &desc->flags));
602 		status = -EPERM;
603 		goto err_unlock;
604 	}
605 	spin_unlock_irqrestore(&gpio_lock, flags);
606 
607 	data = kzalloc(sizeof(*data), GFP_KERNEL);
608 	if (!data) {
609 		status = -ENOMEM;
610 		goto err_unlock;
611 	}
612 
613 	data->desc = desc;
614 	mutex_init(&data->mutex);
615 	if (chip->direction_input && chip->direction_output)
616 		data->direction_can_change = direction_may_change;
617 	else
618 		data->direction_can_change = false;
619 
620 	offset = gpio_chip_hwgpio(desc);
621 	if (chip->names && chip->names[offset])
622 		ioname = chip->names[offset];
623 
624 	dev = device_create_with_groups(&gpio_class, &gdev->dev,
625 					MKDEV(0, 0), data, gpio_groups,
626 					ioname ? ioname : "gpio%u",
627 					desc_to_gpio(desc));
628 	if (IS_ERR(dev)) {
629 		status = PTR_ERR(dev);
630 		goto err_free_data;
631 	}
632 
633 	set_bit(FLAG_EXPORT, &desc->flags);
634 	mutex_unlock(&sysfs_lock);
635 	return 0;
636 
637 err_free_data:
638 	kfree(data);
639 err_unlock:
640 	mutex_unlock(&sysfs_lock);
641 	gpiod_dbg(desc, "%s: status %d\n", __func__, status);
642 	return status;
643 }
644 EXPORT_SYMBOL_GPL(gpiod_export);
645 
match_export(struct device * dev,const void * desc)646 static int match_export(struct device *dev, const void *desc)
647 {
648 	struct gpiod_data *data = dev_get_drvdata(dev);
649 
650 	return data->desc == desc;
651 }
652 
653 /**
654  * gpiod_export_link - create a sysfs link to an exported GPIO node
655  * @dev: device under which to create symlink
656  * @name: name of the symlink
657  * @desc: GPIO to create symlink to, already exported
658  *
659  * Set up a symlink from /sys/.../dev/name to /sys/class/gpio/gpioN
660  * node. Caller is responsible for unlinking.
661  *
662  * Returns zero on success, else an error.
663  */
gpiod_export_link(struct device * dev,const char * name,struct gpio_desc * desc)664 int gpiod_export_link(struct device *dev, const char *name,
665 		      struct gpio_desc *desc)
666 {
667 	struct device *cdev;
668 	int ret;
669 
670 	if (!desc) {
671 		pr_warn("%s: invalid GPIO\n", __func__);
672 		return -EINVAL;
673 	}
674 
675 	cdev = class_find_device(&gpio_class, NULL, desc, match_export);
676 	if (!cdev)
677 		return -ENODEV;
678 
679 	ret = sysfs_create_link(&dev->kobj, &cdev->kobj, name);
680 	put_device(cdev);
681 
682 	return ret;
683 }
684 EXPORT_SYMBOL_GPL(gpiod_export_link);
685 
686 /**
687  * gpiod_unexport - reverse effect of gpiod_export()
688  * @desc: GPIO to make unavailable
689  *
690  * This is implicit on gpiod_free().
691  */
gpiod_unexport(struct gpio_desc * desc)692 void gpiod_unexport(struct gpio_desc *desc)
693 {
694 	struct gpiod_data *data;
695 	struct device *dev;
696 
697 	if (!desc) {
698 		pr_warn("%s: invalid GPIO\n", __func__);
699 		return;
700 	}
701 
702 	mutex_lock(&sysfs_lock);
703 
704 	if (!test_bit(FLAG_EXPORT, &desc->flags))
705 		goto err_unlock;
706 
707 	dev = class_find_device(&gpio_class, NULL, desc, match_export);
708 	if (!dev)
709 		goto err_unlock;
710 
711 	data = dev_get_drvdata(dev);
712 
713 	clear_bit(FLAG_EXPORT, &desc->flags);
714 
715 	device_unregister(dev);
716 
717 	/*
718 	 * Release irq after deregistration to prevent race with edge_store.
719 	 */
720 	if (data->irq_flags)
721 		gpio_sysfs_free_irq(dev);
722 
723 	mutex_unlock(&sysfs_lock);
724 
725 	put_device(dev);
726 	kfree(data);
727 
728 	return;
729 
730 err_unlock:
731 	mutex_unlock(&sysfs_lock);
732 }
733 EXPORT_SYMBOL_GPL(gpiod_unexport);
734 
gpiochip_sysfs_register(struct gpio_device * gdev)735 int gpiochip_sysfs_register(struct gpio_device *gdev)
736 {
737 	struct device	*dev;
738 	struct device	*parent;
739 	struct gpio_chip *chip = gdev->chip;
740 
741 	/*
742 	 * Many systems add gpio chips for SOC support very early,
743 	 * before driver model support is available.  In those cases we
744 	 * register later, in gpiolib_sysfs_init() ... here we just
745 	 * verify that _some_ field of gpio_class got initialized.
746 	 */
747 	if (!class_is_registered(&gpio_class))
748 		return 0;
749 
750 	/*
751 	 * For sysfs backward compatibility we need to preserve this
752 	 * preferred parenting to the gpio_chip parent field, if set.
753 	 */
754 	if (chip->parent)
755 		parent = chip->parent;
756 	else
757 		parent = &gdev->dev;
758 
759 	/* use chip->base for the ID; it's already known to be unique */
760 	dev = device_create_with_groups(&gpio_class, parent, MKDEV(0, 0), chip,
761 					gpiochip_groups, GPIOCHIP_NAME "%d",
762 					chip->base);
763 	if (IS_ERR(dev))
764 		return PTR_ERR(dev);
765 
766 	mutex_lock(&sysfs_lock);
767 	gdev->mockdev = dev;
768 	mutex_unlock(&sysfs_lock);
769 
770 	return 0;
771 }
772 
gpiochip_sysfs_unregister(struct gpio_device * gdev)773 void gpiochip_sysfs_unregister(struct gpio_device *gdev)
774 {
775 	struct gpio_desc *desc;
776 	struct gpio_chip *chip = gdev->chip;
777 
778 	scoped_guard(mutex, &sysfs_lock) {
779 		if (!gdev->mockdev)
780 			return;
781 
782 		device_unregister(gdev->mockdev);
783 
784 		/* prevent further gpiod exports */
785 		gdev->mockdev = NULL;
786 	}
787 
788 	/* unregister gpiod class devices owned by sysfs */
789 	for_each_gpio_desc_with_flag(chip, desc, FLAG_SYSFS) {
790 		gpiod_unexport(desc);
791 		gpiod_free(desc);
792 	}
793 }
794 
gpiolib_sysfs_init(void)795 static int __init gpiolib_sysfs_init(void)
796 {
797 	int		status;
798 	unsigned long	flags;
799 	struct gpio_device *gdev;
800 
801 	status = class_register(&gpio_class);
802 	if (status < 0)
803 		return status;
804 
805 	/* Scan and register the gpio_chips which registered very
806 	 * early (e.g. before the class_register above was called).
807 	 *
808 	 * We run before arch_initcall() so chip->dev nodes can have
809 	 * registered, and so arch_initcall() can always gpiod_export().
810 	 */
811 	spin_lock_irqsave(&gpio_lock, flags);
812 	list_for_each_entry(gdev, &gpio_devices, list) {
813 		if (gdev->mockdev)
814 			continue;
815 
816 		/*
817 		 * TODO we yield gpio_lock here because
818 		 * gpiochip_sysfs_register() acquires a mutex. This is unsafe
819 		 * and needs to be fixed.
820 		 *
821 		 * Also it would be nice to use gpio_device_find() here so we
822 		 * can keep gpio_chips local to gpiolib.c, but the yield of
823 		 * gpio_lock prevents us from doing this.
824 		 */
825 		spin_unlock_irqrestore(&gpio_lock, flags);
826 		status = gpiochip_sysfs_register(gdev);
827 		spin_lock_irqsave(&gpio_lock, flags);
828 	}
829 	spin_unlock_irqrestore(&gpio_lock, flags);
830 
831 	return status;
832 }
833 postcore_initcall(gpiolib_sysfs_init);
834