aed.c revision 1.5 1 1.5 thorpej /* $NetBSD: aed.c,v 1.5 2000/03/23 06:40:33 thorpej Exp $ */
2 1.1 tsubai
3 1.1 tsubai /*
4 1.1 tsubai * Copyright (C) 1994 Bradley A. Grantham
5 1.1 tsubai * All rights reserved.
6 1.1 tsubai *
7 1.1 tsubai * Redistribution and use in source and binary forms, with or without
8 1.1 tsubai * modification, are permitted provided that the following conditions
9 1.1 tsubai * are met:
10 1.1 tsubai * 1. Redistributions of source code must retain the above copyright
11 1.1 tsubai * notice, this list of conditions and the following disclaimer.
12 1.1 tsubai * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tsubai * notice, this list of conditions and the following disclaimer in the
14 1.1 tsubai * documentation and/or other materials provided with the distribution.
15 1.1 tsubai * 3. All advertising materials mentioning features or use of this software
16 1.1 tsubai * must display the following acknowledgement:
17 1.1 tsubai * This product includes software developed by Bradley A. Grantham.
18 1.1 tsubai * 4. The name of the author may not be used to endorse or promote products
19 1.1 tsubai * derived from this software without specific prior written permission.
20 1.1 tsubai *
21 1.1 tsubai * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 tsubai * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 tsubai * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 tsubai * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 tsubai * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 tsubai * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 tsubai * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 tsubai * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 tsubai * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 1.1 tsubai * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 tsubai */
32 1.1 tsubai
33 1.1 tsubai #include <sys/param.h>
34 1.1 tsubai #include <sys/device.h>
35 1.1 tsubai #include <sys/fcntl.h>
36 1.1 tsubai #include <sys/poll.h>
37 1.1 tsubai #include <sys/select.h>
38 1.1 tsubai #include <sys/proc.h>
39 1.1 tsubai #include <sys/signalvar.h>
40 1.1 tsubai #include <sys/systm.h>
41 1.1 tsubai
42 1.1 tsubai #include <machine/autoconf.h>
43 1.1 tsubai #include <machine/cpu.h>
44 1.1 tsubai #include <machine/keyboard.h>
45 1.1 tsubai
46 1.1 tsubai #include <macppc/dev/adbvar.h>
47 1.1 tsubai #include <macppc/dev/aedvar.h>
48 1.1 tsubai #include <macppc/dev/akbdvar.h>
49 1.1 tsubai
50 1.1 tsubai #define spladb splhigh
51 1.1 tsubai
52 1.1 tsubai /*
53 1.1 tsubai * Function declarations.
54 1.1 tsubai */
55 1.1 tsubai static int aedmatch __P((struct device *, struct cfdata *, void *));
56 1.1 tsubai static void aedattach __P((struct device *, struct device *, void *));
57 1.1 tsubai static void aed_emulate_mouse __P((adb_event_t *event));
58 1.1 tsubai static void aed_kbdrpt __P((void *kstate));
59 1.1 tsubai static void aed_dokeyupdown __P((adb_event_t *event));
60 1.1 tsubai static void aed_handoff __P((adb_event_t *event));
61 1.1 tsubai static void aed_enqevent __P((adb_event_t *event));
62 1.1 tsubai
63 1.1 tsubai /*
64 1.1 tsubai * Global variables.
65 1.1 tsubai */
66 1.1 tsubai extern int adb_polling; /* Are we polling? (Debugger mode) */
67 1.1 tsubai
68 1.1 tsubai /*
69 1.1 tsubai * Local variables.
70 1.1 tsubai */
71 1.4 tsubai static struct aed_softc *aed_sc = NULL;
72 1.3 tsubai static int aed_options = 0; /* | AED_MSEMUL; */
73 1.1 tsubai
74 1.1 tsubai /* Driver definition */
75 1.1 tsubai struct cfattach aed_ca = {
76 1.1 tsubai sizeof(struct aed_softc), aedmatch, aedattach
77 1.1 tsubai };
78 1.1 tsubai
79 1.1 tsubai extern struct cfdriver aed_cd;
80 1.1 tsubai
81 1.1 tsubai static int
82 1.1 tsubai aedmatch(parent, cf, aux)
83 1.1 tsubai struct device *parent;
84 1.1 tsubai struct cfdata *cf;
85 1.1 tsubai void *aux;
86 1.1 tsubai {
87 1.4 tsubai struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
88 1.1 tsubai static int aed_matched = 0;
89 1.1 tsubai
90 1.1 tsubai /* Allow only one instance. */
91 1.1 tsubai if ((aa_args->origaddr == 0) && (!aed_matched)) {
92 1.1 tsubai aed_matched = 1;
93 1.1 tsubai return (1);
94 1.1 tsubai } else
95 1.1 tsubai return (0);
96 1.1 tsubai }
97 1.1 tsubai
98 1.1 tsubai static void
99 1.1 tsubai aedattach(parent, self, aux)
100 1.1 tsubai struct device *parent, *self;
101 1.1 tsubai void *aux;
102 1.1 tsubai {
103 1.4 tsubai struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
104 1.1 tsubai struct aed_softc *sc = (struct aed_softc *)self;
105 1.1 tsubai
106 1.5 thorpej callout_init(&sc->sc_repeat_ch);
107 1.5 thorpej
108 1.1 tsubai sc->origaddr = aa_args->origaddr;
109 1.1 tsubai sc->adbaddr = aa_args->adbaddr;
110 1.1 tsubai sc->handler_id = aa_args->handler_id;
111 1.1 tsubai
112 1.1 tsubai sc->sc_evq_tail = 0;
113 1.1 tsubai sc->sc_evq_len = 0;
114 1.1 tsubai
115 1.1 tsubai sc->sc_rptdelay = 20;
116 1.1 tsubai sc->sc_rptinterval = 6;
117 1.1 tsubai sc->sc_repeating = -1; /* not repeating */
118 1.1 tsubai
119 1.1 tsubai /* Pull in the options flags. */
120 1.1 tsubai sc->sc_options = (sc->sc_dev.dv_cfdata->cf_flags | aed_options);
121 1.1 tsubai
122 1.1 tsubai sc->sc_ioproc = NULL;
123 1.1 tsubai
124 1.1 tsubai sc->sc_buttons = 0;
125 1.1 tsubai
126 1.1 tsubai sc->sc_open = 0;
127 1.1 tsubai
128 1.1 tsubai aed_sc = sc;
129 1.1 tsubai
130 1.1 tsubai printf("ADB Event device\n");
131 1.1 tsubai
132 1.1 tsubai return;
133 1.1 tsubai }
134 1.1 tsubai
135 1.1 tsubai /*
136 1.1 tsubai * Given a keyboard ADB event, record the keycode and call the key
137 1.1 tsubai * repeat handler, optionally passing the event through the mouse
138 1.1 tsubai * button emulation handler first. Pass mouse events directly to
139 1.1 tsubai * the handoff function.
140 1.1 tsubai */
141 1.1 tsubai void
142 1.1 tsubai aed_input(event)
143 1.1 tsubai adb_event_t *event;
144 1.1 tsubai {
145 1.1 tsubai adb_event_t new_event = *event;
146 1.1 tsubai
147 1.1 tsubai switch (event->def_addr) {
148 1.1 tsubai case ADBADDR_KBD:
149 1.1 tsubai if (aed_sc->sc_options & AED_MSEMUL)
150 1.1 tsubai aed_emulate_mouse(&new_event);
151 1.1 tsubai else
152 1.1 tsubai aed_dokeyupdown(&new_event);
153 1.1 tsubai break;
154 1.1 tsubai case ADBADDR_MS:
155 1.4 tsubai new_event.u.m.buttons |= aed_sc->sc_buttons;
156 1.1 tsubai aed_handoff(&new_event);
157 1.1 tsubai break;
158 1.1 tsubai default: /* God only knows. */
159 1.1 tsubai #ifdef DIAGNOSTIC
160 1.1 tsubai panic("aed: received event from unsupported device!\n");
161 1.1 tsubai #endif
162 1.1 tsubai break;
163 1.1 tsubai }
164 1.1 tsubai
165 1.1 tsubai }
166 1.1 tsubai
167 1.1 tsubai /*
168 1.1 tsubai * Handles mouse button emulation via the keyboard. If the emulation
169 1.1 tsubai * modifier key is down, left and right arrows will generate 2nd and
170 1.1 tsubai * 3rd mouse button events while the 1, 2, and 3 keys will generate
171 1.1 tsubai * the corresponding mouse button event.
172 1.1 tsubai */
173 1.1 tsubai static void
174 1.1 tsubai aed_emulate_mouse(event)
175 1.1 tsubai adb_event_t *event;
176 1.1 tsubai {
177 1.1 tsubai static int emulmodkey_down = 0;
178 1.1 tsubai adb_event_t new_event;
179 1.1 tsubai
180 1.1 tsubai if (event->u.k.key == ADBK_KEYDOWN(ADBK_OPTION)) {
181 1.1 tsubai emulmodkey_down = 1;
182 1.1 tsubai } else if (event->u.k.key == ADBK_KEYUP(ADBK_OPTION)) {
183 1.1 tsubai /* key up */
184 1.1 tsubai emulmodkey_down = 0;
185 1.1 tsubai if (aed_sc->sc_buttons & 0xfe) {
186 1.1 tsubai aed_sc->sc_buttons &= 1;
187 1.1 tsubai new_event.def_addr = ADBADDR_MS;
188 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
189 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
190 1.1 tsubai microtime(&new_event.timestamp);
191 1.1 tsubai aed_handoff(&new_event);
192 1.1 tsubai }
193 1.1 tsubai } else if (emulmodkey_down) {
194 1.1 tsubai switch(event->u.k.key) {
195 1.1 tsubai #ifdef ALTXBUTTONS
196 1.1 tsubai case ADBK_KEYDOWN(ADBK_1):
197 1.1 tsubai aed_sc->sc_buttons |= 1; /* left down */
198 1.1 tsubai new_event.def_addr = ADBADDR_MS;
199 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
200 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
201 1.1 tsubai microtime(&new_event.timestamp);
202 1.1 tsubai aed_handoff(&new_event);
203 1.1 tsubai break;
204 1.1 tsubai case ADBK_KEYUP(ADBK_1):
205 1.4 tsubai aed_sc->sc_buttons &= ~1; /* left up */
206 1.1 tsubai new_event.def_addr = ADBADDR_MS;
207 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
208 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
209 1.1 tsubai microtime(&new_event.timestamp);
210 1.1 tsubai aed_handoff(&new_event);
211 1.1 tsubai break;
212 1.1 tsubai #endif
213 1.1 tsubai case ADBK_KEYDOWN(ADBK_LEFT):
214 1.1 tsubai #ifdef ALTXBUTTONS
215 1.1 tsubai case ADBK_KEYDOWN(ADBK_2):
216 1.1 tsubai #endif
217 1.1 tsubai aed_sc->sc_buttons |= 2; /* middle down */
218 1.1 tsubai new_event.def_addr = ADBADDR_MS;
219 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
220 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
221 1.1 tsubai microtime(&new_event.timestamp);
222 1.1 tsubai aed_handoff(&new_event);
223 1.1 tsubai break;
224 1.1 tsubai case ADBK_KEYUP(ADBK_LEFT):
225 1.1 tsubai #ifdef ALTXBUTTONS
226 1.1 tsubai case ADBK_KEYUP(ADBK_2):
227 1.1 tsubai #endif
228 1.1 tsubai aed_sc->sc_buttons &= ~2; /* middle up */
229 1.1 tsubai new_event.def_addr = ADBADDR_MS;
230 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
231 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
232 1.1 tsubai microtime(&new_event.timestamp);
233 1.1 tsubai aed_handoff(&new_event);
234 1.1 tsubai break;
235 1.1 tsubai case ADBK_KEYDOWN(ADBK_RIGHT):
236 1.1 tsubai #ifdef ALTXBUTTONS
237 1.1 tsubai case ADBK_KEYDOWN(ADBK_3):
238 1.1 tsubai #endif
239 1.1 tsubai aed_sc->sc_buttons |= 4; /* right down */
240 1.1 tsubai new_event.def_addr = ADBADDR_MS;
241 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
242 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
243 1.1 tsubai microtime(&new_event.timestamp);
244 1.1 tsubai aed_handoff(&new_event);
245 1.1 tsubai break;
246 1.1 tsubai case ADBK_KEYUP(ADBK_RIGHT):
247 1.4 tsubai #ifdef ALTXBUTTONS
248 1.1 tsubai case ADBK_KEYUP(ADBK_3):
249 1.4 tsubai #endif
250 1.1 tsubai aed_sc->sc_buttons &= ~4; /* right up */
251 1.1 tsubai new_event.def_addr = ADBADDR_MS;
252 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
253 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
254 1.1 tsubai microtime(&new_event.timestamp);
255 1.1 tsubai aed_handoff(&new_event);
256 1.1 tsubai break;
257 1.4 tsubai case ADBK_KEYUP(ADBK_SHIFT):
258 1.4 tsubai case ADBK_KEYDOWN(ADBK_SHIFT):
259 1.4 tsubai case ADBK_KEYUP(ADBK_CONTROL):
260 1.4 tsubai case ADBK_KEYDOWN(ADBK_CONTROL):
261 1.4 tsubai case ADBK_KEYUP(ADBK_FLOWER):
262 1.4 tsubai case ADBK_KEYDOWN(ADBK_FLOWER):
263 1.1 tsubai /* ctrl, shift, cmd */
264 1.1 tsubai aed_dokeyupdown(event);
265 1.1 tsubai break;
266 1.1 tsubai default:
267 1.1 tsubai if (event->u.k.key & 0x80)
268 1.1 tsubai /* ignore keyup */
269 1.1 tsubai break;
270 1.1 tsubai
271 1.1 tsubai /* key down */
272 1.1 tsubai new_event = *event;
273 1.1 tsubai
274 1.1 tsubai /* send option-down */
275 1.1 tsubai new_event.u.k.key = ADBK_KEYDOWN(ADBK_OPTION);
276 1.1 tsubai new_event.bytes[0] = new_event.u.k.key;
277 1.1 tsubai microtime(&new_event.timestamp);
278 1.1 tsubai aed_dokeyupdown(&new_event);
279 1.1 tsubai
280 1.1 tsubai /* send key-down */
281 1.1 tsubai new_event.u.k.key = event->bytes[0];
282 1.1 tsubai new_event.bytes[0] = new_event.u.k.key;
283 1.1 tsubai microtime(&new_event.timestamp);
284 1.1 tsubai aed_dokeyupdown(&new_event);
285 1.1 tsubai
286 1.1 tsubai /* send key-up */
287 1.1 tsubai new_event.u.k.key =
288 1.1 tsubai ADBK_KEYUP(ADBK_KEYVAL(event->bytes[0]));
289 1.1 tsubai microtime(&new_event.timestamp);
290 1.1 tsubai new_event.bytes[0] = new_event.u.k.key;
291 1.1 tsubai aed_dokeyupdown(&new_event);
292 1.1 tsubai
293 1.1 tsubai /* send option-up */
294 1.1 tsubai new_event.u.k.key = ADBK_KEYUP(ADBK_OPTION);
295 1.1 tsubai new_event.bytes[0] = new_event.u.k.key;
296 1.1 tsubai microtime(&new_event.timestamp);
297 1.1 tsubai aed_dokeyupdown(&new_event);
298 1.1 tsubai break;
299 1.1 tsubai }
300 1.1 tsubai } else {
301 1.1 tsubai aed_dokeyupdown(event);
302 1.1 tsubai }
303 1.1 tsubai }
304 1.1 tsubai
305 1.1 tsubai /*
306 1.1 tsubai * Keyboard autorepeat timeout function. Sends key up/down events
307 1.1 tsubai * for the repeating key and schedules the next call at sc_rptinterval
308 1.1 tsubai * ticks in the future.
309 1.1 tsubai */
310 1.1 tsubai static void
311 1.1 tsubai aed_kbdrpt(kstate)
312 1.1 tsubai void *kstate;
313 1.1 tsubai {
314 1.4 tsubai struct aed_softc *aed_sc = (struct aed_softc *)kstate;
315 1.1 tsubai
316 1.1 tsubai aed_sc->sc_rptevent.bytes[0] |= 0x80;
317 1.1 tsubai microtime(&aed_sc->sc_rptevent.timestamp);
318 1.1 tsubai aed_handoff(&aed_sc->sc_rptevent); /* do key up */
319 1.1 tsubai
320 1.1 tsubai aed_sc->sc_rptevent.bytes[0] &= 0x7f;
321 1.1 tsubai microtime(&aed_sc->sc_rptevent.timestamp);
322 1.1 tsubai aed_handoff(&aed_sc->sc_rptevent); /* do key down */
323 1.1 tsubai
324 1.1 tsubai if (aed_sc->sc_repeating == aed_sc->sc_rptevent.u.k.key) {
325 1.5 thorpej callout_reset(&aed_sc->sc_repeat_ch, aed_sc->sc_rptinterval,
326 1.5 thorpej aed_kbdrpt, kstate);
327 1.1 tsubai }
328 1.1 tsubai }
329 1.1 tsubai
330 1.1 tsubai
331 1.1 tsubai /*
332 1.1 tsubai * Cancels the currently repeating key event if there is one, schedules
333 1.1 tsubai * a new repeating key event if needed, and hands the event off to the
334 1.1 tsubai * appropriate subsystem.
335 1.1 tsubai */
336 1.1 tsubai static void
337 1.1 tsubai aed_dokeyupdown(event)
338 1.1 tsubai adb_event_t *event;
339 1.1 tsubai {
340 1.1 tsubai int kbd_key;
341 1.1 tsubai
342 1.1 tsubai kbd_key = ADBK_KEYVAL(event->u.k.key);
343 1.1 tsubai if (ADBK_PRESS(event->u.k.key) && keyboard[kbd_key][0] != 0) {
344 1.1 tsubai /* ignore shift & control */
345 1.1 tsubai if (aed_sc->sc_repeating != -1) {
346 1.5 thorpej callout_stop(&aed_sc->sc_repeat_ch);
347 1.1 tsubai }
348 1.1 tsubai aed_sc->sc_rptevent = *event;
349 1.1 tsubai aed_sc->sc_repeating = kbd_key;
350 1.5 thorpej callout_reset(&aed_sc->sc_repeat_ch, aed_sc->sc_rptdelay,
351 1.5 thorpej aed_kbdrpt, (void *)aed_sc);
352 1.1 tsubai } else {
353 1.1 tsubai if (aed_sc->sc_repeating != -1) {
354 1.1 tsubai aed_sc->sc_repeating = -1;
355 1.5 thorpej callout_stop(&aed_sc->sc_repeat_ch);
356 1.1 tsubai }
357 1.1 tsubai aed_sc->sc_rptevent = *event;
358 1.1 tsubai }
359 1.1 tsubai aed_handoff(event);
360 1.1 tsubai }
361 1.1 tsubai
362 1.1 tsubai /*
363 1.1 tsubai * Place the event in the event queue if a requesting device is open
364 1.3 tsubai * and we are not polling.
365 1.1 tsubai */
366 1.1 tsubai static void
367 1.1 tsubai aed_handoff(event)
368 1.1 tsubai adb_event_t *event;
369 1.1 tsubai {
370 1.1 tsubai if (aed_sc->sc_open && !adb_polling)
371 1.1 tsubai aed_enqevent(event);
372 1.1 tsubai }
373 1.1 tsubai
374 1.1 tsubai /*
375 1.1 tsubai * Place the event in the event queue and wakeup any waiting processes.
376 1.1 tsubai */
377 1.1 tsubai static void
378 1.1 tsubai aed_enqevent(event)
379 1.1 tsubai adb_event_t *event;
380 1.1 tsubai {
381 1.1 tsubai int s;
382 1.1 tsubai
383 1.1 tsubai s = spladb();
384 1.1 tsubai
385 1.1 tsubai #ifdef DIAGNOSTIC
386 1.1 tsubai if (aed_sc->sc_evq_tail < 0 || aed_sc->sc_evq_tail >= AED_MAX_EVENTS)
387 1.1 tsubai panic("adb: event queue tail is out of bounds");
388 1.1 tsubai
389 1.1 tsubai if (aed_sc->sc_evq_len < 0 || aed_sc->sc_evq_len > AED_MAX_EVENTS)
390 1.1 tsubai panic("adb: event queue len is out of bounds");
391 1.1 tsubai #endif
392 1.1 tsubai
393 1.1 tsubai if (aed_sc->sc_evq_len == AED_MAX_EVENTS) {
394 1.1 tsubai splx(s);
395 1.1 tsubai return; /* Oh, well... */
396 1.1 tsubai }
397 1.1 tsubai aed_sc->sc_evq[(aed_sc->sc_evq_len + aed_sc->sc_evq_tail) %
398 1.1 tsubai AED_MAX_EVENTS] = *event;
399 1.1 tsubai aed_sc->sc_evq_len++;
400 1.1 tsubai
401 1.1 tsubai selwakeup(&aed_sc->sc_selinfo);
402 1.1 tsubai if (aed_sc->sc_ioproc)
403 1.1 tsubai psignal(aed_sc->sc_ioproc, SIGIO);
404 1.1 tsubai
405 1.1 tsubai splx(s);
406 1.1 tsubai }
407 1.1 tsubai
408 1.1 tsubai int
409 1.1 tsubai aedopen(dev, flag, mode, p)
410 1.1 tsubai dev_t dev;
411 1.1 tsubai int flag, mode;
412 1.1 tsubai struct proc *p;
413 1.1 tsubai {
414 1.1 tsubai int unit;
415 1.1 tsubai int error = 0;
416 1.1 tsubai int s;
417 1.1 tsubai
418 1.1 tsubai unit = minor(dev);
419 1.1 tsubai
420 1.1 tsubai if (unit != 0)
421 1.1 tsubai return (ENXIO);
422 1.1 tsubai
423 1.1 tsubai s = spladb();
424 1.1 tsubai if (aed_sc->sc_open) {
425 1.1 tsubai splx(s);
426 1.1 tsubai return (EBUSY);
427 1.1 tsubai }
428 1.1 tsubai aed_sc->sc_evq_tail = 0;
429 1.1 tsubai aed_sc->sc_evq_len = 0;
430 1.1 tsubai aed_sc->sc_open = 1;
431 1.1 tsubai aed_sc->sc_ioproc = p;
432 1.1 tsubai splx(s);
433 1.1 tsubai
434 1.1 tsubai return (error);
435 1.1 tsubai }
436 1.1 tsubai
437 1.1 tsubai
438 1.1 tsubai int
439 1.1 tsubai aedclose(dev, flag, mode, p)
440 1.1 tsubai dev_t dev;
441 1.1 tsubai int flag, mode;
442 1.1 tsubai struct proc *p;
443 1.1 tsubai {
444 1.1 tsubai int s = spladb();
445 1.1 tsubai
446 1.1 tsubai aed_sc->sc_open = 0;
447 1.1 tsubai aed_sc->sc_ioproc = NULL;
448 1.1 tsubai splx(s);
449 1.1 tsubai
450 1.1 tsubai return (0);
451 1.1 tsubai }
452 1.1 tsubai
453 1.1 tsubai
454 1.1 tsubai int
455 1.1 tsubai aedread(dev, uio, flag)
456 1.1 tsubai dev_t dev;
457 1.1 tsubai struct uio *uio;
458 1.1 tsubai int flag;
459 1.1 tsubai {
460 1.1 tsubai int s, error;
461 1.1 tsubai int willfit;
462 1.1 tsubai int total;
463 1.1 tsubai int firstmove;
464 1.1 tsubai int moremove;
465 1.1 tsubai
466 1.1 tsubai if (uio->uio_resid < sizeof(adb_event_t))
467 1.1 tsubai return (EMSGSIZE); /* close enough. */
468 1.1 tsubai
469 1.1 tsubai s = spladb();
470 1.1 tsubai if (aed_sc->sc_evq_len == 0) {
471 1.1 tsubai splx(s);
472 1.1 tsubai return (0);
473 1.1 tsubai }
474 1.1 tsubai willfit = howmany(uio->uio_resid, sizeof(adb_event_t));
475 1.1 tsubai total = (aed_sc->sc_evq_len < willfit) ? aed_sc->sc_evq_len : willfit;
476 1.1 tsubai
477 1.1 tsubai firstmove = (aed_sc->sc_evq_tail + total > AED_MAX_EVENTS)
478 1.1 tsubai ? (AED_MAX_EVENTS - aed_sc->sc_evq_tail) : total;
479 1.1 tsubai
480 1.1 tsubai error = uiomove((caddr_t) & aed_sc->sc_evq[aed_sc->sc_evq_tail],
481 1.1 tsubai firstmove * sizeof(adb_event_t), uio);
482 1.1 tsubai if (error) {
483 1.1 tsubai splx(s);
484 1.1 tsubai return (error);
485 1.1 tsubai }
486 1.1 tsubai moremove = total - firstmove;
487 1.1 tsubai
488 1.1 tsubai if (moremove > 0) {
489 1.1 tsubai error = uiomove((caddr_t) & aed_sc->sc_evq[0],
490 1.1 tsubai moremove * sizeof(adb_event_t), uio);
491 1.1 tsubai if (error) {
492 1.1 tsubai splx(s);
493 1.1 tsubai return (error);
494 1.1 tsubai }
495 1.1 tsubai }
496 1.1 tsubai aed_sc->sc_evq_tail = (aed_sc->sc_evq_tail + total) % AED_MAX_EVENTS;
497 1.1 tsubai aed_sc->sc_evq_len -= total;
498 1.1 tsubai splx(s);
499 1.1 tsubai return (0);
500 1.1 tsubai }
501 1.1 tsubai
502 1.1 tsubai
503 1.1 tsubai int
504 1.1 tsubai aedwrite(dev, uio, flag)
505 1.1 tsubai dev_t dev;
506 1.1 tsubai struct uio *uio;
507 1.1 tsubai int flag;
508 1.1 tsubai {
509 1.1 tsubai return 0;
510 1.1 tsubai }
511 1.1 tsubai
512 1.1 tsubai
513 1.1 tsubai int
514 1.1 tsubai aedioctl(dev, cmd, data, flag, p)
515 1.1 tsubai dev_t dev;
516 1.1 tsubai int cmd;
517 1.1 tsubai caddr_t data;
518 1.1 tsubai int flag;
519 1.1 tsubai struct proc *p;
520 1.1 tsubai {
521 1.1 tsubai switch (cmd) {
522 1.1 tsubai case ADBIOCDEVSINFO: {
523 1.1 tsubai adb_devinfo_t *di;
524 1.1 tsubai ADBDataBlock adbdata;
525 1.1 tsubai int totaldevs;
526 1.1 tsubai int adbaddr;
527 1.1 tsubai int i;
528 1.1 tsubai
529 1.1 tsubai di = (void *)data;
530 1.1 tsubai
531 1.1 tsubai /* Initialize to no devices */
532 1.1 tsubai for (i = 0; i < 16; i++)
533 1.1 tsubai di->dev[i].addr = -1;
534 1.1 tsubai
535 1.1 tsubai totaldevs = CountADBs();
536 1.1 tsubai for (i = 1; i <= totaldevs; i++) {
537 1.1 tsubai adbaddr = GetIndADB(&adbdata, i);
538 1.1 tsubai di->dev[adbaddr].addr = adbaddr;
539 1.4 tsubai di->dev[adbaddr].default_addr = (int)(adbdata.origADBAddr);
540 1.4 tsubai di->dev[adbaddr].handler_id = (int)(adbdata.devType);
541 1.1 tsubai }
542 1.1 tsubai
543 1.1 tsubai /* Must call ADB Manager to get devices now */
544 1.1 tsubai break;
545 1.1 tsubai }
546 1.1 tsubai
547 1.1 tsubai case ADBIOCGETREPEAT:{
548 1.1 tsubai adb_rptinfo_t *ri;
549 1.1 tsubai
550 1.1 tsubai ri = (void *)data;
551 1.1 tsubai ri->delay_ticks = aed_sc->sc_rptdelay;
552 1.1 tsubai ri->interval_ticks = aed_sc->sc_rptinterval;
553 1.1 tsubai break;
554 1.1 tsubai }
555 1.1 tsubai
556 1.1 tsubai case ADBIOCSETREPEAT:{
557 1.1 tsubai adb_rptinfo_t *ri;
558 1.1 tsubai
559 1.1 tsubai ri = (void *) data;
560 1.1 tsubai aed_sc->sc_rptdelay = ri->delay_ticks;
561 1.1 tsubai aed_sc->sc_rptinterval = ri->interval_ticks;
562 1.1 tsubai break;
563 1.1 tsubai }
564 1.1 tsubai
565 1.1 tsubai case ADBIOCRESET:
566 1.1 tsubai /* Do nothing for now */
567 1.1 tsubai break;
568 1.1 tsubai
569 1.1 tsubai case ADBIOCLISTENCMD:{
570 1.1 tsubai adb_listencmd_t *lc;
571 1.1 tsubai
572 1.1 tsubai lc = (void *)data;
573 1.1 tsubai }
574 1.1 tsubai
575 1.1 tsubai default:
576 1.1 tsubai return (EINVAL);
577 1.1 tsubai }
578 1.1 tsubai return (0);
579 1.1 tsubai }
580 1.1 tsubai
581 1.1 tsubai
582 1.1 tsubai int
583 1.1 tsubai aedpoll(dev, events, p)
584 1.1 tsubai dev_t dev;
585 1.1 tsubai int events;
586 1.1 tsubai struct proc *p;
587 1.1 tsubai {
588 1.1 tsubai int s, revents;
589 1.1 tsubai
590 1.1 tsubai revents = events & (POLLOUT | POLLWRNORM);
591 1.1 tsubai
592 1.1 tsubai if ((events & (POLLIN | POLLRDNORM)) == 0)
593 1.1 tsubai return (revents);
594 1.1 tsubai
595 1.1 tsubai s = spladb();
596 1.1 tsubai if (aed_sc->sc_evq_len > 0)
597 1.1 tsubai revents |= events & (POLLIN | POLLRDNORM);
598 1.1 tsubai else
599 1.1 tsubai selrecord(p, &aed_sc->sc_selinfo);
600 1.1 tsubai splx(s);
601 1.1 tsubai
602 1.1 tsubai return (revents);
603 1.1 tsubai }
604