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
/* $NetBSD: ttycons.c,v 1.20 2014/07/25 08:10:35 dholland Exp $ */

/*-
 * Copyright (c) 2007 Jared D. McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * 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.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: ttycons.c,v 1.20 2014/07/25 08:10:35 dholland Exp $");

#include <sys/param.h>
#include <sys/conf.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/kauth.h>
#include <sys/termios.h>
#include <sys/tty.h>

#include <dev/cons.h>

#include <machine/mainbus.h>
#include <machine/thunk.h>

static int	ttycons_match(device_t, cfdata_t, void *);
static void	ttycons_attach(device_t, device_t, void *);

void		ttycons_consinit(void);

struct ttycons_softc {
	device_t	sc_dev;
	struct tty	*sc_tty;
	void		*sc_rd_sih;
	void		*sc_ctrlc_sih;
	void		*sc_ctrlz_sih;
	u_char		sc_buf[1024];
};

dev_type_cngetc(ttycons_cngetc);
dev_type_cnputc(ttycons_cnputc);
dev_type_cnpollc(ttycons_cnpollc);

static struct cnm_state ttycons_cnm_state;
struct consdev ttycons_consdev = {
	.cn_getc = ttycons_cngetc,
	.cn_putc = ttycons_cnputc,
	.cn_pollc = ttycons_cnpollc,
	.cn_dev = NODEV,
	.cn_pri = CN_NORMAL,
};

CFATTACH_DECL_NEW(ttycons, sizeof(struct ttycons_softc),
    ttycons_match, ttycons_attach, NULL, NULL);

extern struct cfdriver ttycons_cd;

dev_type_open(ttycons_open);
dev_type_close(ttycons_close);
dev_type_read(ttycons_read);
dev_type_write(ttycons_write);
dev_type_ioctl(ttycons_ioctl);
dev_type_stop(ttycons_stop);
dev_type_tty(ttycons_tty);
dev_type_poll(ttycons_poll);

const struct cdevsw ttycons_cdevsw = {
	.d_open = ttycons_open,
	.d_close = ttycons_close,
	.d_read = ttycons_read,
	.d_write = ttycons_write,
	.d_ioctl = ttycons_ioctl,
	.d_stop = ttycons_stop,
	.d_tty = ttycons_tty,
	.d_poll = ttycons_poll,
	.d_kqfilter = ttykqfilter,
	.d_flag = D_TTY,
	.d_discard = nodiscard,
};

static void	ttycons_start(struct tty *);
static int	ttycons_param(struct tty *, struct termios *);

static int	ttycons_intr(void *);
static void	ttycons_softintr(void *);

static sigfunc_t  ttycons_ctrlc;
static void	ttycons_softctrlc(void *);
static sigfunc_t  ttycons_ctrlz;
static void	ttycons_softctrlz(void *);

static int
ttycons_match(device_t parent, cfdata_t match, void *opaque)
{
	struct thunkbus_attach_args *taa = opaque;

	if (taa->taa_type != THUNKBUS_TYPE_TTYCONS)
		return 0;

	return 1;
}

static void
ttycons_attach(device_t parent, device_t self, void *opaque)
{
	struct ttycons_softc *sc = device_private(self);
	int maj;

	aprint_naive("\n");
	aprint_normal(": console\n");

	sc->sc_dev = self;
	sc->sc_tty = tty_alloc();
	tty_attach(sc->sc_tty);
	sc->sc_tty->t_oproc = ttycons_start;
	sc->sc_tty->t_param = ttycons_param;
	sc->sc_tty->t_sc = sc;

	maj = cdevsw_lookup_major(&ttycons_cdevsw);
	cn_tab->cn_dev = makedev(maj, device_unit(self));
	sc->sc_tty->t_dev = cn_tab->cn_dev;

	sc->sc_rd_sih = softint_establish(SOFTINT_SERIAL,
	    ttycons_softintr, sc);
	if (sc->sc_rd_sih == NULL)
		panic("couldn't establish ttycons intr handler\n");

	sc->sc_ctrlc_sih = softint_establish(SOFTINT_SERIAL,
	    ttycons_softctrlc, sc);
	if (sc->sc_ctrlc_sih == NULL)
		panic("couldn't establish ttycons ctrlc handler\n");
	sc->sc_ctrlz_sih = softint_establish(SOFTINT_SERIAL,
	    ttycons_softctrlz, sc);
	if (sc->sc_ctrlz_sih == NULL)
		panic("couldn't establish ttycons ctrlz handler\n");

	sigio_intr_establish(ttycons_intr, sc);
	signal_intr_establish(SIGINT,  ttycons_ctrlc);
	signal_intr_establish(SIGTSTP, ttycons_ctrlz);

	if (thunk_set_stdin_sigio(true) != 0)
		panic("couldn't enable stdin async mode");
}

void
ttycons_consinit(void)
{
	struct thunk_termios t;

	thunk_tcgetattr(0, &t);
	t.c_lflag &= ~(ECHO|ICANON);
	t.c_cc[VTIME] = 0;
	t.c_cc[VMIN] = 1;
	thunk_tcsetattr(0, TCSANOW, &t);

	cn_tab = &ttycons_consdev;
	cn_init_magic(&ttycons_cnm_state);
	cn_set_magic("\047\001");
}

int
ttycons_cngetc(dev_t dev)
{
	return thunk_getchar();
}

void
ttycons_cnputc(dev_t dev, int c)
{
	thunk_putchar(c);
}

void
ttycons_cnpollc(dev_t dev, int on)
{
}

int
ttycons_open(dev_t dev, int flag, int mode, lwp_t *l)
{
	struct ttycons_softc *sc;
	struct tty *t;

	sc = device_lookup_private(&ttycons_cd, minor(dev));
	if (sc == NULL)
		return ENXIO;
	t = sc->sc_tty;

	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, t))
		return EBUSY;

	if ((t->t_state & TS_ISOPEN) == 0 && t->t_wopen == 0) {
		t->t_dev = dev;
		ttychars(t);
		t->t_iflag = TTYDEF_IFLAG;
		t->t_oflag = TTYDEF_OFLAG;
		t->t_cflag = TTYDEF_CFLAG;
		t->t_lflag = TTYDEF_LFLAG;
		t->t_ispeed = t->t_ospeed = TTYDEF_SPEED;
		ttycons_param(t, &t->t_termios);
		ttsetwater(t);
	}
	t->t_state |= TS_CARR_ON;

	return t->t_linesw->l_open(dev, t);
}

int
ttycons_close(dev_t dev, int flag, int mode, lwp_t *l)
{
	struct ttycons_softc *sc;
	struct tty *t;

	sc = device_lookup_private(&ttycons_cd, minor(dev));
	t = sc->sc_tty;

	if (t == NULL)
		return 0;

	t->t_linesw->l_close(t, flag);
	ttyclose(t);

	return 0;
}

int
ttycons_read(dev_t dev, struct uio *uio, int flag)
{
	struct ttycons_softc *sc;
	struct tty *t;

	sc = device_lookup_private(&ttycons_cd, minor(dev));
	t = sc->sc_tty;

	return t->t_linesw->l_read(t, uio, flag);
}

int
ttycons_write(dev_t dev, struct uio *uio, int flag)
{
	struct ttycons_softc *sc;
	struct tty *t;

	sc = device_lookup_private(&ttycons_cd, minor(dev));
	t = sc->sc_tty;

	return t->t_linesw->l_write(t, uio, flag);
}

int
ttycons_poll(dev_t dev, int events, lwp_t *l)
{
	struct ttycons_softc *sc;
	struct tty *t;

	sc = device_lookup_private(&ttycons_cd, minor(dev));
	t = sc->sc_tty;

	return t->t_linesw->l_poll(t, events, l);
}

struct tty *
ttycons_tty(dev_t dev)
{
	struct ttycons_softc *sc;

	sc = device_lookup_private(&ttycons_cd, minor(dev));

	return sc->sc_tty;
}

int
ttycons_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
{
	struct ttycons_softc *sc;
	struct tty *t;
	int error;

	sc = device_lookup_private(&ttycons_cd, minor(dev));
	t = sc->sc_tty;

	error = t->t_linesw->l_ioctl(t, cmd, data, flag, l);
	if (error != EPASSTHROUGH)
		return error;

	error = ttioctl(t, cmd, data, flag, l);
	if (error != EPASSTHROUGH)
		return error;

	return EPASSTHROUGH;
}

static void
ttycons_start(struct tty *t)
{
	struct ttycons_softc *sc = t->t_sc;
	u_char *p = sc->sc_buf;
	int s, len, brem;

	s = spltty();
	if (t->t_state & (TS_TIMEOUT|TS_BUSY|TS_TTSTOP)) {
		splx(s);
		return;
	}
	t->t_state |= TS_BUSY;
	splx(s);

	brem = q_to_b(&t->t_outq, sc->sc_buf, sizeof(sc->sc_buf));

	while (brem > 0) {
		len = thunk_write(1, p, brem);
		if (len > 0) {
			p += len;
			brem -= len;
		}
	}

	s = spltty();
	t->t_state &= ~TS_BUSY;
	if (ttypull(t)) {
		t->t_state |= TS_TIMEOUT;
		callout_schedule(&t->t_rstrt_ch, 1);
	}
	splx(s);
}

void
ttycons_stop(struct tty *t, int flag)
{
}

static int
ttycons_param(struct tty *t, struct termios *c)
{
	t->t_ispeed = c->c_ispeed;
	t->t_ospeed = c->c_ospeed;
	t->t_cflag = c->c_cflag;
	return 0;
}

static int
ttycons_intr(void *priv)
{
	struct ttycons_softc *sc = priv;

	softint_schedule(sc->sc_rd_sih);

	return 0;
}

static void
ttycons_softintr(void *priv)
{
	struct ttycons_softc *sc = priv;
	struct tty *t = sc->sc_tty;
	unsigned char ch;
	int c;

	while ((c = thunk_pollchar()) >= 0) {
		ch = (unsigned char)c;
		cn_check_magic(t->t_dev, ch, ttycons_cnm_state);
		t->t_linesw->l_rint(ch, t);
	}
}


/*
 * handle SIGINT signal from trap.c
 *
 * argument 'pc' and 'va' are not used.
 */
