serial_tty.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-11-21 Shell init ver.
  9. */
  10. #define DBG_TAG "drivers.serial"
  11. #define DBG_LVL DBG_INFO
  12. #include <rtdbg.h>
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include <terminal/terminal.h>
  17. #define TTY_NAME_PREFIX "S" /* (S)erial */
  18. #define LWP_TTY_WORKQUEUE_PRIORITY 3
  19. struct serial_tty_context
  20. {
  21. struct rt_serial_device *parent;
  22. struct rt_device_notify backup_notify;
  23. struct rt_work work;
  24. };
  25. static struct rt_workqueue *_ttyworkq; /* system work queue */
  26. #ifndef RT_USING_DM
  27. static rt_atomic_t _device_id_counter = 0;
  28. static long get_dec_digits(rt_ubase_t val)
  29. {
  30. long result = 1;
  31. while (1)
  32. {
  33. if (val < 10)
  34. return result;
  35. if (val < 100)
  36. return result + 1;
  37. if (val < 1000)
  38. return result + 2;
  39. if (val < 10000)
  40. return result + 3;
  41. val /= 10000U;
  42. result += 4;
  43. }
  44. return result;
  45. }
  46. #endif
  47. static char *alloc_device_name(struct rt_serial_device *serial)
  48. {
  49. char *tty_dev_name;
  50. #ifdef RT_USING_DM
  51. char *serial_name = serial->parent.parent.name;
  52. /*
  53. * if RT_USING_DM is defined, the name of the serial device
  54. * must be obtained using the serial_dev_set_name function,
  55. * and it should begin with "uart".
  56. */
  57. RT_ASSERT((strlen(serial_name) > strlen("uart")) && (strncmp(serial_name, "uart", 4) == 0));
  58. long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
  59. + strlen(serial_name + sizeof("uart") - 1) /* suffix of serial device name*/
  60. + 1; /* tailing \0 */
  61. tty_dev_name = rt_malloc(digits_len);
  62. if (tty_dev_name)
  63. rt_sprintf(tty_dev_name, "%s%s", TTY_NAME_PREFIX, serial_name + sizeof("uart") - 1);
  64. #else
  65. RT_UNUSED(serial);
  66. unsigned int devid = rt_atomic_add(&_device_id_counter, 1);
  67. long digits_len = (sizeof(TTY_NAME_PREFIX) - 1) /* raw prefix */
  68. + get_dec_digits(devid) + 1; /* tailing \0 */
  69. tty_dev_name = rt_malloc(digits_len);
  70. if (tty_dev_name)
  71. rt_sprintf(tty_dev_name, "%s%u", TTY_NAME_PREFIX, devid);
  72. #endif
  73. return tty_dev_name;
  74. }
  75. #ifdef LWP_DEBUG_INIT
  76. static volatile int _early_input = 0;
  77. static void _set_debug(rt_device_t dev, rt_size_t size);
  78. RT_OBJECT_HOOKLIST_DEFINE_NODE(rt_hw_serial_rxind, _set_debug_node, _set_debug);
  79. static void _set_debug(rt_device_t dev, rt_size_t size)
  80. {
  81. rt_list_remove(&_set_debug_node.list_node);
  82. _early_input = 1;
  83. }
  84. static void _setup_debug_rxind_hook(void)
  85. {
  86. rt_hw_serial_rxind_sethook(&_set_debug_node);
  87. }
  88. int lwp_startup_debug_request(void)
  89. {
  90. return _early_input;
  91. }
  92. #else /* !LWP_DEBUG_INIT */
  93. static void _setup_debug_rxind_hook(void)
  94. {
  95. return ;
  96. }
  97. #endif /* LWP_DEBUG_INIT */
  98. static rt_err_t _serial_ty_bypass(struct rt_serial_device* serial, char ch,void *data)
  99. {
  100. lwp_tty_t tp;
  101. tp = (lwp_tty_t)data;
  102. tty_lock(tp);
  103. ttydisc_rint(tp, ch, 0);
  104. ttydisc_rint_done(tp);
  105. tty_unlock(tp);
  106. return RT_EOK;
  107. }
  108. rt_inline void _setup_serial(struct rt_serial_device* serial, lwp_tty_t tp,
  109. struct serial_tty_context *softc)
  110. {
  111. rt_bypass_lower_register(serial, "tty", RT_BYPASS_PROTECT_LEVEL_1, _serial_ty_bypass, (void *)tp);
  112. }
  113. rt_inline void _restore_serial(struct rt_serial_device *serial, lwp_tty_t tp,
  114. struct serial_tty_context *softc)
  115. {
  116. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &softc->backup_notify);
  117. }
  118. static void _serial_tty_set_speed(struct lwp_tty *tp)
  119. {
  120. struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
  121. struct rt_serial_device *serial;
  122. struct termios serial_hw_config;
  123. RT_ASSERT(softc);
  124. serial = softc->parent;
  125. rt_device_control(&(serial->parent), TCGETS, &serial_hw_config);
  126. tp->t_termios_init_in.c_cflag |= serial_hw_config.c_cflag;
  127. tp->t_termios_init_in.__c_ispeed = tp->t_termios_init_in.__c_ospeed = cfgetospeed(&tp->t_termios_init_in);
  128. }
  129. static int _serial_isbusy(struct rt_serial_device *serial)
  130. {
  131. rt_thread_t user_thread = rt_console_current_user();
  132. rt_thread_t self_thread = rt_thread_self();
  133. return rt_console_get_device() == &serial->parent &&
  134. (user_thread != RT_NULL && user_thread != self_thread);
  135. }
  136. static void serial_tty_outwakeup(struct lwp_tty *tp)
  137. {
  138. char out_char;
  139. int len;
  140. struct serial_tty_context *context = tty_softc(tp);
  141. struct rt_serial_device *device;
  142. if (!context || !context->parent)
  143. {
  144. LOG_E("%s: Data corruption", __func__);
  145. return;
  146. }
  147. device = context->parent;
  148. if (_serial_isbusy(device))
  149. {
  150. return ;
  151. }
  152. while ((len = ttydisc_getc(tp, &out_char, sizeof(out_char))) != 0)
  153. {
  154. device->ops->putc(device, out_char);
  155. /* discard remaining if emergency output is happened */
  156. if (_serial_isbusy(device))
  157. {
  158. break;
  159. }
  160. }
  161. }
  162. static int serial_tty_open(struct lwp_tty *tp)
  163. {
  164. struct serial_tty_context *softc;
  165. struct rt_serial_device *serial;
  166. rt_err_t error;
  167. int oflags;
  168. softc = tty_softc(tp);
  169. serial = softc->parent;
  170. LOG_D("%s", __func__);
  171. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
  172. error = rt_device_open(&serial->parent, oflags);
  173. if (!error)
  174. {
  175. /**
  176. * to avoid driver accesssing null data,
  177. * these are setup only after tty is registered
  178. */
  179. _setup_serial(serial, tp, softc);
  180. }
  181. return error;
  182. }
  183. static void serial_tty_close(struct lwp_tty *tp)
  184. {
  185. struct serial_tty_context *softc;
  186. struct rt_serial_device *serial;
  187. softc = tty_softc(tp);
  188. serial = softc->parent;
  189. LOG_D("%s", __func__);
  190. rt_bypass_lower_unregister(serial, RT_BYPASS_PROTECT_LEVEL_1);
  191. rt_device_close(&serial->parent);
  192. }
  193. static int serial_tty_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data,
  194. struct rt_thread *td)
  195. {
  196. int error;
  197. switch (cmd)
  198. {
  199. default:
  200. /**
  201. * Note: for the most case, we don't let serial layer handle ioctl,
  202. * for that they can't act properly regarding to the process
  203. * management system, since it is unawared of that. So a ENOSYS is
  204. * returned and caused the TTY layer to handle ioctl itself.
  205. */
  206. error = -ENOSYS;
  207. break;
  208. }
  209. return error;
  210. }
  211. static int serial_tty_param(struct lwp_tty *tp, struct termios *t)
  212. {
  213. struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
  214. struct rt_serial_device *serial;
  215. RT_ASSERT(softc);
  216. serial = softc->parent;
  217. if (!tty_opened(tp))
  218. {
  219. /**
  220. * skip configure on open since all configs are copied from the current
  221. * configuration on device. So we don't bother to set it back to device
  222. * again.
  223. */
  224. return RT_EOK;
  225. }
  226. cfsetispeed(t, t->__c_ispeed);
  227. return rt_device_control(&(serial->parent), TCSETS, t);
  228. }
  229. static struct lwp_ttydevsw serial_ttydevsw = {
  230. .tsw_open = serial_tty_open,
  231. .tsw_close = serial_tty_close,
  232. .tsw_ioctl = serial_tty_ioctl,
  233. .tsw_param = serial_tty_param,
  234. .tsw_outwakeup = serial_tty_outwakeup,
  235. };
  236. rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
  237. {
  238. rt_err_t rc;
  239. lwp_tty_t tty;
  240. char *dev_name;
  241. struct serial_tty_context *softc;
  242. if (serial->rx_notify.dev)
  243. {
  244. return -RT_EBUSY;
  245. }
  246. softc = rt_malloc(sizeof(struct serial_tty_context));
  247. if (softc)
  248. {
  249. dev_name = alloc_device_name(serial);
  250. if (dev_name)
  251. {
  252. softc->parent = serial;
  253. tty = lwp_tty_create(&serial_ttydevsw, softc);
  254. if (tty)
  255. {
  256. _serial_tty_set_speed(tty);
  257. rc = lwp_tty_register(tty, dev_name);
  258. if (rc != RT_EOK)
  259. {
  260. rt_free(tty);
  261. rt_free(softc);
  262. }
  263. }
  264. else
  265. {
  266. rt_free(softc);
  267. rc = -RT_ENOMEM;
  268. }
  269. rt_free(dev_name);
  270. }
  271. else
  272. {
  273. rt_free(softc);
  274. rc = -RT_ENOMEM;
  275. }
  276. }
  277. else
  278. {
  279. rc = -RT_ENOMEM;
  280. }
  281. return rc;
  282. }
  283. rt_err_t rt_hw_serial_unregister_tty(struct rt_serial_device *serial)
  284. {
  285. rt_device_t tty_dev;
  286. lwp_tty_t tp;
  287. struct serial_tty_context *softc;
  288. tty_dev = serial->rx_notify.dev;
  289. tp = rt_container_of(tty_dev, struct lwp_tty, parent);
  290. /* restore serial setting */
  291. softc = tty_softc(tp);
  292. serial->rx_notify = softc->backup_notify;
  293. tty_lock(tp);
  294. tty_rel_gone(tp);
  295. /* device unregister? */
  296. rt_device_destroy(&tp->parent);
  297. /* resource free? */
  298. lwp_tty_delete(tp);
  299. return RT_EOK;
  300. }
  301. static int _tty_workqueue_init(void)
  302. {
  303. if (_ttyworkq != RT_NULL)
  304. return RT_EOK;
  305. _ttyworkq = rt_workqueue_create("ttyworkq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
  306. LWP_TTY_WORKQUEUE_PRIORITY);
  307. RT_ASSERT(_ttyworkq != RT_NULL);
  308. _setup_debug_rxind_hook();
  309. return RT_EOK;
  310. }
  311. INIT_PREV_EXPORT(_tty_workqueue_init);
  312. static rt_err_t _match_tty_iter(struct rt_object *obj, void *data)
  313. {
  314. rt_device_t target = *(rt_device_t *)data;
  315. rt_device_t device = rt_container_of(obj, struct rt_device, parent);
  316. if (device->type == RT_Device_Class_Char)
  317. {
  318. lwp_tty_t tp;
  319. if (rt_strncmp(obj->name, "tty"TTY_NAME_PREFIX,
  320. sizeof("tty"TTY_NAME_PREFIX) - 1) == 0)
  321. {
  322. struct serial_tty_context *softc;
  323. tp = rt_container_of(device, struct lwp_tty, parent);
  324. softc = tty_softc(tp);
  325. if (&softc->parent->parent == target)
  326. {
  327. /* matched, early return */
  328. *(rt_device_t *)data = device;
  329. return 1;
  330. }
  331. }
  332. }
  333. return RT_EOK;
  334. }
  335. /**
  336. * @brief The default console is only a backup device with lowest priority.
  337. * It's always recommended to scratch the console from the boot arguments.
  338. * And dont forget to register the device with a higher priority.
  339. */
  340. static int _default_console_setup(void)
  341. {
  342. rt_err_t rc;
  343. rt_device_t bakdev;
  344. rt_device_t ttydev;
  345. bakdev = rt_console_get_device();
  346. if (!bakdev)
  347. {
  348. return -RT_ENOENT;
  349. }
  350. ttydev = bakdev;
  351. rt_object_for_each(RT_Object_Class_Device, _match_tty_iter, &ttydev);
  352. if (ttydev != bakdev)
  353. {
  354. LOG_I("Using /dev/%.*s as default console", RT_NAME_MAX, ttydev->parent.name);
  355. lwp_console_register_backend(ttydev, LWP_CONSOLE_LOWEST_PRIOR);
  356. rc = RT_EOK;
  357. }
  358. else
  359. {
  360. rc = -RT_EINVAL;
  361. }
  362. return rc;
  363. }
  364. INIT_COMPONENT_EXPORT(_default_console_setup);