--- ct-api/atr.c-	Sat Aug  7 08:38:25 1999
+++ ct-api/atr.c	Wed Mar 22 09:23:45 2000
@@ -14,8 +14,16 @@
  *
  * LICENSE:
  *      See file COPYING
+ *
+ * File modified by Jim Rees December 1999
  */
 
+#ifdef __palmos__
+#include <stdlib.h>
+#include <Common.h>
+#include <SystemMgr.h>
+#include <string.h>
+#endif
 #include "atr.h"
 
 /*
--- ct-api/ctapi.c-	Sun Aug  8 08:39:24 1999
+++ ct-api/ctapi.c	Tue Oct  2 12:31:45 2001
@@ -14,9 +14,17 @@
  * LICENSE:
  *      See file COPYING
  *
+ * File modified by Jim Rees December 1999
+ *
  */
 
 #include <stdio.h>
+#ifdef __palmos__
+#include <stdlib.h>
+#include <Common.h>
+#include <SystemMgr.h>
+#include <string.h>
+#endif
 #include "defines.h"
 #include "ctapi.h"
 #include "serial.h"
@@ -72,7 +80,7 @@
       #endif
 
       #ifndef OS_SOLARIS
-      if(!IO_InitializePort(9600,8,'E',"/dev/ttyS0"))
+      if(!IO_InitializePort(9600,8,'E',"/dev/cua00"))
         return(ERR_MEMORY);
       #endif
 
@@ -86,7 +94,7 @@
       #endif
 
       #ifndef OS_SOLARIS
-      if(!IO_InitializePort(9600,8,'E',"/dev/ttyS1"))
+      if(!IO_InitializePort(9600,8,'E',"/dev/cua01"))
         return(ERR_MEMORY);
       #endif
 
@@ -129,8 +137,10 @@
   }
 
   /* Determine reader type */
-  if(!Towitoko_GetReaderType(&type))
+  if(!Towitoko_GetReaderType(&type)) {
+    IO_Close();
     return(ERR_TRANS);
+  }
 
   #ifdef DEBUG
   printf("---- Reader type: 0x%X\n",type);
@@ -276,7 +286,7 @@
   
    } else if(protocol==ATR_PROTOCOL_T1) 
 
-      /* Sends a T=O TPDU and gets Card TPDU response */
+      /* Sends a T=1 TPDU and gets Card TPDU response */
       return(T1_Command(lc,cmd,lr,rsp));
 
     else {
@@ -833,11 +843,22 @@
  *      CT-API operation status code.
  */
 
-static int T1_Command(int lc, BYTE* cmd, int* lr, BYTE*rsp) {
+int scioT1(int ttyn, int cla, int ins, int p1, int p2, int ilen, unsigned char *ibuf, int olen, unsigned char *obuf, int *sw1p, int *sw2p);
 
-  /*
-   * CODE FOR T=1 PROTOCOL SMARTCARDS
-   * Not implemented
-   */
-  return(OK);
+static int T1_Command(int lc, BYTE* cmd, int* lr, BYTE*rsp)
+{
+#ifndef NO_T_EQ_1
+  int n, r1, r2;
+
+  n = scioT1(0, cmd[0], cmd[1], cmd[2], cmd[3], lc - 5, &cmd[5], *lr, rsp, &r1, &r2);
+  if (n < 0)
+    return ERR_TRANS;
+#ifdef DEBUG
+  printf("scioT1 %d r1 %x r2 %x\n", n, r1, r2);
+#endif
+  rsp[n++] = r1;
+  rsp[n++] = r2;
+  *lr = n;
+#endif
+  return OK;
 }
--- ct-api/main.c-	Fri Jun 11 18:58:28 1999
+++ ct-api/main.c	Wed Mar 22 09:23:45 2000
@@ -26,7 +26,7 @@
   int i;
   int Iret;
   
-  CT_init(1,6000);
+  CT_init(1, 0);
 
   Iret = CT_data(1,&dad,&sad,2,reset,&lr,Brsp);
   
--- ct-api/scT1.c-	Thu Jun  7 15:49:49 2001
+++ ct-api/scT1.c	Fri Jul 27 11:33:58 2001
@@ -1,9 +1,40 @@
+/* $Id: scT1.c,v 1.8 2001/07/27 15:13:56 rees Exp $ */
+
+/*
+copyright 1997, 1999, 2000
+the regents of the university of michigan
+all rights reserved
+
+permission is granted to use, copy, create derivative works 
+and redistribute this software and such derivative works 
+for any purpose, so long as the name of the university of 
+michigan is not used in any advertising or publicity 
+pertaining to the use or distribution of this software 
+without specific, written prior authorization.  if the 
+above copyright notice or any other identification of the 
+university of michigan is included in any copy of any 
+portion of this software, then the disclaimer below must 
+also be included.
+
+this software is provided as is, without representation 
+from the university of michigan as to its fitness for any 
+purpose, and without warranty by the university of 
+michigan of any kind, either express or implied, including 
+without limitation the implied warranties of 
+merchantability and fitness for a particular purpose. the 
+regents of the university of michigan shall not be liable 
+for any damages, including special, indirect, incidental, or 
+consequential damages, with respect to any claim arising 
+out of or in connection with the use of the software, even 
+if it has been or is hereafter advised of the possibility of 
+such damages.
+*/
+
 /*
  * T=1 protocol engine
  *
  * Jim Rees, University of Michigan, October 1997
  */
-static char *rcsid = "$Id: scT1.c,v 1.5 2000/03/22 02:52:19 rees Exp $";
 
 #ifdef __palmos__
 #include <Common.h>
@@ -20,6 +51,7 @@
 #endif
 
 #include "sectok.h"
+#include "sc7816.h"
 
 #ifdef __palmos__
 #undef printf
@@ -146,33 +178,3 @@
 
     return len;
 }
-
-/*
-copyright 1997, 1999, 2000
-the regents of the university of michigan
-all rights reserved
-
-permission is granted to use, copy, create derivative works 
-and redistribute this software and such derivative works 
-for any purpose, so long as the name of the university of 
-michigan is not used in any advertising or publicity 
-pertaining to the use or distribution of this software 
-without specific, written prior authorization.  if the 
-above copyright notice or any other identification of the 
-university of michigan is included in any copy of any 
-portion of this software, then the disclaimer below must 
-also be included.
-
-this software is provided as is, without representation 
-from the university of michigan as to its fitness for any 
-purpose, and without warranty by the university of 
-michigan of any kind, either express or implied, including 
-without limitation the implied warranties of 
-merchantability and fitness for a particular purpose. the 
-regents of the university of michigan shall not be liable 
-for any damages, including special, indirect, incidental, or 
-consequential damages, with respect to any claim arising 
-out of or in connection with the use of the software, even 
-if it has been or is hereafter advised of the possibility of 
-such damages.
-*/
--- ct-api/serial.c-	Tue Sep 12 17:10:55 2000
+++ ct-api/serial.c	Tue Sep 12 17:11:33 2000
@@ -199,7 +199,8 @@
   ioport.bits   = bits;                     
   ioport.stopbits  = 2;
   ioport.parity = parity;
-  ioport.blocktime = 1;
+  ioport.blocktime = 4;
+  
 	
   return TRUE;
 }
