qms.c revision 1.5 1 /* $NetBSD: qms.c,v 1.5 2003/07/15 00:24:45 lukem Exp $ */
2
3 /*
4 * Copyright (c) Scott Stevens 1995 All rights reserved
5 * Copyright (c) Melvin Tang-Richardson 1995 All rights reserved
6 * Copyright (c) Mark Brinicombe 1995 All rights reserved
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed for the NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 /*
35 * Quadrature mouse driver
36 */
37
38 #include <sys/cdefs.h>
39 __KERNEL_RCSID(0, "$NetBSD: qms.c,v 1.5 2003/07/15 00:24:45 lukem Exp $");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/conf.h>
44 #include <sys/ioctl.h>
45 #include <sys/tty.h>
46 #include <sys/kernel.h>
47 #include <sys/types.h>
48 #include <sys/device.h>
49 #include <sys/proc.h>
50 #include <sys/time.h>
51 #include <sys/errno.h>
52 #include <dev/cons.h>
53 #include <sys/fcntl.h>
54 #include <sys/signalvar.h>
55 #include <sys/vnode.h>
56 #include <sys/time.h>
57 #include <sys/poll.h>
58
59 #include <machine/bus.h>
60 #include <machine/mouse.h>
61 #include <arm/iomd/qmsvar.h>
62
63 #define MOUSE_IOC_ACK
64
65 #define QMOUSE_BSIZE 12*64
66
67 #ifdef MOUSE_IOC_ACK
68 static void qmsputbuffer __P((struct qms_softc *sc, struct mousebufrec *buf));
69 #endif
70
71 extern struct cfdriver qms_cd;
72
73 dev_type_open(qmsopen);
74 dev_type_close(qmsclose);
75 dev_type_read(qmsread);
76 dev_type_ioctl(qmsioctl);
77 dev_type_poll(qmspoll);
78 dev_type_kqfilter(qmskqfilter);
79
80 const struct cdevsw qms_cdevsw = {
81 qmsopen, qmsclose, qmsread, nowrite, qmsioctl,
82 nostop, notty, qmspoll, nommap, qmskqfilter,
83 };
84
85 /* qms device structure */
86
87 /* Offsets of hardware registers */
88 #define QMS_MOUSEX 0 /* 16 bit X register */
89 #define QMS_MOUSEY 1 /* 16 bit Y register */
90
91 #define QMS_BUTTONS 0 /* mouse buttons register */
92
93 /*
94 * generic attach routine. This does the generic part of the driver
95 * attachment and is called from the bus specific attach routine.
96 */
97
98 void
99 qmsattach(sc)
100 struct qms_softc *sc;
101 {
102 /* Set up origin and multipliers */
103 sc->origx = 0;
104 sc->origy = 0;
105 sc->xmult = 2;
106 sc->ymult = 2;
107
108 /* Set up bounding box */
109 sc->boundx = -4095;
110 sc->boundy = -4095;
111 sc->bounda = 4096;
112 sc->boundb = 4096;
113
114 sc->sc_state = 0;
115
116 /* Set the mouse X & Y registers to a known state */
117 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX, sc->origx);
118 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX, sc->origx);
119 }
120
121 int
122 qmsopen(dev, flag, mode, p)
123 dev_t dev;
124 int flag;
125 int mode;
126 struct proc *p;
127 {
128 struct qms_softc *sc;
129 int unit = minor(dev);
130
131 /* validate the unit and softc */
132 if (unit >= qms_cd.cd_ndevs)
133 return(ENXIO);
134
135 sc = qms_cd.cd_devs[unit];
136
137 if (!sc) return(ENXIO);
138
139 /* check if we are already open */
140 if (sc->sc_state & QMOUSE_OPEN) return(EBUSY);
141
142 /* update softc */
143 sc->sc_proc = p;
144
145 sc->lastx = -1;
146 sc->lasty = -1;
147 sc->lastb = -1;
148
149 /* initialise buffer */
150 if (clalloc(&sc->sc_buffer, QMOUSE_BSIZE, 0) == -1)
151 return(ENOMEM);
152
153 /* set mode and state */
154 sc->sc_mode = MOUSEMODE_ABS;
155 sc->sc_state |= QMOUSE_OPEN;
156
157 /* enable interrupts */
158 sc->sc_intenable(sc, 1);
159
160 return(0);
161 }
162
163
164 int
165 qmsclose(dev, flag, mode, p)
166 dev_t dev;
167 int flag;
168 int mode;
169 struct proc *p;
170 {
171 int unit = minor(dev);
172 struct qms_softc *sc = qms_cd.cd_devs[unit];
173
174 /* disable interrupts */
175 sc->sc_intenable(sc, 0);
176
177 /* clean up */
178 sc->sc_proc = NULL;
179 sc->sc_state = 0;
180
181 clfree(&sc->sc_buffer);
182
183 return(0);
184 }
185
186 int
187 qmsread(dev, uio, flag)
188 dev_t dev;
189 struct uio *uio;
190 int flag;
191 {
192 int unit = minor(dev);
193 struct qms_softc *sc = qms_cd.cd_devs[unit];
194 int error;
195 int s;
196 int length;
197 u_char buffer[128];
198
199 error = 0;
200 s = spltty();
201 while (sc->sc_buffer.c_cc == 0) {
202 if (flag & IO_NDELAY) {
203 (void)splx(s);
204 return(EWOULDBLOCK);
205 }
206 sc->sc_state |= QMOUSE_ASLEEP;
207 if ((error = tsleep((caddr_t)sc, PZERO | PCATCH, "qmsread", 0))) {
208 sc->sc_state &= ~QMOUSE_ASLEEP;
209 (void)splx(s);
210 return(error);
211 }
212 }
213
214 while (sc->sc_buffer.c_cc > 0 && uio->uio_resid > 0) {
215 length = min(sc->sc_buffer.c_cc, uio->uio_resid);
216 if(length>sizeof(buffer))
217 length=sizeof(buffer);
218
219 (void)q_to_b(&sc->sc_buffer, buffer, length);
220
221 if ((error = (uiomove(buffer, length, uio))))
222 break;
223 }
224 (void)splx(s);
225 return(error);
226 }
227
228
229 #define FMT_START \
230 int x = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX) & 0xffff; \
231 int y = bus_space_read_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY) & 0xffff; \
232 int b = bus_space_read_1(sc->sc_iot, sc->sc_butioh, QMS_BUTTONS) & 0x70;\
233 if (x & 0x8000) x |= 0xffff0000; \
234 if (y & 0x8000) y |= 0xffff0000; \
235 x = (x - sc->origx); \
236 y = (y - sc->origy); \
237 if (x < (sc->boundx)) x = sc->boundx; \
238 if (x > (sc->bounda)) x = sc->bounda; \
239 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX, x + sc->origx); \
240 if (y < (sc->boundy)) y = sc->boundy; \
241 if (y > (sc->boundb)) y = sc->boundb; \
242 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY, y + sc->origy); \
243 x = x * sc->xmult; \
244 y = y * sc->ymult;
245
246 #define FMT_END
247
248
249 int
250 qmsioctl(dev, cmd, data, flag, p)
251 dev_t dev;
252 u_long cmd;
253 caddr_t data;
254 int flag;
255 struct proc *p;
256 {
257 struct qms_softc *sc = qms_cd.cd_devs[minor(dev)];
258
259 switch (cmd) {
260 case MOUSEIOC_WRITEX:
261 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX,
262 *(int *)data + sc->origx);
263 return 0;
264 case MOUSEIOC_WRITEY:
265 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY,
266 *(int *)data + sc->origy);
267 return 0;
268 case MOUSEIOC_SETSTATE:
269 {
270 struct mouse_state *co = (void *)data;
271 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX, co->x);
272 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY, co->y);
273 return 0;
274 }
275 case MOUSEIOC_SETBOUNDS:
276 {
277 struct mouse_boundingbox *bo = (void *)data;
278 struct mousebufrec buffer;
279 #ifdef MOUSE_IOC_ACK
280 int s;
281
282 s = spltty();
283 #endif
284
285 sc->boundx = bo->x;
286 sc->boundy = bo->y;
287 sc->bounda = bo->a;
288 sc->boundb = bo->b;
289
290 buffer.status = IOC_ACK;
291 buffer.x = sc->origx;
292 buffer.y = sc->origy;
293 #ifdef MOUSE_IOC_ACK
294 if (sc->sc_buffer.c_cc > 0)
295 printf("%s: setting bounding with non empty buffer (%d)\n",
296 sc->sc_device.dv_xname, sc->sc_buffer.c_cc);
297 qmsputbuffer(sc, &buffer);
298 (void)splx(s);
299 #endif
300 return 0;
301 }
302 case MOUSEIOC_SETMODE:
303 {
304 struct mousebufrec buffer;
305 #ifdef MOUSE_IOC_ACK
306 int s;
307
308 s = spltty();
309 #endif
310 sc->sc_mode = *(int *)data;
311
312 buffer.status = IOC_ACK;
313 buffer.x = sc->origx;
314 buffer.y = sc->origy;
315 #ifdef MOUSE_IOC_ACK
316 if (sc->sc_buffer.c_cc > 0)
317 printf("%s: setting mode with non empty buffer (%d)\n",
318 sc->sc_device.dv_xname, sc->sc_buffer.c_cc);
319 qmsputbuffer(sc, &buffer);
320 (void)splx(s);
321 #endif
322 return 0;
323 }
324 case MOUSEIOC_SETORIGIN:
325 {
326 struct mouse_origin *oo = (void *)data;
327 struct mousebufrec buffer;
328 #ifdef MOUSE_IOC_ACK
329 int s;
330
331 s = spltty();
332 #endif
333 /* Need to fix up! */
334 sc->origx = oo->x;
335 sc->origy = oo->y;
336
337 buffer.status = IOC_ACK;
338 buffer.x = sc->origx;
339 buffer.y = sc->origy;
340 #ifdef MOUSE_IOC_ACK
341 if (sc->sc_buffer.c_cc > 0)
342 printf("%s: setting origin with non empty buffer (%d)\n",
343 sc->sc_device.dv_xname, sc->sc_buffer.c_cc);
344 qmsputbuffer(sc, &buffer);
345 (void)splx(s);
346 #endif
347 return 0;
348 }
349 case MOUSEIOC_GETSTATE:
350 {
351 struct mouse_state *co = (void *)data;
352 FMT_START
353 co->x = x;
354 co->y = y;
355 co->buttons = b ^ 0x70;
356 FMT_END
357 return 0;
358 }
359 case MOUSEIOC_GETBOUNDS:
360 {
361 struct mouse_boundingbox *bo = (void *)data;
362 bo->x = sc->boundx;
363 bo->y = sc->boundy;
364 bo->a = sc->bounda;
365 bo->b = sc->boundb;
366 return 0;
367 }
368 case MOUSEIOC_GETORIGIN:
369 {
370 struct mouse_origin *oo = (void *)data;
371 oo->x = sc->origx;
372 oo->y = sc->origy;
373 return 0;
374 }
375 }
376
377 return (EINVAL);
378 }
379
380
381 int
382 qmsintr(arg)
383 void *arg;
384 {
385 struct qms_softc *sc = arg;
386 int s;
387 struct mousebufrec buffer;
388 int dosignal=0;
389
390 FMT_START
391
392 b &= 0x70;
393 b >>= 4;
394 if (x != sc->lastx || y != sc->lasty || b != sc->lastb) {
395 /* Mouse state changed */
396 buffer.status = b | ( b ^ sc->lastb) << 3 | (((x==sc->lastx) && (y==sc->lasty))?0:MOVEMENT);
397 if(sc->sc_mode == MOUSEMODE_REL) {
398 sc->origx = sc->origy = 0;
399 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEX, sc->origx);
400 bus_space_write_4(sc->sc_iot, sc->sc_ioh, QMS_MOUSEY, sc->origy);
401 }
402 buffer.x = x;
403 buffer.y = y;
404 microtime(&buffer.event_time);
405
406 if (sc->sc_buffer.c_cc == 0)
407 dosignal = 1;
408
409 s = spltty();
410 (void)b_to_q((char *)&buffer, sizeof(buffer), &sc->sc_buffer);
411 (void)splx(s);
412 selwakeup(&sc->sc_rsel);
413
414 if (sc->sc_state & QMOUSE_ASLEEP) {
415 sc->sc_state &= ~QMOUSE_ASLEEP;
416 wakeup((caddr_t)sc);
417 }
418
419 /* if (dosignal)*/
420 psignal(sc->sc_proc, SIGIO);
421
422 sc->lastx = x;
423 sc->lasty = y;
424 sc->lastb = b;
425 }
426
427 FMT_END
428 return(0); /* Pass interrupt on down the chain */
429 }
430
431
432 int
433 qmspoll(dev, events, p)
434 dev_t dev;
435 int events;
436 struct proc *p;
437 {
438 struct qms_softc *sc = qms_cd.cd_devs[minor(dev)];
439 int revents = 0;
440 int s = spltty();
441
442 if (events & (POLLIN | POLLRDNORM)) {
443 if (sc->sc_buffer.c_cc > 0)
444 revents |= events & (POLLIN | POLLRDNORM);
445 else
446 selrecord(p, &sc->sc_rsel);
447 }
448
449 (void)splx(s);
450 return (revents);
451 }
452
453
454 #ifdef MOUSE_IOC_ACK
455 static void
456 qmsputbuffer(sc, buffer)
457 struct qms_softc *sc;
458 struct mousebufrec *buffer;
459 {
460 int s;
461 int dosignal = 0;
462
463 /* Time stamp the buffer */
464 microtime(&buffer->event_time);
465
466 if (sc->sc_buffer.c_cc == 0)
467 dosignal=1;
468
469 s = spltty();
470 (void)b_to_q((char *)buffer, sizeof(*buffer), &sc->sc_buffer);
471 (void)splx(s);
472 selwakeup(&sc->sc_rsel);
473
474 if (sc->sc_state & QMOUSE_ASLEEP) {
475 sc->sc_state &= ~QMOUSE_ASLEEP;
476 wakeup((caddr_t)sc);
477 }
478
479 if (dosignal)
480 psignal(sc->sc_proc, SIGIO);
481 }
482 #endif
483
484 /* XXXLUKEM (jdolecek) kqueue hooks not tested */
485 static void
486 filt_qmsrdetach(struct knote *kn)
487 {
488 struct qms_softc *sc = kn->kn_hook;
489 int s;
490
491 s = spltty();
492 SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
493 splx(s);
494 }
495
496 static int
497 filt_qmsread(struct knote *kn, long hint)
498 {
499 struct qms_softc *sc = kn->kn_hook;
500
501 kn->kn_data = sc->sc_buffer.c_cc;
502 return (kn->kn_data > 0);
503 }
504
505 static const struct filterops qmsread_filtops =
506 { 1, NULL, filt_qmsrdetach, filt_qmsread };
507
508 int
509 qmskqfilter(dev_t dev, struct knote *kn)
510 {
511 struct qms_softc *sc = qms_cd.cd_devs[minor(dev)];
512 struct klist *klist;
513 int s;
514
515 switch (kn->kn_filter) {
516 case EVFILT_READ:
517 klist = &sc->sc_rsel.sel_klist;
518 kn->kn_fop = &qmsread_filtops;
519 break;
520
521 default:
522 return (1);
523 }
524
525 kn->kn_hook = sc;
526
527 s = spltty();
528 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
529 splx(s);
530
531 return (0);
532 }
533
534 /* End of qms.c */
535