Wednesday 16 January 2013

USB reset on Raspberry Pi

Source: http://marc.info/?l=linux-usb&m=121459435621262&w=2

NOTE: This C script was created by Alan Stern. I put it up here, so that I can easily access it. I have noticed that this is one of the most visited post on this blog, and I want to make sure that it's understood, that when I include the source of the information (as above) it is not my creation, and all credit goes to the scripts' creators.

vi usbreset.c

/* usbreset -- send a USB port reset to a USB device */
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main(int argc, char **argv)
{
const char *filename;
int fd;
int rc;
if (argc != 2) {
fprintf(stderr, "Usage: usbreset device-filename\n");
return 1;
}
filename = argv[1];
fd = open(filename, O_WRONLY);
if (fd < 0) {
perror("Error opening output file");
return 1;
}
printf("Resetting USB device %s\n", filename);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0) {
perror("Error in ioctl");
return 1;
}
printf("Reset successful\n");
close(fd);
return 0;
}


After saving the file: (source: http://askubuntu.com/questions/645/how-do-you-reset-a-usb-device-from-the-command-line )

cc usbreset.c -o usbreset
chmod +x usbreset

To manually reset a device:

lsusb (this gives you a list of usb devices. note the bus# and device#)

sudo ./usbreset /dev/bus/usb/<bus#>/<device#>
sudo ./usbreset /dev/bus/usb/<bus#>/<device#>


With gphoto2, this bash will reset the usb that the camera is attached to: (source: climberhunt's answer at http://www.raspberrypi.org/phpBB3/viewtopic.php?f=41&t=2276&sid=0621b22edb0b3864f73602797960fbf2 )

vi usbcamerareset.sh

#!/bin/bash
#
dev=`gphoto2 --auto-detect | grep usb | cut -b 36-42 | sed 's/,/\//'`
if [ -z ${dev} ]
then
echo "Error: Camera not found"
exit
fi
usbreset /dev/bus/usb/${dev}

Save the file, then:

chmod +x usbcamerareset.sh

No comments:

Post a Comment