[Toybox] Proposal: Add ucsicontrol (USB Type-C UCSI utility) to toybox

M, Madhu madhu.m at intel.com
Tue Jul 14 12:42:17 UTC 2026


Hi Rob,

Thanks for the review. I have addressed the portability and readability comments in v2.

Changes in v2:
- Removed C bitfield and packed protocol struct decoding.
- Decode UCSI responses using explicit byte reads, masks, shifts, get_bits(), and toybox peek_le().
- Added named UCSI command constants instead of raw command literals.
- Added helper macros for one-based connector numbers and connector command fields.
- Added UCSI_MIN_MESSAGE_IN_LEN instead of raw 16-byte response checks.
- Renamed the command failure helper to xucsi_cmd() to follow toybox style.
- Added UCSI_BIT() and named decoded constants for clearer bit/value checks.
- Kept the debugfs response parser aligned with the kernel format: ext, high, low is restored to low-first protocol byte order before decoding.
- Added raw MESSAGE_IN printing for get_pdos responses before decoded PDO output.
- Added short comments for the non-obvious debugfs/UCSI details.

Pls take look, new version.

Attached, v2-0001-Add-ucsicontrol-command-to-control-query-USB-Type.patch

Thanks,
Madhu M

-----Original Message-----
From: Rob Landley <rob at landley.net> 
Sent: 14 July 2026 02:29
To: M, Madhu <madhu.m at intel.com>; toybox <toybox at lists.landley.net>
Subject: Re: [Toybox] Proposal: Add ucsicontrol (USB Type-C UCSI utility) to toybox

On 7/10/26 21:18, M, Madhu via Toybox wrote:
> Hi Rob,
> 
> I think there are a couple of point getting mixed together here.
> 
> USB-C / USB Type-C is indeed the connector and cable ecosystem, and 
> the USB data
 > protocol over it may still be USB 2.0, USB 3.x, DisplayPort alt mode,  > Thunderbolt, etc. So I agree that calling something a “USB-C wire  > protocol” can be confusing if what we mean is not traffic between two  > devices over the cable.
It's arm7 vs armv7 all over again...

> It provides a more readable, user-friendly way to inspect and control 
> Type-C connector
 > and USB Power Delivery state from the OS.

Ha! I was looking for something over the pandemic that would let me power cycle a USB-connected device from a Linux server, and couldn't find anything. I'm presuming this is that thing?

>> Please consider adding the ucsicontrol utility to toybox and share 
>> review comment on attached patch

I've applied it to toys/pending (pushed to codeberg and my website), and I'm taking a look at the code (in small chunks, it'll take a while to get through).

It starts with a LOT of bitfield structure definitions, which seem to be the inputs and outputs to the hardware? Hmmm... get_error_status is the argument to print_error_status() which is filled out by ucsi_xfer() which writes to the filehandle then calls read_resp() to read data from the filehandle, which does a hex->binary conversion but doesn't try to parse contents.

A) There's no existing #include <linux/blah.h> header or something for this? (The structure definitions are 1/4 of the whole file. It already won't work on bsd because of the debugfs requirement...)

B) Four of your structs say attribute packed and the rest don't. Why? 
(Did you annotate those during debugging when you hit errors on a specific target with a specific compiler, or is there a systematic reason?)

C) I've fought bitfields before, the problem is there's no specification of which order the bits go in (is it highest bit first or lowest bit first), and this turns out to be a GCC internal architecture-specific flag that ISN'T THE SAME as byte endianness. And at least at the time I was researching it, there was no way to specify at at runtime which you wanted (it's hardwired in at compile time based on the --target tuple, but DOES VARY). Meanwhile, you've got:

// Response is printed MSB-first; reverse to little-endian struct order.
for (i = 0; i < di; i++) data[i] = tmp[di-1-i];

So the hardware is (consistently?) producing little bit endian within big byte endian? (Hmmm, I'd prefer to use the existing endianness conversion macros but capability_data is over 64 bits. Seems to be the only one...)

I'm wondering if I can get a test environment to play with this. Does it require specific hardware and/or specific kernel config? (Who mounts the
debugfs?)

My laptop's an ancient dell e6230, which says it's got two 8087:0024 in it (the port on the left is a hybrid USB2/eSATA thing, the two on the right are USB3). Would this talk to them? Or do I need to dig up less obsolete hardware? (I've got some Orange Pi 3b boards lying around I've been meaning to resurrect, they have USB3 but I don't remember what;
rk3566 is a hairball SOC of chinese IP, I have no idea what rockchip licenses or "licenses", or what's an external PHY chip...)

I could of course boot all sorts of stuff under QEMU (https://landley.net/bin/mkroot/0.8.14), but the likelihood of this working there seems tiny. :)

Rob

P.S. It would be amazing if I could get "power down the port" and "power back up the port" commands. I have a USE for those: turtle board automated testing with the gdbserver kernel load protocol over USB serial.

P.P.S. I have NO IDEA how to do a regression tests/ucsicontrol.test for this. Is there a fake kernel module that hallucinates a virtual USB device for testing purposes, maybe? That should work fine under qemu...
-------------- next part --------------
A non-text attachment was scrubbed...
Name: v2-0001-Add-ucsicontrol-command-to-control-query-USB-Type.patch
Type: application/octet-stream
Size: 29552 bytes
Desc: v2-0001-Add-ucsicontrol-command-to-control-query-USB-Type.patch
URL: <http://lists.landley.net/pipermail/toybox/attachments/20260714/3104704e/attachment-0001.obj>


More information about the Toybox mailing list