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