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