1675aae73SEmmanuel Vadot /*- 2675aae73SEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3675aae73SEmmanuel Vadot * 4675aae73SEmmanuel Vadot * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org> 5675aae73SEmmanuel Vadot * 6675aae73SEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 7675aae73SEmmanuel Vadot * modification, are permitted provided that the following conditions 8675aae73SEmmanuel Vadot * are met: 9675aae73SEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright 10675aae73SEmmanuel Vadot * notice, this list of conditions and the following disclaimer. 11675aae73SEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright 12675aae73SEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the 13675aae73SEmmanuel Vadot * documentation and/or other materials provided with the distribution. 14675aae73SEmmanuel Vadot * 15675aae73SEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16675aae73SEmmanuel Vadot * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17675aae73SEmmanuel Vadot * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18675aae73SEmmanuel Vadot * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19675aae73SEmmanuel Vadot * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20675aae73SEmmanuel Vadot * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21675aae73SEmmanuel Vadot * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22675aae73SEmmanuel Vadot * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23675aae73SEmmanuel Vadot * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24675aae73SEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25675aae73SEmmanuel Vadot * SUCH DAMAGE. 26675aae73SEmmanuel Vadot * 27675aae73SEmmanuel Vadot * $FreeBSD$ 28675aae73SEmmanuel Vadot */ 29675aae73SEmmanuel Vadot 30675aae73SEmmanuel Vadot #include <sys/cdefs.h> 31675aae73SEmmanuel Vadot __FBSDID("$FreeBSD$"); 32675aae73SEmmanuel Vadot 33675aae73SEmmanuel Vadot #include <sys/param.h> 34675aae73SEmmanuel Vadot #include <sys/systm.h> 35675aae73SEmmanuel Vadot #include <sys/limits.h> 36675aae73SEmmanuel Vadot #include <sys/bus.h> 37675aae73SEmmanuel Vadot #include <sys/conf.h> 38675aae73SEmmanuel Vadot #include <sys/kernel.h> 39675aae73SEmmanuel Vadot #include <sys/malloc.h> 40675aae73SEmmanuel Vadot #include <sys/module.h> 41675aae73SEmmanuel Vadot #include <sys/lock.h> 42675aae73SEmmanuel Vadot #include <sys/sx.h> 43675aae73SEmmanuel Vadot 44675aae73SEmmanuel Vadot #include <dev/backlight/backlight.h> 45675aae73SEmmanuel Vadot 46675aae73SEmmanuel Vadot #include "backlight_if.h" 47675aae73SEmmanuel Vadot 48675aae73SEmmanuel Vadot static struct sx backlight_sx; 49675aae73SEmmanuel Vadot static MALLOC_DEFINE(M_BACKLIGHT, "BACKLIGHT", "Backlight driver"); 50675aae73SEmmanuel Vadot static struct unrhdr *backlight_unit; 51675aae73SEmmanuel Vadot 52675aae73SEmmanuel Vadot struct backlight_softc { 53675aae73SEmmanuel Vadot struct cdev *cdev; 54675aae73SEmmanuel Vadot struct cdev *alias; 55675aae73SEmmanuel Vadot int unit; 56675aae73SEmmanuel Vadot device_t dev; 57675aae73SEmmanuel Vadot uint32_t cached_brightness; 58675aae73SEmmanuel Vadot }; 59675aae73SEmmanuel Vadot 60675aae73SEmmanuel Vadot static int 61675aae73SEmmanuel Vadot backlight_ioctl(struct cdev *dev, u_long cmd, caddr_t data, 62675aae73SEmmanuel Vadot int fflag, struct thread *td) 63675aae73SEmmanuel Vadot { 64675aae73SEmmanuel Vadot struct backlight_softc *sc; 65675aae73SEmmanuel Vadot struct backlight_props props; 66675aae73SEmmanuel Vadot struct backlight_info info; 67675aae73SEmmanuel Vadot int error; 68675aae73SEmmanuel Vadot 69675aae73SEmmanuel Vadot sc = dev->si_drv1; 70675aae73SEmmanuel Vadot 71675aae73SEmmanuel Vadot switch (cmd) { 72675aae73SEmmanuel Vadot case BACKLIGHTGETSTATUS: 73675aae73SEmmanuel Vadot /* Call the driver function so it fills up the props */ 74675aae73SEmmanuel Vadot bcopy(data, &props, sizeof(struct backlight_props)); 75675aae73SEmmanuel Vadot error = BACKLIGHT_GET_STATUS(sc->dev, &props); 76*e26ef41fSEmmanuel Vadot if (error == 0) { 77675aae73SEmmanuel Vadot bcopy(&props, data, sizeof(struct backlight_props)); 78*e26ef41fSEmmanuel Vadot sc->cached_brightness = props.brightness; 79*e26ef41fSEmmanuel Vadot } 80675aae73SEmmanuel Vadot break; 81675aae73SEmmanuel Vadot case BACKLIGHTUPDATESTATUS: 82675aae73SEmmanuel Vadot bcopy(data, &props, sizeof(struct backlight_props)); 83675aae73SEmmanuel Vadot if (props.brightness == sc->cached_brightness) 84675aae73SEmmanuel Vadot return (0); 85675aae73SEmmanuel Vadot error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props); 86675aae73SEmmanuel Vadot if (error == 0) { 87675aae73SEmmanuel Vadot bcopy(&props, data, sizeof(struct backlight_props)); 88675aae73SEmmanuel Vadot sc->cached_brightness = props.brightness; 89675aae73SEmmanuel Vadot } 90675aae73SEmmanuel Vadot break; 91675aae73SEmmanuel Vadot case BACKLIGHTGETINFO: 92675aae73SEmmanuel Vadot memset(&info, 0, sizeof(info)); 93675aae73SEmmanuel Vadot error = BACKLIGHT_GET_INFO(sc->dev, &info); 94675aae73SEmmanuel Vadot if (error == 0) 95675aae73SEmmanuel Vadot bcopy(&info, data, sizeof(struct backlight_info)); 96675aae73SEmmanuel Vadot break; 97675aae73SEmmanuel Vadot } 98675aae73SEmmanuel Vadot 99675aae73SEmmanuel Vadot return (error); 100675aae73SEmmanuel Vadot } 101675aae73SEmmanuel Vadot 102675aae73SEmmanuel Vadot static struct cdevsw backlight_cdevsw = { 103675aae73SEmmanuel Vadot .d_version = D_VERSION, 104675aae73SEmmanuel Vadot .d_ioctl = backlight_ioctl, 105675aae73SEmmanuel Vadot .d_name = "backlight", 106675aae73SEmmanuel Vadot }; 107675aae73SEmmanuel Vadot 108675aae73SEmmanuel Vadot struct cdev * 109675aae73SEmmanuel Vadot backlight_register(const char *name, device_t dev) 110675aae73SEmmanuel Vadot { 111675aae73SEmmanuel Vadot struct make_dev_args args; 112675aae73SEmmanuel Vadot struct backlight_softc *sc; 113675aae73SEmmanuel Vadot struct backlight_props props; 114675aae73SEmmanuel Vadot int error; 115675aae73SEmmanuel Vadot 116675aae73SEmmanuel Vadot sc = malloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO); 117675aae73SEmmanuel Vadot 118675aae73SEmmanuel Vadot sx_xlock(&backlight_sx); 119675aae73SEmmanuel Vadot sc->unit = alloc_unr(backlight_unit); 120675aae73SEmmanuel Vadot sc->dev = dev; 121675aae73SEmmanuel Vadot make_dev_args_init(&args); 122675aae73SEmmanuel Vadot args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK; 123675aae73SEmmanuel Vadot args.mda_devsw = &backlight_cdevsw; 124675aae73SEmmanuel Vadot args.mda_uid = UID_ROOT; 125675aae73SEmmanuel Vadot args.mda_gid = GID_VIDEO; 126675aae73SEmmanuel Vadot args.mda_mode = 0660; 127675aae73SEmmanuel Vadot args.mda_si_drv1 = sc; 128675aae73SEmmanuel Vadot error = make_dev_s(&args, &sc->cdev, "backlight/backlight%d", sc->unit); 129675aae73SEmmanuel Vadot 130675aae73SEmmanuel Vadot if (error != 0) 131675aae73SEmmanuel Vadot goto fail; 132675aae73SEmmanuel Vadot 133675aae73SEmmanuel Vadot error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK, 134675aae73SEmmanuel Vadot &sc->alias, sc->cdev, "backlight/%s%d", name, sc->unit); 135675aae73SEmmanuel Vadot if (error != 0) 136675aae73SEmmanuel Vadot device_printf(dev, "Cannot register with alias %s%d\n", name, 137675aae73SEmmanuel Vadot sc->unit); 138675aae73SEmmanuel Vadot 139675aae73SEmmanuel Vadot sx_xunlock(&backlight_sx); 140675aae73SEmmanuel Vadot 141675aae73SEmmanuel Vadot error = BACKLIGHT_GET_STATUS(sc->dev, &props); 142675aae73SEmmanuel Vadot sc->cached_brightness = props.brightness; 143675aae73SEmmanuel Vadot 144675aae73SEmmanuel Vadot return (sc->cdev); 145675aae73SEmmanuel Vadot fail: 146675aae73SEmmanuel Vadot sx_xunlock(&backlight_sx); 147675aae73SEmmanuel Vadot return (NULL); 148675aae73SEmmanuel Vadot } 149675aae73SEmmanuel Vadot 150675aae73SEmmanuel Vadot int 151675aae73SEmmanuel Vadot backlight_destroy(struct cdev *dev) 152675aae73SEmmanuel Vadot { 153675aae73SEmmanuel Vadot struct backlight_softc *sc; 154675aae73SEmmanuel Vadot 155675aae73SEmmanuel Vadot sc = dev->si_drv1; 156675aae73SEmmanuel Vadot sx_xlock(&backlight_sx); 157675aae73SEmmanuel Vadot free_unr(backlight_unit, sc->unit); 158675aae73SEmmanuel Vadot destroy_dev(dev); 159675aae73SEmmanuel Vadot sx_xunlock(&backlight_sx); 160675aae73SEmmanuel Vadot return (0); 161675aae73SEmmanuel Vadot } 162675aae73SEmmanuel Vadot 163675aae73SEmmanuel Vadot static void 164675aae73SEmmanuel Vadot backlight_drvinit(void *unused) 165675aae73SEmmanuel Vadot { 166675aae73SEmmanuel Vadot 167675aae73SEmmanuel Vadot backlight_unit = new_unrhdr(0, INT_MAX, NULL); 168675aae73SEmmanuel Vadot sx_init(&backlight_sx, "Backlight sx"); 169675aae73SEmmanuel Vadot } 170675aae73SEmmanuel Vadot 171675aae73SEmmanuel Vadot SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL); 172675aae73SEmmanuel Vadot MODULE_VERSION(backlight, 1); 173