aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEmmanuel Vadot <manu@FreeBSD.org>2020-10-02 18:18:01 +0000
committerEmmanuel Vadot <manu@FreeBSD.org>2020-10-02 18:18:01 +0000
commit675aae732d3dd2ec0f12f39df3a1fbfe693a4ebd (patch)
tree417bd9f59e6b5b38e054b5394fee2c51ca02fed4
parentf31695cc64e2328028b7432a2a6bdcd088909b2a (diff)
downloadsrc-675aae732d3.tar.gz
src-675aae732d3.zip
Add backlight subsystem
This is a simple subsystem that allow drivers to register as a backlight. Each backlight creates a device node under /dev/backlight/backlightX and an alias based on the name provided. Relnotes: yes Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D26250
Notes
Notes: svn path=/head/; revision=366369
-rw-r--r--share/man/man9/Makefile1
-rw-r--r--share/man/man9/backlight.977
-rw-r--r--sys/conf/files2
-rw-r--r--sys/dev/backlight/backlight.c170
-rw-r--r--sys/dev/backlight/backlight.h33
-rw-r--r--sys/dev/backlight/backlight_if.m66
-rw-r--r--sys/modules/Makefile1
-rw-r--r--sys/modules/backlight/Makefile13
-rw-r--r--sys/sys/backlight.h61
9 files changed, 424 insertions, 0 deletions
diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile
index c42744cc5d27..b6d2c24339b4 100644
--- a/share/man/man9/Makefile
+++ b/share/man/man9/Makefile
@@ -10,6 +10,7 @@ MAN= accept_filter.9 \
alq.9 \
altq.9 \
atomic.9 \
+ backlight.9 \
bhnd.9 \
bhnd_erom.9 \
bios.9 \
diff --git a/share/man/man9/backlight.9 b/share/man/man9/backlight.9
new file mode 100644
index 000000000000..e8b5de581cd0
--- /dev/null
+++ b/share/man/man9/backlight.9
@@ -0,0 +1,77 @@
+.\" Copyright (c) 2020 Emmanuel Vadot <manu@freebsd.org>
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
+.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
+.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\" $FreeBSD$
+.\"
+.Dd October 02, 2020
+.Dt BACKLIGHT 9
+.Os
+.Sh NAME
+.Nm backlight ,
+.Nm backlight_register ,
+.Nm backlight_destroy ,
+.Nm BACKLIGHT_GET_STATUS ,
+.Nm BACKLIGHT_SET_STATUS ,
+.Nd BACKLIGHT methods
+.Sh SYNOPSIS
+.Cd "device backlight"
+.In "backlight_if.h"
+.In "sys/sys/backlight.h"
+.Ft int
+.Fn BACKLIGHT_GET_STATUS "device_t bus" "struct backlight_props *props"
+.Ft int
+.Fn BACKLIGHT_SET_STATUS "device_t bus" "struct backlight_props *props"
+.Ft struct cdev *
+.Fn backlight_register "const char *name" "device_t dev"
+.Ft int
+.Fn backlight_destroy "struct cdev *cdev"
+.Sh DESCRIPTION
+The backlight driver provides a generic way for handling a panel backlight.
+.Pp
+Drivers for backlight system register themselves globally using the
+.Fn backlight_register
+function.
+They must define two methods,
+.Fn BACKLIGHT_GET_STATUS
+which is used to query the current brightness level and
+.Fn BACKLIGHT_SET_STATUS
+which is used to update it.
+.Sh INTERFACE
+.Bl -tag -width indent
+.It Fn BACKLIGHT_GET_STATUS "device_t bus" "struct backlight_props *props"
+Driver fills the current brightless level and the optional supported levels.
+.It Fn BACKLIGHT_SET_STATUS "device_t bus" "struct backlight_props *props"
+Driver update the backlight level based on the brightness member of the props
+struct.
+.El
+.Sh FILES
+.Bl -tag -width "/dev/backlight/*"
+.It Pa /dev/backlight/*
+.Sh HISTORY
+The
+.Nm backlight
+interface first appear in
+.Fx 13.0 .
+The
+.Nm backlight
+driver and manual page was written by
+.An Emmanuel Vadot Aq Mt manu@FreeBSD.org .
diff --git a/sys/conf/files b/sys/conf/files
index 39a2a50e54af..2c6e30161a52 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -1312,6 +1312,8 @@ dev/ath/ath_rate/sample/sample.c optional ath_rate_sample \
dev/ath/ath_dfs/null/dfs_null.c optional ath \
compile-with "${NORMAL_C} -I$S/dev/ath"
#
+dev/backlight/backlight_if.m optional backlight
+dev/backlight/backlight.c optional backlight
dev/bce/if_bce.c optional bce
dev/bfe/if_bfe.c optional bfe
dev/bge/if_bge.c optional bge
diff --git a/sys/dev/backlight/backlight.c b/sys/dev/backlight/backlight.c
new file mode 100644
index 000000000000..19d273ae55d8
--- /dev/null
+++ b/sys/dev/backlight/backlight.c
@@ -0,0 +1,170 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/limits.h>
+#include <sys/bus.h>
+#include <sys/conf.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
+#include <sys/module.h>
+#include <sys/lock.h>
+#include <sys/sx.h>
+
+#include <dev/backlight/backlight.h>
+
+#include "backlight_if.h"
+
+static struct sx backlight_sx;
+static MALLOC_DEFINE(M_BACKLIGHT, "BACKLIGHT", "Backlight driver");
+static struct unrhdr *backlight_unit;
+
+struct backlight_softc {
+ struct cdev *cdev;
+ struct cdev *alias;
+ int unit;
+ device_t dev;
+ uint32_t cached_brightness;
+};
+
+static int
+backlight_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
+ int fflag, struct thread *td)
+{
+ struct backlight_softc *sc;
+ struct backlight_props props;
+ struct backlight_info info;
+ int error;
+
+ sc = dev->si_drv1;
+
+ switch (cmd) {
+ case BACKLIGHTGETSTATUS:
+ /* Call the driver function so it fills up the props */
+ bcopy(data, &props, sizeof(struct backlight_props));
+ error = BACKLIGHT_GET_STATUS(sc->dev, &props);
+ if (error == 0)
+ bcopy(&props, data, sizeof(struct backlight_props));
+ break;
+ case BACKLIGHTUPDATESTATUS:
+ bcopy(data, &props, sizeof(struct backlight_props));
+ if (props.brightness == sc->cached_brightness)
+ return (0);
+ error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props);
+ if (error == 0) {
+ bcopy(&props, data, sizeof(struct backlight_props));
+ sc->cached_brightness = props.brightness;
+ }
+ break;
+ case BACKLIGHTGETINFO:
+ memset(&info, 0, sizeof(info));
+ error = BACKLIGHT_GET_INFO(sc->dev, &info);
+ if (error == 0)
+ bcopy(&info, data, sizeof(struct backlight_info));
+ break;
+ }
+
+ return (error);
+}
+
+static struct cdevsw backlight_cdevsw = {
+ .d_version = D_VERSION,
+ .d_ioctl = backlight_ioctl,
+ .d_name = "backlight",
+};
+
+struct cdev *
+backlight_register(const char *name, device_t dev)
+{
+ struct make_dev_args args;
+ struct backlight_softc *sc;
+ struct backlight_props props;
+ int error;
+
+ sc = malloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO);
+
+ sx_xlock(&backlight_sx);
+ sc->unit = alloc_unr(backlight_unit);
+ sc->dev = dev;
+ make_dev_args_init(&args);
+ args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
+ args.mda_devsw = &backlight_cdevsw;
+ args.mda_uid = UID_ROOT;
+ args.mda_gid = GID_VIDEO;
+ args.mda_mode = 0660;
+ args.mda_si_drv1 = sc;
+ error = make_dev_s(&args, &sc->cdev, "backlight/backlight%d", sc->unit);
+
+ if (error != 0)
+ goto fail;
+
+ error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
+ &sc->alias, sc->cdev, "backlight/%s%d", name, sc->unit);
+ if (error != 0)
+ device_printf(dev, "Cannot register with alias %s%d\n", name,
+ sc->unit);
+
+ sx_xunlock(&backlight_sx);
+
+ error = BACKLIGHT_GET_STATUS(sc->dev, &props);
+ sc->cached_brightness = props.brightness;
+
+ return (sc->cdev);
+fail:
+ sx_xunlock(&backlight_sx);
+ return (NULL);
+}
+
+int
+backlight_destroy(struct cdev *dev)
+{
+ struct backlight_softc *sc;
+
+ sc = dev->si_drv1;
+ sx_xlock(&backlight_sx);
+ free_unr(backlight_unit, sc->unit);
+ destroy_dev(dev);
+ sx_xunlock(&backlight_sx);
+ return (0);
+}
+
+static void
+backlight_drvinit(void *unused)
+{
+
+ backlight_unit = new_unrhdr(0, INT_MAX, NULL);
+ sx_init(&backlight_sx, "Backlight sx");
+}
+
+SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL);
+MODULE_VERSION(backlight, 1);
diff --git a/sys/dev/backlight/backlight.h b/sys/dev/backlight/backlight.h
new file mode 100644
index 000000000000..571b3fc6be4e
--- /dev/null
+++ b/sys/dev/backlight/backlight.h
@@ -0,0 +1,33 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#include <sys/backlight.h>
+
+struct cdev *backlight_register(const char *name, device_t dev);
+int backlight_destroy(struct cdev *dev);
diff --git a/sys/dev/backlight/backlight_if.m b/sys/dev/backlight/backlight_if.m
new file mode 100644
index 000000000000..449032245629
--- /dev/null
+++ b/sys/dev/backlight/backlight_if.m
@@ -0,0 +1,66 @@
+#-
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+# SUCH DAMAGE.
+#
+# $FreeBSD$
+
+#include <dev/backlight/backlight.h>
+
+INTERFACE backlight;
+
+CODE {
+ static int
+ backlight_default_update_status(device_t dev, struct backlight_props *props)
+ {
+ return (EOPNOTSUPP);
+ }
+
+ static int
+ backlight_default_get_status(device_t dev, struct backlight_props *props)
+ {
+ return (EOPNOTSUPP);
+ }
+
+ static int
+ backlight_default_get_info(device_t dev, struct backlight_info *info)
+ {
+ return (EOPNOTSUPP);
+ }
+};
+
+METHOD int update_status {
+ device_t dev;
+ struct backlight_props *props;
+} DEFAULT backlight_default_update_status;
+
+METHOD int get_status {
+ device_t dev;
+ struct backlight_props *props;
+} DEFAULT backlight_default_get_status;
+
+METHOD int get_info {
+ device_t dev;
+ struct backlight_info *info;
+} DEFAULT backlight_default_get_info;
diff --git a/sys/modules/Makefile b/sys/modules/Makefile
index 305eec0dfdbc..376cbe112107 100644
--- a/sys/modules/Makefile
+++ b/sys/modules/Makefile
@@ -59,6 +59,7 @@ SUBDIR= \
ath_rate \
ath_pci \
${_autofs} \
+ backlight \
${_bce} \
${_bcm283x_clkman} \
${_bcm283x_pwm} \
diff --git a/sys/modules/backlight/Makefile b/sys/modules/backlight/Makefile
new file mode 100644
index 000000000000..1c49d7b55138
--- /dev/null
+++ b/sys/modules/backlight/Makefile
@@ -0,0 +1,13 @@
+# $FreeBSD$
+
+.PATH: ${SRCTOP}/sys/dev/backlight
+KMOD= backlight
+SRCS= backlight.c
+
+SRCS+= bus_if.h \
+ device_if.h \
+ opt_platform.h \
+ backlight_if.h \
+ backlight_if.c
+
+.include <bsd.kmod.mk>
diff --git a/sys/sys/backlight.h b/sys/sys/backlight.h
new file mode 100644
index 000000000000..4742d40e746c
--- /dev/null
+++ b/sys/sys/backlight.h
@@ -0,0 +1,61 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef __BACKLIGHT_H__
+#define __BACKLIGHT_H__
+
+#define BACKLIGHTMAXLEVELS 100
+
+struct backlight_props {
+ uint32_t brightness;
+ uint32_t nlevels;
+ uint32_t levels[BACKLIGHTMAXLEVELS];
+};
+
+enum backlight_info_type {
+ BACKLIGHT_TYPE_PANEL = 0,
+ BACKLIGHT_TYPE_KEYBOARD
+};
+
+#define BACKLIGHTMAXNAMELENGTH 64
+
+struct backlight_info {
+ char name[BACKLIGHTMAXNAMELENGTH];
+ enum backlight_info_type type;
+};
+
+/*
+ * ioctls
+ */
+
+#define BACKLIGHTGETSTATUS _IOWR('G', 0, struct backlight_props)
+#define BACKLIGHTUPDATESTATUS _IOWR('G', 1, struct backlight_props)
+#define BACKLIGHTGETINFO _IOWR('G', 2, struct backlight_info)
+
+#endif /* __BACKLIGHT_H__ */