Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

HOWTO: disable USB device during systemd sleep

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
Dobeedoo
Posts: 30
Joined: 2018-01-24 05:50

HOWTO: disable USB device during systemd sleep

#1 Post by Dobeedoo »

If you ever got annoyed over bumping your mouse just as your computer went to sleep, maybe this little script will make you happy. It could probably have been written more compact, nicer or different, but works for me. Feel free to modify and re-post here if you feel like sharing. :)

My computer sometimes complained about inhibitors when trying to sleep. This was fixed by editing '/etc/systemd/logind.conf' to ignore inhibitors on suspend. Now it enters sleep instantly, no hassle what-so-ever.

As always, read it through before use, usage is described in the script. You will have to modify the usbdevice string prior to running it, or it may have unforeseen consequences. No warranties of any kind, expressed and/or implied.


Enjoy!

Code: Select all

#!/bin/bash
#
# Disables a USB device during sleep. Here a wireless mouse. Keyboard wakes the computer.
#

# USAGE:
#
#	On Debian, put it in /lib/systemd/system-sleep/ as executable
#

echo -e "\nUncomment or remove this line when you have modified the "usbdevice" line below to reflect the device you want to control.\n" && exit 1

usbdevice="045e:07b2 Microsoft Corp."

distribution=`lsb_release -i | awk '{print $3}'`

if [ "$distribution" != "Debian" ]; then echo "This script has been tested on Debian only. You seem to be running on $distribution. You will have to verify correct behavior yourself. I will now exit."; exit 1; fi


# Simple logging of events (for debugging)
log=/tmp/SLEEP-LOG.txt
DATE=`date '+%Y-%m-%d %H:%M:%S'`
#touch $log

# Find a specific USB device
usbstring=`lsusb|grep $usbdevice`
bus=`echo $usbstring | awk '{print $2+0}'`
device=`echo $usbstring | awk '{print $4+0}'`


case $1 in
	pre)
		echo $bus:$device | tee /sys/bus/usb/drivers/usb/unbind
		echo "$DATE - $1: Disabling mouse [$bus:$device] while going to $2" >> $log
		;;
	post)
		echo $bus:$device | tee /sys/bus/usb/drivers/usb/bind
		echo "$DATE - $1 Enabling mouse [$bus:$device] after $2" >> $log
		;;
esac

User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: HOWTO: disable USB device during systemd sleep

#2 Post by bw123 »

Awesome tip!

I have been trying to figure out how to do this with ps/2 mouse and I see it has a similar entry in /sys/bus/serio/drivers/psmouse

I gave up because of the difficulty dealing with sysfs, so this is a nice example to try and figure out. I hate it when I bump the table or mouse and it wakes the machine up for nothing...
resigned by AI ChatGPT

Dobeedoo
Posts: 30
Joined: 2018-01-24 05:50

Re: HOWTO: disable USB device during systemd sleep

#3 Post by Dobeedoo »

Update: The previous method of using 'lsusb' proved to be unreliable. The script simply stopped working for me due to 'lsusb' reporting wrong bus:device information. Why that is, and maybe even more interesting, why it worked initially, I will leave up to someone else to find out. Because of this, the script now uses a slightly less exact matching to find the device to disconnect, but so far, more reliable - time will tell...

Note:
Before running it, edit this line

Code: Select all

raw_string=`lshw -C input -businfo | grep Microsoft`
to something useful to you. As is, it will probe the system for all 'input' devices, select the one named 'Microsoft'. My system only has one, which equals my mouse..

It should
* disable the USB mouse when the systemd-sleep "pre-sleep" event fires
* re-enable the USB mouse when the systemd-sleep "post-sleep" event fires

Code: Select all

#!/bin/bash
#
# Disables a USB device during sleep. Here a wireless Microsoft mouse. Keyboard wakes the computer.
#

# History:
#	2018-01		First version. Proved to be unreliable.
#	2018-01-27	Second version in an effort to increase reliability.
#

# USAGE:
#
#	On Debian, put it in /lib/systemd/system-sleep/ as executable
#

# Note:
#	Since lshw command cannot see disabled usb devices, a file, "current_device" is saved in ./ folder for restore to work.
#

distribution=`lsb_release -i | awk '{print $3}'`
if [ "$distribution" != "Debian" ]; then echo "This script has been tested on Debian only. You seem to be running on $distribution. You will have to verify correct behavior yourself. I will now exit."; exit 1; fi


# Simple logging of events (for debugging)
log=/tmp/SLEEP-LOG.txt
DATE=`date '+%Y-%m-%d %H:%M:%S'`
touch $log


# Find a specific HW device
raw_string=`lshw -C input -businfo | grep Microsoft`

# Extract USB bus:device numbers and convert to "n-n" format.
usb=`echo $raw_string | awk '{print $1}' | cut -d "@" -f2 | sed 's/\:/-/'`
if [ "$usb" ]; then
	echo $usb > ./current_device
fi
device=`cat ./current_device`

case $1 in
	pre)
		echo $device > /sys/bus/usb/drivers/usb/unbind
		echo "$DATE - $1: Disabling mouse [$device] while going to $2" >> $log
		;;
	post)
		echo $device > /sys/bus/usb/drivers/usb/bind
		echo "$DATE - $1 Enabling mouse [$device] after $2" >> $log
		;;
esac

Post Reply