2 * Philips CL RC632 primitives for ISO 14443-A compliant PICC's
4 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
12 #include <rfid/rfid.h>
13 #include <rfid/rfid_asic_rc632.h>
14 #include <rfid/rfid_layer2_iso14443a.h>
18 #include "cm5121_rfid.h" /* FIXME: this needs to be modular */
21 rc632_iso14443a_init(struct rfid_asic_handle *handle)
26 ret = rc632_power_up(handle);
30 ret = rc632_turn_on_rf(handle);
35 // FIXME: some fifo work (drain fifo?)
37 /* flush fifo (our way) */
38 ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
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));
49 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE,
50 CM5121_CW_CONDUCTANCE);
54 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE,
55 CM5121_MOD_CONDUCTANCE);
59 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
60 (RC632_CDRCTRL_TXCD_14443A |
61 RC632_CDRCTRL_RATE_106K));
65 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
69 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
73 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING, 0x00);
77 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
78 (RC632_RXCTRL1_GAIN_35DB |
79 RC632_RXCTRL1_ISO14443 |
80 RC632_RXCTRL1_SUBCP_8));
84 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
85 (RC632_DECCTRL_MANCHESTER |
86 RC632_DECCTRL_RXFR_14443A));
90 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
91 CM5121_14443A_BITPHASE);
95 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
96 CM5121_14443A_THRESHOLD);
100 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL, 0x00);
104 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
105 (RC632_RXCTRL2_DECSRC_INT |
106 RC632_RXCTRL2_CLK_Q));
110 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
114 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
115 (RC632_CR_PARITY_ENABLE |
116 RC632_CR_PARITY_ODD));
120 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x63);
124 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0x63);
132 rc632_iso14443a_fini(struct iso14443a_handle *handle_14443)
136 ret = rc632_turn_off_rf(handle);
147 rc632_iso14443a_select(struct rc632_handle *handle,
148 unsigned char *retptr,
152 unsigned char tx_buf[7];
153 unsigned char rx_buf[64];
154 unsigned char rx_len = 1;
156 memset(rx_buf, 0, sizeof(rx_buf));
160 (u_int32_t *)tx_buf[2] = arg_4;
163 /* disable mifare cryto */
164 ret = rc632_clear_bit(handle, RC632_REG_CONTROL,
165 RC632_CONTROL_CRYPTO1_ON);
169 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
170 (RC632_CR_PARITY_ENABLE |
171 RC632_CR_PARITY_ODD |
172 RC632_CR_TX_CRC_ENABLE |
173 RC632_CR_RX_CRC_ENABLE));
177 ret = rc632_transcieve(handle, tx_buf, sizeof(tx_buf),
178 rx_buf, &rx_len, 0x32, 0);
180 if (ret < 0 || rx_len != 1)
188 /* issue a 14443-3 A PCD -> PICC command, such as REQA, WUPA */
190 rc632_iso14443a_req(sutruct rc632_handle *handle, unsigned char req,
194 unsigned char tx_buf[1];
195 unsigned char rx_buf[0x40];
196 unsigned char rx_len = 2;
198 memset(rx_buf, 0, sizeof(rx_buf));
202 /* transfer only 7 bits of last byte in frame */
203 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
208 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
209 RC632_CONTROL_CRYPTO1_ON);
213 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
214 (RC632_CR_PARITY_ENABLE |
215 RC632_CR_PARITY_ODD));
219 ret = rc632_transcieve(handle, tx_buf, sizeof(tx_buf), rx_buf,
224 /* switch back to normal 8bit last byte */
225 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
229 if ((rx_len != 2) || (rx_buf[1] != 0xf0))
239 /* issue a 14443-3 A PCD -> PICC command in a short frame, such as REQA, WUPA */
241 rc632_iso14443a_transcieve_sf(struct rfid_asic_handle *handle,
243 struct iso14443a_atqa *atqa)
246 unsigned char tx_buf[1];
247 unsigned char rx_len = 2;
249 memset(atqa, 0, sizeof(atqa));
253 /* transfer only 7 bits of last byte in frame */
254 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
259 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
260 RC632_CONTROL_CRYPTO1_ON);
264 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
265 (RC632_CR_PARITY_ENABLE |
266 RC632_CR_PARITY_ODD));
270 ret = rc632_transcieve(handle, tx_buf, sizeof(tx_buf),
271 (unsigned char *)atqa, &rx_len, 0x32, 0);
273 DEBUGP("error during rc632_transcieve()\n");
277 /* switch back to normal 8bit last byte */
278 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
283 DEBUGP("rx_len(%d) != 2\n", rx_len);
290 /* trasncieve regular frame */
292 rc632_iso14443a_transcieve(struct rfid_asic_handle *handle,
293 const unsigned char *tx_buf, unsigned int tx_len,
294 unsigned char *rx_buf, unsigned int *rx_len,
295 unsigned int timeout, unsigned int flags)
298 unsigned char rxl = *rx_len & 0xff;
300 memset(rx_buf, 0, *rx_len);
302 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
303 (RC632_CR_PARITY_ENABLE |
304 RC632_CR_PARITY_ODD |
305 RC632_CR_TX_CRC_ENABLE |
306 RC632_CR_RX_CRC_ENABLE));
310 ret = rc632_transcieve(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
319 /* transcieve anti collission bitframe */
321 rc632_iso14443a_transcieve_acf(struct rfid_asic_handle *handle,
322 struct iso14443a_anticol_cmd *acf,
323 unsigned int *bit_of_col)
326 unsigned char rx_buf[64];
327 unsigned char rx_len = sizeof(rx_buf);
328 unsigned char rx_align = 0, tx_last_bits, tx_bytes;
330 unsigned char error_flag;
331 *bit_of_col = ISO14443A_BITOFCOL_NONE;
332 memset(rx_buf, 0, sizeof(rx_buf));
334 /* disable mifare cryto */
335 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
336 RC632_CONTROL_CRYPTO1_ON);
340 /* disalbe CRC summing */
341 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
342 (RC632_CR_PARITY_ENABLE |
343 RC632_CR_PARITY_ODD));
347 tx_last_bits = acf->nvb & 0x0f; /* lower nibble indicates bits */
348 tx_bytes = acf->nvb >> 4;
351 rx_align = (tx_last_bits+1) % 8;/* rx frame complements tx */
354 //rx_align = 8 - tx_last_bits;/* rx frame complements tx */
356 /* set RxAlign and TxLastBits*/
357 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING,
358 (rx_align << 4) | (tx_last_bits));
362 ret = rc632_transcieve(handle, (unsigned char *)acf, tx_bytes,
363 rx_buf, &rx_len, 0x32, 0);
367 /* bitwise-OR the two halves of the split byte */
368 acf->uid_bits[tx_bytes-2] = (
369 (acf->uid_bits[tx_bytes-2] & (0xff >> (8-tx_last_bits)))
372 memcpy(&acf->uid_bits[tx_bytes+1-2], &rx_buf[1], rx_len-1);
374 /* determine whether there was a collission */
375 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
379 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
380 /* retrieve bit of collission */
381 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
385 /* bit of collission relative to start of part 1 of
386 * anticollision frame (!) */
387 *bit_of_col = 2*8 + boc;