13

Say I have a laptop and a powerbank (or two laptops) with USB type C connected together, then how is it decided which charges which?

Is it controlled by the driver? Does Linux kernel supports such control?

Update:

I see this patch set (currently v14) but it's not in mainline yet.

https://lwn.net/Articles/710741/

But something called UCSI is in 4.10 already:

http://lxr.free-electrons.com/source/drivers/usb/misc/Kconfig

270 config UCSI
271         tristate "USB Type-C Connector System Software Interface driver"
272         depends on ACPI
273         help
274           UCSI driver is meant to be used as a convenience tool for desktop and
275           server systems that are not equipped to handle USB in device mode. It
276           will always select USB host role for the USB Type-C ports on systems
277           that provide UCSI interface.

So is it correct to say that the Linux kernel driver for this problem is work in progress as of 4.10?

2
  • 2
    I know my android phone gives me the option to choose "Charge this device" or "Supply Power" so my assumption is it can be controlled by the driver/OS.
    – heavyd
    Mar 17, 2017 at 21:10
  • 1
    Good question. And it looks like this is ripe for a userspace/contrib UI to be written by the community. Would be nice to have a UI like Android has to control the flow of power. Oct 25, 2022 at 21:08

3 Answers 3

9

In Linux you can control the direction of power on a given port by writing "source" or "sink" to the appropriate sysfs control, eg.:

# echo source >/sys/class/typec/port0/power_role
# cat /sys/class/typec/port0/power_role
[source] sink

The term in bracket indicates how is power currently flowing. See the kernel documentation see: https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-typec

Of course all of this works only if the device connected to the USB Type-C port supports Power Delivery. So it won't work with a typical Type-C power adapter or most Type-C portable batteries as they rarely support PD as of 2019.

16
  • This is actually the patch that I linked in the question. It's finally merged in the 4.19 cycle. Sep 25, 2019 at 4:23
  • 1
    You can control direction of power on a "given port" if the "given port" supports Power Delivery in hardware, which includes the port on a "given gadget". Which is a very big "IF", since USB, Type-C, and PD are nearly independent specifications, and all depends on how a manufacturer of a "given device" feels about implementing all 3 together. So to believe that some "term in bracket" on some Linux terminal window controls anything is very naive. Sep 26, 2019 at 3:07
  • 2
    Ale..chenski: not naive at all. If the port doesn't support PD, or if the remote device doesn't support PD, the echo command will clearly indicate it with an Input/Output Error or Operation Not Supported error. See power_role_store() in drivers/usb/typec/class.c. Whatever is shown in bracket is what is supported by the port, supported by the device, and successfully configured at the hardware level. Sep 26, 2019 at 5:21
  • This answer makes sense, however, I am getting (even with sudo) the error: warning: An error occurred while redirecting file '/sys/class/typec/port1/power_role' open: Permission denied. Oct 25, 2022 at 21:01
  • 3
    If you do sudo echo sink > /sys... shell tries to open the file for writing before it runs sudo. Have you tried sudo bash -c 'echo sink > /sys...`?
    – Zaar Hai
    Nov 20, 2022 at 7:35
5

USB type C connected together, then how is it decided which charges which?

This is all convoluted beyond comprehension so far. Type-C connector carries two roles

  1. data communication
  2. power delivery.

These are nearly independent functions. The entire functionality depends on whether

  1. both ports are USB hosts
  2. both ports are USB devices
  3. one port is Dual-role-Data and another is not
  4. one port is Dual-role-power

...

n. both ports are DRD and DRP,

I lost all permutations here. The UCSI is an attempt by Microsoft to tame this vast permutivity into API standard. For major ideas, see this presentation, and many more publications on "UCSI interface".

2
0

As Marc's answered above, PD direction can be controlled through /sys/class/typec/port*/power_role, however, if it does NOT work, like it does on my Thinkpad Yoga 6:

sudo bash -c 'echo sink > /sys/class/typec/port1/power_role'
bash: line 1: echo: write error: Invalid argument

Then you can try the "hardware" method by quickly re-plugging your powerbank as mentioned by this answer.

Originally I was sceptical myself, but surprisingly it works! It took some practice - you need to unplug and plug it back quick but not too quick. In my case I use Romoss PPD20 powerbank and I need to unplug it for about half a second - just until the charging indicator stops flashing.

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 16, 2023 at 15:53
  • Thank you. I hope it's better
    – Zaar Hai
    Dec 17, 2023 at 0:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .