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