blob: df9ee8f7d659b304540a29f563af52038965d62f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassed32c0c2015-10-18 21:17:10 -06002/*
3 * Copyright (c) 2015 Google, Inc
Simon Glassed32c0c2015-10-18 21:17:10 -06004 */
5
Patrick Delaunay81313352021-04-27 11:02:19 +02006#define LOG_CATEGORY UCLASS_KEYBOARD
7
Simon Glassed32c0c2015-10-18 21:17:10 -06008#include <dm.h>
9#include <keyboard.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glassed32c0c2015-10-18 21:17:10 -060011
12static int keyboard_start(struct stdio_dev *sdev)
13{
14 struct udevice *dev = sdev->priv;
15 struct keyboard_ops *ops = keyboard_get_ops(dev);
16
17 if (ops->start)
18 return ops->start(dev);
19
20 return 0;
21}
22
23static int keyboard_stop(struct stdio_dev *sdev)
24{
25 struct udevice *dev = sdev->priv;
26 struct keyboard_ops *ops = keyboard_get_ops(dev);
27
28 if (ops->stop)
29 return ops->stop(dev);
30
31 return 0;
32}
33
34static int keyboard_tstc(struct stdio_dev *sdev)
35{
36 struct udevice *dev = sdev->priv;
37 struct keyboard_priv *priv = dev_get_uclass_priv(dev);
38 struct keyboard_ops *ops = keyboard_get_ops(dev);
39
40 /* Just get input to do this for us if we can */
41 if (priv->input.dev)
42 return input_tstc(&priv->input);
43 else if (ops->tstc)
44 return ops->tstc(dev);
45
46 return -ENOSYS;
47}
48
49static int keyboard_getc(struct stdio_dev *sdev)
50{
51 struct udevice *dev = sdev->priv;
52 struct keyboard_priv *priv = dev_get_uclass_priv(dev);
53 struct keyboard_ops *ops = keyboard_get_ops(dev);
54
55 /* Just get input to do this for us if we can */
56 if (priv->input.dev)
57 return input_getc(&priv->input);
58 else if (ops->getc)
59 return ops->getc(dev);
60
61 return -ENOSYS;
62}
63
64static int keyboard_pre_probe(struct udevice *dev)
65{
66 struct keyboard_priv *priv = dev_get_uclass_priv(dev);
67 struct stdio_dev *sdev = &priv->sdev;
68 int ret;
69
70 strlcpy(sdev->name, dev->name, sizeof(sdev->name));
71 sdev->flags = DEV_FLAGS_INPUT;
72 sdev->getc = keyboard_getc;
73 sdev->tstc = keyboard_tstc;
74 sdev->start = keyboard_start;
75 sdev->stop = keyboard_stop;
76 sdev->priv = dev;
77 ret = input_init(&priv->input, 0);
78 if (ret) {
79 debug("%s: Cannot set up input, ret=%d - please add DEBUG to drivers/input/input.c to figure out the cause\n",
80 __func__, ret);
81 return ret;
82 }
83
84 return 0;
85}
86
87UCLASS_DRIVER(keyboard) = {
88 .id = UCLASS_KEYBOARD,
89 .name = "keyboard",
90 .pre_probe = keyboard_pre_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -070091 .per_device_auto = sizeof(struct keyboard_priv),
Simon Glassed32c0c2015-10-18 21:17:10 -060092};