1 /* Omnikey CardMan 5121 specific RC632 transport layer
3 * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
5 * The 5121 is an Atmel AT89C5122 based USB CCID reader (probably the same
6 * design like the 3121). It's CL RC632 is connected via address/data bus,
9 * The vendor-supplied reader firmware provides some undocumented extensions
10 * to CCID (via PC_to_RDR_Escape) that allow access to registers and FIFO of
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2
18 * as published by the Free Software Foundation
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
35 #include <librfid/rfid.h>
37 #ifndef LIBRFID_FIRMWARE
40 #include <librfid/rfid_reader.h>
41 #include <librfid/rfid_asic.h>
42 #include <librfid/rfid_asic_rc632.h>
43 #include <librfid/rfid_reader_cm5121.h>
44 #include <librfid/rfid_layer2.h>
45 #include <librfid/rfid_protocol.h>
47 #include "rfid_reader_rc632_common.h"
49 #include "cm5121_source.h"
54 #define SENDBUF_LEN 256+7+10 /* 256bytes max FSD/FSC, plus 7 bytes header,
55 plus 10 bytes reserve */
56 #define RECVBUF_LEN SENDBUF_LEN
59 #define DEBUGRC DEBUGPC
62 #define DEBUGRC(x, args ...) do {} while(0)
63 #define DEBUGR(x, args ...) do {} while(0)
67 int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
68 unsigned char reg, unsigned char value)
70 unsigned char sndbuf[SENDBUF_LEN];
71 unsigned char rcvbuf[RECVBUF_LEN];
72 size_t retlen = RECVBUF_LEN;
83 DEBUGR("reg=0x%02x, val=%02x: ", reg, value);
85 if (PC_to_RDR_Escape(rath->data, sndbuf, 8, rcvbuf,
95 static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath,
99 unsigned char sndbuf[SENDBUF_LEN];
100 unsigned char recvbuf[RECVBUF_LEN];
101 size_t retlen = sizeof(recvbuf);
111 if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf,
114 DEBUGR("reg=0x%02x, val=%02x: ", reg, *value);
123 static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath,
124 unsigned char num_bytes,
127 unsigned char sndbuf[SENDBUF_LEN];
128 unsigned char recvbuf[0x7f];
129 size_t retlen = sizeof(recvbuf);
135 sndbuf[4] = num_bytes;
139 DEBUGR("num_bytes=%u: ", num_bytes);
140 if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, &retlen) == 0) {
141 DEBUGRC("%u [%s]\n", retlen,
142 rfid_hexdump(recvbuf+1, num_bytes));
143 memcpy(buf, recvbuf+1, num_bytes); // len == 0x7f
151 static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath,
153 const unsigned char *bytes,
156 unsigned char sndbuf[SENDBUF_LEN];
157 unsigned char recvbuf[0x7f];
158 size_t retlen = sizeof(recvbuf);
168 DEBUGR("%u [%s]: ", len, rfid_hexdump(bytes, len));
170 memcpy(sndbuf+7, bytes, len);
172 if (PC_to_RDR_Escape(rath->data, sndbuf, len+7, recvbuf, &retlen) == 0) {
173 DEBUGRC("OK (%u [%s])\n", retlen, rfid_hexdump(recvbuf, retlen));
181 struct rfid_asic_transport cm5121_ccid = {
182 .name = "CM5121 OpenCT",
185 .reg_write = &Write1ByteToReg,
186 .reg_read = &Read1ByteFromReg,
187 .fifo_write = &WriteNBytesToFIFO,
188 .fifo_read = &ReadNBytesFromFIFO,
193 static int cm5121_enable_rc632(struct rfid_asic_transport_handle *rath)
195 unsigned char tx_buf[1] = { 0x01 };
196 unsigned char rx_buf[64];
197 size_t rx_len = sizeof(rx_buf);
199 PC_to_RDR_Escape(rath->data, tx_buf, 1, rx_buf, &rx_len);
204 static struct rfid_reader_handle *
205 cm5121_open(void *data)
207 struct rfid_reader_handle *rh;
208 struct rfid_asic_transport_handle *rath;
210 rh = malloc_reader_handle(sizeof(*rh));
213 memset(rh, 0, sizeof(*rh));
215 rath = malloc_rat_handle(sizeof(*rath));
218 memset(rath, 0, sizeof(*rath));
220 rath->rat = &cm5121_ccid;
221 rh->reader = &rfid_reader_cm5121;
223 if (cm5121_source_init(rath) < 0)
226 if (cm5121_enable_rc632(rath) < 0)
229 rh->ah = rc632_open(rath);
233 DEBUGP("returning %p\n", rh);
237 free_rat_handle(rath);
239 free_reader_handle(rh);
245 cm5121_close(struct rfid_reader_handle *rh)
247 struct rfid_asic_transport_handle *rath = rh->ah->rath;
249 free_rat_handle(rath);
250 free_reader_handle(rh);
253 const struct rfid_reader rfid_reader_cm5121 = {
254 .name = "Omnikey CardMan 5121 RFID",
255 .open = &cm5121_open,
256 .close = &cm5121_close,
257 .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
258 (1 << RFID_LAYER2_ISO14443B) |
259 (1 << RFID_LAYER2_ISO15693),
260 .proto_supported = (1 << RFID_PROTOCOL_TCL) |
261 (1 << RFID_PROTOCOL_MIFARE_UL) |
262 (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
263 .getopt = &_rdr_rc632_getopt,
264 .setopt = &_rdr_rc632_setopt,
265 .transceive = &_rdr_rc632_transceive,
266 .init = &_rdr_rc632_l2_init,
268 .transceive_sf = &_rdr_rc632_transceive_sf,
269 .transceive_acf = &_rdr_rc632_transceive_acf,
270 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
271 RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
272 .set_speed = &_rdr_rc632_14443a_set_speed,
275 .transceive_ac = &_rdr_rc632_iso15693_transceive_ac,
278 .setkey = &_rdr_rc632_mifare_setkey,
279 .auth = &_rdr_rc632_mifare_auth,
283 #endif /* LIBRFID_FIRMWARE */