2009-01-16 00:50:44 -07:00
|
|
|
|
/*
|
2009-01-18 02:19:26 -07:00
|
|
|
|
** usbtool - simple tool to (dis)connect a usb devices from a kernel driver
|
|
|
|
|
** Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
|
2009-01-16 00:50:44 -07:00
|
|
|
|
**
|
|
|
|
|
** 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
|
|
|
|
|
** the Free Software Foundation, either version 3 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 General Public License for more details.
|
|
|
|
|
**
|
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
|
** along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2009-01-18 02:19:26 -07:00
|
|
|
|
#include <assert.h>
|
2009-01-16 00:50:44 -07:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <linux/usbdevice_fs.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
2009-01-16 02:55:24 -07:00
|
|
|
|
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]);
|
2009-01-18 02:19:26 -07:00
|
|
|
|
printf(" %s reconnect /dev/bus/usb/${BUS}/${DEV}\n", argv[0]);
|
2009-01-16 02:55:24 -07:00
|
|
|
|
printf("Disconnect or reconnect USB devices\n");
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-18 02:19:26 -07:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-16 00:50:44 -07:00
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
2009-01-16 02:55:24 -07:00
|
|
|
|
if (argc != 3)
|
2009-01-16 00:50:44 -07:00
|
|
|
|
{
|
2009-01-16 02:55:24 -07:00
|
|
|
|
print_usage(argc, argv);
|
2009-01-16 00:50:44 -07:00
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-01-18 02:19:26 -07:00
|
|
|
|
enum Command { CMD_CONNECT,
|
|
|
|
|
CMD_DISCONNECT,
|
|
|
|
|
CMD_RECONNECT
|
|
|
|
|
} cmd;
|
|
|
|
|
|
2009-01-16 02:55:24 -07:00
|
|
|
|
if (strcmp(argv[1], "connect") == 0)
|
|
|
|
|
{
|
2009-01-18 02:19:26 -07:00
|
|
|
|
cmd = CMD_CONNECT;
|
2009-01-16 02:55:24 -07:00
|
|
|
|
}
|
|
|
|
|
else if (strcmp(argv[1], "disconnect") == 0)
|
|
|
|
|
{
|
2009-01-18 02:19:26 -07:00
|
|
|
|
cmd = CMD_DISCONNECT;
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(argv[1], "reconnect") == 0)
|
|
|
|
|
{
|
|
|
|
|
cmd = CMD_RECONNECT;
|
2009-01-16 02:55:24 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
print_usage(argc, argv);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int fd = open(argv[2], O_RDWR);
|
2009-01-16 00:50:44 -07:00
|
|
|
|
|
|
|
|
|
if (fd < 0)
|
|
|
|
|
{
|
|
|
|
|
perror(argv[1]);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-01-18 02:19:26 -07:00
|
|
|
|
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;
|
2009-01-16 00:50:44 -07:00
|
|
|
|
|
2009-01-18 02:19:26 -07:00
|
|
|
|
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");
|
|
|
|
|
}
|
2009-01-16 00:50:44 -07:00
|
|
|
|
|
2009-01-18 02:19:26 -07:00
|
|
|
|
error_handling:
|
2009-01-16 00:50:44 -07:00
|
|
|
|
if (ret < 0)
|
|
|
|
|
{
|
2009-01-16 02:55:24 -07:00
|
|
|
|
printf("%s: Could not issue usb %s on %s: %s\n", argv[0], argv[1], argv[2], strerror(errno));
|
2009-01-16 00:50:44 -07:00
|
|
|
|
close(fd);
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-01-16 02:55:24 -07:00
|
|
|
|
printf("%s: %s of %s successful\n", argv[0], argv[1], argv[2]);
|
2009-01-16 00:50:44 -07:00
|
|
|
|
close(fd);
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* EOF */
|