spic.c revision 1.2 1 1.2 christos /* $NetBSD: spic.c,v 1.2 2005/12/12 01:18:29 christos Exp $ */
2 1.1 augustss
3 1.1 augustss /*
4 1.1 augustss * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 augustss * All rights reserved.
6 1.1 augustss *
7 1.1 augustss * This code is derived from software contributed to The NetBSD Foundation
8 1.1 augustss * by Lennart Augustsson (lennart (at) augustsson.net) at
9 1.1 augustss * Carlstedt Research & Technology.
10 1.1 augustss *
11 1.1 augustss * Redistribution and use in source and binary forms, with or without
12 1.1 augustss * modification, are permitted provided that the following conditions
13 1.1 augustss * are met:
14 1.1 augustss * 1. Redistributions of source code must retain the above copyright
15 1.1 augustss * notice, this list of conditions and the following disclaimer.
16 1.1 augustss * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 augustss * notice, this list of conditions and the following disclaimer in the
18 1.1 augustss * documentation and/or other materials provided with the distribution.
19 1.1 augustss * 3. All advertising materials mentioning features or use of this software
20 1.1 augustss * must display the following acknowledgement:
21 1.1 augustss * This product includes software developed by the NetBSD
22 1.1 augustss * Foundation, Inc. and its contributors.
23 1.1 augustss * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.1 augustss * contributors may be used to endorse or promote products derived
25 1.1 augustss * from this software without specific prior written permission.
26 1.1 augustss *
27 1.1 augustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.1 augustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.1 augustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.1 augustss * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.1 augustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.1 augustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.1 augustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.1 augustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.1 augustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.1 augustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.1 augustss * POSSIBILITY OF SUCH DAMAGE.
38 1.1 augustss */
39 1.1 augustss
40 1.1 augustss /*
41 1.1 augustss * The SPIC is used on some Sony Vaios to handle the jog dial and other
42 1.1 augustss * peripherals.
43 1.1 augustss * The protocol used by the SPIC seems to vary wildly among the different
44 1.1 augustss * models, and I've found no documentation.
45 1.1 augustss * This file handles the jog dial on the SRX77 model, and perhaps nothing
46 1.1 augustss * else.
47 1.1 augustss *
48 1.1 augustss * The general way of talking to the SPIC was gleaned from the Linux and
49 1.1 augustss * FreeBSD drivers. The hex numbers were taken from these drivers (they
50 1.1 augustss * come from reverese engineering.)
51 1.1 augustss *
52 1.1 augustss * TODO:
53 1.1 augustss * Make it handle more models.
54 1.1 augustss * Figure out why the interrupt mode doesn't work.
55 1.1 augustss */
56 1.1 augustss
57 1.1 augustss
58 1.1 augustss #include <sys/cdefs.h>
59 1.2 christos __KERNEL_RCSID(0, "$NetBSD: spic.c,v 1.2 2005/12/12 01:18:29 christos Exp $");
60 1.1 augustss
61 1.1 augustss #include <sys/param.h>
62 1.1 augustss #include <sys/systm.h>
63 1.1 augustss #include <sys/device.h>
64 1.1 augustss #include <sys/proc.h>
65 1.1 augustss #include <sys/kernel.h>
66 1.1 augustss #include <sys/callout.h>
67 1.1 augustss
68 1.1 augustss #include <machine/bus.h>
69 1.1 augustss
70 1.1 augustss #include <dev/ic/spicvar.h>
71 1.1 augustss
72 1.1 augustss #include <dev/wscons/wsconsio.h>
73 1.1 augustss #include <dev/wscons/wsmousevar.h>
74 1.1 augustss
75 1.1 augustss #define POLLRATE (hz/30)
76 1.1 augustss
77 1.1 augustss /* Some hardware constants */
78 1.1 augustss #define SPIC_PORT1 0
79 1.1 augustss #define SPIC_PORT2 4
80 1.1 augustss
81 1.1 augustss #ifdef SPIC_DEBUG
82 1.1 augustss int spicdebug = 0;
83 1.1 augustss #endif
84 1.1 augustss
85 1.1 augustss static int spic_enable(void *);
86 1.1 augustss static void spic_disable(void *);
87 1.2 christos static int spic_ioctl(void *, u_long, caddr_t, int, struct lwp *);
88 1.1 augustss
89 1.1 augustss static const struct wsmouse_accessops spic_accessops = {
90 1.1 augustss spic_enable,
91 1.1 augustss spic_ioctl,
92 1.1 augustss spic_disable,
93 1.1 augustss };
94 1.1 augustss
95 1.1 augustss #define SPIC_COMMAND(quiet, command) do { \
96 1.1 augustss unsigned int n = 10000; \
97 1.1 augustss while (--n && (command)) \
98 1.1 augustss delay(1); \
99 1.1 augustss if (n == 0 && !(quiet)) \
100 1.1 augustss printf("spic: command failed at line %d\n", __LINE__); \
101 1.1 augustss } while (0)
102 1.1 augustss
103 1.1 augustss #if 0
104 1.1 augustss #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)))
105 1.1 augustss #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)
106 1.1 augustss #else
107 1.1 augustss #define INB(sc, p) (delay(100), bus_space_read_1(sc->sc_iot, sc->sc_ioh, (p)))
108 1.1 augustss #define OUTB(sc, v, p) do { delay(100); bus_space_write_1(sc->sc_iot, sc->sc_ioh, (p), (v)); } while(0)
109 1.1 augustss #endif
110 1.1 augustss
111 1.1 augustss static u_int8_t
112 1.1 augustss spic_call1(struct spic_softc *sc, u_int8_t dev)
113 1.1 augustss {
114 1.1 augustss u_int8_t v1, v2;
115 1.1 augustss
116 1.1 augustss SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
117 1.1 augustss OUTB(sc, dev, SPIC_PORT2);
118 1.1 augustss v1 = INB(sc, SPIC_PORT2);
119 1.1 augustss v2 = INB(sc, SPIC_PORT1);
120 1.1 augustss return v2;
121 1.1 augustss }
122 1.1 augustss
123 1.1 augustss static u_int8_t
124 1.1 augustss spic_call2(struct spic_softc *sc, u_int8_t dev, u_int8_t fn)
125 1.1 augustss {
126 1.1 augustss u_int8_t v1;
127 1.1 augustss
128 1.1 augustss SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
129 1.1 augustss OUTB(sc, dev, SPIC_PORT2);
130 1.1 augustss SPIC_COMMAND(0, INB(sc, SPIC_PORT2) & 2);
131 1.1 augustss OUTB(sc, fn, SPIC_PORT1);
132 1.1 augustss v1 = INB(sc, SPIC_PORT1);
133 1.1 augustss return v1;
134 1.1 augustss }
135 1.1 augustss
136 1.1 augustss /* Interrupt handler: some event is available */
137 1.1 augustss int
138 1.1 augustss spic_intr(void *v) {
139 1.1 augustss struct spic_softc *sc = v;
140 1.1 augustss u_int8_t v1, v2;
141 1.1 augustss int dz, buttons;
142 1.1 augustss
143 1.1 augustss v1 = INB(sc, SPIC_PORT1);
144 1.1 augustss v2 = INB(sc, SPIC_PORT2);
145 1.1 augustss
146 1.1 augustss buttons = 0;
147 1.1 augustss if (v1 & 0x40)
148 1.1 augustss buttons |= 1 << 1;
149 1.1 augustss if (v1 & 0x20)
150 1.1 augustss buttons |= 1 << 5;
151 1.1 augustss dz = v1 & 0x1f;
152 1.1 augustss switch (dz) {
153 1.1 augustss case 0:
154 1.1 augustss case 1:
155 1.1 augustss case 2:
156 1.1 augustss case 3:
157 1.1 augustss break;
158 1.1 augustss case 0x1f:
159 1.1 augustss case 0x1e:
160 1.1 augustss case 0x1d:
161 1.1 augustss dz -= 0x20;
162 1.1 augustss break;
163 1.1 augustss default:
164 1.1 augustss printf("spic: v1=0x%02x v2=0x%02x\n", v1, v2);
165 1.1 augustss goto skip;
166 1.1 augustss }
167 1.1 augustss
168 1.1 augustss if (!sc->sc_enabled) {
169 1.1 augustss /*printf("spic: not enabled\n");*/
170 1.1 augustss goto skip;
171 1.1 augustss }
172 1.1 augustss
173 1.1 augustss if (dz != 0 || buttons != sc->sc_buttons) {
174 1.1 augustss #ifdef SPIC_DEBUG
175 1.1 augustss if (spicdebug)
176 1.1 augustss printf("spic: but=0x%x dz=%d v1=0x%02x v2=0x%02x\n",
177 1.1 augustss buttons, dz, v1, v2);
178 1.1 augustss #endif
179 1.1 augustss sc->sc_buttons = buttons;
180 1.1 augustss if (sc->sc_wsmousedev != NULL) {
181 1.1 augustss wsmouse_input(sc->sc_wsmousedev, buttons, 0, 0, dz,
182 1.1 augustss WSMOUSE_INPUT_DELTA);
183 1.1 augustss }
184 1.1 augustss }
185 1.1 augustss
186 1.1 augustss skip:
187 1.1 augustss spic_call2(sc, 0x81, 0xff); /* Clear event */
188 1.1 augustss return (1);
189 1.1 augustss }
190 1.1 augustss
191 1.1 augustss static void
192 1.1 augustss spictimeout(void *v)
193 1.1 augustss {
194 1.1 augustss struct spic_softc *sc = v;
195 1.1 augustss int s = spltty();
196 1.1 augustss spic_intr(v);
197 1.1 augustss splx(s);
198 1.1 augustss callout_reset(&sc->sc_poll, POLLRATE, spictimeout, sc);
199 1.1 augustss }
200 1.1 augustss
201 1.1 augustss void
202 1.1 augustss spic_attach(struct spic_softc *sc)
203 1.1 augustss {
204 1.1 augustss struct wsmousedev_attach_args a;
205 1.1 augustss
206 1.1 augustss #ifdef SPIC_DEBUG
207 1.1 augustss if (spicdebug)
208 1.1 augustss printf("spic_attach %x %x\n", sc->sc_iot, (uint)sc->sc_ioh);
209 1.1 augustss #endif
210 1.1 augustss
211 1.1 augustss callout_init(&sc->sc_poll);
212 1.1 augustss
213 1.1 augustss spic_call1(sc, 0x82);
214 1.1 augustss spic_call2(sc, 0x81, 0xff);
215 1.1 augustss spic_call1(sc, 0x92); /* or 0x82 */
216 1.1 augustss
217 1.1 augustss a.accessops = &spic_accessops;
218 1.1 augustss a.accesscookie = sc;
219 1.1 augustss sc->sc_wsmousedev = config_found(&sc->sc_dev, &a, wsmousedevprint);
220 1.1 augustss }
221 1.1 augustss
222 1.1 augustss
223 1.1 augustss static int
224 1.1 augustss spic_enable(void *v)
225 1.1 augustss {
226 1.1 augustss struct spic_softc *sc = v;
227 1.1 augustss
228 1.1 augustss if (sc->sc_enabled)
229 1.1 augustss return (EBUSY);
230 1.1 augustss
231 1.1 augustss sc->sc_enabled = 1;
232 1.1 augustss sc->sc_buttons = 0;
233 1.1 augustss
234 1.1 augustss #ifdef SPIC_DEBUG
235 1.1 augustss if (spicdebug)
236 1.1 augustss printf("spic_enable\n");
237 1.1 augustss #endif
238 1.1 augustss
239 1.1 augustss callout_reset(&sc->sc_poll, POLLRATE, spictimeout, sc);
240 1.1 augustss
241 1.1 augustss return (0);
242 1.1 augustss }
243 1.1 augustss
244 1.1 augustss static void
245 1.1 augustss spic_disable(void *v)
246 1.1 augustss {
247 1.1 augustss struct spic_softc *sc = v;
248 1.1 augustss
249 1.1 augustss #ifdef DIAGNOSTIC
250 1.1 augustss if (!sc->sc_enabled) {
251 1.1 augustss printf("spic_disable: not enabled\n");
252 1.1 augustss return;
253 1.1 augustss }
254 1.1 augustss #endif
255 1.1 augustss
256 1.1 augustss callout_stop(&sc->sc_poll);
257 1.1 augustss
258 1.1 augustss sc->sc_enabled = 0;
259 1.1 augustss
260 1.1 augustss #ifdef SPIC_DEBUG
261 1.1 augustss if (spicdebug)
262 1.1 augustss printf("spic_disable\n");
263 1.1 augustss #endif
264 1.1 augustss }
265 1.1 augustss
266 1.1 augustss static int
267 1.2 christos spic_ioctl(void *v, u_long cmd, caddr_t data, int flag, struct lwp *l)
268 1.1 augustss {
269 1.1 augustss switch (cmd) {
270 1.1 augustss case WSMOUSEIO_GTYPE:
271 1.1 augustss /* XXX this is not really correct */
272 1.1 augustss *(u_int *)data = WSMOUSE_TYPE_PS2;
273 1.1 augustss return (0);
274 1.1 augustss }
275 1.1 augustss
276 1.1 augustss return (-1);
277 1.1 augustss }
278