make "frametype" a parameter of transcieve functions
[librfid] / rfid_asic_rc632_14443a.c
1 /* 
2  * Philips CL RC632 primitives for ISO 14443-A compliant PICC's
3  *
4  * (C) 2005 by Harald Welte <laforge@gnumonks.org>
5  *
6  */
7
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include <rfid/rfid.h>
13 #include <rfid/rfid_asic_rc632.h>
14 #include <rfid/rfid_layer2_iso14443a.h>
15
16 #include "rc632.h"
17
18 #include "cm5121_rfid.h"        /* FIXME: this needs to be modular */
19
20 static int
21 rc632_iso14443a_init(struct rfid_asic_handle *handle)
22 {
23         int ret;
24
25 #if 0
26         ret = rc632_power_up(handle);
27         if (ret < 0)
28                 return ret;
29
30         ret = rc632_turn_on_rf(handle);
31         if (ret < 0)
32                 return ret;
33 #endif
34
35         // FIXME: some fifo work (drain fifo?)
36         
37         /* flush fifo (our way) */
38         ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
39
40         ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
41                         (RC632_TXCTRL_TX1_RF_EN |
42                          RC632_TXCTRL_TX2_RF_EN |
43                          RC632_TXCTRL_TX2_INV |
44                          RC632_TXCTRL_FORCE_100_ASK |
45                          RC632_TXCTRL_MOD_SRC_INT));
46         if (ret < 0)
47                 return ret;
48
49         ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE,
50                                 CM5121_CW_CONDUCTANCE);
51         if (ret < 0)
52                 return ret;
53
54         /* Since FORCE_100_ASK is set (cf mc073930.pdf), this line may be left out? */
55         ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE,
56                                 CM5121_MOD_CONDUCTANCE);
57         if (ret < 0)
58                 return ret;
59
60         ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
61                                 (RC632_CDRCTRL_TXCD_14443A |
62                                  RC632_CDRCTRL_RATE_106K));
63         if (ret < 0)
64                 return ret;
65
66         ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
67         if (ret < 0)
68                 return ret;
69
70         ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
71         if (ret < 0)
72                 return ret;
73
74         ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING, 0x00);
75         if (ret < 0)
76                 return ret;
77
78         ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
79                               (RC632_RXCTRL1_GAIN_35DB |
80                                RC632_RXCTRL1_ISO14443 |
81                                RC632_RXCTRL1_SUBCP_8));
82         if (ret < 0)
83                 return ret;
84
85         ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
86                               (RC632_DECCTRL_MANCHESTER |
87                                RC632_DECCTRL_RXFR_14443A));
88         if (ret < 0)
89                 return ret;
90
91         ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
92                                 CM5121_14443A_BITPHASE);
93         if (ret < 0)
94                 return ret;
95
96         ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
97                                 CM5121_14443A_THRESHOLD);
98         if (ret < 0)
99                 return ret;
100
101         ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL, 0x00);
102         if (ret < 0)
103                 return ret;
104                               
105         ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
106                               (RC632_RXCTRL2_DECSRC_INT |
107                                RC632_RXCTRL2_CLK_Q));
108         if (ret < 0)
109                 return ret;
110
111         ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
112         if (ret < 0)
113                 return ret;
114
115         ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
116                               (RC632_CR_PARITY_ENABLE |
117                                RC632_CR_PARITY_ODD));
118         if (ret < 0)
119                 return ret;
120
121         ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x63);
122         if (ret < 0)
123                 return ret;
124
125         ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0x63);
126         if (ret < 0)
127                 return ret;
128
129         return 0;
130 }
131
132 static int
133 rc632_iso14443a_fini(struct iso14443a_handle *handle_14443)
134 {
135
136 #if 0
137         ret = rc632_turn_off_rf(handle);
138         if (ret < 0)
139                 return ret;
140 #endif
141
142
143         return 0;
144 }
145
146 #if 0
147 int
148 rc632_iso14443a_select(struct rc632_handle *handle,
149                 unsigned char *retptr,
150         )
151 {
152         int ret;
153         unsigned char tx_buf[7];
154         unsigned char rx_buf[64];
155         unsigned char rx_len = 1;
156
157         memset(rx_buf, 0, sizeof(rx_buf));
158
159         tx_buf[0] = arg_8;
160         tx_buf[1] = 0x70;
161         (u_int32_t *)tx_buf[2] = arg_4;
162         tx_buf[6] = arg4+4;
163
164         /* disable mifare cryto */
165         ret = rc632_clear_bit(handle, RC632_REG_CONTROL,
166                                 RC632_CONTROL_CRYPTO1_ON);
167         if (ret < 0)
168                 return ret;
169
170         ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
171                                 (RC632_CR_PARITY_ENABLE |
172                                  RC632_CR_PARITY_ODD |
173                                  RC632_CR_TX_CRC_ENABLE |
174                                  RC632_CR_RX_CRC_ENABLE));
175         if (ret < 0)
176                 return ret;
177
178         ret = rc632_transcieve(handle, tx_buf, sizeof(tx_buf),
179                                 rx_buf, &rx_len, 0x32, 0);
180
181         if (ret < 0 || rx_len != 1)
182                 return ret;
183
184         *retptr = rx_buf[0];
185
186         return 0;
187 }
188
189 /* issue a 14443-3 A PCD -> PICC command, such as REQA, WUPA */
190 int
191 rc632_iso14443a_req(sutruct rc632_handle *handle, unsigned char req,
192                     unsigned char *resp)
193 {
194         int ret;
195         unsigned char tx_buf[1];
196         unsigned char rx_buf[0x40];
197         unsigned char rx_len = 2;
198
199         memset(rx_buf, 0, sizeof(rx_buf));
200
201         tx_buf[0] = req;
202
203         /* transfer only 7 bits of last byte in frame */
204         ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
205         if (ret < 0)
206                 return ret;
207
208         
209         ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
210                                 RC632_CONTROL_CRYPTO1_ON);
211         if (ret < 0)
212                 return ret;
213
214         ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
215                                 (RC632_CR_PARITY_ENABLE |
216                                  RC632_CR_PARITY_ODD));
217         if (ret < 0)
218                 return ret;
219
220         ret = rc632_transcieve(handle, tx_buf, sizeof(tx_buf), rx_buf,
221                                 &rx_len, 0x32, 0);
222         if (ret < 0)
223                 return ret;
224
225         /* switch back to normal 8bit last byte */
226         ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
227         if (ret < 0)
228                 return ret;
229
230         if ((rx_len != 2) || (rx_buf[1] != 0xf0))
231                 return -1;
232
233         resp[0] = rx_buf[0];
234         resp[1] = rx_buf[1];
235         
236         return 0;
237 }
238 #endif
239
240 /* issue a 14443-3 A PCD -> PICC command in a short frame, such as REQA, WUPA */
241 static int
242 rc632_iso14443a_transcieve_sf(struct rfid_asic_handle *handle,
243                                 unsigned char cmd,
244                                 struct iso14443a_atqa *atqa)
245 {
246         int ret;
247         unsigned char tx_buf[1];
248         unsigned char rx_len = 2;
249
250         memset(atqa, 0, sizeof(atqa));
251
252         tx_buf[0] = cmd;
253
254         /* transfer only 7 bits of last byte in frame */
255         ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
256         if (ret < 0)
257                 return ret;
258
259         
260         ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
261                                 RC632_CONTROL_CRYPTO1_ON);
262         if (ret < 0)
263                 return ret;
264
265         ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
266                                 (RC632_CR_PARITY_ENABLE |
267                                  RC632_CR_PARITY_ODD));
268         if (ret < 0)
269                 return ret;
270
271         ret = rc632_transcieve(handle, tx_buf, sizeof(tx_buf),
272                                 (unsigned char *)atqa, &rx_len, 0x32, 0);
273         if (ret < 0) {
274                 DEBUGP("error during rc632_transcieve()\n");
275                 return ret;
276         }
277
278         /* switch back to normal 8bit last byte */
279         ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
280         if (ret < 0)
281                 return ret;
282
283         if (rx_len != 2) {
284                 DEBUGP("rx_len(%d) != 2\n", rx_len);
285                 return -1;
286         }
287
288         return 0;
289 }
290
291 /* transcieve regular frame */
292 static int
293 rc632_iso14443a_transcieve(struct rfid_asic_handle *handle,
294                            const unsigned char *tx_buf, unsigned int tx_len,
295                            unsigned char *rx_buf, unsigned int *rx_len,
296                            unsigned int timeout, unsigned int flags)
297 {
298         int ret;
299         unsigned char rxl = *rx_len & 0xff;
300
301         memset(rx_buf, 0, *rx_len);
302
303         ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
304                                 (RC632_CR_PARITY_ENABLE |
305                                  RC632_CR_PARITY_ODD |
306                                  RC632_CR_TX_CRC_ENABLE |
307                                  RC632_CR_RX_CRC_ENABLE));
308         if (ret < 0)
309                 return ret;
310
311         ret = rc632_transcieve(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
312         *rx_len = rxl;
313         if (ret < 0)
314                 return ret;
315
316
317         return 0; 
318 }
319
320 /* transcieve anti collission bitframe */
321 static int
322 rc632_iso14443a_transcieve_acf(struct rfid_asic_handle *handle,
323                                 struct iso14443a_anticol_cmd *acf,
324                                 unsigned int *bit_of_col)
325 {
326         int ret;
327         unsigned char rx_buf[64];
328         unsigned char rx_len = sizeof(rx_buf);
329         unsigned char rx_align = 0, tx_last_bits, tx_bytes;
330         unsigned char boc;
331         unsigned char error_flag;
332         *bit_of_col = ISO14443A_BITOFCOL_NONE;
333         memset(rx_buf, 0, sizeof(rx_buf));
334
335         /* disable mifare cryto */
336         ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
337                                 RC632_CONTROL_CRYPTO1_ON);
338         if (ret < 0)
339                 return ret;
340
341         /* disalbe CRC summing */
342         ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
343                                 (RC632_CR_PARITY_ENABLE |
344                                  RC632_CR_PARITY_ODD));
345         if (ret < 0)
346                 return ret;
347
348         tx_last_bits = acf->nvb & 0x0f; /* lower nibble indicates bits */
349         tx_bytes = acf->nvb >> 4;
350         if (tx_last_bits) {
351                 tx_bytes++;
352                 rx_align = (tx_last_bits+1) % 8;/* rx frame complements tx */
353         }
354
355         //rx_align = 8 - tx_last_bits;/* rx frame complements tx */
356
357         /* set RxAlign and TxLastBits*/
358         ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING,
359                                 (rx_align << 4) | (tx_last_bits));
360         if (ret < 0)
361                 return ret;
362
363         ret = rc632_transcieve(handle, (unsigned char *)acf, tx_bytes,
364                                 rx_buf, &rx_len, 0x32, 0);
365         if (ret < 0)
366                 return ret;
367
368         /* bitwise-OR the two halves of the split byte */
369         acf->uid_bits[tx_bytes-2] = (
370                   (acf->uid_bits[tx_bytes-2] & (0xff >> (8-tx_last_bits)))
371                 | rx_buf[0]);
372         /* copy the rest */
373         memcpy(&acf->uid_bits[tx_bytes+1-2], &rx_buf[1], rx_len-1);
374
375         /* determine whether there was a collission */
376         ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
377         if (ret < 0)
378                 return ret;
379
380         if (error_flag & RC632_ERR_FLAG_COL_ERR) {
381                 /* retrieve bit of collission */
382                 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
383                 if (ret < 0)
384                         return ret;
385
386                 /* bit of collission relative to start of part 1 of 
387                  * anticollision frame (!) */
388                 *bit_of_col = 2*8 + boc;
389         }
390
391         return 0;
392 }
393
394