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