Turning off a single usb device... again - usb

I know that this topic has been discussed many times, but none of the answers helped me. For the record, i'm running Debian.
The deal is: I bought an usb powered led lamp, which is very simple and doesn't even have an on/off switch (it works and is always on). I want to be able to turn it on/off via command line. Here's what i tried:
echo on > /sys/bus/usb/devices/usb1/power/level # turn on
echo suspend > /sys/bus/usb/devices/usb1/power/level # turn off
which is what i've found on many forums. Turning "on" works, but "suspending" yields
-su: echo: write error: Invalid argument
for every usbN. I also tried
echo "0" > "/sys/bus/usb/devices/usbX/power/autosuspend_delay_ms"
which doesn't give an error, but also doesn't do anything (again, for every usbN)
trying
echo "usb1" > /sys/bus/usb/drivers/usb/unbind
works only for more "inteligent" devices, like the keyboard, the mouse, or the usb wifi card. What i mean is that only tyhose devices are turned off, other usbN don't give an error, but the lamp never goes off.
the contents of /sys/bus/usb/devices/ are
1-0:1.0 1-1:1.0 1-2:1.0 1-2:1.2 2-0:1.0 4-0:1.0 4-1:1.0 6-0:1.0 8-0:1.0 8-2:1.0 usb2 usb4 usb6 usb8
1-1 1-2 1-2:1.1 1-2:1.3 3-0:1.0 4-1 5-0:1.0 7-0:1.0 8-2 usb1 usb3 usb5 usb7
i tried to do
echo device_name > /sys/bus/usb/drivers/usb/unbind
with every single one of them, but only the devices usbN and N-M react, the ones of the form n-m:x.y yield
tee: /sys/bus/usb/drivers/usb/bind: No such device
(i tried putting in, for instance, "1-0:1.0", "1-0\:1.0" and "1-0\:1.0", all gave the same result).
One last thing, what is shown after executing
lsusb -t
does not change when i plug or unplug the lamp.
Any ideas?

Turn off device ID 2-1:
echo '2-1' |sudo tee /sys/bus/usb/drivers/usb/unbind
Turn device ID 2-1 back on:
echo '2-1' |sudo tee /sys/bus/usb/drivers/usb/bind
In my case, using device ID 2-1 controls power to my usb stick, and as a consequence controls the light.
TIP: If they work for you in Debian, create an alias for them to make life easier for you later.
Hope this helps,
Su

If all you want to do is reset a USB device to fix it once it gets into a broken state, then using the bind/unbind usbfs special files can be a bit of a pain (since device IDs can change, and they're a bit tricky to identify precisely if you don't want to rebind other devices). In this case I've found it much easier to use the vendor and product IDs given by lsusb with usb_modeswitch. For example, if I identify my wireless adapter using:
$ lsusb
Bus 001 Device 042: ID 7392:7811 Edimax Technology Co., Ltd EW-7811Un 802.11n Wireless Adapter [Realtek RTL8188CUS]
Bus 001 Device 035: ID 0409:005a NEC Corp. HighSpeed Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
I can then reset the wireless adapter using:
$ sudo usb_modeswitch -v 0x7392 -p 0x7811 --reset-usb
If you have more than one device attached with the same vendor and product IDs then usb_modeswitch provides bus and device number flags. For the wireless adapter example above I'd add -b 1 -g 42 to the flags.

Try this code it works for me (Only for rooted)
String[] cmdline = { "su", "-c", "echo '1-1' >/sys/bus/usb/drivers/usb/unbind" };
try {
Runtime.getRuntime().exec(cmdline);
} catch (IOException e) {
Log.e("MainActivity","Failed"+e);
}
and for bind again do this
String[] cmdline = { "su", "-c", "echo '1-1' >/sys/bus/usb/drivers/usb/bind" };
try {
Runtime.getRuntime().exec(cmdline);
} catch (IOException e) {
Log.e("MainActivity","Failed"+e);
}

Related

Connect to connman service using the first field rather than the second

