Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/module.h>
0003 #include <linux/virtio.h>
0004 #include <linux/virtio_config.h>
0005 #include <linux/input.h>
0006 #include <linux/slab.h>
0007 
0008 #include <uapi/linux/virtio_ids.h>
0009 #include <uapi/linux/virtio_input.h>
0010 #include <linux/input/mt.h>
0011 
0012 struct virtio_input {
0013     struct virtio_device       *vdev;
0014     struct input_dev           *idev;
0015     char                       name[64];
0016     char                       serial[64];
0017     char                       phys[64];
0018     struct virtqueue           *evt, *sts;
0019     struct virtio_input_event  evts[64];
0020     spinlock_t                 lock;
0021     bool                       ready;
0022 };
0023 
0024 static void virtinput_queue_evtbuf(struct virtio_input *vi,
0025                    struct virtio_input_event *evtbuf)
0026 {
0027     struct scatterlist sg[1];
0028 
0029     sg_init_one(sg, evtbuf, sizeof(*evtbuf));
0030     virtqueue_add_inbuf(vi->evt, sg, 1, evtbuf, GFP_ATOMIC);
0031 }
0032 
0033 static void virtinput_recv_events(struct virtqueue *vq)
0034 {
0035     struct virtio_input *vi = vq->vdev->priv;
0036     struct virtio_input_event *event;
0037     unsigned long flags;
0038     unsigned int len;
0039 
0040     spin_lock_irqsave(&vi->lock, flags);
0041     if (vi->ready) {
0042         while ((event = virtqueue_get_buf(vi->evt, &len)) != NULL) {
0043             spin_unlock_irqrestore(&vi->lock, flags);
0044             input_event(vi->idev,
0045                     le16_to_cpu(event->type),
0046                     le16_to_cpu(event->code),
0047                     le32_to_cpu(event->value));
0048             spin_lock_irqsave(&vi->lock, flags);
0049             virtinput_queue_evtbuf(vi, event);
0050         }
0051         virtqueue_kick(vq);
0052     }
0053     spin_unlock_irqrestore(&vi->lock, flags);
0054 }
0055 
0056 /*
0057  * On error we are losing the status update, which isn't critical as
0058  * this is typically used for stuff like keyboard leds.
0059  */
0060 static int virtinput_send_status(struct virtio_input *vi,
0061                  u16 type, u16 code, s32 value)
0062 {
0063     struct virtio_input_event *stsbuf;
0064     struct scatterlist sg[1];
0065     unsigned long flags;
0066     int rc;
0067 
0068     /*
0069      * Since 29cc309d8bf1 (HID: hid-multitouch: forward MSC_TIMESTAMP),
0070      * EV_MSC/MSC_TIMESTAMP is added to each before EV_SYN event.
0071      * EV_MSC is configured as INPUT_PASS_TO_ALL.
0072      * In case of touch device:
0073      *   BE pass EV_MSC/MSC_TIMESTAMP to FE on receiving event from evdev.
0074      *   FE pass EV_MSC/MSC_TIMESTAMP back to BE.
0075      *   BE writes EV_MSC/MSC_TIMESTAMP to evdev due to INPUT_PASS_TO_ALL.
0076      *   BE receives extra EV_MSC/MSC_TIMESTAMP and pass to FE.
0077      *   >>> Each new frame becomes larger and larger.
0078      * Disable EV_MSC/MSC_TIMESTAMP forwarding for MT.
0079      */
0080     if (vi->idev->mt && type == EV_MSC && code == MSC_TIMESTAMP)
0081         return 0;
0082 
0083     stsbuf = kzalloc(sizeof(*stsbuf), GFP_ATOMIC);
0084     if (!stsbuf)
0085         return -ENOMEM;
0086 
0087     stsbuf->type  = cpu_to_le16(type);
0088     stsbuf->code  = cpu_to_le16(code);
0089     stsbuf->value = cpu_to_le32(value);
0090     sg_init_one(sg, stsbuf, sizeof(*stsbuf));
0091 
0092     spin_lock_irqsave(&vi->lock, flags);
0093     if (vi->ready) {
0094         rc = virtqueue_add_outbuf(vi->sts, sg, 1, stsbuf, GFP_ATOMIC);
0095         virtqueue_kick(vi->sts);
0096     } else {
0097         rc = -ENODEV;
0098     }
0099     spin_unlock_irqrestore(&vi->lock, flags);
0100 
0101     if (rc != 0)
0102         kfree(stsbuf);
0103     return rc;
0104 }
0105 
0106 static void virtinput_recv_status(struct virtqueue *vq)
0107 {
0108     struct virtio_input *vi = vq->vdev->priv;
0109     struct virtio_input_event *stsbuf;
0110     unsigned long flags;
0111     unsigned int len;
0112 
0113     spin_lock_irqsave(&vi->lock, flags);
0114     while ((stsbuf = virtqueue_get_buf(vi->sts, &len)) != NULL)
0115         kfree(stsbuf);
0116     spin_unlock_irqrestore(&vi->lock, flags);
0117 }
0118 
0119 static int virtinput_status(struct input_dev *idev, unsigned int type,
0120                 unsigned int code, int value)
0121 {
0122     struct virtio_input *vi = input_get_drvdata(idev);
0123 
0124     return virtinput_send_status(vi, type, code, value);
0125 }
0126 
0127 static u8 virtinput_cfg_select(struct virtio_input *vi,
0128                    u8 select, u8 subsel)
0129 {
0130     u8 size;
0131 
0132     virtio_cwrite_le(vi->vdev, struct virtio_input_config, select, &select);
0133     virtio_cwrite_le(vi->vdev, struct virtio_input_config, subsel, &subsel);
0134     virtio_cread_le(vi->vdev, struct virtio_input_config, size, &size);
0135     return size;
0136 }
0137 
0138 static void virtinput_cfg_bits(struct virtio_input *vi, int select, int subsel,
0139                    unsigned long *bits, unsigned int bitcount)
0140 {
0141     unsigned int bit;
0142     u8 *virtio_bits;
0143     u8 bytes;
0144 
0145     bytes = virtinput_cfg_select(vi, select, subsel);
0146     if (!bytes)
0147         return;
0148     if (bitcount > bytes * 8)
0149         bitcount = bytes * 8;
0150 
0151     /*
0152      * Bitmap in virtio config space is a simple stream of bytes,
0153      * with the first byte carrying bits 0-7, second bits 8-15 and
0154      * so on.
0155      */
0156     virtio_bits = kzalloc(bytes, GFP_KERNEL);
0157     if (!virtio_bits)
0158         return;
0159     virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
0160                           u.bitmap),
0161                virtio_bits, bytes);
0162     for (bit = 0; bit < bitcount; bit++) {
0163         if (virtio_bits[bit / 8] & (1 << (bit % 8)))
0164             __set_bit(bit, bits);
0165     }
0166     kfree(virtio_bits);
0167 
0168     if (select == VIRTIO_INPUT_CFG_EV_BITS)
0169         __set_bit(subsel, vi->idev->evbit);
0170 }
0171 
0172 static void virtinput_cfg_abs(struct virtio_input *vi, int abs)
0173 {
0174     u32 mi, ma, re, fu, fl;
0175 
0176     virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ABS_INFO, abs);
0177     virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.min, &mi);
0178     virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.max, &ma);
0179     virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.res, &re);
0180     virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.fuzz, &fu);
0181     virtio_cread_le(vi->vdev, struct virtio_input_config, u.abs.flat, &fl);
0182     input_set_abs_params(vi->idev, abs, mi, ma, fu, fl);
0183     input_abs_set_res(vi->idev, abs, re);
0184 }
0185 
0186 static int virtinput_init_vqs(struct virtio_input *vi)
0187 {
0188     struct virtqueue *vqs[2];
0189     vq_callback_t *cbs[] = { virtinput_recv_events,
0190                  virtinput_recv_status };
0191     static const char * const names[] = { "events", "status" };
0192     int err;
0193 
0194     err = virtio_find_vqs(vi->vdev, 2, vqs, cbs, names, NULL);
0195     if (err)
0196         return err;
0197     vi->evt = vqs[0];
0198     vi->sts = vqs[1];
0199 
0200     return 0;
0201 }
0202 
0203 static void virtinput_fill_evt(struct virtio_input *vi)
0204 {
0205     unsigned long flags;
0206     int i, size;
0207 
0208     spin_lock_irqsave(&vi->lock, flags);
0209     size = virtqueue_get_vring_size(vi->evt);
0210     if (size > ARRAY_SIZE(vi->evts))
0211         size = ARRAY_SIZE(vi->evts);
0212     for (i = 0; i < size; i++)
0213         virtinput_queue_evtbuf(vi, &vi->evts[i]);
0214     virtqueue_kick(vi->evt);
0215     spin_unlock_irqrestore(&vi->lock, flags);
0216 }
0217 
0218 static int virtinput_probe(struct virtio_device *vdev)
0219 {
0220     struct virtio_input *vi;
0221     unsigned long flags;
0222     size_t size;
0223     int abs, err, nslots;
0224 
0225     if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1))
0226         return -ENODEV;
0227 
0228     vi = kzalloc(sizeof(*vi), GFP_KERNEL);
0229     if (!vi)
0230         return -ENOMEM;
0231 
0232     vdev->priv = vi;
0233     vi->vdev = vdev;
0234     spin_lock_init(&vi->lock);
0235 
0236     err = virtinput_init_vqs(vi);
0237     if (err)
0238         goto err_init_vq;
0239 
0240     vi->idev = input_allocate_device();
0241     if (!vi->idev) {
0242         err = -ENOMEM;
0243         goto err_input_alloc;
0244     }
0245     input_set_drvdata(vi->idev, vi);
0246 
0247     size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_NAME, 0);
0248     virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
0249                           u.string),
0250                vi->name, min(size, sizeof(vi->name)));
0251     size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_SERIAL, 0);
0252     virtio_cread_bytes(vi->vdev, offsetof(struct virtio_input_config,
0253                           u.string),
0254                vi->serial, min(size, sizeof(vi->serial)));
0255     snprintf(vi->phys, sizeof(vi->phys),
0256          "virtio%d/input0", vdev->index);
0257     vi->idev->name = vi->name;
0258     vi->idev->phys = vi->phys;
0259     vi->idev->uniq = vi->serial;
0260 
0261     size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_ID_DEVIDS, 0);
0262     if (size >= sizeof(struct virtio_input_devids)) {
0263         virtio_cread_le(vi->vdev, struct virtio_input_config,
0264                 u.ids.bustype, &vi->idev->id.bustype);
0265         virtio_cread_le(vi->vdev, struct virtio_input_config,
0266                 u.ids.vendor, &vi->idev->id.vendor);
0267         virtio_cread_le(vi->vdev, struct virtio_input_config,
0268                 u.ids.product, &vi->idev->id.product);
0269         virtio_cread_le(vi->vdev, struct virtio_input_config,
0270                 u.ids.version, &vi->idev->id.version);
0271     } else {
0272         vi->idev->id.bustype = BUS_VIRTUAL;
0273     }
0274 
0275     virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_PROP_BITS, 0,
0276                vi->idev->propbit, INPUT_PROP_CNT);
0277     size = virtinput_cfg_select(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REP);
0278     if (size)
0279         __set_bit(EV_REP, vi->idev->evbit);
0280 
0281     vi->idev->dev.parent = &vdev->dev;
0282     vi->idev->event = virtinput_status;
0283 
0284     /* device -> kernel */
0285     virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_KEY,
0286                vi->idev->keybit, KEY_CNT);
0287     virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_REL,
0288                vi->idev->relbit, REL_CNT);
0289     virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_ABS,
0290                vi->idev->absbit, ABS_CNT);
0291     virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_MSC,
0292                vi->idev->mscbit, MSC_CNT);
0293     virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SW,
0294                vi->idev->swbit,  SW_CNT);
0295 
0296     /* kernel -> device */
0297     virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_LED,
0298                vi->idev->ledbit, LED_CNT);
0299     virtinput_cfg_bits(vi, VIRTIO_INPUT_CFG_EV_BITS, EV_SND,
0300                vi->idev->sndbit, SND_CNT);
0301 
0302     if (test_bit(EV_ABS, vi->idev->evbit)) {
0303         for (abs = 0; abs < ABS_CNT; abs++) {
0304             if (!test_bit(abs, vi->idev->absbit))
0305                 continue;
0306             virtinput_cfg_abs(vi, abs);
0307         }
0308 
0309         if (test_bit(ABS_MT_SLOT, vi->idev->absbit)) {
0310             nslots = input_abs_get_max(vi->idev, ABS_MT_SLOT) + 1;
0311             err = input_mt_init_slots(vi->idev, nslots, 0);
0312             if (err)
0313                 goto err_mt_init_slots;
0314         }
0315     }
0316 
0317     virtio_device_ready(vdev);
0318     vi->ready = true;
0319     err = input_register_device(vi->idev);
0320     if (err)
0321         goto err_input_register;
0322 
0323     virtinput_fill_evt(vi);
0324     return 0;
0325 
0326 err_input_register:
0327     spin_lock_irqsave(&vi->lock, flags);
0328     vi->ready = false;
0329     spin_unlock_irqrestore(&vi->lock, flags);
0330 err_mt_init_slots:
0331     input_free_device(vi->idev);
0332 err_input_alloc:
0333     vdev->config->del_vqs(vdev);
0334 err_init_vq:
0335     kfree(vi);
0336     return err;
0337 }
0338 
0339 static void virtinput_remove(struct virtio_device *vdev)
0340 {
0341     struct virtio_input *vi = vdev->priv;
0342     void *buf;
0343     unsigned long flags;
0344 
0345     spin_lock_irqsave(&vi->lock, flags);
0346     vi->ready = false;
0347     spin_unlock_irqrestore(&vi->lock, flags);
0348 
0349     input_unregister_device(vi->idev);
0350     virtio_reset_device(vdev);
0351     while ((buf = virtqueue_detach_unused_buf(vi->sts)) != NULL)
0352         kfree(buf);
0353     vdev->config->del_vqs(vdev);
0354     kfree(vi);
0355 }
0356 
0357 #ifdef CONFIG_PM_SLEEP
0358 static int virtinput_freeze(struct virtio_device *vdev)
0359 {
0360     struct virtio_input *vi = vdev->priv;
0361     unsigned long flags;
0362 
0363     spin_lock_irqsave(&vi->lock, flags);
0364     vi->ready = false;
0365     spin_unlock_irqrestore(&vi->lock, flags);
0366 
0367     vdev->config->del_vqs(vdev);
0368     return 0;
0369 }
0370 
0371 static int virtinput_restore(struct virtio_device *vdev)
0372 {
0373     struct virtio_input *vi = vdev->priv;
0374     int err;
0375 
0376     err = virtinput_init_vqs(vi);
0377     if (err)
0378         return err;
0379 
0380     virtio_device_ready(vdev);
0381     vi->ready = true;
0382     virtinput_fill_evt(vi);
0383     return 0;
0384 }
0385 #endif
0386 
0387 static unsigned int features[] = {
0388     /* none */
0389 };
0390 static const struct virtio_device_id id_table[] = {
0391     { VIRTIO_ID_INPUT, VIRTIO_DEV_ANY_ID },
0392     { 0 },
0393 };
0394 
0395 static struct virtio_driver virtio_input_driver = {
0396     .driver.name         = KBUILD_MODNAME,
0397     .driver.owner        = THIS_MODULE,
0398     .feature_table       = features,
0399     .feature_table_size  = ARRAY_SIZE(features),
0400     .id_table            = id_table,
0401     .probe               = virtinput_probe,
0402     .remove              = virtinput_remove,
0403 #ifdef CONFIG_PM_SLEEP
0404     .freeze              = virtinput_freeze,
0405     .restore             = virtinput_restore,
0406 #endif
0407 };
0408 
0409 module_virtio_driver(virtio_input_driver);
0410 MODULE_DEVICE_TABLE(virtio, id_table);
0411 
0412 MODULE_LICENSE("GPL");
0413 MODULE_DESCRIPTION("Virtio input device driver");
0414 MODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");