Author: manu
Date: Fri Oct  2 18:18:01 2020
New Revision: 366369
URL: https://svnweb.freebsd.org/changeset/base/366369

Log:
  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

Added:
  head/share/man/man9/backlight.9   (contents, props changed)
  head/sys/dev/backlight/
  head/sys/dev/backlight/backlight.c   (contents, props changed)
  head/sys/dev/backlight/backlight.h   (contents, props changed)
  head/sys/dev/backlight/backlight_if.m   (contents, props changed)
  head/sys/modules/backlight/
  head/sys/modules/backlight/Makefile   (contents, props changed)
  head/sys/sys/backlight.h   (contents, props changed)
Modified:
  head/share/man/man9/Makefile
  head/sys/conf/files
  head/sys/modules/Makefile

Modified: head/share/man/man9/Makefile
==============================================================================
--- head/share/man/man9/Makefile        Fri Oct  2 17:50:22 2020        
(r366368)
+++ head/share/man/man9/Makefile        Fri Oct  2 18:18:01 2020        
(r366369)
@@ -10,6 +10,7 @@ MAN=  accept_filter.9 \
        alq.9 \
        altq.9 \
        atomic.9 \
+       backlight.9 \
        bhnd.9 \
        bhnd_erom.9 \
        bios.9 \

Added: head/share/man/man9/backlight.9
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/share/man/man9/backlight.9     Fri Oct  2 18:18:01 2020        
(r366369)
@@ -0,0 +1,77 @@
+.\" Copyright (c) 2020 Emmanuel Vadot <m...@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 m...@freebsd.org .

Modified: head/sys/conf/files
==============================================================================
--- head/sys/conf/files Fri Oct  2 17:50:22 2020        (r366368)
+++ head/sys/conf/files Fri Oct  2 18:18:01 2020        (r366369)
@@ -1312,6 +1312,8 @@ dev/ath/ath_rate/sample/sample.c  optional ath_rate_sam
 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

Added: head/sys/dev/backlight/backlight.c
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/dev/backlight/backlight.c  Fri Oct  2 18:18:01 2020        
(r366369)
@@ -0,0 +1,170 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2020 Emmanuel Vadot <m...@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);

Added: head/sys/dev/backlight/backlight.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/dev/backlight/backlight.h  Fri Oct  2 18:18:01 2020        
(r366369)
@@ -0,0 +1,33 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2020 Emmanuel Vadot <m...@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);

Added: head/sys/dev/backlight/backlight_if.m
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/dev/backlight/backlight_if.m       Fri Oct  2 18:18:01 2020        
(r366369)
@@ -0,0 +1,66 @@
+#-
+# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+#
+# Copyright (c) 2020 Emmanuel Vadot <m...@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;

Modified: head/sys/modules/Makefile
==============================================================================
--- head/sys/modules/Makefile   Fri Oct  2 17:50:22 2020        (r366368)
+++ head/sys/modules/Makefile   Fri Oct  2 18:18:01 2020        (r366369)
@@ -59,6 +59,7 @@ SUBDIR=       \
        ath_rate \
        ath_pci \
        ${_autofs} \
+       backlight \
        ${_bce} \
        ${_bcm283x_clkman} \
        ${_bcm283x_pwm} \

Added: head/sys/modules/backlight/Makefile
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/modules/backlight/Makefile Fri Oct  2 18:18:01 2020        
(r366369)
@@ -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>

Added: head/sys/sys/backlight.h
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/sys/sys/backlight.h    Fri Oct  2 18:18:01 2020        (r366369)
@@ -0,0 +1,61 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright (c) 2020 Emmanuel Vadot <m...@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__ */
_______________________________________________
svn-src-all@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to