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
/*	$NetBSD: dt.c,v 1.12 2015/06/28 09:15:45 maxv Exp $	*/

/*-
 * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Doran.
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
 */

/*-
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Ralph Campbell and Rick Macklem.
 *
 * 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.
 *
 *	@(#)dtop.c	8.2 (Berkeley) 11/30/93
 */

/*
 * 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 the
 * rights to redistribute these changes.
 */
/*
 * 	Author: Alessandro Forin, Carnegie Mellon University
 *
 *	Hardware-level operations for the Desktop serial line
 *	bus (i2c aka ACCESS).
 */
/************************************************************
Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
and the Massachusetts Institute of Technology, Cambridge, Massachusetts.

                        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 appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Digital or MIT not be
used in advertising or publicity pertaining to distribution of the
software without specific, written prior permission.

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

********************************************************/

/*
 * ACCESS.bus device support for the Personal DECstation.  This code handles
 * only the keyboard and mouse, and will likely not work if other ACCESS.bus
 * devices are physically attached to the system.
 *
 * Since we do not know how to drive the hardware (the only reference being
 * Mach), we can't identify which devices are connected to the system by
 * sending idenfication requests.  With only a mouse and keyboard attached
 * to the system, we do know which two slave addresses will be in use. 
 * However, we don't know which is the mouse, and which is the keyboard. 
 * So, we resort to inspecting device reports and making an educated guess
 * as to which is which.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dt.c,v 1.12 2015/06/28 09:15:45 maxv Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/tty.h>
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/malloc.h>
#include <sys/intr.h>

#include <dev/dec/lk201.h>

#include <dev/tc/tcvar.h>
#include <dev/tc/ioasicreg.h>
#include <dev/tc/ioasicvar.h>

#include <pmax/pmax/maxine.h>

#include <pmax/tc/dtreg.h>
#include <pmax/tc/dtvar.h>

#define	DT_BUF_CNT		16
#define	DT_ESC_CHAR		0xf8
#define	DT_XMT_OK		0xfb
#define	DT_MAX_POLL		0x70000		/* about half a sec */

#define	DT_GET_BYTE(data)	(((*(data)) >> 8) & 0xff)
#define	DT_PUT_BYTE(data,c)	{ *(data) = (c) << 8; wbflush(); }

#define	DT_RX_AVAIL(poll)	((*(poll) & 1) != 0)
#define	DT_TX_AVAIL(poll)	((*(poll) & 2) != 0)

int	dt_match(device_t, cfdata_t, void *);
void	dt_attach(device_t, device_t, void *);
int	dt_intr(void *);
int	dt_print(void *, const char *);
void	dt_strvis(uint8_t *, char *, int);
void	dt_dispatch(void *);

int	dt_kbd_addr = DT_ADDR_KBD;
struct	dt_device dt_kbd_dv;
int	dt_ms_addr = DT_ADDR_MOUSE;
struct	dt_device dt_ms_dv;
struct	dt_state dt_state;

CFATTACH_DECL_NEW(dt, sizeof(struct dt_softc),
    dt_match, dt_attach, NULL, NULL);

int
dt_match(device_t parent, cfdata_t cf, void *aux)
{
	struct ioasicdev_attach_args *d;

	d = aux;

	if (strcmp(d->iada_modname, "dtop") != 0)
		return (0);

	if (badaddr((void *)(d->iada_addr), 2))
		return (0);

	return (1);
}

void
dt_attach(device_t parent, device_t self, void *aux)
{
	struct ioasicdev_attach_args *d;
	struct dt_attach_args dta;
	struct dt_softc *sc;
	struct dt_msg *msg;
	int i;

	d = aux;
	sc = device_private(self);
	sc->sc_dev = self;

	dt_cninit();

	msg = malloc(sizeof(*msg) * DT_BUF_CNT, M_DEVBUF, M_NOWAIT);
	if (msg == NULL) {
		printf("%s: memory exhausted\n", device_xname(self));
		return;
	}

	sc->sc_sih = softint_establish(SOFTINT_SERIAL, dt_dispatch, sc);
	if (sc->sc_sih == NULL) {
		printf("%s: memory exhausted\n", device_xname(self));
		free(msg, M_DEVBUF);
		return;
	}

	SIMPLEQ_INIT(&sc->sc_queue);
	SLIST_INIT(&sc->sc_free);
	for (i = 0; i < DT_BUF_CNT; i++, msg++)
		SLIST_INSERT_HEAD(&sc->sc_free, msg, chain.slist);

	ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_TTY, dt_intr, sc);
	printf("\n");

	dta.dta_addr = DT_ADDR_KBD;
	config_found(self, &dta, dt_print);
	dta.dta_addr = DT_ADDR_MOUSE;
	config_found(self, &dta, dt_print);
}

