Source
x
static ssize_t uleds_read(struct file *file, char __user *buffer, size_t count,
/*
* Userspace driver for the LED subsystem
*
* Copyright (C) 2016 David Lechner <david@lechnology.com>
*
* Based on uinput.c: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
enum uleds_state {
ULEDS_STATE_UNKNOWN,
ULEDS_STATE_REGISTERED,
};
struct uleds_device {
struct uleds_user_dev user_dev;
struct led_classdev led_cdev;
struct mutex mutex;
enum uleds_state state;
wait_queue_head_t waitq;
int brightness;
bool new_data;
};
static struct miscdevice uleds_misc;
static void uleds_brightness_set(struct led_classdev *led_cdev,
enum led_brightness brightness)
{
struct uleds_device *udev = container_of(led_cdev, struct uleds_device,
led_cdev);
if (udev->brightness != brightness) {
udev->brightness = brightness;
udev->new_data = true;
wake_up_interruptible(&udev->waitq);
}
}
static int uleds_open(struct inode *inode, struct file *file)
{
struct uleds_device *udev;
udev = kzalloc(sizeof(*udev), GFP_KERNEL);
if (!udev)
return -ENOMEM;
udev->led_cdev.name = udev->user_dev.name;
udev->led_cdev.brightness_set = uleds_brightness_set;
mutex_init(&udev->mutex);
init_waitqueue_head(&udev->waitq);
udev->state = ULEDS_STATE_UNKNOWN;
file->private_data = udev;
nonseekable_open(inode, file);
return 0;
}
static ssize_t uleds_write(struct file *file, const char __user *buffer,
size_t count, loff_t *ppos)
{
struct uleds_device *udev = file->private_data;
const char *name;
int ret;
if (count == 0)
return 0;
ret = mutex_lock_interruptible(&udev->mutex);