gpiolib.c 112 KB
Newer Older
1
#include <linux/bitmap.h>
2
3
#include <linux/kernel.h>
#include <linux/module.h>
4
#include <linux/interrupt.h>
5
6
#include <linux/irq.h>
#include <linux/spinlock.h>
7
#include <linux/list.h>
David Brownell's avatar
David Brownell committed
8
9
10
11
12
#include <linux/device.h>
#include <linux/err.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <linux/gpio.h>
13
#include <linux/of_gpio.h>
14
#include <linux/idr.h>
15
#include <linux/slab.h>
16
#include <linux/acpi.h>
17
#include <linux/gpio/driver.h>
18
#include <linux/gpio/machine.h>
19
#include <linux/pinctrl/consumer.h>
20
21
22
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
23
#include <linux/compat.h>
24
#include <linux/anon_inodes.h>
25
#include <linux/file.h>
26
27
28
#include <linux/kfifo.h>
#include <linux/poll.h>
#include <linux/timekeeping.h>
29
#include <uapi/linux/gpio.h>
30

31
32
#include "gpiolib.h"

33
34
#define CREATE_TRACE_POINTS
#include <trace/events/gpio.h>
35

36
/* Implementation infrastructure for GPIO interfaces.
37
 *
38
39
40
 * The GPIO programming interface allows for inlining speed-critical
 * get/set operations for common cases, so that access to SOC-integrated
 * GPIOs can sometimes cost only an instruction or two per bit.
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 */


/* When debugging, extend minimal trust to callers and platform code.
 * Also emit diagnostic messages that may help initial bringup, when
 * board setup or driver bugs are most common.
 *
 * Otherwise, minimize overhead in what may be bitbanging codepaths.
 */
#ifdef	DEBUG
#define	extra_checks	1
#else
#define	extra_checks	0
#endif

56
57
/* Device and char device-related information */
static DEFINE_IDA(gpio_ida);
58
59
60
61
62
static dev_t gpio_devt;
#define GPIO_DEV_MAX 256 /* 256 GPIO chip devices supported */
static struct bus_type gpio_bus_type = {
	.name = "gpio",
};
63

64
65
66
67
/* gpio_lock prevents conflicts during gpio_desc[] table updates.
 * While any GPIO is requested, its gpio_chip is not removable;
 * each GPIO's "requested" flag serves as a lock and refcount.
 */
68
DEFINE_SPINLOCK(gpio_lock);
69

70
71
static DEFINE_MUTEX(gpio_lookup_lock);
static LIST_HEAD(gpio_lookup_list);
72
LIST_HEAD(gpio_devices);
73
74

static void gpiochip_free_hogs(struct gpio_chip *chip);
75
static int gpiochip_add_irqchip(struct gpio_chip *gpiochip,
76
77
				struct lock_class_key *lock_key,
				struct lock_class_key *request_key);
78
static void gpiochip_irqchip_remove(struct gpio_chip *gpiochip);
79
80
static int gpiochip_irqchip_init_valid_mask(struct gpio_chip *gpiochip);
static void gpiochip_irqchip_free_valid_mask(struct gpio_chip *gpiochip);
81

82
static bool gpiolib_initialized;
83

84
85
86
87
88
static inline void desc_set_label(struct gpio_desc *d, const char *label)
{
	d->label = label;
}

89
/**
Thierry Reding's avatar
Thierry Reding committed
90
91
92
93
94
95
 * gpio_to_desc - Convert a GPIO number to its descriptor
 * @gpio: global GPIO number
 *
 * Returns:
 * The GPIO descriptor associated with the given GPIO, or %NULL if no GPIO
 * with the given number exists in the system.
96
 */
97
struct gpio_desc *gpio_to_desc(unsigned gpio)
98
{
99
	struct gpio_device *gdev;
100
101
102
103
	unsigned long flags;

	spin_lock_irqsave(&gpio_lock, flags);

104
	list_for_each_entry(gdev, &gpio_devices, list) {
105
106
		if (gdev->base <= gpio &&
		    gdev->base + gdev->ngpio > gpio) {
107
			spin_unlock_irqrestore(&gpio_lock, flags);
108
			return &gdev->descs[gpio - gdev->base];
109
110
111
112
113
		}
	}

	spin_unlock_irqrestore(&gpio_lock, flags);

114
115
116
	if (!gpio_is_valid(gpio))
		WARN(1, "invalid GPIO %d\n", gpio);

117
	return NULL;
118
}
119
EXPORT_SYMBOL_GPL(gpio_to_desc);
120

