Added reconnect command
This commit is contained in:
parent
77729257e3
commit
c8dcfe60fc
1 changed files with 48 additions and 14 deletions
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
** Xbox/Xbox360 USB Gamepad Userspace Driver
|
||||
** Copyright (C) 2008 Ingo Ruhnke <grumbel@gmx.de>
|
||||
** usbtool - simple tool to (dis)connect a usb devices from a kernel driver
|
||||
** Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
|
||||
**
|
||||
** 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
|
||||
|
@ -16,6 +16,7 @@
|
|||
** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
|
@ -31,9 +32,22 @@ void print_usage(int argc, char** argv)
|
|||
{
|
||||
printf("Usage: %s connect /dev/bus/usb/${BUS}/${DEV}\n", argv[0]);
|
||||
printf(" %s disconnect /dev/bus/usb/${BUS}/${DEV}\n", argv[0]);
|
||||
printf(" %s reconnect /dev/bus/usb/${BUS}/${DEV}\n", argv[0]);
|
||||
printf("Disconnect or reconnect USB devices\n");
|
||||
}
|
||||
|
||||
int send_usb_cmd(int fd, int cmd)
|
||||
{
|
||||
struct usbdevfs_ioctl command;
|
||||
|
||||
command.ifno = 0; // interface number, does it matter?
|
||||
|
||||
command.ioctl_code = cmd;
|
||||
command.data = NULL;
|
||||
|
||||
return ioctl(fd, USBDEVFS_IOCTL, &command);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 3)
|
||||
|
@ -43,14 +57,22 @@ int main(int argc, char** argv)
|
|||
}
|
||||
else
|
||||
{
|
||||
int connect;
|
||||
enum Command { CMD_CONNECT,
|
||||
CMD_DISCONNECT,
|
||||
CMD_RECONNECT
|
||||
} cmd;
|
||||
|
||||
if (strcmp(argv[1], "connect") == 0)
|
||||
{
|
||||
connect = 1;
|
||||
cmd = CMD_CONNECT;
|
||||
}
|
||||
else if (strcmp(argv[1], "disconnect") == 0)
|
||||
{
|
||||
connect = 0;
|
||||
cmd = CMD_DISCONNECT;
|
||||
}
|
||||
else if (strcmp(argv[1], "reconnect") == 0)
|
||||
{
|
||||
cmd = CMD_RECONNECT;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -67,17 +89,29 @@ int main(int argc, char** argv)
|
|||
}
|
||||
else
|
||||
{
|
||||
struct usbdevfs_ioctl command;
|
||||
int ret = 0;
|
||||
switch (cmd)
|
||||
{
|
||||
case CMD_CONNECT:
|
||||
ret = send_usb_cmd(fd, USBDEVFS_CONNECT);
|
||||
break;
|
||||
|
||||
case CMD_DISCONNECT:
|
||||
ret = send_usb_cmd(fd, USBDEVFS_DISCONNECT);
|
||||
break;
|
||||
|
||||
command.ifno = 0; // interface number, does it matter?
|
||||
if (connect)
|
||||
command.ioctl_code = USBDEVFS_CONNECT;
|
||||
else
|
||||
command.ioctl_code = USBDEVFS_DISCONNECT;
|
||||
command.data = NULL;
|
||||
|
||||
int ret = ioctl(fd, USBDEVFS_IOCTL, &command);
|
||||
case CMD_RECONNECT:
|
||||
ret = send_usb_cmd(fd, USBDEVFS_DISCONNECT);
|
||||
if (ret < 0) goto error_handling;
|
||||
sleep(1);
|
||||
ret = send_usb_cmd(fd, USBDEVFS_CONNECT);
|
||||
break;
|
||||
|
||||
default:
|
||||
assert(!"Never reached");
|
||||
}
|
||||
|
||||
error_handling:
|
||||
if (ret < 0)
|
||||
{
|
||||
printf("%s: Could not issue usb %s on %s: %s\n", argv[0], argv[1], argv[2], strerror(errno));
|
||||
|
|
Loading…
Reference in a new issue