* implement rfid_reader_{get,set}opt()
[librfid] / src / rfid_reader_cm5121.c
index f2a8b9a..2e9aade 100644 (file)
@@ -1,8 +1,8 @@
 /* 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 AT98C5122 based USB CCID reader (probably the same
+ * 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,
  * not via SPI.
  *
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  */
-
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
 
-#include <rfid/rfid.h>
-#include <rfid/rfid_reader.h>
-#include <rfid/rfid_asic.h>
-#include <rfid/rfid_asic_rc632.h>
-#include <rfid/rfid_reader_cm5121.h>
+#define DEBUG_LIBRFID
+#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 "rfid_reader_rc632_common.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
 
-#if 0
-#ifdef DEBUGP
-#undef DEBUGP
-#define DEBUGP(x, ...)
-#define DEBUGPC(x, ...)
-#endif
+#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
@@ -59,7 +69,7 @@ int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
 {
        unsigned char sndbuf[SENDBUF_LEN];
        unsigned char rcvbuf[RECVBUF_LEN];
-       unsigned int retlen = RECVBUF_LEN;
+       size_t retlen = RECVBUF_LEN;
 
        sndbuf[0] = 0x20;
        sndbuf[1] = 0x00;
@@ -70,15 +80,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;
 }
 
@@ -88,7 +98,7 @@ static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath,
 {
        unsigned char sndbuf[SENDBUF_LEN];
        unsigned char recvbuf[RECVBUF_LEN];
-       unsigned int retlen = sizeof(recvbuf);
+       size_t retlen = sizeof(recvbuf);
 
        sndbuf[0] = 0x20;
        sndbuf[1] = 0x00;
@@ -101,12 +111,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;
 }
 
@@ -116,7 +126,7 @@ static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath,
 {
        unsigned char sndbuf[SENDBUF_LEN];
        unsigned char recvbuf[0x7f];
-       unsigned int retlen = sizeof(recvbuf);
+       size_t retlen = sizeof(recvbuf);
 
        sndbuf[0] = 0x20;
        sndbuf[1] = 0x00;
@@ -126,15 +136,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;
 }
 
@@ -145,7 +155,7 @@ static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath,
 {
        unsigned char sndbuf[SENDBUF_LEN];
        unsigned char recvbuf[0x7f];
-       unsigned int retlen = sizeof(recvbuf);
+       size_t retlen = sizeof(recvbuf);
 
        sndbuf[0] = 0x20;
        sndbuf[1] = 0x00;
@@ -155,120 +165,19 @@ 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;
 }
 
-#if 0
-static int TestFIFO(struct rc632_handle *handle)
-{
-       unsigned char sndbuf[60]; // 0x3c
-
-       // FIXME: repne stosd, call
-
-       memset(sndbuf, 0, sizeof(sndbuf));
-
-       if (WriteNBytesToFIFO(handle, sizeof(sndbuf), sndbuf, 0) < 0)
-               return -1;
-
-       return ReadNBytesFromFIFO(handle, sizeof(sndbuf), sndbuf);
-}
-#endif
-
-static int cm5121_transcieve(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,
-                                               tx_data, tx_len, rx_data,
-                                               rx_len, timeout, flags);
-}
-
-static int cm5121_transcieve_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,
-                                                                  cmd,
-                                                                  atqa);
-}
-
-static int
-cm5121_transcieve_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,
-                                                        cmd, bit_of_col);
-}
-
-static int
-cm5121_14443a_init(struct rfid_reader_handle *rh)
-{
-       return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
-}
-
-static int
-cm5121_14443a_set_speed(struct rfid_reader_handle *rh, unsigned int speed)
-{
-       u_int8_t rate;
-       
-       switch (speed) {
-       case RFID_14443A_SPEED_106K:
-               rate = RC632_CDRCTRL_RATE_106K;
-               break;
-       case RFID_14443A_SPEED_212K:
-               rate = RC632_CDRCTRL_RATE_212K;
-               break;
-       case RFID_14443A_SPEED_424K:
-               rate = RC632_CDRCTRL_RATE_424K;
-               break;
-       case RFID_14443A_SPEED_848K:
-               rate = RC632_CDRCTRL_RATE_848K;
-               break;
-       default:
-               return -EINVAL;
-               break;
-       }
-       return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah, rate);
-}
-
-static int
-cm5121_14443b_init(struct rfid_reader_handle *rh)
-{
-       return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
-}
-
-static int
-cm5121_15693_init(struct rfid_reader_handle *rh)
-{
-       return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
-}
-
-static int
-cm5121_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
-{
-       return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
-}
-
-static int
-cm5121_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
-                  u_int32_t serno, u_int8_t block)
-{
-       return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah, 
-                                                       cmd, serno, block);
-}
-
 struct rfid_asic_transport cm5121_ccid = {
        .name = "CM5121 OpenCT",
        .priv.rc632 = {
@@ -285,10 +194,9 @@ static int cm5121_enable_rc632(struct rfid_asic_transport_handle *rath)
 {
        unsigned char tx_buf[1] = { 0x01 };     
        unsigned char rx_buf[64];
-       unsigned int rx_len = sizeof(rx_buf);
+       size_t rx_len = sizeof(rx_buf);
 
        PC_to_RDR_Escape(rath->data, tx_buf, 1, rx_buf, &rx_len);
-       printf("received %u bytes from 01 command\n", rx_len);
 
        return 0;
 }
@@ -299,12 +207,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 +234,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,30 +246,38 @@ 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,
+       .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),
+       .getopt = &_rdr_rc632_getopt,
+       .setopt = &_rdr_rc632_setopt,
+       .transceive = &_rdr_rc632_transceive,
+       .init = &_rdr_rc632_l2_init,
        .iso14443a = {
-               .init = &cm5121_14443a_init,
-               .transcieve_sf = &cm5121_transcieve_sf,
-               .transcieve_acf = &cm5121_transcieve_acf,
+               .transceive_sf = &_rdr_rc632_transceive_sf,
+               .transceive_acf = &_rdr_rc632_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,
+                        RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
+               .set_speed = &_rdr_rc632_14443a_set_speed,
        },
-       .iso14443b = {
-               .init = &cm5121_14443b_init,
+       .iso15693 = {
+               .transceive_ac = &_rdr_rc632_iso15693_transceive_ac,
        },
        .mifare_classic = {
-               .setkey = &cm5121_mifare_setkey,
-               .auth = &cm5121_mifare_auth,
+               .setkey = &_rdr_rc632_mifare_setkey,
+               .auth = &_rdr_rc632_mifare_auth,
        },
 };
 
-
+#endif /* LIBRFID_FIRMWARE */