void
dt_cninit(void)
{

	dt_state.ds_poll = (volatile u_int *)
	    MIPS_PHYS_TO_KSEG1(XINE_REG_INTR);
	dt_state.ds_data = (volatile u_int *)
	    MIPS_PHYS_TO_KSEG1(XINE_PHYS_TC_3_START + 0x280000);
}

int
dt_print(void *aux, const char *pnp)
{

	return (QUIET);
}

int
dt_establish_handler(struct dt_softc *sc, struct dt_device *dtdv,
    void *arg, void (*hdlr)(void *, struct dt_msg *))
{

	dtdv->dtdv_arg = arg;
	dtdv->dtdv_handler = hdlr;
	return (0);
}

int
dt_intr(void *cookie)
{
	struct dt_softc *sc;
	struct dt_msg *msg, *pend;

	sc = cookie;

	switch (dt_msg_get(&sc->sc_msg, 1)) {
	case DT_GET_ERROR:
		/*
		 * Ugh! The most common occurrence of a data overrun is upon
		 * a key press and the result is a software generated "stuck
		 * key".  All I can think to do is fake an "all keys up"
		 * whenever a data overrun occurs.
		 */
		sc->sc_msg.src = dt_kbd_addr;
		sc->sc_msg.ctl = DT_CTL(1, 0, 0);
		sc->sc_msg.body[0] = DT_KBD_EMPTY;
#ifdef DIAGNOSTIC
		printf("%s: data overrun or stray interrupt\n",
		    device_xname(sc->sc_dev));
#endif
		break;

	case DT_GET_DONE:
		break;

	case DT_GET_NOTYET:
		return (1);
	}

	if ((msg = SLIST_FIRST(&sc->sc_free)) == NULL) {
		printf("%s: input overflow\n", device_xname(sc->sc_dev));
		return (1);
	}
	SLIST_REMOVE_HEAD(&sc->sc_free, chain.slist);
	memcpy(msg, &sc->sc_msg, sizeof(*msg));

	pend = SIMPLEQ_FIRST(&sc->sc_queue);
	SIMPLEQ_INSERT_TAIL(&sc->sc_queue, msg, chain.simpleq);
	if (pend == NULL)
		softint_schedule(sc->sc_sih);

	return (1);
}

void
dt_dispatch(void *cookie)
{
	struct dt_softc *sc;
	struct dt_msg *msg;
	int s;
	struct dt_device *dtdv;

	sc = cookie;
	msg = NULL;

	for (;;) {
		s = spltty();
		if (msg != NULL)
			SLIST_INSERT_HEAD(&sc->sc_free, msg, chain.slist);
		msg = SIMPLEQ_FIRST(&sc->sc_queue);
		if (msg != NULL)
			SIMPLEQ_REMOVE_HEAD(&sc->sc_queue, chain.simpleq);
		splx(s);
		if (msg == NULL)
			break;

		if (msg->src != DT_ADDR_MOUSE && msg->src != DT_ADDR_KBD) {
			printf("%s: message from unknown dev 0x%x\n",
			    device_xname(sc->sc_dev), sc->sc_msg.src);
			dt_msg_dump(msg);
			continue;
		}
		if (DT_CTL_P(msg->ctl) != 0) {
			printf("%s: received control message\n",
			    device_xname(sc->sc_dev));
			dt_msg_dump(msg);
			continue;
		}

		/*
		 * 1. Mouse should have no more than eight buttons, so first
		 *    8 bits of body will be zero.
		 * 2. Mouse should always send full locator report.
		 *    Note:  my mouse does not send 'z' data, so the size
		 *    did not match the size of struct dt_locator_msg - mhitch
		 * 3. Keyboard should never report all-up (0x00) in
		 *    a packet with size > 1.
		 */
		if (DT_CTL_LEN(msg->ctl) >= 6 &&
		    msg->body[0] == 0 && msg->src != dt_ms_addr) {
			dt_kbd_addr = dt_ms_addr;
			dt_ms_addr = msg->src;
		} else if (DT_CTL_LEN(msg->ctl) < 6 && msg->body[0] != 0 &&
		    msg->src != dt_kbd_addr) {
			dt_ms_addr = dt_kbd_addr;
			dt_kbd_addr = msg->src;
		}

		if (msg->src == dt_kbd_addr)
			dtdv = &dt_kbd_dv;
		else
			dtdv = &dt_ms_dv;

		if (dtdv->dtdv_handler != NULL)
			(*dtdv->dtdv_handler)(dtdv->dtdv_arg, msg);
	}
}

