usbh_serial.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <rtthread.h>
  7. #include <rtdevice.h>
  8. #include "usbh_core.h"
  9. #include "usbh_cdc_acm.h"
  10. #include "usbh_ftdi.h"
  11. #include "usbh_cp210x.h"
  12. #include "usbh_ch34x.h"
  13. #include "usbh_pl2303.h"
  14. #define DEV_FORMAT_VENDOR "ttyUSB%d"
  15. #define DEV_FORMAT_CDC_ACM "ttyACM%d"
  16. #define USBH_RX_MAX_SIZE 2048
  17. #ifndef CONFIG_USBHOST_MAX_VENDOR_SERIAL_CLASS
  18. #define CONFIG_USBHOST_MAX_VENDOR_SERIAL_CLASS (4)
  19. #endif
  20. #ifndef CONFIG_USBHOST_SERIAL_RX_BUFSIZE
  21. #define CONFIG_USBHOST_SERIAL_RX_BUFSIZE (USBH_RX_MAX_SIZE * 2)
  22. #endif
  23. enum usbh_serial_type {
  24. USBH_SERIAL_TYPE_CDC_ACM = 0,
  25. USBH_SERIAL_TYPE_FTDI,
  26. USBH_SERIAL_TYPE_CP210X,
  27. USBH_SERIAL_TYPE_CH34X,
  28. USBH_SERIAL_TYPE_PL2303,
  29. };
  30. struct usbh_serial {
  31. struct rt_device parent;
  32. enum usbh_serial_type type;
  33. uint8_t minor;
  34. char name[CONFIG_USBHOST_DEV_NAMELEN];
  35. struct rt_ringbuffer rx_rb;
  36. rt_uint8_t rx_rb_buffer[CONFIG_USBHOST_SERIAL_RX_BUFSIZE];
  37. };
  38. static uint32_t g_devinuse_vendor = 0;
  39. static uint32_t g_devinuse_cdc_acm = 0;
  40. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_usbh_serial_vendor_rx_buf[CONFIG_USBHOST_MAX_VENDOR_SERIAL_CLASS][USB_ALIGN_UP(USBH_RX_MAX_SIZE, CONFIG_USB_ALIGN_SIZE)];
  41. static USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t g_usbh_serial_cdc_acm_rx_buf[CONFIG_USBHOST_MAX_CDC_ACM_CLASS][USB_ALIGN_UP(USBH_RX_MAX_SIZE, CONFIG_USB_ALIGN_SIZE)];
  42. static struct usbh_serial *usbh_serial_alloc(uint8_t type)
  43. {
  44. uint8_t devno;
  45. struct usbh_serial *serial;
  46. for (devno = 0; devno < CONFIG_USBHOST_MAX_VENDOR_SERIAL_CLASS; devno++) {
  47. if ((g_devinuse_vendor & (1U << devno)) == 0) {
  48. g_devinuse_vendor |= (1U << devno);
  49. serial = rt_malloc(sizeof(struct usbh_serial));
  50. memset(serial, 0, sizeof(struct usbh_serial));
  51. serial->type = type;
  52. serial->minor = devno;
  53. snprintf(serial->name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT_VENDOR, serial->minor);
  54. return serial;
  55. }
  56. }
  57. return NULL;
  58. }
  59. static void usbh_serial_free(struct usbh_serial *serial)
  60. {
  61. uint8_t devno = serial->minor;
  62. if (devno < 32) {
  63. g_devinuse_vendor &= ~(1U << devno);
  64. }
  65. memset(serial, 0, sizeof(struct usbh_serial));
  66. rt_free(serial);
  67. }
  68. static struct usbh_serial *usbh_serial_cdc_acm_alloc(uint8_t type)
  69. {
  70. uint8_t devno;
  71. struct usbh_serial *serial;
  72. for (devno = 0; devno < CONFIG_USBHOST_MAX_CDC_ACM_CLASS; devno++) {
  73. if ((g_devinuse_cdc_acm & (1U << devno)) == 0) {
  74. g_devinuse_cdc_acm |= (1U << devno);
  75. serial = rt_malloc(sizeof(struct usbh_serial));
  76. memset(serial, 0, sizeof(struct usbh_serial));
  77. serial->type = type;
  78. serial->minor = devno;
  79. snprintf(serial->name, CONFIG_USBHOST_DEV_NAMELEN, DEV_FORMAT_CDC_ACM, serial->minor);
  80. return serial;
  81. }
  82. }
  83. return NULL;
  84. }
  85. static void usbh_serial_cdc_acm_free(struct usbh_serial *serial)
  86. {
  87. uint8_t devno = serial->minor;
  88. if (devno < 32) {
  89. g_devinuse_cdc_acm &= ~(1U << devno);
  90. }
  91. memset(serial, 0, sizeof(struct usbh_serial));
  92. rt_free(serial);
  93. }
  94. static rt_err_t usbh_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  95. {
  96. struct usbh_serial *serial;
  97. RT_ASSERT(dev != RT_NULL);
  98. serial = (struct usbh_serial *)dev;
  99. switch (serial->type) {
  100. case USBH_SERIAL_TYPE_CDC_ACM:
  101. break;
  102. case USBH_SERIAL_TYPE_FTDI:
  103. break;
  104. case USBH_SERIAL_TYPE_CP210X:
  105. break;
  106. case USBH_SERIAL_TYPE_CH34X:
  107. break;
  108. case USBH_SERIAL_TYPE_PL2303:
  109. break;
  110. default:
  111. break;
  112. }
  113. return RT_EOK;
  114. }
  115. static rt_err_t usbh_serial_close(struct rt_device *dev)
  116. {
  117. struct usbh_serial *serial;
  118. RT_ASSERT(dev != RT_NULL);
  119. serial = (struct usbh_serial *)dev;
  120. switch (serial->type) {
  121. case USBH_SERIAL_TYPE_CDC_ACM:
  122. break;
  123. case USBH_SERIAL_TYPE_FTDI:
  124. break;
  125. case USBH_SERIAL_TYPE_CP210X:
  126. break;
  127. case USBH_SERIAL_TYPE_CH34X:
  128. break;
  129. case USBH_SERIAL_TYPE_PL2303:
  130. break;
  131. default:
  132. break;
  133. }
  134. return RT_EOK;
  135. }
  136. static rt_ssize_t usbh_serial_read(struct rt_device *dev,
  137. rt_off_t pos,
  138. void *buffer,
  139. rt_size_t size)
  140. {
  141. struct usbh_serial *serial;
  142. RT_ASSERT(dev != RT_NULL);
  143. serial = (struct usbh_serial *)dev;
  144. return rt_ringbuffer_get(&serial->rx_rb, (rt_uint8_t *)buffer, size);
  145. }
  146. static rt_ssize_t usbh_serial_write(struct rt_device *dev,
  147. rt_off_t pos,
  148. const void *buffer,
  149. rt_size_t size)
  150. {
  151. struct usbh_serial *serial;
  152. int ret = 0;
  153. rt_uint8_t *align_buf;
  154. RT_ASSERT(dev != RT_NULL);
  155. serial = (struct usbh_serial *)dev;
  156. align_buf = (rt_uint8_t *)buffer;
  157. if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) {
  158. align_buf = rt_malloc_align(USB_ALIGN_UP(size, CONFIG_USB_ALIGN_SIZE), CONFIG_USB_ALIGN_SIZE);
  159. if (!align_buf) {
  160. USB_LOG_ERR("serial get align buf failed\n");
  161. return 0;
  162. }
  163. usb_memcpy(align_buf, buffer, size);
  164. }
  165. switch (serial->type) {
  166. #if defined(PKG_CHERRYUSB_HOST_CDC_ACM) || defined(RT_CHERRYUSB_HOST_CDC_ACM)
  167. case USBH_SERIAL_TYPE_CDC_ACM:
  168. ret = usbh_cdc_acm_bulk_out_transfer((struct usbh_cdc_acm *)dev->user_data, (uint8_t *)align_buf, size, RT_WAITING_FOREVER);
  169. if (ret < 0) {
  170. USB_LOG_ERR("usbh_cdc_acm_bulk_out_transfer failed: %d\n", ret);
  171. ret = 0;
  172. }
  173. break;
  174. #endif
  175. #if defined(PKG_CHERRYUSB_HOST_FTDI) || defined(RT_CHERRYUSB_HOST_FTDI)
  176. case USBH_SERIAL_TYPE_FTDI:
  177. ret = usbh_ftdi_bulk_out_transfer((struct usbh_ftdi *)dev->user_data, (uint8_t *)align_buf, size, RT_WAITING_FOREVER);
  178. if (ret < 0) {
  179. USB_LOG_ERR("usbh_ftdi_bulk_out_transfer failed: %d\n", ret);
  180. ret = 0;
  181. }
  182. break;
  183. #endif
  184. #if defined(PKG_CHERRYUSB_HOST_CH34X) || defined(RT_CHERRYUSB_HOST_CH34X)
  185. case USBH_SERIAL_TYPE_CH34X:
  186. ret = usbh_ch34x_bulk_out_transfer((struct usbh_ch34x *)dev->user_data, (uint8_t *)align_buf, size, RT_WAITING_FOREVER);
  187. if (ret < 0) {
  188. USB_LOG_ERR("usbh_ch34x_bulk_out_transfer failed: %d\n", ret);
  189. ret = 0;
  190. }
  191. break;
  192. #endif
  193. #if defined(PKG_CHERRYUSB_HOST_PL2303) || defined(RT_CHERRYUSB_HOST_PL2303)
  194. case USBH_SERIAL_TYPE_PL2303:
  195. ret = usbh_pl2303_bulk_out_transfer((struct usbh_pl2303 *)dev->user_data, (uint8_t *)align_buf, size, RT_WAITING_FOREVER);
  196. if (ret < 0) {
  197. USB_LOG_ERR("usbh_pl2303_bulk_out_transfer failed: %d\n", ret);
  198. ret = 0;
  199. }
  200. break;
  201. #endif
  202. default:
  203. break;
  204. }
  205. if ((uint32_t)buffer & (CONFIG_USB_ALIGN_SIZE - 1)) {
  206. rt_free_align(align_buf);
  207. }
  208. return ret;
  209. }
  210. static rt_err_t usbh_serial_control(struct rt_device *dev,
  211. int cmd,
  212. void *args)
  213. {
  214. struct usbh_serial *serial;
  215. struct serial_configure *config;
  216. struct cdc_line_coding line_coding;
  217. int ret = -RT_EINVAL;
  218. RT_ASSERT(dev != RT_NULL);
  219. serial = (struct usbh_serial *)dev;
  220. switch (serial->type) {
  221. #if defined(PKG_CHERRYUSB_HOST_CDC_ACM) || defined(RT_CHERRYUSB_HOST_CDC_ACM)
  222. case USBH_SERIAL_TYPE_CDC_ACM:
  223. if (cmd == RT_DEVICE_CTRL_CONFIG) {
  224. struct usbh_cdc_acm *cdc_acm_class;
  225. cdc_acm_class = (struct usbh_cdc_acm *)dev->user_data;
  226. config = (struct serial_configure *)args;
  227. line_coding.dwDTERate = config->baud_rate;
  228. line_coding.bDataBits = config->data_bits;
  229. line_coding.bCharFormat = 0; // STOP_BITS_1
  230. line_coding.bParityType = config->parity;
  231. usbh_cdc_acm_set_line_coding(cdc_acm_class, &line_coding);
  232. }
  233. ret = RT_EOK;
  234. break;
  235. #endif
  236. #if defined(PKG_CHERRYUSB_HOST_FTDI) || defined(RT_CHERRYUSB_HOST_FTDI)
  237. case USBH_SERIAL_TYPE_FTDI:
  238. if (cmd == RT_DEVICE_CTRL_CONFIG) {
  239. struct usbh_ftdi *ftdi_class;
  240. ftdi_class = (struct usbh_ftdi *)dev->user_data;
  241. config = (struct serial_configure *)args;
  242. line_coding.dwDTERate = config->baud_rate;
  243. line_coding.bDataBits = config->data_bits;
  244. line_coding.bCharFormat = 0; // STOP_BITS_1
  245. line_coding.bParityType = config->parity;
  246. usbh_ftdi_set_line_coding(ftdi_class, &line_coding);
  247. }
  248. ret = RT_EOK;
  249. break;
  250. #endif
  251. #if defined(PKG_CHERRYUSB_HOST_CP210X) || defined(RT_CHERRYUSB_HOST_CP210X)
  252. case USBH_SERIAL_TYPE_CP210X:
  253. if (cmd == RT_DEVICE_CTRL_CONFIG) {
  254. struct usbh_cp210x *cp210x_class;
  255. cp210x_class = (struct usbh_cp210x *)dev->user_data;
  256. config = (struct serial_configure *)args;
  257. line_coding.dwDTERate = config->baud_rate;
  258. line_coding.bDataBits = config->data_bits;
  259. line_coding.bCharFormat = 0; // STOP_BITS_1
  260. line_coding.bParityType = config->parity;
  261. usbh_cp210x_set_line_coding(cp210x_class, &line_coding);
  262. }
  263. ret = RT_EOK;
  264. break;
  265. #endif
  266. #if defined(PKG_CHERRYUSB_HOST_CH34X) || defined(RT_CHERRYUSB_HOST_CH34X)
  267. case USBH_SERIAL_TYPE_CH34X:
  268. if (cmd == RT_DEVICE_CTRL_CONFIG) {
  269. struct usbh_ch34x *ch34x_class;
  270. ch34x_class = (struct usbh_ch34x *)dev->user_data;
  271. config = (struct serial_configure *)args;
  272. line_coding.dwDTERate = config->baud_rate;
  273. line_coding.bDataBits = config->data_bits;
  274. line_coding.bCharFormat = 0; // STOP_BITS_1
  275. line_coding.bParityType = config->parity;
  276. usbh_ch34x_set_line_coding(ch34x_class, &line_coding);
  277. }
  278. ret = RT_EOK;
  279. break;
  280. #endif
  281. #if defined(PKG_CHERRYUSB_HOST_PL2303) || defined(RT_CHERRYUSB_HOST_PL2303)
  282. case USBH_SERIAL_TYPE_PL2303:
  283. if (cmd == RT_DEVICE_CTRL_CONFIG) {
  284. struct usbh_pl2303 *pl2303_class;
  285. pl2303_class = (struct usbh_pl2303 *)dev->user_data;
  286. config = (struct serial_configure *)args;
  287. line_coding.dwDTERate = config->baud_rate;
  288. line_coding.bDataBits = config->data_bits;
  289. line_coding.bCharFormat = 0; // STOP_BITS_1
  290. line_coding.bParityType = config->parity;
  291. usbh_pl2303_set_line_coding(pl2303_class, &line_coding);
  292. }
  293. ret = RT_EOK;
  294. break;
  295. #endif
  296. default:
  297. break;
  298. }
  299. return ret;
  300. }
  301. #ifdef RT_USING_DEVICE_OPS
  302. const static struct rt_device_ops usbh_serial_ops = {
  303. NULL,
  304. usbh_serial_open,
  305. usbh_serial_close,
  306. usbh_serial_read,
  307. usbh_serial_write,
  308. usbh_serial_control
  309. };
  310. #endif
  311. #ifdef RT_USING_POSIX_DEVIO
  312. #include <unistd.h>
  313. #include <fcntl.h>
  314. #include <poll.h>
  315. #include <sys/ioctl.h>
  316. #include <dfs_file.h>
  317. #ifdef RT_USING_POSIX_TERMIOS
  318. #include <termios.h>
  319. #endif
  320. static rt_err_t usbh_serial_fops_rx_ind(rt_device_t dev, rt_size_t size)
  321. {
  322. rt_wqueue_wakeup(&(dev->wait_queue), (void*)POLLIN);
  323. return RT_EOK;
  324. }
  325. /* fops for serial */
  326. static int usbh_serial_fops_open(struct dfs_file *fd)
  327. {
  328. rt_err_t ret = 0;
  329. rt_uint16_t flags = 0;
  330. rt_device_t device;
  331. device = (rt_device_t)fd->vnode->data;
  332. RT_ASSERT(device != RT_NULL);
  333. switch (fd->flags & O_ACCMODE)
  334. {
  335. case O_RDONLY:
  336. USB_LOG_DBG("fops open: O_RDONLY!");
  337. flags = RT_DEVICE_FLAG_RDONLY;
  338. break;
  339. case O_WRONLY:
  340. USB_LOG_DBG("fops open: O_WRONLY!");
  341. flags = RT_DEVICE_FLAG_WRONLY;
  342. break;
  343. case O_RDWR:
  344. USB_LOG_DBG("fops open: O_RDWR!");
  345. flags = RT_DEVICE_FLAG_RDWR;
  346. break;
  347. default:
  348. USB_LOG_ERR("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
  349. break;
  350. }
  351. if ((fd->flags & O_ACCMODE) != O_WRONLY)
  352. rt_device_set_rx_indicate(device, usbh_serial_fops_rx_ind);
  353. ret = rt_device_open(device, flags);
  354. if (ret == RT_EOK) return 0;
  355. return ret;
  356. }
  357. static int usbh_serial_fops_close(struct dfs_file *fd)
  358. {
  359. rt_device_t device;
  360. device = (rt_device_t)fd->vnode->data;
  361. rt_device_set_rx_indicate(device, RT_NULL);
  362. rt_device_close(device);
  363. return 0;
  364. }
  365. static int usbh_serial_fops_ioctl(struct dfs_file *fd, int cmd, void *args)
  366. {
  367. rt_device_t device;
  368. int flags = (int)(rt_base_t)args;
  369. int mask = O_NONBLOCK | O_APPEND;
  370. device = (rt_device_t)fd->vnode->data;
  371. switch (cmd)
  372. {
  373. case FIONREAD:
  374. break;
  375. case FIONWRITE:
  376. break;
  377. case F_SETFL:
  378. flags &= mask;
  379. fd->flags &= ~mask;
  380. fd->flags |= flags;
  381. break;
  382. }
  383. return rt_device_control(device, cmd, args);
  384. }
  385. static int usbh_serial_fops_read(struct dfs_file *fd, void *buf, size_t count)
  386. {
  387. int size = 0;
  388. rt_device_t device;
  389. device = (rt_device_t)fd->vnode->data;
  390. do
  391. {
  392. size = rt_device_read(device, -1, buf, count);
  393. if (size <= 0)
  394. {
  395. if (fd->flags & O_NONBLOCK)
  396. {
  397. size = -EAGAIN;
  398. break;
  399. }
  400. rt_wqueue_wait(&(device->wait_queue), 0, RT_WAITING_FOREVER);
  401. }
  402. }while (size <= 0);
  403. return size;
  404. }
  405. static int usbh_serial_fops_write(struct dfs_file *fd, const void *buf, size_t count)
  406. {
  407. rt_device_t device;
  408. device = (rt_device_t)fd->vnode->data;
  409. return rt_device_write(device, -1, buf, count);
  410. }
  411. static int usbh_serial_fops_poll(struct dfs_file *fd, struct rt_pollreq *req)
  412. {
  413. int mask = 0;
  414. int flags = 0;
  415. rt_device_t device;
  416. struct usbh_serial *serial;
  417. device = (rt_device_t)fd->vnode->data;
  418. RT_ASSERT(device != RT_NULL);
  419. serial = (struct usbh_serial *)device;
  420. /* only support POLLIN */
  421. flags = fd->flags & O_ACCMODE;
  422. if (flags == O_RDONLY || flags == O_RDWR)
  423. {
  424. rt_base_t level;
  425. rt_poll_add(&(device->wait_queue), req);
  426. level = rt_hw_interrupt_disable();
  427. if (rt_ringbuffer_data_len(&serial->rx_rb))
  428. mask |= POLLIN;
  429. rt_hw_interrupt_enable(level);
  430. }
  431. // mask|=POLLOUT;
  432. return mask;
  433. }
  434. const static struct dfs_file_ops usbh_serial_fops =
  435. {
  436. usbh_serial_fops_open,
  437. usbh_serial_fops_close,
  438. usbh_serial_fops_ioctl,
  439. usbh_serial_fops_read,
  440. usbh_serial_fops_write,
  441. RT_NULL, /* flush */
  442. RT_NULL, /* lseek */
  443. RT_NULL, /* getdents */
  444. usbh_serial_fops_poll,
  445. };
  446. #endif /* RT_USING_POSIX_DEVIO */
  447. rt_err_t usbh_serial_register(struct usbh_serial *serial,
  448. void *data)
  449. {
  450. rt_err_t ret;
  451. struct rt_device *device;
  452. RT_ASSERT(serial != RT_NULL);
  453. device = &(serial->parent);
  454. device->type = RT_Device_Class_Char;
  455. device->rx_indicate = RT_NULL;
  456. device->tx_complete = RT_NULL;
  457. #ifdef RT_USING_DEVICE_OPS
  458. device->ops = &usbh_serial_ops;
  459. #else
  460. device->init = NULL;
  461. device->open = usbh_serial_open;
  462. device->close = usbh_serial_close;
  463. device->read = usbh_serial_read;
  464. device->write = usbh_serial_write;
  465. device->control = usbh_serial_control;
  466. #endif
  467. device->user_data = data;
  468. /* register a character device */
  469. ret = rt_device_register(device, serial->name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_REMOVABLE);
  470. #ifdef RT_USING_POSIX_DEVIO
  471. /* set fops */
  472. device->fops = &usbh_serial_fops;
  473. #endif
  474. rt_ringbuffer_init(&serial->rx_rb, serial->rx_rb_buffer, sizeof(serial->rx_rb_buffer));
  475. return ret;
  476. }
  477. void usbh_serial_unregister(struct usbh_serial *serial)
  478. {
  479. RT_ASSERT(serial != NULL);
  480. rt_device_unregister(&serial->parent);
  481. if (serial->type == USBH_SERIAL_TYPE_CDC_ACM) {
  482. usbh_serial_cdc_acm_free(serial);
  483. } else {
  484. usbh_serial_free(serial);
  485. }
  486. }
  487. #if defined(PKG_CHERRYUSB_HOST_CDC_ACM) || defined(RT_CHERRYUSB_HOST_CDC_ACM)
  488. void usbh_cdc_acm_callback(void *arg, int nbytes)
  489. {
  490. struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)arg;
  491. struct usbh_serial *serial;
  492. int ret;
  493. struct usbh_urb *urb = &cdc_acm_class->bulkin_urb;
  494. if (nbytes > 0) {
  495. serial = (struct usbh_serial *)cdc_acm_class->user_data;
  496. rt_ringbuffer_put(&serial->rx_rb, g_usbh_serial_cdc_acm_rx_buf[serial->minor], nbytes);
  497. if (serial->parent.rx_indicate) {
  498. serial->parent.rx_indicate(&serial->parent, nbytes);
  499. }
  500. usbh_bulk_urb_fill(urb, cdc_acm_class->hport, cdc_acm_class->bulkin, g_usbh_serial_cdc_acm_rx_buf[serial->minor], sizeof(g_usbh_serial_cdc_acm_rx_buf[serial->minor]), 0, usbh_cdc_acm_callback, cdc_acm_class);
  501. ret = usbh_submit_urb(urb);
  502. if (ret < 0) {
  503. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  504. }
  505. }
  506. }
  507. void usbh_cdc_acm_run(struct usbh_cdc_acm *cdc_acm_class)
  508. {
  509. struct usbh_serial *serial;
  510. int ret;
  511. struct usbh_urb *urb = &cdc_acm_class->bulkin_urb;
  512. serial = usbh_serial_cdc_acm_alloc(USBH_SERIAL_TYPE_CDC_ACM);
  513. cdc_acm_class->user_data = serial;
  514. usbh_serial_register(serial, cdc_acm_class);
  515. struct cdc_line_coding linecoding;
  516. linecoding.dwDTERate = 115200;
  517. linecoding.bDataBits = 8;
  518. linecoding.bParityType = 0;
  519. linecoding.bCharFormat = 0;
  520. usbh_cdc_acm_set_line_coding(cdc_acm_class, &linecoding);
  521. usbh_bulk_urb_fill(urb, cdc_acm_class->hport, cdc_acm_class->bulkin, g_usbh_serial_cdc_acm_rx_buf[serial->minor], sizeof(g_usbh_serial_cdc_acm_rx_buf[serial->minor]), 0, usbh_cdc_acm_callback, cdc_acm_class);
  522. ret = usbh_submit_urb(urb);
  523. if (ret < 0) {
  524. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  525. usbh_serial_unregister(serial);
  526. return;
  527. }
  528. }
  529. void usbh_cdc_acm_stop(struct usbh_cdc_acm *cdc_acm_class)
  530. {
  531. struct usbh_serial *serial;
  532. serial = (struct usbh_serial *)cdc_acm_class->user_data;
  533. usbh_serial_unregister(serial);
  534. }
  535. #endif
  536. #if defined(PKG_CHERRYUSB_HOST_FTDI) || defined(RT_CHERRYUSB_HOST_FTDI)
  537. void usbh_ftdi_callback(void *arg, int nbytes)
  538. {
  539. struct usbh_ftdi *ftdi_class = (struct usbh_ftdi *)arg;
  540. struct usbh_serial *serial;
  541. int ret;
  542. struct usbh_urb *urb = &ftdi_class->bulkin_urb;
  543. if (nbytes >= 2) {
  544. serial = (struct usbh_serial *)ftdi_class->user_data;
  545. nbytes -= 2; // Skip the first two bytes (header)
  546. rt_ringbuffer_put(&serial->rx_rb, &g_usbh_serial_vendor_rx_buf[serial->minor][2], nbytes);
  547. if (serial->parent.rx_indicate && nbytes) {
  548. serial->parent.rx_indicate(&serial->parent, nbytes);
  549. }
  550. usbh_bulk_urb_fill(urb, ftdi_class->hport, ftdi_class->bulkin, g_usbh_serial_vendor_rx_buf[serial->minor], sizeof(g_usbh_serial_vendor_rx_buf[serial->minor]), 0, usbh_ftdi_callback, ftdi_class);
  551. ret = usbh_submit_urb(urb);
  552. if (ret < 0) {
  553. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  554. }
  555. }
  556. }
  557. void usbh_ftdi_run(struct usbh_ftdi *ftdi_class)
  558. {
  559. struct usbh_serial *serial;
  560. int ret;
  561. struct usbh_urb *urb = &ftdi_class->bulkin_urb;
  562. serial = usbh_serial_alloc(USBH_SERIAL_TYPE_FTDI);
  563. ftdi_class->user_data = serial;
  564. usbh_serial_register(serial, ftdi_class);
  565. struct cdc_line_coding linecoding;
  566. linecoding.dwDTERate = 115200;
  567. linecoding.bDataBits = 8;
  568. linecoding.bParityType = 0;
  569. linecoding.bCharFormat = 0;
  570. usbh_ftdi_set_line_coding(ftdi_class, &linecoding);
  571. usbh_bulk_urb_fill(urb, ftdi_class->hport, ftdi_class->bulkin, g_usbh_serial_vendor_rx_buf[serial->minor], sizeof(g_usbh_serial_vendor_rx_buf[serial->minor]), 0, usbh_ftdi_callback, ftdi_class);
  572. ret = usbh_submit_urb(urb);
  573. if (ret < 0) {
  574. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  575. usbh_serial_unregister(serial);
  576. return;
  577. }
  578. }
  579. void usbh_ftdi_stop(struct usbh_ftdi *ftdi_class)
  580. {
  581. struct usbh_serial *serial;
  582. serial = (struct usbh_serial *)ftdi_class->user_data;
  583. usbh_serial_unregister(serial);
  584. }
  585. #endif
  586. #if defined(PKG_CHERRYUSB_HOST_CH34X) || defined(RT_CHERRYUSB_HOST_CH34X)
  587. void usbh_ch34x_callback(void *arg, int nbytes)
  588. {
  589. struct usbh_ch34x *ch34x_class = (struct usbh_ch34x *)arg;
  590. struct usbh_serial *serial;
  591. int ret;
  592. struct usbh_urb *urb = &ch34x_class->bulkin_urb;
  593. if (nbytes > 0) {
  594. serial = (struct usbh_serial *)ch34x_class->user_data;
  595. rt_ringbuffer_put(&serial->rx_rb, g_usbh_serial_vendor_rx_buf[serial->minor], nbytes);
  596. if (serial->parent.rx_indicate) {
  597. serial->parent.rx_indicate(&serial->parent, nbytes);
  598. }
  599. usbh_bulk_urb_fill(urb, ch34x_class->hport, ch34x_class->bulkin, g_usbh_serial_vendor_rx_buf[serial->minor], sizeof(g_usbh_serial_vendor_rx_buf[serial->minor]), 0, usbh_ch34x_callback, ch34x_class);
  600. ret = usbh_submit_urb(urb);
  601. if (ret < 0) {
  602. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  603. }
  604. }
  605. }
  606. void usbh_ch34x_run(struct usbh_ch34x *ch34x_class)
  607. {
  608. struct usbh_serial *serial;
  609. int ret;
  610. struct usbh_urb *urb = &ch34x_class->bulkin_urb;
  611. serial = usbh_serial_alloc(USBH_SERIAL_TYPE_CH34X);
  612. ch34x_class->user_data = serial;
  613. usbh_serial_register(serial, ch34x_class);
  614. struct cdc_line_coding linecoding;
  615. linecoding.dwDTERate = 115200;
  616. linecoding.bDataBits = 8;
  617. linecoding.bParityType = 0;
  618. linecoding.bCharFormat = 0;
  619. usbh_ch34x_set_line_coding(ch34x_class, &linecoding);
  620. usbh_bulk_urb_fill(urb, ch34x_class->hport, ch34x_class->bulkin, g_usbh_serial_vendor_rx_buf[serial->minor], sizeof(g_usbh_serial_vendor_rx_buf[serial->minor]), 0, usbh_ch34x_callback, ch34x_class);
  621. ret = usbh_submit_urb(urb);
  622. if (ret < 0) {
  623. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  624. usbh_serial_unregister(serial);
  625. return;
  626. }
  627. }
  628. void usbh_ch34x_stop(struct usbh_ch34x *ch34x_class)
  629. {
  630. struct usbh_serial *serial;
  631. serial = (struct usbh_serial *)ch34x_class->user_data;
  632. usbh_serial_unregister(serial);
  633. }
  634. #endif
  635. #if defined(PKG_CHERRYUSB_HOST_CP210X) || defined(RT_CHERRYUSB_HOST_CP210X)
  636. void usbh_cp210x_callback(void *arg, int nbytes)
  637. {
  638. struct usbh_cp210x *cp210x_class = (struct usbh_cp210x *)arg;
  639. struct usbh_serial *serial;
  640. int ret;
  641. struct usbh_urb *urb = &cp210x_class->bulkin_urb;
  642. if (nbytes > 0) {
  643. serial = (struct usbh_serial *)cp210x_class->user_data;
  644. rt_ringbuffer_put(&serial->rx_rb, g_usbh_serial_vendor_rx_buf[serial->minor], nbytes);
  645. if (serial->parent.rx_indicate) {
  646. serial->parent.rx_indicate(&serial->parent, nbytes);
  647. }
  648. usbh_bulk_urb_fill(urb, cp210x_class->hport, cp210x_class->bulkin, g_usbh_serial_vendor_rx_buf[serial->minor], sizeof(g_usbh_serial_vendor_rx_buf[serial->minor]), 0, usbh_cp210x_callback, cp210x_class);
  649. ret = usbh_submit_urb(urb);
  650. if (ret < 0) {
  651. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  652. }
  653. }
  654. }
  655. void usbh_cp210x_run(struct usbh_cp210x *cp210x_class)
  656. {
  657. struct usbh_serial *serial;
  658. int ret;
  659. struct usbh_urb *urb = &cp210x_class->bulkin_urb;
  660. serial = usbh_serial_alloc(USBH_SERIAL_TYPE_CP210X);
  661. cp210x_class->user_data = serial;
  662. usbh_serial_register(serial, cp210x_class);
  663. struct cdc_line_coding linecoding;
  664. linecoding.dwDTERate = 115200;
  665. linecoding.bDataBits = 8;
  666. linecoding.bParityType = 0;
  667. linecoding.bCharFormat = 0;
  668. usbh_cp210x_set_line_coding(cp210x_class, &linecoding);
  669. usbh_bulk_urb_fill(urb, cp210x_class->hport, cp210x_class->bulkin, g_usbh_serial_vendor_rx_buf[serial->minor], sizeof(g_usbh_serial_vendor_rx_buf[serial->minor]), 0, usbh_cp210x_callback, cp210x_class);
  670. ret = usbh_submit_urb(urb);
  671. if (ret < 0) {
  672. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  673. usbh_serial_unregister(serial);
  674. return;
  675. }
  676. }
  677. void usbh_cp210x_stop(struct usbh_cp210x *cp210x_class)
  678. {
  679. struct usbh_serial *serial;
  680. serial = (struct usbh_serial *)cp210x_class->user_data;
  681. usbh_serial_unregister(serial);
  682. }
  683. #endif
  684. #if defined(PKG_CHERRYUSB_HOST_PL2303) || defined(RT_CHERRYUSB_HOST_PL2303)
  685. void usbh_pl2303_callback(void *arg, int nbytes)
  686. {
  687. struct usbh_pl2303 *pl2303_class = (struct usbh_pl2303 *)arg;
  688. struct usbh_serial *serial;
  689. int ret;
  690. struct usbh_urb *urb = &pl2303_class->bulkin_urb;
  691. if (nbytes > 0) {
  692. serial = (struct usbh_serial *)pl2303_class->user_data;
  693. rt_ringbuffer_put(&serial->rx_rb, g_usbh_serial_vendor_rx_buf[serial->minor], nbytes);
  694. if (serial->parent.rx_indicate) {
  695. serial->parent.rx_indicate(&serial->parent, nbytes);
  696. }
  697. usbh_bulk_urb_fill(urb, pl2303_class->hport, pl2303_class->bulkin, g_usbh_serial_vendor_rx_buf[serial->minor], sizeof(g_usbh_serial_vendor_rx_buf[serial->minor]), 0, usbh_pl2303_callback, pl2303_class);
  698. ret = usbh_submit_urb(urb);
  699. if (ret < 0) {
  700. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  701. }
  702. }
  703. }
  704. void usbh_pl2303_run(struct usbh_pl2303 *pl2303_class)
  705. {
  706. struct usbh_serial *serial;
  707. int ret;
  708. struct usbh_urb *urb = &pl2303_class->bulkin_urb;
  709. serial = usbh_serial_alloc(USBH_SERIAL_TYPE_PL2303);
  710. pl2303_class->user_data = serial;
  711. usbh_serial_register(serial, pl2303_class);
  712. struct cdc_line_coding linecoding;
  713. linecoding.dwDTERate = 115200;
  714. linecoding.bDataBits = 8;
  715. linecoding.bParityType = 0;
  716. linecoding.bCharFormat = 0;
  717. usbh_pl2303_set_line_coding(pl2303_class, &linecoding);
  718. usbh_bulk_urb_fill(urb, pl2303_class->hport, pl2303_class->bulkin, g_usbh_serial_vendor_rx_buf[serial->minor], sizeof(g_usbh_serial_vendor_rx_buf[serial->minor]), 0, usbh_pl2303_callback, pl2303_class);
  719. ret = usbh_submit_urb(urb);
  720. if (ret < 0) {
  721. USB_LOG_ERR("usbh_submit_urb failed: %d\n", ret);
  722. usbh_serial_unregister(serial);
  723. return;
  724. }
  725. }
  726. void usbh_pl2303_stop(struct usbh_pl2303 *pl2303_class)
  727. {
  728. struct usbh_serial *serial;
  729. serial = (struct usbh_serial *)pl2303_class->user_data;
  730. usbh_serial_unregister(serial);
  731. }
  732. #endif