source: mainline/kernel/generic/src/console/chardev.c@ 059a8e4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 059a8e4 was da1bafb, checked in by Martin Decky <martin@…>, 15 years ago

major code revision

  • replace spinlocks taken with interrupts disabled with irq_spinlocks
  • change spacing (not indendation) to be tab-size independent
  • use unsigned integer types where appropriate (especially bit flags)
  • visual separation
  • remove argument names in function prototypes
  • string changes
  • correct some formating directives
  • replace various cryptic single-character variables (t, a, m, c, b, etc.) with proper identifiers (thread, task, timeout, as, itm, itc, etc.)
  • unify some assembler constructs
  • unused page table levels are now optimized out in compile time
  • replace several ints (with boolean semantics) with bools
  • use specifically sized types instead of generic types where appropriate (size_t, uint32_t, btree_key_t)
  • improve comments
  • split asserts with conjuction into multiple independent asserts
  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[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 */
[a000878c]49void indev_initialize(const char *name, indev_t *indev,
[a7efdec]50 indev_operations_t *op)
[2677758]51{
[a7efdec]52 indev->name = name;
53 waitq_initialize(&indev->wq);
[da1bafb]54 irq_spinlock_initialize(&indev->lock, "chardev.indev.lock");
[a7efdec]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]66void indev_push_character(indev_t *indev, wchar_t ch)
[607c5f9]67{
[a7efdec]68 ASSERT(indev);
69
[da1bafb]70 irq_spinlock_lock(&indev->lock, true);
[a7efdec]71 if (indev->counter == INDEV_BUFLEN - 1) {
72 /* Buffer full */
[da1bafb]73 irq_spinlock_unlock(&indev->lock, true);
[a7efdec]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);
[da1bafb]83 irq_spinlock_unlock(&indev->lock, true);
[a7efdec]84}
85
[44b7783]86/** Pop character from input character device.
87 *
88 * @param indev Input character device.
89 *
90 * @return Character read.
91 *
92 */
93wchar_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);
[da1bafb]116 irq_spinlock_lock(&indev->lock, true);
[44b7783]117 wchar_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN];
118 indev->counter--;
[da1bafb]119 irq_spinlock_unlock(&indev->lock, true);
[44b7783]120
121 return ch;
122}
123
[a7efdec]124/** Initialize output character device.
125 *
126 * @param outdev Output character device.
127 * @param op Implementation of output character device operations.
128 *
129 */
[a000878c]130void outdev_initialize(const char *name, outdev_t *outdev,
[a7efdec]131 outdev_operations_t *op)
132{
133 outdev->name = name;
[da1bafb]134 spinlock_initialize(&outdev->lock, "chardev.outdev.lock");
[b9c7425]135 link_initialize(&outdev->link);
136 list_initialize(&outdev->list);
[a7efdec]137 outdev->op = op;
[2677758]138}
[b45c443]139
[44b7783]140bool 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
[06e1e95]151/** @}
[b45c443]152 */
Note: See TracBrowser for help on using the repository browser.