]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/backlight/backlight.c
awk: Merge upstream 2nd Edition Awk Book
[FreeBSD/FreeBSD.git] / sys / dev / backlight / backlight.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Emmanuel Vadot <manu@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/limits.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/sx.h>
39
40 #include <dev/backlight/backlight.h>
41
42 #include "backlight_if.h"
43
44 static struct sx backlight_sx;
45 static MALLOC_DEFINE(M_BACKLIGHT, "BACKLIGHT", "Backlight driver");
46 static struct unrhdr *backlight_unit;
47
48 struct backlight_softc {
49         struct cdev *cdev;
50         struct cdev *alias;
51         int unit;
52         device_t dev;
53         uint32_t cached_brightness;
54 };
55
56 static int
57 backlight_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
58     int fflag, struct thread *td)
59 {
60         struct backlight_softc *sc;
61         struct backlight_props props;
62         struct backlight_info info;
63         int error;
64
65         sc = dev->si_drv1;
66
67         switch (cmd) {
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);
72                 if (error == 0) {
73                         bcopy(&props, data, sizeof(struct backlight_props));
74                         sc->cached_brightness = props.brightness;
75                 }
76                 break;
77         case BACKLIGHTUPDATESTATUS:
78                 bcopy(data, &props, sizeof(struct backlight_props));
79                 if (props.brightness == sc->cached_brightness)
80                         return (0);
81                 error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props);
82                 if (error == 0) {
83                         bcopy(&props, data, sizeof(struct backlight_props));
84                         sc->cached_brightness = props.brightness;
85                 }
86                 break;
87         case BACKLIGHTGETINFO:
88                 memset(&info, 0, sizeof(info));
89                 error = BACKLIGHT_GET_INFO(sc->dev, &info);
90                 if (error == 0)
91                         bcopy(&info, data, sizeof(struct backlight_info));
92                 break;
93         }
94
95         return (error);
96 }
97
98 static struct cdevsw backlight_cdevsw = {
99         .d_version =    D_VERSION,
100         .d_ioctl =      backlight_ioctl,
101         .d_name =       "backlight",
102 };
103
104 struct cdev *
105 backlight_register(const char *name, device_t dev)
106 {
107         struct make_dev_args args;
108         struct backlight_softc *sc;
109         struct backlight_props props;
110         int error;
111
112         sc = malloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO);
113
114         sx_xlock(&backlight_sx);
115         sc->unit = alloc_unr(backlight_unit);
116         sc->dev = dev;
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);
125
126         if (error != 0)
127                 goto fail;
128
129         error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
130           &sc->alias, sc->cdev, "backlight/%s%d", name, sc->unit);
131         if (error != 0)
132                 device_printf(dev, "Cannot register with alias %s%d\n", name,
133                     sc->unit);
134
135         sx_xunlock(&backlight_sx);
136
137         error = BACKLIGHT_GET_STATUS(sc->dev, &props);
138         sc->cached_brightness = props.brightness;
139
140         return (sc->cdev);
141 fail:
142         sx_xunlock(&backlight_sx);
143         return (NULL);
144 }
145
146 int
147 backlight_destroy(struct cdev *dev)
148 {
149         struct backlight_softc *sc;
150
151         sc = dev->si_drv1;
152         sx_xlock(&backlight_sx);
153         free_unr(backlight_unit, sc->unit);
154         destroy_dev(dev);
155         sx_xunlock(&backlight_sx);
156         return (0);
157 }
158
159 static void
160 backlight_drvinit(void *unused)
161 {
162
163         backlight_unit = new_unrhdr(0, INT_MAX, NULL);
164         sx_init(&backlight_sx, "Backlight sx");
165 }
166
167 SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL);
168 MODULE_VERSION(backlight, 1);