Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*-
 * Copyright (c) 2012 Alexander Motin <mav@FreeBSD.org>
 * All rights reserved.
 *
 * 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 AUTHOR 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 AUTHOR 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>
__FBSDID("$FreeBSD$");

#include "opt_acpi.h"
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/sbuf.h>
#include <sys/module.h>
#include <sys/sysctl.h>

#include <contrib/dev/acpica/include/acpi.h>
#include <contrib/dev/acpica/include/accommon.h>
#include <dev/acpica/acpivar.h>
#include "acpi_wmi_if.h"

#define _COMPONENT	ACPI_OEM
ACPI_MODULE_NAME("ASUS-WMI")

#define ACPI_ASUS_WMI_MGMT_GUID 	"97845ED0-4E6D-11DE-8A39-0800200C9A66"
#define ACPI_ASUS_WMI_EVENT_GUID	"0B3CBB35-E3C2-45ED-91C2-4C5A6D195D1C"
#define ACPI_EEEPC_WMI_EVENT_GUID	"ABBC0F72-8EA1-11D1-00A0-C90629100000"

/* WMI Methods */
#define ASUS_WMI_METHODID_SPEC          0x43455053
#define ASUS_WMI_METHODID_SFUN          0x4E554653
#define ASUS_WMI_METHODID_DSTS          0x53544344
#define ASUS_WMI_METHODID_DSTS2         0x53545344
#define ASUS_WMI_METHODID_DEVS          0x53564544
#define ASUS_WMI_METHODID_INIT          0x54494E49
#define ASUS_WMI_METHODID_HKEY          0x59454B48

#define ASUS_WMI_UNSUPPORTED_METHOD     0xFFFFFFFE

/* Wireless */
#define ASUS_WMI_DEVID_HW_SWITCH        0x00010001
#define ASUS_WMI_DEVID_WIRELESS_LED     0x00010002
#define ASUS_WMI_DEVID_CWAP             0x00010003
#define ASUS_WMI_DEVID_WLAN             0x00010011
#define ASUS_WMI_DEVID_BLUETOOTH        0x00010013
#define ASUS_WMI_DEVID_GPS              0x00010015
#define ASUS_WMI_DEVID_WIMAX            0x00010017
#define ASUS_WMI_DEVID_WWAN3G           0x00010019
#define ASUS_WMI_DEVID_UWB              0x00010021

/* LEDs */
#define ASUS_WMI_DEVID_LED1             0x00020011
#define ASUS_WMI_DEVID_LED2             0x00020012
#define ASUS_WMI_DEVID_LED3             0x00020013
#define ASUS_WMI_DEVID_LED4             0x00020014
#define ASUS_WMI_DEVID_LED5             0x00020015
#define ASUS_WMI_DEVID_LED6             0x00020016

/* Backlight and Brightness */
#define ASUS_WMI_DEVID_BACKLIGHT        0x00050011
#define ASUS_WMI_DEVID_BRIGHTNESS       0x00050012
#define ASUS_WMI_DEVID_KBD_BACKLIGHT    0x00050021
#define ASUS_WMI_DEVID_LIGHT_SENSOR     0x00050022

/* Misc */
#define ASUS_WMI_DEVID_CAMERA           0x00060013
#define ASUS_WMI_DEVID_CARDREADER       0x00080013
#define ASUS_WMI_DEVID_TOUCHPAD         0x00100011
#define ASUS_WMI_DEVID_TOUCHPAD_LED     0x00100012
#define ASUS_WMI_DEVID_THERMAL_CTRL     0x00110011
#define ASUS_WMI_DEVID_FAN_CTRL         0x00110012
#define ASUS_WMI_DEVID_PROCESSOR_STATE  0x00120012

