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 "cm5121_source.h"
52 #define SENDBUF_LEN 256+7+10 /* 256bytes max FSD/FSC, plus 7 bytes header,
53 plus 10 bytes reserve */
54 #define RECVBUF_LEN SENDBUF_LEN
56 #define DEBUG_REGISTER
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));
182 static int TestFIFO(struct rc632_handle *handle)
184 unsigned char sndbuf[60]; // 0x3c
186 // FIXME: repne stosd, call
188 memset(sndbuf, 0, sizeof(sndbuf));
190 if (WriteNBytesToFIFO(handle, sizeof(sndbuf), sndbuf, 0) < 0)
193 return ReadNBytesFromFIFO(handle, sizeof(sndbuf), sndbuf);
197 static int cm5121_transceive(struct rfid_reader_handle *rh,
198 enum rfid_frametype frametype,
199 const unsigned char *tx_data, unsigned int tx_len,
200 unsigned char *rx_data, unsigned int *rx_len,
201 u_int64_t timeout, unsigned int flags)
203 return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
204 tx_data, tx_len, rx_data,
205 rx_len, timeout, flags);
208 static int cm5121_transceive_sf(struct rfid_reader_handle *rh,
209 unsigned char cmd, struct iso14443a_atqa *atqa)
211 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
217 cm5121_transceive_acf(struct rfid_reader_handle *rh,
218 struct iso14443a_anticol_cmd *cmd,
219 unsigned int *bit_of_col)
221 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
226 cm5121_14443a_init(struct rfid_reader_handle *rh)
228 return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
232 cm5121_14443a_set_speed(struct rfid_reader_handle *rh,
238 DEBUGP("setting rate: ");
240 case RFID_14443A_SPEED_106K:
244 case RFID_14443A_SPEED_212K:
248 case RFID_14443A_SPEED_424K:
252 case RFID_14443A_SPEED_848K:
257 DEBUGPC("invalid\n");
261 return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
266 cm5121_14443b_init(struct rfid_reader_handle *rh)
268 return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
272 cm5121_15693_init(struct rfid_reader_handle *rh)
274 return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
278 cm5121_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
280 return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
284 cm5121_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
285 u_int32_t serno, u_int8_t block)
287 return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah,
291 struct rfid_asic_transport cm5121_ccid = {
292 .name = "CM5121 OpenCT",
295 .reg_write = &Write1ByteToReg,
296 .reg_read = &Read1ByteFromReg,
297 .fifo_write = &WriteNBytesToFIFO,
298 .fifo_read = &ReadNBytesFromFIFO,
303 static int cm5121_enable_rc632(struct rfid_asic_transport_handle *rath)
305 unsigned char tx_buf[1] = { 0x01 };
306 unsigned char rx_buf[64];
307 size_t rx_len = sizeof(rx_buf);
309 PC_to_RDR_Escape(rath->data, tx_buf, 1, rx_buf, &rx_len);
314 static struct rfid_reader_handle *
315 cm5121_open(void *data)
317 struct rfid_reader_handle *rh;
318 struct rfid_asic_transport_handle *rath;
320 rh = malloc_reader_handle(sizeof(*rh));
323 memset(rh, 0, sizeof(*rh));
325 rath = malloc_rat_handle(sizeof(*rath));
328 memset(rath, 0, sizeof(*rath));
330 rath->rat = &cm5121_ccid;
331 rh->reader = &rfid_reader_cm5121;
333 if (cm5121_source_init(rath) < 0)
336 if (cm5121_enable_rc632(rath) < 0)
339 rh->ah = rc632_open(rath);
343 DEBUGP("returning %p\n", rh);
347 free_rat_handle(rath);
349 free_reader_handle(rh);
355 cm5121_close(struct rfid_reader_handle *rh)
357 struct rfid_asic_transport_handle *rath = rh->ah->rath;
359 free_rat_handle(rath);
360 free_reader_handle(rh);
363 const struct rfid_reader rfid_reader_cm5121 = {
364 .name = "Omnikey CardMan 5121 RFID",
365 .open = &cm5121_open,
366 .close = &cm5121_close,
367 .transceive = &cm5121_transceive,
368 .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
369 (1 << RFID_LAYER2_ISO14443B) |
370 (1 << RFID_LAYER2_ISO15693),
371 .proto_supported = (1 << RFID_PROTOCOL_TCL) |
372 (1 << RFID_PROTOCOL_MIFARE_UL) |
373 (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
375 .init = &cm5121_14443a_init,
376 .transceive_sf = &cm5121_transceive_sf,
377 .transceive_acf = &cm5121_transceive_acf,
378 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
379 RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
380 .set_speed = &cm5121_14443a_set_speed,
383 .init = &cm5121_14443b_init,
386 .init = &cm5121_15693_init,
389 .setkey = &cm5121_mifare_setkey,
390 .auth = &cm5121_mifare_auth,
394 #endif /* LIBRFID_FIRMWARE */