* implement rfid_reader_{get,set}opt()
[librfid] / src / rfid_reader_openpcd.c
index 15486b6..2a3aca5 100644 (file)
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  */
 
-//#define DEBUG
-
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <errno.h>
 
-#include <usb.h>
-
 #include <librfid/rfid.h>
 #include <librfid/rfid_reader.h>
 #include <librfid/rfid_asic.h>
@@ -42,6 +38,8 @@
 #include <librfid/rfid_layer2.h>
 #include <librfid/rfid_protocol.h>
 
+#include "rfid_reader_rc632_common.h"
+
 /* FIXME */
 #include "rc632.h"
 
@@ -54,9 +52,22 @@ static char rcv_buf[RECVBUF_LEN];
 static struct openpcd_hdr *snd_hdr;
 static struct openpcd_hdr *rcv_hdr;
 
-
 #ifndef LIBRFID_FIRMWARE
 
+#ifdef  __MINGW32__
+#include "libusb_dyn.h"
+#else /*__MINGW32__*/
+#include <usb.h>
+#endif/*__MINGW32__*/
+
+#ifdef DEBUG_REGISTER
+#define DEBUGRC DEBUGPC
+#define DEBUGR DEBUGP
+#else
+#define DEBUGRC(x, args ...)   do {} while(0)
+#define DEBUGR(x, args ...)    do {} while(0)
+#endif
+
 static struct usb_device *dev;
 static struct usb_dev_handle *hdl;
 
@@ -75,7 +86,7 @@ static int openpcd_send_command(u_int8_t cmd, u_int8_t reg, u_int8_t val,
 
        cur = sizeof(*snd_hdr) + len;
 
-       return usb_bulk_write(hdl, OPENPCD_OUT_EP, (char *)snd_hdr, cur, 0);
+       return usb_bulk_write(hdl, OPENPCD_OUT_EP, (char *)snd_hdr, cur, 1000);
 }
 
 static int openpcd_recv_reply(void)
