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