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