opms.c revision 1.21.8.1 1 /* $NetBSD: opms.c,v 1.21.8.1 2016/07/19 06:26:58 pgoyette 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) 1990 The Regents of the University of California.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * William Jolitz and Don Ahn.
11 *
12 * Copyright (c) 1994 Charles M. Hannum.
13 * Copyright (c) 1992, 1993 Erik Forsberg.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * @(#)pccons.c 5.11 (Berkeley) 5/21/91
40 */
41
42 /*-
43 * Copyright (c) 1993, 1994, 1995 Charles M. Hannum. All rights reserved.
44 *
45 * This code is derived from software contributed to Berkeley by
46 * William Jolitz and Don Ahn.
47 *
48 * Copyright (c) 1994 Charles M. Hannum.
49 * Copyright (c) 1992, 1993 Erik Forsberg.
50 *
51 * Redistribution and use in source and binary forms, with or without
52 * modification, are permitted provided that the following conditions
53 * are met:
54 * 1. Redistributions of source code must retain the above copyright
55 * notice, this list of conditions and the following disclaimer.
56 * 2. Redistributions in binary form must reproduce the above copyright
57 * notice, this list of conditions and the following disclaimer in the
58 * documentation and/or other materials provided with the distribution.
59 * 3. All advertising materials mentioning features or use of this software
60 * must display the following acknowledgement:
61 * This product includes software developed by the University of
62 * California, Berkeley and its contributors.
63 * 4. Neither the name of the University nor the names of its contributors
64 * may be used to endorse or promote products derived from this software
65 * without specific prior written permission.
66 *
67 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
68 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
69 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
70 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
71 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
72 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
73 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
74 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
76 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
77 * SUCH DAMAGE.
78 *
79 * @(#)pccons.c 5.11 (Berkeley) 5/21/91
80 */
81
82 #include <sys/cdefs.h>
83 __KERNEL_RCSID(0, "$NetBSD: opms.c,v 1.21.8.1 2016/07/19 06:26:58 pgoyette Exp $");
84
85 #include <sys/param.h>
86 #include <sys/systm.h>
87 #include <sys/vnode.h>
88 #include <sys/poll.h>
89 #include <sys/tty.h>
90 #include <sys/device.h>
91 #include <sys/proc.h>
92 #include <sys/conf.h>
93
94 #include <sys/bus.h>
95 #include <machine/kbdreg.h>
96 #include <machine/mouse.h>
97
98 #include <arc/dev/pcconsvar.h>
99 #include <arc/dev/opmsvar.h>
100
101 #include "ioconf.h"
102
103 #define PMSUNIT(dev) (minor(dev))
104
105 /* status bits */
106 #define PMS_OBUF_FULL 0x01
107 #define PMS_IBUF_FULL 0x02
108
109 /* controller commands */
110 #define PMS_INT_ENABLE 0x47 /* enable controller interrupts */
111 #define PMS_INT_DISABLE 0x65 /* disable controller interrupts */
112 #define PMS_AUX_ENABLE 0xa7 /* enable auxiliary port */
113 #define PMS_AUX_DISABLE 0xa8 /* disable auxiliary port */
114 #define PMS_MAGIC_1 0xa9 /* XXX */
115
116 #define PMS_8042_CMD 0x65
117
118 /* mouse commands */
119 #define PMS_SET_SCALE11 0xe6 /* set scaling 1:1 */
120 #define PMS_SET_SCALE21 0xe7 /* set scaling 2:1 */
121 #define PMS_SET_RES 0xe8 /* set resolution */
122 #define PMS_GET_SCALE 0xe9 /* get scaling factor */
123 #define PMS_SET_STREAM 0xea /* set streaming mode */
124 #define PMS_SET_SAMPLE 0xf3 /* set sampling rate */
125 #define PMS_DEV_ENABLE 0xf4 /* mouse on */
126 #define PMS_DEV_DISABLE 0xf5 /* mouse off */
127 #define PMS_RESET 0xff /* reset */
128
129 #define PMS_CHUNK 128 /* chunk size for read */
130 #define PMS_BSIZE 1020 /* buffer size */
131
132 #define FLUSHQ(q) { if((q)->c_cc) ndflush(q, (q)->c_cc); }
133
134 dev_type_open(opmsopen);
135 dev_type_close(opmsclose);
136 dev_type_read(opmsread);
137 dev_type_ioctl(opmsioctl);
138 dev_type_poll(opmspoll);
139 dev_type_kqfilter(opmskqfilter);
140
141 const struct cdevsw opms_cdevsw = {
142 .d_open = opmsopen,
143 .d_close = opmsclose,
144 .d_read = opmsread,
145 .d_write = nowrite,
146 .d_ioctl = opmsioctl,
147 .d_stop = nostop,
148 .d_tty = notty,
149 .d_poll = opmspoll,
150 .d_mmap = nommap,
151 .d_kqfilter = opmskqfilter,
152 .d_discard = nodiscard,
153 .d_flag = 0
154 };
155
156 static inline void pms_dev_cmd(uint8_t);
157 static inline void pms_aux_cmd(uint8_t);
158 static inline void pms_pit_cmd(uint8_t);
159
160 static inline void
161 pms_dev_cmd(uint8_t value)
162 {
163
164 kbd_flush_input();
165 kbd_cmd_write_1(0xd4);
166 kbd_flush_input();
167 kbd_data_write_1(value);
168 }
169
170 static inline void
171 pms_aux_cmd(uint8_t value)
172 {
173
174 kbd_flush_input();
175 kbd_cmd_write_1(value);
176 }
177
178 static inline void
179 pms_pit_cmd(uint8_t value)
180 {
181
182 kbd_flush_input();
183 kbd_cmd_write_1(0x60);
184 kbd_flush_input();
185 kbd_data_write_1(value);
186 }
187
188 int opms_common_match(bus_space_tag_t kbd_iot, struct pccons_config *config)
189 {
190 uint8_t x;
191
192 kbd_context_init(kbd_iot, config);
193
194 pms_dev_cmd(KBC_RESET);
195 pms_aux_cmd(PMS_MAGIC_1);
196 delay(10000);
197 x = kbd_data_read_1();
198 pms_pit_cmd(PMS_INT_DISABLE);
199 if (x & 0x04)
200 return 0;
201
202 return 1;
203 }
204
205 void
206 opms_common_attach(struct opms_softc *sc, bus_space_tag_t opms_iot,
207 struct pccons_config *config)
208 {
209
210 kbd_context_init(opms_iot, config);
211 selinit(&sc->sc_rsel);
212
213 /* Other initialization was done by opmsprobe. */
214 sc->sc_state = 0;
215 }
216
217 int
218 opmsopen(dev_t dev, int flag, int mode, struct lwp *l)
219 {
220 device_t self;
221 struct opms_softc *sc;
222
223 self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
224 if (self == NULL)
225 return EXNIO;
226 sc = device_private(self);
227 if (!sc) {
228 device_release(self);
229 return ENXIO;
230 }
231
232 if (sc->sc_state & PMS_OPEN) {
233 device_release(self);
234 return EBUSY;
235 }
236
237 if (clalloc(&sc->sc_q, PMS_BSIZE, 0) == -1) {
238 device_release(self);
239 return ENOMEM;
240 }
241
242 sc->sc_state |= PMS_OPEN;
243 sc->sc_status = 0;
244 sc->sc_x = sc->sc_y = 0;
245
246 /* Enable interrupts. */
247 pms_dev_cmd(PMS_DEV_ENABLE);
248 pms_aux_cmd(PMS_AUX_ENABLE);
249 pms_dev_cmd(PMS_SET_RES);
250 pms_dev_cmd(3); /* 8 counts/mm */
251 pms_dev_cmd(PMS_SET_SCALE21);
252 #if 0
253 pms_dev_cmd(PMS_SET_SAMPLE);
254 pms_dev_cmd(100); /* 100 samples/sec */
255 pms_dev_cmd(PMS_SET_STREAM);
256 #endif
257 pms_pit_cmd(PMS_INT_ENABLE);
258
259 device_release(self);
260 return 0;
261 }
262
263 int
264 opmsclose(dev_t dev, int flag, int mode, struct lwp *l)
265 {
266 device_t self;
267 struct opms_softc *sc;
268
269 self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
270 if (self == NULL)
271 return ENXIO;
272 sc = device_private(self);
273 if (sc == NULL) {
274 device_release(self);
275 return ENXIO;
276 }
277
278 /* Disable interrupts. */
279 pms_dev_cmd(PMS_DEV_DISABLE);
280 pms_pit_cmd(PMS_INT_DISABLE);
281 pms_aux_cmd(PMS_AUX_DISABLE);
282
283 sc->sc_state &= ~PMS_OPEN;
284
285 clfree(&sc->sc_q);
286
287 device_release(self);
288 return 0;
289 }
290
291 int
292 opmsread(dev_t dev, struct uio *uio, int flag)
293 {
294 device_t self;
295 struct opms_softc *sc;
296 int s;
297 int error = 0;
298 size_t length;
299 u_char buffer[PMS_CHUNK];
300
301 self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
302 if (self == NULL)
303 return ENXIO;
304 sc = device_private(self);
305 if (sc == NULL) {
306 device_release(self);
307 return ENXIO;
308 }
309
310 /* Block until mouse activity occurred. */
311
312 s = spltty();
313 while (sc->sc_q.c_cc == 0) {
314 if (flag & IO_NDELAY) {
315 splx(s);
316 device_release(self);
317 return EWOULDBLOCK;
318 }
319 sc->sc_state |= PMS_ASLP;
320 error = tsleep((void *)sc, PZERO | PCATCH, "pmsrea", 0);
321 if (error) {
322 sc->sc_state &= ~PMS_ASLP;
323 splx(s);
324 device_release(self);
325 return error;
326 }
327 }
328 splx(s);
329
330 /* Transfer as many chunks as possible. */
331
332 while (sc->sc_q.c_cc > 0 && uio->uio_resid > 0) {
333 length = min(sc->sc_q.c_cc, uio->uio_resid);
334 if (length > sizeof(buffer))
335 length = sizeof(buffer);
336
337 /* Remove a small chunk from the input queue. */
338 (void) q_to_b(&sc->sc_q, buffer, length);
339
340 /* Copy the data to the user process. */
341 error = uiomove(buffer, length, uio);
342 if (error)
343 break;
344 }
345
346 device_release(self);
347 return error;
348 }
349
350 int
351 opmsioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
352 {
353 device_t self;
354 struct opms_softc *sc;
355 struct mouseinfo info;
356 int s;
357 int error;
358
359 self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
360 if (self == NULL)
361 return ENXIO;
362 sc = device_private(self);
363 if (sc == NULL) {
364 device_release(self);
365 return ENXIO;
366 }
367 switch (cmd) {
368 case MOUSEIOCREAD:
369 s = spltty();
370
371 info.status = sc->sc_status;
372 if (sc->sc_x || sc->sc_y)
373 info.status |= MOVEMENT;
374
375 if (sc->sc_x > 127)
376 info.xmotion = 127;
377 else if (sc->sc_x < -127)
378 /* Bounding at -127 avoids a bug in XFree86. */
379 info.xmotion = -127;
380 else
381 info.xmotion = sc->sc_x;
382
383 if (sc->sc_y > 127)
384 info.ymotion = 127;
385 else if (sc->sc_y < -127)
386 info.ymotion = -127;
387 else
388 info.ymotion = sc->sc_y;
389
390 /* Reset historical information. */
391 sc->sc_x = sc->sc_y = 0;
392 sc->sc_status &= ~BUTCHNGMASK;
393 ndflush(&sc->sc_q, sc->sc_q.c_cc);
394
395 splx(s);
396 error = copyout(&info, addr, sizeof(struct mouseinfo));
397 break;
398 default:
399 error = EINVAL;
400 break;
401 }
402
403 device_release(self);
404 return error;
405 }
406
407 /* Masks for the first byte of a packet */
408 #define PS2LBUTMASK 0x01
409 #define PS2RBUTMASK 0x02
410 #define PS2MBUTMASK 0x04
411
412 int
413 opmsintr(void *arg)
414 {
415 struct opms_softc *sc = arg;
416 static int state = 0;
417 static u_char buttons;
418 u_char changed;
419 static char dx, dy;
420 u_char buffer[5];
421
422 if ((sc->sc_state & PMS_OPEN) == 0) {
423 /* Interrupts are not expected. Discard the byte. */
424 kbd_flush_input();
425 return 0;
426 }
427
428 switch (state) {
429
430 case 0:
431 buttons = kbd_data_read_1();
432 if ((buttons & 0xc0) == 0)
433 ++state;
434 break;
435
436 case 1:
437 dx = kbd_data_read_1();
438 /* Bounding at -127 avoids a bug in XFree86. */
439 dx = (dx == -128) ? -127 : dx;
440 ++state;
441 break;
442
443 case 2:
444 dy = kbd_data_read_1();
445 dy = (dy == -128) ? -127 : dy;
446 state = 0;
447
448 buttons = ((buttons & PS2LBUTMASK) << 2) |
449 ((buttons & (PS2RBUTMASK | PS2MBUTMASK)) >> 1);
450 changed = ((buttons ^ sc->sc_status) & BUTSTATMASK) << 3;
451 sc->sc_status = buttons | (sc->sc_status & ~BUTSTATMASK) |
452 changed;
453
454 if (dx || dy || changed) {
455 /* Update accumulated movements. */
456 sc->sc_x += dx;
457 sc->sc_y += dy;
458
459 /* Add this event to the queue. */
460 buffer[0] = 0x80 | (buttons & BUTSTATMASK);
461 if(dx < 0)
462 buffer[0] |= 0x10;
463 buffer[1] = dx & 0x7f;
464 if(dy < 0)
465 buffer[0] |= 0x20;
466 buffer[2] = dy & 0x7f;
467 buffer[3] = buffer[4] = 0;
468 (void) b_to_q(buffer, sizeof buffer, &sc->sc_q);
469
470 if (sc->sc_state & PMS_ASLP) {
471 sc->sc_state &= ~PMS_ASLP;
472 wakeup((void *)sc);
473 }
474 selnotify(&sc->sc_rsel, 0, 0);
475 }
476
477 break;
478 }
479 return -1;
480 }
481
482 int
483 opmspoll(dev_t dev, int events, struct lwp *l)
484 {
485 device_t self;
486 struct opms_softc *sc;
487 int revents = 0;
488 int s = spltty();
489
490 self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
491 if (self == NULL)
492 return 0;
493 sc = device_private(self);
494 if (sc == NULL) {
495 device_release(self);
496 return 0;
497 }
498
499 if (events & (POLLIN | POLLRDNORM)) {
500 if (sc->sc_q.c_cc > 0)
501 revents |= events & (POLLIN | POLLRDNORM);
502 else
503 selrecord(l, &sc->sc_rsel);
504 }
505
506 splx(s);
507 device_release(self);
508 return revents;
509 }
510
511 static void
512 filt_opmsrdetach(struct knote *kn)
513 {
514 struct opms_softc *sc = kn->kn_hook;
515 int s;
516
517 s = spltty();
518 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
519 splx(s);
520 }
521
522 static int
523 filt_opmsread(struct knote *kn, long hint)
524 {
525 struct opms_softc *sc = kn->kn_hook;
526
527 kn->kn_data = sc->sc_q.c_cc;
528 return kn->kn_data > 0;
529 }
530
531 static const struct filterops opmsread_filtops =
532 { 1, NULL, filt_opmsrdetach, filt_opmsread };
533
534 int
535 opmskqfilter(dev_t dev, struct knote *kn)
536 {
537 device_t self;
538 struct opms_softc *sc;
539 struct klist *klist;
540 int s;
541
542 self = device_lookup_acquire(&opms_cd, PMSUNIT(dev));
543 if (self == NULL)
544 return 1;
545 sc = device_private(self);
546 if (sc == NULL) {
547 device_release(self);
548 return 1;
549 }
550
551 switch (kn->kn_filter) {
552 case EVFILT_READ:
553 klist = &sc->sc_rsel.sel_klist;
554 kn->kn_fop = &opmsread_filtops;
555 break;
556
557 default:
558 device_release(self);
559 return 1;
560 }
561
562 kn->kn_hook = sc;
563
564 s = spltty();
565 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
566 splx(s);
567
568 device_release(self);
569 return 0;
570 }
571