introduce new debug macro for debugging register-level accesses
[librfid] / src / rfid_reader_cm5121.c
1 /* Omnikey CardMan 5121 specific RC632 transport layer 
2  *
3  * (C) 2005 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34
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
41 /* FIXME */
42 #include "rc632.h"
43
44 #define SENDBUF_LEN     256+7+10 /* 256bytes max FSD/FSC, plus 7 bytes header,
45                                     plus 10 bytes reserve */
46 #define RECVBUF_LEN     SENDBUF_LEN
47
48 //#define DEBUG_REGISTER
49
50 #ifdef DEBUG_REGISTER
51 #define DEBUGRC DEBUGPC
52 #define DEBUGR DEBUGP
53 #else
54 #define DEBUGRC(x, args ...)    do {} while(0)
55 #define DEBUGR(x, args ...)     do {} while(0)
56 #endif
57
58 static
59 int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
60                     unsigned char reg, unsigned char value)
61 {
62         unsigned char sndbuf[SENDBUF_LEN];
63         unsigned char rcvbuf[RECVBUF_LEN];
64         unsigned int retlen = RECVBUF_LEN;
65
66         sndbuf[0] = 0x20;
67         sndbuf[1] = 0x00;
68         sndbuf[2] = 0x01;
69         sndbuf[3] = 0x00;
70         sndbuf[4] = 0x00;
71         sndbuf[5] = 0x00;
72         sndbuf[6] = reg;
73         sndbuf[7] = value;
74
75         DEBUGR("reg=0x%02x, val=%02x: ", reg, value);
76
77         if (PC_to_RDR_Escape(rath->data, sndbuf, 8, rcvbuf, 
78                              &retlen) == 0) {
79                 DEBUGRC("OK\n");
80                 return 0;
81         }
82
83         DEBUGRC("ERROR\n");
84         return -1;
85 }
86
87 static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath,
88                             unsigned char reg,
89                             unsigned char *value)
90 {
91         unsigned char sndbuf[SENDBUF_LEN];
92         unsigned char recvbuf[RECVBUF_LEN];
93         unsigned int retlen = sizeof(recvbuf);
94
95         sndbuf[0] = 0x20;
96         sndbuf[1] = 0x00;
97         sndbuf[2] = 0x00;
98         sndbuf[3] = 0x00;
99         sndbuf[4] = 0x01;
100         sndbuf[5] = 0x00;
101         sndbuf[6] = reg;
102
103         if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, 
104                              &retlen) == 0) {
105                 *value = recvbuf[1];
106                 DEBUGR("reg=0x%02x, val=%02x: ", reg, *value);
107                 DEBUGRC("OK\n");
108                 return 0;
109         }
110
111         DEBUGRC("ERROR\n");
112         return -1;
113 }
114
115 static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath,
116                               unsigned char num_bytes,
117                               unsigned char *buf)
118 {
119         unsigned char sndbuf[SENDBUF_LEN];
120         unsigned char recvbuf[0x7f];
121         unsigned int retlen = sizeof(recvbuf);
122
123         sndbuf[0] = 0x20;
124         sndbuf[1] = 0x00;
125         sndbuf[2] = 0x00;
126         sndbuf[3] = 0x00;
127         sndbuf[4] = num_bytes;
128         sndbuf[5] = 0x00;
129         sndbuf[6] = 0x02;
130
131         DEBUGR("num_bytes=%u: ", num_bytes);
132         if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, &retlen) == 0) {
133                 DEBUGRC("%u [%s]\n", retlen,
134                         rfid_hexdump(recvbuf+1, num_bytes));
135                 memcpy(buf, recvbuf+1, num_bytes); // len == 0x7f
136                 return 0;
137         }
138
139         DEBUGRC("ERROR\n");
140         return -1;
141 }
142
143 static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath,
144                              unsigned char len,
145                              const unsigned char *bytes,
146                              unsigned char flags)
147 {
148         unsigned char sndbuf[SENDBUF_LEN];
149         unsigned char recvbuf[0x7f];
150         unsigned int retlen = sizeof(recvbuf);
151
152         sndbuf[0] = 0x20;
153         sndbuf[1] = 0x00;
154         sndbuf[2] = len;
155         sndbuf[3] = 0x00;
156         sndbuf[4] = 0x00;
157         sndbuf[5] = flags;
158         sndbuf[6] = 0x02;
159
160         DEBUGR("%u [%s]: ", len, rfid_hexdump(bytes, len));
161
162         memcpy(sndbuf+7, bytes, len);
163
164         if (PC_to_RDR_Escape(rath->data, sndbuf, len+7, recvbuf, &retlen) == 0) {
165                 DEBUGRC("OK (%u [%s])\n", retlen, rfid_hexdump(recvbuf, retlen));
166                 return 0;
167         }
168
169         DEBUGRC("ERROR\n");
170         return -1;
171 }
172
173 #if 0
174 static int TestFIFO(struct rc632_handle *handle)
175 {
176         unsigned char sndbuf[60]; // 0x3c
177
178         // FIXME: repne stosd, call
179
180         memset(sndbuf, 0, sizeof(sndbuf));
181
182         if (WriteNBytesToFIFO(handle, sizeof(sndbuf), sndbuf, 0) < 0)
183                 return -1;
184
185         return ReadNBytesFromFIFO(handle, sizeof(sndbuf), sndbuf);
186 }
187 #endif
188
189 static int cm5121_transceive(struct rfid_reader_handle *rh,
190                              enum rfid_frametype frametype,
191                              const unsigned char *tx_data, unsigned int tx_len,
192                              unsigned char *rx_data, unsigned int *rx_len,
193                              u_int64_t timeout, unsigned int flags)
194 {
195         return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
196                                                 tx_data, tx_len, rx_data,
197                                                 rx_len, timeout, flags);
198 }
199
200 static int cm5121_transceive_sf(struct rfid_reader_handle *rh,
201                                unsigned char cmd, struct iso14443a_atqa *atqa)
202 {
203         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
204                                                                    cmd,
205                                                                    atqa);
206 }
207
208 static int
209 cm5121_transceive_acf(struct rfid_reader_handle *rh,
210                       struct iso14443a_anticol_cmd *cmd,
211                       unsigned int *bit_of_col)
212 {
213         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
214                                                          cmd, bit_of_col);
215 }
216
217 static int
218 cm5121_14443a_init(struct rfid_reader_handle *rh)
219 {
220         return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
221 }
222
223 static int
224 cm5121_14443a_set_speed(struct rfid_reader_handle *rh, 
225                         unsigned int tx,
226                         unsigned int speed)
227 {
228         u_int8_t rate;
229         
230         DEBUGP("setting rate: ");
231         switch (speed) {
232         case RFID_14443A_SPEED_106K:
233                 rate = 0x00;
234                 DEBUGPC("106K\n");
235                 break;
236         case RFID_14443A_SPEED_212K:
237                 rate = 0x01;
238                 DEBUGPC("212K\n");
239                 break;
240         case RFID_14443A_SPEED_424K:
241                 rate = 0x02;
242                 DEBUGPC("424K\n");
243                 break;
244         case RFID_14443A_SPEED_848K:
245                 rate = 0x03;
246                 DEBUGPC("848K\n");
247                 break;
248         default:
249                 DEBUGPC("invalid\n");
250                 return -EINVAL;
251                 break;
252         }
253         return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
254                                                                 tx, rate);
255 }
256
257 static int
258 cm5121_14443b_init(struct rfid_reader_handle *rh)
259 {
260         return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
261 }
262
263 static int
264 cm5121_15693_init(struct rfid_reader_handle *rh)
265 {
266         return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
267 }
268
269 static int
270 cm5121_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
271 {
272         return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
273 }
274
275 static int
276 cm5121_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
277                    u_int32_t serno, u_int8_t block)
278 {
279         return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah, 
280                                                         cmd, serno, block);
281 }
282
283 struct rfid_asic_transport cm5121_ccid = {
284         .name = "CM5121 OpenCT",
285         .priv.rc632 = {
286                 .fn = {
287                         .reg_write      = &Write1ByteToReg,
288                         .reg_read       = &Read1ByteFromReg,
289                         .fifo_write     = &WriteNBytesToFIFO,
290                         .fifo_read      = &ReadNBytesFromFIFO,
291                 },
292         },
293 };
294
295 static int cm5121_enable_rc632(struct rfid_asic_transport_handle *rath)
296 {
297         unsigned char tx_buf[1] = { 0x01 };     
298         unsigned char rx_buf[64];
299         unsigned int rx_len = sizeof(rx_buf);
300
301         PC_to_RDR_Escape(rath->data, tx_buf, 1, rx_buf, &rx_len);
302         printf("received %u bytes from 01 command\n", rx_len);
303
304         return 0;
305 }
306
307 static struct rfid_reader_handle *
308 cm5121_open(void *data)
309 {
310         struct rfid_reader_handle *rh;
311         struct rfid_asic_transport_handle *rath;
312
313         rh = malloc(sizeof(*rh));
314         if (!rh)
315                 return NULL;
316         memset(rh, 0, sizeof(*rh));
317
318         rath = malloc(sizeof(*rath));
319         if (!rath)
320                 goto out_rh;
321         memset(rath, 0, sizeof(*rath));
322
323         rath->rat = &cm5121_ccid;
324         rh->reader = &rfid_reader_cm5121;
325
326         if (cm5121_source_init(rath) < 0)
327                 goto out_rath;
328
329         if (cm5121_enable_rc632(rath) < 0)
330                 goto out_rath;
331
332         rh->ah = rc632_open(rath);
333         if (!rh->ah) 
334                 goto out_rath;
335
336         DEBUGP("returning %p\n", rh);
337         return rh;
338
339 out_rath:
340         free(rath);
341 out_rh:
342         free(rh);
343
344         return NULL;
345 }
346
347 static void
348 cm5121_close(struct rfid_reader_handle *rh)
349 {
350         struct rfid_asic_transport_handle *rath = rh->ah->rath;
351         rc632_close(rh->ah);
352         free(rath);
353         free(rh);
354 }
355
356 struct rfid_reader rfid_reader_cm5121 = {
357         .name   = "Omnikey CardMan 5121 RFID",
358         .open = &cm5121_open,
359         .close = &cm5121_close,
360         .transceive = &cm5121_transceive,
361         .iso14443a = {
362                 .init = &cm5121_14443a_init,
363                 .transceive_sf = &cm5121_transceive_sf,
364                 .transceive_acf = &cm5121_transceive_acf,
365                 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
366                          RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
367                 .set_speed = &cm5121_14443a_set_speed,
368         },
369         .iso14443b = {
370                 .init = &cm5121_14443b_init,
371         },
372         .mifare_classic = {
373                 .setkey = &cm5121_mifare_setkey,
374                 .auth = &cm5121_mifare_auth,
375         },
376 };
377
378