summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/acpi_acad.c
blob: d39232eac2a9b0c18294e2571cf129e66ae6d4fd (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
/*	$NetBSD: acpi_acad.c,v 1.42 2010/03/05 14:00:16 jruoho Exp $	*/

/*
 * Copyright 2001 Wasabi Systems, Inc.
 * All rights reserved.
 *
 * Written by Jason R. Thorpe for Wasabi Systems, Inc.
 *
 * 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.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed for the NetBSD Project by
 *	Wasabi Systems, Inc.
 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
 *    or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC
 * 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.
 */

/*
 * ACPI AC Adapter driver.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: acpi_acad.c,v 1.42 2010/03/05 14:00:16 jruoho Exp $");

#include <sys/param.h>
#include <sys/device.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/systm.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>

#define _COMPONENT		 ACPI_ACAD_COMPONENT
ACPI_MODULE_NAME		 ("acpi_acad")

struct acpiacad_softc {
	struct acpi_devnode	*sc_node;
	struct sysmon_envsys	*sc_sme;
	struct sysmon_pswitch	 sc_smpsw;
	envsys_data_t		 sc_sensor;
	kmutex_t		 sc_mutex;
	int			 sc_status;
};

static const char * const acad_hid[] = {
	"ACPI0003",
	NULL
};

static int	acpiacad_match(device_t, cfdata_t, void *);
static void	acpiacad_attach(device_t, device_t, void *);
static int	acpiacad_detach(device_t, int);
static bool	acpiacad_resume(device_t, const pmf_qual_t *);
static void	acpiacad_get_status(void *);
static void	acpiacad_notify_handler(ACPI_HANDLE, uint32_t, void *);
static void	acpiacad_init_envsys(device_t);

CFATTACH_DECL_NEW(acpiacad, sizeof(struct acpiacad_softc),
    acpiacad_match, acpiacad_attach, acpiacad_detach, NULL);

/*
 * acpiacad_match:
 *
 *	Autoconfiguration `match' routine.
 */
static int
acpiacad_match(device_t parent, cfdata_t match, void *aux)
{
	struct acpi_attach_args *aa = aux;

	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
		return 0;

	return acpi_match_hid(aa->aa_node->ad_devinfo, acad_hid);
}

/*
 * acpiacad_attach:
 *
 *	Autoconfiguration `attach' routine.
 */
static void
acpiacad_attach(device_t parent, device_t self, void *aux)
{
	struct acpiacad_softc *sc = device_private(self);
	struct acpi_attach_args *aa = aux;
	ACPI_STATUS rv;

	aprint_naive(": ACPI AC Adapter\n");
	aprint_normal(": ACPI AC Adapter\n");

	sc->sc_sme = NULL;
	sc->sc_status = -1;
	sc->sc_node = aa->aa_node;

	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);

	sc->sc_smpsw.smpsw_name = device_xname(self);
	sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_ACADAPTER;

	(void)sysmon_pswitch_register(&sc->sc_smpsw);
	(void)pmf_device_register(self, NULL, acpiacad_resume);

	rv = AcpiInstallNotifyHandler(sc->sc_node->ad_handle,
	    ACPI_ALL_NOTIFY, acpiacad_notify_handler, self);

	if (ACPI_SUCCESS(rv))
		acpiacad_init_envsys(self);
}

/*
 * acpiacad_detach:
 *
 *	Autoconfiguration `detach' routine.
 */
static int
acpiacad_detach(device_t self, int flags)
{
	struct acpiacad_softc *sc = device_private(self);
	ACPI_STATUS rv;

	rv = AcpiRemoveNotifyHandler(sc->sc_node->ad_handle,
	    ACPI_ALL_NOTIFY, acpiacad_notify_handler);

	if (ACPI_FAILURE(rv))
		return EBUSY;

	mutex_destroy(&sc->sc_mutex);

	if (sc->sc_sme != NULL)
		sysmon_envsys_unregister(sc->sc_sme);

	pmf_device_deregister(self);
	sysmon_pswitch_unregister(&sc->sc_smpsw);

	return 0;
}

/*
 * acpiacad_resume:
 *
 * 	Queue a new status check.
 */
static bool
acpiacad_resume(device_t dv, const pmf_qual_t *qual)
{

	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);

	return true;
}

/*
 * acpiacad_get_status:
 *
 *	Get, and possibly display, the current AC line status.
 */
