Index: share/man/man9/backlight.9 =================================================================== --- /dev/null +++ share/man/man9/backlight.9 @@ -0,0 +1,77 @@ +.\" Copyright (c) 2020 Emmanuel Vadot +.\" +.\" 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 September 30, 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 ".Pa /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 . Index: sys/conf/files =================================================================== --- sys/conf/files +++ sys/conf/files @@ -1312,6 +1312,8 @@ 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 Index: sys/dev/backlight/backlight.h =================================================================== --- /dev/null +++ sys/dev/backlight/backlight.h @@ -0,0 +1,33 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Emmanuel Vadot + * + * 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 + +struct cdev *backlight_register(const char *name, device_t dev); +int backlight_destroy(struct cdev *dev); Index: sys/dev/backlight/backlight.c =================================================================== --- /dev/null +++ sys/dev/backlight/backlight.c @@ -0,0 +1,163 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Emmanuel Vadot + * + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#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; + 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; + } + + 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); Index: sys/dev/backlight/backlight_if.m =================================================================== --- /dev/null +++ sys/dev/backlight/backlight_if.m @@ -0,0 +1,55 @@ +#- +# SPDX-License-Identifier: BSD-2-Clause-FreeBSD +# +# Copyright (c) 2020 Emmanuel Vadot +# +# 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 + +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); + } +}; + +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; Index: sys/modules/Makefile =================================================================== --- sys/modules/Makefile +++ sys/modules/Makefile @@ -59,6 +59,7 @@ ath_rate \ ath_pci \ ${_autofs} \ + backlight \ ${_bce} \ ${_bcm283x_clkman} \ ${_bcm283x_pwm} \ Index: sys/modules/backlight/Makefile =================================================================== --- /dev/null +++ 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 Index: sys/sys/backlight.h =================================================================== --- /dev/null +++ sys/sys/backlight.h @@ -0,0 +1,48 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2020 Emmanuel Vadot + * + * 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]; +}; + +/* + * ioctls + */ + +#define BACKLIGHTGETSTATUS _IOWR('G', 0, struct backlight_props) +#define BACKLIGHTUPDATESTATUS _IOWR('G', 1, struct backlight_props) + +#endif /* __BACKLIGHT_H__ */