/* DSTS masks */
#define ASUS_WMI_DSTS_STATUS_BIT        0x00000001
#define ASUS_WMI_DSTS_UNKNOWN_BIT       0x00000002
#define ASUS_WMI_DSTS_PRESENCE_BIT      0x00010000
#define ASUS_WMI_DSTS_USER_BIT          0x00020000
#define ASUS_WMI_DSTS_BIOS_BIT          0x00040000
#define ASUS_WMI_DSTS_BRIGHTNESS_MASK   0x000000FF
#define ASUS_WMI_DSTS_MAX_BRIGTH_MASK   0x0000FF00


struct acpi_asus_wmi_softc {
	device_t	dev;
	device_t	wmi_dev;
	const char	*notify_guid;
	struct sysctl_ctx_list	*sysctl_ctx;
	struct sysctl_oid	*sysctl_tree;
	int		dsts_id;
	int		handle_keys;
};

static struct {
	char	*name;
	int	dev_id;
	char	*description;
	int	flag_rdonly;
} acpi_asus_wmi_sysctls[] = {
	{
		.name		= "hw_switch",
		.dev_id		= ASUS_WMI_DEVID_HW_SWITCH,
		.description	= "hw_switch",
	},
	{
		.name		= "wireless_led",
		.dev_id		= ASUS_WMI_DEVID_WIRELESS_LED,
		.description	= "Wireless LED control",
	},
	{
		.name		= "cwap",
		.dev_id		= ASUS_WMI_DEVID_CWAP,
		.description	= "Alt+F2 function",
	},
	{
		.name		= "wlan",
		.dev_id		= ASUS_WMI_DEVID_WLAN,
		.description	= "WLAN power control",
	},
	{
		.name		= "bluetooth",
		.dev_id		= ASUS_WMI_DEVID_BLUETOOTH,
		.description	= "Bluetooth power control",
	},
	{
		.name		= "gps",
		.dev_id		= ASUS_WMI_DEVID_GPS,
		.description	= "GPS power control",
	},
	{
		.name		= "wimax",
		.dev_id		= ASUS_WMI_DEVID_WIMAX,
		.description	= "WiMAX power control",
	},
	{
		.name		= "wwan3g",
		.dev_id		= ASUS_WMI_DEVID_WWAN3G,
		.description	= "WWAN-3G power control",
	},
	{
		.name		= "uwb",
		.dev_id		= ASUS_WMI_DEVID_UWB,
		.description	= "UWB power control",
	},
	{
		.name		= "led1",
		.dev_id		= ASUS_WMI_DEVID_LED1,
		.description	= "LED1 control",
	},
	{
		.name		= "led2",
		.dev_id		= ASUS_WMI_DEVID_LED2,
		.description	= "LED2 control",
	},
	{
		.name		= "led3",
		.dev_id		= ASUS_WMI_DEVID_LED3,
		.description	= "LED3 control",
	},
	{
		.name		= "led4",
		.dev_id		= ASUS_WMI_DEVID_LED4,
		.description	= "LED4 control",
	},
	{
		.name		= "led5",
		.dev_id		= ASUS_WMI_DEVID_LED5,
		.description	= "LED5 control",
	},
	{
		.name		= "led6",
		.dev_id		= ASUS_WMI_DEVID_LED6,
		.description	= "LED6 control",
	},
	{
		.name		= "backlight",
		.dev_id		= ASUS_WMI_DEVID_BACKLIGHT,
		.description	= "LCD backlight on/off control",
	},
	{
		.name		= "brightness",
		.dev_id		= ASUS_WMI_DEVID_BRIGHTNESS,
		.description	= "LCD backlight brightness control",
	},
	{
		.name		= "kbd_backlight",
		.dev_id		= ASUS_WMI_DEVID_KBD_BACKLIGHT,
		.description	= "Keyboard backlight brightness control",
	},
	{
		.name		= "light_sensor",
		.dev_id		= ASUS_WMI_DEVID_LIGHT_SENSOR,
		.description	= "Ambient light sensor",
	},
	{
		.name		= "camera",
		.dev_id		= ASUS_WMI_DEVID_CAMERA,
		.description	= "Camera power control",
	},
	{
		.name		= "cardreader",
		.dev_id		= ASUS_WMI_DEVID_CARDREADER,
		.description	= "Cardreader power control",
	},
	{
		.name		= "touchpad",
		.dev_id		= ASUS_WMI_DEVID_TOUCHPAD,
		.description	= "Touchpad control",
	},
	{
		.name		= "touchpad_led",
		.dev_id		= ASUS_WMI_DEVID_TOUCHPAD_LED,
		.description	= "Touchpad LED control",
	},
	{
		.name		= "themperature",
		.dev_id		= ASUS_WMI_DEVID_THERMAL_CTRL,
		.description	= "Temperature (C)",
		.flag_rdonly	= 1
	},
	{
		.name		= "fan_speed",
		.dev_id		= ASUS_WMI_DEVID_FAN_CTRL,
		.description	= "Fan speed (0-3)",
		.flag_rdonly	= 1
	},
	{
		.name		= "processor_state",
		.dev_id		= ASUS_WMI_DEVID_PROCESSOR_STATE,
		.flag_rdonly	= 1
	},
	{ NULL, 0, NULL, 0 }
};

