summaryrefslogtreecommitdiff
path: root/kernel/serial.c
blob: f5610f5a8a3ace4a0030607a6ff091b87a5887f2 (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
#include <asm/interrupt.h>
#include <asm/io.h>
#include <errno.h>
#include <kernel/con.h>
#include <kernel/sched.h>
#include <sys/types.h>
#include <unistd.h>

/* 115200 baud */
#define DIVISOR 1

#define RSBASE 0x3F8

#define RSDATA    RSBASE
#define RSINTEN   (RSBASE + 1)
#define RSFIFOC   (RSBASE + 2)
#define RSLINEC   (RSBASE + 3)
#define RSMODEMC  (RSBASE + 4)
#define RSLINES   (RSBASE + 5)
#define RSMODEMS  (RSBASE + 6)
#define RSSCRATCH (RSBASE + 7)

#define rswait()\
  while((inb(RSLINES) & 0x20) == 0);

extern void rs_isr(void);

static char present;

static char rxbuf[256];
static unsigned rxread, rxwrite;
static struct task_struct *waiting_task;

int rsputs(char *s) {
  if(!present)
    return -ENXIO;

  while(*s) {
    rswait();
    if(*s == '\n' || *s == '\r') {
      outb(RSDATA, '\n');
      rswait();
      outb(RSDATA, '\r');
      s++;
    } else {
      outb(RSDATA, *s++);
    }
  }

  return 0;
}

void serial_init(void) {
  present = 0;

  outb(RSSCRATCH, 0x00);
  outb(RSSCRATCH, 0xAA);
  if(inb(RSSCRATCH) != 0xAA) {
    printk("[rs] COM0 not detected!\n");
    return;
  }

  /* enable access to the divisor */
  outb(RSLINEC, inb(RSLINEC) | 0x80);
  /* write the divisor */
  outb(RSBASE, DIVISOR);
  outb(RSBASE + 1, DIVISOR >> 8);
  /* restore access to the divisor */
  outb(RSLINEC, inb(RSLINEC) & 0x7F);
  /* configure port parameters */
  outb(RSFIFOC, 0x06);
  outb(RSLINEC, 0x03);
  outb(RSMODEMC, 0x0B);
  outb(RSINTEN, 0x01);
  inb(RSDATA);
  /* register the interrupt handler */
  register_isr(0x24, 0, &rs_isr);
  irq_enable(0x10);

  rsputs("[kernel] Serial monitor initialized!\n\r");
  printk("[rs] Serial port COM0 initialized!\n");

  rxread = 0;
  rxwrite = 1;
  waiting_task = NULL;
  present = 1;
}

ssize_t rsread(void *buf, size_t len) {
  ssize_t n = len;
  char *b = buf;

  if(!present)
    return -ENXIO;

  if(len == 0)
    return 0;

  while(n--) {
    /* check if the buffer's empty and if so, go to sleep */
    cli();
    if(rxread == rxwrite)
      interruptible_sleep_on(&waiting_task);

    /* check again to see if anything was read, or we were interrupted */
    if(rxread == rxwrite) {
      sti();
      return -EINTR;
    }

    sti();

    *b++ = rxbuf[rxread];
    rxread = rxread + 1 % sizeof(rxbuf);
  }

  return len;
}

void rs_handler(void) {
  if((inb(RSLINES) & 1) == 0)
    return;
  if(rxwrite == (rxread - 1 % sizeof(rxbuf)))
    return;
  rxbuf[rxwrite] = inb(RSDATA);
  rxwrite = rxwrite + 1 % sizeof(rxbuf);

  if(waiting_task != NULL) {
    waiting_task->state = TSTATE_RUNNING;
    waiting_task = NULL;
  }
}