summaryrefslogtreecommitdiff
path: root/sys/dev/ic/spic.c
blob: 7c311d5a37565d1b1a1f0e1b144cbf23b12dec7d (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
/*	$NetBSD: spic.c,v 1.22 2022/04/10 09:50:45 andvar Exp $	*/

/*
 * Copyright (c) 2002 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Lennart Augustsson (lennart@augustsson.net) at
 * Carlstedt Research & Technology.
 *
 * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

/*
 * The SPIC is used on some Sony Vaios to handle the jog dial and other
 * peripherals.
 * The protocol used by the SPIC seems to vary wildly among the different
 * models, and I've found no documentation.
 * This file handles the jog dial on the SRX77 model, and perhaps nothing
 * else.
 *
 * The general way of talking to the SPIC was gleaned from the Linux and
 * FreeBSD drivers.  The hex numbers were taken from these drivers (they
 * come from reverse engineering.)
 *
 * TODO:
 *   Make it handle more models.
 *   Figure out why the interrupt mode doesn't work.
 */


#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: spic.c,v 1.22 2022/04/10 09:50:45 andvar Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/callout.h>

#include <sys/bus.h>

#include <dev/sysmon/sysmonvar.h>

#include <dev/ic/spicvar.h>

#include <dev/wscons/wsconsio.h>
#include <dev/wscons/wsmousevar.h>

#define	SPIC_EVENT_BRIGHTNESS_DOWN	0x15
#define	SPIC_EVENT_BRIGHTNESS_UP	0x16

#define POLLRATE (hz/30)

/* Some hardware constants */
#define SPIC_PORT1 0
#define SPIC_PORT2 4

#ifdef SPIC_DEBUG
int spicdebug = 0;
#endif

static int spicerror = 0;

static int	spic_enable(void *);
static void	spic_disable(void *);
static int	spic_ioctl(void *, u_long, void *, int, struct lwp *);

static const struct wsmouse_accessops spic_accessops = {
	spic_enable,
	spic_ioctl,
	spic_disable,
};

#define SPIC_COMMAND(quiet, command) do { \
	unsigned int n = 10000; \
	while (--n && (command)) \
		delay(1); \
	if (n == 0 && !(quiet)) { \
		printf("spic0: command failed at line %d\n", __LINE__); \
		spicerror++; \
	} \
} while (0)

#if 0
#define INB(sc, p) (delay(100), printf("inb(%x)=%x\n", (uint)sc->sc_ioh+p, bus_space_read_1(sc->sc_iot, sc->sc_ioh, p)), delay(100), bus_space_read_1(sc->sc_iot, sc->sc_ioh, (p)))
#define OUTB(sc, v, p) do { delay(100); bus_space_write_1(sc->sc_iot, sc->sc_ioh, (p), (v)); printf("outb(%x, %x)\n", (uint)sc->sc_ioh+p, v); } while(0)
#else
#define INB(sc, p) (delay(100), bus_space_read_1(sc->sc_iot, sc->sc_ioh, (p)))
#define OUTB(sc, v, p) do { delay(100); bus_space_write_1(sc->sc_iot, sc->sc_ioh, (p), (v)); } while(0)
#endif

static u_int8_t
spic_call1(struct spic_softc *sc, u_int8_t dev)
{
	u_int8_t v2;

	SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
	OUTB(sc, dev, SPIC_PORT2);
	(void)INB(sc, SPIC_PORT2);
	v2 = INB(sc, SPIC_PORT1);
	return v2;
}

static u_int8_t
spic_call2(struct spic_softc *sc, u_int8_t dev, u_int8_t fn)
{
	u_int8_t v1;

	SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
	OUTB(sc, dev, SPIC_PORT2);
	SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
	OUTB(sc, fn, SPIC_PORT1);
	v1 = INB(sc, SPIC_PORT1);
	return v1;
}

