1 | /*
|
---|
2 | io_serial.c
|
---|
3 | Serial port input/output functions
|
---|
4 |
|
---|
5 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
6 | Copyright (C) 2000 2001 Carlos Prados <cprados@yahoo.com>
|
---|
7 |
|
---|
8 | This version is modified by doz21 to work in a special manner ;)
|
---|
9 |
|
---|
10 | This library is free software; you can redistribute it and/or
|
---|
11 | modify it under the terms of the GNU Lesser General Public
|
---|
12 | License as published by the Free Software Foundation; either
|
---|
13 | version 2 of the License, or (at your option) any later version.
|
---|
14 |
|
---|
15 | This library is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | Lesser General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU Lesser General Public
|
---|
21 | License along with this library; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "defines.h"
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <errno.h>
|
---|
30 | #ifdef OS_HPUX
|
---|
31 | # include <sys/modem.h>
|
---|
32 | #endif
|
---|
33 | #include <termios.h>
|
---|
34 | #include <unistd.h>
|
---|
35 | #include <sys/stat.h>
|
---|
36 | #include <fcntl.h>
|
---|
37 | #ifdef HAVE_POLL
|
---|
38 | # include <sys/poll.h>
|
---|
39 | #else
|
---|
40 | # include <sys/signal.h>
|
---|
41 | # include <sys/types.h>
|
---|
42 | #endif
|
---|
43 | #include <sys/time.h>
|
---|
44 | #include <sys/ioctl.h>
|
---|
45 | #include <time.h>
|
---|
46 | #include "io_serial.h"
|
---|
47 | #include "mc_global.h"
|
---|
48 |
|
---|
49 | #ifdef OS_LINUX
|
---|
50 | # include <linux/serial.h>
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Internal functions declaration
|
---|
55 | */
|
---|
56 |
|
---|
57 | static int IO_Serial_Bitrate_to_Speed(int bitrate);
|
---|
58 |
|
---|
59 | static int IO_Serial_Bitrate_from_Speed(int speed);
|
---|
60 |
|
---|
61 | static bool IO_Serial_WaitToRead(int hnd, unsigned delay_ms, unsigned timeout_ms);
|
---|
62 |
|
---|
63 | static bool IO_Serial_WaitToWrite(IO_Serial * io, unsigned delay_ms, unsigned timeout_ms);
|
---|
64 |
|
---|
65 | static bool IO_Serial_InitPnP(IO_Serial * io);
|
---|
66 |
|
---|
67 | static void IO_Serial_Clear(IO_Serial * io);
|
---|
68 |
|
---|
69 | static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props);
|
---|
70 |
|
---|
71 | static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props);
|
---|
72 |
|
---|
73 | static void IO_Serial_ClearPropertiesCache(IO_Serial * io);
|
---|
74 |
|
---|
75 | static int _in_echo_read = 0;
|
---|
76 | extern int reader_serial_need_dummy_char;
|
---|
77 |
|
---|
78 | extern unsigned long reader_serial_bitrate_optimal;
|
---|
79 | extern unsigned long reader_serial_bitrate_effective;
|
---|
80 |
|
---|
81 | int fdmc = (-1);
|
---|
82 |
|
---|
83 | #if defined(TUXBOX) && defined(PPC)
|
---|
84 | extern int *oscam_sem;
|
---|
85 |
|
---|
86 | void IO_Serial_Ioctl_Lock(IO_Serial * io, int flag)
|
---|
87 | {
|
---|
88 | if ((io->reader_type != RTYP_DB2COM1) && (io->reader_type != RTYP_DB2COM2))
|
---|
89 | return;
|
---|
90 | if (!flag)
|
---|
91 | *oscam_sem = 0;
|
---|
92 | else
|
---|
93 | while (*oscam_sem != io->reader_type) {
|
---|
94 | while (*oscam_sem)
|
---|
95 | usleep(2000);
|
---|
96 | *oscam_sem = io->reader_type;
|
---|
97 | usleep(1000);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | static bool IO_Serial_DTR_RTS_dbox2(int mcport, int dtr, int set)
|
---|
102 | {
|
---|
103 | int rc;
|
---|
104 | unsigned short msr;
|
---|
105 | unsigned short rts_bits[2] = { 0x10, 0x800 };
|
---|
106 | unsigned short dtr_bits[2] = { 0x100, 0 };
|
---|
107 |
|
---|
108 | # ifdef DEBUG_IO
|
---|
109 | printf("IO: multicam.o %s %s\n", dtr ? "dtr" : "rts", set ? "set" : "clear");
|
---|
110 | fflush(stdout);
|
---|
111 | # endif
|
---|
112 | if ((rc = ioctl(fdmc, GET_PCDAT, &msr)) >= 0) {
|
---|
113 | if (dtr) // DTR
|
---|
114 | {
|
---|
115 | if (dtr_bits[mcport]) {
|
---|
116 | if (set)
|
---|
117 | msr &= (unsigned short) (~dtr_bits[mcport]);
|
---|
118 | else
|
---|
119 | msr |= dtr_bits[mcport];
|
---|
120 | rc = ioctl(fdmc, SET_PCDAT, &msr);
|
---|
121 | } else
|
---|
122 | rc = 0; // Dummy, can't handle using multicam.o
|
---|
123 | } else // RTS
|
---|
124 | {
|
---|
125 | if (set)
|
---|
126 | msr &= (unsigned short) (~rts_bits[mcport]);
|
---|
127 | else
|
---|
128 | msr |= rts_bits[mcport];
|
---|
129 | rc = ioctl(fdmc, SET_PCDAT, &msr);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | return ((rc < 0) ? FALSE : TRUE);
|
---|
133 | }
|
---|
134 | #endif
|
---|
135 |
|
---|
136 | bool IO_Serial_DTR_RTS(IO_Serial * io, int dtr, int set)
|
---|
137 | {
|
---|
138 | unsigned int msr;
|
---|
139 | unsigned int mbit;
|
---|
140 |
|
---|
141 | #if defined(TUXBOX) && defined(PPC)
|
---|
142 | if ((io->reader_type == RTYP_DB2COM1) || (io->reader_type == RTYP_DB2COM2))
|
---|
143 | return (IO_Serial_DTR_RTS_dbox2(io->reader_type == RTYP_DB2COM2, dtr, set));
|
---|
144 | #endif
|
---|
145 |
|
---|
146 | mbit = (dtr) ? TIOCM_DTR : TIOCM_RTS;
|
---|
147 | #if defined(TIOCMBIS) && defined(TIOBMBIC)
|
---|
148 | if (ioctl(io->fd, set ? TIOCMBIS : TIOCMBIC, &mbit) < 0)
|
---|
149 | return FALSE;
|
---|
150 | #else
|
---|
151 | if (ioctl(io->fd, TIOCMGET, &msr) < 0)
|
---|
152 | return FALSE;
|
---|
153 | if (set)
|
---|
154 | msr |= mbit;
|
---|
155 | else
|
---|
156 | msr &= ~mbit;
|
---|
157 | return ((ioctl(io->fd, TIOCMSET, &msr) < 0) ? FALSE : TRUE);
|
---|
158 | #endif
|
---|
159 | }
|
---|
160 |
|
---|
161 | /*
|
---|
162 | * Public functions definition
|
---|
163 | */
|
---|
164 |
|
---|
165 | IO_Serial *IO_Serial_New(void)
|
---|
166 | {
|
---|
167 | IO_Serial *io;
|
---|
168 |
|
---|
169 | io = (IO_Serial *) malloc(sizeof (IO_Serial));
|
---|
170 |
|
---|
171 | if (io != NULL)
|
---|
172 | IO_Serial_Clear(io);
|
---|
173 |
|
---|
174 | return io;
|
---|
175 | }
|
---|
176 |
|
---|
177 | bool IO_Serial_Init(IO_Serial * io, char *device, unsigned long frequency, unsigned short reader_type, bool pnp)
|
---|
178 | {
|
---|
179 | #ifdef DEBUG_IO
|
---|
180 | printf("IO: Opening serial port %s\n", device);
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | memcpy(io->device, device, sizeof(io->device));
|
---|
184 | io->frequency = frequency;
|
---|
185 | io->reader_type = reader_type;
|
---|
186 |
|
---|
187 | #ifdef SCI_DEV
|
---|
188 | if (reader_type == RTYP_SCI)
|
---|
189 | io->fd = open(io->device, O_RDWR);
|
---|
190 | else
|
---|
191 | #endif
|
---|
192 |
|
---|
193 | #ifdef OS_MACOSX
|
---|
194 | io->fd = open(io->device, O_RDWR | O_NOCTTY | O_NDELAY);
|
---|
195 | #else
|
---|
196 | io->fd = open(io->device, O_RDWR | O_NOCTTY | O_SYNC);
|
---|
197 | #endif
|
---|
198 |
|
---|
199 | if (io->fd < 0)
|
---|
200 | return FALSE;
|
---|
201 |
|
---|
202 | #if defined(TUXBOX) && defined(PPC)
|
---|
203 | if ((reader_type == RTYP_DB2COM1) || (reader_type == RTYP_DB2COM2))
|
---|
204 | if ((fdmc = open(DEV_MULTICAM, O_RDWR)) < 0) {
|
---|
205 | close(io->fd);
|
---|
206 | return FALSE;
|
---|
207 | }
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | if (reader_type != RTYP_SCI) {
|
---|
211 | if (pnp)
|
---|
212 | IO_Serial_InitPnP(io);
|
---|
213 | }
|
---|
214 |
|
---|
215 | return TRUE;
|
---|
216 | }
|
---|
217 |
|
---|
218 | bool IO_Serial_GetProperties(IO_Serial * io, IO_Serial_Properties * props)
|
---|
219 | {
|
---|
220 | struct termios currtio;
|
---|
221 | speed_t i_speed, o_speed;
|
---|
222 | unsigned int mctl;
|
---|
223 |
|
---|
224 | #ifdef SCI_DEV
|
---|
225 | if (io->reader_type == RTYP_SCI)
|
---|
226 | return FALSE;
|
---|
227 | #endif
|
---|
228 |
|
---|
229 | if (IO_Serial_GetPropertiesCache(io, props))
|
---|
230 | return TRUE;
|
---|
231 |
|
---|
232 | if (tcgetattr(io->fd, &currtio) != 0)
|
---|
233 | return FALSE;
|
---|
234 |
|
---|
235 | o_speed = cfgetospeed(&currtio);
|
---|
236 | props->output_bitrate = IO_Serial_Bitrate_from_Speed(o_speed);
|
---|
237 |
|
---|
238 | i_speed = cfgetispeed(&currtio);
|
---|
239 | props->input_bitrate = IO_Serial_Bitrate_from_Speed(i_speed);
|
---|
240 |
|
---|
241 | /* Check for custom bitrate */
|
---|
242 | #ifdef OS_LINUX
|
---|
243 | if (props->input_bitrate == 38400 && props->output_bitrate == 38400) {
|
---|
244 | struct serial_struct s;
|
---|
245 | bzero (&s, sizeof (s));
|
---|
246 | if (ioctl(io->fd, TIOCGSERIAL, &s) >= 0) {
|
---|
247 | if ((s.flags & ASYNC_SPD_CUST) != 0 && s.baud_base > 0 && s.custom_divisor > 0) {
|
---|
248 | unsigned long effective_bitrate = (unsigned long) s.baud_base / s.custom_divisor;
|
---|
249 | props->input_bitrate = effective_bitrate;
|
---|
250 | props->output_bitrate = effective_bitrate;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | switch (currtio.c_cflag & CSIZE) {
|
---|
257 | case CS5:
|
---|
258 | props->bits = 5;
|
---|
259 | break;
|
---|
260 | case CS6:
|
---|
261 | props->bits = 6;
|
---|
262 | break;
|
---|
263 | case CS7:
|
---|
264 | props->bits = 7;
|
---|
265 | break;
|
---|
266 | case CS8:
|
---|
267 | props->bits = 8;
|
---|
268 | break;
|
---|
269 | }
|
---|
270 |
|
---|
271 | if (((currtio.c_cflag) & PARENB) == PARENB) {
|
---|
272 | if (((currtio.c_cflag) & PARODD) == PARODD)
|
---|
273 | props->parity = IO_SERIAL_PARITY_ODD;
|
---|
274 | else
|
---|
275 | props->parity = IO_SERIAL_PARITY_EVEN;
|
---|
276 | } else {
|
---|
277 | props->parity = IO_SERIAL_PARITY_NONE;
|
---|
278 | }
|
---|
279 |
|
---|
280 | if (((currtio.c_cflag) & CSTOPB) == CSTOPB)
|
---|
281 | props->stopbits = 2;
|
---|
282 | else
|
---|
283 | props->stopbits = 1;
|
---|
284 |
|
---|
285 | if (ioctl(io->fd, TIOCMGET, &mctl) < 0)
|
---|
286 | return FALSE;
|
---|
287 |
|
---|
288 | props->dtr = ((mctl & TIOCM_DTR) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
|
---|
289 | props->rts = ((mctl & TIOCM_RTS) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
|
---|
290 |
|
---|
291 | IO_Serial_SetPropertiesCache(io, props);
|
---|
292 |
|
---|
293 | #ifdef DEBUG_IO
|
---|
294 | printf("IO: Getting properties: %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", props->input_bitrate, props->bits, props->parity == IO_SERIAL_PARITY_EVEN ? "Even" : props->parity == IO_SERIAL_PARITY_ODD ? "Odd" : "None", props->stopbits, props->dtr, props->rts);
|
---|
295 | #endif
|
---|
296 |
|
---|
297 | return TRUE;
|
---|
298 | }
|
---|
299 |
|
---|
300 | bool IO_Serial_SetProperties(IO_Serial * io, IO_Serial_Properties * props)
|
---|
301 | {
|
---|
302 | struct termios newtio;
|
---|
303 |
|
---|
304 | #ifdef SCI_DEV
|
---|
305 | if (io->reader_type == RTYP_SCI)
|
---|
306 | return FALSE;
|
---|
307 | #endif
|
---|
308 |
|
---|
309 | memset(&newtio, 0, sizeof (newtio));
|
---|
310 |
|
---|
311 | /* Save the optimal bitrate value for OScam */
|
---|
312 | reader_serial_bitrate_optimal = props->output_bitrate;
|
---|
313 |
|
---|
314 | /* Set the bitrate */
|
---|
315 | #ifdef DEBUG_IO
|
---|
316 | printf("IO: Optimal bitrate should be = %lu\n", props->output_bitrate);
|
---|
317 | #endif
|
---|
318 | #ifdef OS_LINUX
|
---|
319 | /* Check if the bitrate is not a standard value */
|
---|
320 | if (props->output_bitrate != props->input_bitrate || (props->output_bitrate == 230400 || props->output_bitrate == 115200 || props->output_bitrate == 57600 || props->output_bitrate == 38400 || props->output_bitrate == 19200 || props->output_bitrate == 9600 || props->output_bitrate == 4800 || props->output_bitrate == 2400 || props->output_bitrate == 1800 || props->output_bitrate == 1200 || props->output_bitrate == 600 || props->output_bitrate == 300 || props->output_bitrate == 200 || props->output_bitrate == 150 || props->output_bitrate == 134 || props->output_bitrate == 110 || props->output_bitrate == 75 || props->output_bitrate == 50 || props->output_bitrate == 0)) {
|
---|
321 | # ifdef DEBUG_IO
|
---|
322 | printf("IO: Using standard bitrate of %lu\n", props->output_bitrate);
|
---|
323 | # endif
|
---|
324 | #endif
|
---|
325 | /* Standard bitrate */
|
---|
326 | int output_speed = IO_Serial_Bitrate_to_Speed(props->output_bitrate);
|
---|
327 | int input_speed = IO_Serial_Bitrate_to_Speed(props->input_bitrate);
|
---|
328 | cfsetospeed(&newtio, output_speed);
|
---|
329 | cfsetispeed(&newtio, input_speed);
|
---|
330 |
|
---|
331 | /* Save the effective bitrate value for OScam */
|
---|
332 | reader_serial_bitrate_effective = IO_Serial_Bitrate_from_Speed(output_speed);
|
---|
333 | #ifdef OS_LINUX
|
---|
334 | } else {
|
---|
335 | /* Special bitrate : these structures are only available on linux as fas as we know so limit this code to OS_LINUX */
|
---|
336 | unsigned long standard_bitrate = props->output_bitrate;
|
---|
337 | unsigned long effective_bitrate = IO_Serial_Bitrate_from_Speed(IO_Serial_Bitrate_to_Speed(standard_bitrate));
|
---|
338 |
|
---|
339 | struct serial_struct s;
|
---|
340 | bzero (&s, sizeof (s));
|
---|
341 | if (ioctl(io->fd, TIOCGSERIAL, &s) >= 0) {
|
---|
342 | unsigned long wanted_bitrate = props->output_bitrate;
|
---|
343 | unsigned long custom_divisor = ((unsigned long) s.baud_base + (wanted_bitrate / 2)) / wanted_bitrate;
|
---|
344 | /* Check if custom_divisor is greater than zero */
|
---|
345 | if (custom_divisor > 0) {
|
---|
346 | effective_bitrate = (unsigned long) s.baud_base / custom_divisor;
|
---|
347 |
|
---|
348 | /* Check if a custom_divisor is needed */
|
---|
349 | if (effective_bitrate == 230400 || effective_bitrate == 115200 || effective_bitrate == 57600 || effective_bitrate == 38400 || effective_bitrate == 19200 || effective_bitrate == 9600 || effective_bitrate == 4800 || effective_bitrate == 2400 || effective_bitrate == 1800 || effective_bitrate == 1200 || effective_bitrate == 600 || effective_bitrate == 300 || effective_bitrate == 200 || effective_bitrate == 150 || effective_bitrate == 134 || effective_bitrate == 110 || effective_bitrate == 75 || effective_bitrate == 50 || effective_bitrate == 0) {
|
---|
350 | /* Use standard bitrate value */
|
---|
351 | if ((s.flags & ASYNC_SPD_CUST) != 0) {
|
---|
352 | s.flags |= ASYNC_SPD_MASK;
|
---|
353 | s.flags &= ~ASYNC_SPD_CUST;
|
---|
354 | ioctl(io->fd, TIOCSSERIAL, &s);
|
---|
355 | }
|
---|
356 | standard_bitrate = effective_bitrate;
|
---|
357 | # ifdef DEBUG_IO
|
---|
358 | printf("IO: Using standard bitrate of %lu (baud_base too small)\n", standard_bitrate);
|
---|
359 | # endif
|
---|
360 | } else {
|
---|
361 | /* Use custom divisor */
|
---|
362 | s.custom_divisor = custom_divisor;
|
---|
363 | s.flags &= ~ASYNC_SPD_MASK;
|
---|
364 | s.flags |= ASYNC_SPD_CUST;
|
---|
365 | if (ioctl(io->fd, TIOCSSERIAL, &s) >= 0) {
|
---|
366 | standard_bitrate = 38400;
|
---|
367 | }
|
---|
368 | # ifdef DEBUG_IO
|
---|
369 | printf("IO: Using special bitrate of = %lu (%+.2f%% off)\n", effective_bitrate, ((double) (effective_bitrate - wanted_bitrate)) / wanted_bitrate * 100);
|
---|
370 | # endif
|
---|
371 | }
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | /* Save the effective bitrate value for OScam */
|
---|
376 | reader_serial_bitrate_effective = effective_bitrate;
|
---|
377 |
|
---|
378 | /* Set the standard bitrate value */
|
---|
379 | cfsetospeed(&newtio, IO_Serial_Bitrate_to_Speed(standard_bitrate));
|
---|
380 | cfsetispeed(&newtio, IO_Serial_Bitrate_to_Speed(standard_bitrate));
|
---|
381 | }
|
---|
382 | #endif
|
---|
383 |
|
---|
384 | /* Set the character size */
|
---|
385 | switch (props->bits) {
|
---|
386 | case 5:
|
---|
387 | newtio.c_cflag |= CS5;
|
---|
388 | break;
|
---|
389 |
|
---|
390 | case 6:
|
---|
391 | newtio.c_cflag |= CS6;
|
---|
392 | break;
|
---|
393 |
|
---|
394 | case 7:
|
---|
395 | newtio.c_cflag |= CS7;
|
---|
396 | break;
|
---|
397 |
|
---|
398 | case 8:
|
---|
399 | newtio.c_cflag |= CS8;
|
---|
400 | break;
|
---|
401 | }
|
---|
402 |
|
---|
403 | /* Set the parity */
|
---|
404 | switch (props->parity) {
|
---|
405 | case IO_SERIAL_PARITY_ODD:
|
---|
406 | newtio.c_cflag |= PARENB;
|
---|
407 | newtio.c_cflag |= PARODD;
|
---|
408 | break;
|
---|
409 |
|
---|
410 | case IO_SERIAL_PARITY_EVEN:
|
---|
411 | newtio.c_cflag |= PARENB;
|
---|
412 | newtio.c_cflag &= ~PARODD;
|
---|
413 | break;
|
---|
414 |
|
---|
415 | case IO_SERIAL_PARITY_NONE:
|
---|
416 | newtio.c_cflag &= ~PARENB;
|
---|
417 | break;
|
---|
418 | }
|
---|
419 |
|
---|
420 | /* Set the number of stop bits */
|
---|
421 | switch (props->stopbits) {
|
---|
422 | case 1:
|
---|
423 | newtio.c_cflag &= (~CSTOPB);
|
---|
424 | break;
|
---|
425 | case 2:
|
---|
426 | newtio.c_cflag |= CSTOPB;
|
---|
427 | break;
|
---|
428 | }
|
---|
429 |
|
---|
430 | /* Selects raw (non-canonical) input and output */
|
---|
431 | newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
---|
432 | newtio.c_oflag &= ~OPOST;
|
---|
433 | #if 1
|
---|
434 | newtio.c_iflag |= IGNPAR;
|
---|
435 | /* Ignore parity errors!!! Windows driver does so why shouldn't I? */
|
---|
436 | #endif
|
---|
437 | /* Enable receiber, hang on close, ignore control line */
|
---|
438 | newtio.c_cflag |= CREAD | HUPCL | CLOCAL;
|
---|
439 |
|
---|
440 | /* Read 1 byte minimun, no timeout specified */
|
---|
441 | newtio.c_cc[VMIN] = 1;
|
---|
442 | newtio.c_cc[VTIME] = 0;
|
---|
443 |
|
---|
444 | // tcdrain(io->fd);
|
---|
445 | if (tcsetattr(io->fd, TCSANOW, &newtio) < 0)
|
---|
446 | return FALSE;
|
---|
447 | // tcflush(io->fd, TCIOFLUSH);
|
---|
448 | // if (tcsetattr (io->fd, TCSAFLUSH, &newtio) < 0)
|
---|
449 | // return FALSE;
|
---|
450 |
|
---|
451 | IO_Serial_Ioctl_Lock(io, 1);
|
---|
452 | IO_Serial_DTR_RTS(io, 0, props->rts == IO_SERIAL_HIGH);
|
---|
453 | IO_Serial_DTR_RTS(io, 1, props->dtr == IO_SERIAL_HIGH);
|
---|
454 | IO_Serial_Ioctl_Lock(io, 0);
|
---|
455 |
|
---|
456 | IO_Serial_SetPropertiesCache(io, props);
|
---|
457 |
|
---|
458 | #ifdef DEBUG_IO
|
---|
459 | printf("IO: Setting properties: device=%s, %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", io->device, props->input_bitrate, props->bits, props->parity == IO_SERIAL_PARITY_EVEN ? "Even" : props->parity == IO_SERIAL_PARITY_ODD ? "Odd" : "None", props->stopbits, props->dtr, props->rts);
|
---|
460 | #endif
|
---|
461 | return TRUE;
|
---|
462 | }
|
---|
463 |
|
---|
464 | void IO_Serial_Flush(IO_Serial * io)
|
---|
465 | {
|
---|
466 | BYTE b;
|
---|
467 |
|
---|
468 | while (IO_Serial_Read(io, 1000, 1, &b));
|
---|
469 | }
|
---|
470 |
|
---|
471 |
|
---|
472 | void IO_Serial_GetPnPId(IO_Serial * io, BYTE * pnp_id, unsigned *length)
|
---|
473 | {
|
---|
474 | (*length) = io->PnP_id_size;
|
---|
475 | memcpy(pnp_id, io->PnP_id, io->PnP_id_size);
|
---|
476 | }
|
---|
477 |
|
---|
478 | bool IO_Serial_Read(IO_Serial * io, unsigned timeout, unsigned size, BYTE * data)
|
---|
479 | {
|
---|
480 | BYTE c;
|
---|
481 | int count = 0;
|
---|
482 |
|
---|
483 |
|
---|
484 | if ((io->reader_type != RTYP_SCI) && (io->wr > 0)) {
|
---|
485 | BYTE buf[256];
|
---|
486 | int n = io->wr;
|
---|
487 |
|
---|
488 | io->wr = 0;
|
---|
489 |
|
---|
490 | if (!IO_Serial_Read(io, timeout, n, buf)) {
|
---|
491 | return FALSE;
|
---|
492 | }
|
---|
493 | }
|
---|
494 | #ifdef DEBUG_IO
|
---|
495 | printf("IO: Receiving: ");
|
---|
496 | fflush(stdout);
|
---|
497 | #endif
|
---|
498 | for (count = 0; count < size * (_in_echo_read ? (1 + reader_serial_need_dummy_char) : 1); count++) {
|
---|
499 | if (IO_Serial_WaitToRead(io->fd, 0, timeout)) {
|
---|
500 | if (read(io->fd, &c, 1) != 1) {
|
---|
501 | #ifdef DEBUG_IO
|
---|
502 | printf("ERROR\n");
|
---|
503 | fflush(stdout);
|
---|
504 | #endif
|
---|
505 | return FALSE;
|
---|
506 | }
|
---|
507 | data[_in_echo_read ? count / (1 + reader_serial_need_dummy_char) : count] = c;
|
---|
508 |
|
---|
509 | #ifdef DEBUG_IO
|
---|
510 | printf("%X ", c);
|
---|
511 | fflush(stdout);
|
---|
512 | #endif
|
---|
513 | } else {
|
---|
514 | #ifdef DEBUG_IO
|
---|
515 | printf("TIMEOUT\n");
|
---|
516 | fflush(stdout);
|
---|
517 | #endif
|
---|
518 | tcflush(io->fd, TCIFLUSH);
|
---|
519 | return FALSE;
|
---|
520 | }
|
---|
521 | }
|
---|
522 |
|
---|
523 | _in_echo_read = 0;
|
---|
524 |
|
---|
525 | #ifdef DEBUG_IO
|
---|
526 | printf("\n");
|
---|
527 | fflush(stdout);
|
---|
528 | #endif
|
---|
529 |
|
---|
530 | return TRUE;
|
---|
531 | }
|
---|
532 |
|
---|
533 | bool IO_Serial_Write(IO_Serial * io, unsigned delay, unsigned size, BYTE * data)
|
---|
534 | {
|
---|
535 | unsigned count, to_send;
|
---|
536 | BYTE data_w[512];
|
---|
537 | int i_w;
|
---|
538 |
|
---|
539 | #ifdef DEBUG_IO
|
---|
540 | unsigned i;
|
---|
541 |
|
---|
542 | printf("IO: Sending: ");
|
---|
543 | fflush(stdout);
|
---|
544 | #endif
|
---|
545 | /* Discard input data from previous commands */
|
---|
546 | // tcflush (io->fd, TCIFLUSH);
|
---|
547 |
|
---|
548 | for (count = 0; count < size; count += to_send) {
|
---|
549 | // if (io->reader_type == RTYP_SCI)
|
---|
550 | // to_send = 1;
|
---|
551 | // else
|
---|
552 | to_send = (delay ? 1 : size);
|
---|
553 |
|
---|
554 | if (IO_Serial_WaitToWrite(io, delay, 1000)) {
|
---|
555 | for (i_w = 0; i_w < to_send; i_w++) {
|
---|
556 | data_w[(1 + reader_serial_need_dummy_char) * i_w] = data[count + i_w];
|
---|
557 | if (reader_serial_need_dummy_char) {
|
---|
558 | data_w[2 * i_w + 1] = 0x00;
|
---|
559 | }
|
---|
560 | }
|
---|
561 | unsigned int u = write(io->fd, data_w, (1 + reader_serial_need_dummy_char) * to_send);
|
---|
562 |
|
---|
563 | _in_echo_read = 1;
|
---|
564 | if (u != (1 + reader_serial_need_dummy_char) * to_send) {
|
---|
565 | #ifdef DEBUG_IO
|
---|
566 | printf("ERROR\n");
|
---|
567 | fflush(stdout);
|
---|
568 | #endif
|
---|
569 | if (io->reader_type != RTYP_SCI)
|
---|
570 | io->wr += u;
|
---|
571 | return FALSE;
|
---|
572 | }
|
---|
573 |
|
---|
574 | if (io->reader_type != RTYP_SCI)
|
---|
575 | io->wr += to_send;
|
---|
576 |
|
---|
577 | #ifdef DEBUG_IO
|
---|
578 | for (i = 0; i < (1 + reader_serial_need_dummy_char) * to_send; i++)
|
---|
579 | printf("%X ", data_w[count + i]);
|
---|
580 | fflush(stdout);
|
---|
581 | #endif
|
---|
582 | } else {
|
---|
583 | #ifdef DEBUG_IO
|
---|
584 | printf("TIMEOUT\n");
|
---|
585 | fflush(stdout);
|
---|
586 | #endif
|
---|
587 | // tcflush (io->fd, TCIFLUSH);
|
---|
588 | return FALSE;
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 | #ifdef DEBUG_IO
|
---|
593 | printf("\n");
|
---|
594 | fflush(stdout);
|
---|
595 | #endif
|
---|
596 |
|
---|
597 | return TRUE;
|
---|
598 | }
|
---|
599 |
|
---|
600 | bool IO_Serial_Close(IO_Serial * io)
|
---|
601 | {
|
---|
602 | #ifdef DEBUG_IO
|
---|
603 | printf("IO: Clossing serial port %s\n", io->device);
|
---|
604 | #endif
|
---|
605 |
|
---|
606 | #if defined(TUXBOX) && defined(PPC)
|
---|
607 | close(fdmc);
|
---|
608 | #endif
|
---|
609 | if (close(io->fd) != 0)
|
---|
610 | return FALSE;
|
---|
611 |
|
---|
612 | IO_Serial_ClearPropertiesCache(io);
|
---|
613 | IO_Serial_Clear(io);
|
---|
614 |
|
---|
615 | return TRUE;
|
---|
616 | }
|
---|
617 |
|
---|
618 | void IO_Serial_Delete(IO_Serial * io)
|
---|
619 | {
|
---|
620 | if (io->props != NULL)
|
---|
621 | free(io->props);
|
---|
622 |
|
---|
623 | free(io);
|
---|
624 | }
|
---|
625 |
|
---|
626 | /*
|
---|
627 | * Internal functions definition
|
---|
628 | */
|
---|
629 |
|
---|
630 | static int IO_Serial_Bitrate_to_Speed(int bitrate)
|
---|
631 | {
|
---|
632 | static const struct BaudRates { int real; speed_t apival; } BaudRateTab[] = {
|
---|
633 | #ifdef B50
|
---|
634 | { 50, B50 },
|
---|
635 | #endif
|
---|
636 | #ifdef B75
|
---|
637 | { 75, B75 },
|
---|
638 | #endif
|
---|
639 | #ifdef B110
|
---|
640 | { 110, B110 },
|
---|
641 | #endif
|
---|
642 | #ifdef B134
|
---|
643 | { 134, B134 },
|
---|
644 | #endif
|
---|
645 | #ifdef B150
|
---|
646 | { 150, B150 },
|
---|
647 | #endif
|
---|
648 | #ifdef B200
|
---|
649 | { 200, B200 },
|
---|
650 | #endif
|
---|
651 | #ifdef B300
|
---|
652 | { 300, B300 },
|
---|
653 | #endif
|
---|
654 | #ifdef B600
|
---|
655 | { 600, B600 },
|
---|
656 | #endif
|
---|
657 | #ifdef B1200
|
---|
658 | { 1200, B1200 },
|
---|
659 | #endif
|
---|
660 | #ifdef B1800
|
---|
661 | { 1800, B1800 },
|
---|
662 | #endif
|
---|
663 | #ifdef B2400
|
---|
664 | { 2400, B2400 },
|
---|
665 | #endif
|
---|
666 | #ifdef B4800
|
---|
667 | { 4800, B4800 },
|
---|
668 | #endif
|
---|
669 | #ifdef B9600
|
---|
670 | { 9600, B9600 },
|
---|
671 | #endif
|
---|
672 | #ifdef B19200
|
---|
673 | { 19200, B19200 },
|
---|
674 | #endif
|
---|
675 | #ifdef B38400
|
---|
676 | { 38400, B38400 },
|
---|
677 | #endif
|
---|
678 | #ifdef B57600
|
---|
679 | { 57600, B57600 },
|
---|
680 | #endif
|
---|
681 | #ifdef B115200
|
---|
682 | { 115200, B115200 },
|
---|
683 | #endif
|
---|
684 | #ifdef B230400
|
---|
685 | { 230400, B230400 },
|
---|
686 | #endif
|
---|
687 | };
|
---|
688 |
|
---|
689 | speed_t result = B0;
|
---|
690 | int i, d_result = bitrate;
|
---|
691 |
|
---|
692 | for ( i = 0 ; i < (int)(sizeof(BaudRateTab)/sizeof(struct BaudRates)) ; i++) {
|
---|
693 | int d = abs(BaudRateTab[i].real - bitrate);
|
---|
694 | if (d < d_result) {
|
---|
695 | result = BaudRateTab[i].apival;
|
---|
696 | d_result = d;
|
---|
697 | } else {
|
---|
698 | break;
|
---|
699 | }
|
---|
700 | }
|
---|
701 |
|
---|
702 | return result;
|
---|
703 | }
|
---|
704 |
|
---|
705 | static int IO_Serial_Bitrate_from_Speed(int speed)
|
---|
706 | {
|
---|
707 | switch (speed) {
|
---|
708 | #ifdef B0
|
---|
709 | case B0:
|
---|
710 | return 0;
|
---|
711 | #endif
|
---|
712 | #ifdef B50
|
---|
713 | case B50:
|
---|
714 | return 50;
|
---|
715 | #endif
|
---|
716 | #ifdef B75
|
---|
717 | case B75:
|
---|
718 | return 75;
|
---|
719 | #endif
|
---|
720 | #ifdef B110
|
---|
721 | case B110:
|
---|
722 | return 110;
|
---|
723 | #endif
|
---|
724 | #ifdef B134
|
---|
725 | case B134:
|
---|
726 | return 134;
|
---|
727 | #endif
|
---|
728 | #ifdef B150
|
---|
729 | case B150:
|
---|
730 | return 150;
|
---|
731 | #endif
|
---|
732 | #ifdef B200
|
---|
733 | case B200:
|
---|
734 | return 200;
|
---|
735 | #endif
|
---|
736 | #ifdef B300
|
---|
737 | case B300:
|
---|
738 | return 300;
|
---|
739 | #endif
|
---|
740 | #ifdef B600
|
---|
741 | case B600:
|
---|
742 | return 600;
|
---|
743 | #endif
|
---|
744 | #ifdef B1200
|
---|
745 | case B1200:
|
---|
746 | return 1200;
|
---|
747 | #endif
|
---|
748 | #ifdef B1800
|
---|
749 | case B1800:
|
---|
750 | return 1800;
|
---|
751 | #endif
|
---|
752 | #ifdef B2400
|
---|
753 | case B2400:
|
---|
754 | return 2400;
|
---|
755 | #endif
|
---|
756 | #ifdef B4800
|
---|
757 | case B4800:
|
---|
758 | return 4800;
|
---|
759 | #endif
|
---|
760 | #ifdef B9600
|
---|
761 | case B9600:
|
---|
762 | return 9600;
|
---|
763 | #endif
|
---|
764 | #ifdef B19200
|
---|
765 | case B19200:
|
---|
766 | return 19200;
|
---|
767 | #endif
|
---|
768 | #ifdef B38400
|
---|
769 | case B38400:
|
---|
770 | return 38400;
|
---|
771 | #endif
|
---|
772 | #ifdef B57600
|
---|
773 | case B57600:
|
---|
774 | return 57600;
|
---|
775 | #endif
|
---|
776 | #ifdef B115200
|
---|
777 | case B115200:
|
---|
778 | return 115200;
|
---|
779 | #endif
|
---|
780 | #ifdef B230400
|
---|
781 | case B230400:
|
---|
782 | return 230400;
|
---|
783 | #endif
|
---|
784 | default:
|
---|
785 | return 1200;
|
---|
786 | }
|
---|
787 |
|
---|
788 | return 0; /* Should never get here */
|
---|
789 | }
|
---|
790 |
|
---|
791 | static bool IO_Serial_WaitToRead(int hnd, unsigned delay_ms, unsigned timeout_ms)
|
---|
792 | {
|
---|
793 | fd_set rfds;
|
---|
794 | fd_set erfds;
|
---|
795 | struct timeval tv;
|
---|
796 | int select_ret;
|
---|
797 | int in_fd;
|
---|
798 |
|
---|
799 | if (delay_ms > 0) {
|
---|
800 | #ifdef HAVE_NANOSLEEP
|
---|
801 | struct timespec req_ts;
|
---|
802 |
|
---|
803 | req_ts.tv_sec = delay_ms / 1000;
|
---|
804 | req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
|
---|
805 | nanosleep(&req_ts, NULL);
|
---|
806 | #else
|
---|
807 | usleep(delay_ms * 1000L);
|
---|
808 | #endif
|
---|
809 | }
|
---|
810 |
|
---|
811 | in_fd = hnd;
|
---|
812 |
|
---|
813 | FD_ZERO(&rfds);
|
---|
814 | FD_SET(in_fd, &rfds);
|
---|
815 |
|
---|
816 | FD_ZERO(&erfds);
|
---|
817 | FD_SET(in_fd, &erfds);
|
---|
818 |
|
---|
819 | tv.tv_sec = timeout_ms / 1000;
|
---|
820 | tv.tv_usec = (timeout_ms % 1000) * 1000L;
|
---|
821 | select_ret = select(in_fd + 1, &rfds, NULL, &erfds, &tv);
|
---|
822 | if (select_ret == -1) {
|
---|
823 | printf("select_ret=%i\n", select_ret);
|
---|
824 | printf("errno =%d\n", errno);
|
---|
825 | fflush(stdout);
|
---|
826 | return (FALSE);
|
---|
827 | }
|
---|
828 |
|
---|
829 | if (FD_ISSET(in_fd, &erfds)) {
|
---|
830 | printf("fd is in error fds\n");
|
---|
831 | printf("errno =%d\n", errno);
|
---|
832 | fflush(stdout);
|
---|
833 | return (FALSE);
|
---|
834 | }
|
---|
835 |
|
---|
836 | return (FD_ISSET(in_fd, &rfds));
|
---|
837 | }
|
---|
838 |
|
---|
839 | static bool IO_Serial_WaitToWrite(IO_Serial * io, unsigned delay_ms, unsigned timeout_ms)
|
---|
840 | {
|
---|
841 | fd_set wfds;
|
---|
842 | fd_set ewfds;
|
---|
843 | struct timeval tv;
|
---|
844 | int select_ret;
|
---|
845 | int out_fd;
|
---|
846 |
|
---|
847 | #ifdef SCI_DEV
|
---|
848 | if (io->reader_type == RTYP_SCI)
|
---|
849 | return TRUE;
|
---|
850 | #endif
|
---|
851 |
|
---|
852 | if (delay_ms > 0) {
|
---|
853 | #ifdef HAVE_NANOSLEEP
|
---|
854 | struct timespec req_ts;
|
---|
855 |
|
---|
856 | req_ts.tv_sec = delay_ms / 1000;
|
---|
857 | req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
|
---|
858 | nanosleep(&req_ts, NULL);
|
---|
859 | #else
|
---|
860 | usleep(delay_ms * 1000L);
|
---|
861 | #endif
|
---|
862 | }
|
---|
863 |
|
---|
864 | out_fd = io->fd;
|
---|
865 |
|
---|
866 | FD_ZERO(&wfds);
|
---|
867 | FD_SET(out_fd, &wfds);
|
---|
868 |
|
---|
869 | FD_ZERO(&ewfds);
|
---|
870 | FD_SET(out_fd, &ewfds);
|
---|
871 |
|
---|
872 | tv.tv_sec = timeout_ms / 1000L;
|
---|
873 | tv.tv_usec = (timeout_ms % 1000) * 1000L;
|
---|
874 |
|
---|
875 | select_ret = select(out_fd + 1, NULL, &wfds, &ewfds, &tv);
|
---|
876 |
|
---|
877 | if (select_ret == -1) {
|
---|
878 | printf("select_ret=%d\n", select_ret);
|
---|
879 | printf("errno =%d\n", errno);
|
---|
880 | fflush(stdout);
|
---|
881 | return (FALSE);
|
---|
882 | }
|
---|
883 |
|
---|
884 | if (FD_ISSET(out_fd, &ewfds)) {
|
---|
885 | printf("fd is in ewfds\n");
|
---|
886 | printf("errno =%d\n", errno);
|
---|
887 | fflush(stdout);
|
---|
888 | return (FALSE);
|
---|
889 | }
|
---|
890 |
|
---|
891 | return (FD_ISSET(out_fd, &wfds));
|
---|
892 |
|
---|
893 | }
|
---|
894 |
|
---|
895 | static void IO_Serial_Clear(IO_Serial * io)
|
---|
896 | {
|
---|
897 | io->fd = -1;
|
---|
898 | io->props = NULL;
|
---|
899 | memset(io->PnP_id, 0, IO_SERIAL_PNPID_SIZE);
|
---|
900 | io->PnP_id_size = 0;
|
---|
901 | io->wr = 0;
|
---|
902 | }
|
---|
903 |
|
---|
904 | static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props)
|
---|
905 | {
|
---|
906 | #ifdef SCI_DEV
|
---|
907 | if (io->reader_type == RTYP_SCI)
|
---|
908 | return;
|
---|
909 | #endif
|
---|
910 |
|
---|
911 | if (io->props == NULL)
|
---|
912 | io->props = (IO_Serial_Properties *) malloc(sizeof (IO_Serial_Properties));
|
---|
913 | #ifdef DEBUG_IO
|
---|
914 | printf("IO: Catching properties\n");
|
---|
915 | #endif
|
---|
916 |
|
---|
917 | memcpy(io->props, props, sizeof (IO_Serial_Properties));
|
---|
918 | }
|
---|
919 |
|
---|
920 | static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props)
|
---|
921 | {
|
---|
922 | if (io->props != NULL) {
|
---|
923 | memcpy(props, io->props, sizeof (IO_Serial_Properties));
|
---|
924 | #ifdef DEBUG_IO
|
---|
925 | printf("IO: Getting properties (catched): %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", props->input_bitrate, props->bits, props->parity == IO_SERIAL_PARITY_EVEN ? "Even" : props->parity == IO_SERIAL_PARITY_ODD ? "Odd" : "None", props->stopbits, props->dtr, props->rts);
|
---|
926 | #endif
|
---|
927 | return TRUE;
|
---|
928 | }
|
---|
929 |
|
---|
930 | return FALSE;
|
---|
931 | }
|
---|
932 |
|
---|
933 | static void IO_Serial_ClearPropertiesCache(IO_Serial * io)
|
---|
934 | {
|
---|
935 | #ifdef DEBUG_IO
|
---|
936 | printf("IO: Clearing properties cache\n");
|
---|
937 | #endif
|
---|
938 | if (io->props != NULL) {
|
---|
939 | free(io->props);
|
---|
940 | io->props = NULL;
|
---|
941 | }
|
---|
942 | }
|
---|
943 |
|
---|
944 | static bool IO_Serial_InitPnP(IO_Serial * io)
|
---|
945 | {
|
---|
946 | IO_Serial_Properties props;
|
---|
947 | int i = 0;
|
---|
948 |
|
---|
949 | props.input_bitrate = io->frequency / 372;
|
---|
950 | props.output_bitrate = io->frequency / 372;
|
---|
951 | props.parity = IO_SERIAL_PARITY_NONE;
|
---|
952 | props.bits = 8;
|
---|
953 | props.stopbits = 1;
|
---|
954 | props.dtr = IO_SERIAL_HIGH;
|
---|
955 | if (io->reader_type == RTYP_PHOENIX) {
|
---|
956 | props.rts = IO_SERIAL_LOW;
|
---|
957 | } else if (io->reader_type == RTYP_SMARTMOUSE) {
|
---|
958 | props.rts = IO_SERIAL_HIGH;
|
---|
959 | } else {
|
---|
960 | props.rts = IO_SERIAL_LOW;
|
---|
961 | }
|
---|
962 |
|
---|
963 | if (!IO_Serial_SetProperties(io, &props))
|
---|
964 | return FALSE;
|
---|
965 |
|
---|
966 | while ((i < IO_SERIAL_PNPID_SIZE) && IO_Serial_Read(io, 200, 1, &(io->PnP_id[i])))
|
---|
967 | i++;
|
---|
968 |
|
---|
969 | io->PnP_id_size = i;
|
---|
970 | return TRUE;
|
---|
971 | }
|
---|