| 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 <cpu.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 | /*
|
|---|
| 97 | * If we are here, we are hopefully on the processor that
|
|---|
| 98 | * issued the 'halt' command, so proceed to read the character
|
|---|
| 99 | * directly from input
|
|---|
| 100 | */
|
|---|
| 101 | if (check_poll(indev))
|
|---|
| 102 | return indev->op->poll(indev);
|
|---|
| 103 |
|
|---|
| 104 | /* No other way of interacting with user */
|
|---|
| 105 | interrupts_disable();
|
|---|
| 106 |
|
|---|
| 107 | if (CPU)
|
|---|
| 108 | printf("cpu%u: ", CPU->id);
|
|---|
| 109 | else
|
|---|
| 110 | printf("cpu: ");
|
|---|
| 111 |
|
|---|
| 112 | printf("halted (no polling input)\n");
|
|---|
| 113 | cpu_halt();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | waitq_sleep(&indev->wq);
|
|---|
| 117 | irq_spinlock_lock(&indev->lock, true);
|
|---|
| 118 | wchar_t ch = indev->buffer[(indev->index - indev->counter) %
|
|---|
| 119 | INDEV_BUFLEN];
|
|---|
| 120 | indev->counter--;
|
|---|
| 121 | irq_spinlock_unlock(&indev->lock, true);
|
|---|
| 122 |
|
|---|
| 123 | return ch;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | /** Signal out-of-band condition
|
|---|
| 127 | *
|
|---|
| 128 | * @param indev Input character device.
|
|---|
| 129 | * @param signal Out-of-band condition to signal.
|
|---|
| 130 | *
|
|---|
| 131 | */
|
|---|
| 132 | void indev_signal(indev_t *indev, indev_signal_t signal)
|
|---|
| 133 | {
|
|---|
| 134 | if ((indev != NULL) && (indev->op != NULL) &&
|
|---|
| 135 | (indev->op->signal != NULL))
|
|---|
| 136 | indev->op->signal(indev, signal);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /** Initialize output character device.
|
|---|
| 140 | *
|
|---|
| 141 | * @param outdev Output character device.
|
|---|
| 142 | * @param op Implementation of output character device operations.
|
|---|
| 143 | *
|
|---|
| 144 | */
|
|---|
| 145 | void outdev_initialize(const char *name, outdev_t *outdev,
|
|---|
| 146 | outdev_operations_t *op)
|
|---|
| 147 | {
|
|---|
| 148 | outdev->name = name;
|
|---|
| 149 | spinlock_initialize(&outdev->lock, "chardev.outdev.lock");
|
|---|
| 150 | link_initialize(&outdev->link);
|
|---|
| 151 | list_initialize(&outdev->list);
|
|---|
| 152 | outdev->op = op;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | bool check_poll(indev_t *indev)
|
|---|
| 156 | {
|
|---|
| 157 | if (indev == NULL)
|
|---|
| 158 | return false;
|
|---|
| 159 |
|
|---|
| 160 | if (indev->op == NULL)
|
|---|
| 161 | return false;
|
|---|
| 162 |
|
|---|
| 163 | return (indev->op->poll != NULL);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /** @}
|
|---|
| 167 | */
|
|---|