[2677758] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Jakub Jermar
|
---|
[2677758] | 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 |
|
---|
[06e1e95] | 29 | /** @addtogroup genericconsole
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[b9c7425] | 35 | #include <adt/list.h>
|
---|
[2677758] | 36 | #include <console/chardev.h>
|
---|
| 37 | #include <synch/waitq.h>
|
---|
| 38 | #include <synch/spinlock.h>
|
---|
[44b7783] | 39 | #include <print.h>
|
---|
| 40 | #include <func.h>
|
---|
| 41 | #include <arch.h>
|
---|
[2677758] | 42 |
|
---|
[a7efdec] | 43 | /** Initialize input character device.
|
---|
| 44 | *
|
---|
| 45 | * @param indev Input character device.
|
---|
| 46 | * @param op Implementation of input character device operations.
|
---|
[607c5f9] | 47 | *
|
---|
| 48 | */
|
---|
[a7efdec] | 49 | void indev_initialize(char *name, indev_t *indev,
|
---|
| 50 | indev_operations_t *op)
|
---|
[2677758] | 51 | {
|
---|
[a7efdec] | 52 | indev->name = name;
|
---|
| 53 | waitq_initialize(&indev->wq);
|
---|
| 54 | spinlock_initialize(&indev->lock, "indev");
|
---|
| 55 | indev->counter = 0;
|
---|
| 56 | indev->index = 0;
|
---|
| 57 | indev->op = op;
|
---|
[607c5f9] | 58 | }
|
---|
| 59 |
|
---|
| 60 | /** Push character read from input character device.
|
---|
| 61 | *
|
---|
[a7efdec] | 62 | * @param indev Input character device.
|
---|
| 63 | * @param ch Character being pushed.
|
---|
| 64 | *
|
---|
[607c5f9] | 65 | */
|
---|
[d1dabe1f] | 66 | void indev_push_character(indev_t *indev, wchar_t ch)
|
---|
[607c5f9] | 67 | {
|
---|
[a7efdec] | 68 | ASSERT(indev);
|
---|
| 69 |
|
---|
| 70 | spinlock_lock(&indev->lock);
|
---|
| 71 | if (indev->counter == INDEV_BUFLEN - 1) {
|
---|
| 72 | /* Buffer full */
|
---|
| 73 | spinlock_unlock(&indev->lock);
|
---|
| 74 | return;
|
---|
[607c5f9] | 75 | }
|
---|
[e4ddfa8] | 76 |
|
---|
[a7efdec] | 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 | spinlock_unlock(&indev->lock);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[44b7783] | 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 | ipl_t ipl = interrupts_disable();
|
---|
| 117 | spinlock_lock(&indev->lock);
|
---|
| 118 | wchar_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN];
|
---|
| 119 | indev->counter--;
|
---|
| 120 | spinlock_unlock(&indev->lock);
|
---|
| 121 | interrupts_restore(ipl);
|
---|
| 122 |
|
---|
| 123 | return ch;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[a7efdec] | 126 | /** Initialize output character device.
|
---|
| 127 | *
|
---|
| 128 | * @param outdev Output character device.
|
---|
| 129 | * @param op Implementation of output character device operations.
|
---|
| 130 | *
|
---|
| 131 | */
|
---|
| 132 | void outdev_initialize(char *name, outdev_t *outdev,
|
---|
| 133 | outdev_operations_t *op)
|
---|
| 134 | {
|
---|
| 135 | outdev->name = name;
|
---|
| 136 | spinlock_initialize(&outdev->lock, "outdev");
|
---|
[b9c7425] | 137 | link_initialize(&outdev->link);
|
---|
| 138 | list_initialize(&outdev->list);
|
---|
[a7efdec] | 139 | outdev->op = op;
|
---|
[2677758] | 140 | }
|
---|
[b45c443] | 141 |
|
---|
[44b7783] | 142 | bool check_poll(indev_t *indev)
|
---|
| 143 | {
|
---|
| 144 | if (indev == NULL)
|
---|
| 145 | return false;
|
---|
| 146 |
|
---|
| 147 | if (indev->op == NULL)
|
---|
| 148 | return false;
|
---|
| 149 |
|
---|
| 150 | return (indev->op->poll != NULL);
|
---|
| 151 | }
|
---|
| 152 |
|
---|
[06e1e95] | 153 | /** @}
|
---|
[b45c443] | 154 | */
|
---|