Given the following table
$ connmanctl services
*AO MyNetwork wifi_dc85de828967_68756773616d_managed_psk
OtherNET wifi_dc85de828967_38303944616e69656c73_managed_psk
AnotherOne wifi_dc85de828967_3257495245363836_managed_wep
FourthNetwork wifi_dc85de828967_4d7572706879_managed_wep
AnOpenNetwork wifi_dc85de828967_4d6568657272696e_managed_none
I'd like to be able to connect to a network, e.g. OtherNET, using the string OtherNET rather than the long wifi_dc85de828967_38303944616e69656c73_managed_psk, as I don't want to count the times I press Tab and/or check that the wifi_ line in the prompt corresponds to the intended network.
Is this possible with connman only? Or do I really have to write a wrapper myself?
The man page of connmanctl contains
services
Shows a list of all available services. This includes the
nearby wifi networks, the wired ethernet connections, blue‐
tooth devices, etc. An asterisk in front of the service
indicates that the service has been connected before.
and
connect service
Connects to the given service. Some services need a so-
called provisioning file in order to connect to them, see
connman-service.config(5).
which both don't say much about the format of the output or the use of the command.
Similarly, the wiki on Arch Linux refers to the last column as the second field beginning with wifi_.
Since nobody has answered yet, I have found some spare time to code the following wrapper, which basically does the following
keeps reading as long as the input is not exit;
when an input is provided which starts with connect, the following word is used to pattern-match one line (only the first matching line) from connman services; the last field of this line (the one which starts with wifi_) is forwarded to connmanctl connect;
any other non-empty input is forwarded to connmanctl as it is.
a certain number (to be passed through the variable NOINPUTS_BEFORE_EXIT whose default is 3) of empty inputs causes the script to exit.
The script is the following
#!/usr/bin/env bash
name=$(basename $0)
noinputs_before_exit=${NOINPUTS_BEFORE_EXIT:=3}
while [[ $cmd != 'exit' ]]; do
echo -n "$name " 1>&2
read cmd
if [[ -z "$cmd" ]]; then
(( --noinputs_before_exit == 0 )) && exit
else
noinputs_before_exit=$NOINPUTS_BEFORE_EXIT
if [[ $cmd =~ ^connect\ ]]; then
connmanctl connect $(connmanctl services | awk '/'"${cmd#* }"'/ { print $NF; exit }')
else
connmanctl $cmd
fi
fi
done
The script limitations are at least the following:
(less importantly, to me) I have no idea whether it meets any security requirements;
it does not allow Tab-completion;
(most importantly, to me) it does not use the readline library, so line editing impossible.

Configuring MPD with an 5.1 external surround sound card

