Automatic date update in version.in
[binutils-gdb.git] / gdb / ser-go32.c
blobae71f73cce92d83dceada9c62cb3e5f627c3b645
1 /* Remote serial interface for local (hardwired) serial ports for GO32.
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4 Contributed by Nigel Stephens, Algorithmics Ltd. (nigel@algor.co.uk).
6 This version uses DPMI interrupts to handle buffered i/o
7 without the separate "asynctsr" program.
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include "gdbcmd.h"
25 #include "serial.h"
27 * NS16550 UART registers
30 #define COM1ADDR 0x3f8
31 #define COM2ADDR 0x2f8
32 #define COM3ADDR 0x3e8
33 #define COM4ADDR 0x3e0
35 #define com_data 0 /* data register (R/W) */
36 #define com_dlbl 0 /* divisor latch low (W) */
37 #define com_ier 1 /* interrupt enable (W) */
38 #define com_dlbh 1 /* divisor latch high (W) */
39 #define com_iir 2 /* interrupt identification (R) */
40 #define com_fifo 2 /* FIFO control (W) */
41 #define com_lctl 3 /* line control register (R/W) */
42 #define com_cfcr 3 /* line control register (R/W) */
43 #define com_mcr 4 /* modem control register (R/W) */
44 #define com_lsr 5 /* line status register (R/W) */
45 #define com_msr 6 /* modem status register (R/W) */
48 * Constants for computing 16 bit baud rate divisor (lower byte
49 * in com_dlbl, upper in com_dlbh) from 1.8432MHz crystal. Divisor is
50 * 1.8432 MHz / (16 * X) for X bps. If the baud rate can't be set
51 * to within +- (desired_rate*SPEED_TOLERANCE/1000) bps, we fail.
53 #define COMTICK (1843200/16)
54 #define SPEED_TOLERANCE 30 /* thousandths; real == desired +- 3.0% */
56 /* interrupt enable register */
57 #define IER_ERXRDY 0x1 /* int on rx ready */
58 #define IER_ETXRDY 0x2 /* int on tx ready */
59 #define IER_ERLS 0x4 /* int on line status change */
60 #define IER_EMSC 0x8 /* int on modem status change */
62 /* interrupt identification register */
63 #define IIR_FIFO_MASK 0xc0 /* set if FIFOs are enabled */
64 #define IIR_IMASK 0xf /* interrupt cause mask */
65 #define IIR_NOPEND 0x1 /* nothing pending */
66 #define IIR_RLS 0x6 /* receive line status */
67 #define IIR_RXRDY 0x4 /* receive ready */
68 #define IIR_RXTOUT 0xc /* receive timeout */
69 #define IIR_TXRDY 0x2 /* transmit ready */
70 #define IIR_MLSC 0x0 /* modem status */
73 /* fifo control register */
74 #define FIFO_ENABLE 0x01 /* enable fifo */
75 #define FIFO_RCV_RST 0x02 /* reset receive fifo */
76 #define FIFO_XMT_RST 0x04 /* reset transmit fifo */
77 #define FIFO_DMA_MODE 0x08 /* enable dma mode */
78 #define FIFO_TRIGGER_1 0x00 /* trigger at 1 char */
79 #define FIFO_TRIGGER_4 0x40 /* trigger at 4 chars */
80 #define FIFO_TRIGGER_8 0x80 /* trigger at 8 chars */
81 #define FIFO_TRIGGER_14 0xc0 /* trigger at 14 chars */
83 /* character format control register */
84 #define CFCR_DLAB 0x80 /* divisor latch */
85 #define CFCR_SBREAK 0x40 /* send break */
86 #define CFCR_PZERO 0x30 /* zero parity */
87 #define CFCR_PONE 0x20 /* one parity */
88 #define CFCR_PEVEN 0x10 /* even parity */
89 #define CFCR_PODD 0x00 /* odd parity */
90 #define CFCR_PENAB 0x08 /* parity enable */
91 #define CFCR_STOPB 0x04 /* 2 stop bits */
92 #define CFCR_8BITS 0x03 /* 8 data bits */
93 #define CFCR_7BITS 0x02 /* 7 data bits */
94 #define CFCR_6BITS 0x01 /* 6 data bits */
95 #define CFCR_5BITS 0x00 /* 5 data bits */
97 /* modem control register */
98 #define MCR_LOOPBACK 0x10 /* loopback */
99 #define MCR_IENABLE 0x08 /* output 2 = int enable */
100 #define MCR_DRS 0x04 /* output 1 = xxx */
101 #define MCR_RTS 0x02 /* enable RTS */
102 #define MCR_DTR 0x01 /* enable DTR */
104 /* line status register */
105 #define LSR_RCV_FIFO 0x80 /* error in receive fifo */
106 #define LSR_TSRE 0x40 /* transmitter empty */
107 #define LSR_TXRDY 0x20 /* transmitter ready */
108 #define LSR_BI 0x10 /* break detected */
109 #define LSR_FE 0x08 /* framing error */
110 #define LSR_PE 0x04 /* parity error */
111 #define LSR_OE 0x02 /* overrun error */
112 #define LSR_RXRDY 0x01 /* receiver ready */
113 #define LSR_RCV_MASK 0x1f
115 /* modem status register */
116 #define MSR_DCD 0x80
117 #define MSR_RI 0x40
118 #define MSR_DSR 0x20
119 #define MSR_CTS 0x10
120 #define MSR_DDCD 0x08
121 #define MSR_TERI 0x04
122 #define MSR_DDSR 0x02
123 #define MSR_DCTS 0x01
125 #include <time.h>
126 #include <dos.h>
127 #include <go32.h>
128 #include <dpmi.h>
129 typedef unsigned long u_long;
131 /* 16550 rx fifo trigger point */
132 #define FIFO_TRIGGER FIFO_TRIGGER_4
134 /* input buffer size */
135 #define CBSIZE 4096
137 #define RAWHZ 18
139 #ifdef DOS_STATS
140 #define CNT_RX 16
141 #define CNT_TX 17
142 #define CNT_STRAY 18
143 #define CNT_ORUN 19
144 #define NCNT 20
146 static int intrcnt;
147 static size_t cnts[NCNT];
148 static char *cntnames[NCNT] =
150 /* h/w interrupt counts. */
151 "mlsc", "nopend", "txrdy", "?3",
152 "rxrdy", "?5", "rls", "?7",
153 "?8", "?9", "?a", "?b",
154 "rxtout", "?d", "?e", "?f",
155 /* s/w counts. */
156 "rxcnt", "txcnt", "stray", "swoflo"
159 #define COUNT(x) cnts[x]++
160 #else
161 #define COUNT(x)
162 #endif
164 /* Main interrupt controller port addresses. */
165 #define ICU_BASE 0x20
166 #define ICU_OCW2 (ICU_BASE + 0)
167 #define ICU_MASK (ICU_BASE + 1)
169 /* Original interrupt controller mask register. */
170 unsigned char icu_oldmask;
172 /* Maximum of 8 interrupts (we don't handle the slave icu yet). */
173 #define NINTR 8
175 static struct intrupt
177 char inuse;
178 struct dos_ttystate *port;
179 _go32_dpmi_seginfo old_rmhandler;
180 _go32_dpmi_seginfo old_pmhandler;
181 _go32_dpmi_seginfo new_rmhandler;
182 _go32_dpmi_seginfo new_pmhandler;
183 _go32_dpmi_registers regs;
185 intrupts[NINTR];
188 static struct dos_ttystate
190 int base;
191 int irq;
192 int refcnt;
193 struct intrupt *intrupt;
194 int fifo;
195 int baudrate;
196 unsigned char cbuf[CBSIZE];
197 unsigned int first;
198 unsigned int count;
199 int txbusy;
200 unsigned char old_mcr;
201 int ferr;
202 int perr;
203 int oflo;
204 int msr;
206 ports[4] =
209 COM1ADDR, 4, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
213 COM2ADDR, 3, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
217 COM3ADDR, 4, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
221 COM4ADDR, 3, 0, NULL, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0
225 static int dos_open (struct serial *scb, const char *name);
226 static void dos_raw (struct serial *scb);
227 static int dos_readchar (struct serial *scb, int timeout);
228 static int dos_setbaudrate (struct serial *scb, int rate);
229 static int dos_write (struct serial *scb, const void *buf, size_t count);
230 static void dos_close (struct serial *scb);
231 static serial_ttystate dos_get_tty_state (struct serial *scb);
232 static int dos_set_tty_state (struct serial *scb, serial_ttystate state);
233 static int dos_baudconv (int rate);
235 #define inb(p,a) inportb((p)->base + (a))
236 #define outb(p,a,v) outportb((p)->base + (a), (v))
237 #define disable() asm volatile ("cli");
238 #define enable() asm volatile ("sti");
241 static int
242 dos_getc (volatile struct dos_ttystate *port)
244 int c;
246 if (port->count == 0)
247 return -1;
249 c = port->cbuf[port->first];
250 disable ();
251 port->first = (port->first + 1) & (CBSIZE - 1);
252 port->count--;
253 enable ();
254 return c;
258 static int
259 dos_putc (int c, struct dos_ttystate *port)
261 if (port->count >= CBSIZE - 1)
262 return -1;
263 port->cbuf[(port->first + port->count) & (CBSIZE - 1)] = c;
264 port->count++;
265 return 0;
270 static void
271 dos_comisr (int irq)
273 struct dos_ttystate *port;
274 unsigned char iir, lsr, c;
276 disable (); /* Paranoia */
277 outportb (ICU_OCW2, 0x20); /* End-Of-Interrupt */
278 #ifdef DOS_STATS
279 ++intrcnt;
280 #endif
282 port = intrupts[irq].port;
283 if (!port)
285 COUNT (CNT_STRAY);
286 return; /* not open */
289 while (1)
291 iir = inb (port, com_iir) & IIR_IMASK;
292 switch (iir)
295 case IIR_RLS:
296 lsr = inb (port, com_lsr);
297 goto rx;
299 case IIR_RXTOUT:
300 case IIR_RXRDY:
301 lsr = 0;
306 c = inb (port, com_data);
307 if (lsr & (LSR_BI | LSR_FE | LSR_PE | LSR_OE))
309 if (lsr & (LSR_BI | LSR_FE))
310 port->ferr++;
311 else if (lsr & LSR_PE)
312 port->perr++;
313 if (lsr & LSR_OE)
314 port->oflo++;
317 if (dos_putc (c, port) < 0)
319 COUNT (CNT_ORUN);
321 else
323 COUNT (CNT_RX);
326 while ((lsr = inb (port, com_lsr)) & LSR_RXRDY);
327 break;
329 case IIR_MLSC:
330 /* could be used to flowcontrol Tx */
331 port->msr = inb (port, com_msr);
332 break;
334 case IIR_TXRDY:
335 port->txbusy = 0;
336 break;
338 case IIR_NOPEND:
339 /* No more pending interrupts, all done. */
340 return;
342 default:
343 /* Unexpected interrupt, ignore. */
344 break;
346 COUNT (iir);
350 #define ISRNAME(x) dos_comisr##x
351 #define ISR(x) static void ISRNAME(x)(void) {dos_comisr(x);}
353 ISR (0) ISR (1) ISR (2) ISR (3) /* OK */
354 ISR (4) ISR (5) ISR (6) ISR (7) /* OK */
356 typedef void (*isr_t) (void);
358 static isr_t isrs[NINTR] =
360 ISRNAME (0), ISRNAME (1), ISRNAME (2), ISRNAME (3),
361 ISRNAME (4), ISRNAME (5), ISRNAME (6), ISRNAME (7)
366 static struct intrupt *
367 dos_hookirq (unsigned int irq)
369 struct intrupt *intr;
370 unsigned int vec;
371 isr_t isr;
373 if (irq >= NINTR)
374 return 0;
376 intr = &intrupts[irq];
377 if (intr->inuse)
378 return 0;
380 vec = 0x08 + irq;
381 isr = isrs[irq];
383 /* Setup real mode handler. */
384 _go32_dpmi_get_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
386 intr->new_rmhandler.pm_selector = _go32_my_cs ();
387 intr->new_rmhandler.pm_offset = (u_long) isr;
388 if (_go32_dpmi_allocate_real_mode_callback_iret (&intr->new_rmhandler,
389 &intr->regs))
391 return 0;
394 if (_go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->new_rmhandler))
396 return 0;
399 /* Setup protected mode handler. */
400 _go32_dpmi_get_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
402 intr->new_pmhandler.pm_selector = _go32_my_cs ();
403 intr->new_pmhandler.pm_offset = (u_long) isr;
404 _go32_dpmi_allocate_iret_wrapper (&intr->new_pmhandler);
406 if (_go32_dpmi_set_protected_mode_interrupt_vector (vec,
407 &intr->new_pmhandler))
409 return 0;
412 /* Setup interrupt controller mask. */
413 disable ();
414 outportb (ICU_MASK, inportb (ICU_MASK) & ~(1 << irq));
415 enable ();
417 intr->inuse = 1;
418 return intr;
422 static void
423 dos_unhookirq (struct intrupt *intr)
425 unsigned int irq, vec;
426 unsigned char mask;
428 irq = intr - intrupts;
429 vec = 0x08 + irq;
431 /* Restore old interrupt mask bit. */
432 mask = 1 << irq;
433 disable ();
434 outportb (ICU_MASK, inportb (ICU_MASK) | (mask & icu_oldmask));
435 enable ();
437 /* Remove real mode handler. */
438 _go32_dpmi_set_real_mode_interrupt_vector (vec, &intr->old_rmhandler);
439 _go32_dpmi_free_real_mode_callback (&intr->new_rmhandler);
441 /* Remove protected mode handler. */
442 _go32_dpmi_set_protected_mode_interrupt_vector (vec, &intr->old_pmhandler);
443 _go32_dpmi_free_iret_wrapper (&intr->new_pmhandler);
444 intr->inuse = 0;
449 static int
450 dos_open (struct serial *scb, const char *name)
452 struct dos_ttystate *port;
453 int fd, i;
455 if (strncasecmp (name, "/dev/", 5) == 0)
456 name += 5;
457 else if (strncasecmp (name, "\\dev\\", 5) == 0)
458 name += 5;
460 if (strlen (name) != 4 || strncasecmp (name, "com", 3) != 0)
462 errno = ENOENT;
463 return -1;
466 if (name[3] < '1' || name[3] > '4')
468 errno = ENOENT;
469 return -1;
472 /* FIXME: this is a Bad Idea (tm)! One should *never* invent file
473 handles, since they might be already used by other files/devices.
474 The Right Way to do this is to create a real handle by dup()'ing
475 some existing one. */
476 fd = name[3] - '1';
477 port = &ports[fd];
478 if (port->refcnt++ > 0)
480 /* Device already opened another user. Just point at it. */
481 scb->fd = fd;
482 return 0;
485 /* Force access to ID reg. */
486 outb (port, com_cfcr, 0);
487 outb (port, com_iir, 0);
488 for (i = 0; i < 17; i++)
490 if ((inb (port, com_iir) & 0x38) == 0)
491 goto ok;
492 (void) inb (port, com_data); /* clear recv */
494 errno = ENODEV;
495 return -1;
498 /* Disable all interrupts in chip. */
499 outb (port, com_ier, 0);
501 /* Tentatively enable 16550 fifo, and see if it responds. */
502 outb (port, com_fifo,
503 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER);
504 sleep (1);
505 port->fifo = ((inb (port, com_iir) & IIR_FIFO_MASK) == IIR_FIFO_MASK);
507 /* clear pending status reports. */
508 (void) inb (port, com_lsr);
509 (void) inb (port, com_msr);
511 /* Enable external interrupt gate (to avoid floating IRQ). */
512 outb (port, com_mcr, MCR_IENABLE);
514 /* Hook up interrupt handler and initialise icu. */
515 port->intrupt = dos_hookirq (port->irq);
516 if (!port->intrupt)
518 outb (port, com_mcr, 0);
519 outb (port, com_fifo, 0);
520 errno = ENODEV;
521 return -1;
524 disable ();
526 /* record port */
527 port->intrupt->port = port;
528 scb->fd = fd;
530 /* Clear rx buffer, tx busy flag and overflow count. */
531 port->first = port->count = 0;
532 port->txbusy = 0;
533 port->oflo = 0;
535 /* Set default baud rate and mode: 9600,8,n,1 */
536 i = dos_baudconv (port->baudrate = 9600);
537 outb (port, com_cfcr, CFCR_DLAB);
538 outb (port, com_dlbl, i & 0xff);
539 outb (port, com_dlbh, i >> 8);
540 outb (port, com_cfcr, CFCR_8BITS);
542 /* Enable all interrupts. */
543 outb (port, com_ier, IER_ETXRDY | IER_ERXRDY | IER_ERLS | IER_EMSC);
545 /* Enable DTR & RTS. */
546 outb (port, com_mcr, MCR_DTR | MCR_RTS | MCR_IENABLE);
548 enable ();
550 return 0;
554 static void
555 dos_close (struct serial *scb)
557 struct dos_ttystate *port;
558 struct intrupt *intrupt;
560 if (!scb)
561 return;
563 port = &ports[scb->fd];
565 if (port->refcnt-- > 1)
566 return;
568 if (!(intrupt = port->intrupt))
569 return;
571 /* Disable interrupts, fifo, flow control. */
572 disable ();
573 port->intrupt = 0;
574 intrupt->port = 0;
575 outb (port, com_fifo, 0);
576 outb (port, com_ier, 0);
577 enable ();
579 /* Unhook handler, and disable interrupt gate. */
580 dos_unhookirq (intrupt);
581 outb (port, com_mcr, 0);
583 /* Check for overflow errors. */
584 if (port->oflo)
586 gdb_printf (gdb_stderr,
587 "Serial input overruns occurred.\n");
588 gdb_printf (gdb_stderr, "This system %s handle %d baud.\n",
589 port->fifo ? "cannot" : "needs a 16550 to",
590 port->baudrate);
595 /* Implementation of the serial_ops flush_output method. */
597 static int
598 dos_flush_output (struct serial *scb)
600 return 0;
603 /* Implementation of the serial_ops setparity method. */
605 static int
606 dos_setparity (struct serial *scb, int parity)
608 return 0;
611 /* Implementation of the serial_ops drain_output method. */
613 static int
614 dos_drain_output (struct serial *scb)
616 return 0;
619 static void
620 dos_raw (struct serial *scb)
622 /* Always in raw mode. */
625 static int
626 dos_readchar (struct serial *scb, int timeout)
628 struct dos_ttystate *port = &ports[scb->fd];
629 long then;
630 int c;
632 then = rawclock () + (timeout * RAWHZ);
633 while ((c = dos_getc (port)) < 0)
635 QUIT;
637 if (timeout >= 0 && (rawclock () - then) >= 0)
638 return SERIAL_TIMEOUT;
641 return c;
645 static serial_ttystate
646 dos_get_tty_state (struct serial *scb)
648 struct dos_ttystate *port = &ports[scb->fd];
649 struct dos_ttystate *state;
651 /* Are they asking about a port we opened? */
652 if (port->refcnt <= 0)
654 /* We've never heard about this port. We should fail this call,
655 unless they are asking about one of the 3 standard handles,
656 in which case we pretend the handle was open by us if it is
657 connected to a terminal device. This is because Unix
658 terminals use the serial interface, so GDB expects the
659 standard handles to go through here. */
660 if (scb->fd >= 3 || !isatty (scb->fd))
661 return NULL;
664 state = XNEW (struct dos_ttystate);
665 *state = *port;
666 return (serial_ttystate) state;
669 static serial_ttystate
670 dos_copy_tty_state (struct serial *scb, serial_ttystate ttystate)
672 struct dos_ttystate *state;
674 state = XNEW (struct dos_ttystate);
675 *state = *(struct dos_ttystate *) ttystate;
677 return (serial_ttystate) state;
680 static int
681 dos_set_tty_state (struct serial *scb, serial_ttystate ttystate)
683 struct dos_ttystate *state;
685 state = (struct dos_ttystate *) ttystate;
686 dos_setbaudrate (scb, state->baudrate);
687 return 0;
690 static int
691 dos_flush_input (struct serial *scb)
693 struct dos_ttystate *port = &ports[scb->fd];
695 disable ();
696 port->first = port->count = 0;
697 if (port->fifo)
698 outb (port, com_fifo, FIFO_ENABLE | FIFO_RCV_RST | FIFO_TRIGGER);
699 enable ();
700 return 0;
703 static void
704 dos_print_tty_state (struct serial *scb, serial_ttystate ttystate,
705 struct ui_file *stream)
707 /* Nothing to print. */
708 return;
711 static int
712 dos_baudconv (int rate)
714 long x, err;
716 if (rate <= 0)
717 return -1;
719 #define divrnd(n, q) (((n) * 2 / (q) + 1) / 2) /* Divide and round off. */
720 x = divrnd (COMTICK, rate);
721 if (x <= 0)
722 return -1;
724 err = divrnd (1000 * COMTICK, x * rate) - 1000;
725 if (err < 0)
726 err = -err;
727 if (err > SPEED_TOLERANCE)
728 return -1;
729 #undef divrnd
730 return x;
734 static int
735 dos_setbaudrate (struct serial *scb, int rate)
737 struct dos_ttystate *port = &ports[scb->fd];
739 if (port->baudrate != rate)
741 int x;
742 unsigned char cfcr;
744 x = dos_baudconv (rate);
745 if (x <= 0)
747 gdb_printf (gdb_stderr, "%d: impossible baudrate\n", rate);
748 errno = EINVAL;
749 return -1;
752 disable ();
753 cfcr = inb (port, com_cfcr);
755 outb (port, com_cfcr, CFCR_DLAB);
756 outb (port, com_dlbl, x & 0xff);
757 outb (port, com_dlbh, x >> 8);
758 outb (port, com_cfcr, cfcr);
759 port->baudrate = rate;
760 enable ();
763 return 0;
766 static int
767 dos_setstopbits (struct serial *scb, int num)
769 struct dos_ttystate *port = &ports[scb->fd];
770 unsigned char cfcr;
772 disable ();
773 cfcr = inb (port, com_cfcr);
775 switch (num)
777 case SERIAL_1_STOPBITS:
778 outb (port, com_cfcr, cfcr & ~CFCR_STOPB);
779 break;
780 case SERIAL_1_AND_A_HALF_STOPBITS:
781 case SERIAL_2_STOPBITS:
782 outb (port, com_cfcr, cfcr | CFCR_STOPB);
783 break;
784 default:
785 enable ();
786 return 1;
788 enable ();
790 return 0;
793 static int
794 dos_write (struct serial *scb, const void *buf, size_t count)
796 volatile struct dos_ttystate *port = &ports[scb->fd];
797 size_t fifosize = port->fifo ? 16 : 1;
798 long then;
799 size_t cnt;
800 const char *str = (const char *) buf;
802 while (count > 0)
804 QUIT;
806 /* Send the data, fifosize bytes at a time. */
807 cnt = fifosize > count ? count : fifosize;
808 port->txbusy = 1;
809 /* Francisco Pastor <fpastor.etra-id@etra.es> says OUTSB messes
810 up the communications with UARTs with FIFOs. */
811 #ifdef UART_FIFO_WORKS
812 outportsb (port->base + com_data, str, cnt);
813 str += cnt;
814 count -= cnt;
815 #else
816 for ( ; cnt > 0; cnt--, count--)
817 outportb (port->base + com_data, *str++);
818 #endif
819 #ifdef DOS_STATS
820 cnts[CNT_TX] += cnt;
821 #endif
822 /* Wait for transmission to complete (max 1 sec). */
823 then = rawclock () + RAWHZ;
824 while (port->txbusy)
826 if ((rawclock () - then) >= 0)
828 errno = EIO;
829 return SERIAL_ERROR;
833 return 0;
837 static int
838 dos_sendbreak (struct serial *scb)
840 volatile struct dos_ttystate *port = &ports[scb->fd];
841 unsigned char cfcr;
842 long then;
844 cfcr = inb (port, com_cfcr);
845 outb (port, com_cfcr, cfcr | CFCR_SBREAK);
847 /* 0.25 sec delay */
848 then = rawclock () + RAWHZ / 4;
849 while ((rawclock () - then) < 0)
850 continue;
852 outb (port, com_cfcr, cfcr);
853 return 0;
857 static const struct serial_ops dos_ops =
859 "hardwire",
860 dos_open,
861 dos_close,
862 NULL, /* fdopen, not implemented */
863 dos_readchar,
864 dos_write,
865 dos_flush_output,
866 dos_flush_input,
867 dos_sendbreak,
868 dos_raw,
869 dos_get_tty_state,
870 dos_copy_tty_state,
871 dos_set_tty_state,
872 dos_print_tty_state,
873 dos_setbaudrate,
874 dos_setstopbits,
875 dos_setparity,
876 dos_drain_output,
877 (void (*)(struct serial *, int))NULL /* Change into async mode. */
881 gdb_pipe (int pdes[2])
883 /* No support for pipes. */
884 errno = ENOSYS;
885 return -1;
888 static void
889 info_serial_command (const char *arg, int from_tty)
891 struct dos_ttystate *port;
892 #ifdef DOS_STATS
893 int i;
894 #endif
896 for (port = ports; port < &ports[4]; port++)
898 if (port->baudrate == 0)
899 continue;
900 gdb_printf ("Port:\tCOM%ld (%sactive)\n", (long)(port - ports) + 1,
901 port->intrupt ? "" : "not ");
902 gdb_printf ("Addr:\t0x%03x (irq %d)\n", port->base, port->irq);
903 gdb_printf ("16550:\t%s\n", port->fifo ? "yes" : "no");
904 gdb_printf ("Speed:\t%d baud\n", port->baudrate);
905 gdb_printf ("Errs:\tframing %d parity %d overflow %d\n\n",
906 port->ferr, port->perr, port->oflo);
909 #ifdef DOS_STATS
910 gdb_printf ("\nTotal interrupts: %d\n", intrcnt);
911 for (i = 0; i < NCNT; i++)
912 if (cnts[i])
913 gdb_printf ("%s:\t%lu\n", cntnames[i], (unsigned long) cnts[i]);
914 #endif
917 void _initialize_ser_dos ();
918 void
919 _initialize_ser_dos ()
921 serial_add_interface (&dos_ops);
923 /* Save original interrupt mask register. */
924 icu_oldmask = inportb (ICU_MASK);
926 /* Mark fixed motherboard irqs as inuse. */
927 intrupts[0].inuse = /* timer tick */
928 intrupts[1].inuse = /* keyboard */
929 intrupts[2].inuse = 1; /* slave icu */
931 add_setshow_zinteger_cmd ("com1base", class_obscure, &ports[0].base, _("\
932 Set COM1 base i/o port address."), _("\
933 Show COM1 base i/o port address."), NULL,
934 NULL,
935 NULL, /* FIXME: i18n: */
936 &setlist, &showlist);
938 add_setshow_zinteger_cmd ("com1irq", class_obscure, &ports[0].irq, _("\
939 Set COM1 interrupt request."), _("\
940 Show COM1 interrupt request."), NULL,
941 NULL,
942 NULL, /* FIXME: i18n: */
943 &setlist, &showlist);
945 add_setshow_zinteger_cmd ("com2base", class_obscure, &ports[1].base, _("\
946 Set COM2 base i/o port address."), _("\
947 Show COM2 base i/o port address."), NULL,
948 NULL,
949 NULL, /* FIXME: i18n: */
950 &setlist, &showlist);
952 add_setshow_zinteger_cmd ("com2irq", class_obscure, &ports[1].irq, _("\
953 Set COM2 interrupt request."), _("\
954 Show COM2 interrupt request."), NULL,
955 NULL,
956 NULL, /* FIXME: i18n: */
957 &setlist, &showlist);
959 add_setshow_zinteger_cmd ("com3base", class_obscure, &ports[2].base, _("\
960 Set COM3 base i/o port address."), _("\
961 Show COM3 base i/o port address."), NULL,
962 NULL,
963 NULL, /* FIXME: i18n: */
964 &setlist, &showlist);
966 add_setshow_zinteger_cmd ("com3irq", class_obscure, &ports[2].irq, _("\
967 Set COM3 interrupt request."), _("\
968 Show COM3 interrupt request."), NULL,
969 NULL,
970 NULL, /* FIXME: i18n: */
971 &setlist, &showlist);
973 add_setshow_zinteger_cmd ("com4base", class_obscure, &ports[3].base, _("\
974 Set COM4 base i/o port address."), _("\
975 Show COM4 base i/o port address."), NULL,
976 NULL,
977 NULL, /* FIXME: i18n: */
978 &setlist, &showlist);
980 add_setshow_zinteger_cmd ("com4irq", class_obscure, &ports[3].irq, _("\
981 Set COM4 interrupt request."), _("\
982 Show COM4 interrupt request."), NULL,
983 NULL,
984 NULL, /* FIXME: i18n: */
985 &setlist, &showlist);
987 add_info ("serial", info_serial_command,
988 _("Print DOS serial port status."));