drivers/usb/serial/ch341.c
Source file repositories/reference/linux-study-clean/drivers/usb/serial/ch341.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/usb/serial/ch341.c- Extension
.c- Size
- 23133 bytes
- Lines
- 894
- Domain
- Driver Families
- Bucket
- drivers/usb
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/tty.hlinux/module.hlinux/slab.hlinux/usb.hlinux/usb/serial.hlinux/serial.hlinux/unaligned.h
Detected Declarations
struct ch341_privatefunction ch341_control_outfunction ch341_control_infunction ch341_get_divisorfunction clockfunction ch341_set_baudrate_lcrfunction ch341_set_handshakefunction ch341_get_statusfunction ch341_configurefunction ch341_detect_quirksfunction ch341_port_probefunction ch341_port_removefunction ch341_carrier_raisedfunction ch341_dtr_rtsfunction ch341_closefunction ch341_openfunction ch341_set_flow_controlfunction ch341_set_termiosfunction failsfunction ch341_break_ctlfunction ch341_tiocmsetfunction ch341_update_statusfunction ch341_read_int_callbackfunction ch341_tiocmgetfunction ch341_reset_resume
Annotated Snippet
struct ch341_private {
spinlock_t lock; /* access lock */
unsigned baud_rate; /* set baud rate */
u8 mcr;
u8 msr;
u8 lcr;
unsigned long quirks;
u8 version;
unsigned long break_end;
};
static void ch341_set_termios(struct tty_struct *tty,
struct usb_serial_port *port,
const struct ktermios *old_termios);
static int ch341_control_out(struct usb_device *dev, u8 request,
u16 value, u16 index)
{
int r;
dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x)\n", __func__,
request, value, index);
r = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT,
value, index, NULL, 0, DEFAULT_TIMEOUT);
if (r < 0)
dev_err(&dev->dev, "failed to send control message: %d\n", r);
return r;
}
static int ch341_control_in(struct usb_device *dev,
u8 request, u16 value, u16 index,
char *buf, unsigned bufsize)
{
int r;
dev_dbg(&dev->dev, "%s - (%02x,%04x,%04x,%u)\n", __func__,
request, value, index, bufsize);
r = usb_control_msg_recv(dev, 0, request,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN,
value, index, buf, bufsize, DEFAULT_TIMEOUT,
GFP_KERNEL);
if (r) {
dev_err(&dev->dev, "failed to receive control message: %d\n",
r);
return r;
}
return 0;
}
#define CH341_CLKRATE 48000000
#define CH341_CLK_DIV(ps, fact) (1 << (12 - 3 * (ps) - (fact)))
#define CH341_MIN_RATE(ps) (CH341_CLKRATE / (CH341_CLK_DIV((ps), 1) * 512))
static const speed_t ch341_min_rates[] = {
CH341_MIN_RATE(0),
CH341_MIN_RATE(1),
CH341_MIN_RATE(2),
CH341_MIN_RATE(3),
};
/* Supported range is 46 to 3000000 bps. */
#define CH341_MIN_BPS DIV_ROUND_UP(CH341_CLKRATE, CH341_CLK_DIV(0, 0) * 256)
#define CH341_MAX_BPS (CH341_CLKRATE / (CH341_CLK_DIV(3, 0) * 2))
/*
* The device line speed is given by the following equation:
*
* baudrate = 48000000 / (2^(12 - 3 * ps - fact) * div), where
*
* 0 <= ps <= 3,
* 0 <= fact <= 1,
* 2 <= div <= 256 if fact = 0, or
* 9 <= div <= 256 if fact = 1
*/
static int ch341_get_divisor(struct ch341_private *priv, speed_t speed)
{
unsigned int fact, div, clk_div;
bool force_fact0 = false;
int ps;
/*
* Clamp to supported range, this makes the (ps < 0) and (div < 2)
* sanity checks below redundant.
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/tty.h`, `linux/module.h`, `linux/slab.h`, `linux/usb.h`, `linux/usb/serial.h`, `linux/serial.h`, `linux/unaligned.h`.
- Detected declarations: `struct ch341_private`, `function ch341_control_out`, `function ch341_control_in`, `function ch341_get_divisor`, `function clock`, `function ch341_set_baudrate_lcr`, `function ch341_set_handshake`, `function ch341_get_status`, `function ch341_configure`, `function ch341_detect_quirks`.
- Atlas domain: Driver Families / drivers/usb.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.