121
/**
Thierry Reding's avatar
Thierry Reding committed
122
123
124
125
126
127
128
129
 * gpiochip_get_desc - get the GPIO descriptor corresponding to the given
 *                     hardware number for this chip
 * @chip: GPIO chip
 * @hwnum: hardware number of the GPIO for this chip
 *
 * Returns:
 * A pointer to the GPIO descriptor or %ERR_PTR(-EINVAL) if no GPIO exists
 * in the given chip for the specified hardware number.
130
 */
131
132
struct gpio_desc *gpiochip_get_desc(struct gpio_chip *chip,
				    u16 hwnum)
133
{
134
135
136
	struct gpio_device *gdev = chip->gpiodev;

	if (hwnum >= gdev->ngpio)
137
		return ERR_PTR(-EINVAL);
138

139
	return &gdev->descs[hwnum];
140
}
141
142

/**
Thierry Reding's avatar
Thierry Reding committed
143
144
145
 * desc_to_gpio - convert a GPIO descriptor to the integer namespace
 * @desc: GPIO descriptor
 *
146
 * This should disappear in the future but is needed since we still
Thierry Reding's avatar
Thierry Reding committed
147
148
149
150
 * use GPIO numbers for error messages and sysfs nodes.
 *
 * Returns:
 * The global GPIO number for the GPIO specified by its descriptor.
151
 */
152
int desc_to_gpio(const struct gpio_desc *desc)
153
{
154
	return desc->gdev->base + (desc - &desc->gdev->descs[0]);
155
}
156
EXPORT_SYMBOL_GPL(desc_to_gpio);
157
158


159
160
161
162
163
/**
 * gpiod_to_chip - Return the GPIO chip to which a GPIO descriptor belongs
 * @desc:	descriptor to return the chip of
 */
struct gpio_chip *gpiod_to_chip(const struct gpio_desc *desc)
164
{
165
	if (!desc || !desc->gdev)
166
167
		return NULL;
	return desc->gdev->chip;
168
}
169
EXPORT_SYMBOL_GPL(gpiod_to_chip);
170

171
172
173
/* dynamic allocation of GPIOs, e.g. on a hotplugged device */
static int gpiochip_find_base(int ngpio)
{
174
	struct gpio_device *gdev;
175
	int base = ARCH_NR_GPIOS - ngpio;
176

177
	list_for_each_entry_reverse(gdev, &gpio_devices, list) {
178
		/* found a free space? */
179
		if (gdev->base + gdev->ngpio <= base)
180
181
182
			break;
		else
			/* nope, check the space right before the chip */
183
			base = gdev->base - ngpio;
184
185
	}

186
	if (gpio_is_valid(base)) {
187
		pr_debug("%s: found new base at %d\n", __func__, base);
188
189
190
191
		return base;
	} else {
		pr_err("%s: cannot find free range\n", __func__);
		return -ENOSPC;
Anton Vorontsov's avatar
Anton Vorontsov committed
192
193
194
	}
}

195
196
197
198
/**
 * gpiod_get_direction - return the current direction of a GPIO
 * @desc:	GPIO to get the direction of
 *
199
 * Returns 0 for output, 1 for input, or an error code in case of error.
200
201
202
 *
 * This function may sleep if gpiod_cansleep() is true.
 */
203
int gpiod_get_direction(struct gpio_desc *desc)
204
205
{
	struct gpio_chip	*chip;
206
	unsigned		offset;
207
208
	int			status = -EINVAL;

209
210
	chip = gpiod_to_chip(desc);
	offset = gpio_chip_hwgpio(desc);
211
212
213
214

	if (!chip->get_direction)
		return status;

215
	status = chip->get_direction(chip, offset);
216
217
218
	if (status > 0) {
		/* GPIOF_DIR_IN, or other positive */
		status = 1;
219
		clear_bit(FLAG_IS_OUT, &desc->flags);
220
221
222
	}
	if (status == 0) {
		/* GPIOF_DIR_OUT */
223
		set_bit(FLAG_IS_OUT, &desc->flags);
224
225
226
	}
	return status;
}
227
EXPORT_SYMBOL_GPL(gpiod_get_direction);
228

