OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.3/] [gdb/] [ser-unix.c] - Blame information for rev 1775

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1181 sfurman
/* Serial interface for local (hardwired) serial ports on Un*x like systems
2
   Copyright 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001
3
   Free Software Foundation, Inc.
4
 
5
   This file is part of GDB.
6
 
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 2 of the License, or
10
   (at your option) any later version.
11
 
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
 
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 59 Temple Place - Suite 330,
20
   Boston, MA 02111-1307, USA.  */
21
 
22
#include "defs.h"
23
#include "serial.h"
24
#include "ser-unix.h"
25
 
26
#include <fcntl.h>
27
#include <sys/types.h>
28
#include "terminal.h"
29
#include <sys/socket.h>
30
#include <sys/time.h>
31
 
32
#include "gdb_string.h"
33
#include "event-loop.h"
34
 
35
#ifdef HAVE_TERMIOS
36
 
37
struct hardwire_ttystate
38
  {
39
    struct termios termios;
40
  };
41
#endif /* termios */
42
 
43
#ifdef HAVE_TERMIO
44
 
45
/* It is believed that all systems which have added job control to SVR3
46
   (e.g. sco) have also added termios.  Even if not, trying to figure out
47
   all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
48
   bewildering.  So we don't attempt it.  */
49
 
50
struct hardwire_ttystate
51
  {
52
    struct termio termio;
53
  };
54
#endif /* termio */
55
 
56
#ifdef HAVE_SGTTY
57
struct hardwire_ttystate
58
  {
59
    struct sgttyb sgttyb;
60
    struct tchars tc;
61
    struct ltchars ltc;
62
    /* Line discipline flags.  */
63
    int lmode;
64
  };
65
#endif /* sgtty */
66
 
67
static int hardwire_open (struct serial *scb, const char *name);
68
static void hardwire_raw (struct serial *scb);
69
static int wait_for (struct serial *scb, int timeout);
70
static int hardwire_readchar (struct serial *scb, int timeout);
71
static int do_hardwire_readchar (struct serial *scb, int timeout);
72
static int generic_readchar (struct serial *scb, int timeout,
73
                             int (*do_readchar) (struct serial *scb,
74
                                                 int timeout));
75
static int rate_to_code (int rate);
76
static int hardwire_setbaudrate (struct serial *scb, int rate);
77
static void hardwire_close (struct serial *scb);
78
static int get_tty_state (struct serial *scb,
79
                          struct hardwire_ttystate * state);
80
static int set_tty_state (struct serial *scb,
81
                          struct hardwire_ttystate * state);
82
static serial_ttystate hardwire_get_tty_state (struct serial *scb);
83
static int hardwire_set_tty_state (struct serial *scb, serial_ttystate state);
84
static int hardwire_noflush_set_tty_state (struct serial *, serial_ttystate,
85
                                           serial_ttystate);
86
static void hardwire_print_tty_state (struct serial *, serial_ttystate,
87
                                      struct ui_file *);
88
static int hardwire_drain_output (struct serial *);
89
static int hardwire_flush_output (struct serial *);
90
static int hardwire_flush_input (struct serial *);
91
static int hardwire_send_break (struct serial *);
92
static int hardwire_setstopbits (struct serial *, int);
93
 
94
static int do_unix_readchar (struct serial *scb, int timeout);
95
static timer_handler_func push_event;
96
static handler_func fd_event;
97
static void reschedule (struct serial *scb);
98
 
99
void _initialize_ser_hardwire (void);
100
 
101
extern int (*ui_loop_hook) (int);
102
 
103
/* Open up a real live device for serial I/O */
104
 
105
static int
106
hardwire_open (struct serial *scb, const char *name)
107
{
108
  scb->fd = open (name, O_RDWR);
109
  if (scb->fd < 0)
110
    return -1;
111
 
112
  return 0;
113
}
114
 
115
static int
116
get_tty_state (struct serial *scb, struct hardwire_ttystate *state)
117
{
118
#ifdef HAVE_TERMIOS
119
  if (tcgetattr (scb->fd, &state->termios) < 0)
120
    return -1;
121
 
122
  return 0;
123
#endif
124
 
125
#ifdef HAVE_TERMIO
126
  if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
127
    return -1;
128
  return 0;
129
#endif
130
 
131
#ifdef HAVE_SGTTY
132
  if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
133
    return -1;
134
  if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
135
    return -1;
136
  if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
137
    return -1;
138
  if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
139
    return -1;
140
 
141
  return 0;
142
#endif
143
}
144
 
145
static int
146
set_tty_state (struct serial *scb, struct hardwire_ttystate *state)
147
{
148
#ifdef HAVE_TERMIOS
149
  if (tcsetattr (scb->fd, TCSANOW, &state->termios) < 0)
150
    return -1;
151
 
152
  return 0;
153
#endif
154
 
155
#ifdef HAVE_TERMIO
156
  if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
157
    return -1;
158
  return 0;
159
#endif
160
 
161
#ifdef HAVE_SGTTY
162
  if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
163
    return -1;
164
  if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
165
    return -1;
166
  if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
167
    return -1;
168
  if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
169
    return -1;
170
 
171
  return 0;
172
#endif
173
}
174
 
175
static serial_ttystate
176
hardwire_get_tty_state (struct serial *scb)
177
{
178
  struct hardwire_ttystate *state;
179
 
180
  state = (struct hardwire_ttystate *) xmalloc (sizeof *state);
181
 
182
  if (get_tty_state (scb, state))
183
    return NULL;
184
 
185
  return (serial_ttystate) state;
186
}
187
 
