source: branches/monitor-improvement/csctapi/io_serial.c@ 1180

Last change on this file since 1180 was 1180, checked in by alno, 12 years ago

WebIf:

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