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 | #define IO_SERIAL_FILENAME_LENGTH 32
|
---|
54 |
|
---|
55 | /*
|
---|
56 | * Internal functions declaration
|
---|
57 | */
|
---|
58 |
|
---|
59 | static int IO_Serial_Bitrate(int bitrate);
|
---|
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 void IO_Serial_DeviceName (unsigned com, bool usbserial, char * filename, unsigned length);
|
---|
66 |
|
---|
67 | static bool IO_Serial_InitPnP (IO_Serial * io);
|
---|
68 |
|
---|
69 | static void IO_Serial_Clear (IO_Serial * io);
|
---|
70 |
|
---|
71 | static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props);
|
---|
72 |
|
---|
73 | static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props);
|
---|
74 |
|
---|
75 | static void IO_Serial_ClearPropertiesCache (IO_Serial * io);
|
---|
76 |
|
---|
77 | static int _in_echo_read = 0;
|
---|
78 | int io_serial_need_dummy_char = 0;
|
---|
79 |
|
---|
80 | int fdmc=(-1);
|
---|
81 |
|
---|
82 | #if defined(TUXBOX) && defined(PPC)
|
---|
83 | void IO_Serial_Ioctl_Lock(IO_Serial * io, int flag)
|
---|
84 | {
|
---|
85 | extern int *oscam_sem;
|
---|
86 | if ((io->com!=RTYP_DB2COM1) && (io->com!=RTYP_DB2COM2)) return;
|
---|
87 | if (!flag)
|
---|
88 | *oscam_sem=0;
|
---|
89 | else while (*oscam_sem!=io->com)
|
---|
90 | {
|
---|
91 | while (*oscam_sem)
|
---|
92 | usleep((io->com)*2000);
|
---|
93 | *oscam_sem=io->com;
|
---|
94 | usleep(1000);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | static bool IO_Serial_DTR_RTS_dbox2(int mcport, int dtr, int set)
|
---|
99 | {
|
---|
100 | int rc;
|
---|
101 | unsigned short msr;
|
---|
102 | unsigned int mbit;
|
---|
103 | unsigned short rts_bits[2]={ 0x10, 0x800};
|
---|
104 | unsigned short dtr_bits[2]={0x100, 0};
|
---|
105 |
|
---|
106 | #ifdef DEBUG_IO
|
---|
107 | printf("IO: multicam.o %s %s\n", dtr ? "dtr" : "rts", set ? "set" : "clear"); fflush(stdout);
|
---|
108 | #endif
|
---|
109 | if ((rc=ioctl(fdmc, GET_PCDAT, &msr))>=0)
|
---|
110 | {
|
---|
111 | if (dtr) // DTR
|
---|
112 | {
|
---|
113 | if (dtr_bits[mcport])
|
---|
114 | {
|
---|
115 | if (set)
|
---|
116 | msr&=(unsigned short)(~dtr_bits[mcport]);
|
---|
117 | else
|
---|
118 | msr|=dtr_bits[mcport];
|
---|
119 | rc=ioctl(fdmc, SET_PCDAT, &msr);
|
---|
120 | }
|
---|
121 | else
|
---|
122 | rc=0; // Dummy, can't handle using multicam.o
|
---|
123 | }
|
---|
124 | else // RTS
|
---|
125 | {
|
---|
126 | if (set)
|
---|
127 | msr&=(unsigned short)(~rts_bits[mcport]);
|
---|
128 | else
|
---|
129 | msr|=rts_bits[mcport];
|
---|
130 | rc=ioctl(fdmc, SET_PCDAT, &msr);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | return((rc<0) ? FALSE : TRUE);
|
---|
134 | }
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | bool IO_Serial_DTR_RTS(IO_Serial * io, int dtr, int set)
|
---|
138 | {
|
---|
139 | unsigned int msr;
|
---|
140 | unsigned int mbit;
|
---|
141 |
|
---|
142 | #if defined(TUXBOX) && defined(PPC)
|
---|
143 | if ((io->com==RTYP_DB2COM1) || (io->com==RTYP_DB2COM2))
|
---|
144 | return(IO_Serial_DTR_RTS_dbox2(io->com==RTYP_DB2COM2, dtr, set));
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | mbit=(dtr) ? TIOCM_DTR : TIOCM_RTS;
|
---|
148 | #if defined(TIOCMBIS) && defined(TIOBMBIC)
|
---|
149 | if (ioctl (io->fd, set ? TIOCMBIS : TIOCMBIC, &mbit) < 0)
|
---|
150 | return FALSE;
|
---|
151 | #else
|
---|
152 | if (ioctl(io->fd, TIOCMGET, &msr) < 0)
|
---|
153 | return FALSE;
|
---|
154 | if (set)
|
---|
155 | msr|=mbit;
|
---|
156 | else
|
---|
157 | msr&=~mbit;
|
---|
158 | return((ioctl(io->fd, TIOCMSET, &msr)<0) ? FALSE : TRUE);
|
---|
159 | #endif
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*
|
---|
163 | * Public functions definition
|
---|
164 | */
|
---|
165 |
|
---|
166 | IO_Serial * IO_Serial_New (int reader_type)
|
---|
167 | {
|
---|
168 | IO_Serial *io;
|
---|
169 |
|
---|
170 | io = (IO_Serial *) malloc (sizeof (IO_Serial));
|
---|
171 |
|
---|
172 | if (io != NULL)
|
---|
173 | IO_Serial_Clear (io);
|
---|
174 |
|
---|
175 | io->reader_type=reader_type;
|
---|
176 |
|
---|
177 | return io;
|
---|
178 | }
|
---|
179 |
|
---|
180 | bool IO_Serial_Init (IO_Serial * io, unsigned com, bool usbserial, bool pnp)
|
---|
181 | {
|
---|
182 | char filename[IO_SERIAL_FILENAME_LENGTH];
|
---|
183 |
|
---|
184 | IO_Serial_DeviceName (com, usbserial, filename, IO_SERIAL_FILENAME_LENGTH);
|
---|
185 |
|
---|
186 | #ifdef DEBUG_IO
|
---|
187 | printf ("IO: Opening serial port %s\n", filename);
|
---|
188 | #endif
|
---|
189 |
|
---|
190 | if (com < 1)
|
---|
191 | return FALSE;
|
---|
192 |
|
---|
193 | io->com = com;
|
---|
194 |
|
---|
195 | #ifdef SCI_DEV
|
---|
196 | if (com==RTYP_SCI)
|
---|
197 | io->fd = open (filename, O_RDWR);
|
---|
198 | else
|
---|
199 | #endif
|
---|
200 |
|
---|
201 | #ifdef OS_MACOSX
|
---|
202 | io->fd = open (filename, O_RDWR | O_NOCTTY | O_NDELAY );
|
---|
203 | #else
|
---|
204 | io->fd = open (filename, O_RDWR | O_NOCTTY | O_SYNC);
|
---|
205 | #endif
|
---|
206 |
|
---|
207 | if (io->fd < 0)
|
---|
208 | return FALSE;
|
---|
209 |
|
---|
210 | #if defined(TUXBOX) && defined(PPC)
|
---|
211 | if ((com==RTYP_DB2COM1) || (com==RTYP_DB2COM2))
|
---|
212 | if ((fdmc = open(DEV_MULTICAM, O_RDWR)) < 0)
|
---|
213 | {
|
---|
214 | close(io->fd);
|
---|
215 | return FALSE;
|
---|
216 | }
|
---|
217 | #endif
|
---|
218 |
|
---|
219 | if (com!=RTYP_SCI)
|
---|
220 | IO_Serial_InitPnP (io);
|
---|
221 |
|
---|
222 | io->usbserial=usbserial;
|
---|
223 |
|
---|
224 | if(io->com!=RTYP_SCI)
|
---|
225 | IO_Serial_Flush(io);
|
---|
226 |
|
---|
227 | return TRUE;
|
---|
228 | }
|
---|
229 |
|
---|
230 | bool IO_Serial_GetProperties (IO_Serial * io, IO_Serial_Properties * props)
|
---|
231 | {
|
---|
232 | struct termios currtio;
|
---|
233 | speed_t i_speed, o_speed;
|
---|
234 | unsigned int mctl;
|
---|
235 |
|
---|
236 | #ifdef SCI_DEV
|
---|
237 | if(io->com==RTYP_SCI)
|
---|
238 | return FALSE;
|
---|
239 | #endif
|
---|
240 |
|
---|
241 | if (IO_Serial_GetPropertiesCache(io, props))
|
---|
242 | return TRUE;
|
---|
243 |
|
---|
244 | if (tcgetattr (io->fd, &currtio) != 0)
|
---|
245 | return FALSE;
|
---|
246 |
|
---|
247 | o_speed = cfgetospeed (&currtio);
|
---|
248 |
|
---|
249 | switch (o_speed)
|
---|
250 | {
|
---|
251 | #ifdef B0
|
---|
252 | case B0:
|
---|
253 | props->output_bitrate = 0;
|
---|
254 | break;
|
---|
255 | #endif
|
---|
256 | #ifdef B50
|
---|
257 | case B50:
|
---|
258 | props->output_bitrate = 50;
|
---|
259 | break;
|
---|
260 | #endif
|
---|
261 | #ifdef B75
|
---|
262 | case B75:
|
---|
263 | props->output_bitrate = 75;
|
---|
264 | break;
|
---|
265 | #endif
|
---|
266 | #ifdef B110
|
---|
267 | case B110:
|
---|
268 | props->output_bitrate = 110;
|
---|
269 | break;
|
---|
270 | #endif
|
---|
271 | #ifdef B134
|
---|
272 | case B134:
|
---|
273 | props->output_bitrate = 134;
|
---|
274 | break;
|
---|
275 | #endif
|
---|
276 | #ifdef B150
|
---|
277 | case B150:
|
---|
278 | props->output_bitrate = 150;
|
---|
279 | break;
|
---|
280 | #endif
|
---|
281 | #ifdef B200
|
---|
282 | case B200:
|
---|
283 | props->output_bitrate = 200;
|
---|
284 | break;
|
---|
285 | #endif
|
---|
286 | #ifdef B300
|
---|
287 | case B300:
|
---|
288 | props->output_bitrate = 300;
|
---|
289 | break;
|
---|
290 | #endif
|
---|
291 | #ifdef B600
|
---|
292 | case B600:
|
---|
293 | props->output_bitrate = 600;
|
---|
294 | break;
|
---|
295 | #endif
|
---|
296 | #ifdef B1200
|
---|
297 | case B1200:
|
---|
298 | props->output_bitrate = 1200;
|
---|
299 | break;
|
---|
300 | #endif
|
---|
301 | #ifdef B1800
|
---|
302 | case B1800:
|
---|
303 | props->output_bitrate = 1800;
|
---|
304 | break;
|
---|
305 | #endif
|
---|
306 | #ifdef B2400
|
---|
307 | case B2400:
|
---|
308 | props->output_bitrate = 2400;
|
---|
309 | break;
|
---|
310 | #endif
|
---|
311 | #ifdef B4800
|
---|
312 | case B4800:
|
---|
313 | props->output_bitrate = 4800;
|
---|
314 | break;
|
---|
315 | #endif
|
---|
316 | #ifdef B9600
|
---|
317 | case B9600:
|
---|
318 | props->output_bitrate = 9600;
|
---|
319 | break;
|
---|
320 | #endif
|
---|
321 | #ifdef B19200
|
---|
322 | case B19200:
|
---|
323 | props->output_bitrate = 19200;
|
---|
324 | break;
|
---|
325 | #endif
|
---|
326 | #ifdef B38400
|
---|
327 | case B38400:
|
---|
328 | props->output_bitrate = 38400;
|
---|
329 | break;
|
---|
330 | #endif
|
---|
331 | #ifdef B57600
|
---|
332 | case B57600:
|
---|
333 | props->output_bitrate = 57600;
|
---|
334 | break;
|
---|
335 | #endif
|
---|
336 | #ifdef B115200
|
---|
337 | case B115200:
|
---|
338 | props->output_bitrate = 115200;
|
---|
339 | break;
|
---|
340 | #endif
|
---|
341 | #ifdef B230400
|
---|
342 | case B230400:
|
---|
343 | props->output_bitrate = 230400;
|
---|
344 | break;
|
---|
345 | #endif
|
---|
346 | default:
|
---|
347 | props->output_bitrate = 1200;
|
---|
348 | break;
|
---|
349 | }
|
---|
350 |
|
---|
351 | i_speed = cfgetispeed (&currtio);
|
---|
352 |
|
---|
353 | switch (i_speed)
|
---|
354 | {
|
---|
355 | #ifdef B0
|
---|
356 | case B0:
|
---|
357 | props->input_bitrate = 0;
|
---|
358 | break;
|
---|
359 | #endif
|
---|
360 | #ifdef B50
|
---|
361 | case B50:
|
---|
362 | props->input_bitrate = 50;
|
---|
363 | break;
|
---|
364 | #endif
|
---|
365 | #ifdef B75
|
---|
366 | case B75:
|
---|
367 | props->input_bitrate = 75;
|
---|
368 | break;
|
---|
369 | #endif
|
---|
370 | #ifdef B110
|
---|
371 | case B110:
|
---|
372 | props->input_bitrate = 110;
|
---|
373 | break;
|
---|
374 | #endif
|
---|
375 | #ifdef B134
|
---|
376 | case B134:
|
---|
377 | props->input_bitrate = 134;
|
---|
378 | break;
|
---|
379 | #endif
|
---|
380 | #ifdef B150
|
---|
381 | case B150:
|
---|
382 | props->input_bitrate = 150;
|
---|
383 | break;
|
---|
384 | #endif
|
---|
385 | #ifdef B200
|
---|
386 | case B200:
|
---|
387 | props->input_bitrate = 200;
|
---|
388 | break;
|
---|
389 | #endif
|
---|
390 | #ifdef B300
|
---|
391 | case B300:
|
---|
392 | props->input_bitrate = 300;
|
---|
393 | break;
|
---|
394 | #endif
|
---|
395 | #ifdef B600
|
---|
396 | case B600:
|
---|
397 | props->input_bitrate = 600;
|
---|
398 | break;
|
---|
399 | #endif
|
---|
400 | #ifdef B1200
|
---|
401 | case B1200:
|
---|
402 | props->input_bitrate = 1200;
|
---|
403 | break;
|
---|
404 | #endif
|
---|
405 | #ifdef B1800
|
---|
406 | case B1800:
|
---|
407 | props->input_bitrate = 1800;
|
---|
408 | break;
|
---|
409 | #endif
|
---|
410 | #ifdef B2400
|
---|
411 | case B2400:
|
---|
412 | props->input_bitrate = 2400;
|
---|
413 | break;
|
---|
414 | #endif
|
---|
415 | #ifdef B4800
|
---|
416 | case B4800:
|
---|
417 | props->input_bitrate = 4800;
|
---|
418 | break;
|
---|
419 | #endif
|
---|
420 | #ifdef B9600
|
---|
421 | case B9600:
|
---|
422 | props->input_bitrate = 9600;
|
---|
423 | break;
|
---|
424 | #endif
|
---|
425 | #ifdef B19200
|
---|
426 | case B19200:
|
---|
427 | props->input_bitrate = 19200;
|
---|
428 | break;
|
---|
429 | #endif
|
---|
430 | #ifdef B38400
|
---|
431 | case B38400:
|
---|
432 | props->input_bitrate = 38400;
|
---|
433 | break;
|
---|
434 | #endif
|
---|
435 | #ifdef B57600
|
---|
436 | case B57600:
|
---|
437 | props->input_bitrate = 57600;
|
---|
438 | break;
|
---|
439 | #endif
|
---|
440 | #ifdef B115200
|
---|
441 | case B115200:
|
---|
442 | props->input_bitrate = 115200;
|
---|
443 | break;
|
---|
444 | #endif
|
---|
445 | #ifdef B230400
|
---|
446 | case B230400:
|
---|
447 | props->input_bitrate = 230400;
|
---|
448 | break;
|
---|
449 | #endif
|
---|
450 | default:
|
---|
451 | props->input_bitrate = 1200;
|
---|
452 | break;
|
---|
453 | }
|
---|
454 |
|
---|
455 | switch (currtio.c_cflag & CSIZE)
|
---|
456 | {
|
---|
457 | case CS5:
|
---|
458 | props->bits = 5;
|
---|
459 | break;
|
---|
460 | case CS6:
|
---|
461 | props->bits = 6;
|
---|
462 | break;
|
---|
463 | case CS7:
|
---|
464 | props->bits = 7;
|
---|
465 | break;
|
---|
466 | case CS8:
|
---|
467 | props->bits = 8;
|
---|
468 | break;
|
---|
469 | }
|
---|
470 |
|
---|
471 | if (((currtio.c_cflag) & PARENB) == PARENB)
|
---|
472 | {
|
---|
473 | if (((currtio.c_cflag) & PARODD) == PARODD)
|
---|
474 | props->parity = IO_SERIAL_PARITY_ODD;
|
---|
475 | else
|
---|
476 | props->parity = IO_SERIAL_PARITY_EVEN;
|
---|
477 | }
|
---|
478 | else
|
---|
479 | {
|
---|
480 | props->parity = IO_SERIAL_PARITY_NONE;
|
---|
481 | }
|
---|
482 |
|
---|
483 | if (((currtio.c_cflag) & CSTOPB) == CSTOPB)
|
---|
484 | props->stopbits = 2;
|
---|
485 | else
|
---|
486 | props->stopbits = 1;
|
---|
487 |
|
---|
488 | if (ioctl (io->fd, TIOCMGET, &mctl) < 0)
|
---|
489 | return FALSE;
|
---|
490 |
|
---|
491 | props->dtr = ((mctl & TIOCM_DTR) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
|
---|
492 | props->rts = ((mctl & TIOCM_RTS) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
|
---|
493 |
|
---|
494 | IO_Serial_SetPropertiesCache (io, props);
|
---|
495 |
|
---|
496 | #ifdef DEBUG_IO
|
---|
497 | 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);
|
---|
498 | #endif
|
---|
499 |
|
---|
500 | return TRUE;
|
---|
501 | }
|
---|
502 |
|
---|
503 | bool IO_Serial_SetProperties (IO_Serial * io, IO_Serial_Properties * props)
|
---|
504 | {
|
---|
505 | struct termios newtio;
|
---|
506 | unsigned int modembits;
|
---|
507 |
|
---|
508 | #ifdef SCI_DEV
|
---|
509 | if(io->com==RTYP_SCI)
|
---|
510 | return FALSE;
|
---|
511 | #endif
|
---|
512 |
|
---|
513 | // printf("IO: Setting properties: com%d, %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", io->com, 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);
|
---|
514 | memset (&newtio, 0, sizeof (newtio));
|
---|
515 | /* Set the bitrate */
|
---|
516 |
|
---|
517 | extern int mhz;
|
---|
518 | extern int reader_irdeto_mode;
|
---|
519 |
|
---|
520 | if(io->reader_type==RTYP_SMART)
|
---|
521 | {
|
---|
522 | #ifdef DEBUG_IO
|
---|
523 | printf("IO: SMARTREADER .. switching to frequency to %2.2fMHz\n", (float)mhz/100.0);
|
---|
524 | #endif
|
---|
525 | if(!IO_Serial_Set_Smartreader_Freq(io,mhz,reader_irdeto_mode))
|
---|
526 | {
|
---|
527 | #ifdef DEBUG_IO
|
---|
528 | printf("IO: SMARTREADER .. ERROR switching to 6MHz\n");
|
---|
529 | #endif
|
---|
530 | return FALSE;
|
---|
531 | }
|
---|
532 | }
|
---|
533 |
|
---|
534 | if (mhz == 600)
|
---|
535 | {
|
---|
536 |
|
---|
537 | /* for 6MHz */
|
---|
538 | if (reader_irdeto_mode)
|
---|
539 | {
|
---|
540 | cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate));
|
---|
541 | cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate));
|
---|
542 | }
|
---|
543 | else
|
---|
544 | {
|
---|
545 | #ifdef OS_LINUX
|
---|
546 | /* these structures are only available on linux as fas as we know so limit this code to OS_LINUX */
|
---|
547 | struct serial_struct nuts;
|
---|
548 | ioctl(io->fd, TIOCGSERIAL, &nuts);
|
---|
549 | nuts.custom_divisor = nuts.baud_base / 9600 * 3.57 / 6;
|
---|
550 | nuts.flags &= ~ASYNC_SPD_MASK;
|
---|
551 | nuts.flags |= ASYNC_SPD_CUST;
|
---|
552 | ioctl(io->fd, TIOCSSERIAL, &nuts);
|
---|
553 | cfsetospeed(&newtio, IO_Serial_Bitrate(38400));
|
---|
554 | cfsetispeed(&newtio, IO_Serial_Bitrate(38400));
|
---|
555 | #else
|
---|
556 | cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate));
|
---|
557 | cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate));
|
---|
558 | #endif
|
---|
559 | }
|
---|
560 | }
|
---|
561 | else if (mhz == 357 || mhz == 358)
|
---|
562 | {
|
---|
563 | /* for 3.57 MHz */
|
---|
564 | if (reader_irdeto_mode)
|
---|
565 | {
|
---|
566 | #ifdef OS_LINUX
|
---|
567 | /* these structures are only available on linux as fas as we know so limit this code to OS_LINUX */
|
---|
568 | struct serial_struct nuts;
|
---|
569 | ioctl(io->fd, TIOCGSERIAL, &nuts);
|
---|
570 | nuts.custom_divisor = nuts.baud_base / 5713;
|
---|
571 | nuts.flags &= ~ASYNC_SPD_MASK;
|
---|
572 | nuts.flags |= ASYNC_SPD_CUST;
|
---|
573 | ioctl(io->fd, TIOCSSERIAL, &nuts);
|
---|
574 | cfsetospeed(&newtio, IO_Serial_Bitrate(38400));
|
---|
575 | cfsetispeed(&newtio, IO_Serial_Bitrate(38400));
|
---|
576 | #else
|
---|
577 | cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate));
|
---|
578 | cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate));
|
---|
579 | #endif
|
---|
580 | }
|
---|
581 | else
|
---|
582 | {
|
---|
583 | cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate));
|
---|
584 | cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate));
|
---|
585 | }
|
---|
586 | }
|
---|
587 | else if (mhz == 1000)
|
---|
588 | {
|
---|
589 |
|
---|
590 | /* for 10MHz */
|
---|
591 | if (reader_irdeto_mode)
|
---|
592 | {
|
---|
593 | cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate));
|
---|
594 | cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate));
|
---|
595 | }
|
---|
596 | else
|
---|
597 | {
|
---|
598 | #ifdef OS_LINUX
|
---|
599 | /* these structures are only available on linux as fas as we know so limit this code to OS_LINUX */
|
---|
600 | struct serial_struct nuts;
|
---|
601 | ioctl(io->fd, TIOCGSERIAL, &nuts);
|
---|
602 | nuts.custom_divisor = nuts.baud_base / 9600 * 6 / 10;
|
---|
603 | nuts.flags &= ~ASYNC_SPD_MASK;
|
---|
604 | nuts.flags |= ASYNC_SPD_CUST;
|
---|
605 | ioctl(io->fd, TIOCSSERIAL, &nuts);
|
---|
606 | cfsetospeed(&newtio, IO_Serial_Bitrate(38400));
|
---|
607 | cfsetispeed(&newtio, IO_Serial_Bitrate(38400));
|
---|
608 | #else
|
---|
609 | cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate));
|
---|
610 | cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate));
|
---|
611 | #endif
|
---|
612 | }
|
---|
613 | }
|
---|
614 | else
|
---|
615 | {
|
---|
616 | /* invalid */
|
---|
617 | return FALSE;
|
---|
618 | }
|
---|
619 |
|
---|
620 | /* Set the character size */
|
---|
621 | switch (props->bits)
|
---|
622 | {
|
---|
623 | case 5:
|
---|
624 | newtio.c_cflag |= CS5;
|
---|
625 | break;
|
---|
626 |
|
---|
627 | case 6:
|
---|
628 | newtio.c_cflag |= CS6;
|
---|
629 | break;
|
---|
630 |
|
---|
631 | case 7:
|
---|
632 | newtio.c_cflag |= CS7;
|
---|
633 | break;
|
---|
634 |
|
---|
635 | case 8:
|
---|
636 | newtio.c_cflag |= CS8;
|
---|
637 | break;
|
---|
638 | }
|
---|
639 |
|
---|
640 | /* Set the parity */
|
---|
641 | switch (props->parity)
|
---|
642 | {
|
---|
643 | case IO_SERIAL_PARITY_ODD:
|
---|
644 | newtio.c_cflag |= PARENB;
|
---|
645 | newtio.c_cflag |= PARODD;
|
---|
646 | break;
|
---|
647 |
|
---|
648 | case IO_SERIAL_PARITY_EVEN:
|
---|
649 | newtio.c_cflag |= PARENB;
|
---|
650 | newtio.c_cflag &= ~PARODD;
|
---|
651 | break;
|
---|
652 |
|
---|
653 | case IO_SERIAL_PARITY_NONE:
|
---|
654 | newtio.c_cflag &= ~PARENB;
|
---|
655 | break;
|
---|
656 | }
|
---|
657 |
|
---|
658 | /* Set the number of stop bits */
|
---|
659 | switch (props->stopbits)
|
---|
660 | {
|
---|
661 | case 1:
|
---|
662 | newtio.c_cflag &= (~CSTOPB);
|
---|
663 | break;
|
---|
664 | case 2:
|
---|
665 | newtio.c_cflag |= CSTOPB;
|
---|
666 | break;
|
---|
667 | }
|
---|
668 |
|
---|
669 | /* Selects raw (non-canonical) input and output */
|
---|
670 | newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
---|
671 | newtio.c_oflag &= ~OPOST;
|
---|
672 | #if 1
|
---|
673 | newtio.c_iflag |= IGNPAR;
|
---|
674 | /* Ignore parity errors!!! Windows driver does so why shouldn't I? */
|
---|
675 | #endif
|
---|
676 | /* Enable receiber, hang on close, ignore control line */
|
---|
677 | newtio.c_cflag |= CREAD | HUPCL | CLOCAL;
|
---|
678 |
|
---|
679 | /* Read 1 byte minimun, no timeout specified */
|
---|
680 | newtio.c_cc[VMIN] = 1;
|
---|
681 | newtio.c_cc[VTIME] = 0;
|
---|
682 |
|
---|
683 | // tcdrain(io->fd);
|
---|
684 | if (tcsetattr (io->fd, TCSANOW, &newtio) < 0)
|
---|
685 | return FALSE;
|
---|
686 | // tcflush(io->fd, TCIOFLUSH);
|
---|
687 | // if (tcsetattr (io->fd, TCSAFLUSH, &newtio) < 0)
|
---|
688 | // return FALSE;
|
---|
689 |
|
---|
690 | IO_Serial_Ioctl_Lock(io, 1);
|
---|
691 | IO_Serial_DTR_RTS(io, 0, props->rts == IO_SERIAL_HIGH);
|
---|
692 | IO_Serial_DTR_RTS(io, 1, props->dtr == IO_SERIAL_HIGH);
|
---|
693 | IO_Serial_Ioctl_Lock(io, 0);
|
---|
694 |
|
---|
695 | IO_Serial_SetPropertiesCache (io, props);
|
---|
696 |
|
---|
697 | #ifdef DEBUG_IO
|
---|
698 | printf("IO: Setting properties: com%d, %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", io->com, 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);
|
---|
699 | #endif
|
---|
700 | return TRUE;
|
---|
701 | }
|
---|
702 |
|
---|
703 | void IO_Serial_Flush (IO_Serial * io)
|
---|
704 | {
|
---|
705 | BYTE b;
|
---|
706 | while(IO_Serial_Read(io, 1000, 1, &b));
|
---|
707 | }
|
---|
708 |
|
---|
709 |
|
---|
710 | void IO_Serial_GetPnPId (IO_Serial * io, BYTE * pnp_id, unsigned *length)
|
---|
711 | {
|
---|
712 | (*length) = io->PnP_id_size;
|
---|
713 | memcpy (pnp_id, io->PnP_id, io->PnP_id_size);
|
---|
714 | }
|
---|
715 |
|
---|
716 | unsigned IO_Serial_GetCom (IO_Serial * io)
|
---|
717 | {
|
---|
718 | return io->com;
|
---|
719 | }
|
---|
720 |
|
---|
721 |
|
---|
722 | bool IO_Serial_Read (IO_Serial * io, unsigned timeout, unsigned size, BYTE * data)
|
---|
723 | {
|
---|
724 | BYTE c;
|
---|
725 | int count = 0;
|
---|
726 |
|
---|
727 |
|
---|
728 | if((io->com!=RTYP_SCI) && (io->wr>0))
|
---|
729 | {
|
---|
730 | BYTE buf[256];
|
---|
731 | int n = io->wr;
|
---|
732 | io->wr = 0;
|
---|
733 |
|
---|
734 | if(!IO_Serial_Read (io, timeout, n, buf))
|
---|
735 | {
|
---|
736 | return FALSE;
|
---|
737 | }
|
---|
738 | }
|
---|
739 |
|
---|
740 | #ifdef DEBUG_IO
|
---|
741 | printf ("IO: Receiving: ");
|
---|
742 | fflush (stdout);
|
---|
743 | #endif
|
---|
744 | for (count = 0; count < size * (_in_echo_read ? (1+io_serial_need_dummy_char) : 1); count++)
|
---|
745 | {
|
---|
746 | if (IO_Serial_WaitToRead (io->fd, 0, timeout))
|
---|
747 | {
|
---|
748 | if (read (io->fd, &c, 1) != 1)
|
---|
749 | {
|
---|
750 | #ifdef DEBUG_IO
|
---|
751 | printf ("ERROR\n");
|
---|
752 | fflush (stdout);
|
---|
753 | #endif
|
---|
754 | return FALSE;
|
---|
755 | }
|
---|
756 | data[_in_echo_read ? count/(1+io_serial_need_dummy_char) : count] = c;
|
---|
757 |
|
---|
758 | #ifdef DEBUG_IO
|
---|
759 | printf ("%X ", c);
|
---|
760 | fflush (stdout);
|
---|
761 | #endif
|
---|
762 | }
|
---|
763 | else
|
---|
764 | {
|
---|
765 | #ifdef DEBUG_IO
|
---|
766 | printf ("TIMEOUT\n");
|
---|
767 | fflush (stdout);
|
---|
768 | #endif
|
---|
769 | tcflush (io->fd, TCIFLUSH);
|
---|
770 | return FALSE;
|
---|
771 | }
|
---|
772 | }
|
---|
773 |
|
---|
774 | _in_echo_read = 0;
|
---|
775 |
|
---|
776 | #ifdef DEBUG_IO
|
---|
777 | printf ("\n");
|
---|
778 | fflush (stdout);
|
---|
779 | #endif
|
---|
780 |
|
---|
781 | return TRUE;
|
---|
782 | }
|
---|
783 |
|
---|
784 |
|
---|
785 |
|
---|
786 |
|
---|
787 | bool IO_Serial_Write (IO_Serial * io, unsigned delay, unsigned size, BYTE * data)
|
---|
788 | {
|
---|
789 | unsigned count, to_send;
|
---|
790 | BYTE data_w[512];
|
---|
791 | int i_w;
|
---|
792 | #ifdef DEBUG_IO
|
---|
793 | unsigned i;
|
---|
794 |
|
---|
795 | printf ("IO: Sending: ");
|
---|
796 | fflush (stdout);
|
---|
797 | #endif
|
---|
798 | /* Discard input data from previous commands */
|
---|
799 | // tcflush (io->fd, TCIFLUSH);
|
---|
800 |
|
---|
801 | for (count = 0; count < size; count += to_send)
|
---|
802 | {
|
---|
803 | // if(io->com==RTYP_SCI)
|
---|
804 | // to_send = 1;
|
---|
805 | // else
|
---|
806 | to_send = (delay? 1: size);
|
---|
807 |
|
---|
808 | if (IO_Serial_WaitToWrite (io, delay, 1000))
|
---|
809 | {
|
---|
810 | for (i_w=0; i_w < to_send; i_w++) {
|
---|
811 | data_w [(1+io_serial_need_dummy_char)*i_w] = data [count + i_w];
|
---|
812 | if (io_serial_need_dummy_char) {
|
---|
813 | data_w [2*i_w+1] = 0x00;
|
---|
814 | }
|
---|
815 | }
|
---|
816 | unsigned int u = write (io->fd, data_w, (1+io_serial_need_dummy_char)*to_send);
|
---|
817 | _in_echo_read = 1;
|
---|
818 | if (u != (1+io_serial_need_dummy_char)*to_send)
|
---|
819 | {
|
---|
820 | #ifdef DEBUG_IO
|
---|
821 | printf ("ERROR\n");
|
---|
822 | fflush (stdout);
|
---|
823 | #endif
|
---|
824 | if(io->com!=RTYP_SCI)
|
---|
825 | io->wr += u;
|
---|
826 | return FALSE;
|
---|
827 | }
|
---|
828 |
|
---|
829 | if(io->com!=RTYP_SCI)
|
---|
830 | io->wr += to_send;
|
---|
831 |
|
---|
832 | #ifdef DEBUG_IO
|
---|
833 | for (i=0; i<(1+io_serial_need_dummy_char)*to_send; i++)
|
---|
834 | printf ("%X ", data_w[count + i]);
|
---|
835 | fflush (stdout);
|
---|
836 | #endif
|
---|
837 | }
|
---|
838 | else
|
---|
839 | {
|
---|
840 | #ifdef DEBUG_IO
|
---|
841 | printf ("TIMEOUT\n");
|
---|
842 | fflush (stdout);
|
---|
843 | #endif
|
---|
844 | // tcflush (io->fd, TCIFLUSH);
|
---|
845 | return FALSE;
|
---|
846 | }
|
---|
847 | }
|
---|
848 |
|
---|
849 | #ifdef DEBUG_IO
|
---|
850 | printf ("\n");
|
---|
851 | fflush (stdout);
|
---|
852 | #endif
|
---|
853 |
|
---|
854 | return TRUE;
|
---|
855 | }
|
---|
856 |
|
---|
857 | bool IO_Serial_Close (IO_Serial * io)
|
---|
858 | {
|
---|
859 | char filename[IO_SERIAL_FILENAME_LENGTH];
|
---|
860 |
|
---|
861 | IO_Serial_DeviceName (io->com, io->usbserial, filename, IO_SERIAL_FILENAME_LENGTH);
|
---|
862 |
|
---|
863 | #ifdef DEBUG_IO
|
---|
864 | printf ("IO: Clossing serial port %s\n", filename);
|
---|
865 | #endif
|
---|
866 |
|
---|
867 | #if defined(TUXBOX) && defined(PPC)
|
---|
868 | close(fdmc);
|
---|
869 | #endif
|
---|
870 | if (close (io->fd) != 0)
|
---|
871 | return FALSE;
|
---|
872 |
|
---|
873 | IO_Serial_ClearPropertiesCache (io);
|
---|
874 | IO_Serial_Clear (io);
|
---|
875 |
|
---|
876 | return TRUE;
|
---|
877 | }
|
---|
878 |
|
---|
879 | void IO_Serial_Delete (IO_Serial * io)
|
---|
880 | {
|
---|
881 | if (io->props != NULL)
|
---|
882 | free (io->props);
|
---|
883 |
|
---|
884 | free (io);
|
---|
885 | }
|
---|
886 |
|
---|
887 | /*
|
---|
888 | * Internal functions definition
|
---|
889 | */
|
---|
890 |
|
---|
891 | static int IO_Serial_Bitrate(int bitrate)
|
---|
892 | {
|
---|
893 | #ifdef B230400
|
---|
894 | if ((bitrate)>=230400) return B230400;
|
---|
895 | #endif
|
---|
896 | #ifdef B115200
|
---|
897 | if ((bitrate)>=115200) return B115200;
|
---|
898 | #endif
|
---|
899 | #ifdef B57600
|
---|
900 | if ((bitrate)>=57600) return B57600;
|
---|
901 | #endif
|
---|
902 | #ifdef B38400
|
---|
903 | if ((bitrate)>=38400) return B38400;
|
---|
904 | #endif
|
---|
905 | #ifdef B19200
|
---|
906 | if ((bitrate)>=19200) return B19200;
|
---|
907 | #endif
|
---|
908 | #ifdef B9600
|
---|
909 | if ((bitrate)>=9600) return B9600;
|
---|
910 | #endif
|
---|
911 | #ifdef B4800
|
---|
912 | if ((bitrate)>=4800) return B4800;
|
---|
913 | #endif
|
---|
914 | #ifdef B2400
|
---|
915 | if ((bitrate)>=2400) return B2400;
|
---|
916 | #endif
|
---|
917 | #ifdef B1800
|
---|
918 | if ((bitrate)>=1800) return B1800;
|
---|
919 | #endif
|
---|
920 | #ifdef B1200
|
---|
921 | if ((bitrate)>=1200) return B1200;
|
---|
922 | #endif
|
---|
923 | #ifdef B600
|
---|
924 | if ((bitrate)>=600) return B600;
|
---|
925 | #endif
|
---|
926 | #ifdef B300
|
---|
927 | if ((bitrate)>=300) return B300;
|
---|
928 | #endif
|
---|
929 | #ifdef B200
|
---|
930 | if ((bitrate)>=200) return B200;
|
---|
931 | #endif
|
---|
932 | #ifdef B150
|
---|
933 | if ((bitrate)>=150) return B150;
|
---|
934 | #endif
|
---|
935 | #ifdef B134
|
---|
936 | if ((bitrate)>=134) return B134;
|
---|
937 | #endif
|
---|
938 | #ifdef B110
|
---|
939 | if ((bitrate)>=110) return B110;
|
---|
940 | #endif
|
---|
941 | #ifdef B75
|
---|
942 | if ((bitrate)>=75) return B75;
|
---|
943 | #endif
|
---|
944 | #ifdef B50
|
---|
945 | if ((bitrate)>=50) return B50;
|
---|
946 | #endif
|
---|
947 | #ifdef B0
|
---|
948 | if ((bitrate)>=0) return B0;
|
---|
949 | #endif
|
---|
950 | return 0; /* Should never get here */
|
---|
951 | }
|
---|
952 |
|
---|
953 | static bool IO_Serial_WaitToRead (int hnd, unsigned delay_ms, unsigned timeout_ms)
|
---|
954 | {
|
---|
955 | fd_set rfds;
|
---|
956 | fd_set erfds;
|
---|
957 | struct timeval tv;
|
---|
958 | int select_ret;
|
---|
959 | int in_fd;
|
---|
960 |
|
---|
961 | if (delay_ms > 0)
|
---|
962 | {
|
---|
963 | #ifdef HAVE_NANOSLEEP
|
---|
964 | struct timespec req_ts;
|
---|
965 |
|
---|
966 | req_ts.tv_sec = delay_ms / 1000;
|
---|
967 | req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
|
---|
968 | nanosleep (&req_ts, NULL);
|
---|
969 | #else
|
---|
970 | usleep (delay_ms * 1000L);
|
---|
971 | #endif
|
---|
972 | }
|
---|
973 |
|
---|
974 | in_fd=hnd;
|
---|
975 |
|
---|
976 | FD_ZERO(&rfds);
|
---|
977 | FD_SET(in_fd, &rfds);
|
---|
978 |
|
---|
979 | FD_ZERO(&erfds);
|
---|
980 | FD_SET(in_fd, &erfds);
|
---|
981 |
|
---|
982 | tv.tv_sec = timeout_ms/1000;
|
---|
983 | tv.tv_usec = (timeout_ms % 1000) * 1000L;
|
---|
984 | select_ret = select(in_fd+1, &rfds, NULL, &erfds, &tv);
|
---|
985 | if(select_ret==-1)
|
---|
986 | {
|
---|
987 | printf("select_ret=%i\n" , select_ret);
|
---|
988 | printf("errno =%d\n", errno);
|
---|
989 | fflush(stdout);
|
---|
990 | return (FALSE);
|
---|
991 | }
|
---|
992 |
|
---|
993 | if (FD_ISSET(in_fd, &erfds))
|
---|
994 | {
|
---|
995 | printf("fd is in error fds\n");
|
---|
996 | printf("errno =%d\n", errno);
|
---|
997 | fflush(stdout);
|
---|
998 | return (FALSE);
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | return(FD_ISSET(in_fd,&rfds));
|
---|
1002 | }
|
---|
1003 |
|
---|
1004 | static bool IO_Serial_WaitToWrite (IO_Serial *io, unsigned delay_ms, unsigned timeout_ms)
|
---|
1005 | {
|
---|
1006 | fd_set wfds;
|
---|
1007 | fd_set ewfds;
|
---|
1008 | struct timeval tv;
|
---|
1009 | int select_ret;
|
---|
1010 | int out_fd;
|
---|
1011 |
|
---|
1012 | #ifdef SCI_DEV
|
---|
1013 | if(io->com==RTYP_SCI)
|
---|
1014 | return TRUE;
|
---|
1015 | #endif
|
---|
1016 |
|
---|
1017 | if (delay_ms > 0)
|
---|
1018 | {
|
---|
1019 | #ifdef HAVE_NANOSLEEP
|
---|
1020 | struct timespec req_ts;
|
---|
1021 |
|
---|
1022 | req_ts.tv_sec = delay_ms / 1000;
|
---|
1023 | req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
|
---|
1024 | nanosleep (&req_ts, NULL);
|
---|
1025 | #else
|
---|
1026 | usleep (delay_ms * 1000L);
|
---|
1027 | #endif
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | out_fd=io->fd;
|
---|
1031 |
|
---|
1032 | FD_ZERO(&wfds);
|
---|
1033 | FD_SET(out_fd, &wfds);
|
---|
1034 |
|
---|
1035 | FD_ZERO(&ewfds);
|
---|
1036 | FD_SET(out_fd, &ewfds);
|
---|
1037 |
|
---|
1038 | tv.tv_sec = timeout_ms/1000L;
|
---|
1039 | tv.tv_usec = (timeout_ms % 1000) * 1000L;
|
---|
1040 |
|
---|
1041 | select_ret = select(out_fd+1, NULL, &wfds, &ewfds, &tv);
|
---|
1042 |
|
---|
1043 | if(select_ret==-1)
|
---|
1044 | {
|
---|
1045 | printf("select_ret=%d\n" , select_ret);
|
---|
1046 | printf("errno =%d\n", errno);
|
---|
1047 | fflush(stdout);
|
---|
1048 | return (FALSE);
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | if (FD_ISSET(out_fd, &ewfds))
|
---|
1052 | {
|
---|
1053 | printf("fd is in ewfds\n");
|
---|
1054 | printf("errno =%d\n", errno);
|
---|
1055 | fflush(stdout);
|
---|
1056 | return (FALSE);
|
---|
1057 | }
|
---|
1058 |
|
---|
1059 | return(FD_ISSET(out_fd,&wfds));
|
---|
1060 |
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | static void IO_Serial_Clear (IO_Serial * io)
|
---|
1064 | {
|
---|
1065 | io->fd = -1;
|
---|
1066 | io->props = NULL;
|
---|
1067 | io->com = 0;
|
---|
1068 | memset (io->PnP_id, 0, IO_SERIAL_PNPID_SIZE);
|
---|
1069 | io->PnP_id_size = 0;
|
---|
1070 | io->usbserial = FALSE;
|
---|
1071 | io->wr = 0;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props)
|
---|
1075 | {
|
---|
1076 | #ifdef SCI_DEV
|
---|
1077 | if(io->com==RTYP_SCI)
|
---|
1078 | return;
|
---|
1079 | #endif
|
---|
1080 |
|
---|
1081 | if (io->props == NULL)
|
---|
1082 | io->props = (IO_Serial_Properties *) malloc (sizeof (IO_Serial_Properties));
|
---|
1083 | #ifdef DEBUG_IO
|
---|
1084 | printf ("IO: Catching properties\n");
|
---|
1085 | #endif
|
---|
1086 |
|
---|
1087 | memcpy (io->props, props, sizeof (IO_Serial_Properties));
|
---|
1088 | }
|
---|
1089 |
|
---|
1090 | static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props)
|
---|
1091 | {
|
---|
1092 | if (io->props != NULL)
|
---|
1093 | {
|
---|
1094 | memcpy (props, io->props, sizeof (IO_Serial_Properties));
|
---|
1095 | #if 0
|
---|
1096 | #ifdef DEBUG_IO
|
---|
1097 | 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);
|
---|
1098 | #endif
|
---|
1099 | #endif
|
---|
1100 | return TRUE;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | return FALSE;
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | static void IO_Serial_ClearPropertiesCache (IO_Serial * io)
|
---|
1107 | {
|
---|
1108 | #ifdef DEBUG_IO
|
---|
1109 | printf ("IO: Clearing properties cache\n");
|
---|
1110 | #endif
|
---|
1111 | if (io->props != NULL)
|
---|
1112 | {
|
---|
1113 | free (io->props);
|
---|
1114 | io->props = NULL;
|
---|
1115 | }
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | static void IO_Serial_DeviceName (unsigned com, bool usbserial, char * filename, unsigned length)
|
---|
1119 | {
|
---|
1120 | extern char oscam_device[];
|
---|
1121 | snprintf (filename, length, "%s", oscam_device);
|
---|
1122 | // if(com==1)
|
---|
1123 | // snprintf (filename, length, "/dev/tts/%d", com - 1);
|
---|
1124 | // else
|
---|
1125 | // snprintf (filename, length, "/dev/sci%d", com - 2);
|
---|
1126 | }
|
---|
1127 |
|
---|
1128 | static bool IO_Serial_InitPnP (IO_Serial * io)
|
---|
1129 | {
|
---|
1130 | IO_Serial_Properties props;
|
---|
1131 | int i = 0;
|
---|
1132 | props.input_bitrate = 1200;
|
---|
1133 | props.output_bitrate = 1200;
|
---|
1134 | props.parity = IO_SERIAL_PARITY_NONE;
|
---|
1135 | props.bits = 7;
|
---|
1136 | props.stopbits = 1;
|
---|
1137 | props.dtr = IO_SERIAL_HIGH;
|
---|
1138 | // props.rts = IO_SERIAL_HIGH;
|
---|
1139 | props.rts = IO_SERIAL_LOW;
|
---|
1140 |
|
---|
1141 | if (!IO_Serial_SetProperties (io, &props))
|
---|
1142 | return FALSE;
|
---|
1143 |
|
---|
1144 | while ((i < IO_SERIAL_PNPID_SIZE) && IO_Serial_Read (io, 200, 1, &(io->PnP_id[i])))
|
---|
1145 | i++;
|
---|
1146 |
|
---|
1147 | io->PnP_id_size = i;
|
---|
1148 | return TRUE;
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 |
|
---|
1152 | bool IO_Serial_Set_Smartreader_Freq(IO_Serial * io, int freq, int irdeto_mode)
|
---|
1153 | {
|
---|
1154 | struct termios term;
|
---|
1155 | struct termios orig;
|
---|
1156 | unsigned int u;
|
---|
1157 | unsigned char fi_di[4]={0x01, 0x01, 0x74, 0x01};
|
---|
1158 | unsigned char fr[3]={0x02, 0x00, 0x00};
|
---|
1159 | unsigned char nn[2]={0x03, 0x00};
|
---|
1160 | unsigned char pr[2]={0x04, 0x00};
|
---|
1161 | unsigned char in[2]={0x05, 0x00};
|
---|
1162 |
|
---|
1163 | orig=term;
|
---|
1164 |
|
---|
1165 | /* set smartreader in CMD mode */
|
---|
1166 | tcgetattr(io->fd, &term);
|
---|
1167 |
|
---|
1168 | term.c_cflag &= ~CSIZE;
|
---|
1169 | term.c_cflag |= CS5;
|
---|
1170 | cfsetospeed(&term, 9600);
|
---|
1171 | cfsetispeed(&term, 9600);
|
---|
1172 | tcsetattr(io->fd, TCSANOW, &term);
|
---|
1173 |
|
---|
1174 | // our freq comes in as 358, 357 or 600 so it needs this to be in KHz for the FR command
|
---|
1175 | freq*=10;
|
---|
1176 | fr[1]=(unsigned char)((freq & 0xff00)>>8);
|
---|
1177 | fr[2]=(unsigned char)(freq & 0x00ff);
|
---|
1178 |
|
---|
1179 | // Irdeto card supposedly need NN set to 1 .. to be confirmed
|
---|
1180 | if(irdeto_mode)
|
---|
1181 | nn[1]=0x01;
|
---|
1182 |
|
---|
1183 | // send the commands
|
---|
1184 | IO_Serial_Write (io, 0, 4, fi_di);
|
---|
1185 | IO_Serial_Flush(io);
|
---|
1186 |
|
---|
1187 | IO_Serial_Write (io, 0, 3, fr);
|
---|
1188 | IO_Serial_Flush(io);
|
---|
1189 |
|
---|
1190 | IO_Serial_Write (io, 0, 2, nn);
|
---|
1191 | IO_Serial_Flush(io);
|
---|
1192 |
|
---|
1193 | IO_Serial_Write (io, 0, 2, pr);
|
---|
1194 | IO_Serial_Flush(io);
|
---|
1195 |
|
---|
1196 | IO_Serial_Write (io, 0, 2, in);
|
---|
1197 | IO_Serial_Flush(io);
|
---|
1198 |
|
---|
1199 | /* set smartreader in DATA mode */
|
---|
1200 | tcgetattr(io->fd, &orig);
|
---|
1201 | orig.c_cflag &= ~CSIZE;
|
---|
1202 | orig.c_cflag |= CS8;
|
---|
1203 | tcsetattr(io->fd, TCSANOW, &orig);
|
---|
1204 |
|
---|
1205 | return TRUE;
|
---|
1206 | }
|
---|
1207 |
|
---|