Rajaram-Regupathy / libtypec

“libtypec” is aimed to provide a generic interface abstracting all platform complexity for user space to develop tools for efficient USB-C port management. The library can also enable development of diagnostic and debug tools to debug system issues around USB-C/USB PD topology.
34 stars 4 forks source link

typecstatus seems to ignore cable plugged into second port. #6

Open isomer opened 1 year ago

isomer commented 1 year ago

I have a Thinkpad T15 Gen 1 laptop, and typecstatus doesn't seem to detect the usb port correctly.

When plugging into the port used for charging there is no output:

$ ./typecstatus --all

When plugged into the "other" USB-C port:

$ ./typecstatus --all
USB-C Power Status
==================
        USB-C power contract Operating Power 15 W, with Max Power 135 W
        Charging System with TDP 64 W, with boost power requirement of 64 W

lstypec says:

$ ./lstypec 
lstypec 0.2 Session Info
  Using libtypec 0.2
   with Kernel 6.0.0-2-amd64

USB-C Platform Policy Manager Capability
  Number of Connectors: 2
  Number of Alternate Modes: 6
  USB Power Delivery Revision: 2.0
  USB Type-C Revision: 1.0

Connector 0 Capablity/Status
  Operation Mode: DRP
  Alternate Modes Supported:
  Local Mode 0:
    SVID: 0x17ef
    VDO: 0x00000001
  Local Mode 1:
    SVID: 0x8087
    VDO: 0x00000001
  Local Mode 2:
    SVID: 0xff01
    VDO: 0x001c1c47

Connector 1 Capablity/Status
  Operation Mode: DRP
  Alternate Modes Supported:
  Local Mode 0:
    SVID: 0x17ef
    VDO: 0x00000001
  Local Mode 1:
    SVID: 0x8087
    VDO: 0x00000001
  Local Mode 2:
    SVID: 0xff01
    VDO: 0x001c1c47

stracing typecstatus shows:

