usbh_serial.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. * Copyright (c) 2025, MDLZCOOL
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. */
  7. #ifndef USBH_SERIAL_H
  8. #define USBH_SERIAL_H
  9. #include "usb_cdc.h"
  10. #define USBH_SERIAL_CTRL_NOCACHE_OFFSET 0
  11. #define USBH_SERIAL_CTRL_NOCACHE_SIZE 32
  12. #define USBH_SERIAL_INT_NOCACHE_OFFSET USB_ALIGN_UP(USBH_SERIAL_CTRL_NOCACHE_SIZE, CONFIG_USB_ALIGN_SIZE)
  13. #define USBH_SERIAL_INT_NOCACHE_SIZE 32
  14. #define USBH_SERIAL_RX_NOCACHE_OFFSET USB_ALIGN_UP((USBH_SERIAL_INT_NOCACHE_OFFSET + USBH_SERIAL_INT_NOCACHE_SIZE), CONFIG_USB_ALIGN_SIZE)
  15. #define USBH_SERIAL_RX_NOCACHE_SIZE 512
  16. #define USBH_SERIAL_RX2_NOCACHE_OFFSET USB_ALIGN_UP((USBH_SERIAL_RX_NOCACHE_OFFSET + USBH_SERIAL_RX_NOCACHE_SIZE), CONFIG_USB_ALIGN_SIZE)
  17. #define USBH_SERIAL_RX2_NOCACHE_SIZE 512
  18. #define USBH_SERIAL_DATABITS_5 5
  19. #define USBH_SERIAL_DATABITS_6 6
  20. #define USBH_SERIAL_DATABITS_7 7
  21. #define USBH_SERIAL_DATABITS_8 8
  22. #define USBH_SERIAL_PARITY_NONE 0
  23. #define USBH_SERIAL_PARITY_ODD 1
  24. #define USBH_SERIAL_PARITY_EVEN 2
  25. #define USBH_SERIAL_PARITY_MARK 3
  26. #define USBH_SERIAL_PARITY_SPACE 4
  27. #define USBH_SERIAL_STOPBITS_1 0
  28. #define USBH_SERIAL_STOPBITS_1_5 1
  29. #define USBH_SERIAL_STOPBITS_2 2
  30. /* modem lines */
  31. #define USBH_SERIAL_TIOCM_LE 0x001 /* line enable */
  32. #define USBH_SERIAL_TIOCM_DTR 0x002 /* data terminal ready */
  33. #define USBH_SERIAL_TIOCM_RTS 0x004 /* request to send */
  34. #define USBH_SERIAL_TIOCM_ST 0x010 /* secondary transmit */
  35. #define USBH_SERIAL_TIOCM_SR 0x020 /* secondary receive */
  36. #define USBH_SERIAL_TIOCM_CTS 0x040 /* clear to send */
  37. #define USBH_SERIAL_TIOCM_CAR 0x100 /* carrier detect */
  38. #define USBH_SERIAL_TIOCM_CD USBH_SERIAL_TIOCM_CAR
  39. #define USBH_SERIAL_TIOCM_RNG 0x200 /* ring */
  40. #define USBH_SERIAL_TIOCM_RI USBH_SERIAL_TIOCM_RNG
  41. #define USBH_SERIAL_TIOCM_DSR 0x400 /* data set ready */
  42. #define USBH_SERIAL_TIOCM_OUT1 0x2000
  43. #define USBH_SERIAL_TIOCM_OUT2 0x4000
  44. #define USBH_SERIAL_TIOCM_LOOP 0x8000
  45. #define USBH_SERIAL_O_RDONLY 0x0000 /* open for reading only */
  46. #define USBH_SERIAL_O_WRONLY 0x0001 /* open for writing only */
  47. #define USBH_SERIAL_O_RDWR 0x0002 /* open for reading and writing */
  48. #define USBH_SERIAL_O_ACCMODE 0x0003 /* mask for above modes, from 4.4BSD https://minnie.tuhs.org/cgi-bin/utree.pl?file=4.4BSD/usr/include/sys/fcntl.h */
  49. #define USBH_SERIAL_O_NONBLOCK 0x0004 /* non-blocking I/O, from BSD apple https://opensource.apple.com/source/xnu/xnu-1228.0.2/bsd/sys/fcntl.h */
  50. #define USBH_SERIAL_CMD_SET_ATTR 0
  51. #define USBH_SERIAL_CMD_GET_ATTR 1
  52. #define USBH_SERIAL_CMD_IOCMBIS 2
  53. #define USBH_SERIAL_CMD_IOCMBIC 3
  54. #define USBH_SERIAL_CMD_TIOCMSET 4
  55. #define USBH_SERIAL_CMD_TIOCMGET 5
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. typedef struct {
  60. uint32_t in; /*!< Define the write pointer. */
  61. uint32_t out; /*!< Define the read pointer. */
  62. uint32_t mask; /*!< Define the write and read pointer mask. */
  63. void *pool; /*!< Define the memory pointer. */
  64. } usbh_serial_ringbuf_t;
  65. /*
  66. * Counters of the input lines (CTS, DSR, RI, CD) interrupts
  67. */
  68. struct usbh_serial_async_icount {
  69. uint32_t cts, dsr, rng, dcd, tx, rx;
  70. uint32_t frame, parity, overrun, brk;
  71. uint32_t buf_overrun;
  72. };
  73. struct usbh_serial_termios {
  74. uint32_t baudrate;
  75. uint8_t databits;
  76. uint8_t parity;
  77. uint8_t stopbits;
  78. bool rtscts; /* hardware flow control */
  79. uint32_t rx_timeout;
  80. };
  81. struct usbh_serial;
  82. typedef void (*usbh_serial_rx_complete_callback_t)(struct usbh_serial *serial, int nbytes);
  83. /**
  84. * @brief Serial Driver Operations
  85. */
  86. struct usbh_serial_driver {
  87. const char *driver_name;
  88. uint8_t ignore_tx_header;
  89. uint8_t ignore_rx_header;
  90. int (*attach)(struct usbh_serial *serial);
  91. void (*detach)(struct usbh_serial *serial);
  92. int (*open)(struct usbh_serial *serial);
  93. void (*close)(struct usbh_serial *serial);
  94. int (*set_flow_control)(struct usbh_serial *serial, bool enable);
  95. int (*set_line_coding)(struct usbh_serial *serial, struct cdc_line_coding *line_coding);
  96. int (*get_line_coding)(struct usbh_serial *serial, struct cdc_line_coding *line_coding);
  97. int (*set_line_state)(struct usbh_serial *serial, bool dtr, bool rts);
  98. int (*get_modem_status)(struct usbh_serial *serial);
  99. };
  100. /**
  101. * @brief Serial Instance
  102. */
  103. struct usbh_serial {
  104. struct usbh_hubport *hport;
  105. uint8_t intf; /* Interface Number */
  106. int minor; /* Serial Port Number (/dev/ttyUSBx or /dev/ttyACMx) */
  107. int cdc_minor; /* Serial Port Number (/dev/ttyACMx) */
  108. uint8_t *iobuffer; /* I/O buffer for serial transfers */
  109. uint8_t ref_count; /* Reference Count */
  110. uint32_t open_flags;
  111. uint32_t rx_timeout_ms;
  112. struct cdc_line_coding line_coding;
  113. uint16_t line_state;
  114. bool rtscts; /* hardware flow control */
  115. struct usbh_serial_async_icount iocount;
  116. struct usb_endpoint_descriptor *bulkin; /* Bulk IN endpoint */
  117. struct usb_endpoint_descriptor *bulkout; /* Bulk OUT endpoint */
  118. struct usbh_urb bulkout_urb;
  119. struct usbh_urb bulkin_urb;
  120. const struct usbh_serial_driver *driver;
  121. usbh_serial_ringbuf_t rx_rb;
  122. uint8_t rx_rb_pool[CONFIG_USBHOST_SERIAL_RX_SIZE];
  123. usb_osal_sem_t rx_complete_sem;
  124. uint8_t rx_buf_index;
  125. int rx_errorcode;
  126. usbh_serial_rx_complete_callback_t rx_complete_callback;
  127. void *priv; /* Private Data */
  128. void *user_data; /* User Data */
  129. };
  130. /* internal api */
  131. struct usbh_serial *usbh_serial_probe(struct usbh_hubport *hport, uint8_t intf, const struct usbh_serial_driver *driver);
  132. void usbh_serial_remove(struct usbh_serial *serial);
  133. /* public api */
  134. struct usbh_serial *usbh_serial_open(const char *devname, uint32_t open_flags);
  135. int usbh_serial_close(struct usbh_serial *serial);
  136. int usbh_serial_control(struct usbh_serial *serial, int cmd, void *arg);
  137. int usbh_serial_write(struct usbh_serial *serial, const void *buffer, uint32_t buflen);
  138. int usbh_serial_read(struct usbh_serial *serial, void *buffer, uint32_t buflen);
  139. /* cdc only api */
  140. int usbh_serial_cdc_write_async(struct usbh_serial *serial, uint8_t *buffer, uint32_t buflen, usbh_complete_callback_t complete, void *arg);
  141. int usbh_serial_cdc_read_async(struct usbh_serial *serial, uint8_t *buffer, uint32_t buflen, usbh_complete_callback_t complete, void *arg);
  142. /* public weak api */
  143. void usbh_serial_run(struct usbh_serial *serial);
  144. void usbh_serial_stop(struct usbh_serial *serial);
  145. int usbh_serial(int argc, char **argv);
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149. #endif /* USBH_SERIAL_H */