summaryrefslogtreecommitdiff
path: root/minix/servers/input/input.c
blob: 089e779b959fce9e681b0294d350b9106f0f49a5 (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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/* Keyboard/mouse input server. */
#include <minix/drivers.h>
#include <minix/chardriver.h>
#include <minix/ds.h>
#include <sys/ioctl.h>
#include <sys/kbdio.h>

#include "input.h"

#define INPUT_DEBUG 0

static int input_open(devminor_t, int, endpoint_t);
static int input_close(devminor_t);
static ssize_t input_read(devminor_t, u64_t, endpoint_t, cp_grant_id_t, size_t,
	int, cdev_id_t);
static int input_ioctl(devminor_t, unsigned long, endpoint_t, cp_grant_id_t,
	int, endpoint_t, cdev_id_t);
static int input_cancel(devminor_t, endpoint_t, cdev_id_t);
static int input_select(devminor_t, unsigned int, endpoint_t);
static void input_other(message *, int);

static struct input_dev devs[INPUT_DEV_MAX];

#define input_dev_active(dev)		((dev)->owner != NONE || \
					 (dev)->minor == KBDMUX_MINOR || \
					 (dev)->minor == MOUSEMUX_MINOR)
#define input_dev_buf_empty(dev)	((dev)->count == 0)
#define input_dev_buf_full(dev)		((dev)->count == EVENTBUF_SIZE)

/* Entry points to the input driver. */
static struct chardriver input_tab = {
	.cdr_open	= input_open,
	.cdr_close	= input_close,
	.cdr_read	= input_read,
	.cdr_ioctl	= input_ioctl,
	.cdr_cancel	= input_cancel,
	.cdr_select	= input_select,
	.cdr_other	= input_other
};

/*
 * Map a minor number to an input device structure.
 */
static struct input_dev *
input_map(devminor_t minor)
{
	/*
	 * The minor device numbers were chosen not to be equal to the array
	 * slots, so that more keyboards can be added without breaking backward
	 * compatibility later.
	 */
	if (minor == KBDMUX_MINOR)
		return &devs[KBDMUX_DEV];
	else if (minor >= KBD0_MINOR && minor < KBD0_MINOR + KBD_MINORS)
		return &devs[FIRST_KBD_DEV + (minor - KBD0_MINOR)];
	else if (minor == MOUSEMUX_MINOR)
		return &devs[MOUSEMUX_DEV];
	else if (minor >= MOUSE0_MINOR && minor < MOUSE0_MINOR + MOUSE_MINORS)
		return &devs[FIRST_MOUSE_DEV + (minor - MOUSE0_MINOR)];
	else
		return NULL;
}

/*
 * Map an input device structure index to a minor number.
 */
static devminor_t
input_revmap(int id)
{
	if (id == KBDMUX_DEV)
		return KBDMUX_MINOR;
	else if (id >= FIRST_KBD_DEV && id <= LAST_KBD_DEV)
		return KBD0_MINOR + (id - FIRST_KBD_DEV);
	else if (id == MOUSEMUX_DEV)
		return MOUSEMUX_MINOR;
	else if (id >= FIRST_MOUSE_DEV && id <= LAST_MOUSE_DEV)
		return MOUSE0_MINOR + (id - FIRST_MOUSE_DEV);
	else
		panic("reverse-mapping invalid ID %d", id);
}

/*
 * Open an input device.
 */
static int
input_open(devminor_t minor, int UNUSED(access), endpoint_t UNUSED(user_endpt))
{
	struct input_dev *input_dev;

	if ((input_dev = input_map(minor)) == NULL)
		return ENXIO;

	if (!input_dev_active(input_dev))
		return ENXIO;

	if (input_dev->opened)
		return EBUSY;

	input_dev->opened = TRUE;

	return OK;
}

/*
 * Close an input device.
 */
static int
input_close(devminor_t minor)
{
	struct input_dev *input_dev;

	if ((input_dev = input_map(minor)) == NULL)
		return ENXIO;

	if (!input_dev->opened) {
		printf("INPUT: closing already-closed device %d\n", minor);
		return EINVAL;
	}

	input_dev->opened = FALSE;
	input_dev->tail = 0;
	input_dev->count = 0;

	return OK;
}

/*
 * Copy input events to a reader.
 */