229
230
/*
 * Add a new chip to the global chips list, keeping the list of chips sorted
231
 * by range(means [base, base + ngpio - 1]) order.
232
233
234
235
 *
 * Return -EBUSY if the new chip overlaps with some other chip's integer
 * space.
 */
236
static int gpiodev_add_to_list(struct gpio_device *gdev)
237
{
238
	struct gpio_device *prev, *next;
239

240
	if (list_empty(&gpio_devices)) {
241
		/* initial entry in list */
242
		list_add_tail(&gdev->list, &gpio_devices);
243
		return 0;
244
245
	}

246
247
248
249
250
	next = list_entry(gpio_devices.next, struct gpio_device, list);
	if (gdev->base + gdev->ngpio <= next->base) {
		/* add before first entry */
		list_add(&gdev->list, &gpio_devices);
		return 0;
251
252
	}

253
254
255
256
	prev = list_entry(gpio_devices.prev, struct gpio_device, list);
	if (prev->base + prev->ngpio <= gdev->base) {
		/* add behind last entry */
		list_add_tail(&gdev->list, &gpio_devices);
257
		return 0;
258
259
	}

260
261
262
263
	list_for_each_entry_safe(prev, next, &gpio_devices, list) {
		/* at the end of the list */
		if (&next->list == &gpio_devices)
			break;
264

265
266
267
268
269
270
271
272
273
274
		/* add between prev and next */
		if (prev->base + prev->ngpio <= gdev->base
				&& gdev->base + gdev->ngpio <= next->base) {
			list_add(&gdev->list, &prev->list);
			return 0;
		}
	}

	dev_err(&gdev->dev, "GPIO integer space overlap, cannot add chip\n");
	return -EBUSY;
275
276
}

Thierry Reding's avatar
Thierry Reding committed
277
/*
278
279
280
281
 * Convert a GPIO name to its descriptor
 */
static struct gpio_desc *gpio_name_to_desc(const char * const name)
{
282
	struct gpio_device *gdev;
283
284
285
286
	unsigned long flags;

	spin_lock_irqsave(&gpio_lock, flags);

287
	list_for_each_entry(gdev, &gpio_devices, list) {
288
289
		int i;

290
291
		for (i = 0; i != gdev->ngpio; ++i) {
			struct gpio_desc *desc = &gdev->descs[i];
292

293
			if (!desc->name || !name)
294
295
				continue;

296
			if (!strcmp(desc->name, name)) {
297
				spin_unlock_irqrestore(&gpio_lock, flags);
298
				return desc;
299
300
301
302
303
304
305
306
307
			}
		}
	}

	spin_unlock_irqrestore(&gpio_lock, flags);

	return NULL;
}

308
309
310
311
/*
 * Takes the names from gc->names and checks if they are all unique. If they
 * are, they are assigned to their gpio descriptors.
 *
312
 * Warning if one of the names is already used for a different GPIO.
313
314
315
 */
static int gpiochip_set_desc_names(struct gpio_chip *gc)
{
316
	struct gpio_device *gdev = gc->gpiodev;
317
318
319
320
321
322
323
324
325
326
	int i;

	if (!gc->names)
		return 0;

	/* First check all names if they are unique */
	for (i = 0; i != gc->ngpio; ++i) {
		struct gpio_desc *gpio;

		gpio = gpio_name_to_desc(gc->names[i]);
327
		if (gpio)
328
			dev_warn(&gdev->dev,
329
				 "Detected name collision for GPIO name '%s'\n",
330
				 gc->names[i]);
331
332
333
334
	}

	/* Then add all names to the GPIO descriptors */
	for (i = 0; i != gc->ngpio; ++i)
335
		gdev->descs[i].name = gc->names[i];
336
337
338
339

	return 0;
}

340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
 * GPIO line handle management
 */

/**
 * struct linehandle_state - contains the state of a userspace handle
 * @gdev: the GPIO device the handle pertains to
 * @label: consumer label used to tag descriptors
 * @descs: the GPIO descriptors held by this handle
 * @numdescs: the number of descriptors held in the descs array
 */
struct linehandle_state {
	struct gpio_device *gdev;
	const char *label;
	struct gpio_desc *descs[GPIOHANDLES_MAX];
	u32 numdescs;
};

