Debian Codegraph @

i386/i386at/kd_event.c

raw
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
4 * All Rights Reserved.
5 *
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
11 *
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or [email protected]
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
25 */
26 /* **********************************************************************
27 File: kd_event.c
28 Description: Driver for event interface to keyboard.
29
30 $ Header: $
31
32 Copyright Ing. C. Olivetti & C. S.p.A. 1989. All rights reserved.
33 ********************************************************************** */
34 /*
35 Copyright 1988, 1989 by Olivetti Advanced Technology Center, Inc.,
36 Cupertino, California.
37
38 All Rights Reserved
39
40 Permission to use, copy, modify, and distribute this software and
41 its documentation for any purpose and without fee is hereby
42 granted, provided that the above copyright notice appears in all
43 copies and that both the copyright notice and this permission notice
44 appear in supporting documentation, and that the name of Olivetti
45 not be used in advertising or publicity pertaining to distribution
46 of the software without specific, written prior permission.
47
48 OLIVETTI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
49 INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
50 IN NO EVENT SHALL OLIVETTI BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
51 CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
52 LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
53 NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUR OF OR IN CONNECTION
54 WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
55 */
56
57 #include <mach/boolean.h>
58 #include <sys/types.h>
59 #include <kern/printf.h>
60 #include <string.h>
61
62 #include <device/ds_routines.h>
63 #include <device/device_types.h>
64 #include <device/io_req.h>
65 #include <i386/spl.h>
66 #include <i386/pio.h>
67 #include <i386at/kd.h>
68 #include <i386at/kd_queue.h>
69 #ifdef APIC
70 # include <i386/apic.h>
71 #else
72 # include <i386/pic.h>
73 #endif
74
75 #include "kd_event.h"
76
77 /*
78 * Code for /dev/kbd. The interrupt processing is done in kd.c,
79 * which calls into this module to enqueue scancode events when
80 * the keyboard is in Event mode.
81 */
82
83 /*
84 * Note: These globals are protected by raising the interrupt level
85 * via SPLKD.
86 */
87
88 kd_event_queue kbd_queue; /* queue of keyboard events */
89 queue_head_t kbd_read_queue = { &kbd_read_queue, &kbd_read_queue };
90
91 static boolean_t initialized = FALSE;
92
93
94 /*
95 * kbdinit - set up event queue.
96 */
97
98 static void
99 kbdinit(void)
100 {
101 spl_t s = SPLKD();
102
103 if (!initialized) {
104 kdq_reset(&kbd_queue);
105 initialized = TRUE;
106 }
107 splx(s);
108 }
109
110
111 /*
112 * kbdopen - Verify that open is read-only and remember process
113 * group leader.
114 */
115
116 /*ARGSUSED*/
117 int
118 kbdopen(dev_t dev, int flags, io_req_t ior)
119 {
120 spl_t o_pri = spltty();
121 kdinit();
122 splx(o_pri);
123 kbdinit();
124
125 return(0);
126 }
127
128
129 /*
130 * kbdclose - Make sure that the kd driver is in Ascii mode and
131 * reset various flags.
132 */
133
134 /*ARGSUSED*/
135 void
136 kbdclose(
137 dev_t dev,
138 int flags)
139 {
140 spl_t s = SPLKD();
141
142 kb_mode = KB_ASCII;
143 kdq_reset(&kbd_queue);
144 splx(s);
145 }
146
147
148 io_return_t kbdgetstat(
149 dev_t dev,
150 dev_flavor_t flavor,
151 dev_status_t data, /* pointer to OUT array */
152 mach_msg_type_number_t *count) /* OUT */
153 {
154 switch (flavor) {
155 case KDGKBDTYPE:
156 *data = KB_VANILLAKB;
157 *count = 1;
158 break;
159 case DEV_GET_SIZE:
160 data[DEV_GET_SIZE_DEVICE_SIZE] = 0;
161 data[DEV_GET_SIZE_RECORD_SIZE] = sizeof(kd_event);
162 *count = DEV_GET_SIZE_COUNT;
163 break;
164 default:
165 return (D_INVALID_OPERATION);
166 }
167 return (D_SUCCESS);
168 }
169
170 io_return_t kbdsetstat(
171 dev_t dev,
172 dev_flavor_t flavor,
173 dev_status_t data,
174 mach_msg_type_number_t count)
175 {
176 switch (flavor) {
177 case KDSKBDMODE:
178 kb_mode = *data;
179 /* XXX - what to do about unread events? */
180 /* XXX - should check that 'data' contains an OK valud */
181 break;
182 case KDSETLEDS:
183 if (count != 1)
184 return (D_INVALID_OPERATION);
185 kd_setleds1 (*data);
186 break;
187 case K_X_KDB_ENTER:
188 return X_kdb_enter_init((unsigned int *)data, count);
189 case K_X_KDB_EXIT:
190 return X_kdb_exit_init((unsigned int *)data, count);
191 default:
192 return (D_INVALID_OPERATION);
193 }
194 return (D_SUCCESS);
195 }
196
197
198
199 /*
200 * kbdread - dequeue and return any queued events.
201 */
202 int
203 kbdread(
204 dev_t dev,
205 io_req_t ior)
206 {
207 int err, count;
208 spl_t s;
209
210 /* Check if IO_COUNT is a multiple of the record size. */
211 if (ior->io_count % sizeof(kd_event) != 0)
212 return D_INVALID_SIZE;
213
214 err = device_read_alloc(ior, (vm_size_t)ior->io_count);
215 if (err != KERN_SUCCESS)
216 return (err);
217
218 s = SPLKD();
219 if (kdq_empty(&kbd_queue)) {
220 if (ior->io_mode & D_NOWAIT) {
221 splx(s);
222 return (D_WOULD_BLOCK);
223 }
224 ior->io_done = kbd_read_done;
225 enqueue_tail(&kbd_read_queue, (queue_entry_t) ior);
226 splx(s);
227 return (D_IO_QUEUED);
228 }
229 count = 0;
230 while (!kdq_empty(&kbd_queue) && count < ior->io_count) {
231 kd_event *ev;
232
233 ev = kdq_get(&kbd_queue);
234 *(kd_event *)(&ior->io_data[count]) = *ev;
235 count += sizeof(kd_event);
236 }
237 splx(s);
238 ior->io_residual = ior->io_count - count;
239 return (D_SUCCESS);
240 }
241
242 boolean_t kbd_read_done(io_req_t ior)
243 {
244 int count;
245 spl_t s;
246
247 s = SPLKD();
248 if (kdq_empty(&kbd_queue)) {
249 ior->io_done = kbd_read_done;
250 enqueue_tail(&kbd_read_queue, (queue_entry_t)ior);
251 splx(s);
252 return (FALSE);
253 }
254
255 count = 0;
256 while (!kdq_empty(&kbd_queue) && count < ior->io_count) {
257 kd_event *ev;
258
259 ev = kdq_get(&kbd_queue);
260 *(kd_event *)(&ior->io_data[count]) = *ev;
261 count += sizeof(kd_event);
262 }
263 splx(s);
264
265 ior->io_residual = ior->io_count - count;
266 ds_read_done(ior);
267
268 return (TRUE);
269 }
270
271
272
273 /*
274 * kd_enqsc - enqueue a scancode. Should be called at SPLKD.
275 */
276
277 void
278 kd_enqsc(Scancode sc)
279 {
280 kd_event ev;
281
282 ev.type = KEYBD_EVENT;
283 /* Not used but we set it to avoid garbage */
284 ev.unused_time.seconds = 0;
285 ev.unused_time.microseconds = 0;
286 ev.value.sc = sc;
287 kbd_enqueue(&ev);
288 }
289
290
291 /*
292 * kbd_enqueue - enqueue an event and wake up selecting processes, if
293 * any. Should be called at SPLKD.
294 */
295
296 void
297 kbd_enqueue(kd_event *ev)
298 {
299 if (kdq_full(&kbd_queue))
300 printf_once("kbd: queue full\n");
301 else
302 kdq_put(&kbd_queue, ev);
303
304 {
305 io_req_t ior;
306 while ((ior = (io_req_t)dequeue_head(&kbd_read_queue)) != 0)
307 iodone(ior);
308 }
309 }
310
311 u_int X_kdb_enter_str[512], X_kdb_exit_str[512];
312 int X_kdb_enter_len = 0, X_kdb_exit_len = 0;
313
314 static void
315 kdb_in_out(const u_int *p)
316 {
317 int t = p[0];
318
319 switch (t & K_X_TYPE) {
320 case K_X_IN|K_X_BYTE:
321 inb(t & K_X_PORT);
322 break;
323
324 case K_X_IN|K_X_WORD:
325 inw(t & K_X_PORT);
326 break;
327
328 case K_X_IN|K_X_LONG:
329 inl(t & K_X_PORT);
330 break;
331
332 case K_X_OUT|K_X_BYTE:
333 outb(t & K_X_PORT, p[1]);
334 break;
335
336 case K_X_OUT|K_X_WORD:
337 outw(t & K_X_PORT, p[1]);
338 break;
339
340 case K_X_OUT|K_X_LONG:
341 outl(t & K_X_PORT, p[1]);
342 break;
343 }
344 }
345
346 void
347 X_kdb_enter(void)
348 {
349 u_int *u_ip, *endp;
350
351 for (u_ip = X_kdb_enter_str, endp = &X_kdb_enter_str[X_kdb_enter_len];
352 u_ip < endp;
353 u_ip += 2)
354 kdb_in_out(u_ip);
355 }
356
357 void
358 X_kdb_exit(void)
359 {
360 u_int *u_ip, *endp;
361
362 for (u_ip = X_kdb_exit_str, endp = &X_kdb_exit_str[X_kdb_exit_len];
363 u_ip < endp;
364 u_ip += 2)
365 kdb_in_out(u_ip);
366 }
367
368 io_return_t
369 X_kdb_enter_init(
370 u_int *data,
371 u_int count)
372 {
373 if (count * sizeof X_kdb_enter_str[0] > sizeof X_kdb_enter_str)
374 return D_INVALID_OPERATION;
375
376 memcpy(X_kdb_enter_str, data, count * sizeof X_kdb_enter_str[0]);
377 X_kdb_enter_len = count;
378 return D_SUCCESS;
379 }
380
381 io_return_t
382 X_kdb_exit_init(
383 u_int *data,
384 u_int count)
385 {
386 if (count * sizeof X_kdb_exit_str[0] > sizeof X_kdb_exit_str)
387 return D_INVALID_OPERATION;
388
389 memcpy(X_kdb_exit_str, data, count * sizeof X_kdb_exit_str[0]);
390 X_kdb_exit_len = count;
391 return D_SUCCESS;
392 }