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*100/USLEEP_PER_CYCLE) {
199 usleep(USLEEP_PER_CYCLE);
206 rc632_transmit(struct rfid_asic_handle *handle,
212 const u_int8_t *cur_buf = buf;
220 ret = rc632_fifo_write(handle, cur_len, cur_buf, 0x03);
224 if (cur_buf == buf) {
225 /* only start transmit first time */
226 ret = rc632_reg_write(handle, RC632_REG_COMMAND,
233 if (cur_buf < buf + len) {
234 cur_len = buf - cur_buf;
242 return rc632_wait_idle(handle, timeout);
246 tcl_toggle_pcb(struct rfid_asic_handle *handle)
248 // FIXME: toggle something between 0x0a and 0x0b
253 rc632_transceive(struct rfid_asic_handle *handle,
254 const u_int8_t *tx_buf,
262 const u_int8_t *cur_tx_buf = tx_buf;
270 ret = rc632_fifo_write(handle, cur_tx_len, cur_tx_buf, 0x03);
274 if (cur_tx_buf == tx_buf) {
275 ret = rc632_reg_write(handle, RC632_REG_COMMAND,
276 RC632_CMD_TRANSCEIVE);
281 cur_tx_buf += cur_tx_len;
282 if (cur_tx_buf < tx_buf + tx_len) {
284 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH,
289 cur_tx_len = 64 - fifo_fill;
290 printf("refilling tx fifo with %u bytes\n", cur_tx_len);
294 } while (cur_tx_len);
297 tcl_toggle_pcb(handle);
299 ret = rc632_wait_idle(handle, timer);
303 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, rx_len);
310 DEBUGP("rx_len == 0\n");
312 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
313 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
318 return rc632_fifo_read(handle, *rx_len, rx_buf);
322 rc632_read_eeprom(struct rfid_asic_handle *handle)
324 u_int8_t recvbuf[60];
332 ret = rc632_fifo_write(handle, 3, sndbuf, 0x03);
336 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_READ_E2);
342 ret = rc632_fifo_read(handle, sizeof(recvbuf), recvbuf);
346 // FIXME: do something with eeprom contents
351 rc632_calc_crc16_from(struct rfid_asic_handle *handle)
353 u_int8_t sndbuf[2] = { 0x01, 0x02 };
354 u_int8_t crc_lsb = 0x00 , crc_msb = 0x00;
357 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x12);
361 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xe0);
365 ret = rc632_fifo_write(handle, sizeof(sndbuf), sndbuf, 3);
369 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_CALC_CRC);
373 usleep(10000); // FIXME: no checking for cmd completion?
375 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_LSB, &crc_lsb);
379 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_MSB, &crc_msb);
383 // FIXME: what to do with crc result?
389 rc632_register_dump(struct rfid_asic_handle *handle, u_int8_t *buf)
394 for (i = 0; i <= 0x3f; i++) {
395 ret = rc632_reg_read(handle, i, &buf[i]);
396 // do we want error checks?
403 /* generic FIFO access functions (if no more efficient ones provided by
404 * transport driver) */
409 // FIXME: implementation (not needed for CM 5121)
416 // FIXME: implementation (not neded for CM 5121)
421 rc632_init(struct rfid_asic_handle *ah)
425 /* switch off rf (make sure PICCs are reset at init time) */
426 ret = rc632_power_down(ah);
433 ret = rc632_power_up(ah);
437 /* disable register paging */
438 ret = rc632_reg_write(ah, 0x00, 0x00);
442 /* set some sane default values */
443 ret = rc632_reg_write(ah, 0x11, 0x5b);
448 ret = rc632_turn_on_rf(ah);
456 rc632_fini(struct rfid_asic_handle *ah)
461 ret = rc632_turn_off_rf(ah);
465 ret = rc632_power_down(ah);
472 struct rfid_asic_handle *
473 rc632_open(struct rfid_asic_transport_handle *th)
475 struct rfid_asic_handle *h;
477 h = malloc(sizeof(*h));
480 memset(h, 0, sizeof(*h));
485 /* FIXME: this is only cm5121 specific, since the latency
486 * down to the RC632 FIFO is too long to refill during TX/RX */
487 h->mtu = h->mru = 64;
489 if (rc632_init(h) < 0) {
498 rc632_close(struct rfid_asic_handle *h)
506 * Philips CL RC632 primitives for ISO 14443-A compliant PICC's
508 * (C) 2005 by Harald Welte <laforge@gnumonks.org>
513 rc632_iso14443a_init(struct rfid_asic_handle *handle)
517 // FIXME: some fifo work (drain fifo?)
519 /* flush fifo (our way) */
520 ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
522 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
523 (RC632_TXCTRL_TX1_RF_EN |
524 RC632_TXCTRL_TX2_RF_EN |
525 RC632_TXCTRL_TX2_INV |
526 RC632_TXCTRL_FORCE_100_ASK |
527 RC632_TXCTRL_MOD_SRC_INT));
531 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE,
532 CM5121_CW_CONDUCTANCE);
536 /* Since FORCE_100_ASK is set (cf mc073930.pdf), this line may be left out? */
537 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE,
538 CM5121_MOD_CONDUCTANCE);
542 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
543 (RC632_CDRCTRL_TXCD_14443A |
544 RC632_CDRCTRL_RATE_106K));
548 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
552 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
556 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING, 0x00);
560 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
561 (RC632_RXCTRL1_GAIN_35DB |
562 RC632_RXCTRL1_ISO14443 |
563 RC632_RXCTRL1_SUBCP_8));
567 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
568 (RC632_DECCTRL_MANCHESTER |
569 RC632_DECCTRL_RXFR_14443A));
573 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
574 CM5121_14443A_BITPHASE);
578 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
579 CM5121_14443A_THRESHOLD);
583 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL, 0x00);
587 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
588 (RC632_RXCTRL2_DECSRC_INT |
589 RC632_RXCTRL2_CLK_Q));
593 /* Omnikey proprietary driver has 0x03, but 0x06 is the default reset value ?!? */
594 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x06);
598 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
599 (RC632_CR_PARITY_ENABLE |
600 RC632_CR_PARITY_ODD));
604 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x63);
608 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0x63);
616 rc632_iso14443a_fini(struct iso14443a_handle *handle_14443)
620 ret = rc632_turn_off_rf(handle);
630 /* issue a 14443-3 A PCD -> PICC command in a short frame, such as REQA, WUPA */
632 rc632_iso14443a_transceive_sf(struct rfid_asic_handle *handle,
634 struct iso14443a_atqa *atqa)
640 memset(atqa, 0, sizeof(atqa));
644 /* transfer only 7 bits of last byte in frame */
645 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
649 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
650 RC632_CONTROL_CRYPTO1_ON);
655 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
656 (RC632_CR_PARITY_ENABLE |
657 RC632_CR_PARITY_ODD));
659 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
660 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
666 ret = rc632_transceive(handle, tx_buf, sizeof(tx_buf),
667 (u_int8_t *)atqa, &rx_len, 0x32, 0);
669 DEBUGP("error during rc632_transceive()\n");
673 /* switch back to normal 8bit last byte */
674 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
679 DEBUGP("rx_len(%d) != 2\n", rx_len);
686 /* transceive regular frame */
688 rc632_iso14443ab_transceive(struct rfid_asic_handle *handle,
689 unsigned int frametype,
690 const u_int8_t *tx_buf, unsigned int tx_len,
691 u_int8_t *rx_buf, unsigned int *rx_len,
692 u_int64_t timeout, unsigned int flags)
695 u_int8_t rxl = *rx_len & 0xff;
696 u_int8_t channel_red;
698 memset(rx_buf, 0, *rx_len);
701 case RFID_14443A_FRAME_REGULAR:
702 case RFID_MIFARE_FRAME:
703 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
704 |RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
706 case RFID_14443B_FRAME_REGULAR:
707 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
711 case RFID_MIFARE_FRAME:
712 channel_red = RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
719 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
724 ret = rc632_transceive(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
733 /* transceive anti collission bitframe */
735 rc632_iso14443a_transceive_acf(struct rfid_asic_handle *handle,
736 struct iso14443a_anticol_cmd *acf,
737 unsigned int *bit_of_col)
741 u_int8_t rx_len = sizeof(rx_buf);
742 u_int8_t rx_align = 0, tx_last_bits, tx_bytes;
745 *bit_of_col = ISO14443A_BITOFCOL_NONE;
746 memset(rx_buf, 0, sizeof(rx_buf));
748 /* disable mifare cryto */
749 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
750 RC632_CONTROL_CRYPTO1_ON);
754 /* disable CRC summing */
756 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
757 (RC632_CR_PARITY_ENABLE |
758 RC632_CR_PARITY_ODD));
760 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
761 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
766 tx_last_bits = acf->nvb & 0x0f; /* lower nibble indicates bits */
767 tx_bytes = acf->nvb >> 4;
770 rx_align = (tx_last_bits+1) % 8;/* rx frame complements tx */
773 //rx_align = 8 - tx_last_bits;/* rx frame complements tx */
775 /* set RxAlign and TxLastBits*/
776 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING,
777 (rx_align << 4) | (tx_last_bits));
781 ret = rc632_transceive(handle, (u_int8_t *)acf, tx_bytes,
782 rx_buf, &rx_len, 0x32, 0);
786 /* bitwise-OR the two halves of the split byte */
787 acf->uid_bits[tx_bytes-2] = (
788 (acf->uid_bits[tx_bytes-2] & (0xff >> (8-tx_last_bits)))
791 memcpy(&acf->uid_bits[tx_bytes+1-2], &rx_buf[1], rx_len-1);
793 /* determine whether there was a collission */
794 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
798 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
799 /* retrieve bit of collission */
800 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
804 /* bit of collission relative to start of part 1 of
805 * anticollision frame (!) */
806 *bit_of_col = 2*8 + boc;
813 RC632_RATE_106 = 0x00,
814 RC632_RATE_212 = 0x01,
815 RC632_RATE_424 = 0x02,
816 RC632_RATE_848 = 0x03,
820 u_int8_t subc_pulses;
822 u_int8_t rx_threshold;
823 u_int8_t bpsk_dem_ctrl;
831 static struct rx_config rx_configs[] = {
833 .subc_pulses = RC632_RXCTRL1_SUBCP_8,
834 .rx_coding = RC632_DECCTRL_MANCHESTER,
835 .rx_threshold = 0x88,
836 .bpsk_dem_ctrl = 0x00,
839 .subc_pulses = RC632_RXCTRL1_SUBCP_4,
840 .rx_coding = RC632_DECCTRL_BPSK,
841 .rx_threshold = 0x50,
842 .bpsk_dem_ctrl = 0x0c,
845 .subc_pulses = RC632_RXCTRL1_SUBCP_2,
846 .rx_coding = RC632_DECCTRL_BPSK,
847 .rx_threshold = 0x50,
848 .bpsk_dem_ctrl = 0x0c,
851 .subc_pulses = RC632_RXCTRL1_SUBCP_1,
852 .rx_coding = RC632_DECCTRL_BPSK,
853 .rx_threshold = 0x50,
854 .bpsk_dem_ctrl = 0x0c,
858 static struct tx_config tx_configs[] = {
860 .rate = RC632_CDRCTRL_RATE_106K,
864 .rate = RC632_CDRCTRL_RATE_212K,
868 .rate = RC632_CDRCTRL_RATE_424K,
872 .rate = RC632_CDRCTRL_RATE_848K,
877 static int rc632_iso14443a_set_speed(struct rfid_asic_handle *handle,
887 if (rate > ARRAY_SIZE(rx_configs))
890 rc = rc632_set_bit_mask(handle, RC632_REG_RX_CONTROL1,
891 RC632_RXCTRL1_SUBCP_MASK,
892 rx_configs[rate].subc_pulses);
896 rc = rc632_set_bit_mask(handle, RC632_REG_DECODER_CONTROL,
898 rx_configs[rate].rx_coding);
902 rc = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
903 rx_configs[rate].rx_threshold);
907 if (rx_configs[rate].rx_coding == RC632_DECCTRL_BPSK) {
908 rc = rc632_reg_write(handle,
909 RC632_REG_BPSK_DEM_CONTROL,
910 rx_configs[rate].bpsk_dem_ctrl);
916 if (rate > ARRAY_SIZE(tx_configs))
919 rc = rc632_set_bit_mask(handle, RC632_REG_CODER_CONTROL,
920 RC632_CDRCTRL_RATE_MASK,
921 tx_configs[rate].rate);
925 rc = rc632_reg_write(handle, RC632_REG_MOD_WIDTH,
926 tx_configs[rate].mod_width);
934 static int rc632_iso14443b_init(struct rfid_asic_handle *handle)
938 // FIXME: some FIFO work
940 /* flush fifo (our way) */
941 ret = rc632_reg_write(handle, RC632_REG_CONTROL, 0x01);
945 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
946 (RC632_TXCTRL_TX1_RF_EN |
947 RC632_TXCTRL_TX2_RF_EN |
948 RC632_TXCTRL_TX2_INV |
949 RC632_TXCTRL_MOD_SRC_INT));
953 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE, 0x3f);
957 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE, 0x04);
961 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
962 (RC632_CDRCTRL_TXCD_NRZ |
963 RC632_CDRCTRL_RATE_14443B));
967 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
971 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
975 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING,
976 (RC632_TBFRAMING_SOF_11L_3H |
977 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
978 RC632_TBFRAMING_EOF_11));
982 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
983 (RC632_RXCTRL1_GAIN_35DB |
984 RC632_RXCTRL1_ISO14443 |
985 RC632_RXCTRL1_SUBCP_8));
989 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
990 (RC632_DECCTRL_BPSK |
991 RC632_DECCTRL_RXFR_14443B));
995 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
996 CM5121_14443B_BITPHASE);
1000 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
1001 CM5121_14443B_THRESHOLD);
1005 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL,
1006 ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
1007 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
1008 RC632_BPSKD_FILTER_AMP_DETECT |
1009 RC632_BPSKD_NO_RX_EOF |
1010 RC632_BPSKD_NO_RX_EGT));
1014 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
1015 (RC632_RXCTRL2_AUTO_PD |
1016 RC632_RXCTRL2_DECSRC_INT));
1020 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
1024 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1025 (RC632_CR_TX_CRC_ENABLE |
1026 RC632_CR_RX_CRC_ENABLE |
1031 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0xff);
1035 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xff);
1043 rc632_iso15693_init(struct rfid_asic_handle *h)
1047 ret = rc632_reg_write(h, RC632_REG_TX_CONTROL,
1048 (RC632_TXCTRL_MOD_SRC_INT |
1049 RC632_TXCTRL_TX2_INV |
1050 RC632_TXCTRL_TX2_RF_EN |
1051 RC632_TXCTRL_TX1_RF_EN));
1055 ret = rc632_reg_write(h, RC632_REG_CW_CONDUCTANCE, 0x3f);
1059 ret = rc632_reg_write(h, RC632_REG_MOD_CONDUCTANCE, 0x03);
1063 ret = rc632_reg_write(h, RC632_REG_CODER_CONTROL,
1064 (RC632_CDRCTRL_RATE_15693 |
1069 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH, 0x3f);
1073 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1077 ret = rc632_reg_write(h, RC632_REG_TYPE_B_FRAMING, 0x00);
1081 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL1,
1082 (RC632_RXCTRL1_SUBCP_16 |
1083 RC632_RXCTRL1_ISO15693 |
1084 RC632_RXCTRL1_GAIN_35DB));
1088 ret = rc632_reg_write(h, RC632_REG_DECODER_CONTROL,
1089 (RC632_DECCTRL_RXFR_15693 |
1090 RC632_DECCTRL_RX_INVERT));
1094 ret = rc632_reg_write(h, RC632_REG_BIT_PHASE, 0xe0);
1098 ret = rc632_reg_write(h, RC632_REG_RX_THRESHOLD, 0xff);
1102 ret = rc632_reg_write(h, RC632_REG_BPSK_DEM_CONTROL, 0x00);
1106 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL2,
1107 (RC632_RXCTRL2_AUTO_PD |
1108 RC632_RXCTRL2_DECSRC_INT));
1112 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY,
1114 RC632_CR_RX_CRC_ENABLE |
1115 RC632_CR_TX_CRC_ENABLE));
1119 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_LSB, 0xff);
1123 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_MSB, 0xff);
1131 rc632_iso15693_icode_init(struct rfid_asic_handle *h)
1135 ret = rc632_reg_write(h, RC632_REG_TX_CONTROL,
1136 (RC632_TXCTRL_MOD_SRC_INT |
1137 RC632_TXCTRL_TX2_INV |
1138 RC632_TXCTRL_TX2_RF_EN |
1139 RC632_TXCTRL_TX1_RF_EN));
1143 ret = rc632_reg_write(h, RC632_REG_CW_CONDUCTANCE, 0x3f);
1147 ret = rc632_reg_write(h, RC632_REG_MOD_CONDUCTANCE, 0x02);
1151 ret = rc632_reg_write(h, RC632_REG_CODER_CONTROL, 0x2c);
1155 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH, 0x3f);
1159 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1163 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1167 ret = rc632_reg_write(h, RC632_REG_TYPE_B_FRAMING, 0x00);
1171 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL1, 0x8b); /* FIXME */
1175 ret = rc632_reg_write(h, RC632_REG_DECODER_CONTROL, 0x00);
1179 ret = rc632_reg_write(h, RC632_REG_BIT_PHASE, 0x52);
1183 ret = rc632_reg_write(h, RC632_REG_RX_THRESHOLD, 0x66);
1187 ret = rc632_reg_write(h, RC632_REG_BPSK_DEM_CONTROL, 0x00);
1191 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL2,
1192 RC632_RXCTRL2_DECSRC_INT);
1196 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY,
1197 (RC632_CR_RX_CRC_ENABLE |
1198 RC632_CR_TX_CRC_ENABLE));
1199 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_LSB, 0xfe);
1203 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_MSB, 0xff);
1211 rc632_iso15693_icl_init(struct rfid_asic_handle *h)
1217 ret = rc632_reg_write(h, RC632_REG_TX_CONTROL,
1218 (RC632_TXCTRL_MOD_SRC_INT |
1219 RC632_TXCTRL_TX2_INV |
1220 RC632_TXCTRL_TX2_RF_EN |
1221 RC632_TXCTRL_TX1_RF_EN));
1225 ret = rc632_reg_write(h, RC632_REG_CW_CONDUCTANCE, 0x3f);
1229 ret = rc632_reg_write(h, RC632_REG_MOD_CONDUCTANCE, 0x11);
1233 ret = rc632_reg_write(h, RC632_REG_CODER_CONTROL,
1234 (RC632_CDRCTRL_RATE_15693 |
1235 RC632_CDRCTRL_TXCD_ICODE_STD |
1240 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH, 0x3f);
1244 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1247 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL1,
1248 (RC632_RXCTRL1_SUBCP_16|
1249 RC632_RXCTRL1_ISO15693|
1250 RC632_RXCTRL1_GAIN_35DB));
1253 ret = rc632_reg_write(h, RC632_REG_DECODER_CONTROL,
1254 (RC632_DECCTRL_RX_INVERT|
1255 RC632_DECCTRL_RXFR_15693));
1259 ret = rc632_reg_write(h, RC632_REG_BIT_PHASE, 0xbd);
1263 ret = rc632_reg_write(h, RC632_REG_RX_THRESHOLD, 0xff);
1267 ret = rc632_reg_write(h, RC632_REG_BPSK_DEM_CONTROL, 0x00);
1271 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL2,
1272 RC632_RXCTRL2_DECSRC_INT);
1276 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY, 0x00);
1280 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_LSB, 0x12);
1284 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_MSB, 0xe0);
1291 struct mifare_authcmd {
1293 u_int8_t block_address;
1294 u_int32_t serno; /* lsb 1 2 msb */
1295 } __attribute__ ((packed));
1298 #define RFID_MIFARE_KEY_LEN 6
1299 #define RFID_MIFARE_KEY_CODED_LEN 12
1301 /* Transform crypto1 key from generic 6byte into rc632 specific 12byte */
1303 rc632_mifare_transform_key(const u_int8_t *key6, u_int8_t *key12)
1309 for (i = 0; i < RFID_MIFARE_KEY_LEN; i++) {
1310 ln = key6[i] & 0x0f;
1312 key12[i * 2 + 1] = (~ln << 4) | ln;
1313 key12[i * 2] = (~hn << 4) | hn;
1319 rc632_mifare_set_key(struct rfid_asic_handle *h, const u_int8_t *key)
1321 u_int8_t coded_key[RFID_MIFARE_KEY_CODED_LEN];
1325 ret = rc632_mifare_transform_key(key, coded_key);
1329 ret = rc632_fifo_write(h, RFID_MIFARE_KEY_CODED_LEN, coded_key, 0x03);
1333 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY);
1337 ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1341 ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, ®);
1345 if (reg & RC632_ERR_FLAG_KEY_ERR)
1352 rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno,
1356 struct mifare_authcmd acmd;
1359 if (cmd != RFID_CMD_MIFARE_AUTH1A && cmd != RFID_CMD_MIFARE_AUTH1B)
1362 /* Initialize acmd */
1363 acmd.block_address = block & 0xff;
1364 acmd.auth_cmd = cmd;
1365 //acmd.serno = htonl(serno);
1369 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
1370 RC632_CR_RX_CRC_ENABLE);
1374 /* Send Authent1 Command */
1375 ret = rc632_fifo_write(h, sizeof(acmd), (unsigned char *)&acmd, 0x03);
1379 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT1);
1383 /* Wait until transmitter is idle */
1384 ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1388 ret = rc632_reg_read(h, RC632_REG_SECONDARY_STATUS, ®);
1395 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
1396 RC632_CR_TX_CRC_ENABLE);
1400 /* Send Authent2 Command */
1401 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT2);
1405 /* Wait until transmitter is idle */
1406 ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1410 /* Check whether authentication was successful */
1411 ret = rc632_reg_read(h, RC632_REG_CONTROL, ®);
1415 if (!(reg & RC632_CONTROL_CRYPTO1_ON))
1422 /* transceive regular frame */
1424 rc632_mifare_transceive(struct rfid_asic_handle *handle,
1425 const u_int8_t *tx_buf, unsigned int tx_len,
1426 u_int8_t *rx_buf, unsigned int *rx_len,
1427 u_int64_t timeout, unsigned int flags)
1430 u_int8_t rxl = *rx_len & 0xff;
1432 DEBUGP("entered\n");
1433 memset(rx_buf, 0, *rx_len);
1436 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1437 (RC632_CR_PARITY_ENABLE |
1438 RC632_CR_PARITY_ODD |
1439 RC632_CR_TX_CRC_ENABLE |
1440 RC632_CR_RX_CRC_ENABLE));
1442 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
1443 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
1448 ret = rc632_transceive(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
1457 struct rfid_asic rc632 = {
1458 .name = "Philips CL RC632",
1459 .fc = ISO14443_FREQ_CARRIER,
1462 .power_up = &rc632_power_up,
1463 .power_down = &rc632_power_down,
1464 .turn_on_rf = &rc632_turn_on_rf,
1465 .turn_off_rf = &rc632_turn_off_rf,
1466 .transceive = &rc632_iso14443ab_transceive,
1468 .init = &rc632_iso14443a_init,
1469 .transceive_sf = &rc632_iso14443a_transceive_sf,
1470 .transceive_acf = &rc632_iso14443a_transceive_acf,
1471 .set_speed = &rc632_iso14443a_set_speed,
1474 .init = &rc632_iso14443b_init,
1477 .init = &rc632_iso15693_init,
1480 .setkey = &rc632_mifare_set_key,
1481 .auth = &rc632_mifare_auth,