358
359
360
361
362
363
364
#define GPIOHANDLE_REQUEST_VALID_FLAGS \
	(GPIOHANDLE_REQUEST_INPUT | \
	GPIOHANDLE_REQUEST_OUTPUT | \
	GPIOHANDLE_REQUEST_ACTIVE_LOW | \
	GPIOHANDLE_REQUEST_OPEN_DRAIN | \
	GPIOHANDLE_REQUEST_OPEN_SOURCE)

365
366
367
368
369
370
static long linehandle_ioctl(struct file *filep, unsigned int cmd,
			     unsigned long arg)
{
	struct linehandle_state *lh = filep->private_data;
	void __user *ip = (void __user *)arg;
	struct gpiohandle_data ghd;
371
	int vals[GPIOHANDLES_MAX];
372
373
374
	int i;

	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
375
376
377
378
379
380
381
382
		/* TODO: check if descriptors are really input */
		int ret = gpiod_get_array_value_complex(false,
							true,
							lh->numdescs,
							lh->descs,
							vals);
		if (ret)
			return ret;
383

384
		memset(&ghd, 0, sizeof(ghd));
385
386
		for (i = 0; i < lh->numdescs; i++)
			ghd.values[i] = vals[i];
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447

		if (copy_to_user(ip, &ghd, sizeof(ghd)))
			return -EFAULT;

		return 0;
	} else if (cmd == GPIOHANDLE_SET_LINE_VALUES_IOCTL) {
		/* TODO: check if descriptors are really output */
		if (copy_from_user(&ghd, ip, sizeof(ghd)))
			return -EFAULT;

		/* Clamp all values to [0,1] */
		for (i = 0; i < lh->numdescs; i++)
			vals[i] = !!ghd.values[i];

		/* Reuse the array setting function */
		gpiod_set_array_value_complex(false,
					      true,
					      lh->numdescs,
					      lh->descs,
					      vals);
		return 0;
	}
	return -EINVAL;
}

#ifdef CONFIG_COMPAT
static long linehandle_ioctl_compat(struct file *filep, unsigned int cmd,
			     unsigned long arg)
{
	return linehandle_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
}
#endif

static int linehandle_release(struct inode *inode, struct file *filep)
{
	struct linehandle_state *lh = filep->private_data;
	struct gpio_device *gdev = lh->gdev;
	int i;

	for (i = 0; i < lh->numdescs; i++)
		gpiod_free(lh->descs[i]);
	kfree(lh->label);
	kfree(lh);
	put_device(&gdev->dev);
	return 0;
}

static const struct file_operations linehandle_fileops = {
	.release = linehandle_release,
	.owner = THIS_MODULE,
	.llseek = noop_llseek,
	.unlocked_ioctl = linehandle_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = linehandle_ioctl_compat,
#endif
};

static int linehandle_create(struct gpio_device *gdev, void __user *ip)
{
	struct gpiohandle_request handlereq;
	struct linehandle_state *lh;
448
	struct file *file;
449
	int fd, i, ret;
450
	u32 lflags;
451
452
453
454
455
456

	if (copy_from_user(&handlereq, ip, sizeof(handlereq)))
		return -EFAULT;
	if ((handlereq.lines == 0) || (handlereq.lines > GPIOHANDLES_MAX))
		return -EINVAL;

457
458
459
460
461
462
	lflags = handlereq.flags;

	/* Return an error if an unknown flag is set */
	if (lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS)
		return -EINVAL;

463
464
465
466
467
468
469
470
471
	/*
	 * Do not allow OPEN_SOURCE & OPEN_DRAIN flags in a single request. If
	 * the hardware actually supports enabling both at the same time the
	 * electrical result would be disastrous.
	 */
	if ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) &&
	    (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE))
		return -EINVAL;

472
473
474
475
476
477
	/* OPEN_DRAIN and OPEN_SOURCE flags only make sense for output mode. */
	if (!(lflags & GPIOHANDLE_REQUEST_OUTPUT) &&
	    ((lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN) ||
	     (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)))
		return -EINVAL;

