summaryrefslogtreecommitdiff
path: root/components/drivers/tty/console.c
blob: 01349635b44978261b9d82357f82c1097c8ed3b8 (plain)
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
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021.12.07     linzhenxing      first version
 */
#include <rtthread.h>
#include <dfs_file.h>
#include <dfs_fs.h>
#include <tty.h>

#define DBG_TAG               "CONSOLE"
#ifdef RT_TTY_DEBUG
#define DBG_LVL               DBG_LOG
#else
#define DBG_LVL               DBG_INFO
#endif /* RT_TTY_DEBUG */
#include <rtdbg.h>

static struct tty_struct console_driver;

static void console_rx_notify(struct rt_device *dev)
{
    struct tty_struct *console = NULL;
    int len = 0;
    int lens = 0;
    char ch = 0;
    char buf[1024] = {0};

    console = (struct tty_struct *)dev;
    RT_ASSERT(console != RT_NULL);

    while (1)
    {
        len = rt_device_read(console->driver, -1, &ch, 1);
        if (len == 0)
        {
            break;
        }
        lens += len;
        buf[lens-1] = ch;
        if (lens > 1024)
        {
            break;
        }
    }

    if (console->ldisc->ops->receive_buf)
    {
        console->ldisc->ops->receive_buf((struct tty_struct *)console, buf, lens);
    }
}

struct tty_struct *console_tty_get(void)
{
    return &console_driver;
}

static void iodev_close(struct tty_struct *console)
{
    struct rt_device_notify rx_notify;

    rx_notify.notify = RT_NULL;
    rx_notify.dev = RT_NULL;

    /* clear notify */
    rt_device_control(console->driver, RT_DEVICE_CTRL_NOTIFY_SET, &rx_notify);
    rt_device_close(console->driver);
}

static rt_err_t iodev_open(struct tty_struct *console)
{
    rt_err_t ret = RT_EOK;
    struct rt_device_notify rx_notify;
    rt_uint16_t oflags = 0;

    rt_device_control(console->driver, RT_DEVICE_CTRL_CONSOLE_OFLAG, &oflags);

    ret = rt_device_open(console->driver, oflags);
    if (ret != RT_EOK)
    {
        return RT_ERROR;
    }

    rx_notify.notify = console_rx_notify;
    rx_notify.dev = (struct rt_device *)console;
    rt_device_control(console->driver, RT_DEVICE_CTRL_NOTIFY_SET, &rx_notify);
    return RT_EOK;
}

struct rt_device *console_get_iodev(void)
{
    rt_base_t level = 0;
    struct rt_device *iodev = RT_NULL;

    level = rt_hw_interrupt_disable();
    iodev = console_driver.driver;
    rt_hw_interrupt_enable(level);
    return iodev;
}

struct rt_device *console_set_iodev(struct rt_device *iodev)
{
    rt_base_t level = 0;
    struct rt_device *io_before = RT_NULL;
    struct tty_struct *console = RT_NULL;

    RT_ASSERT(iodev != RT_NULL);

    console = &console_driver;

    level = rt_hw_interrupt_disable();

    RT_ASSERT(console->init_flag >= TTY_INIT_FLAG_REGED);

    io_before = console->driver;

    if (iodev == io_before)
    {
        goto exit;
    }

    if (console->init_flag >= TTY_INIT_FLAG_INITED)
    {
        /* close old device */
        iodev_close(console);
    }

    console->driver = iodev;

    if (console->init_flag >= TTY_INIT_FLAG_INITED)
    {
        rt_err_t ret;
        /* open new device */
        ret = iodev_open(console);
        RT_ASSERT(ret == RT_EOK);
    }

exit:
    rt_hw_interrupt_enable(level);
    return io_before;
}

/* RT-Thread Device Interface */
/*
 * This function initializes console device.
 */
static rt_err_t rt_console_init(struct rt_device *dev)
{
    rt_base_t level = 0;
    rt_err_t result = RT_EOK;
    struct tty_struct *console = RT_NULL;

    RT_ASSERT(dev != RT_NULL);

    console = (struct tty_struct *)dev;

    level = rt_hw_interrupt_disable();

    RT_ASSERT(console->init_flag == TTY_INIT_FLAG_REGED);

    result = iodev_open(console);
    if (result != RT_EOK)
    {
        goto exit;
    }

    console->init_flag = TTY_INIT_FLAG_INITED;
exit:
    rt_hw_interrupt_enable(level);
    return result;
}

