1 |
/* usbreset -- send a USB port reset to a USB device */ |
2 |
|
3 |
// http://marc.info/?l=linux-usb-users&m=116827193506484&w=2 |
4 |
|
5 |
/* |
6 |
|
7 |
There is a way to suspend a USB device. In order to use it, |
8 |
you must have a kernel with CONFIG_PM_SYSFS_DEPRECATED turned on. To |
9 |
suspend a device, do (as root): |
10 |
|
11 |
echo -n 2 >/sys/bus/usb/devices/.../power/state |
12 |
|
13 |
where the "..." is the ID for your device. To unsuspend, do the same |
14 |
thing but with a "0" instead of the "2" above. |
15 |
|
16 |
Note that this mechanism is slated to be removed from the kernel within |
17 |
the next year. Hopefully some other mechanism will take its place. |
18 |
|
19 |
> To reset a |
20 |
> device? |
21 |
|
22 |
Here's a program to do it. You invoke it as either |
23 |
|
24 |
usbreset /proc/bus/usb/BBB/DDD |
25 |
or |
26 |
usbreset /dev/usbB.D |
27 |
|
28 |
depending on how your system is set up, where BBB and DDD are the bus and |
29 |
device address numbers. |
30 |
|
31 |
Alan Stern |
32 |
|
33 |
*/ |
34 |
|
35 |
#include <stdio.h> |
36 |
#include <unistd.h> |
37 |
#include <fcntl.h> |
38 |
#include <errno.h> |
39 |
#include <sys/ioctl.h> |
40 |
|
41 |
#include <linux/usbdevice_fs.h> |
42 |
|
43 |
|
44 |
int main(int argc, char **argv) |
45 |
{ |
46 |
const char *filename; |
47 |
int fd; |
48 |
int rc; |
49 |
|
50 |
if (argc != 2) { |
51 |
fprintf(stderr, "Usage: usbreset device-filename\n"); |
52 |
return 1; |
53 |
} |
54 |
filename = argv[1]; |
55 |
|
56 |
fd = open(filename, O_WRONLY); |
57 |
if (fd < 0) { |
58 |
perror("Error opening output file"); |
59 |
return 1; |
60 |
} |
61 |
|
62 |
printf("Resetting USB device %s\n", filename); |
63 |
rc = ioctl(fd, USBDEVFS_RESET, 0); |
64 |
if (rc < 0) { |
65 |
perror("Error in ioctl"); |
66 |
return 1; |
67 |
} |
68 |
printf("Reset successful\n"); |
69 |
|
70 |
close(fd); |
71 |
return 0; |
72 |
} |