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 | |
| 6 | #include <common.h> |
| 7 | #include <dm.h> |
| 8 | #include <keyboard.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Simon Glass | ed32c0c | 2015-10-18 21:17:10 -0600 | [diff] [blame] | 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 | }; |