188
static int
189
hardwire_set_tty_state (struct serial *scb, serial_ttystate ttystate)
190
{
191
  struct hardwire_ttystate *state;
192
 
193
  state = (struct hardwire_ttystate *) ttystate;
194
 
195
  return set_tty_state (scb, state);
196
}
197
 
198
static int
199
hardwire_noflush_set_tty_state (struct serial *scb,
200
                                serial_ttystate new_ttystate,
201
                                serial_ttystate old_ttystate)
202
{
203
  struct hardwire_ttystate new_state;
204
#ifdef HAVE_SGTTY
205
  struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
206
#endif
207
 
208
  new_state = *(struct hardwire_ttystate *) new_ttystate;
209
 
210
  /* Don't change in or out of raw mode; we don't want to flush input.
211
     termio and termios have no such restriction; for them flushing input
212
     is separate from setting the attributes.  */
213
 
214
#ifdef HAVE_SGTTY
215
  if (state->sgttyb.sg_flags & RAW)
216
    new_state.sgttyb.sg_flags |= RAW;
217
  else
218
    new_state.sgttyb.sg_flags &= ~RAW;
219
 
220
  /* I'm not sure whether this is necessary; the manpage just mentions
221
     RAW not CBREAK.  */
222
  if (state->sgttyb.sg_flags & CBREAK)
223
    new_state.sgttyb.sg_flags |= CBREAK;
224
  else
225
    new_state.sgttyb.sg_flags &= ~CBREAK;
226
#endif
227
 
228
  return set_tty_state (scb, &new_state);
229
}
230
 
231
static void
232
hardwire_print_tty_state (struct serial *scb,
233
                          serial_ttystate ttystate,
234
                          struct ui_file *stream)
235
{
236
  struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
237
  int i;
238
 
239
#ifdef HAVE_TERMIOS
240
  fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
241
                    (int) state->termios.c_iflag,
242
                    (int) state->termios.c_oflag);
243
  fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x\n",
244
                    (int) state->termios.c_cflag,
245
                    (int) state->termios.c_lflag);
246
#if 0
247
  /* This not in POSIX, and is not really documented by those systems
248
     which have it (at least not Sun).  */
249
  fprintf_filtered (stream, "c_line = 0x%x.\n", state->termios.c_line);
250
#endif
251
  fprintf_filtered (stream, "c_cc: ");
252
  for (i = 0; i < NCCS; i += 1)
253
    fprintf_filtered (stream, "0x%x ", state->termios.c_cc[i]);
254
  fprintf_filtered (stream, "\n");
255
#endif
256
 
257
#ifdef HAVE_TERMIO
258
  fprintf_filtered (stream, "c_iflag = 0x%x, c_oflag = 0x%x,\n",
259
                    state->termio.c_iflag, state->termio.c_oflag);
260
  fprintf_filtered (stream, "c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
261
                    state->termio.c_cflag, state->termio.c_lflag,
262
                    state->termio.c_line);
263
  fprintf_filtered (stream, "c_cc: ");
264
  for (i = 0; i < NCC; i += 1)
265
    fprintf_filtered (stream, "0x%x ", state->termio.c_cc[i]);
266
  fprintf_filtered (stream, "\n");
267
#endif
268
 
269
#ifdef HAVE_SGTTY
270
  fprintf_filtered (stream, "sgttyb.sg_flags = 0x%x.\n",
271
                    state->sgttyb.sg_flags);
272
 
273
  fprintf_filtered (stream, "tchars: ");
274
  for (i = 0; i < (int) sizeof (struct tchars); i++)
275
    fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->tc)[i]);
276
  fprintf_filtered (stream, "\n");
277
 
278
  fprintf_filtered (stream, "ltchars: ");
279
  for (i = 0; i < (int) sizeof (struct ltchars); i++)
280
    fprintf_filtered (stream, "0x%x ", ((unsigned char *) &state->ltc)[i]);
281
  fprintf_filtered (stream, "\n");
282
 
283
  fprintf_filtered (stream, "lmode:  0x%x\n", state->lmode);
284
#endif
285
}
286
 
287
/* Wait for the output to drain away, as opposed to flushing (discarding) it */
288
 
289
static int
290
hardwire_drain_output (struct serial *scb)
291
{
292
#ifdef HAVE_TERMIOS
293
  return tcdrain (scb->fd);
294
#endif
295
 
296
#ifdef HAVE_TERMIO
297
  return ioctl (scb->fd, TCSBRK, 1);
298
#endif
299
 
300
#ifdef HAVE_SGTTY
301
  /* Get the current state and then restore it using TIOCSETP,
302
     which should cause the output to drain and pending input
303
     to be discarded. */
304
  {
305
    struct hardwire_ttystate state;
306
    if (get_tty_state (scb, &state))
307
      {
308
        return (-1);
309
      }
310
    else
311
      {
312
        return (ioctl (scb->fd, TIOCSETP, &state.sgttyb));
313
      }
314
  }
315
#endif
316
}
317
 
318
static int
319
hardwire_flush_output (struct serial *scb)
320
{
321
#ifdef HAVE_TERMIOS
322
  return tcflush (scb->fd, TCOFLUSH);
323
#endif
324
 
325
#ifdef HAVE_TERMIO
326
  return ioctl (scb->fd, TCFLSH, 1);
327
#endif
328
 
329
#ifdef HAVE_SGTTY
330
  /* This flushes both input and output, but we can't do better.  */
331
  return ioctl (scb->fd, TIOCFLUSH, 0);
332
#endif
333
}
334
 
