1 /*
2  * Copyright (c) 2024, sakumisu
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef USBH_PL2303_H
7 #define USBH_PL2303_H
8 
9 #include "usb_cdc.h"
10 
11 #define PL2303_SET_REQUEST             0x01
12 #define PL2303_SET_REQUEST_PL2303HXN   0x80
13 #define PL2303_SET_CRTSCTS             0x41
14 #define PL2303_SET_CRTSCTS_PL2303X     0x61
15 #define PL2303_SET_CRTSCTS_PL2303HXN   0xFA
16 #define PL2303_CLEAR_CRTSCTS_PL2303HXN 0xFF
17 #define PL2303_CRTSCTS_REG_PL2303HXN   0x0A
18 #define PL2303_STATUS_REG_PL2303HX     0x8080
19 
20 /* Different PL2303 IC types */
21 #define USBH_PL2303_TYPE_UNKNOWN   0
22 #define USBH_PL2303_TYPE_PL2303    1
23 #define USBH_PL2303_TYPE_PL2303HX  2
24 #define USBH_PL2303_TYPE_PL2303HXD 3
25 #define USBH_PL2303_TYPE_PL2303HXN 4
26 
27 struct usbh_pl2303 {
28     struct usbh_hubport *hport;
29     struct usb_endpoint_descriptor *bulkin;  /* Bulk IN endpoint */
30     struct usb_endpoint_descriptor *bulkout; /* Bulk OUT endpoint */
31 
32     struct usbh_urb bulkout_urb;
33     struct usbh_urb bulkin_urb;
34 
35     struct cdc_line_coding linecoding;
36 
37     uint8_t intf;
38     uint8_t minor;
39     uint8_t chiptype;
40 
41     void *user_data;
42 };
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 
48 int usbh_pl2303_set_line_coding(struct usbh_pl2303 *pl2303_class, struct cdc_line_coding *line_coding);
49 int usbh_pl2303_get_line_coding(struct usbh_pl2303 *pl2303_class, struct cdc_line_coding *line_coding);
50 int usbh_pl2303_set_line_state(struct usbh_pl2303 *pl2303_class, bool dtr, bool rts);
51 
52 int usbh_pl2303_bulk_in_transfer(struct usbh_pl2303 *pl2303_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
53 int usbh_pl2303_bulk_out_transfer(struct usbh_pl2303 *pl2303_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout);
54 
55 void usbh_pl2303_run(struct usbh_pl2303 *pl2303_class);
56 void usbh_pl2303_stop(struct usbh_pl2303 *pl2303_class);
57 
58 #ifdef __cplusplus
59 }
60 #endif
61 
62 #endif /* USBH_PL2303_H */
63