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