Repository navigation
Add ch340 support - #2
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
ch340driver for the WCH CH340B's 64-byte on-chip configuration area,alongside the existing FTDI families.
What works
Detection, and it is proven on two chips. WCH gives the whole family one
identity — a CH340G and a CH340B both enumerate as
1a86:7523, with the samebcdDeviceand the same chip version byte — so nothing in the descriptor tellsthem apart. The driver asks the chip instead: a part without the configuration
memory stalls vendor request
0x54, and that refusal is the test. A CH340G(version
0x30) stalls it; the CH340B (version0x31) accepts it. The errormessage says plainly that the chip holds its identity in mask ROM and that no
tool can change it.
What does not
Writing has never been observed to land. The CH340B accepts every
0x54write —no stall, no error — and stores nothing. It was tried with no preamble, with the
three-command pre-read preamble, with the full six-command init, with a settling
delay, and with four different
wIndexvalues; none of them changed a byte. Thesignature byte was deliberately left invalid throughout, so the chip was never at
risk. The likely explanation is an unlock step missing from the public capture,
and settling it needs a USB capture of WCH's own
CH34xSerCfgdoing a write.So the driver is built to fail loudly rather than quietly:
says the chip is unharmed and needs no recovery, rather than sending the user
after a backup they do not need.
are printed, so a table of vendor defaults is never mistaken for values read
off the chip.
0x57at offset0x03) is a one-way door, so nothingwrites it and an image carrying it is refused.
a chip that still enumerates.
The README and the driver description both mark it experimental, and the tables
say "writing unconfirmed".
Layout
Offsets come from WCH's "CH340 Datasheet (I)" version 3B, section 5.2. The
transport that reaches them — request
0x54, address in the high half ofwValue— is not documented by WCH and comes from USB captures ofCH34xSerCfg; the credits section says so.Two things differ from the FTDI parts and shape the code:
0x58means the chip reads the area, anything else means it runs on vendordefaults.
reports the datasheet defaults rather than
0000:0000at 0 mA, and any encodestamps the signature — otherwise
set serial_number=…would commit an identitypicked up from bytes the chip was never reading.
eraseclears the signature rather than wiping the area, so it is reversible.Also
propsnow marks read-only properties as such, since the CH340B is the firstdriver to have any (
config_active,write_protected), andinfoprints them.Tests
11 new cases pin the layout to the datasheet addresses: the inactive-area decode,
signature activation, identity/power offsets, the serial-number and product
descriptor encodings, the write-protect refusal and the round trip. No hardware
needed —
Ch340Configis pure.Lint
The lint job was already red on
mainbefore this branch, on areadability-convert-member-functions-to-staticfinding inftdi_codec.cpp.The new driver added four more of the same, and converting the methods is the
wrong fix:
Ch340Configdeliberately mirrors the FTDICodecas an object withinstance state, so making
decode/encode/sizestatic just moves the error toevery call site. The check is switched off in
.clang-tidyinstead, alongsidethe others there that contradict a deliberate choice in this project. That also
clears the pre-existing failure on
main.Two genuine findings from the same run are fixed rather than silenced: the two
empty catch blocks in the driver are restructured so each handler returns or
throws, and two test locals are made
const.