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/param.h>
31 #include <sys/systm.h>
32 #include <sys/limits.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/device.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/lock.h>
40
41 #include <dev/misc/backlight/backlight.h>
42
43 #include "backlight_if.h"
44
45 static struct lock backlight_lock;
46 static MALLOC_DEFINE(M_BACKLIGHT, "BACKLIGHT", "Backlight driver");
47 static struct unrhdr *backlight_unit;
48
49 struct backlight_softc {
50 struct cdev *cdev;
51 struct cdev *alias;
52 int unit;
53 device_t dev;
54 uint32_t cached_brightness;
55 };
56
57 static int
backlight_ioctl(struct dev_ioctl_args * ap)58 backlight_ioctl(struct dev_ioctl_args *ap)
59 {
60 struct backlight_softc *sc;
61 struct backlight_props props;
62 struct backlight_info info;
63 cdev_t dev = ap->a_head.a_dev;
64 int error = 0;
65
66 sc = dev->si_drv1;
67
68 switch (ap->a_cmd) {
69 case BACKLIGHTGETSTATUS:
70 /* Call the driver function so it fills up the props */
71 bcopy(ap->a_data, &props, sizeof(struct backlight_props));
72 error = BACKLIGHT_GET_STATUS(sc->dev, &props);
73 if (error == 0) {
74 bcopy(&props, ap->a_data, sizeof(struct backlight_props));
75 sc->cached_brightness = props.brightness;
76 }
77 break;
78 case BACKLIGHTUPDATESTATUS:
79 bcopy(ap->a_data, &props, sizeof(struct backlight_props));
80 if (props.brightness == sc->cached_brightness)
81 return (0);
82 error = BACKLIGHT_UPDATE_STATUS(sc->dev, &props);
83 if (error == 0) {
84 bcopy(&props, ap->a_data, sizeof(struct backlight_props));
85 sc->cached_brightness = props.brightness;
86 }
87 break;
88 case BACKLIGHTGETINFO:
89 memset(&info, 0, sizeof(info));
90 error = BACKLIGHT_GET_INFO(sc->dev, &info);
91 if (error == 0)
92 bcopy(&info, ap->a_data, sizeof(struct backlight_info));
93 break;
94 }
95
96 return (error);
97 }
98
99 static struct dev_ops backlight_cdevsw = {
100 .head = { .name = "backlight", .flags = D_MPSAFE },
101 .d_ioctl = backlight_ioctl,
102 };
103
104 struct cdev *
backlight_register(const char * name,device_t dev)105 backlight_register(const char *name, device_t dev)
106 {
107 struct backlight_softc *sc;
108 struct backlight_props props;
109 int error;
110
111 sc = kmalloc(sizeof(*sc), M_BACKLIGHT, M_WAITOK | M_ZERO);
112
113 lockmgr(&backlight_lock, LK_EXCLUSIVE);
114 sc->unit = alloc_unr(backlight_unit);
115 sc->dev = dev;
116 make_dev(&backlight_cdevsw, sc->unit, UID_ROOT, GID_VIDEO,
117 0660, "backlight/backlight%d", sc->unit);
118 sc->cdev->si_drv1 = sc;
119 make_dev_alias(sc->cdev, "backlight/%s%d", name, sc->unit);
120
121 lockmgr(&backlight_lock, LK_RELEASE);
122
123 error = BACKLIGHT_GET_STATUS(sc->dev, &props);
124 sc->cached_brightness = props.brightness;
125
126 return (sc->cdev);
127 }
128
129 int
backlight_destroy(struct cdev * dev)130 backlight_destroy(struct cdev *dev)
131 {
132 struct backlight_softc *sc;
133
134 sc = dev->si_drv1;
135 lockmgr(&backlight_lock, LK_EXCLUSIVE);
136 free_unr(backlight_unit, sc->unit);
137 destroy_dev(dev);
138 lockmgr(&backlight_lock, LK_RELEASE);
139 return (0);
140 }
141
142 static void
backlight_drvinit(void * unused)143 backlight_drvinit(void *unused)
144 {
145
146 backlight_unit = new_unrhdr(0, INT_MAX, NULL);
147 lockuninit(&backlight_lock);
148 }
149
150 SYSINIT(backlightdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, backlight_drvinit, NULL);
151 MODULE_VERSION(backlight, 1);
152