summaryrefslogtreecommitdiffstats
path: root/sys/dev/acpi_support/acpi_ibm.c
blob: adcd760fa90a4cb3f77cbe0a4c89b4ed72e42a70 (plain)
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
/*-
 * Copyright (c) 2004 Takanori Watanabe
 * 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.
 *
 *	$FreeBSD$
 */

#include "opt_acpi.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <machine/cpufunc.h>
#include "acpi.h"
#include "acpi_if.h"
#include <sys/module.h>
#include <dev/acpica/acpivar.h>
#include <sys/sysctl.h>
#include <machine/clock.h>

#define _COMPONENT	ACPI_OEM
ACPI_MODULE_NAME("IBM")

#define IBM_RTC_MISCKEY 0x65
#define IBM_RTC_BRIGHTNESS 0x6c
#define   IBM_RTC_MASK_BRI 0x7
#define   IBM_RTC_MASK_BRKEY 0x40
#define IBM_RTC_KEYLIGHT 0x66
#define   IBM_RTC_MASK_KEYLIGHT 0x10
#define IBM_RTC_VOLUME 0x6e
#define   IBM_RTC_MASK_VOL 0xf
#define   IBM_RTC_MASK_MUTE 0x40
#define   IBM_RTC_MASK_VOLKEY 0x80

#define IBM_NAME_GET_WIRELESS "GBDC"
#define IBM_NAME_SET_WIRELESS "SBDC"
#define IBM_NAME_INTERFACE_VERSION "MHKV"
#define IBM_NAME_AVAIL_MASK "MHKA"
#define IBM_NAME_CURRENT_MASK "MHKN"
#define IBM_NAME_MODIFY_MASK "MHKM"
#define IBM_NAME_GET_EVENT "MHKP"
#define IBM_NAME_ENABLE "MHKC"
#if 0
/* TPX31 Specific? */
#define IBM_UCMS_VOLDN 0x0
#define IBM_UCMS_VOLUP 0x1
#define IBM_UCMS_MUTE 0x2
#define IBM_UCMS_BRIUP 0x4
#define IBM_UCMS_BRIDN 0x5
#define IBM_UCMS_KEYLIGHT 0xe
#endif

struct acpi_ibm_softc {
	unsigned int	ibm_version;
	unsigned int	ibm_availmask;
	unsigned int	ibm_initialmask;
	int		ibm_enable;
	int		device_flag;
#define IBM_MHKN_AVAIL 1
#define IBM_MHKM_AVAIL 2
	struct sysctl_oid *oid_bluetooth;
	struct sysctl_oid *oid_wlan;
};

static int	acpi_ibm_probe(device_t dev);
static int	acpi_ibm_attach(device_t dev);
static int	acpi_ibm_detach(device_t dev);
static void
acpi_ibm_notify_handler(ACPI_HANDLE h, UINT32 notify,
			void *context);
static int	sysctl_acpi_ibm_mask_handler(SYSCTL_HANDLER_ARGS);
static int	sysctl_acpi_ibm_enable_handler(SYSCTL_HANDLER_ARGS);
static int	sysctl_acpi_ibm_misckey_handler(SYSCTL_HANDLER_ARGS);
static int	sysctl_acpi_ibm_volume_handler(SYSCTL_HANDLER_ARGS);
static int	sysctl_acpi_ibm_mute_handler(SYSCTL_HANDLER_ARGS);
static int	sysctl_acpi_ibm_brightness_handler(SYSCTL_HANDLER_ARGS);
static int	sysctl_acpi_ibm_keylight_handler(SYSCTL_HANDLER_ARGS);
static int	sysctl_acpi_ibm_wireless_handler(SYSCTL_HANDLER_ARGS);
static int	acpi_ibm_enable_mask(device_t dev, int val);

static device_method_t acpi_ibm_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe, acpi_ibm_probe),
	DEVMETHOD(device_attach, acpi_ibm_attach),
	DEVMETHOD(device_detach, acpi_ibm_detach),

	{0, 0}
};