static ssize_t
input_copy_events(endpoint_t endpt, cp_grant_id_t grant,
	unsigned int event_count, struct input_dev *input_dev)
{
	int r, nbytes, wrap_left;
	size_t event_size = sizeof(*input_dev->eventbuf);

	if (input_dev->count < event_count)
		panic("input_copy_events: not enough input is ready");

	wrap_left = input_dev->tail + event_count - EVENTBUF_SIZE;
	nbytes = (wrap_left <= 0 ? event_count :
	    EVENTBUF_SIZE - input_dev->tail) * event_size;

	if ((r = sys_safecopyto(endpt, grant, 0,
	    (vir_bytes)(input_dev->eventbuf + input_dev->tail), nbytes)) != OK)
		return r;

	/* Copy possible remaining part if we wrap over. */
	if (wrap_left > 0 && (r = sys_safecopyto(endpt, grant, nbytes,
	    (vir_bytes) input_dev->eventbuf, wrap_left * event_size)) != OK)
		return r;

	input_dev->tail = (input_dev->tail + event_count) % EVENTBUF_SIZE;
	input_dev->count -= event_count;

	return event_size * event_count; /* bytes copied */
}

/*
 * Read from an input device.
 */
static ssize_t
input_read(devminor_t minor, u64_t UNUSED(position), endpoint_t endpt,
	cp_grant_id_t grant, size_t size, int flags, cdev_id_t id)
{
	unsigned int event_count;
	struct input_dev *input_dev;

	if ((input_dev = input_map(minor)) == NULL)
		return ENXIO;

	/* We cannot accept more than one pending read request at once. */
	if (!input_dev_active(input_dev) || input_dev->suspended)
		return EIO;

	/* The caller's buffer must have room for at least one whole event. */
	event_count = size / sizeof(*input_dev->eventbuf);
	if (event_count == 0)
		return EIO;

	/* No data available? Suspend the caller, unless we shouldn't block. */
	if (input_dev_buf_empty(input_dev)) {
		if (flags & CDEV_NONBLOCK)
			return EAGAIN;

		input_dev->suspended = TRUE;
		input_dev->caller = endpt;
		input_dev->grant = grant;
		input_dev->req_id = id;

		/* We should now wake up any selector, but that's lame.. */
		return EDONTREPLY;
	}

	if (event_count > input_dev->count)
		event_count = input_dev->count;

	return input_copy_events(endpt, grant, event_count, input_dev);
}

/*
 * Set keyboard LEDs on one or all keyboards.
 */
static void
input_set_leds(devminor_t minor, unsigned int mask)
{
	struct input_dev *dev;
	message m;
	int i, r;

	/* Prepare the request message */
	memset(&m, 0, sizeof(m));

	m.m_type = INPUT_SETLEDS;
	m.m_input_linputdriver_setleds.led_mask = mask;

	/*
	 * Send the request to all matching keyboard devices.  As side effect,
	 * this approach discards the request on mouse devices.
	 */
	for (i = FIRST_KBD_DEV; i <= LAST_KBD_DEV; i++) {
		dev = &devs[i];

		if (minor != KBDMUX_MINOR && minor != dev->minor)
			continue;

		/* Save the new state; the driver might (re)start later. */
		dev->leds = mask;

		if (dev->owner != NONE) {
			if ((r = asynsend3(dev->owner, &m, AMF_NOREPLY)) != OK)
				printf("INPUT: asynsend to %u failed (%d)\n",
				    dev->owner, r);
		}
	}
}

/*
 * Process an IOCTL request.
 */
static int
input_ioctl(devminor_t minor, unsigned long request, endpoint_t endpt,
	cp_grant_id_t grant, int flags, endpoint_t user_endpt, cdev_id_t id)
{
	struct input_dev *input_dev;
	kio_leds_t leds;
	unsigned int mask;
	int r;

	if ((input_dev = input_map(minor)) == NULL)
		return ENXIO;

	if (!input_dev_active(input_dev))
		return EIO;

	switch (request) {
	case KIOCSLEDS:
		if ((r = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) &leds,
		    sizeof(leds))) != OK)
			return r;

		mask = 0;
		if (leds.kl_bits & KBD_LEDS_NUM)
			mask |= (1 << INPUT_LED_NUMLOCK);
		if (leds.kl_bits & KBD_LEDS_CAPS)
			mask |= (1 << INPUT_LED_CAPSLOCK);
		if (leds.kl_bits & KBD_LEDS_SCROLL)
			mask |= (1 << INPUT_LED_SCROLLLOCK);

		input_set_leds(minor, mask);

		return OK;

	default:
		return ENOTTY;
	}
}

