Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / uts / common / io / kbtrans / kbtrans_polled.c
bloba3f9ca7aa182d6bbb27ed61bb77ad6514064b803
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Generic Keyboard Support: Polled I/O support for kbtrans-supported keyboards.
33 #define KEYMAP_SIZE_VARIABLE
35 #include <sys/types.h>
36 #include <sys/stream.h>
37 #include <sys/kbd.h>
38 #include <sys/kbio.h>
39 #include <sys/vuid_event.h>
40 #include <sys/consdev.h>
41 #include <sys/kbtrans.h>
42 #include "kbtrans_lower.h"
43 #include "kbtrans_streams.h"
46 * Internal Function Prototypes
48 static void kbtrans_polled_pressed(struct kbtrans *, uint_t, kbtrans_key_t,
49 uint_t);
50 static void kbtrans_polled_released(struct kbtrans *, kbtrans_key_t);
51 static void kbtrans_polled_setled(struct kbtrans *);
52 static void kbtrans_polled_setup_repeat(struct kbtrans *, uint_t,
53 kbtrans_key_t);
54 static void kbtrans_polled_cancel_repeat(struct kbtrans *);
57 * Functions to be called when a key is translated during polled
58 * mode
60 struct keyboard_callback kbtrans_polled_callbacks = {
61 NULL, /* keypressed_raw */
62 NULL, /* keyreleased_raw */
63 kbtrans_polled_pressed, /* keypressed */
64 kbtrans_polled_released, /* keyreleased */
65 kbtrans_polled_setup_repeat, /* setup_repeat */
66 kbtrans_polled_cancel_repeat, /* cancel_repeat */
67 kbtrans_polled_setled, /* setled */
71 * kbtrans_ischar:
72 * Return B_TRUE if character is pending, else return B_FALSE
74 boolean_t
75 kbtrans_ischar(struct kbtrans *upper)
77 struct kbtrans_callbacks *cb;
78 struct kbtrans_hardware *hw;
79 kbtrans_key_t key;
80 enum keystate state;
83 * If we've still got input pending, say so.
85 if (*upper->kbtrans_polled_pending_chars != '\0') {
86 return (B_TRUE);
90 * Reset to an empty buffer.
92 upper->kbtrans_polled_buf[0] = '\0';
93 upper->kbtrans_polled_pending_chars = upper->kbtrans_polled_buf;
95 cb = upper->kbtrans_streams_hw_callbacks;
96 hw = upper->kbtrans_streams_hw;
99 * Process scancodes until either we have input ready
100 * or we run out of scancodes.
102 while (cb->kbtrans_polled_keycheck(hw, &key, &state)) {
103 kbtrans_processkey(&upper->kbtrans_lower,
104 &kbtrans_polled_callbacks, key, state);
106 * If that generated any input, we're ready.
108 if (*upper->kbtrans_polled_pending_chars != '\0') {
109 return (B_TRUE);
113 return (B_FALSE);
117 * kbtrans_getchar:
118 * Return a character
121 kbtrans_getchar(struct kbtrans *upper)
123 while (!kbtrans_ischar(upper))
124 /* LOOP */;
126 return (*upper->kbtrans_polled_pending_chars++);
129 void
130 kbtrans_polled_putcode(struct kbtrans *upper, char code)
132 int i;
135 * NB: KBTRANS_POLLED_BUF_SIZE is one smaller than
136 * the size of the buffer, to allow for a trailing
137 * null.
139 for (i = 0; i < KBTRANS_POLLED_BUF_SIZE; i++) {
140 if (upper->kbtrans_polled_buf[i] == '\0') {
141 upper->kbtrans_polled_buf[i] = code;
142 upper->kbtrans_polled_buf[i+1] = '\0';
143 return;
146 DPRINTF(PRINT_L2, PRINT_MASK_PACKET,
147 (upper, "kbtrans_polled_pressed: "
148 "buffer overflow, character 0x%x discarded\n", code));
150 * Didn't fit, throw it on the floor.
155 * kbtrans_polled_pressed:
156 * This function is called when we are in polled mode and a key is
157 * pressed. The key is put into the kbtrans_polled_buf so that it
158 * can be picked up later by kbtrans_ischar()
160 /*ARGSUSED2*/
161 static void
162 kbtrans_polled_pressed(
163 struct kbtrans *upper,
164 uint_t entrytype,
165 kbtrans_key_t key,
166 uint_t entry)
168 struct kbtrans_lower *lower = &upper->kbtrans_lower;
169 register char *cp;
172 * Based on the type of key, we may need to do some ASCII
173 * specific post processing.
175 switch (entrytype) {
177 case BUCKYBITS:
178 case SHIFTKEYS:
179 case FUNNY:
181 * There is no ascii equivalent. We will ignore these
182 * keys
184 break;
186 case FUNCKEYS:
188 * These will no doubt only cause problems. Ignore them.
191 break;
193 case STRING:
195 * These are the multi byte keys (Home, Up, Down ...)
197 cp = &lower->kbtrans_keystringtab[entry & 0x0F][0];
200 * Copy the string from the keystringtable, and send it
201 * upstream a character at a time.
203 while (*cp != '\0') {
204 kbtrans_polled_putcode(upper, *cp);
205 cp++;
208 return;
210 case PADKEYS:
212 * These are the keys on the keypad. Look up the
213 * answer in the kb_numlock_table and send it upstream.
215 kbtrans_polled_putcode(upper,
216 lower->kbtrans_numlock_table[entry&0x1F]);
218 break;
220 case 0: /* normal character */
221 default:
223 * Send the byte upstream.
225 kbtrans_polled_putcode(upper, (char)entry);
226 break;
231 * kbtrans_polled_released:
232 * This function is called when a key is released. Nothing is
233 * done.
235 /*ARGSUSED*/
236 static void
237 kbtrans_polled_released(struct kbtrans *upper, kbtrans_key_t key)
239 /* Nothing for now */
243 * kbtrans_polled_setled:
244 * This function is called to set the LEDs.
246 static void
247 kbtrans_polled_setled(struct kbtrans *upper)
249 struct kbtrans_callbacks *cb;
250 struct kbtrans_hardware *hw;
252 cb = upper->kbtrans_streams_hw_callbacks;
253 hw = upper->kbtrans_streams_hw;
255 cb->kbtrans_polled_setled(hw, upper->kbtrans_lower.kbtrans_led_state);
259 * kbtrans_polled_setup_repeat:
260 * Function to be called in order to handle a repeating key.
261 * Nothing is done.
263 /*ARGSUSED*/
264 static void
265 kbtrans_polled_setup_repeat(
266 struct kbtrans *upper,
267 uint_t entrytype,
268 kbtrans_key_t key)
270 /* Nothing for now */
274 * kbtrans_polled_cancel_repeat:
275 * Function to be called to cancel a repeating key,
276 * so that we don't end up with an autorepeating key
277 * on the stream because its release was handled by the
278 * polled code.
280 static void
281 kbtrans_polled_cancel_repeat(struct kbtrans *upper)
284 * Streams code will time out and will discard the
285 * autorepeat.
287 upper->kbtrans_lower.kbtrans_repeatkey = 0;