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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include <librfid/rfid.h>
36 #include <librfid/rfid_reader.h>
37 #include <librfid/rfid_asic.h>
38 #include <librfid/rfid_asic_rc632.h>
39 #include <librfid/rfid_reader_cm5121.h>
40 #include <librfid/rfid_layer2.h>
41 #include <librfid/rfid_protocol.h>
43 #include "cm5121_source.h"
48 #define SENDBUF_LEN 256+7+10 /* 256bytes max FSD/FSC, plus 7 bytes header,
49 plus 10 bytes reserve */
50 #define RECVBUF_LEN SENDBUF_LEN
52 //#define DEBUG_REGISTER
55 #define DEBUGRC DEBUGPC
58 #define DEBUGRC(x, args ...) do {} while(0)
59 #define DEBUGR(x, args ...) do {} while(0)
63 int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
64 unsigned char reg, unsigned char value)
66 unsigned char sndbuf[SENDBUF_LEN];
67 unsigned char rcvbuf[RECVBUF_LEN];
68 unsigned int retlen = RECVBUF_LEN;
79 DEBUGR("reg=0x%02x, val=%02x: ", reg, value);
81 if (PC_to_RDR_Escape(rath->data, sndbuf, 8, rcvbuf,
91 static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath,
95 unsigned char sndbuf[SENDBUF_LEN];
96 unsigned char recvbuf[RECVBUF_LEN];
97 unsigned int retlen = sizeof(recvbuf);
107 if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf,
110 DEBUGR("reg=0x%02x, val=%02x: ", reg, *value);
119 static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath,
120 unsigned char num_bytes,
123 unsigned char sndbuf[SENDBUF_LEN];
124 unsigned char recvbuf[0x7f];
125 unsigned int retlen = sizeof(recvbuf);
131 sndbuf[4] = num_bytes;
135 DEBUGR("num_bytes=%u: ", num_bytes);
136 if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, &retlen) == 0) {
137 DEBUGRC("%u [%s]\n", retlen,
138 rfid_hexdump(recvbuf+1, num_bytes));
139 memcpy(buf, recvbuf+1, num_bytes); // len == 0x7f
147 static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath,
149 const unsigned char *bytes,
152 unsigned char sndbuf[SENDBUF_LEN];
153 unsigned char recvbuf[0x7f];
154 unsigned int retlen = sizeof(recvbuf);
164 DEBUGR("%u [%s]: ", len, rfid_hexdump(bytes, len));
166 memcpy(sndbuf+7, bytes, len);
168 if (PC_to_RDR_Escape(rath->data, sndbuf, len+7, recvbuf, &retlen) == 0) {
169 DEBUGRC("OK (%u [%s])\n", retlen, rfid_hexdump(recvbuf, retlen));
178 static int TestFIFO(struct rc632_handle *handle)
180 unsigned char sndbuf[60]; // 0x3c
182 // FIXME: repne stosd, call
184 memset(sndbuf, 0, sizeof(sndbuf));
186 if (WriteNBytesToFIFO(handle, sizeof(sndbuf), sndbuf, 0) < 0)
189 return ReadNBytesFromFIFO(handle, sizeof(sndbuf), sndbuf);
193 static int cm5121_transceive(struct rfid_reader_handle *rh,
194 enum rfid_frametype frametype,
195 const unsigned char *tx_data, unsigned int tx_len,
196 unsigned char *rx_data, unsigned int *rx_len,
197 u_int64_t timeout, unsigned int flags)
199 return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
200 tx_data, tx_len, rx_data,
201 rx_len, timeout, flags);
204 static int cm5121_transceive_sf(struct rfid_reader_handle *rh,
205 unsigned char cmd, struct iso14443a_atqa *atqa)
207 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
213 cm5121_transceive_acf(struct rfid_reader_handle *rh,
214 struct iso14443a_anticol_cmd *cmd,
215 unsigned int *bit_of_col)
217 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
222 cm5121_14443a_init(struct rfid_reader_handle *rh)
224 return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
228 cm5121_14443a_set_speed(struct rfid_reader_handle *rh,
234 DEBUGP("setting rate: ");
236 case RFID_14443A_SPEED_106K:
240 case RFID_14443A_SPEED_212K:
244 case RFID_14443A_SPEED_424K:
248 case RFID_14443A_SPEED_848K:
253 DEBUGPC("invalid\n");
257 return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
262 cm5121_14443b_init(struct rfid_reader_handle *rh)
264 return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
268 cm5121_15693_init(struct rfid_reader_handle *rh)
270 return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
274 cm5121_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
276 return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
280 cm5121_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
281 u_int32_t serno, u_int8_t block)
283 return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah,
287 struct rfid_asic_transport cm5121_ccid = {
288 .name = "CM5121 OpenCT",
291 .reg_write = &Write1ByteToReg,
292 .reg_read = &Read1ByteFromReg,
293 .fifo_write = &WriteNBytesToFIFO,
294 .fifo_read = &ReadNBytesFromFIFO,
299 static int cm5121_enable_rc632(struct rfid_asic_transport_handle *rath)
301 unsigned char tx_buf[1] = { 0x01 };
302 unsigned char rx_buf[64];
303 unsigned int rx_len = sizeof(rx_buf);
305 PC_to_RDR_Escape(rath->data, tx_buf, 1, rx_buf, &rx_len);
306 printf("received %u bytes from 01 command\n", rx_len);
311 static struct rfid_reader_handle *
312 cm5121_open(void *data)
314 struct rfid_reader_handle *rh;
315 struct rfid_asic_transport_handle *rath;
317 rh = malloc(sizeof(*rh));
320 memset(rh, 0, sizeof(*rh));
322 rath = malloc(sizeof(*rath));
325 memset(rath, 0, sizeof(*rath));
327 rath->rat = &cm5121_ccid;
328 rh->reader = &rfid_reader_cm5121;
330 if (cm5121_source_init(rath) < 0)
333 if (cm5121_enable_rc632(rath) < 0)
336 rh->ah = rc632_open(rath);
340 DEBUGP("returning %p\n", rh);
352 cm5121_close(struct rfid_reader_handle *rh)
354 struct rfid_asic_transport_handle *rath = rh->ah->rath;
360 struct rfid_reader rfid_reader_cm5121 = {
361 .name = "Omnikey CardMan 5121 RFID",
362 .open = &cm5121_open,
363 .close = &cm5121_close,
364 .transceive = &cm5121_transceive,
365 .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
366 (1 << RFID_LAYER2_ISO14443B) |
367 (1 << RFID_LAYER2_ISO15693),
368 .proto_supported = (1 << RFID_PROTOCOL_TCL) |
369 (1 << RFID_PROTOCOL_MIFARE_UL) |
370 (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
372 .init = &cm5121_14443a_init,
373 .transceive_sf = &cm5121_transceive_sf,
374 .transceive_acf = &cm5121_transceive_acf,
375 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
376 RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
377 .set_speed = &cm5121_14443a_set_speed,
380 .init = &cm5121_14443b_init,
383 .setkey = &cm5121_mifare_setkey,
384 .auth = &cm5121_mifare_auth,