serial_tty.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 void _tty_rx_notify(struct rt_device *device)
  99. {
  100. lwp_tty_t tp;
  101. struct serial_tty_context *softc;
  102. tp = rt_container_of(device, struct lwp_tty, parent);
  103. RT_ASSERT(tp);
  104. softc = tty_softc(tp);
  105. if (_ttyworkq)
  106. rt_workqueue_submit_work(_ttyworkq, &softc->work, 0);
  107. }
  108. static void _tty_rx_worker(struct rt_work *work, void *data)
  109. {
  110. char input;
  111. rt_ssize_t readbytes;
  112. lwp_tty_t tp = data;
  113. struct serial_tty_context *softc;
  114. struct rt_serial_device *serial;
  115. tty_lock(tp);
  116. while (1)
  117. {
  118. softc = tty_softc(tp);
  119. serial = softc->parent;
  120. readbytes = rt_device_read(&serial->parent, -1, &input, 1);
  121. if (readbytes != 1)
  122. {
  123. break;
  124. }
  125. ttydisc_rint(tp, input, 0);
  126. }
  127. ttydisc_rint_done(tp);
  128. tty_unlock(tp);
  129. }
  130. rt_inline void _setup_serial(struct rt_serial_device *serial, lwp_tty_t tp,
  131. struct serial_tty_context *softc)
  132. {
  133. struct rt_device_notify notify;
  134. softc->backup_notify = serial->rx_notify;
  135. notify.dev = &tp->parent;
  136. notify.notify = _tty_rx_notify;
  137. rt_device_init(&serial->parent);
  138. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &notify);
  139. }
  140. rt_inline void _restore_serial(struct rt_serial_device *serial, lwp_tty_t tp,
  141. struct serial_tty_context *softc)
  142. {
  143. rt_device_control(&serial->parent, RT_DEVICE_CTRL_NOTIFY_SET, &softc->backup_notify);
  144. }
  145. static void _serial_tty_set_speed(struct lwp_tty *tp)
  146. {
  147. struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
  148. struct rt_serial_device *serial;
  149. struct termios serial_hw_config;
  150. RT_ASSERT(softc);
  151. serial = softc->parent;
  152. rt_device_control(&(serial->parent), TCGETS, &serial_hw_config);
  153. tp->t_termios_init_in.c_cflag |= serial_hw_config.c_cflag;
  154. tp->t_termios_init_in.__c_ispeed = tp->t_termios_init_in.__c_ospeed = cfgetospeed(&tp->t_termios_init_in);
  155. }
  156. static int _serial_isbusy(struct rt_serial_device *serial)
  157. {
  158. rt_thread_t user_thread = rt_console_current_user();
  159. rt_thread_t self_thread = rt_thread_self();
  160. return rt_console_get_device() == &serial->parent &&
  161. (user_thread != RT_NULL && user_thread != self_thread);
  162. }
  163. static void serial_tty_outwakeup(struct lwp_tty *tp)
  164. {
  165. char out_char;
  166. int len;
  167. struct serial_tty_context *context = tty_softc(tp);
  168. struct rt_serial_device *device;
  169. if (!context || !context->parent)
  170. {
  171. LOG_E("%s: Data corruption", __func__);
  172. return;
  173. }
  174. device = context->parent;
  175. if (_serial_isbusy(device))
  176. {
  177. return ;
  178. }
  179. while ((len = ttydisc_getc(tp, &out_char, sizeof(out_char))) != 0)
  180. {
  181. device->ops->putc(device, out_char);
  182. /* discard remaining if emergency output is happened */
  183. if (_serial_isbusy(device))
  184. {
  185. break;
  186. }
  187. }
  188. }
  189. static int serial_tty_open(struct lwp_tty *tp)
  190. {
  191. struct serial_tty_context *softc;
  192. struct rt_serial_device *serial;
  193. rt_err_t error;
  194. int oflags;
  195. softc = tty_softc(tp);
  196. serial = softc->parent;
  197. LOG_D("%s", __func__);
  198. rt_device_control(&serial->parent, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);
  199. error = rt_device_open(&serial->parent, oflags);
  200. if (!error)
  201. {
  202. /**
  203. * to avoid driver accesssing null data,
  204. * these are setup only after tty is registered
  205. */
  206. _setup_serial(serial, tp, softc);
  207. }
  208. return error;
  209. }
  210. static void serial_tty_close(struct lwp_tty *tp)
  211. {
  212. struct serial_tty_context *softc;
  213. struct rt_serial_device *serial;
  214. softc = tty_softc(tp);
  215. serial = softc->parent;
  216. LOG_D("%s", __func__);
  217. _restore_serial(serial, tp, softc);
  218. rt_device_close(&serial->parent);
  219. }
  220. static int serial_tty_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data,
  221. struct rt_thread *td)
  222. {
  223. int error;
  224. switch (cmd)
  225. {
  226. default:
  227. /**
  228. * Note: for the most case, we don't let serial layer handle ioctl,
  229. * for that they can't act properly regarding to the process
  230. * management system, since it is unawared of that. So a ENOSYS is
  231. * returned and caused the TTY layer to handle ioctl itself.
  232. */
  233. error = -ENOSYS;
  234. break;
  235. }
  236. return error;
  237. }
  238. static int serial_tty_param(struct lwp_tty *tp, struct termios *t)
  239. {
  240. struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
  241. struct rt_serial_device *serial;
  242. RT_ASSERT(softc);
  243. serial = softc->parent;
  244. if (!tty_opened(tp))
  245. {
  246. /**
  247. * skip configure on open since all configs are copied from the current
  248. * configuration on device. So we don't bother to set it back to device
  249. * again.
  250. */
  251. return RT_EOK;
  252. }
  253. cfsetispeed(t, t->__c_ispeed);
  254. return rt_device_control(&(serial->parent), TCSETS, t);
  255. }
  256. static struct lwp_ttydevsw serial_ttydevsw = {
  257. .tsw_open = serial_tty_open,
  258. .tsw_close = serial_tty_close,
  259. .tsw_ioctl = serial_tty_ioctl,
  260. .tsw_param = serial_tty_param,
  261. .tsw_outwakeup = serial_tty_outwakeup,
  262. };
  263. rt_err_t rt_hw_serial_register_tty(struct rt_serial_device *serial)
  264. {
  265. rt_err_t rc;
  266. lwp_tty_t tty;
  267. char *dev_name;
  268. struct serial_tty_context *softc;
  269. if (serial->rx_notify.dev)
  270. {
  271. return -RT_EBUSY;
  272. }
  273. softc = rt_malloc(sizeof(struct serial_tty_context));
  274. if (softc)
  275. {
  276. dev_name = alloc_device_name(serial);
  277. if (dev_name)
  278. {
  279. softc->parent = serial;
  280. tty = lwp_tty_create(&serial_ttydevsw, softc);
  281. if (tty)
  282. {
  283. _serial_tty_set_speed(tty);
  284. rc = lwp_tty_register(tty, dev_name);
  285. rt_work_init(&softc->work, _tty_rx_worker, tty);
  286. if (rc != RT_EOK)
  287. {
  288. rt_free(tty);
  289. rt_free(softc);
  290. }
  291. }
  292. else
  293. {
  294. rt_free(softc);
  295. rc = -RT_ENOMEM;
  296. }
  297. rt_free(dev_name);
  298. }
  299. else
  300. {
  301. rt_free(softc);
  302. rc = -RT_ENOMEM;
  303. }
  304. }
  305. else
  306. {
  307. rc = -RT_ENOMEM;
  308. }
  309. return rc;
  310. }
  311. rt_err_t rt_hw_serial_unregister_tty(struct rt_serial_device *serial)
  312. {
  313. rt_device_t tty_dev;
  314. lwp_tty_t tp;
  315. struct serial_tty_context *softc;
  316. tty_dev = serial->rx_notify.dev;
  317. tp = rt_container_of(tty_dev, struct lwp_tty, parent);
  318. /* restore serial setting */
  319. softc = tty_softc(tp);
  320. serial->rx_notify = softc->backup_notify;
  321. tty_lock(tp);
  322. tty_rel_gone(tp);
  323. /* device unregister? */
  324. rt_device_destroy(&tp->parent);
  325. /* resource free? */
  326. lwp_tty_delete(tp);
  327. return RT_EOK;
  328. }
  329. static int _tty_workqueue_init(void)
  330. {
  331. if (_ttyworkq != RT_NULL)
  332. return RT_EOK;
  333. _ttyworkq = rt_workqueue_create("ttyworkq", RT_SYSTEM_WORKQUEUE_STACKSIZE,
  334. LWP_TTY_WORKQUEUE_PRIORITY);
  335. RT_ASSERT(_ttyworkq != RT_NULL);
  336. _setup_debug_rxind_hook();
  337. return RT_EOK;
  338. }
  339. INIT_PREV_EXPORT(_tty_workqueue_init);
  340. static rt_err_t _match_tty_iter(struct rt_object *obj, void *data)
  341. {
  342. rt_device_t target = *(rt_device_t *)data;
  343. rt_device_t device = rt_container_of(obj, struct rt_device, parent);
  344. if (device->type == RT_Device_Class_Char)
  345. {
  346. lwp_tty_t tp;
  347. if (rt_strncmp(obj->name, "tty"TTY_NAME_PREFIX,
  348. sizeof("tty"TTY_NAME_PREFIX) - 1) == 0)
  349. {
  350. struct serial_tty_context *softc;
  351. tp = rt_container_of(device, struct lwp_tty, parent);
  352. softc = tty_softc(tp);
  353. if (&softc->parent->parent == target)
  354. {
  355. /* matched, early return */
  356. *(rt_device_t *)data = device;
  357. return 1;
  358. }
  359. }
  360. }
  361. return RT_EOK;
  362. }
  363. /**
  364. * @brief The default console is only a backup device with lowest priority.
  365. * It's always recommended to scratch the console from the boot arguments.
  366. * And dont forget to register the device with a higher priority.
  367. */
  368. static int _default_console_setup(void)
  369. {
  370. rt_err_t rc;
  371. rt_device_t bakdev;
  372. rt_device_t ttydev;
  373. bakdev = rt_console_get_device();
  374. if (!bakdev)
  375. {
  376. return -RT_ENOENT;
  377. }
  378. ttydev = bakdev;
  379. rt_object_for_each(RT_Object_Class_Device, _match_tty_iter, &ttydev);
  380. if (ttydev != bakdev)
  381. {
  382. LOG_I("Using /dev/%.*s as default console", RT_NAME_MAX, ttydev->parent.name);
  383. lwp_console_register_backend(ttydev, LWP_CONSOLE_LOWEST_PRIOR);
  384. rc = RT_EOK;
  385. }
  386. else
  387. {
  388. rc = -RT_EINVAL;
  389. }
  390. return rc;
  391. }
  392. INIT_COMPONENT_EXPORT(_default_console_setup);