opms.c revision 1.1.4.2 1 /* $NetBSD: opms.c,v 1.1.4.2 2002/01/10 19:37:42 thorpej 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 cdev_decl(opms);
96
97 static __inline void pms_dev_cmd __P((u_char));
98 static __inline void pms_aux_cmd __P((u_char));
99 static __inline void pms_pit_cmd __P((u_char));
100
101 static __inline void
102 pms_dev_cmd(value)
103 u_char value;
104 {
105 kbd_flush_input();
106 kbd_cmd_write_1(0xd4);
107 kbd_flush_input();
108 kbd_data_write_1(value);
109 }
110
111 static __inline void
112 pms_aux_cmd(value)
113 u_char value;
114 {
115 kbd_flush_input();
116 kbd_cmd_write_1(value);
117 }
118
119 static __inline void
120 pms_pit_cmd(value)
121 u_char value;
122 {
123 kbd_flush_input();
124 kbd_cmd_write_1(0x60);
125 kbd_flush_input();
126 kbd_data_write_1(value);
127 }
128
129 int opms_common_match(kbd_iot, config)
130 bus_space_tag_t kbd_iot;
131 struct pccons_config *config;
132 {
133 u_char x;
134
135 kbd_context_init(kbd_iot, config);
136
137 pms_dev_cmd(KBC_RESET);
138 pms_aux_cmd(PMS_MAGIC_1);
139 delay(10000);
140 x = kbd_data_read_1();
141 pms_pit_cmd(PMS_INT_DISABLE);
142 if (x & 0x04)
143 return 0;
144
145 return 1;
146 }
147
148 void
149 opms_common_attach(sc, opms_iot, config)
150 struct opms_softc *sc;
151 bus_space_tag_t opms_iot;
152 struct pccons_config *config;
153 {
154 kbd_context_init(opms_iot, config);
155
156 /* Other initialization was done by opmsprobe. */
157 sc->sc_state = 0;
158 }
159
160 int
161 opmsopen(dev, flag)
162 dev_t dev;
163 int flag;
164 {
165 int unit = PMSUNIT(dev);
166 struct opms_softc *sc;
167
168 if (unit >= opms_cd.cd_ndevs)
169 return ENXIO;
170 sc = opms_cd.cd_devs[unit];
171 if (!sc)
172 return ENXIO;
173
174 if (sc->sc_state & PMS_OPEN)
175 return EBUSY;
176
177 if (clalloc(&sc->sc_q, PMS_BSIZE, 0) == -1)
178 return ENOMEM;
179
180 sc->sc_state |= PMS_OPEN;
181 sc->sc_status = 0;
182 sc->sc_x = sc->sc_y = 0;
183
184 /* Enable interrupts. */
185 pms_dev_cmd(PMS_DEV_ENABLE);
186 pms_aux_cmd(PMS_AUX_ENABLE);
187 pms_dev_cmd(PMS_SET_RES);
188 pms_dev_cmd(3); /* 8 counts/mm */
189 pms_dev_cmd(PMS_SET_SCALE21);
190 #if 0
191 pms_dev_cmd(PMS_SET_SAMPLE);
192 pms_dev_cmd(100); /* 100 samples/sec */
193 pms_dev_cmd(PMS_SET_STREAM);
194 #endif
195 pms_pit_cmd(PMS_INT_ENABLE);
196
197 return 0;
198 }
199
200 int
201 opmsclose(dev, flag)
202 dev_t dev;
203 int flag;
204 {
205 struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
206
207 /* Disable interrupts. */
208 pms_dev_cmd(PMS_DEV_DISABLE);
209 pms_pit_cmd(PMS_INT_DISABLE);
210 pms_aux_cmd(PMS_AUX_DISABLE);
211
212 sc->sc_state &= ~PMS_OPEN;
213
214 clfree(&sc->sc_q);
215
216 return 0;
217 }
218
219 int
220 opmsread(dev, uio, flag)
221 dev_t dev;
222 struct uio *uio;
223 int flag;
224 {
225 struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
226 int s;
227 int error = 0;
228 size_t length;
229 u_char buffer[PMS_CHUNK];
230
231 /* Block until mouse activity occurred. */
232
233 s = spltty();
234 while (sc->sc_q.c_cc == 0) {
235 if (flag & IO_NDELAY) {
236 splx(s);
237 return EWOULDBLOCK;
238 }
239 sc->sc_state |= PMS_ASLP;
240 error = tsleep((caddr_t)sc, PZERO | PCATCH, "pmsrea", 0);
241 if (error) {
242 sc->sc_state &= ~PMS_ASLP;
243 splx(s);
244 return error;
245 }
246 }
247 splx(s);
248
249 /* Transfer as many chunks as possible. */
250
251 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
252 length = min(sc->sc_q.c_cc, uio->uio_resid);
253 if (length > sizeof(buffer))
254 length = sizeof(buffer);
255
256 /* Remove a small chunk from the input queue. */
257 (void) q_to_b(&sc->sc_q, buffer, length);
258
259 /* Copy the data to the user process. */
260 error = uiomove(buffer, length, uio);
261 if (error)
262 break;
263 }
264
265 return error;
266 }
267
268 int
269 opmsioctl(dev, cmd, addr, flag)
270 dev_t dev;
271 u_long cmd;
272 caddr_t addr;
273 int flag;
274 {
275 struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
276 struct mouseinfo info;
277 int s;
278 int error;
279
280 switch (cmd) {
281 case MOUSEIOCREAD:
282 s = spltty();
283
284 info.status = sc->sc_status;
285 if (sc->sc_x || sc->sc_y)
286 info.status |= MOVEMENT;
287
288 if (sc->sc_x > 127)
289 info.xmotion = 127;
290 else if (sc->sc_x < -127)
291 /* Bounding at -127 avoids a bug in XFree86. */
292 info.xmotion = -127;
293 else
294 info.xmotion = sc->sc_x;
295
296 if (sc->sc_y > 127)
297 info.ymotion = 127;
298 else if (sc->sc_y < -127)
299 info.ymotion = -127;
300 else
301 info.ymotion = sc->sc_y;
302
303 /* Reset historical information. */
304 sc->sc_x = sc->sc_y = 0;
305 sc->sc_status &= ~BUTCHNGMASK;
306 ndflush(&sc->sc_q, sc->sc_q.c_cc);
307
308 splx(s);
309 error = copyout(&info, addr, sizeof(struct mouseinfo));
310 break;
311 default:
312 error = EINVAL;
313 break;
314 }
315
316 return error;
317 }
318
319 /* Masks for the first byte of a packet */
320 #define PS2LBUTMASK 0x01
321 #define PS2RBUTMASK 0x02
322 #define PS2MBUTMASK 0x04
323
324 int
325 opmsintr(arg)
326 void *arg;
327 {
328 struct opms_softc *sc = arg;
329 static int state = 0;
330 static u_char buttons;
331 u_char changed;
332 static char dx, dy;
333 u_char buffer[5];
334
335 if ((sc->sc_state & PMS_OPEN) == 0) {
336 /* Interrupts are not expected. Discard the byte. */
337 kbd_flush_input();
338 return 0;
339 }
340
341 switch (state) {
342
343 case 0:
344 buttons = kbd_data_read_1();
345 if ((buttons & 0xc0) == 0)
346 ++state;
347 break;
348
349 case 1:
350 dx = kbd_data_read_1();
351 /* Bounding at -127 avoids a bug in XFree86. */
352 dx = (dx == -128) ? -127 : dx;
353 ++state;
354 break;
355
356 case 2:
357 dy = kbd_data_read_1();
358 dy = (dy == -128) ? -127 : dy;
359 state = 0;
360
361 buttons = ((buttons & PS2LBUTMASK) << 2) |
362 ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
363 changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
364 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) | changed;
365
366 if (dx || dy || changed) {
367 /* Update accumulated movements. */
368 sc->sc_x += dx;
369 sc->sc_y += dy;
370
371 /* Add this event to the queue. */
372 buffer[0] = 0x80 | (buttons & BUTSTATMASK);
373 if(dx < 0)
374 buffer[0] |= 0x10;
375 buffer[1] = dx & 0x7f;
376 if(dy < 0)
377 buffer[0] |= 0x20;
378 buffer[2] = dy & 0x7f;
379 buffer[3] = buffer[4] = 0;
380 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
381
382 if (sc->sc_state & PMS_ASLP) {
383 sc->sc_state &= ~PMS_ASLP;
384 wakeup((caddr_t)sc);
385 }
386 selnotify(&sc->sc_rsel, 0);
387 }
388
389 break;
390 }
391 return -1;
392 }
393
394 int
395 opmspoll(dev, events, p)
396 dev_t dev;
397 int events;
398 struct proc *p;
399 {
400 struct opms_softc *sc = opms_cd.cd_devs[PMSUNIT(dev)];
401 int revents = 0;
402 int s = spltty();
403
404 if (events & (POLLIN | POLLRDNORM)) {
405 if (sc->sc_q.c_cc > 0)
406 revents |= events & (POLLIN | POLLRDNORM);
407 else
408 selrecord(p, &sc->sc_rsel);
409 }
410
411 splx(s);
412 return (revents);
413 }
414
415 static void
416 filt_opmsrdetach(struct knote *kn)
417 {
418 struct opms_softc *sc = (void *) kn->kn_hook;
419 int s;
420
421 s = spltty();
422 SLIST_REMOVE(&sc->sc_rsel.si_klist, kn, knote, kn_selnext);
423 splx(s);
424 }
425
426 static int
427 filt_opmsread(struct knote *kn, long hint)
428 {
429 struct opms_softc *sc = (void *) kn->kn_hook;
430
431 kn->kn_data = sc->sc_q.c_cc;
432 return (kn->kn_data > 0);
433 }
434
435 static const struct filterops opmsread_filtops =
436 { 1, NULL, filt_opmsrdetach, filt_opmsread };
437
438 int
439 opmskqfilter(dev_t dev, struct knote *kn)
440 {
441 struct opms_softc *sc = opms_cd.cd_devs[LMSUNIT(dev)];
442 struct klist *klist;
443 int s;
444
445 switch (kn->kn_filter) {
446 case EVFILT_READ:
447 klist = &sc->sc_rsel.si_klist;
448 kn->kn_fop = &opmsread_filtops;
449 break;
450
451 default:
452 return (1);
453 }
454
455 kn->kn_hook = (void *) sc;
456
457 s = spltty();
458 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
459 splx(s);
460
461 return (0);
462 }
463