Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | ed32c0c | 2015-10-18 21:17:10 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
Simon Glass | ed32c0c | 2015-10-18 21:17:10 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Patrick Delaunay | 8131335 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 6 | #define LOG_CATEGORY UCLASS_KEYBOARD |
| 7 | |
Simon Glass | ed32c0c | 2015-10-18 21:17:10 -0600 | [diff] [blame] | 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <keyboard.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | ed32c0c | 2015-10-18 21:17:10 -0600 | [diff] [blame] | 12 | |
| 13 | static 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 | |
| 24 | static 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 | |
| 35 | static 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 | |
| 50 | static 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 | |
| 65 | static 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 | |
| 88 | UCLASS_DRIVER(keyboard) = { |
| 89 | .id = UCLASS_KEYBOARD, |
| 90 | .name = "keyboard", |
| 91 | .pre_probe = keyboard_pre_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 92 | .per_device_auto = sizeof(struct keyboard_priv), |
Simon Glass | ed32c0c | 2015-10-18 21:17:10 -0600 | [diff] [blame] | 93 | }; |