1 /* $OpenBSD: joy.c,v 1.17 2023/01/30 10:49:05 jsg Exp $ */
2 /* $NetBSD: joy.c,v 1.3 1996/05/05 19:46:15 christos Exp $ */
3
4 /*-
5 * Copyright (c) 1995 Jean-Marc Zucconi
6 * All rights reserved.
7 *
8 * Ported to NetBSD by Matthieu Herrb <matthieu@laas.fr>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer
15 * in this position and unchanged.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. 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 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/errno.h>
40
41 #include <machine/pio.h>
42 #include <machine/cpufunc.h>
43 #include <machine/joystick.h>
44 #include <machine/conf.h>
45
46 #include <dev/isa/isareg.h>
47 #include <dev/ic/i8253reg.h>
48 #include <i386/isa/joyreg.h>
49
50 static int joy_get_tick(void);
51
52 struct cfdriver joy_cd = {
53 NULL, "joy", DV_DULL
54 };
55
56 int
joyopen(dev_t dev,int flag,int mode,struct proc * p)57 joyopen(dev_t dev, int flag, int mode, struct proc *p)
58 {
59 int unit = JOYUNIT(dev);
60 int i = JOYPART(dev);
61 struct joy_softc *sc;
62
63 if (unit >= joy_cd.cd_ndevs)
64 return (ENXIO);
65
66 sc = joy_cd.cd_devs[unit];
67 if (sc == NULL)
68 return (ENXIO);
69
70 if (sc->timeout[i])
71 return EBUSY;
72
73 sc->x_off[i] = sc->y_off[i] = 0;
74 sc->timeout[i] = JOY_TIMEOUT;
75 return 0;
76 }
77
78 int
joyclose(dev_t dev,int flag,int mode,struct proc * p)79 joyclose(dev_t dev, int flag, int mode, struct proc *p)
80 {
81 int unit = JOYUNIT(dev);
82 int i = JOYPART(dev);
83 struct joy_softc *sc = joy_cd.cd_devs[unit];
84
85 sc->timeout[i] = 0;
86 return 0;
87 }
88
89 int
joyread(dev_t dev,struct uio * uio,int flag)90 joyread(dev_t dev, struct uio *uio, int flag)
91 {
92 int unit = JOYUNIT(dev);
93 struct joy_softc *sc = joy_cd.cd_devs[unit];
94 struct joystick c;
95 int port = sc->port;
96 int i, t0, t1;
97 int state = 0, x = 0, y = 0;
98 u_long s;
99
100 s = intr_disable();
101 outb(port, 0xff);
102 t0 = joy_get_tick();
103 t1 = t0;
104 i = USEC2TICKS(sc->timeout[JOYPART(dev)]);
105 while (t0 - t1 < i) {
106 state = inb(port);
107 if (JOYPART(dev) == 1)
108 state >>= 2;
109 t1 = joy_get_tick();
110 if (t1 > t0)
111 t1 -= TIMER_FREQ / hz;
112 if (!x && !(state & 0x01))
113 x = t1;
114 if (!y && !(state & 0x02))
115 y = t1;
116 if (x && y)
117 break;
118 }
119 intr_restore(s);
120 c.x = x ? sc->x_off[JOYPART(dev)] + TICKS2USEC(t0 - x) : 0x80000000;
121 c.y = y ? sc->y_off[JOYPART(dev)] + TICKS2USEC(t0 - y) : 0x80000000;
122 state >>= 4;
123 c.b1 = ~state & 1;
124 c.b2 = ~(state >> 1) & 1;
125 return uiomove((caddr_t) & c, sizeof(struct joystick), uio);
126 }
127
128 int
joyioctl(dev_t dev,u_long cmd,caddr_t data,int flag,struct proc * p)129 joyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
130 {
131 int unit = JOYUNIT(dev);
132 struct joy_softc *sc = joy_cd.cd_devs[unit];
133 int i = JOYPART(dev);
134 int x;
135
136 switch (cmd) {
137 case JOY_SETTIMEOUT:
138 x = *(int *) data;
139 if (x < 1 || x > 10000) /* 10ms maximum! */
140 return EINVAL;
141 sc->timeout[i] = x;
142 break;
143 case JOY_GETTIMEOUT:
144 *(int *) data = sc->timeout[i];
145 break;
146 case JOY_SET_X_OFFSET:
147 sc->x_off[i] = *(int *) data;
148 break;
149 case JOY_SET_Y_OFFSET:
150 sc->y_off[i] = *(int *) data;
151 break;
152 case JOY_GET_X_OFFSET:
153 *(int *) data = sc->x_off[i];
154 break;
155 case JOY_GET_Y_OFFSET:
156 *(int *) data = sc->y_off[i];
157 break;
158 default:
159 return ENXIO;
160 }
161 return 0;
162 }
163
164 static int
joy_get_tick(void)165 joy_get_tick(void)
166 {
167 int low, high;
168
169 outb(IO_TIMER1 + TIMER_MODE, TIMER_SEL0);
170 low = inb(IO_TIMER1 + TIMER_CNTR0);
171 high = inb(IO_TIMER1 + TIMER_CNTR0);
172
173 return (high << 8) | low;
174 }
175