478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
	lh = kzalloc(sizeof(*lh), GFP_KERNEL);
	if (!lh)
		return -ENOMEM;
	lh->gdev = gdev;
	get_device(&gdev->dev);

	/* Make sure this is terminated */
	handlereq.consumer_label[sizeof(handlereq.consumer_label)-1] = '\0';
	if (strlen(handlereq.consumer_label)) {
		lh->label = kstrdup(handlereq.consumer_label,
				    GFP_KERNEL);
		if (!lh->label) {
			ret = -ENOMEM;
			goto out_free_lh;
		}
	}

	/* Request each GPIO */
	for (i = 0; i < handlereq.lines; i++) {
		u32 offset = handlereq.lineoffsets[i];
		struct gpio_desc *desc;

500
501
502
503
504
		if (offset >= gdev->ngpio) {
			ret = -EINVAL;
			goto out_free_descs;
		}

505
506
507
508
509
510
511
512
513
514
515
516
517
		desc = &gdev->descs[offset];
		ret = gpiod_request(desc, lh->label);
		if (ret)
			goto out_free_descs;
		lh->descs[i] = desc;

		if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
			set_bit(FLAG_ACTIVE_LOW, &desc->flags);
		if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
			set_bit(FLAG_OPEN_DRAIN, &desc->flags);
		if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
			set_bit(FLAG_OPEN_SOURCE, &desc->flags);

518
519
520
521
		ret = gpiod_set_transitory(desc, false);
		if (ret < 0)
			goto out_free_descs;

522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
		/*
		 * Lines have to be requested explicitly for input
		 * or output, else the line will be treated "as is".
		 */
		if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
			int val = !!handlereq.default_values[i];

			ret = gpiod_direction_output(desc, val);
			if (ret)
				goto out_free_descs;
		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
			ret = gpiod_direction_input(desc);
			if (ret)
				goto out_free_descs;
		}
		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
			offset);
	}
540
541
	/* Let i point at the last handle */
	i--;
542
543
	lh->numdescs = handlereq.lines;

544
	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
545
546
547
548
549
	if (fd < 0) {
		ret = fd;
		goto out_free_descs;
	}

550
551
552
553
554
555
556
557
558
	file = anon_inode_getfile("gpio-linehandle",
				  &linehandle_fileops,
				  lh,
				  O_RDONLY | O_CLOEXEC);
	if (IS_ERR(file)) {
		ret = PTR_ERR(file);
		goto out_put_unused_fd;
	}

559
	handlereq.fd = fd;
560
	if (copy_to_user(ip, &handlereq, sizeof(handlereq))) {
561
562
563
564
565
566
567
		/*
		 * fput() will trigger the release() callback, so do not go onto
		 * the regular error cleanup path here.
		 */
		fput(file);
		put_unused_fd(fd);
		return -EFAULT;
568
	}
569

570
571
	fd_install(fd, file);

572
573
574
575
576
	dev_dbg(&gdev->dev, "registered chardev handle for %d lines\n",
		lh->numdescs);

	return 0;

577
578
out_put_unused_fd:
	put_unused_fd(fd);
579
580
581
582
583
584
585
586
587
588
out_free_descs:
	for (; i >= 0; i--)
		gpiod_free(lh->descs[i]);
	kfree(lh->label);
out_free_lh:
	kfree(lh);
	put_device(&gdev->dev);
	return ret;
}

589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/*
 * GPIO line event management
 */

/**
 * struct lineevent_state - contains the state of a userspace event
 * @gdev: the GPIO device the event pertains to
 * @label: consumer label used to tag descriptors
 * @desc: the GPIO descriptor held by this event
 * @eflags: the event flags this line was requested with
 * @irq: the interrupt that trigger in response to events on this GPIO
 * @wait: wait queue that handles blocking reads of events
 * @events: KFIFO for the GPIO events
 * @read_lock: mutex lock to protect reads from colliding with adding
 * new events to the FIFO
604
605
606
 * @timestamp: cache for the timestamp storing it between hardirq
 * and IRQ thread, used to bring the timestamp close to the actual
 * event
607
608
609
610
611
612
613
614
615
616
 */
struct lineevent_state {
	struct gpio_device *gdev;
	const char *label;
	struct gpio_desc *desc;
	u32 eflags;
	int irq;
	wait_queue_head_t wait;
	DECLARE_KFIFO(events, struct gpioevent_data, 16);
	struct mutex read_lock;
617
	u64 timestamp;
618
619
};

