source: trunk/csctapi/ifd_towitoko.c@ 67

Last change on this file since 67 was 67, checked in by rorothetroll, 12 years ago

csctapi/io_serial.[c,h] : added read/write serial routine for OS X
csctapi/ifd_towitoko.c : added calls to the new OS X routines inside #ifdefs
csctapi/atr.c : added calls to the new OS X routines inside #ifdefs

File size: 19.7 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
50/*
51 * Not exported constants
52 */
53
54#define IFD_TOWITOKO_TIMEOUT 1000
55#define IFD_TOWITOKO_DELAY 0
56#define IFD_TOWITOKO_BAUDRATE 9600
57#define IFD_TOWITOKO_PS 15
58#define IFD_TOWITOKO_MAX_TRANSMIT 255
59//#define IFD_TOWITOKO_ATR_TIMEOUT 200
60//#define IFD_TOWITOKO_ATR_TIMEOUT 400
61#define IFD_TOWITOKO_ATR_TIMEOUT 800
62#define IFD_TOWITOKO_ATR_MIN_LENGTH 1
63#define IFD_TOWITOKO_CLOCK_RATE (625L * 9600L)
64//#define IFD_TOWITOKO_CLOCK_RATE (372L * 9600L)
65
66#define HI(a) (((a) & 0xff00) >> 8)
67#define LO(a) ((a) & 0x00ff)
68
69/*
70 * Not exported functions declaration
71 */
72
73static int IFD_Towitoko_GetReaderInfo (IFD * ifd);
74static void IFD_Towitoko_Clear (IFD * ifd);
75
76#ifdef USE_GPIO
77
78int gpio_outen,gpio_out,gpio_in;
79unsigned int pin,gpio;
80int gpio_detect=0;
81
82static void set_gpio(int level)
83{
84 read(gpio_outen, &gpio, sizeof(gpio));
85 gpio |= pin;
86 write(gpio_outen, &gpio, sizeof(gpio));
87
88 read(gpio_out, &gpio, sizeof(gpio));
89 if (level>0)
90 gpio|=pin;
91 else
92 gpio&=~pin;
93 write(gpio_out, &gpio, sizeof(gpio));
94}
95
96static void set_gpio1(int level)
97{
98 read(gpio_outen, &gpio, sizeof(gpio));
99 gpio |= 2;
100 write(gpio_outen, &gpio, sizeof(gpio));
101
102 read(gpio_out, &gpio, sizeof(gpio));
103 if (level>0)
104 gpio|=2;
105 else
106 gpio&=~2;
107 write(gpio_out, &gpio, sizeof(gpio));
108}
109
110static void set_gpio_input(void)
111{
112 read(gpio_outen, &gpio, sizeof(gpio));
113 gpio &= ~pin;
114 write(gpio_outen, &gpio, sizeof(gpio));
115}
116
117static int get_gpio(void)
118{
119 set_gpio_input();
120 read(gpio_in, &gpio, sizeof(gpio));
121 return ((int)((gpio&pin)?1:0));
122}
123#endif
124
125/*
126 * Exported functions definition
127 */
128
129IFD * IFD_Towitoko_New ()
130{
131 IFD *ifd;
132
133 ifd = (IFD *) malloc (sizeof (IFD));
134
135 if (ifd != NULL)
136 IFD_Towitoko_Clear (ifd);
137
138 return ifd;
139}
140
141void IFD_Towitoko_Delete (IFD * ifd)
142{
143 free (ifd);
144}
145
146int IFD_Towitoko_Init (IFD * ifd, IO_Serial * io, BYTE slot)
147{
148 IO_Serial_Properties props;
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: Initialicing 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 props.input_bitrate = IFD_TOWITOKO_BAUDRATE;
183 props.output_bitrate = IFD_TOWITOKO_BAUDRATE;
184 props.bits = 8;
185 props.stopbits = 2;
186 props.parity = IO_SERIAL_PARITY_EVEN;
187 props.dtr = IO_SERIAL_HIGH;
188// props.dtr = IO_SERIAL_LOW;
189// props.rts = IO_SERIAL_HIGH;
190 props.rts = IO_SERIAL_LOW;
191
192
193 if (!IO_Serial_SetProperties (io, &props))
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 (ifd, IFD_TOWITOKO_LED_OFF);
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 IO_Serial_Properties props;
262
263 if(ifd->io->com==RTYP_SCI)
264 {
265 return IFD_TOWITOKO_OK;
266 }
267
268 if (IFD_Towitoko_GetMaxBaudrate (ifd) < baudrate)
269 {
270#ifdef DEBUG_IFD
271 printf ("IFD: Tried to set unsupported baudrate: %lu", baudrate);
272#endif
273 return IFD_TOWITOKO_PARAM_ERROR;
274 }
275
276#ifdef DEBUG_IFD
277 printf ("IFD: Setting baudrate to %lu\n", baudrate);
278#endif
279
280 /* Get current settings */
281 if (!IO_Serial_GetProperties (ifd->io, &props))
282 return IFD_TOWITOKO_IO_ERROR;
283
284 if (props.output_bitrate == baudrate)
285 return IFD_TOWITOKO_OK;
286
287
288 /* Set serial device bitrate */
289 props.output_bitrate = baudrate;
290 props.input_bitrate = baudrate;
291
292 if (!IO_Serial_SetProperties (ifd->io, &props))
293 return IFD_TOWITOKO_IO_ERROR;
294
295 return IFD_TOWITOKO_OK;
296}
297
298int IFD_Towitoko_GetBaudrate (IFD * ifd, unsigned long *baudrate)
299{
300 IO_Serial_Properties props;
301
302 if(ifd->io->com==RTYP_SCI)
303 {
304 return IFD_TOWITOKO_OK;
305 }
306
307 /* Get current settings */
308 if (!IO_Serial_GetProperties (ifd->io, &props))
309 return IFD_TOWITOKO_IO_ERROR;
310
311 (*baudrate) = props.output_bitrate;
312
313 return IFD_TOWITOKO_OK;
314}
315
316extern int IFD_Towitoko_SetParity (IFD * ifd, BYTE parity)
317{
318 IO_Serial_Properties props;
319
320 if(ifd->io->com==RTYP_SCI)
321 {
322 return IFD_TOWITOKO_OK;
323 }
324
325#ifdef DEBUG_IFD
326 printf ("IFD: Parity = %s\n",
327 parity == IFD_TOWITOKO_PARITY_ODD ? "Odd" :
328 parity == IFD_TOWITOKO_PARITY_EVEN ? "Even" : "Invalid");
329#endif
330
331 if ((parity != IFD_TOWITOKO_PARITY_EVEN) && (parity != IFD_TOWITOKO_PARITY_ODD) && (parity != IFD_TOWITOKO_PARITY_NONE))
332 return IFD_TOWITOKO_PARAM_ERROR;
333
334 /* Get current settings */
335 if (!IO_Serial_GetProperties (ifd->io, &props))
336 return IFD_TOWITOKO_IO_ERROR;
337
338 if (props.parity !=parity)
339 {
340 props.parity = parity;
341
342 if (!IO_Serial_SetProperties (ifd->io, &props))
343 return IFD_TOWITOKO_IO_ERROR;
344 }
345
346 return IFD_TOWITOKO_OK;
347}
348
349int IFD_Towitoko_SetLED (IFD * ifd, BYTE color)
350{
351 return IFD_TOWITOKO_OK;
352}
353
354int IFD_Towitoko_GetStatus (IFD * ifd, BYTE * result)
355{
356 BYTE status[2];
357 unsigned int modembits=0;
358 int in;
359
360// printf("\n%08X\n", (int)ifd->io);
361
362// status : 0 -start, 1 - card, 2- no card
363
364#ifdef SCI_DEV
365 if(ifd->io->com==RTYP_SCI)
366 {
367 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
368 return IFD_TOWITOKO_IO_ERROR;
369 }
370 else
371#endif
372#if defined(TUXBOX) && defined(PPC)
373 if ((ifd->io->com==RTYP_DB2COM1) || (ifd->io->com==RTYP_DB2COM2))
374 {
375 ushort msr=1;
376 extern int fdmc;
377 IO_Serial_Ioctl_Lock(ifd->io, 1);
378 ioctl(fdmc, GET_PCDAT, &msr);
379 if (ifd->io->com==RTYP_DB2COM2)
380 in=(!(msr & 1));
381 else
382 in=((msr & 0x0f00) == 0x0f00);
383 IO_Serial_Ioctl_Lock(ifd->io, 0);
384 }
385 else
386#endif
387#ifdef USE_GPIO
388 if (gpio_detect)
389 in=get_gpio();
390 else
391#endif
392 {
393 extern int oscam_card_detect;
394 if (ioctl(ifd->io->fd, TIOCMGET,&modembits)<0)
395 return IFD_TOWITOKO_IO_ERROR;
396 switch(oscam_card_detect&0x7f)
397 {
398 case 0: in=(modembits & TIOCM_CAR); break;
399 case 1: in=(modembits & TIOCM_DSR); break;
400 case 2: in=(modembits & TIOCM_CTS); break;
401 case 3: in=(modembits & TIOCM_RNG); break;
402 default: in=0; // dummy
403 }
404 if (!(oscam_card_detect&0x80))
405 in=!in;
406 }
407
408 if (in)
409 {
410 if(ifd->status == 0)
411 {
412 status[0] = IFD_TOWITOKO_CARD_CHANGE;
413 ifd->status = 1;
414#ifdef USE_GPIO
415 if (gpio_detect) set_gpio1(0);
416#endif
417 }
418 else if(ifd->status == 1)
419 {
420 status[0] = IFD_TOWITOKO_CARD_NOCHANGE;
421 }
422 else
423 {
424 status[0] = IFD_TOWITOKO_CARD_CHANGE;
425 ifd->status = 1;
426#ifdef USE_GPIO
427 if (gpio_detect) set_gpio1(0);
428#endif
429 }
430 }
431 else
432 {
433 if(ifd->status == 0)
434 {
435 status[0] = IFD_TOWITOKO_NOCARD_CHANGE;
436 ifd->status = 2;
437#ifdef USE_GPIO
438 if (gpio_detect) set_gpio1(1);
439#endif
440 }
441 else if(ifd->status == 1)
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
450 {
451 status[0] = IFD_TOWITOKO_NOCARD_NOCHANGE;
452 }
453 }
454
455
456 (*result) = status[0];
457
458#ifdef DEBUG_IFD
459 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");
460#endif
461
462 return IFD_TOWITOKO_OK;
463}
464
465int IFD_Towitoko_ActivateICC (IFD * ifd)
466{
467#ifdef DEBUG_IFD
468 printf ("IFD: Activating card\n");
469#endif
470#ifdef SCI_DEV
471 if(ifd->io->com==RTYP_SCI)
472 {
473 int in;
474
475#if defined(TUXBOX) && defined(MIPSEL)
476 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
477#else
478 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
479#endif
480 return IFD_TOWITOKO_IO_ERROR;
481
482 if(in)
483 {
484 struct timespec req_ts;
485 req_ts.tv_sec = 0;
486 req_ts.tv_nsec = 50000000;
487 nanosleep (&req_ts, NULL);
488 return IFD_TOWITOKO_OK;
489 }
490 else
491 {
492 return IFD_TOWITOKO_IO_ERROR;
493 }
494 }
495 else
496#endif
497 {
498 return IFD_TOWITOKO_OK;
499 }
500}
501
502int IFD_Towitoko_DeactivateICC (IFD * ifd)
503{
504#ifdef DEBUG_IFD
505 printf ("IFD: Deactivating card\n");
506#endif
507
508#ifdef SCI_DEV
509 if(ifd->io->com==RTYP_SCI)
510 {
511 int in;
512
513#if defined(TUXBOX) && defined(MIPSEL)
514 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_PRESENT, &in)<0)
515#else
516 if(ioctl(ifd->io->fd, IOCTL_GET_IS_CARD_ACTIVATED, &in)<0)
517#endif
518 return IFD_TOWITOKO_IO_ERROR;
519
520 if(in)
521 {
522 if(ioctl(ifd->io->fd, IOCTL_SET_DEACTIVATE)<0)
523 return IFD_TOWITOKO_IO_ERROR;
524 }
525
526
527 }
528#endif
529
530 return IFD_TOWITOKO_OK;
531}
532
533//extern void print_hex_data(unsigned char *data, int len);
534
535int IFD_Towitoko_ResetAsyncICC (IFD * ifd, ATR ** atr)
536{
537
538#ifdef DEBUG_IFD
539 printf ("IFD: Resetting card:\n");
540#endif
541
542#ifdef SCI_DEV
543 if(ifd->io->com==RTYP_SCI)
544 {
545 unsigned char buf[SCI_MAX_ATR_SIZE];
546 int n = 0;
547 SCI_PARAMETERS params;
548 static char irdeto[] = "IRDETO";
549
550 (*atr) = NULL;
551
552 if(ioctl(ifd->io->fd, IOCTL_SET_RESET)<0)
553 return IFD_TOWITOKO_IO_ERROR;
554
555 if(ioctl(ifd->io->fd, IOCTL_SET_ATR_READY)<0)
556 return IFD_TOWITOKO_IO_ERROR;
557#ifdef OS_MACOSX
558 while(n<SCI_MAX_ATR_SIZE && IO_Serial_Read_MacOSX(ifd->io, IFD_TOWITOKO_ATR_TIMEOUT, 1, buf+n))
559#else
560 while(n<SCI_MAX_ATR_SIZE && IO_Serial_Read(ifd->io, IFD_TOWITOKO_ATR_TIMEOUT, 1, buf+n))
561#endif
562 {
563 n++;
564 }
565
566 if(n==0)
567 return IFD_TOWITOKO_IO_ERROR;
568
569 if(ioctl(ifd->io->fd, IOCTL_GET_PARAMETERS, &params)<0)
570 return IFD_TOWITOKO_IO_ERROR;
571/*
572 printf("T=%d\n", (int)params.T);
573 printf("f=%d\n", (int)params.f);
574 printf("ETU=%d\n", (int)params.ETU);
575 printf("WWT=%d\n", (int)params.WWT);
576 printf("CWT=%d\n", (int)params.CWT);
577 printf("BWT=%d\n", (int)params.BWT);
578 printf("EGT=%d\n", (int)params.EGT);
579 printf("clock=%d\n", (int)params.clock_stop_polarity);
580 printf("check=%d\n", (int)params.check);
581 printf("P=%d\n", (int)params.P);
582 printf("I=%d\n", (int)params.I);
583 printf("U=%d\n", (int)params.U);
584*/
585
586// print_hex_data(buf, n);
587 if(n>9 && !memcmp(buf+4, irdeto, 6))
588 {
589 params.T = 14;
590 params.WWT = 1500;
591 params.EGT = 5;
592 buf[0]=0x3B;
593 }
594/*
595 if(params.ETU>600 && (buf[0]!=0x3B || buf[0]!=0x3F))
596 {
597 params.T = 14;
598 params.WWT = 1500;
599 params.EGT = 5;
600 buf[0]=0x3B;
601 }
602*/
603 (*atr) = ATR_New ();
604 if(ATR_InitFromArray ((*atr), buf, n) == ATR_OK)
605 {
606 struct timespec req_ts;
607 double a;
608
609 ATR_GetParameter(*atr, ATR_PARAMETER_P, &a);
610// printf("atr P=%f\n", a);
611 params.P = (unsigned char)a;
612 ATR_GetParameter(*atr, ATR_PARAMETER_I, &a);
613// printf("atr I=%f\n", a);
614 params.I = (unsigned char)a;
615
616
617 if(ioctl(ifd->io->fd, IOCTL_SET_PARAMETERS, &params)!=0)
618 {
619 ATR_Delete (*atr);
620 (*atr) = NULL;
621 return IFD_TOWITOKO_IO_ERROR;
622 }
623
624
625/*
626 ioctl(ifd->io->fd, IOCTL_GET_PARAMETERS, &params);
627
628 printf("T=%d\n", (int)params.T);
629 printf("f=%d\n", (int)params.f);
630 printf("ETU=%d\n", (int)params.ETU);
631 printf("WWT=%d\n", (int)params.WWT);
632 printf("CWT=%d\n", (int)params.CWT);
633 printf("BWT=%d\n", (int)params.BWT);
634 printf("EGT=%d\n", (int)params.EGT);
635 printf("clock=%d\n", (int)params.clock_stop_polarity);
636 printf("check=%d\n", (int)params.check);
637 printf("P=%d\n", (int)params.P);
638 printf("I=%d\n", (int)params.I);
639 printf("U=%d\n", (int)params.U);*/
640
641
642
643 req_ts.tv_sec = 0;
644 req_ts.tv_nsec = 50000000;
645 nanosleep (&req_ts, NULL);
646 return IFD_TOWITOKO_OK;
647 }
648 else
649 {
650 ATR_Delete (*atr);
651 (*atr) = NULL;
652 return IFD_TOWITOKO_IO_ERROR;
653 }
654 }
655 else
656#endif
657 {
658 int ret;
659 int parity;
660 int i;
661 int par[3] = {IFD_TOWITOKO_PARITY_EVEN, IFD_TOWITOKO_PARITY_ODD, IFD_TOWITOKO_PARITY_NONE};
662#ifdef HAVE_NANOSLEEP
663 struct timespec req_ts;
664 req_ts.tv_sec = 0;
665 req_ts.tv_nsec = 50000000;
666#endif
667
668 for(i=0; i<3; i++)
669 {
670 parity = par[i];
671 IO_Serial_Flush(ifd->io);
672
673 ret = IFD_Towitoko_SetParity (ifd, parity);
674 if (ret != IFD_TOWITOKO_OK)
675 return ret;
676
677 ret = IFD_TOWITOKO_IO_ERROR;
678
679 IO_Serial_Ioctl_Lock(ifd->io, 1);
680#ifdef USE_GPIO
681 if (gpio_detect)
682 {
683 set_gpio(0);
684 set_gpio1(0);
685 }
686 else
687#endif
688 IO_Serial_RTS_Set(ifd->io);
689
690#ifdef HAVE_NANOSLEEP
691 nanosleep (&req_ts, NULL);
692#else
693 usleep (50000L);
694#endif
695#ifdef USE_GPIO
696 if (gpio_detect)
697 {
698 set_gpio_input();
699 set_gpio1(1);
700 }
701 else
702#endif
703 IO_Serial_RTS_Clr(ifd->io);
704
705 IO_Serial_Ioctl_Lock(ifd->io, 0);
706
707 (*atr) = ATR_New ();
708
709 if(ATR_InitFromStream ((*atr), ifd->io, IFD_TOWITOKO_ATR_TIMEOUT) == ATR_OK)
710 ret = IFD_TOWITOKO_OK;
711
712 /* Succesfully retrive ATR */
713 if (ret == IFD_TOWITOKO_OK)
714 {
715 break;
716 }
717 else
718 {
719 ATR_Delete (*atr);
720 (*atr) = NULL;
721#ifdef USE_GPIO
722 if (gpio_detect) set_gpio1(0);
723#endif
724 }
725 }
726
727 IO_Serial_Flush(ifd->io);
728#ifndef NO_PAR_SWITCH
729 IFD_Towitoko_SetParity (ifd, IFD_TOWITOKO_PARITY_NONE);
730#endif
731 return ret;
732 }
733}
734
735int IFD_Towitoko_Transmit (IFD * ifd, IFD_Timings * timings, unsigned size, BYTE * buffer)
736{
737 unsigned block_delay, char_delay, sent=0, to_send = 0;
738
739#ifdef DEBUG_IFD
740 printf ("IFD: Transmit: ");
741 for (sent = 0; sent < size; sent++)
742 printf ("%X ", buffer[sent]);
743 printf ("\n");
744#endif
745
746
747 /* Calculate delays */
748 char_delay = IFD_TOWITOKO_DELAY + timings->char_delay;
749 block_delay = IFD_TOWITOKO_DELAY + timings->block_delay;
750
751#ifdef USE_GPIO
752 if (gpio_detect) set_gpio1(0);
753#endif
754 for (sent = 0; sent < size; sent = sent + to_send)
755 {
756 /* Calculate number of bytes to send */
757 to_send = MIN(size, IFD_TOWITOKO_MAX_TRANSMIT);
758
759 /* Send data */
760 if ((sent == 0) && (block_delay != char_delay))
761 {
762#ifdef OS_MACOSX
763 if (!IO_Serial_Write_MacOSX (ifd->io, block_delay, 1, buffer))
764#else
765 if (!IO_Serial_Write (ifd->io, block_delay, 1, buffer))
766#endif
767 return IFD_TOWITOKO_IO_ERROR;
768
769#ifdef OS_MACOSX
770 if (!IO_Serial_Write_MacOSX (ifd->io, char_delay, to_send-1, buffer+1))
771#else
772 if (!IO_Serial_Write (ifd->io, char_delay, to_send-1, buffer+1))
773#endif
774 return IFD_TOWITOKO_IO_ERROR;
775 }
776 else
777 {
778#ifdef OS_MACOSX
779 if (!IO_Serial_Write_MacOSX (ifd->io, char_delay, to_send, buffer+sent))
780#else
781 if (!IO_Serial_Write (ifd->io, char_delay, to_send, buffer+sent))
782#endif
783 return IFD_TOWITOKO_IO_ERROR;
784 }
785 }
786#ifdef USE_GPIO
787 if (gpio_detect) set_gpio1(1);
788#endif
789 return IFD_TOWITOKO_OK;
790}
791
792int IFD_Towitoko_Receive (IFD * ifd, IFD_Timings * timings, unsigned size, BYTE * buffer)
793{
794 unsigned char_timeout, block_timeout;
795#ifdef DEBUG_IFD
796 int i;
797#endif
798
799 /* Calculate timeouts */
800 char_timeout = IFD_TOWITOKO_TIMEOUT + timings->char_timeout;
801 block_timeout = IFD_TOWITOKO_TIMEOUT + timings->block_timeout;
802#ifdef USE_GPIO
803 if (gpio_detect) set_gpio1(0);
804#endif
805 if (block_timeout != char_timeout)
806 {
807 /* Read first byte using block timeout */
808#ifdef OS_MACOSX
809 if (!IO_Serial_Read_MacOSX (ifd->io, block_timeout, 1, buffer))
810#else
811 if (!IO_Serial_Read (ifd->io, block_timeout, 1, buffer))
812#endif
813 return IFD_TOWITOKO_IO_ERROR;
814
815 if (size > 1)
816 {
817 /* Read remaining data bytes using char timeout */
818#ifdef OS_MACOSX
819 if (!IO_Serial_Read_MacOSX (ifd->io, char_timeout, size - 1, buffer + 1))
820#else
821 if (!IO_Serial_Read (ifd->io, char_timeout, size - 1, buffer + 1))
822#endif
823 return IFD_TOWITOKO_IO_ERROR;
824 }
825 }
826 else
827 {
828 /* Read all data bytes with the same timeout */
829#ifdef OS_MACOSX
830 if (!IO_Serial_Read_MacOSX (ifd->io, char_timeout, size, buffer))
831#else
832 if (!IO_Serial_Read (ifd->io, char_timeout, size, buffer))
833#endif
834 return IFD_TOWITOKO_IO_ERROR;
835 }
836#ifdef USE_GPIO
837 if (gpio_detect) set_gpio1(1);
838#endif
839
840#ifdef DEBUG_IFD
841 printf ("IFD: Receive: ");
842 for (i = 0; i < size; i++)
843 printf ("%X ", buffer[i]);
844 printf ("\n");
845#endif
846
847 return IFD_TOWITOKO_OK;
848}
849
850
851BYTE IFD_Towitoko_GetType (IFD * ifd)
852{
853 return ifd->type;
854}
855
856void IFD_Towitoko_GetDescription (IFD * ifd, BYTE * desc, unsigned length)
857{
858 char buffer[3];
859
860 if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_II)
861 memcpy (desc,"CE2",MIN(length,3));
862
863 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_I)
864 memcpy (desc,"CE1",MIN(length,3));
865
866 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_INT)
867 memcpy (desc,"CDI",MIN(length,3));
868
869 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_MICRO)
870 memcpy (desc,"CDM",MIN(length,3));
871
872 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG_II)
873 memcpy (desc,"KZ2",MIN(length,3));
874
875 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG)
876 memcpy (desc,"KZ1",MIN(length,3));
877
878 else if (ifd->type == IFD_TOWITOKO_MULTICAM)
879 memcpy (desc,"MCM",MIN(length,3));
880
881 else
882 memcpy (desc,"UNK",MIN(length,3));
883
884 snprintf (buffer, 3, "%02X", ifd->firmware);
885
886 if (length > 3)
887 memcpy (desc+3, buffer, MIN(length-3,2));
888}
889
890BYTE
891IFD_Towitoko_GetFirmware (IFD * ifd)
892{
893 return ifd->firmware;
894}
895
896BYTE
897IFD_Towitoko_GetSlot (IFD * ifd)
898{
899 return ifd->slot;
900}
901
902unsigned
903IFD_Towitoko_GetNumSlots (IFD * ifd)
904{
905 return 1;
906}
907
908unsigned long
909IFD_Towitoko_GetClockRate (IFD * ifd)
910{
911 return IFD_TOWITOKO_CLOCK_RATE;
912}
913
914unsigned long
915IFD_Towitoko_GetMaxBaudrate (IFD * ifd)
916{
917 return 115200L;
918}
919
920/*
921 * Not exported funcions definition
922 */
923
924
925static int IFD_Towitoko_GetReaderInfo (IFD * ifd)
926{
927 BYTE status[3];
928
929 status[0] = IFD_TOWITOKO_MULTICAM;
930 status[1] = 0x00;
931
932 ifd->type = status[0];
933 ifd->firmware = status[1];
934
935#ifdef DEBUG_IFD
936 printf ("IFD: Reader type = %s\n",
937 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_II ? "Chipdrive Extern II" :
938 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_I ? "Chipdrive Extern I" :
939 status[0] == IFD_TOWITOKO_CHIPDRIVE_INT ? "Chipdrive Intern" :
940 status[0] == IFD_TOWITOKO_CHIPDRIVE_MICRO ? "Chipdrive Micro" :
941 status[0] == IFD_TOWITOKO_KARTENZWERG_II ? "Kartenzwerg II" :
942 status[0] == IFD_TOWITOKO_MULTICAM ? "Multicam" :
943 status[0] == IFD_TOWITOKO_KARTENZWERG ? "Kartenzwerg" : "Unknown");
944#endif
945
946 return IFD_TOWITOKO_OK;
947}
948
949
950static void IFD_Towitoko_Clear (IFD * ifd)
951{
952 ifd->io = NULL;
953 ifd->slot = 0x00;
954 ifd->type = 0x00;
955 ifd->firmware = 0x00;
956 ifd->status = 0;
957}
Note: See TracBrowser for help on using the repository browser.