]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/backlight/backlight.c
Move SYSCTL_ADD_PROC() to unlocked context in if_ure to avoid lock order reversal.
[FreeBSD/FreeBSD.git] / sys / dev / backlight / backlight.c
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                 break;
79         case BACKLIGHTUPDATESTATUS:
80                 bcopy(data, &props, sizeof(struct backlight_props));
81                 if (props.brightness == sc->cached_brightness)
82                         return (0);
83                 error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props);
84                 if (error == 0) {
85                         bcopy(&props, data, sizeof(struct backlight_props));
86                         sc->cached_brightness = props.brightness;
87                 }
88                 break;
89         case BACKLIGHTGETINFO:
90                 memset(&info, 0, sizeof(info));
91                 error = BACKLIGHT_GET_INFO(sc->dev, &info);
92                 if (error == 0)
93                         bcopy(&info, data, sizeof(struct backlight_info));
94                 break;
95         }
96
97         return (error);
98 }
99
100 static struct cdevsw backlight_cdevsw = {
101         .d_version =    D_VERSION,
102         .d_ioctl =      backlight_ioctl,
103         .d_name =       "backlight",
104 };
105
106 struct cdev *
107 backlight_register(const char *name, device_t dev)
108 {
109         struct make_dev_args args;
110         struct backlight_softc *sc;
111         struct backlight_props props;
112         int error;
113
114         sc = malloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO);
115
116         sx_xlock(&backlight_sx);
117         sc->unit = alloc_unr(backlight_unit);
118         sc->dev = dev;
119         make_dev_args_init(&args);
120         args.mda_flags = MAKEDEV_CHECKNAME | MAKEDEV_WAITOK;
121         args.mda_devsw = &backlight_cdevsw;
122         args.mda_uid = UID_ROOT;
123         args.mda_gid = GID_VIDEO;
124         args.mda_mode = 0660;
125         args.mda_si_drv1 = sc;
126         error = make_dev_s(&args, &sc->cdev, "backlight/backlight%d", sc->unit);
127
128         if (error != 0)
129                 goto fail;
130
131         error = make_dev_alias_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
132           &sc->alias, sc->cdev, "backlight/%s%d", name, sc->unit);
133         if (error != 0)
134                 device_printf(dev, "Cannot register with alias %s%d\n", name,
135                     sc->unit);
136
137         sx_xunlock(&backlight_sx);
138
139         error = BACKLIGHT_GET_STATUS(sc->dev, &props);
140         sc->cached_brightness = props.brightness;
141
142         return (sc->cdev);
143 fail:
144         sx_xunlock(&backlight_sx);
145         return (NULL);
146 }
147
148 int
149 backlight_destroy(struct cdev *dev)
150 {
151         struct backlight_softc *sc;
152
153         sc = dev->si_drv1;
154         sx_xlock(&backlight_sx);
155         free_unr(backlight_unit, sc->unit);
156         destroy_dev(dev);
157         sx_xunlock(&backlight_sx);
158         return (0);
159 }
160
161 static void
162 backlight_drvinit(void *unused)
163 {
164
165         backlight_unit = new_unrhdr(0, INT_MAX, NULL);
166         sx_init(&backlight_sx, "Backlight sx");
167 }
168
169 SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL);
170 MODULE_VERSION(backlight, 1);