drivers/gpio/gpiolib-cdev.c

Source file repositories/reference/linux-study-clean/drivers/gpio/gpiolib-cdev.c

File Facts

System
Linux kernel
Corpus path
drivers/gpio/gpiolib-cdev.c
Extension
.c
Size
73668 bytes
Lines
2774
Domain
Driver Families
Bucket
drivers/gpio
Inferred role
Driver Families: operation-table or driver-model contract
Status
pattern implementation candidate

Why This File Exists

Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.

Dependency Surface

Detected Declarations

Annotated Snippet

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
};

DEFINE_FREE(linehandle_free, struct linehandle_state *, if (!IS_ERR_OR_NULL(_T)) linehandle_free(_T))

static int linehandle_create(struct gpio_device *gdev, void __user *ip)
{
	struct gpiohandle_request handlereq;
	struct linehandle_state *lh __free(linehandle_free) = NULL;
	int i, ret;
	u32 lflags;

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

	lflags = handlereq.flags;

	ret = linehandle_validate_flags(lflags);
	if (ret)
		return ret;

	lh = kzalloc_obj(*lh);
	if (!lh)
		return -ENOMEM;
	lh->gdev = gpio_device_get(gdev);

	if (handlereq.consumer_label[0] != '\0') {
		/* label is only initialized if consumer_label is set */
		lh->label = kstrndup(handlereq.consumer_label,
				     sizeof(handlereq.consumer_label) - 1,
				     GFP_KERNEL);
		if (!lh->label)
			return -ENOMEM;
	}

	lh->num_descs = handlereq.lines;

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

		if (IS_ERR(desc))
			return PTR_ERR(desc);

		ret = gpiod_request_user(desc, lh->label);
		if (ret)
			return ret;
		lh->descs[i] = desc;
		linehandle_flags_to_desc_flags(handlereq.flags, &desc->flags);

		ret = gpiod_set_transitory(desc, false);
		if (ret < 0)
			return ret;

		/*
		 * 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_nonotify(desc, val);
			if (ret)
				return ret;
		} else if (lflags & GPIOHANDLE_REQUEST_INPUT) {
			ret = gpiod_direction_input_nonotify(desc);
			if (ret)
				return ret;
		}

		gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_REQUESTED);

		dev_dbg(&gdev->dev, "registered chardev handle for line %d\n",
			offset);
	}

	FD_PREPARE(fdf, O_RDONLY | O_CLOEXEC,
		   anon_inode_getfile("gpio-linehandle", &linehandle_fileops,
				      lh, O_RDONLY | O_CLOEXEC));
	if (fdf.err)

Annotation

Implementation Notes