1 /*
2 * Copyright (c) 2024, sakumisu
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include "usbh_core.h"
7 #include "usbh_ch34x.h"
8
9 #define DEV_FORMAT "/dev/ttyUSB%d"
10
11 USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_ch34x_buf[USB_ALIGN_UP(64, CONFIG_USB_ALIGN_SIZE)];
12
13 #define CONFIG_USBHOST_MAX_CP210X_CLASS 1
14
15 static struct usbh_ch34x g_ch34x_class[CONFIG_USBHOST_MAX_CP210X_CLASS];
16 static uint32_t g_devinuse = 0;
17
usbh_ch34x_class_alloc(void)18 static struct usbh_ch34x *usbh_ch34x_class_alloc(void)
19 {
20 uint8_t devno;
21
22 for (devno = 0; devno < CONFIG_USBHOST_MAX_CP210X_CLASS; devno++) {
23 if ((g_devinuse & (1U << devno)) == 0) {
24 g_devinuse |= (1U << devno);
25 memset(&g_ch34x_class[devno], 0, sizeof(struct usbh_ch34x));
26 g_ch34x_class[devno].minor = devno;
27 return &g_ch34x_class[devno];
28 }
29 }
30 return NULL;
31 }
32
usbh_ch34x_class_free(struct usbh_ch34x * ch34x_class)33 static void usbh_ch34x_class_free(struct usbh_ch34x *ch34x_class)
34 {
35 uint8_t devno = ch34x_class->minor;
36
37 if (devno < 32) {
38 g_devinuse &= ~(1U << devno);
39 }
40 memset(ch34x_class, 0, sizeof(struct usbh_ch34x));
41 }
42
usbh_ch34x_get_baudrate_div(uint32_t baudrate,uint8_t * factor,uint8_t * divisor)43 static int usbh_ch34x_get_baudrate_div(uint32_t baudrate, uint8_t *factor, uint8_t *divisor)
44 {
45 uint8_t a;
46 uint8_t b;
47 uint32_t c;
48
49 switch (baudrate) {
50 case 921600:
51 a = 0xf3;
52 b = 7;
53 break;
54
55 case 307200:
56 a = 0xd9;
57 b = 7;
58 break;
59
60 default:
61 if (baudrate > 6000000 / 255) {
62 b = 3;
63 c = 6000000;
64 } else if (baudrate > 750000 / 255) {
65 b = 2;
66 c = 750000;
67 } else if (baudrate > 93750 / 255) {
68 b = 1;
69 c = 93750;
70 } else {
71 b = 0;
72 c = 11719;
73 }
74 a = (uint8_t)(c / baudrate);
75 if (a == 0 || a == 0xFF) {
76 return -USB_ERR_INVAL;
77 }
78 if ((c / a - baudrate) > (baudrate - c / (a + 1))) {
79 a++;
80 }
81 a = (uint8_t)(256 - a);
82 break;
83 }
84
85 *factor = a;
86 *divisor = b;
87
88 return 0;
89 }
90
usbh_ch34x_get_version(struct usbh_ch34x * ch34x_class)91 static int usbh_ch34x_get_version(struct usbh_ch34x *ch34x_class)
92 {
93 struct usb_setup_packet *setup;
94 int ret;
95
96 if (!ch34x_class || !ch34x_class->hport) {
97 return -USB_ERR_INVAL;
98 }
99 setup = ch34x_class->hport->setup;
100
101 setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
102 setup->bRequest = CH34X_READ_VERSION;
103 setup->wValue = 0;
104 setup->wIndex = 0;
105 setup->wLength = 2;
106
107 ret = usbh_control_transfer(ch34x_class->hport, setup, g_ch34x_buf);
108 if (ret < 0) {
109 return ret;
110 }
111
112 USB_LOG_INFO("Ch34x chip version %02x:%02x\r\n", g_ch34x_buf[0], g_ch34x_buf[1]);
113 return ret;
114 }
115
usbh_ch34x_flow_ctrl(struct usbh_ch34x * ch34x_class)116 static int usbh_ch34x_flow_ctrl(struct usbh_ch34x *ch34x_class)
117 {
118 struct usb_setup_packet *setup;
119
120 if (!ch34x_class || !ch34x_class->hport) {
121 return -USB_ERR_INVAL;
122 }
123 setup = ch34x_class->hport->setup;
124
125 setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
126 setup->bRequest = CH34X_WRITE_REG;
127 setup->wValue = 0x2727;
128 setup->wIndex = 0;
129 setup->wLength = 0;
130
131 return usbh_control_transfer(ch34x_class->hport, setup, NULL);
132 }
133
usbh_ch34x_set_line_coding(struct usbh_ch34x * ch34x_class,struct cdc_line_coding * line_coding)134 int usbh_ch34x_set_line_coding(struct usbh_ch34x *ch34x_class, struct cdc_line_coding *line_coding)
135 {
136 struct usb_setup_packet *setup;
137 uint16_t reg_value = 0;
138 uint16_t value = 0;
139 uint8_t factor = 0;
140 uint8_t divisor = 0;
141
142 if (!ch34x_class || !ch34x_class->hport) {
143 return -USB_ERR_INVAL;
144 }
145 setup = ch34x_class->hport->setup;
146
147 memcpy((uint8_t *)&ch34x_class->line_coding, line_coding, sizeof(struct cdc_line_coding));
148
149 /* refer to https://github.com/WCHSoftGroup/ch341ser_linux/blob/main/driver/ch341.c */
150
151 switch (line_coding->bParityType) {
152 case 0:
153 break;
154 case 1:
155 reg_value |= CH341_L_PO;
156 break;
157 case 2:
158 reg_value |= CH341_L_PE;
159 break;
160 case 3:
161 reg_value |= CH341_L_PM;
162 break;
163 case 4:
164 reg_value |= CH341_L_PS;
165 break;
166 default:
167 return -USB_ERR_INVAL;
168 }
169
170 switch (line_coding->bDataBits) {
171 case 5:
172 reg_value |= CH341_L_D5;
173 break;
174 case 6:
175 reg_value |= CH341_L_D6;
176 break;
177 case 7:
178 reg_value |= CH341_L_D7;
179 break;
180 case 8:
181 reg_value |= CH341_L_D8;
182 break;
183 default:
184 return -USB_ERR_INVAL;
185 }
186
187 if (line_coding->bCharFormat == 2) {
188 reg_value |= CH341_L_SB;
189 }
190
191 reg_value |= 0xC0;
192
193 value |= 0x9c;
194 value |= reg_value << 8;
195
196 usbh_ch34x_get_baudrate_div(line_coding->dwDTERate, &factor, &divisor);
197
198 setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
199 setup->bRequest = CH34X_SERIAL_INIT;
200 setup->wValue = value;
201 setup->wIndex = (factor << 8) | 0x80 | divisor;
202 setup->wLength = 0;
203
204 return usbh_control_transfer(ch34x_class->hport, setup, NULL);
205 }
206
usbh_ch34x_get_line_coding(struct usbh_ch34x * ch34x_class,struct cdc_line_coding * line_coding)207 int usbh_ch34x_get_line_coding(struct usbh_ch34x *ch34x_class, struct cdc_line_coding *line_coding)
208 {
209 memcpy(line_coding, (uint8_t *)&ch34x_class->line_coding, sizeof(struct cdc_line_coding));
210 return 0;
211 }
212
usbh_ch34x_set_line_state(struct usbh_ch34x * ch34x_class,bool dtr,bool rts)213 int usbh_ch34x_set_line_state(struct usbh_ch34x *ch34x_class, bool dtr, bool rts)
214 {
215 struct usb_setup_packet *setup;
216
217 if (!ch34x_class || !ch34x_class->hport) {
218 return -USB_ERR_INVAL;
219 }
220 setup = ch34x_class->hport->setup;
221
222 setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
223 setup->bRequest = CH34X_MODEM_CTRL;
224 setup->wValue = 0x0f | (dtr << 5) | (rts << 6);
225 setup->wIndex = 0;
226 setup->wLength = 0;
227
228 return usbh_control_transfer(ch34x_class->hport, setup, NULL);
229 }
230
usbh_ch34x_connect(struct usbh_hubport * hport,uint8_t intf)231 static int usbh_ch34x_connect(struct usbh_hubport *hport, uint8_t intf)
232 {
233 struct usb_endpoint_descriptor *ep_desc;
234 int ret = 0;
235
236 struct usbh_ch34x *ch34x_class = usbh_ch34x_class_alloc();
237 if (ch34x_class == NULL) {
238 USB_LOG_ERR("Fail to alloc ch34x_class\r\n");
239 return -USB_ERR_NOMEM;
240 }
241
242 ch34x_class->hport = hport;
243 ch34x_class->intf = intf;
244
245 hport->config.intf[intf].priv = ch34x_class;
246
247 usbh_ch34x_get_version(ch34x_class);
248 usbh_ch34x_flow_ctrl(ch34x_class);
249
250 for (uint8_t i = 0; i < hport->config.intf[intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
251 ep_desc = &hport->config.intf[intf].altsetting[0].ep[i].ep_desc;
252 if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
253 continue;
254 } else {
255 if (ep_desc->bEndpointAddress & 0x80) {
256 USBH_EP_INIT(ch34x_class->bulkin, ep_desc);
257 } else {
258 USBH_EP_INIT(ch34x_class->bulkout, ep_desc);
259 }
260 }
261 }
262
263 snprintf(hport->config.intf[intf].devname, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT, ch34x_class->minor);
264
265 USB_LOG_INFO("Register CH34X Class:%s\r\n", hport->config.intf[intf].devname);
266
267 #if 0
268 USB_LOG_INFO("Test ch34x rx and tx and rx for 5 times, baudrate is 115200\r\n");
269
270 struct cdc_line_coding linecoding;
271 uint8_t count = 5;
272
273 linecoding.dwDTERate = 115200;
274 linecoding.bDataBits = 8;
275 linecoding.bParityType = 0;
276 linecoding.bCharFormat = 0;
277 usbh_ch34x_set_line_coding(ch34x_class, &linecoding);
278 usbh_ch34x_set_line_state(ch34x_class, true, false);
279
280 memset(g_ch34x_buf, 'a', sizeof(g_ch34x_buf));
281 ret = usbh_ch34x_bulk_out_transfer(ch34x_class, g_ch34x_buf, sizeof(g_ch34x_buf), 0xfffffff);
282 USB_LOG_RAW("out ret:%d\r\n", ret);
283 while (count--) {
284 ret = usbh_ch34x_bulk_in_transfer(ch34x_class, g_ch34x_buf, sizeof(g_ch34x_buf), 0xfffffff);
285 USB_LOG_RAW("in ret:%d\r\n", ret);
286 if (ret > 0) {
287 for (uint32_t i = 0; i < ret; i++) {
288 USB_LOG_RAW("%02x ", g_ch34x_buf[i]);
289 }
290 USB_LOG_RAW("\r\n");
291 }
292 }
293 #endif
294 usbh_ch34x_run(ch34x_class);
295 return ret;
296 }
297
usbh_ch34x_disconnect(struct usbh_hubport * hport,uint8_t intf)298 static int usbh_ch34x_disconnect(struct usbh_hubport *hport, uint8_t intf)
299 {
300 int ret = 0;
301
302 struct usbh_ch34x *ch34x_class = (struct usbh_ch34x *)hport->config.intf[intf].priv;
303
304 if (ch34x_class) {
305 if (ch34x_class->bulkin) {
306 usbh_kill_urb(&ch34x_class->bulkin_urb);
307 }
308
309 if (ch34x_class->bulkout) {
310 usbh_kill_urb(&ch34x_class->bulkout_urb);
311 }
312
313 if (hport->config.intf[intf].devname[0] != '\0') {
314 usb_osal_thread_schedule_other();
315 USB_LOG_INFO("Unregister CH34X Class:%s\r\n", hport->config.intf[intf].devname);
316 usbh_ch34x_stop(ch34x_class);
317 }
318
319 usbh_ch34x_class_free(ch34x_class);
320 }
321
322 return ret;
323 }
324
usbh_ch34x_bulk_in_transfer(struct usbh_ch34x * ch34x_class,uint8_t * buffer,uint32_t buflen,uint32_t timeout)325 int usbh_ch34x_bulk_in_transfer(struct usbh_ch34x *ch34x_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
326 {
327 int ret;
328 struct usbh_urb *urb = &ch34x_class->bulkin_urb;
329
330 usbh_bulk_urb_fill(urb, ch34x_class->hport, ch34x_class->bulkin, buffer, buflen, timeout, NULL, NULL);
331 ret = usbh_submit_urb(urb);
332 if (ret == 0) {
333 ret = urb->actual_length;
334 }
335 return ret;
336 }
337
usbh_ch34x_bulk_out_transfer(struct usbh_ch34x * ch34x_class,uint8_t * buffer,uint32_t buflen,uint32_t timeout)338 int usbh_ch34x_bulk_out_transfer(struct usbh_ch34x *ch34x_class, uint8_t *buffer, uint32_t buflen, uint32_t timeout)
339 {
340 int ret;
341 struct usbh_urb *urb = &ch34x_class->bulkout_urb;
342
343 usbh_bulk_urb_fill(urb, ch34x_class->hport, ch34x_class->bulkout, buffer, buflen, timeout, NULL, NULL);
344 ret = usbh_submit_urb(urb);
345 if (ret == 0) {
346 ret = urb->actual_length;
347 }
348 return ret;
349 }
350
usbh_ch34x_run(struct usbh_ch34x * ch34x_class)351 __WEAK void usbh_ch34x_run(struct usbh_ch34x *ch34x_class)
352 {
353 (void)ch34x_class;
354 }
355
usbh_ch34x_stop(struct usbh_ch34x * ch34x_class)356 __WEAK void usbh_ch34x_stop(struct usbh_ch34x *ch34x_class)
357 {
358 (void)ch34x_class;
359 }
360
361 static const uint16_t ch34x_id_table[][2] = {
362 { 0x1A86, 0x7523 },
363 { 0, 0 },
364 };
365
366 const struct usbh_class_driver ch34x_class_driver = {
367 .driver_name = "ch34x",
368 .connect = usbh_ch34x_connect,
369 .disconnect = usbh_ch34x_disconnect
370 };
371
372 CLASS_INFO_DEFINE const struct usbh_class_info ch34x_class_info = {
373 .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
374 .bInterfaceClass = 0xff,
375 .bInterfaceSubClass = 0x00,
376 .bInterfaceProtocol = 0x00,
377 .id_table = ch34x_id_table,
378 .class_driver = &ch34x_class_driver
379 };