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