335
static int
336
hardwire_flush_input (struct serial *scb)
337
{
338
  ser_unix_flush_input (scb);
339
 
340
#ifdef HAVE_TERMIOS
341
  return tcflush (scb->fd, TCIFLUSH);
342
#endif
343
 
344
#ifdef HAVE_TERMIO
345
  return ioctl (scb->fd, TCFLSH, 0);
346
#endif
347
 
348
#ifdef HAVE_SGTTY
349
  /* This flushes both input and output, but we can't do better.  */
350
  return ioctl (scb->fd, TIOCFLUSH, 0);
351
#endif
352
}
353
 
354
static int
355
hardwire_send_break (struct serial *scb)
356
{
357
#ifdef HAVE_TERMIOS
358
  return tcsendbreak (scb->fd, 0);
359
#endif
360
 
361
#ifdef HAVE_TERMIO
362
  return ioctl (scb->fd, TCSBRK, 0);
363
#endif
364
 
365
#ifdef HAVE_SGTTY
366
  {
367
    int status;
368
    struct timeval timeout;
369
 
370
    status = ioctl (scb->fd, TIOCSBRK, 0);
371
 
372
    /* Can't use usleep; it doesn't exist in BSD 4.2.  */
373
    /* Note that if this select() is interrupted by a signal it will not wait
374
       the full length of time.  I think that is OK.  */
375
    timeout.tv_sec = 0;
376
    timeout.tv_usec = 250000;
377
    select (0, 0, 0, 0, &timeout);
378
    status = ioctl (scb->fd, TIOCCBRK, 0);
379
    return status;
380
  }
381
#endif
382
}
383
 
384
static void
385
hardwire_raw (struct serial *scb)
386
{
387
  struct hardwire_ttystate state;
388
 
389
  if (get_tty_state (scb, &state))
390
    fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
391
 
392
#ifdef HAVE_TERMIOS
393
  state.termios.c_iflag = 0;
394
  state.termios.c_oflag = 0;
395
  state.termios.c_lflag = 0;
396
  state.termios.c_cflag &= ~(CSIZE | PARENB);
397
  state.termios.c_cflag |= CLOCAL | CS8;
398
  state.termios.c_cc[VMIN] = 0;
399
  state.termios.c_cc[VTIME] = 0;
400
#endif
401
 
402
#ifdef HAVE_TERMIO
403
  state.termio.c_iflag = 0;
404
  state.termio.c_oflag = 0;
405
  state.termio.c_lflag = 0;
406
  state.termio.c_cflag &= ~(CSIZE | PARENB);
407
  state.termio.c_cflag |= CLOCAL | CS8;
408
  state.termio.c_cc[VMIN] = 0;
409
  state.termio.c_cc[VTIME] = 0;
410
#endif
411
 
412
#ifdef HAVE_SGTTY
413
  state.sgttyb.sg_flags |= RAW | ANYP;
414
  state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
415
#endif
416
 
417
  scb->current_timeout = 0;
418
 
419
  if (set_tty_state (scb, &state))
420
    fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
421
}
422
 
423
/* Wait for input on scb, with timeout seconds.  Returns 0 on success,
424
   otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
425
 
426
   For termio{s}, we actually just setup VTIME if necessary, and let the
427
   timeout occur in the read() in hardwire_read().
428
 */
429
 
430
/* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
431
   ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
432
   flushed. . */
433
 
434
/* NOTE: cagney/1999-09-30: Much of the code below is dead.  The only
435
   possible values of the TIMEOUT parameter are ONE and ZERO.
436
   Consequently all the code that tries to handle the possability of
437
   an overflowed timer is unnecessary. */
438
 
