Line 0
Link Here
|
|
|
1 |
/*- |
2 |
* Copyright (c) 2017 Henry Hu <henry.hu.sh@gmail.com> |
3 |
* All rights reserved. |
4 |
* |
5 |
* Redistribution and use in source and binary forms, with or without |
6 |
* modification, are permitted provided that the following conditions |
7 |
* are met: |
8 |
* 1. Redistributions of source code must retain the above copyright |
9 |
* notice, this list of conditions and the following disclaimer. |
10 |
* 2. Redistributions in binary form must reproduce the above copyright |
11 |
* notice, this list of conditions and the following disclaimer in the |
12 |
* documentation and/or other materials provided with the distribution. |
13 |
* |
14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
15 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
18 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 |
* SUCH DAMAGE. |
25 |
*/ |
26 |
|
27 |
#include <sys/cdefs.h> |
28 |
__FBSDID("$FreeBSD$"); |
29 |
|
30 |
/* |
31 |
* Driver for the ambient light sensor found on recent Asus laptops. Inspired |
32 |
* by the als project which implements the same functionality for Linux. |
33 |
* |
34 |
* <https://github.com/victorenator/als> |
35 |
* |
36 |
* May also work on other laptops. |
37 |
*/ |
38 |
|
39 |
#include "opt_acpi.h" |
40 |
#include <sys/param.h> |
41 |
#include <sys/kernel.h> |
42 |
#include <sys/module.h> |
43 |
#include <sys/bus.h> |
44 |
|
45 |
#include <contrib/dev/acpica/include/acpi.h> |
46 |
#include <contrib/dev/acpica/include/accommon.h> |
47 |
|
48 |
#include <dev/acpica/acpivar.h> |
49 |
|
50 |
#define _COMPONENT ACPI_OEM |
51 |
ACPI_MODULE_NAME("ASUS-ALS") |
52 |
|
53 |
struct acpi_asus_als_softc { |
54 |
device_t dev; |
55 |
ACPI_HANDLE handle; |
56 |
|
57 |
struct sysctl_ctx_list sysctl_ctx; |
58 |
struct sysctl_oid *sysctl_tree; |
59 |
}; |
60 |
|
61 |
ACPI_SERIAL_DECL(asus_als, "ACPI ASUS ambient light"); |
62 |
|
63 |
static int acpi_asus_als_probe(device_t dev); |
64 |
static int acpi_asus_als_attach(device_t dev); |
65 |
static int acpi_asus_als_detach(device_t dev); |
66 |
|
67 |
static int acpi_asus_als_sysctl(SYSCTL_HANDLER_ARGS); |
68 |
static int acpi_asus_als_sysctl_get(struct acpi_asus_als_softc *sc); |
69 |
|
70 |
static device_method_t acpi_asus_als_methods[] = { |
71 |
DEVMETHOD(device_probe, acpi_asus_als_probe), |
72 |
DEVMETHOD(device_attach, acpi_asus_als_attach), |
73 |
DEVMETHOD(device_detach, acpi_asus_als_detach), |
74 |
|
75 |
{ 0, 0 } |
76 |
}; |
77 |
|
78 |
static driver_t acpi_asus_als_driver = { |
79 |
"acpi_asus_als", |
80 |
acpi_asus_als_methods, |
81 |
sizeof(struct acpi_asus_als_softc) |
82 |
}; |
83 |
|
84 |
static devclass_t acpi_asus_als_devclass; |
85 |
|
86 |
DRIVER_MODULE(acpi_asus_als, acpi, acpi_asus_als_driver, |
87 |
acpi_asus_als_devclass, 0, 0); |
88 |
MODULE_DEPEND(acpi_asus_als, acpi, 1, 1, 1); |
89 |
|
90 |
static int |
91 |
acpi_asus_als_probe(device_t dev) |
92 |
{ |
93 |
static char *asus_ids[] = { "ACPI0008", NULL }; |
94 |
|
95 |
struct acpi_asus_als_softc *sc; |
96 |
char *rstr; |
97 |
|
98 |
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); |
99 |
|
100 |
if (acpi_disabled("asus_als")) |
101 |
return (ENXIO); |
102 |
|
103 |
/* Check if ambient light sensor is present. */ |
104 |
rstr = ACPI_ID_PROBE(device_get_parent(dev), dev, asus_ids); |
105 |
if (rstr == NULL) { |
106 |
return (ENXIO); |
107 |
} |
108 |
|
109 |
sc = device_get_softc(dev); |
110 |
sc->dev = dev; |
111 |
sc->handle = acpi_get_handle(dev); |
112 |
|
113 |
device_set_desc_copy(dev, "ASUS ambient light sensor"); |
114 |
|
115 |
return 0; |
116 |
} |
117 |
|
118 |
static int |
119 |
acpi_asus_als_attach(device_t dev) |
120 |
{ |
121 |
struct acpi_asus_als_softc *sc; |
122 |
struct acpi_softc *acpi_sc; |
123 |
|
124 |
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); |
125 |
|
126 |
sc = device_get_softc(dev); |
127 |
acpi_sc = acpi_device_get_parent_softc(dev); |
128 |
|
129 |
sysctl_ctx_init(&sc->sysctl_ctx); |
130 |
sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx, |
131 |
SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), |
132 |
OID_AUTO, "asus_als", CTLFLAG_RD, 0, ""); |
133 |
|
134 |
SYSCTL_ADD_PROC(&sc->sysctl_ctx, |
135 |
SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, |
136 |
"light", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_ANYBODY, |
137 |
sc, 0, acpi_asus_als_sysctl, "I", "Ambient light value"); |
138 |
|
139 |
return (0); |
140 |
} |
141 |
|
142 |
static int |
143 |
acpi_asus_als_detach(device_t dev) |
144 |
{ |
145 |
struct acpi_asus_als_softc *sc; |
146 |
|
147 |
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); |
148 |
|
149 |
sc = device_get_softc(dev); |
150 |
|
151 |
sysctl_ctx_free(&sc->sysctl_ctx); |
152 |
|
153 |
return (0); |
154 |
} |
155 |
|
156 |
static int |
157 |
acpi_asus_als_sysctl(SYSCTL_HANDLER_ARGS) |
158 |
{ |
159 |
struct acpi_asus_als_softc *sc; |
160 |
int value; |
161 |
int error = 0; |
162 |
|
163 |
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); |
164 |
|
165 |
sc = (struct acpi_asus_als_softc *)oidp->oid_arg1; |
166 |
|
167 |
ACPI_SERIAL_BEGIN(asus_als); |
168 |
|
169 |
value = acpi_asus_als_sysctl_get(sc); |
170 |
error = sysctl_handle_int(oidp, &value, 0, req); |
171 |
|
172 |
ACPI_SERIAL_END(asus_als); |
173 |
|
174 |
return (error); |
175 |
} |
176 |
|
177 |
static int |
178 |
acpi_asus_als_sysctl_get(struct acpi_asus_als_softc *sc) |
179 |
{ |
180 |
ACPI_BUFFER Buf; |
181 |
ACPI_OBJECT *Obj; |
182 |
int value = 0; |
183 |
|
184 |
ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); |
185 |
ACPI_SERIAL_ASSERT(asus_als); |
186 |
|
187 |
Buf.Pointer = NULL; |
188 |
Buf.Length = ACPI_ALLOCATE_BUFFER; |
189 |
|
190 |
/* Calls _ALI to get the value. */ |
191 |
AcpiEvaluateObject(sc->handle, "_ALI", NULL, &Buf); |
192 |
Obj = Buf.Pointer; |
193 |
|
194 |
if (Obj->Type != ACPI_TYPE_INTEGER) { |
195 |
device_printf(sc->dev, "_ALI returned non-integer\n"); |
196 |
} else { |
197 |
value = Obj->Integer.Value; |
198 |
} |
199 |
|
200 |
AcpiOsFree(Buf.Pointer); |
201 |
|
202 |
return (value); |
203 |
} |