@@ -95,7 +106,7 @@ static int openpcd_xcv(u_int8_t cmd, u_int8_t reg, u_int8_t val,
        ret = openpcd_send_command(cmd, reg, val, len, data);
        if (ret < 0)
                return ret;
-       if (ret < sizeof(sizeof(struct openpcd_hdr)))
+       if (ret < sizeof(struct openpcd_hdr))
                return -EINVAL;
 
        return openpcd_recv_reply();
@@ -115,7 +126,7 @@ static struct usb_device *find_opcd_device(void)
 {
        struct usb_bus *bus;
 
-       for (bus = usb_busses; bus; bus = bus->next) {
+       for (bus = usb_get_busses(); bus; bus = bus->next) {
                struct usb_device *dev;
                for (dev = bus->devices; dev; dev = dev->next) {
                        int i;
@@ -137,13 +148,13 @@ static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
 {
        int ret;
 
-       DEBUGP("reg=0x%02x, val=%02x: ", reg, value);
+       DEBUGR("reg=0x%02x, val=%02x: ", reg, value);
 
        ret = openpcd_xcv(OPENPCD_CMD_WRITE_REG, reg, value, 0, NULL);
        if (ret < 0)
-               DEBUGPC("ERROR sending command\n");
+               DEBUGRC("ERROR sending command\n");
        else
-               DEBUGPC("OK\n");
+               DEBUGRC("OK\n");
 
        return ret;
 }
@@ -154,21 +165,21 @@ static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
 {
        int ret;        
 
-       DEBUGP("reg=0x%02x, ", reg);
+       DEBUGR("reg=0x%02x, ", reg);
 
        ret = openpcd_xcv(OPENPCD_CMD_READ_REG, reg, 0, 0, NULL);
        if (ret < 0) {
-               DEBUGPC("ERROR sending command\n");
+               DEBUGRC("ERROR sending command\n");
                return ret;
        }
 
        if (ret < sizeof(struct openpcd_hdr)) {
-               DEBUGPC("ERROR: short packet\n");
+               DEBUGRC("ERROR: short packet\n");
                return ret;
        }
 
        *value = rcv_hdr->val;
-       DEBUGPC("val=%02x: OK\n", *value);
+       DEBUGRC("val=%02x: OK\n", *value);
 
        return ret;
 }
@@ -179,17 +190,17 @@ static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
 {
        int ret;
 
-       DEBUGP(" ");
+       DEBUGR(" ");
 
        ret = openpcd_xcv(OPENPCD_CMD_READ_FIFO, 0x00, num_bytes, 0, NULL);
        if (ret < 0) {
-               DEBUGPC("ERROR sending command\n");
+               DEBUGRC("ERROR sending command\n");
                return ret;
        }
-       DEBUGPC("ret = %d\n", ret);
+       DEBUGRC("ret = %d\n", ret);
 
        memcpy(buf, rcv_hdr->data, ret - sizeof(struct openpcd_hdr));
-       DEBUGPC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
+       DEBUGRC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
                rfid_hexdump(rcv_hdr->data, ret - sizeof(struct openpcd_hdr)));
 
        return ret;
@@ -202,7 +213,7 @@ static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
 {
        int ret;
 
-       DEBUGP("len=%u, data=%s\n", len, rfid_hexdump(bytes, len));
+       DEBUGR("len=%u, data=%s\n", len, rfid_hexdump(bytes, len));
        ret = openpcd_xcv(OPENPCD_CMD_WRITE_FIFO, 0, 0, len, bytes);
 
        return ret;
@@ -220,18 +231,95 @@ const struct rfid_asic_transport openpcd_rat = {
        },
 };
 
+static int openpcd_get_api_version(struct rfid_reader_handle *rh, u_int8_t *version)
+{
+       int ret;
+       
+       // preset version result to zero
+       rcv_hdr->val=0;
+    
+       ret = openpcd_xcv(OPENPCD_CMD_GET_API_VERSION, 0, 0, 0, NULL);
+       if (ret < 0) {
+               DEBUGPC("ERROR sending command [%i]\n", ret);
+               return ret;
+       }
+
+       if (ret < sizeof(struct openpcd_hdr)) {
+               DEBUGPC("ERROR: short packet [%i]\n", ret);
+               return -EINVAL;
+       }
+
+       *version = rcv_hdr->val;
+       
+       return ret;
+}
+
+static int openpcd_get_environment(struct rfid_reader_handle *rh,
+                                  unsigned char num_bytes,
+                                  unsigned char *buf)
+{
+       int ret;
+
+       DEBUGP(" ");
+
+       ret = openpcd_xcv(OPENPCD_CMD_GET_ENVIRONMENT, 0x00, num_bytes, 0, NULL);
+       if (ret < 0) {
+               DEBUGPC("ERROR sending command [%i]\n",ret);
+               return ret;
+       }
+       DEBUGPC("ret = %d\n", ret);
+
+       memcpy(buf, rcv_hdr->data, ret - sizeof(struct openpcd_hdr));
+       DEBUGPC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
+               rfid_hexdump(rcv_hdr->data, ret - sizeof(struct openpcd_hdr)));
+
+       return ret;
+}
+
+static int openpcd_set_environment(struct rfid_reader_handle *rh, 
+                                  unsigned char num_bytes,
+                                  const unsigned char *buf)
+{
+       int ret;
+       
+       ret = openpcd_xcv(OPENPCD_CMD_SET_ENVIRONMENT, 0, 0, num_bytes, buf);
+       if (ret < 0) {
+               DEBUGPC("ERROR sending command [%i]\n",ret);
+               return ret;
+       }
+
+       if (ret < sizeof(struct openpcd_hdr)) {
+               DEBUGPC("ERROR: short packet [%i]\n", ret);
+               return -EINVAL;
+       }
+
+       return rcv_hdr->val;
+}
+
+static int openpcd_reset(struct rfid_reader_handle *rh)
+{
+       int ret;
+
+       DEBUGP("reset ");
+       ret = openpcd_xcv(OPENPCD_CMD_RESET, 0, 0, 0, 0);
+
+       return ret;
+}
+
 #else
 /* RC632 access primitives for librfid inside reader firmware */
 
 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
                             unsigned char reg, unsigned char value)
 {
+       return opcd_rc632_reg_write(rath, reg, value);
 }
 
 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
                            unsigned char reg,
                            unsigned char *value)
 {
+       return opcd_rc632_reg_read(rath, reg, value);
 }
 
 
@@ -239,6 +327,7 @@ static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
                             unsigned char num_bytes,
                             unsigned char *buf)
 {
+       return opcd_rc632_fifo_read(rath, num_bytes, buf);
 }
 
 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
@@ -246,6 +335,7 @@ static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
                             const unsigned char *bytes,
                             unsigned char flags)
 {
+       return opcd_rc632_fifo_write(rath, len, bytes, flags);
 }
 
 const struct rfid_asic_transport openpcd_rat = {
@@ -262,99 +352,24 @@ const struct rfid_asic_transport openpcd_rat = {
 
 #endif /* LIBRFID_FIRMWARE */
 
-static int openpcd_transceive(struct rfid_reader_handle *rh,
-                            enum rfid_frametype frametype,
-                            const unsigned char *tx_data, unsigned int tx_len,
-                            unsigned char *rx_data, unsigned int *rx_len,
-                            u_int64_t timeout, unsigned int flags)
+static int openpcd_getopt(struct rfid_reader_handle *rh, int optname,
+                         void *optval, unsigned int *optlen)
 {
-       return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
-                                                     tx_data, tx_len, 
-                                                     rx_data, rx_len,
-                                                     timeout, flags);
-}
+       int rc;
+       u_int8_t *val_u8 = (u_int8_t *) optval;
 
-static int openpcd_transceive_sf(struct rfid_reader_handle *rh,
-                              unsigned char cmd, struct iso14443a_atqa *atqa)
-{
-       return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
-                                                                  cmd,
-                                                                  atqa);
-}
-
-static int
-openpcd_transceive_acf(struct rfid_reader_handle *rh,
-                     struct iso14443a_anticol_cmd *cmd,
-                     unsigned int *bit_of_col)
-{
-       return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
-                                                        cmd, bit_of_col);
-}
-
-static int
-openpcd_14443a_init(struct rfid_reader_handle *rh)
-{
-       return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
-}
-
-static int
-openpcd_14443a_set_speed(struct rfid_reader_handle *rh, 
-                       unsigned int tx,
-                       unsigned int speed)
-{
-       u_int8_t rate;
-       
-       DEBUGP("setting rate: ");
-       switch (speed) {
-       case RFID_14443A_SPEED_106K:
-               rate = 0x00;
-               DEBUGPC("106K\n");
-               break;
-       case RFID_14443A_SPEED_212K:
-               rate = 0x01;
-               DEBUGPC("212K\n");
-               break;
-       case RFID_14443A_SPEED_424K:
-               rate = 0x02;
-               DEBUGPC("424K\n");
-               break;
-       case RFID_14443A_SPEED_848K:
-               rate = 0x03;
-               DEBUGPC("848K\n");
-               break;
+       switch (optname) {
+#ifndef LIBRFID_FIRMWARE
+       case RFID_OPT_RDR_FW_VERSION:
+               return openpcd_get_api_version(rh, val_u8);
+#endif
        default:
-               return -EINVAL;
-               break;
+               return _rdr_rc632_getopt(rh, optname, optval, optlen);
        }
-       return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
-                                                               tx, rate);
-}
-
-static int
-openpcd_14443b_init(struct rfid_reader_handle *rh)
-{
-       return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
-}
 