439
static int
440
wait_for (struct serial *scb, int timeout)
441
{
442
#ifdef HAVE_SGTTY
443
  while (1)
444
    {
445
      struct timeval tv;
446
      fd_set readfds;
447
      int numfds;
448
 
449
      /* NOTE: Some OS's can scramble the READFDS when the select()
450
         call fails (ex the kernel with Red Hat 5.2).  Initialize all
451
         arguments before each call. */
452
 
453
      tv.tv_sec = timeout;
454
      tv.tv_usec = 0;
455
 
456
      FD_ZERO (&readfds);
457
      FD_SET (scb->fd, &readfds);
458
 
459
      if (timeout >= 0)
460
        numfds = select (scb->fd + 1, &readfds, 0, 0, &tv);
461
      else
462
        numfds = select (scb->fd + 1, &readfds, 0, 0, 0);
463
 
464
      if (numfds <= 0)
465
        if (numfds == 0)
466
          return SERIAL_TIMEOUT;
467
        else if (errno == EINTR)
468
          continue;
469
        else
470
          return SERIAL_ERROR;  /* Got an error from select or poll */
471
 
472
      return 0;
473
    }
474
#endif /* HAVE_SGTTY */
475
 
476
#if defined HAVE_TERMIO || defined HAVE_TERMIOS
477
  if (timeout == scb->current_timeout)
478
    return 0;
479
 
480
  scb->current_timeout = timeout;
481
 
482
  {
483
    struct hardwire_ttystate state;
484
 
485
    if (get_tty_state (scb, &state))
486
      fprintf_unfiltered (gdb_stderr, "get_tty_state failed: %s\n", safe_strerror (errno));
487
 
488
#ifdef HAVE_TERMIOS
489
    if (timeout < 0)
490
      {
491
        /* No timeout.  */
492
        state.termios.c_cc[VTIME] = 0;
493
        state.termios.c_cc[VMIN] = 1;
494
      }
495
    else
496
      {
497
        state.termios.c_cc[VMIN] = 0;
498
        state.termios.c_cc[VTIME] = timeout * 10;
499
        if (state.termios.c_cc[VTIME] != timeout * 10)
500
          {
501
 
502
            /* If c_cc is an 8-bit signed character, we can't go
503
               bigger than this.  If it is always unsigned, we could use
504
               25.  */
505
 
506
            scb->current_timeout = 12;
507
            state.termios.c_cc[VTIME] = scb->current_timeout * 10;
508
            scb->timeout_remaining = timeout - scb->current_timeout;
509
          }
510
      }
511
#endif
512
 
513
#ifdef HAVE_TERMIO
514
    if (timeout < 0)
515
      {
516
        /* No timeout.  */
517
        state.termio.c_cc[VTIME] = 0;
518
        state.termio.c_cc[VMIN] = 1;
519
      }
520
    else
521
      {
522
        state.termio.c_cc[VMIN] = 0;
523
        state.termio.c_cc[VTIME] = timeout * 10;
524
        if (state.termio.c_cc[VTIME] != timeout * 10)
525
          {
526
            /* If c_cc is an 8-bit signed character, we can't go
527
               bigger than this.  If it is always unsigned, we could use
528
               25.  */
529
 
530
            scb->current_timeout = 12;
531
            state.termio.c_cc[VTIME] = scb->current_timeout * 10;
532
            scb->timeout_remaining = timeout - scb->current_timeout;
533
          }
534
      }
535
#endif
536
 
537
    if (set_tty_state (scb, &state))
538
      fprintf_unfiltered (gdb_stderr, "set_tty_state failed: %s\n", safe_strerror (errno));
539
 
540
    return 0;
541
  }
542
#endif /* HAVE_TERMIO || HAVE_TERMIOS */
543
}
544
 
545
/* Read a character with user-specified timeout.  TIMEOUT is number of seconds
546
   to wait, or -1 to wait forever.  Use timeout of 0 to effect a poll.  Returns
547
   char if successful.  Returns SERIAL_TIMEOUT if timeout expired, EOF if line
548
   dropped dead, or SERIAL_ERROR for any other error (see errno in that case).  */
549
 
550
/* FIXME: cagney/1999-09-16: Don't replace this with the equivalent
551
   ser_unix*() until the old TERMIOS/SGTTY/... timer code has been
552
   flushed. */
553
 
554
/* NOTE: cagney/1999-09-16: This function is not identical to
555
   ser_unix_readchar() as part of replacing it with ser_unix*()
556
   merging will be required - this code handles the case where read()
557
   times out due to no data while ser_unix_readchar() doesn't expect
558
   that. */
559
 
560
static int
561
do_hardwire_readchar (struct serial *scb, int timeout)
562
{
563
  int status, delta;
564
  int detach = 0;
565
 
566
  if (timeout > 0)
567
    timeout++;
568
 
569
  /* We have to be able to keep the GUI alive here, so we break the original
570
     timeout into steps of 1 second, running the "keep the GUI alive" hook
571
     each time through the loop.
572
     Also, timeout = 0 means to poll, so we just set the delta to 0, so we
573
     will only go through the loop once. */
574
 
575
  delta = (timeout == 0 ? 0 : 1);
576
  while (1)
577
    {
578
 
579
      /* N.B. The UI may destroy our world (for instance by calling
580
         remote_stop,) in which case we want to get out of here as
581
         quickly as possible.  It is not safe to touch scb, since
582
         someone else might have freed it.  The ui_loop_hook signals that
583
         we should exit by returning 1. */
584
 
585
      if (ui_loop_hook)
586
        detach = ui_loop_hook (0);
587
 
588
      if (detach)
589
        return SERIAL_TIMEOUT;
590
 
591
      scb->timeout_remaining = (timeout < 0 ? timeout : timeout - delta);
592
      status = wait_for (scb, delta);
593
 
594
      if (status < 0)
595
        return status;
596
 
597
      status = read (scb->fd, scb->buf, BUFSIZ);
598
 
599
      if (status <= 0)
600
        {
601
          if (status == 0)
602
            {
603
              /* Zero characters means timeout (it could also be EOF, but
604
                 we don't (yet at least) distinguish).  */
605
              if (scb->timeout_remaining > 0)
606
                {
607
                  timeout = scb->timeout_remaining;
608
                  continue;
609
                }
610
              else if (scb->timeout_remaining < 0)
611
                continue;
612
              else
613
                return SERIAL_TIMEOUT;
614
            }
615
          else if (errno == EINTR)
616
            continue;
617
          else
618
            return SERIAL_ERROR;        /* Got an error from read */
619
        }
620
 
621
      scb->bufcnt = status;
622
      scb->bufcnt--;
623
      scb->bufp = scb->buf;
624
      return *scb->bufp++;
625
    }
626
}
627
 
628
static int
629
hardwire_readchar (struct serial *scb, int timeout)
630
{
631
  return generic_readchar (scb, timeout, do_hardwire_readchar);
632
}
633
 
634
 
635
#ifndef B19200
636
#define B19200 EXTA
637
#endif
638
 
639
#ifndef B38400
640
#define B38400 EXTB
641
#endif
642
 
643
/* Translate baud rates from integers to damn B_codes.  Unix should
644
   have outgrown this crap years ago, but even POSIX wouldn't buck it.  */
645
 
