1 /* Generic Philips CL RC632 Routines
3 * (C) Harald Welte <laforge@gnumonks.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/types.h>
28 #include <librfid/rfid.h>
29 #include <librfid/rfid_asic.h>
30 #include <librfid/rfid_asic_rc632.h>
31 #include <librfid/rfid_reader_cm5121.h>
32 #include <librfid/rfid_layer2_iso14443a.h>
33 #include <librfid/rfid_protocol_mifare_classic.h>
35 #include "rfid_iso14443_common.h"
37 //#include "rc632_14443a.h"
40 #define RC632_TMO_AUTH1 14000
42 #define ENTER() DEBUGP("entering\n")
43 struct rfid_asic rc632;
45 /* Register and FIFO Access functions */
47 rc632_reg_write(struct rfid_asic_handle *handle,
51 return handle->rath->rat->priv.rc632.fn.reg_write(handle->rath, reg, val);
55 rc632_reg_read(struct rfid_asic_handle *handle,
59 return handle->rath->rat->priv.rc632.fn.reg_read(handle->rath, reg, val);
63 rc632_fifo_write(struct rfid_asic_handle *handle,
68 return handle->rath->rat->priv.rc632.fn.fifo_write(handle->rath,
73 rc632_fifo_read(struct rfid_asic_handle *handle,
77 return handle->rath->rat->priv.rc632.fn.fifo_read(handle->rath, len, buf);
82 rc632_set_bits(struct rfid_asic_handle *handle,
89 ret = rc632_reg_read(handle, reg, &tmp);
93 /* if bits are already set, no need to set them again */
94 if ((tmp & val) == val)
97 return rc632_reg_write(handle, reg, (tmp|val)&0xff);
100 rc632_set_bit_mask(struct rfid_asic_handle *handle,
101 u_int8_t reg, u_int8_t mask, u_int8_t val)
106 ret = rc632_reg_read(handle, reg, &tmp);
110 /* if bits are already like we want them, abort */
111 if ((tmp & mask) == val)
114 return rc632_reg_write(handle, reg, (tmp & ~mask)|(val & mask));
118 rc632_clear_bits(struct rfid_asic_handle *handle,
125 ret = rc632_reg_read(handle, reg, &tmp);
127 DEBUGP("error during reg_read(%p, %d):%d\n",
131 /* if bits are already cleared, no need to clear them again */
132 if ((tmp & val) == 0)
135 return rc632_reg_write(handle, reg, (tmp & ~val)&0xff);
139 rc632_turn_on_rf(struct rfid_asic_handle *handle)
142 return rc632_set_bits(handle, RC632_REG_TX_CONTROL, 0x03);
146 rc632_turn_off_rf(struct rfid_asic_handle *handle)
149 return rc632_clear_bits(handle, RC632_REG_TX_CONTROL, 0x03);
153 rc632_power_up(struct rfid_asic_handle *handle)
156 return rc632_clear_bits(handle, RC632_REG_CONTROL,
157 RC632_CONTROL_POWERDOWN);
161 rc632_power_down(struct rfid_asic_handle *handle)
163 return rc632_set_bits(handle, RC632_REG_CONTROL,
164 RC632_CONTROL_POWERDOWN);
167 /* Stupid RC623 implementations don't evaluate interrupts but poll the
168 * command register for "status idle" */
170 rc632_wait_idle(struct rfid_asic_handle *handle, u_int64_t timeout)
174 #define USLEEP_PER_CYCLE 128
177 ret = rc632_reg_read(handle, RC632_REG_COMMAND, &cmd);
182 /* FIXME: read second time ?? */
188 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &foo);
190 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &foo);
193 /* Abort after some timeout */
194 if (cycles > timeout*10/USLEEP_PER_CYCLE) {
195 fprintf(stderr, "TIMEOUT!!\n");
200 usleep(USLEEP_PER_CYCLE);
207 rc632_transmit(struct rfid_asic_handle *handle,
214 ret = rc632_fifo_write(handle, len, buf, 0x03);
218 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_TRANSMIT);
222 return rc632_wait_idle(handle, timeout);
226 tcl_toggle_pcb(struct rfid_asic_handle *handle)
228 // FIXME: toggle something between 0x0a and 0x0b
233 rc632_transcieve(struct rfid_asic_handle *handle,
234 const u_int8_t *tx_buf,
243 ret = rc632_fifo_write(handle, tx_len, tx_buf, 0x03);
247 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_TRANSCIEVE);
252 tcl_toggle_pcb(handle);
254 ret = rc632_wait_idle(handle, timer);
258 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, rx_len);
265 DEBUGP("rx_len == 0\n");
267 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
268 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
273 return rc632_fifo_read(handle, *rx_len, rx_buf);
277 rc632_read_eeprom(struct rfid_asic_handle *handle)
279 u_int8_t recvbuf[60];
287 ret = rc632_fifo_write(handle, 3, sndbuf, 0x03);
291 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_READ_E2);
297 ret = rc632_fifo_read(handle, sizeof(recvbuf), recvbuf);
301 // FIXME: do something with eeprom contents
306 rc632_calc_crc16_from(struct rfid_asic_handle *handle)
308 u_int8_t sndbuf[2] = { 0x01, 0x02 };
309 u_int8_t crc_lsb = 0x00 , crc_msb = 0x00;
312 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x12);
316 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xe0);
320 ret = rc632_fifo_write(handle, sizeof(sndbuf), sndbuf, 3);
324 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_CALC_CRC);
328 usleep(10000); // FIXME: no checking for cmd completion?
330 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_LSB, &crc_lsb);
334 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_MSB, &crc_msb);
338 // FIXME: what to do with crc result?
344 rc632_register_dump(struct rfid_asic_handle *handle, u_int8_t *buf)
349 for (i = 0; i <= 0x3f; i++) {
350 ret = rc632_reg_read(handle, i, &buf[i]);
351 // do we want error checks?
358 /* generic FIFO access functions (if no more efficient ones provided by
359 * transport driver) */
364 // FIXME: implementation (not needed for CM 5121)
371 // FIXME: implementation (not neded for CM 5121)
376 rc632_init(struct rfid_asic_handle *ah)
380 /* switch off rf (make sure PICCs are reset at init time) */
381 ret = rc632_power_down(ah);
388 ret = rc632_power_up(ah);
392 /* disable register paging */
393 ret = rc632_reg_write(ah, 0x00, 0x00);
397 /* set some sane default values */
398 ret = rc632_reg_write(ah, 0x11, 0x5b);
403 ret = rc632_turn_on_rf(ah);
411 rc632_fini(struct rfid_asic_handle *ah)
416 ret = rc632_turn_off_rf(ah);
420 ret = rc632_power_down(ah);
427 struct rfid_asic_handle *
428 rc632_open(struct rfid_asic_transport_handle *th)
430 struct rfid_asic_handle *h;
432 h = malloc(sizeof(*h));
435 memset(h, 0, sizeof(*h));
440 h->mtu = h->mru = 40; /* FIXME */
442 if (rc632_init(h) < 0) {
451 rc632_close(struct rfid_asic_handle *h)
459 * Philips CL RC632 primitives for ISO 14443-A compliant PICC's
461 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
466 rc632_iso14443a_init(struct rfid_asic_handle *handle)
470 // FIXME: some fifo work (drain fifo?)
472 /* flush fifo (our way) */
473 ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
475 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
476 (RC632_TXCTRL_TX1_RF_EN |
477 RC632_TXCTRL_TX2_RF_EN |
478 RC632_TXCTRL_TX2_INV |
479 RC632_TXCTRL_FORCE_100_ASK |
480 RC632_TXCTRL_MOD_SRC_INT));
484 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE,
485 CM5121_CW_CONDUCTANCE);
489 /* Since FORCE_100_ASK is set (cf mc073930.pdf), this line may be left out? */
490 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE,
491 CM5121_MOD_CONDUCTANCE);
495 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
496 (RC632_CDRCTRL_TXCD_14443A |
497 RC632_CDRCTRL_RATE_106K));
501 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
505 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
509 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING, 0x00);
513 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
514 (RC632_RXCTRL1_GAIN_35DB |
515 RC632_RXCTRL1_ISO14443 |
516 RC632_RXCTRL1_SUBCP_8));
520 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
521 (RC632_DECCTRL_MANCHESTER |
522 RC632_DECCTRL_RXFR_14443A));
526 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
527 CM5121_14443A_BITPHASE);
531 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
532 CM5121_14443A_THRESHOLD);
536 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL, 0x00);
540 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
541 (RC632_RXCTRL2_DECSRC_INT |
542 RC632_RXCTRL2_CLK_Q));
546 /* Omnikey proprietary driver has 0x03, but 0x06 is the default reset value ?!? */
547 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x06);
551 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
552 (RC632_CR_PARITY_ENABLE |
553 RC632_CR_PARITY_ODD));
557 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x63);
561 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0x63);
569 rc632_iso14443a_fini(struct iso14443a_handle *handle_14443)
573 ret = rc632_turn_off_rf(handle);
583 /* issue a 14443-3 A PCD -> PICC command in a short frame, such as REQA, WUPA */
585 rc632_iso14443a_transcieve_sf(struct rfid_asic_handle *handle,
587 struct iso14443a_atqa *atqa)
593 memset(atqa, 0, sizeof(atqa));
597 /* transfer only 7 bits of last byte in frame */
598 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
602 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
603 RC632_CONTROL_CRYPTO1_ON);
608 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
609 (RC632_CR_PARITY_ENABLE |
610 RC632_CR_PARITY_ODD));
612 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
613 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
619 ret = rc632_transcieve(handle, tx_buf, sizeof(tx_buf),
620 (u_int8_t *)atqa, &rx_len, 0x32, 0);
622 DEBUGP("error during rc632_transcieve()\n");
626 /* switch back to normal 8bit last byte */
627 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
632 DEBUGP("rx_len(%d) != 2\n", rx_len);
639 /* transcieve regular frame */
641 rc632_iso14443ab_transcieve(struct rfid_asic_handle *handle,
642 unsigned int frametype,
643 const u_int8_t *tx_buf, unsigned int tx_len,
644 u_int8_t *rx_buf, unsigned int *rx_len,
645 u_int64_t timeout, unsigned int flags)
648 u_int8_t rxl = *rx_len & 0xff;
649 u_int8_t channel_red;
651 memset(rx_buf, 0, *rx_len);
654 case RFID_14443A_FRAME_REGULAR:
655 case RFID_MIFARE_FRAME:
656 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
657 |RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
659 case RFID_14443B_FRAME_REGULAR:
660 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
664 case RFID_MIFARE_FRAME:
665 channel_red = RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
672 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
677 ret = rc632_transcieve(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
686 /* transcieve anti collission bitframe */
688 rc632_iso14443a_transcieve_acf(struct rfid_asic_handle *handle,
689 struct iso14443a_anticol_cmd *acf,
690 unsigned int *bit_of_col)
694 u_int8_t rx_len = sizeof(rx_buf);
695 u_int8_t rx_align = 0, tx_last_bits, tx_bytes;
698 *bit_of_col = ISO14443A_BITOFCOL_NONE;
699 memset(rx_buf, 0, sizeof(rx_buf));
701 /* disable mifare cryto */
702 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
703 RC632_CONTROL_CRYPTO1_ON);
707 /* disable CRC summing */
709 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
710 (RC632_CR_PARITY_ENABLE |
711 RC632_CR_PARITY_ODD));
713 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
714 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
719 tx_last_bits = acf->nvb & 0x0f; /* lower nibble indicates bits */
720 tx_bytes = acf->nvb >> 4;
723 rx_align = (tx_last_bits+1) % 8;/* rx frame complements tx */
726 //rx_align = 8 - tx_last_bits;/* rx frame complements tx */
728 /* set RxAlign and TxLastBits*/
729 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING,
730 (rx_align << 4) | (tx_last_bits));
734 ret = rc632_transcieve(handle, (u_int8_t *)acf, tx_bytes,
735 rx_buf, &rx_len, 0x32, 0);
739 /* bitwise-OR the two halves of the split byte */
740 acf->uid_bits[tx_bytes-2] = (
741 (acf->uid_bits[tx_bytes-2] & (0xff >> (8-tx_last_bits)))
744 memcpy(&acf->uid_bits[tx_bytes+1-2], &rx_buf[1], rx_len-1);
746 /* determine whether there was a collission */
747 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
751 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
752 /* retrieve bit of collission */
753 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
757 /* bit of collission relative to start of part 1 of
758 * anticollision frame (!) */
759 *bit_of_col = 2*8 + boc;
766 RC632_RATE_106 = 0x00,
767 RC632_RATE_212 = 0x01,
768 RC632_RATE_424 = 0x02,
769 RC632_RATE_848 = 0x03,
773 u_int8_t subc_pulses;
775 u_int8_t rx_threshold;
776 u_int8_t bpsk_dem_ctrl;
784 static struct rx_config rx_configs[] = {
786 .subc_pulses = RC632_RXCTRL1_SUBCP_8,
787 .rx_coding = RC632_DECCTRL_MANCHESTER,
788 .rx_threshold = 0x88,
789 .bpsk_dem_ctrl = 0x00,
792 .subc_pulses = RC632_RXCTRL1_SUBCP_4,
793 .rx_coding = RC632_DECCTRL_BPSK,
794 .rx_threshold = 0x50,
795 .bpsk_dem_ctrl = 0x0c,
798 .subc_pulses = RC632_RXCTRL1_SUBCP_2,
799 .rx_coding = RC632_DECCTRL_BPSK,
800 .rx_threshold = 0x50,
801 .bpsk_dem_ctrl = 0x0c,
804 .subc_pulses = RC632_RXCTRL1_SUBCP_1,
805 .rx_coding = RC632_DECCTRL_BPSK,
806 .rx_threshold = 0x50,
807 .bpsk_dem_ctrl = 0x0c,
811 static struct tx_config tx_configs[] = {
813 .rate = RC632_CDRCTRL_RATE_106K,
817 .rate = RC632_CDRCTRL_RATE_212K,
821 .rate = RC632_CDRCTRL_RATE_424K,
825 .rate = RC632_CDRCTRL_RATE_848K,
830 static int rc632_iso14443a_set_speed(struct rfid_asic_handle *handle,
840 if (rate > ARRAY_SIZE(rx_configs))
843 rc = rc632_set_bit_mask(handle, RC632_REG_RX_CONTROL1,
844 RC632_RXCTRL1_SUBCP_MASK,
845 rx_configs[rate].subc_pulses);
849 rc = rc632_set_bit_mask(handle, RC632_REG_DECODER_CONTROL,
851 rx_configs[rate].rx_coding);
855 rc = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
856 rx_configs[rate].rx_threshold);
860 if (rx_configs[rate].rx_coding == RC632_DECCTRL_BPSK) {
861 rc = rc632_reg_write(handle,
862 RC632_REG_BPSK_DEM_CONTROL,
863 rx_configs[rate].bpsk_dem_ctrl);
869 if (rate > ARRAY_SIZE(tx_configs))
872 rc = rc632_set_bit_mask(handle, RC632_REG_CODER_CONTROL,
873 RC632_CDRCTRL_RATE_MASK,
874 tx_configs[rate].rate);
878 rc = rc632_reg_write(handle, RC632_REG_MOD_WIDTH,
879 tx_configs[rate].mod_width);
887 static int rc632_iso14443b_init(struct rfid_asic_handle *handle)
891 // FIXME: some FIFO work
893 /* flush fifo (our way) */
894 ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
898 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
899 (RC632_TXCTRL_TX1_RF_EN |
900 RC632_TXCTRL_TX2_RF_EN |
901 RC632_TXCTRL_TX2_INV |
902 RC632_TXCTRL_MOD_SRC_INT));
906 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE, 0x3f);
910 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE, 0x04);
914 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
915 (RC632_CDRCTRL_TXCD_NRZ |
916 RC632_CDRCTRL_RATE_14443B));
920 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
924 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
928 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING,
929 (RC632_TBFRAMING_SOF_11L_3H |
930 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
931 RC632_TBFRAMING_EOF_11));
935 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
936 (RC632_RXCTRL1_GAIN_35DB |
937 RC632_RXCTRL1_ISO14443 |
938 RC632_RXCTRL1_SUBCP_8));
942 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
943 (RC632_DECCTRL_BPSK |
944 RC632_DECCTRL_RXFR_14443B));
948 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
949 CM5121_14443B_BITPHASE);
953 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
954 CM5121_14443B_THRESHOLD);
958 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL,
959 ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
960 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
961 RC632_BPSKD_FILTER_AMP_DETECT |
962 RC632_BPSKD_NO_RX_EOF |
963 RC632_BPSKD_NO_RX_EGT));
967 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
968 (RC632_RXCTRL2_AUTO_PD |
969 RC632_RXCTRL2_DECSRC_INT));
973 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
977 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
978 (RC632_CR_TX_CRC_ENABLE |
979 RC632_CR_RX_CRC_ENABLE |
984 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0xff);
988 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xff);
996 rc632_iso15693_init(struct rfid_asic_handle *h)
1000 ret = rc632_reg_write(h, RC632_REG_TX_CONTROL,
1001 (RC632_TXCTRL_MOD_SRC_INT |
1002 RC632_TXCTRL_TX2_INV |
1003 RC632_TXCTRL_TX2_RF_EN |
1004 RC632_TXCTRL_TX1_RF_EN));
1008 ret = rc632_reg_write(h, RC632_REG_CW_CONDUCTANCE, 0x3f);
1012 ret = rc632_reg_write(h, RC632_REG_MOD_CONDUCTANCE, 0x03);
1016 ret = rc632_reg_write(h, RC632_REG_CODER_CONTROL,
1017 (RC632_CDRCTRL_RATE_15693 |
1022 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH, 0x3f);
1026 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1030 ret = rc632_reg_write(h, RC632_REG_TYPE_B_FRAMING, 0x00);
1034 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL1,
1035 (RC632_RXCTRL1_SUBCP_16 |
1036 RC632_RXCTRL1_ISO15693 |
1037 RC632_RXCTRL1_GAIN_35DB));
1041 ret = rc632_reg_write(h, RC632_REG_DECODER_CONTROL,
1042 (RC632_DECCTRL_RXFR_15693 |
1043 RC632_DECCTRL_RX_INVERT));
1047 ret = rc632_reg_write(h, RC632_REG_BIT_PHASE, 0xe0);
1051 ret = rc632_reg_write(h, RC632_REG_RX_THRESHOLD, 0xff);
1055 ret = rc632_reg_write(h, RC632_REG_BPSK_DEM_CONTROL, 0x00);
1059 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL2,
1060 (RC632_RXCTRL2_AUTO_PD |
1061 RC632_RXCTRL2_DECSRC_INT));
1065 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY,
1067 RC632_CR_RX_CRC_ENABLE |
1068 RC632_CR_TX_CRC_ENABLE));
1072 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_LSB, 0xff);
1076 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_MSB, 0xff);
1084 rc632_iso15693_icode_init(struct rfid_asic_handle *h)
1088 ret = rc632_reg_write(h, RC632_REG_TX_CONTROL,
1089 (RC632_TXCTRL_MOD_SRC_INT |
1090 RC632_TXCTRL_TX2_INV |
1091 RC632_TXCTRL_TX2_RF_EN |
1092 RC632_TXCTRL_TX1_RF_EN));
1096 ret = rc632_reg_write(h, RC632_REG_CW_CONDUCTANCE, 0x3f);
1100 ret = rc632_reg_write(h, RC632_REG_MOD_CONDUCTANCE, 0x02);
1104 ret = rc632_reg_write(h, RC632_REG_CODER_CONTROL, 0x2c);
1108 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH, 0x3f);
1112 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1116 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1120 ret = rc632_reg_write(h, RC632_REG_TYPE_B_FRAMING, 0x00);
1124 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL1, 0x8b); /* FIXME */
1128 ret = rc632_reg_write(h, RC632_REG_DECODER_CONTROL, 0x00);
1132 ret = rc632_reg_write(h, RC632_REG_BIT_PHASE, 0x52);
1136 ret = rc632_reg_write(h, RC632_REG_RX_THRESHOLD, 0x66);
1140 ret = rc632_reg_write(h, RC632_REG_BPSK_DEM_CONTROL, 0x00);
1144 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL2,
1145 RC632_RXCTRL2_DECSRC_INT);
1149 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY,
1150 (RC632_CR_RX_CRC_ENABLE |
1151 RC632_CR_TX_CRC_ENABLE));
1152 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_LSB, 0xfe);
1156 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_MSB, 0xff);
1164 rc632_iso15693_icl_init(struct rfid_asic_handle *h)
1170 ret = rc632_reg_write(h, RC632_REG_TX_CONTROL,
1171 (RC632_TXCTRL_MOD_SRC_INT |
1172 RC632_TXCTRL_TX2_INV |
1173 RC632_TXCTRL_TX2_RF_EN |
1174 RC632_TXCTRL_TX1_RF_EN));
1178 ret = rc632_reg_write(h, RC632_REG_CW_CONDUCTANCE, 0x3f);
1182 ret = rc632_reg_write(h, RC632_REG_MOD_CONDUCTANCE, 0x11);
1186 ret = rc632_reg_write(h, RC632_REG_CODER_CONTROL,
1187 (RC632_CDRCTRL_RATE_15693 |
1188 RC632_CDRCTRL_TXCD_ICODE_STD |
1193 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH, 0x3f);
1197 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1200 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL1,
1201 (RC632_RXCTRL1_SUBCP_16|
1202 RC632_RXCTRL1_ISO15693|
1203 RC632_RXCTRL1_GAIN_35DB));
1206 ret = rc632_reg_write(h, RC632_REG_DECODER_CONTROL,
1207 (RC632_DECCTRL_RX_INVERT|
1208 RC632_DECCTRL_RXFR_15693));
1212 ret = rc632_reg_write(h, RC632_REG_BIT_PHASE, 0xbd);
1216 ret = rc632_reg_write(h, RC632_REG_RX_THRESHOLD, 0xff);
1220 ret = rc632_reg_write(h, RC632_REG_BPSK_DEM_CONTROL, 0x00);
1224 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL2,
1225 RC632_RXCTRL2_DECSRC_INT);
1229 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY, 0x00);
1233 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_LSB, 0x12);
1237 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_MSB, 0xe0);
1244 struct mifare_authcmd {
1246 u_int8_t block_address;
1247 u_int32_t serno; /* lsb 1 2 msb */
1248 } __attribute__ ((packed));
1251 #define RFID_MIFARE_KEY_LEN 6
1252 #define RFID_MIFARE_KEY_CODED_LEN 12
1254 /* Transform crypto1 key from generic 6byte into rc632 specific 12byte */
1256 rc632_mifare_transform_key(const u_int8_t *key6, u_int8_t *key12)
1262 for (i = 0; i < RFID_MIFARE_KEY_LEN; i++) {
1263 ln = key6[i] & 0x0f;
1265 key12[i * 2 + 1] = (~ln << 4) | ln;
1266 key12[i * 2] = (~hn << 4) | hn;
1272 rc632_mifare_set_key(struct rfid_asic_handle *h, const u_int8_t *key)
1274 u_int8_t coded_key[RFID_MIFARE_KEY_CODED_LEN];
1278 ret = rc632_mifare_transform_key(key, coded_key);
1282 ret = rc632_fifo_write(h, RFID_MIFARE_KEY_CODED_LEN, coded_key, 0x03);
1286 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY);
1290 ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1294 ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, ®);
1298 if (reg & RC632_ERR_FLAG_KEY_ERR)
1305 rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno,
1309 struct mifare_authcmd acmd;
1312 if (cmd != RFID_CMD_MIFARE_AUTH1A && cmd != RFID_CMD_MIFARE_AUTH1B)
1315 /* Initialize acmd */
1316 acmd.block_address = block & 0xff;
1317 acmd.auth_cmd = cmd;
1318 //acmd.serno = htonl(serno);
1322 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
1323 RC632_CR_RX_CRC_ENABLE);
1327 /* Send Authent1 Command */
1328 ret = rc632_fifo_write(h, sizeof(acmd), (unsigned char *)&acmd, 0x03);
1332 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT1);
1336 /* Wait until transmitter is idle */
1337 ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1341 ret = rc632_reg_read(h, RC632_REG_SECONDARY_STATUS, ®);
1348 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
1349 RC632_CR_TX_CRC_ENABLE);
1353 /* Send Authent2 Command */
1354 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT2);
1358 /* Wait until transmitter is idle */
1359 ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1363 /* Check whether authentication was successful */
1364 ret = rc632_reg_read(h, RC632_REG_CONTROL, ®);
1368 if (!(reg & RC632_CONTROL_CRYPTO1_ON))
1375 /* transcieve regular frame */
1377 rc632_mifare_transcieve(struct rfid_asic_handle *handle,
1378 const u_int8_t *tx_buf, unsigned int tx_len,
1379 u_int8_t *rx_buf, unsigned int *rx_len,
1380 u_int64_t timeout, unsigned int flags)
1383 u_int8_t rxl = *rx_len & 0xff;
1385 DEBUGP("entered\n");
1386 memset(rx_buf, 0, *rx_len);
1389 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1390 (RC632_CR_PARITY_ENABLE |
1391 RC632_CR_PARITY_ODD |
1392 RC632_CR_TX_CRC_ENABLE |
1393 RC632_CR_RX_CRC_ENABLE));
1395 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
1396 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
1401 ret = rc632_transcieve(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
1410 struct rfid_asic rc632 = {
1411 .name = "Philips CL RC632",
1412 .fc = ISO14443_FREQ_CARRIER,
1415 .power_up = &rc632_power_up,
1416 .power_down = &rc632_power_down,
1417 .turn_on_rf = &rc632_turn_on_rf,
1418 .turn_off_rf = &rc632_turn_off_rf,
1419 .transcieve = &rc632_iso14443ab_transcieve,
1421 .init = &rc632_iso14443a_init,
1422 .transcieve_sf = &rc632_iso14443a_transcieve_sf,
1423 .transcieve_acf = &rc632_iso14443a_transcieve_acf,
1424 .set_speed = &rc632_iso14443a_set_speed,
1427 .init = &rc632_iso14443b_init,
1430 .init = &rc632_iso15693_init,
1433 .setkey = &rc632_mifare_set_key,
1434 .auth = &rc632_mifare_auth,