1 /*
2  * Copyright (c) 2024, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef USBH_FTDI_H
7 #define USBH_FTDI_H
8 
9 #include "usb_cdc.h"
10 
11 /* Requests */
12 #define SIO_RESET_REQUEST             0x00 /* Reset the port */
13 #define SIO_SET_MODEM_CTRL_REQUEST    0x01 /* Set the modem control register */
14 #define SIO_SET_FLOW_CTRL_REQUEST     0x02 /* Set flow control register */
15 #define SIO_SET_BAUDRATE_REQUEST      0x03 /* Set baud rate */
16 #define SIO_SET_DATA_REQUEST          0x04 /* Set the data characteristics of the port */
17 #define SIO_POLL_MODEM_STATUS_REQUEST 0x05
18 #define SIO_SET_EVENT_CHAR_REQUEST    0x06
19 #define SIO_SET_ERROR_CHAR_REQUEST    0x07
20 #define SIO_SET_LATENCY_TIMER_REQUEST 0x09
21 #define SIO_GET_LATENCY_TIMER_REQUEST 0x0A
22 #define SIO_SET_BITMODE_REQUEST       0x0B
23 #define SIO_READ_PINS_REQUEST         0x0C
24 #define SIO_READ_EEPROM_REQUEST       0x90
25 #define SIO_WRITE_EEPROM_REQUEST      0x91
26 #define SIO_ERASE_EEPROM_REQUEST      0x92
27 
28 #define SIO_DISABLE_FLOW_CTRL 0x0
29 #define SIO_RTS_CTS_HS        (0x1 << 8)
30 #define SIO_DTR_DSR_HS        (0x2 << 8)
31 #define SIO_XON_XOFF_HS       (0x4 << 8)
32 
33 #define SIO_SET_DTR_MASK 0x1
34 #define SIO_SET_DTR_HIGH (1 | (SIO_SET_DTR_MASK << 8))
35 #define SIO_SET_DTR_LOW  (0 | (SIO_SET_DTR_MASK << 8))
36 #define SIO_SET_RTS_MASK 0x2
37 #define SIO_SET_RTS_HIGH (2 | (SIO_SET_RTS_MASK << 8))
38 #define SIO_SET_RTS_LOW  (0 | (SIO_SET_RTS_MASK << 8))
39 
40 #define SIO_RTS_CTS_HS (0x1 << 8)
41 
42 enum ftdi_chip_type {
43     SIO,
44     FT232A,
45     FT232B,
46     FT2232C,
47     FT232R,
48     FT232H,
49     FT2232H,
50     FT4232H,
51     FT4232HA,
52     FT232HP,
53     FT233HP,
54     FT2232HP,
55     FT2233HP,
56     FT4232HP,
57     FT4233HP,
58     FTX,
59 };
60 
61 struct usbh_ftdi {
62     struct usbh_hubport *hport;
63     struct usb_endpoint_descriptor *bulkin;  /* Bulk IN endpoint */
64     struct usb_endpoint_descriptor *bulkout; /* Bulk OUT endpoint */
65     struct usbh_urb bulkout_urb;
66     struct usbh_urb bulkin_urb;
67 
68     struct cdc_line_coding line_coding;
69 
70     uint8_t intf;
71     uint8_t minor;
72     uint8_t modem_status[2];
73     enum ftdi_chip_type chip_type;
74 
75     void *user_data;
76 };
77 
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
82 int usbh_ftdi_set_line_coding(struct usbh_ftdi *ftdi_class, struct cdc_line_coding *line_coding);
83 int usbh_ftdi_get_line_coding(struct usbh_ftdi *ftdi_class, struct cdc_line_coding *line_coding);
84 int usbh_ftdi_set_line_state(struct usbh_ftdi *ftdi_class, bool dtr, bool rts);
85 
86 int usbh_ftdi_bulk_in_transfer(struct usbh_ftdi *ftdi_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
87 int usbh_ftdi_bulk_out_transfer(struct usbh_ftdi *ftdi_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
88 
89 void usbh_ftdi_run(struct usbh_ftdi *ftdi_class);
90 void usbh_ftdi_stop(struct usbh_ftdi *ftdi_class);
91 
92 #ifdef __cplusplus
93 }
94 #endif
95 
96 #endif /* USBH_FTDI_H */
97