646
static struct
647
{
648
  int rate;
649
  int code;
650
}
651
baudtab[] =
652
{
653
  {
654
    50, B50
655
  }
656
  ,
657
  {
658
    75, B75
659
  }
660
  ,
661
  {
662
    110, B110
663
  }
664
  ,
665
  {
666
    134, B134
667
  }
668
  ,
669
  {
670
    150, B150
671
  }
672
  ,
673
  {
674
    200, B200
675
  }
676
  ,
677
  {
678
    300, B300
679
  }
680
  ,
681
  {
682
    600, B600
683
  }
684
  ,
685
  {
686
    1200, B1200
687
  }
688
  ,
689
  {
690
    1800, B1800
691
  }
692
  ,
693
  {
694
    2400, B2400
695
  }
696
  ,
697
  {
698
    4800, B4800
699
  }
700
  ,
701
  {
702
    9600, B9600
703
  }
704
  ,
705
  {
706
    19200, B19200
707
  }
708
  ,
709
  {
710
    38400, B38400
711
  }
712
  ,
713
#ifdef B57600
714
  {
715
    57600, B57600
716
  }
717
  ,
718
#endif
719
#ifdef B115200
720
  {
721
    115200, B115200
722
  }
723
  ,
724
#endif
725
#ifdef B230400
726
  {
727
    230400, B230400
728
  }
729
  ,
730
#endif
731
#ifdef B460800
732
  {
733
    460800, B460800
734
  }
735
  ,
736
#endif
737
  {
738
    -1, -1
739
  }
740
  ,
741
};
742
 
743
static int
744
rate_to_code (int rate)
745
{
746
  int i;
747
 
748
  for (i = 0; baudtab[i].rate != -1; i++)
749
    {
750
      /* test for perfect macth. */
751
      if (rate == baudtab[i].rate)
752
        return baudtab[i].code;
753
      else
754
        {
755
          /* check if it is in between valid values. */
756
          if (rate < baudtab[i].rate)
757
            {
758
              if (i)
759
                {
760
                  warning ("Invalid baud rate %d.  Closest values are %d and %d.",
761
                            rate, baudtab[i - 1].rate, baudtab[i].rate);
762
                }
763
              else
764
                {
765
                  warning ("Invalid baud rate %d.  Minimum value is %d.",
766
                            rate, baudtab[0].rate);
767
                }
768
              return -1;
769
            }
770
        }
771
    }
772
 
773
  /* The requested speed was too large. */
774
  warning ("Invalid baud rate %d.  Maximum value is %d.",
775
            rate, baudtab[i - 1].rate);
776
  return -1;
777
}
778
 
779
static int
780
hardwire_setbaudrate (struct serial *scb, int rate)
781
{
782
  struct hardwire_ttystate state;
783
  int baud_code = rate_to_code (rate);
784
 
785
  if (baud_code < 0)
786
    {
787
      /* The baud rate was not valid.
788
         A warning has already been issued. */
789
      errno = EINVAL;
790
      return -1;
791
    }
792
 
793
  if (get_tty_state (scb, &state))
794
    return -1;
795
 
796
#ifdef HAVE_TERMIOS
797
  cfsetospeed (&state.termios, baud_code);
798
  cfsetispeed (&state.termios, baud_code);
799
#endif
800
 
801
#ifdef HAVE_TERMIO
802
#ifndef CIBAUD
803
#define CIBAUD CBAUD
804
#endif
805
 
806
  state.termio.c_cflag &= ~(CBAUD | CIBAUD);
807
  state.termio.c_cflag |= baud_code;
808
#endif
809
 
810
#ifdef HAVE_SGTTY
811
  state.sgttyb.sg_ispeed = baud_code;
812
  state.sgttyb.sg_ospeed = baud_code;
813
#endif
814
 
815
  return set_tty_state (scb, &state);
816
}
817
 
818
static int
819
hardwire_setstopbits (struct serial *scb, int num)
820
{
821
  struct hardwire_ttystate state;
822
  int newbit;
823
 
824
  if (get_tty_state (scb, &state))
825
    return -1;
826
 
827
  switch (num)
828
    {
829
    case SERIAL_1_STOPBITS:
830
      newbit = 0;
831
      break;
832
    case SERIAL_1_AND_A_HALF_STOPBITS:
833
    case SERIAL_2_STOPBITS:
834
      newbit = 1;
835
      break;
836
    default:
837
      return 1;
838
    }
839
 
840
#ifdef HAVE_TERMIOS
841
  if (!newbit)
842
    state.termios.c_cflag &= ~CSTOPB;
843
  else
844
    state.termios.c_cflag |= CSTOPB;    /* two bits */
845
#endif
846
 
847
#ifdef HAVE_TERMIO
848
  if (!newbit)
849
    state.termio.c_cflag &= ~CSTOPB;
850
  else
851
    state.termio.c_cflag |= CSTOPB;     /* two bits */
852
#endif
853
 
854
#ifdef HAVE_SGTTY
855
  return 0;                      /* sgtty doesn't support this */
856
#endif
857
 
858
  return set_tty_state (scb, &state);
859
}
860
 
861
static void
862
hardwire_close (struct serial *scb)
863
{
864
  if (scb->fd < 0)
865
    return;
866
 
867
  close (scb->fd);
868
  scb->fd = -1;
869
}
870
 
871
 
872
/* Generic operations used by all UNIX/FD based serial interfaces. */
873
 
874
serial_ttystate
875
ser_unix_nop_get_tty_state (struct serial *scb)
876
{
877
  /* allocate a dummy */
878
  return (serial_ttystate) XMALLOC (int);
879
}
880
 
