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