620
621
622
623
#define GPIOEVENT_REQUEST_VALID_FLAGS \
	(GPIOEVENT_REQUEST_RISING_EDGE | \
	GPIOEVENT_REQUEST_FALLING_EDGE)

624
static __poll_t lineevent_poll(struct file *filep,
625
626
627
				   struct poll_table_struct *wait)
{
	struct lineevent_state *le = filep->private_data;
628
	__poll_t events = 0;
629
630
631
632

	poll_wait(filep, &le->wait, wait);

	if (!kfifo_is_empty(&le->events))
633
		events = EPOLLIN | EPOLLRDNORM;
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710

	return events;
}


static ssize_t lineevent_read(struct file *filep,
			      char __user *buf,
			      size_t count,
			      loff_t *f_ps)
{
	struct lineevent_state *le = filep->private_data;
	unsigned int copied;
	int ret;

	if (count < sizeof(struct gpioevent_data))
		return -EINVAL;

	do {
		if (kfifo_is_empty(&le->events)) {
			if (filep->f_flags & O_NONBLOCK)
				return -EAGAIN;

			ret = wait_event_interruptible(le->wait,
					!kfifo_is_empty(&le->events));
			if (ret)
				return ret;
		}

		if (mutex_lock_interruptible(&le->read_lock))
			return -ERESTARTSYS;
		ret = kfifo_to_user(&le->events, buf, count, &copied);
		mutex_unlock(&le->read_lock);

		if (ret)
			return ret;

		/*
		 * If we couldn't read anything from the fifo (a different
		 * thread might have been faster) we either return -EAGAIN if
		 * the file descriptor is non-blocking, otherwise we go back to
		 * sleep and wait for more data to arrive.
		 */
		if (copied == 0 && (filep->f_flags & O_NONBLOCK))
			return -EAGAIN;

	} while (copied == 0);

	return copied;
}

static int lineevent_release(struct inode *inode, struct file *filep)
{
	struct lineevent_state *le = filep->private_data;
	struct gpio_device *gdev = le->gdev;

	free_irq(le->irq, le);
	gpiod_free(le->desc);
	kfree(le->label);
	kfree(le);
	put_device(&gdev->dev);
	return 0;
}

static long lineevent_ioctl(struct file *filep, unsigned int cmd,
			    unsigned long arg)
{
	struct lineevent_state *le = filep->private_data;
	void __user *ip = (void __user *)arg;
	struct gpiohandle_data ghd;

	/*
	 * We can get the value for an event line but not set it,
	 * because it is input by definition.
	 */
	if (cmd == GPIOHANDLE_GET_LINE_VALUES_IOCTL) {
		int val;

711
712
		memset(&ghd, 0, sizeof(ghd));

713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
		val = gpiod_get_value_cansleep(le->desc);
		if (val < 0)
			return val;
		ghd.values[0] = val;

		if (copy_to_user(ip, &ghd, sizeof(ghd)))
			return -EFAULT;

		return 0;
	}
	return -EINVAL;
}

#ifdef CONFIG_COMPAT
static long lineevent_ioctl_compat(struct file *filep, unsigned int cmd,
				   unsigned long arg)
{
	return lineevent_ioctl(filep, cmd, (unsigned long)compat_ptr(arg));
}
#endif

static const struct file_operations lineevent_fileops = {
	.release = lineevent_release,
	.read = lineevent_read,
	.poll = lineevent_poll,
	.owner = THIS_MODULE,
	.llseek = noop_llseek,
	.unlocked_ioctl = lineevent_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = lineevent_ioctl_compat,
#endif
};

746
static irqreturn_t lineevent_irq_thread(int irq, void *p)
747
748
749
{
	struct lineevent_state *le = p;
	struct gpioevent_data ge;
750
	int ret, level;
751

752
753
754
	/* Do not leak kernel stack to userspace */
	memset(&ge, 0, sizeof(ge));

755
	ge.timestamp = le->timestamp;
756
	level = gpiod_get_value_cansleep(le->desc);
757

758
759
	if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE
	    && le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE) {
760
761
762
763
764
765
		if (level)
			/* Emit low-to-high event */
			ge.id = GPIOEVENT_EVENT_RISING_EDGE;
		else
			/* Emit high-to-low event */
			ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
766
	} else if (le->eflags & GPIOEVENT_REQUEST_RISING_EDGE && level) {
767
768
		/* Emit low-to-high event */
		ge.id = GPIOEVENT_EVENT_RISING_EDGE;
769
	} else if (le->eflags & GPIOEVENT_REQUEST_FALLING_EDGE && !level) {
770
771
		/* Emit high-to-low event */
		ge.id = GPIOEVENT_EVENT_FALLING_EDGE;
772
773
	} else {
		return IRQ_NONE;
774
775
776
777
	}

	ret = kfifo_put(&le->events, ge);
	if (ret != 0)
