2009-11-16 23:52:10 +00:00
|
|
|
/* MSPDebug - debugging tool for the eZ430
|
2012-09-10 02:22:00 +00:00
|
|
|
* Copyright (C) 2009-2012 Daniel Beer
|
2009-11-16 23:52:10 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2011-07-25 14:46:26 +00:00
|
|
|
#include <stdlib.h>
|
2009-11-16 23:52:10 +00:00
|
|
|
#include <string.h>
|
2018-11-18 19:49:08 +00:00
|
|
|
#if !defined(__Windows__) || defined(__MINGW32__)
|
2009-11-16 23:52:10 +00:00
|
|
|
#include <usb.h>
|
2016-12-20 20:57:23 +00:00
|
|
|
#else
|
|
|
|
#include <lusb0_usb.h>
|
|
|
|
#endif
|
2009-11-16 23:52:10 +00:00
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
#include "rf2500.h"
|
2010-01-08 05:03:51 +00:00
|
|
|
#include "util.h"
|
2010-06-28 03:13:51 +00:00
|
|
|
#include "usbutil.h"
|
2010-08-16 23:07:03 +00:00
|
|
|
#include "output.h"
|
2009-11-17 02:11:46 +00:00
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
struct rf2500_transport {
|
|
|
|
struct transport base;
|
|
|
|
|
|
|
|
int int_number;
|
|
|
|
struct usb_dev_handle *handle;
|
|
|
|
|
2012-04-24 23:02:22 +00:00
|
|
|
uint8_t buf[64];
|
2010-04-30 04:57:57 +00:00
|
|
|
int len;
|
|
|
|
int offset;
|
|
|
|
};
|
|
|
|
|
2009-11-16 23:52:10 +00:00
|
|
|
/*********************************************************************
|
|
|
|
* USB transport
|
|
|
|
*
|
|
|
|
* These functions handle the details of slicing data over USB
|
|
|
|
* transfers. The interface presented is a continuous byte stream with
|
|
|
|
* no slicing codes.
|
|
|
|
*
|
|
|
|
* Writes are unbuffered -- a single write translates to at least
|
|
|
|
* one transfer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define USB_FET_VENDOR 0x0451
|
|
|
|
#define USB_FET_PRODUCT 0xf432
|
|
|
|
#define USB_FET_INTERFACE_CLASS 3
|
|
|
|
|
|
|
|
#define USB_FET_IN_EP 0x81
|
|
|
|
#define USB_FET_OUT_EP 0x01
|
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
static int open_interface(struct rf2500_transport *tr,
|
|
|
|
struct usb_device *dev, int ino)
|
2009-11-16 23:52:10 +00:00
|
|
|
{
|
2013-10-25 20:59:48 +00:00
|
|
|
printc_dbg("Trying to open interface %d on %s\n", ino, dev->filename);
|
2009-11-16 23:52:10 +00:00
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
tr->int_number = ino;
|
2009-11-16 23:52:10 +00:00
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
tr->handle = usb_open(dev);
|
|
|
|
if (!tr->handle) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("rf2500: can't open device");
|
2009-11-16 23:52:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-12-05 23:25:45 +00:00
|
|
|
#if defined(__linux__)
|
2010-04-30 04:57:57 +00:00
|
|
|
if (usb_detach_kernel_driver_np(tr->handle, tr->int_number) < 0)
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("rf2500: warning: can't "
|
2009-11-16 23:52:10 +00:00
|
|
|
"detach kernel driver");
|
2010-05-11 22:13:39 +00:00
|
|
|
#endif
|
2009-11-16 23:52:10 +00:00
|
|
|
|
2012-04-24 23:02:22 +00:00
|
|
|
#ifdef __Windows__
|
2011-07-21 21:20:29 +00:00
|
|
|
if (usb_set_configuration(tr->handle, 1) < 0) {
|
|
|
|
pr_error("rf2500: can't set configuration 1");
|
|
|
|
usb_close(tr->handle);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-07-30 00:51:44 +00:00
|
|
|
#endif
|
2011-07-21 21:20:29 +00:00
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
if (usb_claim_interface(tr->handle, tr->int_number) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("rf2500: can't claim interface");
|
2010-04-30 04:57:57 +00:00
|
|
|
usb_close(tr->handle);
|
2009-11-16 23:52:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
static int open_device(struct rf2500_transport *tr,
|
|
|
|
struct usb_device *dev)
|
2009-11-16 23:52:10 +00:00
|
|
|
{
|
|
|
|
struct usb_config_descriptor *c = &dev->config[0];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < c->bNumInterfaces; i++) {
|
|
|
|
struct usb_interface *intf = &c->interface[i];
|
|
|
|
struct usb_interface_descriptor *desc = &intf->altsetting[0];
|
|
|
|
|
|
|
|
if (desc->bInterfaceClass == USB_FET_INTERFACE_CLASS &&
|
2010-04-30 04:57:57 +00:00
|
|
|
!open_interface(tr, dev, desc->bInterfaceNumber))
|
2009-11-16 23:52:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-05-13 00:57:21 +00:00
|
|
|
static int usbtr_send(transport_t tr_base, const uint8_t *data, int len)
|
2009-11-16 23:52:10 +00:00
|
|
|
{
|
2010-04-30 04:57:57 +00:00
|
|
|
struct rf2500_transport *tr = (struct rf2500_transport *)tr_base;
|
|
|
|
|
2009-11-16 23:52:10 +00:00
|
|
|
while (len) {
|
2010-05-13 00:57:21 +00:00
|
|
|
uint8_t pbuf[256];
|
2009-11-16 23:52:10 +00:00
|
|
|
int plen = len > 255 ? 255 : len;
|
|
|
|
int txlen = plen + 1;
|
|
|
|
|
|
|
|
memcpy(pbuf + 1, data, plen);
|
|
|
|
|
|
|
|
/* This padding is needed to work around an apparent bug in
|
|
|
|
* the RF2500 FET. Without this, the device hangs.
|
|
|
|
*/
|
|
|
|
if (txlen > 32 && (txlen & 0x3f))
|
|
|
|
while (txlen < 255 && (txlen & 0x3f))
|
|
|
|
pbuf[txlen++] = 0xff;
|
|
|
|
else if (txlen > 16 && (txlen & 0xf))
|
|
|
|
while (txlen < 255 && (txlen & 0xf) != 1)
|
|
|
|
pbuf[txlen++] = 0xff;
|
|
|
|
pbuf[0] = txlen - 1;
|
|
|
|
|
|
|
|
#ifdef DEBUG_USBTR
|
2010-04-21 02:12:12 +00:00
|
|
|
debug_hexdump("USB transfer out", pbuf, txlen);
|
2009-11-16 23:52:10 +00:00
|
|
|
#endif
|
2010-04-30 04:57:57 +00:00
|
|
|
if (usb_bulk_write(tr->handle, USB_FET_OUT_EP,
|
2010-05-13 04:45:38 +00:00
|
|
|
(char *)pbuf, txlen, 10000) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("rf2500: can't send data");
|
2009-11-16 23:52:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
data += plen;
|
|
|
|
len -= plen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-13 00:57:21 +00:00
|
|
|
static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len)
|
2009-11-16 23:52:10 +00:00
|
|
|
{
|
2010-04-30 04:57:57 +00:00
|
|
|
struct rf2500_transport *tr = (struct rf2500_transport *)tr_base;
|
2009-11-16 23:52:10 +00:00
|
|
|
int rlen;
|
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
if (tr->offset >= tr->len) {
|
|
|
|
if (usb_bulk_read(tr->handle, USB_FET_IN_EP,
|
|
|
|
(char *)tr->buf, sizeof(tr->buf),
|
2010-01-05 04:21:45 +00:00
|
|
|
10000) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("rf2500: can't receive data");
|
2009-11-16 23:52:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_USBTR
|
2010-04-30 04:57:57 +00:00
|
|
|
debug_hexdump("USB transfer in", tr->buf, 64);
|
2009-11-16 23:52:10 +00:00
|
|
|
#endif
|
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
tr->len = tr->buf[1] + 2;
|
|
|
|
if (tr->len > sizeof(tr->buf))
|
|
|
|
tr->len = sizeof(tr->buf);
|
|
|
|
tr->offset = 2;
|
2009-11-16 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
rlen = tr->len - tr->offset;
|
2009-11-16 23:52:10 +00:00
|
|
|
if (rlen > max_len)
|
|
|
|
rlen = max_len;
|
2010-04-30 04:57:57 +00:00
|
|
|
memcpy(databuf, tr->buf + tr->offset, rlen);
|
|
|
|
tr->offset += rlen;
|
2009-11-16 23:52:10 +00:00
|
|
|
|
|
|
|
return rlen;
|
|
|
|
}
|
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
static void usbtr_destroy(transport_t tr_base)
|
2009-11-16 23:52:10 +00:00
|
|
|
{
|
2010-04-30 04:57:57 +00:00
|
|
|
struct rf2500_transport *tr = (struct rf2500_transport *)tr_base;
|
2009-11-16 23:52:10 +00:00
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
usb_release_interface(tr->handle, tr->int_number);
|
|
|
|
usb_close(tr->handle);
|
|
|
|
free(tr);
|
|
|
|
}
|
2009-11-16 23:52:10 +00:00
|
|
|
|
2012-09-10 02:22:00 +00:00
|
|
|
static int usbtr_flush(transport_t tr_base)
|
|
|
|
{
|
|
|
|
struct rf2500_transport *tr = (struct rf2500_transport *)tr_base;
|
2012-11-28 18:55:36 +00:00
|
|
|
|
2014-07-04 22:35:07 +00:00
|
|
|
#if !defined(__APPLE__) && !defined(__sun__)
|
2012-09-10 02:22:00 +00:00
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
/* Flush out lingering data.
|
|
|
|
*
|
|
|
|
* The timeout apparently doesn't work on OS/X, and this loop
|
|
|
|
* just hangs once the endpoint buffer empties.
|
|
|
|
*/
|
|
|
|
while (usb_bulk_read(tr->handle, USB_FET_IN_EP,
|
|
|
|
buf, sizeof(buf),
|
|
|
|
100) > 0);
|
|
|
|
#endif
|
|
|
|
|
2012-11-28 18:55:36 +00:00
|
|
|
tr->len = 0;
|
|
|
|
tr->offset = 0;
|
2012-09-10 02:22:00 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usbtr_set_modem(transport_t tr_base, transport_modem_t state)
|
|
|
|
{
|
|
|
|
printc_err("rf2500: unsupported operation: set_modem\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct transport_class rf2500_transport = {
|
|
|
|
.destroy = usbtr_destroy,
|
|
|
|
.send = usbtr_send,
|
|
|
|
.recv = usbtr_recv,
|
|
|
|
.flush = usbtr_flush,
|
|
|
|
.set_modem = usbtr_set_modem
|
|
|
|
};
|
|
|
|
|
2021-10-05 17:34:48 +00:00
|
|
|
transport_t rf2500_open(const char *devpath, const char *requested_serial,
|
|
|
|
int has_vid_pid, uint16_t vid, uint16_t pid)
|
2009-11-16 23:52:10 +00:00
|
|
|
{
|
2010-04-30 04:57:57 +00:00
|
|
|
struct rf2500_transport *tr = malloc(sizeof(*tr));
|
2010-06-28 03:13:51 +00:00
|
|
|
struct usb_device *dev;
|
2009-11-16 23:52:10 +00:00
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
if (!tr) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error("rf2500: can't allocate memory");
|
2010-04-30 04:57:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-11-28 18:55:36 +00:00
|
|
|
memset(tr, 0, sizeof(*tr));
|
2012-09-10 02:22:00 +00:00
|
|
|
tr->base.ops = &rf2500_transport;
|
2010-04-30 04:57:57 +00:00
|
|
|
|
2009-11-16 23:52:10 +00:00
|
|
|
usb_init();
|
|
|
|
usb_find_busses();
|
|
|
|
usb_find_devices();
|
|
|
|
|
2010-06-28 03:13:51 +00:00
|
|
|
if (devpath)
|
|
|
|
dev = usbutil_find_by_loc(devpath);
|
|
|
|
else
|
2021-10-05 17:34:48 +00:00
|
|
|
dev = usbutil_find_by_id(has_vid_pid ? vid : USB_FET_VENDOR,
|
|
|
|
has_vid_pid ? pid : USB_FET_PRODUCT,
|
2011-07-18 02:56:29 +00:00
|
|
|
requested_serial);
|
2009-11-16 23:52:10 +00:00
|
|
|
|
2010-06-28 03:13:51 +00:00
|
|
|
if (!dev) {
|
|
|
|
free(tr);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-04-30 04:57:57 +00:00
|
|
|
|
2010-06-28 03:13:51 +00:00
|
|
|
if (open_device(tr, dev) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
printc_err("rf2500: failed to open RF2500 device\n");
|
2012-10-26 17:01:29 +00:00
|
|
|
free(tr);
|
2010-06-28 03:13:51 +00:00
|
|
|
return NULL;
|
2009-11-16 23:52:10 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 02:22:00 +00:00
|
|
|
usbtr_flush(&tr->base);
|
2010-06-28 03:13:51 +00:00
|
|
|
|
|
|
|
return (transport_t)tr;
|
2009-11-16 23:52:10 +00:00
|
|
|
}
|