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