Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / leds / uleds.c
blob7320337b22d2cf6bd6324c713c6ef032e77c6cb3
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Userspace driver for the LED subsystem
5 * Copyright (C) 2016 David Lechner <david@lechnology.com>
7 * Based on uinput.c: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
8 */
9 #include <linux/fs.h>
10 #include <linux/init.h>
11 #include <linux/leds.h>
12 #include <linux/miscdevice.h>
13 #include <linux/module.h>
14 #include <linux/poll.h>
15 #include <linux/sched.h>
16 #include <linux/slab.h>
18 #include <uapi/linux/uleds.h>
20 #define ULEDS_NAME "uleds"
22 enum uleds_state {
23 ULEDS_STATE_UNKNOWN,
24 ULEDS_STATE_REGISTERED,
27 struct uleds_device {
28 struct uleds_user_dev user_dev;
29 struct led_classdev led_cdev;
30 struct mutex mutex;
31 enum uleds_state state;
32 wait_queue_head_t waitq;
33 int brightness;
34 bool new_data;
37 static struct miscdevice uleds_misc;
39 static void uleds_brightness_set(struct led_classdev *led_cdev,
40 enum led_brightness brightness)
42 struct uleds_device *udev = container_of(led_cdev, struct uleds_device,
43 led_cdev);
45 if (udev->brightness != brightness) {
46 udev->brightness = brightness;
47 udev->new_data = true;
48 wake_up_interruptible(&udev->waitq);
52 static int uleds_open(struct inode *inode, struct file *file)
54 struct uleds_device *udev;
56 udev = kzalloc(sizeof(*udev), GFP_KERNEL);
57 if (!udev)
58 return -ENOMEM;
60 udev->led_cdev.name = udev->user_dev.name;
61 udev->led_cdev.brightness_set = uleds_brightness_set;
63 mutex_init(&udev->mutex);
64 init_waitqueue_head(&udev->waitq);
65 udev->state = ULEDS_STATE_UNKNOWN;
67 file->private_data = udev;
68 stream_open(inode, file);
70 return 0;
73 static ssize_t uleds_write(struct file *file, const char __user *buffer,
74 size_t count, loff_t *ppos)
76 struct uleds_device *udev = file->private_data;
77 const char *name;
78 int ret;
80 if (count == 0)
81 return 0;
83 ret = mutex_lock_interruptible(&udev->mutex);
84 if (ret)
85 return ret;
87 if (udev->state == ULEDS_STATE_REGISTERED) {
88 ret = -EBUSY;
89 goto out;
92 if (count != sizeof(struct uleds_user_dev)) {
93 ret = -EINVAL;
94 goto out;
97 if (copy_from_user(&udev->user_dev, buffer,
98 sizeof(struct uleds_user_dev))) {
99 ret = -EFAULT;
100 goto out;
103 name = udev->user_dev.name;
104 if (!name[0] || !strcmp(name, ".") || !strcmp(name, "..") ||
105 strchr(name, '/')) {
106 ret = -EINVAL;
107 goto out;
110 if (udev->user_dev.max_brightness <= 0) {
111 ret = -EINVAL;
112 goto out;
114 udev->led_cdev.max_brightness = udev->user_dev.max_brightness;
116 ret = devm_led_classdev_register(uleds_misc.this_device,
117 &udev->led_cdev);
118 if (ret < 0)
119 goto out;
121 udev->new_data = true;
122 udev->state = ULEDS_STATE_REGISTERED;
123 ret = count;
125 out:
126 mutex_unlock(&udev->mutex);
128 return ret;
131 static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count,
132 loff_t *ppos)
134 struct uleds_device *udev = file->private_data;
135 ssize_t retval;
137 if (count < sizeof(udev->brightness))
138 return 0;
140 do {
141 retval = mutex_lock_interruptible(&udev->mutex);
142 if (retval)
143 return retval;
145 if (udev->state != ULEDS_STATE_REGISTERED) {
146 retval = -ENODEV;
147 } else if (!udev->new_data && (file->f_flags & O_NONBLOCK)) {
148 retval = -EAGAIN;
149 } else if (udev->new_data) {
150 retval = copy_to_user(buffer, &udev->brightness,
151 sizeof(udev->brightness));
152 udev->new_data = false;
153 retval = sizeof(udev->brightness);
156 mutex_unlock(&udev->mutex);
158 if (retval)
159 break;
161 if (!(file->f_flags & O_NONBLOCK))
162 retval = wait_event_interruptible(udev->waitq,
163 udev->new_data ||
164 udev->state != ULEDS_STATE_REGISTERED);
165 } while (retval == 0);
167 return retval;
170 static __poll_t uleds_poll(struct file *file, poll_table *wait)
172 struct uleds_device *udev = file->private_data;
174 poll_wait(file, &udev->waitq, wait);
176 if (udev->new_data)
177 return EPOLLIN | EPOLLRDNORM;
179 return 0;
182 static int uleds_release(struct inode *inode, struct file *file)
184 struct uleds_device *udev = file->private_data;
186 if (udev->state == ULEDS_STATE_REGISTERED) {
187 udev->state = ULEDS_STATE_UNKNOWN;
188 devm_led_classdev_unregister(uleds_misc.this_device,
189 &udev->led_cdev);
191 kfree(udev);
193 return 0;
196 static const struct file_operations uleds_fops = {
197 .owner = THIS_MODULE,
198 .open = uleds_open,
199 .release = uleds_release,
200 .read = uleds_read,
201 .write = uleds_write,
202 .poll = uleds_poll,
203 .llseek = no_llseek,
206 static struct miscdevice uleds_misc = {
207 .fops = &uleds_fops,
208 .minor = MISC_DYNAMIC_MINOR,
209 .name = ULEDS_NAME,
212 static int __init uleds_init(void)
214 return misc_register(&uleds_misc);
216 module_init(uleds_init);
218 static void __exit uleds_exit(void)
220 misc_deregister(&uleds_misc);
222 module_exit(uleds_exit);
224 MODULE_AUTHOR("David Lechner <david@lechnology.com>");
225 MODULE_DESCRIPTION("Userspace driver for the LED subsystem");
226 MODULE_LICENSE("GPL");