]>
Commit | Line | Data |
---|---|---|
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 | ||
32 | struct mac_ttystate | |
33 | { | |
34 | int bogus; | |
35 | }; | |
36 | ||
37 | static int mac_open PARAMS ((serial_t scb, const char *name)); | |
38 | static void mac_raw PARAMS ((serial_t scb)); | |
39 | static int mac_readchar PARAMS ((serial_t scb, int timeout)); | |
40 | static int mac_setbaudrate PARAMS ((serial_t scb, int rate)); | |
41 | static int mac_write PARAMS ((serial_t scb, const char *str, int len)); | |
42 | static void mac_close PARAMS ((serial_t scb)); | |
43 | static serial_ttystate mac_get_tty_state PARAMS ((serial_t scb)); | |
44 | static int mac_set_tty_state PARAMS ((serial_t scb, serial_ttystate state)); | |
45 | static char *aptr PARAMS ((short p)); | |
46 | ||
47 | short input_refnum; | |
48 | short output_refnum; | |
49 | ||
50 | char *mac_input_buffer; | |
51 | char *mac_output_buffer; | |
52 | ||
53 | static int | |
54 | mac_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 | ||
132 | static int | |
133 | mac_noop (scb) | |
134 | serial_t scb; | |
135 | { | |
136 | return 0; | |
137 | } | |
138 | ||
139 | static void | |
140 | mac_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 | ||
151 | static int | |
152 | mac_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 | ||
204 | static serial_ttystate | |
205 | mac_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 | ||
215 | static int | |
216 | mac_set_tty_state (scb, ttystate) | |
217 | serial_t scb; | |
218 | serial_ttystate ttystate; | |
219 | { | |
220 | return 0; | |
221 | } | |
222 | ||
223 | static int | |
224 | mac_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 | ||
232 | static void | |
233 | mac_print_tty_state (scb, ttystate) | |
234 | serial_t scb; | |
235 | serial_ttystate ttystate; | |
236 | { | |
237 | /* Nothing to print. */ | |
238 | return; | |
239 | } | |
240 | ||
241 | static int | |
242 | mac_set_baud_rate (scb, rate) | |
243 | serial_t scb; | |
244 | int rate; | |
245 | { | |
246 | return 0; | |
247 | } | |
248 | ||
58c0b523 SS |
249 | int first_mac_write = 0; |
250 | ||
8dc3e3d7 SS |
251 | static int |
252 | mac_write (scb, str, len) | |
253 | serial_t scb; | |
254 | const char *str; | |
255 | int len; | |
256 | { | |
257 | OSErr err; | |
258 | IOParam pb; | |
259 | ||
b8ec8d4a | 260 | if (first_mac_write++ < 4) |
58c0b523 | 261 | { |
b8ec8d4a | 262 | sec_sleep (1); |
58c0b523 | 263 | } |
8dc3e3d7 SS |
264 | pb.ioRefNum = output_refnum; |
265 | pb.ioBuffer = (Ptr) str; | |
266 | pb.ioReqCount = len; | |
267 | err = PBWrite ((ParmBlkPtr) &pb, 0); | |
268 | if (err < 0) | |
269 | { | |
270 | return 1; | |
271 | } | |
272 | return 0; | |
273 | } | |
274 | ||
b8ec8d4a SS |
275 | sec_sleep (int timeout) |
276 | { | |
277 | unsigned long start_time, now; | |
278 | ||
279 | time (&start_time); | |
280 | ||
281 | while (1) | |
282 | { | |
283 | time (&now); | |
284 | if (now > start_time + timeout) | |
285 | return; | |
286 | } | |
287 | } | |
288 | ||
8dc3e3d7 | 289 | static void |
b8ec8d4a | 290 | mac_close (serial_t scb) |
8dc3e3d7 SS |
291 | { |
292 | if (input_refnum) | |
293 | { | |
294 | if (1 /* custom buffer */) | |
295 | SerSetBuf (input_refnum, mac_input_buffer, 0); | |
296 | CloseDriver (input_refnum); | |
297 | input_refnum = 0; | |
298 | } | |
299 | if (output_refnum) | |
300 | { | |
301 | if (0 /* custom buffer */) | |
302 | SetSetBuf (input_refnum, mac_output_buffer, 0); | |
303 | CloseDriver (output_refnum); | |
304 | output_refnum = 0; | |
305 | } | |
306 | } | |
307 | ||
308 | static struct serial_ops mac_ops = | |
309 | { | |
310 | "hardwire", | |
311 | 0, | |
312 | mac_open, | |
313 | mac_close, | |
314 | mac_readchar, | |
315 | mac_write, | |
316 | mac_noop, /* flush output */ | |
317 | mac_noop, /* flush input */ | |
318 | mac_noop, /* send break -- currently only for nindy */ | |
319 | mac_raw, | |
320 | mac_get_tty_state, | |
321 | mac_set_tty_state, | |
322 | mac_print_tty_state, | |
323 | mac_noflush_set_tty_state, | |
324 | mac_set_baud_rate, | |
325 | }; | |
326 | ||
327 | void | |
328 | _initialize_ser_mac () | |
329 | { | |
330 | serial_add_interface (&mac_ops); | |
331 | } |