881
int
882
ser_unix_nop_set_tty_state (struct serial *scb, serial_ttystate ttystate)
883
{
884
  return 0;
885
}
886
 
887
void
888
ser_unix_nop_raw (struct serial *scb)
889
{
890
  return;                       /* Always in raw mode */
891
}
892
 
893
/* Wait for input on scb, with timeout seconds.  Returns 0 on success,
894
   otherwise SERIAL_TIMEOUT or SERIAL_ERROR. */
895
 
896
int
897
ser_unix_wait_for (struct serial *scb, int timeout)
898
{
899
  while (1)
900
    {
901
      int numfds;
902
      struct timeval tv;
903
      fd_set readfds, exceptfds;
904
 
905
      /* NOTE: Some OS's can scramble the READFDS when the select()
906
         call fails (ex the kernel with Red Hat 5.2).  Initialize all
907
         arguments before each call. */
908
 
909
      tv.tv_sec = timeout;
910
      tv.tv_usec = 0;
911
 
912
      FD_ZERO (&readfds);
913
      FD_ZERO (&exceptfds);
914
      FD_SET (scb->fd, &readfds);
915
      FD_SET (scb->fd, &exceptfds);
916
 
917
      if (timeout >= 0)
918
        numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
919
      else
920
        numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
921
 
922
      if (numfds <= 0)
923
        {
924
          if (numfds == 0)
925
            return SERIAL_TIMEOUT;
926
          else if (errno == EINTR)
927
            continue;
928
          else
929
            return SERIAL_ERROR;        /* Got an error from select or poll */
930
        }
931
 
932
      return 0;
933
    }
934
}
935
 
936
/* Read a character with user-specified timeout.  TIMEOUT is number of seconds
937
   to wait, or -1 to wait forever.  Use timeout of 0 to effect a poll.  Returns
938
   char if successful.  Returns -2 if timeout expired, EOF if line dropped
939
   dead, or -3 for any other error (see errno in that case). */
940
 
941
static int
942
do_unix_readchar (struct serial *scb, int timeout)
943
{
944
  int status;
945
  int delta;
946
 
947
  /* We have to be able to keep the GUI alive here, so we break the original
948
     timeout into steps of 1 second, running the "keep the GUI alive" hook
949
     each time through the loop.
950
 
951
     Also, timeout = 0 means to poll, so we just set the delta to 0, so we
952
     will only go through the loop once. */
953
 
954
  delta = (timeout == 0 ? 0 : 1);
955
  while (1)
956
    {
957
 
958
      /* N.B. The UI may destroy our world (for instance by calling
959
         remote_stop,) in which case we want to get out of here as
960
         quickly as possible.  It is not safe to touch scb, since
961
         someone else might have freed it.  The ui_loop_hook signals that
962
         we should exit by returning 1. */
963
 
964
      if (ui_loop_hook)
965
        {
966
          if (ui_loop_hook (0))
967
            return SERIAL_TIMEOUT;
968
        }
969
 
970
      status = ser_unix_wait_for (scb, delta);
971
      if (timeout > 0)
972
        timeout -= delta;
973
 
974
      /* If we got a character or an error back from wait_for, then we can
975
         break from the loop before the timeout is completed. */
976
 
977
      if (status != SERIAL_TIMEOUT)
978
        {
979
          break;
980
        }
981
 
982
      /* If we have exhausted the original timeout, then generate
983
         a SERIAL_TIMEOUT, and pass it out of the loop. */
984
 
985
      else if (timeout == 0)
986
        {
987
          status = SERIAL_TIMEOUT;
988
          break;
989
        }
990
    }
991
 
992
  if (status < 0)
993
    return status;
994
 
995
  while (1)
996
    {
997
      status = read (scb->fd, scb->buf, BUFSIZ);
998
      if (status != -1 || errno != EINTR)
999
        break;
1000
    }
1001
 
1002
  if (status <= 0)
1003
    {
1004
      if (status == 0)
1005
        return SERIAL_TIMEOUT;  /* 0 chars means timeout [may need to
1006
                                   distinguish between EOF & timeouts
1007
                                   someday] */
1008
      else
1009
        return SERIAL_ERROR;    /* Got an error from read */
1010
    }
1011
 
1012
  scb->bufcnt = status;
1013
  scb->bufcnt--;
1014
  scb->bufp = scb->buf;
1015
  return *scb->bufp++;
1016
}
1017
 
1018
/* Perform operations common to both old and new readchar. */
1019
 
1020
/* Return the next character from the input FIFO.  If the FIFO is
1021
   empty, call the SERIAL specific routine to try and read in more
1022
   characters.
1023
 
1024
   Initially data from the input FIFO is returned (fd_event()
1025
   pre-reads the input into that FIFO.  Once that has been emptied,
1026
   further data is obtained by polling the input FD using the device
1027
   specific readchar() function.  Note: reschedule() is called after
1028
   every read.  This is because there is no guarentee that the lower
1029
   level fd_event() poll_event() code (which also calls reschedule())
1030
   will be called. */
1031
 
1032
static int
1033
generic_readchar (struct serial *scb, int timeout,
1034
                  int (do_readchar) (struct serial *scb, int timeout))
