source: branches/monitor-improvement/csctapi/ifd_towitoko.c@ 1177

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

WebIf:

  • Merging with 1171 of trunk
File size: 20.0 KB
Line 
1/*
2 ifd_towitoko.c
3 This module provides IFD handling 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#ifdef OS_HPUX
30#include <sys/modem.h>
31#endif
32#include <termios.h>
33#include <unistd.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36#ifdef HAVE_POLL
37#include <sys/poll.h>
38#else
39#include <sys/signal.h>
40#include <sys/types.h>
41#include <sys/time.h>
42#endif
43#include <sys/ioctl.h>
44#include <time.h>
45#include "ifd_towitoko.h"
46#include "io_serial.h"
47#include "sci_global.h"
48#include "sci_ioctl.h"
49#include "ifd.h"
50
51/*
52 * Not exported constants
53 */
54
55#define IFD_TOWITOKO_TIMEOUT 1000
56#define IFD_TOWITOKO_DELAY 0
57#define IFD_TOWITOKO_BAUDRATE 9600
58//#define IFD_TOWITOKO_PS 15
59#define IFD_TOWITOKO_MAX_TRANSMIT 255
60//#define IFD_TOWITOKO_ATR_TIMEOUT 200
61//#define IFD_TOWITOKO_ATR_TIMEOUT 400
62#define IFD_TOWITOKO_ATR_TIMEOUT 800
63#define IFD_TOWITOKO_ATR_MIN_LENGTH 1
64#define IFD_TOWITOKO_CLOCK_RATE (625L * 9600L)
65//#define IFD_TOWITOKO_CLOCK_RATE (372L * 9600L)
66
67#define HI(a) (((a) & 0xff00) >> 8)
68#define LO(a) ((a) & 0x00ff)
69
70/*
71 * Not exported functions declaration
72 */
73
74static int IFD_Towitoko_GetReaderInfo (IFD * ifd);
75static void IFD_Towitoko_Clear (IFD * ifd);
76
77#ifdef USE_GPIO
78
79int gpio_outen,gpio_out,gpio_in;
80unsigned int pin,gpio;
81int gpio_detect=0;
82
83static void set_gpio(int level)
84{
85 read(gpio_outen, &gpio, sizeof(gpio));
86 gpio |= pin;
87 write(gpio_outen, &gpio, sizeof(gpio));
88
89 read(gpio_out, &gpio, sizeof(gpio));
90 if (level>0)
91 gpio|=pin;
92 else
93 gpio&=~pin;
94 write(gpio_out, &gpio, sizeof(gpio));
95}
96
97static void set_gpio1(int level)
98{
99 read(gpio_outen, &gpio, sizeof(gpio));
100 gpio |= 2;
101 write(gpio_outen, &gpio, sizeof(gpio));
102
103 read(gpio_out, &gpio, sizeof(gpio));
104 if (level>0)
105 gpio|=2;
106 else
107 gpio&=~2;
108 write(gpio_out, &gpio, sizeof(gpio));
109}
110
111static void set_gpio_input(void)
112{
113 read(gpio_outen, &gpio, sizeof(gpio));
114 gpio &= ~pin;
115 write(gpio_outen, &gpio, sizeof(gpio));
116}
117
118static int get_gpio(void)
119{
120 set_gpio_input();
121 read(gpio_in, &gpio, sizeof(gpio));
122 return ((int)((gpio&pin)?1:0));
123}
124#endif
125
126/*
127 * Exported functions definition
128 */
129
130IFD * IFD_Towitoko_New ()
131{
132 IFD *ifd;
133
134 ifd = (IFD *) malloc (sizeof (IFD));
135
136 if (ifd != NULL)
137 IFD_Towitoko_Clear (ifd);
138
139 return ifd;
140}
141
142void IFD_Towitoko_Delete (IFD * ifd)
143{
144 free (ifd);
145}
146
147int IFD_Towitoko_Init (IFD * ifd, IO_Serial * io, BYTE slot)
148{
149 int ret;
150
151#ifdef USE_GPIO
152 extern int oscam_card_detect;
153 if (oscam_card_detect>4)
154 {
155 gpio_detect=oscam_card_detect-3;
156 pin = 1<<gpio_detect;
157 gpio_outen=open("/dev/gpio/outen",O_RDWR);
158 gpio_out=open("/dev/gpio/out",O_RDWR);
159 gpio_in=open("/dev/gpio/in",O_RDWR);
160 set_gpio_input();
161 }
162#endif
163
164#ifdef DEBUG_IFD
165 printf ("IFD: Initializing slot number %d, com=%d\n", slot, io->com);
166#endif
167
168// if ((slot != IFD_TOWITOKO_SLOT_MULTICAM) && (slot != IFD_TOWITOKO_SLOT_A) && (slot != IFD_TOWITOKO_SLOT_B))
169 if (slot != IFD_TOWITOKO_SLOT_MULTICAM )
170 return IFD_TOWITOKO_PARAM_ERROR;
171
172 if(io->com==RTYP_SCI)
173 {
174 ifd->io = io;
175 ifd->slot = slot;
176 ifd->type = IFD_TOWITOKO_MULTICAM;
177 return IFD_TOWITOKO_OK;
178 }
179
180
181 /* Default serial port settings */
182 io->input_bitrate = IFD_TOWITOKO_BAUDRATE;
183 io->output_bitrate = IFD_TOWITOKO_BAUDRATE;
184 io->bits = 8;
185 io->stopbits = 2;
186 io->parity = IO_SERIAL_PARITY_EVEN;
187 io->dtr = IO_SERIAL_HIGH;
188// io->dtr = IO_SERIAL_LOW;
189// io->rts = IO_SERIAL_HIGH;
190 io->rts = IO_SERIAL_LOW;
191
192
193 if (!IO_Serial_SetProperties (io))
194 return IFD_TOWITOKO_IO_ERROR;
195
196 /* Default ifd settings */
197
198 ifd->io = io;
199 ifd->slot = slot;
200 ifd->type = IFD_TOWITOKO_MULTICAM;
201
202 ret = IFD_Towitoko_SetBaudrate (ifd, IFD_TOWITOKO_BAUDRATE);
203
204 if (ret != IFD_TOWITOKO_OK)
205 {
206 IFD_Towitoko_Clear (ifd);
207 return ret;
208 }
209
210 ret = IFD_Towitoko_SetParity (ifd, IFD_TOWITOKO_PARITY_EVEN);
211
212 if (ret != IFD_TOWITOKO_OK)
213 {
214 IFD_Towitoko_Clear (ifd);
215 return ret;
216 }
217
218 ret = IFD_Towitoko_GetReaderInfo (ifd);
219
220 if (ret != IFD_TOWITOKO_OK)
221 {
222 IFD_Towitoko_Clear (ifd);
223 }
224 else
225 {
226 IO_Serial_Flush(ifd->io);
227 }
228
229 return ret;
230}
231
232int IFD_Towitoko_Close (IFD * ifd)
233{
234 int ret;
235
236#ifdef USE_GPIO
237 if(gpio_detect)
238 {
239 close(gpio_outen);
240 close(gpio_out);
241 close(gpio_in);
242 }
243#endif
244
245#ifdef DEBUG_IFD
246 printf ("IFD: Closing slot number %d\n", ifd->slot);
247#endif
248
249 ret = IFD_Towitoko_SetLED ();
250 if (ret != IFD_TOWITOKO_OK)
251 return ret;
252
253 IFD_Towitoko_Clear (ifd);
254
255
256 return IFD_TOWITOKO_OK;
257}
258
259int IFD_Towitoko_SetBaudrate (IFD * ifd, unsigned long baudrate)
260{
261 if(ifd->io->com==RTYP_SCI)
262 {
263 return IFD_TOWITOKO_OK;
264 }
265/*
266 if (IFD_Towitoko_GetMaxBaudrate () < baudrate)
267 {
268#ifdef DEBUG_IFD
269 printf ("IFD: Tried to set unsupported baudrate: %lu", baudrate);
270#endif
271 return IFD_TOWITOKO_PARAM_ERROR;
272 }*/
273
274#ifdef DEBUG_IFD
275 printf ("IFD: Setting baudrate to %lu\n", baudrate);
276#endif
277 /* Get current settings */
278 if (!IO_Serial_GetProperties (ifd->io))
279 return IFD_TOWITOKO_IO_ERROR;
280
281 if (ifd->io->output_bitrate == baudrate)
282 return IFD_TOWITOKO_OK;
283
284
285 /* Set serial device bitrate */
286 ifd->io->output_bitrate = baudrate;
287 ifd->io->input_bitrate = baudrate;
288
289 if (!IO_Serial_SetProperties (ifd->io))
290 return IFD_TOWITOKO_IO_ERROR;
291
292 return IFD_TOWITOKO_OK;
293}
294
295int IFD_Towitoko_GetBaudrate (IFD * ifd, unsigned long *baudrate)
296{
297 if(ifd->io->com==RTYP_SCI)
298 {
299 return IFD_TOWITOKO_OK;
300 }
301
302 /* Get current settings */
303 if (!IO_Serial_GetProperties (ifd->io))
304 return IFD_TOWITOKO_IO_ERROR;
305
306 (*baudrate) = ifd->io->output_bitrate;
307
308 return IFD_TOWITOKO_OK;
309}
310
311extern int IFD_Towitoko_SetParity (IFD * ifd, BYTE parity)
312{
313 if(ifd->io->com==RTYP_SCI)
314 {
315 return IFD_TOWITOKO_OK;
316 }
317
318#ifdef DEBUG_IFD
319 printf ("IFD: Parity = %s\n",
320 parity == IFD_TOWITOKO_PARITY_ODD ? "Odd" :
321 parity == IFD_TOWITOKO_PARITY_EVEN ? "Even" : "Invalid");
322#endif
323
324 if ((parity != IFD_TOWITOKO_PARITY_EVEN) && (parity != IFD_TOWITOKO_PARITY_ODD) && (parity != IFD_TOWITOKO_PARITY_NONE))
325 return IFD_TOWITOKO_PARAM_ERROR;
326
327 /* Get current settings */
328 if (!IO_Serial_GetProperties (ifd->io))
329 return IFD_TOWITOKO_IO_ERROR;
330
331 if (ifd->io->parity !=parity)
332 {
333 ifd->io->parity = parity;
334
335 if (!IO_Serial_SetProperties (ifd->io))
336 return IFD_TOWITOKO_IO_ERROR;
337 }
338
339 return IFD_TOWITOKO_OK;
340}
341
342int IFD_Towitoko_SetLED ()
343{
344 return IFD_TOWITOKO_OK;
345}
346
347int IFD_Towitoko_GetStatus (IFD * ifd, BYTE * result)
348{
349 BYTE status[2];
350 unsigned int modembits=0;
351 int in;
352
353// printf("\n%08X\n", (int)ifd->io);
354
355// status : 0 -start, 1 - card, 2- no card
356
357#ifdef SCI_DEV
358 if(ifd->io->com==RTYP_SCI)
359 {
360 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
361 return IFD_TOWITOKO_IO_ERROR;
362 }
363 else
364#elif COOL
365 if(ifd->io->com==RTYP_SCI)
366 {
367 // in=1;//FIXME
368 int state;
369 if (cnxt_smc_get_state(handle, &state))
370 return IFD_TOWITOKO_IO_ERROR;
371 //guessing: state = 0 no card, 1 = not ready, 2 = ready
372 if (state)
373 in = 1; //CARD, even if not ready report card is in, or it will never get activated
374 else
375 in = 0; //Nocard
376 }
377 else
378#endif
379
380#if defined(TUXBOX) && defined(PPC)
381 if ((ifd->io->com==RTYP_DB2COM1) || (ifd->io->com==RTYP_DB2COM2))
382 {
383 ushort msr=1;
384 extern int fdmc;
385 IO_Serial_Ioctl_Lock(ifd->io, 1);
386 ioctl(fdmc, GET_PCDAT, &msr);
387 if (ifd->io->com==RTYP_DB2COM2)
388 in=(!(msr & 1));
389 else
390 in=((msr & 0x0f00) == 0x0f00);
391 IO_Serial_Ioctl_Lock(ifd->io, 0);
392 }
393 else
394#endif
395#ifdef USE_GPIO
396 if (gpio_detect)
397 in=get_gpio();
398 else
399#endif
400 {
401 extern int oscam_card_detect;
402 if (ioctl(ifd->io->fd, TIOCMGET,&modembits)<0)
403 return IFD_TOWITOKO_IO_ERROR;
404 switch(oscam_card_detect&0x7f)
405 {
406 case 0: in=(modembits & TIOCM_CAR); break;
407 case 1: in=(modembits & TIOCM_DSR); break;
408 case 2: in=(modembits & TIOCM_CTS); break;
409 case 3: in=(modembits & TIOCM_RNG); break;
410 default: in=0; // dummy
411 }
412 if (!(oscam_card_detect&0x80))
413 in=!in;
414 }
415
416 if (in)
417 {
418 if(ifd->status == 0)
419 {
420 status[0] = IFD_TOWITOKO_CARD_CHANGE;
421 ifd->status = 1;
422#ifdef USE_GPIO
423 if (gpio_detect) set_gpio1(0);
424#endif
425 }
426 else if(ifd->status == 1)
427 {
428 status[0] = IFD_TOWITOKO_CARD_NOCHANGE;
429 }
430 else
431 {
432 status[0] = IFD_TOWITOKO_CARD_CHANGE;
433 ifd->status = 1;
434#ifdef USE_GPIO
435 if (gpio_detect) set_gpio1(0);
436#endif
437 }
438 }
439 else
440 {
441 if(ifd->status == 0)
442 {
443 status[0] = IFD_TOWITOKO_NOCARD_CHANGE;
444 ifd->status = 2;
445#ifdef USE_GPIO
446 if (gpio_detect) set_gpio1(1);
447#endif
448 }
449 else if(ifd->status == 1)
450 {
451 status[0] = IFD_TOWITOKO_NOCARD_CHANGE;
452 ifd->status = 2;
453#ifdef USE_GPIO
454 if (gpio_detect) set_gpio1(1);
455#endif
456 }
457 else
458 {
459 status[0] = IFD_TOWITOKO_NOCARD_NOCHANGE;
460 }
461 }
462
463
464 (*result) = status[0];
465
466#ifdef DEBUG_IFD
467 printf ("IFD: com%d Status = %s / %s\n", ifd->io->com, IFD_TOWITOKO_CARD(status[0])? "card": "no card", IFD_TOWITOKO_CHANGE(status[0])? "change": "no change");
468#endif
469
470 return IFD_TOWITOKO_OK;
471}
472
473int IFD_Towitoko_ActivateICC (IFD * ifd)
474{
475#ifdef DEBUG_IFD
476 printf ("IFD: Activating card\n");
477#endif
478#ifdef SCI_DEV
479 if(ifd->io->com==RTYP_SCI)
480 {
481 int in;
482
483#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
484 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
485#else
486 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
487#endif
488 return IFD_TOWITOKO_IO_ERROR;
489
490 if(in)
491 {
492 struct timespec req_ts;
493 req_ts.tv_sec = 0;
494 req_ts.tv_nsec = 50000000;
495 nanosleep (&req_ts, NULL);
496 return IFD_TOWITOKO_OK;
497 }
498 else
499 {
500 return IFD_TOWITOKO_IO_ERROR;
501 }
502 }
503 else
504#endif
505 {
506 return IFD_TOWITOKO_OK;
507 }
508}
509
510int IFD_Towitoko_DeactivateICC (IFD * ifd)
511{
512#ifdef DEBUG_IFD
513 printf ("IFD: Deactivating card\n");
514#endif
515
516#ifdef SCI_DEV
517 if(ifd->io->com==RTYP_SCI)
518 {
519 int in;
520
521#if defined(TUXBOX) && (defined(MIPSEL) || defined(PPC) || defined(SH4))
522 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
523#else
524 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
525#endif
526 return IFD_TOWITOKO_IO_ERROR;
527
528 if(in)
529 {
530 if(ioctl(ifd->io->fd, IOCTL_SET_DEACTIVATE)<0)
531 return IFD_TOWITOKO_IO_ERROR;
532 }
533
534
535 }
536#endif
537
538 return IFD_TOWITOKO_OK;
539}
540
541//extern void print_hex_data(unsigned char *data, int len);
542
543int IFD_Towitoko_ResetAsyncICC (IFD * ifd, ATR ** atr)
544{
545
546#ifdef DEBUG_IFD
547 printf ("IFD: Resetting card:\n");
548#endif
549
550#ifdef SCI_DEV
551 if(ifd->io->com==RTYP_SCI)
552 {
553 unsigned char buf[SCI_MAX_ATR_SIZE];
554 int n = 0;
555 SCI_PARAMETERS params;
556#ifdef SH4
557 struct timeval tv, tv_spent;
558 int atr_size = 2, TDi_exists = 0;
559#endif
560
561 (*atr) = NULL;
562
563#ifdef SH4
564 memset(&params,0,sizeof(SCI_PARAMETERS));
565
566 params.ETU = 372;
567 params.EGT = 3;
568 params.fs = 9;
569 params.T = 0;
570
571 if(ioctl(ifd->io->fd, IOCTL_SET_PARAMETERS, &params)!=0)
572 return IFD_TOWITOKO_IO_ERROR;
573#endif
574
575 if(ioctl(ifd->io->fd, IOCTL_SET_RESET)<0)
576 return IFD_TOWITOKO_IO_ERROR;
577
578#ifdef SH4
579 gettimeofday(&tv,0);
580 memcpy(&tv_spent,&tv,sizeof(struct timeval));
581
582 while(n<atr_size && (tv_spent.tv_sec-tv.tv_sec)<10)
583 {
584 if(IO_Serial_Read(ifd->io, IFD_TOWITOKO_ATR_TIMEOUT, 1, buf+n))
585 n++;
586 gettimeofday(&tv_spent,0);
587 if(n==2) // format character
588 {
589 // high nibble = TA1 , TB1 , TC1 , TD1
590 if(buf[n-1] & 0x10)
591 atr_size++;
592 if(buf[n-1] & 0x20)
593 atr_size++;
594 if(buf[n-1] & 0x40)
595 atr_size++;
596 if(buf[n-1] & 0x80)
597 {
598 atr_size++;
599 TDi_exists=atr_size;
600 }
601 atr_size+=(buf[n-1] & 0x0F); // historical bytes
602 }
603 if( (TDi_exists>0) && (n==TDi_exists) )
604 {
605 TDi_exists=0;
606 // high nibble = TA1 , TB1 , TC1 , TD1
607 if(buf[n-1] & 0x10)
608 atr_size++;
609 if(buf[n-1] & 0x20)
610 atr_size++;
611 if(buf[n-1] & 0x40)
612 atr_size++;
613 if(buf[n-1] & 0x80)
614 {
615 atr_size++;
616 TDi_exists=atr_size;
617 }
618 }
619 }
620#else
621 while(n<SCI_MAX_ATR_SIZE && IO_Serial_Read(ifd->io, IFD_TOWITOKO_ATR_TIMEOUT, 1, buf+n))
622 {
623 n++;
624 }
625#endif
626
627 if(n==0)
628 return IFD_TOWITOKO_IO_ERROR;
629#elif COOL
630 if(ifd->io->com==RTYP_SCI) {
631 //Cool_Reset(atr);
632 //reset needs clock to be reset by hand
633 typedef unsigned long u_int32;
634 u_int32 clk;
635 clk = 357*10000; // MHZ
636 if (cnxt_smc_set_clock_freq(handle, clk))
637 return IFD_TOWITOKO_IO_ERROR;
638
639 //reset card
640 int timeout = 5000; // Timout in ms?
641 if (cnxt_smc_reset_card (handle, timeout, NULL, NULL))
642 return IFD_TOWITOKO_IO_ERROR;
643
644 int n = 40;
645 unsigned char buf[40];
646 if (cnxt_smc_get_atr (handle, buf, &n))
647 return IFD_TOWITOKO_IO_ERROR;
648#endif
649
650#if defined(SCI_DEV) || defined(COOL)
651 (*atr) = ATR_New ();
652 if(ATR_InitFromArray ((*atr), buf, n) == ATR_OK)
653 {
654 struct timespec req_ts;
655 req_ts.tv_sec = 0;
656 req_ts.tv_nsec = 50000000;
657 nanosleep (&req_ts, NULL);
658#ifdef SCI_DEV
659 if (ioctl(ifd->io->fd, IOCTL_SET_ATR_READY)<0)
660 return IFD_TOWITOKO_IO_ERROR;
661#endif
662 return IFD_TOWITOKO_OK;
663 }
664 else
665 {
666 ATR_Delete (*atr);
667 (*atr) = NULL;
668 return IFD_TOWITOKO_IO_ERROR;
669 }
670 }
671 else
672#endif
673 {
674 int ret;
675 int parity;
676 int i;
677 int par[3] = {IFD_TOWITOKO_PARITY_EVEN, IFD_TOWITOKO_PARITY_ODD, IFD_TOWITOKO_PARITY_NONE};
678#ifdef HAVE_NANOSLEEP
679 struct timespec req_ts;
680 req_ts.tv_sec = 0;
681 req_ts.tv_nsec = 50000000;
682#endif
683
684 (*atr) = NULL;
685 for(i=0; i<3; i++)
686 {
687 parity = par[i];
688 IO_Serial_Flush(ifd->io);
689
690 ret = IFD_Towitoko_SetParity (ifd, parity);
691 if (ret != IFD_TOWITOKO_OK)
692 return ret;
693
694 ret = IFD_TOWITOKO_IO_ERROR;
695
696 IO_Serial_Ioctl_Lock(ifd->io, 1);
697#ifdef USE_GPIO
698 if (gpio_detect)
699 {
700 set_gpio(0);
701 set_gpio1(0);
702 }
703 else
704#endif
705 IO_Serial_RTS_Set(ifd->io);
706
707#ifdef HAVE_NANOSLEEP
708 nanosleep (&req_ts, NULL);
709#else
710 usleep (50000L);
711#endif
712#ifdef USE_GPIO
713 if (gpio_detect)
714 {
715 set_gpio_input();
716 set_gpio1(1);
717 }
718 else
719#endif
720 IO_Serial_RTS_Clr(ifd->io);
721
722 IO_Serial_Ioctl_Lock(ifd->io, 0);
723
724 (*atr) = ATR_New ();
725
726 if(ATR_InitFromStream ((*atr), ifd->io, IFD_TOWITOKO_ATR_TIMEOUT) == ATR_OK)
727 ret = IFD_TOWITOKO_OK;
728
729 /* Succesfully retrive ATR */
730 if (ret == IFD_TOWITOKO_OK)
731 {
732 break;
733 }
734 else
735 {
736 ATR_Delete (*atr);
737 (*atr) = NULL;
738#ifdef USE_GPIO
739 if (gpio_detect) set_gpio1(0);
740#endif
741 }
742 }
743
744 IO_Serial_Flush(ifd->io);
745#ifndef NO_PAR_SWITCH
746 IFD_Towitoko_SetParity (ifd, IFD_TOWITOKO_PARITY_NONE);
747#endif
748
749/*
750 //PLAYGROUND faking ATR for test purposes only
751 //
752 // sky 919 unsigned char atr_test[] = { 0x3F, 0xFF, 0x13, 0x25, 0x03, 0x10, 0x80, 0x33, 0xB0, 0x0E, 0x69, 0xFF, 0x4A, 0x50, 0x70, 0x00, 0x00, 0x49, 0x54, 0x02, 0x00, 0x00 };
753 // HD+ unsigned char atr_test[] = { 0x3F, 0xFF, 0x95, 0x00, 0xFF, 0x91, 0x81, 0x71, 0xFE, 0x47, 0x00, 0x44, 0x4E, 0x41, 0x53, 0x50, 0x31, 0x34, 0x32, 0x20, 0x52, 0x65, 0x76, 0x47, 0x43, 0x34, 0x63 };
754 // S02 = irdeto unsigned char atr_test[] = { 0x3B, 0x9F, 0x21, 0x0E, 0x49, 0x52, 0x44, 0x45, 0x54, 0x4F, 0x20, 0x41, 0x43, 0x53, 0x03};
755 //cryptoworks unsigned char atr_test[] = { 0x3B, 0x78, 0x12, 0x00, 0x00, 0x65, 0xC4, 0x05, 0xFF, 0x8F, 0xF1, 0x90, 0x00 };
756 ATR_Delete(*atr); //throw away actual ATR
757 (*atr) = ATR_New ();
758 ATR_InitFromArray ((*atr), atr_test, sizeof(atr_test));
759 //END OF PLAYGROUND
760*/
761
762 return ret;
763 }
764}
765
766int IFD_Towitoko_Transmit (IFD * ifd, IFD_Timings * timings, unsigned size, BYTE * buffer)
767{
768 unsigned block_delay, char_delay, sent=0, to_send = 0;
769
770#ifdef DEBUG_IFD
771 printf ("IFD: Transmit: ");
772 for (sent = 0; sent < size; sent++)
773 printf ("%X ", buffer[sent]);
774 printf ("\n");
775#endif
776
777
778 /* Calculate delays */
779 char_delay = IFD_TOWITOKO_DELAY + timings->char_delay;
780 block_delay = IFD_TOWITOKO_DELAY + timings->block_delay;
781
782#ifdef USE_GPIO
783 if (gpio_detect) set_gpio1(0);
784#endif
785 for (sent = 0; sent < size; sent = sent + to_send)
786 {
787 /* Calculate number of bytes to send */
788 to_send = MIN(size, IFD_TOWITOKO_MAX_TRANSMIT);
789
790 /* Send data */
791 if ((sent == 0) && (block_delay != char_delay))
792 {
793 if (!IO_Serial_Write (ifd->io, block_delay, 1, buffer))
794 return IFD_TOWITOKO_IO_ERROR;
795
796 if (!IO_Serial_Write (ifd->io, char_delay, to_send-1, buffer+1))
797 return IFD_TOWITOKO_IO_ERROR;
798 }
799 else
800 {
801 if (!IO_Serial_Write (ifd->io, char_delay, to_send, buffer+sent))
802 return IFD_TOWITOKO_IO_ERROR;
803 }
804 }
805#ifdef USE_GPIO
806 if (gpio_detect) set_gpio1(1);
807#endif
808 return IFD_TOWITOKO_OK;
809}
810
811int IFD_Towitoko_Receive (IFD * ifd, IFD_Timings * timings, unsigned size, BYTE * buffer)
812{
813 unsigned char_timeout, block_timeout;
814#ifdef DEBUG_IFD
815 int i;
816#endif
817
818 /* Calculate timeouts */
819 char_timeout = IFD_TOWITOKO_TIMEOUT + timings->char_timeout;
820 block_timeout = IFD_TOWITOKO_TIMEOUT + timings->block_timeout;
821#ifdef USE_GPIO
822 if (gpio_detect) set_gpio1(0);
823#endif
824 if (block_timeout != char_timeout)
825 {
826 /* Read first byte using block timeout */
827 if (!IO_Serial_Read (ifd->io, block_timeout, 1, buffer))
828 return IFD_TOWITOKO_IO_ERROR;
829
830 if (size > 1)
831 {
832 /* Read remaining data bytes using char timeout */
833 if (!IO_Serial_Read (ifd->io, char_timeout, size - 1, buffer + 1))
834 return IFD_TOWITOKO_IO_ERROR;
835 }
836 }
837 else
838 {
839 /* Read all data bytes with the same timeout */
840 if (!IO_Serial_Read (ifd->io, char_timeout, size, buffer))
841 return IFD_TOWITOKO_IO_ERROR;
842 }
843#ifdef USE_GPIO
844 if (gpio_detect) set_gpio1(1);
845#endif
846
847#ifdef DEBUG_IFD
848 printf ("IFD: Receive: ");
849 for (i = 0; i < size; i++)
850 printf ("%X ", buffer[i]);
851 printf ("\n");
852#endif
853
854 return IFD_TOWITOKO_OK;
855}
856
857
858BYTE IFD_Towitoko_GetType (IFD * ifd)
859{
860 return ifd->type;
861}
862
863void IFD_Towitoko_GetDescription (IFD * ifd, BYTE * desc, unsigned length)
864{
865 char buffer[3];
866
867 if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_II)
868 memcpy (desc,"CE2",MIN(length,3));
869
870 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_I)
871 memcpy (desc,"CE1",MIN(length,3));
872
873 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_INT)
874 memcpy (desc,"CDI",MIN(length,3));
875
876 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_MICRO)
877 memcpy (desc,"CDM",MIN(length,3));
878
879 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG_II)
880 memcpy (desc,"KZ2",MIN(length,3));
881
882 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG)
883 memcpy (desc,"KZ1",MIN(length,3));
884
885 else if (ifd->type == IFD_TOWITOKO_MULTICAM)
886 memcpy (desc,"MCM",MIN(length,3));
887
888 else
889 memcpy (desc,"UNK",MIN(length,3));
890
891 snprintf (buffer, 3, "%02X", ifd->firmware);
892
893 if (length > 3)
894 memcpy (desc+3, buffer, MIN(length-3,2));
895}
896
897BYTE
898IFD_Towitoko_GetFirmware (IFD * ifd)
899{
900 return ifd->firmware;
901}
902
903BYTE
904IFD_Towitoko_GetSlot (IFD * ifd)
905{
906 return ifd->slot;
907}
908
909unsigned
910IFD_Towitoko_GetNumSlots ()
911{
912 return 1;
913}
914
915unsigned long
916IFD_Towitoko_GetMaxBaudrate ()
917{
918 return 115200L;
919}
920
921/*
922 * Not exported funcions definition
923 */
924
925
926static int IFD_Towitoko_GetReaderInfo (IFD * ifd)
927{
928 BYTE status[3];
929
930 status[0] = IFD_TOWITOKO_MULTICAM;
931 status[1] = 0x00;
932
933 ifd->type = status[0];
934 ifd->firmware = status[1];
935
936#ifdef DEBUG_IFD
937 printf ("IFD: Reader type = %s\n",
938 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_II ? "Chipdrive Extern II" :
939 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_I ? "Chipdrive Extern I" :
940 status[0] == IFD_TOWITOKO_CHIPDRIVE_INT ? "Chipdrive Intern" :
941 status[0] == IFD_TOWITOKO_CHIPDRIVE_MICRO ? "Chipdrive Micro" :
942 status[0] == IFD_TOWITOKO_KARTENZWERG_II ? "Kartenzwerg II" :
943 status[0] == IFD_TOWITOKO_MULTICAM ? "Multicam" :
944 status[0] == IFD_TOWITOKO_KARTENZWERG ? "Kartenzwerg" : "Unknown");
945#endif
946
947 return IFD_TOWITOKO_OK;
948}
949
950
951static void IFD_Towitoko_Clear (IFD * ifd)
952{
953 ifd->io = NULL;
954 ifd->slot = 0x00;
955 ifd->type = 0x00;
956 ifd->firmware = 0x00;
957 ifd->status = 0;
958}
Note: See TracBrowser for help on using the repository browser.