summaryrefslogtreecommitdiff
path: root/i386/i386at/kd_event.c
blob: 909f066bb07620afcb4614490fc1e48fee8ec554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
 * Mach Operating System
 * Copyright (c) 1991,1990,1989 Carnegie Mellon University
 * All Rights Reserved.
 *
 * Permission to use, copy, modify and distribute this software and its
 * documentation is hereby granted, provided that both the copyright
 * notice and this permission notice appear in all copies of the
 * software, derivative works or modified versions, and any portions
 * thereof, and that both notices appear in supporting documentation.
 *
 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
 *
 * Carnegie Mellon requests users of this software to return to
 *
 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
 *  School of Computer Science
 *  Carnegie Mellon University
 *  Pittsburgh PA 15213-3890
 *
 * any improvements or extensions that they make and grant Carnegie Mellon
 * the rights to redistribute these changes.
 */
/* **********************************************************************
 File:         kd_event.c
 Description:  Driver for event interface to keyboard.

 $ Header: $

 Copyright Ing. C. Olivetti & C. S.p.A. 1989.  All rights reserved.
********************************************************************** */
/*
  Copyright 1988, 1989 by Olivetti Advanced Technology Center, Inc.,
Cupertino, California.

		All Rights Reserved

  Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appears in all
copies and that both the copyright notice and this permission notice
appear in supporting documentation, and that the name of Olivetti
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.

  OLIVETTI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
IN NO EVENT SHALL OLIVETTI BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUR OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <mach/boolean.h>
#include <sys/types.h>
#include <string.h>

#ifdef	MACH_KERNEL
#include <device/errno.h>
#include <device/io_req.h>
#else	/* MACH_KERNEL */
#include <sys/file.h>
#include <sys/errno.h>
#include <kern/thread.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/ioctl.h>
#include <sys/tty.h>
#endif	/* MACH_KERNEL */
#include <i386/machspl.h>
#include <i386/pio.h>
#include <i386at/kd.h>
#include <i386at/kd_queue.h>

/*
 * Code for /dev/kbd.   The interrupt processing is done in kd.c,
 * which calls into this module to enqueue scancode events when
 * the keyboard is in Event mode.
 */

/*
 * Note: These globals are protected by raising the interrupt level
 * via SPLKD.
 */

kd_event_queue kbd_queue;		/* queue of keyboard events */
#ifdef	MACH_KERNEL
queue_head_t	kbd_read_queue = { &kbd_read_queue, &kbd_read_queue };
#else	/* MACH_KERNEL */
struct proc *kbd_sel = 0;		/* selecting process, if any */
short kbdpgrp = 0;		/* process group leader when dev is open */

int kbdflag = 0;
#define KBD_COLL	1		/* select collision */
#define KBD_ASYNC	2		/* user wants asynch notification */
#define KBD_NBIO	4		/* user wants non-blocking I/O */
#endif	/* MACH_KERNEL */


void kbd_enqueue();
#ifdef	MACH_KERNEL
io_return_t X_kdb_enter_init();
io_return_t X_kdb_exit_init();
#endif	/* MACH_KERNEL */

static boolean_t initialized = FALSE;


/*
 * kbdinit - set up event queue.
 */

kbdinit()
{
	spl_t s = SPLKD();

	if (!initialized) {
		kdq_reset(&kbd_queue);
		initialized = TRUE;
	}
	splx(s);
}


/*
 * kbdopen - Verify that open is read-only and remember process
 * group leader.
 */

/*ARGSUSED*/
kbdopen(dev, flags)
	dev_t dev;
	int flags;
{
	kbdinit();

#ifdef	MACH_KERNEL
#else	/* MACH_KERNEL */
	if (flags & FWRITE)
		return(ENODEV);

	if (kbdpgrp == 0)
		kbdpgrp = u.u_procp->p_pgrp;
#endif	/* MACH_KERNEL */
	return(0);
}


/*
 * kbdclose - Make sure that the kd driver is in Ascii mode and
 * reset various flags.
 */

/*ARGSUSED*/
kbdclose(dev, flags)
	dev_t dev;
	int flags;
{
	spl_t s = SPLKD();

	kb_mode = KB_ASCII;
#ifdef	MACH_KERNEL
#else	/* MACH_KERNEL */
	kbdpgrp = 0;
	kbdflag = 0;
	kbd_sel = 0;
#endif	/* MACH_KERNEL */
	kdq_reset(&kbd_queue);
	splx(s);
}


#ifdef	MACH_KERNEL
io_return_t kbdgetstat(dev, flavor, data, count)
	dev_t		dev;
	int		flavor;
	int *		data;		/* pointer to OUT array */
	unsigned int	*count;		/* OUT */
{
	io_return_t	result;

	switch (flavor) {
	    case KDGKBDTYPE:
		*data = KB_VANILLAKB;
		*count = 1;
		break;
	    case DEV_GET_SIZE:
		data[DEV_GET_SIZE_DEVICE_SIZE] = 0;
		data[DEV_GET_SIZE_RECORD_SIZE] = sizeof(kd_event);
		*count = DEV_GET_SIZE_COUNT;
		break;
	    default:
		return (D_INVALID_OPERATION);
	}
	return (D_SUCCESS);
}

