1 |
/* |
2 |
* serial.cpp - Serial device driver |
3 |
* |
4 |
* SheepShaver (C) 1997-2002 Marc Hellwig and Christian Bauer |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#include "sysdeps.h" |
22 |
#include "main.h" |
23 |
#include "macos_util.h" |
24 |
#include "serial.h" |
25 |
#include "serial_defs.h" |
26 |
|
27 |
#define DEBUG 0 |
28 |
#include "debug.h" |
29 |
|
30 |
|
31 |
// Global variables |
32 |
SERDPort *the_serd_port[2]; |
33 |
|
34 |
// Function pointers from imported functions |
35 |
typedef int16 (*iocic_ptr)(uint32, int16); |
36 |
static uint32 iocic_tvect = 0; |
37 |
static inline int16 IOCommandIsComplete(uint32 arg1, int16 arg2) |
38 |
{ |
39 |
return (int16)CallMacOS2(iocic_ptr, iocic_tvect, arg1, arg2); |
40 |
} |
41 |
|
42 |
|
43 |
/* |
44 |
* Empty function (AIn/BIn Open/Close) |
45 |
*/ |
46 |
|
47 |
int16 SerialNothing(uint32 pb, uint32 dce) |
48 |
{ |
49 |
return noErr; |
50 |
} |
51 |
|
52 |
|
53 |
/* |
54 |
* Driver Open() routine (output side only) |
55 |
*/ |
56 |
|
57 |
int16 SerialOpen(uint32 pb, uint32 dce) |
58 |
{ |
59 |
D(bug("SerialOpen pb %08lx, dce %08lx\n", pb, dce)); |
60 |
|
61 |
// Get IOCommandIsComplete function |
62 |
iocic_tvect = (uint32)FindLibSymbol("\021DriverServicesLib", "\023IOCommandIsComplete"); |
63 |
D(bug("IOCommandIsComplete TVECT at %08lx\n", iocic_tvect)); |
64 |
if (iocic_tvect == 0) { |
65 |
printf("FATAL: SerialOpen(): Can't find IOCommandIsComplete()\n"); |
66 |
return openErr; |
67 |
} |
68 |
|
69 |
// Do nothing if port is already open |
70 |
SERDPort *the_port = the_serd_port[(-(int16)ReadMacInt16(dce + dCtlRefNum)-6) >> 1]; |
71 |
if (the_port->is_open) |
72 |
return noErr; |
73 |
|
74 |
// Init variables |
75 |
the_port->read_pending = the_port->write_pending = false; |
76 |
the_port->read_done = the_port->write_done = false; |
77 |
the_port->cum_errors = 0; |
78 |
|
79 |
// Open port |
80 |
int16 res = the_port->open(ReadMacInt16(0x1fc + ((-(int16)ReadMacInt16(dce + dCtlRefNum)-6) & 2))); |
81 |
if (res) |
82 |
return res; |
83 |
|
84 |
// Allocate Deferred Task structures |
85 |
uint32 input_dt = the_port->input_dt = (uint32)the_port->dt_store; |
86 |
uint32 output_dt = the_port->output_dt = (uint32)the_port->dt_store + SIZEOF_serdt; |
87 |
D(bug(" input_dt %08lx, output_dt %08lx\n", input_dt, output_dt)); |
88 |
|
89 |
WriteMacInt16(input_dt + qType, dtQType); |
90 |
WriteMacInt32(input_dt + dtAddr, input_dt + serdtCode); |
91 |
WriteMacInt32(input_dt + dtParam, input_dt + serdtResult); |
92 |
// Deferred function for signalling that Prime is complete (pointer to mydtResult in a1) |
93 |
WriteMacInt16(input_dt + serdtCode, 0x2019); // move.l (a1)+,d0 (result) |
94 |
WriteMacInt16(input_dt + serdtCode + 2, 0x2251); // move.l (a1),a1 (dce) |
95 |
WriteMacInt32(input_dt + serdtCode + 4, 0x207808fc); // move.l JIODone,a0 |
96 |
WriteMacInt16(input_dt + serdtCode + 8, 0x4ed0); // jmp (a0) |
97 |
|
98 |
WriteMacInt16(output_dt + qType, dtQType); |
99 |
WriteMacInt32(output_dt + dtAddr, output_dt + serdtCode); |
100 |
WriteMacInt32(output_dt + dtParam, output_dt + serdtResult); |
101 |
// Deferred function for signalling that Prime is complete (pointer to mydtResult in a1) |
102 |
WriteMacInt16(output_dt + serdtCode, 0x2019); // move.l (a1)+,d0 (result) |
103 |
WriteMacInt16(output_dt + serdtCode + 2, 0x2251); // move.l (a1),a1 (dce) |
104 |
WriteMacInt32(output_dt + serdtCode + 4, 0x207808fc); // move.l JIODone,a0 |
105 |
WriteMacInt16(output_dt + serdtCode + 8, 0x4ed0); // jmp (a0) |
106 |
|
107 |
the_port->is_open = true; |
108 |
return noErr; |
109 |
} |
110 |
|
111 |
|
112 |
/* |
113 |
* Driver Prime() routines |
114 |
*/ |
115 |
|
116 |
int16 SerialPrimeIn(uint32 pb, uint32 dce) |
117 |
{ |
118 |
D(bug("SerialPrimeIn pb %08lx, dce %08lx\n", pb, dce)); |
119 |
int16 res; |
120 |
|
121 |
SERDPort *the_port = the_serd_port[(-(int16)ReadMacInt16(dce + dCtlRefNum)-6) >> 1]; |
122 |
if (!the_port->is_open) |
123 |
res = notOpenErr; |
124 |
else { |
125 |
if (the_port->read_pending) { |
126 |
printf("FATAL: SerialPrimeIn() called while request is pending\n"); |
127 |
res = readErr; |
128 |
} else |
129 |
res = the_port->prime_in(pb, dce); |
130 |
} |
131 |
|
132 |
if (ReadMacInt16(pb + ioTrap) & 0x0200) |
133 |
if (res > 0) { |
134 |
WriteMacInt16(pb + ioResult, 0); |
135 |
return 0; // Command in progress |
136 |
} else { |
137 |
WriteMacInt16(pb + ioResult, res); |
138 |
return res; |
139 |
} |
140 |
else |
141 |
if (res > 0) |
142 |
return 0; // Command in progress |
143 |
else { |
144 |
IOCommandIsComplete(pb, res); |
145 |
return res; |
146 |
} |
147 |
} |
148 |
|
149 |
int16 SerialPrimeOut(uint32 pb, uint32 dce) |
150 |
{ |
151 |
D(bug("SerialPrimeOut pb %08lx, dce %08lx\n", pb, dce)); |
152 |
int16 res; |
153 |
|
154 |
SERDPort *the_port = the_serd_port[(-(int16)ReadMacInt16(dce + dCtlRefNum)-6) >> 1]; |
155 |
if (!the_port->is_open) |
156 |
res = notOpenErr; |
157 |
else { |
158 |
if (the_port->write_pending) { |
159 |
printf("FATAL: SerialPrimeOut() called while request is pending\n"); |
160 |
res = writErr; |
161 |
} else |
162 |
res = the_port->prime_out(pb, dce); |
163 |
} |
164 |
|
165 |
if (ReadMacInt16(pb + ioTrap) & 0x0200) |
166 |
if (res > 0) { |
167 |
WriteMacInt16(pb + ioResult, 0); |
168 |
return 0; // Command in progress |
169 |
} else { |
170 |
WriteMacInt16(pb + ioResult, res); |
171 |
return res; |
172 |
} |
173 |
else |
174 |
if (res > 0) |
175 |
return 0; // Command in progress |
176 |
else { |
177 |
IOCommandIsComplete(pb, res); |
178 |
return res; |
179 |
} |
180 |
} |
181 |
|
182 |
|
183 |
/* |
184 |
* Driver Control() routine |
185 |
*/ |
186 |
|
187 |
int16 SerialControl(uint32 pb, uint32 dce) |
188 |
{ |
189 |
uint16 code = ReadMacInt16(pb + csCode); |
190 |
D(bug("SerialControl %d, pb %08lx, dce %08lx\n", code, pb, dce)); |
191 |
int16 res; |
192 |
|
193 |
SERDPort *the_port = the_serd_port[(-(int16)ReadMacInt16(dce + dCtlRefNum)-6) >> 1]; |
194 |
if (!the_port->is_open) |
195 |
res = notOpenErr; |
196 |
else { |
197 |
switch (code) { |
198 |
case kSERDSetPollWrite: |
199 |
res = noErr; |
200 |
break; |
201 |
default: |
202 |
res = the_port->control(pb, dce, code); |
203 |
break; |
204 |
} |
205 |
} |
206 |
|
207 |
if (code == 1) |
208 |
return res; |
209 |
else if (ReadMacInt16(pb + ioTrap) & 0x0200) { |
210 |
WriteMacInt16(pb + ioResult, res); |
211 |
return res; |
212 |
} else { |
213 |
IOCommandIsComplete(pb, res); |
214 |
return res; |
215 |
} |
216 |
} |
217 |
|
218 |
|
219 |
/* |
220 |
* Driver Status() routine |
221 |
*/ |
222 |
|
223 |
int16 SerialStatus(uint32 pb, uint32 dce) |
224 |
{ |
225 |
uint16 code = ReadMacInt16(pb + csCode); |
226 |
D(bug("SerialStatus %d, pb %08lx, dce %08lx\n", code, pb, dce)); |
227 |
int16 res; |
228 |
|
229 |
SERDPort *the_port = the_serd_port[(-(int16)ReadMacInt16(dce + dCtlRefNum)-6) >> 1]; |
230 |
if (!the_port->is_open) |
231 |
res = notOpenErr; |
232 |
else { |
233 |
switch (code) { |
234 |
case kSERDVersion: |
235 |
WriteMacInt8(pb + csParam, 9); // Second-generation SerialDMA driver |
236 |
res = noErr; |
237 |
break; |
238 |
|
239 |
case 0x8000: |
240 |
WriteMacInt8(pb + csParam, 9); // Second-generation SerialDMA driver |
241 |
WriteMacInt16(pb + csParam + 4, 0x1997); // Date of serial driver |
242 |
WriteMacInt16(pb + csParam + 6, 0x0616); |
243 |
res = noErr; |
244 |
break; |
245 |
|
246 |
default: |
247 |
res = the_port->status(pb, dce, code); |
248 |
break; |
249 |
} |
250 |
} |
251 |
|
252 |
if (ReadMacInt16(pb + ioTrap) & 0x0200) { |
253 |
WriteMacInt16(pb + ioResult, res); |
254 |
return res; |
255 |
} else { |
256 |
IOCommandIsComplete(pb, res); |
257 |
return res; |
258 |
} |
259 |
} |
260 |
|
261 |
|
262 |
/* |
263 |
* Driver Close() routine |
264 |
*/ |
265 |
|
266 |
int16 SerialClose(uint32 pb, uint32 dce) |
267 |
{ |
268 |
D(bug("SerialClose pb %08lx, dce %08lx\n", pb, dce)); |
269 |
|
270 |
// Close port if open |
271 |
SERDPort *the_port = the_serd_port[(-(int16)ReadMacInt16(dce + dCtlRefNum)-6) >> 1]; |
272 |
if (the_port->is_open) { |
273 |
int16 res = the_port->close(); |
274 |
the_port->is_open = false; |
275 |
return res; |
276 |
} else |
277 |
return noErr; |
278 |
} |
279 |
|
280 |
|
281 |
/* |
282 |
* Serial interrupt - Prime command completed, activate deferred tasks to call IODone |
283 |
*/ |
284 |
|
285 |
void SerialInterrupt(void) |
286 |
{ |
287 |
D(bug("SerialIRQ\n")); |
288 |
|
289 |
// Port 0 |
290 |
if (the_serd_port[0]->is_open) { |
291 |
if (the_serd_port[0]->read_pending && the_serd_port[0]->read_done) { |
292 |
Enqueue(the_serd_port[0]->input_dt, 0xd92); |
293 |
the_serd_port[0]->read_pending = the_serd_port[0]->read_done = false; |
294 |
} |
295 |
if (the_serd_port[0]->write_pending && the_serd_port[0]->write_done) { |
296 |
Enqueue(the_serd_port[0]->output_dt, 0xd92); |
297 |
the_serd_port[0]->write_pending = the_serd_port[0]->write_done = false; |
298 |
} |
299 |
} |
300 |
|
301 |
// Port 1 |
302 |
if (the_serd_port[1]->is_open) { |
303 |
if (the_serd_port[1]->read_pending && the_serd_port[1]->read_done) { |
304 |
Enqueue(the_serd_port[1]->input_dt, 0xd92); |
305 |
the_serd_port[1]->read_pending = the_serd_port[1]->read_done = false; |
306 |
} |
307 |
if (the_serd_port[1]->write_pending && the_serd_port[1]->write_done) { |
308 |
Enqueue(the_serd_port[1]->output_dt, 0xd92); |
309 |
the_serd_port[1]->write_pending = the_serd_port[1]->write_done = false; |
310 |
} |
311 |
} |
312 |
} |