1035
{
1036
  int ch;
1037
  if (scb->bufcnt > 0)
1038
    {
1039
      ch = *scb->bufp;
1040
      scb->bufcnt--;
1041
      scb->bufp++;
1042
    }
1043
  else if (scb->bufcnt < 0)
1044
    {
1045
      /* Some errors/eof are are sticky. */
1046
      ch = scb->bufcnt;
1047
    }
1048
  else
1049
    {
1050
      ch = do_readchar (scb, timeout);
1051
      if (ch < 0)
1052
        {
1053
          switch ((enum serial_rc) ch)
1054
            {
1055
            case SERIAL_EOF:
1056
            case SERIAL_ERROR:
1057
              /* Make the error/eof stick. */
1058
              scb->bufcnt = ch;
1059
              break;
1060
            case SERIAL_TIMEOUT:
1061
              scb->bufcnt = 0;
1062
              break;
1063
            }
1064
        }
1065
    }
1066
  reschedule (scb);
1067
  return ch;
1068
}
1069
 
1070
int
1071
ser_unix_readchar (struct serial *scb, int timeout)
1072
{
1073
  return generic_readchar (scb, timeout, do_unix_readchar);
1074
}
1075
 
1076
int
1077
ser_unix_nop_noflush_set_tty_state (struct serial *scb,
1078
                                    serial_ttystate new_ttystate,
1079
                                    serial_ttystate old_ttystate)
1080
{
1081
  return 0;
1082
}
1083
 
1084
void
1085
ser_unix_nop_print_tty_state (struct serial *scb,
1086
                              serial_ttystate ttystate,
1087
                              struct ui_file *stream)
1088
{
1089
  /* Nothing to print.  */
1090
  return;
1091
}
1092
 
1093
int
1094
ser_unix_nop_setbaudrate (struct serial *scb, int rate)
1095
{
1096
  return 0;                      /* Never fails! */
1097
}
1098
 
1099
int
1100
ser_unix_nop_setstopbits (struct serial *scb, int num)
1101
{
1102
  return 0;                      /* Never fails! */
1103
}
1104
 
1105
int
1106
ser_unix_write (struct serial *scb, const char *str, int len)
1107
{
1108
  int cc;
1109
 
1110
  while (len > 0)
1111
    {
1112
      cc = write (scb->fd, str, len);
1113
 
1114
      if (cc < 0)
1115
        return 1;
1116
      len -= cc;
1117
      str += cc;
1118
    }
1119
  return 0;
1120
}
1121
 
1122
int
1123
ser_unix_nop_flush_output (struct serial *scb)
1124
{
1125
  return 0;
1126
}
1127
 
1128
int
1129
ser_unix_flush_input (struct serial *scb)
1130
{
1131
  if (scb->bufcnt >= 0)
1132
    {
1133
      scb->bufcnt = 0;
1134
      scb->bufp = scb->buf;
1135
      return 0;
1136
    }
1137
  else
1138
    return SERIAL_ERROR;
1139
}
1140
 
1141
int
1142
ser_unix_nop_send_break (struct serial *scb)
1143
{
1144
  return 0;
1145
}
1146
 
1147
int
1148
ser_unix_nop_drain_output (struct serial *scb)
1149
{
1150
  return 0;
1151
}
1152
 
1153
 
1154
 
1155
/* Event handling for ASYNC serial code.
1156
 
1157
   At any time the SERIAL device either: has an empty FIFO and is
1158
   waiting on a FD event; or has a non-empty FIFO/error condition and
1159
   is constantly scheduling timer events.
1160
 
1161
   ASYNC only stops pestering its client when it is de-async'ed or it
1162
   is told to go away. */
1163
 
1164
/* Value of scb->async_state: */
1165
enum {
1166
  /* >= 0 (TIMER_SCHEDULED) */
1167
  /* The ID of the currently scheduled timer event. This state is
1168
     rarely encountered.  Timer events are one-off so as soon as the
1169
     event is delivered the state is shanged to NOTHING_SCHEDULED. */
1170
  FD_SCHEDULED = -1,
1171
  /* The fd_event() handler is scheduled.  It is called when ever the
1172
     file descriptor becomes ready. */
1173
  NOTHING_SCHEDULED = -2
1174
  /* Either no task is scheduled (just going into ASYNC mode) or a
1175
     timer event has just gone off and the current state has been
1176
     forced into nothing scheduled. */
1177
};
1178
 
1179
/* Identify and schedule the next ASYNC task based on scb->async_state
1180
   and scb->buf* (the input FIFO).  A state machine is used to avoid
1181
   the need to make redundant calls into the event-loop - the next
1182
   scheduled task is only changed when needed. */
1183
 
1184
static void
1185
reschedule (struct serial *scb)
1186
{
1187
  if (serial_is_async_p (scb))
1188
    {
1189
      int next_state;
1190
      switch (scb->async_state)
1191
        {
1192
        case FD_SCHEDULED:
1193
          if (scb->bufcnt == 0)
1194
            next_state = FD_SCHEDULED;
1195
          else
1196
            {
1197
              delete_file_handler (scb->fd);
1198
              next_state = create_timer (0, push_event, scb);
1199
            }
1200
          break;
1201
        case NOTHING_SCHEDULED:
1202
          if (scb->bufcnt == 0)
1203
            {
1204
              add_file_handler (scb->fd, fd_event, scb);
1205
              next_state = FD_SCHEDULED;
1206
            }
1207
          else
1208
            {
1209
              next_state = create_timer (0, push_event, scb);
1210
            }
1211
          break;
1212
        default: /* TIMER SCHEDULED */
1213
          if (scb->bufcnt == 0)
1214
            {
1215
              delete_timer (scb->async_state);
1216
              add_file_handler (scb->fd, fd_event, scb);
1217
              next_state = FD_SCHEDULED;
1218
            }
1219
          else
1220
            next_state = scb->async_state;
1221
          break;
1222
        }
1223
      if (serial_debug_p (scb))
1224
        {
1225
          switch (next_state)
1226
            {
1227
            case FD_SCHEDULED:
1228
              if (scb->async_state != FD_SCHEDULED)
1229
                fprintf_unfiltered (gdb_stdlog, "[fd%d->fd-scheduled]\n",
1230
                                    scb->fd);
1231
              break;
1232
            default: /* TIMER SCHEDULED */
1233
              if (scb->async_state == FD_SCHEDULED)
1234
                fprintf_unfiltered (gdb_stdlog, "[fd%d->timer-scheduled]\n",
1235
                                    scb->fd);
1236
              break;
1237
            }
1238
        }
1239
      scb->async_state = next_state;
1240
    }
1241
}
1242
 