static driver_t	acpi_ibm_driver = {
	"acpi_ibm",
	acpi_ibm_methods,
	sizeof(struct acpi_ibm_softc),
};

static devclass_t acpi_ibm_devclass;

DRIVER_MODULE(acpi_ibm, acpi, acpi_ibm_driver, acpi_ibm_devclass,
	      0, 0);
MODULE_DEPEND(acpi_ibm, acpi, 1, 1, 1);
static char    *ibm_id[] = {"IBM0068", NULL};

static int
acpi_ibm_probe(device_t dev)
{
	struct acpi_ibm_softc *sc;
	int		ret = ENXIO;

	sc = device_get_softc(dev);

	if (ACPI_ID_PROBE(device_get_parent(dev), dev, ibm_id)) {
		device_set_desc(dev, "IBM ThinkPad Button");
		ret = 0;
	}
	return (ret);
}

static int
acpi_ibm_call_two_method(device_t dev, char *name, int val1, int val2)
{
	ACPI_OBJECT	arg [2];
	ACPI_OBJECT_LIST args = {.Count = 2,.Pointer = arg};
	arg[0].Type = ACPI_TYPE_INTEGER;
	arg[0].Integer.Value = val1;
	arg[1].Type = ACPI_TYPE_INTEGER;
	arg[1].Integer.Value = val2;
	return AcpiEvaluateObject(acpi_get_handle(dev), name, &args, NULL);
}

static int
acpi_ibm_attach(device_t dev)
{
	struct acpi_ibm_softc *sc;
	ACPI_STATUS	status;
	ACPI_HANDLE	h;
	int		dummy;
	struct sysctl_oid *oid;
	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);

	sc = device_get_softc(dev);
	sc->device_flag = 0;
	if (ACPI_FAILURE
	    (acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_INTERFACE_VERSION, &sc->ibm_version))) {
		sc->ibm_version = 0;
	}
	device_printf(dev, "Version %x\n", sc->ibm_version);
	if (ACPI_FAILURE
	    (acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_AVAIL_MASK, &sc->ibm_availmask)))
		sc->ibm_availmask = 0xffffffff;

	if (ACPI_FAILURE
	    (acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_CURRENT_MASK, &sc->ibm_initialmask)))
		sc->ibm_initialmask = 0xffffffff;
	else
		sc->device_flag |= IBM_MHKN_AVAIL;

	if (ACPI_SUCCESS(status = AcpiGetHandle(acpi_get_handle(dev), IBM_NAME_MODIFY_MASK, &h)))
		sc->device_flag |= IBM_MHKM_AVAIL;
	else
		printf("%s\n", AcpiFormatException(status));

	device_printf(dev, "Available Mask %x\n", sc->ibm_availmask);
	device_printf(dev, "Initial Mask %x\n", sc->ibm_initialmask);
	/* Install Specific Handler */
	status = AcpiInstallNotifyHandler(acpi_get_handle(dev), ACPI_DEVICE_NOTIFY, acpi_ibm_notify_handler, dev);
	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
			SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
			OID_AUTO, "key_mask", CTLTYPE_INT | CTLFLAG_RW,
			dev, 0,
			sysctl_acpi_ibm_mask_handler, "I", "Hot key mask");
	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
		       SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
		       OID_AUTO, "version", CTLFLAG_RD,
		       &sc->ibm_version, 0, "Interface version");
	SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
		       SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
		       OID_AUTO, "avail_mask", CTLFLAG_RD,
		       &sc->ibm_availmask, 0, "Available Key mask");
	if (ACPI_FAILURE(acpi_SetInteger(acpi_get_handle(dev), IBM_NAME_ENABLE, 1)))
		goto fail;
	sc->ibm_enable = 1;
	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
			SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
			OID_AUTO, "enable", CTLTYPE_INT | CTLFLAG_RW,
			dev, 0,
		     sysctl_acpi_ibm_enable_handler, "I", "Hot key enable");
	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
			SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
			OID_AUTO, "misckey", CTLTYPE_INT | CTLFLAG_RD,
			dev, 0,
	       sysctl_acpi_ibm_misckey_handler, "I", "Key Status: Poll me");
	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
			SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
			OID_AUTO, "brightness", CTLTYPE_INT | CTLFLAG_RD,
			dev, 0,
		     sysctl_acpi_ibm_brightness_handler, "I", "Brightness");

	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
			SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
			OID_AUTO, "volume", CTLTYPE_INT | CTLFLAG_RD,
			dev, 0,
			sysctl_acpi_ibm_volume_handler, "I", "Volume");

	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
			SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
			OID_AUTO, "mute", CTLTYPE_INT | CTLFLAG_RD,
			dev, 0,
			sysctl_acpi_ibm_mute_handler, "I", "Muting");


	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
			SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
			OID_AUTO, "keylight", CTLTYPE_INT | CTLFLAG_RD,
			dev, 0,
			sysctl_acpi_ibm_keylight_handler, "I", "Key Light");
	if (ACPI_SUCCESS(acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_GET_WIRELESS, &dummy))) {
		oid = SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
			       SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
		    OID_AUTO, "bluetooth", CTLTYPE_INT | CTLFLAG_RW, dev, 0,
		 sysctl_acpi_ibm_wireless_handler, "I", "Bluetooth Enable");
		sc->oid_bluetooth = oid;
		oid = SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
			       SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
			 OID_AUTO, "wlan", CTLTYPE_INT | CTLFLAG_RW, dev, 0,
		      sysctl_acpi_ibm_wireless_handler, "I", "WLAN Enable");
		sc->oid_wlan = oid;
	}
	return_VALUE(0);
