1/*
2 * Copyright 2022, Gerasim Troeglazov <3dEyes@gmail.com>
3 * Distributed under the terms of the MIT License.
4 */
5
6#ifndef _USB_WINCHIPHEAD_H_
7#define _USB_WINCHIPHEAD_H_
8
9#include "SerialDevice.h"
10
11#define CH34X_DEFAULT_BAUD_RATE	9600
12
13#define CH34X_INPUT_BUF_SIZE	8
14
15#define CH34X_VER_20			0x20
16#define CH34X_VER_30			0x30
17
18#define CH34X_BPS_PRE_IMM		0x80
19
20#define CH34X_BIT_CTS			0x01
21#define CH34X_BIT_DSR			0x02
22#define CH34X_BIT_RI			0x04
23#define CH34X_BIT_DCD			0x08
24#define CH34X_BIT_DTR			0x20
25#define CH34X_BIT_RTS			0x40
26#define CH34X_BITS_MODEM_STAT	0x0f
27
28#define CH34X_MULT_STAT 		0x04
29
30#define CH34X_REQ_READ_VERSION	0x5F
31#define CH34X_REQ_WRITE_REG		0x9A
32#define CH34X_REQ_READ_REG		0x95
33#define CH34X_REQ_SERIAL_INIT	0xA1
34#define CH34X_REQ_MODEM_CTRL	0xA4
35
36#define CH34X_REG_BREAK			0x05
37#define CH34X_REG_STAT1			0x06
38#define CH34X_REG_STAT2			0x07
39#define CH34X_REG_BPS_PRE		0x12
40#define CH34X_REG_BPS_DIV		0x13
41#define CH34X_REG_LCR			0x18
42#define CH34X_REG_LCR2			0x25
43
44#define CH34X_LCR_CS5			0x00
45#define CH34X_LCR_CS6			0x01
46#define CH34X_LCR_CS7			0x02
47#define CH34X_LCR_CS8			0x03
48#define CH34X_LCR_STOP_BITS_2	0x04
49#define CH34X_LCR_ENABLE_PAR	0x08
50#define CH34X_LCR_PAR_EVEN		0x10
51#define CH34X_LCR_MARK_SPACE	0x20
52#define CH34X_LCR_ENABLE_TX		0x40
53#define CH34X_LCR_ENABLE_RX		0x80
54
55#define CH34X_SIO_600			0x6401
56#define CH34X_SIO_1200			0xB201
57#define CH34X_SIO_1800			0xCC01
58#define CH34X_SIO_2400			0xD901
59#define CH34X_SIO_4800			0x6402
60#define CH34X_SIO_9600			0xB202
61#define CH34X_SIO_19200			0xD902
62#define CH34X_SIO_31250			0x4003
63#define CH34X_SIO_38400			0x6403
64#define CH34X_SIO_57600			0xF302
65#define CH34X_SIO_115200		0xCC03
66#define CH34X_SIO_230400		0xE603
67
68/* supported vendor and product ids */
69#define VENDOR_WCH				0x4348	// WinChipHead
70#define VENDOR_QIN_HENG			0x1a86	// QinHeng Electronics
71#define VENDOR_GW_INSTEK		0x2184	// GW Instek
72
73const usb_serial_device kWCHDevices[] = {
74	{VENDOR_WCH,		0x5523, "CH341 serial converter"},
75	{VENDOR_QIN_HENG,	0x5523, "QinHeng CH341A serial converter"},
76	{VENDOR_QIN_HENG,	0x7522, "QinHeng CH340 serial converter"},
77	{VENDOR_QIN_HENG,	0x7523, "QinHeng CH340 serial converter"},
78	{VENDOR_GW_INSTEK,	0x0057, "GW Instek Oscilloscope"}
79};
80
81class WCHDevice : public SerialDevice {
82public:
83						WCHDevice(usb_device device, uint16 vendorID,
84							uint16 productID, const char *description);
85
86	virtual	status_t	AddDevice(const usb_configuration_info *config);
87
88	virtual	status_t	ResetDevice();
89
90	virtual	status_t	SetLineCoding(usb_cdc_line_coding *coding);
91	virtual	status_t	SetControlLineState(uint16 state);
92
93private:
94	status_t			WriteConfig(uint16 dataRate, uint8 lcr, uint8 mcr);
95
96	uint8				fChipVersion;
97	uint8				fStatusMCR;
98	uint8				fStatusLCR;
99	uint16				fDataRate;
100};
101
102
103#endif //_USB_WINCHIPHEAD_H_
104