ACPI_SERIAL_DECL(asus_wmi, "ASUS WMI device");

static void	acpi_asus_wmi_identify(driver_t *driver, device_t parent);
static int	acpi_asus_wmi_probe(device_t dev);
static int	acpi_asus_wmi_attach(device_t dev);
static int	acpi_asus_wmi_detach(device_t dev);

static int	acpi_asus_wmi_sysctl(SYSCTL_HANDLER_ARGS);
static int	acpi_asus_wmi_sysctl_set(struct acpi_asus_wmi_softc *sc, int dev_id,
		    int arg, int oldarg);
static int	acpi_asus_wmi_sysctl_get(struct acpi_asus_wmi_softc *sc, int dev_id);
static int	acpi_asus_wmi_evaluate_method(device_t wmi_dev, int method,
		    UINT32 arg0, UINT32 arg1, UINT32 *retval);
static int	acpi_wpi_asus_get_devstate(struct acpi_asus_wmi_softc *sc,
		    UINT32 dev_id, UINT32 *retval);
static int	acpi_wpi_asus_set_devstate(struct acpi_asus_wmi_softc *sc,
		    UINT32 dev_id, UINT32 ctrl_param, UINT32 *retval);
static void	acpi_asus_wmi_notify(ACPI_HANDLE h, UINT32 notify, void *context);

static device_method_t acpi_asus_wmi_methods[] = {
	DEVMETHOD(device_identify, acpi_asus_wmi_identify),
	DEVMETHOD(device_probe, acpi_asus_wmi_probe),
	DEVMETHOD(device_attach, acpi_asus_wmi_attach),
	DEVMETHOD(device_detach, acpi_asus_wmi_detach),

	DEVMETHOD_END
};

static driver_t	acpi_asus_wmi_driver = {
	"acpi_asus_wmi",
	acpi_asus_wmi_methods,
	sizeof(struct acpi_asus_wmi_softc),
};

static devclass_t acpi_asus_wmi_devclass;

DRIVER_MODULE(acpi_asus_wmi, acpi_wmi, acpi_asus_wmi_driver,
    acpi_asus_wmi_devclass, 0, 0);
MODULE_DEPEND(acpi_asus_wmi, acpi_wmi, 1, 1, 1);
MODULE_DEPEND(acpi_asus_wmi, acpi, 1, 1, 1);

static void
acpi_asus_wmi_identify(driver_t *driver, device_t parent)
{

	/* Don't do anything if driver is disabled. */
	if (acpi_disabled("asus_wmi"))
		return;

	/* Add only a single device instance. */
	if (device_find_child(parent, "acpi_asus_wmi", -1) != NULL)
		return;

	/* Check management GUID to see whether system is compatible. */
	if (!ACPI_WMI_PROVIDES_GUID_STRING(parent,
	    ACPI_ASUS_WMI_MGMT_GUID))
		return;

	if (BUS_ADD_CHILD(parent, 0, "acpi_asus_wmi", -1) == NULL)
		device_printf(parent, "add acpi_asus_wmi child failed\n");
}