I've read tons of threads about this, but i cant figure it out.
So i have a MPD server running on Raspbian, with an external sound card attached to it. Music works fine trough mpd with the 2 channels, but not for 6 channels.
Also i am confused what is the task of all programs involved.
So there's alsa. I Have tried to change the /etc/asound.conf to many different configurations. Like:
pcm.!default {
type route
slave.pcm surround51
slave.channels 6
ttable.0.0 1
ttable.1.1 1
ttable.0.2 1
ttable.1.3 1
ttable.0.5 0.5
ttable.1.5 0.5
}
But it did not work. My current settings are:
pcm.!default {
type hw
card 2
device 0
}
ctl.!default {
type hw
card 2 #If you want to set HDMI as output ,turn 0 to 1.
}
Then there is pulse audio. I am not sure if the function of both programs overlap, or if you should use them together. To the pulseaudio config file /etc/pulse/daemon.conf i have edited the following line:
default-sample-channels = 6
Furthermore i have added the following line to /etc/pulse/default.pa
set-card-profile 2 output:analog-surround-51
load-module module-native-protocol-tcp auth-ip-acl=127.0.0.1 # IP of l$
But i am not sure it has any effect at all.
So last there is mpd configuration which I had configure at ~/.mpd/mpd.conf
audio_output {
type "alsa"
name "My ALSA Device"
mixer_type "software"
mixer_control "PCM"
}
If i change alsa to pulse i cant hear sound. Furthermore, the command speaker-test -c 6 -t wav
IS working for 6 channels. Since mpd is configured with alsa, and speaker-test does work, i guess that it is pulseaudio that creates the surround sound. But how can i configure MPD to work with surround sound?
Does anyone have any idea? Any help is much appreciated!
Here is the ouput of aplay -l
xcb_connection_has_error() returned true
card 0: sunxicodec [sunxi-CODEC], device 0: M1 PCM [sunxi PCM]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: sunxisndhdmi [sunxi-sndhdmi], device 0: SUNXI-HDMIAUDIO sndhdmi-0 []
Subdevices: 1/1
Subdevice #0: subdevice #0
card 2: Device [USB Sound Device], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
and
Update:
If i play sound as the mpd user it also works with surround sound
sudo -u mpd speaker-test -c 6 -t wav
Eventually this worked for me:
pcm.ch51 {
slave sl2
#slave.channels 6
type route
ttable.0.0 1
ttable.1.1 1
ttable.0.2 1
ttable.1.3 1
ttable.0.4 0.5
ttable.1.4 0.5
ttable.0.5 0.5
ttable.1.5 0.5
}
pcm_slave.sl2 {
channels 6
pcm "hw:1,0"
}
In ~/.asoundrc. The table thing duplicates sound to other speakers so all 5+1 speakers are used. The pcm "hw:1,0" selects my external usb sound card.
Additionally in ~/.mpd/mpd.conf
audio_output {
type "alsa"
name "My ALSA Device"
channels "6"
device "ch51"
You select the device specified in asound.rc

In Bluez A2DP: how can I modify the default audio sample rate

I am using Bluez4 to sink Audio from an iphone 5 to a Raspberry pi audio output.
The default settings for BLuez 4 A2DP appear to be S16_LE, 44,1kHz Stereo.
Similar to other posts about Bluez, I can't catch Select_Configuration DBus messages in order to change the sample rate dynamically. Instead I decided to try to find the default A2DP sample rate in the BLuez Stack.
Does anyone know where the default sample rate is set? My first thought was that it was in the BLuez/audio/ folder but nothing appears to change the default 44.1kHz sample rate.
Now I'm very curious to know where it is set.
Currently using this: sudo ./a2dp-alsa --sink | aplay -c 2 -r 44100 -f S16
would like to use this sudo ./a2dp-alsa --sink | aplay -c 2 -r 16000 -f S16
I came across these lines in a2dp-alsa.c
/* Initialise connection to ALSA */
g_handle = audio_init("hw:0,0", 48000);
maybe its hard coded in a2dp-alsa - not parameterizable

Setting DTR high, RTS low using Linux bash?

I have a serial device that has no flow control, but is powered from the RS232 port by holding the RTS high and DTR low
I was hoping to read from this device using a simple bash script, but can't find any way to set the handshaking lines, using stty or otherwise, to allow for the above configuration.
Any ideas if this is possible?
I don't have an answer about setting RTS without touching DTR, because I don't have any DTR pin on my dongle; but, trying to set RTS was already very tricky un pure shell.
You may need to play with stty crtscts and clocal flags.
I have published a detailed answer here:
https://forums.gentoo.org/viewtopic-p-8132756.html#8132756
Here is the short version:
#!/bin/bash
MySerialPort="/dev/ttyUSB0"
MyLatency="2"
echo "#include <fcntl.h>
#include <sys/ioctl.h>
main()
{ int fd; fd = open(\"${MySerialPort}\",O_RDWR | O_NOCTTY );
int RTS_flag; RTS_flag = TIOCM_RTS;
ioctl(fd,TIOCMBIS,&RTS_flag);
sleep (${MyLatency});
ioctl(fd,TIOCMBIC,&RTS_flag);
close(fd); } " | tcc -run -
Note that sending data on TX will probably mess RTS; see the Gentoo forum for details.
I've been trying something similar here.
I used the ioctls. Then measured with a multimeter.
this is what I found:
dsrv=TIOCM_DTR;//this sets RTS to -11.7?
dsrv=TIOCM_RTS;//sets DTR -11.7
ioctl(fd, TIOCMBIS, &dsrv);
dsrv=TIOCM_DTR;//this sets RTS to 11.7?
dsrv=TIOCM_RTS;//sets DTR 11.7
ioctl(fd, TIOCMBIC, &dsrv);
It is somewhat weird...
socat controls DTR when hucpcl=1
(Atmel EDBG USB Serial needs DTR high)
sleep 1; while true; do echo -en "\x01\x02"; sleep 0.1; done | socat -T1 -t1 - /dev/ttyUSB0,hupcl=1,raw,b1000000,cs8,echo=0
man socat - may be find more solutions for your serial problem.

remove usb node

My aim is to disable usb port usage except a specific kind of usb. Every time a usb device is inserted a udev rule is called and it runs a program to handle the work.
I can't unload usb_storage module since it is needed for usage, so how can i remove usb dev link if it doesn't meet my case?
unbind the device. Pass vendor id and product id to below script.
VENDOR=$1
PRODUCT=$2
if [ n$VENDOR = n ] || [ n$PRODUCT = n ]; then
echo "Unbinding the first non-hub device..."
ID=`grep -l "^0$" /sys/bus/usb/drivers/usb/*/maxchild | sed -e "s|/sys/bus/usb/drivers/usb/\(.*\)/maxchild|\1|" | head -n 1`
VENDOR=`cat /sys/bus/usb/drivers/usb/$ID/idVendor`
PRODUCT=`cat /sys/bus/usb/drivers/usb/$ID/idProduct`
echo $ID > /sys/bus/usb/drivers/usb/unbind
echo "Device found ($VENDOR:$PRODUCT), and unbound!"
exit 0
fi
Regards,
Barun Parichha