Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
/*	$NetBSD: ms.c,v 1.43 2022/08/01 01:32:15 rin Exp $ */

/*
 * based on:
 *
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This software was developed by the Computer Systems Engineering group
 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
 * contributed to Berkeley.
 *
 * All advertising materials mentioning features or use of this software
 * must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Lawrence Berkeley Laboratory.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)ms.c	8.1 (Berkeley) 6/11/93
 *
 * Header: ms.c,v 1.5 92/11/26 01:28:47 torek Exp  (LBL)
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ms.c,v 1.43 2022/08/01 01:32:15 rin Exp $");

/*
 * Mouse driver.
 *
 * wscons aware. Attaches two wsmouse devices, one for each port.
 * Also still exports its own device entry points so it is possible
 * to open this and read firm_events.
 * The events go only to one place at a time:
 * - When somebody has opened a ms device directly wsmouse cannot be activated.
 *   (when wsmouse is opened it calls ms_enable to activate)
 * - When feeding events to wsmouse open of ms device will fail.
 */

#include "wsmouse.h"

#include <sys/param.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/kernel.h>
#include <sys/proc.h>
#include <sys/syslog.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/tty.h>
#include <sys/signalvar.h>
#include <sys/conf.h>

#include <amiga/dev/event_var.h>
#include <amiga/dev/vuid_event.h>

#include <amiga/amiga/custom.h>
#include <amiga/amiga/cia.h>
#include <amiga/amiga/device.h>

#if NWSMOUSE > 0
#include <dev/wscons/wsmousevar.h>
#include <dev/wscons/wsconsio.h>
#endif

void msattach(device_t, device_t, void *);
int msmatch(device_t, cfdata_t, void *);

/* per-port state */
struct ms_port {
	int	ms_portno;	   /* which hardware port, for msintr() */

	struct callout ms_intr_ch;

	u_char	ms_horc;	   /* horizontal counter on last scan */
  	u_char	ms_verc;	   /* vertical counter on last scan */
	char	ms_mb;		   /* mouse button state */
	char	ms_ub;		   /* user button state */
	int	ms_dx;		   /* delta-x */
	int	ms_dy;		   /* delta-y */
	volatile int ms_ready;	   /* event queue is ready */
	struct	evvar ms_events;   /* event queue state */
#if NWSMOUSE > 0
	device_t ms_wsmousedev; /* wsmouse device */
	int     ms_wsenabled;      /* feeding events to wscons */
#endif
};

#define	MS_NPORTS	2

struct ms_softc {
	struct ms_port sc_ports[MS_NPORTS];
};

CFATTACH_DECL_NEW(ms, sizeof(struct ms_softc),
    msmatch, msattach, NULL, NULL);

void msintr(void *);
void ms_enable(struct ms_port *);
void ms_disable(struct ms_port *);

extern struct cfdriver ms_cd;

dev_type_open(msopen);
dev_type_close(msclose);
dev_type_read(msread);
dev_type_ioctl(msioctl);
dev_type_poll(mspoll);
dev_type_kqfilter(mskqfilter);

const struct cdevsw ms_cdevsw = {
	.d_open = msopen,
	.d_close = msclose,
	.d_read = msread,
	.d_write = nowrite,
	.d_ioctl = msioctl,
	.d_stop = nostop,
	.d_tty = notty,
	.d_poll = mspoll,
	.d_mmap = nommap,
	.d_kqfilter = mskqfilter,
	.d_discard = nodiscard,
	.d_flag = 0
};

#define	MS_UNIT(d)	((minor(d) & ~0x1) >> 1)
#define	MS_PORT(d)	(minor(d) & 0x1)

/*
 * Given a dev_t, return a pointer to the port's hardware state.
 * Assumes the unit to be valid, so do *not* use this in msopen().
 */
#define	MS_DEV2MSPORT(d) \
    (&(((struct ms_softc *)getsoftc(ms_cd, MS_UNIT(d)))->sc_ports[MS_PORT(d)]))

#if NWSMOUSE > 0
/*
 * Callbacks for wscons.
 */
static int ms_wscons_enable(void *);
static int ms_wscons_ioctl(void *, u_long, void *, int, struct lwp *);
static void ms_wscons_disable(void *);

static struct wsmouse_accessops ms_wscons_accessops = {
	ms_wscons_enable,
	ms_wscons_ioctl,
	ms_wscons_disable
};
#endif

int
msmatch(device_t parent, cfdata_t cf, void *aux)
{
	static int ms_matched = 0;

	/* Allow only one instance. */
	if (!matchname((char *)aux, "ms") || ms_matched)
		return 0;

	ms_matched = 1;
	return 1;
}

void
msattach(device_t parent, device_t self, void *aux)
{
#if NWSMOUSE > 0
	struct wsmousedev_attach_args waa;
#endif
	struct ms_softc *sc = device_private(self);
	int i;

	printf("\n");
	for (i = 0; i < MS_NPORTS; i++) {
		sc->sc_ports[i].ms_portno = i;
		callout_init(&sc->sc_ports[i].ms_intr_ch, 0);
#if NWSMOUSE > 0
		waa.accessops = &ms_wscons_accessops;
		waa.accesscookie = &sc->sc_ports[i];
		
		sc->sc_ports[i].ms_wsenabled = 0;
		sc->sc_ports[i].ms_wsmousedev = 
		    config_found(self, &waa, wsmousedevprint,
			CFARGS(.iattr = "wsmousedev"));
#endif
	}
}