static int
acpi_asus_wmi_probe(device_t dev)
{

	if (!ACPI_WMI_PROVIDES_GUID_STRING(device_get_parent(dev),
	    ACPI_ASUS_WMI_MGMT_GUID))
		return (EINVAL);
	device_set_desc(dev, "ASUS WMI device");
	return (0);
}

static int
acpi_asus_wmi_attach(device_t dev)
{
	struct acpi_asus_wmi_softc	*sc;
	UINT32			val;
	int			dev_id, i;

	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);

	sc = device_get_softc(dev);
	sc->dev = dev;
	sc->wmi_dev = device_get_parent(dev);
	sc->handle_keys = 1;

	/* Check management GUID. */
	if (!ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
	    ACPI_ASUS_WMI_MGMT_GUID)) {
		device_printf(dev,
		    "WMI device does not provide the ASUS management GUID\n");
		return (EINVAL);
	}

	/* Find proper DSTS method. */
	sc->dsts_id = ASUS_WMI_METHODID_DSTS;
next:
	for (i = 0; acpi_asus_wmi_sysctls[i].name != NULL; ++i) {
		dev_id = acpi_asus_wmi_sysctls[i].dev_id;
		if (acpi_wpi_asus_get_devstate(sc, dev_id, &val))
			continue;
		break;
	}
	if (acpi_asus_wmi_sysctls[i].name == NULL) {
		if (sc->dsts_id == ASUS_WMI_METHODID_DSTS) {
			sc->dsts_id = ASUS_WMI_METHODID_DSTS2;
			goto next;
		} else {
			device_printf(dev, "Can not detect DSTS method ID\n");
			return (EINVAL);
		}
	}

	/* Find proper and attach to notufy GUID. */
	if (ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
	    ACPI_ASUS_WMI_EVENT_GUID))
		sc->notify_guid = ACPI_ASUS_WMI_EVENT_GUID;
	else if (ACPI_WMI_PROVIDES_GUID_STRING(sc->wmi_dev,
	    ACPI_EEEPC_WMI_EVENT_GUID))
		sc->notify_guid = ACPI_EEEPC_WMI_EVENT_GUID;
	else
		sc->notify_guid = NULL;
	if (sc->notify_guid != NULL) {
		if (ACPI_WMI_INSTALL_EVENT_HANDLER(sc->wmi_dev,
		    sc->notify_guid, acpi_asus_wmi_notify, dev))
			sc->notify_guid = NULL;
	}
	if (sc->notify_guid == NULL)
		device_printf(dev, "Could not install event handler!\n");

	/* Initialize. */
	if (!acpi_asus_wmi_evaluate_method(sc->wmi_dev,
	    ASUS_WMI_METHODID_INIT, 0, 0, &val) && bootverbose)
		device_printf(dev, "Initialization: %#x\n", val);
	if (!acpi_asus_wmi_evaluate_method(sc->wmi_dev,
	    ASUS_WMI_METHODID_SPEC, 0, 0x9, &val) && bootverbose)
		device_printf(dev, "WMI BIOS version: %d.%d\n",
		    val >> 16, val & 0xFF);
	if (!acpi_asus_wmi_evaluate_method(sc->wmi_dev,
	    ASUS_WMI_METHODID_SFUN, 0, 0, &val) && bootverbose)
		device_printf(dev, "SFUN value: %#x\n", val);

	ACPI_SERIAL_BEGIN(asus_wmi);

	sc->sysctl_ctx = device_get_sysctl_ctx(dev);
	sc->sysctl_tree = device_get_sysctl_tree(dev);
	SYSCTL_ADD_INT(sc->sysctl_ctx,
	    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
	    "handle_keys", CTLFLAG_RW, &sc->handle_keys,
	    0, "Handle some hardware keys inside the driver");
	for (i = 0; acpi_asus_wmi_sysctls[i].name != NULL; ++i) {
		dev_id = acpi_asus_wmi_sysctls[i].dev_id;
		if (acpi_wpi_asus_get_devstate(sc, dev_id, &val))
			continue;
		switch (dev_id) {
		case ASUS_WMI_DEVID_THERMAL_CTRL:
		case ASUS_WMI_DEVID_PROCESSOR_STATE:
		case ASUS_WMI_DEVID_FAN_CTRL:
		case ASUS_WMI_DEVID_BRIGHTNESS:
			if (val == 0)
				continue;
			break;
		default:
			if ((val & ASUS_WMI_DSTS_PRESENCE_BIT) == 0)
				continue;
			break;
		}

		if (acpi_asus_wmi_sysctls[i].flag_rdonly != 0) {
			SYSCTL_ADD_PROC(sc->sysctl_ctx,
			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
			    acpi_asus_wmi_sysctls[i].name,
			    CTLTYPE_INT | CTLFLAG_RD,
			    sc, i, acpi_asus_wmi_sysctl, "I",
			    acpi_asus_wmi_sysctls[i].description);
		} else {
			SYSCTL_ADD_PROC(sc->sysctl_ctx,
			    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
			    acpi_asus_wmi_sysctls[i].name,
			    CTLTYPE_INT | CTLFLAG_RW,
			    sc, i, acpi_asus_wmi_sysctl, "I",
			    acpi_asus_wmi_sysctls[i].description);
		}
	}
	ACPI_SERIAL_END(asus_wmi);

	return (0);
}

