2009-08-14 13:19:32 +00:00
|
|
|
/*
|
2012-12-12 17:04:51 +00:00
|
|
|
* virusb.c: helper APIs for managing host USB devices
|
|
|
|
*
|
2014-04-25 20:45:49 +00:00
|
|
|
* Copyright (C) 2009-2014 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
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-12-12 17:04:51 +00:00
|
|
|
#include "virusb.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 17:44:57 +00:00
|
|
|
#include "virutil.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2013-05-09 18:59:04 +00:00
|
|
|
#include "virfile.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2009-08-14 13:19:32 +00:00
|
|
|
|
2010-01-11 16:40:46 +00:00
|
|
|
#define USB_SYSFS "/sys/bus/usb"
|
|
|
|
#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
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.usb");
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
struct _virUSBDevice {
|
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;
|
2014-03-01 06:28:56 +00:00
|
|
|
|
|
|
|
/* driver:domain using this dev */
|
|
|
|
char *used_by_drvname;
|
|
|
|
char *used_by_domname;
|
2011-12-21 17:58:29 +00:00
|
|
|
};
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
struct _virUSBDeviceList {
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectLockable parent;
|
2013-07-05 18:46:35 +00:00
|
|
|
size_t count;
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr *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,
|
2013-01-14 22:11:44 +00:00
|
|
|
} virUSBDeviceFindFlags;
|
2009-08-14 13:19:32 +00:00
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
static virClassPtr virUSBDeviceListClass;
|
|
|
|
|
|
|
|
static void virUSBDeviceListDispose(void *obj);
|
|
|
|
|
|
|
|
static int virUSBOnceInit(void)
|
|
|
|
{
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virUSBDeviceList, virClassForObjectLockable()))
|
2013-01-16 11:49:54 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virUSB);
|
2013-01-16 11:49:54 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
static int virUSBSysReadFile(const char *f_name, const char *d_name,
|
|
|
|
int base, unsigned int *value)
|
2010-01-11 16:40:46 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *buf = NULL;
|
|
|
|
g_autofree char *filename = NULL;
|
2010-01-11 16:40:46 +00:00
|
|
|
char *ignore = NULL;
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
filename = g_strdup_printf(USB_SYSFS "/devices/%s/%s", d_name, f_name);
|
2010-01-11 16:40:46 +00:00
|
|
|
|
|
|
|
if (virFileReadAll(filename, 1024, &buf) < 0)
|
2018-07-24 15:52:25 +00:00
|
|
|
return -1;
|
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);
|
2018-07-24 15:52:25 +00:00
|
|
|
return -1;
|
2010-01-11 16:40:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:25 +00:00
|
|
|
return 0;
|
2010-01-11 16:40:46 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
static virUSBDeviceListPtr
|
|
|
|
virUSBDeviceSearch(unsigned int vendor,
|
|
|
|
unsigned int product,
|
|
|
|
unsigned int bus,
|
|
|
|
unsigned int devno,
|
|
|
|
const char *vroot,
|
|
|
|
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;
|
2018-07-24 15:52:26 +00:00
|
|
|
virUSBDeviceListPtr list = NULL;
|
|
|
|
virUSBDeviceListPtr ret = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virUSBDevice) usb = NULL;
|
2014-04-25 20:45:49 +00:00
|
|
|
int direrr;
|
2012-05-04 07:49:58 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (!(list = virUSBDeviceListNew()))
|
2012-05-04 07:49:58 +00:00
|
|
|
goto cleanup;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
2016-06-21 14:34:08 +00:00
|
|
|
if (virDirOpen(&dir, USB_SYSFS "/devices") < 0)
|
2010-01-25 15:54:06 +00:00
|
|
|
goto cleanup;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((direrr = virDirRead(dir, &de, USB_SYSFS "/devices")) > 0) {
|
2012-05-04 07:49:58 +00:00
|
|
|
unsigned int found_prod, found_vend, found_bus, found_devno;
|
|
|
|
char *tmpstr = de->d_name;
|
|
|
|
|
2016-06-21 15:29:47 +00:00
|
|
|
if (strchr(de->d_name, ':'))
|
2010-01-11 16:40:46 +00:00
|
|
|
continue;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virUSBSysReadFile("idVendor", de->d_name,
|
|
|
|
16, &found_vend) < 0)
|
2010-01-25 15:54:06 +00:00
|
|
|
goto cleanup;
|
2012-05-04 07:49:58 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virUSBSysReadFile("idProduct", de->d_name,
|
|
|
|
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
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virUSBSysReadFile("devnum", de->d_name,
|
|
|
|
10, &found_devno) < 0)
|
2012-05-04 07:49:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
usb = virUSBDeviceNew(found_bus, found_devno, vroot);
|
2018-07-24 15:52:26 +00:00
|
|
|
|
2012-05-04 07:49:58 +00:00
|
|
|
if (!usb)
|
|
|
|
goto cleanup;
|
|
|
|
|
2018-07-24 15:52:26 +00:00
|
|
|
if (virUSBDeviceListAdd(list, &usb) < 0)
|
2012-05-04 07:49:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
}
|
2014-04-25 20:45:49 +00:00
|
|
|
if (direrr < 0)
|
|
|
|
goto cleanup;
|
2012-05-04 07:49:58 +00:00
|
|
|
ret = list;
|
2010-01-11 16:40:46 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2016-06-21 10:40:29 +00:00
|
|
|
VIR_DIR_CLOSE(dir);
|
2012-05-04 07:49:58 +00:00
|
|
|
if (!ret)
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(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
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceFindByVendor(unsigned int vendor,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int product,
|
2013-01-14 22:11:44 +00:00
|
|
|
const char *vroot,
|
|
|
|
bool mandatory,
|
|
|
|
virUSBDeviceListPtr *devices)
|
2012-05-04 07:49:58 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListPtr list;
|
2012-10-03 14:57:28 +00:00
|
|
|
int count;
|
|
|
|
|
2013-11-19 23:00:32 +00:00
|
|
|
if (!(list = virUSBDeviceSearch(vendor, product, 0, 0,
|
2013-01-14 22:11:44 +00:00
|
|
|
vroot,
|
|
|
|
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) {
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2012-10-03 14:57:28 +00:00
|
|
|
if (!mandatory) {
|
2017-05-19 07:17:32 +00:00
|
|
|
VIR_DEBUG("Did not find USB device %04x:%04x",
|
2012-10-03 14:57:28 +00:00
|
|
|
vendor, product);
|
|
|
|
if (devices)
|
|
|
|
*devices = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2017-05-19 07:17:32 +00:00
|
|
|
_("Did not find USB device %04x:%04x"), 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
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2012-10-03 14:57:28 +00:00
|
|
|
|
|
|
|
return count;
|
2012-05-04 07:49:58 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceFindByBus(unsigned int bus,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int devno,
|
2013-01-14 22:11:44 +00:00
|
|
|
const char *vroot,
|
|
|
|
bool mandatory,
|
|
|
|
virUSBDevicePtr *usb)
|
2012-05-04 07:49:58 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListPtr list;
|
2012-05-04 07:49:58 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (!(list = virUSBDeviceSearch(0, 0, bus, devno,
|
|
|
|
vroot,
|
|
|
|
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) {
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2012-10-03 14:57:28 +00:00
|
|
|
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) {
|
2013-01-14 22:11:44 +00:00
|
|
|
*usb = virUSBDeviceListGet(list, 0);
|
|
|
|
virUSBDeviceListSteal(list, *usb);
|
2012-10-03 14:57:28 +00:00
|
|
|
}
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2012-05-04 07:49:58 +00:00
|
|
|
|
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
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceFind(unsigned int vendor,
|
|
|
|
unsigned int product,
|
|
|
|
unsigned int bus,
|
|
|
|
unsigned int devno,
|
|
|
|
const char *vroot,
|
|
|
|
bool mandatory,
|
|
|
|
virUSBDevicePtr *usb)
|
2012-05-04 07:49:58 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListPtr list;
|
2012-05-04 07:49:58 +00:00
|
|
|
|
|
|
|
unsigned int flags = USB_DEVICE_FIND_BY_VENDOR|USB_DEVICE_FIND_BY_BUS;
|
2013-01-14 22:11:44 +00:00
|
|
|
if (!(list = virUSBDeviceSearch(vendor, product, bus, devno,
|
|
|
|
vroot, flags)))
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
2012-05-04 07:49:58 +00:00
|
|
|
|
|
|
|
if (list->count == 0) {
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2012-10-03 14:57:28 +00:00
|
|
|
if (!mandatory) {
|
2017-05-19 07:17:32 +00:00
|
|
|
VIR_DEBUG("Did not find USB device %04x:%04x bus:%u device:%u",
|
2012-10-03 14:57:28 +00:00
|
|
|
vendor, product, bus, devno);
|
|
|
|
if (usb)
|
|
|
|
*usb = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2017-05-19 07:17:32 +00:00
|
|
|
_("Did not find USB device %04x:%04x bus:%u device:%u"),
|
2012-05-04 07:49:58 +00:00
|
|
|
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) {
|
2013-01-14 22:11:44 +00:00
|
|
|
*usb = virUSBDeviceListGet(list, 0);
|
|
|
|
virUSBDeviceListSteal(list, *usb);
|
2012-10-03 14:57:28 +00:00
|
|
|
}
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2012-05-04 07:49:58 +00:00
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
return 0;
|
2012-05-04 07:49:58 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr
|
|
|
|
virUSBDeviceNew(unsigned int bus,
|
|
|
|
unsigned int devno,
|
|
|
|
const char *vroot)
|
2009-08-14 13:19:32 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr dev;
|
2009-08-14 13:19:32 +00:00
|
|
|
|
2013-07-04 10:17:18 +00:00
|
|
|
if (VIR_ALLOC(dev) < 0)
|
2009-08-14 13:19:32 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dev->bus = bus;
|
|
|
|
dev->dev = devno;
|
|
|
|
|
2019-11-13 13:53:42 +00:00
|
|
|
if (g_snprintf(dev->name, sizeof(dev->name), "%.3d:%.3d",
|
|
|
|
dev->bus, dev->dev) >= sizeof(dev->name)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2013-05-31 16:36:11 +00:00
|
|
|
_("dev->name buffer overflow: %.3d:%.3d"),
|
2011-06-22 20:52:32 +00:00
|
|
|
dev->bus, dev->dev);
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceFree(dev);
|
2011-06-22 20:52:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-01-24 09:26:07 +00:00
|
|
|
|
|
|
|
if (vroot) {
|
2019-10-22 13:26:14 +00:00
|
|
|
dev->path = g_strdup_printf("%s/%03d/%03d",
|
|
|
|
vroot, dev->bus, dev->dev);
|
2018-01-24 09:26:07 +00:00
|
|
|
} else {
|
2019-10-22 13:26:14 +00:00
|
|
|
dev->path = g_strdup_printf(USB_DEVFS "%03d/%03d",
|
|
|
|
dev->bus, dev->dev);
|
2011-06-22 20:52:32 +00:00
|
|
|
}
|
2009-08-14 13:19:32 +00:00
|
|
|
|
|
|
|
/* XXX fixme. this should be product/vendor */
|
2019-11-13 13:53:42 +00:00
|
|
|
if (g_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);
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceFree(dev);
|
2011-06-22 20:52:32 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2009-08-14 13:19:32 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: initialized", dev->id, dev->name);
|
|
|
|
|
|
|
|
return dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceFree(virUSBDevicePtr dev)
|
2009-08-14 13:19:32 +00:00
|
|
|
{
|
2013-01-08 12:42:26 +00:00
|
|
|
if (!dev)
|
|
|
|
return;
|
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);
|
2014-03-01 06:28:56 +00:00
|
|
|
VIR_FREE(dev->used_by_drvname);
|
|
|
|
VIR_FREE(dev->used_by_domname);
|
2009-08-14 13:19:32 +00:00
|
|
|
VIR_FREE(dev);
|
|
|
|
}
|
|
|
|
|
2014-03-01 06:28:56 +00:00
|
|
|
int
|
|
|
|
virUSBDeviceSetUsedBy(virUSBDevicePtr dev,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
2014-03-01 06:28:56 +00:00
|
|
|
VIR_FREE(dev->used_by_drvname);
|
|
|
|
VIR_FREE(dev->used_by_domname);
|
2019-10-20 11:49:46 +00:00
|
|
|
dev->used_by_drvname = g_strdup(drv_name);
|
|
|
|
dev->used_by_domname = g_strdup(dom_name);
|
2014-03-01 06:28:56 +00:00
|
|
|
|
|
|
|
return 0;
|
2011-12-21 17:58:29 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 06:28:56 +00:00
|
|
|
void
|
|
|
|
virUSBDeviceGetUsedBy(virUSBDevicePtr dev,
|
|
|
|
const char **drv_name,
|
|
|
|
const char **dom_name)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
2014-03-01 06:28:56 +00:00
|
|
|
*drv_name = dev->used_by_drvname;
|
|
|
|
*dom_name = dev->used_by_domname;
|
2011-12-21 17:58:29 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
const char *virUSBDeviceGetName(virUSBDevicePtr dev)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
|
|
|
return dev->name;
|
|
|
|
}
|
|
|
|
|
2016-11-16 14:26:59 +00:00
|
|
|
const char *virUSBDeviceGetPath(virUSBDevicePtr dev)
|
|
|
|
{
|
|
|
|
return dev->path;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
unsigned int virUSBDeviceGetBus(virUSBDevicePtr dev)
|
2010-03-04 11:48:16 +00:00
|
|
|
{
|
|
|
|
return dev->bus;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
unsigned int virUSBDeviceGetDevno(virUSBDevicePtr dev)
|
2010-03-04 11:48:16 +00:00
|
|
|
{
|
|
|
|
return dev->dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
int virUSBDeviceFileIterate(virUSBDevicePtr dev,
|
|
|
|
virUSBDeviceFileActor actor,
|
|
|
|
void *opaque)
|
2009-08-14 13:19:32 +00:00
|
|
|
{
|
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
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListPtr
|
|
|
|
virUSBDeviceListNew(void)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListPtr list;
|
2011-12-21 17:58:29 +00:00
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
if (virUSBInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(list = virObjectLockableNew(virUSBDeviceListClass)))
|
2011-12-21 17:58:29 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
static void
|
|
|
|
virUSBDeviceListDispose(void *obj)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
2013-01-16 11:49:54 +00:00
|
|
|
virUSBDeviceListPtr list = obj;
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2011-12-21 17:58:29 +00:00
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++)
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceFree(list->devs[i]);
|
2011-12-21 17:58:29 +00:00
|
|
|
|
|
|
|
VIR_FREE(list->devs);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListAdd(virUSBDeviceListPtr list,
|
2018-07-24 15:52:23 +00:00
|
|
|
virUSBDevicePtr *dev)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
2018-07-24 15:52:23 +00:00
|
|
|
if (virUSBDeviceListFind(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"),
|
2018-07-24 15:52:23 +00:00
|
|
|
(*dev)->name);
|
2011-12-21 17:58:29 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-07-24 15:52:23 +00:00
|
|
|
return VIR_APPEND_ELEMENT(list->devs, list->count, *dev);
|
2011-12-21 17:58:29 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr
|
|
|
|
virUSBDeviceListGet(virUSBDeviceListPtr list,
|
|
|
|
int idx)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
|
|
|
if (idx >= list->count ||
|
|
|
|
idx < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list->devs[idx];
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:44:27 +00:00
|
|
|
size_t
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListCount(virUSBDeviceListPtr list)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
|
|
|
return list->count;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr
|
|
|
|
virUSBDeviceListSteal(virUSBDeviceListPtr list,
|
|
|
|
virUSBDevicePtr dev)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr ret = NULL;
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2011-12-21 17:58:29 +00:00
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
2013-07-05 18:46:35 +00:00
|
|
|
if (list->devs[i]->bus == dev->bus &&
|
|
|
|
list->devs[i]->dev == dev->dev) {
|
|
|
|
ret = list->devs[i];
|
|
|
|
VIR_DELETE_ELEMENT(list->devs, i, list->count);
|
|
|
|
break;
|
2011-12-21 17:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListDel(virUSBDeviceListPtr list,
|
|
|
|
virUSBDevicePtr dev)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
2018-07-27 21:17:34 +00:00
|
|
|
virUSBDeviceFree(virUSBDeviceListSteal(list, dev));
|
2011-12-21 17:58:29 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr
|
|
|
|
virUSBDeviceListFind(virUSBDeviceListPtr list,
|
|
|
|
virUSBDevicePtr dev)
|
2011-12-21 17:58:29 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2011-12-21 17:58:29 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|