1*4882a593Smuzhiyun /* 2*4882a593Smuzhiyun * Keyboard input helper functions (too small to be called a layer) 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Copyright (c) 2011 The Chromium OS Authors. 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+ 7*4882a593Smuzhiyun */ 8*4882a593Smuzhiyun 9*4882a593Smuzhiyun #ifndef _INPUT_H 10*4882a593Smuzhiyun #define _INPUT_H 11*4882a593Smuzhiyun 12*4882a593Smuzhiyun enum { 13*4882a593Smuzhiyun INPUT_MAX_MODIFIERS = 4, 14*4882a593Smuzhiyun INPUT_BUFFER_LEN = 16, 15*4882a593Smuzhiyun }; 16*4882a593Smuzhiyun 17*4882a593Smuzhiyun enum { 18*4882a593Smuzhiyun /* Keyboard LEDs */ 19*4882a593Smuzhiyun INPUT_LED_SCROLL = 1 << 0, 20*4882a593Smuzhiyun INPUT_LED_NUM = 1 << 1, 21*4882a593Smuzhiyun INPUT_LED_CAPS = 1 << 2, 22*4882a593Smuzhiyun }; 23*4882a593Smuzhiyun 24*4882a593Smuzhiyun /* 25*4882a593Smuzhiyun * This table translates key codes to ASCII. Most of the entries are ASCII 26*4882a593Smuzhiyun * codes, but entries after KEY_FIRST_MOD indicate that this key is a 27*4882a593Smuzhiyun * modifier key, like shift, ctrl. KEY_FIRST_MOD + MOD_SHIFT is the shift 28*4882a593Smuzhiyun * key, for example. 29*4882a593Smuzhiyun */ 30*4882a593Smuzhiyun struct input_key_xlate { 31*4882a593Smuzhiyun /* keycode of the modifiers which select this table, -1 if none */ 32*4882a593Smuzhiyun int left_keycode; 33*4882a593Smuzhiyun int right_keycode; 34*4882a593Smuzhiyun const uchar *xlate; /* keycode to ASCII table */ 35*4882a593Smuzhiyun int num_entries; /* number of entries in this table */ 36*4882a593Smuzhiyun }; 37*4882a593Smuzhiyun 38*4882a593Smuzhiyun struct input_config { 39*4882a593Smuzhiyun struct udevice *dev; 40*4882a593Smuzhiyun uchar fifo[INPUT_BUFFER_LEN]; 41*4882a593Smuzhiyun int fifo_in, fifo_out; 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun /* Which modifiers are active (1 bit for each MOD_... value) */ 44*4882a593Smuzhiyun uchar modifiers; 45*4882a593Smuzhiyun uchar flags; /* active state keys (FLAGS_...) */ 46*4882a593Smuzhiyun uchar leds; /* active LEDs (INPUT_LED_...) */ 47*4882a593Smuzhiyun uchar leds_changed; /* LEDs that just changed */ 48*4882a593Smuzhiyun uchar num_tables; /* number of modifier tables */ 49*4882a593Smuzhiyun int prev_keycodes[INPUT_BUFFER_LEN]; /* keys held last time */ 50*4882a593Smuzhiyun int num_prev_keycodes; /* number of prev keys */ 51*4882a593Smuzhiyun struct input_key_xlate table[INPUT_MAX_MODIFIERS]; 52*4882a593Smuzhiyun 53*4882a593Smuzhiyun /** 54*4882a593Smuzhiyun * Function the input helper calls to scan the keyboard 55*4882a593Smuzhiyun * 56*4882a593Smuzhiyun * @param config Input state 57*4882a593Smuzhiyun * @return 0 if no keys read, otherwise number of keys read, or 1 if 58*4882a593Smuzhiyun * unknown 59*4882a593Smuzhiyun */ 60*4882a593Smuzhiyun int (*read_keys)(struct input_config *config); 61*4882a593Smuzhiyun bool allow_repeats; /* Don't filter out repeats */ 62*4882a593Smuzhiyun unsigned int next_repeat_ms; /* Next time we repeat a key */ 63*4882a593Smuzhiyun unsigned int repeat_delay_ms; /* Time before autorepeat starts */ 64*4882a593Smuzhiyun unsigned int repeat_rate_ms; /* Autorepeat rate in ms */ 65*4882a593Smuzhiyun }; 66*4882a593Smuzhiyun 67*4882a593Smuzhiyun struct stdio_dev; 68*4882a593Smuzhiyun 69*4882a593Smuzhiyun /** 70*4882a593Smuzhiyun * Convert a list of key codes into ASCII and send them 71*4882a593Smuzhiyun * 72*4882a593Smuzhiyun * @param config Input state 73*4882a593Smuzhiyun * @param keycode List of key codes to examine 74*4882a593Smuzhiyun * @param num_keycodes Number of key codes 75*4882a593Smuzhiyun * @return number of ascii characters sent, or 0 if none, or -1 for an 76*4882a593Smuzhiyun * internal error 77*4882a593Smuzhiyun */ 78*4882a593Smuzhiyun int input_send_keycodes(struct input_config *config, int keycode[], int count); 79*4882a593Smuzhiyun 80*4882a593Smuzhiyun /** 81*4882a593Smuzhiyun * Add a new keycode to an existing list of keycodes 82*4882a593Smuzhiyun * 83*4882a593Smuzhiyun * This can be used to handle keyboards which do their own scanning. An 84*4882a593Smuzhiyun * internal list of depressed keys is maintained by the input library. Then 85*4882a593Smuzhiyun * this function is called to add a new key to the list (when a 'make code' is 86*4882a593Smuzhiyun * received), or remove a key (when a 'break code' is received). 87*4882a593Smuzhiyun * 88*4882a593Smuzhiyun * This function looks after maintenance of the list of active keys, and calls 89*4882a593Smuzhiyun * input_send_keycodes() with its updated list. 90*4882a593Smuzhiyun * 91*4882a593Smuzhiyun * @param config Input state 92*4882a593Smuzhiyun * @param new_keycode New keycode to add/remove 93*4882a593Smuzhiyun * @param release true if this key was released, false if depressed 94*4882a593Smuzhiyun * @return number of ascii characters sent, or 0 if none, or -1 for an 95*4882a593Smuzhiyun * internal error 96*4882a593Smuzhiyun */ 97*4882a593Smuzhiyun int input_add_keycode(struct input_config *config, int new_keycode, 98*4882a593Smuzhiyun bool release); 99*4882a593Smuzhiyun 100*4882a593Smuzhiyun /** 101*4882a593Smuzhiyun * Add a new key translation table to the input 102*4882a593Smuzhiyun * 103*4882a593Smuzhiyun * @param config Input state 104*4882a593Smuzhiyun * @param left_keycode Key to hold to get into this table 105*4882a593Smuzhiyun * @param right_keycode Another key to hold to get into this table 106*4882a593Smuzhiyun * @param xlate Conversion table from key codes to ASCII 107*4882a593Smuzhiyun * @param num_entries Number of entries in xlate table 108*4882a593Smuzhiyun */ 109*4882a593Smuzhiyun int input_add_table(struct input_config *config, int left_keycode, 110*4882a593Smuzhiyun int right_keycode, const uchar *xlate, int num_entries); 111*4882a593Smuzhiyun 112*4882a593Smuzhiyun /** 113*4882a593Smuzhiyun * Test if keys are available to be read 114*4882a593Smuzhiyun * 115*4882a593Smuzhiyun * @param config Input state 116*4882a593Smuzhiyun * @return 0 if no keys available, 1 if keys are available 117*4882a593Smuzhiyun */ 118*4882a593Smuzhiyun int input_tstc(struct input_config *config); 119*4882a593Smuzhiyun 120*4882a593Smuzhiyun /** 121*4882a593Smuzhiyun * Read a key 122*4882a593Smuzhiyun * 123*4882a593Smuzhiyun * TODO: U-Boot wants 0 for no key, but Ctrl-@ is a valid key... 124*4882a593Smuzhiyun * 125*4882a593Smuzhiyun * @param config Input state 126*4882a593Smuzhiyun * @return key, or 0 if no key, or -1 if error 127*4882a593Smuzhiyun */ 128*4882a593Smuzhiyun int input_getc(struct input_config *config); 129*4882a593Smuzhiyun 130*4882a593Smuzhiyun /** 131*4882a593Smuzhiyun * Register a new device with stdio and switch to it if wanted 132*4882a593Smuzhiyun * 133*4882a593Smuzhiyun * @param dev Pointer to device 134*4882a593Smuzhiyun * @return 0 if ok, -1 on error 135*4882a593Smuzhiyun */ 136*4882a593Smuzhiyun int input_stdio_register(struct stdio_dev *dev); 137*4882a593Smuzhiyun 138*4882a593Smuzhiyun /** 139*4882a593Smuzhiyun * Set up the keyboard autorepeat delays 140*4882a593Smuzhiyun * 141*4882a593Smuzhiyun * @param repeat_delay_ms Delay before key auto-repeat starts (in ms) 142*4882a593Smuzhiyun * @param repeat_rate_ms Delay between successive key repeats (in ms) 143*4882a593Smuzhiyun */ 144*4882a593Smuzhiyun void input_set_delays(struct input_config *config, int repeat_delay_ms, 145*4882a593Smuzhiyun int repeat_rate_ms); 146*4882a593Smuzhiyun 147*4882a593Smuzhiyun /** 148*4882a593Smuzhiyun * Tell the input layer whether to allow the caller to determine repeats 149*4882a593Smuzhiyun * 150*4882a593Smuzhiyun * Generally the input library handles processing of a list of scanned keys. 151*4882a593Smuzhiyun * Repeated keys need to be generated based on a timer in this case, since all 152*4882a593Smuzhiyun * that is provided is a list of keys current depressed. 153*4882a593Smuzhiyun * 154*4882a593Smuzhiyun * Keyboards which do their own scanning will resend codes when they want to 155*4882a593Smuzhiyun * inject a repeating key. This function can be called at start-up to select 156*4882a593Smuzhiyun * this behaviour. 157*4882a593Smuzhiyun * 158*4882a593Smuzhiyun * @param config Input state 159*4882a593Smuzhiyun * @param allow_repeats true to repeat depressed keys every time 160*4882a593Smuzhiyun * input_send_keycodes() is called, false to do normal 161*4882a593Smuzhiyun * keyboard repeat processing with a timer. 162*4882a593Smuzhiyun */ 163*4882a593Smuzhiyun void input_allow_repeats(struct input_config *config, bool allow_repeats); 164*4882a593Smuzhiyun 165*4882a593Smuzhiyun /** 166*4882a593Smuzhiyun * Check if keyboard LEDs need to be updated 167*4882a593Smuzhiyun * 168*4882a593Smuzhiyun * This can be called after input_tstc() to see if keyboard LEDs need 169*4882a593Smuzhiyun * updating. 170*4882a593Smuzhiyun * 171*4882a593Smuzhiyun * @param config Input state 172*4882a593Smuzhiyun * @return -1 if no LEDs need updating, other value if they do 173*4882a593Smuzhiyun */ 174*4882a593Smuzhiyun int input_leds_changed(struct input_config *config); 175*4882a593Smuzhiyun 176*4882a593Smuzhiyun /** 177*4882a593Smuzhiyun * Set up the key map tables 178*4882a593Smuzhiyun * 179*4882a593Smuzhiyun * This must be called after input_init() or keycode decoding will not work. 180*4882a593Smuzhiyun * 181*4882a593Smuzhiyun * @param config Input state 182*4882a593Smuzhiyun * @param german true to use German keyboard layout, false for US 183*4882a593Smuzhiyun * @return 0 if ok, -1 on error 184*4882a593Smuzhiyun */ 185*4882a593Smuzhiyun int input_add_tables(struct input_config *config, bool german); 186*4882a593Smuzhiyun 187*4882a593Smuzhiyun /** 188*4882a593Smuzhiyun * Set up the input handler with basic key maps. 189*4882a593Smuzhiyun * 190*4882a593Smuzhiyun * @param config Input state 191*4882a593Smuzhiyun * @param leds Initial LED value (INPUT_LED_ mask), 0 suggested 192*4882a593Smuzhiyun * @return 0 if ok, -1 on error 193*4882a593Smuzhiyun */ 194*4882a593Smuzhiyun int input_init(struct input_config *config, int leds); 195*4882a593Smuzhiyun 196*4882a593Smuzhiyun #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE 197*4882a593Smuzhiyun extern int overwrite_console(void); 198*4882a593Smuzhiyun #define OVERWRITE_CONSOLE overwrite_console() 199*4882a593Smuzhiyun #else 200*4882a593Smuzhiyun #define OVERWRITE_CONSOLE 0 201*4882a593Smuzhiyun #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */ 202*4882a593Smuzhiyun 203*4882a593Smuzhiyun #endif 204