Tuesday, February 6, 2018

Raspberry PI - The Audio Engine - Part 9 - USB HUB Control

We're still on the way to collect this or that crumb that's been left on the table.
This exercise is for those who followed my advise to use an external network dongle. 
And for those who still enjoy playing around with our little gadget. 

Today we're looking at how to control our USB devices. 
As you probably already know the RPI Ethernet port is part of the PI USB infrastructure - The PI comes with a combined Ethernet-USB chip. IMO the weakest and most annoying part of the PI! 

A fellow programmer - Yutaka from Japan - has written a nice little tool to control USB-Hubs. 
It has been written in 2006 and - guess what - it still works.




What has all this do do with our Audio engine? 
We already turned off HDMI, Wlan, Bluetooth, LEDs and several processes earlier. 
The idea is to get rid of any potential unnecessary distraction to the audio job.

Now. Since we - at least some of you I guess - use a USB ethernet dongle, for our networking, we can physically switch off all the other ports. 
The onboard-ethernet port in particular.

The port-scheme follows below layout (port rearside of your PI):

####################
#            #          #         #
#            #    1    #    2   #
#            #          #         #
#   eth0  #############
#            #          #         #
#            #    4    #    3   #
#            #          #         #
####################

The tool has a slightly different port numbering. We gonna match it according to below
scheme:

# Hub:Port -- Controlled port(s) 
0:1 -- Controls the PI Ethernet port eth0
0:2 -- Controls all four PI-USB ports (not the Ethernet)
0:3 -- Controls PI-USB Port 4
0:4 -- Controls PI-USB Port 2
0:5 -- Controls PI-USB Port 3

You might have figured already, all ports can be controlled except PI-USB Port 1.
Great. This Port 1 is where we plug-in our ethernet usb dongle. 
Because then we just turn the rest off.

Now's your turn.

To be able to use this HUB tool we have to build it first. 
I assume that you built the squeezelite binary on the way here and that you still have the compiling environment installed.

If so, let's get started. If not install the customized squeezelite binary first, you don't want  to miss that!


As usual ssh login to your RPI as user tc. Then copy/paste following commands:

###############################
tce-load -wi libusb-compat-dev.tcz
cd /tmp
wget https://github.com/codazoda/hub-ctrl.c/archive/master.zip && unzip master.zip
cd ./hub-ctrl.c-master
gcc -o hub-ctrl hub-ctrl.c -lusb
sudo install -o tc -g staff -m 770 hub-ctrl /mnt/mmcblk0p2/tce




###############################

That'll take about 20 seconds.

That was an easy one. Wasn't it !?!?

To check if the hub-ctrl binary is at the right spot, type:

###############################
ls -l /mnt/mmcblk0p2/tce


###############################

Now we have to execute the hub-ctrl tool with the right options at one point in time. 
We do this, as usual, with our "tune.sh" tool, which is already running quite some actions 3 minutes after boot. We just add the hub-ctrl actions to it.

NOTE: If you run below without having an external ethernet dongle and that one not sticking in Port 1 - as indicated above - you'll loose your network connections! 

OK. I told you. Let's go ahead.

Copy/paste below. All text between the hashed-lines:

################################
cat <<'EOF' >>/mnt/mmcblk0p2/tce/tune.sh
# ETH0 = off
/mnt/mmcblk0p2/tce/hub-ctrl -h 0 -P 1 -p 0
# USB2
/mnt/mmcblk0p2/tce/hub-ctrl -h 0 -P 4 -p 0
# USB3
/mnt/mmcblk0p2/tce/hub-ctrl -h 0 -P 5 -p 0
# USB4
/mnt/mmcblk0p2/tce/hub-ctrl -h 0 -P 3 -p 0

EOF
sed -i  '/^\s*$/d' /mnt/mmcblk0p2/tce/tune.sh
sync


##################################

Now you can reboot or repower your PI.

Above commands simply turn all the ports off. 
For those of you who want to play with the hub-ctrl tool. Using e.g. a "-p 1" at the end of above listed commands would turn the respective ports on again.


Done. A 20 minute exercise. Congrats.


As usual make a backup of your SD-card!


Enjoy.




No comments:

Post a Comment