xref: /netbsd-src/sys/dev/acpi/fujbp_acpi.c (revision 3772555307d774424243e6d1cdc66aa160602c26)
1 /*	$NetBSD: fujbp_acpi.c,v 1.5 2021/01/29 15:49:55 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2010, 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gregoire Sutre.
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 
32 /*
33  * ACPI Fujitsu Driver.
34  *
35  * Together with fujhk(4), this driver provides support for the ACPI devices
36  * FUJ02B1 and FUJ02E3 that are commonly found in Fujitsu LifeBooks. The
37  * driver does not support all features of these devices, in particular
38  * volume control is not implemented.
39  *
40  * Information regarding the behavior of these devices was obtained from the
41  * source code of the Linux and FreeBSD drivers, as well as from experiments on
42  * a Fujitsu LifeBook P7120.
43  *
44  * The FUJ02B1 device is used to control the brightness level of the internal
45  * display, the state (on/off) of the internal pointer, and the volume level of
46  * the internal speakers or headphones.
47  *
48  * The FUJ02B1 device provides the following methods (or only a subset):
49  *
50  *	GSIF		supported hotkey status bits (bitmask for GHKS)
51  *	GHKS		active hotkeys (bit field)
52  *	{G,S}BLL	get/set the brightness level of the internal display
53  *	{G,S}VOL	get/set the volume level of the internal speakers
54  *	{G,S}MOU	get/set the switch state of the internal pointer
55  *	RBLL		brightness radix (number of brightness levels)
56  *	RVOL		volume radix (number of volume levels)
57  *
58  * Notifications are delivered to the FUJ02B1 device when functions hotkeys
59  * (brightness, pointer) are released.  However, these notifications seem to be
60  * purely informative: the BIOS already made the hardware changes corresponding
61  * to the hotkey.
62  *
63  * Each bit in the value returned by GHKS remains set until the corresponding
64  * get method (GBLL, GMOU or GVOL) is called.
65  *
66  * The FUJ02E3 device manages the laptop hotkeys (such as the `Eco' button) and
67  * provides additional services (such as backlight on/off control) through the
68  * FUNC method.
69  *
70  * The FUJ02E3 device provides the following methods (or only a subset):
71  *
72  *	GIRB		get next hotkey code from buffer
73  *	FUNC		general-purpose method (four arguments)
74  *
75  * Notifications are delivered to the FUJ02E3 device when hotkeys are pressed
76  * and when they are released.  The BIOS stores the corresponding codes in a
77  * FIFO buffer, that can be read with the GIRB method.
78  */
79 
80 #include <sys/cdefs.h>
81 __KERNEL_RCSID(0, "$NetBSD: fujbp_acpi.c,v 1.5 2021/01/29 15:49:55 thorpej Exp $");
82 
83 #include <sys/param.h>
84 #include <sys/device.h>
85 #include <sys/module.h>
86 #include <sys/mutex.h>
87 #include <sys/sysctl.h>
88 
89 #include <dev/acpi/acpireg.h>
90 #include <dev/acpi/acpivar.h>
91 
92 #define _COMPONENT			ACPI_RESOURCE_COMPONENT
93 ACPI_MODULE_NAME			("fujbp_acpi")
94 
95 /*
96  * Notification value, bits returned by the GHKS method, and
97  * modification status bits (from GBLL, GMOU, GIRB), respectively.
98  */
99 #define FUJITSU_BP_NOTIFY		0x80
100 #define FUJITSU_BP_HKS_BRIGHTNESS	__BIT(0)
101 #define FUJITSU_BP_HKS_POINTER		__BIT(3)
102 #define FUJITSU_BP_MODMASK		0xc0000000
103 
104 /*
105  * ACPI Fujitsu brightness & pointer controller capabilities (methods).
106  */
107 #define FUJITSU_BP_CAP_GHKS		__BIT(0)
108 #define FUJITSU_BP_CAP_RBLL		__BIT(1)
109 #define FUJITSU_BP_CAP_GBLL		__BIT(2)
110 #define FUJITSU_BP_CAP_SBLL		__BIT(3)
111 #define FUJITSU_BP_CAP_GMOU		__BIT(4)
112 #define FUJITSU_BP_CAP_SMOU		__BIT(5)
113 
114 /*
115  * fujitsu_bp_softc:
116  *
117  *	Software state of an ACPI Fujitsu brightness & pointer controller.
118  *	Valid brightness levels range from 0 to (sc_brightness_nlevels - 1).
119  */
120 struct fujitsu_bp_softc {
121 	device_t		 sc_dev;
122 	struct acpi_devnode	*sc_node;
123 	struct sysctllog	*sc_log;
124 	kmutex_t		 sc_mtx;
125 	uint16_t		 sc_caps;
126 	uint8_t			 sc_brightness_nlevels;
127 };
128 
129 static const struct device_compatible_entry compat_data[] = {
130 	{ .compat = "FUJ02B1" },
131 	DEVICE_COMPAT_EOL
132 };
133 
134 static int	fujitsu_bp_match(device_t, cfdata_t, void *);
135 static void	fujitsu_bp_attach(device_t, device_t, void *);
136 static int	fujitsu_bp_detach(device_t, int);
137 static bool	fujitsu_bp_suspend(device_t, const pmf_qual_t *);
138 static bool	fujitsu_bp_resume(device_t, const pmf_qual_t *);
139 static void	fujitsu_bp_brightness_up(device_t);
140 static void	fujitsu_bp_brightness_down(device_t);
141 static uint16_t	fujitsu_bp_capabilities(const struct acpi_devnode *);
142 static void	fujitsu_bp_notify_handler(ACPI_HANDLE, uint32_t, void *);
143 static void	fujitsu_bp_event_callback(void *);
144 static void	fujitsu_bp_sysctl_setup(struct fujitsu_bp_softc *);
145 static int	fujitsu_bp_sysctl_brightness(SYSCTLFN_PROTO);
146 static int	fujitsu_bp_sysctl_pointer(SYSCTLFN_PROTO);
147 static int	fujitsu_bp_get_hks(struct fujitsu_bp_softc *, uint32_t *);
148 static int	fujitsu_bp_init_brightness(struct fujitsu_bp_softc *,uint8_t*);
149 static int	fujitsu_bp_get_brightness(struct fujitsu_bp_softc *,uint8_t *);
150 static int	fujitsu_bp_set_brightness(struct fujitsu_bp_softc *, uint8_t);
151 static int	fujitsu_bp_get_pointer(struct fujitsu_bp_softc *, bool *);
152 static int	fujitsu_bp_set_pointer(struct fujitsu_bp_softc *, bool);
153 static bool	fujitsu_bp_cap(ACPI_HANDLE, const char *, ACPI_OBJECT_TYPE);
154 
155 CFATTACH_DECL_NEW(fujbp, sizeof(struct fujitsu_bp_softc),
156     fujitsu_bp_match, fujitsu_bp_attach, fujitsu_bp_detach, NULL);
157 
158 static int
fujitsu_bp_match(device_t parent,cfdata_t match,void * aux)159 fujitsu_bp_match(device_t parent, cfdata_t match, void *aux)
160 {
161 	struct acpi_attach_args *aa = aux;
162 
163 	return acpi_compatible_match(aa, compat_data);
164 }
165 
166 static void
fujitsu_bp_attach(device_t parent,device_t self,void * aux)167 fujitsu_bp_attach(device_t parent, device_t self, void *aux)
168 {
169 	struct fujitsu_bp_softc *sc = device_private(self);
170 	struct acpi_attach_args *aa = aux;
171 	struct acpi_devnode *ad = aa->aa_node;
172 
173 	aprint_naive(": Fujitsu Brightness & Pointer\n");
174 	aprint_normal(": Fujitsu Brightness & Pointer\n");
175 
176 	sc->sc_dev = self;
177 	sc->sc_node = ad;
178 	sc->sc_log = NULL;
179 	sc->sc_caps = fujitsu_bp_capabilities(ad);
180 
181 	mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_NONE);
182 
183 	if (fujitsu_bp_init_brightness(sc, &sc->sc_brightness_nlevels))
184 		sc->sc_brightness_nlevels = 0;
185 
186 	(void)acpi_register_notify(sc->sc_node, fujitsu_bp_notify_handler);
187 	(void)pmf_device_register(self, fujitsu_bp_suspend, fujitsu_bp_resume);
188 
189 	fujitsu_bp_sysctl_setup(sc);
190 
191 	(void)pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_UP,
192 	    fujitsu_bp_brightness_up, true);
193 
194 	(void)pmf_event_register(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
195 	    fujitsu_bp_brightness_down, true);
196 }
197 
198 static int
fujitsu_bp_detach(device_t self,int flags)199 fujitsu_bp_detach(device_t self, int flags)
200 {
201 	struct fujitsu_bp_softc *sc = device_private(self);
202 
203 	pmf_event_deregister(self, PMFE_DISPLAY_BRIGHTNESS_DOWN,
204 	    fujitsu_bp_brightness_down, true);
205 
206 	pmf_event_deregister(self, PMFE_DISPLAY_BRIGHTNESS_UP,
207 	    fujitsu_bp_brightness_up, true);
208 
209 	pmf_device_deregister(self);
210 
211 	if (sc->sc_log != NULL)
212 		sysctl_teardown(&sc->sc_log);
213 
214 	acpi_deregister_notify(sc->sc_node);
215 	mutex_destroy(&sc->sc_mtx);
216 
217 	return 0;
218 }
219 
220 /*
221  * On some LifeBook models, a call to the SMOU method is required to make the
222  * internal pointer work after resume.  On the P7120, the internal pointer is
223  * always enabled after resume.  If it was disabled before suspend, the BIOS
224  * apparently believes that it is still disabled after resume.
225  *
226  * To prevent these problems, we disable the internal pointer on suspend and
227  * enable it on resume.
228  */
229 static bool
fujitsu_bp_suspend(device_t self,const pmf_qual_t * qual)230 fujitsu_bp_suspend(device_t self, const pmf_qual_t *qual)
231 {
232 	struct fujitsu_bp_softc *sc = device_private(self);
233 
234 	mutex_enter(&sc->sc_mtx);
235 	(void)fujitsu_bp_set_pointer(sc, false);
236 	mutex_exit(&sc->sc_mtx);
237 
238 	return true;
239 }
240 
241 static bool
fujitsu_bp_resume(device_t self,const pmf_qual_t * qual)242 fujitsu_bp_resume(device_t self, const pmf_qual_t *qual)
243 {
244 	struct fujitsu_bp_softc *sc = device_private(self);
245 
246 	mutex_enter(&sc->sc_mtx);
247 	(void)fujitsu_bp_set_pointer(sc, true);
248 	mutex_exit(&sc->sc_mtx);
249 
250 	return true;
251 }
252 
253 static void
fujitsu_bp_brightness_up(device_t self)254 fujitsu_bp_brightness_up(device_t self)
255 {
256 	struct fujitsu_bp_softc *sc = device_private(self);
257 	uint8_t level;
258 
259 	mutex_enter(&sc->sc_mtx);
260 
261 	if (fujitsu_bp_get_brightness(sc, &level) == 0 &&
262 	    level < (uint8_t)(sc->sc_brightness_nlevels - 1))
263 		(void)fujitsu_bp_set_brightness(sc, level + 1);
264 
265 	mutex_exit(&sc->sc_mtx);
266 }
267 
268 static void
fujitsu_bp_brightness_down(device_t self)269 fujitsu_bp_brightness_down(device_t self)
270 {
271 	struct fujitsu_bp_softc *sc = device_private(self);
272 	uint8_t level;
273 
274 	mutex_enter(&sc->sc_mtx);
275 
276 	if (fujitsu_bp_get_brightness(sc, &level) == 0 && level > 0)
277 		(void)fujitsu_bp_set_brightness(sc, level - 1);
278 
279 	mutex_exit(&sc->sc_mtx);
280 }
281 
282 static uint16_t
fujitsu_bp_capabilities(const struct acpi_devnode * ad)283 fujitsu_bp_capabilities(const struct acpi_devnode *ad)
284 {
285 	uint16_t caps;
286 
287 	caps = 0;
288 
289 	if (fujitsu_bp_cap(ad->ad_handle, "GHKS", ACPI_TYPE_INTEGER))
290 		caps |= FUJITSU_BP_CAP_GHKS;
291 
292 	if (fujitsu_bp_cap(ad->ad_handle, "RBLL", ACPI_TYPE_INTEGER))
293 		caps |= FUJITSU_BP_CAP_RBLL;
294 
295 	if (fujitsu_bp_cap(ad->ad_handle, "GBLL", ACPI_TYPE_INTEGER))
296 		caps |= FUJITSU_BP_CAP_GBLL;
297 
298 	if (fujitsu_bp_cap(ad->ad_handle, "SBLL", ACPI_TYPE_METHOD))
299 		caps |= FUJITSU_BP_CAP_SBLL;
300 
301 	if (fujitsu_bp_cap(ad->ad_handle, "GMOU", ACPI_TYPE_INTEGER))
302 		caps |= FUJITSU_BP_CAP_GMOU;
303 
304 	if (fujitsu_bp_cap(ad->ad_handle, "SMOU", ACPI_TYPE_METHOD))
305 		caps |= FUJITSU_BP_CAP_SMOU;
306 
307 	return caps;
308 }
309 
310 static void
fujitsu_bp_notify_handler(ACPI_HANDLE handle,uint32_t evt,void * context)311 fujitsu_bp_notify_handler(ACPI_HANDLE handle, uint32_t evt, void *context)
312 {
313 	struct fujitsu_bp_softc *sc = device_private(context);
314 	static const int handler = OSL_NOTIFY_HANDLER;
315 
316 	switch (evt) {
317 
318 	case FUJITSU_BP_NOTIFY:
319 		(void)AcpiOsExecute(handler, fujitsu_bp_event_callback, sc);
320 		break;
321 
322 	default:
323 		aprint_debug_dev(sc->sc_dev, "unknown notify 0x%02X\n", evt);
324 	}
325 }
326 
327 static void
fujitsu_bp_event_callback(void * arg)328 fujitsu_bp_event_callback(void *arg)
329 {
330 	struct fujitsu_bp_softc *sc = arg;
331 	int error;
332 	uint32_t hks;
333 	uint8_t level;
334 	bool state;
335 
336 	if (fujitsu_bp_get_hks(sc, &hks))
337 		return;
338 
339 	if (hks & FUJITSU_BP_HKS_BRIGHTNESS) {
340 		mutex_enter(&sc->sc_mtx);
341 		error = fujitsu_bp_get_brightness(sc, &level);
342 		mutex_exit(&sc->sc_mtx);
343 		if (!error)
344 			aprint_verbose_dev(sc->sc_dev,
345 			    "brightness level is now: %"PRIu8"\n", level);
346 	}
347 
348 	if (hks & FUJITSU_BP_HKS_POINTER) {
349 		mutex_enter(&sc->sc_mtx);
350 		error = fujitsu_bp_get_pointer(sc, &state);
351 		mutex_exit(&sc->sc_mtx);
352 		if (!error)
353 			aprint_verbose_dev(sc->sc_dev,
354 			    "internal pointer is now: %s\n",
355 			    state ? "enabled" : "disabled");
356 	}
357 }
358 
359 static void
fujitsu_bp_sysctl_setup(struct fujitsu_bp_softc * sc)360 fujitsu_bp_sysctl_setup(struct fujitsu_bp_softc *sc)
361 {
362 	const struct sysctlnode *rnode;
363 	int access;
364 	uint8_t dummy_level;
365 	bool dummy_state;
366 	bool brightness, pointer;
367 
368 	brightness = (fujitsu_bp_get_brightness(sc, &dummy_level) == 0);
369 	pointer = (fujitsu_bp_get_pointer(sc, &dummy_state) == 0);
370 
371 	if (brightness || pointer) {
372 		if ((sysctl_createv(&sc->sc_log, 0, NULL, &rnode,
373 		    0, CTLTYPE_NODE, "acpi", NULL,
374 		    NULL, 0, NULL, 0,
375 		    CTL_HW, CTL_CREATE, CTL_EOL)) != 0)
376 			goto fail;
377 
378 		if ((sysctl_createv(&sc->sc_log, 0, &rnode, &rnode,
379 		    0, CTLTYPE_NODE, device_xname(sc->sc_dev),
380 		    SYSCTL_DESCR("Fujitsu brightness & pointer controls"),
381 		    NULL, 0, NULL, 0,
382 		    CTL_CREATE, CTL_EOL)) != 0)
383 			goto fail;
384 	}
385 
386 	if (brightness) {
387 		if (sc->sc_caps & FUJITSU_BP_CAP_SBLL)
388 			access = CTLFLAG_READWRITE;
389 		else
390 			access = CTLFLAG_READONLY;
391 
392 		(void)sysctl_createv(&sc->sc_log, 0, &rnode, NULL,
393 		    access, CTLTYPE_INT, "brightness",
394 		    SYSCTL_DESCR("Internal DFP brightness level"),
395 		    fujitsu_bp_sysctl_brightness, 0, (void *)sc, 0,
396 		    CTL_CREATE, CTL_EOL);
397 	}
398 
399 	if (pointer) {
400 		if (sc->sc_caps & FUJITSU_BP_CAP_SMOU)
401 			access = CTLFLAG_READWRITE;
402 		else
403 			access = CTLFLAG_READONLY;
404 
405 		(void)sysctl_createv(&sc->sc_log, 0, &rnode, NULL,
406 		    access, CTLTYPE_BOOL, "pointer",
407 		    SYSCTL_DESCR("Internal pointer switch state"),
408 		    fujitsu_bp_sysctl_pointer, 0, (void *)sc, 0,
409 		    CTL_CREATE, CTL_EOL);
410 	}
411 
412 	return;
413 
414  fail:
415 	aprint_error_dev(sc->sc_dev, "couldn't add sysctl nodes\n");
416 }
417 
418 static int
fujitsu_bp_sysctl_brightness(SYSCTLFN_ARGS)419 fujitsu_bp_sysctl_brightness(SYSCTLFN_ARGS)
420 {
421 	struct sysctlnode node;
422 	struct fujitsu_bp_softc *sc;
423 	int val, error;
424 	uint8_t level;
425 
426 	node = *rnode;
427 	sc = node.sysctl_data;
428 
429 	mutex_enter(&sc->sc_mtx);
430 	error = fujitsu_bp_get_brightness(sc, &level);
431 	mutex_exit(&sc->sc_mtx);
432 
433 	if (error)
434 		return error;
435 
436 	val = (int)level;
437 	node.sysctl_data = &val;
438 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
439 	if (error || newp == NULL)
440 		return error;
441 
442 	if (val < 0 || val > (uint8_t)(sc->sc_brightness_nlevels - 1))
443 		return EINVAL;
444 
445 	mutex_enter(&sc->sc_mtx);
446 	error = fujitsu_bp_set_brightness(sc, (uint8_t)val);
447 	mutex_exit(&sc->sc_mtx);
448 
449 	return error;
450 }
451 
452 static int
fujitsu_bp_sysctl_pointer(SYSCTLFN_ARGS)453 fujitsu_bp_sysctl_pointer(SYSCTLFN_ARGS)
454 {
455 	struct sysctlnode node;
456 	struct fujitsu_bp_softc *sc;
457 	bool val;
458 	int error;
459 
460 	node = *rnode;
461 	sc = node.sysctl_data;
462 
463 	mutex_enter(&sc->sc_mtx);
464 	error = fujitsu_bp_get_pointer(sc, &val);
465 	mutex_exit(&sc->sc_mtx);
466 
467 	if (error)
468 		return error;
469 
470 	node.sysctl_data = &val;
471 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
472 	if (error || newp == NULL)
473 		return error;
474 
475 	mutex_enter(&sc->sc_mtx);
476 	error = fujitsu_bp_set_pointer(sc, val);
477 	mutex_exit(&sc->sc_mtx);
478 
479 	return error;
480 }
481 
482 static int
fujitsu_bp_get_hks(struct fujitsu_bp_softc * sc,uint32_t * valuep)483 fujitsu_bp_get_hks(struct fujitsu_bp_softc *sc, uint32_t *valuep)
484 {
485 	ACPI_HANDLE hdl = sc->sc_node->ad_handle;
486 	ACPI_INTEGER val;
487 	ACPI_STATUS rv;
488 
489 	if (!(sc->sc_caps & FUJITSU_BP_CAP_GHKS))
490 		return ENODEV;
491 
492 	rv = acpi_eval_integer(hdl, "GHKS", &val);
493 	if (ACPI_FAILURE(rv)) {
494 		aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n",
495 		    acpi_name(hdl), "GHKS", AcpiFormatException(rv));
496 		return EIO;
497 	}
498 
499 	*valuep = (uint32_t)val;
500 
501 	return 0;
502 }
503 
504 static int
fujitsu_bp_init_brightness(struct fujitsu_bp_softc * sc,uint8_t * valuep)505 fujitsu_bp_init_brightness(struct fujitsu_bp_softc *sc, uint8_t *valuep)
506 {
507 	ACPI_HANDLE hdl = sc->sc_node->ad_handle;
508 	ACPI_INTEGER val;
509 	ACPI_STATUS rv;
510 
511 	if (!(sc->sc_caps & FUJITSU_BP_CAP_RBLL))
512 		return ENODEV;
513 
514 	rv = acpi_eval_integer(hdl, "RBLL", &val);
515 	if (ACPI_FAILURE(rv)) {
516 		aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n",
517 		    acpi_name(hdl), "RBLL", AcpiFormatException(rv));
518 		return EIO;
519 	}
520 
521 	if (val > UINT8_MAX)
522 		return ERANGE;
523 
524 	*valuep = (uint8_t)val;
525 
526 	return 0;
527 }
528 
529 static int
fujitsu_bp_get_brightness(struct fujitsu_bp_softc * sc,uint8_t * valuep)530 fujitsu_bp_get_brightness(struct fujitsu_bp_softc *sc, uint8_t *valuep)
531 {
532 	ACPI_HANDLE hdl = sc->sc_node->ad_handle;
533 	ACPI_INTEGER val;
534 	ACPI_STATUS rv;
535 
536 	if (!(sc->sc_caps & FUJITSU_BP_CAP_GBLL))
537 		return ENODEV;
538 
539 	rv = acpi_eval_integer(hdl, "GBLL", &val);
540 	if (ACPI_FAILURE(rv)) {
541 		aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n",
542 		    acpi_name(hdl), "GBLL", AcpiFormatException(rv));
543 		return EIO;
544 	}
545 
546 	/* Clear modification bits. */
547 	val &= ~FUJITSU_BP_MODMASK;
548 
549 	if (val > UINT8_MAX)
550 		return ERANGE;
551 
552 	*valuep = (uint8_t)val;
553 
554 	return 0;
555 }
556 
557 static int
fujitsu_bp_set_brightness(struct fujitsu_bp_softc * sc,uint8_t val)558 fujitsu_bp_set_brightness(struct fujitsu_bp_softc *sc, uint8_t val)
559 {
560 	ACPI_HANDLE hdl = sc->sc_node->ad_handle;
561 	ACPI_STATUS rv;
562 
563 	if (!(sc->sc_caps & FUJITSU_BP_CAP_SBLL))
564 		return ENODEV;
565 
566 	rv = acpi_eval_set_integer(hdl, "SBLL", val);
567 
568 	if (ACPI_FAILURE(rv)) {
569 		aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n",
570 		    acpi_name(hdl), "SBLL", AcpiFormatException(rv));
571 		return EIO;
572 	}
573 
574 	return 0;
575 }
576 
577 static int
fujitsu_bp_get_pointer(struct fujitsu_bp_softc * sc,bool * valuep)578 fujitsu_bp_get_pointer(struct fujitsu_bp_softc *sc, bool *valuep)
579 {
580 	ACPI_HANDLE hdl = sc->sc_node->ad_handle;
581 	ACPI_INTEGER val;
582 	ACPI_STATUS rv;
583 
584 	if (!(sc->sc_caps & FUJITSU_BP_CAP_GMOU))
585 		return ENODEV;
586 
587 	rv = acpi_eval_integer(hdl, "GMOU", &val);
588 	if (ACPI_FAILURE(rv)) {
589 		aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n",
590 		    acpi_name(hdl), "GMOU", AcpiFormatException(rv));
591 		return EIO;
592 	}
593 
594 	/* Clear modification bits. */
595 	val &= ~FUJITSU_BP_MODMASK;
596 
597 	if (val > 1)
598 		return ERANGE;
599 
600 	*valuep = (bool)val;
601 
602 	return 0;
603 }
604 
605 static int
fujitsu_bp_set_pointer(struct fujitsu_bp_softc * sc,bool val)606 fujitsu_bp_set_pointer(struct fujitsu_bp_softc *sc, bool val)
607 {
608 	ACPI_HANDLE hdl = sc->sc_node->ad_handle;
609 	ACPI_STATUS rv;
610 
611 	if (!(sc->sc_caps & FUJITSU_BP_CAP_SMOU))
612 		return ENODEV;
613 
614 	rv = acpi_eval_set_integer(hdl, "SMOU", val);
615 
616 	if (ACPI_FAILURE(rv)) {
617 		aprint_error_dev(sc->sc_dev, "failed to evaluate %s.%s: %s\n",
618 		    acpi_name(hdl), "SMOU", AcpiFormatException(rv));
619 		return EIO;
620 	}
621 
622 	return 0;
623 }
624 
625 /*
626  * fujitusu_bp_cap:
627  *
628  *	Returns true if and only if (a) the object handle.path exists and
629  *	(b) this object is a method or has the given type.
630  */
631 static bool
fujitsu_bp_cap(ACPI_HANDLE handle,const char * path,ACPI_OBJECT_TYPE type)632 fujitsu_bp_cap(ACPI_HANDLE handle, const char *path, ACPI_OBJECT_TYPE type)
633 {
634 	ACPI_HANDLE hdl;
635 	ACPI_OBJECT_TYPE typ;
636 
637 	KASSERT(handle != NULL);
638 
639 	if (ACPI_FAILURE(AcpiGetHandle(handle, path, &hdl)))
640 		return false;
641 
642 	if (ACPI_FAILURE(AcpiGetType(hdl, &typ)))
643 		return false;
644 
645 	if (typ != ACPI_TYPE_METHOD && typ != type)
646 		return false;
647 
648 	return true;
649 }
650 
651 MODULE(MODULE_CLASS_DRIVER, fujbp, NULL);
652 
653 #ifdef _MODULE
654 #include "ioconf.c"
655 #endif
656 
657 static int
fujbp_modcmd(modcmd_t cmd,void * aux)658 fujbp_modcmd(modcmd_t cmd, void *aux)
659 {
660 	int rv = 0;
661 
662 	switch (cmd) {
663 
664 	case MODULE_CMD_INIT:
665 
666 #ifdef _MODULE
667 		rv = config_init_component(cfdriver_ioconf_fujbp,
668 		    cfattach_ioconf_fujbp, cfdata_ioconf_fujbp);
669 #endif
670 		break;
671 
672 	case MODULE_CMD_FINI:
673 
674 #ifdef _MODULE
675 		rv = config_fini_component(cfdriver_ioconf_fujbp,
676 		    cfattach_ioconf_fujbp, cfdata_ioconf_fujbp);
677 #endif
678 		break;
679 
680 	default:
681 		rv = ENOTTY;
682 	}
683 
684 	return rv;
685 }
686