-static int
-openpcd_15693_init(struct rfid_reader_handle *rh)
-{
-       return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
+       return 0;
 }
 
-static int
-openpcd_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);
-}
-
-static int
-openpcd_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
-                  u_int32_t serno, u_int8_t block)
-{
-       return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah, 
-                                                       cmd, serno, block);
-}
 
 static struct rfid_reader_handle *
 openpcd_open(void *data)
@@ -365,6 +380,7 @@ openpcd_open(void *data)
        snd_hdr = (struct openpcd_hdr *)snd_buf;
        rcv_hdr = (struct openpcd_hdr *)rcv_buf;
 
+#ifndef LIBRFID_FIRMWARE
        usb_init();
        if (usb_find_busses() < 0)
                return NULL;
@@ -383,11 +399,19 @@ openpcd_open(void *data)
                return NULL;
        }
 
+        if(usb_set_configuration(hdl, 1 ) < 0)
+        {
+            DEBUGP("setting config failed\n");
+            usb_close( hdl );
+            return NULL;
+        }
+                                                                       
        if (usb_claim_interface(hdl, 0) < 0) {
                DEBUGP("Can't claim interface\n");
                usb_close(hdl);
                return NULL;
        }
+#endif
 
        rh = malloc_reader_handle(sizeof(*rh));
        if (!rh)
@@ -426,7 +450,9 @@ openpcd_close(struct rfid_reader_handle *rh)
        free_rat_handle(rath);
        free_reader_handle(rh);
 
+#ifndef LIBRFID_FIRMWARE
        usb_close(hdl);
+#endif
 }
 
 const struct rfid_reader rfid_reader_openpcd = {
@@ -434,7 +460,13 @@ const struct rfid_reader rfid_reader_openpcd = {
        .id = RFID_READER_OPENPCD,
        .open = &openpcd_open,
        .close = &openpcd_close,
-       .transceive = &openpcd_transceive,
+       .getopt = &openpcd_getopt,
+#ifndef LIBRFID_FIRMWARE
+        .reset = &openpcd_reset,
+#endif
+       .setopt = &_rdr_rc632_setopt,
+       .init = &_rdr_rc632_l2_init,
+       .transceive = &_rdr_rc632_transceive,
        .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
                        (1 << RFID_LAYER2_ISO14443B) |
                        (1 << RFID_LAYER2_ISO15693),
@@ -442,18 +474,17 @@ const struct rfid_reader rfid_reader_openpcd = {
                        (1 << RFID_PROTOCOL_MIFARE_UL) |
                        (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
        .iso14443a = {
-               .init = &openpcd_14443a_init,
-               .transceive_sf = &openpcd_transceive_sf,
-               .transceive_acf = &openpcd_transceive_acf,
+               .transceive_sf = &_rdr_rc632_transceive_sf,
+               .transceive_acf = &_rdr_rc632_transceive_acf,
                .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
                         RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
-               .set_speed = &openpcd_14443a_set_speed,
+               .set_speed = &_rdr_rc632_14443a_set_speed,
        },
-       .iso14443b = {
-               .init = &openpcd_14443b_init,
+       .iso15693 = {
+               .transceive_ac = &_rdr_rc632_iso15693_transceive_ac,
        },
        .mifare_classic = {
-               .setkey = &openpcd_mifare_setkey,
-               .auth = &openpcd_mifare_auth,
+               .setkey = &_rdr_rc632_mifare_setkey,
+               .auth = &_rdr_rc632_mifare_auth,
        },
 };