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