fail:
	device_printf(dev, "FAILED\n");
	AcpiRemoveNotifyHandler(acpi_get_handle(dev), ACPI_DEVICE_NOTIFY, acpi_ibm_notify_handler);
	return_VALUE(EINVAL);
}

static int
acpi_ibm_detach(device_t dev)
{
	ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__);

	struct acpi_ibm_softc *sc = device_get_softc(dev);
	acpi_SetInteger(acpi_get_handle(dev), IBM_NAME_ENABLE, 0);
	acpi_ibm_enable_mask(dev, sc->ibm_initialmask);

	AcpiRemoveNotifyHandler(acpi_get_handle(dev), ACPI_DEVICE_NOTIFY, acpi_ibm_notify_handler);
	return_VALUE(0);
}
#if 0
static int
acpi_ibm_suspend(device_t dev)
{
	struct acpi_ibm_softc *sc = device_get_softc(dev);
	return_VALUE(0);
}

static int
acpi_ibm_resume(device_t dev)
{
	return (0);
}
#endif
static void
acpi_ibm_notify_handler(ACPI_HANDLE h, UINT32 notify,
			void *context)
{
	int		mhkp      , arg, type;
	device_t	dev = context;
	struct acpi_ibm_softc *sc = device_get_softc(dev);

	printf("IBM:NOTIFY:%x\n", notify);
	if (notify != 0x80) {
		printf("Unknown notify\n");
	}
	for (;;) {

		acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_GET_EVENT, &mhkp);

		if (mhkp == 0) {
			break;
		}
		printf("notify:%x\n", mhkp);

		type = (mhkp >> 12) & 0xf;
		arg = mhkp & 0xfff;
		switch (type) {
		case 1:
			if (!(sc->ibm_availmask & (1 << (arg - 1)))) {
				printf("Unknown key %d\n", arg);
				break;
			}
			acpi_UserNotify("IBM", h, (arg & 0xff));
			break;
		default:
			break;
		}
	}
}

static int
acpi_ibm_enable_mask(device_t dev, int val)
{
	int		i;
	struct acpi_ibm_softc *sc = device_get_softc(dev);

	if (!(sc->device_flag | IBM_MHKM_AVAIL)) {
		return -1;
	}
	for (i = 0; i < 32; i++) {
		acpi_ibm_call_two_method(dev, IBM_NAME_MODIFY_MASK, i + 1, 1);
		if (!((1 << i) & val))
			acpi_ibm_call_two_method(dev, IBM_NAME_MODIFY_MASK, i + 1, 0);
	}
	return 0;
}

