Permalink
Browse files

Enable/Disable USB port -Linux -Specific devices

The idea of this script is, create a file with a list of devices by their "ID NUMBER" showed in lsusb and fixed to match the same name for Manual driver binding and unbinding as explained here: https://lwn.net/Articles/143397/

The script works and was implemented in production.
  • Loading branch information...
helfgott committed Sep 3, 2018
1 parent b4de9a8 commit dd8f9d2647b6f641b3f7c738992f97b2c256bfac
Showing with 55 additions and 0 deletions.
  1. +55 −0 enable_disable_usb_port_specific_device.sh
@@ -0,0 +1,55 @@
#!/bin/bash
# miguel.ortiz
# enable-disable specific port if a device from devices.list were connected
# ---------------------------------------enable this commented lines for logging (and at the end of the script)
#touch cheap_pinpad.log
#exec 1> >(tee -a cheap_pinpad.log) 2>&1
readarray DEVICES < devices.list
LOCK=pinpad.lock
BINDS='/sys/bus/usb/drivers/usb'
BINDF="$BINDS/bind"
UNBINDF="$BINDS/unbind"
# -----DEFINE PORT ------
if [ ! -f $LOCK ]; then
for device in "${DEVICES[@]}" ; do
PORTID=$(grep -l $device /sys/bus/usb/devices/*/uevent | tail -1)
if [ "$PORTID" == '' ]; then
:
else
PORTID=$(echo $PORTID | tr "/" " "|awk '{print $5}')
PORTID=$(echo ${PORTID::3} > pinpad.lock)
fi
done
fi
# ----- INTERACT -------
if [ -s "$LOCK" ]; then
DEVPORT=$(cat pinpad.lock)
else
echo 'ERROR: Device not registered correctly'
exit 1
fi
if [ "$1" == 'ON' ] && [ ! -e "$BINDS/$DEVPORT" ] ; then
$(echo -n $DEVPORT > $BINDF)
OK='PORT Enabled'
else :
ERROR='ERROR, port already enabled'
fi
if [ "$1" == 'OFF' ] && [ -e "$BINDS/$DEVPORT" ]; then
$(echo -n $DEVPORT > $UNBINDF)
OK='PORT Disabled'
else :
ERROR='ERROR, port already disabled'
fi
# ---------------------------------------enable this commented lines for logging
#echo $ERROR >> cheap_pinpad.log
#echo $OK >> cheap_pinpad.log
29,1 Final

0 comments on commit dd8f9d2

Please sign in to comment.