Debian Codegraph @

src/linux/link_ser.cc

raw
1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id: link_ser2.c 1033 2005-05-06 18:17:02Z roms $ */
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 serial" link & "Black TIGraphLink" link unit (low-level I/O routines) */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include "../ticables.h"
34 #include "../logging.h"
35 #include "../error.h"
36 #include "../gettext.h"
37 #include "../internal.h"
38 #include "ioports.h"
39 #include "detect.h"
40 #include "../bsd/detect.h"
41 #include "../macos/detect.h"
42
43 #define dev_fd (GPOINTER_TO_INT(h->priv))
44
45 #if defined(__BSD__)
46 #if defined(__NetBSD__)
47 #define DEVNAME "dty0"
48 #else /* FreeBSD */
49 #define DEVNAME "cuad"
50 #endif
51 #else
52 #define DEVNAME "ttyS"
53 #endif
54
55 static int ser_prepare(CableHandle *h)
56 {
57 const char * device;
58 int ret;
59
60 switch(h->port)
61 {
62 case PORT_1: h->address = 0x3f8; device = "/dev/" DEVNAME "0"; break;
63 case PORT_2: h->address = 0x2f8; device = "/dev/" DEVNAME "1"; break;
64 case PORT_3: h->address = 0x3e8; device = "/dev/" DEVNAME "2"; break;
65 case PORT_4: h->address = 0x3e8; device = "/dev/" DEVNAME "3"; break;
66 default: return ERR_ILLEGAL_ARG;
67 }
68
69 if (h->device == NULL)
70 {
71 h->device = strdup(device);
72 }
73
74 // detect stuff
75 #if defined(__MACOSX__)
76 ret = macosx_check_tty(h->device);
77 #elif defined(__BSD__)
78 ret = bsd_check_tty(h->device);
79 #else
80 ret = linux_check_tty(h->device);
81 #endif
82 if (ret)
83 {
84 free(h->device); h->device = NULL;
85 return ret;
86 }
87
88 return 0;
89 }
90
91 static int ser_open(CableHandle *h)
92 {
93 int ret;
94 int fd;
95
96 ret = ser_io_open(h->device, &fd);
97 if (!ret)
98 {
99 h->priv = GINT_TO_POINTER(fd);
100 }
101
102 return ret;
103 }
104
105 static int ser_close(CableHandle *h)
106 {
107 return ser_io_close(dev_fd);
108 }
109
110 static int ser_reset(CableHandle *h)
111 {
112 tiTIME clk;
113
114 // wait for releasing of lines
115 TO_START(clk);
116 do
117 {
118 ser_io_wr(dev_fd, 3);
119 if (TO_ELAPSED(clk, h->timeout))
120 {
121 return 0;
122 }
123 //printf("%i", ser_io_rd(dev_fd) >> 4);
124 }
125 while ((ser_io_rd(dev_fd) & 0x30) != 0x30);
126
127 return 0;
128 }
129
130 static int ser_put(CableHandle *h, uint8_t *data, uint32_t len)
131 {
132 int bit;
133 unsigned int i;
134 uint32_t j;
135 tiTIME clk;
136
137 for(j = 0; j < len; j++)
138 {
139 uint8_t byte = data[j];
140
141 for (bit = 0; bit < 8; bit++)
142 {
143 if (byte & 1)
144 {
145 ser_io_wr(dev_fd, 2);
146
147 TO_START(clk);
148 while ((ser_io_rd(dev_fd) & 0x10))
149 {
150 if (TO_ELAPSED(clk, h->timeout))
151 {
152 return ERR_WRITE_TIMEOUT;
153 }
154 }
155
156 ser_io_wr(dev_fd, 3);
157 TO_START(clk);
158 while ((ser_io_rd(dev_fd) & 0x10) == 0x00)
159 {
160 if (TO_ELAPSED(clk, h->timeout))
161 {
162 return ERR_WRITE_TIMEOUT;
163 }
164 }
165 }
166 else
167 {
168 ser_io_wr(dev_fd, 1);
169 TO_START(clk);
170 while (ser_io_rd(dev_fd) & 0x20)
171 {
172 if (TO_ELAPSED(clk, h->timeout))
173 {
174 return ERR_WRITE_TIMEOUT;
175 }
176 }
177
178 ser_io_wr(dev_fd, 3);
179 TO_START(clk);
180 while ((ser_io_rd(dev_fd) & 0x20) == 0x00)
181 {
182 if (TO_ELAPSED(clk, h->timeout))
183 {
184 return ERR_WRITE_TIMEOUT;
185 }
186 }
187 }
188
189 byte >>= 1;
190 for (i = 0; i < h->delay; i++)
191 {
192 ser_io_rd(dev_fd);
193 }
194 }
195 }
196
197 return 0;
198 }
199
200 static int ser_get(CableHandle *h, uint8_t *data, uint32_t len)
201 {
202 int bit;
203 unsigned int i;
204 uint32_t j;
205 tiTIME clk;
206
207 for(j = 0; j < len; j++)
208 {
209 uint8_t v, byte = 0;
210
211 for (bit = 0; bit < 8; bit++)
212 {
213 TO_START(clk);
214 while ((v = ser_io_rd(dev_fd) & 0x30) == 0x30)
215 {
216 if (TO_ELAPSED(clk, h->timeout))
217 {
218 return ERR_READ_TIMEOUT;
219 }
220 }
221
222 if (v == 0x10)
223 {
224 byte = (byte >> 1) | 0x80;
225 ser_io_wr(dev_fd, 1);
226
227 TO_START(clk);
228 while ((ser_io_rd(dev_fd) & 0x20) == 0x00)
229 {
230 if (TO_ELAPSED(clk, h->timeout))
231 {
232 return ERR_READ_TIMEOUT;
233 }
234 }
235 ser_io_wr(dev_fd, 3);
236 }
237 else
238 {
239 byte = (byte >> 1) & 0x7F;
240 ser_io_wr(dev_fd, 2);
241
242 TO_START(clk);
243 while ((ser_io_rd(dev_fd) & 0x10) == 0x00)
244 {
245 if (TO_ELAPSED(clk, h->timeout))
246 {
247 return ERR_READ_TIMEOUT;
248 }
249 }
250 ser_io_wr(dev_fd, 3);
251 }
252
253 for (i = 0; i < h->delay; i++)
254 {
255 ser_io_rd(dev_fd);
256 }
257 }
258
259 data[j] = byte;
260 }
261
262 return 0;
263 }
264
265 static int ser_probe(CableHandle *h)
266 {
267 int timeout = 1;
268 tiTIME clk;
269
270 // 1
271 ser_io_wr(dev_fd, 2);
272 TO_START(clk);
273 while ((ser_io_rd(dev_fd) & 0x10))
274 {
275 if (TO_ELAPSED(clk, timeout))
276 {
277 return ERR_WRITE_TIMEOUT;
278 }
279 }
280
281 ser_io_wr(dev_fd, 3);
282 TO_START(clk);
283 while ((ser_io_rd(dev_fd) & 0x10) == 0x00)
284 {
285 if (TO_ELAPSED(clk, timeout))
286 {
287 return ERR_WRITE_TIMEOUT;
288 }
289 }
290
291 // 0
292 ser_io_wr(dev_fd, 1);
293 TO_START(clk);
294 while (ser_io_rd(dev_fd) & 0x20)
295 {
296 if (TO_ELAPSED(clk, timeout))
297 {
298 return ERR_WRITE_TIMEOUT;
299 }
300 }
301
302 ser_io_wr(dev_fd, 3);
303 TO_START(clk);
304 while ((ser_io_rd(dev_fd) & 0x20) == 0x00)
305 {
306 if (TO_ELAPSED(clk, timeout))
307 {
308 return ERR_WRITE_TIMEOUT;
309 }
310 }
311
312 return 0;
313 }
314
315 static int ser_check(CableHandle *h, int *status)
316 {
317 *status = STATUS_NONE;
318
319 if (!((ser_io_rd(dev_fd) & 0x30) == 0x30))
320 {
321 *status = (STATUS_RX | STATUS_TX);
322 }
323
324 return 0;
325 }
326
327 #define swap_bits(a) (((a&2)>>1) | ((a&1)<<1)) // swap the 2 lowest bits
328
329 static int ser_set_red_wire(CableHandle *h, int b)
330 {
331 int v = swap_bits(ser_io_rd(dev_fd) >> 4);
332
333 if (b)
334 {
335 ser_io_wr(dev_fd, v | 0x02);
336 }
337 else
338 {
339 ser_io_wr(dev_fd, v & ~0x02);
340 }
341
342 return 0;
343 }
344
345 static int ser_set_white_wire(CableHandle *h, int b)
346 {
347 int v = swap_bits(ser_io_rd(dev_fd) >> 4);
348
349 if (b)
350 {
351 ser_io_wr(dev_fd, v | 0x01);
352 }
353 else
354 {
355 ser_io_wr(dev_fd, v & ~0x01);
356 }
357
358 return 0;
359 }
360
361 static int ser_get_red_wire(CableHandle *h)
362 {
363 return ((0x10 & ser_io_rd(dev_fd)) ? 1 : 0);
364 }
365
366 static int ser_get_white_wire(CableHandle *h)
367 {
368 return ((0x20 & ser_io_rd(dev_fd)) ? 1 : 0);
369 }
370
371 static int ser_set_raw(CableHandle *h, int state)
372 {
373 ser_io_wr(dev_fd, swap_bits(state));
374 return 0;
375 }
376
377 static int ser_get_raw(CableHandle *h, int *state)
378 {
379 *state = (ser_io_rd(dev_fd) >> 4) & 3;
380 return 0;
381 }
382
383 static int ser_set_device(CableHandle *h, const char * device)
384 {
385 if (device != NULL)
386 {
387 char * device2 = strdup(device);
388 if (device2 != NULL)
389 {
390 free(h->device);
391 h->device = device2;
392 }
393 else
394 {
395 ticables_warning(_("unable to set device %s.\n"), device);
396 }
397 return 0;
398 }
399 return ERR_ILLEGAL_ARG;
400 }
401
402 extern const CableFncts cable_ser =
403 {
404 CABLE_BLK,
405 "BLK",
406 N_("BlackLink"),
407 N_("BlackLink or home-made serial cable"),
408 !0,
409 &ser_prepare,
410 &ser_open, &ser_close, &ser_reset, &ser_probe, NULL,
411 &ser_put, &ser_get, &ser_check,
412 &ser_set_red_wire, &ser_set_white_wire,
413 &ser_get_red_wire, &ser_get_white_wire,
414 &ser_set_raw, &ser_get_raw,
415 &ser_set_device,
416 NULL
417 };