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