778
		wake_up_poll(&le->wait, EPOLLIN);
779
780
781
782

	return IRQ_HANDLED;
}

783
784
785
786
787
788
789
790
791
792
793
794
795
static irqreturn_t lineevent_irq_handler(int irq, void *p)
{
	struct lineevent_state *le = p;

	/*
	 * Just store the timestamp in hardirq context so we get it as
	 * close in time as possible to the actual event.
	 */
	le->timestamp = ktime_get_real_ns();

	return IRQ_WAKE_THREAD;
}

796
797
798
799
800
static int lineevent_create(struct gpio_device *gdev, void __user *ip)
{
	struct gpioevent_request eventreq;
	struct lineevent_state *le;
	struct gpio_desc *desc;
801
	struct file *file;
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
	u32 offset;
	u32 lflags;
	u32 eflags;
	int fd;
	int ret;
	int irqflags = 0;

	if (copy_from_user(&eventreq, ip, sizeof(eventreq)))
		return -EFAULT;

	le = kzalloc(sizeof(*le), GFP_KERNEL);
	if (!le)
		return -ENOMEM;
	le->gdev = gdev;
	get_device(&gdev->dev);

	/* Make sure this is terminated */
	eventreq.consumer_label[sizeof(eventreq.consumer_label)-1] = '\0';
	if (strlen(eventreq.consumer_label)) {
		le->label = kstrdup(eventreq.consumer_label,
				    GFP_KERNEL);
		if (!le->label) {
			ret = -ENOMEM;
			goto out_free_le;
		}
	}

	offset = eventreq.lineoffset;
	lflags = eventreq.handleflags;
	eflags = eventreq.eventflags;

833
834
835
836
837
	if (offset >= gdev->ngpio) {
		ret = -EINVAL;
		goto out_free_label;
	}

838
839
840
841
842
843
844
	/* Return an error if a unknown flag is set */
	if ((lflags & ~GPIOHANDLE_REQUEST_VALID_FLAGS) ||
	    (eflags & ~GPIOEVENT_REQUEST_VALID_FLAGS)) {
		ret = -EINVAL;
		goto out_free_label;
	}

845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
	/* This is just wrong: we don't look for events on output lines */
	if (lflags & GPIOHANDLE_REQUEST_OUTPUT) {
		ret = -EINVAL;
		goto out_free_label;
	}

	desc = &gdev->descs[offset];
	ret = gpiod_request(desc, le->label);
	if (ret)
		goto out_free_desc;
	le->desc = desc;
	le->eflags = eflags;

	if (lflags & GPIOHANDLE_REQUEST_ACTIVE_LOW)
		set_bit(FLAG_ACTIVE_LOW, &desc->flags);
	if (lflags & GPIOHANDLE_REQUEST_OPEN_DRAIN)
		set_bit(FLAG_OPEN_DRAIN, &desc->flags);
	if (lflags & GPIOHANDLE_REQUEST_OPEN_SOURCE)
		set_bit(FLAG_OPEN_SOURCE, &desc->flags);

	ret = gpiod_direction_input(desc);
	if (ret)
		goto out_free_desc;

	le->irq = gpiod_to_irq(desc);
	if (le->irq <= 0) {
		ret = -ENODEV;
		goto out_free_desc;
	}

	if (eflags & GPIOEVENT_REQUEST_RISING_EDGE)
		irqflags |= IRQF_TRIGGER_RISING;
	if (eflags & GPIOEVENT_REQUEST_FALLING_EDGE)
		irqflags |= IRQF_TRIGGER_FALLING;
	irqflags |= IRQF_ONESHOT;
	irqflags |= IRQF_SHARED;

	INIT_KFIFO(le->events);
	init_waitqueue_head(&le->wait);
	mutex_init(&le->read_lock);

	/* Request a thread to read the events */
	ret = request_threaded_irq(le->irq,
888
			lineevent_irq_handler,
889
890
891
892
893
894
895
			lineevent_irq_thread,
			irqflags,
			le->label,
			le);
	if (ret)
		goto out_free_desc;

896
	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
897
898
899
900
901
	if (fd < 0) {
		ret = fd;
		goto out_free_irq;
	}

902
903
904
905
906
907
908
909
910
	file = anon_inode_getfile("gpio-event",
				  &lineevent_fileops,
				  le,
				  O_RDONLY | O_CLOEXEC);
	if (IS_ERR(file)) {
		ret = PTR_ERR(file);
		goto out_put_unused_fd;
	}

911
	eventreq.fd = fd;
912
	if (copy_to_user(ip, &eventreq, sizeof(eventreq))) {
913
914
915
916
917
918
919
		/*
		 * fput() will trigger the release() callback, so do not go onto
		 * the regular error cleanup path here.
		 */
		fput(file);
		put_unused_fd(fd);
		return -EFAULT;
920
	}
921

922
923
	fd_install(fd, file);

924
925
	return 0;

926
927
out_put_unused_fd:
	put_unused_fd(fd);
928
929
930
931
932
933
934
935
936
937
938
939
out_free_irq:
	free_irq(le->irq, le);
out_free_desc:
	gpiod_free(le->desc);
out_free_label:
	kfree(le->label);
out_free_le:
	kfree(le);
	put_device(&gdev->dev);
	return ret;
}

