Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
/*
 *  kernel/chr_drv/vt.c
 *
 *  Copyright (C) 1992 obz under the linux copyright
 *  Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
 */

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/tty.h>
#include <linux/timer.h>
#include <linux/kernel.h>
#include <linux/kd.h>
#include <linux/vt.h>
#include <linux/string.h>

#include <asm/io.h>
#include <asm/segment.h>

#include "kbd_kern.h"
#include "vt_kern.h"
#include "diacr.h"

extern struct tty_driver console_driver;

/*
 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
 * experimentation and study of X386 SYSV handling.
 *
 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
 * always treat our set of vt as numbered 1..NR_CONSOLES (corresponding to
 * ttys 0..NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
 * to the current console is done by the main ioctl code.
 */

struct vt_struct vt_cons[NR_CONSOLES];

asmlinkage int sys_ioperm(unsigned long from, unsigned long num, int on);

extern void compute_shiftstate(void);
extern void change_console(unsigned int new_console);
extern void complete_change_console(unsigned int new_console);
extern int vt_waitactive(void);

/*
 * routines to load custom translation table and EGA/VGA font from console.c
 */
extern int con_set_trans(char * table);
extern int con_get_trans(char * table);
extern int con_set_font(char * fontmap);
extern int con_get_font(char * fontmap);

/*
 * these are the valid i/o ports we're allowed to change. they map all the
 * video ports
 */
#define GPFIRST 0x3b4
#define GPLAST 0x3df
#define GPNUM (GPLAST - GPFIRST + 1)

/*
 * Generates sound of some count for some number of clock ticks
 * [count = 1193180 / frequency]
 *
 * If freq is 0, will turn off sound, else will turn it on for that time.
 * If msec is 0, will return immediately, else will sleep for msec time, then
 * turn sound off.
 *
 * We use the BEEP_TIMER vector since we're using the same method to
 * generate sound, and we'll overwrite any beep in progress. That may
 * be something to fix later, if we like.
 *
 * We also return immediately, which is what was implied within the X
 * comments - KDMKTONE doesn't put the process to sleep.
 */
static void
kd_nosound(unsigned long ignored)
{
	/* disable counter 2 */
	outb(inb_p(0x61)&0xFC, 0x61);
	return;
}

void
kd_mksound(unsigned int count, unsigned int ticks)
{
	static struct timer_list sound_timer = { NULL, NULL, 0, 0, kd_nosound };

	cli();
	del_timer(&sound_timer);
	if (count) {
		/* enable counter 2 */
		outb_p(inb_p(0x61)|3, 0x61);
		/* set command for counter 2, 2 byte write */
		outb_p(0xB6, 0x43);
		/* select desired HZ */
		outb_p(count & 0xff, 0x42);
		outb((count >> 8) & 0xff, 0x42);

		if (ticks) {
			sound_timer.expires = ticks;
			add_timer(&sound_timer);
		}
	} else
		kd_nosound(0);
	sti();
	return;
}

/*
 * We handle the console-specific ioctl's here.  We allow the
 * capability to modify any console, not just the fg_console. 
 */
