1 /*
2  * Copyright (c) 2024, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef USBH_CH34X_H
7 #define USBH_CH34X_H
8 
9 #include "usb_cdc.h"
10 
11 /* Requests */
12 #define CH34X_READ_VERSION 0x5F
13 #define CH34X_WRITE_REG    0x9A
14 #define CH34X_READ_REG     0x95
15 #define CH34X_SERIAL_INIT  0xA1
16 #define CH34X_MODEM_CTRL   0xA4
17 
18 // modem control bits
19 #define CH34X_BIT_RTS (1 << 6)
20 #define CH34X_BIT_DTR (1 << 5)
21 
22 #define CH341_CTO_O   0x10
23 #define CH341_CTO_D   0x20
24 #define CH341_CTO_R   0x40
25 #define CH341_CTI_C   0x01
26 #define CH341_CTI_DS  0x02
27 #define CH341_CTRL_RI 0x04
28 #define CH341_CTI_DC  0x08
29 #define CH341_CTI_ST  0x0f
30 
31 #define CH341_L_ER 0x80
32 #define CH341_L_ET 0x40
33 #define CH341_L_PS 0x38
34 #define CH341_L_PM 0x28
35 #define CH341_L_PE 0x18
36 #define CH341_L_PO 0x08
37 #define CH341_L_SB 0x04
38 #define CH341_L_D8 0x03
39 #define CH341_L_D7 0x02
40 #define CH341_L_D6 0x01
41 #define CH341_L_D5 0x00
42 
43 struct usbh_ch34x {
44     struct usbh_hubport *hport;
45     struct usb_endpoint_descriptor *bulkin;  /* Bulk IN endpoint */
46     struct usb_endpoint_descriptor *bulkout; /* Bulk OUT endpoint */
47     struct usbh_urb bulkout_urb;
48     struct usbh_urb bulkin_urb;
49 
50     struct cdc_line_coding line_coding;
51 
52     uint8_t intf;
53     uint8_t minor;
54 
55     void *user_data;
56 };
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
62 int usbh_ch34x_set_line_coding(struct usbh_ch34x *ch34x_class, struct cdc_line_coding *line_coding);
63 int usbh_ch34x_get_line_coding(struct usbh_ch34x *ch34x_class, struct cdc_line_coding *line_coding);
64 int usbh_ch34x_set_line_state(struct usbh_ch34x *ch34x_class, bool dtr, bool rts);
65 
66 int usbh_ch34x_bulk_in_transfer(struct usbh_ch34x *ch34x_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
67 int usbh_ch34x_bulk_out_transfer(struct usbh_ch34x *ch34x_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
68 
69 void usbh_ch34x_run(struct usbh_ch34x *ch34x_class);
70 void usbh_ch34x_stop(struct usbh_ch34x *ch34x_class);
71 
72 #ifdef __cplusplus
73 }
74 #endif
75 
76 #endif /* USBH_CH34X_H */
77