Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Userspace driver for the LED subsystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2016 David Lechner <david@lechnology.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Based on uinput.c: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/leds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <uapi/linux/uleds.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define ULEDS_NAME	"uleds"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) enum uleds_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	ULEDS_STATE_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	ULEDS_STATE_REGISTERED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) struct uleds_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct uleds_user_dev	user_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	struct led_classdev	led_cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct mutex		mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	enum uleds_state	state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	wait_queue_head_t	waitq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	int			brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	bool			new_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static struct miscdevice uleds_misc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static void uleds_brightness_set(struct led_classdev *led_cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 				 enum led_brightness brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct uleds_device *udev = container_of(led_cdev, struct uleds_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 						 led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (udev->brightness != brightness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		udev->brightness = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 		udev->new_data = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		wake_up_interruptible(&udev->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) static int uleds_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct uleds_device *udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	udev = kzalloc(sizeof(*udev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (!udev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	udev->led_cdev.name = udev->user_dev.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	udev->led_cdev.brightness_set = uleds_brightness_set;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	mutex_init(&udev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	init_waitqueue_head(&udev->waitq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	udev->state = ULEDS_STATE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	file->private_data = udev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	stream_open(inode, file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static ssize_t uleds_write(struct file *file, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 			   size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct uleds_device *udev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	ret = mutex_lock_interruptible(&udev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (udev->state == ULEDS_STATE_REGISTERED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (count != sizeof(struct uleds_user_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	if (copy_from_user(&udev->user_dev, buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			   sizeof(struct uleds_user_dev))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		ret = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	name = udev->user_dev.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (!name[0] || !strcmp(name, ".") || !strcmp(name, "..") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	    strchr(name, '/')) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (udev->user_dev.max_brightness <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	udev->led_cdev.max_brightness = udev->user_dev.max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	ret = devm_led_classdev_register(uleds_misc.this_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 					 &udev->led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	udev->new_data = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	udev->state = ULEDS_STATE_REGISTERED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	ret = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	mutex_unlock(&udev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			  loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	struct uleds_device *udev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	ssize_t retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	if (count < sizeof(udev->brightness))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		retval = mutex_lock_interruptible(&udev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		if (udev->state != ULEDS_STATE_REGISTERED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 			retval = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		} else if (!udev->new_data && (file->f_flags & O_NONBLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 			retval = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		} else if (udev->new_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			retval = copy_to_user(buffer, &udev->brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 					      sizeof(udev->brightness));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 			udev->new_data = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 			retval = sizeof(udev->brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		mutex_unlock(&udev->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 		if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		if (!(file->f_flags & O_NONBLOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 			retval = wait_event_interruptible(udev->waitq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 					udev->new_data ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 					udev->state != ULEDS_STATE_REGISTERED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	} while (retval == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static __poll_t uleds_poll(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct uleds_device *udev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	poll_wait(file, &udev->waitq, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (udev->new_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static int uleds_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	struct uleds_device *udev = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (udev->state == ULEDS_STATE_REGISTERED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		udev->state = ULEDS_STATE_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		devm_led_classdev_unregister(uleds_misc.this_device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 					     &udev->led_cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	kfree(udev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static const struct file_operations uleds_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.open		= uleds_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.release	= uleds_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	.read		= uleds_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.write		= uleds_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	.poll		= uleds_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static struct miscdevice uleds_misc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	.fops		= &uleds_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	.minor		= MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	.name		= ULEDS_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static int __init uleds_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	return misc_register(&uleds_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) module_init(uleds_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static void __exit uleds_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	misc_deregister(&uleds_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) module_exit(uleds_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) MODULE_AUTHOR("David Lechner <david@lechnology.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) MODULE_DESCRIPTION("Userspace driver for the LED subsystem");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) MODULE_LICENSE("GPL");