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