1 /* Omnikey CardMan 5121 specific RC632 transport layer
3 * (C) 2005 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>
44 //#define SENDBUF_LEN 40
45 #define SENDBUF_LEN 100
46 #define RECVBUF_LEN 40
49 int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
50 unsigned char reg, unsigned char value)
52 unsigned char sndbuf[SENDBUF_LEN];
53 unsigned char rcvbuf[RECVBUF_LEN];
54 unsigned int retlen = RECVBUF_LEN;
65 DEBUGP("reg=0x%02x, val=%02x: ", reg, value);
67 if (PC_to_RDR_Escape(rath->data, sndbuf, 8, rcvbuf,
77 static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath,
81 unsigned char sndbuf[SENDBUF_LEN];
82 unsigned char recvbuf[RECVBUF_LEN];
83 unsigned int retlen = sizeof(recvbuf);
93 if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf,
96 DEBUGP("reg=0x%02x, val=%02x: ", reg, *value);
105 static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath,
106 unsigned char num_bytes,
109 unsigned char sndbuf[SENDBUF_LEN];
110 unsigned char recvbuf[0x7f];
111 unsigned int retlen = sizeof(recvbuf);
117 sndbuf[4] = num_bytes;
121 DEBUGP("num_bytes=%u: ", num_bytes);
122 if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, &retlen) == 0) {
123 DEBUGPC("%u [%s]\n", retlen,
124 rfid_hexdump(recvbuf+1, num_bytes));
125 memcpy(buf, recvbuf+1, num_bytes); // len == 0x7f
133 static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath,
135 const unsigned char *bytes,
138 unsigned char sndbuf[SENDBUF_LEN];
139 unsigned char recvbuf[0x7f];
140 unsigned int retlen = sizeof(recvbuf);
150 DEBUGP("%u [%s]: ", len, rfid_hexdump(bytes, len));
152 memcpy(sndbuf+7, bytes, len);
154 if (PC_to_RDR_Escape(rath->data, sndbuf, len+7, recvbuf, &retlen) == 0) {
155 DEBUGPC("OK (%u [%s])\n", retlen, rfid_hexdump(recvbuf, retlen));
164 static int TestFIFO(struct rc632_handle *handle)
166 unsigned char sndbuf[60]; // 0x3c
168 // FIXME: repne stosd, call
170 memset(sndbuf, 0, sizeof(sndbuf));
172 if (WriteNBytesToFIFO(handle, sizeof(sndbuf), sndbuf, 0) < 0)
175 return ReadNBytesFromFIFO(handle, sizeof(sndbuf), sndbuf);
179 static int cm5121_transcieve(struct rfid_reader_handle *rh,
180 enum rfid_frametype frametype,
181 const unsigned char *tx_data, unsigned int tx_len,
182 unsigned char *rx_data, unsigned int *rx_len,
183 u_int64_t timeout, unsigned int flags)
185 return rh->ah->asic->priv.rc632.fn.transcieve(rh->ah, frametype,
186 tx_data, tx_len, rx_data,
187 rx_len, timeout, flags);
190 static int cm5121_transcieve_sf(struct rfid_reader_handle *rh,
191 unsigned char cmd, struct iso14443a_atqa *atqa)
193 return rh->ah->asic->priv.rc632.fn.iso14443a.transcieve_sf(rh->ah,
199 cm5121_transcieve_acf(struct rfid_reader_handle *rh,
200 struct iso14443a_anticol_cmd *cmd,
201 unsigned int *bit_of_col)
203 return rh->ah->asic->priv.rc632.fn.iso14443a.transcieve_acf(rh->ah,
208 cm5121_14443a_init(struct rfid_reader_handle *rh)
210 return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
214 cm5121_14443a_set_speed(struct rfid_reader_handle *rh,
220 DEBUGP("setting rate: ");
222 case RFID_14443A_SPEED_106K:
226 case RFID_14443A_SPEED_212K:
230 case RFID_14443A_SPEED_424K:
234 case RFID_14443A_SPEED_848K:
242 return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
247 cm5121_14443b_init(struct rfid_reader_handle *rh)
249 return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
253 cm5121_15693_init(struct rfid_reader_handle *rh)
255 return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
259 cm5121_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
261 return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
265 cm5121_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
266 u_int32_t serno, u_int8_t block)
268 return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah,
272 struct rfid_asic_transport cm5121_ccid = {
273 .name = "CM5121 OpenCT",
276 .reg_write = &Write1ByteToReg,
277 .reg_read = &Read1ByteFromReg,
278 .fifo_write = &WriteNBytesToFIFO,
279 .fifo_read = &ReadNBytesFromFIFO,
284 static int cm5121_enable_rc632(struct rfid_asic_transport_handle *rath)
286 unsigned char tx_buf[1] = { 0x01 };
287 unsigned char rx_buf[64];
288 unsigned int rx_len = sizeof(rx_buf);
290 PC_to_RDR_Escape(rath->data, tx_buf, 1, rx_buf, &rx_len);
291 printf("received %u bytes from 01 command\n", rx_len);
296 static struct rfid_reader_handle *
297 cm5121_open(void *data)
299 struct rfid_reader_handle *rh;
300 struct rfid_asic_transport_handle *rath;
302 rh = malloc(sizeof(*rh));
305 memset(rh, 0, sizeof(*rh));
307 rath = malloc(sizeof(*rath));
310 memset(rath, 0, sizeof(*rath));
312 rath->rat = &cm5121_ccid;
313 rh->reader = &rfid_reader_cm5121;
315 if (cm5121_source_init(rath) < 0)
318 if (cm5121_enable_rc632(rath) < 0)
321 rh->ah = rc632_open(rath);
325 DEBUGP("returning %p\n", rh);
337 cm5121_close(struct rfid_reader_handle *rh)
339 struct rfid_asic_transport_handle *rath = rh->ah->rath;
345 struct rfid_reader rfid_reader_cm5121 = {
346 .name = "Omnikey CardMan 5121 RFID",
347 .open = &cm5121_open,
348 .close = &cm5121_close,
349 .transcieve = &cm5121_transcieve,
351 .init = &cm5121_14443a_init,
352 .transcieve_sf = &cm5121_transcieve_sf,
353 .transcieve_acf = &cm5121_transcieve_acf,
354 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
355 RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
356 .set_speed = &cm5121_14443a_set_speed,
359 .init = &cm5121_14443b_init,
362 .setkey = &cm5121_mifare_setkey,
363 .auth = &cm5121_mifare_auth,