1 /* $NetBSD: sony_acpi.c,v 1.24 2021/01/29 15:49:55 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: sony_acpi.c,v 1.24 2021/01/29 15:49:55 thorpej Exp $");
33
34 #include <sys/param.h>
35 #include <sys/sysctl.h>
36 #include <sys/systm.h>
37
38 #include <dev/acpi/acpireg.h>
39 #include <dev/acpi/acpivar.h>
40
41 #define _COMPONENT ACPI_RESOURCE_COMPONENT
42 ACPI_MODULE_NAME ("sony_acpi")
43
44 #define SONY_NOTIFY_FnKeyEvent 0x92
45 #define SONY_NOTIFY_BrightnessDownPressed 0x85
46 #define SONY_NOTIFY_BrightnessDownReleased 0x05
47 #define SONY_NOTIFY_BrightnessUpPressed 0x86
48 #define SONY_NOTIFY_BrightnessUpReleased 0x06
49 #define SONY_NOTIFY_DisplaySwitchPressed 0x87
50 #define SONY_NOTIFY_DisplaySwitchReleased 0x07
51 #define SONY_NOTIFY_ZoomPressed 0x8a
52 #define SONY_NOTIFY_ZoomReleased 0x0a
53 #define SONY_NOTIFY_SuspendPressed 0x8c
54 #define SONY_NOTIFY_SuspendReleased 0x0c
55
56 struct sony_acpi_softc {
57 device_t sc_dev;
58 struct sysctllog *sc_log;
59 struct acpi_devnode *sc_node;
60
61 #define SONY_PSW_SLEEP 0
62 #define SONY_PSW_DISPLAY_CYCLE 1
63 #define SONY_PSW_ZOOM 2
64 #define SONY_PSW_LAST 3
65 struct sysmon_pswitch sc_smpsw[SONY_PSW_LAST];
66 int sc_smpsw_valid;
67
68 #define SONY_ACPI_QUIRK_FNINIT 0x01
69 int sc_quirks;
70 bool sc_has_pic;
71
72 struct sony_acpi_pmstate {
73 ACPI_INTEGER brt;
74 } sc_pmstate;
75 };
76
77 static const struct device_compatible_entry compat_data[] = {
78 { .compat = "SNY5001" },
79 DEVICE_COMPAT_EOL
80 };
81
82 static int sony_acpi_match(device_t, cfdata_t, void *);
83 static void sony_acpi_attach(device_t, device_t, void *);
84 static ACPI_STATUS sony_acpi_eval_set_integer(ACPI_HANDLE, const char *,
85 ACPI_INTEGER, ACPI_INTEGER *);
86 static void sony_acpi_quirk_setup(struct sony_acpi_softc *);
87 static void sony_acpi_notify_handler(ACPI_HANDLE, uint32_t, void *);
88 static bool sony_acpi_suspend(device_t, const pmf_qual_t *);
89 static bool sony_acpi_resume(device_t, const pmf_qual_t *);
90 static void sony_acpi_brightness_down(device_t);
91 static void sony_acpi_brightness_up(device_t);
92 static ACPI_STATUS sony_acpi_find_pic(ACPI_HANDLE, uint32_t, void *, void **);
93
94 CFATTACH_DECL_NEW(sony_acpi, sizeof(struct sony_acpi_softc),
95 sony_acpi_match, sony_acpi_attach, NULL, NULL);
96
97 static int
sony_acpi_match(device_t parent,cfdata_t match,void * aux)98 sony_acpi_match(device_t parent, cfdata_t match, void *aux)
99 {
100 struct acpi_attach_args *aa = aux;
101
102 return acpi_compatible_match(aa, compat_data);
103 }
104
105 static int
sony_sysctl_helper(SYSCTLFN_ARGS)106 sony_sysctl_helper(SYSCTLFN_ARGS)
107 {
108 struct sysctlnode node;
109 ACPI_INTEGER acpi_val;
110 ACPI_STATUS rv;
111 int val, old_val, error;
112 char buf[SYSCTL_NAMELEN + 1], *ptr;
113 struct sony_acpi_softc *sc = rnode->sysctl_data;
114
115 (void)snprintf(buf, sizeof(buf), "G%s", rnode->sysctl_name);
116 for (ptr = buf; *ptr; ptr++)
117 *ptr = toupper((unsigned char)*ptr);
118
119 rv = acpi_eval_integer(sc->sc_node->ad_handle, buf, &acpi_val);
120 if (ACPI_FAILURE(rv)) {
121 #ifdef DIAGNOSTIC
122 printf("%s: couldn't get `%s'\n", device_xname(sc->sc_dev),
123 buf);
124 #endif
125 return EIO;
126 }
127 val = old_val = acpi_val;
128
129 node = *rnode;
130 node.sysctl_data = &val;
131
132 error = sysctl_lookup(SYSCTLFN_CALL(&node));
133 if (error || newp == NULL)
134 return error;
135
136 buf[0] = 'S';
137 acpi_val = val;
138 rv = sony_acpi_eval_set_integer(sc->sc_node->ad_handle, buf,
139 acpi_val, NULL);
140 if (ACPI_FAILURE(rv)) {
141 #ifdef DIAGNOSTIC
142 printf("%s: couldn't set `%s' to %d\n",
143 device_xname(sc->sc_dev), buf, val);
144 #endif
145 return EIO;
146 }
147 return 0;
148 }
149
150 static ACPI_STATUS
sony_walk_cb(ACPI_HANDLE hnd,uint32_t v,void * context,void ** status)151 sony_walk_cb(ACPI_HANDLE hnd, uint32_t v, void *context, void **status)
152 {
153 struct sony_acpi_softc *sc = (void *)context;
154 const struct sysctlnode *node, *snode;
155 const char *name = acpi_name(hnd);
156 ACPI_INTEGER acpi_val;
157 char buf[SYSCTL_NAMELEN + 1], *ptr;
158 int rv;
159
160 if ((name = strrchr(name, '.')) == NULL)
161 return AE_OK;
162
163 name++;
164 if ((*name != 'G') && (*name != 'S'))
165 return AE_OK;
166
167 (void)strlcpy(buf, name, sizeof(buf));
168 *buf = 'G';
169
170 /*
171 * We assume that if the 'get' of the name as an integer is
172 * successful it is ok.
173 */
174 if (acpi_eval_integer(sc->sc_node->ad_handle, buf, &acpi_val))
175 return AE_OK;
176
177 for (ptr = buf; *ptr; ptr++)
178 *ptr = tolower(*ptr);
179
180 if ((rv = sysctl_createv(&sc->sc_log, 0, NULL, &snode, 0,
181 CTLTYPE_NODE, device_xname(sc->sc_dev),
182 SYSCTL_DESCR("sony controls"),
183 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
184 goto out;
185
186 if ((rv = sysctl_createv(&sc->sc_log, 0, &snode, &node,
187 CTLFLAG_READWRITE, CTLTYPE_INT, buf + 1, NULL,
188 sony_sysctl_helper, 0, (void *)sc, 0, CTL_CREATE, CTL_EOL)) != 0)
189 goto out;
190
191 out:
192 #ifdef DIAGNOSTIC
193 if (rv)
194 printf("%s: sysctl_createv failed (rv = %d)\n",
195 device_xname(sc->sc_dev), rv);
196 #endif
197 return AE_OK;
198 }
199
200 ACPI_STATUS
sony_acpi_eval_set_integer(ACPI_HANDLE handle,const char * path,ACPI_INTEGER val,ACPI_INTEGER * valp)201 sony_acpi_eval_set_integer(ACPI_HANDLE handle, const char *path,
202 ACPI_INTEGER val, ACPI_INTEGER *valp)
203 {
204 ACPI_STATUS rv;
205 ACPI_BUFFER buf;
206 ACPI_OBJECT param, ret_val;
207 ACPI_OBJECT_LIST params;
208
209 if (handle == NULL)
210 handle = ACPI_ROOT_OBJECT;
211
212 params.Count = 1;
213 params.Pointer = ¶m;
214
215 param.Type = ACPI_TYPE_INTEGER;
216 param.Integer.Value = val;
217
218 buf.Pointer = &ret_val;
219 buf.Length = sizeof(ret_val);
220
221 rv = AcpiEvaluateObjectTyped(handle, path, ¶ms, &buf,
222 ACPI_TYPE_INTEGER);
223
224 if (ACPI_SUCCESS(rv) && valp)
225 *valp = ret_val.Integer.Value;
226
227 return rv;
228 }
229
230 static void
sony_acpi_attach(device_t parent,device_t self,void * aux)231 sony_acpi_attach(device_t parent, device_t self, void *aux)
232 {
233 struct sony_acpi_softc *sc = device_private(self);
234 struct acpi_attach_args *aa = aux;
235 ACPI_STATUS rv;
236 int i;
237
238 aprint_naive(": Sony Miscellaneous Controller\n");
239 aprint_normal(": Sony Miscellaneous Controller\n");
240
241 sc->sc_node = aa->aa_node;
242 sc->sc_dev = self;
243
244 rv = AcpiWalkNamespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, 100,
245 sony_acpi_find_pic, NULL, sc, NULL);
246 if (ACPI_FAILURE(rv))
247 aprint_error_dev(self, "couldn't walk namespace: %s\n",
248 AcpiFormatException(rv));
249
250 /*
251 * If we don't find an SNY6001 device, assume that we need the
252 * Fn key initialization sequence.
253 */
254 if (sc->sc_has_pic == false)
255 sc->sc_quirks |= SONY_ACPI_QUIRK_FNINIT;
256
257 sony_acpi_quirk_setup(sc);
258
259 /* Configure suspend button and hotkeys */
260 sc->sc_smpsw[SONY_PSW_SLEEP].smpsw_name = device_xname(self);
261 sc->sc_smpsw[SONY_PSW_SLEEP].smpsw_type = PSWITCH_TYPE_SLEEP;
262 sc->sc_smpsw[SONY_PSW_DISPLAY_CYCLE].smpsw_name =
263 PSWITCH_HK_DISPLAY_CYCLE;
264 sc->sc_smpsw[SONY_PSW_DISPLAY_CYCLE].smpsw_type = PSWITCH_TYPE_HOTKEY;
265 sc->sc_smpsw[SONY_PSW_ZOOM].smpsw_name = PSWITCH_HK_ZOOM_BUTTON;
266 sc->sc_smpsw[SONY_PSW_ZOOM].smpsw_type = PSWITCH_TYPE_HOTKEY;
267 sc->sc_smpsw_valid = 1;
268
269 for (i = 0; i < SONY_PSW_LAST; i++)
270 if (sysmon_pswitch_register(&sc->sc_smpsw[i]) != 0) {
271 aprint_error_dev(self,
272 "couldn't register %s with sysmon\n",
273 sc->sc_smpsw[i].smpsw_name);
274 sc->sc_smpsw_valid = 0;
275 }
276
277 (void)acpi_register_notify(sc->sc_node, sony_acpi_notify_handler);
278
279 /* Install sysctl handler */
280 rv = AcpiWalkNamespace(ACPI_TYPE_METHOD,
281 sc->sc_node->ad_handle, 1, sony_walk_cb, NULL, sc, NULL);
282
283 #ifdef DIAGNOSTIC
284 if (ACPI_FAILURE(rv))
285 aprint_error_dev(self, "Cannot walk ACPI namespace (%u)\n",
286 rv);
287 #endif
288
289 if (!pmf_device_register(self, sony_acpi_suspend, sony_acpi_resume))
290 aprint_error_dev(self, "couldn't establish power handler\n");
291
292 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
293 sony_acpi_brightness_up, true))
294 aprint_error_dev(self,
295 "couldn't register BRIGHTNESS UP handler\n");
296
297 if (!pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
298 sony_acpi_brightness_down, true))
299 aprint_error_dev(self,
300 "couldn't register BRIGHTNESS DOWN handler\n");
301 }
302
303 static void
sony_acpi_quirk_setup(struct sony_acpi_softc * sc)304 sony_acpi_quirk_setup(struct sony_acpi_softc *sc)
305 {
306 ACPI_HANDLE hdl = sc->sc_node->ad_handle;
307
308 if (sc->sc_quirks & SONY_ACPI_QUIRK_FNINIT) {
309 /* Initialize extra Fn keys */
310 sony_acpi_eval_set_integer(hdl, "SN02", 0x04, NULL);
311 sony_acpi_eval_set_integer(hdl, "SN07", 0x02, NULL);
312 sony_acpi_eval_set_integer(hdl, "SN02", 0x10, NULL);
313 sony_acpi_eval_set_integer(hdl, "SN07", 0x00, NULL);
314 sony_acpi_eval_set_integer(hdl, "SN03", 0x02, NULL);
315 sony_acpi_eval_set_integer(hdl, "SN07", 0x101, NULL);
316 }
317 }
318
319 static void
sony_acpi_notify_handler(ACPI_HANDLE hdl,uint32_t notify,void * opaque)320 sony_acpi_notify_handler(ACPI_HANDLE hdl, uint32_t notify, void *opaque)
321 {
322 device_t dv = opaque;
323 struct sony_acpi_softc *sc = device_private(dv);
324 ACPI_STATUS rv;
325 ACPI_INTEGER arg;
326
327 if (notify == SONY_NOTIFY_FnKeyEvent) {
328 rv = sony_acpi_eval_set_integer(hdl, "SN07", 0x202, &arg);
329 if (ACPI_FAILURE(rv))
330 return;
331
332 notify = arg & 0xff;
333 }
334
335 switch (notify) {
336 case SONY_NOTIFY_BrightnessDownPressed:
337 sony_acpi_brightness_down(dv);
338 break;
339 case SONY_NOTIFY_BrightnessUpPressed:
340 sony_acpi_brightness_up(dv);
341 break;
342 case SONY_NOTIFY_BrightnessDownReleased:
343 case SONY_NOTIFY_BrightnessUpReleased:
344 break;
345 case SONY_NOTIFY_SuspendPressed:
346 if (!sc->sc_smpsw_valid)
347 break;
348 sysmon_pswitch_event(&sc->sc_smpsw[SONY_PSW_SLEEP],
349 PSWITCH_EVENT_PRESSED);
350 break;
351 case SONY_NOTIFY_SuspendReleased:
352 break;
353 case SONY_NOTIFY_DisplaySwitchPressed:
354 if (!sc->sc_smpsw_valid)
355 break;
356 sysmon_pswitch_event(&sc->sc_smpsw[SONY_PSW_DISPLAY_CYCLE],
357 PSWITCH_EVENT_PRESSED);
358 break;
359 case SONY_NOTIFY_DisplaySwitchReleased:
360 break;
361 case SONY_NOTIFY_ZoomPressed:
362 if (!sc->sc_smpsw_valid)
363 break;
364 sysmon_pswitch_event(&sc->sc_smpsw[SONY_PSW_ZOOM],
365 PSWITCH_EVENT_PRESSED);
366 break;
367 case SONY_NOTIFY_ZoomReleased:
368 break;
369 default:
370 aprint_debug_dev(dv, "unknown notify event 0x%x\n", notify);
371 break;
372 }
373 }
374
375 static bool
sony_acpi_suspend(device_t dv,const pmf_qual_t * qual)376 sony_acpi_suspend(device_t dv, const pmf_qual_t *qual)
377 {
378 struct sony_acpi_softc *sc = device_private(dv);
379
380 acpi_eval_integer(sc->sc_node->ad_handle, "GBRT", &sc->sc_pmstate.brt);
381
382 return true;
383 }
384
385 static bool
sony_acpi_resume(device_t dv,const pmf_qual_t * qual)386 sony_acpi_resume(device_t dv, const pmf_qual_t *qual)
387 {
388 struct sony_acpi_softc *sc = device_private(dv);
389
390 sony_acpi_eval_set_integer(sc->sc_node->ad_handle, "SBRT",
391 sc->sc_pmstate.brt, NULL);
392 sony_acpi_quirk_setup(sc);
393
394 return true;
395 }
396
397 static void
sony_acpi_brightness_up(device_t dv)398 sony_acpi_brightness_up(device_t dv)
399 {
400 struct sony_acpi_softc *sc = device_private(dv);
401 ACPI_INTEGER arg;
402 ACPI_STATUS rv;
403
404 rv = acpi_eval_integer(sc->sc_node->ad_handle, "GBRT", &arg);
405 if (ACPI_FAILURE(rv))
406 return;
407 if (arg >= 8)
408 arg = 8;
409 else
410 arg++;
411 sony_acpi_eval_set_integer(sc->sc_node->ad_handle, "SBRT", arg, NULL);
412 }
413
414 static void
sony_acpi_brightness_down(device_t dv)415 sony_acpi_brightness_down(device_t dv)
416 {
417 struct sony_acpi_softc *sc = device_private(dv);
418 ACPI_INTEGER arg;
419 ACPI_STATUS rv;
420
421 rv = acpi_eval_integer(sc->sc_node->ad_handle, "GBRT", &arg);
422 if (ACPI_FAILURE(rv))
423 return;
424 if (arg <= 0)
425 arg = 0;
426 else
427 arg--;
428 sony_acpi_eval_set_integer(sc->sc_node->ad_handle, "SBRT", arg, NULL);
429 }
430
431 static ACPI_STATUS
sony_acpi_find_pic(ACPI_HANDLE hdl,uint32_t level,void * opaque,void ** status)432 sony_acpi_find_pic(ACPI_HANDLE hdl, uint32_t level,
433 void *opaque, void **status)
434 {
435 struct sony_acpi_softc *sc = opaque;
436 ACPI_STATUS rv;
437 ACPI_DEVICE_INFO *devinfo;
438
439 rv = AcpiGetObjectInfo(hdl, &devinfo);
440 if (ACPI_FAILURE(rv) || devinfo == NULL)
441 return AE_OK; /* we don't want to stop searching */
442
443 if ((devinfo->Valid & ACPI_VALID_HID) != 0 &&
444 devinfo->HardwareId.String &&
445 strncmp(devinfo->HardwareId.String, "SNY6001", 7) == 0)
446 sc->sc_has_pic = true;
447
448 ACPI_FREE(devinfo);
449
450 return AE_OK;
451 }
452