add function to set mifare key from internal eeprom
authorlaforge <laforge@e0336214-984f-0b4b-a45f-81c69e1f0ede>
Sun, 24 Feb 2008 12:35:52 +0000 (12:35 +0000)
committerlaforge <laforge@e0336214-984f-0b4b-a45f-81c69e1f0ede>
Sun, 24 Feb 2008 12:35:52 +0000 (12:35 +0000)
git-svn-id: https://svn.gnumonks.org/trunk/librfid@2086 e0336214-984f-0b4b-a45f-81c69e1f0ede

include/librfid/rfid_asic_rc632.h
include/librfid/rfid_reader.h
src/rfid_asic_rc632.c
src/rfid_proto_mifare_classic.c
src/rfid_reader_cm5121.c
src/rfid_reader_openpcd.c
src/rfid_reader_rc632_common.c
src/rfid_reader_rc632_common.h
src/rfid_reader_spidev.c

index c78a720..d8adac7 100644 (file)
@@ -65,6 +65,8 @@ struct rfid_asic_rc632 {
                struct {
                        int (*setkey)(struct rfid_asic_handle *h,
                                      const unsigned char *key);
+                       int (*setkey_ee)(struct rfid_asic_handle *h,
+                                     const unsigned int addr);
                        int (*auth)(struct rfid_asic_handle *h, u_int8_t cmd, 
                                    u_int32_t serno, u_int8_t block);
                } mifare_classic;
index 1c53677..27bcef3 100644 (file)
@@ -70,6 +70,7 @@ struct rfid_reader {
        } iso15693;
        struct rfid_mifare_classic_reader {
                int (*setkey)(struct rfid_reader_handle *h, const unsigned char *key);
+               int (*setkey_ee)(struct rfid_reader_handle *h, const unsigned int addr);
                int (*auth)(struct rfid_reader_handle *h, u_int8_t cmd,
                            u_int32_t serno, u_int8_t block);
        } mifare_classic;
index 135c7d9..effe813 100644 (file)
@@ -1924,6 +1924,52 @@ rc632_mifare_set_key(struct rfid_asic_handle *h, const u_int8_t *key)
        return 0;
 }
 
+static int
+rc632_mifare_set_key_ee(struct rfid_asic_handle *h, unsigned int addr)
+{
+       int ret;
+       u_int8_t cmd_addr[2];
+       u_int8_t reg;
+
+       if (addr > 0xffff - RFID_MIFARE_KEY_CODED_LEN)
+               return -EINVAL;
+
+       cmd_addr[0] = addr & 0xff;              /* LSB */
+       cmd_addr[1] = (addr >> 8) & 0xff;       /* MSB */
+
+       /* Terminate probably running command */
+       ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_IDLE);    
+       if (ret < 0)
+               return ret;
+
+       /* Write the key address to the FIFO */
+       ret = rc632_fifo_write(h, 2, cmd_addr, 0x03);
+       if (ret < 0)
+               return ret;
+
+       ret = rc632_reg_write(h, RC632_REG_COMMAND, RC632_CMD_LOAD_KEY_E2);
+       if (ret < 0)
+               return ret;
+
+       ret = rc632_timer_set(h, RC632_TMO_AUTH1);
+       if (ret < 0)
+               return ret;
+
+       //ret = rc632_wait_idle(h, RC632_TMO_AUTH1);
+       ret = rc632_wait_idle_timer(h);
+       if (ret < 0)
+               return ret;
+
+       ret = rc632_reg_read(h, RC632_REG_ERROR_FLAG, &reg);
+       if (ret < 0)
+               return ret;
+
+       if (reg & RC632_ERR_FLAG_KEY_ERR)
+               return -EINVAL;
+
+       return 0;
+}
+
 static int
 rc632_mifare_auth(struct rfid_asic_handle *h, u_int8_t cmd, u_int32_t serno,
                  u_int8_t block)
@@ -2092,6 +2138,7 @@ const struct rfid_asic rc632 = {
                        },
                        .mifare_classic = {
                                .setkey = &rc632_mifare_set_key,
+                               .setkey_ee = &rc632_mifare_set_key_ee,
                                .auth = &rc632_mifare_auth,
                        },
                },
index 66e4287..a6e2e4f 100644 (file)
@@ -187,6 +187,14 @@ int mfcl_set_key(struct rfid_protocol_handle *ph, unsigned char *key)
        return ph->l2h->rh->reader->mifare_classic.setkey(ph->l2h->rh, key);
 }
 
+int mfcl_set_key_ee(struct rfid_protocol_handle *ph, unsigned int addr)
+{
+       if (!ph->l2h->rh->reader->mifare_classic.setkey_ee)
+               return -ENODEV;
+
+       return ph->l2h->rh->reader->mifare_classic.setkey_ee(ph->l2h->rh, addr);
+}
+
 int mfcl_auth(struct rfid_protocol_handle *ph, u_int8_t cmd, u_int8_t block)
 {
        u_int32_t serno = *((u_int32_t *)ph->l2h->uid);
index 2d1ce24..12bc358 100644 (file)
@@ -275,6 +275,7 @@ const struct rfid_reader rfid_reader_cm5121 = {
        },
        .mifare_classic = {
                .setkey = &_rdr_rc632_mifare_setkey,
+               .setkey_ee = &_rdr_rc632_mifare_setkey_ee,
                .auth = &_rdr_rc632_mifare_auth,
        },
 };
index 2a3aca5..d81a7dd 100644 (file)
@@ -485,6 +485,7 @@ const struct rfid_reader rfid_reader_openpcd = {
        },
        .mifare_classic = {
                .setkey = &_rdr_rc632_mifare_setkey,
+               .setkey_ee = &_rdr_rc632_mifare_setkey_ee,
                .auth = &_rdr_rc632_mifare_auth,
        },
 };
index 4a41f15..101c1f8 100644 (file)
@@ -116,6 +116,12 @@ _rdr_rc632_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
        return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
 }
 
+int
+_rdr_rc632_mifare_setkey_ee(struct rfid_reader_handle *rh, unsigned int addr)
+{
+       return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey_ee(rh->ah, addr);
+}
+
 int
 _rdr_rc632_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
                   u_int32_t serno, u_int8_t block)
index 38d195d..999dfa3 100644 (file)
@@ -20,6 +20,7 @@ int _rdr_rc632_14443a_set_speed(struct rfid_reader_handle *rh, unsigned int tx,
                                unsigned int speed);
 int _rdr_rc632_l2_init(struct rfid_reader_handle *rh, enum rfid_layer2_id l2);
 int _rdr_rc632_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key);
+int _rdr_rc632_mifare_setkey_ee(struct rfid_reader_handle *rh, const unsigned int addr);
 int _rdr_rc632_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
                           u_int32_t serno, u_int8_t block);
 int _rdr_rc632_getopt(struct rfid_reader_handle *rh, int optname,
index 13d1bcb..0eee241 100644 (file)
@@ -294,6 +294,7 @@ struct rfid_reader rfid_reader_spidev = {
        },
        .mifare_classic = {
                .setkey = &_rdr_rc632_mifare_setkey,
+               .setkey_ee = &_rdr_rc632_mifare_setkey_ee,
                .auth = &_rdr_rc632_mifare_auth,
        },
 };