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;
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_set_bits(handle, RC632_REG_INTERRUPT_RQ, RC632_IRQ_TIMER);
260 ret |= rc632_reg_write(handle, RC632_REG_TIMER_RELOAD, divisor);
265 /* Wait until RC632 is idle or TIMER IRQ has happened */
266 static int rc632_wait_idle_timer(struct rfid_asic_handle *handle)
269 u_int8_t stat, irq, cmd;
272 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &stat);
273 DEBUGP_STATUS_FLAG(stat);
274 if (stat & RC632_STAT_ERR) {
276 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &err);
277 DEBUGP_ERROR_FLAG(err);
278 if (err & (RC632_ERR_FLAG_COL_ERR |
279 RC632_ERR_FLAG_PARITY_ERR |
280 RC632_ERR_FLAG_FRAMING_ERR |
281 RC632_ERR_FLAG_CRC_ERR))
284 if (stat & RC632_STAT_IRQ) {
285 ret = rc632_reg_read(handle, RC632_REG_INTERRUPT_RQ, &irq);
288 DEBUGP_INTERRUPT_FLAG(irq);
290 if (irq & RC632_IRQ_TIMER && !(irq & RC632_IRQ_RX)) {
291 DEBUGP("timer expired before RX!!\n");
296 ret = rc632_reg_read(handle, RC632_REG_COMMAND, &cmd);
303 /* poll every millisecond */
308 /* Stupid RC632 implementations don't evaluate interrupts but poll the
309 * command register for "status idle" */
311 rc632_wait_idle(struct rfid_asic_handle *handle, u_int64_t timeout)
315 #define USLEEP_PER_CYCLE 128
317 timeout *= TIMER_RELAX_FACTOR;
320 ret = rc632_reg_read(handle, RC632_REG_COMMAND, &cmd);
326 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &foo);
327 DEBUGP_STATUS_FLAG(foo);
328 /* check if Error has occured (ERR flag set) */
329 if (foo & RC632_STAT_ERR) {
330 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &foo);
331 DEBUGP_ERROR_FLAG(foo);
333 /* check if IRQ has occurred (IRQ flag set)*/
334 if (foo & RC632_STAT_IRQ) {
335 ret = rc632_reg_read(handle, RC632_REG_INTERRUPT_RQ, &foo);
336 DEBUGP_INTERRUPT_FLAG(foo);
337 /* clear all interrupts */
338 rc632_clear_irqs(handle, 0xff);
342 /* FIXME: read second time ?? */
343 DEBUGP("cmd == 0 (IDLE)\n");
347 /* Abort after some timeout */
348 if (cycles > timeout/USLEEP_PER_CYCLE) {
349 DEBUGP("timeout...\n");
354 usleep(USLEEP_PER_CYCLE);
361 rc632_transmit(struct rfid_asic_handle *handle,
367 const u_int8_t *cur_buf = buf;
369 DEBUGP("timeout=%u, tx_len=%u\n", timeout, len);
377 ret = rc632_fifo_write(handle, cur_len, cur_buf, 0x03);
381 if (cur_buf == buf) {
382 /* only start transmit first time */
383 ret = rc632_reg_write(handle, RC632_REG_COMMAND,
390 if (cur_buf < buf + len) {
391 cur_len = buf - cur_buf;
399 return rc632_wait_idle(handle, timeout);
403 tcl_toggle_pcb(struct rfid_asic_handle *handle)
405 /* FIXME: toggle something between 0x0a and 0x0b */
410 rc632_transceive(struct rfid_asic_handle *handle,
411 const u_int8_t *tx_buf,
418 int ret, cur_tx_len, i;
420 const u_int8_t *cur_tx_buf = tx_buf;
422 DEBUGP("timeout=%u, rx_len=%u, tx_len=%u\n", timer, *rx_len, tx_len);
430 ret = rc632_reg_write(handle, RC632_REG_COMMAND, 0x00);
431 /* clear all interrupts */
432 ret = rc632_reg_write(handle, RC632_REG_INTERRUPT_RQ, 0x7f);
433 ret = rc632_reg_write(handle, RC632_REG_ERROR_FLAG, 0xff);
436 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
437 DEBUGP_STATUS_FLAG(tmp);
438 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
439 DEBUGP_STATUS_FLAG(tmp);
440 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
441 DEBUGP_STATUS_FLAG(tmp);
442 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
443 DEBUGP_ERROR_FLAG(tmp);
446 ret = rc632_timer_set(handle, timer);
451 ret = rc632_fifo_write(handle, cur_tx_len, cur_tx_buf, 0x03);
455 if (cur_tx_buf == tx_buf) {
456 ret = rc632_reg_write(handle, RC632_REG_COMMAND,
457 RC632_CMD_TRANSCEIVE);
462 cur_tx_buf += cur_tx_len;
463 if (cur_tx_buf < tx_buf + tx_len) {
465 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH,
470 cur_tx_len = 64 - fifo_fill;
474 } while (cur_tx_len);
477 tcl_toggle_pcb(handle);
479 ret = rc632_wait_idle_timer(handle);
480 //ret = rc632_wait_idle(handle, timer);
482 DEBUGP("rc632_wait_idle >> ret=%d %s\n",ret,(ret==-ETIMEDOUT)?"ETIMEDOUT":"");
486 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, &rx_avail);
490 if (rx_avail > *rx_len)
491 DEBUGP("rx_avail(%d) > rx_len(%d), JFYI\n", rx_avail, *rx_len);
492 else if (*rx_len > rx_avail)
495 DEBUGP("rx_len == %d\n",*rx_len);
500 for (i = 0; i < 1; i++){
501 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
502 DEBUGP_STATUS_FLAG(tmp);
503 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
504 DEBUGP_ERROR_FLAG(tmp);
506 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
512 return rc632_fifo_read(handle, *rx_len, rx_buf);
513 /* FIXME: discard addidional bytes in FIFO */
518 rc632_receive(struct rfid_asic_handle *handle,
523 int ret, cur_tx_len, i;
526 DEBUGP("timeout=%u, rx_len=%u\n", timer, *rx_len);
527 ret = rc632_reg_write(handle, RC632_REG_COMMAND, 0x00); /* IDLE */
528 /* clear all interrupts */
529 ret = rc632_reg_write(handle, RC632_REG_INTERRUPT_RQ, 0x7f);
531 ret = rc632_timer_set(handle, timer);
535 ret = rc632_reg_write(handle, RC632_REG_COMMAND,RC632_CMD_RECEIVE);
539 /* the timer cannot start in hardware based on the command we just
540 * sent. this means that our timing will always be quite a bit more lax,
541 * i.e. we'll always wait for a bit longer than the specs ask us to. */
542 ret = rc632_set_bits(handle, RC632_REG_CONTROL,
543 RC632_CONTROL_TIMER_START);
547 //ret = rc632_wait_idle(handle, timer);
548 ret = rc632_wait_idle_timer(handle);
552 ret = rc632_reg_read(handle, RC632_REG_FIFO_LENGTH, &rx_avail);
556 if (rx_avail > *rx_len) {
557 //DEBUGP("rx_avail(%d) > rx_len(%d), JFYI\n", rx_avail, *rx_len);
558 } else if (*rx_len > rx_avail)
564 DEBUGP("rx_len == 0\n");
566 for (i = 0; i < 1; i++) {
567 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
568 DEBUGP_STATUS_FLAG(tmp);
569 rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &tmp);
570 DEBUGP_ERROR_FLAG(tmp);
573 rc632_reg_read(handle, RC632_REG_CHANNEL_REDUNDANCY, &tmp);
577 return rc632_fifo_read(handle, *rx_len, rx_buf);
578 /* FIXME: discard additional bytes in FIFO */
581 #define MAX_WRITE_LEN 16 /* see Sec. 18.6.1.2 of RC632 Spec Rev. 3.2. */
584 rc632_write_eeprom(struct rfid_asic_handle *handle, u_int16_t addr,
585 u_int8_t *data, u_int8_t len)
587 u_int8_t sndbuf[MAX_WRITE_LEN + 2];
591 if (len > MAX_WRITE_LEN)
598 sndbuf[0] = addr & 0x00ff; /* LSB */
599 sndbuf[1] = addr >> 8; /* MSB */
600 memcpy(&sndbuf[2], data, len);
602 ret = rc632_fifo_write(handle, len + 2, sndbuf, 0x03);
606 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_WRITE_E2);
610 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, ®);
614 if (reg & RC632_ERR_FLAG_ACCESS_ERR)
619 ret = rc632_reg_read(handle, RC632_REG_SECONDARY_STATUS, ®);
623 if (reg & RC632_SEC_ST_E2_READY) {
624 /* the E2Write command must be terminated, See sec. 18.6.1.3 */
625 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_IDLE);
634 rc632_read_eeprom(struct rfid_asic_handle *handle, u_int16_t addr,
635 u_int8_t *buf, u_int8_t len)
641 sndbuf[0] = addr & 0xff;
642 sndbuf[1] = addr >> 8;
645 ret = rc632_fifo_write(handle, 3, sndbuf, 0x03);
649 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_READ_E2);
653 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, ®);
657 if (reg & RC632_ERR_FLAG_ACCESS_ERR)
662 return rc632_fifo_read(handle, len, buf);
666 rc632_calc_crc16_from(struct rfid_asic_handle *handle)
668 u_int8_t sndbuf[2] = { 0x01, 0x02 };
669 u_int8_t crc_lsb = 0x00 , crc_msb = 0x00;
672 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0x12);
676 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xe0);
680 ret = rc632_fifo_write(handle, sizeof(sndbuf), sndbuf, 3);
684 ret = rc632_reg_write(handle, RC632_REG_COMMAND, RC632_CMD_CALC_CRC);
688 usleep(10000); /* FIXME: no checking for cmd completion? *
690 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_LSB, &crc_lsb);
694 ret = rc632_reg_read(handle, RC632_REG_CRC_RESULT_MSB, &crc_msb);
698 /* FIXME: what to do with crc result? */
704 rc632_register_dump(struct rfid_asic_handle *handle, u_int8_t *buf)
709 for (i = 0; i <= 0x3f; i++)
710 ret |= rc632_reg_read(handle, i, &buf[i]);
715 /* generic FIFO access functions (if no more efficient ones provided by
716 * transport driver) */
721 /* FIXME: implementation (not needed for CM 5121) */
728 /* FIXME: implementation (not neded for CM 5121) */
733 rc632_init(struct rfid_asic_handle *ah)
737 /* switch off rf (make sure PICCs are reset at init time) */
738 ret = rc632_power(ah, 0);
745 ret = rc632_power(ah, 1);
749 /* disable register paging */
750 ret = rc632_reg_write(ah, 0x00, 0x00);
754 /* set some sane default values */
755 ret = rc632_reg_write(ah, 0x11, 0x5b);
760 ret = rc632_rf_power(ah, 0);
767 ret = rc632_rf_power(ah, 1);
775 rc632_fini(struct rfid_asic_handle *ah)
780 ret = rc632_rf_power(ah, 0);
784 ret = rc632_power(ah, 0);
791 struct rfid_asic_handle *
792 rc632_open(struct rfid_asic_transport_handle *th)
794 struct rfid_asic_handle *h;
796 h = malloc_asic_handle(sizeof(*h));
799 memset(h, 0, sizeof(*h));
801 h->asic = (void*)&rc632;
804 /* FIXME: this is only cm5121 specific, since the latency
805 * down to the RC632 FIFO is too long to refill during TX/RX */
806 h->mtu = h->mru = 64;
808 if (rc632_init(h) < 0) {
817 rc632_close(struct rfid_asic_handle *h)
827 /* Register file for ISO14443A standard */
828 static struct register_file iso14443a_script[] = {
830 .reg = RC632_REG_TX_CONTROL,
831 .val = RC632_TXCTRL_MOD_SRC_INT |
832 RC632_TXCTRL_TX2_INV |
833 RC632_TXCTRL_FORCE_100_ASK |
834 RC632_TXCTRL_TX2_RF_EN |
835 RC632_TXCTRL_TX1_RF_EN,
837 .reg = RC632_REG_CW_CONDUCTANCE,
838 .val = CM5121_CW_CONDUCTANCE,
840 .reg = RC632_REG_MOD_CONDUCTANCE,
841 .val = CM5121_MOD_CONDUCTANCE,
843 .reg = RC632_REG_CODER_CONTROL,
844 .val = (RC632_CDRCTRL_TXCD_14443A |
845 RC632_CDRCTRL_RATE_106K),
847 .reg = RC632_REG_MOD_WIDTH,
850 .reg = RC632_REG_MOD_WIDTH_SOF,
853 .reg = RC632_REG_TYPE_B_FRAMING,
856 .reg = RC632_REG_RX_CONTROL1,
857 .val = (RC632_RXCTRL1_GAIN_35DB |
858 RC632_RXCTRL1_ISO14443 |
859 RC632_RXCTRL1_SUBCP_8),
861 .reg = RC632_REG_DECODER_CONTROL,
862 .val = (RC632_DECCTRL_MANCHESTER |
863 RC632_DECCTRL_RXFR_14443A),
865 .reg = RC632_REG_BIT_PHASE,
866 .val = CM5121_14443A_BITPHASE,
868 .reg = RC632_REG_RX_THRESHOLD,
869 .val = CM5121_14443A_THRESHOLD,
871 .reg = RC632_REG_BPSK_DEM_CONTROL,
874 .reg = RC632_REG_RX_CONTROL2,
875 .val = (RC632_RXCTRL2_DECSRC_INT |
876 RC632_RXCTRL2_CLK_Q),
878 .reg = RC632_REG_RX_WAIT,
879 //.val = 0x03, /* default value */
880 .val = 0x06, /* omnikey */
882 .reg = RC632_REG_CHANNEL_REDUNDANCY,
883 .val = (RC632_CR_PARITY_ENABLE |
884 RC632_CR_PARITY_ODD),
886 .reg = RC632_REG_CRC_PRESET_LSB,
889 .reg = RC632_REG_CRC_PRESET_MSB,
895 rc632_iso14443a_init(struct rfid_asic_handle *handle)
899 /* flush fifo (our way) */
900 ret = rc632_reg_write(handle, RC632_REG_CONTROL,
901 RC632_CONTROL_FIFO_FLUSH);
903 ret = rc632_execute_script(handle, iso14443a_script,
904 ARRAY_SIZE(iso14443a_script));
912 rc632_iso14443a_fini(struct iso14443a_handle *handle_14443)
916 ret = rc632_rf_power(handle, 0);
926 /* issue a 14443-3 A PCD -> PICC command in a short frame, such as REQA, WUPA */
928 rc632_iso14443a_transceive_sf(struct rfid_asic_handle *handle,
930 struct iso14443a_atqa *atqa)
937 memset(atqa, 0, sizeof(*atqa));
941 /* transfer only 7 bits of last byte in frame */
942 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x07);
946 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
947 RC632_CONTROL_CRYPTO1_ON);
952 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
953 (RC632_CR_PARITY_ENABLE |
954 RC632_CR_PARITY_ODD));
956 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
957 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
963 ret = rc632_transceive(handle, tx_buf, sizeof(tx_buf),
964 (u_int8_t *)atqa, &rx_len,
965 ISO14443A_FDT_ANTICOL_LAST1, 0);
967 DEBUGP("error during rc632_transceive()\n");
971 /* switch back to normal 8bit last byte */
972 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING, 0x00);
976 /* determine whether there was a collission */
977 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
981 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
983 /* retrieve bit of collission */
984 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
987 DEBUGP("collision detected in xcv_sf: bit_of_col=%u\n", boc);
988 /* FIXME: how to signal this up the stack */
992 DEBUGP("rx_len(%d) != 2\n", rx_len);
999 /* transceive regular frame */
1001 rc632_iso14443ab_transceive(struct rfid_asic_handle *handle,
1002 unsigned int frametype,
1003 const u_int8_t *tx_buf, unsigned int tx_len,
1004 u_int8_t *rx_buf, unsigned int *rx_len,
1005 u_int64_t timeout, unsigned int flags)
1009 u_int8_t channel_red;
1016 memset(rx_buf, 0, *rx_len);
1018 switch (frametype) {
1019 case RFID_14443A_FRAME_REGULAR:
1020 case RFID_MIFARE_FRAME:
1021 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
1022 |RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
1024 case RFID_14443B_FRAME_REGULAR:
1025 channel_red = RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE
1029 case RFID_MIFARE_FRAME:
1030 channel_red = RC632_CR_PARITY_ENABLE|RC632_CR_PARITY_ODD;
1033 case RFID_15693_FRAME:
1034 channel_red = RC632_CR_CRC3309 | RC632_CR_RX_CRC_ENABLE
1035 | RC632_CR_TX_CRC_ENABLE;
1037 case RFID_15693_FRAME_ICODE1:
1038 /* FIXME: implement */
1043 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1047 DEBUGP("tx_len=%u\n",tx_len);
1048 ret = rc632_transceive(handle, tx_buf, tx_len, rx_buf, &rxl, timeout, 0);
1057 /* transceive anti collission bitframe */
1059 rc632_iso14443a_transceive_acf(struct rfid_asic_handle *handle,
1060 struct iso14443a_anticol_cmd *acf,
1061 unsigned int *bit_of_col)
1064 u_int8_t rx_buf[64];
1065 u_int8_t rx_len = sizeof(rx_buf);
1066 u_int8_t rx_align = 0, tx_last_bits, tx_bytes, tx_bytes_total;
1068 u_int8_t error_flag;
1069 *bit_of_col = ISO14443A_BITOFCOL_NONE;
1070 memset(rx_buf, 0, sizeof(rx_buf));
1072 /* disable mifare cryto */
1073 ret = rc632_clear_bits(handle, RC632_REG_CONTROL,
1074 RC632_CONTROL_CRYPTO1_ON);
1078 /* disable CRC summing */
1080 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1081 (RC632_CR_PARITY_ENABLE |
1082 RC632_CR_PARITY_ODD));
1084 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
1085 RC632_CR_TX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
1090 tx_last_bits = acf->nvb & 0x07; /* lower nibble indicates bits */
1091 tx_bytes = ( acf->nvb >> 4 ) & 0x07;
1093 tx_bytes_total = tx_bytes+1;
1094 rx_align = tx_last_bits & 0x07; /* rx frame complements tx */
1097 tx_bytes_total = tx_bytes;
1099 /* set RxAlign and TxLastBits*/
1100 ret = rc632_reg_write(handle, RC632_REG_BIT_FRAMING,
1101 (rx_align << 4) | (tx_last_bits));
1105 ret = rc632_transceive(handle, (u_int8_t *)acf, tx_bytes_total,
1106 rx_buf, &rx_len, 0x32, 0);
1110 /* bitwise-OR the two halves of the split byte */
1111 acf->uid_bits[tx_bytes-2] = (
1112 (acf->uid_bits[tx_bytes-2] & (0xff >> (8-tx_last_bits)))
1117 memcpy(&acf->uid_bits[tx_bytes-1], &rx_buf[1], rx_len-1);
1119 /* determine whether there was a collission */
1120 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1124 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1125 /* retrieve bit of collission */
1126 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1130 /* bit of collission relative to start of part 1 of
1131 * anticollision frame (!) */
1132 *bit_of_col = 2*8 + boc;
1139 RC632_RATE_106 = 0x00,
1140 RC632_RATE_212 = 0x01,
1141 RC632_RATE_424 = 0x02,
1142 RC632_RATE_848 = 0x03,
1146 u_int8_t subc_pulses;
1148 u_int8_t rx_threshold;
1149 u_int8_t bpsk_dem_ctrl;
1157 static struct rx_config rx_configs[] = {
1159 .subc_pulses = RC632_RXCTRL1_SUBCP_8,
1160 .rx_coding = RC632_DECCTRL_MANCHESTER,
1161 .rx_threshold = 0x88,
1162 .bpsk_dem_ctrl = 0x00,
1165 .subc_pulses = RC632_RXCTRL1_SUBCP_4,
1166 .rx_coding = RC632_DECCTRL_BPSK,
1167 .rx_threshold = 0x50,
1168 .bpsk_dem_ctrl = 0x0c,
1171 .subc_pulses = RC632_RXCTRL1_SUBCP_2,
1172 .rx_coding = RC632_DECCTRL_BPSK,
1173 .rx_threshold = 0x50,
1174 .bpsk_dem_ctrl = 0x0c,
1177 .subc_pulses = RC632_RXCTRL1_SUBCP_1,
1178 .rx_coding = RC632_DECCTRL_BPSK,
1179 .rx_threshold = 0x50,
1180 .bpsk_dem_ctrl = 0x0c,
1184 static struct tx_config tx_configs[] = {
1186 .rate = RC632_CDRCTRL_RATE_106K,
1190 .rate = RC632_CDRCTRL_RATE_212K,
1194 .rate = RC632_CDRCTRL_RATE_424K,
1198 .rate = RC632_CDRCTRL_RATE_848K,
1203 static int rc632_iso14443a_set_speed(struct rfid_asic_handle *handle,
1204 unsigned int tx, unsigned int rate)
1212 if (rate > ARRAY_SIZE(rx_configs))
1215 rc = rc632_set_bit_mask(handle, RC632_REG_RX_CONTROL1,
1216 RC632_RXCTRL1_SUBCP_MASK,
1217 rx_configs[rate].subc_pulses);
1221 rc = rc632_set_bit_mask(handle, RC632_REG_DECODER_CONTROL,
1223 rx_configs[rate].rx_coding);
1227 rc = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
1228 rx_configs[rate].rx_threshold);
1232 if (rx_configs[rate].rx_coding == RC632_DECCTRL_BPSK) {
1233 rc = rc632_reg_write(handle,
1234 RC632_REG_BPSK_DEM_CONTROL,
1235 rx_configs[rate].bpsk_dem_ctrl);
1241 if (rate > ARRAY_SIZE(tx_configs))
1244 rc = rc632_set_bit_mask(handle, RC632_REG_CODER_CONTROL,
1245 RC632_CDRCTRL_RATE_MASK,
1246 tx_configs[rate].rate);
1250 rc = rc632_reg_write(handle, RC632_REG_MOD_WIDTH,
1251 tx_configs[rate].mod_width);
1260 static struct register_file iso14443b_script[] = {
1262 .reg = RC632_REG_TX_CONTROL,
1263 .val = (RC632_TXCTRL_TX1_RF_EN |
1264 RC632_TXCTRL_TX2_RF_EN |
1265 RC632_TXCTRL_TX2_INV |
1266 RC632_TXCTRL_MOD_SRC_INT),
1268 .reg = RC632_REG_CW_CONDUCTANCE,
1271 .reg = RC632_REG_MOD_CONDUCTANCE,
1274 .reg = RC632_REG_CODER_CONTROL,
1275 .val = (RC632_CDRCTRL_TXCD_NRZ |
1276 RC632_CDRCTRL_RATE_14443B),
1278 .reg = RC632_REG_MOD_WIDTH,
1281 .reg = RC632_REG_MOD_WIDTH_SOF,
1284 .reg = RC632_REG_TYPE_B_FRAMING,
1285 .val = (RC632_TBFRAMING_SOF_11L_3H |
1286 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
1287 RC632_TBFRAMING_EOF_11);
1289 .reg = RC632_REG_RX_CONTROL1,
1290 .val = (RC632_RXCTRL1_GAIN_35DB |
1291 RC632_RXCTRL1_ISO14443,
1292 RC632_RXCTRL1_SUBCP_8),
1294 .reg = RC632_REG_DECODER_CONTROL,
1295 .val = (RC632_DECCTRL_BPSK |
1296 RC632_DECCTRL_RXFR_14443B),
1298 .reg = RC632_REG_BIT_PHASE,
1299 .val = CM5121_14443B_BITPHASE,
1301 .reg = RC632_REG_RX_THRESHOLD,
1302 .val = CM5121_14443B_THRESHOLD,
1304 .reg = RC632_REG_BPSK_DEM_CONTROL,
1305 .val = ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
1306 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
1307 RC632_BPSKD_FILTER_AMP_DETECT |
1308 RC632_BPSKD_NO_RX_EOF |
1309 RC632_BPSKD_NO_RX_EGT),
1311 .reg = RC632_REG_RX_CONTROL2,
1312 .val = (RC632_RXCTRL2_AUTO_PD |
1313 RC632_RXCTRL2_DECSRC_INT),
1315 .reg = RC632_REG_RX_WAIT,
1318 .reg = RC632_REG_CHANNEL_REDUNDANCY,
1319 .val = (RC632_CR_TX_CRC_ENABLE |
1320 RC632_CR_RX_CRC_ENABLE |
1323 .reg = RC632_REG_CRC_PRESET_LSB,
1326 .reg = RC632_REG_CRC_PRESET_MSB,
1332 static int rc632_iso14443b_init(struct rfid_asic_handle *handle)
1336 /* FIXME: some FIFO work */
1338 /* flush fifo (our way) */
1339 ret = rc632_reg_write(handle, RC632_REG_CONTROL,
1340 RC632_CONTROL_FIFO_FLUSH);
1344 ret = rc632_reg_write(handle, RC632_REG_TX_CONTROL,
1345 (RC632_TXCTRL_TX1_RF_EN |
1346 RC632_TXCTRL_TX2_RF_EN |
1347 RC632_TXCTRL_TX2_INV |
1348 RC632_TXCTRL_MOD_SRC_INT));
1352 ret = rc632_reg_write(handle, RC632_REG_CW_CONDUCTANCE, 0x3f);
1356 ret = rc632_reg_write(handle, RC632_REG_MOD_CONDUCTANCE, 0x04);
1360 ret = rc632_reg_write(handle, RC632_REG_CODER_CONTROL,
1361 (RC632_CDRCTRL_TXCD_NRZ |
1362 RC632_CDRCTRL_RATE_14443B));
1366 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH, 0x13);
1370 ret = rc632_reg_write(handle, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1374 ret = rc632_reg_write(handle, RC632_REG_TYPE_B_FRAMING,
1375 (RC632_TBFRAMING_SOF_11L_3H |
1376 (6 << RC632_TBFRAMING_SPACE_SHIFT) |
1377 RC632_TBFRAMING_EOF_11));
1381 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL1,
1382 (RC632_RXCTRL1_GAIN_35DB |
1383 RC632_RXCTRL1_ISO14443 |
1384 RC632_RXCTRL1_SUBCP_8));
1388 ret = rc632_reg_write(handle, RC632_REG_DECODER_CONTROL,
1389 (RC632_DECCTRL_BPSK |
1390 RC632_DECCTRL_RXFR_14443B));
1394 ret = rc632_reg_write(handle, RC632_REG_BIT_PHASE,
1395 CM5121_14443B_BITPHASE);
1399 ret = rc632_reg_write(handle, RC632_REG_RX_THRESHOLD,
1400 CM5121_14443B_THRESHOLD);
1404 ret = rc632_reg_write(handle, RC632_REG_BPSK_DEM_CONTROL,
1405 ((0x2 & RC632_BPSKD_TAUB_MASK)<<RC632_BPSKD_TAUB_SHIFT |
1406 (0x3 & RC632_BPSKD_TAUD_MASK)<<RC632_BPSKD_TAUD_SHIFT |
1407 RC632_BPSKD_FILTER_AMP_DETECT |
1408 RC632_BPSKD_NO_RX_EOF |
1409 RC632_BPSKD_NO_RX_EGT));
1413 ret = rc632_reg_write(handle, RC632_REG_RX_CONTROL2,
1414 (RC632_RXCTRL2_AUTO_PD |
1415 RC632_RXCTRL2_DECSRC_INT));
1419 ret = rc632_reg_write(handle, RC632_REG_RX_WAIT, 0x03);
1423 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
1424 (RC632_CR_TX_CRC_ENABLE |
1425 RC632_CR_RX_CRC_ENABLE |
1430 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_LSB, 0xff);
1434 ret = rc632_reg_write(handle, RC632_REG_CRC_PRESET_MSB, 0xff);
1447 /* Register file for ISO15693 standard */
1448 static struct register_file iso15693_fast_script[] = {
1450 .reg = RC632_REG_TX_CONTROL,
1451 .val = RC632_TXCTRL_MOD_SRC_INT |
1452 RC632_TXCTRL_TX2_INV |
1453 RC632_TXCTRL_TX2_RF_EN |
1454 RC632_TXCTRL_TX1_RF_EN,
1456 .reg = RC632_REG_CW_CONDUCTANCE,
1459 .reg = RC632_REG_MOD_CONDUCTANCE,
1460 /* FIXME: nxp default for icode1/15693: 0x05 */
1462 .val = 0x21, /* omnikey */
1464 .reg = RC632_REG_CODER_CONTROL,
1465 .val = RC632_CDRCTRL_TXCD_15693_FAST |
1466 RC632_CDRCTRL_RATE_15693,
1468 .reg = RC632_REG_MOD_WIDTH,
1471 .reg = RC632_REG_MOD_WIDTH_SOF,
1474 .reg = RC632_REG_TYPE_B_FRAMING,
1477 .reg = RC632_REG_RX_CONTROL1,
1478 .val = RC632_RXCTRL1_ISO15693 |
1479 RC632_RXCTRL1_SUBCP_16 |
1480 RC632_RXCTRL1_GAIN_35DB,
1482 /* FIXME: this should always be the case */
1483 .reg = RC632_REG_RX_CONTROL2,
1484 .val = RC632_RXCTRL2_DECSRC_INT,
1486 .reg = RC632_REG_DECODER_CONTROL,
1487 .val = RC632_DECCTRL_MANCHESTER |
1488 RC632_DECCTRL_RX_INVERT |
1489 RC632_DECCTRL_ZEROAFTERCOL |
1490 RC632_DECCTRL_RXFR_15693,
1492 .reg = RC632_REG_BIT_PHASE,
1493 /* FIXME: nxp default for icode1/15693: 0x54 */
1495 .val = 0xd0, /* omnikey */
1497 .reg = RC632_REG_RX_THRESHOLD,
1498 /* FIXME: nxp default for icode1/15693: 0x68 */
1502 .reg = RC632_REG_BPSK_DEM_CONTROL,
1505 .reg = RC632_REG_CHANNEL_REDUNDANCY,
1506 .val = RC632_CR_RX_CRC_ENABLE |
1507 RC632_CR_TX_CRC_ENABLE |
1510 .reg = RC632_REG_CRC_PRESET_LSB,
1513 .reg = RC632_REG_CRC_PRESET_MSB,
1518 /* Register file for I*Code standard */
1519 static struct register_file icode1_std_script[] = {
1521 .reg = RC632_REG_TX_CONTROL,
1522 .val = RC632_TXCTRL_MOD_SRC_INT |
1523 RC632_TXCTRL_TX2_INV |
1524 RC632_TXCTRL_TX2_RF_EN |
1525 RC632_TXCTRL_TX1_RF_EN,
1527 .reg = RC632_REG_CW_CONDUCTANCE,
1530 .reg = RC632_REG_MOD_CONDUCTANCE,
1531 /* FIXME: nxp default for icode1/15693: 0x05 */
1534 .reg = RC632_REG_CODER_CONTROL,
1535 .val = RC632_CDRCTRL_TXCD_ICODE_STD |
1536 RC632_CDRCTRL_RATE_15693,
1538 .reg = RC632_REG_MOD_WIDTH,
1541 .reg = RC632_REG_MOD_WIDTH_SOF,
1544 .reg = RC632_REG_TYPE_B_FRAMING,
1547 .reg = RC632_REG_RX_CONTROL1,
1548 .val = RC632_RXCTRL1_ISO15693 |
1549 RC632_RXCTRL1_SUBCP_16 |
1550 RC632_RXCTRL1_GAIN_35DB,
1552 /* FIXME: this should always be the case */
1553 .reg = RC632_REG_RX_CONTROL2,
1554 .val = RC632_RXCTRL2_DECSRC_INT,
1556 .reg = RC632_REG_DECODER_CONTROL,
1557 .val = RC632_DECCTRL_MANCHESTER |
1558 RC632_DECCTRL_RXFR_ICODE,
1560 .reg = RC632_REG_BIT_PHASE,
1561 /* FIXME: nxp default for icode1/15693: 0x54 */
1564 .reg = RC632_REG_RX_THRESHOLD,
1565 /* FIXME: nxp default for icode1/15693: 0x68 */
1568 .reg = RC632_REG_BPSK_DEM_CONTROL,
1571 .reg = RC632_REG_CHANNEL_REDUNDANCY,
1572 /* 16bit CRC, no parity, not CRC3309 */
1573 .val = RC632_CR_RX_CRC_ENABLE |
1574 RC632_CR_TX_CRC_ENABLE,
1576 .reg = RC632_REG_CRC_PRESET_LSB,
1579 .reg = RC632_REG_CRC_PRESET_MSB,
1584 /* incremental changes on top of icode1_std_script */
1585 static struct register_file icode1_fast_patch[] = {
1587 .reg = RC632_REG_CODER_CONTROL,
1588 .val = RC632_CDRCTRL_TXCD_ICODE_FAST |
1589 RC632_CDRCTRL_RATE_ICODE_FAST,
1591 .reg = RC632_REG_MOD_WIDTH_SOF,
1592 .val = 0x73, /* 18.88uS */
1598 rc632_iso15693_init(struct rfid_asic_handle *h)
1602 /* flush fifo (our way) */
1603 ret = rc632_reg_write(h, RC632_REG_CONTROL,
1604 RC632_CONTROL_FIFO_FLUSH);
1608 ret = rc632_execute_script(h, iso15693_fast_script,
1609 ARRAY_SIZE(iso15693_fast_script));
1617 rc632_iso15693_icode1_init(struct rfid_asic_handle *h)
1621 ret = rc632_execute_script(h, icode1_std_script,
1622 ARRAY_SIZE(icode1_std_script));
1626 /* FIXME: how to configure fast/slow properly? */
1629 ret = rc632_execute_script(h, icode1_fast_patch,
1630 ARRAY_SIZE(icode1_fast_patch));
1640 rc632_iso15693_icl_init(struct rfid_asic_handle *h)
1646 ret = rc632_reg_write(h, RC632_REG_TX_CONTROL,
1647 (RC632_TXCTRL_MOD_SRC_INT |
1648 RC632_TXCTRL_TX2_INV |
1649 RC632_TXCTRL_TX2_RF_EN |
1650 RC632_TXCTRL_TX1_RF_EN));
1654 ret = rc632_reg_write(h, RC632_REG_CW_CONDUCTANCE, 0x3f);
1658 ret = rc632_reg_write(h, RC632_REG_MOD_CONDUCTANCE, 0x11);
1662 ret = rc632_reg_write(h, RC632_REG_CODER_CONTROL,
1663 (RC632_CDRCTRL_RATE_15693 |
1664 RC632_CDRCTRL_TXCD_ICODE_STD |
1669 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH, 0x3f);
1673 ret = rc632_reg_write(h, RC632_REG_MOD_WIDTH_SOF, 0x3f);
1676 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL1,
1677 (RC632_RXCTRL1_SUBCP_16|
1678 RC632_RXCTRL1_ISO15693|
1679 RC632_RXCTRL1_GAIN_35DB));
1682 ret = rc632_reg_write(h, RC632_REG_DECODER_CONTROL,
1683 (RC632_DECCTRL_RX_INVERT|
1684 RC632_DECCTRL_RXFR_15693));
1688 ret = rc632_reg_write(h, RC632_REG_BIT_PHASE, 0xbd);
1692 ret = rc632_reg_write(h, RC632_REG_RX_THRESHOLD, 0xff);
1696 ret = rc632_reg_write(h, RC632_REG_BPSK_DEM_CONTROL, 0x00);
1700 ret = rc632_reg_write(h, RC632_REG_RX_CONTROL2,
1701 RC632_RXCTRL2_DECSRC_INT);
1705 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY, 0x00);
1709 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_LSB, 0x12);
1713 ret = rc632_reg_write(h, RC632_REG_CRC_PRESET_MSB, 0xe0);
1720 static void uuid_reversecpy(unsigned char* out, unsigned char* in, int len)
1731 rc632_iso15693_transceive_ac(struct rfid_asic_handle *handle,
1732 const struct iso15693_anticol_cmd *acf,
1733 unsigned int acf_len,
1734 struct iso15693_anticol_resp *resp,
1735 unsigned int *rx_len, char *bit_of_col)
1737 u_int8_t error_flag, boc;
1740 int ret, tx_len, mask_len_bytes;
1741 unsigned int rate = ISO15693_T_SLOW;
1743 if (acf->req.flags & RFID_15693_F_RATE_HIGH)
1744 rate = ISO15693_T_FAST;
1746 DEBUGP("acf = %s\n", rfid_hexdump(acf, acf_len));
1748 ret = rc632_transceive(handle, (u_int8_t *)acf, acf_len,
1749 (u_int8_t *) resp, rx_len,
1750 iso15693_timing[rate][ISO15693_T1], 0);
1751 if (ret == -ETIMEDOUT)
1754 /* determine whether there was a collission */
1755 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1758 DEBUGP_ERROR_FLAG(error_flag);
1760 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1761 /* retrieve bit of collission */
1762 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1773 mask_len_bytes = (acf->mask_len % 8) ? acf->mask_len/8+1 : acf->mask_len/8;
1775 if (acf->current_slot == 0) {
1776 /* first call: transmit Inventory frame */
1777 DEBUGP("first_frame\n");
1779 tx_len = sizeof(struct iso15693_request) + 1 + mask_len_bytes;
1781 ret = rc632_transceive(handle, (u_int8_t *)&req, tx_len,
1782 (u_int8_t *)&rx_buf, &rx_len, ISO15693_T1, 0);
1783 acf->current_slot = 1;
1784 DEBUGP("rc632_transceive ret: %d rx_len: %d\n",ret,rx_len);
1785 /* if ((ret < 0)&&(ret != -ETIMEDOUT))
1789 /* second++ call: end timeslot with EOFpulse and read */
1790 DEBUGP("second++_frame\n");
1791 if ((acf->current_slot > 16) ||
1792 ((acf->flags & RFID_15693_F5_NSLOTS_1 == 0)
1793 && (acf->current_slot > 1))) {
1795 memset(uuid, 0, ISO15693_UID_LEN);
1799 /* reset EOF-pulse-bit to 0 */
1800 ret = rc632_clear_bits(handle, RC632_REG_CODER_CONTROL,
1801 RC632_CDRCTRL_15693_EOF_PULSE);
1803 /* generate EOF pulse */
1804 ret = rc632_set_bits(handle, RC632_REG_CODER_CONTROL,
1805 RC632_CDRCTRL_15693_EOF_PULSE);
1808 // DEBUGP("waiting for EOF pulse\n");
1809 // ret = rc632_wait_idle(handle, 10); //wait for idle
1811 rx_len = sizeof(rx_buf);
1812 ret = rc632_receive(handle, (u_int8_t*)&rx_buf, &rx_len, ISO15693_T3);
1813 DEBUGP("rc632_receive ret: %d rx_len: %d\n", ret, rx_len);
1814 acf->current_slot++;
1816 /* if ((ret < 0)&&(ret != -ETIMEDOUT))
1820 rc632_reg_read(handle, RC632_REG_PRIMARY_STATUS, &tmp);
1821 DEBUGP_STATUS_FLAG(tmp);
1823 if (ret == -ETIMEDOUT) {
1824 /* no VICC answer in this timeslot*/
1825 memset(uuid, 0, ISO15693_UID_LEN);
1828 /* determine whether there was a collission */
1829 ret = rc632_reg_read(handle, RC632_REG_ERROR_FLAG, &error_flag);
1830 DEBUGP_ERROR_FLAG(error_flag);
1834 if (error_flag & RC632_ERR_FLAG_COL_ERR) {
1835 /* retrieve bit of collission */
1836 ret = rc632_reg_read(handle, RC632_REG_COLL_POS, &boc);
1840 memcpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1841 // uuid_reversecpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1842 DEBUGP("Collision in slot %d bit %d\n",
1843 acf->current_slot,boc);
1846 /* no collision-> retrieve uuid */
1847 DEBUGP("no collision in slot %d\n", acf->current_slot);
1848 memcpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1849 //uuid_reversecpy(uuid, rx_buf.uuid, ISO15693_UID_LEN);
1857 struct mifare_authcmd {
1859 u_int8_t block_address;
1860 u_int32_t serno; /* lsb 1 2 msb */
1861 } __attribute__ ((packed));
1864 #define RFID_MIFARE_KEY_LEN 6
1865 #define RFID_MIFARE_KEY_CODED_LEN 12
1867 /* Transform crypto1 key from generic 6byte into rc632 specific 12byte */
1869 rc632_mifare_transform_key(const u_int8_t *key6, u_int8_t *key12)
1875 for (i = 0; i < RFID_MIFARE_KEY_LEN; i++) {
1876 ln = key6[i] & 0x0f;
1878 key12[i * 2 + 1] = (~ln << 4) | ln;
1879 key12[i * 2] = (~hn << 4) | hn;
1885 rc632_mifare_set_key(struct rfid_asic_handle *h, const u_int8_t *key)
1887 u_int8_t coded_key[RFID_MIFARE_KEY_CODED_LEN];
1891 ret = rc632_mifare_transform_key(key, coded_key);
1895 /* Terminate probably running command */
1896 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_IDLE);
1900 ret = rc632_fifo_write(h, RFID_MIFARE_KEY_CODED_LEN, coded_key, 0x03);
1904 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY);
1908 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
1912 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1913 ret = rc632_wait_idle_timer(h);
1917 ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, ®);
1921 if (reg & RC632_ERR_FLAG_KEY_ERR)
1928 rc632_mifare_set_key_ee(struct rfid_asic_handle *h, unsigned int addr)
1931 u_int8_t cmd_addr[2];
1934 if (addr > 0xffff - RFID_MIFARE_KEY_CODED_LEN)
1937 cmd_addr[0] = addr & 0xff; /* LSB */
1938 cmd_addr[1] = (addr >> 8) & 0xff; /* MSB */
1940 /* Terminate probably running command */
1941 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_IDLE);
1945 /* Write the key address to the FIFO */
1946 ret = rc632_fifo_write(h, 2, cmd_addr, 0x03);
1950 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY_E2);
1954 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
1958 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
1959 ret = rc632_wait_idle_timer(h);
1963 ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, ®);
1967 if (reg & RC632_ERR_FLAG_KEY_ERR)
1974 rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno,
1978 struct mifare_authcmd acmd;
1981 if (cmd != RFID_CMD_MIFARE_AUTH1A && cmd != RFID_CMD_MIFARE_AUTH1B) {
1982 DEBUGP("invalid auth command\n");
1986 /* Initialize acmd */
1987 acmd.block_address = block & 0xff;
1988 acmd.auth_cmd = cmd;
1989 //acmd.serno = htonl(serno);
1994 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
1995 RC632_CR_RX_CRC_ENABLE);
1997 /* Clear Rx CRC, Set Tx CRC and Odd Parity */
1998 ret = rc632_reg_write(h, RC632_REG_CHANNEL_REDUNDANCY,
1999 RC632_CR_TX_CRC_ENABLE | RC632_CR_PARITY_ODD |
2000 RC632_CR_PARITY_ENABLE);
2005 /* Send Authent1 Command */
2006 ret = rc632_fifo_write(h, sizeof(acmd), (unsigned char *)&acmd, 0x03);
2010 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT1);
2012 DEBUGP("error during AUTHENT1");
2016 /* Wait until transmitter is idle */
2017 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
2021 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
2022 ret = rc632_wait_idle_timer(h);
2026 ret = rc632_reg_read(h, RC632_REG_SECONDARY_STATUS, ®);
2030 DEBUGP("bitframe?");
2035 ret = rc632_clear_bits(h, RC632_REG_CHANNEL_REDUNDANCY,
2036 RC632_CR_TX_CRC_ENABLE);
2040 /* Wait until transmitter is idle */
2041 ret = rc632_timer_set(h, RC632_TMO_AUTH1);
2045 /* Send Authent2 Command */
2046 ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_AUTHENT2);
2050 /* Wait until transmitter is idle */
2051 //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
2052 ret = rc632_wait_idle_timer(h);
2056 /* Check whether authentication was successful */
2057 ret = rc632_reg_read(h, RC632_REG_CONTROL, ®);
2061 if (!(reg & RC632_CONTROL_CRYPTO1_ON)) {
2062 DEBUGP("authentication not successful");
2069 /* transceive regular frame */
2071 rc632_mifare_transceive(struct rfid_asic_handle *handle,
2072 const u_int8_t *tx_buf, unsigned int tx_len,
2073 u_int8_t *rx_buf, unsigned int *rx_len,
2074 u_int64_t timeout, unsigned int flags)
2077 u_int8_t rxl = *rx_len & 0xff;
2079 DEBUGP("entered\n");
2080 memset(rx_buf, 0, *rx_len);
2083 ret = rc632_reg_write(handle, RC632_REG_CHANNEL_REDUNDANCY,
2084 (RC632_CR_PARITY_ENABLE |
2085 RC632_CR_PARITY_ODD |
2086 RC632_CR_TX_CRC_ENABLE |
2087 RC632_CR_RX_CRC_ENABLE));
2089 ret = rc632_clear_bits(handle, RC632_REG_CHANNEL_REDUNDANCY,
2090 RC632_CR_RX_CRC_ENABLE|RC632_CR_TX_CRC_ENABLE);
2095 ret = rc632_transceive(handle, tx_buf, tx_len, rx_buf, &rxl, 0x32, 0);
2106 rc632_layer2_init(struct rfid_asic_handle *h, enum rfid_layer2_id l2)
2109 case RFID_LAYER2_ISO14443A:
2110 return rc632_iso14443a_init(h);
2111 case RFID_LAYER2_ISO14443B:
2112 return rc632_iso14443b_init(h);
2113 case RFID_LAYER2_ISO15693:
2114 return rc632_iso15693_init(h);
2115 case RFID_LAYER2_ICODE1:
2116 return rc632_iso15693_icode1_init(h);
2122 const struct rfid_asic rc632 = {
2123 .name = "Philips CL RC632",
2124 .fc = ISO14443_FREQ_CARRIER,
2127 .power = &rc632_power,
2128 .rf_power = &rc632_rf_power,
2129 .transceive = &rc632_iso14443ab_transceive,
2130 .init = &rc632_layer2_init,
2132 .transceive_sf = &rc632_iso14443a_transceive_sf,
2133 .transceive_acf = &rc632_iso14443a_transceive_acf,
2134 .set_speed = &rc632_iso14443a_set_speed,
2137 .transceive_ac = &rc632_iso15693_transceive_ac,
2140 .setkey = &rc632_mifare_set_key,
2141 .setkey_ee = &rc632_mifare_set_key_ee,
2142 .auth = &rc632_mifare_auth,