1243
/* FD_EVENT: This is scheduled when the input FIFO is empty (and there
1244
   is no pending error).  As soon as data arrives, it is read into the
1245
   input FIFO and the client notified.  The client should then drain
1246
   the FIFO using readchar().  If the FIFO isn't immediatly emptied,
1247
   push_event() is used to nag the client until it is. */
1248
 
1249
static void
1250
fd_event (int error, void *context)
1251
{
1252
  struct serial *scb = context;
1253
  if (error != 0)
1254
    {
1255
      scb->bufcnt = SERIAL_ERROR;
1256
    }
1257
  else if (scb->bufcnt == 0)
1258
    {
1259
      /* Prime the input FIFO.  The readchar() function is used to
1260
         pull characters out of the buffer.  See also
1261
         generic_readchar(). */
1262
      int nr;
1263
      do
1264
        {
1265
          nr = read (scb->fd, scb->buf, BUFSIZ);
1266
        }
1267
      while (nr == -1 && errno == EINTR);
1268
      if (nr == 0)
1269
        {
1270
          scb->bufcnt = SERIAL_EOF;
1271
        }
1272
      else if (nr > 0)
1273
        {
1274
          scb->bufcnt = nr;
1275
          scb->bufp = scb->buf;
1276
        }
1277
      else
1278
        {
1279
          scb->bufcnt = SERIAL_ERROR;
1280
        }
1281
    }
1282
  scb->async_handler (scb, scb->async_context);
1283
  reschedule (scb);
1284
}
1285
 
1286
/* PUSH_EVENT: The input FIFO is non-empty (or there is a pending
1287
   error).  Nag the client until all the data has been read.  In the
1288
   case of errors, the client will need to close or de-async the
1289
   device before naging stops. */
1290
 
1291
static void
1292
push_event (void *context)
1293
{
1294
  struct serial *scb = context;
1295
  scb->async_state = NOTHING_SCHEDULED; /* Timers are one-off */
1296
  scb->async_handler (scb, scb->async_context);
1297
  /* re-schedule */
1298
  reschedule (scb);
1299
}
1300
 
1301
/* Put the SERIAL device into/out-of ASYNC mode.  */
1302
 
1303
void
1304
ser_unix_async (struct serial *scb,
1305
                int async_p)
1306
{
1307
  if (async_p)
1308
    {
1309
      /* Force a re-schedule. */
1310
      scb->async_state = NOTHING_SCHEDULED;
1311
      if (serial_debug_p (scb))
1312
        fprintf_unfiltered (gdb_stdlog, "[fd%d->asynchronous]\n",
1313
                            scb->fd);
1314
      reschedule (scb);
1315
    }
1316
  else
1317
    {
1318
      if (serial_debug_p (scb))
1319
        fprintf_unfiltered (gdb_stdlog, "[fd%d->synchronous]\n",
1320
                            scb->fd);
1321
      /* De-schedule whatever tasks are currently scheduled. */
1322
      switch (scb->async_state)
1323
        {
1324
        case FD_SCHEDULED:
1325
          delete_file_handler (scb->fd);
1326
          break;
1327
        NOTHING_SCHEDULED:
1328
          break;
1329
        default: /* TIMER SCHEDULED */
1330
          delete_timer (scb->async_state);
1331
          break;
1332
        }
1333
    }
1334
}
1335
 
1336
void
1337
_initialize_ser_hardwire (void)
1338
{
1339
  struct serial_ops *ops = XMALLOC (struct serial_ops);
1340
  memset (ops, sizeof (struct serial_ops), 0);
1341
  ops->name = "hardwire";
1342
  ops->next = 0;
1343
  ops->open = hardwire_open;
1344
  ops->close = hardwire_close;
1345
  /* FIXME: Don't replace this with the equivalent ser_unix*() until
1346
     the old TERMIOS/SGTTY/... timer code has been flushed. cagney
1347
     1999-09-16. */
1348
  ops->readchar = hardwire_readchar;
1349
  ops->write = ser_unix_write;
1350
  ops->flush_output = hardwire_flush_output;
1351
  ops->flush_input = hardwire_flush_input;
1352
  ops->send_break = hardwire_send_break;
1353
  ops->go_raw = hardwire_raw;
1354
  ops->get_tty_state = hardwire_get_tty_state;
1355
  ops->set_tty_state = hardwire_set_tty_state;
1356
  ops->print_tty_state = hardwire_print_tty_state;
1357
  ops->noflush_set_tty_state = hardwire_noflush_set_tty_state;
1358
  ops->setbaudrate = hardwire_setbaudrate;
1359
  ops->setstopbits = hardwire_setstopbits;
1360
  ops->drain_output = hardwire_drain_output;
1361
  ops->async = ser_unix_async;
1362
  serial_add_interface (ops);
1363
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.