static void
ttycons_ctrlc(siginfo_t *info, vaddr_t from_userland, vaddr_t pc, vaddr_t va)
{
	struct ttycons_softc *sc;

	sc = device_lookup_private(&ttycons_cd, minor(cn_tab->cn_dev));
	if (sc)
		softint_schedule(sc->sc_ctrlc_sih);

}

static void
ttycons_softctrlc(void *priv)
{
	struct ttycons_softc *sc = priv;
	struct tty *t = sc->sc_tty;
	unsigned char ch = 3;	/* ETX */

	cn_check_magic(t->t_dev, ch, ttycons_cnm_state);
	t->t_linesw->l_rint(ch, t);
}

/*
 * handle SIGTSTP signal from trap.c
 *
 * argument 'pc' and 'va' are not used.
 */
static void
ttycons_ctrlz(siginfo_t *info, vaddr_t from_userland, vaddr_t pc, vaddr_t va)
{
	struct ttycons_softc *sc;

	sc = device_lookup_private(&ttycons_cd, minor(cn_tab->cn_dev));
	if (sc)
		softint_schedule(sc->sc_ctrlz_sih);
}

static void
ttycons_softctrlz(void *priv)
{
	struct ttycons_softc *sc = priv;
	struct tty *t = sc->sc_tty;
	unsigned char ch = 26;	/* SUB */

	cn_check_magic(t->t_dev, ch, ttycons_cnm_state);
	t->t_linesw->l_rint(ch, t);
}