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