opms.c revision 1.3 1 /* $NetBSD: opms.c,v 1.3 2001/09/28 11:59:51 chs Exp $ */
2 /* $OpenBSD: pccons.c,v 1.22 1999/01/30 22:39:37 imp Exp $ */
3 /* NetBSD: pms.c,v 1.21 1995/04/18 02:25:18 mycroft Exp */
4
5 /*-
6 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
7 * Copyright (c) 1990 The Regents of the University of California.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * William Jolitz and Don Ahn.
12 *
13 * Copyright (c) 1994 Charles M. Hannum.
14 * Copyright (c) 1992, 1993 Erik Forsberg.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)pccons.c 5.11 (Berkeley) 5/21/91
45 */
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/vnode.h>
50 #include <sys/poll.h>
51 #include <sys/tty.h>
52 #include <sys/device.h>
53 #include <sys/proc.h>
54
55 #include <machine/bus.h>
56 #include <machine/kbdreg.h>
57 #include <machine/mouse.h>
58
59 #include <arc/dev/pcconsvar.h>
60 #include <arc/dev/opmsvar.h>
61
62 #define PMSUNIT(dev) (minor(dev))
63
64 /* status bits */
65 #define PMS_OBUF_FULL 0x01
66 #define PMS_IBUF_FULL 0x02
67
68 /* controller commands */
69 #define PMS_INT_ENABLE 0x47 /* enable controller interrupts */
70 #define PMS_INT_DISABLE 0x65 /* disable controller interrupts */
71 #define PMS_AUX_ENABLE 0xa7 /* enable auxiliary port */
72 #define PMS_AUX_DISABLE 0xa8 /* disable auxiliary port */
73 #define PMS_MAGIC_1 0xa9 /* XXX */
74
75 #define PMS_8042_CMD 0x65
76
77 /* mouse commands */
78 #define PMS_SET_SCALE11 0xe6 /* set scaling 1:1 */
79 #define PMS_SET_SCALE21 0xe7 /* set scaling 2:1 */
80 #define PMS_SET_RES 0xe8 /* set resolution */
81 #define PMS_GET_SCALE 0xe9 /* get scaling factor */
82 #define PMS_SET_STREAM 0xea /* set streaming mode */
83 #define PMS_SET_SAMPLE 0xf3 /* set sampling rate */
84 #define PMS_DEV_ENABLE 0xf4 /* mouse on */
85 #define PMS_DEV_DISABLE 0xf5 /* mouse off */
86 #define PMS_RESET 0xff /* reset */
87
88 #define PMS_CHUNK 128 /* chunk size for read */
89 #define PMS_BSIZE 1020 /* buffer size */
90
91 #define FLUSHQ(q) { if((q)->c_cc) ndflush(q, (q)->c_cc); }
92
93 extern struct cfdriver opms_cd;
94
95 int opmsopen __P((dev_t, int));
96 int opmsclose __P((dev_t, int));
97 int opmsread __P((dev_t, struct uio *, int));
98 int opmsioctl __P((dev_t, u_long, caddr_t, int));
99 int opmsselect __P((dev_t, int, struct proc *));
100 int opmspoll __P((dev_t, int, struct proc *));
101 static __inline void pms_dev_cmd __P((u_char));
102 static __inline void pms_aux_cmd __P((u_char));
103 static __inline void pms_pit_cmd __P((u_char));
104
105 static __inline void
106 pms_dev_cmd(value)
107 u_char value;
108 {
109 kbd_flush_input();
110 kbd_cmd_write_1(0xd4);
111 kbd_flush_input();
112 kbd_data_write_1(value);
113 }
114
115 static __inline void
116 pms_aux_cmd(value)
117 u_char value;
118 {
119 kbd_flush_input();
120 kbd_cmd_write_1(value);
121 }
122
123 static __inline void
124 pms_pit_cmd(value)
125 u_char value;
126 {
127 kbd_flush_input();
128 kbd_cmd_write_1(0x60);
129 kbd_flush_input();
130 kbd_data_write_1(value);
131 }
132
133 int opms_common_match(kbd_iot, config)
134 bus_space_tag_t kbd_iot;
135 struct pccons_config *config;
136 {
137 u_char x;
138
139 kbd_context_init(kbd_iot, config);
140
141 pms_dev_cmd(KBC_RESET);
142 pms_aux_cmd(PMS_MAGIC_1);
143 delay(10000);
144 x = kbd_data_read_1();
145 pms_pit_cmd(PMS_INT_DISABLE);
146 if (x & 0x04)
147 return 0;
148
149 return 1;
150 }
151
152 void
153 opms_common_attach(sc, opms_iot, config)
154 struct opms_softc *sc;
155 bus_space_tag_t opms_iot;
156 struct pccons_config *config;
157 {
158 kbd_context_init(opms_iot, config);
159
160 /* Other initialization was done by opmsprobe. */
161 sc->sc_state = 0;
162 }
163
164 int
165 opmsopen(dev, flag)
166 dev_t dev;
167 int flag;
168 {
169 int unit = PMSUNIT(dev);
170 struct opms_softc *sc;
171
172 if (unit >= opms_cd.cd_ndevs)
173 return ENXIO;
174 sc = opms_cd.cd_devs[unit];
175 if (!sc)
176 return ENXIO;
177
178 if (sc->sc_state & PMS_OPEN)
179 return EBUSY;
180
181 if (clalloc(&sc->sc_q, PMS_BSIZE, 0) == -1)
182 return ENOMEM;
183
184 sc->sc_state |= PMS_OPEN;
185 sc->sc_status = 0;
186 sc->sc_x = sc->sc_y = 0;
187
188 /* Enable interrupts. */
189 pms_dev_cmd(PMS_DEV_ENABLE);
190 pms_aux_cmd(PMS_AUX_ENABLE);
191 pms_dev_cmd(PMS_SET_RES);
192 pms_dev_cmd(3); /* 8 counts/mm */
193 pms_dev_cmd(PMS_SET_SCALE21);
194 #if 0
195 pms_dev_cmd(PMS_SET_SAMPLE);
196 pms_dev_cmd(100); /* 100 samples/sec */
197 pms_dev_cmd(PMS_SET_STREAM);
198 #endif
199 pms_pit_cmd(PMS_INT_ENABLE);
200
201 return 0;
202 }
203
204 int
205 opmsclose(dev, flag)
206 dev_t dev;
207 int flag;
208 {
209 struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
210
211 /* Disable interrupts. */
212 pms_dev_cmd(PMS_DEV_DISABLE);
213 pms_pit_cmd(PMS_INT_DISABLE);
214 pms_aux_cmd(PMS_AUX_DISABLE);
215
216 sc->sc_state &= ~PMS_OPEN;
217
218 clfree(&sc->sc_q);
219
220 return 0;
221 }
222
223 int
224 opmsread(dev, uio, flag)
225 dev_t dev;
226 struct uio *uio;
227 int flag;
228 {
229 struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
230 int s;
231 int error = 0;
232 size_t length;
233 u_char buffer[PMS_CHUNK];
234
235 /* Block until mouse activity occurred. */
236
237 s = spltty();
238 while (sc->sc_q.c_cc == 0) {
239 if (flag & IO_NDELAY) {
240 splx(s);
241 return EWOULDBLOCK;
242 }
243 sc->sc_state |= PMS_ASLP;
244 error = tsleep((caddr_t)sc, PZERO | PCATCH, "pmsrea", 0);
245 if (error) {
246 sc->sc_state &= ~PMS_ASLP;
247 splx(s);
248 return error;
249 }
250 }
251 splx(s);
252
253 /* Transfer as many chunks as possible. */
254
255 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
256 length = min(sc->sc_q.c_cc, uio->uio_resid);
257 if (length > sizeof(buffer))
258 length = sizeof(buffer);
259
260 /* Remove a small chunk from the input queue. */
261 (void) q_to_b(&sc->sc_q, buffer, length);
262
263 /* Copy the data to the user process. */
264 error = uiomove(buffer, length, uio);
265 if (error)
266 break;
267 }
268
269 return error;
270 }
271
272 int
273 opmsioctl(dev, cmd, addr, flag)
274 dev_t dev;
275 u_long cmd;
276 caddr_t addr;
277 int flag;
278 {
279 struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
280 struct mouseinfo info;
281 int s;
282 int error;
283
284 switch (cmd) {
285 case MOUSEIOCREAD:
286 s = spltty();
287
288 info.status = sc->sc_status;
289 if (sc->sc_x || sc->sc_y)
290 info.status |= MOVEMENT;
291
292 if (sc->sc_x > 127)
293 info.xmotion = 127;
294 else if (sc->sc_x < -127)
295 /* Bounding at -127 avoids a bug in XFree86. */
296 info.xmotion = -127;
297 else
298 info.xmotion = sc->sc_x;
299
300 if (sc->sc_y > 127)
301 info.ymotion = 127;
302 else if (sc->sc_y < -127)
303 info.ymotion = -127;
304 else
305 info.ymotion = sc->sc_y;
306
307 /* Reset historical information. */
308 sc->sc_x = sc->sc_y = 0;
309 sc->sc_status &= ~BUTCHNGMASK;
310 ndflush(&sc->sc_q, sc->sc_q.c_cc);
311
312 splx(s);
313 error = copyout(&info, addr, sizeof(struct mouseinfo));
314 break;
315 default:
316 error = EINVAL;
317 break;
318 }
319
320 return error;
321 }
322
323 /* Masks for the first byte of a packet */
324 #define PS2LBUTMASK 0x01
325 #define PS2RBUTMASK 0x02
326 #define PS2MBUTMASK 0x04
327
328 int
329 opmsintr(arg)
330 void *arg;
331 {
332 struct opms_softc *sc = arg;
333 static int state = 0;
334 static u_char buttons;
335 u_char changed;
336 static char dx, dy;
337 u_char buffer[5];
338
339 if ((sc->sc_state & PMS_OPEN) == 0) {
340 /* Interrupts are not expected. Discard the byte. */
341 kbd_flush_input();
342 return 0;
343 }
344
345 switch (state) {
346
347 case 0:
348 buttons = kbd_data_read_1();
349 if ((buttons & 0xc0) == 0)
350 ++state;
351 break;
352
353 case 1:
354 dx = kbd_data_read_1();
355 /* Bounding at -127 avoids a bug in XFree86. */
356 dx = (dx == -128) ? -127 : dx;
357 ++state;
358 break;
359
360 case 2:
361 dy = kbd_data_read_1();
362 dy = (dy == -128) ? -127 : dy;
363 state = 0;
364
365 buttons = ((buttons & PS2LBUTMASK) << 2) |
366 ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
367 changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
368 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
369
370 if (dx || dy || changed) {
371 /* Update accumulated movements. */
372 sc->sc_x += dx;
373 sc->sc_y += dy;
374
375 /* Add this event to the queue. */
376 buffer[0] = 0x80 | (buttons & BUTSTATMASK);
377 if(dx < 0)
378 buffer[0] |= 0x10;
379 buffer[1] = dx & 0x7f;
380 if(dy < 0)
381 buffer[0] |= 0x20;
382 buffer[2] = dy & 0x7f;
383 buffer[3] = buffer[4] = 0;
384 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
385
386 if (sc->sc_state & PMS_ASLP) {
387 sc->sc_state &= ~PMS_ASLP;
388 wakeup((caddr_t)sc);
389 }
390 selwakeup(&sc->sc_rsel);
391 }
392
393 break;
394 }
395 return -1;
396 }
397
398 int
399 opmspoll(dev, events, p)
400 dev_t dev;
401 int events;
402 struct proc *p;
403 {
404 struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
405 int revents = 0;
406 int s = spltty();
407
408 if (events & (POLLIN | POLLRDNORM)) {
409 if (sc->sc_q.c_cc > 0)
410 revents |= events & (POLLIN | POLLRDNORM);
411 else
412 selrecord(p, &sc->sc_rsel);
413 }
414
415 splx(s);
416 return (revents);
417 }
418