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 <dm.h> |
| 9 | #include <keyboard.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | ed32c0c | 2015-10-18 21:17:10 -0600 | [diff] [blame] | 11 | |
| 12 | static 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 | |
| 23 | static 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 | |
| 34 | static 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 | |
| 49 | static 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 | |
| 64 | static 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 | |
| 87 | UCLASS_DRIVER(keyboard) = { |
| 88 | .id = UCLASS_KEYBOARD, |
| 89 | .name = "keyboard", |
| 90 | .pre_probe = keyboard_pre_probe, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 91 | .per_device_auto = sizeof(struct keyboard_priv), |
Simon Glass | ed32c0c | 2015-10-18 21:17:10 -0600 | [diff] [blame] | 92 | }; |