2005-11-01 23:02:02 +00:00
|
|
|
/*
|
2006-10-15 00:27:52 +00:00
|
|
|
* Hamlib Interface - USB communication low-level support
|
2009-09-14 07:32:36 +00:00
|
|
|
* Copyright (c) 2000-2009 by Stephane Fillod
|
2005-11-01 23:02:02 +00:00
|
|
|
*
|
2007-10-07 20:12:36 +00:00
|
|
|
* $Id: usb_port.c,v 1.5 2007-10-07 20:12:36 fillods Exp $
|
2005-11-01 23:02:02 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library 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 Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup rig_internal
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief USB IO
|
|
|
|
* \file usb_port.c
|
|
|
|
*
|
|
|
|
* doc todo: deal with defined(HAVE_LIBUSB)... quashing the doc process.
|
|
|
|
*/
|
|
|
|
|
2005-11-01 23:02:02 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2005-11-19 14:41:37 +00:00
|
|
|
#include <hamlib/rig.h>
|
|
|
|
|
2005-11-01 23:02:02 +00:00
|
|
|
/*
|
2006-02-20 22:41:17 +00:00
|
|
|
* Compile only if libusb is available
|
2005-11-01 23:02:02 +00:00
|
|
|
*/
|
2005-11-19 14:41:37 +00:00
|
|
|
#if defined(HAVE_LIBUSB) && defined(HAVE_USB_H)
|
2005-11-01 23:02:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h> /* Standard input/output definitions */
|
|
|
|
#include <string.h> /* String function definitions */
|
|
|
|
#include <unistd.h> /* UNIX standard function definitions */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <usb.h>
|
|
|
|
#include "usb_port.h"
|
|
|
|
|
2009-09-14 07:32:36 +00:00
|
|
|
|
2006-10-15 00:27:52 +00:00
|
|
|
/**
|
2009-09-14 07:32:36 +00:00
|
|
|
* \brief Get ASCII string from USB descriptor
|
|
|
|
* \param udh
|
|
|
|
* \param index
|
|
|
|
* \param langid
|
|
|
|
* \param buf
|
|
|
|
* \param buflen
|
|
|
|
* \return status/len
|
2006-10-15 00:27:52 +00:00
|
|
|
*/
|
2009-09-14 07:32:36 +00:00
|
|
|
static int usbGetStringAscii(usb_dev_handle *udh, int index, int langid, char *buf, int buflen)
|
2005-11-01 23:02:02 +00:00
|
|
|
{
|
2009-09-14 07:32:36 +00:00
|
|
|
char buffer[256];
|
|
|
|
int rval, i;
|
|
|
|
|
|
|
|
rval = usb_control_msg(udh, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR,
|
|
|
|
(USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000);
|
|
|
|
if(rval < 0)
|
|
|
|
return rval;
|
|
|
|
if(buffer[1] != USB_DT_STRING)
|
|
|
|
return 0;
|
|
|
|
if((unsigned char)buffer[0] < rval)
|
|
|
|
rval = (unsigned char)buffer[0];
|
|
|
|
rval /= 2;
|
|
|
|
/* lossy conversion to ISO Latin1 */
|
|
|
|
for(i=1;i<rval;i++){
|
|
|
|
if(i > buflen) /* destination buffer overflow */
|
|
|
|
break;
|
|
|
|
buf[i-1] = buffer[2 * i];
|
|
|
|
if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
|
|
|
|
buf[i-1] = '?';
|
|
|
|
}
|
|
|
|
buf[i-1] = 0;
|
|
|
|
return i-1;
|
|
|
|
}
|
2005-11-01 23:02:02 +00:00
|
|
|
|
2009-09-14 07:32:36 +00:00
|
|
|
/**
|
|
|
|
* \brief Find and open USB device
|
|
|
|
* \param port
|
|
|
|
* \return usb_handle
|
|
|
|
*/
|
|
|
|
static struct usb_dev_handle *find_and_open_device(const hamlib_port_t *port)
|
|
|
|
{
|
|
|
|
struct usb_bus *bus;
|
|
|
|
struct usb_device *dev;
|
|
|
|
struct usb_dev_handle *udh;
|
|
|
|
char string[256];
|
|
|
|
int len;
|
2007-10-07 20:12:36 +00:00
|
|
|
|
|
|
|
rig_debug(RIG_DEBUG_TRACE, "%s: looking for device %04x:%04x...", __FUNCTION__,
|
|
|
|
port->parm.usb.vid, port->parm.usb.pid);
|
|
|
|
|
2009-09-14 07:32:36 +00:00
|
|
|
for (bus = usb_get_busses(); bus != NULL; bus = bus->next) {
|
|
|
|
|
|
|
|
for (dev = bus->devices; dev != NULL; dev = dev->next) {
|
|
|
|
|
|
|
|
rig_debug(RIG_DEBUG_TRACE, " %04x:%04x,", dev->descriptor.idVendor,
|
|
|
|
dev->descriptor.idProduct);
|
|
|
|
if (dev->descriptor.idVendor == port->parm.usb.vid &&
|
|
|
|
dev->descriptor.idProduct == port->parm.usb.pid) {
|
|
|
|
|
|
|
|
/* we need to open the device in order to query strings */
|
|
|
|
udh = usb_open(dev);
|
|
|
|
if (!udh){
|
|
|
|
rig_debug(RIG_DEBUG_WARN, "Warning: cannot open USB device: %s\n", usb_strerror());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now check whether the names match: */
|
|
|
|
if (port->parm.usb.vendor_name) {
|
|
|
|
len = usbGetStringAscii(udh, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string));
|
|
|
|
if (len < 0) {
|
|
|
|
rig_debug(RIG_DEBUG_WARN, "Warning: cannot query manufacturer for USB device: %s\n", usb_strerror());
|
|
|
|
usb_close(udh);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rig_debug(RIG_DEBUG_TRACE, " vendor >%s<", string);
|
|
|
|
if (strcmp(string, port->parm.usb.vendor_name) != 0){
|
|
|
|
usb_close(udh);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (port->parm.usb.product) {
|
|
|
|
len = usbGetStringAscii(udh, dev->descriptor.iProduct, 0x0409, string, sizeof(string));
|
|
|
|
if (len < 0) {
|
|
|
|
rig_debug(RIG_DEBUG_WARN, "Warning: cannot query product for USB device: %s\n", usb_strerror());
|
|
|
|
usb_close(udh);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rig_debug(RIG_DEBUG_TRACE, " product >%s<", string);
|
|
|
|
if (strcmp(string, port->parm.usb.product) != 0){
|
|
|
|
usb_close(udh);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rig_debug(RIG_DEBUG_TRACE, " -> found\n");
|
|
|
|
return udh;
|
2005-11-01 23:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 20:12:36 +00:00
|
|
|
rig_debug(RIG_DEBUG_TRACE, " -> not found\n");
|
2005-11-01 23:02:02 +00:00
|
|
|
return NULL; /* not found */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-14 07:32:36 +00:00
|
|
|
/**
|
|
|
|
* \brief Open hamlib_port of USB device
|
|
|
|
* \param port
|
|
|
|
* \return status
|
|
|
|
*/
|
2005-11-01 23:02:02 +00:00
|
|
|
int usb_port_open(hamlib_port_t *port)
|
|
|
|
{
|
|
|
|
struct usb_dev_handle *udh;
|
|
|
|
|
|
|
|
|
|
|
|
if (!port->pathname)
|
|
|
|
return -RIG_EINVAL;
|
|
|
|
|
|
|
|
usb_init (); /* usb library init */
|
2007-10-07 20:12:36 +00:00
|
|
|
if (usb_find_busses () < 0)
|
2009-09-14 07:32:36 +00:00
|
|
|
rig_debug(RIG_DEBUG_ERR, "%s: usb_find_busses failed %s\n",
|
2007-10-07 20:12:36 +00:00
|
|
|
__FUNCTION__, usb_strerror());
|
|
|
|
if (usb_find_devices() < 0)
|
2009-09-14 07:32:36 +00:00
|
|
|
rig_debug(RIG_DEBUG_ERR, "%s: usb_find_devices failed %s\n",
|
2007-10-07 20:12:36 +00:00
|
|
|
__FUNCTION__, usb_strerror());
|
2005-11-01 23:02:02 +00:00
|
|
|
|
2009-09-14 07:32:36 +00:00
|
|
|
udh = find_and_open_device(port);
|
|
|
|
if (udh == 0)
|
|
|
|
return -RIG_EIO;
|
2005-11-01 23:02:02 +00:00
|
|
|
|
2007-10-07 20:12:36 +00:00
|
|
|
#ifdef LIBUSB_HAS_GET_DRIVER_NP
|
|
|
|
/* Try to detach ftdi_sio kernel module
|
|
|
|
* Returns ENODATA if driver is not loaded
|
|
|
|
*/
|
|
|
|
if (usb_detach_kernel_driver_np(udh, port->parm.usb.iface) != 0 && errno != ENODATA) {
|
|
|
|
if (errno == EPERM)
|
|
|
|
rig_debug(RIG_DEBUG_ERR, "%s: inappropriate permissions\n", __FUNCTION__);
|
|
|
|
usb_close(udh);
|
|
|
|
return -RIG_EIO;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2005-11-01 23:02:02 +00:00
|
|
|
if (usb_set_configuration (udh, port->parm.usb.conf) < 0){
|
2007-10-07 20:12:36 +00:00
|
|
|
rig_debug(RIG_DEBUG_ERR, "%s: usb_set_configuration: failed conf %d: %s\n",
|
2005-11-01 23:02:02 +00:00
|
|
|
__FUNCTION__,port->parm.usb.conf, usb_strerror());
|
|
|
|
usb_close (udh);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-07 20:12:36 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
rig_debug(RIG_DEBUG_VERBOSE, "%s: %d\n", __FUNCTION__, port->parm.usb.iface);
|
2005-11-01 23:02:02 +00:00
|
|
|
|
|
|
|
if (usb_claim_interface (udh, port->parm.usb.iface) < 0){
|
|
|
|
rig_debug(RIG_DEBUG_ERR, "%s:usb_claim_interface: failed interface %d: %s\n",
|
|
|
|
__FUNCTION__,port->parm.usb.iface, usb_strerror());
|
|
|
|
usb_close (udh);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-07 20:12:36 +00:00
|
|
|
#if 0
|
2005-11-01 23:02:02 +00:00
|
|
|
if (usb_set_altinterface (udh, port->parm.usb.alt) < 0){
|
|
|
|
fprintf (stderr, "%s:usb_set_alt_interface: failed: %s\n", __FUNCTION__,
|
|
|
|
usb_strerror());
|
|
|
|
usb_release_interface (udh, port->parm.usb.iface);
|
|
|
|
usb_close (udh);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-10-07 20:12:36 +00:00
|
|
|
#endif
|
2005-11-01 23:02:02 +00:00
|
|
|
|
|
|
|
port->handle = (void*) udh;
|
|
|
|
|
|
|
|
return RIG_OK;
|
|
|
|
}
|
|
|
|
|
2009-09-14 07:32:36 +00:00
|
|
|
/**
|
|
|
|
* \brief Close hamlib_port of USB device
|
|
|
|
* \param port
|
|
|
|
* \return status
|
|
|
|
*/
|
2005-11-01 23:02:02 +00:00
|
|
|
int usb_port_close(hamlib_port_t *port)
|
|
|
|
{
|
|
|
|
struct usb_dev_handle *udh = port->handle;
|
|
|
|
|
|
|
|
/* we're assuming that closing an interface automatically releases it. */
|
|
|
|
return usb_close (udh) == 0 ? RIG_OK : -RIG_EIO;
|
|
|
|
}
|
|
|
|
|
2005-11-19 14:41:37 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
int usb_port_open(hamlib_port_t *port)
|
|
|
|
{
|
|
|
|
return -RIG_ENAVAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int usb_port_close(hamlib_port_t *port)
|
|
|
|
{
|
|
|
|
return -RIG_ENAVAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* defined(HAVE_LIBUSB) && defined(HAVE_USB_H) */
|
2006-10-15 00:27:52 +00:00
|
|
|
|
|
|
|
/** @} */
|