From 49d6067e887e91038e83ade0233eba7e0d99ecdb Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Tue, 12 Jul 2011 12:10:48 -0300 Subject: [PATCH] tools/lib/: added HardMAC functions to the atusb drivers --- tools/lib/atusb-common.c | 79 ++++++++++++++++++++++++++++++++++++++++ tools/lib/atusb-common.h | 3 ++ tools/lib/atusb-spi.c | 3 ++ tools/lib/atusb.c | 3 ++ 4 files changed, 88 insertions(+) diff --git a/tools/lib/atusb-common.c b/tools/lib/atusb-common.c index 0899730..9e24b45 100644 --- a/tools/lib/atusb-common.c +++ b/tools/lib/atusb-common.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -276,6 +277,84 @@ int atusb_set_clkm(void *handle, int mhz) } +/* ----- HardMAC ----------------------------------------------------------- */ + + +void atusb_rx_mode(void *handle, int on) +{ + struct atusb_dsc *dsc = handle; + int res; + + if (dsc->error) + return; + + res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_RX_MODE, + on, 0, NULL, 0, 1000); + if (res < 0) { + fprintf(stderr, "ATUSB_RX_MODE: %d\n", res); + dsc->error = 1; + } +} + + +int atusb_rx(void *handle, void *buf, int size, uint8_t *lqi) +{ + struct atusb_dsc *dsc = handle; + uint8_t len; + int res; + uint8_t tmp[MAX_PSDU+2]; /* PHR, LQI */ + + /* + * Seems that either the USB stack of libusb doesn't like it if we do a + * read of size one followed by the full read. Therefore, we just do + * a maximum-sized read and hope that we don't split packets. + */ + res = usb_bulk_read(dsc->dev, 1, (char *) tmp, sizeof(tmp), 0); + if (res < 0) { + fprintf(stderr, "usb_bulk_read: %d\n", res); + dsc->error = 1; + return 0; + } + + len = tmp[0]; + if (len & 0x80) { + fprintf(stderr, "atusb_rx: invalid length 0x%02x\n", len); + return 0; + } + if (len > size) { + fprintf(stderr, "atusb_rx: len %u > size %d\n", len, size); + return 0; + } + if (len > res+2) { + fprintf(stderr, "atusb_rx: len %u > res %d+2\n", len, res); + return 0; + } + + memcpy(buf, tmp+1, len); + if (lqi) + *lqi = tmp[len+1]; + + return len; +} + + +void atusb_tx(void *handle, const void *buf, int size) +{ + struct atusb_dsc *dsc = handle; + int res; + + if (dsc->error) + return; + + res = usb_control_msg(dsc->dev, TO_DEV, ATUSB_TX, + 0, 0, (void *) buf, size, 1000); + if (res < 0) { + fprintf(stderr, "ATUSB_TX: %d\n", res); + dsc->error = 1; + } +} + + /* ----- Driver-specific hacks --------------------------------------------- */ diff --git a/tools/lib/atusb-common.h b/tools/lib/atusb-common.h index 6569665..052ae59 100644 --- a/tools/lib/atusb-common.h +++ b/tools/lib/atusb-common.h @@ -34,6 +34,9 @@ void atusb_reset_rf(void *handle); void atusb_test_mode(void *handle); void atusb_slp_tr(void *handle, int on, int pulse); int atusb_interrupt_wait(void *handle, int timeout_ms); +void atusb_rx_mode(void *handle, int on); +int atusb_rx(void *handle, void *buf, int size, uint8_t *lqi); +void atusb_tx(void *handle, const void *buf, int size); int atusb_set_clkm(void *handle, int mhz); diff --git a/tools/lib/atusb-spi.c b/tools/lib/atusb-spi.c index e913957..d770dc5 100644 --- a/tools/lib/atusb-spi.c +++ b/tools/lib/atusb-spi.c @@ -168,4 +168,7 @@ struct atrf_driver atusb_spi_driver = { .sram_write = atusb_spi_sram_write, .sram_read = atusb_spi_sram_read, .interrupt_wait = atusb_interrupt_wait, + .rx_mode = atusb_rx_mode, + .rx = atusb_rx, + .tx = atusb_tx, }; diff --git a/tools/lib/atusb.c b/tools/lib/atusb.c index e035718..538f101 100644 --- a/tools/lib/atusb.c +++ b/tools/lib/atusb.c @@ -162,4 +162,7 @@ struct atrf_driver atusb_driver = { .sram_write = atusb_sram_write, .sram_read = atusb_sram_read, .interrupt_wait = atusb_interrupt_wait, + .rx_mode = atusb_rx_mode, + .rx = atusb_rx, + .tx = atusb_tx, };