|
| 1 | +/* |
| 2 | + * Copyright (C) 2018 Intel Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * Redistribution and use in source and binary forms, with or without |
| 5 | + * modification, are permitted provided that the following conditions |
| 6 | + * are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer. |
| 10 | + * * Redistributions in binary form must reproduce the above copyright |
| 11 | + * notice, this list of conditions and the following disclaimer in |
| 12 | + * the documentation and/or other materials provided with the |
| 13 | + * distribution. |
| 14 | + * * Neither the name of Intel Corporation nor the names of its |
| 15 | + * contributors may be used to endorse or promote products derived |
| 16 | + * from this software without specific prior written permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * |
| 30 | + */ |
| 31 | + |
| 32 | +#include <sys/cdefs.h> |
| 33 | +#include <sys/param.h> |
| 34 | +#include <sys/types.h> |
| 35 | +#include <err.h> |
| 36 | +#include <errno.h> |
| 37 | +#include <fcntl.h> |
| 38 | +#include <stdio.h> |
| 39 | +#include <stdlib.h> |
| 40 | +#include <stddef.h> |
| 41 | +#include <stdbool.h> |
| 42 | +#include <string.h> |
| 43 | +#include <unistd.h> |
| 44 | +#include <assert.h> |
| 45 | +#include <pthread.h> |
| 46 | + |
| 47 | +#include "dm.h" |
| 48 | +#include "pci_core.h" |
| 49 | +#include "virtio.h" |
| 50 | +#include "mevent.h" |
| 51 | +#include <linux/input.h> |
| 52 | + |
| 53 | +static int virtio_input_debug; |
| 54 | +#define DPRINTF(params) do { if (virtio_input_debug) printf params; } while (0) |
| 55 | +#define WPRINTF(params) (printf params) |
| 56 | + |
| 57 | +/* |
| 58 | + * Queue definitions. |
| 59 | + */ |
| 60 | +#define VIRTIO_INPUT_EVENT_QUEUE 0 |
| 61 | +#define VIRTIO_INPUT_STATUS_QUEUE 1 |
| 62 | +#define VIRTIO_INPUT_MAXQ 2 |
| 63 | + |
| 64 | +/* |
| 65 | + * Virtqueue size. |
| 66 | + */ |
| 67 | +#define VIRTIO_INPUT_RINGSZ 64 |
| 68 | + |
| 69 | +/* |
| 70 | + * Default size of the buffer used to hold events between SYN |
| 71 | + */ |
| 72 | +#define VIRTIO_INPUT_PACKET_SIZE 10 |
| 73 | + |
| 74 | +/* |
| 75 | + * Host capabilities |
| 76 | + */ |
| 77 | +#define VIRTIO_INPUT_S_HOSTCAPS (VIRTIO_F_VERSION_1) |
| 78 | + |
| 79 | +enum virtio_input_config_select { |
| 80 | + VIRTIO_INPUT_CFG_UNSET = 0x00, |
| 81 | + VIRTIO_INPUT_CFG_ID_NAME = 0x01, |
| 82 | + VIRTIO_INPUT_CFG_ID_SERIAL = 0x02, |
| 83 | + VIRTIO_INPUT_CFG_ID_DEVIDS = 0x03, |
| 84 | + VIRTIO_INPUT_CFG_PROP_BITS = 0x10, |
| 85 | + VIRTIO_INPUT_CFG_EV_BITS = 0x11, |
| 86 | + VIRTIO_INPUT_CFG_ABS_INFO = 0x12, |
| 87 | +}; |
| 88 | + |
| 89 | +struct virtio_input_absinfo { |
| 90 | + uint32_t min; |
| 91 | + uint32_t max; |
| 92 | + uint32_t fuzz; |
| 93 | + uint32_t flat; |
| 94 | + uint32_t res; |
| 95 | +}; |
| 96 | + |
| 97 | +struct virtio_input_devids { |
| 98 | + uint16_t bustype; |
| 99 | + uint16_t vendor; |
| 100 | + uint16_t product; |
| 101 | + uint16_t version; |
| 102 | +}; |
| 103 | + |
| 104 | +struct virtio_input_event { |
| 105 | + uint16_t type; |
| 106 | + uint16_t code; |
| 107 | + uint32_t value; |
| 108 | +}; |
| 109 | + |
| 110 | +/* |
| 111 | + * Device-specific configuration registers |
| 112 | + * To query a specific piece of configuration information FE driver sets |
| 113 | + * "select" and "subsel" accordingly, information size is returned in "size" |
| 114 | + * and information data is returned in union "u" |
| 115 | + */ |
| 116 | +struct virtio_input_config { |
| 117 | + uint8_t select; |
| 118 | + uint8_t subsel; |
| 119 | + uint8_t size; |
| 120 | + uint8_t reserved[5]; |
| 121 | + union { |
| 122 | + char string[128]; |
| 123 | + uint8_t bitmap[128]; |
| 124 | + struct virtio_input_absinfo abs; |
| 125 | + struct virtio_input_devids ids; |
| 126 | + } u; |
| 127 | +}; |
| 128 | + |
| 129 | +struct virtio_input_event_elem { |
| 130 | + struct virtio_input_event event; |
| 131 | + struct iovec iov; |
| 132 | + uint16_t idx; |
| 133 | +}; |
| 134 | + |
| 135 | +/* |
| 136 | + * Per-device struct |
| 137 | + */ |
| 138 | +struct virtio_input { |
| 139 | + struct virtio_base base; |
| 140 | + struct virtio_vq_info queues[VIRTIO_INPUT_MAXQ]; |
| 141 | + pthread_mutex_t mtx; |
| 142 | + struct mevent *mevp; |
| 143 | + uint64_t features; |
| 144 | + struct virtio_input_config cfg; |
| 145 | + char *evdev; |
| 146 | + char *serial; |
| 147 | + int fd; |
| 148 | + bool ready; |
| 149 | + |
| 150 | + struct virtio_input_event_elem *event_queue; |
| 151 | + uint32_t event_qsize; |
| 152 | + uint32_t event_qindex; |
| 153 | +}; |
| 154 | + |
| 155 | +static void virtio_input_reset(void *); |
| 156 | +static void virtio_input_neg_features(void *, uint64_t); |
| 157 | +static void virtio_input_set_status(void *, uint64_t); |
| 158 | +static int virtio_input_cfgread(void *, int, int, uint32_t *); |
| 159 | +static int virtio_input_cfgwrite(void *, int, int, uint32_t); |
| 160 | + |
| 161 | +static struct virtio_ops virtio_input_ops = { |
| 162 | + "virtio_input", /* our name */ |
| 163 | + VIRTIO_INPUT_MAXQ, /* we support VTCON_MAXQ virtqueues */ |
| 164 | + sizeof(struct virtio_input_config), /* config reg size */ |
| 165 | + virtio_input_reset, /* reset */ |
| 166 | + NULL, /* device-wide qnotify */ |
| 167 | + virtio_input_cfgread, /* read virtio config */ |
| 168 | + virtio_input_cfgwrite, /* write virtio config */ |
| 169 | + virtio_input_neg_features, /* apply negotiated features */ |
| 170 | + virtio_input_set_status, /* called on guest set status */ |
| 171 | + VIRTIO_INPUT_S_HOSTCAPS, /* our capabilities */ |
| 172 | +}; |
| 173 | + |
| 174 | +static void |
| 175 | +virtio_input_reset(void *vdev) |
| 176 | +{ |
| 177 | + /* to be implemented */ |
| 178 | +} |
| 179 | + |
| 180 | +static void |
| 181 | +virtio_input_neg_features(void *vdev, uint64_t negotiated_features) |
| 182 | +{ |
| 183 | + /* to be implemented */ |
| 184 | +} |
| 185 | + |
| 186 | +static void |
| 187 | +virtio_input_set_status(void *vdev, uint64_t status) |
| 188 | +{ |
| 189 | + /* to be implemented */ |
| 190 | +} |
| 191 | + |
| 192 | +static int |
| 193 | +virtio_input_cfgread(void *vdev, int offset, int size, uint32_t *retval) |
| 194 | +{ |
| 195 | + /* to be implemented */ |
| 196 | + return 0; |
| 197 | +} |
| 198 | + |
| 199 | +static int |
| 200 | +virtio_input_cfgwrite(void *vdev, int offset, int size, uint32_t val) |
| 201 | +{ |
| 202 | + /* to be implemented */ |
| 203 | + return 0; |
| 204 | +} |
| 205 | + |
| 206 | +static int |
| 207 | +virtio_input_init(struct vmctx *ctx, struct pci_vdev *dev, char *opts) |
| 208 | +{ |
| 209 | + /* to be implemented */ |
| 210 | + DPRINTF(("%s\n", __func__)); |
| 211 | + (void)virtio_input_ops; |
| 212 | + return 0; |
| 213 | +} |
| 214 | + |
| 215 | +static void |
| 216 | +virtio_input_deinit(struct vmctx *ctx, struct pci_vdev *dev, char *opts) |
| 217 | +{ |
| 218 | + /* to be implemented */ |
| 219 | +} |
| 220 | + |
| 221 | +struct pci_vdev_ops pci_ops_virtio_input = { |
| 222 | + .class_name = "virtio-input", |
| 223 | + .vdev_init = virtio_input_init, |
| 224 | + .vdev_deinit = virtio_input_deinit, |
| 225 | + .vdev_barwrite = virtio_pci_write, |
| 226 | + .vdev_barread = virtio_pci_read |
| 227 | +}; |
| 228 | +DEFINE_PCI_DEVTYPE(pci_ops_virtio_input); |
0 commit comments