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