NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Resetting usb device from terminal..

Sun Jul 14, 2013 2:34 pm

I am using a USB IR Toy v2 and using this excellent https://github.com/crleblanc/PyIrToy python library to get it working after failing to get things going with LIRC.
However, while it works once, repeated usage results in failure because of some sort of "buffer underrun" as detailed by the developer here https://github.com/crleblanc/PyIrToy/issues/2. Since the only solution seems is to perform a replug, can't we use a dirty hack of programatically reset (equivalent to manual unplugging and replugging) the /dev/ttyACM0 device.
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

User avatar
rpdom
Posts: 15212
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Resetting usb device from terminal..

Sun Jul 14, 2013 3:07 pm

Is there a kernel module (driver) that gets loaded when you plug the device in?

If so, you might be able to get away with unloading the driver and reloading it using the rmmod and modprobe or insmod commands.

NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Re: Resetting usb device from terminal..

Mon Jul 15, 2013 2:47 am

rpdom wrote:Is there a kernel module (driver) that gets loaded when you plug the device in?

If so, you might be able to get away with unloading the driver and reloading it using the rmmod and modprobe or insmod commands.
It shows as a CDC device attached to USB. I am not sure if any drivers get loaded, anyway to check this ?
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

User avatar
rpdom
Posts: 15212
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Resetting usb device from terminal..

Mon Jul 15, 2013 4:19 am

NewPi wrote:
rpdom wrote:Is there a kernel module (driver) that gets loaded when you plug the device in?

If so, you might be able to get away with unloading the driver and reloading it using the rmmod and modprobe or insmod commands.
It shows as a CDC device attached to USB. I am not sure if any drivers get loaded, anyway to check this ?
Do a lsmod before plugging it in, and again afterwards. Compare the two lists. Also there may be something in the output of dmesg when it has been plugged in.

NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Re: Resetting usb device from terminal..

Tue Jul 16, 2013 3:09 am

The list remains the same but the driver associated with this is cdc_acm :

Code: Select all

cdc_acm                15569  0
w1_therm                2987  0
w1_gpio                 1625  0
wire                   24845  2 w1_gpio,w1_therm
cn                      4794  1 wire
snd_bcm2835            16304  0
snd_pcm                77560  1 snd_bcm2835
snd_page_alloc          5145  1 snd_pcm
snd_seq                53329  0
snd_seq_device          6438  1 snd_seq
snd_timer              19998  2 snd_pcm,snd_seq
snd                    58447  5 snd_bcm2835,snd_timer,snd_pcm,snd_seq,snd_seq_device
leds_gpio               2235  0
led_class               3562  1 leds_gpio
The problem I am facing is that for some reasons the device goes into debug mode after some time of inactivity (or some other reason) and needs a replug to get working again. This is what I found using lsusb -v | less :
When working correctly :

Code: Select all

 
   Device Status:     0x0000
  (Bus Powered)
When NOT :

Code: Select all

  Device Status:     0x47d8
  (Bus Powered)
  Debug Mode
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

User avatar
rpdom
Posts: 15212
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Resetting usb device from terminal..

Tue Jul 16, 2013 5:54 am

Strange that the module appears to have been loaded before the device is connected, unless it is listed in /etc/modules or one of the files in /etc/modprobe.d/

So, what happens if it goes into debug mode and you do "sudo rmmod cdc_acm", wait a bit, and then do "sudo modprobe cdc_acm"? Does it cause the device to reset? I used to have to do something similar to the wifi on my old laptop when it lost connection.

NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Re: Resetting usb device from terminal..

Tue Jul 16, 2013 6:19 am

rmmod and modprobe resets the device. I am still waiting for IRToy to go into debug mode again to test this reset in that condition. Also,

Code: Select all

