xref: /openbmc/u-boot/include/input.h (revision e8f80a5a)
1*83d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */
29bc590e5SSimon Glass /*
39bc590e5SSimon Glass  * Keyboard input helper functions (too small to be called a layer)
49bc590e5SSimon Glass  *
59bc590e5SSimon Glass  * Copyright (c) 2011 The Chromium OS Authors.
69bc590e5SSimon Glass  */
79bc590e5SSimon Glass 
89bc590e5SSimon Glass #ifndef _INPUT_H
99bc590e5SSimon Glass #define _INPUT_H
109bc590e5SSimon Glass 
119bc590e5SSimon Glass enum {
129bc590e5SSimon Glass 	INPUT_MAX_MODIFIERS	= 4,
139bc590e5SSimon Glass 	INPUT_BUFFER_LEN	= 16,
149bc590e5SSimon Glass };
159bc590e5SSimon Glass 
169bc590e5SSimon Glass enum {
179bc590e5SSimon Glass 	/* Keyboard LEDs */
189bc590e5SSimon Glass 	INPUT_LED_SCROLL	= 1 << 0,
19377a0696SBin Meng 	INPUT_LED_NUM		= 1 << 1,
20377a0696SBin Meng 	INPUT_LED_CAPS		= 1 << 2,
219bc590e5SSimon Glass };
229bc590e5SSimon Glass 
239bc590e5SSimon Glass /*
249bc590e5SSimon Glass  * This table translates key codes to ASCII. Most of the entries are ASCII
259bc590e5SSimon Glass  * codes, but entries after KEY_FIRST_MOD indicate that this key is a
269bc590e5SSimon Glass  * modifier key, like shift, ctrl. KEY_FIRST_MOD + MOD_SHIFT is the shift
279bc590e5SSimon Glass  * key, for example.
289bc590e5SSimon Glass  */
299bc590e5SSimon Glass struct input_key_xlate {
309bc590e5SSimon Glass 	/* keycode of the modifiers which select this table, -1 if none */
319bc590e5SSimon Glass 	int left_keycode;
329bc590e5SSimon Glass 	int right_keycode;
339bc590e5SSimon Glass 	const uchar *xlate;	/* keycode to ASCII table */
349bc590e5SSimon Glass 	int num_entries;	/* number of entries in this table */
359bc590e5SSimon Glass };
369bc590e5SSimon Glass 
379bc590e5SSimon Glass struct input_config {
38c9cac4cdSSimon Glass 	struct udevice *dev;
399bc590e5SSimon Glass 	uchar fifo[INPUT_BUFFER_LEN];
409bc590e5SSimon Glass 	int fifo_in, fifo_out;
419bc590e5SSimon Glass 
429bc590e5SSimon Glass 	/* Which modifiers are active (1 bit for each MOD_... value) */
439bc590e5SSimon Glass 	uchar modifiers;
449bc590e5SSimon Glass 	uchar flags;		/* active state keys (FLAGS_...) */
453b5f6f50SSimon Glass 	uchar leds;		/* active LEDs (INPUT_LED_...) */
463b5f6f50SSimon Glass 	uchar leds_changed;	/* LEDs that just changed */
479bc590e5SSimon Glass 	uchar num_tables;	/* number of modifier tables */
489bc590e5SSimon Glass 	int prev_keycodes[INPUT_BUFFER_LEN];	/* keys held last time */
499bc590e5SSimon Glass 	int num_prev_keycodes;	/* number of prev keys */
509bc590e5SSimon Glass 	struct input_key_xlate table[INPUT_MAX_MODIFIERS];
519bc590e5SSimon Glass 
529bc590e5SSimon Glass 	/**
539bc590e5SSimon Glass 	 * Function the input helper calls to scan the keyboard
549bc590e5SSimon Glass 	 *
559bc590e5SSimon Glass 	 * @param config	Input state
569bc590e5SSimon Glass 	 * @return 0 if no keys read, otherwise number of keys read, or 1 if
579bc590e5SSimon Glass 	 *		unknown
589bc590e5SSimon Glass 	 */
599bc590e5SSimon Glass 	int (*read_keys)(struct input_config *config);
600b186c08SSimon Glass 	bool allow_repeats;		/* Don't filter out repeats */
619bc590e5SSimon Glass 	unsigned int next_repeat_ms;	/* Next time we repeat a key */
629bc590e5SSimon Glass 	unsigned int repeat_delay_ms;	/* Time before autorepeat starts */
639bc590e5SSimon Glass 	unsigned int repeat_rate_ms;	/* Autorepeat rate in ms */
649bc590e5SSimon Glass };
659bc590e5SSimon Glass 
669bc590e5SSimon Glass struct stdio_dev;
679bc590e5SSimon Glass 
689bc590e5SSimon Glass /**
699bc590e5SSimon Glass  * Convert a list of key codes into ASCII and send them
709bc590e5SSimon Glass  *
719bc590e5SSimon Glass  * @param config	Input state
729bc590e5SSimon Glass  * @param keycode	List of key codes to examine
739bc590e5SSimon Glass  * @param num_keycodes	Number of key codes
7444abe47dSHung-Te Lin  * @return number of ascii characters sent, or 0 if none, or -1 for an
7544abe47dSHung-Te Lin  *	internal error
769bc590e5SSimon Glass  */
779bc590e5SSimon Glass int input_send_keycodes(struct input_config *config, int keycode[], int count);
789bc590e5SSimon Glass 
799bc590e5SSimon Glass /**
803a85e436SSimon Glass  * Add a new keycode to an existing list of keycodes
813a85e436SSimon Glass  *
823a85e436SSimon Glass  * This can be used to handle keyboards which do their own scanning. An
833a85e436SSimon Glass  * internal list of depressed keys is maintained by the input library. Then
843a85e436SSimon Glass  * this function is called to add a new key to the list (when a 'make code' is
853a85e436SSimon Glass  * received), or remove a key (when a 'break code' is received).
863a85e436SSimon Glass  *
873a85e436SSimon Glass  * This function looks after maintenance of the list of active keys, and calls
883a85e436SSimon Glass  * input_send_keycodes() with its updated list.
893a85e436SSimon Glass  *
903a85e436SSimon Glass  * @param config	Input state
913a85e436SSimon Glass  * @param new_keycode	New keycode to add/remove
923a85e436SSimon Glass  * @param release	true if this key was released, false if depressed
933a85e436SSimon Glass  * @return number of ascii characters sent, or 0 if none, or -1 for an
943a85e436SSimon Glass  *	internal error
953a85e436SSimon Glass  */
963a85e436SSimon Glass int input_add_keycode(struct input_config *config, int new_keycode,
973a85e436SSimon Glass 		      bool release);
983a85e436SSimon Glass 
993a85e436SSimon Glass /**
1009bc590e5SSimon Glass  * Add a new key translation table to the input
1019bc590e5SSimon Glass  *
1029bc590e5SSimon Glass  * @param config	Input state
1039bc590e5SSimon Glass  * @param left_keycode	Key to hold to get into this table
1049bc590e5SSimon Glass  * @param right_keycode	Another key to hold to get into this table
1059bc590e5SSimon Glass  * @param xlate		Conversion table from key codes to ASCII
1069bc590e5SSimon Glass  * @param num_entries	Number of entries in xlate table
1079bc590e5SSimon Glass  */
1089bc590e5SSimon Glass int input_add_table(struct input_config *config, int left_keycode,
1099bc590e5SSimon Glass 		    int right_keycode, const uchar *xlate, int num_entries);
1109bc590e5SSimon Glass 
1119bc590e5SSimon Glass /**
1129bc590e5SSimon Glass  * Test if keys are available to be read
1139bc590e5SSimon Glass  *
1149bc590e5SSimon Glass  * @param config	Input state
1159bc590e5SSimon Glass  * @return 0 if no keys available, 1 if keys are available
1169bc590e5SSimon Glass  */
1179bc590e5SSimon Glass int input_tstc(struct input_config *config);
1189bc590e5SSimon Glass 
1199bc590e5SSimon Glass /**
1209bc590e5SSimon Glass  * Read a key
1219bc590e5SSimon Glass  *
1229bc590e5SSimon Glass  * TODO: U-Boot wants 0 for no key, but Ctrl-@ is a valid key...
1239bc590e5SSimon Glass  *
1249bc590e5SSimon Glass  * @param config	Input state
1259bc590e5SSimon Glass  * @return key, or 0 if no key, or -1 if error
1269bc590e5SSimon Glass  */
1279bc590e5SSimon Glass int input_getc(struct input_config *config);
1289bc590e5SSimon Glass 
1299bc590e5SSimon Glass /**
1309bc590e5SSimon Glass  * Register a new device with stdio and switch to it if wanted
1319bc590e5SSimon Glass  *
1329bc590e5SSimon Glass  * @param dev	Pointer to device
1339bc590e5SSimon Glass  * @return 0 if ok, -1 on error
1349bc590e5SSimon Glass  */
1359bc590e5SSimon Glass int input_stdio_register(struct stdio_dev *dev);
1369bc590e5SSimon Glass 
1379bc590e5SSimon Glass /**
1381b1d3e64SSimon Glass  * Set up the keyboard autorepeat delays
1391b1d3e64SSimon Glass  *
1401b1d3e64SSimon Glass  * @param repeat_delay_ms	Delay before key auto-repeat starts (in ms)
1411b1d3e64SSimon Glass  * @param repeat_rate_ms	Delay between successive key repeats (in ms)
1421b1d3e64SSimon Glass  */
1431b1d3e64SSimon Glass void input_set_delays(struct input_config *config, int repeat_delay_ms,
1441b1d3e64SSimon Glass 	       int repeat_rate_ms);
1451b1d3e64SSimon Glass 
1461b1d3e64SSimon Glass /**
1470b186c08SSimon Glass  * Tell the input layer whether to allow the caller to determine repeats
1480b186c08SSimon Glass  *
1490b186c08SSimon Glass  * Generally the input library handles processing of a list of scanned keys.
1500b186c08SSimon Glass  * Repeated keys need to be generated based on a timer in this case, since all
1510b186c08SSimon Glass  * that is provided is a list of keys current depressed.
1520b186c08SSimon Glass  *
1530b186c08SSimon Glass  * Keyboards which do their own scanning will resend codes when they want to
1540b186c08SSimon Glass  * inject a repeating key. This function can be called at start-up to select
1550b186c08SSimon Glass  * this behaviour.
1560b186c08SSimon Glass  *
1570b186c08SSimon Glass  * @param config	Input state
1580b186c08SSimon Glass  * @param allow_repeats	true to repeat depressed keys every time
1590b186c08SSimon Glass  *			input_send_keycodes() is called, false to do normal
1600b186c08SSimon Glass  *			keyboard repeat processing with a timer.
1610b186c08SSimon Glass  */
1620b186c08SSimon Glass void input_allow_repeats(struct input_config *config, bool allow_repeats);
1630b186c08SSimon Glass 
1640b186c08SSimon Glass /**
1653b5f6f50SSimon Glass  * Check if keyboard LEDs need to be updated
1663b5f6f50SSimon Glass  *
1673b5f6f50SSimon Glass  * This can be called after input_tstc() to see if keyboard LEDs need
1683b5f6f50SSimon Glass  * updating.
1693b5f6f50SSimon Glass  *
1703b5f6f50SSimon Glass  * @param config	Input state
1713b5f6f50SSimon Glass  * @return -1 if no LEDs need updating, other value if they do
1723b5f6f50SSimon Glass  */
1733b5f6f50SSimon Glass int input_leds_changed(struct input_config *config);
1743b5f6f50SSimon Glass 
1753b5f6f50SSimon Glass /**
17666877b0fSSimon Glass  * Set up the key map tables
17766877b0fSSimon Glass  *
17866877b0fSSimon Glass  * This must be called after input_init() or keycode decoding will not work.
17966877b0fSSimon Glass  *
18066877b0fSSimon Glass  * @param config	Input state
181b1d7a187SSimon Glass  * @param german	true to use German keyboard layout, false for US
18266877b0fSSimon Glass  * @return 0 if ok, -1 on error
18366877b0fSSimon Glass  */
184b1d7a187SSimon Glass int input_add_tables(struct input_config *config, bool german);
18566877b0fSSimon Glass 
18666877b0fSSimon Glass /**
1879bc590e5SSimon Glass  * Set up the input handler with basic key maps.
1889bc590e5SSimon Glass  *
1899bc590e5SSimon Glass  * @param config	Input state
1909bc590e5SSimon Glass  * @param leds		Initial LED value (INPUT_LED_ mask), 0 suggested
1919bc590e5SSimon Glass  * @return 0 if ok, -1 on error
1929bc590e5SSimon Glass  */
1931b1d3e64SSimon Glass int input_init(struct input_config *config, int leds);
1949bc590e5SSimon Glass 
1959bc590e5SSimon Glass #ifdef CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE
1969bc590e5SSimon Glass extern int overwrite_console(void);
1979bc590e5SSimon Glass #define OVERWRITE_CONSOLE overwrite_console()
1989bc590e5SSimon Glass #else
1999bc590e5SSimon Glass #define OVERWRITE_CONSOLE 0
2009bc590e5SSimon Glass #endif /* CONFIG_SYS_CONSOLE_OVERWRITE_ROUTINE */
2019bc590e5SSimon Glass 
2029bc590e5SSimon Glass #endif
203