/*
 * Cancel a suspended read request.
 */
static int
input_cancel(devminor_t minor, endpoint_t endpt, cdev_id_t id)
{
	struct input_dev *input_dev;

	if ((input_dev = input_map(minor)) == NULL)
		return ENXIO;

	if (input_dev->suspended && input_dev->caller == endpt &&
	    input_dev->req_id == id) {
		input_dev->suspended = FALSE;

		return EINTR;
	}

	return EDONTREPLY;
}

/*
 * Perform a select call on an input device.
 */
static int
input_select(devminor_t minor, unsigned int ops, endpoint_t endpt)
{
	struct input_dev *input_dev;
	int ready_ops;

	if ((input_dev = input_map(minor)) == NULL)
		return ENXIO;

	ready_ops = 0;

	if (ops & CDEV_OP_RD) {
		if (!input_dev_active(input_dev) || input_dev->suspended)
			ready_ops |= CDEV_OP_RD;	/* immediate error */
		else if (!input_dev_buf_empty(input_dev))
			ready_ops |= CDEV_OP_RD;	/* data available */
		else if (ops & CDEV_NOTIFY)
			input_dev->selector = endpt;	/* report later */
	}

	if (ops & CDEV_OP_WR) ready_ops |= CDEV_OP_WR;	/* immediate error */

	return ready_ops;
}

/*
 * An input device receives an input event.  Enqueue it, and possibly unsuspend
 * a read request or wake up a selector.
 */
static void
input_process(struct input_dev *input_dev, const message *m)
{
	unsigned int next;
	int r;

	if (input_dev_buf_full(input_dev)) {
		/* Overflow.  Overwrite the oldest event. */
		input_dev->tail = (input_dev->tail + 1) % EVENTBUF_SIZE;
		input_dev->count--;

#if INPUT_DEBUG
		printf("INPUT: overflow on device %u\n", input_dev - devs);
#endif
	}
	next = (input_dev->tail + input_dev->count) % EVENTBUF_SIZE;
	input_dev->eventbuf[next].page = m->m_linputdriver_input_event.page;
	input_dev->eventbuf[next].code = m->m_linputdriver_input_event.code;
	input_dev->eventbuf[next].value = m->m_linputdriver_input_event.value;
	input_dev->eventbuf[next].flags = m->m_linputdriver_input_event.flags;
	input_dev->eventbuf[next].devid = m->m_linputdriver_input_event.id;
	input_dev->eventbuf[next].rsvd[0] = 0;
	input_dev->eventbuf[next].rsvd[1] = 0;
	input_dev->count++;

	/*
	 * There is new input.  Revive a suspended reader if there was one.
	 * Otherwise see if we should reply to a select query.
	 */
	if (input_dev->suspended) {
		r = input_copy_events(input_dev->caller, input_dev->grant, 1,
		    input_dev);
		chardriver_reply_task(input_dev->caller, input_dev->req_id, r);
		input_dev->suspended = FALSE;
	} else if (input_dev->selector != NONE) {
		chardriver_reply_select(input_dev->selector, input_dev->minor,
		    CDEV_OP_RD);
		input_dev->selector = NONE;
	}
}

/*
 * An input event has arrived from a driver.
 */
static void
input_event(message *m)
{
	struct input_dev *input_dev, *mux_dev;
	int r, id;

	/* Unlike minor numbers, device IDs are in fact array indices. */
	id = m->m_linputdriver_input_event.id;
	if (id < 0 || id >= INPUT_DEV_MAX)
		return;

	/* The sender must owner the device. */
	input_dev = &devs[id];
	if (input_dev->owner != m->m_source)
		return;

	/* Input events are also delivered to the respective multiplexer. */
	if (input_dev->minor >= KBD0_MINOR &&
	    input_dev->minor < KBD0_MINOR + KBD_MINORS)
		mux_dev = &devs[KBDMUX_DEV];
	else
		mux_dev = &devs[MOUSEMUX_DEV];

	/*
	 * Try to deliver the event to the input device or otherwise the
	 * corresponding multiplexer.  If neither are opened, forward the event
	 * to TTY.
	 */
	if (input_dev->opened)
		input_process(input_dev, m);
	else if (mux_dev->opened)
		input_process(mux_dev, m);
	else {
		message fwd;
		mess_input_tty_event *tty_event = &(fwd.m_input_tty_event);

		fwd.m_type = TTY_INPUT_EVENT;
		tty_event->id = m->m_linputdriver_input_event.id;
		tty_event->page = m->m_linputdriver_input_event.page;
		tty_event->code = m->m_linputdriver_input_event.code;
		tty_event->value = m->m_linputdriver_input_event.value;
		tty_event->flags = m->m_linputdriver_input_event.flags;

		if ((r = ipc_send(TTY_PROC_NR, &fwd)) != OK)
			printf("INPUT: send to TTY failed (%d)\n", r);
	}
}