--- ct-api/towitoko.c-	Sun Aug  8 08:33:31 1999
+++ ct-api/towitoko.c	Tue Jul 25 18:46:49 2000
@@ -13,9 +13,17 @@
  *
  * LICENSE:
  *      See file COPYING
+ *
+ * File modified by Jim Rees December 1999
  */
 
 #include <stdio.h>
+#ifdef __palmos__
+#include <stdlib.h>
+#include <Common.h>
+#include <SystemMgr.h>
+#include <string.h>
+#endif
 #include "towitoko.h"
 #include "serial.h"
 
@@ -23,7 +31,7 @@
  * Internal Size of the commands and responses
  */
 
-#define MAX_WRITE_SIZE 15
+#define MAX_WRITE_SIZE 31
 #define MAX_READ_SIZE 255
 
 /*
@@ -120,7 +128,10 @@
     // Send command
     if(!IO_Write(card_bytes+4,command))
       return FALSE;
-    
+#ifdef __palmos__
+    scsleep(200 + card_bytes * 8);
+#endif
+
     // Increment pointer
     pointer+=card_bytes;
   }
@@ -267,6 +278,7 @@
   BYTE TDi;
   int i;
   bool loop; 
+  int hiproto = 0;
 
   // First sequence
   buffer1[0] = 0x80;
@@ -386,6 +398,7 @@
         if(!IO_Read(1,status))
           return(FALSE);
         TDi = Atr[(*Length)++] = status[0];
+	hiproto += TDi & 0xf;
         loop = TRUE;
       } 
 
@@ -397,6 +410,10 @@
       return(FALSE);
     memcpy(Atr+(*Length),status,Atr[1]&0x0F);
     (*Length) += Atr[1]&0x0F;
+
+    // Read checksum if present
+    if (hiproto > 0 && IO_Read(1,status))
+      Atr[(*Length)++] = status[0];
 
     #ifdef DEBUG
     printf("---- ATR: ");
--- ifd-handler/IFD_Handler.c-	Fri Jun 11 18:58:28 1999
+++ ifd-handler/IFD_Handler.c	Tue Sep 12 12:23:47 2000
@@ -123,7 +123,7 @@
 
 RESPONSECODE IFD_Get_Capabilities ( DWORD Tag, BYTE Value[] ) {
 
-  RESPONSECODE lRetVal;
+  RESPONSECODE lRetVal = IFD_NOT_SUPPORTED;
   DWORD HighNibble;
   DWORD LowNibble;
   int i;
@@ -146,13 +146,7 @@
       case 0x00:
 	break;
       case 0x01:
-
-        if ( IFD_Is_ICC_Present() == IFD_SUCCESS ) {
-          *(DWORD*)Value = IFD_ICC_PRESENT;
-        } else {
-          *(DWORD*)Value = IFD_ICC_NOT_PRESENT;
-        }
-
+	*(DWORD *) Value = IFD_Is_ICC_Present();
         lRetVal = IFD_SUCCESS;  
 	break;
       case 0x03:
@@ -173,7 +167,7 @@
 
 RESPONSECODE IFD_Set_Capabilities ( DWORD Tag, BYTE Value[] ) {
 
-  RESPONSECODE lRetVal;
+  RESPONSECODE lRetVal = IFD_NOT_SUPPORTED;
   DWORD HighNibble;
   DWORD LowNibble;
 
@@ -235,7 +229,7 @@
   dad = 1;
 
   lc = 5;
-  lr = 2;
+  lr = MAX_RESPONSE_SIZE;
 
   if ( ActionRequested == IFD_POWER_UP ) {
     
