xref: /freebsd/sys/dev/backlight/backlight.c (revision 1dfcff294e44d4b45813288ef4095c36abb22f0e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/limits.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/lock.h>
42 #include <sys/sx.h>
43 
44 #include <dev/backlight/backlight.h>
45 
46 #include "backlight_if.h"
47 
48 static struct sx backlight_sx;
49 static MALLOC_DEFINE(M_BACKLIGHT, "BACKLIGHT", "Backlight driver");
50 static struct unrhdr *backlight_unit;
51 
52 struct backlight_softc {
53 	struct cdev *cdev;
54 	struct cdev *alias;
55 	int unit;
56 	device_t dev;
57 	uint32_t cached_brightness;
58 };
59 
60 static int
61 backlight_ioctl(struct cdev *dev, u_long cmd, caddr_t data,
62     int fflag, struct thread *td)
63 {
64 	struct backlight_softc *sc;
65 	struct backlight_props props;
66 	struct backlight_info info;
67 	int error;
68 
69 	sc = dev->si_drv1;
70 
71 	switch (cmd) {
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);
76 		if (error == 0) {
77 			bcopy(&props, data, sizeof(struct backlight_props));
78 			sc->cached_brightness = props.brightness;
79 		}
80 		break;
81 	case BACKLIGHTUPDATESTATUS:
82 		bcopy(data, &props, sizeof(struct backlight_props));
83 		if (props.brightness == sc->cached_brightness)
84 			return (0);
85 		error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props);
86 		if (error == 0) {
87 			bcopy(&props, data, sizeof(struct backlight_props));
88 			sc->cached_brightness = props.brightness;
89 		}
90 		break;
91 	case BACKLIGHTGETINFO:
92 		memset(&info, 0, sizeof(info));
93 		error = BACKLIGHT_GET_INFO(sc->dev, &info);
94 		if (error == 0)
95 			bcopy(&info, data, sizeof(struct backlight_info));
96 		break;
97 	}
98 
99 	return (error);
100 }
101 
102 static struct cdevsw backlight_cdevsw = {
103 	.d_version =	D_VERSION,
104 	.d_ioctl =	backlight_ioctl,
105 	.d_name =	"backlight",
106 };
107 
108 struct cdev *
109 backlight_register(const char *name, device_t dev)
110 {
111 	struct make_dev_args args;
112 	struct backlight_softc *sc;
113 	struct backlight_props props;
114 	int error;
115 
116 	sc = malloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO);
117 
118 	sx_xlock(&backlight_sx);
119 	sc->unit = alloc_unr(backlight_unit);
120 	sc->dev = dev;
121 	make_dev_args_init(&args);
122 	args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
123 	args.mda_devsw = &backlight_cdevsw;
124 	args.mda_uid = UID_ROOT;
125 	args.mda_gid = GID_VIDEO;
126 	args.mda_mode = 0660;
127 	args.mda_si_drv1 = sc;
128 	error = make_dev_s(&args, &sc->cdev, "backlight/backlight%d", sc->unit);
129 
130 	if (error != 0)
131 		goto fail;
132 
133 	error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
134 	  &sc->alias, sc->cdev, "backlight/%s%d", name, sc->unit);
135 	if (error != 0)
136 		device_printf(dev, "Cannot register with alias %s%d\n", name,
137 		    sc->unit);
138 
139 	sx_xunlock(&backlight_sx);
140 
141 	error = BACKLIGHT_GET_STATUS(sc->dev, &props);
142 	sc->cached_brightness = props.brightness;
143 
144 	return (sc->cdev);
145 fail:
146 	sx_xunlock(&backlight_sx);
147 	return (NULL);
148 }
149 
150 int
151 backlight_destroy(struct cdev *dev)
152 {
153 	struct backlight_softc *sc;
154 
155 	sc = dev->si_drv1;
156 	sx_xlock(&backlight_sx);
157 	free_unr(backlight_unit, sc->unit);
158 	destroy_dev(dev);
159 	sx_xunlock(&backlight_sx);
160 	return (0);
161 }
162 
163 static void
164 backlight_drvinit(void *unused)
165 {
166 
167 	backlight_unit = new_unrhdr(0, INT_MAX, NULL);
168 	sx_init(&backlight_sx, "Backlight sx");
169 }
170 
171 SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL);
172 MODULE_VERSION(backlight, 1);
173