/*
 * Allocate a device structure for an input driver of the given type, and
 * return its ID.  If the given label already owns a device ID of the right
 * type, update that entry instead.  If no device ID could be allocated, return
 * INVALID_INPUT_ID.
 */
static int
input_alloc_id(int mouse, endpoint_t owner, const char *label)
{
	int n, id, start, end;

	if (!mouse) {
		start = FIRST_KBD_DEV;
		end = LAST_KBD_DEV;
	} else {
		start = FIRST_MOUSE_DEV;
		end = LAST_MOUSE_DEV;
	}

	id = INVALID_INPUT_ID;
	for (n = start; n <= end; n++) {
		if (devs[n].owner != NONE) {
			if (!strcmp(devs[n].label, label)) {
				devs[n].owner = owner;
				return n;
			}
		/* Do not allocate the ID of a disconnected but open device. */
		} else if (!devs[n].opened && id == INVALID_INPUT_ID) {
			id = n;
		}
	}

	if (id != INVALID_INPUT_ID) {
		devs[id].owner = owner;
		strlcpy(devs[id].label, label, sizeof(devs[id].label));

#if INPUT_DEBUG
		printf("INPUT: connected device %u to %u (%s)\n", id,
		    owner, label);
#endif
	} else {
		printf("INPUT: out of %s slots for new driver %d\n",
		    mouse ? "mouse" : "keyboard", owner);
	}

	return id;
}

/*
 * Register keyboard and/or a mouse devices for a driver.
 */
static void
input_connect(endpoint_t owner, char *labelp, int typemask)
{
	message m;
	char label[DS_MAX_KEYLEN];
	int r, kbd_id, mouse_id;

#if INPUT_DEBUG
	printf("INPUT: connect request from %u (%s) for mask %x\n", owner,
	    labelp, typemask);
#endif

	/* Check the driver's label. */
	if ((r = ds_retrieve_label_name(label, owner)) != OK) {
		printf("INPUT: unable to get label for %u: %d\n", owner, r);
		return;
	}
	if (strcmp(label, labelp)) {
		printf("INPUT: ignoring driver %s label %s\n", label, labelp);
		return;
	}

	kbd_id = INVALID_INPUT_ID;
	mouse_id = INVALID_INPUT_ID;

	/*
	 * We ignore allocation failures here, thus possibly sending invalid
	 * IDs to the driver even for either or both the devices types it
	 * requested.  As a result, the driver will not send us input for these
	 * device types, possibly effectively disabling the driver altogether.
	 * Theoretically we could still admit events to the multiplexers for
	 * such drivers, but that would lead to unexpected behavior with
	 * respect to keyboard LEDs, for example.
	 */
	if (typemask & INPUT_DEV_KBD)
		kbd_id = input_alloc_id(FALSE /*mouse*/, owner, label);
	if (typemask & INPUT_DEV_MOUSE)
		mouse_id = input_alloc_id(TRUE /*mouse*/, owner, label);

	memset(&m, 0, sizeof(m));

	m.m_type = INPUT_CONF;
	m.m_input_linputdriver_input_conf.kbd_id = kbd_id;
	m.m_input_linputdriver_input_conf.mouse_id = mouse_id;
	m.m_input_linputdriver_input_conf.rsvd1_id = INVALID_INPUT_ID;	/* reserved (joystick?) */
	m.m_input_linputdriver_input_conf.rsvd2_id = INVALID_INPUT_ID;	/* reserved for future use */

	if ((r = asynsend3(owner, &m, AMF_NOREPLY)) != OK)
		printf("INPUT: asynsend to %u failed (%d)\n", owner, r);

	/* If a keyboard was registered, also set its initial LED state. */
	if (kbd_id != INVALID_INPUT_ID)
		input_set_leds(devs[kbd_id].minor, devs[kbd_id].leds);
}