static int
acpi_asus_wmi_detach(device_t dev)
{
	struct acpi_asus_wmi_softc *sc = device_get_softc(dev);
	
	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);

	if (sc->notify_guid)
		ACPI_WMI_REMOVE_EVENT_HANDLER(dev, sc->notify_guid);

	return (0);
}

static int
acpi_asus_wmi_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct acpi_asus_wmi_softc	*sc;
	int			arg;
	int			oldarg;
	int			error = 0;
	int			function;
	int			dev_id;
	
	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);

	sc = (struct acpi_asus_wmi_softc *)oidp->oid_arg1;
	function = oidp->oid_arg2;
	dev_id = acpi_asus_wmi_sysctls[function].dev_id;

	ACPI_SERIAL_BEGIN(asus_wmi);
	arg = acpi_asus_wmi_sysctl_get(sc, dev_id);
	oldarg = arg;
	error = sysctl_handle_int(oidp, &arg, 0, req);
	if (!error && req->newptr != NULL)
		error = acpi_asus_wmi_sysctl_set(sc, dev_id, arg, oldarg);
	ACPI_SERIAL_END(asus_wmi);

	return (error);
}

static int
acpi_asus_wmi_sysctl_get(struct acpi_asus_wmi_softc *sc, int dev_id)
{
	UINT32	val = 0;

	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
	ACPI_SERIAL_ASSERT(asus_wmi);

	acpi_wpi_asus_get_devstate(sc, dev_id, &val);

	switch(dev_id) {
	case ASUS_WMI_DEVID_THERMAL_CTRL:
		val = (val - 2731 + 5) / 10;
		break;
	case ASUS_WMI_DEVID_PROCESSOR_STATE:
	case ASUS_WMI_DEVID_FAN_CTRL:
		break;
	case ASUS_WMI_DEVID_BRIGHTNESS:
		val &= ASUS_WMI_DSTS_BRIGHTNESS_MASK;
		break;
	case ASUS_WMI_DEVID_KBD_BACKLIGHT:
		val &= 0x7;
		break;
	default:
		if (val & ASUS_WMI_DSTS_UNKNOWN_BIT)
			val = -1;
		else
			val = !!(val & ASUS_WMI_DSTS_STATUS_BIT);
		break;
	}

	return (val);
}

static int
acpi_asus_wmi_sysctl_set(struct acpi_asus_wmi_softc *sc, int dev_id, int arg, int oldarg)
{
	ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
	ACPI_SERIAL_ASSERT(asus_wmi);

	switch(dev_id) {
	case ASUS_WMI_DEVID_KBD_BACKLIGHT:
		arg = min(0x7, arg);
		if (arg != 0)
			arg |= 0x80;
		break;
	}

	acpi_wpi_asus_set_devstate(sc, dev_id, arg, NULL);

	return (0);
}

