source: trunk/csctapi/ifd_towitoko.c@ 1

Last change on this file since 1 was 1, checked in by root, 14 years ago

initial import

File size: 19.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
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
558 while(n<SCI_MAX_ATR_SIZE && IO_Serial_Read(ifd->io, IFD_TOWITOKO_ATR_TIMEOUT, 1, buf+n))
559 {
560 n++;
561 }
562
563 if(n==0)
564 return IFD_TOWITOKO_IO_ERROR;
565
566 if(ioctl(ifd->io->fd, IOCTL_GET_PARAMETERS, &params)<0)
567 return IFD_TOWITOKO_IO_ERROR;
568/*
569 printf("T=%d\n", (int)params.T);
570 printf("f=%d\n", (int)params.f);
571 printf("ETU=%d\n", (int)params.ETU);
572 printf("WWT=%d\n", (int)params.WWT);
573 printf("CWT=%d\n", (int)params.CWT);
574 printf("BWT=%d\n", (int)params.BWT);
575 printf("EGT=%d\n", (int)params.EGT);
576 printf("clock=%d\n", (int)params.clock_stop_polarity);
577 printf("check=%d\n", (int)params.check);
578 printf("P=%d\n", (int)params.P);
579 printf("I=%d\n", (int)params.I);
580 printf("U=%d\n", (int)params.U);
581*/
582
583// print_hex_data(buf, n);
584 if(n>9 && !memcmp(buf+4, irdeto, 6))
585 {
586 params.T = 14;
587 params.WWT = 1500;
588 params.EGT = 5;
589 buf[0]=0x3B;
590 }
591/*
592 if(params.ETU>600 && (buf[0]!=0x3B || buf[0]!=0x3F))
593 {
594 params.T = 14;
595 params.WWT = 1500;
596 params.EGT = 5;
597 buf[0]=0x3B;
598 }
599*/
600 (*atr) = ATR_New ();
601 if(ATR_InitFromArray ((*atr), buf, n) == ATR_OK)
602 {
603 struct timespec req_ts;
604 double a;
605
606 ATR_GetParameter(*atr, ATR_PARAMETER_P, &a);
607// printf("atr P=%f\n", a);
608 params.P = (unsigned char)a;
609 ATR_GetParameter(*atr, ATR_PARAMETER_I, &a);
610// printf("atr I=%f\n", a);
611 params.I = (unsigned char)a;
612
613
614 if(ioctl(ifd->io->fd, IOCTL_SET_PARAMETERS, &params)!=0)
615 {
616 ATR_Delete (*atr);
617 (*atr) = NULL;
618 return IFD_TOWITOKO_IO_ERROR;
619 }
620
621
622/*
623 ioctl(ifd->io->fd, IOCTL_GET_PARAMETERS, &params);
624
625 printf("T=%d\n", (int)params.T);
626 printf("f=%d\n", (int)params.f);
627 printf("ETU=%d\n", (int)params.ETU);
628 printf("WWT=%d\n", (int)params.WWT);
629 printf("CWT=%d\n", (int)params.CWT);
630 printf("BWT=%d\n", (int)params.BWT);
631 printf("EGT=%d\n", (int)params.EGT);
632 printf("clock=%d\n", (int)params.clock_stop_polarity);
633 printf("check=%d\n", (int)params.check);
634 printf("P=%d\n", (int)params.P);
635 printf("I=%d\n", (int)params.I);
636 printf("U=%d\n", (int)params.U);*/
637
638
639
640 req_ts.tv_sec = 0;
641 req_ts.tv_nsec = 50000000;
642 nanosleep (&req_ts, NULL);
643 return IFD_TOWITOKO_OK;
644 }
645 else
646 {
647 ATR_Delete (*atr);
648 (*atr) = NULL;
649 return IFD_TOWITOKO_IO_ERROR;
650 }
651 }
652 else
653#endif
654 {
655 int ret;
656 int parity;
657 int i;
658 int par[3] = {IFD_TOWITOKO_PARITY_EVEN, IFD_TOWITOKO_PARITY_ODD, IFD_TOWITOKO_PARITY_NONE};
659#ifdef HAVE_NANOSLEEP
660 struct timespec req_ts;
661 req_ts.tv_sec = 0;
662 req_ts.tv_nsec = 50000000;
663#endif
664
665 for(i=0; i<3; i++)
666 {
667 parity = par[i];
668 IO_Serial_Flush(ifd->io);
669
670 ret = IFD_Towitoko_SetParity (ifd, parity);
671 if (ret != IFD_TOWITOKO_OK)
672 return ret;
673
674 ret = IFD_TOWITOKO_IO_ERROR;
675
676 IO_Serial_Ioctl_Lock(ifd->io, 1);
677#ifdef USE_GPIO
678 if (gpio_detect)
679 {
680 set_gpio(0);
681 set_gpio1(0);
682 }
683 else
684#endif
685 IO_Serial_RTS_Set(ifd->io);
686
687#ifdef HAVE_NANOSLEEP
688 nanosleep (&req_ts, NULL);
689#else
690 usleep (50000L);
691#endif
692#ifdef USE_GPIO
693 if (gpio_detect)
694 {
695 set_gpio_input();
696 set_gpio1(1);
697 }
698 else
699#endif
700 IO_Serial_RTS_Clr(ifd->io);
701
702 IO_Serial_Ioctl_Lock(ifd->io, 0);
703
704 (*atr) = ATR_New ();
705
706 if(ATR_InitFromStream ((*atr), ifd->io, IFD_TOWITOKO_ATR_TIMEOUT) == ATR_OK)
707 ret = IFD_TOWITOKO_OK;
708
709 /* Succesfully retrive ATR */
710 if (ret == IFD_TOWITOKO_OK)
711 {
712 break;
713 }
714 else
715 {
716 ATR_Delete (*atr);
717 (*atr) = NULL;
718#ifdef USE_GPIO
719 if (gpio_detect) set_gpio1(0);
720#endif
721 }
722 }
723
724 IO_Serial_Flush(ifd->io);
725#ifndef NO_PAR_SWITCH
726 IFD_Towitoko_SetParity (ifd, IFD_TOWITOKO_PARITY_NONE);
727#endif
728 return ret;
729 }
730}
731
732int IFD_Towitoko_Transmit (IFD * ifd, IFD_Timings * timings, unsigned size, BYTE * buffer)
733{
734 unsigned block_delay, char_delay, sent=0, to_send = 0;
735
736#ifdef DEBUG_IFD
737 printf ("IFD: Transmit: ");
738 for (sent = 0; sent < size; sent++)
739 printf ("%X ", buffer[sent]);
740 printf ("\n");
741#endif
742
743
744 /* Calculate delays */
745 char_delay = IFD_TOWITOKO_DELAY + timings->char_delay;
746 block_delay = IFD_TOWITOKO_DELAY + timings->block_delay;
747
748#ifdef USE_GPIO
749 if (gpio_detect) set_gpio1(0);
750#endif
751 for (sent = 0; sent < size; sent = sent + to_send)
752 {
753 /* Calculate number of bytes to send */
754 to_send = MIN(size, IFD_TOWITOKO_MAX_TRANSMIT);
755
756 /* Send data */
757 if ((sent == 0) && (block_delay != char_delay))
758 {
759 if (!IO_Serial_Write (ifd->io, block_delay, 1, buffer))
760 return IFD_TOWITOKO_IO_ERROR;
761
762 if (!IO_Serial_Write (ifd->io, char_delay, to_send-1, buffer+1))
763 return IFD_TOWITOKO_IO_ERROR;
764 }
765 else
766 {
767 if (!IO_Serial_Write (ifd->io, char_delay, to_send, buffer+sent))
768 return IFD_TOWITOKO_IO_ERROR;
769 }
770 }
771#ifdef USE_GPIO
772 if (gpio_detect) set_gpio1(1);
773#endif
774 return IFD_TOWITOKO_OK;
775}
776
777int IFD_Towitoko_Receive (IFD * ifd, IFD_Timings * timings, unsigned size, BYTE * buffer)
778{
779 unsigned char_timeout, block_timeout;
780#ifdef DEBUG_IFD
781 int i;
782#endif
783
784 /* Calculate timeouts */
785 char_timeout = IFD_TOWITOKO_TIMEOUT + timings->char_timeout;
786 block_timeout = IFD_TOWITOKO_TIMEOUT + timings->block_timeout;
787#ifdef USE_GPIO
788 if (gpio_detect) set_gpio1(0);
789#endif
790 if (block_timeout != char_timeout)
791 {
792 /* Read first byte using block timeout */
793 if (!IO_Serial_Read (ifd->io, block_timeout, 1, buffer))
794 return IFD_TOWITOKO_IO_ERROR;
795
796 if (size > 1)
797 {
798 /* Read remaining data bytes using char timeout */
799 if (!IO_Serial_Read (ifd->io, char_timeout, size - 1, buffer + 1))
800 return IFD_TOWITOKO_IO_ERROR;
801 }
802 }
803 else
804 {
805 /* Read all data bytes with the same timeout */
806 if (!IO_Serial_Read (ifd->io, char_timeout, size, buffer))
807 return IFD_TOWITOKO_IO_ERROR;
808 }
809#ifdef USE_GPIO
810 if (gpio_detect) set_gpio1(1);
811#endif
812
813#ifdef DEBUG_IFD
814 printf ("IFD: Receive: ");
815 for (i = 0; i < size; i++)
816 printf ("%X ", buffer[i]);
817 printf ("\n");
818#endif
819
820 return IFD_TOWITOKO_OK;
821}
822
823
824BYTE IFD_Towitoko_GetType (IFD * ifd)
825{
826 return ifd->type;
827}
828
829void IFD_Towitoko_GetDescription (IFD * ifd, BYTE * desc, unsigned length)
830{
831 char buffer[3];
832
833 if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_II)
834 memcpy (desc,"CE2",MIN(length,3));
835
836 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_EXT_I)
837 memcpy (desc,"CE1",MIN(length,3));
838
839 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_INT)
840 memcpy (desc,"CDI",MIN(length,3));
841
842 else if (ifd->type == IFD_TOWITOKO_CHIPDRIVE_MICRO)
843 memcpy (desc,"CDM",MIN(length,3));
844
845 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG_II)
846 memcpy (desc,"KZ2",MIN(length,3));
847
848 else if (ifd->type == IFD_TOWITOKO_KARTENZWERG)
849 memcpy (desc,"KZ1",MIN(length,3));
850
851 else if (ifd->type == IFD_TOWITOKO_MULTICAM)
852 memcpy (desc,"MCM",MIN(length,3));
853
854 else
855 memcpy (desc,"UNK",MIN(length,3));
856
857 snprintf (buffer, 3, "%02X", ifd->firmware);
858
859 if (length > 3)
860 memcpy (desc+3, buffer, MIN(length-3,2));
861}
862
863BYTE
864IFD_Towitoko_GetFirmware (IFD * ifd)
865{
866 return ifd->firmware;
867}
868
869BYTE
870IFD_Towitoko_GetSlot (IFD * ifd)
871{
872 return ifd->slot;
873}
874
875unsigned
876IFD_Towitoko_GetNumSlots (IFD * ifd)
877{
878 return 1;
879}
880
881unsigned long
882IFD_Towitoko_GetClockRate (IFD * ifd)
883{
884 return IFD_TOWITOKO_CLOCK_RATE;
885}
886
887unsigned long
888IFD_Towitoko_GetMaxBaudrate (IFD * ifd)
889{
890 return 115200L;
891}
892
893/*
894 * Not exported funcions definition
895 */
896
897
898static int IFD_Towitoko_GetReaderInfo (IFD * ifd)
899{
900 BYTE status[3];
901
902 status[0] = IFD_TOWITOKO_MULTICAM;
903 status[1] = 0x00;
904
905 ifd->type = status[0];
906 ifd->firmware = status[1];
907
908#ifdef DEBUG_IFD
909 printf ("IFD: Reader type = %s\n",
910 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_II ? "Chipdrive Extern II" :
911 status[0] == IFD_TOWITOKO_CHIPDRIVE_EXT_I ? "Chipdrive Extern I" :
912 status[0] == IFD_TOWITOKO_CHIPDRIVE_INT ? "Chipdrive Intern" :
913 status[0] == IFD_TOWITOKO_CHIPDRIVE_MICRO ? "Chipdrive Micro" :
914 status[0] == IFD_TOWITOKO_KARTENZWERG_II ? "Kartenzwerg II" :
915 status[0] == IFD_TOWITOKO_MULTICAM ? "Multicam" :
916 status[0] == IFD_TOWITOKO_KARTENZWERG ? "Kartenzwerg" : "Unknown");
917#endif
918
919 return IFD_TOWITOKO_OK;
920}
921
922
923static void IFD_Towitoko_Clear (IFD * ifd)
924{
925 ifd->io = NULL;
926 ifd->slot = 0x00;
927 ifd->type = 0x00;
928 ifd->firmware = 0x00;
929 ifd->status = 0;
930}
Note: See TracBrowser for help on using the repository browser.