Thierry Reding's avatar
Thierry Reding committed
940
/*
941
942
943
944
945
946
 * gpio_ioctl() - ioctl handler for the GPIO chardev
 */
static long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct gpio_device *gdev = filp->private_data;
	struct gpio_chip *chip = gdev->chip;
947
	void __user *ip = (void __user *)arg;
948
949
950
951
952

	/* We fail any subsequent ioctl():s when the chip is gone */
	if (!chip)
		return -ENODEV;

953
	/* Fill in the struct and pass to userspace */
954
	if (cmd == GPIO_GET_CHIPINFO_IOCTL) {
955
956
		struct gpiochip_info chipinfo;

957
958
		memset(&chipinfo, 0, sizeof(chipinfo));

959
960
961
		strncpy(chipinfo.name, dev_name(&gdev->dev),
			sizeof(chipinfo.name));
		chipinfo.name[sizeof(chipinfo.name)-1] = '\0';
962
963
964
		strncpy(chipinfo.label, gdev->label,
			sizeof(chipinfo.label));
		chipinfo.label[sizeof(chipinfo.label)-1] = '\0';
965
		chipinfo.lines = gdev->ngpio;
966
967
968
		if (copy_to_user(ip, &chipinfo, sizeof(chipinfo)))
			return -EFAULT;
		return 0;
969
970
971
972
973
974
	} else if (cmd == GPIO_GET_LINEINFO_IOCTL) {
		struct gpioline_info lineinfo;
		struct gpio_desc *desc;

		if (copy_from_user(&lineinfo, ip, sizeof(lineinfo)))
			return -EFAULT;
975
		if (lineinfo.line_offset >= gdev->ngpio)
976
977
978
979
980
981
982
983
984
985
986
			return -EINVAL;

		desc = &gdev->descs[lineinfo.line_offset];
		if (desc->name) {
			strncpy(lineinfo.name, desc->name,
				sizeof(lineinfo.name));
			lineinfo.name[sizeof(lineinfo.name)-1] = '\0';
		} else {
			lineinfo.name[0] = '\0';
		}
		if (desc->label) {
987
988
989
			strncpy(lineinfo.consumer, desc->label,
				sizeof(lineinfo.consumer));
			lineinfo.consumer[sizeof(lineinfo.consumer)-1] = '\0';
990
		} else {
991
			lineinfo.consumer[0] = '\0';
992
993
994
995
996
997
998
		}

		/*
		 * Userspace only need to know that the kernel is using
		 * this GPIO so it can't use it.
		 */
		lineinfo.flags = 0;
999
1000
		if (test_bit(FLAG_REQUESTED, &desc->flags) ||
		    test_bit(FLAG_IS_HOGGED, &desc->flags) ||