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