- separate 'STATIC' (non-dyamic-allocation) from 'FIRMWARE' (no host drivers)
[librfid] / src / rfid_reader_cm5121.c
index 697ac64..a62cc4c 100644 (file)
@@ -1,6 +1,6 @@
 /* Omnikey CardMan 5121 specific RC632 transport layer 
  *
- * (C) 2005 by Harald Welte <laforge@gnumonks.org>
+ * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
  *
  * The 5121 is an Atmel AT89C5122 based USB CCID reader (probably the same
  * design like the 3121).  It's CL RC632 is connected via address/data bus,
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
-
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
 
 #include <librfid/rfid.h>
+
+#ifndef LIBRFID_FIRMWARE
+
 #include <librfid/rfid_reader.h>
 #include <librfid/rfid_asic.h>
 #include <librfid/rfid_asic_rc632.h>
 #include <librfid/rfid_reader_cm5121.h>
+#include <librfid/rfid_layer2.h>
+#include <librfid/rfid_protocol.h>
+
+#include "cm5121_source.h"
 
 /* FIXME */
 #include "rc632.h"
 
-//#define SENDBUF_LEN  40
-#define SENDBUF_LEN    100
-#define RECVBUF_LEN    40
+#define SENDBUF_LEN    256+7+10 /* 256bytes max FSD/FSC, plus 7 bytes header,
+                                   plus 10 bytes reserve */
+#define RECVBUF_LEN    SENDBUF_LEN
+
+//#define DEBUG_REGISTER
+
+#ifdef DEBUG_REGISTER
+#define DEBUGRC DEBUGPC
+#define DEBUGR DEBUGP
+#else
+#define DEBUGRC(x, args ...)   do {} while(0)
+#define DEBUGR(x, args ...)    do {} while(0)
+#endif
 
 static
 int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
@@ -62,15 +78,15 @@ int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
        sndbuf[6] = reg;
        sndbuf[7] = value;
 
-       DEBUGP("reg=0x%02x, val=%02x: ", reg, value);
+       DEBUGR("reg=0x%02x, val=%02x: ", reg, value);
 
        if (PC_to_RDR_Escape(rath->data, sndbuf, 8, rcvbuf, 
                             &retlen) == 0) {
-               DEBUGPC("OK\n");
+               DEBUGRC("OK\n");
                return 0;
        }
 
-       DEBUGPC("ERROR\n");
+       DEBUGRC("ERROR\n");
        return -1;
 }
 
@@ -93,12 +109,12 @@ static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath,
        if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, 
                             &retlen) == 0) {
                *value = recvbuf[1];
-               DEBUGP("reg=0x%02x, val=%02x: ", reg, *value);
-               DEBUGPC("OK\n");
+               DEBUGR("reg=0x%02x, val=%02x: ", reg, *value);
+               DEBUGRC("OK\n");
                return 0;
        }
 
-       DEBUGPC("ERROR\n");
+       DEBUGRC("ERROR\n");
        return -1;
 }
 
@@ -118,15 +134,15 @@ static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath,
        sndbuf[5] = 0x00;
        sndbuf[6] = 0x02;
 
-       DEBUGP("num_bytes=%u: ", num_bytes);
+       DEBUGR("num_bytes=%u: ", num_bytes);
        if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, &retlen) == 0) {
-               DEBUGPC("%u [%s]\n", retlen,
+               DEBUGRC("%u [%s]\n", retlen,
                        rfid_hexdump(recvbuf+1, num_bytes));
                memcpy(buf, recvbuf+1, num_bytes); // len == 0x7f
                return 0;
        }
 
-       DEBUGPC("ERROR\n");
+       DEBUGRC("ERROR\n");
        return -1;
 }
 
@@ -147,16 +163,16 @@ static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath,
        sndbuf[5] = flags;
        sndbuf[6] = 0x02;
 
-       DEBUGP("%u [%s]: ", len, rfid_hexdump(bytes, len));
+       DEBUGR("%u [%s]: ", len, rfid_hexdump(bytes, len));
 
        memcpy(sndbuf+7, bytes, len);
 
        if (PC_to_RDR_Escape(rath->data, sndbuf, len+7, recvbuf, &retlen) == 0) {
-               DEBUGPC("OK (%u [%s])\n", retlen, rfid_hexdump(recvbuf, retlen));
+               DEBUGRC("OK (%u [%s])\n", retlen, rfid_hexdump(recvbuf, retlen));
                return 0;
        }
 
-       DEBUGPC("ERROR\n");
+       DEBUGRC("ERROR\n");
        return -1;
 }
 
