Subversion Repositories HelenOS

Rev

Rev 3424 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3424 Rev 4377
Line 31... Line 31...
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <console/chardev.h>
35
#include <console/chardev.h>
36
#include <putchar.h>
-
 
37
#include <synch/waitq.h>
36
#include <synch/waitq.h>
38
#include <synch/spinlock.h>
37
#include <synch/spinlock.h>
-
 
38
#include <print.h>
-
 
39
#include <func.h>
-
 
40
#include <arch.h>
39
 
41
 
40
/** Initialize character device.
42
/** Initialize input character device.
-
 
43
 *
-
 
44
 * @param indev Input character device.
-
 
45
 * @param op    Implementation of input character device operations.
41
 *
46
 *
42
 * @param chardev Character device.
-
 
43
 * @param op Implementation of character device operations.
-
 
44
 */
47
 */
45
void chardev_initialize(char *name, chardev_t *chardev,
48
void indev_initialize(char *name, indev_t *indev,
46
            chardev_operations_t *op)
49
    indev_operations_t *op)
47
{
50
{
48
    chardev->name = name;
51
    indev->name = name;
49
 
-
 
50
    waitq_initialize(&chardev->wq);
52
    waitq_initialize(&indev->wq);
51
    spinlock_initialize(&chardev->lock, "chardev");
53
    spinlock_initialize(&indev->lock, "indev");
52
    chardev->counter = 0;
54
    indev->counter = 0;
53
    chardev->index = 0;
55
    indev->index = 0;
54
    chardev->op = op;
56
    indev->op = op;
55
}
57
}
56
 
58
 
57
/** Push character read from input character device.
59
/** Push character read from input character device.
58
 *
60
 *
59
 * @param chardev Character device.
61
 * @param indev Input character device.
60
 * @param ch Character being pushed.
62
 * @param ch    Character being pushed.
-
 
63
 *
-
 
64
 */
-
 
65
void indev_push_character(indev_t *indev, wchar_t ch)
-
 
66
{
-
 
67
    ASSERT(indev);
-
 
68
   
-
 
69
    spinlock_lock(&indev->lock);
-
 
70
    if (indev->counter == INDEV_BUFLEN - 1) {
-
 
71
        /* Buffer full */
-
 
72
        spinlock_unlock(&indev->lock);
-
 
73
        return;
-
 
74
    }
-
 
75
   
-
 
76
    indev->counter++;
-
 
77
    indev->buffer[indev->index++] = ch;
-
 
78
   
-
 
79
    /* Index modulo size of buffer */
-
 
80
    indev->index = indev->index % INDEV_BUFLEN;
-
 
81
    waitq_wakeup(&indev->wq, WAKEUP_FIRST);
-
 
82
    spinlock_unlock(&indev->lock);
-
 
83
}
-
 
84
 
-
 
85
/** Pop character from input character device.
-
 
86
 *
-
 
87
 * @param indev Input character device.
-
 
88
 *
-
 
89
 * @return Character read.
-
 
90
 *
61
 */
91
 */
62
void chardev_push_character(chardev_t *chardev, uint8_t ch)
92
wchar_t indev_pop_character(indev_t *indev)
63
{
93
{
64
    spinlock_lock(&chardev->lock);
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
         */
65
    chardev->counter++;
99
        if (check_poll(indev))
66
    if (chardev->counter == CHARDEV_BUFLEN - 1) {
100
            return indev->op->poll(indev);
-
 
101
       
67
        /* buffer full => disable device interrupt */
102
        /* No other way of interacting with user */
68
        chardev->op->suspend(chardev);
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();
69
    }
112
    }
70
   
113
   
71
    chardev->buffer[chardev->index++] = ch;
114
    waitq_sleep(&indev->wq);
-
 
115
    ipl_t ipl = interrupts_disable();
-
 
116
    spinlock_lock(&indev->lock);
72
    chardev->index = chardev->index % CHARDEV_BUFLEN; /* index modulo size of buffer */
117
    wchar_t ch = indev->buffer[(indev->index - indev->counter) % INDEV_BUFLEN];
73
    waitq_wakeup(&chardev->wq, WAKEUP_FIRST);
118
    indev->counter--;
74
    spinlock_unlock(&chardev->lock);
119
    spinlock_unlock(&indev->lock);
-
 
120
    interrupts_restore(ipl);
-
 
121
   
-
 
122
    return ch;
-
 
123
}
-
 
124
 
-
 
125
/** Initialize output character device.
-
 
126
 *
-
 
127
 * @param outdev Output character device.
-
 
128
 * @param op     Implementation of output character device operations.
-
 
129
 *
-
 
130
 */
-
 
131
void outdev_initialize(char *name, outdev_t *outdev,
-
 
132
    outdev_operations_t *op)
-
 
133
{
-
 
134
    outdev->name = name;
-
 
135
    spinlock_initialize(&outdev->lock, "outdev");
-
 
136
    outdev->op = op;
-
 
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);
75
}
148
}
76
 
149
 
77
/** @}
150
/** @}
78
 */
151
 */