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, 0x00);
451 /* clear all interrupts */
452 ret = rc632_reg_write(handle, RC632_REG_INTERRUPT_RQ, 0x7f);
453 ret = rc632_reg_write(handle, RC632_REG_ERROR_FLAG, 0xff);
456 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
457 DEBUGP_STATUS_FLAG(tmp);
458 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
459 DEBUGP_STATUS_FLAG(tmp);
460 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
461 DEBUGP_STATUS_FLAG(tmp);
462 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
463 DEBUGP_ERROR_FLAG(tmp);
466 ret = rc632_timer_set(handle, timer);
471 ret = rc632_fifo_write(handle, cur_tx_len, cur_tx_buf, 0x03);
475 if (cur_tx_buf == tx_buf) {
476 ret = rc632_reg_write(handle, RC632_REG_COMMAND,
477 RC632_CMD_TRANSCEIVE);
482 cur_tx_buf += cur_tx_len;
483 if (cur_tx_buf < tx_buf + tx_len) {
485 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH,
490 cur_tx_len = 64 - fifo_fill;
494 } while (cur_tx_len);
497 tcl_toggle_pcb(handle);
499 ret = rc632_wait_idle_timer(handle);
500 //ret = rc632_wait_idle(handle, timer);
502 DEBUGP("rc632_wait_idle >> ret=%d %s\n",ret,(ret==-ETIMEDOUT)?"ETIMEDOUT":"");
506 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, &rx_avail);
510 if (rx_avail > *rx_len)
511 DEBUGP("rx_avail(%d) > rx_len(%d), JFYI\n", rx_avail, *rx_len);
512 else if (*rx_len > rx_avail)
515 DEBUGP("rx_len == %d\n",*rx_len);
520 for (i = 0; i < 1; i++){
521 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
522 DEBUGP_STATUS_FLAG(tmp);
523 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
524 DEBUGP_ERROR_FLAG(tmp);
526 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
532 return rc632_fifo_read(handle, *rx_len, rx_buf);
533 /* FIXME: discard addidional bytes in FIFO */
538 rc632_receive(struct rfid_asic_handle *handle,
543 int ret, cur_tx_len, i;
546 DEBUGP("timeout=%u, rx_len=%u\n", timer, *rx_len);
547 ret = rc632_reg_write(handle, RC632_REG_COMMAND, 0x00); /* IDLE */
548 /* clear all interrupts */
549 ret = rc632_reg_write(handle, RC632_REG_INTERRUPT_RQ, 0x7f);
551 ret = rc632_timer_set(handle, timer);
555 ret = rc632_reg_write(handle, RC632_REG_COMMAND,RC632_CMD_RECEIVE);
559 /* the timer cannot start in hardware based on the command we just
560 * sent. this means that our timing will always be quite a bit more lax,
561 * i.e. we'll always wait for a bit longer than the specs ask us to. */
562 ret = rc632_set_bits(handle, RC632_REG_CONTROL,
563 RC632_CONTROL_TIMER_START);
567 //ret = rc632_wait_idle(handle, timer);
568 ret = rc632_wait_idle_timer(handle);
572 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, &rx_avail);
576 if (rx_avail > *rx_len) {
577 //DEBUGP("rx_avail(%d) > rx_len(%d), JFYI\n", rx_avail, *rx_len);
578 } else if (*rx_len > rx_avail)
584 DEBUGP("rx_len == 0\n");
586 for (i = 0; i < 1; i++) {
587 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
588 DEBUGP_STATUS_FLAG(tmp);
589 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
590 DEBUGP_ERROR_FLAG(tmp);
593 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
597 return rc632_fifo_read(handle, *rx_len, rx_buf);
598 /* FIXME: discard additional bytes in FIFO */
601 #define MAX_WRITE_LEN 16 /* see Sec. 18.6.1.2 of RC632 Spec Rev. 3.2. */
604 rc632_write_eeprom(struct rfid_asic_handle *handle, u_int16_t addr,
605 u_int8_t *data, u_int8_t len)
607 u_int8_t sndbuf[MAX_WRITE_LEN + 2];
611 if (len > MAX_WRITE_LEN)
618 sndbuf[0] = addr & 0x00ff; /* LSB */
619 sndbuf[1] = addr >> 8; /* MSB */
620 memcpy(&sndbuf[2], data, len);
622 ret = rc632_fifo_write(handle, len + 2, sndbuf, 0x03);
626 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_WRITE_E2);
630 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, ®);
634 if (reg & RC632_ERR_FLAG_ACCESS_ERR)
639 ret = rc632_reg_read(handle, RC632_REG_SECONDARY_STATUS, ®);
643 if (reg & RC632_SEC_ST_E2_READY) {
644 /* the E2Write command must be terminated, See sec. 18.6.1.3 */
645 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_IDLE);
654 rc632_read_eeprom(struct rfid_asic_handle *handle, u_int16_t addr,
655 u_int8_t *buf, u_int8_t len)
661 sndbuf[0] = addr & 0xff;
662 sndbuf[1] = addr >> 8;
665 ret = rc632_fifo_write(handle, 3, sndbuf, 0x03);
669 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_READ_E2);
673 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, ®);
677 if (reg & RC632_ERR_FLAG_ACCESS_ERR)
682 return rc632_fifo_read(handle, len, buf);
686 rc632_calc_crc16_from(struct rfid_asic_handle *handle)
688 u_int8_t sndbuf[2] = { 0x01, 0x02 };
689 u_int8_t crc_lsb = 0x00 , crc_msb = 0x00;
692 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x12);
696 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xe0);
700 ret = rc632_fifo_write(handle, sizeof(sndbuf), sndbuf, 3);
704 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_CALC_CRC);
708 usleep(10000); /* FIXME: no checking for cmd completion? *
710 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_LSB, &crc_lsb);
714 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_MSB, &crc_msb);
718 /* FIXME: what to do with crc result? */
724 rc632_register_dump(struct rfid_asic_handle *handle, u_int8_t *buf)
729 for (i = 0; i <= 0x3f; i++)
730 ret |= rc632_reg_read(handle, i, &buf[i]);
735 /* generic FIFO access functions (if no more efficient ones provided by
736 * transport driver) */
741 /* FIXME: implementation (not needed for CM 5121) */
748 /* FIXME: implementation (not neded for CM 5121) */
753 rc632_init(struct rfid_asic_handle *ah)
757 /* switch off rf (make sure PICCs are reset at init time) */
758 ret = rc632_power(ah, 0);
765 ret = rc632_power(ah, 1);
769 /* disable register paging */
770 ret = rc632_reg_write(ah, 0x00, 0x00);
774 /* set some sane default values */
775 ret = rc632_reg_write(ah, 0x11, 0x5b);
780 ret = rc632_rf_power(ah, 0);
787 ret = rc632_rf_power(ah, 1);
795 rc632_fini(struct rfid_asic_handle *ah)
800 ret = rc632_rf_power(ah, 0);
804 ret = rc632_power(ah, 0);
811 struct rfid_asic_handle *
812 rc632_open(struct rfid_asic_transport_handle *th)
814 struct rfid_asic_handle *h;
816 h = malloc_asic_handle(sizeof(*h));
819 memset(h, 0, sizeof(*h));
821 h->asic = (void*)&rc632;
824 /* FIXME: this is only cm5121 specific, since the latency
825 * down to the RC632 FIFO is too long to refill during TX/RX */
826 h->mtu = h->mru = 64;
828 if (rc632_init(h) < 0) {
837 rc632_close(struct rfid_asic_handle *h)
847 /* Register file for ISO14443A standard */
848 static struct register_file iso14443a_script[] = {
850 .reg = RC632_REG_TX_CONTROL,
851 .val = RC632_TXCTRL_MOD_SRC_INT |
852 RC632_TXCTRL_TX2_INV |
853 RC632_TXCTRL_FORCE_100_ASK |
854 RC632_TXCTRL_TX2_RF_EN |
855 RC632_TXCTRL_TX1_RF_EN,
857 .reg = RC632_REG_CW_CONDUCTANCE,
858 .val = CM5121_CW_CONDUCTANCE,
860 .reg = RC632_REG_MOD_CONDUCTANCE,
861 .val = CM5121_MOD_CONDUCTANCE,
863 .reg = RC632_REG_CODER_CONTROL,
864 .val = (RC632_CDRCTRL_TXCD_14443A |
865 RC632_CDRCTRL_RATE_106K),
867 .reg = RC632_REG_MOD_WIDTH,
870 .reg = RC632_REG_MOD_WIDTH_SOF,
873 .reg = RC632_REG_TYPE_B_FRAMING,
876 .reg = RC632_REG_RX_CONTROL1,
877 .val = (RC632_RXCTRL1_GAIN_35DB |
878 RC632_RXCTRL1_ISO14443 |
879 RC632_RXCTRL1_SUBCP_8),
881 .reg = RC632_REG_DECODER_CONTROL,
882 .val = (RC632_DECCTRL_MANCHESTER |
883 RC632_DECCTRL_RXFR_14443A),
885 .reg = RC632_REG_BIT_PHASE,
886 .val = CM5121_14443A_BITPHASE,
888 .reg = RC632_REG_RX_THRESHOLD,
889 .val = CM5121_14443A_THRESHOLD,
891 .reg = RC632_REG_BPSK_DEM_CONTROL,
894 .reg = RC632_REG_RX_CONTROL2,
895 .val = (RC632_RXCTRL2_DECSRC_INT |
896 RC632_RXCTRL2_CLK_Q),
898 .reg = RC632_REG_RX_WAIT,
899 //.val = 0x03, /* default value */
900 .val = 0x06, /* omnikey */
902 .reg = RC632_REG_CHANNEL_REDUNDANCY,
903 .val = (RC632_CR_PARITY_ENABLE |
904 RC632_CR_PARITY_ODD),
906 .reg = RC632_REG_CRC_PRESET_LSB,
909 .reg = RC632_REG_CRC_PRESET_MSB,
915 rc632_iso14443a_init(struct rfid_asic_handle *handle)
919 /* flush fifo (our way) */
920 ret = rc632_reg_write(handle, RC632_REG_CONTROL,
921 RC632_CONTROL_FIFO_FLUSH);
923 ret = rc632_execute_script(handle, iso14443a_script,
924 ARRAY_SIZE(iso14443a_script));
932 rc632_iso14443a_fini(struct iso14443a_handle *handle_14443)
936 ret = rc632_rf_power(handle, 0);
946 /* issue a 14443-3 A PCD -> PICC command in a short frame, such as REQA, WUPA */
948 rc632_iso14443a_transceive_sf(struct rfid_asic_handle *handle,
950 struct iso14443a_atqa *atqa)
957 memset(atqa, 0, sizeof(*atqa));
961 /* transfer only 7 bits of last byte in frame */
962 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
966 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
967 RC632_CONTROL_CRYPTO1_ON);
972 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
973 (RC632_CR_PARITY_ENABLE |
974 RC632_CR_PARITY_ODD));
976 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
977 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
983 ret = rc632_transceive(handle, tx_buf, sizeof(tx_buf),
984 (u_int8_t *)atqa, &rx_len,
985 ISO14443A_FDT_ANTICOL_LAST1, 0);
987 DEBUGP("error during rc632_transceive()\n");
991 /* switch back to normal 8bit last byte */
992 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
996 /* determine whether there was a collission */
997 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1001 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1003 /* retrieve bit of collission */
1004 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1007 DEBUGP("collision detected in xcv_sf: bit_of_col=%u\n", boc);
1008 /* FIXME: how to signal this up the stack */
1012 DEBUGP("rx_len(%d) != 2\n", rx_len);
1019 /* transceive regular frame */
1021 rc632_iso14443ab_transceive(struct rfid_asic_handle *handle,
1022 unsigned int frametype,
1023 const u_int8_t *tx_buf, unsigned int tx_len,
1024 u_int8_t *rx_buf, unsigned int *rx_len,
1025 u_int64_t timeout, unsigned int flags)
1029 u_int8_t channel_red;
1036 memset(rx_buf, 0, *rx_len);
1038 switch (frametype) {
1039 case RFID_14443A_FRAME_REGULAR:
1040 case RFID_MIFARE_FRAME:
1041 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
1042 |RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
1044 case RFID_14443B_FRAME_REGULAR:
1045 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
1049 case RFID_MIFARE_FRAME:
1050 channel_red = RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
1053 case RFID_15693_FRAME:
1054 channel_red = RC632_CR_CRC3309 | RC632_CR_RX_CRC_ENABLE
1055 | RC632_CR_TX_CRC_ENABLE;
1057 case RFID_15693_FRAME_ICODE1:
1058 /* FIXME: implement */
1063 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1067 DEBUGP("tx_len=%u\n",tx_len);
1068 ret = rc632_transceive(handle, tx_buf, tx_len, rx_buf, &rxl, timeout, 0);
1077 /* transceive anti collission bitframe */
1079 rc632_iso14443a_transceive_acf(struct rfid_asic_handle *handle,
1080 struct iso14443a_anticol_cmd *acf,
1081 unsigned int *bit_of_col)
1084 u_int8_t rx_buf[64];
1085 u_int8_t rx_len = sizeof(rx_buf);
1086 u_int8_t rx_align = 0, tx_last_bits, tx_bytes, tx_bytes_total;
1088 u_int8_t error_flag;
1089 *bit_of_col = ISO14443A_BITOFCOL_NONE;
1090 memset(rx_buf, 0, sizeof(rx_buf));
1092 /* disable mifare cryto */
1093 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
1094 RC632_CONTROL_CRYPTO1_ON);
1098 /* disable CRC summing */
1100 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1101 (RC632_CR_PARITY_ENABLE |
1102 RC632_CR_PARITY_ODD));
1104 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
1105 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
1110 tx_last_bits = acf->nvb & 0x07; /* lower nibble indicates bits */
1111 tx_bytes = ( acf->nvb >> 4 ) & 0x07;
1113 tx_bytes_total = tx_bytes+1;
1114 rx_align = tx_last_bits & 0x07; /* rx frame complements tx */
1117 tx_bytes_total = tx_bytes;
1119 /* set RxAlign and TxLastBits*/
1120 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING,
1121 (rx_align << 4) | (tx_last_bits));
1125 ret = rc632_transceive(handle, (u_int8_t *)acf, tx_bytes_total,
1126 rx_buf, &rx_len, 0x32, 0);
1130 /* bitwise-OR the two halves of the split byte */
1131 acf->uid_bits[tx_bytes-2] = (
1132 (acf->uid_bits[tx_bytes-2] & (0xff >> (8-tx_last_bits)))
1137 memcpy(&acf->uid_bits[tx_bytes-1], &rx_buf[1], rx_len-1);
1139 /* determine whether there was a collission */
1140 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1144 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1145 /* retrieve bit of collission */
1146 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1150 /* bit of collission relative to start of part 1 of
1151 * anticollision frame (!) */
1152 *bit_of_col = 2*8 + boc;
1159 RC632_RATE_106 = 0x00,
1160 RC632_RATE_212 = 0x01,
1161 RC632_RATE_424 = 0x02,
1162 RC632_RATE_848 = 0x03,
1166 u_int8_t subc_pulses;
1168 u_int8_t rx_threshold;
1169 u_int8_t bpsk_dem_ctrl;
1177 static struct rx_config rx_configs[] = {
1179 .subc_pulses = RC632_RXCTRL1_SUBCP_8,
1180 .rx_coding = RC632_DECCTRL_MANCHESTER,
1181 .rx_threshold = 0x88,
1182 .bpsk_dem_ctrl = 0x00,
1185 .subc_pulses = RC632_RXCTRL1_SUBCP_4,
1186 .rx_coding = RC632_DECCTRL_BPSK,
1187 .rx_threshold = 0x50,
1188 .bpsk_dem_ctrl = 0x0c,
1191 .subc_pulses = RC632_RXCTRL1_SUBCP_2,
1192 .rx_coding = RC632_DECCTRL_BPSK,
1193 .rx_threshold = 0x50,
1194 .bpsk_dem_ctrl = 0x0c,
1197 .subc_pulses = RC632_RXCTRL1_SUBCP_1,
1198 .rx_coding = RC632_DECCTRL_BPSK,
1199 .rx_threshold = 0x50,
1200 .bpsk_dem_ctrl = 0x0c,
1204 static struct tx_config tx_configs[] = {
1206 .rate = RC632_CDRCTRL_RATE_106K,
1210 .rate = RC632_CDRCTRL_RATE_212K,
1214 .rate = RC632_CDRCTRL_RATE_424K,
1218 .rate = RC632_CDRCTRL_RATE_848K,
1223 static int rc632_iso14443a_set_speed(struct rfid_asic_handle *handle,
1224 unsigned int tx, unsigned int rate)
1232 if (rate > ARRAY_SIZE(rx_configs))
1235 rc = rc632_set_bit_mask(handle, RC632_REG_RX_CONTROL1,
1236 RC632_RXCTRL1_SUBCP_MASK,
1237 rx_configs[rate].subc_pulses);
1241 rc = rc632_set_bit_mask(handle, RC632_REG_DECODER_CONTROL,
1243 rx_configs[rate].rx_coding);
1247 rc = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
1248 rx_configs[rate].rx_threshold);
1252 if (rx_configs[rate].rx_coding == RC632_DECCTRL_BPSK) {
1253 rc = rc632_reg_write(handle,
1254 RC632_REG_BPSK_DEM_CONTROL,
1255 rx_configs[rate].bpsk_dem_ctrl);
1261 if (rate > ARRAY_SIZE(tx_configs))
1264 rc = rc632_set_bit_mask(handle, RC632_REG_CODER_CONTROL,
1265 RC632_CDRCTRL_RATE_MASK,
1266 tx_configs[rate].rate);
1270 rc = rc632_reg_write(handle, RC632_REG_MOD_WIDTH,
1271 tx_configs[rate].mod_width);
1280 static struct register_file iso14443b_script[] = {
1282 .reg = RC632_REG_TX_CONTROL,
1283 .val = (RC632_TXCTRL_TX1_RF_EN |
1284 RC632_TXCTRL_TX2_RF_EN |
1285 RC632_TXCTRL_TX2_INV |
1286 RC632_TXCTRL_MOD_SRC_INT),
1288 .reg = RC632_REG_CW_CONDUCTANCE,
1291 .reg = RC632_REG_MOD_CONDUCTANCE,
1294 .reg = RC632_REG_CODER_CONTROL,
1295 .val = (RC632_CDRCTRL_TXCD_NRZ |
1296 RC632_CDRCTRL_RATE_14443B),
1298 .reg = RC632_REG_MOD_WIDTH,
1301 .reg = RC632_REG_MOD_WIDTH_SOF,
1304 .reg = RC632_REG_TYPE_B_FRAMING,
1305 .val = (RC632_TBFRAMING_SOF_11L_3H |
1306 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
1307 RC632_TBFRAMING_EOF_11);
1309 .reg = RC632_REG_RX_CONTROL1,
1310 .val = (RC632_RXCTRL1_GAIN_35DB |
1311 RC632_RXCTRL1_ISO14443,
1312 RC632_RXCTRL1_SUBCP_8),
1314 .reg = RC632_REG_DECODER_CONTROL,
1315 .val = (RC632_DECCTRL_BPSK |
1316 RC632_DECCTRL_RXFR_14443B),
1318 .reg = RC632_REG_BIT_PHASE,
1319 .val = CM5121_14443B_BITPHASE,
1321 .reg = RC632_REG_RX_THRESHOLD,
1322 .val = CM5121_14443B_THRESHOLD,
1324 .reg = RC632_REG_BPSK_DEM_CONTROL,
1325 .val = ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
1326 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
1327 RC632_BPSKD_FILTER_AMP_DETECT |
1328 RC632_BPSKD_NO_RX_EOF |
1329 RC632_BPSKD_NO_RX_EGT),
1331 .reg = RC632_REG_RX_CONTROL2,
1332 .val = (RC632_RXCTRL2_AUTO_PD |
1333 RC632_RXCTRL2_DECSRC_INT),
1335 .reg = RC632_REG_RX_WAIT,
1338 .reg = RC632_REG_CHANNEL_REDUNDANCY,
1339 .val = (RC632_CR_TX_CRC_ENABLE |
1340 RC632_CR_RX_CRC_ENABLE |
1343 .reg = RC632_REG_CRC_PRESET_LSB,
1346 .reg = RC632_REG_CRC_PRESET_MSB,
1352 static int rc632_iso14443b_init(struct rfid_asic_handle *handle)
1356 /* FIXME: some FIFO work */
1358 /* flush fifo (our way) */
1359 ret = rc632_reg_write(handle, RC632_REG_CONTROL,
1360 RC632_CONTROL_FIFO_FLUSH);
1364 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
1365 (RC632_TXCTRL_TX1_RF_EN |
1366 RC632_TXCTRL_TX2_RF_EN |
1367 RC632_TXCTRL_TX2_INV |
1368 RC632_TXCTRL_MOD_SRC_INT));
1372 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE, 0x3f);
1376 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE, 0x04);
1380 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
1381 (RC632_CDRCTRL_TXCD_NRZ |
1382 RC632_CDRCTRL_RATE_14443B));
1386 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
1390 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1394 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING,
1395 (RC632_TBFRAMING_SOF_11L_3H |
1396 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
1397 RC632_TBFRAMING_EOF_11));
1401 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
1402 (RC632_RXCTRL1_GAIN_35DB |
1403 RC632_RXCTRL1_ISO14443 |
1404 RC632_RXCTRL1_SUBCP_8));
1408 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
1409 (RC632_DECCTRL_BPSK |
1410 RC632_DECCTRL_RXFR_14443B));
1414 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
1415 CM5121_14443B_BITPHASE);
1419 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
1420 CM5121_14443B_THRESHOLD);
1424 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL,
1425 ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
1426 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
1427 RC632_BPSKD_FILTER_AMP_DETECT |
1428 RC632_BPSKD_NO_RX_EOF |
1429 RC632_BPSKD_NO_RX_EGT));
1433 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
1434 (RC632_RXCTRL2_AUTO_PD |
1435 RC632_RXCTRL2_DECSRC_INT));
1439 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
1443 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1444 (RC632_CR_TX_CRC_ENABLE |
1445 RC632_CR_RX_CRC_ENABLE |
1450 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0xff);
1454 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xff);
1467 /* Register file for ISO15693 standard */
1468 static struct register_file iso15693_fast_script[] = {
1470 .reg = RC632_REG_TX_CONTROL,
1471 .val = RC632_TXCTRL_MOD_SRC_INT |
1472 RC632_TXCTRL_TX2_INV |
1473 RC632_TXCTRL_TX2_RF_EN |
1474 RC632_TXCTRL_TX1_RF_EN,
1476 .reg = RC632_REG_CW_CONDUCTANCE,
1479 .reg = RC632_REG_MOD_CONDUCTANCE,
1480 /* FIXME: nxp default for icode1/15693: 0x05 */
1482 .val = 0x21, /* omnikey */
1484 .reg = RC632_REG_CODER_CONTROL,
1485 .val = RC632_CDRCTRL_TXCD_15693_FAST |
1486 RC632_CDRCTRL_RATE_15693,
1488 .reg = RC632_REG_MOD_WIDTH,
1491 .reg = RC632_REG_MOD_WIDTH_SOF,
1494 .reg = RC632_REG_TYPE_B_FRAMING,
1497 .reg = RC632_REG_RX_CONTROL1,
1498 .val = RC632_RXCTRL1_ISO15693 |
1499 RC632_RXCTRL1_SUBCP_16 |
1500 RC632_RXCTRL1_GAIN_35DB,
1502 /* FIXME: this should always be the case */
1503 .reg = RC632_REG_RX_CONTROL2,
1504 .val = RC632_RXCTRL2_DECSRC_INT,
1506 .reg = RC632_REG_DECODER_CONTROL,
1507 .val = RC632_DECCTRL_MANCHESTER |
1508 RC632_DECCTRL_RX_INVERT |
1509 RC632_DECCTRL_ZEROAFTERCOL |
1510 RC632_DECCTRL_RXFR_15693,
1512 .reg = RC632_REG_BIT_PHASE,
1513 /* FIXME: nxp default for icode1/15693: 0x54 */
1515 .val = 0xd0, /* omnikey */
1517 .reg = RC632_REG_RX_THRESHOLD,
1518 /* FIXME: nxp default for icode1/15693: 0x68 */
1522 .reg = RC632_REG_BPSK_DEM_CONTROL,
1525 .reg = RC632_REG_CHANNEL_REDUNDANCY,
1526 .val = RC632_CR_RX_CRC_ENABLE |
1527 RC632_CR_TX_CRC_ENABLE |
1530 .reg = RC632_REG_CRC_PRESET_LSB,
1533 .reg = RC632_REG_CRC_PRESET_MSB,
1538 /* Register file for I*Code standard */
1539 static struct register_file icode1_std_script[] = {
1541 .reg = RC632_REG_TX_CONTROL,
1542 .val = RC632_TXCTRL_MOD_SRC_INT |
1543 RC632_TXCTRL_TX2_INV |
1544 RC632_TXCTRL_TX2_RF_EN |
1545 RC632_TXCTRL_TX1_RF_EN,
1547 .reg = RC632_REG_CW_CONDUCTANCE,
1550 .reg = RC632_REG_MOD_CONDUCTANCE,
1551 /* FIXME: nxp default for icode1/15693: 0x05 */
1554 .reg = RC632_REG_CODER_CONTROL,
1555 .val = RC632_CDRCTRL_TXCD_ICODE_STD |
1556 RC632_CDRCTRL_RATE_15693,
1558 .reg = RC632_REG_MOD_WIDTH,
1561 .reg = RC632_REG_MOD_WIDTH_SOF,
1564 .reg = RC632_REG_TYPE_B_FRAMING,
1567 .reg = RC632_REG_RX_CONTROL1,
1568 .val = RC632_RXCTRL1_ISO15693 |
1569 RC632_RXCTRL1_SUBCP_16 |
1570 RC632_RXCTRL1_GAIN_35DB,
1572 /* FIXME: this should always be the case */
1573 .reg = RC632_REG_RX_CONTROL2,
1574 .val = RC632_RXCTRL2_DECSRC_INT,
1576 .reg = RC632_REG_DECODER_CONTROL,
1577 .val = RC632_DECCTRL_MANCHESTER |
1578 RC632_DECCTRL_RXFR_ICODE,
1580 .reg = RC632_REG_BIT_PHASE,
1581 /* FIXME: nxp default for icode1/15693: 0x54 */
1584 .reg = RC632_REG_RX_THRESHOLD,
1585 /* FIXME: nxp default for icode1/15693: 0x68 */
1588 .reg = RC632_REG_BPSK_DEM_CONTROL,
1591 .reg = RC632_REG_CHANNEL_REDUNDANCY,
1592 /* 16bit CRC, no parity, not CRC3309 */
1593 .val = RC632_CR_RX_CRC_ENABLE |
1594 RC632_CR_TX_CRC_ENABLE,
1596 .reg = RC632_REG_CRC_PRESET_LSB,
1599 .reg = RC632_REG_CRC_PRESET_MSB,
1602 .reg = RC632_REG_INTERRUPT_EN,
1603 .val = RC632_INT_IDLE |
1610 /* incremental changes on top of icode1_std_script */
1611 static struct register_file icode1_fast_patch[] = {
1613 .reg = RC632_REG_CODER_CONTROL,
1614 .val = RC632_CDRCTRL_TXCD_ICODE_FAST |
1615 RC632_CDRCTRL_RATE_ICODE_FAST,
1617 .reg = RC632_REG_MOD_WIDTH_SOF,
1618 .val = 0x73, /* 18.88uS */
1624 rc632_iso15693_init(struct rfid_asic_handle *h)
1628 /* flush fifo (our way) */
1629 ret = rc632_reg_write(h, RC632_REG_CONTROL,
1630 RC632_CONTROL_FIFO_FLUSH);
1634 ret = rc632_execute_script(h, iso15693_fast_script,
1635 ARRAY_SIZE(iso15693_fast_script));
1643 rc632_iso15693_icode1_init(struct rfid_asic_handle *h)
1647 ret = rc632_execute_script(h, icode1_std_script,
1648 ARRAY_SIZE(icode1_std_script));
1652 /* FIXME: how to configure fast/slow properly? */
1655 ret = rc632_execute_script(h, icode1_fast_patch,
1656 ARRAY_SIZE(icode1_fast_patch));
1666 rc632_iso15693_icl_init(struct rfid_asic_handle *h)
1672 ret = rc632_reg_write(h, RC632_REG_TX_CONTROL,
1673 (RC632_TXCTRL_MOD_SRC_INT |
1674 RC632_TXCTRL_TX2_INV |
1675 RC632_TXCTRL_TX2_RF_EN |
1676 RC632_TXCTRL_TX1_RF_EN));
1680 ret = rc632_reg_write(h, RC632_REG_CW_CONDUCTANCE, 0x3f);
1684 ret = rc632_reg_write(h, RC632_REG_MOD_CONDUCTANCE, 0x11);
1688 ret = rc632_reg_write(h, RC632_REG_CODER_CONTROL,
1689 (RC632_CDRCTRL_RATE_15693 |
1690 RC632_CDRCTRL_TXCD_ICODE_STD |
1695 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH, 0x3f);
1699 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1702 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL1,
1703 (RC632_RXCTRL1_SUBCP_16|
1704 RC632_RXCTRL1_ISO15693|
1705 RC632_RXCTRL1_GAIN_35DB));
1708 ret = rc632_reg_write(h, RC632_REG_DECODER_CONTROL,
1709 (RC632_DECCTRL_RX_INVERT|
1710 RC632_DECCTRL_RXFR_15693));
1714 ret = rc632_reg_write(h, RC632_REG_BIT_PHASE, 0xbd);
1718 ret = rc632_reg_write(h, RC632_REG_RX_THRESHOLD, 0xff);
1722 ret = rc632_reg_write(h, RC632_REG_BPSK_DEM_CONTROL, 0x00);
1726 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL2,
1727 RC632_RXCTRL2_DECSRC_INT);
1731 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY, 0x00);
1735 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_LSB, 0x12);
1739 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_MSB, 0xe0);
1746 static void uuid_reversecpy(unsigned char* out, unsigned char* in, int len)
1757 rc632_iso15693_transceive_ac(struct rfid_asic_handle *handle,
1758 const struct iso15693_anticol_cmd *acf,
1759 unsigned int acf_len,
1760 struct iso15693_anticol_resp *resp,
1761 unsigned int *rx_len, unsigned char *bit_of_col)
1763 u_int8_t error_flag, boc;
1766 int ret, tx_len, mask_len_bytes;
1767 unsigned int rate = ISO15693_T_SLOW;
1769 if (acf->req.flags & RFID_15693_F_RATE_HIGH)
1770 rate = ISO15693_T_FAST;
1772 DEBUGP("acf = %s\n", rfid_hexdump(acf, acf_len));
1774 ret = rc632_transceive(handle, (u_int8_t *)acf, acf_len,
1775 (u_int8_t *) resp, rx_len,
1776 iso15693_timing[rate][ISO15693_T1], 0);
1777 if (ret == -ETIMEDOUT)
1780 /* determine whether there was a collission */
1781 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1784 DEBUGP_ERROR_FLAG(error_flag);
1786 //FIXME: check for framing and crc errors...
1787 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1788 /* retrieve bit of collission */
1789 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1802 mask_len_bytes = (acf->mask_len % 8) ? acf->mask_len/8+1 : acf->mask_len/8;
1804 if (acf->current_slot == 0) {
1805 /* first call: transmit Inventory frame */
1806 DEBUGP("first_frame\n");
1808 tx_len = sizeof(struct iso15693_request) + 1 + mask_len_bytes;
1810 ret = rc632_transceive(handle, (u_int8_t *)&req, tx_len,
1811 (u_int8_t *)&rx_buf, &rx_len, ISO15693_T1, 0);
1812 acf->current_slot = 1;
1813 DEBUGP("rc632_transceive ret: %d rx_len: %d\n",ret,rx_len);
1814 /* if ((ret < 0)&&(ret != -ETIMEDOUT))
1818 /* second++ call: end timeslot with EOFpulse and read */
1819 DEBUGP("second++_frame\n");
1820 if ((acf->current_slot > 16) ||
1821 ((acf->flags & RFID_15693_F5_NSLOTS_1 == 0)
1822 && (acf->current_slot > 1))) {
1824 memset(uuid, 0, ISO15693_UID_LEN);
1828 /* reset EOF-pulse-bit to 0 */
1829 ret = rc632_clear_bits(handle, RC632_REG_CODER_CONTROL,
1830 RC632_CDRCTRL_15693_EOF_PULSE);
1832 /* generate EOF pulse */
1833 ret = rc632_set_bits(handle, RC632_REG_CODER_CONTROL,
1834 RC632_CDRCTRL_15693_EOF_PULSE);
1837 // DEBUGP("waiting for EOF pulse\n");
1838 // ret = rc632_wait_idle(handle, 10); //wait for idle
1840 rx_len = sizeof(rx_buf);
1841 ret = rc632_receive(handle, (u_int8_t*)&rx_buf, &rx_len, ISO15693_T3);
1842 DEBUGP("rc632_receive ret: %d rx_len: %d\n", ret, rx_len);
1843 acf->current_slot++;
1845 /* if ((ret < 0)&&(ret != -ETIMEDOUT))
1849 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
1850 DEBUGP_STATUS_FLAG(tmp);
1852 if (ret == -ETIMEDOUT) {
1853 /* no VICC answer in this timeslot*/
1854 memset(uuid, 0, ISO15693_UID_LEN);
1857 /* determine whether there was a collission */
1858 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1859 DEBUGP_ERROR_FLAG(error_flag);
1863 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1864 /* retrieve bit of collission */
1865 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1869 memcpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1870 // uuid_reversecpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1871 DEBUGP("Collision in slot %d bit %d\n",
1872 acf->current_slot,boc);
1875 /* no collision-> retrieve uuid */
1876 DEBUGP("no collision in slot %d\n", acf->current_slot);
1877 memcpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1878 //uuid_reversecpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1886 struct mifare_authcmd {
1888 u_int8_t block_address;
1889 u_int32_t serno; /* lsb 1 2 msb */
1890 } __attribute__ ((packed));
1893 #define RFID_MIFARE_KEY_LEN 6
1894 #define RFID_MIFARE_KEY_CODED_LEN 12
1896 /* Transform crypto1 key from generic 6byte into rc632 specific 12byte */
1898 rc632_mifare_transform_key(const u_int8_t *key6, u_int8_t *key12)
1904 for (i = 0; i < RFID_MIFARE_KEY_LEN; i++) {
1905 ln = key6[i] & 0x0f;
1907 key12[i * 2 + 1] = (~ln << 4) | ln;
1908 key12[i * 2] = (~hn << 4) | hn;
1914 rc632_mifare_set_key(struct rfid_asic_handle *h, const u_int8_t *key)
1916 u_int8_t coded_key[RFID_MIFARE_KEY_CODED_LEN];
1920 ret = rc632_mifare_transform_key(key, coded_key);
1924 /* Terminate probably running command */
1925 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_IDLE);
1929 ret = rc632_fifo_write(h, RFID_MIFARE_KEY_CODED_LEN, coded_key, 0x03);
1933 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY);
1937 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
1941 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1942 ret = rc632_wait_idle_timer(h);
1946 ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, ®);
1950 if (reg & RC632_ERR_FLAG_KEY_ERR)
1957 rc632_mifare_set_key_ee(struct rfid_asic_handle *h, unsigned int addr)
1960 u_int8_t cmd_addr[2];
1963 if (addr > 0xffff - RFID_MIFARE_KEY_CODED_LEN)
1966 cmd_addr[0] = addr & 0xff; /* LSB */
1967 cmd_addr[1] = (addr >> 8) & 0xff; /* MSB */
1969 /* Terminate probably running command */
1970 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_IDLE);
1974 /* Write the key address to the FIFO */
1975 ret = rc632_fifo_write(h, 2, cmd_addr, 0x03);
1979 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY_E2);
1983 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
1987 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1988 ret = rc632_wait_idle_timer(h);
1992 ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, ®);
1996 if (reg & RC632_ERR_FLAG_KEY_ERR)
2003 rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno,
2007 struct mifare_authcmd acmd;
2010 if (cmd != RFID_CMD_MIFARE_AUTH1A && cmd != RFID_CMD_MIFARE_AUTH1B) {
2011 DEBUGP("invalid auth command\n");
2015 /* Initialize acmd */
2016 acmd.block_address = block & 0xff;
2017 acmd.auth_cmd = cmd;
2018 //acmd.serno = htonl(serno);
2023 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
2024 RC632_CR_RX_CRC_ENABLE);
2026 /* Clear Rx CRC, Set Tx CRC and Odd Parity */
2027 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY,
2028 RC632_CR_TX_CRC_ENABLE | RC632_CR_PARITY_ODD |
2029 RC632_CR_PARITY_ENABLE);
2034 /* Send Authent1 Command */
2035 ret = rc632_fifo_write(h, sizeof(acmd), (unsigned char *)&acmd, 0x03);
2039 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT1);
2041 DEBUGP("error during AUTHENT1");
2045 /* Wait until transmitter is idle */
2046 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
2050 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
2051 ret = rc632_wait_idle_timer(h);
2055 ret = rc632_reg_read(h, RC632_REG_SECONDARY_STATUS, ®);
2059 DEBUGP("bitframe?");
2064 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
2065 RC632_CR_TX_CRC_ENABLE);
2069 /* Wait until transmitter is idle */
2070 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
2074 /* Send Authent2 Command */
2075 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT2);
2079 /* Wait until transmitter is idle */
2080 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
2081 ret = rc632_wait_idle_timer(h);
2085 /* Check whether authentication was successful */
2086 ret = rc632_reg_read(h, RC632_REG_CONTROL, ®);
2090 if (!(reg & RC632_CONTROL_CRYPTO1_ON)) {
2091 DEBUGP("authentication not successful");
2098 /* transceive regular frame */
2100 rc632_mifare_transceive(struct rfid_asic_handle *handle,
2101 const u_int8_t *tx_buf, unsigned int tx_len,
2102 u_int8_t *rx_buf, unsigned int *rx_len,
2103 u_int64_t timeout, unsigned int flags)
2106 u_int8_t rxl = *rx_len & 0xff;
2108 DEBUGP("entered\n");
2109 memset(rx_buf, 0, *rx_len);
2112 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
2113 (RC632_CR_PARITY_ENABLE |
2114 RC632_CR_PARITY_ODD |
2115 RC632_CR_TX_CRC_ENABLE |
2116 RC632_CR_RX_CRC_ENABLE));
2118 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
2119 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
2124 ret = rc632_transceive(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
2135 rc632_layer2_init(struct rfid_asic_handle *h, enum rfid_layer2_id l2)
2138 case RFID_LAYER2_ISO14443A:
2139 return rc632_iso14443a_init(h);
2140 case RFID_LAYER2_ISO14443B:
2141 return rc632_iso14443b_init(h);
2142 case RFID_LAYER2_ISO15693:
2143 return rc632_iso15693_init(h);
2144 case RFID_LAYER2_ICODE1:
2145 return rc632_iso15693_icode1_init(h);
2151 const struct rfid_asic rc632 = {
2152 .name = "Philips CL RC632",
2153 .fc = ISO14443_FREQ_CARRIER,
2156 .power = &rc632_power,
2157 .rf_power = &rc632_rf_power,
2158 .transceive = &rc632_iso14443ab_transceive,
2159 .init = &rc632_layer2_init,
2161 .transceive_sf = &rc632_iso14443a_transceive_sf,
2162 .transceive_acf = &rc632_iso14443a_transceive_acf,
2163 .set_speed = &rc632_iso14443a_set_speed,
2166 .transceive_ac = &rc632_iso15693_transceive_ac,
2169 .setkey = &rc632_mifare_set_key,
2170 .setkey_ee = &rc632_mifare_set_key_ee,
2171 .auth = &rc632_mifare_auth,