1 /* Generic Philips CL RC632 Routines
3 * (C) 2005-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
27 #include <sys/types.h>
29 #include <librfid/rfid.h>
30 #include <librfid/rfid_asic.h>
31 #include <librfid/rfid_asic_rc632.h>
32 #include <librfid/rfid_reader_cm5121.h>
33 #include <librfid/rfid_layer2_iso14443a.h>
34 #include <librfid/rfid_layer2_iso15693.h>
35 #include <librfid/rfid_protocol_mifare_classic.h>
37 #include "rfid_iso14443_common.h"
44 #define RC632_TMO_AUTH1 140
46 #define TIMER_RELAX_FACTOR 10
48 #define ENTER() DEBUGP("entering\n")
49 const struct rfid_asic rc632;
51 struct register_file {
56 /* Register and FIFO Access functions */
58 rc632_reg_write(struct rfid_asic_handle *handle,
62 return handle->rath->rat->priv.rc632.fn.reg_write(handle->rath, reg, val);
66 rc632_reg_read(struct rfid_asic_handle *handle,
70 return handle->rath->rat->priv.rc632.fn.reg_read(handle->rath, reg, val);
74 rc632_fifo_write(struct rfid_asic_handle *handle,
79 return handle->rath->rat->priv.rc632.fn.fifo_write(handle->rath,
84 rc632_fifo_read(struct rfid_asic_handle *handle,
88 return handle->rath->rat->priv.rc632.fn.fifo_read(handle->rath, len, buf);
93 rc632_set_bits(struct rfid_asic_handle *handle,
100 ret = rc632_reg_read(handle, reg, &tmp);
104 /* if bits are already set, no need to set them again */
105 if ((tmp & val) == val)
108 return rc632_reg_write(handle, reg, (tmp|val)&0xff);
111 rc632_set_bit_mask(struct rfid_asic_handle *handle,
112 u_int8_t reg, u_int8_t mask, u_int8_t val)
117 ret = rc632_reg_read(handle, reg, &tmp);
121 /* if bits are already like we want them, abort */
122 if ((tmp & mask) == val)
125 return rc632_reg_write(handle, reg, (tmp & ~mask)|(val & mask));
129 rc632_clear_bits(struct rfid_asic_handle *handle,
136 ret = rc632_reg_read(handle, reg, &tmp);
138 DEBUGP("error during reg_read(%p, %d):%d\n",
142 /* if bits are already cleared, no need to clear them again */
143 if ((tmp & val) == 0)
146 return rc632_reg_write(handle, reg, (tmp & ~val)&0xff);
150 rc632_clear_irqs(struct rfid_asic_handle *handle, u_int8_t bits)
152 return rc632_reg_write(handle, RC632_REG_INTERRUPT_RQ, (~RC632_INT_SET)&bits);
156 rc632_rf_power(struct rfid_asic_handle *handle, int on)
160 return rc632_set_bits(handle, RC632_REG_TX_CONTROL,
161 RC632_TXCTRL_TX1_RF_EN|
162 RC632_TXCTRL_TX2_RF_EN);
164 return rc632_clear_bits(handle, RC632_REG_TX_CONTROL,
165 RC632_TXCTRL_TX1_RF_EN|
166 RC632_TXCTRL_TX2_RF_EN);
170 rc632_power(struct rfid_asic_handle *handle, int on)
174 return rc632_clear_bits(handle, RC632_REG_CONTROL,
175 RC632_CONTROL_POWERDOWN);
177 return rc632_set_bits(handle, RC632_REG_CONTROL,
178 RC632_CONTROL_POWERDOWN);
182 rc632_execute_script(struct rfid_asic_handle *h, struct register_file *f,
187 for (i = 0; i < len; i++) {
188 ret = rc632_reg_write(h, f[i].reg, f[i].val);
196 /* calculate best 8bit prescaler and divisor for given usec timeout */
197 static int best_prescaler(u_int64_t timeout, u_int8_t *prescaler,
200 u_int8_t best_prescaler, best_divisor, i;
201 int64_t smallest_diff;
203 smallest_diff = LLONG_MAX;
206 for (i = 0; i < 21; i++) {
207 u_int64_t clk, tmp_div, res;
209 clk = 13560000 / (1 << i);
210 tmp_div = (clk * timeout) / 1000000;
213 if ((tmp_div > 0xff) || (tmp_div > clk))
216 res = 1000000 / (clk / tmp_div);
217 diff = res - timeout;
222 if (diff < smallest_diff) {
224 best_divisor = tmp_div;
225 smallest_diff = diff;
229 *prescaler = best_prescaler;
230 *divisor = best_divisor;
232 DEBUGP("timeout %u usec, prescaler = %u, divisor = %u\n",
233 timeout, best_prescaler, best_divisor);
239 rc632_timer_set(struct rfid_asic_handle *handle,
243 u_int8_t prescaler, divisor, irq;
245 timeout *= TIMER_RELAX_FACTOR;
247 ret = best_prescaler(timeout, &prescaler, &divisor);
249 ret = rc632_reg_write(handle, RC632_REG_TIMER_CLOCK,
254 ret = rc632_reg_write(handle, RC632_REG_TIMER_CONTROL,
255 RC632_TMR_START_TX_END|RC632_TMR_STOP_RX_BEGIN);
257 /* clear timer irq bit */
258 ret = rc632_clear_irqs(handle, RC632_IRQ_TIMER);
260 /* enable timer IRQ */
261 ret |= rc632_reg_write(handle, RC632_REG_INTERRUPT_EN, RC632_IRQ_SET | RC632_IRQ_TIMER);
263 ret |= rc632_reg_write(handle, RC632_REG_TIMER_RELOAD, divisor);
268 /* Wait until RC632 is idle or TIMER IRQ has happened */
269 static int rc632_wait_idle_timer(struct rfid_asic_handle *handle)
272 u_int8_t stat, irq, cmd;
274 ret = rc632_reg_read(handle, RC632_REG_INTERRUPT_EN, &irq);
277 DEBUGP_INTERRUPT_FLAG("irq_en",irq);
279 ret = rc632_reg_write(handle, RC632_REG_INTERRUPT_EN, RC632_IRQ_SET
287 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &stat);
288 DEBUGP_STATUS_FLAG(stat);
289 if (stat & RC632_STAT_ERR) {
291 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &err);
292 DEBUGP_ERROR_FLAG(err);
293 if (err & (RC632_ERR_FLAG_COL_ERR |
294 RC632_ERR_FLAG_PARITY_ERR |
295 RC632_ERR_FLAG_FRAMING_ERR |
296 /* FIXME: why get we CRC errors in CL2 anticol at iso14443a operation with mifare UL? */
297 /* RC632_ERR_FLAG_CRC_ERR | */
301 if (stat & RC632_STAT_IRQ) {
302 ret = rc632_reg_read(handle, RC632_REG_INTERRUPT_RQ, &irq);
305 DEBUGP_INTERRUPT_FLAG("irq_rq",irq);
307 if (irq & RC632_IRQ_TIMER && !(irq & RC632_IRQ_RX)) {
308 DEBUGP("timer expired before RX!!\n");
309 rc632_clear_irqs(handle, RC632_IRQ_TIMER);
314 ret = rc632_reg_read(handle, RC632_REG_COMMAND, &cmd);
319 rc632_clear_irqs(handle, RC632_IRQ_RX);
323 /* poll every millisecond */
328 /* Stupid RC632 implementations don't evaluate interrupts but poll the
329 * command register for "status idle" */
331 rc632_wait_idle(struct rfid_asic_handle *handle, u_int64_t timeout)
335 #define USLEEP_PER_CYCLE 128
337 timeout *= TIMER_RELAX_FACTOR;
340 ret = rc632_reg_read(handle, RC632_REG_COMMAND, &cmd);
346 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &foo);
347 DEBUGP_STATUS_FLAG(foo);
348 /* check if Error has occured (ERR flag set) */
349 if (foo & RC632_STAT_ERR) {
350 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &foo);
351 DEBUGP_ERROR_FLAG(foo);
353 /* check if IRQ has occurred (IRQ flag set)*/
354 if (foo & RC632_STAT_IRQ) {
355 ret = rc632_reg_read(handle, RC632_REG_INTERRUPT_RQ, &foo);
356 DEBUGP_INTERRUPT_FLAG("irq_rq",foo);
357 /* clear all interrupts */
358 rc632_clear_irqs(handle, 0xff);
362 /* FIXME: read second time ?? */
363 DEBUGP("cmd == 0 (IDLE)\n");
367 /* Abort after some timeout */
368 if (cycles > timeout/USLEEP_PER_CYCLE) {
369 DEBUGP("timeout...\n");
374 usleep(USLEEP_PER_CYCLE);
381 rc632_transmit(struct rfid_asic_handle *handle,
387 const u_int8_t *cur_buf = buf;
389 DEBUGP("timeout=%u, tx_len=%u\n", timeout, len);
397 ret = rc632_fifo_write(handle, cur_len, cur_buf, 0x03);
401 if (cur_buf == buf) {
402 /* only start transmit first time */
403 ret = rc632_reg_write(handle, RC632_REG_COMMAND,
410 if (cur_buf < buf + len) {
411 cur_len = buf - cur_buf;
419 return rc632_wait_idle(handle, timeout);
423 tcl_toggle_pcb(struct rfid_asic_handle *handle)
425 /* FIXME: toggle something between 0x0a and 0x0b */
430 rc632_transceive(struct rfid_asic_handle *handle,
431 const u_int8_t *tx_buf,
438 int ret, cur_tx_len, i;
440 const u_int8_t *cur_tx_buf = tx_buf;
442 DEBUGP("timeout=%u, rx_len=%u, tx_len=%u\n", timer, *rx_len, tx_len);
450 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_IDLE);
451 /* clear all interrupts */
452 ret = rc632_reg_write(handle, RC632_REG_INTERRUPT_RQ, 0x7f);
455 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
456 DEBUGP_STATUS_FLAG(tmp);
457 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
458 DEBUGP_ERROR_FLAG(tmp);
461 ret = rc632_timer_set(handle, timer);
466 ret = rc632_fifo_write(handle, cur_tx_len, cur_tx_buf, 0x03);
470 if (cur_tx_buf == tx_buf) {
471 ret = rc632_reg_write(handle, RC632_REG_COMMAND,
472 RC632_CMD_TRANSCEIVE);
477 cur_tx_buf += cur_tx_len;
478 if (cur_tx_buf < tx_buf + tx_len) {
480 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH,
485 cur_tx_len = 64 - fifo_fill;
489 } while (cur_tx_len);
492 tcl_toggle_pcb(handle);
494 ret = rc632_wait_idle_timer(handle);
495 //ret = rc632_wait_idle(handle, timer);
497 DEBUGP("rc632_wait_idle >> ret=%d %s\n",ret,(ret==-ETIMEDOUT)?"ETIMEDOUT":"");
501 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, &rx_avail);
505 if (rx_avail > *rx_len)
506 DEBUGP("rx_avail(%d) > rx_len(%d), JFYI\n", rx_avail, *rx_len);
507 else if (*rx_len > rx_avail)
510 DEBUGP("rx_len == %d\n",*rx_len);
515 for (i = 0; i < 1; i++){
516 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
517 DEBUGP_STATUS_FLAG(tmp);
518 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
519 DEBUGP_ERROR_FLAG(tmp);
521 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
527 return rc632_fifo_read(handle, *rx_len, rx_buf);
528 /* FIXME: discard addidional bytes in FIFO */
533 rc632_receive(struct rfid_asic_handle *handle,
538 int ret, cur_tx_len, i;
541 DEBUGP("timeout=%u, rx_len=%u\n", timer, *rx_len);
542 ret = rc632_reg_write(handle, RC632_REG_COMMAND, 0x00); /* IDLE */
543 /* clear all interrupts */
544 ret = rc632_reg_write(handle, RC632_REG_INTERRUPT_RQ, 0x7f);
546 ret = rc632_timer_set(handle, timer);
550 ret = rc632_reg_write(handle, RC632_REG_COMMAND,RC632_CMD_RECEIVE);
554 /* the timer cannot start in hardware based on the command we just
555 * sent. this means that our timing will always be quite a bit more lax,
556 * i.e. we'll always wait for a bit longer than the specs ask us to. */
557 ret = rc632_set_bits(handle, RC632_REG_CONTROL,
558 RC632_CONTROL_TIMER_START);
562 //ret = rc632_wait_idle(handle, timer);
563 ret = rc632_wait_idle_timer(handle);
567 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, &rx_avail);
571 if (rx_avail > *rx_len) {
572 //DEBUGP("rx_avail(%d) > rx_len(%d), JFYI\n", rx_avail, *rx_len);
573 } else if (*rx_len > rx_avail)
579 DEBUGP("rx_len == 0\n");
581 for (i = 0; i < 1; i++) {
582 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
583 DEBUGP_STATUS_FLAG(tmp);
584 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
585 DEBUGP_ERROR_FLAG(tmp);
588 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
592 return rc632_fifo_read(handle, *rx_len, rx_buf);
593 /* FIXME: discard additional bytes in FIFO */
596 #define MAX_WRITE_LEN 16 /* see Sec. 18.6.1.2 of RC632 Spec Rev. 3.2. */
599 rc632_write_eeprom(struct rfid_asic_handle *handle, u_int16_t addr,
600 u_int8_t *data, u_int8_t len)
602 u_int8_t sndbuf[MAX_WRITE_LEN + 2];
606 if (len > MAX_WRITE_LEN)
613 sndbuf[0] = addr & 0x00ff; /* LSB */
614 sndbuf[1] = addr >> 8; /* MSB */
615 memcpy(&sndbuf[2], data, len);
617 ret = rc632_fifo_write(handle, len + 2, sndbuf, 0x03);
621 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_WRITE_E2);
625 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, ®);
629 if (reg & RC632_ERR_FLAG_ACCESS_ERR)
634 ret = rc632_reg_read(handle, RC632_REG_SECONDARY_STATUS, ®);
638 if (reg & RC632_SEC_ST_E2_READY) {
639 /* the E2Write command must be terminated, See sec. 18.6.1.3 */
640 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_IDLE);
649 rc632_read_eeprom(struct rfid_asic_handle *handle, u_int16_t addr,
650 u_int8_t *buf, u_int8_t len)
656 sndbuf[0] = addr & 0xff;
657 sndbuf[1] = addr >> 8;
660 ret = rc632_fifo_write(handle, 3, sndbuf, 0x03);
664 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_READ_E2);
668 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, ®);
672 if (reg & RC632_ERR_FLAG_ACCESS_ERR)
677 return rc632_fifo_read(handle, len, buf);
681 rc632_calc_crc16_from(struct rfid_asic_handle *handle)
683 u_int8_t sndbuf[2] = { 0x01, 0x02 };
684 u_int8_t crc_lsb = 0x00 , crc_msb = 0x00;
687 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x12);
691 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xe0);
695 ret = rc632_fifo_write(handle, sizeof(sndbuf), sndbuf, 3);
699 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_CALC_CRC);
703 usleep(10000); /* FIXME: no checking for cmd completion? *
705 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_LSB, &crc_lsb);
709 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_MSB, &crc_msb);
713 /* FIXME: what to do with crc result? */
719 rc632_register_dump(struct rfid_asic_handle *handle, u_int8_t *buf)
724 for (i = 0; i <= 0x3f; i++)
725 ret |= rc632_reg_read(handle, i, &buf[i]);
730 /* generic FIFO access functions (if no more efficient ones provided by
731 * transport driver) */
736 /* FIXME: implementation (not needed for CM 5121) */
743 /* FIXME: implementation (not neded for CM 5121) */
748 rc632_init(struct rfid_asic_handle *ah)
752 /* switch off rf (make sure PICCs are reset at init time) */
753 ret = rc632_power(ah, 0);
760 ret = rc632_power(ah, 1);
764 /* disable register paging */
765 ret = rc632_reg_write(ah, 0x00, 0x00);
769 /* set some sane default values */
770 ret = rc632_reg_write(ah, 0x11, 0x5b);
775 ret = rc632_rf_power(ah, 0);
782 ret = rc632_rf_power(ah, 1);
790 rc632_fini(struct rfid_asic_handle *ah)
795 ret = rc632_rf_power(ah, 0);
799 ret = rc632_power(ah, 0);
806 struct rfid_asic_handle *
807 rc632_open(struct rfid_asic_transport_handle *th)
809 struct rfid_asic_handle *h;
811 h = malloc_asic_handle(sizeof(*h));
814 memset(h, 0, sizeof(*h));
816 h->asic = (void*)&rc632;
819 /* FIXME: this is only cm5121 specific, since the latency
820 * down to the RC632 FIFO is too long to refill during TX/RX */
821 h->mtu = h->mru = 64;
823 if (rc632_init(h) < 0) {
832 rc632_close(struct rfid_asic_handle *h)
842 /* Register file for ISO14443A standard */
843 static struct register_file iso14443a_script[] = {
845 .reg = RC632_REG_TX_CONTROL,
846 .val = RC632_TXCTRL_MOD_SRC_INT |
847 RC632_TXCTRL_TX2_INV |
848 RC632_TXCTRL_FORCE_100_ASK |
849 RC632_TXCTRL_TX2_RF_EN |
850 RC632_TXCTRL_TX1_RF_EN,
852 .reg = RC632_REG_CW_CONDUCTANCE,
853 .val = CM5121_CW_CONDUCTANCE,
855 .reg = RC632_REG_MOD_CONDUCTANCE,
856 .val = CM5121_MOD_CONDUCTANCE,
858 .reg = RC632_REG_CODER_CONTROL,
859 .val = (RC632_CDRCTRL_TXCD_14443A |
860 RC632_CDRCTRL_RATE_106K),
862 .reg = RC632_REG_MOD_WIDTH,
865 .reg = RC632_REG_MOD_WIDTH_SOF,
868 .reg = RC632_REG_TYPE_B_FRAMING,
871 .reg = RC632_REG_RX_CONTROL1,
872 .val = (RC632_RXCTRL1_GAIN_35DB |
873 RC632_RXCTRL1_ISO14443 |
874 RC632_RXCTRL1_SUBCP_8),
876 .reg = RC632_REG_DECODER_CONTROL,
877 .val = (RC632_DECCTRL_MANCHESTER |
878 RC632_DECCTRL_RXFR_14443A),
880 .reg = RC632_REG_BIT_PHASE,
881 .val = CM5121_14443A_BITPHASE,
883 .reg = RC632_REG_RX_THRESHOLD,
884 .val = CM5121_14443A_THRESHOLD,
886 .reg = RC632_REG_BPSK_DEM_CONTROL,
889 .reg = RC632_REG_RX_CONTROL2,
890 .val = (RC632_RXCTRL2_DECSRC_INT |
891 RC632_RXCTRL2_CLK_Q),
893 .reg = RC632_REG_RX_WAIT,
894 //.val = 0x03, /* default value */
895 .val = 0x06, /* omnikey */
897 .reg = RC632_REG_CHANNEL_REDUNDANCY,
898 .val = (RC632_CR_PARITY_ENABLE |
899 RC632_CR_PARITY_ODD),
901 .reg = RC632_REG_CRC_PRESET_LSB,
904 .reg = RC632_REG_CRC_PRESET_MSB,
910 rc632_iso14443a_init(struct rfid_asic_handle *handle)
914 /* flush fifo (our way) */
915 ret = rc632_reg_write(handle, RC632_REG_CONTROL,
916 RC632_CONTROL_FIFO_FLUSH);
918 ret = rc632_execute_script(handle, iso14443a_script,
919 ARRAY_SIZE(iso14443a_script));
927 rc632_iso14443a_fini(struct iso14443a_handle *handle_14443)
931 ret = rc632_rf_power(handle, 0);
941 /* issue a 14443-3 A PCD -> PICC command in a short frame, such as REQA, WUPA */
943 rc632_iso14443a_transceive_sf(struct rfid_asic_handle *handle,
945 struct iso14443a_atqa *atqa)
952 memset(atqa, 0, sizeof(*atqa));
956 /* transfer only 7 bits of last byte in frame */
957 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
961 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
962 RC632_CONTROL_CRYPTO1_ON);
967 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
968 (RC632_CR_PARITY_ENABLE |
969 RC632_CR_PARITY_ODD));
971 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
972 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
978 ret = rc632_transceive(handle, tx_buf, sizeof(tx_buf),
979 (u_int8_t *)atqa, &rx_len,
980 ISO14443A_FDT_ANTICOL_LAST1, 0);
982 DEBUGP("error during rc632_transceive()\n");
986 /* switch back to normal 8bit last byte */
987 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
991 /* determine whether there was a collission */
992 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
996 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
998 /* retrieve bit of collission */
999 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1002 DEBUGP("collision detected in xcv_sf: bit_of_col=%u\n", boc);
1003 /* FIXME: how to signal this up the stack */
1007 DEBUGP("rx_len(%d) != 2\n", rx_len);
1014 /* transceive regular frame */
1016 rc632_iso14443ab_transceive(struct rfid_asic_handle *handle,
1017 unsigned int frametype,
1018 const u_int8_t *tx_buf, unsigned int tx_len,
1019 u_int8_t *rx_buf, unsigned int *rx_len,
1020 u_int64_t timeout, unsigned int flags)
1024 u_int8_t channel_red;
1031 memset(rx_buf, 0, *rx_len);
1033 switch (frametype) {
1034 case RFID_14443A_FRAME_REGULAR:
1035 case RFID_MIFARE_FRAME:
1036 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
1037 |RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
1039 case RFID_14443B_FRAME_REGULAR:
1040 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
1044 case RFID_MIFARE_FRAME:
1045 channel_red = RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
1048 case RFID_15693_FRAME:
1049 channel_red = RC632_CR_CRC3309 | RC632_CR_RX_CRC_ENABLE
1050 | RC632_CR_TX_CRC_ENABLE;
1052 case RFID_15693_FRAME_ICODE1:
1053 /* FIXME: implement */
1058 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1062 DEBUGP("tx_len=%u\n",tx_len);
1063 ret = rc632_transceive(handle, tx_buf, tx_len, rx_buf, &rxl, timeout, 0);
1072 /* transceive anti collission bitframe */
1074 rc632_iso14443a_transceive_acf(struct rfid_asic_handle *handle,
1075 struct iso14443a_anticol_cmd *acf,
1076 unsigned int *bit_of_col)
1079 u_int8_t rx_buf[64];
1080 u_int8_t rx_len = sizeof(rx_buf);
1081 u_int8_t rx_align = 0, tx_last_bits, tx_bytes, tx_bytes_total;
1083 u_int8_t error_flag;
1084 *bit_of_col = ISO14443A_BITOFCOL_NONE;
1085 memset(rx_buf, 0, sizeof(rx_buf));
1087 /* disable mifare cryto */
1088 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
1089 RC632_CONTROL_CRYPTO1_ON);
1093 /* disable CRC summing */
1095 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1096 (RC632_CR_PARITY_ENABLE |
1097 RC632_CR_PARITY_ODD));
1099 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
1100 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
1105 tx_last_bits = acf->nvb & 0x07; /* lower nibble indicates bits */
1106 tx_bytes = ( acf->nvb >> 4 ) & 0x07;
1108 tx_bytes_total = tx_bytes+1;
1109 rx_align = tx_last_bits & 0x07; /* rx frame complements tx */
1112 tx_bytes_total = tx_bytes;
1114 /* set RxAlign and TxLastBits*/
1115 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING,
1116 (rx_align << 4) | (tx_last_bits));
1120 ret = rc632_transceive(handle, (u_int8_t *)acf, tx_bytes_total,
1121 rx_buf, &rx_len, 0x32, 0);
1125 /* bitwise-OR the two halves of the split byte */
1126 acf->uid_bits[tx_bytes-2] = (
1127 (acf->uid_bits[tx_bytes-2] & (0xff >> (8-tx_last_bits)))
1132 memcpy(&acf->uid_bits[tx_bytes-1], &rx_buf[1], rx_len-1);
1134 /* determine whether there was a collission */
1135 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1139 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1140 /* retrieve bit of collission */
1141 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1145 /* bit of collission relative to start of part 1 of
1146 * anticollision frame (!) */
1147 *bit_of_col = 2*8 + boc;
1154 RC632_RATE_106 = 0x00,
1155 RC632_RATE_212 = 0x01,
1156 RC632_RATE_424 = 0x02,
1157 RC632_RATE_848 = 0x03,
1161 u_int8_t subc_pulses;
1163 u_int8_t rx_threshold;
1164 u_int8_t bpsk_dem_ctrl;
1172 static struct rx_config rx_configs[] = {
1174 .subc_pulses = RC632_RXCTRL1_SUBCP_8,
1175 .rx_coding = RC632_DECCTRL_MANCHESTER,
1176 .rx_threshold = 0x88,
1177 .bpsk_dem_ctrl = 0x00,
1180 .subc_pulses = RC632_RXCTRL1_SUBCP_4,
1181 .rx_coding = RC632_DECCTRL_BPSK,
1182 .rx_threshold = 0x50,
1183 .bpsk_dem_ctrl = 0x0c,
1186 .subc_pulses = RC632_RXCTRL1_SUBCP_2,
1187 .rx_coding = RC632_DECCTRL_BPSK,
1188 .rx_threshold = 0x50,
1189 .bpsk_dem_ctrl = 0x0c,
1192 .subc_pulses = RC632_RXCTRL1_SUBCP_1,
1193 .rx_coding = RC632_DECCTRL_BPSK,
1194 .rx_threshold = 0x50,
1195 .bpsk_dem_ctrl = 0x0c,
1199 static struct tx_config tx_configs[] = {
1201 .rate = RC632_CDRCTRL_RATE_106K,
1205 .rate = RC632_CDRCTRL_RATE_212K,
1209 .rate = RC632_CDRCTRL_RATE_424K,
1213 .rate = RC632_CDRCTRL_RATE_848K,
1218 static int rc632_iso14443a_set_speed(struct rfid_asic_handle *handle,
1219 unsigned int tx, unsigned int rate)
1227 if (rate > ARRAY_SIZE(rx_configs))
1230 rc = rc632_set_bit_mask(handle, RC632_REG_RX_CONTROL1,
1231 RC632_RXCTRL1_SUBCP_MASK,
1232 rx_configs[rate].subc_pulses);
1236 rc = rc632_set_bit_mask(handle, RC632_REG_DECODER_CONTROL,
1238 rx_configs[rate].rx_coding);
1242 rc = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
1243 rx_configs[rate].rx_threshold);
1247 if (rx_configs[rate].rx_coding == RC632_DECCTRL_BPSK) {
1248 rc = rc632_reg_write(handle,
1249 RC632_REG_BPSK_DEM_CONTROL,
1250 rx_configs[rate].bpsk_dem_ctrl);
1256 if (rate > ARRAY_SIZE(tx_configs))
1259 rc = rc632_set_bit_mask(handle, RC632_REG_CODER_CONTROL,
1260 RC632_CDRCTRL_RATE_MASK,
1261 tx_configs[rate].rate);
1265 rc = rc632_reg_write(handle, RC632_REG_MOD_WIDTH,
1266 tx_configs[rate].mod_width);
1275 static struct register_file iso14443b_script[] = {
1277 .reg = RC632_REG_TX_CONTROL,
1278 .val = (RC632_TXCTRL_TX1_RF_EN |
1279 RC632_TXCTRL_TX2_RF_EN |
1280 RC632_TXCTRL_TX2_INV |
1281 RC632_TXCTRL_MOD_SRC_INT),
1283 .reg = RC632_REG_CW_CONDUCTANCE,
1286 .reg = RC632_REG_MOD_CONDUCTANCE,
1289 .reg = RC632_REG_CODER_CONTROL,
1290 .val = (RC632_CDRCTRL_TXCD_NRZ |
1291 RC632_CDRCTRL_RATE_14443B),
1293 .reg = RC632_REG_MOD_WIDTH,
1296 .reg = RC632_REG_MOD_WIDTH_SOF,
1299 .reg = RC632_REG_TYPE_B_FRAMING,
1300 .val = (RC632_TBFRAMING_SOF_11L_3H |
1301 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
1302 RC632_TBFRAMING_EOF_11);
1304 .reg = RC632_REG_RX_CONTROL1,
1305 .val = (RC632_RXCTRL1_GAIN_35DB |
1306 RC632_RXCTRL1_ISO14443,
1307 RC632_RXCTRL1_SUBCP_8),
1309 .reg = RC632_REG_DECODER_CONTROL,
1310 .val = (RC632_DECCTRL_BPSK |
1311 RC632_DECCTRL_RXFR_14443B),
1313 .reg = RC632_REG_BIT_PHASE,
1314 .val = CM5121_14443B_BITPHASE,
1316 .reg = RC632_REG_RX_THRESHOLD,
1317 .val = CM5121_14443B_THRESHOLD,
1319 .reg = RC632_REG_BPSK_DEM_CONTROL,
1320 .val = ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
1321 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
1322 RC632_BPSKD_FILTER_AMP_DETECT |
1323 RC632_BPSKD_NO_RX_EOF |
1324 RC632_BPSKD_NO_RX_EGT),
1326 .reg = RC632_REG_RX_CONTROL2,
1327 .val = (RC632_RXCTRL2_AUTO_PD |
1328 RC632_RXCTRL2_DECSRC_INT),
1330 .reg = RC632_REG_RX_WAIT,
1333 .reg = RC632_REG_CHANNEL_REDUNDANCY,
1334 .val = (RC632_CR_TX_CRC_ENABLE |
1335 RC632_CR_RX_CRC_ENABLE |
1338 .reg = RC632_REG_CRC_PRESET_LSB,
1341 .reg = RC632_REG_CRC_PRESET_MSB,
1347 static int rc632_iso14443b_init(struct rfid_asic_handle *handle)
1351 /* FIXME: some FIFO work */
1353 /* flush fifo (our way) */
1354 ret = rc632_reg_write(handle, RC632_REG_CONTROL,
1355 RC632_CONTROL_FIFO_FLUSH);
1359 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
1360 (RC632_TXCTRL_TX1_RF_EN |
1361 RC632_TXCTRL_TX2_RF_EN |
1362 RC632_TXCTRL_TX2_INV |
1363 RC632_TXCTRL_MOD_SRC_INT));
1367 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE, 0x3f);
1371 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE, 0x04);
1375 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
1376 (RC632_CDRCTRL_TXCD_NRZ |
1377 RC632_CDRCTRL_RATE_14443B));
1381 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
1385 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1389 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING,
1390 (RC632_TBFRAMING_SOF_11L_3H |
1391 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
1392 RC632_TBFRAMING_EOF_11));
1396 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
1397 (RC632_RXCTRL1_GAIN_35DB |
1398 RC632_RXCTRL1_ISO14443 |
1399 RC632_RXCTRL1_SUBCP_8));
1403 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
1404 (RC632_DECCTRL_BPSK |
1405 RC632_DECCTRL_RXFR_14443B));
1409 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
1410 CM5121_14443B_BITPHASE);
1414 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
1415 CM5121_14443B_THRESHOLD);
1419 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL,
1420 ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
1421 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
1422 RC632_BPSKD_FILTER_AMP_DETECT |
1423 RC632_BPSKD_NO_RX_EOF |
1424 RC632_BPSKD_NO_RX_EGT));
1428 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
1429 (RC632_RXCTRL2_AUTO_PD |
1430 RC632_RXCTRL2_DECSRC_INT));
1434 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
1438 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1439 (RC632_CR_TX_CRC_ENABLE |
1440 RC632_CR_RX_CRC_ENABLE |
1445 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0xff);
1449 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xff);
1462 /* Register file for ISO15693 standard */
1463 static struct register_file iso15693_fast_script[] = {
1465 .reg = RC632_REG_TX_CONTROL,
1466 .val = RC632_TXCTRL_MOD_SRC_INT |
1467 RC632_TXCTRL_TX2_INV |
1468 RC632_TXCTRL_TX2_RF_EN |
1469 RC632_TXCTRL_TX1_RF_EN,
1471 .reg = RC632_REG_CW_CONDUCTANCE,
1474 .reg = RC632_REG_MOD_CONDUCTANCE,
1475 /* FIXME: nxp default for icode1/15693: 0x05 */
1477 .val = 0x21, /* omnikey */
1479 .reg = RC632_REG_CODER_CONTROL,
1480 .val = RC632_CDRCTRL_TXCD_15693_FAST |
1481 RC632_CDRCTRL_RATE_15693,
1483 .reg = RC632_REG_MOD_WIDTH,
1486 .reg = RC632_REG_MOD_WIDTH_SOF,
1489 .reg = RC632_REG_TYPE_B_FRAMING,
1492 .reg = RC632_REG_RX_CONTROL1,
1493 .val = RC632_RXCTRL1_ISO15693 |
1494 RC632_RXCTRL1_SUBCP_16 |
1495 RC632_RXCTRL1_GAIN_35DB,
1497 /* FIXME: this should always be the case */
1498 .reg = RC632_REG_RX_CONTROL2,
1499 .val = RC632_RXCTRL2_DECSRC_INT,
1501 .reg = RC632_REG_DECODER_CONTROL,
1502 .val = RC632_DECCTRL_MANCHESTER |
1503 RC632_DECCTRL_RX_INVERT |
1504 RC632_DECCTRL_ZEROAFTERCOL |
1505 RC632_DECCTRL_RXFR_15693,
1507 .reg = RC632_REG_BIT_PHASE,
1508 /* FIXME: nxp default for icode1/15693: 0x54 */
1510 .val = 0xd0, /* omnikey */
1512 .reg = RC632_REG_RX_THRESHOLD,
1513 /* FIXME: nxp default for icode1/15693: 0x68 */
1517 .reg = RC632_REG_BPSK_DEM_CONTROL,
1520 .reg = RC632_REG_CHANNEL_REDUNDANCY,
1521 .val = RC632_CR_RX_CRC_ENABLE |
1522 RC632_CR_TX_CRC_ENABLE |
1525 .reg = RC632_REG_CRC_PRESET_LSB,
1528 .reg = RC632_REG_CRC_PRESET_MSB,
1533 /* Register file for I*Code standard */
1534 static struct register_file icode1_std_script[] = {
1536 .reg = RC632_REG_TX_CONTROL,
1537 .val = RC632_TXCTRL_MOD_SRC_INT |
1538 RC632_TXCTRL_TX2_INV |
1539 RC632_TXCTRL_TX2_RF_EN |
1540 RC632_TXCTRL_TX1_RF_EN,
1542 .reg = RC632_REG_CW_CONDUCTANCE,
1545 .reg = RC632_REG_MOD_CONDUCTANCE,
1546 /* FIXME: nxp default for icode1/15693: 0x05 */
1549 .reg = RC632_REG_CODER_CONTROL,
1550 .val = RC632_CDRCTRL_TXCD_ICODE_STD |
1551 RC632_CDRCTRL_RATE_15693,
1553 .reg = RC632_REG_MOD_WIDTH,
1556 .reg = RC632_REG_MOD_WIDTH_SOF,
1559 .reg = RC632_REG_TYPE_B_FRAMING,
1562 .reg = RC632_REG_RX_CONTROL1,
1563 .val = RC632_RXCTRL1_ISO15693 |
1564 RC632_RXCTRL1_SUBCP_16 |
1565 RC632_RXCTRL1_GAIN_35DB,
1567 /* FIXME: this should always be the case */
1568 .reg = RC632_REG_RX_CONTROL2,
1569 .val = RC632_RXCTRL2_DECSRC_INT,
1571 .reg = RC632_REG_DECODER_CONTROL,
1572 .val = RC632_DECCTRL_MANCHESTER |
1573 RC632_DECCTRL_RXFR_ICODE,
1575 .reg = RC632_REG_BIT_PHASE,
1576 /* FIXME: nxp default for icode1/15693: 0x54 */
1579 .reg = RC632_REG_RX_THRESHOLD,
1580 /* FIXME: nxp default for icode1/15693: 0x68 */
1583 .reg = RC632_REG_BPSK_DEM_CONTROL,
1586 .reg = RC632_REG_CHANNEL_REDUNDANCY,
1587 /* 16bit CRC, no parity, not CRC3309 */
1588 .val = RC632_CR_RX_CRC_ENABLE |
1589 RC632_CR_TX_CRC_ENABLE,
1591 .reg = RC632_REG_CRC_PRESET_LSB,
1594 .reg = RC632_REG_CRC_PRESET_MSB,
1597 .reg = RC632_REG_INTERRUPT_EN,
1598 .val = RC632_INT_IDLE |
1605 /* incremental changes on top of icode1_std_script */
1606 static struct register_file icode1_fast_patch[] = {
1608 .reg = RC632_REG_CODER_CONTROL,
1609 .val = RC632_CDRCTRL_TXCD_ICODE_FAST |
1610 RC632_CDRCTRL_RATE_ICODE_FAST,
1612 .reg = RC632_REG_MOD_WIDTH_SOF,
1613 .val = 0x73, /* 18.88uS */
1619 rc632_iso15693_init(struct rfid_asic_handle *h)
1623 /* flush fifo (our way) */
1624 ret = rc632_reg_write(h, RC632_REG_CONTROL,
1625 RC632_CONTROL_FIFO_FLUSH);
1629 ret = rc632_execute_script(h, iso15693_fast_script,
1630 ARRAY_SIZE(iso15693_fast_script));
1638 rc632_iso15693_icode1_init(struct rfid_asic_handle *h)
1642 ret = rc632_execute_script(h, icode1_std_script,
1643 ARRAY_SIZE(icode1_std_script));
1647 /* FIXME: how to configure fast/slow properly? */
1650 ret = rc632_execute_script(h, icode1_fast_patch,
1651 ARRAY_SIZE(icode1_fast_patch));
1661 rc632_iso15693_icl_init(struct rfid_asic_handle *h)
1667 ret = rc632_reg_write(h, RC632_REG_TX_CONTROL,
1668 (RC632_TXCTRL_MOD_SRC_INT |
1669 RC632_TXCTRL_TX2_INV |
1670 RC632_TXCTRL_TX2_RF_EN |
1671 RC632_TXCTRL_TX1_RF_EN));
1675 ret = rc632_reg_write(h, RC632_REG_CW_CONDUCTANCE, 0x3f);
1679 ret = rc632_reg_write(h, RC632_REG_MOD_CONDUCTANCE, 0x11);
1683 ret = rc632_reg_write(h, RC632_REG_CODER_CONTROL,
1684 (RC632_CDRCTRL_RATE_15693 |
1685 RC632_CDRCTRL_TXCD_ICODE_STD |
1690 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH, 0x3f);
1694 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1697 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL1,
1698 (RC632_RXCTRL1_SUBCP_16|
1699 RC632_RXCTRL1_ISO15693|
1700 RC632_RXCTRL1_GAIN_35DB));
1703 ret = rc632_reg_write(h, RC632_REG_DECODER_CONTROL,
1704 (RC632_DECCTRL_RX_INVERT|
1705 RC632_DECCTRL_RXFR_15693));
1709 ret = rc632_reg_write(h, RC632_REG_BIT_PHASE, 0xbd);
1713 ret = rc632_reg_write(h, RC632_REG_RX_THRESHOLD, 0xff);
1717 ret = rc632_reg_write(h, RC632_REG_BPSK_DEM_CONTROL, 0x00);
1721 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL2,
1722 RC632_RXCTRL2_DECSRC_INT);
1726 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY, 0x00);
1730 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_LSB, 0x12);
1734 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_MSB, 0xe0);
1741 static void uuid_reversecpy(unsigned char* out, unsigned char* in, int len)
1752 rc632_iso15693_transceive_ac(struct rfid_asic_handle *handle,
1753 const struct iso15693_anticol_cmd *acf,
1754 unsigned int acf_len,
1755 struct iso15693_anticol_resp *resp,
1756 unsigned int *rx_len, unsigned char *bit_of_col)
1758 u_int8_t error_flag, boc;
1761 int ret, tx_len, mask_len_bytes;
1762 unsigned int rate = ISO15693_T_SLOW;
1764 if (acf->req.flags & RFID_15693_F_RATE_HIGH)
1765 rate = ISO15693_T_FAST;
1767 DEBUGP("acf = %s\n", rfid_hexdump(acf, acf_len));
1769 ret = rc632_transceive(handle, (u_int8_t *)acf, acf_len,
1770 (u_int8_t *) resp, rx_len,
1771 iso15693_timing[rate][ISO15693_T1], 0);
1772 if (ret == -ETIMEDOUT || ret == -EIO)
1775 /* determine whether there was a collission */
1776 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1779 DEBUGP_ERROR_FLAG(error_flag);
1781 //FIXME: check for framing and crc errors...
1782 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1783 /* retrieve bit of collission */
1784 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1790 if (error_flag & RC632_ERR_FLAG_CRC_ERR)
1799 mask_len_bytes = (acf->mask_len % 8) ? acf->mask_len/8+1 : acf->mask_len/8;
1801 if (acf->current_slot == 0) {
1802 /* first call: transmit Inventory frame */
1803 DEBUGP("first_frame\n");
1805 tx_len = sizeof(struct iso15693_request) + 1 + mask_len_bytes;
1807 ret = rc632_transceive(handle, (u_int8_t *)&req, tx_len,
1808 (u_int8_t *)&rx_buf, &rx_len, ISO15693_T1, 0);
1809 acf->current_slot = 1;
1810 DEBUGP("rc632_transceive ret: %d rx_len: %d\n",ret,rx_len);
1811 /* if ((ret < 0)&&(ret != -ETIMEDOUT))
1815 /* second++ call: end timeslot with EOFpulse and read */
1816 DEBUGP("second++_frame\n");
1817 if ((acf->current_slot > 16) ||
1818 ((acf->flags & RFID_15693_F5_NSLOTS_1 == 0)
1819 && (acf->current_slot > 1))) {
1821 memset(uuid, 0, ISO15693_UID_LEN);
1825 /* reset EOF-pulse-bit to 0 */
1826 ret = rc632_clear_bits(handle, RC632_REG_CODER_CONTROL,
1827 RC632_CDRCTRL_15693_EOF_PULSE);
1829 /* generate EOF pulse */
1830 ret = rc632_set_bits(handle, RC632_REG_CODER_CONTROL,
1831 RC632_CDRCTRL_15693_EOF_PULSE);
1834 // DEBUGP("waiting for EOF pulse\n");
1835 // ret = rc632_wait_idle(handle, 10); //wait for idle
1837 rx_len = sizeof(rx_buf);
1838 ret = rc632_receive(handle, (u_int8_t*)&rx_buf, &rx_len, ISO15693_T3);
1839 DEBUGP("rc632_receive ret: %d rx_len: %d\n", ret, rx_len);
1840 acf->current_slot++;
1842 /* if ((ret < 0)&&(ret != -ETIMEDOUT))
1846 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
1847 DEBUGP_STATUS_FLAG(tmp);
1849 if (ret == -ETIMEDOUT) {
1850 /* no VICC answer in this timeslot*/
1851 memset(uuid, 0, ISO15693_UID_LEN);
1854 /* determine whether there was a collission */
1855 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1856 DEBUGP_ERROR_FLAG(error_flag);
1860 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1861 /* retrieve bit of collission */
1862 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1866 memcpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1867 // uuid_reversecpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1868 DEBUGP("Collision in slot %d bit %d\n",
1869 acf->current_slot,boc);
1872 /* no collision-> retrieve uuid */
1873 DEBUGP("no collision in slot %d\n", acf->current_slot);
1874 memcpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1875 //uuid_reversecpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1883 struct mifare_authcmd {
1885 u_int8_t block_address;
1886 u_int32_t serno; /* lsb 1 2 msb */
1887 } __attribute__ ((packed));
1890 #define RFID_MIFARE_KEY_LEN 6
1891 #define RFID_MIFARE_KEY_CODED_LEN 12
1893 /* Transform crypto1 key from generic 6byte into rc632 specific 12byte */
1895 rc632_mifare_transform_key(const u_int8_t *key6, u_int8_t *key12)
1901 for (i = 0; i < RFID_MIFARE_KEY_LEN; i++) {
1902 ln = key6[i] & 0x0f;
1904 key12[i * 2 + 1] = (~ln << 4) | ln;
1905 key12[i * 2] = (~hn << 4) | hn;
1911 rc632_mifare_set_key(struct rfid_asic_handle *h, const u_int8_t *key)
1913 u_int8_t coded_key[RFID_MIFARE_KEY_CODED_LEN];
1917 ret = rc632_mifare_transform_key(key, coded_key);
1921 /* Terminate probably running command */
1922 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_IDLE);
1926 ret = rc632_fifo_write(h, RFID_MIFARE_KEY_CODED_LEN, coded_key, 0x03);
1930 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY);
1934 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
1938 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1939 ret = rc632_wait_idle_timer(h);
1943 ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, ®);
1947 if (reg & RC632_ERR_FLAG_KEY_ERR)
1954 rc632_mifare_set_key_ee(struct rfid_asic_handle *h, unsigned int addr)
1957 u_int8_t cmd_addr[2];
1960 if (addr > 0xffff - RFID_MIFARE_KEY_CODED_LEN)
1963 cmd_addr[0] = addr & 0xff; /* LSB */
1964 cmd_addr[1] = (addr >> 8) & 0xff; /* MSB */
1966 /* Terminate probably running command */
1967 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_IDLE);
1971 /* Write the key address to the FIFO */
1972 ret = rc632_fifo_write(h, 2, cmd_addr, 0x03);
1976 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY_E2);
1980 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
1984 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1985 ret = rc632_wait_idle_timer(h);
1989 ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, ®);
1993 if (reg & RC632_ERR_FLAG_KEY_ERR)
2000 rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno,
2004 struct mifare_authcmd acmd;
2007 if (cmd != RFID_CMD_MIFARE_AUTH1A && cmd != RFID_CMD_MIFARE_AUTH1B) {
2008 DEBUGP("invalid auth command\n");
2012 /* Initialize acmd */
2013 acmd.block_address = block & 0xff;
2014 acmd.auth_cmd = cmd;
2015 //acmd.serno = htonl(serno);
2020 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
2021 RC632_CR_RX_CRC_ENABLE);
2023 /* Clear Rx CRC, Set Tx CRC and Odd Parity */
2024 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY,
2025 RC632_CR_TX_CRC_ENABLE | RC632_CR_PARITY_ODD |
2026 RC632_CR_PARITY_ENABLE);
2031 /* Send Authent1 Command */
2032 ret = rc632_fifo_write(h, sizeof(acmd), (unsigned char *)&acmd, 0x03);
2036 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT1);
2038 DEBUGP("error during AUTHENT1");
2042 /* Wait until transmitter is idle */
2043 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
2047 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
2048 ret = rc632_wait_idle_timer(h);
2052 ret = rc632_reg_read(h, RC632_REG_SECONDARY_STATUS, ®);
2056 DEBUGP("bitframe?");
2061 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
2062 RC632_CR_TX_CRC_ENABLE);
2066 /* Wait until transmitter is idle */
2067 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
2071 /* Send Authent2 Command */
2072 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT2);
2076 /* Wait until transmitter is idle */
2077 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
2078 ret = rc632_wait_idle_timer(h);
2082 /* Check whether authentication was successful */
2083 ret = rc632_reg_read(h, RC632_REG_CONTROL, ®);
2087 if (!(reg & RC632_CONTROL_CRYPTO1_ON)) {
2088 DEBUGP("authentication not successful");
2095 /* transceive regular frame */
2097 rc632_mifare_transceive(struct rfid_asic_handle *handle,
2098 const u_int8_t *tx_buf, unsigned int tx_len,
2099 u_int8_t *rx_buf, unsigned int *rx_len,
2100 u_int64_t timeout, unsigned int flags)
2103 u_int8_t rxl = *rx_len & 0xff;
2105 DEBUGP("entered\n");
2106 memset(rx_buf, 0, *rx_len);
2109 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
2110 (RC632_CR_PARITY_ENABLE |
2111 RC632_CR_PARITY_ODD |
2112 RC632_CR_TX_CRC_ENABLE |
2113 RC632_CR_RX_CRC_ENABLE));
2115 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
2116 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
2121 ret = rc632_transceive(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
2132 rc632_layer2_init(struct rfid_asic_handle *h, enum rfid_layer2_id l2)
2135 case RFID_LAYER2_ISO14443A:
2136 return rc632_iso14443a_init(h);
2137 case RFID_LAYER2_ISO14443B:
2138 return rc632_iso14443b_init(h);
2139 case RFID_LAYER2_ISO15693:
2140 return rc632_iso15693_init(h);
2141 case RFID_LAYER2_ICODE1:
2142 return rc632_iso15693_icode1_init(h);
2148 const struct rfid_asic rc632 = {
2149 .name = "Philips CL RC632",
2150 .fc = ISO14443_FREQ_CARRIER,
2153 .power = &rc632_power,
2154 .rf_power = &rc632_rf_power,
2155 .transceive = &rc632_iso14443ab_transceive,
2156 .init = &rc632_layer2_init,
2158 .transceive_sf = &rc632_iso14443a_transceive_sf,
2159 .transceive_acf = &rc632_iso14443a_transceive_acf,
2160 .set_speed = &rc632_iso14443a_set_speed,
2163 .transceive_ac = &rc632_iso15693_transceive_ac,
2166 .setkey = &rc632_mifare_set_key,
2167 .setkey_ee = &rc632_mifare_set_key_ee,
2168 .auth = &rc632_mifare_auth,