partial ISO15693 support (based on patch by Bjoern Kaiser)
[librfid] / src / rfid_reader_cm5121.c
1 /* Omnikey CardMan 5121 specific RC632 transport layer 
2  *
3  * (C) 2005-2006 by Harald Welte <laforge@gnumonks.org>
4  *
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,
7  * not via SPI.
8  *
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
11  * the RC632.
12  * 
13  */
14
15 /*
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
19  *
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.
24  *
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
28  */
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #define DEBUG_LIBRFID
35 #include <librfid/rfid.h>
36
37 #ifndef LIBRFID_FIRMWARE
38
39
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>
46
47 #include "cm5121_source.h"
48
49 /* FIXME */
50 #include "rc632.h"
51
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
55
56 #define DEBUG_REGISTER
57
58 #ifdef DEBUG_REGISTER
59 #define DEBUGRC DEBUGPC
60 #define DEBUGR DEBUGP
61 #else
62 #define DEBUGRC(x, args ...)    do {} while(0)
63 #define DEBUGR(x, args ...)     do {} while(0)
64 #endif
65
66 static
67 int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
68                     unsigned char reg, unsigned char value)
69 {
70         unsigned char sndbuf[SENDBUF_LEN];
71         unsigned char rcvbuf[RECVBUF_LEN];
72         size_t retlen = RECVBUF_LEN;
73
74         sndbuf[0] = 0x20;
75         sndbuf[1] = 0x00;
76         sndbuf[2] = 0x01;
77         sndbuf[3] = 0x00;
78         sndbuf[4] = 0x00;
79         sndbuf[5] = 0x00;
80         sndbuf[6] = reg;
81         sndbuf[7] = value;
82
83         DEBUGR("reg=0x%02x, val=%02x: ", reg, value);
84
85         if (PC_to_RDR_Escape(rath->data, sndbuf, 8, rcvbuf, 
86                              &retlen) == 0) {
87                 DEBUGRC("OK\n");
88                 return 0;
89         }
90
91         DEBUGRC("ERROR\n");
92         return -1;
93 }
94
95 static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath,
96                             unsigned char reg,
97                             unsigned char *value)
98 {
99         unsigned char sndbuf[SENDBUF_LEN];
100         unsigned char recvbuf[RECVBUF_LEN];
101         size_t retlen = sizeof(recvbuf);
102
103         sndbuf[0] = 0x20;
104         sndbuf[1] = 0x00;
105         sndbuf[2] = 0x00;
106         sndbuf[3] = 0x00;
107         sndbuf[4] = 0x01;
108         sndbuf[5] = 0x00;
109         sndbuf[6] = reg;
110
111         if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, 
112                              &retlen) == 0) {
113                 *value = recvbuf[1];
114                 DEBUGR("reg=0x%02x, val=%02x: ", reg, *value);
115                 DEBUGRC("OK\n");
116                 return 0;
117         }
118
119         DEBUGRC("ERROR\n");
120         return -1;
121 }
122
123 static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath,
124                               unsigned char num_bytes,
125                               unsigned char *buf)
126 {
127         unsigned char sndbuf[SENDBUF_LEN];
128         unsigned char recvbuf[0x7f];
129         size_t retlen = sizeof(recvbuf);
130
131         sndbuf[0] = 0x20;
132         sndbuf[1] = 0x00;
133         sndbuf[2] = 0x00;
134         sndbuf[3] = 0x00;
135         sndbuf[4] = num_bytes;
136         sndbuf[5] = 0x00;
137         sndbuf[6] = 0x02;
138
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
144                 return 0;
145         }
146
147         DEBUGRC("ERROR\n");
148         return -1;
149 }
150
151 static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath,
152                              unsigned char len,
153                              const unsigned char *bytes,
154                              unsigned char flags)
155 {
156         unsigned char sndbuf[SENDBUF_LEN];
157         unsigned char recvbuf[0x7f];
158         size_t retlen = sizeof(recvbuf);
159
160         sndbuf[0] = 0x20;
161         sndbuf[1] = 0x00;
162         sndbuf[2] = len;
163         sndbuf[3] = 0x00;
164         sndbuf[4] = 0x00;
165         sndbuf[5] = flags;
166         sndbuf[6] = 0x02;
167
168         DEBUGR("%u [%s]: ", len, rfid_hexdump(bytes, len));
169
170         memcpy(sndbuf+7, bytes, len);
171
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));
174                 return 0;
175         }
176
177         DEBUGRC("ERROR\n");
178         return -1;
179 }
180
181 #if 0
182 static int TestFIFO(struct rc632_handle *handle)
183 {
184         unsigned char sndbuf[60]; // 0x3c
185
186         // FIXME: repne stosd, call
187
188         memset(sndbuf, 0, sizeof(sndbuf));
189
190         if (WriteNBytesToFIFO(handle, sizeof(sndbuf), sndbuf, 0) < 0)
191                 return -1;
192
193         return ReadNBytesFromFIFO(handle, sizeof(sndbuf), sndbuf);
194 }
195 #endif
196
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)
202 {
203         return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
204                                                 tx_data, tx_len, rx_data,
205                                                 rx_len, timeout, flags);
206 }
207
208 static int cm5121_transceive_sf(struct rfid_reader_handle *rh,
209                                unsigned char cmd, struct iso14443a_atqa *atqa)
210 {
211         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
212                                                                    cmd,
213                                                                    atqa);
214 }
215
216 static int
217 cm5121_transceive_acf(struct rfid_reader_handle *rh,
218                       struct iso14443a_anticol_cmd *cmd,
219                       unsigned int *bit_of_col)
220 {
221         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
222                                                          cmd, bit_of_col);
223 }
224
225 static int
226 cm5121_14443a_init(struct rfid_reader_handle *rh)
227 {
228         return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
229 }
230
231 static int
232 cm5121_14443a_set_speed(struct rfid_reader_handle *rh, 
233                         unsigned int tx,
234                         unsigned int speed)
235 {
236         u_int8_t rate;
237         
238         DEBUGP("setting rate: ");
239         switch (speed) {
240         case RFID_14443A_SPEED_106K:
241                 rate = 0x00;
242                 DEBUGPC("106K\n");
243                 break;
244         case RFID_14443A_SPEED_212K:
245                 rate = 0x01;
246                 DEBUGPC("212K\n");
247                 break;
248         case RFID_14443A_SPEED_424K:
249                 rate = 0x02;
250                 DEBUGPC("424K\n");
251                 break;
252         case RFID_14443A_SPEED_848K:
253                 rate = 0x03;
254                 DEBUGPC("848K\n");
255                 break;
256         default:
257                 DEBUGPC("invalid\n");
258                 return -EINVAL;
259                 break;
260         }
261         return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
262                                                                 tx, rate);
263 }
264
265 static int
266 cm5121_14443b_init(struct rfid_reader_handle *rh)
267 {
268         return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
269 }
270
271 static int
272 cm5121_15693_init(struct rfid_reader_handle *rh)
273 {
274         return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
275 }
276
277 static int
278 cm5121_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
279 {
280         return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
281 }
282
283 static int
284 cm5121_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
285                    u_int32_t serno, u_int8_t block)
286 {
287         return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah, 
288                                                         cmd, serno, block);
289 }
290
291 static int
292 cm5121_rf_power(struct rfid_reader_handle *rh, int on)
293 {
294         return rh->ah->asic->priv.rc632.fn.rf_power(rh->ah, on);
295 }
296
297 struct rfid_asic_transport cm5121_ccid = {
298         .name = "CM5121 OpenCT",
299         .priv.rc632 = {
300                 .fn = {
301                         .reg_write      = &Write1ByteToReg,
302                         .reg_read       = &Read1ByteFromReg,
303                         .fifo_write     = &WriteNBytesToFIFO,
304                         .fifo_read      = &ReadNBytesFromFIFO,
305                 },
306         },
307 };
308
309 static int cm5121_enable_rc632(struct rfid_asic_transport_handle *rath)
310 {
311         unsigned char tx_buf[1] = { 0x01 };     
312         unsigned char rx_buf[64];
313         size_t rx_len = sizeof(rx_buf);
314
315         PC_to_RDR_Escape(rath->data, tx_buf, 1, rx_buf, &rx_len);
316
317         return 0;
318 }
319
320 static struct rfid_reader_handle *
321 cm5121_open(void *data)
322 {
323         struct rfid_reader_handle *rh;
324         struct rfid_asic_transport_handle *rath;
325
326         rh = malloc_reader_handle(sizeof(*rh));
327         if (!rh)
328                 return NULL;
329         memset(rh, 0, sizeof(*rh));
330
331         rath = malloc_rat_handle(sizeof(*rath));
332         if (!rath)
333                 goto out_rh;
334         memset(rath, 0, sizeof(*rath));
335
336         rath->rat = &cm5121_ccid;
337         rh->reader = &rfid_reader_cm5121;
338
339         if (cm5121_source_init(rath) < 0)
340                 goto out_rath;
341
342         if (cm5121_enable_rc632(rath) < 0)
343                 goto out_rath;
344
345         rh->ah = rc632_open(rath);
346         if (!rh->ah) 
347                 goto out_rath;
348
349         DEBUGP("returning %p\n", rh);
350         return rh;
351
352 out_rath:
353         free_rat_handle(rath);
354 out_rh:
355         free_reader_handle(rh);
356
357         return NULL;
358 }
359
360 static void
361 cm5121_close(struct rfid_reader_handle *rh)
362 {
363         struct rfid_asic_transport_handle *rath = rh->ah->rath;
364         rc632_close(rh->ah);
365         free_rat_handle(rath);
366         free_reader_handle(rh);
367 }
368
369 static int
370 cm5121_iso15693_transceive_ac(struct rfid_reader_handle *rh,
371                               struct iso15693_anticol_cmd *acf,
372                               unsigned char uuid[ISO15693_UID_LEN],
373                               char *bit_of_col)
374 {
375         return rh->ah->asic->priv.rc632.fn.iso15693.transceive_ac(
376                                         rh->ah, acf, uuid, bit_of_col);
377 }
378
379 const struct rfid_reader rfid_reader_cm5121 = {
380         .name   = "Omnikey CardMan 5121 RFID",
381         .open = &cm5121_open,
382         .close = &cm5121_close,
383         .rf_power = &cm5121_rf_power,
384         .transceive = &cm5121_transceive,
385         .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
386                         (1 << RFID_LAYER2_ISO14443B) |
387                         (1 << RFID_LAYER2_ISO15693),
388         .proto_supported = (1 << RFID_PROTOCOL_TCL) |
389                         (1 << RFID_PROTOCOL_MIFARE_UL) |
390                         (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
391         .iso14443a = {
392                 .init = &cm5121_14443a_init,
393                 .transceive_sf = &cm5121_transceive_sf,
394                 .transceive_acf = &cm5121_transceive_acf,
395                 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
396                          RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
397                 .set_speed = &cm5121_14443a_set_speed,
398         },
399         .iso14443b = {
400                 .init = &cm5121_14443b_init,
401         },
402         .iso15693 = {
403                 .init = &cm5121_15693_init,
404                 .transceive_ac = &cm5121_iso15693_transceive_ac,
405         },
406         .mifare_classic = {
407                 .setkey = &cm5121_mifare_setkey,
408                 .auth = &cm5121_mifare_auth,
409         },
410 };
411
412 #endif /* LIBRFID_FIRMWARE */