Loading...
1/*
2 * ChromeOS EC keyboard driver
3 *
4 * Copyright (C) 2012 Google, Inc
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * This driver uses the Chrome OS EC byte-level message-based protocol for
16 * communicating the keyboard state (which keys are pressed) from a keyboard EC
17 * to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
18 * but everything else (including deghosting) is done here. The main
19 * motivation for this is to keep the EC firmware as simple as possible, since
20 * it cannot be easily upgraded and EC flash/IRAM space is relatively
21 * expensive.
22 */
23
24#include <linux/module.h>
25#include <linux/i2c.h>
26#include <linux/input.h>
27#include <linux/kernel.h>
28#include <linux/notifier.h>
29#include <linux/platform_device.h>
30#include <linux/slab.h>
31#include <linux/input/matrix_keypad.h>
32#include <linux/mfd/cros_ec.h>
33#include <linux/mfd/cros_ec_commands.h>
34
35/*
36 * @rows: Number of rows in the keypad
37 * @cols: Number of columns in the keypad
38 * @row_shift: log2 or number of rows, rounded up
39 * @keymap_data: Matrix keymap data used to convert to keyscan values
40 * @ghost_filter: true to enable the matrix key-ghosting filter
41 * @old_kb_state: bitmap of keys pressed last scan
42 * @dev: Device pointer
43 * @idev: Input device
44 * @ec: Top level ChromeOS device to use to talk to EC
45 * @event_notifier: interrupt event notifier for transport devices
46 */
47struct cros_ec_keyb {
48 unsigned int rows;
49 unsigned int cols;
50 int row_shift;
51 const struct matrix_keymap_data *keymap_data;
52 bool ghost_filter;
53 uint8_t *old_kb_state;
54
55 struct device *dev;
56 struct input_dev *idev;
57 struct cros_ec_device *ec;
58 struct notifier_block notifier;
59};
60
61
62static bool cros_ec_keyb_row_has_ghosting(struct cros_ec_keyb *ckdev,
63 uint8_t *buf, int row)
64{
65 int pressed_in_row = 0;
66 int row_has_teeth = 0;
67 int col, mask;
68
69 mask = 1 << row;
70 for (col = 0; col < ckdev->cols; col++) {
71 if (buf[col] & mask) {
72 pressed_in_row++;
73 row_has_teeth |= buf[col] & ~mask;
74 if (pressed_in_row > 1 && row_has_teeth) {
75 /* ghosting */
76 dev_dbg(ckdev->dev,
77 "ghost found at: r%d c%d, pressed %d, teeth 0x%x\n",
78 row, col, pressed_in_row,
79 row_has_teeth);
80 return true;
81 }
82 }
83 }
84
85 return false;
86}
87
88/*
89 * Returns true when there is at least one combination of pressed keys that
90 * results in ghosting.
91 */
92static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
93{
94 int row;
95
96 /*
97 * Ghosting happens if for any pressed key X there are other keys
98 * pressed both in the same row and column of X as, for instance,
99 * in the following diagram:
100 *
101 * . . Y . g .
102 * . . . . . .
103 * . . . . . .
104 * . . X . Z .
105 *
106 * In this case only X, Y, and Z are pressed, but g appears to be
107 * pressed too (see Wikipedia).
108 *
109 * We can detect ghosting in a single pass (*) over the keyboard state
110 * by maintaining two arrays. pressed_in_row counts how many pressed
111 * keys we have found in a row. row_has_teeth is true if any of the
112 * pressed keys for this row has other pressed keys in its column. If
113 * at any point of the scan we find that a row has multiple pressed
114 * keys, and at least one of them is at the intersection with a column
115 * with multiple pressed keys, we're sure there is ghosting.
116 * Conversely, if there is ghosting, we will detect such situation for
117 * at least one key during the pass.
118 *
119 * (*) This looks linear in the number of keys, but it's not. We can
120 * cheat because the number of rows is small.
121 */
122 for (row = 0; row < ckdev->rows; row++)
123 if (cros_ec_keyb_row_has_ghosting(ckdev, buf, row))
124 return true;
125
126 return false;
127}
128
129/*
130 * Compares the new keyboard state to the old one and produces key
131 * press/release events accordingly. The keyboard state is 13 bytes (one byte
132 * per column)
133 */
134static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
135 uint8_t *kb_state, int len)
136{
137 struct input_dev *idev = ckdev->idev;
138 int col, row;
139 int new_state;
140 int old_state;
141 int num_cols;
142
143 num_cols = len;
144
145 if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
146 /*
147 * Simple-minded solution: ignore this state. The obvious
148 * improvement is to only ignore changes to keys involved in
149 * the ghosting, but process the other changes.
150 */
151 dev_dbg(ckdev->dev, "ghosting found\n");
152 return;
153 }
154
155 for (col = 0; col < ckdev->cols; col++) {
156 for (row = 0; row < ckdev->rows; row++) {
157 int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
158 const unsigned short *keycodes = idev->keycode;
159
160 new_state = kb_state[col] & (1 << row);
161 old_state = ckdev->old_kb_state[col] & (1 << row);
162 if (new_state != old_state) {
163 dev_dbg(ckdev->dev,
164 "changed: [r%d c%d]: byte %02x\n",
165 row, col, new_state);
166
167 input_report_key(idev, keycodes[pos],
168 new_state);
169 }
170 }
171 ckdev->old_kb_state[col] = kb_state[col];
172 }
173 input_sync(ckdev->idev);
174}
175
176static int cros_ec_keyb_open(struct input_dev *dev)
177{
178 struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
179
180 return blocking_notifier_chain_register(&ckdev->ec->event_notifier,
181 &ckdev->notifier);
182}
183
184static void cros_ec_keyb_close(struct input_dev *dev)
185{
186 struct cros_ec_keyb *ckdev = input_get_drvdata(dev);
187
188 blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
189 &ckdev->notifier);
190}
191
192static int cros_ec_keyb_get_state(struct cros_ec_keyb *ckdev, uint8_t *kb_state)
193{
194 return ckdev->ec->command_recv(ckdev->ec, EC_CMD_MKBP_STATE,
195 kb_state, ckdev->cols);
196}
197
198static int cros_ec_keyb_work(struct notifier_block *nb,
199 unsigned long state, void *_notify)
200{
201 int ret;
202 struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
203 notifier);
204 uint8_t kb_state[ckdev->cols];
205
206 ret = cros_ec_keyb_get_state(ckdev, kb_state);
207 if (ret >= 0)
208 cros_ec_keyb_process(ckdev, kb_state, ret);
209
210 return NOTIFY_DONE;
211}
212
213static int cros_ec_keyb_probe(struct platform_device *pdev)
214{
215 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
216 struct device *dev = ec->dev;
217 struct cros_ec_keyb *ckdev;
218 struct input_dev *idev;
219 struct device_node *np;
220 int err;
221
222 np = pdev->dev.of_node;
223 if (!np)
224 return -ENODEV;
225
226 ckdev = devm_kzalloc(&pdev->dev, sizeof(*ckdev), GFP_KERNEL);
227 if (!ckdev)
228 return -ENOMEM;
229 err = matrix_keypad_parse_of_params(&pdev->dev, &ckdev->rows,
230 &ckdev->cols);
231 if (err)
232 return err;
233 ckdev->old_kb_state = devm_kzalloc(&pdev->dev, ckdev->cols, GFP_KERNEL);
234 if (!ckdev->old_kb_state)
235 return -ENOMEM;
236
237 idev = devm_input_allocate_device(&pdev->dev);
238 if (!idev)
239 return -ENOMEM;
240
241 ckdev->ec = ec;
242 ckdev->notifier.notifier_call = cros_ec_keyb_work;
243 ckdev->dev = dev;
244 dev_set_drvdata(&pdev->dev, ckdev);
245
246 idev->name = ec->ec_name;
247 idev->phys = ec->phys_name;
248 __set_bit(EV_REP, idev->evbit);
249
250 idev->id.bustype = BUS_VIRTUAL;
251 idev->id.version = 1;
252 idev->id.product = 0;
253 idev->dev.parent = &pdev->dev;
254 idev->open = cros_ec_keyb_open;
255 idev->close = cros_ec_keyb_close;
256
257 ckdev->ghost_filter = of_property_read_bool(np,
258 "google,needs-ghost-filter");
259
260 err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
261 NULL, idev);
262 if (err) {
263 dev_err(dev, "cannot build key matrix\n");
264 return err;
265 }
266
267 ckdev->row_shift = get_count_order(ckdev->cols);
268
269 input_set_capability(idev, EV_MSC, MSC_SCAN);
270 input_set_drvdata(idev, ckdev);
271 ckdev->idev = idev;
272 err = input_register_device(ckdev->idev);
273 if (err) {
274 dev_err(dev, "cannot register input device\n");
275 return err;
276 }
277
278 return 0;
279}
280
281#ifdef CONFIG_PM_SLEEP
282/* Clear any keys in the buffer */
283static void cros_ec_keyb_clear_keyboard(struct cros_ec_keyb *ckdev)
284{
285 uint8_t old_state[ckdev->cols];
286 uint8_t new_state[ckdev->cols];
287 unsigned long duration;
288 int i, ret;
289
290 /*
291 * Keep reading until we see that the scan state does not change.
292 * That indicates that we are done.
293 *
294 * Assume that the EC keyscan buffer is at most 32 deep.
295 */
296 duration = jiffies;
297 ret = cros_ec_keyb_get_state(ckdev, new_state);
298 for (i = 1; !ret && i < 32; i++) {
299 memcpy(old_state, new_state, sizeof(old_state));
300 ret = cros_ec_keyb_get_state(ckdev, new_state);
301 if (0 == memcmp(old_state, new_state, sizeof(old_state)))
302 break;
303 }
304 duration = jiffies - duration;
305 dev_info(ckdev->dev, "Discarded %d keyscan(s) in %dus\n", i,
306 jiffies_to_usecs(duration));
307}
308
309static int cros_ec_keyb_resume(struct device *dev)
310{
311 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
312
313 /*
314 * When the EC is not a wake source, then it could not have caused the
315 * resume, so we clear the EC's key scan buffer. If the EC was a
316 * wake source (e.g. the lid is open and the user might press a key to
317 * wake) then the key scan buffer should be preserved.
318 */
319 if (ckdev->ec->was_wake_device)
320 cros_ec_keyb_clear_keyboard(ckdev);
321
322 return 0;
323}
324
325#endif
326
327static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
328
329static struct platform_driver cros_ec_keyb_driver = {
330 .probe = cros_ec_keyb_probe,
331 .driver = {
332 .name = "cros-ec-keyb",
333 .pm = &cros_ec_keyb_pm_ops,
334 },
335};
336
337module_platform_driver(cros_ec_keyb_driver);
338
339MODULE_LICENSE("GPL");
340MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
341MODULE_ALIAS("platform:cros-ec-keyb");
1// SPDX-License-Identifier: GPL-2.0
2// ChromeOS EC keyboard driver
3//
4// Copyright (C) 2012 Google, Inc.
5//
6// This driver uses the ChromeOS EC byte-level message-based protocol for
7// communicating the keyboard state (which keys are pressed) from a keyboard EC
8// to the AP over some bus (such as i2c, lpc, spi). The EC does debouncing,
9// but everything else (including deghosting) is done here. The main
10// motivation for this is to keep the EC firmware as simple as possible, since
11// it cannot be easily upgraded and EC flash/IRAM space is relatively
12// expensive.
13
14#include <linux/module.h>
15#include <linux/bitops.h>
16#include <linux/i2c.h>
17#include <linux/input.h>
18#include <linux/interrupt.h>
19#include <linux/kernel.h>
20#include <linux/notifier.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23#include <linux/sysrq.h>
24#include <linux/input/matrix_keypad.h>
25#include <linux/platform_data/cros_ec_commands.h>
26#include <linux/platform_data/cros_ec_proto.h>
27
28#include <asm/unaligned.h>
29
30#define MAX_NUM_TOP_ROW_KEYS 15
31
32/**
33 * struct cros_ec_keyb - Structure representing EC keyboard device
34 *
35 * @rows: Number of rows in the keypad
36 * @cols: Number of columns in the keypad
37 * @row_shift: log2 or number of rows, rounded up
38 * @keymap_data: Matrix keymap data used to convert to keyscan values
39 * @ghost_filter: true to enable the matrix key-ghosting filter
40 * @valid_keys: bitmap of existing keys for each matrix column
41 * @old_kb_state: bitmap of keys pressed last scan
42 * @dev: Device pointer
43 * @ec: Top level ChromeOS device to use to talk to EC
44 * @idev: The input device for the matrix keys.
45 * @bs_idev: The input device for non-matrix buttons and switches (or NULL).
46 * @notifier: interrupt event notifier for transport devices
47 * @function_row_physmap: An array of the encoded rows/columns for the top
48 * row function keys, in an order from left to right
49 * @num_function_row_keys: The number of top row keys in a custom keyboard
50 */
51struct cros_ec_keyb {
52 unsigned int rows;
53 unsigned int cols;
54 int row_shift;
55 const struct matrix_keymap_data *keymap_data;
56 bool ghost_filter;
57 uint8_t *valid_keys;
58 uint8_t *old_kb_state;
59
60 struct device *dev;
61 struct cros_ec_device *ec;
62
63 struct input_dev *idev;
64 struct input_dev *bs_idev;
65 struct notifier_block notifier;
66
67 u16 function_row_physmap[MAX_NUM_TOP_ROW_KEYS];
68 size_t num_function_row_keys;
69};
70
71/**
72 * struct cros_ec_bs_map - Mapping between Linux keycodes and EC button/switch
73 * bitmap #defines
74 *
75 * @ev_type: The type of the input event to generate (e.g., EV_KEY).
76 * @code: A linux keycode
77 * @bit: A #define like EC_MKBP_POWER_BUTTON or EC_MKBP_LID_OPEN
78 * @inverted: If the #define and EV_SW have opposite meanings, this is true.
79 * Only applicable to switches.
80 */
81struct cros_ec_bs_map {
82 unsigned int ev_type;
83 unsigned int code;
84 u8 bit;
85 bool inverted;
86};
87
88/* cros_ec_keyb_bs - Map EC button/switch #defines into kernel ones */
89static const struct cros_ec_bs_map cros_ec_keyb_bs[] = {
90 /* Buttons */
91 {
92 .ev_type = EV_KEY,
93 .code = KEY_POWER,
94 .bit = EC_MKBP_POWER_BUTTON,
95 },
96 {
97 .ev_type = EV_KEY,
98 .code = KEY_VOLUMEUP,
99 .bit = EC_MKBP_VOL_UP,
100 },
101 {
102 .ev_type = EV_KEY,
103 .code = KEY_VOLUMEDOWN,
104 .bit = EC_MKBP_VOL_DOWN,
105 },
106
107 /* Switches */
108 {
109 .ev_type = EV_SW,
110 .code = SW_LID,
111 .bit = EC_MKBP_LID_OPEN,
112 .inverted = true,
113 },
114 {
115 .ev_type = EV_SW,
116 .code = SW_TABLET_MODE,
117 .bit = EC_MKBP_TABLET_MODE,
118 },
119};
120
121/*
122 * Returns true when there is at least one combination of pressed keys that
123 * results in ghosting.
124 */
125static bool cros_ec_keyb_has_ghosting(struct cros_ec_keyb *ckdev, uint8_t *buf)
126{
127 int col1, col2, buf1, buf2;
128 struct device *dev = ckdev->dev;
129 uint8_t *valid_keys = ckdev->valid_keys;
130
131 /*
132 * Ghosting happens if for any pressed key X there are other keys
133 * pressed both in the same row and column of X as, for instance,
134 * in the following diagram:
135 *
136 * . . Y . g .
137 * . . . . . .
138 * . . . . . .
139 * . . X . Z .
140 *
141 * In this case only X, Y, and Z are pressed, but g appears to be
142 * pressed too (see Wikipedia).
143 */
144 for (col1 = 0; col1 < ckdev->cols; col1++) {
145 buf1 = buf[col1] & valid_keys[col1];
146 for (col2 = col1 + 1; col2 < ckdev->cols; col2++) {
147 buf2 = buf[col2] & valid_keys[col2];
148 if (hweight8(buf1 & buf2) > 1) {
149 dev_dbg(dev, "ghost found at: B[%02d]:0x%02x & B[%02d]:0x%02x",
150 col1, buf1, col2, buf2);
151 return true;
152 }
153 }
154 }
155
156 return false;
157}
158
159
160/*
161 * Compares the new keyboard state to the old one and produces key
162 * press/release events accordingly. The keyboard state is 13 bytes (one byte
163 * per column)
164 */
165static void cros_ec_keyb_process(struct cros_ec_keyb *ckdev,
166 uint8_t *kb_state, int len)
167{
168 struct input_dev *idev = ckdev->idev;
169 int col, row;
170 int new_state;
171 int old_state;
172
173 if (ckdev->ghost_filter && cros_ec_keyb_has_ghosting(ckdev, kb_state)) {
174 /*
175 * Simple-minded solution: ignore this state. The obvious
176 * improvement is to only ignore changes to keys involved in
177 * the ghosting, but process the other changes.
178 */
179 dev_dbg(ckdev->dev, "ghosting found\n");
180 return;
181 }
182
183 for (col = 0; col < ckdev->cols; col++) {
184 for (row = 0; row < ckdev->rows; row++) {
185 int pos = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
186 const unsigned short *keycodes = idev->keycode;
187
188 new_state = kb_state[col] & (1 << row);
189 old_state = ckdev->old_kb_state[col] & (1 << row);
190 if (new_state != old_state) {
191 dev_dbg(ckdev->dev,
192 "changed: [r%d c%d]: byte %02x\n",
193 row, col, new_state);
194
195 input_event(idev, EV_MSC, MSC_SCAN, pos);
196 input_report_key(idev, keycodes[pos],
197 new_state);
198 }
199 }
200 ckdev->old_kb_state[col] = kb_state[col];
201 }
202 input_sync(ckdev->idev);
203}
204
205/**
206 * cros_ec_keyb_report_bs - Report non-matrixed buttons or switches
207 *
208 * This takes a bitmap of buttons or switches from the EC and reports events,
209 * syncing at the end.
210 *
211 * @ckdev: The keyboard device.
212 * @ev_type: The input event type (e.g., EV_KEY).
213 * @mask: A bitmap of buttons from the EC.
214 */
215static void cros_ec_keyb_report_bs(struct cros_ec_keyb *ckdev,
216 unsigned int ev_type, u32 mask)
217
218{
219 struct input_dev *idev = ckdev->bs_idev;
220 int i;
221
222 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
223 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
224
225 if (map->ev_type != ev_type)
226 continue;
227
228 input_event(idev, ev_type, map->code,
229 !!(mask & BIT(map->bit)) ^ map->inverted);
230 }
231 input_sync(idev);
232}
233
234static int cros_ec_keyb_work(struct notifier_block *nb,
235 unsigned long queued_during_suspend, void *_notify)
236{
237 struct cros_ec_keyb *ckdev = container_of(nb, struct cros_ec_keyb,
238 notifier);
239 u32 val;
240 unsigned int ev_type;
241
242 /*
243 * If not wake enabled, discard key state changes during
244 * suspend. Switches will be re-checked in
245 * cros_ec_keyb_resume() to be sure nothing is lost.
246 */
247 if (queued_during_suspend && !device_may_wakeup(ckdev->dev))
248 return NOTIFY_OK;
249
250 switch (ckdev->ec->event_data.event_type) {
251 case EC_MKBP_EVENT_KEY_MATRIX:
252 pm_wakeup_event(ckdev->dev, 0);
253
254 if (ckdev->ec->event_size != ckdev->cols) {
255 dev_err(ckdev->dev,
256 "Discarded incomplete key matrix event.\n");
257 return NOTIFY_OK;
258 }
259
260 cros_ec_keyb_process(ckdev,
261 ckdev->ec->event_data.data.key_matrix,
262 ckdev->ec->event_size);
263 break;
264
265 case EC_MKBP_EVENT_SYSRQ:
266 pm_wakeup_event(ckdev->dev, 0);
267
268 val = get_unaligned_le32(&ckdev->ec->event_data.data.sysrq);
269 dev_dbg(ckdev->dev, "sysrq code from EC: %#x\n", val);
270 handle_sysrq(val);
271 break;
272
273 case EC_MKBP_EVENT_BUTTON:
274 case EC_MKBP_EVENT_SWITCH:
275 pm_wakeup_event(ckdev->dev, 0);
276
277 if (ckdev->ec->event_data.event_type == EC_MKBP_EVENT_BUTTON) {
278 val = get_unaligned_le32(
279 &ckdev->ec->event_data.data.buttons);
280 ev_type = EV_KEY;
281 } else {
282 val = get_unaligned_le32(
283 &ckdev->ec->event_data.data.switches);
284 ev_type = EV_SW;
285 }
286 cros_ec_keyb_report_bs(ckdev, ev_type, val);
287 break;
288
289 default:
290 return NOTIFY_DONE;
291 }
292
293 return NOTIFY_OK;
294}
295
296/*
297 * Walks keycodes flipping bit in buffer COLUMNS deep where bit is ROW. Used by
298 * ghosting logic to ignore NULL or virtual keys.
299 */
300static void cros_ec_keyb_compute_valid_keys(struct cros_ec_keyb *ckdev)
301{
302 int row, col;
303 int row_shift = ckdev->row_shift;
304 unsigned short *keymap = ckdev->idev->keycode;
305 unsigned short code;
306
307 BUG_ON(ckdev->idev->keycodesize != sizeof(*keymap));
308
309 for (col = 0; col < ckdev->cols; col++) {
310 for (row = 0; row < ckdev->rows; row++) {
311 code = keymap[MATRIX_SCAN_CODE(row, col, row_shift)];
312 if (code && (code != KEY_BATTERY))
313 ckdev->valid_keys[col] |= 1 << row;
314 }
315 dev_dbg(ckdev->dev, "valid_keys[%02d] = 0x%02x\n",
316 col, ckdev->valid_keys[col]);
317 }
318}
319
320/**
321 * cros_ec_keyb_info - Wrap the EC command EC_CMD_MKBP_INFO
322 *
323 * This wraps the EC_CMD_MKBP_INFO, abstracting out all of the marshalling and
324 * unmarshalling and different version nonsense into something simple.
325 *
326 * @ec_dev: The EC device
327 * @info_type: Either EC_MKBP_INFO_SUPPORTED or EC_MKBP_INFO_CURRENT.
328 * @event_type: Either EC_MKBP_EVENT_BUTTON or EC_MKBP_EVENT_SWITCH. Actually
329 * in some cases this could be EC_MKBP_EVENT_KEY_MATRIX or
330 * EC_MKBP_EVENT_HOST_EVENT too but we don't use in this driver.
331 * @result: Where we'll store the result; a union
332 * @result_size: The size of the result. Expected to be the size of one of
333 * the elements in the union.
334 *
335 * Returns 0 if no error or -error upon error.
336 */
337static int cros_ec_keyb_info(struct cros_ec_device *ec_dev,
338 enum ec_mkbp_info_type info_type,
339 enum ec_mkbp_event event_type,
340 union ec_response_get_next_data *result,
341 size_t result_size)
342{
343 struct ec_params_mkbp_info *params;
344 struct cros_ec_command *msg;
345 int ret;
346
347 msg = kzalloc(sizeof(*msg) + max_t(size_t, result_size,
348 sizeof(*params)), GFP_KERNEL);
349 if (!msg)
350 return -ENOMEM;
351
352 msg->command = EC_CMD_MKBP_INFO;
353 msg->version = 1;
354 msg->outsize = sizeof(*params);
355 msg->insize = result_size;
356 params = (struct ec_params_mkbp_info *)msg->data;
357 params->info_type = info_type;
358 params->event_type = event_type;
359
360 ret = cros_ec_cmd_xfer_status(ec_dev, msg);
361 if (ret == -ENOPROTOOPT) {
362 /* With older ECs we just return 0 for everything */
363 memset(result, 0, result_size);
364 ret = 0;
365 } else if (ret < 0) {
366 dev_warn(ec_dev->dev, "Transfer error %d/%d: %d\n",
367 (int)info_type, (int)event_type, ret);
368 } else if (ret != result_size) {
369 dev_warn(ec_dev->dev, "Wrong size %d/%d: %d != %zu\n",
370 (int)info_type, (int)event_type,
371 ret, result_size);
372 ret = -EPROTO;
373 } else {
374 memcpy(result, msg->data, result_size);
375 ret = 0;
376 }
377
378 kfree(msg);
379
380 return ret;
381}
382
383/**
384 * cros_ec_keyb_query_switches - Query the state of switches and report
385 *
386 * This will ask the EC about the current state of switches and report to the
387 * kernel. Note that we don't query for buttons because they are more
388 * transitory and we'll get an update on the next release / press.
389 *
390 * @ckdev: The keyboard device
391 *
392 * Returns 0 if no error or -error upon error.
393 */
394static int cros_ec_keyb_query_switches(struct cros_ec_keyb *ckdev)
395{
396 struct cros_ec_device *ec_dev = ckdev->ec;
397 union ec_response_get_next_data event_data = {};
398 int ret;
399
400 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_CURRENT,
401 EC_MKBP_EVENT_SWITCH, &event_data,
402 sizeof(event_data.switches));
403 if (ret)
404 return ret;
405
406 cros_ec_keyb_report_bs(ckdev, EV_SW,
407 get_unaligned_le32(&event_data.switches));
408
409 return 0;
410}
411
412/**
413 * cros_ec_keyb_resume - Resume the keyboard
414 *
415 * We use the resume notification as a chance to query the EC for switches.
416 *
417 * @dev: The keyboard device
418 *
419 * Returns 0 if no error or -error upon error.
420 */
421static __maybe_unused int cros_ec_keyb_resume(struct device *dev)
422{
423 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
424
425 if (ckdev->bs_idev)
426 return cros_ec_keyb_query_switches(ckdev);
427
428 return 0;
429}
430
431/**
432 * cros_ec_keyb_register_bs - Register non-matrix buttons/switches
433 *
434 * Handles all the bits of the keyboard driver related to non-matrix buttons
435 * and switches, including asking the EC about which are present and telling
436 * the kernel to expect them.
437 *
438 * If this device has no support for buttons and switches we'll return no error
439 * but the ckdev->bs_idev will remain NULL when this function exits.
440 *
441 * @ckdev: The keyboard device
442 *
443 * Returns 0 if no error or -error upon error.
444 */
445static int cros_ec_keyb_register_bs(struct cros_ec_keyb *ckdev)
446{
447 struct cros_ec_device *ec_dev = ckdev->ec;
448 struct device *dev = ckdev->dev;
449 struct input_dev *idev;
450 union ec_response_get_next_data event_data = {};
451 const char *phys;
452 u32 buttons;
453 u32 switches;
454 int ret;
455 int i;
456
457 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
458 EC_MKBP_EVENT_BUTTON, &event_data,
459 sizeof(event_data.buttons));
460 if (ret)
461 return ret;
462 buttons = get_unaligned_le32(&event_data.buttons);
463
464 ret = cros_ec_keyb_info(ec_dev, EC_MKBP_INFO_SUPPORTED,
465 EC_MKBP_EVENT_SWITCH, &event_data,
466 sizeof(event_data.switches));
467 if (ret)
468 return ret;
469 switches = get_unaligned_le32(&event_data.switches);
470
471 if (!buttons && !switches)
472 return 0;
473
474 /*
475 * We call the non-matrix buttons/switches 'input1', if present.
476 * Allocate phys before input dev, to ensure correct tear-down
477 * ordering.
478 */
479 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input1", ec_dev->phys_name);
480 if (!phys)
481 return -ENOMEM;
482
483 idev = devm_input_allocate_device(dev);
484 if (!idev)
485 return -ENOMEM;
486
487 idev->name = "cros_ec_buttons";
488 idev->phys = phys;
489 __set_bit(EV_REP, idev->evbit);
490
491 idev->id.bustype = BUS_VIRTUAL;
492 idev->id.version = 1;
493 idev->id.product = 0;
494 idev->dev.parent = dev;
495
496 input_set_drvdata(idev, ckdev);
497 ckdev->bs_idev = idev;
498
499 for (i = 0; i < ARRAY_SIZE(cros_ec_keyb_bs); i++) {
500 const struct cros_ec_bs_map *map = &cros_ec_keyb_bs[i];
501
502 if ((map->ev_type == EV_KEY && (buttons & BIT(map->bit))) ||
503 (map->ev_type == EV_SW && (switches & BIT(map->bit))))
504 input_set_capability(idev, map->ev_type, map->code);
505 }
506
507 ret = cros_ec_keyb_query_switches(ckdev);
508 if (ret) {
509 dev_err(dev, "cannot query switches\n");
510 return ret;
511 }
512
513 ret = input_register_device(ckdev->bs_idev);
514 if (ret) {
515 dev_err(dev, "cannot register input device\n");
516 return ret;
517 }
518
519 return 0;
520}
521
522/**
523 * cros_ec_keyb_register_bs - Register matrix keys
524 *
525 * Handles all the bits of the keyboard driver related to matrix keys.
526 *
527 * @ckdev: The keyboard device
528 *
529 * Returns 0 if no error or -error upon error.
530 */
531static int cros_ec_keyb_register_matrix(struct cros_ec_keyb *ckdev)
532{
533 struct cros_ec_device *ec_dev = ckdev->ec;
534 struct device *dev = ckdev->dev;
535 struct input_dev *idev;
536 const char *phys;
537 int err;
538 struct property *prop;
539 const __be32 *p;
540 u16 *physmap;
541 u32 key_pos;
542 int row, col;
543
544 err = matrix_keypad_parse_properties(dev, &ckdev->rows, &ckdev->cols);
545 if (err)
546 return err;
547
548 ckdev->valid_keys = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
549 if (!ckdev->valid_keys)
550 return -ENOMEM;
551
552 ckdev->old_kb_state = devm_kzalloc(dev, ckdev->cols, GFP_KERNEL);
553 if (!ckdev->old_kb_state)
554 return -ENOMEM;
555
556 /*
557 * We call the keyboard matrix 'input0'. Allocate phys before input
558 * dev, to ensure correct tear-down ordering.
559 */
560 phys = devm_kasprintf(dev, GFP_KERNEL, "%s/input0", ec_dev->phys_name);
561 if (!phys)
562 return -ENOMEM;
563
564 idev = devm_input_allocate_device(dev);
565 if (!idev)
566 return -ENOMEM;
567
568 idev->name = CROS_EC_DEV_NAME;
569 idev->phys = phys;
570 __set_bit(EV_REP, idev->evbit);
571
572 idev->id.bustype = BUS_VIRTUAL;
573 idev->id.version = 1;
574 idev->id.product = 0;
575 idev->dev.parent = dev;
576
577 ckdev->ghost_filter = of_property_read_bool(dev->of_node,
578 "google,needs-ghost-filter");
579
580 err = matrix_keypad_build_keymap(NULL, NULL, ckdev->rows, ckdev->cols,
581 NULL, idev);
582 if (err) {
583 dev_err(dev, "cannot build key matrix\n");
584 return err;
585 }
586
587 ckdev->row_shift = get_count_order(ckdev->cols);
588
589 input_set_capability(idev, EV_MSC, MSC_SCAN);
590 input_set_drvdata(idev, ckdev);
591 ckdev->idev = idev;
592 cros_ec_keyb_compute_valid_keys(ckdev);
593
594 physmap = ckdev->function_row_physmap;
595 of_property_for_each_u32(dev->of_node, "function-row-physmap",
596 prop, p, key_pos) {
597 if (ckdev->num_function_row_keys == MAX_NUM_TOP_ROW_KEYS) {
598 dev_warn(dev, "Only support up to %d top row keys\n",
599 MAX_NUM_TOP_ROW_KEYS);
600 break;
601 }
602 row = KEY_ROW(key_pos);
603 col = KEY_COL(key_pos);
604 *physmap = MATRIX_SCAN_CODE(row, col, ckdev->row_shift);
605 physmap++;
606 ckdev->num_function_row_keys++;
607 }
608
609 err = input_register_device(ckdev->idev);
610 if (err) {
611 dev_err(dev, "cannot register input device\n");
612 return err;
613 }
614
615 return 0;
616}
617
618static ssize_t function_row_physmap_show(struct device *dev,
619 struct device_attribute *attr,
620 char *buf)
621{
622 ssize_t size = 0;
623 int i;
624 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
625 u16 *physmap = ckdev->function_row_physmap;
626
627 for (i = 0; i < ckdev->num_function_row_keys; i++)
628 size += scnprintf(buf + size, PAGE_SIZE - size,
629 "%s%02X", size ? " " : "", physmap[i]);
630 if (size)
631 size += scnprintf(buf + size, PAGE_SIZE - size, "\n");
632
633 return size;
634}
635
636static DEVICE_ATTR_RO(function_row_physmap);
637
638static struct attribute *cros_ec_keyb_attrs[] = {
639 &dev_attr_function_row_physmap.attr,
640 NULL,
641};
642
643static umode_t cros_ec_keyb_attr_is_visible(struct kobject *kobj,
644 struct attribute *attr,
645 int n)
646{
647 struct device *dev = kobj_to_dev(kobj);
648 struct cros_ec_keyb *ckdev = dev_get_drvdata(dev);
649
650 if (attr == &dev_attr_function_row_physmap.attr &&
651 !ckdev->num_function_row_keys)
652 return 0;
653
654 return attr->mode;
655}
656
657static const struct attribute_group cros_ec_keyb_attr_group = {
658 .is_visible = cros_ec_keyb_attr_is_visible,
659 .attrs = cros_ec_keyb_attrs,
660};
661
662
663static int cros_ec_keyb_probe(struct platform_device *pdev)
664{
665 struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
666 struct device *dev = &pdev->dev;
667 struct cros_ec_keyb *ckdev;
668 int err;
669
670 if (!dev->of_node)
671 return -ENODEV;
672
673 ckdev = devm_kzalloc(dev, sizeof(*ckdev), GFP_KERNEL);
674 if (!ckdev)
675 return -ENOMEM;
676
677 ckdev->ec = ec;
678 ckdev->dev = dev;
679 dev_set_drvdata(dev, ckdev);
680
681 err = cros_ec_keyb_register_matrix(ckdev);
682 if (err) {
683 dev_err(dev, "cannot register matrix inputs: %d\n", err);
684 return err;
685 }
686
687 err = cros_ec_keyb_register_bs(ckdev);
688 if (err) {
689 dev_err(dev, "cannot register non-matrix inputs: %d\n", err);
690 return err;
691 }
692
693 err = devm_device_add_group(dev, &cros_ec_keyb_attr_group);
694 if (err) {
695 dev_err(dev, "failed to create attributes. err=%d\n", err);
696 return err;
697 }
698
699 ckdev->notifier.notifier_call = cros_ec_keyb_work;
700 err = blocking_notifier_chain_register(&ckdev->ec->event_notifier,
701 &ckdev->notifier);
702 if (err) {
703 dev_err(dev, "cannot register notifier: %d\n", err);
704 return err;
705 }
706
707 device_init_wakeup(ckdev->dev, true);
708 return 0;
709}
710
711static int cros_ec_keyb_remove(struct platform_device *pdev)
712{
713 struct cros_ec_keyb *ckdev = dev_get_drvdata(&pdev->dev);
714
715 blocking_notifier_chain_unregister(&ckdev->ec->event_notifier,
716 &ckdev->notifier);
717
718 return 0;
719}
720
721#ifdef CONFIG_OF
722static const struct of_device_id cros_ec_keyb_of_match[] = {
723 { .compatible = "google,cros-ec-keyb" },
724 {},
725};
726MODULE_DEVICE_TABLE(of, cros_ec_keyb_of_match);
727#endif
728
729static SIMPLE_DEV_PM_OPS(cros_ec_keyb_pm_ops, NULL, cros_ec_keyb_resume);
730
731static struct platform_driver cros_ec_keyb_driver = {
732 .probe = cros_ec_keyb_probe,
733 .remove = cros_ec_keyb_remove,
734 .driver = {
735 .name = "cros-ec-keyb",
736 .of_match_table = of_match_ptr(cros_ec_keyb_of_match),
737 .pm = &cros_ec_keyb_pm_ops,
738 },
739};
740
741module_platform_driver(cros_ec_keyb_driver);
742
743MODULE_LICENSE("GPL v2");
744MODULE_DESCRIPTION("ChromeOS EC keyboard driver");
745MODULE_ALIAS("platform:cros-ec-keyb");