/*
 * Disconnect a device.
 */
static void
input_disconnect(struct input_dev *input_dev)
{
#if INPUT_DEBUG
	printf("INPUT: disconnected device %u\n", input_dev - devs);
#endif

	if (input_dev->suspended) {
		chardriver_reply_task(input_dev->caller, input_dev->req_id,
		    EIO);
		input_dev->suspended = FALSE;
	}

	if (input_dev->selector != NONE) {
		chardriver_reply_select(input_dev->selector, input_dev->minor,
		    CDEV_OP_RD);
		input_dev->selector = NONE;
	}

	input_dev->owner = NONE;
}

/*
 * Check for driver status changes in the data store.
 */
static void
input_check(void)
{
	char key[DS_MAX_KEYLEN], *label;
	const char *driver_prefix = "drv.inp.";
	u32_t value;
	size_t len;
	int i, r, type;
	endpoint_t owner;

	len = strlen(driver_prefix);

	/* Check for new (input driver) entries. */
	while (ds_check(key, &type, &owner) == OK) {
		if ((r = ds_retrieve_u32(key, &value)) != OK) {
			printf("INPUT: ds_retrieve_u32 failed (%d)\n", r);
			continue;
		}

		/* Only check for input driver registration events. */
		if (strncmp(key, driver_prefix, len))
			continue;

		/* The prefix is followed by the driver's own label. */
		label = &key[len];

		input_connect(owner, label, value);
	}

	/* Check for removed (label) entries. */
	for (i = 0; i < INPUT_DEV_MAX; i++) {
		/* This also skips the multiplexers. */
		if (devs[i].owner == NONE)
			continue;

		r = ds_retrieve_label_endpt(devs[i].label, &owner);

		if (r == OK)
			devs[i].owner = owner;	/* not really necessary */
		else if (r == ESRCH)
			input_disconnect(&devs[i]);
		else
			printf("INPUT: ds_retrieve_label_endpt failed (%d)\n",
			    r);
	}
}

/*
 * Process messages not part of the character driver protocol.
 */
static void
input_other(message *m, int ipc_status)
{
	if (is_ipc_notify(ipc_status)) {
		switch (m->m_source) {
		case DS_PROC_NR:
			input_check();
			break;
		default:
			printf("INPUT: unexpected notify from %d\n",
			    m->m_source);
		}
		return;
	}

	/* An input event from a registered driver. */
	switch (m->m_type) {
	case INPUT_EVENT:
		input_event(m);

		break;

	case INPUT_SETLEDS:
		if (m->m_source == TTY_PROC_NR) {
			input_set_leds(KBDMUX_MINOR, m->m_input_linputdriver_setleds.led_mask);

			break;
		}
		/* FALLTHROUGH */
	default:
		printf("INPUT: unexpected message %d from %d\n",
		    m->m_type, m->m_source);
	}
}

/*
 * Initialize the input server.
 */
static int
input_init(int UNUSED(type), sef_init_info_t *UNUSED(info))
{
	message m;
	int i, r;

	/* Initialize input device structures. */
	for (i = 0; i < INPUT_DEV_MAX; i++) {
		devs[i].minor = input_revmap(i);
		devs[i].owner = NONE;
		devs[i].tail = 0;
		devs[i].count = 0;
		devs[i].opened = FALSE;
		devs[i].suspended = FALSE;
		devs[i].selector = NONE;
		devs[i].leds = 0;
	}

	/* Subscribe to driver registration events for input drivers. */
	if ((r = ds_subscribe("drv\\.inp\\..*", DSF_INITIAL)) != OK)
		panic("INPUT: can't subscribe to driver events (%d)", r);

	/* Announce our presence to VFS. */
	chardriver_announce();

	/* Announce our presence to TTY. */
	memset(&m, 0, sizeof(m));

	m.m_type = TTY_INPUT_UP;

	if ((r = ipc_send(TTY_PROC_NR, &m)) != OK)
		printf("INPUT: send to TTY failed (%d)\n", r);

	return OK;
}

/*
 * Set callbacks and invoke SEF startup.
 */
static void
input_startup(void)
{
	sef_setcb_init_fresh(input_init);

	sef_startup();
}

/*
 * Main program of the input server.
 */
int
main(void)
{
	input_startup();

	chardriver_task(&input_tab);

	return 0;
}