Debian Codegraph @

src/win32/link_par.cc

raw
1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id$ */
3
4 /* libticables2 - link cable library, a part of the TiLP project
5 * Copyright (C) 1999-2005 Romain Lievin
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 Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /* "Home-made parallel" link cable unit */
23
24 #include <stdio.h>
25
26 #include "../ticables.h"
27 #include "../logging.h"
28 #include "../error.h"
29 #include "../gettext.h"
30 #include "../internal.h"
31 #include "detect.h"
32 #include "ioports.h"
33
34 #define lpt_out (h->address + 0)
35 #define lpt_in (h->address + 1)
36 #define lpt_ctl (h->address + 2)
37
38 static int par_prepare(CableHandle *h)
39 {
40 const char * device;
41 switch(h->port)
42 {
43 case PORT_1: h->address = 0x378; device = "LPT1"; break;
44 case PORT_2: h->address = 0x278; device = "LPT2"; break;
45 case PORT_3: h->address = 0x3bc; device = "LPT3"; break;
46 default: return ERR_ILLEGAL_ARG;
47 }
48
49 if (h->device == NULL)
50 {
51 h->device = strdup(device);
52 }
53
54 if (win32_check_os() == WIN_NT)
55 {
56 if (win32_check_dha())
57 {
58 free(h->device); h->device = NULL;
59 return ERR_DHA_NOT_FOUND;
60 }
61 }
62 else if (win32_check_os() == WIN_64)
63 {
64 if (win32_check_rwp())
65 {
66 free(h->device); h->device = NULL;
67 return ERR_DHA_NOT_FOUND;
68 }
69 }
70
71 return 0;
72 }
73
74 static int par_open(CableHandle *h)
75 {
76 int ret;
77 ret = io_open(h->address);
78 if (!ret)
79 {
80 io_wr(lpt_ctl, io_rd(lpt_ctl) & ~0x20); // output mode only
81 }
82 return ret;
83 }
84
85 static int par_close(CableHandle *h)
86 {
87 return io_close(h->address);
88 }
89
90 static int par_reset(CableHandle *h)
91 {
92 tiTIME clk;
93
94 // wait for releasing of lines
95 TO_START(clk);
96 do
97 {
98 io_wr(lpt_out, 3);
99 if (TO_ELAPSED(clk, h->timeout))
100 return 0;
101 } while ((io_rd(lpt_in) & 0x30) != 0x30);
102
103 return 0;
104 }
105
106 static int par_probe(CableHandle *h)
107 {
108 int timeout = 1;
109 tiTIME clk;
110
111 // 1
112 io_wr(lpt_out, 2);
113 TO_START(clk);
114 do
115 {
116 if (TO_ELAPSED(clk, timeout))
117 {
118 return ERR_WRITE_TIMEOUT;
119 }
120 }
121 while ((io_rd(lpt_in) & 0x10));
122
123 io_wr(lpt_out, 3);
124 TO_START(clk);
125 do
126 {
127 if (TO_ELAPSED(clk, timeout))
128 {
129 return ERR_WRITE_TIMEOUT;
130 }
131 }
132 while (!(io_rd(lpt_in) & 0x10));
133
134 // 0
135 io_wr(lpt_out, 1);
136 TO_START(clk);
137 do
138 {
139 if (TO_ELAPSED(clk, timeout))
140 {
141 return ERR_WRITE_TIMEOUT;
142 }
143 }
144 while (io_rd(lpt_in) & 0x20);
145
146 io_wr(lpt_out, 3);
147 TO_START(clk);
148 do
149 {
150 if (TO_ELAPSED(clk, timeout))
151 {
152 return ERR_WRITE_TIMEOUT;
153 }
154 }
155 while (!(io_rd(lpt_in) & 0x20));
156
157 return 0;
158 }
159
160 static int par_put(CableHandle *h, uint8_t *data, uint32_t len)
161 {
162 int bit;
163 unsigned int i;
164 unsigned int j;
165 tiTIME clk;
166
167 for (j = 0; j < len; j++)
168 {
169 uint8_t curbyte = data[j];
170
171 for (bit = 0; bit < 8; bit++)
172 {
173 if (curbyte & 1)
174 {
175 io_wr(lpt_out, 2);
176 TO_START(clk);
177 do
178 {
179 if (TO_ELAPSED(clk, h->timeout))
180 {
181 return ERR_WRITE_TIMEOUT;
182 }
183 } while ((io_rd(lpt_in) & 0x10));
184
185 io_wr(lpt_out, 3);
186 TO_START(clk);
187 do
188 {
189 if (TO_ELAPSED(clk, h->timeout))
190 {
191 return ERR_WRITE_TIMEOUT;
192 }
193 } while (!(io_rd(lpt_in) & 0x10));
194 }
195 else
196 {
197 io_wr(lpt_out, 1);
198 TO_START(clk);
199 do
200 {
201 if (TO_ELAPSED(clk, h->timeout))
202 {
203 return ERR_WRITE_TIMEOUT;
204 }
205 } while (io_rd(lpt_in) & 0x20);
206
207 io_wr(lpt_out, 3);
208 TO_START(clk);
209 do
210 {
211 if (TO_ELAPSED(clk, h->timeout))
212 {
213 return ERR_WRITE_TIMEOUT;
214 }
215 } while (!(io_rd(lpt_in) & 0x20));
216 }
217
218 curbyte >>= 1;
219 for (i = 0; i < h->delay; i++)
220 {
221 io_rd(lpt_in);
222 }
223 }
224 }
225
226 return 0;
227 }
228
229 static int par_get(CableHandle *h, uint8_t *data, uint32_t len)
230 {
231 int bit;
232 unsigned int i;
233 unsigned int j;
234 tiTIME clk;
235
236 for(j = 0; j < len; j++)
237 {
238 uint8_t v, curbyte = 0;
239
240 for (bit = 0; bit < 8; bit++)
241 {
242 TO_START(clk);
243 while ((v = io_rd(lpt_in) & 0x30) == 0x30)
244 {
245 if (TO_ELAPSED(clk, h->timeout))
246 {
247 return ERR_READ_TIMEOUT;
248 }
249 }
250
251 if (v == 0x10)
252 {
253 curbyte = (curbyte >> 1) | 0x80;
254 io_wr(lpt_out, 1);
255
256 TO_START(clk);
257 while ((io_rd(lpt_in) & 0x20) == 0x00)
258 {
259 if (TO_ELAPSED(clk, h->timeout))
260 {
261 return ERR_WRITE_TIMEOUT;
262 }
263 }
264 io_wr(lpt_out, 3);
265 }
266 else
267 {
268 curbyte = (curbyte >> 1) & 0x7F;
269 io_wr(lpt_out, 2);
270
271 TO_START(clk);
272 while ((io_rd(lpt_in) & 0x10) == 0x00)
273 {
274 if (TO_ELAPSED(clk, h->timeout))
275 {
276 return ERR_WRITE_TIMEOUT;
277 }
278 }
279 io_wr(lpt_out, 3);
280 }
281
282 for (i = 0; i < h->delay; i++)
283 {
284 io_rd(lpt_in);
285 }
286 }
287
288 data[j] = curbyte;
289 }
290
291 return 0;
292 }
293
294 static int par_check(CableHandle *h, int *status)
295 {
296 *status = STATUS_NONE;
297
298 if (!((io_rd(lpt_in) & 0x30) == 0x30))
299 {
300 *status = STATUS_RX | STATUS_TX;
301 }
302
303 return 0;
304 }
305
306 #define swap_bits(a) (((a&2)>>1) | ((a&1)<<1)) // swap the 2 lowest bits
307
308 static int par_set_red_wire(CableHandle *h, int b)
309 {
310 int v = swap_bits(io_rd(lpt_in) >> 4);
311
312 if (b)
313 {
314 io_wr(lpt_out, v | 0x02);
315 }
316 else
317 {
318 io_wr(lpt_out, v & ~0x02);
319 }
320
321 return 0;
322 }
323
324 static int par_set_white_wire(CableHandle *h, int b)
325 {
326 int v = swap_bits(io_rd(lpt_in) >> 4);
327
328 if (b)
329 {
330 io_wr(lpt_out, v | 0x01);
331 }
332 else
333 {
334 io_wr(lpt_out, v & ~0x01);
335 }
336
337 return 0;
338 }
339
340 static int par_get_red_wire(CableHandle *h)
341 {
342 return (0x10 & io_rd(lpt_in)) ? 1 : 0;
343 }
344
345 static int par_get_white_wire(CableHandle *h)
346 {
347 return (0x20 & io_rd(lpt_in)) ? 1 : 0;
348 }
349
350 static int par_set_raw(CableHandle *h, int state)
351 {
352 io_wr(lpt_out, swap_bits(state));
353 return 0;
354 }
355
356 static int par_get_raw(CableHandle *h, int *state)
357 {
358 *state = (io_rd(lpt_in) >> 4) & 3;
359 return 0;
360 }
361
362 static int par_set_device(CableHandle *h, const char * device)
363 {
364 if (device != NULL)
365 {
366 char * device2 = strdup(device);
367 if (device2 != NULL)
368 {
369 free(h->device);
370 h->device = device2;
371 }
372 else
373 {
374 ticables_warning(_("unable to set device %s.\n"), device);
375 }
376 return 0;
377 }
378 return ERR_ILLEGAL_ARG;
379 }
380
381 extern const CableFncts cable_par =
382 {
383 CABLE_PAR,
384 "PAR",
385 N_("Parallel"),
386 N_("Home-made parallel cable"),
387 !0,
388 &par_prepare,
389 &par_open, &par_close, &par_reset, &par_probe, NULL,
390 &par_put, &par_get, &par_check,
391 &par_set_red_wire, &par_set_white_wire,
392 &par_get_red_wire, &par_get_white_wire,
393 &par_set_raw, &par_get_raw,
394 &par_set_device,
395 NULL
396 };