1 | /*
|
---|
2 | * Copyright (c) 2009 Vineeth Pillai
|
---|
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 kernel_genarch
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief PC/AT Keyboard processing.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <assert.h>
|
---|
38 | #include <genarch/kbrd/kbrd.h>
|
---|
39 | #include <genarch/kbrd/scanc.h>
|
---|
40 |
|
---|
41 | #include <genarch/kbrd/scanc_at.h>
|
---|
42 |
|
---|
43 | #include <synch/spinlock.h>
|
---|
44 | #include <console/chardev.h>
|
---|
45 | #include <console/console.h>
|
---|
46 | #include <proc/thread.h>
|
---|
47 | #include <arch.h>
|
---|
48 | #include <macros.h>
|
---|
49 | #include <stdlib.h>
|
---|
50 |
|
---|
51 | #define PRESSED_SHIFT (1 << 0)
|
---|
52 | #define PRESSED_CAPSLOCK (1 << 1)
|
---|
53 | #define LOCKED_CAPSLOCK (1 << 0)
|
---|
54 |
|
---|
55 | #define AT_KEY_RELEASE 0xF0
|
---|
56 | #define AT_ESC_KEY 0xE0
|
---|
57 | #define AT_CAPS_SCAN_CODE 0x58
|
---|
58 | #define AT_NUM_SCAN_CODE 0x77
|
---|
59 | #define AT_SCROLL_SCAN_CODE 0x7E
|
---|
60 |
|
---|
61 | static bool is_lock_key(char32_t);
|
---|
62 |
|
---|
63 | static indev_operations_t kbrd_raw_ops = {
|
---|
64 | .poll = NULL
|
---|
65 | };
|
---|
66 |
|
---|
67 | /** Process release of key.
|
---|
68 | *
|
---|
69 | * @param sc Scancode of the key being released.
|
---|
70 | */
|
---|
71 | static void key_released(kbrd_instance_t *instance, char32_t sc)
|
---|
72 | {
|
---|
73 | spinlock_lock(&instance->keylock);
|
---|
74 |
|
---|
75 | switch (sc) {
|
---|
76 | case SC_LSHIFT:
|
---|
77 | case SC_RSHIFT:
|
---|
78 | instance->keyflags &= ~PRESSED_SHIFT;
|
---|
79 | break;
|
---|
80 | case SC_CAPSLOCK:
|
---|
81 | instance->keyflags &= ~PRESSED_CAPSLOCK;
|
---|
82 | if (instance->lockflags & LOCKED_CAPSLOCK)
|
---|
83 | instance->lockflags &= ~LOCKED_CAPSLOCK;
|
---|
84 | else
|
---|
85 | instance->lockflags |= LOCKED_CAPSLOCK;
|
---|
86 | break;
|
---|
87 | default:
|
---|
88 | break;
|
---|
89 | }
|
---|
90 |
|
---|
91 | spinlock_unlock(&instance->keylock);
|
---|
92 | }
|
---|
93 |
|
---|
94 | /** Process keypress.
|
---|
95 | *
|
---|
96 | * @param sc Scancode of the key being pressed.
|
---|
97 | */
|
---|
98 | static void key_pressed(kbrd_instance_t *instance, char32_t sc)
|
---|
99 | {
|
---|
100 | bool letter;
|
---|
101 | bool shift;
|
---|
102 | bool capslock;
|
---|
103 |
|
---|
104 | spinlock_lock(&instance->keylock);
|
---|
105 |
|
---|
106 | switch (sc) {
|
---|
107 | case SC_LSHIFT:
|
---|
108 | case SC_RSHIFT:
|
---|
109 | instance->keyflags |= PRESSED_SHIFT;
|
---|
110 | break;
|
---|
111 | case SC_CAPSLOCK:
|
---|
112 | instance->keyflags |= PRESSED_CAPSLOCK;
|
---|
113 | break;
|
---|
114 | case SC_SCAN_ESCAPE:
|
---|
115 | break;
|
---|
116 | default:
|
---|
117 | letter = islower(sc_primary_map[sc]);
|
---|
118 | shift = instance->keyflags & PRESSED_SHIFT;
|
---|
119 | capslock = (instance->keyflags & PRESSED_CAPSLOCK) ||
|
---|
120 | (instance->lockflags & LOCKED_CAPSLOCK);
|
---|
121 |
|
---|
122 | if ((letter) && (capslock))
|
---|
123 | shift = !shift;
|
---|
124 |
|
---|
125 | if (shift)
|
---|
126 | indev_push_character(instance->sink, sc_secondary_map[sc]);
|
---|
127 | else
|
---|
128 | indev_push_character(instance->sink, sc_primary_map[sc]);
|
---|
129 | break;
|
---|
130 | }
|
---|
131 |
|
---|
132 | spinlock_unlock(&instance->keylock);
|
---|
133 | }
|
---|
134 |
|
---|
135 | static void kkbrd(void *arg)
|
---|
136 | {
|
---|
137 | static int key_released_flag = 0;
|
---|
138 | static int is_locked = 0;
|
---|
139 | kbrd_instance_t *instance = (kbrd_instance_t *) arg;
|
---|
140 |
|
---|
141 | while (true) {
|
---|
142 | char32_t sc = indev_pop_character(&instance->raw);
|
---|
143 |
|
---|
144 | if (sc == AT_KEY_RELEASE) {
|
---|
145 | key_released_flag = 1;
|
---|
146 | } else {
|
---|
147 | if (key_released_flag) {
|
---|
148 | key_released_flag = 0;
|
---|
149 | if (is_lock_key(sc)) {
|
---|
150 | if (!is_locked) {
|
---|
151 | is_locked = 1;
|
---|
152 | } else {
|
---|
153 | is_locked = 0;
|
---|
154 | continue;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | key_released(instance, sc);
|
---|
158 |
|
---|
159 | } else {
|
---|
160 | if (is_lock_key(sc) && is_locked)
|
---|
161 | continue;
|
---|
162 | key_pressed(instance, sc);
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|
169 | kbrd_instance_t *kbrd_init(void)
|
---|
170 | {
|
---|
171 | kbrd_instance_t *instance;
|
---|
172 |
|
---|
173 | instance = malloc(sizeof(kbrd_instance_t));
|
---|
174 | if (instance) {
|
---|
175 | instance->thread = thread_create(kkbrd, (void *) instance, TASK, 0,
|
---|
176 | "kkbrd");
|
---|
177 |
|
---|
178 | if (!instance->thread) {
|
---|
179 | free(instance);
|
---|
180 | return NULL;
|
---|
181 | }
|
---|
182 |
|
---|
183 | instance->sink = NULL;
|
---|
184 | indev_initialize("kbrd", &instance->raw, &kbrd_raw_ops);
|
---|
185 |
|
---|
186 | spinlock_initialize(&instance->keylock, "kbrd_at.instance.keylock");
|
---|
187 | instance->keyflags = 0;
|
---|
188 | instance->lockflags = 0;
|
---|
189 | }
|
---|
190 |
|
---|
191 | return instance;
|
---|
192 | }
|
---|
193 |
|
---|
194 | indev_t *kbrd_wire(kbrd_instance_t *instance, indev_t *sink)
|
---|
195 | {
|
---|
196 | assert(instance);
|
---|
197 | assert(sink);
|
---|
198 |
|
---|
199 | instance->sink = sink;
|
---|
200 | thread_start(instance->thread);
|
---|
201 |
|
---|
202 | return &instance->raw;
|
---|
203 | }
|
---|
204 |
|
---|
205 | static bool is_lock_key(char32_t sc)
|
---|
206 | {
|
---|
207 | return ((sc == AT_CAPS_SCAN_CODE) || (sc == AT_NUM_SCAN_CODE) ||
|
---|
208 | (sc == AT_SCROLL_SCAN_CODE));
|
---|
209 | }
|
---|
210 |
|
---|
211 | /** @}
|
---|
212 | */
|
---|