newfstatat(AT_FDCWD, "/sys/class/typec/port0", {st_mode=S_IFLNK|0777, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0
newfstatat(AT_FDCWD, "/sys/class/typec/port0/port0-partner", 0x7ffc106150d0, AT_SYMLINK_NOFOLLOW) = -1 ENOENT (No such file or directory)
newfstatat(AT_FDCWD, "/sys/class/power_supply/ucsi-source-psy-USBC000:001", {st_mode=S_IFLNK|0777, st_size=0, ...}, AT_SYMLINK_NOFOLLOW) = 0
openat(AT_FDCWD, "/sys/class/power_supply/ucsi-source-psy-USBC000:001/online", O_RDONLY) = 5
newfstatat(5, "", {st_mode=S_IFREG|0444, st_size=4096, ...}, AT_EMPTY_PATH) = 0
read(5, "0\n", 4096)                    = 2
close(5)                                = 0

This shows it probing /sys/class/typec/port0/port0-partner, but it doesn't seem to probe /sys/class/typec/port1/port1-partner despite it existing. (ie, the "main" power port on this laptop appears to be port1, not port0).

The code seems to suggest that port 0 is hard coded in the binary.

I would expect to see information about the usb-c port that is used for charging when running "typecstatus --all", at the moment I see no information at all.

Rajaram-Regupathy commented 1 year ago

Fixed pls confirm

isomer commented 1 year ago

(sorry for the delay, but I've finally got a spare evening to tinker)

Without a cable:

$ ./typecstatus --ro
USB-C Power Status
==================
Number of USB-C port(s): 2
======================
        No Power Contract on port 0
        No Power Contract on port 1
Detected 0 BB device(s)
=======================

In (only) the rearmost port (the port I use for charging):

$ ./typecstatus --ro
USB-C Power Status
==================
Number of USB-C port(s): 2
======================
        No Power Contract on port 0
        USB-C power contract Operating Power 15 W, with Max Power 65 W
        Charging System with TDP 191 W, with boost power requirement of 64 W
Detected 0 BB device(s)
=======================

Cable in (only) the foremost port (the "other" port):

USB-C Power Status
==================
Number of USB-C port(s): 2
======================
        USB-C power contract Operating Power 15 W, with Max Power 65 W
        Charging System with TDP 196 W, with boost power requirement of 64 W
        USB-C power contract Operating Power 15 W, with Max Power 65 W
        Charging System with TDP 196 W, with boost power requirement of 64 W
Detected 0 BB device(s)
=======================

I'm not sure why it's duplicating the results when it's in the foremost ("other") port, but it's definitely an improvement!

Rajaram-Regupathy commented 1 year ago

The duplicate result could be due to residue of previous connected in the sysfs path. Possible gap in the typec class driver of Linux. Could you please confirm "ls /sys/class/typec" when you see duplicate results ?

isomer commented 1 year ago

I don't think that's it.

No cable:

$ ./typecstatus --ro
USB-C Power Status
==================
Number of USB-C port(s): 2
======================
        No Power Contract on port 0
        No Power Contract on port 1
Detected 0 BB device(s)
=======================
$ grep . /sys/class/typec/*/* 2>/dev/null
/sys/class/typec/port0/data_role:host [device]
/sys/class/typec/port0/power_operation_mode:default
/sys/class/typec/port0/power_role:source [sink]
/sys/class/typec/port0/supported_accessory_modes:none
/sys/class/typec/port0/uevent:DEVTYPE=typec_port
/sys/class/typec/port0/uevent:TYPEC_PORT=port0
/sys/class/typec/port0/usb_power_delivery_revision:2.0
/sys/class/typec/port0/usb_typec_revision:1.0
/sys/class/typec/port0/vconn_source:no
/sys/class/typec/port0/waiting_for_supplier:0
/sys/class/typec/port1/data_role:host [device]
/sys/class/typec/port1/power_operation_mode:default
/sys/class/typec/port1/power_role:source [sink]
/sys/class/typec/port1/supported_accessory_modes:none
/sys/class/typec/port1/uevent:DEVTYPE=typec_port
/sys/class/typec/port1/uevent:TYPEC_PORT=port1
/sys/class/typec/port1/usb_power_delivery_revision:2.0
/sys/class/typec/port1/usb_typec_revision:1.0
/sys/class/typec/port1/vconn_source:no
/sys/class/typec/port1/waiting_for_supplier:0

Rear port:

$ ./typecstatus --ro
USB-C Power Status
==================
Number of USB-C port(s): 2
======================
        No Power Contract on port 0
        USB-C power contract Operating Power 15 W, with Max Power 65 W
        Charging System with TDP 200 W, with boost power requirement of 64 W
Detected 0 BB device(s)
=======================
$ grep . /sys/class/typec/*/* 2>/dev/null
/sys/class/typec/port0/data_role:host [device]
/sys/class/typec/port0/power_operation_mode:default
/sys/class/typec/port0/power_role:source [sink]
/sys/class/typec/port0/supported_accessory_modes:none
/sys/class/typec/port0/uevent:DEVTYPE=typec_port
/sys/class/typec/port0/uevent:TYPEC_PORT=port0
/sys/class/typec/port0/usb_power_delivery_revision:2.0
/sys/class/typec/port0/usb_typec_revision:1.0
/sys/class/typec/port0/vconn_source:no
/sys/class/typec/port0/waiting_for_supplier:0
/sys/class/typec/port1/data_role:host [device]
/sys/class/typec/port1-partner/accessory_mode:none
/sys/class/typec/port1-partner/supports_usb_power_delivery:yes
/sys/class/typec/port1-partner/uevent:DEVTYPE=typec_partner
/sys/class/typec/port1-partner/usb_power_delivery_revision:0.0
/sys/class/typec/port1/power_operation_mode:usb_power_delivery
/sys/class/typec/port1/power_role:source [sink]
/sys/class/typec/port1/supported_accessory_modes:none
/sys/class/typec/port1/uevent:DEVTYPE=typec_port
/sys/class/typec/port1/uevent:TYPEC_PORT=port1
/sys/class/typec/port1/usb_power_delivery_revision:2.0
/sys/class/typec/port1/usb_typec_revision:1.0
/sys/class/typec/port1/vconn_source:no
/sys/class/typec/port1/waiting_for_supplier:0

No cable (again - showing no stale state):

$ ./typecstatus --ro
USB-C Power Status
==================
Number of USB-C port(s): 2
======================
        No Power Contract on port 0
        No Power Contract on port 1
Detected 0 BB device(s)
=======================
$ grep . /sys/class/typec/*/* 2>/dev/null
/sys/class/typec/port0/data_role:host [device]
/sys/class/typec/port0/power_operation_mode:default
/sys/class/typec/port0/power_role:source [sink]
/sys/class/typec/port0/supported_accessory_modes:none
/sys/class/typec/port0/uevent:DEVTYPE=typec_port
/sys/class/typec/port0/uevent:TYPEC_PORT=port0
/sys/class/typec/port0/usb_power_delivery_revision:2.0
/sys/class/typec/port0/usb_typec_revision:1.0
/sys/class/typec/port0/vconn_source:no
/sys/class/typec/port0/waiting_for_supplier:0
/sys/class/typec/port1/data_role:host [device]
/sys/class/typec/port1/power_operation_mode:default
/sys/class/typec/port1/power_role:source [sink]
/sys/class/typec/port1/supported_accessory_modes:none
/sys/class/typec/port1/uevent:DEVTYPE=typec_port
/sys/class/typec/port1/uevent:TYPEC_PORT=port1
/sys/class/typec/port1/usb_power_delivery_revision:2.0
/sys/class/typec/port1/usb_typec_revision:1.0
/sys/class/typec/port1/vconn_source:no
/sys/class/typec/port1/waiting_for_supplier:0

Foremost port:

$ ./typecstatus --ro
USB-C Power Status
==================
Number of USB-C port(s): 2
======================
        USB-C power contract Operating Power 15 W, with Max Power 65 W
        Charging System with TDP 200 W, with boost power requirement of 64 W
        USB-C power contract Operating Power 15 W, with Max Power 65 W
        Charging System with TDP 200 W, with boost power requirement of 64 W
Detected 0 BB device(s)
=======================
$ grep . /sys/class/typec/*/* 2>/dev/null
/sys/class/typec/port0/data_role:host [device]
/sys/class/typec/port0-partner/accessory_mode:none
/sys/class/typec/port0-partner/supports_usb_power_delivery:yes
/sys/class/typec/port0-partner/uevent:DEVTYPE=typec_partner
/sys/class/typec/port0-partner/usb_power_delivery_revision:0.0
/sys/class/typec/port0/power_operation_mode:usb_power_delivery
/sys/class/typec/port0/power_role:source [sink]
/sys/class/typec/port0/supported_accessory_modes:none
/sys/class/typec/port0/uevent:DEVTYPE=typec_port
/sys/class/typec/port0/uevent:TYPEC_PORT=port0
/sys/class/typec/port0/usb_power_delivery_revision:2.0
/sys/class/typec/port0/usb_typec_revision:1.0
/sys/class/typec/port0/vconn_source:no
/sys/class/typec/port0/waiting_for_supplier:0
/sys/class/typec/port1/data_role:host [device]
/sys/class/typec/port1/power_operation_mode:default
/sys/class/typec/port1/power_role:source [sink]
/sys/class/typec/port1/supported_accessory_modes:none
/sys/class/typec/port1/uevent:DEVTYPE=typec_port
/sys/class/typec/port1/uevent:TYPEC_PORT=port1
/sys/class/typec/port1/usb_power_delivery_revision:2.0
/sys/class/typec/port1/usb_typec_revision:1.0
/sys/class/typec/port1/vconn_source:no
/sys/class/typec/port1/waiting_for_supplier:0