1 | /*
|
---|
2 | * Copyright (c) 2005 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 genericconsole
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <adt/list.h>
|
---|
36 | #include <console/chardev.h>
|
---|
37 | #include <synch/waitq.h>
|
---|
38 | #include <synch/spinlock.h>
|
---|
39 | #include <print.h>
|
---|
40 | #include <func.h>
|
---|
41 | #include <arch.h>
|
---|
42 |
|
---|
43 | /** Initialize input character device.
|
---|
44 | *
|
---|
45 | * @param indev Input character device.
|
---|
46 | * @param op Implementation of input character device operations.
|
---|
47 | *
|
---|
48 | */
|
---|
49 | void indev_initialize(const char *name, indev_t *indev,
|
---|
50 | indev_operations_t *op)
|
---|
51 | {
|
---|
52 | indev->name = name;
|
---|
53 | waitq_initialize(&indev->wq);
|
---|
54 | irq_spinlock_initialize(&indev->lock, "chardev.indev.lock");
|
---|
55 | indev->counter = 0;
|
---|
56 | indev->index = 0;
|
---|
57 | indev->op = op;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** Push character read from input character device.
|
---|
61 | *
|
---|
62 | * @param indev Input character device.
|
---|
63 | * @param ch Character being pushed.
|
---|
64 | *
|
---|
65 | */
|
---|
66 | void indev_push_character(indev_t *indev, wchar_t ch)
|
---|
67 | {
|
---|
68 | ASSERT(indev);
|
---|
69 |
|
---|
70 | irq_spinlock_lock(&indev->lock, true);
|
---|
71 | if (indev->counter == INDEV_BUFLEN - 1) {
|
---|
72 | /* Buffer full */
|
---|
73 | irq_spinlock_unlock(&indev->lock, true);
|
---|
74 | return;
|
---|
75 | }
|
---|
76 |
|
---|
77 | indev->counter++;
|
---|
78 | indev->buffer[indev->index++] = ch;
|
---|
79 |
|
---|
80 | /* Index modulo size of buffer */
|
---|
81 | indev->index = indev->index % INDEV_BUFLEN;
|
---|
82 | waitq_wakeup(&indev->wq, WAKEUP_FIRST);
|
---|
83 | irq_spinlock_unlock(&indev->lock, true);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Pop character from input character device.
|
---|
87 | *
|
---|
88 | * @param indev Input character device.
|
---|
89 | *
|
---|
90 | * @return Character read.
|
---|
91 | *
|
---|
92 | */
|
---|
93 | wchar_t indev_pop_character(indev_t *indev)
|
---|
94 | {
|
---|
95 | if (atomic_get(&haltstate)) {
|
---|
96 | /* If we are here, we are hopefully on the processor that
|
---|
97 | * issued the 'halt' command, so proceed to read the character
|
---|
98 | * directly from input
|
---|
99 | */
|
---|
100 | if (check_poll(indev))
|
---|
101 | return indev->op->poll(indev);
|
---|
102 |
|
---|
103 | /* No other way of interacting with user */
|
---|
104 | interrupts_disable();
|
---|
105 |
|
---|
106 | if (CPU)
|
---|
107 | printf("cpu%u: ", CPU->id);
|
---|
108 | else
|
---|
109 | printf("cpu: ");
|
---|
110 |
|
---|
111 | printf("halted (no polling input)\n");
|
---|
112 | cpu_halt();
|
---|
113 | }
|
---|
114 |
|
---|
115 | waitq_sleep(&indev->wq);
|
---|
116 | irq_spinlock_lock(&indev->lock, true);
|
---|
117 | wchar_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN];
|
---|
118 | indev->counter--;
|
---|
119 | irq_spinlock_unlock(&indev->lock, true);
|
---|
120 |
|
---|
121 | return ch;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /** Initialize output character device.
|
---|
125 | *
|
---|
126 | * @param outdev Output character device.
|
---|
127 | * @param op Implementation of output character device operations.
|
---|
128 | *
|
---|
129 | */
|
---|
130 | void outdev_initialize(const char *name, outdev_t *outdev,
|
---|
131 | outdev_operations_t *op)
|
---|
132 | {
|
---|
133 | outdev->name = name;
|
---|
134 | spinlock_initialize(&outdev->lock, "chardev.outdev.lock");
|
---|
135 | link_initialize(&outdev->link);
|
---|
136 | list_initialize(&outdev->list);
|
---|
137 | outdev->op = op;
|
---|
138 | }
|
---|
139 |
|
---|
140 | bool check_poll(indev_t *indev)
|
---|
141 | {
|
---|
142 | if (indev == NULL)
|
---|
143 | return false;
|
---|
144 |
|
---|
145 | if (indev->op == NULL)
|
---|
146 | return false;
|
---|
147 |
|
---|
148 | return (indev->op->poll != NULL);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** @}
|
---|
152 | */
|
---|