Solution of Ubuntu suspend errors

I have a Thinkpad T480 and use Ubuntu 18.04.

Recently, I occassionally encounter black screen problem when I tried to wake the computer up from suspend.

The error log is:

pci_pm_suspend(): hcd_pci_suspend+0x0/0x30 returns -16
dpm_run_callback(): pci_pm_suspend+0x0/0x130 returns -16
PM: Device 0000:00:14.0 failed to suspend async: error -16
PM: Some devices failed to suspend, or early wake event detected

I found the preliminary answer from here:
https://askubuntu.com/questions/1047229/sometimes-gnome-suspend-immediately-resumes-ubuntu-18-04-on-pure-intel-laptop/1047754#1047754

And according to that post, there is an extra link talking about the USB 3.0 constroller may cause the problem: https://gist.github.com/ioggstream/8f380d398aef989ac455b93b92d42048.

So I used the script in the above link. Its basic idea is to disable the USB 3.0 constroller when suspend and enable it after wakeup.

I only modified a little bit. I only want to modify the corresponding information not to change the whole file named wakeup. The following is my modified script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/bin/sh
#
# This script should prevent the following suspend errors
# which freezes the Dell Inspiron laptop.
#
# Put it in /usr/lib/systemd/system-sleep/xhci.sh
#
# The PCI 00:14.0 device is the usb xhci controller.
#
# kernel: [67445.560610] pci_pm_suspend(): hcd_pci_suspend+0x0/0x30 returns -16
# kernel: [67445.560619] dpm_run_callback(): pci_pm_suspend+0x0/0x150 returns -16
# kernel: [67445.560624] PM: Device 0000:00:14.0 failed to suspend async: error -16
# kernel: [67445.886961] PM: Some devices failed to suspend, or early wake event detected

if [ "${1}" == "pre" ]; then
# Do the thing you want before suspend here, e.g.:
echo "Disable broken xhci module before suspending at $(date)..." > /tmp/systemd_suspend_test
grep XHC.*enable /proc/acpi/wakeup && sed '/XHC/ s/enabled/disabled/' /proc/acpi/wakeup
# grep XHC.*enable /proc/acpi/wakeup && echo XHC > /proc/acpi/wakeup
elif [ "${1}" == "post" ]; then
# Do the thing you want after resume here, e.g.:
echo "Enable broken xhci module at wakeup from $(date)" >> /tmp/systemd_suspend_test
grep XHC.*disable /proc/acpi/wakeup && sed '/XHC/ s/disabled/enabled/' /proc/acpi/wakeup
# grep XHC.*disable /proc/acpi/wakeup && echo XHC > /proc/acpi/wakeup
fi

When I run it by

1
sudo ./xhci.sh

it reported error

./xhci.sh: 15: [: unexpected operator
./xhci.sh: 19: [: unexpected operator

However, when I change #!/bin/sh to #!/bin/bash, it is OK.

However, both cases, the /proc/acpi/wakeup file is not modified properly. I have no idea about the solution yet.