@@ -176,31 +192,31 @@ static int TestFIFO(struct rc632_handle *handle)
 }
 #endif
 
-static int cm5121_transcieve(struct rfid_reader_handle *rh,
+static int cm5121_transceive(struct rfid_reader_handle *rh,
                             enum rfid_frametype frametype,
                             const unsigned char *tx_data, unsigned int tx_len,
                             unsigned char *rx_data, unsigned int *rx_len,
                             u_int64_t timeout, unsigned int flags)
 {
-       return rh->ah->asic->priv.rc632.fn.transcieve(rh->ah, frametype,
+       return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
                                                tx_data, tx_len, rx_data,
                                                rx_len, timeout, flags);
 }
 
-static int cm5121_transcieve_sf(struct rfid_reader_handle *rh,
+static int cm5121_transceive_sf(struct rfid_reader_handle *rh,
                               unsigned char cmd, struct iso14443a_atqa *atqa)
 {
-       return rh->ah->asic->priv.rc632.fn.iso14443a.transcieve_sf(rh->ah,
+       return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
                                                                   cmd,
                                                                   atqa);
 }
 
 static int
-cm5121_transcieve_acf(struct rfid_reader_handle *rh,
+cm5121_transceive_acf(struct rfid_reader_handle *rh,
                      struct iso14443a_anticol_cmd *cmd,
                      unsigned int *bit_of_col)
 {
-       return rh->ah->asic->priv.rc632.fn.iso14443a.transcieve_acf(rh->ah,
+       return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
                                                         cmd, bit_of_col);
 }
 
@@ -236,6 +252,7 @@ cm5121_14443a_set_speed(struct rfid_reader_handle *rh,
                DEBUGPC("848K\n");
                break;
        default:
+               DEBUGPC("invalid\n");
                return -EINVAL;
                break;
        }
@@ -299,12 +316,12 @@ cm5121_open(void *data)
        struct rfid_reader_handle *rh;
        struct rfid_asic_transport_handle *rath;
 
-       rh = malloc(sizeof(*rh));
+       rh = malloc_reader_handle(sizeof(*rh));
        if (!rh)
                return NULL;
        memset(rh, 0, sizeof(*rh));
 
-       rath = malloc(sizeof(*rath));
+       rath = malloc_rat_handle(sizeof(*rath));
        if (!rath)
                goto out_rh;
        memset(rath, 0, sizeof(*rath));
@@ -326,9 +343,9 @@ cm5121_open(void *data)
        return rh;
 
 out_rath:
-       free(rath);
+       free_rat_handle(rath);
 out_rh:
-       free(rh);
+       free_reader_handle(rh);
 
        return NULL;
 }
@@ -338,19 +355,25 @@ cm5121_close(struct rfid_reader_handle *rh)
 {
        struct rfid_asic_transport_handle *rath = rh->ah->rath;
        rc632_close(rh->ah);
-       free(rath);
-       free(rh);
+       free_rat_handle(rath);
+       free_reader_handle(rh);
 }
 
-struct rfid_reader rfid_reader_cm5121 = {
+const struct rfid_reader rfid_reader_cm5121 = {
        .name   = "Omnikey CardMan 5121 RFID",
        .open = &cm5121_open,
        .close = &cm5121_close,
-       .transcieve = &cm5121_transcieve,
+       .transceive = &cm5121_transceive,
+       .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
+                       (1 << RFID_LAYER2_ISO14443B) |
+                       (1 << RFID_LAYER2_ISO15693),
+       .proto_supported = (1 << RFID_PROTOCOL_TCL) |
+                       (1 << RFID_PROTOCOL_MIFARE_UL) |
+                       (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
        .iso14443a = {
                .init = &cm5121_14443a_init,
-               .transcieve_sf = &cm5121_transcieve_sf,
-               .transcieve_acf = &cm5121_transcieve_acf,
+               .transceive_sf = &cm5121_transceive_sf,
+               .transceive_acf = &cm5121_transceive_acf,
                .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
                         RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
                .set_speed = &cm5121_14443a_set_speed,
@@ -358,10 +381,13 @@ struct rfid_reader rfid_reader_cm5121 = {
        .iso14443b = {
                .init = &cm5121_14443b_init,
        },
+       .iso15693 = {
+               .init = &cm5121_15693_init,
+       },
        .mifare_classic = {
                .setkey = &cm5121_mifare_setkey,
                .auth = &cm5121_mifare_auth,
        },
 };
 
-
+#endif /* LIBRFID_FIRMWARE */