Debian Codegraph @

src/linux/link_gry.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 /* "Grey TIGraphLink" link cable unit */
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 <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <termios.h>
36 #include <unistd.h>
37 #include <sys/ioctl.h>
38
39 #include "../ticables.h"
40 #include "../logging.h"
41 #include "../error.h"
42 #include "../gettext.h"
43 #include "../internal.h"
44 #include "detect.h"
45 #include "../bsd/detect.h"
46 #include "../macos/detect.h"
47
48 #define dev_fd (GPOINTER_TO_INT(h->priv))
49 #define termset ((struct termios *)(h->priv2))
50
51 #if defined(__BSD__)
52 #if defined(__NetBSD__)
53 #define DEVNAME "dty0"
54 #else /* FreeBSD */
55 #define DEVNAME "cuad"
56 #endif
57 #else
58 #define DEVNAME "ttyS"
59 #endif
60
61 static int gry_prepare(CableHandle *h)
62 {
63 const char * device;
64 int ret;
65
66 switch (h->port)
67 {
68 case PORT_1: h->address = 0x3f8; device = "/dev/" DEVNAME "0"; break;
69 case PORT_2: h->address = 0x2f8; device = "/dev/" DEVNAME "1"; break;
70 case PORT_3: h->address = 0x3e8; device = "/dev/" DEVNAME "2"; break;
71 case PORT_4: h->address = 0x3e8; device = "/dev/" DEVNAME "3"; break;
72 default: return ERR_ILLEGAL_ARG;
73 }
74
75 if (h->device == NULL)
76 {
77 h->device = strdup(device);
78 }
79 h->priv2 = (struct termios *)calloc(1, sizeof(struct termios));
80
81 #if defined(__MACOSX__)
82 ret = macosx_check_tty(h->device);
83 #elif defined(__BSD__)
84 ret = bsd_check_tty(h->device);
85 #else
86 ret = linux_check_tty(h->device);
87 #endif
88 if (ret)
89 {
90 free(h->device); h->device = NULL;
91 free(h->priv2); h->priv2 = NULL;
92 return ret;
93 }
94
95 return 0;
96 }
97
98 static int gry_open(CableHandle *h)
99 {
100 int flags = 0;
101
102 #if defined(__MACOSX__)
103 flags = O_RDWR | O_NDELAY;
104 #elif defined(__BSD__)
105 flags = O_RDWR | O_FSYNC;
106 #elif defined(__LINUX__)
107 flags = O_RDWR | O_SYNC;
108 #endif
109
110 //dev_fd = (int)open(h->device, flags);
111 h->priv = GINT_TO_POINTER(open(h->device, flags));
112 if (dev_fd == -1)
113 {
114 if (errno == EACCES)
115 {
116 ticables_warning(_("unable to open this serial port: %s (wrong permissions).\n"), h->device);
117 }
118 else
119 {
120 ticables_warning(_("unable to open this serial port: %s\n"), h->device);
121 }
122 return ERR_GRY_OPEN;
123 }
124
125 // Initialize it: 9600,8,N,1
126 tcgetattr(dev_fd, termset);
127 #ifdef HAVE_CFMAKERAW
128 cfmakeraw(termset);
129 #else
130 termset->c_iflag = 0;
131 termset->c_oflag = 0;
132 termset->c_cflag = CS8 | CLOCAL | CREAD;
133 termset->c_lflag = 0;
134 #endif
135 termset->c_cc[VMIN] = 0;
136 termset->c_cc[VTIME] = h->timeout;
137
138 cfsetispeed(termset, B9600);
139 cfsetospeed(termset, B9600);
140 tcsetattr(dev_fd, TCSANOW, termset);
141
142 if (tcflush(dev_fd, TCIOFLUSH) == -1)
143 {
144 return ERR_FLUSH_ERROR;
145 }
146
147 return 0;
148 }
149
150 static int gry_close(CableHandle *h)
151 {
152 close(dev_fd);
153 free(h->priv2);
154 h->priv2 = NULL;
155
156 return 0;
157 }
158
159 static int gry_reset(CableHandle *h)
160 {
161 if (tcflush(dev_fd, TCIOFLUSH) == -1)
162 {
163 return ERR_FLUSH_ERROR;
164 }
165
166 return 0;
167 }
168
169 static int gry_put(CableHandle* h, uint8_t *data, uint32_t len)
170 {
171 ssize_t ret;
172
173 ret = write(dev_fd, (void *)data, len);
174 switch (ret)
175 {
176 case -1: //error
177 return ERR_WRITE_ERROR;
178 case 0: // timeout
179 return ERR_WRITE_TIMEOUT;
180 }
181
182 return 0;
183 }
184
185 static int gry_get(CableHandle* h, uint8_t *data, uint32_t len)
186 {
187 ssize_t ret;
188 ssize_t i;
189
190 tcdrain(dev_fd); // waits for all output written
191
192 // Doesn't work as expected by the manpage. Use a 'for' loop instead.
193 //termset->c_cc[VMIN] = len;
194 //tcsetattr(dev_fd, TCSANOW, termset);
195
196 for(i = 0; i < len; )
197 {
198 ret = read(dev_fd, (void *)(data+i), len - i);
199 switch (ret)
200 {
201 case -1: //error
202 return ERR_READ_ERROR;
203 case 0: // timeout
204 return ERR_READ_TIMEOUT;
205 }
206
207 i += ret;
208 }
209
210 return 0;
211 }
212
213 // Migrate these functions into ioports.c
214 static int dcb_read_io(CableHandle *h)
215 {
216 #ifdef HAVE_TERMIOS_H
217 unsigned int flags;
218
219 if (ioctl(dev_fd, TIOCMGET, &flags) == -1)
220 {
221 return ERR_GRY_IOCTL;
222 }
223
224 return (flags & TIOCM_CTS ? 1 : 0) | (flags & TIOCM_DSR ? 2 : 0);
225 #endif
226 }
227
228 static int dcb_write_io(CableHandle *h, int data)
229 {
230 #ifdef HAVE_TERMIOS_H
231 unsigned int flags = 0;
232
233 flags |= (data & 2) ? TIOCM_RTS : 0;
234 flags |= (data & 1) ? TIOCM_DTR : 0;
235 if (ioctl(dev_fd, TIOCMSET, &flags) == -1)
236 {
237 return ERR_GRY_IOCTL;
238 }
239
240 return 0;
241 #endif
242 }
243
244 static int gry_probe(CableHandle *h)
245 {
246 int i;
247 int seq_in[] = { 3, 2, 0, 1, 3 };
248 int seq_out[] = { 2, 0, 0, 2, 2 };
249
250 for (i = 0; i < 5; i++)
251 {
252 dcb_write_io(h, seq_in[i]);
253 usleep(1000000);
254 //printf("%i : %i\n", seq[i], dcb_read_io() & 0x3);
255
256 if ((dcb_read_io(h) & 0x3) != seq_out[i])
257 {
258 dcb_write_io(h, 3);
259 return ERR_PROBE_FAILED;
260 }
261 }
262
263 return 0;
264 }
265
266 static int gry_check(CableHandle *h, int *status)
267 {
268 fd_set rdfs;
269 struct timeval tv;
270 int retval;
271
272 if (dev_fd < 0)
273 {
274 return ERR_READ_ERROR;
275 }
276
277 *status = STATUS_NONE;
278
279 FD_ZERO(&rdfs);
280 FD_SET(dev_fd, &rdfs);
281 tv.tv_sec = 0;
282 tv.tv_usec = 0;
283
284 retval = select(dev_fd + 1, &rdfs, NULL, NULL, &tv);
285 switch (retval) {
286 case -1: //error
287 return ERR_READ_ERROR;
288 case 0: //no data
289 return 0;
290 default: // data available
291 *status = STATUS_RX;
292 break;
293 }
294
295 return 0;
296 }
297
298 static int gry_timeout(CableHandle *h)
299 {
300 termset->c_cc[VTIME] = h->timeout;
301 tcsetattr(dev_fd, TCSANOW, termset);
302
303 return 0;
304 }
305
306 static int gry_set_device(CableHandle *h, const char * device)
307 {
308 if (device != NULL)
309 {
310 char * device2 = strdup(device);
311 if (device2 != NULL)
312 {
313 free(h->device);
314 h->device = device2;
315 }
316 else
317 {
318 ticables_warning(_("unable to set device %s.\n"), device);
319 }
320 return 0;
321 }
322 return ERR_ILLEGAL_ARG;
323 }
324
325 extern const CableFncts cable_gry =
326 {
327 CABLE_GRY,
328 "GRY",
329 N_("GrayLink"),
330 N_("GrayLink serial cable"),
331 !0,
332 &gry_prepare,
333 &gry_open, &gry_close, &gry_reset, &gry_probe, &gry_timeout,
334 &gry_put, &gry_get, &gry_check,
335 &noop_set_red_wire, &noop_set_white_wire,
336 &noop_get_red_wire, &noop_get_white_wire,
337 NULL, NULL,
338 &gry_set_device,
339 NULL
340 };