summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/acpidock.c
blob: 9e94df40641011cae2bc94fe3cc61c1bc589128d (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
/* $OpenBSD: acpidock.c,v 1.15 2007/02/15 20:20:39 mk Exp $ */
/*
 * Copyright (c) 2006,2007 Michael Knudsen <mk@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/sensors.h>

#include <machine/bus.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpidev.h>
#include <dev/acpi/amltypes.h>
#include <dev/acpi/dsdt.h>

int	acpidock_match(struct device *, void *, void *);
void	acpidock_attach(struct device *, struct device *, void *);

struct cfattach acpidock_ca = {
	sizeof(struct acpidock_softc), acpidock_match, acpidock_attach
};

struct cfdriver acpidock_cd = {
	NULL, "acpidock", DV_DULL
};


int	acpidock_docklock(struct acpidock_softc *, int);
int	acpidock_dockctl(struct acpidock_softc *, int);
int	acpidock_eject(struct acpidock_softc *);
int	acpidock_notify(struct aml_node *, int, void *);
int	acpidock_status(struct acpidock_softc *);

int
acpidock_match(struct device *parent, void *match, void *aux)
{
	struct acpi_attach_args	 *aaa = aux;
	struct cfdata		 *cf = match;

	/* sanity */
	if (aaa->aaa_name == NULL ||
	    strcmp(aaa->aaa_name, cf->cf_driver->cd_name) != 0 ||
	    aaa->aaa_table != NULL)
		return (0);

	return (1);
}

void
acpidock_attach(struct device *parent, struct device *self, void *aux)
{
	struct acpidock_softc	*sc = (struct acpidock_softc *)self;
	struct acpi_attach_args *aa = aux;

	sc->sc_acpi = (struct acpi_softc *)parent;
	sc->sc_devnode = aa->aaa_node->child;

	printf(": %s", sc->sc_devnode->parent->name);

	acpidock_status(sc);
	if (sc->sc_docked == ACPIDOCK_STATUS_DOCKED) {
		acpidock_docklock(sc, 1);
		acpidock_dockctl(sc, 1);
	} else {
		acpidock_dockctl(sc, 0);
		acpidock_docklock(sc, 0);
	}

	acpidock_status(sc);
	printf(":%s docked (%d)\n",
	    sc->sc_docked == ACPIDOCK_STATUS_DOCKED ? "" : " not",
	    sc->sc_sta);

	strlcpy(sc->sc_sensdev.xname, DEVNAME(sc),
	    sizeof(sc->sc_sensdev.xname));
	if (sc->sc_docked)
		strlcpy(sc->sc_sens[0].desc, "docked",
		    sizeof(sc->sc_sens[0].desc));
	else
		strlcpy(sc->sc_sens[0].desc, "not docked",
		    sizeof(sc->sc_sens[0].desc));

	sc->sc_sens[0].type = SENSOR_INDICATOR;
	sc->sc_sens[0].value = sc->sc_docked == ACPIDOCK_STATUS_DOCKED;
	sensor_attach(&sc->sc_sensdev, &sc->sc_sens[0]);
	sensordev_install(&sc->sc_sensdev);

	aml_register_notify(sc->sc_devnode->parent, aa->aaa_dev, 
	    acpidock_notify, sc, ACPIDEV_NOPOLL);

}

int
acpidock_status(struct acpidock_softc *sc)
{
	struct aml_value	res;
	int			rv, sta;

	if (aml_evalname(sc->sc_acpi, sc->sc_devnode, "_STA", 0, NULL,
	    &res) != 0)
		rv = 0;
	else
		rv = 1;
	
	sta = aml_val2int(&res);
	sc->sc_sta = sta;

	/* XXX: _STA bit defines */
	sc->sc_docked = sta & 0x01;

	aml_freevalue(&res);

	return (rv);
}

int
acpidock_docklock(struct acpidock_softc *sc, int lock)
{
	struct aml_value	cmd;
	struct aml_value	res;

	memset(&cmd, 0, sizeof cmd);
	cmd.v_integer = lock;
	cmd.type = AML_OBJTYPE_INTEGER;
	if (aml_evalname(sc->sc_acpi, sc->sc_devnode, "_LCK", 1, &cmd,
	    &res) != 0) {
		dnprintf(20, "%s: _LCD %d failed\n", DEVNAME(sc), lock);

		aml_freevalue(&res);
		return (0);
	} else {
		dnprintf(20, "%s: _LCK %d successful\n", DEVNAME(sc), lock);

		aml_freevalue(&res);
		return (1);
	}

}

int
acpidock_dockctl(struct acpidock_softc *sc, int dock)
{
	struct aml_value	cmd;
	struct aml_value	res;

	memset(&cmd, 0, sizeof cmd);
	cmd.v_integer = 1;
	cmd.type = AML_OBJTYPE_INTEGER;
	if (aml_evalname(sc->sc_acpi, sc->sc_devnode, "_DCK", 1, &cmd,
	    &res) != 0) {
		/* XXX */
		dnprintf(15, "%s: _DCK %d failed\n", DEVNAME(sc), dock);

		sc->sc_docked = 0;

		aml_freevalue(&res);
		return (0);
	} else {
		dnprintf(15, "%s: _DCK %d successful\n", DEVNAME(sc), dock);

		sc->sc_docked = 1;
		aml_freevalue(&res);
		return (1);
	}

}

int
acpidock_eject(struct acpidock_softc *sc)
{
	struct aml_value	cmd;
	struct aml_value	res;

	memset(&cmd, 0, sizeof cmd);
	cmd.v_integer = 1;
	cmd.type = AML_OBJTYPE_INTEGER;
	if (aml_evalname(sc->sc_acpi, sc->sc_devnode, "_EJ0", 1, &cmd,
	    &res) != 0) {
		/* XXX */
		dnprintf(15, "%s: _EJ0 failed\n", DEVNAME(sc));

		aml_freevalue(&res);
		return (0);
	} else {
		dnprintf(15, "%s: _EJ0 successful\n", DEVNAME(sc));

		sc->sc_docked = 0;
		aml_freevalue(&res);
		return (1);
	}

}

int
acpidock_notify(struct aml_node *node, int notify_type, void *arg)
{
	struct acpidock_softc	*sc = arg;

	printf("acpidock_notify: notify %d\n", notify_type);
	switch (notify_type) {
	case ACPIDOCK_EVENT_INSERT:
		dnprintf(10, "INSERT\n");
		acpidock_docklock(sc, 1);
		acpidock_dockctl(sc, 1);
		break;
	case ACPIDOCK_EVENT_EJECT:
		dnprintf(10, "EJECT\n");
		acpidock_dockctl(sc, 0);
		acpidock_docklock(sc, 0);

		/* now actually undock */
		acpidock_eject(sc);
		
		break;
	}

	acpidock_status(sc);
	sc->sc_sens[0].value = sc->sc_docked == ACPIDOCK_STATUS_DOCKED;
	if (sc->sc_docked)
		strlcpy(sc->sc_sens[0].desc, "docked",
		    sizeof(sc->sc_sens[0].desc));
	else
		strlcpy(sc->sc_sens[0].desc, "not docked",
		    sizeof(sc->sc_sens[0].desc));

	dnprintf(5, "acpidock_notify: status %s\n",
	    sc->sc_docked == ACPIDOCK_STATUS_DOCKED ? "docked" : "undocked");

	return (0);
}