aed.c revision 1.24 1 1.24 dsl /* $NetBSD: aed.c,v 1.24 2009/03/14 21:04:11 dsl 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.14 lukem
33 1.14 lukem #include <sys/cdefs.h>
34 1.24 dsl __KERNEL_RCSID(0, "$NetBSD: aed.c,v 1.24 2009/03/14 21:04:11 dsl Exp $");
35 1.1 tsubai
36 1.1 tsubai #include <sys/param.h>
37 1.1 tsubai #include <sys/device.h>
38 1.1 tsubai #include <sys/fcntl.h>
39 1.1 tsubai #include <sys/poll.h>
40 1.1 tsubai #include <sys/select.h>
41 1.1 tsubai #include <sys/proc.h>
42 1.1 tsubai #include <sys/signalvar.h>
43 1.1 tsubai #include <sys/systm.h>
44 1.6 gehenna #include <sys/conf.h>
45 1.1 tsubai
46 1.1 tsubai #include <machine/autoconf.h>
47 1.1 tsubai #include <machine/cpu.h>
48 1.1 tsubai #include <machine/keyboard.h>
49 1.1 tsubai
50 1.1 tsubai #include <macppc/dev/adbvar.h>
51 1.1 tsubai #include <macppc/dev/aedvar.h>
52 1.1 tsubai #include <macppc/dev/akbdvar.h>
53 1.1 tsubai
54 1.1 tsubai #define spladb splhigh
55 1.1 tsubai
56 1.1 tsubai /*
57 1.1 tsubai * Function declarations.
58 1.1 tsubai */
59 1.22 dsl static int aedmatch(struct device *, struct cfdata *, void *);
60 1.22 dsl static void aedattach(struct device *, struct device *, void *);
61 1.22 dsl static void aed_emulate_mouse(adb_event_t *event);
62 1.22 dsl static void aed_kbdrpt(void *kstate);
63 1.22 dsl static void aed_dokeyupdown(adb_event_t *event);
64 1.22 dsl static void aed_handoff(adb_event_t *event);
65 1.22 dsl static void aed_enqevent(adb_event_t *event);
66 1.1 tsubai
67 1.1 tsubai /*
68 1.1 tsubai * Global variables.
69 1.1 tsubai */
70 1.1 tsubai extern int adb_polling; /* Are we polling? (Debugger mode) */
71 1.1 tsubai
72 1.1 tsubai /*
73 1.1 tsubai * Local variables.
74 1.1 tsubai */
75 1.4 tsubai static struct aed_softc *aed_sc = NULL;
76 1.3 tsubai static int aed_options = 0; /* | AED_MSEMUL; */
77 1.1 tsubai
78 1.1 tsubai /* Driver definition */
79 1.9 thorpej CFATTACH_DECL(aed, sizeof(struct aed_softc),
80 1.9 thorpej aedmatch, aedattach, NULL, NULL);
81 1.1 tsubai
82 1.1 tsubai extern struct cfdriver aed_cd;
83 1.1 tsubai
84 1.6 gehenna dev_type_open(aedopen);
85 1.6 gehenna dev_type_close(aedclose);
86 1.6 gehenna dev_type_read(aedread);
87 1.6 gehenna dev_type_ioctl(aedioctl);
88 1.6 gehenna dev_type_poll(aedpoll);
89 1.10 jdolecek dev_type_kqfilter(aedkqfilter);
90 1.6 gehenna
91 1.6 gehenna const struct cdevsw aed_cdevsw = {
92 1.6 gehenna aedopen, aedclose, aedread, nullwrite, aedioctl,
93 1.10 jdolecek nostop, notty, aedpoll, nommap, aedkqfilter,
94 1.6 gehenna };
95 1.6 gehenna
96 1.1 tsubai static int
97 1.23 dsl aedmatch(struct device *parent, struct cfdata *cf, void *aux)
98 1.1 tsubai {
99 1.4 tsubai struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
100 1.1 tsubai static int aed_matched = 0;
101 1.1 tsubai
102 1.1 tsubai /* Allow only one instance. */
103 1.1 tsubai if ((aa_args->origaddr == 0) && (!aed_matched)) {
104 1.1 tsubai aed_matched = 1;
105 1.1 tsubai return (1);
106 1.1 tsubai } else
107 1.1 tsubai return (0);
108 1.1 tsubai }
109 1.1 tsubai
110 1.1 tsubai static void
111 1.24 dsl aedattach(struct device *parent, struct device *self, void *aux)
112 1.1 tsubai {
113 1.4 tsubai struct adb_attach_args *aa_args = (struct adb_attach_args *)aux;
114 1.1 tsubai struct aed_softc *sc = (struct aed_softc *)self;
115 1.1 tsubai
116 1.19 ad callout_init(&sc->sc_repeat_ch, 0);
117 1.21 rmind selinit(&sc->sc_selinfo);
118 1.5 thorpej
119 1.1 tsubai sc->origaddr = aa_args->origaddr;
120 1.1 tsubai sc->adbaddr = aa_args->adbaddr;
121 1.1 tsubai sc->handler_id = aa_args->handler_id;
122 1.1 tsubai
123 1.1 tsubai sc->sc_evq_tail = 0;
124 1.1 tsubai sc->sc_evq_len = 0;
125 1.1 tsubai
126 1.1 tsubai sc->sc_rptdelay = 20;
127 1.1 tsubai sc->sc_rptinterval = 6;
128 1.1 tsubai sc->sc_repeating = -1; /* not repeating */
129 1.1 tsubai
130 1.1 tsubai /* Pull in the options flags. */
131 1.17 thorpej sc->sc_options = (device_cfdata(&sc->sc_dev)->cf_flags | aed_options);
132 1.1 tsubai
133 1.1 tsubai sc->sc_ioproc = NULL;
134 1.1 tsubai
135 1.1 tsubai sc->sc_buttons = 0;
136 1.1 tsubai
137 1.1 tsubai sc->sc_open = 0;
138 1.1 tsubai
139 1.1 tsubai aed_sc = sc;
140 1.1 tsubai
141 1.1 tsubai printf("ADB Event device\n");
142 1.1 tsubai
143 1.1 tsubai return;
144 1.1 tsubai }
145 1.1 tsubai
146 1.1 tsubai /*
147 1.1 tsubai * Given a keyboard ADB event, record the keycode and call the key
148 1.1 tsubai * repeat handler, optionally passing the event through the mouse
149 1.1 tsubai * button emulation handler first. Pass mouse events directly to
150 1.1 tsubai * the handoff function.
151 1.1 tsubai */
152 1.1 tsubai void
153 1.23 dsl aed_input(adb_event_t *event)
154 1.1 tsubai {
155 1.1 tsubai adb_event_t new_event = *event;
156 1.1 tsubai
157 1.1 tsubai switch (event->def_addr) {
158 1.1 tsubai case ADBADDR_KBD:
159 1.1 tsubai if (aed_sc->sc_options & AED_MSEMUL)
160 1.1 tsubai aed_emulate_mouse(&new_event);
161 1.1 tsubai else
162 1.1 tsubai aed_dokeyupdown(&new_event);
163 1.1 tsubai break;
164 1.1 tsubai case ADBADDR_MS:
165 1.4 tsubai new_event.u.m.buttons |= aed_sc->sc_buttons;
166 1.1 tsubai aed_handoff(&new_event);
167 1.1 tsubai break;
168 1.1 tsubai default: /* God only knows. */
169 1.1 tsubai #ifdef DIAGNOSTIC
170 1.7 provos panic("aed: received event from unsupported device!");
171 1.1 tsubai #endif
172 1.1 tsubai break;
173 1.1 tsubai }
174 1.1 tsubai
175 1.1 tsubai }
176 1.1 tsubai
177 1.1 tsubai /*
178 1.1 tsubai * Handles mouse button emulation via the keyboard. If the emulation
179 1.1 tsubai * modifier key is down, left and right arrows will generate 2nd and
180 1.1 tsubai * 3rd mouse button events while the 1, 2, and 3 keys will generate
181 1.1 tsubai * the corresponding mouse button event.
182 1.1 tsubai */
183 1.1 tsubai static void
184 1.23 dsl aed_emulate_mouse(adb_event_t *event)
185 1.1 tsubai {
186 1.1 tsubai static int emulmodkey_down = 0;
187 1.1 tsubai adb_event_t new_event;
188 1.1 tsubai
189 1.1 tsubai if (event->u.k.key == ADBK_KEYDOWN(ADBK_OPTION)) {
190 1.1 tsubai emulmodkey_down = 1;
191 1.1 tsubai } else if (event->u.k.key == ADBK_KEYUP(ADBK_OPTION)) {
192 1.1 tsubai /* key up */
193 1.1 tsubai emulmodkey_down = 0;
194 1.1 tsubai if (aed_sc->sc_buttons & 0xfe) {
195 1.1 tsubai aed_sc->sc_buttons &= 1;
196 1.1 tsubai new_event.def_addr = ADBADDR_MS;
197 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
198 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
199 1.1 tsubai microtime(&new_event.timestamp);
200 1.1 tsubai aed_handoff(&new_event);
201 1.1 tsubai }
202 1.1 tsubai } else if (emulmodkey_down) {
203 1.1 tsubai switch(event->u.k.key) {
204 1.1 tsubai #ifdef ALTXBUTTONS
205 1.1 tsubai case ADBK_KEYDOWN(ADBK_1):
206 1.1 tsubai aed_sc->sc_buttons |= 1; /* left down */
207 1.1 tsubai new_event.def_addr = ADBADDR_MS;
208 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
209 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
210 1.1 tsubai microtime(&new_event.timestamp);
211 1.1 tsubai aed_handoff(&new_event);
212 1.1 tsubai break;
213 1.1 tsubai case ADBK_KEYUP(ADBK_1):
214 1.4 tsubai aed_sc->sc_buttons &= ~1; /* left up */
215 1.1 tsubai new_event.def_addr = ADBADDR_MS;
216 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
217 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
218 1.1 tsubai microtime(&new_event.timestamp);
219 1.1 tsubai aed_handoff(&new_event);
220 1.1 tsubai break;
221 1.1 tsubai #endif
222 1.1 tsubai case ADBK_KEYDOWN(ADBK_LEFT):
223 1.1 tsubai #ifdef ALTXBUTTONS
224 1.1 tsubai case ADBK_KEYDOWN(ADBK_2):
225 1.1 tsubai #endif
226 1.1 tsubai aed_sc->sc_buttons |= 2; /* middle down */
227 1.1 tsubai new_event.def_addr = ADBADDR_MS;
228 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
229 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
230 1.1 tsubai microtime(&new_event.timestamp);
231 1.1 tsubai aed_handoff(&new_event);
232 1.1 tsubai break;
233 1.1 tsubai case ADBK_KEYUP(ADBK_LEFT):
234 1.1 tsubai #ifdef ALTXBUTTONS
235 1.1 tsubai case ADBK_KEYUP(ADBK_2):
236 1.1 tsubai #endif
237 1.1 tsubai aed_sc->sc_buttons &= ~2; /* middle up */
238 1.1 tsubai new_event.def_addr = ADBADDR_MS;
239 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
240 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
241 1.1 tsubai microtime(&new_event.timestamp);
242 1.1 tsubai aed_handoff(&new_event);
243 1.1 tsubai break;
244 1.1 tsubai case ADBK_KEYDOWN(ADBK_RIGHT):
245 1.1 tsubai #ifdef ALTXBUTTONS
246 1.1 tsubai case ADBK_KEYDOWN(ADBK_3):
247 1.1 tsubai #endif
248 1.1 tsubai aed_sc->sc_buttons |= 4; /* right down */
249 1.1 tsubai new_event.def_addr = ADBADDR_MS;
250 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
251 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
252 1.1 tsubai microtime(&new_event.timestamp);
253 1.1 tsubai aed_handoff(&new_event);
254 1.1 tsubai break;
255 1.1 tsubai case ADBK_KEYUP(ADBK_RIGHT):
256 1.4 tsubai #ifdef ALTXBUTTONS
257 1.1 tsubai case ADBK_KEYUP(ADBK_3):
258 1.4 tsubai #endif
259 1.1 tsubai aed_sc->sc_buttons &= ~4; /* right up */
260 1.1 tsubai new_event.def_addr = ADBADDR_MS;
261 1.1 tsubai new_event.u.m.buttons = aed_sc->sc_buttons;
262 1.1 tsubai new_event.u.m.dx = new_event.u.m.dy = 0;
263 1.1 tsubai microtime(&new_event.timestamp);
264 1.1 tsubai aed_handoff(&new_event);
265 1.1 tsubai break;
266 1.4 tsubai case ADBK_KEYUP(ADBK_SHIFT):
267 1.4 tsubai case ADBK_KEYDOWN(ADBK_SHIFT):
268 1.4 tsubai case ADBK_KEYUP(ADBK_CONTROL):
269 1.4 tsubai case ADBK_KEYDOWN(ADBK_CONTROL):
270 1.4 tsubai case ADBK_KEYUP(ADBK_FLOWER):
271 1.4 tsubai case ADBK_KEYDOWN(ADBK_FLOWER):
272 1.1 tsubai /* ctrl, shift, cmd */
273 1.1 tsubai aed_dokeyupdown(event);
274 1.1 tsubai break;
275 1.1 tsubai default:
276 1.1 tsubai if (event->u.k.key & 0x80)
277 1.1 tsubai /* ignore keyup */
278 1.1 tsubai break;
279 1.1 tsubai
280 1.1 tsubai /* key down */
281 1.1 tsubai new_event = *event;
282 1.1 tsubai
283 1.1 tsubai /* send option-down */
284 1.1 tsubai new_event.u.k.key = ADBK_KEYDOWN(ADBK_OPTION);
285 1.1 tsubai new_event.bytes[0] = new_event.u.k.key;
286 1.1 tsubai microtime(&new_event.timestamp);
287 1.1 tsubai aed_dokeyupdown(&new_event);
288 1.1 tsubai
289 1.1 tsubai /* send key-down */
290 1.1 tsubai new_event.u.k.key = event->bytes[0];
291 1.1 tsubai new_event.bytes[0] = new_event.u.k.key;
292 1.1 tsubai microtime(&new_event.timestamp);
293 1.1 tsubai aed_dokeyupdown(&new_event);
294 1.1 tsubai
295 1.1 tsubai /* send key-up */
296 1.1 tsubai new_event.u.k.key =
297 1.1 tsubai ADBK_KEYUP(ADBK_KEYVAL(event->bytes[0]));
298 1.1 tsubai microtime(&new_event.timestamp);
299 1.1 tsubai new_event.bytes[0] = new_event.u.k.key;
300 1.1 tsubai aed_dokeyupdown(&new_event);
301 1.1 tsubai
302 1.1 tsubai /* send option-up */
303 1.1 tsubai new_event.u.k.key = ADBK_KEYUP(ADBK_OPTION);
304 1.1 tsubai new_event.bytes[0] = new_event.u.k.key;
305 1.1 tsubai microtime(&new_event.timestamp);
306 1.1 tsubai aed_dokeyupdown(&new_event);
307 1.1 tsubai break;
308 1.1 tsubai }
309 1.1 tsubai } else {
310 1.1 tsubai aed_dokeyupdown(event);
311 1.1 tsubai }
312 1.1 tsubai }
313 1.1 tsubai
314 1.1 tsubai /*
315 1.1 tsubai * Keyboard autorepeat timeout function. Sends key up/down events
316 1.1 tsubai * for the repeating key and schedules the next call at sc_rptinterval
317 1.1 tsubai * ticks in the future.
318 1.1 tsubai */
319 1.1 tsubai static void
320 1.23 dsl aed_kbdrpt(void *kstate)
321 1.1 tsubai {
322 1.15 nathanw struct aed_softc *sc = (struct aed_softc *)kstate;
323 1.1 tsubai
324 1.15 nathanw sc->sc_rptevent.bytes[0] |= 0x80;
325 1.15 nathanw microtime(&sc->sc_rptevent.timestamp);
326 1.15 nathanw aed_handoff(&sc->sc_rptevent); /* do key up */
327 1.15 nathanw
328 1.15 nathanw sc->sc_rptevent.bytes[0] &= 0x7f;
329 1.15 nathanw microtime(&sc->sc_rptevent.timestamp);
330 1.15 nathanw aed_handoff(&sc->sc_rptevent); /* do key down */
331 1.1 tsubai
332 1.15 nathanw if (sc->sc_repeating == sc->sc_rptevent.u.k.key) {
333 1.15 nathanw callout_reset(&sc->sc_repeat_ch, sc->sc_rptinterval,
334 1.5 thorpej aed_kbdrpt, kstate);
335 1.1 tsubai }
336 1.1 tsubai }
337 1.1 tsubai
338 1.1 tsubai
339 1.1 tsubai /*
340 1.1 tsubai * Cancels the currently repeating key event if there is one, schedules
341 1.1 tsubai * a new repeating key event if needed, and hands the event off to the
342 1.1 tsubai * appropriate subsystem.
343 1.1 tsubai */
344 1.1 tsubai static void
345 1.23 dsl aed_dokeyupdown(adb_event_t *event)
346 1.1 tsubai {
347 1.1 tsubai int kbd_key;
348 1.1 tsubai
349 1.1 tsubai kbd_key = ADBK_KEYVAL(event->u.k.key);
350 1.1 tsubai if (ADBK_PRESS(event->u.k.key) && keyboard[kbd_key][0] != 0) {
351 1.1 tsubai /* ignore shift & control */
352 1.1 tsubai if (aed_sc->sc_repeating != -1) {
353 1.5 thorpej callout_stop(&aed_sc->sc_repeat_ch);
354 1.1 tsubai }
355 1.1 tsubai aed_sc->sc_rptevent = *event;
356 1.1 tsubai aed_sc->sc_repeating = kbd_key;
357 1.5 thorpej callout_reset(&aed_sc->sc_repeat_ch, aed_sc->sc_rptdelay,
358 1.5 thorpej aed_kbdrpt, (void *)aed_sc);
359 1.1 tsubai } else {
360 1.1 tsubai if (aed_sc->sc_repeating != -1) {
361 1.1 tsubai aed_sc->sc_repeating = -1;
362 1.5 thorpej callout_stop(&aed_sc->sc_repeat_ch);
363 1.1 tsubai }
364 1.1 tsubai aed_sc->sc_rptevent = *event;
365 1.1 tsubai }
366 1.1 tsubai aed_handoff(event);
367 1.1 tsubai }
368 1.1 tsubai
369 1.1 tsubai /*
370 1.1 tsubai * Place the event in the event queue if a requesting device is open
371 1.3 tsubai * and we are not polling.
372 1.1 tsubai */
373 1.1 tsubai static void
374 1.23 dsl aed_handoff(adb_event_t *event)
375 1.1 tsubai {
376 1.1 tsubai if (aed_sc->sc_open && !adb_polling)
377 1.1 tsubai aed_enqevent(event);
378 1.1 tsubai }
379 1.1 tsubai
380 1.1 tsubai /*
381 1.1 tsubai * Place the event in the event queue and wakeup any waiting processes.
382 1.1 tsubai */
383 1.1 tsubai static void
384 1.23 dsl aed_enqevent(adb_event_t *event)
385 1.1 tsubai {
386 1.1 tsubai int s;
387 1.1 tsubai
388 1.1 tsubai s = spladb();
389 1.1 tsubai
390 1.1 tsubai #ifdef DIAGNOSTIC
391 1.1 tsubai if (aed_sc->sc_evq_tail < 0 || aed_sc->sc_evq_tail >= AED_MAX_EVENTS)
392 1.1 tsubai panic("adb: event queue tail is out of bounds");
393 1.1 tsubai
394 1.1 tsubai if (aed_sc->sc_evq_len < 0 || aed_sc->sc_evq_len > AED_MAX_EVENTS)
395 1.1 tsubai panic("adb: event queue len is out of bounds");
396 1.1 tsubai #endif
397 1.1 tsubai
398 1.1 tsubai if (aed_sc->sc_evq_len == AED_MAX_EVENTS) {
399 1.1 tsubai splx(s);
400 1.1 tsubai return; /* Oh, well... */
401 1.1 tsubai }
402 1.1 tsubai aed_sc->sc_evq[(aed_sc->sc_evq_len + aed_sc->sc_evq_tail) %
403 1.1 tsubai AED_MAX_EVENTS] = *event;
404 1.1 tsubai aed_sc->sc_evq_len++;
405 1.1 tsubai
406 1.21 rmind selnotify(&aed_sc->sc_selinfo, 0, 0);
407 1.1 tsubai if (aed_sc->sc_ioproc)
408 1.1 tsubai psignal(aed_sc->sc_ioproc, SIGIO);
409 1.1 tsubai
410 1.1 tsubai splx(s);
411 1.1 tsubai }
412 1.1 tsubai
413 1.1 tsubai int
414 1.24 dsl aedopen(dev_t dev, int flag, int mode, struct lwp *l)
415 1.1 tsubai {
416 1.1 tsubai int unit;
417 1.1 tsubai int error = 0;
418 1.1 tsubai int s;
419 1.1 tsubai
420 1.1 tsubai unit = minor(dev);
421 1.1 tsubai
422 1.1 tsubai if (unit != 0)
423 1.1 tsubai return (ENXIO);
424 1.1 tsubai
425 1.1 tsubai s = spladb();
426 1.1 tsubai if (aed_sc->sc_open) {
427 1.1 tsubai splx(s);
428 1.1 tsubai return (EBUSY);
429 1.1 tsubai }
430 1.1 tsubai aed_sc->sc_evq_tail = 0;
431 1.1 tsubai aed_sc->sc_evq_len = 0;
432 1.1 tsubai aed_sc->sc_open = 1;
433 1.16 christos aed_sc->sc_ioproc = l->l_proc;
434 1.1 tsubai splx(s);
435 1.1 tsubai
436 1.1 tsubai return (error);
437 1.1 tsubai }
438 1.1 tsubai
439 1.1 tsubai
440 1.1 tsubai int
441 1.24 dsl aedclose(dev_t dev, int flag, int mode, struct lwp *l)
442 1.1 tsubai {
443 1.1 tsubai int s = spladb();
444 1.1 tsubai
445 1.1 tsubai aed_sc->sc_open = 0;
446 1.1 tsubai aed_sc->sc_ioproc = NULL;
447 1.1 tsubai splx(s);
448 1.1 tsubai
449 1.1 tsubai return (0);
450 1.1 tsubai }
451 1.1 tsubai
452 1.1 tsubai
453 1.1 tsubai int
454 1.23 dsl aedread(dev_t dev, struct uio *uio, int flag)
455 1.1 tsubai {
456 1.1 tsubai int s, error;
457 1.1 tsubai int willfit;
458 1.1 tsubai int total;
459 1.1 tsubai int firstmove;
460 1.1 tsubai int moremove;
461 1.1 tsubai
462 1.1 tsubai if (uio->uio_resid < sizeof(adb_event_t))
463 1.1 tsubai return (EMSGSIZE); /* close enough. */
464 1.1 tsubai
465 1.1 tsubai s = spladb();
466 1.1 tsubai if (aed_sc->sc_evq_len == 0) {
467 1.1 tsubai splx(s);
468 1.1 tsubai return (0);
469 1.1 tsubai }
470 1.1 tsubai willfit = howmany(uio->uio_resid, sizeof(adb_event_t));
471 1.1 tsubai total = (aed_sc->sc_evq_len < willfit) ? aed_sc->sc_evq_len : willfit;
472 1.1 tsubai
473 1.1 tsubai firstmove = (aed_sc->sc_evq_tail + total > AED_MAX_EVENTS)
474 1.1 tsubai ? (AED_MAX_EVENTS - aed_sc->sc_evq_tail) : total;
475 1.1 tsubai
476 1.18 christos error = uiomove((void *) & aed_sc->sc_evq[aed_sc->sc_evq_tail],
477 1.1 tsubai firstmove * sizeof(adb_event_t), uio);
478 1.1 tsubai if (error) {
479 1.1 tsubai splx(s);
480 1.1 tsubai return (error);
481 1.1 tsubai }
482 1.1 tsubai moremove = total - firstmove;
483 1.1 tsubai
484 1.1 tsubai if (moremove > 0) {
485 1.18 christos error = uiomove((void *) & aed_sc->sc_evq[0],
486 1.1 tsubai moremove * sizeof(adb_event_t), uio);
487 1.1 tsubai if (error) {
488 1.1 tsubai splx(s);
489 1.1 tsubai return (error);
490 1.1 tsubai }
491 1.1 tsubai }
492 1.1 tsubai aed_sc->sc_evq_tail = (aed_sc->sc_evq_tail + total) % AED_MAX_EVENTS;
493 1.1 tsubai aed_sc->sc_evq_len -= total;
494 1.1 tsubai splx(s);
495 1.1 tsubai return (0);
496 1.1 tsubai }
497 1.1 tsubai
498 1.1 tsubai int
499 1.23 dsl aedioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
500 1.1 tsubai {
501 1.1 tsubai switch (cmd) {
502 1.1 tsubai case ADBIOCDEVSINFO: {
503 1.1 tsubai adb_devinfo_t *di;
504 1.1 tsubai ADBDataBlock adbdata;
505 1.1 tsubai int totaldevs;
506 1.1 tsubai int adbaddr;
507 1.1 tsubai int i;
508 1.1 tsubai
509 1.1 tsubai di = (void *)data;
510 1.1 tsubai
511 1.1 tsubai /* Initialize to no devices */
512 1.1 tsubai for (i = 0; i < 16; i++)
513 1.1 tsubai di->dev[i].addr = -1;
514 1.1 tsubai
515 1.1 tsubai totaldevs = CountADBs();
516 1.1 tsubai for (i = 1; i <= totaldevs; i++) {
517 1.1 tsubai adbaddr = GetIndADB(&adbdata, i);
518 1.1 tsubai di->dev[adbaddr].addr = adbaddr;
519 1.4 tsubai di->dev[adbaddr].default_addr = (int)(adbdata.origADBAddr);
520 1.4 tsubai di->dev[adbaddr].handler_id = (int)(adbdata.devType);
521 1.1 tsubai }
522 1.1 tsubai
523 1.1 tsubai /* Must call ADB Manager to get devices now */
524 1.1 tsubai break;
525 1.1 tsubai }
526 1.1 tsubai
527 1.1 tsubai case ADBIOCGETREPEAT:{
528 1.1 tsubai adb_rptinfo_t *ri;
529 1.1 tsubai
530 1.1 tsubai ri = (void *)data;
531 1.1 tsubai ri->delay_ticks = aed_sc->sc_rptdelay;
532 1.1 tsubai ri->interval_ticks = aed_sc->sc_rptinterval;
533 1.1 tsubai break;
534 1.1 tsubai }
535 1.1 tsubai
536 1.1 tsubai case ADBIOCSETREPEAT:{
537 1.1 tsubai adb_rptinfo_t *ri;
538 1.1 tsubai
539 1.1 tsubai ri = (void *) data;
540 1.1 tsubai aed_sc->sc_rptdelay = ri->delay_ticks;
541 1.1 tsubai aed_sc->sc_rptinterval = ri->interval_ticks;
542 1.1 tsubai break;
543 1.1 tsubai }
544 1.1 tsubai
545 1.1 tsubai case ADBIOCRESET:
546 1.1 tsubai /* Do nothing for now */
547 1.1 tsubai break;
548 1.1 tsubai
549 1.1 tsubai case ADBIOCLISTENCMD:{
550 1.1 tsubai adb_listencmd_t *lc;
551 1.1 tsubai
552 1.1 tsubai lc = (void *)data;
553 1.1 tsubai }
554 1.1 tsubai
555 1.1 tsubai default:
556 1.1 tsubai return (EINVAL);
557 1.1 tsubai }
558 1.1 tsubai return (0);
559 1.1 tsubai }
560 1.1 tsubai
561 1.1 tsubai
562 1.1 tsubai int
563 1.23 dsl aedpoll(dev_t dev, int events, struct lwp *l)
564 1.1 tsubai {
565 1.1 tsubai int s, revents;
566 1.1 tsubai
567 1.1 tsubai revents = events & (POLLOUT | POLLWRNORM);
568 1.1 tsubai
569 1.1 tsubai if ((events & (POLLIN | POLLRDNORM)) == 0)
570 1.1 tsubai return (revents);
571 1.1 tsubai
572 1.1 tsubai s = spladb();
573 1.1 tsubai if (aed_sc->sc_evq_len > 0)
574 1.1 tsubai revents |= events & (POLLIN | POLLRDNORM);
575 1.1 tsubai else
576 1.16 christos selrecord(l, &aed_sc->sc_selinfo);
577 1.1 tsubai splx(s);
578 1.1 tsubai
579 1.1 tsubai return (revents);
580 1.10 jdolecek }
581 1.10 jdolecek
582 1.10 jdolecek static void
583 1.10 jdolecek filt_aedrdetach(struct knote *kn)
584 1.10 jdolecek {
585 1.10 jdolecek int s;
586 1.10 jdolecek
587 1.10 jdolecek s = spladb();
588 1.11 christos SLIST_REMOVE(&aed_sc->sc_selinfo.sel_klist, kn, knote, kn_selnext);
589 1.10 jdolecek splx(s);
590 1.10 jdolecek }
591 1.10 jdolecek
592 1.10 jdolecek static int
593 1.10 jdolecek filt_aedread(struct knote *kn, long hint)
594 1.10 jdolecek {
595 1.10 jdolecek
596 1.10 jdolecek kn->kn_data = aed_sc->sc_evq_len * sizeof(adb_event_t);
597 1.10 jdolecek return (kn->kn_data > 0);
598 1.10 jdolecek }
599 1.10 jdolecek
600 1.10 jdolecek static const struct filterops aedread_filtops =
601 1.10 jdolecek { 1, NULL, filt_aedrdetach, filt_aedread };
602 1.10 jdolecek
603 1.10 jdolecek static const struct filterops aed_seltrue_filtops =
604 1.10 jdolecek { 1, NULL, filt_aedrdetach, filt_seltrue };
605 1.10 jdolecek
606 1.10 jdolecek int
607 1.10 jdolecek aedkqfilter(dev_t dev, struct knote *kn)
608 1.10 jdolecek {
609 1.10 jdolecek struct klist *klist;
610 1.10 jdolecek int s;
611 1.10 jdolecek
612 1.10 jdolecek switch (kn->kn_filter) {
613 1.10 jdolecek case EVFILT_READ:
614 1.11 christos klist = &aed_sc->sc_selinfo.sel_klist;
615 1.10 jdolecek kn->kn_fop = &aedread_filtops;
616 1.10 jdolecek break;
617 1.10 jdolecek
618 1.10 jdolecek case EVFILT_WRITE:
619 1.11 christos klist = &aed_sc->sc_selinfo.sel_klist;
620 1.10 jdolecek kn->kn_fop = &aed_seltrue_filtops;
621 1.10 jdolecek break;
622 1.10 jdolecek
623 1.10 jdolecek default:
624 1.10 jdolecek return (1);
625 1.10 jdolecek }
626 1.10 jdolecek
627 1.10 jdolecek kn->kn_hook = NULL;
628 1.10 jdolecek
629 1.10 jdolecek s = spladb();
630 1.10 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
631 1.10 jdolecek splx(s);
632 1.10 jdolecek
633 1.10 jdolecek return (0);
634 1.1 tsubai }
635