int vt_ioctl(struct tty_struct *tty, struct file * file,
	     unsigned int cmd, unsigned long arg)
{
	int console, i;
	unsigned char ucval;
	struct kbd_struct * kbd;
	struct vt_struct *vt = tty->driver_data;

	console = vt->vc_num;

	if (console < 0 || console >= NR_CONSOLES)
		return -ENOIOCTLCMD;

	kbd = kbd_table + console;
	switch (cmd) {
	case KIOCSOUND:
		kd_mksound((unsigned int)arg, 0);
		return 0;

	case KDMKTONE:
	{
		unsigned int ticks = HZ * ((arg >> 16) & 0xffff) / 1000;

		/*
		 * Generate the tone for the appropriate number of ticks.
		 * If the time is zero, turn off sound ourselves.
		 */
		kd_mksound(arg & 0xffff, ticks);
		if (ticks == 0)
			kd_nosound(0);
		return 0;
	}

	case KDGKBTYPE:
		/*
		 * this is naive.
		 */
		i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned char));
		if (!i)
			put_fs_byte(KB_101, (char *) arg);
		return i;

	case KDADDIO:
	case KDDELIO:
		/*
		 * KDADDIO and KDDELIO may be able to add ports beyond what
		 * we reject here, but to be safe...
		 */
		if (arg < GPFIRST || arg > GPLAST)
			return -EINVAL;
		return sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;

	case KDENABIO:
	case KDDISABIO:
		return sys_ioperm(GPFIRST, GPNUM,
				  (cmd == KDENABIO)) ? -ENXIO : 0;

	case KDSETMODE:
		/*
		 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
		 * doesn't do a whole lot. i'm not sure if it should do any
		 * restoration of modes or what...
		 */
		switch (arg) {
		case KD_GRAPHICS:
			break;
		case KD_TEXT0:
		case KD_TEXT1:
			arg = KD_TEXT;
		case KD_TEXT:
			break;
		default:
			return -EINVAL;
		}
		if (vt_cons[console].vc_mode == (unsigned char) arg)
			return 0;
		vt_cons[console].vc_mode = (unsigned char) arg;
		if (console != fg_console)
			return 0;
		/*
		 * explicitly blank/unblank the screen if switching modes
		 */
		if (arg == KD_TEXT)
			unblank_screen();
		else {
			timer_active &= ~(1<<BLANK_TIMER);
			blank_screen();
		}
		return 0;

	case KDGETMODE:
		i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned long));
		if (!i)
			put_fs_long(vt_cons[console].vc_mode, (unsigned long *) arg);
		return i;

	case KDMAPDISP:
	case KDUNMAPDISP:
		/*
		 * these work like a combination of mmap and KDENABIO.
		 * this could be easily finished.
		 */
		return -EINVAL;

	case KDSKBMODE:
		switch(arg) {
		  case K_RAW:
		        set_vc_kbd_mode(kbd, VC_RAW);
			clr_vc_kbd_mode(kbd, VC_MEDIUMRAW);
			break;
		  case K_MEDIUMRAW:
			clr_vc_kbd_mode(kbd, VC_RAW);
			set_vc_kbd_mode(kbd, VC_MEDIUMRAW);
			break;
		  case K_XLATE:
			clr_vc_kbd_mode(kbd, VC_RAW);
			clr_vc_kbd_mode(kbd, VC_MEDIUMRAW);
			compute_shiftstate();
			break;
		  default:
			return -EINVAL;
		}
		if (tty->ldisc.flush_buffer)
			tty->ldisc.flush_buffer(tty);
		return 0;

	case KDGKBMODE:
		i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned long));
		if (!i) {
			ucval = (vc_kbd_mode(kbd, VC_RAW) ? K_RAW :
				 vc_kbd_mode(kbd, VC_MEDIUMRAW) ? K_MEDIUMRAW :
				 K_XLATE);
			put_fs_long(ucval, (unsigned long *) arg);
		}
		return i;

	/* this could be folded into KDSKBMODE, but for compatibility
	   reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
	case KDSKBMETA:
		switch(arg) {
		  case K_METABIT:
			clr_vc_kbd_mode(kbd, VC_META);
			break;
		  case K_ESCPREFIX:
			set_vc_kbd_mode(kbd, VC_META);
			break;
		  default:
			return -EINVAL;
		}
		return 0;

	case KDGKBMETA:
		i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned long));
		if (!i) {
			ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX :
				 K_METABIT);
			put_fs_long(ucval, (unsigned long *) arg);
		}
		return i;

	case KDGKBENT:
	{
		struct kbentry * const a = (struct kbentry *)arg;
		u_char s;

		i = verify_area(VERIFY_WRITE, (void *)a, sizeof(struct kbentry));
		if (i)
			return i;
		if ((i = get_fs_byte((char *) &a->kb_index)) >= NR_KEYS)
			return -EINVAL;
		if ((s = get_fs_byte((char *) &a->kb_table)) >= NR_KEYMAPS)
			return -EINVAL;
		put_fs_word(key_map[s][i], (short *) &a->kb_value);
		return 0;
	}

	case KDSKBENT:
	{
		const struct kbentry * a = (struct kbentry *)arg;
		u_char s;
		u_short v;

		i = verify_area(VERIFY_READ, (void *)a, sizeof(struct kbentry));
		if (i)
			return i;
		if ((i = get_fs_byte((char *) &a->kb_index)) >= NR_KEYS)
			return -EINVAL;
		if ((s = get_fs_byte((char *) &a->kb_table)) >= NR_KEYMAPS)
			return -EINVAL;
		if (KTYP(v = get_fs_word(&a->kb_value)) >= NR_TYPES)
			return -EINVAL;
		if (KVAL(v) > max_vals[KTYP(v)])
			return -EINVAL;
		/*
		 * Only the Superuser can set or unset the Secure
		 * Attention Key.
		 */
		if (((key_map[s][i] == K_SAK) || (v == K_SAK)) &&
		    !suser())
			return -EPERM;
		key_map[s][i] = v;
		return 0;
	}

	case KDGKBSENT:
	{
		struct kbsentry *a = (struct kbsentry *)arg;
		char *p;
		u_char *q;

		i = verify_area(VERIFY_WRITE, (void *)a, sizeof(struct kbsentry));
		if (i)
			return i;
		if ((i = get_fs_byte(&a->kb_func)) >= NR_FUNC || i < 0)
			return -EINVAL;
		q = a->kb_string;
		p = func_table[i];
		if(!p) {
		    /* beware of tables generated for a smaller NR_FUNC */
		    printk("KDGKBSENT error: func_table[%d] is nil.\n",
			   i);
		    return -EINVAL;
		}
		for ( ; *p; p++)
			put_fs_byte(*p, q++);
		put_fs_byte(0, q);
		return 0;
	}

	case KDSKBSENT:
	{
		struct kbsentry * const a = (struct kbsentry *)arg;
		int delta;
		char *first_free;
		int k;
		u_char *p;
		char *q;

		i = verify_area(VERIFY_READ, (void *)a, sizeof(struct kbsentry));
		if (i)
			return i;
		if ((i = get_fs_byte(&a->kb_func)) >= NR_FUNC)
			return -EINVAL;
		q = func_table[i];
		if (!q) {
		    /* beware of tables generated for a smaller NR_FUNC */
		    printk("KDSKBSENT error: func_table[%d] is nil.\n",
			   i);
		    return -EINVAL;
		}
		delta = -strlen(q);
		for (p = a->kb_string; get_fs_byte(p); p++)
			delta++;
		first_free = func_table[NR_FUNC - 1] +
			strlen(func_table[NR_FUNC - 1]) + 1;
		if (
			delta > 0 &&
			first_free + delta > func_buf + FUNC_BUFSIZE
		)
			return -EINVAL;
		if (i < NR_FUNC - 1) {
			memmove(
				func_table[i + 1] + delta,
				func_table[i + 1],
				first_free - func_table[i + 1]);
			for (k = i + 1; k < NR_FUNC; k++)
			    if (func_table[k])  /* just to be sure */
				func_table[k] += delta;
		}
		for (p = a->kb_string, q = func_table[i]; ; p++, q++)
			if (!(*q = get_fs_byte(p)))
				break;
		return 0;
	}

	case KDGKBDIACR:
	{
	        struct kbdiacrs *a = (struct kbdiacrs *)arg;

	        i = verify_area(VERIFY_WRITE, (void *) a, sizeof(struct kbdiacrs));
		if (i)
		        return i;
		put_fs_long(accent_table_size, &a->kb_cnt);
		memcpy_tofs(a->kbdiacr, accent_table,
			    accent_table_size*sizeof(struct kbdiacr));
		return 0;
	}

	case KDSKBDIACR:
	{
	        struct kbdiacrs *a = (struct kbdiacrs *)arg;
		unsigned int ct;

	        i = verify_area(VERIFY_READ, (void *) a, sizeof(struct kbdiacrs));
		if (i)
		        return i;
		ct = get_fs_long(&a->kb_cnt);
		if (ct >= MAX_DIACR)
		        return -EINVAL;
		accent_table_size = ct;
		memcpy_fromfs(accent_table, a->kbdiacr, ct*sizeof(struct kbdiacr));
		return 0;
	}

	case KDGETLED:
		i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(unsigned char));
		if (i)
			return i;
		put_fs_byte(kbd->ledstate, (char *) arg);
		return 0;

	case KDSETLED:
		if (arg & ~7)
			return -EINVAL;
		kbd->ledstate = arg;
		set_leds();
		return 0;

	case VT_SETMODE:
	{
		struct vt_mode *vtmode = (struct vt_mode *)arg;
		char mode;

		i = verify_area(VERIFY_WRITE, (void *)vtmode, sizeof(struct vt_mode));
		if (i)
			return i;
		mode = get_fs_byte(&vtmode->mode);
		if (mode != VT_AUTO && mode != VT_PROCESS)
			return -EINVAL;
		vt_cons[console].vt_mode.mode = mode;
		vt_cons[console].vt_mode.waitv = get_fs_byte(&vtmode->waitv);
		vt_cons[console].vt_mode.relsig = get_fs_word(&vtmode->relsig);
		vt_cons[console].vt_mode.acqsig = get_fs_word(&vtmode->acqsig);
		/* the frsig is ignored, so we set it to 0 */
		vt_cons[console].vt_mode.frsig = 0;
		vt_cons[console].vt_pid = current->pid;
		vt_cons[console].vt_newvt = 0;
		return 0;
	}

	case VT_GETMODE:
	{
		struct vt_mode *vtmode = (struct vt_mode *)arg;

		i = verify_area(VERIFY_WRITE, (void *)arg, sizeof(struct vt_mode));
		if (i)
			return i;
		put_fs_byte(vt_cons[console].vt_mode.mode, &vtmode->mode);
		put_fs_byte(vt_cons[console].vt_mode.waitv, &vtmode->waitv);
		put_fs_word(vt_cons[console].vt_mode.relsig, &vtmode->relsig);
		put_fs_word(vt_cons[console].vt_mode.acqsig, &vtmode->acqsig);
		put_fs_word(vt_cons[console].vt_mode.frsig, &vtmode->frsig);
		return 0;
	}

	/*
	 * Returns global vt state. Note that VT 0 is always open, since
	 * it's an alias for the current VT, and people can't use it here.
	 */
	case VT_GETSTATE:
	{
		struct vt_stat *vtstat = (struct vt_stat *)arg;
		unsigned short state, mask;

		i = verify_area(VERIFY_WRITE,(void *)vtstat, sizeof(struct vt_stat));
		if (i)
			return i;
		put_fs_word(fg_console + 1, &vtstat->v_active);
		state = 1;	/* /dev/tty0 is always open */
		for (i = 0, mask = 2; i < NR_CONSOLES; ++i, mask <<= 1)
			if (console_driver.table[i] &&
			    console_driver.table[i]->count > 0)
				state |= mask;
		put_fs_word(state, &vtstat->v_state);
		return 0;
	}

	/*
	 * Returns the first available (non-opened) console.
	 */
	case VT_OPENQRY:
		i = verify_area(VERIFY_WRITE, (void *) arg, sizeof(long));
		if (i)
			return i;
		for (i = 0; i < NR_CONSOLES; ++i)
			if (!console_driver.table[i] ||
			    console_driver.table[i]->count == 0)
				break;
		put_fs_long(i < NR_CONSOLES ? (i+1) : -1,
			    (unsigned long *)arg);
		return 0;

	/*
	 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
	 * with num >= 1 (switches to vt 0, our console) are not allowed, just
	 * to preserve sanity.
	 */
	case VT_ACTIVATE:
		if (arg == 0 || arg > NR_CONSOLES)
			return -ENXIO;
		change_console(arg - 1);
		return 0;

	/*
	 * wait until the specified VT has been activated
	 */
	case VT_WAITACTIVE:
		if (arg == 0 || arg > NR_CONSOLES)
			return -ENXIO;
		while (fg_console != arg - 1)
		{
			if (vt_waitactive() < 0)
				return -EINTR;
		}
		return 0;

	/*
	 * If a vt is under process control, the kernel will not switch to it
	 * immediately, but postpone the operation until the process calls this
	 * ioctl, allowing the switch to complete.
	 *
	 * According to the X sources this is the behavior:
	 *	0:	pending switch-from not OK
	 *	1:	pending switch-from OK
	 *	2:	completed switch-to OK
	 */
	case VT_RELDISP:
		if (vt_cons[console].vt_mode.mode != VT_PROCESS)
			return -EINVAL;

		/*
		 * Switching-from response
		 */
		if (vt_cons[console].vt_newvt >= 0)
		{
			if (arg == 0)
				/*
				 * Switch disallowed, so forget we were trying
				 * to do it.
				 */
				vt_cons[console].vt_newvt = -1;

			else
			{
				/*
				 * The current vt has been released, so
				 * complete the switch.
				 */
				int newvt = vt_cons[console].vt_newvt;
				vt_cons[console].vt_newvt = -1;
				complete_change_console(newvt);
			}
		}

		/*
		 * Switched-to response
		 */
		else
		{
			/*
			 * If it's just an ACK, ignore it
			 */
			if (arg != VT_ACKACQ)
				return -EINVAL;
		}

		return 0;

	case PIO_FONT:
		return con_set_font((char *)arg);
		/* con_set_font() defined in console.c */

	case GIO_FONT:
		return con_get_font((char *)arg);
		/* con_get_font() defined in console.c */

	case PIO_SCRNMAP:
		return con_set_trans((char *)arg);
		/* con_set_trans() defined in console.c */

	case GIO_SCRNMAP:
		return con_get_trans((char *)arg);
		/* con_get_trans() defined in console.c */

	default:
		return -ENOIOCTLCMD;
	}
}