static int
sysctl_acpi_ibm_mask_handler(SYSCTL_HANDLER_ARGS)
{
	device_t	dev = arg1;
	int		val = 0xffffffff;
	int		error = 0;

	struct acpi_ibm_softc *sc = device_get_softc(dev);

	if (sc->device_flag & IBM_MHKN_AVAIL)
		acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_CURRENT_MASK, &val);

	error = sysctl_handle_int(oidp, &val, 0, req);

	if (error || !req->newptr)
		return error;

	val &= sc->ibm_availmask;
	val |= sc->ibm_initialmask;

	acpi_ibm_enable_mask(dev, val);

	return 0;
}

static int 
sysctl_acpi_ibm_misckey_handler(SYSCTL_HANDLER_ARGS)
{
	int		val       , error;
	val = rtcin(IBM_RTC_MISCKEY);
	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr)
		return error;
	return 0;
}


static int 
sysctl_acpi_ibm_brightness_handler(SYSCTL_HANDLER_ARGS)
{
	int		val       , error;
	val = rtcin(IBM_RTC_BRIGHTNESS);
	val &= IBM_RTC_MASK_BRI;
	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr)
		return error;
	return 0;
}

static int 
sysctl_acpi_ibm_mute_handler(SYSCTL_HANDLER_ARGS)
{
	int		val       , error;
	val = rtcin(IBM_RTC_VOLUME);
	val = ((val & IBM_RTC_MASK_MUTE) == IBM_RTC_MASK_MUTE);

	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr)
		return error;
	return 0;
}

static int 
sysctl_acpi_ibm_keylight_handler(SYSCTL_HANDLER_ARGS)
{
	int		val       , error;
	val = ((rtcin(IBM_RTC_KEYLIGHT) & IBM_RTC_MASK_KEYLIGHT)
	       == IBM_RTC_MASK_KEYLIGHT);

	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr)
		return error;
	return 0;
}

static int 
sysctl_acpi_ibm_volume_handler(SYSCTL_HANDLER_ARGS)
{
	int		val       , error;
	val = rtcin(IBM_RTC_VOLUME);
	val &= IBM_RTC_MASK_VOL;
	error = sysctl_handle_int(oidp, &val, 0, req);
	if (error || !req->newptr)
		return error;
	return 0;
}

static int
sysctl_acpi_ibm_enable_handler(SYSCTL_HANDLER_ARGS)
{
	device_t	dev = arg1;
	struct acpi_ibm_softc *sc = device_get_softc(dev);
	int		error = 0;

	error = sysctl_handle_int(oidp, &sc->ibm_enable, 0, req);

	if (error || !req->newptr)
		return error;

	if (sc->ibm_enable)
		sc->ibm_enable = 1;
	else
		sc->ibm_enable = 0;
	acpi_SetInteger(acpi_get_handle(dev), IBM_NAME_ENABLE, sc->ibm_enable);

	return 0;
}

static int 
sysctl_acpi_ibm_wireless_handler(SYSCTL_HANDLER_ARGS)
{
	device_t	dev = arg1;
	struct acpi_ibm_softc *sc = device_get_softc(dev);
	int		error = 0,	val, oldval, mask;
	if (sc->oid_bluetooth == oidp) {
		mask = 2;
	} else if (sc->oid_wlan == oidp) {
		mask = 4;
	} else {
		printf("WARNING: wrong handler invoked\n");
		return ENOENT;
	}

	acpi_GetInteger(acpi_get_handle(dev), IBM_NAME_GET_WIRELESS, &oldval);
	val = !((oldval & mask) == 0);
	error = sysctl_handle_int(oidp, &val, 0, req);

	if (error || !req->newptr)
		return error;
	oldval &= (~mask);
	if (val)
		oldval |= mask;
	acpi_SetInteger(acpi_get_handle(dev), IBM_NAME_SET_WIRELESS, oldval);
	return 0;


}
OpenPOWER on IntegriCloud