2 * SPDX-License-Identifier: BSD-2-Clause
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
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/limits.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
40 #include <dev/backlight/backlight.h>
42 #include "backlight_if.h"
44 static struct sx backlight_sx;
45 static MALLOC_DEFINE(M_BACKLIGHT, "BACKLIGHT", "Backlight driver");
46 static struct unrhdr *backlight_unit;
48 struct backlight_softc {
53 uint32_t cached_brightness;
57 backlight_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
58 int fflag, struct thread *td)
60 struct backlight_softc *sc;
61 struct backlight_props props;
62 struct backlight_info info;
68 case BACKLIGHTGETSTATUS:
69 /* Call the driver function so it fills up the props */
70 bcopy(data, &props, sizeof(struct backlight_props));
71 error = BACKLIGHT_GET_STATUS(sc->dev, &props);
73 bcopy(&props, data, sizeof(struct backlight_props));
74 sc->cached_brightness = props.brightness;
77 case BACKLIGHTUPDATESTATUS:
78 bcopy(data, &props, sizeof(struct backlight_props));
79 if (props.brightness == sc->cached_brightness)
81 error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props);
83 bcopy(&props, data, sizeof(struct backlight_props));
84 sc->cached_brightness = props.brightness;
87 case BACKLIGHTGETINFO:
88 memset(&info, 0, sizeof(info));
89 error = BACKLIGHT_GET_INFO(sc->dev, &info);
91 bcopy(&info, data, sizeof(struct backlight_info));
98 static struct cdevsw backlight_cdevsw = {
99 .d_version = D_VERSION,
100 .d_ioctl = backlight_ioctl,
101 .d_name = "backlight",
105 backlight_register(const char *name, device_t dev)
107 struct make_dev_args args;
108 struct backlight_softc *sc;
109 struct backlight_props props;
112 sc = malloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO);
114 sx_xlock(&backlight_sx);
115 sc->unit = alloc_unr(backlight_unit);
117 make_dev_args_init(&args);
118 args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
119 args.mda_devsw = &backlight_cdevsw;
120 args.mda_uid = UID_ROOT;
121 args.mda_gid = GID_VIDEO;
122 args.mda_mode = 0660;
123 args.mda_si_drv1 = sc;
124 error = make_dev_s(&args, &sc->cdev, "backlight/backlight%d", sc->unit);
129 error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
130 &sc->alias, sc->cdev, "backlight/%s%d", name, sc->unit);
132 device_printf(dev, "Cannot register with alias %s%d\n", name,
135 sx_xunlock(&backlight_sx);
137 error = BACKLIGHT_GET_STATUS(sc->dev, &props);
138 sc->cached_brightness = props.brightness;
142 sx_xunlock(&backlight_sx);
147 backlight_destroy(struct cdev *dev)
149 struct backlight_softc *sc;
152 sx_xlock(&backlight_sx);
153 free_unr(backlight_unit, sc->unit);
155 sx_xunlock(&backlight_sx);
160 backlight_drvinit(void *unused)
163 backlight_unit = new_unrhdr(0, INT_MAX, NULL);
164 sx_init(&backlight_sx, "Backlight sx");
167 SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL);
168 MODULE_VERSION(backlight, 1);