Merge ~ghadi-rahme/ubuntu/+source/powermgmt-base:ubuntu/devel into ubuntu/+source/powermgmt-base:ubuntu/devel

Proposed by Ghadi Rahme
Status: Needs review
Proposed branch: ~ghadi-rahme/ubuntu/+source/powermgmt-base:ubuntu/devel
Merge into: ubuntu/+source/powermgmt-base:ubuntu/devel
Diff against target: 62 lines (+36/-1)
2 files modified
debian/changelog (+7/-0)
on_ac_power (+29/-1)
Reviewer Review Type Date Requested Status
Simon Chopin (community) Needs Fixing
Ubuntu Sponsors Pending
git-ubuntu import Pending
Review via email: mp+466231@code.launchpad.net

Commit message

on_ac_power: Fix incorrect AC state on some machines with USB type-C ports (LP: #1980991)

Description of the change

On some machines, on_ac_power will incorrectly report the power state due to USB-C ports.

The current implementation of on_ac_power only checks if the USB-C port is in the connected state without checking first if any of the ports are running in sink or source mode.
With this patch, on_ac_power will first make sure that there is at least a USB port operating in sink mode before considering the USB* type as a possible power supply source.
If no source mode USB-C devices are found assume that there are USB-C ports in sink mode.

This implementation mimics what is already present in systemd with the systemd-ac-power command, where if a USB-C port in source mode is found, USB-C ports are ignored when determining the current power state.

The source code for the systemd implementation can be found here: https://github.com/systemd/systemd/blob/main/src/shared/battery-util.c#L12

To post a comment you must log in.
Revision history for this message
Simon Chopin (schopin) wrote :

Hi Ghadi,

Thanks a lot for taking this on.

It looks pretty solid, but I do have some questions and minor issues.
In addition to the diff comments, would you be OK splitting the changes to debian/changelog into its own commit? It makes it easier to maintain the delta further down the line when using git-ubuntu, see https://github.com/canonical/ubuntu-maintainers-handbook/blob/main/PackageMerging.md

Cheers,
Simon

review: Needs Fixing
Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

Thank you for the review Simon!

I will answer your question, follow your suggestions, fix the issues you pointed out and split the commits before uploading a new version of the patch.

Regards,
Ghadi

Revision history for this message
Simon Chopin (schopin) wrote :

Please request a new review from ubuntu-sponsors once you think you've addressed all my comments :)

Revision history for this message
Ghadi Rahme (ghadi-rahme) :
Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

> Please request a new review from ubuntu-sponsors once you think you've
> addressed all my comments :)

Sounds good, I will request a review now just to get some feedback on how to move forward with the line you had a question about. In case I should remove it or keep it. That way I can do all the changes at once.

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

I went ahead and addresses the changes requested, I also removed the extra line I had added to get around a kernel bug, I think this should be fixed in the kernel and not have a workaround in this package.
I also split the commits in two with the changelog being the top most.

Let me know how it looks!

Revision history for this message
Dan Bungert (dbungert) wrote :

Hi Ghadi, a few more suggestions, see inline comments.

Also, I suggest forwarding this to Debian - I believe that https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1025141 is the same issue as discussed here, forwarding a debdiff to that bug would be nice. Thank you.

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

Hello Dan, thank you for the feedback! I will forward it to debian as you recommended once I have the patch ready.

057e9d3... by Ghadi Rahme

on_ac_power: Fix incorrect AC state on some machines with USB type-C ports

35149b8... by Ghadi Rahme

d/changelog: Add changelog entry for LP: #1980991 fix

Revision history for this message
Ghadi Rahme (ghadi-rahme) wrote :

Hello Dan,

I have applied the changes requested and edited the file using vim with the config that Simon recommended. Let me know how it looks!

Unmerged commits

35149b8... by Ghadi Rahme

d/changelog: Add changelog entry for LP: #1980991 fix

057e9d3... by Ghadi Rahme

on_ac_power: Fix incorrect AC state on some machines with USB type-C ports

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/debian/changelog b/debian/changelog
2index 9394fc6..0d0c179 100644
3--- a/debian/changelog
4+++ b/debian/changelog
5@@ -1,3 +1,10 @@
6+powermgmt-base (1.37+nmu2) oracular; urgency=medium
7+
8+ * Fix on_ac_power incorrectly reporting AC state on some
9+ machines with USB type-C ports (LP: #1980991)
10+
11+ -- Ghadi Elie Rahme <ghadi.rahme@canonical.com> Tue, 30 Jul 2024 16:59:34 +0300
12+
13 powermgmt-base (1.37+nmu1) unstable; urgency=medium
14
15 * Non-maintainer upload.
16diff --git a/on_ac_power b/on_ac_power
17index 963e914..2e65fc0 100755
18--- a/on_ac_power
19+++ b/on_ac_power
20@@ -21,13 +21,41 @@ set -e
21 # no adapters are on-line but one or more are off-line, we return 1.
22 #
23 OFF_LINE_P=no
24+USB_IS_SINK=no
25+USB_IS_SOURCE=no
26+power_type="Mains"
27+
28+# USB-C sysfs documentation:
29+# https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-typec
30+#
31+# We verify first if there are any USB-C devices in power sink mode to
32+# check if we should consider USB as a power source. If no source ports
33+# are found assume they are sink ports.
34+#
35+if [ -d /sys/class/typec/ ]; then
36+ for FN in /sys/class/typec/*; do
37+ if test -d "${FN}" && test -r "${FN}/power_role"; then
38+ power_role="$(cat "${FN}/power_role")"
39+ if echo $power_role | grep -q '\[source\]'; then
40+ USB_IS_SOURCE=yes
41+ elif echo $power_role | grep -q '\[sink\]'; then
42+ USB_IS_SINK=yes
43+ fi
44+ fi
45+ done
46+ # Add USB* power type if any USB ports are sink or no source is found
47+ if [ "${USB_IS_SINK}" = "yes" ] || [ "${USB_IS_SOURCE}" = "no" ]; then
48+ power_type="USB*"
49+ fi
50+fi
51+
52
53 if [ -d /sys/class/power_supply/ ]; then
54 for FN in /sys/class/power_supply/*; do
55 if test -d "${FN}" && test -r "${FN}/type"; then
56 type="$(cat "${FN}/type")"
57 case "${type}" in
58- Mains|USB*|BrickID|Wireless)
59+ Mains|$power_type|BrickID|Wireless)
60 if [ -r "${FN}/online" ]; then
61 online="$(cat "${FN}/online")"
62 [ "$online" = 1 ] && exit 0

Subscribers

People subscribed via source and target branches