Rev 4263 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4263 | Rev 4327 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2005 Jakub Jermar |
2 | * Copyright (c) 2005 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup genericconsole |
29 | /** @addtogroup genericconsole |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** @file |
32 | /** @file |
33 | */ |
33 | */ |
34 | 34 | ||
35 | #include <console/chardev.h> |
35 | #include <console/chardev.h> |
36 | #include <synch/waitq.h> |
36 | #include <synch/waitq.h> |
37 | #include <synch/spinlock.h> |
37 | #include <synch/spinlock.h> |
- | 38 | #include <print.h> |
|
- | 39 | #include <func.h> |
|
- | 40 | #include <arch.h> |
|
38 | 41 | ||
39 | /** Initialize input character device. |
42 | /** Initialize input character device. |
40 | * |
43 | * |
41 | * @param indev Input character device. |
44 | * @param indev Input character device. |
42 | * @param op Implementation of input character device operations. |
45 | * @param op Implementation of input character device operations. |
43 | * |
46 | * |
44 | */ |
47 | */ |
45 | void indev_initialize(char *name, indev_t *indev, |
48 | void indev_initialize(char *name, indev_t *indev, |
46 | indev_operations_t *op) |
49 | indev_operations_t *op) |
47 | { |
50 | { |
48 | indev->name = name; |
51 | indev->name = name; |
49 | waitq_initialize(&indev->wq); |
52 | waitq_initialize(&indev->wq); |
50 | spinlock_initialize(&indev->lock, "indev"); |
53 | spinlock_initialize(&indev->lock, "indev"); |
51 | indev->counter = 0; |
54 | indev->counter = 0; |
52 | indev->index = 0; |
55 | indev->index = 0; |
53 | indev->op = op; |
56 | indev->op = op; |
54 | } |
57 | } |
55 | 58 | ||
56 | /** Push character read from input character device. |
59 | /** Push character read from input character device. |
57 | * |
60 | * |
58 | * @param indev Input character device. |
61 | * @param indev Input character device. |
59 | * @param ch Character being pushed. |
62 | * @param ch Character being pushed. |
60 | * |
63 | * |
61 | */ |
64 | */ |
62 | void indev_push_character(indev_t *indev, wchar_t ch) |
65 | void indev_push_character(indev_t *indev, wchar_t ch) |
63 | { |
66 | { |
64 | ASSERT(indev); |
67 | ASSERT(indev); |
65 | 68 | ||
66 | spinlock_lock(&indev->lock); |
69 | spinlock_lock(&indev->lock); |
67 | if (indev->counter == INDEV_BUFLEN - 1) { |
70 | if (indev->counter == INDEV_BUFLEN - 1) { |
68 | /* Buffer full */ |
71 | /* Buffer full */ |
69 | spinlock_unlock(&indev->lock); |
72 | spinlock_unlock(&indev->lock); |
70 | return; |
73 | return; |
71 | } |
74 | } |
72 | 75 | ||
73 | indev->counter++; |
76 | indev->counter++; |
74 | indev->buffer[indev->index++] = ch; |
77 | indev->buffer[indev->index++] = ch; |
75 | 78 | ||
76 | /* Index modulo size of buffer */ |
79 | /* Index modulo size of buffer */ |
77 | indev->index = indev->index % INDEV_BUFLEN; |
80 | indev->index = indev->index % INDEV_BUFLEN; |
78 | waitq_wakeup(&indev->wq, WAKEUP_FIRST); |
81 | waitq_wakeup(&indev->wq, WAKEUP_FIRST); |
79 | spinlock_unlock(&indev->lock); |
82 | spinlock_unlock(&indev->lock); |
80 | } |
83 | } |
81 | 84 | ||
- | 85 | /** Pop character from input character device. |
|
- | 86 | * |
|
- | 87 | * @param indev Input character device. |
|
- | 88 | * |
|
- | 89 | * @return Character read. |
|
- | 90 | * |
|
- | 91 | */ |
|
- | 92 | wchar_t indev_pop_character(indev_t *indev) |
|
- | 93 | { |
|
- | 94 | if (atomic_get(&haltstate)) { |
|
- | 95 | /* If we are here, we are hopefully on the processor that |
|
- | 96 | * issued the 'halt' command, so proceed to read the character |
|
- | 97 | * directly from input |
|
- | 98 | */ |
|
- | 99 | if (check_poll(indev)) |
|
- | 100 | return indev->op->poll(indev); |
|
- | 101 | ||
- | 102 | /* No other way of interacting with user */ |
|
- | 103 | interrupts_disable(); |
|
- | 104 | ||
- | 105 | if (CPU) |
|
- | 106 | printf("cpu%u: ", CPU->id); |
|
- | 107 | else |
|
- | 108 | printf("cpu: "); |
|
- | 109 | ||
- | 110 | printf("halted (no polling input)\n"); |
|
- | 111 | cpu_halt(); |
|
- | 112 | } |
|
- | 113 | ||
- | 114 | waitq_sleep(&indev->wq); |
|
- | 115 | ipl_t ipl = interrupts_disable(); |
|
- | 116 | spinlock_lock(&indev->lock); |
|
- | 117 | wchar_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN]; |
|
- | 118 | indev->counter--; |
|
- | 119 | spinlock_unlock(&indev->lock); |
|
- | 120 | interrupts_restore(ipl); |
|
- | 121 | ||
- | 122 | return ch; |
|
- | 123 | } |
|
- | 124 | ||
82 | /** Initialize output character device. |
125 | /** Initialize output character device. |
83 | * |
126 | * |
84 | * @param outdev Output character device. |
127 | * @param outdev Output character device. |
85 | * @param op Implementation of output character device operations. |
128 | * @param op Implementation of output character device operations. |
86 | * |
129 | * |
87 | */ |
130 | */ |
88 | void outdev_initialize(char *name, outdev_t *outdev, |
131 | void outdev_initialize(char *name, outdev_t *outdev, |
89 | outdev_operations_t *op) |
132 | outdev_operations_t *op) |
90 | { |
133 | { |
91 | outdev->name = name; |
134 | outdev->name = name; |
92 | spinlock_initialize(&outdev->lock, "outdev"); |
135 | spinlock_initialize(&outdev->lock, "outdev"); |
93 | outdev->op = op; |
136 | outdev->op = op; |
94 | } |
137 | } |
- | 138 | ||
- | 139 | bool check_poll(indev_t *indev) |
|
- | 140 | { |
|
- | 141 | if (indev == NULL) |
|
- | 142 | return false; |
|
- | 143 | ||
- | 144 | if (indev->op == NULL) |
|
- | 145 | return false; |
|
- | 146 | ||
- | 147 | return (indev->op->poll != NULL); |
|
- | 148 | } |
|
95 | 149 | ||
96 | /** @} |
150 | /** @} |
97 | */ |
151 | */ |
98 | 152 |