2012-02-29 23:23:51 +04:00
|
|
|
/*
|
|
|
|
* libbacklight - userspace interface to Linux backlight control
|
|
|
|
*
|
|
|
|
* Copyright © 2012 Intel Corporation
|
|
|
|
* Copyright 2010 Red Hat <mjg@redhat.com>
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
* Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Matthew Garrett <mjg@redhat.com>
|
|
|
|
* Tiago Vignatti <vignatti@freedesktop.org>
|
|
|
|
*/
|
|
|
|
|
2013-05-22 19:03:19 +04:00
|
|
|
#include "config.h"
|
2012-02-29 23:23:51 +04:00
|
|
|
|
2012-03-02 19:33:17 +04:00
|
|
|
#include "libbacklight.h"
|
2016-07-19 14:16:27 +03:00
|
|
|
#include <stdint.h>
|
2012-02-29 23:23:51 +04:00
|
|
|
#include <stdio.h>
|
2012-03-02 19:33:17 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2012-02-29 23:23:51 +04:00
|
|
|
#include <linux/types.h>
|
|
|
|
#include <dirent.h>
|
2012-03-02 19:33:17 +04:00
|
|
|
#include <drm.h>
|
2012-02-29 23:23:51 +04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2016-08-04 03:40:52 +03:00
|
|
|
#include "shared/string-helpers.h"
|
|
|
|
|
2012-02-29 23:23:51 +04:00
|
|
|
static long backlight_get(struct backlight *backlight, char *node)
|
|
|
|
{
|
|
|
|
char buffer[100];
|
|
|
|
char *path;
|
2016-08-04 03:40:52 +03:00
|
|
|
int fd, value;
|
|
|
|
long ret;
|
2012-02-29 23:23:51 +04:00
|
|
|
|
2022-04-05 19:33:40 +03:00
|
|
|
str_printf(&path, "%s/%s", backlight->path, node);
|
|
|
|
if (!path)
|
2014-01-15 01:58:34 +04:00
|
|
|
return -ENOMEM;
|
2022-04-05 19:33:40 +03:00
|
|
|
|
2012-02-29 23:23:51 +04:00
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read(fd, &buffer, sizeof(buffer));
|
|
|
|
if (ret < 1) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-04-05 19:33:40 +03:00
|
|
|
if (buffer[ret - 1] == '\n')
|
|
|
|
buffer[ret - 1] = '\0';
|
|
|
|
|
2016-08-04 03:40:52 +03:00
|
|
|
if (!safe_strtoint(buffer, &value)) {
|
2016-08-04 03:40:48 +03:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2012-02-29 23:23:51 +04:00
|
|
|
ret = value;
|
2016-08-04 03:40:48 +03:00
|
|
|
|
2012-02-29 23:23:51 +04:00
|
|
|
out:
|
2013-01-10 23:48:56 +04:00
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2012-12-05 22:47:06 +04:00
|
|
|
free(path);
|
2012-02-29 23:23:51 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
long backlight_get_brightness(struct backlight *backlight)
|
|
|
|
{
|
|
|
|
return backlight_get(backlight, "brightness");
|
|
|
|
}
|
|
|
|
|
|
|
|
long backlight_get_max_brightness(struct backlight *backlight)
|
|
|
|
{
|
|
|
|
return backlight_get(backlight, "max_brightness");
|
|
|
|
}
|
|
|
|
|
|
|
|
long backlight_get_actual_brightness(struct backlight *backlight)
|
|
|
|
{
|
|
|
|
return backlight_get(backlight, "actual_brightness");
|
|
|
|
}
|
|
|
|
|
|
|
|
long backlight_set_brightness(struct backlight *backlight, long brightness)
|
|
|
|
{
|
|
|
|
char *path;
|
|
|
|
char *buffer = NULL;
|
|
|
|
int fd;
|
|
|
|
long ret;
|
|
|
|
|
2022-04-05 19:33:40 +03:00
|
|
|
str_printf(&path, "%s/%s", backlight->path, "brightness");
|
|
|
|
if (!path)
|
2012-02-29 23:23:51 +04:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
fd = open(path, O_RDWR);
|
|
|
|
if (fd < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = read(fd, &buffer, sizeof(buffer));
|
|
|
|
if (ret < 1) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-04-05 19:33:40 +03:00
|
|
|
str_printf(&buffer, "%ld", brightness);
|
|
|
|
if (!buffer) {
|
2012-10-09 21:44:36 +04:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2012-02-29 23:23:51 +04:00
|
|
|
|
|
|
|
ret = write(fd, buffer, strlen(buffer));
|
|
|
|
if (ret < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = backlight_get_brightness(backlight);
|
|
|
|
backlight->brightness = ret;
|
|
|
|
out:
|
2013-01-10 23:48:56 +04:00
|
|
|
free(buffer);
|
2012-12-05 22:47:06 +04:00
|
|
|
free(path);
|
2013-01-10 23:48:56 +04:00
|
|
|
if (fd >= 0)
|
|
|
|
close(fd);
|
2012-02-29 23:23:51 +04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void backlight_destroy(struct backlight *backlight)
|
|
|
|
{
|
|
|
|
if (!backlight)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (backlight->path)
|
|
|
|
free(backlight->path);
|
|
|
|
|
|
|
|
free(backlight);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct backlight *backlight_init(struct udev_device *drm_device,
|
|
|
|
uint32_t connector_type)
|
|
|
|
{
|
|
|
|
const char *syspath = NULL;
|
|
|
|
char *pci_name = NULL;
|
|
|
|
char *chosen_path = NULL;
|
|
|
|
char *path = NULL;
|
2012-12-05 22:47:09 +04:00
|
|
|
DIR *backlights = NULL;
|
2012-02-29 23:23:51 +04:00
|
|
|
struct dirent *entry;
|
|
|
|
enum backlight_type type = 0;
|
|
|
|
char buffer[100];
|
2012-12-05 22:47:09 +04:00
|
|
|
struct backlight *backlight = NULL;
|
2012-03-02 19:33:17 +04:00
|
|
|
int ret;
|
2012-02-29 23:23:51 +04:00
|
|
|
|
|
|
|
if (!drm_device)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
syspath = udev_device_get_syspath(drm_device);
|
|
|
|
if (!syspath)
|
|
|
|
return NULL;
|
|
|
|
|
2022-04-05 19:33:40 +03:00
|
|
|
str_printf(&path, "%s/%s", syspath, "device");
|
|
|
|
if (!path)
|
2012-02-29 23:23:51 +04:00
|
|
|
return NULL;
|
|
|
|
|
2012-10-09 21:44:33 +04:00
|
|
|
ret = readlink(path, buffer, sizeof(buffer) - 1);
|
2012-02-29 23:23:51 +04:00
|
|
|
free(path);
|
|
|
|
if (ret < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buffer[ret] = '\0';
|
|
|
|
pci_name = basename(buffer);
|
|
|
|
|
|
|
|
if (connector_type <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
backlights = opendir("/sys/class/backlight");
|
|
|
|
if (!backlights)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Find the "best" backlight for the device. Firmware
|
|
|
|
interfaces are preferred over platform interfaces are
|
|
|
|
preferred over raw interfaces. For raw interfaces we'll
|
|
|
|
check if the device ID in the form of pci match, while
|
|
|
|
for firmware interfaces we require the pci ID to
|
|
|
|
match. It's assumed that platform interfaces always match,
|
|
|
|
since we can't actually associate them with IDs.
|
|
|
|
|
|
|
|
A further awkwardness is that, while it's theoretically
|
|
|
|
possible for an ACPI interface to include support for
|
|
|
|
changing the backlight of external devices, it's unlikely
|
|
|
|
to ever be done. It's effectively impossible for a platform
|
|
|
|
interface to do so. So if we get asked about anything that
|
|
|
|
isn't LVDS or eDP, we pretty much have to require that the
|
|
|
|
control be supplied via a raw interface */
|
|
|
|
|
|
|
|
while ((entry = readdir(backlights))) {
|
|
|
|
char *backlight_path;
|
|
|
|
char *parent;
|
|
|
|
enum backlight_type entry_type;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (entry->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
2022-04-05 19:33:40 +03:00
|
|
|
str_printf(&backlight_path, "%s/%s", "/sys/class/backlight",
|
|
|
|
entry->d_name);
|
|
|
|
if (!backlight_path)
|
2012-12-05 22:47:09 +04:00
|
|
|
goto err;
|
2012-02-29 23:23:51 +04:00
|
|
|
|
2022-04-05 19:33:40 +03:00
|
|
|
str_printf(&path, "%s/%s", backlight_path, "type");
|
|
|
|
if (!path) {
|
2014-01-15 20:12:19 +04:00
|
|
|
free(backlight_path);
|
2012-12-05 22:47:09 +04:00
|
|
|
goto err;
|
2014-01-15 20:12:19 +04:00
|
|
|
}
|
2012-02-29 23:23:51 +04:00
|
|
|
|
|
|
|
fd = open(path, O_RDONLY);
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = read (fd, &buffer, sizeof(buffer));
|
|
|
|
close (fd);
|
|
|
|
|
|
|
|
if (ret < 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
buffer[ret] = '\0';
|
|
|
|
|
|
|
|
if (!strncmp(buffer, "raw\n", sizeof(buffer)))
|
|
|
|
entry_type = BACKLIGHT_RAW;
|
|
|
|
else if (!strncmp(buffer, "platform\n", sizeof(buffer)))
|
|
|
|
entry_type = BACKLIGHT_PLATFORM;
|
|
|
|
else if (!strncmp(buffer, "firmware\n", sizeof(buffer)))
|
|
|
|
entry_type = BACKLIGHT_FIRMWARE;
|
|
|
|
else
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (connector_type != DRM_MODE_CONNECTOR_LVDS &&
|
|
|
|
connector_type != DRM_MODE_CONNECTOR_eDP) {
|
|
|
|
/* External displays are assumed to require
|
|
|
|
gpu control at the moment */
|
|
|
|
if (entry_type != BACKLIGHT_RAW)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
free (path);
|
|
|
|
|
2022-04-05 19:33:40 +03:00
|
|
|
str_printf(&path, "%s/%s", backlight_path, "device");
|
|
|
|
if (!path)
|
2012-12-05 22:47:09 +04:00
|
|
|
goto err;
|
2012-02-29 23:23:51 +04:00
|
|
|
|
2012-10-09 21:44:33 +04:00
|
|
|
ret = readlink(path, buffer, sizeof(buffer) - 1);
|
2012-02-29 23:23:51 +04:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
buffer[ret] = '\0';
|
|
|
|
|
|
|
|
parent = basename(buffer);
|
|
|
|
|
2015-06-06 16:02:22 +03:00
|
|
|
/* Perform matching for raw and firmware backlights -
|
2012-02-29 23:23:51 +04:00
|
|
|
platform backlights have to be assumed to match */
|
|
|
|
if (entry_type == BACKLIGHT_RAW ||
|
|
|
|
entry_type == BACKLIGHT_FIRMWARE) {
|
|
|
|
if (!(pci_name && !strcmp(pci_name, parent)))
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (entry_type < type)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
type = entry_type;
|
|
|
|
|
|
|
|
if (chosen_path)
|
|
|
|
free(chosen_path);
|
|
|
|
chosen_path = strdup(backlight_path);
|
|
|
|
|
|
|
|
out:
|
|
|
|
free(backlight_path);
|
|
|
|
free(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!chosen_path)
|
2012-12-05 22:47:09 +04:00
|
|
|
goto err;
|
2012-02-29 23:23:51 +04:00
|
|
|
|
|
|
|
backlight = malloc(sizeof(struct backlight));
|
|
|
|
|
|
|
|
if (!backlight)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
backlight->path = chosen_path;
|
|
|
|
backlight->type = type;
|
|
|
|
|
|
|
|
backlight->max_brightness = backlight_get_max_brightness(backlight);
|
|
|
|
if (backlight->max_brightness < 0)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
backlight->brightness = backlight_get_actual_brightness(backlight);
|
|
|
|
if (backlight->brightness < 0)
|
|
|
|
goto err;
|
|
|
|
|
2012-12-05 22:47:09 +04:00
|
|
|
closedir(backlights);
|
2012-02-29 23:23:51 +04:00
|
|
|
return backlight;
|
|
|
|
err:
|
2012-12-05 22:47:09 +04:00
|
|
|
closedir(backlights);
|
|
|
|
free (chosen_path);
|
2012-02-29 23:23:51 +04:00
|
|
|
free (backlight);
|
|
|
|
return NULL;
|
|
|
|
}
|