static void
acpiacad_get_status(void *arg)
{
	device_t dv = arg;
	struct acpiacad_softc *sc = device_private(dv);
	ACPI_INTEGER status;
	ACPI_STATUS rv;

	mutex_enter(&sc->sc_mutex);

	rv = acpi_eval_integer(sc->sc_node->ad_handle, "_PSR", &status);

	if (ACPI_FAILURE(rv))
		goto fail;

	if (status != 0 && status != 1) {
		rv = AE_BAD_VALUE;
		goto fail;
	}

	if (sc->sc_status != status) {

		/*
		 * If status has changed, send the event:
		 *
		 * PSWITCH_EVENT_PRESSED  : _PSR = 1 : AC online.
		 * PSWITCH_EVENT_RELEASED : _PSR = 0 : AC offline.
		 */
		sysmon_pswitch_event(&sc->sc_smpsw, (status != 0) ?
	    	    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);

		aprint_debug_dev(dv, "AC adapter %sconnected\n",
		    status == 0 ? "not " : "");
	}

	sc->sc_status = status;
	sc->sc_sensor.state = ENVSYS_SVALID;
	sc->sc_sensor.value_cur = sc->sc_status;

	mutex_exit(&sc->sc_mutex);

	return;

fail:
	sc->sc_status = -1;
	sc->sc_sensor.state = ENVSYS_SINVALID;

	aprint_debug_dev(dv, "failed to evaluate _PSR: %s\n",
	    AcpiFormatException(rv));

	mutex_exit(&sc->sc_mutex);
}

/*
 * acpiacad_notify_handler:
 *
 *	Callback from ACPI interrupt handler to notify us of an event.
 */
static void
acpiacad_notify_handler(ACPI_HANDLE handle, uint32_t notify, void *context)
{
	static const int handler = OSL_NOTIFY_HANDLER;
	device_t dv = context;

	switch (notify) {
	/*
	 * XXX So, BusCheck is not exactly what I would expect,
	 * but at least my IBM T21 sends it on AC adapter status
	 * change.  --thorpej@wasabisystems.com
	 */
	/*
	 * XXX My Acer TravelMate 291 sends DeviceCheck on AC
	 * adapter status change.
	 *  --rpaulo@NetBSD.org
	 */
	/*
	 * XXX Sony VAIO VGN-N250E sends BatteryInformationChanged on AC
	 * adapter status change.
	 *  --jmcneill@NetBSD.org
	 */
	case ACPI_NOTIFY_BusCheck:
	case ACPI_NOTIFY_DeviceCheck:
	case ACPI_NOTIFY_PowerSourceStatusChanged:
	case ACPI_NOTIFY_BatteryInformationChanged:
		(void)AcpiOsExecute(handler, acpiacad_get_status, dv);
		break;

	default:
		aprint_error_dev(dv, "unknown notify 0x%02X\n", notify);
	}
}

static void
acpiacad_init_envsys(device_t dv)
{
	struct acpiacad_softc *sc = device_private(dv);

	sc->sc_sme = sysmon_envsys_create();

	sc->sc_sensor.state = ENVSYS_SINVALID;
	sc->sc_sensor.units = ENVSYS_INDICATOR;

 	(void)strlcpy(sc->sc_sensor.desc, "connected", ENVSYS_DESCLEN);

	if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensor) != 0)
		goto fail;

	sc->sc_sme->sme_name = device_xname(dv);
	sc->sc_sme->sme_class = SME_CLASS_ACADAPTER;
	sc->sc_sme->sme_flags = SME_DISABLE_REFRESH;

	if (sysmon_envsys_register(sc->sc_sme) != 0)
		goto fail;

	(void)AcpiOsExecute(OSL_NOTIFY_HANDLER, acpiacad_get_status, dv);

	return;

fail:
	aprint_error_dev(dv, "failed to initialize sysmon\n");
	sysmon_envsys_destroy(sc->sc_sme);
	sc->sc_sme = NULL;
}

#ifdef _MODULE

MODULE(MODULE_CLASS_DRIVER, acpiacad, NULL);
CFDRIVER_DECL(acpiacad, DV_DULL, NULL);

static int acpiacadloc[] = { -1 };
extern struct cfattach acpiacad_ca;

static struct cfparent acpiparent = {
	"acpinodebus", NULL, DVUNIT_ANY
};

static struct cfdata acpiacad_cfdata[] = {
	{
		.cf_name = "acpiacad",
		.cf_atname = "acpiacad",
		.cf_unit = 0,
		.cf_fstate = FSTATE_STAR,
		.cf_loc = acpiacadloc,
		.cf_flags = 0,
		.cf_pspec = &acpiparent,
	},

	{ NULL }
};

static int
acpiacad_modcmd(modcmd_t cmd, void *context)
{
	int err;

	switch (cmd) {

	case MODULE_CMD_INIT:

		err = config_cfdriver_attach(&acpiacad_cd);

		if (err != 0)
			return err;

		err = config_cfattach_attach("acpiacad", &acpiacad_ca);

		if (err != 0) {
			config_cfdriver_detach(&acpiacad_cd);
			return err;
		}

		err = config_cfdata_attach(acpiacad_cfdata, 1);

		if (err != 0) {
			config_cfattach_detach("acpiacad", &acpiacad_ca);
			config_cfdriver_detach(&acpiacad_cd);
			return err;
		}

		return 0;

	case MODULE_CMD_FINI:

		err = config_cfdata_detach(acpiacad_cfdata);

		if (err != 0)
			return err;

		config_cfattach_detach("acpiacad", &acpiacad_ca);
		config_cfdriver_detach(&acpiacad_cd);

		return 0;

	default:
		return ENOTTY;
	}
}

#endif	/* _MODULE */