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