int
dt_msg_get(struct dt_msg *msg, int intr)
{
	volatile u_int *poll, *data;
	uint8_t c;
	int max_polls;

	poll = dt_state.ds_poll;
	data = dt_state.ds_data;

	/*
	 * The interface does not hand us the first byte, which is our
	 * address and cannot ever be anything else but 0x50.
	 */
	if (dt_state.ds_state == 0) {
		dt_state.ds_escaped = 0;
		dt_state.ds_ptr = 0;
	}

	for (;;) {
		max_polls = DT_MAX_POLL;

		while (!DT_RX_AVAIL(poll)) {
			if (intr)
				return (DT_GET_NOTYET);
			if (max_polls-- <= 0)
				break;
			DELAY(1);
		}

		if (max_polls <= 0) {
			if (dt_state.ds_state != 0) {
				dt_state.ds_bad_pkts++;
				dt_state.ds_state = 0;
			}
			return (DT_GET_ERROR);
		}

		c = DT_GET_BYTE(data);

		if (dt_state.ds_escaped) {
			switch (c) {
			case 0xe8:
			case 0xe9:
			case 0xea:
			case 0xeb:
				c += 0x10;
				break;
			}
			if (c == 'O') {
				dt_state.ds_bad_pkts++;
				dt_state.ds_state = 0;
				return (DT_GET_ERROR);
			}
			dt_state.ds_escaped = 0;
		} else if (c == DT_ESC_CHAR) {
			dt_state.ds_escaped = 1;
			continue;
		}

		if (dt_state.ds_state == 0) {
			msg->src = c;
			dt_state.ds_state = 1;
		} else if (dt_state.ds_state == 1) {
			msg->ctl = c;
			dt_state.ds_state = 2;
			dt_state.ds_len = DT_CTL_LEN(msg->ctl) + 1;
			if (dt_state.ds_len > sizeof(msg->body))
				printf("dt_msg_get: msg truncated: %d\n",
				    dt_state.ds_len);
		} else /* if (dt_state.ds_state == 2) */ {
			if (dt_state.ds_ptr < sizeof(msg->body))
				msg->body[dt_state.ds_ptr++] = c;
			if (dt_state.ds_ptr >= dt_state.ds_len)
				break;
		}
	}

	msg->dst = DT_ADDR_HOST;
	dt_state.ds_state = 0;
	return (DT_GET_DONE);
}

void
dt_msg_dump(struct dt_msg *msg)
{
	int i, l;

	l = DT_CTL_LEN(msg->ctl);

	printf("hdr: dst=%02x src=%02x p=%02x sub=%02x len=%02x\n",
	   msg->dst, msg->src, DT_CTL_P(msg->ctl), DT_CTL_SUBADDR(msg->ctl),
	   l);

	printf("body: ");	   
	for (i = 0; i < l && i < 20; i++)
		printf("%02x ", msg->body[i]);
	if (i < l) {
		printf("\n");
		for (; i < l; i++)
			printf("%02x ", msg->body[i]);
	}
	printf("\n");
}