cat /sys/bus/usb/devices/1-1.3/power/*
shows

Code: Select all

1
as the result, can we toggle/control this somehow so the device does not go into suspend/debug mode ?
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

mba
Posts: 110
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark

Re: Resetting usb device from terminal..

Tue Jul 16, 2013 7:37 pm

Hi NewPi,

its a shame you gave up on IrToy and LIRC. I had it running for a while and it actually worked quite well. What was the problem? Maybe I can help somehow, I have some pretty detailed instructions on how I did it if you are interested?

Best regards
Martin B. Andersen
AMOTE - a LIRC client for Android. Build your own Android-based universal remote.

NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Re: Resetting usb device from terminal..

Wed Jul 17, 2013 5:04 am

mba wrote:its a shame you gave up on IrToy and LIRC. I had it running for a while and it actually worked quite well. What was the problem? Maybe I can help somehow, I have some pretty detailed instructions on how I did it if you are interested?
Hi Maritn,

I faced the issue as detailed here - http://dangerousprototypes.com/forum/vi ... 688#p53296

Now, why I gave up has two reasons :

A). It's should be a universal problem as the issue seems to how IRToy goes into debug mode and never truly unsuspends to a working state, as also addressed by the developer himself - http://dangerousprototypes.com/forum/vi ... 393#p53392

B). Why run LIRC service in background when you can simply use all the features provided by it when and as needed using python and the linux native "Rec-Play" binary.

Do you see any additional benefits of using LIRC ?

Thanks
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

mba
Posts: 110
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark

Re: Resetting usb device from terminal..

Wed Jul 17, 2013 7:07 am

The benefit of using LIRC (IMO) is that its a tested and proven software system with whole lot of predefined remotes you can pick and choose from and its not tied to a single hardware device. You can use IrToy for a while and if you want to go in another direction at one point you don't need to change your softwareplatform. I abandoned IrToy because I liked the simplicity of using a simple selfmade GPIO blaster and the LIRC gpio driver written by Ar0n. That said, I find IrToy to be a really nice gadget with an attractive pricetag.

Regarding your issue with LIRC and IrToy, I did not experience anything like this. I used the device for a couple of weeks, running LIRC on a Raspberry Pi 24/7 with a IrToy (v22) attached. But as its probably not a installation and configuration issue your are having I guess I can't be very helpful :-).

But if you come to the conclusion that you want to go in another direction I can only recommend using a GPIO based receiver/blaster. Its very easy to set up and it runs as stable as you could wish. I haven't had a single glitch, crash or anything else.

Br.
Martin
AMOTE - a LIRC client for Android. Build your own Android-based universal remote.

NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Re: Resetting usb device from terminal..

Wed Jul 17, 2013 7:38 am

A GPIO based receiver/blaster sounds a good idea, pls. tell details on how to get it done and how much it costed you ?
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

MattF
Posts: 55
Joined: Tue Feb 12, 2013 10:01 am

Re: Resetting usb device from terminal..

Wed Jul 17, 2013 10:14 am

Caveat:

I didn't write this code, I found it with google, but have long since lost the source/attribution:


#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
void main(int argc, char **argv)
{
const char *filename;
int fd;
filename = argv[1];
fd = open(filename, O_WRONLY);
ioctl(fd, USBDEVFS_RESET, 0);
close(fd);
return;
}


Compile this up, and it will reset a USB device.
The usage is usbreset /dev/bus/usb/XXX/YYY where XXX is the bus number (nearly always 001 on the Pi) and YYY is the device number (get both of these from lsusb).

Of course, there is always a caveat.
This works well if the device is plugged directly into a USB port on the Pi.

If it is connected to a powered hub, the power to the device will not be removed, which may or may not be sufficient to get a full reset to happen.

User avatar
rpdom
Posts: 15212
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Resetting usb device from terminal..

Wed Jul 17, 2013 10:56 am

MattF wrote:Of course, there is always a caveat.
This works well if the device is plugged directly into a USB port on the Pi.

If it is connected to a powered hub, the power to the device will not be removed, which may or may not be sufficient to get a full reset to happen.
Well, seeing as the power isn't removed when it is plugged directly into a USB port on the Pi, it should work just as well through a hub :)

A useful program, thanks!

mba
Posts: 110
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark

Re: Resetting usb device from terminal..

Fri Jul 19, 2013 6:11 pm

@NewPi these are my notes from when I got IrToy running, maybe you can find the patch files on google but if you fail just leave me a mail at [email protected] and I will send them to you:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
git clone git://lirc.git.sourceforge.net/gitroot/lirc/lirc

This will create a directory lirc in the current directory and populate it with the latest lirc source files.

cd to the lirc directory, apply the irtoy driver patches:

zcat 0001-Add-driver-for-USB-Infrared-Toy-in-sample-mode.patch.gz | patch -p1
zcat 0002-Add-USB-Infrared-Toy-driver-to-setup-system.patch.gz | patch -p1

./autogen.sh

make
sudo make install

sudo mkdir /etc/lirc
sudo cp lircd.conf /etc/lirc/

sudo cp contrib/lirc.debian /etc/init.d/lirc

nano /etc/init.d/lirc

replace start and restart with:

start-stop-daemon --start --quiet --exec /usr/local/sbin/lircd -- --listen --driver=usb_irtoy --device=/dev/ttyACM0

Then enable the initscript
sudo update-rc.d lirc defaults

the whole file should look like this (without the dashed line):
--------------------------------------------------------------------------------------------------------------------------
### BEGIN INIT INFO
# Provides: lirc
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO#! /bin/sh
if test ! -d /var/run/lirc; then
mkdir /var/run/lirc
fi

test -f /usr/local/sbin/lircd || exit 0
test -f /usr/local/sbin/lircmd || exit 0

case "$1" in
start)
echo -n "Starting lirc daemon: lircd"
start-stop-daemon --start --quiet --exec /usr/local/sbin/lircd -- --listen --driver=usb_irtoy --device=/dev/ttyACM0
echo -n " lircmd"
start-stop-daemon --start --quiet --exec /usr/local/sbin/lircmd
echo "."
;;
stop)
echo -n "Stopping lirc daemon: lircmd"
start-stop-daemon --stop --quiet --exec /usr/local/sbin/lircmd
echo -n " lircd"
start-stop-daemon --stop --quiet --exec /usr/local/sbin/lircd
echo "."
;;
reload|force-reload)
start-stop-daemon --stop --quiet --signal 1 --exec /usr/local/sbin/lircd
start-stop-daemon --stop --quiet --signal 1 --exec /usr/local/sbin/lircmd
;;
restart)
echo -n "Stopping lirc daemon: lircmd"
start-stop-daemon --stop --quiet --exec /usr/local/sbin/lircmd
echo " lircd"
start-stop-daemon --stop --quiet --exec /usr/local/sbin/lircd
sleep 1
echo -n "Starting lirc daemon: lircd"
start-stop-daemon --start --quiet --exec /usr/local/sbin/lircd -- --listen --driver=usb_irtoy --device=/dev/ttyACM0
echo -n " lircmd"
start-stop-daemon --start --quiet --exec /usr/local/sbin/lircmd
echo "."
;;
*)
echo "Usage: /etc/init.d/lircd {start|stop|reload|restart|force-reload}"
exit 1
esac

exit 0
--------------------------------------------------------------------------------------------------------------------------
AMOTE - a LIRC client for Android. Build your own Android-based universal remote.

NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Re: Resetting usb device from terminal..

Sat Jul 20, 2013 1:55 am

Thanks for posting the howto, this is exactly what I did earlier with the exception of the changes in the init.d script. As earlier it is not working, here is the lirc error log :

Code: Select all

Jul 20 07:21:04 NAS lircd: lircd(usb_irtoy) ready, using /var/run/lirc/lircd
Jul 20 07:21:04 NAS lircd: accepted new client on /var/run/lirc/lircd
Jul 20 07:21:09 NAS lircd: irtoy_getversion: couldn't read version
Jul 20 07:21:09 NAS lircd: please make sure you are using firmware v20 or higher
Jul 20 07:21:09 NAS lircd: usb_irtoy: No USB Irtoy device found at /dev/ttyACM0
Jul 20 07:21:39 NAS lircd: WARNING: Failed to initialize hardware
Jul 20 07:21:39 NAS lircd: select() failed
Jul 20 07:21:39 NAS lircd: Bad file descriptor
Jul 20 07:21:39 NAS lircd: removed client
Jul 20 07:23:07 NAS lircd: lircd(usb_irtoy) ready, using /var/run/lirc/lircd
Jul 20 07:23:07 NAS lircd: accepted new client on /var/run/lirc/lircd
Jul 20 07:23:07 NAS lircd: readlink() failed for "/dev/lirc"
Jul 20 07:23:07 NAS lircd: No such file or directory
Jul 20 07:23:07 NAS lircd: usb_irtoy: could not create lock files
Jul 20 07:23:07 NAS lircd: WARNING: Failed to initialize hardware
Jul 20 07:23:47 NAS lircd: accepted new client on /var/run/lirc/lircd
Jul 20 07:24:16 NAS lircd: removed client
When using irw nothing is shown on screen whereas IRToy led blinks showing data reception.
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

mba
Posts: 110
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark

Re: Resetting usb device from terminal..

Sat Jul 27, 2013 3:30 pm

I am sorry I can't be of any more help. I haven't experienced anything like that, I had it running for a couple of weeks. That should be plenty time to witness something like the problem you are seeing right? I works for a while and suddenly it spits out that error message, or doesn't it work at all?

Regarding using a GPIO blaster on Raspberry Pi I have written a small tutorial and description of a simple circuit (I am using myself) at http://www.datscharf.dk/amote under Tutorials and Hardware. The site is a work-in-progress, but my intention is to document many aspects of setting up LIRC, Raspberry Pi, blasting circuitry and so on.

Best regards
Martin
AMOTE - a LIRC client for Android. Build your own Android-based universal remote.

NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Re: Resetting usb device from terminal..

Sun Jul 28, 2013 3:22 am

If I compile LIRC with this patched lirc followed by install using make install it works fine and launches without any error. However, after a reboot the same command fails with following error :

Code: Select all

[email protected]:~# lircd --device=/dev/ttyACM0 --driver=usb_irtoy
lircd: can't open or create /var/run/lirc/lircd.pid
lircd: No such file or directory
[email protected]:~#
Any ideas what could be the reason for this ?
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

NewPi
Posts: 66
Joined: Sat Aug 18, 2012 2:52 pm

Re: Resetting usb device from terminal..

Sun Jul 28, 2013 6:03 am

NewPi wrote:If I compile LIRC with this patched lirc followed by install using make install it works fine and launches without any error. However, after a reboot the same command fails with following error :

Code: Select all

[email protected]:~# lircd --device=/dev/ttyACM0 --driver=usb_irtoy
lircd: can't open or create /var/run/lirc/lircd.pid
lircd: No such file or directory
[email protected]:~#
Any ideas what could be the reason for this ?
OK, worked out the solution on my own. Read here - http://unix.stackexchange.com/a/84693/43912
Raspberry Pi Howto, Tips, Tricks and Tools -> http://bit.ly/RPiTricks

mba
Posts: 110
Joined: Fri Jun 08, 2012 7:05 pm
Location: Denmark

Re: Resetting usb device from terminal..

Sun Jul 28, 2013 8:31 am

Thats great to hear! Do you mean it works now or does it still fail after a while with:
Jul 20 07:21:09 NAS lircd: irtoy_getversion: couldn't read version
Jul 20 07:21:09 NAS lircd: please make sure you are using firmware v20 or higher
Jul 20 07:21:09 NAS lircd: usb_irtoy: No USB Irtoy device found at /dev/ttyACM0
Jul 20 07:21:39 NAS lircd: WARNING: Failed to initialize hardware
Jul 20 07:21:39 NAS lircd: select() failed
Jul 20 07:21:39 NAS lircd: Bad file descriptor
Jul 20 07:21:39 NAS lircd: removed client
AMOTE - a LIRC client for Android. Build your own Android-based universal remote.

Return to “Advanced users”