source: mainline/kernel/genarch/src/srln/srln.c@ 7c3fb9b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7c3fb9b was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * Copyright (c) 2009 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup genarch
30 * @{
31 */
32/**
33 * @file
34 * @brief Serial line processing.
35 */
36
37#include <assert.h>
38#include <genarch/srln/srln.h>
39#include <console/chardev.h>
40#include <console/console.h>
41#include <proc/thread.h>
42#include <arch.h>
43#include <str.h>
44
45static indev_operations_t srln_raw_ops = {
46 .poll = NULL,
47 .signal = NULL
48};
49
50static void ksrln(void *arg)
51{
52 srln_instance_t *instance = (srln_instance_t *) arg;
53 bool cr = false;
54 uint32_t escape = 0;
55
56 while (true) {
57 wchar_t ch = indev_pop_character(&instance->raw);
58
59 /* ANSI escape sequence processing */
60 if (escape != 0) {
61 escape <<= 8;
62 escape |= ch & 0xff;
63
64 if ((escape == 0x1b4f) || (escape == 0x1b5b) || (escape == 0x1b5b33))
65 continue;
66
67 switch (escape) {
68 case 0x1b4f46:
69 case 0x1b5b46:
70 ch = U_END_ARROW;
71 escape = 0;
72 break;
73 case 0x1b4f48:
74 case 0x1b5b48:
75 ch = U_HOME_ARROW;
76 escape = 0;
77 break;
78 case 0x1b5b41:
79 ch = U_UP_ARROW;
80 escape = 0;
81 break;
82 case 0x1b5b42:
83 ch = U_DOWN_ARROW;
84 escape = 0;
85 break;
86 case 0x1b5b43:
87 ch = U_RIGHT_ARROW;
88 escape = 0;
89 break;
90 case 0x1b5b44:
91 ch = U_LEFT_ARROW;
92 escape = 0;
93 break;
94 case 0x1b5b337e:
95 ch = U_DELETE;
96 escape = 0;
97 break;
98 default:
99 escape = 0;
100 }
101 }
102
103 if (ch == 0x1b) {
104 escape = ch & 0xff;
105 continue;
106 }
107
108 /*
109 * Replace carriage return with line feed
110 * and suppress any following line feed
111 */
112 if ((ch == '\n') && (cr)) {
113 cr = false;
114 continue;
115 }
116
117 if (ch == '\r') {
118 ch = '\n';
119 cr = true;
120 } else
121 cr = false;
122
123 /* Backspace */
124 if (ch == 0x7f)
125 ch = '\b';
126
127 indev_push_character(instance->sink, ch);
128 }
129}
130
131srln_instance_t *srln_init(void)
132{
133 srln_instance_t *instance =
134 malloc(sizeof(srln_instance_t));
135 if (instance) {
136 instance->thread = thread_create(ksrln, (void *) instance,
137 TASK, THREAD_FLAG_NONE, "ksrln");
138
139 if (!instance->thread) {
140 free(instance);
141 return NULL;
142 }
143
144 instance->sink = NULL;
145 indev_initialize("srln", &instance->raw, &srln_raw_ops);
146 }
147
148 return instance;
149}
150
151indev_t *srln_wire(srln_instance_t *instance, indev_t *sink)
152{
153 assert(instance);
154 assert(sink);
155
156 instance->sink = sink;
157 thread_ready(instance->thread);
158
159 return &instance->raw;
160}
161
162/** @}
163 */
Note: See TracBrowser for help on using the repository browser.