cynix

x86 UNIX-like OS
git clone git://git.2f30.org/cynix
Log | Files | Refs | README | LICENSE

serial.c (1151B)


      1 /*
      2  *  core/serial.c
      3  *
      4  *  Copyright (C) 2009 stateless
      5  */
      6 
      7 #include <serial.h>
      8 #include <x86.h>
      9 #include <stdarg.h>
     10 #include <stdio.h>
     11 #include <sys/types.h>
     12 #include <errno.h>
     13 
     14 static int serial_has_init = 0;
     15 
     16 void
     17 serial_init(void)
     18 {
     19 	uint32_t state;
     20 
     21 	save_flags(&state);
     22 	cli();
     23 	outb(COM_PORT + 1, 0x0);
     24 	outb(COM_PORT + 3, 1 << 7);
     25 	outb(COM_PORT + 0, 0x3);
     26 	outb(COM_PORT + 1, 0x0);
     27 	outb(COM_PORT + 3, 0 << 7);
     28 	outb(COM_PORT + 3, 0x3);
     29 	outb(COM_PORT + 2, 0xc7);
     30 	outb(COM_PORT + 4, 0x0b);
     31 	serial_has_init = 1;
     32 	load_flags(state);
     33 }
     34 
     35 int
     36 serial_dump(const char *s, ...)
     37 {
     38 	char buf[512], *ptr;
     39 	va_list ap;
     40 	int c;
     41 	uint32_t state;
     42 
     43 	if (!serial_has_init)
     44 		return -EIO;
     45 	save_flags(&state);
     46 	cli();
     47 	va_start(ap, s);
     48 	c = vsprintf(buf, s, ap);
     49 	va_end(ap);
     50 	ptr = buf;
     51 	while (*ptr)
     52 		if (inb(COM_PORT + 5) & 0x20)
     53 			outb(COM_PORT, *ptr++);
     54 	load_flags(state);
     55 	return c;
     56 }
     57 
     58 int
     59 serial_putchar(int c)
     60 {
     61 	uint32_t state;
     62 
     63 	if (!serial_has_init)
     64 		return -EIO;
     65 	save_flags(&state);
     66 	cli();
     67 	if (inb(COM_PORT + 5) & 0x20)
     68 		outb(COM_PORT, (uint8_t)c);
     69 	load_flags(state);
     70 	return 0;
     71 }
     72 
     73 int
     74 serial_avail(void)
     75 {
     76 	return serial_has_init;
     77 }
     78