Proper line discipline for TTYs and simple line discipline for chardevs

This commit is contained in:
Mark
2020-01-21 17:08:31 +02:00
parent beecf5e2b3
commit 0105aa8002
16 changed files with 383 additions and 237 deletions
+3
View File
@@ -4,6 +4,9 @@ struct winsize;
void amd64_con_get_size(struct winsize *ws);
// For tty
int pc_con_putc(void *dev, char c);
void con_blink(void);
void amd64_con_putc(int c);
void amd64_con_init(void);
+2
View File
@@ -1,4 +1,6 @@
#pragma once
struct chrdev;
void ps2_init(void);
void ps2_register_device(void);
void ps2_kbd_set_tty(struct chrdev *dev);
+9 -3
View File
@@ -1,19 +1,25 @@
#pragma once
#include "sys/termios.h"
#include "sys/types.h"
#include "sys/ring.h"
struct chrdev {
void *dev_data;
struct ring buffer;
enum {
CHRDEV_GENERIC = 0,
CHRDEV_TTY
} type;
// TODO: move tty info somewhere else?
struct termios tc;
int (*ioctl) (struct chrdev *chr, unsigned int cmd, void *arg);
ssize_t (*write) (struct chrdev *chr, const void *buf, size_t pos, size_t lim);
ssize_t (*read) (struct chrdev *chr, void *buf, size_t pos, size_t lim);
};
// Generic ring-buffer reader
ssize_t chr_read_ring(struct chrdev *chr, void *buf, size_t pos, size_t lim);
int chr_ioctl(struct chrdev *chr, unsigned int cmd, void *arg);
ssize_t chr_write(struct chrdev *chr, const void *buf, size_t pos, size_t lim);
ssize_t chr_read(struct chrdev *chr, void *buf, size_t pos, size_t lim);
+12
View File
@@ -0,0 +1,12 @@
#pragma once
struct chrdev;
// Common TTY line discipline
ssize_t line_read(struct chrdev *chr, void *buf, size_t pos, size_t lim);
// Simple line discipline - no control, disregard termios, spit out data right after
// a single byte is available
ssize_t simple_line_read(struct chrdev *chr, void *buf, size_t pos, size_t lim);
// TODO: invent some magic for devices like /dev/urandom and /dev/zero etc.
// they're implemented as block devices right now
+16 -11
View File
@@ -2,19 +2,24 @@
#include "sys/types.h"
#include "sys/spin.h"
struct thread;
#define RING_SIGNAL_BRK (1 << 0)
#define RING_SIGNAL_EOF (1 << 1)
// Ring buffer
struct ring {
char *data;
size_t wrptr;
size_t rdptr;
size_t rd, wr;
size_t cap;
spin_t lock;
int post;
struct thread *listener;
char *base;
int flags;
};
void ring_post(struct ring *b);
size_t ring_avail(struct ring *b);
ssize_t ring_read(struct thread *ctx, struct ring *b, void *buf, size_t lim);
int ring_putc(struct thread *ctx, struct ring *b, char c);
void ring_init(struct ring *b, size_t cap);
int ring_readable(struct ring *b);
int ring_getc(struct thread *ctx, struct ring *b, char *c, int err);
void ring_signal(struct ring *b, int type);
int ring_putc(struct thread *ctx, struct ring *b, char c, int wait);
int ring_write(struct thread *ctx, struct ring *b, const void *data, size_t count, int wait);
int ring_init(struct ring *b, size_t cap);
+34
View File
@@ -1,11 +1,45 @@
#pragma once
#define TCGETS 0x5401
#define TCSETS 0x5402
#define TIOCSPGRP 0x5410
#define TIOCGWINSZ 0x5413
// Output flags
#define OPOST (1 << 0)
#define ONLCR (1 << 2)
#define OCRNL (1 << 4)
#define ONLRET (1 << 6)
// Control flags
#define ECHO (1 << 4) // Echo input characters
#define ECHOE (1 << 5) // if ICANON, ERASE erases prec. character,
// WERASE erases prec. word
#define ECHOK (1 << 6) // if ICANON, KILL character erases the line
#define ECHONL (1 << 8) // if ICANON, echo newline (even if no ECHO)
#define TERMIOS_DEFAULT \
{ \
.c_iflag = 0, \
.c_oflag = OPOST, \
.c_cflag = 0, \
.c_lflag = ECHO | ECHONL | ECHOE | ECHOK, \
}
typedef unsigned int tcflag_t;
struct winsize {
unsigned short ws_row;
unsigned short ws_col;
unsigned short ws_xpixel;
unsigned short ws_ypixel;
};
struct termios {
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
};
#define TCSANOW 0
+9 -3
View File
@@ -1,6 +1,12 @@
#pragma once
#include "sys/fs/node.h"
struct chrdev;
// From keyboard handlers and stuff
void tty_control_write(struct chrdev *n, char c);
void tty_data_write(struct chrdev *n, char c);
// From code directly to the screen
void tty_puts(struct chrdev *n, const char *s);
void tty_putc(struct chrdev *n, char c);
void tty_control_write(int n, char c);
void tty_buffer_write(int n, char c);
void tty_init(void);