2011-12-25 13:59:17 +00:00
|
|
|
/* usbreset -- send a USB port reset to a USB device */
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
http://marc.info/?l=linux-usb-users&m=116827193506484&w=2
|
|
|
|
|
|
|
|
and needs mounted usbfs filesystem
|
|
|
|
|
|
|
|
sudo mount -t usbfs none /proc/bus/usb
|
|
|
|
|
2012-07-16 00:39:39 +00:00
|
|
|
There is a way to suspend a USB device. In order to use it,
|
|
|
|
you must have a kernel with CONFIG_PM_SYSFS_DEPRECATED turned on. To
|
2011-12-25 13:59:17 +00:00
|
|
|
suspend a device, do (as root):
|
|
|
|
|
|
|
|
echo -n 2 >/sys/bus/usb/devices/.../power/state
|
|
|
|
|
2012-07-16 00:39:39 +00:00
|
|
|
where the "..." is the ID for your device. To unsuspend, do the same
|
2011-12-25 13:59:17 +00:00
|
|
|
thing but with a "0" instead of the "2" above.
|
|
|
|
|
2012-07-16 00:39:39 +00:00
|
|
|
Note that this mechanism is slated to be removed from the kernel within
|
2011-12-25 13:59:17 +00:00
|
|
|
the next year. Hopefully some other mechanism will take its place.
|
|
|
|
|
|
|
|
> To reset a
|
|
|
|
> device?
|
|
|
|
|
|
|
|
Here's a program to do it. You invoke it as either
|
|
|
|
|
|
|
|
usbreset /proc/bus/usb/BBB/DDD
|
|
|
|
or
|
|
|
|
usbreset /dev/usbB.D
|
|
|
|
|
|
|
|
depending on how your system is set up, where BBB and DDD are the bus and
|
|
|
|
device address numbers.
|
|
|
|
|
|
|
|
Alan Stern
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-07-16 00:39:39 +00:00
|
|
|
#include <stdbool.h>
|
2011-12-25 13:59:17 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
2012-07-16 00:39:39 +00:00
|
|
|
#include <string.h>
|
2013-09-03 15:46:39 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <dirent.h>
|
2011-12-25 13:59:17 +00:00
|
|
|
#include <sys/ioctl.h>
|
2013-09-03 15:46:39 +00:00
|
|
|
#include <sys/types.h>
|
2011-12-25 13:59:17 +00:00
|
|
|
|
|
|
|
#include <linux/usbdevice_fs.h>
|
|
|
|
|
|
|
|
|
2012-07-16 00:39:39 +00:00
|
|
|
static char *usbfs = NULL;
|
|
|
|
|
|
|
|
struct usbentry {
|
|
|
|
int bus_num;
|
|
|
|
int dev_num;
|
|
|
|
int vendor_id;
|
|
|
|
int product_id;
|
|
|
|
char vendor_name[128];
|
|
|
|
char product_name[128];
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
static char *sysfs_attr(const char *dev, const char *attr)
|
2012-07-16 00:39:39 +00:00
|
|
|
{
|
2014-02-23 23:42:12 +00:00
|
|
|
int fd, len = 0;
|
2013-09-03 15:46:39 +00:00
|
|
|
char path[PATH_MAX];
|
|
|
|
static char buf[129];
|
2012-07-16 00:39:39 +00:00
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
snprintf(path, sizeof(path) - 1, "/sys/bus/usb/devices/%s/%s", dev, attr);
|
2012-07-16 00:39:39 +00:00
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
if ((fd = open(path, O_RDONLY)) >= 0)
|
2012-07-16 00:39:39 +00:00
|
|
|
{
|
2013-09-03 15:46:39 +00:00
|
|
|
len = read(fd, buf, sizeof(buf) - 1);
|
|
|
|
close(fd);
|
2012-07-16 00:39:39 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
while (--len > 0 && isspace(buf[len]))
|
|
|
|
buf[len] = 0;
|
2012-07-16 00:39:39 +00:00
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
return (len >= 0) ? buf : NULL;
|
2012-07-16 00:39:39 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
static struct usbentry * parse_devlist(DIR *d)
|
2012-07-16 00:39:39 +00:00
|
|
|
{
|
2013-09-03 15:46:39 +00:00
|
|
|
char *attr;
|
|
|
|
struct dirent *e;
|
2012-07-16 00:39:39 +00:00
|
|
|
static struct usbentry dev;
|
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
do {
|
|
|
|
e = readdir(d);
|
|
|
|
|
|
|
|
if (!e)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
while(!isdigit(e->d_name[0]) || strchr(e->d_name, ':'));
|
|
|
|
|
2012-07-16 00:39:39 +00:00
|
|
|
memset(&dev, 0, sizeof(dev));
|
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
if ((attr = sysfs_attr(e->d_name, "busnum")) != NULL)
|
|
|
|
dev.bus_num = strtoul(attr, NULL, 10);
|
2012-07-16 00:39:39 +00:00
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
if ((attr = sysfs_attr(e->d_name, "devnum")) != NULL)
|
|
|
|
dev.dev_num = strtoul(attr, NULL, 10);
|
2012-07-16 00:39:39 +00:00
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
if ((attr = sysfs_attr(e->d_name, "idVendor")) != NULL)
|
|
|
|
dev.vendor_id = strtoul(attr, NULL, 16);
|
2012-07-16 00:39:39 +00:00
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
if ((attr = sysfs_attr(e->d_name, "idProduct")) != NULL)
|
|
|
|
dev.product_id = strtoul(attr, NULL, 16);
|
2012-07-16 00:39:39 +00:00
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
if ((attr = sysfs_attr(e->d_name, "manufacturer")) != NULL)
|
|
|
|
strcpy(dev.vendor_name, attr);
|
|
|
|
|
|
|
|
if ((attr = sysfs_attr(e->d_name, "product")) != NULL)
|
|
|
|
strcpy(dev.product_name, attr);
|
|
|
|
|
|
|
|
if (dev.bus_num && dev.dev_num && dev.vendor_id && dev.product_id)
|
|
|
|
return &dev;
|
2012-07-16 00:39:39 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void list_devices(void)
|
|
|
|
{
|
2013-09-03 15:46:39 +00:00
|
|
|
DIR *devs = opendir("/sys/bus/usb/devices");
|
2012-07-16 00:39:39 +00:00
|
|
|
struct usbentry *dev;
|
|
|
|
|
|
|
|
if (!devs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while ((dev = parse_devlist(devs)) != NULL)
|
|
|
|
{
|
|
|
|
printf(" Number %03d/%03d ID %04x:%04x %s\n",
|
|
|
|
dev->bus_num, dev->dev_num,
|
|
|
|
dev->vendor_id, dev->product_id,
|
|
|
|
dev->product_name);
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
closedir(devs);
|
2012-07-16 00:39:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct usbentry * find_device(int *bus, int *dev,
|
|
|
|
int *vid, int *pid,
|
|
|
|
const char *product)
|
|
|
|
{
|
2013-09-03 15:46:39 +00:00
|
|
|
DIR *devs = opendir("/sys/bus/usb/devices");
|
2012-07-16 00:39:39 +00:00
|
|
|
|
|
|
|
struct usbentry *e, *match = NULL;
|
|
|
|
|
|
|
|
if (!devs)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while ((e = parse_devlist(devs)) != NULL)
|
|
|
|
{
|
|
|
|
if ((bus && (e->bus_num == *bus) && (e->dev_num == *dev)) ||
|
|
|
|
(vid && (e->vendor_id == *vid) && (e->product_id == *pid)) ||
|
|
|
|
(product && !strcasecmp(e->product_name, product)))
|
|
|
|
{
|
|
|
|
match = e;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
closedir(devs);
|
2012-07-16 00:39:39 +00:00
|
|
|
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void reset_device(struct usbentry *dev)
|
2011-12-25 13:59:17 +00:00
|
|
|
{
|
|
|
|
int fd;
|
2013-09-03 15:46:39 +00:00
|
|
|
char path[PATH_MAX];
|
2012-07-16 00:39:39 +00:00
|
|
|
|
2013-09-03 15:46:39 +00:00
|
|
|
snprintf(path, sizeof(path) - 1, "/dev/bus/usb/%03d/%03d",
|
|
|
|
dev->bus_num, dev->dev_num);
|
2012-07-16 00:39:39 +00:00
|
|
|
|
|
|
|
printf("Resetting %s ... ", dev->product_name);
|
|
|
|
|
|
|
|
if ((fd = open(path, O_WRONLY)) > -1)
|
|
|
|
{
|
|
|
|
if (ioctl(fd, USBDEVFS_RESET, 0) < 0)
|
|
|
|
printf("failed [%s]\n", strerror(errno));
|
|
|
|
else
|
|
|
|
printf("ok\n");
|
2011-12-25 13:59:17 +00:00
|
|
|
|
2012-07-16 00:39:39 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("can't open [%s]\n", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int id1, id2;
|
|
|
|
struct usbentry *dev;
|
|
|
|
|
|
|
|
if ((argc == 2) && (sscanf(argv[1], "%3d/%3d", &id1, &id2) == 2))
|
|
|
|
{
|
|
|
|
dev = find_device(&id1, &id2, NULL, NULL, NULL);
|
|
|
|
}
|
|
|
|
else if ((argc == 2) && (sscanf(argv[1], "%4x:%4x", &id1, &id2) == 2))
|
|
|
|
{
|
|
|
|
dev = find_device(NULL, NULL, &id1, &id2, NULL);
|
|
|
|
}
|
|
|
|
else if ((argc == 2) && strlen(argv[1]) < 128)
|
|
|
|
{
|
|
|
|
dev = find_device(NULL, NULL, NULL, NULL, argv[1]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Usage:\n"
|
|
|
|
" usbreset PPPP:VVVV - reset by product and vendor id\n"
|
|
|
|
" usbreset BBB/DDD - reset by bus and device number\n"
|
|
|
|
" usbreset \"Product\" - reset by product name\n\n"
|
|
|
|
"Devices:\n");
|
|
|
|
list_devices();
|
2011-12-25 13:59:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-16 00:39:39 +00:00
|
|
|
if (!dev)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "No such device found\n");
|
2011-12-25 13:59:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-16 00:39:39 +00:00
|
|
|
reset_device(dev);
|
2011-12-25 13:59:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|