* implement rfid_reader_{get,set}opt()
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
28  */
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #define DEBUG_LIBRFID
35 #include <librfid/rfid.h>
36
37 #ifndef LIBRFID_FIRMWARE
38
39
40 #include <librfid/rfid_reader.h>
41 #include <librfid/rfid_asic.h>
42 #include <librfid/rfid_asic_rc632.h>
43 #include <librfid/rfid_reader_cm5121.h>
44 #include <librfid/rfid_layer2.h>
45 #include <librfid/rfid_protocol.h>
46
47 #include "rfid_reader_rc632_common.h"
48
49 #include "cm5121_source.h"
50
51 /* FIXME */
52 #include "rc632.h"
53
54 #define SENDBUF_LEN     256+7+10 /* 256bytes max FSD/FSC, plus 7 bytes header,
55                                     plus 10 bytes reserve */
56 #define RECVBUF_LEN     SENDBUF_LEN
57
58 #ifdef DEBUG_REGISTER
59 #define DEBUGRC DEBUGPC
60 #define DEBUGR DEBUGP
61 #else
62 #define DEBUGRC(x, args ...)    do {} while(0)
63 #define DEBUGR(x, args ...)     do {} while(0)
64 #endif
65
66 static
67 int Write1ByteToReg(struct rfid_asic_transport_handle *rath,
68                     unsigned char reg, unsigned char value)
69 {
70         unsigned char sndbuf[SENDBUF_LEN];
71         unsigned char rcvbuf[RECVBUF_LEN];
72         size_t retlen = RECVBUF_LEN;
73
74         sndbuf[0] = 0x20;
75         sndbuf[1] = 0x00;
76         sndbuf[2] = 0x01;
77         sndbuf[3] = 0x00;
78         sndbuf[4] = 0x00;
79         sndbuf[5] = 0x00;
80         sndbuf[6] = reg;
81         sndbuf[7] = value;
82
83         DEBUGR("reg=0x%02x, val=%02x: ", reg, value);
84
85         if (PC_to_RDR_Escape(rath->data, sndbuf, 8, rcvbuf, 
86                              &retlen) == 0) {
87                 DEBUGRC("OK\n");
88                 return 0;
89         }
90
91         DEBUGRC("ERROR\n");
92         return -1;
93 }
94
95 static int Read1ByteFromReg(struct rfid_asic_transport_handle *rath,
96                             unsigned char reg,
97                             unsigned char *value)
98 {
99         unsigned char sndbuf[SENDBUF_LEN];
100         unsigned char recvbuf[RECVBUF_LEN];
101         size_t retlen = sizeof(recvbuf);
102
103         sndbuf[0] = 0x20;
104         sndbuf[1] = 0x00;
105         sndbuf[2] = 0x00;
106         sndbuf[3] = 0x00;
107         sndbuf[4] = 0x01;
108         sndbuf[5] = 0x00;
109         sndbuf[6] = reg;
110
111         if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, 
112                              &retlen) == 0) {
113                 *value = recvbuf[1];
114                 DEBUGR("reg=0x%02x, val=%02x: ", reg, *value);
115                 DEBUGRC("OK\n");
116                 return 0;
117         }
118
119         DEBUGRC("ERROR\n");
120         return -1;
121 }
122
123 static int ReadNBytesFromFIFO(struct rfid_asic_transport_handle *rath,
124                               unsigned char num_bytes,
125                               unsigned char *buf)
126 {
127         unsigned char sndbuf[SENDBUF_LEN];
128         unsigned char recvbuf[0x7f];
129         size_t retlen = sizeof(recvbuf);
130
131         sndbuf[0] = 0x20;
132         sndbuf[1] = 0x00;
133         sndbuf[2] = 0x00;
134         sndbuf[3] = 0x00;
135         sndbuf[4] = num_bytes;
136         sndbuf[5] = 0x00;
137         sndbuf[6] = 0x02;
138
139         DEBUGR("num_bytes=%u: ", num_bytes);
140         if (PC_to_RDR_Escape(rath->data, sndbuf, 7, recvbuf, &retlen) == 0) {
141                 DEBUGRC("%u [%s]\n", retlen,
142                         rfid_hexdump(recvbuf+1, num_bytes));
143                 memcpy(buf, recvbuf+1, num_bytes); // len == 0x7f
144                 return 0;
145         }
146
147         DEBUGRC("ERROR\n");
148         return -1;
149 }
150
151 static int WriteNBytesToFIFO(struct rfid_asic_transport_handle *rath,
152                              unsigned char len,
153                              const unsigned char *bytes,
154                              unsigned char flags)
155 {
156         unsigned char sndbuf[SENDBUF_LEN];
157         unsigned char recvbuf[0x7f];
158         size_t retlen = sizeof(recvbuf);
159
160         sndbuf[0] = 0x20;
161         sndbuf[1] = 0x00;
162         sndbuf[2] = len;
163         sndbuf[3] = 0x00;
164         sndbuf[4] = 0x00;
165         sndbuf[5] = flags;
166         sndbuf[6] = 0x02;
167
168         DEBUGR("%u [%s]: ", len, rfid_hexdump(bytes, len));
169
170         memcpy(sndbuf+7, bytes, len);
171
172         if (PC_to_RDR_Escape(rath->data, sndbuf, len+7, recvbuf, &retlen) == 0) {
173                 DEBUGRC("OK (%u [%s])\n", retlen, rfid_hexdump(recvbuf, retlen));
174                 return 0;
175         }
176
177         DEBUGRC("ERROR\n");
178         return -1;
179 }
180
181 struct rfid_asic_transport cm5121_ccid = {
182         .name = "CM5121 OpenCT",
183         .priv.rc632 = {
184                 .fn = {
185                         .reg_write      = &Write1ByteToReg,
186                         .reg_read       = &Read1ByteFromReg,
187                         .fifo_write     = &WriteNBytesToFIFO,
188                         .fifo_read      = &ReadNBytesFromFIFO,
189                 },
190         },
191 };
192
193 static int cm5121_enable_rc632(struct rfid_asic_transport_handle *rath)
194 {
195         unsigned char tx_buf[1] = { 0x01 };     
196         unsigned char rx_buf[64];
197         size_t rx_len = sizeof(rx_buf);
198
199         PC_to_RDR_Escape(rath->data, tx_buf, 1, rx_buf, &rx_len);
200
201         return 0;
202 }
203
204 static struct rfid_reader_handle *
205 cm5121_open(void *data)
206 {
207         struct rfid_reader_handle *rh;
208         struct rfid_asic_transport_handle *rath;
209
210         rh = malloc_reader_handle(sizeof(*rh));
211         if (!rh)
212                 return NULL;
213         memset(rh, 0, sizeof(*rh));
214
215         rath = malloc_rat_handle(sizeof(*rath));
216         if (!rath)
217                 goto out_rh;
218         memset(rath, 0, sizeof(*rath));
219
220         rath->rat = &cm5121_ccid;
221         rh->reader = &rfid_reader_cm5121;
222
223         if (cm5121_source_init(rath) < 0)
224                 goto out_rath;
225
226         if (cm5121_enable_rc632(rath) < 0)
227                 goto out_rath;
228
229         rh->ah = rc632_open(rath);
230         if (!rh->ah) 
231                 goto out_rath;
232
233         DEBUGP("returning %p\n", rh);
234         return rh;
235
236 out_rath:
237         free_rat_handle(rath);
238 out_rh:
239         free_reader_handle(rh);
240
241         return NULL;
242 }
243
244 static void
245 cm5121_close(struct rfid_reader_handle *rh)
246 {
247         struct rfid_asic_transport_handle *rath = rh->ah->rath;
248         rc632_close(rh->ah);
249         free_rat_handle(rath);
250         free_reader_handle(rh);
251 }
252
253 const struct rfid_reader rfid_reader_cm5121 = {
254         .name   = "Omnikey CardMan 5121 RFID",
255         .open = &cm5121_open,
256         .close = &cm5121_close,
257         .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
258                         (1 << RFID_LAYER2_ISO14443B) |
259                         (1 << RFID_LAYER2_ISO15693),
260         .proto_supported = (1 << RFID_PROTOCOL_TCL) |
261                         (1 << RFID_PROTOCOL_MIFARE_UL) |
262                         (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
263         .getopt = &_rdr_rc632_getopt,
264         .setopt = &_rdr_rc632_setopt,
265         .transceive = &_rdr_rc632_transceive,
266         .init = &_rdr_rc632_l2_init,
267         .iso14443a = {
268                 .transceive_sf = &_rdr_rc632_transceive_sf,
269                 .transceive_acf = &_rdr_rc632_transceive_acf,
270                 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
271                          RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
272                 .set_speed = &_rdr_rc632_14443a_set_speed,
273         },
274         .iso15693 = {
275                 .transceive_ac = &_rdr_rc632_iso15693_transceive_ac,
276         },
277         .mifare_classic = {
278                 .setkey = &_rdr_rc632_mifare_setkey,
279                 .auth = &_rdr_rc632_mifare_auth,
280         },
281 };
282
283 #endif /* LIBRFID_FIRMWARE */