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