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