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