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