* Makefile.in: Add rules for monitor.o and rom68k-rom.o to make
[deliverable/binutils-gdb.git] / gdb / ser-mac.c
CommitLineData
8dc3e3d7
SS
1/* Remote serial interface for local (hardwired) serial ports for Macintosh.
2 Copyright 1994 Free Software Foundation, Inc.
58c0b523 3 Contributed by Cygnus Support. Written by Stan Shebs.
8dc3e3d7
SS
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21#include "defs.h"
22#include "serial.h"
23
24#include <Types.h>
25#include <Devices.h>
7f6572f5
SS
26/* This is the regular Mac Serial.h, but copied to a different name
27 so as not to get confused with the GDB serial.h above. */
28#include "MacSerial.h"
8dc3e3d7
SS
29
30/* This is unused for now. We just return a placeholder. */
31
32struct mac_ttystate
33 {
34 int bogus;
35 };
36
37static int mac_open PARAMS ((serial_t scb, const char *name));
38static void mac_raw PARAMS ((serial_t scb));
39static int mac_readchar PARAMS ((serial_t scb, int timeout));
40static int mac_setbaudrate PARAMS ((serial_t scb, int rate));
41static int mac_write PARAMS ((serial_t scb, const char *str, int len));
42static void mac_close PARAMS ((serial_t scb));
43static serial_ttystate mac_get_tty_state PARAMS ((serial_t scb));
44static int mac_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
45static char *aptr PARAMS ((short p));
46
47short input_refnum;
48short output_refnum;
49
50char *mac_input_buffer;
51char *mac_output_buffer;
52
53static int
54mac_open (scb, name)
55 serial_t scb;
56 const char *name;
57{
58 OSErr err;
59
60 /* Alloc buffer space first - that way any allocation failures are
61 intercepted before the serial driver gets involved. */
62 if (mac_input_buffer == NULL)
63 mac_input_buffer = (char *) xmalloc (256);
64 /* Match on a name and open a port. */
65 if (strcmp (name, "modem") == 0)
66 {
67 err = OpenDriver ("\p.AIn", &input_refnum);
68 if (err != 0)
69 {
70 return (-1);
71 }
72 err = OpenDriver ("\p.AOut", &output_refnum);
73 if (err != 0)
74 {
75 CloseDriver (input_refnum);
76 return (-1);
77 }
78 }
79 else if (strcmp (name, "printer") == 0)
80 {
81 err = OpenDriver ("\p.BIn", &input_refnum);
82 if (err != 0)
83 {
84 return (-1);
85 }
86 err = OpenDriver ("\p.BOut", &output_refnum);
87 if (err != 0)
88 {
89 CloseDriver (input_refnum);
90 return (-1);
91 }
92 /* fake */
93 scb->fd = 1;
94 return 0;
95 }
96 else
97 {
58c0b523 98 error ("You must specify a port. Choices are `modem' or `printer'.");
8dc3e3d7
SS
99 errno = ENOENT;
100 return (-1);
101 }
102 /* We got something open. */
103 if (1 /* using custom buffer */)
104 SerSetBuf (input_refnum, mac_input_buffer, 256);
105 /* Set to a GDB-preferred state. */
106 SerReset (input_refnum, stop10|noParity|data8|baud9600);
107 SerReset (output_refnum, stop10|noParity|data8|baud9600);
108 {
109 CntrlParam cb;
110 struct SerShk *handshake;
111
112 cb.ioCRefNum = output_refnum;
113 cb.csCode = 14;
114 handshake = (struct SerShk *) &cb.csParam[0];
115 handshake->fXOn = 0;
116 handshake->fCTS = 0;
117 handshake->xOn = 0;
118 handshake->xOff = 0;
119 handshake->errs = 0;
120 handshake->evts = 0;
121 handshake->fInX = 0;
122 handshake->fDTR = 0;
123 err = PBControl ((ParmBlkPtr) &cb, 0);
124 if (err < 0)
125 return (-1);
126 }
127 /* fake */
128 scb->fd = 1;
129 return 0;
130}
131
132static int
133mac_noop (scb)
134 serial_t scb;
135{
136 return 0;
137}
138
139static void
140mac_raw (scb)
141 serial_t scb;
142{
143 /* Always effectively in raw mode. */
144}
145
146/* Read a character with user-specified timeout. TIMEOUT is number of seconds
147 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
148 char if successful. Returns -2 if timeout expired, EOF if line dropped
149 dead, or -3 for any other error (see errno in that case). */
150
151static int
152mac_readchar (scb, timeout)
153 serial_t scb;
154 int timeout;
155{
156 int status, n;
b8ec8d4a 157 /* time_t */ unsigned long start_time, now;
8dc3e3d7
SS
158 OSErr err;
159 CntrlParam cb;
160 IOParam pb;
161
162 if (scb->bufcnt-- > 0)
163 return *scb->bufp++;
164
b8ec8d4a 165 time (&start_time);
8dc3e3d7
SS
166
167 while (1)
168 {
169 cb.ioCRefNum = input_refnum;
170 cb.csCode = 2;
171 err = PBStatus ((ParmBlkPtr) &cb, 0);
172 if (err < 0)
173 return SERIAL_ERROR;
174 n = *((long *) &cb.csParam[0]);
175 if (n > 0)
176 {
177 pb.ioRefNum = input_refnum;
178 pb.ioBuffer = (Ptr) (scb->buf);
179 pb.ioReqCount = (n > 64 ? 64 : n);
180 err = PBRead ((ParmBlkPtr) &pb, 0);
181 if (err < 0)
182 return SERIAL_ERROR;
183 scb->bufcnt = pb.ioReqCount;
184 scb->bufcnt--;
185 scb->bufp = scb->buf;
186 return *scb->bufp++;
187 }
188 else if (timeout == 0)
189 return SERIAL_TIMEOUT;
190 else if (timeout == -1)
191 ;
192 else
193 {
194 time (&now);
b8ec8d4a 195 if (now > start_time + timeout)
8dc3e3d7 196 return SERIAL_TIMEOUT;
8dc3e3d7
SS
197 }
198 }
199}
200
201/* mac_{get set}_tty_state() are both dummys to fill out the function
202 vector. Someday, they may do something real... */
203
204static serial_ttystate
205mac_get_tty_state (scb)
206 serial_t scb;
207{
208 struct mac_ttystate *state;
209
210 state = (struct mac_ttystate *) xmalloc (sizeof *state);
211
212 return (serial_ttystate) state;
213}
214
215static int
216mac_set_tty_state (scb, ttystate)
217 serial_t scb;
218 serial_ttystate ttystate;
219{
220 return 0;
221}
222
223static int
224mac_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
225 serial_t scb;
226 serial_ttystate new_ttystate;
227 serial_ttystate old_ttystate;
228{
229 return 0;
230}
231
232static void
233mac_print_tty_state (scb, ttystate)
234 serial_t scb;
235 serial_ttystate ttystate;
236{
237 /* Nothing to print. */
238 return;
239}
240
241static int
242mac_set_baud_rate (scb, rate)
243 serial_t scb;
244 int rate;
245{
246 return 0;
247}
248
85c8b135
SG
249static int
250mac_set_stop_bits (scb, num)
251 serial_t scb;
252 int num;
253{
254 return 0;
255}
256
58c0b523
SS
257int first_mac_write = 0;
258
8dc3e3d7
SS
259static int
260mac_write (scb, str, len)
261 serial_t scb;
262 const char *str;
263 int len;
264{
265 OSErr err;
266 IOParam pb;
267
b8ec8d4a 268 if (first_mac_write++ < 4)
58c0b523 269 {
b8ec8d4a 270 sec_sleep (1);
58c0b523 271 }
8dc3e3d7
SS
272 pb.ioRefNum = output_refnum;
273 pb.ioBuffer = (Ptr) str;
274 pb.ioReqCount = len;
275 err = PBWrite ((ParmBlkPtr) &pb, 0);
276 if (err < 0)
277 {
278 return 1;
279 }
280 return 0;
281}
282
b8ec8d4a
SS
283sec_sleep (int timeout)
284{
285 unsigned long start_time, now;
286
287 time (&start_time);
288
289 while (1)
290 {
291 time (&now);
292 if (now > start_time + timeout)
293 return;
294 }
295}
296
8dc3e3d7 297static void
b8ec8d4a 298mac_close (serial_t scb)
8dc3e3d7
SS
299{
300 if (input_refnum)
301 {
302 if (1 /* custom buffer */)
303 SerSetBuf (input_refnum, mac_input_buffer, 0);
304 CloseDriver (input_refnum);
305 input_refnum = 0;
306 }
307 if (output_refnum)
308 {
309 if (0 /* custom buffer */)
310 SetSetBuf (input_refnum, mac_output_buffer, 0);
311 CloseDriver (output_refnum);
312 output_refnum = 0;
313 }
314}
315
316static struct serial_ops mac_ops =
317{
318 "hardwire",
319 0,
320 mac_open,
321 mac_close,
322 mac_readchar,
323 mac_write,
324 mac_noop, /* flush output */
325 mac_noop, /* flush input */
326 mac_noop, /* send break -- currently only for nindy */
327 mac_raw,
328 mac_get_tty_state,
329 mac_set_tty_state,
330 mac_print_tty_state,
331 mac_noflush_set_tty_state,
332 mac_set_baud_rate,
85c8b135 333 mac_set_stop_bits,
8dc3e3d7
SS
334};
335
336void
337_initialize_ser_mac ()
338{
339 serial_add_interface (&mac_ops);
340}
This page took 0.081851 seconds and 4 git commands to generate.