static rt_err_t rt_console_open(struct rt_device *dev, rt_uint16_t oflag)
{
    rt_err_t result = RT_EOK;
    struct tty_struct *console = RT_NULL;

    RT_ASSERT(dev != RT_NULL);
    console = (struct tty_struct *)dev;
    RT_ASSERT(console != RT_NULL);
    RT_ASSERT(console->init_flag == TTY_INIT_FLAG_INITED);
    return result;
}

static rt_err_t rt_console_close(struct rt_device *dev)
{
    rt_err_t result = RT_EOK;
    struct tty_struct *console = RT_NULL;

    console = (struct tty_struct *)dev;
    RT_ASSERT(console != RT_NULL);
    RT_ASSERT(console->init_flag == TTY_INIT_FLAG_INITED);
    return result;
}

static rt_ssize_t rt_console_read(struct rt_device *dev,
        rt_off_t          pos,
        void             *buffer,
        rt_size_t         size)
{
    rt_size_t len = 0;
    return len;
}

static rt_ssize_t rt_console_write(struct rt_device *dev,
        rt_off_t          pos,
        const void       *buffer,
        rt_size_t         size)
{
    rt_size_t len = 0;
    struct tty_struct *console = RT_NULL;

    console = (struct tty_struct *)dev;
    RT_ASSERT(console != RT_NULL);
    RT_ASSERT(console->init_flag == TTY_INIT_FLAG_INITED);

    len = rt_device_write((struct rt_device *)console->driver, -1, buffer, size);

    return len;
}

static rt_err_t  rt_console_control(rt_device_t dev, int cmd, void *args)
{
    rt_size_t len = 0;
    struct tty_struct *console = RT_NULL;

    console = (struct tty_struct *)dev;
    RT_ASSERT(console != RT_NULL);
    RT_ASSERT(console->init_flag == TTY_INIT_FLAG_INITED);

    len = rt_device_control((struct rt_device *)console->driver, cmd, args);

    return len;
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops console_ops =
{
    rt_console_init,
    rt_console_open,
    rt_console_close,
    rt_console_read,
    rt_console_write,
    rt_console_control,
};
#endif
/*
 * console register
 */
 static struct dfs_file_ops con_fops;
rt_err_t console_register(const char *name, struct rt_device *iodev)
{
    rt_base_t level = 0;
    rt_err_t ret = RT_EOK;
    struct rt_device *device = RT_NULL;
    struct tty_struct *console = &console_driver;

    level = rt_hw_interrupt_disable();
    RT_ASSERT(console->init_flag == TTY_INIT_FLAG_NONE);
    RT_ASSERT(iodev != RT_NULL);

    device = &(console->parent);

    device->type        = RT_Device_Class_Char;

#ifdef RT_USING_DEVICE_OPS
    device->ops         = &console_ops;
#else
    device->init        = rt_console_init;
    device->open        = rt_console_open;
    device->close       = rt_console_close;
    device->read        = rt_console_read;
    device->write       = rt_console_write;
    device->control     = rt_console_control;
#endif


    /* register a character device */
    ret = rt_device_register(device, name, 0);
    if (ret != RT_EOK)
    {
        LOG_E("console driver register fail\n");
        goto exit;
    }

#ifdef RT_USING_POSIX_DEVIO
    /* set fops */
    console_set_fops(&con_fops);
    device->fops = &con_fops;
#endif
    console->type = TTY_DRIVER_TYPE_CONSOLE;
    console->subtype = SERIAL_TYPE_NORMAL;
    console->driver = iodev;
    console->head = rt_calloc(1, sizeof(struct tty_node));
    if (!console->head)
    {
        return -RT_ENOMEM;
    }
    tty_initstack(console->head);

    rt_mutex_init(&console->lock, "ttylock", RT_IPC_FLAG_PRIO);
    console->pgrp = -1;
    console->session = -1;
    console->foreground = RT_NULL;
    rt_wqueue_init(&console->wait_queue);

    tty_ldisc_init(console);

extern struct termios tty_std_termios;
    console->init_termios = tty_std_termios;
    console->init_termios.c_cflag =
        B9600 | CS8 | CREAD | HUPCL; /* is normally B9600 default... */
    console->init_termios.__c_ispeed = 9600;
    console->init_termios.__c_ospeed = 9600;

    console_ldata_init(console);
    console->init_flag = TTY_INIT_FLAG_REGED;
exit:
    rt_hw_interrupt_enable(level);
    return ret;
}