static __inline void
acpi_asus_wmi_free_buffer(ACPI_BUFFER* buf) {
	if (buf && buf->Pointer) {
		AcpiOsFree(buf->Pointer);
	}
}

static void
acpi_asus_wmi_notify(ACPI_HANDLE h, UINT32 notify, void *context)
{
	device_t dev = context;
	ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, notify);
	UINT32 val;
	int code = 0;

	struct acpi_asus_wmi_softc *sc = device_get_softc(dev);
	ACPI_BUFFER response = { ACPI_ALLOCATE_BUFFER, NULL };
	ACPI_OBJECT *obj;
	ACPI_WMI_GET_EVENT_DATA(sc->wmi_dev, notify, &response);
	obj = (ACPI_OBJECT*) response.Pointer;
	if (obj && obj->Type == ACPI_TYPE_INTEGER) {
		code = obj->Integer.Value;
		acpi_UserNotify("ASUS", ACPI_ROOT_OBJECT,
		    code);
	}
	if (code && sc->handle_keys) {
		/* Keyboard backlight control. */
		if (code == 0xc4 || code == 0xc5) {
			acpi_wpi_asus_get_devstate(sc,
			    ASUS_WMI_DEVID_KBD_BACKLIGHT, &val);
			val &= 0x7;
			if (code == 0xc4) {
				if (val < 0x7)
					val++;
			} else if (val > 0)
				val--;
			if (val != 0)
				val |= 0x80;
			acpi_wpi_asus_set_devstate(sc,
			    ASUS_WMI_DEVID_KBD_BACKLIGHT, val, NULL);
		}
		/* Touchpad control. */
		if (code == 0x6b) {
			acpi_wpi_asus_get_devstate(sc,
			    ASUS_WMI_DEVID_TOUCHPAD, &val);
			val = !(val & 1);
			acpi_wpi_asus_set_devstate(sc,
			    ASUS_WMI_DEVID_TOUCHPAD, val, NULL);
		}
	}
	acpi_asus_wmi_free_buffer(&response);
}

static int
acpi_asus_wmi_evaluate_method(device_t wmi_dev, int method,
    UINT32 arg0, UINT32 arg1, UINT32 *retval)
{
	UINT32		params[2] = { arg0, arg1 };
	UINT32		result;
	ACPI_OBJECT	*obj;
	ACPI_BUFFER	in = { sizeof(params), &params };
	ACPI_BUFFER	out = { ACPI_ALLOCATE_BUFFER, NULL };
	
	if (ACPI_FAILURE(ACPI_WMI_EVALUATE_CALL(wmi_dev,
	    ACPI_ASUS_WMI_MGMT_GUID, 1, method, &in, &out))) {
		acpi_asus_wmi_free_buffer(&out);
		return (-EINVAL);
	}
	obj = out.Pointer;
	if (obj && obj->Type == ACPI_TYPE_INTEGER)
		result = (UINT32) obj->Integer.Value;
	else
		result = 0;
	acpi_asus_wmi_free_buffer(&out);
	if (retval)
		*retval = result;
	return (result == ASUS_WMI_UNSUPPORTED_METHOD ? -ENODEV : 0);
}

static int
acpi_wpi_asus_get_devstate(struct acpi_asus_wmi_softc *sc,
    UINT32 dev_id, UINT32 *retval)
{

	return (acpi_asus_wmi_evaluate_method(sc->wmi_dev,
	    sc->dsts_id, dev_id, 0, retval));
}

static int
acpi_wpi_asus_set_devstate(struct acpi_asus_wmi_softc *sc,
    UINT32 dev_id, UINT32 ctrl_param, UINT32 *retval)
{

	return (acpi_asus_wmi_evaluate_method(sc->wmi_dev,
	    ASUS_WMI_METHODID_DEVS, dev_id, ctrl_param, retval));
}