| 1 | /* Remote serial interface for local (hardwired) serial ports for GO32. |
| 2 | Copyright 1992, 1993 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GDB. |
| 5 | |
| 6 | This program is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 2 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | This program is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program; if not, write to the Free Software |
| 18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 19 | |
| 20 | #include "defs.h" |
| 21 | #include "serial.h" |
| 22 | #include <sys/dos.h> |
| 23 | |
| 24 | /* This is unused for now. We just return a placeholder. */ |
| 25 | struct go32_ttystate |
| 26 | { |
| 27 | int bogus; |
| 28 | }; |
| 29 | |
| 30 | typedef struct |
| 31 | { |
| 32 | short jmp_op; |
| 33 | short signature; |
| 34 | short version; |
| 35 | short buffer_start; |
| 36 | short buffer_end; |
| 37 | short getp; |
| 38 | short putp; |
| 39 | short iov; |
| 40 | } |
| 41 | ASYNC_STRUCT; |
| 42 | |
| 43 | static int go32_open PARAMS ((serial_t scb, const char *name)); |
| 44 | static void go32_raw PARAMS ((serial_t scb)); |
| 45 | static int go32_readchar PARAMS ((serial_t scb, int timeout)); |
| 46 | static int go32_setbaudrate PARAMS ((serial_t scb, int rate)); |
| 47 | static int go32_write PARAMS ((serial_t scb, const char *str, int len)); |
| 48 | static void go32_close PARAMS ((serial_t scb)); |
| 49 | static serial_ttystate go32_get_tty_state PARAMS ((serial_t scb)); |
| 50 | static int go32_set_tty_state PARAMS ((serial_t scb, serial_ttystate state)); |
| 51 | static char *aptr PARAMS ((short p)); |
| 52 | static ASYNC_STRUCT *getivec PARAMS ((int which)); |
| 53 | static int dos_async_init PARAMS ((int port)); |
| 54 | static void dos_async_tx PARAMS ((const char c)); |
| 55 | static int dos_async_rx PARAMS (()); |
| 56 | static int dosasync_read PARAMS ((int fd, char *buf, int len, int timeout)); |
| 57 | static int dosasync_write PARAMS ((int fd, const char *buf, int len)); |
| 58 | |
| 59 | #define SIGNATURE 0x4154 |
| 60 | #define VERSION 1 |
| 61 | #define OFFSET 0x104 |
| 62 | |
| 63 | #define peek(a,b) (*(unsigned short *)(0xe0000000 + (a)*16 + (b))) |
| 64 | |
| 65 | static volatile ASYNC_STRUCT *async; |
| 66 | static int iov; |
| 67 | #define com_rb iov |
| 68 | #define com_tb iov |
| 69 | #define com_ier iov+1 |
| 70 | #define com_ifr iov+2 |
| 71 | #define com_bfr iov+3 |
| 72 | #define com_mcr iov+4 |
| 73 | #define com_lsr iov+5 |
| 74 | #define com_msr iov+6 |
| 75 | |
| 76 | static char * |
| 77 | aptr (p) |
| 78 | short p; |
| 79 | { |
| 80 | return (char *) ((unsigned) async - OFFSET + p); |
| 81 | } |
| 82 | |
| 83 | static ASYNC_STRUCT * |
| 84 | getivec (int which) |
| 85 | { |
| 86 | ASYNC_STRUCT *a; |
| 87 | |
| 88 | if (peek (0, which * 4) != OFFSET) |
| 89 | return 0; |
| 90 | |
| 91 | a = (ASYNC_STRUCT *) (0xe0000000 + peek (0, which * 4 + 2) * 16 + peek (0, which * 4)); |
| 92 | |
| 93 | if (a->signature != SIGNATURE) |
| 94 | return 0; |
| 95 | |
| 96 | if (a->version != VERSION) |
| 97 | return 0; |
| 98 | |
| 99 | return a; |
| 100 | } |
| 101 | |
| 102 | static int |
| 103 | dos_async_init (port) |
| 104 | int port; |
| 105 | { |
| 106 | switch (port) |
| 107 | { |
| 108 | case 1: |
| 109 | async = getivec (12); |
| 110 | break; |
| 111 | case 2: |
| 112 | async = getivec (11); |
| 113 | break; |
| 114 | default: |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | if (!async) |
| 119 | { |
| 120 | error ("GDB cannot connect to asynctsr program, check that it is installed\n\ |
| 121 | and that serial I/O is not being redirected (perhaps by NFS)\n\n\ |
| 122 | example configuration:\n\ |
| 123 | C> mode com%d:9600,n,8,1,p\n\ |
| 124 | C> asynctsr %d\n\ |
| 125 | C> gdb \n", port, port); |
| 126 | } |
| 127 | |
| 128 | iov = async->iov; |
| 129 | outportb (com_ier, 0x0f); |
| 130 | outportb (com_bfr, 0x03); |
| 131 | outportb (com_mcr, 0x0b); |
| 132 | async->getp = async->putp = async->buffer_start; |
| 133 | |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | static void |
| 138 | dos_async_tx (c) |
| 139 | const char c; |
| 140 | { |
| 141 | while (~inportb (com_lsr) & 0x20) |
| 142 | ; |
| 143 | outportb (com_tb, c); |
| 144 | } |
| 145 | |
| 146 | #define dos_async_ready() (async->getp != async->putp) |
| 147 | |
| 148 | static int |
| 149 | dos_async_rx () |
| 150 | { |
| 151 | char rv; |
| 152 | |
| 153 | while (!dos_async_ready ()) |
| 154 | { |
| 155 | if (kbhit ()) |
| 156 | { |
| 157 | printf_unfiltered ("abort!\n"); |
| 158 | return 0; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | rv = *aptr (async->getp++); |
| 163 | if (async->getp >= async->buffer_end) |
| 164 | async->getp = async->buffer_start; |
| 165 | |
| 166 | return rv; |
| 167 | } |
| 168 | |
| 169 | static int |
| 170 | dosasync_read (fd, buf, len, timeout) |
| 171 | int fd; |
| 172 | char *buf; |
| 173 | int len; |
| 174 | int timeout; |
| 175 | { |
| 176 | long now, then; |
| 177 | int i; |
| 178 | |
| 179 | time (&now); |
| 180 | then = now + timeout; |
| 181 | |
| 182 | for (i = 0; i < len; i++) |
| 183 | { |
| 184 | if (timeout) |
| 185 | { |
| 186 | while (!dos_async_ready ()) |
| 187 | { |
| 188 | time (&now); |
| 189 | if (now >= then) |
| 190 | return i; |
| 191 | } |
| 192 | } |
| 193 | *buf++ = dos_async_rx (); |
| 194 | } |
| 195 | return len; |
| 196 | } |
| 197 | |
| 198 | static int |
| 199 | dosasync_write (fd, buf, len) |
| 200 | int fd; |
| 201 | const char *buf; |
| 202 | int len; |
| 203 | { |
| 204 | int l; |
| 205 | |
| 206 | for (l = 0; l < len; l++) |
| 207 | dos_async_tx (*buf++); |
| 208 | |
| 209 | return len; |
| 210 | } |
| 211 | |
| 212 | static int |
| 213 | go32_open (scb, name) |
| 214 | serial_t scb; |
| 215 | const char *name; |
| 216 | { |
| 217 | int port; |
| 218 | |
| 219 | if (strncasecmp (name, "com", 3) != 0) |
| 220 | { |
| 221 | errno = ENOENT; |
| 222 | return -1; |
| 223 | } |
| 224 | |
| 225 | port = name[3] - '0'; |
| 226 | |
| 227 | if ((port != 1) && (port != 2)) |
| 228 | { |
| 229 | errno = ENOENT; |
| 230 | return -11; |
| 231 | } |
| 232 | |
| 233 | scb->fd = dos_async_init (port); |
| 234 | if (!scb->fd) |
| 235 | return -1; |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static int |
| 241 | go32_noop (scb) |
| 242 | serial_t scb; |
| 243 | { |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static void |
| 248 | go32_raw (scb) |
| 249 | serial_t scb; |
| 250 | { |
| 251 | /* Always in raw mode */ |
| 252 | } |
| 253 | |
| 254 | static int |
| 255 | go32_readchar (scb, timeout) |
| 256 | serial_t scb; |
| 257 | int timeout; |
| 258 | { |
| 259 | char buf; |
| 260 | |
| 261 | /* Shortcut for polling */ |
| 262 | if (timeout == 0) |
| 263 | { |
| 264 | if (dos_async_ready ()) |
| 265 | { |
| 266 | return dos_async_rx (); |
| 267 | } |
| 268 | return SERIAL_TIMEOUT; |
| 269 | } |
| 270 | |
| 271 | if (dosasync_read (scb->fd, &buf, 1, timeout)) |
| 272 | return buf; |
| 273 | else |
| 274 | return SERIAL_TIMEOUT; |
| 275 | } |
| 276 | |
| 277 | /* go32_{get set}_tty_state() are both dummys to fill out the function |
| 278 | vector. Someday, they may do something real... */ |
| 279 | |
| 280 | static serial_ttystate |
| 281 | go32_get_tty_state (scb) |
| 282 | serial_t scb; |
| 283 | { |
| 284 | struct go32_ttystate *state; |
| 285 | |
| 286 | state = (struct go32_ttystate *) xmalloc (sizeof *state); |
| 287 | |
| 288 | return (serial_ttystate) state; |
| 289 | } |
| 290 | |
| 291 | static int |
| 292 | go32_set_tty_state (scb, ttystate) |
| 293 | serial_t scb; |
| 294 | serial_ttystate ttystate; |
| 295 | { |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int |
| 300 | go32_noflush_set_tty_state (scb, new_ttystate, old_ttystate) |
| 301 | serial_t scb; |
| 302 | serial_ttystate new_ttystate; |
| 303 | serial_ttystate old_ttystate; |
| 304 | { |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static void |
| 309 | go32_print_tty_state (scb, ttystate) |
| 310 | serial_t scb; |
| 311 | serial_ttystate ttystate; |
| 312 | { |
| 313 | /* Nothing to print. */ |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | static int |
| 318 | go32_setbaudrate (scb, rate) |
| 319 | serial_t scb; |
| 320 | int rate; |
| 321 | { |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | static int |
| 326 | go32_write (scb, str, len) |
| 327 | serial_t scb; |
| 328 | const char *str; |
| 329 | int len; |
| 330 | { |
| 331 | dosasync_write (scb->fd, str, len); |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static void |
| 337 | go32_close (scb) |
| 338 | serial_t scb; |
| 339 | { |
| 340 | } |
| 341 | |
| 342 | static struct serial_ops go32_ops = |
| 343 | { |
| 344 | "hardwire", |
| 345 | 0, |
| 346 | go32_open, |
| 347 | go32_close, |
| 348 | go32_readchar, |
| 349 | go32_write, |
| 350 | go32_noop, /* flush output */ |
| 351 | go32_noop, /* flush input */ |
| 352 | go32_noop, /* send break -- currently used only for nindy */ |
| 353 | go32_raw, |
| 354 | go32_get_tty_state, |
| 355 | go32_set_tty_state, |
| 356 | go32_print_tty_state, |
| 357 | go32_noflush_set_tty_state, |
| 358 | go32_setbaudrate, |
| 359 | }; |
| 360 | |
| 361 | void |
| 362 | _initialize_ser_go32 () |
| 363 | { |
| 364 | serial_add_interface (&go32_ops); |
| 365 | } |