2009-08-14 13:19:32 +00:00
|
|
|
/*
|
2012-05-04 07:49:58 +00:00
|
|
|
* Copyright (C) 2009-2012 Red Hat, Inc.
|
2009-08-14 13:19:32 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-08-14 13:19:32 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "hostusb.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "virterror_internal.h"
|
|
|
|
|
2010-01-11 16:40:46 +00:00
|
|
|
#define USB_SYSFS "/sys/bus/usb"
|
2009-08-14 13:19:32 +00:00
|
|
|
#define USB_DEVFS "/dev/bus/usb/"
|
2010-01-11 16:40:46 +00:00
|
|
|
#define USB_ID_LEN 10 /* "1234 5678" */
|
|
|
|
#define USB_ADDR_LEN 8 /* "123:456" */
|
2009-08-14 13:19:32 +00:00
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
/* For virReportOOMError() and virReportSystemError() */
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2009-08-14 13:19:32 +00:00
|
|
|
struct _usbDevice {
|
2012-05-04 07:49:58 +00:00
|
|
|
unsigned int bus;
|
|
|
|
unsigned int dev;
|
2009-08-14 13:19:32 +00:00
|
|
|
|
|
|
|
char name[USB_ADDR_LEN]; /* domain:bus:slot.function */
|
|
|
|
char id[USB_ID_LEN]; /* product vendor */
|
2011-06-22 20:52:32 +00:00
|
|
|
char *path;
|
2011-12-21 17:58:29 +00:00
|
|
|
const char *used_by; /* name of the domain using this dev */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _usbDeviceList {
|
|
|
|
unsigned int count;
|
|
|
|
usbDevice **devs;
|
2009-08-14 13:19:32 +00:00
|
|
|
};
|
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
typedef enum {
|
|
|
|
USB_DEVICE_ALL = 0,
|
|
|
|
USB_DEVICE_FIND_BY_VENDOR = 1 << 0,
|
|
|
|
USB_DEVICE_FIND_BY_BUS = 1 << 1,
|
|
|
|
} usbDeviceFindFlags;
|
2009-08-14 13:19:32 +00:00
|
|
|
|
2010-02-04 23:25:16 +00:00
|
|
|
static int usbSysReadFile(const char *f_name, const char *d_name,
|
2012-05-04 07:49:58 +00:00
|
|
|
int base, unsigned int *value)
|
2010-01-11 16:40:46 +00:00
|
|
|
{
|
|
|
|
int ret = -1, tmp;
|
|
|
|
char *buf = NULL;
|
|
|
|
char *filename = NULL;
|
|
|
|
char *ignore = NULL;
|
|
|
|
|
|
|
|
tmp = virAsprintf(&filename, USB_SYSFS "/devices/%s/%s", d_name, f_name);
|
|
|
|
if (tmp < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2010-01-25 15:54:06 +00:00
|
|
|
goto cleanup;
|
2010-01-11 16:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (virFileReadAll(filename, 1024, &buf) < 0)
|
2010-01-25 15:54:06 +00:00
|
|
|
goto cleanup;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
|
|
|
if (virStrToLong_ui(buf, &ignore, base, value) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-11 16:40:46 +00:00
|
|
|
_("Could not parse usb file %s"), filename);
|
2010-01-25 15:54:06 +00:00
|
|
|
goto cleanup;
|
2010-01-11 16:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2010-01-25 15:54:06 +00:00
|
|
|
cleanup:
|
2010-01-11 16:40:46 +00:00
|
|
|
VIR_FREE(filename);
|
|
|
|
VIR_FREE(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
static usbDeviceList *
|
|
|
|
usbDeviceSearch(unsigned int vendor,
|
|
|
|
unsigned int product,
|
|
|
|
unsigned int bus,
|
|
|
|
unsigned int devno,
|
|
|
|
unsigned int flags)
|
2010-01-11 16:40:46 +00:00
|
|
|
{
|
|
|
|
DIR *dir = NULL;
|
2012-05-04 07:49:58 +00:00
|
|
|
bool found = false;
|
2010-01-11 16:40:46 +00:00
|
|
|
char *ignore = NULL;
|
|
|
|
struct dirent *de;
|
2012-05-04 07:49:58 +00:00
|
|
|
usbDeviceList *list = NULL, *ret = NULL;
|
|
|
|
usbDevice *usb;
|
|
|
|
|
|
|
|
if (!(list = usbDeviceListNew()))
|
|
|
|
goto cleanup;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
|
|
|
dir = opendir(USB_SYSFS "/devices");
|
|
|
|
if (!dir) {
|
2010-02-04 20:02:58 +00:00
|
|
|
virReportSystemError(errno,
|
2010-01-11 16:40:46 +00:00
|
|
|
_("Could not open directory %s"),
|
|
|
|
USB_SYSFS "/devices");
|
2010-01-25 15:54:06 +00:00
|
|
|
goto cleanup;
|
2010-01-11 16:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while ((de = readdir(dir))) {
|
2012-05-04 07:49:58 +00:00
|
|
|
unsigned int found_prod, found_vend, found_bus, found_devno;
|
|
|
|
char *tmpstr = de->d_name;
|
|
|
|
|
2010-01-11 16:40:46 +00:00
|
|
|
if (de->d_name[0] == '.' || strchr(de->d_name, ':'))
|
|
|
|
continue;
|
|
|
|
|
2010-02-04 23:25:16 +00:00
|
|
|
if (usbSysReadFile("idVendor", de->d_name,
|
2010-01-11 16:40:46 +00:00
|
|
|
16, &found_vend) < 0)
|
2010-01-25 15:54:06 +00:00
|
|
|
goto cleanup;
|
2012-05-04 07:49:58 +00:00
|
|
|
|
2010-02-04 23:25:16 +00:00
|
|
|
if (usbSysReadFile("idProduct", de->d_name,
|
2010-01-11 16:40:46 +00:00
|
|
|
16, &found_prod) < 0)
|
2010-01-25 15:54:06 +00:00
|
|
|
goto cleanup;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
if (STRPREFIX(de->d_name, "usb"))
|
|
|
|
tmpstr += 3;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
if (virStrToLong_ui(tmpstr, &ignore, 10, &found_bus) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2012-05-04 07:49:58 +00:00
|
|
|
_("Failed to parse dir name '%s'"),
|
|
|
|
de->d_name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2010-01-11 16:40:46 +00:00
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
if (usbSysReadFile("devnum", de->d_name,
|
|
|
|
10, &found_devno) < 0)
|
|
|
|
goto cleanup;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
if ((flags & USB_DEVICE_FIND_BY_VENDOR) &&
|
|
|
|
(found_prod != product || found_vend != vendor))
|
|
|
|
continue;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
if (flags & USB_DEVICE_FIND_BY_BUS) {
|
|
|
|
if (found_bus != bus || found_devno != devno)
|
|
|
|
continue;
|
|
|
|
found = true;
|
2010-01-11 16:40:46 +00:00
|
|
|
}
|
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
usb = usbGetDevice(found_bus, found_devno);
|
|
|
|
if (!usb)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (usbDeviceListAdd(list, usb) < 0) {
|
|
|
|
usbFreeDevice(usb);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = list;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
2010-01-25 15:54:06 +00:00
|
|
|
cleanup:
|
|
|
|
if (dir) {
|
|
|
|
int saved_errno = errno;
|
2012-05-04 07:49:58 +00:00
|
|
|
closedir(dir);
|
2010-01-25 15:54:06 +00:00
|
|
|
errno = saved_errno;
|
|
|
|
}
|
2012-05-04 07:49:58 +00:00
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
usbDeviceListFree(list);
|
2010-01-11 16:40:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2009-08-14 13:19:32 +00:00
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
int
|
|
|
|
usbFindDeviceByVendor(unsigned int vendor,
|
|
|
|
unsigned product,
|
|
|
|
bool mandatory,
|
|
|
|
usbDeviceList **devices)
|
2012-05-04 07:49:58 +00:00
|
|
|
{
|
|
|
|
usbDeviceList *list;
|
2012-10-03 14:57:28 +00:00
|
|
|
int count;
|
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
if (!(list = usbDeviceSearch(vendor, product, 0 , 0,
|
|
|
|
USB_DEVICE_FIND_BY_VENDOR)))
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
2012-05-04 07:49:58 +00:00
|
|
|
|
|
|
|
if (list->count == 0) {
|
2012-10-03 14:57:28 +00:00
|
|
|
usbDeviceListFree(list);
|
|
|
|
if (!mandatory) {
|
|
|
|
VIR_DEBUG("Did not find USB device %x:%x",
|
|
|
|
vendor, product);
|
|
|
|
if (devices)
|
|
|
|
*devices = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2012-05-04 07:49:58 +00:00
|
|
|
_("Did not find USB device %x:%x"), vendor, product);
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
2012-05-04 07:49:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
count = list->count;
|
|
|
|
if (devices)
|
|
|
|
*devices = list;
|
|
|
|
else
|
|
|
|
usbDeviceListFree(list);
|
|
|
|
|
|
|
|
return count;
|
2012-05-04 07:49:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
int
|
|
|
|
usbFindDeviceByBus(unsigned int bus,
|
|
|
|
unsigned devno,
|
|
|
|
bool mandatory,
|
|
|
|
usbDevice **usb)
|
2012-05-04 07:49:58 +00:00
|
|
|
{
|
|
|
|
usbDeviceList *list;
|
|
|
|
|
|
|
|
if (!(list = usbDeviceSearch(0, 0, bus, devno,
|
|
|
|
USB_DEVICE_FIND_BY_BUS)))
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
2012-05-04 07:49:58 +00:00
|
|
|
|
|
|
|
if (list->count == 0) {
|
2012-10-03 14:57:28 +00:00
|
|
|
usbDeviceListFree(list);
|
|
|
|
if (!mandatory) {
|
|
|
|
VIR_DEBUG("Did not find USB device bus:%u device:%u",
|
|
|
|
bus, devno);
|
|
|
|
if (usb)
|
|
|
|
*usb = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2012-05-04 07:49:58 +00:00
|
|
|
_("Did not find USB device bus:%u device:%u"),
|
|
|
|
bus, devno);
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
2012-05-04 07:49:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
if (usb) {
|
|
|
|
*usb = usbDeviceListGet(list, 0);
|
|
|
|
usbDeviceListSteal(list, *usb);
|
|
|
|
}
|
2012-05-04 07:49:58 +00:00
|
|
|
usbDeviceListFree(list);
|
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
return 0;
|
2012-05-04 07:49:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
int
|
2012-05-04 07:49:58 +00:00
|
|
|
usbFindDevice(unsigned int vendor,
|
|
|
|
unsigned int product,
|
|
|
|
unsigned int bus,
|
2012-10-03 14:57:28 +00:00
|
|
|
unsigned int devno,
|
|
|
|
bool mandatory,
|
|
|
|
usbDevice **usb)
|
2012-05-04 07:49:58 +00:00
|
|
|
{
|
|
|
|
usbDeviceList *list;
|
|
|
|
|
|
|
|
unsigned int flags = USB_DEVICE_FIND_BY_VENDOR|USB_DEVICE_FIND_BY_BUS;
|
|
|
|
if (!(list = usbDeviceSearch(vendor, product, bus, devno, flags)))
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
2012-05-04 07:49:58 +00:00
|
|
|
|
|
|
|
if (list->count == 0) {
|
2012-10-03 14:57:28 +00:00
|
|
|
usbDeviceListFree(list);
|
|
|
|
if (!mandatory) {
|
|
|
|
VIR_DEBUG("Did not find USB device %x:%x bus:%u device:%u",
|
|
|
|
vendor, product, bus, devno);
|
|
|
|
if (usb)
|
|
|
|
*usb = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2012-05-04 07:49:58 +00:00
|
|
|
_("Did not find USB device %x:%x bus:%u device:%u"),
|
|
|
|
vendor, product, bus, devno);
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
2012-05-04 07:49:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
if (usb) {
|
|
|
|
*usb = usbDeviceListGet(list, 0);
|
|
|
|
usbDeviceListSteal(list, *usb);
|
|
|
|
}
|
2012-05-04 07:49:58 +00:00
|
|
|
usbDeviceListFree(list);
|
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
return 0;
|
2012-05-04 07:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
usbDevice *
|
|
|
|
usbGetDevice(unsigned int bus,
|
|
|
|
unsigned int devno)
|
2009-08-14 13:19:32 +00:00
|
|
|
{
|
|
|
|
usbDevice *dev;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(dev) < 0) {
|
2010-02-04 18:19:08 +00:00
|
|
|
virReportOOMError();
|
2009-08-14 13:19:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->bus = bus;
|
|
|
|
dev->dev = devno;
|
|
|
|
|
2011-06-22 20:52:32 +00:00
|
|
|
if (snprintf(dev->name, sizeof(dev->name), "%.3o:%.3o",
|
|
|
|
dev->bus, dev->dev) >= sizeof(dev->name)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-06-22 20:52:32 +00:00
|
|
|
_("dev->name buffer overflow: %.3o:%.3o"),
|
|
|
|
dev->bus, dev->dev);
|
|
|
|
usbFreeDevice(dev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (virAsprintf(&dev->path, USB_DEVFS "%03d/%03d",
|
|
|
|
dev->bus, dev->dev) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
usbFreeDevice(dev);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-08-14 13:19:32 +00:00
|
|
|
|
|
|
|
/* XXX fixme. this should be product/vendor */
|
2011-06-22 20:52:32 +00:00
|
|
|
if (snprintf(dev->id, sizeof(dev->id), "%d %d", dev->bus,
|
|
|
|
dev->dev) >= sizeof(dev->id)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-06-22 20:52:32 +00:00
|
|
|
_("dev->id buffer overflow: %d %d"),
|
|
|
|
dev->bus, dev->dev);
|
|
|
|
usbFreeDevice(dev);
|
|
|
|
return NULL;
|
|
|
|
}
|
2009-08-14 13:19:32 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: initialized", dev->id, dev->name);
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-02-04 23:25:16 +00:00
|
|
|
usbFreeDevice(usbDevice *dev)
|
2009-08-14 13:19:32 +00:00
|
|
|
{
|
|
|
|
VIR_DEBUG("%s %s: freeing", dev->id, dev->name);
|
2011-06-22 20:52:32 +00:00
|
|
|
VIR_FREE(dev->path);
|
2009-08-14 13:19:32 +00:00
|
|
|
VIR_FREE(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-21 17:58:29 +00:00
|
|
|
void usbDeviceSetUsedBy(usbDevice *dev,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
dev->used_by = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * usbDeviceGetUsedBy(usbDevice *dev)
|
|
|
|
{
|
|
|
|
return dev->used_by;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *usbDeviceGetName(usbDevice *dev)
|
|
|
|
{
|
|
|
|
return dev->name;
|
|
|
|
}
|
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
unsigned int usbDeviceGetBus(usbDevice *dev)
|
2010-03-04 11:48:16 +00:00
|
|
|
{
|
|
|
|
return dev->bus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
unsigned int usbDeviceGetDevno(usbDevice *dev)
|
2010-03-04 11:48:16 +00:00
|
|
|
{
|
|
|
|
return dev->dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-02-10 09:55:39 +00:00
|
|
|
int usbDeviceFileIterate(usbDevice *dev,
|
2009-08-14 13:19:32 +00:00
|
|
|
usbDeviceFileActor actor,
|
|
|
|
void *opaque)
|
|
|
|
{
|
2010-02-10 09:55:39 +00:00
|
|
|
return (actor)(dev, dev->path, opaque);
|
2009-08-14 13:19:32 +00:00
|
|
|
}
|
2011-12-21 17:58:29 +00:00
|
|
|
|
|
|
|
usbDeviceList *
|
|
|
|
usbDeviceListNew(void)
|
|
|
|
{
|
|
|
|
usbDeviceList *list;
|
|
|
|
|
|
|
|
if (VIR_ALLOC(list) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usbDeviceListFree(usbDeviceList *list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++)
|
|
|
|
usbFreeDevice(list->devs[i]);
|
|
|
|
|
|
|
|
VIR_FREE(list->devs);
|
|
|
|
VIR_FREE(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
usbDeviceListAdd(usbDeviceList *list,
|
|
|
|
usbDevice *dev)
|
|
|
|
{
|
|
|
|
if (usbDeviceListFind(list, dev)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-12-21 17:58:29 +00:00
|
|
|
_("Device %s is already in use"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (VIR_REALLOC_N(list->devs, list->count+1) < 0) {
|
|
|
|
virReportOOMError();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
list->devs[list->count++] = dev;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
usbDevice *
|
|
|
|
usbDeviceListGet(usbDeviceList *list,
|
|
|
|
int idx)
|
|
|
|
{
|
|
|
|
if (idx >= list->count ||
|
|
|
|
idx < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list->devs[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
usbDeviceListCount(usbDeviceList *list)
|
|
|
|
{
|
|
|
|
return list->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
usbDevice *
|
|
|
|
usbDeviceListSteal(usbDeviceList *list,
|
|
|
|
usbDevice *dev)
|
|
|
|
{
|
|
|
|
usbDevice *ret = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
|
|
|
if (list->devs[i]->bus != dev->bus ||
|
|
|
|
list->devs[i]->dev != dev->dev)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = list->devs[i];
|
|
|
|
|
|
|
|
if (i != list->count--)
|
|
|
|
memmove(&list->devs[i],
|
|
|
|
&list->devs[i+1],
|
|
|
|
sizeof(*list->devs) * (list->count - i));
|
|
|
|
|
|
|
|
if (VIR_REALLOC_N(list->devs, list->count) < 0) {
|
|
|
|
; /* not fatal */
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
usbDeviceListDel(usbDeviceList *list,
|
|
|
|
usbDevice *dev)
|
|
|
|
{
|
|
|
|
usbDevice *ret = usbDeviceListSteal(list, dev);
|
|
|
|
if (ret)
|
|
|
|
usbFreeDevice(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
usbDevice *
|
|
|
|
usbDeviceListFind(usbDeviceList *list,
|
|
|
|
usbDevice *dev)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
|
|
|
if (list->devs[i]->bus == dev->bus &&
|
|
|
|
list->devs[i]->dev == dev->dev)
|
|
|
|
return list->devs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|