io_return_t kbdsetstat(dev, flavor, data, count)
	dev_t		dev;
	int		flavor;
	int *		data;
	unsigned int	count;
{
	io_return_t	result;

	switch (flavor) {
	    case KDSKBDMODE:
		kb_mode = *data;
		/* XXX - what to do about unread events? */
		/* XXX - should check that 'data' contains an OK valud */
		break;
   	   case KDSETLEDS:
	        if (count != 1)
	            return (D_INVALID_OPERATION);
		kd_setleds1 (*data);
		break;
	    case K_X_KDB_ENTER:
		return X_kdb_enter_init(data, count);
	    case K_X_KDB_EXIT:
		return X_kdb_exit_init(data, count);
	    default:
		return (D_INVALID_OPERATION);
	}
	return (D_SUCCESS);
}

#else	/* MACH_KERNEL */
/*
 * kbdioctl - handling for asynch & non-blocking I/O.
 */

/*ARGSUSED*/
kbdioctl(dev, cmd, data, flag)
	dev_t dev;
	int cmd;
	caddr_t data;
	int flag;
{
	spl_t s = SPLKD();
	int err = 0;

	switch (cmd) {
	case KDSKBDMODE:
		kb_mode = *(int *)data;
		/* XXX - what to do about unread events? */
		/* XXX - should check that "data" contains an OK value */
		break;
	case KDGKBDTYPE:
		*(int *)data = KB_VANILLAKB;
		break;
	case K_X_KDB_ENTER:
		X_kdb_enter_init((struct X_kdb *) data);
		break;
	case K_X_KDB_EXIT:
		X_kdb_exit_init( (struct X_kdb *) data);
		break;
	case FIONBIO:
		if (*(int *)data)
			kbdflag |= KBD_NBIO;
		else
			kbdflag &= ~KBD_NBIO;
		break;
	case FIOASYNC:
		if (*(int *)data)
			kbdflag |= KBD_ASYNC;
		else
			kbdflag &= ~KBD_ASYNC;
		break;
	default:
		err = ENOTTY;
		break;
	}

	splx(s);
	return(err);
}


/*
 * kbdselect
 */

/*ARGSUSED*/
kbdselect(dev, rw)
{
	spl_t s = SPLKD();

	if (!kdq_empty(&kbd_queue)) {
		splx(s);
		return(1);
	}

	if (kbd_sel)
		kbdflag |= KBD_COLL;
	else
		kbd_sel = (struct proc *)current_thread();
					/* eeeyuck */

	splx(s);
	return(0);
}
#endif	/* MACH_KERNEL */


/*
 * kbdread - dequeue and return any queued events.
 */

#ifdef	MACH_KERNEL
boolean_t	kbd_read_done();	/* forward */

kbdread(dev, ior)
	dev_t	dev;
	register io_req_t	ior;
{
	register int	err, count;
	register spl_t	s;

	/* Check if IO_COUNT is a multiple of the record size. */
	if (ior->io_count % sizeof(kd_event) != 0)
	    return D_INVALID_SIZE;

	err = device_read_alloc(ior, (vm_size_t)ior->io_count);
	if (err != KERN_SUCCESS)
	    return (err);

	s = SPLKD();
	if (kdq_empty(&kbd_queue)) {
	    if (ior->io_mode & D_NOWAIT) {
		splx(s);
		return (D_WOULD_BLOCK);
	    }
	    ior->io_done = kbd_read_done;
	    enqueue_tail(&kbd_read_queue, (queue_entry_t) ior);
	    splx(s);
	    return (D_IO_QUEUED);
	}
	count = 0;
	while (!kdq_empty(&kbd_queue) && count < ior->io_count) {
	    register kd_event *ev;

	    ev = kdq_get(&kbd_queue);
	    *(kd_event *)(&ior->io_data[count]) = *ev;
	    count += sizeof(kd_event);
	}
	splx(s);
	ior->io_residual = ior->io_count - count;
	return (D_SUCCESS);
}

boolean_t kbd_read_done(ior)
	register io_req_t	ior;
{
	register int	count;
	register spl_t	s;

	s = SPLKD();
	if (kdq_empty(&kbd_queue)) {
	    ior->io_done = kbd_read_done;
	    enqueue_tail(&kbd_read_queue, (queue_entry_t)ior);
	    splx(s);
	    return (FALSE);
	}

	count = 0;
	while (!kdq_empty(&kbd_queue) && count < ior->io_count) {
	    register kd_event *ev;

	    ev = kdq_get(&kbd_queue);
	    *(kd_event *)(&ior->io_data[count]) = *ev;
	    count += sizeof(kd_event);
	}
	splx(s);

	ior->io_residual = ior->io_count - count;
	ds_read_done(ior);

	return (TRUE);
}

#else	/* MACH_KERNEL */
/*ARGSUSED*/
kbdread(dev, uio)
	dev_t dev;
	struct uio *uio;
{
	int s = SPLKD();
	int err = 0;
	kd_event *ev;
	int i;
	char *cp;

