1 /* Omnikey CardMan 5121 specific RC632 transport layer
3 * (C) 2005-2008 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
34 #include <librfid/rfid.h>
36 #ifndef LIBRFID_FIRMWARE
39 #include <librfid/rfid_reader.h>
40 #include <librfid/rfid_asic.h>
41 #include <librfid/rfid_asic_rc632.h>
42 #include <librfid/rfid_reader_cm5121.h>
43 #include <librfid/rfid_layer2.h>
44 #include <librfid/rfid_protocol.h>
46 #include "rfid_reader_rc632_common.h"
48 #include "cm5121_source.h"
53 #define SENDBUF_LEN 256+7+10 /* 256bytes max FSD/FSC, plus 7 bytes header,
54 plus 10 bytes reserve */
55 #define RECVBUF_LEN SENDBUF_LEN
58 #define DEBUGRC DEBUGPC
61 #define DEBUGRC(x, args ...) do {} while(0)
62 #define DEBUGR(x, args ...) do {} while(0)
66 int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
67 unsigned char reg, unsigned char value)
69 unsigned char sndbuf[SENDBUF_LEN];
70 unsigned char rcvbuf[RECVBUF_LEN];
71 size_t retlen = RECVBUF_LEN;
82 DEBUGR("reg=0x%02x, val=%02x: ", reg, value);
84 if (PC_to_RDR_Escape(rath->data, sndbuf, 8, rcvbuf,
94 static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath,
98 unsigned char sndbuf[SENDBUF_LEN];
99 unsigned char recvbuf[RECVBUF_LEN];
100 size_t retlen = sizeof(recvbuf);
110 if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf,
113 DEBUGR("reg=0x%02x, val=%02x: ", reg, *value);
122 static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath,
123 unsigned char num_bytes,
126 unsigned char sndbuf[SENDBUF_LEN];
127 unsigned char recvbuf[0x7f];
128 size_t retlen = sizeof(recvbuf);
134 sndbuf[4] = num_bytes;
138 DEBUGR("num_bytes=%u: ", num_bytes);
139 if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, &retlen) == 0) {
140 DEBUGRC("%u [%s]\n", retlen,
141 rfid_hexdump(recvbuf+1, num_bytes));
142 memcpy(buf, recvbuf+1, num_bytes); // len == 0x7f
150 static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath,
152 const unsigned char *bytes,
155 unsigned char sndbuf[SENDBUF_LEN];
156 unsigned char recvbuf[0x7f];
157 size_t retlen = sizeof(recvbuf);
167 DEBUGR("%u [%s]: ", len, rfid_hexdump(bytes, len));
169 memcpy(sndbuf+7, bytes, len);
171 if (PC_to_RDR_Escape(rath->data, sndbuf, len+7, recvbuf, &retlen) == 0) {
172 DEBUGRC("OK (%u [%s])\n", retlen, rfid_hexdump(recvbuf, retlen));
180 struct rfid_asic_transport cm5121_ccid = {
181 .name = "CM5121 OpenCT",
184 .reg_write = &Write1ByteToReg,
185 .reg_read = &Read1ByteFromReg,
186 .fifo_write = &WriteNBytesToFIFO,
187 .fifo_read = &ReadNBytesFromFIFO,
192 static int cm5121_enable_rc632(struct rfid_asic_transport_handle *rath)
194 unsigned char tx_buf[1] = { 0x01 };
195 unsigned char rx_buf[64];
196 size_t rx_len = sizeof(rx_buf);
198 PC_to_RDR_Escape(rath->data, tx_buf, 1, rx_buf, &rx_len);
203 static struct rfid_reader_handle *
204 cm5121_open(void *data)
206 struct rfid_reader_handle *rh;
207 struct rfid_asic_transport_handle *rath;
209 rh = malloc_reader_handle(sizeof(*rh));
212 memset(rh, 0, sizeof(*rh));
214 rath = malloc_rat_handle(sizeof(*rath));
217 memset(rath, 0, sizeof(*rath));
219 rath->rat = &cm5121_ccid;
220 rh->reader = &rfid_reader_cm5121;
222 if (cm5121_source_init(rath) < 0)
225 if (cm5121_enable_rc632(rath) < 0)
228 rh->ah = rc632_open(rath);
232 DEBUGP("returning %p\n", rh);
236 free_rat_handle(rath);
238 free_reader_handle(rh);
244 cm5121_close(struct rfid_reader_handle *rh)
246 struct rfid_asic_transport_handle *rath = rh->ah->rath;
248 free_rat_handle(rath);
249 free_reader_handle(rh);
252 const struct rfid_reader rfid_reader_cm5121 = {
253 .name = "Omnikey CardMan 5121 RFID",
254 .open = &cm5121_open,
255 .close = &cm5121_close,
256 .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
257 (1 << RFID_LAYER2_ISO14443B) |
258 (1 << RFID_LAYER2_ISO15693),
259 .proto_supported = (1 << RFID_PROTOCOL_TCL) |
260 (1 << RFID_PROTOCOL_MIFARE_UL) |
261 (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
262 .getopt = &_rdr_rc632_getopt,
263 .setopt = &_rdr_rc632_setopt,
264 .transceive = &_rdr_rc632_transceive,
265 .init = &_rdr_rc632_l2_init,
267 .transceive_sf = &_rdr_rc632_transceive_sf,
268 .transceive_acf = &_rdr_rc632_transceive_acf,
269 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
270 RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
271 .set_speed = &_rdr_rc632_14443a_set_speed,
274 .transceive_ac = &_rdr_rc632_iso15693_transceive_ac,
277 .setkey = &_rdr_rc632_mifare_setkey,
278 .setkey_ee = &_rdr_rc632_mifare_setkey_ee,
279 .auth = &_rdr_rc632_mifare_auth,
283 #endif /* LIBRFID_FIRMWARE */