2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/limits.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
44 #include <dev/backlight/backlight.h>
46 #include "backlight_if.h"
48 static struct sx backlight_sx;
49 static MALLOC_DEFINE(M_BACKLIGHT, "BACKLIGHT", "Backlight driver");
50 static struct unrhdr *backlight_unit;
52 struct backlight_softc {
57 uint32_t cached_brightness;
61 backlight_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
62 int fflag, struct thread *td)
64 struct backlight_softc *sc;
65 struct backlight_props props;
66 struct backlight_info info;
72 case BACKLIGHTGETSTATUS:
73 /* Call the driver function so it fills up the props */
74 bcopy(data, &props, sizeof(struct backlight_props));
75 error = BACKLIGHT_GET_STATUS(sc->dev, &props);
77 bcopy(&props, data, sizeof(struct backlight_props));
79 case BACKLIGHTUPDATESTATUS:
80 bcopy(data, &props, sizeof(struct backlight_props));
81 if (props.brightness == sc->cached_brightness)
83 error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props);
85 bcopy(&props, data, sizeof(struct backlight_props));
86 sc->cached_brightness = props.brightness;
89 case BACKLIGHTGETINFO:
90 memset(&info, 0, sizeof(info));
91 error = BACKLIGHT_GET_INFO(sc->dev, &info);
93 bcopy(&info, data, sizeof(struct backlight_info));
100 static struct cdevsw backlight_cdevsw = {
101 .d_version = D_VERSION,
102 .d_ioctl = backlight_ioctl,
103 .d_name = "backlight",
107 backlight_register(const char *name, device_t dev)
109 struct make_dev_args args;
110 struct backlight_softc *sc;
111 struct backlight_props props;
114 sc = malloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO);
116 sx_xlock(&backlight_sx);
117 sc->unit = alloc_unr(backlight_unit);
119 make_dev_args_init(&args);
120 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
121 args.mda_devsw = &backlight_cdevsw;
122 args.mda_uid = UID_ROOT;
123 args.mda_gid = GID_VIDEO;
124 args.mda_mode = 0660;
125 args.mda_si_drv1 = sc;
126 error = make_dev_s(&args, &sc->cdev, "backlight/backlight%d", sc->unit);
131 error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
132 &sc->alias, sc->cdev, "backlight/%s%d", name, sc->unit);
134 device_printf(dev, "Cannot register with alias %s%d\n", name,
137 sx_xunlock(&backlight_sx);
139 error = BACKLIGHT_GET_STATUS(sc->dev, &props);
140 sc->cached_brightness = props.brightness;
144 sx_xunlock(&backlight_sx);
149 backlight_destroy(struct cdev *dev)
151 struct backlight_softc *sc;
154 sx_xlock(&backlight_sx);
155 free_unr(backlight_unit, sc->unit);
157 sx_xunlock(&backlight_sx);
162 backlight_drvinit(void *unused)
165 backlight_unit = new_unrhdr(0, INT_MAX, NULL);
166 sx_init(&backlight_sx, "Backlight sx");
169 SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL);
170 MODULE_VERSION(backlight, 1);