You are not logged in.

#1 2024-11-26 09:06:01

SpaceMarine62
Member
Registered: 2024-11-26
Posts: 5

How to automatically reset my USB mouse and NIC after hibernation?

Hello.

I want to hibernate my Arch Linux machine when I'm not using it for a moment. I've set up everything needed for hibernation and hibernation works correctly.
Now I have a problem with 2 devices when resuming from hibernation. My USB mouse and my network interface for Ethernet internet.

After resuming from hibernation, my USB mouse and my network interface do not work at all.
I can only use my keyboard to navigate around my system and I do not have any network connection, neither to my local home network nor to the internet.

I found out that using these commands

sudo usbreset "USB OPTICAL MOUSE"
sudo ip link set enp25s0 down
sudo ip link set enp25s0 up

do the trick. After running these, my USB mouse and my NIC work again and I can use my machine as intended.

How do I make my computer run these commands automatically after resuming from hibernation?
Or is there a better solution to get these 2 devices work after resuming from hibernation?

I read the Arch Wiki page about hibernation (https://wiki.archlinux.org/title/Hibernation) but I couldn't find a section regarding handling certain devices not working after resuming from hibernation.
This problem only affects my USB mouse and NIC. All other devices, like my USB headset, USB webcam, printer, keyboard etc. work fine after resuming from hibernation.

I am fairly new to Arch Linux and I don't know where to ask this question so I thought the Newbie Corner is the right place. Please move it to the right place if needed.

Thank you.

Last edited by SpaceMarine62 (2024-11-27 07:10:07)

Offline

#2 2024-11-26 10:01:58

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,289

Re: How to automatically reset my USB mouse and NIC after hibernation?

Punching them into post hibernate hook file will probably be simplest: https://wiki.archlinux.org/title/Power_ … stem-sleep -- add a sleep for a few secs to ensure everything else init wise and has settled. you can drop the sudo since the files will be ran as root in that context anyway.

Last edited by V1del (2024-11-26 10:03:09)

Offline

#3 2024-11-26 10:17:16

SpaceMarine62
Member
Registered: 2024-11-26
Posts: 5

Re: How to automatically reset my USB mouse and NIC after hibernation?

V1del wrote:

Punching them into post hibernate hook file will probably be simplest: https://wiki.archlinux.org/title/Power_ … stem-sleep -- add a sleep for a few secs to ensure everything else init wise and has settled. you can drop the sudo since the files will be ran as root in that context anyway.

Yeah, I tried something similar by using a systemd unit. But I couldn't get it to work automatically.

Here is my systemd unit called "hibernation-resume.service":

[Unit]
Description=Reset USB mouse and network interface after hibernation resume
After=suspend.target

[Service]
Type=simple
ExecStartPre=/bin/sleep 5
ExecStart=/usr/local/bin/reset_usb_and_nic.sh
RemainAfterExit=true

[Install]
WantedBy=suspend.target

and here is my "reset_usb_and_nic.sh":

#!/bin/bash

usbreset "USB OPTICAL MOUSE"
ip link set enp25s0 down
ip link set enp25s0 up

It only works when I start the service manually with  "sudo systemctl start hibernation-resume.service" but it does not work automatically despite having the systemd unit enabled with "sudo systemctl enable hibernation-resume.service".

Last edited by SpaceMarine62 (2024-11-27 07:10:44)

Offline

#4 2024-11-26 10:26:28

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,289

Re: How to automatically reset my USB mouse and NIC after hibernation?

afaik suspend.target would be wrong, you want hibernate.target if this is explictly after hibernation (... or additionally so)

You should also use a type=oneshot instead and probably not a remainafterexit since that wouldn't retrigger it a second time potentially.

Offline

#5 2024-11-26 13:30:02

SpaceMarine62
Member
Registered: 2024-11-26
Posts: 5

Re: How to automatically reset my USB mouse and NIC after hibernation?

V1del wrote:

afaik suspend.target would be wrong, you want hibernate.target if this is explictly after hibernation (... or additionally so)

You should also use a type=oneshot instead and probably not a remainafterexit since that wouldn't retrigger it a second time potentially.

I tried "hibernate.target", "suspend.target" and "sleep.target" but nothing worked. I did "remainafterexit" intentionally because I thought maybe the script needed to remain running because it didn't work.

Since I had no success at all with the systemd service unit, I have now used your method by putting my script into "/usr/lib/systemd/system-sleep/reset_usb_and_nic.sh"

My script now looks like this:

#!/bin/sh

case $1/$2 in
  post/*)
    sleep 3
    usbreset "USB OPTICAL MOUSE"
    sleep 3
    ip link set enp25s0 down
    sleep 3
    ip link set enp25s0 up
    ;;
esac

I also followed your suggestion in putting in a sleep.

This made the problem with my USB mouse get resolved. Now when I resume from hibernation my mouse works as intended.
However, my network interface still wont work at all. Network Manager remains in a state where it is trying to get an IP address from my DHCP server forever, until I either reboot or run "sudo ip link set enp25s0 down" and "... up" manually.

Setting my machine's IP address statically makes Network Manager think my machine is connected to the internet... but it isn't. I can't visit any webpages and all "ping" commands fail with "temporary failure in name resolution". It remains this way until I either reboot or run the 2 "sudo ip link..." commands manually in my terminal again. I tried to put sleep between each command because I thought maybe the commands were executed too fast after each other, but that didn't help either.

So you helped me solve 1 device problem, thank you. Now I need to know how to solve the network interface problem after hibernation.

My network interface is built into my MSI B550 A Pro mainboard.

Last edited by SpaceMarine62 (2024-11-27 07:08:29)

Offline

#6 2024-11-26 15:40:15

SpaceMarine62
Member
Registered: 2024-11-26
Posts: 5

Re: How to automatically reset my USB mouse and NIC after hibernation?

Alright, after refining my script I actually managed to solve the network issue too.

Well... "refining" is a bit exaggerated. More like I got frustrated and thought "Maybe spamming the command will make it work" and it actually did.

This is how my "/usr/lib/systemd/system-sleep/reset_usb_and_nic.sh" looks now:

#!/bin/sh

case $1/$2 in
  post/*)
    sleep 1
    usbreset "USB OPTICAL MOUSE"
    sleep 1
    ip link set enp25s0 down
    sleep 1
    ip link set enp25s0 up
    sleep 1
    ip link set enp25s0 down
    sleep 1
    ip link set enp25s0 up
    ;;
esac

As you can see, nothing really changed except that I decreased the sleep times and I run each "ip link... down" and "... up" command twice for good measure.

But to my surprise, this actually works flawless. Now each time when I resume from hibernation, my network interface works fine.

I know that spamming commands until they work certainly isn't best practice, but unless somebody gives me a better solution to get my network interface working after hibernation, I see spamming commands until they work as the best practice I could come up with and actually works.

Anyways, thank you for your help. I appreciate it very much.

Last edited by SpaceMarine62 (2024-11-27 07:06:25)

Offline

#7 2024-11-26 18:55:07

seth
Member
Registered: 2012-09-03
Posts: 58,974

Re: How to automatically reset my USB mouse and NIC after hibernation?

Why don't you down the nic in the pre/*) case instead of in post/*)?

Offline

#8 2024-11-26 23:51:12

SpaceMarine62
Member
Registered: 2024-11-26
Posts: 5

Re: How to automatically reset my USB mouse and NIC after hibernation?

seth wrote:

Why don't you down the nic in the pre/*) case instead of in post/*)?

My first "design" of the script actually did that.

This was my first design of the script:

#!/bin/sh

case $1/$2 in
  pre/*)
    ip link set enp25s0 down
    sleep 3
    ;;
  post/*)
    sleep 3
    usbreset "USB OPTICAL MOUSE"
    sleep 3
    ip link set enp25s0 up
    ;;
esac

However, it didn't work. Even worse: When I resumed from hibernation, my network interface was no longer present in Network Manager at all,
and using "sudo ip link set enp25s0 up" returned an error saying "Cannot find device "enp25s0"" and there was no way for me to re-activate my network interface without a full reboot.

I don't know what is happening, but apparently setting my network interface to down before hibernation sort of... kicks my network interface completely out of the Linux kernel??? And a full reboot is required for my network interface to come back.

However, I appreciate your efforts to help me nonetheless. I just have to run everything in post/*) and brute force the "ip link..." command a couple of times to get my network interface working again after resuming from hibernation.
Currently, this is the only way I know that works. I am still open to suggestions for improvements, though.

Offline

#9 2024-11-27 07:47:12

seth
Member
Registered: 2012-09-03
Posts: 58,974

Re: How to automatically reset my USB mouse and NIC after hibernation?

I don't know what is happening

Please post a system journal covering such bogus S3 cycle, eg

sudo journalctl -b -1 | curl -F 'file=@-' 0x0.st

for the previous (-1) boot.

Offline

Board footer

Powered by FluxBB