/*
 * Amiga mice are hooked up to one of the two "game" ports, where
 * the main mouse is usually on the first port, and port 2 can
 * be used by a joystick. Nevertheless, we support two mouse
 * devices, /dev/mouse0 and /dev/mouse1 (with a link of /dev/mouse to
 * the device that represents the port of the mouse in use).
 */

/*
 * enable scanner, called when someone opens the port.
 */
void
ms_enable(struct ms_port *ms)
{

	/*
	 * use this as flag to the "interrupt" to tell it when to
	 * shut off (when it's reset to 0).
	 */
	ms->ms_ready = 1;

	callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
}

/*
 * disable scanner. Just set ms_ready to 0, and after the next
 * timeout taken, no further timeouts will be initiated.
 */
void
ms_disable(struct ms_port *ms)
{
	int s;

	s = splhigh ();
	ms->ms_ready = 0;
	/*
	 * sync with the interrupt
	 */
	tsleep(ms, PZERO - 1, "mouse-disable", 0);
	splx(s);
}


/*
 * we're emulating a mousesystems serial mouse here..
 */
void
msintr(void *arg)
{
	static const char to_one[] = { 1, 2, 2, 4, 4, 4, 4 };
	static const int to_id[] = { MS_RIGHT, MS_MIDDLE, 0, MS_LEFT };
	struct ms_port *ms = arg;
	struct firm_event *fe;
	int mb, ub, d, get, put, any, port;
	u_char pra, *horc, *verc;
	u_short pot, count;
	short dx, dy;

	port = ms->ms_portno;

	horc = ((u_char *) &count) + 1;
	verc = (u_char *) &count;

	/*
	 * first read the three buttons.
	 */
	pot  = custom.potgor;
	pra  = ciaa.pra;
	pot >>= port == 0 ? 8 : 12;	/* contains right and middle button */
	pra >>= port == 0 ? 6 : 7;	/* contains left button */
	mb = (pot & 4) / 4 + (pot & 1) * 2 + (pra & 1) * 4;
	mb ^= 0x07;

	/*
	 * read current values of counter registers
	 */
	if (port == 0)
		count = custom.joy0dat;
	else
		count = custom.joy1dat;

	/*
	 * take care of wraparound
	 */
	dx = *horc - ms->ms_horc;
	if (dx < -127)
		dx += 255;
	else if (dx > 127)
		dx -= 255;
	dy = *verc - ms->ms_verc;
	if (dy < -127)
		dy += 255;
	else if (dy > 127)
		dy -= 255;

	/*
	 * remember current values for next scan
	 */
	ms->ms_horc = *horc;
	ms->ms_verc = *verc;

	ms->ms_dx = dx;
	ms->ms_dy = dy;
	ms->ms_mb = mb;

#if NWSMOUSE > 0
	/*
	 * If we have attached wsmouse and we are not opened
	 * directly then pass events to wscons.
	 */
	if (ms->ms_wsmousedev && ms->ms_wsenabled)
	{
		int buttons = 0;

		if (mb & 4)
			buttons |= 1;
		if (mb & 2)
			buttons |= 2;
		if (mb & 1)
			buttons |= 4;

		wsmouse_input(ms->ms_wsmousedev, 
			      buttons,
			      dx, -dy, 0, 0,
			      WSMOUSE_INPUT_DELTA);

	} else
#endif
	if (dx || dy || ms->ms_ub != ms->ms_mb) {
		/*
		 * We have at least one event (mouse button, delta-X, or
		 * delta-Y; possibly all three, and possibly three separate
		 * button events).  Deliver these events until we are out of
		 * changes or out of room.  As events get delivered, mark them
		 * `unchanged'.
		 */
		any = 0;
		get = ms->ms_events.ev_get;
		put = ms->ms_events.ev_put;
		fe = &ms->ms_events.ev_q[put];

		mb = ms->ms_mb;
		ub = ms->ms_ub;
		while ((d = mb ^ ub) != 0) {
			/*
			 * Mouse button change.  Convert up to three changes
			 * to the `first' change, and drop it into the event
			 * queue.
			 */
			if ((++put) % EV_QSIZE == get) {
				put--;
				goto out;
			}

			d = to_one[d - 1];	/* from 1..7 to {1,2,4} */
			fe->id = to_id[d - 1];	/* from {1,2,4} to ID */
			fe->value = mb & d ? VKEY_DOWN : VKEY_UP;
			firm_gettime(fe);
			fe++;

			if (put >= EV_QSIZE) {
				put = 0;
				fe = &ms->ms_events.ev_q[0];
			}
			any = 1;

			ub ^= d;
		}
		if (ms->ms_dx) {
			if ((++put) % EV_QSIZE == get) {
				put--;
				goto out;
			}

			fe->id = LOC_X_DELTA;
			fe->value = ms->ms_dx;
			firm_gettime(fe);
			fe++;

			if (put >= EV_QSIZE) {
				put = 0;
				fe = &ms->ms_events.ev_q[0];
			}
			any = 1;

			ms->ms_dx = 0;
		}
		if (ms->ms_dy) {
			if ((++put) % EV_QSIZE == get) {
				put--;
				goto out;
			}

			fe->id = LOC_Y_DELTA;
			fe->value = ms->ms_dy;
			firm_gettime(fe);
			fe++;

			if (put >= EV_QSIZE) {
				put = 0;
				fe = &ms->ms_events.ev_q[0];
			}
			any = 1;

			ms->ms_dy = 0;
		}
out:
		if (any) {
			ms->ms_ub = ub;
			ms->ms_events.ev_put = put;
			EV_WAKEUP(&ms->ms_events);
		}
	}

	/*
	 * reschedule handler, or if terminating,
	 * handshake with ms_disable
	 */
	if (ms->ms_ready)
		callout_reset(&ms->ms_intr_ch, 2, msintr, ms);
	else
		wakeup(ms);
}

