From 889d81a57277d2b498cd27b454a7be17cc78a294 Mon Sep 17 00:00:00 2001 From: hackbard Date: Sun, 22 Jan 2006 23:12:47 +0000 Subject: [PATCH] sed s/cm5121/x501/ - need to be hacked now (hopefully there is a way to access registers and fifo of rc632) --- include/librfid/rfid.h | 2 +- include/librfid/rfid_reader.h | 1 + include/librfid/rfid_reader_x501.h | 21 ++ src/Makefile.am | 2 +- src/rfid.c | 18 +- src/rfid_reader_x501.c | 359 +++++++++++++++++++++++++++++ 6 files changed, 399 insertions(+), 4 deletions(-) create mode 100644 include/librfid/rfid_reader_x501.h create mode 100644 src/rfid_reader_x501.c diff --git a/include/librfid/rfid.h b/include/librfid/rfid.h index 76e86e3..a2e8bf7 100644 --- a/include/librfid/rfid.h +++ b/include/librfid/rfid.h @@ -27,6 +27,6 @@ extern const char *rfid_hexdump(const void *data, unsigned int len); #endif /* __LIBRFID__ */ -int rfid_init(); +int rfid_init(unsigned int id); #endif /* _RFID_H */ diff --git a/include/librfid/rfid_reader.h b/include/librfid/rfid_reader.h index 3581cc1..7bff46d 100644 --- a/include/librfid/rfid_reader.h +++ b/include/librfid/rfid_reader.h @@ -47,6 +47,7 @@ struct rfid_reader { enum rfid_reader_id { RFID_READER_CM5121, + RFID_READER_X501, RFID_READER_PEGODA, }; diff --git a/include/librfid/rfid_reader_x501.h b/include/librfid/rfid_reader_x501.h new file mode 100644 index 0000000..a715469 --- /dev/null +++ b/include/librfid/rfid_reader_x501.h @@ -0,0 +1,21 @@ +#ifndef _RFID_READER_X501_H +#define _RFID_READER_X501_H + +#define X501_CONDUCTANCE 0x3f +#define X501_MOD_CONDUCTANCE 0x3f +#define X501_14443A_BITPHASE 0xa9 +#define X501_14443A_THRESHOLD 0xff + +#define X501_14443B_BITPHASE 0xad +#define X501_14443B_THRESHOLD 0xff + + +extern int +PC_to_RDR_Escape(void *handle, + const unsigned char *tx_buf, unsigned int tx_len, + unsigned char *rx_buf, unsigned int *rx_len); + +extern struct rfid_reader rfid_reader_x501; +// extern struct rfid_asic_transport x501_ccid; + +#endif diff --git a/src/Makefile.am b/src/Makefile.am index dbc608f..81bf468 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,7 +6,7 @@ lib_LTLIBRARIES = librfid.la CORE=rfid.c rfid_layer2.c rfid_protocol.c rfid_reader.c L2=rfid_layer2_iso14443a.c rfid_layer2_iso14443b.c rfid_layer2_iso15693.c rfid_iso14443_common.c PROTO=rfid_proto_tcl.c rfid_proto_mifare_ul.c rfid_proto_mifare_classic.c -READER=rfid_reader_cm5121.c rfid_reader_cm5121_openct.c rfid_asic_rc632.c +READER=rfid_reader_cm5121.c rfid_reader_cm5121_openct.c rfid_reader_x501.c rfid_reader_x501_openct.c rfid_asic_rc632.c librfid_la_LDFLAGS = -Wc,-nostartfiles -version-info $(LIBVERSION) -lopenct librfid_la_SOURCES = $(CORE) $(L2) $(PROTO) $(READER) diff --git a/src/rfid.c b/src/rfid.c index 6fba46a..9f60bab 100644 --- a/src/rfid.c +++ b/src/rfid.c @@ -38,9 +38,23 @@ rfid_hexdump(const void *data, unsigned int len) return string; } -int rfid_init() +int rfid_init(unsigned int id) { - rfid_reader_register(&rfid_reader_cm5121); + swicth(id) { + case RFID_READER_CM5121: + rfid_reader_register(&rfid_reader_cm5121); + break; + case RFID_READER_X501: + rfid_reader_register(&rfid_reader_x501); + break; + /* + case RFID_READER_PEGODA: + rfid_reader_register(&rfid_reader_pegoda); + break; + */ + default: + return -EINVAL; + } rfid_layer2_register(&rfid_layer2_iso14443a); rfid_layer2_register(&rfid_layer2_iso14443b); rfid_protocol_register(&rfid_protocol_tcl); diff --git a/src/rfid_reader_x501.c b/src/rfid_reader_x501.c new file mode 100644 index 0000000..4547642 --- /dev/null +++ b/src/rfid_reader_x501.c @@ -0,0 +1,359 @@ +/* Gemini 2000 x501 specific RC632 transport layer + * + * (C) 2006 by Frank Zirkelbach + * + * Are there extensions allowing to access registers and FIFO of the rc632? + * + */ + +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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 + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "rc632.h" + +#define SENDBUF_LEN 100 +#define RECVBUF_LEN 40 + +static +int Write1ByteToReg(struct rfid_asic_transport_handle *rath, + unsigned char reg, unsigned char value) +{ + unsigned char sndbuf[SENDBUF_LEN]; + unsigned char rcvbuf[RECVBUF_LEN]; + unsigned int retlen = RECVBUF_LEN; + + sndbuf[0] = 0x20; + sndbuf[1] = 0x00; + sndbuf[2] = 0x01; + sndbuf[3] = 0x00; + sndbuf[4] = 0x00; + sndbuf[5] = 0x00; + sndbuf[6] = reg; + sndbuf[7] = value; + + DEBUGP("reg=0x%02x, val=%02x: ", reg, value); + + if (PC_to_RDR_Escape(rath->data, sndbuf, 8, rcvbuf, + &retlen) == 0) { + DEBUGPC("OK\n"); + return 0; + } + + DEBUGPC("ERROR\n"); + return -1; +} + +static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath, + unsigned char reg, + unsigned char *value) +{ + unsigned char sndbuf[SENDBUF_LEN]; + unsigned char recvbuf[RECVBUF_LEN]; + unsigned int retlen = sizeof(recvbuf); + + sndbuf[0] = 0x20; + sndbuf[1] = 0x00; + sndbuf[2] = 0x00; + sndbuf[3] = 0x00; + sndbuf[4] = 0x01; + sndbuf[5] = 0x00; + sndbuf[6] = reg; + + 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"); + return 0; + } + + DEBUGPC("ERROR\n"); + return -1; +} + +static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath, + unsigned char num_bytes, + unsigned char *buf) +{ + unsigned char sndbuf[SENDBUF_LEN]; + unsigned char recvbuf[0x7f]; + unsigned int retlen = sizeof(recvbuf); + + sndbuf[0] = 0x20; + sndbuf[1] = 0x00; + sndbuf[2] = 0x00; + sndbuf[3] = 0x00; + sndbuf[4] = num_bytes; + sndbuf[5] = 0x00; + sndbuf[6] = 0x02; + + DEBUGP("num_bytes=%u: ", num_bytes); + if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, &retlen) == 0) { + DEBUGPC("%u [%s]\n", retlen, + rfid_hexdump(recvbuf+1, num_bytes)); + memcpy(buf, recvbuf+1, num_bytes); // len == 0x7f + return 0; + } + + DEBUGPC("ERROR\n"); + return -1; +} + +static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath, + unsigned char len, + const unsigned char *bytes, + unsigned char flags) +{ + unsigned char sndbuf[SENDBUF_LEN]; + unsigned char recvbuf[0x7f]; + unsigned int retlen = sizeof(recvbuf); + + sndbuf[0] = 0x20; + sndbuf[1] = 0x00; + sndbuf[2] = len; + sndbuf[3] = 0x00; + sndbuf[4] = 0x00; + sndbuf[5] = flags; + sndbuf[6] = 0x02; + + DEBUGP("%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)); + return 0; + } + + DEBUGPC("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 x501_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 x501_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 +x501_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 +x501_14443a_init(struct rfid_reader_handle *rh) +{ + return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah); +} + +static int +x501_14443a_set_speed(struct rfid_reader_handle *rh, + unsigned int tx, + unsigned int speed) +{ + u_int8_t rate; + + DEBUGP("setting rate: "); + switch (speed) { + case RFID_14443A_SPEED_106K: + rate = 0x00; + DEBUGPC("106K\n"); + break; + case RFID_14443A_SPEED_212K: + rate = 0x01; + DEBUGPC("212K\n"); + break; + case RFID_14443A_SPEED_424K: + rate = 0x02; + DEBUGPC("424K\n"); + break; + case RFID_14443A_SPEED_848K: + rate = 0x03; + DEBUGPC("848K\n"); + break; + default: + return -EINVAL; + break; + } + return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah, + tx, rate); +} + +static int +x501_14443b_init(struct rfid_reader_handle *rh) +{ + return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah); +} + +static int +x501_15693_init(struct rfid_reader_handle *rh) +{ + return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah); +} + +static int +x501_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 +x501_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 x501_ccid = { + .name = "X501 OpenCT", + .priv.rc632 = { + .fn = { + .reg_write = &Write1ByteToReg, + .reg_read = &Read1ByteFromReg, + .fifo_write = &WriteNBytesToFIFO, + .fifo_read = &ReadNBytesFromFIFO, + }, + }, +}; + +static int x501_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); + + 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; +} + +static struct rfid_reader_handle * +x501_open(void *data) +{ + struct rfid_reader_handle *rh; + struct rfid_asic_transport_handle *rath; + + rh = malloc(sizeof(*rh)); + if (!rh) + return NULL; + memset(rh, 0, sizeof(*rh)); + + rath = malloc(sizeof(*rath)); + if (!rath) + goto out_rh; + memset(rath, 0, sizeof(*rath)); + + rath->rat = &x501_ccid; + rh->reader = &rfid_reader_x501; + + if (x501_source_init(rath) < 0) + goto out_rath; + + if (x501_enable_rc632(rath) < 0) + goto out_rath; + + rh->ah = rc632_open(rath); + if (!rh->ah) + goto out_rath; + + DEBUGP("returning %p\n", rh); + return rh; + +out_rath: + free(rath); +out_rh: + free(rh); + + return NULL; +} + +static void +x501_close(struct rfid_reader_handle *rh) +{ + struct rfid_asic_transport_handle *rath = rh->ah->rath; + rc632_close(rh->ah); + free(rath); + free(rh); +} + +struct rfid_reader rfid_reader_x501 = { + .name = "Omnikey CardMan 5121 RFID", + .open = &x501_open, + .close = &x501_close, + .transcieve = &x501_transcieve, + .iso14443a = { + .init = &x501_14443a_init, + .transcieve_sf = &x501_transcieve_sf, + .transcieve_acf = &x501_transcieve_acf, + .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K | + RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K, + .set_speed = &x501_14443a_set_speed, + }, + .iso14443b = { + .init = &x501_14443b_init, + }, + .mifare_classic = { + .setkey = &x501_mifare_setkey, + .auth = &x501_mifare_auth, + }, +}; + + -- 2.39.2