source: mainline/kernel/genarch/src/srln/srln.c

Last change on this file was 0f4f1b2, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 18 months ago

Add (and use) functions thread_start() and thread_detach()

Mostly cosmetic, with thread_start() replacing calls to thread_ready(),
but not consuming the passed reference, and thread_detach() being
synonym for thread_put(). Makes the code's function more obvious.

Also modify some threaded tests to use thread_join() for waiting,
instead of counting threads with atomics or semaphores.

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[4c84368e]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
[6404aca]29/** @addtogroup kernel_genarch
[4c84368e]30 * @{
31 */
32/**
33 * @file
[b6f2ebc]34 * @brief Serial line processing.
[4c84368e]35 */
36
[63e27ef]37#include <assert.h>
[4c84368e]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>
[19f857a]43#include <str.h>
[aafed15]44#include <stdlib.h>
[4c84368e]45
[c2417bc]46static indev_operations_t srln_raw_ops = {
[7ddc2c7]47 .poll = NULL,
48 .signal = NULL
[4c84368e]49};
50
51static void ksrln(void *arg)
52{
[c2417bc]53 srln_instance_t *instance = (srln_instance_t *) arg;
[b6f2ebc]54 bool cr = false;
[c8bf88d]55 uint32_t escape = 0;
[a35b458]56
[b6f2ebc]57 while (true) {
[28a5ebd]58 char32_t ch = indev_pop_character(&instance->raw);
[a35b458]59
[c8bf88d]60 /* ANSI escape sequence processing */
61 if (escape != 0) {
62 escape <<= 8;
63 escape |= ch & 0xff;
[a35b458]64
[c8bf88d]65 if ((escape == 0x1b4f) || (escape == 0x1b5b) || (escape == 0x1b5b33))
66 continue;
[a35b458]67
[c8bf88d]68 switch (escape) {
69 case 0x1b4f46:
70 case 0x1b5b46:
71 ch = U_END_ARROW;
72 escape = 0;
73 break;
74 case 0x1b4f48:
75 case 0x1b5b48:
76 ch = U_HOME_ARROW;
77 escape = 0;
78 break;
79 case 0x1b5b41:
80 ch = U_UP_ARROW;
81 escape = 0;
82 break;
83 case 0x1b5b42:
84 ch = U_DOWN_ARROW;
85 escape = 0;
86 break;
87 case 0x1b5b43:
88 ch = U_RIGHT_ARROW;
89 escape = 0;
90 break;
91 case 0x1b5b44:
92 ch = U_LEFT_ARROW;
93 escape = 0;
94 break;
95 case 0x1b5b337e:
96 ch = U_DELETE;
97 escape = 0;
98 break;
99 default:
100 escape = 0;
101 }
102 }
[a35b458]103
[c8bf88d]104 if (ch == 0x1b) {
105 escape = ch & 0xff;
106 continue;
107 }
[a35b458]108
[7c3fb9b]109 /*
110 * Replace carriage return with line feed
111 * and suppress any following line feed
112 */
[b6f2ebc]113 if ((ch == '\n') && (cr)) {
114 cr = false;
[4c84368e]115 continue;
[b6f2ebc]116 }
[a35b458]117
[b6f2ebc]118 if (ch == '\r') {
119 ch = '\n';
120 cr = true;
121 } else
122 cr = false;
[a35b458]123
[c8bf88d]124 /* Backspace */
[b6f2ebc]125 if (ch == 0x7f)
126 ch = '\b';
[a35b458]127
[c2417bc]128 indev_push_character(instance->sink, ch);
[4c84368e]129 }
130}
131
[c2417bc]132srln_instance_t *srln_init(void)
[4c84368e]133{
[3bacee1]134 srln_instance_t *instance =
[11b285d]135 malloc(sizeof(srln_instance_t));
[c2417bc]136 if (instance) {
[6eef3c4]137 instance->thread = thread_create(ksrln, (void *) instance,
138 TASK, THREAD_FLAG_NONE, "ksrln");
[a35b458]139
[c2417bc]140 if (!instance->thread) {
141 free(instance);
142 return NULL;
143 }
[a35b458]144
[c2417bc]145 instance->sink = NULL;
146 indev_initialize("srln", &instance->raw, &srln_raw_ops);
[b6f2ebc]147 }
[a35b458]148
[c2417bc]149 return instance;
150}
151
152indev_t *srln_wire(srln_instance_t *instance, indev_t *sink)
153{
[63e27ef]154 assert(instance);
155 assert(sink);
[a35b458]156
[c2417bc]157 instance->sink = sink;
[0f4f1b2]158 thread_start(instance->thread);
[a35b458]159
[c2417bc]160 return &instance->raw;
[4c84368e]161}
162
163/** @}
[c24c26c6]164 */
Note: See TracBrowser for help on using the repository browser.