2 #include <klibc/string.h>
4 #include <lunaix/mm/valloc.h>
5 #include <lunaix/process.h>
6 #include <lunaix/spike.h>
7 #include <lunaix/status.h>
9 #include <usr/lunaix/ioctl_defs.h>
12 #define termdev(dev) ((struct term*)(dev)->underlay)
14 extern struct term_lcntl ansi_line_controller;
15 static struct term_lcntl* line_controls[] = {[ANSI_LCNTL] =
16 &ansi_line_controller};
17 #define LCNTL_TABLE_LEN (sizeof(line_controls) / sizeof(struct term_lcntl*))
19 static struct devclass termdev = DEVCLASS(DEVIF_NON, DEVFN_TTY, DEV_VTERM);
21 struct device* sysconsole = NULL;
24 term_exec_cmd(struct device* dev, u32_t req, va_list args)
26 struct term* term = termdev(dev);
33 pid_t pgid = va_arg(args, pid_t);
43 case TDEV_TCSETCHDEV: {
44 int fd = va_arg(args, int);
47 if (vfs_getfd(fd, &vfd)) {
52 struct device* cdev = resolve_device(vfd->file->inode->data);
57 if (cdev->dev_type != DEV_IFSEQ) {
62 term_bind(term, cdev);
65 case TDEV_TCGETCHDEV: {
66 struct dev_info* devinfo = va_arg(args, struct dev_info*);
74 device_populate_info(term->chdev, devinfo);
78 case TDEV_TCGETATTR: {
79 struct termios* tios = va_arg(args, struct termios*);
80 *tios = (struct termios){.c_oflag = term->oflags,
81 .c_iflag = term->iflags,
82 .c_lflag = term->lflags,
83 .c_cflag = term->cflags};
84 memcpy(tios->c_cc, term->cc, _NCCS * sizeof(cc_t));
85 tios->c_baud = term->iospeed;
87 case TDEV_TCSETATTR: {
88 struct termios* tios = va_arg(args, struct termios*);
89 term->iflags = tios->c_iflag;
90 term->oflags = tios->c_oflag;
91 term->lflags = tios->c_lflag;
92 memcpy(term->cc, tios->c_cc, _NCCS * sizeof(cc_t));
94 tcflag_t old_cf = term->cflags;
95 term->cflags = tios->c_cflag;
101 if (tios->c_baud != term->iospeed) {
102 term->iospeed = tios->c_baud;
104 term->tp_cap->set_speed(term->chdev, tios->c_baud);
107 if (old_cf != tios->c_cflag) {
108 term->tp_cap->set_cntrl_mode(term->chdev, tios->c_cflag);
122 tdev_do_write(struct device* dev, void* buf, off_t fpos, size_t len)
124 struct term* tdev = termdev(dev);
125 lbuf_ref_t current = ref_current(&tdev->line_out);
128 wrsz += rbuffer_puts(
129 deref(current), &((char*)buf)[wrsz], len - wrsz);
131 if (rbuffer_full(deref(current))) {
136 if (!rbuffer_empty(deref(current))) {
143 tdev_do_read(struct device* dev, void* buf, off_t fpos, size_t len)
145 struct term* tdev = termdev(dev);
146 lbuf_ref_t current = ref_current(&tdev->line_in);
149 while (cont && rdsz < len) {
150 if (rbuffer_empty(deref(current))) {
151 tdev->line_in.sflags = 0;
152 cont = term_read(tdev);
155 rdsz += rbuffer_gets(
156 deref(current), &((char*)buf)[rdsz], len - rdsz);
162 static cc_t default_cc[_NCCS] = {4, '\n', 0x7f, 3, 1, 24, 22, 0, 0, 1, 1};
165 load_default_setting(struct term* tdev)
167 tdev->lflags = _ICANON | _IEXTEN | _ISIG | _ECHO | _ECHOE | _ECHONL;
168 tdev->iflags = _ICRNL | _IGNBRK;
169 tdev->oflags = _ONLCR | _OPOST;
170 memcpy(tdev->cc, default_cc, _NCCS * sizeof(cc_t));
174 alloc_term_buffer(struct term* terminal, size_t sz_hlf)
176 line_alloc(&terminal->line_in, sz_hlf);
177 line_alloc(&terminal->line_out, sz_hlf);
178 terminal->scratch_pad = valloc(sz_hlf);
182 term_create(struct device* chardev, char* suffix)
184 struct term* terminal = vzalloc(sizeof(struct term));
190 terminal->dev = device_allocseq(NULL, terminal);
191 terminal->chdev = chardev;
193 terminal->dev->ops.read = tdev_do_read;
194 terminal->dev->ops.write = tdev_do_write;
195 terminal->dev->ops.exec_cmd = term_exec_cmd;
197 // TODO choice of lcntl can be flexible
198 terminal->lcntl = line_controls[ANSI_LCNTL];
200 alloc_term_buffer(terminal, 1024);
203 int cdev_var = DEV_VAR_FROM(chardev->ident.unique);
204 register_device(terminal->dev, &termdev, "tty%s%d", suffix, cdev_var);
206 register_device(terminal->dev, &termdev, "tty%d", termdev.variant++);
209 struct capability_meta* termport_cap = device_get_capability(chardev, TERMPORT_CAP);
211 terminal->tp_cap = get_capability(termport_cap, struct termport_capability);
214 struct capability_meta* term_cap = new_capability_marker(TERMIOS_CAP);
215 device_grant_capability(terminal->dev, term_cap);
217 load_default_setting(terminal);
223 term_bind(struct term* term, struct device* chdev)
225 device_lock(term->dev);
229 device_unlock(term->dev);
235 line_alloc(struct linebuffer* lbf, size_t sz_hlf)
237 char* buffer = valloc(sz_hlf * 2);
238 memset(lbf, 0, sizeof(*lbf));
239 lbf->current = rbuffer_create(buffer, sz_hlf);
240 lbf->next = rbuffer_create(&buffer[sz_hlf], sz_hlf);
241 lbf->sz_hlf = sz_hlf;
245 line_free(struct linebuffer* lbf, size_t sz_hlf)
248 (void*)MIN((ptr_t)lbf->current->buffer, (ptr_t)lbf->next->buffer);
253 term_sendsig(struct term* tdev, int signal)
255 if ((tdev->lflags & _ISIG)) {
256 proc_setsignal(get_process(tdev->fggrp), signal);