summaryrefslogtreecommitdiff
path: root/kernel/tty.c
blob: bfbf7750fa85acd29f50c8f45defa2959ce1e67c (plain)
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
#include <asm/interrupt.h>
#include <errno.h>
#include <kernel/sched.h>
#include <kernel/serial.h>
#include <kernel/tty.h>
#include <limits.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

#define NRTTY \
  (sizeof(ttys) / sizeof(struct tty_struct*))

static struct tty_struct *ttys[] = {
  NULL,
  &tty_rs
};

/* check if a buffer's empty and if so, go to sleep */
static int sleep_while_empty(struct tty_queue *q) {
  cli();
  while(1) {
    if(q->pread == q->pwrite)
      interruptible_sleep_on(&q->waiting);

    /* check again to see if anything was read, or we were interrupted */
    if(q->pread == q->pwrite) {
      sti();
      return -1;
    }

    sti();
    return 0;
  }
}

/* check if a buffer's full and if so, go to sleep */
static int sleep_while_full(struct tty_queue *q, struct tty_struct *tty) {
  cli();
  while(1) {
    /*
     * if the buffer is full, first notify the
     * driver so it can try to remove some
     * data then, check again and if it's
     * still full, go to sleep.
     */
    if(q->pwrite == ((q->pread - 1) % sizeof(q->buf)))
      if(tty->write)
        tty->write();

    if(q->pwrite == ((q->pread - 1) % sizeof(q->buf)))
      interruptible_sleep_on(&q->waiting);

    /* check again to see if anything was removed, or we were interrupted */
    if(q->pwrite == ((q->pread - 1) % sizeof(q->buf))) {
      sti();
      return -1;
    }

    sti();
    return 0;
  }
}

ssize_t tty_read(unsigned tty, void *buf, size_t len) {
  size_t i;
  char *b = buf;
  struct tty_queue *q = &ttys[tty]->rqueue;

  if(tty > NRTTY || ttys[tty] == NULL)
    return -1;

  if(len == 0)
    return 0;

  if(len > SSIZE_MAX)
    len = SSIZE_MAX;

  for(i = 0; i < len; i++) {
    /* check if the buffer's empty and if so, go to sleep */
    if(sleep_while_empty(q) < 0) {
      if(i == 0)
        return -EINTR;

      return i;
    }

    *b++ = q->buf[q->pread];
    q->pread = (q->pread + 1) % TTY_BUF_SIZE;
  }

  return len;
}

ssize_t tty_write(unsigned tty, void *buf, size_t len) {
  size_t i;
  char *b = buf;
  struct tty_queue *q = &ttys[tty]->wqueue;

  if(tty > NRTTY || ttys[tty] == NULL)
    return -1;

  if(len == 0)
    return 0;

  if(len > SSIZE_MAX)
    len = SSIZE_MAX;

  for(i = 0; i < len; i++) {
    /* check if the buffer's full and if so, go to sleep */
    if(sleep_while_full(q, ttys[tty]) < 0) {
      if(i == 0)
        return -EINTR;

      return i;
    }

    q->buf[q->pwrite] = *b++;
    q->pwrite = (q->pwrite + 1) % TTY_BUF_SIZE;
  }

  /* tell the driver that new data is available */
  cli();
  if(ttys[tty]->write)
    ttys[tty]->write();
  sti();

  return len;
}

void tty_init(void) {
  int i;

  for(i = 0; i < NRTTY; i++) {
    if(!ttys[i])
      continue;

    memset(&ttys[i]->rqueue, 0, sizeof(struct tty_queue));
    memset(&ttys[i]->wqueue, 0, sizeof(struct tty_queue));

    if(ttys[i]->init)
      ttys[i]->init();
  }
}