pc.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00038 #include <kbd.h>
00039 #include <io/console.h>
00040 #include <io/keycode.h>
00041 #include <kbd_ctl.h>
00042 #include <kbd_port.h>
00043 #include <gsp.h>
00044 
00045 enum dec_state {
00046         ds_s,
00047         ds_e
00048 };
00049 
00050 enum special_code {
00051         SC_ACK = 0xfa,
00052         SC_NAK = 0xfe
00053 };
00054 
00055 enum lock_ind_bits {
00056         LI_SCROLL       = 0x01,
00057         LI_NUM          = 0x02,
00058         LI_CAPS         = 0x04
00059 };
00060 
00061 enum kbd_command {
00062         KBD_CMD_SET_LEDS = 0xed
00063 };
00064 
00065 static enum dec_state ds;
00066 
00067 static int scanmap_simple[] = {
00068 
00069         [0x29] = KC_BACKTICK,
00070 
00071         [0x02] = KC_1,
00072         [0x03] = KC_2,
00073         [0x04] = KC_3,
00074         [0x05] = KC_4,
00075         [0x06] = KC_5,
00076         [0x07] = KC_6,
00077         [0x08] = KC_7,
00078         [0x09] = KC_8,
00079         [0x0a] = KC_9,
00080         [0x0b] = KC_0,
00081 
00082         [0x0c] = KC_MINUS,
00083         [0x0d] = KC_EQUALS,
00084         [0x0e] = KC_BACKSPACE,
00085 
00086         [0x0f] = KC_TAB,
00087 
00088         [0x10] = KC_Q,
00089         [0x11] = KC_W,
00090         [0x12] = KC_E,
00091         [0x13] = KC_R,
00092         [0x14] = KC_T,
00093         [0x15] = KC_Y,
00094         [0x16] = KC_U,
00095         [0x17] = KC_I,
00096         [0x18] = KC_O,
00097         [0x19] = KC_P,
00098 
00099         [0x1a] = KC_LBRACKET,
00100         [0x1b] = KC_RBRACKET,
00101 
00102         [0x3a] = KC_CAPS_LOCK,
00103 
00104         [0x1e] = KC_A,
00105         [0x1f] = KC_S,
00106         [0x20] = KC_D,
00107         [0x21] = KC_F,
00108         [0x22] = KC_G,
00109         [0x23] = KC_H,
00110         [0x24] = KC_J,
00111         [0x25] = KC_K,
00112         [0x26] = KC_L,
00113 
00114         [0x27] = KC_SEMICOLON,
00115         [0x28] = KC_QUOTE,
00116         [0x2b] = KC_BACKSLASH,
00117 
00118         [0x2a] = KC_LSHIFT,
00119 
00120         [0x2c] = KC_Z,
00121         [0x2d] = KC_X,
00122         [0x2e] = KC_C,
00123         [0x2f] = KC_V,
00124         [0x30] = KC_B,
00125         [0x31] = KC_N,
00126         [0x32] = KC_M,
00127 
00128         [0x33] = KC_COMMA,
00129         [0x34] = KC_PERIOD,
00130         [0x35] = KC_SLASH,
00131 
00132         [0x36] = KC_RSHIFT,
00133 
00134         [0x1d] = KC_LCTRL,
00135         [0x38] = KC_LALT,
00136         [0x39] = KC_SPACE,
00137 
00138         [0x01] = KC_ESCAPE,
00139 
00140         [0x3b] = KC_F1,
00141         [0x3c] = KC_F2,
00142         [0x3d] = KC_F3,
00143         [0x3e] = KC_F4,
00144         [0x3f] = KC_F5,
00145         [0x40] = KC_F6,
00146         [0x41] = KC_F7,
00147 
00148         [0x42] = KC_F8,
00149         [0x43] = KC_F9,
00150         [0x44] = KC_F10,
00151 
00152         [0x57] = KC_F11,
00153         [0x58] = KC_F12,
00154 
00155         [0x46] = KC_SCROLL_LOCK,
00156 
00157         [0x1c] = KC_ENTER,
00158 
00159         [0x45] = KC_NUM_LOCK,
00160         [0x37] = KC_NTIMES,
00161         [0x4a] = KC_NMINUS,
00162         [0x4e] = KC_NPLUS,
00163         [0x47] = KC_N7,
00164         [0x48] = KC_N8,
00165         [0x49] = KC_N9,
00166         [0x4b] = KC_N4,
00167         [0x4c] = KC_N5,
00168         [0x4d] = KC_N6,
00169         [0x4f] = KC_N1,
00170         [0x50] = KC_N2,
00171         [0x51] = KC_N3,
00172         [0x52] = KC_N0,
00173         [0x53] = KC_NPERIOD
00174 };
00175 
00176 static int scanmap_e0[] = {
00177         [0x38] = KC_RALT,
00178         [0x1d] = KC_RSHIFT,
00179 
00180         [0x37] = KC_PRTSCR,
00181 
00182         [0x52] = KC_INSERT,
00183         [0x47] = KC_HOME,
00184         [0x49] = KC_PAGE_UP,
00185 
00186         [0x53] = KC_DELETE,
00187         [0x4f] = KC_END,
00188         [0x51] = KC_PAGE_DOWN,
00189 
00190         [0x48] = KC_UP,
00191         [0x4b] = KC_LEFT,
00192         [0x50] = KC_DOWN,
00193         [0x4d] = KC_RIGHT,
00194 
00195         [0x35] = KC_NSLASH,
00196         [0x1c] = KC_NENTER
00197 };
00198 
00199 int kbd_ctl_init(void)
00200 {
00201         ds = ds_s;
00202         return 0;
00203 }
00204 
00205 void kbd_ctl_parse_scancode(int scancode)
00206 {
00207         console_ev_type_t type;
00208         unsigned int key;
00209         int *map;
00210         size_t map_length;
00211 
00212         /*
00213          * ACK/NAK are returned as response to us sending a command.
00214          * We are not interested in them.
00215          */
00216         if (scancode == SC_ACK || scancode == SC_NAK)
00217                 return;
00218 
00219         if (scancode == 0xe0) {
00220                 ds = ds_e;
00221                 return;
00222         }
00223 
00224         switch (ds) {
00225         case ds_s:
00226                 map = scanmap_simple;
00227                 map_length = sizeof(scanmap_simple) / sizeof(int);
00228                 break;
00229         case ds_e:
00230                 map = scanmap_e0;
00231                 map_length = sizeof(scanmap_e0) / sizeof(int);
00232                 break;
00233         default:
00234                 map = NULL;
00235                 map_length = 0;
00236         }
00237 
00238         ds = ds_s;
00239 
00240         if (scancode & 0x80) {
00241                 scancode &= ~0x80;
00242                 type = KEY_RELEASE;
00243         } else {
00244                 type = KEY_PRESS;
00245         }
00246 
00247         if ((scancode < 0) || ((size_t) scancode >= map_length))
00248                 return;
00249 
00250         key = map[scancode];
00251         if (key != 0)
00252                 kbd_push_ev(type, key);
00253 }
00254 
00255 void kbd_ctl_set_ind(unsigned mods)
00256 {
00257         uint8_t b;
00258 
00259         b = 0;
00260         if ((mods & KM_CAPS_LOCK) != 0)
00261                 b = b | LI_CAPS;
00262         if ((mods & KM_NUM_LOCK) != 0)
00263                 b = b | LI_NUM;
00264         if ((mods & KM_SCROLL_LOCK) != 0)
00265                 b = b | LI_SCROLL;
00266 
00267         kbd_port_write(KBD_CMD_SET_LEDS);
00268         kbd_port_write(b);
00269 }
00270 

Generated on Thu Jun 2 07:45:49 2011 for HelenOS/USB by  doxygen 1.4.7