	if (kdq_empty(&kbd_queue))
		if (kbdflag & KBD_NBIO) {
			err = EWOULDBLOCK;
			goto done;
		} else
			while (kdq_empty(&kbd_queue)) {
				splx(s);
				sleep((caddr_t)&kbd_queue, TTIPRI);
				s = SPLKD();
			}

	while (!kdq_empty(&kbd_queue) && uio->uio_resid >= sizeof(kd_event)) {
		ev = kdq_get(&kbd_queue);
		for (cp = (char *)ev, i = 0; i < sizeof(kd_event);
		     ++i, ++cp) {
			err = ureadc(*cp, uio);
			if (err)
				goto done;
		}
	}

done:
	splx(s);
	return(err);
}
#endif	/* MACH_KERNEL */


/*
 * kd_enqsc - enqueue a scancode.  Should be called at SPLKD.
 */

void
kd_enqsc(sc)
	Scancode sc;
{
	kd_event ev;

	ev.type = KEYBD_EVENT;
	ev.time = time;
	ev.value.sc = sc;
	kbd_enqueue(&ev);
}


/*
 * kbd_enqueue - enqueue an event and wake up selecting processes, if
 * any.  Should be called at SPLKD.
 */

void
kbd_enqueue(ev)
	kd_event *ev;
{
	if (kdq_full(&kbd_queue))
		printf("kbd: queue full\n");
	else
		kdq_put(&kbd_queue, ev);

#ifdef	MACH_KERNEL
	{
	    register io_req_t	ior;
	    while ((ior = (io_req_t)dequeue_head(&kbd_read_queue)) != 0)
		iodone(ior);
	}
#else	/* MACH_KERNEL */
	if (kbd_sel) {
		selwakeup(kbd_sel, kbdflag & KBD_COLL);
		kbd_sel = 0;
		kbdflag &= ~KBD_COLL;
	}
	if (kbdflag & KBD_ASYNC)
		gsignal(kbdpgrp, SIGIO);
	wakeup((caddr_t)&kbd_queue);
#endif	/* MACH_KERNEL */
}

u_int X_kdb_enter_str[512], X_kdb_exit_str[512];
int   X_kdb_enter_len = 0,  X_kdb_exit_len = 0;

kdb_in_out(p)
u_int *p;
{
register int t = p[0];

	switch (t & K_X_TYPE) {
		case K_X_IN|K_X_BYTE:
			inb(t & K_X_PORT);
			break;

		case K_X_IN|K_X_WORD:
			inw(t & K_X_PORT);
			break;

		case K_X_IN|K_X_LONG:
			inl(t & K_X_PORT);
			break;

		case K_X_OUT|K_X_BYTE:
			outb(t & K_X_PORT, p[1]);
			break;

		case K_X_OUT|K_X_WORD:
			outw(t & K_X_PORT, p[1]);
			break;

		case K_X_OUT|K_X_LONG:
			outl(t & K_X_PORT, p[1]);
			break;
	}
}

X_kdb_enter()
{
register u_int *u_ip, *endp;

	for (u_ip = X_kdb_enter_str, endp = &X_kdb_enter_str[X_kdb_enter_len];
	     u_ip < endp;
	     u_ip += 2)
	    kdb_in_out(u_ip);
}

X_kdb_exit()
{
register u_int *u_ip, *endp;

	for (u_ip = X_kdb_exit_str, endp = &X_kdb_exit_str[X_kdb_exit_len];
	     u_ip < endp;
	     u_ip += 2)
	   kdb_in_out(u_ip);
}

#ifdef	MACH_KERNEL
io_return_t
X_kdb_enter_init(data, count)
    u_int *data;
    u_int count;
{
    if (count * sizeof X_kdb_enter_str[0] > sizeof X_kdb_enter_str)
	return D_INVALID_OPERATION;

    memcpy(X_kdb_enter_str, data, count * sizeof X_kdb_enter_str[0]);
    X_kdb_enter_len = count;
    return D_SUCCESS;
}

io_return_t
X_kdb_exit_init(data, count)
    u_int *data;
    u_int count;
{
    if (count * sizeof X_kdb_exit_str[0] > sizeof X_kdb_exit_str)
	return D_INVALID_OPERATION;

    memcpy(X_kdb_exit_str, data, count * sizeof X_kdb_exit_str[0]);
    X_kdb_exit_len = count;
    return D_SUCCESS;
}
#else	/* MACH_KERNEL */
X_kdb_enter_init(kp)
struct X_kdb *kp;
{
	if (kp->size > sizeof X_kdb_enter_str)
		u.u_error = ENOENT;
	else if(copyin(kp->ptr, X_kdb_enter_str, kp->size) == EFAULT)
		u.u_error = EFAULT;

	X_kdb_enter_len = kp->size>>2;
}

X_kdb_exit_init(kp)
struct X_kdb *kp;
{
	if (kp->size > sizeof X_kdb_exit_str)
		u.u_error = ENOENT;
	else if(copyin(kp->ptr, X_kdb_exit_str, kp->size) == EFAULT)
		u.u_error = EFAULT;

	X_kdb_exit_len = kp->size>>2;
}
#endif	/* MACH_KERNEL */