/* Interrupt handler: some event is available */
int
spic_intr(void *v) {
	struct spic_softc *sc = v;
	u_int8_t v1, v2;
	int dz, buttons;

	v1 = INB(sc, SPIC_PORT1);
	v2 = INB(sc, SPIC_PORT2);

	/* Handle lid switch */
	if (v2 == 0x30) {
		switch (v1) {
		case 0x50:	/* opened */
			sysmon_pswitch_event(&sc->sc_smpsw[SPIC_PSWITCH_LID],
			    PSWITCH_EVENT_RELEASED);
			goto skip;
			break;
		case 0x51:	/* closed */
			sysmon_pswitch_event(&sc->sc_smpsw[SPIC_PSWITCH_LID],
			    PSWITCH_EVENT_PRESSED);
			goto skip;
			break;
		default:
			aprint_debug_dev(sc->sc_dev, "unknown lid event 0x%02x\n", v1);
			goto skip;
			break;
		}
	}

	/* Handle suspend/hibernate buttons */
	if (v2 == 0x20) {
		switch (v1) {
		case 0x10:	/* suspend */
			sysmon_pswitch_event(
			    &sc->sc_smpsw[SPIC_PSWITCH_SUSPEND],
			    PSWITCH_EVENT_PRESSED);
			goto skip;
			break;
		case 0x1c:	/* hibernate */
			sysmon_pswitch_event(
			    &sc->sc_smpsw[SPIC_PSWITCH_HIBERNATE],
			    PSWITCH_EVENT_PRESSED);
			goto skip;
			break;
		}
	}

	buttons = 0;
	if (v1 & 0x40)
		buttons |= 1 << 1;
	if (v1 & 0x20)
		buttons |= 1 << 5;
	dz = v1 & 0x1f;
	switch (dz) {
	case 0:
	case 1:
	case 2:
	case 3:
		break;
	case 0x1f:
	case 0x1e:
	case 0x1d:
		dz -= 0x20;
		break;
	case SPIC_EVENT_BRIGHTNESS_UP:
		pmf_event_inject(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_UP);
		break;
	case SPIC_EVENT_BRIGHTNESS_DOWN:
		pmf_event_inject(sc->sc_dev, PMFE_DISPLAY_BRIGHTNESS_DOWN);
		break;
	default:
		printf("spic0: v1=0x%02x v2=0x%02x\n", v1, v2);
		goto skip;
	}

	if (!sc->sc_enabled) {
		/*printf("spic: not enabled\n");*/
		goto skip;
	}

	if (dz != 0 || buttons != sc->sc_buttons) {
#ifdef SPIC_DEBUG
		if (spicdebug)
			printf("spic: but=0x%x dz=%d v1=0x%02x v2=0x%02x\n",
			       buttons, dz, v1, v2);
#endif
		sc->sc_buttons = buttons;
		if (sc->sc_wsmousedev != NULL) {
			wsmouse_input(sc->sc_wsmousedev, buttons, 0, 0, dz, 0,
				      WSMOUSE_INPUT_DELTA);
		}
	}

skip:
	spic_call2(sc, 0x81, 0xff); /* Clear event */
	return (1);
}

static void
spictimeout(void *v)
{
	struct spic_softc *sc = v;
	int s;

	if (spicerror >= 3)
		return;

	s = spltty();
	spic_intr(v);
	splx(s);
	callout_reset(&sc->sc_poll, POLLRATE, spictimeout, sc);
}

void
spic_attach(struct spic_softc *sc)
{
	struct wsmousedev_attach_args a;
	int i, rv;

#ifdef SPIC_DEBUG
	if (spicdebug)
		printf("spic_attach %x\n", (uint)sc->sc_ioh);
#endif

	callout_init(&sc->sc_poll, 0);

	spic_call1(sc, 0x82);
	spic_call2(sc, 0x81, 0xff);
	spic_call1(sc, 0x92);	/* or 0x82 */

	a.accessops = &spic_accessops;
	a.accesscookie = sc;
	sc->sc_wsmousedev = config_found(sc->sc_dev, &a, wsmousedevprint,
	    CFARGS_NONE);

	sc->sc_smpsw[SPIC_PSWITCH_LID].smpsw_name = "spiclid0";
	sc->sc_smpsw[SPIC_PSWITCH_LID].smpsw_type = PSWITCH_TYPE_LID;
	sc->sc_smpsw[SPIC_PSWITCH_SUSPEND].smpsw_name = "spicsuspend0";
	sc->sc_smpsw[SPIC_PSWITCH_SUSPEND].smpsw_type = PSWITCH_TYPE_SLEEP;
	sc->sc_smpsw[SPIC_PSWITCH_HIBERNATE].smpsw_name = "spichibernate0";
	sc->sc_smpsw[SPIC_PSWITCH_HIBERNATE].smpsw_type = PSWITCH_TYPE_SLEEP;

	for (i = 0; i < SPIC_NPSWITCH; i++) {
		rv = sysmon_pswitch_register(&sc->sc_smpsw[i]);
		if (rv != 0)
			aprint_error_dev(sc->sc_dev, "unable to register %s with sysmon\n",
			    sc->sc_smpsw[i].smpsw_name);
	}

	callout_reset(&sc->sc_poll, POLLRATE, spictimeout, sc);

	return;
}

bool
spic_suspend(device_t dev, const pmf_qual_t *qual)
{
	struct spic_softc *sc = device_private(dev);

	callout_stop(&sc->sc_poll);

	return true;
}

bool
spic_resume(device_t dev, const pmf_qual_t *qual)
{
	struct spic_softc *sc = device_private(dev);

	spic_call1(sc, 0x82);
	spic_call2(sc, 0x81, 0xff);
	spic_call1(sc, 0x92);	/* or 0x82 */

	callout_reset(&sc->sc_poll, POLLRATE, spictimeout, sc);
	return true;
}

static int
spic_enable(void *v)
{
	struct spic_softc *sc = v;

	if (sc->sc_enabled)
		return (EBUSY);

	sc->sc_enabled = 1;
	sc->sc_buttons = 0;

#ifdef SPIC_DEBUG
	if (spicdebug)
		printf("spic_enable\n");
#endif

	return (0);
}

static void
spic_disable(void *v)
{
	struct spic_softc *sc = v;

#ifdef DIAGNOSTIC
	if (!sc->sc_enabled) {
		printf("spic_disable: not enabled\n");
		return;
	}
#endif

	sc->sc_enabled = 0;

#ifdef SPIC_DEBUG
	if (spicdebug)
		printf("spic_disable\n");
#endif
}

static int
spic_ioctl(void *v, u_long cmd, void *data,
    int flag, struct lwp *l)
{
	switch (cmd) {
	case WSMOUSEIO_GTYPE:
		/* XXX this is not really correct */
		*(u_int *)data = WSMOUSE_TYPE_PS2;
		return (0);
	}

	return (-1);
}