2010-06-27 22:35:36 +00:00
|
|
|
/* MSPDebug - debugging tool for the eZ430
|
2012-09-10 02:22:00 +00:00
|
|
|
* Copyright (C) 2009-2012 Daniel Beer
|
2010-06-27 22:35:36 +00:00
|
|
|
* Copyright (C) 2010 Peter Jansen
|
|
|
|
*
|
|
|
|
* 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>
|
2010-06-27 22:35:36 +00:00
|
|
|
#include <string.h>
|
2016-12-20 20:57:23 +00:00
|
|
|
|
|
|
|
#ifndef __Windows__
|
2010-06-27 22:35:36 +00:00
|
|
|
#include <usb.h>
|
2016-12-20 20:57:23 +00:00
|
|
|
#else
|
|
|
|
#include <lusb0_usb.h>
|
|
|
|
#endif
|
|
|
|
|
2013-05-06 20:08:33 +00:00
|
|
|
#include <time.h>
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
#include "cp210x.h"
|
2010-06-27 22:35:36 +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"
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
struct cp210x_transport {
|
2010-06-27 22:35:36 +00:00
|
|
|
struct transport base;
|
|
|
|
|
|
|
|
struct usb_dev_handle *handle;
|
2012-09-13 01:55:14 +00:00
|
|
|
int int_number;
|
2010-06-27 22:35:36 +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.
|
|
|
|
*/
|
|
|
|
|
2013-01-16 21:19:20 +00:00
|
|
|
#define CP210X_CLOCK 3500000
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2011-06-05 23:28:13 +00:00
|
|
|
#define V1_INTERFACE_CLASS 255
|
|
|
|
#define V1_IN_EP 0x81
|
|
|
|
#define V1_OUT_EP 0x01
|
|
|
|
|
2010-06-27 22:35:36 +00:00
|
|
|
#define CP210x_REQTYPE_HOST_TO_DEVICE 0x41
|
|
|
|
|
|
|
|
#define CP210X_IFC_ENABLE 0x00
|
2010-06-29 00:17:57 +00:00
|
|
|
#define CP210X_SET_BAUDDIV 0x01
|
2010-06-27 22:35:36 +00:00
|
|
|
#define CP210X_SET_MHS 0x07
|
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
/* CP210X_(SET_MHS|GET_MDMSTS) */
|
|
|
|
#define CP210X_DTR 0x0001
|
|
|
|
#define CP210X_RTS 0x0002
|
|
|
|
#define CP210X_CTS 0x0010
|
|
|
|
#define CP210X_DSR 0x0020
|
|
|
|
#define CP210X_RING 0x0040
|
|
|
|
#define CP210X_DCD 0x0080
|
|
|
|
#define CP210X_WRITE_DTR 0x0100
|
|
|
|
#define CP210X_WRITE_RTS 0x0200
|
|
|
|
|
2013-05-06 20:08:33 +00:00
|
|
|
#define TIMEOUT_S 30
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
static int configure_port(struct cp210x_transport *tr, int baud_rate)
|
2011-12-25 22:09:13 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE,
|
|
|
|
CP210X_IFC_ENABLE, 0x1, 0, NULL, 0, 300);
|
2012-09-13 01:55:14 +00:00
|
|
|
#ifdef DEBUG_CP210X
|
2011-12-25 22:09:13 +00:00
|
|
|
printc("%s: %s: Sending control message "
|
|
|
|
"CP210x_REQTYPE_HOST_TO_DEVICE, ret = %d\n",
|
|
|
|
__FILE__, __FUNCTION__, ret);
|
|
|
|
#endif
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_error(__FILE__": can't enable CP210x UART");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the baud rate to 500000 bps */
|
|
|
|
ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE,
|
2012-09-13 01:55:14 +00:00
|
|
|
CP210X_SET_BAUDDIV, CP210X_CLOCK / baud_rate,
|
|
|
|
0, NULL, 0, 300);
|
|
|
|
#ifdef DEBUG_CP210X
|
2011-12-25 22:09:13 +00:00
|
|
|
printc("%s: %s: Sending control message "
|
|
|
|
"CP210X_SET_BAUDDIV, ret = %d\n",
|
|
|
|
__FILE__, __FUNCTION__, ret);
|
|
|
|
#endif
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_error(__FILE__": can't set baud rate");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the modem control settings.
|
2012-09-13 01:55:14 +00:00
|
|
|
* Clear RTS, DTR and WRITE_DTR, WRITE_RTS
|
2011-12-25 22:09:13 +00:00
|
|
|
*/
|
|
|
|
ret = usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE,
|
|
|
|
CP210X_SET_MHS, 0x303, 0, NULL, 0, 300);
|
2012-09-13 01:55:14 +00:00
|
|
|
#ifdef DEBUG_CP210X
|
2011-12-25 22:09:13 +00:00
|
|
|
printc("%s: %s: Sending control message "
|
|
|
|
"CP210X_SET_MHS, ret %d\n",
|
|
|
|
__FILE__, __FUNCTION__, ret);
|
|
|
|
#endif
|
|
|
|
if (ret < 0) {
|
|
|
|
pr_error(__FILE__": can't set modem control");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
static int open_interface(struct cp210x_transport *tr,
|
|
|
|
struct usb_device *dev, int ino,
|
|
|
|
int baud_rate)
|
2010-06-27 22:35:36 +00:00
|
|
|
{
|
2010-12-05 23:25:45 +00:00
|
|
|
#if defined(__linux__)
|
2010-06-27 22:35:36 +00:00
|
|
|
int drv;
|
|
|
|
char drName[256];
|
|
|
|
#endif
|
|
|
|
|
2013-10-25 20:59:48 +00:00
|
|
|
printc_dbg(__FILE__": Trying to open interface %d on %s\n",
|
2010-06-27 22:35:36 +00:00
|
|
|
ino, dev->filename);
|
|
|
|
|
|
|
|
tr->int_number = ino;
|
|
|
|
|
|
|
|
tr->handle = usb_open(dev);
|
|
|
|
if (!tr->handle) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error(__FILE__": can't open device");
|
2010-06-27 22:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-12-05 23:25:45 +00:00
|
|
|
#if defined(__linux__)
|
2010-06-27 22:35:36 +00:00
|
|
|
drv = usb_get_driver_np(tr->handle, tr->int_number, drName,
|
|
|
|
sizeof(drName));
|
2010-08-16 23:07:03 +00:00
|
|
|
printc(__FILE__" : driver %d\n", drv);
|
2010-06-27 22:35:36 +00:00
|
|
|
if (drv >= 0) {
|
|
|
|
if (usb_detach_kernel_driver_np(tr->handle,
|
|
|
|
tr->int_number) < 0)
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error(__FILE__": warning: can't detach "
|
2010-06-27 22:35:36 +00:00
|
|
|
"kernel driver");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
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(__FILE__": 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-06-27 22:35:36 +00:00
|
|
|
if (usb_claim_interface(tr->handle, tr->int_number) < 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error(__FILE__": can't claim interface");
|
2010-06-27 22:35:36 +00:00
|
|
|
usb_close(tr->handle);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
if (configure_port(tr, baud_rate) < 0) {
|
2011-12-25 22:09:13 +00:00
|
|
|
printc_err("Failed to configure for V1 device\n");
|
2011-11-13 11:20:37 +00:00
|
|
|
usb_close(tr->handle);
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-27 22:35:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
static int open_device(struct cp210x_transport *tr, struct usb_device *dev,
|
|
|
|
int baud_rate)
|
2010-06-27 22:35:36 +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];
|
|
|
|
|
2011-06-05 23:28:13 +00:00
|
|
|
if (desc->bInterfaceClass == V1_INTERFACE_CLASS &&
|
2012-09-13 01:55:14 +00:00
|
|
|
!open_interface(tr, dev, desc->bInterfaceNumber,
|
|
|
|
baud_rate))
|
2011-06-05 23:28:13 +00:00
|
|
|
return 0;
|
2010-06-27 22:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usbtr_send(transport_t tr_base, const uint8_t *data, int len)
|
|
|
|
{
|
2012-09-13 01:55:14 +00:00
|
|
|
struct cp210x_transport *tr = (struct cp210x_transport *)tr_base;
|
2010-06-27 22:35:36 +00:00
|
|
|
int sent;
|
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
#ifdef DEBUG_CP210X
|
2012-02-29 03:08:26 +00:00
|
|
|
debug_hexdump(__FILE__ ": USB transfer out", data, len);
|
2010-06-27 22:35:36 +00:00
|
|
|
#endif
|
2012-02-29 03:08:26 +00:00
|
|
|
while (len) {
|
2012-09-13 01:55:14 +00:00
|
|
|
sent = usb_bulk_write(tr->handle, V1_OUT_EP,
|
2013-05-06 20:08:33 +00:00
|
|
|
(char *)data, len, TIMEOUT_S * 1000);
|
2012-02-29 03:08:26 +00:00
|
|
|
if (sent <= 0) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error(__FILE__": can't send data");
|
2010-06-27 22:35:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-02-29 03:08:26 +00:00
|
|
|
data += sent;
|
2010-06-27 22:35:36 +00:00
|
|
|
len -= sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usbtr_recv(transport_t tr_base, uint8_t *databuf, int max_len)
|
|
|
|
{
|
2012-09-13 01:55:14 +00:00
|
|
|
struct cp210x_transport *tr = (struct cp210x_transport *)tr_base;
|
2010-06-27 22:35:36 +00:00
|
|
|
int rlen;
|
2013-05-06 20:08:33 +00:00
|
|
|
time_t deadline = time(NULL) + TIMEOUT_S;
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
#ifdef DEBUG_CP210X
|
2010-08-16 23:07:03 +00:00
|
|
|
printc(__FILE__": %s : read max %d\n", __FUNCTION__, max_len);
|
2010-06-27 22:35:36 +00:00
|
|
|
#endif
|
|
|
|
|
2013-05-06 20:08:33 +00:00
|
|
|
while (time(NULL) < deadline) {
|
|
|
|
rlen = usb_bulk_read(tr->handle, V1_IN_EP, (char *)databuf,
|
|
|
|
max_len, TIMEOUT_S * 1000);
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
#ifdef DEBUG_CP210X
|
2013-05-06 20:08:33 +00:00
|
|
|
printc(__FILE__": %s : read %d\n", __FUNCTION__, rlen);
|
2010-06-27 22:35:36 +00:00
|
|
|
#endif
|
|
|
|
|
2013-05-06 20:08:33 +00:00
|
|
|
if (rlen < 0) {
|
|
|
|
pr_error(__FILE__": can't receive data");
|
|
|
|
return -1;
|
|
|
|
}
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2013-05-06 20:08:33 +00:00
|
|
|
if (rlen > 0) {
|
2012-09-13 01:55:14 +00:00
|
|
|
#ifdef DEBUG_CP210X
|
2013-05-06 20:08:33 +00:00
|
|
|
debug_hexdump(__FILE__": USB transfer in", databuf, rlen);
|
2010-06-27 22:35:36 +00:00
|
|
|
#endif
|
2013-05-06 20:08:33 +00:00
|
|
|
return rlen;
|
|
|
|
}
|
|
|
|
}
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2013-05-06 20:08:33 +00:00
|
|
|
pr_error(__FILE__": read operation timed out");
|
|
|
|
return -1;
|
2010-06-27 22:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void usbtr_destroy(transport_t tr_base)
|
|
|
|
{
|
2012-09-13 01:55:14 +00:00
|
|
|
struct cp210x_transport *tr = (struct cp210x_transport *)tr_base;
|
2010-06-27 22:35:36 +00:00
|
|
|
|
|
|
|
usb_release_interface(tr->handle, tr->int_number);
|
|
|
|
usb_close(tr->handle);
|
|
|
|
free(tr);
|
|
|
|
}
|
|
|
|
|
2012-09-10 02:22:00 +00:00
|
|
|
static int usbtr_flush(transport_t tr_base)
|
|
|
|
{
|
2012-09-13 01:55:14 +00:00
|
|
|
struct cp210x_transport *tr = (struct cp210x_transport *)tr_base;
|
2012-09-10 02:22:00 +00:00
|
|
|
char buf[64];
|
|
|
|
|
|
|
|
/* Flush out lingering data */
|
2012-09-13 01:55:14 +00:00
|
|
|
while (usb_bulk_read(tr->handle, V1_IN_EP,
|
2012-09-10 02:22:00 +00:00
|
|
|
buf, sizeof(buf),
|
|
|
|
100) > 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int usbtr_set_modem(transport_t tr_base, transport_modem_t state)
|
|
|
|
{
|
2012-09-13 01:55:14 +00:00
|
|
|
struct cp210x_transport *tr = (struct cp210x_transport *)tr_base;
|
|
|
|
int value = CP210X_WRITE_DTR | CP210X_WRITE_RTS;
|
|
|
|
|
|
|
|
/* DTR and RTS bits are active-low for this device */
|
|
|
|
if (!(state & TRANSPORT_MODEM_DTR))
|
|
|
|
value |= CP210X_DTR;
|
|
|
|
if (!(state & TRANSPORT_MODEM_RTS))
|
|
|
|
value |= CP210X_RTS;
|
|
|
|
|
|
|
|
if (usb_control_msg(tr->handle, CP210x_REQTYPE_HOST_TO_DEVICE,
|
|
|
|
CP210X_SET_MHS, value, 0, NULL, 0, 300) < 0) {
|
|
|
|
pr_error("cp210x: failed to set modem control lines\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2012-09-10 02:22:00 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
static const struct transport_class cp210x_class = {
|
2012-09-10 02:22:00 +00:00
|
|
|
.destroy = usbtr_destroy,
|
|
|
|
.send = usbtr_send,
|
|
|
|
.recv = usbtr_recv,
|
|
|
|
.flush = usbtr_flush,
|
|
|
|
.set_modem = usbtr_set_modem
|
|
|
|
};
|
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
transport_t cp210x_open(const char *devpath, const char *requested_serial,
|
|
|
|
int baud_rate, uint16_t product, uint16_t vendor)
|
2010-06-27 22:35:36 +00:00
|
|
|
{
|
2012-09-13 01:55:14 +00:00
|
|
|
struct cp210x_transport *tr = malloc(sizeof(*tr));
|
2010-06-28 03:13:51 +00:00
|
|
|
struct usb_device *dev;
|
2010-06-27 22:35:36 +00:00
|
|
|
|
|
|
|
if (!tr) {
|
2010-08-16 23:07:03 +00:00
|
|
|
pr_error(__FILE__": can't allocate memory");
|
2010-06-27 22:35:36 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
tr->base.ops = &cp210x_class;
|
2010-06-27 22:35:36 +00:00
|
|
|
|
|
|
|
usb_init();
|
|
|
|
usb_find_busses();
|
|
|
|
usb_find_devices();
|
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
if (devpath)
|
2010-06-28 03:13:51 +00:00
|
|
|
dev = usbutil_find_by_loc(devpath);
|
2012-09-13 01:55:14 +00:00
|
|
|
else
|
|
|
|
dev = usbutil_find_by_id(product, vendor, requested_serial);
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2010-06-28 03:13:51 +00:00
|
|
|
if (!dev) {
|
|
|
|
free(tr);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-06-27 22:35:36 +00:00
|
|
|
|
2012-09-13 01:55:14 +00:00
|
|
|
if (open_device(tr, dev, baud_rate) < 0) {
|
|
|
|
printc_err(__FILE__ ": failed to open CP210X device\n");
|
|
|
|
free(tr);
|
2010-06-28 03:13:51 +00:00
|
|
|
return NULL;
|
2010-06-27 22:35:36 +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;
|
2010-06-27 22:35:36 +00:00
|
|
|
}
|