serial_tty.c 12 KB

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