int
msopen(dev_t dev, int flags, int mode, struct lwp *l)
{
	struct ms_softc *sc;
	struct ms_port *ms;
	int unit, port;

	unit = MS_UNIT(dev);
	sc = (struct ms_softc *)getsoftc(ms_cd, unit);

	if (sc == NULL)
		return(EXDEV);

	port = MS_PORT(dev);
	ms = &sc->sc_ports[port];

	if (ms->ms_events.ev_io)
		return(EBUSY);

#if NWSMOUSE > 0
	/* don't allow opening when sending events to wsmouse */
	if (ms->ms_wsenabled)
		return EBUSY;
#endif
	/* initialize potgo bits for mouse mode */
	custom.potgo = custom.potgor | (0xf00 << (port * 4));

	ms->ms_events.ev_io = l->l_proc;
	ev_init(&ms->ms_events);	/* may cause sleep */
	ms_enable(ms);
	return(0);
}

int
msclose(dev_t dev, int flags, int mode, struct lwp *l)
{
	struct ms_port *ms;

	ms = MS_DEV2MSPORT(dev);

	ms_disable(ms);
	ev_fini(&ms->ms_events);
	ms->ms_events.ev_io = NULL;
	return(0);
}

int
msread(dev_t dev, struct uio *uio, int flags)
{
	struct ms_port *ms;

	ms = MS_DEV2MSPORT(dev);

	return(ev_read(&ms->ms_events, uio, flags));
}

int
msioctl(dev_t dev, u_long cmd, register void *data, int flag,
        struct lwp *l)
{
	struct ms_port *ms;

	ms = MS_DEV2MSPORT(dev);

	switch (cmd) {
	case FIONBIO:		/* we will remove this someday (soon???) */
		return(0);
	case FIOASYNC:
		ms->ms_events.ev_async = *(int *)data != 0;
		return(0);
	case FIOSETOWN:
		if (-*(int *)data != ms->ms_events.ev_io->p_pgid
		    && *(int *)data != ms->ms_events.ev_io->p_pid)
			return(EPERM);
		return(0);
	case TIOCSPGRP:
		if (*(int *)data != ms->ms_events.ev_io->p_pgid)
			return(EPERM);
		return(0);
	case VUIDGFORMAT:	/* we only do firm_events */
		*(int *)data = VUID_FIRM_EVENT;
		return(0);
	case VUIDSFORMAT:
		if (*(int *)data != VUID_FIRM_EVENT)
			return(EINVAL);
		return(0);
	}
	return(ENOTTY);
}

int
mspoll(dev_t dev, int events, struct lwp *l)
{
	struct ms_port *ms;

	ms = MS_DEV2MSPORT(dev);

	return(ev_poll(&ms->ms_events, events, l));
}

int
mskqfilter(dev_t dev, struct knote *kn)
{
	struct ms_port *ms;

	ms = MS_DEV2MSPORT(dev);

	return (ev_kqfilter(&ms->ms_events, kn));
}

#if NWSMOUSE > 0

static int
ms_wscons_ioctl(void *cookie, u_long cmd, void *data, int flag, 
		struct lwp *l)
{
	switch(cmd) {
	case WSMOUSEIO_GTYPE:
		*(u_int*)data = WSMOUSE_TYPE_AMIGA;
		return (0);
	}

	return EPASSTHROUGH;
}

static int
ms_wscons_enable(void *cookie)
{
	struct ms_port *port = cookie;

	/* somebody reading events from us directly? */
	if (port->ms_events.ev_io)
		return EBUSY;

	port->ms_wsenabled = 1;
	ms_enable(port);

	return 0;
}

static void
ms_wscons_disable(void *cookie)
{
	struct ms_port *port = cookie;

	if (port->ms_wsenabled)
		ms_disable(port);
	port->ms_wsenabled = 0;
}

#endif